From patchwork Fri Jun 7 11:49:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-Chien Peter Lin X-Patchwork-Id: 1945071 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.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=rxLm+1Of; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VwfdQ39vpz20PW for ; Fri, 7 Jun 2024 21:50:02 +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: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:In-Reply-To:References: List-Owner; bh=uVqCvBpplUJe43waqMg0+t4z1mJ6UqNA/4GiArvugwI=; b=rxLm+1OfL2obtd XeKcVu81LXcMCgKlHEyMzWq08rk00bUhECb4Kevt57E5LTxYdQgyHn3cvZLX/O4NL3mUdnDL2Rg1r mUez+Gdn/9h9zr6rcn0z64zFUtMMMCD88djDy4rnrZBCks0Urau2/2GKDklCHvJe6rBpRwMPRNBq4 dIiQ71mof895bLRb0Btsh5qpfmCQpdv0lOFhImSOoVrWlAjq+MWgNeQaPM3XdUIAMvB4w4hQPPt3v BICDSDBxw2Bkqt0zXF/s8PcXlSXbipwUdsVSZLYALO2Lug5oizd7+GrMtqXZfEmPmTpKZOt/HKeR/ 5Pyp9Ehi3Ne0scKtfoNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sFY6P-0000000DoEu-1g6c; Fri, 07 Jun 2024 11:49:49 +0000 Received: from 60-248-80-70.hinet-ip.hinet.net ([60.248.80.70] helo=Atcsqr.andestech.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sFY6L-0000000DoDk-32ak for opensbi@lists.infradead.org; Fri, 07 Jun 2024 11:49:47 +0000 Received: from mail.andestech.com (ATCPCS34.andestech.com [10.0.1.134]) by Atcsqr.andestech.com with ESMTP id 457BndkT067795; Fri, 7 Jun 2024 19:49:39 +0800 (+08) (envelope-from peterlin@andestech.com) Received: from swlinux02.andestech.com (10.0.15.183) by ATCPCS34.andestech.com (10.0.1.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Fri, 7 Jun 2024 19:49:40 +0800 From: Yu Chien Peter Lin To: CC: , , Yu Chien Peter Lin , Alvin Chang Subject: [RFC PATCH] sbi: sbi_domain_context: Add 'domain context switched' firmware event Date: Fri, 7 Jun 2024 19:49:35 +0800 Message-ID: <20240607114935.1291206-1-peterlin@andestech.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.0.15.183] X-ClientProxiedBy: ATCPCS33.andestech.com (10.0.1.100) To ATCPCS34.andestech.com (10.0.1.134) X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: Atcsqr.andestech.com 457BndkT067795 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240607_044946_064304_38D69116 X-CRM114-Status: GOOD ( 14.67 ) X-Spam-Score: 0.4 (/) 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: Event codes 256-65534 are reserved for counting SBI-specific firmware events. Allocate event code 256 as the 'domain context switched' firmware event. This enables the perf tool to monitor trusted app [...] Content analysis details: (0.4 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 TVD_RCVD_IP Message was received from an IP address 0.4 RDNS_DYNAMIC Delivered to internal network by host with dynamic-looking rDNS -0.0 T_SCC_BODY_TEXT_LINE No description available. 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 Event codes 256-65534 are reserved for counting SBI-specific firmware events. Allocate event code 256 as the 'domain context switched' firmware event. This enables the perf tool to monitor trusted application invocations. $ perf stat -e r8000000000000100 optee_example_random Invoking TA to generate random UUID... TA generated UUID value = 0xd1dafb5b988373c9a14d1a85361b436 Performance counter stats for 'optee_example_random': 96 r8000000000000100 0.186135100 seconds time elapsed 0.051499000 seconds user 0.125070000 seconds sys Also, factor out valid event code checking to the fw_event_code_valid() function. Signed-off-by: Yu Chien Peter Lin Reviewed-by: Alvin Chang --- include/sbi/sbi_ecall_interface.h | 9 +++++-- lib/sbi/sbi_domain_context.c | 3 +++ lib/sbi/sbi_pmu.c | 43 +++++++++++++++++++------------ 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/include/sbi/sbi_ecall_interface.h b/include/sbi/sbi_ecall_interface.h index ec845c88..aff9ec4c 100644 --- a/include/sbi/sbi_ecall_interface.h +++ b/include/sbi/sbi_ecall_interface.h @@ -199,13 +199,18 @@ enum sbi_pmu_fw_event_code_id { SBI_PMU_FW_HFENCE_VVMA_RCVD = 19, SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20, SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21, - SBI_PMU_FW_MAX, + SBI_PMU_FW_LAST, /* * Event codes 22 to 255 are reserved for future use. + */ + SBI_PMU_FW_MAX = 255, + SBI_PMU_FW_DOMAIN_CONTEXT_SWITCHED = 256, + SBI_PMU_FW_IMPL_LAST, + /* * Event codes 256 to 65534 are reserved for SBI implementation * specific custom firmware events. */ - SBI_PMU_FW_RESERVED_MAX = 0xFFFE, + SBI_PMU_FW_IMPL_MAX = 0xFFFE, /* * Event code 0xFFFF is used for platform specific firmware * events where the event data contains any event specific information. diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c index d9395490..d0b174c9 100755 --- a/lib/sbi/sbi_domain_context.c +++ b/lib/sbi/sbi_domain_context.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -33,6 +34,8 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); unsigned int pmp_count = sbi_hart_pmp_count(scratch); + sbi_pmu_ctr_incr_fw(SBI_PMU_FW_DOMAIN_CONTEXT_SWITCHED); + /* Assign current hart to target domain */ spin_lock(¤t_dom->assigned_harts_lock); sbi_hartmask_clear_hartindex(hartindex, ¤t_dom->assigned_harts); diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c index b9e84543..fd3e70f2 100644 --- a/lib/sbi/sbi_pmu.c +++ b/lib/sbi/sbi_pmu.c @@ -130,6 +130,23 @@ static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt, return false; } +static bool fw_event_code_valid(uint32_t event_idx_code) +{ + /* Standard firmware events */ + if ((0 <= event_idx_code) && + (event_idx_code < SBI_PMU_FW_LAST)) + return true; + /* Implementation specific events */ + if ((SBI_PMU_FW_MAX < event_idx_code) && + (event_idx_code < SBI_PMU_FW_IMPL_LAST)) + return true; + /* Platform specific firmware events */ + if (event_idx_code == SBI_PMU_FW_PLATFORM) + return true; + + return false; +} + static int pmu_event_validate(struct sbi_pmu_hart_state *phs, unsigned long event_idx, uint64_t edata) { @@ -143,9 +160,7 @@ static int pmu_event_validate(struct sbi_pmu_hart_state *phs, event_idx_code_max = SBI_PMU_HW_GENERAL_MAX; break; case SBI_PMU_EVENT_TYPE_FW: - if ((event_idx_code >= SBI_PMU_FW_MAX && - event_idx_code <= SBI_PMU_FW_RESERVED_MAX) || - event_idx_code > SBI_PMU_FW_PLATFORM) + if (!fw_event_code_valid(event_idx_code)) return SBI_EINVAL; if (SBI_PMU_FW_PLATFORM == event_idx_code && @@ -153,7 +168,7 @@ static int pmu_event_validate(struct sbi_pmu_hart_state *phs, return pmu_dev->fw_event_validate_encoding(phs->hartid, edata); else - event_idx_code_max = SBI_PMU_FW_MAX; + event_idx_code_max = SBI_PMU_FW_IMPL_MAX; break; case SBI_PMU_EVENT_TYPE_HW_CACHE: cache_ops_result = event_idx_code & @@ -217,9 +232,7 @@ int sbi_pmu_ctr_fw_read(uint32_t cidx, uint64_t *cval) if (event_idx_type != SBI_PMU_EVENT_TYPE_FW) return SBI_EINVAL; - if ((event_code >= SBI_PMU_FW_MAX && - event_code <= SBI_PMU_FW_RESERVED_MAX) || - event_code > SBI_PMU_FW_PLATFORM) + if (!fw_event_code_valid(event_code)) return SBI_EINVAL; if (SBI_PMU_FW_PLATFORM == event_code) { @@ -414,9 +427,7 @@ static int pmu_ctr_start_fw(struct sbi_pmu_hart_state *phs, uint64_t event_data, uint64_t ival, bool ival_update) { - if ((event_code >= SBI_PMU_FW_MAX && - event_code <= SBI_PMU_FW_RESERVED_MAX) || - event_code > SBI_PMU_FW_PLATFORM) + if (!fw_event_code_valid(event_code)) return SBI_EINVAL; if (SBI_PMU_FW_PLATFORM == event_code) { @@ -518,9 +529,7 @@ static int pmu_ctr_stop_fw(struct sbi_pmu_hart_state *phs, { int ret; - if ((event_code >= SBI_PMU_FW_MAX && - event_code <= SBI_PMU_FW_RESERVED_MAX) || - event_code > SBI_PMU_FW_PLATFORM) + if (!fw_event_code_valid(event_code)) return SBI_EINVAL; if (SBI_PMU_FW_PLATFORM == event_code && @@ -792,9 +801,7 @@ static int pmu_ctr_find_fw(struct sbi_pmu_hart_state *phs, { int i, cidx; - if ((event_code >= SBI_PMU_FW_MAX && - event_code <= SBI_PMU_FW_RESERVED_MAX) || - event_code > SBI_PMU_FW_PLATFORM) + if (!fw_event_code_valid(event_code)) return SBI_EINVAL; for_each_set_bit(i, &cmask, BITS_PER_LONG) { @@ -907,8 +914,10 @@ int sbi_pmu_ctr_incr_fw(enum sbi_pmu_fw_event_code_id fw_id) if (likely(!phs->fw_counters_started)) return 0; - if (unlikely(fw_id >= SBI_PMU_FW_MAX)) + if (unlikely((fw_id == SBI_PMU_FW_PLATFORM) || + !fw_event_code_valid(fw_id))) { return SBI_EINVAL; + } for (cidx = num_hw_ctrs; cidx < total_ctrs; cidx++) { if (get_cidx_code(phs->active_events[cidx]) == fw_id &&