1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This file contains the routines for handling the MMU on those
4 * PowerPC implementations where the MMU is not using the hash
5 * table, such as 8xx, 4xx, BookE's etc...
7 * Copyright 2008 Ben Herrenschmidt <benh@kernel.crashing.org>
10 * Derived from previous arch/powerpc/mm/mmu_context.c
11 * and arch/powerpc/include/asm/mmu_context.h
15 * - The global context lock will not scale very well
16 * - The maps should be dynamically allocated to allow for processors
17 * that support more PID bits at runtime
18 * - Implement flush_tlb_mm() by making the context stale and picking
20 * - More aggressively clear stale map bits and maybe find some way to
21 * also clear mm->cpu_vm_mask bits when processes are migrated
24 //#define DEBUG_MAP_CONSISTENCY
25 //#define DEBUG_CLAMP_LAST_CONTEXT 31
26 //#define DEBUG_HARDER
28 /* We don't use DEBUG because it tends to be compiled in always nowadays
29 * and this would generate way too much output
32 #define pr_hard(args...) printk(KERN_DEBUG args)
33 #define pr_hardcont(args...) printk(KERN_CONT args)
35 #define pr_hard(args...) do { } while(0)
36 #define pr_hardcont(args...) do { } while(0)
39 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/spinlock.h>
43 #include <linux/memblock.h>
44 #include <linux/notifier.h>
45 #include <linux/cpu.h>
46 #include <linux/slab.h>
48 #include <asm/mmu_context.h>
49 #include <asm/tlbflush.h>
51 #include <mm/mmu_decl.h>
54 * The MPC8xx has only 16 contexts. We rotate through them on each task switch.
55 * A better way would be to keep track of tasks that own contexts, and implement
56 * an LRU usage. That way very active tasks don't always have to pay the TLB
57 * reload overhead. The kernel pages are mapped shared, so the kernel can run on
58 * behalf of any task that makes a kernel entry. Shared does not mean they are
59 * not protected, just that the ASID comparison is not performed. -- Dan
61 * The IBM4xx has 256 contexts, so we can just rotate through these as a way of
62 * "switching" contexts. If the TID of the TLB is zero, the PID/TID comparison
63 * is disabled, so we can use a TID of zero to represent all kernel pages as
64 * shared among all contexts. -- Dan
66 * The IBM 47x core supports 16-bit PIDs, thus 65535 contexts. We should
67 * normally never have to steal though the facility is present if needed.
70 #define FIRST_CONTEXT 1
71 #ifdef DEBUG_CLAMP_LAST_CONTEXT
72 #define LAST_CONTEXT DEBUG_CLAMP_LAST_CONTEXT
73 #elif defined(CONFIG_PPC_8xx)
74 #define LAST_CONTEXT 16
75 #elif defined(CONFIG_PPC_47x)
76 #define LAST_CONTEXT 65535
78 #define LAST_CONTEXT 255
81 static unsigned int next_context
, nr_free_contexts
;
82 static unsigned long *context_map
;
84 static unsigned long *stale_map
[NR_CPUS
];
86 static struct mm_struct
**context_mm
;
87 static DEFINE_RAW_SPINLOCK(context_lock
);
89 #define CTX_MAP_SIZE \
90 (sizeof(unsigned long) * (LAST_CONTEXT / BITS_PER_LONG + 1))
93 /* Steal a context from a task that has one at the moment.
95 * This is used when we are running out of available PID numbers
98 * This isn't an LRU system, it just frees up each context in
99 * turn (sort-of pseudo-random replacement :). This would be the
100 * place to implement an LRU scheme if anyone was motivated to do it.
103 * For context stealing, we use a slightly different approach for
104 * SMP and UP. Basically, the UP one is simpler and doesn't use
105 * the stale map as we can just flush the local CPU
109 static unsigned int steal_context_smp(unsigned int id
)
111 struct mm_struct
*mm
;
112 unsigned int cpu
, max
, i
;
114 max
= LAST_CONTEXT
- FIRST_CONTEXT
;
116 /* Attempt to free next_context first and then loop until we manage */
118 /* Pick up the victim mm */
121 /* We have a candidate victim, check if it's active, on SMP
122 * we cannot steal active contexts
124 if (mm
->context
.active
) {
126 if (id
> LAST_CONTEXT
)
130 pr_hardcont(" | steal %d from 0x%p", id
, mm
);
132 /* Mark this mm has having no context anymore */
133 mm
->context
.id
= MMU_NO_CONTEXT
;
135 /* Mark it stale on all CPUs that used this mm. For threaded
136 * implementations, we set it on all threads on each core
137 * represented in the mask. A future implementation will use
138 * a core map instead but this will do for now.
140 for_each_cpu(cpu
, mm_cpumask(mm
)) {
141 for (i
= cpu_first_thread_sibling(cpu
);
142 i
<= cpu_last_thread_sibling(cpu
); i
++) {
144 __set_bit(id
, stale_map
[i
]);
151 /* This will happen if you have more CPUs than available contexts,
152 * all we can do here is wait a bit and try again
154 raw_spin_unlock(&context_lock
);
156 raw_spin_lock(&context_lock
);
158 /* This will cause the caller to try again */
159 return MMU_NO_CONTEXT
;
161 #endif /* CONFIG_SMP */
163 static unsigned int steal_all_contexts(void)
165 struct mm_struct
*mm
;
167 int cpu
= smp_processor_id();
171 for (id
= FIRST_CONTEXT
; id
<= LAST_CONTEXT
; id
++) {
172 /* Pick up the victim mm */
175 pr_hardcont(" | steal %d from 0x%p", id
, mm
);
177 /* Mark this mm as having no context anymore */
178 mm
->context
.id
= MMU_NO_CONTEXT
;
179 if (id
!= FIRST_CONTEXT
) {
180 context_mm
[id
] = NULL
;
181 __clear_bit(id
, context_map
);
182 #ifdef DEBUG_MAP_CONSISTENCY
183 mm
->context
.active
= 0;
187 __clear_bit(id
, stale_map
[cpu
]);
191 /* Flush the TLB for all contexts (not to be used on SMP) */
194 nr_free_contexts
= LAST_CONTEXT
- FIRST_CONTEXT
;
196 return FIRST_CONTEXT
;
199 /* Note that this will also be called on SMP if all other CPUs are
200 * offlined, which means that it may be called for cpu != 0. For
201 * this to work, we somewhat assume that CPUs that are onlined
202 * come up with a fully clean TLB (or are cleaned when offlined)
204 static unsigned int steal_context_up(unsigned int id
)
206 struct mm_struct
*mm
;
208 int cpu
= smp_processor_id();
211 /* Pick up the victim mm */
214 pr_hardcont(" | steal %d from 0x%p", id
, mm
);
216 /* Flush the TLB for that context */
217 local_flush_tlb_mm(mm
);
219 /* Mark this mm has having no context anymore */
220 mm
->context
.id
= MMU_NO_CONTEXT
;
222 /* XXX This clear should ultimately be part of local_flush_tlb_mm */
224 __clear_bit(id
, stale_map
[cpu
]);
230 #ifdef DEBUG_MAP_CONSISTENCY
231 static void context_check_map(void)
233 unsigned int id
, nrf
, nact
;
236 for (id
= FIRST_CONTEXT
; id
<= LAST_CONTEXT
; id
++) {
237 int used
= test_bit(id
, context_map
);
240 if (used
!= (context_mm
[id
] != NULL
))
241 pr_err("MMU: Context %d is %s and MM is %p !\n",
242 id
, used
? "used" : "free", context_mm
[id
]);
243 if (context_mm
[id
] != NULL
)
244 nact
+= context_mm
[id
]->context
.active
;
246 if (nrf
!= nr_free_contexts
) {
247 pr_err("MMU: Free context count out of sync ! (%d vs %d)\n",
248 nr_free_contexts
, nrf
);
249 nr_free_contexts
= nrf
;
251 if (nact
> num_online_cpus())
252 pr_err("MMU: More active contexts than CPUs ! (%d vs %d)\n",
253 nact
, num_online_cpus());
254 if (FIRST_CONTEXT
> 0 && !test_bit(0, context_map
))
255 pr_err("MMU: Context 0 has been freed !!!\n");
258 static void context_check_map(void) { }
261 void switch_mmu_context(struct mm_struct
*prev
, struct mm_struct
*next
,
262 struct task_struct
*tsk
)
266 unsigned int i
, cpu
= smp_processor_id();
270 /* No lockless fast path .. yet */
271 raw_spin_lock(&context_lock
);
273 pr_hard("[%d] activating context for mm @%p, active=%d, id=%d",
274 cpu
, next
, next
->context
.active
, next
->context
.id
);
277 /* Mark us active and the previous one not anymore */
278 next
->context
.active
++;
280 pr_hardcont(" (old=0x%p a=%d)", prev
, prev
->context
.active
);
281 WARN_ON(prev
->context
.active
< 1);
282 prev
->context
.active
--;
286 #endif /* CONFIG_SMP */
288 /* If we already have a valid assigned context, skip all that */
289 id
= next
->context
.id
;
290 if (likely(id
!= MMU_NO_CONTEXT
)) {
291 #ifdef DEBUG_MAP_CONSISTENCY
292 if (context_mm
[id
] != next
)
293 pr_err("MMU: mm 0x%p has id %d but context_mm[%d] says 0x%p\n",
294 next
, id
, id
, context_mm
[id
]);
299 /* We really don't have a context, let's try to acquire one */
301 if (id
> LAST_CONTEXT
)
305 /* No more free contexts, let's try to steal one */
306 if (nr_free_contexts
== 0) {
308 if (num_online_cpus() > 1) {
309 id
= steal_context_smp(id
);
310 if (id
== MMU_NO_CONTEXT
)
314 #endif /* CONFIG_SMP */
315 if (IS_ENABLED(CONFIG_PPC_8xx
))
316 id
= steal_all_contexts();
318 id
= steal_context_up(id
);
323 /* We know there's at least one free context, try to find it */
324 while (__test_and_set_bit(id
, map
)) {
325 id
= find_next_zero_bit(map
, LAST_CONTEXT
+1, id
);
326 if (id
> LAST_CONTEXT
)
330 next_context
= id
+ 1;
331 context_mm
[id
] = next
;
332 next
->context
.id
= id
;
333 pr_hardcont(" | new id=%d,nrf=%d", id
, nr_free_contexts
);
338 /* If that context got marked stale on this CPU, then flush the
339 * local TLB for it and unmark it before we use it
342 if (test_bit(id
, stale_map
[cpu
])) {
343 pr_hardcont(" | stale flush %d [%d..%d]",
344 id
, cpu_first_thread_sibling(cpu
),
345 cpu_last_thread_sibling(cpu
));
347 local_flush_tlb_mm(next
);
349 /* XXX This clear should ultimately be part of local_flush_tlb_mm */
350 for (i
= cpu_first_thread_sibling(cpu
);
351 i
<= cpu_last_thread_sibling(cpu
); i
++) {
353 __clear_bit(id
, stale_map
[i
]);
358 /* Flick the MMU and release lock */
359 pr_hardcont(" -> %d\n", id
);
360 set_context(id
, next
->pgd
);
361 raw_spin_unlock(&context_lock
);
365 * Set up the context for a new address space.
367 int init_new_context(struct task_struct
*t
, struct mm_struct
*mm
)
369 pr_hard("initing context for mm @%p\n", mm
);
372 * We have MMU_NO_CONTEXT set to be ~0. Hence check
373 * explicitly against context.id == 0. This ensures that we properly
374 * initialize context slice details for newly allocated mm's (which will
375 * have id == 0) and don't alter context slice inherited via fork (which
376 * will have id != 0).
378 if (mm
->context
.id
== 0)
379 slice_init_new_context_exec(mm
);
380 mm
->context
.id
= MMU_NO_CONTEXT
;
381 mm
->context
.active
= 0;
382 pte_frag_set(&mm
->context
, NULL
);
387 * We're finished using the context for an address space.
389 void destroy_context(struct mm_struct
*mm
)
394 if (mm
->context
.id
== MMU_NO_CONTEXT
)
397 WARN_ON(mm
->context
.active
!= 0);
399 raw_spin_lock_irqsave(&context_lock
, flags
);
401 if (id
!= MMU_NO_CONTEXT
) {
402 __clear_bit(id
, context_map
);
403 mm
->context
.id
= MMU_NO_CONTEXT
;
404 #ifdef DEBUG_MAP_CONSISTENCY
405 mm
->context
.active
= 0;
407 context_mm
[id
] = NULL
;
410 raw_spin_unlock_irqrestore(&context_lock
, flags
);
414 static int mmu_ctx_cpu_prepare(unsigned int cpu
)
416 /* We don't touch CPU 0 map, it's allocated at aboot and kept
419 if (cpu
== boot_cpuid
)
422 pr_devel("MMU: Allocating stale context map for CPU %d\n", cpu
);
423 stale_map
[cpu
] = kzalloc(CTX_MAP_SIZE
, GFP_KERNEL
);
427 static int mmu_ctx_cpu_dead(unsigned int cpu
)
429 #ifdef CONFIG_HOTPLUG_CPU
430 if (cpu
== boot_cpuid
)
433 pr_devel("MMU: Freeing stale context map for CPU %d\n", cpu
);
434 kfree(stale_map
[cpu
]);
435 stale_map
[cpu
] = NULL
;
437 /* We also clear the cpu_vm_mask bits of CPUs going away */
438 clear_tasks_mm_cpumask(cpu
);
443 #endif /* CONFIG_SMP */
446 * Initialize the context management stuff.
448 void __init
mmu_context_init(void)
450 /* Mark init_mm as being active on all possible CPUs since
451 * we'll get called with prev == init_mm the first time
452 * we schedule on a given CPU
454 init_mm
.context
.active
= NR_CPUS
;
457 * Allocate the maps used by context management
459 context_map
= memblock_alloc(CTX_MAP_SIZE
, SMP_CACHE_BYTES
);
461 panic("%s: Failed to allocate %zu bytes\n", __func__
,
463 context_mm
= memblock_alloc(sizeof(void *) * (LAST_CONTEXT
+ 1),
466 panic("%s: Failed to allocate %zu bytes\n", __func__
,
467 sizeof(void *) * (LAST_CONTEXT
+ 1));
469 stale_map
[boot_cpuid
] = memblock_alloc(CTX_MAP_SIZE
, SMP_CACHE_BYTES
);
470 if (!stale_map
[boot_cpuid
])
471 panic("%s: Failed to allocate %zu bytes\n", __func__
,
474 cpuhp_setup_state_nocalls(CPUHP_POWERPC_MMU_CTX_PREPARE
,
475 "powerpc/mmu/ctx:prepare",
476 mmu_ctx_cpu_prepare
, mmu_ctx_cpu_dead
);
480 "MMU: Allocated %zu bytes of context maps for %d contexts\n",
481 2 * CTX_MAP_SIZE
+ (sizeof(void *) * (LAST_CONTEXT
+ 1)),
482 LAST_CONTEXT
- FIRST_CONTEXT
+ 1);
485 * Some processors have too few contexts to reserve one for
486 * init_mm, and require using context 0 for a normal task.
487 * Other processors reserve the use of context zero for the kernel.
488 * This code assumes FIRST_CONTEXT < 32.
490 context_map
[0] = (1 << FIRST_CONTEXT
) - 1;
491 next_context
= FIRST_CONTEXT
;
492 nr_free_contexts
= LAST_CONTEXT
- FIRST_CONTEXT
+ 1;