]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - arch/arm/mm/dma-mapping.c
ARM: integrate CMA with DMA-mapping subsystem
[can-eth-gw-linux.git] / arch / arm / mm / dma-mapping.c
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/dma-contiguous.h>
21 #include <linux/highmem.h>
22 #include <linux/memblock.h>
23 #include <linux/slab.h>
24
25 #include <asm/memory.h>
26 #include <asm/highmem.h>
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
29 #include <asm/sizes.h>
30 #include <asm/mach/arch.h>
31 #include <asm/mach/map.h>
32 #include <asm/system_info.h>
33 #include <asm/dma-contiguous.h>
34
35 #include "mm.h"
36
37 static u64 get_coherent_dma_mask(struct device *dev)
38 {
39         u64 mask = (u64)arm_dma_limit;
40
41         if (dev) {
42                 mask = dev->coherent_dma_mask;
43
44                 /*
45                  * Sanity check the DMA mask - it must be non-zero, and
46                  * must be able to be satisfied by a DMA allocation.
47                  */
48                 if (mask == 0) {
49                         dev_warn(dev, "coherent DMA mask is unset\n");
50                         return 0;
51                 }
52
53                 if ((~mask) & (u64)arm_dma_limit) {
54                         dev_warn(dev, "coherent DMA mask %#llx is smaller "
55                                  "than system GFP_DMA mask %#llx\n",
56                                  mask, (u64)arm_dma_limit);
57                         return 0;
58                 }
59         }
60
61         return mask;
62 }
63
64 static void __dma_clear_buffer(struct page *page, size_t size)
65 {
66         void *ptr;
67         /*
68          * Ensure that the allocated pages are zeroed, and that any data
69          * lurking in the kernel direct-mapped region is invalidated.
70          */
71         ptr = page_address(page);
72         memset(ptr, 0, size);
73         dmac_flush_range(ptr, ptr + size);
74         outer_flush_range(__pa(ptr), __pa(ptr) + size);
75 }
76
77 /*
78  * Allocate a DMA buffer for 'dev' of size 'size' using the
79  * specified gfp mask.  Note that 'size' must be page aligned.
80  */
81 static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
82 {
83         unsigned long order = get_order(size);
84         struct page *page, *p, *e;
85
86         page = alloc_pages(gfp, order);
87         if (!page)
88                 return NULL;
89
90         /*
91          * Now split the huge page and free the excess pages
92          */
93         split_page(page, order);
94         for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
95                 __free_page(p);
96
97         __dma_clear_buffer(page, size);
98
99         return page;
100 }
101
102 /*
103  * Free a DMA buffer.  'size' must be page aligned.
104  */
105 static void __dma_free_buffer(struct page *page, size_t size)
106 {
107         struct page *e = page + (size >> PAGE_SHIFT);
108
109         while (page < e) {
110                 __free_page(page);
111                 page++;
112         }
113 }
114
115 #ifdef CONFIG_MMU
116
117 #define CONSISTENT_OFFSET(x)    (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
118 #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
119
120 /*
121  * These are the page tables (2MB each) covering uncached, DMA consistent allocations
122  */
123 static pte_t **consistent_pte;
124
125 #define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
126
127 unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
128
129 void __init init_consistent_dma_size(unsigned long size)
130 {
131         unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
132
133         BUG_ON(consistent_pte); /* Check we're called before DMA region init */
134         BUG_ON(base < VMALLOC_END);
135
136         /* Grow region to accommodate specified size  */
137         if (base < consistent_base)
138                 consistent_base = base;
139 }
140
141 #include "vmregion.h"
142
143 static struct arm_vmregion_head consistent_head = {
144         .vm_lock        = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
145         .vm_list        = LIST_HEAD_INIT(consistent_head.vm_list),
146         .vm_end         = CONSISTENT_END,
147 };
148
149 #ifdef CONFIG_HUGETLB_PAGE
150 #error ARM Coherent DMA allocator does not (yet) support huge TLB
151 #endif
152
153 /*
154  * Initialise the consistent memory allocation.
155  */
156 static int __init consistent_init(void)
157 {
158         int ret = 0;
159         pgd_t *pgd;
160         pud_t *pud;
161         pmd_t *pmd;
162         pte_t *pte;
163         int i = 0;
164         unsigned long base = consistent_base;
165         unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
166
167         if (cpu_architecture() >= CPU_ARCH_ARMv6)
168                 return 0;
169
170         consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
171         if (!consistent_pte) {
172                 pr_err("%s: no memory\n", __func__);
173                 return -ENOMEM;
174         }
175
176         pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
177         consistent_head.vm_start = base;
178
179         do {
180                 pgd = pgd_offset(&init_mm, base);
181
182                 pud = pud_alloc(&init_mm, pgd, base);
183                 if (!pud) {
184                         printk(KERN_ERR "%s: no pud tables\n", __func__);
185                         ret = -ENOMEM;
186                         break;
187                 }
188
189                 pmd = pmd_alloc(&init_mm, pud, base);
190                 if (!pmd) {
191                         printk(KERN_ERR "%s: no pmd tables\n", __func__);
192                         ret = -ENOMEM;
193                         break;
194                 }
195                 WARN_ON(!pmd_none(*pmd));
196
197                 pte = pte_alloc_kernel(pmd, base);
198                 if (!pte) {
199                         printk(KERN_ERR "%s: no pte tables\n", __func__);
200                         ret = -ENOMEM;
201                         break;
202                 }
203
204                 consistent_pte[i++] = pte;
205                 base += PMD_SIZE;
206         } while (base < CONSISTENT_END);
207
208         return ret;
209 }
210 core_initcall(consistent_init);
211
212 static void *__alloc_from_contiguous(struct device *dev, size_t size,
213                                      pgprot_t prot, struct page **ret_page);
214
215 static struct arm_vmregion_head coherent_head = {
216         .vm_lock        = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
217         .vm_list        = LIST_HEAD_INIT(coherent_head.vm_list),
218 };
219
220 size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
221
222 static int __init early_coherent_pool(char *p)
223 {
224         coherent_pool_size = memparse(p, &p);
225         return 0;
226 }
227 early_param("coherent_pool", early_coherent_pool);
228
229 /*
230  * Initialise the coherent pool for atomic allocations.
231  */
232 static int __init coherent_init(void)
233 {
234         pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
235         size_t size = coherent_pool_size;
236         struct page *page;
237         void *ptr;
238
239         if (cpu_architecture() < CPU_ARCH_ARMv6)
240                 return 0;
241
242         ptr = __alloc_from_contiguous(NULL, size, prot, &page);
243         if (ptr) {
244                 coherent_head.vm_start = (unsigned long) ptr;
245                 coherent_head.vm_end = (unsigned long) ptr + size;
246                 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
247                        (unsigned)size / 1024);
248                 return 0;
249         }
250         printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
251                (unsigned)size / 1024);
252         return -ENOMEM;
253 }
254 /*
255  * CMA is activated by core_initcall, so we must be called after it.
256  */
257 postcore_initcall(coherent_init);
258
259 struct dma_contig_early_reserve {
260         phys_addr_t base;
261         unsigned long size;
262 };
263
264 static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
265
266 static int dma_mmu_remap_num __initdata;
267
268 void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
269 {
270         dma_mmu_remap[dma_mmu_remap_num].base = base;
271         dma_mmu_remap[dma_mmu_remap_num].size = size;
272         dma_mmu_remap_num++;
273 }
274
275 void __init dma_contiguous_remap(void)
276 {
277         int i;
278         for (i = 0; i < dma_mmu_remap_num; i++) {
279                 phys_addr_t start = dma_mmu_remap[i].base;
280                 phys_addr_t end = start + dma_mmu_remap[i].size;
281                 struct map_desc map;
282                 unsigned long addr;
283
284                 if (end > arm_lowmem_limit)
285                         end = arm_lowmem_limit;
286                 if (start >= end)
287                         return;
288
289                 map.pfn = __phys_to_pfn(start);
290                 map.virtual = __phys_to_virt(start);
291                 map.length = end - start;
292                 map.type = MT_MEMORY_DMA_READY;
293
294                 /*
295                  * Clear previous low-memory mapping
296                  */
297                 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
298                      addr += PGDIR_SIZE)
299                         pmd_clear(pmd_off_k(addr));
300
301                 iotable_init(&map, 1);
302         }
303 }
304
305 static void *
306 __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
307         const void *caller)
308 {
309         struct arm_vmregion *c;
310         size_t align;
311         int bit;
312
313         if (!consistent_pte) {
314                 printk(KERN_ERR "%s: not initialised\n", __func__);
315                 dump_stack();
316                 return NULL;
317         }
318
319         /*
320          * Align the virtual region allocation - maximum alignment is
321          * a section size, minimum is a page size.  This helps reduce
322          * fragmentation of the DMA space, and also prevents allocations
323          * smaller than a section from crossing a section boundary.
324          */
325         bit = fls(size - 1);
326         if (bit > SECTION_SHIFT)
327                 bit = SECTION_SHIFT;
328         align = 1 << bit;
329
330         /*
331          * Allocate a virtual address in the consistent mapping region.
332          */
333         c = arm_vmregion_alloc(&consistent_head, align, size,
334                             gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
335         if (c) {
336                 pte_t *pte;
337                 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
338                 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
339
340                 pte = consistent_pte[idx] + off;
341                 c->vm_pages = page;
342
343                 do {
344                         BUG_ON(!pte_none(*pte));
345
346                         set_pte_ext(pte, mk_pte(page, prot), 0);
347                         page++;
348                         pte++;
349                         off++;
350                         if (off >= PTRS_PER_PTE) {
351                                 off = 0;
352                                 pte = consistent_pte[++idx];
353                         }
354                 } while (size -= PAGE_SIZE);
355
356                 dsb();
357
358                 return (void *)c->vm_start;
359         }
360         return NULL;
361 }
362
363 static void __dma_free_remap(void *cpu_addr, size_t size)
364 {
365         struct arm_vmregion *c;
366         unsigned long addr;
367         pte_t *ptep;
368         int idx;
369         u32 off;
370
371         c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
372         if (!c) {
373                 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
374                        __func__, cpu_addr);
375                 dump_stack();
376                 return;
377         }
378
379         if ((c->vm_end - c->vm_start) != size) {
380                 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
381                        __func__, c->vm_end - c->vm_start, size);
382                 dump_stack();
383                 size = c->vm_end - c->vm_start;
384         }
385
386         idx = CONSISTENT_PTE_INDEX(c->vm_start);
387         off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
388         ptep = consistent_pte[idx] + off;
389         addr = c->vm_start;
390         do {
391                 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
392
393                 ptep++;
394                 addr += PAGE_SIZE;
395                 off++;
396                 if (off >= PTRS_PER_PTE) {
397                         off = 0;
398                         ptep = consistent_pte[++idx];
399                 }
400
401                 if (pte_none(pte) || !pte_present(pte))
402                         printk(KERN_CRIT "%s: bad page in kernel page table\n",
403                                __func__);
404         } while (size -= PAGE_SIZE);
405
406         flush_tlb_kernel_range(c->vm_start, c->vm_end);
407
408         arm_vmregion_free(&consistent_head, c);
409 }
410
411 static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
412                             void *data)
413 {
414         struct page *page = virt_to_page(addr);
415         pgprot_t prot = *(pgprot_t *)data;
416
417         set_pte_ext(pte, mk_pte(page, prot), 0);
418         return 0;
419 }
420
421 static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
422 {
423         unsigned long start = (unsigned long) page_address(page);
424         unsigned end = start + size;
425
426         apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
427         dsb();
428         flush_tlb_kernel_range(start, end);
429 }
430
431 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
432                                  pgprot_t prot, struct page **ret_page,
433                                  const void *caller)
434 {
435         struct page *page;
436         void *ptr;
437         page = __dma_alloc_buffer(dev, size, gfp);
438         if (!page)
439                 return NULL;
440
441         ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
442         if (!ptr) {
443                 __dma_free_buffer(page, size);
444                 return NULL;
445         }
446
447         *ret_page = page;
448         return ptr;
449 }
450
451 static void *__alloc_from_pool(struct device *dev, size_t size,
452                                struct page **ret_page, const void *caller)
453 {
454         struct arm_vmregion *c;
455         size_t align;
456
457         if (!coherent_head.vm_start) {
458                 printk(KERN_ERR "%s: coherent pool not initialised!\n",
459                        __func__);
460                 dump_stack();
461                 return NULL;
462         }
463
464         /*
465          * Align the region allocation - allocations from pool are rather
466          * small, so align them to their order in pages, minimum is a page
467          * size. This helps reduce fragmentation of the DMA space.
468          */
469         align = PAGE_SIZE << get_order(size);
470         c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
471         if (c) {
472                 void *ptr = (void *)c->vm_start;
473                 struct page *page = virt_to_page(ptr);
474                 *ret_page = page;
475                 return ptr;
476         }
477         return NULL;
478 }
479
480 static int __free_from_pool(void *cpu_addr, size_t size)
481 {
482         unsigned long start = (unsigned long)cpu_addr;
483         unsigned long end = start + size;
484         struct arm_vmregion *c;
485
486         if (start < coherent_head.vm_start || end > coherent_head.vm_end)
487                 return 0;
488
489         c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
490
491         if ((c->vm_end - c->vm_start) != size) {
492                 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
493                        __func__, c->vm_end - c->vm_start, size);
494                 dump_stack();
495                 size = c->vm_end - c->vm_start;
496         }
497
498         arm_vmregion_free(&coherent_head, c);
499         return 1;
500 }
501
502 static void *__alloc_from_contiguous(struct device *dev, size_t size,
503                                      pgprot_t prot, struct page **ret_page)
504 {
505         unsigned long order = get_order(size);
506         size_t count = size >> PAGE_SHIFT;
507         struct page *page;
508
509         page = dma_alloc_from_contiguous(dev, count, order);
510         if (!page)
511                 return NULL;
512
513         __dma_clear_buffer(page, size);
514         __dma_remap(page, size, prot);
515
516         *ret_page = page;
517         return page_address(page);
518 }
519
520 static void __free_from_contiguous(struct device *dev, struct page *page,
521                                    size_t size)
522 {
523         __dma_remap(page, size, pgprot_kernel);
524         dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
525 }
526
527 #define nommu() 0
528
529 #else   /* !CONFIG_MMU */
530
531 #define nommu() 1
532
533 #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c)      NULL
534 #define __alloc_from_pool(dev, size, ret_page, c)               NULL
535 #define __alloc_from_contiguous(dev, size, prot, ret)           NULL
536 #define __free_from_pool(cpu_addr, size)                        0
537 #define __free_from_contiguous(dev, page, size)                 do { } while (0)
538 #define __dma_free_remap(cpu_addr, size)                        do { } while (0)
539
540 #endif  /* CONFIG_MMU */
541
542 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
543                                    struct page **ret_page)
544 {
545         struct page *page;
546         page = __dma_alloc_buffer(dev, size, gfp);
547         if (!page)
548                 return NULL;
549
550         *ret_page = page;
551         return page_address(page);
552 }
553
554
555
556 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
557                          gfp_t gfp, pgprot_t prot, const void *caller)
558 {
559         u64 mask = get_coherent_dma_mask(dev);
560         struct page *page;
561         void *addr;
562
563 #ifdef CONFIG_DMA_API_DEBUG
564         u64 limit = (mask + 1) & ~mask;
565         if (limit && size >= limit) {
566                 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
567                         size, mask);
568                 return NULL;
569         }
570 #endif
571
572         if (!mask)
573                 return NULL;
574
575         if (mask < 0xffffffffULL)
576                 gfp |= GFP_DMA;
577
578         /*
579          * Following is a work-around (a.k.a. hack) to prevent pages
580          * with __GFP_COMP being passed to split_page() which cannot
581          * handle them.  The real problem is that this flag probably
582          * should be 0 on ARM as it is not supported on this
583          * platform; see CONFIG_HUGETLBFS.
584          */
585         gfp &= ~(__GFP_COMP);
586
587         *handle = ~0;
588         size = PAGE_ALIGN(size);
589
590         if (arch_is_coherent() || nommu())
591                 addr = __alloc_simple_buffer(dev, size, gfp, &page);
592         else if (cpu_architecture() < CPU_ARCH_ARMv6)
593                 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
594         else if (gfp & GFP_ATOMIC)
595                 addr = __alloc_from_pool(dev, size, &page, caller);
596         else
597                 addr = __alloc_from_contiguous(dev, size, prot, &page);
598
599         if (addr)
600                 *handle = pfn_to_dma(dev, page_to_pfn(page));
601
602         return addr;
603 }
604
605 /*
606  * Allocate DMA-coherent memory space and return both the kernel remapped
607  * virtual and bus address for that space.
608  */
609 void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle,
610                          gfp_t gfp)
611 {
612         void *memory;
613
614         if (dma_alloc_from_coherent(dev, size, handle, &memory))
615                 return memory;
616
617         return __dma_alloc(dev, size, handle, gfp,
618                            pgprot_dmacoherent(pgprot_kernel),
619                            __builtin_return_address(0));
620 }
621 EXPORT_SYMBOL(dma_alloc_coherent);
622
623 /*
624  * Allocate a writecombining region, in much the same way as
625  * dma_alloc_coherent above.
626  */
627 void *
628 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
629 {
630         return __dma_alloc(dev, size, handle, gfp,
631                            pgprot_writecombine(pgprot_kernel),
632                            __builtin_return_address(0));
633 }
634 EXPORT_SYMBOL(dma_alloc_writecombine);
635
636 static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
637                     void *cpu_addr, dma_addr_t dma_addr, size_t size)
638 {
639         int ret = -ENXIO;
640 #ifdef CONFIG_MMU
641         unsigned long pfn = dma_to_pfn(dev, dma_addr);
642         ret = remap_pfn_range(vma, vma->vm_start,
643                               pfn + vma->vm_pgoff,
644                               vma->vm_end - vma->vm_start,
645                               vma->vm_page_prot);
646 #endif  /* CONFIG_MMU */
647
648         return ret;
649 }
650
651 int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
652                       void *cpu_addr, dma_addr_t dma_addr, size_t size)
653 {
654         vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
655         return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
656 }
657 EXPORT_SYMBOL(dma_mmap_coherent);
658
659 int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
660                           void *cpu_addr, dma_addr_t dma_addr, size_t size)
661 {
662         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
663         return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
664 }
665 EXPORT_SYMBOL(dma_mmap_writecombine);
666
667
668 /*
669  * Free a buffer as defined by the above mapping.
670  */
671 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
672 {
673         struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
674
675         if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
676                 return;
677
678         size = PAGE_ALIGN(size);
679
680         if (arch_is_coherent() || nommu()) {
681                 __dma_free_buffer(page, size);
682         } else if (cpu_architecture() < CPU_ARCH_ARMv6) {
683                 __dma_free_remap(cpu_addr, size);
684                 __dma_free_buffer(page, size);
685         } else {
686                 if (__free_from_pool(cpu_addr, size))
687                         return;
688                 /*
689                  * Non-atomic allocations cannot be freed with IRQs disabled
690                  */
691                 WARN_ON(irqs_disabled());
692                 __free_from_contiguous(dev, page, size);
693         }
694 }
695 EXPORT_SYMBOL(dma_free_coherent);
696
697 /*
698  * Make an area consistent for devices.
699  * Note: Drivers should NOT use this function directly, as it will break
700  * platforms with CONFIG_DMABOUNCE.
701  * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
702  */
703 void ___dma_single_cpu_to_dev(const void *kaddr, size_t size,
704         enum dma_data_direction dir)
705 {
706         unsigned long paddr;
707
708         BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
709
710         dmac_map_area(kaddr, size, dir);
711
712         paddr = __pa(kaddr);
713         if (dir == DMA_FROM_DEVICE) {
714                 outer_inv_range(paddr, paddr + size);
715         } else {
716                 outer_clean_range(paddr, paddr + size);
717         }
718         /* FIXME: non-speculating: flush on bidirectional mappings? */
719 }
720 EXPORT_SYMBOL(___dma_single_cpu_to_dev);
721
722 void ___dma_single_dev_to_cpu(const void *kaddr, size_t size,
723         enum dma_data_direction dir)
724 {
725         BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
726
727         /* FIXME: non-speculating: not required */
728         /* don't bother invalidating if DMA to device */
729         if (dir != DMA_TO_DEVICE) {
730                 unsigned long paddr = __pa(kaddr);
731                 outer_inv_range(paddr, paddr + size);
732         }
733
734         dmac_unmap_area(kaddr, size, dir);
735 }
736 EXPORT_SYMBOL(___dma_single_dev_to_cpu);
737
738 static void dma_cache_maint_page(struct page *page, unsigned long offset,
739         size_t size, enum dma_data_direction dir,
740         void (*op)(const void *, size_t, int))
741 {
742         /*
743          * A single sg entry may refer to multiple physically contiguous
744          * pages.  But we still need to process highmem pages individually.
745          * If highmem is not configured then the bulk of this loop gets
746          * optimized out.
747          */
748         size_t left = size;
749         do {
750                 size_t len = left;
751                 void *vaddr;
752
753                 if (PageHighMem(page)) {
754                         if (len + offset > PAGE_SIZE) {
755                                 if (offset >= PAGE_SIZE) {
756                                         page += offset / PAGE_SIZE;
757                                         offset %= PAGE_SIZE;
758                                 }
759                                 len = PAGE_SIZE - offset;
760                         }
761                         vaddr = kmap_high_get(page);
762                         if (vaddr) {
763                                 vaddr += offset;
764                                 op(vaddr, len, dir);
765                                 kunmap_high(page);
766                         } else if (cache_is_vipt()) {
767                                 /* unmapped pages might still be cached */
768                                 vaddr = kmap_atomic(page);
769                                 op(vaddr + offset, len, dir);
770                                 kunmap_atomic(vaddr);
771                         }
772                 } else {
773                         vaddr = page_address(page) + offset;
774                         op(vaddr, len, dir);
775                 }
776                 offset = 0;
777                 page++;
778                 left -= len;
779         } while (left);
780 }
781
782 void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
783         size_t size, enum dma_data_direction dir)
784 {
785         unsigned long paddr;
786
787         dma_cache_maint_page(page, off, size, dir, dmac_map_area);
788
789         paddr = page_to_phys(page) + off;
790         if (dir == DMA_FROM_DEVICE) {
791                 outer_inv_range(paddr, paddr + size);
792         } else {
793                 outer_clean_range(paddr, paddr + size);
794         }
795         /* FIXME: non-speculating: flush on bidirectional mappings? */
796 }
797 EXPORT_SYMBOL(___dma_page_cpu_to_dev);
798
799 void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
800         size_t size, enum dma_data_direction dir)
801 {
802         unsigned long paddr = page_to_phys(page) + off;
803
804         /* FIXME: non-speculating: not required */
805         /* don't bother invalidating if DMA to device */
806         if (dir != DMA_TO_DEVICE)
807                 outer_inv_range(paddr, paddr + size);
808
809         dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
810
811         /*
812          * Mark the D-cache clean for this page to avoid extra flushing.
813          */
814         if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
815                 set_bit(PG_dcache_clean, &page->flags);
816 }
817 EXPORT_SYMBOL(___dma_page_dev_to_cpu);
818
819 /**
820  * dma_map_sg - map a set of SG buffers for streaming mode DMA
821  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
822  * @sg: list of buffers
823  * @nents: number of buffers to map
824  * @dir: DMA transfer direction
825  *
826  * Map a set of buffers described by scatterlist in streaming mode for DMA.
827  * This is the scatter-gather version of the dma_map_single interface.
828  * Here the scatter gather list elements are each tagged with the
829  * appropriate dma address and length.  They are obtained via
830  * sg_dma_{address,length}.
831  *
832  * Device ownership issues as mentioned for dma_map_single are the same
833  * here.
834  */
835 int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
836                 enum dma_data_direction dir)
837 {
838         struct scatterlist *s;
839         int i, j;
840
841         BUG_ON(!valid_dma_direction(dir));
842
843         for_each_sg(sg, s, nents, i) {
844                 s->dma_address = __dma_map_page(dev, sg_page(s), s->offset,
845                                                 s->length, dir);
846                 if (dma_mapping_error(dev, s->dma_address))
847                         goto bad_mapping;
848         }
849         debug_dma_map_sg(dev, sg, nents, nents, dir);
850         return nents;
851
852  bad_mapping:
853         for_each_sg(sg, s, i, j)
854                 __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
855         return 0;
856 }
857 EXPORT_SYMBOL(dma_map_sg);
858
859 /**
860  * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
861  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
862  * @sg: list of buffers
863  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
864  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
865  *
866  * Unmap a set of streaming mode DMA translations.  Again, CPU access
867  * rules concerning calls here are the same as for dma_unmap_single().
868  */
869 void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
870                 enum dma_data_direction dir)
871 {
872         struct scatterlist *s;
873         int i;
874
875         debug_dma_unmap_sg(dev, sg, nents, dir);
876
877         for_each_sg(sg, s, nents, i)
878                 __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
879 }
880 EXPORT_SYMBOL(dma_unmap_sg);
881
882 /**
883  * dma_sync_sg_for_cpu
884  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
885  * @sg: list of buffers
886  * @nents: number of buffers to map (returned from dma_map_sg)
887  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
888  */
889 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
890                         int nents, enum dma_data_direction dir)
891 {
892         struct scatterlist *s;
893         int i;
894
895         for_each_sg(sg, s, nents, i) {
896                 if (!dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
897                                             sg_dma_len(s), dir))
898                         continue;
899
900                 __dma_page_dev_to_cpu(sg_page(s), s->offset,
901                                       s->length, dir);
902         }
903
904         debug_dma_sync_sg_for_cpu(dev, sg, nents, dir);
905 }
906 EXPORT_SYMBOL(dma_sync_sg_for_cpu);
907
908 /**
909  * dma_sync_sg_for_device
910  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
911  * @sg: list of buffers
912  * @nents: number of buffers to map (returned from dma_map_sg)
913  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
914  */
915 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
916                         int nents, enum dma_data_direction dir)
917 {
918         struct scatterlist *s;
919         int i;
920
921         for_each_sg(sg, s, nents, i) {
922                 if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
923                                         sg_dma_len(s), dir))
924                         continue;
925
926                 __dma_page_cpu_to_dev(sg_page(s), s->offset,
927                                       s->length, dir);
928         }
929
930         debug_dma_sync_sg_for_device(dev, sg, nents, dir);
931 }
932 EXPORT_SYMBOL(dma_sync_sg_for_device);
933
934 /*
935  * Return whether the given device DMA address mask can be supported
936  * properly.  For example, if your device can only drive the low 24-bits
937  * during bus mastering, then you would pass 0x00ffffff as the mask
938  * to this function.
939  */
940 int dma_supported(struct device *dev, u64 mask)
941 {
942         if (mask < (u64)arm_dma_limit)
943                 return 0;
944         return 1;
945 }
946 EXPORT_SYMBOL(dma_supported);
947
948 int dma_set_mask(struct device *dev, u64 dma_mask)
949 {
950         if (!dev->dma_mask || !dma_supported(dev, dma_mask))
951                 return -EIO;
952
953 #ifndef CONFIG_DMABOUNCE
954         *dev->dma_mask = dma_mask;
955 #endif
956
957         return 0;
958 }
959 EXPORT_SYMBOL(dma_set_mask);
960
961 #define PREALLOC_DMA_DEBUG_ENTRIES      4096
962
963 static int __init dma_debug_do_init(void)
964 {
965 #ifdef CONFIG_MMU
966         arm_vmregion_create_proc("dma-mappings", &consistent_head);
967 #endif
968         dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
969         return 0;
970 }
971 fs_initcall(dma_debug_do_init);