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