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