14e342025SAlexander Graf /* 24e342025SAlexander Graf * This program is free software; you can redistribute it and/or modify 34e342025SAlexander Graf * it under the terms of the GNU General Public License, version 2, as 44e342025SAlexander Graf * published by the Free Software Foundation. 54e342025SAlexander Graf * 64e342025SAlexander Graf * This program is distributed in the hope that it will be useful, 74e342025SAlexander Graf * but WITHOUT ANY WARRANTY; without even the implied warranty of 84e342025SAlexander Graf * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 94e342025SAlexander Graf * GNU General Public License for more details. 104e342025SAlexander Graf * 114e342025SAlexander Graf * You should have received a copy of the GNU General Public License 124e342025SAlexander Graf * along with this program; if not, write to the Free Software 134e342025SAlexander Graf * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 144e342025SAlexander Graf * 154e342025SAlexander Graf * Copyright SUSE Linux Products GmbH 2009 164e342025SAlexander Graf * 174e342025SAlexander Graf * Authors: Alexander Graf <agraf@suse.de> 184e342025SAlexander Graf */ 194e342025SAlexander Graf 204e342025SAlexander Graf #ifndef __ASM_KVM_BOOK3S_H__ 214e342025SAlexander Graf #define __ASM_KVM_BOOK3S_H__ 224e342025SAlexander Graf 234e342025SAlexander Graf #include <linux/types.h> 244e342025SAlexander Graf #include <linux/kvm_host.h> 252191d657SAlexander Graf #include <asm/kvm_book3s_asm.h> 264e342025SAlexander Graf 274e342025SAlexander Graf struct kvmppc_bat { 28e15a1137SAlexander Graf u64 raw; 294e342025SAlexander Graf u32 bepi; 304e342025SAlexander Graf u32 bepi_mask; 314e342025SAlexander Graf u32 brpn; 324e342025SAlexander Graf u8 wimg; 334e342025SAlexander Graf u8 pp; 343ed9c6d2SAlexander Graf bool vs : 1; 353ed9c6d2SAlexander Graf bool vp : 1; 364e342025SAlexander Graf }; 374e342025SAlexander Graf 384e342025SAlexander Graf struct kvmppc_sid_map { 394e342025SAlexander Graf u64 guest_vsid; 404e342025SAlexander Graf u64 guest_esid; 414e342025SAlexander Graf u64 host_vsid; 423ed9c6d2SAlexander Graf bool valid : 1; 434e342025SAlexander Graf }; 444e342025SAlexander Graf 454e342025SAlexander Graf #define SID_MAP_BITS 9 464e342025SAlexander Graf #define SID_MAP_NUM (1 << SID_MAP_BITS) 474e342025SAlexander Graf #define SID_MAP_MASK (SID_MAP_NUM - 1) 484e342025SAlexander Graf 498b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_64 508b6db3bcSAlexander Graf #define SID_CONTEXTS 1 518b6db3bcSAlexander Graf #else 528b6db3bcSAlexander Graf #define SID_CONTEXTS 128 538b6db3bcSAlexander Graf #define VSID_POOL_SIZE (SID_CONTEXTS * 16) 548b6db3bcSAlexander Graf #endif 558b6db3bcSAlexander Graf 56c4befc58SPaul Mackerras struct hpte_cache { 57c4befc58SPaul Mackerras struct hlist_node list_pte; 58c4befc58SPaul Mackerras struct hlist_node list_pte_long; 59c4befc58SPaul Mackerras struct hlist_node list_vpte; 60c4befc58SPaul Mackerras struct hlist_node list_vpte_long; 61a4a0f252SPaul Mackerras #ifdef CONFIG_PPC_BOOK3S_64 62a4a0f252SPaul Mackerras struct hlist_node list_vpte_64k; 63a4a0f252SPaul Mackerras #endif 64c4befc58SPaul Mackerras struct rcu_head rcu_head; 655524a27dSAneesh Kumar K.V u64 host_vpn; 66c4befc58SPaul Mackerras u64 pfn; 67c4befc58SPaul Mackerras ulong slot; 68c4befc58SPaul Mackerras struct kvmppc_pte pte; 69c9029c34SPaul Mackerras int pagesize; 70c4befc58SPaul Mackerras }; 71c4befc58SPaul Mackerras 724e342025SAlexander Graf struct kvmppc_vcpu_book3s { 734e342025SAlexander Graf struct kvmppc_sid_map sid_map[SID_MAP_NUM]; 744e342025SAlexander Graf struct { 754e342025SAlexander Graf u64 esid; 764e342025SAlexander Graf u64 vsid; 774e342025SAlexander Graf } slb_shadow[64]; 784e342025SAlexander Graf u8 slb_shadow_max; 794e342025SAlexander Graf struct kvmppc_bat ibat[8]; 804e342025SAlexander Graf struct kvmppc_bat dbat[8]; 814e342025SAlexander Graf u64 hid[6]; 82d6d549b2SAlexander Graf u64 gqr[8]; 834e342025SAlexander Graf u64 sdr1; 844e342025SAlexander Graf u64 hior; 854e342025SAlexander Graf u64 msr_mask; 86b0a94d4eSPaul Mackerras u64 purr_offset; 87b0a94d4eSPaul Mackerras u64 spurr_offset; 888b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32 898b6db3bcSAlexander Graf u32 vsid_pool[VSID_POOL_SIZE]; 90ffe36492SBenjamin Herrenschmidt u32 vsid_next; 918b6db3bcSAlexander Graf #else 92ffe36492SBenjamin Herrenschmidt u64 proto_vsid_first; 93ffe36492SBenjamin Herrenschmidt u64 proto_vsid_max; 94ffe36492SBenjamin Herrenschmidt u64 proto_vsid_next; 958b6db3bcSAlexander Graf #endif 968b6db3bcSAlexander Graf int context_id[SID_CONTEXTS]; 97c4befc58SPaul Mackerras 981022fc3dSAlexander Graf bool hior_explicit; /* HIOR is set by ioctl, not PVR */ 991022fc3dSAlexander Graf 100c4befc58SPaul Mackerras struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE]; 101c4befc58SPaul Mackerras struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG]; 102c4befc58SPaul Mackerras struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE]; 103c4befc58SPaul Mackerras struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG]; 104a4a0f252SPaul Mackerras #ifdef CONFIG_PPC_BOOK3S_64 105a4a0f252SPaul Mackerras struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K]; 106a4a0f252SPaul Mackerras #endif 107c4befc58SPaul Mackerras int hpte_cache_count; 108c4befc58SPaul Mackerras spinlock_t mmu_lock; 1094e342025SAlexander Graf }; 1104e342025SAlexander Graf 1114e342025SAlexander Graf #define CONTEXT_HOST 0 1124e342025SAlexander Graf #define CONTEXT_GUEST 1 1134e342025SAlexander Graf #define CONTEXT_GUEST_END 2 1144e342025SAlexander Graf 115c9029c34SPaul Mackerras #define VSID_REAL 0x07ffffffffc00000ULL 116c9029c34SPaul Mackerras #define VSID_BAT 0x07ffffffffb00000ULL 117c9029c34SPaul Mackerras #define VSID_64K 0x0800000000000000ULL 1180f296829SPaul Mackerras #define VSID_1T 0x1000000000000000ULL 119f7bc74e1SAlexander Graf #define VSID_REAL_DR 0x2000000000000000ULL 120f7bc74e1SAlexander Graf #define VSID_REAL_IR 0x4000000000000000ULL 1215a1b419fSAlexander Graf #define VSID_PR 0x8000000000000000ULL 1224e342025SAlexander Graf 123af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask); 1244e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask); 125af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end); 1264e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr); 1274e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu); 1284e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu); 129de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu); 13093b159b4SPaul Mackerras extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte, 13193b159b4SPaul Mackerras bool iswrite); 13293b159b4SPaul Mackerras extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte); 1334e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr); 1340f296829SPaul Mackerras extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size); 1354e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu); 136697d3899SPaul Mackerras extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run, 137697d3899SPaul Mackerras struct kvm_vcpu *vcpu, unsigned long addr, 138697d3899SPaul Mackerras unsigned long status); 139697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, 140697d3899SPaul Mackerras unsigned long slb_v, unsigned long valid); 141fef093beSAlexander Graf 142fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte); 143fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu); 144d78bca72SPaul Mackerras extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte); 145fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu); 146fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu); 147fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte); 148fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void); 149fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void); 150de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void); 151fef093beSAlexander Graf 1525467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data); 1535467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data); 1544e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec); 155bc5ad3f3SBenjamin Herrenschmidt extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, 156bc5ad3f3SBenjamin Herrenschmidt unsigned int vec); 157de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags); 158e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, 159e15a1137SAlexander Graf bool upper, u32 val); 160aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr); 161831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu); 16293b159b4SPaul Mackerras extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, bool writing, 16393b159b4SPaul Mackerras bool *writable); 164342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev, 165342d3db7SPaul Mackerras unsigned long *rmap, long pte_index, int realmode); 166342d3db7SPaul Mackerras extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep, 167342d3db7SPaul Mackerras unsigned long pte_index); 16855514893SPaul Mackerras void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep, 16955514893SPaul Mackerras unsigned long pte_index); 17093e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr, 17193e60249SPaul Mackerras unsigned long *nb_ret); 172c35635efSPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr, 173c35635efSPaul Mackerras unsigned long gpa, bool dirty); 174c77162deSPaul Mackerras extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags, 175c77162deSPaul Mackerras long pte_index, unsigned long pteh, unsigned long ptel); 1767ed661bfSPaul Mackerras extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags, 1777ed661bfSPaul Mackerras long pte_index, unsigned long pteh, unsigned long ptel, 1787ed661bfSPaul Mackerras pgd_t *pgdir, bool realmode, unsigned long *idx_ret); 1796b445ad4SPaul Mackerras extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags, 1806b445ad4SPaul Mackerras unsigned long pte_index, unsigned long avpn, 1816b445ad4SPaul Mackerras unsigned long *hpret); 18282ed3616SPaul Mackerras extern long kvmppc_hv_get_dirty_log(struct kvm *kvm, 183dfe49dbdSPaul Mackerras struct kvm_memory_slot *memslot, unsigned long *map); 184a0144e2aSPaul Mackerras extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, 185a0144e2aSPaul Mackerras unsigned long mask); 1864e342025SAlexander Graf 18702143947SPaul Mackerras extern void kvmppc_entry_trampoline(void); 188de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void); 189ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst); 190ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst); 1910254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd); 1924e342025SAlexander Graf 1934e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu) 1944e342025SAlexander Graf { 1953ff95502SPaul Mackerras return vcpu->arch.book3s; 1964e342025SAlexander Graf } 1974e342025SAlexander Graf 198de56a948SPaul Mackerras /* Also add subarch specific defines */ 199de56a948SPaul Mackerras 200de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER 201de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h> 202de56a948SPaul Mackerras #endif 203de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER 204de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h> 205de56a948SPaul Mackerras #endif 206de56a948SPaul Mackerras 207de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val) 208de56a948SPaul Mackerras { 209de56a948SPaul Mackerras vcpu->arch.gpr[num] = val; 210de56a948SPaul Mackerras } 211de56a948SPaul Mackerras 212de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num) 213de56a948SPaul Mackerras { 214de56a948SPaul Mackerras return vcpu->arch.gpr[num]; 215de56a948SPaul Mackerras } 216de56a948SPaul Mackerras 217de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val) 218de56a948SPaul Mackerras { 219de56a948SPaul Mackerras vcpu->arch.cr = val; 220de56a948SPaul Mackerras } 221de56a948SPaul Mackerras 222de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu) 223de56a948SPaul Mackerras { 224de56a948SPaul Mackerras return vcpu->arch.cr; 225de56a948SPaul Mackerras } 226de56a948SPaul Mackerras 227de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val) 228de56a948SPaul Mackerras { 229de56a948SPaul Mackerras vcpu->arch.xer = val; 230de56a948SPaul Mackerras } 231de56a948SPaul Mackerras 232de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu) 233de56a948SPaul Mackerras { 234de56a948SPaul Mackerras return vcpu->arch.xer; 235de56a948SPaul Mackerras } 236de56a948SPaul Mackerras 237de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val) 238de56a948SPaul Mackerras { 239de56a948SPaul Mackerras vcpu->arch.ctr = val; 240de56a948SPaul Mackerras } 241de56a948SPaul Mackerras 242de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu) 243de56a948SPaul Mackerras { 244de56a948SPaul Mackerras return vcpu->arch.ctr; 245de56a948SPaul Mackerras } 246de56a948SPaul Mackerras 247de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val) 248de56a948SPaul Mackerras { 249de56a948SPaul Mackerras vcpu->arch.lr = val; 250de56a948SPaul Mackerras } 251de56a948SPaul Mackerras 252de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu) 253de56a948SPaul Mackerras { 254de56a948SPaul Mackerras return vcpu->arch.lr; 255de56a948SPaul Mackerras } 256de56a948SPaul Mackerras 257de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val) 258de56a948SPaul Mackerras { 259de56a948SPaul Mackerras vcpu->arch.pc = val; 260de56a948SPaul Mackerras } 261de56a948SPaul Mackerras 262de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu) 263de56a948SPaul Mackerras { 264de56a948SPaul Mackerras return vcpu->arch.pc; 265de56a948SPaul Mackerras } 266de56a948SPaul Mackerras 2677a8ff56bSAlexander Graf static inline u32 kvmppc_get_last_inst_internal(struct kvm_vcpu *vcpu, ulong pc) 268de56a948SPaul Mackerras { 269de56a948SPaul Mackerras /* Load the instruction manually if it failed to do so in the 270de56a948SPaul Mackerras * exit path */ 271de56a948SPaul Mackerras if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) 272de56a948SPaul Mackerras kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false); 273de56a948SPaul Mackerras 274de56a948SPaul Mackerras return vcpu->arch.last_inst; 275de56a948SPaul Mackerras } 276de56a948SPaul Mackerras 2777a8ff56bSAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu) 2787a8ff56bSAlexander Graf { 2797a8ff56bSAlexander Graf return kvmppc_get_last_inst_internal(vcpu, kvmppc_get_pc(vcpu)); 2807a8ff56bSAlexander Graf } 2817a8ff56bSAlexander Graf 2828b23de29SPaul Mackerras /* 2838b23de29SPaul Mackerras * Like kvmppc_get_last_inst(), but for fetching a sc instruction. 2848b23de29SPaul Mackerras * Because the sc instruction sets SRR0 to point to the following 2858b23de29SPaul Mackerras * instruction, we have to fetch from pc - 4. 2868b23de29SPaul Mackerras */ 2878b23de29SPaul Mackerras static inline u32 kvmppc_get_last_sc(struct kvm_vcpu *vcpu) 2888b23de29SPaul Mackerras { 2897a8ff56bSAlexander Graf return kvmppc_get_last_inst_internal(vcpu, kvmppc_get_pc(vcpu) - 4); 2908b23de29SPaul Mackerras } 2918b23de29SPaul Mackerras 292de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu) 293de56a948SPaul Mackerras { 294de56a948SPaul Mackerras return vcpu->arch.fault_dar; 295de56a948SPaul Mackerras } 296de56a948SPaul Mackerras 297ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly 298ad0a048bSAlexander Graf * instruction for the OSI hypercalls */ 299ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3 0x113724FA 300ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4 0x77810F9B 301ad0a048bSAlexander Graf 3024e342025SAlexander Graf #define INS_DCBZ 0x7c0007ec 3038c32a2eaSBharat Bhushan /* TO = 31 for unconditional trap */ 3048c32a2eaSBharat Bhushan #define INS_TW 0x7fe00008 3054e342025SAlexander Graf 306043cc4d7SScott Wood /* LPIDs we support with this build -- runtime limit may be lower */ 307043cc4d7SScott Wood #define KVMPPC_NR_LPIDS (LPID_RSVD + 1) 308043cc4d7SScott Wood 3094e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */ 310