dma-iommu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. for (i = 0; i < num_pages; i++) {
  167. msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL);
  168. if (!msi_page)
  169. return -ENOMEM;
  170. msi_page->phys = start;
  171. msi_page->iova = start;
  172. INIT_LIST_HEAD(&msi_page->list);
  173. list_add(&msi_page->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 (dev->bus_dma_mask)
  317. dma_limit &= dev->bus_dma_mask;
  318. if (domain->geometry.force_aperture)
  319. dma_limit = min(dma_limit, domain->geometry.aperture_end);
  320. /* Try to get PCI devices a SAC address */
  321. if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev))
  322. iova = alloc_iova_fast(iovad, iova_len,
  323. DMA_BIT_MASK(32) >> shift, false);
  324. if (!iova)
  325. iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift,
  326. true);
  327. return (dma_addr_t)iova << shift;
  328. }
  329. static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
  330. dma_addr_t iova, size_t size)
  331. {
  332. struct iova_domain *iovad = &cookie->iovad;
  333. /* The MSI case is only ever cleaning up its most recent allocation */
  334. if (cookie->type == IOMMU_DMA_MSI_COOKIE)
  335. cookie->msi_iova -= size;
  336. else
  337. free_iova_fast(iovad, iova_pfn(iovad, iova),
  338. size >> iova_shift(iovad));
  339. }
  340. static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr,
  341. size_t size)
  342. {
  343. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  344. struct iova_domain *iovad = &cookie->iovad;
  345. size_t iova_off = iova_offset(iovad, dma_addr);
  346. dma_addr -= iova_off;
  347. size = iova_align(iovad, size + iova_off);
  348. WARN_ON(iommu_unmap(domain, dma_addr, size) != size);
  349. iommu_dma_free_iova(cookie, dma_addr, size);
  350. }
  351. static void __iommu_dma_free_pages(struct page **pages, int count)
  352. {
  353. while (count--)
  354. __free_page(pages[count]);
  355. kvfree(pages);
  356. }
  357. static struct page **__iommu_dma_alloc_pages(unsigned int count,
  358. unsigned long order_mask, gfp_t gfp)
  359. {
  360. struct page **pages;
  361. unsigned int i = 0, array_size = count * sizeof(*pages);
  362. order_mask &= (2U << MAX_ORDER) - 1;
  363. if (!order_mask)
  364. return NULL;
  365. if (array_size <= PAGE_SIZE)
  366. pages = kzalloc(array_size, GFP_KERNEL);
  367. else
  368. pages = vzalloc(array_size);
  369. if (!pages)
  370. return NULL;
  371. /* IOMMU can map any pages, so himem can also be used here */
  372. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  373. while (count) {
  374. struct page *page = NULL;
  375. unsigned int order_size;
  376. /*
  377. * Higher-order allocations are a convenience rather
  378. * than a necessity, hence using __GFP_NORETRY until
  379. * falling back to minimum-order allocations.
  380. */
  381. for (order_mask &= (2U << __fls(count)) - 1;
  382. order_mask; order_mask &= ~order_size) {
  383. unsigned int order = __fls(order_mask);
  384. order_size = 1U << order;
  385. page = alloc_pages((order_mask - order_size) ?
  386. gfp | __GFP_NORETRY : gfp, order);
  387. if (!page)
  388. continue;
  389. if (!order)
  390. break;
  391. if (!PageCompound(page)) {
  392. split_page(page, order);
  393. break;
  394. } else if (!split_huge_page(page)) {
  395. break;
  396. }
  397. __free_pages(page, order);
  398. }
  399. if (!page) {
  400. __iommu_dma_free_pages(pages, i);
  401. return NULL;
  402. }
  403. count -= order_size;
  404. while (order_size--)
  405. pages[i++] = page++;
  406. }
  407. return pages;
  408. }
  409. /**
  410. * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc()
  411. * @dev: Device which owns this buffer
  412. * @pages: Array of buffer pages as returned by iommu_dma_alloc()
  413. * @size: Size of buffer in bytes
  414. * @handle: DMA address of buffer
  415. *
  416. * Frees both the pages associated with the buffer, and the array
  417. * describing them
  418. */
  419. void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
  420. dma_addr_t *handle)
  421. {
  422. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), *handle, size);
  423. __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
  424. *handle = IOMMU_MAPPING_ERROR;
  425. }
  426. /**
  427. * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space
  428. * @dev: Device to allocate memory for. Must be a real device
  429. * attached to an iommu_dma_domain
  430. * @size: Size of buffer in bytes
  431. * @gfp: Allocation flags
  432. * @attrs: DMA attributes for this allocation
  433. * @prot: IOMMU mapping flags
  434. * @handle: Out argument for allocated DMA handle
  435. * @flush_page: Arch callback which must ensure PAGE_SIZE bytes from the
  436. * given VA/PA are visible to the given non-coherent device.
  437. *
  438. * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
  439. * but an IOMMU which supports smaller pages might not map the whole thing.
  440. *
  441. * Return: Array of struct page pointers describing the buffer,
  442. * or NULL on failure.
  443. */
  444. struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
  445. unsigned long attrs, int prot, dma_addr_t *handle,
  446. void (*flush_page)(struct device *, const void *, phys_addr_t))
  447. {
  448. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  449. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  450. struct iova_domain *iovad = &cookie->iovad;
  451. struct page **pages;
  452. struct sg_table sgt;
  453. dma_addr_t iova;
  454. unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
  455. *handle = IOMMU_MAPPING_ERROR;
  456. min_size = alloc_sizes & -alloc_sizes;
  457. if (min_size < PAGE_SIZE) {
  458. min_size = PAGE_SIZE;
  459. alloc_sizes |= PAGE_SIZE;
  460. } else {
  461. size = ALIGN(size, min_size);
  462. }
  463. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  464. alloc_sizes = min_size;
  465. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  466. pages = __iommu_dma_alloc_pages(count, alloc_sizes >> PAGE_SHIFT, gfp);
  467. if (!pages)
  468. return NULL;
  469. size = iova_align(iovad, size);
  470. iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
  471. if (!iova)
  472. goto out_free_pages;
  473. if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
  474. goto out_free_iova;
  475. if (!(prot & IOMMU_CACHE)) {
  476. struct sg_mapping_iter miter;
  477. /*
  478. * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
  479. * sufficient here, so skip it by using the "wrong" direction.
  480. */
  481. sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG);
  482. while (sg_miter_next(&miter))
  483. flush_page(dev, miter.addr, page_to_phys(miter.page));
  484. sg_miter_stop(&miter);
  485. }
  486. if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, prot)
  487. < size)
  488. goto out_free_sg;
  489. *handle = iova;
  490. sg_free_table(&sgt);
  491. return pages;
  492. out_free_sg:
  493. sg_free_table(&sgt);
  494. out_free_iova:
  495. iommu_dma_free_iova(cookie, iova, size);
  496. out_free_pages:
  497. __iommu_dma_free_pages(pages, count);
  498. return NULL;
  499. }
  500. /**
  501. * iommu_dma_mmap - Map a buffer into provided user VMA
  502. * @pages: Array representing buffer from iommu_dma_alloc()
  503. * @size: Size of buffer in bytes
  504. * @vma: VMA describing requested userspace mapping
  505. *
  506. * Maps the pages of the buffer in @pages into @vma. The caller is responsible
  507. * for verifying the correct size and protection of @vma beforehand.
  508. */
  509. int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
  510. {
  511. unsigned long uaddr = vma->vm_start;
  512. unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  513. int ret = -ENXIO;
  514. for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
  515. ret = vm_insert_page(vma, uaddr, pages[i]);
  516. if (ret)
  517. break;
  518. uaddr += PAGE_SIZE;
  519. }
  520. return ret;
  521. }
  522. static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
  523. size_t size, int prot)
  524. {
  525. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  526. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  527. size_t iova_off = 0;
  528. dma_addr_t iova;
  529. if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
  530. iova_off = iova_offset(&cookie->iovad, phys);
  531. size = iova_align(&cookie->iovad, size + iova_off);
  532. }
  533. iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
  534. if (!iova)
  535. return IOMMU_MAPPING_ERROR;
  536. if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
  537. iommu_dma_free_iova(cookie, iova, size);
  538. return IOMMU_MAPPING_ERROR;
  539. }
  540. return iova + iova_off;
  541. }
  542. dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
  543. unsigned long offset, size_t size, int prot)
  544. {
  545. return __iommu_dma_map(dev, page_to_phys(page) + offset, size, prot);
  546. }
  547. void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
  548. enum dma_data_direction dir, unsigned long attrs)
  549. {
  550. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size);
  551. }
  552. /*
  553. * Prepare a successfully-mapped scatterlist to give back to the caller.
  554. *
  555. * At this point the segments are already laid out by iommu_dma_map_sg() to
  556. * avoid individually crossing any boundaries, so we merely need to check a
  557. * segment's start address to avoid concatenating across one.
  558. */
  559. static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
  560. dma_addr_t dma_addr)
  561. {
  562. struct scatterlist *s, *cur = sg;
  563. unsigned long seg_mask = dma_get_seg_boundary(dev);
  564. unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
  565. int i, count = 0;
  566. for_each_sg(sg, s, nents, i) {
  567. /* Restore this segment's original unaligned fields first */
  568. unsigned int s_iova_off = sg_dma_address(s);
  569. unsigned int s_length = sg_dma_len(s);
  570. unsigned int s_iova_len = s->length;
  571. s->offset += s_iova_off;
  572. s->length = s_length;
  573. sg_dma_address(s) = IOMMU_MAPPING_ERROR;
  574. sg_dma_len(s) = 0;
  575. /*
  576. * Now fill in the real DMA data. If...
  577. * - there is a valid output segment to append to
  578. * - and this segment starts on an IOVA page boundary
  579. * - but doesn't fall at a segment boundary
  580. * - and wouldn't make the resulting output segment too long
  581. */
  582. if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
  583. (max_len - cur_len >= s_length)) {
  584. /* ...then concatenate it with the previous one */
  585. cur_len += s_length;
  586. } else {
  587. /* Otherwise start the next output segment */
  588. if (i > 0)
  589. cur = sg_next(cur);
  590. cur_len = s_length;
  591. count++;
  592. sg_dma_address(cur) = dma_addr + s_iova_off;
  593. }
  594. sg_dma_len(cur) = cur_len;
  595. dma_addr += s_iova_len;
  596. if (s_length + s_iova_off < s_iova_len)
  597. cur_len = 0;
  598. }
  599. return count;
  600. }
  601. /*
  602. * If mapping failed, then just restore the original list,
  603. * but making sure the DMA fields are invalidated.
  604. */
  605. static void __invalidate_sg(struct scatterlist *sg, int nents)
  606. {
  607. struct scatterlist *s;
  608. int i;
  609. for_each_sg(sg, s, nents, i) {
  610. if (sg_dma_address(s) != IOMMU_MAPPING_ERROR)
  611. s->offset += sg_dma_address(s);
  612. if (sg_dma_len(s))
  613. s->length = sg_dma_len(s);
  614. sg_dma_address(s) = IOMMU_MAPPING_ERROR;
  615. sg_dma_len(s) = 0;
  616. }
  617. }
  618. /*
  619. * The DMA API client is passing in a scatterlist which could describe
  620. * any old buffer layout, but the IOMMU API requires everything to be
  621. * aligned to IOMMU pages. Hence the need for this complicated bit of
  622. * impedance-matching, to be able to hand off a suitably-aligned list,
  623. * but still preserve the original offsets and sizes for the caller.
  624. */
  625. int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
  626. int nents, int prot)
  627. {
  628. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  629. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  630. struct iova_domain *iovad = &cookie->iovad;
  631. struct scatterlist *s, *prev = NULL;
  632. dma_addr_t iova;
  633. size_t iova_len = 0;
  634. unsigned long mask = dma_get_seg_boundary(dev);
  635. int i;
  636. /*
  637. * Work out how much IOVA space we need, and align the segments to
  638. * IOVA granules for the IOMMU driver to handle. With some clever
  639. * trickery we can modify the list in-place, but reversibly, by
  640. * stashing the unaligned parts in the as-yet-unused DMA fields.
  641. */
  642. for_each_sg(sg, s, nents, i) {
  643. size_t s_iova_off = iova_offset(iovad, s->offset);
  644. size_t s_length = s->length;
  645. size_t pad_len = (mask - iova_len + 1) & mask;
  646. sg_dma_address(s) = s_iova_off;
  647. sg_dma_len(s) = s_length;
  648. s->offset -= s_iova_off;
  649. s_length = iova_align(iovad, s_length + s_iova_off);
  650. s->length = s_length;
  651. /*
  652. * Due to the alignment of our single IOVA allocation, we can
  653. * depend on these assumptions about the segment boundary mask:
  654. * - If mask size >= IOVA size, then the IOVA range cannot
  655. * possibly fall across a boundary, so we don't care.
  656. * - If mask size < IOVA size, then the IOVA range must start
  657. * exactly on a boundary, therefore we can lay things out
  658. * based purely on segment lengths without needing to know
  659. * the actual addresses beforehand.
  660. * - The mask must be a power of 2, so pad_len == 0 if
  661. * iova_len == 0, thus we cannot dereference prev the first
  662. * time through here (i.e. before it has a meaningful value).
  663. */
  664. if (pad_len && pad_len < s_length - 1) {
  665. prev->length += pad_len;
  666. iova_len += pad_len;
  667. }
  668. iova_len += s_length;
  669. prev = s;
  670. }
  671. iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
  672. if (!iova)
  673. goto out_restore_sg;
  674. /*
  675. * We'll leave any physical concatenation to the IOMMU driver's
  676. * implementation - it knows better than we do.
  677. */
  678. if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len)
  679. goto out_free_iova;
  680. return __finalise_sg(dev, sg, nents, iova);
  681. out_free_iova:
  682. iommu_dma_free_iova(cookie, iova, iova_len);
  683. out_restore_sg:
  684. __invalidate_sg(sg, nents);
  685. return 0;
  686. }
  687. void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  688. enum dma_data_direction dir, unsigned long attrs)
  689. {
  690. dma_addr_t start, end;
  691. struct scatterlist *tmp;
  692. int i;
  693. /*
  694. * The scatterlist segments are mapped into a single
  695. * contiguous IOVA allocation, so this is incredibly easy.
  696. */
  697. start = sg_dma_address(sg);
  698. for_each_sg(sg_next(sg), tmp, nents - 1, i) {
  699. if (sg_dma_len(tmp) == 0)
  700. break;
  701. sg = tmp;
  702. }
  703. end = sg_dma_address(sg) + sg_dma_len(sg);
  704. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), start, end - start);
  705. }
  706. dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
  707. size_t size, enum dma_data_direction dir, unsigned long attrs)
  708. {
  709. return __iommu_dma_map(dev, phys, size,
  710. dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO);
  711. }
  712. void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
  713. size_t size, enum dma_data_direction dir, unsigned long attrs)
  714. {
  715. __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size);
  716. }
  717. int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  718. {
  719. return dma_addr == IOMMU_MAPPING_ERROR;
  720. }
  721. static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
  722. phys_addr_t msi_addr, struct iommu_domain *domain)
  723. {
  724. struct iommu_dma_cookie *cookie = domain->iova_cookie;
  725. struct iommu_dma_msi_page *msi_page;
  726. dma_addr_t iova;
  727. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  728. size_t size = cookie_msi_granule(cookie);
  729. msi_addr &= ~(phys_addr_t)(size - 1);
  730. list_for_each_entry(msi_page, &cookie->msi_page_list, list)
  731. if (msi_page->phys == msi_addr)
  732. return msi_page;
  733. msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC);
  734. if (!msi_page)
  735. return NULL;
  736. iova = __iommu_dma_map(dev, msi_addr, size, prot);
  737. if (iommu_dma_mapping_error(dev, iova))
  738. goto out_free_page;
  739. INIT_LIST_HEAD(&msi_page->list);
  740. msi_page->phys = msi_addr;
  741. msi_page->iova = iova;
  742. list_add(&msi_page->list, &cookie->msi_page_list);
  743. return msi_page;
  744. out_free_page:
  745. kfree(msi_page);
  746. return NULL;
  747. }
  748. void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg)
  749. {
  750. struct device *dev = msi_desc_to_dev(irq_get_msi_desc(irq));
  751. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  752. struct iommu_dma_cookie *cookie;
  753. struct iommu_dma_msi_page *msi_page;
  754. phys_addr_t msi_addr = (u64)msg->address_hi << 32 | msg->address_lo;
  755. unsigned long flags;
  756. if (!domain || !domain->iova_cookie)
  757. return;
  758. cookie = domain->iova_cookie;
  759. /*
  760. * We disable IRQs to rule out a possible inversion against
  761. * irq_desc_lock if, say, someone tries to retarget the affinity
  762. * of an MSI from within an IPI handler.
  763. */
  764. spin_lock_irqsave(&cookie->msi_lock, flags);
  765. msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
  766. spin_unlock_irqrestore(&cookie->msi_lock, flags);
  767. if (WARN_ON(!msi_page)) {
  768. /*
  769. * We're called from a void callback, so the best we can do is
  770. * 'fail' by filling the message with obviously bogus values.
  771. * Since we got this far due to an IOMMU being present, it's
  772. * not like the existing address would have worked anyway...
  773. */
  774. msg->address_hi = ~0U;
  775. msg->address_lo = ~0U;
  776. msg->data = ~0U;
  777. } else {
  778. msg->address_hi = upper_32_bits(msi_page->iova);
  779. msg->address_lo &= cookie_msi_granule(cookie) - 1;
  780. msg->address_lo += lower_32_bits(msi_page->iova);
  781. }
  782. }