dma-mapping.c 48 KB

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