2 * drm gem CMA (contiguous memory allocator) helper functions
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
6 * Based on Samsung Exynos code
8 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/export.h>
24 #include <linux/dma-buf.h>
25 #include <linux/dma-mapping.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_vma_manager.h>
35 * The Contiguous Memory Allocator reserves a pool of memory at early boot
36 * that is used to service requests for large blocks of contiguous memory.
38 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
39 * objects that are physically contiguous in memory. This is useful for
40 * display drivers that are unable to map scattered buffers via an IOMMU.
44 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
46 * @size: size of the object to allocate
48 * This function creates and initializes a GEM CMA object of the given size,
49 * but doesn't allocate any memory to back the object.
52 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
53 * error code on failure.
55 static struct drm_gem_cma_object
*
56 __drm_gem_cma_create(struct drm_device
*drm
, size_t size
)
58 struct drm_gem_cma_object
*cma_obj
;
59 struct drm_gem_object
*gem_obj
;
62 if (drm
->driver
->gem_create_object
)
63 gem_obj
= drm
->driver
->gem_create_object(drm
, size
);
65 gem_obj
= kzalloc(sizeof(*cma_obj
), GFP_KERNEL
);
67 return ERR_PTR(-ENOMEM
);
68 cma_obj
= container_of(gem_obj
, struct drm_gem_cma_object
, base
);
70 ret
= drm_gem_object_init(drm
, gem_obj
, size
);
74 ret
= drm_gem_create_mmap_offset(gem_obj
);
76 drm_gem_object_release(gem_obj
);
88 * drm_gem_cma_create - allocate an object with the given size
90 * @size: size of the object to allocate
92 * This function creates a CMA GEM object and allocates a contiguous chunk of
93 * memory as backing store. The backing memory has the writecombine attribute
97 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
98 * error code on failure.
100 struct drm_gem_cma_object
*drm_gem_cma_create(struct drm_device
*drm
,
103 struct drm_gem_cma_object
*cma_obj
;
106 size
= round_up(size
, PAGE_SIZE
);
108 cma_obj
= __drm_gem_cma_create(drm
, size
);
112 cma_obj
->vaddr
= dma_alloc_wc(drm
->dev
, size
, &cma_obj
->paddr
,
113 GFP_KERNEL
| __GFP_NOWARN
);
114 if (!cma_obj
->vaddr
) {
115 dev_dbg(drm
->dev
, "failed to allocate buffer with size %zu\n",
124 drm_gem_object_put_unlocked(&cma_obj
->base
);
127 EXPORT_SYMBOL_GPL(drm_gem_cma_create
);
130 * drm_gem_cma_create_with_handle - allocate an object with the given size and
131 * return a GEM handle to it
132 * @file_priv: DRM file-private structure to register the handle for
134 * @size: size of the object to allocate
135 * @handle: return location for the GEM handle
137 * This function creates a CMA GEM object, allocating a physically contiguous
138 * chunk of memory as backing store. The GEM object is then added to the list
139 * of object associated with the given file and a handle to it is returned.
142 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
143 * error code on failure.
145 static struct drm_gem_cma_object
*
146 drm_gem_cma_create_with_handle(struct drm_file
*file_priv
,
147 struct drm_device
*drm
, size_t size
,
150 struct drm_gem_cma_object
*cma_obj
;
151 struct drm_gem_object
*gem_obj
;
154 cma_obj
= drm_gem_cma_create(drm
, size
);
158 gem_obj
= &cma_obj
->base
;
161 * allocate a id of idr table where the obj is registered
162 * and handle has the id what user can see.
164 ret
= drm_gem_handle_create(file_priv
, gem_obj
, handle
);
165 /* drop reference from allocate - handle holds it now. */
166 drm_gem_object_put_unlocked(gem_obj
);
174 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
175 * @gem_obj: GEM object to free
177 * This function frees the backing memory of the CMA GEM object, cleans up the
178 * GEM object state and frees the memory used to store the object itself.
179 * If the buffer is imported and the virtual address is set, it is released.
180 * Drivers using the CMA helpers should set this as their
181 * &drm_driver.gem_free_object_unlocked callback.
183 void drm_gem_cma_free_object(struct drm_gem_object
*gem_obj
)
185 struct drm_gem_cma_object
*cma_obj
;
187 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
189 if (cma_obj
->vaddr
) {
190 dma_free_wc(gem_obj
->dev
->dev
, cma_obj
->base
.size
,
191 cma_obj
->vaddr
, cma_obj
->paddr
);
192 } else if (gem_obj
->import_attach
) {
194 dma_buf_vunmap(gem_obj
->import_attach
->dmabuf
, cma_obj
->vaddr
);
195 drm_prime_gem_destroy(gem_obj
, cma_obj
->sgt
);
198 drm_gem_object_release(gem_obj
);
202 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object
);
205 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
206 * @file_priv: DRM file-private structure to create the dumb buffer for
210 * This aligns the pitch and size arguments to the minimum required. This is
211 * an internal helper that can be wrapped by a driver to account for hardware
212 * with more specific alignment requirements. It should not be used directly
213 * as their &drm_driver.dumb_create callback.
216 * 0 on success or a negative error code on failure.
218 int drm_gem_cma_dumb_create_internal(struct drm_file
*file_priv
,
219 struct drm_device
*drm
,
220 struct drm_mode_create_dumb
*args
)
222 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
223 struct drm_gem_cma_object
*cma_obj
;
225 if (args
->pitch
< min_pitch
)
226 args
->pitch
= min_pitch
;
228 if (args
->size
< args
->pitch
* args
->height
)
229 args
->size
= args
->pitch
* args
->height
;
231 cma_obj
= drm_gem_cma_create_with_handle(file_priv
, drm
, args
->size
,
233 return PTR_ERR_OR_ZERO(cma_obj
);
235 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal
);
238 * drm_gem_cma_dumb_create - create a dumb buffer object
239 * @file_priv: DRM file-private structure to create the dumb buffer for
243 * This function computes the pitch of the dumb buffer and rounds it up to an
244 * integer number of bytes per pixel. Drivers for hardware that doesn't have
245 * any additional restrictions on the pitch can directly use this function as
246 * their &drm_driver.dumb_create callback.
248 * For hardware with additional restrictions, drivers can adjust the fields
249 * set up by userspace and pass the IOCTL data along to the
250 * drm_gem_cma_dumb_create_internal() function.
253 * 0 on success or a negative error code on failure.
255 int drm_gem_cma_dumb_create(struct drm_file
*file_priv
,
256 struct drm_device
*drm
,
257 struct drm_mode_create_dumb
*args
)
259 struct drm_gem_cma_object
*cma_obj
;
261 args
->pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
262 args
->size
= args
->pitch
* args
->height
;
264 cma_obj
= drm_gem_cma_create_with_handle(file_priv
, drm
, args
->size
,
266 return PTR_ERR_OR_ZERO(cma_obj
);
268 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create
);
270 const struct vm_operations_struct drm_gem_cma_vm_ops
= {
271 .open
= drm_gem_vm_open
,
272 .close
= drm_gem_vm_close
,
274 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops
);
276 static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object
*cma_obj
,
277 struct vm_area_struct
*vma
)
282 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
283 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
286 vma
->vm_flags
&= ~VM_PFNMAP
;
289 ret
= dma_mmap_wc(cma_obj
->base
.dev
->dev
, vma
, cma_obj
->vaddr
,
290 cma_obj
->paddr
, vma
->vm_end
- vma
->vm_start
);
292 drm_gem_vm_close(vma
);
298 * drm_gem_cma_mmap - memory-map a CMA GEM object
300 * @vma: VMA for the area to be mapped
302 * This function implements an augmented version of the GEM DRM file mmap
303 * operation for CMA objects: In addition to the usual GEM VMA setup it
304 * immediately faults in the entire object instead of using on-demaind
305 * faulting. Drivers which employ the CMA helpers should use this function
306 * as their ->mmap() handler in the DRM device file's file_operations
309 * Instead of directly referencing this function, drivers should use the
310 * DEFINE_DRM_GEM_CMA_FOPS().macro.
313 * 0 on success or a negative error code on failure.
315 int drm_gem_cma_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
317 struct drm_gem_cma_object
*cma_obj
;
318 struct drm_gem_object
*gem_obj
;
321 ret
= drm_gem_mmap(filp
, vma
);
325 gem_obj
= vma
->vm_private_data
;
326 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
328 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
330 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap
);
334 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
336 * @addr: memory address
338 * @pgoff: page offset
339 * @flags: memory flags
341 * This function is used in noMMU platforms to propose address mapping
342 * for a given buffer.
343 * It's intended to be used as a direct handler for the struct
344 * &file_operations.get_unmapped_area operation.
347 * mapping address on success or a negative error code on failure.
349 unsigned long drm_gem_cma_get_unmapped_area(struct file
*filp
,
355 struct drm_gem_cma_object
*cma_obj
;
356 struct drm_gem_object
*obj
= NULL
;
357 struct drm_file
*priv
= filp
->private_data
;
358 struct drm_device
*dev
= priv
->minor
->dev
;
359 struct drm_vma_offset_node
*node
;
361 if (drm_dev_is_unplugged(dev
))
364 drm_vma_offset_lock_lookup(dev
->vma_offset_manager
);
365 node
= drm_vma_offset_exact_lookup_locked(dev
->vma_offset_manager
,
369 obj
= container_of(node
, struct drm_gem_object
, vma_node
);
371 * When the object is being freed, after it hits 0-refcnt it
372 * proceeds to tear down the object. In the process it will
373 * attempt to remove the VMA offset and so acquire this
374 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
375 * that matches our range, we know it is in the process of being
376 * destroyed and will be freed as soon as we release the lock -
377 * so we have to check for the 0-refcnted object and treat it as
380 if (!kref_get_unless_zero(&obj
->refcount
))
384 drm_vma_offset_unlock_lookup(dev
->vma_offset_manager
);
389 if (!drm_vma_node_is_allowed(node
, priv
)) {
390 drm_gem_object_put_unlocked(obj
);
394 cma_obj
= to_drm_gem_cma_obj(obj
);
396 drm_gem_object_put_unlocked(obj
);
398 return cma_obj
->vaddr
? (unsigned long)cma_obj
->vaddr
: -EINVAL
;
400 EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area
);
404 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
406 * @indent: Tab indentation level
409 * This function can be used as the &drm_driver->gem_print_info callback.
410 * It prints paddr and vaddr for use in e.g. debugfs output.
412 void drm_gem_cma_print_info(struct drm_printer
*p
, unsigned int indent
,
413 const struct drm_gem_object
*obj
)
415 const struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
417 drm_printf_indent(p
, indent
, "paddr=%pad\n", &cma_obj
->paddr
);
418 drm_printf_indent(p
, indent
, "vaddr=%p\n", cma_obj
->vaddr
);
420 EXPORT_SYMBOL(drm_gem_cma_print_info
);
423 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
424 * pages for a CMA GEM object
427 * This function exports a scatter/gather table suitable for PRIME usage by
428 * calling the standard DMA mapping API. Drivers using the CMA helpers should
429 * set this as their &drm_driver.gem_prime_get_sg_table callback.
432 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
434 struct sg_table
*drm_gem_cma_prime_get_sg_table(struct drm_gem_object
*obj
)
436 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
437 struct sg_table
*sgt
;
440 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
442 return ERR_PTR(-ENOMEM
);
444 ret
= dma_get_sgtable(obj
->dev
->dev
, sgt
, cma_obj
->vaddr
,
445 cma_obj
->paddr
, obj
->size
);
455 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table
);
458 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
459 * driver's scatter/gather table of pinned pages
460 * @dev: device to import into
461 * @attach: DMA-BUF attachment
462 * @sgt: scatter/gather table of pinned pages
464 * This function imports a scatter/gather table exported via DMA-BUF by
465 * another driver. Imported buffers must be physically contiguous in memory
466 * (i.e. the scatter/gather table must contain a single entry). Drivers that
467 * use the CMA helpers should set this as their
468 * &drm_driver.gem_prime_import_sg_table callback.
471 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
472 * error code on failure.
474 struct drm_gem_object
*
475 drm_gem_cma_prime_import_sg_table(struct drm_device
*dev
,
476 struct dma_buf_attachment
*attach
,
477 struct sg_table
*sgt
)
479 struct drm_gem_cma_object
*cma_obj
;
481 if (sgt
->nents
!= 1) {
482 /* check if the entries in the sg_table are contiguous */
483 dma_addr_t next_addr
= sg_dma_address(sgt
->sgl
);
484 struct scatterlist
*s
;
487 for_each_sg(sgt
->sgl
, s
, sgt
->nents
, i
) {
489 * sg_dma_address(s) is only valid for entries
490 * that have sg_dma_len(s) != 0
495 if (sg_dma_address(s
) != next_addr
)
496 return ERR_PTR(-EINVAL
);
498 next_addr
= sg_dma_address(s
) + sg_dma_len(s
);
502 /* Create a CMA GEM buffer. */
503 cma_obj
= __drm_gem_cma_create(dev
, attach
->dmabuf
->size
);
505 return ERR_CAST(cma_obj
);
507 cma_obj
->paddr
= sg_dma_address(sgt
->sgl
);
510 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj
->paddr
, attach
->dmabuf
->size
);
512 return &cma_obj
->base
;
514 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table
);
517 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
519 * @vma: VMA for the area to be mapped
521 * This function maps a buffer imported via DRM PRIME into a userspace
522 * process's address space. Drivers that use the CMA helpers should set this
523 * as their &drm_driver.gem_prime_mmap callback.
526 * 0 on success or a negative error code on failure.
528 int drm_gem_cma_prime_mmap(struct drm_gem_object
*obj
,
529 struct vm_area_struct
*vma
)
531 struct drm_gem_cma_object
*cma_obj
;
534 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
538 cma_obj
= to_drm_gem_cma_obj(obj
);
539 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
541 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap
);
544 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
548 * This function maps a buffer exported via DRM PRIME into the kernel's
549 * virtual address space. Since the CMA buffers are already mapped into the
550 * kernel virtual address space this simply returns the cached virtual
551 * address. Drivers using the CMA helpers should set this as their DRM
552 * driver's &drm_driver.gem_prime_vmap callback.
555 * The kernel virtual address of the CMA GEM object's backing store.
557 void *drm_gem_cma_prime_vmap(struct drm_gem_object
*obj
)
559 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
561 return cma_obj
->vaddr
;
563 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap
);
566 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
569 * @vaddr: kernel virtual address where the CMA GEM object was mapped
571 * This function removes a buffer exported via DRM PRIME from the kernel's
572 * virtual address space. This is a no-op because CMA buffers cannot be
573 * unmapped from kernel space. Drivers using the CMA helpers should set this
574 * as their &drm_driver.gem_prime_vunmap callback.
576 void drm_gem_cma_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
580 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap
);
582 static const struct drm_gem_object_funcs drm_cma_gem_default_funcs
= {
583 .free
= drm_gem_cma_free_object
,
584 .print_info
= drm_gem_cma_print_info
,
585 .get_sg_table
= drm_gem_cma_prime_get_sg_table
,
586 .vmap
= drm_gem_cma_prime_vmap
,
587 .vm_ops
= &drm_gem_cma_vm_ops
,
591 * drm_cma_gem_create_object_default_funcs - Create a CMA GEM object with a
592 * default function table
594 * @size: Size of the object to allocate
596 * This sets the GEM object functions to the default CMA helper functions.
597 * This function can be used as the &drm_driver.gem_create_object callback.
600 * A pointer to a allocated GEM object or an error pointer on failure.
602 struct drm_gem_object
*
603 drm_cma_gem_create_object_default_funcs(struct drm_device
*dev
, size_t size
)
605 struct drm_gem_cma_object
*cma_obj
;
607 cma_obj
= kzalloc(sizeof(*cma_obj
), GFP_KERNEL
);
611 cma_obj
->base
.funcs
= &drm_cma_gem_default_funcs
;
613 return &cma_obj
->base
;
615 EXPORT_SYMBOL(drm_cma_gem_create_object_default_funcs
);
618 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
619 * scatter/gather table and get the virtual address of the buffer
621 * @attach: DMA-BUF attachment
622 * @sgt: Scatter/gather table of pinned pages
624 * This function imports a scatter/gather table using
625 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
626 * virtual address. This ensures that a CMA GEM object always has its virtual
627 * address set. This address is released when the object is freed.
629 * This function can be used as the &drm_driver.gem_prime_import_sg_table
630 * callback. The DRM_GEM_CMA_VMAP_DRIVER_OPS() macro provides a shortcut to set
631 * the necessary DRM driver operations.
634 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
635 * error code on failure.
637 struct drm_gem_object
*
638 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device
*dev
,
639 struct dma_buf_attachment
*attach
,
640 struct sg_table
*sgt
)
642 struct drm_gem_cma_object
*cma_obj
;
643 struct drm_gem_object
*obj
;
646 vaddr
= dma_buf_vmap(attach
->dmabuf
);
648 DRM_ERROR("Failed to vmap PRIME buffer\n");
649 return ERR_PTR(-ENOMEM
);
652 obj
= drm_gem_cma_prime_import_sg_table(dev
, attach
, sgt
);
654 dma_buf_vunmap(attach
->dmabuf
, vaddr
);
658 cma_obj
= to_drm_gem_cma_obj(obj
);
659 cma_obj
->vaddr
= vaddr
;
663 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap
);