// 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)) {205s16 offset = (s16)(__pa(swapper_pg_dir));206207/*208* Register M_TWB will contain base address of level 1 table minus the209* lower part of the kernel PGDIR base address, so that all accesses to210* level 1 table are done relative to lower part of kernel PGDIR base211* address.212*/213mtspr(SPRN_M_TWB, __pa(pgd) - offset);214215/* Update context */216mtspr(SPRN_M_CASID, id - 1);217218/* sync */219mb();220} else if (kuap_is_disabled()) {221mtspr(SPRN_PID, id);222isync();223}224}225226void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,227struct task_struct *tsk)228{229unsigned int id;230unsigned int i, cpu = smp_processor_id();231unsigned long *map;232233/* No lockless fast path .. yet */234raw_spin_lock(&context_lock);235236if (IS_ENABLED(CONFIG_SMP)) {237/* Mark us active and the previous one not anymore */238next->context.active++;239if (prev) {240WARN_ON(prev->context.active < 1);241prev->context.active--;242}243}244245again:246247/* If we already have a valid assigned context, skip all that */248id = next->context.id;249if (likely(id != MMU_NO_CONTEXT))250goto ctxt_ok;251252/* We really don't have a context, let's try to acquire one */253id = next_context;254if (id > LAST_CONTEXT)255id = FIRST_CONTEXT;256map = context_map;257258/* No more free contexts, let's try to steal one */259if (nr_free_contexts == 0) {260if (num_online_cpus() > 1) {261id = steal_context_smp(id);262if (id == MMU_NO_CONTEXT)263goto again;264goto stolen;265}266if (IS_ENABLED(CONFIG_PPC_8xx))267id = steal_all_contexts();268else269id = steal_context_up(id);270goto stolen;271}272nr_free_contexts--;273274/* We know there's at least one free context, try to find it */275while (__test_and_set_bit(id, map)) {276id = find_next_zero_bit(map, LAST_CONTEXT+1, id);277if (id > LAST_CONTEXT)278id = FIRST_CONTEXT;279}280stolen:281next_context = id + 1;282context_mm[id] = next;283next->context.id = id;284285ctxt_ok:286287/* If that context got marked stale on this CPU, then flush the288* local TLB for it and unmark it before we use it289*/290if (IS_ENABLED(CONFIG_SMP) && test_bit(id, stale_map[cpu])) {291local_flush_tlb_mm(next);292293/* XXX This clear should ultimately be part of local_flush_tlb_mm */294for (i = cpu_first_thread_sibling(cpu);295i <= cpu_last_thread_sibling(cpu); i++) {296if (stale_map[i])297__clear_bit(id, stale_map[i]);298}299}300301/* Flick the MMU and release lock */302if (IS_ENABLED(CONFIG_BDI_SWITCH))303abatron_pteptrs[1] = next->pgd;304set_context(id, next->pgd);305#if defined(CONFIG_BOOKE) && defined(CONFIG_PPC_KUAP)306tsk->thread.pid = id;307#endif308raw_spin_unlock(&context_lock);309}310311/*312* Set up the context for a new address space.313*/314int init_new_context(struct task_struct *t, struct mm_struct *mm)315{316mm->context.id = MMU_NO_CONTEXT;317mm->context.active = 0;318pte_frag_set(&mm->context, NULL);319return 0;320}321322/*323* We're finished using the context for an address space.324*/325void destroy_context(struct mm_struct *mm)326{327unsigned long flags;328unsigned int id;329330if (mm->context.id == MMU_NO_CONTEXT)331return;332333WARN_ON(mm->context.active != 0);334335raw_spin_lock_irqsave(&context_lock, flags);336id = mm->context.id;337if (id != MMU_NO_CONTEXT) {338__clear_bit(id, context_map);339mm->context.id = MMU_NO_CONTEXT;340context_mm[id] = NULL;341nr_free_contexts++;342}343raw_spin_unlock_irqrestore(&context_lock, flags);344}345346static int mmu_ctx_cpu_prepare(unsigned int cpu)347{348/* We don't touch CPU 0 map, it's allocated at aboot and kept349* around forever350*/351if (cpu == boot_cpuid)352return 0;353354stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL);355return 0;356}357358static int mmu_ctx_cpu_dead(unsigned int cpu)359{360#ifdef CONFIG_HOTPLUG_CPU361if (cpu == boot_cpuid)362return 0;363364kfree(stale_map[cpu]);365stale_map[cpu] = NULL;366367/* We also clear the cpu_vm_mask bits of CPUs going away */368clear_tasks_mm_cpumask(cpu);369#endif370return 0;371}372373/*374* Initialize the context management stuff.375*/376void __init mmu_context_init(void)377{378/* Mark init_mm as being active on all possible CPUs since379* we'll get called with prev == init_mm the first time380* we schedule on a given CPU381*/382init_mm.context.active = NR_CPUS;383384/*385* Allocate the maps used by context management386*/387context_map = memblock_alloc_or_panic(CTX_MAP_SIZE, SMP_CACHE_BYTES);388context_mm = memblock_alloc_or_panic(sizeof(void *) * (LAST_CONTEXT + 1),389SMP_CACHE_BYTES);390if (IS_ENABLED(CONFIG_SMP)) {391stale_map[boot_cpuid] = memblock_alloc_or_panic(CTX_MAP_SIZE, SMP_CACHE_BYTES);392cpuhp_setup_state_nocalls(CPUHP_POWERPC_MMU_CTX_PREPARE,393"powerpc/mmu/ctx:prepare",394mmu_ctx_cpu_prepare, mmu_ctx_cpu_dead);395}396397printk(KERN_INFO398"MMU: Allocated %zu bytes of context maps for %d contexts\n",3992 * CTX_MAP_SIZE + (sizeof(void *) * (LAST_CONTEXT + 1)),400LAST_CONTEXT - FIRST_CONTEXT + 1);401402/*403* Some processors have too few contexts to reserve one for404* init_mm, and require using context 0 for a normal task.405* Other processors reserve the use of context zero for the kernel.406* This code assumes FIRST_CONTEXT < 32.407*/408context_map[0] = (1 << FIRST_CONTEXT) - 1;409next_context = FIRST_CONTEXT;410nr_free_contexts = LAST_CONTEXT - FIRST_CONTEXT + 1;411}412413414