// SPDX-License-Identifier: GPL-2.0-or-later1/*2* This file contains the routines for handling the MMU on those3* PowerPC implementations where the MMU is not using the hash4* table, such as 8xx, 4xx, BookE's etc...5*6* Copyright 2008 Ben Herrenschmidt <[email protected]>7* IBM Corp.8*9* Derived from previous arch/powerpc/mm/mmu_context.c10* and arch/powerpc/include/asm/mmu_context.h11*12* TODO:13*14* - The global context lock will not scale very well15* - The maps should be dynamically allocated to allow for processors16* that support more PID bits at runtime17* - Implement flush_tlb_mm() by making the context stale and picking18* a new one19* - More aggressively clear stale map bits and maybe find some way to20* also clear mm->cpu_vm_mask bits when processes are migrated21*/2223#include <linux/kernel.h>24#include <linux/mm.h>25#include <linux/init.h>26#include <linux/spinlock.h>27#include <linux/memblock.h>28#include <linux/notifier.h>29#include <linux/cpu.h>30#include <linux/slab.h>3132#include <asm/mmu_context.h>33#include <asm/tlbflush.h>34#include <asm/smp.h>35#include <asm/kup.h>3637#include <mm/mmu_decl.h>3839/*40* Room for two PTE table pointers, usually the kernel and current user41* pointer to their respective root page table (pgdir).42*/43void *abatron_pteptrs[2];4445/*46* The MPC8xx has only 16 contexts. We rotate through them on each task switch.47* A better way would be to keep track of tasks that own contexts, and implement48* an LRU usage. That way very active tasks don't always have to pay the TLB49* reload overhead. The kernel pages are mapped shared, so the kernel can run on50* behalf of any task that makes a kernel entry. Shared does not mean they are51* not protected, just that the ASID comparison is not performed. -- Dan52*53* The IBM4xx has 256 contexts, so we can just rotate through these as a way of54* "switching" contexts. If the TID of the TLB is zero, the PID/TID comparison55* is disabled, so we can use a TID of zero to represent all kernel pages as56* shared among all contexts. -- Dan57*58* The IBM 47x core supports 16-bit PIDs, thus 65535 contexts. We should59* normally never have to steal though the facility is present if needed.60* -- BenH61*/62#define FIRST_CONTEXT 163#if defined(CONFIG_PPC_8xx)64#define LAST_CONTEXT 1665#elif defined(CONFIG_PPC_47x)66#define LAST_CONTEXT 6553567#else68#define LAST_CONTEXT 25569#endif7071static unsigned int next_context, nr_free_contexts;72static unsigned long *context_map;73static unsigned long *stale_map[NR_CPUS];74static struct mm_struct **context_mm;75static DEFINE_RAW_SPINLOCK(context_lock);7677#define CTX_MAP_SIZE \78(sizeof(unsigned long) * (LAST_CONTEXT / BITS_PER_LONG + 1))798081/* Steal a context from a task that has one at the moment.82*83* This is used when we are running out of available PID numbers84* on the processors.85*86* This isn't an LRU system, it just frees up each context in87* turn (sort-of pseudo-random replacement :). This would be the88* place to implement an LRU scheme if anyone was motivated to do it.89* -- paulus90*91* For context stealing, we use a slightly different approach for92* SMP and UP. Basically, the UP one is simpler and doesn't use93* the stale map as we can just flush the local CPU94* -- benh95*/96static unsigned int steal_context_smp(unsigned int id)97{98struct mm_struct *mm;99unsigned int cpu, max, i;100101max = LAST_CONTEXT - FIRST_CONTEXT;102103/* Attempt to free next_context first and then loop until we manage */104while (max--) {105/* Pick up the victim mm */106mm = context_mm[id];107108/* We have a candidate victim, check if it's active, on SMP109* we cannot steal active contexts110*/111if (mm->context.active) {112id++;113if (id > LAST_CONTEXT)114id = FIRST_CONTEXT;115continue;116}117118/* Mark this mm has having no context anymore */119mm->context.id = MMU_NO_CONTEXT;120121/* Mark it stale on all CPUs that used this mm. For threaded122* implementations, we set it on all threads on each core123* represented in the mask. A future implementation will use124* a core map instead but this will do for now.125*/126for_each_cpu(cpu, mm_cpumask(mm)) {127for (i = cpu_first_thread_sibling(cpu);128i <= cpu_last_thread_sibling(cpu); i++) {129if (stale_map[i])130__set_bit(id, stale_map[i]);131}132cpu = i - 1;133}134return id;135}136137/* This will happen if you have more CPUs than available contexts,138* all we can do here is wait a bit and try again139*/140raw_spin_unlock(&context_lock);141cpu_relax();142raw_spin_lock(&context_lock);143144/* This will cause the caller to try again */145return MMU_NO_CONTEXT;146}147148static unsigned int steal_all_contexts(void)149{150struct mm_struct *mm;151int cpu = smp_processor_id();152unsigned int id;153154for (id = FIRST_CONTEXT; id <= LAST_CONTEXT; id++) {155/* Pick up the victim mm */156mm = context_mm[id];157158/* Mark this mm as having no context anymore */159mm->context.id = MMU_NO_CONTEXT;160if (id != FIRST_CONTEXT) {161context_mm[id] = NULL;162__clear_bit(id, context_map);163}164if (IS_ENABLED(CONFIG_SMP))165__clear_bit(id, stale_map[cpu]);166}167168/* Flush the TLB for all contexts (not to be used on SMP) */169_tlbil_all();170171nr_free_contexts = LAST_CONTEXT - FIRST_CONTEXT;172173return FIRST_CONTEXT;174}175176/* Note that this will also be called on SMP if all other CPUs are177* offlined, which means that it may be called for cpu != 0. For178* this to work, we somewhat assume that CPUs that are onlined179* come up with a fully clean TLB (or are cleaned when offlined)180*/181static unsigned int steal_context_up(unsigned int id)182{183struct mm_struct *mm;184int cpu = smp_processor_id();185186/* Pick up the victim mm */187mm = context_mm[id];188189/* Flush the TLB for that context */190local_flush_tlb_mm(mm);191192/* Mark this mm has having no context anymore */193mm->context.id = MMU_NO_CONTEXT;194195/* XXX This clear should ultimately be part of local_flush_tlb_mm */196if (IS_ENABLED(CONFIG_SMP))197__clear_bit(id, stale_map[cpu]);198199return id;200}201202static void set_context(unsigned long id, pgd_t *pgd)203{204if (IS_ENABLED(CONFIG_PPC_8xx)) {205mtspr(SPRN_M_TWB, __pa(pgd));206207/* Update context */208mtspr(SPRN_M_CASID, id - 1);209210/* sync */211mb();212} else if (kuap_is_disabled()) {213mtspr(SPRN_PID, id);214isync();215}216}217218void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,219struct task_struct *tsk)220{221unsigned int id;222unsigned int i, cpu = smp_processor_id();223unsigned long *map;224225/* No lockless fast path .. yet */226raw_spin_lock(&context_lock);227228if (IS_ENABLED(CONFIG_SMP)) {229/* Mark us active and the previous one not anymore */230next->context.active++;231if (prev) {232WARN_ON(prev->context.active < 1);233prev->context.active--;234}235}236237again:238239/* If we already have a valid assigned context, skip all that */240id = next->context.id;241if (likely(id != MMU_NO_CONTEXT))242goto ctxt_ok;243244/* We really don't have a context, let's try to acquire one */245id = next_context;246if (id > LAST_CONTEXT)247id = FIRST_CONTEXT;248map = context_map;249250/* No more free contexts, let's try to steal one */251if (nr_free_contexts == 0) {252if (num_online_cpus() > 1) {253id = steal_context_smp(id);254if (id == MMU_NO_CONTEXT)255goto again;256goto stolen;257}258if (IS_ENABLED(CONFIG_PPC_8xx))259id = steal_all_contexts();260else261id = steal_context_up(id);262goto stolen;263}264nr_free_contexts--;265266/* We know there's at least one free context, try to find it */267while (__test_and_set_bit(id, map)) {268id = find_next_zero_bit(map, LAST_CONTEXT+1, id);269if (id > LAST_CONTEXT)270id = FIRST_CONTEXT;271}272stolen:273next_context = id + 1;274context_mm[id] = next;275next->context.id = id;276277ctxt_ok:278279/* If that context got marked stale on this CPU, then flush the280* local TLB for it and unmark it before we use it281*/282if (IS_ENABLED(CONFIG_SMP) && test_bit(id, stale_map[cpu])) {283local_flush_tlb_mm(next);284285/* XXX This clear should ultimately be part of local_flush_tlb_mm */286for (i = cpu_first_thread_sibling(cpu);287i <= cpu_last_thread_sibling(cpu); i++) {288if (stale_map[i])289__clear_bit(id, stale_map[i]);290}291}292293/* Flick the MMU and release lock */294if (IS_ENABLED(CONFIG_BDI_SWITCH))295abatron_pteptrs[1] = next->pgd;296set_context(id, next->pgd);297#if defined(CONFIG_BOOKE) && defined(CONFIG_PPC_KUAP)298tsk->thread.pid = id;299#endif300raw_spin_unlock(&context_lock);301}302303/*304* Set up the context for a new address space.305*/306int init_new_context(struct task_struct *t, struct mm_struct *mm)307{308mm->context.id = MMU_NO_CONTEXT;309mm->context.active = 0;310pte_frag_set(&mm->context, NULL);311return 0;312}313314/*315* We're finished using the context for an address space.316*/317void destroy_context(struct mm_struct *mm)318{319unsigned long flags;320unsigned int id;321322if (mm->context.id == MMU_NO_CONTEXT)323return;324325WARN_ON(mm->context.active != 0);326327raw_spin_lock_irqsave(&context_lock, flags);328id = mm->context.id;329if (id != MMU_NO_CONTEXT) {330__clear_bit(id, context_map);331mm->context.id = MMU_NO_CONTEXT;332context_mm[id] = NULL;333nr_free_contexts++;334}335raw_spin_unlock_irqrestore(&context_lock, flags);336}337338static int mmu_ctx_cpu_prepare(unsigned int cpu)339{340/* We don't touch CPU 0 map, it's allocated at aboot and kept341* around forever342*/343if (cpu == boot_cpuid)344return 0;345346stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL);347return 0;348}349350static int mmu_ctx_cpu_dead(unsigned int cpu)351{352#ifdef CONFIG_HOTPLUG_CPU353if (cpu == boot_cpuid)354return 0;355356kfree(stale_map[cpu]);357stale_map[cpu] = NULL;358359/* We also clear the cpu_vm_mask bits of CPUs going away */360clear_tasks_mm_cpumask(cpu);361#endif362return 0;363}364365/*366* Initialize the context management stuff.367*/368void __init mmu_context_init(void)369{370/* Mark init_mm as being active on all possible CPUs since371* we'll get called with prev == init_mm the first time372* we schedule on a given CPU373*/374init_mm.context.active = NR_CPUS;375376/*377* Allocate the maps used by context management378*/379context_map = memblock_alloc_or_panic(CTX_MAP_SIZE, SMP_CACHE_BYTES);380context_mm = memblock_alloc_or_panic(sizeof(void *) * (LAST_CONTEXT + 1),381SMP_CACHE_BYTES);382if (IS_ENABLED(CONFIG_SMP)) {383stale_map[boot_cpuid] = memblock_alloc_or_panic(CTX_MAP_SIZE, SMP_CACHE_BYTES);384cpuhp_setup_state_nocalls(CPUHP_POWERPC_MMU_CTX_PREPARE,385"powerpc/mmu/ctx:prepare",386mmu_ctx_cpu_prepare, mmu_ctx_cpu_dead);387}388389printk(KERN_INFO390"MMU: Allocated %zu bytes of context maps for %d contexts\n",3912 * CTX_MAP_SIZE + (sizeof(void *) * (LAST_CONTEXT + 1)),392LAST_CONTEXT - FIRST_CONTEXT + 1);393394/*395* Some processors have too few contexts to reserve one for396* init_mm, and require using context 0 for a normal task.397* Other processors reserve the use of context zero for the kernel.398* This code assumes FIRST_CONTEXT < 32.399*/400context_map[0] = (1 << FIRST_CONTEXT) - 1;401next_context = FIRST_CONTEXT;402nr_free_contexts = LAST_CONTEXT - FIRST_CONTEXT + 1;403}404405406