18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2008
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Authors: Hollis Blanchard <hollisb@us.ibm.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef __POWERPC_KVM_PPC_H__
108c2ecf20Sopenharmony_ci#define __POWERPC_KVM_PPC_H__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* This file exists just so we can dereference kvm_vcpu, avoiding nested header
138c2ecf20Sopenharmony_ci * dependencies. */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci#include <linux/timer.h>
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/kvm_types.h>
198c2ecf20Sopenharmony_ci#include <linux/kvm_host.h>
208c2ecf20Sopenharmony_ci#include <linux/bug.h>
218c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S
228c2ecf20Sopenharmony_ci#include <asm/kvm_book3s.h>
238c2ecf20Sopenharmony_ci#else
248c2ecf20Sopenharmony_ci#include <asm/kvm_booke.h>
258c2ecf20Sopenharmony_ci#endif
268c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
278c2ecf20Sopenharmony_ci#include <asm/paca.h>
288c2ecf20Sopenharmony_ci#include <asm/xive.h>
298c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h>
308c2ecf20Sopenharmony_ci#endif
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
348c2ecf20Sopenharmony_ci * for supporting software breakpoint.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define KVMPPC_INST_SW_BREAKPOINT	0x00dddd00
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cienum emulation_result {
398c2ecf20Sopenharmony_ci	EMULATE_DONE,         /* no further processing */
408c2ecf20Sopenharmony_ci	EMULATE_DO_MMIO,      /* kvm_run filled with MMIO request */
418c2ecf20Sopenharmony_ci	EMULATE_FAIL,         /* can't emulate this instruction */
428c2ecf20Sopenharmony_ci	EMULATE_AGAIN,        /* something went wrong. go again */
438c2ecf20Sopenharmony_ci	EMULATE_EXIT_USER,    /* emulation requires exit to user-space */
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum instruction_fetch_type {
478c2ecf20Sopenharmony_ci	INST_GENERIC,
488c2ecf20Sopenharmony_ci	INST_SC,		/* system call */
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cienum xlate_instdata {
528c2ecf20Sopenharmony_ci	XLATE_INST,		/* translate instruction address */
538c2ecf20Sopenharmony_ci	XLATE_DATA		/* translate data address */
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cienum xlate_readwrite {
578c2ecf20Sopenharmony_ci	XLATE_READ,		/* check for read permissions */
588c2ecf20Sopenharmony_ci	XLATE_WRITE		/* check for write permissions */
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciextern int kvmppc_vcpu_run(struct kvm_vcpu *vcpu);
628c2ecf20Sopenharmony_ciextern int __kvmppc_vcpu_run(struct kvm_vcpu *vcpu);
638c2ecf20Sopenharmony_ciextern void kvmppc_handler_highmem(void);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciextern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
668c2ecf20Sopenharmony_ciextern int kvmppc_handle_load(struct kvm_vcpu *vcpu,
678c2ecf20Sopenharmony_ci                              unsigned int rt, unsigned int bytes,
688c2ecf20Sopenharmony_ci			      int is_default_endian);
698c2ecf20Sopenharmony_ciextern int kvmppc_handle_loads(struct kvm_vcpu *vcpu,
708c2ecf20Sopenharmony_ci                               unsigned int rt, unsigned int bytes,
718c2ecf20Sopenharmony_ci			       int is_default_endian);
728c2ecf20Sopenharmony_ciextern int kvmppc_handle_vsx_load(struct kvm_vcpu *vcpu,
738c2ecf20Sopenharmony_ci				unsigned int rt, unsigned int bytes,
748c2ecf20Sopenharmony_ci			int is_default_endian, int mmio_sign_extend);
758c2ecf20Sopenharmony_ciextern int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu,
768c2ecf20Sopenharmony_ci		unsigned int rt, unsigned int bytes, int is_default_endian);
778c2ecf20Sopenharmony_ciextern int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu,
788c2ecf20Sopenharmony_ci		unsigned int rs, unsigned int bytes, int is_default_endian);
798c2ecf20Sopenharmony_ciextern int kvmppc_handle_store(struct kvm_vcpu *vcpu,
808c2ecf20Sopenharmony_ci			       u64 val, unsigned int bytes,
818c2ecf20Sopenharmony_ci			       int is_default_endian);
828c2ecf20Sopenharmony_ciextern int kvmppc_handle_vsx_store(struct kvm_vcpu *vcpu,
838c2ecf20Sopenharmony_ci				int rs, unsigned int bytes,
848c2ecf20Sopenharmony_ci				int is_default_endian);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciextern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
878c2ecf20Sopenharmony_ci				 enum instruction_fetch_type type, u32 *inst);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciextern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
908c2ecf20Sopenharmony_ci		     bool data);
918c2ecf20Sopenharmony_ciextern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
928c2ecf20Sopenharmony_ci		     bool data);
938c2ecf20Sopenharmony_ciextern int kvmppc_emulate_instruction(struct kvm_vcpu *vcpu);
948c2ecf20Sopenharmony_ciextern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
958c2ecf20Sopenharmony_ciextern int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu);
968c2ecf20Sopenharmony_ciextern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
978c2ecf20Sopenharmony_ciextern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
988c2ecf20Sopenharmony_ciextern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
998c2ecf20Sopenharmony_ciextern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
1008c2ecf20Sopenharmony_ciextern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
1018c2ecf20Sopenharmony_ciextern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Core-specific hooks */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciextern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
1068c2ecf20Sopenharmony_ci                           unsigned int gtlb_idx);
1078c2ecf20Sopenharmony_ciextern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
1088c2ecf20Sopenharmony_ciextern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
1098c2ecf20Sopenharmony_ciextern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
1108c2ecf20Sopenharmony_ciextern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
1118c2ecf20Sopenharmony_ciextern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
1128c2ecf20Sopenharmony_ci                              gva_t eaddr);
1138c2ecf20Sopenharmony_ciextern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
1148c2ecf20Sopenharmony_ciextern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
1158c2ecf20Sopenharmony_ciextern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
1168c2ecf20Sopenharmony_ci			enum xlate_instdata xlid, enum xlate_readwrite xlrw,
1178c2ecf20Sopenharmony_ci			struct kvmppc_pte *pte);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciextern int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu);
1208c2ecf20Sopenharmony_ciextern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
1218c2ecf20Sopenharmony_ciextern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
1228c2ecf20Sopenharmony_ciextern int kvmppc_core_check_processor_compat(void);
1238c2ecf20Sopenharmony_ciextern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
1248c2ecf20Sopenharmony_ci                                      struct kvm_translation *tr);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciextern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
1278c2ecf20Sopenharmony_ciextern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciextern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
1308c2ecf20Sopenharmony_ciextern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
1318c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu, ulong flags);
1328c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
1338c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu);
1348c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu);
1358c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu);
1368c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
1378c2ecf20Sopenharmony_ciextern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
1388c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
1398c2ecf20Sopenharmony_ci                                       struct kvm_interrupt *irq);
1408c2ecf20Sopenharmony_ciextern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
1418c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
1428c2ecf20Sopenharmony_ci					ulong esr_flags);
1438c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
1448c2ecf20Sopenharmony_ci					   ulong dear_flags,
1458c2ecf20Sopenharmony_ci					   ulong esr_flags);
1468c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
1478c2ecf20Sopenharmony_ciextern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
1488c2ecf20Sopenharmony_ci					   ulong esr_flags);
1498c2ecf20Sopenharmony_ciextern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
1508c2ecf20Sopenharmony_ciextern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ciextern int kvmppc_booke_init(void);
1538c2ecf20Sopenharmony_ciextern void kvmppc_booke_exit(void);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciextern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
1568c2ecf20Sopenharmony_ciextern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
1578c2ecf20Sopenharmony_ciextern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciextern int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order);
1608c2ecf20Sopenharmony_ciextern void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info);
1618c2ecf20Sopenharmony_ciextern long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order);
1628c2ecf20Sopenharmony_ciextern void kvmppc_free_hpt(struct kvm_hpt_info *info);
1638c2ecf20Sopenharmony_ciextern void kvmppc_rmap_reset(struct kvm *kvm);
1648c2ecf20Sopenharmony_ciextern long kvmppc_prepare_vrma(struct kvm *kvm,
1658c2ecf20Sopenharmony_ci				struct kvm_userspace_memory_region *mem);
1668c2ecf20Sopenharmony_ciextern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
1678c2ecf20Sopenharmony_ci			struct kvm_memory_slot *memslot, unsigned long porder);
1688c2ecf20Sopenharmony_ciextern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
1698c2ecf20Sopenharmony_ciextern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
1708c2ecf20Sopenharmony_ci		struct iommu_group *grp);
1718c2ecf20Sopenharmony_ciextern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
1728c2ecf20Sopenharmony_ci		struct iommu_group *grp);
1738c2ecf20Sopenharmony_ciextern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
1748c2ecf20Sopenharmony_ciextern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
1758c2ecf20Sopenharmony_ciextern void kvmppc_setup_partition_table(struct kvm *kvm);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciextern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
1788c2ecf20Sopenharmony_ci				struct kvm_create_spapr_tce_64 *args);
1798c2ecf20Sopenharmony_ciextern struct kvmppc_spapr_tce_table *kvmppc_find_table(
1808c2ecf20Sopenharmony_ci		struct kvm *kvm, unsigned long liobn);
1818c2ecf20Sopenharmony_ci#define kvmppc_ioba_validate(stt, ioba, npages)                         \
1828c2ecf20Sopenharmony_ci		(iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
1838c2ecf20Sopenharmony_ci				(stt)->size, (ioba), (npages)) ?        \
1848c2ecf20Sopenharmony_ci				H_PARAMETER : H_SUCCESS)
1858c2ecf20Sopenharmony_ciextern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
1868c2ecf20Sopenharmony_ci			     unsigned long ioba, unsigned long tce);
1878c2ecf20Sopenharmony_ciextern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
1888c2ecf20Sopenharmony_ci		unsigned long liobn, unsigned long ioba,
1898c2ecf20Sopenharmony_ci		unsigned long tce_list, unsigned long npages);
1908c2ecf20Sopenharmony_ciextern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
1918c2ecf20Sopenharmony_ci		unsigned long liobn, unsigned long ioba,
1928c2ecf20Sopenharmony_ci		unsigned long tce_value, unsigned long npages);
1938c2ecf20Sopenharmony_ciextern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
1948c2ecf20Sopenharmony_ci			     unsigned long ioba);
1958c2ecf20Sopenharmony_ciextern struct page *kvm_alloc_hpt_cma(unsigned long nr_pages);
1968c2ecf20Sopenharmony_ciextern void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages);
1978c2ecf20Sopenharmony_ciextern int kvmppc_core_init_vm(struct kvm *kvm);
1988c2ecf20Sopenharmony_ciextern void kvmppc_core_destroy_vm(struct kvm *kvm);
1998c2ecf20Sopenharmony_ciextern void kvmppc_core_free_memslot(struct kvm *kvm,
2008c2ecf20Sopenharmony_ci				     struct kvm_memory_slot *slot);
2018c2ecf20Sopenharmony_ciextern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
2028c2ecf20Sopenharmony_ci				struct kvm_memory_slot *memslot,
2038c2ecf20Sopenharmony_ci				const struct kvm_userspace_memory_region *mem,
2048c2ecf20Sopenharmony_ci				enum kvm_mr_change change);
2058c2ecf20Sopenharmony_ciextern void kvmppc_core_commit_memory_region(struct kvm *kvm,
2068c2ecf20Sopenharmony_ci				const struct kvm_userspace_memory_region *mem,
2078c2ecf20Sopenharmony_ci				const struct kvm_memory_slot *old,
2088c2ecf20Sopenharmony_ci				const struct kvm_memory_slot *new,
2098c2ecf20Sopenharmony_ci				enum kvm_mr_change change);
2108c2ecf20Sopenharmony_ciextern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
2118c2ecf20Sopenharmony_ci				      struct kvm_ppc_smmu_info *info);
2128c2ecf20Sopenharmony_ciextern void kvmppc_core_flush_memslot(struct kvm *kvm,
2138c2ecf20Sopenharmony_ci				      struct kvm_memory_slot *memslot);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciextern int kvmppc_bookehv_init(void);
2168c2ecf20Sopenharmony_ciextern void kvmppc_bookehv_exit(void);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ciextern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciextern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
2218c2ecf20Sopenharmony_ciextern long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
2228c2ecf20Sopenharmony_ci					    struct kvm_ppc_resize_hpt *rhpt);
2238c2ecf20Sopenharmony_ciextern long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
2248c2ecf20Sopenharmony_ci					   struct kvm_ppc_resize_hpt *rhpt);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciint kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciextern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
2298c2ecf20Sopenharmony_ciextern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
2308c2ecf20Sopenharmony_ciextern void kvmppc_rtas_tokens_free(struct kvm *kvm);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciextern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
2338c2ecf20Sopenharmony_ci				u32 priority);
2348c2ecf20Sopenharmony_ciextern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
2358c2ecf20Sopenharmony_ci				u32 *priority);
2368c2ecf20Sopenharmony_ciextern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
2378c2ecf20Sopenharmony_ciextern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_civoid kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
2408c2ecf20Sopenharmony_civoid kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ciunion kvmppc_one_reg {
2438c2ecf20Sopenharmony_ci	u32	wval;
2448c2ecf20Sopenharmony_ci	u64	dval;
2458c2ecf20Sopenharmony_ci	vector128 vval;
2468c2ecf20Sopenharmony_ci	u64	vsxval[2];
2478c2ecf20Sopenharmony_ci	u32	vsx32val[4];
2488c2ecf20Sopenharmony_ci	u16	vsx16val[8];
2498c2ecf20Sopenharmony_ci	u8	vsx8val[16];
2508c2ecf20Sopenharmony_ci	struct {
2518c2ecf20Sopenharmony_ci		u64	addr;
2528c2ecf20Sopenharmony_ci		u64	length;
2538c2ecf20Sopenharmony_ci	}	vpaval;
2548c2ecf20Sopenharmony_ci	u64	xive_timaval[2];
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistruct kvmppc_ops {
2588c2ecf20Sopenharmony_ci	struct module *owner;
2598c2ecf20Sopenharmony_ci	int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
2608c2ecf20Sopenharmony_ci	int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
2618c2ecf20Sopenharmony_ci	int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
2628c2ecf20Sopenharmony_ci			   union kvmppc_one_reg *val);
2638c2ecf20Sopenharmony_ci	int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
2648c2ecf20Sopenharmony_ci			   union kvmppc_one_reg *val);
2658c2ecf20Sopenharmony_ci	void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
2668c2ecf20Sopenharmony_ci	void (*vcpu_put)(struct kvm_vcpu *vcpu);
2678c2ecf20Sopenharmony_ci	void (*inject_interrupt)(struct kvm_vcpu *vcpu, int vec, u64 srr1_flags);
2688c2ecf20Sopenharmony_ci	void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
2698c2ecf20Sopenharmony_ci	int (*vcpu_run)(struct kvm_vcpu *vcpu);
2708c2ecf20Sopenharmony_ci	int (*vcpu_create)(struct kvm_vcpu *vcpu);
2718c2ecf20Sopenharmony_ci	void (*vcpu_free)(struct kvm_vcpu *vcpu);
2728c2ecf20Sopenharmony_ci	int (*check_requests)(struct kvm_vcpu *vcpu);
2738c2ecf20Sopenharmony_ci	int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
2748c2ecf20Sopenharmony_ci	void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
2758c2ecf20Sopenharmony_ci	int (*prepare_memory_region)(struct kvm *kvm,
2768c2ecf20Sopenharmony_ci				     struct kvm_memory_slot *memslot,
2778c2ecf20Sopenharmony_ci				     const struct kvm_userspace_memory_region *mem,
2788c2ecf20Sopenharmony_ci				     enum kvm_mr_change change);
2798c2ecf20Sopenharmony_ci	void (*commit_memory_region)(struct kvm *kvm,
2808c2ecf20Sopenharmony_ci				     const struct kvm_userspace_memory_region *mem,
2818c2ecf20Sopenharmony_ci				     const struct kvm_memory_slot *old,
2828c2ecf20Sopenharmony_ci				     const struct kvm_memory_slot *new,
2838c2ecf20Sopenharmony_ci				     enum kvm_mr_change change);
2848c2ecf20Sopenharmony_ci	int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
2858c2ecf20Sopenharmony_ci			   unsigned long end);
2868c2ecf20Sopenharmony_ci	int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
2878c2ecf20Sopenharmony_ci	int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
2888c2ecf20Sopenharmony_ci	void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
2898c2ecf20Sopenharmony_ci	void (*free_memslot)(struct kvm_memory_slot *slot);
2908c2ecf20Sopenharmony_ci	int (*init_vm)(struct kvm *kvm);
2918c2ecf20Sopenharmony_ci	void (*destroy_vm)(struct kvm *kvm);
2928c2ecf20Sopenharmony_ci	int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
2938c2ecf20Sopenharmony_ci	int (*emulate_op)(struct kvm_vcpu *vcpu,
2948c2ecf20Sopenharmony_ci			  unsigned int inst, int *advance);
2958c2ecf20Sopenharmony_ci	int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
2968c2ecf20Sopenharmony_ci	int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
2978c2ecf20Sopenharmony_ci	void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
2988c2ecf20Sopenharmony_ci	long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
2998c2ecf20Sopenharmony_ci			      unsigned long arg);
3008c2ecf20Sopenharmony_ci	int (*hcall_implemented)(unsigned long hcall);
3018c2ecf20Sopenharmony_ci	int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
3028c2ecf20Sopenharmony_ci				       struct irq_bypass_producer *);
3038c2ecf20Sopenharmony_ci	void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
3048c2ecf20Sopenharmony_ci					struct irq_bypass_producer *);
3058c2ecf20Sopenharmony_ci	int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
3068c2ecf20Sopenharmony_ci	int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
3078c2ecf20Sopenharmony_ci	int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
3088c2ecf20Sopenharmony_ci			    unsigned long flags);
3098c2ecf20Sopenharmony_ci	void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
3108c2ecf20Sopenharmony_ci	int (*enable_nested)(struct kvm *kvm);
3118c2ecf20Sopenharmony_ci	int (*load_from_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
3128c2ecf20Sopenharmony_ci			       int size);
3138c2ecf20Sopenharmony_ci	int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
3148c2ecf20Sopenharmony_ci			      int size);
3158c2ecf20Sopenharmony_ci	int (*enable_svm)(struct kvm *kvm);
3168c2ecf20Sopenharmony_ci	int (*svm_off)(struct kvm *kvm);
3178c2ecf20Sopenharmony_ci};
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ciextern struct kvmppc_ops *kvmppc_hv_ops;
3208c2ecf20Sopenharmony_ciextern struct kvmppc_ops *kvmppc_pr_ops;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
3238c2ecf20Sopenharmony_ci				enum instruction_fetch_type type, u32 *inst)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	int ret = EMULATE_DONE;
3268c2ecf20Sopenharmony_ci	u32 fetched_inst;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Load the instruction manually if it failed to do so in the
3298c2ecf20Sopenharmony_ci	 * exit path */
3308c2ecf20Sopenharmony_ci	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
3318c2ecf20Sopenharmony_ci		ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/*  Write fetch_failed unswapped if the fetch failed */
3348c2ecf20Sopenharmony_ci	if (ret == EMULATE_DONE)
3358c2ecf20Sopenharmony_ci		fetched_inst = kvmppc_need_byteswap(vcpu) ?
3368c2ecf20Sopenharmony_ci				swab32(vcpu->arch.last_inst) :
3378c2ecf20Sopenharmony_ci				vcpu->arch.last_inst;
3388c2ecf20Sopenharmony_ci	else
3398c2ecf20Sopenharmony_ci		fetched_inst = vcpu->arch.last_inst;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	*inst = fetched_inst;
3428c2ecf20Sopenharmony_ci	return ret;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	return kvm->arch.kvm_ops == kvmppc_hv_ops;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciextern int kvmppc_hwrng_present(void);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/*
3538c2ecf20Sopenharmony_ci * Cuts out inst bits with ordering according to spec.
3548c2ecf20Sopenharmony_ci * That means the leftmost bit is zero. All given bits are included.
3558c2ecf20Sopenharmony_ci */
3568c2ecf20Sopenharmony_cistatic inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	u32 r;
3598c2ecf20Sopenharmony_ci	u32 mask;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	BUG_ON(msb > lsb);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	mask = (1 << (lsb - msb + 1)) - 1;
3648c2ecf20Sopenharmony_ci	r = (inst >> (63 - lsb)) & mask;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return r;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/*
3708c2ecf20Sopenharmony_ci * Replaces inst bits with ordering according to spec.
3718c2ecf20Sopenharmony_ci */
3728c2ecf20Sopenharmony_cistatic inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	u32 r;
3758c2ecf20Sopenharmony_ci	u32 mask;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	BUG_ON(msb > lsb);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
3808c2ecf20Sopenharmony_ci	r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	return r;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci#define one_reg_size(id)	\
3868c2ecf20Sopenharmony_ci	(1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci#define get_reg_val(id, reg)	({		\
3898c2ecf20Sopenharmony_ci	union kvmppc_one_reg __u;		\
3908c2ecf20Sopenharmony_ci	switch (one_reg_size(id)) {		\
3918c2ecf20Sopenharmony_ci	case 4: __u.wval = (reg); break;	\
3928c2ecf20Sopenharmony_ci	case 8: __u.dval = (reg); break;	\
3938c2ecf20Sopenharmony_ci	default: BUG();				\
3948c2ecf20Sopenharmony_ci	}					\
3958c2ecf20Sopenharmony_ci	__u;					\
3968c2ecf20Sopenharmony_ci})
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci#define set_reg_val(id, val)	({		\
4008c2ecf20Sopenharmony_ci	u64 __v;				\
4018c2ecf20Sopenharmony_ci	switch (one_reg_size(id)) {		\
4028c2ecf20Sopenharmony_ci	case 4: __v = (val).wval; break;	\
4038c2ecf20Sopenharmony_ci	case 8: __v = (val).dval; break;	\
4048c2ecf20Sopenharmony_ci	default: BUG();				\
4058c2ecf20Sopenharmony_ci	}					\
4068c2ecf20Sopenharmony_ci	__v;					\
4078c2ecf20Sopenharmony_ci})
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ciint kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
4108c2ecf20Sopenharmony_ciint kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ciint kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
4138c2ecf20Sopenharmony_ciint kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ciint kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
4168c2ecf20Sopenharmony_ciint kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
4178c2ecf20Sopenharmony_ciint kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
4188c2ecf20Sopenharmony_ciint kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_civoid kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistruct openpic;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
4258c2ecf20Sopenharmony_ciextern void kvm_cma_reserve(void) __init;
4268c2ecf20Sopenharmony_cistatic inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic inline void kvmppc_set_xive_tima(int cpu,
4328c2ecf20Sopenharmony_ci					unsigned long phys_addr,
4338c2ecf20Sopenharmony_ci					void __iomem *virt_addr)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
4368c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic inline u32 kvmppc_get_xics_latch(void)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	u32 xirr;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	xirr = get_paca()->kvm_hstate.saved_xirr;
4448c2ecf20Sopenharmony_ci	get_paca()->kvm_hstate.saved_xirr = 0;
4458c2ecf20Sopenharmony_ci	return xirr;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci/*
4498c2ecf20Sopenharmony_ci * To avoid the need to unnecessarily exit fully to the host kernel, an IPI to
4508c2ecf20Sopenharmony_ci * a CPU thread that's running/napping inside of a guest is by default regarded
4518c2ecf20Sopenharmony_ci * as a request to wake the CPU (if needed) and continue execution within the
4528c2ecf20Sopenharmony_ci * guest, potentially to process new state like externally-generated
4538c2ecf20Sopenharmony_ci * interrupts or IPIs sent from within the guest itself (e.g. H_PROD/H_IPI).
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * To force an exit to the host kernel, kvmppc_set_host_ipi() must be called
4568c2ecf20Sopenharmony_ci * prior to issuing the IPI to set the corresponding 'host_ipi' flag in the
4578c2ecf20Sopenharmony_ci * target CPU's PACA. To avoid unnecessary exits to the host, this flag should
4588c2ecf20Sopenharmony_ci * be immediately cleared via kvmppc_clear_host_ipi() by the IPI handler on
4598c2ecf20Sopenharmony_ci * the receiving side prior to processing the IPI work.
4608c2ecf20Sopenharmony_ci *
4618c2ecf20Sopenharmony_ci * NOTE:
4628c2ecf20Sopenharmony_ci *
4638c2ecf20Sopenharmony_ci * We currently issue an smp_mb() at the beginning of kvmppc_set_host_ipi().
4648c2ecf20Sopenharmony_ci * This is to guard against sequences such as the following:
4658c2ecf20Sopenharmony_ci *
4668c2ecf20Sopenharmony_ci *      CPU
4678c2ecf20Sopenharmony_ci *        X: smp_muxed_ipi_set_message():
4688c2ecf20Sopenharmony_ci *        X:   smp_mb()
4698c2ecf20Sopenharmony_ci *        X:   message[RESCHEDULE] = 1
4708c2ecf20Sopenharmony_ci *        X: doorbell_global_ipi(42):
4718c2ecf20Sopenharmony_ci *        X:   kvmppc_set_host_ipi(42)
4728c2ecf20Sopenharmony_ci *        X:   ppc_msgsnd_sync()/smp_mb()
4738c2ecf20Sopenharmony_ci *        X:   ppc_msgsnd() -> 42
4748c2ecf20Sopenharmony_ci *       42: doorbell_exception(): // from CPU X
4758c2ecf20Sopenharmony_ci *       42:   ppc_msgsync()
4768c2ecf20Sopenharmony_ci *      105: smp_muxed_ipi_set_message():
4778c2ecf20Sopenharmony_ci *      105:   smb_mb()
4788c2ecf20Sopenharmony_ci *           // STORE DEFERRED DUE TO RE-ORDERING
4798c2ecf20Sopenharmony_ci *    --105:   message[CALL_FUNCTION] = 1
4808c2ecf20Sopenharmony_ci *    | 105: doorbell_global_ipi(42):
4818c2ecf20Sopenharmony_ci *    | 105:   kvmppc_set_host_ipi(42)
4828c2ecf20Sopenharmony_ci *    |  42:   kvmppc_clear_host_ipi(42)
4838c2ecf20Sopenharmony_ci *    |  42: smp_ipi_demux_relaxed()
4848c2ecf20Sopenharmony_ci *    |  42: // returns to executing guest
4858c2ecf20Sopenharmony_ci *    |      // RE-ORDERED STORE COMPLETES
4868c2ecf20Sopenharmony_ci *    ->105:   message[CALL_FUNCTION] = 1
4878c2ecf20Sopenharmony_ci *      105:   ppc_msgsnd_sync()/smp_mb()
4888c2ecf20Sopenharmony_ci *      105:   ppc_msgsnd() -> 42
4898c2ecf20Sopenharmony_ci *       42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
4908c2ecf20Sopenharmony_ci *      105: // hangs waiting on 42 to process messages/call_single_queue
4918c2ecf20Sopenharmony_ci *
4928c2ecf20Sopenharmony_ci * We also issue an smp_mb() at the end of kvmppc_clear_host_ipi(). This is
4938c2ecf20Sopenharmony_ci * to guard against sequences such as the following (as well as to create
4948c2ecf20Sopenharmony_ci * a read-side pairing with the barrier in kvmppc_set_host_ipi()):
4958c2ecf20Sopenharmony_ci *
4968c2ecf20Sopenharmony_ci *      CPU
4978c2ecf20Sopenharmony_ci *        X: smp_muxed_ipi_set_message():
4988c2ecf20Sopenharmony_ci *        X:   smp_mb()
4998c2ecf20Sopenharmony_ci *        X:   message[RESCHEDULE] = 1
5008c2ecf20Sopenharmony_ci *        X: doorbell_global_ipi(42):
5018c2ecf20Sopenharmony_ci *        X:   kvmppc_set_host_ipi(42)
5028c2ecf20Sopenharmony_ci *        X:   ppc_msgsnd_sync()/smp_mb()
5038c2ecf20Sopenharmony_ci *        X:   ppc_msgsnd() -> 42
5048c2ecf20Sopenharmony_ci *       42: doorbell_exception(): // from CPU X
5058c2ecf20Sopenharmony_ci *       42:   ppc_msgsync()
5068c2ecf20Sopenharmony_ci *           // STORE DEFERRED DUE TO RE-ORDERING
5078c2ecf20Sopenharmony_ci *    -- 42:   kvmppc_clear_host_ipi(42)
5088c2ecf20Sopenharmony_ci *    |  42: smp_ipi_demux_relaxed()
5098c2ecf20Sopenharmony_ci *    | 105: smp_muxed_ipi_set_message():
5108c2ecf20Sopenharmony_ci *    | 105:   smb_mb()
5118c2ecf20Sopenharmony_ci *    | 105:   message[CALL_FUNCTION] = 1
5128c2ecf20Sopenharmony_ci *    | 105: doorbell_global_ipi(42):
5138c2ecf20Sopenharmony_ci *    | 105:   kvmppc_set_host_ipi(42)
5148c2ecf20Sopenharmony_ci *    |      // RE-ORDERED STORE COMPLETES
5158c2ecf20Sopenharmony_ci *    -> 42:   kvmppc_clear_host_ipi(42)
5168c2ecf20Sopenharmony_ci *       42: // returns to executing guest
5178c2ecf20Sopenharmony_ci *      105:   ppc_msgsnd_sync()/smp_mb()
5188c2ecf20Sopenharmony_ci *      105:   ppc_msgsnd() -> 42
5198c2ecf20Sopenharmony_ci *       42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
5208c2ecf20Sopenharmony_ci *      105: // hangs waiting on 42 to process messages/call_single_queue
5218c2ecf20Sopenharmony_ci */
5228c2ecf20Sopenharmony_cistatic inline void kvmppc_set_host_ipi(int cpu)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	/*
5258c2ecf20Sopenharmony_ci	 * order stores of IPI messages vs. setting of host_ipi flag
5268c2ecf20Sopenharmony_ci	 *
5278c2ecf20Sopenharmony_ci	 * pairs with the barrier in kvmppc_clear_host_ipi()
5288c2ecf20Sopenharmony_ci	 */
5298c2ecf20Sopenharmony_ci	smp_mb();
5308c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->kvm_hstate.host_ipi = 1;
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic inline void kvmppc_clear_host_ipi(int cpu)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->kvm_hstate.host_ipi = 0;
5368c2ecf20Sopenharmony_ci	/*
5378c2ecf20Sopenharmony_ci	 * order clearing of host_ipi flag vs. processing of IPI messages
5388c2ecf20Sopenharmony_ci	 *
5398c2ecf20Sopenharmony_ci	 * pairs with the barrier in kvmppc_set_host_ipi()
5408c2ecf20Sopenharmony_ci	 */
5418c2ecf20Sopenharmony_ci	smp_mb();
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ciextern void kvm_hv_vm_activated(void);
5508c2ecf20Sopenharmony_ciextern void kvm_hv_vm_deactivated(void);
5518c2ecf20Sopenharmony_ciextern bool kvm_hv_mode_active(void);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ciextern void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
5548c2ecf20Sopenharmony_ci					struct kvm_nested_guest *nested);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci#else
5578c2ecf20Sopenharmony_cistatic inline void __init kvm_cma_reserve(void)
5588c2ecf20Sopenharmony_ci{}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
5618c2ecf20Sopenharmony_ci{}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic inline void kvmppc_set_xive_tima(int cpu,
5648c2ecf20Sopenharmony_ci					unsigned long phys_addr,
5658c2ecf20Sopenharmony_ci					void __iomem *virt_addr)
5668c2ecf20Sopenharmony_ci{}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic inline u32 kvmppc_get_xics_latch(void)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	return 0;
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic inline void kvmppc_set_host_ipi(int cpu)
5748c2ecf20Sopenharmony_ci{}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic inline void kvmppc_clear_host_ipi(int cpu)
5778c2ecf20Sopenharmony_ci{}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	kvm_vcpu_kick(vcpu);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic inline bool kvm_hv_mode_active(void)		{ return false; }
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci#endif
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_XICS
5898c2ecf20Sopenharmony_cistatic inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
5958c2ecf20Sopenharmony_ci				struct kvm *kvm)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	if (kvm && kvm_irq_bypass)
5988c2ecf20Sopenharmony_ci		return kvm->arch.pimap;
5998c2ecf20Sopenharmony_ci	return NULL;
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ciextern void kvmppc_alloc_host_rm_ops(void);
6038c2ecf20Sopenharmony_ciextern void kvmppc_free_host_rm_ops(void);
6048c2ecf20Sopenharmony_ciextern void kvmppc_free_pimap(struct kvm *kvm);
6058c2ecf20Sopenharmony_ciextern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
6068c2ecf20Sopenharmony_ciextern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
6078c2ecf20Sopenharmony_ciextern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
6088c2ecf20Sopenharmony_ciextern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
6098c2ecf20Sopenharmony_ciextern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
6108c2ecf20Sopenharmony_ciextern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
6118c2ecf20Sopenharmony_ci			struct kvm_vcpu *vcpu, u32 cpu);
6128c2ecf20Sopenharmony_ciextern void kvmppc_xics_ipi_action(void);
6138c2ecf20Sopenharmony_ciextern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
6148c2ecf20Sopenharmony_ci				   unsigned long host_irq);
6158c2ecf20Sopenharmony_ciextern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
6168c2ecf20Sopenharmony_ci				   unsigned long host_irq);
6178c2ecf20Sopenharmony_ciextern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
6188c2ecf20Sopenharmony_ci					struct kvmppc_irq_map *irq_map,
6198c2ecf20Sopenharmony_ci					struct kvmppc_passthru_irqmap *pimap,
6208c2ecf20Sopenharmony_ci					bool *again);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ciextern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
6238c2ecf20Sopenharmony_ci			       int level, bool line_status);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ciextern int h_ipi_redirect;
6268c2ecf20Sopenharmony_ci#else
6278c2ecf20Sopenharmony_cistatic inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
6288c2ecf20Sopenharmony_ci				struct kvm *kvm)
6298c2ecf20Sopenharmony_ci	{ return NULL; }
6308c2ecf20Sopenharmony_cistatic inline void kvmppc_alloc_host_rm_ops(void) {};
6318c2ecf20Sopenharmony_cistatic inline void kvmppc_free_host_rm_ops(void) {};
6328c2ecf20Sopenharmony_cistatic inline void kvmppc_free_pimap(struct kvm *kvm) {};
6338c2ecf20Sopenharmony_cistatic inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
6348c2ecf20Sopenharmony_ci	{ return 0; }
6358c2ecf20Sopenharmony_cistatic inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
6368c2ecf20Sopenharmony_ci	{ return 0; }
6378c2ecf20Sopenharmony_cistatic inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
6388c2ecf20Sopenharmony_cistatic inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
6398c2ecf20Sopenharmony_ci	{ return 0; }
6408c2ecf20Sopenharmony_ci#endif
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_XIVE
6438c2ecf20Sopenharmony_ci/*
6448c2ecf20Sopenharmony_ci * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
6458c2ecf20Sopenharmony_ci * ie. P9 new interrupt controller, while the second "xive" is the legacy
6468c2ecf20Sopenharmony_ci * "eXternal Interrupt Vector Entry" which is the configuration of an
6478c2ecf20Sopenharmony_ci * interrupt on the "xics" interrupt controller on P8 and earlier. Those
6488c2ecf20Sopenharmony_ci * two function consume or produce a legacy "XIVE" state from the
6498c2ecf20Sopenharmony_ci * new "XIVE" interrupt controller.
6508c2ecf20Sopenharmony_ci */
6518c2ecf20Sopenharmony_ciextern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
6528c2ecf20Sopenharmony_ci				u32 priority);
6538c2ecf20Sopenharmony_ciextern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
6548c2ecf20Sopenharmony_ci				u32 *priority);
6558c2ecf20Sopenharmony_ciextern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
6568c2ecf20Sopenharmony_ciextern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
6578c2ecf20Sopenharmony_ciextern void kvmppc_xive_init_module(void);
6588c2ecf20Sopenharmony_ciextern void kvmppc_xive_exit_module(void);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ciextern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
6618c2ecf20Sopenharmony_ci				    struct kvm_vcpu *vcpu, u32 cpu);
6628c2ecf20Sopenharmony_ciextern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
6638c2ecf20Sopenharmony_ciextern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
6648c2ecf20Sopenharmony_ci				  struct irq_desc *host_desc);
6658c2ecf20Sopenharmony_ciextern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
6668c2ecf20Sopenharmony_ci				  struct irq_desc *host_desc);
6678c2ecf20Sopenharmony_ciextern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
6688c2ecf20Sopenharmony_ciextern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ciextern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
6718c2ecf20Sopenharmony_ci			       int level, bool line_status);
6728c2ecf20Sopenharmony_ciextern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ciextern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
6808c2ecf20Sopenharmony_ci					   struct kvm_vcpu *vcpu, u32 cpu);
6818c2ecf20Sopenharmony_ciextern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
6828c2ecf20Sopenharmony_ciextern void kvmppc_xive_native_init_module(void);
6838c2ecf20Sopenharmony_ciextern void kvmppc_xive_native_exit_module(void);
6848c2ecf20Sopenharmony_ciextern int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
6858c2ecf20Sopenharmony_ci				     union kvmppc_one_reg *val);
6868c2ecf20Sopenharmony_ciextern int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
6878c2ecf20Sopenharmony_ci				     union kvmppc_one_reg *val);
6888c2ecf20Sopenharmony_ciextern bool kvmppc_xive_native_supported(void);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci#else
6918c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
6928c2ecf20Sopenharmony_ci				       u32 priority) { return -1; }
6938c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
6948c2ecf20Sopenharmony_ci				       u32 *priority) { return -1; }
6958c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
6968c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
6978c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_init_module(void) { }
6988c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_exit_module(void) { }
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
7018c2ecf20Sopenharmony_ci					   struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
7028c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
7038c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
7048c2ecf20Sopenharmony_ci					 struct irq_desc *host_desc) { return -ENODEV; }
7058c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
7068c2ecf20Sopenharmony_ci					 struct irq_desc *host_desc) { return -ENODEV; }
7078c2ecf20Sopenharmony_cistatic inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
7088c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
7118c2ecf20Sopenharmony_ci				      int level, bool line_status) { return -ENODEV; }
7128c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
7158c2ecf20Sopenharmony_ci	{ return 0; }
7168c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
7178c2ecf20Sopenharmony_ci			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
7188c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
7198c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_native_init_module(void) { }
7208c2ecf20Sopenharmony_cistatic inline void kvmppc_xive_native_exit_module(void) { }
7218c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
7228c2ecf20Sopenharmony_ci					    union kvmppc_one_reg *val)
7238c2ecf20Sopenharmony_ci{ return 0; }
7248c2ecf20Sopenharmony_cistatic inline int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
7258c2ecf20Sopenharmony_ci					    union kvmppc_one_reg *val)
7268c2ecf20Sopenharmony_ci{ return -ENOENT; }
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci#endif /* CONFIG_KVM_XIVE */
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_POWERNV) && defined(CONFIG_KVM_BOOK3S_64_HANDLER)
7318c2ecf20Sopenharmony_cistatic inline bool xics_on_xive(void)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	return xive_enabled() && cpu_has_feature(CPU_FTR_HVMODE);
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci#else
7368c2ecf20Sopenharmony_cistatic inline bool xics_on_xive(void)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	return false;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci#endif
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci/*
7438c2ecf20Sopenharmony_ci * Prototypes for functions called only from assembler code.
7448c2ecf20Sopenharmony_ci * Having prototypes reduces sparse errors.
7458c2ecf20Sopenharmony_ci */
7468c2ecf20Sopenharmony_cilong kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
7478c2ecf20Sopenharmony_ci			 unsigned long ioba, unsigned long tce);
7488c2ecf20Sopenharmony_cilong kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
7498c2ecf20Sopenharmony_ci				  unsigned long liobn, unsigned long ioba,
7508c2ecf20Sopenharmony_ci				  unsigned long tce_list, unsigned long npages);
7518c2ecf20Sopenharmony_cilong kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
7528c2ecf20Sopenharmony_ci			   unsigned long liobn, unsigned long ioba,
7538c2ecf20Sopenharmony_ci			   unsigned long tce_value, unsigned long npages);
7548c2ecf20Sopenharmony_cilong int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
7558c2ecf20Sopenharmony_ci                            unsigned int yield_count);
7568c2ecf20Sopenharmony_cilong kvmppc_h_random(struct kvm_vcpu *vcpu);
7578c2ecf20Sopenharmony_civoid kvmhv_commence_exit(int trap);
7588c2ecf20Sopenharmony_civoid kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
7598c2ecf20Sopenharmony_civoid kvmppc_subcore_enter_guest(void);
7608c2ecf20Sopenharmony_civoid kvmppc_subcore_exit_guest(void);
7618c2ecf20Sopenharmony_cilong kvmppc_realmode_hmi_handler(void);
7628c2ecf20Sopenharmony_cilong kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
7638c2ecf20Sopenharmony_ci                    long pte_index, unsigned long pteh, unsigned long ptel);
7648c2ecf20Sopenharmony_cilong kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
7658c2ecf20Sopenharmony_ci                     unsigned long pte_index, unsigned long avpn);
7668c2ecf20Sopenharmony_cilong kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
7678c2ecf20Sopenharmony_cilong kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
7688c2ecf20Sopenharmony_ci                      unsigned long pte_index, unsigned long avpn,
7698c2ecf20Sopenharmony_ci                      unsigned long va);
7708c2ecf20Sopenharmony_cilong kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
7718c2ecf20Sopenharmony_ci                   unsigned long pte_index);
7728c2ecf20Sopenharmony_cilong kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
7738c2ecf20Sopenharmony_ci                        unsigned long pte_index);
7748c2ecf20Sopenharmony_cilong kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
7758c2ecf20Sopenharmony_ci                        unsigned long pte_index);
7768c2ecf20Sopenharmony_cilong kvmppc_rm_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
7778c2ecf20Sopenharmony_ci			   unsigned long dest, unsigned long src);
7788c2ecf20Sopenharmony_cilong kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
7798c2ecf20Sopenharmony_ci                          unsigned long slb_v, unsigned int status, bool data);
7808c2ecf20Sopenharmony_ciunsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu);
7818c2ecf20Sopenharmony_ciunsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu);
7828c2ecf20Sopenharmony_ciunsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
7838c2ecf20Sopenharmony_ciint kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
7848c2ecf20Sopenharmony_ci                    unsigned long mfrr);
7858c2ecf20Sopenharmony_ciint kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
7868c2ecf20Sopenharmony_ciint kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
7878c2ecf20Sopenharmony_civoid kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci/*
7908c2ecf20Sopenharmony_ci * Host-side operations we want to set up while running in real
7918c2ecf20Sopenharmony_ci * mode in the guest operating on the xics.
7928c2ecf20Sopenharmony_ci * Currently only VCPU wakeup is supported.
7938c2ecf20Sopenharmony_ci */
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ciunion kvmppc_rm_state {
7968c2ecf20Sopenharmony_ci	unsigned long raw;
7978c2ecf20Sopenharmony_ci	struct {
7988c2ecf20Sopenharmony_ci		u32 in_host;
7998c2ecf20Sopenharmony_ci		u32 rm_action;
8008c2ecf20Sopenharmony_ci	};
8018c2ecf20Sopenharmony_ci};
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistruct kvmppc_host_rm_core {
8048c2ecf20Sopenharmony_ci	union kvmppc_rm_state rm_state;
8058c2ecf20Sopenharmony_ci	void *rm_data;
8068c2ecf20Sopenharmony_ci	char pad[112];
8078c2ecf20Sopenharmony_ci};
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistruct kvmppc_host_rm_ops {
8108c2ecf20Sopenharmony_ci	struct kvmppc_host_rm_core	*rm_core;
8118c2ecf20Sopenharmony_ci	void		(*vcpu_kick)(struct kvm_vcpu *vcpu);
8128c2ecf20Sopenharmony_ci};
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ciextern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_cistatic inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_BOOKE_HV
8198c2ecf20Sopenharmony_ci	return mfspr(SPRN_GEPR);
8208c2ecf20Sopenharmony_ci#elif defined(CONFIG_BOOKE)
8218c2ecf20Sopenharmony_ci	return vcpu->arch.epr;
8228c2ecf20Sopenharmony_ci#else
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci#endif
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_BOOKE_HV
8308c2ecf20Sopenharmony_ci	mtspr(SPRN_GEPR, epr);
8318c2ecf20Sopenharmony_ci#elif defined(CONFIG_BOOKE)
8328c2ecf20Sopenharmony_ci	vcpu->arch.epr = epr;
8338c2ecf20Sopenharmony_ci#endif
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_MPIC
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_civoid kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
8398c2ecf20Sopenharmony_ciint kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
8408c2ecf20Sopenharmony_ci			     u32 cpu);
8418c2ecf20Sopenharmony_civoid kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci#else
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
8508c2ecf20Sopenharmony_ci		struct kvm_vcpu *vcpu, u32 cpu)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	return -EINVAL;
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
8568c2ecf20Sopenharmony_ci		struct kvm_vcpu *vcpu)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci#endif /* CONFIG_KVM_MPIC */
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ciint kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
8638c2ecf20Sopenharmony_ci			      struct kvm_config_tlb *cfg);
8648c2ecf20Sopenharmony_ciint kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
8658c2ecf20Sopenharmony_ci			     struct kvm_dirty_tlb *cfg);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_cilong kvmppc_alloc_lpid(void);
8688c2ecf20Sopenharmony_civoid kvmppc_claim_lpid(long lpid);
8698c2ecf20Sopenharmony_civoid kvmppc_free_lpid(long lpid);
8708c2ecf20Sopenharmony_civoid kvmppc_init_lpid(unsigned long nr_lpids);
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	struct page *page;
8758c2ecf20Sopenharmony_ci	/*
8768c2ecf20Sopenharmony_ci	 * We can only access pages that the kernel maps
8778c2ecf20Sopenharmony_ci	 * as memory. Bail out for unmapped ones.
8788c2ecf20Sopenharmony_ci	 */
8798c2ecf20Sopenharmony_ci	if (!pfn_valid(pfn))
8808c2ecf20Sopenharmony_ci		return;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	/* Clear i-cache for new pages */
8838c2ecf20Sopenharmony_ci	page = pfn_to_page(pfn);
8848c2ecf20Sopenharmony_ci	if (!test_bit(PG_arch_1, &page->flags)) {
8858c2ecf20Sopenharmony_ci		flush_dcache_icache_page(page);
8868c2ecf20Sopenharmony_ci		set_bit(PG_arch_1, &page->flags);
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci/*
8918c2ecf20Sopenharmony_ci * Shared struct helpers. The shared struct can be little or big endian,
8928c2ecf20Sopenharmony_ci * depending on the guest endianness. So expose helpers to all of them.
8938c2ecf20Sopenharmony_ci */
8948c2ecf20Sopenharmony_cistatic inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
8978c2ecf20Sopenharmony_ci	/* Only Book3S_64 PR supports bi-endian for now */
8988c2ecf20Sopenharmony_ci	return vcpu->arch.shared_big_endian;
8998c2ecf20Sopenharmony_ci#elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
9008c2ecf20Sopenharmony_ci	/* Book3s_64 HV on little endian is always little endian */
9018c2ecf20Sopenharmony_ci	return false;
9028c2ecf20Sopenharmony_ci#else
9038c2ecf20Sopenharmony_ci	return true;
9048c2ecf20Sopenharmony_ci#endif
9058c2ecf20Sopenharmony_ci}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci#define SPRNG_WRAPPER_GET(reg, bookehv_spr)				\
9088c2ecf20Sopenharmony_cistatic inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu)		\
9098c2ecf20Sopenharmony_ci{									\
9108c2ecf20Sopenharmony_ci	return mfspr(bookehv_spr);					\
9118c2ecf20Sopenharmony_ci}									\
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci#define SPRNG_WRAPPER_SET(reg, bookehv_spr)				\
9148c2ecf20Sopenharmony_cistatic inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val)	\
9158c2ecf20Sopenharmony_ci{									\
9168c2ecf20Sopenharmony_ci	mtspr(bookehv_spr, val);						\
9178c2ecf20Sopenharmony_ci}									\
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci#define SHARED_WRAPPER_GET(reg, size)					\
9208c2ecf20Sopenharmony_cistatic inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu)		\
9218c2ecf20Sopenharmony_ci{									\
9228c2ecf20Sopenharmony_ci	if (kvmppc_shared_big_endian(vcpu))				\
9238c2ecf20Sopenharmony_ci	       return be##size##_to_cpu(vcpu->arch.shared->reg);	\
9248c2ecf20Sopenharmony_ci	else								\
9258c2ecf20Sopenharmony_ci	       return le##size##_to_cpu(vcpu->arch.shared->reg);	\
9268c2ecf20Sopenharmony_ci}									\
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci#define SHARED_WRAPPER_SET(reg, size)					\
9298c2ecf20Sopenharmony_cistatic inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val)	\
9308c2ecf20Sopenharmony_ci{									\
9318c2ecf20Sopenharmony_ci	if (kvmppc_shared_big_endian(vcpu))				\
9328c2ecf20Sopenharmony_ci	       vcpu->arch.shared->reg = cpu_to_be##size(val);		\
9338c2ecf20Sopenharmony_ci	else								\
9348c2ecf20Sopenharmony_ci	       vcpu->arch.shared->reg = cpu_to_le##size(val);		\
9358c2ecf20Sopenharmony_ci}									\
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci#define SHARED_WRAPPER(reg, size)					\
9388c2ecf20Sopenharmony_ci	SHARED_WRAPPER_GET(reg, size)					\
9398c2ecf20Sopenharmony_ci	SHARED_WRAPPER_SET(reg, size)					\
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci#define SPRNG_WRAPPER(reg, bookehv_spr)					\
9428c2ecf20Sopenharmony_ci	SPRNG_WRAPPER_GET(reg, bookehv_spr)				\
9438c2ecf20Sopenharmony_ci	SPRNG_WRAPPER_SET(reg, bookehv_spr)				\
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci#ifdef CONFIG_KVM_BOOKE_HV
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr)			\
9488c2ecf20Sopenharmony_ci	SPRNG_WRAPPER(reg, bookehv_spr)					\
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci#else
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr)			\
9538c2ecf20Sopenharmony_ci	SHARED_WRAPPER(reg, size)					\
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci#endif
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ciSHARED_WRAPPER(critical, 64)
9588c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
9598c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
9608c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
9618c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
9628c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
9638c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
9648c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
9658c2ecf20Sopenharmony_ciSHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
9668c2ecf20Sopenharmony_ciSHARED_WRAPPER_GET(msr, 64)
9678c2ecf20Sopenharmony_cistatic inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	if (kvmppc_shared_big_endian(vcpu))
9708c2ecf20Sopenharmony_ci	       vcpu->arch.shared->msr = cpu_to_be64(val);
9718c2ecf20Sopenharmony_ci	else
9728c2ecf20Sopenharmony_ci	       vcpu->arch.shared->msr = cpu_to_le64(val);
9738c2ecf20Sopenharmony_ci}
9748c2ecf20Sopenharmony_ciSHARED_WRAPPER(dsisr, 32)
9758c2ecf20Sopenharmony_ciSHARED_WRAPPER(int_pending, 32)
9768c2ecf20Sopenharmony_ciSHARED_WRAPPER(sprg4, 64)
9778c2ecf20Sopenharmony_ciSHARED_WRAPPER(sprg5, 64)
9788c2ecf20Sopenharmony_ciSHARED_WRAPPER(sprg6, 64)
9798c2ecf20Sopenharmony_ciSHARED_WRAPPER(sprg7, 64)
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	if (kvmppc_shared_big_endian(vcpu))
9848c2ecf20Sopenharmony_ci	       return be32_to_cpu(vcpu->arch.shared->sr[nr]);
9858c2ecf20Sopenharmony_ci	else
9868c2ecf20Sopenharmony_ci	       return le32_to_cpu(vcpu->arch.shared->sr[nr]);
9878c2ecf20Sopenharmony_ci}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_cistatic inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
9908c2ecf20Sopenharmony_ci{
9918c2ecf20Sopenharmony_ci	if (kvmppc_shared_big_endian(vcpu))
9928c2ecf20Sopenharmony_ci	       vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
9938c2ecf20Sopenharmony_ci	else
9948c2ecf20Sopenharmony_ci	       vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci/*
9988c2ecf20Sopenharmony_ci * Please call after prepare_to_enter. This function puts the lazy ee and irq
9998c2ecf20Sopenharmony_ci * disabled tracking state back to normal mode, without actually enabling
10008c2ecf20Sopenharmony_ci * interrupts.
10018c2ecf20Sopenharmony_ci */
10028c2ecf20Sopenharmony_cistatic inline void kvmppc_fix_ee_before_entry(void)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	trace_hardirqs_on();
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
10078c2ecf20Sopenharmony_ci	/*
10088c2ecf20Sopenharmony_ci	 * To avoid races, the caller must have gone directly from having
10098c2ecf20Sopenharmony_ci	 * interrupts fully-enabled to hard-disabled.
10108c2ecf20Sopenharmony_ci	 */
10118c2ecf20Sopenharmony_ci	WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	/* Only need to enable IRQs by hard enabling them after this */
10148c2ecf20Sopenharmony_ci	local_paca->irq_happened = 0;
10158c2ecf20Sopenharmony_ci	irq_soft_mask_set(IRQS_ENABLED);
10168c2ecf20Sopenharmony_ci#endif
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_cistatic inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
10208c2ecf20Sopenharmony_ci{
10218c2ecf20Sopenharmony_ci	ulong ea;
10228c2ecf20Sopenharmony_ci	ulong msr_64bit = 0;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	ea = kvmppc_get_gpr(vcpu, rb);
10258c2ecf20Sopenharmony_ci	if (ra)
10268c2ecf20Sopenharmony_ci		ea += kvmppc_get_gpr(vcpu, ra);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_BOOK3E_64)
10298c2ecf20Sopenharmony_ci	msr_64bit = MSR_CM;
10308c2ecf20Sopenharmony_ci#elif defined(CONFIG_PPC_BOOK3S_64)
10318c2ecf20Sopenharmony_ci	msr_64bit = MSR_SF;
10328c2ecf20Sopenharmony_ci#endif
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (!(kvmppc_get_msr(vcpu) & msr_64bit))
10358c2ecf20Sopenharmony_ci		ea = (uint32_t)ea;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	return ea;
10388c2ecf20Sopenharmony_ci}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ciextern void xics_wake_cpu(int cpu);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci#endif /* __POWERPC_KVM_PPC_H__ */
1043