dma-mapping.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /*
  2. * linux/arch/arm/mm/dma-mapping.c
  3. *
  4. * Copyright (C) 2000-2004 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * DMA uncached mapping support.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/gfp.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dma-contiguous.h>
  21. #include <linux/highmem.h>
  22. #include <linux/memblock.h>
  23. #include <linux/slab.h>
  24. #include <linux/iommu.h>
  25. #include <linux/io.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/sizes.h>
  28. #include <asm/memory.h>
  29. #include <asm/highmem.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/mach/arch.h>
  33. #include <asm/dma-iommu.h>
  34. #include <asm/mach/map.h>
  35. #include <asm/system_info.h>
  36. #include <asm/dma-contiguous.h>
  37. #include "mm.h"
  38. /*
  39. * The DMA API is built upon the notion of "buffer ownership". A buffer
  40. * is either exclusively owned by the CPU (and therefore may be accessed
  41. * by it) or exclusively owned by the DMA device. These helper functions
  42. * represent the transitions between these two ownership states.
  43. *
  44. * Note, however, that on later ARMs, this notion does not work due to
  45. * speculative prefetches. We model our approach on the assumption that
  46. * the CPU does do speculative prefetches, which means we clean caches
  47. * before transfers and delay cache invalidation until transfer completion.
  48. *
  49. */
  50. static void __dma_page_cpu_to_dev(struct page *, unsigned long,
  51. size_t, enum dma_data_direction);
  52. static void __dma_page_dev_to_cpu(struct page *, unsigned long,
  53. size_t, enum dma_data_direction);
  54. /**
  55. * arm_dma_map_page - map a portion of a page for streaming DMA
  56. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  57. * @page: page that buffer resides in
  58. * @offset: offset into page for start of buffer
  59. * @size: size of buffer to map
  60. * @dir: DMA transfer direction
  61. *
  62. * Ensure that any data held in the cache is appropriately discarded
  63. * or written back.
  64. *
  65. * The device owns this memory once this call has completed. The CPU
  66. * can regain ownership by calling dma_unmap_page().
  67. */
  68. static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
  69. unsigned long offset, size_t size, enum dma_data_direction dir,
  70. struct dma_attrs *attrs)
  71. {
  72. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  73. __dma_page_cpu_to_dev(page, offset, size, dir);
  74. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  75. }
  76. /**
  77. * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  78. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  79. * @handle: DMA address of buffer
  80. * @size: size of buffer (same as passed to dma_map_page)
  81. * @dir: DMA transfer direction (same as passed to dma_map_page)
  82. *
  83. * Unmap a page streaming mode DMA translation. The handle and size
  84. * must match what was provided in the previous dma_map_page() call.
  85. * All other usages are undefined.
  86. *
  87. * After this call, reads by the CPU to the buffer are guaranteed to see
  88. * whatever the device wrote there.
  89. */
  90. static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
  91. size_t size, enum dma_data_direction dir,
  92. struct dma_attrs *attrs)
  93. {
  94. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  95. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
  96. handle & ~PAGE_MASK, size, dir);
  97. }
  98. static void arm_dma_sync_single_for_cpu(struct device *dev,
  99. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  100. {
  101. unsigned int offset = handle & (PAGE_SIZE - 1);
  102. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  103. if (!arch_is_coherent())
  104. __dma_page_dev_to_cpu(page, offset, size, dir);
  105. }
  106. static void arm_dma_sync_single_for_device(struct device *dev,
  107. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  108. {
  109. unsigned int offset = handle & (PAGE_SIZE - 1);
  110. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  111. if (!arch_is_coherent())
  112. __dma_page_cpu_to_dev(page, offset, size, dir);
  113. }
  114. static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
  115. struct dma_map_ops arm_dma_ops = {
  116. .alloc = arm_dma_alloc,
  117. .free = arm_dma_free,
  118. .mmap = arm_dma_mmap,
  119. .get_sgtable = arm_dma_get_sgtable,
  120. .map_page = arm_dma_map_page,
  121. .unmap_page = arm_dma_unmap_page,
  122. .map_sg = arm_dma_map_sg,
  123. .unmap_sg = arm_dma_unmap_sg,
  124. .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
  125. .sync_single_for_device = arm_dma_sync_single_for_device,
  126. .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
  127. .sync_sg_for_device = arm_dma_sync_sg_for_device,
  128. .set_dma_mask = arm_dma_set_mask,
  129. };
  130. EXPORT_SYMBOL(arm_dma_ops);
  131. static u64 get_coherent_dma_mask(struct device *dev)
  132. {
  133. u64 mask = (u64)arm_dma_limit;
  134. if (dev) {
  135. mask = dev->coherent_dma_mask;
  136. /*
  137. * Sanity check the DMA mask - it must be non-zero, and
  138. * must be able to be satisfied by a DMA allocation.
  139. */
  140. if (mask == 0) {
  141. dev_warn(dev, "coherent DMA mask is unset\n");
  142. return 0;
  143. }
  144. if ((~mask) & (u64)arm_dma_limit) {
  145. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  146. "than system GFP_DMA mask %#llx\n",
  147. mask, (u64)arm_dma_limit);
  148. return 0;
  149. }
  150. }
  151. return mask;
  152. }
  153. static void __dma_clear_buffer(struct page *page, size_t size)
  154. {
  155. void *ptr;
  156. /*
  157. * Ensure that the allocated pages are zeroed, and that any data
  158. * lurking in the kernel direct-mapped region is invalidated.
  159. */
  160. ptr = page_address(page);
  161. if (ptr) {
  162. memset(ptr, 0, size);
  163. dmac_flush_range(ptr, ptr + size);
  164. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  165. }
  166. }
  167. /*
  168. * Allocate a DMA buffer for 'dev' of size 'size' using the
  169. * specified gfp mask. Note that 'size' must be page aligned.
  170. */
  171. static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  172. {
  173. unsigned long order = get_order(size);
  174. struct page *page, *p, *e;
  175. page = alloc_pages(gfp, order);
  176. if (!page)
  177. return NULL;
  178. /*
  179. * Now split the huge page and free the excess pages
  180. */
  181. split_page(page, order);
  182. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  183. __free_page(p);
  184. __dma_clear_buffer(page, size);
  185. return page;
  186. }
  187. /*
  188. * Free a DMA buffer. 'size' must be page aligned.
  189. */
  190. static void __dma_free_buffer(struct page *page, size_t size)
  191. {
  192. struct page *e = page + (size >> PAGE_SHIFT);
  193. while (page < e) {
  194. __free_page(page);
  195. page++;
  196. }
  197. }
  198. #ifdef CONFIG_MMU
  199. #ifdef CONFIG_HUGETLB_PAGE
  200. #error ARM Coherent DMA allocator does not (yet) support huge TLB
  201. #endif
  202. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  203. pgprot_t prot, struct page **ret_page);
  204. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  205. pgprot_t prot, struct page **ret_page,
  206. const void *caller);
  207. static void *
  208. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
  209. const void *caller)
  210. {
  211. struct vm_struct *area;
  212. unsigned long addr;
  213. /*
  214. * DMA allocation can be mapped to user space, so lets
  215. * set VM_USERMAP flags too.
  216. */
  217. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  218. caller);
  219. if (!area)
  220. return NULL;
  221. addr = (unsigned long)area->addr;
  222. area->phys_addr = __pfn_to_phys(page_to_pfn(page));
  223. if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) {
  224. vunmap((void *)addr);
  225. return NULL;
  226. }
  227. return (void *)addr;
  228. }
  229. static void __dma_free_remap(void *cpu_addr, size_t size)
  230. {
  231. unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
  232. struct vm_struct *area = find_vm_area(cpu_addr);
  233. if (!area || (area->flags & flags) != flags) {
  234. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  235. return;
  236. }
  237. unmap_kernel_range((unsigned long)cpu_addr, size);
  238. vunmap(cpu_addr);
  239. }
  240. struct dma_pool {
  241. size_t size;
  242. spinlock_t lock;
  243. unsigned long *bitmap;
  244. unsigned long nr_pages;
  245. void *vaddr;
  246. struct page *page;
  247. };
  248. static struct dma_pool atomic_pool = {
  249. .size = SZ_256K,
  250. };
  251. static int __init early_coherent_pool(char *p)
  252. {
  253. atomic_pool.size = memparse(p, &p);
  254. return 0;
  255. }
  256. early_param("coherent_pool", early_coherent_pool);
  257. /*
  258. * Initialise the coherent pool for atomic allocations.
  259. */
  260. static int __init atomic_pool_init(void)
  261. {
  262. struct dma_pool *pool = &atomic_pool;
  263. pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
  264. unsigned long nr_pages = pool->size >> PAGE_SHIFT;
  265. unsigned long *bitmap;
  266. struct page *page;
  267. void *ptr;
  268. int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
  269. bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  270. if (!bitmap)
  271. goto no_bitmap;
  272. if (IS_ENABLED(CONFIG_CMA))
  273. ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page);
  274. else
  275. ptr = __alloc_remap_buffer(NULL, pool->size, GFP_KERNEL, prot,
  276. &page, NULL);
  277. if (ptr) {
  278. spin_lock_init(&pool->lock);
  279. pool->vaddr = ptr;
  280. pool->page = page;
  281. pool->bitmap = bitmap;
  282. pool->nr_pages = nr_pages;
  283. pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
  284. (unsigned)pool->size / 1024);
  285. return 0;
  286. }
  287. kfree(bitmap);
  288. no_bitmap:
  289. pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
  290. (unsigned)pool->size / 1024);
  291. return -ENOMEM;
  292. }
  293. /*
  294. * CMA is activated by core_initcall, so we must be called after it.
  295. */
  296. postcore_initcall(atomic_pool_init);
  297. struct dma_contig_early_reserve {
  298. phys_addr_t base;
  299. unsigned long size;
  300. };
  301. static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
  302. static int dma_mmu_remap_num __initdata;
  303. void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  304. {
  305. dma_mmu_remap[dma_mmu_remap_num].base = base;
  306. dma_mmu_remap[dma_mmu_remap_num].size = size;
  307. dma_mmu_remap_num++;
  308. }
  309. void __init dma_contiguous_remap(void)
  310. {
  311. int i;
  312. for (i = 0; i < dma_mmu_remap_num; i++) {
  313. phys_addr_t start = dma_mmu_remap[i].base;
  314. phys_addr_t end = start + dma_mmu_remap[i].size;
  315. struct map_desc map;
  316. unsigned long addr;
  317. if (end > arm_lowmem_limit)
  318. end = arm_lowmem_limit;
  319. if (start >= end)
  320. continue;
  321. map.pfn = __phys_to_pfn(start);
  322. map.virtual = __phys_to_virt(start);
  323. map.length = end - start;
  324. map.type = MT_MEMORY_DMA_READY;
  325. /*
  326. * Clear previous low-memory mapping
  327. */
  328. for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
  329. addr += PMD_SIZE)
  330. pmd_clear(pmd_off_k(addr));
  331. iotable_init(&map, 1);
  332. }
  333. }
  334. static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
  335. void *data)
  336. {
  337. struct page *page = virt_to_page(addr);
  338. pgprot_t prot = *(pgprot_t *)data;
  339. set_pte_ext(pte, mk_pte(page, prot), 0);
  340. return 0;
  341. }
  342. static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
  343. {
  344. unsigned long start = (unsigned long) page_address(page);
  345. unsigned end = start + size;
  346. apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
  347. dsb();
  348. flush_tlb_kernel_range(start, end);
  349. }
  350. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  351. pgprot_t prot, struct page **ret_page,
  352. const void *caller)
  353. {
  354. struct page *page;
  355. void *ptr;
  356. page = __dma_alloc_buffer(dev, size, gfp);
  357. if (!page)
  358. return NULL;
  359. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  360. if (!ptr) {
  361. __dma_free_buffer(page, size);
  362. return NULL;
  363. }
  364. *ret_page = page;
  365. return ptr;
  366. }
  367. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  368. {
  369. struct dma_pool *pool = &atomic_pool;
  370. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  371. unsigned int pageno;
  372. unsigned long flags;
  373. void *ptr = NULL;
  374. unsigned long align_mask;
  375. if (!pool->vaddr) {
  376. WARN(1, "coherent pool not initialised!\n");
  377. return NULL;
  378. }
  379. /*
  380. * Align the region allocation - allocations from pool are rather
  381. * small, so align them to their order in pages, minimum is a page
  382. * size. This helps reduce fragmentation of the DMA space.
  383. */
  384. align_mask = (1 << get_order(size)) - 1;
  385. spin_lock_irqsave(&pool->lock, flags);
  386. pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
  387. 0, count, align_mask);
  388. if (pageno < pool->nr_pages) {
  389. bitmap_set(pool->bitmap, pageno, count);
  390. ptr = pool->vaddr + PAGE_SIZE * pageno;
  391. *ret_page = pool->page + pageno;
  392. }
  393. spin_unlock_irqrestore(&pool->lock, flags);
  394. return ptr;
  395. }
  396. static int __free_from_pool(void *start, size_t size)
  397. {
  398. struct dma_pool *pool = &atomic_pool;
  399. unsigned long pageno, count;
  400. unsigned long flags;
  401. if (start < pool->vaddr || start > pool->vaddr + pool->size)
  402. return 0;
  403. if (start + size > pool->vaddr + pool->size) {
  404. WARN(1, "freeing wrong coherent size from pool\n");
  405. return 0;
  406. }
  407. pageno = (start - pool->vaddr) >> PAGE_SHIFT;
  408. count = size >> PAGE_SHIFT;
  409. spin_lock_irqsave(&pool->lock, flags);
  410. bitmap_clear(pool->bitmap, pageno, count);
  411. spin_unlock_irqrestore(&pool->lock, flags);
  412. return 1;
  413. }
  414. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  415. pgprot_t prot, struct page **ret_page)
  416. {
  417. unsigned long order = get_order(size);
  418. size_t count = size >> PAGE_SHIFT;
  419. struct page *page;
  420. page = dma_alloc_from_contiguous(dev, count, order);
  421. if (!page)
  422. return NULL;
  423. __dma_clear_buffer(page, size);
  424. __dma_remap(page, size, prot);
  425. *ret_page = page;
  426. return page_address(page);
  427. }
  428. static void __free_from_contiguous(struct device *dev, struct page *page,
  429. size_t size)
  430. {
  431. __dma_remap(page, size, pgprot_kernel);
  432. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  433. }
  434. static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
  435. {
  436. prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
  437. pgprot_writecombine(prot) :
  438. pgprot_dmacoherent(prot);
  439. return prot;
  440. }
  441. #define nommu() 0
  442. #else /* !CONFIG_MMU */
  443. #define nommu() 1
  444. #define __get_dma_pgprot(attrs, prot) __pgprot(0)
  445. #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
  446. #define __alloc_from_pool(size, ret_page) NULL
  447. #define __alloc_from_contiguous(dev, size, prot, ret) NULL
  448. #define __free_from_pool(cpu_addr, size) 0
  449. #define __free_from_contiguous(dev, page, size) do { } while (0)
  450. #define __dma_free_remap(cpu_addr, size) do { } while (0)
  451. #endif /* CONFIG_MMU */
  452. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  453. struct page **ret_page)
  454. {
  455. struct page *page;
  456. page = __dma_alloc_buffer(dev, size, gfp);
  457. if (!page)
  458. return NULL;
  459. *ret_page = page;
  460. return page_address(page);
  461. }
  462. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  463. gfp_t gfp, pgprot_t prot, const void *caller)
  464. {
  465. u64 mask = get_coherent_dma_mask(dev);
  466. struct page *page;
  467. void *addr;
  468. #ifdef CONFIG_DMA_API_DEBUG
  469. u64 limit = (mask + 1) & ~mask;
  470. if (limit && size >= limit) {
  471. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  472. size, mask);
  473. return NULL;
  474. }
  475. #endif
  476. if (!mask)
  477. return NULL;
  478. if (mask < 0xffffffffULL)
  479. gfp |= GFP_DMA;
  480. /*
  481. * Following is a work-around (a.k.a. hack) to prevent pages
  482. * with __GFP_COMP being passed to split_page() which cannot
  483. * handle them. The real problem is that this flag probably
  484. * should be 0 on ARM as it is not supported on this
  485. * platform; see CONFIG_HUGETLBFS.
  486. */
  487. gfp &= ~(__GFP_COMP);
  488. *handle = DMA_ERROR_CODE;
  489. size = PAGE_ALIGN(size);
  490. if (arch_is_coherent() || nommu())
  491. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  492. else if (gfp & GFP_ATOMIC)
  493. addr = __alloc_from_pool(size, &page);
  494. else if (!IS_ENABLED(CONFIG_CMA))
  495. addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
  496. else
  497. addr = __alloc_from_contiguous(dev, size, prot, &page);
  498. if (addr)
  499. *handle = pfn_to_dma(dev, page_to_pfn(page));
  500. return addr;
  501. }
  502. /*
  503. * Allocate DMA-coherent memory space and return both the kernel remapped
  504. * virtual and bus address for that space.
  505. */
  506. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  507. gfp_t gfp, struct dma_attrs *attrs)
  508. {
  509. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  510. void *memory;
  511. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  512. return memory;
  513. return __dma_alloc(dev, size, handle, gfp, prot,
  514. __builtin_return_address(0));
  515. }
  516. /*
  517. * Create userspace mapping for the DMA-coherent memory.
  518. */
  519. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  520. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  521. struct dma_attrs *attrs)
  522. {
  523. int ret = -ENXIO;
  524. #ifdef CONFIG_MMU
  525. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  526. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  527. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  528. unsigned long off = vma->vm_pgoff;
  529. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  530. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  531. return ret;
  532. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  533. ret = remap_pfn_range(vma, vma->vm_start,
  534. pfn + off,
  535. vma->vm_end - vma->vm_start,
  536. vma->vm_page_prot);
  537. }
  538. #endif /* CONFIG_MMU */
  539. return ret;
  540. }
  541. /*
  542. * Free a buffer as defined by the above mapping.
  543. */
  544. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  545. dma_addr_t handle, struct dma_attrs *attrs)
  546. {
  547. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  548. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  549. return;
  550. size = PAGE_ALIGN(size);
  551. if (arch_is_coherent() || nommu()) {
  552. __dma_free_buffer(page, size);
  553. } else if (!IS_ENABLED(CONFIG_CMA)) {
  554. __dma_free_remap(cpu_addr, size);
  555. __dma_free_buffer(page, size);
  556. } else {
  557. if (__free_from_pool(cpu_addr, size))
  558. return;
  559. /*
  560. * Non-atomic allocations cannot be freed with IRQs disabled
  561. */
  562. WARN_ON(irqs_disabled());
  563. __free_from_contiguous(dev, page, size);
  564. }
  565. }
  566. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  567. void *cpu_addr, dma_addr_t handle, size_t size,
  568. struct dma_attrs *attrs)
  569. {
  570. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  571. int ret;
  572. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  573. if (unlikely(ret))
  574. return ret;
  575. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  576. return 0;
  577. }
  578. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  579. size_t size, enum dma_data_direction dir,
  580. void (*op)(const void *, size_t, int))
  581. {
  582. /*
  583. * A single sg entry may refer to multiple physically contiguous
  584. * pages. But we still need to process highmem pages individually.
  585. * If highmem is not configured then the bulk of this loop gets
  586. * optimized out.
  587. */
  588. size_t left = size;
  589. do {
  590. size_t len = left;
  591. void *vaddr;
  592. if (PageHighMem(page)) {
  593. if (len + offset > PAGE_SIZE) {
  594. if (offset >= PAGE_SIZE) {
  595. page += offset / PAGE_SIZE;
  596. offset %= PAGE_SIZE;
  597. }
  598. len = PAGE_SIZE - offset;
  599. }
  600. vaddr = kmap_high_get(page);
  601. if (vaddr) {
  602. vaddr += offset;
  603. op(vaddr, len, dir);
  604. kunmap_high(page);
  605. } else if (cache_is_vipt()) {
  606. /* unmapped pages might still be cached */
  607. vaddr = kmap_atomic(page);
  608. op(vaddr + offset, len, dir);
  609. kunmap_atomic(vaddr);
  610. }
  611. } else {
  612. vaddr = page_address(page) + offset;
  613. op(vaddr, len, dir);
  614. }
  615. offset = 0;
  616. page++;
  617. left -= len;
  618. } while (left);
  619. }
  620. /*
  621. * Make an area consistent for devices.
  622. * Note: Drivers should NOT use this function directly, as it will break
  623. * platforms with CONFIG_DMABOUNCE.
  624. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  625. */
  626. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  627. size_t size, enum dma_data_direction dir)
  628. {
  629. unsigned long paddr;
  630. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  631. paddr = page_to_phys(page) + off;
  632. if (dir == DMA_FROM_DEVICE) {
  633. outer_inv_range(paddr, paddr + size);
  634. } else {
  635. outer_clean_range(paddr, paddr + size);
  636. }
  637. /* FIXME: non-speculating: flush on bidirectional mappings? */
  638. }
  639. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  640. size_t size, enum dma_data_direction dir)
  641. {
  642. unsigned long paddr = page_to_phys(page) + off;
  643. /* FIXME: non-speculating: not required */
  644. /* don't bother invalidating if DMA to device */
  645. if (dir != DMA_TO_DEVICE)
  646. outer_inv_range(paddr, paddr + size);
  647. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  648. /*
  649. * Mark the D-cache clean for this page to avoid extra flushing.
  650. */
  651. if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
  652. set_bit(PG_dcache_clean, &page->flags);
  653. }
  654. /**
  655. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  656. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  657. * @sg: list of buffers
  658. * @nents: number of buffers to map
  659. * @dir: DMA transfer direction
  660. *
  661. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  662. * This is the scatter-gather version of the dma_map_single interface.
  663. * Here the scatter gather list elements are each tagged with the
  664. * appropriate dma address and length. They are obtained via
  665. * sg_dma_{address,length}.
  666. *
  667. * Device ownership issues as mentioned for dma_map_single are the same
  668. * here.
  669. */
  670. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  671. enum dma_data_direction dir, struct dma_attrs *attrs)
  672. {
  673. struct dma_map_ops *ops = get_dma_ops(dev);
  674. struct scatterlist *s;
  675. int i, j;
  676. for_each_sg(sg, s, nents, i) {
  677. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  678. s->dma_length = s->length;
  679. #endif
  680. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  681. s->length, dir, attrs);
  682. if (dma_mapping_error(dev, s->dma_address))
  683. goto bad_mapping;
  684. }
  685. return nents;
  686. bad_mapping:
  687. for_each_sg(sg, s, i, j)
  688. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  689. return 0;
  690. }
  691. /**
  692. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  693. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  694. * @sg: list of buffers
  695. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  696. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  697. *
  698. * Unmap a set of streaming mode DMA translations. Again, CPU access
  699. * rules concerning calls here are the same as for dma_unmap_single().
  700. */
  701. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  702. enum dma_data_direction dir, struct dma_attrs *attrs)
  703. {
  704. struct dma_map_ops *ops = get_dma_ops(dev);
  705. struct scatterlist *s;
  706. int i;
  707. for_each_sg(sg, s, nents, i)
  708. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  709. }
  710. /**
  711. * arm_dma_sync_sg_for_cpu
  712. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  713. * @sg: list of buffers
  714. * @nents: number of buffers to map (returned from dma_map_sg)
  715. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  716. */
  717. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  718. int nents, enum dma_data_direction dir)
  719. {
  720. struct dma_map_ops *ops = get_dma_ops(dev);
  721. struct scatterlist *s;
  722. int i;
  723. for_each_sg(sg, s, nents, i)
  724. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  725. dir);
  726. }
  727. /**
  728. * arm_dma_sync_sg_for_device
  729. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  730. * @sg: list of buffers
  731. * @nents: number of buffers to map (returned from dma_map_sg)
  732. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  733. */
  734. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  735. int nents, enum dma_data_direction dir)
  736. {
  737. struct dma_map_ops *ops = get_dma_ops(dev);
  738. struct scatterlist *s;
  739. int i;
  740. for_each_sg(sg, s, nents, i)
  741. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  742. dir);
  743. }
  744. /*
  745. * Return whether the given device DMA address mask can be supported
  746. * properly. For example, if your device can only drive the low 24-bits
  747. * during bus mastering, then you would pass 0x00ffffff as the mask
  748. * to this function.
  749. */
  750. int dma_supported(struct device *dev, u64 mask)
  751. {
  752. if (mask < (u64)arm_dma_limit)
  753. return 0;
  754. return 1;
  755. }
  756. EXPORT_SYMBOL(dma_supported);
  757. static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  758. {
  759. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  760. return -EIO;
  761. *dev->dma_mask = dma_mask;
  762. return 0;
  763. }
  764. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  765. static int __init dma_debug_do_init(void)
  766. {
  767. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  768. return 0;
  769. }
  770. fs_initcall(dma_debug_do_init);
  771. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  772. /* IOMMU */
  773. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  774. size_t size)
  775. {
  776. unsigned int order = get_order(size);
  777. unsigned int align = 0;
  778. unsigned int count, start;
  779. unsigned long flags;
  780. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  781. (1 << mapping->order) - 1) >> mapping->order;
  782. if (order > mapping->order)
  783. align = (1 << (order - mapping->order)) - 1;
  784. spin_lock_irqsave(&mapping->lock, flags);
  785. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  786. count, align);
  787. if (start > mapping->bits) {
  788. spin_unlock_irqrestore(&mapping->lock, flags);
  789. return DMA_ERROR_CODE;
  790. }
  791. bitmap_set(mapping->bitmap, start, count);
  792. spin_unlock_irqrestore(&mapping->lock, flags);
  793. return mapping->base + (start << (mapping->order + PAGE_SHIFT));
  794. }
  795. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  796. dma_addr_t addr, size_t size)
  797. {
  798. unsigned int start = (addr - mapping->base) >>
  799. (mapping->order + PAGE_SHIFT);
  800. unsigned int count = ((size >> PAGE_SHIFT) +
  801. (1 << mapping->order) - 1) >> mapping->order;
  802. unsigned long flags;
  803. spin_lock_irqsave(&mapping->lock, flags);
  804. bitmap_clear(mapping->bitmap, start, count);
  805. spin_unlock_irqrestore(&mapping->lock, flags);
  806. }
  807. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  808. {
  809. struct page **pages;
  810. int count = size >> PAGE_SHIFT;
  811. int array_size = count * sizeof(struct page *);
  812. int i = 0;
  813. if (array_size <= PAGE_SIZE)
  814. pages = kzalloc(array_size, gfp);
  815. else
  816. pages = vzalloc(array_size);
  817. if (!pages)
  818. return NULL;
  819. while (count) {
  820. int j, order = __fls(count);
  821. pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
  822. while (!pages[i] && order)
  823. pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
  824. if (!pages[i])
  825. goto error;
  826. if (order)
  827. split_page(pages[i], order);
  828. j = 1 << order;
  829. while (--j)
  830. pages[i + j] = pages[i] + j;
  831. __dma_clear_buffer(pages[i], PAGE_SIZE << order);
  832. i += 1 << order;
  833. count -= 1 << order;
  834. }
  835. return pages;
  836. error:
  837. while (i--)
  838. if (pages[i])
  839. __free_pages(pages[i], 0);
  840. if (array_size <= PAGE_SIZE)
  841. kfree(pages);
  842. else
  843. vfree(pages);
  844. return NULL;
  845. }
  846. static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size)
  847. {
  848. int count = size >> PAGE_SHIFT;
  849. int array_size = count * sizeof(struct page *);
  850. int i;
  851. for (i = 0; i < count; i++)
  852. if (pages[i])
  853. __free_pages(pages[i], 0);
  854. if (array_size <= PAGE_SIZE)
  855. kfree(pages);
  856. else
  857. vfree(pages);
  858. return 0;
  859. }
  860. /*
  861. * Create a CPU mapping for a specified pages
  862. */
  863. static void *
  864. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  865. const void *caller)
  866. {
  867. unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  868. struct vm_struct *area;
  869. unsigned long p;
  870. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  871. caller);
  872. if (!area)
  873. return NULL;
  874. area->pages = pages;
  875. area->nr_pages = nr_pages;
  876. p = (unsigned long)area->addr;
  877. for (i = 0; i < nr_pages; i++) {
  878. phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
  879. if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
  880. goto err;
  881. p += PAGE_SIZE;
  882. }
  883. return area->addr;
  884. err:
  885. unmap_kernel_range((unsigned long)area->addr, size);
  886. vunmap(area->addr);
  887. return NULL;
  888. }
  889. /*
  890. * Create a mapping in device IO address space for specified pages
  891. */
  892. static dma_addr_t
  893. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  894. {
  895. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  896. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  897. dma_addr_t dma_addr, iova;
  898. int i, ret = DMA_ERROR_CODE;
  899. dma_addr = __alloc_iova(mapping, size);
  900. if (dma_addr == DMA_ERROR_CODE)
  901. return dma_addr;
  902. iova = dma_addr;
  903. for (i = 0; i < count; ) {
  904. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  905. phys_addr_t phys = page_to_phys(pages[i]);
  906. unsigned int len, j;
  907. for (j = i + 1; j < count; j++, next_pfn++)
  908. if (page_to_pfn(pages[j]) != next_pfn)
  909. break;
  910. len = (j - i) << PAGE_SHIFT;
  911. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  912. if (ret < 0)
  913. goto fail;
  914. iova += len;
  915. i = j;
  916. }
  917. return dma_addr;
  918. fail:
  919. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  920. __free_iova(mapping, dma_addr, size);
  921. return DMA_ERROR_CODE;
  922. }
  923. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  924. {
  925. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  926. /*
  927. * add optional in-page offset from iova to size and align
  928. * result to page size
  929. */
  930. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  931. iova &= PAGE_MASK;
  932. iommu_unmap(mapping->domain, iova, size);
  933. __free_iova(mapping, iova, size);
  934. return 0;
  935. }
  936. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  937. {
  938. struct vm_struct *area;
  939. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  940. return cpu_addr;
  941. area = find_vm_area(cpu_addr);
  942. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  943. return area->pages;
  944. return NULL;
  945. }
  946. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  947. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  948. {
  949. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  950. struct page **pages;
  951. void *addr = NULL;
  952. *handle = DMA_ERROR_CODE;
  953. size = PAGE_ALIGN(size);
  954. pages = __iommu_alloc_buffer(dev, size, gfp);
  955. if (!pages)
  956. return NULL;
  957. *handle = __iommu_create_mapping(dev, pages, size);
  958. if (*handle == DMA_ERROR_CODE)
  959. goto err_buffer;
  960. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  961. return pages;
  962. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  963. __builtin_return_address(0));
  964. if (!addr)
  965. goto err_mapping;
  966. return addr;
  967. err_mapping:
  968. __iommu_remove_mapping(dev, *handle, size);
  969. err_buffer:
  970. __iommu_free_buffer(dev, pages, size);
  971. return NULL;
  972. }
  973. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  974. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  975. struct dma_attrs *attrs)
  976. {
  977. unsigned long uaddr = vma->vm_start;
  978. unsigned long usize = vma->vm_end - vma->vm_start;
  979. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  980. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  981. if (!pages)
  982. return -ENXIO;
  983. do {
  984. int ret = vm_insert_page(vma, uaddr, *pages++);
  985. if (ret) {
  986. pr_err("Remapping memory failed: %d\n", ret);
  987. return ret;
  988. }
  989. uaddr += PAGE_SIZE;
  990. usize -= PAGE_SIZE;
  991. } while (usize > 0);
  992. return 0;
  993. }
  994. /*
  995. * free a page as defined by the above mapping.
  996. * Must not be called with IRQs disabled.
  997. */
  998. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  999. dma_addr_t handle, struct dma_attrs *attrs)
  1000. {
  1001. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1002. size = PAGE_ALIGN(size);
  1003. if (!pages) {
  1004. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1005. return;
  1006. }
  1007. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1008. unmap_kernel_range((unsigned long)cpu_addr, size);
  1009. vunmap(cpu_addr);
  1010. }
  1011. __iommu_remove_mapping(dev, handle, size);
  1012. __iommu_free_buffer(dev, pages, size);
  1013. }
  1014. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1015. void *cpu_addr, dma_addr_t dma_addr,
  1016. size_t size, struct dma_attrs *attrs)
  1017. {
  1018. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1019. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1020. if (!pages)
  1021. return -ENXIO;
  1022. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1023. GFP_KERNEL);
  1024. }
  1025. /*
  1026. * Map a part of the scatter-gather list into contiguous io address space
  1027. */
  1028. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1029. size_t size, dma_addr_t *handle,
  1030. enum dma_data_direction dir, struct dma_attrs *attrs)
  1031. {
  1032. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1033. dma_addr_t iova, iova_base;
  1034. int ret = 0;
  1035. unsigned int count;
  1036. struct scatterlist *s;
  1037. size = PAGE_ALIGN(size);
  1038. *handle = DMA_ERROR_CODE;
  1039. iova_base = iova = __alloc_iova(mapping, size);
  1040. if (iova == DMA_ERROR_CODE)
  1041. return -ENOMEM;
  1042. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1043. phys_addr_t phys = page_to_phys(sg_page(s));
  1044. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1045. if (!arch_is_coherent() &&
  1046. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1047. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1048. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1049. if (ret < 0)
  1050. goto fail;
  1051. count += len >> PAGE_SHIFT;
  1052. iova += len;
  1053. }
  1054. *handle = iova_base;
  1055. return 0;
  1056. fail:
  1057. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1058. __free_iova(mapping, iova_base, size);
  1059. return ret;
  1060. }
  1061. /**
  1062. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1063. * @dev: valid struct device pointer
  1064. * @sg: list of buffers
  1065. * @nents: number of buffers to map
  1066. * @dir: DMA transfer direction
  1067. *
  1068. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1069. * The scatter gather list elements are merged together (if possible) and
  1070. * tagged with the appropriate dma address and length. They are obtained via
  1071. * sg_dma_{address,length}.
  1072. */
  1073. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1074. enum dma_data_direction dir, struct dma_attrs *attrs)
  1075. {
  1076. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1077. int i, count = 0;
  1078. unsigned int offset = s->offset;
  1079. unsigned int size = s->offset + s->length;
  1080. unsigned int max = dma_get_max_seg_size(dev);
  1081. for (i = 1; i < nents; i++) {
  1082. s = sg_next(s);
  1083. s->dma_address = DMA_ERROR_CODE;
  1084. s->dma_length = 0;
  1085. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1086. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1087. dir, attrs) < 0)
  1088. goto bad_mapping;
  1089. dma->dma_address += offset;
  1090. dma->dma_length = size - offset;
  1091. size = offset = s->offset;
  1092. start = s;
  1093. dma = sg_next(dma);
  1094. count += 1;
  1095. }
  1096. size += s->length;
  1097. }
  1098. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs) < 0)
  1099. goto bad_mapping;
  1100. dma->dma_address += offset;
  1101. dma->dma_length = size - offset;
  1102. return count+1;
  1103. bad_mapping:
  1104. for_each_sg(sg, s, count, i)
  1105. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1106. return 0;
  1107. }
  1108. /**
  1109. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1110. * @dev: valid struct device pointer
  1111. * @sg: list of buffers
  1112. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1113. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1114. *
  1115. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1116. * rules concerning calls here are the same as for dma_unmap_single().
  1117. */
  1118. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1119. enum dma_data_direction dir, struct dma_attrs *attrs)
  1120. {
  1121. struct scatterlist *s;
  1122. int i;
  1123. for_each_sg(sg, s, nents, i) {
  1124. if (sg_dma_len(s))
  1125. __iommu_remove_mapping(dev, sg_dma_address(s),
  1126. sg_dma_len(s));
  1127. if (!arch_is_coherent() &&
  1128. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1129. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1130. s->length, dir);
  1131. }
  1132. }
  1133. /**
  1134. * arm_iommu_sync_sg_for_cpu
  1135. * @dev: valid struct device pointer
  1136. * @sg: list of buffers
  1137. * @nents: number of buffers to map (returned from dma_map_sg)
  1138. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1139. */
  1140. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1141. int nents, enum dma_data_direction dir)
  1142. {
  1143. struct scatterlist *s;
  1144. int i;
  1145. for_each_sg(sg, s, nents, i)
  1146. if (!arch_is_coherent())
  1147. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1148. }
  1149. /**
  1150. * arm_iommu_sync_sg_for_device
  1151. * @dev: valid struct device pointer
  1152. * @sg: list of buffers
  1153. * @nents: number of buffers to map (returned from dma_map_sg)
  1154. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1155. */
  1156. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1157. int nents, enum dma_data_direction dir)
  1158. {
  1159. struct scatterlist *s;
  1160. int i;
  1161. for_each_sg(sg, s, nents, i)
  1162. if (!arch_is_coherent())
  1163. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1164. }
  1165. /**
  1166. * arm_iommu_map_page
  1167. * @dev: valid struct device pointer
  1168. * @page: page that buffer resides in
  1169. * @offset: offset into page for start of buffer
  1170. * @size: size of buffer to map
  1171. * @dir: DMA transfer direction
  1172. *
  1173. * IOMMU aware version of arm_dma_map_page()
  1174. */
  1175. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1176. unsigned long offset, size_t size, enum dma_data_direction dir,
  1177. struct dma_attrs *attrs)
  1178. {
  1179. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1180. dma_addr_t dma_addr;
  1181. int ret, len = PAGE_ALIGN(size + offset);
  1182. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1183. __dma_page_cpu_to_dev(page, offset, size, dir);
  1184. dma_addr = __alloc_iova(mapping, len);
  1185. if (dma_addr == DMA_ERROR_CODE)
  1186. return dma_addr;
  1187. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
  1188. if (ret < 0)
  1189. goto fail;
  1190. return dma_addr + offset;
  1191. fail:
  1192. __free_iova(mapping, dma_addr, len);
  1193. return DMA_ERROR_CODE;
  1194. }
  1195. /**
  1196. * arm_iommu_unmap_page
  1197. * @dev: valid struct device pointer
  1198. * @handle: DMA address of buffer
  1199. * @size: size of buffer (same as passed to dma_map_page)
  1200. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1201. *
  1202. * IOMMU aware version of arm_dma_unmap_page()
  1203. */
  1204. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1205. size_t size, enum dma_data_direction dir,
  1206. struct dma_attrs *attrs)
  1207. {
  1208. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1209. dma_addr_t iova = handle & PAGE_MASK;
  1210. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1211. int offset = handle & ~PAGE_MASK;
  1212. int len = PAGE_ALIGN(size + offset);
  1213. if (!iova)
  1214. return;
  1215. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1216. __dma_page_dev_to_cpu(page, offset, size, dir);
  1217. iommu_unmap(mapping->domain, iova, len);
  1218. __free_iova(mapping, iova, len);
  1219. }
  1220. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1221. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1222. {
  1223. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1224. dma_addr_t iova = handle & PAGE_MASK;
  1225. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1226. unsigned int offset = handle & ~PAGE_MASK;
  1227. if (!iova)
  1228. return;
  1229. if (!arch_is_coherent())
  1230. __dma_page_dev_to_cpu(page, offset, size, dir);
  1231. }
  1232. static void arm_iommu_sync_single_for_device(struct device *dev,
  1233. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1234. {
  1235. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1236. dma_addr_t iova = handle & PAGE_MASK;
  1237. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1238. unsigned int offset = handle & ~PAGE_MASK;
  1239. if (!iova)
  1240. return;
  1241. __dma_page_cpu_to_dev(page, offset, size, dir);
  1242. }
  1243. struct dma_map_ops iommu_ops = {
  1244. .alloc = arm_iommu_alloc_attrs,
  1245. .free = arm_iommu_free_attrs,
  1246. .mmap = arm_iommu_mmap_attrs,
  1247. .get_sgtable = arm_iommu_get_sgtable,
  1248. .map_page = arm_iommu_map_page,
  1249. .unmap_page = arm_iommu_unmap_page,
  1250. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1251. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1252. .map_sg = arm_iommu_map_sg,
  1253. .unmap_sg = arm_iommu_unmap_sg,
  1254. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1255. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1256. };
  1257. /**
  1258. * arm_iommu_create_mapping
  1259. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1260. * @base: start address of the valid IO address space
  1261. * @size: size of the valid IO address space
  1262. * @order: accuracy of the IO addresses allocations
  1263. *
  1264. * Creates a mapping structure which holds information about used/unused
  1265. * IO address ranges, which is required to perform memory allocation and
  1266. * mapping with IOMMU aware functions.
  1267. *
  1268. * The client device need to be attached to the mapping with
  1269. * arm_iommu_attach_device function.
  1270. */
  1271. struct dma_iommu_mapping *
  1272. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
  1273. int order)
  1274. {
  1275. unsigned int count = size >> (PAGE_SHIFT + order);
  1276. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  1277. struct dma_iommu_mapping *mapping;
  1278. int err = -ENOMEM;
  1279. if (!count)
  1280. return ERR_PTR(-EINVAL);
  1281. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1282. if (!mapping)
  1283. goto err;
  1284. mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1285. if (!mapping->bitmap)
  1286. goto err2;
  1287. mapping->base = base;
  1288. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1289. mapping->order = order;
  1290. spin_lock_init(&mapping->lock);
  1291. mapping->domain = iommu_domain_alloc(bus);
  1292. if (!mapping->domain)
  1293. goto err3;
  1294. kref_init(&mapping->kref);
  1295. return mapping;
  1296. err3:
  1297. kfree(mapping->bitmap);
  1298. err2:
  1299. kfree(mapping);
  1300. err:
  1301. return ERR_PTR(err);
  1302. }
  1303. static void release_iommu_mapping(struct kref *kref)
  1304. {
  1305. struct dma_iommu_mapping *mapping =
  1306. container_of(kref, struct dma_iommu_mapping, kref);
  1307. iommu_domain_free(mapping->domain);
  1308. kfree(mapping->bitmap);
  1309. kfree(mapping);
  1310. }
  1311. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1312. {
  1313. if (mapping)
  1314. kref_put(&mapping->kref, release_iommu_mapping);
  1315. }
  1316. /**
  1317. * arm_iommu_attach_device
  1318. * @dev: valid struct device pointer
  1319. * @mapping: io address space mapping structure (returned from
  1320. * arm_iommu_create_mapping)
  1321. *
  1322. * Attaches specified io address space mapping to the provided device,
  1323. * this replaces the dma operations (dma_map_ops pointer) with the
  1324. * IOMMU aware version. More than one client might be attached to
  1325. * the same io address space mapping.
  1326. */
  1327. int arm_iommu_attach_device(struct device *dev,
  1328. struct dma_iommu_mapping *mapping)
  1329. {
  1330. int err;
  1331. err = iommu_attach_device(mapping->domain, dev);
  1332. if (err)
  1333. return err;
  1334. kref_get(&mapping->kref);
  1335. dev->archdata.mapping = mapping;
  1336. set_dma_ops(dev, &iommu_ops);
  1337. pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1338. return 0;
  1339. }
  1340. #endif