dma-mapping.c 62 KB

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