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