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