dma-mapping.c 63 KB

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