soundwire: code hardening and suspend-resume support
Subsystems (2)
v3 cover soundwire: code hardening and suspend-resume support
this patchset applies on top of "[PATCH v3 00/15] soundwire: intel:
implement new ASoC interfaces".
It implements a series of improvements for:
a) interrupt handling on Intel platforms in MSI mode
b) race conditions on codec probe and enumeration
c) suspend-resume issues (clock-stop mode not supported for now)
d) underflow handling
e) updates to the stream state machine which did not support valid ALSA transitions.
These patches were tested extensively on 4 different platforms and are viewed as required for any sort of SoundWire-based product. While tested extensively on Intel platforms only, they should also benefit Qualcomm platforms who haven't yet enabled power management.
Changes since v2: (no feedback received since November 6) Added idle scheduling to deal with pm_runtime issues when devices are exposed in the DSDT, but are not populated on the board. A quirk is introduced to deal with potential cases where the devices might be powered at a later time, in which case it's legit to leave the bus active.
Fixed .prepare callback to handle both underflow and resume cases. The previous version was incorrect in the first case and did not follow recommended programming sequence
Fixed an additional race condition leading to a timeout when the codec device was suspended while the master remained active. Fixed a couple of warnings reported by static analysis Removed non-essential pr_err() traces in stream.c, left others when useful
Changed subject of patches dealing with race conditions to make sure reviewers can link with the interface changes.
Changes since v1: (no feedback received since October 23) added support for initialization_complete, integration with Realtek codecs exposed an additional race condition between the resume operation and restoration of settings in separate thread triggered by Slave status change.
No other functional change
Bard Liao (3):
soundwire: intel/cadence: fix timeouts in MSI mode
soundwire: stream: only prepare stream when it is configured.
soundwire: intel: reinitialize IP+DSP in .prepare(), but only when
resuming
Pierre-Louis Bossart (19):
soundwire: bus: fix race condition with probe_complete signaling
soundwire: bus: add PM/no-PM versions of read/write functions
soundwire: bus: write Slave Device Number without runtime_pm
soundwire: intel: add helpers for link power down and shim wake
soundwire: intel: Add basic power management support
soundwire: intel: add pm_runtime support
soundwire: intel: reset pm_runtime status during system resume
soundwire: bus: add helper to reset Slave status to UNATTACHED
soundwire: intel: call helper to reset Slave states on resume
soundwire: bus: check first if Slaves become UNATTACHED
soundwire: bus: fix race condition with enumeration_complete signaling
soundwire: bus: fix race condition with initialization_complete
signaling
soundwire: bus: fix race condition by tracking UNATTACHED transition
soundwire: intel: disable pm_runtime when removing a master
soundwire: bus: disable pm_runtime in sdw_slave_delete
soundwire: stream: remove redundant pr_err traces
soundwire: stream: update state machine and add state checks
soundwire: stream: do not update parameters during DISABLED-PREPARED
transition
soundwire: intel: pm_runtime idle scheduling
Documentation/driver-api/soundwire/stream.rst | 63 ++- drivers/soundwire/bus.c | 169 +++++++- drivers/soundwire/bus.h | 9 + drivers/soundwire/bus_type.c | 5 + drivers/soundwire/cadence_master.c | 17 +- drivers/soundwire/cadence_master.h | 8 + drivers/soundwire/intel.c | 400 ++++++++++++++++-- drivers/soundwire/intel.h | 2 + drivers/soundwire/intel_init.c | 45 +- drivers/soundwire/slave.c | 4 + drivers/soundwire/stream.c | 71 +++- include/linux/soundwire/sdw.h | 1 + include/linux/soundwire/sdw_intel.h | 4 + 13 files changed, 714 insertions(+), 84 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/22 soundwire: intel/cadence: fix timeouts in MSI mode
From: Bard Liao <[email protected]>
The existing code uses one pair of interrupt handler/thread per link but at the hardware level the interrupt is shared. This works fine for legacy PCI interrupts, but leads to timeouts in MSI (Message-Signaled Interrupt) mode, likely due to edges being lost.
This patch unifies interrupt handling for all links with a single threaded IRQ handler. The handler is simplified to the bare minimum of detecting a SoundWire interrupt, and the thread takes care of dealing with interrupt sources. This partition follows the model used for the SOF IPC on HDaudio platforms, where similar timeout issues were noticed and doing all the interrupt handling/clearing in the thread improved reliability/stability.
Validation results with 4 links active in parallel show a night-and-day improvement with no timeouts noticed even during stress tests. Latency and quality of service are not affected by the change - mostly because events on a SoundWire link are throttled by the bus frame rate (typically 8..48kHz).
--- drivers/soundwire/cadence_master.c | 17 ++++++----- drivers/soundwire/cadence_master.h | 4 +++ drivers/soundwire/intel.c | 27 ++++------------- drivers/soundwire/intel.h | 2 ++ drivers/soundwire/intel_init.c | 45 ++++++++++++++++++++++++++++- include/linux/soundwire/sdw_intel.h | 4 +++ 6 files changed, 69 insertions(+), 30 deletions(-)
drivers/soundwire/cadence_master.c +9 -8
@@ -17,6 +17,7 @@#include <linux/soundwire/sdw.h> #include <sound/pcm_params.h> #include <sound/soc.h> +#include <linux/workqueue.h> #include "bus.h" #include "cadence_master.h"
@@ -745,7 +746,7 @@ irqreturn_t sdw_cdns_irq(int irq, void *dev_id) CDNS_MCP_INT_SLAVE_MASK, 0); int_status &= ~CDNS_MCP_INT_SLAVE_MASK ; - ret =IRQ_WAKE_THREAD ; +schedule_work (&cdns->work); }cdns_writel (cdns,CDNS_MCP_INTSTAT , int_status);
@@ -754,13 +755,14 @@ irqreturn_t sdw_cdns_irq(int irq, void *dev_id)EXPORT_SYMBOL (sdw_cdns_irq);/** -* sdw_cdns_thread() - Cadence irq thread handler -* @irq: irq number -* @dev_id: irq context +* To update slave status in a work since we will need to handle +* other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave +* process. */ -irqreturn_t sdw_cdns_thread (int irq,void *dev_id) +static void cdns_update_slave_status_work (struct work_struct *work) { -struct sdw_cdns *cdns = dev_id; +struct sdw_cdns *cdns = + container_of(work, struct sdw_cdns, work);u32 slave0, slave1;dev_dbg_ratelimited (cdns->dev,"Slave status change\n" );
@@ -777,9 +779,7 @@ irqreturn_t sdw_cdns_thread(int irq, void *dev_id)cdns_updatel (cdns,CDNS_MCP_INTMASK ,CDNS_MCP_INT_SLAVE_MASK ,CDNS_MCP_INT_SLAVE_MASK ); -return IRQ_HANDLED ; } -EXPORT_SYMBOL (sdw_cdns_thread);/* * init routines
@@ -1187,6 +1187,7 @@ int sdw_cdns_probe(struct sdw_cdns *cdns)init_completion (&cdns->tx_complete); cdns->bus.port_ops = &cdns_port_ops; +INIT_WORK (&cdns->work, cdns_update_slave_status_work);return 0 ; }EXPORT_SYMBOL (sdw_cdns_probe);
drivers/soundwire/cadence_master.h +4 -0
@@ -126,6 +126,10 @@ struct sdw_cdns {bool link_up;unsigned int msg_count; + +struct work_struct work; + +struct list_head list; };#define bus_to_cdns (_bus) container_of(_bus, struct sdw_cdns, bus)
drivers/soundwire/intel.c +6 -21
@@ -1095,6 +1095,7 @@ static int intel_master_probe(struct sdw_master_device *md, void *link_ctx) sdw->cdns.msg_count =0 ; sdw->cdns.bus.dev = &md->dev; sdw->cdns.bus.link_id = md->link_id; + sdw->link_res->cdns = &sdw->cdns;sdw_cdns_probe (&sdw->cdns);
@@ -1113,27 +1114,12 @@ static int intel_master_probe(struct sdw_master_device *md, void *link_ctx)return ret; } -if (sdw->cdns.bus.prop.hw_disabled) { -dev_info (&md->dev,"SoundWire master %d is disabled, ignoring\n" , +if (sdw->cdns.bus.prop.hw_disabled) +dev_info (&md->dev, +"SoundWire master %d is disabled, will be ignored\n" , sdw->cdns.bus.link_id); -return 0 ; - } - -/* Acquire IRQ */ - ret =request_threaded_irq (sdw->link_res->irq, - sdw_cdns_irq, sdw_cdns_thread, -IRQF_SHARED ,KBUILD_MODNAME , &sdw->cdns); -if (ret <0 ) { -dev_err (sdw->cdns.dev,"unable to grab IRQ %d, disabling device\n" , - sdw->link_res->irq); - goto err_init; - }return 0 ; - -err_init: -sdw_delete_bus_master (&sdw->cdns.bus); -return ret; }static int intel_master_startup (struct sdw_master_device *md)
@@ -1145,7 +1131,8 @@ static int intel_master_startup(struct sdw_master_device *md) sdw = md->pdata;if (sdw->cdns.bus.prop.hw_disabled) { -dev_info (&md->dev,"SoundWire master %d is disabled, ignoring\n" , +dev_info (&md->dev, +"SoundWire master %d is disabled, ignoring\n" , sdw->cdns.bus.link_id);return 0 ; }
@@ -1190,7 +1177,6 @@ static int intel_master_startup(struct sdw_master_device *md) err_interrupt:sdw_cdns_enable_interrupt (&sdw->cdns, false); err_init: -free_irq (sdw->link_res->irq, sdw);sdw_delete_bus_master (&sdw->cdns.bus);return ret; }
@@ -1204,7 +1190,6 @@ static int intel_master_remove(struct sdw_master_device *md)if (!sdw->cdns.bus.prop.hw_disabled) {intel_debugfs_exit (sdw);sdw_cdns_enable_interrupt (&sdw->cdns, false); -free_irq (sdw->link_res->irq, sdw);snd_soc_unregister_component (sdw->cdns.dev); }sdw_delete_bus_master (&sdw->cdns.bus);
drivers/soundwire/intel.h +2 -0
@@ -25,6 +25,8 @@ struct sdw_intel_link_res {int irq;const struct sdw_intel_ops *ops;struct device *dev; +struct sdw_cdns *cdns; +struct list_head list; };#define SDW_INTEL_QUIRK_MASK_BUS_DISABLE BIT(1)
drivers/soundwire/intel_init.c +44 -1
92 lines not on this page · raw message
include/linux/soundwire/sdw_intel.h +4 -0
@@ -68,6 +68,8 @@ struct sdw_intel_link_res;* @handle: ACPI parent handle * @links: information for each link (controller-specific and kept * opaque here) +* @irq: shared interrupts for all links +* @link_list: list to handle interrupts across all links */ struct sdw_intel_ctx {int count;
@@ -75,6 +77,8 @@ struct sdw_intel_ctx {u32 link_mask;acpi_handle handle;struct sdw_intel_link_res *links; +int irq; +struct list_head link_list; };/**
Signature · 1 line
2.20.1
PATCH v3 3/22 soundwire: bus: add PM/no-PM versions of read/write functions
In reply to Pierre-Louis BossartAdd support for pm_runtime with the appropriate error checks for sdw_write/read functions, e.g. when pm_runtime is not supported.
Also expose internal functions without pm_runtime support, which are required to perform any sort of suspend/resume operation, as well as any enumeration tasks.
--- drivers/soundwire/bus.c | 68 +++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-)
drivers/soundwire/bus.c +52 -16
@@ -317,6 +317,46 @@ int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,return 0 ; } +/* +* Read/Write IO functions. +* no_pm versions can only be called by the bus, e.g. while enumerating or +* handling suspend-resume sequences. +* all clients need to use the pm versions +*/ + +static int +sdw_nread_no_pm (struct sdw_slave *slave,u32 addr,size_t count,u8 *val) +{ +struct sdw_msg msg; +int ret; + + ret =sdw_fill_msg (&msg, slave, addr, count, + slave->dev_num,SDW_MSG_FLAG_READ , val); +if (ret <0 ) +return ret; + +return sdw_transfer (slave->bus, &msg); +} + +static int +sdw_nwrite_no_pm (struct sdw_slave *slave,u32 addr,size_t count,u8 *val) +{ +struct sdw_msg msg; +int ret; + + ret =sdw_fill_msg (&msg, slave, addr, count, + slave->dev_num,SDW_MSG_FLAG_WRITE , val); +if (ret <0 ) +return ret; + +return sdw_transfer (slave->bus, &msg); +} + +int sdw_write_no_pm (struct sdw_slave *slave,u32 addr,u8 value) +{ +return sdw_nwrite_no_pm (slave, addr,1 , &value); +} +/** * sdw_nread() - Read "n" contiguous SDW Slave registers * @slave: SDW Slave
@@ -326,19 +366,17 @@ int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,*/ int sdw_nread (struct sdw_slave *slave,u32 addr,size_t count,u8 *val) { -struct sdw_msg msg;int ret; - ret =sdw_fill_msg (&msg, slave, addr, count, - slave->dev_num,SDW_MSG_FLAG_READ , val); -if (ret <0 ) -return ret; - ret =pm_runtime_get_sync (slave->bus->dev); -if (ret <0 ) +if (ret <0 && ret != -EACCES ) { +pm_runtime_put_noidle (slave->bus->dev);return ret; + } + + ret =sdw_nread_no_pm (slave, addr, count, val); - ret =sdw_transfer (slave->bus, &msg); +pm_runtime_mark_last_busy (slave->bus->dev);pm_runtime_put (slave->bus->dev);return ret;
@@ -354,19 +392,17 @@ EXPORT_SYMBOL(sdw_nread);*/ int sdw_nwrite (struct sdw_slave *slave,u32 addr,size_t count,u8 *val) { -struct sdw_msg msg;int ret; - ret =sdw_fill_msg (&msg, slave, addr, count, - slave->dev_num,SDW_MSG_FLAG_WRITE , val); -if (ret <0 ) -return ret; - ret =pm_runtime_get_sync (slave->bus->dev); -if (ret <0 ) +if (ret <0 && ret != -EACCES ) { +pm_runtime_put_noidle (slave->bus->dev);return ret; + } + + ret =sdw_nwrite_no_pm (slave, addr, count, val); - ret =sdw_transfer (slave->bus, &msg); +pm_runtime_mark_last_busy (slave->bus->dev);pm_runtime_put (slave->bus->dev);return ret;
Signature · 1 line
2.20.1
PATCH v3 4/22 soundwire: bus: write Slave Device Number without runtime_pm
In reply to Pierre-Louis BossartWhile handling the Device0, we can safely use sdw_write_no_pm.
This move will also helps us track that all other usages of sdw_write() happen when the Slave is already enumerated.
--- drivers/soundwire/bus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soundwire/bus.c +1 -1
@@ -513,7 +513,7 @@ static int sdw_assign_device_num(struct sdw_slave *slave) slave->dev_num =0 ; } - ret =sdw_write (slave,SDW_SCP_DEVNUMBER , dev_num); + ret =sdw_write_no_pm (slave,SDW_SCP_DEVNUMBER , dev_num);if (ret <0 ) {dev_err (&slave->dev,"Program device_num %d failed: %d\n" , dev_num, ret);
Signature · 1 line
2.20.1
PATCH v3 6/22 soundwire: intel: Add basic power management support
In reply to Pierre-Louis BossartImplement suspend/resume capabilities (not runtime_pm for now) The resume part is essentially a full-blown re-enumeration.
When S0ix is supported, we will select clock stop mode when the ACPI target state is S0, and tear down the link for S3.
--- drivers/soundwire/intel.c | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
drivers/soundwire/intel.c +75 -0
@@ -1253,10 +1253,85 @@ static int intel_master_remove(struct sdw_master_device *md)return 0 ; } +/* +* PM calls +*/ + +#ifdef CONFIG_PM + +static int intel_suspend (struct device *dev) +{ +struct sdw_cdns *cdns =dev_get_drvdata (dev); +struct sdw_intel *sdw =cdns_to_intel (cdns); +int ret; + +if (cdns->bus.prop.hw_disabled) { +dev_dbg (dev,"SoundWire master %d is disabled, ignoring\n" , + cdns->bus.link_id); +return 0 ; + } + + ret =sdw_cdns_enable_interrupt (cdns, false); +if (ret <0 ) { +dev_err (dev,"cannot disable interrupts on suspend\n" ); +return ret; + } + + ret =intel_link_power_down (sdw); +if (ret) { +dev_err (dev,"Link power down failed: %d" , ret); +return ret; + } + +intel_shim_wake (sdw, false); + +return 0 ; +} + +static int intel_resume (struct device *dev) +{ +struct sdw_cdns *cdns =dev_get_drvdata (dev); +struct sdw_intel *sdw =cdns_to_intel (cdns); +int ret; + +if (cdns->bus.prop.hw_disabled) { +dev_dbg (dev,"SoundWire master %d is disabled, ignoring\n" , + cdns->bus.link_id); +return 0 ; + } + + ret =intel_init (sdw); +if (ret) { +dev_err (dev,"%s failed: %d" , __func__, ret); +return ret; + } + + ret =sdw_cdns_enable_interrupt (cdns, true); +if (ret <0 ) { +dev_err (dev,"cannot enable interrupts during resume\n" ); +return ret; + } + + ret =sdw_cdns_exit_reset (cdns); +if (ret <0 ) { +dev_err (dev,"unable to exit bus reset sequence during resume\n" ); +return ret; + } + +return ret; +} + +#endif + +static const struct dev_pm_ops intel_pm = { +SET_SYSTEM_SLEEP_PM_OPS (intel_suspend, intel_resume) +}; +struct sdw_md_driver intel_sdw_driver = { .driver = { .name ="intel-sdw" , .owner =THIS_MODULE , + .pm = &intel_pm, }, .probe = intel_master_probe, .startup = intel_master_startup,
Signature · 1 line
2.20.1
PATCH v3 7/22 soundwire: intel: add pm_runtime support
In reply to Pierre-Louis BossartAdd basic hooks in DAI .startup and .shutdown callbacks.
The SoundWire IP should be powered between those two calls. The power dependencies between SoundWire and DSP are handled with the parent/child relationship, before the SoundWire master device becomes active the parent device will become active and power-up the shared rails.
For now the strategy is to rely on complete enumeration when the device becomes active, so the code is a copy/paste of the sequence for system suspend/resume. In future patches, the strategy will optionally be to rely on clock stop if the enumeration time is prohibitive or when the devices connected to a link can signal a wake.
A module parameter is added to make integration of new Slave devices easier, to e.g. keep the device active or prevent clock-stop.
--- drivers/soundwire/intel.c | 108 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-)
drivers/soundwire/intel.c +104 -4
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/interrupt.h> #include <linux/io.h> +#include <linux/pm_runtime.h> #include <linux/platform_device.h> #include <sound/pcm_params.h> #include <sound/soc.h>
@@ -21,6 +22,20 @@#include "bus.h" #include "intel.h" +/* +* debug/config flags for the Intel SoundWire Master. +* +* Since we may have multiple masters active, we can have up to 8 +* flags reused in each byte, with master0 using the ls-byte, etc. +*/ + +#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0) +#define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1) + +static int md_flags; +module_param_named (sdw_md_flags, md_flags, int,0444 ); +MODULE_PARM_DESC (sdw_md_flags,"SoundWire Intel Master device flags (0x0 all off)" ); +/* Intel SHIM Registers Definition */ #define SDW_SHIM_LCAP 0x0#define SDW_SHIM_LCTL 0x4
@@ -742,10 +757,16 @@ static int sdw_stream_setup(struct snd_pcm_substream *substream,static int intel_startup (struct snd_pcm_substream *substream,struct snd_soc_dai *dai) { -/* -* TODO: add pm_runtime support here, the startup callback -* will make sure the IP is 'active' -*/ +struct sdw_cdns *cdns =snd_soc_dai_get_drvdata (dai); +int ret; + + ret =pm_runtime_get_sync (cdns->dev); +if (ret <0 && ret != -EACCES ) { +dev_err_ratelimited (cdns->dev, +"pm_runtime_get_sync failed in %s, ret %d\n" , + __func__, ret); +pm_runtime_put_noidle (cdns->dev); + }return sdw_stream_setup (substream, dai); }
@@ -924,6 +945,7 @@ static void intel_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) {struct sdw_cdns_dma_data *dma; +struct sdw_cdns *cdns =snd_soc_dai_get_drvdata (dai); dma =snd_soc_dai_get_dma_data (dai, substream);if (!dma)
@@ -931,6 +953,9 @@ static void intel_shutdown(struct snd_pcm_substream *substream,snd_soc_dai_set_dma_data (dai, substream,NULL );kfree (dma); + +pm_runtime_mark_last_busy (cdns->dev); +pm_runtime_put_autosuspend (cdns->dev); } static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
@@ -1179,6 +1204,7 @@ static int intel_master_startup(struct sdw_master_device *md) {struct sdw_cdns_stream_config config;struct sdw_intel *sdw; +int link_flags;int ret; sdw = md->pdata;
@@ -1225,6 +1251,17 @@ static int intel_master_startup(struct sdw_master_device *md)intel_debugfs_init (sdw); +/* Enable runtime PM */ + link_flags = md_flags >> (sdw->cdns.bus.link_id *8 ); +if (!(link_flags &SDW_INTEL_MASTER_DISABLE_PM_RUNTIME )) { +pm_runtime_set_autosuspend_delay (&md->dev,3000 ); +pm_runtime_use_autosuspend (&md->dev); +pm_runtime_mark_last_busy (&md->dev); + +pm_runtime_set_active (&md->dev); +pm_runtime_enable (&md->dev); + } +return 0 ; err_interrupt:
@@ -1288,6 +1325,35 @@ static int intel_suspend(struct device *dev)return 0 ; } +static int intel_suspend_runtime (struct device *dev) +{ +struct sdw_cdns *cdns =dev_get_drvdata (dev); +struct sdw_intel *sdw =cdns_to_intel (cdns); +int ret; + +if (cdns->bus.prop.hw_disabled) { +dev_dbg (dev,"SoundWire master %d is disabled, ignoring\n" , + cdns->bus.link_id); +return 0 ; + } + + ret =sdw_cdns_enable_interrupt (cdns, false); +if (ret <0 ) { +dev_err (dev,"cannot disable interrupts on suspend\n" ); +return ret; + } + + ret =intel_link_power_down (sdw); +if (ret) { +dev_err (dev,"Link power down failed: %d" , ret); +return ret; + } + +intel_shim_wake (sdw, false); + +return 0 ; +} +static int intel_resume (struct device *dev) {struct sdw_cdns *cdns =dev_get_drvdata (dev);
@@ -1321,10 +1387,44 @@ static int intel_resume(struct device *dev)return ret; } +static int intel_resume_runtime (struct device *dev) +{ +struct sdw_cdns *cdns =dev_get_drvdata (dev); +struct sdw_intel *sdw =cdns_to_intel (cdns); +int ret; + +if (cdns->bus.prop.hw_disabled) { +dev_dbg (dev,"SoundWire master %d is disabled, ignoring\n" , + cdns->bus.link_id); +return 0 ; + } + + ret =intel_init (sdw); +if (ret) { +dev_err (dev,"%s failed: %d" , __func__, ret); +return ret; + } + + ret =sdw_cdns_enable_interrupt (cdns, true); +if (ret <0 ) { +dev_err (dev,"cannot enable interrupts during resume\n" ); +return ret; + } + + ret =sdw_cdns_exit_reset (cdns); +if (ret <0 ) { +dev_err (dev,"unable to exit bus reset sequence during resume\n" ); +return ret; + } + +return ret; +} +#endif static const struct dev_pm_ops intel_pm = { SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume) +SET_RUNTIME_PM_OPS (intel_suspend_runtime, intel_resume_runtime,NULL ) };struct sdw_md_driver intel_sdw_driver = {
Signature · 1 line
2.20.1
PATCH v3 9/22 soundwire: bus: add helper to reset Slave status to UNATTACHED
In reply to Pierre-Louis BossartWhen resuming, we need to re-enumerate and restart from UNATTACHED.
This will help implement a more robust state machine avoiding race conditions on resume.
--- drivers/soundwire/bus.c | 24 ++++++++++++++++++++++++ drivers/soundwire/bus.h | 2 ++ 2 files changed, 26 insertions(+)
drivers/soundwire/bus.c +24 -0
@@ -1108,3 +1108,27 @@ int sdw_handle_slave_status(struct sdw_bus *bus,return ret; }EXPORT_SYMBOL (sdw_handle_slave_status); + +void sdw_clear_slave_status (struct sdw_bus *bus) +{ +struct sdw_slave *slave; +int i; + +/* Check all non-zero devices */ +for (i =1 ; i <=SDW_MAX_DEVICES ; i++) { +mutex_lock (&bus->bus_lock); +if (test_bit (i, bus->assigned) == false) { +mutex_unlock (&bus->bus_lock); +continue ; + } +mutex_unlock (&bus->bus_lock); + + slave =sdw_get_slave (bus, i); +if (!slave) +continue ; + +if (slave->status !=SDW_SLAVE_UNATTACHED ) +sdw_modify_slave_status (slave,SDW_SLAVE_UNATTACHED ); + } +} +EXPORT_SYMBOL (sdw_clear_slave_status);
drivers/soundwire/bus.h +2 -0
@@ -167,4 +167,6 @@ sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) return sdw_write(slave, addr, tmp); } +void sdw_clear_slave_status (struct sdw_bus *bus); +#endif /* __SDW_BUS_H */
Signature · 1 line
2.20.1
PATCH v3 10/22 soundwire: intel: call helper to reset Slave states on resume
In reply to Pierre-Louis BossartThis helps make sure they are all UNATTACHED and reset the state machines.
At the moment we perform a bus reset both for resume and pm_resume, this will be modified when clock-stop mode is supported
--- drivers/soundwire/intel.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/soundwire/intel.c +6 -0
@@ -1402,6 +1402,9 @@ static int intel_resume(struct device *dev)return ret; } +/* make sure all Slaves are tagged as UNATTACHED */ +sdw_clear_slave_status (&sdw->cdns.bus); + ret =sdw_cdns_enable_interrupt (cdns, true);if (ret <0 ) {dev_err (dev,"cannot enable interrupts during resume\n" );
@@ -1435,6 +1438,9 @@ static int intel_resume_runtime(struct device *dev)return ret; } +/* make sure all Slaves are tagged as UNATTACHED */ +sdw_clear_slave_status (&sdw->cdns.bus); + ret =sdw_cdns_enable_interrupt (cdns, true);if (ret <0 ) {dev_err (dev,"cannot enable interrupts during resume\n" );
Signature · 1 line
2.20.1
PATCH v3 11/22 soundwire: bus: check first if Slaves become UNATTACHED
In reply to Pierre-Louis BossartBefore checking for the presence of Device0, we first need to clean-up the internal state of Slaves that are no longer attached.
--- drivers/soundwire/bus.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/soundwire/bus.c +18 -0
@@ -1034,6 +1034,24 @@ int sdw_handle_slave_status(struct sdw_bus *bus,struct sdw_slave *slave;int i, ret =0 ; +/* first check if any Slaves fell off the bus */ +for (i =1 ; i <=SDW_MAX_DEVICES ; i++) { +mutex_lock (&bus->bus_lock); +if (test_bit (i, bus->assigned) == false) { +mutex_unlock (&bus->bus_lock); +continue ; + } +mutex_unlock (&bus->bus_lock); + + slave =sdw_get_slave (bus, i); +if (!slave) +continue ; + +if (status[i] ==SDW_SLAVE_UNATTACHED && + slave->status !=SDW_SLAVE_UNATTACHED ) +sdw_modify_slave_status (slave,SDW_SLAVE_UNATTACHED ); + } +if (status[0 ] ==SDW_SLAVE_ATTACHED ) {dev_dbg (bus->dev,"Slave attached, programming device number\n" ); ret =sdw_program_device_num (bus);
Signature · 1 line
2.20.1
PATCH v3 13/22 soundwire: bus: fix race condition with initialization_complete signaling
In reply to Pierre-Louis BossartWaiting for the enumeration to be complete may not be enough for a Slave driver, there is a possible race condition between resume operations and initializations handled in an interrupt thread, which can results in settings not being fully restored after system or pm_runtime resume.
This patch builds on the changes added for enumeration_complete, init_completion() is called when the Slave device becomes UNATTACHED, as done with enumeration_complete.
The difference with the enumeration_complete case is that complete() is signaled after the Slave device is fully initialized after the .update_status() callback is called.
A Slave device driver can decide to wait on either of the two complete() cases, depending on its initialization code and requirements.
--- drivers/soundwire/bus.c | 8 ++++++++ drivers/soundwire/slave.c | 1 + 2 files changed, 9 insertions(+)
drivers/soundwire/bus.c +8 -0
@@ -648,6 +648,7 @@ static void sdw_modify_slave_status(struct sdw_slave *slave, __func__, slave->dev_num);init_completion (&slave->enumeration_complete); +init_completion (&slave->initialization_complete); }else if (status ==SDW_SLAVE_ATTACHED ) { dev_dbg(&slave->dev,
@@ -1051,6 +1052,7 @@ int sdw_handle_slave_status(struct sdw_bus *bus, {enum sdw_slave_status prev_status;struct sdw_slave *slave; +bool attached_initializing;int i, ret =0 ;/* first check if any Slaves fell off the bus */
@@ -1096,6 +1098,8 @@ int sdw_handle_slave_status(struct sdw_bus *bus,if (!slave)continue ; + attached_initializing = false; +switch (status[i]) {case SDW_SLAVE_UNATTACHED :if (slave->status ==SDW_SLAVE_UNATTACHED )
@@ -1122,6 +1126,8 @@ int sdw_handle_slave_status(struct sdw_bus *bus,if (prev_status ==SDW_SLAVE_ALERT )break ; + attached_initializing = true; + ret =sdw_initialize_slave (slave);if (ret) dev_err(bus->dev,
@@ -1140,6 +1146,8 @@ int sdw_handle_slave_status(struct sdw_bus *bus,if (ret)dev_err (slave->bus->dev,"Update Slave status failed:%d\n" , ret); +if (attached_initializing) +complete (&slave->initialization_complete); }return ret;
drivers/soundwire/slave.c +1 -0
@@ -53,6 +53,7 @@ static int sdw_slave_add(struct sdw_bus *bus, slave->bus = bus; slave->status =SDW_SLAVE_UNATTACHED ;init_completion (&slave->enumeration_complete); +init_completion (&slave->initialization_complete); slave->dev_num =0 ;init_completion (&slave->probe_complete); slave->probed = false;
Signature · 1 line
2.20.1
PATCH v3 14/22 soundwire: bus: fix race condition by tracking UNATTACHED transition
In reply to Pierre-Louis BossartIn previous patches, we added enumeration_ and initialization_complete fields to avoid race conditions. When streaming restarts, the Master first resumes, then requests all Slaves to renumerate/reinitialized, and then the Slave devices resume.
Intel validation exposed a corner case where the Slave device may transition to D3 when streaming stops, but streaming restarts before the Master transitions to D3. In that case, the Slave status was not cleared as UNATTACHED, and the wait_for_completion will time out.
The proposed solution is that when the Master clears the Slave(s) status, the reason for the Slave(s) becoming unattached is memorized. When the slave resumes, it can check if a Master-initiated re-enumeration and initialization takes place and skip the wait_for_completion() if there is no reason to wait.
--- drivers/soundwire/bus.c | 5 ++++- drivers/soundwire/bus.h | 8 +++++++- drivers/soundwire/intel.c | 16 ++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-)
drivers/soundwire/bus.c +4 -1
@@ -1154,7 +1154,7 @@ int sdw_handle_slave_status(struct sdw_bus *bus, }EXPORT_SYMBOL (sdw_handle_slave_status); -void sdw_clear_slave_status (struct sdw_bus *bus) +void sdw_clear_slave_status (struct sdw_bus *bus,u32 request) {struct sdw_slave *slave;int i;
@@ -1174,6 +1174,9 @@ void sdw_clear_slave_status(struct sdw_bus *bus)if (slave->status !=SDW_SLAVE_UNATTACHED )sdw_modify_slave_status (slave,SDW_SLAVE_UNATTACHED ); + +/* keep track of request, used in pm_runtime resume */ + slave->unattach_request = request; } }EXPORT_SYMBOL (sdw_clear_slave_status);
drivers/soundwire/bus.h +7 -1
@@ -167,6 +167,12 @@ sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) return sdw_write(slave, addr, tmp); } -void sdw_clear_slave_status (struct sdw_bus *bus); +/* +* At the moment we only track Master-initiated hw_reset. +* Additional fields can be added as needed +*/ +#define SDW_UNATTACH_REQUEST_MASTER_RESET BIT(0) + +void sdw_clear_slave_status (struct sdw_bus *bus,u32 request);#endif /* __SDW_BUS_H */
drivers/soundwire/intel.c +12 -4
@@ -1402,8 +1402,12 @@ static int intel_resume(struct device *dev)return ret; } -/* make sure all Slaves are tagged as UNATTACHED */ -sdw_clear_slave_status (&sdw->cdns.bus); +/* +* make sure all Slaves are tagged as UNATTACHED and provide +* reason for reinitialization +*/ +sdw_clear_slave_status (&sdw->cdns.bus, +SDW_UNATTACH_REQUEST_MASTER_RESET ); ret =sdw_cdns_enable_interrupt (cdns, true);if (ret <0 ) {
@@ -1438,8 +1442,12 @@ static int intel_resume_runtime(struct device *dev)return ret; } -/* make sure all Slaves are tagged as UNATTACHED */ -sdw_clear_slave_status (&sdw->cdns.bus); +/* +* make sure all Slaves are tagged as UNATTACHED and provide +* reason for reinitialization +*/ +sdw_clear_slave_status (&sdw->cdns.bus, +SDW_UNATTACH_REQUEST_MASTER_RESET ); ret =sdw_cdns_enable_interrupt (cdns, true);if (ret <0 ) {
Signature · 1 line
2.20.1
PATCH v3 15/22 soundwire: intel: disable pm_runtime when removing a master
In reply to Pierre-Louis BossartPrevent race conditions between remove and resume by disabling pm_runtime.
Note that this only takes care of pm_runtime at the Master level, the same precautions are needed when removing a Slave device.
--- drivers/soundwire/intel.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/soundwire/intel.c +2 -0
@@ -1275,6 +1275,8 @@ static int intel_master_remove(struct sdw_master_device *md) {struct sdw_intel *sdw; +pm_runtime_disable (&md->dev); + sdw = md->pdata;if (!sdw->cdns.bus.prop.hw_disabled) {
Signature · 1 line
2.20.1
PATCH v3 17/22 soundwire: stream: remove redundant pr_err traces
In reply to Pierre-Louis BossartOnly keep pr_err to flag critical configuration errors that will typically only happen during system integration.
For errors on prepare/deprepare/enable/disable, the caller can do a much better job with more information on the DAI and device that caused the issue.
--- drivers/soundwire/stream.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/soundwire/stream.c +0 -8
@@ -1554,8 +1554,6 @@ int sdw_prepare_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); ret =_sdw_prepare_stream (stream); -if (ret <0 ) -pr_err ("Prepare for stream:%s failed: %d\n" , stream->name, ret);sdw_release_bus_lock (stream);return ret;
@@ -1622,8 +1620,6 @@ int sdw_enable_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); ret =_sdw_enable_stream (stream); -if (ret <0 ) -pr_err ("Enable for stream:%s failed: %d\n" , stream->name, ret);sdw_release_bus_lock (stream);return ret;
@@ -1698,8 +1694,6 @@ int sdw_disable_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); ret =_sdw_disable_stream (stream); -if (ret <0 ) -pr_err ("Disable for stream:%s failed: %d\n" , stream->name, ret);sdw_release_bus_lock (stream);return ret;
@@ -1756,8 +1750,6 @@ int sdw_deprepare_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); ret =_sdw_deprepare_stream (stream); -if (ret <0 ) -pr_err ("De-prepare for stream:%d failed: %d\n" , ret, ret);sdw_release_bus_lock (stream);return ret;
Signature · 1 line
2.20.1
PATCH v3 18/22 soundwire: stream: update state machine and add state checks
In reply to Pierre-Louis BossartThe state machine and notes don't accurately explain or allow transitions from STREAM_DEPREPARED and STREAM_DISABLED.
Add more explanations and allow for more transitions as a result of a trigger_stop(), trigger_suspend() and prepare(), depending on the ALSA/ASoC layer behavior defined by the INFO_RESUME and INFO_PAUSE flags.
Also add basic checks to help debug inconsistent states and illegal state machine transitions.
--- Documentation/driver-api/soundwire/stream.rst | 63 +++++++++++++------ drivers/soundwire/stream.c | 37 +++++++++++ 2 files changed, 82 insertions(+), 18 deletions(-)
Documentation/driver-api/soundwire/stream.rst +45 -18
@@ -156,22 +156,27 @@ Below shows the SoundWire stream states and state transition diagram. :: +-----------+ +------------+ +----------+ +----------+ | ALLOCATED +---->| CONFIGURED +---->| PREPARED +---->| ENABLED | | STATE | | STATE | | STATE | | STATE | - +-----------+ +------------+ +----------+ +----+-----+ - ^ - | - | - v - +----------+ +------------+ +----+-----+ + +-----------+ +------------+ +---+--+---+ +----+-----+ + ^ ^ ^ + | | | + __| |___________ | + | | | + v | v + +----------+ +-----+------+ +-+--+-----+ | RELEASED |<----------+ DEPREPARED |<-------+ DISABLED | | STATE | | STATE | | STATE | +----------+ +------------+ +----------+ -NOTE: State transition between prepare and deprepare is supported in Spec -but not in the software (subsystem) +NOTE: State transitions between ``SDW_STREAM_ENABLED`` and +``SDW_STREAM_DISABLED`` are only relevant when then INFO_PAUSE flag is +supported at the ALSA/ASoC level. Likewise the transition between +``SDW_DISABLED_STATE`` and ``SDW_PREPARED_STATE`` depends on the +INFO_RESUME flag. -NOTE2: Stream state transition checks need to be handled by caller -framework, for example ALSA/ASoC. No checks for stream transition exist in -SoundWire subsystem. +NOTE2: The framework implements basic state transition checks, but +does not e.g. check if a transition from DISABLED to ENABLED is valid +on a specific platform. Such tests need to be added at the ALSA/ASoC +level. Stream State Operations -----------------------
@@ -246,6 +251,9 @@ SDW_STREAM_PREPARED Prepare state of stream. Operations performed before entering in this state: + (0) Steps 1 and 2 are omitted in the case of a resume operation, + where the bus bandwidth is known. + (1) Bus parameters such as bandwidth, frame shape, clock frequency, are computed based on current stream as well as already active stream(s) on Bus. Re-computation is required to accommodate current
@@ -270,13 +278,15 @@ Prepare state of stream. Operations performed before entering in this state: After all above operations are successful, stream state is set to ``SDW_STREAM_PREPARED``. -Bus implements below API for PREPARE state which needs to be called once per -stream. From ASoC DPCM framework, this stream state is linked to -.prepare() operation. +Bus implements below API for PREPARE state which needs to be called +once per stream. From ASoC DPCM framework, this stream state is linked +to .prepare() operation. Since the .trigger() operations may not +follow the .prepare(), a direct transitions from +``SDW_STREAM_PREPARED`` to ``SDW_STREAM_DEPREPARED`` is allowed. .. code-block:: c - int sdw_prepare_stream(struct sdw_stream_runtime * stream); + int sdw_prepare_stream(struct sdw_stream_runtime * stream, bool resume); SDW_STREAM_ENABLED
@@ -332,6 +342,14 @@ Bus implements below API for DISABLED state which needs to be called once per stream. From ASoC DPCM framework, this stream state is linked to .trigger() stop operation. +When the INFO_PAUSE flag is supported, a direct transition to +``SDW_STREAM_ENABLED`` is allowed. + +For resume operations where ASoC will use the .prepare() callback, the +stream can transition from ``SDW_STREAM_DISABLED`` to +``SDW_STREAM_PREPARED``, with all required settings restored but +without updating the bandwidth and bit allocation. + .. code-block:: c int sdw_disable_stream(struct sdw_stream_runtime * stream);
@@ -353,9 +371,18 @@ state: After all above operations are successful, stream state is set to ``SDW_STREAM_DEPREPARED``. -Bus implements below API for DEPREPARED state which needs to be called once -per stream. From ASoC DPCM framework, this stream state is linked to -.trigger() stop operation. +Bus implements below API for DEPREPARED state which needs to be called +once per stream. ALSA/ASoC do not have a concept of 'deprepare', and +the mapping from this stream state to ALSA/ASoC operation may be +implementation specific. + +When the INFO_PAUSE flag is supported, the stream state is linked to +the .hw_free() operation - the stream is not deprepared on a +TRIGGER_STOP. + +Other implementations may transition to the ``SDW_STREAM_DEPREPARED`` +state on TRIGGER_STOP, should they require a transition through the +``SDW_STREAM_PREPARED`` state. .. code-block:: c
drivers/soundwire/stream.c +37 -0
@@ -1553,8 +1553,18 @@ int sdw_prepare_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); +if (stream->state !=SDW_STREAM_CONFIGURED && + stream->state !=SDW_STREAM_DEPREPARED && + stream->state !=SDW_STREAM_DISABLED ) { +pr_err ("%s: %s: inconsistent state state %d\n" , + __func__, stream->name, stream->state); + ret = -EINVAL ; + goto state_err; + } + ret =_sdw_prepare_stream (stream); +state_err:sdw_release_bus_lock (stream);return ret; }
@@ -1619,8 +1629,17 @@ int sdw_enable_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); +if (stream->state !=SDW_STREAM_PREPARED && + stream->state !=SDW_STREAM_DISABLED ) { +pr_err ("%s: %s: inconsistent state state %d\n" , + __func__, stream->name, stream->state); + ret = -EINVAL ; + goto state_err; + } + ret =_sdw_enable_stream (stream); +state_err:sdw_release_bus_lock (stream);return ret; }
@@ -1693,8 +1712,16 @@ int sdw_disable_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); +if (stream->state !=SDW_STREAM_ENABLED ) { +pr_err ("%s: %s: inconsistent state state %d\n" , + __func__, stream->name, stream->state); + ret = -EINVAL ; + goto state_err; + } + ret =_sdw_disable_stream (stream); +state_err:sdw_release_bus_lock (stream);return ret; }
@@ -1749,8 +1776,18 @@ int sdw_deprepare_stream(struct sdw_stream_runtime *stream) }sdw_acquire_bus_lock (stream); + +if (stream->state !=SDW_STREAM_PREPARED && + stream->state !=SDW_STREAM_DISABLED ) { +pr_err ("%s: %s: inconsistent state state %d\n" , + __func__, stream->name, stream->state); + ret = -EINVAL ; + goto state_err; + } + ret =_sdw_deprepare_stream (stream); +state_err:sdw_release_bus_lock (stream);return ret; }
Signature · 1 line
2.20.1
PATCH v3 21/22 soundwire: intel: reinitialize IP+DSP in .prepare(), but only when resuming
In reply to Pierre-Louis BossartFrom: Bard Liao <[email protected]>
The .prepare() callback is invoked for normal streaming, underflows or during the system resume transition. In the latter case, the context for the ALH PDIs is lost, and the DSP is not initialized properly either, but the bus parameters don't need to be recomputed.
Conversely, when doing a regular .prepare() during an underflow, the ALH/SHIM registers shall not be changed as the hardware cannot be reprogrammed after the DMA started (hardware spec requirement).
This patch adds storage of PDI and hw_params in the DAI dma context, and the difference between the types of .prepare() usages is handled via a simple boolean, updated when suspending, and tested for in the .prepare() case.
--- drivers/soundwire/cadence_master.h | 4 ++ drivers/soundwire/intel.c | 69 +++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-)
drivers/soundwire/cadence_master.h +4 -0
@@ -81,6 +81,8 @@ struct sdw_cdns_stream_config {* @bus: Bus handle * @stream_type: Stream type * @link_id: Master link id +* @hw_params: hw_params to be applied in .prepare step +* @suspended: status set when suspended, to be used in .prepare */ struct sdw_cdns_dma_data {char *name;
@@ -89,6 +91,8 @@ struct sdw_cdns_dma_data {struct sdw_bus *bus;enum sdw_stream_type stream_type;int link_id; +struct snd_pcm_hw_params *hw_params; +bool suspended; };/**
drivers/soundwire/intel.c +68 -1
106 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v3 22/22 soundwire: intel: pm_runtime idle scheduling
In reply to Pierre-Louis BossartAdd quirk and pm_runtime idle scheduling to let the Master suspend if no Slaves become attached. This can happen when a link is not marked as disabled and has devices exposed in the DSDT, if the power is controlled by sideband means or the link includes a pluggable connector.
--- drivers/soundwire/intel.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/soundwire/intel.c +24 -2
@@ -29,8 +29,9 @@* flags reused in each byte, with master0 using the ls-byte, etc. */ -#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0) -#define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1) +#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0) +#define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1) +#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)static int md_flags;module_param_named (sdw_md_flags, md_flags, int,0444 );
@@ -1329,6 +1330,22 @@ static int intel_master_startup(struct sdw_master_device *md)pm_runtime_enable (&md->dev); } +/* +* Slave devices have an pm_runtime of 'Unsupported' until +* they report as ATTACHED. If they don't, e.g. because there +* are no Slave devices populated or if the power-on is +* delayed or dependent on a power switch, the Master will +* remain active and prevent its parent from suspending. +* +* Conditionally force the pm_runtime core to re-evaluate the +* Master status in the absence of any Slave activity. A quirk +* is provided to e.g. deal with Slaves that may be powered on +* with a delay. A more complete solution would require the +* definition of Master properties. +*/ +if (!(link_flags &SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE )) +pm_runtime_idle (&md->dev); +return 0 ; err_interrupt:
@@ -1443,6 +1460,7 @@ static int intel_resume(struct device *dev)struct sdw_master_device *md =to_sdw_master_device (dev);struct sdw_cdns *cdns =dev_get_drvdata (dev);struct sdw_intel *sdw =cdns_to_intel (cdns); +int link_flags;int ret;if (cdns->bus.prop.hw_disabled) {
@@ -1463,6 +1481,10 @@ static int intel_resume(struct device *dev)pm_runtime_enable (dev); md->pm_runtime_suspended = false; + + link_flags = md_flags >> (sdw->cdns.bus.link_id *8 ); +if (!(link_flags &SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE )) +pm_runtime_idle (&md->dev); } ret =intel_init (sdw);
Signature · 1 line
2.20.1
PATCH v3 20/22 soundwire: stream: do not update parameters during DISABLED-PREPARED transition
In reply to Pierre-Louis BossartAfter a system suspend, the ALSA/ASoC core will invoke the .prepare() callback and a TRIGGER_START when INFO_RESUME is not supported.
Likewise, when an underflow occurs, the .prepare callback will be invoked.
In both cases, the stream can be in DISABLED mode, and will transition into the PREPARED mode. We however don't want the bus bandwidth to be recomputed.
--- drivers/soundwire/stream.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
drivers/soundwire/stream.c +17 -2
@@ -1460,7 +1460,8 @@ static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) } } -static int _sdw_prepare_stream (struct sdw_stream_runtime *stream) +static int _sdw_prepare_stream (struct sdw_stream_runtime *stream, +bool update_params) {struct sdw_master_runtime *m_rt;struct sdw_bus *bus =NULL ;
@@ -1480,6 +1481,9 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)return -EINVAL ; } +if (!update_params) + goto program_params; +/* Increment cumulative bus bandwidth */ /* TODO: Update this during Device-Device support */ bus->params.bandwidth += m_rt->stream->params.rate *
@@ -1495,6 +1499,7 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream) } } +program_params:/* Program params */ ret =sdw_program_params (bus);if (ret <0 ) {
@@ -1544,6 +1549,7 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)*/ int sdw_prepare_stream (struct sdw_stream_runtime *stream) { +bool update_params = true;int ret;if (!stream) {
@@ -1567,7 +1573,16 @@ int sdw_prepare_stream(struct sdw_stream_runtime *stream) goto state_err; } - ret =_sdw_prepare_stream (stream); +/* +* when the stream is DISABLED, this means sdw_prepare_stream() +* is called as a result of an underflow or a resume operation. +* In this case, the bus parameters shall not be recomputed, but +* still need to be re-applied +*/ +if (stream->state ==SDW_STREAM_DISABLED ) + update_params = false; + + ret =_sdw_prepare_stream (stream, update_params); state_err:sdw_release_bus_lock (stream);
Signature · 1 line
2.20.1
PATCH v3 19/22 soundwire: stream: only prepare stream when it is configured.
In reply to Pierre-Louis BossartFrom: Bard Liao <[email protected]>
We don't need to prepare the stream again if the stream is already prepared.
sdw_prepare_stream() could be called multiple times without calling sdw_deprepare_stream(). We call sdw_prepare_stream() in the prepare dai ops and sdw_deprepare_stream() in the hw_free dai ops. If an xrun happens, sdw_prepare_stream() will be called but sdw_deprepare_stream() will not, which results in an imbalance and an invalid total bandwidth.
--- drivers/soundwire/stream.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/soundwire/stream.c +6 -1
@@ -1544,7 +1544,7 @@ static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)*/ int sdw_prepare_stream (struct sdw_stream_runtime *stream) { -int ret =0 ; +int ret;if (!stream) {pr_err ("SoundWire: Handle not found for stream\n" );
@@ -1553,6 +1553,11 @@ int sdw_prepare_stream(struct sdw_stream_runtime *stream)sdw_acquire_bus_lock (stream); +if (stream->state ==SDW_STREAM_PREPARED ) { + ret =0 ; + goto state_err; + } +if (stream->state !=SDW_STREAM_CONFIGURED && stream->state !=SDW_STREAM_DEPREPARED && stream->state !=SDW_STREAM_DISABLED ) {
Signature · 1 line
2.20.1
PATCH v3 2/22 soundwire: bus: fix race condition with probe_complete signaling
In reply to Pierre-Louis BossartThe driver probe takes care of basic initialization and is invoked when a Slave becomes attached, after a match between the Slave DevID registers and ACPI/DT entries.
The update_status callback is invoked when a Slave state changes, e.g. when it is assigned a non-zero Device Number and it reports with an ATTACHED/ALERT state.
The state change detection is usually hardware-based and based on the SoundWire frame rate (e.g. double-digit microseconds) while the probe is a pure software operation, which may involve a kernel module load. In corner cases, it's possible that the state changes before the probe completes.
This patch suggests the use of wait_for_completion to avoid races on startup, so that the update_status callback does not rely on invalid pointers/data structures.
--- drivers/soundwire/bus.c | 25 ++++++++++++++++++++++--- drivers/soundwire/bus.h | 1 + drivers/soundwire/bus_type.c | 5 +++++ drivers/soundwire/slave.c | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-)
drivers/soundwire/bus.c +22 -3
@@ -961,10 +961,29 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)static int sdw_update_slave_status (struct sdw_slave *slave,enum sdw_slave_status status) { -if (slave->ops && slave->ops->update_status) -return slave->ops->update_status (slave, status); +unsigned long time; -return 0 ; +if (!slave->probed) { +/* +* the slave status update is typically handled in an +* interrupt thread, which can race with the driver +* probe, e.g. when a module needs to be loaded. +* +* make sure the probe is complete before updating +* status. +*/ + time =wait_for_completion_timeout (&slave->probe_complete, +msecs_to_jiffies (DEFAULT_PROBE_TIMEOUT )); +if (!time) { +dev_err (&slave->dev,"Probe not complete, timed out\n" ); +return -ETIMEDOUT ; + } + } + +if (!slave->ops || !slave->ops->update_status) +return 0 ; + +return slave->ops->update_status (slave, status); }/**
drivers/soundwire/bus.h +1 -0
@@ -5,6 +5,7 @@#define __SDW_BUS_H#define DEFAULT_BANK_SWITCH_TIMEOUT 3000 +#define DEFAULT_PROBE_TIMEOUT 2000int sdw_uevent (struct device *dev,struct kobj_uevent_env *env);
drivers/soundwire/bus_type.c +5 -0
@@ -120,6 +120,11 @@ static int sdw_slave_drv_probe(struct device *dev) slave->bus->clk_stop_timeout =max_t (u32, slave->bus->clk_stop_timeout, slave->prop.clk_stop_timeout); + slave->probed = true; +complete (&slave->probe_complete); + +dev_dbg (dev,"probe complete\n" ); +return 0 ; }
drivers/soundwire/slave.c +2 -0
@@ -53,6 +53,8 @@ static int sdw_slave_add(struct sdw_bus *bus, slave->bus = bus; slave->status =SDW_SLAVE_UNATTACHED ; slave->dev_num =0 ; +init_completion (&slave->probe_complete); + slave->probed = false;mutex_lock (&bus->bus_lock);list_add_tail (&slave->node, &bus->slaves);
Signature · 6 lines
2.20.1 _______________________________________________ Alsa-devel mailing list [email protected] https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
PATCH v3 16/22 soundwire: bus: disable pm_runtime in sdw_slave_delete
In reply to Pierre-Louis BossartBefore removing the slave device, disable pm_runtime to prevent any race condition with the resume being executed after the bus and slave devices are removed.
Since this pm_runtime_disable() is handled in common routines, implementations of Slave drivers do not need to call it in their .remove() routine.
--- drivers/soundwire/bus.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/soundwire/bus.c +2 -0
@@ -113,6 +113,8 @@ static int sdw_delete_slave(struct device *dev, void *data)struct sdw_slave *slave =to_sdw_slave_device (dev);struct sdw_bus *bus = slave->bus; +pm_runtime_disable (dev); +sdw_slave_debugfs_exit (slave);mutex_lock (&bus->bus_lock);
Signature · 1 line
2.20.1
PATCH v3 12/22 soundwire: bus: fix race condition with enumeration_complete signaling
In reply to Pierre-Louis BossartThis patch adds the signaling needed for Slave drivers to wait until the enumeration completes so that race conditions when issuing read/write commands are avoided. The calls for wait_for_completion() will be added in codec drivers in follow-up patches.
The order between init_completion() and complete() is deterministic, the Slave is marked as UNATTACHED either during a Master-initiated HardReset, or when the hardware detects the Slave no longer reports as ATTACHED.
--- drivers/soundwire/bus.c | 19 +++++++++++++++++++ drivers/soundwire/slave.c | 1 + 2 files changed, 20 insertions(+)
drivers/soundwire/bus.c +19 -0
@@ -637,6 +637,25 @@ static void sdw_modify_slave_status(struct sdw_slave *slave, enum sdw_slave_status status) {mutex_lock (&slave->bus->bus_lock); + +dev_vdbg (&slave->dev, +"%s: changing status slave %d status %d new status %d\n" , + __func__, slave->dev_num, slave->status, status); + +if (status ==SDW_SLAVE_UNATTACHED ) { +dev_dbg (&slave->dev, +"%s: initializing completion for Slave %d\n" , + __func__, slave->dev_num); + +init_completion (&slave->enumeration_complete); + + }else if (status ==SDW_SLAVE_ATTACHED ) { +dev_dbg (&slave->dev, +"%s: signaling completion for Slave %d\n" , + __func__, slave->dev_num); + +complete (&slave->enumeration_complete); + } slave->status = status;mutex_unlock (&slave->bus->bus_lock); }
drivers/soundwire/slave.c +1 -0
@@ -52,6 +52,7 @@ static int sdw_slave_add(struct sdw_bus *bus, slave->dev.type = &sdw_slave_type; slave->bus = bus; slave->status =SDW_SLAVE_UNATTACHED ; +init_completion (&slave->enumeration_complete); slave->dev_num =0 ;init_completion (&slave->probe_complete); slave->probed = false;
Signature · 1 line
2.20.1
PATCH v3 8/22 soundwire: intel: reset pm_runtime status during system resume
In reply to Pierre-Louis BossartThe system resume does the entire bus re-initialization and brings it to full-power. If the device was pm_runtime suspended, there is no need to run the pm_runtime resume sequence after the system runtime.
Follow the documentation from runtime_pm.rst, and conditionally disable, set_active and re-enable the device on system resume.
--- drivers/soundwire/intel.c | 30 ++++++++++++++++++++++++++++++ include/linux/soundwire/sdw.h | 1 + 2 files changed, 31 insertions(+)
drivers/soundwire/intel.c +30 -0
@@ -1298,6 +1298,7 @@ static int intel_master_remove(struct sdw_master_device *md)static int intel_suspend (struct device *dev) { +struct sdw_master_device *md =to_sdw_master_device (dev);struct sdw_cdns *cdns =dev_get_drvdata (dev);struct sdw_intel *sdw =cdns_to_intel (cdns);int ret;
@@ -1308,6 +1309,20 @@ static int intel_suspend(struct device *dev)return 0 ; } +if (pm_runtime_status_suspended (dev)) { +dev_dbg (dev, +"%s: pm_runtime status: suspended\n" , + __func__); + +/* +* keep track of the state for the system resume, where +* we will need to reset the pm_runtime status to active +*/ + md->pm_runtime_suspended = true; + +return 0 ; + } + ret =sdw_cdns_enable_interrupt (cdns, false);if (ret <0 ) {dev_err (dev,"cannot disable interrupts on suspend\n" );
@@ -1356,6 +1371,7 @@ static int intel_suspend_runtime(struct device *dev)static int intel_resume (struct device *dev) { +struct sdw_master_device *md =to_sdw_master_device (dev);struct sdw_cdns *cdns =dev_get_drvdata (dev);struct sdw_intel *sdw =cdns_to_intel (cdns);int ret;
@@ -1366,6 +1382,20 @@ static int intel_resume(struct device *dev)return 0 ; } +if (md->pm_runtime_suspended) { +dev_dbg (dev, +"%s: pm_runtime status was suspended, forcing active\n" , + __func__); + +/* follow required sequence from runtime_pm.rst */ +pm_runtime_disable (dev); +pm_runtime_set_active (dev); +pm_runtime_mark_last_busy (dev); +pm_runtime_enable (dev); + + md->pm_runtime_suspended = false; + } + ret =intel_init (sdw);if (ret) {dev_err (dev,"%s failed: %d" , __func__, ret);
include/linux/soundwire/sdw.h +1 -0
@@ -589,6 +589,7 @@ struct sdw_master_device {struct device dev;int link_id;struct sdw_md_driver *driver; +bool pm_runtime_suspended;void *pdata;/* core does not touch */ };
Signature · 1 line
2.20.1
PATCH v3 5/22 soundwire: intel: add helpers for link power down and shim wake
In reply to Pierre-Louis BossartThese routines are required for power management
--- drivers/soundwire/intel.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/soundwire/intel.c +53 -0
@@ -362,6 +362,59 @@ static int intel_shim_init(struct sdw_intel *sdw)return ret; } +static void intel_shim_wake (struct sdw_intel *sdw,bool wake_enable) +{ +void __iomem *shim = sdw->link_res->shim; +unsigned int link_id = sdw->instance; +u16 wake_en, wake_sts; + +if (wake_enable) { +/* Enable the wakeup */ +intel_writew (shim,SDW_SHIM_WAKEEN , + (SDW_SHIM_WAKEEN_ENABLE << link_id)); + }else { +/* Disable the wake up interrupt */ + wake_en =intel_readw (shim,SDW_SHIM_WAKEEN ); + wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id); +intel_writew (shim,SDW_SHIM_WAKEEN , wake_en); + +/* Clear wake status */ + wake_sts =intel_readw (shim,SDW_SHIM_WAKESTS ); + wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id); +intel_writew (shim,SDW_SHIM_WAKESTS_STATUS , wake_sts); + } +} + +static int intel_link_power_down (struct sdw_intel *sdw) +{ +int link_control, spa_mask, cpa_mask, ret; +unsigned int link_id = sdw->instance; +void __iomem *shim = sdw->link_res->shim; +u16 ioctl; + +/* Glue logic */ + ioctl =intel_readw (shim,SDW_SHIM_IOCTL (link_id)); + ioctl |=SDW_SHIM_IOCTL_BKE ; + ioctl |=SDW_SHIM_IOCTL_COE ; +intel_writew (shim,SDW_SHIM_IOCTL (link_id), ioctl); + + ioctl &= ~(SDW_SHIM_IOCTL_MIF ); +intel_writew (shim,SDW_SHIM_IOCTL (link_id), ioctl); + +/* Link power down sequence */ + link_control =intel_readl (shim,SDW_SHIM_LCTL ); + spa_mask = ~(SDW_SHIM_LCTL_SPA << link_id); + cpa_mask = (SDW_SHIM_LCTL_CPA << link_id); + link_control &= spa_mask; + + ret =intel_clear_bit (shim,SDW_SHIM_LCTL , link_control, cpa_mask); +if (ret <0 ) +return ret; + + sdw->cdns.link_up = false; +return 0 ; +} +/* * PDI routines */
Signature · 1 line
2.20.1