dma-mapping.c 17 KB

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