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 extern void tlbmiss_handler_setup_pgd(unsigned long); \
32 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
35 #define TLBMISS_HANDLER_SETUP() \
37 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
38 write_c0_xcontext((unsigned long) smp_processor_id() << 51); \
41 #else /* CONFIG_MIPS_PGD_C0_CONTEXT: using pgd_current*/
44 * For the fast tlb miss handlers, we keep a per cpu array of pointers
45 * to the current pgd for each processor. Also, the proc. id is stuffed
46 * into the context register.
48 extern unsigned long pgd_current[];
50 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
51 pgd_current[smp_processor_id()] = (unsigned long)(pgd)
54 #define TLBMISS_HANDLER_SETUP() \
55 write_c0_context((unsigned long) smp_processor_id() << 25); \
56 back_to_back_c0_hazard(); \
57 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
60 #define TLBMISS_HANDLER_SETUP() \
61 write_c0_context((unsigned long) smp_processor_id() << 26); \
62 back_to_back_c0_hazard(); \
63 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
65 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
66 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
69 #define ASID_MASK 0xfc0
71 #elif defined(CONFIG_CPU_R8000)
74 #define ASID_MASK 0xff0
76 #elif defined(CONFIG_MIPS_MT_SMTC)
79 extern unsigned long smtc_asid_mask;
80 #define ASID_MASK (smtc_asid_mask)
81 #define HW_ASID_MASK 0xff
82 /* End SMTC/34K debug hack */
83 #else /* FIXME: not correct for R6000 */
86 #define ASID_MASK 0xff
90 #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
91 #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
92 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
94 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
99 * All unused by hardware upper bits will be considered
100 * as a software asid extension.
102 #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
103 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
105 #ifndef CONFIG_MIPS_MT_SMTC
106 /* Normal, classic MIPS get_new_mmu_context */
108 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
110 extern void kvm_local_flush_tlb_all(void);
111 unsigned long asid = asid_cache(cpu);
113 if (! ((asid += ASID_INC) & ASID_MASK) ) {
114 if (cpu_has_vtag_icache)
117 kvm_local_flush_tlb_all(); /* start new asid cycle */
119 local_flush_tlb_all(); /* start new asid cycle */
121 if (!asid) /* fix version if needed */
122 asid = ASID_FIRST_VERSION;
125 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
128 #else /* CONFIG_MIPS_MT_SMTC */
130 #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
132 #endif /* CONFIG_MIPS_MT_SMTC */
135 * Initialize the context related info for a new mm_struct
139 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
143 for_each_possible_cpu(i)
144 cpu_context(i, mm) = 0;
149 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
150 struct task_struct *tsk)
152 unsigned int cpu = smp_processor_id();
154 #ifdef CONFIG_MIPS_MT_SMTC
155 unsigned long oldasid;
156 unsigned long mtflags;
157 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
158 local_irq_save(flags);
161 local_irq_save(flags);
162 #endif /* CONFIG_MIPS_MT_SMTC */
164 /* Check if our ASID is of an older version and thus invalid */
165 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
166 get_new_mmu_context(next, cpu);
167 #ifdef CONFIG_MIPS_MT_SMTC
169 * If the EntryHi ASID being replaced happens to be
170 * the value flagged at ASID recycling time as having
171 * an extended life, clear the bit showing it being
172 * in use by this "CPU", and if that's the last bit,
173 * free up the ASID value for use and flush any old
174 * instances of it from the TLB.
176 oldasid = (read_c0_entryhi() & ASID_MASK);
177 if(smtc_live_asid[mytlb][oldasid]) {
178 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
179 if(smtc_live_asid[mytlb][oldasid] == 0)
180 smtc_flush_tlb_asid(oldasid);
183 * Tread softly on EntryHi, and so long as we support
184 * having ASID_MASK smaller than the hardware maximum,
185 * make sure no "soft" bits become "hard"...
187 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
188 cpu_asid(cpu, next));
189 ehb(); /* Make sure it propagates to TCStatus */
192 write_c0_entryhi(cpu_asid(cpu, next));
193 #endif /* CONFIG_MIPS_MT_SMTC */
194 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
197 * Mark current->active_mm as not "active" anymore.
198 * We don't want to mislead possible IPI tlb flush routines.
200 cpumask_clear_cpu(cpu, mm_cpumask(prev));
201 cpumask_set_cpu(cpu, mm_cpumask(next));
203 local_irq_restore(flags);
207 * Destroy context related info for an mm_struct that is about
210 static inline void destroy_context(struct mm_struct *mm)
214 #define deactivate_mm(tsk, mm) do { } while (0)
217 * After we have set current->mm to a new value, this activates
218 * the context for the new mm so we see the new mappings.
221 activate_mm(struct mm_struct *prev, struct mm_struct *next)
224 unsigned int cpu = smp_processor_id();
226 #ifdef CONFIG_MIPS_MT_SMTC
227 unsigned long oldasid;
228 unsigned long mtflags;
229 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
230 #endif /* CONFIG_MIPS_MT_SMTC */
232 local_irq_save(flags);
234 /* Unconditionally get a new ASID. */
235 get_new_mmu_context(next, cpu);
237 #ifdef CONFIG_MIPS_MT_SMTC
238 /* See comments for similar code above */
240 oldasid = read_c0_entryhi() & ASID_MASK;
241 if(smtc_live_asid[mytlb][oldasid]) {
242 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
243 if(smtc_live_asid[mytlb][oldasid] == 0)
244 smtc_flush_tlb_asid(oldasid);
246 /* See comments for similar code above */
247 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
248 cpu_asid(cpu, next));
249 ehb(); /* Make sure it propagates to TCStatus */
252 write_c0_entryhi(cpu_asid(cpu, next));
253 #endif /* CONFIG_MIPS_MT_SMTC */
254 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
256 /* mark mmu ownership change */
257 cpumask_clear_cpu(cpu, mm_cpumask(prev));
258 cpumask_set_cpu(cpu, mm_cpumask(next));
260 local_irq_restore(flags);
264 * If mm is currently active_mm, we can't really drop it. Instead,
265 * we will get a new one for it.
268 drop_mmu_context(struct mm_struct *mm, unsigned cpu)
271 #ifdef CONFIG_MIPS_MT_SMTC
272 unsigned long oldasid;
273 /* Can't use spinlock because called from TLB flush within DVPE */
274 unsigned int prevvpe;
275 int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
276 #endif /* CONFIG_MIPS_MT_SMTC */
278 local_irq_save(flags);
280 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
281 get_new_mmu_context(mm, cpu);
282 #ifdef CONFIG_MIPS_MT_SMTC
283 /* See comments for similar code above */
285 oldasid = (read_c0_entryhi() & ASID_MASK);
286 if (smtc_live_asid[mytlb][oldasid]) {
287 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
288 if(smtc_live_asid[mytlb][oldasid] == 0)
289 smtc_flush_tlb_asid(oldasid);
291 /* See comments for similar code above */
292 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK)
293 | cpu_asid(cpu, mm));
294 ehb(); /* Make sure it propagates to TCStatus */
296 #else /* not CONFIG_MIPS_MT_SMTC */
297 write_c0_entryhi(cpu_asid(cpu, mm));
298 #endif /* CONFIG_MIPS_MT_SMTC */
300 /* will get a new context next time */
301 #ifndef CONFIG_MIPS_MT_SMTC
302 cpu_context(cpu, mm) = 0;
306 /* SMTC shares the TLB (and ASIDs) across VPEs */
307 for_each_online_cpu(i) {
308 if((smtc_status & SMTC_TLB_SHARED)
309 || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
310 cpu_context(i, mm) = 0;
312 #endif /* CONFIG_MIPS_MT_SMTC */
314 local_irq_restore(flags);
317 #endif /* _ASM_MMU_CONTEXT_H */