dma-mapping.c 63 KB

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