dma-mapping.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. /*
  658. * A single sg entry may refer to multiple physically contiguous
  659. * pages. But we still need to process highmem pages individually.
  660. * If highmem is not configured then the bulk of this loop gets
  661. * optimized out.
  662. */
  663. size_t left = size;
  664. do {
  665. size_t len = left;
  666. void *vaddr;
  667. if (PageHighMem(page)) {
  668. if (len + offset > PAGE_SIZE) {
  669. if (offset >= PAGE_SIZE) {
  670. page += offset / PAGE_SIZE;
  671. offset %= PAGE_SIZE;
  672. }
  673. len = PAGE_SIZE - offset;
  674. }
  675. vaddr = kmap_high_get(page);
  676. if (vaddr) {
  677. vaddr += offset;
  678. op(vaddr, len, dir);
  679. kunmap_high(page);
  680. } else if (cache_is_vipt()) {
  681. /* unmapped pages might still be cached */
  682. vaddr = kmap_atomic(page);
  683. op(vaddr + offset, len, dir);
  684. kunmap_atomic(vaddr);
  685. }
  686. } else {
  687. vaddr = page_address(page) + offset;
  688. op(vaddr, len, dir);
  689. }
  690. offset = 0;
  691. page++;
  692. left -= len;
  693. } while (left);
  694. }
  695. /*
  696. * Make an area consistent for devices.
  697. * Note: Drivers should NOT use this function directly, as it will break
  698. * platforms with CONFIG_DMABOUNCE.
  699. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  700. */
  701. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  702. size_t size, enum dma_data_direction dir)
  703. {
  704. unsigned long paddr;
  705. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  706. paddr = page_to_phys(page) + off;
  707. if (dir == DMA_FROM_DEVICE) {
  708. outer_inv_range(paddr, paddr + size);
  709. } else {
  710. outer_clean_range(paddr, paddr + size);
  711. }
  712. /* FIXME: non-speculating: flush on bidirectional mappings? */
  713. }
  714. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  715. size_t size, enum dma_data_direction dir)
  716. {
  717. unsigned long paddr = page_to_phys(page) + off;
  718. /* FIXME: non-speculating: not required */
  719. /* don't bother invalidating if DMA to device */
  720. if (dir != DMA_TO_DEVICE)
  721. outer_inv_range(paddr, paddr + size);
  722. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  723. /*
  724. * Mark the D-cache clean for this page to avoid extra flushing.
  725. */
  726. if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
  727. set_bit(PG_dcache_clean, &page->flags);
  728. }
  729. /**
  730. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  731. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  732. * @sg: list of buffers
  733. * @nents: number of buffers to map
  734. * @dir: DMA transfer direction
  735. *
  736. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  737. * This is the scatter-gather version of the dma_map_single interface.
  738. * Here the scatter gather list elements are each tagged with the
  739. * appropriate dma address and length. They are obtained via
  740. * sg_dma_{address,length}.
  741. *
  742. * Device ownership issues as mentioned for dma_map_single are the same
  743. * here.
  744. */
  745. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  746. enum dma_data_direction dir, struct dma_attrs *attrs)
  747. {
  748. struct dma_map_ops *ops = get_dma_ops(dev);
  749. struct scatterlist *s;
  750. int i, j;
  751. for_each_sg(sg, s, nents, i) {
  752. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  753. s->dma_length = s->length;
  754. #endif
  755. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  756. s->length, dir, attrs);
  757. if (dma_mapping_error(dev, s->dma_address))
  758. goto bad_mapping;
  759. }
  760. return nents;
  761. bad_mapping:
  762. for_each_sg(sg, s, i, j)
  763. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  764. return 0;
  765. }
  766. /**
  767. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  768. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  769. * @sg: list of buffers
  770. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  771. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  772. *
  773. * Unmap a set of streaming mode DMA translations. Again, CPU access
  774. * rules concerning calls here are the same as for dma_unmap_single().
  775. */
  776. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  777. enum dma_data_direction dir, struct dma_attrs *attrs)
  778. {
  779. struct dma_map_ops *ops = get_dma_ops(dev);
  780. struct scatterlist *s;
  781. int i;
  782. for_each_sg(sg, s, nents, i)
  783. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  784. }
  785. /**
  786. * arm_dma_sync_sg_for_cpu
  787. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  788. * @sg: list of buffers
  789. * @nents: number of buffers to map (returned from dma_map_sg)
  790. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  791. */
  792. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  793. int nents, enum dma_data_direction dir)
  794. {
  795. struct dma_map_ops *ops = get_dma_ops(dev);
  796. struct scatterlist *s;
  797. int i;
  798. for_each_sg(sg, s, nents, i)
  799. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  800. dir);
  801. }
  802. /**
  803. * arm_dma_sync_sg_for_device
  804. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  805. * @sg: list of buffers
  806. * @nents: number of buffers to map (returned from dma_map_sg)
  807. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  808. */
  809. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  810. int nents, enum dma_data_direction dir)
  811. {
  812. struct dma_map_ops *ops = get_dma_ops(dev);
  813. struct scatterlist *s;
  814. int i;
  815. for_each_sg(sg, s, nents, i)
  816. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  817. dir);
  818. }
  819. /*
  820. * Return whether the given device DMA address mask can be supported
  821. * properly. For example, if your device can only drive the low 24-bits
  822. * during bus mastering, then you would pass 0x00ffffff as the mask
  823. * to this function.
  824. */
  825. int dma_supported(struct device *dev, u64 mask)
  826. {
  827. if (mask < (u64)arm_dma_limit)
  828. return 0;
  829. return 1;
  830. }
  831. EXPORT_SYMBOL(dma_supported);
  832. int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  833. {
  834. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  835. return -EIO;
  836. *dev->dma_mask = dma_mask;
  837. return 0;
  838. }
  839. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  840. static int __init dma_debug_do_init(void)
  841. {
  842. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  843. return 0;
  844. }
  845. fs_initcall(dma_debug_do_init);
  846. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  847. /* IOMMU */
  848. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  849. size_t size)
  850. {
  851. unsigned int order = get_order(size);
  852. unsigned int align = 0;
  853. unsigned int count, start;
  854. unsigned long flags;
  855. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  856. (1 << mapping->order) - 1) >> mapping->order;
  857. if (order > mapping->order)
  858. align = (1 << (order - mapping->order)) - 1;
  859. spin_lock_irqsave(&mapping->lock, flags);
  860. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  861. count, align);
  862. if (start > mapping->bits) {
  863. spin_unlock_irqrestore(&mapping->lock, flags);
  864. return DMA_ERROR_CODE;
  865. }
  866. bitmap_set(mapping->bitmap, start, count);
  867. spin_unlock_irqrestore(&mapping->lock, flags);
  868. return mapping->base + (start << (mapping->order + PAGE_SHIFT));
  869. }
  870. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  871. dma_addr_t addr, size_t size)
  872. {
  873. unsigned int start = (addr - mapping->base) >>
  874. (mapping->order + PAGE_SHIFT);
  875. unsigned int count = ((size >> PAGE_SHIFT) +
  876. (1 << mapping->order) - 1) >> mapping->order;
  877. unsigned long flags;
  878. spin_lock_irqsave(&mapping->lock, flags);
  879. bitmap_clear(mapping->bitmap, start, count);
  880. spin_unlock_irqrestore(&mapping->lock, flags);
  881. }
  882. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  883. gfp_t gfp, struct dma_attrs *attrs)
  884. {
  885. struct page **pages;
  886. int count = size >> PAGE_SHIFT;
  887. int array_size = count * sizeof(struct page *);
  888. int i = 0;
  889. if (array_size <= PAGE_SIZE)
  890. pages = kzalloc(array_size, gfp);
  891. else
  892. pages = vzalloc(array_size);
  893. if (!pages)
  894. return NULL;
  895. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
  896. {
  897. unsigned long order = get_order(size);
  898. struct page *page;
  899. page = dma_alloc_from_contiguous(dev, count, order);
  900. if (!page)
  901. goto error;
  902. __dma_clear_buffer(page, size);
  903. for (i = 0; i < count; i++)
  904. pages[i] = page + i;
  905. return pages;
  906. }
  907. while (count) {
  908. int j, order = __fls(count);
  909. pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
  910. while (!pages[i] && order)
  911. pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
  912. if (!pages[i])
  913. goto error;
  914. if (order) {
  915. split_page(pages[i], order);
  916. j = 1 << order;
  917. while (--j)
  918. pages[i + j] = pages[i] + j;
  919. }
  920. __dma_clear_buffer(pages[i], PAGE_SIZE << order);
  921. i += 1 << order;
  922. count -= 1 << order;
  923. }
  924. return pages;
  925. error:
  926. while (i--)
  927. if (pages[i])
  928. __free_pages(pages[i], 0);
  929. if (array_size <= PAGE_SIZE)
  930. kfree(pages);
  931. else
  932. vfree(pages);
  933. return NULL;
  934. }
  935. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  936. size_t size, struct dma_attrs *attrs)
  937. {
  938. int count = size >> PAGE_SHIFT;
  939. int array_size = count * sizeof(struct page *);
  940. int i;
  941. if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
  942. dma_release_from_contiguous(dev, pages[0], count);
  943. } else {
  944. for (i = 0; i < count; i++)
  945. if (pages[i])
  946. __free_pages(pages[i], 0);
  947. }
  948. if (array_size <= PAGE_SIZE)
  949. kfree(pages);
  950. else
  951. vfree(pages);
  952. return 0;
  953. }
  954. /*
  955. * Create a CPU mapping for a specified pages
  956. */
  957. static void *
  958. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  959. const void *caller)
  960. {
  961. unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  962. struct vm_struct *area;
  963. unsigned long p;
  964. area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  965. caller);
  966. if (!area)
  967. return NULL;
  968. area->pages = pages;
  969. area->nr_pages = nr_pages;
  970. p = (unsigned long)area->addr;
  971. for (i = 0; i < nr_pages; i++) {
  972. phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
  973. if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
  974. goto err;
  975. p += PAGE_SIZE;
  976. }
  977. return area->addr;
  978. err:
  979. unmap_kernel_range((unsigned long)area->addr, size);
  980. vunmap(area->addr);
  981. return NULL;
  982. }
  983. /*
  984. * Create a mapping in device IO address space for specified pages
  985. */
  986. static dma_addr_t
  987. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  988. {
  989. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  990. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  991. dma_addr_t dma_addr, iova;
  992. int i, ret = DMA_ERROR_CODE;
  993. dma_addr = __alloc_iova(mapping, size);
  994. if (dma_addr == DMA_ERROR_CODE)
  995. return dma_addr;
  996. iova = dma_addr;
  997. for (i = 0; i < count; ) {
  998. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  999. phys_addr_t phys = page_to_phys(pages[i]);
  1000. unsigned int len, j;
  1001. for (j = i + 1; j < count; j++, next_pfn++)
  1002. if (page_to_pfn(pages[j]) != next_pfn)
  1003. break;
  1004. len = (j - i) << PAGE_SHIFT;
  1005. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1006. if (ret < 0)
  1007. goto fail;
  1008. iova += len;
  1009. i = j;
  1010. }
  1011. return dma_addr;
  1012. fail:
  1013. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1014. __free_iova(mapping, dma_addr, size);
  1015. return DMA_ERROR_CODE;
  1016. }
  1017. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1018. {
  1019. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1020. /*
  1021. * add optional in-page offset from iova to size and align
  1022. * result to page size
  1023. */
  1024. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1025. iova &= PAGE_MASK;
  1026. iommu_unmap(mapping->domain, iova, size);
  1027. __free_iova(mapping, iova, size);
  1028. return 0;
  1029. }
  1030. static struct page **__atomic_get_pages(void *addr)
  1031. {
  1032. struct dma_pool *pool = &atomic_pool;
  1033. struct page **pages = pool->pages;
  1034. int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
  1035. return pages + offs;
  1036. }
  1037. static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
  1038. {
  1039. struct vm_struct *area;
  1040. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1041. return __atomic_get_pages(cpu_addr);
  1042. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1043. return cpu_addr;
  1044. area = find_vm_area(cpu_addr);
  1045. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1046. return area->pages;
  1047. return NULL;
  1048. }
  1049. static void *__iommu_alloc_atomic(struct device *dev, size_t size,
  1050. dma_addr_t *handle)
  1051. {
  1052. struct page *page;
  1053. void *addr;
  1054. addr = __alloc_from_pool(size, &page);
  1055. if (!addr)
  1056. return NULL;
  1057. *handle = __iommu_create_mapping(dev, &page, size);
  1058. if (*handle == DMA_ERROR_CODE)
  1059. goto err_mapping;
  1060. return addr;
  1061. err_mapping:
  1062. __free_from_pool(addr, size);
  1063. return NULL;
  1064. }
  1065. static void __iommu_free_atomic(struct device *dev, struct page **pages,
  1066. dma_addr_t handle, size_t size)
  1067. {
  1068. __iommu_remove_mapping(dev, handle, size);
  1069. __free_from_pool(page_address(pages[0]), size);
  1070. }
  1071. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1072. dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
  1073. {
  1074. pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
  1075. struct page **pages;
  1076. void *addr = NULL;
  1077. *handle = DMA_ERROR_CODE;
  1078. size = PAGE_ALIGN(size);
  1079. if (gfp & GFP_ATOMIC)
  1080. return __iommu_alloc_atomic(dev, size, handle);
  1081. pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
  1082. if (!pages)
  1083. return NULL;
  1084. *handle = __iommu_create_mapping(dev, pages, size);
  1085. if (*handle == DMA_ERROR_CODE)
  1086. goto err_buffer;
  1087. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
  1088. return pages;
  1089. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1090. __builtin_return_address(0));
  1091. if (!addr)
  1092. goto err_mapping;
  1093. return addr;
  1094. err_mapping:
  1095. __iommu_remove_mapping(dev, *handle, size);
  1096. err_buffer:
  1097. __iommu_free_buffer(dev, pages, size, attrs);
  1098. return NULL;
  1099. }
  1100. static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1101. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1102. struct dma_attrs *attrs)
  1103. {
  1104. unsigned long uaddr = vma->vm_start;
  1105. unsigned long usize = vma->vm_end - vma->vm_start;
  1106. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1107. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1108. if (!pages)
  1109. return -ENXIO;
  1110. do {
  1111. int ret = vm_insert_page(vma, uaddr, *pages++);
  1112. if (ret) {
  1113. pr_err("Remapping memory failed: %d\n", ret);
  1114. return ret;
  1115. }
  1116. uaddr += PAGE_SIZE;
  1117. usize -= PAGE_SIZE;
  1118. } while (usize > 0);
  1119. return 0;
  1120. }
  1121. /*
  1122. * free a page as defined by the above mapping.
  1123. * Must not be called with IRQs disabled.
  1124. */
  1125. void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1126. dma_addr_t handle, struct dma_attrs *attrs)
  1127. {
  1128. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1129. size = PAGE_ALIGN(size);
  1130. if (!pages) {
  1131. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1132. return;
  1133. }
  1134. if (__in_atomic_pool(cpu_addr, size)) {
  1135. __iommu_free_atomic(dev, pages, handle, size);
  1136. return;
  1137. }
  1138. if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
  1139. unmap_kernel_range((unsigned long)cpu_addr, size);
  1140. vunmap(cpu_addr);
  1141. }
  1142. __iommu_remove_mapping(dev, handle, size);
  1143. __iommu_free_buffer(dev, pages, size, attrs);
  1144. }
  1145. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1146. void *cpu_addr, dma_addr_t dma_addr,
  1147. size_t size, struct dma_attrs *attrs)
  1148. {
  1149. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1150. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1151. if (!pages)
  1152. return -ENXIO;
  1153. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1154. GFP_KERNEL);
  1155. }
  1156. /*
  1157. * Map a part of the scatter-gather list into contiguous io address space
  1158. */
  1159. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1160. size_t size, dma_addr_t *handle,
  1161. enum dma_data_direction dir, struct dma_attrs *attrs,
  1162. bool is_coherent)
  1163. {
  1164. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1165. dma_addr_t iova, iova_base;
  1166. int ret = 0;
  1167. unsigned int count;
  1168. struct scatterlist *s;
  1169. size = PAGE_ALIGN(size);
  1170. *handle = DMA_ERROR_CODE;
  1171. iova_base = iova = __alloc_iova(mapping, size);
  1172. if (iova == DMA_ERROR_CODE)
  1173. return -ENOMEM;
  1174. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1175. phys_addr_t phys = page_to_phys(sg_page(s));
  1176. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1177. if (!is_coherent &&
  1178. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1179. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1180. ret = iommu_map(mapping->domain, iova, phys, len, 0);
  1181. if (ret < 0)
  1182. goto fail;
  1183. count += len >> PAGE_SHIFT;
  1184. iova += len;
  1185. }
  1186. *handle = iova_base;
  1187. return 0;
  1188. fail:
  1189. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1190. __free_iova(mapping, iova_base, size);
  1191. return ret;
  1192. }
  1193. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1194. enum dma_data_direction dir, struct dma_attrs *attrs,
  1195. bool is_coherent)
  1196. {
  1197. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1198. int i, count = 0;
  1199. unsigned int offset = s->offset;
  1200. unsigned int size = s->offset + s->length;
  1201. unsigned int max = dma_get_max_seg_size(dev);
  1202. for (i = 1; i < nents; i++) {
  1203. s = sg_next(s);
  1204. s->dma_address = DMA_ERROR_CODE;
  1205. s->dma_length = 0;
  1206. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1207. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1208. dir, attrs, is_coherent) < 0)
  1209. goto bad_mapping;
  1210. dma->dma_address += offset;
  1211. dma->dma_length = size - offset;
  1212. size = offset = s->offset;
  1213. start = s;
  1214. dma = sg_next(dma);
  1215. count += 1;
  1216. }
  1217. size += s->length;
  1218. }
  1219. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1220. is_coherent) < 0)
  1221. goto bad_mapping;
  1222. dma->dma_address += offset;
  1223. dma->dma_length = size - offset;
  1224. return count+1;
  1225. bad_mapping:
  1226. for_each_sg(sg, s, count, i)
  1227. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1228. return 0;
  1229. }
  1230. /**
  1231. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1232. * @dev: valid struct device pointer
  1233. * @sg: list of buffers
  1234. * @nents: number of buffers to map
  1235. * @dir: DMA transfer direction
  1236. *
  1237. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1238. * mode for DMA. The scatter gather list elements are merged together (if
  1239. * possible) and tagged with the appropriate dma address and length. They are
  1240. * obtained via sg_dma_{address,length}.
  1241. */
  1242. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1243. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1244. {
  1245. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1246. }
  1247. /**
  1248. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1249. * @dev: valid struct device pointer
  1250. * @sg: list of buffers
  1251. * @nents: number of buffers to map
  1252. * @dir: DMA transfer direction
  1253. *
  1254. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1255. * The scatter gather list elements are merged together (if possible) and
  1256. * tagged with the appropriate dma address and length. They are obtained via
  1257. * sg_dma_{address,length}.
  1258. */
  1259. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1260. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1261. {
  1262. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1263. }
  1264. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1265. int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
  1266. bool is_coherent)
  1267. {
  1268. struct scatterlist *s;
  1269. int i;
  1270. for_each_sg(sg, s, nents, i) {
  1271. if (sg_dma_len(s))
  1272. __iommu_remove_mapping(dev, sg_dma_address(s),
  1273. sg_dma_len(s));
  1274. if (!is_coherent &&
  1275. !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1276. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1277. s->length, dir);
  1278. }
  1279. }
  1280. /**
  1281. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1282. * @dev: valid struct device pointer
  1283. * @sg: list of buffers
  1284. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1285. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1286. *
  1287. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1288. * rules concerning calls here are the same as for dma_unmap_single().
  1289. */
  1290. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1291. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  1292. {
  1293. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1294. }
  1295. /**
  1296. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1297. * @dev: valid struct device pointer
  1298. * @sg: list of buffers
  1299. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1300. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1301. *
  1302. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1303. * rules concerning calls here are the same as for dma_unmap_single().
  1304. */
  1305. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1306. enum dma_data_direction dir, struct dma_attrs *attrs)
  1307. {
  1308. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1309. }
  1310. /**
  1311. * arm_iommu_sync_sg_for_cpu
  1312. * @dev: valid struct device pointer
  1313. * @sg: list of buffers
  1314. * @nents: number of buffers to map (returned from dma_map_sg)
  1315. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1316. */
  1317. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1318. int nents, enum dma_data_direction dir)
  1319. {
  1320. struct scatterlist *s;
  1321. int i;
  1322. for_each_sg(sg, s, nents, i)
  1323. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1324. }
  1325. /**
  1326. * arm_iommu_sync_sg_for_device
  1327. * @dev: valid struct device pointer
  1328. * @sg: list of buffers
  1329. * @nents: number of buffers to map (returned from dma_map_sg)
  1330. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1331. */
  1332. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1333. int nents, enum dma_data_direction dir)
  1334. {
  1335. struct scatterlist *s;
  1336. int i;
  1337. for_each_sg(sg, s, nents, i)
  1338. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1339. }
  1340. /**
  1341. * arm_coherent_iommu_map_page
  1342. * @dev: valid struct device pointer
  1343. * @page: page that buffer resides in
  1344. * @offset: offset into page for start of buffer
  1345. * @size: size of buffer to map
  1346. * @dir: DMA transfer direction
  1347. *
  1348. * Coherent IOMMU aware version of arm_dma_map_page()
  1349. */
  1350. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1351. unsigned long offset, size_t size, enum dma_data_direction dir,
  1352. struct dma_attrs *attrs)
  1353. {
  1354. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1355. dma_addr_t dma_addr;
  1356. int ret, len = PAGE_ALIGN(size + offset);
  1357. dma_addr = __alloc_iova(mapping, len);
  1358. if (dma_addr == DMA_ERROR_CODE)
  1359. return dma_addr;
  1360. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
  1361. if (ret < 0)
  1362. goto fail;
  1363. return dma_addr + offset;
  1364. fail:
  1365. __free_iova(mapping, dma_addr, len);
  1366. return DMA_ERROR_CODE;
  1367. }
  1368. /**
  1369. * arm_iommu_map_page
  1370. * @dev: valid struct device pointer
  1371. * @page: page that buffer resides in
  1372. * @offset: offset into page for start of buffer
  1373. * @size: size of buffer to map
  1374. * @dir: DMA transfer direction
  1375. *
  1376. * IOMMU aware version of arm_dma_map_page()
  1377. */
  1378. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1379. unsigned long offset, size_t size, enum dma_data_direction dir,
  1380. struct dma_attrs *attrs)
  1381. {
  1382. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1383. __dma_page_cpu_to_dev(page, offset, size, dir);
  1384. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1385. }
  1386. /**
  1387. * arm_coherent_iommu_unmap_page
  1388. * @dev: valid struct device pointer
  1389. * @handle: DMA address of buffer
  1390. * @size: size of buffer (same as passed to dma_map_page)
  1391. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1392. *
  1393. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1394. */
  1395. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1396. size_t size, enum dma_data_direction dir,
  1397. struct dma_attrs *attrs)
  1398. {
  1399. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1400. dma_addr_t iova = handle & PAGE_MASK;
  1401. int offset = handle & ~PAGE_MASK;
  1402. int len = PAGE_ALIGN(size + offset);
  1403. if (!iova)
  1404. return;
  1405. iommu_unmap(mapping->domain, iova, len);
  1406. __free_iova(mapping, iova, len);
  1407. }
  1408. /**
  1409. * arm_iommu_unmap_page
  1410. * @dev: valid struct device pointer
  1411. * @handle: DMA address of buffer
  1412. * @size: size of buffer (same as passed to dma_map_page)
  1413. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1414. *
  1415. * IOMMU aware version of arm_dma_unmap_page()
  1416. */
  1417. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1418. size_t size, enum dma_data_direction dir,
  1419. struct dma_attrs *attrs)
  1420. {
  1421. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1422. dma_addr_t iova = handle & PAGE_MASK;
  1423. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1424. int offset = handle & ~PAGE_MASK;
  1425. int len = PAGE_ALIGN(size + offset);
  1426. if (!iova)
  1427. return;
  1428. if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  1429. __dma_page_dev_to_cpu(page, offset, size, dir);
  1430. iommu_unmap(mapping->domain, iova, len);
  1431. __free_iova(mapping, iova, len);
  1432. }
  1433. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1434. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1435. {
  1436. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1437. dma_addr_t iova = handle & PAGE_MASK;
  1438. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1439. unsigned int offset = handle & ~PAGE_MASK;
  1440. if (!iova)
  1441. return;
  1442. __dma_page_dev_to_cpu(page, offset, size, dir);
  1443. }
  1444. static void arm_iommu_sync_single_for_device(struct device *dev,
  1445. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1446. {
  1447. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  1448. dma_addr_t iova = handle & PAGE_MASK;
  1449. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1450. unsigned int offset = handle & ~PAGE_MASK;
  1451. if (!iova)
  1452. return;
  1453. __dma_page_cpu_to_dev(page, offset, size, dir);
  1454. }
  1455. struct dma_map_ops iommu_ops = {
  1456. .alloc = arm_iommu_alloc_attrs,
  1457. .free = arm_iommu_free_attrs,
  1458. .mmap = arm_iommu_mmap_attrs,
  1459. .get_sgtable = arm_iommu_get_sgtable,
  1460. .map_page = arm_iommu_map_page,
  1461. .unmap_page = arm_iommu_unmap_page,
  1462. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1463. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1464. .map_sg = arm_iommu_map_sg,
  1465. .unmap_sg = arm_iommu_unmap_sg,
  1466. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1467. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1468. };
  1469. struct dma_map_ops iommu_coherent_ops = {
  1470. .alloc = arm_iommu_alloc_attrs,
  1471. .free = arm_iommu_free_attrs,
  1472. .mmap = arm_iommu_mmap_attrs,
  1473. .get_sgtable = arm_iommu_get_sgtable,
  1474. .map_page = arm_coherent_iommu_map_page,
  1475. .unmap_page = arm_coherent_iommu_unmap_page,
  1476. .map_sg = arm_coherent_iommu_map_sg,
  1477. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1478. };
  1479. /**
  1480. * arm_iommu_create_mapping
  1481. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1482. * @base: start address of the valid IO address space
  1483. * @size: size of the valid IO address space
  1484. * @order: accuracy of the IO addresses allocations
  1485. *
  1486. * Creates a mapping structure which holds information about used/unused
  1487. * IO address ranges, which is required to perform memory allocation and
  1488. * mapping with IOMMU aware functions.
  1489. *
  1490. * The client device need to be attached to the mapping with
  1491. * arm_iommu_attach_device function.
  1492. */
  1493. struct dma_iommu_mapping *
  1494. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
  1495. int order)
  1496. {
  1497. unsigned int count = size >> (PAGE_SHIFT + order);
  1498. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  1499. struct dma_iommu_mapping *mapping;
  1500. int err = -ENOMEM;
  1501. if (!count)
  1502. return ERR_PTR(-EINVAL);
  1503. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1504. if (!mapping)
  1505. goto err;
  1506. mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1507. if (!mapping->bitmap)
  1508. goto err2;
  1509. mapping->base = base;
  1510. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1511. mapping->order = order;
  1512. spin_lock_init(&mapping->lock);
  1513. mapping->domain = iommu_domain_alloc(bus);
  1514. if (!mapping->domain)
  1515. goto err3;
  1516. kref_init(&mapping->kref);
  1517. return mapping;
  1518. err3:
  1519. kfree(mapping->bitmap);
  1520. err2:
  1521. kfree(mapping);
  1522. err:
  1523. return ERR_PTR(err);
  1524. }
  1525. static void release_iommu_mapping(struct kref *kref)
  1526. {
  1527. struct dma_iommu_mapping *mapping =
  1528. container_of(kref, struct dma_iommu_mapping, kref);
  1529. iommu_domain_free(mapping->domain);
  1530. kfree(mapping->bitmap);
  1531. kfree(mapping);
  1532. }
  1533. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1534. {
  1535. if (mapping)
  1536. kref_put(&mapping->kref, release_iommu_mapping);
  1537. }
  1538. /**
  1539. * arm_iommu_attach_device
  1540. * @dev: valid struct device pointer
  1541. * @mapping: io address space mapping structure (returned from
  1542. * arm_iommu_create_mapping)
  1543. *
  1544. * Attaches specified io address space mapping to the provided device,
  1545. * this replaces the dma operations (dma_map_ops pointer) with the
  1546. * IOMMU aware version. More than one client might be attached to
  1547. * the same io address space mapping.
  1548. */
  1549. int arm_iommu_attach_device(struct device *dev,
  1550. struct dma_iommu_mapping *mapping)
  1551. {
  1552. int err;
  1553. err = iommu_attach_device(mapping->domain, dev);
  1554. if (err)
  1555. return err;
  1556. kref_get(&mapping->kref);
  1557. dev->archdata.mapping = mapping;
  1558. set_dma_ops(dev, &iommu_ops);
  1559. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1560. return 0;
  1561. }
  1562. #endif