Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4978646rdb; Sat, 30 Dec 2023 00:10:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGlJCGfG/FQhhXS9pZUT6xdjDBLDXr57a0UjJAGk2mgC232FexclQHdcdJmd0iIab5YWyO4 X-Received: by 2002:ac8:7c41:0:b0:428:1276:1f52 with SMTP id o1-20020ac87c41000000b0042812761f52mr435429qtv.26.1703923845991; Sat, 30 Dec 2023 00:10:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703923845; cv=none; d=google.com; s=arc-20160816; b=rH9UY6PuOjrrkUoco51nVRVWeEqlTHzwtocOtNCUy4gtvfjA4pi+jxU9Hah5SGiN/J mRlC+q647Ae+fgrmgYWThMkDaO5XRcFCGKhST1IziAG1Fb4icVTxOL8o1O17hJhtY497 oR33sSBEKV17KhbyrVJ1QOOG0/z6HPVoDCNzRRJyloaGpCnlnAoXAsqBSSt3atH/pkJr k1xizvaHThpveE62SQ7nRxBN9xdrBRS+5ykUxcpvY9cH17T44Nf69nphS4PmuXecWIcx YORx0wk7PbtDx8AmNhTFdvYhnsPsKEj6e+dE8zKKtpXg/EdTkzcC5HzhM//tfyke6f7a Upfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=yiNrSH9oUjINY/vtkmdArFK8GmKKmu32NZyqnMgcK1w=; fh=FsN7YI7vtCi1XL+rgBNsdMzf1zwrS55wZ61JkiQB8AM=; b=bn1BhaWGj9cnX6Gj+MpDez2L2ZBplSe0gguu8VGCW35hppQ0IElheFqGzTcScSWPMy umH4RaXPA5jweODJ2+RiSmC36ZwxvOs8VlgRRGuL1pIU3UH5x6kVe8dekpBMkURxXnZ+ tyRQ6IURQTRYx2iqwSFQclPvbF1Gbn3nG6pw8l57hoLeMafapOWy2wuUZo2lfVzdAUlK DWmvnE6j8Rp24tPOLcLfUE07u8q8n/S4mcgLFw151lXT4kgw4QBrmuKNwyI5RuVCHLHY hksSlGKxmLePlYAE3hczo1fc8CI7kJ9aIrfpPqI++4ERf8LIRzzDHu5lqD+pNPtSPvIy rPEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20230601.gappssmtp.com header.s=20230601 header.b=SWbnYKeE; spf=pass (google.com: domain of linux-kernel+bounces-13361-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13361-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q18-20020a05622a04d200b00425ef4a05d2si20021403qtx.7.2023.12.30.00.10.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Dec 2023 00:10:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13361-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20230601.gappssmtp.com header.s=20230601 header.b=SWbnYKeE; spf=pass (google.com: domain of linux-kernel+bounces-13361-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13361-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AF1B21C20A00 for ; Sat, 30 Dec 2023 08:10:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7B6663C1E; Sat, 30 Dec 2023 08:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=brainfault-org.20230601.gappssmtp.com header.i=@brainfault-org.20230601.gappssmtp.com header.b="SWbnYKeE" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-io1-f49.google.com (mail-io1-f49.google.com [209.85.166.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58B1A23AE for ; Sat, 30 Dec 2023 08:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=brainfault.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=brainfault.org Received: by mail-io1-f49.google.com with SMTP id ca18e2360f4ac-7ba737ee9b5so530850739f.0 for ; Sat, 30 Dec 2023 00:10:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20230601.gappssmtp.com; s=20230601; t=1703923830; x=1704528630; 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=yiNrSH9oUjINY/vtkmdArFK8GmKKmu32NZyqnMgcK1w=; b=SWbnYKeEQgImfiXRdKfjLr17q3Ih3+w1K9rEy2kvHK6ESKTDS6sz/SBGDyQ1o3ek2X 22Visn/sb/E9nK4NtWHJ7pufyiMXfCEHyHAuIr0zuSlHNh8U1v7CRko/zRCtl9tW2Smq dtaP5i/xU1pjP2x75A/UO2n5oXTVKdcpIUxL/xsVotPNgA5CGWW4DfL+0AwTLgs6ogPd KSLqbbG5Q0uhwp9UqQk4x+32vQ+AI7AGO0CfmCZGE777OKyqAh/Wu1vtBnF+kT5R9Odd RVqSgbq/PZJagDFCv2g79ETWvLRM6PRlwOWzX9T112HrchgizNANi2tRf0dv4QE2lSj+ UceQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703923830; x=1704528630; 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=yiNrSH9oUjINY/vtkmdArFK8GmKKmu32NZyqnMgcK1w=; b=Lk9PWeFF6saN9ppODm30+kuTti4JOdzpY+xFrsQAzMLwDUg3hw+jjO4OuoKu17x2RE HfDdTfPI4CfgrXARV2LJcESbi2XUbBO9fMz0ja8zOMvSlOo1YDuUXsa7Aj+SJH2Dpdb3 0WMfiE0+WR9B/XPxx3tDJrDh6OnZV8+H2QcvlvymQ+QmF/mFekI3NnuFPeTCTMlaYiz7 xJYSidIgoq1Q1Cu95Cq4VcfXGKscD/L91CeCpzE9TOW4KWKIp+/vtHDGZg/0AMEUcwBY KOJqXQeDfCBcyjzZV8piOnsPmD60a2W5SuF9/ZdQalCC8cn+AatkG8gPWdtOKnOHWxeh lTTg== X-Gm-Message-State: AOJu0YwszMEuPMR2MWuYWIeuf4y8Om9T1hjbE13Dv0r+b2nqdsK9wKzT PX371a1zWBcdeQg4u4k4xK5KByCCaUFXNZgTTjqqBHQEnpI3Uw== X-Received: by 2002:a05:6e02:2408:b0:35d:5995:7992 with SMTP id bs8-20020a056e02240800b0035d59957992mr23726864ilb.44.1703923830374; Sat, 30 Dec 2023 00:10:30 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231229214950.4061381-1-atishp@rivosinc.com> <20231229214950.4061381-11-atishp@rivosinc.com> In-Reply-To: <20231229214950.4061381-11-atishp@rivosinc.com> From: Anup Patel Date: Sat, 30 Dec 2023 13:40:20 +0530 Message-ID: Subject: Re: [v2 10/10] RISC-V: KVM: Support 64 bit firmware counters on RV32 To: Atish Patra Cc: linux-kernel@vger.kernel.org, Albert Ou , Alexandre Ghiti , Andrew Jones , Atish Patra , Conor Dooley , Guo Ren , Heiko Stuebner , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Dec 30, 2023 at 3:20=E2=80=AFAM Atish Patra w= rote: > > The SBI v2.0 introduced a fw_read_hi function to read 64 bit firmware > counters for RV32 based systems. > > Add infrastructure to support that. > > Signed-off-by: Atish Patra LGTM. Reviewed-by: Anup Patel Regards, Anup > --- > arch/riscv/include/asm/kvm_vcpu_pmu.h | 4 ++- > arch/riscv/kvm/vcpu_pmu.c | 37 ++++++++++++++++++++++++++- > arch/riscv/kvm/vcpu_sbi_pmu.c | 6 +++++ > 3 files changed, 45 insertions(+), 2 deletions(-) > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/a= sm/kvm_vcpu_pmu.h > index af6d0ff5ce41..463c349a9ea5 100644 > --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > @@ -20,7 +20,7 @@ static_assert(RISCV_KVM_MAX_COUNTERS <=3D 64); > > struct kvm_fw_event { > /* Current value of the event */ > - unsigned long value; > + u64 value; > > /* Event monitoring status */ > bool started; > @@ -91,6 +91,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *v= cpu, unsigned long ctr_ba > struct kvm_vcpu_sbi_return *retdata)= ; > int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cid= x, > struct kvm_vcpu_sbi_return *retdata); > +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned lo= ng cidx, > + struct kvm_vcpu_sbi_return *retdata= ); > void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned lo= ng saddr_low, > unsigned long saddr_high, unsigned = long flags, > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > index f2bf5b5bdd61..e6ce37819ca2 100644 > --- a/arch/riscv/kvm/vcpu_pmu.c > +++ b/arch/riscv/kvm/vcpu_pmu.c > @@ -196,6 +196,29 @@ static int pmu_get_pmc_index(struct kvm_pmu *pmu, un= signed long eidx, > return kvm_pmu_get_programmable_pmc_index(pmu, eidx, cbase, cmask= ); > } > > +static int pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, > + unsigned long *out_val) > +{ > + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); > + struct kvm_pmc *pmc; > + int fevent_code; > + > + if (!IS_ENABLED(CONFIG_32BIT)) > + return -EINVAL; > + > + pmc =3D &kvpmu->pmc[cidx]; > + > + if (pmc->cinfo.type !=3D SBI_PMU_CTR_TYPE_FW) > + return -EINVAL; > + > + fevent_code =3D get_event_code(pmc->event_idx); > + pmc->counter_val =3D kvpmu->fw_event[fevent_code].value; > + > + *out_val =3D pmc->counter_val >> 32; > + > + return 0; > +} > + > static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > unsigned long *out_val) > { > @@ -701,6 +724,18 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu= *vcpu, unsigned long ctr_ba > return 0; > } > > +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned lo= ng cidx, > + struct kvm_vcpu_sbi_return *retdata= ) > +{ > + int ret; > + > + ret =3D pmu_fw_ctr_read_hi(vcpu, cidx, &retdata->out_val); > + if (ret =3D=3D -EINVAL) > + retdata->err_val =3D SBI_ERR_INVALID_PARAM; > + > + return 0; > +} > + > int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cid= x, > struct kvm_vcpu_sbi_return *retdata) > { > @@ -774,7 +809,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > pmc->cinfo.csr =3D CSR_CYCLE + i; > } else { > pmc->cinfo.type =3D SBI_PMU_CTR_TYPE_FW; > - pmc->cinfo.width =3D BITS_PER_LONG - 1; > + pmc->cinfo.width =3D 63; > } > } > > diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.= c > index 9f61136e4bb1..58a0e5587e2a 100644 > --- a/arch/riscv/kvm/vcpu_sbi_pmu.c > +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c > @@ -64,6 +64,12 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vc= pu, struct kvm_run *run, > case SBI_EXT_PMU_COUNTER_FW_READ: > ret =3D kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata= ); > break; > + case SBI_EXT_PMU_COUNTER_FW_READ_HI: > + if (IS_ENABLED(CONFIG_32BIT)) > + ret =3D kvm_riscv_vcpu_pmu_fw_ctr_read_hi(vcpu, c= p->a0, retdata); > + else > + retdata->out_val =3D 0; > + break; > case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: > ret =3D kvm_riscv_vcpu_pmu_setup_snapshot(vcpu, cp->a0, c= p->a1, cp->a2, retdata); > break; > -- > 2.34.1 >