dma-mapping.c 52 KB

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