Received: by 2002:a05:7412:8d23:b0:f7:29d7:fb05 with SMTP id bj35csp446226rdb; Sat, 16 Dec 2023 15:54:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IEc99audZEMdutRGcwq1KZHSFqtsvB2PIoYFWkcFGe7csUsIJcmeJlXhW2xeYcZjx5gL1ar X-Received: by 2002:a17:906:30ca:b0:a02:9c3d:6de7 with SMTP id b10-20020a17090630ca00b00a029c3d6de7mr7160749ejb.13.1702770875597; Sat, 16 Dec 2023 15:54:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702770875; cv=none; d=google.com; s=arc-20160816; b=rGv7aRS7OGbjA8azsCs4l7j/5JnYXy3lrQ+0vjXN6fwGhnTQnpFf4xFuS789jBSaCy qltLjKtF9OWmRwRX5UvvFW/O+Yw8J1QQg3KtnTsxxPSCNx6H8KwqZMg+WLmimG7bBp+N PrEZGtzg7s7+4qoX8+MHlIx2CeXddMLZETG1Nj0tDbNoqWRoEO7J2esH3/bockThm3Dt MY6UNqi2W2RoJTI+TBuvVTkWf8TeqQ131HOlKVqnWHUpAyJRl8MlByi4kxBFp9JuBTZj Ap1Ab7yM7mPk5LURAcGQ2tay6om1f69YtBHQuYvktDxzV0db+e/JR+8W0v7DBubLQzZj UTFw== 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=zrVvV1utl9GFRZ2LZ5WcICVBPuOzU+o2aQHc1q2IHuo=; fh=3AbuYYlxKBQdATOFZVilDZxFPMoRb/WWpLLVN/nlgP4=; b=w+H2nw7tL5jbSi/gOwXDrToWgk76PxBVX299FNEUenFqS1UW6QVo3QckI1dHuFsv/H IVD8L4XxMU6noeOXBD/V0PA2XpauzkTAPNlxCtURnqeiuqXogM+9cPdqKJTmLkSUM7yc Sulr8IFioEJzqjv9T/dhQ3xZFyDc71p7So7cA3aXLLCXvUNPf47wGAFNAuRTSQDPkQWr db8vJi8jes+v849Wa8PWKyH7ZlQgehltEIiTuL6tWNKdGZHhCDjDnjBXHediK36uICOD nxt0NK0Yc6CuLoNtU/9uDWQxpidx9JsoWh5AKiDiHl4amPGnEA54hn7qo7nZtFLnWBiS L5Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jX5E6oi0; spf=pass (google.com: domain of linux-kernel+bounces-2398-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2398-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r4-20020a1709062cc400b00a23149214c6si2255851ejr.354.2023.12.16.15.54.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 15:54:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2398-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jX5E6oi0; spf=pass (google.com: domain of linux-kernel+bounces-2398-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2398-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 2B8E31F22268 for ; Sat, 16 Dec 2023 23:54:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44BA237D0F; Sat, 16 Dec 2023 23:54:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="jX5E6oi0" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 5AA84374CA for ; Sat, 16 Dec 2023 23:54:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-50e23c620e8so1355880e87.1 for ; Sat, 16 Dec 2023 15:54:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702770857; x=1703375657; 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=zrVvV1utl9GFRZ2LZ5WcICVBPuOzU+o2aQHc1q2IHuo=; b=jX5E6oi0H7s1I6GmgXD5DFApKaiBNmfCpzpeKGXuJ7rwEkYo0JjRwEUd6kWMEnUbPF VFtBzL4pilvY/nykek25tDLvsvtIW2zpnNUaGp1pbZOnxX2LAp2FW2v6rv2Qm9nZRJ// D9qVHs6VccmhP7Mge/aQWfjYULg9jHL+cH9uZavxDLoN47exSlyA/B7B/Dsfsg9ePTCn F9jIT3Zwy88Wf09ruUvfQ8qbMyEkuoO9xXHlOeGuk/LvAhXbn2YE6AQ+/t7+Xz1B/bC1 BF5Wb7SOeTvFX2vT0PdMgzGzCtLoK6NcdhFGFuslmi76A88C+JOUXu5exN3izGiRbY5Y GLxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702770857; x=1703375657; 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=zrVvV1utl9GFRZ2LZ5WcICVBPuOzU+o2aQHc1q2IHuo=; b=ud3UXzpaUGL21ilWidJrzO6SRSr4Fiudilbi5zQBwWa65Qe7gocd1SHNJVXyJsByx+ ijd/QKJOXzW6Msd96sFquW1WhXEELiPz2jr3dHrT3XdXh9CpPvQNL/eweGTxdKY0nl6y EQH1joGWCmFOrYKpqT8BEtJlo7eXW1WsufzTDgoervkP+D46P4aiHCHqFKFH9jvVQs0L T3eSeUpl1zZg7SX9ZkbMgUNetC8eYtdj6Htnyoz3tRqdCGOyhc4j/iPneihcuuMWP1Tx JfhwN8sMF+u+xvrknqvuXH4ZYzc0b93aFalOsqaP3UnZw2vBpd2PyBD0158GEGWkGUlk y1gg== X-Gm-Message-State: AOJu0Yy/Ge7ofq2gbsjSLs3oXepV4UZ1Fy3OhmKA8Gnrz2Ors/Td0HiQ 4Dsi6WPNSUy/51u7sp2I1mXMWmVUoJI0IDr5t5UmCA== X-Received: by 2002:ac2:4c4f:0:b0:50e:17ea:905 with SMTP id o15-20020ac24c4f000000b0050e17ea0905mr3172436lfk.91.1702770857122; Sat, 16 Dec 2023 15:54:17 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231205024310.1593100-1-atishp@rivosinc.com> <20231205024310.1593100-5-atishp@rivosinc.com> <20231207-fidgeting-plywood-8347d9d346be@wendy> In-Reply-To: From: Atish Kumar Patra Date: Sat, 16 Dec 2023 15:54:06 -0800 Message-ID: Subject: Re: [RFC 4/9] drivers/perf: riscv: Read upper bits of a firmware counter To: Anup Patel Cc: Conor Dooley , linux-kernel@vger.kernel.org, Alexandre Ghiti , Andrew Jones , Atish Patra , Guo Ren , Icenowy Zheng , 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 Thu, Dec 14, 2023 at 4:30=E2=80=AFAM Anup Patel wr= ote: > > On Thu, Dec 7, 2023 at 6:03=E2=80=AFPM Conor Dooley wrote: > > > > On Mon, Dec 04, 2023 at 06:43:05PM -0800, Atish Patra wrote: > > > SBI v2.0 introduced a explicit function to read the upper bits > > > for any firmwar counter width that is longer than XLEN. Currently, > > > this is only applicable for RV32 where firmware counter can be > > > 64 bit. > > > > The v2.0 spec explicitly says that this function returns the upper > > 32 bits of the counter for rv32 and will always return 0 for rv64 > > or higher. The commit message here seems overly generic compared to > > the actual definition in the spec, and makes it seem like it could > > be used with a 128 bit counter on rv64 to get the upper 64 bits. > > > > I tried to think about what "generic" situation the commit message > > had been written for, but the things I came up with would all require > > changes to the spec to define behaviour for FID #5 and/or FID #1, so > > in the end I couldn't figure out the rationale behind the non-committal > > wording used here. > > The intention was to show that this can be extended in the future for other XLEN systems (obviously with spec modification). But I got your point. We can update it whenever we have such systems and the spec. Modified the commit text to match what is in the spec . > > > > > > Signed-off-by: Atish Patra > > > --- > > > drivers/perf/riscv_pmu_sbi.c | 11 +++++++++-- > > > 1 file changed, 9 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sb= i.c > > > index 40a335350d08..1c9049e6b574 100644 > > > --- a/drivers/perf/riscv_pmu_sbi.c > > > +++ b/drivers/perf/riscv_pmu_sbi.c > > > @@ -490,16 +490,23 @@ static u64 pmu_sbi_ctr_read(struct perf_event *= event) > > > struct hw_perf_event *hwc =3D &event->hw; > > > int idx =3D hwc->idx; > > > struct sbiret ret; > > > - union sbi_pmu_ctr_info info; > > > u64 val =3D 0; > > > + union sbi_pmu_ctr_info info =3D pmu_ctr_list[idx]; > > > > > > if (pmu_sbi_is_fw_event(event)) { > > > ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_R= EAD, > > > hwc->idx, 0, 0, 0, 0, 0); > > > if (!ret.error) > > > val =3D ret.value; > > > +#if defined(CONFIG_32BIT) > > > > Why is this not IS_ENABLED()? The code below uses one. You could then > > fold it into the if statement below. > > Done. > > > + if (sbi_v2_available && info.width >=3D 32) { > > > > >=3D 32? I know it is from the spec, but why does the spec define it a= s > > "One less than number of bits in CSR"? Saving bits in the structure I > > guess? > > Yes, it is for using fewer bits in counter_info. > > The maximum width of a HW counter is 64 bits. The absolute value 64 > requires 7 bits in counter_info whereas absolute value 63 requires 6 bits > in counter_info. Also, a HW counter if it exists will have at least 1 bit > implemented otherwise the HW counter does not exist. > > Regards, > Anup > > > > > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUN= TER_FW_READ_HI, > > > + hwc->idx, 0, 0, 0, 0, 0); > > > > > + if (!ret.error) > > > + val =3D val | ((u64)ret.value << 32); > > > > If the first ecall fails but the second one doesn't won't we corrupt > > val by only setting the upper bits? If returning val =3D=3D 0 is the th= ing > > to do in the error case (which it is in the existing code) should the > > first `if (!ret.error)` become `if (ret.error)` -> `return 0`? > > Sure. Fixed it. > > > > > + val =3D val | ((u64)ret.value << 32); > > > > Also, |=3D ? > > Done. > > Cheers, > > Conor. > > > > > + } > > > +#endif > > > } else { > > > - info =3D pmu_ctr_list[idx]; > > > val =3D riscv_pmu_ctr_read_csr(info.csr); > > > if (IS_ENABLED(CONFIG_32BIT)) > > > val =3D ((u64)riscv_pmu_ctr_read_csr(info.csr += 0x80)) << 31 | val; > > > -- > > > 2.34.1 > > >