dma-mapping.c 26 KB

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