system_heap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMABUF System heap exporter
  4. *
  5. * Copyright (C) 2011 Google, Inc.
  6. * Copyright (C) 2019, 2020 Linaro Ltd.
  7. *
  8. * Portions based off 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/dma-buf.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dma-heap.h>
  15. #include <linux/err.h>
  16. #include <linux/highmem.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. static struct dma_heap *sys_heap;
  23. struct system_heap_buffer {
  24. struct dma_heap *heap;
  25. struct list_head attachments;
  26. struct mutex lock;
  27. unsigned long len;
  28. struct sg_table sg_table;
  29. int vmap_cnt;
  30. void *vaddr;
  31. };
  32. struct dma_heap_attachment {
  33. struct device *dev;
  34. struct sg_table *table;
  35. struct list_head list;
  36. bool mapped;
  37. };
  38. #define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO)
  39. #define HIGH_ORDER_GFP (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
  40. | __GFP_NORETRY) & ~__GFP_RECLAIM) \
  41. | __GFP_COMP)
  42. static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
  43. /*
  44. * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
  45. * to match with the sizes often found in IOMMUs. Using order 4 pages instead
  46. * of order 0 pages can significantly improve the performance of many IOMMUs
  47. * by reducing TLB pressure and time spent updating page tables.
  48. */
  49. static const unsigned int orders[] = {8, 4, 0};
  50. #define NUM_ORDERS ARRAY_SIZE(orders)
  51. static struct sg_table *dup_sg_table(struct sg_table *table)
  52. {
  53. struct sg_table *new_table;
  54. int ret, i;
  55. struct scatterlist *sg, *new_sg;
  56. new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
  57. if (!new_table)
  58. return ERR_PTR(-ENOMEM);
  59. ret = sg_alloc_table(new_table, table->orig_nents, GFP_KERNEL);
  60. if (ret) {
  61. kfree(new_table);
  62. return ERR_PTR(-ENOMEM);
  63. }
  64. new_sg = new_table->sgl;
  65. for_each_sgtable_sg(table, sg, i) {
  66. sg_set_page(new_sg, sg_page(sg), sg->length, sg->offset);
  67. new_sg = sg_next(new_sg);
  68. }
  69. return new_table;
  70. }
  71. static int system_heap_attach(struct dma_buf *dmabuf,
  72. struct dma_buf_attachment *attachment)
  73. {
  74. struct system_heap_buffer *buffer = dmabuf->priv;
  75. struct dma_heap_attachment *a;
  76. struct sg_table *table;
  77. a = kzalloc(sizeof(*a), GFP_KERNEL);
  78. if (!a)
  79. return -ENOMEM;
  80. table = dup_sg_table(&buffer->sg_table);
  81. if (IS_ERR(table)) {
  82. kfree(a);
  83. return -ENOMEM;
  84. }
  85. a->table = table;
  86. a->dev = attachment->dev;
  87. INIT_LIST_HEAD(&a->list);
  88. a->mapped = false;
  89. attachment->priv = a;
  90. mutex_lock(&buffer->lock);
  91. list_add(&a->list, &buffer->attachments);
  92. mutex_unlock(&buffer->lock);
  93. return 0;
  94. }
  95. static void system_heap_detach(struct dma_buf *dmabuf,
  96. struct dma_buf_attachment *attachment)
  97. {
  98. struct system_heap_buffer *buffer = dmabuf->priv;
  99. struct dma_heap_attachment *a = attachment->priv;
  100. mutex_lock(&buffer->lock);
  101. list_del(&a->list);
  102. mutex_unlock(&buffer->lock);
  103. sg_free_table(a->table);
  104. kfree(a->table);
  105. kfree(a);
  106. }
  107. static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
  108. enum dma_data_direction direction)
  109. {
  110. struct dma_heap_attachment *a = attachment->priv;
  111. struct sg_table *table = a->table;
  112. int ret;
  113. ret = dma_map_sgtable(attachment->dev, table, direction, 0);
  114. if (ret)
  115. return ERR_PTR(ret);
  116. a->mapped = true;
  117. return table;
  118. }
  119. static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
  120. struct sg_table *table,
  121. enum dma_data_direction direction)
  122. {
  123. struct dma_heap_attachment *a = attachment->priv;
  124. a->mapped = false;
  125. dma_unmap_sgtable(attachment->dev, table, direction, 0);
  126. }
  127. static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  128. enum dma_data_direction direction)
  129. {
  130. struct system_heap_buffer *buffer = dmabuf->priv;
  131. struct dma_heap_attachment *a;
  132. mutex_lock(&buffer->lock);
  133. if (buffer->vmap_cnt)
  134. invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
  135. list_for_each_entry(a, &buffer->attachments, list) {
  136. if (!a->mapped)
  137. continue;
  138. dma_sync_sgtable_for_cpu(a->dev, a->table, direction);
  139. }
  140. mutex_unlock(&buffer->lock);
  141. return 0;
  142. }
  143. static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
  144. enum dma_data_direction direction)
  145. {
  146. struct system_heap_buffer *buffer = dmabuf->priv;
  147. struct dma_heap_attachment *a;
  148. mutex_lock(&buffer->lock);
  149. if (buffer->vmap_cnt)
  150. flush_kernel_vmap_range(buffer->vaddr, buffer->len);
  151. list_for_each_entry(a, &buffer->attachments, list) {
  152. if (!a->mapped)
  153. continue;
  154. dma_sync_sgtable_for_device(a->dev, a->table, direction);
  155. }
  156. mutex_unlock(&buffer->lock);
  157. return 0;
  158. }
  159. static int system_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
  160. {
  161. struct system_heap_buffer *buffer = dmabuf->priv;
  162. struct sg_table *table = &buffer->sg_table;
  163. unsigned long addr = vma->vm_start;
  164. struct sg_page_iter piter;
  165. int ret;
  166. for_each_sgtable_page(table, &piter, vma->vm_pgoff) {
  167. struct page *page = sg_page_iter_page(&piter);
  168. ret = remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE,
  169. vma->vm_page_prot);
  170. if (ret)
  171. return ret;
  172. addr += PAGE_SIZE;
  173. if (addr >= vma->vm_end)
  174. return 0;
  175. }
  176. return 0;
  177. }
  178. static void *system_heap_do_vmap(struct system_heap_buffer *buffer)
  179. {
  180. struct sg_table *table = &buffer->sg_table;
  181. int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
  182. struct page **pages = vmalloc(sizeof(struct page *) * npages);
  183. struct page **tmp = pages;
  184. struct sg_page_iter piter;
  185. void *vaddr;
  186. if (!pages)
  187. return ERR_PTR(-ENOMEM);
  188. for_each_sgtable_page(table, &piter, 0) {
  189. WARN_ON(tmp - pages >= npages);
  190. *tmp++ = sg_page_iter_page(&piter);
  191. }
  192. vaddr = vmap(pages, npages, VM_MAP, PAGE_KERNEL);
  193. vfree(pages);
  194. if (!vaddr)
  195. return ERR_PTR(-ENOMEM);
  196. return vaddr;
  197. }
  198. static int system_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  199. {
  200. struct system_heap_buffer *buffer = dmabuf->priv;
  201. void *vaddr;
  202. int ret = 0;
  203. mutex_lock(&buffer->lock);
  204. if (buffer->vmap_cnt) {
  205. buffer->vmap_cnt++;
  206. iosys_map_set_vaddr(map, buffer->vaddr);
  207. goto out;
  208. }
  209. vaddr = system_heap_do_vmap(buffer);
  210. if (IS_ERR(vaddr)) {
  211. ret = PTR_ERR(vaddr);
  212. goto out;
  213. }
  214. buffer->vaddr = vaddr;
  215. buffer->vmap_cnt++;
  216. iosys_map_set_vaddr(map, buffer->vaddr);
  217. out:
  218. mutex_unlock(&buffer->lock);
  219. return ret;
  220. }
  221. static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
  222. {
  223. struct system_heap_buffer *buffer = dmabuf->priv;
  224. mutex_lock(&buffer->lock);
  225. if (!--buffer->vmap_cnt) {
  226. vunmap(buffer->vaddr);
  227. buffer->vaddr = NULL;
  228. }
  229. mutex_unlock(&buffer->lock);
  230. iosys_map_clear(map);
  231. }
  232. static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
  233. {
  234. struct system_heap_buffer *buffer = dmabuf->priv;
  235. struct sg_table *table;
  236. struct scatterlist *sg;
  237. int i;
  238. table = &buffer->sg_table;
  239. for_each_sgtable_sg(table, sg, i) {
  240. struct page *page = sg_page(sg);
  241. __free_pages(page, compound_order(page));
  242. }
  243. sg_free_table(table);
  244. kfree(buffer);
  245. }
  246. static const struct dma_buf_ops system_heap_buf_ops = {
  247. .attach = system_heap_attach,
  248. .detach = system_heap_detach,
  249. .map_dma_buf = system_heap_map_dma_buf,
  250. .unmap_dma_buf = system_heap_unmap_dma_buf,
  251. .begin_cpu_access = system_heap_dma_buf_begin_cpu_access,
  252. .end_cpu_access = system_heap_dma_buf_end_cpu_access,
  253. .mmap = system_heap_mmap,
  254. .vmap = system_heap_vmap,
  255. .vunmap = system_heap_vunmap,
  256. .release = system_heap_dma_buf_release,
  257. };
  258. static struct page *alloc_largest_available(unsigned long size,
  259. unsigned int max_order)
  260. {
  261. struct page *page;
  262. int i;
  263. for (i = 0; i < NUM_ORDERS; i++) {
  264. if (size < (PAGE_SIZE << orders[i]))
  265. continue;
  266. if (max_order < orders[i])
  267. continue;
  268. page = alloc_pages(order_flags[i], orders[i]);
  269. if (!page)
  270. continue;
  271. return page;
  272. }
  273. return NULL;
  274. }
  275. static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
  276. unsigned long len,
  277. u32 fd_flags,
  278. u64 heap_flags)
  279. {
  280. struct system_heap_buffer *buffer;
  281. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  282. unsigned long size_remaining = len;
  283. unsigned int max_order = orders[0];
  284. struct dma_buf *dmabuf;
  285. struct sg_table *table;
  286. struct scatterlist *sg;
  287. struct list_head pages;
  288. struct page *page, *tmp_page;
  289. int i, ret = -ENOMEM;
  290. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  291. if (!buffer)
  292. return ERR_PTR(-ENOMEM);
  293. INIT_LIST_HEAD(&buffer->attachments);
  294. mutex_init(&buffer->lock);
  295. buffer->heap = heap;
  296. buffer->len = len;
  297. INIT_LIST_HEAD(&pages);
  298. i = 0;
  299. while (size_remaining > 0) {
  300. /*
  301. * Avoid trying to allocate memory if the process
  302. * has been killed by SIGKILL
  303. */
  304. if (fatal_signal_pending(current)) {
  305. ret = -EINTR;
  306. goto free_buffer;
  307. }
  308. page = alloc_largest_available(size_remaining, max_order);
  309. if (!page)
  310. goto free_buffer;
  311. list_add_tail(&page->lru, &pages);
  312. size_remaining -= page_size(page);
  313. max_order = compound_order(page);
  314. i++;
  315. }
  316. table = &buffer->sg_table;
  317. if (sg_alloc_table(table, i, GFP_KERNEL))
  318. goto free_buffer;
  319. sg = table->sgl;
  320. list_for_each_entry_safe(page, tmp_page, &pages, lru) {
  321. sg_set_page(sg, page, page_size(page), 0);
  322. sg = sg_next(sg);
  323. list_del(&page->lru);
  324. }
  325. /* create the dmabuf */
  326. exp_info.exp_name = dma_heap_get_name(heap);
  327. exp_info.ops = &system_heap_buf_ops;
  328. exp_info.size = buffer->len;
  329. exp_info.flags = fd_flags;
  330. exp_info.priv = buffer;
  331. dmabuf = dma_buf_export(&exp_info);
  332. if (IS_ERR(dmabuf)) {
  333. ret = PTR_ERR(dmabuf);
  334. goto free_pages;
  335. }
  336. return dmabuf;
  337. free_pages:
  338. for_each_sgtable_sg(table, sg, i) {
  339. struct page *p = sg_page(sg);
  340. __free_pages(p, compound_order(p));
  341. }
  342. sg_free_table(table);
  343. free_buffer:
  344. list_for_each_entry_safe(page, tmp_page, &pages, lru)
  345. __free_pages(page, compound_order(page));
  346. kfree(buffer);
  347. return ERR_PTR(ret);
  348. }
  349. static const struct dma_heap_ops system_heap_ops = {
  350. .allocate = system_heap_allocate,
  351. };
  352. static int system_heap_create(void)
  353. {
  354. struct dma_heap_export_info exp_info;
  355. exp_info.name = "system";
  356. exp_info.ops = &system_heap_ops;
  357. exp_info.priv = NULL;
  358. sys_heap = dma_heap_add(&exp_info);
  359. if (IS_ERR(sys_heap))
  360. return PTR_ERR(sys_heap);
  361. return 0;
  362. }
  363. module_init(system_heap_create);