powerpc/kvm/xics: Add kernel emulation for the XICS interrupt controller
Subsystems (3)
PATCH 4/7 powerpc/kvm/xics: Add kernel emulation for the XICS interrupt controller
This is an initial variant of the in-kernel XICS emulation for both HV and PR KVM running in PAPR mode.
This is based on an initial implementation by Michael Ellerman <[email protected]> reworked by myself.
It supports up to 4095 "BUID" (blocks of interrupts) of up to 4096 interrupts each.
Signed-off-by Benjamin Herrenschmidt <[email protected]>
--- arch/powerpc/include/asm/kvm.h | 39 ++ arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 30 +- arch/powerpc/kvm/Makefile | 1 + arch/powerpc/kvm/book3s.c | 3 +- arch/powerpc/kvm/book3s_hv.c | 20 + arch/powerpc/kvm/book3s_pr.c | 13 + arch/powerpc/kvm/book3s_pr_papr.c | 19 +- arch/powerpc/kvm/book3s_rtas.c | 51 +- arch/powerpc/kvm/book3s_xics.c | 882 +++++++++++++++++++++++++++++++++++ arch/powerpc/kvm/booke.c | 3 +- arch/powerpc/kvm/powerpc.c | 28 +- include/linux/kvm.h | 9 +- 13 files changed, 1090 insertions(+), 16 deletions(-) create mode 100644 arch/powerpc/kvm/book3s_xics.c
arch/powerpc/include/asm/kvm.h +39 -0
@@ -296,6 +296,45 @@ struct kvm_rtas_token_args {__u64 token;/* Use a token of 0 to undefine a mapping */ }; +/* for KVM_CAP_SPAPR_XICS */ +#define __KVM_HAVE_IRQCHIP_ARGS +struct kvm_irqchip_args { +#define KVM_IRQCHIP_TYPE_ICP 0/* XICS: ICP (presentation controller) */ +#define KVM_IRQCHIP_TYPE_ICS 1/* XICS: ICS (source controller) */ +__u32 type; +union { +/* XICS ICP arguments. This needs to be called once before +* creating any VCPU to initialize the main kernel XICS data +* structures. +*/ +struct { +#define KVM_ICP_FLAG_NOREALMODE 0x00000001/* Disable real mode ICP */ +__u32 flags; + } icp; + +/* XICS ICS arguments. You can call this for every BUID you +* want to make available. +* +* The BUID is 12 bits, the interrupt number within a BUID +* is up to 12 bits as well. The resulting interrupt numbers +* exposed to the guest are BUID || IRQ which is 24 bit +* +* BUID cannot be 0. +*/ +struct { +__u32 flags; +__u16 buid; +__u16 nr_irqs; + } ics; + }; +}; + +struct kvm_spapr_xics_xive { +__u32 irq; +__u32 server; +__u32 priority; +}; +struct kvm_book3e_206_tlb_entry {__u32 mas8;__u32 mas1;
arch/powerpc/include/asm/kvm_host.h +8 -0
@@ -187,6 +187,10 @@ struct kvmppc_linear_info {int type; }; +/* XICS components, defined in boo3s_xics.c */ +struct kvmppc_xics ; +struct kvmppc_icp ; +/* * The reverse mapping array has one entry for each HPTE, * which stores the guest's view of the second word of the HPTE
@@ -251,6 +255,7 @@ struct kvm_arch {#ifdef CONFIG_PPC_BOOK3S_64 struct list_head spapr_tce_tables;struct list_head rtas_tokens; +struct kvmppc_xics *xics;#endif };
@@ -532,6 +537,9 @@ struct kvm_vcpu_arch {u64 stolen_logged;struct kvmppc_vpa slb_shadow;#endif +#ifdef CONFIG_PPC_BOOK3S_64 +struct kvmppc_icp *icp;/* XICS presentation controller */ +#endif };/* Values for vcpu->arch.state */
arch/powerpc/include/asm/kvm_ppc.h +28 -2
@@ -102,8 +102,7 @@ extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);extern void kvmppc_core_dequeue_dec (struct kvm_vcpu *vcpu);extern void kvmppc_core_queue_external (struct kvm_vcpu *vcpu,struct kvm_interrupt *irq); -extern void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu, -struct kvm_interrupt *irq); +extern void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu);extern int kvmppc_core_emulate_op (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int op,int *advance);
@@ -127,6 +126,12 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern int kvmppc_xics_hcall (struct kvm_vcpu *vcpu,u32 cmd); +extern int kvmppc_xics_ioctl (struct kvm *kvm,unsigned ioctl,unsigned long arg); +extern void kvmppc_xics_free_icp (struct kvm_vcpu *vcpu); +extern int kvmppc_xics_create_icp (struct kvm_vcpu *vcpu); +extern void kvmppc_xics_free (struct kvm *kvm); +extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce *args); extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
@@ -152,6 +157,8 @@ extern void kvmppc_bookehv_exit(void);extern int kvm_vm_ioctl_rtas_define_token (struct kvm *kvm,void __user *argp);extern int kvmppc_rtas_hcall (struct kvm_vcpu *vcpu);extern void kvmppc_rtas_tokens_free (struct kvm *kvm); +extern int kvmppc_xics_set_xive (struct kvm *kvm,u32 irq,u32 server,u32 priority); +extern int kvmppc_xics_get_xive (struct kvm *kvm,u32 irq,u32 *server,u32 *priority);/* * Cuts out inst bits with ordering according to spec.
@@ -213,6 +220,25 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)static inline void kvm_linear_init (void ) {} + +#endif + +#ifdef CONFIG_PPC_BOOK3S_64 + +extern int kvmppc_xics_ioctl (struct kvm *kvm,unsigned ioctl,unsigned long arg); + +static inline int kvmppc_xics_enabled (struct kvm *kvm) +{ +return kvm->arch.xics !=NULL ; +} + +#else +static inline int kvmppc_xics_ioctl (struct kvm *kvm,unsigned ioctl, +unsigned long arg)\ +{ +return -ENOTTY ; +} +static inline int kvmppc_xics_enabled (struct kvm *kvm) {return 0 ; }#endif int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
arch/powerpc/kvm/Makefile +1 -0
@@ -81,6 +81,7 @@ kvm-book3s_64-module-objs := \ book3s.o \ book3s_64_vio.o \ book3s_rtas.o \ + book3s_xics.o \ $(kvm-book3s_64-objs-y) kvm-objs-$(CONFIG_KVM_BOOK3S_64) := $(kvm-book3s_64-module-objs)
arch/powerpc/kvm/book3s.c +1 -2
@@ -160,8 +160,7 @@ void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,kvmppc_book3s_queue_irqprio (vcpu, vec); } -void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu, -struct kvm_interrupt *irq) +void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu) {kvmppc_book3s_dequeue_irqprio (vcpu,BOOK3S_INTERRUPT_EXTERNAL );kvmppc_book3s_dequeue_irqprio (vcpu,BOOK3S_INTERRUPT_EXTERNAL_LEVEL );
arch/powerpc/kvm/book3s_hv.c +20 -0
@@ -465,6 +465,14 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)/* Send the error out to userspace via KVM_RUN */ return rc; +case H_XIRR : +case H_CPPR : +case H_EOI : +case H_IPI : +if (kvmppc_xics_enabled (vcpu->kvm)) { + ret =kvmppc_xics_hcall (vcpu, req); +break ; + }/* fallthrough */ default :return RESUME_HOST ; }
@@ -673,6 +681,13 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)kvmppc_set_pvr (vcpu, vcpu->arch.pvr);spin_lock_init (&vcpu->arch.vpa_update_lock); +/* Create the XICS */ +if (kvmppc_xics_enabled (kvm)) { + err =kvmppc_xics_create_icp (vcpu); +if (err <0 ) + goto free_vcpu; + } +kvmppc_mmu_book3s_hv_init (vcpu);/*
@@ -727,6 +742,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)kvmppc_unpin_guest_page (vcpu->kvm, vcpu->arch.vpa.pinned_addr);spin_unlock (&vcpu->arch.vpa_update_lock);kvm_vcpu_uninit (vcpu); +if (kvmppc_xics_enabled (vcpu->kvm)) +kvmppc_xics_free_icp (vcpu);kmem_cache_free (kvm_vcpu_cache, vcpu); }
@@ -1602,6 +1619,9 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)kvmppc_rtas_tokens_free (kvm); +if (kvmppc_xics_enabled (kvm)) +kvmppc_xics_free (kvm); +kvmppc_free_hpt (kvm);WARN_ON (!list_empty (&kvm->arch.spapr_tce_tables)); }
arch/powerpc/kvm/book3s_pr.c +13 -0
@@ -984,6 +984,13 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)if (err <0 ) goto uninit_vcpu; +/* Create the XICS */ +if (kvmppc_xics_enabled (kvm)) { + err =kvmppc_xics_create_icp (vcpu); +if (err <0 ) + goto free_vcpu; + } +return vcpu; uninit_vcpu:
@@ -1000,6 +1007,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) {struct kvmppc_vcpu_book3s *vcpu_book3s =to_book3s (vcpu); +if (kvmppc_xics_enabled (vcpu->kvm)) +kvmppc_xics_free_icp (vcpu);free_page ((unsigned long )vcpu->arch.shared &PAGE_MASK );kvm_vcpu_uninit (vcpu);kfree (vcpu_book3s->shadow_vcpu);
@@ -1199,6 +1208,7 @@ int kvmppc_core_init_vm(struct kvm *kvm) {#ifdef CONFIG_PPC64 INIT_LIST_HEAD (&kvm->arch.spapr_tce_tables); +INIT_LIST_HEAD (&kvm->arch.rtas_tokens);#endif return 0 ;
@@ -1209,6 +1219,9 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)#ifdef CONFIG_PPC64 WARN_ON (!list_empty (&kvm->arch.spapr_tce_tables));#endif +if (kvmppc_xics_enabled (kvm)) +kvmppc_xics_free (kvm); + }static int kvmppc_book3s_init (void )
arch/powerpc/kvm/book3s_pr_papr.c +17 -2
@@ -227,6 +227,15 @@ static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)return EMULATE_DONE ; } +static int kvmppc_h_pr_xics_hcall (struct kvm_vcpu *vcpu,u32 cmd) +{ +long rc =kvmppc_xics_hcall (vcpu, cmd); +if (rc =H_TOO_HARD ) +return EMULATE_FAIL ; +kvmppc_set_gpr (vcpu,3 , rc); +return EMULATE_DONE ; +} +int kvmppc_h_pr (struct kvm_vcpu *vcpu,unsigned long cmd) {switch (cmd) {
@@ -246,11 +255,17 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)clear_bit (KVM_REQ_UNHALT , &vcpu->requests); vcpu->stat.halt_wakeup++;return EMULATE_DONE ; +case H_XIRR : +case H_CPPR : +case H_EOI : +case H_IPI : +if (kvmppc_xics_enabled (vcpu->kvm)) +return kvmppc_h_pr_xics_hcall (vcpu, cmd); +break ;case H_RTAS :if (list_empty (&vcpu->kvm->arch.rtas_tokens))return RESUME_HOST ; - rc =kvmppc_rtas_hcall (vcpu); -if (rc !=0 ) +if (kvmppc_rtas_hcall (vcpu) !=0 )break ;kvmppc_set_gpr (vcpu,3 ,0 );return EMULATE_DONE ;
arch/powerpc/kvm/book3s_rtas.c +50 -1
62 lines not on this page · raw message
arch/powerpc/kvm/book3s_xics.c new file +880 -0
880 lines not on this page · raw message
arch/powerpc/kvm/booke.c +1 -2
@@ -199,8 +199,7 @@ void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,kvmppc_booke_queue_irqprio (vcpu, prio); } -void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu, -struct kvm_interrupt *irq) +void kvmppc_core_dequeue_external (struct kvm_vcpu *vcpu) {clear_bit (BOOKE_IRQPRIO_EXTERNAL , &vcpu->arch.pending_exceptions);clear_bit (BOOKE_IRQPRIO_EXTERNAL_LEVEL , &vcpu->arch.pending_exceptions);
arch/powerpc/kvm/powerpc.c +22 -6
@@ -286,6 +286,7 @@ int kvm_dev_ioctl_check_extension(long ext)break ;#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_PPC_GET_SMMU_INFO : +case KVM_CAP_SPAPR_XICS : r =1 ;break ;#endif
@@ -611,7 +612,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)int kvm_vcpu_ioctl_interrupt (struct kvm_vcpu *vcpu,struct kvm_interrupt *irq) {if (irq->irq =KVM_INTERRUPT_UNSET ) { -kvmppc_core_dequeue_external (vcpu, irq); +kvmppc_core_dequeue_external (vcpu);return 0 ; }
@@ -841,11 +842,6 @@ long kvm_arch_vm_ioctl(struct file *filp, r =0 ; break; } - -case KVM_PPC_RTAS_DEFINE_TOKEN : - r =kvm_vm_ioctl_rtas_define_token (kvm, argp); -break ; -#endif /* CONFIG_KVM_BOOK3S_64_HV */ #ifdef CONFIG_PPC_BOOK3S_64
@@ -859,7 +855,27 @@ long kvm_arch_vm_ioctl(struct file *filp, r = -EFAULT ; break; } +case KVM_PPC_RTAS_DEFINE_TOKEN : + r =kvm_vm_ioctl_rtas_define_token (kvm, argp); +break ;#endif /* CONFIG_PPC_BOOK3S_64 */ +case KVM_IRQ_LINE : +if (kvmppc_xics_enabled (kvm)) + r =kvmppc_xics_ioctl (kvm, ioctl, arg); +else + r = -ENOTTY ; +break ; +case KVM_CREATE_IRQCHIP : { +u32 type; + + r = -EFAULT ; +if (get_user (type, (u32 __user *)argp)) +break ; + r = -EINVAL ; +if (type =KVM_IRQCHIP_TYPE_ICP || type =KVM_IRQCHIP_TYPE_ICS ) + r =kvmppc_xics_ioctl (kvm, ioctl, arg); +break ; + }default : r = -ENOTTY ; }
include/linux/kvm.h +8 -1
@@ -111,6 +111,7 @@ struct kvm_irq_level {* ACPI gsi notion of irq. * For IA-64 (APIC model) IOAPIC0: irq 0-23; IOAPIC1: irq 24-47.. * For X86 (standard AT mode) PIC0/1: irq 0-15. IOAPIC0: 0-23.. +* On powerpc SPAPR, the ICS source number, level is ignored. */ union {__u32 irq;
@@ -620,6 +621,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_PPC_ALLOC_HTAB 80#define KVM_CAP_PPC_VPA 81#define KVM_CAP_PPC_RTAS 82 +#define KVM_CAP_SPAPR_XICS 83#ifdef KVM_CAP_IRQ_ROUTING
@@ -753,6 +755,11 @@ struct kvm_msi {__u8 pad[16 ]; }; +#ifndef __KVM_HAVE_IRQCHIP_ARGS +/* Allow arch code to optionally define args for KVM_CREATE_IRQCHIP */ +struct kvm_irqchip_args { }; +#endif +/* * ioctls for VM fds */
@@ -783,7 +790,7 @@ struct kvm_s390_ucas_mapping {#define KVM_S390_VCPU_FAULT _IOW(KVMIO, 0x52, unsigned long)/* Device model IOC */ -#define KVM_CREATE_IRQCHIP _IO(KVMIO, 0x60) +#define KVM_CREATE_IRQCHIP _IOW(KVMIO, 0x60, struct kvm_irqchip_args)#define KVM_IRQ_LINE _IOW(KVMIO, 0x61, struct kvm_irq_level)#define KVM_GET_IRQCHIP _IOWR(KVMIO, 0x62, struct kvm_irqchip)#define KVM_SET_IRQCHIP _IOR(KVMIO, 0x63, struct kvm_irqchip)