Skip to contentSkip to conversation

soundwire: qcom: fix WCD AHB serialization and wr_fifo_depth init order

PATCH New Robert Ijtsma 2 participants 4 messages Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover soundwire: qcom: fix WCD AHB serialization and wr_fifo_depth init order

Fix two race conditions observed on WCD934x-hosted Qualcomm SoundWire controllers (such as on Microsoft Surface Pro X / SC8180X):

1. Serialize each two-step WCD AHB bridge register access (RD_ADDR + RD_DATA or WR_DATA + WR_ADDR) with a controller mutex so concurrent IRQ and init/bus accesses cannot interleave on the shared bridge registers.
2. Read and validate SWRM_COMP_PARAMS write FIFO depth before registering the threaded IRQ in probe and before enabling CPU interrupts in qcom_swrm_init(), avoiding false write-FIFO overflow errors while wr_fifo_depth is still zero.

Tested on Microsoft Surface Pro X SQ2 (SC8180X, WCD934x + WSA881x).

---
Robert Ijtsma (2):
soundwire: qcom: serialize WCD AHB bridge transactions
soundwire: qcom: populate wr_fifo_depth before enabling interrupts

 drivers/soundwire/qcom.c | 57 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)

---
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
change-id: 20261124-qcom-soundwire-fixes-9d31c8dc5303

Best regards,

Signature · 1 line
Robert Ijtsma <[email protected]>
#1 lore raw

PATCH 1/2 soundwire: qcom: serialize WCD AHB bridge transactions

On SLIMbus-hosted WCD934x Qualcomm SoundWire controllers, each AHB bridge register access spans two separate regmap_bulk_*() transfers (SWRM_AHB_BRIDGE_RD_ADDR_0 followed by SWRM_AHB_BRIDGE_RD_DATA_0, or SWRM_AHB_BRIDGE_WR_DATA_0 followed by SWRM_AHB_BRIDGE_WR_ADDR_0).

Without controller-level serialization across each pair, concurrent accesses from the threaded IRQ handler and bus/init callers can interleave on the shared AHB bridge registers and corrupt reads or writes.

Add ahb_lock to struct qcom_swrm_ctrl to serialize each two-step AHB bridge register transaction.

Fixes 02efb49aa805 ("soundwire: qcom: add support for SoundWire controller")
---
 drivers/soundwire/qcom.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/soundwire/qcom.c +17 -13
index c31d921..368f511 100644
@@ -202,6 +202,8 @@ struct qcom_swrm_ctrl {
 	struct completion enumeration;
 	/* Port alloc/free lock */
 	struct mutex port_lock;
+	/* Serialize two-step WCD AHB bridge address/data accesses */
+	struct mutex ahb_lock;
 	struct clk *hclk;
 	int irq;
 	u32 ee;
@@ -388,18 +390,18 @@ static int qcom_swrm_ahb_reg_read(struct qcom_swrm_ctrl *ctrl, int reg,
 	struct regmap *wcd_regmap = ctrl->regmap;
 	int ret;
 
+	mutex_lock(&ctrl->ahb_lock);
 	/* pg register + offset */
 	ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_RD_ADDR_0,
-			  (u8 *)&reg, 4);
+				(u8 *)&reg, 4);
 	if (ret < 0)
-		return SDW_CMD_FAIL;
+		goto out;
 
 	ret = regmap_bulk_read(wcd_regmap, SWRM_AHB_BRIDGE_RD_DATA_0,
 			       val, 4);
-	if (ret < 0)
-		return SDW_CMD_FAIL;
-
-	return SDW_CMD_OK;
+out:
+	mutex_unlock(&ctrl->ahb_lock);
+	return ret < 0 ? SDW_CMD_FAIL : SDW_CMD_OK;
 }
 
 static int qcom_swrm_ahb_reg_write(struct qcom_swrm_ctrl *ctrl,
@@ -407,19 +409,20 @@ static int qcom_swrm_ahb_reg_write(struct qcom_swrm_ctrl *ctrl,
 {
 	struct regmap *wcd_regmap = ctrl->regmap;
 	int ret;
+
+	mutex_lock(&ctrl->ahb_lock);
 	/* pg register + offset */
 	ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_DATA_0,
-			  (u8 *)&val, 4);
+				(u8 *)&val, 4);
 	if (ret)
-		return SDW_CMD_FAIL;
+		goto out;
 
 	/* write address register */
 	ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_ADDR_0,
-			  (u8 *)&reg, 4);
-	if (ret)
-		return SDW_CMD_FAIL;
-
-	return SDW_CMD_OK;
+				(u8 *)&reg, 4);
+out:
+	mutex_unlock(&ctrl->ahb_lock);
+	return ret ? SDW_CMD_FAIL : SDW_CMD_OK;
 }
 
 static int qcom_swrm_cpu_reg_read(struct qcom_swrm_ctrl *ctrl, int reg,
@@ -1680,6 +1683,7 @@ static int qcom_swrm_probe(struct platform_device *pdev)
 	ctrl->dev = dev;
 	dev_set_drvdata(&pdev->dev, ctrl);
 	mutex_init(&ctrl->port_lock);
+	mutex_init(&ctrl->ahb_lock);
 	init_completion(&ctrl->broadcast);
 	init_completion(&ctrl->enumeration);
 
Signature · 1 line
2.56.0
#2 lore raw

PATCH 2/2 soundwire: qcom: populate wr_fifo_depth before enabling interrupts

In reply to Robert Ijtsma

ctrl->wr_fifo_depth is currently populated at the end of qcom_swrm_init(), after devm_request_threaded_irq() in qcom_swrm_probe() and after enabling CPU interrupts in qcom_swrm_init().

If a SoundWire interrupt fires before SWRM_COMP_PARAMS has been read, qcom_swrm_cmd_fifo_wr_cmd() observes ctrl->wr_fifo_depth == 0 and fails with -EIO ("err write overflow").

Read and validate SWRM_COMP_PARAMS in qcom_swrm_probe() prior to requesting the threaded IRQ, and read SWRM_COMP_PARAMS before enabling CPU interrupts in qcom_swrm_init().

Fixes 02efb49aa805 ("soundwire: qcom: add support for SoundWire controller")
---
 drivers/soundwire/qcom.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/soundwire/qcom.c +21 -6
index 368f511..3a42ab8 100644
@@ -997,6 +997,12 @@ static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl)
 	ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
 			0xFFFFFFFF);
 
+	ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
+	if (ctrl->version >= SWRM_VERSION_3_1_0)
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_V3_COMP_PARAMS_WR_FIFO_DEPTH, val);
+	else
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_WR_FIFO_DEPTH, val);
+
 	/* enable CPU IRQs for the selected EE window */
 	ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
 			SWRM_INTERRUPT_STATUS_RMSK);
@@ -1008,12 +1014,6 @@ static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl)
 
 	swrm_wait_for_frame_gen_enabled(ctrl);
 	ctrl->slave_status = 0;
-	ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
-
-	if (ctrl->version >= SWRM_VERSION_3_1_0)
-		ctrl->wr_fifo_depth = FIELD_GET(SWRM_V3_COMP_PARAMS_WR_FIFO_DEPTH, val);
-	else
-		ctrl->wr_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_WR_FIFO_DEPTH, val);
 
 	return 0;
 }
@@ -1717,6 +1717,21 @@ static int qcom_swrm_probe(struct platform_device *pdev)
 	ctrl->reg_read(ctrl, SWRM_COMP_HW_VERSION, &ctrl->version);
 	qcom_swrm_set_ee_register_layout(ctrl, data);
 
+	ret = ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
+	if (ret != SDW_CMD_OK) {
+		ret = -EIO;
+		goto err_clk;
+	}
+	if (ctrl->version >= SWRM_VERSION_3_1_0)
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_V3_COMP_PARAMS_WR_FIFO_DEPTH, val);
+	else
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_WR_FIFO_DEPTH, val);
+	if (!ctrl->wr_fifo_depth) {
+		dev_err(dev, "Invalid write FIFO depth in SWRM_COMP_PARAMS: %#x\n", val);
+		ret = -EINVAL;
+		goto err_clk;
+	}
+
 	ret = devm_request_threaded_irq(dev, ctrl->irq, NULL,
 					qcom_swrm_irq_handler,
 					IRQF_TRIGGER_RISING |
Signature · 1 line
2.56.0
#3 lore raw

Re: [PATCH 2/2] soundwire: qcom: populate wr_fifo_depth before enabling interrupts

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] soundwire: qcom: Fixes trailer cites 02efb49aa805 instead of a661308c34de - [Low] soundwire: qcom…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] soundwire: qcom: Fixes trailer cites 02efb49aa805 instead of a661308c34de
- [Low] soundwire: qcom: redundant unchecked SWRM_COMP_PARAMS read in qcom_swrm_init()
--

commit 6cb5da021ef8535ab5b5f9bfc4e8100a46f9d35b
Author: Robert Ijtsma <[email protected]>

Quoted 10 lines

soundwire: qcom: populate wr_fifo_depth before enabling interrupts

ctrl->wr_fifo_depth is currently populated at the end of qcom_swrm_init(), after devm_request_threaded_irq() in qcom_swrm_probe() and after enabling CPU interrupts in qcom_swrm_init().

If a SoundWire interrupt fires before SWRM_COMP_PARAMS has been read, qcom_swrm_cmd_fifo_wr_cmd() observes ctrl->wr_fifo_depth == 0 and fails with -EIO ("err write overflow").

Quoted 6 lines

Read and validate SWRM_COMP_PARAMS in qcom_swrm_probe() prior to requesting the threaded IRQ, and read SWRM_COMP_PARAMS before enabling CPU interrupts in qcom_swrm_init().

Fixes 02efb49aa805 ("soundwire: qcom: add support for SoundWire controller")

[Severity: Low]
Should the Fixes: tag point to a661308c34de ("soundwire: qcom: wait for fifo space to be available before read/write") instead of 02efb49aa805 ("soundwire: qcom: add support for SoundWire controller")?

Commit a661308c34de ("soundwire: qcom: wait for fifo space to be available before read/write") introduced ctrl->wr_fifo_depth, the SWRM_COMP_PARAMS read at the end of qcom_swrm_init(), and swrm_wait_for_wr_fifo_avail() in qcom_swrm_cmd_fifo_wr_cmd().

Quoted 5 lines
drivers/soundwire/qcom.c +0 -0
index 368f511867835..3a42ab85be7c6 100644

drivers/soundwire/qcom.c static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl) · line 1005

Quoted 9 lines
drivers/soundwire/qcom.c +6 -0
@@ -997,6 +997,12 @@ static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl)
 	ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
 			0xFFFFFFFF);
 
+	ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
+	if (ctrl->version >= SWRM_VERSION_3_1_0)
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_V3_COMP_PARAMS_WR_FIFO_DEPTH, val);
+	else
+		ctrl->wr_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_WR_FIFO_DEPTH, val);
+

[Severity: Low]
Is re-reading SWRM_COMP_PARAMS in qcom_swrm_init() needed now that qcom_swrm_probe() reads and validates ctrl->wr_fifo_depth before requesting the IRQ or calling qcom_swrm_init()?

SWRM_COMP_PARAMS is a static capability register, and this second read does not check the return status of ctrl->reg_read(). If a transient regmap read error occurs during controller initialization or runtime resume, val retains the value previously read from SWRM_MCP_CFG_ADDR and overwrites the validated ctrl->wr_fifo_depth with 0, causing later command FIFO writes to fail with -EIO.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2