dma-mapping.c 56 KB

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