1 // SPDX-License-Identifier: GPL-2.0
3 * Framework for userspace DMA-BUF allocations
5 * Copyright (C) 2011 Google, Inc.
6 * Copyright (C) 2019 Linaro Ltd.
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/nospec.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/dma-heap.h>
21 #include <uapi/linux/dma-heap.h>
23 #define DEVNAME "dma_heap"
25 #define NUM_HEAP_MINORS 128
28 * struct dma_heap - represents a dmabuf heap in the system
29 * @name: used for debugging/device-node name
30 * @ops: ops struct for this heap
31 * @heap_devt heap device node
32 * @list list head connecting to list of heaps
33 * @heap_cdev heap char device
35 * Represents a heap of memory from which buffers can be made.
39 const struct dma_heap_ops *ops;
42 struct list_head list;
43 struct cdev heap_cdev;
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);
52 static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
53 unsigned int fd_flags,
54 unsigned int heap_flags)
56 struct dma_buf *dmabuf;
60 * Allocations from all heaps have to begin
61 * and end on page boundaries.
63 len = PAGE_ALIGN(len);
67 dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags);
69 return PTR_ERR(dmabuf);
71 fd = dma_buf_fd(dmabuf, fd_flags);
74 /* just return, as put will call release and that will free */
79 static int dma_heap_open(struct inode *inode, struct file *file)
81 struct dma_heap *heap;
83 heap = xa_load(&dma_heap_minors, iminor(inode));
85 pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
89 /* instance data as context */
90 file->private_data = heap;
91 nonseekable_open(inode, file);
96 static long dma_heap_ioctl_allocate(struct file *file, void *data)
98 struct dma_heap_allocation_data *heap_allocation = data;
99 struct dma_heap *heap = file->private_data;
102 if (heap_allocation->fd)
105 if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
108 if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
111 fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
112 heap_allocation->fd_flags,
113 heap_allocation->heap_flags);
117 heap_allocation->fd = fd;
122 static unsigned int dma_heap_ioctl_cmds[] = {
123 DMA_HEAP_IOCTL_ALLOC,
126 static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
129 char stack_kdata[128];
130 char *kdata = stack_kdata;
132 unsigned int in_size, out_size, drv_size, ksize;
133 int nr = _IOC_NR(ucmd);
136 if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
139 nr = array_index_nospec(nr, ARRAY_SIZE(dma_heap_ioctl_cmds));
140 /* Get the kernel ioctl cmd that matches */
141 kcmd = dma_heap_ioctl_cmds[nr];
143 /* Figure out the delta between user cmd size and kernel cmd size */
144 drv_size = _IOC_SIZE(kcmd);
145 out_size = _IOC_SIZE(ucmd);
147 if ((ucmd & kcmd & IOC_IN) == 0)
149 if ((ucmd & kcmd & IOC_OUT) == 0)
151 ksize = max(max(in_size, out_size), drv_size);
153 /* If necessary, allocate buffer for ioctl argument */
154 if (ksize > sizeof(stack_kdata)) {
155 kdata = kmalloc(ksize, GFP_KERNEL);
160 if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
165 /* zero out any difference between the kernel/user structure size */
167 memset(kdata + in_size, 0, ksize - in_size);
170 case DMA_HEAP_IOCTL_ALLOC:
171 ret = dma_heap_ioctl_allocate(file, kdata);
178 if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
181 if (kdata != stack_kdata)
186 static const struct file_operations dma_heap_fops = {
187 .owner = THIS_MODULE,
188 .open = dma_heap_open,
189 .unlocked_ioctl = dma_heap_ioctl,
191 .compat_ioctl = dma_heap_ioctl,
196 * dma_heap_get_drvdata() - get per-subdriver data for the heap
197 * @heap: DMA-Heap to retrieve private data for
200 * The per-subdriver data for the heap.
202 void *dma_heap_get_drvdata(struct dma_heap *heap)
208 * dma_heap_get_name() - get heap name
209 * @heap: DMA-Heap to retrieve private data for
212 * The char* for the heap name.
214 const char *dma_heap_get_name(struct dma_heap *heap)
219 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
221 struct dma_heap *heap, *h, *err_ret;
222 struct device *dev_ret;
226 if (!exp_info->name || !strcmp(exp_info->name, "")) {
227 pr_err("dma_heap: Cannot add heap without a name\n");
228 return ERR_PTR(-EINVAL);
231 if (!exp_info->ops || !exp_info->ops->allocate) {
232 pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
233 return ERR_PTR(-EINVAL);
236 heap = kzalloc(sizeof(*heap), GFP_KERNEL);
238 return ERR_PTR(-ENOMEM);
240 heap->name = exp_info->name;
241 heap->ops = exp_info->ops;
242 heap->priv = exp_info->priv;
244 /* Find unused minor number */
245 ret = xa_alloc(&dma_heap_minors, &minor, heap,
246 XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
248 pr_err("dma_heap: Unable to get minor number for heap\n");
249 err_ret = ERR_PTR(ret);
254 heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
256 cdev_init(&heap->heap_cdev, &dma_heap_fops);
257 ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
259 pr_err("dma_heap: Unable to add char device\n");
260 err_ret = ERR_PTR(ret);
264 dev_ret = device_create(dma_heap_class,
269 if (IS_ERR(dev_ret)) {
270 pr_err("dma_heap: Unable to create device\n");
271 err_ret = ERR_CAST(dev_ret);
275 mutex_lock(&heap_list_lock);
276 /* check the name is unique */
277 list_for_each_entry(h, &heap_list, list) {
278 if (!strcmp(h->name, exp_info->name)) {
279 mutex_unlock(&heap_list_lock);
280 pr_err("dma_heap: Already registered heap named %s\n",
282 err_ret = ERR_PTR(-EINVAL);
287 /* Add heap to the list */
288 list_add(&heap->list, &heap_list);
289 mutex_unlock(&heap_list_lock);
294 device_destroy(dma_heap_class, heap->heap_devt);
296 cdev_del(&heap->heap_cdev);
298 xa_erase(&dma_heap_minors, minor);
304 static char *dma_heap_devnode(const struct device *dev, umode_t *mode)
306 return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
309 static int dma_heap_init(void)
313 ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
317 dma_heap_class = class_create(DEVNAME);
318 if (IS_ERR(dma_heap_class)) {
319 unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
320 return PTR_ERR(dma_heap_class);
322 dma_heap_class->devnode = dma_heap_devnode;
326 subsys_initcall(dma_heap_init);