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