dma-mapping.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  241. struct dma_pool {
  242. size_t size;
  243. spinlock_t lock;
  244. unsigned long *bitmap;
  245. unsigned long nr_pages;
  246. void *vaddr;
  247. struct page **pages;
  248. };
  249. static struct dma_pool atomic_pool = {
  250. .size = DEFAULT_DMA_COHERENT_POOL_SIZE,
  251. };
  252. static int __init early_coherent_pool(char *p)
  253. {
  254. atomic_pool.size = memparse(p, &p);
  255. return 0;
  256. }
  257. early_param("coherent_pool", early_coherent_pool);
  258. void __init init_dma_coherent_pool_size(unsigned long size)
  259. {
  260. /*
  261. * Catch any attempt to set the pool size too late.
  262. */
  263. BUG_ON(atomic_pool.vaddr);
  264. /*
  265. * Set architecture specific coherent pool size only if
  266. * it has not been changed by kernel command line parameter.
  267. */
  268. if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
  269. atomic_pool.size = size;
  270. }
  271. /*
  272. * Initialise the coherent pool for atomic allocations.
  273. */
  274. static int __init atomic_pool_init(void)
  275. {
  276. struct dma_pool *pool = &atomic_pool;
  277. pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
  278. unsigned long nr_pages = pool->size >> PAGE_SHIFT;
  279. unsigned long *bitmap;
  280. struct page *page;
  281. struct page **pages;
  282. void *ptr;
  283. int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
  284. bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  285. if (!bitmap)
  286. goto no_bitmap;
  287. pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
  288. if (!pages)
  289. goto no_pages;
  290. if (IS_ENABLED(CONFIG_CMA))
  291. ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page);
  292. else
  293. ptr = __alloc_remap_buffer(NULL, pool->size, GFP_KERNEL, prot,
  294. &page, NULL);
  295. if (ptr) {
  296. int i;
  297. for (i = 0; i < nr_pages; i++)
  298. pages[i] = page + i;
  299. spin_lock_init(&pool->lock);
  300. pool->vaddr = ptr;
  301. pool->pages = pages;
  302. pool->bitmap = bitmap;
  303. pool->nr_pages = nr_pages;
  304. pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
  305. (unsigned)pool->size / 1024);
  306. return 0;
  307. }
  308. kfree(pages);
  309. no_pages:
  310. kfree(bitmap);
  311. no_bitmap:
  312. pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
  313. (unsigned)pool->size / 1024);
  314. return -ENOMEM;
  315. }
  316. /*
  317. * CMA is activated by core_initcall, so we must be called after it.
  318. */
  319. postcore_initcall(atomic_pool_init);
  320. struct dma_contig_early_reserve {
  321. phys_addr_t base;
  322. unsigned long size;
  323. };
  324. static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
  325. static int dma_mmu_remap_num __initdata;
  326. void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  327. {
  328. dma_mmu_remap[dma_mmu_remap_num].base = base;
  329. dma_mmu_remap[dma_mmu_remap_num].size = size;
  330. dma_mmu_remap_num++;
  331. }
  332. void __init dma_contiguous_remap(void)
  333. {
  334. int i;
  335. for (i = 0; i < dma_mmu_remap_num; i++) {
  336. phys_addr_t start = dma_mmu_remap[i].base;
  337. phys_addr_t end = start + dma_mmu_remap[i].size;
  338. struct map_desc map;
  339. unsigned long addr;
  340. if (end > arm_lowmem_limit)
  341. end = arm_lowmem_limit;
  342. if (start >= end)
  343. continue;
  344. map.pfn = __phys_to_pfn(start);
  345. map.virtual = __phys_to_virt(start);
  346. map.length = end - start;
  347. map.type = MT_MEMORY_DMA_READY;
  348. /*
  349. * Clear previous low-memory mapping
  350. */
  351. for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
  352. addr += PMD_SIZE)
  353. pmd_clear(pmd_off_k(addr));
  354. iotable_init(&map, 1);
  355. }
  356. }
  357. static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
  358. void *data)
  359. {
  360. struct page *page = virt_to_page(addr);
  361. pgprot_t prot = *(pgprot_t *)data;
  362. set_pte_ext(pte, mk_pte(page, prot), 0);
  363. return 0;
  364. }
  365. static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
  366. {
  367. unsigned long start = (unsigned long) page_address(page);
  368. unsigned end = start + size;
  369. apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
  370. dsb();
  371. flush_tlb_kernel_range(start, end);
  372. }
  373. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  374. pgprot_t prot, struct page **ret_page,
  375. const void *caller)
  376. {
  377. struct page *page;
  378. void *ptr;
  379. page = __dma_alloc_buffer(dev, size, gfp);
  380. if (!page)
  381. return NULL;
  382. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  383. if (!ptr) {
  384. __dma_free_buffer(page, size);
  385. return NULL;
  386. }
  387. *ret_page = page;
  388. return ptr;
  389. }
  390. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  391. {
  392. struct dma_pool *pool = &atomic_pool;
  393. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  394. unsigned int pageno;
  395. unsigned long flags;
  396. void *ptr = NULL;
  397. unsigned long align_mask;
  398. if (!pool->vaddr) {
  399. WARN(1, "coherent pool not initialised!\n");
  400. return NULL;
  401. }
  402. /*
  403. * Align the region allocation - allocations from pool are rather
  404. * small, so align them to their order in pages, minimum is a page
  405. * size. This helps reduce fragmentation of the DMA space.
  406. */
  407. align_mask = (1 << get_order(size)) - 1;
  408. spin_lock_irqsave(&pool->lock, flags);
  409. pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
  410. 0, count, align_mask);
  411. if (pageno < pool->nr_pages) {
  412. bitmap_set(pool->bitmap, pageno, count);
  413. ptr = pool->vaddr + PAGE_SIZE * pageno;
  414. *ret_page = pool->pages[pageno];
  415. } else {
  416. pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
  417. "Please increase it with coherent_pool= kernel parameter!\n",
  418. (unsigned)pool->size / 1024);
  419. }
  420. spin_unlock_irqrestore(&pool->lock, flags);
  421. return ptr;
  422. }
  423. static bool __in_atomic_pool(void *start, size_t size)
  424. {
  425. struct dma_pool *pool = &atomic_pool;
  426. void *end = start + size;
  427. void *pool_start = pool->vaddr;
  428. void *pool_end = pool->vaddr + pool->size;
  429. if (start < pool_start || start >= pool_end)
  430. return false;
  431. if (end <= pool_end)
  432. return true;
  433. WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
  434. start, end - 1, pool_start, pool_end - 1);
  435. return false;
  436. }
  437. static int __free_from_pool(void *start, size_t size)
  438. {
  439. struct dma_pool *pool = &atomic_pool;
  440. unsigned long pageno, count;
  441. unsigned long flags;
  442. if (!__in_atomic_pool(start, size))
  443. return 0;
  444. pageno = (start - pool->vaddr) >> PAGE_SHIFT;
  445. count = size >> PAGE_SHIFT;
  446. spin_lock_irqsave(&pool->lock, flags);
  447. bitmap_clear(pool->bitmap, pageno, count);
  448. spin_unlock_irqrestore(&pool->lock, flags);
  449. return 1;
  450. }
  451. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  452. pgprot_t prot, struct page **ret_page)
  453. {
  454. unsigned long order = get_order(size);
  455. size_t count = size >> PAGE_SHIFT;
  456. struct page *page;
  457. page = dma_alloc_from_contiguous(dev, count, order);
  458. if (!page)
  459. return NULL;
  460. __dma_clear_buffer(page, size);
  461. __dma_remap(page, size, prot);
  462. *ret_page = page;
  463. return page_address(page);
  464. }
  465. static void __free_from_contiguous(struct device *dev, struct page *page,
  466. size_t size)
  467. {
  468. __dma_remap(page, size, pgprot_kernel);
  469. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  470. }
  471. static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
  472. {
  473. prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
  474. pgprot_writecombine(prot) :
  475. pgprot_dmacoherent(prot);
  476. return prot;
  477. }
  478. #define nommu() 0
  479. #else /* !CONFIG_MMU */
  480. #define nommu() 1
  481. #define __get_dma_pgprot(attrs, prot) __pgprot(0)
  482. #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
  483. #define __alloc_from_pool(size, ret_page) NULL
  484. #define __alloc_from_contiguous(dev, size, prot, ret) NULL
  485. #define __free_from_pool(cpu_addr, size) 0
  486. #define __free_from_contiguous(dev, page, size) do { } while (0)
  487. #define __dma_free_remap(cpu_addr, size) do { } while (0)
  488. #endif /* CONFIG_MMU */
  489. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  490. struct page **ret_page)
  491. {
  492. struct page *page;
  493. page = __dma_alloc_buffer(dev, size, gfp);
  494. if (!page)
  495. return NULL;
  496. *ret_page = page;
  497. return page_address(page);
  498. }
  499. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  500. gfp_t gfp, pgprot_t prot, const void *caller)
  501. {
  502. u64 mask = get_coherent_dma_mask(dev);
  503. struct page *page;
  504. void *addr;
  505. #ifdef CONFIG_DMA_API_DEBUG
  506. u64 limit = (mask + 1) & ~mask;
  507. if (limit && size >= limit) {
  508. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  509. size, mask);
  510. return NULL;
  511. }
  512. #endif
  513. if (!mask)
  514. return NULL;
  515. if (mask < 0xffffffffULL)
  516. gfp |= GFP_DMA;
  517. /*
  518. * Following is a work-around (a.k.a. hack) to prevent pages
  519. * with __GFP_COMP being passed to split_page() which cannot
  520. * handle them. The real problem is that this flag probably
  521. * should be 0 on ARM as it is not supported on this
  522. * platform; see CONFIG_HUGETLBFS.
  523. */
  524. gfp &= ~(__GFP_COMP);
  525. *handle = DMA_ERROR_CODE;
  526. size = PAGE_ALIGN(size);
  527. if (arch_is_coherent() || nommu())
  528. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  529. else if (gfp & GFP_ATOMIC)
  530. addr = __alloc_from_pool(size, &page);
  531. else if (!IS_ENABLED(CONFIG_CMA))
  532. addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
  533. else
  534. addr = __alloc_from_contiguous(dev, size, prot, &page);
  535. if (addr)
  536. *handle = pfn_to_dma(dev, page_to_pfn(page));
  537. return addr;
  538. }
  539. /*
  540. * Allocate DMA-coherent memory space and return both the kernel remapped
  541. * virtual and bus address for that space.
  542. */
  543. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  544. gfp_t gfp, struct dma_attrs *attrs)
  545. {
  546. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  547. void *memory;
  548. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  549. return memory;
  550. return __dma_alloc(dev, size, handle, gfp, prot,
  551. __builtin_return_address(0));
  552. }
  553. /*
  554. * Create userspace mapping for the DMA-coherent memory.
  555. */
  556. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  557. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  558. struct dma_attrs *attrs)
  559. {
  560. int ret = -ENXIO;
  561. #ifdef CONFIG_MMU
  562. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  563. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  564. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  565. unsigned long off = vma->vm_pgoff;
  566. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  567. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  568. return ret;
  569. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  570. ret = remap_pfn_range(vma, vma->vm_start,
  571. pfn + off,
  572. vma->vm_end - vma->vm_start,
  573. vma->vm_page_prot);
  574. }
  575. #endif /* CONFIG_MMU */
  576. return ret;
  577. }
  578. /*
  579. * Free a buffer as defined by the above mapping.
  580. */
  581. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  582. dma_addr_t handle, struct dma_attrs *attrs)
  583. {
  584. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  585. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  586. return;
  587. size = PAGE_ALIGN(size);
  588. if (arch_is_coherent() || nommu()) {
  589. __dma_free_buffer(page, size);
  590. } else if (__free_from_pool(cpu_addr, size)) {
  591. return;
  592. } else if (!IS_ENABLED(CONFIG_CMA)) {
  593. __dma_free_remap(cpu_addr, size);
  594. __dma_free_buffer(page, size);
  595. } else {
  596. /*
  597. * Non-atomic allocations cannot be freed with IRQs disabled
  598. */
  599. WARN_ON(irqs_disabled());
  600. __free_from_contiguous(dev, page, size);
  601. }
  602. }
  603. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  604. void *cpu_addr, dma_addr_t handle, size_t size,
  605. struct dma_attrs *attrs)
  606. {
  607. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  608. int ret;
  609. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  610. if (unlikely(ret))
  611. return ret;
  612. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  613. return 0;
  614. }
  615. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  616. size_t size, enum dma_data_direction dir,
  617. void (*op)(const void *, size_t, int))
  618. {
  619. /*
  620. * A single sg entry may refer to multiple physically contiguous
  621. * pages. But we still need to process highmem pages individually.
  622. * If highmem is not configured then the bulk of this loop gets
  623. * optimized out.
  624. */
  625. size_t left = size;
  626. do {
  627. size_t len = left;
  628. void *vaddr;
  629. if (PageHighMem(page)) {
  630. if (len + offset > PAGE_SIZE) {
  631. if (offset >= PAGE_SIZE) {
  632. page += offset / PAGE_SIZE;
  633. offset %= PAGE_SIZE;
  634. }
  635. len = PAGE_SIZE - offset;
  636. }
  637. vaddr = kmap_high_get(page);
  638. if (vaddr) {
  639. vaddr += offset;
  640. op(vaddr, len, dir);
  641. kunmap_high(page);
  642. } else if (cache_is_vipt()) {
  643. /* unmapped pages might still be cached */
  644. vaddr = kmap_atomic(page);
  645. op(vaddr + offset, len, dir);
  646. kunmap_atomic(vaddr);
  647. }
  648. } else {
  649. vaddr = page_address(page) + offset;
  650. op(vaddr, len, dir);
  651. }
  652. offset = 0;
  653. page++;
  654. left -= len;
  655. } while (left);
  656. }
  657. /*
  658. * Make an area consistent for devices.
  659. * Note: Drivers should NOT use this function directly, as it will break
  660. * platforms with CONFIG_DMABOUNCE.
  661. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  662. */
  663. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  664. size_t size, enum dma_data_direction dir)
  665. {
  666. unsigned long paddr;
  667. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  668. paddr = page_to_phys(page) + off;
  669. if (dir == DMA_FROM_DEVICE) {
  670. outer_inv_range(paddr, paddr + size);
  671. } else {
  672. outer_clean_range(paddr, paddr + size);
  673. }
  674. /* FIXME: non-speculating: flush on bidirectional mappings? */
  675. }
  676. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  677. size_t size, enum dma_data_direction dir)
  678. {
  679. unsigned long paddr = page_to_phys(page) + off;
  680. /* FIXME: non-speculating: not required */
  681. /* don't bother invalidating if DMA to device */
  682. if (dir != DMA_TO_DEVICE)
  683. outer_inv_range(paddr, paddr + size);
  684. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  685. /*
  686. * Mark the D-cache clean for this page to avoid extra flushing.
  687. */
  688. if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
  689. set_bit(PG_dcache_clean, &page->flags);
  690. }
  691. /**
  692. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  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 map
  696. * @dir: DMA transfer direction
  697. *
  698. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  699. * This is the scatter-gather version of the dma_map_single interface.
  700. * Here the scatter gather list elements are each tagged with the
  701. * appropriate dma address and length. They are obtained via
  702. * sg_dma_{address,length}.
  703. *
  704. * Device ownership issues as mentioned for dma_map_single are the same
  705. * here.
  706. */
  707. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  708. enum dma_data_direction dir, struct dma_attrs *attrs)
  709. {
  710. struct dma_map_ops *ops = get_dma_ops(dev);
  711. struct scatterlist *s;
  712. int i, j;
  713. for_each_sg(sg, s, nents, i) {
  714. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  715. s->dma_length = s->length;
  716. #endif
  717. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  718. s->length, dir, attrs);
  719. if (dma_mapping_error(dev, s->dma_address))
  720. goto bad_mapping;
  721. }
  722. return nents;
  723. bad_mapping:
  724. for_each_sg(sg, s, i, j)
  725. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  726. return 0;
  727. }
  728. /**
  729. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  730. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  731. * @sg: list of buffers
  732. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  733. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  734. *
  735. * Unmap a set of streaming mode DMA translations. Again, CPU access
  736. * rules concerning calls here are the same as for dma_unmap_single().
  737. */
  738. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  739. enum dma_data_direction dir, struct dma_attrs *attrs)
  740. {
  741. struct dma_map_ops *ops = get_dma_ops(dev);
  742. struct scatterlist *s;
  743. int i;
  744. for_each_sg(sg, s, nents, i)
  745. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  746. }
  747. /**
  748. * arm_dma_sync_sg_for_cpu
  749. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  750. * @sg: list of buffers
  751. * @nents: number of buffers to map (returned from dma_map_sg)
  752. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  753. */
  754. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  755. int nents, enum dma_data_direction dir)
  756. {
  757. struct dma_map_ops *ops = get_dma_ops(dev);
  758. struct scatterlist *s;
  759. int i;
  760. for_each_sg(sg, s, nents, i)
  761. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  762. dir);
  763. }
  764. /**
  765. * arm_dma_sync_sg_for_device
  766. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  767. * @sg: list of buffers
  768. * @nents: number of buffers to map (returned from dma_map_sg)
  769. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  770. */
  771. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  772. int nents, enum dma_data_direction dir)
  773. {
  774. struct dma_map_ops *ops = get_dma_ops(dev);
  775. struct scatterlist *s;
  776. int i;
  777. for_each_sg(sg, s, nents, i)
  778. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  779. dir);
  780. }
  781. /*
  782. * Return whether the given device DMA address mask can be supported
  783. * properly. For example, if your device can only drive the low 24-bits
  784. * during bus mastering, then you would pass 0x00ffffff as the mask
  785. * to this function.
  786. */
  787. int dma_supported(struct device *dev, u64 mask)
  788. {
  789. if (mask < (u64)arm_dma_limit)
  790. return 0;
  791. return 1;
  792. }
  793. EXPORT_SYMBOL(dma_supported);
  794. static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  795. {
  796. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  797. return -EIO;
  798. *dev->dma_mask = dma_mask;
  799. return 0;
  800. }
  801. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  802. static int __init dma_debug_do_init(void)
  803. {
  804. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  805. return 0;
  806. }
  807. fs_initcall(dma_debug_do_init);
  808. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  809. /* IOMMU */
  810. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  811. size_t size)
  812. {
  813. unsigned int order = get_order(size);
  814. unsigned int align = 0;
  815. unsigned int count, start;
  816. unsigned long flags;
  817. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  818. (1 << mapping->order) - 1) >> mapping->order;
  819. if (order > mapping->order)
  820. align = (1 << (order - mapping->order)) - 1;
  821. spin_lock_irqsave(&mapping->lock, flags);
  822. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  823. count, align);
  824. if (start > mapping->bits) {
  825. spin_unlock_irqrestore(&mapping->lock, flags);
  826. return DMA_ERROR_CODE;
  827. }
  828. bitmap_set(mapping->bitmap, start, count);
  829. spin_unlock_irqrestore(&mapping->lock, flags);
  830. return mapping->base + (start << (mapping->order + PAGE_SHIFT));
  831. }
  832. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  833. dma_addr_t addr, size_t size)
  834. {
  835. unsigned int start = (addr - mapping->base) >>
  836. (mapping->order + PAGE_SHIFT);
  837. unsigned int count = ((size >> PAGE_SHIFT) +
  838. (1 << mapping->order) - 1) >> mapping->order;
  839. unsigned long flags;
  840. spin_lock_irqsave(&mapping->lock, flags);
  841. bitmap_clear(mapping->bitmap, start, count);
  842. spin_unlock_irqrestore(&mapping->lock, flags);
  843. }
  844. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  845. {
  846. struct page **pages;
  847. int count = size >> PAGE_SHIFT;
  848. int array_size = count * sizeof(struct page *);
  849. int i = 0;
  850. if (array_size <= PAGE_SIZE)
  851. pages = kzalloc(array_size, gfp);
  852. else
  853. pages = vzalloc(array_size);
  854. if (!pages)
  855. return NULL;
  856. while (count) {
  857. int j, order = __fls(count);
  858. pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
  859. while (!pages[i] && order)
  860. pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
  861. if (!pages[i])
  862. goto error;
  863. if (order)
  864. split_page(pages[i], order);
  865. j = 1 << order;
  866. while (--j)
  867. pages[i + j] = pages[i] + j;
  868. __dma_clear_buffer(pages[i], PAGE_SIZE << order);
  869. i += 1 << order;
  870. count -= 1 << order;
  871. }
  872. return pages;
  873. error:
  874. while (i--)
  875. if (pages[i])
  876. __free_pages(pages[i], 0);
  877. if (array_size <= PAGE_SIZE)
  878. kfree(pages);
  879. else
  880. vfree(pages);
  881. return NULL;
  882. }
  883. static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size)
  884. {
  885. int count = size >> PAGE_SHIFT;
  886. int array_size = count * sizeof(struct page *);
  887. int i;
  888. for (i = 0; i < count; i++)
  889. if (pages[i])
  890. __free_pages(pages[i], 0);
  891. if (array_size <= PAGE_SIZE)
  892. kfree(pages);
  893. else
  894. vfree(pages);
  895. return 0;
  896. }
  897. /*
  898. * Create a CPU mapping for a specified pages
  899. */
  900. static void *
  901. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  902. const void *caller)
  903. {
  904. unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  905. struct vm_struct *area;
  906. unsigned long p;
  907. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  908. caller);
  909. if (!area)
  910. return NULL;
  911. area->pages = pages;
  912. area->nr_pages = nr_pages;
  913. p = (unsigned long)area->addr;
  914. for (i = 0; i < nr_pages; i++) {
  915. phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
  916. if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
  917. goto err;
  918. p += PAGE_SIZE;
  919. }
  920. return area->addr;
  921. err:
  922. unmap_kernel_range((unsigned long)area->addr, size);
  923. vunmap(area->addr);
  924. return NULL;
  925. }
  926. /*
  927. * Create a mapping in device IO address space for specified pages
  928. */
  929. static dma_addr_t
  930. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  931. {
  932. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  933. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  934. dma_addr_t dma_addr, iova;
  935. int i, ret = DMA_ERROR_CODE;
  936. dma_addr = __alloc_iova(mapping, size);
  937. if (dma_addr == DMA_ERROR_CODE)
  938. return dma_addr;
  939. iova = dma_addr;
  940. for (i = 0; i < count; ) {
  941. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  942. phys_addr_t phys = page_to_phys(pages[i]);
  943. unsigned int len, j;
  944. for (j = i + 1; j < count; j++, next_pfn++)
  945. if (page_to_pfn(pages[j]) != next_pfn)
  946. break;
  947. len = (j - i) << PAGE_SHIFT;
  948. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  949. if (ret < 0)
  950. goto fail;
  951. iova += len;
  952. i = j;
  953. }
  954. return dma_addr;
  955. fail:
  956. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  957. __free_iova(mapping, dma_addr, size);
  958. return DMA_ERROR_CODE;
  959. }
  960. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  961. {
  962. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  963. /*
  964. * add optional in-page offset from iova to size and align
  965. * result to page size
  966. */
  967. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  968. iova &= PAGE_MASK;
  969. iommu_unmap(mapping->domain, iova, size);
  970. __free_iova(mapping, iova, size);
  971. return 0;
  972. }
  973. static struct page **__atomic_get_pages(void *addr)
  974. {
  975. struct dma_pool *pool = &atomic_pool;
  976. struct page **pages = pool->pages;
  977. int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
  978. return pages + offs;
  979. }
  980. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  981. {
  982. struct vm_struct *area;
  983. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  984. return __atomic_get_pages(cpu_addr);
  985. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  986. return cpu_addr;
  987. area = find_vm_area(cpu_addr);
  988. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  989. return area->pages;
  990. return NULL;
  991. }
  992. static void *__iommu_alloc_atomic(struct device *dev, size_t size,
  993. dma_addr_t *handle)
  994. {
  995. struct page *page;
  996. void *addr;
  997. addr = __alloc_from_pool(size, &page);
  998. if (!addr)
  999. return NULL;
  1000. *handle = __iommu_create_mapping(dev, &page, size);
  1001. if (*handle == DMA_ERROR_CODE)
  1002. goto err_mapping;
  1003. return addr;
  1004. err_mapping:
  1005. __free_from_pool(addr, size);
  1006. return NULL;
  1007. }
  1008. static void __iommu_free_atomic(struct device *dev, struct page **pages,
  1009. dma_addr_t handle, size_t size)
  1010. {
  1011. __iommu_remove_mapping(dev, handle, size);
  1012. __free_from_pool(page_address(pages[0]), size);
  1013. }
  1014. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1015. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  1016. {
  1017. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  1018. struct page **pages;
  1019. void *addr = NULL;
  1020. *handle = DMA_ERROR_CODE;
  1021. size = PAGE_ALIGN(size);
  1022. if (gfp & GFP_ATOMIC)
  1023. return __iommu_alloc_atomic(dev, size, handle);
  1024. pages = __iommu_alloc_buffer(dev, size, gfp);
  1025. if (!pages)
  1026. return NULL;
  1027. *handle = __iommu_create_mapping(dev, pages, size);
  1028. if (*handle == DMA_ERROR_CODE)
  1029. goto err_buffer;
  1030. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1031. return pages;
  1032. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1033. __builtin_return_address(0));
  1034. if (!addr)
  1035. goto err_mapping;
  1036. return addr;
  1037. err_mapping:
  1038. __iommu_remove_mapping(dev, *handle, size);
  1039. err_buffer:
  1040. __iommu_free_buffer(dev, pages, size);
  1041. return NULL;
  1042. }
  1043. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1044. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1045. struct dma_attrs *attrs)
  1046. {
  1047. unsigned long uaddr = vma->vm_start;
  1048. unsigned long usize = vma->vm_end - vma->vm_start;
  1049. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1050. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1051. if (!pages)
  1052. return -ENXIO;
  1053. do {
  1054. int ret = vm_insert_page(vma, uaddr, *pages++);
  1055. if (ret) {
  1056. pr_err("Remapping memory failed: %d\n", ret);
  1057. return ret;
  1058. }
  1059. uaddr += PAGE_SIZE;
  1060. usize -= PAGE_SIZE;
  1061. } while (usize > 0);
  1062. return 0;
  1063. }
  1064. /*
  1065. * free a page as defined by the above mapping.
  1066. * Must not be called with IRQs disabled.
  1067. */
  1068. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1069. dma_addr_t handle, struct dma_attrs *attrs)
  1070. {
  1071. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1072. size = PAGE_ALIGN(size);
  1073. if (!pages) {
  1074. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1075. return;
  1076. }
  1077. if (__in_atomic_pool(cpu_addr, size)) {
  1078. __iommu_free_atomic(dev, pages, handle, size);
  1079. return;
  1080. }
  1081. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1082. unmap_kernel_range((unsigned long)cpu_addr, size);
  1083. vunmap(cpu_addr);
  1084. }
  1085. __iommu_remove_mapping(dev, handle, size);
  1086. __iommu_free_buffer(dev, pages, size);
  1087. }
  1088. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1089. void *cpu_addr, dma_addr_t dma_addr,
  1090. size_t size, struct dma_attrs *attrs)
  1091. {
  1092. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1093. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1094. if (!pages)
  1095. return -ENXIO;
  1096. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1097. GFP_KERNEL);
  1098. }
  1099. /*
  1100. * Map a part of the scatter-gather list into contiguous io address space
  1101. */
  1102. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1103. size_t size, dma_addr_t *handle,
  1104. enum dma_data_direction dir, struct dma_attrs *attrs)
  1105. {
  1106. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1107. dma_addr_t iova, iova_base;
  1108. int ret = 0;
  1109. unsigned int count;
  1110. struct scatterlist *s;
  1111. size = PAGE_ALIGN(size);
  1112. *handle = DMA_ERROR_CODE;
  1113. iova_base = iova = __alloc_iova(mapping, size);
  1114. if (iova == DMA_ERROR_CODE)
  1115. return -ENOMEM;
  1116. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1117. phys_addr_t phys = page_to_phys(sg_page(s));
  1118. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1119. if (!arch_is_coherent() &&
  1120. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1121. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1122. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1123. if (ret < 0)
  1124. goto fail;
  1125. count += len >> PAGE_SHIFT;
  1126. iova += len;
  1127. }
  1128. *handle = iova_base;
  1129. return 0;
  1130. fail:
  1131. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1132. __free_iova(mapping, iova_base, size);
  1133. return ret;
  1134. }
  1135. /**
  1136. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1137. * @dev: valid struct device pointer
  1138. * @sg: list of buffers
  1139. * @nents: number of buffers to map
  1140. * @dir: DMA transfer direction
  1141. *
  1142. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1143. * The scatter gather list elements are merged together (if possible) and
  1144. * tagged with the appropriate dma address and length. They are obtained via
  1145. * sg_dma_{address,length}.
  1146. */
  1147. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1148. enum dma_data_direction dir, struct dma_attrs *attrs)
  1149. {
  1150. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1151. int i, count = 0;
  1152. unsigned int offset = s->offset;
  1153. unsigned int size = s->offset + s->length;
  1154. unsigned int max = dma_get_max_seg_size(dev);
  1155. for (i = 1; i < nents; i++) {
  1156. s = sg_next(s);
  1157. s->dma_address = DMA_ERROR_CODE;
  1158. s->dma_length = 0;
  1159. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1160. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1161. dir, attrs) < 0)
  1162. goto bad_mapping;
  1163. dma->dma_address += offset;
  1164. dma->dma_length = size - offset;
  1165. size = offset = s->offset;
  1166. start = s;
  1167. dma = sg_next(dma);
  1168. count += 1;
  1169. }
  1170. size += s->length;
  1171. }
  1172. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs) < 0)
  1173. goto bad_mapping;
  1174. dma->dma_address += offset;
  1175. dma->dma_length = size - offset;
  1176. return count+1;
  1177. bad_mapping:
  1178. for_each_sg(sg, s, count, i)
  1179. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1180. return 0;
  1181. }
  1182. /**
  1183. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1184. * @dev: valid struct device pointer
  1185. * @sg: list of buffers
  1186. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1187. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1188. *
  1189. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1190. * rules concerning calls here are the same as for dma_unmap_single().
  1191. */
  1192. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1193. enum dma_data_direction dir, struct dma_attrs *attrs)
  1194. {
  1195. struct scatterlist *s;
  1196. int i;
  1197. for_each_sg(sg, s, nents, i) {
  1198. if (sg_dma_len(s))
  1199. __iommu_remove_mapping(dev, sg_dma_address(s),
  1200. sg_dma_len(s));
  1201. if (!arch_is_coherent() &&
  1202. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1203. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1204. s->length, dir);
  1205. }
  1206. }
  1207. /**
  1208. * arm_iommu_sync_sg_for_cpu
  1209. * @dev: valid struct device pointer
  1210. * @sg: list of buffers
  1211. * @nents: number of buffers to map (returned from dma_map_sg)
  1212. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1213. */
  1214. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1215. int nents, enum dma_data_direction dir)
  1216. {
  1217. struct scatterlist *s;
  1218. int i;
  1219. for_each_sg(sg, s, nents, i)
  1220. if (!arch_is_coherent())
  1221. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1222. }
  1223. /**
  1224. * arm_iommu_sync_sg_for_device
  1225. * @dev: valid struct device pointer
  1226. * @sg: list of buffers
  1227. * @nents: number of buffers to map (returned from dma_map_sg)
  1228. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1229. */
  1230. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1231. int nents, enum dma_data_direction dir)
  1232. {
  1233. struct scatterlist *s;
  1234. int i;
  1235. for_each_sg(sg, s, nents, i)
  1236. if (!arch_is_coherent())
  1237. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1238. }
  1239. /**
  1240. * arm_iommu_map_page
  1241. * @dev: valid struct device pointer
  1242. * @page: page that buffer resides in
  1243. * @offset: offset into page for start of buffer
  1244. * @size: size of buffer to map
  1245. * @dir: DMA transfer direction
  1246. *
  1247. * IOMMU aware version of arm_dma_map_page()
  1248. */
  1249. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1250. unsigned long offset, size_t size, enum dma_data_direction dir,
  1251. struct dma_attrs *attrs)
  1252. {
  1253. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1254. dma_addr_t dma_addr;
  1255. int ret, len = PAGE_ALIGN(size + offset);
  1256. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1257. __dma_page_cpu_to_dev(page, offset, size, dir);
  1258. dma_addr = __alloc_iova(mapping, len);
  1259. if (dma_addr == DMA_ERROR_CODE)
  1260. return dma_addr;
  1261. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
  1262. if (ret < 0)
  1263. goto fail;
  1264. return dma_addr + offset;
  1265. fail:
  1266. __free_iova(mapping, dma_addr, len);
  1267. return DMA_ERROR_CODE;
  1268. }
  1269. /**
  1270. * arm_iommu_unmap_page
  1271. * @dev: valid struct device pointer
  1272. * @handle: DMA address of buffer
  1273. * @size: size of buffer (same as passed to dma_map_page)
  1274. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1275. *
  1276. * IOMMU aware version of arm_dma_unmap_page()
  1277. */
  1278. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1279. size_t size, enum dma_data_direction dir,
  1280. struct dma_attrs *attrs)
  1281. {
  1282. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1283. dma_addr_t iova = handle & PAGE_MASK;
  1284. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1285. int offset = handle & ~PAGE_MASK;
  1286. int len = PAGE_ALIGN(size + offset);
  1287. if (!iova)
  1288. return;
  1289. if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1290. __dma_page_dev_to_cpu(page, offset, size, dir);
  1291. iommu_unmap(mapping->domain, iova, len);
  1292. __free_iova(mapping, iova, len);
  1293. }
  1294. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1295. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1296. {
  1297. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1298. dma_addr_t iova = handle & PAGE_MASK;
  1299. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1300. unsigned int offset = handle & ~PAGE_MASK;
  1301. if (!iova)
  1302. return;
  1303. if (!arch_is_coherent())
  1304. __dma_page_dev_to_cpu(page, offset, size, dir);
  1305. }
  1306. static void arm_iommu_sync_single_for_device(struct device *dev,
  1307. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1308. {
  1309. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1310. dma_addr_t iova = handle & PAGE_MASK;
  1311. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1312. unsigned int offset = handle & ~PAGE_MASK;
  1313. if (!iova)
  1314. return;
  1315. __dma_page_cpu_to_dev(page, offset, size, dir);
  1316. }
  1317. struct dma_map_ops iommu_ops = {
  1318. .alloc = arm_iommu_alloc_attrs,
  1319. .free = arm_iommu_free_attrs,
  1320. .mmap = arm_iommu_mmap_attrs,
  1321. .get_sgtable = arm_iommu_get_sgtable,
  1322. .map_page = arm_iommu_map_page,
  1323. .unmap_page = arm_iommu_unmap_page,
  1324. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1325. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1326. .map_sg = arm_iommu_map_sg,
  1327. .unmap_sg = arm_iommu_unmap_sg,
  1328. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1329. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1330. };
  1331. /**
  1332. * arm_iommu_create_mapping
  1333. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1334. * @base: start address of the valid IO address space
  1335. * @size: size of the valid IO address space
  1336. * @order: accuracy of the IO addresses allocations
  1337. *
  1338. * Creates a mapping structure which holds information about used/unused
  1339. * IO address ranges, which is required to perform memory allocation and
  1340. * mapping with IOMMU aware functions.
  1341. *
  1342. * The client device need to be attached to the mapping with
  1343. * arm_iommu_attach_device function.
  1344. */
  1345. struct dma_iommu_mapping *
  1346. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
  1347. int order)
  1348. {
  1349. unsigned int count = size >> (PAGE_SHIFT + order);
  1350. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  1351. struct dma_iommu_mapping *mapping;
  1352. int err = -ENOMEM;
  1353. if (!count)
  1354. return ERR_PTR(-EINVAL);
  1355. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1356. if (!mapping)
  1357. goto err;
  1358. mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1359. if (!mapping->bitmap)
  1360. goto err2;
  1361. mapping->base = base;
  1362. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1363. mapping->order = order;
  1364. spin_lock_init(&mapping->lock);
  1365. mapping->domain = iommu_domain_alloc(bus);
  1366. if (!mapping->domain)
  1367. goto err3;
  1368. kref_init(&mapping->kref);
  1369. return mapping;
  1370. err3:
  1371. kfree(mapping->bitmap);
  1372. err2:
  1373. kfree(mapping);
  1374. err:
  1375. return ERR_PTR(err);
  1376. }
  1377. static void release_iommu_mapping(struct kref *kref)
  1378. {
  1379. struct dma_iommu_mapping *mapping =
  1380. container_of(kref, struct dma_iommu_mapping, kref);
  1381. iommu_domain_free(mapping->domain);
  1382. kfree(mapping->bitmap);
  1383. kfree(mapping);
  1384. }
  1385. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1386. {
  1387. if (mapping)
  1388. kref_put(&mapping->kref, release_iommu_mapping);
  1389. }
  1390. /**
  1391. * arm_iommu_attach_device
  1392. * @dev: valid struct device pointer
  1393. * @mapping: io address space mapping structure (returned from
  1394. * arm_iommu_create_mapping)
  1395. *
  1396. * Attaches specified io address space mapping to the provided device,
  1397. * this replaces the dma operations (dma_map_ops pointer) with the
  1398. * IOMMU aware version. More than one client might be attached to
  1399. * the same io address space mapping.
  1400. */
  1401. int arm_iommu_attach_device(struct device *dev,
  1402. struct dma_iommu_mapping *mapping)
  1403. {
  1404. int err;
  1405. err = iommu_attach_device(mapping->domain, dev);
  1406. if (err)
  1407. return err;
  1408. kref_get(&mapping->kref);
  1409. dev->archdata.mapping = mapping;
  1410. set_dma_ops(dev, &iommu_ops);
  1411. pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1412. return 0;
  1413. }
  1414. #endif