soundwire: amd: SoundWire manager driver bug fixes
v8 cover soundwire: amd: SoundWire manager driver bug fixes
This series collects bug fixes for the AMD SoundWire manager driver (ACP6.3/7.0/7.1/7.2). The patches address correctness issues across six areas. Patches 9, 12, 13, 15, 20, and 21 fix issues reported by Lee Jones <[email protected]>.
Wake interrupt handling (patches 1-8): fixes reported on OEM platforms running ACP6.3/7.0/7.1/7.2. Consolidate pad register macros into amd_init.h, add correct SCHMEN/OE programming for ACP 7.0/7.1/7.2, introduce D0/D3 PM pad helpers, track full pad teardown across suspend/resume, fix missing wake interrupt and group wake-arming calls in runtime suspend, fix the IRQ thread dropping STATUS_0TO7 on simultaneous wake and state-change events, remove the spurious STATUS_8TO11 clear in amd_sdw_process_wake_event(), and fix a missing wake interrupt for ACP6.3 in POWER_OFF suspend.
Command response handling (patches 9-11): fix silent widening of -ETIMEDOUT to u64 (reported by Lee Jones), cache the last successful PING result to avoid spurious disconnect warnings on transient timeouts, and add lockdep_assert_held() to enforce the msg_lock contract.
Work-queue and driver lifecycle (patches 12-16): fix drain ordering in the remove path and defer dev_set_drvdata() until after INIT_WORK() to close a concurrent-remove window (both reported by Lee Jones), guard pm_runtime_disable() against unpaired calls, guard IRQ work execution with the interrupt_enabled flag using WRITE_ONCE/READ_ONCE, clear stale ACP_SW_STATE_CHANGE_STATUS registers before re-enabling interrupt delivery, and add explanatory comments in amd_disable_sdw_interrupts() and amd_sdw_irq_thread() (reported by Lee Jones), and fix *sdw_ctx leak when sdw_amd_startup() fails. Companion NULL-check patches are already merged in Mark Brown's ASoC tree:
34ea81a7f284 ("ASoC: SOF: amd: guard adata->sdw dereferences with
NULL check in IRQ handler")
41e06aaef9c0 ("ASoC: amd: ps: guard adata->sdw dereferences with
NULL check in IRQ handler")
Power management (patches 17-20): propagate amd_init_sdw_manager() errors on POWER_OFF resume, drop amd_deinit_sdw_manager() in POWER_OFF suspend, replace the open-coded >= ACP70 comparison with an explicit switch/case, and fix interrupt-gate and work-drain ordering in PM ops (reported by Lee Jones).
Drvdata and register safety (patches 21-23): add a NULL drvdata guard in sdw_amd_startup() (reported by Lee Jones), and fix shared-register read-modify-write races in amd_sdw_set_device_state() and amd_sdw_host_wake_enable() by holding acp_sdw_lock.
Generic bus fix (patch 24): replace pm_runtime_get_sync() with pm_runtime_get_if_active() in sdw_handle_slave_alerts() to fix a deadlock when the parent manager is in RPM_SUSPENDING. The manager's runtime_suspend callback blocks in cancel_work_sync() waiting for the alert work to complete, while the alert work blocks in pm_runtime_get_sync() propagating up to the manager. The fix conditions the early return on pm_runtime_status_suspended() so genuinely suspended slaves defer the alert, while slaves in RPM_RESUMING or RPM_SUSPENDING proceed without a PM reference.
Changes since v7:
- Patch 15: expand beyond the interrupt_enabled guard to also clear stale ACP_SW_STATE_CHANGE_STATUS_0TO7 and _8TO11 registers in amd_enable_sdw_interrupts() before re-enabling interrupt delivery, add STATUS clears in the ACP6.3 POWER_OFF_MODE wake-re-enable block in amd_suspend_runtime(), and add explanatory comments in amd_disable_sdw_interrupts() and amd_sdw_irq_thread(). Rename to "guard IRQ work with interrupt_enabled and clear STATUS".
- Add patch 24: fix deadlock in sdw_handle_slave_alerts() caused by pm_runtime_get_sync() blocking when the parent manager is in RPM_SUSPENDING; replace with pm_runtime_get_if_active() conditioned on pm_runtime_status_suspended().
- Link to v7:
https://lore.kernel.org/linux-sound/[email protected]
Changes since v6:
- Patch 7: drop Fixes tag; the STATUS_8TO11 double-clear is an irq-thread logic bug not attributable to a single introducing commit.
- Patch 16: change approach to publish *sdw_ctx = ctx before sdw_amd_startup() and reset *sdw_ctx = NULL on startup failure before calling sdw_amd_exit(); update commit description to match. With this ordering *sdw_ctx is visible before amd_enable_sdw_interrupts() so the IRQ handler window between interrupt enable and ctx publication is closed.
- Link to v6:
https://lore.kernel.org/linux-sound/[email protected]
Changes since v5:
- Drop patch 10 (return response on IMM status cleanup timeout) and patch 11 (clarify bad-state guard poll omission in IMM_RES_VALID path): dropped after review feedback.
- Patch 2: rename disable_oe to needs_schmen_oe for clarity; update Fixes tags to cite ACP 7.x introduction commits instead of the
ACP 6.3-only refactor commit:
commit 2c0ae8ef1e5e ("soundwire: amd: add support for ACP7.0 & ACP7.1 platforms") commit 34b1cb4ec286 ("soundwire: amd: Add support for acp7.2 platform")
- Patch 4: use "POWER_OFF_MODE runtime suspend" instead of
"wake-armed runtime suspend" for precision.
- Patch 5: add commit description explaining why the early return before wake-arming is correct when amd_deinit_sdw_manager() fails; update Fixes tag to commit dcc48a73eae7 ("soundwire: amd: change the soundwire wake enable/disable sequence").
- Patch 8: update Fixes tag to correct commit SHA.
- Patch 9: drop dev_err_ratelimited() calls for ping status timeout; the error is already tracked via the cache introduced in patch 10.
- Patch 14: update Fixes tag to commit ed5e8741b8db ("soundwire: amd:
refactor amd soundwire manager device node creation"), which introduced the partial-init startup sequence that makes the unconditional pm_runtime_disable() incorrect.
- Patch 15: add WRITE_ONCE() for all stores to interrupt_enabled and READ_ONCE() for the load in amd_sdw_irq_thread(); restore interrupt_enabled in the ACP6.3-specific wake-re-enable block in amd_suspend_runtime() POWER_OFF_MODE; guard IRQ work execution instead of scheduling.
- Patch 16: simplify to single-problem description (ctx leak on startup failure); remove the concurrent IRQ handler race rationale. Two ASoC companion patches addressing the IRQ handler NULL dereference window (adata->sdw dereferenced without NULL check between amd_enable_sdw_interrupts() and *sdw_ctx assignment) have been posted and merged in Mark Brown's ASoC tree:
34ea81a7f284 ("ASoC: SOF: amd: guard adata->sdw dereferences with
NULL check in IRQ handler")
41e06aaef9c0 ("ASoC: amd: ps: guard adata->sdw dereferences with
NULL check in IRQ handler")
- Patch 19: add WRITE_ONCE(interrupt_enabled, true) in the ACP6.3 wake-re-enable block in amd_suspend_runtime() POWER_OFF_MODE.
- Patch 20: expand description to explain the redundant amd_sdw_wake_enable(false) calls and clarify CLK_STOP_MODE
interrupt handling through D3.
- Link to v5:
https://lore.kernel.org/linux-sound/[email protected]/
Changes since v4:
- Add patches 1-8 (OEM-reported wake interrupt fixes for ACP6.3/7.0/7.1/7.2): pad register macro consolidation, SCHMEN/OE programming for ACP 7.0/7.1/7.2, D0/D3 PM helpers, pad teardown tracking, missing wake interrupt in runtime suspend, STATUS_0TO7 race fix (promoted from patch 12/13 in v4), STATUS_8TO11 fix reworked to a targeted removal of the clear in amd_sdw_process_wake_event(), and ACP6.3 POWER_OFF wake fix.
- Add patch 11: clarify bad-state guard poll omission in IMM_RES_VALID path.
- Add patch 15: defer dev_set_drvdata() until after INIT_WORK().
- Add patches 17-18: guard IRQ work with interrupt_enabled flag; delay *sdw_ctx publication until after startup succeeds (supersedes v4 patch 7 ctx-leak fix with a more robust approach).
- Add patches 23-25: NULL drvdata guard in sdw_amd_startup(), and shared-register RMW races in amd_sdw_set_device_state() and amd_sdw_host_wake_enable().
- Drop v4 patch 7 (fix ctx leak when sdw_amd_startup() fails):
superseded by patch 18.
- Link to v4:
https://lore.kernel.org/all/[email protected]/
Changes since v3:
- Add patch 12/13 to fix IRQ thread dropping STATUS_0TO7 when a wake event and a device-state change arrive simultaneously.
- Add patch 13/13 to fix STATUS_8TO11 double-clear race in the wake handler introduced by the simultaneous-wake fix.
- Add lockdep_assert_held() to amd_sdw_read_ping_status() (patch 4/13) to enforce the msg_lock contract machine-checkably.
- Add return response on IMM status cleanup timeout (patch 2/13).
- Update patch 11/13 (fix interrupt gate and work drain ordering in PM ops) to also fix amd_sdw_wake_enable() ordering in both CLK_STOP_MODE
paths: move the call to after amd_enable_sdw_interrupts() so the unconditional interrupt mask write does not clobber the wake bit.
Changes since v2:
- Update comment for ping_slave_stat variable description in patch 3/13.
- Split the remove path work drain and pm ops guard patch into separate patches (patches 5/13 and 6/13) to improve clarity and maintainability.
Changes since v1:
- Rework patch 1 to fix the u64 return type properly via out-parameter.
Vijendar Mukunda (24):
soundwire: amd: consolidate pad register macros into amd_init.h
soundwire: amd: program pad SCHMEN and OE control for ACP 7.0/7.1/7.2
soundwire: amd: add pad ctrl helpers for D0/D3 PM transitions
soundwire: amd: track full pad teardown across suspend/resume
soundwire: amd: fix missing wake interrupt and group wake-arming calls
in runtime suspend
soundwire: amd: fix IRQ thread dropping STATUS_0TO7 on simultaneous
wake
soundwire: amd: remove STATUS_8TO11 clear from
amd_sdw_process_wake_event()
soundwire: amd: fix missing wake interrupt for ACP6.3 in POWER_OFF
suspend
soundwire: amd: fix SDW command timeout return value handling
soundwire: amd: cache ping slave status to avoid spurious disconnect
on timeout
soundwire: amd: assert msg_lock is held in amd_sdw_read_ping_status()
soundwire: amd: fix work drain ordering in remove path
soundwire: amd: defer dev_set_drvdata() until after INIT_WORK()
soundwire: amd: pm_runtime guard in remove path
soundwire: amd: guard IRQ work with interrupt_enabled and clear STATUS
soundwire: amd: fix *sdw_ctx leak when sdw_amd_startup() fails
soundwire: amd: propagate amd_init_sdw_manager() error on resume
soundwire: amd: drop amd_deinit_sdw_manager() in POWER_OFF suspend
soundwire: amd: replace >= ACP70 with explicit switch/case in PM paths
soundwire: amd: fix interrupt gate and work drain ordering in PM ops
soundwire: amd: check for NULL manager drvdata in sdw_amd_startup()
soundwire: amd: fix shared-register RMW race in
amd_sdw_set_device_state()
soundwire: amd: fix shared-register RMW race in
amd_sdw_host_wake_enable()
soundwire: bus: use pm_runtime_get_if_active() in
sdw_handle_slave_alerts()
drivers/soundwire/amd_init.c | 76 ++++-- drivers/soundwire/amd_init.h | 18 ++ drivers/soundwire/amd_manager.c | 372 ++++++++++++++++++++++++------ drivers/soundwire/amd_manager.h | 10 - drivers/soundwire/bus.c | 45 +++- include/linux/soundwire/sdw_amd.h | 15 ++ 6 files changed, 425 insertions(+), 111 deletions(-)
--
2.53.0
PATCH v8 1/24 soundwire: amd: consolidate pad register macros into amd_init.h
Move ACP_PAD_PULLDOWN_CTRL and ACP_SW_PAD_KEEPER_EN from their duplicate definitions in amd_init.c and amd_manager.h into amd_init.h, so a single canonical definition is shared by both translation units.
Drop the eight stale AMD_SDW{0,1}_PAD_*_{ENABLE,DISABLE}_MASK definitions from amd_manager.h; they are no longer referenced anywhere.
No functional change.
--- drivers/soundwire/amd_init.c | 2 -- drivers/soundwire/amd_init.h | 3 +++ drivers/soundwire/amd_manager.h | 10 ---------- 3 files changed, 3 insertions(+), 12 deletions(-)
drivers/soundwire/amd_init.c +0 -2
@@ -16,8 +16,6 @@#include "amd_init.h" -#define ACP_PAD_PULLDOWN_CTRL 0x0001448 -#define ACP_SW_PAD_KEEPER_EN 0x0001454#define AMD_SDW0_PAD_CTRL_MASK 0x60#define AMD_SDW1_PAD_CTRL_MASK 5#define AMD_SDW_PAD_CTRL_MASK (AMD_SDW0_PAD_CTRL_MASK | AMD_SDW1_PAD_CTRL_MASK)
drivers/soundwire/amd_init.h +3 -0
@@ -8,6 +8,9 @@#include <linux/soundwire/sdw_amd.h> +#define ACP_PAD_PULLDOWN_CTRL 0x0001448 +#define ACP_SW_PAD_KEEPER_EN 0x0001454 +int amd_sdw_manager_start (struct amd_sdw_manager *amd_manager);static inline void amd_updatel (void __iomem *mmio,int offset,u32 mask,u32 val)
drivers/soundwire/amd_manager.h +0 -10
@@ -11,8 +11,6 @@#define SDW_MANAGER_REG_OFFSET 0xc00#define AMD_SDW_DEFAULT_ROWS 50#define AMD_SDW_DEFAULT_COLUMNS 10 -#define ACP_PAD_PULLDOWN_CTRL 0x0001448 -#define ACP_SW_PAD_KEEPER_EN 0x0001454#define ACP_SW0_WAKE_EN 0x0001458#define ACP_EXTERNAL_INTR_CNTL0 0x0001a04#define ACP_EXTERNAL_INTR_STAT0 0x0001a0c
@@ -180,14 +178,6 @@#define AMD_DPN_CH_EN_LCTRL GENMASK(2, 0)#define AMD_DPN_CH_EN_CHMASK GENMASK(10, 3)#define AMD_SDW_STAT_MAX_RETRY_COUNT 100 -#define AMD_SDW0_PAD_PULLDOWN_CTRL_ENABLE_MASK 0x7f9f -#define AMD_SDW1_PAD_PULLDOWN_CTRL_ENABLE_MASK 0x7ffa -#define AMD_SDW0_PAD_PULLDOWN_CTRL_DISABLE_MASK 0x60 -#define AMD_SDW1_PAD_PULLDOWN_CTRL_DISABLE_MASK 5 -#define AMD_SDW0_PAD_KEEPER_EN_MASK 1 -#define AMD_SDW1_PAD_KEEPER_EN_MASK 0x10 -#define AMD_SDW0_PAD_KEEPER_DISABLE_MASK 0x1e -#define AMD_SDW1_PAD_KEEPER_DISABLE_MASK 0xf#define AMD_SDW_PREQ_INTR_STAT BIT(19)#define AMD_SDW_CLK_STOP_DONE 1#define AMD_SDW_CLK_RESUME_REQ 2
Signature · 1 line
2.53.0
PATCH v8 2/24 soundwire: amd: program pad SCHMEN and OE control for ACP 7.0/7.1/7.2
In reply to Vijendar MukundaACP 7.0/7.1/7.2 hardware requires SCHMEN control and output-enable (OE) registers to be programmed as part of the pad enable sequence, in addition to the existing keeper and pulldown writes.
Update amd_enable_sdw_pads() to program ACP_PAD_SCHMEN_CTRL and ACP_PAD_DISABLE_OE_CTRL for active pads. Pass acp_rev to amd_enable_sdw_pads() and restrict these writes to ACP 7.0/7.1/7.2; ACP 6.3 is unaffected.
Define per-register masks in amd_init.h replacing generic local defines in amd_init.c.
amd_enable_sdw_pads() targets ACP6.3/7.0/7.1/7.2 only. The default -EINVAL prevents applying this pad sequence to unsupported ACP revisions.
--- drivers/soundwire/amd_init.c | 54 ++++++++++++++++++++++++------------ drivers/soundwire/amd_init.h | 15 ++++++++++ 2 files changed, 51 insertions(+), 18 deletions(-)
drivers/soundwire/amd_init.c +36 -18
@@ -16,37 +16,55 @@#include "amd_init.h" -#define AMD_SDW0_PAD_CTRL_MASK 0x60 -#define AMD_SDW1_PAD_CTRL_MASK 5 -#define AMD_SDW_PAD_CTRL_MASK (AMD_SDW0_PAD_CTRL_MASK | AMD_SDW1_PAD_CTRL_MASK) -#define AMD_SDW0_PAD_EN 1 -#define AMD_SDW1_PAD_EN 0x10 -#define AMD_SDW_PAD_EN (AMD_SDW0_PAD_EN | AMD_SDW1_PAD_EN) - -static int amd_enable_sdw_pads (void __iomem *mmio,u32 link_mask,struct device *dev) +static int amd_enable_sdw_pads (void __iomem *mmio,u32 link_mask,u32 acp_rev,struct device *dev) { -u32 pad_keeper_en, pad_pulldown_ctrl_mask; +u32 pad_keeper_mask, pad_pulldown_mask, pad_schmen_mask, pad_oe_mask; +bool needs_schmen_oe; + +switch (acp_rev) { +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : + needs_schmen_oe = true; +break ; +case ACP63_PCI_REV_ID : + needs_schmen_oe = false; +break ; +default : +dev_err (dev,"unsupported ACP revision 0x%x\n" , acp_rev); +return -EINVAL ; + }switch (link_mask) {case 1 : - pad_keeper_en =AMD_SDW0_PAD_EN ; - pad_pulldown_ctrl_mask =AMD_SDW0_PAD_CTRL_MASK ; + pad_keeper_mask =AMD_SDW0_PAD_KEEPER_EN_MASK ; + pad_pulldown_mask =AMD_SDW0_PAD_PULLDOWN_MASK ; + pad_schmen_mask =AMD_SDW0_PAD_SCHMEN_MASK ; + pad_oe_mask =AMD_SDW0_PAD_OE_CTRL_MASK ;break ;case 2 : - pad_keeper_en =AMD_SDW1_PAD_EN ; - pad_pulldown_ctrl_mask =AMD_SDW1_PAD_CTRL_MASK ; + pad_keeper_mask =AMD_SDW1_PAD_KEEPER_EN_MASK ; + pad_pulldown_mask =AMD_SDW1_PAD_PULLDOWN_MASK ; + pad_schmen_mask =AMD_SDW1_PAD_SCHMEN_MASK ; + pad_oe_mask =AMD_SDW1_PAD_OE_CTRL_MASK ;break ;case 3 : - pad_keeper_en =AMD_SDW_PAD_EN ; - pad_pulldown_ctrl_mask =AMD_SDW_PAD_CTRL_MASK ; + pad_keeper_mask =AMD_SDW_PAD_KEEPER_EN_MASK ; + pad_pulldown_mask =AMD_SDW_PAD_PULLDOWN_MASK ; + pad_schmen_mask =AMD_SDW_PAD_SCHMEN_MASK ; + pad_oe_mask =AMD_SDW_PAD_OE_CTRL_MASK ;break ;default :dev_err (dev,"No SDW Links are enabled\n" );return -ENODEV ; } -amd_updatel (mmio,ACP_SW_PAD_KEEPER_EN , pad_keeper_en, pad_keeper_en); -amd_updatel (mmio,ACP_PAD_PULLDOWN_CTRL , pad_pulldown_ctrl_mask,0 ); +amd_updatel (mmio,ACP_SW_PAD_KEEPER_EN , pad_keeper_mask, pad_keeper_mask); +amd_updatel (mmio,ACP_PAD_PULLDOWN_CTRL , pad_pulldown_mask,0 ); +if (needs_schmen_oe) { +amd_updatel (mmio,ACP_PAD_SCHMEN_CTRL , pad_schmen_mask, pad_schmen_mask); +amd_updatel (mmio,ACP_PAD_DISABLE_OE_CTRL , pad_oe_mask,0 ); + }return 0 ; }
@@ -86,7 +104,7 @@ static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res) count = res->count;dev_dbg (&adev->dev,"Creating %d SDW Link devices\n" , count); - ret =amd_enable_sdw_pads (res->mmio_base, res->link_mask, res->parent); + ret =amd_enable_sdw_pads (res->mmio_base, res->link_mask, res->acp_rev, res->parent);if (ret)return NULL ;
drivers/soundwire/amd_init.h +15 -0
@@ -9,7 +9,22 @@#include <linux/soundwire/sdw_amd.h> #define ACP_PAD_PULLDOWN_CTRL 0x0001448 +#define ACP_PAD_SCHMEN_CTRL 0x0001450#define ACP_SW_PAD_KEEPER_EN 0x0001454 +#define ACP_PAD_DISABLE_OE_CTRL 0x0001468 + +#define AMD_SDW0_PAD_PULLDOWN_MASK (BIT(5) | BIT(6)) +#define AMD_SDW1_PAD_PULLDOWN_MASK (BIT(0) | BIT(2)) +#define AMD_SDW_PAD_PULLDOWN_MASK (AMD_SDW0_PAD_PULLDOWN_MASK | AMD_SDW1_PAD_PULLDOWN_MASK) +#define AMD_SDW0_PAD_KEEPER_EN_MASK BIT(0) +#define AMD_SDW1_PAD_KEEPER_EN_MASK BIT(4) +#define AMD_SDW_PAD_KEEPER_EN_MASK (AMD_SDW0_PAD_KEEPER_EN_MASK | AMD_SDW1_PAD_KEEPER_EN_MASK) +#define AMD_SDW0_PAD_OE_CTRL_MASK (BIT(5) | BIT(6)) +#define AMD_SDW1_PAD_OE_CTRL_MASK (BIT(0) | BIT(2)) +#define AMD_SDW_PAD_OE_CTRL_MASK (AMD_SDW0_PAD_OE_CTRL_MASK | AMD_SDW1_PAD_OE_CTRL_MASK) +#define AMD_SDW0_PAD_SCHMEN_MASK (BIT(5) | BIT(6)) +#define AMD_SDW1_PAD_SCHMEN_MASK (BIT(0) | BIT(2)) +#define AMD_SDW_PAD_SCHMEN_MASK (AMD_SDW0_PAD_SCHMEN_MASK | AMD_SDW1_PAD_SCHMEN_MASK)int amd_sdw_manager_start (struct amd_sdw_manager *amd_manager);
Signature · 1 line
2.53.0
PATCH v8 3/24 soundwire: amd: add pad ctrl helpers for D0/D3 PM transitions
In reply to Vijendar MukundaDuring POWER_OFF_MODE clock stop the SoundWire clock is expected to transition High to Low cleanly. On some OEM platforms based on ACP 7.0/7.1/7.2, a transient High pulse is observed after the initial Low; the clock briefly follows a High -> Low -> High -> Low sequence (High lasting a few milliseconds) instead of the expected High -> Low. This spurious High edge interferes with wake interrupt detection, causing wake events to be silently missed.
Hardware team analysis identified that the pad keeper, pulldown, SCHMEN control and output-enable registers must be programmed as a coordinated sequence during D3 entry and D0 re-entry to suppress the transient bounce and restore the pads correctly on resume.
Introduce amd_sdw_pad_ctrl() to program PAD_KEEPER_EN, PAD_PULLDOWN_CTRL, PAD_SCHMEN_CTRL and PAD_DISABLE_OE_CTRL in a single call for D0 entry (enable=true) or D3 entry (enable=false). Introduce amd_sdw_oe_ctrl() as a lighter helper that only toggles OE. Introduce amd_sdw_pad_ctrl_supported() to consolidate the ACP revision check; both helpers use it in place of duplicate switch statements.
Both helpers apply only to ACP 7.0/7.1/7.2; ACP 6.3 returns without error and unknown revisions emit a dev_err and return.
Wire amd_sdw_pad_ctrl() into amd_suspend() and amd_resume_runtime(). Wire amd_sdw_oe_ctrl() into amd_suspend_runtime() to disable OE while leaving keeper and schmen enabled; pads remain capable of detecting wake events when wake is armed.
--- drivers/soundwire/amd_manager.c | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+)
drivers/soundwire/amd_manager.c +108 -0
@@ -152,6 +152,104 @@ static int amd_deinit_sdw_manager(struct amd_sdw_manager *amd_manager)return amd_disable_sdw_manager (amd_manager); } +static void __amd_sdw_oe_update (struct amd_sdw_manager *amd_manager, +u32 mask,bool enable) +{ +amd_updatel (amd_manager->acp_mmio,ACP_PAD_DISABLE_OE_CTRL , + mask, enable ?0 : mask); +} + +static bool amd_sdw_pad_ctrl_supported (struct amd_sdw_manager *amd_manager) +{ +switch (amd_manager->acp_rev) { +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : +return true; +case ACP63_PCI_REV_ID : +return false; +default : +dev_err (amd_manager->dev,"unknown ACP revision 0x%x\n" , + amd_manager->acp_rev); +return false; + } +} + +static void amd_sdw_oe_ctrl (struct amd_sdw_manager *amd_manager,bool enable) +{ +u32 mask; + +if (!amd_sdw_pad_ctrl_supported (amd_manager)) +return ; + +switch (amd_manager->instance) { +case ACP_SDW0 : + mask =AMD_SDW0_PAD_OE_CTRL_MASK ; +break ; +case ACP_SDW1 : + mask =AMD_SDW1_PAD_OE_CTRL_MASK ; +break ; +default : +dev_err (amd_manager->dev,"invalid SDW instance %d\n" , + amd_manager->instance); +return ; + } + +mutex_lock (amd_manager->acp_sdw_lock); +__amd_sdw_oe_update (amd_manager, mask, enable); +mutex_unlock (amd_manager->acp_sdw_lock); +} + +static void amd_sdw_pad_ctrl (struct amd_sdw_manager *amd_manager,bool enable) +{ +u32 keeper_mask, pulldown_mask, schmen_mask, oe_mask; + +if (!amd_sdw_pad_ctrl_supported (amd_manager)) +return ; + +switch (amd_manager->instance) { +case ACP_SDW0 : + keeper_mask =AMD_SDW0_PAD_KEEPER_EN_MASK ; + pulldown_mask =AMD_SDW0_PAD_PULLDOWN_MASK ; + schmen_mask =AMD_SDW0_PAD_SCHMEN_MASK ; + oe_mask =AMD_SDW0_PAD_OE_CTRL_MASK ; +break ; +case ACP_SDW1 : + keeper_mask =AMD_SDW1_PAD_KEEPER_EN_MASK ; + pulldown_mask =AMD_SDW1_PAD_PULLDOWN_MASK ; + schmen_mask =AMD_SDW1_PAD_SCHMEN_MASK ; + oe_mask =AMD_SDW1_PAD_OE_CTRL_MASK ; +break ; +default : +dev_err (amd_manager->dev,"invalid SDW instance %d\n" , + amd_manager->instance); +return ; + } + +mutex_lock (amd_manager->acp_sdw_lock); +if (enable) { +/* D0: set keeper, clear pulldown, set schmen */ +amd_updatel (amd_manager->acp_mmio,ACP_SW_PAD_KEEPER_EN , + keeper_mask, keeper_mask); +amd_updatel (amd_manager->acp_mmio,ACP_PAD_PULLDOWN_CTRL , + pulldown_mask,0 ); +amd_updatel (amd_manager->acp_mmio,ACP_PAD_SCHMEN_CTRL , + schmen_mask, schmen_mask); + }else { +/* D3: clear keeper, set pulldown, clear schmen */ +amd_updatel (amd_manager->acp_mmio,ACP_SW_PAD_KEEPER_EN , + keeper_mask,0 ); +amd_updatel (amd_manager->acp_mmio,ACP_PAD_PULLDOWN_CTRL , + pulldown_mask, pulldown_mask); +amd_updatel (amd_manager->acp_mmio,ACP_PAD_SCHMEN_CTRL , + schmen_mask,0 ); + } + +/* enable OE on D0 (clear disable bits), disable OE on D3 (set them) */ +__amd_sdw_oe_update (amd_manager, oe_mask, enable); +mutex_unlock (amd_manager->acp_sdw_lock); +} +static void amd_sdw_set_frameshape (struct amd_sdw_manager *amd_manager) {u32 frame_size;
@@ -1319,6 +1417,8 @@ static int __maybe_unused amd_suspend(struct device *dev) ret =amd_deinit_sdw_manager (amd_manager);if (ret)return ret; +/* System suspend: tear down all pads */ +amd_sdw_pad_ctrl (amd_manager, false); }if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D3 );
@@ -1363,6 +1463,13 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev) ret =amd_deinit_sdw_manager (amd_manager);if (ret)return ret; +/* +* For ACP 7.0/7.1/7.2: disable OE only; keeper and schmen remain +* enabled unconditionally so that wake-capable devices can detect +* wake events if wake is armed. +* For ACP 6.3 this call is a no-op. +*/ +amd_sdw_oe_ctrl (amd_manager, false); }if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D3 );
@@ -1403,6 +1510,7 @@ static int __maybe_unused amd_resume_runtime(struct device *dev)return ret; } }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_sdw_pad_ctrl (amd_manager, true);writel (0x00 , amd_manager->acp_mmio +ACP_SW_WAKE_EN (amd_manager->instance));if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_host_wake_enable (amd_manager, false);
Signature · 1 line
2.53.0
PATCH v8 4/24 soundwire: amd: track full pad teardown across suspend/resume
In reply to Vijendar Mukundaamd_sdw_pad_ctrl() performs a full pad teardown (keeper, pulldown, schmen and OE) for system suspend, while amd_sdw_oe_ctrl() disables only OE for POWER_OFF_MODE runtime suspend. amd_resume_runtime() must distinguish between these two cases to restore pads correctly: a full teardown requires amd_sdw_pad_ctrl(true) on resume; a POWER_OFF_MODE runtime suspend in which only OE was disabled requires only amd_sdw_oe_ctrl(true).
Introduce pad_fully_torn_down flag to track when a full pad teardown was performed. Set the flag after amd_sdw_pad_ctrl(false) in amd_suspend() using amd_sdw_pad_ctrl_supported() to gate the assignment to revisions that support pad control. Clear it after the full restore in amd_resume_runtime(). On resume, restore all pads if the flag is set, otherwise restore OE only; both paths delegate revision gating to the respective helpers.
--- drivers/soundwire/amd_manager.c | 12 ++++++++++-- include/linux/soundwire/sdw_amd.h | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-)
drivers/soundwire/amd_manager.c +10 -2
@@ -1418,7 +1418,10 @@ static int __maybe_unused amd_suspend(struct device *dev)if (ret)return ret;/* System suspend: tear down all pads */ -amd_sdw_pad_ctrl (amd_manager, false); +if (amd_sdw_pad_ctrl_supported (amd_manager)) { +amd_sdw_pad_ctrl (amd_manager, false); + amd_manager->pad_fully_torn_down = true; + } }if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D3 );
@@ -1510,7 +1513,12 @@ static int __maybe_unused amd_resume_runtime(struct device *dev)return ret; } }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { -amd_sdw_pad_ctrl (amd_manager, true); +if (amd_manager->pad_fully_torn_down) { +amd_sdw_pad_ctrl (amd_manager, true); + amd_manager->pad_fully_torn_down = false; + }else { +amd_sdw_oe_ctrl (amd_manager, true); + }writel (0x00 , amd_manager->acp_mmio +ACP_SW_WAKE_EN (amd_manager->instance));if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_host_wake_enable (amd_manager, false);
include/linux/soundwire/sdw_amd.h +7 -0
@@ -75,6 +75,12 @@ struct sdw_amd_dai_runtime {* @wake_en_mask: wake enable mask per SoundWire manager * @acp_rev: acp pci device revision id * @clk_stopped: flag set to true when clock is stopped +* @pad_fully_torn_down: true when system suspend performed a full pad +* teardown (keeper, schmen and OE cleared, pulldown asserted). Cleared after +* all pads are restored in amd_resume_runtime(). False on initial probe +* (zero-initialized) and after a POWER_OFF_MODE runtime suspend in which +* only OE was disabled; in the latter case amd_resume_runtime() restores +* OE only, leaving keeper and schmen untouched. * @power_mode_mask: flag interprets amd SoundWire manager power mode * @dai_runtime_array: dai runtime array */
@@ -106,6 +112,7 @@ struct amd_sdw_manager {u32 power_mode_mask;u32 acp_rev;bool clk_stopped; +bool pad_fully_torn_down;struct sdw_amd_dai_runtime **dai_runtime_array; };
Signature · 1 line
2.53.0
PATCH v8 5/24 soundwire: amd: fix missing wake interrupt and group wake-arming calls in runtime suspend
In reply to Vijendar MukundaIn amd_suspend_runtime() POWER_OFF_MODE, amd_sdw_wake_enable() was called before amd_deinit_sdw_manager(), causing the wake IRQ mask to be silently overwritten.
amd_deinit_sdw_manager() calls amd_disable_sdw_interrupts() which writes 0x00 to ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11, wiping AMD_SDW_WAKE_INTR_MASK (BIT(16)) set by amd_sdw_wake_enable(). Move amd_sdw_wake_enable() to after amd_deinit_sdw_manager() so the wake mask is armed after the interrupt-disable sequence and cannot be clobbered.
Move amd_sdw_host_wake_enable() after amd_deinit_sdw_manager() as well, grouping both wake-arming calls together after the interrupt-disable sequence.
If amd_deinit_sdw_manager() fails (timeout waiting for ACP_SW_EN_STATUS), the hardware is in an indeterminate state and wake IRQ delivery cannot be expected to function; the early return before the wake-arming calls is correct in this case.
--- drivers/soundwire/amd_manager.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/soundwire/amd_manager.c +6 -6
@@ -1454,18 +1454,18 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)if (ret)return ret; }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { -amd_sdw_wake_enable (amd_manager, true); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, true); -if (ret) -return ret; - } ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; ret =amd_deinit_sdw_manager (amd_manager);if (ret)return ret; +amd_sdw_wake_enable (amd_manager, true); +if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { + ret =amd_sdw_host_wake_enable (amd_manager, true); +if (ret) +return ret; + }/* * For ACP 7.0/7.1/7.2: disable OE only; keeper and schmen remain * enabled unconditionally so that wake-capable devices can detect
Signature · 1 line
2.53.0
PATCH v8 6/24 soundwire: amd: fix IRQ thread dropping STATUS_0TO7 on simultaneous wake
In reply to Vijendar MukundaWhen a wake interrupt and a device-state change arrived simultaneously, amd_sdw_irq_thread() returned early after the wake path, silently discarding the STATUS_0TO7 bits.
Remove the early return and fall through to the normal slave-status processing path. Clear the wake bit from the local status_change_8to11 copy before proceeding so the remaining status checks are not confused by the wake bit.
--- drivers/soundwire/amd_manager.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/soundwire/amd_manager.c +5 -2
@@ -1110,8 +1110,11 @@ static void amd_sdw_irq_thread(struct work_struct *work)dev_dbg (amd_manager->dev,"[SDW%d] SDW INT: 0to7=0x%x, 8to11=0x%x\n" , amd_manager->instance, status_change_0to7, status_change_8to11); -if (status_change_8to11 &AMD_SDW_WAKE_STAT_MASK ) -return amd_sdw_process_wake_event (amd_manager); + +if (status_change_8to11 &AMD_SDW_WAKE_STAT_MASK ) { +amd_sdw_process_wake_event (amd_manager); + status_change_8to11 &= ~AMD_SDW_WAKE_STAT_MASK ; + }if (status_change_8to11 &AMD_SDW_PREQ_INTR_STAT ) {amd_sdw_read_and_process_ping_status (amd_manager);
Signature · 1 line
2.53.0
PATCH v8 7/24 soundwire: amd: remove STATUS_8TO11 clear from amd_sdw_process_wake_event()
In reply to Vijendar Mukundaamd_sdw_irq_thread() falls through after handling a wake event to process any coalesced PREQ or peripheral status events, rather than returning early. With the fall-through in place, both amd_sdw_process_wake_event() and the tail of amd_sdw_irq_thread() write 0x00 to ACP_SW_STATE_CHANGE_STATUS_8TO11 in the same invocation. Any status bit asserted by hardware between those two writes would be lost before the next invocation reads the register.
Remove the writel() from amd_sdw_process_wake_event() so that ACP_SW_STATE_CHANGE_STATUS_8TO11 is cleared exactly once per invocation by the tail of amd_sdw_irq_thread().
--- drivers/soundwire/amd_manager.c | 1 - 1 file changed, 1 deletion(-)
drivers/soundwire/amd_manager.c +0 -1
@@ -1093,7 +1093,6 @@ static void amd_sdw_process_wake_event(struct amd_sdw_manager *amd_manager)dev_dbg (amd_manager->dev,"SoundWire Wake event reported\n" );pm_request_resume (amd_manager->dev);writel (0x00 , amd_manager->acp_mmio +ACP_SW_WAKE_EN (amd_manager->instance)); -writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_8TO11 ); }static void amd_sdw_irq_thread (struct work_struct *work)
Signature · 1 line
2.53.0
PATCH v8 8/24 soundwire: amd: fix missing wake interrupt for ACP6.3 in POWER_OFF suspend
In reply to Vijendar MukundaOn some OEM platforms based on ACP 6.3, with a SoundWire topology where the UAJ peripheral is on one instance and the speaker peripheral is on the other, wake events from the UAJ instance are silently lost every time it enters POWER_OFF runtime suspend (D3) while speaker playback keeps ACP in D0.
On ACP6.3, unlike ACP7.0/7.1/7.2, there is no dedicated host wake interrupt path (no ACP_EXTERNAL_INTR_CNTL1 host wake mask bits and no PME wake mechanism). The wake event from a suspended SoundWire instance is delivered solely through the standard SoundWire interrupt bit in ACP_EXTERNAL_INTR_CNTL.
In the POWER_OFF_MODE runtime suspend path, amd_deinit_sdw_manager() calls amd_disable_sdw_interrupts() which clears the SoundWire instance bit in ACP_EXTERNAL_INTR_CNTL. After this, the wake interrupt has no delivery path to the CPU.
Fix this by re-enabling the SoundWire instance bit in ACP_EXTERNAL_INTR_CNTL after amd_deinit_sdw_manager() and amd_sdw_wake_enable() complete, when acp_rev is ACP63_PCI_REV_ID and wake_en_mask is set. The acp_sdw_lock mutex is held around the read-modify-write to prevent a race when both instances suspend or resume concurrently.
--- drivers/soundwire/amd_manager.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/soundwire/amd_manager.c +7 -0
@@ -1467,6 +1467,13 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev) ret =amd_sdw_host_wake_enable (amd_manager, true);if (ret)return ret; + }else if ((amd_manager->acp_rev ==ACP63_PCI_REV_ID ) && + amd_manager->wake_en_mask) { +mutex_lock (amd_manager->acp_sdw_lock); + val = sdw_manager_reg_mask_array[amd_manager->instance]; +amd_updatel (amd_manager->acp_mmio, +ACP_EXTERNAL_INTR_CNTL (amd_manager->instance), val, val); +mutex_unlock (amd_manager->acp_sdw_lock); }/* * For ACP 7.0/7.1/7.2: disable OE only; keeper and schmen remain
Signature · 1 line
2.53.0
PATCH v8 9/24 soundwire: amd: fix SDW command timeout return value handling
In reply to Vijendar Mukundaamd_sdw_send_cmd_get_resp() returned u64 but was returning -ETIMEDOUT (a negative int) on readl_poll_timeout() failures, which silently widens to a large u64 value.
Change the return type to int and pass the raw u64 response through an out-parameter. Timeout is detected directly from readl_poll_timeout() return value.
Update all callers: amd_program_scp_addr(), _amd_sdw_xfer_msg(), amd_sdw_read_and_process_ping_status(), and amd_sdw_read_ping_status() to check the return value and handle timeout explicitly.
--- drivers/soundwire/amd_manager.c | 49 ++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 22 deletions(-)
drivers/soundwire/amd_manager.c +27 -22
@@ -348,10 +348,9 @@ static void amd_sdw_ctl_word_prep(u32 *lower_word, u32 *upper_word, struct sdw_m *lower_word = lower_data; } -static u64 amd_sdw_send_cmd_get_resp (struct amd_sdw_manager *amd_manager,u32 lower_data, -u32 upper_data) +static int amd_sdw_send_cmd_get_resp (struct amd_sdw_manager *amd_manager,u32 lower_data, +u32 upper_data,u64 *response) { -u64 resp;u32 lower_resp, upper_resp;u32 sts;int ret;
@@ -389,9 +388,8 @@ static u64 amd_sdw_send_cmd_get_resp(struct amd_sdw_manager *amd_manager, u32 lo amd_manager->instance);return ret; } - resp = upper_resp; - resp = (resp <<32 ) | lower_resp; -return resp; + *response = ((u64 )upper_resp <<32 ) | lower_resp; +return 0 ; } static enum sdw_command_response
@@ -407,19 +405,22 @@ amd_program_scp_addr(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg) scp_msg.buf = &msg->addr_page1; scp_msg.flags =SDW_MSG_FLAG_WRITE ;amd_sdw_ctl_word_prep (&lower_data, &upper_data, &scp_msg,0 ); - response_buf[0 ] =amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data); +if (amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data, &response_buf[0 ])) { +dev_err_ratelimited (amd_manager->dev, +"SCP_addrpage command timeout for Slave %d\n" , msg->dev_num); +return SDW_CMD_TIMEOUT ; + } scp_msg.addr =SDW_SCP_ADDRPAGE2 ; scp_msg.buf = &msg->addr_page2;amd_sdw_ctl_word_prep (&lower_data, &upper_data, &scp_msg,0 ); - response_buf[1 ] =amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data); +if (amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data, &response_buf[1 ])) { +dev_err_ratelimited (amd_manager->dev, +"SCP_addrpage command timeout for Slave %d\n" , msg->dev_num); +return SDW_CMD_TIMEOUT ; + }for (index =0 ; index <2 ; index++) { -if (response_buf[index] == -ETIMEDOUT ) { -dev_err_ratelimited (amd_manager->dev, -"SCP_addrpage command timeout for Slave %d\n" , - msg->dev_num); -return SDW_CMD_TIMEOUT ; - }else if (!(response_buf[index] &AMD_SDW_MCP_RESP_ACK )) { +if (!(response_buf[index] &AMD_SDW_MCP_RESP_ACK )) {if (response_buf[index] &AMD_SDW_MCP_RESP_NACK ) { dev_err_ratelimited(amd_manager->dev, "SCP_addrpage NACKed for Slave %d\n",
@@ -464,11 +465,7 @@ static enum sdw_command_response amd_sdw_fill_msg_resp(struct amd_sdw_manager *aif (msg->flags ==SDW_MSG_FLAG_READ ) msg->buf[offset] =FIELD_GET (AMD_SDW_MCP_RESP_RDATA , response); }else { -if (response == -ETIMEDOUT ) { -dev_err_ratelimited (amd_manager->dev,"command timeout for Slave %d\n" , - msg->dev_num); -return SDW_CMD_TIMEOUT ; - }else if (response &AMD_SDW_MCP_RESP_NACK ) { +if (response &AMD_SDW_MCP_RESP_NACK ) {dev_err_ratelimited (amd_manager->dev,"command response NACK received for Slave %d\n" , msg->dev_num);
@@ -488,7 +485,11 @@ static unsigned int _amd_sdw_xfer_msg(struct amd_sdw_manager *amd_manager, strucu32 upper_data =0 , lower_data =0 ;amd_sdw_ctl_word_prep (&lower_data, &upper_data, msg, cmd_offset); - response =amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data); +if (amd_sdw_send_cmd_get_resp (amd_manager, lower_data, upper_data, &response)) { +dev_err_ratelimited (amd_manager->dev,"command timeout for Slave %d\n" , + msg->dev_num); +return SDW_CMD_TIMEOUT ; + }return amd_sdw_fill_msg_resp (amd_manager, msg, response, cmd_offset); }
@@ -544,7 +545,10 @@ static void amd_sdw_read_and_process_ping_status(struct amd_sdw_manager *amd_manu64 response;mutex_lock (&amd_manager->bus.msg_lock); - response =amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 ); +if (amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 , &response)) { +mutex_unlock (&amd_manager->bus.msg_lock); +return ; + }mutex_unlock (&amd_manager->bus.msg_lock);amd_sdw_process_ping_status (response, amd_manager); }
@@ -555,7 +559,8 @@ static u32 amd_sdw_read_ping_status(struct sdw_bus *bus)u64 response;u32 slave_stat; - response =amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 ); +if (amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 , &response)) +return 0 ;/* slave status from ping response */ slave_stat =FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_0_3 , response); slave_stat |=FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_4_11 , response) <<8 ;
Signature · 1 line
2.53.0
PATCH v8 10/24 soundwire: amd: cache ping slave status to avoid spurious disconnect on timeout
In reply to Vijendar Mukundaamd_sdw_read_ping_status() returned 0 on a PING command timeout. This was indistinguishable from a valid "no peripherals attached" response, causing sdw_show_ping_status() to log a spurious disconnect warning even when devices were actually present.
Add ping_slave_stat to struct amd_sdw_manager to cache the slave_stat from the last successful ping response. On timeout, the cached value is returned instead of 0, so sdw_show_ping_status() sees the last known good state. The timeout itself is still reported via dev_err() - the cache does not suppress the error.
ping_slave_stat is initialised to U32_MAX in probe and reset to U32_MAX under msg_lock before any failable step in the POWER_OFF_MODE resume path, so a partially-failed resume cannot leave stale pre-suspend data behind for a subsequent ping timeout to report.
--- drivers/soundwire/amd_manager.c | 18 +++++++++++++++++- include/linux/soundwire/sdw_amd.h | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/soundwire/amd_manager.c +17 -1
@@ -549,6 +549,9 @@ static void amd_sdw_read_and_process_ping_status(struct amd_sdw_manager *amd_manmutex_unlock (&amd_manager->bus.msg_lock);return ; } + amd_manager->ping_slave_stat = +FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_0_3 , response) | + (FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_4_11 , response) <<8 );mutex_unlock (&amd_manager->bus.msg_lock);amd_sdw_process_ping_status (response, amd_manager); }
@@ -560,11 +563,12 @@ static u32 amd_sdw_read_ping_status(struct sdw_bus *bus)u32 slave_stat;if (amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 , &response)) -return 0 ; +return amd_manager->ping_slave_stat ==U32_MAX ?0 : amd_manager->ping_slave_stat;/* slave status from ping response */ slave_stat =FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_0_3 , response); slave_stat |=FIELD_GET (AMD_SDW_MCP_SLAVE_STAT_4_11 , response) <<8 ;dev_dbg (amd_manager->dev,"slave_stat:0x%x\n" , slave_stat); + amd_manager->ping_slave_stat = slave_stat;return slave_stat; }
@@ -1182,6 +1186,7 @@ static int amd_sdw_manager_probe(struct platform_device *pdev)dev_err (dev,"mmio not found\n" );return -ENOMEM ; } + amd_manager->ping_slave_stat =U32_MAX ; amd_manager->instance = pdata->instance; amd_manager->mmio = amd_manager->acp_mmio + (amd_manager->instance *SDW_MANAGER_REG_OFFSET );
@@ -1534,6 +1539,17 @@ static int __maybe_unused amd_resume_runtime(struct device *dev)amd_sdw_oe_ctrl (amd_manager, true); }writel (0x00 , amd_manager->acp_mmio +ACP_SW_WAKE_EN (amd_manager->instance)); +/* +* POWER_OFF_MODE tears the bus down completely; invalidate the +* cached ping status before any failable step so that a +* partially-failed resume leaves the first post-resume ping +* timeout returning 0 rather than stale pre-suspend data. +* CLK_STOP_MODE leaves peripherals attached, so the cache +* remains valid there. +*/ +mutex_lock (&amd_manager->bus.msg_lock); + amd_manager->ping_slave_stat =U32_MAX ; +mutex_unlock (&amd_manager->bus.msg_lock);if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { ret =amd_sdw_host_wake_enable (amd_manager, false);if (ret)
include/linux/soundwire/sdw_amd.h +4 -0
@@ -82,6 +82,9 @@ struct sdw_amd_dai_runtime {* only OE was disabled; in the latter case amd_resume_runtime() restores * OE only, leaving keeper and schmen untouched. * @power_mode_mask: flag interprets amd SoundWire manager power mode +* @ping_slave_stat: cached slave status from the last ping +* transaction that returned a response; +* initialised to U32_MAX ("no valid reading yet") * @dai_runtime_array: dai runtime array */ struct amd_sdw_manager {
@@ -111,6 +114,7 @@ struct amd_sdw_manager {u32 wake_en_mask;u32 power_mode_mask;u32 acp_rev; +u32 ping_slave_stat;bool clk_stopped;bool pad_fully_torn_down;
Signature · 1 line
2.53.0
PATCH v8 11/24 soundwire: amd: assert msg_lock is held in amd_sdw_read_ping_status()
In reply to Vijendar Mukundaamd_sdw_read_ping_status() is always called from sdw_show_ping_status() which holds bus->msg_lock. Add lockdep_assert_held() to document this locking requirement and catch any future callers that omit the lock.
--- drivers/soundwire/amd_manager.c | 1 + 1 file changed, 1 insertion(+)
drivers/soundwire/amd_manager.c +1 -0
@@ -562,6 +562,7 @@ static u32 amd_sdw_read_ping_status(struct sdw_bus *bus)u64 response;u32 slave_stat; +lockdep_assert_held (&bus->msg_lock);if (amd_sdw_send_cmd_get_resp (amd_manager,0 ,0 , &response))return amd_manager->ping_slave_stat ==U32_MAX ?0 : amd_manager->ping_slave_stat;/* slave status from ping response */
Signature · 1 line
2.53.0
PATCH v8 12/24 soundwire: amd: fix work drain ordering in remove path
In reply to Vijendar Mukundaamd_sdw_manager_remove() cancelled amd_sdw_work but not amd_sdw_irq_thread. Since amd_sdw_irq_thread() calls schedule_work(&amd_sdw_work), an in-flight irq_thread item can re-queue amd_sdw_work after its cancel returns, defeating the cancellation.
Fix by calling amd_disable_sdw_interrupts() first to quiesce the hardware IRQ source, then cancel_work_sync() for amd_sdw_irq_thread, then cancel_work_sync() for amd_sdw_work. The existing cancel_work_sync(amd_sdw_work) is also moved to after amd_disable_sdw_interrupts() so that any work item queued between the old cancel position and the interrupt disable cannot escape draining.
synchronize_irq() is deliberately not used before the cancel_work_sync() calls. Once SoundWire interrupts are masked, no new IRQ deliveries can occur. An IRQ handler already in flight may still queue amd_sdw_irq_thread, so cancel_work_sync() is used to drain both amd_sdw_irq_thread and any amd_sdw_work items it may have scheduled. This fully quiesces the driver workqueues, making synchronize_irq() unnecessary.
--- drivers/soundwire/amd_manager.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/soundwire/amd_manager.c +2 -1
@@ -1266,8 +1266,9 @@ static void amd_sdw_manager_remove(struct platform_device *pdev)int ret;pm_runtime_disable (&pdev->dev); -cancel_work_sync (&amd_manager->amd_sdw_work);amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work);sdw_bus_master_delete (&amd_manager->bus); ret =amd_disable_sdw_manager (amd_manager);if (ret)
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v8 12/24] soundwire: amd: fix work drain ordering in remove path
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] soundwire: amd: unsynchronized in-flight IRQ in amd_sdw_manager_remove() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] soundwire: amd: unsynchronized in-flight IRQ in amd_sdw_manager_remove()
--
commit a1b902c580b9ecf41d510f5e3161a9e0ef33c879
Author: Vijendar Mukunda <[email protected]>
soundwire: amd: fix work drain ordering in remove path
This commit updates amd_sdw_manager_remove() to disable SoundWire interrupts before cancelling amd_sdw_irq_thread and amd_sdw_work.
drivers/soundwire/amd_manager.c static void amd_sdw_manager_remove(struct platform_device *pdev) · line 1270
Quoted 12 lines
drivers/soundwire/amd_manager.c +2
-1@@ -1265,8 +1265,9 @@ static void amd_sdw_manager_remove(struct platform_device *pdev)int ret;pm_runtime_disable (&pdev->dev); -cancel_work_sync (&amd_manager->amd_sdw_work);amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work);
[Severity: Medium]
Can an in-flight parent ACP hard IRQ handler queue amd_sdw_irq_thread after cancel_work_sync() returns, causing a use-after-free of amd_manager?
The commit message notes that synchronize_irq() is skipped because cancel_work_sync() will drain work queued by an IRQ handler already in flight. However, cancel_work_sync() only drains work that is already pending or running at the moment it checks, and leaves the work item enabled on return. If the platform device is unbound while the parent ACP PCI IRQ is active, an IRQ asserted just before amd_disable_sdw_interrupts() masks ACP_EXTERNAL_INTR_CNTL can race with removal:
CPU0 CPU1 (<hardirq>)amd_sdw_manager_remove() acp63_irq_handler()
readl(... + ACP_EXTERNAL_INTR_STAT)
amd_manager = dev_get_drvdata(...)
amd_disable_sdw_interrupts()
cancel_work_sync(&...->amd_sdw_irq_thread)
cancel_work_sync(&...->amd_sdw_work)
schedule_work(&...->amd_sdw_irq_thread)
device_unbind_cleanup()
devres_release_all()
amd_sdw_irq_thread()
amd_manager->... <-- use-after-freeEven with the interrupt_enabled check added in the later commit "soundwire: amd: guard IRQ work with interrupt_enabled and clear STATUS", amd_sdw_irq_thread() still reads amd_manager->interrupt_enabled after devres_release_all() has freed amd_manager.
Using disable_work_sync() instead of cancel_work_sync() (or synchronizing with the parent IRQ after masking interrupts) would prevent new enqueues after draining finishes.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v8 12/24] soundwire: amd: fix work drain ordering in remove path
Quoted 21 lines · On 23/11/26 22:57, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] soundwire: amd: unsynchronized in-flight IRQ inamd_sdw_manager_remove()
--commit
a1b902c580b9ecf41d510f5e3161a9e0ef33c879
Author: Vijendar Mukunda <[email protected]>soundwire: amd: fix work drain ordering in remove path
This commit updates
amd_sdw_manager_remove()to disable SoundWire interrupts before cancellingamd_sdw_irq_threadandamd_sdw_work.Quoted 7 lines
drivers/soundwire/amd_manager.c +0
-0@@ -1265,8 +1265,9 @@ static void amd_sdw_manager_remove(struct platform_device *pdev)int ret;
Quoted 9 lines
drivers/soundwire/amd_manager.c static void amd_sdw_manager_remove(struct platform_device *pdev) · line 1271
Quoted 5 lines
drivers/soundwire/amd_manager.c +2
-1pm_runtime_disable (&pdev->dev); -cancel_work_sync (&amd_manager->amd_sdw_work);amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work);[Severity: Medium]
Can an in-flight parent ACP hard IRQ handler queueamd_sdw_irq_threadaftercancel_work_sync()returns, causing a use-after-free ofamd_manager?
++
++
No change required. False positive for the AMD ACP hardware.
The race assumes that the in-flight acp63_irq_handler() on CPU1 calls schedule_work() AFTER cancel_work_sync() on CPU0 returns. For this to occur, CPU1's hard IRQ handler must be delayed for the entire duration of CPU0's amd_disable_sdw_interrupts(), cancel_work_sync() x2, sdw_bus_master_delete(), amd_disable_sdw_manager(), and then devres_release_all() -- hundreds of microseconds to milliseconds.
AMD ACP uses a dedicated, level-triggered, line-based interrupt, not MSI/MSI-X. acp63_irq_handler() runs as a hard IRQ handler with interrupts disabled. It executes atomically and completes in
nanoseconds: the path from dev_get_drvdata() to schedule_work() is a handful of instructions. By the time CPU0 begins cancel_work_sync(), CPU1 has already called schedule_work() and the work item is already in the queue. cancel_work_sync() sees the pending work and drains it correctly.
The extended delay required for the race is a characteristic of MSI/MSI-X based controllers where interrupt delivery can be deferred or reordered across CPUs. It does not apply to AMD ACP's dedicated line-based hardirq handler.
Additionally, the companion patches already merged in Mark Brown's ASoC tree guard adata->sdw before entering the SoundWire IRQ path, providing defence-in-depth at the parent ACP IRQ handler level.
Quoted 31 lines
The commit message notes that
synchronize_irq()is skipped becausecancel_work_sync()will drain work queued by an IRQ handler already in flight. However,cancel_work_sync()only drains work that is already pending or running at the moment it checks, and leaves the work item enabled on return. If the platform device is unbound while the parent ACP PCI IRQ is active, an IRQ asserted just beforeamd_disable_sdw_interrupts()masksACP_EXTERNAL_INTR_CNTLcan race with removal:CPU0 CPU1 (<hardirq>)
amd_sdw_manager_remove()acp63_irq_handler()readl(... + ACP_EXTERNAL_INTR_STAT) amd_manager = dev_get_drvdata(...) amd_disable_sdw_interrupts() cancel_work_sync(&...->amd_sdw_irq_thread) cancel_work_sync(&...->amd_sdw_work) schedule_work(&...->amd_sdw_irq_thread) device_unbind_cleanup() devres_release_all() amd_sdw_irq_thread() amd_manager->... <-- use-after-freeEven with the
interrupt_enabledcheck added in the later commit "soundwire: amd: guard IRQ work withinterrupt_enabledand clear STATUS",amd_sdw_irq_thread()still readsamd_manager->interrupt_enabledafterdevres_release_all()has freedamd_manager.Using
disable_work_sync()instead ofcancel_work_sync()(or synchronizing with the parent IRQ after masking interrupts) would prevent new enqueues after draining finishes.
PATCH v8 13/24 soundwire: amd: defer dev_set_drvdata() until after INIT_WORK()
In reply to Vijendar Mukundaamd_sdw_manager_probe() called dev_set_drvdata() before initialising the amd_sdw_irq_thread and amd_sdw_work structures with INIT_WORK(). An interrupt arriving in that window could call the IRQ handler which enqueues amd_sdw_irq_thread before the work struct is initialised, leading to undefined behaviour.
Move dev_set_drvdata() to after both INIT_WORK() calls to close the race window.
--- drivers/soundwire/amd_manager.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soundwire/amd_manager.c +1 -1
@@ -1254,9 +1254,9 @@ static int amd_sdw_manager_probe(struct platform_device *pdev)sdw_bus_master_delete (&amd_manager->bus);return ret; } -dev_set_drvdata (dev, amd_manager);INIT_WORK (&amd_manager->amd_sdw_irq_thread, amd_sdw_irq_thread);INIT_WORK (&amd_manager->amd_sdw_work, amd_sdw_update_slave_status_work); +dev_set_drvdata (dev, amd_manager);return 0 ; }
Signature · 1 line
2.53.0
PATCH v8 14/24 soundwire: amd: pm_runtime guard in remove path
In reply to Vijendar MukundaGuard pm_runtime_disable() so it is only called when runtime PM was actually enabled. amd_sdw_manager_start() calls pm_runtime_enable() only at the very end, after several fallible hardware init steps. If sdw_amd_startup() fails mid-loop (one manager started, the next fails before pm_runtime_enable()), sdw_amd_exit() triggers platform_device_unregister() for all managers. Calling pm_runtime_disable() on the partially-started manager finds disable_depth already at its initial value of 1, silently increments it to 2 and returns without a warning, so a later pm_runtime_enable() would only bring it back to 1 and leave runtime PM disabled. Use pm_runtime_enabled() to skip the call when it was never paired with an enable.
--- drivers/soundwire/amd_manager.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/soundwire/amd_manager.c +2 -1
@@ -1265,7 +1265,8 @@ static void amd_sdw_manager_remove(struct platform_device *pdev)struct amd_sdw_manager *amd_manager =dev_get_drvdata (&pdev->dev);int ret; -pm_runtime_disable (&pdev->dev); +if (pm_runtime_enabled (&pdev->dev)) +pm_runtime_disable (&pdev->dev);amd_disable_sdw_interrupts (amd_manager);cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work);
Signature · 1 line
2.53.0
PATCH v8 15/24 soundwire: amd: guard IRQ work with interrupt_enabled and clear STATUS
In reply to Vijendar MukundaAdd an interrupt_enabled boolean to struct amd_sdw_manager to prevent already-queued work from executing during the window between starting to disable interrupts and the hardware masks taking effect.
- Add interrupt_enabled to struct amd_sdw_manager.
- Clear ACP_SW_STATE_CHANGE_STATUS_8TO11 and _0TO7 at the start of amd_enable_sdw_interrupts(), before setting interrupt_enabled, so that stale pre-suspend STATUS bits do not trigger spurious processing on POWER_OFF_MODE resume. After amd_sdw_clock_stop() completes, hardware cannot assert new STATUS bits, so the clear loses no real events.
- Set interrupt_enabled to true in amd_enable_sdw_interrupts(), after clearing stale STATUS and before unmasking any hardware registers, so no IRQ can fire with the flag false.
- Clear interrupt_enabled to false at the start of amd_disable_sdw_interrupts(), before touching any hardware registers, so that queued work items that start executing after the flag is cleared will return early.
- Guard execution of amd_sdw_irq_thread() with an early-return flag check.
- Restore interrupt_enabled inside the ACP6.3-specific wake-re-enable block in amd_suspend_runtime() POWER_OFF_MODE, so that wake events are processed after hardware wake delivery is re-armed.
- Clear STATUS registers in the ACP6.3 POWER_OFF_MODE wake-re-enable block before restoring interrupt_enabled, so that a wake interrupt does not cause amd_sdw_irq_thread() to process pre-suspend state-change or PREQ bits while the manager is disabled.
- Use WRITE_ONCE() for all stores to interrupt_enabled and READ_ONCE() for the load in amd_sdw_irq_thread() to annotate the intended single-writer/single-reader cross-context access and prevent compiler caching under LKMM.
--- drivers/soundwire/amd_manager.c | 38 +++++++++++++++++++++++++++++++ include/linux/soundwire/sdw_amd.h | 4 ++++ 2 files changed, 42 insertions(+)
drivers/soundwire/amd_manager.c +38 -0
@@ -119,6 +119,9 @@ static void amd_enable_sdw_interrupts(struct amd_sdw_manager *amd_manager) {u32 val; +writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_8TO11 ); +writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_0TO7 ); +WRITE_ONCE (amd_manager->interrupt_enabled, true);mutex_lock (amd_manager->acp_sdw_lock); val = sdw_manager_reg_mask_array[amd_manager->instance];amd_updatel (amd_manager->acp_mmio,ACP_EXTERNAL_INTR_CNTL (amd_manager->instance), val, val);
@@ -135,6 +138,26 @@ static void amd_disable_sdw_interrupts(struct amd_sdw_manager *amd_manager) {u32 irq_mask; +/* +* Clear the flag before updating hardware masks. Work items already +* queued but not yet started will see the flag false at entry and +* return early; work items already running will complete their current +* invocation. Callers that need to ensure no instance of +* amd_sdw_irq_thread() is executing on return must follow this call +* with cancel_work_sync(&amd_manager->amd_sdw_irq_thread). Once the +* mask registers are written, the ACP hardware will not assert any new +* SDW interrupt. +* +* ACP_EXTERNAL_INTR_CNTL is the top-level gate: clearing it here +* stops interrupt delivery to the CPU. An in-flight +* amd_sdw_update_slave_status_work() on the SDW_SLAVE_ATTACHED path +* may re-write ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7 and _MASK_8TO11 +* after this function returns, but that work never touches +* ACP_EXTERNAL_INTR_CNTL, so the gate remains closed and no interrupt +* can reach the CPU. +*/ +WRITE_ONCE (amd_manager->interrupt_enabled, false); +mutex_lock (amd_manager->acp_sdw_lock); irq_mask = sdw_manager_reg_mask_array[amd_manager->instance]; amd_updatel(amd_manager->acp_mmio, ACP_EXTERNAL_INTR_CNTL(amd_manager->instance),
@@ -1112,6 +1135,18 @@ static void amd_sdw_irq_thread(struct work_struct *work)u32 status_change_8to11;u32 status_change_0to7; +/* +* Interrupts are being disabled (PM suspend or driver remove). +* Stale ACP_SW_STATE_CHANGE_STATUS_0TO7 and _8TO11 bits are +* expected to be cleared before interrupt delivery is re-enabled: +* amd_enable_sdw_interrupts() clears them on POWER_OFF_MODE resume, +* and the ACP6.3 POWER_OFF_MODE wake-re-enable block in +* amd_suspend_runtime() clears them before restoring +* interrupt_enabled for wake delivery. +*/ +if (!READ_ONCE (amd_manager->interrupt_enabled)) +return ; + status_change_8to11 =readl (amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_8TO11 ); status_change_0to7 =readl (amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_0TO7 );if (!status_change_0to7 && !status_change_8to11)
@@ -1482,6 +1517,9 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)return ret; }else if ((amd_manager->acp_rev ==ACP63_PCI_REV_ID ) && amd_manager->wake_en_mask) { +writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_8TO11 ); +writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_0TO7 ); +WRITE_ONCE (amd_manager->interrupt_enabled, true);mutex_lock (amd_manager->acp_sdw_lock); val = sdw_manager_reg_mask_array[amd_manager->instance]; amd_updatel(amd_manager->acp_mmio,
include/linux/soundwire/sdw_amd.h +4 -0
@@ -85,6 +85,9 @@ struct sdw_amd_dai_runtime {* @ping_slave_stat: cached slave status from the last ping * transaction that returned a response; * initialised to U32_MAX ("no valid reading yet") +* @interrupt_enabled: set when hardware interrupts are enabled; cleared +* before disabling to prevent queued work from executing +* interrupt events during teardown * @dai_runtime_array: dai runtime array */ struct amd_sdw_manager {
@@ -117,6 +120,7 @@ struct amd_sdw_manager {u32 ping_slave_stat;bool clk_stopped;bool pad_fully_torn_down; +bool interrupt_enabled;struct sdw_amd_dai_runtime **dai_runtime_array; };
Signature · 1 line
2.53.0
PATCH v8 16/24 soundwire: amd: fix *sdw_ctx leak when sdw_amd_startup() fails
In reply to Vijendar Mukundasdw_amd_probe() published *sdw_ctx immediately after sdw_amd_probe_controller() returned, before sdw_amd_startup() ran. If sdw_amd_startup() failed, the error was returned directly, leaving *sdw_ctx pointing to an allocated context whose platform devices were already registered. The caller has no way to distinguish this from a genuine probe success, so the context and devices leak.
Fix this by publishing *sdw_ctx = ctx right after sdw_amd_probe_controller() succeeds, then calling sdw_amd_startup(). On failure reset *sdw_ctx = NULL and call sdw_amd_exit(ctx) before returning the error. The caller always observes *sdw_ctx as either NULL (startup did not complete) or a pointer to a fully-started, valid context.
--- drivers/soundwire/amd_init.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/soundwire/amd_init.c +13 -3
@@ -182,11 +182,21 @@ static int sdw_amd_startup(struct sdw_amd_ctx *ctx)int sdw_amd_probe (struct sdw_amd_res *res,struct sdw_amd_ctx **sdw_ctx) { - *sdw_ctx =sdw_amd_probe_controller (res); -if (!*sdw_ctx) +struct sdw_amd_ctx *ctx; +int ret; + + ctx =sdw_amd_probe_controller (res); +if (!ctx)return -ENODEV ; -return sdw_amd_startup (*sdw_ctx); + *sdw_ctx = ctx; + ret =sdw_amd_startup (ctx); +if (ret) { + *sdw_ctx =NULL ; +sdw_amd_exit (ctx); +return ret; + } +return 0 ; }EXPORT_SYMBOL_NS (sdw_amd_probe,"SOUNDWIRE_AMD_INIT" );
Signature · 1 line
2.53.0
PATCH v8 17/24 soundwire: amd: propagate amd_init_sdw_manager() error on resume
In reply to Vijendar MukundaIn amd_resume_runtime() POWER_OFF_MODE, amd_init_sdw_manager() was called without capturing its return value. The function performs hardware bus resets using readl_poll_timeout() and can return -ETIMEDOUT if the hardware does not respond. When that happens, execution fell through to amd_enable_sdw_interrupts() and amd_enable_sdw_manager() on hardware that may still be in a broken reset state.
Fix by capturing the return value and returning early on failure.
--- drivers/soundwire/amd_manager.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/soundwire/amd_manager.c +3 -1
@@ -1612,7 +1612,9 @@ static int __maybe_unused amd_resume_runtime(struct device *dev) ret =amd_sdw_clk_init_ctrl (amd_manager);if (ret)return ret; -amd_init_sdw_manager (amd_manager); + ret =amd_init_sdw_manager (amd_manager); +if (ret) +return ret;amd_enable_sdw_interrupts (amd_manager); ret =amd_enable_sdw_manager (amd_manager);if (ret)
Signature · 1 line
2.53.0
PATCH v8 18/24 soundwire: amd: drop amd_deinit_sdw_manager() in POWER_OFF suspend
In reply to Vijendar Mukundaamd_deinit_sdw_manager() was a small helper that bundled amd_disable_sdw_interrupts() and amd_disable_sdw_manager() together. Expand the two POWER_OFF_MODE call sites in amd_suspend() and amd_suspend_runtime() to call those two functions directly, then remove the helper. No functional change.
--- drivers/soundwire/amd_manager.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/soundwire/amd_manager.c +4 -8
@@ -169,12 +169,6 @@ static void amd_disable_sdw_interrupts(struct amd_sdw_manager *amd_manager)writel (0x00 , amd_manager->mmio +ACP_SW_ERROR_INTR_MASK ); } -static int amd_deinit_sdw_manager (struct amd_sdw_manager *amd_manager) -{ -amd_disable_sdw_interrupts (amd_manager); -return amd_disable_sdw_manager (amd_manager); -} -static void __amd_sdw_oe_update (struct amd_sdw_manager *amd_manager,u32 mask,bool enable) {
@@ -1464,7 +1458,8 @@ static int __maybe_unused amd_suspend(struct device *dev) ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; - ret =amd_deinit_sdw_manager (amd_manager); +amd_disable_sdw_interrupts (amd_manager); + ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;/* System suspend: tear down all pads */
@@ -1507,7 +1502,8 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev) ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; - ret =amd_deinit_sdw_manager (amd_manager); +amd_disable_sdw_interrupts (amd_manager); + ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;amd_sdw_wake_enable (amd_manager, true);
Signature · 1 line
2.53.0
PATCH v8 19/24 soundwire: amd: replace >= ACP70 with explicit switch/case in PM paths
In reply to Vijendar MukundaReplace open-ended acp_rev >= ACP70_PCI_REV_ID comparisons in amd_suspend(), amd_suspend_runtime(), and amd_resume_runtime() with explicit per-revision handling enumerating ACP63, ACP70, ACP71, and ACP72, so that every supported revision is handled explicitly.
The host wake enable step is guarded by acp_rev at six PM call sites. Factor that repeated block into amd_sdw_host_wake_enable_by_rev() to avoid duplicating the switch/case at every site. The remaining device state transitions keep an inline switch/case as one of them carries additional PME handling.
An unrecognised revision cannot reach these PM callbacks because amd_sdw_manager_probe() already validates acp_rev with the same switch and fails registration for anything unknown. The switch default therefore emits dev_err() and returns -EINVAL to make the unexpected case visible while keeping the error path consistent across all PM operations.
--- drivers/soundwire/amd_manager.c | 100 +++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 34 deletions(-)
drivers/soundwire/amd_manager.c +66 -34
@@ -1420,6 +1420,23 @@ static int __maybe_unused amd_pm_prepare(struct device *dev)return 0 ; } +static int __maybe_unused amd_sdw_host_wake_enable_by_rev (struct amd_sdw_manager *amd_manager, +bool enable) +{ +switch (amd_manager->acp_rev) { +case ACP63_PCI_REV_ID : +break ; +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : +return amd_sdw_host_wake_enable (amd_manager, enable); +default : +dev_err (amd_manager->dev,"unexpected acp_rev 0x%x\n" , amd_manager->acp_rev); +return -EINVAL ; + } +return 0 ; +} +static int __maybe_unused amd_suspend (struct device *dev) {struct amd_sdw_manager *amd_manager =dev_get_drvdata (dev);
@@ -1435,22 +1452,18 @@ static int __maybe_unused amd_suspend(struct device *dev)if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) {cancel_work_sync (&amd_manager->amd_sdw_work);amd_sdw_wake_enable (amd_manager, false); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, false); -if (ret) -return ret; - } + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false); +if (ret) +return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) {cancel_work_sync (&amd_manager->amd_sdw_work);amd_sdw_wake_enable (amd_manager, false); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, false); -if (ret) -return ret; - } + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false); +if (ret) +return ret;/* * As per hardware programming sequence on AMD platforms, * clock stop should be invoked first before powering-off
@@ -1468,10 +1481,19 @@ static int __maybe_unused amd_suspend(struct device *dev) amd_manager->pad_fully_torn_down = true; } } -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { +switch (amd_manager->acp_rev) { +case ACP63_PCI_REV_ID : +break ; +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D3 );if (ret)return ret; +break ; +default : +dev_err (amd_manager->dev,"unexpected acp_rev 0x%x\n" , amd_manager->acp_rev); +return -EINVAL ; }return 0 ; }
@@ -1490,11 +1512,9 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev) }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) {amd_sdw_wake_enable (amd_manager, true); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, true); -if (ret) -return ret; - } + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, true); +if (ret) +return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret;
@@ -1507,12 +1527,10 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)if (ret)return ret;amd_sdw_wake_enable (amd_manager, true); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, true); -if (ret) -return ret; - }else if ((amd_manager->acp_rev ==ACP63_PCI_REV_ID ) && - amd_manager->wake_en_mask) { + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, true); +if (ret) +return ret; +if (amd_manager->acp_rev ==ACP63_PCI_REV_ID && amd_manager->wake_en_mask) {writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_8TO11 );writel (0x00 , amd_manager->mmio +ACP_SW_STATE_CHANGE_STATUS_0TO7 );WRITE_ONCE (amd_manager->interrupt_enabled, true);
@@ -1530,7 +1548,12 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)*/ amd_sdw_oe_ctrl (amd_manager, false); } -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { +switch (amd_manager->acp_rev) { +case ACP63_PCI_REV_ID : +break ; +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D3 );if (ret)return ret;
@@ -1542,6 +1565,10 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)dev_dbg (amd_manager->dev,"ACP_PME_EN:0x%x\n" , val); } } +break ; +default : +dev_err (amd_manager->dev,"unexpected acp_rev 0x%x\n" , amd_manager->acp_rev); +return -EINVAL ; }return 0 ; }
@@ -1563,11 +1590,9 @@ static int __maybe_unused amd_resume_runtime(struct device *dev) ret =amd_sdw_clock_stop_exit (amd_manager);if (ret)return ret; -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, false); -if (ret) -return ret; - } + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false); +if (ret) +return ret; }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) {if (amd_manager->pad_fully_torn_down) {amd_sdw_pad_ctrl (amd_manager, true);
@@ -1587,11 +1612,9 @@ static int __maybe_unused amd_resume_runtime(struct device *dev)mutex_lock (&amd_manager->bus.msg_lock); amd_manager->ping_slave_stat =U32_MAX ;mutex_unlock (&amd_manager->bus.msg_lock); -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { - ret =amd_sdw_host_wake_enable (amd_manager, false); -if (ret) -return ret; - } + ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false); +if (ret) +return ret; val =readl (amd_manager->mmio +ACP_SW_CLK_RESUME_CTRL );if (val) { val |=AMD_SDW_CLK_RESUME_REQ ;
@@ -1617,10 +1640,19 @@ static int __maybe_unused amd_resume_runtime(struct device *dev)return ret;amd_sdw_set_frameshape (amd_manager); } -if (amd_manager->acp_rev >=ACP70_PCI_REV_ID ) { +switch (amd_manager->acp_rev) { +case ACP63_PCI_REV_ID : +break ; +case ACP70_PCI_REV_ID : +case ACP71_PCI_REV_ID : +case ACP72_PCI_REV_ID : ret =amd_sdw_set_device_state (amd_manager,AMD_SDW_DEVICE_STATE_D0 );if (ret)return ret; +break ; +default : +dev_err (amd_manager->dev,"unexpected acp_rev 0x%x\n" , amd_manager->acp_rev); +return -EINVAL ; }return 0 ; }
Signature · 1 line
2.53.0
PATCH v8 20/24 soundwire: amd: fix interrupt gate and work drain ordering in PM ops
In reply to Vijendar MukundaFix ordering and sequencing bugs across the CLK_STOP_MODE and POWER_OFF_MODE suspend paths:
amd_suspend():
- CLK_STOP_MODE: cancel_work_sync(&amd_sdw_work) ran without first closing the ACP_EXTERNAL_INTR_CNTL delivery gate or draining amd_sdw_irq_thread. An ISR already in flight when the drain completed could re-queue work after cancel_work_sync() returned, racing amd_sdw_clock_stop(). amd_sdw_wake_enable(false) preceding the work drain was also redundant: amd_disable_sdw_interrupts() already masks all SDW interrupt delivery including wake. The amd_enable_sdw_interrupts() call after clock stop restores all state-change, error, and delivery masks; in CLK_STOP_MODE only the SDW clock is gated so no state-change interrupt can be asserted after clock stop. The wake bit is not part of the restored masks; wake interrupts are only asserted when the system transitions to D3. If amd_sdw_host_wake_enable_by_rev() returns -EINVAL the operation aborts; this indicates an unrecognized acp_rev, which is an unsupported configuration on this driver.
- POWER_OFF_MODE: same missing gate closure and irq_thread drain before the work drain; a redundant amd_disable_sdw_interrupts() call was placed after clock stop instead of before the drains; amd_sdw_wake_enable(false) at the top of this branch was redundant because amd_disable_sdw_interrupts() (now moved first) already zeroes ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11, which covers the wake bit.
amd_suspend_runtime():
- CLK_STOP_MODE: neither amd_sdw_irq_thread nor amd_sdw_work was drained at all before amd_sdw_clock_stop(); the interrupt delivery gate was not closed before the drains and not reopened after clock stop; amd_sdw_wake_enable(true) was placed before the drains instead of after clock stop. If amd_sdw_host_wake_enable_by_rev() returns -EINVAL the operation aborts; this indicates an unrecognized acp_rev, which is an unsupported configuration on this driver.
- POWER_OFF_MODE: missing gate closure, irq_thread drain, and work drain before clock stop; amd_disable_sdw_interrupts() was placed after clock stop instead of before.
--- drivers/soundwire/amd_manager.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/soundwire/amd_manager.c +13 -5
@@ -1450,17 +1450,20 @@ static int __maybe_unused amd_suspend(struct device *dev) }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret;
@@ -1471,7 +1474,6 @@ static int __maybe_unused amd_suspend(struct device *dev) ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; -amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;
@@ -1511,18 +1513,24 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)return 0 ; }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { -amd_sdw_wake_enable (amd_manager, true); +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, true);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); +amd_sdw_wake_enable (amd_manager, true); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; -amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v8 20/24] soundwire: amd: fix interrupt gate and work drain ordering in PM ops
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] soundwire: amd: draining amd_sdw_work re-unmasks IRQs in POWER_OFF_MODE --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] soundwire: amd: draining amd_sdw_work re-unmasks IRQs in POWER_OFF_MODE
--
commit eb9a0322b337ad61ced64ca897101c35c70d2a33
Author: Vijendar Mukunda <[email protected]>
soundwire: amd: fix interrupt gate and work drain ordering in PM ops
This commit disables SoundWire interrupts and drains amd_sdw_irq_thread and amd_sdw_work before stopping the clock in amd_suspend() and amd_suspend_runtime().
Quoted 52 lines
drivers/soundwire/amd_manager.c +10
-4@@ -1449,17 +1449,20 @@ static int __maybe_unused amd_suspend(struct device *dev) }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret;@@ -1470,7 +1473,6 @@ static int __maybe_unused amd_suspend(struct device *dev) ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; -amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;@@ -1510,18 +1512,24 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)return 0 ; }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { -amd_sdw_wake_enable (amd_manager, true); +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, true);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); +amd_sdw_wake_enable (amd_manager, true); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) {
drivers/soundwire/amd_manager.c static int __maybe_unused amd_suspend_runtime(struct device *dev) · line 1535
Quoted 10 lines
drivers/soundwire/amd_manager.c +3
-1+amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; -amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;
[Severity: Medium]
Can moving amd_disable_sdw_interrupts() before cancel_work_sync() in AMD_SDW_POWER_OFF_MODE leave ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7 and ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11 unmasked after amd_sdw_work drains?
CPU0 kworkeramd_suspend_runtime() amd_sdw_update_slave_status_work()
amd_disable_sdw_interrupts()
writel(0, ..._MASK_0TO7)
writel(0, ..._MASK_8TO11)
cancel_work_sync(&amd_sdw_work)
if (status[0] == SDW_SLAVE_ATTACHED)
writel(AMD_SDW_IRQ_MASK_0TO7, ...)
writel(AMD_SDW_IRQ_MASK_8TO11, ...)
...
amd_sdw_wake_enable(..., true)
/* RMW preserves 0x000c7777 */ /* ACP63: re-enables ACP_EXTERNAL_INTR_CNTL */
<-- state-change IRQs unmasked in D3
As noted in amd_disable_sdw_interrupts(), an in-flight amd_sdw_update_slave_status_work() on the SDW_SLAVE_ATTACHED path rewrites both status mask registers before cancel_work_sync(&amd_manager->amd_sdw_work) returns. The comment in amd_disable_sdw_interrupts() assumes this is safe because ACP_EXTERNAL_INTR_CNTL remains closed, and in AMD_SDW_CLK_STOP_MODE amd_enable_sdw_interrupts() overwrites the masks anyway.
In AMD_SDW_POWER_OFF_MODE, however, removing the post-clock-stop amd_disable_sdw_interrupts() call means nothing re-zeroes the mask registers after amd_sdw_work finishes. In amd_suspend_runtime(), amd_sdw_wake_enable() then does a read-modify-write on ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11 that preserves AMD_SDW_IRQ_MASK_8TO11 alongside AMD_SDW_WAKE_INTR_MASK, and on ACP63_PCI_REV_ID with wake_en_mask set, the driver sets interrupt_enabled = true and re-opens ACP_EXTERNAL_INTR_CNTL while all peripheral state-change and PREQ masks are still enabled with the manager powered off.
Zeroing ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7 and ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11 again after cancel_work_sync(&amd_manager->amd_sdw_work) in the POWER_OFF_MODE paths would ensure only the wake interrupt is unmasked.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Re: [PATCH v8 20/24] soundwire: amd: fix interrupt gate and work drain ordering in PM ops
Quoted 73 lines · On 23/11/26 23:02, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] soundwire: amd: drainingamd_sdw_workre-unmasks IRQs inPOWER_OFF_MODE
--commit
eb9a0322b337ad61ced64ca897101c35c70d2a33
Author: Vijendar Mukunda <[email protected]>soundwire: amd: fix interrupt gate and work drain ordering in PM ops
This commit disables SoundWire interrupts and drains
amd_sdw_irq_threadandamd_sdw_workbefore stopping the clock inamd_suspend()andamd_suspend_runtime().Quoted 58 lines
drivers/soundwire/amd_manager.c +13
-4@@ -1449,17 +1449,20 @@ static int __maybe_unused amd_suspend(struct device *dev) }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread);cancel_work_sync (&amd_manager->amd_sdw_work); -amd_sdw_wake_enable (amd_manager, false); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, false);if (ret)return ret;@@ -1470,7 +1473,6 @@ static int __maybe_unused amd_suspend(struct device *dev) ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; -amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;@@ -1510,18 +1512,24 @@ static int __maybe_unused amd_suspend_runtime(struct device *dev)return 0 ; }if (amd_manager->power_mode_mask &AMD_SDW_CLK_STOP_MODE ) { -amd_sdw_wake_enable (amd_manager, true); +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_host_wake_enable_by_rev (amd_manager, true);if (ret)return ret; ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret; +amd_enable_sdw_interrupts (amd_manager); +amd_sdw_wake_enable (amd_manager, true); }else if (amd_manager->power_mode_mask &AMD_SDW_POWER_OFF_MODE ) { +amd_disable_sdw_interrupts (amd_manager); +cancel_work_sync (&amd_manager->amd_sdw_irq_thread); +cancel_work_sync (&amd_manager->amd_sdw_work); ret =amd_sdw_clock_stop (amd_manager);if (ret)return ret;
Quoted 8 lines
drivers/soundwire/amd_manager.c static int __maybe_unused amd_suspend(struct device *dev) · line 1479
Quoted 4 lines
drivers/soundwire/amd_manager.c +0
-1-amd_disable_sdw_interrupts (amd_manager); ret =amd_disable_sdw_manager (amd_manager);if (ret)return ret;[Severity: Medium]
Can movingamd_disable_sdw_interrupts()beforecancel_work_sync()inAMD_SDW_POWER_OFF_MODEleaveACP_SW_STATE_CHANGE_STATUS_MASK_0TO7andACP_SW_STATE_CHANGE_STATUS_MASK_8TO11unmasked afteramd_sdw_workdrains?
++
No change required. As noted in the V7 reply, ACP_EXTERNAL_INTR_CNTL is the top-level delivery gate and remains closed for the full POWER_OFF_MODE suspend sequence, so stale MASK values from an in-flight amd_sdw_update_slave_status_work() cannot trigger an interrupt.
For ACP6.3 with wake_en_mask set, ACP_EXTERNAL_INTR_CNTL is re-opened in the wake block after amd_sdw_clock_stop() has already run. With the SoundWire link clock stopped, the hardware cannot assert any state-change interrupt: there is no bus activity and no enumeration sequence. The wake block also explicitly clears both STATUS registers before re-opening the gate:
writel(0x00, ACP_SW_STATE_CHANGE_STATUS_8TO11);
writel(0x00, ACP_SW_STATE_CHANGE_STATUS_0TO7);
...
amd_updatel(..., ACP_EXTERNAL_INTR_CNTL(...), val, val);
At the point the gate opens, STATUS == 0. With the clock stopped, hardware has no path to set state-change STATUS bits in D3, so (STATUS & MASK) stays 0 for all state-change bits regardless of the MASK register values.
False positive.
Quoted 38 lines
CPU0 kworker
amd_suspend_runtime()amd_sdw_update_slave_status_work()
amd_disable_sdw_interrupts()writel(0, ..._MASK_0TO7) writel(0, ..._MASK_8TO11) cancel_work_sync(&amd_sdw_work) if (status[0] == SDW_SLAVE_ATTACHED) writel(AMD_SDW_IRQ_MASK_0TO7, ...) writel(AMD_SDW_IRQ_MASK_8TO11, ...) ... amd_sdw_wake_enable(..., true) /* RMW preserves 0x000c7777 *//* ACP63: re-enables
ACP_EXTERNAL_INTR_CNTL*/
<-- state-change IRQs unmasked in D3As noted in
amd_disable_sdw_interrupts(), an in-flightamd_sdw_update_slave_status_work()on theSDW_SLAVE_ATTACHEDpath rewrites both status mask registers beforecancel_work_sync(&amd_manager->amd_sdw_work)returns. The comment inamd_disable_sdw_interrupts()assumes this is safe becauseACP_EXTERNAL_INTR_CNTLremains closed, and inAMD_SDW_CLK_STOP_MODEamd_enable_sdw_interrupts()overwrites the masks anyway.In
AMD_SDW_POWER_OFF_MODE, however, removing the post-clock-stopamd_disable_sdw_interrupts()call means nothing re-zeroes the mask registers afteramd_sdw_workfinishes. Inamd_suspend_runtime(),amd_sdw_wake_enable()then does a read-modify-write onACP_SW_STATE_CHANGE_STATUS_MASK_8TO11that preservesAMD_SDW_IRQ_MASK_8TO11alongsideAMD_SDW_WAKE_INTR_MASK, and onACP63_PCI_REV_IDwithwake_en_maskset, the driver setsinterrupt_enabled= true and re-opensACP_EXTERNAL_INTR_CNTLwhile all peripheral state-change and PREQ masks are still enabled with the manager powered off.Zeroing
ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7andACP_SW_STATE_CHANGE_STATUS_MASK_8TO11again aftercancel_work_sync(&amd_manager->amd_sdw_work)in thePOWER_OFF_MODEpaths would ensure only the wake interrupt is unmasked.
PATCH v8 21/24 soundwire: amd: check for NULL manager drvdata in sdw_amd_startup()
In reply to Vijendar Mukundasdw_amd_startup() calls dev_get_drvdata() to retrieve the per-manager context and passes the result unconditionally to amd_sdw_manager_start(). If the platform device for a manager instance was registered successfully but its probe failed (e.g. due to ENOMEM or a deferred probe), dev_get_drvdata() returns NULL and amd_sdw_manager_start() immediately dereferences it, causing a kernel panic.
Add a NULL check after dev_get_drvdata() and return -ENODEV so that a failed manager probe surfaces as an error from sdw_amd_probe() rather than a kernel crash.
--- drivers/soundwire/amd_init.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/soundwire/amd_init.c +4 -0
@@ -172,6 +172,10 @@ static int sdw_amd_startup(struct sdw_amd_ctx *ctx)if (!(ctx->link_mask &BIT (i)))continue ; amd_manager =dev_get_drvdata (&ctx->pdev[i]->dev); +if (!amd_manager) { +dev_err (&ctx->pdev[i]->dev,"SoundWire manager not probed\n" ); +return -ENODEV ; + } ret =amd_sdw_manager_start (amd_manager);if (ret)return ret;
Signature · 1 line
2.53.0
PATCH v8 22/24 soundwire: amd: fix shared-register RMW race in amd_sdw_set_device_state()
In reply to Vijendar MukundaAMD_SDW_DEVICE_STATE packs the D0/D3 state for SDW0 (bits [1:0]) and SDW1 (bits [3:2]) in a single ACP-level register. amd_sdw_set_device_state() performed a read-modify-write sequence on this shared register without holding acp_sdw_lock, leaving it open to a lost-update race when SDW0 and SDW1 execute concurrent PM transitions.
Acquire acp_sdw_lock around the read-modify-write sequence. Unlock before returning -EINVAL from the default branch so the mutex is always released on every exit path.
--- drivers/soundwire/amd_manager.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/soundwire/amd_manager.c +3 -0
@@ -292,6 +292,7 @@ static int amd_sdw_set_device_state(struct amd_sdw_manager *amd_manager, u32 tar {u32 sdw_dev_state; +mutex_lock (amd_manager->acp_sdw_lock); sdw_dev_state =readl (amd_manager->acp_mmio +AMD_SDW_DEVICE_STATE );switch (amd_manager->instance) {case ACP_SDW0 :
@@ -303,10 +304,12 @@ static int amd_sdw_set_device_state(struct amd_sdw_manager *amd_manager, u32 tar AMD_SDW1_DEVICE_STATE_MASK);break ;default : +mutex_unlock (amd_manager->acp_sdw_lock);return -EINVAL ; }writel (sdw_dev_state, amd_manager->acp_mmio +AMD_SDW_DEVICE_STATE ); sdw_dev_state =readl (amd_manager->acp_mmio +AMD_SDW_DEVICE_STATE ); +mutex_unlock (amd_manager->acp_sdw_lock);dev_dbg (amd_manager->dev,"AMD_SDW_DEVICE_STATE:0x%x\n" , sdw_dev_state);return 0 ; }
Signature · 1 line
2.53.0
PATCH v8 23/24 soundwire: amd: fix shared-register RMW race in amd_sdw_host_wake_enable()
In reply to Vijendar MukundaACP_EXTERNAL_INTR_CNTL(ACP_SDW1) holds host-wake interrupt bits for both instances (BIT(22) for SDW0, BIT(23) for SDW1). amd_sdw_host_wake_enable() performed a read-modify-write sequence on this shared register without holding acp_sdw_lock, leaving it open to a lost-update race when SDW0 and SDW1 execute concurrent PM transitions.
Acquire acp_sdw_lock around the read-modify-write sequence.
--- drivers/soundwire/amd_manager.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/soundwire/amd_manager.c +2 -0
@@ -333,12 +333,14 @@ static int amd_sdw_host_wake_enable(struct amd_sdw_manager *amd_manager, bool enreturn -EINVAL ; } +mutex_lock (amd_manager->acp_sdw_lock); intr_cntl1 =readl (amd_manager->acp_mmio +ACP_EXTERNAL_INTR_CNTL (ACP_SDW1 ));if (enable) intr_cntl1 |= sdw_host_wake_irq_mask;else intr_cntl1 &= ~sdw_host_wake_irq_mask;writel (intr_cntl1, amd_manager->acp_mmio +ACP_EXTERNAL_INTR_CNTL (ACP_SDW1 )); +mutex_unlock (amd_manager->acp_sdw_lock);return 0 ; }
Signature · 1 line
2.53.0
PATCH v8 24/24 soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
In reply to Vijendar Mukundasdw_handle_slave_alerts() calls pm_runtime_get_sync(&slave->dev) which, when slave->dev is not in RPM_ACTIVE, calls rpm_resume() on the slave. rpm_resume() propagates synchronously up the parent chain: slave->dev -> bus->dev -> manager->dev. If manager->dev is in RPM_SUSPENDING at that point, rpm_resume() blocks on dev->power.wait_queue waiting for the suspend to complete.
This creates a deadlock: the manager's runtime_suspend callback (e.g. amd_suspend_runtime() or intel_suspend_runtime()) is blocked in cancel_work_sync() waiting for the alert work to finish, while the alert work is blocked in pm_runtime_get_sync() waiting for the manager's runtime_suspend to complete.
Fix this by replacing pm_runtime_get_sync() with pm_runtime_get_if_active(), which increments the usage counter and returns 1 only if slave->dev is already in RPM_ACTIVE, and returns 0 immediately without calling rpm_resume() otherwise.
Children suspend before parents, so when the parent manager is in RPM_SUSPENDING the slave is already RPM_SUSPENDED. The early return is therefore conditioned on pm_runtime_status_suspended() so that only a genuinely suspended slave triggers the deferral path:
- When the SoundWire link enters clock-stop mode, the MIPI SoundWire spec guarantees the peripheral re-asserts the alert after clock resume, so the interrupt is deferred rather than dropped.
- When the link fully powers off (e.g. D3 or equivalent), the peripheral loses power and its alert state does not survive; the early return is harmless.
When ret == 0 but the slave is not suspended (RPM_RESUMING or RPM_SUSPENDING), the parent manager must be active so there is no deadlock risk and the hardware is accessible. These cases fall through to read and clear interrupt registers without a PM reference.
When ret == 0, the usage counter was not incremented, so the early return must bypass the io_err: label (which calls pm_runtime_put_autosuspend()) to avoid a reference count underflow.
pm_runtime_get_if_active() returns -EINVAL when PM is disabled on the device (disable_depth > 0, e.g. during sdw_delete_slave() teardown) or when CONFIG_PM=n. In both cases the old pm_runtime_get_sync() path returned -EACCES, which the original guard exempted so alert handling could proceed without a PM reference. Preserve this behaviour: when ret < 0, fall through to read and clear hardware interrupt registers without a reference. Introduce have_pm_ref to guard the pm_runtime_put_autosuspend() call at io_err: so it is only reached when a reference was actually taken.
--- drivers/soundwire/bus.c | 45 ++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)
drivers/soundwire/bus.c +38 -7
@@ -1667,15 +1667,44 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)u8 buf, buf2[2 ];bool parity_check;bool parity_quirk; +bool have_pm_ref = false;sdw_modify_slave_status (slave,SDW_SLAVE_ALERT ); - ret =pm_runtime_get_sync (&slave->dev); -if (ret <0 && ret != -EACCES ) { -dev_err (&slave->dev,"Failed to resume device: %d\n" , ret); -pm_runtime_put_noidle (&slave->dev); -return ret; + ret =pm_runtime_get_if_active (&slave->dev); +if (ret ==0 &&pm_runtime_status_suspended (&slave->dev)) { +/* +* Slave device is genuinely suspended. Return early to avoid +* I/O on an inaccessible device and to prevent a deadlock: +* children suspend before parents, so a suspended slave +* implies the parent manager may be in RPM_SUSPENDING, where +* its runtime_suspend callback blocks in cancel_work_sync(). +* Calling pm_runtime_get_sync() here would propagate up to +* the manager and deadlock. +* +* When the SoundWire link enters clock-stop mode, the MIPI +* SoundWire spec guarantees the peripheral re-asserts the +* alert after clock resume, so the interrupt is deferred +* rather than dropped. When the link fully powers off +* (e.g. D3 or equivalent), the peripheral loses power and +* its alert state does not survive; the early return is +* harmless. +*/ +dev_dbg (&slave->dev,"Alert deferred: slave suspended\n" ); +return 0 ; } +/* +* ret == 0 but slave not suspended (RPM_RESUMING or RPM_SUSPENDING): +* hardware is accessible and no deadlock risk applies since the parent +* manager must be active for the slave to be resuming. Proceed +* without a PM reference to read and clear interrupt registers. +* ret < 0 (-EINVAL): PM is disabled (disable_depth > 0 or CONFIG_PM=n); +* proceed without a reference, consistent with the previous +* -EACCES exemption. Skip PM release inside io_err: since no reference +* was taken. +*/ +if (ret >0 ) + have_pm_ref = true;/* Read Intstat 1, Intstat 2 and Intstat 3 registers */ ret =sdw_read_no_pm (slave,SDW_SCP_INT1 );
@@ -1861,8 +1890,10 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)dev_warn (&slave->dev,"Reached MAX_RETRY on alert read\n" ); io_err: -pm_runtime_mark_last_busy (&slave->dev); -pm_runtime_put_autosuspend (&slave->dev); +if (have_pm_ref) { +pm_runtime_mark_last_busy (&slave->dev); +pm_runtime_put_autosuspend (&slave->dev); + }return ret; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v8 24/24] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] soundwire: bus: sdw_handle_slave_alerts() drops alerts when slave is suspended - [High] soundwire…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] soundwire: bus: sdw_handle_slave_alerts() drops alerts when slave is suspended
- [High] soundwire: bus: sdw_handle_slave_alerts() races with slave runtime PM callbacks
- [Low] soundwire: bus: inaccurate Fixes trailer in commit message --
commit 30482e5286dcebe16b970ebf3c3846bd0b707da1
Author: Vijendar Mukunda <[email protected]>
Quoted 51 lines
soundwire: bus: use
pm_runtime_get_if_active()insdw_handle_slave_alerts()
sdw_handle_slave_alerts()callspm_runtime_get_sync(&slave->dev)which, whenslave->devis not inRPM_ACTIVE, callsrpm_resume()on the slave.rpm_resume()propagates synchronously up the parent chain:slave->dev->bus->dev->manager->dev. Ifmanager->devis inRPM_SUSPENDINGat that point,rpm_resume()blocks ondev->power.wait_queuewaiting for the suspend to complete.This creates a deadlock: the manager's
runtime_suspendcallback (e.g.amd_suspend_runtime()orintel_suspend_runtime()) is blocked incancel_work_sync()waiting for the alert work to finish, while the alert work is blocked inpm_runtime_get_sync()waiting for the manager'sruntime_suspendto complete.Fix this by replacing
pm_runtime_get_sync()withpm_runtime_get_if_active(), which increments the usage counter and returns 1 only ifslave->devis already inRPM_ACTIVE, and returns 0 immediately without callingrpm_resume()otherwise.Children suspend before parents, so when the parent manager is in
RPM_SUSPENDINGthe slave is alreadyRPM_SUSPENDED. The early return is therefore conditioned onpm_runtime_status_suspended()so that only a genuinely suspended slave triggers the deferral path:- When the SoundWire link enters clock-stop mode, the MIPI SoundWire spec guarantees the peripheral re-asserts the alert after clock resume, so the interrupt is deferred rather than dropped.
- When the link fully powers off (e.g. D3 or equivalent), the peripheral loses power and its alert state does not survive; the early return is harmless.When ret == 0 but the slave is not suspended (
RPM_RESUMINGorRPM_SUSPENDING), the parent manager must be active so there is no deadlock risk and the hardware is accessible. These cases fall through to read and clear interrupt registers without a PM reference.When ret == 0, the usage counter was not incremented, so the early return must bypass the
io_err: label (which callspm_runtime_put_autosuspend()) to avoid a reference count underflow.
pm_runtime_get_if_active()returns-EINVALwhen PM is disabled on the device (disable_depth> 0, e.g. duringsdw_delete_slave()teardown) or whenCONFIG_PM=n. In both cases the oldpm_runtime_get_sync()path returned-EACCES, which the original guard exempted so alert handling could proceed without a PM reference. Preserve this behaviour: when ret < 0, fall through to read and clear hardware interrupt registers without a reference. Introducehave_pm_refto guard thepm_runtime_put_autosuspend()call atio_err: so it is only reached when a reference was actually taken.
Quoted 2 lines
Fixes e9537962519e ("soundwire: bus: Fix unbalanced pm_runtime_put() causing usage count underflow")Signed-off-by Vijendar Mukunda <[email protected]>
[Severity: Low]
Commit e9537962519e ("soundwire: bus: Fix unbalanced pm_runtime_put() causing usage count underflow") only changed pm_runtime_resume_and_get() back to pm_runtime_get_sync() to fix an error-path underflow. Synchronous runtime resume in sdw_handle_slave_alerts() was introduced by commit aa79293517b3 ("soundwire: bus: fix io error when processing alert event"), so should the Fixes tag reference that commit instead?
Quoted 29 lines
drivers/soundwire/bus.c +13
-5@@ -1667,15 +1667,44 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)u8 buf, buf2[2 ];bool parity_check;bool parity_quirk; +bool have_pm_ref = false;sdw_modify_slave_status (slave,SDW_SLAVE_ALERT ); - ret =pm_runtime_get_sync (&slave->dev); -if (ret <0 && ret != -EACCES ) { -dev_err (&slave->dev,"Failed to resume device: %d\n" , ret); -pm_runtime_put_noidle (&slave->dev); -return ret; + ret =pm_runtime_get_if_active (&slave->dev); +if (ret ==0 &&pm_runtime_status_suspended (&slave->dev)) { +/* +* Slave device is genuinely suspended. Return early to avoid +* I/O on an inaccessible device and to prevent a deadlock: +* children suspend before parents, so a suspended slave +* implies the parent manager may be in RPM_SUSPENDING, where +* its runtime_suspend callback blocks in cancel_work_sync(). +* Calling pm_runtime_get_sync() here would propagate up to +* the manager and deadlock. +* +* When the SoundWire link enters clock-stop mode, the MIPI
drivers/soundwire/bus.c static int sdw_handle_slave_alerts(struct sdw_slave *slave) · line 1695
Quoted 10 lines
drivers/soundwire/bus.c +9
-0+* SoundWire spec guarantees the peripheral re-asserts the +* alert after clock resume, so the interrupt is deferred +* rather than dropped. When the link fully powers off +* (e.g. D3 or equivalent), the peripheral loses power and +* its alert state does not survive; the early return is +* harmless. +*/ +dev_dbg (&slave->dev,"Alert deferred: slave suspended\n" ); +return 0 ; }
[Severity: High]
Does returning early whenever slave->dev is RPM_SUSPENDED drop peripheral alerts while the manager is active or after waking from Clock Stop Mode 0?
SoundWire codec drivers enable runtime PM autosuspend on slave->dev, so a peripheral is routinely in RPM_SUSPENDED while the manager is still RPM_ACTIVE (for example during the manager's autosuspend delay, when another peripheral on the link is active, or when clock stop is disabled). Likewise, on wake from Clock Stop Mode 0, manager wake handlers such as amd_sdw_process_wake_event() and intel_link_process_wakeen_event() only call pm_request_resume() on the manager device; because peripherals remain SDW_SLAVE_ATTACHED across Clock Stop Mode 0 without re-enumeration, sdw_handle_slave_status() never calls pm_request_resume(&slave->dev).
With pm_runtime_get_sync(&slave->dev) removed here, nothing resumes slave->dev and SDW_SCP_INT1 is never cleared. The peripheral stays stuck in SDW_SLAVE_ALERT without notifying the codec driver, and on Qualcomm controllers qcom_swrm_irq_handler() will loop indefinitely because SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ remains asserted.
drivers/soundwire/bus.c static int sdw_handle_slave_alerts(struct sdw_slave *slave) · line 1707
Quoted 12 lines
drivers/soundwire/bus.c +12
-0+/* +* ret == 0 but slave not suspended (RPM_RESUMING or RPM_SUSPENDING): +* hardware is accessible and no deadlock risk applies since the parent +* manager must be active for the slave to be resuming. Proceed +* without a PM reference to read and clear interrupt registers. +* ret < 0 (-EINVAL): PM is disabled (disable_depth > 0 or CONFIG_PM=n); +* proceed without a reference, consistent with the previous +* -EACCES exemption. Skip PM release inside io_err: since no reference +* was taken. +*/ +if (ret >0 ) + have_pm_ref = true;
[Severity: High]
When ret == 0 and slave->dev is in RPM_SUSPENDING or RPM_RESUMING, can falling through without waiting for the transition or holding a PM reference race with the slave driver's runtime PM callbacks and still deadlock?
CPU0 (alert work) CPU1 (PM workqueue)sdw_handle_slave_alerts()
rpm_suspend(&slave->dev)
/* status == RPM_SUSPENDING */
pm_runtime_get_if_active() -> 0
pm_runtime_status_suspended() -> false
rt711_sdca_dev_suspend()
cancel_delayed_work_sync(...)
regcache_cache_only(..., true)
/* slave->dev -> RPM_SUSPENDED */
rpm_suspend(manager->dev)
/* manager->dev -> RPM_SUSPENDING */
amd_suspend_runtime()
cancel_work_sync(&amd_sdw_work)
handle_nested_irq(slave->irq)
regmap_irq_thread()
pm_runtime_get_sync(&slave->dev) <-- deadlocks on manager->devBesides the deadlock when a domain IRQ handler with runtime_pm enabled (such as sdca_irq_chip or cs42l43_irq_chip via regmap_irq_thread()) runs after slave->dev finishes suspending, drv->ops->interrupt_callback() and handle_nested_irq() now run concurrently with .runtime_suspend and .runtime_resume while the codec's regmap is in cache-only mode, failing register accesses while sdw_handle_slave_alerts() clears SDW_SCP_INT1 and drops the interrupt.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
Re: [PATCH v8 24/24] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
Quoted 65 lines · On 23/11/26 23:03, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] soundwire: bus:sdw_handle_slave_alerts()drops alerts when slave is suspended
- [High] soundwire: bus:sdw_handle_slave_alerts()races with slave runtime PM callbacks
- [Low] soundwire: bus: inaccurate Fixes trailer in commit message --commit
30482e5286dcebe16b970ebf3c3846bd0b707da1
Author: Vijendar Mukunda <[email protected]>Quoted 51 lines
soundwire: bus: use
pm_runtime_get_if_active()insdw_handle_slave_alerts()
sdw_handle_slave_alerts()callspm_runtime_get_sync(&slave->dev)which, whenslave->devis not inRPM_ACTIVE, callsrpm_resume()on the slave.rpm_resume()propagates synchronously up the parent chain:slave->dev->bus->dev->manager->dev. Ifmanager->devis inRPM_SUSPENDINGat that point,rpm_resume()blocks ondev->power.wait_queuewaiting for the suspend to complete.This creates a deadlock: the manager's
runtime_suspendcallback (e.g.amd_suspend_runtime()orintel_suspend_runtime()) is blocked incancel_work_sync()waiting for the alert work to finish, while the alert work is blocked inpm_runtime_get_sync()waiting for the manager'sruntime_suspendto complete.Fix this by replacing
pm_runtime_get_sync()withpm_runtime_get_if_active(), which increments the usage counter and returns 1 only ifslave->devis already inRPM_ACTIVE, and returns 0 immediately without callingrpm_resume()otherwise.Children suspend before parents, so when the parent manager is in
RPM_SUSPENDINGthe slave is alreadyRPM_SUSPENDED. The early return is therefore conditioned onpm_runtime_status_suspended()so that only a genuinely suspended slave triggers the deferral path:- When the SoundWire link enters clock-stop mode, the MIPI SoundWire spec guarantees the peripheral re-asserts the alert after clock resume, so the interrupt is deferred rather than dropped.
- When the link fully powers off (e.g. D3 or equivalent), the peripheral loses power and its alert state does not survive; the early return is harmless.When ret == 0 but the slave is not suspended (
RPM_RESUMINGorRPM_SUSPENDING), the parent manager must be active so there is no deadlock risk and the hardware is accessible. These cases fall through to read and clear interrupt registers without a PM reference.When ret == 0, the usage counter was not incremented, so the early return must bypass the
io_err: label (which callspm_runtime_put_autosuspend()) to avoid a reference count underflow.
pm_runtime_get_if_active()returns-EINVALwhen PM is disabled on the device (disable_depth> 0, e.g. duringsdw_delete_slave()teardown) or whenCONFIG_PM=n. In both cases the oldpm_runtime_get_sync()path returned-EACCES, which the original guard exempted so alert handling could proceed without a PM reference. Preserve this behaviour: when ret < 0, fall through to read and clear hardware interrupt registers without a reference. Introducehave_pm_refto guard thepm_runtime_put_autosuspend()call atio_err: so it is only reached when a reference was actually taken.Quoted 2 lines
Fixes e9537962519e ("soundwire: bus: Fix unbalanced pm_runtime_put() causing usage count underflow")Signed-off-by Vijendar Mukunda <[email protected]>
Quoted 7 lines
[Severity: Low]
Commite9537962519e("soundwire: bus: Fix unbalancedpm_runtime_put()causing usage count underflow") only changedpm_runtime_resume_and_get()back topm_runtime_get_sync()to fix an error-path underflow. Synchronous runtime resume insdw_handle_slave_alerts()was introduced by commitaa79293517b3("soundwire: bus: fix io error when processing alert event"), so should the Fixes tag reference that commit instead?
Yes, you are right. aa79293517b3 introduced the synchronous runtime resume in sdw_handle_slave_alerts(). e9537962519e only changed the PM API and did not introduce the deadlock.
I will correct the Fixes tag to:
alert event")
Quoted 35 lines
Quoted 35 lines
drivers/soundwire/bus.c +18
-5@@ -1667,15 +1667,44 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)u8 buf, buf2[2 ];bool parity_check;bool parity_quirk; +bool have_pm_ref = false;sdw_modify_slave_status (slave,SDW_SLAVE_ALERT ); - ret =pm_runtime_get_sync (&slave->dev); -if (ret <0 && ret != -EACCES ) { -dev_err (&slave->dev,"Failed to resume device: %d\n" , ret); -pm_runtime_put_noidle (&slave->dev); -return ret; + ret =pm_runtime_get_if_active (&slave->dev); +if (ret ==0 &&pm_runtime_status_suspended (&slave->dev)) { +/* +* Slave device is genuinely suspended. Return early to avoid +* I/O on an inaccessible device and to prevent a deadlock: +* children suspend before parents, so a suspended slave +* implies the parent manager may be in RPM_SUSPENDING, where +* its runtime_suspend callback blocks in cancel_work_sync(). +* Calling pm_runtime_get_sync() here would propagate up to +* the manager and deadlock. +* +* When the SoundWire link enters clock-stop mode, the MIPI +* SoundWire spec guarantees the peripheral re-asserts the +* alert after clock resume, so the interrupt is deferred +* rather than dropped. When the link fully powers off +* (e.g. D3 or equivalent), the peripheral loses power and +* its alert state does not survive; the early return is
Quoted 8 lines
drivers/soundwire/bus.c static int sdw_handle_slave_alerts(struct sdw_slave *slave) · line 1695
Quoted 5 lines
drivers/soundwire/bus.c +4
-0+* harmless. +*/ +dev_dbg (&slave->dev,"Alert deferred: slave suspended\n" ); +return 0 ; }[Severity: High]
Does returning early wheneverslave->devisRPM_SUSPENDEDdrop peripheral alerts while the manager is active or after waking from Clock Stop Mode 0?
Yes, this is an issue with the current patch. A suspended slave does not imply that the manager is suspending, so checking the slave PM state can incorrectly drop alerts while the manager is still active.
I will rework this to hold a manager PM reference using pm_runtime_get_if_active(slave->bus->dev->parent). If the manager is active, the slave can be resumed and the alert processed while the manager reference prevents it from suspending concurrently. Otherwise, the alert handling will be deferred.
Quoted 53 lines
SoundWire codec drivers enable runtime PM autosuspend on
slave->dev, so a peripheral is routinely inRPM_SUSPENDEDwhile the manager is stillRPM_ACTIVE(for example during the manager's autosuspend delay, when another peripheral on the link is active, or when clock stop is disabled). Likewise, on wake from Clock Stop Mode 0, manager wake handlers such asamd_sdw_process_wake_event()andintel_link_process_wakeen_event()only callpm_request_resume()on the manager device; because peripherals remainSDW_SLAVE_ATTACHEDacross Clock Stop Mode 0 without re-enumeration,sdw_handle_slave_status()never callspm_request_resume(&slave->dev).With
pm_runtime_get_sync(&slave->dev)removed here, nothing resumesslave->devandSDW_SCP_INT1is never cleared. The peripheral stays stuck inSDW_SLAVE_ALERTwithout notifying the codec driver, and on Qualcomm controllersqcom_swrm_irq_handler()will loop indefinitely becauseSWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQremains asserted.drivers/soundwire/bus.c static int sdw_handle_slave_alerts(struct sdw_slave *slave) · line 1707
Quoted 12 lines
drivers/soundwire/bus.c +12
-0+/* +* ret == 0 but slave not suspended (RPM_RESUMING or RPM_SUSPENDING): +* hardware is accessible and no deadlock risk applies since the parent +* manager must be active for the slave to be resuming. Proceed +* without a PM reference to read and clear interrupt registers. +* ret < 0 (-EINVAL): PM is disabled (disable_depth > 0 or CONFIG_PM=n); +* proceed without a reference, consistent with the previous +* -EACCES exemption. Skip PM release inside io_err: since no reference +* was taken. +*/ +if (ret >0 ) + have_pm_ref = true;[Severity: High]
When ret == 0 andslave->devis inRPM_SUSPENDINGorRPM_RESUMING, can falling through without waiting for the transition or holding a PM reference race with the slave driver's runtime PM callbacks and still deadlock?CPU0 (alert work) CPU1 (PM workqueue)
sdw_handle_slave_alerts()rpm_suspend(&slave->dev) /* status == RPM_SUSPENDING */ pm_runtime_get_if_active() -> 0 pm_runtime_status_suspended() -> false rt711_sdca_dev_suspend() cancel_delayed_work_sync(...) regcache_cache_only(..., true) /* slave->dev -> RPM_SUSPENDED */ rpm_suspend(manager->dev) /* manager->dev -> RPM_SUSPENDING */ amd_suspend_runtime() cancel_work_sync(&amd_sdw_work) handle_nested_irq(slave->irq) regmap_irq_thread() pm_runtime_get_sync(&slave->dev) <-- deadlocks on manager->dev
Quoted 7 lines
Besides the deadlock when a domain IRQ handler with
runtime_pmenabled (such assdca_irq_chiporcs42l43_irq_chipviaregmap_irq_thread()) runs afterslave->devfinishes suspending,drv->ops->interrupt_callback()andhandle_nested_irq()now run concurrently with .runtime_suspend and .runtime_resume while the codec's regmap is in cache-only mode, failing register accesses whilesdw_handle_slave_alerts()clearsSDW_SCP_INT1and drops the interrupt.
Agreed. Holding the manager PM reference while resuming and processing the slave alert also prevents this race with manager suspend.
I will send v9 with:
- Fixes tag corrected to aa79293517b3.
- Manager PM state checked with pm_runtime_get_if_active().
- Manager PM reference held while resuming and handling the slave alert.
- Commit message updated to describe the revised PM handling.