dma-mapping.c 54 KB

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