diff --git a/src/audio/dai-legacy.c b/src/audio/dai-legacy.c index 73f8704de36c..2332346889f1 100644 --- a/src/audio/dai-legacy.c +++ b/src/audio/dai-legacy.c @@ -44,14 +44,20 @@ SOF_DEFINE_REG_UUID(dai); static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev, int cmd); -static void dai_atomic_trigger(void *arg, enum notify_id type, void *data) +/* Trigger all DAIs of a group, called by the last DAI to receive the command */ +static int dai_group_trigger(struct dai_group *group, int cmd) { - struct comp_dev *dev = arg; - struct dai_data *dd = comp_get_drvdata(dev); - struct dai_group *group = dd->group; + struct list_item *item; + int ret = 0; + + /* return the error of the last trigger */ + list_for_item(item, &group->dais) { + struct dai_data *dd = container_of(item, struct dai_data, group_item); - /* Atomic context set by the last DAI to receive trigger command */ - group->trigger_ret = dai_comp_trigger_internal(dd, dev, group->trigger_cmd); + ret = dai_comp_trigger_internal(dd, dd->dai_dev, cmd); + } + + return ret; } /* Assign DAI to a group */ @@ -78,8 +84,9 @@ int dai_assign_group(struct dai_data *dd, struct comp_dev *dev, uint32_t group_i comp_dbg(dev, "group %d num %d", group_id, dd->group->num_dais); - /* Register for the atomic trigger event */ - notifier_register(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER, dai_atomic_trigger); + /* The group trigger walks the DAIs by their data */ + dd->dai_dev = dev; + list_item_prepend(&dd->group_item, &dd->group->dais); return 0; } @@ -215,8 +222,10 @@ static struct comp_dev *dai_new(const struct comp_driver *drv, void dai_common_free(struct dai_data *dd) { - if (dd->group) + if (dd->group) { + list_item_del(&dd->group_item); dai_group_put(dd->group); + } if (dd->chan) { dd->chan->dev_data = NULL; @@ -237,9 +246,6 @@ static void dai_free(struct comp_dev *dev) { struct dai_data *dd = comp_get_drvdata(dev); - if (dd->group) - notifier_unregister(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER); - if (dd->chan) notifier_unregister(dev, dd->chan, NOTIFIER_ID_DMA_COPY); @@ -851,37 +857,33 @@ int dai_common_trigger(struct dai_data *dd, struct comp_dev *dev, int cmd) comp_dbg(dev, "begin atomic trigger for group %d", group->group_id); group->trigger_cmd = cmd; - group->trigger_counter = group->num_dais - 1; + group->trigger_counter = group->num_dais; } else if (group->trigger_cmd != cmd) { /* Already processing a different trigger command */ comp_err(dev, "already processing atomic trigger"); - ret = -EAGAIN; - } else { - /* Count down the number of remaining DAIs required - * to receive the trigger command before atomic trigger - * takes place - */ - group->trigger_counter--; - comp_dbg(dev, "trigger counter %d, group %d", - group->trigger_counter, group->group_id); - - if (!group->trigger_counter) { - /* The counter has reached 0, which means - * all DAIs have received the same trigger command - * and we may begin the actual trigger process - * synchronously. - */ - - irq_local_disable(irq_flags); - notifier_event(group, NOTIFIER_ID_DAI_TRIGGER, - BIT(cpu_get_id()), NULL, 0); - irq_local_enable(irq_flags); - - /* return error of last trigger */ - ret = group->trigger_ret; - } + return -EAGAIN; } + /* Count down the number of remaining DAIs required to receive the + * trigger command before the atomic trigger takes place. The first + * DAI counts too, so a group of one DAI triggers at once. + */ + group->trigger_counter--; + comp_dbg(dev, "trigger counter %d, group %d", + group->trigger_counter, group->group_id); + + if (group->trigger_counter) + return 0; + + /* The counter has reached 0, which means all DAIs have received the + * same trigger command and we may begin the actual trigger process + * synchronously. + */ + + irq_local_disable(irq_flags); + ret = dai_group_trigger(group, cmd); + irq_local_enable(irq_flags); + return ret; } diff --git a/src/audio/dai-zephyr.c b/src/audio/dai-zephyr.c index 2cbdfe998a2c..dc7c5542a81a 100644 --- a/src/audio/dai-zephyr.c +++ b/src/audio/dai-zephyr.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -73,14 +72,20 @@ SOF_DEFINE_REG_UUID(dai); static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev, int cmd); -static void dai_atomic_trigger(void *arg, enum notify_id type, void *data) +/* Trigger all DAIs of a group, called by the last DAI to receive the command */ +static int dai_group_trigger(struct dai_group *group, int cmd) { - struct comp_dev *dev = arg; - struct dai_data *dd = comp_get_drvdata(dev); - struct dai_group *group = dd->group; + struct list_item *item; + int ret = 0; + + /* return the error of the last trigger */ + list_for_item(item, &group->dais) { + struct dai_data *dd = container_of(item, struct dai_data, group_item); - /* Atomic context set by the last DAI to receive trigger command */ - group->trigger_ret = dai_comp_trigger_internal(dd, dev, group->trigger_cmd); + ret = dai_comp_trigger_internal(dd, dd->dai_dev, cmd); + } + + return ret; } /* Assign DAI to a group */ @@ -109,8 +114,9 @@ __cold int dai_assign_group(struct dai_data *dd, struct comp_dev *dev, uint32_t comp_dbg(dev, "group %d num %d", group_id, dd->group->num_dais); - /* Register for the atomic trigger event */ - notifier_register(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER, dai_atomic_trigger); + /* The group trigger walks the DAIs by their data */ + dd->dai_dev = dev; + list_item_prepend(&dd->group_item, &dd->group->dais); return 0; } @@ -799,8 +805,10 @@ __cold void dai_common_free(struct dai_data *dd) #endif telemetry_unregister_dai(dd); - if (dd->group) + if (dd->group) { + list_item_del(&dd->group_item); dai_group_put(dd->group); + } dai_dma_release_channel(dd); sof_dma_put(dd->dma); @@ -822,9 +830,6 @@ __cold static void dai_free(struct comp_dev *dev) assert_can_be_cold(); - if (dd->group) - notifier_unregister(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER); - dai_common_free(dd); /* heap is NULL to match what is passed in dai_new() */ @@ -1636,7 +1641,9 @@ static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev, int dai_common_trigger(struct dai_data *dd, struct comp_dev *dev, int cmd) { struct dai_group *group = dd->group; +#ifndef CONFIG_SOF_USERSPACE_LL uint32_t irq_flags; +#endif int ret = 0; /* DAI not in a group, use normal trigger */ @@ -1654,36 +1661,43 @@ int dai_common_trigger(struct dai_data *dd, struct comp_dev *dev, int cmd) comp_dbg(dev, "begin atomic trigger for group %d", group->group_id); group->trigger_cmd = cmd; - group->trigger_counter = group->num_dais - 1; + group->trigger_counter = group->num_dais; } else if (group->trigger_cmd != cmd) { /* Already processing a different trigger command */ comp_err(dev, "already processing atomic trigger"); - ret = -EAGAIN; - } else { - /* Count down the number of remaining DAIs required - * to receive the trigger command before atomic trigger - * takes place - */ - group->trigger_counter--; - comp_dbg(dev, "trigger counter %d, group %d", - group->trigger_counter, group->group_id); - - if (!group->trigger_counter) { - /* The counter has reached 0, which means - * all DAIs have received the same trigger command - * and we may begin the actual trigger process - * synchronously. - */ + return -EAGAIN; + } - irq_local_disable(irq_flags); - notifier_event(group, NOTIFIER_ID_DAI_TRIGGER, - NOTIFIER_TARGET_CORE_LOCAL, NULL, 0); - irq_local_enable(irq_flags); + /* Count down the number of remaining DAIs required to receive the + * trigger command before the atomic trigger takes place. The first + * DAI counts too, so a group of one DAI triggers at once. + */ + group->trigger_counter--; + comp_dbg(dev, "trigger counter %d, group %d", + group->trigger_counter, group->group_id); - /* return error of last trigger */ - ret = group->trigger_ret; - } - } + if (group->trigger_counter) + return 0; + + /* The counter has reached 0, which means all DAIs have received the + * same trigger command and we may begin the actual trigger process + * synchronously. + */ + + /* In user-space builds irq_local_disable() is privileged, use the LL + * scheduler lock instead (re-entrant, so safe if the caller holds it). + */ +#ifdef CONFIG_SOF_USERSPACE_LL + user_ll_lock_sched(dev->ipc_config.core); +#else + irq_local_disable(irq_flags); +#endif + ret = dai_group_trigger(group, cmd); +#ifdef CONFIG_SOF_USERSPACE_LL + user_ll_unlock_sched(dev->ipc_config.core); +#else + irq_local_enable(irq_flags); +#endif return ret; } diff --git a/src/include/sof/lib/dai-legacy.h b/src/include/sof/lib/dai-legacy.h index 4e050e6b87c8..5c28b198dc37 100644 --- a/src/include/sof/lib/dai-legacy.h +++ b/src/include/sof/lib/dai-legacy.h @@ -182,6 +182,7 @@ struct dai_data { struct dai *dai; struct dma *dma; struct dai_group *group; /* NULL if no group assigned */ + struct list_item group_item; /* in group->dais */ int xrun; /* true if we are doing xrun recovery */ pcm_converter_func process; /* processing function */ @@ -363,9 +364,9 @@ struct dai_group { int trigger_cmd; /** - * Last trigger error + * DAIs in this group, linked by struct dai_data::group_item */ - int trigger_ret; + struct list_item dais; /** * Group list diff --git a/src/include/sof/lib/dai-zephyr.h b/src/include/sof/lib/dai-zephyr.h index 69a7d965483e..91da81147ef7 100644 --- a/src/include/sof/lib/dai-zephyr.h +++ b/src/include/sof/lib/dai-zephyr.h @@ -92,9 +92,9 @@ struct dai_group { int trigger_cmd; /** - * Last trigger error + * DAIs in this group, linked by struct dai_data::group_item */ - int trigger_ret; + struct list_item dais; /** * Group list @@ -152,6 +152,7 @@ struct dai_data { struct dai *dai; struct sof_dma *dma; struct dai_group *group; /* NULL if no group assigned */ + struct list_item group_item; /* in group->dais */ int xrun; /* true if we are doing xrun recovery */ pcm_converter_func process; /* processing function */ diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index e5413a512cb4..07fdffb15aa4 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -26,7 +26,6 @@ enum notify_id { NOTIFIER_ID_DMA_DOMAIN_CHANGE, /* struct dma_chan_data * */ NOTIFIER_ID_DMA_COPY, /* struct dma_cb_data* */ NOTIFIER_ID_DMA_IRQ, /* struct dma_chan_data * */ - NOTIFIER_ID_DAI_TRIGGER, /* struct dai_group * */ NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE, /* struct mic_privacy_settings * */ NOTIFIER_ID_VAD, /* int * (WEBRTC_VAD_SPEECH / WEBRTC_VAD_SILENCE) */ NOTIFIER_ID_COUNT diff --git a/src/lib/dai.c b/src/lib/dai.c index 5b196c75eb8f..4b6a69a021da 100644 --- a/src/lib/dai.c +++ b/src/lib/dai.c @@ -106,6 +106,7 @@ __cold static struct dai_group *dai_group_alloc(void) return NULL; } + list_init(&group->dais); list_item_prepend(&group->list, dai_groups); return group; @@ -154,7 +155,13 @@ __cold void dai_group_put(struct dai_group *group) group->num_dais--; - /* Mark as unused if there are no more DAIs in this group */ - if (!group->num_dais) + /* Mark as unused if there are no more DAIs in this group, and drop + * any trigger that was pending, so that the next group ID that reuses + * it starts from a clean state. + */ + if (!group->num_dais) { group->group_id = 0; + group->trigger_counter = 0; + group->trigger_cmd = 0; + } } diff --git a/test/ztest/unit/audio/dai_group/CMakeLists.txt b/test/ztest/unit/audio/dai_group/CMakeLists.txt new file mode 100644 index 000000000000..b1cf7222949c --- /dev/null +++ b/test/ztest/unit/audio/dai_group/CMakeLists.txt @@ -0,0 +1,78 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DAI group trigger unit tests + +cmake_minimum_required(VERSION 3.20.0) + +# dai-zephyr.h declares dai_get_device() as a system call, so its generated +# header is needed. The SOF module adds it only to full applications, and it +# must be scanned before find_package(Zephyr). +list(APPEND SYSCALL_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/include/sof/lib) +# The zephyr,posix-dai binding of the virtual DAIs in app.overlay +list(APPEND DTS_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../app) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_dai_group) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../..") +set(sof_top_dir ${SOF_ROOT}) + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) +include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake) + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + ${SOF_ROOT}/src/kernel/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + ${PROJECT_BINARY_DIR}/include/generated +) + +# No CONFIG_LIBRARY: with it, dai_copier.h defines inline versions of +# functions of dai-zephyr.c. +# pcm_converter.h does not pick an implementation under UNIT_TEST, select the +# generic C one. +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DUNIT_TEST=1 + -DPCM_CONVERTER_GENERIC +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + dai_group_ztest.c + dai_group_stubs.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/dai-zephyr.c + ${SOF_ROOT}/src/lib/dai.c + # the native_sim virtual DAI and POSIX DMA models + ${SOF_ROOT}/src/kernel/platform/posix/dai_virtual.c + ${SOF_ROOT}/src/kernel/platform/posix/dma.c + # the DAI component driver registration, and its other operations, which + # the tests do not call + ${SOF_ROOT}/src/audio/component.c + ${SOF_ROOT}/src/lib/dma.c + ${SOF_ROOT}/src/audio/pcm_converter/pcm_converter.c + ${SOF_ROOT}/src/audio/pcm_converter/pcm_converter_generic.c + ${SOF_ROOT}/src/audio/buffers/comp_buffer.c + ${SOF_ROOT}/src/audio/buffers/audio_buffer.c + ${SOF_ROOT}/src/audio/source_api_helper.c + ${SOF_ROOT}/src/audio/sink_api_helper.c + ${SOF_ROOT}/src/audio/sink_source_utils.c + ${SOF_ROOT}/src/audio/audio_stream.c + ${SOF_ROOT}/src/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/math/numbers.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/dai_group/app.overlay b/test/ztest/unit/audio/dai_group/app.overlay new file mode 100644 index 000000000000..cf20c089c81e --- /dev/null +++ b/test/ztest/unit/audio/dai_group/app.overlay @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. + * + * Two virtual DAIs of the native_sim build (zephyr,posix-dai, handled by + * src/kernel/platform/posix/dai_virtual.c) for the DAI group test on the + * native_sim DAI and DMA models. + */ + +/ { + sim_dai0: sim-dai0 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <8>; + }; + + sim_dai1: sim-dai1 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <9>; + }; +}; diff --git a/test/ztest/unit/audio/dai_group/dai_group_stubs.c b/test/ztest/unit/audio/dai_group/dai_group_stubs.c new file mode 100644 index 000000000000..515a051ff8ef --- /dev/null +++ b/test/ztest/unit/audio/dai_group/dai_group_stubs.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Platform and IPC functions used by src/audio/dai-zephyr.c, which the DAI +// group tests do not link. The DAI component driver is registered at boot, so +// all its operations are linked, but the tests only create, trigger and free +// DAIs: the functions of the other operations must never be called. + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/* called on START and RELEASE, the tests do not check the wall clock */ +void platform_dai_wallclock(struct comp_dev *dai, uint64_t *wallclock) +{ + *wallclock = 0; +} + +/* called by dai_common_free(), the tests do not use LLP slots */ +void dai_release_llp_slot(struct dai_data *dd) +{ +} + +/* not used by the tests */ + +int dai_config(struct dai_data *dd, struct comp_dev *dev, struct ipc_config_dai *common_cfg, + const void *spec_cfg) +{ + zassert_unreachable(); + return -ENOTSUP; +} + +int dai_config_dma_channel(struct dai_data *dd, struct comp_dev *dev, const void *config) +{ + zassert_unreachable(); + return -ENOTSUP; +} + +int ipc_dai_data_config(struct dai_data *dd, struct comp_dev *dev) +{ + zassert_unreachable(); + return -ENOTSUP; +} + +int dai_position(struct comp_dev *dev, struct sof_ipc_stream_posn *posn) +{ + zassert_unreachable(); + return -ENOTSUP; +} + +void dai_dma_position_update(struct dai_data *dd, struct comp_dev *dev) +{ + zassert_unreachable(); +} + +void dai_dma_release(struct dai_data *dd, struct comp_dev *dev) +{ + zassert_unreachable(); +} + +int copier_gain_input(struct comp_dev *dev, struct comp_buffer *buff, + struct copier_gain_params *gain_params, + enum copier_gain_envelope_dir dir, uint32_t stream_bytes) +{ + zassert_unreachable(); + return -ENOTSUP; +} + +void pipeline_xrun(struct pipeline *p, struct comp_dev *dev, int32_t bytes) +{ + zassert_unreachable(); +} + +bool send_copier_gateway_xrun_notif_msg(uint32_t pipeline_id, enum sof_ipc_stream_direction dir) +{ + zassert_unreachable(); + return false; +} diff --git a/test/ztest/unit/audio/dai_group/dai_group_ztest.c b/test/ztest/unit/audio/dai_group/dai_group_ztest.c new file mode 100644 index 000000000000..954f1145b808 --- /dev/null +++ b/test/ztest/unit/audio/dai_group/dai_group_ztest.c @@ -0,0 +1,886 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// DAI group trigger tests. +// +// The DAIs are real DAI component devices, created and freed by the DAI +// component driver of src/audio/dai-zephyr.c, and the groups are managed by +// the real code of dai-zephyr.c and src/lib/dai.c. The tests assign and +// trigger the DAIs with dai_assign_group() and dai_common_trigger(), which the +// DAI component and the IPC4 DAI copier share. The hardware is modelled by +// fake Zephyr DAI and DMA devices, defined below, which record every call in a +// log, so that the tests can check which DAIs a trigger reaches, in which +// order, and the order of the DMA and DAI operations of each DAI. A failure +// can be injected into any recorded operation of any DAI. + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* DAIs available to the tests, the DMA channel of a DAI is its index */ +#define TEST_NUM_DAIS 4 + +/* Hardware operations recorded by the fake devices */ +enum hw_op { + HW_NONE, + HW_DMA_CONFIG, + HW_DMA_START, + HW_DMA_STOP, + HW_DMA_SUSPEND, + HW_DMA_RELEASE, + HW_DAI_TRIGGER, +}; + +struct hw_call { + enum hw_op op; + int dai; /* DAI index */ + int cmd; /* enum dai_trigger_cmd for HW_DAI_TRIGGER, else 0 */ +}; + +#define HW_LOG_SIZE 64 + +static struct hw_call hw_log[HW_LOG_SIZE]; +static size_t hw_log_len; + +/* Failure injected into one operation of one DAI */ +static struct { + enum hw_op op; + int dai; + int ret; +} hw_fail; + +static int hw_record(enum hw_op op, int dai, int cmd) +{ + zassert_true(hw_log_len < HW_LOG_SIZE, "hardware call log overflow"); + hw_log[hw_log_len++] = (struct hw_call){ .op = op, .dai = dai, .cmd = cmd }; + + return hw_fail.op == op && hw_fail.dai == dai ? hw_fail.ret : 0; +} + +static void hw_inject_failure(enum hw_op op, int dai, int ret) +{ + hw_fail.op = op; + hw_fail.dai = dai; + hw_fail.ret = ret; +} + +static void hw_log_clear(void) +{ + hw_log_len = 0; +} + +/* + * Fake Zephyr DAI devices, one per DAI. Only the trigger is used by the code + * under test. + */ +struct fake_dai_config { + int index; +}; + +static int fake_dai_trigger(const struct device *dev, enum dai_dir dir, + enum dai_trigger_cmd cmd) +{ + const struct fake_dai_config *cfg = dev->config; + + /* all the test DAIs are playback DAIs, SOF_IPC_STREAM_PLAYBACK is DAI_DIR_TX */ + zassert_equal(dir, DAI_DIR_TX); + + return hw_record(HW_DAI_TRIGGER, cfg->index, cmd); +} + +static DEVICE_API(dai, fake_dai_api) = { + .trigger = fake_dai_trigger, +}; + +#define FAKE_DAI_DEFINE(n) \ + static const struct fake_dai_config fake_dai_config##n = { .index = n };\ + DEVICE_DEFINE(fake_dai##n, "fake_dai" #n, NULL, NULL, NULL, \ + &fake_dai_config##n, POST_KERNEL, 0, &fake_dai_api) + +FAKE_DAI_DEFINE(0); +FAKE_DAI_DEFINE(1); +FAKE_DAI_DEFINE(2); +FAKE_DAI_DEFINE(3); + +/* Fake Zephyr DMA device, shared by all DAIs, channel N belongs to DAI N */ +static int fake_dma_config(const struct device *dev, uint32_t channel, + struct dma_config *config) +{ + return hw_record(HW_DMA_CONFIG, channel, 0); +} + +static int fake_dma_start(const struct device *dev, uint32_t channel) +{ + return hw_record(HW_DMA_START, channel, 0); +} + +static int fake_dma_stop(const struct device *dev, uint32_t channel) +{ + return hw_record(HW_DMA_STOP, channel, 0); +} + +static int fake_dma_suspend(const struct device *dev, uint32_t channel) +{ + return hw_record(HW_DMA_SUSPEND, channel, 0); +} + +static void fake_dma_chan_release(const struct device *dev, uint32_t channel) +{ + hw_record(HW_DMA_RELEASE, channel, 0); +} + +static DEVICE_API(dma, fake_dma_api) = { + .config = fake_dma_config, + .start = fake_dma_start, + .stop = fake_dma_stop, + .suspend = fake_dma_suspend, + .chan_release = fake_dma_chan_release, +}; + +/* dma_release_channel() needs the generic DMA context */ +static ATOMIC_DEFINE(fake_dma_channels, TEST_NUM_DAIS); + +static struct dma_context fake_dma_context = { + .magic = DMA_MAGIC, + .dma_channels = TEST_NUM_DAIS, + .atomic = fake_dma_channels, +}; + +DEVICE_DEFINE(fake_dma, "fake_dma", NULL, NULL, &fake_dma_context, NULL, POST_KERNEL, 0, + &fake_dma_api); + +/* SOF DAI objects of the fake DAIs */ +static struct dai test_dais[TEST_NUM_DAIS] = { + /* + * HDA DMA: RELEASE does not reset the DMA buffer, which these tests do + * not allocate (and, as playback DAIs, they do not zero it either). + */ + { .index = 0, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dev = DEVICE_GET(fake_dai0) }, + { .index = 1, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dev = DEVICE_GET(fake_dai1) }, + { .index = 2, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dev = DEVICE_GET(fake_dai2) }, + { .index = 3, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dev = DEVICE_GET(fake_dai3) }, +}; + +/* SOF DMA object of the fake DMA */ +static struct sof_dma test_dma = { + .z_dev = DEVICE_GET(fake_dma), +}; + +/* + * DAI and DMA lookup, in place of src/kernel/lib/dai-kernel.c and + * src/kernel/lib/dma-kernel.c, called by dai_common_new() and + * dai_common_free() + */ +/* + * The native_sim models: two virtual DAIs (app.overlay, dai_virtual.c) on the + * POSIX DMA model (dma.c), whose channels assert their state transitions. + * They are DAI indices SIM_DAI_FIRST and up, on DMA device SIM_DMA_DEV. + */ +#define SIM_NUM_DAIS 2 +#define SIM_DAI_FIRST 8 +#define SIM_DMA_DEV 1 + +static struct dai sim_dais[SIM_NUM_DAIS] = { + { .index = SIM_DAI_FIRST, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dma_dev = SIM_DMA_DEV, .dev = DEVICE_DT_GET(DT_NODELABEL(sim_dai0)) }, + { .index = SIM_DAI_FIRST + 1, .type = SOF_DAI_INTEL_HDA, .dma_caps = SOF_DMA_CAP_HDA, + .dma_dev = SIM_DMA_DEV, .dev = DEVICE_DT_GET(DT_NODELABEL(sim_dai1)) }, +}; + +/* z_dev is looked up by name: the DMA model's devices are not in the devicetree */ +static struct sof_dma sim_dma; + +struct dai *dai_get(uint32_t type, uint32_t index, uint32_t flags) +{ + zassert_equal(type, SOF_DAI_INTEL_HDA); + if (index >= SIM_DAI_FIRST && index < SIM_DAI_FIRST + SIM_NUM_DAIS) + return &sim_dais[index - SIM_DAI_FIRST]; + zassert_true(index < TEST_NUM_DAIS, "no DAI %u", index); + + return &test_dais[index]; +} + +void dai_put(struct dai *dai) +{ +} + +struct sof_dma *z_impl_sof_dma_get(uint32_t dir, uint32_t cap, uint32_t dev, uint32_t flags) +{ + return dev == SIM_DMA_DEV ? &sim_dma : &test_dma; +} + +void z_impl_sof_dma_put(struct sof_dma *dma) +{ + zassert_true(dma == &test_dma || dma == &sim_dma); +} + +/* DAI component devices of the current test, freed by the after() hook */ +static struct comp_dev *test_devs[TEST_NUM_DAIS]; + +static const struct comp_driver *dai_driver_get(void) +{ + struct comp_driver_list *drivers = comp_drivers_get(); + struct list_item *item; + + list_for_item(item, &drivers->list) { + struct comp_driver_info *info = container_of(item, struct comp_driver_info, + list); + + if (info->drv->type == SOF_COMP_DAI) + return info->drv; + } + + return NULL; +} + +/* Create the DAI component of DAI @index, ready to be triggered */ +static void test_dai_new(int index) +{ + const struct comp_driver *drv = dai_driver_get(); + struct comp_ipc_config config = { + .id = index, + .type = SOF_COMP_DAI, + }; + struct ipc_config_dai dai_cfg = { + .direction = SOF_IPC_STREAM_PLAYBACK, + .dai_index = index, + .type = SOF_DAI_INTEL_HDA, + }; + struct comp_dev *dev; + struct dai_data *dd; + + zassert_not_null(drv, "DAI component driver not registered"); + zassert_is_null(test_devs[index], "DAI %d already created", index); + + dev = drv->ops.create(drv, &config, &dai_cfg); + zassert_not_null(dev, "cannot create DAI %d", index); + dev->direction = SOF_IPC_STREAM_PLAYBACK; + test_devs[index] = dev; + + dd = comp_get_drvdata(dev); + zassert_equal(dd->dai, &test_dais[index]); + zassert_equal(dd->dma, &test_dma); + zassert_is_null(dd->group); + + /* the DMA channel is normally assigned by dai_config_dma_channel() */ + dd->chan_index = index; +} + +/* Free the DAI component of DAI @index, as the IPC does */ +static void test_dai_free(int index) +{ + struct comp_dev *dev = test_devs[index]; + + test_devs[index] = NULL; + comp_free(dev); +} + +static struct dai_data *dd_of(int index) +{ + return comp_get_drvdata(test_devs[index]); +} + +static int test_assign(int index, uint32_t group_id) +{ + return dai_assign_group(dd_of(index), test_devs[index], group_id); +} + +static int test_trigger(int index, int cmd) +{ + return dai_common_trigger(dd_of(index), test_devs[index], cmd); +} + +static const char *hw_op_name(enum hw_op op) +{ + static const char * const names[] = { + [HW_NONE] = "none", + [HW_DMA_CONFIG] = "dma_config", + [HW_DMA_START] = "dma_start", + [HW_DMA_STOP] = "dma_stop", + [HW_DMA_SUSPEND] = "dma_suspend", + [HW_DMA_RELEASE] = "dma_release", + [HW_DAI_TRIGGER] = "dai_trigger", + }; + + return op < ARRAY_SIZE(names) ? names[op] : "?"; +} + +/* Expected hardware call log */ +static struct hw_call hw_exp[HW_LOG_SIZE]; +static size_t hw_exp_len; + +static void expect_hw(enum hw_op op, int dai, int cmd) +{ + zassert_true(hw_exp_len < HW_LOG_SIZE, "expected call log overflow"); + hw_exp[hw_exp_len++] = (struct hw_call){ .op = op, .dai = dai, .cmd = cmd }; +} + +/* + * Expect the hardware operations of DAI @dai for trigger command @cmd, in the + * order of dai_comp_trigger_internal() with the default + * CONFIG_COMP_DAI_STOP_TRIGGER_ORDER_REVERSE=n + */ +static void expect_triggered(int cmd, int dai) +{ + switch (cmd) { + case COMP_TRIGGER_START: + expect_hw(HW_DMA_START, dai, 0); + expect_hw(HW_DAI_TRIGGER, dai, DAI_TRIGGER_START); + break; + case COMP_TRIGGER_RELEASE: + expect_hw(HW_DMA_STOP, dai, 0); + expect_hw(HW_DMA_CONFIG, dai, 0); + expect_hw(HW_DMA_START, dai, 0); + expect_hw(HW_DAI_TRIGGER, dai, DAI_TRIGGER_START); + break; + case COMP_TRIGGER_STOP: + expect_hw(HW_DAI_TRIGGER, dai, DAI_TRIGGER_STOP); + expect_hw(HW_DMA_STOP, dai, 0); + break; + case COMP_TRIGGER_PAUSE: + expect_hw(HW_DAI_TRIGGER, dai, DAI_TRIGGER_PAUSE); + expect_hw(HW_DMA_SUSPEND, dai, 0); + break; + default: + zassert_unreachable("command %d not modelled", cmd); + } +} + +static void expect_triggered_dais(int cmd, const int *dais, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) + expect_triggered(cmd, dais[i]); +} + +/* Expect the DAIs of the arguments to be triggered with @cmd, in this order */ +#define EXPECT_TRIGGERED(cmd, ...) \ + expect_triggered_dais(cmd, (const int[]){ __VA_ARGS__ }, \ + sizeof((const int[]){ __VA_ARGS__ }) / sizeof(int)) + +/* Check the hardware call log against the expected one, and clear both */ +static void check_hw_log(void) +{ + size_t n = hw_exp_len; + size_t i; + + for (i = 0; i < MIN(n, hw_log_len); i++) + zassert_true(hw_log[i].op == hw_exp[i].op && hw_log[i].dai == hw_exp[i].dai && + hw_log[i].cmd == hw_exp[i].cmd, + "call %zu: got %s dai %d cmd %d, expected %s dai %d cmd %d", i, + hw_op_name(hw_log[i].op), hw_log[i].dai, hw_log[i].cmd, + hw_op_name(hw_exp[i].op), hw_exp[i].dai, hw_exp[i].cmd); + + if (hw_log_len > n) + zassert_unreachable("unexpected call %zu: %s dai %d cmd %d", n, + hw_op_name(hw_log[n].op), hw_log[n].dai, hw_log[n].cmd); + + zassert_equal(hw_log_len, n, "%zu hardware calls, expected %zu", hw_log_len, n); + hw_log_clear(); + hw_exp_len = 0; +} + +/* Create DAIs 0, 1 and 2 and assign them to group @group_id in this order */ +static struct dai_group *test_group_of_3(uint32_t group_id) +{ + struct dai_group *group; + int i; + + for (i = 0; i < 3; i++) { + test_dai_new(i); + zassert_equal(test_assign(i, group_id), 0); + } + + group = dd_of(0)->group; + zassert_not_null(group); + zassert_equal(dd_of(1)->group, group); + zassert_equal(dd_of(2)->group, group); + zassert_equal(group->group_id, group_id); + zassert_equal(group->num_dais, 3); + + return group; +} + +/* + * dai_group_get() and dai_group_put(): one group per ID, with a DAI count, and + * a group that loses its last DAI is marked unused and reused for the next new + * ID. + */ +ZTEST(dai_group, test_group_get_put) +{ + struct dai_group *g1, *g2, *g3; + + g1 = dai_group_get(11, DAI_CREAT); + zassert_not_null(g1); + zassert_equal(g1->group_id, 11); + zassert_equal(g1->num_dais, 1); + + /* the same ID gives the same group, with or without DAI_CREAT */ + zassert_equal(dai_group_get(11, DAI_CREAT), g1); + zassert_equal(g1->num_dais, 2); + zassert_equal(dai_group_get(11, 0), g1); + zassert_equal(g1->num_dais, 3); + + /* another ID gives another group */ + g2 = dai_group_get(12, DAI_CREAT); + zassert_not_null(g2); + zassert_not_equal(g2, g1); + zassert_equal(g2->group_id, 12); + zassert_equal(g2->num_dais, 1); + + /* group ID 0 means no group, it cannot be requested */ + zassert_is_null(dai_group_get(0, DAI_CREAT)); + + dai_group_put(g1); + dai_group_put(g1); + zassert_equal(g1->num_dais, 1); + zassert_equal(g1->group_id, 11); + + /* the last DAI marks the group unused, with ID 0 */ + dai_group_put(g1); + zassert_equal(g1->num_dais, 0); + zassert_equal(g1->group_id, 0); + + /* and it is reused for a new ID */ + g3 = dai_group_get(13, DAI_CREAT); + zassert_equal(g3, g1); + zassert_equal(g3->group_id, 13); + zassert_equal(g3->num_dais, 1); + + /* the other group is not affected */ + zassert_equal(g2->group_id, 12); + zassert_equal(g2->num_dais, 1); + + dai_group_put(g3); + dai_group_put(g2); + zassert_equal(g3->group_id, 0); + zassert_equal(g2->group_id, 0); +} + +/* + * dai_assign_group(): a DAI joins a group once, assigning it to the same group + * again does nothing and assigning it to another group fails. + */ +ZTEST(dai_group, test_assign_group) +{ + struct dai_group *group; + + test_dai_new(0); + test_dai_new(1); + + zassert_equal(test_assign(0, 21), 0); + group = dd_of(0)->group; + zassert_not_null(group); + zassert_equal(group->group_id, 21); + zassert_equal(group->num_dais, 1); + + /* the same group again is a no-op */ + zassert_equal(test_assign(0, 21), 0); + zassert_equal(dd_of(0)->group, group); + zassert_equal(group->num_dais, 1); + + /* another group is rejected, and the DAI stays in its group */ + zassert_equal(test_assign(0, 22), -EINVAL); + zassert_equal(dd_of(0)->group, group); + zassert_equal(group->group_id, 21); + zassert_equal(group->num_dais, 1); + + zassert_equal(test_assign(1, 21), 0); + zassert_equal(dd_of(1)->group, group); + zassert_equal(group->num_dais, 2); + + /* DAI 0 joined the group once, so the group trigger reaches it once */ + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + check_hw_log(); + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 1, 0); + check_hw_log(); +} + +/* + * Atomic group trigger: the DAIs that receive a command first trigger no + * hardware, the last one triggers every DAI of the group, once each, in the + * group order, which is the most recently assigned DAI first, whatever the + * order in which the DAIs receive the command. + */ +ZTEST(dai_group, test_group_trigger) +{ + struct dai_group *group = test_group_of_3(31); + + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(group->trigger_counter, 2); + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + zassert_equal(group->trigger_counter, 1); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_START), 0); + zassert_equal(group->trigger_counter, 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 2, 1, 0); + check_hw_log(); + + zassert_equal(test_trigger(2, COMP_TRIGGER_PAUSE), 0); + zassert_equal(test_trigger(0, COMP_TRIGGER_PAUSE), 0); + check_hw_log(); + zassert_equal(test_trigger(1, COMP_TRIGGER_PAUSE), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_PAUSE, 2, 1, 0); + check_hw_log(); + + zassert_equal(test_trigger(1, COMP_TRIGGER_RELEASE), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_RELEASE), 0); + check_hw_log(); + zassert_equal(test_trigger(0, COMP_TRIGGER_RELEASE), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_RELEASE, 2, 1, 0); + check_hw_log(); + + zassert_equal(test_trigger(1, COMP_TRIGGER_STOP), 0); + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_STOP), 0); + zassert_equal(group->trigger_counter, 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 2, 1, 0); + check_hw_log(); +} + +/* + * Result of a group trigger: every DAI of the group is triggered, even if one + * fails, and the group trigger returns the result of the last DAI triggered, + * the first one assigned, so the error of any other DAI is lost. A DAI whose + * DMA fails to start does not trigger its DAI. dai_comp_trigger_internal() + * ignores the result of the DAI hardware trigger. + */ +ZTEST(dai_group, test_group_trigger_error) +{ + test_group_of_3(41); + + /* the DMA of the first DAI triggered fails to start: the error is lost */ + hw_inject_failure(HW_DMA_START, 2, -EIO); + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_START), 0); + expect_hw(HW_DMA_START, 2, 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 1, 0); + check_hw_log(); + + /* the DMA of the last DAI triggered fails: the group returns its error */ + hw_inject_failure(HW_DMA_START, 0, -EBUSY); + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_START), -EBUSY); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 2, 1); + expect_hw(HW_DMA_START, 0, 0); + check_hw_log(); + + /* same for the DMA suspend on PAUSE */ + hw_inject_failure(HW_DMA_SUSPEND, 0, -ENODEV); + zassert_equal(test_trigger(0, COMP_TRIGGER_PAUSE), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_PAUSE), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_PAUSE), -ENODEV); + EXPECT_TRIGGERED(COMP_TRIGGER_PAUSE, 2, 1, 0); + check_hw_log(); + + /* a DAI hardware trigger failure is not reported */ + hw_inject_failure(HW_DAI_TRIGGER, 0, -EIO); + zassert_equal(test_trigger(0, COMP_TRIGGER_RELEASE), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_RELEASE), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_RELEASE), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_RELEASE, 2, 1, 0); + check_hw_log(); +} + +/* + * A DAI that receives another command while a group trigger is pending gets + * -EAGAIN and does not count for the pending trigger, which still happens when + * the remaining DAIs receive the original command. + */ +ZTEST(dai_group, test_group_trigger_busy) +{ + struct dai_group *group = test_group_of_3(51); + + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_STOP), -EAGAIN); + zassert_equal(test_trigger(2, COMP_TRIGGER_PAUSE), -EAGAIN); + zassert_equal(group->trigger_cmd, COMP_TRIGGER_START); + zassert_equal(group->trigger_counter, 2); + check_hw_log(); + + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_START), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 2, 1, 0); + check_hw_log(); + + /* the group takes another command once the trigger is done */ + zassert_equal(test_trigger(1, COMP_TRIGGER_STOP), 0); + zassert_equal(group->trigger_cmd, COMP_TRIGGER_STOP); + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 2, 1, 0); + check_hw_log(); +} + +/* A DAI that is not in a group triggers its own hardware immediately */ +ZTEST(dai_group, test_no_group_trigger) +{ + test_group_of_3(61); + test_dai_new(3); + + zassert_equal(test_trigger(3, COMP_TRIGGER_START), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 3); + check_hw_log(); + + /* also while a group trigger is pending */ + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + check_hw_log(); + zassert_equal(test_trigger(3, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 3); + check_hw_log(); + + /* and it returns its own error */ + hw_inject_failure(HW_DMA_START, 3, -EIO); + zassert_equal(test_trigger(3, COMP_TRIGGER_START), -EIO); + expect_hw(HW_DMA_START, 3, 0); + check_hw_log(); + + /* the pending group trigger is not affected */ + zassert_equal(test_trigger(1, COMP_TRIGGER_STOP), 0); + zassert_equal(test_trigger(2, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 2, 1, 0); + check_hw_log(); +} + +/* + * A DAI that is freed leaves its group: the group trigger then waits only for + * the remaining DAIs, and reaches only them. + */ +ZTEST(dai_group, test_free_leaves_group) +{ + struct dai_group *group = test_group_of_3(71); + + test_dai_free(1); + zassert_equal(group->num_dais, 2); + zassert_equal(group->group_id, 71); + expect_hw(HW_DMA_RELEASE, 1, 0); + check_hw_log(); + + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_START), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 2, 0); + check_hw_log(); + + /* a DAI that joins later is triggered first */ + test_dai_new(3); + zassert_equal(test_assign(3, 71), 0); + zassert_equal(dd_of(3)->group, group); + zassert_equal(group->num_dais, 3); + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + zassert_equal(test_trigger(3, COMP_TRIGGER_STOP), 0); + check_hw_log(); + zassert_equal(test_trigger(2, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 3, 2, 0); + check_hw_log(); + + /* the last DAI to leave releases the group */ + test_dai_free(0); + test_dai_free(2); + zassert_equal(group->num_dais, 1); + test_dai_free(3); + zassert_equal(group->num_dais, 0); + zassert_equal(group->group_id, 0); + expect_hw(HW_DMA_RELEASE, 0, 0); + expect_hw(HW_DMA_RELEASE, 2, 0); + expect_hw(HW_DMA_RELEASE, 3, 0); + check_hw_log(); +} + +/* A group of one DAI triggers at once, also when the others have left it */ +ZTEST(dai_group, test_single_dai_group) +{ + struct dai_group *group; + + test_dai_new(0); + zassert_equal(test_assign(0, 81), 0); + group = dd_of(0)->group; + zassert_equal(group->num_dais, 1); + + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(group->trigger_counter, 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 0); + check_hw_log(); + + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 0); + check_hw_log(); + + test_dai_free(0); + expect_hw(HW_DMA_RELEASE, 0, 0); + check_hw_log(); + + group = test_group_of_3(82); + test_dai_free(0); + test_dai_free(1); + zassert_equal(group->num_dais, 1); + expect_hw(HW_DMA_RELEASE, 0, 0); + expect_hw(HW_DMA_RELEASE, 1, 0); + check_hw_log(); + + zassert_equal(test_trigger(2, COMP_TRIGGER_START), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_START, 2); + check_hw_log(); +} + +/* + * A group released while a trigger was pending starts from a clean state when + * it is reused for another group ID. + */ +ZTEST(dai_group, test_released_group_reset) +{ + struct dai_group *group = test_group_of_3(91); + struct dai_group *reused; + int i; + + zassert_equal(test_trigger(0, COMP_TRIGGER_START), 0); + zassert_equal(test_trigger(1, COMP_TRIGGER_START), 0); + zassert_equal(group->trigger_counter, 1); + check_hw_log(); + + for (i = 0; i < 3; i++) { + test_dai_free(i); + expect_hw(HW_DMA_RELEASE, i, 0); + } + check_hw_log(); + zassert_equal(group->group_id, 0); + zassert_equal(group->trigger_counter, 0); + + test_dai_new(0); + test_dai_new(1); + zassert_equal(test_assign(0, 92), 0); + zassert_equal(test_assign(1, 92), 0); + reused = dd_of(0)->group; + zassert_equal(reused, group); + zassert_equal(reused->num_dais, 2); + + zassert_equal(test_trigger(0, COMP_TRIGGER_STOP), 0); + check_hw_log(); + zassert_equal(test_trigger(1, COMP_TRIGGER_STOP), 0); + EXPECT_TRIGGERED(COMP_TRIGGER_STOP, 1, 0); + check_hw_log(); +} + +static void dai_group_before(void *fixture) +{ + hw_log_clear(); + hw_exp_len = 0; + hw_inject_failure(HW_NONE, 0, 0); +} + +static void dai_group_after(void *fixture) +{ + int i; + + for (i = 0; i < TEST_NUM_DAIS; i++) + if (test_devs[i]) + test_dai_free(i); +} + +ZTEST_SUITE(dai_group, NULL, NULL, dai_group_before, dai_group_after, NULL); + +/* + * The group trigger on the native_sim virtual DAI and POSIX DMA models. The DMA + * model asserts that each channel is configured and started only when stopped, + * and suspended or stopped only when started, so a wrong sequence of DMA + * operations in any step fails the test. + */ +static struct comp_dev *sim_devs[SIM_NUM_DAIS]; +static struct dma_config sim_dma_config[SIM_NUM_DAIS]; + +static void sim_dai_new(int i) +{ + const struct comp_driver *drv = dai_driver_get(); + struct comp_ipc_config config = { + .id = SIM_DAI_FIRST + i, + .type = SOF_COMP_DAI, + }; + struct ipc_config_dai dai_cfg = { + .direction = SOF_IPC_STREAM_PLAYBACK, + .dai_index = SIM_DAI_FIRST + i, + .type = SOF_DAI_INTEL_HDA, + }; + struct dai_data *dd; + + sim_devs[i] = drv->ops.create(drv, &config, &dai_cfg); + zassert_not_null(sim_devs[i]); + sim_devs[i]->direction = SOF_IPC_STREAM_PLAYBACK; + + dd = comp_get_drvdata(sim_devs[i]); + zassert_equal(dd->dma, &sim_dma); + /* normally set up by dai_config_dma_channel() and dai_params() */ + dd->chan_index = i; + dd->z_config = &sim_dma_config[i]; +} + +ZTEST(dai_group_sim, test_sim_group_trigger) +{ + static const int cmds[] = { + COMP_TRIGGER_START, COMP_TRIGGER_PAUSE, COMP_TRIGGER_RELEASE, COMP_TRIGGER_STOP, + }; + struct dai_data *dd[SIM_NUM_DAIS]; + size_t c; + int i; + + sim_dma.z_dev = device_get_binding("pzdma0"); + zassert_not_null(sim_dma.z_dev); + for (i = 0; i < SIM_NUM_DAIS; i++) { + zassert_true(device_is_ready(sim_dais[i].dev)); + sim_dai_new(i); + dd[i] = comp_get_drvdata(sim_devs[i]); + zassert_equal(dai_assign_group(dd[i], sim_devs[i], 101), 0); + } + + for (c = 0; c < ARRAY_SIZE(cmds); c++) + for (i = 0; i < SIM_NUM_DAIS; i++) + zassert_equal(dai_common_trigger(dd[i], sim_devs[i], cmds[c]), 0, + "command %d on DAI %d", cmds[c], i); + + /* both channels are stopped: the DMA model only configures those */ + for (i = 0; i < SIM_NUM_DAIS; i++) + zassert_equal(sof_dma_config(&sim_dma, i, &sim_dma_config[i]), 0); +} + +static void dai_group_sim_after(void *fixture) +{ + int i; + + for (i = 0; i < SIM_NUM_DAIS; i++) { + if (!sim_devs[i]) + continue; + ((struct dai_data *)comp_get_drvdata(sim_devs[i]))->z_config = NULL; + comp_free(sim_devs[i]); + sim_devs[i] = NULL; + } +} + +ZTEST_SUITE(dai_group_sim, NULL, NULL, NULL, dai_group_sim_after, NULL); diff --git a/test/ztest/unit/audio/dai_group/prj.conf b/test/ztest/unit/audio/dai_group/prj.conf new file mode 100644 index 000000000000..f270eb3ae798 --- /dev/null +++ b/test/ztest/unit/audio/dai_group/prj.conf @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_IPC_MAJOR_4=y +# The DAI code of src/audio/dai-zephyr.c, on the Zephyr DAI and DMA driver APIs +CONFIG_ZEPHYR_NATIVE_DRIVERS=y +CONFIG_COMP_DAI=y +CONFIG_COMP_DAI_GROUP=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y +# The virtual DAIs register at CONFIG_DAI_INIT_PRIORITY, and the DAI and DMA +# driver API sections are only collected with the subsystems enabled, which +# the API checks of CONFIG_ASSERT need +CONFIG_DAI=y +CONFIG_DMA=y +# The POSIX DMA model asserts its channel state transitions. Its start takes +# the channel lock again in resume, so spinlock validation is off. +CONFIG_ASSERT=y +CONFIG_SPIN_VALIDATE=n diff --git a/test/ztest/unit/audio/dai_group/testcase.yaml b/test/ztest/unit/audio/dai_group/testcase.yaml new file mode 100644 index 000000000000..a10d2c5d0d0f --- /dev/null +++ b/test/ztest/unit/audio/dai_group/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DAI group trigger unit tests +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - dai + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.dai_group: + platform_allow: + - native_sim + build_only: false