dma-mapping.c 55 KB

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