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 #include <asm-generic/mm_hooks.h>
23 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
25 extern void tlbmiss_handler_setup_pgd(unsigned long); \
26 tlbmiss_handler_setup_pgd((unsigned long)(pgd)); \
29 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
31 #define TLBMISS_HANDLER_RESTORE() \
32 write_c0_xcontext((unsigned long) smp_processor_id() << \
35 #define TLBMISS_HANDLER_SETUP() \
37 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir); \
38 TLBMISS_HANDLER_RESTORE(); \
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_RESTORE() \
51 write_c0_context((unsigned long) smp_processor_id() << \
54 #define TLBMISS_HANDLER_SETUP() \
55 TLBMISS_HANDLER_RESTORE(); \
56 back_to_back_c0_hazard(); \
57 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
58 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
59 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
62 #define ASID_MASK 0xfc0
64 #elif defined(CONFIG_CPU_R8000)
67 #define ASID_MASK 0xff0
69 #else /* FIXME: not correct for R6000 */
72 #define ASID_MASK 0xff
76 #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
77 #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & ASID_MASK)
78 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
80 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
85 * All unused by hardware upper bits will be considered
86 * as a software asid extension.
88 #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
89 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
91 /* Normal, classic MIPS get_new_mmu_context */
93 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
95 extern void kvm_local_flush_tlb_all(void);
96 unsigned long asid = asid_cache(cpu);
98 if (! ((asid += ASID_INC) & ASID_MASK) ) {
99 if (cpu_has_vtag_icache)
102 kvm_local_flush_tlb_all(); /* start new asid cycle */
104 local_flush_tlb_all(); /* start new asid cycle */
106 if (!asid) /* fix version if needed */
107 asid = ASID_FIRST_VERSION;
110 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
114 * Initialize the context related info for a new mm_struct
118 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
122 for_each_possible_cpu(i)
123 cpu_context(i, mm) = 0;
128 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
129 struct task_struct *tsk)
131 unsigned int cpu = smp_processor_id();
133 local_irq_save(flags);
135 /* Check if our ASID is of an older version and thus invalid */
136 if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
137 get_new_mmu_context(next, cpu);
138 write_c0_entryhi(cpu_asid(cpu, next));
139 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
142 * Mark current->active_mm as not "active" anymore.
143 * We don't want to mislead possible IPI tlb flush routines.
145 cpumask_clear_cpu(cpu, mm_cpumask(prev));
146 cpumask_set_cpu(cpu, mm_cpumask(next));
148 local_irq_restore(flags);
152 * Destroy context related info for an mm_struct that is about
155 static inline void destroy_context(struct mm_struct *mm)
159 #define deactivate_mm(tsk, mm) do { } while (0)
162 * After we have set current->mm to a new value, this activates
163 * the context for the new mm so we see the new mappings.
166 activate_mm(struct mm_struct *prev, struct mm_struct *next)
169 unsigned int cpu = smp_processor_id();
171 local_irq_save(flags);
173 /* Unconditionally get a new ASID. */
174 get_new_mmu_context(next, cpu);
176 write_c0_entryhi(cpu_asid(cpu, next));
177 TLBMISS_HANDLER_SETUP_PGD(next->pgd);
179 /* mark mmu ownership change */
180 cpumask_clear_cpu(cpu, mm_cpumask(prev));
181 cpumask_set_cpu(cpu, mm_cpumask(next));
183 local_irq_restore(flags);
187 * If mm is currently active_mm, we can't really drop it. Instead,
188 * we will get a new one for it.
191 drop_mmu_context(struct mm_struct *mm, unsigned cpu)
195 local_irq_save(flags);
197 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
198 get_new_mmu_context(mm, cpu);
199 write_c0_entryhi(cpu_asid(cpu, mm));
201 /* will get a new context next time */
202 cpu_context(cpu, mm) = 0;
204 local_irq_restore(flags);
207 #endif /* _ASM_MMU_CONTEXT_H */