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*/
70 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
73 #define ASID_MASK 0xfc0
75 #elif defined(CONFIG_CPU_R8000)
78 #define ASID_MASK 0xff0
80 #elif defined(CONFIG_MIPS_MT_SMTC)
83 extern unsigned long smtc_asid_mask
;
84 #define ASID_MASK (smtc_asid_mask)
85 #define HW_ASID_MASK 0xff
86 /* End SMTC/34K debug hack */
87 #else /* FIXME: not correct for R6000 */
90 #define ASID_MASK 0xff
94 #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
95 #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
96 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
98 static inline void enter_lazy_tlb(struct mm_struct
*mm
, struct task_struct
*tsk
)
103 * All unused by hardware upper bits will be considered
104 * as a software asid extension.
106 #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
107 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
109 #ifndef CONFIG_MIPS_MT_SMTC
110 /* Normal, classic MIPS get_new_mmu_context */
112 get_new_mmu_context(struct mm_struct
*mm
, unsigned long cpu
)
114 unsigned long asid
= asid_cache(cpu
);
116 if (! ((asid
+= ASID_INC
) & ASID_MASK
) ) {
117 if (cpu_has_vtag_icache
)
119 local_flush_tlb_all(); /* start new asid cycle */
120 if (!asid
) /* fix version if needed */
121 asid
= ASID_FIRST_VERSION
;
123 cpu_context(cpu
, mm
) = asid_cache(cpu
) = asid
;
126 #else /* CONFIG_MIPS_MT_SMTC */
128 #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
130 #endif /* CONFIG_MIPS_MT_SMTC */
133 * Initialize the context related info for a new mm_struct
137 init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
141 for_each_possible_cpu(i
)
142 cpu_context(i
, mm
) = 0;
147 static inline void switch_mm(struct mm_struct
*prev
, struct mm_struct
*next
,
148 struct task_struct
*tsk
)
150 unsigned int cpu
= smp_processor_id();
152 #ifdef CONFIG_MIPS_MT_SMTC
153 unsigned long oldasid
;
154 unsigned long mtflags
;
155 int mytlb
= (smtc_status
& SMTC_TLB_SHARED
) ? 0 : cpu_data
[cpu
].vpe_id
;
156 local_irq_save(flags
);
159 local_irq_save(flags
);
160 #endif /* CONFIG_MIPS_MT_SMTC */
162 /* Check if our ASID is of an older version and thus invalid */
163 if ((cpu_context(cpu
, next
) ^ asid_cache(cpu
)) & ASID_VERSION_MASK
)
164 get_new_mmu_context(next
, cpu
);
165 #ifdef CONFIG_MIPS_MT_SMTC
167 * If the EntryHi ASID being replaced happens to be
168 * the value flagged at ASID recycling time as having
169 * an extended life, clear the bit showing it being
170 * in use by this "CPU", and if that's the last bit,
171 * free up the ASID value for use and flush any old
172 * instances of it from the TLB.
174 oldasid
= (read_c0_entryhi() & ASID_MASK
);
175 if(smtc_live_asid
[mytlb
][oldasid
]) {
176 smtc_live_asid
[mytlb
][oldasid
] &= ~(0x1 << cpu
);
177 if(smtc_live_asid
[mytlb
][oldasid
] == 0)
178 smtc_flush_tlb_asid(oldasid
);
181 * Tread softly on EntryHi, and so long as we support
182 * having ASID_MASK smaller than the hardware maximum,
183 * make sure no "soft" bits become "hard"...
185 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK
) |
186 cpu_asid(cpu
, next
));
187 ehb(); /* Make sure it propagates to TCStatus */
190 write_c0_entryhi(cpu_asid(cpu
, next
));
191 #endif /* CONFIG_MIPS_MT_SMTC */
192 TLBMISS_HANDLER_SETUP_PGD(next
->pgd
);
195 * Mark current->active_mm as not "active" anymore.
196 * We don't want to mislead possible IPI tlb flush routines.
198 cpumask_clear_cpu(cpu
, mm_cpumask(prev
));
199 cpumask_set_cpu(cpu
, mm_cpumask(next
));
201 local_irq_restore(flags
);
205 * Destroy context related info for an mm_struct that is about
208 static inline void destroy_context(struct mm_struct
*mm
)
212 #define deactivate_mm(tsk, mm) do { } while (0)
215 * After we have set current->mm to a new value, this activates
216 * the context for the new mm so we see the new mappings.
219 activate_mm(struct mm_struct
*prev
, struct mm_struct
*next
)
222 unsigned int cpu
= smp_processor_id();
224 #ifdef CONFIG_MIPS_MT_SMTC
225 unsigned long oldasid
;
226 unsigned long mtflags
;
227 int mytlb
= (smtc_status
& SMTC_TLB_SHARED
) ? 0 : cpu_data
[cpu
].vpe_id
;
228 #endif /* CONFIG_MIPS_MT_SMTC */
230 local_irq_save(flags
);
232 /* Unconditionally get a new ASID. */
233 get_new_mmu_context(next
, cpu
);
235 #ifdef CONFIG_MIPS_MT_SMTC
236 /* See comments for similar code above */
238 oldasid
= read_c0_entryhi() & ASID_MASK
;
239 if(smtc_live_asid
[mytlb
][oldasid
]) {
240 smtc_live_asid
[mytlb
][oldasid
] &= ~(0x1 << cpu
);
241 if(smtc_live_asid
[mytlb
][oldasid
] == 0)
242 smtc_flush_tlb_asid(oldasid
);
244 /* See comments for similar code above */
245 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK
) |
246 cpu_asid(cpu
, next
));
247 ehb(); /* Make sure it propagates to TCStatus */
250 write_c0_entryhi(cpu_asid(cpu
, next
));
251 #endif /* CONFIG_MIPS_MT_SMTC */
252 TLBMISS_HANDLER_SETUP_PGD(next
->pgd
);
254 /* mark mmu ownership change */
255 cpumask_clear_cpu(cpu
, mm_cpumask(prev
));
256 cpumask_set_cpu(cpu
, mm_cpumask(next
));
258 local_irq_restore(flags
);
262 * If mm is currently active_mm, we can't really drop it. Instead,
263 * we will get a new one for it.
266 drop_mmu_context(struct mm_struct
*mm
, unsigned cpu
)
269 #ifdef CONFIG_MIPS_MT_SMTC
270 unsigned long oldasid
;
271 /* Can't use spinlock because called from TLB flush within DVPE */
272 unsigned int prevvpe
;
273 int mytlb
= (smtc_status
& SMTC_TLB_SHARED
) ? 0 : cpu_data
[cpu
].vpe_id
;
274 #endif /* CONFIG_MIPS_MT_SMTC */
276 local_irq_save(flags
);
278 if (cpumask_test_cpu(cpu
, mm_cpumask(mm
))) {
279 get_new_mmu_context(mm
, cpu
);
280 #ifdef CONFIG_MIPS_MT_SMTC
281 /* See comments for similar code above */
283 oldasid
= (read_c0_entryhi() & ASID_MASK
);
284 if (smtc_live_asid
[mytlb
][oldasid
]) {
285 smtc_live_asid
[mytlb
][oldasid
] &= ~(0x1 << cpu
);
286 if(smtc_live_asid
[mytlb
][oldasid
] == 0)
287 smtc_flush_tlb_asid(oldasid
);
289 /* See comments for similar code above */
290 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK
)
291 | cpu_asid(cpu
, mm
));
292 ehb(); /* Make sure it propagates to TCStatus */
294 #else /* not CONFIG_MIPS_MT_SMTC */
295 write_c0_entryhi(cpu_asid(cpu
, mm
));
296 #endif /* CONFIG_MIPS_MT_SMTC */
298 /* will get a new context next time */
299 #ifndef CONFIG_MIPS_MT_SMTC
300 cpu_context(cpu
, mm
) = 0;
304 /* SMTC shares the TLB (and ASIDs) across VPEs */
305 for_each_online_cpu(i
) {
306 if((smtc_status
& SMTC_TLB_SHARED
)
307 || (cpu_data
[i
].vpe_id
== cpu_data
[cpu
].vpe_id
))
308 cpu_context(i
, mm
) = 0;
310 #endif /* CONFIG_MIPS_MT_SMTC */
312 local_irq_restore(flags
);
315 #endif /* _ASM_MMU_CONTEXT_H */