dma-mapping.c 56 KB

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