dma-iommu.c 21 KB

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