cleanup
[linux-2.4.21-pre4.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/slab.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
18
19 /*
20  * For the fast tlb miss handlers, we currently keep a per cpu array
21  * of pointers to the current pgd for each processor. Also, the proc.
22  * id is stuffed into the context register. This should be changed to
23  * use the processor id via current->processor, where current is stored
24  * in watchhi/lo. The context register should be used to contiguously
25  * map the page tables.
26  */
27 #define TLBMISS_HANDLER_SETUP_PGD(pgd) \
28         pgd_current[smp_processor_id()] = (unsigned long)(pgd)
29 #define TLBMISS_HANDLER_SETUP() \
30         set_context((unsigned long) smp_processor_id() << (23 + 3)); \
31         TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir)
32 extern unsigned long pgd_current[];
33
34 #ifndef CONFIG_SMP
35 #define CPU_CONTEXT(cpu, mm)    (mm)->context
36 #else
37 #define CPU_CONTEXT(cpu, mm)    (*((unsigned long *)((mm)->context) + cpu))
38 #endif
39 #define ASID_CACHE(cpu)         cpu_data[cpu].asid_cache
40
41 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
42
43 #define ASID_INC        0x40
44 #define ASID_MASK       0xfc0
45
46 #else /* FIXME: not correct for R6000, R8000 */
47
48 #define ASID_INC        0x1
49 #define ASID_MASK       0xff
50
51 #endif
52
53 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk, unsigned cpu)
54 {
55 }
56
57 /*
58  *  All unused by hardware upper bits will be considered
59  *  as a software asid extension.
60  */
61 #define ASID_VERSION_MASK  ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
62 #define ASID_FIRST_VERSION ((unsigned long)(~ASID_VERSION_MASK) + 1)
63
64 static inline void
65 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
66 {
67         unsigned long asid = ASID_CACHE(cpu);
68
69         if (! ((asid += ASID_INC) & ASID_MASK) ) {
70                 flush_icache_all();
71                 local_flush_tlb_all();  /* start new asid cycle */
72                 if (!asid)              /* fix version if needed */
73                         asid = ASID_FIRST_VERSION;
74         }
75         CPU_CONTEXT(cpu, mm) = ASID_CACHE(cpu) = asid;
76 }
77
78 /*
79  * Initialize the context related info for a new mm_struct
80  * instance.
81  */
82 static inline int
83 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
84 {
85 #ifndef CONFIG_SMP
86         mm->context = 0;
87 #else
88         mm->context = (unsigned long)kmalloc(smp_num_cpus *
89                                 sizeof(unsigned long), GFP_KERNEL);
90         /*
91          * Init the "context" values so that a tlbpid allocation
92          * happens on the first switch.
93          */
94         if (mm->context == 0)
95                 return -ENOMEM;
96         memset((void *)mm->context, 0, smp_num_cpus * sizeof(unsigned long));
97 #endif
98         return 0;
99 }
100
101 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
102                              struct task_struct *tsk, unsigned cpu)
103 {
104         /* Check if our ASID is of an older version and thus invalid */
105         if ((CPU_CONTEXT(cpu, next) ^ ASID_CACHE(cpu)) & ASID_VERSION_MASK)
106                 get_new_mmu_context(next, cpu);
107
108         set_entryhi(CPU_CONTEXT(cpu, next));
109         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
110 }
111
112 /*
113  * Destroy context related info for an mm_struct that is about
114  * to be put to rest.
115  */
116 static inline void destroy_context(struct mm_struct *mm)
117 {
118 #ifdef CONFIG_SMP
119         if (mm->context)
120                 kfree((void *)mm->context);
121 #endif
122 }
123
124 /*
125  * After we have set current->mm to a new value, this activates
126  * the context for the new mm so we see the new mappings.
127  */
128 static inline void
129 activate_mm(struct mm_struct *prev, struct mm_struct *next)
130 {
131         /* Unconditionally get a new ASID.  */
132         get_new_mmu_context(next, smp_processor_id());
133
134         set_entryhi(CPU_CONTEXT(smp_processor_id(), next));
135         TLBMISS_HANDLER_SETUP_PGD(next->pgd);
136 }
137
138 #endif /* _ASM_MMU_CONTEXT_H */