dma-mapping.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  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/genalloc.h>
  16. #include <linux/gfp.h>
  17. #include <linux/errno.h>
  18. #include <linux/list.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dma-contiguous.h>
  23. #include <linux/highmem.h>
  24. #include <linux/memblock.h>
  25. #include <linux/slab.h>
  26. #include <linux/iommu.h>
  27. #include <linux/io.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/sizes.h>
  30. #include <linux/cma.h>
  31. #include <asm/memory.h>
  32. #include <asm/highmem.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/mach/arch.h>
  36. #include <asm/dma-iommu.h>
  37. #include <asm/mach/map.h>
  38. #include <asm/system_info.h>
  39. #include <asm/dma-contiguous.h>
  40. #include "dma.h"
  41. #include "mm.h"
  42. struct arm_dma_alloc_args {
  43. struct device *dev;
  44. size_t size;
  45. gfp_t gfp;
  46. pgprot_t prot;
  47. const void *caller;
  48. bool want_vaddr;
  49. int coherent_flag;
  50. };
  51. struct arm_dma_free_args {
  52. struct device *dev;
  53. size_t size;
  54. void *cpu_addr;
  55. struct page *page;
  56. bool want_vaddr;
  57. };
  58. #define NORMAL 0
  59. #define COHERENT 1
  60. struct arm_dma_allocator {
  61. void *(*alloc)(struct arm_dma_alloc_args *args,
  62. struct page **ret_page);
  63. void (*free)(struct arm_dma_free_args *args);
  64. };
  65. struct arm_dma_buffer {
  66. struct list_head list;
  67. void *virt;
  68. struct arm_dma_allocator *allocator;
  69. };
  70. static LIST_HEAD(arm_dma_bufs);
  71. static DEFINE_SPINLOCK(arm_dma_bufs_lock);
  72. static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
  73. {
  74. struct arm_dma_buffer *buf, *found = NULL;
  75. unsigned long flags;
  76. spin_lock_irqsave(&arm_dma_bufs_lock, flags);
  77. list_for_each_entry(buf, &arm_dma_bufs, list) {
  78. if (buf->virt == virt) {
  79. list_del(&buf->list);
  80. found = buf;
  81. break;
  82. }
  83. }
  84. spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
  85. return found;
  86. }
  87. /*
  88. * The DMA API is built upon the notion of "buffer ownership". A buffer
  89. * is either exclusively owned by the CPU (and therefore may be accessed
  90. * by it) or exclusively owned by the DMA device. These helper functions
  91. * represent the transitions between these two ownership states.
  92. *
  93. * Note, however, that on later ARMs, this notion does not work due to
  94. * speculative prefetches. We model our approach on the assumption that
  95. * the CPU does do speculative prefetches, which means we clean caches
  96. * before transfers and delay cache invalidation until transfer completion.
  97. *
  98. */
  99. static void __dma_page_cpu_to_dev(struct page *, unsigned long,
  100. size_t, enum dma_data_direction);
  101. static void __dma_page_dev_to_cpu(struct page *, unsigned long,
  102. size_t, enum dma_data_direction);
  103. /**
  104. * arm_dma_map_page - map a portion of a page for streaming DMA
  105. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  106. * @page: page that buffer resides in
  107. * @offset: offset into page for start of buffer
  108. * @size: size of buffer to map
  109. * @dir: DMA transfer direction
  110. *
  111. * Ensure that any data held in the cache is appropriately discarded
  112. * or written back.
  113. *
  114. * The device owns this memory once this call has completed. The CPU
  115. * can regain ownership by calling dma_unmap_page().
  116. */
  117. static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
  118. unsigned long offset, size_t size, enum dma_data_direction dir,
  119. unsigned long attrs)
  120. {
  121. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  122. __dma_page_cpu_to_dev(page, offset, size, dir);
  123. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  124. }
  125. static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
  126. unsigned long offset, size_t size, enum dma_data_direction dir,
  127. unsigned long attrs)
  128. {
  129. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  130. }
  131. /**
  132. * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  133. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  134. * @handle: DMA address of buffer
  135. * @size: size of buffer (same as passed to dma_map_page)
  136. * @dir: DMA transfer direction (same as passed to dma_map_page)
  137. *
  138. * Unmap a page streaming mode DMA translation. The handle and size
  139. * must match what was provided in the previous dma_map_page() call.
  140. * All other usages are undefined.
  141. *
  142. * After this call, reads by the CPU to the buffer are guaranteed to see
  143. * whatever the device wrote there.
  144. */
  145. static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
  146. size_t size, enum dma_data_direction dir, unsigned long attrs)
  147. {
  148. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  149. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
  150. handle & ~PAGE_MASK, size, dir);
  151. }
  152. static void arm_dma_sync_single_for_cpu(struct device *dev,
  153. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  154. {
  155. unsigned int offset = handle & (PAGE_SIZE - 1);
  156. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  157. __dma_page_dev_to_cpu(page, offset, size, dir);
  158. }
  159. static void arm_dma_sync_single_for_device(struct device *dev,
  160. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  161. {
  162. unsigned int offset = handle & (PAGE_SIZE - 1);
  163. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  164. __dma_page_cpu_to_dev(page, offset, size, dir);
  165. }
  166. static int arm_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  167. {
  168. return dma_addr == ARM_MAPPING_ERROR;
  169. }
  170. const struct dma_map_ops arm_dma_ops = {
  171. .alloc = arm_dma_alloc,
  172. .free = arm_dma_free,
  173. .mmap = arm_dma_mmap,
  174. .get_sgtable = arm_dma_get_sgtable,
  175. .map_page = arm_dma_map_page,
  176. .unmap_page = arm_dma_unmap_page,
  177. .map_sg = arm_dma_map_sg,
  178. .unmap_sg = arm_dma_unmap_sg,
  179. .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
  180. .sync_single_for_device = arm_dma_sync_single_for_device,
  181. .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
  182. .sync_sg_for_device = arm_dma_sync_sg_for_device,
  183. .mapping_error = arm_dma_mapping_error,
  184. .dma_supported = arm_dma_supported,
  185. };
  186. EXPORT_SYMBOL(arm_dma_ops);
  187. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  188. dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
  189. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  190. dma_addr_t handle, unsigned long attrs);
  191. static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  192. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  193. unsigned long attrs);
  194. const struct dma_map_ops arm_coherent_dma_ops = {
  195. .alloc = arm_coherent_dma_alloc,
  196. .free = arm_coherent_dma_free,
  197. .mmap = arm_coherent_dma_mmap,
  198. .get_sgtable = arm_dma_get_sgtable,
  199. .map_page = arm_coherent_dma_map_page,
  200. .map_sg = arm_dma_map_sg,
  201. .mapping_error = arm_dma_mapping_error,
  202. .dma_supported = arm_dma_supported,
  203. };
  204. EXPORT_SYMBOL(arm_coherent_dma_ops);
  205. static int __dma_supported(struct device *dev, u64 mask, bool warn)
  206. {
  207. unsigned long max_dma_pfn;
  208. /*
  209. * If the mask allows for more memory than we can address,
  210. * and we actually have that much memory, then we must
  211. * indicate that DMA to this device is not supported.
  212. */
  213. if (sizeof(mask) != sizeof(dma_addr_t) &&
  214. mask > (dma_addr_t)~0 &&
  215. dma_to_pfn(dev, ~0) < max_pfn - 1) {
  216. if (warn) {
  217. dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
  218. mask);
  219. dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
  220. }
  221. return 0;
  222. }
  223. max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
  224. /*
  225. * Translate the device's DMA mask to a PFN limit. This
  226. * PFN number includes the page which we can DMA to.
  227. */
  228. if (dma_to_pfn(dev, mask) < max_dma_pfn) {
  229. if (warn)
  230. 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",
  231. mask,
  232. dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
  233. max_dma_pfn + 1);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. static u64 get_coherent_dma_mask(struct device *dev)
  239. {
  240. u64 mask = (u64)DMA_BIT_MASK(32);
  241. if (dev) {
  242. mask = dev->coherent_dma_mask;
  243. /*
  244. * Sanity check the DMA mask - it must be non-zero, and
  245. * must be able to be satisfied by a DMA allocation.
  246. */
  247. if (mask == 0) {
  248. dev_warn(dev, "coherent DMA mask is unset\n");
  249. return 0;
  250. }
  251. if (!__dma_supported(dev, mask, true))
  252. return 0;
  253. }
  254. return mask;
  255. }
  256. static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
  257. {
  258. /*
  259. * Ensure that the allocated pages are zeroed, and that any data
  260. * lurking in the kernel direct-mapped region is invalidated.
  261. */
  262. if (PageHighMem(page)) {
  263. phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
  264. phys_addr_t end = base + size;
  265. while (size > 0) {
  266. void *ptr = kmap_atomic(page);
  267. memset(ptr, 0, PAGE_SIZE);
  268. if (coherent_flag != COHERENT)
  269. dmac_flush_range(ptr, ptr + PAGE_SIZE);
  270. kunmap_atomic(ptr);
  271. page++;
  272. size -= PAGE_SIZE;
  273. }
  274. if (coherent_flag != COHERENT)
  275. outer_flush_range(base, end);
  276. } else {
  277. void *ptr = page_address(page);
  278. memset(ptr, 0, size);
  279. if (coherent_flag != COHERENT) {
  280. dmac_flush_range(ptr, ptr + size);
  281. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  282. }
  283. }
  284. }
  285. /*
  286. * Allocate a DMA buffer for 'dev' of size 'size' using the
  287. * specified gfp mask. Note that 'size' must be page aligned.
  288. */
  289. static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
  290. gfp_t gfp, int coherent_flag)
  291. {
  292. unsigned long order = get_order(size);
  293. struct page *page, *p, *e;
  294. page = alloc_pages(gfp, order);
  295. if (!page)
  296. return NULL;
  297. /*
  298. * Now split the huge page and free the excess pages
  299. */
  300. split_page(page, order);
  301. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  302. __free_page(p);
  303. __dma_clear_buffer(page, size, coherent_flag);
  304. return page;
  305. }
  306. /*
  307. * Free a DMA buffer. 'size' must be page aligned.
  308. */
  309. static void __dma_free_buffer(struct page *page, size_t size)
  310. {
  311. struct page *e = page + (size >> PAGE_SHIFT);
  312. while (page < e) {
  313. __free_page(page);
  314. page++;
  315. }
  316. }
  317. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  318. pgprot_t prot, struct page **ret_page,
  319. const void *caller, bool want_vaddr,
  320. int coherent_flag, gfp_t gfp);
  321. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  322. pgprot_t prot, struct page **ret_page,
  323. const void *caller, bool want_vaddr);
  324. static void *
  325. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
  326. const void *caller)
  327. {
  328. /*
  329. * DMA allocation can be mapped to user space, so lets
  330. * set VM_USERMAP flags too.
  331. */
  332. return dma_common_contiguous_remap(page, size,
  333. VM_ARM_DMA_CONSISTENT | VM_USERMAP,
  334. prot, caller);
  335. }
  336. static void __dma_free_remap(void *cpu_addr, size_t size)
  337. {
  338. dma_common_free_remap(cpu_addr, size,
  339. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  340. }
  341. #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
  342. static struct gen_pool *atomic_pool __ro_after_init;
  343. static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
  344. static int __init early_coherent_pool(char *p)
  345. {
  346. atomic_pool_size = memparse(p, &p);
  347. return 0;
  348. }
  349. early_param("coherent_pool", early_coherent_pool);
  350. /*
  351. * Initialise the coherent pool for atomic allocations.
  352. */
  353. static int __init atomic_pool_init(void)
  354. {
  355. pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
  356. gfp_t gfp = GFP_KERNEL | GFP_DMA;
  357. struct page *page;
  358. void *ptr;
  359. atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
  360. if (!atomic_pool)
  361. goto out;
  362. /*
  363. * The atomic pool is only used for non-coherent allocations
  364. * so we must pass NORMAL for coherent_flag.
  365. */
  366. if (dev_get_cma_area(NULL))
  367. ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
  368. &page, atomic_pool_init, true, NORMAL,
  369. GFP_KERNEL);
  370. else
  371. ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
  372. &page, atomic_pool_init, true);
  373. if (ptr) {
  374. int ret;
  375. ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
  376. page_to_phys(page),
  377. atomic_pool_size, -1);
  378. if (ret)
  379. goto destroy_genpool;
  380. gen_pool_set_algo(atomic_pool,
  381. gen_pool_first_fit_order_align,
  382. NULL);
  383. pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
  384. atomic_pool_size / 1024);
  385. return 0;
  386. }
  387. destroy_genpool:
  388. gen_pool_destroy(atomic_pool);
  389. atomic_pool = NULL;
  390. out:
  391. pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
  392. atomic_pool_size / 1024);
  393. return -ENOMEM;
  394. }
  395. /*
  396. * CMA is activated by core_initcall, so we must be called after it.
  397. */
  398. postcore_initcall(atomic_pool_init);
  399. struct dma_contig_early_reserve {
  400. phys_addr_t base;
  401. unsigned long size;
  402. };
  403. static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
  404. static int dma_mmu_remap_num __initdata;
  405. void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  406. {
  407. dma_mmu_remap[dma_mmu_remap_num].base = base;
  408. dma_mmu_remap[dma_mmu_remap_num].size = size;
  409. dma_mmu_remap_num++;
  410. }
  411. void __init dma_contiguous_remap(void)
  412. {
  413. int i;
  414. if (!dma_mmu_remap_num)
  415. return;
  416. /* call flush_cache_all() since CMA area would be large enough */
  417. flush_cache_all();
  418. for (i = 0; i < dma_mmu_remap_num; i++) {
  419. phys_addr_t start = dma_mmu_remap[i].base;
  420. phys_addr_t end = start + dma_mmu_remap[i].size;
  421. struct map_desc map;
  422. unsigned long addr;
  423. if (end > arm_lowmem_limit)
  424. end = arm_lowmem_limit;
  425. if (start >= end)
  426. continue;
  427. map.pfn = __phys_to_pfn(start);
  428. map.virtual = __phys_to_virt(start);
  429. map.length = end - start;
  430. map.type = MT_MEMORY_DMA_READY;
  431. /*
  432. * Clear previous low-memory mapping to ensure that the
  433. * TLB does not see any conflicting entries, then flush
  434. * the TLB of the old entries before creating new mappings.
  435. *
  436. * This ensures that any speculatively loaded TLB entries
  437. * (even though they may be rare) can not cause any problems,
  438. * and ensures that this code is architecturally compliant.
  439. */
  440. for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
  441. addr += PMD_SIZE)
  442. pmd_clear(pmd_off_k(addr));
  443. flush_tlb_kernel_range(__phys_to_virt(start),
  444. __phys_to_virt(end));
  445. /*
  446. * All the memory in CMA region will be on ZONE_MOVABLE.
  447. * If that zone is considered as highmem, the memory in CMA
  448. * region is also considered as highmem even if it's
  449. * physical address belong to lowmem. In this case,
  450. * re-mapping isn't required.
  451. */
  452. if (!is_highmem_idx(ZONE_MOVABLE))
  453. iotable_init(&map, 1);
  454. }
  455. }
  456. static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
  457. void *data)
  458. {
  459. struct page *page = virt_to_page(addr);
  460. pgprot_t prot = *(pgprot_t *)data;
  461. set_pte_ext(pte, mk_pte(page, prot), 0);
  462. return 0;
  463. }
  464. static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
  465. {
  466. unsigned long start = (unsigned long) page_address(page);
  467. unsigned end = start + size;
  468. apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
  469. flush_tlb_kernel_range(start, end);
  470. }
  471. static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
  472. pgprot_t prot, struct page **ret_page,
  473. const void *caller, bool want_vaddr)
  474. {
  475. struct page *page;
  476. void *ptr = NULL;
  477. /*
  478. * __alloc_remap_buffer is only called when the device is
  479. * non-coherent
  480. */
  481. page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
  482. if (!page)
  483. return NULL;
  484. if (!want_vaddr)
  485. goto out;
  486. ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
  487. if (!ptr) {
  488. __dma_free_buffer(page, size);
  489. return NULL;
  490. }
  491. out:
  492. *ret_page = page;
  493. return ptr;
  494. }
  495. static void *__alloc_from_pool(size_t size, struct page **ret_page)
  496. {
  497. unsigned long val;
  498. void *ptr = NULL;
  499. if (!atomic_pool) {
  500. WARN(1, "coherent pool not initialised!\n");
  501. return NULL;
  502. }
  503. val = gen_pool_alloc(atomic_pool, size);
  504. if (val) {
  505. phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
  506. *ret_page = phys_to_page(phys);
  507. ptr = (void *)val;
  508. }
  509. return ptr;
  510. }
  511. static bool __in_atomic_pool(void *start, size_t size)
  512. {
  513. return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
  514. }
  515. static int __free_from_pool(void *start, size_t size)
  516. {
  517. if (!__in_atomic_pool(start, size))
  518. return 0;
  519. gen_pool_free(atomic_pool, (unsigned long)start, size);
  520. return 1;
  521. }
  522. static void *__alloc_from_contiguous(struct device *dev, size_t size,
  523. pgprot_t prot, struct page **ret_page,
  524. const void *caller, bool want_vaddr,
  525. int coherent_flag, gfp_t gfp)
  526. {
  527. unsigned long order = get_order(size);
  528. size_t count = size >> PAGE_SHIFT;
  529. struct page *page;
  530. void *ptr = NULL;
  531. page = dma_alloc_from_contiguous(dev, count, order, gfp);
  532. if (!page)
  533. return NULL;
  534. __dma_clear_buffer(page, size, coherent_flag);
  535. if (!want_vaddr)
  536. goto out;
  537. if (PageHighMem(page)) {
  538. ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
  539. if (!ptr) {
  540. dma_release_from_contiguous(dev, page, count);
  541. return NULL;
  542. }
  543. } else {
  544. __dma_remap(page, size, prot);
  545. ptr = page_address(page);
  546. }
  547. out:
  548. *ret_page = page;
  549. return ptr;
  550. }
  551. static void __free_from_contiguous(struct device *dev, struct page *page,
  552. void *cpu_addr, size_t size, bool want_vaddr)
  553. {
  554. if (want_vaddr) {
  555. if (PageHighMem(page))
  556. __dma_free_remap(cpu_addr, size);
  557. else
  558. __dma_remap(page, size, PAGE_KERNEL);
  559. }
  560. dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
  561. }
  562. static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
  563. {
  564. prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
  565. pgprot_writecombine(prot) :
  566. pgprot_dmacoherent(prot);
  567. return prot;
  568. }
  569. static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
  570. struct page **ret_page)
  571. {
  572. struct page *page;
  573. /* __alloc_simple_buffer is only called when the device is coherent */
  574. page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
  575. if (!page)
  576. return NULL;
  577. *ret_page = page;
  578. return page_address(page);
  579. }
  580. static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
  581. struct page **ret_page)
  582. {
  583. return __alloc_simple_buffer(args->dev, args->size, args->gfp,
  584. ret_page);
  585. }
  586. static void simple_allocator_free(struct arm_dma_free_args *args)
  587. {
  588. __dma_free_buffer(args->page, args->size);
  589. }
  590. static struct arm_dma_allocator simple_allocator = {
  591. .alloc = simple_allocator_alloc,
  592. .free = simple_allocator_free,
  593. };
  594. static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
  595. struct page **ret_page)
  596. {
  597. return __alloc_from_contiguous(args->dev, args->size, args->prot,
  598. ret_page, args->caller,
  599. args->want_vaddr, args->coherent_flag,
  600. args->gfp);
  601. }
  602. static void cma_allocator_free(struct arm_dma_free_args *args)
  603. {
  604. __free_from_contiguous(args->dev, args->page, args->cpu_addr,
  605. args->size, args->want_vaddr);
  606. }
  607. static struct arm_dma_allocator cma_allocator = {
  608. .alloc = cma_allocator_alloc,
  609. .free = cma_allocator_free,
  610. };
  611. static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
  612. struct page **ret_page)
  613. {
  614. return __alloc_from_pool(args->size, ret_page);
  615. }
  616. static void pool_allocator_free(struct arm_dma_free_args *args)
  617. {
  618. __free_from_pool(args->cpu_addr, args->size);
  619. }
  620. static struct arm_dma_allocator pool_allocator = {
  621. .alloc = pool_allocator_alloc,
  622. .free = pool_allocator_free,
  623. };
  624. static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
  625. struct page **ret_page)
  626. {
  627. return __alloc_remap_buffer(args->dev, args->size, args->gfp,
  628. args->prot, ret_page, args->caller,
  629. args->want_vaddr);
  630. }
  631. static void remap_allocator_free(struct arm_dma_free_args *args)
  632. {
  633. if (args->want_vaddr)
  634. __dma_free_remap(args->cpu_addr, args->size);
  635. __dma_free_buffer(args->page, args->size);
  636. }
  637. static struct arm_dma_allocator remap_allocator = {
  638. .alloc = remap_allocator_alloc,
  639. .free = remap_allocator_free,
  640. };
  641. static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  642. gfp_t gfp, pgprot_t prot, bool is_coherent,
  643. unsigned long attrs, const void *caller)
  644. {
  645. u64 mask = get_coherent_dma_mask(dev);
  646. struct page *page = NULL;
  647. void *addr;
  648. bool allowblock, cma;
  649. struct arm_dma_buffer *buf;
  650. struct arm_dma_alloc_args args = {
  651. .dev = dev,
  652. .size = PAGE_ALIGN(size),
  653. .gfp = gfp,
  654. .prot = prot,
  655. .caller = caller,
  656. .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
  657. .coherent_flag = is_coherent ? COHERENT : NORMAL,
  658. };
  659. #ifdef CONFIG_DMA_API_DEBUG
  660. u64 limit = (mask + 1) & ~mask;
  661. if (limit && size >= limit) {
  662. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  663. size, mask);
  664. return NULL;
  665. }
  666. #endif
  667. if (!mask)
  668. return NULL;
  669. buf = kzalloc(sizeof(*buf),
  670. gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
  671. if (!buf)
  672. return NULL;
  673. if (mask < 0xffffffffULL)
  674. gfp |= GFP_DMA;
  675. /*
  676. * Following is a work-around (a.k.a. hack) to prevent pages
  677. * with __GFP_COMP being passed to split_page() which cannot
  678. * handle them. The real problem is that this flag probably
  679. * should be 0 on ARM as it is not supported on this
  680. * platform; see CONFIG_HUGETLBFS.
  681. */
  682. gfp &= ~(__GFP_COMP);
  683. args.gfp = gfp;
  684. *handle = ARM_MAPPING_ERROR;
  685. allowblock = gfpflags_allow_blocking(gfp);
  686. cma = allowblock ? dev_get_cma_area(dev) : false;
  687. if (cma)
  688. buf->allocator = &cma_allocator;
  689. else if (is_coherent)
  690. buf->allocator = &simple_allocator;
  691. else if (allowblock)
  692. buf->allocator = &remap_allocator;
  693. else
  694. buf->allocator = &pool_allocator;
  695. addr = buf->allocator->alloc(&args, &page);
  696. if (page) {
  697. unsigned long flags;
  698. *handle = pfn_to_dma(dev, page_to_pfn(page));
  699. buf->virt = args.want_vaddr ? addr : page;
  700. spin_lock_irqsave(&arm_dma_bufs_lock, flags);
  701. list_add(&buf->list, &arm_dma_bufs);
  702. spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
  703. } else {
  704. kfree(buf);
  705. }
  706. return args.want_vaddr ? addr : page;
  707. }
  708. /*
  709. * Allocate DMA-coherent memory space and return both the kernel remapped
  710. * virtual and bus address for that space.
  711. */
  712. void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  713. gfp_t gfp, unsigned long attrs)
  714. {
  715. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  716. return __dma_alloc(dev, size, handle, gfp, prot, false,
  717. attrs, __builtin_return_address(0));
  718. }
  719. static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
  720. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  721. {
  722. return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
  723. attrs, __builtin_return_address(0));
  724. }
  725. static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  726. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  727. unsigned long attrs)
  728. {
  729. int ret;
  730. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  731. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  732. unsigned long pfn = dma_to_pfn(dev, dma_addr);
  733. unsigned long off = vma->vm_pgoff;
  734. if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  735. return ret;
  736. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  737. ret = remap_pfn_range(vma, vma->vm_start,
  738. pfn + off,
  739. vma->vm_end - vma->vm_start,
  740. vma->vm_page_prot);
  741. }
  742. return ret;
  743. }
  744. /*
  745. * Create userspace mapping for the DMA-coherent memory.
  746. */
  747. static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  748. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  749. unsigned long attrs)
  750. {
  751. return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  752. }
  753. int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  754. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  755. unsigned long attrs)
  756. {
  757. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  758. return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  759. }
  760. /*
  761. * Free a buffer as defined by the above mapping.
  762. */
  763. static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  764. dma_addr_t handle, unsigned long attrs,
  765. bool is_coherent)
  766. {
  767. struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
  768. struct arm_dma_buffer *buf;
  769. struct arm_dma_free_args args = {
  770. .dev = dev,
  771. .size = PAGE_ALIGN(size),
  772. .cpu_addr = cpu_addr,
  773. .page = page,
  774. .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
  775. };
  776. buf = arm_dma_buffer_find(cpu_addr);
  777. if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
  778. return;
  779. buf->allocator->free(&args);
  780. kfree(buf);
  781. }
  782. void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  783. dma_addr_t handle, unsigned long attrs)
  784. {
  785. __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
  786. }
  787. static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
  788. dma_addr_t handle, unsigned long attrs)
  789. {
  790. __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
  791. }
  792. /*
  793. * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
  794. * that the intention is to allow exporting memory allocated via the
  795. * coherent DMA APIs through the dma_buf API, which only accepts a
  796. * scattertable. This presents a couple of problems:
  797. * 1. Not all memory allocated via the coherent DMA APIs is backed by
  798. * a struct page
  799. * 2. Passing coherent DMA memory into the streaming APIs is not allowed
  800. * as we will try to flush the memory through a different alias to that
  801. * actually being used (and the flushes are redundant.)
  802. */
  803. int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  804. void *cpu_addr, dma_addr_t handle, size_t size,
  805. unsigned long attrs)
  806. {
  807. unsigned long pfn = dma_to_pfn(dev, handle);
  808. struct page *page;
  809. int ret;
  810. /* If the PFN is not valid, we do not have a struct page */
  811. if (!pfn_valid(pfn))
  812. return -ENXIO;
  813. page = pfn_to_page(pfn);
  814. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  815. if (unlikely(ret))
  816. return ret;
  817. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  818. return 0;
  819. }
  820. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  821. size_t size, enum dma_data_direction dir,
  822. void (*op)(const void *, size_t, int))
  823. {
  824. unsigned long pfn;
  825. size_t left = size;
  826. pfn = page_to_pfn(page) + offset / PAGE_SIZE;
  827. offset %= PAGE_SIZE;
  828. /*
  829. * A single sg entry may refer to multiple physically contiguous
  830. * pages. But we still need to process highmem pages individually.
  831. * If highmem is not configured then the bulk of this loop gets
  832. * optimized out.
  833. */
  834. do {
  835. size_t len = left;
  836. void *vaddr;
  837. page = pfn_to_page(pfn);
  838. if (PageHighMem(page)) {
  839. if (len + offset > PAGE_SIZE)
  840. len = PAGE_SIZE - offset;
  841. if (cache_is_vipt_nonaliasing()) {
  842. vaddr = kmap_atomic(page);
  843. op(vaddr + offset, len, dir);
  844. kunmap_atomic(vaddr);
  845. } else {
  846. vaddr = kmap_high_get(page);
  847. if (vaddr) {
  848. op(vaddr + offset, len, dir);
  849. kunmap_high(page);
  850. }
  851. }
  852. } else {
  853. vaddr = page_address(page) + offset;
  854. op(vaddr, len, dir);
  855. }
  856. offset = 0;
  857. pfn++;
  858. left -= len;
  859. } while (left);
  860. }
  861. /*
  862. * Make an area consistent for devices.
  863. * Note: Drivers should NOT use this function directly, as it will break
  864. * platforms with CONFIG_DMABOUNCE.
  865. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  866. */
  867. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  868. size_t size, enum dma_data_direction dir)
  869. {
  870. phys_addr_t paddr;
  871. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  872. paddr = page_to_phys(page) + off;
  873. if (dir == DMA_FROM_DEVICE) {
  874. outer_inv_range(paddr, paddr + size);
  875. } else {
  876. outer_clean_range(paddr, paddr + size);
  877. }
  878. /* FIXME: non-speculating: flush on bidirectional mappings? */
  879. }
  880. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  881. size_t size, enum dma_data_direction dir)
  882. {
  883. phys_addr_t paddr = page_to_phys(page) + off;
  884. /* FIXME: non-speculating: not required */
  885. /* in any case, don't bother invalidating if DMA to device */
  886. if (dir != DMA_TO_DEVICE) {
  887. outer_inv_range(paddr, paddr + size);
  888. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  889. }
  890. /*
  891. * Mark the D-cache clean for these pages to avoid extra flushing.
  892. */
  893. if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
  894. unsigned long pfn;
  895. size_t left = size;
  896. pfn = page_to_pfn(page) + off / PAGE_SIZE;
  897. off %= PAGE_SIZE;
  898. if (off) {
  899. pfn++;
  900. left -= PAGE_SIZE - off;
  901. }
  902. while (left >= PAGE_SIZE) {
  903. page = pfn_to_page(pfn++);
  904. set_bit(PG_dcache_clean, &page->flags);
  905. left -= PAGE_SIZE;
  906. }
  907. }
  908. }
  909. /**
  910. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  911. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  912. * @sg: list of buffers
  913. * @nents: number of buffers to map
  914. * @dir: DMA transfer direction
  915. *
  916. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  917. * This is the scatter-gather version of the dma_map_single interface.
  918. * Here the scatter gather list elements are each tagged with the
  919. * appropriate dma address and length. They are obtained via
  920. * sg_dma_{address,length}.
  921. *
  922. * Device ownership issues as mentioned for dma_map_single are the same
  923. * here.
  924. */
  925. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  926. enum dma_data_direction dir, unsigned long attrs)
  927. {
  928. const struct dma_map_ops *ops = get_dma_ops(dev);
  929. struct scatterlist *s;
  930. int i, j;
  931. for_each_sg(sg, s, nents, i) {
  932. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  933. s->dma_length = s->length;
  934. #endif
  935. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  936. s->length, dir, attrs);
  937. if (dma_mapping_error(dev, s->dma_address))
  938. goto bad_mapping;
  939. }
  940. return nents;
  941. bad_mapping:
  942. for_each_sg(sg, s, i, j)
  943. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  944. return 0;
  945. }
  946. /**
  947. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  948. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  949. * @sg: list of buffers
  950. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  951. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  952. *
  953. * Unmap a set of streaming mode DMA translations. Again, CPU access
  954. * rules concerning calls here are the same as for dma_unmap_single().
  955. */
  956. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  957. enum dma_data_direction dir, unsigned long attrs)
  958. {
  959. const struct dma_map_ops *ops = get_dma_ops(dev);
  960. struct scatterlist *s;
  961. int i;
  962. for_each_sg(sg, s, nents, i)
  963. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  964. }
  965. /**
  966. * arm_dma_sync_sg_for_cpu
  967. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  968. * @sg: list of buffers
  969. * @nents: number of buffers to map (returned from dma_map_sg)
  970. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  971. */
  972. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  973. int nents, enum dma_data_direction dir)
  974. {
  975. const struct dma_map_ops *ops = get_dma_ops(dev);
  976. struct scatterlist *s;
  977. int i;
  978. for_each_sg(sg, s, nents, i)
  979. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  980. dir);
  981. }
  982. /**
  983. * arm_dma_sync_sg_for_device
  984. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  985. * @sg: list of buffers
  986. * @nents: number of buffers to map (returned from dma_map_sg)
  987. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  988. */
  989. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  990. int nents, enum dma_data_direction dir)
  991. {
  992. const struct dma_map_ops *ops = get_dma_ops(dev);
  993. struct scatterlist *s;
  994. int i;
  995. for_each_sg(sg, s, nents, i)
  996. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  997. dir);
  998. }
  999. /*
  1000. * Return whether the given device DMA address mask can be supported
  1001. * properly. For example, if your device can only drive the low 24-bits
  1002. * during bus mastering, then you would pass 0x00ffffff as the mask
  1003. * to this function.
  1004. */
  1005. int arm_dma_supported(struct device *dev, u64 mask)
  1006. {
  1007. return __dma_supported(dev, mask, false);
  1008. }
  1009. #ifdef CONFIG_ARM_DMA_USE_IOMMU
  1010. static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
  1011. {
  1012. int prot = 0;
  1013. if (attrs & DMA_ATTR_PRIVILEGED)
  1014. prot |= IOMMU_PRIV;
  1015. switch (dir) {
  1016. case DMA_BIDIRECTIONAL:
  1017. return prot | IOMMU_READ | IOMMU_WRITE;
  1018. case DMA_TO_DEVICE:
  1019. return prot | IOMMU_READ;
  1020. case DMA_FROM_DEVICE:
  1021. return prot | IOMMU_WRITE;
  1022. default:
  1023. return prot;
  1024. }
  1025. }
  1026. /* IOMMU */
  1027. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
  1028. static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
  1029. size_t size)
  1030. {
  1031. unsigned int order = get_order(size);
  1032. unsigned int align = 0;
  1033. unsigned int count, start;
  1034. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1035. unsigned long flags;
  1036. dma_addr_t iova;
  1037. int i;
  1038. if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
  1039. order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
  1040. count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1041. align = (1 << order) - 1;
  1042. spin_lock_irqsave(&mapping->lock, flags);
  1043. for (i = 0; i < mapping->nr_bitmaps; i++) {
  1044. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1045. mapping->bits, 0, count, align);
  1046. if (start > mapping->bits)
  1047. continue;
  1048. bitmap_set(mapping->bitmaps[i], start, count);
  1049. break;
  1050. }
  1051. /*
  1052. * No unused range found. Try to extend the existing mapping
  1053. * and perform a second attempt to reserve an IO virtual
  1054. * address range of size bytes.
  1055. */
  1056. if (i == mapping->nr_bitmaps) {
  1057. if (extend_iommu_mapping(mapping)) {
  1058. spin_unlock_irqrestore(&mapping->lock, flags);
  1059. return ARM_MAPPING_ERROR;
  1060. }
  1061. start = bitmap_find_next_zero_area(mapping->bitmaps[i],
  1062. mapping->bits, 0, count, align);
  1063. if (start > mapping->bits) {
  1064. spin_unlock_irqrestore(&mapping->lock, flags);
  1065. return ARM_MAPPING_ERROR;
  1066. }
  1067. bitmap_set(mapping->bitmaps[i], start, count);
  1068. }
  1069. spin_unlock_irqrestore(&mapping->lock, flags);
  1070. iova = mapping->base + (mapping_size * i);
  1071. iova += start << PAGE_SHIFT;
  1072. return iova;
  1073. }
  1074. static inline void __free_iova(struct dma_iommu_mapping *mapping,
  1075. dma_addr_t addr, size_t size)
  1076. {
  1077. unsigned int start, count;
  1078. size_t mapping_size = mapping->bits << PAGE_SHIFT;
  1079. unsigned long flags;
  1080. dma_addr_t bitmap_base;
  1081. u32 bitmap_index;
  1082. if (!size)
  1083. return;
  1084. bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
  1085. BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
  1086. bitmap_base = mapping->base + mapping_size * bitmap_index;
  1087. start = (addr - bitmap_base) >> PAGE_SHIFT;
  1088. if (addr + size > bitmap_base + mapping_size) {
  1089. /*
  1090. * The address range to be freed reaches into the iova
  1091. * range of the next bitmap. This should not happen as
  1092. * we don't allow this in __alloc_iova (at the
  1093. * moment).
  1094. */
  1095. BUG();
  1096. } else
  1097. count = size >> PAGE_SHIFT;
  1098. spin_lock_irqsave(&mapping->lock, flags);
  1099. bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
  1100. spin_unlock_irqrestore(&mapping->lock, flags);
  1101. }
  1102. /* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
  1103. static const int iommu_order_array[] = { 9, 8, 4, 0 };
  1104. static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
  1105. gfp_t gfp, unsigned long attrs,
  1106. int coherent_flag)
  1107. {
  1108. struct page **pages;
  1109. int count = size >> PAGE_SHIFT;
  1110. int array_size = count * sizeof(struct page *);
  1111. int i = 0;
  1112. int order_idx = 0;
  1113. if (array_size <= PAGE_SIZE)
  1114. pages = kzalloc(array_size, GFP_KERNEL);
  1115. else
  1116. pages = vzalloc(array_size);
  1117. if (!pages)
  1118. return NULL;
  1119. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
  1120. {
  1121. unsigned long order = get_order(size);
  1122. struct page *page;
  1123. page = dma_alloc_from_contiguous(dev, count, order, gfp);
  1124. if (!page)
  1125. goto error;
  1126. __dma_clear_buffer(page, size, coherent_flag);
  1127. for (i = 0; i < count; i++)
  1128. pages[i] = page + i;
  1129. return pages;
  1130. }
  1131. /* Go straight to 4K chunks if caller says it's OK. */
  1132. if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
  1133. order_idx = ARRAY_SIZE(iommu_order_array) - 1;
  1134. /*
  1135. * IOMMU can map any pages, so himem can also be used here
  1136. */
  1137. gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
  1138. while (count) {
  1139. int j, order;
  1140. order = iommu_order_array[order_idx];
  1141. /* Drop down when we get small */
  1142. if (__fls(count) < order) {
  1143. order_idx++;
  1144. continue;
  1145. }
  1146. if (order) {
  1147. /* See if it's easy to allocate a high-order chunk */
  1148. pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
  1149. /* Go down a notch at first sign of pressure */
  1150. if (!pages[i]) {
  1151. order_idx++;
  1152. continue;
  1153. }
  1154. } else {
  1155. pages[i] = alloc_pages(gfp, 0);
  1156. if (!pages[i])
  1157. goto error;
  1158. }
  1159. if (order) {
  1160. split_page(pages[i], order);
  1161. j = 1 << order;
  1162. while (--j)
  1163. pages[i + j] = pages[i] + j;
  1164. }
  1165. __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
  1166. i += 1 << order;
  1167. count -= 1 << order;
  1168. }
  1169. return pages;
  1170. error:
  1171. while (i--)
  1172. if (pages[i])
  1173. __free_pages(pages[i], 0);
  1174. kvfree(pages);
  1175. return NULL;
  1176. }
  1177. static int __iommu_free_buffer(struct device *dev, struct page **pages,
  1178. size_t size, unsigned long attrs)
  1179. {
  1180. int count = size >> PAGE_SHIFT;
  1181. int i;
  1182. if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  1183. dma_release_from_contiguous(dev, pages[0], count);
  1184. } else {
  1185. for (i = 0; i < count; i++)
  1186. if (pages[i])
  1187. __free_pages(pages[i], 0);
  1188. }
  1189. kvfree(pages);
  1190. return 0;
  1191. }
  1192. /*
  1193. * Create a CPU mapping for a specified pages
  1194. */
  1195. static void *
  1196. __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
  1197. const void *caller)
  1198. {
  1199. return dma_common_pages_remap(pages, size,
  1200. VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
  1201. }
  1202. /*
  1203. * Create a mapping in device IO address space for specified pages
  1204. */
  1205. static dma_addr_t
  1206. __iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
  1207. unsigned long attrs)
  1208. {
  1209. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1210. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1211. dma_addr_t dma_addr, iova;
  1212. int i;
  1213. dma_addr = __alloc_iova(mapping, size);
  1214. if (dma_addr == ARM_MAPPING_ERROR)
  1215. return dma_addr;
  1216. iova = dma_addr;
  1217. for (i = 0; i < count; ) {
  1218. int ret;
  1219. unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
  1220. phys_addr_t phys = page_to_phys(pages[i]);
  1221. unsigned int len, j;
  1222. for (j = i + 1; j < count; j++, next_pfn++)
  1223. if (page_to_pfn(pages[j]) != next_pfn)
  1224. break;
  1225. len = (j - i) << PAGE_SHIFT;
  1226. ret = iommu_map(mapping->domain, iova, phys, len,
  1227. __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
  1228. if (ret < 0)
  1229. goto fail;
  1230. iova += len;
  1231. i = j;
  1232. }
  1233. return dma_addr;
  1234. fail:
  1235. iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
  1236. __free_iova(mapping, dma_addr, size);
  1237. return ARM_MAPPING_ERROR;
  1238. }
  1239. static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
  1240. {
  1241. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1242. /*
  1243. * add optional in-page offset from iova to size and align
  1244. * result to page size
  1245. */
  1246. size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
  1247. iova &= PAGE_MASK;
  1248. iommu_unmap(mapping->domain, iova, size);
  1249. __free_iova(mapping, iova, size);
  1250. return 0;
  1251. }
  1252. static struct page **__atomic_get_pages(void *addr)
  1253. {
  1254. struct page *page;
  1255. phys_addr_t phys;
  1256. phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
  1257. page = phys_to_page(phys);
  1258. return (struct page **)page;
  1259. }
  1260. static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
  1261. {
  1262. struct vm_struct *area;
  1263. if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
  1264. return __atomic_get_pages(cpu_addr);
  1265. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1266. return cpu_addr;
  1267. area = find_vm_area(cpu_addr);
  1268. if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
  1269. return area->pages;
  1270. return NULL;
  1271. }
  1272. static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
  1273. dma_addr_t *handle, int coherent_flag,
  1274. unsigned long attrs)
  1275. {
  1276. struct page *page;
  1277. void *addr;
  1278. if (coherent_flag == COHERENT)
  1279. addr = __alloc_simple_buffer(dev, size, gfp, &page);
  1280. else
  1281. addr = __alloc_from_pool(size, &page);
  1282. if (!addr)
  1283. return NULL;
  1284. *handle = __iommu_create_mapping(dev, &page, size, attrs);
  1285. if (*handle == ARM_MAPPING_ERROR)
  1286. goto err_mapping;
  1287. return addr;
  1288. err_mapping:
  1289. __free_from_pool(addr, size);
  1290. return NULL;
  1291. }
  1292. static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
  1293. dma_addr_t handle, size_t size, int coherent_flag)
  1294. {
  1295. __iommu_remove_mapping(dev, handle, size);
  1296. if (coherent_flag == COHERENT)
  1297. __dma_free_buffer(virt_to_page(cpu_addr), size);
  1298. else
  1299. __free_from_pool(cpu_addr, size);
  1300. }
  1301. static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1302. dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
  1303. int coherent_flag)
  1304. {
  1305. pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
  1306. struct page **pages;
  1307. void *addr = NULL;
  1308. *handle = ARM_MAPPING_ERROR;
  1309. size = PAGE_ALIGN(size);
  1310. if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
  1311. return __iommu_alloc_simple(dev, size, gfp, handle,
  1312. coherent_flag, attrs);
  1313. /*
  1314. * Following is a work-around (a.k.a. hack) to prevent pages
  1315. * with __GFP_COMP being passed to split_page() which cannot
  1316. * handle them. The real problem is that this flag probably
  1317. * should be 0 on ARM as it is not supported on this
  1318. * platform; see CONFIG_HUGETLBFS.
  1319. */
  1320. gfp &= ~(__GFP_COMP);
  1321. pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
  1322. if (!pages)
  1323. return NULL;
  1324. *handle = __iommu_create_mapping(dev, pages, size, attrs);
  1325. if (*handle == ARM_MAPPING_ERROR)
  1326. goto err_buffer;
  1327. if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
  1328. return pages;
  1329. addr = __iommu_alloc_remap(pages, size, gfp, prot,
  1330. __builtin_return_address(0));
  1331. if (!addr)
  1332. goto err_mapping;
  1333. return addr;
  1334. err_mapping:
  1335. __iommu_remove_mapping(dev, *handle, size);
  1336. err_buffer:
  1337. __iommu_free_buffer(dev, pages, size, attrs);
  1338. return NULL;
  1339. }
  1340. static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
  1341. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1342. {
  1343. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
  1344. }
  1345. static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
  1346. dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
  1347. {
  1348. return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
  1349. }
  1350. static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  1351. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  1352. unsigned long attrs)
  1353. {
  1354. unsigned long uaddr = vma->vm_start;
  1355. unsigned long usize = vma->vm_end - vma->vm_start;
  1356. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1357. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1358. unsigned long off = vma->vm_pgoff;
  1359. if (!pages)
  1360. return -ENXIO;
  1361. if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
  1362. return -ENXIO;
  1363. pages += off;
  1364. do {
  1365. int ret = vm_insert_page(vma, uaddr, *pages++);
  1366. if (ret) {
  1367. pr_err("Remapping memory failed: %d\n", ret);
  1368. return ret;
  1369. }
  1370. uaddr += PAGE_SIZE;
  1371. usize -= PAGE_SIZE;
  1372. } while (usize > 0);
  1373. return 0;
  1374. }
  1375. static int arm_iommu_mmap_attrs(struct device *dev,
  1376. struct vm_area_struct *vma, void *cpu_addr,
  1377. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1378. {
  1379. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
  1380. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1381. }
  1382. static int arm_coherent_iommu_mmap_attrs(struct device *dev,
  1383. struct vm_area_struct *vma, void *cpu_addr,
  1384. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  1385. {
  1386. return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
  1387. }
  1388. /*
  1389. * free a page as defined by the above mapping.
  1390. * Must not be called with IRQs disabled.
  1391. */
  1392. void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  1393. dma_addr_t handle, unsigned long attrs, int coherent_flag)
  1394. {
  1395. struct page **pages;
  1396. size = PAGE_ALIGN(size);
  1397. if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
  1398. __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
  1399. return;
  1400. }
  1401. pages = __iommu_get_pages(cpu_addr, attrs);
  1402. if (!pages) {
  1403. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  1404. return;
  1405. }
  1406. if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
  1407. dma_common_free_remap(cpu_addr, size,
  1408. VM_ARM_DMA_CONSISTENT | VM_USERMAP);
  1409. }
  1410. __iommu_remove_mapping(dev, handle, size);
  1411. __iommu_free_buffer(dev, pages, size, attrs);
  1412. }
  1413. void arm_iommu_free_attrs(struct device *dev, size_t size,
  1414. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1415. {
  1416. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
  1417. }
  1418. void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
  1419. void *cpu_addr, dma_addr_t handle, unsigned long attrs)
  1420. {
  1421. __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
  1422. }
  1423. static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
  1424. void *cpu_addr, dma_addr_t dma_addr,
  1425. size_t size, unsigned long attrs)
  1426. {
  1427. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  1428. struct page **pages = __iommu_get_pages(cpu_addr, attrs);
  1429. if (!pages)
  1430. return -ENXIO;
  1431. return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
  1432. GFP_KERNEL);
  1433. }
  1434. /*
  1435. * Map a part of the scatter-gather list into contiguous io address space
  1436. */
  1437. static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
  1438. size_t size, dma_addr_t *handle,
  1439. enum dma_data_direction dir, unsigned long attrs,
  1440. bool is_coherent)
  1441. {
  1442. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1443. dma_addr_t iova, iova_base;
  1444. int ret = 0;
  1445. unsigned int count;
  1446. struct scatterlist *s;
  1447. int prot;
  1448. size = PAGE_ALIGN(size);
  1449. *handle = ARM_MAPPING_ERROR;
  1450. iova_base = iova = __alloc_iova(mapping, size);
  1451. if (iova == ARM_MAPPING_ERROR)
  1452. return -ENOMEM;
  1453. for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
  1454. phys_addr_t phys = page_to_phys(sg_page(s));
  1455. unsigned int len = PAGE_ALIGN(s->offset + s->length);
  1456. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1457. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1458. prot = __dma_info_to_prot(dir, attrs);
  1459. ret = iommu_map(mapping->domain, iova, phys, len, prot);
  1460. if (ret < 0)
  1461. goto fail;
  1462. count += len >> PAGE_SHIFT;
  1463. iova += len;
  1464. }
  1465. *handle = iova_base;
  1466. return 0;
  1467. fail:
  1468. iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
  1469. __free_iova(mapping, iova_base, size);
  1470. return ret;
  1471. }
  1472. static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  1473. enum dma_data_direction dir, unsigned long attrs,
  1474. bool is_coherent)
  1475. {
  1476. struct scatterlist *s = sg, *dma = sg, *start = sg;
  1477. int i, count = 0;
  1478. unsigned int offset = s->offset;
  1479. unsigned int size = s->offset + s->length;
  1480. unsigned int max = dma_get_max_seg_size(dev);
  1481. for (i = 1; i < nents; i++) {
  1482. s = sg_next(s);
  1483. s->dma_address = ARM_MAPPING_ERROR;
  1484. s->dma_length = 0;
  1485. if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
  1486. if (__map_sg_chunk(dev, start, size, &dma->dma_address,
  1487. dir, attrs, is_coherent) < 0)
  1488. goto bad_mapping;
  1489. dma->dma_address += offset;
  1490. dma->dma_length = size - offset;
  1491. size = offset = s->offset;
  1492. start = s;
  1493. dma = sg_next(dma);
  1494. count += 1;
  1495. }
  1496. size += s->length;
  1497. }
  1498. if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
  1499. is_coherent) < 0)
  1500. goto bad_mapping;
  1501. dma->dma_address += offset;
  1502. dma->dma_length = size - offset;
  1503. return count+1;
  1504. bad_mapping:
  1505. for_each_sg(sg, s, count, i)
  1506. __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
  1507. return 0;
  1508. }
  1509. /**
  1510. * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1511. * @dev: valid struct device pointer
  1512. * @sg: list of buffers
  1513. * @nents: number of buffers to map
  1514. * @dir: DMA transfer direction
  1515. *
  1516. * Map a set of i/o coherent buffers described by scatterlist in streaming
  1517. * mode for DMA. The scatter gather list elements are merged together (if
  1518. * possible) and tagged with the appropriate dma address and length. They are
  1519. * obtained via sg_dma_{address,length}.
  1520. */
  1521. int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1522. int nents, enum dma_data_direction dir, unsigned long attrs)
  1523. {
  1524. return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
  1525. }
  1526. /**
  1527. * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  1528. * @dev: valid struct device pointer
  1529. * @sg: list of buffers
  1530. * @nents: number of buffers to map
  1531. * @dir: DMA transfer direction
  1532. *
  1533. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  1534. * The scatter gather list elements are merged together (if possible) and
  1535. * tagged with the appropriate dma address and length. They are obtained via
  1536. * sg_dma_{address,length}.
  1537. */
  1538. int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
  1539. int nents, enum dma_data_direction dir, unsigned long attrs)
  1540. {
  1541. return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
  1542. }
  1543. static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1544. int nents, enum dma_data_direction dir,
  1545. unsigned long attrs, bool is_coherent)
  1546. {
  1547. struct scatterlist *s;
  1548. int i;
  1549. for_each_sg(sg, s, nents, i) {
  1550. if (sg_dma_len(s))
  1551. __iommu_remove_mapping(dev, sg_dma_address(s),
  1552. sg_dma_len(s));
  1553. if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1554. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  1555. s->length, dir);
  1556. }
  1557. }
  1558. /**
  1559. * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1560. * @dev: valid struct device pointer
  1561. * @sg: list of buffers
  1562. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1563. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1564. *
  1565. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1566. * rules concerning calls here are the same as for dma_unmap_single().
  1567. */
  1568. void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  1569. int nents, enum dma_data_direction dir,
  1570. unsigned long attrs)
  1571. {
  1572. __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
  1573. }
  1574. /**
  1575. * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  1576. * @dev: valid struct device pointer
  1577. * @sg: list of buffers
  1578. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  1579. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1580. *
  1581. * Unmap a set of streaming mode DMA translations. Again, CPU access
  1582. * rules concerning calls here are the same as for dma_unmap_single().
  1583. */
  1584. void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  1585. enum dma_data_direction dir,
  1586. unsigned long attrs)
  1587. {
  1588. __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
  1589. }
  1590. /**
  1591. * arm_iommu_sync_sg_for_cpu
  1592. * @dev: valid struct device pointer
  1593. * @sg: list of buffers
  1594. * @nents: number of buffers to map (returned from dma_map_sg)
  1595. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1596. */
  1597. void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1598. int nents, enum dma_data_direction dir)
  1599. {
  1600. struct scatterlist *s;
  1601. int i;
  1602. for_each_sg(sg, s, nents, i)
  1603. __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
  1604. }
  1605. /**
  1606. * arm_iommu_sync_sg_for_device
  1607. * @dev: valid struct device pointer
  1608. * @sg: list of buffers
  1609. * @nents: number of buffers to map (returned from dma_map_sg)
  1610. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  1611. */
  1612. void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1613. int nents, enum dma_data_direction dir)
  1614. {
  1615. struct scatterlist *s;
  1616. int i;
  1617. for_each_sg(sg, s, nents, i)
  1618. __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
  1619. }
  1620. /**
  1621. * arm_coherent_iommu_map_page
  1622. * @dev: valid struct device pointer
  1623. * @page: page that buffer resides in
  1624. * @offset: offset into page for start of buffer
  1625. * @size: size of buffer to map
  1626. * @dir: DMA transfer direction
  1627. *
  1628. * Coherent IOMMU aware version of arm_dma_map_page()
  1629. */
  1630. static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
  1631. unsigned long offset, size_t size, enum dma_data_direction dir,
  1632. unsigned long attrs)
  1633. {
  1634. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1635. dma_addr_t dma_addr;
  1636. int ret, prot, len = PAGE_ALIGN(size + offset);
  1637. dma_addr = __alloc_iova(mapping, len);
  1638. if (dma_addr == ARM_MAPPING_ERROR)
  1639. return dma_addr;
  1640. prot = __dma_info_to_prot(dir, attrs);
  1641. ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
  1642. if (ret < 0)
  1643. goto fail;
  1644. return dma_addr + offset;
  1645. fail:
  1646. __free_iova(mapping, dma_addr, len);
  1647. return ARM_MAPPING_ERROR;
  1648. }
  1649. /**
  1650. * arm_iommu_map_page
  1651. * @dev: valid struct device pointer
  1652. * @page: page that buffer resides in
  1653. * @offset: offset into page for start of buffer
  1654. * @size: size of buffer to map
  1655. * @dir: DMA transfer direction
  1656. *
  1657. * IOMMU aware version of arm_dma_map_page()
  1658. */
  1659. static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
  1660. unsigned long offset, size_t size, enum dma_data_direction dir,
  1661. unsigned long attrs)
  1662. {
  1663. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1664. __dma_page_cpu_to_dev(page, offset, size, dir);
  1665. return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
  1666. }
  1667. /**
  1668. * arm_coherent_iommu_unmap_page
  1669. * @dev: valid struct device pointer
  1670. * @handle: DMA address of buffer
  1671. * @size: size of buffer (same as passed to dma_map_page)
  1672. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1673. *
  1674. * Coherent IOMMU aware version of arm_dma_unmap_page()
  1675. */
  1676. static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1677. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1678. {
  1679. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1680. dma_addr_t iova = handle & PAGE_MASK;
  1681. int offset = handle & ~PAGE_MASK;
  1682. int len = PAGE_ALIGN(size + offset);
  1683. if (!iova)
  1684. return;
  1685. iommu_unmap(mapping->domain, iova, len);
  1686. __free_iova(mapping, iova, len);
  1687. }
  1688. /**
  1689. * arm_iommu_unmap_page
  1690. * @dev: valid struct device pointer
  1691. * @handle: DMA address of buffer
  1692. * @size: size of buffer (same as passed to dma_map_page)
  1693. * @dir: DMA transfer direction (same as passed to dma_map_page)
  1694. *
  1695. * IOMMU aware version of arm_dma_unmap_page()
  1696. */
  1697. static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
  1698. size_t size, enum dma_data_direction dir, unsigned long attrs)
  1699. {
  1700. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1701. dma_addr_t iova = handle & PAGE_MASK;
  1702. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1703. int offset = handle & ~PAGE_MASK;
  1704. int len = PAGE_ALIGN(size + offset);
  1705. if (!iova)
  1706. return;
  1707. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  1708. __dma_page_dev_to_cpu(page, offset, size, dir);
  1709. iommu_unmap(mapping->domain, iova, len);
  1710. __free_iova(mapping, iova, len);
  1711. }
  1712. /**
  1713. * arm_iommu_map_resource - map a device resource for DMA
  1714. * @dev: valid struct device pointer
  1715. * @phys_addr: physical address of resource
  1716. * @size: size of resource to map
  1717. * @dir: DMA transfer direction
  1718. */
  1719. static dma_addr_t arm_iommu_map_resource(struct device *dev,
  1720. phys_addr_t phys_addr, size_t size,
  1721. enum dma_data_direction dir, unsigned long attrs)
  1722. {
  1723. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1724. dma_addr_t dma_addr;
  1725. int ret, prot;
  1726. phys_addr_t addr = phys_addr & PAGE_MASK;
  1727. unsigned int offset = phys_addr & ~PAGE_MASK;
  1728. size_t len = PAGE_ALIGN(size + offset);
  1729. dma_addr = __alloc_iova(mapping, len);
  1730. if (dma_addr == ARM_MAPPING_ERROR)
  1731. return dma_addr;
  1732. prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
  1733. ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
  1734. if (ret < 0)
  1735. goto fail;
  1736. return dma_addr + offset;
  1737. fail:
  1738. __free_iova(mapping, dma_addr, len);
  1739. return ARM_MAPPING_ERROR;
  1740. }
  1741. /**
  1742. * arm_iommu_unmap_resource - unmap a device DMA resource
  1743. * @dev: valid struct device pointer
  1744. * @dma_handle: DMA address to resource
  1745. * @size: size of resource to map
  1746. * @dir: DMA transfer direction
  1747. */
  1748. static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
  1749. size_t size, enum dma_data_direction dir,
  1750. unsigned long attrs)
  1751. {
  1752. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1753. dma_addr_t iova = dma_handle & PAGE_MASK;
  1754. unsigned int offset = dma_handle & ~PAGE_MASK;
  1755. size_t len = PAGE_ALIGN(size + offset);
  1756. if (!iova)
  1757. return;
  1758. iommu_unmap(mapping->domain, iova, len);
  1759. __free_iova(mapping, iova, len);
  1760. }
  1761. static void arm_iommu_sync_single_for_cpu(struct device *dev,
  1762. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1763. {
  1764. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1765. dma_addr_t iova = handle & PAGE_MASK;
  1766. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1767. unsigned int offset = handle & ~PAGE_MASK;
  1768. if (!iova)
  1769. return;
  1770. __dma_page_dev_to_cpu(page, offset, size, dir);
  1771. }
  1772. static void arm_iommu_sync_single_for_device(struct device *dev,
  1773. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  1774. {
  1775. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1776. dma_addr_t iova = handle & PAGE_MASK;
  1777. struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
  1778. unsigned int offset = handle & ~PAGE_MASK;
  1779. if (!iova)
  1780. return;
  1781. __dma_page_cpu_to_dev(page, offset, size, dir);
  1782. }
  1783. const struct dma_map_ops iommu_ops = {
  1784. .alloc = arm_iommu_alloc_attrs,
  1785. .free = arm_iommu_free_attrs,
  1786. .mmap = arm_iommu_mmap_attrs,
  1787. .get_sgtable = arm_iommu_get_sgtable,
  1788. .map_page = arm_iommu_map_page,
  1789. .unmap_page = arm_iommu_unmap_page,
  1790. .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
  1791. .sync_single_for_device = arm_iommu_sync_single_for_device,
  1792. .map_sg = arm_iommu_map_sg,
  1793. .unmap_sg = arm_iommu_unmap_sg,
  1794. .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
  1795. .sync_sg_for_device = arm_iommu_sync_sg_for_device,
  1796. .map_resource = arm_iommu_map_resource,
  1797. .unmap_resource = arm_iommu_unmap_resource,
  1798. .mapping_error = arm_dma_mapping_error,
  1799. .dma_supported = arm_dma_supported,
  1800. };
  1801. const struct dma_map_ops iommu_coherent_ops = {
  1802. .alloc = arm_coherent_iommu_alloc_attrs,
  1803. .free = arm_coherent_iommu_free_attrs,
  1804. .mmap = arm_coherent_iommu_mmap_attrs,
  1805. .get_sgtable = arm_iommu_get_sgtable,
  1806. .map_page = arm_coherent_iommu_map_page,
  1807. .unmap_page = arm_coherent_iommu_unmap_page,
  1808. .map_sg = arm_coherent_iommu_map_sg,
  1809. .unmap_sg = arm_coherent_iommu_unmap_sg,
  1810. .map_resource = arm_iommu_map_resource,
  1811. .unmap_resource = arm_iommu_unmap_resource,
  1812. .mapping_error = arm_dma_mapping_error,
  1813. .dma_supported = arm_dma_supported,
  1814. };
  1815. /**
  1816. * arm_iommu_create_mapping
  1817. * @bus: pointer to the bus holding the client device (for IOMMU calls)
  1818. * @base: start address of the valid IO address space
  1819. * @size: maximum size of the valid IO address space
  1820. *
  1821. * Creates a mapping structure which holds information about used/unused
  1822. * IO address ranges, which is required to perform memory allocation and
  1823. * mapping with IOMMU aware functions.
  1824. *
  1825. * The client device need to be attached to the mapping with
  1826. * arm_iommu_attach_device function.
  1827. */
  1828. struct dma_iommu_mapping *
  1829. arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
  1830. {
  1831. unsigned int bits = size >> PAGE_SHIFT;
  1832. unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
  1833. struct dma_iommu_mapping *mapping;
  1834. int extensions = 1;
  1835. int err = -ENOMEM;
  1836. /* currently only 32-bit DMA address space is supported */
  1837. if (size > DMA_BIT_MASK(32) + 1)
  1838. return ERR_PTR(-ERANGE);
  1839. if (!bitmap_size)
  1840. return ERR_PTR(-EINVAL);
  1841. if (bitmap_size > PAGE_SIZE) {
  1842. extensions = bitmap_size / PAGE_SIZE;
  1843. bitmap_size = PAGE_SIZE;
  1844. }
  1845. mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
  1846. if (!mapping)
  1847. goto err;
  1848. mapping->bitmap_size = bitmap_size;
  1849. mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
  1850. GFP_KERNEL);
  1851. if (!mapping->bitmaps)
  1852. goto err2;
  1853. mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
  1854. if (!mapping->bitmaps[0])
  1855. goto err3;
  1856. mapping->nr_bitmaps = 1;
  1857. mapping->extensions = extensions;
  1858. mapping->base = base;
  1859. mapping->bits = BITS_PER_BYTE * bitmap_size;
  1860. spin_lock_init(&mapping->lock);
  1861. mapping->domain = iommu_domain_alloc(bus);
  1862. if (!mapping->domain)
  1863. goto err4;
  1864. kref_init(&mapping->kref);
  1865. return mapping;
  1866. err4:
  1867. kfree(mapping->bitmaps[0]);
  1868. err3:
  1869. kfree(mapping->bitmaps);
  1870. err2:
  1871. kfree(mapping);
  1872. err:
  1873. return ERR_PTR(err);
  1874. }
  1875. EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
  1876. static void release_iommu_mapping(struct kref *kref)
  1877. {
  1878. int i;
  1879. struct dma_iommu_mapping *mapping =
  1880. container_of(kref, struct dma_iommu_mapping, kref);
  1881. iommu_domain_free(mapping->domain);
  1882. for (i = 0; i < mapping->nr_bitmaps; i++)
  1883. kfree(mapping->bitmaps[i]);
  1884. kfree(mapping->bitmaps);
  1885. kfree(mapping);
  1886. }
  1887. static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
  1888. {
  1889. int next_bitmap;
  1890. if (mapping->nr_bitmaps >= mapping->extensions)
  1891. return -EINVAL;
  1892. next_bitmap = mapping->nr_bitmaps;
  1893. mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
  1894. GFP_ATOMIC);
  1895. if (!mapping->bitmaps[next_bitmap])
  1896. return -ENOMEM;
  1897. mapping->nr_bitmaps++;
  1898. return 0;
  1899. }
  1900. void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
  1901. {
  1902. if (mapping)
  1903. kref_put(&mapping->kref, release_iommu_mapping);
  1904. }
  1905. EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
  1906. static int __arm_iommu_attach_device(struct device *dev,
  1907. struct dma_iommu_mapping *mapping)
  1908. {
  1909. int err;
  1910. err = iommu_attach_device(mapping->domain, dev);
  1911. if (err)
  1912. return err;
  1913. kref_get(&mapping->kref);
  1914. to_dma_iommu_mapping(dev) = mapping;
  1915. pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
  1916. return 0;
  1917. }
  1918. /**
  1919. * arm_iommu_attach_device
  1920. * @dev: valid struct device pointer
  1921. * @mapping: io address space mapping structure (returned from
  1922. * arm_iommu_create_mapping)
  1923. *
  1924. * Attaches specified io address space mapping to the provided device.
  1925. * This replaces the dma operations (dma_map_ops pointer) with the
  1926. * IOMMU aware version.
  1927. *
  1928. * More than one client might be attached to the same io address space
  1929. * mapping.
  1930. */
  1931. int arm_iommu_attach_device(struct device *dev,
  1932. struct dma_iommu_mapping *mapping)
  1933. {
  1934. int err;
  1935. err = __arm_iommu_attach_device(dev, mapping);
  1936. if (err)
  1937. return err;
  1938. set_dma_ops(dev, &iommu_ops);
  1939. return 0;
  1940. }
  1941. EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
  1942. /**
  1943. * arm_iommu_detach_device
  1944. * @dev: valid struct device pointer
  1945. *
  1946. * Detaches the provided device from a previously attached map.
  1947. * This voids the dma operations (dma_map_ops pointer)
  1948. */
  1949. void arm_iommu_detach_device(struct device *dev)
  1950. {
  1951. struct dma_iommu_mapping *mapping;
  1952. mapping = to_dma_iommu_mapping(dev);
  1953. if (!mapping) {
  1954. dev_warn(dev, "Not attached\n");
  1955. return;
  1956. }
  1957. iommu_detach_device(mapping->domain, dev);
  1958. kref_put(&mapping->kref, release_iommu_mapping);
  1959. to_dma_iommu_mapping(dev) = NULL;
  1960. set_dma_ops(dev, NULL);
  1961. pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
  1962. }
  1963. EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
  1964. static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
  1965. {
  1966. return coherent ? &iommu_coherent_ops : &iommu_ops;
  1967. }
  1968. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1969. const struct iommu_ops *iommu)
  1970. {
  1971. struct dma_iommu_mapping *mapping;
  1972. if (!iommu)
  1973. return false;
  1974. mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
  1975. if (IS_ERR(mapping)) {
  1976. pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
  1977. size, dev_name(dev));
  1978. return false;
  1979. }
  1980. if (__arm_iommu_attach_device(dev, mapping)) {
  1981. pr_warn("Failed to attached device %s to IOMMU_mapping\n",
  1982. dev_name(dev));
  1983. arm_iommu_release_mapping(mapping);
  1984. return false;
  1985. }
  1986. return true;
  1987. }
  1988. static void arm_teardown_iommu_dma_ops(struct device *dev)
  1989. {
  1990. struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
  1991. if (!mapping)
  1992. return;
  1993. arm_iommu_detach_device(dev);
  1994. arm_iommu_release_mapping(mapping);
  1995. }
  1996. #else
  1997. static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
  1998. const struct iommu_ops *iommu)
  1999. {
  2000. return false;
  2001. }
  2002. static void arm_teardown_iommu_dma_ops(struct device *dev) { }
  2003. #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
  2004. #endif /* CONFIG_ARM_DMA_USE_IOMMU */
  2005. static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
  2006. {
  2007. return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
  2008. }
  2009. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  2010. const struct iommu_ops *iommu, bool coherent)
  2011. {
  2012. const struct dma_map_ops *dma_ops;
  2013. dev->archdata.dma_coherent = coherent;
  2014. /*
  2015. * Don't override the dma_ops if they have already been set. Ideally
  2016. * this should be the only location where dma_ops are set, remove this
  2017. * check when all other callers of set_dma_ops will have disappeared.
  2018. */
  2019. if (dev->dma_ops)
  2020. return;
  2021. if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
  2022. dma_ops = arm_get_iommu_dma_map_ops(coherent);
  2023. else
  2024. dma_ops = arm_get_dma_map_ops(coherent);
  2025. set_dma_ops(dev, dma_ops);
  2026. #ifdef CONFIG_XEN
  2027. if (xen_initial_domain()) {
  2028. dev->archdata.dev_dma_ops = dev->dma_ops;
  2029. dev->dma_ops = xen_dma_ops;
  2030. }
  2031. #endif
  2032. dev->archdata.dma_ops_setup = true;
  2033. }
  2034. void arch_teardown_dma_ops(struct device *dev)
  2035. {
  2036. if (!dev->archdata.dma_ops_setup)
  2037. return;
  2038. arm_teardown_iommu_dma_ops(dev);
  2039. }