dma-mapping.c 64 KB

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