dma-mapping.c 45 KB

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