dma-mapping.c 19 KB

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