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
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.
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.
14 This code is an evoluiton of the Android ION implementation,
15 and a big thanks is due to its authors/maintainers over time
17 Rebecca Schultz Zavin, Colin Cross, Benjamin Gaignard,
18 Laura Abbott, and many other contributors!
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
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
57 @@ -4963,6 +4963,24 @@ F: include/linux/*fence.h
58 F: Documentation/driver-api/dma-buf.rst
59 T: git git://anongit.freedesktop.org/drm/drm-misc
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>
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
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
86 depends on DMA_SHARED_BUFFER
88 +menuconfig DMABUF_HEAPS
89 + bool "DMA-BUF Userland Memory Heaps"
90 + select DMA_SHARED_BUFFER
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
98 --- a/drivers/dma-buf/Makefile
99 +++ b/drivers/dma-buf/Makefile
100 @@ -3,6 +3,7 @@ obj-$(CONFIG_DMA_SHARED_BUFFER) := dma-s
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
109 +++ b/drivers/dma-buf/dma-heap.c
111 +// SPDX-License-Identifier: GPL-2.0
113 + * Framework for userspace DMA-BUF allocations
115 + * Copyright (C) 2011 Google, Inc.
116 + * Copyright (C) 2019 Linaro Ltd.
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>
132 +#define DEVNAME "dma_heap"
134 +#define NUM_HEAP_MINORS 128
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
144 + * Represents a heap of memory from which buffers can be made.
148 + const struct dma_heap_ops *ops;
151 + struct list_head list;
152 + struct cdev heap_cdev;
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);
161 +static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
162 + unsigned int fd_flags,
163 + unsigned int heap_flags)
166 + * Allocations from all heaps have to begin
167 + * and end on page boundaries.
169 + len = PAGE_ALIGN(len);
173 + return heap->ops->allocate(heap, len, fd_flags, heap_flags);
176 +static int dma_heap_open(struct inode *inode, struct file *file)
178 + struct dma_heap *heap;
180 + heap = xa_load(&dma_heap_minors, iminor(inode));
182 + pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
186 + /* instance data as context */
187 + file->private_data = heap;
188 + nonseekable_open(inode, file);
193 +static long dma_heap_ioctl_allocate(struct file *file, void *data)
195 + struct dma_heap_allocation_data *heap_allocation = data;
196 + struct dma_heap *heap = file->private_data;
199 + if (heap_allocation->fd)
202 + if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
205 + if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
208 + fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
209 + heap_allocation->fd_flags,
210 + heap_allocation->heap_flags);
214 + heap_allocation->fd = fd;
219 +unsigned int dma_heap_ioctl_cmds[] = {
220 + DMA_HEAP_IOC_ALLOC,
223 +static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
226 + char stack_kdata[128];
227 + char *kdata = stack_kdata;
229 + unsigned int in_size, out_size, drv_size, ksize;
230 + int nr = _IOC_NR(ucmd);
233 + if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
236 + /* Get the kernel ioctl cmd that matches */
237 + kcmd = dma_heap_ioctl_cmds[nr];
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)
245 + if ((ucmd & kcmd & IOC_OUT) == 0)
247 + ksize = max(max(in_size, out_size), drv_size);
249 + /* If necessary, allocate buffer for ioctl argument */
250 + if (ksize > sizeof(stack_kdata)) {
251 + kdata = kmalloc(ksize, GFP_KERNEL);
256 + if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
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);
266 + case DMA_HEAP_IOC_ALLOC:
267 + ret = dma_heap_ioctl_allocate(file, kdata);
273 + if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
276 + if (kdata != stack_kdata)
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,
291 + * dma_heap_get_drvdata() - get per-subdriver data for the heap
292 + * @heap: DMA-Heap to retrieve private data for
295 + * The per-subdriver data for the heap.
297 +void *dma_heap_get_drvdata(struct dma_heap *heap)
302 +struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
304 + struct dma_heap *heap, *h, *err_ret;
305 + struct device *dev_ret;
306 + unsigned int minor;
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);
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);
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",
326 + return ERR_PTR(-EINVAL);
329 + mutex_unlock(&heap_list_lock);
331 + heap = kzalloc(sizeof(*heap), GFP_KERNEL);
333 + return ERR_PTR(-ENOMEM);
335 + heap->name = exp_info->name;
336 + heap->ops = exp_info->ops;
337 + heap->priv = exp_info->priv;
339 + /* Find unused minor number */
340 + ret = xa_alloc(&dma_heap_minors, &minor, heap,
341 + XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
343 + pr_err("dma_heap: Unable to get minor number for heap\n");
344 + err_ret = ERR_PTR(ret);
348 + /* Create device */
349 + heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
351 + cdev_init(&heap->heap_cdev, &dma_heap_fops);
352 + ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
354 + pr_err("dma_heap: Unable to add char device\n");
355 + err_ret = ERR_PTR(ret);
359 + dev_ret = device_create(dma_heap_class,
364 + if (IS_ERR(dev_ret)) {
365 + pr_err("dma_heap: Unable to create device\n");
366 + err_ret = ERR_CAST(dev_ret);
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);
377 + cdev_del(&heap->heap_cdev);
379 + xa_erase(&dma_heap_minors, minor);
385 +static char *dma_heap_devnode(struct device *dev, umode_t *mode)
387 + return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
390 +static int dma_heap_init(void)
394 + ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
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);
403 + dma_heap_class->devnode = dma_heap_devnode;
407 +subsys_initcall(dma_heap_init);
409 +++ b/include/linux/dma-heap.h
411 +/* SPDX-License-Identifier: GPL-2.0 */
413 + * DMABUF Heaps Allocation Infrastructure
415 + * Copyright (C) 2011 Google, Inc.
416 + * Copyright (C) 2019 Linaro Ltd.
419 +#ifndef _DMA_HEAPS_H
420 +#define _DMA_HEAPS_H
422 +#include <linux/cdev.h>
423 +#include <linux/types.h>
428 + * struct dma_heap_ops - ops to operate on a given heap
429 + * @allocate: allocate dmabuf and return fd
431 + * allocate returns dmabuf fd on success, -errno on error.
433 +struct dma_heap_ops {
434 + int (*allocate)(struct dma_heap *heap,
436 + unsigned long fd_flags,
437 + unsigned long heap_flags);
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
446 + * Information needed to export a new dmabuf heap.
448 +struct dma_heap_export_info {
450 + const struct dma_heap_ops *ops;
455 + * dma_heap_get_drvdata() - get per-heap driver data
456 + * @heap: DMA-Heap to retrieve private data for
459 + * The per-heap data for the heap.
461 +void *dma_heap_get_drvdata(struct dma_heap *heap);
464 + * dma_heap_add - adds a heap to dmabuf heaps
465 + * @exp_info: information needed to register this heap
467 +struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
469 +#endif /* _DMA_HEAPS_H */
471 +++ b/include/uapi/linux/dma-heap.h
473 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
475 + * DMABUF Heaps Userspace API
477 + * Copyright (C) 2011 Google, Inc.
478 + * Copyright (C) 2019 Linaro Ltd.
480 +#ifndef _UAPI_LINUX_DMABUF_POOL_H
481 +#define _UAPI_LINUX_DMABUF_POOL_H
483 +#include <linux/ioctl.h>
484 +#include <linux/types.h>
487 + * DOC: DMABUF Heaps Userspace API
490 +/* Valid FD_FLAGS are O_CLOEXEC, O_RDONLY, O_WRONLY, O_RDWR */
491 +#define DMA_HEAP_VALID_FD_FLAGS (O_CLOEXEC | O_ACCMODE)
493 +/* Currently no heap flags */
494 +#define DMA_HEAP_VALID_HEAP_FLAGS (0)
497 + * struct dma_heap_allocation_data - metadata passed from userspace for
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
505 + * Provided by userspace as an argument to the ioctl
507 +struct dma_heap_allocation_data {
514 +#define DMA_HEAP_IOC_MAGIC 'H'
517 + * DOC: DMA_HEAP_IOC_ALLOC - allocate memory from pool
519 + * Takes a dma_heap_allocation_data struct and returns it with the fd field
520 + * populated with the dmabuf handle of the allocation.
522 +#define DMA_HEAP_IOC_ALLOC _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,\
523 + struct dma_heap_allocation_data)
525 +#endif /* _UAPI_LINUX_DMABUF_POOL_H */