dma-mapping.c 54 KB

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