dma-mapping.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 (!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. static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
  77. unsigned long offset, size_t size, enum dma_data_direction dir,
  78. struct dma_attrs *attrs)
  79. {
  80. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  81. }
  82. /**
  83. * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  84. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  85. * @handle: DMA address of buffer
  86. * @size: size of buffer (same as passed to dma_map_page)
  87. * @dir: DMA transfer direction (same as passed to dma_map_page)
  88. *
  89. * Unmap a page streaming mode DMA translation. The handle and size
  90. * must match what was provided in the previous dma_map_page() call.
  91. * All other usages are undefined.
  92. *
  93. * After this call, reads by the CPU to the buffer are guaranteed to see
  94. * whatever the device wrote there.
  95. */
  96. static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
  97. size_t size, enum dma_data_direction dir,
  98. struct dma_attrs *attrs)
  99. {
  100. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  101. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
  102. handle & ~PAGE_MASK, size, dir);
  103. }
  104. static void arm_dma_sync_single_for_cpu(struct device *dev,
  105. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  106. {
  107. unsigned int offset = handle & (PAGE_SIZE - 1);
  108. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  109. __dma_page_dev_to_cpu(page, offset, size, dir);
  110. }
  111. static void arm_dma_sync_single_for_device(struct device *dev,
  112. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  113. {
  114. unsigned int offset = handle & (PAGE_SIZE - 1);
  115. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  116. __dma_page_cpu_to_dev(page, offset, size, dir);
  117. }
  118. struct dma_map_ops arm_dma_ops = {
  119. .alloc = arm_dma_alloc,
  120. .free = arm_dma_free,
  121. .mmap = arm_dma_mmap,
  122. .get_sgtable = arm_dma_get_sgtable,
  123. .map_page = arm_dma_map_page,
  124. .unmap_page = arm_dma_unmap_page,
  125. .map_sg = arm_dma_map_sg,
  126. .unmap_sg = arm_dma_unmap_sg,
  127. .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
  128. .sync_single_for_device = arm_dma_sync_single_for_device,
  129. .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
  130. .sync_sg_for_device = arm_dma_sync_sg_for_device,
  131. .set_dma_mask = arm_dma_set_mask,
  132. };
  133. EXPORT_SYMBOL(arm_dma_ops);
  134. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  135. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
  136. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  137. dma_addr_t handle, struct dma_attrs *attrs);
  138. struct dma_map_ops arm_coherent_dma_ops = {
  139. .alloc = arm_coherent_dma_alloc,
  140. .free = arm_coherent_dma_free,
  141. .mmap = arm_dma_mmap,
  142. .get_sgtable = arm_dma_get_sgtable,
  143. .map_page = arm_coherent_dma_map_page,
  144. .map_sg = arm_dma_map_sg,
  145. .set_dma_mask = arm_dma_set_mask,
  146. };
  147. EXPORT_SYMBOL(arm_coherent_dma_ops);
  148. static u64 get_coherent_dma_mask(struct device *dev)
  149. {
  150. u64 mask = (u64)arm_dma_limit;
  151. if (dev) {
  152. mask = dev->coherent_dma_mask;
  153. /*
  154. * Sanity check the DMA mask - it must be non-zero, and
  155. * must be able to be satisfied by a DMA allocation.
  156. */
  157. if (mask == 0) {
  158. dev_warn(dev, "coherent DMA mask is unset\n");
  159. return 0;
  160. }
  161. if ((~mask) & (u64)arm_dma_limit) {
  162. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  163. "than system GFP_DMA mask %#llx\n",
  164. mask, (u64)arm_dma_limit);
  165. return 0;
  166. }
  167. }
  168. return mask;
  169. }
  170. static void __dma_clear_buffer(struct page *page, size_t size)
  171. {
  172. /*
  173. * Ensure that the allocated pages are zeroed, and that any data
  174. * lurking in the kernel direct-mapped region is invalidated.
  175. */
  176. if (PageHighMem(page)) {
  177. phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
  178. phys_addr_t end = base + size;
  179. while (size > 0) {
  180. void *ptr = kmap_atomic(page);
  181. memset(ptr, 0, PAGE_SIZE);
  182. dmac_flush_range(ptr, ptr + PAGE_SIZE);
  183. kunmap_atomic(ptr);
  184. page++;
  185. size -= PAGE_SIZE;
  186. }
  187. outer_flush_range(base, end);
  188. } else {
  189. void *ptr = page_address(page);
  190. memset(ptr, 0, size);
  191. dmac_flush_range(ptr, ptr + size);
  192. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  193. }
  194. }
  195. /*
  196. * Allocate a DMA buffer for 'dev' of size 'size' using the
  197. * specified gfp mask. Note that 'size' must be page aligned.
  198. */
  199. static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  200. {
  201. unsigned long order = get_order(size);
  202. struct page *page, *p, *e;
  203. page = alloc_pages(gfp, order);
  204. if (!page)
  205. return NULL;
  206. /*
  207. * Now split the huge page and free the excess pages
  208. */
  209. split_page(page, order);
  210. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  211. __free_page(p);
  212. __dma_clear_buffer(page, size);
  213. return page;
  214. }
  215. /*
  216. * Free a DMA buffer. 'size' must be page aligned.
  217. */
  218. static void __dma_free_buffer(struct page *page, size_t size)
  219. {
  220. struct page *e = page + (size >> PAGE_SHIFT);
  221. while (page < e) {
  222. __free_page(page);
  223. page++;
  224. }
  225. }
  226. #ifdef CONFIG_MMU
  227. #ifdef CONFIG_HUGETLB_PAGE
  228. #warning ARM Coherent DMA allocator does not (yet) support huge TLB
  229. #endif
  230. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  231. pgprot_t prot, struct page **ret_page,
  232. const void *caller);
  233. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  234. pgprot_t prot, struct page **ret_page,
  235. const void *caller);
  236. static void *
  237. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
  238. const void *caller)
  239. {
  240. struct vm_struct *area;
  241. unsigned long addr;
  242. /*
  243. * DMA allocation can be mapped to user space, so lets
  244. * set VM_USERMAP flags too.
  245. */
  246. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  247. caller);
  248. if (!area)
  249. return NULL;
  250. addr = (unsigned long)area->addr;
  251. area->phys_addr = __pfn_to_phys(page_to_pfn(page));
  252. if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) {
  253. vunmap((void *)addr);
  254. return NULL;
  255. }
  256. return (void *)addr;
  257. }
  258. static void __dma_free_remap(void *cpu_addr, size_t size)
  259. {
  260. unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
  261. struct vm_struct *area = find_vm_area(cpu_addr);
  262. if (!area || (area->flags & flags) != flags) {
  263. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  264. return;
  265. }
  266. unmap_kernel_range((unsigned long)cpu_addr, size);
  267. vunmap(cpu_addr);
  268. }
  269. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  270. struct dma_pool {
  271. size_t size;
  272. spinlock_t lock;
  273. unsigned long *bitmap;
  274. unsigned long nr_pages;
  275. void *vaddr;
  276. struct page **pages;
  277. };
  278. static struct dma_pool atomic_pool = {
  279. .size = DEFAULT_DMA_COHERENT_POOL_SIZE,
  280. };
  281. static int __init early_coherent_pool(char *p)
  282. {
  283. atomic_pool.size = memparse(p, &p);
  284. return 0;
  285. }
  286. early_param("coherent_pool", early_coherent_pool);
  287. void __init init_dma_coherent_pool_size(unsigned long size)
  288. {
  289. /*
  290. * Catch any attempt to set the pool size too late.
  291. */
  292. BUG_ON(atomic_pool.vaddr);
  293. /*
  294. * Set architecture specific coherent pool size only if
  295. * it has not been changed by kernel command line parameter.
  296. */
  297. if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
  298. atomic_pool.size = size;
  299. }
  300. /*
  301. * Initialise the coherent pool for atomic allocations.
  302. */
  303. static int __init atomic_pool_init(void)
  304. {
  305. struct dma_pool *pool = &atomic_pool;
  306. pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
  307. gfp_t gfp = GFP_KERNEL | GFP_DMA;
  308. unsigned long nr_pages = pool->size >> PAGE_SHIFT;
  309. unsigned long *bitmap;
  310. struct page *page;
  311. struct page **pages;
  312. void *ptr;
  313. int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
  314. bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  315. if (!bitmap)
  316. goto no_bitmap;
  317. pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
  318. if (!pages)
  319. goto no_pages;
  320. if (IS_ENABLED(CONFIG_DMA_CMA))
  321. ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page,
  322. atomic_pool_init);
  323. else
  324. ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page,
  325. atomic_pool_init);
  326. if (ptr) {
  327. int i;
  328. for (i = 0; i < nr_pages; i++)
  329. pages[i] = page + i;
  330. spin_lock_init(&pool->lock);
  331. pool->vaddr = ptr;
  332. pool->pages = pages;
  333. pool->bitmap = bitmap;
  334. pool->nr_pages = nr_pages;
  335. pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
  336. (unsigned)pool->size / 1024);
  337. return 0;
  338. }
  339. kfree(pages);
  340. no_pages:
  341. kfree(bitmap);
  342. no_bitmap:
  343. pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
  344. (unsigned)pool->size / 1024);
  345. return -ENOMEM;
  346. }
  347. /*
  348. * CMA is activated by core_initcall, so we must be called after it.
  349. */
  350. postcore_initcall(atomic_pool_init);
  351. struct dma_contig_early_reserve {
  352. phys_addr_t base;
  353. unsigned long size;
  354. };
  355. static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
  356. static int dma_mmu_remap_num __initdata;
  357. void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  358. {
  359. dma_mmu_remap[dma_mmu_remap_num].base = base;
  360. dma_mmu_remap[dma_mmu_remap_num].size = size;
  361. dma_mmu_remap_num++;
  362. }
  363. void __init dma_contiguous_remap(void)
  364. {
  365. int i;
  366. for (i = 0; i < dma_mmu_remap_num; i++) {
  367. phys_addr_t start = dma_mmu_remap[i].base;
  368. phys_addr_t end = start + dma_mmu_remap[i].size;
  369. struct map_desc map;
  370. unsigned long addr;
  371. if (end > arm_lowmem_limit)
  372. end = arm_lowmem_limit;
  373. if (start >= end)
  374. continue;
  375. map.pfn = __phys_to_pfn(start);
  376. map.virtual = __phys_to_virt(start);
  377. map.length = end - start;
  378. map.type = MT_MEMORY_DMA_READY;
  379. /*
  380. * Clear previous low-memory mapping
  381. */
  382. for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
  383. addr += PMD_SIZE)
  384. pmd_clear(pmd_off_k(addr));
  385. iotable_init(&map, 1);
  386. }
  387. }
  388. static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
  389. void *data)
  390. {
  391. struct page *page = virt_to_page(addr);
  392. pgprot_t prot = *(pgprot_t *)data;
  393. set_pte_ext(pte, mk_pte(page, prot), 0);
  394. return 0;
  395. }
  396. static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
  397. {
  398. unsigned long start = (unsigned long) page_address(page);
  399. unsigned end = start + size;
  400. apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
  401. flush_tlb_kernel_range(start, end);
  402. }
  403. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  404. pgprot_t prot, struct page **ret_page,
  405. const void *caller)
  406. {
  407. struct page *page;
  408. void *ptr;
  409. page = __dma_alloc_buffer(dev, size, gfp);
  410. if (!page)
  411. return NULL;
  412. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  413. if (!ptr) {
  414. __dma_free_buffer(page, size);
  415. return NULL;
  416. }
  417. *ret_page = page;
  418. return ptr;
  419. }
  420. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  421. {
  422. struct dma_pool *pool = &atomic_pool;
  423. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  424. unsigned int pageno;
  425. unsigned long flags;
  426. void *ptr = NULL;
  427. unsigned long align_mask;
  428. if (!pool->vaddr) {
  429. WARN(1, "coherent pool not initialised!\n");
  430. return NULL;
  431. }
  432. /*
  433. * Align the region allocation - allocations from pool are rather
  434. * small, so align them to their order in pages, minimum is a page
  435. * size. This helps reduce fragmentation of the DMA space.
  436. */
  437. align_mask = (1 << get_order(size)) - 1;
  438. spin_lock_irqsave(&pool->lock, flags);
  439. pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
  440. 0, count, align_mask);
  441. if (pageno < pool->nr_pages) {
  442. bitmap_set(pool->bitmap, pageno, count);
  443. ptr = pool->vaddr + PAGE_SIZE * pageno;
  444. *ret_page = pool->pages[pageno];
  445. } else {
  446. pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
  447. "Please increase it with coherent_pool= kernel parameter!\n",
  448. (unsigned)pool->size / 1024);
  449. }
  450. spin_unlock_irqrestore(&pool->lock, flags);
  451. return ptr;
  452. }
  453. static bool __in_atomic_pool(void *start, size_t size)
  454. {
  455. struct dma_pool *pool = &atomic_pool;
  456. void *end = start + size;
  457. void *pool_start = pool->vaddr;
  458. void *pool_end = pool->vaddr + pool->size;
  459. if (start < pool_start || start >= pool_end)
  460. return false;
  461. if (end <= pool_end)
  462. return true;
  463. WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
  464. start, end - 1, pool_start, pool_end - 1);
  465. return false;
  466. }
  467. static int __free_from_pool(void *start, size_t size)
  468. {
  469. struct dma_pool *pool = &atomic_pool;
  470. unsigned long pageno, count;
  471. unsigned long flags;
  472. if (!__in_atomic_pool(start, size))
  473. return 0;
  474. pageno = (start - pool->vaddr) >> PAGE_SHIFT;
  475. count = size >> PAGE_SHIFT;
  476. spin_lock_irqsave(&pool->lock, flags);
  477. bitmap_clear(pool->bitmap, pageno, count);
  478. spin_unlock_irqrestore(&pool->lock, flags);
  479. return 1;
  480. }
  481. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  482. pgprot_t prot, struct page **ret_page,
  483. const void *caller)
  484. {
  485. unsigned long order = get_order(size);
  486. size_t count = size >> PAGE_SHIFT;
  487. struct page *page;
  488. void *ptr;
  489. page = dma_alloc_from_contiguous(dev, count, order);
  490. if (!page)
  491. return NULL;
  492. __dma_clear_buffer(page, size);
  493. if (PageHighMem(page)) {
  494. ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
  495. if (!ptr) {
  496. dma_release_from_contiguous(dev, page, count);
  497. return NULL;
  498. }
  499. } else {
  500. __dma_remap(page, size, prot);
  501. ptr = page_address(page);
  502. }
  503. *ret_page = page;
  504. return ptr;
  505. }
  506. static void __free_from_contiguous(struct device *dev, struct page *page,
  507. void *cpu_addr, size_t size)
  508. {
  509. if (PageHighMem(page))
  510. __dma_free_remap(cpu_addr, size);
  511. else
  512. __dma_remap(page, size, pgprot_kernel);
  513. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  514. }
  515. static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
  516. {
  517. prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
  518. pgprot_writecombine(prot) :
  519. pgprot_dmacoherent(prot);
  520. return prot;
  521. }
  522. #define nommu() 0
  523. #else /* !CONFIG_MMU */
  524. #define nommu() 1
  525. #define __get_dma_pgprot(attrs, prot) __pgprot(0)
  526. #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
  527. #define __alloc_from_pool(size, ret_page) NULL
  528. #define __alloc_from_contiguous(dev, size, prot, ret, c) NULL
  529. #define __free_from_pool(cpu_addr, size) 0
  530. #define __free_from_contiguous(dev, page, cpu_addr, size) do { } while (0)
  531. #define __dma_free_remap(cpu_addr, size) do { } while (0)
  532. #endif /* CONFIG_MMU */
  533. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  534. struct page **ret_page)
  535. {
  536. struct page *page;
  537. page = __dma_alloc_buffer(dev, size, gfp);
  538. if (!page)
  539. return NULL;
  540. *ret_page = page;
  541. return page_address(page);
  542. }
  543. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  544. gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
  545. {
  546. u64 mask = get_coherent_dma_mask(dev);
  547. struct page *page = NULL;
  548. void *addr;
  549. #ifdef CONFIG_DMA_API_DEBUG
  550. u64 limit = (mask + 1) & ~mask;
  551. if (limit && size >= limit) {
  552. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  553. size, mask);
  554. return NULL;
  555. }
  556. #endif
  557. if (!mask)
  558. return NULL;
  559. if (mask < 0xffffffffULL)
  560. gfp |= GFP_DMA;
  561. /*
  562. * Following is a work-around (a.k.a. hack) to prevent pages
  563. * with __GFP_COMP being passed to split_page() which cannot
  564. * handle them. The real problem is that this flag probably
  565. * should be 0 on ARM as it is not supported on this
  566. * platform; see CONFIG_HUGETLBFS.
  567. */
  568. gfp &= ~(__GFP_COMP);
  569. *handle = DMA_ERROR_CODE;
  570. size = PAGE_ALIGN(size);
  571. if (is_coherent || nommu())
  572. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  573. else if (!(gfp & __GFP_WAIT))
  574. addr = __alloc_from_pool(size, &page);
  575. else if (!IS_ENABLED(CONFIG_DMA_CMA))
  576. addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
  577. else
  578. addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
  579. if (addr)
  580. *handle = pfn_to_dma(dev, page_to_pfn(page));
  581. return addr;
  582. }
  583. /*
  584. * Allocate DMA-coherent memory space and return both the kernel remapped
  585. * virtual and bus address for that space.
  586. */
  587. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  588. gfp_t gfp, struct dma_attrs *attrs)
  589. {
  590. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  591. void *memory;
  592. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  593. return memory;
  594. return __dma_alloc(dev, size, handle, gfp, prot, false,
  595. __builtin_return_address(0));
  596. }
  597. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  598. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  599. {
  600. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  601. void *memory;
  602. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  603. return memory;
  604. return __dma_alloc(dev, size, handle, gfp, prot, true,
  605. __builtin_return_address(0));
  606. }
  607. /*
  608. * Create userspace mapping for the DMA-coherent memory.
  609. */
  610. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  611. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  612. struct dma_attrs *attrs)
  613. {
  614. int ret = -ENXIO;
  615. #ifdef CONFIG_MMU
  616. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  617. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  618. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  619. unsigned long off = vma->vm_pgoff;
  620. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  621. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  622. return ret;
  623. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  624. ret = remap_pfn_range(vma, vma->vm_start,
  625. pfn + off,
  626. vma->vm_end - vma->vm_start,
  627. vma->vm_page_prot);
  628. }
  629. #endif /* CONFIG_MMU */
  630. return ret;
  631. }
  632. /*
  633. * Free a buffer as defined by the above mapping.
  634. */
  635. static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  636. dma_addr_t handle, struct dma_attrs *attrs,
  637. bool is_coherent)
  638. {
  639. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  640. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  641. return;
  642. size = PAGE_ALIGN(size);
  643. if (is_coherent || nommu()) {
  644. __dma_free_buffer(page, size);
  645. } else if (__free_from_pool(cpu_addr, size)) {
  646. return;
  647. } else if (!IS_ENABLED(CONFIG_DMA_CMA)) {
  648. __dma_free_remap(cpu_addr, size);
  649. __dma_free_buffer(page, size);
  650. } else {
  651. /*
  652. * Non-atomic allocations cannot be freed with IRQs disabled
  653. */
  654. WARN_ON(irqs_disabled());
  655. __free_from_contiguous(dev, page, cpu_addr, size);
  656. }
  657. }
  658. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  659. dma_addr_t handle, struct dma_attrs *attrs)
  660. {
  661. __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
  662. }
  663. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  664. dma_addr_t handle, struct dma_attrs *attrs)
  665. {
  666. __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
  667. }
  668. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  669. void *cpu_addr, dma_addr_t handle, size_t size,
  670. struct dma_attrs *attrs)
  671. {
  672. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  673. int ret;
  674. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  675. if (unlikely(ret))
  676. return ret;
  677. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  678. return 0;
  679. }
  680. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  681. size_t size, enum dma_data_direction dir,
  682. void (*op)(const void *, size_t, int))
  683. {
  684. unsigned long pfn;
  685. size_t left = size;
  686. pfn = page_to_pfn(page) + offset / PAGE_SIZE;
  687. offset %= PAGE_SIZE;
  688. /*
  689. * A single sg entry may refer to multiple physically contiguous
  690. * pages. But we still need to process highmem pages individually.
  691. * If highmem is not configured then the bulk of this loop gets
  692. * optimized out.
  693. */
  694. do {
  695. size_t len = left;
  696. void *vaddr;
  697. page = pfn_to_page(pfn);
  698. if (PageHighMem(page)) {
  699. if (len + offset > PAGE_SIZE)
  700. len = PAGE_SIZE - offset;
  701. if (cache_is_vipt_nonaliasing()) {
  702. vaddr = kmap_atomic(page);
  703. op(vaddr + offset, len, dir);
  704. kunmap_atomic(vaddr);
  705. } else {
  706. vaddr = kmap_high_get(page);
  707. if (vaddr) {
  708. op(vaddr + offset, len, dir);
  709. kunmap_high(page);
  710. }
  711. }
  712. } else {
  713. vaddr = page_address(page) + offset;
  714. op(vaddr, len, dir);
  715. }
  716. offset = 0;
  717. pfn++;
  718. left -= len;
  719. } while (left);
  720. }
  721. /*
  722. * Make an area consistent for devices.
  723. * Note: Drivers should NOT use this function directly, as it will break
  724. * platforms with CONFIG_DMABOUNCE.
  725. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  726. */
  727. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  728. size_t size, enum dma_data_direction dir)
  729. {
  730. unsigned long paddr;
  731. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  732. paddr = page_to_phys(page) + off;
  733. if (dir == DMA_FROM_DEVICE) {
  734. outer_inv_range(paddr, paddr + size);
  735. } else {
  736. outer_clean_range(paddr, paddr + size);
  737. }
  738. /* FIXME: non-speculating: flush on bidirectional mappings? */
  739. }
  740. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  741. size_t size, enum dma_data_direction dir)
  742. {
  743. unsigned long paddr = page_to_phys(page) + off;
  744. /* FIXME: non-speculating: not required */
  745. /* don't bother invalidating if DMA to device */
  746. if (dir != DMA_TO_DEVICE)
  747. outer_inv_range(paddr, paddr + size);
  748. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  749. /*
  750. * Mark the D-cache clean for these pages to avoid extra flushing.
  751. */
  752. if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
  753. unsigned long pfn;
  754. size_t left = size;
  755. pfn = page_to_pfn(page) + off / PAGE_SIZE;
  756. off %= PAGE_SIZE;
  757. if (off) {
  758. pfn++;
  759. left -= PAGE_SIZE - off;
  760. }
  761. while (left >= PAGE_SIZE) {
  762. page = pfn_to_page(pfn++);
  763. set_bit(PG_dcache_clean, &page->flags);
  764. left -= PAGE_SIZE;
  765. }
  766. }
  767. }
  768. /**
  769. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  770. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  771. * @sg: list of buffers
  772. * @nents: number of buffers to map
  773. * @dir: DMA transfer direction
  774. *
  775. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  776. * This is the scatter-gather version of the dma_map_single interface.
  777. * Here the scatter gather list elements are each tagged with the
  778. * appropriate dma address and length. They are obtained via
  779. * sg_dma_{address,length}.
  780. *
  781. * Device ownership issues as mentioned for dma_map_single are the same
  782. * here.
  783. */
  784. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  785. enum dma_data_direction dir, struct dma_attrs *attrs)
  786. {
  787. struct dma_map_ops *ops = get_dma_ops(dev);
  788. struct scatterlist *s;
  789. int i, j;
  790. for_each_sg(sg, s, nents, i) {
  791. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  792. s->dma_length = s->length;
  793. #endif
  794. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  795. s->length, dir, attrs);
  796. if (dma_mapping_error(dev, s->dma_address))
  797. goto bad_mapping;
  798. }
  799. return nents;
  800. bad_mapping:
  801. for_each_sg(sg, s, i, j)
  802. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  803. return 0;
  804. }
  805. /**
  806. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  807. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  808. * @sg: list of buffers
  809. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  810. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  811. *
  812. * Unmap a set of streaming mode DMA translations. Again, CPU access
  813. * rules concerning calls here are the same as for dma_unmap_single().
  814. */
  815. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  816. enum dma_data_direction dir, struct dma_attrs *attrs)
  817. {
  818. struct dma_map_ops *ops = get_dma_ops(dev);
  819. struct scatterlist *s;
  820. int i;
  821. for_each_sg(sg, s, nents, i)
  822. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  823. }
  824. /**
  825. * arm_dma_sync_sg_for_cpu
  826. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  827. * @sg: list of buffers
  828. * @nents: number of buffers to map (returned from dma_map_sg)
  829. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  830. */
  831. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  832. int nents, enum dma_data_direction dir)
  833. {
  834. struct dma_map_ops *ops = get_dma_ops(dev);
  835. struct scatterlist *s;
  836. int i;
  837. for_each_sg(sg, s, nents, i)
  838. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  839. dir);
  840. }
  841. /**
  842. * arm_dma_sync_sg_for_device
  843. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  844. * @sg: list of buffers
  845. * @nents: number of buffers to map (returned from dma_map_sg)
  846. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  847. */
  848. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  849. int nents, enum dma_data_direction dir)
  850. {
  851. struct dma_map_ops *ops = get_dma_ops(dev);
  852. struct scatterlist *s;
  853. int i;
  854. for_each_sg(sg, s, nents, i)
  855. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  856. dir);
  857. }
  858. /*
  859. * Return whether the given device DMA address mask can be supported
  860. * properly. For example, if your device can only drive the low 24-bits
  861. * during bus mastering, then you would pass 0x00ffffff as the mask
  862. * to this function.
  863. */
  864. int dma_supported(struct device *dev, u64 mask)
  865. {
  866. if (mask < (u64)arm_dma_limit)
  867. return 0;
  868. return 1;
  869. }
  870. EXPORT_SYMBOL(dma_supported);
  871. int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  872. {
  873. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  874. return -EIO;
  875. *dev->dma_mask = dma_mask;
  876. return 0;
  877. }
  878. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  879. static int __init dma_debug_do_init(void)
  880. {
  881. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  882. return 0;
  883. }
  884. fs_initcall(dma_debug_do_init);
  885. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  886. /* IOMMU */
  887. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  888. size_t size)
  889. {
  890. unsigned int order = get_order(size);
  891. unsigned int align = 0;
  892. unsigned int count, start;
  893. unsigned long flags;
  894. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  895. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  896. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  897. (1 << mapping->order) - 1) >> mapping->order;
  898. if (order > mapping->order)
  899. align = (1 << (order - mapping->order)) - 1;
  900. spin_lock_irqsave(&mapping->lock, flags);
  901. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  902. count, align);
  903. if (start > mapping->bits) {
  904. spin_unlock_irqrestore(&mapping->lock, flags);
  905. return DMA_ERROR_CODE;
  906. }
  907. bitmap_set(mapping->bitmap, start, count);
  908. spin_unlock_irqrestore(&mapping->lock, flags);
  909. return mapping->base + (start << (mapping->order + PAGE_SHIFT));
  910. }
  911. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  912. dma_addr_t addr, size_t size)
  913. {
  914. unsigned int start = (addr - mapping->base) >>
  915. (mapping->order + PAGE_SHIFT);
  916. unsigned int count = ((size >> PAGE_SHIFT) +
  917. (1 << mapping->order) - 1) >> mapping->order;
  918. unsigned long flags;
  919. spin_lock_irqsave(&mapping->lock, flags);
  920. bitmap_clear(mapping->bitmap, start, count);
  921. spin_unlock_irqrestore(&mapping->lock, flags);
  922. }
  923. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  924. gfp_t gfp, struct dma_attrs *attrs)
  925. {
  926. struct page **pages;
  927. int count = size >> PAGE_SHIFT;
  928. int array_size = count * sizeof(struct page *);
  929. int i = 0;
  930. if (array_size <= PAGE_SIZE)
  931. pages = kzalloc(array_size, gfp);
  932. else
  933. pages = vzalloc(array_size);
  934. if (!pages)
  935. return NULL;
  936. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
  937. {
  938. unsigned long order = get_order(size);
  939. struct page *page;
  940. page = dma_alloc_from_contiguous(dev, count, order);
  941. if (!page)
  942. goto error;
  943. __dma_clear_buffer(page, size);
  944. for (i = 0; i < count; i++)
  945. pages[i] = page + i;
  946. return pages;
  947. }
  948. /*
  949. * IOMMU can map any pages, so himem can also be used here
  950. */
  951. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  952. while (count) {
  953. int j, order = __fls(count);
  954. pages[i] = alloc_pages(gfp, order);
  955. while (!pages[i] && order)
  956. pages[i] = alloc_pages(gfp, --order);
  957. if (!pages[i])
  958. goto error;
  959. if (order) {
  960. split_page(pages[i], order);
  961. j = 1 << order;
  962. while (--j)
  963. pages[i + j] = pages[i] + j;
  964. }
  965. __dma_clear_buffer(pages[i], PAGE_SIZE << order);
  966. i += 1 << order;
  967. count -= 1 << order;
  968. }
  969. return pages;
  970. error:
  971. while (i--)
  972. if (pages[i])
  973. __free_pages(pages[i], 0);
  974. if (array_size <= PAGE_SIZE)
  975. kfree(pages);
  976. else
  977. vfree(pages);
  978. return NULL;
  979. }
  980. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  981. size_t size, struct dma_attrs *attrs)
  982. {
  983. int count = size >> PAGE_SHIFT;
  984. int array_size = count * sizeof(struct page *);
  985. int i;
  986. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
  987. dma_release_from_contiguous(dev, pages[0], count);
  988. } else {
  989. for (i = 0; i < count; i++)
  990. if (pages[i])
  991. __free_pages(pages[i], 0);
  992. }
  993. if (array_size <= PAGE_SIZE)
  994. kfree(pages);
  995. else
  996. vfree(pages);
  997. return 0;
  998. }
  999. /*
  1000. * Create a CPU mapping for a specified pages
  1001. */
  1002. static void *
  1003. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1004. const void *caller)
  1005. {
  1006. unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1007. struct vm_struct *area;
  1008. unsigned long p;
  1009. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  1010. caller);
  1011. if (!area)
  1012. return NULL;
  1013. area->pages = pages;
  1014. area->nr_pages = nr_pages;
  1015. p = (unsigned long)area->addr;
  1016. for (i = 0; i < nr_pages; i++) {
  1017. phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
  1018. if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
  1019. goto err;
  1020. p += PAGE_SIZE;
  1021. }
  1022. return area->addr;
  1023. err:
  1024. unmap_kernel_range((unsigned long)area->addr, size);
  1025. vunmap(area->addr);
  1026. return NULL;
  1027. }
  1028. /*
  1029. * Create a mapping in device IO address space for specified pages
  1030. */
  1031. static dma_addr_t
  1032. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  1033. {
  1034. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1035. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1036. dma_addr_t dma_addr, iova;
  1037. int i, ret = DMA_ERROR_CODE;
  1038. dma_addr = __alloc_iova(mapping, size);
  1039. if (dma_addr == DMA_ERROR_CODE)
  1040. return dma_addr;
  1041. iova = dma_addr;
  1042. for (i = 0; i < count; ) {
  1043. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1044. phys_addr_t phys = page_to_phys(pages[i]);
  1045. unsigned int len, j;
  1046. for (j = i + 1; j < count; j++, next_pfn++)
  1047. if (page_to_pfn(pages[j]) != next_pfn)
  1048. break;
  1049. len = (j - i) << PAGE_SHIFT;
  1050. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1051. if (ret < 0)
  1052. goto fail;
  1053. iova += len;
  1054. i = j;
  1055. }
  1056. return dma_addr;
  1057. fail:
  1058. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1059. __free_iova(mapping, dma_addr, size);
  1060. return DMA_ERROR_CODE;
  1061. }
  1062. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1063. {
  1064. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1065. /*
  1066. * add optional in-page offset from iova to size and align
  1067. * result to page size
  1068. */
  1069. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1070. iova &= PAGE_MASK;
  1071. iommu_unmap(mapping->domain, iova, size);
  1072. __free_iova(mapping, iova, size);
  1073. return 0;
  1074. }
  1075. static struct page **__atomic_get_pages(void *addr)
  1076. {
  1077. struct dma_pool *pool = &atomic_pool;
  1078. struct page **pages = pool->pages;
  1079. int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
  1080. return pages + offs;
  1081. }
  1082. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  1083. {
  1084. struct vm_struct *area;
  1085. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1086. return __atomic_get_pages(cpu_addr);
  1087. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1088. return cpu_addr;
  1089. area = find_vm_area(cpu_addr);
  1090. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1091. return area->pages;
  1092. return NULL;
  1093. }
  1094. static void *__iommu_alloc_atomic(struct device *dev, size_t size,
  1095. dma_addr_t *handle)
  1096. {
  1097. struct page *page;
  1098. void *addr;
  1099. addr = __alloc_from_pool(size, &page);
  1100. if (!addr)
  1101. return NULL;
  1102. *handle = __iommu_create_mapping(dev, &page, size);
  1103. if (*handle == DMA_ERROR_CODE)
  1104. goto err_mapping;
  1105. return addr;
  1106. err_mapping:
  1107. __free_from_pool(addr, size);
  1108. return NULL;
  1109. }
  1110. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1111. dma_addr_t handle, size_t size)
  1112. {
  1113. __iommu_remove_mapping(dev, handle, size);
  1114. __free_from_pool(cpu_addr, size);
  1115. }
  1116. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1117. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  1118. {
  1119. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  1120. struct page **pages;
  1121. void *addr = NULL;
  1122. *handle = DMA_ERROR_CODE;
  1123. size = PAGE_ALIGN(size);
  1124. if (gfp & GFP_ATOMIC)
  1125. return __iommu_alloc_atomic(dev, size, handle);
  1126. /*
  1127. * Following is a work-around (a.k.a. hack) to prevent pages
  1128. * with __GFP_COMP being passed to split_page() which cannot
  1129. * handle them. The real problem is that this flag probably
  1130. * should be 0 on ARM as it is not supported on this
  1131. * platform; see CONFIG_HUGETLBFS.
  1132. */
  1133. gfp &= ~(__GFP_COMP);
  1134. pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
  1135. if (!pages)
  1136. return NULL;
  1137. *handle = __iommu_create_mapping(dev, pages, size);
  1138. if (*handle == DMA_ERROR_CODE)
  1139. goto err_buffer;
  1140. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1141. return pages;
  1142. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1143. __builtin_return_address(0));
  1144. if (!addr)
  1145. goto err_mapping;
  1146. return addr;
  1147. err_mapping:
  1148. __iommu_remove_mapping(dev, *handle, size);
  1149. err_buffer:
  1150. __iommu_free_buffer(dev, pages, size, attrs);
  1151. return NULL;
  1152. }
  1153. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1154. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1155. struct dma_attrs *attrs)
  1156. {
  1157. unsigned long uaddr = vma->vm_start;
  1158. unsigned long usize = vma->vm_end - vma->vm_start;
  1159. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1160. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1161. if (!pages)
  1162. return -ENXIO;
  1163. do {
  1164. int ret = vm_insert_page(vma, uaddr, *pages++);
  1165. if (ret) {
  1166. pr_err("Remapping memory failed: %d\n", ret);
  1167. return ret;
  1168. }
  1169. uaddr += PAGE_SIZE;
  1170. usize -= PAGE_SIZE;
  1171. } while (usize > 0);
  1172. return 0;
  1173. }
  1174. /*
  1175. * free a page as defined by the above mapping.
  1176. * Must not be called with IRQs disabled.
  1177. */
  1178. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1179. dma_addr_t handle, struct dma_attrs *attrs)
  1180. {
  1181. struct page **pages;
  1182. size = PAGE_ALIGN(size);
  1183. if (__in_atomic_pool(cpu_addr, size)) {
  1184. __iommu_free_atomic(dev, cpu_addr, handle, size);
  1185. return;
  1186. }
  1187. pages = __iommu_get_pages(cpu_addr, attrs);
  1188. if (!pages) {
  1189. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1190. return;
  1191. }
  1192. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1193. unmap_kernel_range((unsigned long)cpu_addr, size);
  1194. vunmap(cpu_addr);
  1195. }
  1196. __iommu_remove_mapping(dev, handle, size);
  1197. __iommu_free_buffer(dev, pages, size, attrs);
  1198. }
  1199. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1200. void *cpu_addr, dma_addr_t dma_addr,
  1201. size_t size, struct dma_attrs *attrs)
  1202. {
  1203. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1204. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1205. if (!pages)
  1206. return -ENXIO;
  1207. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1208. GFP_KERNEL);
  1209. }
  1210. /*
  1211. * Map a part of the scatter-gather list into contiguous io address space
  1212. */
  1213. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1214. size_t size, dma_addr_t *handle,
  1215. enum dma_data_direction dir, struct dma_attrs *attrs,
  1216. bool is_coherent)
  1217. {
  1218. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1219. dma_addr_t iova, iova_base;
  1220. int ret = 0;
  1221. unsigned int count;
  1222. struct scatterlist *s;
  1223. size = PAGE_ALIGN(size);
  1224. *handle = DMA_ERROR_CODE;
  1225. iova_base = iova = __alloc_iova(mapping, size);
  1226. if (iova == DMA_ERROR_CODE)
  1227. return -ENOMEM;
  1228. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1229. phys_addr_t phys = page_to_phys(sg_page(s));
  1230. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1231. if (!is_coherent &&
  1232. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1233. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1234. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1235. if (ret < 0)
  1236. goto fail;
  1237. count += len >> PAGE_SHIFT;
  1238. iova += len;
  1239. }
  1240. *handle = iova_base;
  1241. return 0;
  1242. fail:
  1243. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1244. __free_iova(mapping, iova_base, size);
  1245. return ret;
  1246. }
  1247. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1248. enum dma_data_direction dir, struct dma_attrs *attrs,
  1249. bool is_coherent)
  1250. {
  1251. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1252. int i, count = 0;
  1253. unsigned int offset = s->offset;
  1254. unsigned int size = s->offset + s->length;
  1255. unsigned int max = dma_get_max_seg_size(dev);
  1256. for (i = 1; i < nents; i++) {
  1257. s = sg_next(s);
  1258. s->dma_address = DMA_ERROR_CODE;
  1259. s->dma_length = 0;
  1260. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1261. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1262. dir, attrs, is_coherent) < 0)
  1263. goto bad_mapping;
  1264. dma->dma_address += offset;
  1265. dma->dma_length = size - offset;
  1266. size = offset = s->offset;
  1267. start = s;
  1268. dma = sg_next(dma);
  1269. count += 1;
  1270. }
  1271. size += s->length;
  1272. }
  1273. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1274. is_coherent) < 0)
  1275. goto bad_mapping;
  1276. dma->dma_address += offset;
  1277. dma->dma_length = size - offset;
  1278. return count+1;
  1279. bad_mapping:
  1280. for_each_sg(sg, s, count, i)
  1281. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1282. return 0;
  1283. }
  1284. /**
  1285. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1286. * @dev: valid struct device pointer
  1287. * @sg: list of buffers
  1288. * @nents: number of buffers to map
  1289. * @dir: DMA transfer direction
  1290. *
  1291. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1292. * mode for DMA. The scatter gather list elements are merged together (if
  1293. * possible) and tagged with the appropriate dma address and length. They are
  1294. * obtained via sg_dma_{address,length}.
  1295. */
  1296. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1297. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1298. {
  1299. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1300. }
  1301. /**
  1302. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1303. * @dev: valid struct device pointer
  1304. * @sg: list of buffers
  1305. * @nents: number of buffers to map
  1306. * @dir: DMA transfer direction
  1307. *
  1308. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1309. * The scatter gather list elements are merged together (if possible) and
  1310. * tagged with the appropriate dma address and length. They are obtained via
  1311. * sg_dma_{address,length}.
  1312. */
  1313. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1314. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1315. {
  1316. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1317. }
  1318. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1319. int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
  1320. bool is_coherent)
  1321. {
  1322. struct scatterlist *s;
  1323. int i;
  1324. for_each_sg(sg, s, nents, i) {
  1325. if (sg_dma_len(s))
  1326. __iommu_remove_mapping(dev, sg_dma_address(s),
  1327. sg_dma_len(s));
  1328. if (!is_coherent &&
  1329. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1330. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1331. s->length, dir);
  1332. }
  1333. }
  1334. /**
  1335. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1336. * @dev: valid struct device pointer
  1337. * @sg: list of buffers
  1338. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1339. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1340. *
  1341. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1342. * rules concerning calls here are the same as for dma_unmap_single().
  1343. */
  1344. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1345. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1346. {
  1347. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1348. }
  1349. /**
  1350. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1351. * @dev: valid struct device pointer
  1352. * @sg: list of buffers
  1353. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1354. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1355. *
  1356. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1357. * rules concerning calls here are the same as for dma_unmap_single().
  1358. */
  1359. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1360. enum dma_data_direction dir, struct dma_attrs *attrs)
  1361. {
  1362. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1363. }
  1364. /**
  1365. * arm_iommu_sync_sg_for_cpu
  1366. * @dev: valid struct device pointer
  1367. * @sg: list of buffers
  1368. * @nents: number of buffers to map (returned from dma_map_sg)
  1369. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1370. */
  1371. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1372. int nents, enum dma_data_direction dir)
  1373. {
  1374. struct scatterlist *s;
  1375. int i;
  1376. for_each_sg(sg, s, nents, i)
  1377. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1378. }
  1379. /**
  1380. * arm_iommu_sync_sg_for_device
  1381. * @dev: valid struct device pointer
  1382. * @sg: list of buffers
  1383. * @nents: number of buffers to map (returned from dma_map_sg)
  1384. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1385. */
  1386. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1387. int nents, enum dma_data_direction dir)
  1388. {
  1389. struct scatterlist *s;
  1390. int i;
  1391. for_each_sg(sg, s, nents, i)
  1392. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1393. }
  1394. /**
  1395. * arm_coherent_iommu_map_page
  1396. * @dev: valid struct device pointer
  1397. * @page: page that buffer resides in
  1398. * @offset: offset into page for start of buffer
  1399. * @size: size of buffer to map
  1400. * @dir: DMA transfer direction
  1401. *
  1402. * Coherent IOMMU aware version of arm_dma_map_page()
  1403. */
  1404. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1405. unsigned long offset, size_t size, enum dma_data_direction dir,
  1406. struct dma_attrs *attrs)
  1407. {
  1408. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1409. dma_addr_t dma_addr;
  1410. int ret, prot, len = PAGE_ALIGN(size + offset);
  1411. dma_addr = __alloc_iova(mapping, len);
  1412. if (dma_addr == DMA_ERROR_CODE)
  1413. return dma_addr;
  1414. switch (dir) {
  1415. case DMA_BIDIRECTIONAL:
  1416. prot = IOMMU_READ | IOMMU_WRITE;
  1417. break;
  1418. case DMA_TO_DEVICE:
  1419. prot = IOMMU_READ;
  1420. break;
  1421. case DMA_FROM_DEVICE:
  1422. prot = IOMMU_WRITE;
  1423. break;
  1424. default:
  1425. prot = 0;
  1426. }
  1427. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1428. if (ret < 0)
  1429. goto fail;
  1430. return dma_addr + offset;
  1431. fail:
  1432. __free_iova(mapping, dma_addr, len);
  1433. return DMA_ERROR_CODE;
  1434. }
  1435. /**
  1436. * arm_iommu_map_page
  1437. * @dev: valid struct device pointer
  1438. * @page: page that buffer resides in
  1439. * @offset: offset into page for start of buffer
  1440. * @size: size of buffer to map
  1441. * @dir: DMA transfer direction
  1442. *
  1443. * IOMMU aware version of arm_dma_map_page()
  1444. */
  1445. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1446. unsigned long offset, size_t size, enum dma_data_direction dir,
  1447. struct dma_attrs *attrs)
  1448. {
  1449. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1450. __dma_page_cpu_to_dev(page, offset, size, dir);
  1451. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1452. }
  1453. /**
  1454. * arm_coherent_iommu_unmap_page
  1455. * @dev: valid struct device pointer
  1456. * @handle: DMA address of buffer
  1457. * @size: size of buffer (same as passed to dma_map_page)
  1458. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1459. *
  1460. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1461. */
  1462. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1463. size_t size, enum dma_data_direction dir,
  1464. struct dma_attrs *attrs)
  1465. {
  1466. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1467. dma_addr_t iova = handle & PAGE_MASK;
  1468. int offset = handle & ~PAGE_MASK;
  1469. int len = PAGE_ALIGN(size + offset);
  1470. if (!iova)
  1471. return;
  1472. iommu_unmap(mapping->domain, iova, len);
  1473. __free_iova(mapping, iova, len);
  1474. }
  1475. /**
  1476. * arm_iommu_unmap_page
  1477. * @dev: valid struct device pointer
  1478. * @handle: DMA address of buffer
  1479. * @size: size of buffer (same as passed to dma_map_page)
  1480. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1481. *
  1482. * IOMMU aware version of arm_dma_unmap_page()
  1483. */
  1484. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1485. size_t size, enum dma_data_direction dir,
  1486. struct dma_attrs *attrs)
  1487. {
  1488. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1489. dma_addr_t iova = handle & PAGE_MASK;
  1490. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1491. int offset = handle & ~PAGE_MASK;
  1492. int len = PAGE_ALIGN(size + offset);
  1493. if (!iova)
  1494. return;
  1495. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1496. __dma_page_dev_to_cpu(page, offset, size, dir);
  1497. iommu_unmap(mapping->domain, iova, len);
  1498. __free_iova(mapping, iova, len);
  1499. }
  1500. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1501. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1502. {
  1503. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1504. dma_addr_t iova = handle & PAGE_MASK;
  1505. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1506. unsigned int offset = handle & ~PAGE_MASK;
  1507. if (!iova)
  1508. return;
  1509. __dma_page_dev_to_cpu(page, offset, size, dir);
  1510. }
  1511. static void arm_iommu_sync_single_for_device(struct device *dev,
  1512. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1513. {
  1514. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1515. dma_addr_t iova = handle & PAGE_MASK;
  1516. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1517. unsigned int offset = handle & ~PAGE_MASK;
  1518. if (!iova)
  1519. return;
  1520. __dma_page_cpu_to_dev(page, offset, size, dir);
  1521. }
  1522. struct dma_map_ops iommu_ops = {
  1523. .alloc = arm_iommu_alloc_attrs,
  1524. .free = arm_iommu_free_attrs,
  1525. .mmap = arm_iommu_mmap_attrs,
  1526. .get_sgtable = arm_iommu_get_sgtable,
  1527. .map_page = arm_iommu_map_page,
  1528. .unmap_page = arm_iommu_unmap_page,
  1529. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1530. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1531. .map_sg = arm_iommu_map_sg,
  1532. .unmap_sg = arm_iommu_unmap_sg,
  1533. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1534. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1535. .set_dma_mask = arm_dma_set_mask,
  1536. };
  1537. struct dma_map_ops iommu_coherent_ops = {
  1538. .alloc = arm_iommu_alloc_attrs,
  1539. .free = arm_iommu_free_attrs,
  1540. .mmap = arm_iommu_mmap_attrs,
  1541. .get_sgtable = arm_iommu_get_sgtable,
  1542. .map_page = arm_coherent_iommu_map_page,
  1543. .unmap_page = arm_coherent_iommu_unmap_page,
  1544. .map_sg = arm_coherent_iommu_map_sg,
  1545. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1546. .set_dma_mask = arm_dma_set_mask,
  1547. };
  1548. /**
  1549. * arm_iommu_create_mapping
  1550. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1551. * @base: start address of the valid IO address space
  1552. * @size: size of the valid IO address space
  1553. * @order: accuracy of the IO addresses allocations
  1554. *
  1555. * Creates a mapping structure which holds information about used/unused
  1556. * IO address ranges, which is required to perform memory allocation and
  1557. * mapping with IOMMU aware functions.
  1558. *
  1559. * The client device need to be attached to the mapping with
  1560. * arm_iommu_attach_device function.
  1561. */
  1562. struct dma_iommu_mapping *
  1563. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
  1564. int order)
  1565. {
  1566. unsigned int count = size >> (PAGE_SHIFT + order);
  1567. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  1568. struct dma_iommu_mapping *mapping;
  1569. int err = -ENOMEM;
  1570. if (!count)
  1571. return ERR_PTR(-EINVAL);
  1572. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1573. if (!mapping)
  1574. goto err;
  1575. mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1576. if (!mapping->bitmap)
  1577. goto err2;
  1578. mapping->base = base;
  1579. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1580. mapping->order = order;
  1581. spin_lock_init(&mapping->lock);
  1582. mapping->domain = iommu_domain_alloc(bus);
  1583. if (!mapping->domain)
  1584. goto err3;
  1585. kref_init(&mapping->kref);
  1586. return mapping;
  1587. err3:
  1588. kfree(mapping->bitmap);
  1589. err2:
  1590. kfree(mapping);
  1591. err:
  1592. return ERR_PTR(err);
  1593. }
  1594. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1595. static void release_iommu_mapping(struct kref *kref)
  1596. {
  1597. struct dma_iommu_mapping *mapping =
  1598. container_of(kref, struct dma_iommu_mapping, kref);
  1599. iommu_domain_free(mapping->domain);
  1600. kfree(mapping->bitmap);
  1601. kfree(mapping);
  1602. }
  1603. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1604. {
  1605. if (mapping)
  1606. kref_put(&mapping->kref, release_iommu_mapping);
  1607. }
  1608. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1609. /**
  1610. * arm_iommu_attach_device
  1611. * @dev: valid struct device pointer
  1612. * @mapping: io address space mapping structure (returned from
  1613. * arm_iommu_create_mapping)
  1614. *
  1615. * Attaches specified io address space mapping to the provided device,
  1616. * this replaces the dma operations (dma_map_ops pointer) with the
  1617. * IOMMU aware version. More than one client might be attached to
  1618. * the same io address space mapping.
  1619. */
  1620. int arm_iommu_attach_device(struct device *dev,
  1621. struct dma_iommu_mapping *mapping)
  1622. {
  1623. int err;
  1624. err = iommu_attach_device(mapping->domain, dev);
  1625. if (err)
  1626. return err;
  1627. kref_get(&mapping->kref);
  1628. dev->archdata.mapping = mapping;
  1629. set_dma_ops(dev, &iommu_ops);
  1630. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1631. return 0;
  1632. }
  1633. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1634. /**
  1635. * arm_iommu_detach_device
  1636. * @dev: valid struct device pointer
  1637. *
  1638. * Detaches the provided device from a previously attached map.
  1639. * This voids the dma operations (dma_map_ops pointer)
  1640. */
  1641. void arm_iommu_detach_device(struct device *dev)
  1642. {
  1643. struct dma_iommu_mapping *mapping;
  1644. mapping = to_dma_iommu_mapping(dev);
  1645. if (!mapping) {
  1646. dev_warn(dev, "Not attached\n");
  1647. return;
  1648. }
  1649. iommu_detach_device(mapping->domain, dev);
  1650. kref_put(&mapping->kref, release_iommu_mapping);
  1651. dev->archdata.mapping = NULL;
  1652. set_dma_ops(dev, NULL);
  1653. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1654. }
  1655. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1656. #endif