dma-mapping.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  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 - 1) {
  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, bool want_vaddr);
  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, bool want_vaddr);
  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, true);
  319. else
  320. ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
  321. &page, atomic_pool_init, true);
  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, bool want_vaddr)
  411. {
  412. struct page *page;
  413. void *ptr = NULL;
  414. page = __dma_alloc_buffer(dev, size, gfp);
  415. if (!page)
  416. return NULL;
  417. if (!want_vaddr)
  418. goto out;
  419. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  420. if (!ptr) {
  421. __dma_free_buffer(page, size);
  422. return NULL;
  423. }
  424. out:
  425. *ret_page = page;
  426. return ptr;
  427. }
  428. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  429. {
  430. unsigned long val;
  431. void *ptr = NULL;
  432. if (!atomic_pool) {
  433. WARN(1, "coherent pool not initialised!\n");
  434. return NULL;
  435. }
  436. val = gen_pool_alloc(atomic_pool, size);
  437. if (val) {
  438. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  439. *ret_page = phys_to_page(phys);
  440. ptr = (void *)val;
  441. }
  442. return ptr;
  443. }
  444. static bool __in_atomic_pool(void *start, size_t size)
  445. {
  446. return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
  447. }
  448. static int __free_from_pool(void *start, size_t size)
  449. {
  450. if (!__in_atomic_pool(start, size))
  451. return 0;
  452. gen_pool_free(atomic_pool, (unsigned long)start, size);
  453. return 1;
  454. }
  455. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  456. pgprot_t prot, struct page **ret_page,
  457. const void *caller, bool want_vaddr)
  458. {
  459. unsigned long order = get_order(size);
  460. size_t count = size >> PAGE_SHIFT;
  461. struct page *page;
  462. void *ptr = NULL;
  463. page = dma_alloc_from_contiguous(dev, count, order);
  464. if (!page)
  465. return NULL;
  466. __dma_clear_buffer(page, size);
  467. if (!want_vaddr)
  468. goto out;
  469. if (PageHighMem(page)) {
  470. ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
  471. if (!ptr) {
  472. dma_release_from_contiguous(dev, page, count);
  473. return NULL;
  474. }
  475. } else {
  476. __dma_remap(page, size, prot);
  477. ptr = page_address(page);
  478. }
  479. out:
  480. *ret_page = page;
  481. return ptr;
  482. }
  483. static void __free_from_contiguous(struct device *dev, struct page *page,
  484. void *cpu_addr, size_t size, bool want_vaddr)
  485. {
  486. if (want_vaddr) {
  487. if (PageHighMem(page))
  488. __dma_free_remap(cpu_addr, size);
  489. else
  490. __dma_remap(page, size, PAGE_KERNEL);
  491. }
  492. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  493. }
  494. static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
  495. {
  496. prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
  497. pgprot_writecombine(prot) :
  498. pgprot_dmacoherent(prot);
  499. return prot;
  500. }
  501. #define nommu() 0
  502. #else /* !CONFIG_MMU */
  503. #define nommu() 1
  504. #define __get_dma_pgprot(attrs, prot) __pgprot(0)
  505. #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
  506. #define __alloc_from_pool(size, ret_page) NULL
  507. #define __alloc_from_contiguous(dev, size, prot, ret, c, wv) NULL
  508. #define __free_from_pool(cpu_addr, size) 0
  509. #define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
  510. #define __dma_free_remap(cpu_addr, size) do { } while (0)
  511. #endif /* CONFIG_MMU */
  512. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  513. struct page **ret_page)
  514. {
  515. struct page *page;
  516. page = __dma_alloc_buffer(dev, size, gfp);
  517. if (!page)
  518. return NULL;
  519. *ret_page = page;
  520. return page_address(page);
  521. }
  522. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  523. gfp_t gfp, pgprot_t prot, bool is_coherent,
  524. struct dma_attrs *attrs, const void *caller)
  525. {
  526. u64 mask = get_coherent_dma_mask(dev);
  527. struct page *page = NULL;
  528. void *addr;
  529. bool want_vaddr;
  530. #ifdef CONFIG_DMA_API_DEBUG
  531. u64 limit = (mask + 1) & ~mask;
  532. if (limit && size >= limit) {
  533. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  534. size, mask);
  535. return NULL;
  536. }
  537. #endif
  538. if (!mask)
  539. return NULL;
  540. if (mask < 0xffffffffULL)
  541. gfp |= GFP_DMA;
  542. /*
  543. * Following is a work-around (a.k.a. hack) to prevent pages
  544. * with __GFP_COMP being passed to split_page() which cannot
  545. * handle them. The real problem is that this flag probably
  546. * should be 0 on ARM as it is not supported on this
  547. * platform; see CONFIG_HUGETLBFS.
  548. */
  549. gfp &= ~(__GFP_COMP);
  550. *handle = DMA_ERROR_CODE;
  551. size = PAGE_ALIGN(size);
  552. want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
  553. if (is_coherent || nommu())
  554. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  555. else if (!(gfp & __GFP_WAIT))
  556. addr = __alloc_from_pool(size, &page);
  557. else if (!dev_get_cma_area(dev))
  558. addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller, want_vaddr);
  559. else
  560. addr = __alloc_from_contiguous(dev, size, prot, &page, caller, want_vaddr);
  561. if (page)
  562. *handle = pfn_to_dma(dev, page_to_pfn(page));
  563. return want_vaddr ? addr : page;
  564. }
  565. /*
  566. * Allocate DMA-coherent memory space and return both the kernel remapped
  567. * virtual and bus address for that space.
  568. */
  569. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  570. gfp_t gfp, struct dma_attrs *attrs)
  571. {
  572. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  573. void *memory;
  574. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  575. return memory;
  576. return __dma_alloc(dev, size, handle, gfp, prot, false,
  577. attrs, __builtin_return_address(0));
  578. }
  579. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  580. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  581. {
  582. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  583. void *memory;
  584. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  585. return memory;
  586. return __dma_alloc(dev, size, handle, gfp, prot, true,
  587. attrs, __builtin_return_address(0));
  588. }
  589. /*
  590. * Create userspace mapping for the DMA-coherent memory.
  591. */
  592. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  593. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  594. struct dma_attrs *attrs)
  595. {
  596. int ret = -ENXIO;
  597. #ifdef CONFIG_MMU
  598. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  599. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  600. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  601. unsigned long off = vma->vm_pgoff;
  602. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  603. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  604. return ret;
  605. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  606. ret = remap_pfn_range(vma, vma->vm_start,
  607. pfn + off,
  608. vma->vm_end - vma->vm_start,
  609. vma->vm_page_prot);
  610. }
  611. #endif /* CONFIG_MMU */
  612. return ret;
  613. }
  614. /*
  615. * Free a buffer as defined by the above mapping.
  616. */
  617. static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  618. dma_addr_t handle, struct dma_attrs *attrs,
  619. bool is_coherent)
  620. {
  621. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  622. bool want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
  623. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  624. return;
  625. size = PAGE_ALIGN(size);
  626. if (is_coherent || nommu()) {
  627. __dma_free_buffer(page, size);
  628. } else if (__free_from_pool(cpu_addr, size)) {
  629. return;
  630. } else if (!dev_get_cma_area(dev)) {
  631. if (want_vaddr)
  632. __dma_free_remap(cpu_addr, size);
  633. __dma_free_buffer(page, size);
  634. } else {
  635. /*
  636. * Non-atomic allocations cannot be freed with IRQs disabled
  637. */
  638. WARN_ON(irqs_disabled());
  639. __free_from_contiguous(dev, page, cpu_addr, size, want_vaddr);
  640. }
  641. }
  642. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  643. dma_addr_t handle, struct dma_attrs *attrs)
  644. {
  645. __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
  646. }
  647. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  648. dma_addr_t handle, struct dma_attrs *attrs)
  649. {
  650. __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
  651. }
  652. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  653. void *cpu_addr, dma_addr_t handle, size_t size,
  654. struct dma_attrs *attrs)
  655. {
  656. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  657. int ret;
  658. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  659. if (unlikely(ret))
  660. return ret;
  661. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  662. return 0;
  663. }
  664. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  665. size_t size, enum dma_data_direction dir,
  666. void (*op)(const void *, size_t, int))
  667. {
  668. unsigned long pfn;
  669. size_t left = size;
  670. pfn = page_to_pfn(page) + offset / PAGE_SIZE;
  671. offset %= PAGE_SIZE;
  672. /*
  673. * A single sg entry may refer to multiple physically contiguous
  674. * pages. But we still need to process highmem pages individually.
  675. * If highmem is not configured then the bulk of this loop gets
  676. * optimized out.
  677. */
  678. do {
  679. size_t len = left;
  680. void *vaddr;
  681. page = pfn_to_page(pfn);
  682. if (PageHighMem(page)) {
  683. if (len + offset > PAGE_SIZE)
  684. len = PAGE_SIZE - offset;
  685. if (cache_is_vipt_nonaliasing()) {
  686. vaddr = kmap_atomic(page);
  687. op(vaddr + offset, len, dir);
  688. kunmap_atomic(vaddr);
  689. } else {
  690. vaddr = kmap_high_get(page);
  691. if (vaddr) {
  692. op(vaddr + offset, len, dir);
  693. kunmap_high(page);
  694. }
  695. }
  696. } else {
  697. vaddr = page_address(page) + offset;
  698. op(vaddr, len, dir);
  699. }
  700. offset = 0;
  701. pfn++;
  702. left -= len;
  703. } while (left);
  704. }
  705. /*
  706. * Make an area consistent for devices.
  707. * Note: Drivers should NOT use this function directly, as it will break
  708. * platforms with CONFIG_DMABOUNCE.
  709. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  710. */
  711. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  712. size_t size, enum dma_data_direction dir)
  713. {
  714. phys_addr_t paddr;
  715. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  716. paddr = page_to_phys(page) + off;
  717. if (dir == DMA_FROM_DEVICE) {
  718. outer_inv_range(paddr, paddr + size);
  719. } else {
  720. outer_clean_range(paddr, paddr + size);
  721. }
  722. /* FIXME: non-speculating: flush on bidirectional mappings? */
  723. }
  724. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  725. size_t size, enum dma_data_direction dir)
  726. {
  727. phys_addr_t paddr = page_to_phys(page) + off;
  728. /* FIXME: non-speculating: not required */
  729. /* in any case, don't bother invalidating if DMA to device */
  730. if (dir != DMA_TO_DEVICE) {
  731. outer_inv_range(paddr, paddr + size);
  732. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  733. }
  734. /*
  735. * Mark the D-cache clean for these pages to avoid extra flushing.
  736. */
  737. if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
  738. unsigned long pfn;
  739. size_t left = size;
  740. pfn = page_to_pfn(page) + off / PAGE_SIZE;
  741. off %= PAGE_SIZE;
  742. if (off) {
  743. pfn++;
  744. left -= PAGE_SIZE - off;
  745. }
  746. while (left >= PAGE_SIZE) {
  747. page = pfn_to_page(pfn++);
  748. set_bit(PG_dcache_clean, &page->flags);
  749. left -= PAGE_SIZE;
  750. }
  751. }
  752. }
  753. /**
  754. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  755. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  756. * @sg: list of buffers
  757. * @nents: number of buffers to map
  758. * @dir: DMA transfer direction
  759. *
  760. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  761. * This is the scatter-gather version of the dma_map_single interface.
  762. * Here the scatter gather list elements are each tagged with the
  763. * appropriate dma address and length. They are obtained via
  764. * sg_dma_{address,length}.
  765. *
  766. * Device ownership issues as mentioned for dma_map_single are the same
  767. * here.
  768. */
  769. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  770. enum dma_data_direction dir, struct dma_attrs *attrs)
  771. {
  772. struct dma_map_ops *ops = get_dma_ops(dev);
  773. struct scatterlist *s;
  774. int i, j;
  775. for_each_sg(sg, s, nents, i) {
  776. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  777. s->dma_length = s->length;
  778. #endif
  779. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  780. s->length, dir, attrs);
  781. if (dma_mapping_error(dev, s->dma_address))
  782. goto bad_mapping;
  783. }
  784. return nents;
  785. bad_mapping:
  786. for_each_sg(sg, s, i, j)
  787. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  788. return 0;
  789. }
  790. /**
  791. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  792. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  793. * @sg: list of buffers
  794. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  795. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  796. *
  797. * Unmap a set of streaming mode DMA translations. Again, CPU access
  798. * rules concerning calls here are the same as for dma_unmap_single().
  799. */
  800. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  801. enum dma_data_direction dir, struct dma_attrs *attrs)
  802. {
  803. struct dma_map_ops *ops = get_dma_ops(dev);
  804. struct scatterlist *s;
  805. int i;
  806. for_each_sg(sg, s, nents, i)
  807. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  808. }
  809. /**
  810. * arm_dma_sync_sg_for_cpu
  811. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  812. * @sg: list of buffers
  813. * @nents: number of buffers to map (returned from dma_map_sg)
  814. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  815. */
  816. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  817. int nents, enum dma_data_direction dir)
  818. {
  819. struct dma_map_ops *ops = get_dma_ops(dev);
  820. struct scatterlist *s;
  821. int i;
  822. for_each_sg(sg, s, nents, i)
  823. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  824. dir);
  825. }
  826. /**
  827. * arm_dma_sync_sg_for_device
  828. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  829. * @sg: list of buffers
  830. * @nents: number of buffers to map (returned from dma_map_sg)
  831. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  832. */
  833. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  834. int nents, enum dma_data_direction dir)
  835. {
  836. struct dma_map_ops *ops = get_dma_ops(dev);
  837. struct scatterlist *s;
  838. int i;
  839. for_each_sg(sg, s, nents, i)
  840. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  841. dir);
  842. }
  843. /*
  844. * Return whether the given device DMA address mask can be supported
  845. * properly. For example, if your device can only drive the low 24-bits
  846. * during bus mastering, then you would pass 0x00ffffff as the mask
  847. * to this function.
  848. */
  849. int dma_supported(struct device *dev, u64 mask)
  850. {
  851. return __dma_supported(dev, mask, false);
  852. }
  853. EXPORT_SYMBOL(dma_supported);
  854. int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  855. {
  856. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  857. return -EIO;
  858. *dev->dma_mask = dma_mask;
  859. return 0;
  860. }
  861. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  862. static int __init dma_debug_do_init(void)
  863. {
  864. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  865. return 0;
  866. }
  867. fs_initcall(dma_debug_do_init);
  868. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  869. /* IOMMU */
  870. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
  871. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  872. size_t size)
  873. {
  874. unsigned int order = get_order(size);
  875. unsigned int align = 0;
  876. unsigned int count, start;
  877. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  878. unsigned long flags;
  879. dma_addr_t iova;
  880. int i;
  881. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  882. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  883. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  884. align = (1 << order) - 1;
  885. spin_lock_irqsave(&mapping->lock, flags);
  886. for (i = 0; i < mapping->nr_bitmaps; i++) {
  887. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  888. mapping->bits, 0, count, align);
  889. if (start > mapping->bits)
  890. continue;
  891. bitmap_set(mapping->bitmaps[i], start, count);
  892. break;
  893. }
  894. /*
  895. * No unused range found. Try to extend the existing mapping
  896. * and perform a second attempt to reserve an IO virtual
  897. * address range of size bytes.
  898. */
  899. if (i == mapping->nr_bitmaps) {
  900. if (extend_iommu_mapping(mapping)) {
  901. spin_unlock_irqrestore(&mapping->lock, flags);
  902. return DMA_ERROR_CODE;
  903. }
  904. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  905. mapping->bits, 0, count, align);
  906. if (start > mapping->bits) {
  907. spin_unlock_irqrestore(&mapping->lock, flags);
  908. return DMA_ERROR_CODE;
  909. }
  910. bitmap_set(mapping->bitmaps[i], start, count);
  911. }
  912. spin_unlock_irqrestore(&mapping->lock, flags);
  913. iova = mapping->base + (mapping_size * i);
  914. iova += start << PAGE_SHIFT;
  915. return iova;
  916. }
  917. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  918. dma_addr_t addr, size_t size)
  919. {
  920. unsigned int start, count;
  921. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  922. unsigned long flags;
  923. dma_addr_t bitmap_base;
  924. u32 bitmap_index;
  925. if (!size)
  926. return;
  927. bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
  928. BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
  929. bitmap_base = mapping->base + mapping_size * bitmap_index;
  930. start = (addr - bitmap_base) >> PAGE_SHIFT;
  931. if (addr + size > bitmap_base + mapping_size) {
  932. /*
  933. * The address range to be freed reaches into the iova
  934. * range of the next bitmap. This should not happen as
  935. * we don't allow this in __alloc_iova (at the
  936. * moment).
  937. */
  938. BUG();
  939. } else
  940. count = size >> PAGE_SHIFT;
  941. spin_lock_irqsave(&mapping->lock, flags);
  942. bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
  943. spin_unlock_irqrestore(&mapping->lock, flags);
  944. }
  945. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  946. gfp_t gfp, struct dma_attrs *attrs)
  947. {
  948. struct page **pages;
  949. int count = size >> PAGE_SHIFT;
  950. int array_size = count * sizeof(struct page *);
  951. int i = 0;
  952. if (array_size <= PAGE_SIZE)
  953. pages = kzalloc(array_size, GFP_KERNEL);
  954. else
  955. pages = vzalloc(array_size);
  956. if (!pages)
  957. return NULL;
  958. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
  959. {
  960. unsigned long order = get_order(size);
  961. struct page *page;
  962. page = dma_alloc_from_contiguous(dev, count, order);
  963. if (!page)
  964. goto error;
  965. __dma_clear_buffer(page, size);
  966. for (i = 0; i < count; i++)
  967. pages[i] = page + i;
  968. return pages;
  969. }
  970. /*
  971. * IOMMU can map any pages, so himem can also be used here
  972. */
  973. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  974. while (count) {
  975. int j, order;
  976. for (order = __fls(count); order > 0; --order) {
  977. /*
  978. * We do not want OOM killer to be invoked as long
  979. * as we can fall back to single pages, so we force
  980. * __GFP_NORETRY for orders higher than zero.
  981. */
  982. pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
  983. if (pages[i])
  984. break;
  985. }
  986. if (!pages[i]) {
  987. /*
  988. * Fall back to single page allocation.
  989. * Might invoke OOM killer as last resort.
  990. */
  991. pages[i] = alloc_pages(gfp, 0);
  992. if (!pages[i])
  993. goto error;
  994. }
  995. if (order) {
  996. split_page(pages[i], order);
  997. j = 1 << order;
  998. while (--j)
  999. pages[i + j] = pages[i] + j;
  1000. }
  1001. __dma_clear_buffer(pages[i], PAGE_SIZE << order);
  1002. i += 1 << order;
  1003. count -= 1 << order;
  1004. }
  1005. return pages;
  1006. error:
  1007. while (i--)
  1008. if (pages[i])
  1009. __free_pages(pages[i], 0);
  1010. if (array_size <= PAGE_SIZE)
  1011. kfree(pages);
  1012. else
  1013. vfree(pages);
  1014. return NULL;
  1015. }
  1016. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1017. size_t size, struct dma_attrs *attrs)
  1018. {
  1019. int count = size >> PAGE_SHIFT;
  1020. int array_size = count * sizeof(struct page *);
  1021. int i;
  1022. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
  1023. dma_release_from_contiguous(dev, pages[0], count);
  1024. } else {
  1025. for (i = 0; i < count; i++)
  1026. if (pages[i])
  1027. __free_pages(pages[i], 0);
  1028. }
  1029. if (array_size <= PAGE_SIZE)
  1030. kfree(pages);
  1031. else
  1032. vfree(pages);
  1033. return 0;
  1034. }
  1035. /*
  1036. * Create a CPU mapping for a specified pages
  1037. */
  1038. static void *
  1039. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1040. const void *caller)
  1041. {
  1042. return dma_common_pages_remap(pages, size,
  1043. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1044. }
  1045. /*
  1046. * Create a mapping in device IO address space for specified pages
  1047. */
  1048. static dma_addr_t
  1049. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  1050. {
  1051. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1052. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1053. dma_addr_t dma_addr, iova;
  1054. int i, ret = DMA_ERROR_CODE;
  1055. dma_addr = __alloc_iova(mapping, size);
  1056. if (dma_addr == DMA_ERROR_CODE)
  1057. return dma_addr;
  1058. iova = dma_addr;
  1059. for (i = 0; i < count; ) {
  1060. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1061. phys_addr_t phys = page_to_phys(pages[i]);
  1062. unsigned int len, j;
  1063. for (j = i + 1; j < count; j++, next_pfn++)
  1064. if (page_to_pfn(pages[j]) != next_pfn)
  1065. break;
  1066. len = (j - i) << PAGE_SHIFT;
  1067. ret = iommu_map(mapping->domain, iova, phys, len,
  1068. IOMMU_READ|IOMMU_WRITE);
  1069. if (ret < 0)
  1070. goto fail;
  1071. iova += len;
  1072. i = j;
  1073. }
  1074. return dma_addr;
  1075. fail:
  1076. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1077. __free_iova(mapping, dma_addr, size);
  1078. return DMA_ERROR_CODE;
  1079. }
  1080. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1081. {
  1082. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1083. /*
  1084. * add optional in-page offset from iova to size and align
  1085. * result to page size
  1086. */
  1087. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1088. iova &= PAGE_MASK;
  1089. iommu_unmap(mapping->domain, iova, size);
  1090. __free_iova(mapping, iova, size);
  1091. return 0;
  1092. }
  1093. static struct page **__atomic_get_pages(void *addr)
  1094. {
  1095. struct page *page;
  1096. phys_addr_t phys;
  1097. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1098. page = phys_to_page(phys);
  1099. return (struct page **)page;
  1100. }
  1101. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  1102. {
  1103. struct vm_struct *area;
  1104. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1105. return __atomic_get_pages(cpu_addr);
  1106. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1107. return cpu_addr;
  1108. area = find_vm_area(cpu_addr);
  1109. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1110. return area->pages;
  1111. return NULL;
  1112. }
  1113. static void *__iommu_alloc_atomic(struct device *dev, size_t size,
  1114. dma_addr_t *handle)
  1115. {
  1116. struct page *page;
  1117. void *addr;
  1118. addr = __alloc_from_pool(size, &page);
  1119. if (!addr)
  1120. return NULL;
  1121. *handle = __iommu_create_mapping(dev, &page, size);
  1122. if (*handle == DMA_ERROR_CODE)
  1123. goto err_mapping;
  1124. return addr;
  1125. err_mapping:
  1126. __free_from_pool(addr, size);
  1127. return NULL;
  1128. }
  1129. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1130. dma_addr_t handle, size_t size)
  1131. {
  1132. __iommu_remove_mapping(dev, handle, size);
  1133. __free_from_pool(cpu_addr, size);
  1134. }
  1135. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1136. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  1137. {
  1138. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1139. struct page **pages;
  1140. void *addr = NULL;
  1141. *handle = DMA_ERROR_CODE;
  1142. size = PAGE_ALIGN(size);
  1143. if (!(gfp & __GFP_WAIT))
  1144. return __iommu_alloc_atomic(dev, size, handle);
  1145. /*
  1146. * Following is a work-around (a.k.a. hack) to prevent pages
  1147. * with __GFP_COMP being passed to split_page() which cannot
  1148. * handle them. The real problem is that this flag probably
  1149. * should be 0 on ARM as it is not supported on this
  1150. * platform; see CONFIG_HUGETLBFS.
  1151. */
  1152. gfp &= ~(__GFP_COMP);
  1153. pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
  1154. if (!pages)
  1155. return NULL;
  1156. *handle = __iommu_create_mapping(dev, pages, size);
  1157. if (*handle == DMA_ERROR_CODE)
  1158. goto err_buffer;
  1159. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1160. return pages;
  1161. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1162. __builtin_return_address(0));
  1163. if (!addr)
  1164. goto err_mapping;
  1165. return addr;
  1166. err_mapping:
  1167. __iommu_remove_mapping(dev, *handle, size);
  1168. err_buffer:
  1169. __iommu_free_buffer(dev, pages, size, attrs);
  1170. return NULL;
  1171. }
  1172. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1173. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1174. struct dma_attrs *attrs)
  1175. {
  1176. unsigned long uaddr = vma->vm_start;
  1177. unsigned long usize = vma->vm_end - vma->vm_start;
  1178. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1179. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1180. if (!pages)
  1181. return -ENXIO;
  1182. do {
  1183. int ret = vm_insert_page(vma, uaddr, *pages++);
  1184. if (ret) {
  1185. pr_err("Remapping memory failed: %d\n", ret);
  1186. return ret;
  1187. }
  1188. uaddr += PAGE_SIZE;
  1189. usize -= PAGE_SIZE;
  1190. } while (usize > 0);
  1191. return 0;
  1192. }
  1193. /*
  1194. * free a page as defined by the above mapping.
  1195. * Must not be called with IRQs disabled.
  1196. */
  1197. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1198. dma_addr_t handle, struct dma_attrs *attrs)
  1199. {
  1200. struct page **pages;
  1201. size = PAGE_ALIGN(size);
  1202. if (__in_atomic_pool(cpu_addr, size)) {
  1203. __iommu_free_atomic(dev, cpu_addr, handle, size);
  1204. return;
  1205. }
  1206. pages = __iommu_get_pages(cpu_addr, attrs);
  1207. if (!pages) {
  1208. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1209. return;
  1210. }
  1211. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1212. dma_common_free_remap(cpu_addr, size,
  1213. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1214. }
  1215. __iommu_remove_mapping(dev, handle, size);
  1216. __iommu_free_buffer(dev, pages, size, attrs);
  1217. }
  1218. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1219. void *cpu_addr, dma_addr_t dma_addr,
  1220. size_t size, struct dma_attrs *attrs)
  1221. {
  1222. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1223. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1224. if (!pages)
  1225. return -ENXIO;
  1226. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1227. GFP_KERNEL);
  1228. }
  1229. static int __dma_direction_to_prot(enum dma_data_direction dir)
  1230. {
  1231. int prot;
  1232. switch (dir) {
  1233. case DMA_BIDIRECTIONAL:
  1234. prot = IOMMU_READ | IOMMU_WRITE;
  1235. break;
  1236. case DMA_TO_DEVICE:
  1237. prot = IOMMU_READ;
  1238. break;
  1239. case DMA_FROM_DEVICE:
  1240. prot = IOMMU_WRITE;
  1241. break;
  1242. default:
  1243. prot = 0;
  1244. }
  1245. return prot;
  1246. }
  1247. /*
  1248. * Map a part of the scatter-gather list into contiguous io address space
  1249. */
  1250. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1251. size_t size, dma_addr_t *handle,
  1252. enum dma_data_direction dir, struct dma_attrs *attrs,
  1253. bool is_coherent)
  1254. {
  1255. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1256. dma_addr_t iova, iova_base;
  1257. int ret = 0;
  1258. unsigned int count;
  1259. struct scatterlist *s;
  1260. int prot;
  1261. size = PAGE_ALIGN(size);
  1262. *handle = DMA_ERROR_CODE;
  1263. iova_base = iova = __alloc_iova(mapping, size);
  1264. if (iova == DMA_ERROR_CODE)
  1265. return -ENOMEM;
  1266. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1267. phys_addr_t phys = page_to_phys(sg_page(s));
  1268. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1269. if (!is_coherent &&
  1270. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1271. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1272. prot = __dma_direction_to_prot(dir);
  1273. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1274. if (ret < 0)
  1275. goto fail;
  1276. count += len >> PAGE_SHIFT;
  1277. iova += len;
  1278. }
  1279. *handle = iova_base;
  1280. return 0;
  1281. fail:
  1282. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1283. __free_iova(mapping, iova_base, size);
  1284. return ret;
  1285. }
  1286. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1287. enum dma_data_direction dir, struct dma_attrs *attrs,
  1288. bool is_coherent)
  1289. {
  1290. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1291. int i, count = 0;
  1292. unsigned int offset = s->offset;
  1293. unsigned int size = s->offset + s->length;
  1294. unsigned int max = dma_get_max_seg_size(dev);
  1295. for (i = 1; i < nents; i++) {
  1296. s = sg_next(s);
  1297. s->dma_address = DMA_ERROR_CODE;
  1298. s->dma_length = 0;
  1299. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1300. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1301. dir, attrs, is_coherent) < 0)
  1302. goto bad_mapping;
  1303. dma->dma_address += offset;
  1304. dma->dma_length = size - offset;
  1305. size = offset = s->offset;
  1306. start = s;
  1307. dma = sg_next(dma);
  1308. count += 1;
  1309. }
  1310. size += s->length;
  1311. }
  1312. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1313. is_coherent) < 0)
  1314. goto bad_mapping;
  1315. dma->dma_address += offset;
  1316. dma->dma_length = size - offset;
  1317. return count+1;
  1318. bad_mapping:
  1319. for_each_sg(sg, s, count, i)
  1320. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1321. return 0;
  1322. }
  1323. /**
  1324. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1325. * @dev: valid struct device pointer
  1326. * @sg: list of buffers
  1327. * @nents: number of buffers to map
  1328. * @dir: DMA transfer direction
  1329. *
  1330. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1331. * mode for DMA. The scatter gather list elements are merged together (if
  1332. * possible) and tagged with the appropriate dma address and length. They are
  1333. * obtained via sg_dma_{address,length}.
  1334. */
  1335. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1336. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1337. {
  1338. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1339. }
  1340. /**
  1341. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1342. * @dev: valid struct device pointer
  1343. * @sg: list of buffers
  1344. * @nents: number of buffers to map
  1345. * @dir: DMA transfer direction
  1346. *
  1347. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1348. * The scatter gather list elements are merged together (if possible) and
  1349. * tagged with the appropriate dma address and length. They are obtained via
  1350. * sg_dma_{address,length}.
  1351. */
  1352. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1353. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1354. {
  1355. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1356. }
  1357. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1358. int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
  1359. bool is_coherent)
  1360. {
  1361. struct scatterlist *s;
  1362. int i;
  1363. for_each_sg(sg, s, nents, i) {
  1364. if (sg_dma_len(s))
  1365. __iommu_remove_mapping(dev, sg_dma_address(s),
  1366. sg_dma_len(s));
  1367. if (!is_coherent &&
  1368. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1369. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1370. s->length, dir);
  1371. }
  1372. }
  1373. /**
  1374. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1375. * @dev: valid struct device pointer
  1376. * @sg: list of buffers
  1377. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1378. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1379. *
  1380. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1381. * rules concerning calls here are the same as for dma_unmap_single().
  1382. */
  1383. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1384. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1385. {
  1386. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1387. }
  1388. /**
  1389. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1390. * @dev: valid struct device pointer
  1391. * @sg: list of buffers
  1392. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1393. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1394. *
  1395. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1396. * rules concerning calls here are the same as for dma_unmap_single().
  1397. */
  1398. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1399. enum dma_data_direction dir, struct dma_attrs *attrs)
  1400. {
  1401. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1402. }
  1403. /**
  1404. * arm_iommu_sync_sg_for_cpu
  1405. * @dev: valid struct device pointer
  1406. * @sg: list of buffers
  1407. * @nents: number of buffers to map (returned from dma_map_sg)
  1408. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1409. */
  1410. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1411. int nents, enum dma_data_direction dir)
  1412. {
  1413. struct scatterlist *s;
  1414. int i;
  1415. for_each_sg(sg, s, nents, i)
  1416. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1417. }
  1418. /**
  1419. * arm_iommu_sync_sg_for_device
  1420. * @dev: valid struct device pointer
  1421. * @sg: list of buffers
  1422. * @nents: number of buffers to map (returned from dma_map_sg)
  1423. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1424. */
  1425. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1426. int nents, enum dma_data_direction dir)
  1427. {
  1428. struct scatterlist *s;
  1429. int i;
  1430. for_each_sg(sg, s, nents, i)
  1431. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1432. }
  1433. /**
  1434. * arm_coherent_iommu_map_page
  1435. * @dev: valid struct device pointer
  1436. * @page: page that buffer resides in
  1437. * @offset: offset into page for start of buffer
  1438. * @size: size of buffer to map
  1439. * @dir: DMA transfer direction
  1440. *
  1441. * Coherent IOMMU aware version of arm_dma_map_page()
  1442. */
  1443. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1444. unsigned long offset, size_t size, enum dma_data_direction dir,
  1445. struct dma_attrs *attrs)
  1446. {
  1447. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1448. dma_addr_t dma_addr;
  1449. int ret, prot, len = PAGE_ALIGN(size + offset);
  1450. dma_addr = __alloc_iova(mapping, len);
  1451. if (dma_addr == DMA_ERROR_CODE)
  1452. return dma_addr;
  1453. prot = __dma_direction_to_prot(dir);
  1454. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1455. if (ret < 0)
  1456. goto fail;
  1457. return dma_addr + offset;
  1458. fail:
  1459. __free_iova(mapping, dma_addr, len);
  1460. return DMA_ERROR_CODE;
  1461. }
  1462. /**
  1463. * arm_iommu_map_page
  1464. * @dev: valid struct device pointer
  1465. * @page: page that buffer resides in
  1466. * @offset: offset into page for start of buffer
  1467. * @size: size of buffer to map
  1468. * @dir: DMA transfer direction
  1469. *
  1470. * IOMMU aware version of arm_dma_map_page()
  1471. */
  1472. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1473. unsigned long offset, size_t size, enum dma_data_direction dir,
  1474. struct dma_attrs *attrs)
  1475. {
  1476. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1477. __dma_page_cpu_to_dev(page, offset, size, dir);
  1478. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1479. }
  1480. /**
  1481. * arm_coherent_iommu_unmap_page
  1482. * @dev: valid struct device pointer
  1483. * @handle: DMA address of buffer
  1484. * @size: size of buffer (same as passed to dma_map_page)
  1485. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1486. *
  1487. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1488. */
  1489. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1490. size_t size, enum dma_data_direction dir,
  1491. struct dma_attrs *attrs)
  1492. {
  1493. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1494. dma_addr_t iova = handle & PAGE_MASK;
  1495. int offset = handle & ~PAGE_MASK;
  1496. int len = PAGE_ALIGN(size + offset);
  1497. if (!iova)
  1498. return;
  1499. iommu_unmap(mapping->domain, iova, len);
  1500. __free_iova(mapping, iova, len);
  1501. }
  1502. /**
  1503. * arm_iommu_unmap_page
  1504. * @dev: valid struct device pointer
  1505. * @handle: DMA address of buffer
  1506. * @size: size of buffer (same as passed to dma_map_page)
  1507. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1508. *
  1509. * IOMMU aware version of arm_dma_unmap_page()
  1510. */
  1511. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1512. size_t size, enum dma_data_direction dir,
  1513. struct dma_attrs *attrs)
  1514. {
  1515. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1516. dma_addr_t iova = handle & PAGE_MASK;
  1517. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1518. int offset = handle & ~PAGE_MASK;
  1519. int len = PAGE_ALIGN(size + offset);
  1520. if (!iova)
  1521. return;
  1522. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1523. __dma_page_dev_to_cpu(page, offset, size, dir);
  1524. iommu_unmap(mapping->domain, iova, len);
  1525. __free_iova(mapping, iova, len);
  1526. }
  1527. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1528. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1529. {
  1530. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1531. dma_addr_t iova = handle & PAGE_MASK;
  1532. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1533. unsigned int offset = handle & ~PAGE_MASK;
  1534. if (!iova)
  1535. return;
  1536. __dma_page_dev_to_cpu(page, offset, size, dir);
  1537. }
  1538. static void arm_iommu_sync_single_for_device(struct device *dev,
  1539. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1540. {
  1541. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1542. dma_addr_t iova = handle & PAGE_MASK;
  1543. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1544. unsigned int offset = handle & ~PAGE_MASK;
  1545. if (!iova)
  1546. return;
  1547. __dma_page_cpu_to_dev(page, offset, size, dir);
  1548. }
  1549. struct dma_map_ops iommu_ops = {
  1550. .alloc = arm_iommu_alloc_attrs,
  1551. .free = arm_iommu_free_attrs,
  1552. .mmap = arm_iommu_mmap_attrs,
  1553. .get_sgtable = arm_iommu_get_sgtable,
  1554. .map_page = arm_iommu_map_page,
  1555. .unmap_page = arm_iommu_unmap_page,
  1556. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1557. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1558. .map_sg = arm_iommu_map_sg,
  1559. .unmap_sg = arm_iommu_unmap_sg,
  1560. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1561. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1562. .set_dma_mask = arm_dma_set_mask,
  1563. };
  1564. struct dma_map_ops iommu_coherent_ops = {
  1565. .alloc = arm_iommu_alloc_attrs,
  1566. .free = arm_iommu_free_attrs,
  1567. .mmap = arm_iommu_mmap_attrs,
  1568. .get_sgtable = arm_iommu_get_sgtable,
  1569. .map_page = arm_coherent_iommu_map_page,
  1570. .unmap_page = arm_coherent_iommu_unmap_page,
  1571. .map_sg = arm_coherent_iommu_map_sg,
  1572. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1573. .set_dma_mask = arm_dma_set_mask,
  1574. };
  1575. /**
  1576. * arm_iommu_create_mapping
  1577. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1578. * @base: start address of the valid IO address space
  1579. * @size: maximum size of the valid IO address space
  1580. *
  1581. * Creates a mapping structure which holds information about used/unused
  1582. * IO address ranges, which is required to perform memory allocation and
  1583. * mapping with IOMMU aware functions.
  1584. *
  1585. * The client device need to be attached to the mapping with
  1586. * arm_iommu_attach_device function.
  1587. */
  1588. struct dma_iommu_mapping *
  1589. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1590. {
  1591. unsigned int bits = size >> PAGE_SHIFT;
  1592. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1593. struct dma_iommu_mapping *mapping;
  1594. int extensions = 1;
  1595. int err = -ENOMEM;
  1596. /* currently only 32-bit DMA address space is supported */
  1597. if (size > DMA_BIT_MASK(32) + 1)
  1598. return ERR_PTR(-ERANGE);
  1599. if (!bitmap_size)
  1600. return ERR_PTR(-EINVAL);
  1601. if (bitmap_size > PAGE_SIZE) {
  1602. extensions = bitmap_size / PAGE_SIZE;
  1603. bitmap_size = PAGE_SIZE;
  1604. }
  1605. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1606. if (!mapping)
  1607. goto err;
  1608. mapping->bitmap_size = bitmap_size;
  1609. mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
  1610. GFP_KERNEL);
  1611. if (!mapping->bitmaps)
  1612. goto err2;
  1613. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1614. if (!mapping->bitmaps[0])
  1615. goto err3;
  1616. mapping->nr_bitmaps = 1;
  1617. mapping->extensions = extensions;
  1618. mapping->base = base;
  1619. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1620. spin_lock_init(&mapping->lock);
  1621. mapping->domain = iommu_domain_alloc(bus);
  1622. if (!mapping->domain)
  1623. goto err4;
  1624. kref_init(&mapping->kref);
  1625. return mapping;
  1626. err4:
  1627. kfree(mapping->bitmaps[0]);
  1628. err3:
  1629. kfree(mapping->bitmaps);
  1630. err2:
  1631. kfree(mapping);
  1632. err:
  1633. return ERR_PTR(err);
  1634. }
  1635. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1636. static void release_iommu_mapping(struct kref *kref)
  1637. {
  1638. int i;
  1639. struct dma_iommu_mapping *mapping =
  1640. container_of(kref, struct dma_iommu_mapping, kref);
  1641. iommu_domain_free(mapping->domain);
  1642. for (i = 0; i < mapping->nr_bitmaps; i++)
  1643. kfree(mapping->bitmaps[i]);
  1644. kfree(mapping->bitmaps);
  1645. kfree(mapping);
  1646. }
  1647. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1648. {
  1649. int next_bitmap;
  1650. if (mapping->nr_bitmaps > mapping->extensions)
  1651. return -EINVAL;
  1652. next_bitmap = mapping->nr_bitmaps;
  1653. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1654. GFP_ATOMIC);
  1655. if (!mapping->bitmaps[next_bitmap])
  1656. return -ENOMEM;
  1657. mapping->nr_bitmaps++;
  1658. return 0;
  1659. }
  1660. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1661. {
  1662. if (mapping)
  1663. kref_put(&mapping->kref, release_iommu_mapping);
  1664. }
  1665. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1666. static int __arm_iommu_attach_device(struct device *dev,
  1667. struct dma_iommu_mapping *mapping)
  1668. {
  1669. int err;
  1670. err = iommu_attach_device(mapping->domain, dev);
  1671. if (err)
  1672. return err;
  1673. kref_get(&mapping->kref);
  1674. to_dma_iommu_mapping(dev) = mapping;
  1675. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1676. return 0;
  1677. }
  1678. /**
  1679. * arm_iommu_attach_device
  1680. * @dev: valid struct device pointer
  1681. * @mapping: io address space mapping structure (returned from
  1682. * arm_iommu_create_mapping)
  1683. *
  1684. * Attaches specified io address space mapping to the provided device.
  1685. * This replaces the dma operations (dma_map_ops pointer) with the
  1686. * IOMMU aware version.
  1687. *
  1688. * More than one client might be attached to the same io address space
  1689. * mapping.
  1690. */
  1691. int arm_iommu_attach_device(struct device *dev,
  1692. struct dma_iommu_mapping *mapping)
  1693. {
  1694. int err;
  1695. err = __arm_iommu_attach_device(dev, mapping);
  1696. if (err)
  1697. return err;
  1698. set_dma_ops(dev, &iommu_ops);
  1699. return 0;
  1700. }
  1701. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1702. static void __arm_iommu_detach_device(struct device *dev)
  1703. {
  1704. struct dma_iommu_mapping *mapping;
  1705. mapping = to_dma_iommu_mapping(dev);
  1706. if (!mapping) {
  1707. dev_warn(dev, "Not attached\n");
  1708. return;
  1709. }
  1710. iommu_detach_device(mapping->domain, dev);
  1711. kref_put(&mapping->kref, release_iommu_mapping);
  1712. to_dma_iommu_mapping(dev) = NULL;
  1713. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1714. }
  1715. /**
  1716. * arm_iommu_detach_device
  1717. * @dev: valid struct device pointer
  1718. *
  1719. * Detaches the provided device from a previously attached map.
  1720. * This voids the dma operations (dma_map_ops pointer)
  1721. */
  1722. void arm_iommu_detach_device(struct device *dev)
  1723. {
  1724. __arm_iommu_detach_device(dev);
  1725. set_dma_ops(dev, NULL);
  1726. }
  1727. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1728. static struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1729. {
  1730. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1731. }
  1732. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1733. struct iommu_ops *iommu)
  1734. {
  1735. struct dma_iommu_mapping *mapping;
  1736. if (!iommu)
  1737. return false;
  1738. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1739. if (IS_ERR(mapping)) {
  1740. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1741. size, dev_name(dev));
  1742. return false;
  1743. }
  1744. if (__arm_iommu_attach_device(dev, mapping)) {
  1745. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  1746. dev_name(dev));
  1747. arm_iommu_release_mapping(mapping);
  1748. return false;
  1749. }
  1750. return true;
  1751. }
  1752. static void arm_teardown_iommu_dma_ops(struct device *dev)
  1753. {
  1754. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1755. if (!mapping)
  1756. return;
  1757. __arm_iommu_detach_device(dev);
  1758. arm_iommu_release_mapping(mapping);
  1759. }
  1760. #else
  1761. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1762. struct iommu_ops *iommu)
  1763. {
  1764. return false;
  1765. }
  1766. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  1767. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  1768. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  1769. static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  1770. {
  1771. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  1772. }
  1773. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1774. struct iommu_ops *iommu, bool coherent)
  1775. {
  1776. struct dma_map_ops *dma_ops;
  1777. dev->archdata.dma_coherent = coherent;
  1778. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  1779. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  1780. else
  1781. dma_ops = arm_get_dma_map_ops(coherent);
  1782. set_dma_ops(dev, dma_ops);
  1783. }
  1784. void arch_teardown_dma_ops(struct device *dev)
  1785. {
  1786. arm_teardown_iommu_dma_ops(dev);
  1787. }