1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
10 */
11
12 #include <linux/dma-buf.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/export.h>
15 #include <linux/mm.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18
19 #include <drm/drm.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>
24
25 /**
26 * DOC: cma helpers
27 *
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.
30 *
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.
34 */
35
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,
43 };
44
45 /**
46 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
47 * @drm: DRM device
48 * @size: size of the object to allocate
49 * @private: true if used for internal purposes
50 *
51 * This function creates and initializes a GEM CMA object of the given size,
52 * but doesn't allocate any memory to back the object.
53 *
54 * Returns:
55 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
56 * error code on failure.
57 */
58 static struct drm_gem_cma_object *
__drm_gem_cma_create(struct drm_device * drm,size_t size,bool private)59 __drm_gem_cma_create(struct drm_device *drm, size_t size, bool private)
60 {
61 struct drm_gem_cma_object *cma_obj;
62 struct drm_gem_object *gem_obj;
63 int ret = 0;
64
65 if (drm->driver->gem_create_object)
66 gem_obj = drm->driver->gem_create_object(drm, size);
67 else
68 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
69 if (!gem_obj)
70 return ERR_PTR(-ENOMEM);
71
72 if (!gem_obj->funcs)
73 gem_obj->funcs = &drm_gem_cma_default_funcs;
74
75 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
76
77 if (private) {
78 drm_gem_private_object_init(drm, gem_obj, size);
79
80 /* Always use writecombine for dma-buf mappings */
81 cma_obj->map_noncoherent = false;
82 } else {
83 ret = drm_gem_object_init(drm, gem_obj, size);
84 }
85 if (ret)
86 goto error;
87
88 ret = drm_gem_create_mmap_offset(gem_obj);
89 if (ret) {
90 drm_gem_object_release(gem_obj);
91 goto error;
92 }
93
94 return cma_obj;
95
96 error:
97 kfree(cma_obj);
98 return ERR_PTR(ret);
99 }
100
101 /**
102 * drm_gem_cma_create - allocate an object with the given size
103 * @drm: DRM device
104 * @size: size of the object to allocate
105 *
106 * This function creates a CMA GEM object and allocates a contiguous chunk of
107 * memory as backing store.
108 *
109 * Returns:
110 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
111 * error code on failure.
112 */
drm_gem_cma_create(struct drm_device * drm,size_t size)113 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
114 size_t size)
115 {
116 struct drm_gem_cma_object *cma_obj;
117 int ret;
118
119 size = round_up(size, PAGE_SIZE);
120
121 cma_obj = __drm_gem_cma_create(drm, size, false);
122 if (IS_ERR(cma_obj))
123 return cma_obj;
124
125 if (cma_obj->map_noncoherent) {
126 cma_obj->vaddr = dma_alloc_noncoherent(drm->dev, size,
127 &cma_obj->paddr,
128 DMA_TO_DEVICE,
129 GFP_KERNEL | __GFP_NOWARN);
130 } else {
131 cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
132 GFP_KERNEL | __GFP_NOWARN);
133 }
134 if (!cma_obj->vaddr) {
135 drm_dbg(drm, "failed to allocate buffer with size %zu\n",
136 size);
137 ret = -ENOMEM;
138 goto error;
139 }
140
141 return cma_obj;
142
143 error:
144 drm_gem_object_put(&cma_obj->base);
145 return ERR_PTR(ret);
146 }
147 EXPORT_SYMBOL_GPL(drm_gem_cma_create);
148
149 /**
150 * drm_gem_cma_create_with_handle - allocate an object with the given size and
151 * return a GEM handle to it
152 * @file_priv: DRM file-private structure to register the handle for
153 * @drm: DRM device
154 * @size: size of the object to allocate
155 * @handle: return location for the GEM handle
156 *
157 * This function creates a CMA GEM object, allocating a physically contiguous
158 * chunk of memory as backing store. The GEM object is then added to the list
159 * of object associated with the given file and a handle to it is returned.
160 *
161 * Returns:
162 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
163 * error code on failure.
164 */
165 static struct drm_gem_cma_object *
drm_gem_cma_create_with_handle(struct drm_file * file_priv,struct drm_device * drm,size_t size,uint32_t * handle)166 drm_gem_cma_create_with_handle(struct drm_file *file_priv,
167 struct drm_device *drm, size_t size,
168 uint32_t *handle)
169 {
170 struct drm_gem_cma_object *cma_obj;
171 struct drm_gem_object *gem_obj;
172 int ret;
173
174 cma_obj = drm_gem_cma_create(drm, size);
175 if (IS_ERR(cma_obj))
176 return cma_obj;
177
178 gem_obj = &cma_obj->base;
179
180 /*
181 * allocate a id of idr table where the obj is registered
182 * and handle has the id what user can see.
183 */
184 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
185 /* drop reference from allocate - handle holds it now. */
186 drm_gem_object_put(gem_obj);
187 if (ret)
188 return ERR_PTR(ret);
189
190 return cma_obj;
191 }
192
193 /**
194 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
195 * @gem_obj: GEM object to free
196 *
197 * This function frees the backing memory of the CMA GEM object, cleans up the
198 * GEM object state and frees the memory used to store the object itself.
199 * If the buffer is imported and the virtual address is set, it is released.
200 * Drivers using the CMA helpers should set this as their
201 * &drm_gem_object_funcs.free callback.
202 */
drm_gem_cma_free_object(struct drm_gem_object * gem_obj)203 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
204 {
205 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(gem_obj);
206 struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(cma_obj->vaddr);
207
208 if (gem_obj->import_attach) {
209 if (cma_obj->vaddr)
210 dma_buf_vunmap(gem_obj->import_attach->dmabuf, &map);
211 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
212 } else if (cma_obj->vaddr) {
213 if (cma_obj->map_noncoherent)
214 dma_free_noncoherent(gem_obj->dev->dev, cma_obj->base.size,
215 cma_obj->vaddr, cma_obj->paddr,
216 DMA_TO_DEVICE);
217 else
218 dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
219 cma_obj->vaddr, cma_obj->paddr);
220 }
221
222 drm_gem_object_release(gem_obj);
223
224 kfree(cma_obj);
225 }
226 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
227
228 /**
229 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
230 * @file_priv: DRM file-private structure to create the dumb buffer for
231 * @drm: DRM device
232 * @args: IOCTL data
233 *
234 * This aligns the pitch and size arguments to the minimum required. This is
235 * an internal helper that can be wrapped by a driver to account for hardware
236 * with more specific alignment requirements. It should not be used directly
237 * as their &drm_driver.dumb_create callback.
238 *
239 * Returns:
240 * 0 on success or a negative error code on failure.
241 */
drm_gem_cma_dumb_create_internal(struct drm_file * file_priv,struct drm_device * drm,struct drm_mode_create_dumb * args)242 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
243 struct drm_device *drm,
244 struct drm_mode_create_dumb *args)
245 {
246 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
247 struct drm_gem_cma_object *cma_obj;
248
249 if (args->pitch < min_pitch)
250 args->pitch = min_pitch;
251
252 if (args->size < args->pitch * args->height)
253 args->size = args->pitch * args->height;
254
255 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
256 &args->handle);
257 return PTR_ERR_OR_ZERO(cma_obj);
258 }
259 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
260
261 /**
262 * drm_gem_cma_dumb_create - create a dumb buffer object
263 * @file_priv: DRM file-private structure to create the dumb buffer for
264 * @drm: DRM device
265 * @args: IOCTL data
266 *
267 * This function computes the pitch of the dumb buffer and rounds it up to an
268 * integer number of bytes per pixel. Drivers for hardware that doesn't have
269 * any additional restrictions on the pitch can directly use this function as
270 * their &drm_driver.dumb_create callback.
271 *
272 * For hardware with additional restrictions, drivers can adjust the fields
273 * set up by userspace and pass the IOCTL data along to the
274 * drm_gem_cma_dumb_create_internal() function.
275 *
276 * Returns:
277 * 0 on success or a negative error code on failure.
278 */
drm_gem_cma_dumb_create(struct drm_file * file_priv,struct drm_device * drm,struct drm_mode_create_dumb * args)279 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
280 struct drm_device *drm,
281 struct drm_mode_create_dumb *args)
282 {
283 struct drm_gem_cma_object *cma_obj;
284
285 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
286 args->size = args->pitch * args->height;
287
288 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
289 &args->handle);
290 return PTR_ERR_OR_ZERO(cma_obj);
291 }
292 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
293
294 const struct vm_operations_struct drm_gem_cma_vm_ops = {
295 .open = drm_gem_vm_open,
296 .close = drm_gem_vm_close,
297 };
298 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
299
300 #ifndef CONFIG_MMU
301 /**
302 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
303 * @filp: file object
304 * @addr: memory address
305 * @len: buffer size
306 * @pgoff: page offset
307 * @flags: memory flags
308 *
309 * This function is used in noMMU platforms to propose address mapping
310 * for a given buffer.
311 * It's intended to be used as a direct handler for the struct
312 * &file_operations.get_unmapped_area operation.
313 *
314 * Returns:
315 * mapping address on success or a negative error code on failure.
316 */
drm_gem_cma_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)317 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
318 unsigned long addr,
319 unsigned long len,
320 unsigned long pgoff,
321 unsigned long flags)
322 {
323 struct drm_gem_cma_object *cma_obj;
324 struct drm_gem_object *obj = NULL;
325 struct drm_file *priv = filp->private_data;
326 struct drm_device *dev = priv->minor->dev;
327 struct drm_vma_offset_node *node;
328
329 if (drm_dev_is_unplugged(dev))
330 return -ENODEV;
331
332 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
333 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
334 pgoff,
335 len >> PAGE_SHIFT);
336 if (likely(node)) {
337 obj = container_of(node, struct drm_gem_object, vma_node);
338 /*
339 * When the object is being freed, after it hits 0-refcnt it
340 * proceeds to tear down the object. In the process it will
341 * attempt to remove the VMA offset and so acquire this
342 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
343 * that matches our range, we know it is in the process of being
344 * destroyed and will be freed as soon as we release the lock -
345 * so we have to check for the 0-refcnted object and treat it as
346 * invalid.
347 */
348 if (!kref_get_unless_zero(&obj->refcount))
349 obj = NULL;
350 }
351
352 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
353
354 if (!obj)
355 return -EINVAL;
356
357 if (!drm_vma_node_is_allowed(node, priv)) {
358 drm_gem_object_put(obj);
359 return -EACCES;
360 }
361
362 cma_obj = to_drm_gem_cma_obj(obj);
363
364 drm_gem_object_put(obj);
365
366 return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
367 }
368 EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
369 #endif
370
371 /**
372 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
373 * @p: DRM printer
374 * @indent: Tab indentation level
375 * @obj: GEM object
376 *
377 * This function can be used as the &drm_driver->gem_print_info callback.
378 * It prints paddr and vaddr for use in e.g. debugfs output.
379 */
drm_gem_cma_print_info(struct drm_printer * p,unsigned int indent,const struct drm_gem_object * obj)380 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
381 const struct drm_gem_object *obj)
382 {
383 const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
384
385 drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
386 drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
387 }
388 EXPORT_SYMBOL(drm_gem_cma_print_info);
389
390 /**
391 * drm_gem_cma_get_sg_table - provide a scatter/gather table of pinned
392 * pages for a CMA GEM object
393 * @obj: GEM object
394 *
395 * This function exports a scatter/gather table by
396 * calling the standard DMA mapping API. Drivers using the CMA helpers should
397 * set this as their &drm_gem_object_funcs.get_sg_table callback.
398 *
399 * Returns:
400 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
401 */
drm_gem_cma_get_sg_table(struct drm_gem_object * obj)402 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj)
403 {
404 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
405 struct sg_table *sgt;
406 int ret;
407
408 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
409 if (!sgt)
410 return ERR_PTR(-ENOMEM);
411
412 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
413 cma_obj->paddr, obj->size);
414 if (ret < 0)
415 goto out;
416
417 return sgt;
418
419 out:
420 kfree(sgt);
421 return ERR_PTR(ret);
422 }
423 EXPORT_SYMBOL_GPL(drm_gem_cma_get_sg_table);
424
425 /**
426 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
427 * driver's scatter/gather table of pinned pages
428 * @dev: device to import into
429 * @attach: DMA-BUF attachment
430 * @sgt: scatter/gather table of pinned pages
431 *
432 * This function imports a scatter/gather table exported via DMA-BUF by
433 * another driver. Imported buffers must be physically contiguous in memory
434 * (i.e. the scatter/gather table must contain a single entry). Drivers that
435 * use the CMA helpers should set this as their
436 * &drm_driver.gem_prime_import_sg_table callback.
437 *
438 * Returns:
439 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
440 * error code on failure.
441 */
442 struct drm_gem_object *
drm_gem_cma_prime_import_sg_table(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * sgt)443 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
444 struct dma_buf_attachment *attach,
445 struct sg_table *sgt)
446 {
447 struct drm_gem_cma_object *cma_obj;
448
449 /* check if the entries in the sg_table are contiguous */
450 if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size)
451 return ERR_PTR(-EINVAL);
452
453 /* Create a CMA GEM buffer. */
454 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size, true);
455 if (IS_ERR(cma_obj))
456 return ERR_CAST(cma_obj);
457
458 cma_obj->paddr = sg_dma_address(sgt->sgl);
459 cma_obj->sgt = sgt;
460
461 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
462
463 return &cma_obj->base;
464 }
465 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
466
467 /**
468 * drm_gem_cma_vmap - map a CMA GEM object into the kernel's virtual
469 * address space
470 * @obj: GEM object
471 * @map: Returns the kernel virtual address of the CMA GEM object's backing
472 * store.
473 *
474 * This function maps a buffer into the kernel's
475 * virtual address space. Since the CMA buffers are already mapped into the
476 * kernel virtual address space this simply returns the cached virtual
477 * address. Drivers using the CMA helpers should set this as their DRM
478 * driver's &drm_gem_object_funcs.vmap callback.
479 *
480 * Returns:
481 * 0 on success, or a negative error code otherwise.
482 */
drm_gem_cma_vmap(struct drm_gem_object * obj,struct dma_buf_map * map)483 int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
484 {
485 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
486
487 dma_buf_map_set_vaddr(map, cma_obj->vaddr);
488
489 return 0;
490 }
491 EXPORT_SYMBOL_GPL(drm_gem_cma_vmap);
492
493 /**
494 * drm_gem_cma_mmap - memory-map an exported CMA GEM object
495 * @obj: GEM object
496 * @vma: VMA for the area to be mapped
497 *
498 * This function maps a buffer into a userspace process's address space.
499 * In addition to the usual GEM VMA setup it immediately faults in the entire
500 * object instead of using on-demand faulting. Drivers that use the CMA
501 * helpers should set this as their &drm_gem_object_funcs.mmap callback.
502 *
503 * Returns:
504 * 0 on success or a negative error code on failure.
505 */
drm_gem_cma_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)506 int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
507 {
508 struct drm_gem_cma_object *cma_obj;
509 int ret;
510
511 /*
512 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
513 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
514 * the whole buffer.
515 */
516 vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
517 vma->vm_flags &= ~VM_PFNMAP;
518
519 cma_obj = to_drm_gem_cma_obj(obj);
520
521 if (cma_obj->map_noncoherent) {
522 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
523
524 ret = dma_mmap_pages(cma_obj->base.dev->dev,
525 vma, vma->vm_end - vma->vm_start,
526 virt_to_page(cma_obj->vaddr));
527 } else {
528 ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
529 cma_obj->paddr, vma->vm_end - vma->vm_start);
530 }
531 if (ret)
532 drm_gem_vm_close(vma);
533
534 return ret;
535 }
536 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
537
538 /**
539 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
540 * scatter/gather table and get the virtual address of the buffer
541 * @dev: DRM device
542 * @attach: DMA-BUF attachment
543 * @sgt: Scatter/gather table of pinned pages
544 *
545 * This function imports a scatter/gather table using
546 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
547 * virtual address. This ensures that a CMA GEM object always has its virtual
548 * address set. This address is released when the object is freed.
549 *
550 * This function can be used as the &drm_driver.gem_prime_import_sg_table
551 * callback. The &DRM_GEM_CMA_DRIVER_OPS_VMAP macro provides a shortcut to set
552 * the necessary DRM driver operations.
553 *
554 * Returns:
555 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
556 * error code on failure.
557 */
558 struct drm_gem_object *
drm_gem_cma_prime_import_sg_table_vmap(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * sgt)559 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
560 struct dma_buf_attachment *attach,
561 struct sg_table *sgt)
562 {
563 struct drm_gem_cma_object *cma_obj;
564 struct drm_gem_object *obj;
565 struct dma_buf_map map;
566 int ret;
567
568 ret = dma_buf_vmap(attach->dmabuf, &map);
569 if (ret) {
570 DRM_ERROR("Failed to vmap PRIME buffer\n");
571 return ERR_PTR(ret);
572 }
573
574 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
575 if (IS_ERR(obj)) {
576 dma_buf_vunmap(attach->dmabuf, &map);
577 return obj;
578 }
579
580 cma_obj = to_drm_gem_cma_obj(obj);
581 cma_obj->vaddr = map.vaddr;
582
583 return obj;
584 }
585 EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);
586