From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 5B794288C3F for ; Thu, 22 May 2025 19:03:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940627; cv=none; b=SWGlS0FzckAX3SLUkYsb8L0QA1Df17dCfBLjF3vv3OYafMBd09+mSQAeKh0nsdrCrLIeyQSg1jjMeGZxv5Hhw0t4HSCZgqi9Ota7Jgmt1GDTNI3usAwNjses8XU552Wv/n13346symtnuknFwy11Nwsm06LriFyCVfaz5cRY6tA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940627; c=relaxed/simple; bh=3aJLhlZAq85VhJRCvA3+qitnySOgX8ITRb2RAt2NMqo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QWDFdoGHV5/mzx54wp+h1cQEHQx2l9NrPKttEQ1Zkoqjolyj+O9RO0Iwpy1bnR/s/mGlkUEoloVyTM5zJB4E6QxTNWuaRIrq5brQkUADcdjfGhZh5g6HbcXaaq97Q1GfV4u07uiJWEh5JLGy1zguTze2XXoDtyWTUyqCHDSOxuk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=vmyVhTgK; arc=none smtp.client-ip=209.85.214.171 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 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="vmyVhTgK" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-23035b3edf1so69334265ad.3 for ; Thu, 22 May 2025 12:03:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940625; x=1748545425; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1p4+AtteizaQRcJNZKx3uxkrKCkThIoqx663iDBnv3Q=; b=vmyVhTgKzwZYeNfv7D89dhW5tS1+vi5yWe1c0Xtws8mC/Jpd48FyXDApWIlzJbP/8f a+PeWDpN+MTKyNlZpcYCBjApG8/rKbhw/LFg79QFYukG9pskZqUv4gWwCFHUURlOC/S4 NcPxvccyhwbVXCS7bkRVs2e88Zz69Fz4YNkuTLR6mDPxDM746msnuzoBvwq2R4kF1GEE zdTEu2yq/Rwg+XSNKuZ0w2RdAQAjAIeShWMdo2ztInPbD/JHiPoKzMwyTQ8AQFjYuTce ykZH/yCWUHL/HldR+V9si6XjkF0WdG4BD24+E4VymXHc6SBswmHLWWlAF3q5kjF+hJLr nlOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940625; x=1748545425; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1p4+AtteizaQRcJNZKx3uxkrKCkThIoqx663iDBnv3Q=; b=IAFsiDxxd4cPgd1wxygDA0aS+/x5Cpx68bIAiBQx1iQPEiqaS77xs7XT/MHncWLibE V3DzSdI9aj3R5hSkCZThWMtA58OfGVtB73soDEaJ5+dq2pUotKCi8o4274Mgi5zH9t2p qHeSh5FSYYqWUO7uYd05f7Jle10CSzSVFkmnTvoQ9TgSAIcY9+XzWDiRfVudo/MS4NhK M1YnsZBp2QVpZ6J16mXAtzj01Fc/e3rawNTjAI4sgRwAvxeU4lNvPJya1UpcKym16eAQ 2wLExdDl+Pg4aKPIzbnnAzxivgsLMEakrY3U+ajhDw/wybAzOnyS3bFmKKKPXy4AX5el bbug== X-Forwarded-Encrypted: i=1; AJvYcCUapl+H0isG8PQ+/mqP361xwC9uMGJuaFxM0QLcjlwdlTXU8nZOxPseOQU8MtjdOv7E27pi7abfdJmSSfg=@vger.kernel.org X-Gm-Message-State: AOJu0YwV203xm3lk0Swo8TjbTkuMAXovmhJSGFAcdvYDJoLk5URLWpVl OhrPF8Utndqna3xjd8QZzagJlnftn4J46nwCLTFzEm6DfxFxEsvwweSt1+A5Zknj0rY= X-Gm-Gg: ASbGncvBtek2CJAJQba453sSupcPXJq16CVrqj+bUV+5AoOD9x+mUH0HPpBOxZTNy9c Srz2ah/ToUAVgf1QY6QNcF5EO4zYXwgwDRHmF9SZtESVKOQI29EcOeoI0QhNeUdBD0yUvED6LX4 VjKyndm6aWZKGnWQHCltJKyWcXhbXNPGMkAhfLsz507gHloeSGnzPoOCx2YFuFRytiQ/Gij2PnY HT2g0kp269Cc1L3tVxigmWpfLIGUvkcTxMAFD9bv8ZtcJFRkAbvNOytPolQj9DZsppLEFp4Qwg2 W5Nw/VLdpPNqxRE+udGi6xZILDk1ZjjWZQUV4+9zhkqMyTG6BHn3RRcFf6XmkKoe X-Google-Smtp-Source: AGHT+IF60CjGVS+XkZOaK5ku4lafcj/BktLeKZMLaYLRiMYA2gu0KSgvj+oaPNmS3vGSkFgOpISftg== X-Received: by 2002:a17:903:18d:b0:224:c47:cbd with SMTP id d9443c01a7336-231d3257fc6mr374153715ad.0.1747940625547; Thu, 22 May 2025 12:03:45 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:45 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:35 -0700 Subject: [PATCH v3 1/9] drivers/perf: riscv: Add SBI v3.0 flag Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-1-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 There are new PMU related features introduced in SBI v3.0. 1. Raw Event v2 which allows mhpmeventX value to be 56 bit wide. 2. Get Event info function to do a bulk query at one shot. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- drivers/perf/riscv_pmu_sbi.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 698de8ddf895..cfd6946fca42 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -63,6 +63,7 @@ PMU_FORMAT_ATTR(event, "config:0-47"); PMU_FORMAT_ATTR(firmware, "config:62-63"); =20 static bool sbi_v2_available; +static bool sbi_v3_available; static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available); #define sbi_pmu_snapshot_available() \ static_branch_unlikely(&sbi_pmu_snapshot_available) @@ -1452,6 +1453,9 @@ static int __init pmu_sbi_devinit(void) if (sbi_spec_version >=3D sbi_mk_version(2, 0)) sbi_v2_available =3D true; =20 + if (sbi_spec_version >=3D sbi_mk_version(3, 0)) + sbi_v3_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.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 5FB9C289364 for ; Thu, 22 May 2025 19:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940629; cv=none; b=uYY772Gr7NAKhNWLHeHxJJP2ToMwAZvdpeHc3cXd9vbt8Jovug7ZXtFeXX3mIZYQiUuxouu1/BqOxw6yYtnO/Wdi1GDQZYf/dvwO0AePpk7oS7gjNNGwh3Bydd5nXNocl76dM2O4ln3+zJ3lz/OseDMUEQJc6ZMX2pyfAq8VVWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940629; c=relaxed/simple; bh=yOk4Au5mSZlqyc+VJN4Q60m39cr1WKJMLznBn6OTtgI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aSkuDJF/Z6/nL6csVHMT8T6VkDRrpTURO+9KuSQN4D5gye+8mr1GZ8+Ay2B+Cb3C06E1W/OepPdaQ8pLgucqLlTLksmLzvPgclG7z4OeerM+nJN7ffsalfDoNtajMbST4/A+U5G2LrfT3+3FXs2ni2/Jp5be/hW919YKFI0Fm2E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=JTd3WUXO; arc=none smtp.client-ip=209.85.214.174 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 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="JTd3WUXO" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-231e98e46c0so55194115ad.3 for ; Thu, 22 May 2025 12:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940627; x=1748545427; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LdMt+/71IZVg41NURvd5gzpotrZUIy+pYhJIn00Ob54=; b=JTd3WUXOtP502Po+/2hwr3IbPmS3Dr1LbgwRIQcHeQhMc6w02JYtDBpcDwnrQ/2OFi fb/zZBEKPyp4zL80YDYutfDQWrzUsiavXOxztSCELRK0Vc3HQ8hn9TkPT3fRddBHmjXU +ubgkwSxogMXnEcDpCM/qnCnyGll8wNIcK0KHHuKLgYVJ37886xYULROuflGrx928W7a UWX96e8sZwUEeL9e1apBNV81MDyddJbNI0qlF9zNsl9H/YgE5eMPYLfnYOzPnh3aNebD XNhQLSFCwMTnvW/cX9JHPP5MJsRKO3MGVtyILnI+CxWChHQZA/9y0bwNPI8FoPQOl7Ky psiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940627; x=1748545427; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LdMt+/71IZVg41NURvd5gzpotrZUIy+pYhJIn00Ob54=; b=N+g60ZsTYV1M8SE4MTbCcpe0wCYc0QgIiK+c/Xm4j/EmPfJ9DJRxzsLK/8nl4HJTC7 9dEZzLVDGgyUeRXdwfYZEDTI2cMgRYconmYIUSdZLve1gpd7fnMnDQac/doOu8s5QUa7 uYZMwENDFpngeWNX3JtWhZ0UGtciOq1IHvM+6kARAn/ajKPLhaI0MSAwkCcjPJaH/nEc nYffdn6jjlWLV1TqpF6R2WDU7YohPsFcFjR5cnbApdVFsGaCFztviKSD+NrCkgSVGFNB JG9ruDrOzk2ALRSU6cX9AI/9LAQ/lxtut4xPJ8xIfxNqyi8srdrsb+1KMS+yMUhBOE5E 6Hxg== X-Forwarded-Encrypted: i=1; AJvYcCXFSLIO/qEAwMdWe9MfswqA20Gkcd+c+iY8e5IgIO6JutX0v59MTavEXcx7ocaFVcm+1C6LYonK2lETmiU=@vger.kernel.org X-Gm-Message-State: AOJu0YzaqRDvva8WRWxexmvr7Zvtno5f8YZg3ssa1vcb2kohSeivNyYv ia4q7EWtQMCRGcHVuz4gqDDOvBDE9qvqcsMrZdQ39c9fF262ENfLoQdHVKAs2/GG7+8= X-Gm-Gg: ASbGncu8scgnTe7uVL2orwy+5N+uK1hi5HRxH6K8UcibmHkrCXN/yoq1jEtZJY2ul7q SiLN7y+uq2BuDMHRe3rZr8yBU/IdtfXj330qdoMQsA3m77t/aZyK8SmpLvOMlaM+SN1/l4I0Ukm Jdn+XljVRWl0Wvfkb1Z2HJJH7Wr7FdgDEq/YW9kYBW2u0VYt3eO3auCEha2HK3S014L4sijHxdz P2ZYeDJQHRBGrwEuo2k2JhSC0RW+1Gbbw9pojJdpTECtYrNRk5KFuA9h3cEp83l4RwXTXbjkzek pkmDBhPr3oMOSM8WLxyx2IrFrU/Mhv8TlVkKxQ1p1u3cl00sFnPG/j8NevSCM44h X-Google-Smtp-Source: AGHT+IFfCV//qxOe2okhTwLGpNoq8DsTJkacWkNUL4qxPXo2ylZ1GEBeSAkJ2y8sxhW9UlvW9cu03w== X-Received: by 2002:a17:903:124b:b0:231:fc6a:9639 with SMTP id d9443c01a7336-233f23cbf73mr1605975ad.27.1747940626613; Thu, 22 May 2025 12:03:46 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:46 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:36 -0700 Subject: [PATCH v3 2/9] drivers/perf: riscv: Add raw event v2 support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-2-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 SBI v3.0 introduced a new raw event type that allows wider mhpmeventX width to be programmed via CFG_MATCH. Use the raw event v2 if SBI v3.0 is available. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/include/asm/sbi.h | 4 ++++ drivers/perf/riscv_pmu_sbi.c | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 3d250824178b..6ce385a3a7bb 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -160,7 +160,10 @@ struct riscv_pmu_snapshot_data { =20 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0) #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0) +/* SBI v3.0 allows extended hpmeventX width value */ +#define RISCV_PMU_RAW_EVENT_V2_MASK GENMASK_ULL(55, 0) #define RISCV_PMU_RAW_EVENT_IDX 0x20000 +#define RISCV_PMU_RAW_EVENT_V2_IDX 0x30000 #define RISCV_PLAT_FW_EVENT 0xFFFF =20 /** General pmu event codes specified in SBI PMU extension */ @@ -218,6 +221,7 @@ enum sbi_pmu_event_type { SBI_PMU_EVENT_TYPE_HW =3D 0x0, SBI_PMU_EVENT_TYPE_CACHE =3D 0x1, SBI_PMU_EVENT_TYPE_RAW =3D 0x2, + SBI_PMU_EVENT_TYPE_RAW_V2 =3D 0x3, SBI_PMU_EVENT_TYPE_FW =3D 0xf, }; =20 diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index cfd6946fca42..273ed70098a3 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -59,7 +59,7 @@ asm volatile(ALTERNATIVE( \ #define PERF_EVENT_FLAG_USER_ACCESS BIT(SYSCTL_USER_ACCESS) #define PERF_EVENT_FLAG_LEGACY BIT(SYSCTL_LEGACY) =20 -PMU_FORMAT_ATTR(event, "config:0-47"); +PMU_FORMAT_ATTR(event, "config:0-55"); PMU_FORMAT_ATTR(firmware, "config:62-63"); =20 static bool sbi_v2_available; @@ -527,8 +527,10 @@ static int pmu_sbi_event_map(struct perf_event *event,= u64 *econfig) break; case PERF_TYPE_RAW: /* - * As per SBI specification, the upper 16 bits must be unused - * for a hardware raw event. + * As per SBI v0.3 specification, + * -- the upper 16 bits must be unused for a hardware raw event. + * As per SBI v3.0 specification, + * -- the upper 8 bits must be unused for a hardware raw event. * Bits 63:62 are used to distinguish between raw events * 00 - Hardware raw event * 10 - SBI firmware events @@ -537,8 +539,14 @@ static int pmu_sbi_event_map(struct perf_event *event,= u64 *econfig) =20 switch (config >> 62) { case 0: + if (sbi_v3_available) { + /* Return error any bits [56-63] is set as it is not allowed by the sp= ec */ + if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { + *econfig =3D config & RISCV_PMU_RAW_EVENT_V2_MASK; + ret =3D RISCV_PMU_RAW_EVENT_V2_IDX; + } /* Return error any bits [48-63] is set as it is not allowed by the sp= ec */ - if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { + } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { *econfig =3D config & RISCV_PMU_RAW_EVENT_MASK; ret =3D RISCV_PMU_RAW_EVENT_IDX; } --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 77F99289814 for ; Thu, 22 May 2025 19:03:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940630; cv=none; b=QjTmVfzjpp6fy43P9OjbLh/HEPyjNDklr7qrFxq9SBA2TwsZcm9EK6cfudDDXiBLm+YGoZam5TxNEjy5s4HPROXUlV0oVFLl4drVvdYt+R2B8IV34j5VWA5jab+n2hAPXL2zL+mjUMmyem2dNtAk654ucf4AB1fL1asuaUFTMdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940630; c=relaxed/simple; bh=rbEY388+vf1xBc6RFqGTgB4Goxixu7Il6QwVlf57D/w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=S/aaW9lNzPLflvu6CeUSnDGbAnht7jkVJzyvx4HEn7Vxs7Sz5ibn0WdUiIPRa+3m9hU5CF6QKtgS6qAlQvAU3dh/yiCnUSkkQ5nOiZvHbj/nBlHiZZY46KSPpqUSI3udFgQ3LmZb0aUYzZPcteO2nyRCG67mWwC1CvE1pTsVOyI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=AsiODRO8; arc=none smtp.client-ip=209.85.214.181 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 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="AsiODRO8" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2322bace4ceso47538425ad.2 for ; Thu, 22 May 2025 12:03:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940627; x=1748545427; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=MgSYDCFyOFl1ODnG5fRD1TrPKu6WyFc6mOzCNie7R+g=; b=AsiODRO8H2rBIh6sLO7S049C/CObiWyhOO0hyOr/g2gnv0rzr7bbLSWn1z9yVSksKX kYmlQ66TcsIIq93jgFZnwnKd8DlEBUQhSyzhEs3aV6KzgZceYRHJtFyiQwSj9gXafJ2V gUQ2LranTDclUq99NUqsTo1hKhQ19PUa9/kpq8yW30OeXkOEjWVTyiQC8K4gX0Ds6L+A MbJVDMfvYp0boysfv7DokZY1RBejnkEzdrckejpvOkkrPLDb54tA2J5EBXLtwXPg1psp I/bsc9iG78DhfvNa3xfLqSEuPt/GS4a1iwUoxJfua1hUMI+NAl5nsBEwpYMiy6eUrAGB tgbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940627; x=1748545427; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MgSYDCFyOFl1ODnG5fRD1TrPKu6WyFc6mOzCNie7R+g=; b=Dm9UOlU9EL4RRV3+ubw/W4iEkT5sFIY7yRYTKDv0aWoxpj7WKTyRDgus2uBhn3Jl2q 4NkdwEcfjvvwT3yavyJ08QCS4mHSIAB6mWjK/moxl9+9Id1Qa5v9OAVEqnjF8BKF+QxM eBd4k2AWlY8oeW6OppbepZBv7e6Qa80F+3xUmnVeFEtazXAk8quylcP9+gaenz6Jj3Sr imc8TEdJJGEW7MRqXjfudMIY4r46MNckfSRm2+tnDi6zgvk/rWtuSF67C0Vv4UXrd7WP +haHf33ojeiyVJi9s9ilSI/jZxZglkk1HfsQXvwQFiaiCAY3rh6BTgLXdrLL2RrLv5NW XRGA== X-Forwarded-Encrypted: i=1; AJvYcCW0SUjVeZsu4t2TV2u1HRf7SSqCFj1IqWx+uKRXnpFle8yuL25L9U3uQM0gb0NBxQN9k9oAsskvnpieCVI=@vger.kernel.org X-Gm-Message-State: AOJu0YxNErYvUFVKKi1wrfeW6EzXVaMsfzpAp9YKT07AdPwFeLI7t2nH ajar/3bObq5G0pcRMTh+KsqHwXjEcY6LvSsGv8KRgZSmcVRnauyBTWVKMYZOdJXcty0= X-Gm-Gg: ASbGncty/HuokqhcW11uHtIYqv8rHEVxVU5gRIP2zQ8Bw9mUhrOB/nd5U/OOs8JLhbA ndlHWGhGJQbJwg0kOCfn0ZqmZKImcs9HDfVaJkZHCalvHqcHr4+S6zIx5RR3pPsxZhDqlpEhhtk /WRfh0+W/vVNy3l8lRaxqKUfQi8bEvIgcqill0QqOhdhoUVwSenP+79XBfNxE2VR5cx3s6shIaA VEZ+9XZTq1o5HRp5NJvG5D0cXTbK33nDC92c0hOXRIMFuzD7ouz3wPmOsIhvztDYk8tYafpTSze 6CHCXthGGFSbDPPwV+Byd0/gpXZNjqaT7RNaJHKXLY6F2Tu7C7ez8zXQlOzeWHIF X-Google-Smtp-Source: AGHT+IGs856eL1BrViuwG7Snet4kjwTZqseUJx/cN/TovXlD6rlR9pixORAh5bRSKluNwVXzmFHaxw== X-Received: by 2002:a17:903:2301:b0:22e:364b:4f3e with SMTP id d9443c01a7336-231d4562cc5mr379326455ad.49.1747940627664; Thu, 22 May 2025 12:03:47 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:47 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:37 -0700 Subject: [PATCH v3 3/9] RISC-V: KVM: Add support for Raw event v2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-3-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 SBI v3.0 introuced a new raw event type v2 for wider mhpmeventX programming. Add the support in kvm for that. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/kvm/vcpu_pmu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 78ac3216a54d..15d71a7b75ba 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -60,6 +60,7 @@ static u32 kvm_pmu_get_perf_event_type(unsigned long eidx) type =3D PERF_TYPE_HW_CACHE; break; case SBI_PMU_EVENT_TYPE_RAW: + case SBI_PMU_EVENT_TYPE_RAW_V2: case SBI_PMU_EVENT_TYPE_FW: type =3D PERF_TYPE_RAW; break; @@ -128,6 +129,9 @@ static u64 kvm_pmu_get_perf_event_config(unsigned long = eidx, uint64_t evt_data) case SBI_PMU_EVENT_TYPE_RAW: config =3D evt_data & RISCV_PMU_RAW_EVENT_MASK; break; + case SBI_PMU_EVENT_TYPE_RAW_V2: + config =3D evt_data & RISCV_PMU_RAW_EVENT_V2_MASK; + break; case SBI_PMU_EVENT_TYPE_FW: if (ecode < SBI_PMU_FW_MAX) config =3D (1ULL << 63) | ecode; --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 9477F29AAF0 for ; Thu, 22 May 2025 19:03:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940631; cv=none; b=jo23TqXP+SVpY5UaFWKV+RMPeHPE0kzTlRakYqCDHpHc8POiMWSA3myCU4FYcyr14cHFodOR54LOOF62DJqJqXhBrVAWMdDzpD+SlDQ4M774wTamYChIQG6iOJnjC1041EFUG1VbnhnJCS/RfvFx/piJJ4qXU8plD80TZ80HckY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940631; c=relaxed/simple; bh=H9KzbsOadvXKfagdeIiEb89m8/n30ul8CWjn2anZ1Ww=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RctxZ1M7ZsfMi7OynyxflR3Z9zGObUhhFi+TFCZAoLy/HynYtuoE80j6BU7R0gtNBBGW1hirRP12IMAfwQmq4xNpirwpVO8diNVfCxZwD9JSPxgt/oeqlCJVDwjksuqWZDFRXw1WQ7jJdEIkYAhPalSlGTdiim6jE0aTvWont78= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=pr0sl2Ls; arc=none smtp.client-ip=209.85.215.177 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 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="pr0sl2Ls" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-b170c99aa49so5601246a12.1 for ; Thu, 22 May 2025 12:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940629; x=1748545429; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=fDxMnssaauwljQxlk9zc0qXTf5HAsn9k6GC49F2uSIA=; b=pr0sl2LsUduqLrQhnx6l2dXR7ZXc4JQiquUmHnwAF1y/1+cHit834oob72gqtcmymC BYxh39qlf5E8/WpvQN2AlHB3oTZUd4DH7pgpigiboNesh6JFQLyvOFPf2dtX09oCjg5e ek0aMYUqUyJE3OdVMXtK9jZVnrn3JgOKOLhNwyK75Eh1TTdxdWg1XpEMw2h40V6PJgC0 6Y3KDaBlsL3pWfu0tO9vTDa84ljX2DxieP9LMNYTSn+Tsmt6NGM0QnQbjB/6E/16OBh6 Dbae9HU3lAP+/btiZxghgkKzODEC5HN1faya6t7tS1J0novIVYmQ4PQ4i2uc+A+CRLeU YQ1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940629; x=1748545429; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fDxMnssaauwljQxlk9zc0qXTf5HAsn9k6GC49F2uSIA=; b=XZQMIYB+DZEWVHEBM3kfKDPBXEDlDEG+lBB7xfv8qImmDOAH7Fv2vELBPX5tAlAuMs 5F6J++kOL8yc4kchrweAkjFUaUjnMyBJTmZS7KWTiv5WUz+k/ydQxxs2XU2R7iBz4PLZ w1p5bFQv8QNLMxm4pyQQ6qIvfZqxJFxN1AULug0T5+0gg+YjgMe4BXxj4NeRgRgg33DH lv1ctGRZWColdz0QzOLqyZ0PQn0buO9xq/IRPVzqGsc3omjol9NWd/4p57GHMFk7W+dv OBPDOY6IICeEleYc4h7T/0oxnfSztFl8/Im3G6eZ8lyZsI4hlgQkLShMvC1tCn7C/oGt R4Aw== X-Forwarded-Encrypted: i=1; AJvYcCWT8S2yUT30B8XVA9Y+3vZ9Ectus9l/kaRVLc1sP96eFgaSn57SQUzn2Jq4leYe2gmEvPXgkwKwp8+gYNk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1t3b0tQdPDtfpS+3SEBFoUhQbohgAERBVm2b95xnHPJv7ishL XfU7pKF9gNNnhvO4RMCx1zBQfW6D+/bDsaPx8GT46w3sAlD6GlntDNn2ieHz9bCL/oI= X-Gm-Gg: ASbGncs9qOP8p1BdCNJEqnBgHXalEkKCLqhzM/Hz3e1JF+GUrczV+ebeutua/avhIIZ s/rXXxevspPZ6SXq66O60K9rz8QyVUFfZMubWne+LZauQFGBaxe1bHg1fxNKLE0o/ZFnfclZP0O MXrTbOW2NQ3ugJWm4p3iCmz/kPF2G0omff3wMB0LaAZYA9JtthYXFUJWs3XUwPMI97x1p1FB7NY FYpDl8l/EYRhv3QxeONzxFnKjfk7dH5ZfYdxOZXkZFEi2pFjMi6htdW4OpGZWRmV3kh4WRAewot MOByDENaJ8vdGqkWJrneh2vCu9Ghk9DaVHVvWV7+byGoHuzt9UjVbLKPcXGSdeyX X-Google-Smtp-Source: AGHT+IG3oxLFDfbSNt6EH0ZdTZ5dYeLSDKnP9JAmNGN1V7oKcB4sbiLpNV8c6lgFnjfD3N9PA0Fqjg== X-Received: by 2002:a17:902:cf09:b0:22d:b240:34fe with SMTP id d9443c01a7336-231d451b294mr410839265ad.25.1747940628742; Thu, 22 May 2025 12:03:48 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:48 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:38 -0700 Subject: [PATCH v3 4/9] drivers/perf: riscv: Implement PMU event info function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-4-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 With the new SBI PMU event info function, we can query the availability of the all standard SBI PMU events at boot time with a single ecall. This improves the bootime by avoiding making an SBI call for each standard PMU event. Since this function is defined only in SBI v3.0, invoke this only if the underlying SBI implementation is v3.0 or higher. Signed-off-by: Atish Patra --- arch/riscv/include/asm/sbi.h | 9 ++++++ drivers/perf/riscv_pmu_sbi.c | 68 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 77 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 6ce385a3a7bb..77b6997eb6c5 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -135,6 +135,7 @@ enum sbi_ext_pmu_fid { SBI_EXT_PMU_COUNTER_FW_READ, SBI_EXT_PMU_COUNTER_FW_READ_HI, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, + SBI_EXT_PMU_EVENT_GET_INFO, }; =20 union sbi_pmu_ctr_info { @@ -158,6 +159,14 @@ struct riscv_pmu_snapshot_data { u64 reserved[447]; }; =20 +struct riscv_pmu_event_info { + u32 event_idx; + u32 output; + u64 event_data; +}; + +#define RISCV_PMU_EVENT_INFO_OUTPUT_MASK 0x01 + #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0) #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0) /* SBI v3.0 allows extended hpmeventX width value */ diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 273ed70098a3..33d8348bf68a 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -299,6 +299,66 @@ static struct sbi_pmu_event_data pmu_cache_event_map[P= ERF_COUNT_HW_CACHE_MAX] }, }; =20 +static int pmu_sbi_check_event_info(void) +{ + int num_events =3D ARRAY_SIZE(pmu_hw_event_map) + PERF_COUNT_HW_CACHE_MAX= * + PERF_COUNT_HW_CACHE_OP_MAX * PERF_COUNT_HW_CACHE_RESULT_MAX; + struct riscv_pmu_event_info *event_info_shmem; + phys_addr_t base_addr; + int i, j, k, result =3D 0, count =3D 0; + struct sbiret ret; + + event_info_shmem =3D kcalloc(num_events, sizeof(*event_info_shmem), GFP_K= ERNEL); + if (!event_info_shmem) + return -ENOMEM; + + for (i =3D 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) + event_info_shmem[count++].event_idx =3D pmu_hw_event_map[i].event_idx; + + for (i =3D 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) { + for (j =3D 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) { + for (k =3D 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) + event_info_shmem[count++].event_idx =3D + pmu_cache_event_map[i][j][k].event_idx; + } + } + + base_addr =3D __pa(event_info_shmem); + if (IS_ENABLED(CONFIG_32BIT)) + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, lower_32_bits= (base_addr), + upper_32_bits(base_addr), count, 0, 0, 0); + else + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, base_addr, 0, + count, 0, 0, 0); + if (ret.error) { + result =3D -EOPNOTSUPP; + goto free_mem; + } + + for (i =3D 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) { + if (!(event_info_shmem[i].output & RISCV_PMU_EVENT_INFO_OUTPUT_MASK)) + pmu_hw_event_map[i].event_idx =3D -ENOENT; + } + + count =3D ARRAY_SIZE(pmu_hw_event_map); + + for (i =3D 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) { + for (j =3D 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) { + for (k =3D 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) { + if (!(event_info_shmem[count].output & + RISCV_PMU_EVENT_INFO_OUTPUT_MASK)) + pmu_cache_event_map[i][j][k].event_idx =3D -ENOENT; + count++; + } + } + } + +free_mem: + kfree(event_info_shmem); + + return result; +} + static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata) { struct sbiret ret; @@ -316,6 +376,14 @@ static void pmu_sbi_check_event(struct sbi_pmu_event_d= ata *edata) =20 static void pmu_sbi_check_std_events(struct work_struct *work) { + int ret; + + if (sbi_v3_available) { + ret =3D pmu_sbi_check_event_info(); + if (!ret) + return; + } + for (int i =3D 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) pmu_sbi_check_event(&pmu_hw_event_map[i]); =20 --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 011AC29ACE5 for ; Thu, 22 May 2025 19:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940633; cv=none; b=A2B4kdKC9cTBCKDPjzTg3NQzmgLMdgiCu9dpWuXZUjFljfj3z2W1xso4Np5i0TfrVld1v3DtIc095aDOBAs+wu7SxUo+LoQRwUbO16eOomdnn8jVChj0ierffDcDU9DUWGZkLGVaCv72E1IytYZji0rD1Po2YP2kB09xSEncXNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940633; c=relaxed/simple; bh=XtnAlGo1CMxnPO6JKVTmzRQxeB6Au4XDCuScFVWUWUo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hGNbOnCaqPwJLDaZyPTLKcjXriKfSGXp5B25wra6V2QKhXk2NvtGSLUlDY40XluaRU0u43tjI5ALnHu1ahSAbFYtv3FNEUovyiCbtYWipCM/bpjX5S8gm5+2+PUzpt29wgPc0BX9RxmdiokrM0ww4boSyw/i6XpEUakgnTzykHg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=hBqJluA7; arc=none smtp.client-ip=209.85.214.180 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 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="hBqJluA7" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-232054aa4ebso46272905ad.1 for ; Thu, 22 May 2025 12:03:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940630; x=1748545430; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vACMU+uOLyj1bSQA4LVJfCqGiOCws5jCYjHnigZ5yyQ=; b=hBqJluA7T1hPq4nhcbGH1tMB+Flx7JrRS30F7wX7GSI4gvdRq0cELC8J2mjLeXfDPn sfSzcsmqfC9nW+lqU7ZM3sRRbsAZLviIN8qzhW2UpS854v+NqEIyrorzD+vGoyql13lo L/v4hXWiML5xuWQSDc6h4FvMygPXxG31DuFwfEN+i7MBWUkOEX7f3js4laNonnjJJCK+ PUyG09Z0LuzQZ7Rw4GnT2chn1/FPidZCZRzgsqcKH4bvgN+cFiZhwtVZxo48lC9u2hH5 5hVXVemJ+rVqQMubhJ08cg9uRNPWtIbHDTBC2bEqpUJUHedUTmRKkX8YQ7Z76UaS9Je6 7/og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940630; x=1748545430; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vACMU+uOLyj1bSQA4LVJfCqGiOCws5jCYjHnigZ5yyQ=; b=HpZUT3rPsnI0a24oOVaRIF92M2tg2N8R7Mh062Hq/nwPh9GieUECzOmuGt9BkMTWO+ Z7YNYmTgG/B8qj3t6wcZX1U+R+n69f/wdFt+3gCPMHizhoNEHX8kXMZrsUnPYGhlfxhD Eb0+g6n4UaKia736/Nxp0U/v161dF69z/IwF0HMD6KR41+A3Ig+Riw9rwf71/4mnuIBh 6Z7L1q+ge68JeXgNvIIpd6yWA/zj6Tfsc1v0DKOanNzEp1RBEOQDaoVlrgvY5gHCZ6oE U0W8JqAT8YwRksB0JtDxe9FVjsZjA47k/dd4+h4YS9FgFJlP+XqJCUYFTMjB7jqGGyIC HrBQ== X-Forwarded-Encrypted: i=1; AJvYcCVXtd7Ke5LkhMYqmZyWM7q7QQtN/rqrfLBUGg4FXLvk20DzvCoonwcVSGJ2yofWhsksUX56btqIwmKLgfM=@vger.kernel.org X-Gm-Message-State: AOJu0YwyXaoYH0Q/8jR6u6JDt+gjAEoLq6pTBVCvMAKXr4bkl32pDiAr MT9bb0dnjKSc7ydbWS7rEp0XKxuLT0C+eRVAGMGHFbmI8hRdZ7yzFmlyZsS/TvIHbgY= X-Gm-Gg: ASbGncsYFi3pEA82/bx9S/wh0lmnUyAlnJ2zprTR2LV+MfTMcpEwNkdtPGVIwV1cPBz aM26DjB827OiiIlIMM0qbN4ENJnJyUJosJgvILG/zihOqtbj/lzSuSPtOAH/bT1XBvkITTias9N cI5C1thOkt4ITbc65UzwbhFPlbCw7caUmyM8+Od9jg58kNUac16705TTHHWtQoWPkxO3S0Nn2Xb hVuPydNPUrgRpdF19qYATgdnRk0ZX2dqnsxc2lekozoD1Cwct07FCLikbMxe6xnoV7zznN4pAkc sm1ifkY0T47/1BzjWoM9KKWe5tcWXqmrWFfLSWnNB7sGgPeu2H8YaSpW6Ut1WitBx7ek6HYIbvI = X-Google-Smtp-Source: AGHT+IE3kKHYdKAzGiVNjvDReRIr3YIXelNJnoPPmROIVHQY8jqz/Q50hb8MBVNgcI6K6CgV+C9sRg== X-Received: by 2002:a17:902:ea0e:b0:22e:8183:1fae with SMTP id d9443c01a7336-233f25f4019mr1589425ad.41.1747940629899; Thu, 22 May 2025 12:03:49 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:49 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:39 -0700 Subject: [PATCH v3 5/9] drivers/perf: riscv: Export PMU event info function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-5-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 The event mapping function can be used in event info function to find out the corresponding SBI PMU event encoding during the get_event_info function as well. Refactor and export it so that it can be invoked from kvm and internal driver. Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_sbi.c | 124 ++++++++++++++++++++++---------------= ---- include/linux/perf/riscv_pmu.h | 2 + 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 33d8348bf68a..f5d3db6dba18 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -100,6 +100,7 @@ static unsigned int riscv_pmu_irq; /* Cache the available counters in a bitmask */ static unsigned long cmask; =20 +static int pmu_event_find_cache(u64 config); struct sbi_pmu_event_data { union { union { @@ -411,6 +412,71 @@ static bool pmu_sbi_ctr_is_fw(int cidx) return (info->type =3D=3D SBI_PMU_CTR_TYPE_FW) ? true : false; } =20 +int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig) +{ + int ret =3D -ENOENT; + + switch (type) { + case PERF_TYPE_HARDWARE: + if (config >=3D PERF_COUNT_HW_MAX) + return -EINVAL; + ret =3D pmu_hw_event_map[config].event_idx; + break; + case PERF_TYPE_HW_CACHE: + ret =3D pmu_event_find_cache(config); + break; + case PERF_TYPE_RAW: + /* + * As per SBI v0.3 specification, + * -- the upper 16 bits must be unused for a hardware raw event. + * As per SBI v3.0 specification, + * -- the upper 8 bits must be unused for a hardware raw event. + * Bits 63:62 are used to distinguish between raw events + * 00 - Hardware raw event + * 10 - SBI firmware events + * 11 - Risc-V platform specific firmware event + */ + switch (config >> 62) { + case 0: + if (sbi_v3_available) { + /* Return error any bits [56-63] is set as it is not allowed by the sp= ec */ + if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { + if (econfig) + *econfig =3D config & RISCV_PMU_RAW_EVENT_V2_MASK; + ret =3D RISCV_PMU_RAW_EVENT_V2_IDX; + } + /* Return error any bits [48-63] is set as it is not allowed by the sp= ec */ + } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { + if (econfig) + *econfig =3D config & RISCV_PMU_RAW_EVENT_MASK; + ret =3D RISCV_PMU_RAW_EVENT_IDX; + } + break; + case 2: + ret =3D (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16); + break; + case 3: + /* + * For Risc-V platform specific firmware events + * Event code - 0xFFFF + * Event data - raw event encoding + */ + ret =3D SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT; + if (econfig) + *econfig =3D config & RISCV_PMU_PLAT_FW_EVENT_MASK; + break; + default: + break; + } + break; + default: + break; + } + + return ret; +} +EXPORT_SYMBOL_GPL(riscv_pmu_get_event_info); + /* * Returns the counter width of a programmable counter and number of hardw= are * counters. As we don't support heterogeneous CPUs yet, it is okay to just @@ -576,7 +642,6 @@ static int pmu_sbi_event_map(struct perf_event *event, = u64 *econfig) { u32 type =3D event->attr.type; u64 config =3D event->attr.config; - int ret =3D -ENOENT; =20 /* * Ensure we are finished checking standard hardware events for @@ -584,62 +649,7 @@ static int pmu_sbi_event_map(struct perf_event *event,= u64 *econfig) */ flush_work(&check_std_events_work); =20 - switch (type) { - case PERF_TYPE_HARDWARE: - if (config >=3D PERF_COUNT_HW_MAX) - return -EINVAL; - ret =3D pmu_hw_event_map[event->attr.config].event_idx; - break; - case PERF_TYPE_HW_CACHE: - ret =3D pmu_event_find_cache(config); - break; - case PERF_TYPE_RAW: - /* - * As per SBI v0.3 specification, - * -- the upper 16 bits must be unused for a hardware raw event. - * As per SBI v3.0 specification, - * -- the upper 8 bits must be unused for a hardware raw event. - * Bits 63:62 are used to distinguish between raw events - * 00 - Hardware raw event - * 10 - SBI firmware events - * 11 - Risc-V platform specific firmware event - */ - - switch (config >> 62) { - case 0: - if (sbi_v3_available) { - /* Return error any bits [56-63] is set as it is not allowed by the sp= ec */ - if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { - *econfig =3D config & RISCV_PMU_RAW_EVENT_V2_MASK; - ret =3D RISCV_PMU_RAW_EVENT_V2_IDX; - } - /* Return error any bits [48-63] is set as it is not allowed by the sp= ec */ - } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { - *econfig =3D config & RISCV_PMU_RAW_EVENT_MASK; - ret =3D RISCV_PMU_RAW_EVENT_IDX; - } - break; - case 2: - ret =3D (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16); - break; - case 3: - /* - * For Risc-V platform specific firmware events - * Event code - 0xFFFF - * Event data - raw event encoding - */ - ret =3D SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT; - *econfig =3D config & RISCV_PMU_PLAT_FW_EVENT_MASK; - break; - default: - break; - } - break; - default: - break; - } - - return ret; + return riscv_pmu_get_event_info(type, config, econfig); } =20 static void pmu_sbi_snapshot_free(struct riscv_pmu *pmu) diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h index 701974639ff2..4a5e3209c473 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -91,6 +91,8 @@ struct riscv_pmu *riscv_pmu_alloc(void); int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr); #endif =20 +int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig); + #endif /* CONFIG_RISCV_PMU */ =20 #endif /* _RISCV_PMU_H */ --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 C41FE29B219 for ; Thu, 22 May 2025 19:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940633; cv=none; b=ooEpxd8W95VoBFKzVauoZ86hrx08fKYq0OT8WZ6Gmaj4Ej3Ws1Zq3UBgbhnHFzaN0jgyJsfr1i8Z+lo93Oo0nz+AIJJx+GY3EPUdYUPmFoapxudqhNR/xuNCk9mug5T++U8z+jyPk4QL0Ft29vkJxDZeuEkyeooad/DV9yLi74Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940633; c=relaxed/simple; bh=Ty1W0taSmzUHkELzUxGMfb+RxYmgPja/Am3sZkteGZQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h2eCFtDSzHlR4DhEw858WiT1EJpeA7SylyIpucnxZrbukjpy8leWSytzYdv7zk0MHhhjPhkm1vV1q0gEYP2PuPG9mkJpxE3dVfQ95sJprL488YZPZaBkpzhtBL7Olnpz95mTcXtdxuaVUlSu52gZ0Y7d1NEVP/ES6WUYRXXvAlk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=smORANQo; arc=none smtp.client-ip=209.85.214.170 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 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="smORANQo" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2320d06b728so46162035ad.1 for ; Thu, 22 May 2025 12:03:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940631; x=1748545431; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vZzBI2/8zxd6TBktJeo9hVnWnoPSXGrqMFkDVMKCuo8=; b=smORANQojjDNT7Kdq/AI/RG3vh94JHZcxgEm42nIVhhO7C8NkECpvqPD41lRuNiote LvttFFVfv9YTdB94FpGFPhn/1VnfzJlNvhNaVJDQWqwT2aaH+SbDQJsTeOMil29H+jGO BJezZrElHD3/eiz6Zs7JkTDP+rbqJ1ye8Dh07BcgyPsmWh4BAd3HcnnSM0sm3jodH2N5 hNkySnun8Vap8oAsb+kSiYamzgeQaSdOU+SbXfu++0cEiJY+R8AfNAP5VNCGOOWLnKRm WRhoZmUwQES88Cl8irIi1LakFDirhVL+CKW3ZoQ3Uj2Pr/WQeYQeio1++3Y9JZLBE9jU 1lAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940631; x=1748545431; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vZzBI2/8zxd6TBktJeo9hVnWnoPSXGrqMFkDVMKCuo8=; b=dVvaN8odh6JdjTN4wc694ya1XAwCFx3+S5eHNowuCkpI7ixvhW1c9kVyyzX/ftpF1Y 6062yZbtd79l/A5Q7lhNBtP+iZukvT7RM5osxi+9ovWRJAR+z5Xosd0uFZbk/b5VS9e7 7B6xvyQ8sxrb0WFH0T7LGyjYzOU8Lc7KdlpLbbNoUo+uFAWgwi3RcJtDJglXBHEHuq3Y cFFDAqH34Qj7drrUvDSm6WjB0ztbMlU5hQq06pqaF4UU7/HDYM3nZ4G+LZPNdO2zOsX2 VZhUS7XQDx8mCcVKI06LmOrAUKyI+zUWMKxAnZC3lnIT6Y0r1aOkQuXuixKI9w31Rpco PnSg== X-Forwarded-Encrypted: i=1; AJvYcCXv+4bdLFV9bGeNz/+50mofdNHVLBtoz+ZUn1lqYqwlJvdz1fUFlP3PxopiHzbwiq+f82l4FrXc7xqvE80=@vger.kernel.org X-Gm-Message-State: AOJu0Yxkoqjsahljrh/Wr34a5YReqhR/GgLVTerik1W8eoBxQjZmlp4L xOqkozgVXob/TSMnFtzaun4lronvP6sB4P+oeZiJs5JqlTwAdTQmj6FwpqEqOd8bI+E= X-Gm-Gg: ASbGncvRAusB506WqCQ2H3FNv+8dWFUrLlPHZrCpv+/qXQ/e3Ov87tfwop9ZoOaN0Qf fSoaoj5GyPKI8JX45wNxe6CJYz/lbqQN3qsoTABszQeDxrytYEJ3umwJJhtGLHNF0/ZhjaTYDb/ 7EI+9IDOlJbjMW9I/lJrZ/fvkcWyVBth5/ij6DFcpOeva5QPPd92uBUQRh3tAS1jqJ3jQsHP/4q D1Q/moq0ASBD6AtyhfxtXZyvH31ttHkNQczyxYCNyz6qkVwe9Ik+gFdKL/SEpoiQ2qlzDwWH7Vi N/Mkf9syKMFRIfhhD8Ur5fPL8NOxQMxvZesP2ICz82oy5FbX09hvBFnJ7Mse0BRf X-Google-Smtp-Source: AGHT+IEr79nTwwFBEJoxFRK5RNwqD1jq2SDcjYk7D0rQW6WwVVGL0IpJIiwt2oBQtuzcW3AhD+odEw== X-Received: by 2002:a17:903:94f:b0:22c:3609:97ed with SMTP id d9443c01a7336-231d43d9bccmr330946415ad.30.1747940631058; Thu, 22 May 2025 12:03:51 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:50 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:40 -0700 Subject: [PATCH v3 6/9] KVM: Add a helper function to validate vcpu gpa range Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-6-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 The arch specific code may need to validate a gpa range if it is a shared memory between the host and the guest. Currently, there are few places where it is used in RISC-V implementation. Given the nature of the function it may be used for other architectures. Hence, a common helper function is added. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 291d49b9bf05..adda61cc4072 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1383,6 +1383,8 @@ static inline int kvm_vcpu_map_readonly(struct kvm_vc= pu *vcpu, gpa_t gpa, =20 unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn); unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, b= ool *writable); +int kvm_vcpu_validate_gpa_range(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned= long len, + bool write_access); int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,= int offset, int len); int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *dat= a, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e85b33a92624..3f52f5571fa6 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3301,6 +3301,27 @@ int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_= t gpa, const void *data, } EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest); =20 +int kvm_vcpu_validate_gpa_range(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned= long len, + bool write_access) +{ + gfn_t gfn =3D gpa >> PAGE_SHIFT; + int seg; + int offset =3D offset_in_page(gpa); + bool writable =3D false; + unsigned long hva; + + while ((seg =3D next_segment(len, offset)) !=3D 0) { + hva =3D kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, &writable); + if (kvm_is_error_hva(hva) || (writable ^ write_access)) + return -EPERM; + offset =3D 0; + len -=3D seg; + ++gfn; + } + return 0; +} +EXPORT_SYMBOL_GPL(kvm_vcpu_validate_gpa_range); + static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots, struct gfn_to_hva_cache *ghc, gpa_t gpa, unsigned long len) --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 E70C029C322 for ; Thu, 22 May 2025 19:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940636; cv=none; b=IcDcnG3vjILAuZ7G2vo6DPc+NonADZJ9Fi94LlucmGrqvLuyO4vlSOWwCgIZ00COuUsuvQ1m42jMvmBWrpF+9QaimHdKTTKkc4we6euua759RwDomsm4W37Ymwa3Bw6eOgy28/q8nVP/rL1HjVQdjw6d8Ni0uHKuYvQR4enbt6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940636; c=relaxed/simple; bh=15uNoWw1oFmc2RxCK2PSoncmAe9FQepCxYJUbSkoVrQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MHjwzb7J4zRGnRS/oZ/jO3IqRWyg2x/0ReZMekWGPXcph7HeD6XSJh1Ky/jijshGoFeRGheP5uetXbmsBEs1xh2yhuZYqU69Je1Yei0/izk638J7o0S8Q0BlYPY8AvGHBq3rs9p9rBdmNTPqeYU0j3Db3l/7El23yZdKrDAnai8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=Jvv0HrfZ; arc=none smtp.client-ip=209.85.215.182 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 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="Jvv0HrfZ" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-b13e0471a2dso5445216a12.2 for ; Thu, 22 May 2025 12:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940632; x=1748545432; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=hlpauo+rroktx9kyAnwP3yRkffTzlKUHEtIrSQJtdwQ=; b=Jvv0HrfZiLEVmlP0FLF2oJo4pr+TqEaSjswrUwy7iqfS3VKpYHVEFc5OFRnxo9pK/t yqgdQfE5z5SQt2yEnvdyzdmY/C0QbZhAysbAsoXgFtIjwJBbFpBntMHAaCN8fR4yukA7 sPpUf3JIgkuL46DO16eUvi2lMgYAOd2k7cxgAE8Wr6SSGsLNx7hu7ZwAd/78/XebEsPG qRT30LBXKpWeO3jHHAtMfntlbMP8mGUOKrjBj4qy8Jinda/RRMS8qHGqdyppZRIK1adw QBYGYdDygrX2gUr1TLICAlZ5HNdvKx7qJUGDkUm3oR3Aiu7567zbitNQbRzFo7DnyG8y N9mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940632; x=1748545432; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hlpauo+rroktx9kyAnwP3yRkffTzlKUHEtIrSQJtdwQ=; b=tHYviEoQIp14AW5nq2uevAFtdKUAxiVfyzFng2282Qlq0oDUqK6Dh1v2TYaHw59yqF aDBg48eG0OG3OLcFKENXO0SDRs5EJc3tCTDphqq1D+JhvvnO+xoGhq4v3Axn8B9wvLCl 0T/RL4wycAYr/5RGPf5Pl/kBCFOCUAPC22LnU+/kprWK94ESiceG3wGEDmzAje6EfTDa 4F3+OTRYEBNXbbDMovBXI1XkMuwLnwj8vJ6bVWNPQbk5wk4A1hKuHMDAJAa+jrCRZ+hW F7Y42StRtZ5WuSDMiH6JYAYZQyw4fGNWw/fQLmP9gI6X04umaAYpO3qL6XonSihTTEFr 95PA== X-Forwarded-Encrypted: i=1; AJvYcCWmIWcSgUKCE3BSUJxdMBHhWesDnllqvEmpeJCk282dbJ159xiLX9FRYCba0r1iMqnBcPNCetEYidQQe1g=@vger.kernel.org X-Gm-Message-State: AOJu0YwmZEqDl3JZRWZxPrMPYXE7krukuLO//hKnzEaE5CQUHX88XIk1 wmMysyjHFDGlVoKLbuQCsnuBogheiVqKzjAZVOf+WU5Jlfl873ufUYwHBFKbtb1j5ro= X-Gm-Gg: ASbGncvwl8IzQoJxbwsmvwjuF1hPCaavN4LPdwb6h/A24KM+Y2U2U23XqsY1AEe8qKh 1AoPFBdQKNH5Z5jP4UuCG74BeKrk/YZi1N3jbBH6uQekRwly7NBo5vvYXRTTlEN5OBCuYFPpNAG Qq+KEXfVfftQXs0RaJd3xNfmUNfOWRy5A43u/1x65yMej2toZUvQHyV+DFlz0uT0iQULR7znzAC Inw6Thmt43ES10/iYLI2S5WET9YIp/g3K9gs8WTBqwe092yiMtSK3YIsdNjDByBhTdFe1UayKzc uU5JAPwySWLUnWKSsyA69UiCGbJKjJdiVN0cDx/T15Url/YlszBm4PDoAOOQfebq X-Google-Smtp-Source: AGHT+IFokfHrS0cqFKUDrcGnm0bjUiSThIlPHjvO9RTk66q9cyYFiPgwK1o9zc41Z294fx1ovdpLTA== X-Received: by 2002:a17:903:1aec:b0:22d:e57a:27a2 with SMTP id d9443c01a7336-231de36bb6bmr358484745ad.23.1747940632165; Thu, 22 May 2025 12:03:52 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:51 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:41 -0700 Subject: [PATCH v3 7/9] RISC-V: KVM: Use the new gpa range validate helper function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-7-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 Remove the duplicate code and use the new helper function to validate the shared memory gpa address. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/kvm/vcpu_pmu.c | 5 +---- arch/riscv/kvm/vcpu_sbi_sta.c | 6 ++---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 15d71a7b75ba..163bd4403fd0 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -409,8 +409,6 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vc= pu *vcpu, unsigned long s int snapshot_area_size =3D sizeof(struct riscv_pmu_snapshot_data); int sbiret =3D 0; gpa_t saddr; - unsigned long hva; - bool writable; =20 if (!kvpmu || flags) { sbiret =3D SBI_ERR_INVALID_PARAM; @@ -432,8 +430,7 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vc= pu *vcpu, unsigned long s goto out; } =20 - hva =3D kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writable); - if (kvm_is_error_hva(hva) || !writable) { + if (kvm_vcpu_validate_gpa_range(vcpu, saddr, PAGE_SIZE, true)) { sbiret =3D SBI_ERR_INVALID_ADDRESS; goto out; } diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c index 5f35427114c1..67dfb613df6a 100644 --- a/arch/riscv/kvm/vcpu_sbi_sta.c +++ b/arch/riscv/kvm/vcpu_sbi_sta.c @@ -85,8 +85,6 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vc= pu *vcpu) unsigned long shmem_phys_hi =3D cp->a1; u32 flags =3D cp->a2; struct sbi_sta_struct zero_sta =3D {0}; - unsigned long hva; - bool writable; gpa_t shmem; int ret; =20 @@ -111,8 +109,8 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_= vcpu *vcpu) return SBI_ERR_INVALID_ADDRESS; } =20 - hva =3D kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable); - if (kvm_is_error_hva(hva) || !writable) + /* The spec requires the shmem to be 64-byte aligned. */ + if (kvm_vcpu_validate_gpa_range(vcpu, shmem, 64, true)) return SBI_ERR_INVALID_ADDRESS; =20 ret =3D kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta)); --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 24E5329C328 for ; Thu, 22 May 2025 19:03:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940636; cv=none; b=kreiarBCjR6QtsY99aV3WZBVV//Vsj2EhsYgLBqW1OAMl3ZVU7k/on5fZv4N73kMe60mwnvrwlcqKQwvvRlBSj6se1HS+YQ7tn9Isczya+AiREZu79w821WiAk6Jk+GOvt+Qhw7tEaSglxyiw+MTL0av/D13+pjuB4s2pPAlni4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940636; c=relaxed/simple; bh=UtTdmWX6U4vkBHMHrjueL7kq6ik9nHTWYO5el8M7IAE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=G8gIVNhYVh8s5onms6lklZXAGiHhPcP8odwKL53aAoydV1GXq+xaeARicERcoMVvrqHCEEqX9r4AIKFoM5pKkvEHZ7y2GMC9+K2thzDBd68h90eLvf+yy9TH1zSkNc1VBjTiSgFyHC5SPEXUgQ8bLZ2JodmNPTM2U5jKUzrF5dI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=gJgCx5uw; arc=none smtp.client-ip=209.85.214.179 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 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="gJgCx5uw" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-231fd67a9aeso1774575ad.1 for ; Thu, 22 May 2025 12:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940633; x=1748545433; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=DF8qwdvQiSR8ZsqVN4u28RN14jUMJY3Pi0vpFa2D75A=; b=gJgCx5uwiIEOFpuFRJaDH8ctpfF6q2d3WdGgGIct8A+LC4q2W8P/qj4qUQex3YjOPm cygfnlZpERJpy0UY3v9wsnB/dY5iZczltJw3exry2nvzAnHEzawwciPRhwwOJ4OmRi4w z0Pw6BpdeeqbrgZtlroWYbXvsz/d+2qsz/8XQP1EFNFkOhtopfm1k8SFIBJL+QFGIiTz kmLRF1nwwZ5pE0uL6Rz/R0Ogi4Dw7yn8ByL/81jyyru2Cnq2W9FeHGRGLhGyAIpdFqGK bx82k4SG9+ivx6jzm/SJmewZbUOjL+HLE7j1hEvsB8j5P8KWa1rYV6GjD8ZXngsr0rIW 04GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940633; x=1748545433; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DF8qwdvQiSR8ZsqVN4u28RN14jUMJY3Pi0vpFa2D75A=; b=uCO/dVG+48E60mfZmtOYaX5hK7M1vM0vs7ebGmuhFkE6JlyiX2zLa3vvqPrEyUPBwe bGsGhQHuORJUdFweA0XnRNds/6y5V8j/QYV9e9fouebfUM5N/0Bgz3iKqCJLfkjeXLRz arFSu8DwgpsmhIEhlMZT+kGy7rUP97XYNIVhikqf4LoDFqO1mQiz34Li0nCSBqrFQISe uvPphA1LpzGIHA6bxT0C7elOlQv0zb7EbxmfT7crjVh4qk23iaYE9itoR/T62gyize99 pcIaXdWffnTLJhEOVFsfs0b2ZdFJv3J5VkXfa/WgGwuE1gEeVz60PieOBiTLSMHBa9/Z rYhg== X-Forwarded-Encrypted: i=1; AJvYcCU5whjDqqQYeOdTelM/x+GSCfsPx+TLWtpYeXPGaRyCjJsUmTfo4sHe7U+lgMUz0XCK/ZvfJ3jK4OOOvcs=@vger.kernel.org X-Gm-Message-State: AOJu0YwAUX0HhCj0E192qn+Vj8JPVOwe6w3+/c87V5aftIYJqhJBAvXH mVd9Dg5jTWQrdrS1ZO+9vZG1nkZkPGLs14uL86j9e9/zdnkopowgQOSbSGi0atx2ePY= X-Gm-Gg: ASbGnctB7fLdBrarCrTL0/sjaJE9QZ8ZWuM8k1bb7m7cEZ57iSmIVQHCUoA4Wzgf2bx tUz2NgCTW4eMNt/EaTo9yGk1ZFwZajTaC2eeER11fyuuKoK7Aj12CsyHu5STQRmdyTeqhKY/Nny 2kiuF4ivNRwz0IhBf4vmc9mWqpWSedWH+32B4npHLaL6m200VtQXuq5jiCtdORtwCEKwAetk97V SmlEXeEiMGjnk47jSDZY4AT3c4pexEB9FWw2rH1yIbceOloVOkAIvGlD+IVQZv5EQEMjce9ja+/ KoznUI0ww6CQSPGsaNFJANlWRPq4DKz9B7NEb0WUfrUYtRVgZnlsWaOBTJN56nh5 X-Google-Smtp-Source: AGHT+IFOp5NbQAetfcmDIrzFN/H7GeRjVktpIXFM9JpmrYmqufWq4+kFSWRLQINNJ6B0dpiY8ak3gg== X-Received: by 2002:a17:902:e78b:b0:231:7fbc:19f3 with SMTP id d9443c01a7336-233f0680998mr8028135ad.12.1747940633282; Thu, 22 May 2025 12:03:53 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:53 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:42 -0700 Subject: [PATCH v3 8/9] RISC-V: KVM: Implement get event info function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-8-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 The new get_event_info funciton allows the guest to query the presence of multiple events with single SBI call. Currently, the perf driver in linux guest invokes it for all the standard SBI PMU events. Support the SBI function implementation in KVM as well. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 3 ++ arch/riscv/kvm/vcpu_pmu.c | 66 +++++++++++++++++++++++++++++++= ++++ arch/riscv/kvm/vcpu_sbi_pmu.c | 3 ++ 3 files changed, 72 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm= /kvm_vcpu_pmu.h index 1d85b6617508..9a930afc8f57 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -98,6 +98,9 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned = long saddr_low, unsigned long saddr_high, unsigned long flags, struct kvm_vcpu_sbi_return *retdata); +int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long sad= dr_low, + unsigned long saddr_high, unsigned long num_events, + 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 163bd4403fd0..70a6bdfc42f5 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -453,6 +453,72 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_v= cpu *vcpu, unsigned long s return 0; } =20 +int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long sad= dr_low, + unsigned long saddr_high, unsigned long num_events, + unsigned long flags, struct kvm_vcpu_sbi_return *retdata) +{ + struct riscv_pmu_event_info *einfo; + int shmem_size =3D num_events * sizeof(*einfo); + gpa_t shmem; + u32 eidx, etype; + u64 econfig; + int ret; + + if (flags !=3D 0 || (saddr_low & (SZ_16 - 1))) { + ret =3D SBI_ERR_INVALID_PARAM; + goto out; + } + + shmem =3D saddr_low; + if (saddr_high !=3D 0) { + if (IS_ENABLED(CONFIG_32BIT)) { + shmem |=3D ((gpa_t)saddr_high << 32); + } else { + ret =3D SBI_ERR_INVALID_ADDRESS; + goto out; + } + } + + if (kvm_vcpu_validate_gpa_range(vcpu, shmem, shmem_size, true)) { + ret =3D SBI_ERR_INVALID_ADDRESS; + goto out; + } + + einfo =3D kzalloc(shmem_size, GFP_KERNEL); + if (!einfo) + return -ENOMEM; + + ret =3D kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret =3D SBI_ERR_FAILURE; + goto free_mem; + } + + for (int i =3D 0; i < num_events; i++) { + eidx =3D einfo[i].event_idx; + etype =3D kvm_pmu_get_perf_event_type(eidx); + econfig =3D kvm_pmu_get_perf_event_config(eidx, einfo[i].event_data); + ret =3D riscv_pmu_get_event_info(etype, econfig, NULL); + if (ret > 0) + einfo[i].output =3D 1; + else + einfo[i].output =3D 0; + } + + kvm_vcpu_write_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret =3D SBI_ERR_FAILURE; + goto free_mem; + } + +free_mem: + kfree(einfo); +out: + retdata->err_val =3D ret; + + return 0; +} + int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_return *retdata) { diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index e4be34e03e83..a020d979d179 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -73,6 +73,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu,= struct kvm_run *run, case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: ret =3D kvm_riscv_vcpu_pmu_snapshot_set_shmem(vcpu, cp->a0, cp->a1, cp->= a2, retdata); break; + case SBI_EXT_PMU_EVENT_GET_INFO: + ret =3D kvm_riscv_vcpu_pmu_event_info(vcpu, cp->a0, cp->a1, cp->a2, cp->= a3, retdata); + break; default: retdata->err_val =3D SBI_ERR_NOT_SUPPORTED; } --=20 2.43.0 From nobody Mon Dec 15 00:33:12 2025 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 2400B29CB3E for ; Thu, 22 May 2025 19:03:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940637; cv=none; b=GTkE75+tNtvwHgcs4nt+7b5Y6emm1Q66QX1IjfgJ/HsT8JVsmSrbwnUVAu8cVkgOAfiU5rYKxQ+B8ktfcl7EH32J3MfTZ3/wmUYOpdpJbTHv3SDWue6qpaioxnpCT0yNppLxjNj90NxC5HOkzAh3nxI8NFwoHtVbyXXhzPcbuP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747940637; c=relaxed/simple; bh=+prUsGSfboQFhKZbNUw86ACE/0BQoa8YLYJgH17nChg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JMfD45HIZ9jQP+MnmFvadkWLkyDnb/I8Rc4iZcfO6Edy0788XeRNIbvg+yvG74WII+vArA915YXB+bbniZOPn8PRchnyT3jglvx/T4p5mUw9xWZN4gPJcq0zGeZ2n2d70r7LPVRU6kb86GQ9nsv/D3emyW1NTw/G4rPYFeLurYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=0p11iUaT; arc=none smtp.client-ip=209.85.215.182 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 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="0p11iUaT" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-b26f5eb16a5so196167a12.0 for ; Thu, 22 May 2025 12:03:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747940634; x=1748545434; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NL+JjaCkPLIbpCFGEZpnZEqKtEvm89wxDo8gtW9i88g=; b=0p11iUaT8U4KfYVX4wsHjz+PP/4MPkc1EZ4dobSvBL2m+uaZJu3V0I0YtNCbn8UEXd k0hct8l550QR/JewSlGF284F6tyolOwrOyV6Tt3OGWEaQA8O61FLbybHclWBmV/vg9B+ adUL+2a0H5wxZc6X45yaHisMnuBV7YLT2pH2NnK4ZEB6L3dqFjFSk4o+m6lKprpHSy1X pmrCByxvo/SYlKMwe785Z8WQB4guoCG6qZxhcOQDnMBlGV4FQXpqfG14XfUtTIr/oHzX LLJ6Myecm6QOtTtPpYAJHCaFexbvy6PCOoZcRFxgRkwJDRwN9744e6wXGFs8CQswXe8b T39Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747940634; x=1748545434; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NL+JjaCkPLIbpCFGEZpnZEqKtEvm89wxDo8gtW9i88g=; b=QtpXK/B/tVLHN1fbsgb+P2OuQpVVBay8UmKs8AOD14cJAGnfoVLdXznB/3hlALtFWI H74gRdayCES2MXCXzADaR7AEG92QPiY7PTFJrq/IAJs7PPS7KlzME/YjCbadaipa4xwf oWqfwKKedsGG98R82j4DPBlp4mGyt9CFOi64YMSE5LKNTvDv8DI1SPwIgDBdSeiKom9T xaRfps5wI0jnVs89wITZDPYBO+h+SxRy/krhH3ojbVlhYCkEl+lDSJUjymOPom14KsOE r64RE7jQjzrenaUfmLs7TuEf8X66n8FKslRTkU4L8rwW/KHxarGgCketJG+ppmARFO/t wL5g== X-Forwarded-Encrypted: i=1; AJvYcCUC+3FF3yNGaPJFcmiF+hU08cT+kPmQ4KFC6cExS53tg4VzFXaAeomo9UhkP3iPMn85jNKck2JyM8yHOqI=@vger.kernel.org X-Gm-Message-State: AOJu0YwRNU5W4TctsOtxaGKF0V5aWlAMS+CQDTifOIRdVWQeAKahPRre 5J5bzMTlt+cOhiozGf+uiN2sc5kLYkBMs1VthdeipQJ+nuzgWQL+6KD5U+/JDj7kTmM= X-Gm-Gg: ASbGncuzafulX9RPJZwuLTXGH0jxuYGJlvSMchIEn2CIorDxC1Sp286ymjPyZXXX7Q9 t/dZ3dmFTXERoTl6jQ6NGb3WTgxNHZCe36/Ckl/e5dVrQeUAYhAZWCoGkO+EFldVx057J/1AOs5 4npphB9ww56c/QX+GAzBSDpDPwgFRWxQCHTUsKaK/h6zl8ZpCuiMmYdxBAqsuC0yCXTcRQ1a9Zz gqRbKVJ988P4kLe5dU6JAaLAIlYxkmVPXMClDQLNoVdrFIO5SEZVE5pq9APVHDwzWy+q+U8wuor yIcbFRinRUHKdy/7RJ4HMea8wLr5BNbWoPxq9BBxON2OZay2f1z3h2KuanCRVz9D X-Google-Smtp-Source: AGHT+IE7MkEZajOwtulJ1pSoS+YJGY/C1JUznV8zU99zjbdE3O1pdB4nhaqsx7X0UH6Pko2Lha+e4w== X-Received: by 2002:a17:902:e545:b0:232:1d89:722f with SMTP id d9443c01a7336-233f067c489mr6091775ad.15.1747940634357; Thu, 22 May 2025 12:03:54 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4e9736esm111879155ad.149.2025.05.22.12.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 12:03:54 -0700 (PDT) From: Atish Patra Date: Thu, 22 May 2025 12:03:43 -0700 Subject: [PATCH v3 9/9] RISC-V: KVM: Upgrade the supported SBI version to 3.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250522-pmu_event_info-v3-9-f7bba7fd9cfe@rivosinc.com> References: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> In-Reply-To: <20250522-pmu_event_info-v3-0-f7bba7fd9cfe@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-42535 Upgrade the SBI version to v3.0 so that corresponding features can be enabled in the guest. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/include/asm/kvm_vcpu_sbi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm= /kvm_vcpu_sbi.h index 4ed6203cdd30..194299e0ab0e 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -11,7 +11,7 @@ =20 #define KVM_SBI_IMPID 3 =20 -#define KVM_SBI_VERSION_MAJOR 2 +#define KVM_SBI_VERSION_MAJOR 3 #define KVM_SBI_VERSION_MINOR 0 =20 enum kvm_riscv_sbi_ext_status { --=20 2.43.0