cma_heap.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMABUF CMA heap exporter
  4. *
  5. * Copyright (C) 2012, 2019, 2020 Linaro Ltd.
  6. * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
  7. *
  8. * Also utilizing parts of Andrew Davis' SRAM heap:
  9. * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
  10. * Andrew F. Davis <afd@ti.com>
  11. */
  12. #include <linux/cma.h>
  13. #include <linux/dma-buf.h>
  14. #include <linux/dma-heap.h>
  15. #include <linux/dma-map-ops.h>
  16. #include <linux/err.h>
  17. #include <linux/highmem.h>
  18. #include <linux/io.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. struct cma_heap {
  25. struct dma_heap *heap;
  26. struct cma *cma;
  27. };
  28. struct cma_heap_buffer {
  29. struct cma_heap *heap;
  30. struct list_head attachments;
  31. struct mutex lock;
  32. unsigned long len;
  33. struct page *cma_pages;
  34. struct page **pages;
  35. pgoff_t pagecount;
  36. int vmap_cnt;
  37. void *vaddr;
  38. };
  39. struct dma_heap_attachment {
  40. struct device *dev;
  41. struct sg_table table;
  42. struct list_head list;
  43. bool mapped;
  44. };
  45. static int cma_heap_attach(struct dma_buf *dmabuf,
  46. struct dma_buf_attachment *attachment)
  47. {
  48. struct cma_heap_buffer *buffer = dmabuf->priv;
  49. struct dma_heap_attachment *a;
  50. int ret;
  51. a = kzalloc(sizeof(*a), GFP_KERNEL);
  52. if (!a)
  53. return -ENOMEM;
  54. ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
  55. buffer->pagecount, 0,
  56. buffer->pagecount << PAGE_SHIFT,
  57. GFP_KERNEL);
  58. if (ret) {
  59. kfree(a);
  60. return ret;
  61. }
  62. a->dev = attachment->dev;
  63. INIT_LIST_HEAD(&a->list);
  64. a->mapped = false;
  65. attachment->priv = a;
  66. mutex_lock(&buffer->lock);
  67. list_add(&a->list, &buffer->attachments);
  68. mutex_unlock(&buffer->lock);
  69. return 0;
  70. }
  71. static void cma_heap_detach(struct dma_buf *dmabuf,
  72. struct dma_buf_attachment *attachment)
  73. {
  74. struct cma_heap_buffer *buffer = dmabuf->priv;
  75. struct dma_heap_attachment *a = attachment->priv;
  76. mutex_lock(&buffer->lock);
  77. list_del(&a->list);
  78. mutex_unlock(&buffer->lock);
  79. sg_free_table(&a->table);
  80. kfree(a);
  81. }
  82. static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
  83. enum dma_data_direction direction)
  84. {
  85. struct dma_heap_attachment *a = attachment->priv;
  86. struct sg_table *table = &a->table;
  87. int ret;
  88. ret = dma_map_sgtable(attachment->dev, table, direction, 0);
  89. if (ret)
  90. return ERR_PTR(-ENOMEM);
  91. a->mapped = true;
  92. return table;
  93. }
  94. static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
  95. struct sg_table *table,
  96. enum dma_data_direction direction)
  97. {
  98. struct dma_heap_attachment *a = attachment->priv;
  99. a->mapped = false;
  100. dma_unmap_sgtable(attachment->dev, table, direction, 0);
  101. }
  102. static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  103. enum dma_data_direction direction)
  104. {
  105. struct cma_heap_buffer *buffer = dmabuf->priv;
  106. struct dma_heap_attachment *a;
  107. mutex_lock(&buffer->lock);
  108. if (buffer->vmap_cnt)
  109. invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
  110. list_for_each_entry(a, &buffer->attachments, list) {
  111. if (!a->mapped)
  112. continue;
  113. dma_sync_sgtable_for_cpu(a->dev, &a->table, direction);
  114. }
  115. mutex_unlock(&buffer->lock);
  116. return 0;
  117. }
  118. static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
  119. enum dma_data_direction direction)
  120. {
  121. struct cma_heap_buffer *buffer = dmabuf->priv;
  122. struct dma_heap_attachment *a;
  123. mutex_lock(&buffer->lock);
  124. if (buffer->vmap_cnt)
  125. flush_kernel_vmap_range(buffer->vaddr, buffer->len);
  126. list_for_each_entry(a, &buffer->attachments, list) {
  127. if (!a->mapped)
  128. continue;
  129. dma_sync_sgtable_for_device(a->dev, &a->table, direction);
  130. }
  131. mutex_unlock(&buffer->lock);
  132. return 0;
  133. }
  134. static vm_fault_t cma_heap_vm_fault(struct vm_fault *vmf)
  135. {
  136. struct vm_area_struct *vma = vmf->vma;
  137. struct cma_heap_buffer *buffer = vma->vm_private_data;
  138. if (vmf->pgoff >= buffer->pagecount)
  139. return VM_FAULT_SIGBUS;
  140. return vmf_insert_pfn(vma, vmf->address, page_to_pfn(buffer->pages[vmf->pgoff]));
  141. }
  142. static const struct vm_operations_struct dma_heap_vm_ops = {
  143. .fault = cma_heap_vm_fault,
  144. };
  145. static int cma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
  146. {
  147. struct cma_heap_buffer *buffer = dmabuf->priv;
  148. if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
  149. return -EINVAL;
  150. vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
  151. vma->vm_ops = &dma_heap_vm_ops;
  152. vma->vm_private_data = buffer;
  153. return 0;
  154. }
  155. static void *cma_heap_do_vmap(struct cma_heap_buffer *buffer)
  156. {
  157. void *vaddr;
  158. vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL);
  159. if (!vaddr)
  160. return ERR_PTR(-ENOMEM);
  161. return vaddr;
  162. }
  163. static int cma_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  164. {
  165. struct cma_heap_buffer *buffer = dmabuf->priv;
  166. void *vaddr;
  167. int ret = 0;
  168. mutex_lock(&buffer->lock);
  169. if (buffer->vmap_cnt) {
  170. buffer->vmap_cnt++;
  171. iosys_map_set_vaddr(map, buffer->vaddr);
  172. goto out;
  173. }
  174. vaddr = cma_heap_do_vmap(buffer);
  175. if (IS_ERR(vaddr)) {
  176. ret = PTR_ERR(vaddr);
  177. goto out;
  178. }
  179. buffer->vaddr = vaddr;
  180. buffer->vmap_cnt++;
  181. iosys_map_set_vaddr(map, buffer->vaddr);
  182. out:
  183. mutex_unlock(&buffer->lock);
  184. return ret;
  185. }
  186. static void cma_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
  187. {
  188. struct cma_heap_buffer *buffer = dmabuf->priv;
  189. mutex_lock(&buffer->lock);
  190. if (!--buffer->vmap_cnt) {
  191. vunmap(buffer->vaddr);
  192. buffer->vaddr = NULL;
  193. }
  194. mutex_unlock(&buffer->lock);
  195. iosys_map_clear(map);
  196. }
  197. static void cma_heap_dma_buf_release(struct dma_buf *dmabuf)
  198. {
  199. struct cma_heap_buffer *buffer = dmabuf->priv;
  200. struct cma_heap *cma_heap = buffer->heap;
  201. if (buffer->vmap_cnt > 0) {
  202. WARN(1, "%s: buffer still mapped in the kernel\n", __func__);
  203. vunmap(buffer->vaddr);
  204. buffer->vaddr = NULL;
  205. }
  206. /* free page list */
  207. kfree(buffer->pages);
  208. /* release memory */
  209. cma_release(cma_heap->cma, buffer->cma_pages, buffer->pagecount);
  210. kfree(buffer);
  211. }
  212. static const struct dma_buf_ops cma_heap_buf_ops = {
  213. .attach = cma_heap_attach,
  214. .detach = cma_heap_detach,
  215. .map_dma_buf = cma_heap_map_dma_buf,
  216. .unmap_dma_buf = cma_heap_unmap_dma_buf,
  217. .begin_cpu_access = cma_heap_dma_buf_begin_cpu_access,
  218. .end_cpu_access = cma_heap_dma_buf_end_cpu_access,
  219. .mmap = cma_heap_mmap,
  220. .vmap = cma_heap_vmap,
  221. .vunmap = cma_heap_vunmap,
  222. .release = cma_heap_dma_buf_release,
  223. };
  224. static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
  225. unsigned long len,
  226. u32 fd_flags,
  227. u64 heap_flags)
  228. {
  229. struct cma_heap *cma_heap = dma_heap_get_drvdata(heap);
  230. struct cma_heap_buffer *buffer;
  231. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  232. size_t size = PAGE_ALIGN(len);
  233. pgoff_t pagecount = size >> PAGE_SHIFT;
  234. unsigned long align = get_order(size);
  235. struct page *cma_pages;
  236. struct dma_buf *dmabuf;
  237. int ret = -ENOMEM;
  238. pgoff_t pg;
  239. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  240. if (!buffer)
  241. return ERR_PTR(-ENOMEM);
  242. INIT_LIST_HEAD(&buffer->attachments);
  243. mutex_init(&buffer->lock);
  244. buffer->len = size;
  245. if (align > CONFIG_CMA_ALIGNMENT)
  246. align = CONFIG_CMA_ALIGNMENT;
  247. cma_pages = cma_alloc(cma_heap->cma, pagecount, align, false);
  248. if (!cma_pages)
  249. goto free_buffer;
  250. /* Clear the cma pages */
  251. if (PageHighMem(cma_pages)) {
  252. unsigned long nr_clear_pages = pagecount;
  253. struct page *page = cma_pages;
  254. while (nr_clear_pages > 0) {
  255. void *vaddr = kmap_atomic(page);
  256. memset(vaddr, 0, PAGE_SIZE);
  257. kunmap_atomic(vaddr);
  258. /*
  259. * Avoid wasting time zeroing memory if the process
  260. * has been killed by by SIGKILL
  261. */
  262. if (fatal_signal_pending(current))
  263. goto free_cma;
  264. page++;
  265. nr_clear_pages--;
  266. }
  267. } else {
  268. memset(page_address(cma_pages), 0, size);
  269. }
  270. buffer->pages = kmalloc_array(pagecount, sizeof(*buffer->pages), GFP_KERNEL);
  271. if (!buffer->pages) {
  272. ret = -ENOMEM;
  273. goto free_cma;
  274. }
  275. for (pg = 0; pg < pagecount; pg++)
  276. buffer->pages[pg] = &cma_pages[pg];
  277. buffer->cma_pages = cma_pages;
  278. buffer->heap = cma_heap;
  279. buffer->pagecount = pagecount;
  280. /* create the dmabuf */
  281. exp_info.exp_name = dma_heap_get_name(heap);
  282. exp_info.ops = &cma_heap_buf_ops;
  283. exp_info.size = buffer->len;
  284. exp_info.flags = fd_flags;
  285. exp_info.priv = buffer;
  286. dmabuf = dma_buf_export(&exp_info);
  287. if (IS_ERR(dmabuf)) {
  288. ret = PTR_ERR(dmabuf);
  289. goto free_pages;
  290. }
  291. return dmabuf;
  292. free_pages:
  293. kfree(buffer->pages);
  294. free_cma:
  295. cma_release(cma_heap->cma, cma_pages, pagecount);
  296. free_buffer:
  297. kfree(buffer);
  298. return ERR_PTR(ret);
  299. }
  300. static const struct dma_heap_ops cma_heap_ops = {
  301. .allocate = cma_heap_allocate,
  302. };
  303. static int __add_cma_heap(struct cma *cma, void *data)
  304. {
  305. struct cma_heap *cma_heap;
  306. struct dma_heap_export_info exp_info;
  307. cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
  308. if (!cma_heap)
  309. return -ENOMEM;
  310. cma_heap->cma = cma;
  311. exp_info.name = cma_get_name(cma);
  312. exp_info.ops = &cma_heap_ops;
  313. exp_info.priv = cma_heap;
  314. cma_heap->heap = dma_heap_add(&exp_info);
  315. if (IS_ERR(cma_heap->heap)) {
  316. int ret = PTR_ERR(cma_heap->heap);
  317. kfree(cma_heap);
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. static int add_default_cma_heap(void)
  323. {
  324. struct cma *default_cma = dev_get_cma_area(NULL);
  325. int ret = 0;
  326. if (default_cma)
  327. ret = __add_cma_heap(default_cma, NULL);
  328. return ret;
  329. }
  330. module_init(add_default_cma_heap);
  331. MODULE_DESCRIPTION("DMA-BUF CMA Heap");