linux/drivers/gpu/drm/drm_gem_dma_helper.c

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * drm gem DMA helper functions
 *
 * Copyright (C) 2012 Sascha Hauer, Pengutronix
 *
 * Based on Samsung Exynos code
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 */

#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/export.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>

#include <drm/drm.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_gem_dma_helper.h>
#include <drm/drm_vma_manager.h>

/**
 * DOC: dma helpers
 *
 * The DRM GEM/DMA helpers are a means to provide buffer objects that are
 * presented to the device as a contiguous chunk of memory. This is useful
 * for devices that do not support scatter-gather DMA (either directly or
 * by using an intimately attached IOMMU).
 *
 * For devices that access the memory bus through an (external) IOMMU then
 * the buffer objects are allocated using a traditional page-based
 * allocator and may be scattered through physical memory. However they
 * are contiguous in the IOVA space so appear contiguous to devices using
 * them.
 *
 * For other devices then the helpers rely on CMA to provide buffer
 * objects that are physically contiguous in memory.
 *
 * For GEM callback helpers in struct &drm_gem_object functions, see likewise
 * named functions with an _object_ infix (e.g., drm_gem_dma_object_vmap() wraps
 * drm_gem_dma_vmap()). These helpers perform the necessary type conversion.
 */

static const struct drm_gem_object_funcs drm_gem_dma_default_funcs =;

/**
 * __drm_gem_dma_create - Create a GEM DMA object without allocating memory
 * @drm: DRM device
 * @size: size of the object to allocate
 * @private: true if used for internal purposes
 *
 * This function creates and initializes a GEM DMA object of the given size,
 * but doesn't allocate any memory to back the object.
 *
 * Returns:
 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative
 * error code on failure.
 */
static struct drm_gem_dma_object *
__drm_gem_dma_create(struct drm_device *drm, size_t size, bool private)
{}

/**
 * drm_gem_dma_create - allocate an object with the given size
 * @drm: DRM device
 * @size: size of the object to allocate
 *
 * This function creates a DMA GEM object and allocates memory as backing store.
 * The allocated memory will occupy a contiguous chunk of bus address space.
 *
 * For devices that are directly connected to the memory bus then the allocated
 * memory will be physically contiguous. For devices that access through an
 * IOMMU, then the allocated memory is not expected to be physically contiguous
 * because having contiguous IOVAs is sufficient to meet a devices DMA
 * requirements.
 *
 * Returns:
 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative
 * error code on failure.
 */
struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,
					      size_t size)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_create_with_handle - allocate an object with the given size and
 *     return a GEM handle to it
 * @file_priv: DRM file-private structure to register the handle for
 * @drm: DRM device
 * @size: size of the object to allocate
 * @handle: return location for the GEM handle
 *
 * This function creates a DMA GEM object, allocating a chunk of memory as
 * backing store. The GEM object is then added to the list of object associated
 * with the given file and a handle to it is returned.
 *
 * The allocated memory will occupy a contiguous chunk of bus address space.
 * See drm_gem_dma_create() for more details.
 *
 * Returns:
 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative
 * error code on failure.
 */
static struct drm_gem_dma_object *
drm_gem_dma_create_with_handle(struct drm_file *file_priv,
			       struct drm_device *drm, size_t size,
			       uint32_t *handle)
{}

/**
 * drm_gem_dma_free - free resources associated with a DMA GEM object
 * @dma_obj: DMA GEM object to free
 *
 * This function frees the backing memory of the DMA GEM object, cleans up the
 * GEM object state and frees the memory used to store the object itself.
 * If the buffer is imported and the virtual address is set, it is released.
 */
void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_dumb_create_internal - create a dumb buffer object
 * @file_priv: DRM file-private structure to create the dumb buffer for
 * @drm: DRM device
 * @args: IOCTL data
 *
 * This aligns the pitch and size arguments to the minimum required. This is
 * an internal helper that can be wrapped by a driver to account for hardware
 * with more specific alignment requirements. It should not be used directly
 * as their &drm_driver.dumb_create callback.
 *
 * Returns:
 * 0 on success or a negative error code on failure.
 */
int drm_gem_dma_dumb_create_internal(struct drm_file *file_priv,
				     struct drm_device *drm,
				     struct drm_mode_create_dumb *args)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_dumb_create - create a dumb buffer object
 * @file_priv: DRM file-private structure to create the dumb buffer for
 * @drm: DRM device
 * @args: IOCTL data
 *
 * This function computes the pitch of the dumb buffer and rounds it up to an
 * integer number of bytes per pixel. Drivers for hardware that doesn't have
 * any additional restrictions on the pitch can directly use this function as
 * their &drm_driver.dumb_create callback.
 *
 * For hardware with additional restrictions, drivers can adjust the fields
 * set up by userspace and pass the IOCTL data along to the
 * drm_gem_dma_dumb_create_internal() function.
 *
 * Returns:
 * 0 on success or a negative error code on failure.
 */
int drm_gem_dma_dumb_create(struct drm_file *file_priv,
			    struct drm_device *drm,
			    struct drm_mode_create_dumb *args)
{}
EXPORT_SYMBOL_GPL();

const struct vm_operations_struct drm_gem_dma_vm_ops =;
EXPORT_SYMBOL_GPL();

#ifndef CONFIG_MMU
/**
 * drm_gem_dma_get_unmapped_area - propose address for mapping in noMMU cases
 * @filp: file object
 * @addr: memory address
 * @len: buffer size
 * @pgoff: page offset
 * @flags: memory flags
 *
 * This function is used in noMMU platforms to propose address mapping
 * for a given buffer.
 * It's intended to be used as a direct handler for the struct
 * &file_operations.get_unmapped_area operation.
 *
 * Returns:
 * mapping address on success or a negative error code on failure.
 */
unsigned long drm_gem_dma_get_unmapped_area(struct file *filp,
					    unsigned long addr,
					    unsigned long len,
					    unsigned long pgoff,
					    unsigned long flags)
{
	struct drm_gem_dma_object *dma_obj;
	struct drm_gem_object *obj = NULL;
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->minor->dev;
	struct drm_vma_offset_node *node;

	if (drm_dev_is_unplugged(dev))
		return -ENODEV;

	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
						  pgoff,
						  len >> PAGE_SHIFT);
	if (likely(node)) {
		obj = container_of(node, struct drm_gem_object, vma_node);
		/*
		 * When the object is being freed, after it hits 0-refcnt it
		 * proceeds to tear down the object. In the process it will
		 * attempt to remove the VMA offset and so acquire this
		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
		 * that matches our range, we know it is in the process of being
		 * destroyed and will be freed as soon as we release the lock -
		 * so we have to check for the 0-refcnted object and treat it as
		 * invalid.
		 */
		if (!kref_get_unless_zero(&obj->refcount))
			obj = NULL;
	}

	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);

	if (!obj)
		return -EINVAL;

	if (!drm_vma_node_is_allowed(node, priv)) {
		drm_gem_object_put(obj);
		return -EACCES;
	}

	dma_obj = to_drm_gem_dma_obj(obj);

	drm_gem_object_put(obj);

	return dma_obj->vaddr ? (unsigned long)dma_obj->vaddr : -EINVAL;
}
EXPORT_SYMBOL_GPL(drm_gem_dma_get_unmapped_area);
#endif

/**
 * drm_gem_dma_print_info() - Print &drm_gem_dma_object info for debugfs
 * @dma_obj: DMA GEM object
 * @p: DRM printer
 * @indent: Tab indentation level
 *
 * This function prints dma_addr and vaddr for use in e.g. debugfs output.
 */
void drm_gem_dma_print_info(const struct drm_gem_dma_object *dma_obj,
			    struct drm_printer *p, unsigned int indent)
{}
EXPORT_SYMBOL();

/**
 * drm_gem_dma_get_sg_table - provide a scatter/gather table of pinned
 *     pages for a DMA GEM object
 * @dma_obj: DMA GEM object
 *
 * This function exports a scatter/gather table by calling the standard
 * DMA mapping API.
 *
 * Returns:
 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
 */
struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_prime_import_sg_table - produce a DMA GEM object from another
 *     driver's scatter/gather table of pinned pages
 * @dev: device to import into
 * @attach: DMA-BUF attachment
 * @sgt: scatter/gather table of pinned pages
 *
 * This function imports a scatter/gather table exported via DMA-BUF by
 * another driver. Imported buffers must be physically contiguous in memory
 * (i.e. the scatter/gather table must contain a single entry). Drivers that
 * use the DMA helpers should set this as their
 * &drm_driver.gem_prime_import_sg_table callback.
 *
 * Returns:
 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
 * error code on failure.
 */
struct drm_gem_object *
drm_gem_dma_prime_import_sg_table(struct drm_device *dev,
				  struct dma_buf_attachment *attach,
				  struct sg_table *sgt)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_vmap - map a DMA GEM object into the kernel's virtual
 *     address space
 * @dma_obj: DMA GEM object
 * @map: Returns the kernel virtual address of the DMA GEM object's backing
 *       store.
 *
 * This function maps a buffer into the kernel's virtual address space.
 * Since the DMA buffers are already mapped into the kernel virtual address
 * space this simply returns the cached virtual address.
 *
 * Returns:
 * 0 on success, or a negative error code otherwise.
 */
int drm_gem_dma_vmap(struct drm_gem_dma_object *dma_obj,
		     struct iosys_map *map)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_mmap - memory-map an exported DMA GEM object
 * @dma_obj: DMA GEM object
 * @vma: VMA for the area to be mapped
 *
 * This function maps a buffer into a userspace process's address space.
 * In addition to the usual GEM VMA setup it immediately faults in the entire
 * object instead of using on-demand faulting.
 *
 * Returns:
 * 0 on success or a negative error code on failure.
 */
int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *vma)
{}
EXPORT_SYMBOL_GPL();

/**
 * drm_gem_dma_prime_import_sg_table_vmap - PRIME import another driver's
 *	scatter/gather table and get the virtual address of the buffer
 * @dev: DRM device
 * @attach: DMA-BUF attachment
 * @sgt: Scatter/gather table of pinned pages
 *
 * This function imports a scatter/gather table using
 * drm_gem_dma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
 * virtual address. This ensures that a DMA GEM object always has its virtual
 * address set. This address is released when the object is freed.
 *
 * This function can be used as the &drm_driver.gem_prime_import_sg_table
 * callback. The &DRM_GEM_DMA_DRIVER_OPS_VMAP macro provides a shortcut to set
 * the necessary DRM driver operations.
 *
 * Returns:
 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
 * error code on failure.
 */
struct drm_gem_object *
drm_gem_dma_prime_import_sg_table_vmap(struct drm_device *dev,
				       struct dma_buf_attachment *attach,
				       struct sg_table *sgt)
{}
EXPORT_SYMBOL();

MODULE_DESCRIPTION();
MODULE_IMPORT_NS();
MODULE_LICENSE();