From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9F3FC4167B for ; Tue, 5 Dec 2023 02:43:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346461AbjLECn2 (ORCPT ); Mon, 4 Dec 2023 21:43:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346186AbjLECn0 (ORCPT ); Mon, 4 Dec 2023 21:43:26 -0500 Received: from mail-oo1-xc2a.google.com (mail-oo1-xc2a.google.com [IPv6:2607:f8b0:4864:20::c2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC237A4 for ; Mon, 4 Dec 2023 18:43:32 -0800 (PST) Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-58cecfb4412so3525958eaf.3 for ; Mon, 04 Dec 2023 18:43:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744211; x=1702349011; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+doFZUOdGHfe3e+JV/h+u6c2KRCECetYvlqoS/1QVjk=; b=2RjRvL92eNsj8HtHxT7D/PACS/tbegyGv34eb3m75Tq7agd1G9fB20BKX5JjPW5CVS DAb4/5p40HWJAMhtLajUsKBtxq5kAnZ1i8/2R/fp3GMx3UCfQViIA95TKmPV/GnGUkUd mEcjOi4p3oSSBvTagb+1XE9rOVKy5jhnZFaL/3Pe8KSlXf9P4dISyI5x57vKDOS8Qd8t SGc9mQz1YVsxSodHQm8Pb6977tfWUCKuLH6xMwmhynKtcXR9LlWmi2M9CXkiqEiY0jk9 84WG+9kQ47TgOtwhNPRsqjP5fG29G4tC1YWK8pi9J5c0ru1NofGlJNsdhVH1plQftle5 cukQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744211; x=1702349011; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+doFZUOdGHfe3e+JV/h+u6c2KRCECetYvlqoS/1QVjk=; b=inmg4Ly4t+tI6lOpkGlA0t3MHEiENRuQhzjWQSqQEfZC1sCA3O+ZJrqdUWPXK7I8UR HVlnhRT/oITy+IUSFVXwaqSvb44JcjtuI643R2h12xEsmgpxp6djfkVgrLLs2Tf+hVtI surwnfGAco+5P6GXVvF+bYDcnXNtEt1JMS7WcYuI3Nw8cew7ZaVErSesCu2u4VtbU+6/ b1KMO6WkRvobbbTQuygrSeVcMNla25T5QKlFw2ORnSiJCfkJ4kIk2VbruKhKye3GSdt+ zqQf08E1f2xLXTXyMxhikYATiOCYjApCu9JGJFv+cpY+8duYHyHnNxu9CpwVH/a5ppKW o7lA== X-Gm-Message-State: AOJu0Yy4rV5vvdgkoF1C/Vm7pdEu83IoE/kxhn4jWcp1F6pMW5cjpycM W8Dlx4qm2Wu3QWs654ZpcxrQlKFbGhkhYkutF3Kfew== X-Google-Smtp-Source: AGHT+IESa+uIMlhGJ8omMCACn4oXC680NqScC0tWffrPykeRZIe5TFUQYcAdM2FafioZUI/dBXwO1w== X-Received: by 2002:a05:6820:2a09:b0:58e:1c48:1ee0 with SMTP id dr9-20020a0568202a0900b0058e1c481ee0mr3283907oob.18.1701744211227; Mon, 04 Dec 2023 18:43:31 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:30 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 1/9] RISC-V: Fix the typo in Scountovf CSR name Date: Mon, 4 Dec 2023 18:43:02 -0800 Message-Id: <20231205024310.1593100-2-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The counter overflow CSR name is "scountovf" not "sscountovf". Fix the csr name. Fixes: 4905ec2fb7e6 ("RISC-V: Add sscofpmf extension support") Signed-off-by: Atish Patra Reviewed-by: Anup Patel Reviewed-by: Conor Dooley --- arch/riscv/include/asm/csr.h | 2 +- arch/riscv/include/asm/errata_list.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 306a19a5509c..88cdc8a3e654 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -281,7 +281,7 @@ #define CSR_HPMCOUNTER30H 0xc9e #define CSR_HPMCOUNTER31H 0xc9f =20 -#define CSR_SSCOUNTOVF 0xda0 +#define CSR_SCOUNTOVF 0xda0 =20 #define CSR_SSTATUS 0x100 #define CSR_SIE 0x104 diff --git a/arch/riscv/include/asm/errata_list.h b/arch/riscv/include/asm/= errata_list.h index 83ed25e43553..7026fba12eeb 100644 --- a/arch/riscv/include/asm/errata_list.h +++ b/arch/riscv/include/asm/errata_list.h @@ -152,7 +152,7 @@ asm volatile(ALTERNATIVE_2( \ =20 #define ALT_SBI_PMU_OVERFLOW(__ovl) \ asm volatile(ALTERNATIVE( \ - "csrr %0, " __stringify(CSR_SSCOUNTOVF), \ + "csrr %0, " __stringify(CSR_SCOUNTOVF), \ "csrr %0, " __stringify(THEAD_C9XX_CSR_SCOUNTEROF), \ THEAD_VENDOR_ID, ERRATA_THEAD_PMU, \ CONFIG_ERRATA_THEAD_PMU) \ --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC395C10DC1 for ; Tue, 5 Dec 2023 02:43:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346655AbjLECna (ORCPT ); Mon, 4 Dec 2023 21:43:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346337AbjLECn2 (ORCPT ); Mon, 4 Dec 2023 21:43:28 -0500 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 870DECD for ; Mon, 4 Dec 2023 18:43:34 -0800 (PST) Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6d87a83ec27so1843951a34.2 for ; Mon, 04 Dec 2023 18:43:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744213; x=1702349013; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ESe2+H6xRcIvQHZC4sCC1Ujc/nu3bNcuHE36+amdpNs=; b=Y+ciD/tNCJQ8NcC2aGaOUHpwaeXN8Bf4qOEpdx8qdaMxXj+XhQ0CJGVcbaW2iopBha EWPptcxKOXaAIF44bTipFnQAzSBJax29OwbCbYsdm96LVzlQqU34IvNM2H5mDmVLqJW9 ctBByOzacU7BTiAoLrbLg0SDJIktlQUEnFSX4nvhA/HGoXe2XF2M0rbLeYyqZg/Gvuht AjCdMW3YlZt29/NrIT3WYz6bIbU/ERucE2PS0Qp6SNUk0hGWCXp2WGOwN6EO1aOGAgVm S1T1NEUI/PzvYqnC0uBE6LbmffXt8h5AUhB/S1hoJNQPP3xlWCHjN1+RyJ8JL/bDnxTW e3ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744213; x=1702349013; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ESe2+H6xRcIvQHZC4sCC1Ujc/nu3bNcuHE36+amdpNs=; b=Y8UMd3RLuQ6OYkGgT8X2uIS6XFEt+QUGz6Vs1h0+q7WyuYa1b1Cy7oMob6hWaXjbBd J721jhi7HOR9e5CjFR76OcHIvXDtxF2625mxp2Y1gy3FnXTFLjX5ufDsZ6bCDwetse3k Ey54usXnl/EzagBmO+4o0CfAk4+Jh/KznNdFwjYcsOjbkdTcTJsObxzMF7njxvTYcwqS mzLW5O/hpwqh994dYt+U3q3leCsCgyyLd9zCCxcDlPm9GtxQvWTBgwWofeQ9bGSSIGbT MYS7id8ndSj0312B/4AXt4o3dZM6pRmDqYW2ucMOMm9DQw2YWKKfy9z9T6JT64YOlWZQ nMCw== X-Gm-Message-State: AOJu0YxSLW9bz/v7RfSTRlHIoXHLbw/BZYm4zAefD2eNuqjoGN+fjl5S XWMRl9flzS3AZJNFy87PGPfMvRfvX2UsbeiE2usatw== X-Google-Smtp-Source: AGHT+IGEgSVldCBVjabdNWyXSrgKexmOSpdOUxfqb+i3/FDPH6nx8GslbA4iHobHadY6ni/KUBPIug== X-Received: by 2002:a9d:748b:0:b0:6d9:9ef8:84b7 with SMTP id t11-20020a9d748b000000b006d99ef884b7mr2852315otk.26.1701744213065; Mon, 04 Dec 2023 18:43:33 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:32 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 2/9] drivers/perf: riscv: Add a flag to indicate SBI v2.0 support Date: Mon, 4 Dec 2023 18:43:03 -0800 Message-Id: <20231205024310.1593100-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" SBI v2.0 added few functions to improve SBI PMU extension. In order to be backward compatible, the driver must use these functions only if SBI v2.0 is available. Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_sbi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 16acd4dcdb96..40a335350d08 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -35,6 +35,8 @@ PMU_FORMAT_ATTR(event, "config:0-47"); PMU_FORMAT_ATTR(firmware, "config:63"); =20 +static bool sbi_v2_available; + static struct attribute *riscv_arch_formats_attr[] =3D { &format_attr_event.attr, &format_attr_firmware.attr, @@ -1108,6 +1110,9 @@ static int __init pmu_sbi_devinit(void) return 0; } =20 + if (sbi_spec_version >=3D sbi_mk_version(2, 0)) + sbi_v2_available =3D true; + ret =3D cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING, "perf/riscv/pmu:starting", pmu_sbi_starting_cpu, pmu_sbi_dying_cpu); --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72937C10DC1 for ; Tue, 5 Dec 2023 02:43:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346673AbjLECnf (ORCPT ); Mon, 4 Dec 2023 21:43:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346662AbjLECna (ORCPT ); Mon, 4 Dec 2023 21:43:30 -0500 Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 942E0A4 for ; Mon, 4 Dec 2023 18:43:36 -0800 (PST) Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6d855efb920so2943868a34.1 for ; Mon, 04 Dec 2023 18:43:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744215; x=1702349015; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0t/9Xz5DiK/wXH2NqGUZGNOtmRM2KXduxsMbe0LSN5g=; b=rhY+hiRgeh/1Mf/In9gQYD8QCUPWj5f5fJw3PNkcHgOKcvUSSRsski2xZ28ICISpcx tSBr1mmxDtP2iTQ92wjrdaEQw7EYLSroqUbJfeIbPC8U4WX/7rfj7LgBZHuJ2EmAXTfe Ioa2oMUtrGS9yF9R8HurzNp7K1GL0LkHEtvscFgS+EZ8GM4f00uN3BEPQL0eJObse9Ch GxYlBmWIhUDPSDDhN9M/fgixu5pemWX4hSJ9HNTQ9A6yqO1pZaRhcTWLfHcCX1vkbulc OhM8JkU9H5BDp49zqmaTxbFYy/9Q48uwsu+/Fleh7D33rsyC+fBHdiQH7xwBKwS0YM4f SvEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744215; x=1702349015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0t/9Xz5DiK/wXH2NqGUZGNOtmRM2KXduxsMbe0LSN5g=; b=uh3kNHO8qQL+EiDKGnaR8P03F/jNjwFAch/im7gSelHqdiZfruWtDRQqk0vGi9Oqpw iANoAtAN/yO9ocbL9oeaPCQplHK1Ze28qR/063O12kcXO/h3d5C0jdCJEMOqJKm84blh S9kgdB+HkPQcujyXindnG3v7o2uj9kfBT+x18cEIYCYpSslfH3DDjoGyrBqF8jahls4X LAPCvOgkiFEAY7x7wkAiG3UBygugsP7a5BE6qZ1a4dmvHIF8r6PvKJmTrjDYMcC09D0D ed4kd9LDxWKbtnrCTzQJuHYr54yudza+6BEjoxV85lxgCg+ATf/6SQjteqL3YP/to6lK Re9w== X-Gm-Message-State: AOJu0YyZKqZVkTd8w754/FicWKdJf4j50eYGEzztzPItZKzY+l3kHKqO nR1GppOsw7W79l4nR/kOqxhkmCOpqEbXZ0/YPAx/qg== X-Google-Smtp-Source: AGHT+IF/knJEBDAyziZ5bGsFkd0zgoM9yPDKanU8wPN8v+9Xu+OCLihiseLcaJtMfQMkMK0rGT1qkg== X-Received: by 2002:a05:6830:100f:b0:6d8:a456:d99d with SMTP id a15-20020a056830100f00b006d8a456d99dmr3450114otp.5.1701744215637; Mon, 04 Dec 2023 18:43:35 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:35 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 3/9] RISC-V: Add FIRMWARE_READ_HI definition Date: Mon, 4 Dec 2023 18:43:04 -0800 Message-Id: <20231205024310.1593100-4-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" SBI v2.0 added another function to SBI PMU extension to read the upper bits of a counter with width larger than XLEN. Add the definition for that function. Signed-off-by: Atish Patra Acked-by: Conor Dooley Reviewed-by: Anup Patel --- arch/riscv/include/asm/sbi.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 0892f4421bc4..f3eeca79a02d 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -121,6 +121,7 @@ enum sbi_ext_pmu_fid { SBI_EXT_PMU_COUNTER_START, SBI_EXT_PMU_COUNTER_STOP, SBI_EXT_PMU_COUNTER_FW_READ, + SBI_EXT_PMU_COUNTER_FW_READ_HI, }; =20 union sbi_pmu_ctr_info { --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6B07C10DCE for ; Tue, 5 Dec 2023 02:43:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346755AbjLECni (ORCPT ); Mon, 4 Dec 2023 21:43:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346687AbjLECnd (ORCPT ); Mon, 4 Dec 2023 21:43:33 -0500 Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2550A109 for ; Mon, 4 Dec 2023 18:43:39 -0800 (PST) Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6d84ddd642fso2807758a34.0 for ; Mon, 04 Dec 2023 18:43:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744217; x=1702349017; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ezsx5Eqv7C1IIJcHeXVdQzYdP4PWuNbr/jfR9i9uc+E=; b=FKH1Gs8B1mqIu9eyoVmvKmmrtqbDrYKlv0vtgXtIxQJmNTeb5piflB3M9hRwo3r1JC f51kFxYpLx505XbsNejjSN353ZLlwQ5IMt4KqnW88yHFpPFGjYD9q5JhwfJyW9LZAmvK 6MSHbLFY5h4lk8taYY7dpw9CIDzsnMSKAMzKUJELD8k6JBkxkFffrCcruEh6kSlV4/1c Q3fxN3Gxm8XhgmVm3MFbDqqYWGsSdXZLH5/1aRmE8aHqhW2eiCmS0FfOfG+HC0hmkgB6 r37cx5zgfhxlDkX9137V+0E4O+NIqXJpFmWBAkamHiYP6oFAEZpZ25ezg16xC7ADMznH xKSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744217; x=1702349017; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ezsx5Eqv7C1IIJcHeXVdQzYdP4PWuNbr/jfR9i9uc+E=; b=j0afK9KcjQh9CqA7FQxk2iwoCwXbgy2CrDPPZtFBS4aLEDGNXbCqqM/T2oVFPSJfyn A0KDOtrnSHTWLOXijlr4ZoQDDaFgERWGNOa4qBTlGBJ0rCNk5sF0nRoo3UJXgUZ/HFVQ Wxhvb2/lr8B2sWFO+0UE2qt4zrGDd/Ljll3MfIRXXryRFc6fjct7oSiUhDwUUgbRYy/j tOkBSsS36f6ZE0uUf3+C6g8ulJJr4bgxM5VR10ML7cjV+s8Y8L3mxR2B8lD0w3dg7Js+ E1XQV6hFcw1WvXQIeDZ94pDwHAP8oUimboPzDaGJoHgJLskADm2zWcVcdcMkGu3/If6L 23uQ== X-Gm-Message-State: AOJu0YwqUHVX+EP3Y6Y0nArPpfhFRbiuPqcSY2PHKHxEJIDFKmWa3pXP O7h5QcRQKujeKl+nCkaQPzMsroC2iChqA74cqMHB8Q== X-Google-Smtp-Source: AGHT+IF8HbmBfGcwhzrY8QgNBoAV0D9dftfiztmlrc9uBoUNExR6eq192ybD6wyNke0oHa4GT14FGA== X-Received: by 2002:a9d:6398:0:b0:6d9:a17c:237 with SMTP id w24-20020a9d6398000000b006d9a17c0237mr2375646otk.23.1701744217652; Mon, 04 Dec 2023 18:43:37 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:37 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 4/9] drivers/perf: riscv: Read upper bits of a firmware counter Date: Mon, 4 Dec 2023 18:43:05 -0800 Message-Id: <20231205024310.1593100-5-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. 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_sbi.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]; =20 if (pmu_sbi_is_fw_event(event)) { ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ, hwc->idx, 0, 0, 0, 0, 0); if (!ret.error) val =3D ret.value; +#if defined(CONFIG_32BIT) + if (sbi_v2_available && info.width >=3D 32) { + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ_HI, + hwc->idx, 0, 0, 0, 0, 0); + if (!ret.error) + val =3D val | ((u64)ret.value << 32); + } +#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; --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0788C10DC1 for ; Tue, 5 Dec 2023 02:43:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346819AbjLECno (ORCPT ); Mon, 4 Dec 2023 21:43:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346724AbjLECnf (ORCPT ); Mon, 4 Dec 2023 21:43:35 -0500 Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E27ED199 for ; Mon, 4 Dec 2023 18:43:40 -0800 (PST) Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6d8750718adso2404755a34.0 for ; Mon, 04 Dec 2023 18:43:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744220; x=1702349020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ARpkTD0DGKCutL1TwjMjDMoOjcqYmkFa31lyR5g4T2Q=; b=KOMlUAwvldFKgdnWVLDmJTfUo6zxkGezTTyO5DgVzigxj+xvrmKAC+oVLWVemfsfUA F5nHpRor4TACjK5jJy6oVHWLsRnM1veLLA0pTS4cbL4jcLcpn0jYRCLF78cpnReK07Z0 tXP1FKMdOW445sZnx7co1epZcmc7pPcl1WWRDcvgIaa7sFRglcR3LsI1LL9j1FpnosP2 WCbYg0gE24RA1MVkxwNI+iV/C3Rc+SOU8ez5dnWQBk+NYgiDuJwq5HX8Em4/7APsAKAD L1o/ESgxZyC6rKs02dT7GjK1oHgr5X2rDDqlSlIZXfA9YftFZ4DMD6GxGylB7vr9nwm0 5TRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744220; x=1702349020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ARpkTD0DGKCutL1TwjMjDMoOjcqYmkFa31lyR5g4T2Q=; b=Bu5dLdL6/WaJXURFtvj3ZsUYRkqf9+xH2QktvAU6SKOoXuhiyWCITQ4yk1dF/vzCSD L6/gUlDLI9fOkEkl/OJLmVfKHaFLC7E+jW8rQ5UlBPBSqbOl3M2PXP97Bq+IQCY4SrkD bS/+9hLhPa5qqkbqzqT0UboKlqNwd2+Yg6QMDvFFRlL6eG52EYh7aL+yQl0+GW+gJSPO p74b0xHZu2pl8iX9x8FKmxA0ILRA5nk7dnh5p+nFcfPoXqCDJw2mxVx3NLZ6ddqnh2EU QVglHD3DK4mWVrKx2GP4VbeTSJ7+pofELXOAgJyRkzcZp9OIjmvOoDWfnkXT+KfoSRt3 uHVA== X-Gm-Message-State: AOJu0YxmQqTfVTliUUFR3u5CKmTlLazVT6qkVlfUcVlozOkBW2x7kF9L CYF24Ja37DK5tWGDqk1nVSgUFafaiq2VDR3cu1D6TA== X-Google-Smtp-Source: AGHT+IFXeXXz41Hsj4C/GCefAovxqAXWbbpNAzmIuIuJp4iWAyn+2eqWNtIeCNa4tWlwO/mFe0g5LA== X-Received: by 2002:a05:6830:61c7:b0:6d8:74e2:7cdc with SMTP id cc7-20020a05683061c700b006d874e27cdcmr7785693otb.55.1701744219906; Mon, 04 Dec 2023 18:43:39 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:39 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 5/9] RISC-V: Add SBI PMU snapshot definitions Date: Mon, 4 Dec 2023 18:43:06 -0800 Message-Id: <20231205024310.1593100-6-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" SBI PMU Snapshot function optimizes the number of traps to higher privilege mode by leveraging a shared memory between the S/VS-mode and the M/HS mode. Add the definitions for that extension Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/include/asm/sbi.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index f3eeca79a02d..29821addb9b7 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -122,6 +122,7 @@ enum sbi_ext_pmu_fid { SBI_EXT_PMU_COUNTER_STOP, SBI_EXT_PMU_COUNTER_FW_READ, SBI_EXT_PMU_COUNTER_FW_READ_HI, + SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, }; =20 union sbi_pmu_ctr_info { @@ -138,6 +139,13 @@ union sbi_pmu_ctr_info { }; }; =20 +/* Data structure to contain the pmu snapshot data */ +struct riscv_pmu_snapshot_data { + uint64_t ctr_overflow_mask; + uint64_t ctr_values[64]; + uint64_t reserved[447]; +}; + #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0) #define RISCV_PMU_RAW_EVENT_IDX 0x20000 =20 @@ -234,9 +242,11 @@ enum sbi_pmu_ctr_type { =20 /* Flags defined for counter start function */ #define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0) +#define SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT (1 << 1) =20 /* Flags defined for counter stop function */ #define SBI_PMU_STOP_FLAG_RESET (1 << 0) +#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT (1 << 1) =20 enum sbi_ext_dbcn_fid { SBI_EXT_DBCN_CONSOLE_WRITE =3D 0, --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0527C10DC1 for ; Tue, 5 Dec 2023 02:43:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376588AbjLECnu (ORCPT ); Mon, 4 Dec 2023 21:43:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346702AbjLECnl (ORCPT ); Mon, 4 Dec 2023 21:43:41 -0500 Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8D791B3 for ; Mon, 4 Dec 2023 18:43:43 -0800 (PST) Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6d858670630so2794848a34.0 for ; Mon, 04 Dec 2023 18:43:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744222; x=1702349022; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FzrxBS9zzAN2X80OdUne9aaJh676n2ABvleQ+If87nA=; b=itTlfs55ewsfiw60opboyQCP3Re7bFdTKIDdtboH0Am5YaoZzDKSoL6G3e/g3ekK4g Sq9SxAhnxRisYwvRTmWKJIhRYpFVzTBnvblMQxubUgLOyQSwVCQNr7xrksHNU4Xx3PPk sDqgFRFj/QQTqji1br4ZBEzXr0wIpcLLdg6d81SmVPsn2nCsmdVPrmwFsgYaoMvReAk+ QrC8/L88ls3f2i8ZjhIOl4NyqSTn4NKKJTtpZrQEfmJHgCie8pblB6GodwW4fptPRWcL kgbrUOHNuZJPp7t7spdniGy0td2j2hovOAf44CTWWaSUKF/sFpd9yGoOhpzPb9EBb4RI tNuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744222; x=1702349022; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FzrxBS9zzAN2X80OdUne9aaJh676n2ABvleQ+If87nA=; b=Bcz4GS63RrisXTwcz/YF8UP7oojZO7WIlfOde8WEPrd1EJDvLv9juOjxaiAC114Afq sDY5FR17bah5H2EvzWKJwispRTngRdG3HUKMT8Bz9luXcJAap+C5vNdt5dHDiDR7oEGo 09FrxbGdlSU437if8SSDKqL/DxF+wR2FTXIvHpquo2lq4unUo6S8n8zLHqs97qx60wUb VuNxrO+02eFF407tZ2SaXYBj24iWG5RA2UIZEC8aADekmYCjNVpCLHRC1ICOcmdhv2bz Ui0z0ESzbiI9488nBzOpwym9fhnI0mYbXO3EJr1QCy1eYza7WC7iP4DAJvroD7rN2+K9 mW6g== X-Gm-Message-State: AOJu0YwG7UkPUwM+LLgogeSJF/9zPCjOU6K2cyHHaezn+CM/QSuzQqOJ 8cH93rnFiWEIXk4FTscXYCv36yefppRyMHm10y4hfg== X-Google-Smtp-Source: AGHT+IFry2dKeLDD8omgbo3Lus3pH/5pMUu5z/RJqz64Q0fTcqyZNbGo6MKRIzEbt6ZoO59xkd3s1A== X-Received: by 2002:a05:6830:3b84:b0:6d8:8077:8017 with SMTP id dm4-20020a0568303b8400b006d880778017mr4639035otb.4.1701744222082; Mon, 04 Dec 2023 18:43:42 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:41 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 6/9] drivers/perf: riscv: Implement SBI PMU snapshot function Date: Mon, 4 Dec 2023 18:43:07 -0800 Message-Id: <20231205024310.1593100-7-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" SBI v2.0 SBI introduced PMU snapshot feature which adds the following features. 1. Read counter values directly from the shared memory instead of csr read. 2. Start multiple counters with initial values with one SBI call. These functionalities optimizes the number of traps to the higher privilege mode. If the kernel is in VS mode while the hypervisor deploy trap & emulate method, this would minimize all the hpmcounter CSR read traps. If the kernel is running in S-mode, the benfits reduced to CSR latency vs DRAM/cache latency as there is no trap involved while accessing the hpmcounter CSRs. In both modes, it does saves the number of ecalls while starting multiple counter together with an initial values. This is a likely scenario if multiple counters overflow at the same time. Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu.c | 1 + drivers/perf/riscv_pmu_sbi.c | 203 ++++++++++++++++++++++++++++++--- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 197 insertions(+), 13 deletions(-) diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c index 0dda70e1ef90..5b57acb770d3 100644 --- a/drivers/perf/riscv_pmu.c +++ b/drivers/perf/riscv_pmu.c @@ -412,6 +412,7 @@ struct riscv_pmu *riscv_pmu_alloc(void) cpuc->n_events =3D 0; for (i =3D 0; i < RISCV_MAX_COUNTERS; i++) cpuc->events[i] =3D NULL; + cpuc->snapshot_addr =3D NULL; } pmu->pmu =3D (struct pmu) { .event_init =3D riscv_pmu_event_init, diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 1c9049e6b574..1b8b6de63b69 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -36,6 +36,9 @@ PMU_FORMAT_ATTR(event, "config:0-47"); PMU_FORMAT_ATTR(firmware, "config:63"); =20 static bool sbi_v2_available; +static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available); +#define sbi_pmu_snapshot_available() \ + static_branch_unlikely(&sbi_pmu_snapshot_available) =20 static struct attribute *riscv_arch_formats_attr[] =3D { &format_attr_event.attr, @@ -485,14 +488,101 @@ static int pmu_sbi_event_map(struct perf_event *even= t, u64 *econfig) return ret; } =20 +static void pmu_sbi_snapshot_free(struct riscv_pmu *pmu) +{ + int cpu; + struct cpu_hw_events *cpu_hw_evt; + + for_each_possible_cpu(cpu) { + cpu_hw_evt =3D per_cpu_ptr(pmu->hw_events, cpu); + if (!cpu_hw_evt->snapshot_addr) + continue; + free_page((unsigned long)cpu_hw_evt->snapshot_addr); + cpu_hw_evt->snapshot_addr =3D NULL; + cpu_hw_evt->snapshot_addr_phys =3D 0; + } +} + +static int pmu_sbi_snapshot_alloc(struct riscv_pmu *pmu) +{ + int cpu; + struct page *snapshot_page; + struct cpu_hw_events *cpu_hw_evt; + + for_each_possible_cpu(cpu) { + cpu_hw_evt =3D per_cpu_ptr(pmu->hw_events, cpu); + if (cpu_hw_evt->snapshot_addr) + continue; + snapshot_page =3D alloc_page(GFP_ATOMIC | __GFP_ZERO); + if (!snapshot_page) { + pmu_sbi_snapshot_free(pmu); + return -ENOMEM; + } + cpu_hw_evt->snapshot_addr =3D page_to_virt(snapshot_page); + cpu_hw_evt->snapshot_addr_phys =3D page_to_phys(snapshot_page); + } + + return 0; +} + +static void pmu_sbi_snapshot_disable(void) +{ + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, -1, + -1, 0, 0, 0, 0); +} + +static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu) +{ + struct cpu_hw_events *cpu_hw_evt; + struct sbiret ret =3D {0}; + int rc; + + cpu_hw_evt =3D per_cpu_ptr(pmu->hw_events, cpu); + if (!cpu_hw_evt->snapshot_addr_phys) + return -EINVAL; + + if (cpu_hw_evt->snapshot_set_done) + return 0; + +#if defined(CONFIG_32BIT) + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, cpu_hw_evt= ->snapshot_addr_phys, + (u64)(cpu_hw_evt->snapshot_addr_phys) >> 32, 0, 0, 0, 0); +#else + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, cpu_hw_evt= ->snapshot_addr_phys, + 0, 0, 0, 0, 0); +#endif + /* Free up the snapshot area memory and fall back to default SBI */ + if (ret.error) { + if (ret.error !=3D SBI_ERR_NOT_SUPPORTED) + pr_warn("%s: pmu snapshot setup failed with error %ld\n", __func__, + ret.error); + rc =3D sbi_err_map_linux_errno(ret.error); + if (rc) + return rc; + } + + cpu_hw_evt->snapshot_set_done =3D true; + + return 0; +} + 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; u64 val =3D 0; + struct riscv_pmu *pmu =3D to_riscv_pmu(event->pmu); + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); + struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_addr; union sbi_pmu_ctr_info info =3D pmu_ctr_list[idx]; =20 + /* Read the value from the shared memory directly */ + if (sbi_pmu_snapshot_available()) { + val =3D sdata->ctr_values[idx]; + goto done; + } + if (pmu_sbi_is_fw_event(event)) { ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ, hwc->idx, 0, 0, 0, 0, 0); @@ -512,6 +602,7 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event) val =3D ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 31 | val; } =20 +done: return val; } =20 @@ -539,6 +630,7 @@ static void pmu_sbi_ctr_start(struct perf_event *event,= u64 ival) struct hw_perf_event *hwc =3D &event->hw; unsigned long flag =3D SBI_PMU_START_FLAG_SET_INIT_VALUE; =20 + /* There is no benefit setting SNAPSHOT FLAG for a single counter */ #if defined(CONFIG_32BIT) ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx, 1, flag, ival, ival >> 32, 0); @@ -559,16 +651,29 @@ static void pmu_sbi_ctr_stop(struct perf_event *event= , unsigned long flag) { struct sbiret ret; struct hw_perf_event *hwc =3D &event->hw; + struct riscv_pmu *pmu =3D to_riscv_pmu(event->pmu); + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); + struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_addr; =20 if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) && (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT)) pmu_sbi_reset_scounteren((void *)event); =20 + if (sbi_pmu_snapshot_available()) + flag |=3D SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, fla= g, 0, 0, 0); - if (ret.error && (ret.error !=3D SBI_ERR_ALREADY_STOPPED) && - flag !=3D SBI_PMU_STOP_FLAG_RESET) + if (!ret.error && sbi_pmu_snapshot_available()) { + /* Snapshot is taken relative to the counter idx base. Apply a fixup. */ + if (hwc->idx > 0) { + sdata->ctr_values[hwc->idx] =3D sdata->ctr_values[0]; + sdata->ctr_values[0] =3D 0; + } + } else if (ret.error && (ret.error !=3D SBI_ERR_ALREADY_STOPPED) && + flag !=3D SBI_PMU_STOP_FLAG_RESET) { pr_err("Stopping counter idx %d failed with error %d\n", hwc->idx, sbi_err_map_linux_errno(ret.error)); + } } =20 static int pmu_sbi_find_num_ctrs(void) @@ -626,10 +731,14 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu = *pmu) static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) { struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); + unsigned long flag =3D 0; + + if (sbi_pmu_snapshot_available()) + flag =3D SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; =20 /* No need to check the error here as we can't do anything about the erro= r */ sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, 0, - cpu_hw_evt->used_hw_ctrs[0], 0, 0, 0, 0); + cpu_hw_evt->used_hw_ctrs[0], flag, 0, 0, 0); } =20 /* @@ -638,11 +747,10 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_= pmu *pmu) * while the overflowed counters need to be started with updated initializ= ation * value. */ -static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, - unsigned long ctr_ovf_mask) +static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_= hw_evt, + unsigned long ctr_ovf_mask) { int idx =3D 0; - struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); struct perf_event *event; unsigned long flag =3D SBI_PMU_START_FLAG_SET_INIT_VALUE; unsigned long ctr_start_mask =3D 0; @@ -677,6 +785,49 @@ static inline void pmu_sbi_start_overflow_mask(struct = riscv_pmu *pmu, } } =20 +static noinline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events = *cpu_hw_evt, + unsigned long ctr_ovf_mask) +{ + int idx =3D 0; + struct perf_event *event; + unsigned long flag =3D SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT; + uint64_t max_period; + struct hw_perf_event *hwc; + u64 init_val =3D 0; + unsigned long ctr_start_mask =3D 0; + struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_addr; + + for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) { + if (ctr_ovf_mask & (1 << idx)) { + event =3D cpu_hw_evt->events[idx]; + hwc =3D &event->hw; + max_period =3D riscv_pmu_ctr_get_width_mask(event); + init_val =3D local64_read(&hwc->prev_count) & max_period; + sdata->ctr_values[idx] =3D init_val; + } + /* We donot need to update the non-overflow counters the previous + * value should have been there already. + */ + } + + ctr_start_mask =3D cpu_hw_evt->used_hw_ctrs[0]; + + /* Start all the counters in a single shot */ + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, 0, ctr_start_mask, + flag, 0, 0, 0); +} + +static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, + unsigned long ctr_ovf_mask) +{ + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); + + if (sbi_pmu_snapshot_available()) + pmu_sbi_start_ovf_ctrs_snapshot(cpu_hw_evt, ctr_ovf_mask); + else + pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask); +} + static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) { struct perf_sample_data data; @@ -690,6 +841,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *d= ev) unsigned long overflowed_ctrs =3D 0; struct cpu_hw_events *cpu_hw_evt =3D dev; u64 start_clock =3D sched_clock(); + struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_addr; =20 if (WARN_ON_ONCE(!cpu_hw_evt)) return IRQ_NONE; @@ -711,8 +863,10 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *= dev) pmu_sbi_stop_hw_ctrs(pmu); =20 /* Overflow status register should only be read after counter are stopped= */ - ALT_SBI_PMU_OVERFLOW(overflow); - + if (sbi_pmu_snapshot_available()) + overflow =3D sdata->ctr_overflow_mask; + else + ALT_SBI_PMU_OVERFLOW(overflow); /* * Overflow interrupt pending bit should only be cleared after stopping * all the counters to avoid any race condition. @@ -774,6 +928,7 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struc= t hlist_node *node) { struct riscv_pmu *pmu =3D hlist_entry_safe(node, struct riscv_pmu, node); struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); + int ret =3D 0; =20 /* * We keep enabling userspace access to CYCLE, TIME and INSTRET via the @@ -794,7 +949,10 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, stru= ct hlist_node *node) enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE); } =20 - return 0; + if (sbi_pmu_snapshot_available()) + ret =3D pmu_sbi_snapshot_setup(pmu, cpu); + + return ret; } =20 static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node) @@ -807,6 +965,9 @@ static int pmu_sbi_dying_cpu(unsigned int cpu, struct h= list_node *node) /* Disable all counters access for user mode now */ csr_write(CSR_SCOUNTEREN, 0x0); =20 + if (sbi_pmu_snapshot_available()) + pmu_sbi_snapshot_disable(); + return 0; } =20 @@ -1076,10 +1237,6 @@ static int pmu_sbi_device_probe(struct platform_devi= ce *pdev) pmu->event_unmapped =3D pmu_sbi_event_unmapped; pmu->csr_index =3D pmu_sbi_csr_index; =20 - ret =3D cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node= ); - if (ret) - return ret; - ret =3D riscv_pm_pmu_register(pmu); if (ret) goto out_unregister; @@ -1088,8 +1245,28 @@ static int pmu_sbi_device_probe(struct platform_devi= ce *pdev) if (ret) goto out_unregister; =20 + /* SBI PMU Snasphot is only available in SBI v2.0 */ + if (sbi_v2_available) { + ret =3D pmu_sbi_snapshot_alloc(pmu); + if (ret) + goto out_unregister; + ret =3D pmu_sbi_snapshot_setup(pmu, smp_processor_id()); + if (!ret) { + pr_info("SBI PMU snapshot is available to optimize the PMU traps\n"); + /* We enable it once here for the boot cpu. If snapshot shmem fails dur= ing + * cpu hotplug on, it should bail out. + */ + static_branch_enable(&sbi_pmu_snapshot_available); + } + /* Snapshot is an optional feature. Continue if not available */ + } + register_sysctl("kernel", sbi_pmu_sysctl_table); =20 + ret =3D cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node= ); + if (ret) + return ret; + return 0; =20 out_unregister: diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h index 43282e22ebe1..c3fa90970042 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -39,6 +39,12 @@ struct cpu_hw_events { DECLARE_BITMAP(used_hw_ctrs, RISCV_MAX_COUNTERS); /* currently enabled firmware counters */ DECLARE_BITMAP(used_fw_ctrs, RISCV_MAX_COUNTERS); + /* The virtual address of the shared memory where counter snapshot will b= e taken */ + void *snapshot_addr; + /* The physical address of the shared memory where counter snapshot will = be taken */ + phys_addr_t snapshot_addr_phys; + /* Boolean flag to indicate setup is already done */ + bool snapshot_set_done; }; =20 struct riscv_pmu { --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1049EC4167B for ; Tue, 5 Dec 2023 02:44:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376741AbjLECn4 (ORCPT ); Mon, 4 Dec 2023 21:43:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346680AbjLECnm (ORCPT ); Mon, 4 Dec 2023 21:43:42 -0500 Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99023D4E for ; Mon, 4 Dec 2023 18:43:45 -0800 (PST) Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6d99bc65219so1348622a34.3 for ; Mon, 04 Dec 2023 18:43:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744224; x=1702349024; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lzz5g7ES5nKjgwbjahh46Km+H+w1xHmv8nQyMYp7oKE=; b=0mCh4PiVmWf/ZblXlt5Jjpb+P4H16ONZOhWp8rT4jI9wRmrPCZM1iwogbxLRVXfSNq 3xEpIhLigoTSjt91z6gVdn16kUpOpx97YrCB955BeUNk4PbBwRx6U+kJK1q0eqTyaBk9 bbW8wp+ubUDsHVoopRXDGkS1DZfUaFu6PfR/5kMOseMogZVXfAQkyRvY1btSy/6/nuax +ac2QKVAx7vqRj4sUJ63ydl29/ossHsP8sNv7IqQgPOfY/UfFKlJOgctj/NsaCqpDfBt MANH1beJ3Q4GspJexV4YV+YvcEDJc7hY2+RFReVXwgM1AdyI9x9P4wu1K7nreqZee5b/ gCCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744224; x=1702349024; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lzz5g7ES5nKjgwbjahh46Km+H+w1xHmv8nQyMYp7oKE=; b=KFexUtzmHamVnliKGekRqrCZSDdRUtA0AZ6jDLOBhY5WPDl3mAA55xA/XOluxhd0FP NIo3nTY39ouVola1xt7PJis4sz3nZY0TsIFyufirLDXVHQ/MTgxJP41AUPHXMyrC2CIQ sIHlIoXc24f+/ByiBsGszd2QIYg1SxqKWL7Lhnch2utq3t9f6RSARdaC4HZdHTMfZNis WVU9t7li3kCZURvZcvoKriv+MmedWChg5m2TOefsVaXNvrWHmGLldI047YeXxNh1ru8E zV4Cq48sPkDGzNY2UMLrL9BtESTOJs7pNuRvdx+lhYgaKzrQKRasctHKh0weXUqCWMRS TOig== X-Gm-Message-State: AOJu0YykPTfQtvrnFYNWia4TfOLypErc6O0w6KJ57NqMKJ3YLU9rw6Bl Zra2In0FJt6qkP8ef7Nm/k27CjWXzR/aoVyL9nY1YA== X-Google-Smtp-Source: AGHT+IHAmPQMffRLEjxogkRBgQxGjgGKbV3BI9MZCuyp69gtTWruQBhsw7cE61+U8EFJPv8+v2izAg== X-Received: by 2002:a05:6830:7185:b0:6d8:1c4b:6c0e with SMTP id el5-20020a056830718500b006d81c4b6c0emr7813977otb.9.1701744223937; Mon, 04 Dec 2023 18:43:43 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:43 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 7/9] RISC-V: KVM: Implement SBI PMU Snapshot feature Date: Mon, 4 Dec 2023 18:43:08 -0800 Message-Id: <20231205024310.1593100-8-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" PMU Snapshot function allows to minimize the number of traps when the guest access configures/access the hpmcounters. If the snapshot feature is enabled, the hypervisor updates the shared memory with counter data and state of overflown counters. The guest can just read the shared memory instead of trap & emulate done by the hypervisor. This patch doesn't implement the counter overflow yet. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 10 ++ arch/riscv/kvm/vcpu_pmu.c | 129 ++++++++++++++++++++++++-- arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + 3 files changed, 134 insertions(+), 8 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm= /kvm_vcpu_pmu.h index 395518a1664e..64c75acad6ba 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -36,6 +36,7 @@ struct kvm_pmc { bool started; /* Monitoring event ID */ unsigned long event_idx; + struct kvm_vcpu *vcpu; }; =20 /* PMU data structure per vcpu */ @@ -50,6 +51,12 @@ struct kvm_pmu { bool init_done; /* Bit map of all the virtual counter used */ DECLARE_BITMAP(pmc_in_use, RISCV_KVM_MAX_COUNTERS); + /* Bit map of all the virtual counter overflown */ + DECLARE_BITMAP(pmc_overflown, RISCV_KVM_MAX_COUNTERS); + /* The address of the counter snapshot area (guest physical address) */ + unsigned long snapshot_addr; + /* The actual data of the snapshot */ + struct riscv_pmu_snapshot_data *sdata; }; =20 #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu_context) @@ -85,6 +92,9 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcp= u, unsigned long ctr_ba int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long 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 long= saddr_low, + unsigned long saddr_high, unsigned long flags, + struct kvm_vcpu_sbi_return *retdata); void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); =20 diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 86391a5061dd..622c4ee89e7b 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -310,6 +310,79 @@ int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu,= unsigned int csr_num, return ret; } =20 +static void kvm_pmu_clear_snapshot_area(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); + int snapshot_area_size =3D sizeof(struct riscv_pmu_snapshot_data); + + if (kvpmu->sdata) { + memset(kvpmu->sdata, 0, snapshot_area_size); + if (kvpmu->snapshot_addr !=3D INVALID_GPA) + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, + kvpmu->sdata, snapshot_area_size); + } + kvpmu->snapshot_addr =3D INVALID_GPA; +} + +int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned long= saddr_low, + unsigned long saddr_high, unsigned long flags, + struct kvm_vcpu_sbi_return *retdata) +{ + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); + int snapshot_area_size =3D sizeof(struct riscv_pmu_snapshot_data); + int sbiret =3D 0; + gpa_t saddr; + unsigned long hva; + bool writable; + + if (!kvpmu) { + sbiret =3D SBI_ERR_INVALID_PARAM; + goto out; + } + + if (saddr_low =3D=3D -1 && saddr_high =3D=3D -1) { + kvm_pmu_clear_snapshot_area(vcpu); + return 0; + } + + saddr =3D saddr_low; + + if (saddr_high !=3D 0) { +#ifdef CONFIG_32BIT + saddr |=3D ((gpa_t)saddr << 32); +#else + sbiret =3D SBI_ERR_INVALID_ADDRESS; + goto out; +#endif + } + + if (kvm_is_error_gpa(vcpu->kvm, saddr)) { + sbiret =3D SBI_ERR_INVALID_PARAM; + goto out; + } + + hva =3D kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writable); + if (kvm_is_error_hva(hva) || !writable) { + sbiret =3D SBI_ERR_INVALID_ADDRESS; + goto out; + } + + kvpmu->snapshot_addr =3D saddr; + kvpmu->sdata =3D kzalloc(snapshot_area_size, GFP_ATOMIC); + if (!kvpmu->sdata) + return -ENOMEM; + + if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) { + kfree(kvpmu->sdata); + kvpmu->snapshot_addr =3D INVALID_GPA; + sbiret =3D SBI_ERR_FAILURE; + } +out: + retdata->err_val =3D sbiret; + + return 0; +} + int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_return *retdata) { @@ -343,8 +416,10 @@ int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu= , unsigned long ctr_base, int i, pmc_index, sbiret =3D 0; struct kvm_pmc *pmc; int fevent_code; + bool bSnapshot =3D flags & SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT; =20 - if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) { + if ((kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) || + (bSnapshot && kvpmu->snapshot_addr =3D=3D INVALID_GPA)) { sbiret =3D SBI_ERR_INVALID_PARAM; goto out; } @@ -355,8 +430,14 @@ int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu= , unsigned long ctr_base, if (!test_bit(pmc_index, kvpmu->pmc_in_use)) continue; pmc =3D &kvpmu->pmc[pmc_index]; - if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) + if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) { pmc->counter_val =3D ival; + } else if (bSnapshot) { + kvm_vcpu_read_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata, + sizeof(struct riscv_pmu_snapshot_data)); + pmc->counter_val =3D kvpmu->sdata->ctr_values[pmc_index]; + } + if (pmc->cinfo.type =3D=3D SBI_PMU_CTR_TYPE_FW) { fevent_code =3D get_event_code(pmc->event_idx); if (fevent_code >=3D SBI_PMU_FW_MAX) { @@ -400,8 +481,10 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu,= unsigned long ctr_base, u64 enabled, running; struct kvm_pmc *pmc; int fevent_code; + bool bSnapshot =3D flags & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; =20 - if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) { + if ((kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) || + (bSnapshot && (kvpmu->snapshot_addr =3D=3D INVALID_GPA))) { sbiret =3D SBI_ERR_INVALID_PARAM; goto out; } @@ -423,27 +506,52 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu= , unsigned long ctr_base, sbiret =3D SBI_ERR_ALREADY_STOPPED; =20 kvpmu->fw_event[fevent_code].started =3D false; + /* No need to increment the value as it is absolute for firmware events= */ + pmc->counter_val =3D kvpmu->fw_event[fevent_code].value; } else if (pmc->perf_event) { if (pmc->started) { /* Stop counting the counter */ perf_event_disable(pmc->perf_event); - pmc->started =3D false; } else { sbiret =3D SBI_ERR_ALREADY_STOPPED; } =20 - if (flags & SBI_PMU_STOP_FLAG_RESET) { - /* Relase the counter if this is a reset request */ + /* Stop counting the counter */ + perf_event_disable(pmc->perf_event); + + /* We only update if stopped is already called. The caller may stop/res= et + * the event in two steps. + */ + if (pmc->started) { pmc->counter_val +=3D perf_event_read_value(pmc->perf_event, &enabled, &running); + pmc->started =3D false; + } + + if (flags & SBI_PMU_STOP_FLAG_RESET) { + /* Relase the counter if this is a reset request */ kvm_pmu_release_perf_event(pmc); } } else { sbiret =3D SBI_ERR_INVALID_PARAM; } + + if (bSnapshot && !sbiret) { + //TODO: Add counter overflow support when sscofpmf support is added + kvpmu->sdata->ctr_values[i] =3D pmc->counter_val; + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata, + sizeof(struct riscv_pmu_snapshot_data)); + } + if (flags & SBI_PMU_STOP_FLAG_RESET) { pmc->event_idx =3D SBI_PMU_EVENT_IDX_INVALID; clear_bit(pmc_index, kvpmu->pmc_in_use); + if (bSnapshot) { + /* Clear the snapshot area for the upcoming deletion event */ + kvpmu->sdata->ctr_values[i] =3D 0; + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata, + sizeof(struct riscv_pmu_snapshot_data)); + } } } =20 @@ -517,8 +625,10 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *= vcpu, unsigned long ctr_ba kvpmu->fw_event[event_code].started =3D true; } else { ret =3D kvm_pmu_create_perf_event(pmc, &attr, flags, eidx, evtdata); - if (ret) - return ret; + if (ret) { + sbiret =3D SBI_ERR_NOT_SUPPORTED; + goto out; + } } =20 set_bit(ctr_idx, kvpmu->pmc_in_use); @@ -566,6 +676,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) kvpmu->num_hw_ctrs =3D num_hw_ctrs + 1; kvpmu->num_fw_ctrs =3D SBI_PMU_FW_MAX; memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event)); + kvpmu->snapshot_addr =3D INVALID_GPA; =20 if (kvpmu->num_hw_ctrs > RISCV_KVM_MAX_HW_CTRS) { pr_warn_once("Limiting the hardware counters to 32 as specified by the I= SA"); @@ -585,6 +696,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) pmc =3D &kvpmu->pmc[i]; pmc->idx =3D i; pmc->event_idx =3D SBI_PMU_EVENT_IDX_INVALID; + pmc->vcpu =3D vcpu; if (i < kvpmu->num_hw_ctrs) { pmc->cinfo.type =3D SBI_PMU_CTR_TYPE_HW; if (i < 3) @@ -625,6 +737,7 @@ void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) } bitmap_zero(kvpmu->pmc_in_use, RISCV_MAX_COUNTERS); memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event)); + kvpmu->snapshot_addr =3D INVALID_GPA; } =20 void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index 7eca72df2cbd..77c20a61fd7d 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -64,6 +64,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu,= 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_SNAPSHOT_SET_SHMEM: + ret =3D kvm_riscv_vcpu_pmu_setup_snapshot(vcpu, cp->a0, cp->a1, cp->a2, = retdata); + break; default: retdata->err_val =3D SBI_ERR_NOT_SUPPORTED; } --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E6BBC4167B for ; Tue, 5 Dec 2023 02:44:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376789AbjLECoA (ORCPT ); Mon, 4 Dec 2023 21:44:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346821AbjLECno (ORCPT ); Mon, 4 Dec 2023 21:43:44 -0500 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 641D0D6C for ; Mon, 4 Dec 2023 18:43:47 -0800 (PST) Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6d87a8228e0so1145245a34.1 for ; Mon, 04 Dec 2023 18:43:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744226; x=1702349026; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UUFFL9WHJXhpyF2jxsBVxmfmjltHCrCzHnKko1KoPMA=; b=GJ14nbCjNglQTH4dYkCI78LgNrPCNHRIRpqxg9iGeBTX/eXZX8bfiGPgs+6N+PZR8W cOeTYyY3RrlIoun/qUjTft564N8rOC0Q8HXf4i347YAep3Jf7wrnc5GE8Xk04BWPef9c tzvnv+X9CgbC3LmZ2nCUK5oTS6W4JT4mjaR1f8Q5VjwUdSkYbJbQUcD2uFS54eKbEUPg akAEHz359/FsWMLjZ/ZM84B8uVoCp54+b1jV4EncoRg3mKqfidO3k+7zRxSI/sy/lDqF nLWtm6ks5PF4uCiVd/sSKmw5/Cxcv1EVQghz9oqmcjsif98d7h8DvfvWdaLofPnDTEPD Z2FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744226; x=1702349026; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UUFFL9WHJXhpyF2jxsBVxmfmjltHCrCzHnKko1KoPMA=; b=EV+N1wewAOF6KHWArtiXyaCWVHr2/tBHe+5nZcuWf4ZlSpd3kso5cEoz2X+nTmiarb oKtLMilConN5ZaiY3frdkKLu2C8Zt5glHFOc3FDm9zqIiHkeFMN2t2xKEwFCkqI7a+El Lpq0u28z52J8I5zTZc2C9SRQ3FpaPZEOcRNKjIG9/ZWEllilNLvdtPcdMvizQratgO2K 2ZEDssCuKRHFebeSS+0u9m+yQKvHBXsFbTTRk3h7cvwN9S9BBDvBYYB6GefBhjibNOj5 Ypt1o7ft7ANlyYDKkwP9BhM2Cl8SNNckzCejWRsKQn/URFoGYzUPcwremrkL7pQZXCnq 88sg== X-Gm-Message-State: AOJu0YxpSyzpAhjG+uC4aF6O3y/dXJBpy1ifwT9ZOUILx+Cjj+Nj7EPq dRpERm1FeMJF08iYSydyykmYTVQdG0q9Ox1L6obhWw== X-Google-Smtp-Source: AGHT+IEMmstOAFiJ8gg3Y0WTozLtdyFTXZCkTRoGOpB7jPiJ5WttcikKExLkiwyI+BGtCWCySc1/+g== X-Received: by 2002:a05:6830:1183:b0:6d8:74e2:94f6 with SMTP id u3-20020a056830118300b006d874e294f6mr2396699otq.60.1701744225782; Mon, 04 Dec 2023 18:43:45 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:45 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 8/9] RISC-V: KVM: Add perf sampling support for guests Date: Mon, 4 Dec 2023 18:43:09 -0800 Message-Id: <20231205024310.1593100-9-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" KVM enables perf for guest via counter virtualization. However, the sampling can not be supported as there is no mechanism to enabled trap/emulate scountovf in ISA yet. Rely on the SBI PMU snapshot to provide the counter overflow data via the shared memory. In case of sampling event, the host first guest the LCOFI interrupt and injects to the guest via irq filtering mechanism defined in AIA specification. Thus, ssaia must be enabled in the host in order to use perf sampling in the guest. No other AIA dpeendancy w.r.t kernel is required. Signed-off-by: Atish Patra --- arch/riscv/include/asm/csr.h | 3 +- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 1 + arch/riscv/kvm/vcpu.c | 8 ++-- arch/riscv/kvm/vcpu_onereg.c | 1 + arch/riscv/kvm/vcpu_pmu.c | 69 ++++++++++++++++++++++++++++--- 6 files changed, 73 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 88cdc8a3e654..bec09b33e2f0 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -168,7 +168,8 @@ #define VSIP_TO_HVIP_SHIFT (IRQ_VS_SOFT - IRQ_S_SOFT) #define VSIP_VALID_MASK ((_AC(1, UL) << IRQ_S_SOFT) | \ (_AC(1, UL) << IRQ_S_TIMER) | \ - (_AC(1, UL) << IRQ_S_EXT)) + (_AC(1, UL) << IRQ_S_EXT) | \ + (_AC(1, UL) << IRQ_PMU_OVF)) =20 /* AIA CSR bits */ #define TOPI_IID_SHIFT 16 diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/as= m/kvm.h index 60d3b21dead7..741c16f4518e 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -139,6 +139,7 @@ enum KVM_RISCV_ISA_EXT_ID { KVM_RISCV_ISA_EXT_ZIHPM, KVM_RISCV_ISA_EXT_SMSTATEEN, KVM_RISCV_ISA_EXT_ZICOND, + KVM_RISCV_ISA_EXT_SSCOFPMF, KVM_RISCV_ISA_EXT_MAX, }; =20 diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 225a435d9c9a..5a3a4cee0e3d 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -43,6 +43,7 @@ int kvm_arch_hardware_enable(void) csr_write(CSR_HCOUNTEREN, 0x02); =20 csr_write(CSR_HVIP, 0); + csr_write(CSR_HVIEN, 1UL << IRQ_PMU_OVF); =20 kvm_riscv_aia_enable(); =20 diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e087c809073c..2d9f252356c3 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -380,7 +380,8 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu,= unsigned int irq) if (irq < IRQ_LOCAL_MAX && irq !=3D IRQ_VS_SOFT && irq !=3D IRQ_VS_TIMER && - irq !=3D IRQ_VS_EXT) + irq !=3D IRQ_VS_EXT && + irq !=3D IRQ_PMU_OVF) return -EINVAL; =20 set_bit(irq, vcpu->arch.irqs_pending); @@ -395,14 +396,15 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcp= u, unsigned int irq) int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) { /* - * We only allow VS-mode software, timer, and external + * We only allow VS-mode software, timer, counter overflow and external * interrupts when irq is one of the local interrupts * defined by RISC-V privilege specification. */ if (irq < IRQ_LOCAL_MAX && irq !=3D IRQ_VS_SOFT && irq !=3D IRQ_VS_TIMER && - irq !=3D IRQ_VS_EXT) + irq !=3D IRQ_VS_EXT && + irq !=3D IRQ_PMU_OVF) return -EINVAL; =20 clear_bit(irq, vcpu->arch.irqs_pending); diff --git a/arch/riscv/kvm/vcpu_onereg.c b/arch/riscv/kvm/vcpu_onereg.c index f8c9fa0c03c5..19a0e4eaf0df 100644 --- a/arch/riscv/kvm/vcpu_onereg.c +++ b/arch/riscv/kvm/vcpu_onereg.c @@ -36,6 +36,7 @@ static const unsigned long kvm_isa_ext_arr[] =3D { /* Multi letter extensions (alphabetically sorted) */ KVM_ISA_EXT_ARR(SMSTATEEN), KVM_ISA_EXT_ARR(SSAIA), + KVM_ISA_EXT_ARR(SSCOFPMF), KVM_ISA_EXT_ARR(SSTC), KVM_ISA_EXT_ARR(SVINVAL), KVM_ISA_EXT_ARR(SVNAPOT), diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 622c4ee89e7b..86c8e92f92d3 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -229,6 +229,47 @@ static int kvm_pmu_validate_counter_mask(struct kvm_pm= u *kvpmu, unsigned long ct return 0; } =20 +static void kvm_riscv_pmu_overflow(struct perf_event *perf_event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + struct kvm_pmc *pmc =3D perf_event->overflow_handler_context; + struct kvm_vcpu *vcpu =3D pmc->vcpu; + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); + struct riscv_pmu *rpmu =3D to_riscv_pmu(perf_event->pmu); + u64 period; + + /* + * Stop the event counting by directly accessing the perf_event. + * Otherwise, this needs to deferred via a workqueue. + * That will introduce skew in the counter value because the actual + * physical counter would start after returning from this function. + * It will be stopped again once the workqueue is scheduled + */ + rpmu->pmu.stop(perf_event, PERF_EF_UPDATE); + + /* + * The hw counter would start automatically when this function returns. + * Thus, the host may continue to interrupts and inject it to the guest + * even without guest configuring the next event. Depending on the hardwa= re + * the host may some sluggishness only if privilege mode filtering is not + * available. In an ideal world, where qemu is not the only capable hardw= are, + * this can be removed. + * FYI: ARM64 does this way while x86 doesn't do anything as such. + * TODO: Should we keep it for RISC-V ? + */ + period =3D -(local64_read(&perf_event->count)); + + local64_set(&perf_event->hw.period_left, 0); + perf_event->attr.sample_period =3D period; + perf_event->hw.sample_period =3D period; + + set_bit(pmc->idx, kvpmu->pmc_overflown); + kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_PMU_OVF); + + rpmu->pmu.start(perf_event, PERF_EF_RELOAD); +} + static int kvm_pmu_create_perf_event(struct kvm_pmc *pmc, struct perf_even= t_attr *attr, unsigned long flags, unsigned long eidx, unsigned long evtdata) { @@ -247,7 +288,7 @@ static int kvm_pmu_create_perf_event(struct kvm_pmc *pm= c, struct perf_event_attr */ attr->sample_period =3D kvm_pmu_get_sample_period(pmc); =20 - event =3D perf_event_create_kernel_counter(attr, -1, current, NULL, pmc); + event =3D perf_event_create_kernel_counter(attr, -1, current, kvm_riscv_p= mu_overflow, pmc); if (IS_ERR(event)) { pr_err("kvm pmu event creation failed for eidx %lx: %ld\n", eidx, PTR_ER= R(event)); return PTR_ERR(event); @@ -466,6 +507,12 @@ int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu= , unsigned long ctr_base, } } =20 + /* The guest have serviced the interrupt and starting the counter again */ + if (test_bit(IRQ_PMU_OVF, vcpu->arch.irqs_pending)) { + clear_bit(pmc_index, kvpmu->pmc_overflown); + kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_PMU_OVF); + } + out: retdata->err_val =3D sbiret; =20 @@ -537,7 +584,12 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu,= unsigned long ctr_base, } =20 if (bSnapshot && !sbiret) { - //TODO: Add counter overflow support when sscofpmf support is added + /* The counter and overflow indicies in the snapshot region are w.r.to + * cbase. Modify the set bit in the counter mask instead of the pmc_ind= ex + * which indicates the absolute counter index. + */ + if (test_bit(pmc_index, kvpmu->pmc_overflown)) + kvpmu->sdata->ctr_overflow_mask |=3D (1UL << i); kvpmu->sdata->ctr_values[i] =3D pmc->counter_val; kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata, sizeof(struct riscv_pmu_snapshot_data)); @@ -546,15 +598,19 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu= , unsigned long ctr_base, if (flags & SBI_PMU_STOP_FLAG_RESET) { pmc->event_idx =3D SBI_PMU_EVENT_IDX_INVALID; clear_bit(pmc_index, kvpmu->pmc_in_use); + clear_bit(pmc_index, kvpmu->pmc_overflown); if (bSnapshot) { /* Clear the snapshot area for the upcoming deletion event */ kvpmu->sdata->ctr_values[i] =3D 0; + /* Only clear the given counter as the caller is responsible to + * validate both the overflow mask and configured counters. + */ + kvpmu->sdata->ctr_overflow_mask &=3D ~(1UL << i); kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata, sizeof(struct riscv_pmu_snapshot_data)); } } } - out: retdata->err_val =3D sbiret; =20 @@ -729,15 +785,16 @@ void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) if (!kvpmu) return; =20 - for_each_set_bit(i, kvpmu->pmc_in_use, RISCV_MAX_COUNTERS) { + for_each_set_bit(i, kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS) { pmc =3D &kvpmu->pmc[i]; pmc->counter_val =3D 0; kvm_pmu_release_perf_event(pmc); pmc->event_idx =3D SBI_PMU_EVENT_IDX_INVALID; } - bitmap_zero(kvpmu->pmc_in_use, RISCV_MAX_COUNTERS); + bitmap_zero(kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS); + bitmap_zero(kvpmu->pmc_overflown, RISCV_KVM_MAX_COUNTERS); memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event)); - kvpmu->snapshot_addr =3D INVALID_GPA; + kvm_pmu_clear_snapshot_area(vcpu); } =20 void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) --=20 2.34.1 From nobody Sun Dec 28 21:18:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D49D9C4167B for ; Tue, 5 Dec 2023 02:44:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235394AbjLECoI (ORCPT ); Mon, 4 Dec 2023 21:44:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346698AbjLECnq (ORCPT ); Mon, 4 Dec 2023 21:43:46 -0500 Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A338118B for ; Mon, 4 Dec 2023 18:43:49 -0800 (PST) Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6d857f6f1c0so2956548a34.0 for ; Mon, 04 Dec 2023 18:43:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701744228; x=1702349028; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o7KPiKrm0gOhakxC/Wb7OeJ/9kc5D1cjNr8HphWNKE8=; b=V4V0HHwFqI7vfBLDpScjHw2i44uOEurGwxLEjtWphPdTQSegNJutLhV0J/UZwyJWwo SIiv8c24ETDwTRWqFeu3orv0iFUBb15lOdB55bQzV1Daaz0IjKpjlYV7t7dQAsBOGx+V WUDDyl7ruXw2D0gzHMAEy096vVwkkzqQCW1rAU7bX9nyEQ89un2MMHOCM4p7sVeL4ew6 7G+fbcQODMpsvSMJhUDswgOzBff8yTAbbW3OCZ9eFog7b9abK5Kmm3BPAE1poMgtUQ0B h5YV5M//hihvVmFJN0F5tqgseWXIhv0PMN5eUiZ1m7zZ/ev4ehRSUHm3fYMPdMNcESEz AO2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701744228; x=1702349028; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o7KPiKrm0gOhakxC/Wb7OeJ/9kc5D1cjNr8HphWNKE8=; b=hARWL41fH/XxW4a6bUV+ixdlqvVIjvWnEhS7Nok49wmD1RZOevNqMDMTIprmWxk7Nu 9LzKMT+K/qbBtkcdduzslYIyXW8GsnVuQMh2zGuvJ76uuB6DYDSJnDmvAhzY694jA19P t2xDtlkCXZvqQfTUHB0LuPKvnaY25f7p7J5QjlAoi5TWZovHgJfbk4wGhk4LBxy045xy MMJl1NAnHIq7qeHCJ2T/PJuq5BLHAH+0ErYlm3/6vppsEnvoKcBVQ9mSlzhPFFBycsfP +0sBBco5J3GDSAvIPzfm4un4JOTjyho0JWi4bQk3pfBzVa4ZW63HUFd9OgDFSfL1ZP1V L/zg== X-Gm-Message-State: AOJu0YyS8zOhtZuE7QQkXqLiyx2bYhuI0Dj0KoZ3pWzoAcJC560hdQUb fA9g39MNEFDfXhvXuLyj6Yw2slQ3qbFWsWZWCI88Yw== X-Google-Smtp-Source: AGHT+IFjpVDyqkSHbaNqwFBeSjOTvhTrHeLCZiADHAwpcAR48Wdh3tXdrlaF62vWprf7yMCdI/PbwA== X-Received: by 2002:a05:6830:1c8:b0:6d8:322d:e76d with SMTP id r8-20020a05683001c800b006d8322de76dmr5108332ota.38.1701744227938; Mon, 04 Dec 2023 18:43:47 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id z17-20020a9d62d1000000b006b9848f8aa7sm2157655otk.45.2023.12.04.18.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 18:43:47 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , 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 Subject: [RFC 9/9] RISC-V: KVM: Support 64 bit firmware counters on RV32 Date: Mon, 4 Dec 2023 18:43:10 -0800 Message-Id: <20231205024310.1593100-10-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205024310.1593100-1-atishp@rivosinc.com> References: <20231205024310.1593100-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Anup Patel --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 6 ++++- arch/riscv/kvm/vcpu_pmu.c | 38 ++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 7 +++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm= /kvm_vcpu_pmu.h index 64c75acad6ba..dd655315e706 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); =20 struct kvm_fw_event { /* Current value of the event */ - unsigned long value; + uint64_t value; =20 /* Event monitoring status */ bool started; @@ -91,6 +91,10 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vc= pu, unsigned long ctr_ba struct kvm_vcpu_sbi_return *retdata); int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata); +#if defined(CONFIG_32BIT) +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long= cidx, + struct kvm_vcpu_sbi_return *retdata); +#endif void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned long= 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 86c8e92f92d3..5b4a93647256 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -195,6 +195,28 @@ static int pmu_get_pmc_index(struct kvm_pmu *pmu, unsi= gned long eidx, =20 return kvm_pmu_get_programmable_pmc_index(pmu, eidx, cbase, cmask); } +#if defined(CONFIG_32BIT) +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; + u64 enabled, running; + int fevent_code; + + 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; +} +#endif =20 static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, unsigned long *out_val) @@ -696,6 +718,20 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *= vcpu, unsigned long ctr_ba return 0; } =20 +#if defined(CONFIG_32BIT) +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long= 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; +} +#endif + int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata) { @@ -769,7 +805,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; } } =20 diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index 77c20a61fd7d..0cd051d5a448 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -64,6 +64,13 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu= , 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 defined(CONFIG_32BIT) + ret =3D kvm_riscv_vcpu_pmu_fw_ctr_read_hi(vcpu, cp->a0, retdata); +#else + retdata->out_val =3D 0; +#endif + break; case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: ret =3D kvm_riscv_vcpu_pmu_setup_snapshot(vcpu, cp->a0, cp->a1, cp->a2, = retdata); break; --=20 2.34.1