xref: /openbmc/linux/arch/riscv/mm/context.c (revision ca2478a7d974f38d29d27acb42a952c7f168916e)
1f6635f87SGary Guo // SPDX-License-Identifier: GPL-2.0
2f6635f87SGary Guo /*
3f6635f87SGary Guo  * Copyright (C) 2012 Regents of the University of California
4f6635f87SGary Guo  * Copyright (C) 2017 SiFive
565d4b9c5SAnup Patel  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
6f6635f87SGary Guo  */
7f6635f87SGary Guo 
865d4b9c5SAnup Patel #include <linux/bitops.h>
965d4b9c5SAnup Patel #include <linux/cpumask.h>
10f6635f87SGary Guo #include <linux/mm.h>
1165d4b9c5SAnup Patel #include <linux/percpu.h>
1265d4b9c5SAnup Patel #include <linux/slab.h>
1365d4b9c5SAnup Patel #include <linux/spinlock.h>
1465d4b9c5SAnup Patel #include <linux/static_key.h>
15f6635f87SGary Guo #include <asm/tlbflush.h>
16f6635f87SGary Guo #include <asm/cacheflush.h>
175ed881bcSPaul Walmsley #include <asm/mmu_context.h>
18f6635f87SGary Guo 
1965d4b9c5SAnup Patel #ifdef CONFIG_MMU
2065d4b9c5SAnup Patel 
213f1e7829SGuo Ren DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
2265d4b9c5SAnup Patel 
2365d4b9c5SAnup Patel static unsigned long asid_bits;
2465d4b9c5SAnup Patel static unsigned long num_asids;
259a801afdSDylan Jhong unsigned long asid_mask;
2665d4b9c5SAnup Patel 
2765d4b9c5SAnup Patel static atomic_long_t current_version;
2865d4b9c5SAnup Patel 
2965d4b9c5SAnup Patel static DEFINE_RAW_SPINLOCK(context_lock);
3065d4b9c5SAnup Patel static cpumask_t context_tlb_flush_pending;
3165d4b9c5SAnup Patel static unsigned long *context_asid_map;
3265d4b9c5SAnup Patel 
3365d4b9c5SAnup Patel static DEFINE_PER_CPU(atomic_long_t, active_context);
3465d4b9c5SAnup Patel static DEFINE_PER_CPU(unsigned long, reserved_context);
3565d4b9c5SAnup Patel 
check_update_reserved_context(unsigned long cntx,unsigned long newcntx)3665d4b9c5SAnup Patel static bool check_update_reserved_context(unsigned long cntx,
3765d4b9c5SAnup Patel 					  unsigned long newcntx)
3865d4b9c5SAnup Patel {
3965d4b9c5SAnup Patel 	int cpu;
4065d4b9c5SAnup Patel 	bool hit = false;
4165d4b9c5SAnup Patel 
4265d4b9c5SAnup Patel 	/*
4365d4b9c5SAnup Patel 	 * Iterate over the set of reserved CONTEXT looking for a match.
4465d4b9c5SAnup Patel 	 * If we find one, then we can update our mm to use new CONTEXT
4565d4b9c5SAnup Patel 	 * (i.e. the same CONTEXT in the current_version) but we can't
4665d4b9c5SAnup Patel 	 * exit the loop early, since we need to ensure that all copies
4765d4b9c5SAnup Patel 	 * of the old CONTEXT are updated to reflect the mm. Failure to do
4865d4b9c5SAnup Patel 	 * so could result in us missing the reserved CONTEXT in a future
4965d4b9c5SAnup Patel 	 * version.
5065d4b9c5SAnup Patel 	 */
5165d4b9c5SAnup Patel 	for_each_possible_cpu(cpu) {
5265d4b9c5SAnup Patel 		if (per_cpu(reserved_context, cpu) == cntx) {
5365d4b9c5SAnup Patel 			hit = true;
5465d4b9c5SAnup Patel 			per_cpu(reserved_context, cpu) = newcntx;
5565d4b9c5SAnup Patel 		}
5665d4b9c5SAnup Patel 	}
5765d4b9c5SAnup Patel 
5865d4b9c5SAnup Patel 	return hit;
5965d4b9c5SAnup Patel }
6065d4b9c5SAnup Patel 
__flush_context(void)6165d4b9c5SAnup Patel static void __flush_context(void)
6265d4b9c5SAnup Patel {
6365d4b9c5SAnup Patel 	int i;
6465d4b9c5SAnup Patel 	unsigned long cntx;
6565d4b9c5SAnup Patel 
6665d4b9c5SAnup Patel 	/* Must be called with context_lock held */
6765d4b9c5SAnup Patel 	lockdep_assert_held(&context_lock);
6865d4b9c5SAnup Patel 
6965d4b9c5SAnup Patel 	/* Update the list of reserved ASIDs and the ASID bitmap. */
70665c51f6SYe Xingchen 	bitmap_zero(context_asid_map, num_asids);
7165d4b9c5SAnup Patel 
7265d4b9c5SAnup Patel 	/* Mark already active ASIDs as used */
7365d4b9c5SAnup Patel 	for_each_possible_cpu(i) {
7465d4b9c5SAnup Patel 		cntx = atomic_long_xchg_relaxed(&per_cpu(active_context, i), 0);
7565d4b9c5SAnup Patel 		/*
7665d4b9c5SAnup Patel 		 * If this CPU has already been through a rollover, but
7765d4b9c5SAnup Patel 		 * hasn't run another task in the meantime, we must preserve
7865d4b9c5SAnup Patel 		 * its reserved CONTEXT, as this is the only trace we have of
7965d4b9c5SAnup Patel 		 * the process it is still running.
8065d4b9c5SAnup Patel 		 */
8165d4b9c5SAnup Patel 		if (cntx == 0)
8265d4b9c5SAnup Patel 			cntx = per_cpu(reserved_context, i);
8365d4b9c5SAnup Patel 
8465d4b9c5SAnup Patel 		__set_bit(cntx & asid_mask, context_asid_map);
8565d4b9c5SAnup Patel 		per_cpu(reserved_context, i) = cntx;
8665d4b9c5SAnup Patel 	}
8765d4b9c5SAnup Patel 
8865d4b9c5SAnup Patel 	/* Mark ASID #0 as used because it is used at boot-time */
8965d4b9c5SAnup Patel 	__set_bit(0, context_asid_map);
9065d4b9c5SAnup Patel 
9165d4b9c5SAnup Patel 	/* Queue a TLB invalidation for each CPU on next context-switch */
9265d4b9c5SAnup Patel 	cpumask_setall(&context_tlb_flush_pending);
9365d4b9c5SAnup Patel }
9465d4b9c5SAnup Patel 
__new_context(struct mm_struct * mm)9565d4b9c5SAnup Patel static unsigned long __new_context(struct mm_struct *mm)
9665d4b9c5SAnup Patel {
9765d4b9c5SAnup Patel 	static u32 cur_idx = 1;
9865d4b9c5SAnup Patel 	unsigned long cntx = atomic_long_read(&mm->context.id);
9965d4b9c5SAnup Patel 	unsigned long asid, ver = atomic_long_read(&current_version);
10065d4b9c5SAnup Patel 
10165d4b9c5SAnup Patel 	/* Must be called with context_lock held */
10265d4b9c5SAnup Patel 	lockdep_assert_held(&context_lock);
10365d4b9c5SAnup Patel 
10465d4b9c5SAnup Patel 	if (cntx != 0) {
10565d4b9c5SAnup Patel 		unsigned long newcntx = ver | (cntx & asid_mask);
10665d4b9c5SAnup Patel 
10765d4b9c5SAnup Patel 		/*
10865d4b9c5SAnup Patel 		 * If our current CONTEXT was active during a rollover, we
10965d4b9c5SAnup Patel 		 * can continue to use it and this was just a false alarm.
11065d4b9c5SAnup Patel 		 */
11165d4b9c5SAnup Patel 		if (check_update_reserved_context(cntx, newcntx))
11265d4b9c5SAnup Patel 			return newcntx;
11365d4b9c5SAnup Patel 
11465d4b9c5SAnup Patel 		/*
11565d4b9c5SAnup Patel 		 * We had a valid CONTEXT in a previous life, so try to
11665d4b9c5SAnup Patel 		 * re-use it if possible.
11765d4b9c5SAnup Patel 		 */
11865d4b9c5SAnup Patel 		if (!__test_and_set_bit(cntx & asid_mask, context_asid_map))
11965d4b9c5SAnup Patel 			return newcntx;
12065d4b9c5SAnup Patel 	}
12165d4b9c5SAnup Patel 
12265d4b9c5SAnup Patel 	/*
12365d4b9c5SAnup Patel 	 * Allocate a free ASID. If we can't find one then increment
12465d4b9c5SAnup Patel 	 * current_version and flush all ASIDs.
12565d4b9c5SAnup Patel 	 */
12665d4b9c5SAnup Patel 	asid = find_next_zero_bit(context_asid_map, num_asids, cur_idx);
12765d4b9c5SAnup Patel 	if (asid != num_asids)
12865d4b9c5SAnup Patel 		goto set_asid;
12965d4b9c5SAnup Patel 
13065d4b9c5SAnup Patel 	/* We're out of ASIDs, so increment current_version */
13165d4b9c5SAnup Patel 	ver = atomic_long_add_return_relaxed(num_asids, &current_version);
13265d4b9c5SAnup Patel 
13365d4b9c5SAnup Patel 	/* Flush everything  */
13465d4b9c5SAnup Patel 	__flush_context();
13565d4b9c5SAnup Patel 
13665d4b9c5SAnup Patel 	/* We have more ASIDs than CPUs, so this will always succeed */
13765d4b9c5SAnup Patel 	asid = find_next_zero_bit(context_asid_map, num_asids, 1);
13865d4b9c5SAnup Patel 
13965d4b9c5SAnup Patel set_asid:
14065d4b9c5SAnup Patel 	__set_bit(asid, context_asid_map);
14165d4b9c5SAnup Patel 	cur_idx = asid;
14265d4b9c5SAnup Patel 	return asid | ver;
14365d4b9c5SAnup Patel }
14465d4b9c5SAnup Patel 
set_mm_asid(struct mm_struct * mm,unsigned int cpu)14565d4b9c5SAnup Patel static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
14665d4b9c5SAnup Patel {
14765d4b9c5SAnup Patel 	unsigned long flags;
14865d4b9c5SAnup Patel 	bool need_flush_tlb = false;
14965d4b9c5SAnup Patel 	unsigned long cntx, old_active_cntx;
15065d4b9c5SAnup Patel 
15165d4b9c5SAnup Patel 	cntx = atomic_long_read(&mm->context.id);
15265d4b9c5SAnup Patel 
15365d4b9c5SAnup Patel 	/*
15465d4b9c5SAnup Patel 	 * If our active_context is non-zero and the context matches the
15565d4b9c5SAnup Patel 	 * current_version, then we update the active_context entry with a
15665d4b9c5SAnup Patel 	 * relaxed cmpxchg.
15765d4b9c5SAnup Patel 	 *
15865d4b9c5SAnup Patel 	 * Following is how we handle racing with a concurrent rollover:
15965d4b9c5SAnup Patel 	 *
16065d4b9c5SAnup Patel 	 * - We get a zero back from the cmpxchg and end up waiting on the
16165d4b9c5SAnup Patel 	 *   lock. Taking the lock synchronises with the rollover and so
16265d4b9c5SAnup Patel 	 *   we are forced to see the updated verion.
16365d4b9c5SAnup Patel 	 *
16465d4b9c5SAnup Patel 	 * - We get a valid context back from the cmpxchg then we continue
16565d4b9c5SAnup Patel 	 *   using old ASID because __flush_context() would have marked ASID
16665d4b9c5SAnup Patel 	 *   of active_context as used and next context switch we will
16765d4b9c5SAnup Patel 	 *   allocate new context.
16865d4b9c5SAnup Patel 	 */
16965d4b9c5SAnup Patel 	old_active_cntx = atomic_long_read(&per_cpu(active_context, cpu));
17065d4b9c5SAnup Patel 	if (old_active_cntx &&
17165d4b9c5SAnup Patel 	    ((cntx & ~asid_mask) == atomic_long_read(&current_version)) &&
17265d4b9c5SAnup Patel 	    atomic_long_cmpxchg_relaxed(&per_cpu(active_context, cpu),
17365d4b9c5SAnup Patel 					old_active_cntx, cntx))
17465d4b9c5SAnup Patel 		goto switch_mm_fast;
17565d4b9c5SAnup Patel 
17665d4b9c5SAnup Patel 	raw_spin_lock_irqsave(&context_lock, flags);
17765d4b9c5SAnup Patel 
17865d4b9c5SAnup Patel 	/* Check that our ASID belongs to the current_version. */
17965d4b9c5SAnup Patel 	cntx = atomic_long_read(&mm->context.id);
18065d4b9c5SAnup Patel 	if ((cntx & ~asid_mask) != atomic_long_read(&current_version)) {
18165d4b9c5SAnup Patel 		cntx = __new_context(mm);
18265d4b9c5SAnup Patel 		atomic_long_set(&mm->context.id, cntx);
18365d4b9c5SAnup Patel 	}
18465d4b9c5SAnup Patel 
18565d4b9c5SAnup Patel 	if (cpumask_test_and_clear_cpu(cpu, &context_tlb_flush_pending))
18665d4b9c5SAnup Patel 		need_flush_tlb = true;
18765d4b9c5SAnup Patel 
18865d4b9c5SAnup Patel 	atomic_long_set(&per_cpu(active_context, cpu), cntx);
18965d4b9c5SAnup Patel 
19065d4b9c5SAnup Patel 	raw_spin_unlock_irqrestore(&context_lock, flags);
19165d4b9c5SAnup Patel 
19265d4b9c5SAnup Patel switch_mm_fast:
19365d4b9c5SAnup Patel 	csr_write(CSR_SATP, virt_to_pfn(mm->pgd) |
19465d4b9c5SAnup Patel 		  ((cntx & asid_mask) << SATP_ASID_SHIFT) |
195e8a62cc2SAlexandre Ghiti 		  satp_mode);
19665d4b9c5SAnup Patel 
19765d4b9c5SAnup Patel 	if (need_flush_tlb)
19865d4b9c5SAnup Patel 		local_flush_tlb_all();
19965d4b9c5SAnup Patel }
20065d4b9c5SAnup Patel 
set_mm_noasid(struct mm_struct * mm)20165d4b9c5SAnup Patel static void set_mm_noasid(struct mm_struct *mm)
20265d4b9c5SAnup Patel {
20365d4b9c5SAnup Patel 	/* Switch the page table and blindly nuke entire local TLB */
204e8a62cc2SAlexandre Ghiti 	csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | satp_mode);
20565d4b9c5SAnup Patel 	local_flush_tlb_all();
20665d4b9c5SAnup Patel }
20765d4b9c5SAnup Patel 
set_mm(struct mm_struct * prev,struct mm_struct * next,unsigned int cpu)20882dd33fdSGuo Ren static inline void set_mm(struct mm_struct *prev,
20982dd33fdSGuo Ren 			  struct mm_struct *next, unsigned int cpu)
21065d4b9c5SAnup Patel {
21182dd33fdSGuo Ren 	/*
21282dd33fdSGuo Ren 	 * The mm_cpumask indicates which harts' TLBs contain the virtual
21382dd33fdSGuo Ren 	 * address mapping of the mm. Compared to noasid, using asid
21482dd33fdSGuo Ren 	 * can't guarantee that stale TLB entries are invalidated because
21582dd33fdSGuo Ren 	 * the asid mechanism wouldn't flush TLB for every switch_mm for
21682dd33fdSGuo Ren 	 * performance. So when using asid, keep all CPUs footmarks in
21782dd33fdSGuo Ren 	 * cpumask() until mm reset.
21882dd33fdSGuo Ren 	 */
21982dd33fdSGuo Ren 	cpumask_set_cpu(cpu, mm_cpumask(next));
22082dd33fdSGuo Ren 	if (static_branch_unlikely(&use_asid_allocator)) {
22182dd33fdSGuo Ren 		set_mm_asid(next, cpu);
22282dd33fdSGuo Ren 	} else {
22382dd33fdSGuo Ren 		cpumask_clear_cpu(cpu, mm_cpumask(prev));
22482dd33fdSGuo Ren 		set_mm_noasid(next);
22582dd33fdSGuo Ren 	}
22665d4b9c5SAnup Patel }
22765d4b9c5SAnup Patel 
asids_init(void)2283df952aeSJisheng Zhang static int __init asids_init(void)
22965d4b9c5SAnup Patel {
23065d4b9c5SAnup Patel 	unsigned long old;
23165d4b9c5SAnup Patel 
23265d4b9c5SAnup Patel 	/* Figure-out number of ASID bits in HW */
23365d4b9c5SAnup Patel 	old = csr_read(CSR_SATP);
23465d4b9c5SAnup Patel 	asid_bits = old | (SATP_ASID_MASK << SATP_ASID_SHIFT);
23565d4b9c5SAnup Patel 	csr_write(CSR_SATP, asid_bits);
23665d4b9c5SAnup Patel 	asid_bits = (csr_read(CSR_SATP) >> SATP_ASID_SHIFT)  & SATP_ASID_MASK;
23765d4b9c5SAnup Patel 	asid_bits = fls_long(asid_bits);
23865d4b9c5SAnup Patel 	csr_write(CSR_SATP, old);
23965d4b9c5SAnup Patel 
24065d4b9c5SAnup Patel 	/*
24165d4b9c5SAnup Patel 	 * In the process of determining number of ASID bits (above)
24265d4b9c5SAnup Patel 	 * we polluted the TLB of current HART so let's do TLB flushed
24365d4b9c5SAnup Patel 	 * to remove unwanted TLB enteries.
24465d4b9c5SAnup Patel 	 */
24565d4b9c5SAnup Patel 	local_flush_tlb_all();
24665d4b9c5SAnup Patel 
24765d4b9c5SAnup Patel 	/* Pre-compute ASID details */
24821ccdccdSVineet Gupta 	if (asid_bits) {
24965d4b9c5SAnup Patel 		num_asids = 1 << asid_bits;
25065d4b9c5SAnup Patel 		asid_mask = num_asids - 1;
25121ccdccdSVineet Gupta 	}
25265d4b9c5SAnup Patel 
25365d4b9c5SAnup Patel 	/*
25465d4b9c5SAnup Patel 	 * Use ASID allocator only if number of HW ASIDs are
25565d4b9c5SAnup Patel 	 * at-least twice more than CPUs
25665d4b9c5SAnup Patel 	 */
25765d4b9c5SAnup Patel 	if (num_asids > (2 * num_possible_cpus())) {
25865d4b9c5SAnup Patel 		atomic_long_set(&current_version, num_asids);
25965d4b9c5SAnup Patel 
2605def4429SKefeng Wang 		context_asid_map = bitmap_zalloc(num_asids, GFP_KERNEL);
26165d4b9c5SAnup Patel 		if (!context_asid_map)
26265d4b9c5SAnup Patel 			panic("Failed to allocate bitmap for %lu ASIDs\n",
26365d4b9c5SAnup Patel 			      num_asids);
26465d4b9c5SAnup Patel 
26565d4b9c5SAnup Patel 		__set_bit(0, context_asid_map);
26665d4b9c5SAnup Patel 
26765d4b9c5SAnup Patel 		static_branch_enable(&use_asid_allocator);
26865d4b9c5SAnup Patel 
26965d4b9c5SAnup Patel 		pr_info("ASID allocator using %lu bits (%lu entries)\n",
27065d4b9c5SAnup Patel 			asid_bits, num_asids);
27165d4b9c5SAnup Patel 	} else {
27221ccdccdSVineet Gupta 		pr_info("ASID allocator disabled (%lu bits)\n", asid_bits);
27365d4b9c5SAnup Patel 	}
27465d4b9c5SAnup Patel 
27565d4b9c5SAnup Patel 	return 0;
27665d4b9c5SAnup Patel }
27765d4b9c5SAnup Patel early_initcall(asids_init);
27865d4b9c5SAnup Patel #else
set_mm(struct mm_struct * prev,struct mm_struct * next,unsigned int cpu)27982dd33fdSGuo Ren static inline void set_mm(struct mm_struct *prev,
28082dd33fdSGuo Ren 			  struct mm_struct *next, unsigned int cpu)
28165d4b9c5SAnup Patel {
28265d4b9c5SAnup Patel 	/* Nothing to do here when there is no MMU */
28365d4b9c5SAnup Patel }
28465d4b9c5SAnup Patel #endif
28565d4b9c5SAnup Patel 
286f6635f87SGary Guo /*
287f6635f87SGary Guo  * When necessary, performs a deferred icache flush for the given MM context,
288f6635f87SGary Guo  * on the local CPU.  RISC-V has no direct mechanism for instruction cache
289f6635f87SGary Guo  * shoot downs, so instead we send an IPI that informs the remote harts they
290f6635f87SGary Guo  * need to flush their local instruction caches.  To avoid pathologically slow
291f6635f87SGary Guo  * behavior in a common case (a bunch of single-hart processes on a many-hart
292f6635f87SGary Guo  * machine, ie 'make -j') we avoid the IPIs for harts that are not currently
293f6635f87SGary Guo  * executing a MM context and instead schedule a deferred local instruction
294f6635f87SGary Guo  * cache flush to be performed before execution resumes on each hart.  This
295f6635f87SGary Guo  * actually performs that local instruction cache flush, which implicitly only
296f6635f87SGary Guo  * refers to the current hart.
2978237c524SJisheng Zhang  *
2988237c524SJisheng Zhang  * The "cpu" argument must be the current local CPU number.
299f6635f87SGary Guo  */
flush_icache_deferred(struct mm_struct * mm,unsigned int cpu)3008237c524SJisheng Zhang static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu)
301f6635f87SGary Guo {
302f6635f87SGary Guo #ifdef CONFIG_SMP
303f6635f87SGary Guo 	cpumask_t *mask = &mm->context.icache_stale_mask;
304f6635f87SGary Guo 
305f6635f87SGary Guo 	if (cpumask_test_cpu(cpu, mask)) {
306f6635f87SGary Guo 		cpumask_clear_cpu(cpu, mask);
307f6635f87SGary Guo 		/*
308f6635f87SGary Guo 		 * Ensure the remote hart's writes are visible to this hart.
309f6635f87SGary Guo 		 * This pairs with a barrier in flush_icache_mm.
310f6635f87SGary Guo 		 */
311f6635f87SGary Guo 		smp_mb();
312f6635f87SGary Guo 		local_flush_icache_all();
313f6635f87SGary Guo 	}
314f6635f87SGary Guo 
315f6635f87SGary Guo #endif
316f6635f87SGary Guo }
317f6635f87SGary Guo 
switch_mm(struct mm_struct * prev,struct mm_struct * next,struct task_struct * task)318f6635f87SGary Guo void switch_mm(struct mm_struct *prev, struct mm_struct *next,
319f6635f87SGary Guo 	struct task_struct *task)
320f6635f87SGary Guo {
321f6635f87SGary Guo 	unsigned int cpu;
322f6635f87SGary Guo 
323f6635f87SGary Guo 	if (unlikely(prev == next))
324f6635f87SGary Guo 		return;
325f6635f87SGary Guo 
326*a2977c0cSAndrea Parri 	membarrier_arch_switch_mm(prev, next, task);
327*a2977c0cSAndrea Parri 
328f6635f87SGary Guo 	/*
329f6635f87SGary Guo 	 * Mark the current MM context as inactive, and the next as
330f6635f87SGary Guo 	 * active.  This is at least used by the icache flushing
331f6635f87SGary Guo 	 * routines in order to determine who should be flushed.
332f6635f87SGary Guo 	 */
333f6635f87SGary Guo 	cpu = smp_processor_id();
334f6635f87SGary Guo 
33582dd33fdSGuo Ren 	set_mm(prev, next, cpu);
336f6635f87SGary Guo 
3378237c524SJisheng Zhang 	flush_icache_deferred(next, cpu);
338f6635f87SGary Guo }
339