Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into for-linus
[sfrench/cifs-2.6.git] / arch / sparc64 / mm / tlb.c
1 /* arch/sparc64/mm/tlb.c
2  *
3  * Copyright (C) 2004 David S. Miller <davem@redhat.com>
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/percpu.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/preempt.h>
12
13 #include <asm/pgtable.h>
14 #include <asm/pgalloc.h>
15 #include <asm/tlbflush.h>
16 #include <asm/cacheflush.h>
17 #include <asm/mmu_context.h>
18 #include <asm/tlb.h>
19
20 /* Heavily inspired by the ppc64 code.  */
21
22 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers) = { 0, };
23
24 void flush_tlb_pending(void)
25 {
26         struct mmu_gather *mp;
27
28         preempt_disable();
29
30         mp = &__get_cpu_var(mmu_gathers);
31         if (mp->tlb_nr) {
32                 flush_tsb_user(mp);
33
34                 if (CTX_VALID(mp->mm->context)) {
35 #ifdef CONFIG_SMP
36                         smp_flush_tlb_pending(mp->mm, mp->tlb_nr,
37                                               &mp->vaddrs[0]);
38 #else
39                         __flush_tlb_pending(CTX_HWBITS(mp->mm->context),
40                                             mp->tlb_nr, &mp->vaddrs[0]);
41 #endif
42                 }
43                 mp->tlb_nr = 0;
44         }
45
46         preempt_enable();
47 }
48
49 void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr, pte_t *ptep, pte_t orig)
50 {
51         struct mmu_gather *mp = &__get_cpu_var(mmu_gathers);
52         unsigned long nr;
53
54         vaddr &= PAGE_MASK;
55         if (pte_exec(orig))
56                 vaddr |= 0x1UL;
57
58         if (tlb_type != hypervisor &&
59             pte_dirty(orig)) {
60                 unsigned long paddr, pfn = pte_pfn(orig);
61                 struct address_space *mapping;
62                 struct page *page;
63
64                 if (!pfn_valid(pfn))
65                         goto no_cache_flush;
66
67                 page = pfn_to_page(pfn);
68                 if (PageReserved(page))
69                         goto no_cache_flush;
70
71                 /* A real file page? */
72                 mapping = page_mapping(page);
73                 if (!mapping)
74                         goto no_cache_flush;
75
76                 paddr = (unsigned long) page_address(page);
77                 if ((paddr ^ vaddr) & (1 << 13))
78                         flush_dcache_page_all(mm, page);
79         }
80
81 no_cache_flush:
82
83         if (mp->fullmm)
84                 return;
85
86         nr = mp->tlb_nr;
87
88         if (unlikely(nr != 0 && mm != mp->mm)) {
89                 flush_tlb_pending();
90                 nr = 0;
91         }
92
93         if (nr == 0)
94                 mp->mm = mm;
95
96         mp->vaddrs[nr] = vaddr;
97         mp->tlb_nr = ++nr;
98         if (nr >= TLB_BATCH_NR)
99                 flush_tlb_pending();
100 }