Skip to content

ipc4: replace fw_reg spinlock with mutex#10693

Open
kv2019i wants to merge 2 commits intothesofproject:mainfrom
kv2019i:202604-fw-regs-remove-spinlocks
Open

ipc4: replace fw_reg spinlock with mutex#10693
kv2019i wants to merge 2 commits intothesofproject:mainfrom
kv2019i:202604-fw-regs-remove-spinlocks

Conversation

@kv2019i
Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i commented Apr 9, 2026

Series to add sys/mutex.h support for SOF audio code and use it to remove spinlocks from ipc4/dai.c code.

kv2019i added 2 commits April 9, 2026 16:44
To enable use of Zephyr sys/mutex.h interface in SOF audio pipeline
code, add a rtos/mutex.h wrapper for this. In Zephyr build, the native
Zephyr interface is used and in posix builds (e.g. testbench), a no-op
implementation is provided (similarly that already exists for k_mutex).

Signed-off-by: Kai Vehmanen <[email protected]>
Replace the spinlock-based synchronization mechanism for firmware register
access with a mutex-based approach. This change removes the fw_reg_lock
spinlock field from the sof structure (both POSIX and Zephyr variants),
eliminates the spinlock initialization in primary_core_init(), and updates
the dai_dma_release(), dai_release_llp_slot(), and dai_get_unused_llp_slot()
functions to use sys_mutex_lock/unlock instead of k_spin_lock/unlock.

All SOF IPC and audio pipeline code is run in threads, so using
spinlocks is no longer necessary. Using mutex is a best fit and allows
the audio pipeline code to be used also in user-space threads.

Signed-off-by: Kai Vehmanen <[email protected]>
Copilot AI review requested due to automatic review settings April 9, 2026 14:10
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented Apr 9, 2026

I'm using this to move audio pipelines to user-space in #10558 , but wanted to submit this early to get review feedback. If ok, I'll proceed to use sys_mutex more widely in the audio pipeline code.

Copy link
Copy Markdown
Contributor

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR replaces IPC4 firmware register (fw_reg) synchronization from a per-struct sof Zephyr spinlock to a sys_mutex, with the goal of removing spinlocks from ipc4/dai.c.

Changes:

  • Remove fw_reg_lock (k_spinlock) from struct sof for both Zephyr and POSIX builds.
  • Introduce a file-scope SYS_MUTEX_DEFINE(fw_reg_lock) in src/ipc/ipc4/dai.c and use sys_mutex_lock/unlock() around mailbox register accesses.
  • Add sys_mutex header support in Zephyr and a stub sys_mutex implementation for POSIX builds.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
zephyr/include/rtos/sof.h Removes IPC4 fw_reg_lock from struct sof.
zephyr/include/rtos/mutex.h Includes Zephyr sys/mutex.h to support sys_mutex.
src/ipc/ipc4/dai.c Replaces k_spin_lock usage with a static sys_mutex for fw register access.
src/init/init.c Removes initialization of the deleted sof->fw_reg_lock spinlock.
posix/include/rtos/sof.h Mirrors Zephyr: removes fw_reg_lock from struct sof.
posix/include/rtos/mutex.h Adds a POSIX-side sys_mutex API stub.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +43 to +63
/* provide a no-op implementation for zephyr/sys/mutex.h */

struct sys_mutex {
};

#define SYS_MUTEX_DEFINE(name) \
struct sys_mutex name

static inline void sys_mutex_init(struct sys_mutex *mutex)
{
}

static inline int sys_mutex_lock(struct sys_mutex *mutex, k_timeout_t timeout)
{
return 0;
}

static inline int sys_mutex_unlock(struct sys_mutex *mutex)
{
return 0;
}
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The POSIX sys_mutex_*() implementation is a no-op, so the new critical sections in ipc4/dai.c are not actually synchronized under POSIX builds. If POSIX can execute these paths concurrently (threads or async callbacks), this introduces data races on mailbox fw registers. Implement sys_mutex using a real lock (e.g., pthread_mutex_t) or map sys_mutex_*() onto the existing k_mutex_*() wrappers in this header so the locking semantics remain effective on POSIX.

Copilot uses AI. Check for mistakes.
Comment on lines +231 to +233
sys_mutex_lock(&fw_reg_lock, K_FOREVER);
mailbox_sw_regs_write(dd->slot_info.reg_offset, &slot, sizeof(slot));
k_spin_unlock(&sof_get()->fw_reg_lock, key);
sys_mutex_unlock(&fw_reg_lock);
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

sys_mutex_lock() / sys_mutex_unlock() return status codes, but the results are currently ignored. If the Zephyr implementation can fail (e.g., timeout/cancellation/invalid context), execution would continue without the intended protection. Consider checking the return values (at least asserting success for K_FOREVER, or propagating an error) in all three call sites so failures don’t silently degrade synchronization.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants