dma-mapping.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  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_WAIT))
  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 (!(gfp & __GFP_WAIT))
  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. if (array_size <= PAGE_SIZE)
  1024. kfree(pages);
  1025. else
  1026. vfree(pages);
  1027. return NULL;
  1028. }
  1029. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1030. size_t size, struct dma_attrs *attrs)
  1031. {
  1032. int count = size >> PAGE_SHIFT;
  1033. int array_size = count * sizeof(struct page *);
  1034. int i;
  1035. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
  1036. dma_release_from_contiguous(dev, pages[0], count);
  1037. } else {
  1038. for (i = 0; i < count; i++)
  1039. if (pages[i])
  1040. __free_pages(pages[i], 0);
  1041. }
  1042. if (array_size <= PAGE_SIZE)
  1043. kfree(pages);
  1044. else
  1045. vfree(pages);
  1046. return 0;
  1047. }
  1048. /*
  1049. * Create a CPU mapping for a specified pages
  1050. */
  1051. static void *
  1052. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1053. const void *caller)
  1054. {
  1055. return dma_common_pages_remap(pages, size,
  1056. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1057. }
  1058. /*
  1059. * Create a mapping in device IO address space for specified pages
  1060. */
  1061. static dma_addr_t
  1062. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  1063. {
  1064. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1065. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1066. dma_addr_t dma_addr, iova;
  1067. int i, ret = DMA_ERROR_CODE;
  1068. dma_addr = __alloc_iova(mapping, size);
  1069. if (dma_addr == DMA_ERROR_CODE)
  1070. return dma_addr;
  1071. iova = dma_addr;
  1072. for (i = 0; i < count; ) {
  1073. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1074. phys_addr_t phys = page_to_phys(pages[i]);
  1075. unsigned int len, j;
  1076. for (j = i + 1; j < count; j++, next_pfn++)
  1077. if (page_to_pfn(pages[j]) != next_pfn)
  1078. break;
  1079. len = (j - i) << PAGE_SHIFT;
  1080. ret = iommu_map(mapping->domain, iova, phys, len,
  1081. IOMMU_READ|IOMMU_WRITE);
  1082. if (ret < 0)
  1083. goto fail;
  1084. iova += len;
  1085. i = j;
  1086. }
  1087. return dma_addr;
  1088. fail:
  1089. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1090. __free_iova(mapping, dma_addr, size);
  1091. return DMA_ERROR_CODE;
  1092. }
  1093. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1094. {
  1095. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1096. /*
  1097. * add optional in-page offset from iova to size and align
  1098. * result to page size
  1099. */
  1100. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1101. iova &= PAGE_MASK;
  1102. iommu_unmap(mapping->domain, iova, size);
  1103. __free_iova(mapping, iova, size);
  1104. return 0;
  1105. }
  1106. static struct page **__atomic_get_pages(void *addr)
  1107. {
  1108. struct page *page;
  1109. phys_addr_t phys;
  1110. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1111. page = phys_to_page(phys);
  1112. return (struct page **)page;
  1113. }
  1114. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  1115. {
  1116. struct vm_struct *area;
  1117. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1118. return __atomic_get_pages(cpu_addr);
  1119. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1120. return cpu_addr;
  1121. area = find_vm_area(cpu_addr);
  1122. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1123. return area->pages;
  1124. return NULL;
  1125. }
  1126. static void *__iommu_alloc_atomic(struct device *dev, size_t size,
  1127. dma_addr_t *handle)
  1128. {
  1129. struct page *page;
  1130. void *addr;
  1131. addr = __alloc_from_pool(size, &page);
  1132. if (!addr)
  1133. return NULL;
  1134. *handle = __iommu_create_mapping(dev, &page, size);
  1135. if (*handle == DMA_ERROR_CODE)
  1136. goto err_mapping;
  1137. return addr;
  1138. err_mapping:
  1139. __free_from_pool(addr, size);
  1140. return NULL;
  1141. }
  1142. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1143. dma_addr_t handle, size_t size)
  1144. {
  1145. __iommu_remove_mapping(dev, handle, size);
  1146. __free_from_pool(cpu_addr, size);
  1147. }
  1148. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1149. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  1150. {
  1151. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1152. struct page **pages;
  1153. void *addr = NULL;
  1154. *handle = DMA_ERROR_CODE;
  1155. size = PAGE_ALIGN(size);
  1156. if (!(gfp & __GFP_WAIT))
  1157. return __iommu_alloc_atomic(dev, size, handle);
  1158. /*
  1159. * Following is a work-around (a.k.a. hack) to prevent pages
  1160. * with __GFP_COMP being passed to split_page() which cannot
  1161. * handle them. The real problem is that this flag probably
  1162. * should be 0 on ARM as it is not supported on this
  1163. * platform; see CONFIG_HUGETLBFS.
  1164. */
  1165. gfp &= ~(__GFP_COMP);
  1166. pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
  1167. if (!pages)
  1168. return NULL;
  1169. *handle = __iommu_create_mapping(dev, pages, size);
  1170. if (*handle == DMA_ERROR_CODE)
  1171. goto err_buffer;
  1172. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1173. return pages;
  1174. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1175. __builtin_return_address(0));
  1176. if (!addr)
  1177. goto err_mapping;
  1178. return addr;
  1179. err_mapping:
  1180. __iommu_remove_mapping(dev, *handle, size);
  1181. err_buffer:
  1182. __iommu_free_buffer(dev, pages, size, attrs);
  1183. return NULL;
  1184. }
  1185. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1186. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1187. struct dma_attrs *attrs)
  1188. {
  1189. unsigned long uaddr = vma->vm_start;
  1190. unsigned long usize = vma->vm_end - vma->vm_start;
  1191. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1192. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1193. if (!pages)
  1194. return -ENXIO;
  1195. do {
  1196. int ret = vm_insert_page(vma, uaddr, *pages++);
  1197. if (ret) {
  1198. pr_err("Remapping memory failed: %d\n", ret);
  1199. return ret;
  1200. }
  1201. uaddr += PAGE_SIZE;
  1202. usize -= PAGE_SIZE;
  1203. } while (usize > 0);
  1204. return 0;
  1205. }
  1206. /*
  1207. * free a page as defined by the above mapping.
  1208. * Must not be called with IRQs disabled.
  1209. */
  1210. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1211. dma_addr_t handle, struct dma_attrs *attrs)
  1212. {
  1213. struct page **pages;
  1214. size = PAGE_ALIGN(size);
  1215. if (__in_atomic_pool(cpu_addr, size)) {
  1216. __iommu_free_atomic(dev, cpu_addr, handle, size);
  1217. return;
  1218. }
  1219. pages = __iommu_get_pages(cpu_addr, attrs);
  1220. if (!pages) {
  1221. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1222. return;
  1223. }
  1224. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1225. dma_common_free_remap(cpu_addr, size,
  1226. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1227. }
  1228. __iommu_remove_mapping(dev, handle, size);
  1229. __iommu_free_buffer(dev, pages, size, attrs);
  1230. }
  1231. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1232. void *cpu_addr, dma_addr_t dma_addr,
  1233. size_t size, struct dma_attrs *attrs)
  1234. {
  1235. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1236. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1237. if (!pages)
  1238. return -ENXIO;
  1239. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1240. GFP_KERNEL);
  1241. }
  1242. static int __dma_direction_to_prot(enum dma_data_direction dir)
  1243. {
  1244. int prot;
  1245. switch (dir) {
  1246. case DMA_BIDIRECTIONAL:
  1247. prot = IOMMU_READ | IOMMU_WRITE;
  1248. break;
  1249. case DMA_TO_DEVICE:
  1250. prot = IOMMU_READ;
  1251. break;
  1252. case DMA_FROM_DEVICE:
  1253. prot = IOMMU_WRITE;
  1254. break;
  1255. default:
  1256. prot = 0;
  1257. }
  1258. return prot;
  1259. }
  1260. /*
  1261. * Map a part of the scatter-gather list into contiguous io address space
  1262. */
  1263. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1264. size_t size, dma_addr_t *handle,
  1265. enum dma_data_direction dir, struct dma_attrs *attrs,
  1266. bool is_coherent)
  1267. {
  1268. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1269. dma_addr_t iova, iova_base;
  1270. int ret = 0;
  1271. unsigned int count;
  1272. struct scatterlist *s;
  1273. int prot;
  1274. size = PAGE_ALIGN(size);
  1275. *handle = DMA_ERROR_CODE;
  1276. iova_base = iova = __alloc_iova(mapping, size);
  1277. if (iova == DMA_ERROR_CODE)
  1278. return -ENOMEM;
  1279. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1280. phys_addr_t phys = sg_phys(s) & PAGE_MASK;
  1281. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1282. if (!is_coherent &&
  1283. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1284. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1285. prot = __dma_direction_to_prot(dir);
  1286. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1287. if (ret < 0)
  1288. goto fail;
  1289. count += len >> PAGE_SHIFT;
  1290. iova += len;
  1291. }
  1292. *handle = iova_base;
  1293. return 0;
  1294. fail:
  1295. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1296. __free_iova(mapping, iova_base, size);
  1297. return ret;
  1298. }
  1299. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1300. enum dma_data_direction dir, struct dma_attrs *attrs,
  1301. bool is_coherent)
  1302. {
  1303. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1304. int i, count = 0;
  1305. unsigned int offset = s->offset;
  1306. unsigned int size = s->offset + s->length;
  1307. unsigned int max = dma_get_max_seg_size(dev);
  1308. for (i = 1; i < nents; i++) {
  1309. s = sg_next(s);
  1310. s->dma_address = DMA_ERROR_CODE;
  1311. s->dma_length = 0;
  1312. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1313. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1314. dir, attrs, is_coherent) < 0)
  1315. goto bad_mapping;
  1316. dma->dma_address += offset;
  1317. dma->dma_length = size - offset;
  1318. size = offset = s->offset;
  1319. start = s;
  1320. dma = sg_next(dma);
  1321. count += 1;
  1322. }
  1323. size += s->length;
  1324. }
  1325. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1326. is_coherent) < 0)
  1327. goto bad_mapping;
  1328. dma->dma_address += offset;
  1329. dma->dma_length = size - offset;
  1330. return count+1;
  1331. bad_mapping:
  1332. for_each_sg(sg, s, count, i)
  1333. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1334. return 0;
  1335. }
  1336. /**
  1337. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1338. * @dev: valid struct device pointer
  1339. * @sg: list of buffers
  1340. * @nents: number of buffers to map
  1341. * @dir: DMA transfer direction
  1342. *
  1343. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1344. * mode for DMA. The scatter gather list elements are merged together (if
  1345. * possible) and tagged with the appropriate dma address and length. They are
  1346. * obtained via sg_dma_{address,length}.
  1347. */
  1348. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1349. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1350. {
  1351. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1352. }
  1353. /**
  1354. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1355. * @dev: valid struct device pointer
  1356. * @sg: list of buffers
  1357. * @nents: number of buffers to map
  1358. * @dir: DMA transfer direction
  1359. *
  1360. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1361. * The scatter gather list elements are merged together (if possible) and
  1362. * tagged with the appropriate dma address and length. They are obtained via
  1363. * sg_dma_{address,length}.
  1364. */
  1365. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1366. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1367. {
  1368. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1369. }
  1370. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1371. int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
  1372. bool is_coherent)
  1373. {
  1374. struct scatterlist *s;
  1375. int i;
  1376. for_each_sg(sg, s, nents, i) {
  1377. if (sg_dma_len(s))
  1378. __iommu_remove_mapping(dev, sg_dma_address(s),
  1379. sg_dma_len(s));
  1380. if (!is_coherent &&
  1381. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1382. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1383. s->length, dir);
  1384. }
  1385. }
  1386. /**
  1387. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1388. * @dev: valid struct device pointer
  1389. * @sg: list of buffers
  1390. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1391. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1392. *
  1393. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1394. * rules concerning calls here are the same as for dma_unmap_single().
  1395. */
  1396. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1397. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1398. {
  1399. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1400. }
  1401. /**
  1402. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1403. * @dev: valid struct device pointer
  1404. * @sg: list of buffers
  1405. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1406. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1407. *
  1408. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1409. * rules concerning calls here are the same as for dma_unmap_single().
  1410. */
  1411. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1412. enum dma_data_direction dir, struct dma_attrs *attrs)
  1413. {
  1414. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1415. }
  1416. /**
  1417. * arm_iommu_sync_sg_for_cpu
  1418. * @dev: valid struct device pointer
  1419. * @sg: list of buffers
  1420. * @nents: number of buffers to map (returned from dma_map_sg)
  1421. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1422. */
  1423. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1424. int nents, enum dma_data_direction dir)
  1425. {
  1426. struct scatterlist *s;
  1427. int i;
  1428. for_each_sg(sg, s, nents, i)
  1429. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1430. }
  1431. /**
  1432. * arm_iommu_sync_sg_for_device
  1433. * @dev: valid struct device pointer
  1434. * @sg: list of buffers
  1435. * @nents: number of buffers to map (returned from dma_map_sg)
  1436. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1437. */
  1438. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1439. int nents, enum dma_data_direction dir)
  1440. {
  1441. struct scatterlist *s;
  1442. int i;
  1443. for_each_sg(sg, s, nents, i)
  1444. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1445. }
  1446. /**
  1447. * arm_coherent_iommu_map_page
  1448. * @dev: valid struct device pointer
  1449. * @page: page that buffer resides in
  1450. * @offset: offset into page for start of buffer
  1451. * @size: size of buffer to map
  1452. * @dir: DMA transfer direction
  1453. *
  1454. * Coherent IOMMU aware version of arm_dma_map_page()
  1455. */
  1456. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1457. unsigned long offset, size_t size, enum dma_data_direction dir,
  1458. struct dma_attrs *attrs)
  1459. {
  1460. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1461. dma_addr_t dma_addr;
  1462. int ret, prot, len = PAGE_ALIGN(size + offset);
  1463. dma_addr = __alloc_iova(mapping, len);
  1464. if (dma_addr == DMA_ERROR_CODE)
  1465. return dma_addr;
  1466. prot = __dma_direction_to_prot(dir);
  1467. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1468. if (ret < 0)
  1469. goto fail;
  1470. return dma_addr + offset;
  1471. fail:
  1472. __free_iova(mapping, dma_addr, len);
  1473. return DMA_ERROR_CODE;
  1474. }
  1475. /**
  1476. * arm_iommu_map_page
  1477. * @dev: valid struct device pointer
  1478. * @page: page that buffer resides in
  1479. * @offset: offset into page for start of buffer
  1480. * @size: size of buffer to map
  1481. * @dir: DMA transfer direction
  1482. *
  1483. * IOMMU aware version of arm_dma_map_page()
  1484. */
  1485. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1486. unsigned long offset, size_t size, enum dma_data_direction dir,
  1487. struct dma_attrs *attrs)
  1488. {
  1489. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1490. __dma_page_cpu_to_dev(page, offset, size, dir);
  1491. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1492. }
  1493. /**
  1494. * arm_coherent_iommu_unmap_page
  1495. * @dev: valid struct device pointer
  1496. * @handle: DMA address of buffer
  1497. * @size: size of buffer (same as passed to dma_map_page)
  1498. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1499. *
  1500. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1501. */
  1502. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1503. size_t size, enum dma_data_direction dir,
  1504. struct dma_attrs *attrs)
  1505. {
  1506. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1507. dma_addr_t iova = handle & PAGE_MASK;
  1508. int offset = handle & ~PAGE_MASK;
  1509. int len = PAGE_ALIGN(size + offset);
  1510. if (!iova)
  1511. return;
  1512. iommu_unmap(mapping->domain, iova, len);
  1513. __free_iova(mapping, iova, len);
  1514. }
  1515. /**
  1516. * arm_iommu_unmap_page
  1517. * @dev: valid struct device pointer
  1518. * @handle: DMA address of buffer
  1519. * @size: size of buffer (same as passed to dma_map_page)
  1520. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1521. *
  1522. * IOMMU aware version of arm_dma_unmap_page()
  1523. */
  1524. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1525. size_t size, enum dma_data_direction dir,
  1526. struct dma_attrs *attrs)
  1527. {
  1528. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1529. dma_addr_t iova = handle & PAGE_MASK;
  1530. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1531. int offset = handle & ~PAGE_MASK;
  1532. int len = PAGE_ALIGN(size + offset);
  1533. if (!iova)
  1534. return;
  1535. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1536. __dma_page_dev_to_cpu(page, offset, size, dir);
  1537. iommu_unmap(mapping->domain, iova, len);
  1538. __free_iova(mapping, iova, len);
  1539. }
  1540. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1541. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1542. {
  1543. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1544. dma_addr_t iova = handle & PAGE_MASK;
  1545. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1546. unsigned int offset = handle & ~PAGE_MASK;
  1547. if (!iova)
  1548. return;
  1549. __dma_page_dev_to_cpu(page, offset, size, dir);
  1550. }
  1551. static void arm_iommu_sync_single_for_device(struct device *dev,
  1552. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1553. {
  1554. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1555. dma_addr_t iova = handle & PAGE_MASK;
  1556. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1557. unsigned int offset = handle & ~PAGE_MASK;
  1558. if (!iova)
  1559. return;
  1560. __dma_page_cpu_to_dev(page, offset, size, dir);
  1561. }
  1562. struct dma_map_ops iommu_ops = {
  1563. .alloc = arm_iommu_alloc_attrs,
  1564. .free = arm_iommu_free_attrs,
  1565. .mmap = arm_iommu_mmap_attrs,
  1566. .get_sgtable = arm_iommu_get_sgtable,
  1567. .map_page = arm_iommu_map_page,
  1568. .unmap_page = arm_iommu_unmap_page,
  1569. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1570. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1571. .map_sg = arm_iommu_map_sg,
  1572. .unmap_sg = arm_iommu_unmap_sg,
  1573. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1574. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1575. .set_dma_mask = arm_dma_set_mask,
  1576. };
  1577. struct dma_map_ops iommu_coherent_ops = {
  1578. .alloc = arm_iommu_alloc_attrs,
  1579. .free = arm_iommu_free_attrs,
  1580. .mmap = arm_iommu_mmap_attrs,
  1581. .get_sgtable = arm_iommu_get_sgtable,
  1582. .map_page = arm_coherent_iommu_map_page,
  1583. .unmap_page = arm_coherent_iommu_unmap_page,
  1584. .map_sg = arm_coherent_iommu_map_sg,
  1585. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1586. .set_dma_mask = arm_dma_set_mask,
  1587. };
  1588. /**
  1589. * arm_iommu_create_mapping
  1590. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1591. * @base: start address of the valid IO address space
  1592. * @size: maximum size of the valid IO address space
  1593. *
  1594. * Creates a mapping structure which holds information about used/unused
  1595. * IO address ranges, which is required to perform memory allocation and
  1596. * mapping with IOMMU aware functions.
  1597. *
  1598. * The client device need to be attached to the mapping with
  1599. * arm_iommu_attach_device function.
  1600. */
  1601. struct dma_iommu_mapping *
  1602. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1603. {
  1604. unsigned int bits = size >> PAGE_SHIFT;
  1605. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1606. struct dma_iommu_mapping *mapping;
  1607. int extensions = 1;
  1608. int err = -ENOMEM;
  1609. /* currently only 32-bit DMA address space is supported */
  1610. if (size > DMA_BIT_MASK(32) + 1)
  1611. return ERR_PTR(-ERANGE);
  1612. if (!bitmap_size)
  1613. return ERR_PTR(-EINVAL);
  1614. if (bitmap_size > PAGE_SIZE) {
  1615. extensions = bitmap_size / PAGE_SIZE;
  1616. bitmap_size = PAGE_SIZE;
  1617. }
  1618. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1619. if (!mapping)
  1620. goto err;
  1621. mapping->bitmap_size = bitmap_size;
  1622. mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
  1623. GFP_KERNEL);
  1624. if (!mapping->bitmaps)
  1625. goto err2;
  1626. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1627. if (!mapping->bitmaps[0])
  1628. goto err3;
  1629. mapping->nr_bitmaps = 1;
  1630. mapping->extensions = extensions;
  1631. mapping->base = base;
  1632. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1633. spin_lock_init(&mapping->lock);
  1634. mapping->domain = iommu_domain_alloc(bus);
  1635. if (!mapping->domain)
  1636. goto err4;
  1637. kref_init(&mapping->kref);
  1638. return mapping;
  1639. err4:
  1640. kfree(mapping->bitmaps[0]);
  1641. err3:
  1642. kfree(mapping->bitmaps);
  1643. err2:
  1644. kfree(mapping);
  1645. err:
  1646. return ERR_PTR(err);
  1647. }
  1648. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1649. static void release_iommu_mapping(struct kref *kref)
  1650. {
  1651. int i;
  1652. struct dma_iommu_mapping *mapping =
  1653. container_of(kref, struct dma_iommu_mapping, kref);
  1654. iommu_domain_free(mapping->domain);
  1655. for (i = 0; i < mapping->nr_bitmaps; i++)
  1656. kfree(mapping->bitmaps[i]);
  1657. kfree(mapping->bitmaps);
  1658. kfree(mapping);
  1659. }
  1660. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1661. {
  1662. int next_bitmap;
  1663. if (mapping->nr_bitmaps >= mapping->extensions)
  1664. return -EINVAL;
  1665. next_bitmap = mapping->nr_bitmaps;
  1666. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1667. GFP_ATOMIC);
  1668. if (!mapping->bitmaps[next_bitmap])
  1669. return -ENOMEM;
  1670. mapping->nr_bitmaps++;
  1671. return 0;
  1672. }
  1673. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1674. {
  1675. if (mapping)
  1676. kref_put(&mapping->kref, release_iommu_mapping);
  1677. }
  1678. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1679. static int __arm_iommu_attach_device(struct device *dev,
  1680. struct dma_iommu_mapping *mapping)
  1681. {
  1682. int err;
  1683. err = iommu_attach_device(mapping->domain, dev);
  1684. if (err)
  1685. return err;
  1686. kref_get(&mapping->kref);
  1687. to_dma_iommu_mapping(dev) = mapping;
  1688. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1689. return 0;
  1690. }
  1691. /**
  1692. * arm_iommu_attach_device
  1693. * @dev: valid struct device pointer
  1694. * @mapping: io address space mapping structure (returned from
  1695. * arm_iommu_create_mapping)
  1696. *
  1697. * Attaches specified io address space mapping to the provided device.
  1698. * This replaces the dma operations (dma_map_ops pointer) with the
  1699. * IOMMU aware version.
  1700. *
  1701. * More than one client might be attached to the same io address space
  1702. * mapping.
  1703. */
  1704. int arm_iommu_attach_device(struct device *dev,
  1705. struct dma_iommu_mapping *mapping)
  1706. {
  1707. int err;
  1708. err = __arm_iommu_attach_device(dev, mapping);
  1709. if (err)
  1710. return err;
  1711. set_dma_ops(dev, &iommu_ops);
  1712. return 0;
  1713. }
  1714. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1715. static void __arm_iommu_detach_device(struct device *dev)
  1716. {
  1717. struct dma_iommu_mapping *mapping;
  1718. mapping = to_dma_iommu_mapping(dev);
  1719. if (!mapping) {
  1720. dev_warn(dev, "Not attached\n");
  1721. return;
  1722. }
  1723. iommu_detach_device(mapping->domain, dev);
  1724. kref_put(&mapping->kref, release_iommu_mapping);
  1725. to_dma_iommu_mapping(dev) = NULL;
  1726. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1727. }
  1728. /**
  1729. * arm_iommu_detach_device
  1730. * @dev: valid struct device pointer
  1731. *
  1732. * Detaches the provided device from a previously attached map.
  1733. * This voids the dma operations (dma_map_ops pointer)
  1734. */
  1735. void arm_iommu_detach_device(struct device *dev)
  1736. {
  1737. __arm_iommu_detach_device(dev);
  1738. set_dma_ops(dev, NULL);
  1739. }
  1740. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1741. static struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1742. {
  1743. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1744. }
  1745. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1746. struct iommu_ops *iommu)
  1747. {
  1748. struct dma_iommu_mapping *mapping;
  1749. if (!iommu)
  1750. return false;
  1751. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1752. if (IS_ERR(mapping)) {
  1753. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1754. size, dev_name(dev));
  1755. return false;
  1756. }
  1757. if (__arm_iommu_attach_device(dev, mapping)) {
  1758. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  1759. dev_name(dev));
  1760. arm_iommu_release_mapping(mapping);
  1761. return false;
  1762. }
  1763. return true;
  1764. }
  1765. static void arm_teardown_iommu_dma_ops(struct device *dev)
  1766. {
  1767. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1768. if (!mapping)
  1769. return;
  1770. __arm_iommu_detach_device(dev);
  1771. arm_iommu_release_mapping(mapping);
  1772. }
  1773. #else
  1774. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1775. struct iommu_ops *iommu)
  1776. {
  1777. return false;
  1778. }
  1779. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  1780. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  1781. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  1782. static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  1783. {
  1784. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  1785. }
  1786. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1787. struct iommu_ops *iommu, bool coherent)
  1788. {
  1789. struct dma_map_ops *dma_ops;
  1790. dev->archdata.dma_coherent = coherent;
  1791. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  1792. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  1793. else
  1794. dma_ops = arm_get_dma_map_ops(coherent);
  1795. set_dma_ops(dev, dma_ops);
  1796. }
  1797. void arch_teardown_dma_ops(struct device *dev)
  1798. {
  1799. arm_teardown_iommu_dma_ops(dev);
  1800. }