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.
36 static const struct drm_gem_object_funcs drm_gem_cma_default_funcs = {
37 .free = drm_gem_cma_free_object,
38 .print_info = drm_gem_cma_print_info,
39 .get_sg_table = drm_gem_cma_get_sg_table,
40 .vmap = drm_gem_cma_vmap,
41 .mmap = drm_gem_cma_mmap,
42 .vm_ops = &drm_gem_cma_vm_ops,
46 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
48 * @size: size of the object to allocate
50 * This function creates and initializes a GEM CMA object of the given size,
51 * but doesn't allocate any memory to back the object.
54 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
55 * error code on failure.
57 static struct drm_gem_cma_object *
58 __drm_gem_cma_create(struct drm_device *drm, size_t size)
60 struct drm_gem_cma_object *cma_obj;
61 struct drm_gem_object *gem_obj;
64 if (drm->driver->gem_create_object)
65 gem_obj = drm->driver->gem_create_object(drm, size);
67 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
69 return ERR_PTR(-ENOMEM);
72 gem_obj->funcs = &drm_gem_cma_default_funcs;
74 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
76 ret = drm_gem_object_init(drm, gem_obj, size);
80 ret = drm_gem_create_mmap_offset(gem_obj);
82 drm_gem_object_release(gem_obj);
94 * drm_gem_cma_create - allocate an object with the given size
96 * @size: size of the object to allocate
98 * This function creates a CMA GEM object and allocates a contiguous chunk of
99 * memory as backing store. The backing memory has the writecombine attribute
103 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
104 * error code on failure.
106 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
109 struct drm_gem_cma_object *cma_obj;
112 size = round_up(size, PAGE_SIZE);
114 cma_obj = __drm_gem_cma_create(drm, size);
118 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
119 GFP_KERNEL | __GFP_NOWARN);
120 if (!cma_obj->vaddr) {
121 drm_dbg(drm, "failed to allocate buffer with size %zu\n",
130 drm_gem_object_put(&cma_obj->base);
133 EXPORT_SYMBOL_GPL(drm_gem_cma_create);
136 * drm_gem_cma_create_with_handle - allocate an object with the given size and
137 * return a GEM handle to it
138 * @file_priv: DRM file-private structure to register the handle for
140 * @size: size of the object to allocate
141 * @handle: return location for the GEM handle
143 * This function creates a CMA GEM object, allocating a physically contiguous
144 * chunk of memory as backing store. The GEM object is then added to the list
145 * of object associated with the given file and a handle to it is returned.
148 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
149 * error code on failure.
151 static struct drm_gem_cma_object *
152 drm_gem_cma_create_with_handle(struct drm_file *file_priv,
153 struct drm_device *drm, size_t size,
156 struct drm_gem_cma_object *cma_obj;
157 struct drm_gem_object *gem_obj;
160 cma_obj = drm_gem_cma_create(drm, size);
164 gem_obj = &cma_obj->base;
167 * allocate a id of idr table where the obj is registered
168 * and handle has the id what user can see.
170 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
171 /* drop reference from allocate - handle holds it now. */
172 drm_gem_object_put(gem_obj);
180 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
181 * @gem_obj: GEM object to free
183 * This function frees the backing memory of the CMA GEM object, cleans up the
184 * GEM object state and frees the memory used to store the object itself.
185 * If the buffer is imported and the virtual address is set, it is released.
186 * Drivers using the CMA helpers should set this as their
187 * &drm_gem_object_funcs.free callback.
189 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
191 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(gem_obj);
192 struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(cma_obj->vaddr);
194 if (gem_obj->import_attach) {
196 dma_buf_vunmap(gem_obj->import_attach->dmabuf, &map);
197 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
198 } else if (cma_obj->vaddr) {
199 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
200 cma_obj->vaddr, cma_obj->paddr);
203 drm_gem_object_release(gem_obj);
207 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
210 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
211 * @file_priv: DRM file-private structure to create the dumb buffer for
215 * This aligns the pitch and size arguments to the minimum required. This is
216 * an internal helper that can be wrapped by a driver to account for hardware
217 * with more specific alignment requirements. It should not be used directly
218 * as their &drm_driver.dumb_create callback.
221 * 0 on success or a negative error code on failure.
223 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
224 struct drm_device *drm,
225 struct drm_mode_create_dumb *args)
227 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
228 struct drm_gem_cma_object *cma_obj;
230 if (args->pitch < min_pitch)
231 args->pitch = min_pitch;
233 if (args->size < args->pitch * args->height)
234 args->size = args->pitch * args->height;
236 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
238 return PTR_ERR_OR_ZERO(cma_obj);
240 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
243 * drm_gem_cma_dumb_create - create a dumb buffer object
244 * @file_priv: DRM file-private structure to create the dumb buffer for
248 * This function computes the pitch of the dumb buffer and rounds it up to an
249 * integer number of bytes per pixel. Drivers for hardware that doesn't have
250 * any additional restrictions on the pitch can directly use this function as
251 * their &drm_driver.dumb_create callback.
253 * For hardware with additional restrictions, drivers can adjust the fields
254 * set up by userspace and pass the IOCTL data along to the
255 * drm_gem_cma_dumb_create_internal() function.
258 * 0 on success or a negative error code on failure.
260 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
261 struct drm_device *drm,
262 struct drm_mode_create_dumb *args)
264 struct drm_gem_cma_object *cma_obj;
266 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
267 args->size = args->pitch * args->height;
269 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
271 return PTR_ERR_OR_ZERO(cma_obj);
273 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
275 const struct vm_operations_struct drm_gem_cma_vm_ops = {
276 .open = drm_gem_vm_open,
277 .close = drm_gem_vm_close,
279 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
283 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
285 * @addr: memory address
287 * @pgoff: page offset
288 * @flags: memory flags
290 * This function is used in noMMU platforms to propose address mapping
291 * for a given buffer.
292 * It's intended to be used as a direct handler for the struct
293 * &file_operations.get_unmapped_area operation.
296 * mapping address on success or a negative error code on failure.
298 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
304 struct drm_gem_cma_object *cma_obj;
305 struct drm_gem_object *obj = NULL;
306 struct drm_file *priv = filp->private_data;
307 struct drm_device *dev = priv->minor->dev;
308 struct drm_vma_offset_node *node;
310 if (drm_dev_is_unplugged(dev))
313 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
314 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
318 obj = container_of(node, struct drm_gem_object, vma_node);
320 * When the object is being freed, after it hits 0-refcnt it
321 * proceeds to tear down the object. In the process it will
322 * attempt to remove the VMA offset and so acquire this
323 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
324 * that matches our range, we know it is in the process of being
325 * destroyed and will be freed as soon as we release the lock -
326 * so we have to check for the 0-refcnted object and treat it as
329 if (!kref_get_unless_zero(&obj->refcount))
333 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
338 if (!drm_vma_node_is_allowed(node, priv)) {
339 drm_gem_object_put(obj);
343 cma_obj = to_drm_gem_cma_obj(obj);
345 drm_gem_object_put(obj);
347 return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
349 EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
353 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
355 * @indent: Tab indentation level
358 * This function can be used as the &drm_driver->gem_print_info callback.
359 * It prints paddr and vaddr for use in e.g. debugfs output.
361 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
362 const struct drm_gem_object *obj)
364 const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
366 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
367 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
369 EXPORT_SYMBOL(drm_gem_cma_print_info);
372 * drm_gem_cma_get_sg_table - provide a scatter/gather table of pinned
373 * pages for a CMA GEM object
376 * This function exports a scatter/gather table by
377 * calling the standard DMA mapping API. Drivers using the CMA helpers should
378 * set this as their &drm_gem_object_funcs.get_sg_table callback.
381 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
383 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj)
385 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
386 struct sg_table *sgt;
389 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
391 return ERR_PTR(-ENOMEM);
393 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
394 cma_obj->paddr, obj->size);
404 EXPORT_SYMBOL_GPL(drm_gem_cma_get_sg_table);
407 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
408 * driver's scatter/gather table of pinned pages
409 * @dev: device to import into
410 * @attach: DMA-BUF attachment
411 * @sgt: scatter/gather table of pinned pages
413 * This function imports a scatter/gather table exported via DMA-BUF by
414 * another driver. Imported buffers must be physically contiguous in memory
415 * (i.e. the scatter/gather table must contain a single entry). Drivers that
416 * use the CMA helpers should set this as their
417 * &drm_driver.gem_prime_import_sg_table callback.
420 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
421 * error code on failure.
423 struct drm_gem_object *
424 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
425 struct dma_buf_attachment *attach,
426 struct sg_table *sgt)
428 struct drm_gem_cma_object *cma_obj;
430 /* check if the entries in the sg_table are contiguous */
431 if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size)
432 return ERR_PTR(-EINVAL);
434 /* Create a CMA GEM buffer. */
435 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
437 return ERR_CAST(cma_obj);
439 cma_obj->paddr = sg_dma_address(sgt->sgl);
442 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
444 return &cma_obj->base;
446 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
449 * drm_gem_cma_vmap - map a CMA GEM object into the kernel's virtual
452 * @map: Returns the kernel virtual address of the CMA GEM object's backing
455 * This function maps a buffer into the kernel's
456 * virtual address space. Since the CMA buffers are already mapped into the
457 * kernel virtual address space this simply returns the cached virtual
458 * address. Drivers using the CMA helpers should set this as their DRM
459 * driver's &drm_gem_object_funcs.vmap callback.
462 * 0 on success, or a negative error code otherwise.
464 int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
466 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
468 dma_buf_map_set_vaddr(map, cma_obj->vaddr);
472 EXPORT_SYMBOL_GPL(drm_gem_cma_vmap);
475 * drm_gem_cma_mmap - memory-map an exported CMA GEM object
477 * @vma: VMA for the area to be mapped
479 * This function maps a buffer into a userspace process's address space.
480 * In addition to the usual GEM VMA setup it immediately faults in the entire
481 * object instead of using on-demand faulting. Drivers that use the CMA
482 * helpers should set this as their &drm_gem_object_funcs.mmap callback.
485 * 0 on success or a negative error code on failure.
487 int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
489 struct drm_gem_cma_object *cma_obj;
493 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
494 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
497 vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
498 vma->vm_flags &= ~VM_PFNMAP;
500 cma_obj = to_drm_gem_cma_obj(obj);
502 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
503 cma_obj->paddr, vma->vm_end - vma->vm_start);
505 drm_gem_vm_close(vma);
509 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
512 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
513 * scatter/gather table and get the virtual address of the buffer
515 * @attach: DMA-BUF attachment
516 * @sgt: Scatter/gather table of pinned pages
518 * This function imports a scatter/gather table using
519 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
520 * virtual address. This ensures that a CMA GEM object always has its virtual
521 * address set. This address is released when the object is freed.
523 * This function can be used as the &drm_driver.gem_prime_import_sg_table
524 * callback. The &DRM_GEM_CMA_DRIVER_OPS_VMAP macro provides a shortcut to set
525 * the necessary DRM driver operations.
528 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
529 * error code on failure.
531 struct drm_gem_object *
532 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
533 struct dma_buf_attachment *attach,
534 struct sg_table *sgt)
536 struct drm_gem_cma_object *cma_obj;
537 struct drm_gem_object *obj;
538 struct dma_buf_map map;
541 ret = dma_buf_vmap(attach->dmabuf, &map);
543 DRM_ERROR("Failed to vmap PRIME buffer\n");
547 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
549 dma_buf_vunmap(attach->dmabuf, &map);
553 cma_obj = to_drm_gem_cma_obj(obj);
554 cma_obj->vaddr = map.vaddr;
558 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);