dma-mapping.c 18 KB

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