system_heap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 <[email protected]>
  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. #include "page_pool.h"
  23. static struct dma_heap *sys_heap;
  24. static struct dma_heap *sys_uncached_heap;
  25. struct system_heap_buffer {
  26. struct dma_heap *heap;
  27. struct list_head attachments;
  28. struct mutex lock;
  29. unsigned long len;
  30. struct sg_table sg_table;
  31. int vmap_cnt;
  32. void *vaddr;
  33. bool uncached;
  34. };
  35. struct dma_heap_attachment {
  36. struct device *dev;
  37. struct sg_table *table;
  38. struct list_head list;
  39. bool mapped;
  40. bool uncached;
  41. };
  42. #define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO | __GFP_COMP)
  43. #define MID_ORDER_GFP (LOW_ORDER_GFP | __GFP_NOWARN)
  44. #define HIGH_ORDER_GFP (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
  45. | __GFP_NORETRY) & ~__GFP_RECLAIM) \
  46. | __GFP_COMP)
  47. static gfp_t order_flags[] = {HIGH_ORDER_GFP, MID_ORDER_GFP, LOW_ORDER_GFP};
  48. /*
  49. * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
  50. * to match with the sizes often found in IOMMUs. Using order 4 pages instead
  51. * of order 0 pages can significantly improve the performance of many IOMMUs
  52. * by reducing TLB pressure and time spent updating page tables.
  53. */
  54. static const unsigned int orders[] = {8, 4, 0};
  55. #define NUM_ORDERS ARRAY_SIZE(orders)
  56. struct dmabuf_page_pool *pools[NUM_ORDERS];
  57. static struct sg_table *dup_sg_table(struct sg_table *table)
  58. {
  59. struct sg_table *new_table;
  60. int ret, i;
  61. struct scatterlist *sg, *new_sg;
  62. new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
  63. if (!new_table)
  64. return ERR_PTR(-ENOMEM);
  65. ret = sg_alloc_table(new_table, table->orig_nents, GFP_KERNEL);
  66. if (ret) {
  67. kfree(new_table);
  68. return ERR_PTR(-ENOMEM);
  69. }
  70. new_sg = new_table->sgl;
  71. for_each_sgtable_sg(table, sg, i) {
  72. sg_set_page(new_sg, sg_page(sg), sg->length, sg->offset);
  73. new_sg = sg_next(new_sg);
  74. }
  75. return new_table;
  76. }
  77. static int system_heap_attach(struct dma_buf *dmabuf,
  78. struct dma_buf_attachment *attachment)
  79. {
  80. struct system_heap_buffer *buffer = dmabuf->priv;
  81. struct dma_heap_attachment *a;
  82. struct sg_table *table;
  83. a = kzalloc(sizeof(*a), GFP_KERNEL);
  84. if (!a)
  85. return -ENOMEM;
  86. table = dup_sg_table(&buffer->sg_table);
  87. if (IS_ERR(table)) {
  88. kfree(a);
  89. return -ENOMEM;
  90. }
  91. a->table = table;
  92. a->dev = attachment->dev;
  93. INIT_LIST_HEAD(&a->list);
  94. a->mapped = false;
  95. a->uncached = buffer->uncached;
  96. attachment->priv = a;
  97. mutex_lock(&buffer->lock);
  98. list_add(&a->list, &buffer->attachments);
  99. mutex_unlock(&buffer->lock);
  100. return 0;
  101. }
  102. static void system_heap_detach(struct dma_buf *dmabuf,
  103. struct dma_buf_attachment *attachment)
  104. {
  105. struct system_heap_buffer *buffer = dmabuf->priv;
  106. struct dma_heap_attachment *a = attachment->priv;
  107. mutex_lock(&buffer->lock);
  108. list_del(&a->list);
  109. mutex_unlock(&buffer->lock);
  110. sg_free_table(a->table);
  111. kfree(a->table);
  112. kfree(a);
  113. }
  114. static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
  115. enum dma_data_direction direction)
  116. {
  117. struct dma_heap_attachment *a = attachment->priv;
  118. struct sg_table *table = a->table;
  119. int attr = attachment->dma_map_attrs;
  120. int ret;
  121. if (a->uncached)
  122. attr |= DMA_ATTR_SKIP_CPU_SYNC;
  123. ret = dma_map_sgtable(attachment->dev, table, direction, attr);
  124. if (ret)
  125. return ERR_PTR(ret);
  126. a->mapped = true;
  127. return table;
  128. }
  129. static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
  130. struct sg_table *table,
  131. enum dma_data_direction direction)
  132. {
  133. struct dma_heap_attachment *a = attachment->priv;
  134. int attr = attachment->dma_map_attrs;
  135. if (a->uncached)
  136. attr |= DMA_ATTR_SKIP_CPU_SYNC;
  137. a->mapped = false;
  138. dma_unmap_sgtable(attachment->dev, table, direction, attr);
  139. }
  140. static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  141. enum dma_data_direction direction)
  142. {
  143. struct system_heap_buffer *buffer = dmabuf->priv;
  144. struct dma_heap_attachment *a;
  145. mutex_lock(&buffer->lock);
  146. if (buffer->vmap_cnt)
  147. invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
  148. if (!buffer->uncached) {
  149. list_for_each_entry(a, &buffer->attachments, list) {
  150. if (!a->mapped)
  151. continue;
  152. dma_sync_sgtable_for_cpu(a->dev, a->table, direction);
  153. }
  154. }
  155. mutex_unlock(&buffer->lock);
  156. return 0;
  157. }
  158. static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
  159. enum dma_data_direction direction)
  160. {
  161. struct system_heap_buffer *buffer = dmabuf->priv;
  162. struct dma_heap_attachment *a;
  163. mutex_lock(&buffer->lock);
  164. if (buffer->vmap_cnt)
  165. flush_kernel_vmap_range(buffer->vaddr, buffer->len);
  166. if (!buffer->uncached) {
  167. list_for_each_entry(a, &buffer->attachments, list) {
  168. if (!a->mapped)
  169. continue;
  170. dma_sync_sgtable_for_device(a->dev, a->table, direction);
  171. }
  172. }
  173. mutex_unlock(&buffer->lock);
  174. return 0;
  175. }
  176. static int system_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
  177. {
  178. struct system_heap_buffer *buffer = dmabuf->priv;
  179. struct sg_table *table = &buffer->sg_table;
  180. unsigned long addr = vma->vm_start;
  181. struct sg_page_iter piter;
  182. int ret;
  183. if (buffer->uncached)
  184. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  185. for_each_sgtable_page(table, &piter, vma->vm_pgoff) {
  186. struct page *page = sg_page_iter_page(&piter);
  187. ret = remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE,
  188. vma->vm_page_prot);
  189. if (ret)
  190. return ret;
  191. addr += PAGE_SIZE;
  192. if (addr >= vma->vm_end)
  193. return 0;
  194. }
  195. return 0;
  196. }
  197. static void *system_heap_do_vmap(struct system_heap_buffer *buffer)
  198. {
  199. struct sg_table *table = &buffer->sg_table;
  200. int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
  201. struct page **pages = vmalloc(sizeof(struct page *) * npages);
  202. struct page **tmp = pages;
  203. struct sg_page_iter piter;
  204. pgprot_t pgprot = PAGE_KERNEL;
  205. void *vaddr;
  206. if (!pages)
  207. return ERR_PTR(-ENOMEM);
  208. if (buffer->uncached)
  209. pgprot = pgprot_writecombine(PAGE_KERNEL);
  210. for_each_sgtable_page(table, &piter, 0) {
  211. WARN_ON(tmp - pages >= npages);
  212. *tmp++ = sg_page_iter_page(&piter);
  213. }
  214. vaddr = vmap(pages, npages, VM_MAP, pgprot);
  215. vfree(pages);
  216. if (!vaddr)
  217. return ERR_PTR(-ENOMEM);
  218. return vaddr;
  219. }
  220. static int system_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  221. {
  222. struct system_heap_buffer *buffer = dmabuf->priv;
  223. void *vaddr;
  224. int ret = 0;
  225. mutex_lock(&buffer->lock);
  226. if (buffer->vmap_cnt) {
  227. buffer->vmap_cnt++;
  228. iosys_map_set_vaddr(map, buffer->vaddr);
  229. goto out;
  230. }
  231. vaddr = system_heap_do_vmap(buffer);
  232. if (IS_ERR(vaddr)) {
  233. ret = PTR_ERR(vaddr);
  234. goto out;
  235. }
  236. buffer->vaddr = vaddr;
  237. buffer->vmap_cnt++;
  238. iosys_map_set_vaddr(map, buffer->vaddr);
  239. out:
  240. mutex_unlock(&buffer->lock);
  241. return ret;
  242. }
  243. static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
  244. {
  245. struct system_heap_buffer *buffer = dmabuf->priv;
  246. mutex_lock(&buffer->lock);
  247. if (!--buffer->vmap_cnt) {
  248. vunmap(buffer->vaddr);
  249. buffer->vaddr = NULL;
  250. }
  251. mutex_unlock(&buffer->lock);
  252. iosys_map_clear(map);
  253. }
  254. static int system_heap_zero_buffer(struct system_heap_buffer *buffer)
  255. {
  256. struct sg_table *sgt = &buffer->sg_table;
  257. struct sg_page_iter piter;
  258. struct page *p;
  259. void *vaddr;
  260. int ret = 0;
  261. for_each_sgtable_page(sgt, &piter, 0) {
  262. p = sg_page_iter_page(&piter);
  263. vaddr = kmap_local_page(p);
  264. memset(vaddr, 0, PAGE_SIZE);
  265. kunmap_local(vaddr);
  266. }
  267. return ret;
  268. }
  269. static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
  270. {
  271. struct system_heap_buffer *buffer = dmabuf->priv;
  272. struct sg_table *table;
  273. struct scatterlist *sg;
  274. int i, j;
  275. /* Zero the buffer pages before adding back to the pool */
  276. system_heap_zero_buffer(buffer);
  277. table = &buffer->sg_table;
  278. for_each_sgtable_sg(table, sg, i) {
  279. struct page *page = sg_page(sg);
  280. for (j = 0; j < NUM_ORDERS; j++) {
  281. if (compound_order(page) == orders[j])
  282. break;
  283. }
  284. dmabuf_page_pool_free(pools[j], page);
  285. }
  286. sg_free_table(table);
  287. kfree(buffer);
  288. }
  289. static const struct dma_buf_ops system_heap_buf_ops = {
  290. .attach = system_heap_attach,
  291. .detach = system_heap_detach,
  292. .map_dma_buf = system_heap_map_dma_buf,
  293. .unmap_dma_buf = system_heap_unmap_dma_buf,
  294. .begin_cpu_access = system_heap_dma_buf_begin_cpu_access,
  295. .end_cpu_access = system_heap_dma_buf_end_cpu_access,
  296. .mmap = system_heap_mmap,
  297. .vmap = system_heap_vmap,
  298. .vunmap = system_heap_vunmap,
  299. .release = system_heap_dma_buf_release,
  300. };
  301. static struct page *alloc_largest_available(unsigned long size,
  302. unsigned int max_order)
  303. {
  304. struct page *page;
  305. int i;
  306. for (i = 0; i < NUM_ORDERS; i++) {
  307. if (size < (PAGE_SIZE << orders[i]))
  308. continue;
  309. if (max_order < orders[i])
  310. continue;
  311. page = dmabuf_page_pool_alloc(pools[i]);
  312. if (!page)
  313. continue;
  314. return page;
  315. }
  316. return NULL;
  317. }
  318. static struct dma_buf *system_heap_do_allocate(struct dma_heap *heap,
  319. unsigned long len,
  320. unsigned long fd_flags,
  321. unsigned long heap_flags,
  322. bool uncached)
  323. {
  324. struct system_heap_buffer *buffer;
  325. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  326. unsigned long size_remaining = len;
  327. unsigned int max_order = orders[0];
  328. struct dma_buf *dmabuf;
  329. struct sg_table *table;
  330. struct scatterlist *sg;
  331. struct list_head pages;
  332. struct page *page, *tmp_page;
  333. int i, ret = -ENOMEM;
  334. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  335. if (!buffer)
  336. return ERR_PTR(-ENOMEM);
  337. INIT_LIST_HEAD(&buffer->attachments);
  338. mutex_init(&buffer->lock);
  339. buffer->heap = heap;
  340. buffer->len = len;
  341. buffer->uncached = uncached;
  342. INIT_LIST_HEAD(&pages);
  343. i = 0;
  344. while (size_remaining > 0) {
  345. /*
  346. * Avoid trying to allocate memory if the process
  347. * has been killed by SIGKILL
  348. */
  349. if (fatal_signal_pending(current)) {
  350. ret = -EINTR;
  351. goto free_buffer;
  352. }
  353. page = alloc_largest_available(size_remaining, max_order);
  354. if (!page)
  355. goto free_buffer;
  356. list_add_tail(&page->lru, &pages);
  357. size_remaining -= page_size(page);
  358. max_order = compound_order(page);
  359. i++;
  360. }
  361. table = &buffer->sg_table;
  362. if (sg_alloc_table(table, i, GFP_KERNEL))
  363. goto free_buffer;
  364. sg = table->sgl;
  365. list_for_each_entry_safe(page, tmp_page, &pages, lru) {
  366. sg_set_page(sg, page, page_size(page), 0);
  367. sg = sg_next(sg);
  368. list_del(&page->lru);
  369. }
  370. /* create the dmabuf */
  371. exp_info.exp_name = dma_heap_get_name(heap);
  372. exp_info.ops = &system_heap_buf_ops;
  373. exp_info.size = buffer->len;
  374. exp_info.flags = fd_flags;
  375. exp_info.priv = buffer;
  376. dmabuf = dma_buf_export(&exp_info);
  377. if (IS_ERR(dmabuf)) {
  378. ret = PTR_ERR(dmabuf);
  379. goto free_pages;
  380. }
  381. /*
  382. * For uncached buffers, we need to initially flush cpu cache, since
  383. * the __GFP_ZERO on the allocation means the zeroing was done by the
  384. * cpu and thus it is likely cached. Map (and implicitly flush) and
  385. * unmap it now so we don't get corruption later on.
  386. */
  387. if (buffer->uncached) {
  388. dma_map_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
  389. dma_unmap_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
  390. }
  391. return dmabuf;
  392. free_pages:
  393. for_each_sgtable_sg(table, sg, i) {
  394. struct page *p = sg_page(sg);
  395. __free_pages(p, compound_order(p));
  396. }
  397. sg_free_table(table);
  398. free_buffer:
  399. list_for_each_entry_safe(page, tmp_page, &pages, lru)
  400. __free_pages(page, compound_order(page));
  401. kfree(buffer);
  402. return ERR_PTR(ret);
  403. }
  404. static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
  405. unsigned long len,
  406. unsigned long fd_flags,
  407. unsigned long heap_flags)
  408. {
  409. return system_heap_do_allocate(heap, len, fd_flags, heap_flags, false);
  410. }
  411. static long system_get_pool_size(struct dma_heap *heap)
  412. {
  413. unsigned long num_bytes = 0;
  414. struct dmabuf_page_pool **pool = pools;
  415. for (int i = 0; i < NUM_ORDERS; i++, pool++)
  416. num_bytes += dmabuf_page_pool_get_size(*pool);
  417. return num_bytes;
  418. }
  419. static const struct dma_heap_ops system_heap_ops = {
  420. .allocate = system_heap_allocate,
  421. .get_pool_size = system_get_pool_size,
  422. };
  423. static struct dma_buf *system_uncached_heap_allocate(struct dma_heap *heap,
  424. unsigned long len,
  425. unsigned long fd_flags,
  426. unsigned long heap_flags)
  427. {
  428. return system_heap_do_allocate(heap, len, fd_flags, heap_flags, true);
  429. }
  430. /* Dummy function to be used until we can call coerce_mask_and_coherent */
  431. static struct dma_buf *system_uncached_heap_not_initialized(struct dma_heap *heap,
  432. unsigned long len,
  433. unsigned long fd_flags,
  434. unsigned long heap_flags)
  435. {
  436. return ERR_PTR(-EBUSY);
  437. }
  438. static struct dma_heap_ops system_uncached_heap_ops = {
  439. /* After system_heap_create is complete, we will swap this */
  440. .allocate = system_uncached_heap_not_initialized,
  441. };
  442. static int system_heap_create(void)
  443. {
  444. struct dma_heap_export_info exp_info;
  445. int i;
  446. for (i = 0; i < NUM_ORDERS; i++) {
  447. pools[i] = dmabuf_page_pool_create(order_flags[i], orders[i]);
  448. if (IS_ERR(pools[i])) {
  449. int j;
  450. pr_err("%s: page pool creation failed!\n", __func__);
  451. for (j = 0; j < i; j++)
  452. dmabuf_page_pool_destroy(pools[j]);
  453. return PTR_ERR(pools[i]);
  454. }
  455. }
  456. exp_info.name = "system";
  457. exp_info.ops = &system_heap_ops;
  458. exp_info.priv = NULL;
  459. sys_heap = dma_heap_add(&exp_info);
  460. if (IS_ERR(sys_heap))
  461. return PTR_ERR(sys_heap);
  462. exp_info.name = "system-uncached";
  463. exp_info.ops = &system_uncached_heap_ops;
  464. exp_info.priv = NULL;
  465. sys_uncached_heap = dma_heap_add(&exp_info);
  466. if (IS_ERR(sys_uncached_heap))
  467. return PTR_ERR(sys_uncached_heap);
  468. dma_coerce_mask_and_coherent(dma_heap_get_dev(sys_uncached_heap), DMA_BIT_MASK(64));
  469. mb(); /* make sure we only set allocate after dma_mask is set */
  470. system_uncached_heap_ops.allocate = system_uncached_heap_allocate;
  471. return 0;
  472. }
  473. module_init(system_heap_create);
  474. MODULE_LICENSE("GPL v2");
  475. MODULE_IMPORT_NS(DMA_BUF);