dma-mapping.c 43 KB

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