dma-mapping.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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/slab.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 <asm/memory.h>
  21. #include <asm/highmem.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/sizes.h>
  25. /* Sanity check size */
  26. #if (CONSISTENT_DMA_SIZE % SZ_2M)
  27. #error "CONSISTENT_DMA_SIZE must be multiple of 2MiB"
  28. #endif
  29. #define CONSISTENT_END (0xffe00000)
  30. #define CONSISTENT_BASE (CONSISTENT_END - CONSISTENT_DMA_SIZE)
  31. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  32. #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT)
  33. #define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT)
  34. /*
  35. * These are the page tables (2MB each) covering uncached, DMA consistent allocations
  36. */
  37. static pte_t *consistent_pte[NUM_CONSISTENT_PTES];
  38. static DEFINE_SPINLOCK(consistent_lock);
  39. /*
  40. * VM region handling support.
  41. *
  42. * This should become something generic, handling VM region allocations for
  43. * vmalloc and similar (ioremap, module space, etc).
  44. *
  45. * I envisage vmalloc()'s supporting vm_struct becoming:
  46. *
  47. * struct vm_struct {
  48. * struct vm_region region;
  49. * unsigned long flags;
  50. * struct page **pages;
  51. * unsigned int nr_pages;
  52. * unsigned long phys_addr;
  53. * };
  54. *
  55. * get_vm_area() would then call vm_region_alloc with an appropriate
  56. * struct vm_region head (eg):
  57. *
  58. * struct vm_region vmalloc_head = {
  59. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  60. * .vm_start = VMALLOC_START,
  61. * .vm_end = VMALLOC_END,
  62. * };
  63. *
  64. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  65. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  66. * would have to initialise this each time prior to calling vm_region_alloc().
  67. */
  68. struct arm_vm_region {
  69. struct list_head vm_list;
  70. unsigned long vm_start;
  71. unsigned long vm_end;
  72. struct page *vm_pages;
  73. int vm_active;
  74. };
  75. static struct arm_vm_region consistent_head = {
  76. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  77. .vm_start = CONSISTENT_BASE,
  78. .vm_end = CONSISTENT_END,
  79. };
  80. static struct arm_vm_region *
  81. arm_vm_region_alloc(struct arm_vm_region *head, size_t size, gfp_t gfp)
  82. {
  83. unsigned long addr = head->vm_start, end = head->vm_end - size;
  84. unsigned long flags;
  85. struct arm_vm_region *c, *new;
  86. new = kmalloc(sizeof(struct arm_vm_region), gfp);
  87. if (!new)
  88. goto out;
  89. spin_lock_irqsave(&consistent_lock, flags);
  90. list_for_each_entry(c, &head->vm_list, vm_list) {
  91. if ((addr + size) < addr)
  92. goto nospc;
  93. if ((addr + size) <= c->vm_start)
  94. goto found;
  95. addr = c->vm_end;
  96. if (addr > end)
  97. goto nospc;
  98. }
  99. found:
  100. /*
  101. * Insert this entry _before_ the one we found.
  102. */
  103. list_add_tail(&new->vm_list, &c->vm_list);
  104. new->vm_start = addr;
  105. new->vm_end = addr + size;
  106. new->vm_active = 1;
  107. spin_unlock_irqrestore(&consistent_lock, flags);
  108. return new;
  109. nospc:
  110. spin_unlock_irqrestore(&consistent_lock, flags);
  111. kfree(new);
  112. out:
  113. return NULL;
  114. }
  115. static struct arm_vm_region *arm_vm_region_find(struct arm_vm_region *head, unsigned long addr)
  116. {
  117. struct arm_vm_region *c;
  118. list_for_each_entry(c, &head->vm_list, vm_list) {
  119. if (c->vm_active && c->vm_start == addr)
  120. goto out;
  121. }
  122. c = NULL;
  123. out:
  124. return c;
  125. }
  126. #ifdef CONFIG_HUGETLB_PAGE
  127. #error ARM Coherent DMA allocator does not (yet) support huge TLB
  128. #endif
  129. static void *
  130. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  131. pgprot_t prot)
  132. {
  133. struct page *page;
  134. struct arm_vm_region *c;
  135. unsigned long order;
  136. u64 mask = ISA_DMA_THRESHOLD, limit;
  137. if (!consistent_pte[0]) {
  138. printk(KERN_ERR "%s: not initialised\n", __func__);
  139. dump_stack();
  140. return NULL;
  141. }
  142. if (dev) {
  143. mask = dev->coherent_dma_mask;
  144. /*
  145. * Sanity check the DMA mask - it must be non-zero, and
  146. * must be able to be satisfied by a DMA allocation.
  147. */
  148. if (mask == 0) {
  149. dev_warn(dev, "coherent DMA mask is unset\n");
  150. goto no_page;
  151. }
  152. if ((~mask) & ISA_DMA_THRESHOLD) {
  153. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  154. "than system GFP_DMA mask %#llx\n",
  155. mask, (unsigned long long)ISA_DMA_THRESHOLD);
  156. goto no_page;
  157. }
  158. }
  159. /*
  160. * Sanity check the allocation size.
  161. */
  162. size = PAGE_ALIGN(size);
  163. limit = (mask + 1) & ~mask;
  164. if ((limit && size >= limit) ||
  165. size >= (CONSISTENT_END - CONSISTENT_BASE)) {
  166. printk(KERN_WARNING "coherent allocation too big "
  167. "(requested %#x mask %#llx)\n", size, mask);
  168. goto no_page;
  169. }
  170. order = get_order(size);
  171. if (mask != 0xffffffff)
  172. gfp |= GFP_DMA;
  173. page = alloc_pages(gfp, order);
  174. if (!page)
  175. goto no_page;
  176. /*
  177. * Invalidate any data that might be lurking in the
  178. * kernel direct-mapped region for device DMA.
  179. */
  180. {
  181. void *ptr = page_address(page);
  182. memset(ptr, 0, size);
  183. dmac_flush_range(ptr, ptr + size);
  184. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  185. }
  186. /*
  187. * Allocate a virtual address in the consistent mapping region.
  188. */
  189. c = arm_vm_region_alloc(&consistent_head, size,
  190. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  191. if (c) {
  192. pte_t *pte;
  193. struct page *end = page + (1 << order);
  194. int idx = CONSISTENT_PTE_INDEX(c->vm_start);
  195. u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  196. pte = consistent_pte[idx] + off;
  197. c->vm_pages = page;
  198. split_page(page, order);
  199. /*
  200. * Set the "dma handle"
  201. */
  202. *handle = page_to_dma(dev, page);
  203. do {
  204. BUG_ON(!pte_none(*pte));
  205. /*
  206. * x86 does not mark the pages reserved...
  207. */
  208. SetPageReserved(page);
  209. set_pte_ext(pte, mk_pte(page, prot), 0);
  210. page++;
  211. pte++;
  212. off++;
  213. if (off >= PTRS_PER_PTE) {
  214. off = 0;
  215. pte = consistent_pte[++idx];
  216. }
  217. } while (size -= PAGE_SIZE);
  218. /*
  219. * Free the otherwise unused pages.
  220. */
  221. while (page < end) {
  222. __free_page(page);
  223. page++;
  224. }
  225. return (void *)c->vm_start;
  226. }
  227. if (page)
  228. __free_pages(page, order);
  229. no_page:
  230. *handle = ~0;
  231. return NULL;
  232. }
  233. /*
  234. * Allocate DMA-coherent memory space and return both the kernel remapped
  235. * virtual and bus address for that space.
  236. */
  237. void *
  238. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  239. {
  240. void *memory;
  241. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  242. return memory;
  243. if (arch_is_coherent()) {
  244. void *virt;
  245. virt = kmalloc(size, gfp);
  246. if (!virt)
  247. return NULL;
  248. *handle = virt_to_dma(dev, virt);
  249. return virt;
  250. }
  251. return __dma_alloc(dev, size, handle, gfp,
  252. pgprot_noncached(pgprot_kernel));
  253. }
  254. EXPORT_SYMBOL(dma_alloc_coherent);
  255. /*
  256. * Allocate a writecombining region, in much the same way as
  257. * dma_alloc_coherent above.
  258. */
  259. void *
  260. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  261. {
  262. return __dma_alloc(dev, size, handle, gfp,
  263. pgprot_writecombine(pgprot_kernel));
  264. }
  265. EXPORT_SYMBOL(dma_alloc_writecombine);
  266. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  267. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  268. {
  269. unsigned long flags, user_size, kern_size;
  270. struct arm_vm_region *c;
  271. int ret = -ENXIO;
  272. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  273. spin_lock_irqsave(&consistent_lock, flags);
  274. c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  275. spin_unlock_irqrestore(&consistent_lock, flags);
  276. if (c) {
  277. unsigned long off = vma->vm_pgoff;
  278. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  279. if (off < kern_size &&
  280. user_size <= (kern_size - off)) {
  281. ret = remap_pfn_range(vma, vma->vm_start,
  282. page_to_pfn(c->vm_pages) + off,
  283. user_size << PAGE_SHIFT,
  284. vma->vm_page_prot);
  285. }
  286. }
  287. return ret;
  288. }
  289. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  290. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  291. {
  292. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  293. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  294. }
  295. EXPORT_SYMBOL(dma_mmap_coherent);
  296. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  297. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  298. {
  299. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  300. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  301. }
  302. EXPORT_SYMBOL(dma_mmap_writecombine);
  303. /*
  304. * free a page as defined by the above mapping.
  305. * Must not be called with IRQs disabled.
  306. */
  307. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  308. {
  309. struct arm_vm_region *c;
  310. unsigned long flags, addr;
  311. pte_t *ptep;
  312. int idx;
  313. u32 off;
  314. WARN_ON(irqs_disabled());
  315. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  316. return;
  317. if (arch_is_coherent()) {
  318. kfree(cpu_addr);
  319. return;
  320. }
  321. size = PAGE_ALIGN(size);
  322. spin_lock_irqsave(&consistent_lock, flags);
  323. c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  324. if (!c)
  325. goto no_area;
  326. c->vm_active = 0;
  327. spin_unlock_irqrestore(&consistent_lock, flags);
  328. if ((c->vm_end - c->vm_start) != size) {
  329. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  330. __func__, c->vm_end - c->vm_start, size);
  331. dump_stack();
  332. size = c->vm_end - c->vm_start;
  333. }
  334. idx = CONSISTENT_PTE_INDEX(c->vm_start);
  335. off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  336. ptep = consistent_pte[idx] + off;
  337. addr = c->vm_start;
  338. do {
  339. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  340. unsigned long pfn;
  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. pfn = pte_pfn(pte);
  350. if (pfn_valid(pfn)) {
  351. struct page *page = pfn_to_page(pfn);
  352. /*
  353. * x86 does not mark the pages reserved...
  354. */
  355. ClearPageReserved(page);
  356. __free_page(page);
  357. continue;
  358. }
  359. }
  360. printk(KERN_CRIT "%s: bad page in kernel page table\n",
  361. __func__);
  362. } while (size -= PAGE_SIZE);
  363. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  364. spin_lock_irqsave(&consistent_lock, flags);
  365. list_del(&c->vm_list);
  366. spin_unlock_irqrestore(&consistent_lock, flags);
  367. kfree(c);
  368. return;
  369. no_area:
  370. spin_unlock_irqrestore(&consistent_lock, flags);
  371. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  372. __func__, cpu_addr);
  373. dump_stack();
  374. }
  375. EXPORT_SYMBOL(dma_free_coherent);
  376. /*
  377. * Initialise the consistent memory allocation.
  378. */
  379. static int __init consistent_init(void)
  380. {
  381. pgd_t *pgd;
  382. pmd_t *pmd;
  383. pte_t *pte;
  384. int ret = 0, i = 0;
  385. u32 base = CONSISTENT_BASE;
  386. do {
  387. pgd = pgd_offset(&init_mm, base);
  388. pmd = pmd_alloc(&init_mm, pgd, base);
  389. if (!pmd) {
  390. printk(KERN_ERR "%s: no pmd tables\n", __func__);
  391. ret = -ENOMEM;
  392. break;
  393. }
  394. WARN_ON(!pmd_none(*pmd));
  395. pte = pte_alloc_kernel(pmd, base);
  396. if (!pte) {
  397. printk(KERN_ERR "%s: no pte tables\n", __func__);
  398. ret = -ENOMEM;
  399. break;
  400. }
  401. consistent_pte[i++] = pte;
  402. base += (1 << PGDIR_SHIFT);
  403. } while (base < CONSISTENT_END);
  404. return ret;
  405. }
  406. core_initcall(consistent_init);
  407. /*
  408. * Make an area consistent for devices.
  409. * Note: Drivers should NOT use this function directly, as it will break
  410. * platforms with CONFIG_DMABOUNCE.
  411. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  412. */
  413. void dma_cache_maint(const void *start, size_t size, int direction)
  414. {
  415. void (*inner_op)(const void *, const void *);
  416. void (*outer_op)(unsigned long, unsigned long);
  417. BUG_ON(!virt_addr_valid(start) || !virt_addr_valid(start + size - 1));
  418. switch (direction) {
  419. case DMA_FROM_DEVICE: /* invalidate only */
  420. inner_op = dmac_inv_range;
  421. outer_op = outer_inv_range;
  422. break;
  423. case DMA_TO_DEVICE: /* writeback only */
  424. inner_op = dmac_clean_range;
  425. outer_op = outer_clean_range;
  426. break;
  427. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  428. inner_op = dmac_flush_range;
  429. outer_op = outer_flush_range;
  430. break;
  431. default:
  432. BUG();
  433. }
  434. inner_op(start, start + size);
  435. outer_op(__pa(start), __pa(start) + size);
  436. }
  437. EXPORT_SYMBOL(dma_cache_maint);
  438. static void dma_cache_maint_contiguous(struct page *page, unsigned long offset,
  439. size_t size, int direction)
  440. {
  441. void *vaddr;
  442. unsigned long paddr;
  443. void (*inner_op)(const void *, const void *);
  444. void (*outer_op)(unsigned long, unsigned long);
  445. switch (direction) {
  446. case DMA_FROM_DEVICE: /* invalidate only */
  447. inner_op = dmac_inv_range;
  448. outer_op = outer_inv_range;
  449. break;
  450. case DMA_TO_DEVICE: /* writeback only */
  451. inner_op = dmac_clean_range;
  452. outer_op = outer_clean_range;
  453. break;
  454. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  455. inner_op = dmac_flush_range;
  456. outer_op = outer_flush_range;
  457. break;
  458. default:
  459. BUG();
  460. }
  461. if (!PageHighMem(page)) {
  462. vaddr = page_address(page) + offset;
  463. inner_op(vaddr, vaddr + size);
  464. } else {
  465. vaddr = kmap_high_get(page);
  466. if (vaddr) {
  467. vaddr += offset;
  468. inner_op(vaddr, vaddr + size);
  469. kunmap_high(page);
  470. }
  471. }
  472. paddr = page_to_phys(page) + offset;
  473. outer_op(paddr, paddr + size);
  474. }
  475. void dma_cache_maint_page(struct page *page, unsigned long offset,
  476. size_t size, int dir)
  477. {
  478. /*
  479. * A single sg entry may refer to multiple physically contiguous
  480. * pages. But we still need to process highmem pages individually.
  481. * If highmem is not configured then the bulk of this loop gets
  482. * optimized out.
  483. */
  484. size_t left = size;
  485. do {
  486. size_t len = left;
  487. if (PageHighMem(page) && len + offset > PAGE_SIZE) {
  488. if (offset >= PAGE_SIZE) {
  489. page += offset / PAGE_SIZE;
  490. offset %= PAGE_SIZE;
  491. }
  492. len = PAGE_SIZE - offset;
  493. }
  494. dma_cache_maint_contiguous(page, offset, len, dir);
  495. offset = 0;
  496. page++;
  497. left -= len;
  498. } while (left);
  499. }
  500. EXPORT_SYMBOL(dma_cache_maint_page);
  501. /**
  502. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  503. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  504. * @sg: list of buffers
  505. * @nents: number of buffers to map
  506. * @dir: DMA transfer direction
  507. *
  508. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  509. * This is the scatter-gather version of the dma_map_single interface.
  510. * Here the scatter gather list elements are each tagged with the
  511. * appropriate dma address and length. They are obtained via
  512. * sg_dma_{address,length}.
  513. *
  514. * Device ownership issues as mentioned for dma_map_single are the same
  515. * here.
  516. */
  517. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  518. enum dma_data_direction dir)
  519. {
  520. struct scatterlist *s;
  521. int i, j;
  522. for_each_sg(sg, s, nents, i) {
  523. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  524. s->length, dir);
  525. if (dma_mapping_error(dev, s->dma_address))
  526. goto bad_mapping;
  527. }
  528. return nents;
  529. bad_mapping:
  530. for_each_sg(sg, s, i, j)
  531. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  532. return 0;
  533. }
  534. EXPORT_SYMBOL(dma_map_sg);
  535. /**
  536. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  537. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  538. * @sg: list of buffers
  539. * @nents: number of buffers to unmap (returned from dma_map_sg)
  540. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  541. *
  542. * Unmap a set of streaming mode DMA translations. Again, CPU access
  543. * rules concerning calls here are the same as for dma_unmap_single().
  544. */
  545. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  546. enum dma_data_direction dir)
  547. {
  548. struct scatterlist *s;
  549. int i;
  550. for_each_sg(sg, s, nents, i)
  551. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  552. }
  553. EXPORT_SYMBOL(dma_unmap_sg);
  554. /**
  555. * dma_sync_sg_for_cpu
  556. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  557. * @sg: list of buffers
  558. * @nents: number of buffers to map (returned from dma_map_sg)
  559. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  560. */
  561. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  562. int nents, enum dma_data_direction dir)
  563. {
  564. struct scatterlist *s;
  565. int i;
  566. for_each_sg(sg, s, nents, i) {
  567. dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
  568. sg_dma_len(s), dir);
  569. }
  570. }
  571. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  572. /**
  573. * dma_sync_sg_for_device
  574. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  575. * @sg: list of buffers
  576. * @nents: number of buffers to map (returned from dma_map_sg)
  577. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  578. */
  579. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  580. int nents, enum dma_data_direction dir)
  581. {
  582. struct scatterlist *s;
  583. int i;
  584. for_each_sg(sg, s, nents, i) {
  585. if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
  586. sg_dma_len(s), dir))
  587. continue;
  588. if (!arch_is_coherent())
  589. dma_cache_maint_page(sg_page(s), s->offset,
  590. s->length, dir);
  591. }
  592. }
  593. EXPORT_SYMBOL(dma_sync_sg_for_device);