dma-mapping.c 49 KB

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