dma-mapping.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. else
  272. __dma_free_buffer(page, size);
  273. return addr;
  274. }
  275. /*
  276. * Allocate DMA-coherent memory space and return both the kernel remapped
  277. * virtual and bus address for that space.
  278. */
  279. void *
  280. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  281. {
  282. void *memory;
  283. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  284. return memory;
  285. return __dma_alloc(dev, size, handle, gfp,
  286. pgprot_dmacoherent(pgprot_kernel));
  287. }
  288. EXPORT_SYMBOL(dma_alloc_coherent);
  289. /*
  290. * Allocate a writecombining region, in much the same way as
  291. * dma_alloc_coherent above.
  292. */
  293. void *
  294. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  295. {
  296. return __dma_alloc(dev, size, handle, gfp,
  297. pgprot_writecombine(pgprot_kernel));
  298. }
  299. EXPORT_SYMBOL(dma_alloc_writecombine);
  300. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  301. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  302. {
  303. int ret = -ENXIO;
  304. #ifdef CONFIG_MMU
  305. unsigned long user_size, kern_size;
  306. struct arm_vmregion *c;
  307. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  308. c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
  309. if (c) {
  310. unsigned long off = vma->vm_pgoff;
  311. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  312. if (off < kern_size &&
  313. user_size <= (kern_size - off)) {
  314. ret = remap_pfn_range(vma, vma->vm_start,
  315. page_to_pfn(c->vm_pages) + off,
  316. user_size << PAGE_SHIFT,
  317. vma->vm_page_prot);
  318. }
  319. }
  320. #endif /* CONFIG_MMU */
  321. return ret;
  322. }
  323. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  324. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  325. {
  326. vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
  327. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  328. }
  329. EXPORT_SYMBOL(dma_mmap_coherent);
  330. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  331. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  332. {
  333. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  334. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  335. }
  336. EXPORT_SYMBOL(dma_mmap_writecombine);
  337. /*
  338. * free a page as defined by the above mapping.
  339. * Must not be called with IRQs disabled.
  340. */
  341. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  342. {
  343. WARN_ON(irqs_disabled());
  344. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  345. return;
  346. size = PAGE_ALIGN(size);
  347. if (!arch_is_coherent())
  348. __dma_free_remap(cpu_addr, size);
  349. __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
  350. }
  351. EXPORT_SYMBOL(dma_free_coherent);
  352. /*
  353. * Make an area consistent for devices.
  354. * Note: Drivers should NOT use this function directly, as it will break
  355. * platforms with CONFIG_DMABOUNCE.
  356. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  357. */
  358. void ___dma_single_cpu_to_dev(const void *kaddr, size_t size,
  359. enum dma_data_direction dir)
  360. {
  361. unsigned long paddr;
  362. BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
  363. dmac_map_area(kaddr, size, dir);
  364. paddr = __pa(kaddr);
  365. if (dir == DMA_FROM_DEVICE) {
  366. outer_inv_range(paddr, paddr + size);
  367. } else {
  368. outer_clean_range(paddr, paddr + size);
  369. }
  370. /* FIXME: non-speculating: flush on bidirectional mappings? */
  371. }
  372. EXPORT_SYMBOL(___dma_single_cpu_to_dev);
  373. void ___dma_single_dev_to_cpu(const void *kaddr, size_t size,
  374. enum dma_data_direction dir)
  375. {
  376. BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
  377. /* FIXME: non-speculating: not required */
  378. /* don't bother invalidating if DMA to device */
  379. if (dir != DMA_TO_DEVICE) {
  380. unsigned long paddr = __pa(kaddr);
  381. outer_inv_range(paddr, paddr + size);
  382. }
  383. dmac_unmap_area(kaddr, size, dir);
  384. }
  385. EXPORT_SYMBOL(___dma_single_dev_to_cpu);
  386. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  387. size_t size, enum dma_data_direction dir,
  388. void (*op)(const void *, size_t, int))
  389. {
  390. /*
  391. * A single sg entry may refer to multiple physically contiguous
  392. * pages. But we still need to process highmem pages individually.
  393. * If highmem is not configured then the bulk of this loop gets
  394. * optimized out.
  395. */
  396. size_t left = size;
  397. do {
  398. size_t len = left;
  399. void *vaddr;
  400. if (PageHighMem(page)) {
  401. if (len + offset > PAGE_SIZE) {
  402. if (offset >= PAGE_SIZE) {
  403. page += offset / PAGE_SIZE;
  404. offset %= PAGE_SIZE;
  405. }
  406. len = PAGE_SIZE - offset;
  407. }
  408. vaddr = kmap_high_get(page);
  409. if (vaddr) {
  410. vaddr += offset;
  411. op(vaddr, len, dir);
  412. kunmap_high(page);
  413. } else if (cache_is_vipt()) {
  414. /* unmapped pages might still be cached */
  415. vaddr = kmap_atomic(page);
  416. op(vaddr + offset, len, dir);
  417. kunmap_atomic(vaddr);
  418. }
  419. } else {
  420. vaddr = page_address(page) + offset;
  421. op(vaddr, len, dir);
  422. }
  423. offset = 0;
  424. page++;
  425. left -= len;
  426. } while (left);
  427. }
  428. void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
  429. size_t size, enum dma_data_direction dir)
  430. {
  431. unsigned long paddr;
  432. dma_cache_maint_page(page, off, size, dir, dmac_map_area);
  433. paddr = page_to_phys(page) + off;
  434. if (dir == DMA_FROM_DEVICE) {
  435. outer_inv_range(paddr, paddr + size);
  436. } else {
  437. outer_clean_range(paddr, paddr + size);
  438. }
  439. /* FIXME: non-speculating: flush on bidirectional mappings? */
  440. }
  441. EXPORT_SYMBOL(___dma_page_cpu_to_dev);
  442. void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
  443. size_t size, enum dma_data_direction dir)
  444. {
  445. unsigned long paddr = page_to_phys(page) + off;
  446. /* FIXME: non-speculating: not required */
  447. /* don't bother invalidating if DMA to device */
  448. if (dir != DMA_TO_DEVICE)
  449. outer_inv_range(paddr, paddr + size);
  450. dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
  451. /*
  452. * Mark the D-cache clean for this page to avoid extra flushing.
  453. */
  454. if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
  455. set_bit(PG_dcache_clean, &page->flags);
  456. }
  457. EXPORT_SYMBOL(___dma_page_dev_to_cpu);
  458. /**
  459. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  460. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  461. * @sg: list of buffers
  462. * @nents: number of buffers to map
  463. * @dir: DMA transfer direction
  464. *
  465. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  466. * This is the scatter-gather version of the dma_map_single interface.
  467. * Here the scatter gather list elements are each tagged with the
  468. * appropriate dma address and length. They are obtained via
  469. * sg_dma_{address,length}.
  470. *
  471. * Device ownership issues as mentioned for dma_map_single are the same
  472. * here.
  473. */
  474. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  475. enum dma_data_direction dir)
  476. {
  477. struct scatterlist *s;
  478. int i, j;
  479. BUG_ON(!valid_dma_direction(dir));
  480. for_each_sg(sg, s, nents, i) {
  481. s->dma_address = __dma_map_page(dev, sg_page(s), s->offset,
  482. s->length, dir);
  483. if (dma_mapping_error(dev, s->dma_address))
  484. goto bad_mapping;
  485. }
  486. debug_dma_map_sg(dev, sg, nents, nents, dir);
  487. return nents;
  488. bad_mapping:
  489. for_each_sg(sg, s, i, j)
  490. __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(dma_map_sg);
  494. /**
  495. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  496. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  497. * @sg: list of buffers
  498. * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
  499. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  500. *
  501. * Unmap a set of streaming mode DMA translations. Again, CPU access
  502. * rules concerning calls here are the same as for dma_unmap_single().
  503. */
  504. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  505. enum dma_data_direction dir)
  506. {
  507. struct scatterlist *s;
  508. int i;
  509. debug_dma_unmap_sg(dev, sg, nents, dir);
  510. for_each_sg(sg, s, nents, i)
  511. __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  512. }
  513. EXPORT_SYMBOL(dma_unmap_sg);
  514. /**
  515. * dma_sync_sg_for_cpu
  516. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  517. * @sg: list of buffers
  518. * @nents: number of buffers to map (returned from dma_map_sg)
  519. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  520. */
  521. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  522. int nents, enum dma_data_direction dir)
  523. {
  524. struct scatterlist *s;
  525. int i;
  526. for_each_sg(sg, s, nents, i) {
  527. if (!dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
  528. sg_dma_len(s), dir))
  529. continue;
  530. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  531. s->length, dir);
  532. }
  533. debug_dma_sync_sg_for_cpu(dev, sg, nents, dir);
  534. }
  535. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  536. /**
  537. * dma_sync_sg_for_device
  538. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  539. * @sg: list of buffers
  540. * @nents: number of buffers to map (returned from dma_map_sg)
  541. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  542. */
  543. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  544. int nents, enum dma_data_direction dir)
  545. {
  546. struct scatterlist *s;
  547. int i;
  548. for_each_sg(sg, s, nents, i) {
  549. if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
  550. sg_dma_len(s), dir))
  551. continue;
  552. __dma_page_cpu_to_dev(sg_page(s), s->offset,
  553. s->length, dir);
  554. }
  555. debug_dma_sync_sg_for_device(dev, sg, nents, dir);
  556. }
  557. EXPORT_SYMBOL(dma_sync_sg_for_device);
  558. /*
  559. * Return whether the given device DMA address mask can be supported
  560. * properly. For example, if your device can only drive the low 24-bits
  561. * during bus mastering, then you would pass 0x00ffffff as the mask
  562. * to this function.
  563. */
  564. int dma_supported(struct device *dev, u64 mask)
  565. {
  566. if (mask < (u64)arm_dma_limit)
  567. return 0;
  568. return 1;
  569. }
  570. EXPORT_SYMBOL(dma_supported);
  571. int dma_set_mask(struct device *dev, u64 dma_mask)
  572. {
  573. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  574. return -EIO;
  575. #ifndef CONFIG_DMABOUNCE
  576. *dev->dma_mask = dma_mask;
  577. #endif
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(dma_set_mask);
  581. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  582. static int __init dma_debug_do_init(void)
  583. {
  584. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  585. return 0;
  586. }
  587. fs_initcall(dma_debug_do_init);