ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-ppc / mmu_context.h
1 #ifdef __KERNEL__
2 #ifndef __PPC_MMU_CONTEXT_H
3 #define __PPC_MMU_CONTEXT_H
4
5 #include <linux/config.h>
6 #include <asm/atomic.h>
7 #include <asm/bitops.h>
8 #include <asm/mmu.h>
9 #include <asm/cputable.h>
10
11 /*
12  * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
13  * (virtual segment identifiers) for each context.  Although the
14  * hardware supports 24-bit VSIDs, and thus >1 million contexts,
15  * we only use 32,768 of them.  That is ample, since there can be
16  * at most around 30,000 tasks in the system anyway, and it means
17  * that we can use a bitmap to indicate which contexts are in use.
18  * Using a bitmap means that we entirely avoid all of the problems
19  * that we used to have when the context number overflowed,
20  * particularly on SMP systems.
21  *  -- paulus.
22  */
23
24 /*
25  * This function defines the mapping from contexts to VSIDs (virtual
26  * segment IDs).  We use a skew on both the context and the high 4 bits
27  * of the 32-bit virtual address (the "effective segment ID") in order
28  * to spread out the entries in the MMU hash table.  Note, if this
29  * function is changed then arch/ppc/mm/hashtable.S will have to be
30  * changed to correspond.
31  */
32 #define CTX_TO_VSID(ctx, va)    (((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
33                                  & 0xffffff)
34
35 /*
36    The MPC8xx has only 16 contexts.  We rotate through them on each
37    task switch.  A better way would be to keep track of tasks that
38    own contexts, and implement an LRU usage.  That way very active
39    tasks don't always have to pay the TLB reload overhead.  The
40    kernel pages are mapped shared, so the kernel can run on behalf
41    of any task that makes a kernel entry.  Shared does not mean they
42    are not protected, just that the ASID comparison is not performed.
43         -- Dan
44
45    The IBM4xx has 256 contexts, so we can just rotate through these
46    as a way of "switching" contexts.  If the TID of the TLB is zero,
47    the PID/TID comparison is disabled, so we can use a TID of zero
48    to represent all kernel pages as shared among all contexts.
49         -- Dan
50  */
51
52 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
53 {
54 }
55
56 #ifdef CONFIG_8xx
57 #define NO_CONTEXT              16
58 #define LAST_CONTEXT            15
59 #define FIRST_CONTEXT           0
60
61 #elif defined(CONFIG_4xx)
62 #define NO_CONTEXT              256
63 #define LAST_CONTEXT            255
64 #define FIRST_CONTEXT           1
65
66 #else
67
68 /* PPC 6xx, 7xx CPUs */
69 #define NO_CONTEXT              ((mm_context_t) -1)
70 #define LAST_CONTEXT            32767
71 #define FIRST_CONTEXT           1
72 #endif
73
74 /*
75  * Set the current MMU context.
76  * On 32-bit PowerPCs (other than the 8xx embedded chips), this is done by
77  * loading up the segment registers for the user part of the address space.
78  *
79  * Since the PGD is immediately available, it is much faster to simply
80  * pass this along as a second parameter, which is required for 8xx and
81  * can be used for debugging on all processors (if you happen to have
82  * an Abatron).
83  */
84 extern void set_context(mm_context_t context, pgd_t *pgd);
85
86 /*
87  * Bitmap of contexts in use.
88  * The size of this bitmap is LAST_CONTEXT + 1 bits.
89  */
90 extern unsigned long context_map[];
91
92 /*
93  * This caches the next context number that we expect to be free.
94  * Its use is an optimization only, we can't rely on this context
95  * number to be free, but it usually will be.
96  */
97 extern mm_context_t next_mmu_context;
98
99 /*
100  * If we don't have sufficient contexts to give one to every task
101  * that could be in the system, we need to be able to steal contexts.
102  * These variables support that.
103  */
104 #if LAST_CONTEXT < 30000
105 #define FEW_CONTEXTS    1
106 extern atomic_t nr_free_contexts;
107 extern struct mm_struct *context_mm[LAST_CONTEXT+1];
108 extern void steal_context(void);
109 #endif
110
111 /*
112  * Get a new mmu context for the address space described by `mm'.
113  */
114 static inline void get_mmu_context(struct mm_struct *mm)
115 {
116         mm_context_t ctx;
117
118         if (mm->context != NO_CONTEXT)
119                 return;
120 #ifdef FEW_CONTEXTS
121         while (atomic_dec_if_positive(&nr_free_contexts) < 0)
122                 steal_context();
123 #endif
124         ctx = next_mmu_context;
125         while (test_and_set_bit(ctx, context_map)) {
126                 ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
127                 if (ctx > LAST_CONTEXT)
128                         ctx = 0;
129         }
130         next_mmu_context = (ctx + 1) & LAST_CONTEXT;
131         mm->context = ctx;
132 #ifdef FEW_CONTEXTS
133         context_mm[ctx] = mm;
134 #endif
135 }
136
137 /*
138  * Set up the context for a new address space.
139  */
140 #define init_new_context(tsk,mm)        (((mm)->context = NO_CONTEXT), 0)
141
142 /*
143  * We're finished using the context for an address space.
144  */
145 static inline void destroy_context(struct mm_struct *mm)
146 {
147         if (mm->context != NO_CONTEXT) {
148                 clear_bit(mm->context, context_map);
149                 mm->context = NO_CONTEXT;
150 #ifdef FEW_CONTEXTS
151                 atomic_inc(&nr_free_contexts);
152 #endif
153         }
154 }
155
156 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
157                              struct task_struct *tsk)
158 {
159 #ifdef CONFIG_ALTIVEC
160         asm volatile (
161  BEGIN_FTR_SECTION
162         "dssall;\n"
163 #ifndef CONFIG_POWER4
164          "sync;\n" /* G4 needs a sync here, G5 apparently not */
165 #endif
166  END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
167          : : );
168 #endif /* CONFIG_ALTIVEC */
169
170         tsk->thread.pgdir = next->pgd;
171
172         /* No need to flush userspace segments if the mm doesnt change */
173         if (prev == next)
174                 return;
175
176         /* Setup new userspace context */
177         get_mmu_context(next);
178         set_context(next->context, next->pgd);
179 }
180
181 #define deactivate_mm(tsk,mm)   do { } while (0)
182
183 /*
184  * After we have set current->mm to a new value, this activates
185  * the context for the new mm so we see the new mappings.
186  */
187 #define activate_mm(active_mm, mm)   switch_mm(active_mm, mm, current)
188
189 extern void mmu_context_init(void);
190
191 #endif /* __PPC_MMU_CONTEXT_H */
192 #endif /* __KERNEL__ */