dma-heap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <linux/cdev.h>
  9. #include <linux/device.h>
  10. #include <linux/dma-buf.h>
  11. #include <linux/dma-heap.h>
  12. #include <linux/err.h>
  13. #include <linux/list.h>
  14. #include <linux/nospec.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/xarray.h>
  18. #include <uapi/linux/dma-heap.h>
  19. #define DEVNAME "dma_heap"
  20. #define NUM_HEAP_MINORS 128
  21. /**
  22. * struct dma_heap - represents a dmabuf heap in the system
  23. * @name: used for debugging/device-node name
  24. * @ops: ops struct for this heap
  25. * @priv: private data for this heap
  26. * @heap_devt: heap device node
  27. * @list: list head connecting to list of heaps
  28. * @heap_cdev: heap char device
  29. *
  30. * Represents a heap of memory from which buffers can be made.
  31. */
  32. struct dma_heap {
  33. const char *name;
  34. const struct dma_heap_ops *ops;
  35. void *priv;
  36. dev_t heap_devt;
  37. struct list_head list;
  38. struct cdev heap_cdev;
  39. };
  40. static LIST_HEAD(heap_list);
  41. static DEFINE_MUTEX(heap_list_lock);
  42. static dev_t dma_heap_devt;
  43. static struct class *dma_heap_class;
  44. static DEFINE_XARRAY_ALLOC(dma_heap_minors);
  45. static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
  46. u32 fd_flags,
  47. u64 heap_flags)
  48. {
  49. struct dma_buf *dmabuf;
  50. int fd;
  51. /*
  52. * Allocations from all heaps have to begin
  53. * and end on page boundaries.
  54. */
  55. len = PAGE_ALIGN(len);
  56. if (!len)
  57. return -EINVAL;
  58. dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags);
  59. if (IS_ERR(dmabuf))
  60. return PTR_ERR(dmabuf);
  61. fd = dma_buf_fd(dmabuf, fd_flags);
  62. if (fd < 0) {
  63. dma_buf_put(dmabuf);
  64. /* just return, as put will call release and that will free */
  65. }
  66. return fd;
  67. }
  68. static int dma_heap_open(struct inode *inode, struct file *file)
  69. {
  70. struct dma_heap *heap;
  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. /* instance data as context */
  77. file->private_data = heap;
  78. nonseekable_open(inode, file);
  79. return 0;
  80. }
  81. static long dma_heap_ioctl_allocate(struct file *file, void *data)
  82. {
  83. struct dma_heap_allocation_data *heap_allocation = data;
  84. struct dma_heap *heap = file->private_data;
  85. int fd;
  86. if (heap_allocation->fd)
  87. return -EINVAL;
  88. if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
  89. return -EINVAL;
  90. if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
  91. return -EINVAL;
  92. fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
  93. heap_allocation->fd_flags,
  94. heap_allocation->heap_flags);
  95. if (fd < 0)
  96. return fd;
  97. heap_allocation->fd = fd;
  98. return 0;
  99. }
  100. static unsigned int dma_heap_ioctl_cmds[] = {
  101. DMA_HEAP_IOCTL_ALLOC,
  102. };
  103. static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
  104. unsigned long arg)
  105. {
  106. char stack_kdata[128];
  107. char *kdata = stack_kdata;
  108. unsigned int kcmd;
  109. unsigned int in_size, out_size, drv_size, ksize;
  110. int nr = _IOC_NR(ucmd);
  111. int ret = 0;
  112. if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
  113. return -EINVAL;
  114. nr = array_index_nospec(nr, ARRAY_SIZE(dma_heap_ioctl_cmds));
  115. /* Get the kernel ioctl cmd that matches */
  116. kcmd = dma_heap_ioctl_cmds[nr];
  117. /* Figure out the delta between user cmd size and kernel cmd size */
  118. drv_size = _IOC_SIZE(kcmd);
  119. out_size = _IOC_SIZE(ucmd);
  120. in_size = out_size;
  121. if ((ucmd & kcmd & IOC_IN) == 0)
  122. in_size = 0;
  123. if ((ucmd & kcmd & IOC_OUT) == 0)
  124. out_size = 0;
  125. ksize = max(max(in_size, out_size), drv_size);
  126. /* If necessary, allocate buffer for ioctl argument */
  127. if (ksize > sizeof(stack_kdata)) {
  128. kdata = kmalloc(ksize, GFP_KERNEL);
  129. if (!kdata)
  130. return -ENOMEM;
  131. }
  132. if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
  133. ret = -EFAULT;
  134. goto err;
  135. }
  136. /* zero out any difference between the kernel/user structure size */
  137. if (ksize > in_size)
  138. memset(kdata + in_size, 0, ksize - in_size);
  139. switch (kcmd) {
  140. case DMA_HEAP_IOCTL_ALLOC:
  141. ret = dma_heap_ioctl_allocate(file, kdata);
  142. break;
  143. default:
  144. ret = -ENOTTY;
  145. goto err;
  146. }
  147. if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
  148. ret = -EFAULT;
  149. err:
  150. if (kdata != stack_kdata)
  151. kfree(kdata);
  152. return ret;
  153. }
  154. static const struct file_operations dma_heap_fops = {
  155. .owner = THIS_MODULE,
  156. .open = dma_heap_open,
  157. .unlocked_ioctl = dma_heap_ioctl,
  158. #ifdef CONFIG_COMPAT
  159. .compat_ioctl = dma_heap_ioctl,
  160. #endif
  161. };
  162. /**
  163. * dma_heap_get_drvdata - get per-heap driver data
  164. * @heap: DMA-Heap to retrieve private data for
  165. *
  166. * Returns:
  167. * The per-heap data for the heap.
  168. */
  169. void *dma_heap_get_drvdata(struct dma_heap *heap)
  170. {
  171. return heap->priv;
  172. }
  173. /**
  174. * dma_heap_get_name - get heap name
  175. * @heap: DMA-Heap to retrieve the name of
  176. *
  177. * Returns:
  178. * The char* for the heap name.
  179. */
  180. const char *dma_heap_get_name(struct dma_heap *heap)
  181. {
  182. return heap->name;
  183. }
  184. /**
  185. * dma_heap_add - adds a heap to dmabuf heaps
  186. * @exp_info: information needed to register this heap
  187. */
  188. struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
  189. {
  190. struct dma_heap *heap, *h, *err_ret;
  191. struct device *dev_ret;
  192. unsigned int minor;
  193. int ret;
  194. if (!exp_info->name || !strcmp(exp_info->name, "")) {
  195. pr_err("dma_heap: Cannot add heap without a name\n");
  196. return ERR_PTR(-EINVAL);
  197. }
  198. if (!exp_info->ops || !exp_info->ops->allocate) {
  199. pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
  200. return ERR_PTR(-EINVAL);
  201. }
  202. heap = kzalloc(sizeof(*heap), GFP_KERNEL);
  203. if (!heap)
  204. return ERR_PTR(-ENOMEM);
  205. heap->name = exp_info->name;
  206. heap->ops = exp_info->ops;
  207. heap->priv = exp_info->priv;
  208. /* Find unused minor number */
  209. ret = xa_alloc(&dma_heap_minors, &minor, heap,
  210. XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
  211. if (ret < 0) {
  212. pr_err("dma_heap: Unable to get minor number for heap\n");
  213. err_ret = ERR_PTR(ret);
  214. goto err0;
  215. }
  216. /* Create device */
  217. heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
  218. cdev_init(&heap->heap_cdev, &dma_heap_fops);
  219. ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
  220. if (ret < 0) {
  221. pr_err("dma_heap: Unable to add char device\n");
  222. err_ret = ERR_PTR(ret);
  223. goto err1;
  224. }
  225. dev_ret = device_create(dma_heap_class,
  226. NULL,
  227. heap->heap_devt,
  228. NULL,
  229. heap->name);
  230. if (IS_ERR(dev_ret)) {
  231. pr_err("dma_heap: Unable to create device\n");
  232. err_ret = ERR_CAST(dev_ret);
  233. goto err2;
  234. }
  235. mutex_lock(&heap_list_lock);
  236. /* check the name is unique */
  237. list_for_each_entry(h, &heap_list, list) {
  238. if (!strcmp(h->name, exp_info->name)) {
  239. mutex_unlock(&heap_list_lock);
  240. pr_err("dma_heap: Already registered heap named %s\n",
  241. exp_info->name);
  242. err_ret = ERR_PTR(-EINVAL);
  243. goto err3;
  244. }
  245. }
  246. /* Add heap to the list */
  247. list_add(&heap->list, &heap_list);
  248. mutex_unlock(&heap_list_lock);
  249. return heap;
  250. err3:
  251. device_destroy(dma_heap_class, heap->heap_devt);
  252. err2:
  253. cdev_del(&heap->heap_cdev);
  254. err1:
  255. xa_erase(&dma_heap_minors, minor);
  256. err0:
  257. kfree(heap);
  258. return err_ret;
  259. }
  260. static char *dma_heap_devnode(const struct device *dev, umode_t *mode)
  261. {
  262. return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
  263. }
  264. static int dma_heap_init(void)
  265. {
  266. int ret;
  267. ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
  268. if (ret)
  269. return ret;
  270. dma_heap_class = class_create(DEVNAME);
  271. if (IS_ERR(dma_heap_class)) {
  272. unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
  273. return PTR_ERR(dma_heap_class);
  274. }
  275. dma_heap_class->devnode = dma_heap_devnode;
  276. return 0;
  277. }
  278. subsys_initcall(dma_heap_init);