From patchwork Thu Jun 30 09:46:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adnan Rahman Chowdhury X-Patchwork-Id: 1650666 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=Ha67QSyw; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=PmQKCyQn; dkim-atps=neutral Authentication-Results: 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=) 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LYYQQ1k2hz9s0r for ; Thu, 30 Jun 2022 19:47:10 +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=qZwzEOS8K4lDSaU9nHNmOmEtKQnfrp/gnkX0BHButHI=; b=Ha67QSywdsVGsI Jj8QUzGw513tWmg+67G38hZ+TCCjgU5G1iRrfo9/Iee+GAwh+nN9yJFxgXURvMj/NJgonGy0G7oD+ utNUX54sP2rQf5X0yystIkra6hq78PhuAZSqFVE6IJAnHhj0cSgPJGp4Yl1HccewY16vtMkLXV8Ii Nv/mr8Tn1TrdC6GEGK0PiXjUrJOJyVo1997xRIfVlHAESOMEeX1nUt7wBsJ7EaFxQoB6Z8k0gJe0g 4Pk8hhU+nWWnxHXN0dHMYjFhYJhFR+52PsKs6mi1Ihvx6jZqfBJDQmT3ubhxj1eIFT2gaAvEgzlS4 LH8i62V7/PEShfAG9tQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o6qlE-00GLfE-02; Thu, 30 Jun 2022 09:46:56 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o6ql9-00GLc7-Uw for opensbi@lists.infradead.org; Thu, 30 Jun 2022 09:46:54 +0000 Received: by mail-wr1-x432.google.com with SMTP id v9so10837431wrp.7 for ; Thu, 30 Jun 2022 02:46:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=O+xeqOoIOR9q11ccQJaqHfc1Uiwg7G6EQjX/bKQXGAA=; b=PmQKCyQnkW7c1e4IPUQzw0tZJQZ+wBwoyK4vX8QquBqVYpDVHkqiffs+1mUbNJYcTn CV7zbgfCSQnUf7ODovhbR+1OE+1A16J744uuV6JtfHdrkVS6bfEhXskQ6El7An6SvdA7 ZCrCO4fuhU2tqdfJOsmmKhcnBvFP1fuYxJf2d9dGbnLJwQOoUaN2Xm8aR0d1JZ055Hge yR1huDXoECot5nhQEBUdL6dsROFPIElgfIjPwXi8o4fusN36b3ayZQyzjlyC5zHvfjLl NcNW7eIOTFLsGm9Fff8ryMozpghZ+lp8YD7nfpxX1+hX0BTghTQ1QHrEd2hK6cCm60TC jQNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O+xeqOoIOR9q11ccQJaqHfc1Uiwg7G6EQjX/bKQXGAA=; b=eOzB00bzK/6KgeQqb+4DvMLmr6Std7Md1gljBANioZSjxeG+Ccai0yETYSBc89vxJ6 QmKkHPwRe2UnzAbolL9Kf5cwAH3ewb2XCoT5B1YCy0HJ7sFfpY/WZNwMqjyiXWzbBfVD Jw7e21dvO6sPjMx+qD+KaVtWjUCBqIKDMngktbz4nwkxvQo1OM2noEnqo1lH6Zh2LQ8m y/zAooZ/23+BYSb8hYBFJepHxFhFWwAD0RYF6fK0hXKFJyOLJIfvjxq+zLH61YI9Vb7L i9HlDdYvM1nPY0oguuCp2EP75c37x9gaAziqs1CWIiCCNbYPzrSpm8wSNu5NtGaYRT9x D7Xw== X-Gm-Message-State: AJIora9vboPayutteKx0bjfw8Ni3XywBpTf683/WfNZtZwyBwTzLCE2F osTB2dvQWVbn9kO2wZHAlUCvpI/IAzjGnw== X-Google-Smtp-Source: AGRyM1uNC81dZx8h9TD+JXS8mM/wHiMszBF09I8qPdkWO6rLip17yFicMnKIvoS98SCx9i9M1Vf4Rw== X-Received: by 2002:adf:f112:0:b0:21b:c01f:e6c8 with SMTP id r18-20020adff112000000b0021bc01fe6c8mr7190809wro.681.1656582408005; Thu, 30 Jun 2022 02:46:48 -0700 (PDT) Received: from ct-lt-2255.office.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id l10-20020a5d560a000000b0021b9270de3csm19974672wrv.10.2022.06.30.02.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jun 2022 02:46:46 -0700 (PDT) From: Adnan Rahman Chowdhury To: opensbi@lists.infradead.org Cc: Adnan Rahman Chowdhury , Xiang W Subject: [PATCH v2 1/1] lib: sbi_timer: Added a blocking wait function that waits until a certain condition is satisfied or timeout occurs Date: Thu, 30 Jun 2022 10:46:35 +0100 Message-Id: <20220630094635.389645-2-adnan.chowdhury@sifive.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220630094635.389645-1-adnan.chowdhury@sifive.com> References: <20220630094635.389645-1-adnan.chowdhury@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220630_024652_028639_27689733 X-CRM114-Status: GOOD ( 11.44 ) 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: Motivation: Suppose a peripheral needs to be configured to transmit data. There is an SFR bit which indicates that the peripheral is ready to transmit. The firmware should check the bit and will only [...] 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:432 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 Motivation: Suppose a peripheral needs to be configured to transmit data. There is an SFR bit which indicates that the peripheral is ready to transmit. The firmware should check the bit and will only transmit data when the peripheral is ready. When the firmware starts polling the SFR, the peripheral could be busy transmitting/receiving other data so the firmware must wait till that completes. Assuming that there is no other way, the firmware shouldn't wait indefinitely. The function sbi_timer_waitms_until() will constantly check whether a certain condition is satisfied, or timeout occurs. It can be used for the cases when a timeout is required. Signed-off-by: Adnan Rahman Chowdhury Reviewed-by: Xiang W Reviewed-by: Anup Patel --- include/sbi/sbi_timer.h | 18 ++++++++++++++++++ lib/sbi/sbi_timer.c | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/sbi/sbi_timer.h b/include/sbi/sbi_timer.h index 63ef1af..ac48e2b 100644 --- a/include/sbi/sbi_timer.h +++ b/include/sbi/sbi_timer.h @@ -48,6 +48,24 @@ static inline void sbi_timer_udelay(ulong usecs) sbi_timer_delay_loop(usecs, 1000000, NULL, NULL); } +/** + * A blocking function that will wait until @p predicate returns true or + * @p timeout_ms milliseconds elapsed. @p arg will be passed as argument to + * @p predicate function. + * + * @param predicate Pointer to a function that returns true if certain + * condition is met. It shouldn't block the code execution. + * @param arg Argument to pass to @p predicate. + * @param timeout_ms Timeout value in milliseconds. The function will return + * false if @p timeout_ms time period elapsed but still @p predicate doesn't + * return true. + * + * @return true if @p predicate returns true within @p timeout_ms, false + * otherwise. + */ +bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg, + uint64_t timeout_ms); + /** Get timer value for current HART */ u64 sbi_timer_value(void); diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c index acdba92..649a4e6 100644 --- a/lib/sbi/sbi_timer.c +++ b/lib/sbi/sbi_timer.c @@ -81,6 +81,19 @@ void sbi_timer_delay_loop(ulong units, u64 unit_freq, delay_fn(opaque); } +bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg, + uint64_t timeout_ms) +{ + uint64_t start_time = sbi_timer_value(); + uint64_t ticks = + (sbi_timer_get_device()->timer_freq / 1000) * + timeout_ms; + while(!predicate(arg)) + if (sbi_timer_value() - start_time >= ticks) + return false; + return true; +} + u64 sbi_timer_value(void) { if (get_time_val)