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.

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 <kai.vehmanen@linux.intel.com>
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.


LOG_MODULE_DECLARE(ipc, CONFIG_SOF_LOG_LEVEL);

static SYS_MUTEX_DEFINE(fw_reg_lock);
Copy link
Copy Markdown
Collaborator

@lyakh lyakh Apr 10, 2026

Choose a reason for hiding this comment

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

is it now only used by the DAI code? Maybe reflect that in the name? Also, this object isn't accessible from the userspace, so it can be a normal k_mutex or is there a plan to make it userspace-accessible in the future? In fact should we follow the Linux example and define exactly what it is protecting?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes. Not sure why the spinlock was aded to the sof() singleton, but it seems just not needed and cleaner to have a local lock. I have a one-liner patch to export this to user-space with:

-static SYS_MUTEX_DEFINE(fw_reg_lock);
+static APP_SYSUSER_BSS SYS_MUTEX_DEFINE(fw_reg_lock);

Ack on the naming, let me address the naming in V2. I just following the old naming practise, but agreed, this can be improved.

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 <kai.vehmanen@linux.intel.com>
@kv2019i kv2019i force-pushed the 202604-fw-regs-remove-spinlocks branch from 47deec9 to ead1b8b Compare April 10, 2026 09:10
@kv2019i
Copy link
Copy Markdown
Collaborator Author

kv2019i commented Apr 10, 2026

V2 pushed:

  • addressed comments from @lyakh and copilot

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.

3 participants