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/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_drv.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_vma_manager.h>
29 * The DRM GEM/CMA helpers are a means to provide buffer objects that are
30 * presented to the device as a contiguous chunk of memory. This is useful
31 * for devices that do not support scatter-gather DMA (either directly or
32 * by using an intimately attached IOMMU).
34 * Despite the name, the DRM GEM/CMA helpers are not hardwired to use the
35 * Contiguous Memory Allocator (CMA).
37 * For devices that access the memory bus through an (external) IOMMU then
38 * the buffer objects are allocated using a traditional page-based
39 * allocator and may be scattered through physical memory. However they
40 * are contiguous in the IOVA space so appear contiguous to devices using
43 * For other devices then the helpers rely on CMA to provide buffer
44 * objects that are physically contiguous in memory.
46 * For GEM callback helpers in struct &drm_gem_object functions, see likewise
47 * named functions with an _object_ infix (e.g., drm_gem_cma_object_vmap() wraps
48 * drm_gem_cma_vmap()). These helpers perform the necessary type conversion.
51 static const struct drm_gem_object_funcs drm_gem_cma_default_funcs = {
52 .free = drm_gem_cma_object_free,
53 .print_info = drm_gem_cma_object_print_info,
54 .get_sg_table = drm_gem_cma_object_get_sg_table,
55 .vmap = drm_gem_cma_object_vmap,
56 .mmap = drm_gem_cma_object_mmap,
57 .vm_ops = &drm_gem_cma_vm_ops,
61 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
63 * @size: size of the object to allocate
64 * @private: true if used for internal purposes
66 * This function creates and initializes a GEM CMA object of the given size,
67 * but doesn't allocate any memory to back the object.
70 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
71 * error code on failure.
73 static struct drm_gem_cma_object *
74 __drm_gem_cma_create(struct drm_device *drm, size_t size, bool private)
76 struct drm_gem_cma_object *cma_obj;
77 struct drm_gem_object *gem_obj;
80 if (drm->driver->gem_create_object) {
81 gem_obj = drm->driver->gem_create_object(drm, size);
83 return ERR_CAST(gem_obj);
84 cma_obj = to_drm_gem_cma_obj(gem_obj);
86 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
88 return ERR_PTR(-ENOMEM);
89 gem_obj = &cma_obj->base;
93 gem_obj->funcs = &drm_gem_cma_default_funcs;
96 drm_gem_private_object_init(drm, gem_obj, size);
98 /* Always use writecombine for dma-buf mappings */
99 cma_obj->map_noncoherent = false;
101 ret = drm_gem_object_init(drm, gem_obj, size);
106 ret = drm_gem_create_mmap_offset(gem_obj);
108 drm_gem_object_release(gem_obj);
120 * drm_gem_cma_create - allocate an object with the given size
122 * @size: size of the object to allocate
124 * This function creates a CMA GEM object and allocates memory as backing store.
125 * The allocated memory will occupy a contiguous chunk of bus address space.
127 * For devices that are directly connected to the memory bus then the allocated
128 * memory will be physically contiguous. For devices that access through an
129 * IOMMU, then the allocated memory is not expected to be physically contiguous
130 * because having contiguous IOVAs is sufficient to meet a devices DMA
134 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
135 * error code on failure.
137 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
140 struct drm_gem_cma_object *cma_obj;
143 size = round_up(size, PAGE_SIZE);
145 cma_obj = __drm_gem_cma_create(drm, size, false);
149 if (cma_obj->map_noncoherent) {
150 cma_obj->vaddr = dma_alloc_noncoherent(drm->dev, size,
153 GFP_KERNEL | __GFP_NOWARN);
155 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
156 GFP_KERNEL | __GFP_NOWARN);
158 if (!cma_obj->vaddr) {
159 drm_dbg(drm, "failed to allocate buffer with size %zu\n",
168 drm_gem_object_put(&cma_obj->base);
171 EXPORT_SYMBOL_GPL(drm_gem_cma_create);
174 * drm_gem_cma_create_with_handle - allocate an object with the given size and
175 * return a GEM handle to it
176 * @file_priv: DRM file-private structure to register the handle for
178 * @size: size of the object to allocate
179 * @handle: return location for the GEM handle
181 * This function creates a CMA GEM object, allocating a chunk of memory as
182 * backing store. The GEM object is then added to the list of object associated
183 * with the given file and a handle to it is returned.
185 * The allocated memory will occupy a contiguous chunk of bus address space.
186 * See drm_gem_cma_create() for more details.
189 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
190 * error code on failure.
192 static struct drm_gem_cma_object *
193 drm_gem_cma_create_with_handle(struct drm_file *file_priv,
194 struct drm_device *drm, size_t size,
197 struct drm_gem_cma_object *cma_obj;
198 struct drm_gem_object *gem_obj;
201 cma_obj = drm_gem_cma_create(drm, size);
205 gem_obj = &cma_obj->base;
208 * allocate a id of idr table where the obj is registered
209 * and handle has the id what user can see.
211 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
212 /* drop reference from allocate - handle holds it now. */
213 drm_gem_object_put(gem_obj);
221 * drm_gem_cma_free - free resources associated with a CMA GEM object
222 * @cma_obj: CMA GEM object to free
224 * This function frees the backing memory of the CMA GEM object, cleans up the
225 * GEM object state and frees the memory used to store the object itself.
226 * If the buffer is imported and the virtual address is set, it is released.
228 void drm_gem_cma_free(struct drm_gem_cma_object *cma_obj)
230 struct drm_gem_object *gem_obj = &cma_obj->base;
231 struct iosys_map map = IOSYS_MAP_INIT_VADDR(cma_obj->vaddr);
233 if (gem_obj->import_attach) {
235 dma_buf_vunmap(gem_obj->import_attach->dmabuf, &map);
236 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
237 } else if (cma_obj->vaddr) {
238 if (cma_obj->map_noncoherent)
239 dma_free_noncoherent(gem_obj->dev->dev, cma_obj->base.size,
240 cma_obj->vaddr, cma_obj->paddr,
243 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
244 cma_obj->vaddr, cma_obj->paddr);
247 drm_gem_object_release(gem_obj);
251 EXPORT_SYMBOL_GPL(drm_gem_cma_free);
254 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
255 * @file_priv: DRM file-private structure to create the dumb buffer for
259 * This aligns the pitch and size arguments to the minimum required. This is
260 * an internal helper that can be wrapped by a driver to account for hardware
261 * with more specific alignment requirements. It should not be used directly
262 * as their &drm_driver.dumb_create callback.
265 * 0 on success or a negative error code on failure.
267 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
268 struct drm_device *drm,
269 struct drm_mode_create_dumb *args)
271 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
272 struct drm_gem_cma_object *cma_obj;
274 if (args->pitch < min_pitch)
275 args->pitch = min_pitch;
277 if (args->size < args->pitch * args->height)
278 args->size = args->pitch * args->height;
280 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
282 return PTR_ERR_OR_ZERO(cma_obj);
284 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
287 * drm_gem_cma_dumb_create - create a dumb buffer object
288 * @file_priv: DRM file-private structure to create the dumb buffer for
292 * This function computes the pitch of the dumb buffer and rounds it up to an
293 * integer number of bytes per pixel. Drivers for hardware that doesn't have
294 * any additional restrictions on the pitch can directly use this function as
295 * their &drm_driver.dumb_create callback.
297 * For hardware with additional restrictions, drivers can adjust the fields
298 * set up by userspace and pass the IOCTL data along to the
299 * drm_gem_cma_dumb_create_internal() function.
302 * 0 on success or a negative error code on failure.
304 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
305 struct drm_device *drm,
306 struct drm_mode_create_dumb *args)
308 struct drm_gem_cma_object *cma_obj;
310 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
311 args->size = args->pitch * args->height;
313 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
315 return PTR_ERR_OR_ZERO(cma_obj);
317 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
319 const struct vm_operations_struct drm_gem_cma_vm_ops = {
320 .open = drm_gem_vm_open,
321 .close = drm_gem_vm_close,
323 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
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
398 * @cma_obj: CMA GEM object
400 * @indent: Tab indentation level
402 * This function prints paddr and vaddr for use in e.g. debugfs output.
404 void drm_gem_cma_print_info(const struct drm_gem_cma_object *cma_obj,
405 struct drm_printer *p, unsigned int indent)
407 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
408 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
410 EXPORT_SYMBOL(drm_gem_cma_print_info);
413 * drm_gem_cma_get_sg_table - provide a scatter/gather table of pinned
414 * pages for a CMA GEM object
415 * @cma_obj: CMA GEM object
417 * This function exports a scatter/gather table by calling the standard
421 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
423 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_cma_object *cma_obj)
425 struct drm_gem_object *obj = &cma_obj->base;
426 struct sg_table *sgt;
429 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
431 return ERR_PTR(-ENOMEM);
433 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
434 cma_obj->paddr, obj->size);
444 EXPORT_SYMBOL_GPL(drm_gem_cma_get_sg_table);
447 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
448 * driver's scatter/gather table of pinned pages
449 * @dev: device to import into
450 * @attach: DMA-BUF attachment
451 * @sgt: scatter/gather table of pinned pages
453 * This function imports a scatter/gather table exported via DMA-BUF by
454 * another driver. Imported buffers must be physically contiguous in memory
455 * (i.e. the scatter/gather table must contain a single entry). Drivers that
456 * use the CMA helpers should set this as their
457 * &drm_driver.gem_prime_import_sg_table callback.
460 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
461 * error code on failure.
463 struct drm_gem_object *
464 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
465 struct dma_buf_attachment *attach,
466 struct sg_table *sgt)
468 struct drm_gem_cma_object *cma_obj;
470 /* check if the entries in the sg_table are contiguous */
471 if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size)
472 return ERR_PTR(-EINVAL);
474 /* Create a CMA GEM buffer. */
475 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size, true);
477 return ERR_CAST(cma_obj);
479 cma_obj->paddr = sg_dma_address(sgt->sgl);
482 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
484 return &cma_obj->base;
486 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
489 * drm_gem_cma_vmap - map a CMA GEM object into the kernel's virtual
491 * @cma_obj: CMA GEM object
492 * @map: Returns the kernel virtual address of the CMA GEM object's backing
495 * This function maps a buffer into the kernel's virtual address space.
496 * Since the CMA buffers are already mapped into the kernel virtual address
497 * space this simply returns the cached virtual address.
500 * 0 on success, or a negative error code otherwise.
502 int drm_gem_cma_vmap(struct drm_gem_cma_object *cma_obj,
503 struct iosys_map *map)
505 iosys_map_set_vaddr(map, cma_obj->vaddr);
509 EXPORT_SYMBOL_GPL(drm_gem_cma_vmap);
512 * drm_gem_cma_mmap - memory-map an exported CMA GEM object
513 * @cma_obj: CMA GEM object
514 * @vma: VMA for the area to be mapped
516 * This function maps a buffer into a userspace process's address space.
517 * In addition to the usual GEM VMA setup it immediately faults in the entire
518 * object instead of using on-demand faulting.
521 * 0 on success or a negative error code on failure.
523 int drm_gem_cma_mmap(struct drm_gem_cma_object *cma_obj, struct vm_area_struct *vma)
525 struct drm_gem_object *obj = &cma_obj->base;
529 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
530 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
533 vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
534 vma->vm_flags &= ~VM_PFNMAP;
535 vma->vm_flags |= VM_DONTEXPAND;
537 if (cma_obj->map_noncoherent) {
538 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
540 ret = dma_mmap_pages(cma_obj->base.dev->dev,
541 vma, vma->vm_end - vma->vm_start,
542 virt_to_page(cma_obj->vaddr));
544 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
545 cma_obj->paddr, vma->vm_end - vma->vm_start);
548 drm_gem_vm_close(vma);
552 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
555 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
556 * scatter/gather table and get the virtual address of the buffer
558 * @attach: DMA-BUF attachment
559 * @sgt: Scatter/gather table of pinned pages
561 * This function imports a scatter/gather table using
562 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
563 * virtual address. This ensures that a CMA GEM object always has its virtual
564 * address set. This address is released when the object is freed.
566 * This function can be used as the &drm_driver.gem_prime_import_sg_table
567 * callback. The &DRM_GEM_CMA_DRIVER_OPS_VMAP macro provides a shortcut to set
568 * the necessary DRM driver operations.
571 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
572 * error code on failure.
574 struct drm_gem_object *
575 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
576 struct dma_buf_attachment *attach,
577 struct sg_table *sgt)
579 struct drm_gem_cma_object *cma_obj;
580 struct drm_gem_object *obj;
581 struct iosys_map map;
584 ret = dma_buf_vmap(attach->dmabuf, &map);
586 DRM_ERROR("Failed to vmap PRIME buffer\n");
590 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
592 dma_buf_vunmap(attach->dmabuf, &map);
596 cma_obj = to_drm_gem_cma_obj(obj);
597 cma_obj->vaddr = map.vaddr;
601 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);
603 MODULE_DESCRIPTION("DRM CMA memory-management helpers");
604 MODULE_IMPORT_NS(DMA_BUF);
605 MODULE_LICENSE("GPL");