Skip to content

Commit cedef9b

Browse files
committed
zephyr: test: userspace: add test_mailbox for sof/mailbox.h
Add a user-space test for sof/mailbox.h interface usage. The test covers current usage of mailbox by SOF audio pipeline code. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 7549fc2 commit cedef9b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

zephyr/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ if (CONFIG_SOF_BOOT_TEST_STANDALONE AND CONFIG_SOF_USERSPACE_INTERFACE_DMA)
1515
zephyr_library_sources(userspace/test_intel_ssp_dai.c)
1616
endif()
1717
endif()
18+
19+
if (CONFIG_SOF_BOOT_TEST_STANDALONE AND CONFIG_USERSPACE)
20+
zephyr_library_sources(userspace/test_mailbox.c)
21+
endif()

zephyr/test/userspace/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Available tests:
1212
- Test Zephyr DAI interface, together with SOF DMA
1313
wrapper from a user thread. Mimics the call flows done in
1414
sof/src/audio/dai-zephyr.c. Use cavstool.py as host runner.
15+
- test_mailbox.c
16+
- Test use of sof/mailbox.h interface from a Zephyr user thread.
1517

1618
Building for Intel Panther Lake:
1719
./scripts/xtensa-build-zephyr.py --cmake-args=-DCONFIG_SOF_BOOT_TEST_STANDALONE=y \
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/*
3+
* Copyright(c) 2026 Intel Corporation.
4+
*/
5+
6+
/*
7+
* Test case for sof/mailbox.h interface use from a Zephyr user
8+
* thread.
9+
*/
10+
11+
#include <sof/boot_test.h>
12+
#include <sof/lib/mailbox.h>
13+
#include <rtos/userspace_helper.h>
14+
15+
#include <zephyr/kernel.h>
16+
#include <zephyr/ztest.h>
17+
#include <zephyr/logging/log.h>
18+
#include <zephyr/app_memory/app_memdomain.h>
19+
20+
#include <ipc4/fw_reg.h> /* mailbox definitions */
21+
22+
LOG_MODULE_DECLARE(sof_boot_test, LOG_LEVEL_DBG);
23+
24+
#define USER_STACKSIZE 2048
25+
26+
static struct k_thread user_thread;
27+
static K_THREAD_STACK_DEFINE(user_stack, USER_STACKSIZE);
28+
29+
static void mailbox_write_to_pipeline_regs(void)
30+
{
31+
unsigned int offset = offsetof(struct ipc4_fw_registers, pipeline_regs);
32+
struct ipc4_pipeline_registers pipe_reg;
33+
34+
/*
35+
* IPC4 pipe_reg struct used for test, but this test also
36+
* works for IPC3 targets.
37+
*/
38+
pipe_reg.stream_start_offset = (uint64_t)-1;
39+
pipe_reg.stream_end_offset = (uint64_t)-1;
40+
41+
LOG_INF("Write to sw_regs mailbox at offset %u", offset);
42+
43+
mailbox_sw_regs_write(offset, &pipe_reg, sizeof(pipe_reg));
44+
}
45+
46+
static void mailbox_test_thread(void *p1, void *p2, void *p3)
47+
{
48+
zassert_true(k_is_user_context(), "isn't user");
49+
50+
LOG_INF("SOF thread %s (%s)",
51+
k_is_user_context() ? "UserSpace!" : "privileged mode.",
52+
CONFIG_BOARD_TARGET);
53+
54+
mailbox_write_to_pipeline_regs();
55+
}
56+
57+
static void mailbox_test(void)
58+
{
59+
struct k_mem_domain domain;
60+
int ret = k_mem_domain_init(&domain, 0, NULL);
61+
62+
zassert_equal(ret, 0);
63+
64+
k_thread_create(&user_thread, user_stack, USER_STACKSIZE,
65+
mailbox_test_thread, NULL, NULL, NULL,
66+
-1, K_USER, K_FOREVER);
67+
68+
LOG_INF("set up user access to mailbox");
69+
70+
ret = user_access_to_mailbox(&domain, &user_thread);
71+
zassert_equal(ret, 0);
72+
73+
k_thread_start(&user_thread);
74+
75+
LOG_INF("user started, waiting in kernel until test complete");
76+
77+
k_thread_join(&user_thread, K_FOREVER);
78+
}
79+
80+
ZTEST(userspace_mailbox, mailbox_test)
81+
{
82+
/* first test from kernel */
83+
mailbox_write_to_pipeline_regs();
84+
85+
/* then full test in userspace */
86+
mailbox_test();
87+
88+
ztest_test_pass();
89+
}
90+
91+
ZTEST_SUITE(userspace_mailbox, NULL, NULL, NULL, NULL, NULL);
92+
93+
/**
94+
* SOF main has booted up and IPC handling is stopped.
95+
* Run test suites with ztest_run_all.
96+
*/
97+
static int run_tests(void)
98+
{
99+
ztest_run_test_suite(userspace_mailbox, false, 1, 1, NULL);
100+
return 0;
101+
}
102+
103+
SYS_INIT(run_tests, APPLICATION, 99);

0 commit comments

Comments
 (0)