dma-mapping.c 52 KB

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