2 * Switch a MMU context.
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
8 * Copyright (C) 1996, 1997, 1998, 1999 by Ralf Baechle
9 * Copyright (C) 1999 Silicon Graphics, Inc.
11 #ifndef _ASM_MMU_CONTEXT_H
12 #define _ASM_MMU_CONTEXT_H
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/slab.h>
18 #include <asm/cacheflush.h>
19 #include <asm/hazards.h>
20 #include <asm/tlbflush.h>
21 #ifdef CONFIG_MIPS_MT_SMTC
22 #include <asm/mipsmtregs.h>
25 #include <asm-generic/mm_hooks.h>
27 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
29 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
31 void (*tlbmiss_handler_setup_pgd)(unsigned long); \
32 extern u32 tlbmiss_handler_setup_pgd_array[16]; \
34 tlbmiss_handler_setup_pgd = \
35 (__typeof__(tlbmiss_handler_setup_pgd)) tlbmiss_handler_setup_pgd_array; \
36 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
39 #define TLBMISS_HANDLER_SETUP() \
41 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
42 write_c0_xcontext((unsigned long) smp_processor_id() << 51); \
45 #else /* CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
48 * For the fast tlb miss handlers, we keep a per cpu array of pointers
49 * to the current pgd for each processor. Also, the proc. id is stuffed
50 * into the context register.
52 extern unsigned long pgd_current[];
54 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
55 pgd_current[smp_processor_id()] = (unsigned long)(pgd)
58 #define TLBMISS_HANDLER_SETUP() \
59 write_c0_context((unsigned long) smp_processor_id() << 25); \
60 back_to_back_c0_hazard(); \
61 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
64 #define TLBMISS_HANDLER_SETUP() \
65 write_c0_context((unsigned long) smp_processor_id() << 26); \
66 back_to_back_c0_hazard(); \
67 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
69 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
71 #define ASID_INC(asid) \
73 unsigned long __asid = asid; \
74 __asm__("1:\taddiu\t%0,1\t\t\t\t# patched\n\t" \
75 ".section\t__asid_inc,\"a\"\n\t" \
82 #define ASID_MASK(asid) \
84 unsigned long __asid = asid; \
85 __asm__("1:\tandi\t%0,%1,0xfc0\t\t\t# patched\n\t" \
86 ".section\t__asid_mask,\"a\"\n\t" \
93 #define ASID_VERSION_MASK \
95 unsigned long __asid; \
96 __asm__("1:\taddiu\t%0,$0,0xff00\t\t\t\t# patched\n\t" \
97 ".section\t__asid_version_mask,\"a\"\n\t" \
103 #define ASID_FIRST_VERSION \
105 unsigned long __asid = asid; \
106 __asm__("1:\tli\t%0,0x100\t\t\t\t# patched\n\t" \
107 ".section\t__asid_first_version,\"a\"\n\t" \
114 #define ASID_FIRST_VERSION_R3000 0x1000
115 #define ASID_FIRST_VERSION_R4000 0x100
116 #define ASID_FIRST_VERSION_R8000 0x1000
117 #define ASID_FIRST_VERSION_RM9000 0x1000
119 #ifdef CONFIG_MIPS_MT_SMTC
120 #define SMTC_HW_ASID_MASK 0xff
121 extern unsigned int smtc_asid_mask;
124 #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
125 #define cpu_asid(cpu, mm) ASID_MASK(cpu_context((cpu), (mm)))
126 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
128 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
132 #ifndef CONFIG_MIPS_MT_SMTC
133 /* Normal, classic MIPS get_new_mmu_context */
135 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
137 extern void kvm_local_flush_tlb_all(void);
138 unsigned long asid = asid_cache(cpu);
140 if (!ASID_MASK((asid = ASID_INC(asid)))) {
141 if (cpu_has_vtag_icache)
143 #ifdef CONFIG_VIRTUALIZATION
144 kvm_local_flush_tlb_all(); /* start new asid cycle */
146 local_flush_tlb_all(); /* start new asid cycle */
148 if (!asid) /* fix version if needed */
149 asid = ASID_FIRST_VERSION;
152 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
155 #else /* CONFIG_MIPS_MT_SMTC */
157 #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
159 #endif /* CONFIG_MIPS_MT_SMTC */
162 * Initialize the context related info for a new mm_struct
166 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
170 for_each_possible_cpu(i)
171 cpu_context(i, mm) = 0;
176 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
177 struct task_struct *tsk)
179 unsigned int cpu = smp_processor_id();
181 #ifdef CONFIG_MIPS_MT_SMTC
182 unsigned long oldasid;
183 unsigned long mtflags;
184 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
185 local_irq_save(flags);
188 local_irq_save(flags);
189 #endif /* CONFIG_MIPS_MT_SMTC */
191 /* Check if our ASID is of an older version and thus invalid */
192 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
193 get_new_mmu_context(next, cpu);
194 #ifdef CONFIG_MIPS_MT_SMTC
196 * If the EntryHi ASID being replaced happens to be
197 * the value flagged at ASID recycling time as having
198 * an extended life, clear the bit showing it being
199 * in use by this "CPU", and if that's the last bit,
200 * free up the ASID value for use and flush any old
201 * instances of it from the TLB.
203 oldasid = ASID_MASK(read_c0_entryhi());
204 if(smtc_live_asid[mytlb][oldasid]) {
205 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
206 if(smtc_live_asid[mytlb][oldasid] == 0)
207 smtc_flush_tlb_asid(oldasid);
210 * Tread softly on EntryHi, and so long as we support
211 * having ASID_MASK smaller than the hardware maximum,
212 * make sure no "soft" bits become "hard"...
214 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
215 cpu_asid(cpu, next));
216 ehb(); /* Make sure it propagates to TCStatus */
219 write_c0_entryhi(cpu_asid(cpu, next));
220 #endif /* CONFIG_MIPS_MT_SMTC */
221 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
224 * Mark current->active_mm as not "active" anymore.
225 * We don't want to mislead possible IPI tlb flush routines.
227 cpumask_clear_cpu(cpu, mm_cpumask(prev));
228 cpumask_set_cpu(cpu, mm_cpumask(next));
230 local_irq_restore(flags);
234 * Destroy context related info for an mm_struct that is about
237 static inline void destroy_context(struct mm_struct *mm)
241 #define deactivate_mm(tsk, mm) do { } while (0)
244 * After we have set current->mm to a new value, this activates
245 * the context for the new mm so we see the new mappings.
248 activate_mm(struct mm_struct *prev, struct mm_struct *next)
251 unsigned int cpu = smp_processor_id();
253 #ifdef CONFIG_MIPS_MT_SMTC
254 unsigned long oldasid;
255 unsigned long mtflags;
256 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
257 #endif /* CONFIG_MIPS_MT_SMTC */
259 local_irq_save(flags);
261 /* Unconditionally get a new ASID. */
262 get_new_mmu_context(next, cpu);
264 #ifdef CONFIG_MIPS_MT_SMTC
265 /* See comments for similar code above */
267 oldasid = ASID_MASK(read_c0_entryhi());
268 if(smtc_live_asid[mytlb][oldasid]) {
269 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
270 if(smtc_live_asid[mytlb][oldasid] == 0)
271 smtc_flush_tlb_asid(oldasid);
273 /* See comments for similar code above */
274 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK) |
275 cpu_asid(cpu, next));
276 ehb(); /* Make sure it propagates to TCStatus */
279 write_c0_entryhi(cpu_asid(cpu, next));
280 #endif /* CONFIG_MIPS_MT_SMTC */
281 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
283 /* mark mmu ownership change */
284 cpumask_clear_cpu(cpu, mm_cpumask(prev));
285 cpumask_set_cpu(cpu, mm_cpumask(next));
287 local_irq_restore(flags);
291 * If mm is currently active_mm, we can't really drop it. Instead,
292 * we will get a new one for it.
295 drop_mmu_context(struct mm_struct *mm, unsigned cpu)
298 #ifdef CONFIG_MIPS_MT_SMTC
299 unsigned long oldasid;
300 /* Can't use spinlock because called from TLB flush within DVPE */
301 unsigned int prevvpe;
302 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
303 #endif /* CONFIG_MIPS_MT_SMTC */
305 local_irq_save(flags);
307 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
308 get_new_mmu_context(mm, cpu);
309 #ifdef CONFIG_MIPS_MT_SMTC
310 /* See comments for similar code above */
312 oldasid = ASID_MASK(read_c0_entryhi());
313 if (smtc_live_asid[mytlb][oldasid]) {
314 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
315 if(smtc_live_asid[mytlb][oldasid] == 0)
316 smtc_flush_tlb_asid(oldasid);
318 /* See comments for similar code above */
319 write_c0_entryhi((read_c0_entryhi() & ~SMTC_HW_ASID_MASK)
320 | cpu_asid(cpu, mm));
321 ehb(); /* Make sure it propagates to TCStatus */
323 #else /* not CONFIG_MIPS_MT_SMTC */
324 write_c0_entryhi(cpu_asid(cpu, mm));
325 #endif /* CONFIG_MIPS_MT_SMTC */
327 /* will get a new context next time */
328 #ifndef CONFIG_MIPS_MT_SMTC
329 cpu_context(cpu, mm) = 0;
333 /* SMTC shares the TLB (and ASIDs) across VPEs */
334 for_each_online_cpu(i) {
335 if((smtc_status & SMTC_TLB_SHARED)
336 || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
337 cpu_context(i, mm) = 0;
339 #endif /* CONFIG_MIPS_MT_SMTC */
341 local_irq_restore(flags);
344 #endif /* _ASM_MMU_CONTEXT_H */