qcom-dma-iommu-generic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, 2020-2021, The Linux Foundation. All rights reserved.
  4. * Contiguous Memory Allocator for DMA mapping framework
  5. * Copyright (c) 2010-2011 by Samsung Electronics.
  6. * Written by:
  7. * Marek Szyprowski <[email protected]>
  8. * Michal Nazarewicz <[email protected]>
  9. * Copyright (C) 2012, 2014-2015 ARM Ltd.
  10. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mutex.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/genalloc.h>
  19. #include <linux/dma-direct.h>
  20. #include <linux/cma.h>
  21. #include <linux/iova.h>
  22. #include <linux/dma-map-ops.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/qcom-dma-mapping.h>
  25. #include <linux/of_reserved_mem.h>
  26. #include <linux/iommu.h>
  27. #include <linux/qcom-iommu-util.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/debugfs.h>
  30. #include "qcom-dma-iommu-generic.h"
  31. static bool probe_finished;
  32. static struct device *qcom_dma_iommu_dev;
  33. static struct cma *qcom_dma_contiguous_default_area;
  34. struct pci_host_bridge *qcom_pci_find_host_bridge(struct pci_bus *bus)
  35. {
  36. while (bus->parent)
  37. bus = bus->parent;
  38. return to_pci_host_bridge(bus->bridge);
  39. }
  40. /*
  41. * This avoids arch-specific assembly, but may be slower since it calls
  42. * back into the dma layer again.
  43. */
  44. void qcom_arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  45. enum dma_data_direction dir)
  46. {
  47. dma_addr_t dma_addr = phys_to_dma(qcom_dma_iommu_dev, paddr);
  48. dma_sync_single_for_device(qcom_dma_iommu_dev,
  49. dma_addr, size, dir);
  50. }
  51. void qcom_arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  52. enum dma_data_direction dir)
  53. {
  54. dma_addr_t dma_addr = phys_to_dma(qcom_dma_iommu_dev, paddr);
  55. dma_sync_single_for_cpu(qcom_dma_iommu_dev,
  56. dma_addr, size, dir);
  57. }
  58. void qcom_arch_dma_prep_coherent(struct page *page, size_t size)
  59. {
  60. phys_addr_t phys = page_to_phys(page);
  61. dma_addr_t dma_addr = phys_to_dma(qcom_dma_iommu_dev, phys);
  62. dma_sync_single_for_device(qcom_dma_iommu_dev,
  63. dma_addr, size, DMA_TO_DEVICE);
  64. }
  65. static struct cma *qcom_dev_get_cma_area(struct device *dev)
  66. {
  67. if (dev && dev->cma_area)
  68. return dev->cma_area;
  69. return qcom_dma_contiguous_default_area;
  70. }
  71. struct page *qcom_dma_alloc_from_contiguous(struct device *dev, size_t count,
  72. unsigned int align, bool no_warn)
  73. {
  74. if (align > CONFIG_CMA_ALIGNMENT)
  75. align = CONFIG_CMA_ALIGNMENT;
  76. return cma_alloc(qcom_dev_get_cma_area(dev), count, align, no_warn);
  77. }
  78. bool qcom_dma_release_from_contiguous(struct device *dev, struct page *pages,
  79. int count)
  80. {
  81. return cma_release(qcom_dev_get_cma_area(dev), pages, count);
  82. }
  83. static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
  84. {
  85. unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);
  86. return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN);
  87. }
  88. struct page *qcom_dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
  89. {
  90. /* CMA can be used only in the context which permits sleeping */
  91. if (!gfpflags_allow_blocking(gfp))
  92. return NULL;
  93. if (dev->cma_area)
  94. return cma_alloc_aligned(dev->cma_area, size, gfp);
  95. if (size <= PAGE_SIZE || !qcom_dma_contiguous_default_area)
  96. return NULL;
  97. return cma_alloc_aligned(qcom_dma_contiguous_default_area, size, gfp);
  98. }
  99. void qcom_dma_free_contiguous(struct device *dev, struct page *page, size_t size)
  100. {
  101. if (!cma_release(qcom_dev_get_cma_area(dev), page,
  102. PAGE_ALIGN(size) >> PAGE_SHIFT))
  103. __free_pages(page, get_order(size));
  104. }
  105. /*
  106. * find_vm_area is not exported. Some dma apis expect that an array of
  107. * struct pages can be saved in the vm_area, and retrieved at a later time.
  108. */
  109. struct rb_root _root;
  110. struct rb_root *root = &_root;
  111. DEFINE_MUTEX(rbtree_lock);
  112. struct qcom_iommu_dma_area {
  113. struct rb_node node;
  114. unsigned long addr;
  115. struct page **pages;
  116. };
  117. static void qcom_insert_vm_area(struct qcom_iommu_dma_area *area)
  118. {
  119. struct rb_node **new, *parent;
  120. mutex_lock(&rbtree_lock);
  121. parent = NULL;
  122. new = &root->rb_node;
  123. while (*new) {
  124. struct qcom_iommu_dma_area *entry;
  125. entry = rb_entry(*new,
  126. struct qcom_iommu_dma_area,
  127. node);
  128. parent = *new;
  129. if (area->addr < entry->addr)
  130. new = &((*new)->rb_left);
  131. else if (area->addr > entry->addr)
  132. new = &((*new)->rb_right);
  133. else {
  134. mutex_unlock(&rbtree_lock);
  135. WARN_ON(1);
  136. return;
  137. }
  138. }
  139. rb_link_node(&area->node, parent, new);
  140. rb_insert_color(&area->node, root);
  141. mutex_unlock(&rbtree_lock);
  142. }
  143. static struct qcom_iommu_dma_area *qcom_find_vm_area(const void *cpu_addr)
  144. {
  145. struct rb_node *node;
  146. struct qcom_iommu_dma_area *entry;
  147. unsigned long addr = (unsigned long)cpu_addr;
  148. mutex_lock(&rbtree_lock);
  149. node = root->rb_node;
  150. while (node) {
  151. entry = rb_entry(node,
  152. struct qcom_iommu_dma_area,
  153. node);
  154. if (addr < entry->addr)
  155. node = node->rb_left;
  156. else if (addr > entry->addr)
  157. node = node->rb_right;
  158. else {
  159. mutex_unlock(&rbtree_lock);
  160. return entry;
  161. }
  162. }
  163. mutex_unlock(&rbtree_lock);
  164. return NULL;
  165. }
  166. struct page **qcom_dma_common_find_pages(void *cpu_addr)
  167. {
  168. struct qcom_iommu_dma_area *area = qcom_find_vm_area(cpu_addr);
  169. if (!area)
  170. return NULL;
  171. return area->pages;
  172. }
  173. /*
  174. * Remaps an array of PAGE_SIZE pages into another vm_area.
  175. * Cannot be used in non-sleeping contexts
  176. */
  177. void *qcom_dma_common_pages_remap(struct page **pages, size_t size,
  178. pgprot_t prot, const void *caller)
  179. {
  180. struct qcom_iommu_dma_area *area;
  181. void *vaddr;
  182. area = kzalloc(sizeof(*area), GFP_KERNEL);
  183. if (!area)
  184. return NULL;
  185. vaddr = vmap(pages, PAGE_ALIGN(size) >> PAGE_SHIFT,
  186. VM_DMA_COHERENT, prot);
  187. if (!vaddr) {
  188. kfree(area);
  189. return NULL;
  190. }
  191. area->pages = pages;
  192. area->addr = (unsigned long)vaddr;
  193. qcom_insert_vm_area(area);
  194. return vaddr;
  195. }
  196. /*
  197. * Remaps an allocated contiguous region into another vm_area.
  198. * Cannot be used in non-sleeping contexts
  199. */
  200. void *qcom_dma_common_contiguous_remap(struct page *page, size_t size,
  201. pgprot_t prot, const void *caller)
  202. {
  203. int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  204. struct page **pages;
  205. void *vaddr;
  206. int i;
  207. pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
  208. if (!pages)
  209. return NULL;
  210. for (i = 0; i < count; i++)
  211. pages[i] = nth_page(page, i);
  212. vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
  213. kfree(pages);
  214. return vaddr;
  215. }
  216. /*
  217. * Unmaps a range previously mapped by dma_common_contiguous_remap or
  218. * dma_common_pages_remap. Note that dma_common_contiguous_remap does
  219. * not insert an rb_tree entry since there is no pages array to save.
  220. */
  221. void qcom_dma_common_free_remap(void *cpu_addr, size_t size)
  222. {
  223. struct qcom_iommu_dma_area *area;
  224. /* qcom_dma_common_contiguous_remap doesn't save the pages array */
  225. area = qcom_find_vm_area(cpu_addr);
  226. if (area) {
  227. mutex_lock(&rbtree_lock);
  228. rb_erase(&area->node, root);
  229. mutex_unlock(&rbtree_lock);
  230. kfree(area);
  231. }
  232. vunmap(cpu_addr);
  233. }
  234. static struct gen_pool *atomic_pool __ro_after_init;
  235. static size_t atomic_pool_size;
  236. static unsigned long current_pool_size;
  237. /* Dynamic background expansion when the atomic pool is near capacity */
  238. static struct work_struct atomic_pool_work;
  239. static void dma_atomic_pool_debugfs_init(void)
  240. {
  241. struct dentry *root;
  242. root = debugfs_create_dir("qcom_dma_pools", NULL);
  243. if (IS_ERR_OR_NULL(root))
  244. return;
  245. debugfs_create_ulong("pool_size", 0400, root, &current_pool_size);
  246. }
  247. static void dma_atomic_pool_size_add(gfp_t gfp, size_t size)
  248. {
  249. current_pool_size += size;
  250. }
  251. static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,
  252. gfp_t gfp)
  253. {
  254. unsigned int order;
  255. struct page *page = NULL;
  256. void *addr;
  257. int ret = -ENOMEM;
  258. /* Cannot allocate larger than MAX_ORDER - 1 */
  259. order = min(get_order(pool_size), MAX_ORDER - 1);
  260. do {
  261. pool_size = 1 << (PAGE_SHIFT + order);
  262. if (qcom_dev_get_cma_area(NULL))
  263. page = qcom_dma_alloc_from_contiguous(NULL, 1 << order,
  264. order, false);
  265. else
  266. page = alloc_pages(gfp, order);
  267. } while (!page && order-- > 0);
  268. if (!page)
  269. goto out;
  270. qcom_arch_dma_prep_coherent(page, pool_size);
  271. addr = qcom_dma_common_contiguous_remap(page, pool_size,
  272. pgprot_dmacoherent(PAGE_KERNEL),
  273. __builtin_return_address(0));
  274. if (!addr)
  275. goto free_page;
  276. ret = gen_pool_add_virt(pool, (unsigned long)addr, page_to_phys(page),
  277. pool_size, NUMA_NO_NODE);
  278. if (ret)
  279. goto remove_mapping;
  280. dma_atomic_pool_size_add(gfp, pool_size);
  281. return 0;
  282. remove_mapping:
  283. qcom_dma_common_free_remap(addr, pool_size);
  284. free_page:
  285. if (!qcom_dma_release_from_contiguous(NULL, page, 1 << order))
  286. __free_pages(page, order);
  287. out:
  288. return ret;
  289. }
  290. static void atomic_pool_resize(struct gen_pool *pool, gfp_t gfp)
  291. {
  292. if (pool && gen_pool_avail(pool) < atomic_pool_size)
  293. atomic_pool_expand(pool, gen_pool_size(pool), gfp);
  294. }
  295. static void atomic_pool_work_fn(struct work_struct *work)
  296. {
  297. atomic_pool_resize(atomic_pool, GFP_KERNEL);
  298. }
  299. static struct gen_pool *__dma_atomic_pool_init(size_t pool_size, gfp_t gfp)
  300. {
  301. struct gen_pool *pool;
  302. int ret;
  303. pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE);
  304. if (!pool)
  305. return NULL;
  306. gen_pool_set_algo(pool, gen_pool_first_fit_order_align, NULL);
  307. ret = atomic_pool_expand(pool, pool_size, gfp);
  308. if (ret) {
  309. gen_pool_destroy(pool);
  310. pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n",
  311. pool_size >> 10, &gfp);
  312. return NULL;
  313. }
  314. pr_info("DMA preallocated %zu KiB %pGg pool for atomic allocations\n",
  315. gen_pool_size(pool) >> 10, &gfp);
  316. return pool;
  317. }
  318. static int dma_atomic_pool_init(struct device *dev)
  319. {
  320. int ret = 0;
  321. unsigned long pages;
  322. /* Default the pool size to 128KB per 1 GB of memory, min 128 KB, max MAX_ORDER - 1. */
  323. pages = totalram_pages() / (SZ_1G / SZ_128K);
  324. pages = min_t(unsigned long, pages, MAX_ORDER_NR_PAGES);
  325. atomic_pool_size = max_t(size_t, pages << PAGE_SHIFT, SZ_128K);
  326. INIT_WORK(&atomic_pool_work, atomic_pool_work_fn);
  327. atomic_pool = __dma_atomic_pool_init(atomic_pool_size, GFP_KERNEL);
  328. if (!atomic_pool)
  329. return -ENOMEM;
  330. dma_atomic_pool_debugfs_init();
  331. return ret;
  332. }
  333. /*
  334. * Couldn't implement this via dma_alloc_attrs(qcom_iommu_dma_dev, GFP_ATOMIC)
  335. * due to dma_free_from_pool only passing in cpu_addr & not dma_handle.
  336. */
  337. void *qcom_dma_alloc_from_pool(struct device *dev, size_t size,
  338. struct page **ret_page, gfp_t flags)
  339. {
  340. unsigned long val;
  341. void *ptr = NULL;
  342. if (!atomic_pool) {
  343. WARN(1, "coherent pool not initialised!\n");
  344. return NULL;
  345. }
  346. val = gen_pool_alloc(atomic_pool, size);
  347. if (val) {
  348. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  349. *ret_page = pfn_to_page(__phys_to_pfn(phys));
  350. ptr = (void *)val;
  351. memset(ptr, 0, size);
  352. }
  353. if (gen_pool_avail(atomic_pool) < atomic_pool_size)
  354. schedule_work(&atomic_pool_work);
  355. return ptr;
  356. }
  357. bool qcom_dma_free_from_pool(struct device *dev, void *start, size_t size)
  358. {
  359. if (!atomic_pool || !gen_pool_has_addr(atomic_pool, (unsigned long)start, size))
  360. return false;
  361. gen_pool_free(atomic_pool, (unsigned long)start, size);
  362. return true;
  363. }
  364. static void qcom_dma_atomic_pool_exit(struct device *dev)
  365. {
  366. unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
  367. void *addr;
  368. struct page *page;
  369. /*
  370. * Find the starting address. Pool is expected to be unused.
  371. *
  372. * While the pool size can expand, it is okay to use the initial size
  373. * here, as this function can only ever be called prior to the pool
  374. * ever being used. The pool can only expand when an allocation is satisfied
  375. * from it, which would not be possible by the time this function is called.
  376. * Therefore the size of the pool will be the initial size.
  377. */
  378. addr = (void *)gen_pool_alloc(atomic_pool, atomic_pool_size);
  379. if (!addr) {
  380. WARN_ON(1);
  381. return;
  382. }
  383. gen_pool_free(atomic_pool, (unsigned long)addr, atomic_pool_size);
  384. gen_pool_destroy(atomic_pool);
  385. page = vmalloc_to_page(addr);
  386. qcom_dma_common_free_remap(addr, atomic_pool_size);
  387. qcom_dma_release_from_contiguous(dev, page, nr_pages);
  388. }
  389. /*
  390. * struct dma_coherent_mem is private, so we cna't access it. 0 indicates
  391. * an error condition for dma_mmap_from_dev_coherent.
  392. */
  393. int qcom_dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
  394. void *vaddr, size_t size, int *ret)
  395. {
  396. return 0;
  397. }
  398. /*
  399. * Return the page attributes used for mapping dma_alloc_* memory, either in
  400. * kernel space if remapping is needed, or to userspace through dma_mmap_*.
  401. */
  402. pgprot_t qcom_dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
  403. {
  404. if (dev_is_dma_coherent(dev))
  405. return prot;
  406. #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
  407. if (attrs & DMA_ATTR_WRITE_COMBINE)
  408. return pgprot_writecombine(prot);
  409. #endif
  410. return pgprot_dmacoherent(prot);
  411. }
  412. /**
  413. * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
  414. * page flags.
  415. * @dir: Direction of DMA transfer
  416. * @coherent: Is the DMA master cache-coherent?
  417. * @attrs: DMA attributes for the mapping
  418. *
  419. * Return: corresponding IOMMU API page protection flags
  420. */
  421. int qcom_dma_info_to_prot(enum dma_data_direction dir, bool coherent,
  422. unsigned long attrs)
  423. {
  424. int prot = coherent ? IOMMU_CACHE : 0;
  425. if (attrs & DMA_ATTR_PRIVILEGED)
  426. prot |= IOMMU_PRIV;
  427. if (attrs & DMA_ATTR_SYS_CACHE_ONLY)
  428. prot |= IOMMU_SYS_CACHE;
  429. if (attrs & DMA_ATTR_SYS_CACHE_ONLY_NWA)
  430. prot |= IOMMU_SYS_CACHE_NWA;
  431. switch (dir) {
  432. case DMA_BIDIRECTIONAL:
  433. return prot | IOMMU_READ | IOMMU_WRITE;
  434. case DMA_TO_DEVICE:
  435. return prot | IOMMU_READ;
  436. case DMA_FROM_DEVICE:
  437. return prot | IOMMU_WRITE;
  438. default:
  439. return 0;
  440. }
  441. }
  442. /*
  443. * The DMA API client is passing in a scatterlist which could describe
  444. * any old buffer layout, but the IOMMU API requires everything to be
  445. * aligned to IOMMU pages. Hence the need for this complicated bit of
  446. * impedance-matching, to be able to hand off a suitably-aligned list,
  447. * but still preserve the original offsets and sizes for the caller.
  448. */
  449. size_t qcom_iommu_dma_prepare_map_sg(struct device *dev, struct iova_domain *iovad,
  450. struct scatterlist *sg, int nents)
  451. {
  452. struct scatterlist *s, *prev = NULL;
  453. size_t iova_len = 0;
  454. unsigned long mask = dma_get_seg_boundary(dev);
  455. int i;
  456. /*
  457. * Work out how much IOVA space we need, and align the segments to
  458. * IOVA granules for the IOMMU driver to handle. With some clever
  459. * trickery we can modify the list in-place, but reversibly, by
  460. * stashing the unaligned parts in the as-yet-unused DMA fields.
  461. */
  462. for_each_sg(sg, s, nents, i) {
  463. size_t s_iova_off = iova_offset(iovad, s->offset);
  464. size_t s_length = s->length;
  465. size_t pad_len = (mask - iova_len + 1) & mask;
  466. sg_dma_address(s) = s_iova_off;
  467. sg_dma_len(s) = s_length;
  468. s->offset -= s_iova_off;
  469. s_length = iova_align(iovad, s_length + s_iova_off);
  470. s->length = s_length;
  471. /*
  472. * Due to the alignment of our single IOVA allocation, we can
  473. * depend on these assumptions about the segment boundary mask:
  474. * - If mask size >= IOVA size, then the IOVA range cannot
  475. * possibly fall across a boundary, so we don't care.
  476. * - If mask size < IOVA size, then the IOVA range must start
  477. * exactly on a boundary, therefore we can lay things out
  478. * based purely on segment lengths without needing to know
  479. * the actual addresses beforehand.
  480. * - The mask must be a power of 2, so pad_len == 0 if
  481. * iova_len == 0, thus we cannot dereference prev the first
  482. * time through here (i.e. before it has a meaningful value).
  483. */
  484. if (pad_len && pad_len < s_length - 1) {
  485. prev->length += pad_len;
  486. iova_len += pad_len;
  487. }
  488. iova_len += s_length;
  489. prev = s;
  490. }
  491. return iova_len;
  492. }
  493. /*
  494. * Prepare a successfully-mapped scatterlist to give back to the caller.
  495. *
  496. * At this point the segments are already laid out by iommu_dma_map_sg() to
  497. * avoid individually crossing any boundaries, so we merely need to check a
  498. * segment's start address to avoid concatenating across one.
  499. */
  500. int qcom_iommu_dma_finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
  501. dma_addr_t dma_addr)
  502. {
  503. struct scatterlist *s, *cur = sg;
  504. unsigned long seg_mask = dma_get_seg_boundary(dev);
  505. unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
  506. int i, count = 0;
  507. for_each_sg(sg, s, nents, i) {
  508. /* Restore this segment's original unaligned fields first */
  509. unsigned int s_iova_off = sg_dma_address(s);
  510. unsigned int s_length = sg_dma_len(s);
  511. unsigned int s_iova_len = s->length;
  512. s->offset += s_iova_off;
  513. s->length = s_length;
  514. sg_dma_address(s) = DMA_MAPPING_ERROR;
  515. sg_dma_len(s) = 0;
  516. /*
  517. * Now fill in the real DMA data. If...
  518. * - there is a valid output segment to append to
  519. * - and this segment starts on an IOVA page boundary
  520. * - but doesn't fall at a segment boundary
  521. * - and wouldn't make the resulting output segment too long
  522. */
  523. if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
  524. (max_len - cur_len >= s_length)) {
  525. /* ...then concatenate it with the previous one */
  526. cur_len += s_length;
  527. } else {
  528. /* Otherwise start the next output segment */
  529. if (i > 0)
  530. cur = sg_next(cur);
  531. cur_len = s_length;
  532. count++;
  533. sg_dma_address(cur) = dma_addr + s_iova_off;
  534. }
  535. sg_dma_len(cur) = cur_len;
  536. dma_addr += s_iova_len;
  537. if (s_length + s_iova_off < s_iova_len)
  538. cur_len = 0;
  539. }
  540. return count;
  541. }
  542. /*
  543. * If mapping failed, then just restore the original list,
  544. * but making sure the DMA fields are invalidated.
  545. */
  546. void qcom_iommu_dma_invalidate_sg(struct scatterlist *sg, int nents)
  547. {
  548. struct scatterlist *s;
  549. int i;
  550. for_each_sg(sg, s, nents, i) {
  551. if (sg_dma_address(s) != DMA_MAPPING_ERROR)
  552. s->offset += sg_dma_address(s);
  553. if (sg_dma_len(s))
  554. s->length = sg_dma_len(s);
  555. sg_dma_address(s) = DMA_MAPPING_ERROR;
  556. sg_dma_len(s) = 0;
  557. }
  558. }
  559. /**
  560. * __iommu_dma_mmap - Map a buffer into provided user VMA
  561. * @pages: Array representing buffer from __iommu_dma_alloc()
  562. * @size: Size of buffer in bytes
  563. * @vma: VMA describing requested userspace mapping
  564. *
  565. * Maps the pages of the buffer in @pages into @vma. The caller is responsible
  566. * for verifying the correct size and protection of @vma beforehand.
  567. */
  568. static int __qcom_iommu_dma_mmap(struct page **pages, size_t size,
  569. struct vm_area_struct *vma)
  570. {
  571. return vm_map_pages(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
  572. }
  573. int qcom_iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  574. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  575. unsigned long attrs)
  576. {
  577. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  578. unsigned long pfn, off = vma->vm_pgoff;
  579. int ret;
  580. vma->vm_page_prot = qcom_dma_pgprot(dev, vma->vm_page_prot, attrs);
  581. if (qcom_dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  582. return ret;
  583. if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
  584. return -ENXIO;
  585. if (is_vmalloc_addr(cpu_addr)) {
  586. struct page **pages = qcom_dma_common_find_pages(cpu_addr);
  587. if (pages)
  588. return __qcom_iommu_dma_mmap(pages, size, vma);
  589. pfn = vmalloc_to_pfn(cpu_addr);
  590. } else {
  591. pfn = page_to_pfn(virt_to_page(cpu_addr));
  592. }
  593. return remap_pfn_range(vma, vma->vm_start, pfn + off,
  594. vma->vm_end - vma->vm_start,
  595. vma->vm_page_prot);
  596. }
  597. int qcom_iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  598. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  599. unsigned long attrs)
  600. {
  601. struct page *page;
  602. int ret;
  603. if (is_vmalloc_addr(cpu_addr)) {
  604. struct page **pages = qcom_dma_common_find_pages(cpu_addr);
  605. if (pages) {
  606. return sg_alloc_table_from_pages(sgt, pages,
  607. PAGE_ALIGN(size) >> PAGE_SHIFT,
  608. 0, size, GFP_KERNEL);
  609. }
  610. page = vmalloc_to_page(cpu_addr);
  611. } else {
  612. page = virt_to_page(cpu_addr);
  613. }
  614. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  615. if (!ret)
  616. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  617. return ret;
  618. }
  619. static int qcom_dma_iommu_probe(struct platform_device *pdev)
  620. {
  621. int ret;
  622. struct device *dev = &pdev->dev;
  623. qcom_dma_iommu_dev = dev;
  624. if (dev_is_dma_coherent(dev)) {
  625. dev_err(dev, "Cannot be dma-coherent\n");
  626. return -EINVAL;
  627. }
  628. /* Should be connected to linux,cma-default node */
  629. ret = of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
  630. if (ret)
  631. return ret;
  632. qcom_dma_contiguous_default_area = dev->cma_area;
  633. if (!qcom_dma_contiguous_default_area) {
  634. dev_err(dev, "Unable to find cma area\n");
  635. return -EINVAL;
  636. }
  637. ret = dma_atomic_pool_init(dev);
  638. if (ret)
  639. goto out_iova_cache;
  640. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  641. if (ret)
  642. goto out_atomic_pool;
  643. probe_finished = true;
  644. return 0;
  645. out_atomic_pool:
  646. qcom_dma_atomic_pool_exit(dev);
  647. out_iova_cache:
  648. return ret;
  649. }
  650. bool qcom_dma_iommu_is_ready(void)
  651. {
  652. if (!probe_finished)
  653. return false;
  654. return true;
  655. }
  656. EXPORT_SYMBOL(qcom_dma_iommu_is_ready);
  657. static int qcom_dma_iommu_remove(struct platform_device *pdev)
  658. {
  659. qcom_dma_atomic_pool_exit(&pdev->dev);
  660. return 0;
  661. }
  662. static const struct of_device_id qcom_dma_iommu_of_match[] = {
  663. {.compatible = "qcom,iommu-dma"},
  664. {}
  665. };
  666. MODULE_DEVICE_TABLE(of, qcom_dma_iommu_of_match);
  667. static struct platform_driver qcom_dma_iommu_driver = {
  668. .probe = qcom_dma_iommu_probe,
  669. .remove = qcom_dma_iommu_remove,
  670. .driver = {
  671. .name = "qcom_dma_iommu",
  672. .of_match_table = qcom_dma_iommu_of_match,
  673. .suppress_bind_attrs = true,
  674. },
  675. };
  676. int __init qcom_dma_iommu_generic_driver_init(void)
  677. {
  678. return platform_driver_register(&qcom_dma_iommu_driver);
  679. }
  680. void qcom_dma_iommu_generic_driver_exit(void)
  681. {
  682. platform_driver_unregister(&qcom_dma_iommu_driver);
  683. }