dma-mapping.c 54 KB

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