Skip to content

treewide: use the Zephyr atomic names and remove rtos/atomic.h - #11596

Draft
lgirdwood wants to merge 6 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-atomic-names
Draft

lgirdwood wants to merge 6 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-atomic-names

Conversation

@lgirdwood

@lgirdwood lgirdwood commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Use the Zephyr atomic names in all code and remove rtos/atomic.h. Part of #11517 (steps 4 and 16). Depends on #11583, which adds the host <zephyr/sys/atomic.h> stub; the first four commits here are that PR.

Patches

  1. treewide: atomic_read()/atomic_init() → atomic_get()/atomic_set() (22 call sites), and remove the SOF names from both versions of rtos/atomic.h.
    • atomic_set() returns the previous value where atomic_init() returned nothing; no caller uses it.
    • The Zephyr atomic_read() cast its value to long for the %ld log formats. atomic_get() already returns long on Zephyr, so the formats are unaffected.
  2. treewide: include <zephyr/sys/atomic.h> and remove rtos/atomic.h. Both versions only included <zephyr/sys/atomic.h> after patch 1: Zephyr's header on Zephyr, the stub on the host. The host arch atomics are now only included from the stub.

Functional change

None.

Testing

  • Builds: tested as a stack of the four Wave C PRs on top of posix: host stubs for the Zephyr atomic, cache and assert APIs #11583, first on main 829c0ac: every commit built incrementally and the tip from scratch, for tgl, tgl-h, mtl, lnl, ptl, wcl, nvl, nvl-s, imx8, imx8x, imx8m, imx8m_cm7, imx8ulp, imx93, imx95, qemu_xtensa, qemu_xtensa_mmu, native_sim, and the PTL userspace, LL-userspace and LLEXT, MTL LLEXT, TGL/PTL debug-stream and TGL/MTL/LNL/PTL CI overlay configurations.
  • Objects: every object hashed with the debug sections stripped and compared commit by commit. Neither commit changes any object, apart from the objects that embed the git version.
  • Platforms that already fail on main (acp_6_0, acp_7_0, acp_7_x, mt8186, mt8188, mt8195, mt8196, mt8365, phyboard): same error sets.
  • After the rebase on main c95d85f and the updated posix: host stubs for the Zephyr atomic, cache and assert APIs #11583: ptl, ptl-us, mtl, lnl, tgl, imx8m and native_sim build.
  • Cadence xt-clang: MTL, PTL and NVL compile.
  • ZTest: 38 of 38 configurations pass, run as in CI.
  • Host: the host library builds, and the CI testbench steps (SOF alsa-lib/alsa-utils, test topologies, testbench build, host-testbench.sh) pass all 19 component tests.

🤖 Generated with Claude Code

lrgirdwo and others added 6 commits December 3, 2026 11:50
The non-Zephyr host builds (the host library and the testbench) compile
the shared code against posix/include, which gives it the SOF atomic
names from rtos/atomic.h. To switch that code to the Zephyr names, these
builds need a <zephyr/sys/atomic.h> that behaves like Zephyr's. Zephyr
builds, native_sim included, keep using the Zephyr header.

Add the stub with atomic_get(), atomic_set(), atomic_add() and
atomic_sub(). Like Zephyr, atomic_set(), atomic_add() and atomic_sub()
return the previous value. The GCC version of the host atomics returned
the new value from add and sub, while the xt-xcc version (for the xt-run
testbench) already returned the previous one. Make the GCC version
return the previous value too, and replace the set and init functions
with a swap that returns it. No host caller uses these return values.

rtos/atomic.h now keeps only atomic_read() and atomic_init() on top of
the stub, as on Zephyr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
In the non-Zephyr host builds (the host library and the testbench),
shared code calls the SOF cache wrappers because posix/include has no
<zephyr/cache.h>. Add a stub with sys_cache_data_flush_range(),
sys_cache_data_invd_range(), sys_cache_data_flush_and_invd_range() and
sys_cache_instr_invd_range(). The host has no cache to maintain, so they
return -ENOTSUP, like Zephyr built without CONFIG_CACHE_MANAGEMENT.
Zephyr builds, native_sim included, keep using the Zephyr header.

The cached and uncached pointer conversions are not stubbed yet: the
host currently reports every pointer as uncached, which does not map
directly onto the Zephyr calls. They come with their first user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Shared code uses assert() and sof_panic() because the non-Zephyr host
builds (the host library and the testbench) have neither
<zephyr/sys/__assert.h> nor k_panic() in posix/include. Code that
already uses the Zephyr names has to be hidden from these builds, such
as the coherent API checks.

Add <zephyr/sys/__assert.h> with __ASSERT() and __ASSERT_NO_MSG(). As
in Zephyr, the __ASSERT() message is optional and its format does not
have to be a string literal. The host checks its assertions in every
build, as assert() does: a failed assertion prints the condition and
any message, then panics with SOF_IPC_PANIC_ASSERT.

Add k_panic() to rtos/panic.h, which already defines sof_panic(). It
takes no panic code, so it reports SOF_IPC_PANIC_ASSERT, the code of a
failed assert(): Zephyr also ends a failed assertion with k_panic().

Zephyr builds, native_sim included, keep using the Zephyr macros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
SOF has two kinds of host build. The Zephyr ones, such as the native_sim
ztests and the IPC fuzzer, use Zephyr itself and never see the stubs in
posix/include. The non-Zephyr host library and testbench compile the
shared code against posix/include, and nothing tested its stubs of the
Zephyr APIs.

Add a test that compiles one source file the way the non-Zephyr host
builds compile the shared code: with the host include paths only, so
that <zephyr/...> resolves to the stubs. The ztest code, built normally
against Zephyr, calls it and compares the results with the Zephyr
implementations. ztest and native_sim are only the harness here; the
Zephyr builds do not depend on the stubs.

The test checks that:
- atomic_set(), atomic_add() and atomic_sub() return the previous value,
  and the values match Zephyr's;
- the cache functions return -ENOTSUP, as on native_sim;
- __ASSERT() and __ASSERT_NO_MSG() pass on a true condition and panic
  with SOF_IPC_PANIC_ASSERT on a false one, with a literal message, a
  format that is not a literal and no message, and evaluate the
  condition once;
- k_panic() panics with SOF_IPC_PANIC_ASSERT.

It catches panics by replacing the host __panic() with a longjmp() back
to the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
atomic_read() and atomic_init() are SOF names for the Zephyr
atomic_get() and atomic_set(). Now that the host builds have a
<zephyr/sys/atomic.h> stub with the Zephyr names and semantics, use the
Zephyr names in all code and remove the SOF ones from both versions of
rtos/atomic.h.

atomic_set() returns the previous value where atomic_init() returned
nothing; no caller uses it. The Zephyr atomic_read() cast the value to
long for the printf formats, but atomic_get() returns long on Zephyr
already, so the formats are unaffected.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Both versions of rtos/atomic.h only include <zephyr/sys/atomic.h> now:
Zephyr's header on Zephyr, the posix stub on the host. Include it
directly and remove rtos/atomic.h. The host arch atomics are now only
included from the stub.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the migration to native Zephyr atomic APIs.

No open issues 🎉

What changed in this PR

Migrates SOF’s atomic usage to native Zephyr APIs as part of #11517, including the host compatibility groundwork from #11583.

Changes:

  • Replaces legacy atomic names and includes, removing both rtos/atomic.h wrappers.
  • Adds host atomic, cache, assertion and panic compatibility support.
  • Adds native_sim tests for host stub behavior and Zephyr-compatible atomic return values.
File Description
zephyr/​include/​sof/​lib/​dma.h Uses Zephyr’s atomic header.
zephyr/​include/​rtos/​atomic.h Removes the Zephyr wrapper.
test/​ztest/​unit/​host_stubs/​testcase.yaml Registers native_sim tests.
test/​ztest/​unit/​host_stubs/​test_host_stubs_ztest.c Tests stub behavior and compatibility.
test/​ztest/​unit/​host_stubs/​prj.conf Configures the test application.
test/​ztest/​unit/​host_stubs/​host_stubs.h Defines the host-test interface.
test/​ztest/​unit/​host_stubs/​host_stubs.c Exercises stubs with host headers.
test/​ztest/​unit/​host_stubs/​CMakeLists.txt Separates host and Zephyr compilation.
src/​schedule/​zephyr_ll.c Uses atomic_get() for task counts.
src/​lib/​dma.c Uses Zephyr’s atomic header.
src/​kernel/​schedule/​zephyr_domain-kernel.c Migrates atomic task-count reads.
src/​kernel/​platform/​amd/​acp_7_x/​lib/​clk.c Uses Zephyr’s atomic header.
src/​kernel/​platform/​amd/​acp_7_0/​lib/​clk.c Uses Zephyr’s atomic header.
src/​kernel/​lib/​dma-kernel.c Migrates atomic includes and reads.
src/​kernel/​lib/​agent.c Initializes counters with atomic_set().
src/​kernel/​ipc/​ipc4/​handler-user-kernel.c Uses Zephyr’s atomic header.
src/​kernel/​ipc/​ipc4/​handler-ll-user.c Uses Zephyr’s atomic header.
src/​kernel/​ipc/​ipc4/​handler-ll-kernel.c Uses Zephyr’s atomic header.
src/​kernel/​ipc/​ipc4/​handler-kernel.c Migrates delayed-reply atomic reads.
src/​kernel/​drivers/​generic/​dummy-dma.c Migrates atomic includes and initialization.
src/​kernel/​drivers/​amd/​rembrandt/​acp_sp_dma.c Uses Zephyr’s atomic header.
src/​kernel/​drivers/​amd/​rembrandt/​acp_hs_dma.c Migrates atomic includes and initialization.
src/​kernel/​drivers/​amd/​rembrandt/​acp_dmic_dma.c Uses Zephyr’s atomic header.
src/​kernel/​drivers/​amd/​rembrandt/​acp_bt_dma.c Migrates atomic includes and initialization.
src/​kernel/​drivers/​amd/​common/​acp_sp_dma.c Migrates atomic includes and initialization.
src/​kernel/​drivers/​amd/​common/​acp_dmic_dma.c Migrates atomic includes and initialization.
src/​kernel/​arch/​host/​include/​arch/​atomic.h Aligns atomic return values with Zephyr.
src/​ipc/​ipc4/​handler-user.c Uses Zephyr’s atomic header.
src/​include/​sof/​schedule/​ll_schedule_domain.h Migrates domain counter initialization.
src/​include/​sof/​lib/​agent.h Migrates panic-counter reads.
src/​audio/​module_adapter/​module/​dolby/​dax.h Uses Zephyr’s atomic header.
src/​audio/​module_adapter/​module/​dolby/​dax.c Migrates host processing-flag reads.
posix/​include/​zephyr/​sys/​atomic.h Adds host atomic API stubs.
posix/​include/​zephyr/​sys/​__assert.h Adds host assertion macros.
posix/​include/​zephyr/​cache.h Adds unsupported-cache operation stubs.
posix/​include/​sof/​lib/​dma.h Uses the host Zephyr atomic header.
posix/​include/​sof/​lib/​agent.h Migrates host panic-counter reads.
posix/​include/​rtos/​panic.h Adds host k_panic() support.
posix/​include/​rtos/​atomic.h Removes the host atomic wrapper.

Review effort: Balanced

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