dma-mapping.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  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. /*
  800. * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
  801. * that the intention is to allow exporting memory allocated via the
  802. * coherent DMA APIs through the dma_buf API, which only accepts a
  803. * scattertable. This presents a couple of problems:
  804. * 1. Not all memory allocated via the coherent DMA APIs is backed by
  805. * a struct page
  806. * 2. Passing coherent DMA memory into the streaming APIs is not allowed
  807. * as we will try to flush the memory through a different alias to that
  808. * actually being used (and the flushes are redundant.)
  809. */
  810. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  811. void *cpu_addr, dma_addr_t handle, size_t size,
  812. unsigned long attrs)
  813. {
  814. unsigned long pfn = dma_to_pfn(dev, handle);
  815. struct page *page;
  816. int ret;
  817. /* If the PFN is not valid, we do not have a struct page */
  818. if (!pfn_valid(pfn))
  819. return -ENXIO;
  820. page = pfn_to_page(pfn);
  821. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  822. if (unlikely(ret))
  823. return ret;
  824. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  825. return 0;
  826. }
  827. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  828. size_t size, enum dma_data_direction dir,
  829. void (*op)(const void *, size_t, int))
  830. {
  831. unsigned long pfn;
  832. size_t left = size;
  833. pfn = page_to_pfn(page) + offset / PAGE_SIZE;
  834. offset %= PAGE_SIZE;
  835. /*
  836. * A single sg entry may refer to multiple physically contiguous
  837. * pages. But we still need to process highmem pages individually.
  838. * If highmem is not configured then the bulk of this loop gets
  839. * optimized out.
  840. */
  841. do {
  842. size_t len = left;
  843. void *vaddr;
  844. page = pfn_to_page(pfn);
  845. if (PageHighMem(page)) {
  846. if (len + offset > PAGE_SIZE)
  847. len = PAGE_SIZE - offset;
  848. if (cache_is_vipt_nonaliasing()) {
  849. vaddr = kmap_atomic(page);
  850. op(vaddr + offset, len, dir);
  851. kunmap_atomic(vaddr);
  852. } else {
  853. vaddr = kmap_high_get(page);
  854. if (vaddr) {
  855. op(vaddr + offset, len, dir);
  856. kunmap_high(page);
  857. }
  858. }
  859. } else {
  860. vaddr = page_address(page) + offset;
  861. op(vaddr, len, dir);
  862. }
  863. offset = 0;
  864. pfn++;
  865. left -= len;
  866. } while (left);
  867. }
  868. /*
  869. * Make an area consistent for devices.
  870. * Note: Drivers should NOT use this function directly, as it will break
  871. * platforms with CONFIG_DMABOUNCE.
  872. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  873. */
  874. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  875. size_t size, enum dma_data_direction dir)
  876. {
  877. phys_addr_t paddr;
  878. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  879. paddr = page_to_phys(page) + off;
  880. if (dir == DMA_FROM_DEVICE) {
  881. outer_inv_range(paddr, paddr + size);
  882. } else {
  883. outer_clean_range(paddr, paddr + size);
  884. }
  885. /* FIXME: non-speculating: flush on bidirectional mappings? */
  886. }
  887. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  888. size_t size, enum dma_data_direction dir)
  889. {
  890. phys_addr_t paddr = page_to_phys(page) + off;
  891. /* FIXME: non-speculating: not required */
  892. /* in any case, don't bother invalidating if DMA to device */
  893. if (dir != DMA_TO_DEVICE) {
  894. outer_inv_range(paddr, paddr + size);
  895. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  896. }
  897. /*
  898. * Mark the D-cache clean for these pages to avoid extra flushing.
  899. */
  900. if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
  901. unsigned long pfn;
  902. size_t left = size;
  903. pfn = page_to_pfn(page) + off / PAGE_SIZE;
  904. off %= PAGE_SIZE;
  905. if (off) {
  906. pfn++;
  907. left -= PAGE_SIZE - off;
  908. }
  909. while (left >= PAGE_SIZE) {
  910. page = pfn_to_page(pfn++);
  911. set_bit(PG_dcache_clean, &page->flags);
  912. left -= PAGE_SIZE;
  913. }
  914. }
  915. }
  916. /**
  917. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  918. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  919. * @sg: list of buffers
  920. * @nents: number of buffers to map
  921. * @dir: DMA transfer direction
  922. *
  923. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  924. * This is the scatter-gather version of the dma_map_single interface.
  925. * Here the scatter gather list elements are each tagged with the
  926. * appropriate dma address and length. They are obtained via
  927. * sg_dma_{address,length}.
  928. *
  929. * Device ownership issues as mentioned for dma_map_single are the same
  930. * here.
  931. */
  932. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  933. enum dma_data_direction dir, unsigned long attrs)
  934. {
  935. struct dma_map_ops *ops = get_dma_ops(dev);
  936. struct scatterlist *s;
  937. int i, j;
  938. for_each_sg(sg, s, nents, i) {
  939. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  940. s->dma_length = s->length;
  941. #endif
  942. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  943. s->length, dir, attrs);
  944. if (dma_mapping_error(dev, s->dma_address))
  945. goto bad_mapping;
  946. }
  947. return nents;
  948. bad_mapping:
  949. for_each_sg(sg, s, i, j)
  950. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  951. return 0;
  952. }
  953. /**
  954. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  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 unmap (same as was passed to dma_map_sg)
  958. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  959. *
  960. * Unmap a set of streaming mode DMA translations. Again, CPU access
  961. * rules concerning calls here are the same as for dma_unmap_single().
  962. */
  963. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  964. enum dma_data_direction dir, unsigned long attrs)
  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->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  971. }
  972. /**
  973. * arm_dma_sync_sg_for_cpu
  974. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  975. * @sg: list of buffers
  976. * @nents: number of buffers to map (returned from dma_map_sg)
  977. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  978. */
  979. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  980. int nents, enum dma_data_direction dir)
  981. {
  982. struct dma_map_ops *ops = get_dma_ops(dev);
  983. struct scatterlist *s;
  984. int i;
  985. for_each_sg(sg, s, nents, i)
  986. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  987. dir);
  988. }
  989. /**
  990. * arm_dma_sync_sg_for_device
  991. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  992. * @sg: list of buffers
  993. * @nents: number of buffers to map (returned from dma_map_sg)
  994. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  995. */
  996. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  997. int nents, enum dma_data_direction dir)
  998. {
  999. struct dma_map_ops *ops = get_dma_ops(dev);
  1000. struct scatterlist *s;
  1001. int i;
  1002. for_each_sg(sg, s, nents, i)
  1003. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  1004. dir);
  1005. }
  1006. /*
  1007. * Return whether the given device DMA address mask can be supported
  1008. * properly. For example, if your device can only drive the low 24-bits
  1009. * during bus mastering, then you would pass 0x00ffffff as the mask
  1010. * to this function.
  1011. */
  1012. int dma_supported(struct device *dev, u64 mask)
  1013. {
  1014. return __dma_supported(dev, mask, false);
  1015. }
  1016. EXPORT_SYMBOL(dma_supported);
  1017. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  1018. static int __init dma_debug_do_init(void)
  1019. {
  1020. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  1021. return 0;
  1022. }
  1023. core_initcall(dma_debug_do_init);
  1024. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  1025. /* IOMMU */
  1026. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
  1027. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  1028. size_t size)
  1029. {
  1030. unsigned int order = get_order(size);
  1031. unsigned int align = 0;
  1032. unsigned int count, start;
  1033. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1034. unsigned long flags;
  1035. dma_addr_t iova;
  1036. int i;
  1037. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  1038. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  1039. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1040. align = (1 << order) - 1;
  1041. spin_lock_irqsave(&mapping->lock, flags);
  1042. for (i = 0; i < mapping->nr_bitmaps; i++) {
  1043. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1044. mapping->bits, 0, count, align);
  1045. if (start > mapping->bits)
  1046. continue;
  1047. bitmap_set(mapping->bitmaps[i], start, count);
  1048. break;
  1049. }
  1050. /*
  1051. * No unused range found. Try to extend the existing mapping
  1052. * and perform a second attempt to reserve an IO virtual
  1053. * address range of size bytes.
  1054. */
  1055. if (i == mapping->nr_bitmaps) {
  1056. if (extend_iommu_mapping(mapping)) {
  1057. spin_unlock_irqrestore(&mapping->lock, flags);
  1058. return DMA_ERROR_CODE;
  1059. }
  1060. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1061. mapping->bits, 0, count, align);
  1062. if (start > mapping->bits) {
  1063. spin_unlock_irqrestore(&mapping->lock, flags);
  1064. return DMA_ERROR_CODE;
  1065. }
  1066. bitmap_set(mapping->bitmaps[i], start, count);
  1067. }
  1068. spin_unlock_irqrestore(&mapping->lock, flags);
  1069. iova = mapping->base + (mapping_size * i);
  1070. iova += start << PAGE_SHIFT;
  1071. return iova;
  1072. }
  1073. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  1074. dma_addr_t addr, size_t size)
  1075. {
  1076. unsigned int start, count;
  1077. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1078. unsigned long flags;
  1079. dma_addr_t bitmap_base;
  1080. u32 bitmap_index;
  1081. if (!size)
  1082. return;
  1083. bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
  1084. BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
  1085. bitmap_base = mapping->base + mapping_size * bitmap_index;
  1086. start = (addr - bitmap_base) >> PAGE_SHIFT;
  1087. if (addr + size > bitmap_base + mapping_size) {
  1088. /*
  1089. * The address range to be freed reaches into the iova
  1090. * range of the next bitmap. This should not happen as
  1091. * we don't allow this in __alloc_iova (at the
  1092. * moment).
  1093. */
  1094. BUG();
  1095. } else
  1096. count = size >> PAGE_SHIFT;
  1097. spin_lock_irqsave(&mapping->lock, flags);
  1098. bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
  1099. spin_unlock_irqrestore(&mapping->lock, flags);
  1100. }
  1101. /* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
  1102. static const int iommu_order_array[] = { 9, 8, 4, 0 };
  1103. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  1104. gfp_t gfp, unsigned long attrs,
  1105. int coherent_flag)
  1106. {
  1107. struct page **pages;
  1108. int count = size >> PAGE_SHIFT;
  1109. int array_size = count * sizeof(struct page *);
  1110. int i = 0;
  1111. int order_idx = 0;
  1112. if (array_size <= PAGE_SIZE)
  1113. pages = kzalloc(array_size, GFP_KERNEL);
  1114. else
  1115. pages = vzalloc(array_size);
  1116. if (!pages)
  1117. return NULL;
  1118. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
  1119. {
  1120. unsigned long order = get_order(size);
  1121. struct page *page;
  1122. page = dma_alloc_from_contiguous(dev, count, order);
  1123. if (!page)
  1124. goto error;
  1125. __dma_clear_buffer(page, size, coherent_flag);
  1126. for (i = 0; i < count; i++)
  1127. pages[i] = page + i;
  1128. return pages;
  1129. }
  1130. /* Go straight to 4K chunks if caller says it's OK. */
  1131. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  1132. order_idx = ARRAY_SIZE(iommu_order_array) - 1;
  1133. /*
  1134. * IOMMU can map any pages, so himem can also be used here
  1135. */
  1136. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  1137. while (count) {
  1138. int j, order;
  1139. order = iommu_order_array[order_idx];
  1140. /* Drop down when we get small */
  1141. if (__fls(count) < order) {
  1142. order_idx++;
  1143. continue;
  1144. }
  1145. if (order) {
  1146. /* See if it's easy to allocate a high-order chunk */
  1147. pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
  1148. /* Go down a notch at first sign of pressure */
  1149. if (!pages[i]) {
  1150. order_idx++;
  1151. continue;
  1152. }
  1153. } else {
  1154. pages[i] = alloc_pages(gfp, 0);
  1155. if (!pages[i])
  1156. goto error;
  1157. }
  1158. if (order) {
  1159. split_page(pages[i], order);
  1160. j = 1 << order;
  1161. while (--j)
  1162. pages[i + j] = pages[i] + j;
  1163. }
  1164. __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
  1165. i += 1 << order;
  1166. count -= 1 << order;
  1167. }
  1168. return pages;
  1169. error:
  1170. while (i--)
  1171. if (pages[i])
  1172. __free_pages(pages[i], 0);
  1173. kvfree(pages);
  1174. return NULL;
  1175. }
  1176. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1177. size_t size, unsigned long attrs)
  1178. {
  1179. int count = size >> PAGE_SHIFT;
  1180. int i;
  1181. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  1182. dma_release_from_contiguous(dev, pages[0], count);
  1183. } else {
  1184. for (i = 0; i < count; i++)
  1185. if (pages[i])
  1186. __free_pages(pages[i], 0);
  1187. }
  1188. kvfree(pages);
  1189. return 0;
  1190. }
  1191. /*
  1192. * Create a CPU mapping for a specified pages
  1193. */
  1194. static void *
  1195. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1196. const void *caller)
  1197. {
  1198. return dma_common_pages_remap(pages, size,
  1199. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1200. }
  1201. /*
  1202. * Create a mapping in device IO address space for specified pages
  1203. */
  1204. static dma_addr_t
  1205. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
  1206. {
  1207. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1208. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1209. dma_addr_t dma_addr, iova;
  1210. int i;
  1211. dma_addr = __alloc_iova(mapping, size);
  1212. if (dma_addr == DMA_ERROR_CODE)
  1213. return dma_addr;
  1214. iova = dma_addr;
  1215. for (i = 0; i < count; ) {
  1216. int ret;
  1217. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1218. phys_addr_t phys = page_to_phys(pages[i]);
  1219. unsigned int len, j;
  1220. for (j = i + 1; j < count; j++, next_pfn++)
  1221. if (page_to_pfn(pages[j]) != next_pfn)
  1222. break;
  1223. len = (j - i) << PAGE_SHIFT;
  1224. ret = iommu_map(mapping->domain, iova, phys, len,
  1225. IOMMU_READ|IOMMU_WRITE);
  1226. if (ret < 0)
  1227. goto fail;
  1228. iova += len;
  1229. i = j;
  1230. }
  1231. return dma_addr;
  1232. fail:
  1233. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1234. __free_iova(mapping, dma_addr, size);
  1235. return DMA_ERROR_CODE;
  1236. }
  1237. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1238. {
  1239. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1240. /*
  1241. * add optional in-page offset from iova to size and align
  1242. * result to page size
  1243. */
  1244. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1245. iova &= PAGE_MASK;
  1246. iommu_unmap(mapping->domain, iova, size);
  1247. __free_iova(mapping, iova, size);
  1248. return 0;
  1249. }
  1250. static struct page **__atomic_get_pages(void *addr)
  1251. {
  1252. struct page *page;
  1253. phys_addr_t phys;
  1254. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1255. page = phys_to_page(phys);
  1256. return (struct page **)page;
  1257. }
  1258. static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
  1259. {
  1260. struct vm_struct *area;
  1261. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1262. return __atomic_get_pages(cpu_addr);
  1263. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1264. return cpu_addr;
  1265. area = find_vm_area(cpu_addr);
  1266. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1267. return area->pages;
  1268. return NULL;
  1269. }
  1270. static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
  1271. dma_addr_t *handle, int coherent_flag)
  1272. {
  1273. struct page *page;
  1274. void *addr;
  1275. if (coherent_flag == COHERENT)
  1276. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  1277. else
  1278. addr = __alloc_from_pool(size, &page);
  1279. if (!addr)
  1280. return NULL;
  1281. *handle = __iommu_create_mapping(dev, &page, size);
  1282. if (*handle == DMA_ERROR_CODE)
  1283. goto err_mapping;
  1284. return addr;
  1285. err_mapping:
  1286. __free_from_pool(addr, size);
  1287. return NULL;
  1288. }
  1289. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1290. dma_addr_t handle, size_t size, int coherent_flag)
  1291. {
  1292. __iommu_remove_mapping(dev, handle, size);
  1293. if (coherent_flag == COHERENT)
  1294. __dma_free_buffer(virt_to_page(cpu_addr), size);
  1295. else
  1296. __free_from_pool(cpu_addr, size);
  1297. }
  1298. static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1299. dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
  1300. int coherent_flag)
  1301. {
  1302. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1303. struct page **pages;
  1304. void *addr = NULL;
  1305. *handle = DMA_ERROR_CODE;
  1306. size = PAGE_ALIGN(size);
  1307. if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
  1308. return __iommu_alloc_simple(dev, size, gfp, handle,
  1309. coherent_flag);
  1310. /*
  1311. * Following is a work-around (a.k.a. hack) to prevent pages
  1312. * with __GFP_COMP being passed to split_page() which cannot
  1313. * handle them. The real problem is that this flag probably
  1314. * should be 0 on ARM as it is not supported on this
  1315. * platform; see CONFIG_HUGETLBFS.
  1316. */
  1317. gfp &= ~(__GFP_COMP);
  1318. pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
  1319. if (!pages)
  1320. return NULL;
  1321. *handle = __iommu_create_mapping(dev, pages, size);
  1322. if (*handle == DMA_ERROR_CODE)
  1323. goto err_buffer;
  1324. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1325. return pages;
  1326. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1327. __builtin_return_address(0));
  1328. if (!addr)
  1329. goto err_mapping;
  1330. return addr;
  1331. err_mapping:
  1332. __iommu_remove_mapping(dev, *handle, size);
  1333. err_buffer:
  1334. __iommu_free_buffer(dev, pages, size, attrs);
  1335. return NULL;
  1336. }
  1337. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1338. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1339. {
  1340. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
  1341. }
  1342. static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
  1343. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1344. {
  1345. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
  1346. }
  1347. static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1348. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1349. unsigned long attrs)
  1350. {
  1351. unsigned long uaddr = vma->vm_start;
  1352. unsigned long usize = vma->vm_end - vma->vm_start;
  1353. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1354. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1355. unsigned long off = vma->vm_pgoff;
  1356. if (!pages)
  1357. return -ENXIO;
  1358. if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
  1359. return -ENXIO;
  1360. pages += off;
  1361. do {
  1362. int ret = vm_insert_page(vma, uaddr, *pages++);
  1363. if (ret) {
  1364. pr_err("Remapping memory failed: %d\n", ret);
  1365. return ret;
  1366. }
  1367. uaddr += PAGE_SIZE;
  1368. usize -= PAGE_SIZE;
  1369. } while (usize > 0);
  1370. return 0;
  1371. }
  1372. static int arm_iommu_mmap_attrs(struct device *dev,
  1373. struct vm_area_struct *vma, void *cpu_addr,
  1374. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1375. {
  1376. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1377. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1378. }
  1379. static int arm_coherent_iommu_mmap_attrs(struct device *dev,
  1380. struct vm_area_struct *vma, void *cpu_addr,
  1381. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1382. {
  1383. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1384. }
  1385. /*
  1386. * free a page as defined by the above mapping.
  1387. * Must not be called with IRQs disabled.
  1388. */
  1389. void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1390. dma_addr_t handle, unsigned long attrs, int coherent_flag)
  1391. {
  1392. struct page **pages;
  1393. size = PAGE_ALIGN(size);
  1394. if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
  1395. __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
  1396. return;
  1397. }
  1398. pages = __iommu_get_pages(cpu_addr, attrs);
  1399. if (!pages) {
  1400. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1401. return;
  1402. }
  1403. if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
  1404. dma_common_free_remap(cpu_addr, size,
  1405. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1406. }
  1407. __iommu_remove_mapping(dev, handle, size);
  1408. __iommu_free_buffer(dev, pages, size, attrs);
  1409. }
  1410. void arm_iommu_free_attrs(struct device *dev, size_t size,
  1411. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1412. {
  1413. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
  1414. }
  1415. void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
  1416. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1417. {
  1418. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
  1419. }
  1420. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1421. void *cpu_addr, dma_addr_t dma_addr,
  1422. size_t size, unsigned long attrs)
  1423. {
  1424. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1425. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1426. if (!pages)
  1427. return -ENXIO;
  1428. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1429. GFP_KERNEL);
  1430. }
  1431. static int __dma_direction_to_prot(enum dma_data_direction dir)
  1432. {
  1433. int prot;
  1434. switch (dir) {
  1435. case DMA_BIDIRECTIONAL:
  1436. prot = IOMMU_READ | IOMMU_WRITE;
  1437. break;
  1438. case DMA_TO_DEVICE:
  1439. prot = IOMMU_READ;
  1440. break;
  1441. case DMA_FROM_DEVICE:
  1442. prot = IOMMU_WRITE;
  1443. break;
  1444. default:
  1445. prot = 0;
  1446. }
  1447. return prot;
  1448. }
  1449. /*
  1450. * Map a part of the scatter-gather list into contiguous io address space
  1451. */
  1452. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1453. size_t size, dma_addr_t *handle,
  1454. enum dma_data_direction dir, unsigned long attrs,
  1455. bool is_coherent)
  1456. {
  1457. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1458. dma_addr_t iova, iova_base;
  1459. int ret = 0;
  1460. unsigned int count;
  1461. struct scatterlist *s;
  1462. int prot;
  1463. size = PAGE_ALIGN(size);
  1464. *handle = DMA_ERROR_CODE;
  1465. iova_base = iova = __alloc_iova(mapping, size);
  1466. if (iova == DMA_ERROR_CODE)
  1467. return -ENOMEM;
  1468. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1469. phys_addr_t phys = page_to_phys(sg_page(s));
  1470. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1471. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1472. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1473. prot = __dma_direction_to_prot(dir);
  1474. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1475. if (ret < 0)
  1476. goto fail;
  1477. count += len >> PAGE_SHIFT;
  1478. iova += len;
  1479. }
  1480. *handle = iova_base;
  1481. return 0;
  1482. fail:
  1483. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1484. __free_iova(mapping, iova_base, size);
  1485. return ret;
  1486. }
  1487. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1488. enum dma_data_direction dir, unsigned long attrs,
  1489. bool is_coherent)
  1490. {
  1491. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1492. int i, count = 0;
  1493. unsigned int offset = s->offset;
  1494. unsigned int size = s->offset + s->length;
  1495. unsigned int max = dma_get_max_seg_size(dev);
  1496. for (i = 1; i < nents; i++) {
  1497. s = sg_next(s);
  1498. s->dma_address = DMA_ERROR_CODE;
  1499. s->dma_length = 0;
  1500. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1501. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1502. dir, attrs, is_coherent) < 0)
  1503. goto bad_mapping;
  1504. dma->dma_address += offset;
  1505. dma->dma_length = size - offset;
  1506. size = offset = s->offset;
  1507. start = s;
  1508. dma = sg_next(dma);
  1509. count += 1;
  1510. }
  1511. size += s->length;
  1512. }
  1513. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1514. is_coherent) < 0)
  1515. goto bad_mapping;
  1516. dma->dma_address += offset;
  1517. dma->dma_length = size - offset;
  1518. return count+1;
  1519. bad_mapping:
  1520. for_each_sg(sg, s, count, i)
  1521. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1522. return 0;
  1523. }
  1524. /**
  1525. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1526. * @dev: valid struct device pointer
  1527. * @sg: list of buffers
  1528. * @nents: number of buffers to map
  1529. * @dir: DMA transfer direction
  1530. *
  1531. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1532. * mode for DMA. The scatter gather list elements are merged together (if
  1533. * possible) and tagged with the appropriate dma address and length. They are
  1534. * obtained via sg_dma_{address,length}.
  1535. */
  1536. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1537. int nents, enum dma_data_direction dir, unsigned long attrs)
  1538. {
  1539. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1540. }
  1541. /**
  1542. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1543. * @dev: valid struct device pointer
  1544. * @sg: list of buffers
  1545. * @nents: number of buffers to map
  1546. * @dir: DMA transfer direction
  1547. *
  1548. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1549. * The scatter gather list elements are merged together (if possible) and
  1550. * tagged with the appropriate dma address and length. They are obtained via
  1551. * sg_dma_{address,length}.
  1552. */
  1553. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1554. int nents, enum dma_data_direction dir, unsigned long attrs)
  1555. {
  1556. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1557. }
  1558. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1559. int nents, enum dma_data_direction dir,
  1560. unsigned long attrs, bool is_coherent)
  1561. {
  1562. struct scatterlist *s;
  1563. int i;
  1564. for_each_sg(sg, s, nents, i) {
  1565. if (sg_dma_len(s))
  1566. __iommu_remove_mapping(dev, sg_dma_address(s),
  1567. sg_dma_len(s));
  1568. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1569. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1570. s->length, dir);
  1571. }
  1572. }
  1573. /**
  1574. * arm_coherent_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_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1584. int nents, enum dma_data_direction dir,
  1585. unsigned long attrs)
  1586. {
  1587. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1588. }
  1589. /**
  1590. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1591. * @dev: valid struct device pointer
  1592. * @sg: list of buffers
  1593. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1594. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1595. *
  1596. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1597. * rules concerning calls here are the same as for dma_unmap_single().
  1598. */
  1599. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1600. enum dma_data_direction dir,
  1601. unsigned long attrs)
  1602. {
  1603. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1604. }
  1605. /**
  1606. * arm_iommu_sync_sg_for_cpu
  1607. * @dev: valid struct device pointer
  1608. * @sg: list of buffers
  1609. * @nents: number of buffers to map (returned from dma_map_sg)
  1610. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1611. */
  1612. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1613. int nents, enum dma_data_direction dir)
  1614. {
  1615. struct scatterlist *s;
  1616. int i;
  1617. for_each_sg(sg, s, nents, i)
  1618. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1619. }
  1620. /**
  1621. * arm_iommu_sync_sg_for_device
  1622. * @dev: valid struct device pointer
  1623. * @sg: list of buffers
  1624. * @nents: number of buffers to map (returned from dma_map_sg)
  1625. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1626. */
  1627. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1628. int nents, enum dma_data_direction dir)
  1629. {
  1630. struct scatterlist *s;
  1631. int i;
  1632. for_each_sg(sg, s, nents, i)
  1633. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1634. }
  1635. /**
  1636. * arm_coherent_iommu_map_page
  1637. * @dev: valid struct device pointer
  1638. * @page: page that buffer resides in
  1639. * @offset: offset into page for start of buffer
  1640. * @size: size of buffer to map
  1641. * @dir: DMA transfer direction
  1642. *
  1643. * Coherent IOMMU aware version of arm_dma_map_page()
  1644. */
  1645. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1646. unsigned long offset, size_t size, enum dma_data_direction dir,
  1647. unsigned long attrs)
  1648. {
  1649. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1650. dma_addr_t dma_addr;
  1651. int ret, prot, len = PAGE_ALIGN(size + offset);
  1652. dma_addr = __alloc_iova(mapping, len);
  1653. if (dma_addr == DMA_ERROR_CODE)
  1654. return dma_addr;
  1655. prot = __dma_direction_to_prot(dir);
  1656. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1657. if (ret < 0)
  1658. goto fail;
  1659. return dma_addr + offset;
  1660. fail:
  1661. __free_iova(mapping, dma_addr, len);
  1662. return DMA_ERROR_CODE;
  1663. }
  1664. /**
  1665. * arm_iommu_map_page
  1666. * @dev: valid struct device pointer
  1667. * @page: page that buffer resides in
  1668. * @offset: offset into page for start of buffer
  1669. * @size: size of buffer to map
  1670. * @dir: DMA transfer direction
  1671. *
  1672. * IOMMU aware version of arm_dma_map_page()
  1673. */
  1674. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1675. unsigned long offset, size_t size, enum dma_data_direction dir,
  1676. unsigned long attrs)
  1677. {
  1678. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1679. __dma_page_cpu_to_dev(page, offset, size, dir);
  1680. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1681. }
  1682. /**
  1683. * arm_coherent_iommu_unmap_page
  1684. * @dev: valid struct device pointer
  1685. * @handle: DMA address of buffer
  1686. * @size: size of buffer (same as passed to dma_map_page)
  1687. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1688. *
  1689. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1690. */
  1691. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1692. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1693. {
  1694. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1695. dma_addr_t iova = handle & PAGE_MASK;
  1696. int offset = handle & ~PAGE_MASK;
  1697. int len = PAGE_ALIGN(size + offset);
  1698. if (!iova)
  1699. return;
  1700. iommu_unmap(mapping->domain, iova, len);
  1701. __free_iova(mapping, iova, len);
  1702. }
  1703. /**
  1704. * arm_iommu_unmap_page
  1705. * @dev: valid struct device pointer
  1706. * @handle: DMA address of buffer
  1707. * @size: size of buffer (same as passed to dma_map_page)
  1708. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1709. *
  1710. * IOMMU aware version of arm_dma_unmap_page()
  1711. */
  1712. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1713. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1714. {
  1715. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1716. dma_addr_t iova = handle & PAGE_MASK;
  1717. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1718. int offset = handle & ~PAGE_MASK;
  1719. int len = PAGE_ALIGN(size + offset);
  1720. if (!iova)
  1721. return;
  1722. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1723. __dma_page_dev_to_cpu(page, offset, size, dir);
  1724. iommu_unmap(mapping->domain, iova, len);
  1725. __free_iova(mapping, iova, len);
  1726. }
  1727. /**
  1728. * arm_iommu_map_resource - map a device resource for DMA
  1729. * @dev: valid struct device pointer
  1730. * @phys_addr: physical address of resource
  1731. * @size: size of resource to map
  1732. * @dir: DMA transfer direction
  1733. */
  1734. static dma_addr_t arm_iommu_map_resource(struct device *dev,
  1735. phys_addr_t phys_addr, size_t size,
  1736. enum dma_data_direction dir, unsigned long attrs)
  1737. {
  1738. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1739. dma_addr_t dma_addr;
  1740. int ret, prot;
  1741. phys_addr_t addr = phys_addr & PAGE_MASK;
  1742. unsigned int offset = phys_addr & ~PAGE_MASK;
  1743. size_t len = PAGE_ALIGN(size + offset);
  1744. dma_addr = __alloc_iova(mapping, len);
  1745. if (dma_addr == DMA_ERROR_CODE)
  1746. return dma_addr;
  1747. prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
  1748. ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
  1749. if (ret < 0)
  1750. goto fail;
  1751. return dma_addr + offset;
  1752. fail:
  1753. __free_iova(mapping, dma_addr, len);
  1754. return DMA_ERROR_CODE;
  1755. }
  1756. /**
  1757. * arm_iommu_unmap_resource - unmap a device DMA resource
  1758. * @dev: valid struct device pointer
  1759. * @dma_handle: DMA address to resource
  1760. * @size: size of resource to map
  1761. * @dir: DMA transfer direction
  1762. */
  1763. static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
  1764. size_t size, enum dma_data_direction dir,
  1765. unsigned long attrs)
  1766. {
  1767. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1768. dma_addr_t iova = dma_handle & PAGE_MASK;
  1769. unsigned int offset = dma_handle & ~PAGE_MASK;
  1770. size_t len = PAGE_ALIGN(size + offset);
  1771. if (!iova)
  1772. return;
  1773. iommu_unmap(mapping->domain, iova, len);
  1774. __free_iova(mapping, iova, len);
  1775. }
  1776. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1777. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1778. {
  1779. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1780. dma_addr_t iova = handle & PAGE_MASK;
  1781. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1782. unsigned int offset = handle & ~PAGE_MASK;
  1783. if (!iova)
  1784. return;
  1785. __dma_page_dev_to_cpu(page, offset, size, dir);
  1786. }
  1787. static void arm_iommu_sync_single_for_device(struct device *dev,
  1788. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1789. {
  1790. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1791. dma_addr_t iova = handle & PAGE_MASK;
  1792. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1793. unsigned int offset = handle & ~PAGE_MASK;
  1794. if (!iova)
  1795. return;
  1796. __dma_page_cpu_to_dev(page, offset, size, dir);
  1797. }
  1798. struct dma_map_ops iommu_ops = {
  1799. .alloc = arm_iommu_alloc_attrs,
  1800. .free = arm_iommu_free_attrs,
  1801. .mmap = arm_iommu_mmap_attrs,
  1802. .get_sgtable = arm_iommu_get_sgtable,
  1803. .map_page = arm_iommu_map_page,
  1804. .unmap_page = arm_iommu_unmap_page,
  1805. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1806. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1807. .map_sg = arm_iommu_map_sg,
  1808. .unmap_sg = arm_iommu_unmap_sg,
  1809. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1810. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1811. .map_resource = arm_iommu_map_resource,
  1812. .unmap_resource = arm_iommu_unmap_resource,
  1813. };
  1814. struct dma_map_ops iommu_coherent_ops = {
  1815. .alloc = arm_coherent_iommu_alloc_attrs,
  1816. .free = arm_coherent_iommu_free_attrs,
  1817. .mmap = arm_coherent_iommu_mmap_attrs,
  1818. .get_sgtable = arm_iommu_get_sgtable,
  1819. .map_page = arm_coherent_iommu_map_page,
  1820. .unmap_page = arm_coherent_iommu_unmap_page,
  1821. .map_sg = arm_coherent_iommu_map_sg,
  1822. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1823. .map_resource = arm_iommu_map_resource,
  1824. .unmap_resource = arm_iommu_unmap_resource,
  1825. };
  1826. /**
  1827. * arm_iommu_create_mapping
  1828. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1829. * @base: start address of the valid IO address space
  1830. * @size: maximum size of the valid IO address space
  1831. *
  1832. * Creates a mapping structure which holds information about used/unused
  1833. * IO address ranges, which is required to perform memory allocation and
  1834. * mapping with IOMMU aware functions.
  1835. *
  1836. * The client device need to be attached to the mapping with
  1837. * arm_iommu_attach_device function.
  1838. */
  1839. struct dma_iommu_mapping *
  1840. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1841. {
  1842. unsigned int bits = size >> PAGE_SHIFT;
  1843. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1844. struct dma_iommu_mapping *mapping;
  1845. int extensions = 1;
  1846. int err = -ENOMEM;
  1847. /* currently only 32-bit DMA address space is supported */
  1848. if (size > DMA_BIT_MASK(32) + 1)
  1849. return ERR_PTR(-ERANGE);
  1850. if (!bitmap_size)
  1851. return ERR_PTR(-EINVAL);
  1852. if (bitmap_size > PAGE_SIZE) {
  1853. extensions = bitmap_size / PAGE_SIZE;
  1854. bitmap_size = PAGE_SIZE;
  1855. }
  1856. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1857. if (!mapping)
  1858. goto err;
  1859. mapping->bitmap_size = bitmap_size;
  1860. mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
  1861. GFP_KERNEL);
  1862. if (!mapping->bitmaps)
  1863. goto err2;
  1864. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1865. if (!mapping->bitmaps[0])
  1866. goto err3;
  1867. mapping->nr_bitmaps = 1;
  1868. mapping->extensions = extensions;
  1869. mapping->base = base;
  1870. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1871. spin_lock_init(&mapping->lock);
  1872. mapping->domain = iommu_domain_alloc(bus);
  1873. if (!mapping->domain)
  1874. goto err4;
  1875. kref_init(&mapping->kref);
  1876. return mapping;
  1877. err4:
  1878. kfree(mapping->bitmaps[0]);
  1879. err3:
  1880. kfree(mapping->bitmaps);
  1881. err2:
  1882. kfree(mapping);
  1883. err:
  1884. return ERR_PTR(err);
  1885. }
  1886. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1887. static void release_iommu_mapping(struct kref *kref)
  1888. {
  1889. int i;
  1890. struct dma_iommu_mapping *mapping =
  1891. container_of(kref, struct dma_iommu_mapping, kref);
  1892. iommu_domain_free(mapping->domain);
  1893. for (i = 0; i < mapping->nr_bitmaps; i++)
  1894. kfree(mapping->bitmaps[i]);
  1895. kfree(mapping->bitmaps);
  1896. kfree(mapping);
  1897. }
  1898. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1899. {
  1900. int next_bitmap;
  1901. if (mapping->nr_bitmaps >= mapping->extensions)
  1902. return -EINVAL;
  1903. next_bitmap = mapping->nr_bitmaps;
  1904. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1905. GFP_ATOMIC);
  1906. if (!mapping->bitmaps[next_bitmap])
  1907. return -ENOMEM;
  1908. mapping->nr_bitmaps++;
  1909. return 0;
  1910. }
  1911. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1912. {
  1913. if (mapping)
  1914. kref_put(&mapping->kref, release_iommu_mapping);
  1915. }
  1916. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1917. static int __arm_iommu_attach_device(struct device *dev,
  1918. struct dma_iommu_mapping *mapping)
  1919. {
  1920. int err;
  1921. err = iommu_attach_device(mapping->domain, dev);
  1922. if (err)
  1923. return err;
  1924. kref_get(&mapping->kref);
  1925. to_dma_iommu_mapping(dev) = mapping;
  1926. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1927. return 0;
  1928. }
  1929. /**
  1930. * arm_iommu_attach_device
  1931. * @dev: valid struct device pointer
  1932. * @mapping: io address space mapping structure (returned from
  1933. * arm_iommu_create_mapping)
  1934. *
  1935. * Attaches specified io address space mapping to the provided device.
  1936. * This replaces the dma operations (dma_map_ops pointer) with the
  1937. * IOMMU aware version.
  1938. *
  1939. * More than one client might be attached to the same io address space
  1940. * mapping.
  1941. */
  1942. int arm_iommu_attach_device(struct device *dev,
  1943. struct dma_iommu_mapping *mapping)
  1944. {
  1945. int err;
  1946. err = __arm_iommu_attach_device(dev, mapping);
  1947. if (err)
  1948. return err;
  1949. set_dma_ops(dev, &iommu_ops);
  1950. return 0;
  1951. }
  1952. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1953. static void __arm_iommu_detach_device(struct device *dev)
  1954. {
  1955. struct dma_iommu_mapping *mapping;
  1956. mapping = to_dma_iommu_mapping(dev);
  1957. if (!mapping) {
  1958. dev_warn(dev, "Not attached\n");
  1959. return;
  1960. }
  1961. iommu_detach_device(mapping->domain, dev);
  1962. kref_put(&mapping->kref, release_iommu_mapping);
  1963. to_dma_iommu_mapping(dev) = NULL;
  1964. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1965. }
  1966. /**
  1967. * arm_iommu_detach_device
  1968. * @dev: valid struct device pointer
  1969. *
  1970. * Detaches the provided device from a previously attached map.
  1971. * This voids the dma operations (dma_map_ops pointer)
  1972. */
  1973. void arm_iommu_detach_device(struct device *dev)
  1974. {
  1975. __arm_iommu_detach_device(dev);
  1976. set_dma_ops(dev, NULL);
  1977. }
  1978. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1979. static struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1980. {
  1981. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1982. }
  1983. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1984. const struct iommu_ops *iommu)
  1985. {
  1986. struct dma_iommu_mapping *mapping;
  1987. if (!iommu)
  1988. return false;
  1989. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1990. if (IS_ERR(mapping)) {
  1991. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1992. size, dev_name(dev));
  1993. return false;
  1994. }
  1995. if (__arm_iommu_attach_device(dev, mapping)) {
  1996. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  1997. dev_name(dev));
  1998. arm_iommu_release_mapping(mapping);
  1999. return false;
  2000. }
  2001. return true;
  2002. }
  2003. static void arm_teardown_iommu_dma_ops(struct device *dev)
  2004. {
  2005. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  2006. if (!mapping)
  2007. return;
  2008. __arm_iommu_detach_device(dev);
  2009. arm_iommu_release_mapping(mapping);
  2010. }
  2011. #else
  2012. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  2013. const struct iommu_ops *iommu)
  2014. {
  2015. return false;
  2016. }
  2017. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  2018. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  2019. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  2020. static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  2021. {
  2022. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  2023. }
  2024. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  2025. const struct iommu_ops *iommu, bool coherent)
  2026. {
  2027. struct dma_map_ops *dma_ops;
  2028. dev->archdata.dma_coherent = coherent;
  2029. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  2030. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  2031. else
  2032. dma_ops = arm_get_dma_map_ops(coherent);
  2033. set_dma_ops(dev, dma_ops);
  2034. }
  2035. void arch_teardown_dma_ops(struct device *dev)
  2036. {
  2037. arm_teardown_iommu_dma_ops(dev);
  2038. }