cregit-Linux how code gets into the kernel

Release 4.7 drivers/gpu/drm/drm_gem_cma_helper.c

Directory: drivers/gpu/drm
/*
 * drm gem CMA (contiguous memory allocator) helper functions
 *
 * Copyright (C) 2012 Sascha Hauer, Pengutronix
 *
 * Based on Samsung Exynos code
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

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

#include <drm/drmP.h>
#include <drm/drm.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_vma_manager.h>

/**
 * DOC: cma helpers
 *
 * The Contiguous Memory Allocator reserves a pool of memory at early boot
 * that is used to service requests for large blocks of contiguous memory.
 *
 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
 * objects that are physically contiguous in memory. This is useful for
 * display drivers that are unable to map scattered buffers via an IOMMU.
 */

/**
 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
 * @drm: DRM device
 * @size: size of the object to allocate
 *
 * This function creates and initializes a GEM CMA object of the given size,
 * but doesn't allocate any memory to back the object.
 *
 * Returns:
 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
 * error code on failure.
 */

static struct drm_gem_cma_object * __drm_gem_cma_create(struct drm_device *drm, size_t size) { struct drm_gem_cma_object *cma_obj; struct drm_gem_object *gem_obj; int ret; if (drm->driver->gem_create_object) gem_obj = drm->driver->gem_create_object(drm, size); else gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL); if (!gem_obj) return ERR_PTR(-ENOMEM); cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base); ret = drm_gem_object_init(drm, gem_obj, size); if (ret) goto error; ret = drm_gem_create_mmap_offset(gem_obj); if (ret) { drm_gem_object_release(gem_obj); goto error; } return cma_obj; error: kfree(cma_obj); return ERR_PTR(ret); }

Contributors

PersonTokensPropCommitsCommitProp
laurent pinchartlaurent pinchart8861.54%125.00%
eric anholteric anholt3323.08%125.00%
sascha hauersascha hauer2114.69%125.00%
thierry redingthierry reding10.70%125.00%
Total143100.00%4100.00%

/** * drm_gem_cma_create - allocate an object with the given size * @drm: DRM device * @size: size of the object to allocate * * This function creates a CMA GEM object and allocates a contiguous chunk of * memory as backing store. The backing memory has the writecombine attribute * set. * * Returns: * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative * error code on failure. */
struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, size_t size) { struct drm_gem_cma_object *cma_obj; int ret; size = round_up(size, PAGE_SIZE); cma_obj = __drm_gem_cma_create(drm, size); if (IS_ERR(cma_obj)) return cma_obj; cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN); if (!cma_obj->vaddr) { dev_err(drm->dev, "failed to allocate buffer with size %zu\n", size); ret = -ENOMEM; goto error; } return cma_obj; error: drm_gem_object_unreference_unlocked(&cma_obj->base); return ERR_PTR(ret); }

Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer9075.00%114.29%
laurent pinchartlaurent pinchart2621.67%228.57%
thierry redingthierry reding10.83%114.29%
magnus dammmagnus damm10.83%114.29%
luis r. rodriguezluis r. rodriguez10.83%114.29%
chris wilsonchris wilson10.83%114.29%
Total120100.00%7100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_create); /** * drm_gem_cma_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 CMA GEM object, allocating a physically contiguous * 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. * * Returns: * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative * error code on failure. */
static struct drm_gem_cma_object * drm_gem_cma_create_with_handle(struct drm_file *file_priv, struct drm_device *drm, size_t size, uint32_t *handle) { struct drm_gem_cma_object *cma_obj; struct drm_gem_object *gem_obj; int ret; cma_obj = drm_gem_cma_create(drm, size); if (IS_ERR(cma_obj)) return cma_obj; gem_obj = &cma_obj->base; /* * allocate a id of idr table where the obj is registered * and handle has the id what user can see. */ ret = drm_gem_handle_create(file_priv, gem_obj, handle); /* drop reference from allocate - handle holds it now. */ drm_gem_object_unreference_unlocked(gem_obj); if (ret) return ERR_PTR(ret); return cma_obj; }

Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer8690.53%133.33%
chris wilsonchris wilson77.37%133.33%
thierry redingthierry reding22.11%133.33%
Total95100.00%3100.00%

/** * drm_gem_cma_free_object - free resources associated with a CMA GEM object * @gem_obj: GEM object to free * * This function frees the backing memory of the CMA GEM object, cleans up the * GEM object state and frees the memory used to store the object itself. * Drivers using the CMA helpers should set this as their DRM driver's * ->gem_free_object() callback. */
void drm_gem_cma_free_object(struct drm_gem_object *gem_obj) { struct drm_gem_cma_object *cma_obj; cma_obj = to_drm_gem_cma_obj(gem_obj); if (cma_obj->vaddr) { dma_free_wc(gem_obj->dev->dev, cma_obj->base.size, cma_obj->vaddr, cma_obj->paddr); } else if (gem_obj->import_attach) { drm_prime_gem_destroy(gem_obj, cma_obj->sgt); } drm_gem_object_release(gem_obj); kfree(cma_obj); }

Contributors

PersonTokensPropCommitsCommitProp
laurent pinchartlaurent pinchart4555.56%250.00%
sascha hauersascha hauer3543.21%125.00%
luis r. rodriguezluis r. rodriguez11.23%125.00%
Total81100.00%4100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_free_object); /** * drm_gem_cma_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 the ->dumb_create() callback in a DRM driver. * * Returns: * 0 on success or a negative error code on failure. */
int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv, struct drm_device *drm, struct drm_mode_create_dumb *args) { unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); struct drm_gem_cma_object *cma_obj; if (args->pitch < min_pitch) args->pitch = min_pitch; if (args->size < args->pitch * args->height) args->size = args->pitch * args->height; cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size, &args->handle); return PTR_ERR_OR_ZERO(cma_obj); }

Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer6662.26%133.33%
thierry redingthierry reding4037.74%266.67%
Total106100.00%3100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal); /** * drm_gem_cma_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 ->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_cma_dumb_create_internal() function. * * Returns: * 0 on success or a negative error code on failure. */
int drm_gem_cma_dumb_create(struct drm_file *file_priv, struct drm_device *drm, struct drm_mode_create_dumb *args) { struct drm_gem_cma_object *cma_obj; args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8); args->size = args->pitch * args->height; cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size, &args->handle); return PTR_ERR_OR_ZERO(cma_obj); }

Contributors

PersonTokensPropCommitsCommitProp
thierry redingthierry reding4760.26%250.00%
sascha hauersascha hauer3038.46%125.00%
sachin kamatsachin kamat11.28%125.00%
Total78100.00%4100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create); /** * drm_gem_cma_dumb_map_offset - return the fake mmap offset for a CMA GEM * object * @file_priv: DRM file-private structure containing the GEM object * @drm: DRM device * @handle: GEM object handle * @offset: return location for the fake mmap offset * * This function look up an object by its handle and returns the fake mmap * offset associated with it. Drivers using the CMA helpers should set this * as their DRM driver's ->dumb_map_offset() callback. * * Returns: * 0 on success or a negative error code on failure. */
int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv, struct drm_device *drm, u32 handle, u64 *offset) { struct drm_gem_object *gem_obj; gem_obj = drm_gem_object_lookup(file_priv, handle); if (!gem_obj) { dev_err(drm->dev, "failed to lookup GEM object\n"); return -EINVAL; } *offset = drm_vma_node_offset_addr(&gem_obj->vma_node); drm_gem_object_unreference_unlocked(gem_obj); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer6789.33%125.00%
david herrmanndavid herrmann45.33%125.00%
thierry redingthierry reding34.00%125.00%
daniel vetterdaniel vetter11.33%125.00%
Total75100.00%4100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset); const struct vm_operations_struct drm_gem_cma_vm_ops = { .open = drm_gem_vm_open, .close = drm_gem_vm_close, }; EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj, struct vm_area_struct *vma) { int ret; /* * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map * the whole buffer. */ vma->vm_flags &= ~VM_PFNMAP; vma->vm_pgoff = 0; ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr, cma_obj->paddr, vma->vm_end - vma->vm_start); if (ret) drm_gem_vm_close(vma); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
laurent pinchartlaurent pinchart7598.68%266.67%
luis r. rodriguezluis r. rodriguez11.32%133.33%
Total76100.00%3100.00%

/** * drm_gem_cma_mmap - memory-map a CMA GEM object * @filp: file object * @vma: VMA for the area to be mapped * * This function implements an augmented version of the GEM DRM file mmap * operation for CMA objects: In addition to the usual GEM VMA setup it * immediately faults in the entire object instead of using on-demaind * faulting. Drivers which employ the CMA helpers should use this function * as their ->mmap() handler in the DRM device file's file_operations * structure. * * Returns: * 0 on success or a negative error code on failure. */
int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma) { struct drm_gem_cma_object *cma_obj; struct drm_gem_object *gem_obj; int ret; ret = drm_gem_mmap(filp, vma); if (ret) return ret; gem_obj = vma->vm_private_data; cma_obj = to_drm_gem_cma_obj(gem_obj); return drm_gem_cma_mmap_obj(cma_obj, vma); }

Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer5990.77%150.00%
laurent pinchartlaurent pinchart69.23%150.00%
Total65100.00%2100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_mmap); #ifdef CONFIG_DEBUG_FS /** * drm_gem_cma_describe - describe a CMA GEM object for debugfs * @cma_obj: CMA GEM object * @m: debugfs file handle * * This function can be used to dump a human-readable representation of the * CMA GEM object into a synthetic file. */
void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m) { struct drm_gem_object *obj = &cma_obj->base; uint64_t off; off = drm_vma_node_start(&obj->vma_node); seq_printf(m, "%2d (%2d) %08llx %pad %p %zu", obj->name, obj->refcount.refcount.counter, off, &cma_obj->paddr, cma_obj->vaddr, obj->size); seq_printf(m, "\n"); }

Contributors

PersonTokensPropCommitsCommitProp
rob clarkrob clark7291.14%125.00%
david herrmanndavid herrmann56.33%125.00%
laurent pinchartlaurent pinchart11.27%125.00%
magnus dammmagnus damm11.27%125.00%
Total79100.00%4100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_describe); #endif /** * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned * pages for a CMA GEM object * @obj: GEM object * * This function exports a scatter/gather table suitable for PRIME usage by * calling the standard DMA mapping API. Drivers using the CMA helpers should * set this as their DRM driver's ->gem_prime_get_sg_table() callback. * * Returns: * A pointer to the scatter/gather table of pinned pages or NULL on failure. */
struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj) { struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); struct sg_table *sgt; int ret; sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); if (!sgt) return NULL; ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr, cma_obj->paddr, obj->size); if (ret < 0) goto out; return sgt; out: kfree(sgt); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
joonyoung shimjoonyoung shim98100.00%1100.00%
Total98100.00%1100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table); /** * drm_gem_cma_prime_import_sg_table - produce a CMA 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 CMA helpers should set this as their DRM driver's * ->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_cma_prime_import_sg_table(struct drm_device *dev, struct dma_buf_attachment *attach, struct sg_table *sgt) { struct drm_gem_cma_object *cma_obj; if (sgt->nents != 1) return ERR_PTR(-EINVAL); /* Create a CMA GEM buffer. */ cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size); if (IS_ERR(cma_obj)) return ERR_CAST(cma_obj); cma_obj->paddr = sg_dma_address(sgt->sgl); cma_obj->sgt = sgt; DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size); return &cma_obj->base; }

Contributors

PersonTokensPropCommitsCommitProp
joonyoung shimjoonyoung shim9386.11%125.00%
maarten lankhorstmaarten lankhorst1211.11%125.00%
laurent pinchartlaurent pinchart21.85%125.00%
sachin kamatsachin kamat10.93%125.00%
Total108100.00%4100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table); /** * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object * @obj: GEM object * @vma: VMA for the area to be mapped * * This function maps a buffer imported via DRM PRIME into a userspace * process's address space. Drivers that use the CMA helpers should set this * as their DRM driver's ->gem_prime_mmap() callback. * * Returns: * 0 on success or a negative error code on failure. */
int drm_gem_cma_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) { struct drm_gem_cma_object *cma_obj; int ret; ret = drm_gem_mmap_obj(obj, obj->size, vma); if (ret < 0) return ret; cma_obj = to_drm_gem_cma_obj(obj); return drm_gem_cma_mmap_obj(cma_obj, vma); }

Contributors

PersonTokensPropCommitsCommitProp
joonyoung shimjoonyoung shim60100.00%1100.00%
Total60100.00%1100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap); /** * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual * address space * @obj: GEM object * * This function maps a buffer exported via DRM PRIME into the kernel's * virtual address space. Since the CMA buffers are already mapped into the * kernel virtual address space this simply returns the cached virtual * address. Drivers using the CMA helpers should set this as their DRM * driver's ->gem_prime_vmap() callback. * * Returns: * The kernel virtual address of the CMA GEM object's backing store. */
void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj) { struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); return cma_obj->vaddr; }

Contributors

PersonTokensPropCommitsCommitProp
joonyoung shimjoonyoung shim26100.00%1100.00%
Total26100.00%1100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap); /** * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual * address space * @obj: GEM object * @vaddr: kernel virtual address where the CMA GEM object was mapped * * This function removes a buffer exported via DRM PRIME from the kernel's * virtual address space. This is a no-op because CMA buffers cannot be * unmapped from kernel space. Drivers using the CMA helpers should set this * as their DRM driver's ->gem_prime_vunmap() callback. */
void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr) { /* Nothing to do */ }

Contributors

PersonTokensPropCommitsCommitProp
joonyoung shimjoonyoung shim15100.00%1100.00%
Total15100.00%1100.00%

EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);

Overall Contributors

PersonTokensPropCommitsCommitProp
sascha hauersascha hauer52638.73%15.26%
joonyoung shimjoonyoung shim31723.34%15.26%
laurent pinchartlaurent pinchart24618.11%526.32%
thierry redingthierry reding1148.39%210.53%
rob clarkrob clark826.04%15.26%
eric anholteric anholt332.43%15.26%
maarten lankhorstmaarten lankhorst120.88%15.26%
david herrmanndavid herrmann120.88%15.26%
chris wilsonchris wilson80.59%15.26%
luis r. rodriguezluis r. rodriguez30.22%15.26%
magnus dammmagnus damm20.15%15.26%
sachin kamatsachin kamat20.15%210.53%
daniel vetterdaniel vetter10.07%15.26%
Total1358100.00%19100.00%
Directory: drivers/gpu/drm
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}