dma-mapping.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /*
  2. * linux/arch/arm/mm/dma-mapping.c
  3. *
  4. * Copyright (C) 2000-2004 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * DMA uncached mapping support.
  11. */
  12. #include <linux/bootmem.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/genalloc.h>
  16. #include <linux/gfp.h>
  17. #include <linux/errno.h>
  18. #include <linux/list.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dma-contiguous.h>
  23. #include <linux/highmem.h>
  24. #include <linux/memblock.h>
  25. #include <linux/slab.h>
  26. #include <linux/iommu.h>
  27. #include <linux/io.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/sizes.h>
  30. #include <linux/cma.h>
  31. #include <asm/memory.h>
  32. #include <asm/highmem.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/mach/arch.h>
  36. #include <asm/dma-iommu.h>
  37. #include <asm/mach/map.h>
  38. #include <asm/system_info.h>
  39. #include <asm/dma-contiguous.h>
  40. #include "dma.h"
  41. #include "mm.h"
  42. struct arm_dma_alloc_args {
  43. struct device *dev;
  44. size_t size;
  45. gfp_t gfp;
  46. pgprot_t prot;
  47. const void *caller;
  48. bool want_vaddr;
  49. int coherent_flag;
  50. };
  51. struct arm_dma_free_args {
  52. struct device *dev;
  53. size_t size;
  54. void *cpu_addr;
  55. struct page *page;
  56. bool want_vaddr;
  57. };
  58. #define NORMAL 0
  59. #define COHERENT 1
  60. struct arm_dma_allocator {
  61. void *(*alloc)(struct arm_dma_alloc_args *args,
  62. struct page **ret_page);
  63. void (*free)(struct arm_dma_free_args *args);
  64. };
  65. struct arm_dma_buffer {
  66. struct list_head list;
  67. void *virt;
  68. struct arm_dma_allocator *allocator;
  69. };
  70. static LIST_HEAD(arm_dma_bufs);
  71. static DEFINE_SPINLOCK(arm_dma_bufs_lock);
  72. static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
  73. {
  74. struct arm_dma_buffer *buf, *found = NULL;
  75. unsigned long flags;
  76. spin_lock_irqsave(&arm_dma_bufs_lock, flags);
  77. list_for_each_entry(buf, &arm_dma_bufs, list) {
  78. if (buf->virt == virt) {
  79. list_del(&buf->list);
  80. found = buf;
  81. break;
  82. }
  83. }
  84. spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
  85. return found;
  86. }
  87. /*
  88. * The DMA API is built upon the notion of "buffer ownership". A buffer
  89. * is either exclusively owned by the CPU (and therefore may be accessed
  90. * by it) or exclusively owned by the DMA device. These helper functions
  91. * represent the transitions between these two ownership states.
  92. *
  93. * Note, however, that on later ARMs, this notion does not work due to
  94. * speculative prefetches. We model our approach on the assumption that
  95. * the CPU does do speculative prefetches, which means we clean caches
  96. * before transfers and delay cache invalidation until transfer completion.
  97. *
  98. */
  99. static void __dma_page_cpu_to_dev(struct page *, unsigned long,
  100. size_t, enum dma_data_direction);
  101. static void __dma_page_dev_to_cpu(struct page *, unsigned long,
  102. size_t, enum dma_data_direction);
  103. /**
  104. * arm_dma_map_page - map a portion of a page for streaming DMA
  105. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  106. * @page: page that buffer resides in
  107. * @offset: offset into page for start of buffer
  108. * @size: size of buffer to map
  109. * @dir: DMA transfer direction
  110. *
  111. * Ensure that any data held in the cache is appropriately discarded
  112. * or written back.
  113. *
  114. * The device owns this memory once this call has completed. The CPU
  115. * can regain ownership by calling dma_unmap_page().
  116. */
  117. static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
  118. unsigned long offset, size_t size, enum dma_data_direction dir,
  119. unsigned long attrs)
  120. {
  121. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  122. __dma_page_cpu_to_dev(page, offset, size, dir);
  123. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  124. }
  125. static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
  126. unsigned long offset, size_t size, enum dma_data_direction dir,
  127. unsigned long attrs)
  128. {
  129. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  130. }
  131. /**
  132. * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  133. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  134. * @handle: DMA address of buffer
  135. * @size: size of buffer (same as passed to dma_map_page)
  136. * @dir: DMA transfer direction (same as passed to dma_map_page)
  137. *
  138. * Unmap a page streaming mode DMA translation. The handle and size
  139. * must match what was provided in the previous dma_map_page() call.
  140. * All other usages are undefined.
  141. *
  142. * After this call, reads by the CPU to the buffer are guaranteed to see
  143. * whatever the device wrote there.
  144. */
  145. static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
  146. size_t size, enum dma_data_direction dir, unsigned long attrs)
  147. {
  148. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  149. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
  150. handle & ~PAGE_MASK, size, dir);
  151. }
  152. static void arm_dma_sync_single_for_cpu(struct device *dev,
  153. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  154. {
  155. unsigned int offset = handle & (PAGE_SIZE - 1);
  156. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  157. __dma_page_dev_to_cpu(page, offset, size, dir);
  158. }
  159. static void arm_dma_sync_single_for_device(struct device *dev,
  160. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  161. {
  162. unsigned int offset = handle & (PAGE_SIZE - 1);
  163. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  164. __dma_page_cpu_to_dev(page, offset, size, dir);
  165. }
  166. const struct dma_map_ops arm_dma_ops = {
  167. .alloc = arm_dma_alloc,
  168. .free = arm_dma_free,
  169. .mmap = arm_dma_mmap,
  170. .get_sgtable = arm_dma_get_sgtable,
  171. .map_page = arm_dma_map_page,
  172. .unmap_page = arm_dma_unmap_page,
  173. .map_sg = arm_dma_map_sg,
  174. .unmap_sg = arm_dma_unmap_sg,
  175. .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
  176. .sync_single_for_device = arm_dma_sync_single_for_device,
  177. .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
  178. .sync_sg_for_device = arm_dma_sync_sg_for_device,
  179. };
  180. EXPORT_SYMBOL(arm_dma_ops);
  181. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  182. dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
  183. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  184. dma_addr_t handle, unsigned long attrs);
  185. static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  186. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  187. unsigned long attrs);
  188. const struct dma_map_ops arm_coherent_dma_ops = {
  189. .alloc = arm_coherent_dma_alloc,
  190. .free = arm_coherent_dma_free,
  191. .mmap = arm_coherent_dma_mmap,
  192. .get_sgtable = arm_dma_get_sgtable,
  193. .map_page = arm_coherent_dma_map_page,
  194. .map_sg = arm_dma_map_sg,
  195. };
  196. EXPORT_SYMBOL(arm_coherent_dma_ops);
  197. static int __dma_supported(struct device *dev, u64 mask, bool warn)
  198. {
  199. unsigned long max_dma_pfn;
  200. /*
  201. * If the mask allows for more memory than we can address,
  202. * and we actually have that much memory, then we must
  203. * indicate that DMA to this device is not supported.
  204. */
  205. if (sizeof(mask) != sizeof(dma_addr_t) &&
  206. mask > (dma_addr_t)~0 &&
  207. dma_to_pfn(dev, ~0) < max_pfn - 1) {
  208. if (warn) {
  209. dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
  210. mask);
  211. dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
  212. }
  213. return 0;
  214. }
  215. max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
  216. /*
  217. * Translate the device's DMA mask to a PFN limit. This
  218. * PFN number includes the page which we can DMA to.
  219. */
  220. if (dma_to_pfn(dev, mask) < max_dma_pfn) {
  221. if (warn)
  222. dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
  223. mask,
  224. dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
  225. max_dma_pfn + 1);
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static u64 get_coherent_dma_mask(struct device *dev)
  231. {
  232. u64 mask = (u64)DMA_BIT_MASK(32);
  233. if (dev) {
  234. mask = dev->coherent_dma_mask;
  235. /*
  236. * Sanity check the DMA mask - it must be non-zero, and
  237. * must be able to be satisfied by a DMA allocation.
  238. */
  239. if (mask == 0) {
  240. dev_warn(dev, "coherent DMA mask is unset\n");
  241. return 0;
  242. }
  243. if (!__dma_supported(dev, mask, true))
  244. return 0;
  245. }
  246. return mask;
  247. }
  248. static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
  249. {
  250. /*
  251. * Ensure that the allocated pages are zeroed, and that any data
  252. * lurking in the kernel direct-mapped region is invalidated.
  253. */
  254. if (PageHighMem(page)) {
  255. phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
  256. phys_addr_t end = base + size;
  257. while (size > 0) {
  258. void *ptr = kmap_atomic(page);
  259. memset(ptr, 0, PAGE_SIZE);
  260. if (coherent_flag != COHERENT)
  261. dmac_flush_range(ptr, ptr + PAGE_SIZE);
  262. kunmap_atomic(ptr);
  263. page++;
  264. size -= PAGE_SIZE;
  265. }
  266. if (coherent_flag != COHERENT)
  267. outer_flush_range(base, end);
  268. } else {
  269. void *ptr = page_address(page);
  270. memset(ptr, 0, size);
  271. if (coherent_flag != COHERENT) {
  272. dmac_flush_range(ptr, ptr + size);
  273. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  274. }
  275. }
  276. }
  277. /*
  278. * Allocate a DMA buffer for 'dev' of size 'size' using the
  279. * specified gfp mask. Note that 'size' must be page aligned.
  280. */
  281. static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
  282. gfp_t gfp, int coherent_flag)
  283. {
  284. unsigned long order = get_order(size);
  285. struct page *page, *p, *e;
  286. page = alloc_pages(gfp, order);
  287. if (!page)
  288. return NULL;
  289. /*
  290. * Now split the huge page and free the excess pages
  291. */
  292. split_page(page, order);
  293. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  294. __free_page(p);
  295. __dma_clear_buffer(page, size, coherent_flag);
  296. return page;
  297. }
  298. /*
  299. * Free a DMA buffer. 'size' must be page aligned.
  300. */
  301. static void __dma_free_buffer(struct page *page, size_t size)
  302. {
  303. struct page *e = page + (size >> PAGE_SHIFT);
  304. while (page < e) {
  305. __free_page(page);
  306. page++;
  307. }
  308. }
  309. #ifdef CONFIG_MMU
  310. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  311. pgprot_t prot, struct page **ret_page,
  312. const void *caller, bool want_vaddr,
  313. int coherent_flag, gfp_t gfp);
  314. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  315. pgprot_t prot, struct page **ret_page,
  316. const void *caller, bool want_vaddr);
  317. static void *
  318. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
  319. const void *caller)
  320. {
  321. /*
  322. * DMA allocation can be mapped to user space, so lets
  323. * set VM_USERMAP flags too.
  324. */
  325. return dma_common_contiguous_remap(page, size,
  326. VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  327. prot, caller);
  328. }
  329. static void __dma_free_remap(void *cpu_addr, size_t size)
  330. {
  331. dma_common_free_remap(cpu_addr, size,
  332. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  333. }
  334. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  335. static struct gen_pool *atomic_pool;
  336. static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
  337. static int __init early_coherent_pool(char *p)
  338. {
  339. atomic_pool_size = memparse(p, &p);
  340. return 0;
  341. }
  342. early_param("coherent_pool", early_coherent_pool);
  343. void __init init_dma_coherent_pool_size(unsigned long size)
  344. {
  345. /*
  346. * Catch any attempt to set the pool size too late.
  347. */
  348. BUG_ON(atomic_pool);
  349. /*
  350. * Set architecture specific coherent pool size only if
  351. * it has not been changed by kernel command line parameter.
  352. */
  353. if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
  354. atomic_pool_size = size;
  355. }
  356. /*
  357. * Initialise the coherent pool for atomic allocations.
  358. */
  359. static int __init atomic_pool_init(void)
  360. {
  361. pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
  362. gfp_t gfp = GFP_KERNEL | GFP_DMA;
  363. struct page *page;
  364. void *ptr;
  365. atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
  366. if (!atomic_pool)
  367. goto out;
  368. /*
  369. * The atomic pool is only used for non-coherent allocations
  370. * so we must pass NORMAL for coherent_flag.
  371. */
  372. if (dev_get_cma_area(NULL))
  373. ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
  374. &page, atomic_pool_init, true, NORMAL,
  375. GFP_KERNEL);
  376. else
  377. ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
  378. &page, atomic_pool_init, true);
  379. if (ptr) {
  380. int ret;
  381. ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
  382. page_to_phys(page),
  383. atomic_pool_size, -1);
  384. if (ret)
  385. goto destroy_genpool;
  386. gen_pool_set_algo(atomic_pool,
  387. gen_pool_first_fit_order_align,
  388. (void *)PAGE_SHIFT);
  389. pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
  390. atomic_pool_size / 1024);
  391. return 0;
  392. }
  393. destroy_genpool:
  394. gen_pool_destroy(atomic_pool);
  395. atomic_pool = NULL;
  396. out:
  397. pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
  398. atomic_pool_size / 1024);
  399. return -ENOMEM;
  400. }
  401. /*
  402. * CMA is activated by core_initcall, so we must be called after it.
  403. */
  404. postcore_initcall(atomic_pool_init);
  405. struct dma_contig_early_reserve {
  406. phys_addr_t base;
  407. unsigned long size;
  408. };
  409. static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
  410. static int dma_mmu_remap_num __initdata;
  411. void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  412. {
  413. dma_mmu_remap[dma_mmu_remap_num].base = base;
  414. dma_mmu_remap[dma_mmu_remap_num].size = size;
  415. dma_mmu_remap_num++;
  416. }
  417. void __init dma_contiguous_remap(void)
  418. {
  419. int i;
  420. for (i = 0; i < dma_mmu_remap_num; i++) {
  421. phys_addr_t start = dma_mmu_remap[i].base;
  422. phys_addr_t end = start + dma_mmu_remap[i].size;
  423. struct map_desc map;
  424. unsigned long addr;
  425. if (end > arm_lowmem_limit)
  426. end = arm_lowmem_limit;
  427. if (start >= end)
  428. continue;
  429. map.pfn = __phys_to_pfn(start);
  430. map.virtual = __phys_to_virt(start);
  431. map.length = end - start;
  432. map.type = MT_MEMORY_DMA_READY;
  433. /*
  434. * Clear previous low-memory mapping to ensure that the
  435. * TLB does not see any conflicting entries, then flush
  436. * the TLB of the old entries before creating new mappings.
  437. *
  438. * This ensures that any speculatively loaded TLB entries
  439. * (even though they may be rare) can not cause any problems,
  440. * and ensures that this code is architecturally compliant.
  441. */
  442. for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
  443. addr += PMD_SIZE)
  444. pmd_clear(pmd_off_k(addr));
  445. flush_tlb_kernel_range(__phys_to_virt(start),
  446. __phys_to_virt(end));
  447. iotable_init(&map, 1);
  448. }
  449. }
  450. static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
  451. void *data)
  452. {
  453. struct page *page = virt_to_page(addr);
  454. pgprot_t prot = *(pgprot_t *)data;
  455. set_pte_ext(pte, mk_pte(page, prot), 0);
  456. return 0;
  457. }
  458. static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
  459. {
  460. unsigned long start = (unsigned long) page_address(page);
  461. unsigned end = start + size;
  462. apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
  463. flush_tlb_kernel_range(start, end);
  464. }
  465. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  466. pgprot_t prot, struct page **ret_page,
  467. const void *caller, bool want_vaddr)
  468. {
  469. struct page *page;
  470. void *ptr = NULL;
  471. /*
  472. * __alloc_remap_buffer is only called when the device is
  473. * non-coherent
  474. */
  475. page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
  476. if (!page)
  477. return NULL;
  478. if (!want_vaddr)
  479. goto out;
  480. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  481. if (!ptr) {
  482. __dma_free_buffer(page, size);
  483. return NULL;
  484. }
  485. out:
  486. *ret_page = page;
  487. return ptr;
  488. }
  489. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  490. {
  491. unsigned long val;
  492. void *ptr = NULL;
  493. if (!atomic_pool) {
  494. WARN(1, "coherent pool not initialised!\n");
  495. return NULL;
  496. }
  497. val = gen_pool_alloc(atomic_pool, size);
  498. if (val) {
  499. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  500. *ret_page = phys_to_page(phys);
  501. ptr = (void *)val;
  502. }
  503. return ptr;
  504. }
  505. static bool __in_atomic_pool(void *start, size_t size)
  506. {
  507. return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
  508. }
  509. static int __free_from_pool(void *start, size_t size)
  510. {
  511. if (!__in_atomic_pool(start, size))
  512. return 0;
  513. gen_pool_free(atomic_pool, (unsigned long)start, size);
  514. return 1;
  515. }
  516. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  517. pgprot_t prot, struct page **ret_page,
  518. const void *caller, bool want_vaddr,
  519. int coherent_flag, gfp_t gfp)
  520. {
  521. unsigned long order = get_order(size);
  522. size_t count = size >> PAGE_SHIFT;
  523. struct page *page;
  524. void *ptr = NULL;
  525. page = dma_alloc_from_contiguous(dev, count, order, gfp);
  526. if (!page)
  527. return NULL;
  528. __dma_clear_buffer(page, size, coherent_flag);
  529. if (!want_vaddr)
  530. goto out;
  531. if (PageHighMem(page)) {
  532. ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
  533. if (!ptr) {
  534. dma_release_from_contiguous(dev, page, count);
  535. return NULL;
  536. }
  537. } else {
  538. __dma_remap(page, size, prot);
  539. ptr = page_address(page);
  540. }
  541. out:
  542. *ret_page = page;
  543. return ptr;
  544. }
  545. static void __free_from_contiguous(struct device *dev, struct page *page,
  546. void *cpu_addr, size_t size, bool want_vaddr)
  547. {
  548. if (want_vaddr) {
  549. if (PageHighMem(page))
  550. __dma_free_remap(cpu_addr, size);
  551. else
  552. __dma_remap(page, size, PAGE_KERNEL);
  553. }
  554. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  555. }
  556. static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
  557. {
  558. prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
  559. pgprot_writecombine(prot) :
  560. pgprot_dmacoherent(prot);
  561. return prot;
  562. }
  563. #define nommu() 0
  564. #else /* !CONFIG_MMU */
  565. #define nommu() 1
  566. #define __get_dma_pgprot(attrs, prot) __pgprot(0)
  567. #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
  568. #define __alloc_from_pool(size, ret_page) NULL
  569. #define __alloc_from_contiguous(dev, size, prot, ret, c, wv, coherent_flag, gfp) NULL
  570. #define __free_from_pool(cpu_addr, size) do { } while (0)
  571. #define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
  572. #define __dma_free_remap(cpu_addr, size) do { } while (0)
  573. #endif /* CONFIG_MMU */
  574. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  575. struct page **ret_page)
  576. {
  577. struct page *page;
  578. /* __alloc_simple_buffer is only called when the device is coherent */
  579. page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
  580. if (!page)
  581. return NULL;
  582. *ret_page = page;
  583. return page_address(page);
  584. }
  585. static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
  586. struct page **ret_page)
  587. {
  588. return __alloc_simple_buffer(args->dev, args->size, args->gfp,
  589. ret_page);
  590. }
  591. static void simple_allocator_free(struct arm_dma_free_args *args)
  592. {
  593. __dma_free_buffer(args->page, args->size);
  594. }
  595. static struct arm_dma_allocator simple_allocator = {
  596. .alloc = simple_allocator_alloc,
  597. .free = simple_allocator_free,
  598. };
  599. static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
  600. struct page **ret_page)
  601. {
  602. return __alloc_from_contiguous(args->dev, args->size, args->prot,
  603. ret_page, args->caller,
  604. args->want_vaddr, args->coherent_flag,
  605. args->gfp);
  606. }
  607. static void cma_allocator_free(struct arm_dma_free_args *args)
  608. {
  609. __free_from_contiguous(args->dev, args->page, args->cpu_addr,
  610. args->size, args->want_vaddr);
  611. }
  612. static struct arm_dma_allocator cma_allocator = {
  613. .alloc = cma_allocator_alloc,
  614. .free = cma_allocator_free,
  615. };
  616. static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
  617. struct page **ret_page)
  618. {
  619. return __alloc_from_pool(args->size, ret_page);
  620. }
  621. static void pool_allocator_free(struct arm_dma_free_args *args)
  622. {
  623. __free_from_pool(args->cpu_addr, args->size);
  624. }
  625. static struct arm_dma_allocator pool_allocator = {
  626. .alloc = pool_allocator_alloc,
  627. .free = pool_allocator_free,
  628. };
  629. static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
  630. struct page **ret_page)
  631. {
  632. return __alloc_remap_buffer(args->dev, args->size, args->gfp,
  633. args->prot, ret_page, args->caller,
  634. args->want_vaddr);
  635. }
  636. static void remap_allocator_free(struct arm_dma_free_args *args)
  637. {
  638. if (args->want_vaddr)
  639. __dma_free_remap(args->cpu_addr, args->size);
  640. __dma_free_buffer(args->page, args->size);
  641. }
  642. static struct arm_dma_allocator remap_allocator = {
  643. .alloc = remap_allocator_alloc,
  644. .free = remap_allocator_free,
  645. };
  646. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  647. gfp_t gfp, pgprot_t prot, bool is_coherent,
  648. unsigned long attrs, const void *caller)
  649. {
  650. u64 mask = get_coherent_dma_mask(dev);
  651. struct page *page = NULL;
  652. void *addr;
  653. bool allowblock, cma;
  654. struct arm_dma_buffer *buf;
  655. struct arm_dma_alloc_args args = {
  656. .dev = dev,
  657. .size = PAGE_ALIGN(size),
  658. .gfp = gfp,
  659. .prot = prot,
  660. .caller = caller,
  661. .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
  662. .coherent_flag = is_coherent ? COHERENT : NORMAL,
  663. };
  664. #ifdef CONFIG_DMA_API_DEBUG
  665. u64 limit = (mask + 1) & ~mask;
  666. if (limit && size >= limit) {
  667. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  668. size, mask);
  669. return NULL;
  670. }
  671. #endif
  672. if (!mask)
  673. return NULL;
  674. buf = kzalloc(sizeof(*buf),
  675. gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
  676. if (!buf)
  677. return NULL;
  678. if (mask < 0xffffffffULL)
  679. gfp |= GFP_DMA;
  680. /*
  681. * Following is a work-around (a.k.a. hack) to prevent pages
  682. * with __GFP_COMP being passed to split_page() which cannot
  683. * handle them. The real problem is that this flag probably
  684. * should be 0 on ARM as it is not supported on this
  685. * platform; see CONFIG_HUGETLBFS.
  686. */
  687. gfp &= ~(__GFP_COMP);
  688. args.gfp = gfp;
  689. *handle = DMA_ERROR_CODE;
  690. allowblock = gfpflags_allow_blocking(gfp);
  691. cma = allowblock ? dev_get_cma_area(dev) : false;
  692. if (cma)
  693. buf->allocator = &cma_allocator;
  694. else if (nommu() || is_coherent)
  695. buf->allocator = &simple_allocator;
  696. else if (allowblock)
  697. buf->allocator = &remap_allocator;
  698. else
  699. buf->allocator = &pool_allocator;
  700. addr = buf->allocator->alloc(&args, &page);
  701. if (page) {
  702. unsigned long flags;
  703. *handle = pfn_to_dma(dev, page_to_pfn(page));
  704. buf->virt = args.want_vaddr ? addr : page;
  705. spin_lock_irqsave(&arm_dma_bufs_lock, flags);
  706. list_add(&buf->list, &arm_dma_bufs);
  707. spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
  708. } else {
  709. kfree(buf);
  710. }
  711. return args.want_vaddr ? addr : page;
  712. }
  713. /*
  714. * Allocate DMA-coherent memory space and return both the kernel remapped
  715. * virtual and bus address for that space.
  716. */
  717. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  718. gfp_t gfp, unsigned long attrs)
  719. {
  720. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  721. return __dma_alloc(dev, size, handle, gfp, prot, false,
  722. attrs, __builtin_return_address(0));
  723. }
  724. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  725. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  726. {
  727. return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
  728. attrs, __builtin_return_address(0));
  729. }
  730. static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  731. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  732. unsigned long attrs)
  733. {
  734. int ret = -ENXIO;
  735. #ifdef CONFIG_MMU
  736. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  737. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  738. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  739. unsigned long off = vma->vm_pgoff;
  740. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  741. return ret;
  742. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  743. ret = remap_pfn_range(vma, vma->vm_start,
  744. pfn + off,
  745. vma->vm_end - vma->vm_start,
  746. vma->vm_page_prot);
  747. }
  748. #else
  749. ret = vm_iomap_memory(vma, vma->vm_start,
  750. (vma->vm_end - vma->vm_start));
  751. #endif /* CONFIG_MMU */
  752. return ret;
  753. }
  754. /*
  755. * Create userspace mapping for the DMA-coherent memory.
  756. */
  757. static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  758. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  759. unsigned long attrs)
  760. {
  761. return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  762. }
  763. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  764. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  765. unsigned long attrs)
  766. {
  767. #ifdef CONFIG_MMU
  768. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  769. #endif /* CONFIG_MMU */
  770. return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  771. }
  772. /*
  773. * Free a buffer as defined by the above mapping.
  774. */
  775. static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  776. dma_addr_t handle, unsigned long attrs,
  777. bool is_coherent)
  778. {
  779. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  780. struct arm_dma_buffer *buf;
  781. struct arm_dma_free_args args = {
  782. .dev = dev,
  783. .size = PAGE_ALIGN(size),
  784. .cpu_addr = cpu_addr,
  785. .page = page,
  786. .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
  787. };
  788. buf = arm_dma_buffer_find(cpu_addr);
  789. if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
  790. return;
  791. buf->allocator->free(&args);
  792. kfree(buf);
  793. }
  794. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  795. dma_addr_t handle, unsigned long attrs)
  796. {
  797. __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
  798. }
  799. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  800. dma_addr_t handle, unsigned long attrs)
  801. {
  802. __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
  803. }
  804. /*
  805. * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
  806. * that the intention is to allow exporting memory allocated via the
  807. * coherent DMA APIs through the dma_buf API, which only accepts a
  808. * scattertable. This presents a couple of problems:
  809. * 1. Not all memory allocated via the coherent DMA APIs is backed by
  810. * a struct page
  811. * 2. Passing coherent DMA memory into the streaming APIs is not allowed
  812. * as we will try to flush the memory through a different alias to that
  813. * actually being used (and the flushes are redundant.)
  814. */
  815. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  816. void *cpu_addr, dma_addr_t handle, size_t size,
  817. unsigned long attrs)
  818. {
  819. unsigned long pfn = dma_to_pfn(dev, handle);
  820. struct page *page;
  821. int ret;
  822. /* If the PFN is not valid, we do not have a struct page */
  823. if (!pfn_valid(pfn))
  824. return -ENXIO;
  825. page = pfn_to_page(pfn);
  826. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  827. if (unlikely(ret))
  828. return ret;
  829. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  830. return 0;
  831. }
  832. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  833. size_t size, enum dma_data_direction dir,
  834. void (*op)(const void *, size_t, int))
  835. {
  836. unsigned long pfn;
  837. size_t left = size;
  838. pfn = page_to_pfn(page) + offset / PAGE_SIZE;
  839. offset %= PAGE_SIZE;
  840. /*
  841. * A single sg entry may refer to multiple physically contiguous
  842. * pages. But we still need to process highmem pages individually.
  843. * If highmem is not configured then the bulk of this loop gets
  844. * optimized out.
  845. */
  846. do {
  847. size_t len = left;
  848. void *vaddr;
  849. page = pfn_to_page(pfn);
  850. if (PageHighMem(page)) {
  851. if (len + offset > PAGE_SIZE)
  852. len = PAGE_SIZE - offset;
  853. if (cache_is_vipt_nonaliasing()) {
  854. vaddr = kmap_atomic(page);
  855. op(vaddr + offset, len, dir);
  856. kunmap_atomic(vaddr);
  857. } else {
  858. vaddr = kmap_high_get(page);
  859. if (vaddr) {
  860. op(vaddr + offset, len, dir);
  861. kunmap_high(page);
  862. }
  863. }
  864. } else {
  865. vaddr = page_address(page) + offset;
  866. op(vaddr, len, dir);
  867. }
  868. offset = 0;
  869. pfn++;
  870. left -= len;
  871. } while (left);
  872. }
  873. /*
  874. * Make an area consistent for devices.
  875. * Note: Drivers should NOT use this function directly, as it will break
  876. * platforms with CONFIG_DMABOUNCE.
  877. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  878. */
  879. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  880. size_t size, enum dma_data_direction dir)
  881. {
  882. phys_addr_t paddr;
  883. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  884. paddr = page_to_phys(page) + off;
  885. if (dir == DMA_FROM_DEVICE) {
  886. outer_inv_range(paddr, paddr + size);
  887. } else {
  888. outer_clean_range(paddr, paddr + size);
  889. }
  890. /* FIXME: non-speculating: flush on bidirectional mappings? */
  891. }
  892. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  893. size_t size, enum dma_data_direction dir)
  894. {
  895. phys_addr_t paddr = page_to_phys(page) + off;
  896. /* FIXME: non-speculating: not required */
  897. /* in any case, don't bother invalidating if DMA to device */
  898. if (dir != DMA_TO_DEVICE) {
  899. outer_inv_range(paddr, paddr + size);
  900. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  901. }
  902. /*
  903. * Mark the D-cache clean for these pages to avoid extra flushing.
  904. */
  905. if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
  906. unsigned long pfn;
  907. size_t left = size;
  908. pfn = page_to_pfn(page) + off / PAGE_SIZE;
  909. off %= PAGE_SIZE;
  910. if (off) {
  911. pfn++;
  912. left -= PAGE_SIZE - off;
  913. }
  914. while (left >= PAGE_SIZE) {
  915. page = pfn_to_page(pfn++);
  916. set_bit(PG_dcache_clean, &page->flags);
  917. left -= PAGE_SIZE;
  918. }
  919. }
  920. }
  921. /**
  922. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  923. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  924. * @sg: list of buffers
  925. * @nents: number of buffers to map
  926. * @dir: DMA transfer direction
  927. *
  928. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  929. * This is the scatter-gather version of the dma_map_single interface.
  930. * Here the scatter gather list elements are each tagged with the
  931. * appropriate dma address and length. They are obtained via
  932. * sg_dma_{address,length}.
  933. *
  934. * Device ownership issues as mentioned for dma_map_single are the same
  935. * here.
  936. */
  937. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  938. enum dma_data_direction dir, unsigned long attrs)
  939. {
  940. const struct dma_map_ops *ops = get_dma_ops(dev);
  941. struct scatterlist *s;
  942. int i, j;
  943. for_each_sg(sg, s, nents, i) {
  944. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  945. s->dma_length = s->length;
  946. #endif
  947. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  948. s->length, dir, attrs);
  949. if (dma_mapping_error(dev, s->dma_address))
  950. goto bad_mapping;
  951. }
  952. return nents;
  953. bad_mapping:
  954. for_each_sg(sg, s, i, j)
  955. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  956. return 0;
  957. }
  958. /**
  959. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  960. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  961. * @sg: list of buffers
  962. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  963. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  964. *
  965. * Unmap a set of streaming mode DMA translations. Again, CPU access
  966. * rules concerning calls here are the same as for dma_unmap_single().
  967. */
  968. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  969. enum dma_data_direction dir, unsigned long attrs)
  970. {
  971. const struct dma_map_ops *ops = get_dma_ops(dev);
  972. struct scatterlist *s;
  973. int i;
  974. for_each_sg(sg, s, nents, i)
  975. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  976. }
  977. /**
  978. * arm_dma_sync_sg_for_cpu
  979. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  980. * @sg: list of buffers
  981. * @nents: number of buffers to map (returned from dma_map_sg)
  982. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  983. */
  984. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  985. int nents, enum dma_data_direction dir)
  986. {
  987. const struct dma_map_ops *ops = get_dma_ops(dev);
  988. struct scatterlist *s;
  989. int i;
  990. for_each_sg(sg, s, nents, i)
  991. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  992. dir);
  993. }
  994. /**
  995. * arm_dma_sync_sg_for_device
  996. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  997. * @sg: list of buffers
  998. * @nents: number of buffers to map (returned from dma_map_sg)
  999. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1000. */
  1001. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1002. int nents, enum dma_data_direction dir)
  1003. {
  1004. const struct dma_map_ops *ops = get_dma_ops(dev);
  1005. struct scatterlist *s;
  1006. int i;
  1007. for_each_sg(sg, s, nents, i)
  1008. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  1009. dir);
  1010. }
  1011. /*
  1012. * Return whether the given device DMA address mask can be supported
  1013. * properly. For example, if your device can only drive the low 24-bits
  1014. * during bus mastering, then you would pass 0x00ffffff as the mask
  1015. * to this function.
  1016. */
  1017. int dma_supported(struct device *dev, u64 mask)
  1018. {
  1019. return __dma_supported(dev, mask, false);
  1020. }
  1021. EXPORT_SYMBOL(dma_supported);
  1022. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  1023. static int __init dma_debug_do_init(void)
  1024. {
  1025. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  1026. return 0;
  1027. }
  1028. core_initcall(dma_debug_do_init);
  1029. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  1030. static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
  1031. {
  1032. int prot = 0;
  1033. if (attrs & DMA_ATTR_PRIVILEGED)
  1034. prot |= IOMMU_PRIV;
  1035. switch (dir) {
  1036. case DMA_BIDIRECTIONAL:
  1037. return prot | IOMMU_READ | IOMMU_WRITE;
  1038. case DMA_TO_DEVICE:
  1039. return prot | IOMMU_READ;
  1040. case DMA_FROM_DEVICE:
  1041. return prot | IOMMU_WRITE;
  1042. default:
  1043. return prot;
  1044. }
  1045. }
  1046. /* IOMMU */
  1047. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
  1048. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  1049. size_t size)
  1050. {
  1051. unsigned int order = get_order(size);
  1052. unsigned int align = 0;
  1053. unsigned int count, start;
  1054. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1055. unsigned long flags;
  1056. dma_addr_t iova;
  1057. int i;
  1058. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  1059. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  1060. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1061. align = (1 << order) - 1;
  1062. spin_lock_irqsave(&mapping->lock, flags);
  1063. for (i = 0; i < mapping->nr_bitmaps; i++) {
  1064. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1065. mapping->bits, 0, count, align);
  1066. if (start > mapping->bits)
  1067. continue;
  1068. bitmap_set(mapping->bitmaps[i], start, count);
  1069. break;
  1070. }
  1071. /*
  1072. * No unused range found. Try to extend the existing mapping
  1073. * and perform a second attempt to reserve an IO virtual
  1074. * address range of size bytes.
  1075. */
  1076. if (i == mapping->nr_bitmaps) {
  1077. if (extend_iommu_mapping(mapping)) {
  1078. spin_unlock_irqrestore(&mapping->lock, flags);
  1079. return DMA_ERROR_CODE;
  1080. }
  1081. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1082. mapping->bits, 0, count, align);
  1083. if (start > mapping->bits) {
  1084. spin_unlock_irqrestore(&mapping->lock, flags);
  1085. return DMA_ERROR_CODE;
  1086. }
  1087. bitmap_set(mapping->bitmaps[i], start, count);
  1088. }
  1089. spin_unlock_irqrestore(&mapping->lock, flags);
  1090. iova = mapping->base + (mapping_size * i);
  1091. iova += start << PAGE_SHIFT;
  1092. return iova;
  1093. }
  1094. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  1095. dma_addr_t addr, size_t size)
  1096. {
  1097. unsigned int start, count;
  1098. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1099. unsigned long flags;
  1100. dma_addr_t bitmap_base;
  1101. u32 bitmap_index;
  1102. if (!size)
  1103. return;
  1104. bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
  1105. BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
  1106. bitmap_base = mapping->base + mapping_size * bitmap_index;
  1107. start = (addr - bitmap_base) >> PAGE_SHIFT;
  1108. if (addr + size > bitmap_base + mapping_size) {
  1109. /*
  1110. * The address range to be freed reaches into the iova
  1111. * range of the next bitmap. This should not happen as
  1112. * we don't allow this in __alloc_iova (at the
  1113. * moment).
  1114. */
  1115. BUG();
  1116. } else
  1117. count = size >> PAGE_SHIFT;
  1118. spin_lock_irqsave(&mapping->lock, flags);
  1119. bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
  1120. spin_unlock_irqrestore(&mapping->lock, flags);
  1121. }
  1122. /* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
  1123. static const int iommu_order_array[] = { 9, 8, 4, 0 };
  1124. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  1125. gfp_t gfp, unsigned long attrs,
  1126. int coherent_flag)
  1127. {
  1128. struct page **pages;
  1129. int count = size >> PAGE_SHIFT;
  1130. int array_size = count * sizeof(struct page *);
  1131. int i = 0;
  1132. int order_idx = 0;
  1133. if (array_size <= PAGE_SIZE)
  1134. pages = kzalloc(array_size, GFP_KERNEL);
  1135. else
  1136. pages = vzalloc(array_size);
  1137. if (!pages)
  1138. return NULL;
  1139. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
  1140. {
  1141. unsigned long order = get_order(size);
  1142. struct page *page;
  1143. page = dma_alloc_from_contiguous(dev, count, order, gfp);
  1144. if (!page)
  1145. goto error;
  1146. __dma_clear_buffer(page, size, coherent_flag);
  1147. for (i = 0; i < count; i++)
  1148. pages[i] = page + i;
  1149. return pages;
  1150. }
  1151. /* Go straight to 4K chunks if caller says it's OK. */
  1152. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  1153. order_idx = ARRAY_SIZE(iommu_order_array) - 1;
  1154. /*
  1155. * IOMMU can map any pages, so himem can also be used here
  1156. */
  1157. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  1158. while (count) {
  1159. int j, order;
  1160. order = iommu_order_array[order_idx];
  1161. /* Drop down when we get small */
  1162. if (__fls(count) < order) {
  1163. order_idx++;
  1164. continue;
  1165. }
  1166. if (order) {
  1167. /* See if it's easy to allocate a high-order chunk */
  1168. pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
  1169. /* Go down a notch at first sign of pressure */
  1170. if (!pages[i]) {
  1171. order_idx++;
  1172. continue;
  1173. }
  1174. } else {
  1175. pages[i] = alloc_pages(gfp, 0);
  1176. if (!pages[i])
  1177. goto error;
  1178. }
  1179. if (order) {
  1180. split_page(pages[i], order);
  1181. j = 1 << order;
  1182. while (--j)
  1183. pages[i + j] = pages[i] + j;
  1184. }
  1185. __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
  1186. i += 1 << order;
  1187. count -= 1 << order;
  1188. }
  1189. return pages;
  1190. error:
  1191. while (i--)
  1192. if (pages[i])
  1193. __free_pages(pages[i], 0);
  1194. kvfree(pages);
  1195. return NULL;
  1196. }
  1197. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1198. size_t size, unsigned long attrs)
  1199. {
  1200. int count = size >> PAGE_SHIFT;
  1201. int i;
  1202. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  1203. dma_release_from_contiguous(dev, pages[0], count);
  1204. } else {
  1205. for (i = 0; i < count; i++)
  1206. if (pages[i])
  1207. __free_pages(pages[i], 0);
  1208. }
  1209. kvfree(pages);
  1210. return 0;
  1211. }
  1212. /*
  1213. * Create a CPU mapping for a specified pages
  1214. */
  1215. static void *
  1216. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1217. const void *caller)
  1218. {
  1219. return dma_common_pages_remap(pages, size,
  1220. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1221. }
  1222. /*
  1223. * Create a mapping in device IO address space for specified pages
  1224. */
  1225. static dma_addr_t
  1226. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
  1227. unsigned long attrs)
  1228. {
  1229. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1230. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1231. dma_addr_t dma_addr, iova;
  1232. int i;
  1233. dma_addr = __alloc_iova(mapping, size);
  1234. if (dma_addr == DMA_ERROR_CODE)
  1235. return dma_addr;
  1236. iova = dma_addr;
  1237. for (i = 0; i < count; ) {
  1238. int ret;
  1239. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1240. phys_addr_t phys = page_to_phys(pages[i]);
  1241. unsigned int len, j;
  1242. for (j = i + 1; j < count; j++, next_pfn++)
  1243. if (page_to_pfn(pages[j]) != next_pfn)
  1244. break;
  1245. len = (j - i) << PAGE_SHIFT;
  1246. ret = iommu_map(mapping->domain, iova, phys, len,
  1247. __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
  1248. if (ret < 0)
  1249. goto fail;
  1250. iova += len;
  1251. i = j;
  1252. }
  1253. return dma_addr;
  1254. fail:
  1255. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1256. __free_iova(mapping, dma_addr, size);
  1257. return DMA_ERROR_CODE;
  1258. }
  1259. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1260. {
  1261. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1262. /*
  1263. * add optional in-page offset from iova to size and align
  1264. * result to page size
  1265. */
  1266. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1267. iova &= PAGE_MASK;
  1268. iommu_unmap(mapping->domain, iova, size);
  1269. __free_iova(mapping, iova, size);
  1270. return 0;
  1271. }
  1272. static struct page **__atomic_get_pages(void *addr)
  1273. {
  1274. struct page *page;
  1275. phys_addr_t phys;
  1276. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1277. page = phys_to_page(phys);
  1278. return (struct page **)page;
  1279. }
  1280. static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
  1281. {
  1282. struct vm_struct *area;
  1283. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1284. return __atomic_get_pages(cpu_addr);
  1285. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1286. return cpu_addr;
  1287. area = find_vm_area(cpu_addr);
  1288. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1289. return area->pages;
  1290. return NULL;
  1291. }
  1292. static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
  1293. dma_addr_t *handle, int coherent_flag,
  1294. unsigned long attrs)
  1295. {
  1296. struct page *page;
  1297. void *addr;
  1298. if (coherent_flag == COHERENT)
  1299. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  1300. else
  1301. addr = __alloc_from_pool(size, &page);
  1302. if (!addr)
  1303. return NULL;
  1304. *handle = __iommu_create_mapping(dev, &page, size, attrs);
  1305. if (*handle == DMA_ERROR_CODE)
  1306. goto err_mapping;
  1307. return addr;
  1308. err_mapping:
  1309. __free_from_pool(addr, size);
  1310. return NULL;
  1311. }
  1312. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1313. dma_addr_t handle, size_t size, int coherent_flag)
  1314. {
  1315. __iommu_remove_mapping(dev, handle, size);
  1316. if (coherent_flag == COHERENT)
  1317. __dma_free_buffer(virt_to_page(cpu_addr), size);
  1318. else
  1319. __free_from_pool(cpu_addr, size);
  1320. }
  1321. static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1322. dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
  1323. int coherent_flag)
  1324. {
  1325. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1326. struct page **pages;
  1327. void *addr = NULL;
  1328. *handle = DMA_ERROR_CODE;
  1329. size = PAGE_ALIGN(size);
  1330. if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
  1331. return __iommu_alloc_simple(dev, size, gfp, handle,
  1332. coherent_flag, attrs);
  1333. /*
  1334. * Following is a work-around (a.k.a. hack) to prevent pages
  1335. * with __GFP_COMP being passed to split_page() which cannot
  1336. * handle them. The real problem is that this flag probably
  1337. * should be 0 on ARM as it is not supported on this
  1338. * platform; see CONFIG_HUGETLBFS.
  1339. */
  1340. gfp &= ~(__GFP_COMP);
  1341. pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
  1342. if (!pages)
  1343. return NULL;
  1344. *handle = __iommu_create_mapping(dev, pages, size, attrs);
  1345. if (*handle == DMA_ERROR_CODE)
  1346. goto err_buffer;
  1347. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1348. return pages;
  1349. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1350. __builtin_return_address(0));
  1351. if (!addr)
  1352. goto err_mapping;
  1353. return addr;
  1354. err_mapping:
  1355. __iommu_remove_mapping(dev, *handle, size);
  1356. err_buffer:
  1357. __iommu_free_buffer(dev, pages, size, attrs);
  1358. return NULL;
  1359. }
  1360. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1361. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1362. {
  1363. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
  1364. }
  1365. static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
  1366. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1367. {
  1368. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
  1369. }
  1370. static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1371. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1372. unsigned long attrs)
  1373. {
  1374. unsigned long uaddr = vma->vm_start;
  1375. unsigned long usize = vma->vm_end - vma->vm_start;
  1376. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1377. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1378. unsigned long off = vma->vm_pgoff;
  1379. if (!pages)
  1380. return -ENXIO;
  1381. if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
  1382. return -ENXIO;
  1383. pages += off;
  1384. do {
  1385. int ret = vm_insert_page(vma, uaddr, *pages++);
  1386. if (ret) {
  1387. pr_err("Remapping memory failed: %d\n", ret);
  1388. return ret;
  1389. }
  1390. uaddr += PAGE_SIZE;
  1391. usize -= PAGE_SIZE;
  1392. } while (usize > 0);
  1393. return 0;
  1394. }
  1395. static int arm_iommu_mmap_attrs(struct device *dev,
  1396. struct vm_area_struct *vma, void *cpu_addr,
  1397. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1398. {
  1399. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1400. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1401. }
  1402. static int arm_coherent_iommu_mmap_attrs(struct device *dev,
  1403. struct vm_area_struct *vma, void *cpu_addr,
  1404. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1405. {
  1406. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1407. }
  1408. /*
  1409. * free a page as defined by the above mapping.
  1410. * Must not be called with IRQs disabled.
  1411. */
  1412. void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1413. dma_addr_t handle, unsigned long attrs, int coherent_flag)
  1414. {
  1415. struct page **pages;
  1416. size = PAGE_ALIGN(size);
  1417. if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
  1418. __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
  1419. return;
  1420. }
  1421. pages = __iommu_get_pages(cpu_addr, attrs);
  1422. if (!pages) {
  1423. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1424. return;
  1425. }
  1426. if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
  1427. dma_common_free_remap(cpu_addr, size,
  1428. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1429. }
  1430. __iommu_remove_mapping(dev, handle, size);
  1431. __iommu_free_buffer(dev, pages, size, attrs);
  1432. }
  1433. void arm_iommu_free_attrs(struct device *dev, size_t size,
  1434. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1435. {
  1436. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
  1437. }
  1438. void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
  1439. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1440. {
  1441. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
  1442. }
  1443. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1444. void *cpu_addr, dma_addr_t dma_addr,
  1445. size_t size, unsigned long attrs)
  1446. {
  1447. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1448. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1449. if (!pages)
  1450. return -ENXIO;
  1451. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1452. GFP_KERNEL);
  1453. }
  1454. /*
  1455. * Map a part of the scatter-gather list into contiguous io address space
  1456. */
  1457. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1458. size_t size, dma_addr_t *handle,
  1459. enum dma_data_direction dir, unsigned long attrs,
  1460. bool is_coherent)
  1461. {
  1462. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1463. dma_addr_t iova, iova_base;
  1464. int ret = 0;
  1465. unsigned int count;
  1466. struct scatterlist *s;
  1467. int prot;
  1468. size = PAGE_ALIGN(size);
  1469. *handle = DMA_ERROR_CODE;
  1470. iova_base = iova = __alloc_iova(mapping, size);
  1471. if (iova == DMA_ERROR_CODE)
  1472. return -ENOMEM;
  1473. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1474. phys_addr_t phys = page_to_phys(sg_page(s));
  1475. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1476. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1477. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1478. prot = __dma_info_to_prot(dir, attrs);
  1479. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1480. if (ret < 0)
  1481. goto fail;
  1482. count += len >> PAGE_SHIFT;
  1483. iova += len;
  1484. }
  1485. *handle = iova_base;
  1486. return 0;
  1487. fail:
  1488. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1489. __free_iova(mapping, iova_base, size);
  1490. return ret;
  1491. }
  1492. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1493. enum dma_data_direction dir, unsigned long attrs,
  1494. bool is_coherent)
  1495. {
  1496. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1497. int i, count = 0;
  1498. unsigned int offset = s->offset;
  1499. unsigned int size = s->offset + s->length;
  1500. unsigned int max = dma_get_max_seg_size(dev);
  1501. for (i = 1; i < nents; i++) {
  1502. s = sg_next(s);
  1503. s->dma_address = DMA_ERROR_CODE;
  1504. s->dma_length = 0;
  1505. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1506. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1507. dir, attrs, is_coherent) < 0)
  1508. goto bad_mapping;
  1509. dma->dma_address += offset;
  1510. dma->dma_length = size - offset;
  1511. size = offset = s->offset;
  1512. start = s;
  1513. dma = sg_next(dma);
  1514. count += 1;
  1515. }
  1516. size += s->length;
  1517. }
  1518. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1519. is_coherent) < 0)
  1520. goto bad_mapping;
  1521. dma->dma_address += offset;
  1522. dma->dma_length = size - offset;
  1523. return count+1;
  1524. bad_mapping:
  1525. for_each_sg(sg, s, count, i)
  1526. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1527. return 0;
  1528. }
  1529. /**
  1530. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1531. * @dev: valid struct device pointer
  1532. * @sg: list of buffers
  1533. * @nents: number of buffers to map
  1534. * @dir: DMA transfer direction
  1535. *
  1536. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1537. * mode for DMA. The scatter gather list elements are merged together (if
  1538. * possible) and tagged with the appropriate dma address and length. They are
  1539. * obtained via sg_dma_{address,length}.
  1540. */
  1541. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1542. int nents, enum dma_data_direction dir, unsigned long attrs)
  1543. {
  1544. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1545. }
  1546. /**
  1547. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1548. * @dev: valid struct device pointer
  1549. * @sg: list of buffers
  1550. * @nents: number of buffers to map
  1551. * @dir: DMA transfer direction
  1552. *
  1553. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1554. * The scatter gather list elements are merged together (if possible) and
  1555. * tagged with the appropriate dma address and length. They are obtained via
  1556. * sg_dma_{address,length}.
  1557. */
  1558. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1559. int nents, enum dma_data_direction dir, unsigned long attrs)
  1560. {
  1561. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1562. }
  1563. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1564. int nents, enum dma_data_direction dir,
  1565. unsigned long attrs, bool is_coherent)
  1566. {
  1567. struct scatterlist *s;
  1568. int i;
  1569. for_each_sg(sg, s, nents, i) {
  1570. if (sg_dma_len(s))
  1571. __iommu_remove_mapping(dev, sg_dma_address(s),
  1572. sg_dma_len(s));
  1573. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1574. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1575. s->length, dir);
  1576. }
  1577. }
  1578. /**
  1579. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1580. * @dev: valid struct device pointer
  1581. * @sg: list of buffers
  1582. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1583. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1584. *
  1585. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1586. * rules concerning calls here are the same as for dma_unmap_single().
  1587. */
  1588. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1589. int nents, enum dma_data_direction dir,
  1590. unsigned long attrs)
  1591. {
  1592. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1593. }
  1594. /**
  1595. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1596. * @dev: valid struct device pointer
  1597. * @sg: list of buffers
  1598. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1599. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1600. *
  1601. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1602. * rules concerning calls here are the same as for dma_unmap_single().
  1603. */
  1604. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1605. enum dma_data_direction dir,
  1606. unsigned long attrs)
  1607. {
  1608. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1609. }
  1610. /**
  1611. * arm_iommu_sync_sg_for_cpu
  1612. * @dev: valid struct device pointer
  1613. * @sg: list of buffers
  1614. * @nents: number of buffers to map (returned from dma_map_sg)
  1615. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1616. */
  1617. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1618. int nents, enum dma_data_direction dir)
  1619. {
  1620. struct scatterlist *s;
  1621. int i;
  1622. for_each_sg(sg, s, nents, i)
  1623. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1624. }
  1625. /**
  1626. * arm_iommu_sync_sg_for_device
  1627. * @dev: valid struct device pointer
  1628. * @sg: list of buffers
  1629. * @nents: number of buffers to map (returned from dma_map_sg)
  1630. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1631. */
  1632. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1633. int nents, enum dma_data_direction dir)
  1634. {
  1635. struct scatterlist *s;
  1636. int i;
  1637. for_each_sg(sg, s, nents, i)
  1638. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1639. }
  1640. /**
  1641. * arm_coherent_iommu_map_page
  1642. * @dev: valid struct device pointer
  1643. * @page: page that buffer resides in
  1644. * @offset: offset into page for start of buffer
  1645. * @size: size of buffer to map
  1646. * @dir: DMA transfer direction
  1647. *
  1648. * Coherent IOMMU aware version of arm_dma_map_page()
  1649. */
  1650. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1651. unsigned long offset, size_t size, enum dma_data_direction dir,
  1652. unsigned long attrs)
  1653. {
  1654. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1655. dma_addr_t dma_addr;
  1656. int ret, prot, len = PAGE_ALIGN(size + offset);
  1657. dma_addr = __alloc_iova(mapping, len);
  1658. if (dma_addr == DMA_ERROR_CODE)
  1659. return dma_addr;
  1660. prot = __dma_info_to_prot(dir, attrs);
  1661. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1662. if (ret < 0)
  1663. goto fail;
  1664. return dma_addr + offset;
  1665. fail:
  1666. __free_iova(mapping, dma_addr, len);
  1667. return DMA_ERROR_CODE;
  1668. }
  1669. /**
  1670. * arm_iommu_map_page
  1671. * @dev: valid struct device pointer
  1672. * @page: page that buffer resides in
  1673. * @offset: offset into page for start of buffer
  1674. * @size: size of buffer to map
  1675. * @dir: DMA transfer direction
  1676. *
  1677. * IOMMU aware version of arm_dma_map_page()
  1678. */
  1679. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1680. unsigned long offset, size_t size, enum dma_data_direction dir,
  1681. unsigned long attrs)
  1682. {
  1683. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1684. __dma_page_cpu_to_dev(page, offset, size, dir);
  1685. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1686. }
  1687. /**
  1688. * arm_coherent_iommu_unmap_page
  1689. * @dev: valid struct device pointer
  1690. * @handle: DMA address of buffer
  1691. * @size: size of buffer (same as passed to dma_map_page)
  1692. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1693. *
  1694. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1695. */
  1696. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1697. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1698. {
  1699. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1700. dma_addr_t iova = handle & PAGE_MASK;
  1701. int offset = handle & ~PAGE_MASK;
  1702. int len = PAGE_ALIGN(size + offset);
  1703. if (!iova)
  1704. return;
  1705. iommu_unmap(mapping->domain, iova, len);
  1706. __free_iova(mapping, iova, len);
  1707. }
  1708. /**
  1709. * arm_iommu_unmap_page
  1710. * @dev: valid struct device pointer
  1711. * @handle: DMA address of buffer
  1712. * @size: size of buffer (same as passed to dma_map_page)
  1713. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1714. *
  1715. * IOMMU aware version of arm_dma_unmap_page()
  1716. */
  1717. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1718. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1719. {
  1720. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1721. dma_addr_t iova = handle & PAGE_MASK;
  1722. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1723. int offset = handle & ~PAGE_MASK;
  1724. int len = PAGE_ALIGN(size + offset);
  1725. if (!iova)
  1726. return;
  1727. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1728. __dma_page_dev_to_cpu(page, offset, size, dir);
  1729. iommu_unmap(mapping->domain, iova, len);
  1730. __free_iova(mapping, iova, len);
  1731. }
  1732. /**
  1733. * arm_iommu_map_resource - map a device resource for DMA
  1734. * @dev: valid struct device pointer
  1735. * @phys_addr: physical address of resource
  1736. * @size: size of resource to map
  1737. * @dir: DMA transfer direction
  1738. */
  1739. static dma_addr_t arm_iommu_map_resource(struct device *dev,
  1740. phys_addr_t phys_addr, size_t size,
  1741. enum dma_data_direction dir, unsigned long attrs)
  1742. {
  1743. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1744. dma_addr_t dma_addr;
  1745. int ret, prot;
  1746. phys_addr_t addr = phys_addr & PAGE_MASK;
  1747. unsigned int offset = phys_addr & ~PAGE_MASK;
  1748. size_t len = PAGE_ALIGN(size + offset);
  1749. dma_addr = __alloc_iova(mapping, len);
  1750. if (dma_addr == DMA_ERROR_CODE)
  1751. return dma_addr;
  1752. prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
  1753. ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
  1754. if (ret < 0)
  1755. goto fail;
  1756. return dma_addr + offset;
  1757. fail:
  1758. __free_iova(mapping, dma_addr, len);
  1759. return DMA_ERROR_CODE;
  1760. }
  1761. /**
  1762. * arm_iommu_unmap_resource - unmap a device DMA resource
  1763. * @dev: valid struct device pointer
  1764. * @dma_handle: DMA address to resource
  1765. * @size: size of resource to map
  1766. * @dir: DMA transfer direction
  1767. */
  1768. static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
  1769. size_t size, enum dma_data_direction dir,
  1770. unsigned long attrs)
  1771. {
  1772. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1773. dma_addr_t iova = dma_handle & PAGE_MASK;
  1774. unsigned int offset = dma_handle & ~PAGE_MASK;
  1775. size_t len = PAGE_ALIGN(size + offset);
  1776. if (!iova)
  1777. return;
  1778. iommu_unmap(mapping->domain, iova, len);
  1779. __free_iova(mapping, iova, len);
  1780. }
  1781. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1782. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1783. {
  1784. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1785. dma_addr_t iova = handle & PAGE_MASK;
  1786. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1787. unsigned int offset = handle & ~PAGE_MASK;
  1788. if (!iova)
  1789. return;
  1790. __dma_page_dev_to_cpu(page, offset, size, dir);
  1791. }
  1792. static void arm_iommu_sync_single_for_device(struct device *dev,
  1793. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1794. {
  1795. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1796. dma_addr_t iova = handle & PAGE_MASK;
  1797. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1798. unsigned int offset = handle & ~PAGE_MASK;
  1799. if (!iova)
  1800. return;
  1801. __dma_page_cpu_to_dev(page, offset, size, dir);
  1802. }
  1803. const struct dma_map_ops iommu_ops = {
  1804. .alloc = arm_iommu_alloc_attrs,
  1805. .free = arm_iommu_free_attrs,
  1806. .mmap = arm_iommu_mmap_attrs,
  1807. .get_sgtable = arm_iommu_get_sgtable,
  1808. .map_page = arm_iommu_map_page,
  1809. .unmap_page = arm_iommu_unmap_page,
  1810. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1811. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1812. .map_sg = arm_iommu_map_sg,
  1813. .unmap_sg = arm_iommu_unmap_sg,
  1814. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1815. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1816. .map_resource = arm_iommu_map_resource,
  1817. .unmap_resource = arm_iommu_unmap_resource,
  1818. };
  1819. const struct dma_map_ops iommu_coherent_ops = {
  1820. .alloc = arm_coherent_iommu_alloc_attrs,
  1821. .free = arm_coherent_iommu_free_attrs,
  1822. .mmap = arm_coherent_iommu_mmap_attrs,
  1823. .get_sgtable = arm_iommu_get_sgtable,
  1824. .map_page = arm_coherent_iommu_map_page,
  1825. .unmap_page = arm_coherent_iommu_unmap_page,
  1826. .map_sg = arm_coherent_iommu_map_sg,
  1827. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1828. .map_resource = arm_iommu_map_resource,
  1829. .unmap_resource = arm_iommu_unmap_resource,
  1830. };
  1831. /**
  1832. * arm_iommu_create_mapping
  1833. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1834. * @base: start address of the valid IO address space
  1835. * @size: maximum size of the valid IO address space
  1836. *
  1837. * Creates a mapping structure which holds information about used/unused
  1838. * IO address ranges, which is required to perform memory allocation and
  1839. * mapping with IOMMU aware functions.
  1840. *
  1841. * The client device need to be attached to the mapping with
  1842. * arm_iommu_attach_device function.
  1843. */
  1844. struct dma_iommu_mapping *
  1845. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1846. {
  1847. unsigned int bits = size >> PAGE_SHIFT;
  1848. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1849. struct dma_iommu_mapping *mapping;
  1850. int extensions = 1;
  1851. int err = -ENOMEM;
  1852. /* currently only 32-bit DMA address space is supported */
  1853. if (size > DMA_BIT_MASK(32) + 1)
  1854. return ERR_PTR(-ERANGE);
  1855. if (!bitmap_size)
  1856. return ERR_PTR(-EINVAL);
  1857. if (bitmap_size > PAGE_SIZE) {
  1858. extensions = bitmap_size / PAGE_SIZE;
  1859. bitmap_size = PAGE_SIZE;
  1860. }
  1861. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1862. if (!mapping)
  1863. goto err;
  1864. mapping->bitmap_size = bitmap_size;
  1865. mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
  1866. GFP_KERNEL);
  1867. if (!mapping->bitmaps)
  1868. goto err2;
  1869. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1870. if (!mapping->bitmaps[0])
  1871. goto err3;
  1872. mapping->nr_bitmaps = 1;
  1873. mapping->extensions = extensions;
  1874. mapping->base = base;
  1875. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1876. spin_lock_init(&mapping->lock);
  1877. mapping->domain = iommu_domain_alloc(bus);
  1878. if (!mapping->domain)
  1879. goto err4;
  1880. kref_init(&mapping->kref);
  1881. return mapping;
  1882. err4:
  1883. kfree(mapping->bitmaps[0]);
  1884. err3:
  1885. kfree(mapping->bitmaps);
  1886. err2:
  1887. kfree(mapping);
  1888. err:
  1889. return ERR_PTR(err);
  1890. }
  1891. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1892. static void release_iommu_mapping(struct kref *kref)
  1893. {
  1894. int i;
  1895. struct dma_iommu_mapping *mapping =
  1896. container_of(kref, struct dma_iommu_mapping, kref);
  1897. iommu_domain_free(mapping->domain);
  1898. for (i = 0; i < mapping->nr_bitmaps; i++)
  1899. kfree(mapping->bitmaps[i]);
  1900. kfree(mapping->bitmaps);
  1901. kfree(mapping);
  1902. }
  1903. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1904. {
  1905. int next_bitmap;
  1906. if (mapping->nr_bitmaps >= mapping->extensions)
  1907. return -EINVAL;
  1908. next_bitmap = mapping->nr_bitmaps;
  1909. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1910. GFP_ATOMIC);
  1911. if (!mapping->bitmaps[next_bitmap])
  1912. return -ENOMEM;
  1913. mapping->nr_bitmaps++;
  1914. return 0;
  1915. }
  1916. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1917. {
  1918. if (mapping)
  1919. kref_put(&mapping->kref, release_iommu_mapping);
  1920. }
  1921. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1922. static int __arm_iommu_attach_device(struct device *dev,
  1923. struct dma_iommu_mapping *mapping)
  1924. {
  1925. int err;
  1926. err = iommu_attach_device(mapping->domain, dev);
  1927. if (err)
  1928. return err;
  1929. kref_get(&mapping->kref);
  1930. to_dma_iommu_mapping(dev) = mapping;
  1931. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1932. return 0;
  1933. }
  1934. /**
  1935. * arm_iommu_attach_device
  1936. * @dev: valid struct device pointer
  1937. * @mapping: io address space mapping structure (returned from
  1938. * arm_iommu_create_mapping)
  1939. *
  1940. * Attaches specified io address space mapping to the provided device.
  1941. * This replaces the dma operations (dma_map_ops pointer) with the
  1942. * IOMMU aware version.
  1943. *
  1944. * More than one client might be attached to the same io address space
  1945. * mapping.
  1946. */
  1947. int arm_iommu_attach_device(struct device *dev,
  1948. struct dma_iommu_mapping *mapping)
  1949. {
  1950. int err;
  1951. err = __arm_iommu_attach_device(dev, mapping);
  1952. if (err)
  1953. return err;
  1954. set_dma_ops(dev, &iommu_ops);
  1955. return 0;
  1956. }
  1957. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1958. static void __arm_iommu_detach_device(struct device *dev)
  1959. {
  1960. struct dma_iommu_mapping *mapping;
  1961. mapping = to_dma_iommu_mapping(dev);
  1962. if (!mapping) {
  1963. dev_warn(dev, "Not attached\n");
  1964. return;
  1965. }
  1966. iommu_detach_device(mapping->domain, dev);
  1967. kref_put(&mapping->kref, release_iommu_mapping);
  1968. to_dma_iommu_mapping(dev) = NULL;
  1969. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1970. }
  1971. /**
  1972. * arm_iommu_detach_device
  1973. * @dev: valid struct device pointer
  1974. *
  1975. * Detaches the provided device from a previously attached map.
  1976. * This voids the dma operations (dma_map_ops pointer)
  1977. */
  1978. void arm_iommu_detach_device(struct device *dev)
  1979. {
  1980. __arm_iommu_detach_device(dev);
  1981. set_dma_ops(dev, NULL);
  1982. }
  1983. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1984. static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1985. {
  1986. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1987. }
  1988. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1989. const struct iommu_ops *iommu)
  1990. {
  1991. struct dma_iommu_mapping *mapping;
  1992. if (!iommu)
  1993. return false;
  1994. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1995. if (IS_ERR(mapping)) {
  1996. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1997. size, dev_name(dev));
  1998. return false;
  1999. }
  2000. if (__arm_iommu_attach_device(dev, mapping)) {
  2001. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  2002. dev_name(dev));
  2003. arm_iommu_release_mapping(mapping);
  2004. return false;
  2005. }
  2006. return true;
  2007. }
  2008. static void arm_teardown_iommu_dma_ops(struct device *dev)
  2009. {
  2010. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  2011. if (!mapping)
  2012. return;
  2013. __arm_iommu_detach_device(dev);
  2014. arm_iommu_release_mapping(mapping);
  2015. }
  2016. #else
  2017. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  2018. const struct iommu_ops *iommu)
  2019. {
  2020. return false;
  2021. }
  2022. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  2023. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  2024. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  2025. static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  2026. {
  2027. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  2028. }
  2029. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  2030. const struct iommu_ops *iommu, bool coherent)
  2031. {
  2032. const struct dma_map_ops *dma_ops;
  2033. dev->archdata.dma_coherent = coherent;
  2034. /*
  2035. * Don't override the dma_ops if they have already been set. Ideally
  2036. * this should be the only location where dma_ops are set, remove this
  2037. * check when all other callers of set_dma_ops will have disappeared.
  2038. */
  2039. if (dev->dma_ops)
  2040. return;
  2041. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  2042. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  2043. else
  2044. dma_ops = arm_get_dma_map_ops(coherent);
  2045. set_dma_ops(dev, dma_ops);
  2046. }
  2047. void arch_teardown_dma_ops(struct device *dev)
  2048. {
  2049. arm_teardown_iommu_dma_ops(dev);
  2050. }