03d5d0c85bafdfea4c5406643deaf1719a5c9bb1
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-5.4 / 950-0732-dma-buf-Add-dma-buf-heaps-framework.patch
1 From 4c5a9a5db543b5fd998fbc3e15fd4a2d2a3971a9 Mon Sep 17 00:00:00 2001
2 From: "Andrew F. Davis" <afd@ti.com>
3 Date: Tue, 3 Dec 2019 17:26:37 +0000
4 Subject: [PATCH] dma-buf: Add dma-buf heaps framework
5
6 Commit c02a81fba74fe3488ad6b08bfb5a1329005418f8 upstream.
7 This framework allows a unified userspace interface for dma-buf
8 exporters, allowing userland to allocate specific types of memory
9 for use in dma-buf sharing.
10
11 Each heap is given its own device node, which a user can allocate
12 a dma-buf fd from using the DMA_HEAP_IOC_ALLOC.
13
14 This code is an evoluiton of the Android ION implementation,
15 and a big thanks is due to its authors/maintainers over time
16 for their effort:
17 Rebecca Schultz Zavin, Colin Cross, Benjamin Gaignard,
18 Laura Abbott, and many other contributors!
19
20 Cc: Laura Abbott <labbott@redhat.com>
21 Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
22 Cc: Sumit Semwal <sumit.semwal@linaro.org>
23 Cc: Liam Mark <lmark@codeaurora.org>
24 Cc: Pratik Patel <pratikp@codeaurora.org>
25 Cc: Brian Starkey <Brian.Starkey@arm.com>
26 Cc: Vincent Donnefort <Vincent.Donnefort@arm.com>
27 Cc: Sudipto Paul <Sudipto.Paul@arm.com>
28 Cc: Andrew F. Davis <afd@ti.com>
29 Cc: Christoph Hellwig <hch@infradead.org>
30 Cc: Chenbo Feng <fengc@google.com>
31 Cc: Alistair Strachan <astrachan@google.com>
32 Cc: Hridya Valsaraju <hridya@google.com>
33 Cc: Sandeep Patil <sspatil@google.com>
34 Cc: Hillf Danton <hdanton@sina.com>
35 Cc: Dave Airlie <airlied@gmail.com>
36 Cc: dri-devel@lists.freedesktop.org
37 Reviewed-by: Brian Starkey <brian.starkey@arm.com>
38 Acked-by: Sandeep Patil <sspatil@android.com>
39 Signed-off-by: Andrew F. Davis <afd@ti.com>
40 Signed-off-by: John Stultz <john.stultz@linaro.org>
41 Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
42 Link: https://patchwork.freedesktop.org/patch/msgid/20191203172641.66642-2-john.stultz@linaro.org
43 ---
44 MAINTAINERS | 18 +++
45 drivers/dma-buf/Kconfig | 9 ++
46 drivers/dma-buf/Makefile | 1 +
47 drivers/dma-buf/dma-heap.c | 297 ++++++++++++++++++++++++++++++++++
48 include/linux/dma-heap.h | 59 +++++++
49 include/uapi/linux/dma-heap.h | 53 ++++++
50 6 files changed, 437 insertions(+)
51 create mode 100644 drivers/dma-buf/dma-heap.c
52 create mode 100644 include/linux/dma-heap.h
53 create mode 100644 include/uapi/linux/dma-heap.h
54
55 --- a/MAINTAINERS
56 +++ b/MAINTAINERS
57 @@ -4962,6 +4962,24 @@ F: include/linux/*fence.h
58 F: Documentation/driver-api/dma-buf.rst
59 T: git git://anongit.freedesktop.org/drm/drm-misc
60
61 +DMA-BUF HEAPS FRAMEWORK
62 +M: Sumit Semwal <sumit.semwal@linaro.org>
63 +R: Andrew F. Davis <afd@ti.com>
64 +R: Benjamin Gaignard <benjamin.gaignard@linaro.org>
65 +R: Liam Mark <lmark@codeaurora.org>
66 +R: Laura Abbott <labbott@redhat.com>
67 +R: Brian Starkey <Brian.Starkey@arm.com>
68 +R: John Stultz <john.stultz@linaro.org>
69 +S: Maintained
70 +L: linux-media@vger.kernel.org
71 +L: dri-devel@lists.freedesktop.org
72 +L: linaro-mm-sig@lists.linaro.org (moderated for non-subscribers)
73 +F: include/uapi/linux/dma-heap.h
74 +F: include/linux/dma-heap.h
75 +F: drivers/dma-buf/dma-heap.c
76 +F: drivers/dma-buf/heaps/*
77 +T: git git://anongit.freedesktop.org/drm/drm-misc
78 +
79 DMA GENERIC OFFLOAD ENGINE SUBSYSTEM
80 M: Vinod Koul <vkoul@kernel.org>
81 L: dmaengine@vger.kernel.org
82 --- a/drivers/dma-buf/Kconfig
83 +++ b/drivers/dma-buf/Kconfig
84 @@ -44,4 +44,13 @@ config DMABUF_SELFTESTS
85 default n
86 depends on DMA_SHARED_BUFFER
87
88 +menuconfig DMABUF_HEAPS
89 + bool "DMA-BUF Userland Memory Heaps"
90 + select DMA_SHARED_BUFFER
91 + help
92 + Choose this option to enable the DMA-BUF userland memory heaps.
93 + This options creates per heap chardevs in /dev/dma_heap/ which
94 + allows userspace to allocate dma-bufs that can be shared
95 + between drivers.
96 +
97 endmenu
98 --- a/drivers/dma-buf/Makefile
99 +++ b/drivers/dma-buf/Makefile
100 @@ -3,6 +3,7 @@ obj-$(CONFIG_DMA_SHARED_BUFFER) := dma-s
101
102 dma-buf-objs-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
103 dma-resv.o seqno-fence.o
104 +obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
105 dma-buf-objs-$(CONFIG_SYNC_FILE) += sync_file.o
106 dma-buf-objs-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o
107 dma-buf-objs-$(CONFIG_UDMABUF) += udmabuf.o
108 --- /dev/null
109 +++ b/drivers/dma-buf/dma-heap.c
110 @@ -0,0 +1,297 @@
111 +// SPDX-License-Identifier: GPL-2.0
112 +/*
113 + * Framework for userspace DMA-BUF allocations
114 + *
115 + * Copyright (C) 2011 Google, Inc.
116 + * Copyright (C) 2019 Linaro Ltd.
117 + */
118 +
119 +#include <linux/cdev.h>
120 +#include <linux/debugfs.h>
121 +#include <linux/device.h>
122 +#include <linux/dma-buf.h>
123 +#include <linux/err.h>
124 +#include <linux/xarray.h>
125 +#include <linux/list.h>
126 +#include <linux/slab.h>
127 +#include <linux/uaccess.h>
128 +#include <linux/syscalls.h>
129 +#include <linux/dma-heap.h>
130 +#include <uapi/linux/dma-heap.h>
131 +
132 +#define DEVNAME "dma_heap"
133 +
134 +#define NUM_HEAP_MINORS 128
135 +
136 +/**
137 + * struct dma_heap - represents a dmabuf heap in the system
138 + * @name: used for debugging/device-node name
139 + * @ops: ops struct for this heap
140 + * @heap_devt heap device node
141 + * @list list head connecting to list of heaps
142 + * @heap_cdev heap char device
143 + *
144 + * Represents a heap of memory from which buffers can be made.
145 + */
146 +struct dma_heap {
147 + const char *name;
148 + const struct dma_heap_ops *ops;
149 + void *priv;
150 + dev_t heap_devt;
151 + struct list_head list;
152 + struct cdev heap_cdev;
153 +};
154 +
155 +static LIST_HEAD(heap_list);
156 +static DEFINE_MUTEX(heap_list_lock);
157 +static dev_t dma_heap_devt;
158 +static struct class *dma_heap_class;
159 +static DEFINE_XARRAY_ALLOC(dma_heap_minors);
160 +
161 +static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
162 + unsigned int fd_flags,
163 + unsigned int heap_flags)
164 +{
165 + /*
166 + * Allocations from all heaps have to begin
167 + * and end on page boundaries.
168 + */
169 + len = PAGE_ALIGN(len);
170 + if (!len)
171 + return -EINVAL;
172 +
173 + return heap->ops->allocate(heap, len, fd_flags, heap_flags);
174 +}
175 +
176 +static int dma_heap_open(struct inode *inode, struct file *file)
177 +{
178 + struct dma_heap *heap;
179 +
180 + heap = xa_load(&dma_heap_minors, iminor(inode));
181 + if (!heap) {
182 + pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
183 + return -ENODEV;
184 + }
185 +
186 + /* instance data as context */
187 + file->private_data = heap;
188 + nonseekable_open(inode, file);
189 +
190 + return 0;
191 +}
192 +
193 +static long dma_heap_ioctl_allocate(struct file *file, void *data)
194 +{
195 + struct dma_heap_allocation_data *heap_allocation = data;
196 + struct dma_heap *heap = file->private_data;
197 + int fd;
198 +
199 + if (heap_allocation->fd)
200 + return -EINVAL;
201 +
202 + if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
203 + return -EINVAL;
204 +
205 + if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
206 + return -EINVAL;
207 +
208 + fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
209 + heap_allocation->fd_flags,
210 + heap_allocation->heap_flags);
211 + if (fd < 0)
212 + return fd;
213 +
214 + heap_allocation->fd = fd;
215 +
216 + return 0;
217 +}
218 +
219 +unsigned int dma_heap_ioctl_cmds[] = {
220 + DMA_HEAP_IOC_ALLOC,
221 +};
222 +
223 +static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
224 + unsigned long arg)
225 +{
226 + char stack_kdata[128];
227 + char *kdata = stack_kdata;
228 + unsigned int kcmd;
229 + unsigned int in_size, out_size, drv_size, ksize;
230 + int nr = _IOC_NR(ucmd);
231 + int ret = 0;
232 +
233 + if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
234 + return -EINVAL;
235 +
236 + /* Get the kernel ioctl cmd that matches */
237 + kcmd = dma_heap_ioctl_cmds[nr];
238 +
239 + /* Figure out the delta between user cmd size and kernel cmd size */
240 + drv_size = _IOC_SIZE(kcmd);
241 + out_size = _IOC_SIZE(ucmd);
242 + in_size = out_size;
243 + if ((ucmd & kcmd & IOC_IN) == 0)
244 + in_size = 0;
245 + if ((ucmd & kcmd & IOC_OUT) == 0)
246 + out_size = 0;
247 + ksize = max(max(in_size, out_size), drv_size);
248 +
249 + /* If necessary, allocate buffer for ioctl argument */
250 + if (ksize > sizeof(stack_kdata)) {
251 + kdata = kmalloc(ksize, GFP_KERNEL);
252 + if (!kdata)
253 + return -ENOMEM;
254 + }
255 +
256 + if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
257 + ret = -EFAULT;
258 + goto err;
259 + }
260 +
261 + /* zero out any difference between the kernel/user structure size */
262 + if (ksize > in_size)
263 + memset(kdata + in_size, 0, ksize - in_size);
264 +
265 + switch (kcmd) {
266 + case DMA_HEAP_IOC_ALLOC:
267 + ret = dma_heap_ioctl_allocate(file, kdata);
268 + break;
269 + default:
270 + return -ENOTTY;
271 + }
272 +
273 + if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
274 + ret = -EFAULT;
275 +err:
276 + if (kdata != stack_kdata)
277 + kfree(kdata);
278 + return ret;
279 +}
280 +
281 +static const struct file_operations dma_heap_fops = {
282 + .owner = THIS_MODULE,
283 + .open = dma_heap_open,
284 + .unlocked_ioctl = dma_heap_ioctl,
285 +#ifdef CONFIG_COMPAT
286 + .compat_ioctl = dma_heap_ioctl,
287 +#endif
288 +};
289 +
290 +/**
291 + * dma_heap_get_drvdata() - get per-subdriver data for the heap
292 + * @heap: DMA-Heap to retrieve private data for
293 + *
294 + * Returns:
295 + * The per-subdriver data for the heap.
296 + */
297 +void *dma_heap_get_drvdata(struct dma_heap *heap)
298 +{
299 + return heap->priv;
300 +}
301 +
302 +struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
303 +{
304 + struct dma_heap *heap, *h, *err_ret;
305 + struct device *dev_ret;
306 + unsigned int minor;
307 + int ret;
308 +
309 + if (!exp_info->name || !strcmp(exp_info->name, "")) {
310 + pr_err("dma_heap: Cannot add heap without a name\n");
311 + return ERR_PTR(-EINVAL);
312 + }
313 +
314 + if (!exp_info->ops || !exp_info->ops->allocate) {
315 + pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
316 + return ERR_PTR(-EINVAL);
317 + }
318 +
319 + /* check the name is unique */
320 + mutex_lock(&heap_list_lock);
321 + list_for_each_entry(h, &heap_list, list) {
322 + if (!strcmp(h->name, exp_info->name)) {
323 + mutex_unlock(&heap_list_lock);
324 + pr_err("dma_heap: Already registered heap named %s\n",
325 + exp_info->name);
326 + return ERR_PTR(-EINVAL);
327 + }
328 + }
329 + mutex_unlock(&heap_list_lock);
330 +
331 + heap = kzalloc(sizeof(*heap), GFP_KERNEL);
332 + if (!heap)
333 + return ERR_PTR(-ENOMEM);
334 +
335 + heap->name = exp_info->name;
336 + heap->ops = exp_info->ops;
337 + heap->priv = exp_info->priv;
338 +
339 + /* Find unused minor number */
340 + ret = xa_alloc(&dma_heap_minors, &minor, heap,
341 + XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
342 + if (ret < 0) {
343 + pr_err("dma_heap: Unable to get minor number for heap\n");
344 + err_ret = ERR_PTR(ret);
345 + goto err0;
346 + }
347 +
348 + /* Create device */
349 + heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
350 +
351 + cdev_init(&heap->heap_cdev, &dma_heap_fops);
352 + ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
353 + if (ret < 0) {
354 + pr_err("dma_heap: Unable to add char device\n");
355 + err_ret = ERR_PTR(ret);
356 + goto err1;
357 + }
358 +
359 + dev_ret = device_create(dma_heap_class,
360 + NULL,
361 + heap->heap_devt,
362 + NULL,
363 + heap->name);
364 + if (IS_ERR(dev_ret)) {
365 + pr_err("dma_heap: Unable to create device\n");
366 + err_ret = ERR_CAST(dev_ret);
367 + goto err2;
368 + }
369 + /* Add heap to the list */
370 + mutex_lock(&heap_list_lock);
371 + list_add(&heap->list, &heap_list);
372 + mutex_unlock(&heap_list_lock);
373 +
374 + return heap;
375 +
376 +err2:
377 + cdev_del(&heap->heap_cdev);
378 +err1:
379 + xa_erase(&dma_heap_minors, minor);
380 +err0:
381 + kfree(heap);
382 + return err_ret;
383 +}
384 +
385 +static char *dma_heap_devnode(struct device *dev, umode_t *mode)
386 +{
387 + return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
388 +}
389 +
390 +static int dma_heap_init(void)
391 +{
392 + int ret;
393 +
394 + ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
395 + if (ret)
396 + return ret;
397 +
398 + dma_heap_class = class_create(THIS_MODULE, DEVNAME);
399 + if (IS_ERR(dma_heap_class)) {
400 + unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
401 + return PTR_ERR(dma_heap_class);
402 + }
403 + dma_heap_class->devnode = dma_heap_devnode;
404 +
405 + return 0;
406 +}
407 +subsys_initcall(dma_heap_init);
408 --- /dev/null
409 +++ b/include/linux/dma-heap.h
410 @@ -0,0 +1,59 @@
411 +/* SPDX-License-Identifier: GPL-2.0 */
412 +/*
413 + * DMABUF Heaps Allocation Infrastructure
414 + *
415 + * Copyright (C) 2011 Google, Inc.
416 + * Copyright (C) 2019 Linaro Ltd.
417 + */
418 +
419 +#ifndef _DMA_HEAPS_H
420 +#define _DMA_HEAPS_H
421 +
422 +#include <linux/cdev.h>
423 +#include <linux/types.h>
424 +
425 +struct dma_heap;
426 +
427 +/**
428 + * struct dma_heap_ops - ops to operate on a given heap
429 + * @allocate: allocate dmabuf and return fd
430 + *
431 + * allocate returns dmabuf fd on success, -errno on error.
432 + */
433 +struct dma_heap_ops {
434 + int (*allocate)(struct dma_heap *heap,
435 + unsigned long len,
436 + unsigned long fd_flags,
437 + unsigned long heap_flags);
438 +};
439 +
440 +/**
441 + * struct dma_heap_export_info - information needed to export a new dmabuf heap
442 + * @name: used for debugging/device-node name
443 + * @ops: ops struct for this heap
444 + * @priv: heap exporter private data
445 + *
446 + * Information needed to export a new dmabuf heap.
447 + */
448 +struct dma_heap_export_info {
449 + const char *name;
450 + const struct dma_heap_ops *ops;
451 + void *priv;
452 +};
453 +
454 +/**
455 + * dma_heap_get_drvdata() - get per-heap driver data
456 + * @heap: DMA-Heap to retrieve private data for
457 + *
458 + * Returns:
459 + * The per-heap data for the heap.
460 + */
461 +void *dma_heap_get_drvdata(struct dma_heap *heap);
462 +
463 +/**
464 + * dma_heap_add - adds a heap to dmabuf heaps
465 + * @exp_info: information needed to register this heap
466 + */
467 +struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
468 +
469 +#endif /* _DMA_HEAPS_H */
470 --- /dev/null
471 +++ b/include/uapi/linux/dma-heap.h
472 @@ -0,0 +1,53 @@
473 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
474 +/*
475 + * DMABUF Heaps Userspace API
476 + *
477 + * Copyright (C) 2011 Google, Inc.
478 + * Copyright (C) 2019 Linaro Ltd.
479 + */
480 +#ifndef _UAPI_LINUX_DMABUF_POOL_H
481 +#define _UAPI_LINUX_DMABUF_POOL_H
482 +
483 +#include <linux/ioctl.h>
484 +#include <linux/types.h>
485 +
486 +/**
487 + * DOC: DMABUF Heaps Userspace API
488 + */
489 +
490 +/* Valid FD_FLAGS are O_CLOEXEC, O_RDONLY, O_WRONLY, O_RDWR */
491 +#define DMA_HEAP_VALID_FD_FLAGS (O_CLOEXEC | O_ACCMODE)
492 +
493 +/* Currently no heap flags */
494 +#define DMA_HEAP_VALID_HEAP_FLAGS (0)
495 +
496 +/**
497 + * struct dma_heap_allocation_data - metadata passed from userspace for
498 + * allocations
499 + * @len: size of the allocation
500 + * @fd: will be populated with a fd which provides the
501 + * handle to the allocated dma-buf
502 + * @fd_flags: file descriptor flags used when allocating
503 + * @heap_flags: flags passed to heap
504 + *
505 + * Provided by userspace as an argument to the ioctl
506 + */
507 +struct dma_heap_allocation_data {
508 + __u64 len;
509 + __u32 fd;
510 + __u32 fd_flags;
511 + __u64 heap_flags;
512 +};
513 +
514 +#define DMA_HEAP_IOC_MAGIC 'H'
515 +
516 +/**
517 + * DOC: DMA_HEAP_IOC_ALLOC - allocate memory from pool
518 + *
519 + * Takes a dma_heap_allocation_data struct and returns it with the fd field
520 + * populated with the dmabuf handle of the allocation.
521 + */
522 +#define DMA_HEAP_IOC_ALLOC _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,\
523 + struct dma_heap_allocation_data)
524 +
525 +#endif /* _UAPI_LINUX_DMABUF_POOL_H */