dma-iommu.c 24 KB

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