1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drm gem CMA (contiguous memory allocator) helper functions
5 * Copyright (C) 2012 Sascha Hauer, Pengutronix
7 * Based on Samsung Exynos code
9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/export.h>
16 #include <linux/dma-buf.h>
17 #include <linux/dma-mapping.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_vma_manager.h>
27 * The Contiguous Memory Allocator reserves a pool of memory at early boot
28 * that is used to service requests for large blocks of contiguous memory.
30 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
31 * objects that are physically contiguous in memory. This is useful for
32 * display drivers that are unable to map scattered buffers via an IOMMU.
36 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
38 * @size: size of the object to allocate
40 * This function creates and initializes a GEM CMA object of the given size,
41 * but doesn't allocate any memory to back the object.
44 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
45 * error code on failure.
47 static struct drm_gem_cma_object *
48 __drm_gem_cma_create(struct drm_device *drm, size_t size)
50 struct drm_gem_cma_object *cma_obj;
51 struct drm_gem_object *gem_obj;
54 if (drm->driver->gem_create_object)
55 gem_obj = drm->driver->gem_create_object(drm, size);
57 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
59 return ERR_PTR(-ENOMEM);
60 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
62 ret = drm_gem_object_init(drm, gem_obj, size);
66 ret = drm_gem_create_mmap_offset(gem_obj);
68 drm_gem_object_release(gem_obj);
80 * drm_gem_cma_create - allocate an object with the given size
82 * @size: size of the object to allocate
84 * This function creates a CMA GEM object and allocates a contiguous chunk of
85 * memory as backing store. The backing memory has the writecombine attribute
89 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
90 * error code on failure.
92 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
95 struct drm_gem_cma_object *cma_obj;
98 size = round_up(size, PAGE_SIZE);
100 cma_obj = __drm_gem_cma_create(drm, size);
104 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
105 GFP_KERNEL | __GFP_NOWARN);
106 if (!cma_obj->vaddr) {
107 dev_dbg(drm->dev, "failed to allocate buffer with size %zu\n",
116 drm_gem_object_put_unlocked(&cma_obj->base);
119 EXPORT_SYMBOL_GPL(drm_gem_cma_create);
122 * drm_gem_cma_create_with_handle - allocate an object with the given size and
123 * return a GEM handle to it
124 * @file_priv: DRM file-private structure to register the handle for
126 * @size: size of the object to allocate
127 * @handle: return location for the GEM handle
129 * This function creates a CMA GEM object, allocating a physically contiguous
130 * chunk of memory as backing store. The GEM object is then added to the list
131 * of object associated with the given file and a handle to it is returned.
134 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
135 * error code on failure.
137 static struct drm_gem_cma_object *
138 drm_gem_cma_create_with_handle(struct drm_file *file_priv,
139 struct drm_device *drm, size_t size,
142 struct drm_gem_cma_object *cma_obj;
143 struct drm_gem_object *gem_obj;
146 cma_obj = drm_gem_cma_create(drm, size);
150 gem_obj = &cma_obj->base;
153 * allocate a id of idr table where the obj is registered
154 * and handle has the id what user can see.
156 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
157 /* drop reference from allocate - handle holds it now. */
158 drm_gem_object_put_unlocked(gem_obj);
166 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
167 * @gem_obj: GEM object to free
169 * This function frees the backing memory of the CMA GEM object, cleans up the
170 * GEM object state and frees the memory used to store the object itself.
171 * If the buffer is imported and the virtual address is set, it is released.
172 * Drivers using the CMA helpers should set this as their
173 * &drm_driver.gem_free_object_unlocked callback.
175 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
177 struct drm_gem_cma_object *cma_obj;
179 cma_obj = to_drm_gem_cma_obj(gem_obj);
181 if (gem_obj->import_attach) {
183 dma_buf_vunmap(gem_obj->import_attach->dmabuf, cma_obj->vaddr);
184 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
185 } else if (cma_obj->vaddr) {
186 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
187 cma_obj->vaddr, cma_obj->paddr);
190 drm_gem_object_release(gem_obj);
194 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
197 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
198 * @file_priv: DRM file-private structure to create the dumb buffer for
202 * This aligns the pitch and size arguments to the minimum required. This is
203 * an internal helper that can be wrapped by a driver to account for hardware
204 * with more specific alignment requirements. It should not be used directly
205 * as their &drm_driver.dumb_create callback.
208 * 0 on success or a negative error code on failure.
210 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
211 struct drm_device *drm,
212 struct drm_mode_create_dumb *args)
214 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
215 struct drm_gem_cma_object *cma_obj;
217 if (args->pitch < min_pitch)
218 args->pitch = min_pitch;
220 if (args->size < args->pitch * args->height)
221 args->size = args->pitch * args->height;
223 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
225 return PTR_ERR_OR_ZERO(cma_obj);
227 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
230 * drm_gem_cma_dumb_create - create a dumb buffer object
231 * @file_priv: DRM file-private structure to create the dumb buffer for
235 * This function computes the pitch of the dumb buffer and rounds it up to an
236 * integer number of bytes per pixel. Drivers for hardware that doesn't have
237 * any additional restrictions on the pitch can directly use this function as
238 * their &drm_driver.dumb_create callback.
240 * For hardware with additional restrictions, drivers can adjust the fields
241 * set up by userspace and pass the IOCTL data along to the
242 * drm_gem_cma_dumb_create_internal() function.
245 * 0 on success or a negative error code on failure.
247 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
248 struct drm_device *drm,
249 struct drm_mode_create_dumb *args)
251 struct drm_gem_cma_object *cma_obj;
253 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
254 args->size = args->pitch * args->height;
256 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
258 return PTR_ERR_OR_ZERO(cma_obj);
260 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
262 const struct vm_operations_struct drm_gem_cma_vm_ops = {
263 .open = drm_gem_vm_open,
264 .close = drm_gem_vm_close,
266 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
268 static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
269 struct vm_area_struct *vma)
274 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
275 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
278 vma->vm_flags &= ~VM_PFNMAP;
281 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
282 cma_obj->paddr, vma->vm_end - vma->vm_start);
284 drm_gem_vm_close(vma);
290 * drm_gem_cma_mmap - memory-map a CMA GEM object
292 * @vma: VMA for the area to be mapped
294 * This function implements an augmented version of the GEM DRM file mmap
295 * operation for CMA objects: In addition to the usual GEM VMA setup it
296 * immediately faults in the entire object instead of using on-demaind
297 * faulting. Drivers which employ the CMA helpers should use this function
298 * as their ->mmap() handler in the DRM device file's file_operations
301 * Instead of directly referencing this function, drivers should use the
302 * DEFINE_DRM_GEM_CMA_FOPS().macro.
305 * 0 on success or a negative error code on failure.
307 int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
309 struct drm_gem_cma_object *cma_obj;
310 struct drm_gem_object *gem_obj;
313 ret = drm_gem_mmap(filp, vma);
317 gem_obj = vma->vm_private_data;
318 cma_obj = to_drm_gem_cma_obj(gem_obj);
320 return drm_gem_cma_mmap_obj(cma_obj, vma);
322 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
326 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
328 * @addr: memory address
330 * @pgoff: page offset
331 * @flags: memory flags
333 * This function is used in noMMU platforms to propose address mapping
334 * for a given buffer.
335 * It's intended to be used as a direct handler for the struct
336 * &file_operations.get_unmapped_area operation.
339 * mapping address on success or a negative error code on failure.
341 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
347 struct drm_gem_cma_object *cma_obj;
348 struct drm_gem_object *obj = NULL;
349 struct drm_file *priv = filp->private_data;
350 struct drm_device *dev = priv->minor->dev;
351 struct drm_vma_offset_node *node;
353 if (drm_dev_is_unplugged(dev))
356 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
357 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
361 obj = container_of(node, struct drm_gem_object, vma_node);
363 * When the object is being freed, after it hits 0-refcnt it
364 * proceeds to tear down the object. In the process it will
365 * attempt to remove the VMA offset and so acquire this
366 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
367 * that matches our range, we know it is in the process of being
368 * destroyed and will be freed as soon as we release the lock -
369 * so we have to check for the 0-refcnted object and treat it as
372 if (!kref_get_unless_zero(&obj->refcount))
376 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
381 if (!drm_vma_node_is_allowed(node, priv)) {
382 drm_gem_object_put_unlocked(obj);
386 cma_obj = to_drm_gem_cma_obj(obj);
388 drm_gem_object_put_unlocked(obj);
390 return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
392 EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
396 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
398 * @indent: Tab indentation level
401 * This function can be used as the &drm_driver->gem_print_info callback.
402 * It prints paddr and vaddr for use in e.g. debugfs output.
404 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
405 const struct drm_gem_object *obj)
407 const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
409 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
410 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
412 EXPORT_SYMBOL(drm_gem_cma_print_info);
415 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
416 * pages for a CMA GEM object
419 * This function exports a scatter/gather table suitable for PRIME usage by
420 * calling the standard DMA mapping API. Drivers using the CMA helpers should
421 * set this as their &drm_driver.gem_prime_get_sg_table callback.
424 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
426 struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
428 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
429 struct sg_table *sgt;
432 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
434 return ERR_PTR(-ENOMEM);
436 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
437 cma_obj->paddr, obj->size);
447 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
450 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
451 * driver's scatter/gather table of pinned pages
452 * @dev: device to import into
453 * @attach: DMA-BUF attachment
454 * @sgt: scatter/gather table of pinned pages
456 * This function imports a scatter/gather table exported via DMA-BUF by
457 * another driver. Imported buffers must be physically contiguous in memory
458 * (i.e. the scatter/gather table must contain a single entry). Drivers that
459 * use the CMA helpers should set this as their
460 * &drm_driver.gem_prime_import_sg_table callback.
463 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
464 * error code on failure.
466 struct drm_gem_object *
467 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
468 struct dma_buf_attachment *attach,
469 struct sg_table *sgt)
471 struct drm_gem_cma_object *cma_obj;
473 if (sgt->nents != 1) {
474 /* check if the entries in the sg_table are contiguous */
475 dma_addr_t next_addr = sg_dma_address(sgt->sgl);
476 struct scatterlist *s;
479 for_each_sg(sgt->sgl, s, sgt->nents, i) {
481 * sg_dma_address(s) is only valid for entries
482 * that have sg_dma_len(s) != 0
487 if (sg_dma_address(s) != next_addr)
488 return ERR_PTR(-EINVAL);
490 next_addr = sg_dma_address(s) + sg_dma_len(s);
494 /* Create a CMA GEM buffer. */
495 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
497 return ERR_CAST(cma_obj);
499 cma_obj->paddr = sg_dma_address(sgt->sgl);
502 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
504 return &cma_obj->base;
506 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
509 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
511 * @vma: VMA for the area to be mapped
513 * This function maps a buffer imported via DRM PRIME into a userspace
514 * process's address space. Drivers that use the CMA helpers should set this
515 * as their &drm_driver.gem_prime_mmap callback.
518 * 0 on success or a negative error code on failure.
520 int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
521 struct vm_area_struct *vma)
523 struct drm_gem_cma_object *cma_obj;
526 ret = drm_gem_mmap_obj(obj, obj->size, vma);
530 cma_obj = to_drm_gem_cma_obj(obj);
531 return drm_gem_cma_mmap_obj(cma_obj, vma);
533 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
536 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
540 * This function maps a buffer exported via DRM PRIME into the kernel's
541 * virtual address space. Since the CMA buffers are already mapped into the
542 * kernel virtual address space this simply returns the cached virtual
543 * address. Drivers using the CMA helpers should set this as their DRM
544 * driver's &drm_driver.gem_prime_vmap callback.
547 * The kernel virtual address of the CMA GEM object's backing store.
549 void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
551 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
553 return cma_obj->vaddr;
555 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
558 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
561 * @vaddr: kernel virtual address where the CMA GEM object was mapped
563 * This function removes a buffer exported via DRM PRIME from the kernel's
564 * virtual address space. This is a no-op because CMA buffers cannot be
565 * unmapped from kernel space. Drivers using the CMA helpers should set this
566 * as their &drm_driver.gem_prime_vunmap callback.
568 void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
572 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);
574 static const struct drm_gem_object_funcs drm_cma_gem_default_funcs = {
575 .free = drm_gem_cma_free_object,
576 .print_info = drm_gem_cma_print_info,
577 .get_sg_table = drm_gem_cma_prime_get_sg_table,
578 .vmap = drm_gem_cma_prime_vmap,
579 .vm_ops = &drm_gem_cma_vm_ops,
583 * drm_cma_gem_create_object_default_funcs - Create a CMA GEM object with a
584 * default function table
586 * @size: Size of the object to allocate
588 * This sets the GEM object functions to the default CMA helper functions.
589 * This function can be used as the &drm_driver.gem_create_object callback.
592 * A pointer to a allocated GEM object or an error pointer on failure.
594 struct drm_gem_object *
595 drm_cma_gem_create_object_default_funcs(struct drm_device *dev, size_t size)
597 struct drm_gem_cma_object *cma_obj;
599 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
603 cma_obj->base.funcs = &drm_cma_gem_default_funcs;
605 return &cma_obj->base;
607 EXPORT_SYMBOL(drm_cma_gem_create_object_default_funcs);
610 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
611 * scatter/gather table and get the virtual address of the buffer
613 * @attach: DMA-BUF attachment
614 * @sgt: Scatter/gather table of pinned pages
616 * This function imports a scatter/gather table using
617 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
618 * virtual address. This ensures that a CMA GEM object always has its virtual
619 * address set. This address is released when the object is freed.
621 * This function can be used as the &drm_driver.gem_prime_import_sg_table
622 * callback. The DRM_GEM_CMA_VMAP_DRIVER_OPS() macro provides a shortcut to set
623 * the necessary DRM driver operations.
626 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
627 * error code on failure.
629 struct drm_gem_object *
630 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
631 struct dma_buf_attachment *attach,
632 struct sg_table *sgt)
634 struct drm_gem_cma_object *cma_obj;
635 struct drm_gem_object *obj;
638 vaddr = dma_buf_vmap(attach->dmabuf);
640 DRM_ERROR("Failed to vmap PRIME buffer\n");
641 return ERR_PTR(-ENOMEM);
644 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
646 dma_buf_vunmap(attach->dmabuf, vaddr);
650 cma_obj = to_drm_gem_cma_obj(obj);
651 cma_obj->vaddr = vaddr;
655 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);