dma-mapping.c 42 KB

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