dma-mapping.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/highmem.h>
  21. #include <linux/slab.h>
  22. #include <asm/memory.h>
  23. #include <asm/highmem.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/sizes.h>
  27. #include <asm/mach/arch.h>
  28. #include "mm.h"
  29. /*
  30. * The DMA API is built upon the notion of "buffer ownership". A buffer
  31. * is either exclusively owned by the CPU (and therefore may be accessed
  32. * by it) or exclusively owned by the DMA device. These helper functions
  33. * represent the transitions between these two ownership states.
  34. *
  35. * Note, however, that on later ARMs, this notion does not work due to
  36. * speculative prefetches. We model our approach on the assumption that
  37. * the CPU does do speculative prefetches, which means we clean caches
  38. * before transfers and delay cache invalidation until transfer completion.
  39. *
  40. */
  41. static void __dma_page_cpu_to_dev(struct page *, unsigned long,
  42. size_t, enum dma_data_direction);
  43. static void __dma_page_dev_to_cpu(struct page *, unsigned long,
  44. size_t, enum dma_data_direction);
  45. /**
  46. * arm_dma_map_page - map a portion of a page for streaming DMA
  47. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  48. * @page: page that buffer resides in
  49. * @offset: offset into page for start of buffer
  50. * @size: size of buffer to map
  51. * @dir: DMA transfer direction
  52. *
  53. * Ensure that any data held in the cache is appropriately discarded
  54. * or written back.
  55. *
  56. * The device owns this memory once this call has completed. The CPU
  57. * can regain ownership by calling dma_unmap_page().
  58. */
  59. static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
  60. unsigned long offset, size_t size, enum dma_data_direction dir,
  61. struct dma_attrs *attrs)
  62. {
  63. if (!arch_is_coherent())
  64. __dma_page_cpu_to_dev(page, offset, size, dir);
  65. return pfn_to_dma(dev, page_to_pfn(page)) + offset;
  66. }
  67. /**
  68. * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  69. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  70. * @handle: DMA address of buffer
  71. * @size: size of buffer (same as passed to dma_map_page)
  72. * @dir: DMA transfer direction (same as passed to dma_map_page)
  73. *
  74. * Unmap a page streaming mode DMA translation. The handle and size
  75. * must match what was provided in the previous dma_map_page() call.
  76. * All other usages are undefined.
  77. *
  78. * After this call, reads by the CPU to the buffer are guaranteed to see
  79. * whatever the device wrote there.
  80. */
  81. static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
  82. size_t size, enum dma_data_direction dir,
  83. struct dma_attrs *attrs)
  84. {
  85. if (!arch_is_coherent())
  86. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
  87. handle & ~PAGE_MASK, size, dir);
  88. }
  89. static void arm_dma_sync_single_for_cpu(struct device *dev,
  90. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  91. {
  92. unsigned int offset = handle & (PAGE_SIZE - 1);
  93. struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
  94. if (!arch_is_coherent())
  95. __dma_page_dev_to_cpu(page, offset, size, dir);
  96. }
  97. static void arm_dma_sync_single_for_device(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_cpu_to_dev(page, offset, size, dir);
  104. }
  105. static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
  106. struct dma_map_ops arm_dma_ops = {
  107. .map_page = arm_dma_map_page,
  108. .unmap_page = arm_dma_unmap_page,
  109. .map_sg = arm_dma_map_sg,
  110. .unmap_sg = arm_dma_unmap_sg,
  111. .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
  112. .sync_single_for_device = arm_dma_sync_single_for_device,
  113. .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
  114. .sync_sg_for_device = arm_dma_sync_sg_for_device,
  115. .set_dma_mask = arm_dma_set_mask,
  116. };
  117. EXPORT_SYMBOL(arm_dma_ops);
  118. static u64 get_coherent_dma_mask(struct device *dev)
  119. {
  120. u64 mask = (u64)arm_dma_limit;
  121. if (dev) {
  122. mask = dev->coherent_dma_mask;
  123. /*
  124. * Sanity check the DMA mask - it must be non-zero, and
  125. * must be able to be satisfied by a DMA allocation.
  126. */
  127. if (mask == 0) {
  128. dev_warn(dev, "coherent DMA mask is unset\n");
  129. return 0;
  130. }
  131. if ((~mask) & (u64)arm_dma_limit) {
  132. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  133. "than system GFP_DMA mask %#llx\n",
  134. mask, (u64)arm_dma_limit);
  135. return 0;
  136. }
  137. }
  138. return mask;
  139. }
  140. /*
  141. * Allocate a DMA buffer for 'dev' of size 'size' using the
  142. * specified gfp mask. Note that 'size' must be page aligned.
  143. */
  144. static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  145. {
  146. unsigned long order = get_order(size);
  147. struct page *page, *p, *e;
  148. void *ptr;
  149. u64 mask = get_coherent_dma_mask(dev);
  150. #ifdef CONFIG_DMA_API_DEBUG
  151. u64 limit = (mask + 1) & ~mask;
  152. if (limit && size >= limit) {
  153. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  154. size, mask);
  155. return NULL;
  156. }
  157. #endif
  158. if (!mask)
  159. return NULL;
  160. if (mask < 0xffffffffULL)
  161. gfp |= GFP_DMA;
  162. page = alloc_pages(gfp, order);
  163. if (!page)
  164. return NULL;
  165. /*
  166. * Now split the huge page and free the excess pages
  167. */
  168. split_page(page, order);
  169. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  170. __free_page(p);
  171. /*
  172. * Ensure that the allocated pages are zeroed, and that any data
  173. * lurking in the kernel direct-mapped region is invalidated.
  174. */
  175. ptr = page_address(page);
  176. memset(ptr, 0, size);
  177. dmac_flush_range(ptr, ptr + size);
  178. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  179. return page;
  180. }
  181. /*
  182. * Free a DMA buffer. 'size' must be page aligned.
  183. */
  184. static void __dma_free_buffer(struct page *page, size_t size)
  185. {
  186. struct page *e = page + (size >> PAGE_SHIFT);
  187. while (page < e) {
  188. __free_page(page);
  189. page++;
  190. }
  191. }
  192. #ifdef CONFIG_MMU
  193. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
  194. #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
  195. /*
  196. * These are the page tables (2MB each) covering uncached, DMA consistent allocations
  197. */
  198. static pte_t **consistent_pte;
  199. #define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
  200. unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
  201. void __init init_consistent_dma_size(unsigned long size)
  202. {
  203. unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
  204. BUG_ON(consistent_pte); /* Check we're called before DMA region init */
  205. BUG_ON(base < VMALLOC_END);
  206. /* Grow region to accommodate specified size */
  207. if (base < consistent_base)
  208. consistent_base = base;
  209. }
  210. #include "vmregion.h"
  211. static struct arm_vmregion_head consistent_head = {
  212. .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
  213. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  214. .vm_end = CONSISTENT_END,
  215. };
  216. #ifdef CONFIG_HUGETLB_PAGE
  217. #error ARM Coherent DMA allocator does not (yet) support huge TLB
  218. #endif
  219. /*
  220. * Initialise the consistent memory allocation.
  221. */
  222. static int __init consistent_init(void)
  223. {
  224. int ret = 0;
  225. pgd_t *pgd;
  226. pud_t *pud;
  227. pmd_t *pmd;
  228. pte_t *pte;
  229. int i = 0;
  230. unsigned long base = consistent_base;
  231. unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
  232. consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
  233. if (!consistent_pte) {
  234. pr_err("%s: no memory\n", __func__);
  235. return -ENOMEM;
  236. }
  237. pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
  238. consistent_head.vm_start = base;
  239. do {
  240. pgd = pgd_offset(&init_mm, base);
  241. pud = pud_alloc(&init_mm, pgd, base);
  242. if (!pud) {
  243. pr_err("%s: no pud tables\n", __func__);
  244. ret = -ENOMEM;
  245. break;
  246. }
  247. pmd = pmd_alloc(&init_mm, pud, base);
  248. if (!pmd) {
  249. pr_err("%s: no pmd tables\n", __func__);
  250. ret = -ENOMEM;
  251. break;
  252. }
  253. WARN_ON(!pmd_none(*pmd));
  254. pte = pte_alloc_kernel(pmd, base);
  255. if (!pte) {
  256. pr_err("%s: no pte tables\n", __func__);
  257. ret = -ENOMEM;
  258. break;
  259. }
  260. consistent_pte[i++] = pte;
  261. base += PMD_SIZE;
  262. } while (base < CONSISTENT_END);
  263. return ret;
  264. }
  265. core_initcall(consistent_init);
  266. static void *
  267. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
  268. const void *caller)
  269. {
  270. struct arm_vmregion *c;
  271. size_t align;
  272. int bit;
  273. if (!consistent_pte) {
  274. pr_err("%s: not initialised\n", __func__);
  275. dump_stack();
  276. return NULL;
  277. }
  278. /*
  279. * Align the virtual region allocation - maximum alignment is
  280. * a section size, minimum is a page size. This helps reduce
  281. * fragmentation of the DMA space, and also prevents allocations
  282. * smaller than a section from crossing a section boundary.
  283. */
  284. bit = fls(size - 1);
  285. if (bit > SECTION_SHIFT)
  286. bit = SECTION_SHIFT;
  287. align = 1 << bit;
  288. /*
  289. * Allocate a virtual address in the consistent mapping region.
  290. */
  291. c = arm_vmregion_alloc(&consistent_head, align, size,
  292. gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
  293. if (c) {
  294. pte_t *pte;
  295. int idx = CONSISTENT_PTE_INDEX(c->vm_start);
  296. u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  297. pte = consistent_pte[idx] + off;
  298. c->vm_pages = page;
  299. do {
  300. BUG_ON(!pte_none(*pte));
  301. set_pte_ext(pte, mk_pte(page, prot), 0);
  302. page++;
  303. pte++;
  304. off++;
  305. if (off >= PTRS_PER_PTE) {
  306. off = 0;
  307. pte = consistent_pte[++idx];
  308. }
  309. } while (size -= PAGE_SIZE);
  310. dsb();
  311. return (void *)c->vm_start;
  312. }
  313. return NULL;
  314. }
  315. static void __dma_free_remap(void *cpu_addr, size_t size)
  316. {
  317. struct arm_vmregion *c;
  318. unsigned long addr;
  319. pte_t *ptep;
  320. int idx;
  321. u32 off;
  322. c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
  323. if (!c) {
  324. pr_err("%s: trying to free invalid coherent area: %p\n",
  325. __func__, cpu_addr);
  326. dump_stack();
  327. return;
  328. }
  329. if ((c->vm_end - c->vm_start) != size) {
  330. pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
  331. __func__, c->vm_end - c->vm_start, size);
  332. dump_stack();
  333. size = c->vm_end - c->vm_start;
  334. }
  335. idx = CONSISTENT_PTE_INDEX(c->vm_start);
  336. off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  337. ptep = consistent_pte[idx] + off;
  338. addr = c->vm_start;
  339. do {
  340. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  341. ptep++;
  342. addr += PAGE_SIZE;
  343. off++;
  344. if (off >= PTRS_PER_PTE) {
  345. off = 0;
  346. ptep = consistent_pte[++idx];
  347. }
  348. if (pte_none(pte) || !pte_present(pte))
  349. pr_crit("%s: bad page in kernel page table\n",
  350. __func__);
  351. } while (size -= PAGE_SIZE);
  352. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  353. arm_vmregion_free(&consistent_head, c);
  354. }
  355. #else /* !CONFIG_MMU */
  356. #define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page)
  357. #define __dma_free_remap(addr, size) do { } while (0)
  358. #endif /* CONFIG_MMU */
  359. static void *
  360. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  361. pgprot_t prot, const void *caller)
  362. {
  363. struct page *page;
  364. void *addr;
  365. /*
  366. * Following is a work-around (a.k.a. hack) to prevent pages
  367. * with __GFP_COMP being passed to split_page() which cannot
  368. * handle them. The real problem is that this flag probably
  369. * should be 0 on ARM as it is not supported on this
  370. * platform; see CONFIG_HUGETLBFS.
  371. */
  372. gfp &= ~(__GFP_COMP);
  373. *handle = DMA_ERROR_CODE;
  374. size = PAGE_ALIGN(size);
  375. page = __dma_alloc_buffer(dev, size, gfp);
  376. if (!page)
  377. return NULL;
  378. if (!arch_is_coherent())
  379. addr = __dma_alloc_remap(page, size, gfp, prot, caller);
  380. else
  381. addr = page_address(page);
  382. if (addr)
  383. *handle = pfn_to_dma(dev, page_to_pfn(page));
  384. else
  385. __dma_free_buffer(page, size);
  386. return addr;
  387. }
  388. /*
  389. * Allocate DMA-coherent memory space and return both the kernel remapped
  390. * virtual and bus address for that space.
  391. */
  392. void *
  393. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  394. {
  395. void *memory;
  396. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  397. return memory;
  398. return __dma_alloc(dev, size, handle, gfp,
  399. pgprot_dmacoherent(pgprot_kernel),
  400. __builtin_return_address(0));
  401. }
  402. EXPORT_SYMBOL(dma_alloc_coherent);
  403. /*
  404. * Allocate a writecombining region, in much the same way as
  405. * dma_alloc_coherent above.
  406. */
  407. void *
  408. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  409. {
  410. return __dma_alloc(dev, size, handle, gfp,
  411. pgprot_writecombine(pgprot_kernel),
  412. __builtin_return_address(0));
  413. }
  414. EXPORT_SYMBOL(dma_alloc_writecombine);
  415. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  416. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  417. {
  418. int ret = -ENXIO;
  419. #ifdef CONFIG_MMU
  420. unsigned long user_size, kern_size;
  421. struct arm_vmregion *c;
  422. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  423. return ret;
  424. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  425. c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
  426. if (c) {
  427. unsigned long off = vma->vm_pgoff;
  428. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  429. if (off < kern_size &&
  430. user_size <= (kern_size - off)) {
  431. ret = remap_pfn_range(vma, vma->vm_start,
  432. page_to_pfn(c->vm_pages) + off,
  433. user_size << PAGE_SHIFT,
  434. vma->vm_page_prot);
  435. }
  436. }
  437. #endif /* CONFIG_MMU */
  438. return ret;
  439. }
  440. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  441. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  442. {
  443. vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
  444. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  445. }
  446. EXPORT_SYMBOL(dma_mmap_coherent);
  447. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  448. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  449. {
  450. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  451. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  452. }
  453. EXPORT_SYMBOL(dma_mmap_writecombine);
  454. /*
  455. * free a page as defined by the above mapping.
  456. * Must not be called with IRQs disabled.
  457. */
  458. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  459. {
  460. WARN_ON(irqs_disabled());
  461. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  462. return;
  463. size = PAGE_ALIGN(size);
  464. if (!arch_is_coherent())
  465. __dma_free_remap(cpu_addr, size);
  466. __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
  467. }
  468. EXPORT_SYMBOL(dma_free_coherent);
  469. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  470. size_t size, enum dma_data_direction dir,
  471. void (*op)(const void *, size_t, int))
  472. {
  473. /*
  474. * A single sg entry may refer to multiple physically contiguous
  475. * pages. But we still need to process highmem pages individually.
  476. * If highmem is not configured then the bulk of this loop gets
  477. * optimized out.
  478. */
  479. size_t left = size;
  480. do {
  481. size_t len = left;
  482. void *vaddr;
  483. if (PageHighMem(page)) {
  484. if (len + offset > PAGE_SIZE) {
  485. if (offset >= PAGE_SIZE) {
  486. page += offset / PAGE_SIZE;
  487. offset %= PAGE_SIZE;
  488. }
  489. len = PAGE_SIZE - offset;
  490. }
  491. vaddr = kmap_high_get(page);
  492. if (vaddr) {
  493. vaddr += offset;
  494. op(vaddr, len, dir);
  495. kunmap_high(page);
  496. } else if (cache_is_vipt()) {
  497. /* unmapped pages might still be cached */
  498. vaddr = kmap_atomic(page);
  499. op(vaddr + offset, len, dir);
  500. kunmap_atomic(vaddr);
  501. }
  502. } else {
  503. vaddr = page_address(page) + offset;
  504. op(vaddr, len, dir);
  505. }
  506. offset = 0;
  507. page++;
  508. left -= len;
  509. } while (left);
  510. }
  511. /*
  512. * Make an area consistent for devices.
  513. * Note: Drivers should NOT use this function directly, as it will break
  514. * platforms with CONFIG_DMABOUNCE.
  515. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  516. */
  517. static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
  518. size_t size, enum dma_data_direction dir)
  519. {
  520. unsigned long paddr;
  521. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  522. paddr = page_to_phys(page) + off;
  523. if (dir == DMA_FROM_DEVICE) {
  524. outer_inv_range(paddr, paddr + size);
  525. } else {
  526. outer_clean_range(paddr, paddr + size);
  527. }
  528. /* FIXME: non-speculating: flush on bidirectional mappings? */
  529. }
  530. static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
  531. size_t size, enum dma_data_direction dir)
  532. {
  533. unsigned long paddr = page_to_phys(page) + off;
  534. /* FIXME: non-speculating: not required */
  535. /* don't bother invalidating if DMA to device */
  536. if (dir != DMA_TO_DEVICE)
  537. outer_inv_range(paddr, paddr + size);
  538. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  539. /*
  540. * Mark the D-cache clean for this page to avoid extra flushing.
  541. */
  542. if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
  543. set_bit(PG_dcache_clean, &page->flags);
  544. }
  545. /**
  546. * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
  547. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  548. * @sg: list of buffers
  549. * @nents: number of buffers to map
  550. * @dir: DMA transfer direction
  551. *
  552. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  553. * This is the scatter-gather version of the dma_map_single interface.
  554. * Here the scatter gather list elements are each tagged with the
  555. * appropriate dma address and length. They are obtained via
  556. * sg_dma_{address,length}.
  557. *
  558. * Device ownership issues as mentioned for dma_map_single are the same
  559. * here.
  560. */
  561. int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  562. enum dma_data_direction dir, struct dma_attrs *attrs)
  563. {
  564. struct dma_map_ops *ops = get_dma_ops(dev);
  565. struct scatterlist *s;
  566. int i, j;
  567. for_each_sg(sg, s, nents, i) {
  568. s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
  569. s->length, dir, attrs);
  570. if (dma_mapping_error(dev, s->dma_address))
  571. goto bad_mapping;
  572. }
  573. return nents;
  574. bad_mapping:
  575. for_each_sg(sg, s, i, j)
  576. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  577. return 0;
  578. }
  579. /**
  580. * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  581. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  582. * @sg: list of buffers
  583. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  584. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  585. *
  586. * Unmap a set of streaming mode DMA translations. Again, CPU access
  587. * rules concerning calls here are the same as for dma_unmap_single().
  588. */
  589. void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  590. enum dma_data_direction dir, struct dma_attrs *attrs)
  591. {
  592. struct dma_map_ops *ops = get_dma_ops(dev);
  593. struct scatterlist *s;
  594. int i;
  595. for_each_sg(sg, s, nents, i)
  596. ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
  597. }
  598. /**
  599. * arm_dma_sync_sg_for_cpu
  600. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  601. * @sg: list of buffers
  602. * @nents: number of buffers to map (returned from dma_map_sg)
  603. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  604. */
  605. void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  606. int nents, enum dma_data_direction dir)
  607. {
  608. struct dma_map_ops *ops = get_dma_ops(dev);
  609. struct scatterlist *s;
  610. int i;
  611. for_each_sg(sg, s, nents, i)
  612. ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
  613. dir);
  614. }
  615. /**
  616. * arm_dma_sync_sg_for_device
  617. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  618. * @sg: list of buffers
  619. * @nents: number of buffers to map (returned from dma_map_sg)
  620. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  621. */
  622. void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  623. int nents, enum dma_data_direction dir)
  624. {
  625. struct dma_map_ops *ops = get_dma_ops(dev);
  626. struct scatterlist *s;
  627. int i;
  628. for_each_sg(sg, s, nents, i)
  629. ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
  630. dir);
  631. }
  632. /*
  633. * Return whether the given device DMA address mask can be supported
  634. * properly. For example, if your device can only drive the low 24-bits
  635. * during bus mastering, then you would pass 0x00ffffff as the mask
  636. * to this function.
  637. */
  638. int dma_supported(struct device *dev, u64 mask)
  639. {
  640. if (mask < (u64)arm_dma_limit)
  641. return 0;
  642. return 1;
  643. }
  644. EXPORT_SYMBOL(dma_supported);
  645. static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
  646. {
  647. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  648. return -EIO;
  649. *dev->dma_mask = dma_mask;
  650. return 0;
  651. }
  652. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  653. static int __init dma_debug_do_init(void)
  654. {
  655. #ifdef CONFIG_MMU
  656. arm_vmregion_create_proc("dma-mappings", &consistent_head);
  657. #endif
  658. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  659. return 0;
  660. }
  661. fs_initcall(dma_debug_do_init);