Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Yu Liu <yu.liu@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *         Scott Wood <scottwood@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *         Ashish Kalra <ashish.kalra@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *         Varun Sethi <varun.sethi@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * This file is based on arch/powerpc/kvm/44x_tlb.h and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * arch/powerpc/include/asm/kvm_44x.h by Hollis Blanchard <hollisb@us.ibm.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Copyright IBM Corp. 2007-2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #ifndef KVM_E500_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define KVM_E500_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/nohash/mmu-book3e.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/tlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/cputhreads.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) enum vcpu_ftr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	VCPU_FTR_MMU_V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define E500_PID_NUM   3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define E500_TLB_NUM   2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /* entry is mapped somewhere in host TLB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define E500_TLB_VALID		(1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /* TLB1 entry is mapped by host TLB1, tracked by bitmaps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define E500_TLB_BITMAP		(1 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* TLB1 entry is mapped by host TLB0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define E500_TLB_TLB0		(1 << 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* bits [6-5] MAS2_X1 and MAS2_X0 and [4-0] bits for WIMGE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define E500_TLB_MAS2_ATTR	(0x7f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct tlbe_ref {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	kvm_pfn_t pfn;		/* valid only for TLB0, except briefly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned int flags;	/* E500_TLB_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct tlbe_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct tlbe_ref ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #ifdef CONFIG_KVM_E500V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct vcpu_id_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) struct kvmppc_e500_tlb_params {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int entries, ways, sets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) struct kvmppc_vcpu_e500 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct kvm_vcpu vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* Unmodified copy of the guest's TLB -- shared with host userspace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct kvm_book3e_206_tlb_entry *gtlb_arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	/* Starting entry number in gtlb_arch[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	int gtlb_offset[E500_TLB_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/* KVM internal information associated with each guest TLB entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct tlbe_priv *gtlb_priv[E500_TLB_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct kvmppc_e500_tlb_params gtlb_params[E500_TLB_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	unsigned int gtlb_nv[E500_TLB_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned int host_tlb1_nv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	u32 svr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u32 l1csr0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u32 l1csr1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	u32 hid0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	u32 hid1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u64 mcar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct page **shared_tlb_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int num_shared_tlb_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u64 *g2h_tlb1_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned int *h2g_tlb1_rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* Minimum and maximum address mapped my TLB1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned long tlb1_min_eaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned long tlb1_max_eaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #ifdef CONFIG_KVM_E500V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u32 pid[E500_PID_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	/* vcpu id table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct vcpu_id_table *idt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline struct kvmppc_vcpu_e500 *to_e500(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return container_of(vcpu, struct kvmppc_vcpu_e500, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* This geometry is the legacy default -- can be overridden by userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define KVM_E500_TLB0_WAY_SIZE		128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define KVM_E500_TLB0_WAY_NUM		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define KVM_E500_TLB0_SIZE  (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define KVM_E500_TLB1_SIZE  16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define index_of(tlbsel, esel)	(((tlbsel) << 16) | ((esel) & 0xFFFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define tlbsel_of(index)	((index) >> 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define esel_of(index)		((index) & 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define MAS2_ATTRIB_MASK \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	  (MAS2_X0 | MAS2_X1 | MAS2_E | MAS2_G)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define MAS3_ATTRIB_MASK \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	  (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	   | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				ulong value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, gva_t ea);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int type, gva_t ea);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, gva_t ea);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int kvmppc_get_one_reg_e500_tlb(struct kvm_vcpu *vcpu, u64 id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				union kvmppc_one_reg *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int kvmppc_set_one_reg_e500_tlb(struct kvm_vcpu *vcpu, u64 id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			       union kvmppc_one_reg *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #ifdef CONFIG_KVM_E500V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				 unsigned int as, unsigned int gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				 unsigned int pr, int avoid_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* TLB helper functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) get_tlb_size(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return (tlbe->mas1 >> 7) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static inline gva_t get_tlb_eaddr(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return tlbe->mas2 & MAS2_EPN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static inline u64 get_tlb_bytes(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	unsigned int pgsize = get_tlb_size(tlbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 1ULL << 10 << pgsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static inline gva_t get_tlb_end(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	u64 bytes = get_tlb_bytes(tlbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return get_tlb_eaddr(tlbe) + bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline u64 get_tlb_raddr(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return tlbe->mas7_3 & ~0xfffULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) get_tlb_tid(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return (tlbe->mas1 >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) get_tlb_ts(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return (tlbe->mas1 >> 12) & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) get_tlb_v(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return (tlbe->mas1 >> 31) & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) get_tlb_iprot(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return (tlbe->mas1 >> 30) & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) get_tlb_tsize(const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return (tlbe->mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return vcpu->arch.pid & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static inline unsigned int get_cur_as(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return !!(vcpu->arch.shared->msr & (MSR_IS | MSR_DS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static inline unsigned int get_cur_pr(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return !!(vcpu->arch.shared->msr & MSR_PR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static inline unsigned int get_cur_spid(const struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return (vcpu->arch.shared->mas6 >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static inline unsigned int get_cur_sas(const struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return vcpu->arch.shared->mas6 & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static inline unsigned int get_tlb_tlbsel(const struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * Manual says that tlbsel has 2 bits wide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * Since we only have two TLBs, only lower bit is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return (vcpu->arch.shared->mas0 >> 28) & 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static inline unsigned int get_tlb_nv_bit(const struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return vcpu->arch.shared->mas0 & 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static inline unsigned int get_tlb_esel_bit(const struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return (vcpu->arch.shared->mas0 >> 16) & 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			const struct kvm_book3e_206_tlb_entry *tlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	gpa_t gpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!get_tlb_v(tlbe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #ifndef CONFIG_KVM_BOOKE_HV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	/* Does it match current guest AS? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	/* XXX what about IS != DS? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	gpa = get_tlb_raddr(tlbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		/* Mapping is not for RAM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static inline struct kvm_book3e_206_tlb_entry *get_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, int entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	int offset = vcpu_e500->gtlb_offset[tlbsel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return &vcpu_e500->gtlb_arch[offset + entry];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			   struct kvm_book3e_206_tlb_entry *gtlbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #ifdef CONFIG_KVM_BOOKE_HV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #define kvmppc_e500_get_tlb_stid(vcpu, gtlbe)       get_tlb_tid(gtlbe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #define get_tlbmiss_tid(vcpu)           get_cur_pid(vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #define get_tlb_sts(gtlbe)              (gtlbe->mas1 & MAS1_TS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * These functions should be called with preemption disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * and the returned value is valid only in that context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static inline int get_thread_specific_lpid(int vm_lpid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int vcpu_lpid = vm_lpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (threads_per_core == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		vcpu_lpid |= smp_processor_id() & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return vcpu_lpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static inline int get_lpid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return get_thread_specific_lpid(vcpu->kvm->arch.lpid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) unsigned int kvmppc_e500_get_tlb_stid(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				      struct kvm_book3e_206_tlb_entry *gtlbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static inline unsigned int get_tlbmiss_tid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	unsigned int tidseld = (vcpu->arch.shared->mas4 >> 16) & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return vcpu_e500->pid[tidseld];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* Force TS=1 for all guest mappings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #define get_tlb_sts(gtlbe)              (MAS1_TS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #endif /* !BOOKE_HV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static inline bool has_feature(const struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			       enum vcpu_ftr ftr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	bool has_ftr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	switch (ftr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	case VCPU_FTR_MMU_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		has_ftr = ((vcpu->arch.mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return has_ftr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #endif /* KVM_E500_H */