]>
Commit | Line | Data |
---|---|---|
5e696617 BH |
1 | /* |
2 | * This file contains the routines for handling the MMU on those | |
3 | * PowerPC implementations where the MMU is not using the hash | |
4 | * table, such as 8xx, 4xx, BookE's etc... | |
5 | * | |
6 | * Copyright 2008 Ben Herrenschmidt <benh@kernel.crashing.org> | |
7 | * IBM Corp. | |
8 | * | |
9 | * Derived from previous arch/powerpc/mm/mmu_context.c | |
10 | * and arch/powerpc/include/asm/mmu_context.h | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; either version | |
15 | * 2 of the License, or (at your option) any later version. | |
16 | * | |
2ca8cf73 BH |
17 | * TODO: |
18 | * | |
19 | * - The global context lock will not scale very well | |
20 | * - The maps should be dynamically allocated to allow for processors | |
21 | * that support more PID bits at runtime | |
22 | * - Implement flush_tlb_mm() by making the context stale and picking | |
23 | * a new one | |
24 | * - More aggressively clear stale map bits and maybe find some way to | |
25 | * also clear mm->cpu_vm_mask bits when processes are migrated | |
5e696617 BH |
26 | */ |
27 | ||
f1167fb3 BH |
28 | //#define DEBUG_MAP_CONSISTENCY |
29 | //#define DEBUG_CLAMP_LAST_CONTEXT 31 | |
fcce8109 BH |
30 | //#define DEBUG_HARDER |
31 | ||
32 | /* We don't use DEBUG because it tends to be compiled in always nowadays | |
33 | * and this would generate way too much output | |
34 | */ | |
35 | #ifdef DEBUG_HARDER | |
36 | #define pr_hard(args...) printk(KERN_DEBUG args) | |
37 | #define pr_hardcont(args...) printk(KERN_CONT args) | |
38 | #else | |
39 | #define pr_hard(args...) do { } while(0) | |
40 | #define pr_hardcont(args...) do { } while(0) | |
41 | #endif | |
2ca8cf73 BH |
42 | |
43 | #include <linux/kernel.h> | |
5e696617 BH |
44 | #include <linux/mm.h> |
45 | #include <linux/init.h> | |
77520351 BH |
46 | #include <linux/spinlock.h> |
47 | #include <linux/bootmem.h> | |
48 | #include <linux/notifier.h> | |
49 | #include <linux/cpu.h> | |
5a0e3ad6 | 50 | #include <linux/slab.h> |
5e696617 BH |
51 | |
52 | #include <asm/mmu_context.h> | |
53 | #include <asm/tlbflush.h> | |
5e696617 | 54 | |
77520351 | 55 | static unsigned int first_context, last_context; |
2ca8cf73 | 56 | static unsigned int next_context, nr_free_contexts; |
77520351 BH |
57 | static unsigned long *context_map; |
58 | static unsigned long *stale_map[NR_CPUS]; | |
59 | static struct mm_struct **context_mm; | |
be833f33 | 60 | static DEFINE_RAW_SPINLOCK(context_lock); |
5e696617 | 61 | |
77520351 BH |
62 | #define CTX_MAP_SIZE \ |
63 | (sizeof(unsigned long) * (last_context / BITS_PER_LONG + 1)) | |
64 | ||
65 | ||
5e696617 | 66 | /* Steal a context from a task that has one at the moment. |
2ca8cf73 BH |
67 | * |
68 | * This is used when we are running out of available PID numbers | |
69 | * on the processors. | |
70 | * | |
5e696617 BH |
71 | * This isn't an LRU system, it just frees up each context in |
72 | * turn (sort-of pseudo-random replacement :). This would be the | |
73 | * place to implement an LRU scheme if anyone was motivated to do it. | |
74 | * -- paulus | |
2ca8cf73 BH |
75 | * |
76 | * For context stealing, we use a slightly different approach for | |
77 | * SMP and UP. Basically, the UP one is simpler and doesn't use | |
78 | * the stale map as we can just flush the local CPU | |
79 | * -- benh | |
5e696617 | 80 | */ |
2ca8cf73 BH |
81 | #ifdef CONFIG_SMP |
82 | static unsigned int steal_context_smp(unsigned int id) | |
5e696617 BH |
83 | { |
84 | struct mm_struct *mm; | |
fcce8109 | 85 | unsigned int cpu, max, i; |
5e696617 | 86 | |
77520351 | 87 | max = last_context - first_context; |
5e696617 | 88 | |
2ca8cf73 BH |
89 | /* Attempt to free next_context first and then loop until we manage */ |
90 | while (max--) { | |
91 | /* Pick up the victim mm */ | |
92 | mm = context_mm[id]; | |
5e696617 | 93 | |
2ca8cf73 BH |
94 | /* We have a candidate victim, check if it's active, on SMP |
95 | * we cannot steal active contexts | |
96 | */ | |
97 | if (mm->context.active) { | |
98 | id++; | |
77520351 BH |
99 | if (id > last_context) |
100 | id = first_context; | |
2ca8cf73 BH |
101 | continue; |
102 | } | |
fcce8109 | 103 | pr_hardcont(" | steal %d from 0x%p", id, mm); |
2ca8cf73 BH |
104 | |
105 | /* Mark this mm has having no context anymore */ | |
106 | mm->context.id = MMU_NO_CONTEXT; | |
107 | ||
fcce8109 BH |
108 | /* Mark it stale on all CPUs that used this mm. For threaded |
109 | * implementations, we set it on all threads on each core | |
110 | * represented in the mask. A future implementation will use | |
111 | * a core map instead but this will do for now. | |
112 | */ | |
113 | for_each_cpu(cpu, mm_cpumask(mm)) { | |
114 | for (i = cpu_first_thread_in_core(cpu); | |
115 | i <= cpu_last_thread_in_core(cpu); i++) | |
116 | __set_bit(id, stale_map[i]); | |
117 | cpu = i - 1; | |
118 | } | |
2ca8cf73 BH |
119 | return id; |
120 | } | |
121 | ||
122 | /* This will happen if you have more CPUs than available contexts, | |
123 | * all we can do here is wait a bit and try again | |
124 | */ | |
be833f33 | 125 | raw_spin_unlock(&context_lock); |
2ca8cf73 | 126 | cpu_relax(); |
be833f33 | 127 | raw_spin_lock(&context_lock); |
3035c863 BH |
128 | |
129 | /* This will cause the caller to try again */ | |
130 | return MMU_NO_CONTEXT; | |
2ca8cf73 BH |
131 | } |
132 | #endif /* CONFIG_SMP */ | |
133 | ||
134 | /* Note that this will also be called on SMP if all other CPUs are | |
135 | * offlined, which means that it may be called for cpu != 0. For | |
136 | * this to work, we somewhat assume that CPUs that are onlined | |
137 | * come up with a fully clean TLB (or are cleaned when offlined) | |
5e696617 | 138 | */ |
2ca8cf73 | 139 | static unsigned int steal_context_up(unsigned int id) |
5e696617 | 140 | { |
2ca8cf73 BH |
141 | struct mm_struct *mm; |
142 | int cpu = smp_processor_id(); | |
5e696617 | 143 | |
2ca8cf73 BH |
144 | /* Pick up the victim mm */ |
145 | mm = context_mm[id]; | |
146 | ||
fcce8109 | 147 | pr_hardcont(" | steal %d from 0x%p", id, mm); |
5e696617 | 148 | |
2ca8cf73 BH |
149 | /* Flush the TLB for that context */ |
150 | local_flush_tlb_mm(mm); | |
151 | ||
8e35961b HS |
152 | /* Mark this mm has having no context anymore */ |
153 | mm->context.id = MMU_NO_CONTEXT; | |
154 | ||
2ca8cf73 BH |
155 | /* XXX This clear should ultimately be part of local_flush_tlb_mm */ |
156 | __clear_bit(id, stale_map[cpu]); | |
157 | ||
158 | return id; | |
159 | } | |
160 | ||
161 | #ifdef DEBUG_MAP_CONSISTENCY | |
162 | static void context_check_map(void) | |
163 | { | |
164 | unsigned int id, nrf, nact; | |
165 | ||
166 | nrf = nact = 0; | |
77520351 | 167 | for (id = first_context; id <= last_context; id++) { |
2ca8cf73 BH |
168 | int used = test_bit(id, context_map); |
169 | if (!used) | |
170 | nrf++; | |
171 | if (used != (context_mm[id] != NULL)) | |
172 | pr_err("MMU: Context %d is %s and MM is %p !\n", | |
173 | id, used ? "used" : "free", context_mm[id]); | |
174 | if (context_mm[id] != NULL) | |
175 | nact += context_mm[id]->context.active; | |
5e696617 | 176 | } |
2ca8cf73 BH |
177 | if (nrf != nr_free_contexts) { |
178 | pr_err("MMU: Free context count out of sync ! (%d vs %d)\n", | |
179 | nr_free_contexts, nrf); | |
180 | nr_free_contexts = nrf; | |
181 | } | |
182 | if (nact > num_online_cpus()) | |
183 | pr_err("MMU: More active contexts than CPUs ! (%d vs %d)\n", | |
184 | nact, num_online_cpus()); | |
77520351 BH |
185 | if (first_context > 0 && !test_bit(0, context_map)) |
186 | pr_err("MMU: Context 0 has been freed !!!\n"); | |
5e696617 | 187 | } |
2ca8cf73 BH |
188 | #else |
189 | static void context_check_map(void) { } | |
190 | #endif | |
5e696617 BH |
191 | |
192 | void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next) | |
193 | { | |
67050b5c | 194 | unsigned int i, id, cpu = smp_processor_id(); |
2ca8cf73 | 195 | unsigned long *map; |
5e696617 | 196 | |
2ca8cf73 | 197 | /* No lockless fast path .. yet */ |
be833f33 | 198 | raw_spin_lock(&context_lock); |
2ca8cf73 | 199 | |
fcce8109 BH |
200 | pr_hard("[%d] activating context for mm @%p, active=%d, id=%d", |
201 | cpu, next, next->context.active, next->context.id); | |
2ca8cf73 BH |
202 | |
203 | #ifdef CONFIG_SMP | |
204 | /* Mark us active and the previous one not anymore */ | |
205 | next->context.active++; | |
206 | if (prev) { | |
fcce8109 | 207 | pr_hardcont(" (old=0x%p a=%d)", prev, prev->context.active); |
2ca8cf73 BH |
208 | WARN_ON(prev->context.active < 1); |
209 | prev->context.active--; | |
210 | } | |
3035c863 BH |
211 | |
212 | again: | |
2ca8cf73 BH |
213 | #endif /* CONFIG_SMP */ |
214 | ||
215 | /* If we already have a valid assigned context, skip all that */ | |
216 | id = next->context.id; | |
fcce8109 BH |
217 | if (likely(id != MMU_NO_CONTEXT)) { |
218 | #ifdef DEBUG_MAP_CONSISTENCY | |
219 | if (context_mm[id] != next) | |
220 | pr_err("MMU: mm 0x%p has id %d but context_mm[%d] says 0x%p\n", | |
221 | next, id, id, context_mm[id]); | |
222 | #endif | |
2ca8cf73 | 223 | goto ctxt_ok; |
fcce8109 | 224 | } |
2ca8cf73 BH |
225 | |
226 | /* We really don't have a context, let's try to acquire one */ | |
227 | id = next_context; | |
77520351 BH |
228 | if (id > last_context) |
229 | id = first_context; | |
2ca8cf73 BH |
230 | map = context_map; |
231 | ||
232 | /* No more free contexts, let's try to steal one */ | |
233 | if (nr_free_contexts == 0) { | |
234 | #ifdef CONFIG_SMP | |
235 | if (num_online_cpus() > 1) { | |
236 | id = steal_context_smp(id); | |
3035c863 BH |
237 | if (id == MMU_NO_CONTEXT) |
238 | goto again; | |
5156ddce | 239 | goto stolen; |
2ca8cf73 BH |
240 | } |
241 | #endif /* CONFIG_SMP */ | |
242 | id = steal_context_up(id); | |
243 | goto stolen; | |
244 | } | |
245 | nr_free_contexts--; | |
246 | ||
247 | /* We know there's at least one free context, try to find it */ | |
248 | while (__test_and_set_bit(id, map)) { | |
77520351 BH |
249 | id = find_next_zero_bit(map, last_context+1, id); |
250 | if (id > last_context) | |
251 | id = first_context; | |
2ca8cf73 BH |
252 | } |
253 | stolen: | |
254 | next_context = id + 1; | |
255 | context_mm[id] = next; | |
256 | next->context.id = id; | |
fcce8109 | 257 | pr_hardcont(" | new id=%d,nrf=%d", id, nr_free_contexts); |
2ca8cf73 BH |
258 | |
259 | context_check_map(); | |
260 | ctxt_ok: | |
261 | ||
262 | /* If that context got marked stale on this CPU, then flush the | |
263 | * local TLB for it and unmark it before we use it | |
264 | */ | |
265 | if (test_bit(id, stale_map[cpu])) { | |
fcce8109 BH |
266 | pr_hardcont(" | stale flush %d [%d..%d]", |
267 | id, cpu_first_thread_in_core(cpu), | |
268 | cpu_last_thread_in_core(cpu)); | |
269 | ||
2ca8cf73 BH |
270 | local_flush_tlb_mm(next); |
271 | ||
272 | /* XXX This clear should ultimately be part of local_flush_tlb_mm */ | |
67050b5c KG |
273 | for (i = cpu_first_thread_in_core(cpu); |
274 | i <= cpu_last_thread_in_core(cpu); i++) { | |
275 | __clear_bit(id, stale_map[i]); | |
276 | } | |
2ca8cf73 BH |
277 | } |
278 | ||
279 | /* Flick the MMU and release lock */ | |
fcce8109 | 280 | pr_hardcont(" -> %d\n", id); |
2ca8cf73 | 281 | set_context(id, next->pgd); |
be833f33 | 282 | raw_spin_unlock(&context_lock); |
5e696617 BH |
283 | } |
284 | ||
285 | /* | |
286 | * Set up the context for a new address space. | |
287 | */ | |
288 | int init_new_context(struct task_struct *t, struct mm_struct *mm) | |
289 | { | |
fcce8109 BH |
290 | pr_hard("initing context for mm @%p\n", mm); |
291 | ||
2ca8cf73 BH |
292 | mm->context.id = MMU_NO_CONTEXT; |
293 | mm->context.active = 0; | |
294 | ||
5e696617 BH |
295 | return 0; |
296 | } | |
297 | ||
298 | /* | |
299 | * We're finished using the context for an address space. | |
300 | */ | |
301 | void destroy_context(struct mm_struct *mm) | |
302 | { | |
b46b6942 | 303 | unsigned long flags; |
2ca8cf73 BH |
304 | unsigned int id; |
305 | ||
306 | if (mm->context.id == MMU_NO_CONTEXT) | |
307 | return; | |
308 | ||
309 | WARN_ON(mm->context.active != 0); | |
310 | ||
be833f33 | 311 | raw_spin_lock_irqsave(&context_lock, flags); |
2ca8cf73 BH |
312 | id = mm->context.id; |
313 | if (id != MMU_NO_CONTEXT) { | |
314 | __clear_bit(id, context_map); | |
315 | mm->context.id = MMU_NO_CONTEXT; | |
316 | #ifdef DEBUG_MAP_CONSISTENCY | |
317 | mm->context.active = 0; | |
2ca8cf73 | 318 | #endif |
3035c863 | 319 | context_mm[id] = NULL; |
2ca8cf73 | 320 | nr_free_contexts++; |
5e696617 | 321 | } |
be833f33 | 322 | raw_spin_unlock_irqrestore(&context_lock, flags); |
5e696617 BH |
323 | } |
324 | ||
77520351 BH |
325 | #ifdef CONFIG_SMP |
326 | ||
327 | static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self, | |
328 | unsigned long action, void *hcpu) | |
329 | { | |
330 | unsigned int cpu = (unsigned int)(long)hcpu; | |
fcce8109 BH |
331 | #ifdef CONFIG_HOTPLUG_CPU |
332 | struct task_struct *p; | |
333 | #endif | |
77520351 BH |
334 | /* We don't touch CPU 0 map, it's allocated at aboot and kept |
335 | * around forever | |
336 | */ | |
337 | if (cpu == 0) | |
338 | return NOTIFY_OK; | |
339 | ||
340 | switch (action) { | |
341 | case CPU_ONLINE: | |
342 | case CPU_ONLINE_FROZEN: | |
a1ac38ab | 343 | pr_devel("MMU: Allocating stale context map for CPU %d\n", cpu); |
77520351 BH |
344 | stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL); |
345 | break; | |
346 | #ifdef CONFIG_HOTPLUG_CPU | |
347 | case CPU_DEAD: | |
348 | case CPU_DEAD_FROZEN: | |
a1ac38ab | 349 | pr_devel("MMU: Freeing stale context map for CPU %d\n", cpu); |
77520351 BH |
350 | kfree(stale_map[cpu]); |
351 | stale_map[cpu] = NULL; | |
fcce8109 BH |
352 | |
353 | /* We also clear the cpu_vm_mask bits of CPUs going away */ | |
354 | read_lock(&tasklist_lock); | |
355 | for_each_process(p) { | |
356 | if (p->mm) | |
f04b10cd | 357 | cpumask_clear_cpu(cpu, mm_cpumask(p->mm)); |
fcce8109 BH |
358 | } |
359 | read_unlock(&tasklist_lock); | |
360 | break; | |
361 | #endif /* CONFIG_HOTPLUG_CPU */ | |
77520351 BH |
362 | } |
363 | return NOTIFY_OK; | |
364 | } | |
365 | ||
366 | static struct notifier_block __cpuinitdata mmu_context_cpu_nb = { | |
367 | .notifier_call = mmu_context_cpu_notify, | |
368 | }; | |
369 | ||
370 | #endif /* CONFIG_SMP */ | |
5e696617 BH |
371 | |
372 | /* | |
373 | * Initialize the context management stuff. | |
374 | */ | |
375 | void __init mmu_context_init(void) | |
376 | { | |
2ca8cf73 BH |
377 | /* Mark init_mm as being active on all possible CPUs since |
378 | * we'll get called with prev == init_mm the first time | |
379 | * we schedule on a given CPU | |
380 | */ | |
381 | init_mm.context.active = NR_CPUS; | |
382 | ||
77520351 BH |
383 | /* |
384 | * The MPC8xx has only 16 contexts. We rotate through them on each | |
385 | * task switch. A better way would be to keep track of tasks that | |
386 | * own contexts, and implement an LRU usage. That way very active | |
387 | * tasks don't always have to pay the TLB reload overhead. The | |
388 | * kernel pages are mapped shared, so the kernel can run on behalf | |
389 | * of any task that makes a kernel entry. Shared does not mean they | |
390 | * are not protected, just that the ASID comparison is not performed. | |
391 | * -- Dan | |
392 | * | |
393 | * The IBM4xx has 256 contexts, so we can just rotate through these | |
394 | * as a way of "switching" contexts. If the TID of the TLB is zero, | |
395 | * the PID/TID comparison is disabled, so we can use a TID of zero | |
396 | * to represent all kernel pages as shared among all contexts. | |
397 | * -- Dan | |
398 | */ | |
399 | if (mmu_has_feature(MMU_FTR_TYPE_8xx)) { | |
400 | first_context = 0; | |
401 | last_context = 15; | |
402 | } else { | |
403 | first_context = 1; | |
404 | last_context = 255; | |
405 | } | |
406 | ||
407 | #ifdef DEBUG_CLAMP_LAST_CONTEXT | |
408 | last_context = DEBUG_CLAMP_LAST_CONTEXT; | |
409 | #endif | |
410 | /* | |
411 | * Allocate the maps used by context management | |
412 | */ | |
413 | context_map = alloc_bootmem(CTX_MAP_SIZE); | |
414 | context_mm = alloc_bootmem(sizeof(void *) * (last_context + 1)); | |
415 | stale_map[0] = alloc_bootmem(CTX_MAP_SIZE); | |
416 | ||
417 | #ifdef CONFIG_SMP | |
418 | register_cpu_notifier(&mmu_context_cpu_nb); | |
419 | #endif | |
420 | ||
421 | printk(KERN_INFO | |
ff7c6600 | 422 | "MMU: Allocated %zu bytes of context maps for %d contexts\n", |
77520351 BH |
423 | 2 * CTX_MAP_SIZE + (sizeof(void *) * (last_context + 1)), |
424 | last_context - first_context + 1); | |
425 | ||
5e696617 BH |
426 | /* |
427 | * Some processors have too few contexts to reserve one for | |
428 | * init_mm, and require using context 0 for a normal task. | |
429 | * Other processors reserve the use of context zero for the kernel. | |
77520351 | 430 | * This code assumes first_context < 32. |
5e696617 | 431 | */ |
77520351 BH |
432 | context_map[0] = (1 << first_context) - 1; |
433 | next_context = first_context; | |
434 | nr_free_contexts = last_context - first_context + 1; | |
5e696617 BH |
435 | } |
436 |