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