From patchwork Thu May 27 00:30:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1484376 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=IfQegPTv; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=Hq5aioTp; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fr7zZ6dG2z9s5R for ; Thu, 27 May 2021 10:31:38 +1000 (AEST) 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=EQrrBo8dd1nUbigYBA41Tooo7umKq5Ujp6pzHQICJF4=; b=IfQegPTvwlSLbS 5vEp7MDDMSkW+GgT1HgCEbO74Im12R3h32UKSj+T/yx3ycgc4tny5SzIVlKpmslmX2p/3oh0iMwq6 YyWL8iPqUSsyOqPubKlkY5roxWM3i7YlzfwLl1bzDWcm/hBMExJFEn9dbz7WJIL7wLHVH92Pl3+3J Ho9HKDWNxSuxTnGkNlKsPTn+tpK4SdZqg0Y4SRH98D27usm1v30mbPduMLzOKZxuCUbmuKjqrtc4V xidN/92D7qlO7bkdSVZSv+8jx/uAOJCRRRyDsemzn5EBTIJRxMA+iXAWjX9qzsY3fM4/wrbT6M1Kv fOSitGFCldqAxD4g06+A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lm3vy-001AjS-Lg; Thu, 27 May 2021 00:31:34 +0000 Received: from esa2.hgst.iphmx.com ([68.232.143.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lm3vv-001AYD-BE for opensbi@lists.infradead.org; Thu, 27 May 2021 00:31:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1622075495; x=1653611495; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v8Jw1QzcRn472NaFgToLcloUxkZ+VDRKBDs7QAUWAbw=; b=Hq5aioTpY7XlyN4KV53hlGHPgRlJwaRZ3MrqEHIfxF6phhA59Zjywx9I ZqocdoTMuTSl8Dj1qlt/KEmz8EZ1xeuqcZNGEfODLXZwqHZr1c7l4/UmZ /A4o/TKXlQ5uuFMf3qWmNaG2sx0dtWFSrGuGzneE46o+f01UJkvCb5O9K BAiOOClpMNjXEEKle7O87oKEzz1VN+5BkcCjRCdKm2P70SWz18oIDPaEF 246HPHcSi3Sfd4ug6r75mgunqRcRw5eLFUG3n1lSDt1Y1isReZVdgJ5Kw qo6ef6ZomfLLc7Ftsk3edIr6cSKtCd88CWH8Re8451Zhi9kbpDgeJIwsq A==; IronPort-SDR: ALqjwwSisuvEozEDMxTC6MfD1xFcn3uVwohns1DkNiGCHSbjiMNg9LdOFAY0HHwE6R85RWrP1Z Urp0bdqyfD75y/cGy1ZAFMDoOAUOhPjV2dgkOTD5WYHemblha9vaEsTaOB7c/rLoCtRQS+Ba8E Yq35D4t7SGcJXkO/oLfr2loXx2Hf+IkrcNjCSEOnRL9tmZKX8zHhIAWdVUIM7rv+OvKTvEcFaj CEJelt1lbafigWT+HEh1OBBt5HzVwI4ZR/4weg/ZNMm7Hz/KW75zg7BtCslFsEmpd81/oI4GNA Sf0= X-IronPort-AV: E=Sophos;i="5.82,333,1613404800"; d="scan'208";a="273477841" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 27 May 2021 08:31:35 +0800 IronPort-SDR: pkzYgskeApbnbvkWS2SlrqJb1oqUny331XuUws/+5PMLatThwGbY7DJqC2O6o0o7Cer8uUqNrH KArMqZm8bct/udCgkKNVQbsIkJEA7ffXg9zIRYvG54Hmtvr8xT6VLscY0wT7X2bxCM+QQlfYhV rr8gzHR/S1/hG1MXK3kxuaYaClwcCTGporv8UMugbQRqV1AiU3HEDmNtbjU7fbmNLP64CB9I5r P/3qfzDs/EbGLXJBCk15xgvaUu1PKuIYPYODHOAgmrq467V1wVCV1z66qLSjfBIaT2tou8m7xA IwlChslg2OrS+YJd6Hryazdg Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2021 17:10:56 -0700 IronPort-SDR: iZ+TBrWtdtS04JXpJ9+b6EAYf33tTLz1qQyEFjTm7FUerEaBersDacN/9zT4VD4BrJ6AJJGHhT gpxLK470So4U8gpFSxwNkJIydymI+0OKche49RyfsvHjvVKoFBinMUaV8ze5EAVJOK34xTY7x8 /KX8y2HMy8IOob8w8Cv6x9eesfF7d5eeINKfd32boLKDluhWWTLxVSutJbw9ItXHMETQlqM7TV SKJQjjkyggbzPQtir5fthTTaYlv4ul0bGGhAs33sqZeMcQwH+qaNAP2rdEoRnXhLU+B8AZCrMK WQc= WDCIronportException: Internal Received: from unknown (HELO jedi-01.wdc.com) ([10.225.163.91]) by uls-op-cesaip02.wdc.com with ESMTP; 26 May 2021 17:31:29 -0700 From: Atish Patra To: opensbi@lists.infradead.org Cc: Atish Patra , anup.patel@wdc.com Subject: [PATCH v2 12/15] lib: sbi: Implement SBI PMU extension Date: Wed, 26 May 2021 17:30:41 -0700 Message-Id: <20210527003044.889681-13-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210527003044.889681-1-atish.patra@wdc.com> References: <20210527003044.889681-1-atish.patra@wdc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210526_173131_545910_0304E159 X-CRM114-Status: GOOD ( 19.00 ) X-Spam-Score: -2.5 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: RISC-V SBI specfication 0.3 defines a PMU extension that allows supervisor mode to start/stop/configure pmu related events. This patch implements all of the functionality defined in the specification. Content analysis details: (-2.5 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [68.232.143.124 listed in list.dnswl.org] 0.0 SPF_NONE SPF: sender does not publish an SPF Record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org RISC-V SBI specfication 0.3 defines a PMU extension that allows supervisor mode to start/stop/configure pmu related events. This patch implements all of the functionality defined in the specification. Signed-off-by: Atish Patra --- include/sbi/sbi_ecall.h | 1 + lib/sbi/objects.mk | 1 + lib/sbi/sbi_ecall.c | 9 +++++ lib/sbi/sbi_ecall_pmu.c | 78 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 lib/sbi/sbi_ecall_pmu.c diff --git a/include/sbi/sbi_ecall.h b/include/sbi/sbi_ecall.h index 63ef866ba2d4..9500d0430429 100644 --- a/include/sbi/sbi_ecall.h +++ b/include/sbi/sbi_ecall.h @@ -39,6 +39,7 @@ extern struct sbi_ecall_extension ecall_ipi; extern struct sbi_ecall_extension ecall_vendor; extern struct sbi_ecall_extension ecall_hsm; extern struct sbi_ecall_extension ecall_srst; +extern struct sbi_ecall_extension ecall_pmu; u16 sbi_ecall_version_major(void); diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index d9068b707854..7096f245ae0c 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -20,6 +20,7 @@ libsbi-objs-y += sbi_ecall.o libsbi-objs-y += sbi_ecall_base.o libsbi-objs-y += sbi_ecall_hsm.o libsbi-objs-y += sbi_ecall_legacy.o +libsbi-objs-y += sbi_ecall_pmu.o libsbi-objs-y += sbi_ecall_replace.o libsbi-objs-y += sbi_ecall_vendor.o libsbi-objs-y += sbi_emulate_csr.o diff --git a/lib/sbi/sbi_ecall.c b/lib/sbi/sbi_ecall.c index e92a53930460..a87bb7ad1c8d 100644 --- a/lib/sbi/sbi_ecall.c +++ b/lib/sbi/sbi_ecall.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include u16 sbi_ecall_version_major(void) @@ -144,6 +146,7 @@ int sbi_ecall_handler(struct sbi_trap_regs *regs) int sbi_ecall_init(void) { int ret; + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); /* The order of below registrations is performance optimized */ ret = sbi_ecall_register_extension(&ecall_time); @@ -164,6 +167,12 @@ int sbi_ecall_init(void) ret = sbi_ecall_register_extension(&ecall_srst); if (ret) return ret; + /* SBI PMU extension is useless without mcount inhibit features */ + if (sbi_hart_has_feature(scratch, SBI_HART_HAS_MCOUNTINHIBIT)) { + ret = sbi_ecall_register_extension(&ecall_pmu); + if (ret) + return ret; + } ret = sbi_ecall_register_extension(&ecall_legacy); if (ret) return ret; diff --git a/lib/sbi/sbi_ecall_pmu.c b/lib/sbi/sbi_ecall_pmu.c new file mode 100644 index 000000000000..938bf31b907e --- /dev/null +++ b/lib/sbi/sbi_ecall_pmu.c @@ -0,0 +1,78 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid, + const struct sbi_trap_regs *regs, + unsigned long *out_val, + struct sbi_trap_info *out_trap) +{ + int ret = 0; + uint64_t temp; + + switch (funcid) { + case SBI_EXT_PMU_NUM_COUNTERS: + ret = sbi_pmu_num_ctr(); + if (ret >= 0) { + *out_val = ret; + ret = 0; + } + break; + case SBI_EXT_PMU_COUNTER_GET_INFO: + ret = sbi_pmu_ctr_get_info(regs->a0, out_val); + break; + case SBI_EXT_PMU_COUNTER_CFG_MATCH: +#if __riscv_xlen == 32 + temp = ((uint64_t)regs->a5 << 32) | regs->a4; +#else + temp = regs->a4; +#endif + ret = sbi_pmu_ctr_cfg_match(regs->a0, regs->a1, regs->a2, + regs->a3, temp); + if (ret >= 0) { + *out_val = ret; + ret = 0; + } + + break; + case SBI_EXT_PMU_COUNTER_FW_READ: + ret = sbi_pmu_ctr_read(regs->a0, out_val); + break; + case SBI_EXT_PMU_COUNTER_START: + +#if __riscv_xlen == 32 + temp = ((uint64_t)regs->a4 << 32) | regs->a3; +#else + temp = regs->a3; +#endif + ret = sbi_pmu_ctr_start(regs->a0, regs->a1, regs->a2, temp); + break; + case SBI_EXT_PMU_COUNTER_STOP: + ret = sbi_pmu_ctr_stop(regs->a0, regs->a1, regs->a2); + break; + default: + ret = SBI_ENOTSUPP; + }; + + return ret; +} + +struct sbi_ecall_extension ecall_pmu = { + .extid_start = SBI_EXT_PMU, + .extid_end = SBI_EXT_PMU, + .handle = sbi_ecall_pmu_handler, +};