From nobody Fri Oct 3 15:35:54 2025 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.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 F25823218DE for ; Fri, 29 Aug 2025 14:41:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756478475; cv=none; b=NMccmjrFHUdVQQMddYM3mA6ppSU2cZbnPTbZPDZRk9kNp4Xxu52SDAnhkOPXSC8hX+HgZBM2VEDSwTvgX6BGCFPEwlny3cTQ0i3M76BZZrmce63tI8Xa/Ug5taBLjychEQ+L850V1PLnF9mbYxnA9VQ4+vnFwYWYNgdruYFQSVs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756478475; c=relaxed/simple; bh=k+LqgjXzzLmGK6Fr1/7D9cO9WxsdSZi3fAFA+heh200=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IiOPa5PxV4koomHTE2zcvgMkicjXKOaoePoYaxDY1N+PP+y8bdfw5tPobxtHDmobEJvf8ObxyUDtVU7c56C9di85SHSm2oM+/fdqQhYfAghGigBP8ngPOJOLj9ze35phDfXhRb/p+Dhw/TJHaU8bjSvlXo17IvjJczVFs5B3CQ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc.com header.i=@rivosinc.com header.b=OC8PKl0g; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (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 header.i=@rivosinc.com header.b="OC8PKl0g" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-77238a3101fso129934b3a.0 for ; Fri, 29 Aug 2025 07:41:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc.com; s=google; t=1756478473; x=1757083273; 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=DuhopmvMJD6AmI9ErsWAbRzYROuI1OFJgLKz4muMCjk=; b=OC8PKl0gBAj++yaGLyQ3jKNZvlby8fI1wznHsN2Eok8nXOal0/+jlGAR1q5g2seeE+ 2cZUYmgqUSxjNkbA5P5mlCNpBv/R2nFlTvjPyHymErEPheeGgZuQFTEf8UWlgmZz9TqW 4c1rGwqbqgFVDZdORjMobmu54uYNezpb1Ob4X5/p2/pEyevq5kmjxJYTWbmqGDkxrJtI BynS/lerBqap5sG+Rc04V29M0AwD0Xo9DuSLY8sNAnIe0e4BSzYnEs36+tSBAG98cn7f RwdClGLhc/4LLIj7d+MkZLAox21M/TCcdafF7RmeDlUOdUSGU6ceuhjqWsQDa2tQVHZg Z0Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756478473; x=1757083273; 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=DuhopmvMJD6AmI9ErsWAbRzYROuI1OFJgLKz4muMCjk=; b=pKCVrEQ5jb73KhhNKQ7zbG+/mzjzIxjk4I2GE5UAgfIh8iKMS3zAMUzYi/VMkqpzDJ 1DaojLE2g5F36c4AY/fPCHyjwIBfss2IG7SzHn3jFHhvzYhCAjOpP2/4J/JLio7VX3kJ ee+/JTMWsguoxq2JeMfBDxS8nwyvr3mwwLi2kRmWsFlLdCxVIr1H9+i7Wmbm/4SKPUIZ JvMEEJeTLBQOmh8/wY8Lcp7OdViC/Cf69Me+bPNUs66b9iSYavQkf5+KSQYci1SmZO/g 1p4hLmDT6Vrvbk5kBwSGAV6uhXDKFoxZli1BoL9Ai8Dv+fStKBr0ttpJ8xZNqQ2c9Swz tKxA== X-Forwarded-Encrypted: i=1; AJvYcCUvpFbHdJxjvDt+V1GZ8VYjfECW+jjK7dTQN4Xcgef+Ifou9vlbUAt+1pCSuJavmJ8iVBi+5/XRtDPuxmg=@vger.kernel.org X-Gm-Message-State: AOJu0Yw4iBQ3+ByVySY2q32eeRlkO7+hE5dXaQmnoY4rU2sMKlonxvDm yuMf4EqvBGRhYssqAJuQxJGwWz5wbugmDO1EA7mDV4+FKQy1H2LlLHiLm99KwCHMcLM= X-Gm-Gg: ASbGncsx5EJfMNh7+Z+yl97MZqgsU5Lqt8tG6GEdJZp3E9jku0y9AtzsJWAy5dVE9K6 mBgJIA0BlF6kRrJvbR92cAqfaLkizTdaxIjbOdKO/Nq0nv7sbqRoCTRCjQJBJPvpatBpqp6StIK W7gg5F83hZpfLNxlNKJ6smGoGXccL7JlVXYlO0m6aeD/pNoFvz55qhoeKfwz/dFLYx42jbIhAd6 IaziZVT5cDyRJTa1Cgidyp0GSinxr6Y/dATWCmdn51QcaZuH/ZxovrBmG1PaVKIVHJBWTetIOv1 RwrmKmaH9MTqxegYOuJrQ4m6rd7b9PfzEfZn76H5sYTkuKRhDxwSxvIEbYiiILxq1tW0ZpYv6Iy 1KNYIYvCKLcjL9TdQ0+l/pxXbUYS5jfp6cdMVEDckNJALQw== X-Google-Smtp-Source: AGHT+IFKu+50yqqSOgpeLJyOFV4rCaLFVpp2RVi5iZb8Nuz83CnzQCACwKb8OL/eXwNpYfHYvSwbXg== X-Received: by 2002:a05:6a00:4611:b0:772:3676:64d with SMTP id d2e1a72fcca58-77236761dcemr1223973b3a.27.1756478473332; Fri, 29 Aug 2025 07:41:13 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4e1f86sm2560999b3a.72.2025.08.29.07.41.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Aug 2025 07:41:12 -0700 (PDT) From: Atish Patra Date: Fri, 29 Aug 2025 07:41:06 -0700 Subject: [PATCH v5 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: <20250829-pmu_event_info-v5-5-9dca26139a33@rivosinc.com> References: <20250829-pmu_event_info-v5-0-9dca26139a33@rivosinc.com> In-Reply-To: <20250829-pmu_event_info-v5-0-9dca26139a33@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, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-50721 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 Reviewed-by: Anup Patel --- drivers/perf/riscv_pmu_sbi.c | 124 ++++++++++++++++++++++---------------= ---- include/linux/perf/riscv_pmu.h | 1 + 2 files changed, 68 insertions(+), 57 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 0800ade1d0d5..0392900d828e 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 { @@ -412,6 +413,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 v2.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 @@ -577,7 +643,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 @@ -585,62 +650,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 v2.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 if any bits [56-63] is set as it is not allowed by the= spec */ - 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 if any bits [48-63] is set as it is not allowed by the= spec */ - } 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..f82a28040594 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -89,6 +89,7 @@ static inline void riscv_pmu_legacy_skip_init(void) {}; struct riscv_pmu *riscv_pmu_alloc(void); #ifdef CONFIG_RISCV_PMU_SBI int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr); +int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig); #endif =20 #endif /* CONFIG_RISCV_PMU */ --=20 2.43.0