dma-mapping.c 53 KB

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