Skip to contentSkip to conversation

Yet another round of SDCA fixes

PATCH In review Charles Keepax 2 participants 8 messages Apr 8

Subsystems (3)
lore
Share & tools
lore

Charles Keepax

author maintainer
#0 lore raw

cover Yet another round of SDCA fixes

Another round of SDCA fixes, a couple of fixes to the IRQ handling from Maciej, a couple of fixes to the IRQ cleanup from Richard, and a minor tweak to the IRQ handling from me.

Thanks,
Charles

Charles Keepax (1):
ASoC: SDCA: Tidy up irq_enable_flags()/sdca_irq_disable()

Maciej Strozek (2):
ASoC: SDCA: Fix overwritten var within for loop
ASoC: SDCA: mask Function_Status value

Richard Fitzgerald (2):
ASoC: SDCA: Fix cleanup inversion in class driver
ASoC: SDCA: Unregister IRQ handlers on module remove

 include/sound/sdca_interrupts.h      |  4 ++--
 sound/soc/sdca/sdca_class.c          | 27 ++++++++++++++-------------
 sound/soc/sdca/sdca_class_function.c | 10 +++++++++-
 sound/soc/sdca/sdca_interrupts.c     | 23 +++++++++--------------
 4 files changed, 34 insertions(+), 30 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/5 ASoC: SDCA: Fix overwritten var within for loop

From: Maciej Strozek <[email protected]>

mask variable should not be overwritten within the for loop or it will skip certain bits. Change to using BIT() macro.

Fixes b9ab3b618241 ("ASoC: SDCA: Add some initial IRQ handlers")
---
 sound/soc/sdca/sdca_interrupts.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
sound/soc/sdca/sdca_interrupts.c +1 -3
index 1838dabcdf604..9acb0be84674c 100644
@@ -117,9 +117,7 @@ static irqreturn_t function_status_handler(int irq, void *data)
 
 	status = val;
 	for_each_set_bit(mask, &status, BITS_PER_BYTE) {
-		mask = 1 << mask;
-
-		switch (mask) {
+		switch (BIT(mask)) {
 		case SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION:
 			//FIXME: Add init writes
 			break;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/5 ASoC: SDCA: mask Function_Status value

In reply to Charles Keepax

From: Maciej Strozek <[email protected]>

According to the SDCA specification [1], when writing Function_Status during handling this control, the value should mask off bit 7.

[1] MIPI Specification for SoundWire Device Class for Audio, version 1.1, section 7.14.1.3 (Host Software Handling of Function_Status)

---
 sound/soc/sdca/sdca_interrupts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/sdca/sdca_interrupts.c +1 -1
index 9acb0be84674c..0693209ffed1b 100644
@@ -138,7 +138,7 @@ static irqreturn_t function_status_handler(int irq, void *data)
 		}
 	}
 
-	ret = regmap_write(interrupt->function_regmap, reg, val);
+	ret = regmap_write(interrupt->function_regmap, reg, val & 0x7F);
 	if (ret < 0) {
 		dev_err(dev, "failed to clear function status: %d\n", ret);
 		goto error;
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/5 ASoC: SDCA: Fix cleanup inversion in class driver

In reply to Charles Keepax

From: Richard Fitzgerald <[email protected]>

Fix inverted cleanup of the SoundWire IRQ and the function drivers that use it.

The devm cleanup function to call sdca_dev_unregister_functions() was being registered at the end of class_sdw_probe(). The bus core creates the parent SoundWire IRQ handler after class_sdw_probe() has returned, and it registers a devm cleanup handler at the same time.

This led to a cleanup inversion where the devm cleanup for the parent Soundwire IRQ runs before the handler that removes the function drivers. So the parent IRQ is destroyed before the function drivers had a chance to do any cleanup and remove their IRQ handlers.

Move the registrations of the function driver cleanup into class_boot_work() after the function drivers are registered, so that it runs before the cleanup of the parent SoundWire IRQ handler.

Fixes 2d877d0659cb ("ASoC: SDCA: Add basic SDCA class driver")
---
 sound/soc/sdca/sdca_class.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
sound/soc/sdca/sdca_class.c +14 -13
index 7af4e5d1b347f..732737f969891 100644
@@ -137,6 +137,15 @@ static const struct regmap_config class_dev_regmap_config = {
 	.unlock			= class_regmap_unlock,
 };
 
+static void class_remove_functions(void *data)
+{
+	struct sdca_class_drv *drv = data;
+
+	cancel_work_sync(&drv->boot_work);
+
+	sdca_dev_unregister_functions(drv->sdw);
+}
+
 static void class_boot_work(struct work_struct *work)
 {
 	struct sdca_class_drv *drv = container_of(work,
@@ -157,6 +166,11 @@ static void class_boot_work(struct work_struct *work)
 	if (ret)
 		goto err;
 
+	/* Ensure function drivers are removed before the IRQ is destroyed */
+	ret = devm_add_action_or_reset(drv->dev, class_remove_functions, drv);
+	if (ret)
+		goto err;
+
 	dev_dbg(drv->dev, "boot work complete\n");
 
 	pm_runtime_mark_last_busy(drv->dev);
@@ -168,15 +182,6 @@ static void class_boot_work(struct work_struct *work)
 	pm_runtime_put_sync(drv->dev);
 }
 
-static void class_dev_remove(void *data)
-{
-	struct sdca_class_drv *drv = data;
-
-	cancel_work_sync(&drv->boot_work);
-
-	sdca_dev_unregister_functions(drv->sdw);
-}
-
 static int class_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *id)
 {
 	struct device *dev = &sdw->dev;
@@ -230,10 +235,6 @@ static int class_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *id
 	if (ret)
 		return ret;
 
-	ret = devm_add_action_or_reset(dev, class_dev_remove, drv);
-	if (ret)
-		return ret;
-
 	queue_work(system_long_wq, &drv->boot_work);
 
 	return 0;
Signature · 1 line
2.47.3
maintainer
#4 lore raw

Re: [PATCH 3/5] ASoC: SDCA: Fix cleanup inversion in class driver

Quoted 19 lines · Charles Keepax

This led to a cleanup inversion where the devm cleanup for the parent Soundwire IRQ runs before the handler that removes the function drivers. So the parent IRQ is destroyed before the function drivers had a chance to do any cleanup and remove their IRQ handlers.

Move the registrations of the function driver cleanup into class_boot_work() after the function drivers are registered, so that it runs before the cleanup of the parent SoundWire IRQ handler.

sound/soc/sdca/sdca_class.c +8 -0
+static void class_remove_functions(void *data)
+{
+	struct sdca_class_drv *drv = data;
+
+	cancel_work_sync(&drv->boot_work);
+
+	sdca_dev_unregister_functions(drv->sdw);
+}

sound/soc/sdca/sdca_class.c static void class_boot_work(struct work_struct *work) · line 173

Quoted 8 lines
sound/soc/sdca/sdca_class.c +5 -0
@@ -157,6 +166,11 @@ static void class_boot_work(struct work_struct *work)
 	if (ret)
 		goto err;
 
+	/* Ensure function drivers are removed before the IRQ is destroyed */
+	ret = devm_add_action_or_reset(drv->dev, class_remove_functions, drv);
+	if (ret)
+		goto err;
+

This means we register the cleanup of boot_work from boot_work which seems a bit fun and...

sound/soc/sdca/sdca_class.c static void class_boot_work(struct work_struct *work) · line 175

Quoted 5 lines
sound/soc/sdca/sdca_class.c +0 -5
-static void class_dev_remove(void *data)
-{
-	struct sdca_class_drv *drv = data;
-
-	cancel_work_sync(&drv->boot_work);

...what now cancels boot_work if we somehow manage to remove before boot_work has completed (possibly even before it's scheduled)?

author maintainer
#5 lore raw

Re: [PATCH 3/5] ASoC: SDCA: Fix cleanup inversion in class driver

Quoted 33 lines · Mark Brown
Quoted 19 lines · Charles Keepax

This led to a cleanup inversion where the devm cleanup for the parent Soundwire IRQ runs before the handler that removes the function drivers. So the parent IRQ is destroyed before the function drivers had a chance to do any cleanup and remove their IRQ handlers.

Move the registrations of the function driver cleanup into class_boot_work() after the function drivers are registered, so that it runs before the cleanup of the parent SoundWire IRQ handler.

sound/soc/sdca/sdca_class.c +8 -0
+static void class_remove_functions(void *data)
+{
+	struct sdca_class_drv *drv = data;
+
+	cancel_work_sync(&drv->boot_work);
+
+	sdca_dev_unregister_functions(drv->sdw);
+}

sound/soc/sdca/sdca_class.c static void class_boot_work(struct work_struct *work) · line 173

Quoted 8 lines
sound/soc/sdca/sdca_class.c +5 -0
@@ -157,6 +166,11 @@ static void class_boot_work(struct work_struct *work)
 	if (ret)
 		goto err;
 
+	/* Ensure function drivers are removed before the IRQ is destroyed */
+	ret = devm_add_action_or_reset(drv->dev, class_remove_functions, drv);
+	if (ret)
+		goto err;
+

This means we register the cleanup of boot_work from boot_work which seems a bit fun and...

Quoted 8 lines

sound/soc/sdca/sdca_class.c static void class_boot_work(struct work_struct *work) · line 175

Quoted 5 lines
sound/soc/sdca/sdca_class.c +0 -5
-static void class_dev_remove(void *data)
-{
-	struct sdca_class_drv *drv = data;
-
-	cancel_work_sync(&drv->boot_work);

...what now cancels boot_work if we somehow manage to remove before boot_work has completed (possibly even before it's scheduled)?

Hm... yeah I think you are right there is still an issue here.

I think the boot work sync should stay in the original remove callback. The sequence of concern was:

1) driver probes
2) boot work runs
3) boot work is scheduled
4) driver removed
5) boot work completes

I think this current patch addresses calling sdca_dev_unregister_functions() but we don't really want any of boot work running once the driver starts being removed.

I will respin a v2.

Thanks,
Charles

author maintainer
#6 lore raw

PATCH 4/5 ASoC: SDCA: Unregister IRQ handlers on module remove

In reply to Charles Keepax

From: Richard Fitzgerald <[email protected]>

Ensure that all interrupt handlers are unregistered before the parent regmap_irq is unregistered.

sdca_irq_cleanup() was only called from the component_remove(). If the module was loaded and removed without ever being component probed the FDL interrupts would not be unregistered and this would hit a WARN when devm called regmap_del_irq_chip() during the removal of the parent IRQ.

Fixes 4e53116437e9 ("ASoC: SDCA: Fix errors in IRQ cleanup")
---
 include/sound/sdca_interrupts.h      |  4 ++--
 sound/soc/sdca/sdca_class_function.c | 10 +++++++++-
 sound/soc/sdca/sdca_interrupts.c     |  7 +++----
 3 files changed, 14 insertions(+), 7 deletions(-)
include/sound/sdca_interrupts.h +2 -2
index b47003c3d26ef..a515cc3df0971 100644
@@ -83,8 +83,8 @@ int sdca_irq_populate_early(struct device *dev, struct regmap *function_regmap,
 int sdca_irq_populate(struct sdca_function_data *function,
 		      struct snd_soc_component *component,
 		      struct sdca_interrupt_info *info);
-void sdca_irq_cleanup(struct sdca_function_data *function,
-		      struct snd_soc_component *component,
+void sdca_irq_cleanup(struct device *dev,
+		      struct sdca_function_data *function,
 		      struct sdca_interrupt_info *info);
 struct sdca_interrupt_info *sdca_irq_allocate(struct device *dev,
 					      struct regmap *regmap, int irq);
sound/soc/sdca/sdca_class_function.c +9 -1
index 61b725cd52056..31fc08d513077 100644
@@ -203,7 +203,7 @@ static void class_function_component_remove(struct snd_soc_component *component)
 	struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
 	struct sdca_class_drv *core = drv->core;
 
-	sdca_irq_cleanup(drv->function, component, core->irq_info);
+	sdca_irq_cleanup(component->dev, drv->function, core->irq_info);
 }
 
 static int class_function_set_jack(struct snd_soc_component *component,
@@ -417,6 +417,13 @@ static int class_function_probe(struct auxiliary_device *auxdev,
 	return 0;
 }
 
+static void class_function_remove(struct auxiliary_device *auxdev)
+{
+	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
+
+	sdca_irq_cleanup(drv->dev, drv->function, drv->core->irq_info);
+}
+
 static int class_function_runtime_suspend(struct device *dev)
 {
 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
@@ -569,6 +576,7 @@ static struct auxiliary_driver class_function_drv = {
 	},
 
 	.probe		= class_function_probe,
+	.remove		= class_function_remove,
 	.id_table	= class_function_id_table
 };
 module_auxiliary_driver(class_function_drv);
sound/soc/sdca/sdca_interrupts.c +3 -4
index 0693209ffed1b..5cdabf8ae9da3 100644
@@ -555,17 +555,16 @@ EXPORT_SYMBOL_NS_GPL(sdca_irq_populate, "SND_SOC_SDCA");
 
 /**
  * sdca_irq_cleanup - Free all the individual IRQs for an SDCA Function
+ * @sdev: Device pointer against which the sdca_interrupt_info was allocated.
  * @function: Pointer to the SDCA Function.
- * @component: Pointer to the ASoC component for the Function.
  * @info: Pointer to the SDCA interrupt info for this device.
  *
  * Typically this would be called from the driver for a single SDCA Function.
  */
-void sdca_irq_cleanup(struct sdca_function_data *function,
-		      struct snd_soc_component *component,
+void sdca_irq_cleanup(struct device *dev,
+		      struct sdca_function_data *function,
 		      struct sdca_interrupt_info *info)
 {
-	struct device *dev = component->dev;
 	int i;
 
 	guard(mutex)(&info->irq_lock);
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 5/5 ASoC: SDCA: Tidy up irq_enable_flags()/sdca_irq_disable()

In reply to Charles Keepax

In irq_enable_flags() and sdca_irq_disable() there is a NULL check on the interrupt data pointer, however this is just pulled from an array so can never be NULL. This was likely left over from an earlier version that looked up the data in a different way. Replace the check with checking for the IRQ itself being non-zero.

Whilst here also drop the sdca_interrupt structure down into the loop within the function to better match the style of the rest of the code in this file.

---
 sound/soc/sdca/sdca_interrupts.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
sound/soc/sdca/sdca_interrupts.c +4 -6
index 5cdabf8ae9da3..279926e1d4c1c 100644
@@ -630,13 +630,12 @@ EXPORT_SYMBOL_NS_GPL(sdca_irq_allocate, "SND_SOC_SDCA");
 static void irq_enable_flags(struct sdca_function_data *function,
 			     struct sdca_interrupt_info *info, bool early)
 {
-	struct sdca_interrupt *interrupt;
 	int i;
 
 	for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
-		interrupt = &info->irqs[i];
+		struct sdca_interrupt *interrupt = &info->irqs[i];
 
-		if (!interrupt || interrupt->function != function)
+		if (!interrupt->irq || interrupt->function != function)
 			continue;
 
 		switch (SDCA_CTL_TYPE(interrupt->entity->type,
@@ -689,13 +688,12 @@ EXPORT_SYMBOL_NS_GPL(sdca_irq_enable, "SND_SOC_SDCA");
 void sdca_irq_disable(struct sdca_function_data *function,
 		      struct sdca_interrupt_info *info)
 {
-	struct sdca_interrupt *interrupt;
 	int i;
 
 	for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
-		interrupt = &info->irqs[i];
+		struct sdca_interrupt *interrupt = &info->irqs[i];
 
-		if (!interrupt || interrupt->function != function)
+		if (!interrupt->irq || interrupt->function != function)
 			continue;
 
 		disable_irq(interrupt->irq);
Signature · 1 line
2.47.3