dma-iommu.c 26 KB

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