|
| 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 = |
| 32 | + offsetof(struct ipc4_fw_registers, pipeline_regs); |
| 33 | + struct ipc4_pipeline_registers pipe_reg; |
| 34 | + |
| 35 | + pipe_reg.stream_start_offset = (uint64_t)-1; |
| 36 | + pipe_reg.stream_end_offset = (uint64_t)-1; |
| 37 | + |
| 38 | + LOG_INF("Write to IPC4 pipeline regs at offset %u", offset); |
| 39 | + |
| 40 | + mailbox_sw_regs_write(offset, &pipe_reg, sizeof(pipe_reg)); |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | + * the "stream" mailbox not available on targets using IPC4 |
| 45 | + * layout for shared memory between host and DSP |
| 46 | + */ |
| 47 | +#ifdef CONFIG_IPC_MAJOR_4 |
| 48 | +static void mailbox_write_to_stream_posn(void) |
| 49 | +{ |
| 50 | +} |
| 51 | +#else |
| 52 | +static void mailbox_write_to_stream_posn(void) |
| 53 | +{ |
| 54 | + struct sof_ipc_stream_posn posn; |
| 55 | + size_t offset = 0; /* first stream position slot */ |
| 56 | + |
| 57 | + LOG_INF("Write to IPC4 stream#0 position info offset %u size %u.", |
| 58 | + offset, sizeof(posn)); |
| 59 | + |
| 60 | + mailbox_stream_write(offset, &posn, sizeof(posn)); |
| 61 | +} |
| 62 | +#endif |
| 63 | + |
| 64 | +static void mailbox_test_thread(void *p1, void *p2, void *p3) |
| 65 | +{ |
| 66 | + zassert_true(k_is_user_context(), "isn't user"); |
| 67 | + |
| 68 | + LOG_INF("SOF thread %s (%s)", |
| 69 | + k_is_user_context() ? "UserSpace!" : "privileged mode.", |
| 70 | + CONFIG_BOARD_TARGET); |
| 71 | + |
| 72 | + mailbox_write_to_pipeline_regs(); |
| 73 | + mailbox_write_to_stream_posn(); |
| 74 | +} |
| 75 | + |
| 76 | +static void mailbox_test(void) |
| 77 | +{ |
| 78 | + struct k_mem_domain domain; |
| 79 | + int ret = k_mem_domain_init(&domain, 0, NULL); |
| 80 | + |
| 81 | + zassert_equal(ret, 0); |
| 82 | + |
| 83 | + k_thread_create(&user_thread, user_stack, USER_STACKSIZE, |
| 84 | + mailbox_test_thread, NULL, NULL, NULL, |
| 85 | + -1, K_USER, K_FOREVER); |
| 86 | + |
| 87 | + LOG_INF("set up user access to mailbox"); |
| 88 | + |
| 89 | + ret = user_access_to_mailbox(&domain, &user_thread); |
| 90 | + zassert_equal(ret, 0); |
| 91 | + |
| 92 | + k_thread_start(&user_thread); |
| 93 | + |
| 94 | + LOG_INF("user started, waiting in kernel until test complete"); |
| 95 | + |
| 96 | + k_thread_join(&user_thread, K_FOREVER); |
| 97 | +} |
| 98 | + |
| 99 | +ZTEST(userspace_mailbox, mailbox_test) |
| 100 | +{ |
| 101 | + /* first test from kernel */ |
| 102 | + mailbox_write_to_pipeline_regs(); |
| 103 | + mailbox_write_to_stream_posn(); |
| 104 | + |
| 105 | + /* then full test in userspace */ |
| 106 | + mailbox_test(); |
| 107 | + |
| 108 | + ztest_test_pass(); |
| 109 | +} |
| 110 | + |
| 111 | +ZTEST_SUITE(userspace_mailbox, NULL, NULL, NULL, NULL, NULL); |
| 112 | + |
| 113 | +/** |
| 114 | + * SOF main has booted up and IPC handling is stopped. |
| 115 | + * Run test suites with ztest_run_all. |
| 116 | + */ |
| 117 | +static int run_tests(void) |
| 118 | +{ |
| 119 | + ztest_run_test_suite(userspace_mailbox, false, 1, 1, NULL); |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +SYS_INIT(run_tests, APPLICATION, 99); |
0 commit comments