// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2012 Regents of the University of California3* Copyright (C) 2017 SiFive4* Copyright (C) 2021 Western Digital Corporation or its affiliates.5*/67#include <linux/bitops.h>8#include <linux/cpumask.h>9#include <linux/mm.h>10#include <linux/percpu.h>11#include <linux/slab.h>12#include <linux/spinlock.h>13#include <linux/static_key.h>14#include <asm/tlbflush.h>15#include <asm/cacheflush.h>16#include <asm/mmu_context.h>17#include <asm/switch_to.h>1819#ifdef CONFIG_MMU2021DEFINE_STATIC_KEY_FALSE(use_asid_allocator);2223static unsigned long num_asids;2425static atomic_long_t current_version;2627static DEFINE_RAW_SPINLOCK(context_lock);28static cpumask_t context_tlb_flush_pending;29static unsigned long *context_asid_map;3031static DEFINE_PER_CPU(atomic_long_t, active_context);32static DEFINE_PER_CPU(unsigned long, reserved_context);3334static bool check_update_reserved_context(unsigned long cntx,35unsigned long newcntx)36{37int cpu;38bool hit = false;3940/*41* Iterate over the set of reserved CONTEXT looking for a match.42* If we find one, then we can update our mm to use new CONTEXT43* (i.e. the same CONTEXT in the current_version) but we can't44* exit the loop early, since we need to ensure that all copies45* of the old CONTEXT are updated to reflect the mm. Failure to do46* so could result in us missing the reserved CONTEXT in a future47* version.48*/49for_each_possible_cpu(cpu) {50if (per_cpu(reserved_context, cpu) == cntx) {51hit = true;52per_cpu(reserved_context, cpu) = newcntx;53}54}5556return hit;57}5859static void __flush_context(void)60{61int i;62unsigned long cntx;6364/* Must be called with context_lock held */65lockdep_assert_held(&context_lock);6667/* Update the list of reserved ASIDs and the ASID bitmap. */68bitmap_zero(context_asid_map, num_asids);6970/* Mark already active ASIDs as used */71for_each_possible_cpu(i) {72cntx = atomic_long_xchg_relaxed(&per_cpu(active_context, i), 0);73/*74* If this CPU has already been through a rollover, but75* hasn't run another task in the meantime, we must preserve76* its reserved CONTEXT, as this is the only trace we have of77* the process it is still running.78*/79if (cntx == 0)80cntx = per_cpu(reserved_context, i);8182__set_bit(cntx2asid(cntx), context_asid_map);83per_cpu(reserved_context, i) = cntx;84}8586/* Mark ASID #0 as used because it is used at boot-time */87__set_bit(0, context_asid_map);8889/* Queue a TLB invalidation for each CPU on next context-switch */90cpumask_setall(&context_tlb_flush_pending);91}9293static unsigned long __new_context(struct mm_struct *mm)94{95static u32 cur_idx = 1;96unsigned long cntx = atomic_long_read(&mm->context.id);97unsigned long asid, ver = atomic_long_read(¤t_version);9899/* Must be called with context_lock held */100lockdep_assert_held(&context_lock);101102if (cntx != 0) {103unsigned long newcntx = ver | cntx2asid(cntx);104105/*106* If our current CONTEXT was active during a rollover, we107* can continue to use it and this was just a false alarm.108*/109if (check_update_reserved_context(cntx, newcntx))110return newcntx;111112/*113* We had a valid CONTEXT in a previous life, so try to114* re-use it if possible.115*/116if (!__test_and_set_bit(cntx2asid(cntx), context_asid_map))117return newcntx;118}119120/*121* Allocate a free ASID. If we can't find one then increment122* current_version and flush all ASIDs.123*/124asid = find_next_zero_bit(context_asid_map, num_asids, cur_idx);125if (asid != num_asids)126goto set_asid;127128/* We're out of ASIDs, so increment current_version */129ver = atomic_long_add_return_relaxed(BIT(SATP_ASID_BITS), ¤t_version);130131/* Flush everything */132__flush_context();133134/* We have more ASIDs than CPUs, so this will always succeed */135asid = find_next_zero_bit(context_asid_map, num_asids, 1);136137set_asid:138__set_bit(asid, context_asid_map);139cur_idx = asid;140return asid | ver;141}142143static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)144{145unsigned long flags;146bool need_flush_tlb = false;147unsigned long cntx, old_active_cntx;148149cntx = atomic_long_read(&mm->context.id);150151/*152* If our active_context is non-zero and the context matches the153* current_version, then we update the active_context entry with a154* relaxed cmpxchg.155*156* Following is how we handle racing with a concurrent rollover:157*158* - We get a zero back from the cmpxchg and end up waiting on the159* lock. Taking the lock synchronises with the rollover and so160* we are forced to see the updated version.161*162* - We get a valid context back from the cmpxchg then we continue163* using old ASID because __flush_context() would have marked ASID164* of active_context as used and next context switch we will165* allocate new context.166*/167old_active_cntx = atomic_long_read(&per_cpu(active_context, cpu));168if (old_active_cntx &&169(cntx2version(cntx) == atomic_long_read(¤t_version)) &&170atomic_long_cmpxchg_relaxed(&per_cpu(active_context, cpu),171old_active_cntx, cntx))172goto switch_mm_fast;173174raw_spin_lock_irqsave(&context_lock, flags);175176/* Check that our ASID belongs to the current_version. */177cntx = atomic_long_read(&mm->context.id);178if (cntx2version(cntx) != atomic_long_read(¤t_version)) {179cntx = __new_context(mm);180atomic_long_set(&mm->context.id, cntx);181}182183if (cpumask_test_and_clear_cpu(cpu, &context_tlb_flush_pending))184need_flush_tlb = true;185186atomic_long_set(&per_cpu(active_context, cpu), cntx);187188raw_spin_unlock_irqrestore(&context_lock, flags);189190switch_mm_fast:191csr_write(CSR_SATP, virt_to_pfn(mm->pgd) |192(cntx2asid(cntx) << SATP_ASID_SHIFT) |193satp_mode);194195if (need_flush_tlb)196local_flush_tlb_all();197}198199static void set_mm_noasid(struct mm_struct *mm)200{201/* Switch the page table and blindly nuke entire local TLB */202csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | satp_mode);203local_flush_tlb_all_asid(0);204}205206static inline void set_mm(struct mm_struct *prev,207struct mm_struct *next, unsigned int cpu)208{209/*210* The mm_cpumask indicates which harts' TLBs contain the virtual211* address mapping of the mm. Compared to noasid, using asid212* can't guarantee that stale TLB entries are invalidated because213* the asid mechanism wouldn't flush TLB for every switch_mm for214* performance. So when using asid, keep all CPUs footmarks in215* cpumask() until mm reset.216*/217cpumask_set_cpu(cpu, mm_cpumask(next));218if (static_branch_unlikely(&use_asid_allocator)) {219set_mm_asid(next, cpu);220} else {221cpumask_clear_cpu(cpu, mm_cpumask(prev));222set_mm_noasid(next);223}224}225226static int __init asids_init(void)227{228unsigned long asid_bits, old;229230/* Figure-out number of ASID bits in HW */231old = csr_read(CSR_SATP);232asid_bits = old | (SATP_ASID_MASK << SATP_ASID_SHIFT);233csr_write(CSR_SATP, asid_bits);234asid_bits = (csr_read(CSR_SATP) >> SATP_ASID_SHIFT) & SATP_ASID_MASK;235asid_bits = fls_long(asid_bits);236csr_write(CSR_SATP, old);237238/*239* In the process of determining number of ASID bits (above)240* we polluted the TLB of current HART so let's do TLB flushed241* to remove unwanted TLB enteries.242*/243local_flush_tlb_all();244245/* Pre-compute ASID details */246if (asid_bits) {247num_asids = 1 << asid_bits;248}249250/*251* Use ASID allocator only if number of HW ASIDs are252* at-least twice more than CPUs253*/254if (num_asids > (2 * num_possible_cpus())) {255atomic_long_set(¤t_version, BIT(SATP_ASID_BITS));256257context_asid_map = bitmap_zalloc(num_asids, GFP_KERNEL);258if (!context_asid_map)259panic("Failed to allocate bitmap for %lu ASIDs\n",260num_asids);261262__set_bit(0, context_asid_map);263264static_branch_enable(&use_asid_allocator);265266pr_info("ASID allocator using %lu bits (%lu entries)\n",267asid_bits, num_asids);268} else {269pr_info("ASID allocator disabled (%lu bits)\n", asid_bits);270}271272return 0;273}274early_initcall(asids_init);275#else276static inline void set_mm(struct mm_struct *prev,277struct mm_struct *next, unsigned int cpu)278{279/* Nothing to do here when there is no MMU */280}281#endif282283/*284* When necessary, performs a deferred icache flush for the given MM context,285* on the local CPU. RISC-V has no direct mechanism for instruction cache286* shoot downs, so instead we send an IPI that informs the remote harts they287* need to flush their local instruction caches. To avoid pathologically slow288* behavior in a common case (a bunch of single-hart processes on a many-hart289* machine, ie 'make -j') we avoid the IPIs for harts that are not currently290* executing a MM context and instead schedule a deferred local instruction291* cache flush to be performed before execution resumes on each hart. This292* actually performs that local instruction cache flush, which implicitly only293* refers to the current hart.294*295* The "cpu" argument must be the current local CPU number.296*/297static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu,298struct task_struct *task)299{300#ifdef CONFIG_SMP301if (cpumask_test_and_clear_cpu(cpu, &mm->context.icache_stale_mask)) {302/*303* Ensure the remote hart's writes are visible to this hart.304* This pairs with a barrier in flush_icache_mm.305*/306smp_mb();307308/*309* If cache will be flushed in switch_to, no need to flush here.310*/311if (!(task && switch_to_should_flush_icache(task)))312local_flush_icache_all();313}314#endif315}316317void switch_mm(struct mm_struct *prev, struct mm_struct *next,318struct task_struct *task)319{320unsigned int cpu;321322if (unlikely(prev == next))323return;324325membarrier_arch_switch_mm(prev, next, task);326327/*328* Mark the current MM context as inactive, and the next as329* active. This is at least used by the icache flushing330* routines in order to determine who should be flushed.331*/332cpu = smp_processor_id();333334set_mm(prev, next, cpu);335336flush_icache_deferred(next, cpu, task);337}338339340