dma-mapping.c 18 KB

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