162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * vineetg: May 2011 662306a36Sopenharmony_ci * -Refactored get_new_mmu_context( ) to only handle live-mm. 762306a36Sopenharmony_ci * retiring-mm handled in other hooks 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Vineetg: March 25th, 2008: Bug #92690 1062306a36Sopenharmony_ci * -Major rewrite of Core ASID allocation routine get_new_mmu_context 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Amit Bhor, Sameer Dhavale: Codito Technologies 2004 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#ifndef _ASM_ARC_MMU_CONTEXT_H 1662306a36Sopenharmony_ci#define _ASM_ARC_MMU_CONTEXT_H 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#include <linux/sched/mm.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#include <asm/tlb.h> 2162306a36Sopenharmony_ci#include <asm-generic/mm_hooks.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* ARC ASID Management 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * MMU tags TLBs with an 8-bit ASID, avoiding need to flush the TLB on 2662306a36Sopenharmony_ci * context-switch. 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * ASID is managed per cpu, so task threads across CPUs can have different 2962306a36Sopenharmony_ci * ASID. Global ASID management is needed if hardware supports TLB shootdown 3062306a36Sopenharmony_ci * and/or shared TLB across cores, which ARC doesn't. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * Each task is assigned unique ASID, with a simple round-robin allocator 3362306a36Sopenharmony_ci * tracked in @asid_cpu. When 8-bit value rolls over,a new cycle is started 3462306a36Sopenharmony_ci * over from 0, and TLB is flushed 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * A new allocation cycle, post rollover, could potentially reassign an ASID 3762306a36Sopenharmony_ci * to a different task. Thus the rule is to refresh the ASID in a new cycle. 3862306a36Sopenharmony_ci * The 32 bit @asid_cpu (and mm->asid) have 8 bits MMU PID and rest 24 bits 3962306a36Sopenharmony_ci * serve as cycle/generation indicator and natural 32 bit unsigned math 4062306a36Sopenharmony_ci * automagically increments the generation when lower 8 bits rollover. 4162306a36Sopenharmony_ci */ 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci#define MM_CTXT_ASID_MASK 0x000000ff /* MMU PID reg :8 bit PID */ 4462306a36Sopenharmony_ci#define MM_CTXT_CYCLE_MASK (~MM_CTXT_ASID_MASK) 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci#define MM_CTXT_FIRST_CYCLE (MM_CTXT_ASID_MASK + 1) 4762306a36Sopenharmony_ci#define MM_CTXT_NO_ASID 0UL 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci#define asid_mm(mm, cpu) mm->context.asid[cpu] 5062306a36Sopenharmony_ci#define hw_pid(mm, cpu) (asid_mm(mm, cpu) & MM_CTXT_ASID_MASK) 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciDECLARE_PER_CPU(unsigned int, asid_cache); 5362306a36Sopenharmony_ci#define asid_cpu(cpu) per_cpu(asid_cache, cpu) 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/* 5662306a36Sopenharmony_ci * Get a new ASID if task doesn't have a valid one (unalloc or from prev cycle) 5762306a36Sopenharmony_ci * Also set the MMU PID register to existing/updated ASID 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_cistatic inline void get_new_mmu_context(struct mm_struct *mm) 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci const unsigned int cpu = smp_processor_id(); 6262306a36Sopenharmony_ci unsigned long flags; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci local_irq_save(flags); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci /* 6762306a36Sopenharmony_ci * Move to new ASID if it was not from current alloc-cycle/generation. 6862306a36Sopenharmony_ci * This is done by ensuring that the generation bits in both mm->ASID 6962306a36Sopenharmony_ci * and cpu's ASID counter are exactly same. 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * Note: Callers needing new ASID unconditionally, independent of 7262306a36Sopenharmony_ci * generation, e.g. local_flush_tlb_mm() for forking parent, 7362306a36Sopenharmony_ci * first need to destroy the context, setting it to invalid 7462306a36Sopenharmony_ci * value. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_ci if (!((asid_mm(mm, cpu) ^ asid_cpu(cpu)) & MM_CTXT_CYCLE_MASK)) 7762306a36Sopenharmony_ci goto set_hw; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci /* move to new ASID and handle rollover */ 8062306a36Sopenharmony_ci if (unlikely(!(++asid_cpu(cpu) & MM_CTXT_ASID_MASK))) { 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci local_flush_tlb_all(); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci /* 8562306a36Sopenharmony_ci * Above check for rollover of 8 bit ASID in 32 bit container. 8662306a36Sopenharmony_ci * If the container itself wrapped around, set it to a non zero 8762306a36Sopenharmony_ci * "generation" to distinguish from no context 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_ci if (!asid_cpu(cpu)) 9062306a36Sopenharmony_ci asid_cpu(cpu) = MM_CTXT_FIRST_CYCLE; 9162306a36Sopenharmony_ci } 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci /* Assign new ASID to tsk */ 9462306a36Sopenharmony_ci asid_mm(mm, cpu) = asid_cpu(cpu); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciset_hw: 9762306a36Sopenharmony_ci mmu_setup_asid(mm, hw_pid(mm, cpu)); 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci local_irq_restore(flags); 10062306a36Sopenharmony_ci} 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci/* 10362306a36Sopenharmony_ci * Initialize the context related info for a new mm_struct 10462306a36Sopenharmony_ci * instance. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_ci#define init_new_context init_new_context 10762306a36Sopenharmony_cistatic inline int 10862306a36Sopenharmony_ciinit_new_context(struct task_struct *tsk, struct mm_struct *mm) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci int i; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci for_each_possible_cpu(i) 11362306a36Sopenharmony_ci asid_mm(mm, i) = MM_CTXT_NO_ASID; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci return 0; 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci#define destroy_context destroy_context 11962306a36Sopenharmony_cistatic inline void destroy_context(struct mm_struct *mm) 12062306a36Sopenharmony_ci{ 12162306a36Sopenharmony_ci unsigned long flags; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci /* Needed to elide CONFIG_DEBUG_PREEMPT warning */ 12462306a36Sopenharmony_ci local_irq_save(flags); 12562306a36Sopenharmony_ci asid_mm(mm, smp_processor_id()) = MM_CTXT_NO_ASID; 12662306a36Sopenharmony_ci local_irq_restore(flags); 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci/* Prepare the MMU for task: setup PID reg with allocated ASID 13062306a36Sopenharmony_ci If task doesn't have an ASID (never alloc or stolen, get a new ASID) 13162306a36Sopenharmony_ci*/ 13262306a36Sopenharmony_cistatic inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 13362306a36Sopenharmony_ci struct task_struct *tsk) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci const int cpu = smp_processor_id(); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci /* 13862306a36Sopenharmony_ci * Note that the mm_cpumask is "aggregating" only, we don't clear it 13962306a36Sopenharmony_ci * for the switched-out task, unlike some other arches. 14062306a36Sopenharmony_ci * It is used to enlist cpus for sending TLB flush IPIs and not sending 14162306a36Sopenharmony_ci * it to CPUs where a task once ran-on, could cause stale TLB entry 14262306a36Sopenharmony_ci * re-use, specially for a multi-threaded task. 14362306a36Sopenharmony_ci * e.g. T1 runs on C1, migrates to C3. T2 running on C2 munmaps. 14462306a36Sopenharmony_ci * For a non-aggregating mm_cpumask, IPI not sent C1, and if T1 14562306a36Sopenharmony_ci * were to re-migrate to C1, it could access the unmapped region 14662306a36Sopenharmony_ci * via any existing stale TLB entries. 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_ci cpumask_set_cpu(cpu, mm_cpumask(next)); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci mmu_setup_pgd(next, next->pgd); 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci get_new_mmu_context(next); 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/* 15662306a36Sopenharmony_ci * activate_mm defaults (in asm-generic) to switch_mm and is called at the 15762306a36Sopenharmony_ci * time of execve() to get a new ASID Note the subtlety here: 15862306a36Sopenharmony_ci * get_new_mmu_context() behaves differently here vs. in switch_mm(). Here 15962306a36Sopenharmony_ci * it always returns a new ASID, because mm has an unallocated "initial" 16062306a36Sopenharmony_ci * value, while in latter, it moves to a new ASID, only if it was 16162306a36Sopenharmony_ci * unallocated 16262306a36Sopenharmony_ci */ 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci/* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping 16562306a36Sopenharmony_ci * for retiring-mm. However destroy_context( ) still needs to do that because 16662306a36Sopenharmony_ci * between mm_release( ) = >deactive_mm( ) and 16762306a36Sopenharmony_ci * mmput => .. => __mmdrop( ) => destroy_context( ) 16862306a36Sopenharmony_ci * there is a good chance that task gets sched-out/in, making it's ASID valid 16962306a36Sopenharmony_ci * again (this teased me for a whole day). 17062306a36Sopenharmony_ci */ 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci#include <asm-generic/mmu_context.h> 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci#endif /* __ASM_ARC_MMU_CONTEXT_H */ 175