dma-iommu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * A fairly generic DMA-API to IOMMU-API glue layer.
  3. *
  4. * Copyright (C) 2014-2015 ARM Ltd.
  5. *
  6. * based in part on arch/arm/mm/dma-mapping.c:
  7. * Copyright (C) 2000-2004 Russell King
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/dma-iommu.h>
  23. #include <linux/gfp.h>
  24. #include <linux/huge_mm.h>
  25. #include <linux/iommu.h>
  26. #include <linux/iova.h>
  27. #include <linux/mm.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/vmalloc.h>
  30. int iommu_dma_init(void)
  31. {
  32. return iova_cache_get();
  33. }
  34. /**
  35. * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
  36. * @domain: IOMMU domain to prepare for DMA-API usage
  37. *
  38. * IOMMU drivers should normally call this from their domain_alloc
  39. * callback when domain->type == IOMMU_DOMAIN_DMA.
  40. */
  41. int iommu_get_dma_cookie(struct iommu_domain *domain)
  42. {
  43. struct iova_domain *iovad;
  44. if (domain->iova_cookie)
  45. return -EEXIST;
  46. iovad = kzalloc(sizeof(*iovad), GFP_KERNEL);
  47. domain->iova_cookie = iovad;
  48. return iovad ? 0 : -ENOMEM;
  49. }
  50. EXPORT_SYMBOL(iommu_get_dma_cookie);
  51. /**
  52. * iommu_put_dma_cookie - Release a domain's DMA mapping resources
  53. * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
  54. *
  55. * IOMMU drivers should normally call this from their domain_free callback.
  56. */
  57. void iommu_put_dma_cookie(struct iommu_domain *domain)
  58. {
  59. struct iova_domain *iovad = domain->iova_cookie;
  60. if (!iovad)
  61. return;
  62. if (iovad->granule)
  63. put_iova_domain(iovad);
  64. kfree(iovad);
  65. domain->iova_cookie = NULL;
  66. }
  67. EXPORT_SYMBOL(iommu_put_dma_cookie);
  68. /**
  69. * iommu_dma_init_domain - Initialise a DMA mapping domain
  70. * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
  71. * @base: IOVA at which the mappable address space starts
  72. * @size: Size of IOVA space
  73. *
  74. * @base and @size should be exact multiples of IOMMU page granularity to
  75. * avoid rounding surprises. If necessary, we reserve the page at address 0
  76. * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
  77. * any change which could make prior IOVAs invalid will fail.
  78. */
  79. int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, u64 size)
  80. {
  81. struct iova_domain *iovad = domain->iova_cookie;
  82. unsigned long order, base_pfn, end_pfn;
  83. if (!iovad)
  84. return -ENODEV;
  85. /* Use the smallest supported page size for IOVA granularity */
  86. order = __ffs(domain->ops->pgsize_bitmap);
  87. base_pfn = max_t(unsigned long, 1, base >> order);
  88. end_pfn = (base + size - 1) >> order;
  89. /* Check the domain allows at least some access to the device... */
  90. if (domain->geometry.force_aperture) {
  91. if (base > domain->geometry.aperture_end ||
  92. base + size <= domain->geometry.aperture_start) {
  93. pr_warn("specified DMA range outside IOMMU capability\n");
  94. return -EFAULT;
  95. }
  96. /* ...then finally give it a kicking to make sure it fits */
  97. base_pfn = max_t(unsigned long, base_pfn,
  98. domain->geometry.aperture_start >> order);
  99. end_pfn = min_t(unsigned long, end_pfn,
  100. domain->geometry.aperture_end >> order);
  101. }
  102. /* All we can safely do with an existing domain is enlarge it */
  103. if (iovad->start_pfn) {
  104. if (1UL << order != iovad->granule ||
  105. base_pfn != iovad->start_pfn ||
  106. end_pfn < iovad->dma_32bit_pfn) {
  107. pr_warn("Incompatible range for DMA domain\n");
  108. return -EFAULT;
  109. }
  110. iovad->dma_32bit_pfn = end_pfn;
  111. } else {
  112. init_iova_domain(iovad, 1UL << order, base_pfn, end_pfn);
  113. }
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(iommu_dma_init_domain);
  117. /**
  118. * dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags
  119. * @dir: Direction of DMA transfer
  120. * @coherent: Is the DMA master cache-coherent?
  121. *
  122. * Return: corresponding IOMMU API page protection flags
  123. */
  124. int dma_direction_to_prot(enum dma_data_direction dir, bool coherent)
  125. {
  126. int prot = coherent ? IOMMU_CACHE : 0;
  127. switch (dir) {
  128. case DMA_BIDIRECTIONAL:
  129. return prot | IOMMU_READ | IOMMU_WRITE;
  130. case DMA_TO_DEVICE:
  131. return prot | IOMMU_READ;
  132. case DMA_FROM_DEVICE:
  133. return prot | IOMMU_WRITE;
  134. default:
  135. return 0;
  136. }
  137. }
  138. static struct iova *__alloc_iova(struct iova_domain *iovad, size_t size,
  139. dma_addr_t dma_limit)
  140. {
  141. unsigned long shift = iova_shift(iovad);
  142. unsigned long length = iova_align(iovad, size) >> shift;
  143. /*
  144. * Enforce size-alignment to be safe - there could perhaps be an
  145. * attribute to control this per-device, or at least per-domain...
  146. */
  147. return alloc_iova(iovad, length, dma_limit >> shift, true);
  148. }
  149. /* The IOVA allocator knows what we mapped, so just unmap whatever that was */
  150. static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr)
  151. {
  152. struct iova_domain *iovad = domain->iova_cookie;
  153. unsigned long shift = iova_shift(iovad);
  154. unsigned long pfn = dma_addr >> shift;
  155. struct iova *iova = find_iova(iovad, pfn);
  156. size_t size;
  157. if (WARN_ON(!iova))
  158. return;
  159. size = iova_size(iova) << shift;
  160. size -= iommu_unmap(domain, pfn << shift, size);
  161. /* ...and if we can't, then something is horribly, horribly wrong */
  162. WARN_ON(size > 0);
  163. __free_iova(iovad, iova);
  164. }
  165. static void __iommu_dma_free_pages(struct page **pages, int count)
  166. {
  167. while (count--)
  168. __free_page(pages[count]);
  169. kvfree(pages);
  170. }
  171. static struct page **__iommu_dma_alloc_pages(unsigned int count, gfp_t gfp)
  172. {
  173. struct page **pages;
  174. unsigned int i = 0, array_size = count * sizeof(*pages);
  175. unsigned int order = MAX_ORDER;
  176. if (array_size <= PAGE_SIZE)
  177. pages = kzalloc(array_size, GFP_KERNEL);
  178. else
  179. pages = vzalloc(array_size);
  180. if (!pages)
  181. return NULL;
  182. /* IOMMU can map any pages, so himem can also be used here */
  183. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  184. while (count) {
  185. struct page *page = NULL;
  186. int j;
  187. /*
  188. * Higher-order allocations are a convenience rather
  189. * than a necessity, hence using __GFP_NORETRY until
  190. * falling back to single-page allocations.
  191. */
  192. for (order = min_t(unsigned int, order, __fls(count));
  193. order > 0; order--) {
  194. page = alloc_pages(gfp | __GFP_NORETRY, order);
  195. if (!page)
  196. continue;
  197. if (PageCompound(page)) {
  198. if (!split_huge_page(page))
  199. break;
  200. __free_pages(page, order);
  201. } else {
  202. split_page(page, order);
  203. break;
  204. }
  205. }
  206. if (!page)
  207. page = alloc_page(gfp);
  208. if (!page) {
  209. __iommu_dma_free_pages(pages, i);
  210. return NULL;
  211. }
  212. j = 1 << order;
  213. count -= j;
  214. while (j--)
  215. pages[i++] = page++;
  216. }
  217. return pages;
  218. }
  219. /**
  220. * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc()
  221. * @dev: Device which owns this buffer
  222. * @pages: Array of buffer pages as returned by iommu_dma_alloc()
  223. * @size: Size of buffer in bytes
  224. * @handle: DMA address of buffer
  225. *
  226. * Frees both the pages associated with the buffer, and the array
  227. * describing them
  228. */
  229. void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
  230. dma_addr_t *handle)
  231. {
  232. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), *handle);
  233. __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
  234. *handle = DMA_ERROR_CODE;
  235. }
  236. /**
  237. * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space
  238. * @dev: Device to allocate memory for. Must be a real device
  239. * attached to an iommu_dma_domain
  240. * @size: Size of buffer in bytes
  241. * @gfp: Allocation flags
  242. * @prot: IOMMU mapping flags
  243. * @handle: Out argument for allocated DMA handle
  244. * @flush_page: Arch callback which must ensure PAGE_SIZE bytes from the
  245. * given VA/PA are visible to the given non-coherent device.
  246. *
  247. * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
  248. * but an IOMMU which supports smaller pages might not map the whole thing.
  249. *
  250. * Return: Array of struct page pointers describing the buffer,
  251. * or NULL on failure.
  252. */
  253. struct page **iommu_dma_alloc(struct device *dev, size_t size,
  254. gfp_t gfp, int prot, dma_addr_t *handle,
  255. void (*flush_page)(struct device *, const void *, phys_addr_t))
  256. {
  257. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  258. struct iova_domain *iovad = domain->iova_cookie;
  259. struct iova *iova;
  260. struct page **pages;
  261. struct sg_table sgt;
  262. dma_addr_t dma_addr;
  263. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  264. *handle = DMA_ERROR_CODE;
  265. pages = __iommu_dma_alloc_pages(count, gfp);
  266. if (!pages)
  267. return NULL;
  268. iova = __alloc_iova(iovad, size, dev->coherent_dma_mask);
  269. if (!iova)
  270. goto out_free_pages;
  271. size = iova_align(iovad, size);
  272. if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
  273. goto out_free_iova;
  274. if (!(prot & IOMMU_CACHE)) {
  275. struct sg_mapping_iter miter;
  276. /*
  277. * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
  278. * sufficient here, so skip it by using the "wrong" direction.
  279. */
  280. sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG);
  281. while (sg_miter_next(&miter))
  282. flush_page(dev, miter.addr, page_to_phys(miter.page));
  283. sg_miter_stop(&miter);
  284. }
  285. dma_addr = iova_dma_addr(iovad, iova);
  286. if (iommu_map_sg(domain, dma_addr, sgt.sgl, sgt.orig_nents, prot)
  287. < size)
  288. goto out_free_sg;
  289. *handle = dma_addr;
  290. sg_free_table(&sgt);
  291. return pages;
  292. out_free_sg:
  293. sg_free_table(&sgt);
  294. out_free_iova:
  295. __free_iova(iovad, iova);
  296. out_free_pages:
  297. __iommu_dma_free_pages(pages, count);
  298. return NULL;
  299. }
  300. /**
  301. * iommu_dma_mmap - Map a buffer into provided user VMA
  302. * @pages: Array representing buffer from iommu_dma_alloc()
  303. * @size: Size of buffer in bytes
  304. * @vma: VMA describing requested userspace mapping
  305. *
  306. * Maps the pages of the buffer in @pages into @vma. The caller is responsible
  307. * for verifying the correct size and protection of @vma beforehand.
  308. */
  309. int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
  310. {
  311. unsigned long uaddr = vma->vm_start;
  312. unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  313. int ret = -ENXIO;
  314. for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
  315. ret = vm_insert_page(vma, uaddr, pages[i]);
  316. if (ret)
  317. break;
  318. uaddr += PAGE_SIZE;
  319. }
  320. return ret;
  321. }
  322. dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
  323. unsigned long offset, size_t size, int prot)
  324. {
  325. dma_addr_t dma_addr;
  326. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  327. struct iova_domain *iovad = domain->iova_cookie;
  328. phys_addr_t phys = page_to_phys(page) + offset;
  329. size_t iova_off = iova_offset(iovad, phys);
  330. size_t len = iova_align(iovad, size + iova_off);
  331. struct iova *iova = __alloc_iova(iovad, len, dma_get_mask(dev));
  332. if (!iova)
  333. return DMA_ERROR_CODE;
  334. dma_addr = iova_dma_addr(iovad, iova);
  335. if (iommu_map(domain, dma_addr, phys - iova_off, len, prot)) {
  336. __free_iova(iovad, iova);
  337. return DMA_ERROR_CODE;
  338. }
  339. return dma_addr + iova_off;
  340. }
  341. void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
  342. enum dma_data_direction dir, struct dma_attrs *attrs)
  343. {
  344. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle);
  345. }
  346. /*
  347. * Prepare a successfully-mapped scatterlist to give back to the caller.
  348. * Handling IOVA concatenation can come later, if needed
  349. */
  350. static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
  351. dma_addr_t dma_addr)
  352. {
  353. struct scatterlist *s;
  354. int i;
  355. for_each_sg(sg, s, nents, i) {
  356. /* Un-swizzling the fields here, hence the naming mismatch */
  357. unsigned int s_offset = sg_dma_address(s);
  358. unsigned int s_length = sg_dma_len(s);
  359. unsigned int s_dma_len = s->length;
  360. s->offset += s_offset;
  361. s->length = s_length;
  362. sg_dma_address(s) = dma_addr + s_offset;
  363. dma_addr += s_dma_len;
  364. }
  365. return i;
  366. }
  367. /*
  368. * If mapping failed, then just restore the original list,
  369. * but making sure the DMA fields are invalidated.
  370. */
  371. static void __invalidate_sg(struct scatterlist *sg, int nents)
  372. {
  373. struct scatterlist *s;
  374. int i;
  375. for_each_sg(sg, s, nents, i) {
  376. if (sg_dma_address(s) != DMA_ERROR_CODE)
  377. s->offset += sg_dma_address(s);
  378. if (sg_dma_len(s))
  379. s->length = sg_dma_len(s);
  380. sg_dma_address(s) = DMA_ERROR_CODE;
  381. sg_dma_len(s) = 0;
  382. }
  383. }
  384. /*
  385. * The DMA API client is passing in a scatterlist which could describe
  386. * any old buffer layout, but the IOMMU API requires everything to be
  387. * aligned to IOMMU pages. Hence the need for this complicated bit of
  388. * impedance-matching, to be able to hand off a suitably-aligned list,
  389. * but still preserve the original offsets and sizes for the caller.
  390. */
  391. int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
  392. int nents, int prot)
  393. {
  394. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  395. struct iova_domain *iovad = domain->iova_cookie;
  396. struct iova *iova;
  397. struct scatterlist *s, *prev = NULL;
  398. dma_addr_t dma_addr;
  399. size_t iova_len = 0;
  400. int i;
  401. /*
  402. * Work out how much IOVA space we need, and align the segments to
  403. * IOVA granules for the IOMMU driver to handle. With some clever
  404. * trickery we can modify the list in-place, but reversibly, by
  405. * hiding the original data in the as-yet-unused DMA fields.
  406. */
  407. for_each_sg(sg, s, nents, i) {
  408. size_t s_offset = iova_offset(iovad, s->offset);
  409. size_t s_length = s->length;
  410. sg_dma_address(s) = s_offset;
  411. sg_dma_len(s) = s_length;
  412. s->offset -= s_offset;
  413. s_length = iova_align(iovad, s_length + s_offset);
  414. s->length = s_length;
  415. /*
  416. * The simple way to avoid the rare case of a segment
  417. * crossing the boundary mask is to pad the previous one
  418. * to end at a naturally-aligned IOVA for this one's size,
  419. * at the cost of potentially over-allocating a little.
  420. */
  421. if (prev) {
  422. size_t pad_len = roundup_pow_of_two(s_length);
  423. pad_len = (pad_len - iova_len) & (pad_len - 1);
  424. prev->length += pad_len;
  425. iova_len += pad_len;
  426. }
  427. iova_len += s_length;
  428. prev = s;
  429. }
  430. iova = __alloc_iova(iovad, iova_len, dma_get_mask(dev));
  431. if (!iova)
  432. goto out_restore_sg;
  433. /*
  434. * We'll leave any physical concatenation to the IOMMU driver's
  435. * implementation - it knows better than we do.
  436. */
  437. dma_addr = iova_dma_addr(iovad, iova);
  438. if (iommu_map_sg(domain, dma_addr, sg, nents, prot) < iova_len)
  439. goto out_free_iova;
  440. return __finalise_sg(dev, sg, nents, dma_addr);
  441. out_free_iova:
  442. __free_iova(iovad, iova);
  443. out_restore_sg:
  444. __invalidate_sg(sg, nents);
  445. return 0;
  446. }
  447. void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  448. enum dma_data_direction dir, struct dma_attrs *attrs)
  449. {
  450. /*
  451. * The scatterlist segments are mapped into a single
  452. * contiguous IOVA allocation, so this is incredibly easy.
  453. */
  454. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), sg_dma_address(sg));
  455. }
  456. int iommu_dma_supported(struct device *dev, u64 mask)
  457. {
  458. /*
  459. * 'Special' IOMMUs which don't have the same addressing capability
  460. * as the CPU will have to wait until we have some way to query that
  461. * before they'll be able to use this framework.
  462. */
  463. return 1;
  464. }
  465. int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  466. {
  467. return dma_addr == DMA_ERROR_CODE;
  468. }