|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* |
| 3 | + * Copyright(c) 2026 Intel Corporation. |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * Test case for creation of low-latency threads in user-space. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <sof/boot_test.h> |
| 11 | +#include <sof/lib/mailbox.h> |
| 12 | +#include <sof/lib/uuid.h> |
| 13 | +#include <sof/schedule/schedule.h> |
| 14 | +#include <sof/schedule/ll_schedule.h> |
| 15 | +#include <rtos/task.h> |
| 16 | +#include <rtos/userspace_helper.h> |
| 17 | +#include <ipc4/fw_reg.h> |
| 18 | + |
| 19 | +#include <zephyr/kernel.h> |
| 20 | +#include <zephyr/ztest.h> |
| 21 | +#include <zephyr/logging/log.h> |
| 22 | +#include <zephyr/app_memory/app_memdomain.h> |
| 23 | + |
| 24 | +#include <stddef.h> /* offsetof() */ |
| 25 | + |
| 26 | +LOG_MODULE_DECLARE(sof_boot_test, LOG_LEVEL_DBG); |
| 27 | + |
| 28 | +/* f11818eb-e92e-4082-82a3-dc54c604ebf3 */ |
| 29 | +SOF_DEFINE_UUID("test_task", test_task_uuid, 0xf11818eb, 0xe92e, 0x4082, |
| 30 | + 0x82, 0xa3, 0xdc, 0x54, 0xc6, 0x04, 0xeb, 0xf3); |
| 31 | + |
| 32 | +K_APPMEM_PARTITION_DEFINE(userspace_ll_part); |
| 33 | + |
| 34 | +/* Global variable for test runs counter, accessible from user-space */ |
| 35 | +K_APP_BMEM(userspace_ll_part) static int test_runs; |
| 36 | + |
| 37 | +static enum task_state task_callback(void *arg) |
| 38 | +{ |
| 39 | + LOG_INF("entry"); |
| 40 | + |
| 41 | + if (++test_runs > 3) |
| 42 | + return SOF_TASK_STATE_COMPLETED; |
| 43 | + |
| 44 | + return SOF_TASK_STATE_RESCHEDULE; |
| 45 | +} |
| 46 | + |
| 47 | +static void ll_task_test(void) |
| 48 | +{ |
| 49 | + struct task *task; |
| 50 | + int priority = 0; |
| 51 | + int core = 0; |
| 52 | + int ret; |
| 53 | + |
| 54 | + /* Initialize global test runs counter */ |
| 55 | + test_runs = 0; |
| 56 | + |
| 57 | + task = zephyr_ll_task_alloc(); |
| 58 | + |
| 59 | + /* allow user space to report status via 'test_runs' */ |
| 60 | + k_mem_domain_add_partition(zephyr_ll_mem_domain(), &userspace_ll_part); |
| 61 | + |
| 62 | + /* work in progress, see pipeline-schedule.c */ |
| 63 | + ret = schedule_task_init_ll(task, SOF_UUID(test_task_uuid), SOF_SCHEDULE_LL_TIMER, |
| 64 | + priority, task_callback, |
| 65 | + (void *)&test_runs, core, 0); |
| 66 | + zassert_equal(ret, 0); |
| 67 | + |
| 68 | + LOG_INF("task init done"); |
| 69 | + |
| 70 | + /* Schedule the task to run immediately with 1ms period */ |
| 71 | + ret = schedule_task(task, 0, 1000); /* 0 = start now, 1000us = 1ms period */ |
| 72 | + zassert_equal(ret, 0); |
| 73 | + |
| 74 | + LOG_INF("task scheduled and running"); |
| 75 | + |
| 76 | + /* Let the task run for a bit */ |
| 77 | + k_sleep(K_MSEC(10)); |
| 78 | + |
| 79 | + /* Cancel the task to stop any scheduled execution */ |
| 80 | + ret = schedule_task_cancel(task); |
| 81 | + zassert_equal(ret, 0); |
| 82 | + |
| 83 | + /* Free task resources */ |
| 84 | + ret = schedule_task_free(task); |
| 85 | + zassert_equal(ret, 0); |
| 86 | + |
| 87 | + LOG_INF("test complete"); |
| 88 | +} |
| 89 | + |
| 90 | +ZTEST(userspace_ll, ll_task_test) |
| 91 | +{ |
| 92 | + ll_task_test(); |
| 93 | + ztest_test_pass(); |
| 94 | +} |
| 95 | + |
| 96 | +ZTEST_SUITE(userspace_ll, NULL, NULL, NULL, NULL, NULL); |
| 97 | + |
| 98 | +/** |
| 99 | + * SOF main has booted up and IPC handling is stopped. |
| 100 | + * Run test suites with ztest_run_all. |
| 101 | + */ |
| 102 | +static int run_tests(void) |
| 103 | +{ |
| 104 | + ztest_run_test_suite(userspace_ll, false, 1, 1, NULL); |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +SYS_INIT(run_tests, APPLICATION, 99); |
0 commit comments