dma-mapping.c 43 KB

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