dma-mapping.c 21 KB

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