dma-mapping.c 54 KB

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