OSDN Git Service

MIPS: traps: Add CPU PM callback for trap configuration
[uclinux-h8/linux.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/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>
23 #include <asm/smtc.h>
24 #endif /* SMTC */
25 #include <asm-generic/mm_hooks.h>
26
27 #define TLBMISS_HANDLER_SETUP_PGD(pgd)                                  \
28 do {                                                                    \
29         extern void tlbmiss_handler_setup_pgd(unsigned long);           \
30         tlbmiss_handler_setup_pgd((unsigned long)(pgd));                \
31 } while (0)
32
33 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
34
35 #define TLBMISS_HANDLER_RESTORE()                                       \
36         write_c0_xcontext((unsigned long) smp_processor_id() <<         \
37                           SMP_CPUID_REGSHIFT)
38
39 #define TLBMISS_HANDLER_SETUP()                                         \
40         do {                                                            \
41                 TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);              \
42                 TLBMISS_HANDLER_RESTORE();                              \
43         } while (0)
44
45 #else /* !CONFIG_MIPS_PGD_C0_CONTEXT: using  pgd_current*/
46
47 /*
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.
51  */
52 extern unsigned long pgd_current[];
53
54 #define TLBMISS_HANDLER_RESTORE()                                       \
55         write_c0_context((unsigned long) smp_processor_id() <<          \
56                          SMP_CPUID_REGSHIFT)
57
58 #define TLBMISS_HANDLER_SETUP()                                         \
59         TLBMISS_HANDLER_RESTORE();                                      \
60         back_to_back_c0_hazard();                                       \
61         TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
62 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT*/
63 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
64
65 #define ASID_INC        0x40
66 #define ASID_MASK       0xfc0
67
68 #elif defined(CONFIG_CPU_R8000)
69
70 #define ASID_INC        0x10
71 #define ASID_MASK       0xff0
72
73 #elif defined(CONFIG_MIPS_MT_SMTC)
74
75 #define ASID_INC        0x1
76 extern unsigned long smtc_asid_mask;
77 #define ASID_MASK       (smtc_asid_mask)
78 #define HW_ASID_MASK    0xff
79 /* End SMTC/34K debug hack */
80 #else /* FIXME: not correct for R6000 */
81
82 #define ASID_INC        0x1
83 #define ASID_MASK       0xff
84
85 #endif
86
87 #define cpu_context(cpu, mm)    ((mm)->context.asid[cpu])
88 #define cpu_asid(cpu, mm)       (cpu_context((cpu), (mm)) & ASID_MASK)
89 #define asid_cache(cpu)         (cpu_data[cpu].asid_cache)
90
91 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
92 {
93 }
94
95 /*
96  *  All unused by hardware upper bits will be considered
97  *  as a software asid extension.
98  */
99 #define ASID_VERSION_MASK  ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
100 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
101
102 #ifndef CONFIG_MIPS_MT_SMTC
103 /* Normal, classic MIPS get_new_mmu_context */
104 static inline void
105 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
106 {
107         extern void kvm_local_flush_tlb_all(void);
108         unsigned long asid = asid_cache(cpu);
109
110         if (! ((asid += ASID_INC) & ASID_MASK) ) {
111                 if (cpu_has_vtag_icache)
112                         flush_icache_all();
113 #ifdef CONFIG_KVM
114                 kvm_local_flush_tlb_all();      /* start new asid cycle */
115 #else
116                 local_flush_tlb_all();  /* start new asid cycle */
117 #endif
118                 if (!asid)              /* fix version if needed */
119                         asid = ASID_FIRST_VERSION;
120         }
121
122         cpu_context(cpu, mm) = asid_cache(cpu) = asid;
123 }
124
125 #else /* CONFIG_MIPS_MT_SMTC */
126
127 #define get_new_mmu_context(mm, cpu) smtc_get_new_mmu_context((mm), (cpu))
128
129 #endif /* CONFIG_MIPS_MT_SMTC */
130
131 /*
132  * Initialize the context related info for a new mm_struct
133  * instance.
134  */
135 static inline int
136 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
137 {
138         int i;
139
140         for_each_possible_cpu(i)
141                 cpu_context(i, mm) = 0;
142
143         return 0;
144 }
145
146 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
147                              struct task_struct *tsk)
148 {
149         unsigned int cpu = smp_processor_id();
150         unsigned long flags;
151 #ifdef CONFIG_MIPS_MT_SMTC
152         unsigned long oldasid;
153         unsigned long mtflags;
154         int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
155         local_irq_save(flags);
156         mtflags = dvpe();
157 #else /* Not SMTC */
158         local_irq_save(flags);
159 #endif /* CONFIG_MIPS_MT_SMTC */
160
161         /* Check if our ASID is of an older version and thus invalid */
162         if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
163                 get_new_mmu_context(next, cpu);
164 #ifdef CONFIG_MIPS_MT_SMTC
165         /*
166          * If the EntryHi ASID being replaced happens to be
167          * the value flagged at ASID recycling time as having
168          * an extended life, clear the bit showing it being
169          * in use by this "CPU", and if that's the last bit,
170          * free up the ASID value for use and flush any old
171          * instances of it from the TLB.
172          */
173         oldasid = (read_c0_entryhi() & ASID_MASK);
174         if(smtc_live_asid[mytlb][oldasid]) {
175                 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
176                 if(smtc_live_asid[mytlb][oldasid] == 0)
177                         smtc_flush_tlb_asid(oldasid);
178         }
179         /*
180          * Tread softly on EntryHi, and so long as we support
181          * having ASID_MASK smaller than the hardware maximum,
182          * make sure no "soft" bits become "hard"...
183          */
184         write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
185                          cpu_asid(cpu, next));
186         ehb(); /* Make sure it propagates to TCStatus */
187         evpe(mtflags);
188 #else
189         write_c0_entryhi(cpu_asid(cpu, next));
190 #endif /* CONFIG_MIPS_MT_SMTC */
191         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
192
193         /*
194          * Mark current->active_mm as not "active" anymore.
195          * We don't want to mislead possible IPI tlb flush routines.
196          */
197         cpumask_clear_cpu(cpu, mm_cpumask(prev));
198         cpumask_set_cpu(cpu, mm_cpumask(next));
199
200         local_irq_restore(flags);
201 }
202
203 /*
204  * Destroy context related info for an mm_struct that is about
205  * to be put to rest.
206  */
207 static inline void destroy_context(struct mm_struct *mm)
208 {
209 }
210
211 #define deactivate_mm(tsk, mm)  do { } while (0)
212
213 /*
214  * After we have set current->mm to a new value, this activates
215  * the context for the new mm so we see the new mappings.
216  */
217 static inline void
218 activate_mm(struct mm_struct *prev, struct mm_struct *next)
219 {
220         unsigned long flags;
221         unsigned int cpu = smp_processor_id();
222
223 #ifdef CONFIG_MIPS_MT_SMTC
224         unsigned long oldasid;
225         unsigned long mtflags;
226         int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
227 #endif /* CONFIG_MIPS_MT_SMTC */
228
229         local_irq_save(flags);
230
231         /* Unconditionally get a new ASID.  */
232         get_new_mmu_context(next, cpu);
233
234 #ifdef CONFIG_MIPS_MT_SMTC
235         /* See comments for similar code above */
236         mtflags = dvpe();
237         oldasid = read_c0_entryhi() & ASID_MASK;
238         if(smtc_live_asid[mytlb][oldasid]) {
239                 smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
240                 if(smtc_live_asid[mytlb][oldasid] == 0)
241                          smtc_flush_tlb_asid(oldasid);
242         }
243         /* See comments for similar code above */
244         write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK) |
245                          cpu_asid(cpu, next));
246         ehb(); /* Make sure it propagates to TCStatus */
247         evpe(mtflags);
248 #else
249         write_c0_entryhi(cpu_asid(cpu, next));
250 #endif /* CONFIG_MIPS_MT_SMTC */
251         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
252
253         /* mark mmu ownership change */
254         cpumask_clear_cpu(cpu, mm_cpumask(prev));
255         cpumask_set_cpu(cpu, mm_cpumask(next));
256
257         local_irq_restore(flags);
258 }
259
260 /*
261  * If mm is currently active_mm, we can't really drop it.  Instead,
262  * we will get a new one for it.
263  */
264 static inline void
265 drop_mmu_context(struct mm_struct *mm, unsigned cpu)
266 {
267         unsigned long flags;
268 #ifdef CONFIG_MIPS_MT_SMTC
269         unsigned long oldasid;
270         /* Can't use spinlock because called from TLB flush within DVPE */
271         unsigned int prevvpe;
272         int mytlb = (smtc_status & SMTC_TLB_SHARED) ? 0 : cpu_data[cpu].vpe_id;
273 #endif /* CONFIG_MIPS_MT_SMTC */
274
275         local_irq_save(flags);
276
277         if (cpumask_test_cpu(cpu, mm_cpumask(mm)))  {
278                 get_new_mmu_context(mm, cpu);
279 #ifdef CONFIG_MIPS_MT_SMTC
280                 /* See comments for similar code above */
281                 prevvpe = dvpe();
282                 oldasid = (read_c0_entryhi() & ASID_MASK);
283                 if (smtc_live_asid[mytlb][oldasid]) {
284                         smtc_live_asid[mytlb][oldasid] &= ~(0x1 << cpu);
285                         if(smtc_live_asid[mytlb][oldasid] == 0)
286                                 smtc_flush_tlb_asid(oldasid);
287                 }
288                 /* See comments for similar code above */
289                 write_c0_entryhi((read_c0_entryhi() & ~HW_ASID_MASK)
290                                 | cpu_asid(cpu, mm));
291                 ehb(); /* Make sure it propagates to TCStatus */
292                 evpe(prevvpe);
293 #else /* not CONFIG_MIPS_MT_SMTC */
294                 write_c0_entryhi(cpu_asid(cpu, mm));
295 #endif /* CONFIG_MIPS_MT_SMTC */
296         } else {
297                 /* will get a new context next time */
298 #ifndef CONFIG_MIPS_MT_SMTC
299                 cpu_context(cpu, mm) = 0;
300 #else /* SMTC */
301                 int i;
302
303                 /* SMTC shares the TLB (and ASIDs) across VPEs */
304                 for_each_online_cpu(i) {
305                     if((smtc_status & SMTC_TLB_SHARED)
306                     || (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
307                         cpu_context(i, mm) = 0;
308                 }
309 #endif /* CONFIG_MIPS_MT_SMTC */
310         }
311         local_irq_restore(flags);
312 }
313
314 #endif /* _ASM_MMU_CONTEXT_H */