18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef __ASM_POWERPC_MMU_CONTEXT_H 38c2ecf20Sopenharmony_ci#define __ASM_POWERPC_MMU_CONTEXT_H 48c2ecf20Sopenharmony_ci#ifdef __KERNEL__ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/kernel.h> 78c2ecf20Sopenharmony_ci#include <linux/mm.h> 88c2ecf20Sopenharmony_ci#include <linux/sched.h> 98c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 108c2ecf20Sopenharmony_ci#include <asm/mmu.h> 118c2ecf20Sopenharmony_ci#include <asm/cputable.h> 128c2ecf20Sopenharmony_ci#include <asm/cputhreads.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* 158c2ecf20Sopenharmony_ci * Most if the context management is out of line 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ciextern int init_new_context(struct task_struct *tsk, struct mm_struct *mm); 188c2ecf20Sopenharmony_ciextern void destroy_context(struct mm_struct *mm); 198c2ecf20Sopenharmony_ci#ifdef CONFIG_SPAPR_TCE_IOMMU 208c2ecf20Sopenharmony_cistruct mm_iommu_table_group_mem_t; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciextern int isolate_lru_page(struct page *page); /* from internal.h */ 238c2ecf20Sopenharmony_ciextern bool mm_iommu_preregistered(struct mm_struct *mm); 248c2ecf20Sopenharmony_ciextern long mm_iommu_new(struct mm_struct *mm, 258c2ecf20Sopenharmony_ci unsigned long ua, unsigned long entries, 268c2ecf20Sopenharmony_ci struct mm_iommu_table_group_mem_t **pmem); 278c2ecf20Sopenharmony_ciextern long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua, 288c2ecf20Sopenharmony_ci unsigned long entries, unsigned long dev_hpa, 298c2ecf20Sopenharmony_ci struct mm_iommu_table_group_mem_t **pmem); 308c2ecf20Sopenharmony_ciextern long mm_iommu_put(struct mm_struct *mm, 318c2ecf20Sopenharmony_ci struct mm_iommu_table_group_mem_t *mem); 328c2ecf20Sopenharmony_ciextern void mm_iommu_init(struct mm_struct *mm); 338c2ecf20Sopenharmony_ciextern void mm_iommu_cleanup(struct mm_struct *mm); 348c2ecf20Sopenharmony_ciextern struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm, 358c2ecf20Sopenharmony_ci unsigned long ua, unsigned long size); 368c2ecf20Sopenharmony_ciextern struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm( 378c2ecf20Sopenharmony_ci struct mm_struct *mm, unsigned long ua, unsigned long size); 388c2ecf20Sopenharmony_ciextern struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm, 398c2ecf20Sopenharmony_ci unsigned long ua, unsigned long entries); 408c2ecf20Sopenharmony_ciextern long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem, 418c2ecf20Sopenharmony_ci unsigned long ua, unsigned int pageshift, unsigned long *hpa); 428c2ecf20Sopenharmony_ciextern long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem, 438c2ecf20Sopenharmony_ci unsigned long ua, unsigned int pageshift, unsigned long *hpa); 448c2ecf20Sopenharmony_ciextern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua); 458c2ecf20Sopenharmony_ciextern bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa, 468c2ecf20Sopenharmony_ci unsigned int pageshift, unsigned long *size); 478c2ecf20Sopenharmony_ciextern long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem); 488c2ecf20Sopenharmony_ciextern void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem); 498c2ecf20Sopenharmony_ci#else 508c2ecf20Sopenharmony_cistatic inline bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa, 518c2ecf20Sopenharmony_ci unsigned int pageshift, unsigned long *size) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci return false; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_cistatic inline void mm_iommu_init(struct mm_struct *mm) { } 568c2ecf20Sopenharmony_ci#endif 578c2ecf20Sopenharmony_ciextern void switch_slb(struct task_struct *tsk, struct mm_struct *mm); 588c2ecf20Sopenharmony_ciextern void set_context(unsigned long id, pgd_t *pgd); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64 618c2ecf20Sopenharmony_ciextern void radix__switch_mmu_context(struct mm_struct *prev, 628c2ecf20Sopenharmony_ci struct mm_struct *next); 638c2ecf20Sopenharmony_cistatic inline void switch_mmu_context(struct mm_struct *prev, 648c2ecf20Sopenharmony_ci struct mm_struct *next, 658c2ecf20Sopenharmony_ci struct task_struct *tsk) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci if (radix_enabled()) 688c2ecf20Sopenharmony_ci return radix__switch_mmu_context(prev, next); 698c2ecf20Sopenharmony_ci return switch_slb(tsk, next); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciextern int hash__alloc_context_id(void); 738c2ecf20Sopenharmony_ciextern void hash__reserve_context_id(int id); 748c2ecf20Sopenharmony_ciextern void __destroy_context(int context_id); 758c2ecf20Sopenharmony_cistatic inline void mmu_context_init(void) { } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic inline int alloc_extended_context(struct mm_struct *mm, 788c2ecf20Sopenharmony_ci unsigned long ea) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci int context_id; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci int index = ea >> MAX_EA_BITS_PER_CONTEXT; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci context_id = hash__alloc_context_id(); 858c2ecf20Sopenharmony_ci if (context_id < 0) 868c2ecf20Sopenharmony_ci return context_id; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci VM_WARN_ON(mm->context.extended_id[index]); 898c2ecf20Sopenharmony_ci mm->context.extended_id[index] = context_id; 908c2ecf20Sopenharmony_ci return context_id; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic inline bool need_extra_context(struct mm_struct *mm, unsigned long ea) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci int context_id; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci context_id = get_user_context(&mm->context, ea); 988c2ecf20Sopenharmony_ci if (!context_id) 998c2ecf20Sopenharmony_ci return true; 1008c2ecf20Sopenharmony_ci return false; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#else 1048c2ecf20Sopenharmony_ciextern void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next, 1058c2ecf20Sopenharmony_ci struct task_struct *tsk); 1068c2ecf20Sopenharmony_ciextern unsigned long __init_new_context(void); 1078c2ecf20Sopenharmony_ciextern void __destroy_context(unsigned long context_id); 1088c2ecf20Sopenharmony_ciextern void mmu_context_init(void); 1098c2ecf20Sopenharmony_cistatic inline int alloc_extended_context(struct mm_struct *mm, 1108c2ecf20Sopenharmony_ci unsigned long ea) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci /* non book3s_64 should never find this called */ 1138c2ecf20Sopenharmony_ci WARN_ON(1); 1148c2ecf20Sopenharmony_ci return -ENOMEM; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic inline bool need_extra_context(struct mm_struct *mm, unsigned long ea) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci return false; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci#endif 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) && defined(CONFIG_PPC_RADIX_MMU) 1248c2ecf20Sopenharmony_ciextern void radix_kvm_prefetch_workaround(struct mm_struct *mm); 1258c2ecf20Sopenharmony_ci#else 1268c2ecf20Sopenharmony_cistatic inline void radix_kvm_prefetch_workaround(struct mm_struct *mm) { } 1278c2ecf20Sopenharmony_ci#endif 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciextern void switch_cop(struct mm_struct *next); 1308c2ecf20Sopenharmony_ciextern int use_cop(unsigned long acop, struct mm_struct *mm); 1318c2ecf20Sopenharmony_ciextern void drop_cop(unsigned long acop, struct mm_struct *mm); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64 1348c2ecf20Sopenharmony_cistatic inline void inc_mm_active_cpus(struct mm_struct *mm) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci atomic_inc(&mm->context.active_cpus); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic inline void dec_mm_active_cpus(struct mm_struct *mm) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci atomic_dec(&mm->context.active_cpus); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic inline void mm_context_add_copro(struct mm_struct *mm) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci /* 1478c2ecf20Sopenharmony_ci * If any copro is in use, increment the active CPU count 1488c2ecf20Sopenharmony_ci * in order to force TLB invalidations to be global as to 1498c2ecf20Sopenharmony_ci * propagate to the Nest MMU. 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_ci if (atomic_inc_return(&mm->context.copros) == 1) 1528c2ecf20Sopenharmony_ci inc_mm_active_cpus(mm); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic inline void mm_context_remove_copro(struct mm_struct *mm) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci int c; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* 1608c2ecf20Sopenharmony_ci * When removing the last copro, we need to broadcast a global 1618c2ecf20Sopenharmony_ci * flush of the full mm, as the next TLBI may be local and the 1628c2ecf20Sopenharmony_ci * nMMU and/or PSL need to be cleaned up. 1638c2ecf20Sopenharmony_ci * 1648c2ecf20Sopenharmony_ci * Both the 'copros' and 'active_cpus' counts are looked at in 1658c2ecf20Sopenharmony_ci * flush_all_mm() to determine the scope (local/global) of the 1668c2ecf20Sopenharmony_ci * TLBIs, so we need to flush first before decrementing 1678c2ecf20Sopenharmony_ci * 'copros'. If this API is used by several callers for the 1688c2ecf20Sopenharmony_ci * same context, it can lead to over-flushing. It's hopefully 1698c2ecf20Sopenharmony_ci * not common enough to be a problem. 1708c2ecf20Sopenharmony_ci * 1718c2ecf20Sopenharmony_ci * Skip on hash, as we don't know how to do the proper flush 1728c2ecf20Sopenharmony_ci * for the time being. Invalidations will remain global if 1738c2ecf20Sopenharmony_ci * used on hash. Note that we can't drop 'copros' either, as 1748c2ecf20Sopenharmony_ci * it could make some invalidations local with no flush 1758c2ecf20Sopenharmony_ci * in-between. 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_ci if (radix_enabled()) { 1788c2ecf20Sopenharmony_ci flush_all_mm(mm); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci c = atomic_dec_if_positive(&mm->context.copros); 1818c2ecf20Sopenharmony_ci /* Detect imbalance between add and remove */ 1828c2ecf20Sopenharmony_ci WARN_ON(c < 0); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci if (c == 0) 1858c2ecf20Sopenharmony_ci dec_mm_active_cpus(mm); 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/* 1908c2ecf20Sopenharmony_ci * vas_windows counter shows number of open windows in the mm 1918c2ecf20Sopenharmony_ci * context. During context switch, use this counter to clear the 1928c2ecf20Sopenharmony_ci * foreign real address mapping (CP_ABORT) for the thread / process 1938c2ecf20Sopenharmony_ci * that intend to use COPY/PASTE. When a process closes all windows, 1948c2ecf20Sopenharmony_ci * disable CP_ABORT which is expensive to run. 1958c2ecf20Sopenharmony_ci * 1968c2ecf20Sopenharmony_ci * For user context, register a copro so that TLBIs are seen by the 1978c2ecf20Sopenharmony_ci * nest MMU. mm_context_add/remove_vas_window() are used only for user 1988c2ecf20Sopenharmony_ci * space windows. 1998c2ecf20Sopenharmony_ci */ 2008c2ecf20Sopenharmony_cistatic inline void mm_context_add_vas_window(struct mm_struct *mm) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci atomic_inc(&mm->context.vas_windows); 2038c2ecf20Sopenharmony_ci mm_context_add_copro(mm); 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic inline void mm_context_remove_vas_window(struct mm_struct *mm) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci int v; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci mm_context_remove_copro(mm); 2118c2ecf20Sopenharmony_ci v = atomic_dec_if_positive(&mm->context.vas_windows); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* Detect imbalance between add and remove */ 2148c2ecf20Sopenharmony_ci WARN_ON(v < 0); 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci#else 2178c2ecf20Sopenharmony_cistatic inline void inc_mm_active_cpus(struct mm_struct *mm) { } 2188c2ecf20Sopenharmony_cistatic inline void dec_mm_active_cpus(struct mm_struct *mm) { } 2198c2ecf20Sopenharmony_cistatic inline void mm_context_add_copro(struct mm_struct *mm) { } 2208c2ecf20Sopenharmony_cistatic inline void mm_context_remove_copro(struct mm_struct *mm) { } 2218c2ecf20Sopenharmony_ci#endif 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciextern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, 2258c2ecf20Sopenharmony_ci struct task_struct *tsk); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 2288c2ecf20Sopenharmony_ci struct task_struct *tsk) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci unsigned long flags; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci local_irq_save(flags); 2338c2ecf20Sopenharmony_ci switch_mm_irqs_off(prev, next, tsk); 2348c2ecf20Sopenharmony_ci local_irq_restore(flags); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci#define switch_mm_irqs_off switch_mm_irqs_off 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci#define deactivate_mm(tsk,mm) do { } while (0) 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci/* 2428c2ecf20Sopenharmony_ci * After we have set current->mm to a new value, this activates 2438c2ecf20Sopenharmony_ci * the context for the new mm so we see the new mappings. 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_cistatic inline void activate_mm(struct mm_struct *prev, struct mm_struct *next) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci switch_mm_irqs_off(prev, next, current); 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci/* We don't currently use enter_lazy_tlb() for anything */ 2518c2ecf20Sopenharmony_cistatic inline void enter_lazy_tlb(struct mm_struct *mm, 2528c2ecf20Sopenharmony_ci struct task_struct *tsk) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci /* 64-bit Book3E keeps track of current PGD in the PACA */ 2558c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3E_64 2568c2ecf20Sopenharmony_ci get_paca()->pgd = NULL; 2578c2ecf20Sopenharmony_ci#endif 2588c2ecf20Sopenharmony_ci} 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ciextern void arch_exit_mmap(struct mm_struct *mm); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cistatic inline void arch_unmap(struct mm_struct *mm, 2638c2ecf20Sopenharmony_ci unsigned long start, unsigned long end) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci if (start <= mm->context.vdso_base && mm->context.vdso_base < end) 2668c2ecf20Sopenharmony_ci mm->context.vdso_base = 0; 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_MEM_KEYS 2708c2ecf20Sopenharmony_cibool arch_vma_access_permitted(struct vm_area_struct *vma, bool write, 2718c2ecf20Sopenharmony_ci bool execute, bool foreign); 2728c2ecf20Sopenharmony_civoid arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm); 2738c2ecf20Sopenharmony_ci#else /* CONFIG_PPC_MEM_KEYS */ 2748c2ecf20Sopenharmony_cistatic inline bool arch_vma_access_permitted(struct vm_area_struct *vma, 2758c2ecf20Sopenharmony_ci bool write, bool execute, bool foreign) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci /* by default, allow everything */ 2788c2ecf20Sopenharmony_ci return true; 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci#define pkey_mm_init(mm) 2828c2ecf20Sopenharmony_ci#define thread_pkey_regs_save(thread) 2838c2ecf20Sopenharmony_ci#define thread_pkey_regs_restore(new_thread, old_thread) 2848c2ecf20Sopenharmony_ci#define thread_pkey_regs_init(thread) 2858c2ecf20Sopenharmony_ci#define arch_dup_pkeys(oldmm, mm) 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cistatic inline u64 pte_to_hpte_pkey_bits(u64 pteflags) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci return 0x0UL; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_MEM_KEYS */ 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_cistatic inline int arch_dup_mmap(struct mm_struct *oldmm, 2958c2ecf20Sopenharmony_ci struct mm_struct *mm) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci arch_dup_pkeys(oldmm, mm); 2988c2ecf20Sopenharmony_ci return 0; 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */ 3028c2ecf20Sopenharmony_ci#endif /* __ASM_POWERPC_MMU_CONTEXT_H */ 303