dma-iommu.c 26 KB

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