From patchwork Wed Jan 15 18:30:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940790 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EDF79C02180 for ; Wed, 15 Jan 2025 18:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=I79XNjeFJDqTt1htfVlvOUzQ8soYlRn1xH9ay7TRmfM=; b=K8ecCwsir4jswx Ztm8FX45aVDxiOdRu8JtOAz9mjfqplE+njp2pukC2C0ysrK+Z2VD0WguxxQwdTibPhW+emgNN4zE0 zQKySJwq1jD4z/qP1HtGzhmlSplhjkHTPuLjq9PDBqQxk1BP4+0BsXwKMjb5wgK6E4gzeUEQiLpvH WamQamZZYy3l/BH4HIsz3b/UxUmT+yBQXDgBY+VemSy8vx2WlO5V6Ae96c/4FKwXAZ7Q3xuodb2lp iS5qryieUEbVK32OcwplHKACk8DCiXIDE8YzGCsNjycQtL+tG2k1ggELNSSSCpRMbs0KiAO7Sisou xEC83jwuNqubUffgPVxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8Bf-0000000Clin-0vyW; Wed, 15 Jan 2025 18:32:19 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AL-0000000Cl8e-1GwV for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:30:59 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2167141dfa1so1960995ad.1 for ; Wed, 15 Jan 2025 10:30:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965856; x=1737570656; darn=lists.infradead.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=R6MbfDRCc6A6mGBsw+fhL6wjTkBIFejSaI+SDSiajdA=; b=JbBJhzksa1B1U282EUSJtPqm3lhRTZChH8Mllx1Q9p+tF/FpXh07NDYX0fuZGFB0mX Kuu+4FpR7a/91y2HFkLTeB0L0Kr7o5Cg1BeJYkUC8viq5iUoU/mfADXaETiOLXuS7ro3 zRXB7XbuHSzsLioxtifL4qZD7BwkFIqFI4x3kv80/SVZUp2UWxqGGmo6WLFQHK7EFPBb yyFXbhnhwKuxBhspALx+Gw9JBq4TFQLOGWcHRQx0IUYOjSR+MKWRhGJAT9OJJSzRnAAV z2SvWg8vCjKPGwE4hUT8lAgNtoPy3wzUWq6a7NjsEkozXUuiw2m42nAVcoVvD+XsReUX v8lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965856; x=1737570656; 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=R6MbfDRCc6A6mGBsw+fhL6wjTkBIFejSaI+SDSiajdA=; b=AL3/crRSptJCBX5Ik9gwhWCrzqf7qGGARimj2Vt4jmnVmTrsrIlswyz/ickXiaSOrm Zfm8cSLNAM84RvIrhHeH+xK7R+/dtNZoaCdMbXW6eNLNxCWFjw5A5y/eqVUaLVLRdxWV T3OdrECOEQ16NOiPNGHFjuNEu/VwWPeIfFfotQy+gSZMQxi7LG83j5YQWyI9efQg3Smm otnuB3he7NdOUMdHPXRXWJGmzoE0fTFELbEwG+vSDHEFWE5N7QXIJBG3/DLsTvur2IFh YOoQHEFz9zUW6r7FpBGajwqK+bW/IywNIChO4FW8WvrifXp0g6aa4UnE0XfxSl6iCTUi Vnjg== X-Gm-Message-State: AOJu0Yyy65zDSG8MlWoPrjqHPbm2ZHWpPx8Hx+a2GgK6YHdKHrpc6Tdd PIi5mPDHI75Kd5fQNhzId55ltTa9wCtTqeaUGq59dhFgD7DxBRp43R5+pWTth9U= X-Gm-Gg: ASbGncuRrB+vUSUEkJ/a4GZOnzDkB4x/JX5Ivo8JMXeL6Y1RPvIjvDVQ/waexlsqjVH 21Y+bAu/DX5BaSUFUjG3ynC5QKAVSojK2M4V6A7JsvahmdpSf7e+xvQ7MuuhNPcq0In/lLbQT/U V4k8VqVT5gOBOpVoq4MC9YxIIr0SzDMmzKZtiVbWdnL3gnzuevHC8Fo2z9JTm6qVsDGTHYRJal4 GHLcTC4Zxz7dN2mohWA1g6YblGzlKEZwzHl58EMwc2EedhUQDO4oWGaxcbA2AyCXCj1PA== X-Google-Smtp-Source: AGHT+IGH5/giAEf8Q/ecUlIbOiNlBZurjf3IoH6KsqiI1EGl06ksaFaVD7yVqQ1dvhjlbV18FEvXig== X-Received: by 2002:a17:902:c40b:b0:215:ba2b:cd55 with SMTP id d9443c01a7336-21bf0b76a12mr58588755ad.2.1736965856596; Wed, 15 Jan 2025 10:30:56 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:30:56 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:41 -0800 Subject: [PATCH v2 1/9] drivers/perf: riscv: Add SBI v3.0 flag MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-1-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103057_350300_41AE6AAE X-CRM114-Status: UNSURE ( 9.17 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 194c153e5d71..170aa93106b9 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"); 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 >= sbi_mk_version(2, 0)) sbi_v2_available = true; + if (sbi_spec_version >= sbi_mk_version(3, 0)) + sbi_v3_available = true; + ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING, "perf/riscv/pmu:starting", pmu_sbi_starting_cpu, pmu_sbi_dying_cpu); From patchwork Wed Jan 15 18:30:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940794 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CD063C02180 for ; Wed, 15 Jan 2025 18:34:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0KqIaxNWg4LqiFDRBid1pC0tEeRZuUo2ZcjVpTsOdyI=; b=MvMFOWMa3Rwf1C G+upvoBVmAGN5f1LhK43G4unvzcAO0v5LlLPwSgCjAfPEm+OoPygrP7MbsdrEt5yD+9Ixp9se1lwD +5h1lHpNtEqPKQgtL8nFFAcrtH9kRH9YcaA583ytTDErg8DCiL9mZW8D4izg+9VXSAy4hbEcngGbh d1M2f4bGDSlG4z8Lrx4ea+FC0Dvaez3RtjJKED8+j7qips61GSh+H9JRdGW8oOudk0b02IDjFEyYw M2nkeq61dMZkxKCsVTbFyaxxbzj9RXa9bj/uMQp5LBofKeeSrOmEOEG4ww07NU91v6SsSmVh4dj37 D5B1DDIjlPja9qGPVnUQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8EA-0000000CmRh-2MpJ; Wed, 15 Jan 2025 18:34:54 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AM-0000000Cl9U-0zIc for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:00 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2167141dfa1so1961265ad.1 for ; Wed, 15 Jan 2025 10:30:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965858; x=1737570658; darn=lists.infradead.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=nTd9Qo5vidtOLE+cxHwuaQqNzwk6goJIP9mJXkel4TI=; b=wzg+C70y9UjY0htO85yiAYz8hzdw7aQUWp0ioyCGBnW5lCzQ9YPMU5X84FeEtMD8PG P5GBibOKB5nmkf4qm7TzdoYRyrPcpP1UpzjXXeH4l9ys7mEjpaWXaqEkYDig39qOkB+0 QBmgdZf6vbWsBspYs+yEntoZGmwou6fAWzbvHex282RE3ni82+Pc9Ep5Xx3XZpMGpZWA gCaBvFJaYuR45DeALO6owTMQ0HwCxgVjUcupTekGFYYQUj90rgdsRSQBX4dCQcyIDXMB BJBnlXz/VpqLEHe+IHsO84nFdjAieXUy6RlcJ2CWaM/TvLDGLeGBbVlLSPHQpGF4Mnux gFqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965858; x=1737570658; 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=nTd9Qo5vidtOLE+cxHwuaQqNzwk6goJIP9mJXkel4TI=; b=GY7zNXFEkVI3+4zn8iqWsHpotTo4z57d6Mr4utQNwaELlktLX4fVYPszfJsyTDReyi Gt1ZUisw2yrCzLCCbj+Oc1o+lDjeqgmzs6thDpUo1kT4ssqvAEefwAKhMWZVepPVySJ6 Ei++CZNLt+PldJcNR2QtMmID9d+4DYnj9qb/d0Jc/DVu7k2dWUrGxDA22ZiSP0Ba0Zvh /NRoVxVRXu2yZAy2nDCNawDo4fvG/McP+WlmD9D8s7GLZQaGSvBq9t7rHtoseobWVS80 y5taNZJ6N59fVm5YRN/a/wrM8dzIbmq9BOWwNq2cxEiKE0PD9xY8Cw/u+JEoDjomKBMn gijQ== X-Gm-Message-State: AOJu0YyNPtbpRWdCz0gfDlrrY8gBq8sfLiosR1RwlNcjQwQLM0XQHEd4 Ag1LtLc1ONVfegZuWDiH04abIX00ZihYyvmgSRcVQuyRUldtH/pKciIGIas9beU= X-Gm-Gg: ASbGncu4EhWKqBbbTDQryK+36XAsW0CvHxc02ZwDfbmT7kbn9kWT7EE/EoxchI55JT+ HIgJrwhBfrpOn8gqyA2e2yC8wvVSkdGxcJQ/LU7w7sn/KWq+FXHlvZtOEvfBCriwnmY4pyMegLy zqZaRKz0kaA3TrTslvxq11A+3a8mkA9V/Jkst9Uvc3CIm9znbcFkuMcsdpEiN6Hm1TM1mZepQa8 RH4hdg6JyThXhLRXTrjwVCmCPXSvXv+3XSg3uwVkOlMLUlI6YAQMGyZNdpbfWI/z6TPGw== X-Google-Smtp-Source: AGHT+IHHO3B1HJhEuywvwBIX7b7bfSC/4ve6r1ZbOzw3ZYVhbL87vWT2YRpjRvKHUMwV10zBAIgK0g== X-Received: by 2002:a17:903:2291:b0:215:58be:334e with SMTP id d9443c01a7336-21bf0bb89cemr59154695ad.10.1736965857674; Wed, 15 Jan 2025 10:30:57 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.30.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:30:57 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:42 -0800 Subject: [PATCH v2 2/9] drivers/perf: riscv: Add raw event v2 support MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-2-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103058_279322_ED6B115B X-CRM114-Status: GOOD ( 15.86 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 { #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 /** General pmu event codes specified in SBI PMU extension */ @@ -218,6 +221,7 @@ enum sbi_pmu_event_type { SBI_PMU_EVENT_TYPE_HW = 0x0, SBI_PMU_EVENT_TYPE_CACHE = 0x1, SBI_PMU_EVENT_TYPE_RAW = 0x2, + SBI_PMU_EVENT_TYPE_RAW_V2 = 0x3, SBI_PMU_EVENT_TYPE_FW = 0xf, }; diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 170aa93106b9..5d5b399b3e77 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) -PMU_FORMAT_ATTR(event, "config:0-47"); +PMU_FORMAT_ATTR(event, "config:0-55"); PMU_FORMAT_ATTR(firmware, "config:62-63"); 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) switch (config >> 62) { case 0: + if (sbi_v3_available) { + /* Return error any bits [56-63] is set as it is not allowed by the spec */ + if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { + *econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK; + ret = RISCV_PMU_RAW_EVENT_V2_IDX; + } /* Return error any bits [48-63] is set as it is not allowed by the spec */ - if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { + } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { *econfig = config & RISCV_PMU_RAW_EVENT_MASK; ret = RISCV_PMU_RAW_EVENT_IDX; } From patchwork Wed Jan 15 18:30:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940796 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 541C4C02180 for ; Wed, 15 Jan 2025 18:36:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ROOgjRt2xEhyIyozzXAsX+4ijL2IyWPxCWf+WrIxwtE=; b=nwcW4Vo+I7dZ/9 7oC1QtAsnwwo6t14Og9p9HxJ1nHVgXBKpIUAqDrgd6a/kdQR8zIKpP2FspEt+RVDfow9qHokspQxc I2rLArGydzatghWTNoQ2Got3PxOMZp1T7sn+uVkaGx3I3fJuu4VsMaUGDU1eOwNsGPm6v7rxdKxtl a2CvJwWxJrG+D/YkNaIeC3ofG7T/6iKu2ekDxMTvpyThl9ZMnw+6emUCMpOlPDh7LFTzdz6tyobtu c7Uvay7eRJdr8ZRCQB8PfBL8AlShl7o5EQhO5AWM5YYUcv6AxRn+X2Fiv2WwYLa0eVNfqwJ1sH7Pv 8RLrIqn4SBBygRdnwiAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8FT-0000000CmnB-0DIq; Wed, 15 Jan 2025 18:36:15 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AO-0000000ClC6-3NYb for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:03 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2166f1e589cso155305615ad.3 for ; Wed, 15 Jan 2025 10:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965860; x=1737570660; darn=lists.infradead.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=hCeV16Tw9Ue2BbFuxP2Jm0elvtAXwe5u9EmpHjH7wzE=; b=h8Yp7TE4M/x2CnoHbrQtjf0sxDOkhXQzJ5XdpO8HWEWW432Xtd6YnFejCEG7e1wWom kXN/s7O6jQSpEtKFyKJB4AwD9TaUWJPqajSyDDwSTaTaNrug7mPMbkkGuEWUWKfsBuCO lx2soA1Dq9oCKrCAGXKBbLI4B/W+M5I04sLYxkIi52g4NhDc3b8Qsp13YTU4movEWUQH mhosY8j/NbmW15LLYi5plThDWbBNrhQgmB4RcMONX/yyNFZnXlkas38nDvCyBgaaGcoj wuThcvgqNAQ9PI4lQZDPUgPBypdGZk0BMx52soz6CzMTrVb1UZuu7Ye+dGBrvIFWfrur aVaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965860; x=1737570660; 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=hCeV16Tw9Ue2BbFuxP2Jm0elvtAXwe5u9EmpHjH7wzE=; b=Ux9L4JnUxp91szUf1gHdyYJItFiBgr2M4a0vsfJRXDPHThATL8T0oZzftahhZYgnR/ BbYM3UF3JESZqIkYAx/8m2QBmj1MS5botDVO290dRi3jDkXXoLQ4dg3DqiZ7QpTMv6YX hhXFjgQO4OMn+K1P+S201/yLIBvnxmI9ZalxEBkNMP48bLEdH9ASPscU2G2S61JoBHTr NXdDrvBids8JqwSIFoNk1MKsbIcM73WCNHfdok0zo1ODpjk90U9MA4XjL88zhF1kDatP goIpvQVlFj3y7+E2RT4kn1PF1owBc7kOVtzf0sfGzOJoimMPTbonvanImE1JGeivyuD5 N/Fw== X-Gm-Message-State: AOJu0YzSA4jRqiQ7KLkug3pAilRRHJMyCRft/vyG6D91eQ3Ko4OAy5Tg 57twOjzS75aZQUzGS55RgLXeCUpYuhoUFoorSvg1DpljlLGVktFH973jcwLAA6c= X-Gm-Gg: ASbGnctTxVtKjvNO6RHgl3P/HOI/Ic4THHR2fVAZrO+pL9KUXwTSyEDZJBohgSu1hN4 bb9d8O08NipPWHucwcdKnI+8z5VIuq945wuSdzblS1SCmxNPeSxaOI0WbNiXCdtYT8cSBUN9DTu DHAxFC4R/KNApj/PTeuRWQUN/ZeMBsNXiwBx4oH0k2WBzaXHZ9yNV12qqI9NSRtAMj+9aGrZFeO xMof+VtvEeK22pTs/gzut5CfzOVPVkYuGpikIWaQCtMmdretqA/u/tcgmfT3hA0vByHCQ== X-Google-Smtp-Source: AGHT+IFhv3d+iNN38adFn+dH29zIS7C6pmY5MPy1fONgMvFeK1H10VHPHqcDN6p90NhFpRdpeycXlA== X-Received: by 2002:a17:902:f68b:b0:216:32c4:f807 with SMTP id d9443c01a7336-21a83fdea82mr472230285ad.45.1736965858786; Wed, 15 Jan 2025 10:30:58 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.30.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:30:58 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:43 -0800 Subject: [PATCH v2 3/9] RISC-V: KVM: Add support for Raw event v2 MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-3-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103100_864151_AE46DEC1 X-CRM114-Status: UNSURE ( 9.95 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 2707a51b082c..efd66835c2b8 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 = 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 = 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 = evt_data & RISCV_PMU_RAW_EVENT_MASK; break; + case SBI_PMU_EVENT_TYPE_RAW_V2: + config = evt_data & RISCV_PMU_RAW_EVENT_V2_MASK; + break; case SBI_PMU_EVENT_TYPE_FW: if (ecode < SBI_PMU_FW_MAX) config = (1ULL << 63) | ecode; From patchwork Wed Jan 15 18:30:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940795 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3ED55C02185 for ; Wed, 15 Jan 2025 18:34:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7Zr0LXgBxxMQpuG3gm1FzZ+aD+TuQ2Ts98Y66Gwfs+M=; b=1dEJTi0GAwxMhx vykeFKhtcJsDoqWdVtj8I99MGCD9YDz8rfxhqn7oyRIWMx0xn0HBgkEHQ2HzgJScsWe6zVAjrx2Ev 6ZdXsuLKe5M0UC0GvoGwto/RLKSo7Fb07CRuSQyQGQq1TDR7pnWtykBX8UVqgQZuMivr/s3jD818t PEVagRGP/AG3CDXFTCgEuzOhu5hAtJWlCqdwJgrfvFt9arX9VvwqKfJmuawp3Vj7J/PBnt3aVQlmd PgA8IZbT07/Pkq9b6LlZlQ5ICWVkZuNDpd3/tNLSWXQp7TloljNI6sIkDN4GWQLXPZRThAPurSDK/ xai+qLTUYOsnOYuKBiSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8EB-0000000CmS9-0h9N; Wed, 15 Jan 2025 18:34:55 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AO-0000000ClBn-1qqr for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:01 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-216395e151bso1994945ad.0 for ; Wed, 15 Jan 2025 10:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965860; x=1737570660; darn=lists.infradead.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=JC3EeyOvWf7FtpVzWoJ1Ny8l+dOUVPRncQAFsx1SwlE=; b=g+mU5jfYneyFqx/lBedl8NIrj0TlhFUVzlitcr1TTnDyTVwIsnioCKwwXcpB5rpTpz JJQ5r6PbTNYEvMYGr3DvW20F2ixOW2UydTS6RU6lWSf2B5268pB4FE47zcWMix1XnCkb pC0trr8tVobtIOTCSleayj2VR/QYa4ldcDCl2KEVfUje16JsDOfIxxLqio9rDCIQoeXy 9ebiF1oQkjrrSqckffOOzj3ODH9FVgoASlIcLTOzpm5VWWXfPnlyS2aJkrGPveSvlmDQ TtX1SKu21baKL6vhkpTgKwEEQrqltchef/dan1s+kAb7ypT/R4j9Ve7NJPTFNoM7xLos E15Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965860; x=1737570660; 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=JC3EeyOvWf7FtpVzWoJ1Ny8l+dOUVPRncQAFsx1SwlE=; b=YcMafvin9Ik2Ue7GimIIZ6j/9O8V3Vh6BnrjPK7GRrBk+2v8jpBrrQtYXO+kmSTW8m KN5wUWPW2c31a/1kqKVdbNub6iYDoXxkyKC1JhjFOVNeIrTHxdRexXJx2Szz3zqGKx8W Jm61B2y+Qat1v8+Z6TrWUK2Xt7MOqQXM9wD1kzA/J7JFeMEM0PKRMcy81u+xYRNfPWLX zpocU3okMNJPWDW6JBNQ1SV8jZncDfgEIRzZ5bF/JCygHjnPc6aKc0b8iLEnOODOjIWJ TkPPMSRQgB4yQkYRMX64ZnKNEOZqREI1pT7u9XKw9Fwqa4f65KGO/TKiy0YpkysPn99b nFPg== X-Gm-Message-State: AOJu0YxESa4JiKQ5g8fr00pvhy1Tha1C+3znI+Or+qgvKDaGnYzUzTmE UN/s7sCJKXGITJHrfhNlyOY/mNm/Eypl2U4x748UJ3mic7RZctdDsvyeg5Oa+e0= X-Gm-Gg: ASbGncvtFG52sKmSk0pXHqAzQ5GRarp4+3tzsWsW+39IiY41TynOBeSRcKfE+odXggv BVJZUlHIdHM6kAAkCg4hHcwfsDJwtXpWm5L8zwlpOkXbRyFumpj/iej9pSQ4m+SWjLrKc096bKl TJuf1LKi2EmuXTeR9EqQ1SfKZdSS+ufLo8p7GvQ7Msz1qCc2gnWfgQy8iNZm52APdWgVrNavzGD bnMv1zFlfVcaJcdQj3mZUAHk0LxqE9nGppgM5V3aONbKuB4Y9G9wQJlW+iX3BxcWn0Tqw== X-Google-Smtp-Source: AGHT+IH3olozIYSo4r3r15dhiFKl3/3XHwJwjdJ6AtULTMFFycwztXRXf6ddeRVzYwbOviD3YM/ZOQ== X-Received: by 2002:a17:903:1107:b0:216:5db1:5dc1 with SMTP id d9443c01a7336-21bf0b76a05mr61009835ad.1.1736965859902; Wed, 15 Jan 2025 10:30:59 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.30.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:30:59 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:44 -0800 Subject: [PATCH v2 4/9] drivers/perf: riscv: Implement PMU event info function MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-4-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103100_488913_C088E479 X-CRM114-Status: GOOD ( 16.03 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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, }; union sbi_pmu_ctr_info { @@ -158,6 +159,14 @@ struct riscv_pmu_snapshot_data { u64 reserved[447]; }; +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 5d5b399b3e77..7db78c7a1524 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[PERF_COUNT_HW_CACHE_MAX] }, }; +static int pmu_sbi_check_event_info(void) +{ + int num_events = 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 = 0, count = 0; + struct sbiret ret; + + event_info_shmem = kcalloc(num_events, sizeof(*event_info_shmem), GFP_KERNEL); + if (!event_info_shmem) + return -ENOMEM; + + for (i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) + event_info_shmem[count++].event_idx = pmu_hw_event_map[i].event_idx; + + for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) { + for (int j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) { + for (int k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) + event_info_shmem[count++].event_idx = + pmu_cache_event_map[i][j][k].event_idx; + } + } + + base_addr = __pa(event_info_shmem); + if (IS_ENABLED(CONFIG_32BIT)) + ret = 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 = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, base_addr, 0, + count, 0, 0, 0); + if (ret.error) { + result = -EOPNOTSUPP; + goto free_mem; + } + + for (i = 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 = -ENOENT; + } + + count = ARRAY_SIZE(pmu_hw_event_map); + + for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) { + for (j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) { + for (k = 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 = -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_data *edata) static void pmu_sbi_check_std_events(struct work_struct *work) { + int ret; + + if (sbi_v3_available) { + ret = pmu_sbi_check_event_info(); + if (!ret) + return; + } + for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) pmu_sbi_check_event(&pmu_hw_event_map[i]); From patchwork Wed Jan 15 18:30:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940797 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DB5E5C02185 for ; Wed, 15 Jan 2025 18:36:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zMQ3gA9fC/LGZdgqvXJZxIYOAs0CVY1k6D9HMAOt3p8=; b=VKIsw35BeWeHiI CbCi988/QgAXEWcZBnZFJHlaVbXbVKEvPcs3tYq9GQ4NiYYRK4duPTVe3BpKpUo0yvuRUVHMMdvJi mHZEAkWJ7XZ/dwaAwS72G5TFOCiLUP3Arrq76ILXX0CdxjavV6wsq5um7IV5VXzetjqlABXNvsyZ0 CGIo36SioFAUUgY0+uy747JcLCZuKY4XKMsCDyXGK4ew6ql3226aXYtKVzdm5TklPQqWFpuVF+0K2 PKH3dTO/EdoLjXtVEYiZZIVHJ4mCfkI0HKJ32Q8rfVdWZuuLVJGq5I/dH0GR7O17lxvT+Yn6C8SAp s7KvVIhSQSSu+DkHiGoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8FT-0000000Cmnp-35we; Wed, 15 Jan 2025 18:36:15 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AP-0000000ClDb-3VZY for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:03 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-21619108a6bso126526705ad.3 for ; Wed, 15 Jan 2025 10:31:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965861; x=1737570661; darn=lists.infradead.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=tgS461j5peg/wBSc44BcdgWOlZZ4NL2WfdCTDE0Whhk=; b=GOMjSK5utRBybbCdQDAJYaKZ6qmhu+X21KByLk1YySkRjeofrUVqSj60PXigu0KJ/P qjuYmeLbGgzgLwTU2qJLuyB+56C5r4C39GbjOl9QSFDXE0xjln19ecFkA6NVliwVpO5w Dian1TVZj6Q3cGageWu4M+bzMW7g+UkSft4nsNNVOtmHixoDu1Ec22RB5lUhZEkgdXHM OCwgchjgLk1Kq6mho8nIe2z4gcqJluAdFZjiY12J75ojMcgJeor9dMAE32/2RAk+Hn4h qJlCZVjowSvZMxnrpIGck0mFaAbk/rvFlNIIspcm/qFTrV/wdGufMTVVvjkSEHJHYxTD zEzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965861; x=1737570661; 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=tgS461j5peg/wBSc44BcdgWOlZZ4NL2WfdCTDE0Whhk=; b=LuUOp4rBA4aZ2HryFYU0kGBQ0SwB9O8demTABxozfxKpeFiJA/jbFL+TKMoz3fI0i7 iPAkq10T/rIhtIVC3rFPr1Ce8S1x9epQgzj3bwx1AbpSkACj9ckIDeS5RAvnaDRaO3Py JOHx28cuTLFsLbTSVflnwdaRukPS35BC181sdpIKvI4QSU8oCS3rBttIxeU+ookOOpDj 6BeOvWpgkZvmcuZKHn+DMHRb3lt/QtTWdN1YISi48H6rF0RBOW1t5RjGU3gnWZcdVYKy g50F30/CiaQXcM4K5zmGWkzEyL/CMZjBrocXP7RbYy1jyBSCX5c+LQU4cGK4RHn5WwYQ I4cQ== X-Gm-Message-State: AOJu0Yxb15GmMG4T2hRgKWpxXK98YeH01ZbhSrbWaacef8ZuklVlTPrE 4HQ4OIIz72w1YoGnjrg6eXAN8O/XVedELjF1CBdKJ0ywbnI+4FVy+6Tni7pNp0I= X-Gm-Gg: ASbGnctRF/BHkyLHOaqY0pLXuuY1WXGFwPBjNROdZS6bhkcB8I5yWGbEp/3qaGB0bc7 d75TEnWd192C6VeBouh2M40XerWEw71UswPKW+rEAeLI54u4S8tg8qUktW8XMm37Ab5ahmrUJV0 kRTL7DW18y8uKGkgI7Cf1Z6HiaZUHhU5h4g6Zu4AXsfv4dokLaESyuM0OH0Aye5wKbT+MvKvXiy YXjON1Hmrk+eWjf24idQ1fB0xrt46EHStMmBvYckjYPL7RTjVbMxsl5cH9K6c31y7UO9Q== X-Google-Smtp-Source: AGHT+IG8TkzyWt94SBOJrOOqLMztl8kBVgG7Yj3MJPUrXXsfaCQL4prK7YB1Ze37aKg7tg/myTnREg== X-Received: by 2002:a17:903:2348:b0:215:5a53:edee with SMTP id d9443c01a7336-21a83f36f56mr441100595ad.9.1736965861248; Wed, 15 Jan 2025 10:31:01 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:31:00 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:45 -0800 Subject: [PATCH v2 5/9] drivers/perf: riscv: Export PMU event info function MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-5-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103101_926869_4C378643 X-CRM114-Status: GOOD ( 20.66 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 7db78c7a1524..f311daf63ece 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; +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 == SBI_PMU_CTR_TYPE_FW) ? true : false; } +int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig) +{ + int ret = -ENOENT; + + switch (type) { + case PERF_TYPE_HARDWARE: + if (config >= PERF_COUNT_HW_MAX) + return -EINVAL; + ret = pmu_hw_event_map[config].event_idx; + break; + case PERF_TYPE_HW_CACHE: + ret = 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 spec */ + if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { + if (econfig) + *econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK; + ret = RISCV_PMU_RAW_EVENT_V2_IDX; + } + /* Return error any bits [48-63] is set as it is not allowed by the spec */ + } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { + if (econfig) + *econfig = config & RISCV_PMU_RAW_EVENT_MASK; + ret = RISCV_PMU_RAW_EVENT_IDX; + } + break; + case 2: + ret = (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 = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT; + if (econfig) + *econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK; + break; + default: + break; + } + break; + default: + break; + } + + return ret; +} +EXPORT_SYMBOL(riscv_pmu_get_event_info); + /* * Returns the counter width of a programmable counter and number of hardware * 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 = event->attr.type; u64 config = event->attr.config; - int ret = -ENOENT; /* * 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); - switch (type) { - case PERF_TYPE_HARDWARE: - if (config >= PERF_COUNT_HW_MAX) - return -EINVAL; - ret = pmu_hw_event_map[event->attr.config].event_idx; - break; - case PERF_TYPE_HW_CACHE: - ret = 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 spec */ - if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) { - *econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK; - ret = RISCV_PMU_RAW_EVENT_V2_IDX; - } - /* Return error any bits [48-63] is set as it is not allowed by the spec */ - } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) { - *econfig = config & RISCV_PMU_RAW_EVENT_MASK; - ret = RISCV_PMU_RAW_EVENT_IDX; - } - break; - case 2: - ret = (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 = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT; - *econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK; - break; - default: - break; - } - break; - default: - break; - } - - return ret; + return riscv_pmu_get_event_info(type, config, econfig); } 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 +int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig); + #endif /* CONFIG_RISCV_PMU */ #endif /* _RISCV_PMU_H */ From patchwork Wed Jan 15 18:30:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940801 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5C3A2C02183 for ; Wed, 15 Jan 2025 18:40:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gfFAXAp8Co4RNoBJ8+aVCY0VNfPhHJKbOQBVTR5bu4I=; b=YuAQ1YXcqAF4UU h67g4G7hwBzVeNfWjnpEzY3rUvZTbu3ZF6mCzCAUlQ8rghLKHemSY5U/MlCTveq78KqBsWBPKO0jP x8cua7NZ4+gYUcKz/wz/HEVAj/YkHK9+/aMsvlPMlT7hrc2Vxlps5gd6la3RIA0ja4vqumT7jHgXD rnaOCS+plFea6T9m3ng2V7fdPOvvwoPWHsTrhts/7WaQ0CBP5gliUVZJk7xoLZc+orGAO5LK/f6Ll BxFF6WPEVP1C8tsssZRKqqgGTpdbsnYMkvRlJLIowcv/cio0MkinKNWhnk1JysZ6VMf8X+jcNuwXB deg/ogm6z5MBLcn2mOlA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8JD-0000000CnJc-0XM7; Wed, 15 Jan 2025 18:40:07 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AS-0000000ClH2-1VQV for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:06 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-21619108a6bso126527315ad.3 for ; Wed, 15 Jan 2025 10:31:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965864; x=1737570664; darn=lists.infradead.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=eGsO74qlfZsPi9kGTe6Mf3rItgKav5O5SJKk6N7d7+Y=; b=wNmOjhd1c8hCYRFvTLrna97YGhT9ZZAOG9Av9zRxwpiu6Unr3OYYvk7kgbGC+hAdp/ GrvmTNyKZXJbgr29wwUY0JRCC+aKkfTiRTVTJabJ5pJstVILrgiGLFmT8e1Z/rzFzisx 2ztUoReBM3w/siIa5m5aY2EGzQB5hjVb5W92S2MuenA32N+e//A2GVQOkVDxpQcjWS8u vH7ktemggKWeyprK0JOsznelI9C7DhhW28nHfqPlCQTuoAKTJEwymbiYeSQebgdaCCQI /ddR+T/xVjY3nYYgoPOUZRS/FvlajgYy689Mb1QX9pG5f0jjMS48b2hgk2f1lC0jpSEg E5Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965864; x=1737570664; 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=eGsO74qlfZsPi9kGTe6Mf3rItgKav5O5SJKk6N7d7+Y=; b=V8HQ/Hg6QqM4VbGbxyow2uqAlCx964+oJSCFz6CJE5yabkyGqkVEk6dde/EfyZ3aV5 18AaTxlgg0l6v5Vp+XF7uEhqtviXKOnqoebFhYM72dt2vokNsed0Wh+3xxE/XI5HR3Nt krKXNHY3EgyXWWQCJnqBVntpSv7zgUKjFGZFKtm8RcpDQ0v3MykO5b3ebqtrlHLho0N8 vbpt1EhANRw6Ffu34sbxwq/LBG/Z29RDmVgn1aCkEgQjmiJ+pG1IW660skvpzFhrf+KD c6s2k15t6dvwAtlPvuO+1ywWrAQeEI3KTPy+XB0uN5egqYi1+Y4gSr7SBqDHWZdndYmT +rEg== X-Gm-Message-State: AOJu0YzwZdw2cQPC5uLunhqJd5s6nThQddoIsWncLTG+WiAtYSzw0uar NY/VqdFq5zxhvQbGJ36TnmExr6BoLrTyHWC1SB8VrOkhMN1f7qlNlus2ncXuQ4E= X-Gm-Gg: ASbGncugUuROqqvKWnQGtz2Sn0wCV2SkfWUf+iZEwQq+EolcCSY5/rxONXj9DW1yHHp KdrreChUGseVttBUUW4mfLuKJEbtTinS8NdBJP6JlrTjwM58bvwKFB6jzwHkZ9NJBEFrYkOUh8C +eSc+Mj2FCfO29yLTBaGNIBG1HIYalgK8oU40IquyFCWcjdgYyYL3GLIDaKG2frwMz8qquY6mNr U8kNCl6dtDPYocPokBllrQUFAspZ+g1vbmGFWPY2HgMGKiWLzdKyVWy/sV0nwSkPmKkmQ== X-Google-Smtp-Source: AGHT+IEVuy0zAtOEk7axH9BqkY46Q1CUJK/CsgMJYOIS0qmHtHOLlyWCGQnQHM7bf0KJ01YfA5xjlQ== X-Received: by 2002:a17:902:cccb:b0:216:6769:9eca with SMTP id d9443c01a7336-21a83fd35bdmr452990815ad.37.1736965862343; Wed, 15 Jan 2025 10:31:02 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.31.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:31:02 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:46 -0800 Subject: [PATCH v2 6/9] KVM: Add a helper function to validate vcpu gpa range MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-6-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103104_414906_2E0AB322 X-CRM114-Status: GOOD ( 12.55 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 401439bb21e3..d999f80c7148 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1363,6 +1363,8 @@ static inline int kvm_vcpu_map_readonly(struct kvm_vcpu *vcpu, gpa_t gpa, 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, bool *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 *data, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..b81522add27e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3283,6 +3283,27 @@ int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, } EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest); +int kvm_vcpu_validate_gpa_range(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long len, + bool write_access) +{ + gfn_t gfn = gpa >> PAGE_SHIFT; + int seg; + int offset = offset_in_page(gpa); + bool writable = false; + unsigned long hva; + + while ((seg = next_segment(len, offset)) != 0) { + hva = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, &writable); + if (kvm_is_error_hva(hva) || (writable ^ write_access)) + return -EPERM; + offset = 0; + len -= 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) From patchwork Wed Jan 15 18:30:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940802 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9A21C02180 for ; Wed, 15 Jan 2025 18:40:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ZvbxxmkUW7ORm/WMUoNPUopyazfa4pjPPrBocH8VN/U=; b=cQSwqeWe2thDQj Sti/a/Z0bTHTU73NCY0PUiWxGukPOX5u6FfYKVEj0yoqzfXoI3Hf6xf66laXyQwnnFgUiJTl7BmnM C435KW5NAa7Zezf4LQg+8Rw+0StyqhFKPP8h8WK4nJJ/W+o19brl4+vseurP96itE7hTACen3xFwq +G5hBu21cH67pYBNu68oxsmS9gEXZBlQmipNunCW/7bMwM8mxs1J0rGWWI8mx8BSNEgPZQ3ofQaDZ 793tt5yO4mNYkwKj8M2q0hpzKPinq/cSzBSKuuUuRioSNxcE8890er6+bvRktnXvkZK9NMyD/Tpj5 TdN8hwRGs7FkGX1y5pSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8JC-0000000CnIt-0ree; Wed, 15 Jan 2025 18:40:06 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AS-0000000ClGC-0Dlv for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:05 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-21669fd5c7cso131362775ad.3 for ; Wed, 15 Jan 2025 10:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965863; x=1737570663; darn=lists.infradead.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=YPQonIfRWz8q533cgyWsgSdgY3t/tGTd1rJqUG9L59Y=; b=Fsm/ZpOAS5KuspMftZT5V0A9ai3TZIsKl6kjutZ3y3JLZEilW+kAGCUWojzXvbr3It 6KsGdSfWUJmTHfqm/yQi8Dn/GtzKOg0j6B4NNWmLCrDTp0b6j+OZrmw20Hhvqj0VLvUh 0SflRrZTgQNb1JYngWYKhXMf9VVdZ+OLzDHLyUrYKv3hsaMw48iLFhuKpiQG68eat/Wd KLARQZyxgTD/ZXSBkKmScihH4kHD8cmka/DHa0B8ohl6WvBidfcFZL+eI4kB2gK0LyLk bW8LHq8BwONK0gW96OxKj6lEbgOuvlufpseS2Rchq9vWFpfTvhlGohX8nWTEKUidPmNg Briw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965863; x=1737570663; 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=YPQonIfRWz8q533cgyWsgSdgY3t/tGTd1rJqUG9L59Y=; b=ubaaG1oXCNV1FAzRs4DoYy635Wbs6nJM0urERFu87lgSSsqzejHk10b+AY/zZgNkB0 JsGLRCrNSklyDzHPqSbo5YkNRB87Mm2mSfpqsygOfSPuhgela+d8EYsz20U4hs8aTyC9 yMSi/sz7DiyJ+F8pVWFa7snkqOKjgDy8EtGRywISbHtms2LmqjBOu39tqqdg7DGbX0iT TpESVPPwCaHpab4dGUpBmOUz438tf18MC3Mhack8qp9GqaHwdJ9/zhSNRGPnpMa6NPmh qFp13MoKR02TB3Gmw2la1rUCn0WuSDWAQL+kIsl8zc/qbaAAIsHUbrs/xw5F+IsCfOfQ HLEw== X-Gm-Message-State: AOJu0YzM/+cu07nxgvMAZpp80+JShLZTUMmul96orCFZBL+wxsjcBUPf hf1cUbhEL0XS6gWZXs5S1KaBpzeX4wOA72fk/yW5xSu4lZmnokhyl1yb7uOWsAc= X-Gm-Gg: ASbGncvFDMiQCIwg4W/Mo57djG0O2NPpvEcIdvMeF97wn2NiW9JwS940y44pn/bXSLt o4gP2OWzIzlwtvhHj+fwZ0PLR0nZ+mxYRBgRwNR3Age8LSWEuQ1WIHlaG8fkhMfT18v+PI8jWRn 2D7+LX58eOLGPqPNRHQd5J8PhxahL1inM4Ucf7C1QXNLAsjrqaZeoVYN3rWlh4MMbPli7QsO6N5 ARXo0GppNUagVVqWaep8tWfozCmxKEhoUkJuzlb31hyO1EMPunjpL5rXjvQ4gRkV7qRoQ== X-Google-Smtp-Source: AGHT+IHrlKrKdeucl7zMxkF/5l3NHVR4wNF51oJC9P55Fo2f9FtCoIn3BV0XWsDAU93AJPqz/FttjQ== X-Received: by 2002:a17:902:ebc4:b0:21b:d105:26b9 with SMTP id d9443c01a7336-21bd10529e9mr125689755ad.16.1736965863474; Wed, 15 Jan 2025 10:31:03 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.31.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:31:03 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:47 -0800 Subject: [PATCH v2 7/9] RISC-V: KVM: Use the new gpa range validate helper function MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-7-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103104_123214_EE778D11 X-CRM114-Status: GOOD ( 11.63 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Remove the duplicate code and use the new helper function to validate the shared memory gpa address. Signed-off-by: Atish Patra --- 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 efd66835c2b8..ca23427edfaa 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_vcpu *vcpu, unsigned long s int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data); int sbiret = 0; gpa_t saddr; - unsigned long hva; - bool writable; if (!kvpmu || flags) { sbiret = SBI_ERR_INVALID_PARAM; @@ -432,8 +430,7 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s goto out; } - hva = 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 = 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_vcpu *vcpu) unsigned long shmem_phys_hi = cp->a1; u32 flags = cp->a2; struct sbi_sta_struct zero_sta = {0}; - unsigned long hva; - bool writable; gpa_t shmem; int ret; @@ -111,8 +109,8 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu) return SBI_ERR_INVALID_ADDRESS; } - hva = 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; ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta)); From patchwork Wed Jan 15 18:30:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940803 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 50CCBC02183 for ; Wed, 15 Jan 2025 18:41:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=G2df07Gp20Wb5mbkZY+zqY4ABsy+o2G/Am8s8B+avj4=; b=nQIKlJ3s5wSVF4 1xkpBW68RRRjgLKtCCLFUsJhgEX2AOKQb1k4wHexDpCN5+KkO6T0gdzI5Z23oJ0OS4axHQ5SiFYJx 74ww7c2VYpA7kt8Z46nzqQwb0jxFwuOzaaMLwZDA5458HTSrUEC2HhKfpozz+WYGFReiKjPR3K3Nh 0xuDmI1UZ7DWZ4rDtAeqpMm0etcmpqBSVzXpcfSPHDbQDW4qe8YorPO7TvNJj1lQfW+j+2jP3Objc tMlb8hPOcLiJ/U17MCznyazm1tv368Y1ydHGeQ32XwkyKT9VSht3yavQdKR1rQbPruCiA2kfirrJG a3Ym/FYIdNMGrd7a4ZmA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8KS-0000000Cnka-3Hvy; Wed, 15 Jan 2025 18:41:24 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AT-0000000ClI7-0SsJ for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:06 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-21649a7bcdcso123382475ad.1 for ; Wed, 15 Jan 2025 10:31:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965864; x=1737570664; darn=lists.infradead.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=BvcM8pFAUo8mVZOX2xDK8xE/vCf4z0GzB3iMLovGRHA=; b=M9W09/dKxI6LTQas8wJ64Ut2YHTk7Lf4hfud8ORhJV/3znMghwTMSKmCS2zEzfJlWY uYc9J5fBYTtkXeE8Ht/d696k4Rrp9lazC+H3qleknHJjvSygPy3/oM0v6xIjSA5ZzDAi tQPXG+A/z+cvU2Yg8t0fnfxK42cWb6ildgeMId4+mCmxTl5am+PiFEcK3IdJ5n1eQLRN YwIJaVZHP458ilWG73D9xDbDaeqmxnDcfGCSmvSVpLQJkcJhNr8KXQljy4TfbSmA03Co tbQThIfcHafI1rhsqskmJv1fiZqEdkxGWBQsEJ8SmOAemqXju7vKoT+800gP+qRfizHU cu5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965864; x=1737570664; 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=BvcM8pFAUo8mVZOX2xDK8xE/vCf4z0GzB3iMLovGRHA=; b=DTeyuRdDwyuyfdzO4hRpagtQsatvIxrQctg35PNVqEzENdH0T48rxErgyNF83tv8/x UpxA3/gWoBZfbGz9pzLvql9JAcCdmhsx7DsyoiiMIcddecOKiei0JmmVQwBxZFV1ev5O RawD24Nc376t4AzmA5GGen1ArlLoo7iS4r9YXe1hvi6eD4WKbOH2O8lMzZ7Vke7eMv2i lH6BRqdnjavAB+ubZI9RKeMPI5ofNQJ8gpYlY2mklOk2KGcml8gqisF6GSeeEroZhFhh P2LVLFWhOvT8Y1xDnN4jthOXLDEJoQf+3kjGu1S9GVGLG9JXD4XELSlEoi8aOmJHhgr9 0Frg== X-Gm-Message-State: AOJu0Yxdvh1i6X1qpMyVl/6qRPj6+pQS5ibSijSaNrSBg186DGyTN2Ej L0n2aIPl3VgWLxgzjE0GZ4yE2lFOasUPV7b0ioOETu9THvWKwQV44coPg7S78BQ= X-Gm-Gg: ASbGnct9a+4tWiFIret+k/PTun88VIRbnwlfjn08E6Ocwyba4DrtYDRDLmWDvaUBTro rzhtR8cgWDsOA0yUJVqNFHfSnVIxhgTcnstlKIL/bBZXoZK/frpqOSwrCU7NdwbRvQkBWsg4A41 XYSEmsPrdYiCqFgUDwTXCmUjIFLjfXwwUGsjwpVrqUIwPqBW07ZRkbB8WBD2IzU64SpjTSHu0/D ioOjc66NwTty7IFYGJvkadDsuRWs1l4iITm0DBdRRid2HpGyiqs7gJmZ6xP9GSYSnXS1g== X-Google-Smtp-Source: AGHT+IGGezhqE5JLeDYHWhClhyZY1cGsFLOXrLsdEJgU2ElrVE/deMFlurFTVr/XKSOcI7Wz9ZYvgg== X-Received: by 2002:a17:902:e5ce:b0:216:386e:dd8 with SMTP id d9443c01a7336-21a83f54a51mr373139315ad.17.1736965864561; Wed, 15 Jan 2025 10:31:04 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.31.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:31:04 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:48 -0800 Subject: [PATCH v2 8/9] RISC-V: KVM: Implement get event info function MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-8-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103105_200584_7B70C13B X-CRM114-Status: GOOD ( 15.02 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 saddr_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); diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index ca23427edfaa..23dedf4c9313 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_vcpu *vcpu, unsigned long s return 0; } +int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_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 = num_events * sizeof(*einfo); + gpa_t shmem; + u32 eidx, etype; + u64 econfig; + int ret; + + if (flags != 0 || (saddr_low & (SZ_16 - 1))) { + ret = SBI_ERR_INVALID_PARAM; + goto out; + } + + shmem = saddr_low; + if (saddr_high != 0) { + if (IS_ENABLED(CONFIG_32BIT)) { + shmem |= ((gpa_t)saddr_high << 32); + } else { + ret = SBI_ERR_INVALID_ADDRESS; + goto out; + } + } + + if (kvm_vcpu_validate_gpa_range(vcpu, shmem, shmem_size, true)) { + ret = SBI_ERR_INVALID_ADDRESS; + goto out; + } + + einfo = kzalloc(shmem_size, GFP_KERNEL); + if (!einfo) + return -ENOMEM; + + ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret = SBI_ERR_FAILURE; + goto free_mem; + } + + for (int i = 0; i < num_events; i++) { + eidx = einfo[i].event_idx; + etype = kvm_pmu_get_perf_event_type(eidx); + econfig = kvm_pmu_get_perf_event_config(eidx, einfo[i].event_data); + ret = riscv_pmu_get_event_info(etype, econfig, NULL); + if (ret > 0) + einfo[i].output = 1; + else + einfo[i].output = 0; + } + + kvm_vcpu_write_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret = SBI_ERR_FAILURE; + goto free_mem; + } + +free_mem: + kfree(einfo); +out: + retdata->err_val = 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 = kvm_riscv_vcpu_pmu_snapshot_set_shmem(vcpu, cp->a0, cp->a1, cp->a2, retdata); break; + case SBI_EXT_PMU_EVENT_GET_INFO: + ret = kvm_riscv_vcpu_pmu_event_info(vcpu, cp->a0, cp->a1, cp->a2, cp->a3, retdata); + break; default: retdata->err_val = SBI_ERR_NOT_SUPPORTED; } From patchwork Wed Jan 15 18:30:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13940804 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F3C76C02185 for ; Wed, 15 Jan 2025 18:44:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=kPUioz98ZhrPRJqf/6oKybXZRTa0hkcYNOiKEaOXd0Q=; b=x7NNaqw+ZUMh1s 9rr7LfiMVRG0NmUrsZLAYiF0U0aPWJncylAqJPSrtkixyfYjRgcg9Bb9IN6Bho38k+OGWuIH+in9J ql590oxEFyZusluVPxIGXIfAdMX2oS2Y68j7jJ322QN71OCVM03eQJf3Q6kV/ICmgGLqcYKqIDTc3 44Ku55jk4hQ6fA6CAWtPPfd4NthJEIuqkFASn1AJROep3K9p3VHoWPVnHSBWs5vW8BiD0bJpqOdpr eZFYR/WZJPE226UI5na76wVPtfhnSazR8/ywbPM/UyycKRJVlBd3Vd3F56y5V6vCLI4bf50M6HRNu 6MYOc8VhLFLESfJwlUng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tY8My-0000000CoCW-3iFb; Wed, 15 Jan 2025 18:44:00 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tY8AU-0000000ClJP-2mWJ for linux-riscv@lists.infradead.org; Wed, 15 Jan 2025 18:31:07 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2165cb60719so129037985ad.0 for ; Wed, 15 Jan 2025 10:31:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736965866; x=1737570666; darn=lists.infradead.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=oS1moOcp9ct9rXCWQSjf/GhpQtGR0rWY3wjXVfG0zao=; b=eU2yNMxKD/j807Dtxo+AL6aNF+LSTXcCNsvZY9rlKoTReyGyQxJo7jvogOmA3cKsTy UtMSOUn4oRGqDMAHZGSdB8vLiQWdohAZ1iTsLlnEBAwf/0kBcTsM2ST79Sm8ZYLouaUO HgBobRkUGgKbT3KbrsuPnwGFradvZFc+tQh/B5iCkzKzq5wy4ZJQtI29G8BR9Staf7Si ZN5nzlL9kaztnzODPdq/4dEKUzGd0Cm8jhcTbppSSCKR1UyidIVSrsgJM69li45q/lcv 5KDdbynu5pVaQgev1+MC0Z/t6UG8hhyuqkQLY4pGzjeShjZNNjVFlL17RKObFPEgxn0a 4ZKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736965866; x=1737570666; 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=oS1moOcp9ct9rXCWQSjf/GhpQtGR0rWY3wjXVfG0zao=; b=Mwb1ScXi0fEvcVqk2S80WzculDXh/0b7Hgv6dD8ttXn+0cqsPUpddnQxbbhGwC7Lvj PiJywS9EF7KnGWRwfyR0w/zQfb+PBW0j2fQE8PkuMJ1uLdSLNgCjI5EAO/XR46dMVp54 b+Ub/iddDYfXn4Ff1EOxq6Vls+hReKjtF1xtIsJfCH5ccJPpNUD7G8K+jhr0xn3OEqxR jBc+4TpuNBhM5QAvdt6dy0NSXE7YJOA1jvIrv3xj249j1aSXcdERBrB4knXLA3Uik0eu GYgsf2SeccJUYZ+dVAaUoi0XppKB7qRiR+zQq5QJWYvglExDESg+DVu6ytD+g9301r9y 8tiQ== X-Gm-Message-State: AOJu0Yzp3pXaBkXoTNhZx5c+FZDEVceIyvdG04uhJOZrP6x20WxO6EyG exdHb1laj15UNB5pldRCRAapx2j2g7zJdBpbP9llWFy/pXVlcla5hG6UBWJQZ83ILHEblGfm9CR E X-Gm-Gg: ASbGncvuyqsxAkKlkwrhH7kt7UDDjHWdY+9jiGvED30JD+wiJU5PaeGDXekhISIZixU CCMvszhk2KDeUUbCFG2H+iwuS2eOqLxxdIgB6Ep+o85XZHO5qnbvWydyrMGT6qaN4jjoTX+MOHX KHEB5xWkmtsVXFoBYkmN3GBuHX/dQwlwMiMiFzyVDGlwybdwAxpTTG1ew7O8X0DNPg2ynF9i2M9 JVoTOD/MutFo75GoSP2WaDul8dJVVk1zYqDEeydRYG1XRkIVFCtdSsbtwcwukcw8IfzVg== X-Google-Smtp-Source: AGHT+IGUgwpGj/plYJTZ8pJ5gD95k5t13F6GWcCCeHibqJhYXgtv9S5eRA0RKAcOGIqFSWIv1IyJjA== X-Received: by 2002:a17:902:fc8f:b0:215:787a:f91a with SMTP id d9443c01a7336-21a83fd1e96mr525986085ad.44.1736965865659; Wed, 15 Jan 2025 10:31:05 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f219f0dsm85333195ad.139.2025.01.15.10.31.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 10:31:05 -0800 (PST) From: Atish Patra Date: Wed, 15 Jan 2025 10:30:49 -0800 Subject: [PATCH v2 9/9] RISC-V: KVM: Upgrade the supported SBI version to 3.0 MIME-Version: 1.0 Message-Id: <20250115-pmu_event_info-v2-9-84815b70383b@rivosinc.com> References: <20250115-pmu_event_info-v2-0-84815b70383b@rivosinc.com> In-Reply-To: <20250115-pmu_event_info-v2-0-84815b70383b@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-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250115_103106_704867_01EC14CF X-CRM114-Status: UNSURE ( 9.88 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Upgrade the SBI version to v3.0 so that corresponding features can be enabled in the guest. Signed-off-by: Atish Patra --- 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 b96705258cf9..239457b864d7 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -11,7 +11,7 @@ #define KVM_SBI_IMPID 3 -#define KVM_SBI_VERSION_MAJOR 2 +#define KVM_SBI_VERSION_MAJOR 3 #define KVM_SBI_VERSION_MINOR 0 enum kvm_riscv_sbi_ext_status {