From patchwork Wed Jan 18 14:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 1728272 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=) 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=rT2tjr0/; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=ldu/BvG1; dkim-atps=neutral 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NxpQw2Sbkz23h2 for ; Thu, 19 Jan 2023 01:43:20 +1100 (AEDT) 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=/HfUcRTLZZvkFED2QzlSGiBObqUwXV+griIpvfm5ucQ=; b=rT2tjr0/RTP9AY yHgBJlcmc5Ni6+kIsSEF+4/RStWNBsjDbLffAe7QdGlspzWCZ0QSr52y3IXVeUhfqmTlj7Oo7lKMi HrsO86sk5iN6XeX17wyismJTfBboG6+4hc7TgqvPp8ZDEnZX4yYhkrD/pjEUQxBnMLF2FtnxTkIRV YG16/qC5el3KpqtCpTMOFbDDUFhW2jb9UE8wVODN7j84JWlS3V0tSUgyzzuvsCEqOb/UtEKD0xw/C W01IAbk0TZfMnLYphBSogpw+HPVNCg9voADwkZfq/1o4LcIWAWAhd8XBk9DpLlwzWM2zbo3scQS/h fJqc34YgG6nolvK5Clyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pI9eZ-001OOe-V1; Wed, 18 Jan 2023 14:43:03 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pI9eC-001O9p-P1 for opensbi@lists.infradead.org; Wed, 18 Jan 2023 14:42:42 +0000 Received: by mail-wm1-x332.google.com with SMTP id j17so6316616wms.0 for ; Wed, 18 Jan 2023 06:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f86K4LITdtrJlh5KZYU36BCLCG0ATdJDYqiNhdsk8fI=; b=ldu/BvG1c+P3vzIJeXR830cFFzMNX3vY/TSlhxdT20qG7aPCZuZqZqBbNdvzcMxem2 xOEhubfdonr7nYXwLOB4NX5kobRyclgn1s65xJk/OQNDi/dV1Niy1besrT3YMCNhRZMD oLQS1k8EtkZhgWyPkJNBiwkALn+wkmHxKfiT+CAt5/QsQXsVrB1SOfbTvwaOj7TCTARY B4l1KSdlFnOeubDzD3uatWEilTxayuHBPrJpniaqptKUwhiEtOfRfj3oDJ50pds3aJGj clP6j02KQyLC5Kl2Zzc0DyV2q8IswaL4i+JBAvXHadtxqKdwNHJ44ozui8DXqJ+p8LTl I3Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f86K4LITdtrJlh5KZYU36BCLCG0ATdJDYqiNhdsk8fI=; b=mdhTi6ywDFlpE1m+c2Ct/KpTFqqb5Xr6MDa9SaaxY6Y765ZqrfYH1XcXFa5EXK5wId wtYwGDvVzr95bV5pepza/VeoyKtaWSdBBv1hdLJheKjnYwz4tYmOO0SYiquQ3DfjfwG8 A1F5JOIkcfYa6ONxvOtyai/sO7O+iJQMNFfZ4uYlfc+3h+MqUQ9OC+MnHg0c+PAVGa68 ZX+i+xyPwsvJeXj/oXwGn4cIZLdaGMe9pbwBZbtOfNF4tvYS7x48yf6CZ2aBR4TuANGH 5ZwvQHMRgZB+h5uXRqHOPIzlF1RTPYbAtOOcgHSVOuMBw9Iw8pHcE6YwnK6pY69H3WjP 2J1A== X-Gm-Message-State: AFqh2kp07miTwMWL0NcN0W7NLqX9RxRuO5xVqLuaj1WnqehH0hNRSihw GnC+kNV1cEGpKxNnuxYpIjFyPzdjnbJHavRS X-Google-Smtp-Source: AMrXdXtZszIpg/UqhaIKJeluymbaBN4W7bM6JZ7PDkF+cb5ln9NEqz9yjXE9MsujiKVEwitJn9r6nA== X-Received: by 2002:a1c:f203:0:b0:3da:fa42:bbf2 with SMTP id s3-20020a1cf203000000b003dafa42bbf2mr6718398wmc.28.1674052956825; Wed, 18 Jan 2023 06:42:36 -0800 (PST) Received: from localhost (2001-1ae9-1c2-4c00-20f-c6b4-1e57-7965.ip6.tmcz.cz. [2001:1ae9:1c2:4c00:20f:c6b4:1e57:7965]) by smtp.gmail.com with ESMTPSA id a3-20020a05600c348300b003db09692364sm2244254wmq.11.2023.01.18.06.42.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 06:42:36 -0800 (PST) From: Andrew Jones To: opensbi@lists.infradead.org Cc: 'Atish Patra ' , 'Anup Patel ' , 'Ley Foon Tan ' , 'Sia Jee Heng ' , Anup Patel Subject: [PATCH 01/11] lib: sbi_hsm: Factor out invalid state detection Date: Wed, 18 Jan 2023 15:42:24 +0100 Message-Id: <20230118144234.79263-2-ajones@ventanamicro.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230118144234.79263-1-ajones@ventanamicro.com> References: <20230118144234.79263-1-ajones@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230118_064240_865150_429E0073 X-CRM114-Status: GOOD ( 11.77 ) X-Spam-Score: -0.2 (/) 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: Remove some redundant code by creating an invalid state detection macro. No functional change intended. Signed-off-by: Andrew Jones Reviewed-by: Anup Patel --- lib/sbi/sbi_hsm.c | 65 +++++++++++++++++ 1 file changed, 24 insert [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:332 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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 Remove some redundant code by creating an invalid state detection macro. No functional change intended. Signed-off-by: Andrew Jones Reviewed-by: Anup Patel --- lib/sbi/sbi_hsm.c | 65 +++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/lib/sbi/sbi_hsm.c b/lib/sbi/sbi_hsm.c index c0a5505423bf..b43f26cfe3a0 100644 --- a/lib/sbi/sbi_hsm.c +++ b/lib/sbi/sbi_hsm.c @@ -26,6 +26,15 @@ #include #include +#define __sbi_hsm_hart_change_state(hdata, oldstate, newstate) \ +({ \ + long state = atomic_cmpxchg(&(hdata)->state, oldstate, newstate); \ + if (state != (oldstate)) \ + sbi_printf("%s: ERR: The hart is in invalid state [%lu]\n", \ + __func__, state); \ + state == (oldstate); \ +}) + static const struct sbi_hsm_device *hsm_dev = NULL; static unsigned long hart_data_offset; @@ -95,13 +104,11 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom, void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid) { - u32 oldstate; struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_START_PENDING, - SBI_HSM_STATE_STARTED); - if (oldstate != SBI_HSM_STATE_START_PENDING) + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_START_PENDING, + SBI_HSM_STATE_STARTED)) sbi_hart_hang(); } @@ -217,14 +224,12 @@ int sbi_hsm_init(struct sbi_scratch *scratch, u32 hartid, bool cold_boot) void __noreturn sbi_hsm_exit(struct sbi_scratch *scratch) { - u32 hstate; struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); void (*jump_warmboot)(void) = (void (*)(void))scratch->warmboot_addr; - hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOP_PENDING, - SBI_HSM_STATE_STOPPED); - if (hstate != SBI_HSM_STATE_STOP_PENDING) + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_STOP_PENDING, + SBI_HSM_STATE_STOPPED)) goto fail_exit; if (hsm_device_has_hart_hotplug()) { @@ -299,7 +304,6 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, int sbi_hsm_hart_stop(struct sbi_scratch *scratch, bool exitnow) { - int oldstate; const struct sbi_domain *dom = sbi_domain_thishart_ptr(); struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); @@ -307,13 +311,9 @@ int sbi_hsm_hart_stop(struct sbi_scratch *scratch, bool exitnow) if (!dom) return SBI_EFAIL; - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STARTED, - SBI_HSM_STATE_STOP_PENDING); - if (oldstate != SBI_HSM_STATE_STARTED) { - sbi_printf("%s: ERR: The hart is in invalid state [%u]\n", - __func__, oldstate); + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_STARTED, + SBI_HSM_STATE_STOP_PENDING)) return SBI_EFAIL; - } if (exitnow) sbi_exit(scratch); @@ -363,36 +363,26 @@ static void __sbi_hsm_suspend_non_ret_restore(struct sbi_scratch *scratch) void sbi_hsm_hart_resume_start(struct sbi_scratch *scratch) { - int oldstate; struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); /* If current HART was SUSPENDED then set RESUME_PENDING state */ - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_SUSPENDED, - SBI_HSM_STATE_RESUME_PENDING); - if (oldstate != SBI_HSM_STATE_SUSPENDED) { - sbi_printf("%s: ERR: The hart is in invalid state [%u]\n", - __func__, oldstate); + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_SUSPENDED, + SBI_HSM_STATE_RESUME_PENDING)) sbi_hart_hang(); - } hsm_device_hart_resume(); } void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch) { - u32 oldstate; struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); /* If current HART was RESUME_PENDING then set STARTED state */ - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_RESUME_PENDING, - SBI_HSM_STATE_STARTED); - if (oldstate != SBI_HSM_STATE_RESUME_PENDING) { - sbi_printf("%s: ERR: The hart is in invalid state [%u]\n", - __func__, oldstate); + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_RESUME_PENDING, + SBI_HSM_STATE_STARTED)) sbi_hart_hang(); - } /* * Restore some of the M-mode CSRs which we are re-configured by @@ -404,7 +394,7 @@ void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch) int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type, ulong raddr, ulong rmode, ulong arg1) { - int oldstate, ret; + int ret; const struct sbi_domain *dom = sbi_domain_thishart_ptr(); struct sbi_hsm_data *hdata = sbi_scratch_offset_ptr(scratch, hart_data_offset); @@ -438,11 +428,8 @@ int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type, scratch->next_mode = rmode; /* Directly move from STARTED to SUSPENDED state */ - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STARTED, - SBI_HSM_STATE_SUSPENDED); - if (oldstate != SBI_HSM_STATE_STARTED) { - sbi_printf("%s: ERR: The hart is in invalid state [%u]\n", - __func__, oldstate); + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_STARTED, + SBI_HSM_STATE_SUSPENDED)) { ret = SBI_EDENIED; goto fail_restore_state; } @@ -485,13 +472,9 @@ fail_restore_state: * We might have successfully resumed from retentive suspend * or suspend failed. In both cases, we restore state of hart. */ - oldstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_SUSPENDED, - SBI_HSM_STATE_STARTED); - if (oldstate != SBI_HSM_STATE_SUSPENDED) { - sbi_printf("%s: ERR: The hart is in invalid state [%u]\n", - __func__, oldstate); + if (!__sbi_hsm_hart_change_state(hdata, SBI_HSM_STATE_SUSPENDED, + SBI_HSM_STATE_STARTED)) sbi_hart_hang(); - } return ret; }