From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F1C4DC021AA for ; Wed, 19 Feb 2025 16:39:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6133/76gB3gWETfShX/iMSttG5argnfv9QscC0l3ym4=; b=xodIBtfjieNR8g0jpQ5MMLeCPy aSuoR8AHDMYzTzXlZDjQZyXIjJtNaTjV9D+KcdLrE4UPSZmDGW13ZjhDh5P7SIntoYkrQnro2zKrW 0gj72G9ok8ujtiQw+LE65cucJeUs6gd3ZnRxKgkleQBlwT/NdneGhK3wwN+vYwXS/bZ/t8YHVceWh 80bQ5OJbnDsNgrLajpU8rF4m1guSK/wLLxLfHKjtiQw95UyKXNP0HHWPwHEnSGdsnHZrRCpK6G1AW hep5ApQNPku+k7SIZQBkDyWV4vonSwrP5+aF7zxFeuAun0VCyGZk2jyaH2B2tFXm3rLF/8Vant5BA m7D5GHRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tkn6j-0000000DroL-0u22; Wed, 19 Feb 2025 16:39:33 +0000 Received: from nyc.source.kernel.org ([147.75.193.91]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tkn5E-0000000DrPX-27Zp for linux-arm-kernel@lists.infradead.org; Wed, 19 Feb 2025 16:38:01 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 6E158A42C2B; Wed, 19 Feb 2025 16:36:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6D65C4CED1; Wed, 19 Feb 2025 16:37:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739983078; bh=zioUkanDxHPR6vNqtgV7v27+nHpYpX9hIMY9zLaK/MI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OmDZpBqVgqaYGBWTpeZEO5oDQmEUIVp2gaY6mlfXoWLnDflb+ktH9/VCXBJaGmsfc RQxoU00FJMJClVDHf9myP3G0WVUyvON7PSKU7vPjAzS8GD4HBQN03BmcWPTh+jeUvE KvrNpLdZG642xBHru+0MTGOxASROKt4E+flniykp/SZi6yE+1pcA1VwSdqru5cJ0yz Xb2TQLKdRPZWUTF/V/2sLsElDFzLNcMMZKplK+54lTdKTv5998AIVCqOLI+QBtuWzG 65TRFAxTvASMlrdC049rkQFQY0q5LE6d/9mSDNgDL54qajKLXHe4tnCrMDgUqDjh2a 3xzBK/yujG7IA== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tkn5A-005tju-Dm; Wed, 19 Feb 2025 16:37:56 +0000 Date: Wed, 19 Feb 2025 16:37:56 +0000 Message-ID: <867c5lsxej.wl-maz@kernel.org> From: Marc Zyngier To: Oliver Upton Cc: kvmarm@lists.linux.dev, 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, Janne Grunau Subject: Re: [PATCH v2 03/14] drivers/perf: apple_m1: Provide helper for mapping PMUv3 events In-Reply-To: <20250203183111.191519-4-oliver.upton@linux.dev> References: <20250203183111.191519-1-oliver.upton@linux.dev> <20250203183111.191519-4-oliver.upton@linux.dev> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.4 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, kvmarm@lists.linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, mizhang@google.com, coltonlewis@google.com, rananta@google.com, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, j@jannau.net X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250219_083800_678193_BA543797 X-CRM114-Status: GOOD ( 27.81 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, 03 Feb 2025 18:31:00 +0000, Oliver Upton wrote: > > 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_pmu.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 @@ > > #include > #include > +#include > #include > > #include > @@ -174,6 +175,17 @@ static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = { > [PERF_COUNT_HW_BRANCH_MISSES] = M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC, > }; > > +#define M1_PMUV3_EVENT_MAP(pmuv3_event, m1_event) \ > + [ARMV8_PMUV3_PERFCTR_##pmuv3_event] = M1_PMU_PERFCTR_##m1_event > + > +static const unsigned int m1_pmu_pmceid_map[ARMV8_PMUV3_MAX_COMMON_EVENTS] = { nit: M1_PMU_PERFCTR* are limited to 8bit quantities, and HW_OP_UNSUPPORTED is a 16bit constant. Probably worth changing the type to u16 and save a whole 128 bytes! ;-) > + [0 ... ARMV8_PMUV3_MAX_COMMON_EVENTS - 1] = 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); > } > > +static int m1_pmu_map_pmuv3_event(unsigned int eventsel) > +{ > + int m1_event = HW_OP_UNSUPPORTED; unsigned? > + > + if (eventsel < ARMV8_PMUV3_MAX_COMMON_EVENTS) > + m1_event = m1_pmu_pmceid_map[eventsel]; > + > + return m1_event == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : m1_event; > +} > + > +static void m1_pmu_init_pmceid(struct arm_pmu *pmu) > +{ > + unsigned int event; > + > + for (event = 0; event < ARMV8_PMUV3_MAX_COMMON_EVENTS; event++) { > + if (m1_pmu_map_pmuv3_event(event) >= 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 flags) > cpu_pmu->reset = m1_pmu_reset; > cpu_pmu->set_event_filter = m1_pmu_set_event_filter; > > + cpu_pmu->map_pmuv3_event = 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] = &m1_pmu_events_attr_group; > cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = &m1_pmu_format_attr_group; > 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 I think it may be more logical to introduce this new callback first, with its usage in KVM, and only then plug it in the PMU driver. Not a big deal though. M. -- Without deviation from the norm, progress is not possible.