Skip to content

Conversation

kv2019i
Copy link
Contributor

@kv2019i kv2019i commented Sep 2, 2025

Fix unsafe usage of k_sleep() in interrupt/nonblocking context.

@@ -315,7 +315,7 @@ static inline void dai_dmic_en_power(const struct dai_intel_dmic *dmic)
#if defined(CONFIG_SOC_INTEL_ACE20_LNL) || defined(CONFIG_SOC_INTEL_ACE30) || \
defined(CONFIG_SOC_INTEL_ACE40)
while (!(sys_read32(base + DMICLCTL_OFFSET) & DMICLCTL_CPA)) {
k_sleep(K_USEC(100));
k_busy_wait(100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this sentence in commit message:
"And even on platforms where this code is used, k_sleep() is not always called, so hitting real issues requires multiple conditions to be met at runtime."

k_busy_wait(100);
if (!WAIT_FOR((sys_read32(base + DMICLCTL_OFFSET) & DMICLCTL_CPA) != 0, 1000,
k_busy_wait(100))) {
LOG_ERR("power-up timeout");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it an error if we timing out waiting for CPA to be set? I see, the dai_dmic_en_power() is void, cannot fail.

The call to k_sleep() is not safe as dai_dmic_probe() is called
from PM dmic_pm_action() and k_can_yield() may be false.

Problem was caught on Intel WCL ADSP platform with SOF as application
and a Zephyr build with asserts enabled.

Fix the issue by using k_busy_wait() instead.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i kv2019i force-pushed the 202508-dmic-power-up-fix branch from d16cc7f to 21013a3 Compare September 3, 2025 10:20
@zephyrbot zephyrbot added the size: XS A PR changing only a single line of code label Sep 3, 2025
@kv2019i
Copy link
Contributor Author

kv2019i commented Sep 3, 2025

V2:

  • rephrase the commit message
  • drop the second commit. let's merge the more urgent fix first and I'll do a follow PR where I'll refactor probe error handling a bit more in dmic drive (we have multiple places with register write timeouts but no error propagation back)

@kv2019i kv2019i requested review from tmleman and Copilot September 3, 2025 10:22
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes unsafe usage of k_sleep() in PM (Power Management) callbacks by replacing it with k_busy_wait(). The sleep function cannot be called in interrupt or non-blocking contexts, which PM callbacks typically execute in.

  • Replaces k_sleep(K_USEC(100)) with k_busy_wait(100) in power enable function
  • Ensures proper synchronization without blocking in PM callback context

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 317 to 319
while (!(sys_read32(base + DMICLCTL_OFFSET) & DMICLCTL_CPA)) {
k_sleep(K_USEC(100));
k_busy_wait(100);
}
Copy link
Preview

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The busy wait loop could potentially run indefinitely if the hardware never sets the CPA bit. Consider adding a timeout counter or maximum iteration limit to prevent infinite spinning and provide error handling for hardware failure scenarios.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack on that. I'll do a separate PR to add the a timeout, but I want to fix the error reporting in other places as well, so I think better done in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not fix it in one go? This loop is incorrect anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ujfalusi this PR is urgent and I need more time to do the follow-up and test it

Copy link

sonarqubecloud bot commented Sep 3, 2025

@kv2019i kv2019i requested review from teburd and dcpleung September 3, 2025 13:56
@kartben kartben merged commit fa02e37 into zephyrproject-rtos:main Sep 4, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: DAI platform: Intel ADSP Intel Audio platforms size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants