|
| 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