From patchwork Tue Aug 5 09:24:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 34895 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f69.google.com (mail-oa0-f69.google.com [209.85.219.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B45B020523 for ; Tue, 5 Aug 2014 09:26:55 +0000 (UTC) Received: by mail-oa0-f69.google.com with SMTP id i7sf2941261oag.4 for ; Tue, 05 Aug 2014 02:26:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id :in-reply-to:references:cc:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-type:content-transfer-encoding; bh=GRrH3nIMg/uP4GbTeoxqOxeuiV8MqcxQPsFUqYWYHKU=; b=CktlS4iCyy1d/HC/NfwaccTAQ+3kuQctorMP4sUUUTMoEobxUhh9ah4ofjpX7U+m2H yUsA0E+y55rWUTnEOXEgxtWvYctrbo3K5YIX2RA49JpHzH1WBK3c+PAy3g0GkT6P39l9 +hNXnhN9H/DSl46dYnymdUzSW4rYkqDhSUkAHffLlMHcl+oG6I9aTstyFvBjIY8DlIW/ pdtvPSFep8U6MRnn/zYq4xuzcyfFpetlL8Ja+EjanyOIa3wnAOVii7KVu9uOw0AtY60f WyGXyeSm4xKsAjfk/w5UN4l3fxrO6WU5eZY3IBqqwDyZA8490dNGifIC+Z2B8rp0mM2T x+MA== X-Gm-Message-State: ALoCoQmUf89f6G3tn8P5NddoyxOIWlqhx14JPpxF0ZCf8wPsnD1892Hx7L3Xcf5QWs8L1Gfwcz3T X-Received: by 10.50.112.68 with SMTP id io4mr8600023igb.0.1407230815147; Tue, 05 Aug 2014 02:26:55 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.93.198 with SMTP id d64ls249580qge.30.gmail; Tue, 05 Aug 2014 02:26:55 -0700 (PDT) X-Received: by 10.52.231.226 with SMTP id tj2mr1796237vdc.16.1407230815053; Tue, 05 Aug 2014 02:26:55 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id p18si678816vcf.70.2014.08.05.02.26.55 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 05 Aug 2014 02:26:55 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id im17so987539vcb.17 for ; Tue, 05 Aug 2014 02:26:55 -0700 (PDT) X-Received: by 10.52.238.227 with SMTP id vn3mr1656626vdc.47.1407230814963; Tue, 05 Aug 2014 02:26:54 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp369489vcb; Tue, 5 Aug 2014 02:26:54 -0700 (PDT) X-Received: by 10.70.108.194 with SMTP id hm2mr2690735pdb.115.1407230813982; Tue, 05 Aug 2014 02:26:53 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id yp8si1246482pac.193.2014.08.05.02.26.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Aug 2014 02:26:53 -0700 (PDT) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XEb00-0002Iy-9j; Tue, 05 Aug 2014 09:25:40 +0000 Received: from mail-pa0-f50.google.com ([209.85.220.50]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XEazb-0000sk-MY for linux-arm-kernel@lists.infradead.org; Tue, 05 Aug 2014 09:25:20 +0000 Received: by mail-pa0-f50.google.com with SMTP id et14so1064067pad.23 for ; Tue, 05 Aug 2014 02:24:58 -0700 (PDT) X-Received: by 10.67.12.175 with SMTP id er15mr2468095pad.143.1407230698526; Tue, 05 Aug 2014 02:24:58 -0700 (PDT) Received: from pnqlab006.amcc.com ([182.73.239.130]) by mx.google.com with ESMTPSA id ho7sm4434486pad.9.2014.08.05.02.24.53 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 05 Aug 2014 02:24:57 -0700 (PDT) From: Anup Patel To: kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 4/6] ARM/ARM64: KVM: Add common code PMU IRQ routing Date: Tue, 5 Aug 2014 14:54:13 +0530 Message-Id: <1407230655-28864-5-git-send-email-anup.patel@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1407230655-28864-1-git-send-email-anup.patel@linaro.org> References: <1407230655-28864-1-git-send-email-anup.patel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140805_022515_793284_D572D298 X-CRM114-Status: GOOD ( 24.14 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.220.50 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [209.85.220.50 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: ian.campbell@citrix.com, kvm@vger.kernel.org, Anup Patel , marc.zyngier@arm.com, patches@apm.com, will.deacon@arm.com, linux-arm-kernel@lists.infradead.org, christoffer.dall@linaro.org, pranavkumar@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: anup.patel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 This patch introduces common PMU IRQ routing code for KVM ARM and KVM ARM64 under virt/kvm/arm directory. The virtual PMU IRQ number for each Guest VCPU will be provided by user space using set device address vm ioctl with prameters: dev_id = KVM_ARM_DEVICE_PMU type = VCPU number addr = PMU IRQ number for the VCPU The low-level context switching code of KVM ARM/ARM64 will determine the state of VCPU PMU IRQ store it in "irq_pending" flag when saving PMU context for the VCPU. The common PMU IRQ routing code will inject virtual PMU IRQ based on "irq_pending" flag and it will also clear the "irq_pending" flag. Signed-off-by: Anup Patel Signed-off-by: Pranavkumar Sawargaonkar --- arch/arm/include/asm/kvm_host.h | 9 ++++ arch/arm/include/uapi/asm/kvm.h | 1 + arch/arm/kvm/arm.c | 6 +++ arch/arm/kvm/reset.c | 4 ++ arch/arm64/include/asm/kvm_host.h | 9 ++++ arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/Kconfig | 7 +++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/reset.c | 4 ++ include/kvm/arm_pmu.h | 52 ++++++++++++++++++ virt/kvm/arm/pmu.c | 105 +++++++++++++++++++++++++++++++++++++ 11 files changed, 199 insertions(+) create mode 100644 include/kvm/arm_pmu.h create mode 100644 virt/kvm/arm/pmu.c diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index 193ceaf..a6a778f 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -24,6 +24,7 @@ #include #include #include +#include #if defined(CONFIG_KVM_ARM_MAX_VCPUS) #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS @@ -53,6 +54,9 @@ struct kvm_arch { /* Timer */ struct arch_timer_kvm timer; + /* PMU */ + struct pmu_kvm pmu; + /* * Anything that is not used directly from assembly code goes * here. @@ -118,8 +122,13 @@ struct kvm_vcpu_arch { /* VGIC state */ struct vgic_cpu vgic_cpu; + + /* Timer state */ struct arch_timer_cpu timer_cpu; + /* PMU state */ + struct pmu_cpu pmu_cpu; + /* * Anything that is not used directly from assembly code goes * here. diff --git a/arch/arm/include/uapi/asm/kvm.h b/arch/arm/include/uapi/asm/kvm.h index e6ebdd3..b21e6eb 100644 --- a/arch/arm/include/uapi/asm/kvm.h +++ b/arch/arm/include/uapi/asm/kvm.h @@ -75,6 +75,7 @@ struct kvm_regs { /* Supported device IDs */ #define KVM_ARM_DEVICE_VGIC_V2 0 +#define KVM_ARM_DEVICE_PMU 1 /* Supported VGIC address types */ #define KVM_VGIC_V2_ADDR_TYPE_DIST 0 diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index 3c82b37..04130f5 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -140,6 +140,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm_timer_init(kvm); + kvm_pmu_init(kvm); + /* Mark the initial VMID generation invalid */ kvm->arch.vmid_gen = 0; @@ -567,6 +569,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) { local_irq_enable(); kvm_timer_sync_hwstate(vcpu); + kvm_pmu_sync_hwstate(vcpu); kvm_vgic_sync_hwstate(vcpu); continue; } @@ -601,6 +604,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) *************************************************************/ kvm_timer_sync_hwstate(vcpu); + kvm_pmu_sync_hwstate(vcpu); kvm_vgic_sync_hwstate(vcpu); ret = handle_exit(vcpu, run, ret); @@ -794,6 +798,8 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, if (!vgic_present) return -ENXIO; return kvm_vgic_addr(kvm, type, &dev_addr->addr, true); + case KVM_ARM_DEVICE_PMU: + return kvm_pmu_addr(kvm, type, &dev_addr->addr, true); default: return -ENODEV; } diff --git a/arch/arm/kvm/reset.c b/arch/arm/kvm/reset.c index f558c07..42e6996 100644 --- a/arch/arm/kvm/reset.c +++ b/arch/arm/kvm/reset.c @@ -28,6 +28,7 @@ #include #include +#include /****************************************************************************** * Cortex-A15 and Cortex-A7 Reset Values @@ -79,5 +80,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) /* Reset arch_timer context */ kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); + /* Reset pmu context */ + kvm_pmu_vcpu_reset(vcpu); + return 0; } diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 7592ddf..ae4cdb2 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -38,6 +38,7 @@ #include #include +#include #define KVM_VCPU_MAX_FEATURES 3 @@ -63,6 +64,9 @@ struct kvm_arch { /* Timer */ struct arch_timer_kvm timer; + + /* PMU */ + struct pmu_kvm pmu; }; #define KVM_NR_MEM_OBJS 40 @@ -109,8 +113,13 @@ struct kvm_vcpu_arch { /* VGIC state */ struct vgic_cpu vgic_cpu; + + /* Timer state */ struct arch_timer_cpu timer_cpu; + /* PMU state */ + struct pmu_cpu pmu_cpu; + /* * Anything that is not used directly from assembly code goes * here. diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index e633ff8..a7fed09 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -69,6 +69,7 @@ struct kvm_regs { /* Supported device IDs */ #define KVM_ARM_DEVICE_VGIC_V2 0 +#define KVM_ARM_DEVICE_PMU 1 /* Supported VGIC address types */ #define KVM_VGIC_V2_ADDR_TYPE_DIST 0 diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 8ba85e9..672213d 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -26,6 +26,7 @@ config KVM select KVM_ARM_HOST select KVM_ARM_VGIC select KVM_ARM_TIMER + select KVM_ARM_PMU ---help--- Support hosting virtualized guest machines. @@ -60,4 +61,10 @@ config KVM_ARM_TIMER ---help--- Adds support for the Architected Timers in virtual machines. +config KVM_ARM_PMU + bool + depends on KVM_ARM_VGIC + ---help--- + Adds support for the Performance Monitoring in virtual machines. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 72a9fd5..6be68bc 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -21,3 +21,4 @@ kvm-$(CONFIG_KVM_ARM_HOST) += guest.o reset.o sys_regs.o sys_regs_generic_v8.o kvm-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic.o kvm-$(CONFIG_KVM_ARM_TIMER) += $(KVM)/arm/arch_timer.o +kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 70a7816..27f4041 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -108,5 +109,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) /* Reset timer */ kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); + /* Reset pmu context */ + kvm_pmu_vcpu_reset(vcpu); + return 0; } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h new file mode 100644 index 0000000..1e3aa44 --- /dev/null +++ b/include/kvm/arm_pmu.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 Linaro Ltd. + * Author: Anup Patel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __ASM_ARM_KVM_PMU_H +#define __ASM_ARM_KVM_PMU_H + +struct pmu_kvm { +#ifdef CONFIG_KVM_ARM_PMU + /* PMU IRQ Numbers */ + unsigned int irq_num[CONFIG_KVM_ARM_MAX_VCPUS]; +#endif +}; + +struct pmu_cpu { +#ifdef CONFIG_KVM_ARM_PMU + /* IRQ pending flag. Updated when registers are saved. */ + u32 irq_pending; +#endif +}; + +#ifdef CONFIG_KVM_ARM_PMU +void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu); +void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu); +int kvm_pmu_addr(struct kvm *kvm, unsigned long cpu, u64 *irq, bool write); +int kvm_pmu_init(struct kvm *kvm); +#else +static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) {} +static inline int kvm_pmu_addr(struct kvm *kvm, + unsigned long cpu, u64 *irq, bool write) +{ + return -ENXIO; +} +static inline int kvm_pmu_init(struct kvm *kvm) { return 0; } +#endif + +#endif diff --git a/virt/kvm/arm/pmu.c b/virt/kvm/arm/pmu.c new file mode 100644 index 0000000..98066ad --- /dev/null +++ b/virt/kvm/arm/pmu.c @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2014 Linaro Ltd. + * Author: Anup Patel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include + +#include +#include + +/** + * kvm_pmu_sync_hwstate - sync pmu state for cpu + * @vcpu: The vcpu pointer + * + * Inject virtual PMU IRQ if IRQ is pending for this cpu. + */ +void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) +{ + struct pmu_cpu *pmu = &vcpu->arch.pmu_cpu; + struct pmu_kvm *kpmu = &vcpu->kvm->arch.pmu; + + if (pmu->irq_pending) { + kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, + kpmu->irq_num[vcpu->vcpu_id], + 1); + pmu->irq_pending = 0; + return; + } +} + +/** + * kvm_pmu_vcpu_reset - reset pmu state for cpu + * @vcpu: The vcpu pointer + * + */ +void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) +{ + struct pmu_cpu *pmu = &vcpu->arch.pmu_cpu; + + pmu->irq_pending = 0; +} + +/** + * kvm_pmu_addr - set or get PMU VM IRQ numbers + * @kvm: pointer to the vm struct + * @cpu: cpu number + * @irq: pointer to irq number value + * @write: if true set the irq number else read the irq number + * + * Set or get the PMU IRQ number for the given cpu number. + */ +int kvm_pmu_addr(struct kvm *kvm, unsigned long cpu, u64 *irq, bool write) +{ + struct pmu_kvm *kpmu = &kvm->arch.pmu; + + if (CONFIG_KVM_ARM_MAX_VCPUS <= cpu) + return -ENODEV; + + mutex_lock(&kvm->lock); + + if (write) { + kpmu->irq_num[cpu] = *irq; + } else { + *irq = kpmu->irq_num[cpu]; + } + + mutex_unlock(&kvm->lock); + + return 0; +} + +/** + * kvm_pmu_init - Initialize global PMU state for a VM + * @kvm: pointer to the kvm struct + * + * Set all the PMU IRQ numbers to invalid value so that + * user space has to explicitly provide PMU IRQ numbers + * using set device address ioctl. + */ +int kvm_pmu_init(struct kvm *kvm) +{ + int i; + struct pmu_kvm *kpmu = &kvm->arch.pmu; + + for (i = 0; i < CONFIG_KVM_ARM_MAX_VCPUS; i++) { + kpmu->irq_num[i] = UINT_MAX; + } + + return 0; +}