dma-mapping.c 42 KB

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