From nobody Thu Dec 18 07:57:12 2025 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D142919938D for ; Mon, 3 Feb 2025 18:31:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607502; cv=none; b=cyEl/FvH9/vrPuVT8Q6js1j/v03CLt03+kxsEPi6L7aIihFF+oVxZOzxghke+IVhaNUtQACkLKjSeJsB6xU5txaP5Zqd20NdfMk5jmA5kwmoutmQGMGmTcvZ9/LvJgGhDJTqdn+cFlT0Vr8I8HDl2c4x2vUIvEtZlYsF7CXMbq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607502; c=relaxed/simple; bh=xY+mxWUIwcoJgnhfGlaSlV532s+yW7KbR+gZRh/8eTo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Pfixkocc7qRKJOiyZBFfHJcpiaNNW/jKpDzZw3XWAB986qTtg+hmy5Titu7/fD+F69Yu0ZQZZRnuLudLVpMIiWeIHE7onfzRXfnYOC5VGOUaf+Sr07SXgcQfdO9YitKUwziJ32NB9kJJSd5iUzbfYl+0KrN/umD8yX4nTfmXgG0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=svjl2qkN; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="svjl2qkN" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607493; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Sf6sBMMAV5XjKoDS/ngB0As3YdpZ4fzfIUulH+JWe4I=; b=svjl2qkNOXxx/ORuRlNmH1+hK72/bHVIzo5KboZBHF8OnpcNXt7NFJ0eQxA+u+YTDVo5n+ RA7dJUgkWmIy3ZtCWhA1clKAjEYG684wATyovmhHkDl+8pC0LGR65BUJR4jdRLa6mprVAi 3uO7IaBXWr6xqKLNgLDamGve0S/zQ9Q= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 01/14] drivers/perf: apple_m1: Refactor event select/filter configuration Date: Mon, 3 Feb 2025 10:30:58 -0800 Message-Id: <20250203183111.191519-2-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Supporting guest mode events will necessitate programming two event filters. Prepare by splitting up the programming of the event selector + event filter into separate headers. Opportunistically replace RMW patterns with sysreg_clear_set_s(). Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/include/asm/apple_m1_pmu.h | 1 + drivers/perf/apple_m1_cpu_pmu.c | 52 ++++++++++++++++----------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/arch/arm64/include/asm/apple_m1_pmu.h b/arch/arm64/include/asm= /apple_m1_pmu.h index 99483b19b99f..02e05d05851f 100644 --- a/arch/arm64/include/asm/apple_m1_pmu.h +++ b/arch/arm64/include/asm/apple_m1_pmu.h @@ -37,6 +37,7 @@ #define PMCR0_PMI_ENABLE_8_9 GENMASK(45, 44) =20 #define SYS_IMP_APL_PMCR1_EL1 sys_reg(3, 1, 15, 1, 0) +#define SYS_IMP_APL_PMCR1_EL12 sys_reg(3, 1, 15, 7, 2) #define PMCR1_COUNT_A64_EL0_0_7 GENMASK(15, 8) #define PMCR1_COUNT_A64_EL1_0_7 GENMASK(23, 16) #define PMCR1_COUNT_A64_EL0_8_9 GENMASK(41, 40) diff --git a/drivers/perf/apple_m1_cpu_pmu.c b/drivers/perf/apple_m1_cpu_pm= u.c index 06fd317529fc..cea80afd1253 100644 --- a/drivers/perf/apple_m1_cpu_pmu.c +++ b/drivers/perf/apple_m1_cpu_pmu.c @@ -327,11 +327,10 @@ static void m1_pmu_disable_counter_interrupt(unsigned= int index) __m1_pmu_enable_counter_interrupt(index, false); } =20 -static void m1_pmu_configure_counter(unsigned int index, u8 event, - bool user, bool kernel) +static void __m1_pmu_configure_event_filter(unsigned int index, bool user, + bool kernel) { - u64 val, user_bit, kernel_bit; - int shift; + u64 clear, set, user_bit, kernel_bit; =20 switch (index) { case 0 ... 7: @@ -346,19 +345,24 @@ static void m1_pmu_configure_counter(unsigned int ind= ex, u8 event, BUG(); } =20 - val =3D read_sysreg_s(SYS_IMP_APL_PMCR1_EL1); - + clear =3D set =3D 0; if (user) - val |=3D user_bit; + set |=3D user_bit; else - val &=3D ~user_bit; + clear |=3D user_bit; =20 if (kernel) - val |=3D kernel_bit; + set |=3D kernel_bit; else - val &=3D ~kernel_bit; + clear |=3D kernel_bit; =20 - write_sysreg_s(val, SYS_IMP_APL_PMCR1_EL1); + sysreg_clear_set_s(SYS_IMP_APL_PMCR1_EL1, clear, set); +} + +static void __m1_pmu_configure_eventsel(unsigned int index, u8 event) +{ + u64 clear =3D 0, set =3D 0; + int shift; =20 /* * Counters 0 and 1 have fixed events. For anything else, @@ -371,21 +375,29 @@ static void m1_pmu_configure_counter(unsigned int ind= ex, u8 event, break; case 2 ... 5: shift =3D (index - 2) * 8; - val =3D read_sysreg_s(SYS_IMP_APL_PMESR0_EL1); - val &=3D ~((u64)0xff << shift); - val |=3D (u64)event << shift; - write_sysreg_s(val, SYS_IMP_APL_PMESR0_EL1); + clear |=3D (u64)0xff << shift; + set |=3D (u64)event << shift; + sysreg_clear_set_s(SYS_IMP_APL_PMESR0_EL1, clear, set); break; case 6 ... 9: shift =3D (index - 6) * 8; - val =3D read_sysreg_s(SYS_IMP_APL_PMESR1_EL1); - val &=3D ~((u64)0xff << shift); - val |=3D (u64)event << shift; - write_sysreg_s(val, SYS_IMP_APL_PMESR1_EL1); + clear |=3D (u64)0xff << shift; + set |=3D (u64)event << shift; + sysreg_clear_set_s(SYS_IMP_APL_PMESR1_EL1, clear, set); break; } } =20 +static void m1_pmu_configure_counter(unsigned int index, unsigned long con= fig_base) +{ + bool kernel =3D config_base & M1_PMU_CFG_COUNT_KERNEL; + bool user =3D config_base & M1_PMU_CFG_COUNT_USER; + u8 evt =3D config_base & M1_PMU_CFG_EVENT; + + __m1_pmu_configure_event_filter(index, user, kernel); + __m1_pmu_configure_eventsel(index, evt); +} + /* arm_pmu backend */ static void m1_pmu_enable_event(struct perf_event *event) { @@ -400,7 +412,7 @@ static void m1_pmu_enable_event(struct perf_event *even= t) m1_pmu_disable_counter(event->hw.idx); isb(); =20 - m1_pmu_configure_counter(event->hw.idx, evt, user, kernel); + m1_pmu_configure_counter(event->hw.idx, event->hw.config_base); m1_pmu_enable_counter(event->hw.idx); m1_pmu_enable_counter_interrupt(event->hw.idx); isb(); --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 789B11D54E2 for ; Mon, 3 Feb 2025 18:31:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607506; cv=none; b=FCg+e1RPlbJ5b5Wlth0nx6ev5ahlIN2Omn5irrqI3jsSyq/I//DDPG9qW3FbxIl2Fa49SwPdxSku5U40K/Hhpg4Tnb/EOZBl6XTOJIyMgCtXuQlBwE4UFvFisbAasFCd8Z5goTQvuqw+RwHU+1Ew6BidZOFUXlQ/3mnYoLngbjo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607506; c=relaxed/simple; bh=hGXtgTxlVOSEkZPZl6Q3pgWkOgOaE72a0yYctig5aeE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fl8ETc0PcFwyd5B+WOwusdQWxc7HH/Ez9gK+kIQk9Yw22jhhRXUHJwxKD/Pp0ooo0VsQU8SaEzRa+cKpj1+gcDP0YP8iplZJg395PczioUDy5aS1RgGQsWH/641ZrdNFgIMDCdjxquqgm/DQ0vHi9XmmxnCzsJTDTVKNWpExikY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=OXs9h+Cx; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="OXs9h+Cx" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HVdhPRPhu/oRpEuf2KXzS/cqoEesNVF//Q5fOtfKKRU=; b=OXs9h+CxohMA1hGtuk0TWjCwiDvzQLQdQEdksVxrQurPDD1Nh2a+w5HYxt/FzTeauxNXew 049151b7alpgPMOmOIahGzTBdn7wkkvaUyqAtndsG44k+IzawmzGO9Mlh7mZ2RjYMOON1c MgcMZLlG4qI0shD4d4SpQdcd5eVEuNo= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 02/14] drivers/perf: apple_m1: Support host/guest event filtering Date: Mon, 3 Feb 2025 10:30:59 -0800 Message-Id: <20250203183111.191519-3-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The PMU appears to have a separate register for filtering 'guest' exception levels (i.e. EL1 and !ELIsInHost(EL0)) which has the same layout as PMCR1_EL1. Conveniently, there exists a VHE register alias (PMCR1_EL12) that can be used to configure it. Support guest events by programming the EL12 register with the intended guest kernel/userspace filters. Limit support for guest events to VHE (i.e. kernel running at EL2), as it avoids involving KVM to context switch PMU registers. VHE is the only supported mode on M* parts anyway, so this isn't an actual feature limitation. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- drivers/perf/apple_m1_cpu_pmu.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/perf/apple_m1_cpu_pmu.c b/drivers/perf/apple_m1_cpu_pm= u.c index cea80afd1253..d6d4ff6da862 100644 --- a/drivers/perf/apple_m1_cpu_pmu.c +++ b/drivers/perf/apple_m1_cpu_pmu.c @@ -120,6 +120,8 @@ enum m1_pmu_events { */ M1_PMU_CFG_COUNT_USER =3D BIT(8), M1_PMU_CFG_COUNT_KERNEL =3D BIT(9), + M1_PMU_CFG_COUNT_HOST =3D BIT(10), + M1_PMU_CFG_COUNT_GUEST =3D BIT(11), }; =20 /* @@ -328,7 +330,7 @@ static void m1_pmu_disable_counter_interrupt(unsigned i= nt index) } =20 static void __m1_pmu_configure_event_filter(unsigned int index, bool user, - bool kernel) + bool kernel, bool host) { u64 clear, set, user_bit, kernel_bit; =20 @@ -356,7 +358,10 @@ static void __m1_pmu_configure_event_filter(unsigned i= nt index, bool user, else clear |=3D kernel_bit; =20 - sysreg_clear_set_s(SYS_IMP_APL_PMCR1_EL1, clear, set); + if (host) + sysreg_clear_set_s(SYS_IMP_APL_PMCR1_EL1, clear, set); + else if (is_kernel_in_hyp_mode()) + sysreg_clear_set_s(SYS_IMP_APL_PMCR1_EL12, clear, set); } =20 static void __m1_pmu_configure_eventsel(unsigned int index, u8 event) @@ -391,10 +396,13 @@ static void __m1_pmu_configure_eventsel(unsigned int = index, u8 event) static void m1_pmu_configure_counter(unsigned int index, unsigned long con= fig_base) { bool kernel =3D config_base & M1_PMU_CFG_COUNT_KERNEL; + bool guest =3D config_base & M1_PMU_CFG_COUNT_GUEST; + bool host =3D config_base & M1_PMU_CFG_COUNT_HOST; bool user =3D config_base & M1_PMU_CFG_COUNT_USER; u8 evt =3D config_base & M1_PMU_CFG_EVENT; =20 - __m1_pmu_configure_event_filter(index, user, kernel); + __m1_pmu_configure_event_filter(index, user && host, kernel && host, true= ); + __m1_pmu_configure_event_filter(index, user && guest, kernel && guest, fa= lse); __m1_pmu_configure_eventsel(index, evt); } =20 @@ -570,7 +578,7 @@ static int m1_pmu_set_event_filter(struct hw_perf_event= *event, { unsigned long config_base =3D 0; =20 - if (!attr->exclude_guest) { + if (!attr->exclude_guest && !is_kernel_in_hyp_mode()) { pr_debug("ARM performance counters do not support mode exclusion\n"); return -EOPNOTSUPP; } @@ -578,6 +586,10 @@ static int m1_pmu_set_event_filter(struct hw_perf_even= t *event, config_base |=3D M1_PMU_CFG_COUNT_KERNEL; if (!attr->exclude_user) config_base |=3D M1_PMU_CFG_COUNT_USER; + if (!attr->exclude_host) + config_base |=3D M1_PMU_CFG_COUNT_HOST; + if (!attr->exclude_guest) + config_base |=3D M1_PMU_CFG_COUNT_GUEST; =20 event->config_base =3D config_base; =20 --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3111D70820 for ; Mon, 3 Feb 2025 18:31:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607514; cv=none; b=PmuvjTy3RBv7MSSX4e2KIDb4ahuLOQbgEJNdiY3noOQJtipqql/ZMZM8UphtuSppFDA0QZ8B4vb+CFjF/eEAN2rDagr/Ct98bbfrveO0+j7Lyn39NhYUPAz9bU7ZNhbYCfisH//6m32L6En83tUbcIIzKmRSc9jKyMZ2XQx6VHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607514; c=relaxed/simple; bh=7c2/3kiKaDtYUq6AVs0XEOloHXlD+EHU/9j2Tglg05g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T5A5DaTvVBkEEiZaMPQTKwZ8N0CHYtIYZCY6/yo6W0vWItS1i07jAfjJQtnGypbkdXo/AYSszoGWFYYEsNuWaqesnSxL3z1KqkQds4bR4aLkr69Yd4a9673n9MDftJ7iq3HwFGjHXWbMFbSA0+4cjEMRJwTxMhv1GTmhGQSJdDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=v7tcnRdN; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="v7tcnRdN" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jhHWaSj7rVpk+0D8GPGQ5v84hJxdzIQwmg+ucmjbBK8=; b=v7tcnRdNSjLwWfftw9UBbYZSiUr7weFv2E/dy0WyowJkCK29mW7t539wY7oCeY4HRnfqaB DtSRwfaK2CaNzgAc/2HHhlyc/5jFSIEvPVjU4xd0IzKvBlYu/qHPupHdGp4nV4m9k4KFu6 Gua9is7xvvwHcO3RY3Lfre494+vAIP8= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 03/14] drivers/perf: apple_m1: Provide helper for mapping PMUv3 events Date: Mon, 3 Feb 2025 10:31:00 -0800 Message-Id: <20250203183111.191519-4-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Apple M* parts carry some IMP DEF traps for guest accesses to PMUv3 registers, even though the underlying hardware doesn't implement PMUv3. This means it is possible to virtualize PMUv3 for KVM guests. Add a helper for mapping common PMUv3 event IDs onto hardware event IDs, keeping the implementation-specific crud in the PMU driver rather than KVM proper. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- drivers/perf/apple_m1_cpu_pmu.c | 35 +++++++++++++++++++++++++++++++++ include/linux/perf/arm_pmu.h | 1 + 2 files changed, 36 insertions(+) diff --git a/drivers/perf/apple_m1_cpu_pmu.c b/drivers/perf/apple_m1_cpu_pm= u.c index d6d4ff6da862..0e54d3f900a7 100644 --- a/drivers/perf/apple_m1_cpu_pmu.c +++ b/drivers/perf/apple_m1_cpu_pmu.c @@ -12,6 +12,7 @@ =20 #include #include +#include #include =20 #include @@ -174,6 +175,17 @@ static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MA= X] =3D { [PERF_COUNT_HW_BRANCH_MISSES] =3D M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC, }; =20 +#define M1_PMUV3_EVENT_MAP(pmuv3_event, m1_event) \ + [ARMV8_PMUV3_PERFCTR_##pmuv3_event] =3D M1_PMU_PERFCTR_##m1_event + +static const unsigned int m1_pmu_pmceid_map[ARMV8_PMUV3_MAX_COMMON_EVENTS]= =3D { + [0 ... ARMV8_PMUV3_MAX_COMMON_EVENTS - 1] =3D HW_OP_UNSUPPORTED, + M1_PMUV3_EVENT_MAP(INST_RETIRED, INST_ALL), + M1_PMUV3_EVENT_MAP(CPU_CYCLES, CORE_ACTIVE_CYCLE), + M1_PMUV3_EVENT_MAP(BR_RETIRED, INST_BRANCH), + M1_PMUV3_EVENT_MAP(BR_MIS_PRED_RETIRED, BRANCH_MISPRED_NONSPEC), +}; + /* sysfs definitions */ static ssize_t m1_pmu_events_sysfs_show(struct device *dev, struct device_attribute *attr, @@ -558,6 +570,26 @@ static int m2_pmu_map_event(struct perf_event *event) return armpmu_map_event(event, &m1_pmu_perf_map, NULL, M1_PMU_CFG_EVENT); } =20 +static int m1_pmu_map_pmuv3_event(unsigned int eventsel) +{ + int m1_event =3D HW_OP_UNSUPPORTED; + + if (eventsel < ARMV8_PMUV3_MAX_COMMON_EVENTS) + m1_event =3D m1_pmu_pmceid_map[eventsel]; + + return m1_event =3D=3D HW_OP_UNSUPPORTED ? -EOPNOTSUPP : m1_event; +} + +static void m1_pmu_init_pmceid(struct arm_pmu *pmu) +{ + unsigned int event; + + for (event =3D 0; event < ARMV8_PMUV3_MAX_COMMON_EVENTS; event++) { + if (m1_pmu_map_pmuv3_event(event) >=3D 0) + set_bit(event, pmu->pmceid_bitmap); + } +} + static void m1_pmu_reset(void *info) { int i; @@ -618,6 +650,9 @@ static int m1_pmu_init(struct arm_pmu *cpu_pmu, u32 fla= gs) cpu_pmu->reset =3D m1_pmu_reset; cpu_pmu->set_event_filter =3D m1_pmu_set_event_filter; =20 + cpu_pmu->map_pmuv3_event =3D m1_pmu_map_pmuv3_event; + m1_pmu_init_pmceid(cpu_pmu); + bitmap_set(cpu_pmu->cntr_mask, 0, M1_PMU_NR_COUNTERS); cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =3D &m1_pmu_events_attr_gr= oup; cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =3D &m1_pmu_format_attr_g= roup; diff --git a/include/linux/perf/arm_pmu.h b/include/linux/perf/arm_pmu.h index 4b5b83677e3f..35f3778ae20e 100644 --- a/include/linux/perf/arm_pmu.h +++ b/include/linux/perf/arm_pmu.h @@ -100,6 +100,7 @@ struct arm_pmu { void (*stop)(struct arm_pmu *); void (*reset)(void *); int (*map_event)(struct perf_event *event); + int (*map_pmuv3_event)(unsigned int eventsel); DECLARE_BITMAP(cntr_mask, ARMPMU_MAX_HWEVENTS); bool secure_access; /* 32-bit ARM only */ #define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40 --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC8081D517E for ; Mon, 3 Feb 2025 18:31:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607521; cv=none; b=HXU60m+deHAgQtMYOw6skMDF/bMoN0b1WEm64tL37N0yVNtaHuK39tjvfyAErMYTUGTYvV4g9/ViRwzQdMOfeiHMhuKCdd07wM+MzIIsSJLsU7do90th/uol0CDoVyREzyUqnDIVXiK9Y5Gb6OMVCFBcqV7Zz+bbjONcSoICKf0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607521; c=relaxed/simple; bh=7q+wM7fdK8a9PjBGsmvPD5wHFK2eT/HLNJVutnTTiNg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KSUjqlxRMbRhcHXV08d7Ds1RgOVzjZRr/2U/uZ4Jy49kvBvkhsncF+ZszHUc9rT1wJ2WconzcxyRic74j6bgxAHoPYwvvdCunZ/AfYfdp38G1uNIa8LsQQPtcOLRz5DjSOpZnS72opHufM/59wk5w+0JdhpYBAaiLUHA+jznCbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=WUruC93q; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WUruC93q" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607513; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xfqIKZG28S2rSPiEQ0brMIbgRTgMNYuFfI/4SHaMPU8=; b=WUruC93qvovs9ABTt0Krz2DAEYJjkwrF9J0uI/lH9ElXmFzjQdjnHsCdaYVsLVAlTxwvie lhHcqul1+hMZhN7DyHjHXHl59lpcqWdpL0LS09va8SSA7P2qUU9UB2+L/RsFHtfVqDZKko /f8cQiFw6O4oUvRZmLBXsRaaSxc2324= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 04/14] KVM: arm64: Compute PMCEID from arm_pmu's event bitmaps Date: Mon, 3 Feb 2025 10:31:01 -0800 Message-Id: <20250203183111.191519-5-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The PMUv3 driver populates a couple of bitmaps with the values of PMCEID{0,1}, from which the guest's PMCEID{0,1} can be derived. This is particularly convenient when virtualizing PMUv3 on IMP DEF hardware, as reading the nonexistent PMCEID registers leads to a rather unpleasant UNDEF. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 47 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 6c5950b9ceac..104672a0c5a2 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -842,8 +842,42 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void) return pmu; } =20 +static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1) +{ + u32 hi[2], lo[2]; + + bitmap_to_arr32(lo, pmu->pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS); + bitmap_to_arr32(hi, pmu->pmceid_ext_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS= ); + + return ((u64)hi[pmceid1] << 32) | lo[pmceid1]; +} + +static u64 compute_pmceid0(struct arm_pmu *pmu) +{ + u64 val =3D __compute_pmceid(pmu, 0); + + /* always support CHAIN */ + val |=3D BIT(ARMV8_PMUV3_PERFCTR_CHAIN); + return val; +} + +static u64 compute_pmceid1(struct arm_pmu *pmu) +{ + u64 val =3D __compute_pmceid(pmu, 1); + + /* + * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled + * as RAZ + */ + val &=3D ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) | + BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) | + BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32)); + return val; +} + u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) { + struct arm_pmu *cpu_pmu =3D vcpu->kvm->arch.arm_pmu; unsigned long *bmap =3D vcpu->kvm->arch.pmu_filter; u64 val, mask =3D 0; int base, i, nr_events; @@ -852,19 +886,10 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pm= ceid1) return 0; =20 if (!pmceid1) { - val =3D read_sysreg(pmceid0_el0); - /* always support CHAIN */ - val |=3D BIT(ARMV8_PMUV3_PERFCTR_CHAIN); + val =3D compute_pmceid0(cpu_pmu); base =3D 0; } else { - val =3D read_sysreg(pmceid1_el0); - /* - * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled - * as RAZ - */ - val &=3D ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) | - BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) | - BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32)); + val =3D compute_pmceid1(cpu_pmu); base =3D 32; } =20 --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D06091ADC98 for ; Mon, 3 Feb 2025 18:32:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607524; cv=none; b=hznrjmko3Z8+IeMBzIBLhXYeT/1G4siLp40uPTkfjIzPL0HBg4I7dlhfI8UQrer2dOJqmedM/HIPHeIPGO9D6GGd/5Vw9c/VmaMzV2wZqAOpawDK2Ps64rutDw7XWr/2GWqN+xAZke6iFb8l0wDCSZCvyyl4cn4Xdgwyd9SioAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607524; c=relaxed/simple; bh=KVMS2S20RqJDXL6CSXSKW6myhxVt+9yuaYWo6eiaa+0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sfy59Q8VkyeYJ8kDj9m8pX8hyd2X570HAEu8OsYxKnRq4LHlW6i6N6LcLrGbQEBkoqA932HiNinIJDX0hRp3EP+hAZroOxbZn09BrhrScMIuHRrg12WggluT7p2zYiC2yAuagKkGuF45OjUw5AT+XeqM/Xifzmdm0I8HGE18YCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=YgdNDFMw; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="YgdNDFMw" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607520; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Eu97JSnDOZJQo0mlrfz4ghF9+QTKmQpDVJVrWRRDCys=; b=YgdNDFMwn/zsm7qQvUEG2ic6Nf1RTxd/ybFWcIhx/8dkfgjW25RwXPyXf4vr8u64OCowzn bcnjbRgJu3opxBddpvgae/5QNyzJQWQkb8NuwdtOxp+OCQ8IH0+K4Q/V6TgZspY4iXiJS5 DLHuFM5tul8Kzz8KX/q4wc00lfiZsKM= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 05/14] KVM: arm64: Always support SW_INCR PMU event Date: Mon, 3 Feb 2025 10:31:02 -0800 Message-Id: <20250203183111.191519-6-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Support for SW_INCR is unconditional, as KVM traps accesses to PMSWINC_EL0 and emulates the intended event increment. While it is expected that ~all PMUv3 implementations already advertise this event, non-PMUv3 hardware may not. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 104672a0c5a2..62349b670cf9 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -856,6 +856,8 @@ static u64 compute_pmceid0(struct arm_pmu *pmu) { u64 val =3D __compute_pmceid(pmu, 0); =20 + /* always support SW_INCR */ + val |=3D BIT(ARMV8_PMUV3_PERFCTR_SW_INCR); /* always support CHAIN */ val |=3D BIT(ARMV8_PMUV3_PERFCTR_CHAIN); return val; --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1EE51D63D5 for ; Mon, 3 Feb 2025 18:32:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607531; cv=none; b=W0jF3ha8bmTngdYIoxe1sJPWWCdOAZBPtt3YHy0XlQ3pwlH4CppgKnOtJGYera+paT0R+pMF0yA/I/L+PISHAuhsczLBUxFhAcHSht3jzz/jHhDclDPXadmjgJ7yPub3R+LzFjmatD37lUnNz6kQIgnAUepMkU0AiMw2k6ykcKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607531; c=relaxed/simple; bh=WCG4x7RAj1A59gvhqFDMZt0/dV83ifx4kdaQVDiGvv8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lNBqcNb4ZDko1PaNqL3IKjksy4U7qll7eNKyDnxyHxAytFhtc71FNRPrFzqs6vYXMRtWjucb/GeP3dVK/HrP7yd5gZnwgYOLC8S70r720OhI9PVDw4HCUjS8N2Jk8u3PtjmNGKNuV0YCwJXSFm/MqNQCEVdM3shk1XQ5PTjVRR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=J023LZet; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="J023LZet" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LKoag9c+54nBr8hdoUHJ8XU3VHyJTR39TjaDM4YVK34=; b=J023LZetrpn9POuixVTXOKfMMZl421/P1IcBET4+XJGCwKJkSNWfwBVpPONX8t+8OgYW1K xwHbeOQMTY4bawwQU0pUOKrb6xP3JdpH/Dpz6Z7DwDa/448NJICLVP3CS9AHihzWrEvfmF fBHxAcXPw2UpGoNeNE0hnZC9Ul0bK4s= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 06/14] KVM: arm64: Remap PMUv3 events onto hardware Date: Mon, 3 Feb 2025 10:31:03 -0800 Message-Id: <20250203183111.191519-7-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Use the provided helper to map PMUv3 event IDs onto hardware, if the driver exposes such a helper. This is expected to be quite rare, and only useful for non-PMUv3 hardware. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 62349b670cf9..60cf973e2af9 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -673,6 +673,18 @@ static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc) return kvm_pmc_read_evtreg(pmc) & ARMV8_PMU_INCLUDE_EL2; } =20 +static u64 kvm_map_pmu_event(struct kvm *kvm, u64 eventsel) +{ + struct arm_pmu *pmu =3D kvm->arch.arm_pmu; + int hw_event; + + if (!pmu->map_pmuv3_event) + return eventsel; + + hw_event =3D pmu->map_pmuv3_event(eventsel); + return (hw_event < 0) ? eventsel : hw_event; +} + /** * kvm_pmu_create_perf_event - create a perf event for a counter * @pmc: Counter context @@ -711,13 +723,13 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc = *pmc) =20 memset(&attr, 0, sizeof(struct perf_event_attr)); attr.type =3D arm_pmu->pmu.type; + attr.config =3D kvm_map_pmu_event(vcpu->kvm, eventsel); attr.size =3D sizeof(attr); attr.pinned =3D 1; attr.disabled =3D !kvm_pmu_counter_is_enabled(pmc); attr.exclude_user =3D !kvm_pmc_counts_at_el0(pmc); attr.exclude_hv =3D 1; /* Don't count EL2 events */ attr.exclude_host =3D 1; /* Don't count host events */ - attr.config =3D eventsel; =20 /* * Filter events at EL1 (i.e. vEL2) when in a hyp context based on the --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 443E220E335 for ; Mon, 3 Feb 2025 18:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607539; cv=none; b=bC4MsoUQz9NQmj5tPVLlStWOpTrfZ+RmTHCFH0VuAjSEDuc98QTzSSeNOUCZSvdu+5PGY/5IwTGHJ4uBmeK+8Fhf2yJ/YiXG66SgEzopX7NHhj/Fr1YB+iF+XHH5KLk7ZL5xfgFXAqfOgAFQ73m+wz0izzf9uPcLb4qsjV3CBAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607539; c=relaxed/simple; bh=966r4XRDwHx6saQofCMb0X0DMSBLumLIn3GOpHAqkvQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bBVHh0Lwsd3T2oGjI9NNmF4RrTqQ19ghtxmFa2kcfZgMV6aHC+HVlW5iqS1zrEsAxjotPI4dNAyfQz/zxTR1TBtZGkKqNXjZydvGwPNllkiijlEaz1zZpAgvwZKr1BKZzHBX+how3RXInfmpQnh6T2mDPDdt1LIDQwESHGXTNs8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JK5Z8n93; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JK5Z8n93" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607530; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VI1u0K/IY7jv16FYqlbD+J2sTAMuvanXPGhmlxwkIMo=; b=JK5Z8n93DomhhQZoSAco76r8j39hFx/zEjeyETLCgQoGCJ5ZumTH0FxScFYTkMex+h9oOm lA0WtT+sUZj7bsZKBuxUKaj8PLL4PClFvrNrwj+KY8tgm/BA3aR3wFVoJMvyB6Xn+A6dW/ lzuxrQnwCVbVOsmih64FAdfKCq+ggqA= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 07/14] KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3 Date: Mon, 3 Feb 2025 10:31:04 -0800 Message-Id: <20250203183111.191519-8-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" KVM is about to learn some new tricks to virtualize PMUv3 on IMPDEF hardware. As part of that, we now need to differentiate host support from guest support for PMUv3. Add a cpucap to determine if an architectural PMUv3 is present to guard host usage of PMUv3 controls. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/include/asm/cpufeature.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 19 +++++++++++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++-- arch/arm64/kvm/pmu.c | 10 +++++----- arch/arm64/tools/cpucaps | 1 + include/kvm/arm_pmu.h | 2 +- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/c= pufeature.h index e0e4478f5fb5..0eff048848b8 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -866,6 +866,11 @@ static __always_inline bool system_supports_mpam_hcr(v= oid) return alternative_has_cap_unlikely(ARM64_MPAM_HCR); } =20 +static inline bool system_supports_pmuv3(void) +{ + return cpus_have_final_cap(ARM64_HAS_PMUV3); +} + int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt); bool try_emulate_mrs(struct pt_regs *regs, u32 isn); =20 diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 4eb7c6698ae4..6886d2875fac 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1898,6 +1898,19 @@ static bool has_lpa2(const struct arm64_cpu_capabili= ties *entry, int scope) } #endif =20 +static bool has_pmuv3(const struct arm64_cpu_capabilities *entry, int scop= e) +{ + u64 dfr0 =3D read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); + unsigned int pmuver; + + pmuver =3D cpuid_feature_extract_unsigned_field(dfr0, + ID_AA64DFR0_EL1_PMUVer_SHIFT); + if (pmuver =3D=3D ID_AA64DFR0_EL1_PMUVer_IMP_DEF) + return false; + + return pmuver >=3D ID_AA64DFR0_EL1_PMUVer_IMP; +} + #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT)) =20 @@ -2999,6 +3012,12 @@ static const struct arm64_cpu_capabilities arm64_fea= tures[] =3D { ARM64_CPUID_FIELDS(ID_AA64PFR1_EL1, GCS, IMP) }, #endif + { + .desc =3D "PMUv3", + .capability =3D ARM64_HAS_PMUV3, + .type =3D ARM64_CPUCAP_SYSTEM_FEATURE, + .matches =3D has_pmuv3, + }, {}, }; =20 diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/i= nclude/hyp/switch.h index f838a45665f2..0edc7882bedb 100644 --- a/arch/arm64/kvm/hyp/include/hyp/switch.h +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h @@ -244,7 +244,7 @@ static inline void __activate_traps_common(struct kvm_v= cpu *vcpu) * counter, which could make a PMXEVCNTR_EL0 access UNDEF at * EL1 instead of being trapped to EL2. */ - if (kvm_arm_support_pmu_v3()) { + if (system_supports_pmuv3()) { struct kvm_cpu_context *hctxt; =20 write_sysreg(0, pmselr_el0); @@ -281,7 +281,7 @@ static inline void __deactivate_traps_common(struct kvm= _vcpu *vcpu) write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2); =20 write_sysreg(0, hstr_el2); - if (kvm_arm_support_pmu_v3()) { + if (system_supports_pmuv3()) { struct kvm_cpu_context *hctxt; =20 hctxt =3D host_data_ptr(host_ctxt); diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c index 0b3adf3e17b4..6b48a3d16d0d 100644 --- a/arch/arm64/kvm/pmu.c +++ b/arch/arm64/kvm/pmu.c @@ -41,7 +41,7 @@ void kvm_set_pmu_events(u64 set, struct perf_event_attr *= attr) { struct kvm_pmu_events *pmu =3D kvm_get_pmu_events(); =20 - if (!kvm_arm_support_pmu_v3() || !kvm_pmu_switch_needed(attr)) + if (!system_supports_pmuv3() || !kvm_pmu_switch_needed(attr)) return; =20 if (!attr->exclude_host) @@ -57,7 +57,7 @@ void kvm_clr_pmu_events(u64 clr) { struct kvm_pmu_events *pmu =3D kvm_get_pmu_events(); =20 - if (!kvm_arm_support_pmu_v3()) + if (!system_supports_pmuv3()) return; =20 pmu->events_host &=3D ~clr; @@ -133,7 +133,7 @@ void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu) struct kvm_pmu_events *pmu; u64 events_guest, events_host; =20 - if (!kvm_arm_support_pmu_v3() || !has_vhe()) + if (!system_supports_pmuv3() || !has_vhe()) return; =20 preempt_disable(); @@ -154,7 +154,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) struct kvm_pmu_events *pmu; u64 events_guest, events_host; =20 - if (!kvm_arm_support_pmu_v3() || !has_vhe()) + if (!system_supports_pmuv3() || !has_vhe()) return; =20 pmu =3D kvm_get_pmu_events(); @@ -180,7 +180,7 @@ bool kvm_set_pmuserenr(u64 val) struct kvm_cpu_context *hctxt; struct kvm_vcpu *vcpu; =20 - if (!kvm_arm_support_pmu_v3() || !has_vhe()) + if (!system_supports_pmuv3() || !has_vhe()) return false; =20 vcpu =3D kvm_get_running_vcpu(); diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index 1e65f2fb45bd..ee4316cb3690 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -45,6 +45,7 @@ HAS_LSE_ATOMICS HAS_MOPS HAS_NESTED_VIRT HAS_PAN +HAS_PMUV3 HAS_S1PIE HAS_S1POE HAS_RAS_EXTN diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 147bd3ee4f7b..3a8edd78240f 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -86,7 +86,7 @@ void kvm_vcpu_pmu_resync_el0(void); */ #define kvm_pmu_update_vcpu_events(vcpu) \ do { \ - if (!has_vhe() && kvm_arm_support_pmu_v3()) \ + if (!has_vhe() && system_supports_pmuv3()) \ vcpu->arch.pmu.events =3D *kvm_get_pmu_events(); \ } while (0) =20 --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37D771D63D5 for ; Mon, 3 Feb 2025 18:32:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607546; cv=none; b=ST8gdQpbd7FiaCIO+H4VkHUtTXPtLo8lO93S0Q8lJQpB2403WPW94SeunyJu4yoFJMq88xs6iLS6J2SmQ26gSWH55LyTMKdynC4HaJc+kgyWta2ygnYr3eIMWN0Qs1JhFj+xIr0suibztzvG5bffbgEXmWKdzmj3Ng7ylQH27/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607546; c=relaxed/simple; bh=IYlE7nLDn03S4JSMTlSMN1sCgaWmpqcg2DKx1sdKGE4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UHyybg097a65SAT9Nt26lUbfdEEwPae1fayMmDT2O9UqlJeAGwm7KLrw7d2uLk7Q6EzW5qPrG0J59RRfQkwz8HVdfoY0QnmZ6r57z2YogDyymi3TKLNkDBN5988ECyfOUvXxpmbl3k5H5lmuSgL5YiIwB9jJqmdldH1pp7c7c6M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=YqXNQuO3; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="YqXNQuO3" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607538; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w2vrbp80/8C9mTsn6/Mpkczjz80Xxh3JUzWmv1I30Vc=; b=YqXNQuO3A+XbZLOETXGr5T8wRuewArA1HNFNxpGKjoLaaC2gwBzC/kQfh+C/QH+SyT3RlM GwpdavzHLQw2BZDgr0K8+dWeIS+o38JaIWalj4/zR4ZLpPE3Kr+KKFUslqls6Y9+6aUqc3 LghuqWtbORYqyRW6gwoacBmbZ2qHbV8= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 08/14] KVM: arm64: Drop kvm_arm_pmu_available static key Date: Mon, 3 Feb 2025 10:31:05 -0800 Message-Id: <20250203183111.191519-9-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" With the PMUv3 cpucap, kvm_arm_pmu_available is no longer used in the hot path of guest entry/exit. On top of that, guest support for PMUv3 may not correlate with host support for the feature, e.g. on IMPDEF hardware. Throw out the static key and just inspect the list of PMUs to determine if PMUv3 is supported for KVM guests. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kernel/image-vars.h | 5 ----- arch/arm64/kvm/arm.c | 4 ++-- arch/arm64/kvm/pmu-emul.c | 11 ++++++----- include/kvm/arm_pmu.h | 10 ++-------- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h index ef3a69cc398e..e705c64138ce 100644 --- a/arch/arm64/kernel/image-vars.h +++ b/arch/arm64/kernel/image-vars.h @@ -112,11 +112,6 @@ KVM_NVHE_ALIAS(broken_cntvoff_key); KVM_NVHE_ALIAS(__start___kvm_ex_table); KVM_NVHE_ALIAS(__stop___kvm_ex_table); =20 -/* PMU available static key */ -#ifdef CONFIG_HW_PERF_EVENTS -KVM_NVHE_ALIAS(kvm_arm_pmu_available); -#endif - /* Position-independent library routines */ KVM_NVHE_ALIAS_HYP(clear_page, __pi_clear_page); KVM_NVHE_ALIAS_HYP(copy_page, __pi_copy_page); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 646e806c6ca6..702a6adef58c 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -366,7 +366,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long = ext) r =3D get_num_wrps(); break; case KVM_CAP_ARM_PMU_V3: - r =3D kvm_arm_support_pmu_v3(); + r =3D kvm_supports_guest_pmuv3(); break; case KVM_CAP_ARM_INJECT_SERROR_ESR: r =3D cpus_have_final_cap(ARM64_HAS_RAS_EXTN); @@ -1388,7 +1388,7 @@ static unsigned long system_supported_vcpu_features(v= oid) if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1)) clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features); =20 - if (!kvm_arm_support_pmu_v3()) + if (!kvm_supports_guest_pmuv3()) clear_bit(KVM_ARM_VCPU_PMU_V3, &features); =20 if (!system_supports_sve()) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 60cf973e2af9..8413a038e6c2 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -17,8 +17,6 @@ =20 #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0) =20 -DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available); - static LIST_HEAD(arm_pmus); static DEFINE_MUTEX(arm_pmus_lock); =20 @@ -26,6 +24,12 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pm= c); static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc); static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc); =20 +bool kvm_supports_guest_pmuv3(void) +{ + guard(mutex)(&arm_pmus_lock); + return !list_empty(&arm_pmus); +} + static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc) { return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]); @@ -807,9 +811,6 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) entry->arm_pmu =3D pmu; list_add_tail(&entry->entry, &arm_pmus); =20 - if (list_is_singular(&arm_pmus)) - static_branch_enable(&kvm_arm_pmu_available); - out_unlock: mutex_unlock(&arm_pmus_lock); } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 3a8edd78240f..58fc7f932b3f 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -37,13 +37,7 @@ struct arm_pmu_entry { struct arm_pmu *arm_pmu; }; =20 -DECLARE_STATIC_KEY_FALSE(kvm_arm_pmu_available); - -static __always_inline bool kvm_arm_support_pmu_v3(void) -{ - return static_branch_likely(&kvm_arm_pmu_available); -} - +bool kvm_supports_guest_pmuv3(void); #define kvm_arm_pmu_irq_initialized(v) ((v)->arch.pmu.irq_num >=3D VGIC_NR= _SGIS) u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx); void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 = val); @@ -102,7 +96,7 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu); struct kvm_pmu { }; =20 -static inline bool kvm_arm_support_pmu_v3(void) +static inline bool kvm_supports_guest_pmuv3(void) { return false; } --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-175.mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2312B20E70A for ; Mon, 3 Feb 2025 18:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607552; cv=none; b=EJpX+KYhI+FSPze5NOmjmyk8uGEaxj86NtFlphBRPtrn+kLNL1LZJnPiv3y1xiz6dsKTStc/HT7mdbQRLtdfykTEPEsg0jcLMJEQzhQt7BH9zWEO8+oxejRafyqmzpwce0ZenG5LJq76yPAqX3j1P5iI9t8Q4ki9mtXfXRDgR+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607552; c=relaxed/simple; bh=V/SLKuz6awEKvKWxXGy1bMn2FNkDHdJBqK+p7mxM0vQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XHMkk9Jggu0mw3O/2Ow/htvSfAjxQbdsaXo/7sCZePt17XtzC8QpSmbQQAZoAoA8xFutdT5ZyA/U8ySf8rguCXsSFBtOE8GHP2On4KPpHK7mytyF50fXKPHQJVJa4mCiu3G4p0IQWa0kCIb29s739pedG4w4uebqfNRZTci0TmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rzs/Ce4g; arc=none smtp.client-ip=91.218.175.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="rzs/Ce4g" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607546; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qNDGXaXWlDnWYxdojyVit6ndHU1GueE33oD7T19VC2c=; b=rzs/Ce4g0oaSYJyETez0mm4OJwtlje8EWZyFWbqrmmbuIhFwOioB8YKEVrrRV2vQ2zzhvZ 2FHlzTz26XW4ED0qT33C9yjcIPwAcn7nvqcBwLyEY5IDq8WHkZLPqq9tvbmIWwNz9BwPsj iFlPkb5MKZBwJT3TqUtgW/DANaei9b0= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 09/14] KVM: arm64: Use guard() to cleanup usage of arm_pmus_lock Date: Mon, 3 Feb 2025 10:31:06 -0800 Message-Id: <20250203183111.191519-10-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Get rid of some goto label patterns by using guard() to drop the arm_pmus_lock when returning from a function. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 8413a038e6c2..3048e22c240b 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -802,26 +802,23 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit())) return; =20 - mutex_lock(&arm_pmus_lock); + guard(mutex)(&arm_pmus_lock); =20 entry =3D kmalloc(sizeof(*entry), GFP_KERNEL); if (!entry) - goto out_unlock; + return; =20 entry->arm_pmu =3D pmu; list_add_tail(&entry->entry, &arm_pmus); - -out_unlock: - mutex_unlock(&arm_pmus_lock); } =20 static struct arm_pmu *kvm_pmu_probe_armpmu(void) { - struct arm_pmu *tmp, *pmu =3D NULL; struct arm_pmu_entry *entry; + struct arm_pmu *pmu; int cpu; =20 - mutex_lock(&arm_pmus_lock); + guard(mutex)(&arm_pmus_lock); =20 /* * It is safe to use a stale cpu to iterate the list of PMUs so long as @@ -842,17 +839,13 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void) */ cpu =3D raw_smp_processor_id(); list_for_each_entry(entry, &arm_pmus, entry) { - tmp =3D entry->arm_pmu; + pmu =3D entry->arm_pmu; =20 - if (cpumask_test_cpu(cpu, &tmp->supported_cpus)) { - pmu =3D tmp; - break; - } + if (cpumask_test_cpu(cpu, &pmu->supported_cpus)) + return pmu; } =20 - mutex_unlock(&arm_pmus_lock); - - return pmu; + return NULL; } =20 static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1) --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9A4F20B7E6 for ; Mon, 3 Feb 2025 18:32:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607556; cv=none; b=R09FPcWMhrG9GCABVEzs5JBu3GV/MeZueVs9UaowtdXRPedIFbADdw6PDTTCnnO/utRJ1Mlxt+uui/MuZKoVOepdHmy7GeUTKBd4SIhfYQPblxmPVwEwRdbrv8xqrNVKIrK6gi3echSXRHzUc0YCDtZWgCbjySfBJTFA2sz2G3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607556; c=relaxed/simple; bh=gfVeTaYGvJgjF3+5dv8ThgPZCvgtXsyI4dPTBxdrunc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eeZC2azujISkQuKw8KPqRYe0IRTbxVGXTcnIWYZodP3tB9DCx4aB/qJ5Rgi/0Qmm5eF/jWwK6GPN96YOKBdqTRxCfTktmLzOwXbFMrmTVkqbD6q23ZC8jRpzBtUQSg7ySufCUVucsGnjcECR/oMnca/TO/gZ4XkijO0i9SEu1K0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=URmdA1um; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="URmdA1um" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fk4znlvn6GbakMwTeJliB+Oo4bK3C8L8Zl+LfXy3Y/M=; b=URmdA1um0bGgiad2xuSYS+M2AFDxdiAhjsORqGvAvOLpYPM96w6DIyJyZrbs9uCva4jhMG ZuyF7k8yj+JTEyYqMR9Q0//F1Nq/b73X0txi8cCNdii5yMQKpGc50VkAT6DK3Jj/tmpDnw 59L15zZAd+LzcseSJ7p1t5+Iy1XS9oA= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 10/14] KVM: arm64: Move PMUVer filtering into KVM code Date: Mon, 3 Feb 2025 10:31:07 -0800 Message-Id: <20250203183111.191519-11-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The supported guest PMU version on a particular platform is ultimately a KVM decision. Move PMUVer filtering into KVM code. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/include/asm/cpufeature.h | 23 ----------------------- arch/arm64/kvm/pmu-emul.c | 15 +++++++++------ 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/c= pufeature.h index 0eff048848b8..c4326f1cb917 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -525,29 +525,6 @@ cpuid_feature_extract_unsigned_field(u64 features, int= field) return cpuid_feature_extract_unsigned_field_width(features, field, 4); } =20 -/* - * Fields that identify the version of the Performance Monitors Extension = do - * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825, - * "Alternative ID scheme used for the Performance Monitors Extension vers= ion". - */ -static inline u64 __attribute_const__ -cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap) -{ - u64 val =3D cpuid_feature_extract_unsigned_field(features, field); - u64 mask =3D GENMASK_ULL(field + 3, field); - - /* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ - if (val =3D=3D ID_AA64DFR0_EL1_PMUVer_IMP_DEF) - val =3D 0; - - if (val > cap) { - features &=3D ~mask; - features |=3D (cap << field) & mask; - } - - return features; -} - static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp) { return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift); diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 3048e22c240b..57ef4f2814fc 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1238,13 +1238,16 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, = struct kvm_device_attr *attr) =20 u8 kvm_arm_pmu_get_pmuver_limit(void) { - u64 tmp; + unsigned int pmuver; =20 - tmp =3D read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); - tmp =3D cpuid_feature_cap_perfmon_field(tmp, - ID_AA64DFR0_EL1_PMUVer_SHIFT, - ID_AA64DFR0_EL1_PMUVer_V3P5); - return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), tmp); + pmuver =3D SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, + read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1)); + + /* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ + if (pmuver =3D=3D ID_AA64DFR0_EL1_PMUVer_IMP_DEF) + return 0; + + return min(pmuver, ID_AA64DFR0_EL1_PMUVer_V3P5); } =20 /** --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0DEA20E022 for ; Mon, 3 Feb 2025 18:32:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607563; cv=none; b=XM1mz8tIhr0mZPISQEM/PDC1Aa/iUAEl6xxhNzkJKgnCqL/lpU9bWx2FNMmk0gVdVSMt/lqzQwcIAghaXntNWHBk3A/fkLf/TBQYkPsfGUiPVnlfW71LErJH4zB96/q3nILb57s8PwAoyW3Y2dbDohM4OI8uxdZMues5w+q3Kws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607563; c=relaxed/simple; bh=qI9InkdJlG3ONsCF2amZtFUht/2CoaJ8pQWWGv2hGTo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rkotzGLARN1sFb4n6Rx6XdLEoR0tHvWvwXPTIU1dZRO/EKoVo9Y2HONPFkdYOuVilCj9kVk7p1ZkMB+jGVq011GWOYDd9AYVxx1jneYknL2GjRb1yWiXJMiuf3fA8z8qHLssdg3yWfUsvxju1yn5ED8hkb1X8UBDIvn2H+y7ahg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=LW15B7nB; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="LW15B7nB" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=760i6dR476/MTNrnwbRQgXi1sS911UZgG0OKDU/QKBc=; b=LW15B7nB+9b3JDc/pkJfTqG1u/J5RKDp9Kt6nctYtrFiGGNKtWoZJTQOFg6JtRBJph4oIB jNZp4vnvuRueOaCNgjT1GJ84A278jDRfCJPdHPZMXP2UZ81knbnnjdKx043jcREqd3NLYq ysF4YUwju1LsdY0/7RwZcgGHeLumoOw= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 11/14] KVM: arm64: Compute synthetic sysreg ESR for Apple PMUv3 traps Date: Mon, 3 Feb 2025 10:31:08 -0800 Message-Id: <20250203183111.191519-12-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Apple M* CPUs provide an IMPDEF trap for PMUv3 sysregs, where ESR_EL2.EC is a reserved value (0x3F) and a sysreg-like ISS is reported in AFSR1_EL2. Compute a synthetic ESR for these PMUv3 traps, giving the illusion of something architectural to the rest of KVM. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/hyp/vhe/switch.c | 22 ++++++++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 2 files changed, 23 insertions(+) diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switc= h.c index b5b9dbaf1fdd..3456996dd65f 100644 --- a/arch/arm64/kvm/hyp/vhe/switch.c +++ b/arch/arm64/kvm/hyp/vhe/switch.c @@ -525,6 +525,25 @@ static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu = *vcpu, u64 *exit_code) return kvm_hyp_handle_sysreg(vcpu, exit_code); } =20 +static bool kvm_hyp_handle_impdef(struct kvm_vcpu *vcpu, u64 *exit_code) +{ + u64 iss; + + if (!cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS)) + return false; + + /* + * Compute a synthetic ESR for a sysreg trap. Conveniently, AFSR1_EL2 + * is populated with a correct ISS for a sysreg trap. These fruity + * parts are 64bit only, so unconditionally set IL. + */ + iss =3D ESR_ELx_ISS(read_sysreg_s(SYS_AFSR1_EL2)); + vcpu->arch.fault.esr_el2 =3D FIELD_PREP(ESR_ELx_EC_MASK, ESR_ELx_EC_SYS64= ) | + FIELD_PREP(ESR_ELx_ISS_MASK, iss) | + ESR_ELx_IL; + return false; +} + static const exit_handler_fn hyp_exit_handlers[] =3D { [0 ... ESR_ELx_EC_MAX] =3D NULL, [ESR_ELx_EC_CP15_32] =3D kvm_hyp_handle_cp15_32, @@ -536,6 +555,9 @@ static const exit_handler_fn hyp_exit_handlers[] =3D { [ESR_ELx_EC_WATCHPT_LOW] =3D kvm_hyp_handle_watchpt_low, [ESR_ELx_EC_ERET] =3D kvm_hyp_handle_eret, [ESR_ELx_EC_MOPS] =3D kvm_hyp_handle_mops, + + /* Apple shenanigans */ + [0x3F] =3D kvm_hyp_handle_impdef, }; =20 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *= vcpu) diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index ee4316cb3690..772c1b008e43 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -105,6 +105,7 @@ WORKAROUND_CAVIUM_TX2_219_TVM WORKAROUND_CLEAN_CACHE WORKAROUND_DEVICE_LOAD_ACQUIRE WORKAROUND_NVIDIA_CARMEL_CNP +WORKAROUND_PMUV3_IMPDEF_TRAPS WORKAROUND_QCOM_FALKOR_E1003 WORKAROUND_QCOM_ORYON_CNTVOFF WORKAROUND_REPEAT_TLBI --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7950620E022 for ; Mon, 3 Feb 2025 18:32:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607571; cv=none; b=dMmjmdRDR64RCPZS2m0glEv8b3rJC9YfnU7uXO0jOpkd8Ikbg84QL7tH/gAcOt67oE6Tp6qsrKpVVHP3poSButu+4vazlgJ9a9SPPI5E2cpbwlK/E/7ge4Fb5M46Z7igNY+N4bCwocXkOaVonhkaWIfMcgT6geilxH57muqmhC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607571; c=relaxed/simple; bh=AH+nkSEjSOmGlcRzTX6OKf73NS+zmT79gH8M3qzluS4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j9mW+KxtU1s5j4yqLzVbUdmEW5jDZsFzwK3abbcdGhwYdPzlI+DCYzPaC2be8+gotjKHeoW5D4dXOKBcqm334kCs/q1XsgLRXieoI6Kteqejq/g8BMfpgKdzckBrzxTTuOkQOsE7+djBFqmnCcaMsBtuhMCj6eY+kKefuNCgOAc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ASPv2DWs; arc=none smtp.client-ip=91.218.175.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ASPv2DWs" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607562; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tsB8DsHJdiQoqXPS3Rw6NkqRDeCazk+YgKLVcGUuCus=; b=ASPv2DWsSwlCcEkU96RkM6PGoyvIht2TDFL/inVk8yPyCKy2mLCUUN222fgZQee9DgABM2 4QZAfFnvlRePJ5Drk4uDoCt+AFlkWHkGVuKJpVqEPGF0SvTddMP6bWlIuh3EQGfGLDzwmy vay3ZczDkikQOVpNJAZqwnJH5KOIH44= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 12/14] KVM: arm64: Advertise PMUv3 if IMPDEF traps are present Date: Mon, 3 Feb 2025 10:31:09 -0800 Message-Id: <20250203183111.191519-13-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Advertise a baseline PMUv3 implementation when running on hardware with IMPDEF traps of the PMUv3 sysregs. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 57ef4f2814fc..97f29153193f 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1243,7 +1243,17 @@ u8 kvm_arm_pmu_get_pmuver_limit(void) pmuver =3D SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1)); =20 - /* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ + /* + * Spoof a barebones PMUv3 implementation if the system supports IMPDEF + * traps of the PMUv3 sysregs + */ + if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS)) + return ID_AA64DFR0_EL1_PMUVer_IMP; + + /* + * Otherwise, treat IMPLEMENTATION DEFINED functionality as + * unimplemented + */ if (pmuver =3D=3D ID_AA64DFR0_EL1_PMUVer_IMP_DEF) return 0; =20 --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A6EC20E32A for ; Mon, 3 Feb 2025 18:32:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607575; cv=none; b=nSgqcU3J55Do53QarhcYnXmm+s6cac5KaZ9AfMJGzoceuNFq7kFHpAN5LQEprUcn9B4Y6ggI/3tPayBePPvmDoGBja9qonX5ehAd2ot89vSVzijt603cpxOnyYJA8csEwYWp8ZopjpXLdyO9y4XJNel+2tpfMzRwUS7/62j4s8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607575; c=relaxed/simple; bh=nMm0cEzXGJ+K6fquIlXfLGgHUP0RZmUtQhbzRpZqTw8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ejzcGOPMYy/CIGtYH6VpCtn1IUi1gi9i88IXXAeE+oOEaWGPbMtK7xJUdKxRphLHIuoX1YqVz8mT4xvEyWZTPwWEMzzQwPruLi7/7lAZUhaxuebBpohrdMFdW3HuInZ1ZDNbURXQeRGPLA2dYZWvnn+rIaO/ANf24sKMGmw3IIg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=lP01iyqm; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="lP01iyqm" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607570; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KIafL33okyEmcVqNJE6QQCzbjIY3PK1DaOakrU1GSY0=; b=lP01iyqm6GNcp0HdNtiFC1Ii0FCWCppkajIGNLxRboyOjkM2ANz3FgLewjCMiMHlGFL9wl 0OX/FAlJpnLXNNHx2tDEtY/5CsI5NFvnF34zvS39CZ+q22UIix7AACIYYBhRLORqxUz1+4 bMhhbIXB8UmUGzIMhQWDoZSZU02fwrU= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 13/14] KVM: arm64: Provide 1 event counter on IMPDEF hardware Date: Mon, 3 Feb 2025 10:31:10 -0800 Message-Id: <20250203183111.191519-14-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" PMUv3 requires that all programmable event counters are capable of counting any event. The Apple M* PMU is quite a bit different, and events have affinities for particular PMCs. Expose 1 event counter on IMPDEF hardware, allowing the guest to do something useful with its PMU while also upholding the requirements of the architecture. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kvm/pmu-emul.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 97f29153193f..e7b732bcaaa2 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1027,6 +1027,13 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) { struct arm_pmu *arm_pmu =3D kvm->arch.arm_pmu; =20 + /* + * PMUv3 requires that all event counters are capable of counting any + * event, though the same may not be true of non-PMUv3 hardware. + */ + if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS)) + return 1; + /* * The arm_pmu->cntr_mask considers the fixed counter(s) as well. * Ignore those and return only the general-purpose counters. --=20 2.39.5 From nobody Thu Dec 18 07:57:12 2025 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EC881D61A2 for ; Mon, 3 Feb 2025 18:32:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607576; cv=none; b=tZVJY8C0zNvcNlLAZqd1PdROTLQjV50FWlt5K6zzXmsu6lBjbLlxIZIYZR8gwdwW8mcQ1IbUqX8grZS18A0S/0PQ5Hq6m55iD9vJkXjZYCC7hAr16XhrU41B3/laeDvQP34Od8az57NjF1X16AjfSCALsRUtJ2th9Xy81wiBTtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738607576; c=relaxed/simple; bh=CMFyWCDGDbW9/mXeVsTOJ32gxb6vUpEPCaJZb22v4aI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uhMvb+F4txJKZHQ3z/feOto8sy5T09PunrUTilPJ9vMfLDIrGhm3Sp/mihMH7L7njQTeyjT7Y8kutjh41mVFxnI/azK1Wwnv8UriHeR7Tdq+uIP2KhBeoithDUmnd513RJDjgfQo7ND1Vp7SPfOamAmojys3PHRJMI1omHFiFQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=lohwccsL; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="lohwccsL" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1738607573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p04z4NA8ANBpeee2BaIv4nfZi9bpNGy7L59DW/TFIH4=; b=lohwccsLQVXqPCGCaUJpvLH2LWJNXS1hJWN3EUOWLZD8Xy0Z6P+pE/iWpnEG+x8xcjdRKm xQQ/GyjkONdhF0Iw8r/ftkacNb+HvFhns4AB+E7YE4eOx60SWOA+AzvFd+qutjeJyW/beh AvimWjSrwANWSEjXLRSTctQ83k1AAys= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: Marc Zyngier , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mingwei Zhang , Colton Lewis , Raghavendra Rao Ananta , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oliver Upton , Janne Grunau Subject: [PATCH v2 14/14] arm64: Enable IMP DEF PMUv3 traps on Apple M* Date: Mon, 3 Feb 2025 10:31:11 -0800 Message-Id: <20250203183111.191519-15-oliver.upton@linux.dev> In-Reply-To: <20250203183111.191519-1-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Apple M1 and M2 CPUs support IMPDEF traps of the PMUv3 sysregs, allowing a hypervisor to virtualize an architectural PMU for a VM. Flip the appropriate bit in HACR_EL2 on supporting hardware. Tested-by: Janne Grunau Signed-off-by: Oliver Upton --- arch/arm64/kernel/cpu_errata.c | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index 7ce555862895..a1e16b156fab 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -194,6 +194,43 @@ has_neoverse_n1_erratum_1542419(const struct arm64_cpu= _capabilities *entry, return is_midr_in_range(midr, &range) && has_dic; } =20 +static const struct midr_range impdef_pmuv3_cpus[] =3D { + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM), + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM), + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM_PRO), + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM_PRO), + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_ICESTORM_MAX), + MIDR_ALL_VERSIONS(MIDR_APPLE_M1_FIRESTORM_MAX), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD_PRO), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_PRO), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD_MAX), + MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_MAX), + {}, +}; + +static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, i= nt scope) +{ + u64 dfr0 =3D read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); + unsigned int pmuver; + + if (!is_kernel_in_hyp_mode()) + return false; + + pmuver =3D cpuid_feature_extract_unsigned_field(dfr0, + ID_AA64DFR0_EL1_PMUVer_SHIFT); + if (pmuver !=3D ID_AA64DFR0_EL1_PMUVer_IMP_DEF) + return false; + + return is_midr_in_range_list(read_cpuid_id(), impdef_pmuv3_cpus); +} + +static void cpu_enable_impdef_pmuv3_traps(const struct arm64_cpu_capabilit= ies *__unused) +{ + sysreg_clear_set_s(SYS_HACR_EL2, 0, BIT(56)); +} + #ifdef CONFIG_ARM64_WORKAROUND_REPEAT_TLBI static const struct arm64_cpu_capabilities arm64_repeat_tlbi_list[] =3D { #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009 @@ -794,6 +831,13 @@ const struct arm64_cpu_capabilities arm64_errata[] =3D= { {} })), }, + { + .desc =3D "Apple IMPDEF PMUv3 Traps", + .capability =3D ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS, + .type =3D ARM64_CPUCAP_LOCAL_CPU_ERRATUM, + .matches =3D has_impdef_pmuv3, + .cpu_enable =3D cpu_enable_impdef_pmuv3_traps, + }, { } }; --=20 2.39.5