|
| 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); |
0 commit comments