dma-mapping.c 53 KB

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