dma-iommu.c 26 KB

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