From patchwork Mon Oct 1 09:14:02 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 1530091 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 977C3DF24C for ; Mon, 1 Oct 2012 09:14:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752749Ab2JAJOG (ORCPT ); Mon, 1 Oct 2012 05:14:06 -0400 Received: from mail-qa0-f53.google.com ([209.85.216.53]:34242 "EHLO mail-qa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752721Ab2JAJOF (ORCPT ); Mon, 1 Oct 2012 05:14:05 -0400 Received: by mail-qa0-f53.google.com with SMTP id s11so1340769qaa.19 for ; Mon, 01 Oct 2012 02:14:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=subject:to:from:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-type:content-transfer-encoding :x-gm-message-state; bh=je4iMYy2+QxQxaNv39CEkTwaNmVQlFvFJqX5UpcYQvE=; b=ALH0xdKJXC8VZcGufxkR+IKJx53tfefjC+QyFBKudhONvOhAEcLRxIL1/syuDNaoTz 5xlV1Er9ed9lvii0r8sOydvE9d2x3ULGMz0nw9bwLocMmKlS221I3mPFCtO5nv9C6r0D obzMp2p0ic/oq8BwAtTK9zQqrnJ1ODnj6EKTNY9iLMmp1lfNnrrMvBtXSQj5eazdNmpE aliRSFSa62kf/8HrOVPTS6AnKdWcXQUmLMUeEQh4DSrXiqqufuTsO5TTQCI4G6SmYrvP WGvP4Kdfk+BTqBUN/5QMybdZGtx4HKzJfVFsbWE4UVYp8RxnQPsatxen4dx0I17+DYgS qCuw== Received: by 10.229.134.205 with SMTP id k13mr9908729qct.23.1349082844605; Mon, 01 Oct 2012 02:14:04 -0700 (PDT) Received: from [127.0.1.1] (pool-72-80-83-148.nycmny.fios.verizon.net. [72.80.83.148]) by mx.google.com with ESMTPS id ck18sm23787473qab.7.2012.10.01.02.14.03 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 01 Oct 2012 02:14:03 -0700 (PDT) Subject: [PATCH v2 06/10] ARM: KVM: VGIC interrupt injection To: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu From: Christoffer Dall Cc: Marc Zyngier Date: Mon, 01 Oct 2012 05:14:02 -0400 Message-ID: <20121001091402.49503.68704.stgit@ubuntu> In-Reply-To: <20121001091244.49503.96318.stgit@ubuntu> References: <20121001091244.49503.96318.stgit@ubuntu> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Gm-Message-State: ALoCoQkZXqs01I82UUsU9m8wDj5HW1s1RY0xEwe02B2Ff1iWB31BopRRYPAPXav4KhubgetujdtN Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Marc Zyngier Plug the interrupt injection code. Interrupts can now be generated from user space. Signed-off-by: Marc Zyngier Signed-off-by: Christoffer Dall --- arch/arm/include/asm/kvm_vgic.h | 8 ++++ arch/arm/kvm/arm.c | 36 ++++++++++++++++++ arch/arm/kvm/vgic.c | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/arm/include/asm/kvm_vgic.h b/arch/arm/include/asm/kvm_vgic.h index bb67076..9740f1f 100644 --- a/arch/arm/include/asm/kvm_vgic.h +++ b/arch/arm/include/asm/kvm_vgic.h @@ -240,6 +240,8 @@ struct kvm_exit_mmio; #ifdef CONFIG_KVM_ARM_VGIC void kvm_vgic_sync_to_cpu(struct kvm_vcpu *vcpu); void kvm_vgic_sync_from_cpu(struct kvm_vcpu *vcpu); +int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num, + bool level); int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu); bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_exit_mmio *mmio); @@ -260,6 +262,12 @@ static inline void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) {} static inline void kvm_vgic_sync_to_cpu(struct kvm_vcpu *vcpu) {} static inline void kvm_vgic_sync_from_cpu(struct kvm_vcpu *vcpu) {} +static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, + const struct kvm_irq_level *irq) +{ + return 0; +} + static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) { return 0; diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index cf13340..be593220 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -758,10 +758,31 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level) switch (irq_type) { case KVM_ARM_IRQ_TYPE_CPU: + if (irqchip_in_kernel(kvm)) + return -ENXIO; + if (irq_num > KVM_ARM_IRQ_CPU_FIQ) return -EINVAL; return vcpu_interrupt_line(vcpu, irq_num, level); +#ifdef CONFIG_KVM_ARM_VGIC + case KVM_ARM_IRQ_TYPE_PPI: + if (!irqchip_in_kernel(kvm)) + return -ENXIO; + + if (irq_num < 16 || irq_num > 31) + return -EINVAL; + + return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level); + case KVM_ARM_IRQ_TYPE_SPI: + if (!irqchip_in_kernel(kvm)) + return -ENXIO; + + if (irq_num < 32 || irq_num > KVM_ARM_IRQ_GIC_MAX) + return -EINVAL; + + return kvm_vgic_inject_irq(kvm, 0, irq_num, level); +#endif } return -EINVAL; @@ -821,7 +842,20 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { - return -EINVAL; + + switch (ioctl) { +#ifdef CONFIG_KVM_ARM_VGIC + case KVM_CREATE_IRQCHIP: { + struct kvm *kvm = filp->private_data; + if (vgic_present) + return kvm_vgic_init(kvm); + else + return -EINVAL; + } +#endif + default: + return -EINVAL; + } } static void cpu_init_hyp_mode(void *vector) diff --git a/arch/arm/kvm/vgic.c b/arch/arm/kvm/vgic.c index 2b90785..b52d4c2 100644 --- a/arch/arm/kvm/vgic.c +++ b/arch/arm/kvm/vgic.c @@ -72,6 +72,7 @@ #define ACCESS_WRITE_MASK(x) ((x) & (3 << 1)) static void vgic_update_state(struct kvm *kvm); +static void vgic_kick_vcpus(struct kvm *kvm); static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg); static inline int vgic_irq_is_edge(struct vgic_dist *dist, int irq) @@ -539,6 +540,9 @@ bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_exi kvm_prepare_mmio(run, mmio); kvm_handle_mmio_return(vcpu, run); + if (updated_state) + vgic_kick_vcpus(vcpu->kvm); + return true; } @@ -831,3 +835,76 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu); } + +static void vgic_kick_vcpus(struct kvm *kvm) +{ + struct kvm_vcpu *vcpu; + int c; + + /* + * We've injected an interrupt, time to find out who deserves + * a good kick... + */ + kvm_for_each_vcpu(c, vcpu, kvm) { + if (kvm_vgic_vcpu_pending_irq(vcpu)) + kvm_vcpu_kick(vcpu); + } +} + +static bool vgic_update_irq_state(struct kvm *kvm, int cpuid, + unsigned int irq_num, bool level) +{ + struct vgic_dist *dist = &kvm->arch.vgic; + struct kvm_vcpu *vcpu; + int is_edge, is_level, state; + int pend, enabled; + + spin_lock(&dist->lock); + + is_edge = vgic_irq_is_edge(dist, irq_num); + is_level = !is_edge; + state = vgic_bitmap_get_irq_val(&dist->irq_state, cpuid, irq_num); + + /* + * Only inject an interrupt if: + * - level triggered and we change level + * - edge triggered and we have a rising edge + */ + if ((is_level && !(state ^ level)) || (is_edge && (state || !level))) { + spin_unlock(&dist->lock); + return false; + } + + vgic_bitmap_set_irq_val(&dist->irq_state, cpuid, irq_num, level); + + enabled = vgic_bitmap_get_irq_val(&dist->irq_enabled, cpuid, irq_num); + pend = level && enabled; + + if (irq_num >= 32) { + cpuid = dist->irq_spi_cpu[irq_num - 32]; + pend &= vgic_bitmap_get_irq_val(&dist->irq_spi_target[cpuid], + 0, irq_num); + } + + kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid); + + vcpu = kvm_get_vcpu(kvm, cpuid); + if (pend) { + set_bit(irq_num, vcpu->arch.vgic_cpu.pending); + set_bit(cpuid, &dist->irq_pending_on_cpu); + } else + clear_bit(irq_num, vcpu->arch.vgic_cpu.pending); + + spin_unlock(&dist->lock); + + return true; +} + +int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num, + bool level) +{ + if (vgic_update_irq_state(kvm, cpuid, irq_num, level)) + vgic_kick_vcpus(kvm); + + return 0; +}