Skip to content

Commit a69b0e8

Browse files
glneosumitsemwal
authored andcommitted
dma-buf: Add dma-buf heaps framework
This framework allows a unified userspace interface for dma-buf exporters, allowing userland to allocate specific types of memory for use in dma-buf sharing. Each heap is given its own device node, which a user can allocate a dma-buf fd from using the DMA_HEAP_IOC_ALLOC. This code is an evoluiton of the Android ION implementation, and a big thanks is due to its authors/maintainers over time for their effort: Rebecca Schultz Zavin, Colin Cross, Benjamin Gaignard, Laura Abbott, and many other contributors! Cc: Laura Abbott <labbott@redhat.com> Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org> Cc: Sumit Semwal <sumit.semwal@linaro.org> Cc: Liam Mark <lmark@codeaurora.org> Cc: Pratik Patel <pratikp@codeaurora.org> Cc: Brian Starkey <Brian.Starkey@arm.com> Cc: Vincent Donnefort <Vincent.Donnefort@arm.com> Cc: Sudipto Paul <Sudipto.Paul@arm.com> Cc: Andrew F. Davis <afd@ti.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Chenbo Feng <fengc@google.com> Cc: Alistair Strachan <astrachan@google.com> Cc: Hridya Valsaraju <hridya@google.com> Cc: Hillf Danton <hdanton@sina.com> Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard <benjamin.gaignard@linaro.org> Reviewed-by: Brian Starkey <brian.starkey@arm.com> Acked-by: Laura Abbott <labbott@redhat.com> Tested-by: Ayan Kumar Halder <ayan.halder@arm.com> Signed-off-by: Andrew F. Davis <afd@ti.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org> Link: https://patchwork.freedesktop.org/patch/msgid/20191021190310.85221-2-john.stultz@linaro.org
1 parent ea7d8c6 commit a69b0e8

6 files changed

Lines changed: 411 additions & 0 deletions

File tree

‎MAINTAINERS‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,24 @@ F: include/linux/*fence.h
49424942
F: Documentation/driver-api/dma-buf.rst
49434943
T: git git://anongit.freedesktop.org/drm/drm-misc
49444944

4945+
DMA-BUF HEAPS FRAMEWORK
4946+
M: Sumit Semwal <sumit.semwal@linaro.org>
4947+
R: Andrew F. Davis <afd@ti.com>
4948+
R: Benjamin Gaignard <benjamin.gaignard@linaro.org>
4949+
R: Liam Mark <lmark@codeaurora.org>
4950+
R: Laura Abbott <labbott@redhat.com>
4951+
R: Brian Starkey <Brian.Starkey@arm.com>
4952+
R: John Stultz <john.stultz@linaro.org>
4953+
S: Maintained
4954+
L: linux-media@vger.kernel.org
4955+
L: dri-devel@lists.freedesktop.org
4956+
L: linaro-mm-sig@lists.linaro.org (moderated for non-subscribers)
4957+
F: include/uapi/linux/dma-heap.h
4958+
F: include/linux/dma-heap.h
4959+
F: drivers/dma-buf/dma-heap.c
4960+
F: drivers/dma-buf/heaps/*
4961+
T: git git://anongit.freedesktop.org/drm/drm-misc
4962+
49454963
DMA GENERIC OFFLOAD ENGINE SUBSYSTEM
49464964
M: Vinod Koul <vkoul@kernel.org>
49474965
L: dmaengine@vger.kernel.org

‎drivers/dma-buf/Kconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ config DMABUF_SELFTESTS
4444
default n
4545
depends on DMA_SHARED_BUFFER
4646

47+
menuconfig DMABUF_HEAPS
48+
bool "DMA-BUF Userland Memory Heaps"
49+
select DMA_SHARED_BUFFER
50+
help
51+
Choose this option to enable the DMA-BUF userland memory heaps.
52+
This options creates per heap chardevs in /dev/dma_heap/ which
53+
allows userspace to allocate dma-bufs that can be shared
54+
between drivers.
55+
4756
endmenu

‎drivers/dma-buf/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
33
dma-resv.o seqno-fence.o
4+
obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
45
obj-$(CONFIG_SYNC_FILE) += sync_file.o
56
obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o
67
obj-$(CONFIG_UDMABUF) += udmabuf.o

‎drivers/dma-buf/dma-heap.c‎

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Framework for userspace DMA-BUF allocations
4+
*
5+
* Copyright (C) 2011 Google, Inc.
6+
* Copyright (C) 2019 Linaro Ltd.
7+
*/
8+
9+
#include <linux/cdev.h>
10+
#include <linux/debugfs.h>
11+
#include <linux/device.h>
12+
#include <linux/dma-buf.h>
13+
#include <linux/err.h>
14+
#include <linux/xarray.h>
15+
#include <linux/list.h>
16+
#include <linux/slab.h>
17+
#include <linux/uaccess.h>
18+
#include <linux/syscalls.h>
19+
#include <linux/dma-heap.h>
20+
#include <uapi/linux/dma-heap.h>
21+
22+
#define DEVNAME "dma_heap"
23+
24+
#define NUM_HEAP_MINORS 128
25+
26+
/**
27+
* struct dma_heap - represents a dmabuf heap in the system
28+
* @name: used for debugging/device-node name
29+
* @ops: ops struct for this heap
30+
* @minor minor number of this heap device
31+
* @heap_devt heap device node
32+
* @heap_cdev heap char device
33+
*
34+
* Represents a heap of memory from which buffers can be made.
35+
*/
36+
struct dma_heap {
37+
const char *name;
38+
const struct dma_heap_ops *ops;
39+
void *priv;
40+
unsigned int minor;
41+
dev_t heap_devt;
42+
struct list_head list;
43+
struct cdev heap_cdev;
44+
};
45+
46+
static LIST_HEAD(heap_list);
47+
static DEFINE_MUTEX(heap_list_lock);
48+
static dev_t dma_heap_devt;
49+
static struct class *dma_heap_class;
50+
static DEFINE_XARRAY_ALLOC(dma_heap_minors);
51+
52+
static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
53+
unsigned int fd_flags,
54+
unsigned int heap_flags)
55+
{
56+
/*
57+
* Allocations from all heaps have to begin
58+
* and end on page boundaries.
59+
*/
60+
len = PAGE_ALIGN(len);
61+
if (!len)
62+
return -EINVAL;
63+
64+
return heap->ops->allocate(heap, len, fd_flags, heap_flags);
65+
}
66+
67+
static int dma_heap_open(struct inode *inode, struct file *file)
68+
{
69+
struct dma_heap *heap;
70+
71+
heap = xa_load(&dma_heap_minors, iminor(inode));
72+
if (!heap) {
73+
pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
74+
return -ENODEV;
75+
}
76+
77+
/* instance data as context */
78+
file->private_data = heap;
79+
nonseekable_open(inode, file);
80+
81+
return 0;
82+
}
83+
84+
static long dma_heap_ioctl_allocate(struct file *file, unsigned long arg)
85+
{
86+
struct dma_heap_allocation_data heap_allocation;
87+
struct dma_heap *heap = file->private_data;
88+
int fd;
89+
90+
if (copy_from_user(&heap_allocation, (void __user *)arg,
91+
sizeof(heap_allocation)))
92+
return -EFAULT;
93+
94+
if (heap_allocation.fd ||
95+
heap_allocation.reserved0 ||
96+
heap_allocation.reserved1) {
97+
pr_warn_once("dma_heap: ioctl data not valid\n");
98+
return -EINVAL;
99+
}
100+
101+
if (heap_allocation.fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) {
102+
pr_warn_once("dma_heap: fd_flags has invalid or unsupported flags set\n");
103+
return -EINVAL;
104+
}
105+
106+
if (heap_allocation.heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) {
107+
pr_warn_once("dma_heap: heap flags has invalid or unsupported flags set\n");
108+
return -EINVAL;
109+
}
110+
111+
fd = dma_heap_buffer_alloc(heap, heap_allocation.len,
112+
heap_allocation.fd_flags,
113+
heap_allocation.heap_flags);
114+
if (fd < 0)
115+
return fd;
116+
117+
heap_allocation.fd = fd;
118+
119+
if (copy_to_user((void __user *)arg, &heap_allocation,
120+
sizeof(heap_allocation))) {
121+
ksys_close(fd);
122+
return -EFAULT;
123+
}
124+
125+
return 0;
126+
}
127+
128+
static long dma_heap_ioctl(struct file *file, unsigned int cmd,
129+
unsigned long arg)
130+
{
131+
int ret = 0;
132+
133+
switch (cmd) {
134+
case DMA_HEAP_IOC_ALLOC:
135+
ret = dma_heap_ioctl_allocate(file, arg);
136+
break;
137+
default:
138+
return -ENOTTY;
139+
}
140+
141+
return ret;
142+
}
143+
144+
static const struct file_operations dma_heap_fops = {
145+
.owner = THIS_MODULE,
146+
.open = dma_heap_open,
147+
.unlocked_ioctl = dma_heap_ioctl,
148+
#ifdef CONFIG_COMPAT
149+
.compat_ioctl = dma_heap_ioctl,
150+
#endif
151+
};
152+
153+
/**
154+
* dma_heap_get_drvdata() - get per-subdriver data for the heap
155+
* @heap: DMA-Heap to retrieve private data for
156+
*
157+
* Returns:
158+
* The per-subdriver data for the heap.
159+
*/
160+
void *dma_heap_get_drvdata(struct dma_heap *heap)
161+
{
162+
return heap->priv;
163+
}
164+
165+
struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
166+
{
167+
struct dma_heap *heap, *h, *err_ret;
168+
struct device *dev_ret;
169+
int ret;
170+
171+
if (!exp_info->name || !strcmp(exp_info->name, "")) {
172+
pr_err("dma_heap: Cannot add heap without a name\n");
173+
return ERR_PTR(-EINVAL);
174+
}
175+
176+
if (!exp_info->ops || !exp_info->ops->allocate) {
177+
pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
178+
return ERR_PTR(-EINVAL);
179+
}
180+
181+
/* check the name is unique */
182+
mutex_lock(&heap_list_lock);
183+
list_for_each_entry(h, &heap_list, list) {
184+
if (!strcmp(h->name, exp_info->name)) {
185+
mutex_unlock(&heap_list_lock);
186+
pr_err("dma_heap: Already registered heap named %s\n",
187+
exp_info->name);
188+
return ERR_PTR(-EINVAL);
189+
}
190+
}
191+
mutex_unlock(&heap_list_lock);
192+
193+
heap = kzalloc(sizeof(*heap), GFP_KERNEL);
194+
if (!heap)
195+
return ERR_PTR(-ENOMEM);
196+
197+
heap->name = exp_info->name;
198+
heap->ops = exp_info->ops;
199+
heap->priv = exp_info->priv;
200+
201+
/* Find unused minor number */
202+
ret = xa_alloc(&dma_heap_minors, &heap->minor, heap,
203+
XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
204+
if (ret < 0) {
205+
pr_err("dma_heap: Unable to get minor number for heap\n");
206+
err_ret = ERR_PTR(ret);
207+
goto err0;
208+
}
209+
210+
/* Create device */
211+
heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), heap->minor);
212+
213+
cdev_init(&heap->heap_cdev, &dma_heap_fops);
214+
ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
215+
if (ret < 0) {
216+
pr_err("dma_heap: Unable to add char device\n");
217+
err_ret = ERR_PTR(ret);
218+
goto err1;
219+
}
220+
221+
dev_ret = device_create(dma_heap_class,
222+
NULL,
223+
heap->heap_devt,
224+
NULL,
225+
heap->name);
226+
if (IS_ERR(dev_ret)) {
227+
pr_err("dma_heap: Unable to create device\n");
228+
err_ret = ERR_CAST(dev_ret);
229+
goto err2;
230+
}
231+
/* Add heap to the list */
232+
mutex_lock(&heap_list_lock);
233+
list_add(&heap->list, &heap_list);
234+
mutex_unlock(&heap_list_lock);
235+
236+
return heap;
237+
238+
err2:
239+
cdev_del(&heap->heap_cdev);
240+
err1:
241+
xa_erase(&dma_heap_minors, heap->minor);
242+
err0:
243+
kfree(heap);
244+
return err_ret;
245+
}
246+
247+
static char *dma_heap_devnode(struct device *dev, umode_t *mode)
248+
{
249+
return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
250+
}
251+
252+
static int dma_heap_init(void)
253+
{
254+
int ret;
255+
256+
ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
257+
if (ret)
258+
return ret;
259+
260+
dma_heap_class = class_create(THIS_MODULE, DEVNAME);
261+
if (IS_ERR(dma_heap_class)) {
262+
unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
263+
return PTR_ERR(dma_heap_class);
264+
}
265+
dma_heap_class->devnode = dma_heap_devnode;
266+
267+
return 0;
268+
}
269+
subsys_initcall(dma_heap_init);

‎include/linux/dma-heap.h‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* DMABUF Heaps Allocation Infrastructure
4+
*
5+
* Copyright (C) 2011 Google, Inc.
6+
* Copyright (C) 2019 Linaro Ltd.
7+
*/
8+
9+
#ifndef _DMA_HEAPS_H
10+
#define _DMA_HEAPS_H
11+
12+
#include <linux/cdev.h>
13+
#include <linux/types.h>
14+
15+
struct dma_heap;
16+
17+
/**
18+
* struct dma_heap_ops - ops to operate on a given heap
19+
* @allocate: allocate dmabuf and return fd
20+
*
21+
* allocate returns dmabuf fd on success, -errno on error.
22+
*/
23+
struct dma_heap_ops {
24+
int (*allocate)(struct dma_heap *heap,
25+
unsigned long len,
26+
unsigned long fd_flags,
27+
unsigned long heap_flags);
28+
};
29+
30+
/**
31+
* struct dma_heap_export_info - information needed to export a new dmabuf heap
32+
* @name: used for debugging/device-node name
33+
* @ops: ops struct for this heap
34+
* @priv: heap exporter private data
35+
*
36+
* Information needed to export a new dmabuf heap.
37+
*/
38+
struct dma_heap_export_info {
39+
const char *name;
40+
const struct dma_heap_ops *ops;
41+
void *priv;
42+
};
43+
44+
/**
45+
* dma_heap_get_drvdata() - get per-heap driver data
46+
* @heap: DMA-Heap to retrieve private data for
47+
*
48+
* Returns:
49+
* The per-heap data for the heap.
50+
*/
51+
void *dma_heap_get_drvdata(struct dma_heap *heap);
52+
53+
/**
54+
* dma_heap_add - adds a heap to dmabuf heaps
55+
* @exp_info: information needed to register this heap
56+
*/
57+
struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
58+
59+
#endif /* _DMA_HEAPS_H */

0 commit comments

Comments
 (0)