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.
12 #include <linux/dma-buf.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_vma_manager.h>
28 * The Contiguous Memory Allocator reserves a pool of memory at early boot
29 * that is used to service requests for large blocks of contiguous memory.
31 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
32 * objects that are physically contiguous in memory. This is useful for
33 * display drivers that are unable to map scattered buffers via an IOMMU.
37 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
39 * @size: size of the object to allocate
41 * This function creates and initializes a GEM CMA object of the given size,
42 * but doesn't allocate any memory to back the object.
45 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
46 * error code on failure.
48 static struct drm_gem_cma_object *
49 __drm_gem_cma_create(struct drm_device *drm, size_t size)
51 struct drm_gem_cma_object *cma_obj;
52 struct drm_gem_object *gem_obj;
55 if (drm->driver->gem_create_object)
56 gem_obj = drm->driver->gem_create_object(drm, size);
58 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
60 return ERR_PTR(-ENOMEM);
61 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
63 ret = drm_gem_object_init(drm, gem_obj, size);
67 ret = drm_gem_create_mmap_offset(gem_obj);
69 drm_gem_object_release(gem_obj);
81 * drm_gem_cma_create - allocate an object with the given size
83 * @size: size of the object to allocate
85 * This function creates a CMA GEM object and allocates a contiguous chunk of
86 * memory as backing store. The backing memory has the writecombine attribute
90 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
91 * error code on failure.
93 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
96 struct drm_gem_cma_object *cma_obj;
99 size = round_up(size, PAGE_SIZE);
101 cma_obj = __drm_gem_cma_create(drm, size);
105 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
106 GFP_KERNEL | __GFP_NOWARN);
107 if (!cma_obj->vaddr) {
108 drm_dbg(drm, "failed to allocate buffer with size %zu\n",
117 drm_gem_object_put(&cma_obj->base);
120 EXPORT_SYMBOL_GPL(drm_gem_cma_create);
123 * drm_gem_cma_create_with_handle - allocate an object with the given size and
124 * return a GEM handle to it
125 * @file_priv: DRM file-private structure to register the handle for
127 * @size: size of the object to allocate
128 * @handle: return location for the GEM handle
130 * This function creates a CMA GEM object, allocating a physically contiguous
131 * chunk of memory as backing store. The GEM object is then added to the list
132 * of object associated with the given file and a handle to it is returned.
135 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
136 * error code on failure.
138 static struct drm_gem_cma_object *
139 drm_gem_cma_create_with_handle(struct drm_file *file_priv,
140 struct drm_device *drm, size_t size,
143 struct drm_gem_cma_object *cma_obj;
144 struct drm_gem_object *gem_obj;
147 cma_obj = drm_gem_cma_create(drm, size);
151 gem_obj = &cma_obj->base;
154 * allocate a id of idr table where the obj is registered
155 * and handle has the id what user can see.
157 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
158 /* drop reference from allocate - handle holds it now. */
159 drm_gem_object_put(gem_obj);
167 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
168 * @gem_obj: GEM object to free
170 * This function frees the backing memory of the CMA GEM object, cleans up the
171 * GEM object state and frees the memory used to store the object itself.
172 * If the buffer is imported and the virtual address is set, it is released.
173 * Drivers using the CMA helpers should set this as their
174 * &drm_driver.gem_free_object_unlocked callback.
176 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
178 struct drm_gem_cma_object *cma_obj;
180 cma_obj = to_drm_gem_cma_obj(gem_obj);
182 if (gem_obj->import_attach) {
184 dma_buf_vunmap(gem_obj->import_attach->dmabuf, cma_obj->vaddr);
185 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
186 } else if (cma_obj->vaddr) {
187 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
188 cma_obj->vaddr, cma_obj->paddr);
191 drm_gem_object_release(gem_obj);
195 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
198 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
199 * @file_priv: DRM file-private structure to create the dumb buffer for
203 * This aligns the pitch and size arguments to the minimum required. This is
204 * an internal helper that can be wrapped by a driver to account for hardware
205 * with more specific alignment requirements. It should not be used directly
206 * as their &drm_driver.dumb_create callback.
209 * 0 on success or a negative error code on failure.
211 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
212 struct drm_device *drm,
213 struct drm_mode_create_dumb *args)
215 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
216 struct drm_gem_cma_object *cma_obj;
218 if (args->pitch < min_pitch)
219 args->pitch = min_pitch;
221 if (args->size < args->pitch * args->height)
222 args->size = args->pitch * args->height;
224 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
226 return PTR_ERR_OR_ZERO(cma_obj);
228 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
231 * drm_gem_cma_dumb_create - create a dumb buffer object
232 * @file_priv: DRM file-private structure to create the dumb buffer for
236 * This function computes the pitch of the dumb buffer and rounds it up to an
237 * integer number of bytes per pixel. Drivers for hardware that doesn't have
238 * any additional restrictions on the pitch can directly use this function as
239 * their &drm_driver.dumb_create callback.
241 * For hardware with additional restrictions, drivers can adjust the fields
242 * set up by userspace and pass the IOCTL data along to the
243 * drm_gem_cma_dumb_create_internal() function.
246 * 0 on success or a negative error code on failure.
248 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
249 struct drm_device *drm,
250 struct drm_mode_create_dumb *args)
252 struct drm_gem_cma_object *cma_obj;
254 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
255 args->size = args->pitch * args->height;
257 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
259 return PTR_ERR_OR_ZERO(cma_obj);
261 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
263 const struct vm_operations_struct drm_gem_cma_vm_ops = {
264 .open = drm_gem_vm_open,
265 .close = drm_gem_vm_close,
267 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
269 static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
270 struct vm_area_struct *vma)
275 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
276 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
279 vma->vm_flags &= ~VM_PFNMAP;
282 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
283 cma_obj->paddr, vma->vm_end - vma->vm_start);
285 drm_gem_vm_close(vma);
291 * drm_gem_cma_mmap - memory-map a CMA GEM object
293 * @vma: VMA for the area to be mapped
295 * This function implements an augmented version of the GEM DRM file mmap
296 * operation for CMA objects: In addition to the usual GEM VMA setup it
297 * immediately faults in the entire object instead of using on-demaind
298 * faulting. Drivers which employ the CMA helpers should use this function
299 * as their ->mmap() handler in the DRM device file's file_operations
302 * Instead of directly referencing this function, drivers should use the
303 * DEFINE_DRM_GEM_CMA_FOPS().macro.
306 * 0 on success or a negative error code on failure.
308 int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
310 struct drm_gem_cma_object *cma_obj;
311 struct drm_gem_object *gem_obj;
314 ret = drm_gem_mmap(filp, vma);
318 gem_obj = vma->vm_private_data;
319 cma_obj = to_drm_gem_cma_obj(gem_obj);
321 return drm_gem_cma_mmap_obj(cma_obj, vma);
323 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
327 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
329 * @addr: memory address
331 * @pgoff: page offset
332 * @flags: memory flags
334 * This function is used in noMMU platforms to propose address mapping
335 * for a given buffer.
336 * It's intended to be used as a direct handler for the struct
337 * &file_operations.get_unmapped_area operation.
340 * mapping address on success or a negative error code on failure.
342 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
348 struct drm_gem_cma_object *cma_obj;
349 struct drm_gem_object *obj = NULL;
350 struct drm_file *priv = filp->private_data;
351 struct drm_device *dev = priv->minor->dev;
352 struct drm_vma_offset_node *node;
354 if (drm_dev_is_unplugged(dev))
357 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
358 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
362 obj = container_of(node, struct drm_gem_object, vma_node);
364 * When the object is being freed, after it hits 0-refcnt it
365 * proceeds to tear down the object. In the process it will
366 * attempt to remove the VMA offset and so acquire this
367 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
368 * that matches our range, we know it is in the process of being
369 * destroyed and will be freed as soon as we release the lock -
370 * so we have to check for the 0-refcnted object and treat it as
373 if (!kref_get_unless_zero(&obj->refcount))
377 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
382 if (!drm_vma_node_is_allowed(node, priv)) {
383 drm_gem_object_put(obj);
387 cma_obj = to_drm_gem_cma_obj(obj);
389 drm_gem_object_put(obj);
391 return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
393 EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
397 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
399 * @indent: Tab indentation level
402 * This function can be used as the &drm_driver->gem_print_info callback.
403 * It prints paddr and vaddr for use in e.g. debugfs output.
405 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
406 const struct drm_gem_object *obj)
408 const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
410 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
411 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
413 EXPORT_SYMBOL(drm_gem_cma_print_info);
416 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
417 * pages for a CMA GEM object
420 * This function exports a scatter/gather table suitable for PRIME usage by
421 * calling the standard DMA mapping API. Drivers using the CMA helpers should
422 * set this as their &drm_driver.gem_prime_get_sg_table callback.
425 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
427 struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
429 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
430 struct sg_table *sgt;
433 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
435 return ERR_PTR(-ENOMEM);
437 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
438 cma_obj->paddr, obj->size);
448 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
451 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
452 * driver's scatter/gather table of pinned pages
453 * @dev: device to import into
454 * @attach: DMA-BUF attachment
455 * @sgt: scatter/gather table of pinned pages
457 * This function imports a scatter/gather table exported via DMA-BUF by
458 * another driver. Imported buffers must be physically contiguous in memory
459 * (i.e. the scatter/gather table must contain a single entry). Drivers that
460 * use the CMA helpers should set this as their
461 * &drm_driver.gem_prime_import_sg_table callback.
464 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
465 * error code on failure.
467 struct drm_gem_object *
468 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
469 struct dma_buf_attachment *attach,
470 struct sg_table *sgt)
472 struct drm_gem_cma_object *cma_obj;
474 /* check if the entries in the sg_table are contiguous */
475 if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size)
476 return ERR_PTR(-EINVAL);
478 /* Create a CMA GEM buffer. */
479 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
481 return ERR_CAST(cma_obj);
483 cma_obj->paddr = sg_dma_address(sgt->sgl);
486 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
488 return &cma_obj->base;
490 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
493 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
495 * @vma: VMA for the area to be mapped
497 * This function maps a buffer imported via DRM PRIME into a userspace
498 * process's address space. Drivers that use the CMA helpers should set this
499 * as their &drm_driver.gem_prime_mmap callback.
502 * 0 on success or a negative error code on failure.
504 int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
505 struct vm_area_struct *vma)
507 struct drm_gem_cma_object *cma_obj;
510 ret = drm_gem_mmap_obj(obj, obj->size, vma);
514 cma_obj = to_drm_gem_cma_obj(obj);
515 return drm_gem_cma_mmap_obj(cma_obj, vma);
517 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
520 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
524 * This function maps a buffer exported via DRM PRIME into the kernel's
525 * virtual address space. Since the CMA buffers are already mapped into the
526 * kernel virtual address space this simply returns the cached virtual
527 * address. Drivers using the CMA helpers should set this as their DRM
528 * driver's &drm_driver.gem_prime_vmap callback.
531 * The kernel virtual address of the CMA GEM object's backing store.
533 void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
535 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
537 return cma_obj->vaddr;
539 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
542 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
545 * @vaddr: kernel virtual address where the CMA GEM object was mapped
547 * This function removes a buffer exported via DRM PRIME from the kernel's
548 * virtual address space. This is a no-op because CMA buffers cannot be
549 * unmapped from kernel space. Drivers using the CMA helpers should set this
550 * as their &drm_driver.gem_prime_vunmap callback.
552 void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
556 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);
558 static const struct drm_gem_object_funcs drm_gem_cma_default_funcs = {
559 .free = drm_gem_cma_free_object,
560 .print_info = drm_gem_cma_print_info,
561 .get_sg_table = drm_gem_cma_prime_get_sg_table,
562 .vmap = drm_gem_cma_prime_vmap,
563 .vm_ops = &drm_gem_cma_vm_ops,
567 * drm_gem_cma_create_object_default_funcs - Create a CMA GEM object with a
568 * default function table
570 * @size: Size of the object to allocate
572 * This sets the GEM object functions to the default CMA helper functions.
573 * This function can be used as the &drm_driver.gem_create_object callback.
576 * A pointer to a allocated GEM object or an error pointer on failure.
578 struct drm_gem_object *
579 drm_gem_cma_create_object_default_funcs(struct drm_device *dev, size_t size)
581 struct drm_gem_cma_object *cma_obj;
583 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
587 cma_obj->base.funcs = &drm_gem_cma_default_funcs;
589 return &cma_obj->base;
591 EXPORT_SYMBOL(drm_gem_cma_create_object_default_funcs);
594 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
595 * scatter/gather table and get the virtual address of the buffer
597 * @attach: DMA-BUF attachment
598 * @sgt: Scatter/gather table of pinned pages
600 * This function imports a scatter/gather table using
601 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
602 * virtual address. This ensures that a CMA GEM object always has its virtual
603 * address set. This address is released when the object is freed.
605 * This function can be used as the &drm_driver.gem_prime_import_sg_table
606 * callback. The &DRM_GEM_CMA_DRIVER_OPS_VMAP macro provides a shortcut to set
607 * the necessary DRM driver operations.
610 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
611 * error code on failure.
613 struct drm_gem_object *
614 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
615 struct dma_buf_attachment *attach,
616 struct sg_table *sgt)
618 struct drm_gem_cma_object *cma_obj;
619 struct drm_gem_object *obj;
622 vaddr = dma_buf_vmap(attach->dmabuf);
624 DRM_ERROR("Failed to vmap PRIME buffer\n");
625 return ERR_PTR(-ENOMEM);
628 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
630 dma_buf_vunmap(attach->dmabuf, vaddr);
634 cma_obj = to_drm_gem_cma_obj(obj);
635 cma_obj->vaddr = vaddr;
639 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);