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 cma_obj
= kzalloc(sizeof(*cma_obj
), GFP_KERNEL
);
64 return ERR_PTR(-ENOMEM
);
66 gem_obj
= &cma_obj
->base
;
68 ret
= drm_gem_object_init(drm
, gem_obj
, size
);
72 ret
= drm_gem_create_mmap_offset(gem_obj
);
74 drm_gem_object_release(gem_obj
);
86 * drm_gem_cma_create - allocate an object with the given size
88 * @size: size of the object to allocate
90 * This function creates a CMA GEM object and allocates a contiguous chunk of
91 * memory as backing store. The backing memory has the writecombine attribute
95 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
96 * error code on failure.
98 struct drm_gem_cma_object
*drm_gem_cma_create(struct drm_device
*drm
,
101 struct drm_gem_cma_object
*cma_obj
;
104 size
= round_up(size
, PAGE_SIZE
);
106 cma_obj
= __drm_gem_cma_create(drm
, size
);
110 cma_obj
->vaddr
= dma_alloc_writecombine(drm
->dev
, size
,
111 &cma_obj
->paddr
, GFP_KERNEL
| __GFP_NOWARN
);
112 if (!cma_obj
->vaddr
) {
113 dev_err(drm
->dev
, "failed to allocate buffer with size %zu\n",
122 drm_gem_cma_free_object(&cma_obj
->base
);
125 EXPORT_SYMBOL_GPL(drm_gem_cma_create
);
128 * drm_gem_cma_create_with_handle - allocate an object with the given size and
129 * return a GEM handle to it
130 * @file_priv: DRM file-private structure to register the handle for
132 * @size: size of the object to allocate
133 * @handle: return location for the GEM handle
135 * This function creates a CMA GEM object, allocating a physically contiguous
136 * chunk of memory as backing store. The GEM object is then added to the list
137 * of object associated with the given file and a handle to it is returned.
140 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
141 * error code on failure.
143 static struct drm_gem_cma_object
*
144 drm_gem_cma_create_with_handle(struct drm_file
*file_priv
,
145 struct drm_device
*drm
, size_t size
,
148 struct drm_gem_cma_object
*cma_obj
;
149 struct drm_gem_object
*gem_obj
;
152 cma_obj
= drm_gem_cma_create(drm
, size
);
156 gem_obj
= &cma_obj
->base
;
159 * allocate a id of idr table where the obj is registered
160 * and handle has the id what user can see.
162 ret
= drm_gem_handle_create(file_priv
, gem_obj
, handle
);
164 goto err_handle_create
;
166 /* drop reference from allocate - handle holds it now. */
167 drm_gem_object_unreference_unlocked(gem_obj
);
172 drm_gem_cma_free_object(gem_obj
);
178 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
179 * @gem_obj: GEM object to free
181 * This function frees the backing memory of the CMA GEM object, cleans up the
182 * GEM object state and frees the memory used to store the object itself.
183 * Drivers using the CMA helpers should set this as their DRM driver's
184 * ->gem_free_object() callback.
186 void drm_gem_cma_free_object(struct drm_gem_object
*gem_obj
)
188 struct drm_gem_cma_object
*cma_obj
;
190 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
192 if (cma_obj
->vaddr
) {
193 dma_free_writecombine(gem_obj
->dev
->dev
, cma_obj
->base
.size
,
194 cma_obj
->vaddr
, cma_obj
->paddr
);
195 } else if (gem_obj
->import_attach
) {
196 drm_prime_gem_destroy(gem_obj
, cma_obj
->sgt
);
199 drm_gem_object_release(gem_obj
);
203 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object
);
206 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
207 * @file_priv: DRM file-private structure to create the dumb buffer for
211 * This aligns the pitch and size arguments to the minimum required. This is
212 * an internal helper that can be wrapped by a driver to account for hardware
213 * with more specific alignment requirements. It should not be used directly
214 * as the ->dumb_create() callback in a DRM driver.
217 * 0 on success or a negative error code on failure.
219 int drm_gem_cma_dumb_create_internal(struct drm_file
*file_priv
,
220 struct drm_device
*drm
,
221 struct drm_mode_create_dumb
*args
)
223 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
224 struct drm_gem_cma_object
*cma_obj
;
226 if (args
->pitch
< min_pitch
)
227 args
->pitch
= min_pitch
;
229 if (args
->size
< args
->pitch
* args
->height
)
230 args
->size
= args
->pitch
* args
->height
;
232 cma_obj
= drm_gem_cma_create_with_handle(file_priv
, drm
, args
->size
,
234 return PTR_ERR_OR_ZERO(cma_obj
);
236 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal
);
239 * drm_gem_cma_dumb_create - create a dumb buffer object
240 * @file_priv: DRM file-private structure to create the dumb buffer for
244 * This function computes the pitch of the dumb buffer and rounds it up to an
245 * integer number of bytes per pixel. Drivers for hardware that doesn't have
246 * any additional restrictions on the pitch can directly use this function as
247 * their ->dumb_create() callback.
249 * For hardware with additional restrictions, drivers can adjust the fields
250 * set up by userspace and pass the IOCTL data along to the
251 * drm_gem_cma_dumb_create_internal() function.
254 * 0 on success or a negative error code on failure.
256 int drm_gem_cma_dumb_create(struct drm_file
*file_priv
,
257 struct drm_device
*drm
,
258 struct drm_mode_create_dumb
*args
)
260 struct drm_gem_cma_object
*cma_obj
;
262 args
->pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
263 args
->size
= args
->pitch
* args
->height
;
265 cma_obj
= drm_gem_cma_create_with_handle(file_priv
, drm
, args
->size
,
267 return PTR_ERR_OR_ZERO(cma_obj
);
269 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create
);
272 * drm_gem_cma_dumb_map_offset - return the fake mmap offset for a CMA GEM
274 * @file_priv: DRM file-private structure containing the GEM object
276 * @handle: GEM object handle
277 * @offset: return location for the fake mmap offset
279 * This function look up an object by its handle and returns the fake mmap
280 * offset associated with it. Drivers using the CMA helpers should set this
281 * as their DRM driver's ->dumb_map_offset() callback.
284 * 0 on success or a negative error code on failure.
286 int drm_gem_cma_dumb_map_offset(struct drm_file
*file_priv
,
287 struct drm_device
*drm
, u32 handle
,
290 struct drm_gem_object
*gem_obj
;
292 mutex_lock(&drm
->struct_mutex
);
294 gem_obj
= drm_gem_object_lookup(drm
, file_priv
, handle
);
296 dev_err(drm
->dev
, "failed to lookup GEM object\n");
297 mutex_unlock(&drm
->struct_mutex
);
301 *offset
= drm_vma_node_offset_addr(&gem_obj
->vma_node
);
303 drm_gem_object_unreference(gem_obj
);
305 mutex_unlock(&drm
->struct_mutex
);
309 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset
);
311 const struct vm_operations_struct drm_gem_cma_vm_ops
= {
312 .open
= drm_gem_vm_open
,
313 .close
= drm_gem_vm_close
,
315 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops
);
317 static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object
*cma_obj
,
318 struct vm_area_struct
*vma
)
323 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
324 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
327 vma
->vm_flags
&= ~VM_PFNMAP
;
330 ret
= dma_mmap_writecombine(cma_obj
->base
.dev
->dev
, vma
,
331 cma_obj
->vaddr
, cma_obj
->paddr
,
332 vma
->vm_end
- vma
->vm_start
);
334 drm_gem_vm_close(vma
);
340 * drm_gem_cma_mmap - memory-map a CMA GEM object
342 * @vma: VMA for the area to be mapped
344 * This function implements an augmented version of the GEM DRM file mmap
345 * operation for CMA objects: In addition to the usual GEM VMA setup it
346 * immediately faults in the entire object instead of using on-demaind
347 * faulting. Drivers which employ the CMA helpers should use this function
348 * as their ->mmap() handler in the DRM device file's file_operations
352 * 0 on success or a negative error code on failure.
354 int drm_gem_cma_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
356 struct drm_gem_cma_object
*cma_obj
;
357 struct drm_gem_object
*gem_obj
;
360 ret
= drm_gem_mmap(filp
, vma
);
364 gem_obj
= vma
->vm_private_data
;
365 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
367 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
369 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap
);
371 #ifdef CONFIG_DEBUG_FS
373 * drm_gem_cma_describe - describe a CMA GEM object for debugfs
374 * @cma_obj: CMA GEM object
375 * @m: debugfs file handle
377 * This function can be used to dump a human-readable representation of the
378 * CMA GEM object into a synthetic file.
380 void drm_gem_cma_describe(struct drm_gem_cma_object
*cma_obj
,
383 struct drm_gem_object
*obj
= &cma_obj
->base
;
384 struct drm_device
*dev
= obj
->dev
;
387 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
389 off
= drm_vma_node_start(&obj
->vma_node
);
391 seq_printf(m
, "%2d (%2d) %08llx %pad %p %zu",
392 obj
->name
, obj
->refcount
.refcount
.counter
,
393 off
, &cma_obj
->paddr
, cma_obj
->vaddr
, obj
->size
);
397 EXPORT_SYMBOL_GPL(drm_gem_cma_describe
);
401 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
402 * pages for a CMA GEM object
405 * This function exports a scatter/gather table suitable for PRIME usage by
406 * calling the standard DMA mapping API. Drivers using the CMA helpers should
407 * set this as their DRM driver's ->gem_prime_get_sg_table() callback.
410 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
412 struct sg_table
*drm_gem_cma_prime_get_sg_table(struct drm_gem_object
*obj
)
414 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
415 struct sg_table
*sgt
;
418 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
422 ret
= dma_get_sgtable(obj
->dev
->dev
, sgt
, cma_obj
->vaddr
,
423 cma_obj
->paddr
, obj
->size
);
433 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table
);
436 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
437 * driver's scatter/gather table of pinned pages
438 * @dev: device to import into
439 * @attach: DMA-BUF attachment
440 * @sgt: scatter/gather table of pinned pages
442 * This function imports a scatter/gather table exported via DMA-BUF by
443 * another driver. Imported buffers must be physically contiguous in memory
444 * (i.e. the scatter/gather table must contain a single entry). Drivers that
445 * use the CMA helpers should set this as their DRM driver's
446 * ->gem_prime_import_sg_table() callback.
449 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
450 * error code on failure.
452 struct drm_gem_object
*
453 drm_gem_cma_prime_import_sg_table(struct drm_device
*dev
,
454 struct dma_buf_attachment
*attach
,
455 struct sg_table
*sgt
)
457 struct drm_gem_cma_object
*cma_obj
;
460 return ERR_PTR(-EINVAL
);
462 /* Create a CMA GEM buffer. */
463 cma_obj
= __drm_gem_cma_create(dev
, attach
->dmabuf
->size
);
465 return ERR_CAST(cma_obj
);
467 cma_obj
->paddr
= sg_dma_address(sgt
->sgl
);
470 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj
->paddr
, attach
->dmabuf
->size
);
472 return &cma_obj
->base
;
474 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table
);
477 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
479 * @vma: VMA for the area to be mapped
481 * This function maps a buffer imported via DRM PRIME into a userspace
482 * process's address space. Drivers that use the CMA helpers should set this
483 * as their DRM driver's ->gem_prime_mmap() callback.
486 * 0 on success or a negative error code on failure.
488 int drm_gem_cma_prime_mmap(struct drm_gem_object
*obj
,
489 struct vm_area_struct
*vma
)
491 struct drm_gem_cma_object
*cma_obj
;
492 struct drm_device
*dev
= obj
->dev
;
495 mutex_lock(&dev
->struct_mutex
);
496 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
497 mutex_unlock(&dev
->struct_mutex
);
501 cma_obj
= to_drm_gem_cma_obj(obj
);
502 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
504 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap
);
507 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
511 * This function maps a buffer exported via DRM PRIME into the kernel's
512 * virtual address space. Since the CMA buffers are already mapped into the
513 * kernel virtual address space this simply returns the cached virtual
514 * address. Drivers using the CMA helpers should set this as their DRM
515 * driver's ->gem_prime_vmap() callback.
518 * The kernel virtual address of the CMA GEM object's backing store.
520 void *drm_gem_cma_prime_vmap(struct drm_gem_object
*obj
)
522 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
524 return cma_obj
->vaddr
;
526 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap
);
529 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
532 * @vaddr: kernel virtual address where the CMA GEM object was mapped
534 * This function removes a buffer exported via DRM PRIME from the kernel's
535 * virtual address space. This is a no-op because CMA buffers cannot be
536 * unmapped from kernel space. Drivers using the CMA helpers should set this
537 * as their DRM driver's ->gem_prime_vunmap() callback.
539 void drm_gem_cma_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
543 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap
);