18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef __SPARC64_MMU_CONTEXT_H 38c2ecf20Sopenharmony_ci#define __SPARC64_MMU_CONTEXT_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* Derived heavily from Linus's Alpha/AXP ASN code... */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 108c2ecf20Sopenharmony_ci#include <linux/mm_types.h> 118c2ecf20Sopenharmony_ci#include <linux/smp.h> 128c2ecf20Sopenharmony_ci#include <linux/sched.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <asm/spitfire.h> 158c2ecf20Sopenharmony_ci#include <asm/adi_64.h> 168c2ecf20Sopenharmony_ci#include <asm-generic/mm_hooks.h> 178c2ecf20Sopenharmony_ci#include <asm/percpu.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci} 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciextern spinlock_t ctx_alloc_lock; 248c2ecf20Sopenharmony_ciextern unsigned long tlb_context_cache; 258c2ecf20Sopenharmony_ciextern unsigned long mmu_context_bmap[]; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ciDECLARE_PER_CPU(struct mm_struct *, per_cpu_secondary_mm); 288c2ecf20Sopenharmony_civoid get_new_mmu_context(struct mm_struct *mm); 298c2ecf20Sopenharmony_ciint init_new_context(struct task_struct *tsk, struct mm_struct *mm); 308c2ecf20Sopenharmony_civoid destroy_context(struct mm_struct *mm); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_civoid __tsb_context_switch(unsigned long pgd_pa, 338c2ecf20Sopenharmony_ci struct tsb_config *tsb_base, 348c2ecf20Sopenharmony_ci struct tsb_config *tsb_huge, 358c2ecf20Sopenharmony_ci unsigned long tsb_descr_pa, 368c2ecf20Sopenharmony_ci unsigned long secondary_ctx); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic inline void tsb_context_switch_ctx(struct mm_struct *mm, 398c2ecf20Sopenharmony_ci unsigned long ctx) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci __tsb_context_switch(__pa(mm->pgd), 428c2ecf20Sopenharmony_ci &mm->context.tsb_block[MM_TSB_BASE], 438c2ecf20Sopenharmony_ci#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE) 448c2ecf20Sopenharmony_ci (mm->context.tsb_block[MM_TSB_HUGE].tsb ? 458c2ecf20Sopenharmony_ci &mm->context.tsb_block[MM_TSB_HUGE] : 468c2ecf20Sopenharmony_ci NULL) 478c2ecf20Sopenharmony_ci#else 488c2ecf20Sopenharmony_ci NULL 498c2ecf20Sopenharmony_ci#endif 508c2ecf20Sopenharmony_ci , __pa(&mm->context.tsb_descr[MM_TSB_BASE]), 518c2ecf20Sopenharmony_ci ctx); 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define tsb_context_switch(X) tsb_context_switch_ctx(X, 0) 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_civoid tsb_grow(struct mm_struct *mm, 578c2ecf20Sopenharmony_ci unsigned long tsb_index, 588c2ecf20Sopenharmony_ci unsigned long mm_rss); 598c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 608c2ecf20Sopenharmony_civoid smp_tsb_sync(struct mm_struct *mm); 618c2ecf20Sopenharmony_ci#else 628c2ecf20Sopenharmony_ci#define smp_tsb_sync(__mm) do { } while (0) 638c2ecf20Sopenharmony_ci#endif 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* Set MMU context in the actual hardware. */ 668c2ecf20Sopenharmony_ci#define load_secondary_context(__mm) \ 678c2ecf20Sopenharmony_ci __asm__ __volatile__( \ 688c2ecf20Sopenharmony_ci "\n661: stxa %0, [%1] %2\n" \ 698c2ecf20Sopenharmony_ci " .section .sun4v_1insn_patch, \"ax\"\n" \ 708c2ecf20Sopenharmony_ci " .word 661b\n" \ 718c2ecf20Sopenharmony_ci " stxa %0, [%1] %3\n" \ 728c2ecf20Sopenharmony_ci " .previous\n" \ 738c2ecf20Sopenharmony_ci " flush %%g6\n" \ 748c2ecf20Sopenharmony_ci : /* No outputs */ \ 758c2ecf20Sopenharmony_ci : "r" (CTX_HWBITS((__mm)->context)), \ 768c2ecf20Sopenharmony_ci "r" (SECONDARY_CONTEXT), "i" (ASI_DMMU), "i" (ASI_MMU)) 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_civoid __flush_tlb_mm(unsigned long, unsigned long); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* Switch the current MM context. */ 818c2ecf20Sopenharmony_cistatic inline void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, struct task_struct *tsk) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci unsigned long ctx_valid, flags; 848c2ecf20Sopenharmony_ci int cpu = smp_processor_id(); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci per_cpu(per_cpu_secondary_mm, cpu) = mm; 878c2ecf20Sopenharmony_ci if (unlikely(mm == &init_mm)) 888c2ecf20Sopenharmony_ci return; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci spin_lock_irqsave(&mm->context.lock, flags); 918c2ecf20Sopenharmony_ci ctx_valid = CTX_VALID(mm->context); 928c2ecf20Sopenharmony_ci if (!ctx_valid) 938c2ecf20Sopenharmony_ci get_new_mmu_context(mm); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* We have to be extremely careful here or else we will miss 968c2ecf20Sopenharmony_ci * a TSB grow if we switch back and forth between a kernel 978c2ecf20Sopenharmony_ci * thread and an address space which has it's TSB size increased 988c2ecf20Sopenharmony_ci * on another processor. 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * It is possible to play some games in order to optimize the 1018c2ecf20Sopenharmony_ci * switch, but the safest thing to do is to unconditionally 1028c2ecf20Sopenharmony_ci * perform the secondary context load and the TSB context switch. 1038c2ecf20Sopenharmony_ci * 1048c2ecf20Sopenharmony_ci * For reference the bad case is, for address space "A": 1058c2ecf20Sopenharmony_ci * 1068c2ecf20Sopenharmony_ci * CPU 0 CPU 1 1078c2ecf20Sopenharmony_ci * run address space A 1088c2ecf20Sopenharmony_ci * set cpu0's bits in cpu_vm_mask 1098c2ecf20Sopenharmony_ci * switch to kernel thread, borrow 1108c2ecf20Sopenharmony_ci * address space A via entry_lazy_tlb 1118c2ecf20Sopenharmony_ci * run address space A 1128c2ecf20Sopenharmony_ci * set cpu1's bit in cpu_vm_mask 1138c2ecf20Sopenharmony_ci * flush_tlb_pending() 1148c2ecf20Sopenharmony_ci * reset cpu_vm_mask to just cpu1 1158c2ecf20Sopenharmony_ci * TSB grow 1168c2ecf20Sopenharmony_ci * run address space A 1178c2ecf20Sopenharmony_ci * context was valid, so skip 1188c2ecf20Sopenharmony_ci * TSB context switch 1198c2ecf20Sopenharmony_ci * 1208c2ecf20Sopenharmony_ci * At that point cpu0 continues to use a stale TSB, the one from 1218c2ecf20Sopenharmony_ci * before the TSB grow performed on cpu1. cpu1 did not cross-call 1228c2ecf20Sopenharmony_ci * cpu0 to update it's TSB because at that point the cpu_vm_mask 1238c2ecf20Sopenharmony_ci * only had cpu1 set in it. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ci tsb_context_switch_ctx(mm, CTX_HWBITS(mm->context)); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* Any time a processor runs a context on an address space 1288c2ecf20Sopenharmony_ci * for the first time, we must flush that context out of the 1298c2ecf20Sopenharmony_ci * local TLB. 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_ci if (!ctx_valid || !cpumask_test_cpu(cpu, mm_cpumask(mm))) { 1328c2ecf20Sopenharmony_ci cpumask_set_cpu(cpu, mm_cpumask(mm)); 1338c2ecf20Sopenharmony_ci __flush_tlb_mm(CTX_HWBITS(mm->context), 1348c2ecf20Sopenharmony_ci SECONDARY_CONTEXT); 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&mm->context.lock, flags); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci#define deactivate_mm(tsk,mm) do { } while (0) 1408c2ecf20Sopenharmony_ci#define activate_mm(active_mm, mm) switch_mm(active_mm, mm, NULL) 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci#define __HAVE_ARCH_START_CONTEXT_SWITCH 1438c2ecf20Sopenharmony_cistatic inline void arch_start_context_switch(struct task_struct *prev) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci /* Save the current state of MCDPER register for the process 1468c2ecf20Sopenharmony_ci * we are switching from 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci if (adi_capable()) { 1498c2ecf20Sopenharmony_ci register unsigned long tmp_mcdper; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci __asm__ __volatile__( 1528c2ecf20Sopenharmony_ci ".word 0x83438000\n\t" /* rd %mcdper, %g1 */ 1538c2ecf20Sopenharmony_ci "mov %%g1, %0\n\t" 1548c2ecf20Sopenharmony_ci : "=r" (tmp_mcdper) 1558c2ecf20Sopenharmony_ci : 1568c2ecf20Sopenharmony_ci : "g1"); 1578c2ecf20Sopenharmony_ci if (tmp_mcdper) 1588c2ecf20Sopenharmony_ci set_tsk_thread_flag(prev, TIF_MCDPER); 1598c2ecf20Sopenharmony_ci else 1608c2ecf20Sopenharmony_ci clear_tsk_thread_flag(prev, TIF_MCDPER); 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci#define finish_arch_post_lock_switch finish_arch_post_lock_switch 1658c2ecf20Sopenharmony_cistatic inline void finish_arch_post_lock_switch(void) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci /* Restore the state of MCDPER register for the new process 1688c2ecf20Sopenharmony_ci * just switched to. 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_ci if (adi_capable()) { 1718c2ecf20Sopenharmony_ci register unsigned long tmp_mcdper; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci tmp_mcdper = test_thread_flag(TIF_MCDPER); 1748c2ecf20Sopenharmony_ci __asm__ __volatile__( 1758c2ecf20Sopenharmony_ci "mov %0, %%g1\n\t" 1768c2ecf20Sopenharmony_ci ".word 0x9d800001\n\t" /* wr %g0, %g1, %mcdper" */ 1778c2ecf20Sopenharmony_ci ".word 0xaf902001\n\t" /* wrpr %g0, 1, %pmcdper */ 1788c2ecf20Sopenharmony_ci : 1798c2ecf20Sopenharmony_ci : "ir" (tmp_mcdper) 1808c2ecf20Sopenharmony_ci : "g1"); 1818c2ecf20Sopenharmony_ci if (current && current->mm && current->mm->context.adi) { 1828c2ecf20Sopenharmony_ci struct pt_regs *regs; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci regs = task_pt_regs(current); 1858c2ecf20Sopenharmony_ci regs->tstate |= TSTATE_MCDE; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci#endif /* !(__ASSEMBLY__) */ 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci#endif /* !(__SPARC64_MMU_CONTEXT_H) */ 193