From patchwork Thu Aug 16 15:29:31 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 1333541 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 AAD85DFB34 for ; Thu, 16 Aug 2012 15:29:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757408Ab2HPP3f (ORCPT ); Thu, 16 Aug 2012 11:29:35 -0400 Received: from mail-qa0-f53.google.com ([209.85.216.53]:42677 "EHLO mail-qa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753424Ab2HPP3d (ORCPT ); Thu, 16 Aug 2012 11:29:33 -0400 Received: by mail-qa0-f53.google.com with SMTP id s11so663779qaa.19 for ; Thu, 16 Aug 2012 08:29:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=subject:to:from:date:message-id:in-reply-to:references:user-agent :mime-version:content-type:content-transfer-encoding :x-gm-message-state; bh=abDT+yXXopeZrDjrdRcqt2doNi8UM3lx71p/o5FrjwQ=; b=pdBiDNtv/30ql/fc4aM5zdD+nsBlJTumWPhye4TiB1F9RaPiTPYvtxYJFdV4ECbZV0 fC1po1n1HsWnYvs9tmJQH8haLnVxdGUdqbB6lDBOFG3mSf+4yhONlCASU8toqNP7i2nA Bt1y/9xUeixQuMhCYFrzzVTSO6rpMP9dH3OkXQfUgh30cxj1QeaSWI/hLZp3kCDGI9SC 2RybEvk9pe8KkP7nWca/+wc6KEHyNZEMot5/Rz12VnBq3ZaEBnwBEpfVm4iUW1SuUQQo IIPA3eSfaekOeFu0DxSr0Ef1NpbT9ax5ukh4TezvyB6GeXpAMikH5J4IRs7B91dUWAy5 uQBg== Received: by 10.229.106.20 with SMTP id v20mr1010221qco.30.1345130973397; Thu, 16 Aug 2012 08:29:33 -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 g3sm6999944qah.5.2012.08.16.08.29.32 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 16 Aug 2012 08:29:32 -0700 (PDT) Subject: [PATCH v10 08/14] KVM: ARM: Inject IRQs and FIQs from userspace To: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org From: Christoffer Dall Date: Thu, 16 Aug 2012 11:29:31 -0400 Message-ID: <20120816152930.21484.70933.stgit@ubuntu> In-Reply-To: <20120816152637.21484.65421.stgit@ubuntu> References: <20120816152637.21484.65421.stgit@ubuntu> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Gm-Message-State: ALoCoQmdJP/GEcTL6gOcMWu1vyFhtOPoj5k3aEiTlFyVgFgrENYtWkaNLxq9ZU/ny1A9UvfsKuoH Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Christoffer Dall Userspace can inject IRQs and FIQs through the KVM_IRQ_LINE VM ioctl. This ioctl is used since the sematics are in fact two lines that can be either raised or lowered on the VCPU - the IRQ and FIQ lines. KVM needs to know which VCPU it must operate on and whether the FIQ or IRQ line is raised/lowered. Hence both pieces of information is packed in the kvm_irq_level->irq field. The irq fild value will be: IRQ: vcpu_index << 1 FIQ: (vcpu_index << 1) | 1 This is documented in Documentation/kvm/api.txt. The effect of the ioctl is simply to simply raise/lower the corresponding irq_line field on the VCPU struct, which will cause the world-switch code to raise/lower virtual interrupts when running the guest on next switch. The wait_for_interrupt flag is also cleared for raised IRQs or FIQs causing an idle VCPU to become active again. CPUs in guest mode are kicked to make sure they refresh their interrupt status. Signed-off-by: Christoffer Dall --- Documentation/virtual/kvm/api.txt | 12 ++++++--- arch/arm/include/asm/kvm.h | 9 +++++++ arch/arm/include/asm/kvm_arm.h | 1 + arch/arm/kvm/arm.c | 47 +++++++++++++++++++++++++++++++++++++ include/linux/kvm.h | 1 + 5 files changed, 66 insertions(+), 4 deletions(-) -- 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/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index bf33aaa..8345b78 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -614,15 +614,19 @@ only go to the IOAPIC. On ia64, a IOSAPIC is created. 4.25 KVM_IRQ_LINE Capability: KVM_CAP_IRQCHIP -Architectures: x86, ia64 +Architectures: x86, ia64, arm Type: vm ioctl Parameters: struct kvm_irq_level Returns: 0 on success, -1 on error Sets the level of a GSI input to the interrupt controller model in the kernel. -Requires that an interrupt controller model has been previously created with -KVM_CREATE_IRQCHIP. Note that edge-triggered interrupts require the level -to be set to 1 and then back to 0. +On some architectures it is required that an interrupt controller model has +been previously created with KVM_CREATE_IRQCHIP. Note that edge-triggered +interrupts require the level to be set to 1 and then back to 0. + +ARM uses two types of interrupt lines per CPU: IRQ and FIQ. The value of the +irq field should be (vcpu_index << 1) for IRQs and ((vcpu_index << 1) | 1) for +FIQs. Level is used to raise/lower the line. struct kvm_irq_level { union { diff --git a/arch/arm/include/asm/kvm.h b/arch/arm/include/asm/kvm.h index bc5d72b..4a3e25d 100644 --- a/arch/arm/include/asm/kvm.h +++ b/arch/arm/include/asm/kvm.h @@ -22,6 +22,15 @@ #include #define __KVM_HAVE_GUEST_DEBUG +#define __KVM_HAVE_IRQ_LINE + +/* + * KVM_IRQ_LINE macros to set/read IRQ/FIQ for specific VCPU index. + */ +enum KVM_ARM_IRQ_LINE_TYPE { + KVM_ARM_IRQ_LINE = 0, + KVM_ARM_FIQ_LINE = 1, +}; /* * Modes used for short-hand mode determinition in the world-switch code and diff --git a/arch/arm/include/asm/kvm_arm.h b/arch/arm/include/asm/kvm_arm.h index 6e46541..0f641c1 100644 --- a/arch/arm/include/asm/kvm_arm.h +++ b/arch/arm/include/asm/kvm_arm.h @@ -74,6 +74,7 @@ #define HCR_GUEST_MASK (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS | \ HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \ HCR_SWIO | HCR_TIDCP) +#define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF) /* Hyp System Control Register (HSCTLR) bits */ #define HSCTLR_TE (1 << 30) diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index 3f97e7c..8306587 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #define CREATE_TRACE_POINTS @@ -265,6 +266,7 @@ void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { + vcpu->cpu = cpu; } void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) @@ -305,6 +307,51 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) return -EINVAL; } +int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level) +{ + unsigned int vcpu_idx; + struct kvm_vcpu *vcpu; + unsigned long *ptr; + bool set; + int bit_index; + + vcpu_idx = irq_level->irq >> 1; + if (vcpu_idx >= KVM_MAX_VCPUS) + return -EINVAL; + + vcpu = kvm_get_vcpu(kvm, vcpu_idx); + if (!vcpu) + return -EINVAL; + + trace_kvm_set_irq(irq_level->irq, irq_level->level, 0); + + if ((irq_level->irq & 1) == KVM_ARM_IRQ_LINE) + bit_index = ffs(HCR_VI) - 1; + else /* KVM_ARM_FIQ_LINE */ + bit_index = ffs(HCR_VF) - 1; + + ptr = (unsigned long *)&vcpu->arch.irq_lines; + if (irq_level->level) + set = test_and_set_bit(bit_index, ptr); + else + set = test_and_clear_bit(bit_index, ptr); + + /* + * If we didn't change anything, no need to wake up or kick other CPUs + */ + if (!!set == !!irq_level->level) + return 0; + + /* + * The vcpu irq_lines field was updated, wake up sleeping VCPUs and + * trigger a world-switch round on the running physical CPU to set the + * virtual IRQ/FIQ fields in the HCR appropriately. + */ + kvm_vcpu_kick(vcpu); + + return 0; +} + long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { diff --git a/include/linux/kvm.h b/include/linux/kvm.h index 5fb08b5..c9b2556 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -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.. + * For ARM: IRQ: irq = (2*vcpu_index). FIQ: irq = (2*vcpu_indx + 1). */ union { __u32 irq;