dma-mapping.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/memblock.h>
  22. #include <linux/cache.h>
  23. #include <linux/export.h>
  24. #include <linux/slab.h>
  25. #include <linux/genalloc.h>
  26. #include <linux/dma-direct.h>
  27. #include <linux/dma-noncoherent.h>
  28. #include <linux/dma-contiguous.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/swiotlb.h>
  31. #include <linux/pci.h>
  32. #include <asm/cacheflush.h>
  33. static struct gen_pool *atomic_pool __ro_after_init;
  34. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  35. static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
  36. static int __init early_coherent_pool(char *p)
  37. {
  38. atomic_pool_size = memparse(p, &p);
  39. return 0;
  40. }
  41. early_param("coherent_pool", early_coherent_pool);
  42. static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
  43. {
  44. unsigned long val;
  45. void *ptr = NULL;
  46. if (!atomic_pool) {
  47. WARN(1, "coherent pool not initialised!\n");
  48. return NULL;
  49. }
  50. val = gen_pool_alloc(atomic_pool, size);
  51. if (val) {
  52. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  53. *ret_page = phys_to_page(phys);
  54. ptr = (void *)val;
  55. memset(ptr, 0, size);
  56. }
  57. return ptr;
  58. }
  59. static bool __in_atomic_pool(void *start, size_t size)
  60. {
  61. return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
  62. }
  63. static int __free_from_pool(void *start, size_t size)
  64. {
  65. if (!__in_atomic_pool(start, size))
  66. return 0;
  67. gen_pool_free(atomic_pool, (unsigned long)start, size);
  68. return 1;
  69. }
  70. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  71. gfp_t flags, unsigned long attrs)
  72. {
  73. struct page *page;
  74. void *ptr, *coherent_ptr;
  75. pgprot_t prot = pgprot_writecombine(PAGE_KERNEL);
  76. size = PAGE_ALIGN(size);
  77. if (!gfpflags_allow_blocking(flags)) {
  78. struct page *page = NULL;
  79. void *addr = __alloc_from_pool(size, &page, flags);
  80. if (addr)
  81. *dma_handle = phys_to_dma(dev, page_to_phys(page));
  82. return addr;
  83. }
  84. ptr = dma_direct_alloc_pages(dev, size, dma_handle, flags, attrs);
  85. if (!ptr)
  86. goto no_mem;
  87. /* remove any dirty cache lines on the kernel alias */
  88. __dma_flush_area(ptr, size);
  89. /* create a coherent mapping */
  90. page = virt_to_page(ptr);
  91. coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
  92. prot, __builtin_return_address(0));
  93. if (!coherent_ptr)
  94. goto no_map;
  95. return coherent_ptr;
  96. no_map:
  97. dma_direct_free_pages(dev, size, ptr, *dma_handle, attrs);
  98. no_mem:
  99. return NULL;
  100. }
  101. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  102. dma_addr_t dma_handle, unsigned long attrs)
  103. {
  104. if (!__free_from_pool(vaddr, PAGE_ALIGN(size))) {
  105. void *kaddr = phys_to_virt(dma_to_phys(dev, dma_handle));
  106. vunmap(vaddr);
  107. dma_direct_free_pages(dev, size, kaddr, dma_handle, attrs);
  108. }
  109. }
  110. long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
  111. dma_addr_t dma_addr)
  112. {
  113. return __phys_to_pfn(dma_to_phys(dev, dma_addr));
  114. }
  115. pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
  116. unsigned long attrs)
  117. {
  118. if (!dev_is_dma_coherent(dev) || (attrs & DMA_ATTR_WRITE_COMBINE))
  119. return pgprot_writecombine(prot);
  120. return prot;
  121. }
  122. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  123. size_t size, enum dma_data_direction dir)
  124. {
  125. __dma_map_area(phys_to_virt(paddr), size, dir);
  126. }
  127. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  128. size_t size, enum dma_data_direction dir)
  129. {
  130. __dma_unmap_area(phys_to_virt(paddr), size, dir);
  131. }
  132. #ifdef CONFIG_IOMMU_DMA
  133. static int __swiotlb_get_sgtable_page(struct sg_table *sgt,
  134. struct page *page, size_t size)
  135. {
  136. int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  137. if (!ret)
  138. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  139. return ret;
  140. }
  141. static int __swiotlb_mmap_pfn(struct vm_area_struct *vma,
  142. unsigned long pfn, size_t size)
  143. {
  144. int ret = -ENXIO;
  145. unsigned long nr_vma_pages = vma_pages(vma);
  146. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  147. unsigned long off = vma->vm_pgoff;
  148. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  149. ret = remap_pfn_range(vma, vma->vm_start,
  150. pfn + off,
  151. vma->vm_end - vma->vm_start,
  152. vma->vm_page_prot);
  153. }
  154. return ret;
  155. }
  156. #endif /* CONFIG_IOMMU_DMA */
  157. static int __init atomic_pool_init(void)
  158. {
  159. pgprot_t prot = __pgprot(PROT_NORMAL_NC);
  160. unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
  161. struct page *page;
  162. void *addr;
  163. unsigned int pool_size_order = get_order(atomic_pool_size);
  164. if (dev_get_cma_area(NULL))
  165. page = dma_alloc_from_contiguous(NULL, nr_pages,
  166. pool_size_order, false);
  167. else
  168. page = alloc_pages(GFP_DMA32, pool_size_order);
  169. if (page) {
  170. int ret;
  171. void *page_addr = page_address(page);
  172. memset(page_addr, 0, atomic_pool_size);
  173. __dma_flush_area(page_addr, atomic_pool_size);
  174. atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
  175. if (!atomic_pool)
  176. goto free_page;
  177. addr = dma_common_contiguous_remap(page, atomic_pool_size,
  178. VM_USERMAP, prot, atomic_pool_init);
  179. if (!addr)
  180. goto destroy_genpool;
  181. ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
  182. page_to_phys(page),
  183. atomic_pool_size, -1);
  184. if (ret)
  185. goto remove_mapping;
  186. gen_pool_set_algo(atomic_pool,
  187. gen_pool_first_fit_order_align,
  188. NULL);
  189. pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
  190. atomic_pool_size / 1024);
  191. return 0;
  192. }
  193. goto out;
  194. remove_mapping:
  195. dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
  196. destroy_genpool:
  197. gen_pool_destroy(atomic_pool);
  198. atomic_pool = NULL;
  199. free_page:
  200. if (!dma_release_from_contiguous(NULL, page, nr_pages))
  201. __free_pages(page, pool_size_order);
  202. out:
  203. pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
  204. atomic_pool_size / 1024);
  205. return -ENOMEM;
  206. }
  207. /********************************************
  208. * The following APIs are for dummy DMA ops *
  209. ********************************************/
  210. static void *__dummy_alloc(struct device *dev, size_t size,
  211. dma_addr_t *dma_handle, gfp_t flags,
  212. unsigned long attrs)
  213. {
  214. return NULL;
  215. }
  216. static void __dummy_free(struct device *dev, size_t size,
  217. void *vaddr, dma_addr_t dma_handle,
  218. unsigned long attrs)
  219. {
  220. }
  221. static int __dummy_mmap(struct device *dev,
  222. struct vm_area_struct *vma,
  223. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  224. unsigned long attrs)
  225. {
  226. return -ENXIO;
  227. }
  228. static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
  229. unsigned long offset, size_t size,
  230. enum dma_data_direction dir,
  231. unsigned long attrs)
  232. {
  233. return 0;
  234. }
  235. static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
  236. size_t size, enum dma_data_direction dir,
  237. unsigned long attrs)
  238. {
  239. }
  240. static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
  241. int nelems, enum dma_data_direction dir,
  242. unsigned long attrs)
  243. {
  244. return 0;
  245. }
  246. static void __dummy_unmap_sg(struct device *dev,
  247. struct scatterlist *sgl, int nelems,
  248. enum dma_data_direction dir,
  249. unsigned long attrs)
  250. {
  251. }
  252. static void __dummy_sync_single(struct device *dev,
  253. dma_addr_t dev_addr, size_t size,
  254. enum dma_data_direction dir)
  255. {
  256. }
  257. static void __dummy_sync_sg(struct device *dev,
  258. struct scatterlist *sgl, int nelems,
  259. enum dma_data_direction dir)
  260. {
  261. }
  262. static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
  263. {
  264. return 1;
  265. }
  266. static int __dummy_dma_supported(struct device *hwdev, u64 mask)
  267. {
  268. return 0;
  269. }
  270. const struct dma_map_ops dummy_dma_ops = {
  271. .alloc = __dummy_alloc,
  272. .free = __dummy_free,
  273. .mmap = __dummy_mmap,
  274. .map_page = __dummy_map_page,
  275. .unmap_page = __dummy_unmap_page,
  276. .map_sg = __dummy_map_sg,
  277. .unmap_sg = __dummy_unmap_sg,
  278. .sync_single_for_cpu = __dummy_sync_single,
  279. .sync_single_for_device = __dummy_sync_single,
  280. .sync_sg_for_cpu = __dummy_sync_sg,
  281. .sync_sg_for_device = __dummy_sync_sg,
  282. .mapping_error = __dummy_mapping_error,
  283. .dma_supported = __dummy_dma_supported,
  284. };
  285. EXPORT_SYMBOL(dummy_dma_ops);
  286. static int __init arm64_dma_init(void)
  287. {
  288. WARN_TAINT(ARCH_DMA_MINALIGN < cache_line_size(),
  289. TAINT_CPU_OUT_OF_SPEC,
  290. "ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
  291. ARCH_DMA_MINALIGN, cache_line_size());
  292. return atomic_pool_init();
  293. }
  294. arch_initcall(arm64_dma_init);
  295. #ifdef CONFIG_IOMMU_DMA
  296. #include <linux/dma-iommu.h>
  297. #include <linux/platform_device.h>
  298. #include <linux/amba/bus.h>
  299. /* Thankfully, all cache ops are by VA so we can ignore phys here */
  300. static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
  301. {
  302. __dma_flush_area(virt, PAGE_SIZE);
  303. }
  304. static void *__iommu_alloc_attrs(struct device *dev, size_t size,
  305. dma_addr_t *handle, gfp_t gfp,
  306. unsigned long attrs)
  307. {
  308. bool coherent = dev_is_dma_coherent(dev);
  309. int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
  310. size_t iosize = size;
  311. void *addr;
  312. if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
  313. return NULL;
  314. size = PAGE_ALIGN(size);
  315. /*
  316. * Some drivers rely on this, and we probably don't want the
  317. * possibility of stale kernel data being read by devices anyway.
  318. */
  319. gfp |= __GFP_ZERO;
  320. if (!gfpflags_allow_blocking(gfp)) {
  321. struct page *page;
  322. /*
  323. * In atomic context we can't remap anything, so we'll only
  324. * get the virtually contiguous buffer we need by way of a
  325. * physically contiguous allocation.
  326. */
  327. if (coherent) {
  328. page = alloc_pages(gfp, get_order(size));
  329. addr = page ? page_address(page) : NULL;
  330. } else {
  331. addr = __alloc_from_pool(size, &page, gfp);
  332. }
  333. if (!addr)
  334. return NULL;
  335. *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
  336. if (iommu_dma_mapping_error(dev, *handle)) {
  337. if (coherent)
  338. __free_pages(page, get_order(size));
  339. else
  340. __free_from_pool(addr, size);
  341. addr = NULL;
  342. }
  343. } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  344. pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
  345. struct page *page;
  346. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  347. get_order(size), gfp & __GFP_NOWARN);
  348. if (!page)
  349. return NULL;
  350. *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
  351. if (iommu_dma_mapping_error(dev, *handle)) {
  352. dma_release_from_contiguous(dev, page,
  353. size >> PAGE_SHIFT);
  354. return NULL;
  355. }
  356. addr = dma_common_contiguous_remap(page, size, VM_USERMAP,
  357. prot,
  358. __builtin_return_address(0));
  359. if (addr) {
  360. if (!coherent)
  361. __dma_flush_area(page_to_virt(page), iosize);
  362. memset(addr, 0, size);
  363. } else {
  364. iommu_dma_unmap_page(dev, *handle, iosize, 0, attrs);
  365. dma_release_from_contiguous(dev, page,
  366. size >> PAGE_SHIFT);
  367. }
  368. } else {
  369. pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
  370. struct page **pages;
  371. pages = iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot,
  372. handle, flush_page);
  373. if (!pages)
  374. return NULL;
  375. addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
  376. __builtin_return_address(0));
  377. if (!addr)
  378. iommu_dma_free(dev, pages, iosize, handle);
  379. }
  380. return addr;
  381. }
  382. static void __iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  383. dma_addr_t handle, unsigned long attrs)
  384. {
  385. size_t iosize = size;
  386. size = PAGE_ALIGN(size);
  387. /*
  388. * @cpu_addr will be one of 4 things depending on how it was allocated:
  389. * - A remapped array of pages for contiguous allocations.
  390. * - A remapped array of pages from iommu_dma_alloc(), for all
  391. * non-atomic allocations.
  392. * - A non-cacheable alias from the atomic pool, for atomic
  393. * allocations by non-coherent devices.
  394. * - A normal lowmem address, for atomic allocations by
  395. * coherent devices.
  396. * Hence how dodgy the below logic looks...
  397. */
  398. if (__in_atomic_pool(cpu_addr, size)) {
  399. iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
  400. __free_from_pool(cpu_addr, size);
  401. } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  402. struct page *page = vmalloc_to_page(cpu_addr);
  403. iommu_dma_unmap_page(dev, handle, iosize, 0, attrs);
  404. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  405. dma_common_free_remap(cpu_addr, size, VM_USERMAP);
  406. } else if (is_vmalloc_addr(cpu_addr)){
  407. struct vm_struct *area = find_vm_area(cpu_addr);
  408. if (WARN_ON(!area || !area->pages))
  409. return;
  410. iommu_dma_free(dev, area->pages, iosize, &handle);
  411. dma_common_free_remap(cpu_addr, size, VM_USERMAP);
  412. } else {
  413. iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
  414. __free_pages(virt_to_page(cpu_addr), get_order(size));
  415. }
  416. }
  417. static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  418. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  419. unsigned long attrs)
  420. {
  421. struct vm_struct *area;
  422. int ret;
  423. vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
  424. if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  425. return ret;
  426. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  427. /*
  428. * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
  429. * hence in the vmalloc space.
  430. */
  431. unsigned long pfn = vmalloc_to_pfn(cpu_addr);
  432. return __swiotlb_mmap_pfn(vma, pfn, size);
  433. }
  434. area = find_vm_area(cpu_addr);
  435. if (WARN_ON(!area || !area->pages))
  436. return -ENXIO;
  437. return iommu_dma_mmap(area->pages, size, vma);
  438. }
  439. static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  440. void *cpu_addr, dma_addr_t dma_addr,
  441. size_t size, unsigned long attrs)
  442. {
  443. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  444. struct vm_struct *area = find_vm_area(cpu_addr);
  445. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  446. /*
  447. * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
  448. * hence in the vmalloc space.
  449. */
  450. struct page *page = vmalloc_to_page(cpu_addr);
  451. return __swiotlb_get_sgtable_page(sgt, page, size);
  452. }
  453. if (WARN_ON(!area || !area->pages))
  454. return -ENXIO;
  455. return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
  456. GFP_KERNEL);
  457. }
  458. static void __iommu_sync_single_for_cpu(struct device *dev,
  459. dma_addr_t dev_addr, size_t size,
  460. enum dma_data_direction dir)
  461. {
  462. phys_addr_t phys;
  463. if (dev_is_dma_coherent(dev))
  464. return;
  465. phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dev_addr);
  466. arch_sync_dma_for_cpu(dev, phys, size, dir);
  467. }
  468. static void __iommu_sync_single_for_device(struct device *dev,
  469. dma_addr_t dev_addr, size_t size,
  470. enum dma_data_direction dir)
  471. {
  472. phys_addr_t phys;
  473. if (dev_is_dma_coherent(dev))
  474. return;
  475. phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dev_addr);
  476. arch_sync_dma_for_device(dev, phys, size, dir);
  477. }
  478. static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
  479. unsigned long offset, size_t size,
  480. enum dma_data_direction dir,
  481. unsigned long attrs)
  482. {
  483. bool coherent = dev_is_dma_coherent(dev);
  484. int prot = dma_info_to_prot(dir, coherent, attrs);
  485. dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
  486. if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
  487. !iommu_dma_mapping_error(dev, dev_addr))
  488. __dma_map_area(page_address(page) + offset, size, dir);
  489. return dev_addr;
  490. }
  491. static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
  492. size_t size, enum dma_data_direction dir,
  493. unsigned long attrs)
  494. {
  495. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  496. __iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
  497. iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
  498. }
  499. static void __iommu_sync_sg_for_cpu(struct device *dev,
  500. struct scatterlist *sgl, int nelems,
  501. enum dma_data_direction dir)
  502. {
  503. struct scatterlist *sg;
  504. int i;
  505. if (dev_is_dma_coherent(dev))
  506. return;
  507. for_each_sg(sgl, sg, nelems, i)
  508. arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
  509. }
  510. static void __iommu_sync_sg_for_device(struct device *dev,
  511. struct scatterlist *sgl, int nelems,
  512. enum dma_data_direction dir)
  513. {
  514. struct scatterlist *sg;
  515. int i;
  516. if (dev_is_dma_coherent(dev))
  517. return;
  518. for_each_sg(sgl, sg, nelems, i)
  519. arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
  520. }
  521. static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  522. int nelems, enum dma_data_direction dir,
  523. unsigned long attrs)
  524. {
  525. bool coherent = dev_is_dma_coherent(dev);
  526. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  527. __iommu_sync_sg_for_device(dev, sgl, nelems, dir);
  528. return iommu_dma_map_sg(dev, sgl, nelems,
  529. dma_info_to_prot(dir, coherent, attrs));
  530. }
  531. static void __iommu_unmap_sg_attrs(struct device *dev,
  532. struct scatterlist *sgl, int nelems,
  533. enum dma_data_direction dir,
  534. unsigned long attrs)
  535. {
  536. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  537. __iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
  538. iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
  539. }
  540. static const struct dma_map_ops iommu_dma_ops = {
  541. .alloc = __iommu_alloc_attrs,
  542. .free = __iommu_free_attrs,
  543. .mmap = __iommu_mmap_attrs,
  544. .get_sgtable = __iommu_get_sgtable,
  545. .map_page = __iommu_map_page,
  546. .unmap_page = __iommu_unmap_page,
  547. .map_sg = __iommu_map_sg_attrs,
  548. .unmap_sg = __iommu_unmap_sg_attrs,
  549. .sync_single_for_cpu = __iommu_sync_single_for_cpu,
  550. .sync_single_for_device = __iommu_sync_single_for_device,
  551. .sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
  552. .sync_sg_for_device = __iommu_sync_sg_for_device,
  553. .map_resource = iommu_dma_map_resource,
  554. .unmap_resource = iommu_dma_unmap_resource,
  555. .mapping_error = iommu_dma_mapping_error,
  556. };
  557. static int __init __iommu_dma_init(void)
  558. {
  559. return iommu_dma_init();
  560. }
  561. arch_initcall(__iommu_dma_init);
  562. static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  563. const struct iommu_ops *ops)
  564. {
  565. struct iommu_domain *domain;
  566. if (!ops)
  567. return;
  568. /*
  569. * The IOMMU core code allocates the default DMA domain, which the
  570. * underlying IOMMU driver needs to support via the dma-iommu layer.
  571. */
  572. domain = iommu_get_domain_for_dev(dev);
  573. if (!domain)
  574. goto out_err;
  575. if (domain->type == IOMMU_DOMAIN_DMA) {
  576. if (iommu_dma_init_domain(domain, dma_base, size, dev))
  577. goto out_err;
  578. dev->dma_ops = &iommu_dma_ops;
  579. }
  580. return;
  581. out_err:
  582. pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
  583. dev_name(dev));
  584. }
  585. void arch_teardown_dma_ops(struct device *dev)
  586. {
  587. dev->dma_ops = NULL;
  588. }
  589. #else
  590. static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  591. const struct iommu_ops *iommu)
  592. { }
  593. #endif /* CONFIG_IOMMU_DMA */
  594. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  595. const struct iommu_ops *iommu, bool coherent)
  596. {
  597. if (!dev->dma_ops)
  598. dev->dma_ops = &swiotlb_dma_ops;
  599. dev->dma_coherent = coherent;
  600. __iommu_setup_dma_ops(dev, dma_base, size, iommu);
  601. #ifdef CONFIG_XEN
  602. if (xen_initial_domain()) {
  603. dev->archdata.dev_dma_ops = dev->dma_ops;
  604. dev->dma_ops = xen_dma_ops;
  605. }
  606. #endif
  607. }