dma-iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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/irq.h>
  28. #include <linux/mm.h>
  29. #include <linux/pci.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/vmalloc.h>
  32. struct iommu_dma_msi_page {
  33. struct list_head list;
  34. dma_addr_t iova;
  35. phys_addr_t phys;
  36. };
  37. struct iommu_dma_cookie {
  38. struct iova_domain iovad;
  39. struct list_head msi_page_list;
  40. spinlock_t msi_lock;
  41. };
  42. static inline struct iova_domain *cookie_iovad(struct iommu_domain *domain)
  43. {
  44. return &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad;
  45. }
  46. int iommu_dma_init(void)
  47. {
  48. return iova_cache_get();
  49. }
  50. /**
  51. * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
  52. * @domain: IOMMU domain to prepare for DMA-API usage
  53. *
  54. * IOMMU drivers should normally call this from their domain_alloc
  55. * callback when domain->type == IOMMU_DOMAIN_DMA.
  56. */
  57. int iommu_get_dma_cookie(struct iommu_domain *domain)
  58. {
  59. struct iommu_dma_cookie *cookie;
  60. if (domain->iova_cookie)
  61. return -EEXIST;
  62. cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
  63. if (!cookie)
  64. return -ENOMEM;
  65. spin_lock_init(&cookie->msi_lock);
  66. INIT_LIST_HEAD(&cookie->msi_page_list);
  67. domain->iova_cookie = cookie;
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(iommu_get_dma_cookie);
  71. /**
  72. * iommu_put_dma_cookie - Release a domain's DMA mapping resources
  73. * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
  74. *
  75. * IOMMU drivers should normally call this from their domain_free callback.
  76. */
  77. void iommu_put_dma_cookie(struct iommu_domain *domain)
  78. {
  79. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  80. struct iommu_dma_msi_page *msi, *tmp;
  81. if (!cookie)
  82. return;
  83. if (cookie->iovad.granule)
  84. put_iova_domain(&cookie->iovad);
  85. list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
  86. list_del(&msi->list);
  87. kfree(msi);
  88. }
  89. kfree(cookie);
  90. domain->iova_cookie = NULL;
  91. }
  92. EXPORT_SYMBOL(iommu_put_dma_cookie);
  93. static void iova_reserve_pci_windows(struct pci_dev *dev,
  94. struct iova_domain *iovad)
  95. {
  96. struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
  97. struct resource_entry *window;
  98. unsigned long lo, hi;
  99. resource_list_for_each_entry(window, &bridge->windows) {
  100. if (resource_type(window->res) != IORESOURCE_MEM &&
  101. resource_type(window->res) != IORESOURCE_IO)
  102. continue;
  103. lo = iova_pfn(iovad, window->res->start - window->offset);
  104. hi = iova_pfn(iovad, window->res->end - window->offset);
  105. reserve_iova(iovad, lo, hi);
  106. }
  107. }
  108. /**
  109. * iommu_dma_init_domain - Initialise a DMA mapping domain
  110. * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
  111. * @base: IOVA at which the mappable address space starts
  112. * @size: Size of IOVA space
  113. * @dev: Device the domain is being initialised for
  114. *
  115. * @base and @size should be exact multiples of IOMMU page granularity to
  116. * avoid rounding surprises. If necessary, we reserve the page at address 0
  117. * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
  118. * any change which could make prior IOVAs invalid will fail.
  119. */
  120. int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
  121. u64 size, struct device *dev)
  122. {
  123. struct iova_domain *iovad = cookie_iovad(domain);
  124. unsigned long order, base_pfn, end_pfn;
  125. if (!iovad)
  126. return -ENODEV;
  127. /* Use the smallest supported page size for IOVA granularity */
  128. order = __ffs(domain->pgsize_bitmap);
  129. base_pfn = max_t(unsigned long, 1, base >> order);
  130. end_pfn = (base + size - 1) >> order;
  131. /* Check the domain allows at least some access to the device... */
  132. if (domain->geometry.force_aperture) {
  133. if (base > domain->geometry.aperture_end ||
  134. base + size <= domain->geometry.aperture_start) {
  135. pr_warn("specified DMA range outside IOMMU capability\n");
  136. return -EFAULT;
  137. }
  138. /* ...then finally give it a kicking to make sure it fits */
  139. base_pfn = max_t(unsigned long, base_pfn,
  140. domain->geometry.aperture_start >> order);
  141. end_pfn = min_t(unsigned long, end_pfn,
  142. domain->geometry.aperture_end >> order);
  143. }
  144. /* All we can safely do with an existing domain is enlarge it */
  145. if (iovad->start_pfn) {
  146. if (1UL << order != iovad->granule ||
  147. base_pfn != iovad->start_pfn ||
  148. end_pfn < iovad->dma_32bit_pfn) {
  149. pr_warn("Incompatible range for DMA domain\n");
  150. return -EFAULT;
  151. }
  152. iovad->dma_32bit_pfn = end_pfn;
  153. } else {
  154. init_iova_domain(iovad, 1UL << order, base_pfn, end_pfn);
  155. if (dev && dev_is_pci(dev))
  156. iova_reserve_pci_windows(to_pci_dev(dev), iovad);
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(iommu_dma_init_domain);
  161. /**
  162. * dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags
  163. * @dir: Direction of DMA transfer
  164. * @coherent: Is the DMA master cache-coherent?
  165. *
  166. * Return: corresponding IOMMU API page protection flags
  167. */
  168. int dma_direction_to_prot(enum dma_data_direction dir, bool coherent)
  169. {
  170. int prot = coherent ? IOMMU_CACHE : 0;
  171. switch (dir) {
  172. case DMA_BIDIRECTIONAL:
  173. return prot | IOMMU_READ | IOMMU_WRITE;
  174. case DMA_TO_DEVICE:
  175. return prot | IOMMU_READ;
  176. case DMA_FROM_DEVICE:
  177. return prot | IOMMU_WRITE;
  178. default:
  179. return 0;
  180. }
  181. }
  182. static struct iova *__alloc_iova(struct iommu_domain *domain, size_t size,
  183. dma_addr_t dma_limit)
  184. {
  185. struct iova_domain *iovad = cookie_iovad(domain);
  186. unsigned long shift = iova_shift(iovad);
  187. unsigned long length = iova_align(iovad, size) >> shift;
  188. if (domain->geometry.force_aperture)
  189. dma_limit = min(dma_limit, domain->geometry.aperture_end);
  190. /*
  191. * Enforce size-alignment to be safe - there could perhaps be an
  192. * attribute to control this per-device, or at least per-domain...
  193. */
  194. return alloc_iova(iovad, length, dma_limit >> shift, true);
  195. }
  196. /* The IOVA allocator knows what we mapped, so just unmap whatever that was */
  197. static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr)
  198. {
  199. struct iova_domain *iovad = cookie_iovad(domain);
  200. unsigned long shift = iova_shift(iovad);
  201. unsigned long pfn = dma_addr >> shift;
  202. struct iova *iova = find_iova(iovad, pfn);
  203. size_t size;
  204. if (WARN_ON(!iova))
  205. return;
  206. size = iova_size(iova) << shift;
  207. size -= iommu_unmap(domain, pfn << shift, size);
  208. /* ...and if we can't, then something is horribly, horribly wrong */
  209. WARN_ON(size > 0);
  210. __free_iova(iovad, iova);
  211. }
  212. static void __iommu_dma_free_pages(struct page **pages, int count)
  213. {
  214. while (count--)
  215. __free_page(pages[count]);
  216. kvfree(pages);
  217. }
  218. static struct page **__iommu_dma_alloc_pages(unsigned int count,
  219. unsigned long order_mask, gfp_t gfp)
  220. {
  221. struct page **pages;
  222. unsigned int i = 0, array_size = count * sizeof(*pages);
  223. order_mask &= (2U << MAX_ORDER) - 1;
  224. if (!order_mask)
  225. return NULL;
  226. if (array_size <= PAGE_SIZE)
  227. pages = kzalloc(array_size, GFP_KERNEL);
  228. else
  229. pages = vzalloc(array_size);
  230. if (!pages)
  231. return NULL;
  232. /* IOMMU can map any pages, so himem can also be used here */
  233. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  234. while (count) {
  235. struct page *page = NULL;
  236. unsigned int order_size;
  237. /*
  238. * Higher-order allocations are a convenience rather
  239. * than a necessity, hence using __GFP_NORETRY until
  240. * falling back to minimum-order allocations.
  241. */
  242. for (order_mask &= (2U << __fls(count)) - 1;
  243. order_mask; order_mask &= ~order_size) {
  244. unsigned int order = __fls(order_mask);
  245. order_size = 1U << order;
  246. page = alloc_pages((order_mask - order_size) ?
  247. gfp | __GFP_NORETRY : gfp, order);
  248. if (!page)
  249. continue;
  250. if (!order)
  251. break;
  252. if (!PageCompound(page)) {
  253. split_page(page, order);
  254. break;
  255. } else if (!split_huge_page(page)) {
  256. break;
  257. }
  258. __free_pages(page, order);
  259. }
  260. if (!page) {
  261. __iommu_dma_free_pages(pages, i);
  262. return NULL;
  263. }
  264. count -= order_size;
  265. while (order_size--)
  266. pages[i++] = page++;
  267. }
  268. return pages;
  269. }
  270. /**
  271. * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc()
  272. * @dev: Device which owns this buffer
  273. * @pages: Array of buffer pages as returned by iommu_dma_alloc()
  274. * @size: Size of buffer in bytes
  275. * @handle: DMA address of buffer
  276. *
  277. * Frees both the pages associated with the buffer, and the array
  278. * describing them
  279. */
  280. void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
  281. dma_addr_t *handle)
  282. {
  283. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), *handle);
  284. __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
  285. *handle = DMA_ERROR_CODE;
  286. }
  287. /**
  288. * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space
  289. * @dev: Device to allocate memory for. Must be a real device
  290. * attached to an iommu_dma_domain
  291. * @size: Size of buffer in bytes
  292. * @gfp: Allocation flags
  293. * @attrs: DMA attributes for this allocation
  294. * @prot: IOMMU mapping flags
  295. * @handle: Out argument for allocated DMA handle
  296. * @flush_page: Arch callback which must ensure PAGE_SIZE bytes from the
  297. * given VA/PA are visible to the given non-coherent device.
  298. *
  299. * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
  300. * but an IOMMU which supports smaller pages might not map the whole thing.
  301. *
  302. * Return: Array of struct page pointers describing the buffer,
  303. * or NULL on failure.
  304. */
  305. struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
  306. unsigned long attrs, int prot, dma_addr_t *handle,
  307. void (*flush_page)(struct device *, const void *, phys_addr_t))
  308. {
  309. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  310. struct iova_domain *iovad = cookie_iovad(domain);
  311. struct iova *iova;
  312. struct page **pages;
  313. struct sg_table sgt;
  314. dma_addr_t dma_addr;
  315. unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
  316. *handle = DMA_ERROR_CODE;
  317. min_size = alloc_sizes & -alloc_sizes;
  318. if (min_size < PAGE_SIZE) {
  319. min_size = PAGE_SIZE;
  320. alloc_sizes |= PAGE_SIZE;
  321. } else {
  322. size = ALIGN(size, min_size);
  323. }
  324. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  325. alloc_sizes = min_size;
  326. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  327. pages = __iommu_dma_alloc_pages(count, alloc_sizes >> PAGE_SHIFT, gfp);
  328. if (!pages)
  329. return NULL;
  330. iova = __alloc_iova(domain, size, dev->coherent_dma_mask);
  331. if (!iova)
  332. goto out_free_pages;
  333. size = iova_align(iovad, size);
  334. if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
  335. goto out_free_iova;
  336. if (!(prot & IOMMU_CACHE)) {
  337. struct sg_mapping_iter miter;
  338. /*
  339. * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
  340. * sufficient here, so skip it by using the "wrong" direction.
  341. */
  342. sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG);
  343. while (sg_miter_next(&miter))
  344. flush_page(dev, miter.addr, page_to_phys(miter.page));
  345. sg_miter_stop(&miter);
  346. }
  347. dma_addr = iova_dma_addr(iovad, iova);
  348. if (iommu_map_sg(domain, dma_addr, sgt.sgl, sgt.orig_nents, prot)
  349. < size)
  350. goto out_free_sg;
  351. *handle = dma_addr;
  352. sg_free_table(&sgt);
  353. return pages;
  354. out_free_sg:
  355. sg_free_table(&sgt);
  356. out_free_iova:
  357. __free_iova(iovad, iova);
  358. out_free_pages:
  359. __iommu_dma_free_pages(pages, count);
  360. return NULL;
  361. }
  362. /**
  363. * iommu_dma_mmap - Map a buffer into provided user VMA
  364. * @pages: Array representing buffer from iommu_dma_alloc()
  365. * @size: Size of buffer in bytes
  366. * @vma: VMA describing requested userspace mapping
  367. *
  368. * Maps the pages of the buffer in @pages into @vma. The caller is responsible
  369. * for verifying the correct size and protection of @vma beforehand.
  370. */
  371. int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
  372. {
  373. unsigned long uaddr = vma->vm_start;
  374. unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  375. int ret = -ENXIO;
  376. for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
  377. ret = vm_insert_page(vma, uaddr, pages[i]);
  378. if (ret)
  379. break;
  380. uaddr += PAGE_SIZE;
  381. }
  382. return ret;
  383. }
  384. static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
  385. size_t size, int prot)
  386. {
  387. dma_addr_t dma_addr;
  388. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  389. struct iova_domain *iovad = cookie_iovad(domain);
  390. size_t iova_off = iova_offset(iovad, phys);
  391. size_t len = iova_align(iovad, size + iova_off);
  392. struct iova *iova = __alloc_iova(domain, len, dma_get_mask(dev));
  393. if (!iova)
  394. return DMA_ERROR_CODE;
  395. dma_addr = iova_dma_addr(iovad, iova);
  396. if (iommu_map(domain, dma_addr, phys - iova_off, len, prot)) {
  397. __free_iova(iovad, iova);
  398. return DMA_ERROR_CODE;
  399. }
  400. return dma_addr + iova_off;
  401. }
  402. dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
  403. unsigned long offset, size_t size, int prot)
  404. {
  405. return __iommu_dma_map(dev, page_to_phys(page) + offset, size, prot);
  406. }
  407. void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
  408. enum dma_data_direction dir, unsigned long attrs)
  409. {
  410. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle);
  411. }
  412. /*
  413. * Prepare a successfully-mapped scatterlist to give back to the caller.
  414. *
  415. * At this point the segments are already laid out by iommu_dma_map_sg() to
  416. * avoid individually crossing any boundaries, so we merely need to check a
  417. * segment's start address to avoid concatenating across one.
  418. */
  419. static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
  420. dma_addr_t dma_addr)
  421. {
  422. struct scatterlist *s, *cur = sg;
  423. unsigned long seg_mask = dma_get_seg_boundary(dev);
  424. unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
  425. int i, count = 0;
  426. for_each_sg(sg, s, nents, i) {
  427. /* Restore this segment's original unaligned fields first */
  428. unsigned int s_iova_off = sg_dma_address(s);
  429. unsigned int s_length = sg_dma_len(s);
  430. unsigned int s_iova_len = s->length;
  431. s->offset += s_iova_off;
  432. s->length = s_length;
  433. sg_dma_address(s) = DMA_ERROR_CODE;
  434. sg_dma_len(s) = 0;
  435. /*
  436. * Now fill in the real DMA data. If...
  437. * - there is a valid output segment to append to
  438. * - and this segment starts on an IOVA page boundary
  439. * - but doesn't fall at a segment boundary
  440. * - and wouldn't make the resulting output segment too long
  441. */
  442. if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
  443. (cur_len + s_length <= max_len)) {
  444. /* ...then concatenate it with the previous one */
  445. cur_len += s_length;
  446. } else {
  447. /* Otherwise start the next output segment */
  448. if (i > 0)
  449. cur = sg_next(cur);
  450. cur_len = s_length;
  451. count++;
  452. sg_dma_address(cur) = dma_addr + s_iova_off;
  453. }
  454. sg_dma_len(cur) = cur_len;
  455. dma_addr += s_iova_len;
  456. if (s_length + s_iova_off < s_iova_len)
  457. cur_len = 0;
  458. }
  459. return count;
  460. }
  461. /*
  462. * If mapping failed, then just restore the original list,
  463. * but making sure the DMA fields are invalidated.
  464. */
  465. static void __invalidate_sg(struct scatterlist *sg, int nents)
  466. {
  467. struct scatterlist *s;
  468. int i;
  469. for_each_sg(sg, s, nents, i) {
  470. if (sg_dma_address(s) != DMA_ERROR_CODE)
  471. s->offset += sg_dma_address(s);
  472. if (sg_dma_len(s))
  473. s->length = sg_dma_len(s);
  474. sg_dma_address(s) = DMA_ERROR_CODE;
  475. sg_dma_len(s) = 0;
  476. }
  477. }
  478. /*
  479. * The DMA API client is passing in a scatterlist which could describe
  480. * any old buffer layout, but the IOMMU API requires everything to be
  481. * aligned to IOMMU pages. Hence the need for this complicated bit of
  482. * impedance-matching, to be able to hand off a suitably-aligned list,
  483. * but still preserve the original offsets and sizes for the caller.
  484. */
  485. int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
  486. int nents, int prot)
  487. {
  488. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  489. struct iova_domain *iovad = cookie_iovad(domain);
  490. struct iova *iova;
  491. struct scatterlist *s, *prev = NULL;
  492. dma_addr_t dma_addr;
  493. size_t iova_len = 0;
  494. unsigned long mask = dma_get_seg_boundary(dev);
  495. int i;
  496. /*
  497. * Work out how much IOVA space we need, and align the segments to
  498. * IOVA granules for the IOMMU driver to handle. With some clever
  499. * trickery we can modify the list in-place, but reversibly, by
  500. * stashing the unaligned parts in the as-yet-unused DMA fields.
  501. */
  502. for_each_sg(sg, s, nents, i) {
  503. size_t s_iova_off = iova_offset(iovad, s->offset);
  504. size_t s_length = s->length;
  505. size_t pad_len = (mask - iova_len + 1) & mask;
  506. sg_dma_address(s) = s_iova_off;
  507. sg_dma_len(s) = s_length;
  508. s->offset -= s_iova_off;
  509. s_length = iova_align(iovad, s_length + s_iova_off);
  510. s->length = s_length;
  511. /*
  512. * Due to the alignment of our single IOVA allocation, we can
  513. * depend on these assumptions about the segment boundary mask:
  514. * - If mask size >= IOVA size, then the IOVA range cannot
  515. * possibly fall across a boundary, so we don't care.
  516. * - If mask size < IOVA size, then the IOVA range must start
  517. * exactly on a boundary, therefore we can lay things out
  518. * based purely on segment lengths without needing to know
  519. * the actual addresses beforehand.
  520. * - The mask must be a power of 2, so pad_len == 0 if
  521. * iova_len == 0, thus we cannot dereference prev the first
  522. * time through here (i.e. before it has a meaningful value).
  523. */
  524. if (pad_len && pad_len < s_length - 1) {
  525. prev->length += pad_len;
  526. iova_len += pad_len;
  527. }
  528. iova_len += s_length;
  529. prev = s;
  530. }
  531. iova = __alloc_iova(domain, iova_len, dma_get_mask(dev));
  532. if (!iova)
  533. goto out_restore_sg;
  534. /*
  535. * We'll leave any physical concatenation to the IOMMU driver's
  536. * implementation - it knows better than we do.
  537. */
  538. dma_addr = iova_dma_addr(iovad, iova);
  539. if (iommu_map_sg(domain, dma_addr, sg, nents, prot) < iova_len)
  540. goto out_free_iova;
  541. return __finalise_sg(dev, sg, nents, dma_addr);
  542. out_free_iova:
  543. __free_iova(iovad, iova);
  544. out_restore_sg:
  545. __invalidate_sg(sg, nents);
  546. return 0;
  547. }
  548. void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  549. enum dma_data_direction dir, unsigned long attrs)
  550. {
  551. /*
  552. * The scatterlist segments are mapped into a single
  553. * contiguous IOVA allocation, so this is incredibly easy.
  554. */
  555. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), sg_dma_address(sg));
  556. }
  557. dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
  558. size_t size, enum dma_data_direction dir, unsigned long attrs)
  559. {
  560. return __iommu_dma_map(dev, phys, size,
  561. dma_direction_to_prot(dir, false) | IOMMU_MMIO);
  562. }
  563. void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
  564. size_t size, enum dma_data_direction dir, unsigned long attrs)
  565. {
  566. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle);
  567. }
  568. int iommu_dma_supported(struct device *dev, u64 mask)
  569. {
  570. /*
  571. * 'Special' IOMMUs which don't have the same addressing capability
  572. * as the CPU will have to wait until we have some way to query that
  573. * before they'll be able to use this framework.
  574. */
  575. return 1;
  576. }
  577. int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  578. {
  579. return dma_addr == DMA_ERROR_CODE;
  580. }
  581. static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
  582. phys_addr_t msi_addr, struct iommu_domain *domain)
  583. {
  584. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  585. struct iommu_dma_msi_page *msi_page;
  586. struct iova_domain *iovad = &cookie->iovad;
  587. struct iova *iova;
  588. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  589. msi_addr &= ~(phys_addr_t)iova_mask(iovad);
  590. list_for_each_entry(msi_page, &cookie->msi_page_list, list)
  591. if (msi_page->phys == msi_addr)
  592. return msi_page;
  593. msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC);
  594. if (!msi_page)
  595. return NULL;
  596. iova = __alloc_iova(domain, iovad->granule, dma_get_mask(dev));
  597. if (!iova)
  598. goto out_free_page;
  599. msi_page->phys = msi_addr;
  600. msi_page->iova = iova_dma_addr(iovad, iova);
  601. if (iommu_map(domain, msi_page->iova, msi_addr, iovad->granule, prot))
  602. goto out_free_iova;
  603. INIT_LIST_HEAD(&msi_page->list);
  604. list_add(&msi_page->list, &cookie->msi_page_list);
  605. return msi_page;
  606. out_free_iova:
  607. __free_iova(iovad, iova);
  608. out_free_page:
  609. kfree(msi_page);
  610. return NULL;
  611. }
  612. void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg)
  613. {
  614. struct device *dev = msi_desc_to_dev(irq_get_msi_desc(irq));
  615. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  616. struct iommu_dma_cookie *cookie;
  617. struct iommu_dma_msi_page *msi_page;
  618. phys_addr_t msi_addr = (u64)msg->address_hi << 32 | msg->address_lo;
  619. unsigned long flags;
  620. if (!domain || !domain->iova_cookie)
  621. return;
  622. cookie = domain->iova_cookie;
  623. /*
  624. * We disable IRQs to rule out a possible inversion against
  625. * irq_desc_lock if, say, someone tries to retarget the affinity
  626. * of an MSI from within an IPI handler.
  627. */
  628. spin_lock_irqsave(&cookie->msi_lock, flags);
  629. msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
  630. spin_unlock_irqrestore(&cookie->msi_lock, flags);
  631. if (WARN_ON(!msi_page)) {
  632. /*
  633. * We're called from a void callback, so the best we can do is
  634. * 'fail' by filling the message with obviously bogus values.
  635. * Since we got this far due to an IOMMU being present, it's
  636. * not like the existing address would have worked anyway...
  637. */
  638. msg->address_hi = ~0U;
  639. msg->address_lo = ~0U;
  640. msg->data = ~0U;
  641. } else {
  642. msg->address_hi = upper_32_bits(msi_page->iova);
  643. msg->address_lo &= iova_mask(&cookie->iovad);
  644. msg->address_lo += lower_32_bits(msi_page->iova);
  645. }
  646. }