dma-mapping.c 43 KB

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