dma-mapping.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405
  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 & __GFP_NOWARN);
  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 = -ENXIO;
  718. unsigned long nr_vma_pages = vma_pages(vma);
  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. static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  998. {
  999. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  1000. }
  1001. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  1002. static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
  1003. {
  1004. int prot = 0;
  1005. if (attrs & DMA_ATTR_PRIVILEGED)
  1006. prot |= IOMMU_PRIV;
  1007. switch (dir) {
  1008. case DMA_BIDIRECTIONAL:
  1009. return prot | IOMMU_READ | IOMMU_WRITE;
  1010. case DMA_TO_DEVICE:
  1011. return prot | IOMMU_READ;
  1012. case DMA_FROM_DEVICE:
  1013. return prot | IOMMU_WRITE;
  1014. default:
  1015. return prot;
  1016. }
  1017. }
  1018. /* IOMMU */
  1019. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
  1020. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  1021. size_t size)
  1022. {
  1023. unsigned int order = get_order(size);
  1024. unsigned int align = 0;
  1025. unsigned int count, start;
  1026. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1027. unsigned long flags;
  1028. dma_addr_t iova;
  1029. int i;
  1030. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  1031. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  1032. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1033. align = (1 << order) - 1;
  1034. spin_lock_irqsave(&mapping->lock, flags);
  1035. for (i = 0; i < mapping->nr_bitmaps; i++) {
  1036. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1037. mapping->bits, 0, count, align);
  1038. if (start > mapping->bits)
  1039. continue;
  1040. bitmap_set(mapping->bitmaps[i], start, count);
  1041. break;
  1042. }
  1043. /*
  1044. * No unused range found. Try to extend the existing mapping
  1045. * and perform a second attempt to reserve an IO virtual
  1046. * address range of size bytes.
  1047. */
  1048. if (i == mapping->nr_bitmaps) {
  1049. if (extend_iommu_mapping(mapping)) {
  1050. spin_unlock_irqrestore(&mapping->lock, flags);
  1051. return ARM_MAPPING_ERROR;
  1052. }
  1053. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1054. mapping->bits, 0, count, align);
  1055. if (start > mapping->bits) {
  1056. spin_unlock_irqrestore(&mapping->lock, flags);
  1057. return ARM_MAPPING_ERROR;
  1058. }
  1059. bitmap_set(mapping->bitmaps[i], start, count);
  1060. }
  1061. spin_unlock_irqrestore(&mapping->lock, flags);
  1062. iova = mapping->base + (mapping_size * i);
  1063. iova += start << PAGE_SHIFT;
  1064. return iova;
  1065. }
  1066. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  1067. dma_addr_t addr, size_t size)
  1068. {
  1069. unsigned int start, count;
  1070. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1071. unsigned long flags;
  1072. dma_addr_t bitmap_base;
  1073. u32 bitmap_index;
  1074. if (!size)
  1075. return;
  1076. bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
  1077. BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
  1078. bitmap_base = mapping->base + mapping_size * bitmap_index;
  1079. start = (addr - bitmap_base) >> PAGE_SHIFT;
  1080. if (addr + size > bitmap_base + mapping_size) {
  1081. /*
  1082. * The address range to be freed reaches into the iova
  1083. * range of the next bitmap. This should not happen as
  1084. * we don't allow this in __alloc_iova (at the
  1085. * moment).
  1086. */
  1087. BUG();
  1088. } else
  1089. count = size >> PAGE_SHIFT;
  1090. spin_lock_irqsave(&mapping->lock, flags);
  1091. bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
  1092. spin_unlock_irqrestore(&mapping->lock, flags);
  1093. }
  1094. /* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
  1095. static const int iommu_order_array[] = { 9, 8, 4, 0 };
  1096. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  1097. gfp_t gfp, unsigned long attrs,
  1098. int coherent_flag)
  1099. {
  1100. struct page **pages;
  1101. int count = size >> PAGE_SHIFT;
  1102. int array_size = count * sizeof(struct page *);
  1103. int i = 0;
  1104. int order_idx = 0;
  1105. if (array_size <= PAGE_SIZE)
  1106. pages = kzalloc(array_size, GFP_KERNEL);
  1107. else
  1108. pages = vzalloc(array_size);
  1109. if (!pages)
  1110. return NULL;
  1111. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
  1112. {
  1113. unsigned long order = get_order(size);
  1114. struct page *page;
  1115. page = dma_alloc_from_contiguous(dev, count, order,
  1116. gfp & __GFP_NOWARN);
  1117. if (!page)
  1118. goto error;
  1119. __dma_clear_buffer(page, size, coherent_flag);
  1120. for (i = 0; i < count; i++)
  1121. pages[i] = page + i;
  1122. return pages;
  1123. }
  1124. /* Go straight to 4K chunks if caller says it's OK. */
  1125. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  1126. order_idx = ARRAY_SIZE(iommu_order_array) - 1;
  1127. /*
  1128. * IOMMU can map any pages, so himem can also be used here
  1129. */
  1130. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  1131. while (count) {
  1132. int j, order;
  1133. order = iommu_order_array[order_idx];
  1134. /* Drop down when we get small */
  1135. if (__fls(count) < order) {
  1136. order_idx++;
  1137. continue;
  1138. }
  1139. if (order) {
  1140. /* See if it's easy to allocate a high-order chunk */
  1141. pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
  1142. /* Go down a notch at first sign of pressure */
  1143. if (!pages[i]) {
  1144. order_idx++;
  1145. continue;
  1146. }
  1147. } else {
  1148. pages[i] = alloc_pages(gfp, 0);
  1149. if (!pages[i])
  1150. goto error;
  1151. }
  1152. if (order) {
  1153. split_page(pages[i], order);
  1154. j = 1 << order;
  1155. while (--j)
  1156. pages[i + j] = pages[i] + j;
  1157. }
  1158. __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
  1159. i += 1 << order;
  1160. count -= 1 << order;
  1161. }
  1162. return pages;
  1163. error:
  1164. while (i--)
  1165. if (pages[i])
  1166. __free_pages(pages[i], 0);
  1167. kvfree(pages);
  1168. return NULL;
  1169. }
  1170. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1171. size_t size, unsigned long attrs)
  1172. {
  1173. int count = size >> PAGE_SHIFT;
  1174. int i;
  1175. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  1176. dma_release_from_contiguous(dev, pages[0], count);
  1177. } else {
  1178. for (i = 0; i < count; i++)
  1179. if (pages[i])
  1180. __free_pages(pages[i], 0);
  1181. }
  1182. kvfree(pages);
  1183. return 0;
  1184. }
  1185. /*
  1186. * Create a CPU mapping for a specified pages
  1187. */
  1188. static void *
  1189. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1190. const void *caller)
  1191. {
  1192. return dma_common_pages_remap(pages, size,
  1193. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1194. }
  1195. /*
  1196. * Create a mapping in device IO address space for specified pages
  1197. */
  1198. static dma_addr_t
  1199. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
  1200. unsigned long attrs)
  1201. {
  1202. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1203. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1204. dma_addr_t dma_addr, iova;
  1205. int i;
  1206. dma_addr = __alloc_iova(mapping, size);
  1207. if (dma_addr == ARM_MAPPING_ERROR)
  1208. return dma_addr;
  1209. iova = dma_addr;
  1210. for (i = 0; i < count; ) {
  1211. int ret;
  1212. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1213. phys_addr_t phys = page_to_phys(pages[i]);
  1214. unsigned int len, j;
  1215. for (j = i + 1; j < count; j++, next_pfn++)
  1216. if (page_to_pfn(pages[j]) != next_pfn)
  1217. break;
  1218. len = (j - i) << PAGE_SHIFT;
  1219. ret = iommu_map(mapping->domain, iova, phys, len,
  1220. __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
  1221. if (ret < 0)
  1222. goto fail;
  1223. iova += len;
  1224. i = j;
  1225. }
  1226. return dma_addr;
  1227. fail:
  1228. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1229. __free_iova(mapping, dma_addr, size);
  1230. return ARM_MAPPING_ERROR;
  1231. }
  1232. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1233. {
  1234. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1235. /*
  1236. * add optional in-page offset from iova to size and align
  1237. * result to page size
  1238. */
  1239. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1240. iova &= PAGE_MASK;
  1241. iommu_unmap(mapping->domain, iova, size);
  1242. __free_iova(mapping, iova, size);
  1243. return 0;
  1244. }
  1245. static struct page **__atomic_get_pages(void *addr)
  1246. {
  1247. struct page *page;
  1248. phys_addr_t phys;
  1249. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1250. page = phys_to_page(phys);
  1251. return (struct page **)page;
  1252. }
  1253. static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
  1254. {
  1255. struct vm_struct *area;
  1256. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1257. return __atomic_get_pages(cpu_addr);
  1258. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1259. return cpu_addr;
  1260. area = find_vm_area(cpu_addr);
  1261. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1262. return area->pages;
  1263. return NULL;
  1264. }
  1265. static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
  1266. dma_addr_t *handle, int coherent_flag,
  1267. unsigned long attrs)
  1268. {
  1269. struct page *page;
  1270. void *addr;
  1271. if (coherent_flag == COHERENT)
  1272. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  1273. else
  1274. addr = __alloc_from_pool(size, &page);
  1275. if (!addr)
  1276. return NULL;
  1277. *handle = __iommu_create_mapping(dev, &page, size, attrs);
  1278. if (*handle == ARM_MAPPING_ERROR)
  1279. goto err_mapping;
  1280. return addr;
  1281. err_mapping:
  1282. __free_from_pool(addr, size);
  1283. return NULL;
  1284. }
  1285. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1286. dma_addr_t handle, size_t size, int coherent_flag)
  1287. {
  1288. __iommu_remove_mapping(dev, handle, size);
  1289. if (coherent_flag == COHERENT)
  1290. __dma_free_buffer(virt_to_page(cpu_addr), size);
  1291. else
  1292. __free_from_pool(cpu_addr, size);
  1293. }
  1294. static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1295. dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
  1296. int coherent_flag)
  1297. {
  1298. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1299. struct page **pages;
  1300. void *addr = NULL;
  1301. *handle = ARM_MAPPING_ERROR;
  1302. size = PAGE_ALIGN(size);
  1303. if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
  1304. return __iommu_alloc_simple(dev, size, gfp, handle,
  1305. coherent_flag, attrs);
  1306. /*
  1307. * Following is a work-around (a.k.a. hack) to prevent pages
  1308. * with __GFP_COMP being passed to split_page() which cannot
  1309. * handle them. The real problem is that this flag probably
  1310. * should be 0 on ARM as it is not supported on this
  1311. * platform; see CONFIG_HUGETLBFS.
  1312. */
  1313. gfp &= ~(__GFP_COMP);
  1314. pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
  1315. if (!pages)
  1316. return NULL;
  1317. *handle = __iommu_create_mapping(dev, pages, size, attrs);
  1318. if (*handle == ARM_MAPPING_ERROR)
  1319. goto err_buffer;
  1320. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1321. return pages;
  1322. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1323. __builtin_return_address(0));
  1324. if (!addr)
  1325. goto err_mapping;
  1326. return addr;
  1327. err_mapping:
  1328. __iommu_remove_mapping(dev, *handle, size);
  1329. err_buffer:
  1330. __iommu_free_buffer(dev, pages, size, attrs);
  1331. return NULL;
  1332. }
  1333. static void *arm_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, NORMAL);
  1337. }
  1338. static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
  1339. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1340. {
  1341. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
  1342. }
  1343. static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1344. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1345. unsigned long attrs)
  1346. {
  1347. unsigned long uaddr = vma->vm_start;
  1348. unsigned long usize = vma->vm_end - vma->vm_start;
  1349. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1350. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1351. unsigned long off = vma->vm_pgoff;
  1352. if (!pages)
  1353. return -ENXIO;
  1354. if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
  1355. return -ENXIO;
  1356. pages += off;
  1357. do {
  1358. int ret = vm_insert_page(vma, uaddr, *pages++);
  1359. if (ret) {
  1360. pr_err("Remapping memory failed: %d\n", ret);
  1361. return ret;
  1362. }
  1363. uaddr += PAGE_SIZE;
  1364. usize -= PAGE_SIZE;
  1365. } while (usize > 0);
  1366. return 0;
  1367. }
  1368. static int arm_iommu_mmap_attrs(struct device *dev,
  1369. struct vm_area_struct *vma, void *cpu_addr,
  1370. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1371. {
  1372. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1373. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1374. }
  1375. static int arm_coherent_iommu_mmap_attrs(struct device *dev,
  1376. struct vm_area_struct *vma, void *cpu_addr,
  1377. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1378. {
  1379. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1380. }
  1381. /*
  1382. * free a page as defined by the above mapping.
  1383. * Must not be called with IRQs disabled.
  1384. */
  1385. void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1386. dma_addr_t handle, unsigned long attrs, int coherent_flag)
  1387. {
  1388. struct page **pages;
  1389. size = PAGE_ALIGN(size);
  1390. if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
  1391. __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
  1392. return;
  1393. }
  1394. pages = __iommu_get_pages(cpu_addr, attrs);
  1395. if (!pages) {
  1396. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1397. return;
  1398. }
  1399. if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
  1400. dma_common_free_remap(cpu_addr, size,
  1401. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1402. }
  1403. __iommu_remove_mapping(dev, handle, size);
  1404. __iommu_free_buffer(dev, pages, size, attrs);
  1405. }
  1406. void arm_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, NORMAL);
  1410. }
  1411. void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
  1412. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1413. {
  1414. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
  1415. }
  1416. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1417. void *cpu_addr, dma_addr_t dma_addr,
  1418. size_t size, unsigned long attrs)
  1419. {
  1420. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1421. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1422. if (!pages)
  1423. return -ENXIO;
  1424. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1425. GFP_KERNEL);
  1426. }
  1427. /*
  1428. * Map a part of the scatter-gather list into contiguous io address space
  1429. */
  1430. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1431. size_t size, dma_addr_t *handle,
  1432. enum dma_data_direction dir, unsigned long attrs,
  1433. bool is_coherent)
  1434. {
  1435. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1436. dma_addr_t iova, iova_base;
  1437. int ret = 0;
  1438. unsigned int count;
  1439. struct scatterlist *s;
  1440. int prot;
  1441. size = PAGE_ALIGN(size);
  1442. *handle = ARM_MAPPING_ERROR;
  1443. iova_base = iova = __alloc_iova(mapping, size);
  1444. if (iova == ARM_MAPPING_ERROR)
  1445. return -ENOMEM;
  1446. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1447. phys_addr_t phys = page_to_phys(sg_page(s));
  1448. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1449. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1450. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1451. prot = __dma_info_to_prot(dir, attrs);
  1452. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1453. if (ret < 0)
  1454. goto fail;
  1455. count += len >> PAGE_SHIFT;
  1456. iova += len;
  1457. }
  1458. *handle = iova_base;
  1459. return 0;
  1460. fail:
  1461. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1462. __free_iova(mapping, iova_base, size);
  1463. return ret;
  1464. }
  1465. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1466. enum dma_data_direction dir, unsigned long attrs,
  1467. bool is_coherent)
  1468. {
  1469. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1470. int i, count = 0;
  1471. unsigned int offset = s->offset;
  1472. unsigned int size = s->offset + s->length;
  1473. unsigned int max = dma_get_max_seg_size(dev);
  1474. for (i = 1; i < nents; i++) {
  1475. s = sg_next(s);
  1476. s->dma_address = ARM_MAPPING_ERROR;
  1477. s->dma_length = 0;
  1478. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1479. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1480. dir, attrs, is_coherent) < 0)
  1481. goto bad_mapping;
  1482. dma->dma_address += offset;
  1483. dma->dma_length = size - offset;
  1484. size = offset = s->offset;
  1485. start = s;
  1486. dma = sg_next(dma);
  1487. count += 1;
  1488. }
  1489. size += s->length;
  1490. }
  1491. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1492. is_coherent) < 0)
  1493. goto bad_mapping;
  1494. dma->dma_address += offset;
  1495. dma->dma_length = size - offset;
  1496. return count+1;
  1497. bad_mapping:
  1498. for_each_sg(sg, s, count, i)
  1499. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1500. return 0;
  1501. }
  1502. /**
  1503. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1504. * @dev: valid struct device pointer
  1505. * @sg: list of buffers
  1506. * @nents: number of buffers to map
  1507. * @dir: DMA transfer direction
  1508. *
  1509. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1510. * mode for DMA. The scatter gather list elements are merged together (if
  1511. * possible) and tagged with the appropriate dma address and length. They are
  1512. * obtained via sg_dma_{address,length}.
  1513. */
  1514. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1515. int nents, enum dma_data_direction dir, unsigned long attrs)
  1516. {
  1517. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1518. }
  1519. /**
  1520. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1521. * @dev: valid struct device pointer
  1522. * @sg: list of buffers
  1523. * @nents: number of buffers to map
  1524. * @dir: DMA transfer direction
  1525. *
  1526. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1527. * The scatter gather list elements are merged together (if possible) and
  1528. * tagged with the appropriate dma address and length. They are obtained via
  1529. * sg_dma_{address,length}.
  1530. */
  1531. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1532. int nents, enum dma_data_direction dir, unsigned long attrs)
  1533. {
  1534. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1535. }
  1536. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1537. int nents, enum dma_data_direction dir,
  1538. unsigned long attrs, bool is_coherent)
  1539. {
  1540. struct scatterlist *s;
  1541. int i;
  1542. for_each_sg(sg, s, nents, i) {
  1543. if (sg_dma_len(s))
  1544. __iommu_remove_mapping(dev, sg_dma_address(s),
  1545. sg_dma_len(s));
  1546. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1547. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1548. s->length, dir);
  1549. }
  1550. }
  1551. /**
  1552. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1553. * @dev: valid struct device pointer
  1554. * @sg: list of buffers
  1555. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1556. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1557. *
  1558. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1559. * rules concerning calls here are the same as for dma_unmap_single().
  1560. */
  1561. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1562. int nents, enum dma_data_direction dir,
  1563. unsigned long attrs)
  1564. {
  1565. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1566. }
  1567. /**
  1568. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1569. * @dev: valid struct device pointer
  1570. * @sg: list of buffers
  1571. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1572. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1573. *
  1574. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1575. * rules concerning calls here are the same as for dma_unmap_single().
  1576. */
  1577. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1578. enum dma_data_direction dir,
  1579. unsigned long attrs)
  1580. {
  1581. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1582. }
  1583. /**
  1584. * arm_iommu_sync_sg_for_cpu
  1585. * @dev: valid struct device pointer
  1586. * @sg: list of buffers
  1587. * @nents: number of buffers to map (returned from dma_map_sg)
  1588. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1589. */
  1590. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1591. int nents, enum dma_data_direction dir)
  1592. {
  1593. struct scatterlist *s;
  1594. int i;
  1595. for_each_sg(sg, s, nents, i)
  1596. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1597. }
  1598. /**
  1599. * arm_iommu_sync_sg_for_device
  1600. * @dev: valid struct device pointer
  1601. * @sg: list of buffers
  1602. * @nents: number of buffers to map (returned from dma_map_sg)
  1603. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1604. */
  1605. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1606. int nents, enum dma_data_direction dir)
  1607. {
  1608. struct scatterlist *s;
  1609. int i;
  1610. for_each_sg(sg, s, nents, i)
  1611. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1612. }
  1613. /**
  1614. * arm_coherent_iommu_map_page
  1615. * @dev: valid struct device pointer
  1616. * @page: page that buffer resides in
  1617. * @offset: offset into page for start of buffer
  1618. * @size: size of buffer to map
  1619. * @dir: DMA transfer direction
  1620. *
  1621. * Coherent IOMMU aware version of arm_dma_map_page()
  1622. */
  1623. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1624. unsigned long offset, size_t size, enum dma_data_direction dir,
  1625. unsigned long attrs)
  1626. {
  1627. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1628. dma_addr_t dma_addr;
  1629. int ret, prot, len = PAGE_ALIGN(size + offset);
  1630. dma_addr = __alloc_iova(mapping, len);
  1631. if (dma_addr == ARM_MAPPING_ERROR)
  1632. return dma_addr;
  1633. prot = __dma_info_to_prot(dir, attrs);
  1634. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1635. if (ret < 0)
  1636. goto fail;
  1637. return dma_addr + offset;
  1638. fail:
  1639. __free_iova(mapping, dma_addr, len);
  1640. return ARM_MAPPING_ERROR;
  1641. }
  1642. /**
  1643. * arm_iommu_map_page
  1644. * @dev: valid struct device pointer
  1645. * @page: page that buffer resides in
  1646. * @offset: offset into page for start of buffer
  1647. * @size: size of buffer to map
  1648. * @dir: DMA transfer direction
  1649. *
  1650. * IOMMU aware version of arm_dma_map_page()
  1651. */
  1652. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1653. unsigned long offset, size_t size, enum dma_data_direction dir,
  1654. unsigned long attrs)
  1655. {
  1656. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1657. __dma_page_cpu_to_dev(page, offset, size, dir);
  1658. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1659. }
  1660. /**
  1661. * arm_coherent_iommu_unmap_page
  1662. * @dev: valid struct device pointer
  1663. * @handle: DMA address of buffer
  1664. * @size: size of buffer (same as passed to dma_map_page)
  1665. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1666. *
  1667. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1668. */
  1669. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1670. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1671. {
  1672. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1673. dma_addr_t iova = handle & PAGE_MASK;
  1674. int offset = handle & ~PAGE_MASK;
  1675. int len = PAGE_ALIGN(size + offset);
  1676. if (!iova)
  1677. return;
  1678. iommu_unmap(mapping->domain, iova, len);
  1679. __free_iova(mapping, iova, len);
  1680. }
  1681. /**
  1682. * arm_iommu_unmap_page
  1683. * @dev: valid struct device pointer
  1684. * @handle: DMA address of buffer
  1685. * @size: size of buffer (same as passed to dma_map_page)
  1686. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1687. *
  1688. * IOMMU aware version of arm_dma_unmap_page()
  1689. */
  1690. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1691. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1692. {
  1693. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1694. dma_addr_t iova = handle & PAGE_MASK;
  1695. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1696. int offset = handle & ~PAGE_MASK;
  1697. int len = PAGE_ALIGN(size + offset);
  1698. if (!iova)
  1699. return;
  1700. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1701. __dma_page_dev_to_cpu(page, offset, size, dir);
  1702. iommu_unmap(mapping->domain, iova, len);
  1703. __free_iova(mapping, iova, len);
  1704. }
  1705. /**
  1706. * arm_iommu_map_resource - map a device resource for DMA
  1707. * @dev: valid struct device pointer
  1708. * @phys_addr: physical address of resource
  1709. * @size: size of resource to map
  1710. * @dir: DMA transfer direction
  1711. */
  1712. static dma_addr_t arm_iommu_map_resource(struct device *dev,
  1713. phys_addr_t phys_addr, size_t size,
  1714. enum dma_data_direction dir, unsigned long attrs)
  1715. {
  1716. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1717. dma_addr_t dma_addr;
  1718. int ret, prot;
  1719. phys_addr_t addr = phys_addr & PAGE_MASK;
  1720. unsigned int offset = phys_addr & ~PAGE_MASK;
  1721. size_t len = PAGE_ALIGN(size + offset);
  1722. dma_addr = __alloc_iova(mapping, len);
  1723. if (dma_addr == ARM_MAPPING_ERROR)
  1724. return dma_addr;
  1725. prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
  1726. ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
  1727. if (ret < 0)
  1728. goto fail;
  1729. return dma_addr + offset;
  1730. fail:
  1731. __free_iova(mapping, dma_addr, len);
  1732. return ARM_MAPPING_ERROR;
  1733. }
  1734. /**
  1735. * arm_iommu_unmap_resource - unmap a device DMA resource
  1736. * @dev: valid struct device pointer
  1737. * @dma_handle: DMA address to resource
  1738. * @size: size of resource to map
  1739. * @dir: DMA transfer direction
  1740. */
  1741. static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
  1742. size_t size, enum dma_data_direction dir,
  1743. unsigned long attrs)
  1744. {
  1745. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1746. dma_addr_t iova = dma_handle & PAGE_MASK;
  1747. unsigned int offset = dma_handle & ~PAGE_MASK;
  1748. size_t len = PAGE_ALIGN(size + offset);
  1749. if (!iova)
  1750. return;
  1751. iommu_unmap(mapping->domain, iova, len);
  1752. __free_iova(mapping, iova, len);
  1753. }
  1754. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1755. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1756. {
  1757. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1758. dma_addr_t iova = handle & PAGE_MASK;
  1759. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1760. unsigned int offset = handle & ~PAGE_MASK;
  1761. if (!iova)
  1762. return;
  1763. __dma_page_dev_to_cpu(page, offset, size, dir);
  1764. }
  1765. static void arm_iommu_sync_single_for_device(struct device *dev,
  1766. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1767. {
  1768. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1769. dma_addr_t iova = handle & PAGE_MASK;
  1770. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1771. unsigned int offset = handle & ~PAGE_MASK;
  1772. if (!iova)
  1773. return;
  1774. __dma_page_cpu_to_dev(page, offset, size, dir);
  1775. }
  1776. const struct dma_map_ops iommu_ops = {
  1777. .alloc = arm_iommu_alloc_attrs,
  1778. .free = arm_iommu_free_attrs,
  1779. .mmap = arm_iommu_mmap_attrs,
  1780. .get_sgtable = arm_iommu_get_sgtable,
  1781. .map_page = arm_iommu_map_page,
  1782. .unmap_page = arm_iommu_unmap_page,
  1783. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1784. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1785. .map_sg = arm_iommu_map_sg,
  1786. .unmap_sg = arm_iommu_unmap_sg,
  1787. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1788. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1789. .map_resource = arm_iommu_map_resource,
  1790. .unmap_resource = arm_iommu_unmap_resource,
  1791. .mapping_error = arm_dma_mapping_error,
  1792. .dma_supported = arm_dma_supported,
  1793. };
  1794. const struct dma_map_ops iommu_coherent_ops = {
  1795. .alloc = arm_coherent_iommu_alloc_attrs,
  1796. .free = arm_coherent_iommu_free_attrs,
  1797. .mmap = arm_coherent_iommu_mmap_attrs,
  1798. .get_sgtable = arm_iommu_get_sgtable,
  1799. .map_page = arm_coherent_iommu_map_page,
  1800. .unmap_page = arm_coherent_iommu_unmap_page,
  1801. .map_sg = arm_coherent_iommu_map_sg,
  1802. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1803. .map_resource = arm_iommu_map_resource,
  1804. .unmap_resource = arm_iommu_unmap_resource,
  1805. .mapping_error = arm_dma_mapping_error,
  1806. .dma_supported = arm_dma_supported,
  1807. };
  1808. /**
  1809. * arm_iommu_create_mapping
  1810. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1811. * @base: start address of the valid IO address space
  1812. * @size: maximum size of the valid IO address space
  1813. *
  1814. * Creates a mapping structure which holds information about used/unused
  1815. * IO address ranges, which is required to perform memory allocation and
  1816. * mapping with IOMMU aware functions.
  1817. *
  1818. * The client device need to be attached to the mapping with
  1819. * arm_iommu_attach_device function.
  1820. */
  1821. struct dma_iommu_mapping *
  1822. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1823. {
  1824. unsigned int bits = size >> PAGE_SHIFT;
  1825. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1826. struct dma_iommu_mapping *mapping;
  1827. int extensions = 1;
  1828. int err = -ENOMEM;
  1829. /* currently only 32-bit DMA address space is supported */
  1830. if (size > DMA_BIT_MASK(32) + 1)
  1831. return ERR_PTR(-ERANGE);
  1832. if (!bitmap_size)
  1833. return ERR_PTR(-EINVAL);
  1834. if (bitmap_size > PAGE_SIZE) {
  1835. extensions = bitmap_size / PAGE_SIZE;
  1836. bitmap_size = PAGE_SIZE;
  1837. }
  1838. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1839. if (!mapping)
  1840. goto err;
  1841. mapping->bitmap_size = bitmap_size;
  1842. mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *),
  1843. GFP_KERNEL);
  1844. if (!mapping->bitmaps)
  1845. goto err2;
  1846. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1847. if (!mapping->bitmaps[0])
  1848. goto err3;
  1849. mapping->nr_bitmaps = 1;
  1850. mapping->extensions = extensions;
  1851. mapping->base = base;
  1852. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1853. spin_lock_init(&mapping->lock);
  1854. mapping->domain = iommu_domain_alloc(bus);
  1855. if (!mapping->domain)
  1856. goto err4;
  1857. kref_init(&mapping->kref);
  1858. return mapping;
  1859. err4:
  1860. kfree(mapping->bitmaps[0]);
  1861. err3:
  1862. kfree(mapping->bitmaps);
  1863. err2:
  1864. kfree(mapping);
  1865. err:
  1866. return ERR_PTR(err);
  1867. }
  1868. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1869. static void release_iommu_mapping(struct kref *kref)
  1870. {
  1871. int i;
  1872. struct dma_iommu_mapping *mapping =
  1873. container_of(kref, struct dma_iommu_mapping, kref);
  1874. iommu_domain_free(mapping->domain);
  1875. for (i = 0; i < mapping->nr_bitmaps; i++)
  1876. kfree(mapping->bitmaps[i]);
  1877. kfree(mapping->bitmaps);
  1878. kfree(mapping);
  1879. }
  1880. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1881. {
  1882. int next_bitmap;
  1883. if (mapping->nr_bitmaps >= mapping->extensions)
  1884. return -EINVAL;
  1885. next_bitmap = mapping->nr_bitmaps;
  1886. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1887. GFP_ATOMIC);
  1888. if (!mapping->bitmaps[next_bitmap])
  1889. return -ENOMEM;
  1890. mapping->nr_bitmaps++;
  1891. return 0;
  1892. }
  1893. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1894. {
  1895. if (mapping)
  1896. kref_put(&mapping->kref, release_iommu_mapping);
  1897. }
  1898. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1899. static int __arm_iommu_attach_device(struct device *dev,
  1900. struct dma_iommu_mapping *mapping)
  1901. {
  1902. int err;
  1903. err = iommu_attach_device(mapping->domain, dev);
  1904. if (err)
  1905. return err;
  1906. kref_get(&mapping->kref);
  1907. to_dma_iommu_mapping(dev) = mapping;
  1908. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1909. return 0;
  1910. }
  1911. /**
  1912. * arm_iommu_attach_device
  1913. * @dev: valid struct device pointer
  1914. * @mapping: io address space mapping structure (returned from
  1915. * arm_iommu_create_mapping)
  1916. *
  1917. * Attaches specified io address space mapping to the provided device.
  1918. * This replaces the dma operations (dma_map_ops pointer) with the
  1919. * IOMMU aware version.
  1920. *
  1921. * More than one client might be attached to the same io address space
  1922. * mapping.
  1923. */
  1924. int arm_iommu_attach_device(struct device *dev,
  1925. struct dma_iommu_mapping *mapping)
  1926. {
  1927. int err;
  1928. err = __arm_iommu_attach_device(dev, mapping);
  1929. if (err)
  1930. return err;
  1931. set_dma_ops(dev, &iommu_ops);
  1932. return 0;
  1933. }
  1934. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1935. /**
  1936. * arm_iommu_detach_device
  1937. * @dev: valid struct device pointer
  1938. *
  1939. * Detaches the provided device from a previously attached map.
  1940. * This voids the dma operations (dma_map_ops pointer)
  1941. */
  1942. void arm_iommu_detach_device(struct device *dev)
  1943. {
  1944. struct dma_iommu_mapping *mapping;
  1945. mapping = to_dma_iommu_mapping(dev);
  1946. if (!mapping) {
  1947. dev_warn(dev, "Not attached\n");
  1948. return;
  1949. }
  1950. iommu_detach_device(mapping->domain, dev);
  1951. kref_put(&mapping->kref, release_iommu_mapping);
  1952. to_dma_iommu_mapping(dev) = NULL;
  1953. set_dma_ops(dev, arm_get_dma_map_ops(dev->archdata.dma_coherent));
  1954. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1955. }
  1956. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1957. static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1958. {
  1959. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1960. }
  1961. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1962. const struct iommu_ops *iommu)
  1963. {
  1964. struct dma_iommu_mapping *mapping;
  1965. if (!iommu)
  1966. return false;
  1967. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1968. if (IS_ERR(mapping)) {
  1969. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1970. size, dev_name(dev));
  1971. return false;
  1972. }
  1973. if (__arm_iommu_attach_device(dev, mapping)) {
  1974. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  1975. dev_name(dev));
  1976. arm_iommu_release_mapping(mapping);
  1977. return false;
  1978. }
  1979. return true;
  1980. }
  1981. static void arm_teardown_iommu_dma_ops(struct device *dev)
  1982. {
  1983. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1984. if (!mapping)
  1985. return;
  1986. arm_iommu_detach_device(dev);
  1987. arm_iommu_release_mapping(mapping);
  1988. }
  1989. #else
  1990. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1991. const struct iommu_ops *iommu)
  1992. {
  1993. return false;
  1994. }
  1995. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  1996. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  1997. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  1998. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1999. const struct iommu_ops *iommu, bool coherent)
  2000. {
  2001. const struct dma_map_ops *dma_ops;
  2002. dev->archdata.dma_coherent = coherent;
  2003. /*
  2004. * Don't override the dma_ops if they have already been set. Ideally
  2005. * this should be the only location where dma_ops are set, remove this
  2006. * check when all other callers of set_dma_ops will have disappeared.
  2007. */
  2008. if (dev->dma_ops)
  2009. return;
  2010. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  2011. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  2012. else
  2013. dma_ops = arm_get_dma_map_ops(coherent);
  2014. set_dma_ops(dev, dma_ops);
  2015. #ifdef CONFIG_XEN
  2016. if (xen_initial_domain()) {
  2017. dev->archdata.dev_dma_ops = dev->dma_ops;
  2018. dev->dma_ops = xen_dma_ops;
  2019. }
  2020. #endif
  2021. dev->archdata.dma_ops_setup = true;
  2022. }
  2023. void arch_teardown_dma_ops(struct device *dev)
  2024. {
  2025. if (!dev->archdata.dma_ops_setup)
  2026. return;
  2027. arm_teardown_iommu_dma_ops(dev);
  2028. /* Let arch_setup_dma_ops() start again from scratch upon re-probe */
  2029. set_dma_ops(dev, NULL);
  2030. }