Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3415997rdh; Mon, 27 Nov 2023 13:49:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEX2cpH/2Z9nF8oyTTMgbB1/BLGd3/hhl8syChVweJQAkKk78URkkFFyXSXl9EedVs4k9LO X-Received: by 2002:a05:6a20:7352:b0:18c:2d7:b84c with SMTP id v18-20020a056a20735200b0018c02d7b84cmr14441477pzc.61.1701121764813; Mon, 27 Nov 2023 13:49:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701121764; cv=none; d=google.com; s=arc-20160816; b=RyfMUfnPQqbMlAjjNTngQ6AKW9KmSDEbbamMqwTUaEDzGmNr+O20pieDDnV8TtaIvB IXIcAZzgNfITbRs1rPAlWx7kvZrR09qzksgs6QghbLKLfTtjRQw56SOYuUSK/GOPTuNs nsM/aMuX1EJCOldDcdm/+NcKrNfC6kry4zc6/4cd0POMpinLsLiEsF0IeWdFXmmDHA3N Uyb57eK4ObTuLccZOiVd/LT+O/HY2+ckPEoK3VIgu/4ynwy/Cfp6CSO3YONdIOUvKNjl MtUO6Dgti5D2IAWPW0cFfSIX7rBRG5KZ4+2XlmWk64MNyMAEIEo3YJ6Adh7i/GKMJ28P 8ejg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=rL7KVwhVfPkfWvKDrxAO+rIkZlfltIKSwwMISdQlBCo=; fh=ni5BsR4W2XDn37ZkODec/OWvj2FNtunG+53Db4f25D4=; b=SwCuGYr/W+YPTJPPsj1Q21wivcBM9DYrTgFBbGoj7LkqjcP9AkrPq/qF7OI2cjHOFM xdcm72Z/bDKTlVl5knZA638PwHubSQPufvy44dm9Ljkb14EkksHPzzg3fHdnYOZaReV+ 0BKq/IZ88fcW+mBRKIRvc3jctZpdHCwY3iPgf9NBb1fsHSZM1am613t07ZdIYc42QXui 3f+58XaE0XDFA+EFSPi1312hhlfNqLb19R0+2owBC+Prqoq9FK6TC7+6C3psctnkxraG TOAdE+QA2fferA3LTBGG2HIUmnztB7aQG4NYB4SagwUmVajRCBB/rW/8a4CXF+GiBzIe BwPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qrJmucY9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id d7-20020a656207000000b005c2135c5018si10424312pgv.571.2023.11.27.13.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 13:49:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qrJmucY9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 8D70E807BA3A; Mon, 27 Nov 2023 13:49:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233260AbjK0VtD (ORCPT + 99 others); Mon, 27 Nov 2023 16:49:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231437AbjK0VtC (ORCPT ); Mon, 27 Nov 2023 16:49:02 -0500 Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05C8BBD for ; Mon, 27 Nov 2023 13:49:08 -0800 (PST) Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-35c6cefa872so3715ab.0 for ; Mon, 27 Nov 2023 13:49:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701121747; x=1701726547; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rL7KVwhVfPkfWvKDrxAO+rIkZlfltIKSwwMISdQlBCo=; b=qrJmucY9Sx2i/mAfpEhGmev/PugqYK3d99REexs6RKGEYsO696OICgXk+Ds0nY6Of6 hIn6eIcBKNIsEogEw+t7tovT6bTzkMMOqv1yZjGPEoLMNy2MCCX6UNArTnZTTaNbP6yb N/ljkd6OABwthiM+hetKtxH/5Nw2FHjFnvJGNpVP0bjStJfZmwJePKfrPXaG80w8YwQ7 kNvqznrkH+2uaTijc0Vyxft1LV4DGHxhn5EkWRlJxIRBmVeegeC+ny1cmEykF0kOsuQ/ zSOI1CFgS4MSDFGR4w6282t2K6E+k9QSDZdhD31dOom2bStf8tWH5KaNREVndJlsCgZo xxRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701121747; x=1701726547; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rL7KVwhVfPkfWvKDrxAO+rIkZlfltIKSwwMISdQlBCo=; b=A3jkPV2nd8OOBlCv5LT8RkwRS/ENG/FEQdaU3hDWCEZnWXSZVfxSwEAa6P8eX4ivPP KrxI7QkrdD7vGDezQIS7NVwSRj3qUiBWtLGtLzxNjLVXbNbiiDkVh081uR0w+QP9NDtn ngxpZMHJZ03ftpcSNHnsx8XWorWuJMoMC39AxsfODCE6XKXGq/yDm887NyD/+6EHMgY8 N0RylX71VnpreHFNLLz/fXN6fq/OhMMV0KFavH7I6he6kSRmNflrl6EMJbTdFJNGifuL Qa/MFNuWsPzhawoa/1Hj3t/cA16UplvWiZTd4mpc6J91niRl/7UW4lKxRM2oumZpxnxn Y3/Q== X-Gm-Message-State: AOJu0Yw6xk/GfGKGogDsqInkyF2x6xiGDzBt8+XOCTRqh1vwmIW4lCGz ikcHpqBuTaULn2wdKX1APQqhft5OO7qA7RJHxkOQdA== X-Received: by 2002:a92:de10:0:b0:35c:b910:3493 with SMTP id x16-20020a92de10000000b0035cb9103493mr269011ilm.29.1701121747268; Mon, 27 Nov 2023 13:49:07 -0800 (PST) MIME-Version: 1.0 References: <20231123063750.2176250-1-shahuang@redhat.com> <20231123063750.2176250-3-shahuang@redhat.com> In-Reply-To: <20231123063750.2176250-3-shahuang@redhat.com> From: Raghavendra Rao Ananta Date: Mon, 27 Nov 2023 13:48:55 -0800 Message-ID: Subject: Re: [PATCH v1 2/3] KVM: selftests: aarch64: Move the pmu helper function into lib/ To: Shaoqin Huang Cc: Oliver Upton , Marc Zyngier , kvmarm@lists.linux.dev, James Morse , Suzuki K Poulose , Zenghui Yu , Paolo Bonzini , Shuah Khan , linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 27 Nov 2023 13:49:21 -0800 (PST) Hi Shaoqin, On Wed, Nov 22, 2023 at 10:39=E2=80=AFPM Shaoqin Huang wrote: > > Move those pmu helper function into lib/, thus it can be used by other > pmu test. > > Signed-off-by: Shaoqin Huang > --- > .../kvm/aarch64/vpmu_counter_access.c | 118 ----------------- > .../selftests/kvm/include/aarch64/vpmu.h | 119 ++++++++++++++++++ > 2 files changed, 119 insertions(+), 118 deletions(-) > > diff --git a/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c b/= tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c > index 17305408a334..62d6315790ab 100644 > --- a/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c > +++ b/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c > @@ -20,12 +20,6 @@ > #include > #include > > -/* The max number of the PMU event counters (excluding the cycle counter= ) */ > -#define ARMV8_PMU_MAX_GENERAL_COUNTERS (ARMV8_PMU_MAX_COUNTERS - 1) > - > -/* The cycle counter bit position that's common among the PMU registers = */ > -#define ARMV8_PMU_CYCLE_IDX 31 > - > static struct vpmu_vm *vpmu_vm; > > struct pmreg_sets { > @@ -35,118 +29,6 @@ struct pmreg_sets { > > #define PMREG_SET(set, clr) {.set_reg_id =3D set, .clr_reg_id =3D clr} > > -static uint64_t get_pmcr_n(uint64_t pmcr) > -{ > - return (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK; > -} > - > -static void set_pmcr_n(uint64_t *pmcr, uint64_t pmcr_n) > -{ > - *pmcr =3D *pmcr & ~(ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHI= FT); > - *pmcr |=3D (pmcr_n << ARMV8_PMU_PMCR_N_SHIFT); > -} > - > -static uint64_t get_counters_mask(uint64_t n) > -{ > - uint64_t mask =3D BIT(ARMV8_PMU_CYCLE_IDX); > - > - if (n) > - mask |=3D GENMASK(n - 1, 0); > - return mask; > -} > - > -/* Read PMEVTCNTR_EL0 through PMXEVCNTR_EL0 */ > -static inline unsigned long read_sel_evcntr(int sel) > -{ > - write_sysreg(sel, pmselr_el0); > - isb(); > - return read_sysreg(pmxevcntr_el0); > -} > - > -/* Write PMEVTCNTR_EL0 through PMXEVCNTR_EL0 */ > -static inline void write_sel_evcntr(int sel, unsigned long val) > -{ > - write_sysreg(sel, pmselr_el0); > - isb(); > - write_sysreg(val, pmxevcntr_el0); > - isb(); > -} > - > -/* Read PMEVTYPER_EL0 through PMXEVTYPER_EL0 */ > -static inline unsigned long read_sel_evtyper(int sel) > -{ > - write_sysreg(sel, pmselr_el0); > - isb(); > - return read_sysreg(pmxevtyper_el0); > -} > - > -/* Write PMEVTYPER_EL0 through PMXEVTYPER_EL0 */ > -static inline void write_sel_evtyper(int sel, unsigned long val) > -{ > - write_sysreg(sel, pmselr_el0); > - isb(); > - write_sysreg(val, pmxevtyper_el0); > - isb(); > -} > - > -static inline void enable_counter(int idx) > -{ > - uint64_t v =3D read_sysreg(pmcntenset_el0); > - > - write_sysreg(BIT(idx) | v, pmcntenset_el0); > - isb(); > -} > - > -static inline void disable_counter(int idx) > -{ > - uint64_t v =3D read_sysreg(pmcntenset_el0); > - > - write_sysreg(BIT(idx) | v, pmcntenclr_el0); > - isb(); > -} > - > -static void pmu_disable_reset(void) > -{ > - uint64_t pmcr =3D read_sysreg(pmcr_el0); > - > - /* Reset all counters, disabling them */ > - pmcr &=3D ~ARMV8_PMU_PMCR_E; > - write_sysreg(pmcr | ARMV8_PMU_PMCR_P, pmcr_el0); > - isb(); > -} > - > -#define RETURN_READ_PMEVCNTRN(n) \ > - return read_sysreg(pmevcntr##n##_el0) > -static unsigned long read_pmevcntrn(int n) > -{ > - PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN); > - return 0; > -} > - > -#define WRITE_PMEVCNTRN(n) \ > - write_sysreg(val, pmevcntr##n##_el0) > -static void write_pmevcntrn(int n, unsigned long val) > -{ > - PMEVN_SWITCH(n, WRITE_PMEVCNTRN); > - isb(); > -} > - > -#define READ_PMEVTYPERN(n) \ > - return read_sysreg(pmevtyper##n##_el0) > -static unsigned long read_pmevtypern(int n) > -{ > - PMEVN_SWITCH(n, READ_PMEVTYPERN); > - return 0; > -} > - > -#define WRITE_PMEVTYPERN(n) \ > - write_sysreg(val, pmevtyper##n##_el0) > -static void write_pmevtypern(int n, unsigned long val) > -{ > - PMEVN_SWITCH(n, WRITE_PMEVTYPERN); > - isb(); > -} > - > /* > * The pmc_accessor structure has pointers to PMEV{CNTR,TYPER}_EL0 > * accessors that test cases will use. Each of the accessors will > diff --git a/tools/testing/selftests/kvm/include/aarch64/vpmu.h b/tools/t= esting/selftests/kvm/include/aarch64/vpmu.h > index 0a56183644ee..e0cc1ca1c4b7 100644 > --- a/tools/testing/selftests/kvm/include/aarch64/vpmu.h > +++ b/tools/testing/selftests/kvm/include/aarch64/vpmu.h > @@ -1,10 +1,17 @@ > /* SPDX-License-Identifier: GPL-2.0 */ > > #include > +#include > > #define GICD_BASE_GPA 0x8000000ULL > #define GICR_BASE_GPA 0x80A0000ULL > > +/* The max number of the PMU event counters (excluding the cycle counter= ) */ > +#define ARMV8_PMU_MAX_GENERAL_COUNTERS (ARMV8_PMU_MAX_COUNTERS - 1) > + > +/* The cycle counter bit position that's common among the PMU registers = */ > +#define ARMV8_PMU_CYCLE_IDX 31 > + > struct vpmu_vm { > struct kvm_vm *vm; > struct kvm_vcpu *vcpu; > @@ -14,3 +21,115 @@ struct vpmu_vm { > struct vpmu_vm *create_vpmu_vm(void *guest_code); > > void destroy_vpmu_vm(struct vpmu_vm *vpmu_vm); > + > +static inline uint64_t get_pmcr_n(uint64_t pmcr) > +{ > + return (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK; > +} > + > +static inline void set_pmcr_n(uint64_t *pmcr, uint64_t pmcr_n) > +{ > + *pmcr =3D *pmcr & ~(ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHI= FT); > + *pmcr |=3D (pmcr_n << ARMV8_PMU_PMCR_N_SHIFT); > +} > + > +static inline uint64_t get_counters_mask(uint64_t n) > +{ > + uint64_t mask =3D BIT(ARMV8_PMU_CYCLE_IDX); > + > + if (n) > + mask |=3D GENMASK(n - 1, 0); > + return mask; > +} > + > +/* Read PMEVTCNTR_EL0 through PMXEVCNTR_EL0 */ > +static inline unsigned long read_sel_evcntr(int sel) > +{ > + write_sysreg(sel, pmselr_el0); > + isb(); > + return read_sysreg(pmxevcntr_el0); > +} > + > +/* Write PMEVTCNTR_EL0 through PMXEVCNTR_EL0 */ > +static inline void write_sel_evcntr(int sel, unsigned long val) > +{ > + write_sysreg(sel, pmselr_el0); > + isb(); > + write_sysreg(val, pmxevcntr_el0); > + isb(); > +} > + > +/* Read PMEVTYPER_EL0 through PMXEVTYPER_EL0 */ > +static inline unsigned long read_sel_evtyper(int sel) > +{ > + write_sysreg(sel, pmselr_el0); > + isb(); > + return read_sysreg(pmxevtyper_el0); > +} > + > +/* Write PMEVTYPER_EL0 through PMXEVTYPER_EL0 */ > +static inline void write_sel_evtyper(int sel, unsigned long val) > +{ > + write_sysreg(sel, pmselr_el0); > + isb(); > + write_sysreg(val, pmxevtyper_el0); > + isb(); > +} > + > +static inline void enable_counter(int idx) > +{ > + uint64_t v =3D read_sysreg(pmcntenset_el0); > + > + write_sysreg(BIT(idx) | v, pmcntenset_el0); > + isb(); > +} > + > +static inline void disable_counter(int idx) > +{ > + uint64_t v =3D read_sysreg(pmcntenset_el0); > + > + write_sysreg(BIT(idx) | v, pmcntenclr_el0); > + isb(); > +} > + As mentioned in [1], the current implementation of disable_counter() is buggy and would end up disabling all the counters. However if you intend to keep it (even though it would remain unused), may be change the definition something to: static inline void disable_counter(int idx) { write_sysreg(BIT(idx), pmcntenclr_el0); isb(); } Thank you. Raghavendra [1]: https://lore.kernel.org/all/20231122221526.2750966-1-rananta@google.co= m/ > +static inline void pmu_disable_reset(void) > +{ > + uint64_t pmcr =3D read_sysreg(pmcr_el0); > + > + /* Reset all counters, disabling them */ > + pmcr &=3D ~ARMV8_PMU_PMCR_E; > + write_sysreg(pmcr | ARMV8_PMU_PMCR_P, pmcr_el0); > + isb(); > +} > + > +#define RETURN_READ_PMEVCNTRN(n) \ > + return read_sysreg(pmevcntr##n##_el0) > +static inline unsigned long read_pmevcntrn(int n) > +{ > + PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN); > + return 0; > +} > + > +#define WRITE_PMEVCNTRN(n) \ > + write_sysreg(val, pmevcntr##n##_el0) > +static inline void write_pmevcntrn(int n, unsigned long val) > +{ > + PMEVN_SWITCH(n, WRITE_PMEVCNTRN); > + isb(); > +} > + > +#define READ_PMEVTYPERN(n) \ > + return read_sysreg(pmevtyper##n##_el0) > +static inline unsigned long read_pmevtypern(int n) > +{ > + PMEVN_SWITCH(n, READ_PMEVTYPERN); > + return 0; > +} > + > +#define WRITE_PMEVTYPERN(n) \ > + write_sysreg(val, pmevtyper##n##_el0) > +static inline void write_pmevtypern(int n, unsigned long val) > +{ > + PMEVN_SWITCH(n, WRITE_PMEVTYPERN); > + isb(); > +} > -- > 2.40.1 > >