Archive-only list for patches
 help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	linux-kernel@vger.kernel.org, patches@lists.linux.dev,
	"Nícolas F. R. A. Prado" <nfraprado@collabora.com>,
	"Chen-Yu Tsai" <wenst@chromium.org>
Subject: [PATCH 03/10] spmi: mtk-pmif: Keep spinlock until read is fully done
Date: Fri, 23 Jan 2026 10:20:31 -0800	[thread overview]
Message-ID: <20260123182039.224314-4-sboyd@kernel.org> (raw)
In-Reply-To: <20260123182039.224314-1-sboyd@kernel.org>

From: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>

Move the spin unlocking to after reading the contents of the
PMIF_SWINF_(x)_RDATA_31_0 register in pmif_spmi_read_cmd():
since this is the only register that we can read to get the
data from all of the arbitered busses, a concurrent request
for reading (especially on a busy arbiter) will show a race
condition and a unexpected or corrupted value may be read.

Doing the entire read sequence while spin locked guarantees
that concurrent access to the arbiter doesn't happen.

Fixes: f200fff8d019 ("spmi: mtk-pmif: Serialize PMIF status check and command submission")
Reviewed-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Reviewed-by: Chen-Yu Tsai <wenst@chromium.org>
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
---
 drivers/spmi/spmi-mtk-pmif.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/spmi/spmi-mtk-pmif.c b/drivers/spmi/spmi-mtk-pmif.c
index 68f458587c67..9f416b231ab8 100644
--- a/drivers/spmi/spmi-mtk-pmif.c
+++ b/drivers/spmi/spmi-mtk-pmif.c
@@ -22,7 +22,7 @@
 #define PMIF_CMD_EXT_REG	2
 #define PMIF_CMD_EXT_REG_LONG	3
 
-#define PMIF_DELAY_US   10
+#define PMIF_DELAY_US   2
 #define PMIF_TIMEOUT_US (10 * 1000)
 
 #define PMIF_CHAN_OFFSET 0x5
@@ -372,7 +372,6 @@ static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
 	/* Send the command. */
 	cmd = (opc << 30) | (sid << 24) | ((len - 1) << 16) | addr;
 	pmif_writel(arb, pbus, cmd, inf_reg->ch_send);
-	raw_spin_unlock_irqrestore(&pbus->lock, flags);
 
 	/*
 	 * Wait for Software Interface FSM state to be WFVLDCLR,
@@ -382,13 +381,16 @@ static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
 					data, GET_SWINF(data) == SWINF_WFVLDCLR,
 					PMIF_DELAY_US, PMIF_TIMEOUT_US);
 	if (ret < 0) {
+		raw_spin_unlock_irqrestore(&pbus->lock, flags);
 		dev_err(&ctrl->dev, "failed to wait for SWINF_WFVLDCLR\n");
 		return ret;
 	}
 
 	data = pmif_readl(arb, pbus, inf_reg->rdata);
-	memcpy(buf, &data, len);
 	pmif_writel(arb, pbus, 1, inf_reg->ch_rdy);
+	raw_spin_unlock_irqrestore(&pbus->lock, flags);
+
+	memcpy(buf, &data, len);
 
 	return 0;
 }
-- 
https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git
https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git


  parent reply	other threads:[~2026-01-23 18:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-23 18:20 [PATCH 00/10] spmi: Patches for the merge window Stephen Boyd
2026-01-23 18:20 ` [PATCH 01/10] dt-bindings: spmi: Add MediaTek MT8196 SPMI 2 Arbiter/Controllers Stephen Boyd
2026-01-23 18:20 ` [PATCH 02/10] spmi: mtk-pmif: Add multi-bus support for SPMI 2.0 Stephen Boyd
2026-01-23 18:20 ` Stephen Boyd [this message]
2026-01-23 18:20 ` [PATCH 04/10] spmi: mtk-pmif: Implement Request Capable Slave (RCS) interrupt Stephen Boyd
2026-01-23 18:20 ` [PATCH 05/10] spmi: mtk-pmif: Add support for MT8196 SPMI Controller Stephen Boyd
2026-01-23 18:20 ` [PATCH 06/10] spmi: apple: Add "apple,t8103-spmi" compatible Stephen Boyd
2026-01-23 18:20 ` [PATCH 07/10] dt-bindings: spmi: split out common QCOM SPMI PMIC arbiter properties Stephen Boyd
2026-01-23 18:20 ` [PATCH 08/10] dt-bindings: spmi: add support for glymur-spmi-pmic-arb (arbiter v8) Stephen Boyd
2026-01-23 18:20 ` [PATCH 09/10] spmi: spmi-pmic-arb: add support for PMIC arbiter v8 Stephen Boyd
2026-01-23 18:20 ` [PATCH 10/10] dt-bindings: spmi: spmi-mtk-pmif: Add compatible for MT8189 SoC Stephen Boyd

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260123182039.224314-4-sboyd@kernel.org \
    --to=sboyd@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nfraprado@collabora.com \
    --cc=patches@lists.linux.dev \
    --cc=wenst@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox