Skip to contentSkip to conversation

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

PATCH New Robert Ijtsma 1 participant 3 messages Nov 26

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

v2 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).

---
Changes in v2:
- Drop redundant unchecked SWRM_COMP_PARAMS read from qcom_swrm_init() in
patch 2/2 since qcom_swrm_probe() already reads and validates
wr_fifo_depth before requesting the IRQ.
- Point patch 2/2 Fixes: tag to a661308c34de ("soundwire: qcom: wait for
fifo space to be available before read/write").
- Link to v1: https://patch.msgid.link/[email protected]

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

 drivers/soundwire/qcom.c | 51 ++++++++++++++++++++++++++++++------------------
 1 file changed, 32 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 v2 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 c31d921f679f968652c744aceecf001ab3794a6c..368f511867835e6ce1e1d7f0827461413c25a96e 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.47.3
#2 lore raw

PATCH v2 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 drop the redundant read from the end of qcom_swrm_init().

Fixes a661308c34de ("soundwire: qcom: wait for fifo space to be available before read/write")
---
 drivers/soundwire/qcom.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/soundwire/qcom.c +15 -6
index 368f511867835e6ce1e1d7f0827461413c25a96e..d4568d948ca9f8f52b061608e9bd33b1fbaded44 100644
@@ -1008,12 +1008,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 +1711,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.47.3