dma-mapping.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * SWIOTLB-based DMA API implementation
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/gfp.h>
  20. #include <linux/acpi.h>
  21. #include <linux/export.h>
  22. #include <linux/slab.h>
  23. #include <linux/genalloc.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/dma-contiguous.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/swiotlb.h>
  28. #include <asm/cacheflush.h>
  29. static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
  30. bool coherent)
  31. {
  32. if (!coherent || dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
  33. return pgprot_writecombine(prot);
  34. return prot;
  35. }
  36. static struct gen_pool *atomic_pool;
  37. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  38. static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
  39. static int __init early_coherent_pool(char *p)
  40. {
  41. atomic_pool_size = memparse(p, &p);
  42. return 0;
  43. }
  44. early_param("coherent_pool", early_coherent_pool);
  45. static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
  46. {
  47. unsigned long val;
  48. void *ptr = NULL;
  49. if (!atomic_pool) {
  50. WARN(1, "coherent pool not initialised!\n");
  51. return NULL;
  52. }
  53. val = gen_pool_alloc(atomic_pool, size);
  54. if (val) {
  55. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  56. *ret_page = phys_to_page(phys);
  57. ptr = (void *)val;
  58. memset(ptr, 0, size);
  59. }
  60. return ptr;
  61. }
  62. static bool __in_atomic_pool(void *start, size_t size)
  63. {
  64. return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
  65. }
  66. static int __free_from_pool(void *start, size_t size)
  67. {
  68. if (!__in_atomic_pool(start, size))
  69. return 0;
  70. gen_pool_free(atomic_pool, (unsigned long)start, size);
  71. return 1;
  72. }
  73. static void *__dma_alloc_coherent(struct device *dev, size_t size,
  74. dma_addr_t *dma_handle, gfp_t flags,
  75. struct dma_attrs *attrs)
  76. {
  77. if (dev == NULL) {
  78. WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
  79. return NULL;
  80. }
  81. if (IS_ENABLED(CONFIG_ZONE_DMA) &&
  82. dev->coherent_dma_mask <= DMA_BIT_MASK(32))
  83. flags |= GFP_DMA;
  84. if (dev_get_cma_area(dev) && gfpflags_allow_blocking(flags)) {
  85. struct page *page;
  86. void *addr;
  87. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  88. get_order(size));
  89. if (!page)
  90. return NULL;
  91. *dma_handle = phys_to_dma(dev, page_to_phys(page));
  92. addr = page_address(page);
  93. memset(addr, 0, size);
  94. return addr;
  95. } else {
  96. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  97. }
  98. }
  99. static void __dma_free_coherent(struct device *dev, size_t size,
  100. void *vaddr, dma_addr_t dma_handle,
  101. struct dma_attrs *attrs)
  102. {
  103. bool freed;
  104. phys_addr_t paddr = dma_to_phys(dev, dma_handle);
  105. if (dev == NULL) {
  106. WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
  107. return;
  108. }
  109. freed = dma_release_from_contiguous(dev,
  110. phys_to_page(paddr),
  111. size >> PAGE_SHIFT);
  112. if (!freed)
  113. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  114. }
  115. static void *__dma_alloc(struct device *dev, size_t size,
  116. dma_addr_t *dma_handle, gfp_t flags,
  117. struct dma_attrs *attrs)
  118. {
  119. struct page *page;
  120. void *ptr, *coherent_ptr;
  121. bool coherent = is_device_dma_coherent(dev);
  122. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
  123. size = PAGE_ALIGN(size);
  124. if (!coherent && !gfpflags_allow_blocking(flags)) {
  125. struct page *page = NULL;
  126. void *addr = __alloc_from_pool(size, &page, flags);
  127. if (addr)
  128. *dma_handle = phys_to_dma(dev, page_to_phys(page));
  129. return addr;
  130. }
  131. ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
  132. if (!ptr)
  133. goto no_mem;
  134. /* no need for non-cacheable mapping if coherent */
  135. if (coherent)
  136. return ptr;
  137. /* remove any dirty cache lines on the kernel alias */
  138. __dma_flush_range(ptr, ptr + size);
  139. /* create a coherent mapping */
  140. page = virt_to_page(ptr);
  141. coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
  142. prot, NULL);
  143. if (!coherent_ptr)
  144. goto no_map;
  145. return coherent_ptr;
  146. no_map:
  147. __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
  148. no_mem:
  149. *dma_handle = DMA_ERROR_CODE;
  150. return NULL;
  151. }
  152. static void __dma_free(struct device *dev, size_t size,
  153. void *vaddr, dma_addr_t dma_handle,
  154. struct dma_attrs *attrs)
  155. {
  156. void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
  157. size = PAGE_ALIGN(size);
  158. if (!is_device_dma_coherent(dev)) {
  159. if (__free_from_pool(vaddr, size))
  160. return;
  161. vunmap(vaddr);
  162. }
  163. __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
  164. }
  165. static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
  166. unsigned long offset, size_t size,
  167. enum dma_data_direction dir,
  168. struct dma_attrs *attrs)
  169. {
  170. dma_addr_t dev_addr;
  171. dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
  172. if (!is_device_dma_coherent(dev))
  173. __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  174. return dev_addr;
  175. }
  176. static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
  177. size_t size, enum dma_data_direction dir,
  178. struct dma_attrs *attrs)
  179. {
  180. if (!is_device_dma_coherent(dev))
  181. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  182. swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
  183. }
  184. static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  185. int nelems, enum dma_data_direction dir,
  186. struct dma_attrs *attrs)
  187. {
  188. struct scatterlist *sg;
  189. int i, ret;
  190. ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
  191. if (!is_device_dma_coherent(dev))
  192. for_each_sg(sgl, sg, ret, i)
  193. __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  194. sg->length, dir);
  195. return ret;
  196. }
  197. static void __swiotlb_unmap_sg_attrs(struct device *dev,
  198. struct scatterlist *sgl, int nelems,
  199. enum dma_data_direction dir,
  200. struct dma_attrs *attrs)
  201. {
  202. struct scatterlist *sg;
  203. int i;
  204. if (!is_device_dma_coherent(dev))
  205. for_each_sg(sgl, sg, nelems, i)
  206. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  207. sg->length, dir);
  208. swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
  209. }
  210. static void __swiotlb_sync_single_for_cpu(struct device *dev,
  211. dma_addr_t dev_addr, size_t size,
  212. enum dma_data_direction dir)
  213. {
  214. if (!is_device_dma_coherent(dev))
  215. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  216. swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
  217. }
  218. static void __swiotlb_sync_single_for_device(struct device *dev,
  219. dma_addr_t dev_addr, size_t size,
  220. enum dma_data_direction dir)
  221. {
  222. swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
  223. if (!is_device_dma_coherent(dev))
  224. __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  225. }
  226. static void __swiotlb_sync_sg_for_cpu(struct device *dev,
  227. struct scatterlist *sgl, int nelems,
  228. enum dma_data_direction dir)
  229. {
  230. struct scatterlist *sg;
  231. int i;
  232. if (!is_device_dma_coherent(dev))
  233. for_each_sg(sgl, sg, nelems, i)
  234. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  235. sg->length, dir);
  236. swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
  237. }
  238. static void __swiotlb_sync_sg_for_device(struct device *dev,
  239. struct scatterlist *sgl, int nelems,
  240. enum dma_data_direction dir)
  241. {
  242. struct scatterlist *sg;
  243. int i;
  244. swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
  245. if (!is_device_dma_coherent(dev))
  246. for_each_sg(sgl, sg, nelems, i)
  247. __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  248. sg->length, dir);
  249. }
  250. static int __swiotlb_mmap(struct device *dev,
  251. struct vm_area_struct *vma,
  252. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  253. struct dma_attrs *attrs)
  254. {
  255. int ret = -ENXIO;
  256. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
  257. PAGE_SHIFT;
  258. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  259. unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
  260. unsigned long off = vma->vm_pgoff;
  261. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
  262. is_device_dma_coherent(dev));
  263. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  264. return ret;
  265. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  266. ret = remap_pfn_range(vma, vma->vm_start,
  267. pfn + off,
  268. vma->vm_end - vma->vm_start,
  269. vma->vm_page_prot);
  270. }
  271. return ret;
  272. }
  273. static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
  274. void *cpu_addr, dma_addr_t handle, size_t size,
  275. struct dma_attrs *attrs)
  276. {
  277. int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  278. if (!ret)
  279. sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
  280. PAGE_ALIGN(size), 0);
  281. return ret;
  282. }
  283. static struct dma_map_ops swiotlb_dma_ops = {
  284. .alloc = __dma_alloc,
  285. .free = __dma_free,
  286. .mmap = __swiotlb_mmap,
  287. .get_sgtable = __swiotlb_get_sgtable,
  288. .map_page = __swiotlb_map_page,
  289. .unmap_page = __swiotlb_unmap_page,
  290. .map_sg = __swiotlb_map_sg_attrs,
  291. .unmap_sg = __swiotlb_unmap_sg_attrs,
  292. .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
  293. .sync_single_for_device = __swiotlb_sync_single_for_device,
  294. .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
  295. .sync_sg_for_device = __swiotlb_sync_sg_for_device,
  296. .dma_supported = swiotlb_dma_supported,
  297. .mapping_error = swiotlb_dma_mapping_error,
  298. };
  299. static int __init atomic_pool_init(void)
  300. {
  301. pgprot_t prot = __pgprot(PROT_NORMAL_NC);
  302. unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
  303. struct page *page;
  304. void *addr;
  305. unsigned int pool_size_order = get_order(atomic_pool_size);
  306. if (dev_get_cma_area(NULL))
  307. page = dma_alloc_from_contiguous(NULL, nr_pages,
  308. pool_size_order);
  309. else
  310. page = alloc_pages(GFP_DMA, pool_size_order);
  311. if (page) {
  312. int ret;
  313. void *page_addr = page_address(page);
  314. memset(page_addr, 0, atomic_pool_size);
  315. __dma_flush_range(page_addr, page_addr + atomic_pool_size);
  316. atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
  317. if (!atomic_pool)
  318. goto free_page;
  319. addr = dma_common_contiguous_remap(page, atomic_pool_size,
  320. VM_USERMAP, prot, atomic_pool_init);
  321. if (!addr)
  322. goto destroy_genpool;
  323. ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
  324. page_to_phys(page),
  325. atomic_pool_size, -1);
  326. if (ret)
  327. goto remove_mapping;
  328. gen_pool_set_algo(atomic_pool,
  329. gen_pool_first_fit_order_align,
  330. (void *)PAGE_SHIFT);
  331. pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
  332. atomic_pool_size / 1024);
  333. return 0;
  334. }
  335. goto out;
  336. remove_mapping:
  337. dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
  338. destroy_genpool:
  339. gen_pool_destroy(atomic_pool);
  340. atomic_pool = NULL;
  341. free_page:
  342. if (!dma_release_from_contiguous(NULL, page, nr_pages))
  343. __free_pages(page, pool_size_order);
  344. out:
  345. pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
  346. atomic_pool_size / 1024);
  347. return -ENOMEM;
  348. }
  349. /********************************************
  350. * The following APIs are for dummy DMA ops *
  351. ********************************************/
  352. static void *__dummy_alloc(struct device *dev, size_t size,
  353. dma_addr_t *dma_handle, gfp_t flags,
  354. struct dma_attrs *attrs)
  355. {
  356. return NULL;
  357. }
  358. static void __dummy_free(struct device *dev, size_t size,
  359. void *vaddr, dma_addr_t dma_handle,
  360. struct dma_attrs *attrs)
  361. {
  362. }
  363. static int __dummy_mmap(struct device *dev,
  364. struct vm_area_struct *vma,
  365. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  366. struct dma_attrs *attrs)
  367. {
  368. return -ENXIO;
  369. }
  370. static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
  371. unsigned long offset, size_t size,
  372. enum dma_data_direction dir,
  373. struct dma_attrs *attrs)
  374. {
  375. return DMA_ERROR_CODE;
  376. }
  377. static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
  378. size_t size, enum dma_data_direction dir,
  379. struct dma_attrs *attrs)
  380. {
  381. }
  382. static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
  383. int nelems, enum dma_data_direction dir,
  384. struct dma_attrs *attrs)
  385. {
  386. return 0;
  387. }
  388. static void __dummy_unmap_sg(struct device *dev,
  389. struct scatterlist *sgl, int nelems,
  390. enum dma_data_direction dir,
  391. struct dma_attrs *attrs)
  392. {
  393. }
  394. static void __dummy_sync_single(struct device *dev,
  395. dma_addr_t dev_addr, size_t size,
  396. enum dma_data_direction dir)
  397. {
  398. }
  399. static void __dummy_sync_sg(struct device *dev,
  400. struct scatterlist *sgl, int nelems,
  401. enum dma_data_direction dir)
  402. {
  403. }
  404. static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
  405. {
  406. return 1;
  407. }
  408. static int __dummy_dma_supported(struct device *hwdev, u64 mask)
  409. {
  410. return 0;
  411. }
  412. struct dma_map_ops dummy_dma_ops = {
  413. .alloc = __dummy_alloc,
  414. .free = __dummy_free,
  415. .mmap = __dummy_mmap,
  416. .map_page = __dummy_map_page,
  417. .unmap_page = __dummy_unmap_page,
  418. .map_sg = __dummy_map_sg,
  419. .unmap_sg = __dummy_unmap_sg,
  420. .sync_single_for_cpu = __dummy_sync_single,
  421. .sync_single_for_device = __dummy_sync_single,
  422. .sync_sg_for_cpu = __dummy_sync_sg,
  423. .sync_sg_for_device = __dummy_sync_sg,
  424. .mapping_error = __dummy_mapping_error,
  425. .dma_supported = __dummy_dma_supported,
  426. };
  427. EXPORT_SYMBOL(dummy_dma_ops);
  428. static int __init arm64_dma_init(void)
  429. {
  430. return atomic_pool_init();
  431. }
  432. arch_initcall(arm64_dma_init);
  433. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  434. static int __init dma_debug_do_init(void)
  435. {
  436. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  437. return 0;
  438. }
  439. fs_initcall(dma_debug_do_init);
  440. #ifdef CONFIG_IOMMU_DMA
  441. #include <linux/dma-iommu.h>
  442. #include <linux/platform_device.h>
  443. #include <linux/amba/bus.h>
  444. /* Thankfully, all cache ops are by VA so we can ignore phys here */
  445. static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
  446. {
  447. __dma_flush_range(virt, virt + PAGE_SIZE);
  448. }
  449. static void *__iommu_alloc_attrs(struct device *dev, size_t size,
  450. dma_addr_t *handle, gfp_t gfp,
  451. struct dma_attrs *attrs)
  452. {
  453. bool coherent = is_device_dma_coherent(dev);
  454. int ioprot = dma_direction_to_prot(DMA_BIDIRECTIONAL, coherent);
  455. size_t iosize = size;
  456. void *addr;
  457. if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
  458. return NULL;
  459. size = PAGE_ALIGN(size);
  460. /*
  461. * Some drivers rely on this, and we probably don't want the
  462. * possibility of stale kernel data being read by devices anyway.
  463. */
  464. gfp |= __GFP_ZERO;
  465. if (gfpflags_allow_blocking(gfp)) {
  466. struct page **pages;
  467. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent);
  468. pages = iommu_dma_alloc(dev, iosize, gfp, ioprot, handle,
  469. flush_page);
  470. if (!pages)
  471. return NULL;
  472. addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
  473. __builtin_return_address(0));
  474. if (!addr)
  475. iommu_dma_free(dev, pages, iosize, handle);
  476. } else {
  477. struct page *page;
  478. /*
  479. * In atomic context we can't remap anything, so we'll only
  480. * get the virtually contiguous buffer we need by way of a
  481. * physically contiguous allocation.
  482. */
  483. if (coherent) {
  484. page = alloc_pages(gfp, get_order(size));
  485. addr = page ? page_address(page) : NULL;
  486. } else {
  487. addr = __alloc_from_pool(size, &page, gfp);
  488. }
  489. if (!addr)
  490. return NULL;
  491. *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
  492. if (iommu_dma_mapping_error(dev, *handle)) {
  493. if (coherent)
  494. __free_pages(page, get_order(size));
  495. else
  496. __free_from_pool(addr, size);
  497. addr = NULL;
  498. }
  499. }
  500. return addr;
  501. }
  502. static void __iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  503. dma_addr_t handle, struct dma_attrs *attrs)
  504. {
  505. size_t iosize = size;
  506. size = PAGE_ALIGN(size);
  507. /*
  508. * @cpu_addr will be one of 3 things depending on how it was allocated:
  509. * - A remapped array of pages from iommu_dma_alloc(), for all
  510. * non-atomic allocations.
  511. * - A non-cacheable alias from the atomic pool, for atomic
  512. * allocations by non-coherent devices.
  513. * - A normal lowmem address, for atomic allocations by
  514. * coherent devices.
  515. * Hence how dodgy the below logic looks...
  516. */
  517. if (__in_atomic_pool(cpu_addr, size)) {
  518. iommu_dma_unmap_page(dev, handle, iosize, 0, NULL);
  519. __free_from_pool(cpu_addr, size);
  520. } else if (is_vmalloc_addr(cpu_addr)){
  521. struct vm_struct *area = find_vm_area(cpu_addr);
  522. if (WARN_ON(!area || !area->pages))
  523. return;
  524. iommu_dma_free(dev, area->pages, iosize, &handle);
  525. dma_common_free_remap(cpu_addr, size, VM_USERMAP);
  526. } else {
  527. iommu_dma_unmap_page(dev, handle, iosize, 0, NULL);
  528. __free_pages(virt_to_page(cpu_addr), get_order(size));
  529. }
  530. }
  531. static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  532. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  533. struct dma_attrs *attrs)
  534. {
  535. struct vm_struct *area;
  536. int ret;
  537. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
  538. is_device_dma_coherent(dev));
  539. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  540. return ret;
  541. area = find_vm_area(cpu_addr);
  542. if (WARN_ON(!area || !area->pages))
  543. return -ENXIO;
  544. return iommu_dma_mmap(area->pages, size, vma);
  545. }
  546. static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  547. void *cpu_addr, dma_addr_t dma_addr,
  548. size_t size, struct dma_attrs *attrs)
  549. {
  550. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  551. struct vm_struct *area = find_vm_area(cpu_addr);
  552. if (WARN_ON(!area || !area->pages))
  553. return -ENXIO;
  554. return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
  555. GFP_KERNEL);
  556. }
  557. static void __iommu_sync_single_for_cpu(struct device *dev,
  558. dma_addr_t dev_addr, size_t size,
  559. enum dma_data_direction dir)
  560. {
  561. phys_addr_t phys;
  562. if (is_device_dma_coherent(dev))
  563. return;
  564. phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
  565. __dma_unmap_area(phys_to_virt(phys), size, dir);
  566. }
  567. static void __iommu_sync_single_for_device(struct device *dev,
  568. dma_addr_t dev_addr, size_t size,
  569. enum dma_data_direction dir)
  570. {
  571. phys_addr_t phys;
  572. if (is_device_dma_coherent(dev))
  573. return;
  574. phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
  575. __dma_map_area(phys_to_virt(phys), size, dir);
  576. }
  577. static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
  578. unsigned long offset, size_t size,
  579. enum dma_data_direction dir,
  580. struct dma_attrs *attrs)
  581. {
  582. bool coherent = is_device_dma_coherent(dev);
  583. int prot = dma_direction_to_prot(dir, coherent);
  584. dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
  585. if (!iommu_dma_mapping_error(dev, dev_addr) &&
  586. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  587. __iommu_sync_single_for_device(dev, dev_addr, size, dir);
  588. return dev_addr;
  589. }
  590. static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
  591. size_t size, enum dma_data_direction dir,
  592. struct dma_attrs *attrs)
  593. {
  594. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  595. __iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
  596. iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
  597. }
  598. static void __iommu_sync_sg_for_cpu(struct device *dev,
  599. struct scatterlist *sgl, int nelems,
  600. enum dma_data_direction dir)
  601. {
  602. struct scatterlist *sg;
  603. int i;
  604. if (is_device_dma_coherent(dev))
  605. return;
  606. for_each_sg(sgl, sg, nelems, i)
  607. __dma_unmap_area(sg_virt(sg), sg->length, dir);
  608. }
  609. static void __iommu_sync_sg_for_device(struct device *dev,
  610. struct scatterlist *sgl, int nelems,
  611. enum dma_data_direction dir)
  612. {
  613. struct scatterlist *sg;
  614. int i;
  615. if (is_device_dma_coherent(dev))
  616. return;
  617. for_each_sg(sgl, sg, nelems, i)
  618. __dma_map_area(sg_virt(sg), sg->length, dir);
  619. }
  620. static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  621. int nelems, enum dma_data_direction dir,
  622. struct dma_attrs *attrs)
  623. {
  624. bool coherent = is_device_dma_coherent(dev);
  625. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  626. __iommu_sync_sg_for_device(dev, sgl, nelems, dir);
  627. return iommu_dma_map_sg(dev, sgl, nelems,
  628. dma_direction_to_prot(dir, coherent));
  629. }
  630. static void __iommu_unmap_sg_attrs(struct device *dev,
  631. struct scatterlist *sgl, int nelems,
  632. enum dma_data_direction dir,
  633. struct dma_attrs *attrs)
  634. {
  635. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  636. __iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
  637. iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
  638. }
  639. static struct dma_map_ops iommu_dma_ops = {
  640. .alloc = __iommu_alloc_attrs,
  641. .free = __iommu_free_attrs,
  642. .mmap = __iommu_mmap_attrs,
  643. .get_sgtable = __iommu_get_sgtable,
  644. .map_page = __iommu_map_page,
  645. .unmap_page = __iommu_unmap_page,
  646. .map_sg = __iommu_map_sg_attrs,
  647. .unmap_sg = __iommu_unmap_sg_attrs,
  648. .sync_single_for_cpu = __iommu_sync_single_for_cpu,
  649. .sync_single_for_device = __iommu_sync_single_for_device,
  650. .sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
  651. .sync_sg_for_device = __iommu_sync_sg_for_device,
  652. .dma_supported = iommu_dma_supported,
  653. .mapping_error = iommu_dma_mapping_error,
  654. };
  655. /*
  656. * TODO: Right now __iommu_setup_dma_ops() gets called too early to do
  657. * everything it needs to - the device is only partially created and the
  658. * IOMMU driver hasn't seen it yet, so it can't have a group. Thus we
  659. * need this delayed attachment dance. Once IOMMU probe ordering is sorted
  660. * to move the arch_setup_dma_ops() call later, all the notifier bits below
  661. * become unnecessary, and will go away.
  662. */
  663. struct iommu_dma_notifier_data {
  664. struct list_head list;
  665. struct device *dev;
  666. const struct iommu_ops *ops;
  667. u64 dma_base;
  668. u64 size;
  669. };
  670. static LIST_HEAD(iommu_dma_masters);
  671. static DEFINE_MUTEX(iommu_dma_notifier_lock);
  672. /*
  673. * Temporarily "borrow" a domain feature flag to to tell if we had to resort
  674. * to creating our own domain here, in case we need to clean it up again.
  675. */
  676. #define __IOMMU_DOMAIN_FAKE_DEFAULT (1U << 31)
  677. static bool do_iommu_attach(struct device *dev, const struct iommu_ops *ops,
  678. u64 dma_base, u64 size)
  679. {
  680. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  681. /*
  682. * Best case: The device is either part of a group which was
  683. * already attached to a domain in a previous call, or it's
  684. * been put in a default DMA domain by the IOMMU core.
  685. */
  686. if (!domain) {
  687. /*
  688. * Urgh. The IOMMU core isn't going to do default domains
  689. * for non-PCI devices anyway, until it has some means of
  690. * abstracting the entirely implementation-specific
  691. * sideband data/SoC topology/unicorn dust that may or
  692. * may not differentiate upstream masters.
  693. * So until then, HORRIBLE HACKS!
  694. */
  695. domain = ops->domain_alloc(IOMMU_DOMAIN_DMA);
  696. if (!domain)
  697. goto out_no_domain;
  698. domain->ops = ops;
  699. domain->type = IOMMU_DOMAIN_DMA | __IOMMU_DOMAIN_FAKE_DEFAULT;
  700. if (iommu_attach_device(domain, dev))
  701. goto out_put_domain;
  702. }
  703. if (iommu_dma_init_domain(domain, dma_base, size))
  704. goto out_detach;
  705. dev->archdata.dma_ops = &iommu_dma_ops;
  706. return true;
  707. out_detach:
  708. iommu_detach_device(domain, dev);
  709. out_put_domain:
  710. if (domain->type & __IOMMU_DOMAIN_FAKE_DEFAULT)
  711. iommu_domain_free(domain);
  712. out_no_domain:
  713. pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
  714. dev_name(dev));
  715. return false;
  716. }
  717. static void queue_iommu_attach(struct device *dev, const struct iommu_ops *ops,
  718. u64 dma_base, u64 size)
  719. {
  720. struct iommu_dma_notifier_data *iommudata;
  721. iommudata = kzalloc(sizeof(*iommudata), GFP_KERNEL);
  722. if (!iommudata)
  723. return;
  724. iommudata->dev = dev;
  725. iommudata->ops = ops;
  726. iommudata->dma_base = dma_base;
  727. iommudata->size = size;
  728. mutex_lock(&iommu_dma_notifier_lock);
  729. list_add(&iommudata->list, &iommu_dma_masters);
  730. mutex_unlock(&iommu_dma_notifier_lock);
  731. }
  732. static int __iommu_attach_notifier(struct notifier_block *nb,
  733. unsigned long action, void *data)
  734. {
  735. struct iommu_dma_notifier_data *master, *tmp;
  736. if (action != BUS_NOTIFY_ADD_DEVICE)
  737. return 0;
  738. mutex_lock(&iommu_dma_notifier_lock);
  739. list_for_each_entry_safe(master, tmp, &iommu_dma_masters, list) {
  740. if (do_iommu_attach(master->dev, master->ops,
  741. master->dma_base, master->size)) {
  742. list_del(&master->list);
  743. kfree(master);
  744. }
  745. }
  746. mutex_unlock(&iommu_dma_notifier_lock);
  747. return 0;
  748. }
  749. static int register_iommu_dma_ops_notifier(struct bus_type *bus)
  750. {
  751. struct notifier_block *nb = kzalloc(sizeof(*nb), GFP_KERNEL);
  752. int ret;
  753. if (!nb)
  754. return -ENOMEM;
  755. /*
  756. * The device must be attached to a domain before the driver probe
  757. * routine gets a chance to start allocating DMA buffers. However,
  758. * the IOMMU driver also needs a chance to configure the iommu_group
  759. * via its add_device callback first, so we need to make the attach
  760. * happen between those two points. Since the IOMMU core uses a bus
  761. * notifier with default priority for add_device, do the same but
  762. * with a lower priority to ensure the appropriate ordering.
  763. */
  764. nb->notifier_call = __iommu_attach_notifier;
  765. nb->priority = -100;
  766. ret = bus_register_notifier(bus, nb);
  767. if (ret) {
  768. pr_warn("Failed to register DMA domain notifier; IOMMU DMA ops unavailable on bus '%s'\n",
  769. bus->name);
  770. kfree(nb);
  771. }
  772. return ret;
  773. }
  774. static int __init __iommu_dma_init(void)
  775. {
  776. int ret;
  777. ret = iommu_dma_init();
  778. if (!ret)
  779. ret = register_iommu_dma_ops_notifier(&platform_bus_type);
  780. if (!ret)
  781. ret = register_iommu_dma_ops_notifier(&amba_bustype);
  782. /* handle devices queued before this arch_initcall */
  783. if (!ret)
  784. __iommu_attach_notifier(NULL, BUS_NOTIFY_ADD_DEVICE, NULL);
  785. return ret;
  786. }
  787. arch_initcall(__iommu_dma_init);
  788. static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  789. const struct iommu_ops *ops)
  790. {
  791. struct iommu_group *group;
  792. if (!ops)
  793. return;
  794. /*
  795. * TODO: As a concession to the future, we're ready to handle being
  796. * called both early and late (i.e. after bus_add_device). Once all
  797. * the platform bus code is reworked to call us late and the notifier
  798. * junk above goes away, move the body of do_iommu_attach here.
  799. */
  800. group = iommu_group_get(dev);
  801. if (group) {
  802. do_iommu_attach(dev, ops, dma_base, size);
  803. iommu_group_put(group);
  804. } else {
  805. queue_iommu_attach(dev, ops, dma_base, size);
  806. }
  807. }
  808. void arch_teardown_dma_ops(struct device *dev)
  809. {
  810. struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
  811. if (domain) {
  812. iommu_detach_device(domain, dev);
  813. if (domain->type & __IOMMU_DOMAIN_FAKE_DEFAULT)
  814. iommu_domain_free(domain);
  815. }
  816. dev->archdata.dma_ops = NULL;
  817. }
  818. #else
  819. static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  820. struct iommu_ops *iommu)
  821. { }
  822. #endif /* CONFIG_IOMMU_DMA */
  823. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  824. struct iommu_ops *iommu, bool coherent)
  825. {
  826. if (!dev->archdata.dma_ops)
  827. dev->archdata.dma_ops = &swiotlb_dma_ops;
  828. dev->archdata.dma_coherent = coherent;
  829. __iommu_setup_dma_ops(dev, dma_base, size, iommu);
  830. }