From patchwork Mon Jul 6 02:17:32 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shannon Zhao X-Patchwork-Id: 50676 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1E261229E5 for ; Mon, 6 Jul 2015 02:20:04 +0000 (UTC) Received: by wgjx7 with SMTP id x7sf44687950wgj.3 for ; Sun, 05 Jul 2015 19:20:03 -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:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:cc:mime-version :content-type:content-transfer-encoding:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list; bh=OkfW+nu+8exFhXVWjjmhwCCOl4JbWkm4qqMYT6m9w+k=; b=QJk776pij3zHsKkjAhwwD2deuWU5QxIyEmIrIKdE7giT43MUXpqVPIeIQhF4CT4QWF lgDe4CPaFg5nu6LaCMwp07mL4hgQFIoeeds2a6QQpRXcp6ua7BGTbbkRJ9YI0zwcvnle 6REyGaHAUxdBuICdqHK9ldKKTm77hJtpmhm01RrTvQJGtseILRq9BZPkPrb9B02GOf/K QItsG1gRBkGS0tqk1bB0fxzXvjFJCLLU+nkUyodVYXXt0CpHAnx6/IZMCMAIk2fWlis6 87XSjRxumqLHCjnXnbraNChgqmVAkN5a+zxv5E5Ah7O/H5VxoXd/7UF5TlkCGHxxwg4g brQA== X-Gm-Message-State: ALoCoQnVcWn60yAmLxK16cxQbYW5sPszKz1F6lOmet4s6dsPcbkG3cJELCYeEhZfQXG5BOUpZnx4 X-Received: by 10.194.60.73 with SMTP id f9mr11187719wjr.6.1436149203468; Sun, 05 Jul 2015 19:20:03 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.4.199 with SMTP id m7ls643354lam.83.gmail; Sun, 05 Jul 2015 19:20:03 -0700 (PDT) X-Received: by 10.112.222.133 with SMTP id qm5mr46687341lbc.86.1436149203209; Sun, 05 Jul 2015 19:20:03 -0700 (PDT) Received: from mail-la0-f41.google.com (mail-la0-f41.google.com. [209.85.215.41]) by mx.google.com with ESMTPS id lh9si13953112lab.51.2015.07.05.19.20.03 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Jul 2015 19:20:03 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.41 as permitted sender) client-ip=209.85.215.41; Received: by lagh6 with SMTP id h6so137401065lag.2 for ; Sun, 05 Jul 2015 19:20:03 -0700 (PDT) X-Received: by 10.152.36.102 with SMTP id p6mr46680447laj.19.1436149203078; Sun, 05 Jul 2015 19:20:03 -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.112.108.230 with SMTP id hn6csp1425997lbb; Sun, 5 Jul 2015 19:20:01 -0700 (PDT) X-Received: by 10.68.246.1 with SMTP id xs1mr99727063pbc.53.1436149197950; Sun, 05 Jul 2015 19:19:57 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id ws2si26416384pab.124.2015.07.05.19.19.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Jul 2015 19:19:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) 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 1ZBvzJ-0005r1-RV; Mon, 06 Jul 2015 02:18:29 +0000 Received: from mail-pd0-f178.google.com ([209.85.192.178]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZBvzE-0005mI-Dk for linux-arm-kernel@lists.infradead.org; Mon, 06 Jul 2015 02:18:25 +0000 Received: by pdbci14 with SMTP id ci14so96439624pdb.2 for ; Sun, 05 Jul 2015 19:18:04 -0700 (PDT) X-Received: by 10.68.114.131 with SMTP id jg3mr73969272pbb.54.1436149084314; Sun, 05 Jul 2015 19:18:04 -0700 (PDT) Received: from localhost ([120.136.34.248]) by mx.google.com with ESMTPSA id q5sm16203616pdd.90.2015.07.05.19.18.02 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sun, 05 Jul 2015 19:18:03 -0700 (PDT) From: shannon.zhao@linaro.org To: kvmarm@lists.cs.columbia.edu Subject: [PATCH 02/18] KVM: ARM64: Add initial support for PMU Date: Mon, 6 Jul 2015 10:17:32 +0800 Message-Id: <1436149068-3784-3-git-send-email-shannon.zhao@linaro.org> X-Mailer: git-send-email 1.9.5.msysgit.1 In-Reply-To: <1436149068-3784-1-git-send-email-shannon.zhao@linaro.org> References: <1436149068-3784-1-git-send-email-shannon.zhao@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150705_191824_520963_5EC8FE30 X-CRM114-Status: GOOD ( 24.28 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.192.178 listed in wl.mailspike.net] -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.192.178 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Cc: kvm@vger.kernel.org, marc.zyngier@arm.com, will.deacon@arm.com, linux-arm-kernel@lists.infradead.org, zhaoshenglong@huawei.com, alex.bennee@linaro.org, christoffer.dall@linaro.org, shannon.zhao@linaro.org 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: shannon.zhao@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.215.41 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 From: Shannon Zhao Here we plan to support virtual PMU for guest by full software emulation, so define some basic structs and functions preparing for futher steps. Define struct kvm_pmc for performance monitor counter and struct kvm_pmu for performance monitor unit for each vcpu. According to ARMv8 spec, the PMU contains at most 32(ARMV8_MAX_COUNTERS) counters. Initialize PMU for each vcpu when kvm initialize vcpus, while reset PMU when kvm reset vcpus. Since this only supports ARM64 (or PMUv3), add a separate config symbol for it. Signed-off-by: Shannon Zhao --- arch/arm/kvm/arm.c | 4 +++ arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/asm/pmu.h | 2 ++ arch/arm64/kvm/Kconfig | 7 +++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/reset.c | 3 +++ include/kvm/arm_pmu.h | 54 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/pmu.c | 55 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+) create mode 100644 include/kvm/arm_pmu.h create mode 100644 virt/kvm/arm/pmu.c diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index bc738d2..4e82625 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -28,6 +28,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include "trace.h" @@ -278,6 +279,9 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) /* Set up the timer */ kvm_timer_vcpu_init(vcpu); + /* Set up the PMU */ + kvm_pmu_init(vcpu); + return 0; } diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 2709db2..3c88873 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -42,6 +42,7 @@ #include #include +#include #define KVM_VCPU_MAX_FEATURES 3 @@ -116,6 +117,7 @@ struct kvm_vcpu_arch { /* VGIC state */ struct vgic_cpu vgic_cpu; struct arch_timer_cpu timer_cpu; + struct kvm_pmu pmu; /* * Anything that is not used directly from assembly code goes diff --git a/arch/arm64/include/asm/pmu.h b/arch/arm64/include/asm/pmu.h index b9f394a..95681e6 100644 --- a/arch/arm64/include/asm/pmu.h +++ b/arch/arm64/include/asm/pmu.h @@ -19,6 +19,8 @@ #ifndef __ASM_PMU_H #define __ASM_PMU_H +#include + #define ARMV8_MAX_COUNTERS 32 #define ARMV8_COUNTER_MASK (ARMV8_MAX_COUNTERS - 1) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index bfffe8f..eae3a1b 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -31,6 +31,7 @@ config KVM select KVM_VFIO select HAVE_KVM_EVENTFD select HAVE_KVM_IRQFD + select KVM_ARM_PMU ---help--- Support hosting virtualized guest machines. @@ -52,4 +53,10 @@ config KVM_ARM_MAX_VCPUS large, so only choose a reasonable number that you expect to actually use. +config KVM_ARM_PMU + bool + depends on KVM_ARM_HOST + ---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 f90f4aa..78db4ee 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -27,3 +27,4 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3-emul.o kvm-$(CONFIG_KVM_ARM_HOST) += vgic-v3-switch.o kvm-$(CONFIG_KVM_ARM_HOST) += $(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 0b43265..ee2c2e9 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -107,5 +107,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) /* Reset timer */ kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); + /* Reset PMU */ + 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..27d14ca --- /dev/null +++ b/include/kvm/arm_pmu.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2015 Linaro Ltd. + * Author: Shannon Zhao + * + * 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, see . + */ + +#ifndef __ASM_ARM_KVM_PMU_H +#define __ASM_ARM_KVM_PMU_H + +#include +#include + +struct kvm_pmc { + u8 idx; + u64 counter; + u64 eventsel; + bool interrupt; + bool enable; + struct perf_event *perf_event; + struct kvm_vcpu *vcpu; +}; + +struct kvm_pmu { +#ifdef CONFIG_KVM_ARM_PMU + /* PMU IRQ Number per VCPU */ + int irq_num; + int overflow_status; + /* IRQ pending flag */ + bool irq_pending; + struct irq_work irq_work; + struct kvm_pmc pmc[ARMV8_MAX_COUNTERS]; +#endif +}; + +#ifdef CONFIG_KVM_ARM_PMU +void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu); +void kvm_pmu_init(struct kvm_vcpu *vcpu); +#else +static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_init(struct kvm_vcpu *vcpu) {} +#endif + +#endif diff --git a/virt/kvm/arm/pmu.c b/virt/kvm/arm/pmu.c new file mode 100644 index 0000000..dc252d0 --- /dev/null +++ b/virt/kvm/arm/pmu.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015 Linaro Ltd. + * Author: Shannon Zhao + * + * 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, see . + */ + +#include +#include +#include +#include + +/** + * kvm_pmu_vcpu_reset - reset pmu state for cpu + * @vcpu: The vcpu pointer + * + */ +void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu = &vcpu->arch.pmu; + + pmu->irq_pending = false; +} + +/** + * kvm_pmu_init - Initialize global PMU state for per vcpu + * @vcpu: The vcpu pointer + * + * Initialize PMC and 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. + */ +void kvm_pmu_init(struct kvm_vcpu *vcpu) +{ + int i; + struct kvm_pmu *pmu = &vcpu->arch.pmu; + + memset(pmu, 0, sizeof(*pmu)); + for (i = 0; i < ARMV8_MAX_COUNTERS; i++) { + pmu->pmc[i].idx = i; + pmu->pmc[i].vcpu = vcpu; + } + + pmu->irq_num = -1; +}