MIPS: mm: Consolidate drop_mmu_context() has-ASID checks
[linux-2.6-microblaze.git] / arch / mips / include / asm / mmu_context.h
1 /*
2  * Switch a MMU context.
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 1996, 1997, 1998, 1999 by Ralf Baechle
9  * Copyright (C) 1999 Silicon Graphics, Inc.
10  */
11 #ifndef _ASM_MMU_CONTEXT_H
12 #define _ASM_MMU_CONTEXT_H
13
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/mm_types.h>
17 #include <linux/smp.h>
18 #include <linux/slab.h>
19
20 #include <asm/cacheflush.h>
21 #include <asm/dsemul.h>
22 #include <asm/hazards.h>
23 #include <asm/tlbflush.h>
24 #include <asm-generic/mm_hooks.h>
25
26 #define htw_set_pwbase(pgd)                                             \
27 do {                                                                    \
28         if (cpu_has_htw) {                                              \
29                 write_c0_pwbase(pgd);                                   \
30                 back_to_back_c0_hazard();                               \
31         }                                                               \
32 } while (0)
33
34 extern void tlbmiss_handler_setup_pgd(unsigned long);
35 extern char tlbmiss_handler_setup_pgd_end[];
36
37 /* Note: This is also implemented with uasm in arch/mips/kvm/entry.c */
38 #define TLBMISS_HANDLER_SETUP_PGD(pgd)                                  \
39 do {                                                                    \
40         tlbmiss_handler_setup_pgd((unsigned long)(pgd));                \
41         htw_set_pwbase((unsigned long)pgd);                             \
42 } while (0)
43
44 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
45
46 #define TLBMISS_HANDLER_RESTORE()                                       \
47         write_c0_xcontext((unsigned long) smp_processor_id() <<         \
48                           SMP_CPUID_REGSHIFT)
49
50 #define TLBMISS_HANDLER_SETUP()                                         \
51         do {                                                            \
52                 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);              \
53                 TLBMISS_HANDLER_RESTORE();                              \
54         } while (0)
55
56 #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using  pgd_current*/
57
58 /*
59  * For the fast tlb miss handlers, we keep a per cpu array of pointers
60  * to the current pgd for each processor. Also, the proc. id is stuffed
61  * into the context register.
62  */
63 extern unsigned long pgd_current[];
64
65 #define TLBMISS_HANDLER_RESTORE()                                       \
66         write_c0_context((unsigned long) smp_processor_id() <<          \
67                          SMP_CPUID_REGSHIFT)
68
69 #define TLBMISS_HANDLER_SETUP()                                         \
70         TLBMISS_HANDLER_RESTORE();                                      \
71         back_to_back_c0_hazard();                                       \
72         TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
73 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
74
75 /*
76  *  All unused by hardware upper bits will be considered
77  *  as a software asid extension.
78  */
79 static inline u64 asid_version_mask(unsigned int cpu)
80 {
81         unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
82
83         return ~(u64)(asid_mask | (asid_mask - 1));
84 }
85
86 static inline u64 asid_first_version(unsigned int cpu)
87 {
88         return ~asid_version_mask(cpu) + 1;
89 }
90
91 #define cpu_context(cpu, mm)    ((mm)->context.asid[cpu])
92 #define asid_cache(cpu)         (cpu_data[cpu].asid_cache)
93 #define cpu_asid(cpu, mm) \
94         (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
95
96 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
97 {
98 }
99
100
101 /* Normal, classic MIPS get_new_mmu_context */
102 static inline void
103 get_new_mmu_context(struct mm_struct *mm)
104 {
105         unsigned int cpu;
106         u64 asid;
107
108         cpu = smp_processor_id();
109         asid = asid_cache(cpu);
110
111         if (!((asid += cpu_asid_inc()) & cpu_asid_mask(&cpu_data[cpu]))) {
112                 if (cpu_has_vtag_icache)
113                         flush_icache_all();
114                 local_flush_tlb_all();  /* start new asid cycle */
115         }
116
117         cpu_context(cpu, mm) = asid_cache(cpu) = asid;
118 }
119
120 /*
121  * Initialize the context related info for a new mm_struct
122  * instance.
123  */
124 static inline int
125 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
126 {
127         int i;
128
129         for_each_possible_cpu(i)
130                 cpu_context(i, mm) = 0;
131
132         mm->context.bd_emupage_allocmap = NULL;
133         spin_lock_init(&mm->context.bd_emupage_lock);
134         init_waitqueue_head(&mm->context.bd_emupage_queue);
135
136         return 0;
137 }
138
139 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
140                              struct task_struct *tsk)
141 {
142         unsigned int cpu = smp_processor_id();
143         unsigned long flags;
144         local_irq_save(flags);
145
146         htw_stop();
147         /* Check if our ASID is of an older version and thus invalid */
148         if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & asid_version_mask(cpu))
149                 get_new_mmu_context(next);
150         write_c0_entryhi(cpu_asid(cpu, next));
151         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
152
153         /*
154          * Mark current->active_mm as not "active" anymore.
155          * We don't want to mislead possible IPI tlb flush routines.
156          */
157         cpumask_clear_cpu(cpu, mm_cpumask(prev));
158         cpumask_set_cpu(cpu, mm_cpumask(next));
159         htw_start();
160
161         local_irq_restore(flags);
162 }
163
164 /*
165  * Destroy context related info for an mm_struct that is about
166  * to be put to rest.
167  */
168 static inline void destroy_context(struct mm_struct *mm)
169 {
170         dsemul_mm_cleanup(mm);
171 }
172
173 #define activate_mm(prev, next) switch_mm(prev, next, current)
174 #define deactivate_mm(tsk, mm)  do { } while (0)
175
176 /*
177  * If mm is currently active_mm, we can't really drop it.  Instead,
178  * we will get a new one for it.
179  */
180 static inline void
181 drop_mmu_context(struct mm_struct *mm)
182 {
183         unsigned long flags;
184         unsigned int cpu;
185
186         local_irq_save(flags);
187
188         cpu = smp_processor_id();
189         if (!cpu_context(cpu, mm)) {
190                 /* no-op */
191         } else if (cpumask_test_cpu(cpu, mm_cpumask(mm)))  {
192                 htw_stop();
193                 get_new_mmu_context(mm);
194                 write_c0_entryhi(cpu_asid(cpu, mm));
195                 htw_start();
196         } else {
197                 /* will get a new context next time */
198                 cpu_context(cpu, mm) = 0;
199         }
200
201         local_irq_restore(flags);
202 }
203
204 #endif /* _ASM_MMU_CONTEXT_H */