]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/gpu/drm/drm_gem_cma_helper.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[thirdparty/kernel/stable.git] / drivers / gpu / drm / drm_gem_cma_helper.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
b9d47450
SH
2/*
3 * drm gem CMA (contiguous memory allocator) helper functions
4 *
5 * Copyright (C) 2012 Sascha Hauer, Pengutronix
6 *
7 * Based on Samsung Exynos code
8 *
9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
b9d47450
SH
10 */
11
12#include <linux/mm.h>
13#include <linux/slab.h>
14#include <linux/mutex.h>
15#include <linux/export.h>
71d7282a 16#include <linux/dma-buf.h>
b9d47450
SH
17#include <linux/dma-mapping.h>
18
19#include <drm/drmP.h>
20#include <drm/drm.h>
21#include <drm/drm_gem_cma_helper.h>
0de23977 22#include <drm/drm_vma_manager.h>
b9d47450 23
d7883f87
TR
24/**
25 * DOC: cma helpers
26 *
27 * The Contiguous Memory Allocator reserves a pool of memory at early boot
28 * that is used to service requests for large blocks of contiguous memory.
29 *
30 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
31 * objects that are physically contiguous in memory. This is useful for
32 * display drivers that are unable to map scattered buffers via an IOMMU.
33 */
34
35/**
a5ed8940 36 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
d7883f87
TR
37 * @drm: DRM device
38 * @size: size of the object to allocate
b9d47450 39 *
d7883f87
TR
40 * This function creates and initializes a GEM CMA object of the given size,
41 * but doesn't allocate any memory to back the object.
a5ed8940 42 *
d7883f87
TR
43 * Returns:
44 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
45 * error code on failure.
b9d47450 46 */
a5ed8940 47static struct drm_gem_cma_object *
d7883f87 48__drm_gem_cma_create(struct drm_device *drm, size_t size)
b9d47450
SH
49{
50 struct drm_gem_cma_object *cma_obj;
51 struct drm_gem_object *gem_obj;
52 int ret;
53
10028c5a
EA
54 if (drm->driver->gem_create_object)
55 gem_obj = drm->driver->gem_create_object(drm, size);
56 else
57 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
58 if (!gem_obj)
b9d47450 59 return ERR_PTR(-ENOMEM);
10028c5a 60 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
b9d47450
SH
61
62 ret = drm_gem_object_init(drm, gem_obj, size);
63 if (ret)
a5ed8940 64 goto error;
b9d47450
SH
65
66 ret = drm_gem_create_mmap_offset(gem_obj);
a5ed8940
LP
67 if (ret) {
68 drm_gem_object_release(gem_obj);
69 goto error;
70 }
b9d47450
SH
71
72 return cma_obj;
73
a5ed8940
LP
74error:
75 kfree(cma_obj);
76 return ERR_PTR(ret);
77}
b9d47450 78
d7883f87 79/**
a5ed8940 80 * drm_gem_cma_create - allocate an object with the given size
d7883f87
TR
81 * @drm: DRM device
82 * @size: size of the object to allocate
83 *
84 * This function creates a CMA GEM object and allocates a contiguous chunk of
85 * memory as backing store. The backing memory has the writecombine attribute
86 * set.
a5ed8940 87 *
d7883f87
TR
88 * Returns:
89 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
90 * error code on failure.
a5ed8940
LP
91 */
92struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
d7883f87 93 size_t size)
a5ed8940
LP
94{
95 struct drm_gem_cma_object *cma_obj;
71d7282a 96 int ret;
b9d47450 97
a5ed8940 98 size = round_up(size, PAGE_SIZE);
b9d47450 99
a5ed8940
LP
100 cma_obj = __drm_gem_cma_create(drm, size);
101 if (IS_ERR(cma_obj))
102 return cma_obj;
103
f6e45661
LR
104 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
105 GFP_KERNEL | __GFP_NOWARN);
a5ed8940 106 if (!cma_obj->vaddr) {
e0ad7c0f 107 dev_dbg(drm->dev, "failed to allocate buffer with size %zu\n",
a5ed8940 108 size);
71d7282a
LP
109 ret = -ENOMEM;
110 goto error;
111 }
112
a5ed8940 113 return cma_obj;
71d7282a
LP
114
115error:
e6b62714 116 drm_gem_object_put_unlocked(&cma_obj->base);
71d7282a 117 return ERR_PTR(ret);
b9d47450
SH
118}
119EXPORT_SYMBOL_GPL(drm_gem_cma_create);
120
d7883f87
TR
121/**
122 * drm_gem_cma_create_with_handle - allocate an object with the given size and
123 * return a GEM handle to it
124 * @file_priv: DRM file-private structure to register the handle for
125 * @drm: DRM device
126 * @size: size of the object to allocate
127 * @handle: return location for the GEM handle
128 *
129 * This function creates a CMA GEM object, allocating a physically contiguous
130 * chunk of memory as backing store. The GEM object is then added to the list
131 * of object associated with the given file and a handle to it is returned.
b9d47450 132 *
d7883f87
TR
133 * Returns:
134 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
135 * error code on failure.
b9d47450 136 */
d7883f87
TR
137static struct drm_gem_cma_object *
138drm_gem_cma_create_with_handle(struct drm_file *file_priv,
139 struct drm_device *drm, size_t size,
140 uint32_t *handle)
b9d47450
SH
141{
142 struct drm_gem_cma_object *cma_obj;
143 struct drm_gem_object *gem_obj;
144 int ret;
145
146 cma_obj = drm_gem_cma_create(drm, size);
147 if (IS_ERR(cma_obj))
148 return cma_obj;
149
150 gem_obj = &cma_obj->base;
151
152 /*
153 * allocate a id of idr table where the obj is registered
154 * and handle has the id what user can see.
155 */
156 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
b9d47450 157 /* drop reference from allocate - handle holds it now. */
e6b62714 158 drm_gem_object_put_unlocked(gem_obj);
afe705be
CW
159 if (ret)
160 return ERR_PTR(ret);
b9d47450
SH
161
162 return cma_obj;
b9d47450
SH
163}
164
d7883f87
TR
165/**
166 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
167 * @gem_obj: GEM object to free
168 *
169 * This function frees the backing memory of the CMA GEM object, cleans up the
170 * GEM object state and frees the memory used to store the object itself.
b9068cde 171 * If the buffer is imported and the virtual address is set, it is released.
421242aa 172 * Drivers using the CMA helpers should set this as their
47f39800 173 * &drm_driver.gem_free_object_unlocked callback.
b9d47450
SH
174 */
175void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
176{
177 struct drm_gem_cma_object *cma_obj;
178
b9d47450
SH
179 cma_obj = to_drm_gem_cma_obj(gem_obj);
180
23e35c0e 181 if (gem_obj->import_attach) {
b9068cde
NT
182 if (cma_obj->vaddr)
183 dma_buf_vunmap(gem_obj->import_attach->dmabuf, cma_obj->vaddr);
71d7282a 184 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
23e35c0e
NT
185 } else if (cma_obj->vaddr) {
186 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
187 cma_obj->vaddr, cma_obj->paddr);
71d7282a 188 }
a5ed8940
LP
189
190 drm_gem_object_release(gem_obj);
b9d47450
SH
191
192 kfree(cma_obj);
193}
194EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
195
6d178291
TR
196/**
197 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
198 * @file_priv: DRM file-private structure to create the dumb buffer for
199 * @drm: DRM device
200 * @args: IOCTL data
201 *
202 * This aligns the pitch and size arguments to the minimum required. This is
203 * an internal helper that can be wrapped by a driver to account for hardware
204 * with more specific alignment requirements. It should not be used directly
421242aa 205 * as their &drm_driver.dumb_create callback.
6d178291
TR
206 *
207 * Returns:
208 * 0 on success or a negative error code on failure.
209 */
210int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
211 struct drm_device *drm,
212 struct drm_mode_create_dumb *args)
213{
214 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
215 struct drm_gem_cma_object *cma_obj;
216
217 if (args->pitch < min_pitch)
218 args->pitch = min_pitch;
219
220 if (args->size < args->pitch * args->height)
221 args->size = args->pitch * args->height;
222
223 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
224 &args->handle);
225 return PTR_ERR_OR_ZERO(cma_obj);
226}
227EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
228
d7883f87
TR
229/**
230 * drm_gem_cma_dumb_create - create a dumb buffer object
231 * @file_priv: DRM file-private structure to create the dumb buffer for
232 * @drm: DRM device
233 * @args: IOCTL data
234 *
235 * This function computes the pitch of the dumb buffer and rounds it up to an
236 * integer number of bytes per pixel. Drivers for hardware that doesn't have
237 * any additional restrictions on the pitch can directly use this function as
421242aa 238 * their &drm_driver.dumb_create callback.
b9d47450 239 *
6d178291
TR
240 * For hardware with additional restrictions, drivers can adjust the fields
241 * set up by userspace and pass the IOCTL data along to the
242 * drm_gem_cma_dumb_create_internal() function.
243 *
d7883f87
TR
244 * Returns:
245 * 0 on success or a negative error code on failure.
b9d47450
SH
246 */
247int drm_gem_cma_dumb_create(struct drm_file *file_priv,
d7883f87
TR
248 struct drm_device *drm,
249 struct drm_mode_create_dumb *args)
b9d47450
SH
250{
251 struct drm_gem_cma_object *cma_obj;
b9d47450 252
6d178291
TR
253 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
254 args->size = args->pitch * args->height;
b9d47450 255
d7883f87
TR
256 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
257 &args->handle);
b03cda51 258 return PTR_ERR_OR_ZERO(cma_obj);
b9d47450
SH
259}
260EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
261
b9d47450
SH
262const struct vm_operations_struct drm_gem_cma_vm_ops = {
263 .open = drm_gem_vm_open,
264 .close = drm_gem_vm_close,
265};
266EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
267
ebaf9e03
LP
268static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
269 struct vm_area_struct *vma)
270{
271 int ret;
272
b65e64f7
LP
273 /*
274 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
275 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
276 * the whole buffer.
277 */
278 vma->vm_flags &= ~VM_PFNMAP;
279 vma->vm_pgoff = 0;
280
f6e45661
LR
281 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
282 cma_obj->paddr, vma->vm_end - vma->vm_start);
ebaf9e03
LP
283 if (ret)
284 drm_gem_vm_close(vma);
285
286 return ret;
287}
288
d7883f87
TR
289/**
290 * drm_gem_cma_mmap - memory-map a CMA GEM object
291 * @filp: file object
292 * @vma: VMA for the area to be mapped
293 *
294 * This function implements an augmented version of the GEM DRM file mmap
295 * operation for CMA objects: In addition to the usual GEM VMA setup it
296 * immediately faults in the entire object instead of using on-demaind
297 * faulting. Drivers which employ the CMA helpers should use this function
298 * as their ->mmap() handler in the DRM device file's file_operations
299 * structure.
300 *
d55f7e5d
DV
301 * Instead of directly referencing this function, drivers should use the
302 * DEFINE_DRM_GEM_CMA_FOPS().macro.
303 *
d7883f87
TR
304 * Returns:
305 * 0 on success or a negative error code on failure.
b9d47450
SH
306 */
307int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
308{
b9d47450 309 struct drm_gem_cma_object *cma_obj;
ebaf9e03 310 struct drm_gem_object *gem_obj;
b9d47450
SH
311 int ret;
312
313 ret = drm_gem_mmap(filp, vma);
314 if (ret)
315 return ret;
316
317 gem_obj = vma->vm_private_data;
318 cma_obj = to_drm_gem_cma_obj(gem_obj);
319
ebaf9e03 320 return drm_gem_cma_mmap_obj(cma_obj, vma);
b9d47450
SH
321}
322EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
323
62a0d98a
BG
324#ifndef CONFIG_MMU
325/**
326 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
327 * @filp: file object
328 * @addr: memory address
329 * @len: buffer size
330 * @pgoff: page offset
331 * @flags: memory flags
332 *
333 * This function is used in noMMU platforms to propose address mapping
334 * for a given buffer.
335 * It's intended to be used as a direct handler for the struct
336 * &file_operations.get_unmapped_area operation.
337 *
338 * Returns:
339 * mapping address on success or a negative error code on failure.
340 */
341unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
342 unsigned long addr,
343 unsigned long len,
344 unsigned long pgoff,
345 unsigned long flags)
346{
347 struct drm_gem_cma_object *cma_obj;
348 struct drm_gem_object *obj = NULL;
349 struct drm_file *priv = filp->private_data;
350 struct drm_device *dev = priv->minor->dev;
351 struct drm_vma_offset_node *node;
352
c07dcd61 353 if (drm_dev_is_unplugged(dev))
62a0d98a
BG
354 return -ENODEV;
355
356 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
357 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
358 pgoff,
359 len >> PAGE_SHIFT);
360 if (likely(node)) {
361 obj = container_of(node, struct drm_gem_object, vma_node);
362 /*
363 * When the object is being freed, after it hits 0-refcnt it
364 * proceeds to tear down the object. In the process it will
365 * attempt to remove the VMA offset and so acquire this
366 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
367 * that matches our range, we know it is in the process of being
368 * destroyed and will be freed as soon as we release the lock -
369 * so we have to check for the 0-refcnted object and treat it as
370 * invalid.
371 */
372 if (!kref_get_unless_zero(&obj->refcount))
373 obj = NULL;
374 }
375
376 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
377
378 if (!obj)
379 return -EINVAL;
380
381 if (!drm_vma_node_is_allowed(node, priv)) {
e6b62714 382 drm_gem_object_put_unlocked(obj);
62a0d98a
BG
383 return -EACCES;
384 }
385
386 cma_obj = to_drm_gem_cma_obj(obj);
387
e6b62714 388 drm_gem_object_put_unlocked(obj);
62a0d98a
BG
389
390 return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
391}
392EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
393#endif
394
d6892012
NT
395/**
396 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
397 * @p: DRM printer
398 * @indent: Tab indentation level
1fbdb980 399 * @obj: GEM object
d6892012
NT
400 *
401 * This function can be used as the &drm_driver->gem_print_info callback.
402 * It prints paddr and vaddr for use in e.g. debugfs output.
403 */
404void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
405 const struct drm_gem_object *obj)
406{
407 const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
408
409 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
410 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
411}
412EXPORT_SYMBOL(drm_gem_cma_print_info);
413
d7883f87
TR
414/**
415 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
416 * pages for a CMA GEM object
417 * @obj: GEM object
418 *
419 * This function exports a scatter/gather table suitable for PRIME usage by
420 * calling the standard DMA mapping API. Drivers using the CMA helpers should
421242aa 421 * set this as their &drm_driver.gem_prime_get_sg_table callback.
d7883f87
TR
422 *
423 * Returns:
424 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
425 */
78467dc5
JS
426struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
427{
428 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
429 struct sg_table *sgt;
430 int ret;
431
432 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
433 if (!sgt)
a31ac0b2 434 return ERR_PTR(-ENOMEM);
78467dc5
JS
435
436 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
437 cma_obj->paddr, obj->size);
438 if (ret < 0)
439 goto out;
440
441 return sgt;
442
443out:
444 kfree(sgt);
a31ac0b2 445 return ERR_PTR(ret);
78467dc5
JS
446}
447EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
448
d7883f87
TR
449/**
450 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
451 * driver's scatter/gather table of pinned pages
452 * @dev: device to import into
453 * @attach: DMA-BUF attachment
454 * @sgt: scatter/gather table of pinned pages
455 *
456 * This function imports a scatter/gather table exported via DMA-BUF by
457 * another driver. Imported buffers must be physically contiguous in memory
458 * (i.e. the scatter/gather table must contain a single entry). Drivers that
421242aa
DV
459 * use the CMA helpers should set this as their
460 * &drm_driver.gem_prime_import_sg_table callback.
d7883f87
TR
461 *
462 * Returns:
463 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
464 * error code on failure.
465 */
78467dc5 466struct drm_gem_object *
b5e9c1a2
ML
467drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
468 struct dma_buf_attachment *attach,
78467dc5
JS
469 struct sg_table *sgt)
470{
471 struct drm_gem_cma_object *cma_obj;
472
998fb1a0
LD
473 if (sgt->nents != 1) {
474 /* check if the entries in the sg_table are contiguous */
475 dma_addr_t next_addr = sg_dma_address(sgt->sgl);
476 struct scatterlist *s;
477 unsigned int i;
478
479 for_each_sg(sgt->sgl, s, sgt->nents, i) {
480 /*
481 * sg_dma_address(s) is only valid for entries
482 * that have sg_dma_len(s) != 0
483 */
484 if (!sg_dma_len(s))
485 continue;
486
487 if (sg_dma_address(s) != next_addr)
488 return ERR_PTR(-EINVAL);
489
490 next_addr = sg_dma_address(s) + sg_dma_len(s);
491 }
492 }
78467dc5
JS
493
494 /* Create a CMA GEM buffer. */
b5e9c1a2 495 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
78467dc5 496 if (IS_ERR(cma_obj))
e7c36347 497 return ERR_CAST(cma_obj);
78467dc5
JS
498
499 cma_obj->paddr = sg_dma_address(sgt->sgl);
500 cma_obj->sgt = sgt;
501
b5e9c1a2 502 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
78467dc5
JS
503
504 return &cma_obj->base;
505}
506EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
507
d7883f87
TR
508/**
509 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
510 * @obj: GEM object
511 * @vma: VMA for the area to be mapped
512 *
513 * This function maps a buffer imported via DRM PRIME into a userspace
514 * process's address space. Drivers that use the CMA helpers should set this
421242aa 515 * as their &drm_driver.gem_prime_mmap callback.
d7883f87
TR
516 *
517 * Returns:
518 * 0 on success or a negative error code on failure.
519 */
78467dc5
JS
520int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
521 struct vm_area_struct *vma)
522{
523 struct drm_gem_cma_object *cma_obj;
78467dc5
JS
524 int ret;
525
78467dc5 526 ret = drm_gem_mmap_obj(obj, obj->size, vma);
78467dc5
JS
527 if (ret < 0)
528 return ret;
529
530 cma_obj = to_drm_gem_cma_obj(obj);
531 return drm_gem_cma_mmap_obj(cma_obj, vma);
532}
533EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
534
d7883f87
TR
535/**
536 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
537 * address space
538 * @obj: GEM object
539 *
540 * This function maps a buffer exported via DRM PRIME into the kernel's
541 * virtual address space. Since the CMA buffers are already mapped into the
542 * kernel virtual address space this simply returns the cached virtual
543 * address. Drivers using the CMA helpers should set this as their DRM
421242aa 544 * driver's &drm_driver.gem_prime_vmap callback.
d7883f87
TR
545 *
546 * Returns:
547 * The kernel virtual address of the CMA GEM object's backing store.
548 */
78467dc5
JS
549void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
550{
551 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
552
553 return cma_obj->vaddr;
554}
555EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
556
d7883f87
TR
557/**
558 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
559 * address space
560 * @obj: GEM object
561 * @vaddr: kernel virtual address where the CMA GEM object was mapped
562 *
563 * This function removes a buffer exported via DRM PRIME from the kernel's
564 * virtual address space. This is a no-op because CMA buffers cannot be
565 * unmapped from kernel space. Drivers using the CMA helpers should set this
421242aa 566 * as their &drm_driver.gem_prime_vunmap callback.
d7883f87 567 */
78467dc5
JS
568void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
569{
570 /* Nothing to do */
571}
572EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);
b9068cde
NT
573
574static const struct drm_gem_object_funcs drm_cma_gem_default_funcs = {
575 .free = drm_gem_cma_free_object,
576 .print_info = drm_gem_cma_print_info,
577 .get_sg_table = drm_gem_cma_prime_get_sg_table,
578 .vmap = drm_gem_cma_prime_vmap,
579 .vm_ops = &drm_gem_cma_vm_ops,
580};
581
582/**
583 * drm_cma_gem_create_object_default_funcs - Create a CMA GEM object with a
584 * default function table
585 * @dev: DRM device
586 * @size: Size of the object to allocate
587 *
588 * This sets the GEM object functions to the default CMA helper functions.
589 * This function can be used as the &drm_driver.gem_create_object callback.
590 *
591 * Returns:
592 * A pointer to a allocated GEM object or an error pointer on failure.
593 */
594struct drm_gem_object *
595drm_cma_gem_create_object_default_funcs(struct drm_device *dev, size_t size)
596{
597 struct drm_gem_cma_object *cma_obj;
598
599 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
600 if (!cma_obj)
601 return NULL;
602
603 cma_obj->base.funcs = &drm_cma_gem_default_funcs;
604
605 return &cma_obj->base;
606}
607EXPORT_SYMBOL(drm_cma_gem_create_object_default_funcs);
608
609/**
610 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
611 * scatter/gather table and get the virtual address of the buffer
612 * @dev: DRM device
613 * @attach: DMA-BUF attachment
614 * @sgt: Scatter/gather table of pinned pages
615 *
616 * This function imports a scatter/gather table using
617 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
618 * virtual address. This ensures that a CMA GEM object always has its virtual
619 * address set. This address is released when the object is freed.
620 *
621 * This function can be used as the &drm_driver.gem_prime_import_sg_table
622 * callback. The DRM_GEM_CMA_VMAP_DRIVER_OPS() macro provides a shortcut to set
623 * the necessary DRM driver operations.
624 *
625 * Returns:
626 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
627 * error code on failure.
628 */
629struct drm_gem_object *
630drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
631 struct dma_buf_attachment *attach,
632 struct sg_table *sgt)
633{
634 struct drm_gem_cma_object *cma_obj;
635 struct drm_gem_object *obj;
636 void *vaddr;
637
638 vaddr = dma_buf_vmap(attach->dmabuf);
639 if (!vaddr) {
640 DRM_ERROR("Failed to vmap PRIME buffer\n");
641 return ERR_PTR(-ENOMEM);
642 }
643
644 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
645 if (IS_ERR(obj)) {
646 dma_buf_vunmap(attach->dmabuf, vaddr);
647 return obj;
648 }
649
650 cma_obj = to_drm_gem_cma_obj(obj);
651 cma_obj->vaddr = vaddr;
652
653 return obj;
654}
655EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);