dma-iommu.c 27 KB

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