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; 868b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32 878b6db3bcSAlexander Graf u32 vsid_pool[VSID_POOL_SIZE]; 88ffe36492SBenjamin Herrenschmidt u32 vsid_next; 898b6db3bcSAlexander Graf #else 90ffe36492SBenjamin Herrenschmidt u64 proto_vsid_first; 91ffe36492SBenjamin Herrenschmidt u64 proto_vsid_max; 92ffe36492SBenjamin Herrenschmidt u64 proto_vsid_next; 938b6db3bcSAlexander Graf #endif 948b6db3bcSAlexander Graf int context_id[SID_CONTEXTS]; 95c4befc58SPaul Mackerras 961022fc3dSAlexander Graf bool hior_explicit; /* HIOR is set by ioctl, not PVR */ 971022fc3dSAlexander Graf 98c4befc58SPaul Mackerras struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE]; 99c4befc58SPaul Mackerras struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG]; 100c4befc58SPaul Mackerras struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE]; 101c4befc58SPaul Mackerras struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG]; 102a4a0f252SPaul Mackerras #ifdef CONFIG_PPC_BOOK3S_64 103a4a0f252SPaul Mackerras struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K]; 104a4a0f252SPaul Mackerras #endif 105c4befc58SPaul Mackerras int hpte_cache_count; 106c4befc58SPaul Mackerras spinlock_t mmu_lock; 1074e342025SAlexander Graf }; 1084e342025SAlexander Graf 1094e342025SAlexander Graf #define CONTEXT_HOST 0 1104e342025SAlexander Graf #define CONTEXT_GUEST 1 1114e342025SAlexander Graf #define CONTEXT_GUEST_END 2 1124e342025SAlexander Graf 113c9029c34SPaul Mackerras #define VSID_REAL 0x07ffffffffc00000ULL 114c9029c34SPaul Mackerras #define VSID_BAT 0x07ffffffffb00000ULL 115c9029c34SPaul Mackerras #define VSID_64K 0x0800000000000000ULL 1160f296829SPaul Mackerras #define VSID_1T 0x1000000000000000ULL 117f7bc74e1SAlexander Graf #define VSID_REAL_DR 0x2000000000000000ULL 118f7bc74e1SAlexander Graf #define VSID_REAL_IR 0x4000000000000000ULL 1195a1b419fSAlexander Graf #define VSID_PR 0x8000000000000000ULL 1204e342025SAlexander Graf 121af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask); 1224e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask); 123af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end); 1244e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr); 1254e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu); 1264e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu); 127de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu); 12893b159b4SPaul Mackerras extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte, 12993b159b4SPaul Mackerras bool iswrite); 13093b159b4SPaul Mackerras extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte); 1314e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr); 1320f296829SPaul Mackerras extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size); 1334e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu); 134697d3899SPaul Mackerras extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run, 135697d3899SPaul Mackerras struct kvm_vcpu *vcpu, unsigned long addr, 136697d3899SPaul Mackerras unsigned long status); 137697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, 138697d3899SPaul Mackerras unsigned long slb_v, unsigned long valid); 139fef093beSAlexander Graf 140fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte); 141fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu); 142d78bca72SPaul Mackerras extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte); 143fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu); 144fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu); 145fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte); 146fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void); 147fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void); 148de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void); 149ae2113a4SPaul Mackerras extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc); 150fef093beSAlexander Graf 15135c4a733SAlexander Graf /* XXX remove this export when load_last_inst() is generic */ 1525467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data); 1534e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec); 154bc5ad3f3SBenjamin Herrenschmidt extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, 155bc5ad3f3SBenjamin Herrenschmidt unsigned int vec); 156de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags); 157e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, 158e15a1137SAlexander Graf bool upper, u32 val); 159aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr); 160831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu); 16189b68c96SAlexander Graf extern pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, bool writing, 16293b159b4SPaul Mackerras bool *writable); 163342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev, 164342d3db7SPaul Mackerras unsigned long *rmap, long pte_index, int realmode); 1656f22bd32SAlexander Graf extern void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep, 166342d3db7SPaul Mackerras unsigned long pte_index); 1676f22bd32SAlexander Graf void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep, 16855514893SPaul Mackerras unsigned long pte_index); 16993e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr, 17093e60249SPaul Mackerras unsigned long *nb_ret); 171c35635efSPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr, 172c35635efSPaul Mackerras unsigned long gpa, bool dirty); 173c77162deSPaul Mackerras extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags, 174c77162deSPaul Mackerras long pte_index, unsigned long pteh, unsigned long ptel); 1757ed661bfSPaul Mackerras extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags, 1767ed661bfSPaul Mackerras long pte_index, unsigned long pteh, unsigned long ptel, 1777ed661bfSPaul Mackerras pgd_t *pgdir, bool realmode, unsigned long *idx_ret); 1786b445ad4SPaul Mackerras extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags, 1796b445ad4SPaul Mackerras unsigned long pte_index, unsigned long avpn, 1806b445ad4SPaul Mackerras unsigned long *hpret); 18182ed3616SPaul Mackerras extern long kvmppc_hv_get_dirty_log(struct kvm *kvm, 182dfe49dbdSPaul Mackerras struct kvm_memory_slot *memslot, unsigned long *map); 183a0144e2aSPaul Mackerras extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, 184a0144e2aSPaul Mackerras unsigned long mask); 1858e6afa36SAlexander Graf extern void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr); 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); 192699a0ea0SPaul Mackerras extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm); 193ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_pr(unsigned long cmd); 194ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_hv_realmode(unsigned long cmd); 195c9dad7f9SAlexander Graf extern void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu, 196c9dad7f9SAlexander Graf struct kvm_vcpu *vcpu); 197c9dad7f9SAlexander Graf extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu, 198c9dad7f9SAlexander Graf struct kvmppc_book3s_shadow_vcpu *svcpu); 1994e342025SAlexander Graf 2004e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu) 2014e342025SAlexander Graf { 2023ff95502SPaul Mackerras return vcpu->arch.book3s; 2034e342025SAlexander Graf } 2044e342025SAlexander Graf 205de56a948SPaul Mackerras /* Also add subarch specific defines */ 206de56a948SPaul Mackerras 207de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER 208de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h> 209de56a948SPaul Mackerras #endif 210de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER 211de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h> 212de56a948SPaul Mackerras #endif 213de56a948SPaul Mackerras 214de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val) 215de56a948SPaul Mackerras { 216de56a948SPaul Mackerras vcpu->arch.gpr[num] = val; 217de56a948SPaul Mackerras } 218de56a948SPaul Mackerras 219de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num) 220de56a948SPaul Mackerras { 221de56a948SPaul Mackerras return vcpu->arch.gpr[num]; 222de56a948SPaul Mackerras } 223de56a948SPaul Mackerras 224de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val) 225de56a948SPaul Mackerras { 226de56a948SPaul Mackerras vcpu->arch.cr = val; 227de56a948SPaul Mackerras } 228de56a948SPaul Mackerras 229de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu) 230de56a948SPaul Mackerras { 231de56a948SPaul Mackerras return vcpu->arch.cr; 232de56a948SPaul Mackerras } 233de56a948SPaul Mackerras 234de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val) 235de56a948SPaul Mackerras { 236de56a948SPaul Mackerras vcpu->arch.xer = val; 237de56a948SPaul Mackerras } 238de56a948SPaul Mackerras 239de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu) 240de56a948SPaul Mackerras { 241de56a948SPaul Mackerras return vcpu->arch.xer; 242de56a948SPaul Mackerras } 243de56a948SPaul Mackerras 244de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val) 245de56a948SPaul Mackerras { 246de56a948SPaul Mackerras vcpu->arch.ctr = val; 247de56a948SPaul Mackerras } 248de56a948SPaul Mackerras 249de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu) 250de56a948SPaul Mackerras { 251de56a948SPaul Mackerras return vcpu->arch.ctr; 252de56a948SPaul Mackerras } 253de56a948SPaul Mackerras 254de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val) 255de56a948SPaul Mackerras { 256de56a948SPaul Mackerras vcpu->arch.lr = val; 257de56a948SPaul Mackerras } 258de56a948SPaul Mackerras 259de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu) 260de56a948SPaul Mackerras { 261de56a948SPaul Mackerras return vcpu->arch.lr; 262de56a948SPaul Mackerras } 263de56a948SPaul Mackerras 264de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val) 265de56a948SPaul Mackerras { 266de56a948SPaul Mackerras vcpu->arch.pc = val; 267de56a948SPaul Mackerras } 268de56a948SPaul Mackerras 269de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu) 270de56a948SPaul Mackerras { 271de56a948SPaul Mackerras return vcpu->arch.pc; 272de56a948SPaul Mackerras } 273de56a948SPaul Mackerras 2745deb8e7aSAlexander Graf static inline u64 kvmppc_get_msr(struct kvm_vcpu *vcpu); 27573601775SCédric Le Goater static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu) 276de56a948SPaul Mackerras { 2775deb8e7aSAlexander Graf return (kvmppc_get_msr(vcpu) & MSR_LE) != (MSR_KERNEL & MSR_LE); 27873601775SCédric Le Goater } 279de56a948SPaul Mackerras 280de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu) 281de56a948SPaul Mackerras { 282de56a948SPaul Mackerras return vcpu->arch.fault_dar; 283de56a948SPaul Mackerras } 284de56a948SPaul Mackerras 285e59d24e6SGreg Kurz static inline bool is_kvmppc_resume_guest(int r) 286e59d24e6SGreg Kurz { 287e59d24e6SGreg Kurz return (r == RESUME_GUEST || r == RESUME_GUEST_NV); 288e59d24e6SGreg Kurz } 289e59d24e6SGreg Kurz 290c12fb43cSAlexander Graf static inline bool is_kvmppc_hv_enabled(struct kvm *kvm); 291c12fb43cSAlexander Graf static inline bool kvmppc_supports_magic_page(struct kvm_vcpu *vcpu) 292c12fb43cSAlexander Graf { 293c12fb43cSAlexander Graf /* Only PR KVM supports the magic page */ 294c12fb43cSAlexander Graf return !is_kvmppc_hv_enabled(vcpu->kvm); 295c12fb43cSAlexander Graf } 296c12fb43cSAlexander Graf 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 309c01e3f66SAlexander Graf #define SPLIT_HACK_MASK 0xff000000 310c01e3f66SAlexander Graf #define SPLIT_HACK_OFFS 0xfb000000 311c01e3f66SAlexander Graf 3124e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */ 313