ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-mips / 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/config.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <asm/cacheflush.h>
19 #include <asm/tlbflush.h>
20
21 /*
22  * For the fast tlb miss handlers, we currently keep a per cpu array
23  * of pointers to the current pgd for each processor. Also, the proc.
24  * id is stuffed into the context register. This should be changed to
25  * use the processor id via current->processor, where current is stored
26  * in watchhi/lo. The context register should be used to contiguously
27  * map the page tables.
28  */
29 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
30         pgd_current[smp_processor_id()] = (unsigned long)(pgd)
31 #ifdef CONFIG_MIPS32
32 #define TLBMISS_HANDLER_SETUP() \
33         write_c0_context((unsigned long) smp_processor_id() << 23); \
34         TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
35 #endif
36 #ifdef CONFIG_MIPS64
37 #define TLBMISS_HANDLER_SETUP() \
38         write_c0_context((unsigned long) &pgd_current[smp_processor_id()] << 23); \
39         TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
40 #endif
41 extern unsigned long pgd_current[];
42
43 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
44
45 #define ASID_INC        0x40
46 #define ASID_MASK       0xfc0
47
48 #elif defined(CONFIG_CPU_RM9000)
49
50 #define ASID_INC        0x1
51 #define ASID_MASK       0xfff
52
53 #else /* FIXME: not correct for R6000, R8000 */
54
55 #define ASID_INC        0x1
56 #define ASID_MASK       0xff
57
58 #endif
59
60 #define cpu_context(cpu, mm)    ((mm)->context[cpu])
61 #define cpu_asid(cpu, mm)       (cpu_context((cpu), (mm)) & ASID_MASK)
62 #define asid_cache(cpu)         (cpu_data[cpu].asid_cache)
63
64 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
65 {
66 }
67
68 /*
69  *  All unused by hardware upper bits will be considered
70  *  as a software asid extension.
71  */
72 #define ASID_VERSION_MASK  ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
73 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
74
75 static inline void
76 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
77 {
78         unsigned long asid = asid_cache(cpu);
79
80         if (! ((asid += ASID_INC) & ASID_MASK) ) {
81 #ifdef CONFIG_VTAG_ICACHE
82                 flush_icache_all();
83 #endif
84                 local_flush_tlb_all();  /* start new asid cycle */
85                 if (!asid)              /* fix version if needed */
86                         asid = ASID_FIRST_VERSION;
87         }
88         cpu_context(cpu, mm) = asid_cache(cpu) = asid;
89 }
90
91 /*
92  * Initialize the context related info for a new mm_struct
93  * instance.
94  */
95 static inline int
96 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
97 {
98         int i;
99
100         for (i = 0; i < num_online_cpus(); i++)
101                 cpu_context(i, mm) = 0;
102
103         return 0;
104 }
105
106 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
107                              struct task_struct *tsk)
108 {
109         unsigned int cpu = smp_processor_id();
110         unsigned long flags;
111
112         local_irq_save(flags);
113
114         /* Check if our ASID is of an older version and thus invalid */
115         if ((cpu_context(cpu, next) ^ asid_cache(cpu)) & ASID_VERSION_MASK)
116                 get_new_mmu_context(next, cpu);
117
118         write_c0_entryhi(cpu_context(cpu, next));
119         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
120
121         /*
122          * Mark current->active_mm as not "active" anymore.
123          * We don't want to mislead possible IPI tlb flush routines.
124          */
125         clear_bit(cpu, &prev->cpu_vm_mask);
126         set_bit(cpu, &next->cpu_vm_mask);
127
128         local_irq_restore(flags);
129 }
130
131 /*
132  * Destroy context related info for an mm_struct that is about
133  * to be put to rest.
134  */
135 static inline void destroy_context(struct mm_struct *mm)
136 {
137 }
138
139 #define deactivate_mm(tsk,mm)   do { } while (0)
140
141 /*
142  * After we have set current->mm to a new value, this activates
143  * the context for the new mm so we see the new mappings.
144  */
145 static inline void
146 activate_mm(struct mm_struct *prev, struct mm_struct *next)
147 {
148         unsigned long flags;
149         int cpu = smp_processor_id();
150
151         local_irq_save(flags);
152
153         /* Unconditionally get a new ASID.  */
154         get_new_mmu_context(next, cpu);
155
156         write_c0_entryhi(cpu_context(cpu, next));
157         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
158
159         /* mark mmu ownership change */
160         clear_bit(cpu, &prev->cpu_vm_mask);
161         set_bit(cpu, &next->cpu_vm_mask);
162
163         local_irq_restore(flags);
164 }
165
166 /*
167  * If mm is currently active_mm, we can't really drop it.  Instead,
168  * we will get a new one for it.
169  */
170 static inline void
171 drop_mmu_context(struct mm_struct *mm, unsigned cpu)
172 {
173         unsigned long flags;
174
175         local_irq_save(flags);
176
177         if (test_bit(cpu, &mm->cpu_vm_mask))  {
178                 get_new_mmu_context(mm, cpu);
179                 write_c0_entryhi(cpu_asid(cpu, mm));
180         } else {
181                 /* will get a new context next time */
182                 cpu_context(cpu, mm) = 0;
183         }
184
185         local_irq_restore(flags);
186 }
187
188 #endif /* _ASM_MMU_CONTEXT_H */