diff --git a/src/audio/Kconfig b/src/audio/Kconfig index fecbf0d25afa..1867e871bf92 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -161,19 +161,6 @@ config COMP_KPB if COMP_KPB -config KPB_CLI_Q - bool "Use a client queue for KPB" - help - Use a Zephyr queue instead of SOF notifiers - -config KPB_NOTIFIER - bool "Enable Notifier for KPB events" - default y - help - Enable KPB client events delivered through the SOF notifier API. - This transport can be enabled independently of the Async Messaging - Service (AMS), including together with AMS. - config KPB_FORCE_COPY_TYPE_NORMAL bool "KPB force copy type normal" default y diff --git a/src/audio/google/google_hotword_detect.c b/src/audio/google/google_hotword_detect.c index aa3c3db5e533..4098dd794c63 100644 --- a/src/audio/google/google_hotword_detect.c +++ b/src/audio/google/google_hotword_detect.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -47,7 +46,6 @@ SOF_DEFINE_REG_UUID(google_hotword); struct comp_data { struct comp_data_blob_handler *model_handler; - struct kpb_event_data event_data; struct kpb_client client_data; struct ipc_msg *msg; @@ -69,18 +67,13 @@ static void notify_host(const struct comp_dev *dev) static void notify_kpb(const struct comp_dev *dev) { struct comp_data *cd = comp_get_drvdata(dev); + int ret; comp_dbg(dev, "entry"); - cd->client_data.r_ptr = NULL; - cd->client_data.sink = NULL; - cd->client_data.id = 0; - cd->event_data.event_id = KPB_EVENT_BEGIN_DRAINING; - cd->event_data.client_data = &cd->client_data; - - notifier_event(dev, NOTIFIER_ID_KPB_CLIENT_EVT, - NOTIFIER_TARGET_CORE_ALL_MASK, &cd->event_data, - sizeof(cd->event_data)); + ret = kpb_request_draining(0, cd->client_data.drain_req); + if (ret < 0) + comp_err(dev, "KPB draining request failed: %d", ret); } static struct comp_dev *ghd_create(const struct comp_driver *drv, diff --git a/src/audio/kpb.c b/src/audio/kpb.c index f0f3ff743584..6a0470da1f9e 100644 --- a/src/audio/kpb.c +++ b/src/audio/kpb.c @@ -51,9 +51,6 @@ #include #include #endif -#if CONFIG_KPB_NOTIFIER -#include -#endif #ifdef KPB_HIFI4 #include @@ -92,6 +89,7 @@ struct comp_data { struct comp_buffer *sel_sink; /**< real time sink (channel selector)*/ struct comp_buffer *host_sink; /**< draining sink (client) */ uint32_t kpb_no_of_clients; /**< number of registered clients */ + uint8_t drain_seq; /**< sequence number of the last draining request taken */ uint32_t source_period_bytes; /**< source number of period bytes */ uint32_t sink_period_bytes; /**< sink number of period bytes */ size_t host_buffer_size; /**< size of host buffer */ @@ -110,32 +108,12 @@ struct comp_data { struct kpb_fmt_dev_list fmt_device_list; struct fast_mode_task fmt; -#if CONFIG_AMS && !CONFIG_KPB_CLI_Q +#if CONFIG_AMS uint32_t kpd_uuid_id; #endif }; /*! KPB private functions */ -#if CONFIG_KPB_CLI_Q -#include - -/* - * Only a single KPB instance is currently supported: the queue, thread and - * dev below are global. Requests are copied by value, so user space never - * passes pointers to the kernel. - */ -K_MSGQ_DEFINE(kpb_cli_msgq, sizeof(struct kpb_client), KPB_MAX_NO_OF_CLIENTS, - __alignof__(struct kpb_client)); -static struct comp_dev *kpb_cli_q_dev; -static struct k_thread kpb_thread; -K_KERNEL_STACK_DEFINE(kpb_stack, 4096); -#endif - -#if CONFIG_KPB_NOTIFIER -static void kpb_event_handler(void *arg, enum notify_id type, void *event_data); -static int kpb_register_client(struct comp_data *kpb, struct kpb_client *cli); -#endif - static void kpb_init_draining(struct comp_dev *dev, struct kpb_client *cli); static enum task_state kpb_draining_task(void *arg); static int kpb_buffer_data(struct comp_dev *dev, @@ -195,7 +173,7 @@ static uint64_t kpb_task_deadline(void *data) #endif } -#if CONFIG_AMS && !CONFIG_KPB_CLI_Q +#if CONFIG_AMS /* Key-phrase detected message*/ static const ams_uuid_t ams_kpd_msg_uuid = AMS_KPD_MSG_UUID; @@ -483,39 +461,6 @@ static void kpb_set_params(struct comp_dev *dev, static int kpb_params(struct comp_dev *dev, struct sof_ipc_stream_params *params); -#if CONFIG_KPB_CLI_Q -static void kpb_thread_fn(void *p1, void *p2, void *p3) -{ - struct comp_dev *dev = p1; - struct kpb_client cli; - - for (;;) { - k_msgq_get(&kpb_cli_msgq, &cli, K_FOREVER); - /* keep the LL thread (kpb_copy()) from preempting draining setup */ - k_sched_lock(); - kpb_init_draining(dev, &cli); - k_sched_unlock(); - } -} - -int z_impl_kpb_notifier_schedule(uint32_t id, uint32_t drain_req) -{ - struct kpb_client cli = { - .id = id, - .drain_req = drain_req, - }; - - if (id >= KPB_MAX_NO_OF_CLIENTS || drain_req > KPB_MAX_DRAINING_REQ) - return -EINVAL; - - if (!kpb_cli_q_dev) - return -ENODEV; - - return k_msgq_put(&kpb_cli_msgq, &cli, K_NO_WAIT); -} -EXPORT_SYMBOL(z_impl_kpb_notifier_schedule); -#endif - /* * \brief Create a key phrase buffer component. * \param[in] config - generic ipc component pointer. @@ -555,13 +500,6 @@ __cold static struct comp_dev *kpb_new(const struct comp_driver *drv, return NULL; } -#if CONFIG_KPB_CLI_Q - if (kpb_cli_q_dev) { - comp_cl_err(&comp_kpb, "only one KPB instance is supported with KPB_CLI_Q"); - return NULL; - } -#endif - dev = comp_alloc(drv, sizeof(*dev)); if (!dev) return NULL; @@ -618,16 +556,6 @@ __cold static struct comp_dev *kpb_new(const struct comp_driver *drv, } #endif -#if CONFIG_KPB_CLI_Q - k_msgq_purge(&kpb_cli_msgq); - k_thread_create(&kpb_thread, kpb_stack, 4096, kpb_thread_fn, dev, NULL, NULL, - 1, 0, K_FOREVER); - - k_thread_cpu_pin(&kpb_thread, cpu_get_id()); - k_thread_start(&kpb_thread); - kpb_cli_q_dev = dev; -#endif - return dev; } @@ -775,11 +703,7 @@ __cold static void kpb_free(struct comp_dev *dev) comp_info(dev, "entry"); -#if CONFIG_KPB_CLI_Q - kpb_cli_q_dev = NULL; - k_thread_abort(&kpb_thread); - k_msgq_purge(&kpb_cli_msgq); -#elif CONFIG_AMS +#if CONFIG_AMS /* Unregister KPB as AMS consumer */ int ret; @@ -787,12 +711,7 @@ __cold static void kpb_free(struct comp_dev *dev) kpb_ams_kpd_notification); if (ret) comp_err(dev, "AMS unregister error %d", ret); -#endif/* CONFIG_KPB_CLI_Q */ - -#if CONFIG_KPB_NOTIFIER - /* Unregister KPB from notifications */ - notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT); -#endif/* CONFIG_KPB_NOTIFIER */ +#endif /* CONFIG_AMS */ /* Reclaim memory occupied by history buffer */ kpb_free_history_buffer(kpb->hd.c_hb); @@ -868,7 +787,7 @@ static int kpb_params(struct comp_dev *dev, kpb->host_period_size = params->host_period_bytes; kpb->config.sampling_width = params->sample_container_bytes * 8; -#if !CONFIG_KPB_CLI_Q && CONFIG_AMS +#if CONFIG_AMS kpb->kpd_uuid_id = AMS_INVALID_MSG_TYPE; #endif @@ -957,33 +876,20 @@ static int kpb_prepare(struct comp_dev *dev) kpb->clients[i].r_ptr = NULL; } -#if CONFIG_KPB_NOTIFIER - /* Register KPB for notification *on the current core* */ - ret = notifier_register(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT, kpb_event_handler); - - if (ret < 0) { - kpb_free_history_buffer(kpb->hd.c_hb); - kpb->hd.c_hb = NULL; - return -ENOMEM; - } -#endif + /* only take the draining requests posted from now on */ + kpb->drain_seq = KPB_DRAIN_REQ_SEQ(atomic_get(&kpb_drain_request)); -#if !CONFIG_KPB_CLI_Q #if CONFIG_AMS /* AMS Register KPB for notification */ ret = ams_helper_register_consumer(dev, &kpb->kpd_uuid_id, ams_kpd_msg_uuid, kpb_ams_kpd_notification); if (ret < 0) { -#if CONFIG_KPB_NOTIFIER - notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT); -#endif kpb_free_history_buffer(kpb->hd.c_hb); kpb->hd.c_hb = NULL; return -ENOMEM; } #endif /* CONFIG_AMS */ -#endif #ifndef CONFIG_IPC_MAJOR_4 /* Search for KPB related sinks. @@ -1109,10 +1015,6 @@ static int kpb_reset(struct comp_dev *dev) kpb_reset_history_buffer(kpb->hd.c_hb); } -#if CONFIG_KPB_NOTIFIER - /* Unregister KPB from notifications */ - notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT); -#endif /* Finally KPB is ready after reset */ kpb_change_state(kpb, KPB_STATE_PREPARING); @@ -1362,6 +1264,27 @@ static void kpb_micselect_copy(struct comp_dev *dev, struct comp_buffer *sink_c, * 0 - success * -EINVAL - failure. */ +/* + * Start draining if a key phrase detector requested it with + * kpb_request_draining() since the last copy, see kpb_init_draining(). + */ +static void kpb_take_draining_request(struct comp_dev *dev) +{ + struct comp_data *kpb = comp_get_drvdata(dev); + uint32_t req = atomic_get(&kpb_drain_request); + struct kpb_client cli = { + .id = KPB_DRAIN_REQ_ID(req), + .drain_req = KPB_DRAIN_REQ_LEN(req), + }; + + if (KPB_DRAIN_REQ_SEQ(req) == kpb->drain_seq) + return; + + kpb->drain_seq = KPB_DRAIN_REQ_SEQ(req); + comp_info(dev, "draining request client %u, %u ms", cli.id, cli.drain_req); + kpb_init_draining(dev, &cli); +} + static int kpb_copy(struct comp_dev *dev) { int ret = 0; @@ -1390,6 +1313,8 @@ static int kpb_copy(struct comp_dev *dev) return ret; } + kpb_take_draining_request(dev); + switch (kpb->state) { case KPB_STATE_RUN: /* In normal RUN state we simply copy to our sink. */ @@ -1678,95 +1603,6 @@ static int kpb_buffer_data(struct comp_dev *dev, return ret; } -/** - * \brief Main event dispatcher. - * \param[in] arg - KPB component internal data. - * \param[in] type - notification type - * \param[in] event_data - event specific data. - * \return none. - */ -#if CONFIG_KPB_NOTIFIER -static void kpb_event_handler(void *arg, enum notify_id type, void *event_data) -{ - struct comp_dev *dev = arg; - struct comp_data *kpb = comp_get_drvdata(dev); - struct kpb_event_data *evd = event_data; - struct kpb_client *cli = evd->client_data; - - comp_info(dev, "received event with ID: %d ", - evd->event_id); - - switch (evd->event_id) { - case KPB_EVENT_REGISTER_CLIENT: - kpb_register_client(kpb, cli); - break; - case KPB_EVENT_UNREGISTER_CLIENT: - if (!cli || cli->id >= KPB_MAX_NO_OF_CLIENTS || - kpb->clients[cli->id].state == KPB_CLIENT_UNREGISTERED) - break; - - kpb->clients[cli->id].state = KPB_CLIENT_UNREGISTERED; - kpb->clients[cli->id].r_ptr = NULL; - kpb->clients[cli->id].sink = NULL; - kpb->kpb_no_of_clients--; - break; - case KPB_EVENT_BEGIN_DRAINING: - kpb_init_draining(dev, cli); - break; - case KPB_EVENT_STOP_DRAINING: - /*TODO*/ - break; - default: - comp_err(dev, "unsupported command"); - break; - } -} - -/** - * \brief Register clients in the system. - * - * \param[in] kpb - kpb device component pointer. - * \param[in] cli - pointer to KPB client's data. - * - * \return integer representing either: - * 0 - success - * -EINVAL - failure. - */ -static int kpb_register_client(struct comp_data *kpb, struct kpb_client *cli) -{ - int ret = 0; - - comp_cl_info(&comp_kpb, "entry"); - - if (!cli) { - comp_cl_err(&comp_kpb, "no client data"); - return -EINVAL; - } - /* Do we have a room for a new client? */ - if (kpb->kpb_no_of_clients >= KPB_MAX_NO_OF_CLIENTS || - cli->id >= KPB_MAX_NO_OF_CLIENTS) { - comp_cl_err(&comp_kpb, "no free room for client = %u", - cli->id); - ret = -EINVAL; - } else if (kpb->clients[cli->id].state != KPB_CLIENT_UNREGISTERED) { - comp_cl_err(&comp_kpb, "client = %u already registered", - cli->id); - ret = -EINVAL; - } else { - /* Client accepted, let's store his data */ - kpb->clients[cli->id].id = cli->id; - kpb->clients[cli->id].drain_req = cli->drain_req; - kpb->clients[cli->id].sink = cli->sink; - kpb->clients[cli->id].r_ptr = NULL; - kpb->clients[cli->id].state = KPB_CLIENT_BUFFERING; - kpb->kpb_no_of_clients++; - ret = 0; - } - - return ret; -} -#endif /* CONFIG_KPB_NOTIFIER */ - /** * \brief Prepare history buffer for draining. * diff --git a/src/audio/module_adapter/Kconfig b/src/audio/module_adapter/Kconfig index 04026dcb5b14..bd99dbcade60 100644 --- a/src/audio/module_adapter/Kconfig +++ b/src/audio/module_adapter/Kconfig @@ -226,7 +226,6 @@ endif # Cadence config INTEL_MODULES bool "Intel modules" depends on LIBRARY_MANAGER - select KPB_NOTIFIER if COMP_KPB && !KPB_CLI_Q help Select for a Intel modules API implementation. This will cause module adapter component to include IADK module diff --git a/src/audio/module_adapter/library/kpb_service_bridge.c b/src/audio/module_adapter/library/kpb_service_bridge.c index b2d7c2f717f1..4f2c5a5831d8 100644 --- a/src/audio/module_adapter/library/kpb_service_bridge.c +++ b/src/audio/module_adapter/library/kpb_service_bridge.c @@ -14,14 +14,8 @@ #include #include #include -#if !CONFIG_KPB_CLI_Q -#include -#endif struct kpb_service_client { -#if !CONFIG_KPB_CLI_Q - struct kpb_event_data event; -#endif struct kpb_client client; size_t current_history_depth; size_t max_history_depth; @@ -42,37 +36,17 @@ static bool kpb_service_client_is_valid(const struct kpb_service_client *client) return false; } -#if CONFIG_KPB_CLI_Q -static AdspErrorCode kpb_service_send_event(struct kpb_service_client *client, - enum kpb_event event_id) +static AdspErrorCode kpb_service_request_draining(struct kpb_service_client *client) { - /* KPB draining does not depend on registration, so only draining is queued */ - if (event_id != KPB_EVENT_BEGIN_DRAINING) - return ADSP_NO_ERROR; - - switch (kpb_notifier_schedule(client->client.id, client->client.drain_req)) { + switch (kpb_request_draining(client->client.id, client->client.drain_req)) { case 0: return ADSP_NO_ERROR; case -ENODEV: return ADSP_SERVICE_UNAVAILABLE; - case -ENOMSG: - return ADSP_BUSY_RESOURCE; default: return ADSP_INVALID_PARAMETERS; } } -#else -static AdspErrorCode kpb_service_send_event(struct kpb_service_client *client, - enum kpb_event event_id) -{ - client->event.event_id = event_id; - client->event.client_data = &client->client; - - notifier_event(client, NOTIFIER_ID_KPB_CLIENT_EVT, NOTIFIER_TARGET_CORE_ALL_MASK, - &client->event, sizeof(client->event)); - return ADSP_NO_ERROR; -} -#endif AdspErrorCode kpb_service_register_client(size_t current_history_depth, size_t max_history_depth, uint32_t output_pin, @@ -99,7 +73,6 @@ AdspErrorCode kpb_service_register_client(size_t current_history_depth, new_client->client.state = KPB_CLIENT_UNREGISTERED; new_client->client.r_ptr = NULL; new_client->client.sink = NULL; - kpb_service_send_event(new_client, KPB_EVENT_REGISTER_CLIENT); *client = new_client; return ADSP_NO_ERROR; @@ -113,7 +86,7 @@ AdspErrorCode kpb_service_signal_detection(struct kpb_service_client *client, client->client.drain_req = MIN(client->current_history_depth + phrase_length, client->max_history_depth); - return kpb_service_send_event(client, KPB_EVENT_BEGIN_DRAINING); + return kpb_service_request_draining(client); } AdspErrorCode kpb_service_unregister_client(struct kpb_service_client *client) @@ -121,7 +94,6 @@ AdspErrorCode kpb_service_unregister_client(struct kpb_service_client *client) if (!kpb_service_client_is_valid(client)) return ADSP_INVALID_PARAMETERS; - kpb_service_send_event(client, KPB_EVENT_UNREGISTER_CLIENT); client->in_use = false; return ADSP_NO_ERROR; } diff --git a/src/include/sof/audio/kpb.h b/src/include/sof/audio/kpb.h index 74eeae964539..d0169ec41673 100644 --- a/src/include/sof/audio/kpb.h +++ b/src/include/sof/audio/kpb.h @@ -97,19 +97,6 @@ enum kpb_state { KPB_STATE_RESETTING, }; -enum kpb_event { - KPB_EVENT_REGISTER_CLIENT = 0, - KPB_EVENT_UPDATE_PARAMS, - KPB_EVENT_BEGIN_DRAINING, - KPB_EVENT_STOP_DRAINING, - KPB_EVENT_UNREGISTER_CLIENT, -}; - -struct kpb_event_data { - enum kpb_event event_id; - struct kpb_client *client_data; -}; - enum kpb_client_state { KPB_CLIENT_UNREGISTERED = 0, KPB_CLIENT_BUFFERING, @@ -258,26 +245,55 @@ struct kpb_fmt_dev_list { struct comp_dev *kpb_mi_ptr; }; -#if CONFIG_KPB_CLI_Q +#if CONFIG_COMP_KPB && !CONFIG_LIBRARY +#include +#include + +/* + * A draining request is a single word, so that any thread can post it and + * the KPB instances can read it in their LL threads without a lock: an + * 8-bit sequence number, the client ID and the draining length. + */ +#define KPB_DRAIN_REQ(seq, id, len) ((((uint32_t)(seq) & 0xff) << 24) | \ + (((uint32_t)(id) & 0xf) << 20) | \ + ((uint32_t)(len) & 0xfffff)) +#define KPB_DRAIN_REQ_SEQ(req) (((uint32_t)(req) >> 24) & 0xff) +#define KPB_DRAIN_REQ_ID(req) (((uint32_t)(req) >> 20) & 0xf) +#define KPB_DRAIN_REQ_LEN(req) ((uint32_t)(req) & 0xfffff) + +BUILD_ASSERT(KPB_MAX_NO_OF_CLIENTS <= 16, "KPB client ID does not fit a draining request"); +BUILD_ASSERT(KPB_MAX_DRAINING_REQ < (1 << 20), + "KPB draining length does not fit a draining request"); + +/* the last draining request, posted by kpb_request_draining() */ +extern atomic_t kpb_drain_request; + #if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) #include /** - * \brief Request draining of the KPB history buffer via the client queue. + * \brief Request draining of the KPB history buffers. * - * Callable from user space. The request is handled asynchronously by the - * KPB client queue thread. + * Callable from user space. Every KPB instance that has been prepared takes + * the request in its next copy, so the draining starts within one LL period. * * \param[in] id Client ID, i.e. the KPB output pin. * \param[in] drain_req Requested draining length in milliseconds. - * \return 0 on success, -EINVAL on invalid arguments, -ENODEV if no KPB - * instance exists, -ENOMSG if the queue is full. + * \return 0 on success, -EINVAL on invalid arguments. */ -__syscall int kpb_notifier_schedule(uint32_t id, uint32_t drain_req); +__syscall int kpb_request_draining(uint32_t id, uint32_t drain_req); #include #else -int z_impl_kpb_notifier_schedule(uint32_t id, uint32_t drain_req); -#define kpb_notifier_schedule z_impl_kpb_notifier_schedule +int z_impl_kpb_request_draining(uint32_t id, uint32_t drain_req); +#define kpb_request_draining z_impl_kpb_request_draining #endif +#else +#include + +/* no KPB in this build, so nothing to drain */ +static inline int kpb_request_draining(uint32_t id, uint32_t drain_req) +{ + return -ENODEV; +} #endif #ifdef UNIT_TEST diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index e5413a512cb4..4b429c86d4fd 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -22,7 +22,6 @@ enum notify_id { NOTIFIER_ID_CPU_FREQ = 0, /* struct clock_notify_data * */ NOTIFIER_ID_SSP_FREQ, /* struct clock_notify_data * */ - NOTIFIER_ID_KPB_CLIENT_EVT, /* struct kpb_event_data * */ 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 * */ diff --git a/src/include/sof/userspace/README.md b/src/include/sof/userspace/README.md index cee658fc1da3..0ee34374a981 100644 --- a/src/include/sof/userspace/README.md +++ b/src/include/sof/userspace/README.md @@ -124,7 +124,7 @@ syscall by subsystem: | Library manager | `lib_manager_free_module` | `sof/lib_manager.h` | scattered: `kernel/library_manager/lib_manager.c` | | Scheduling | `zephyr_ll_task_sem_alloc`, `zephyr_ll_task_sem_free`, `scheduler_dp_internal_free`, `scheduler_dp_ll_tick`, `scheduler_dp_kick` | `sof/schedule/ll_schedule_domain.h`, `sof/schedule/dp_schedule.h` | scattered: `kernel/schedule/zephyr_ll-kernel.c`, `kernel/schedule/zephyr_dp_schedule*-kernel.c` | | Debug | `debug_stream_slot_send_record` | `user/debug_stream_slot.h` | `kernel/debug/debug_stream/debug_stream_slot.c` | -| KPB | `kpb_notifier_schedule` | `sof/audio/kpb.h` | `zephyr/syscall/kpb.c` / `audio/kpb.c` | +| KPB | `kpb_request_draining` | `sof/audio/kpb.h` | `zephyr/syscall/kpb.c` / `kernel/audio/kpb-kernel.c` | ### Allowed callers @@ -137,7 +137,7 @@ from user-writable data such as `ipc_user->thread[]`. | Allowed callers | Syscalls | |-----------------|----------| -| Any user thread, including DP module threads | `cpu_get_id`, all module management syscalls (`mod_*`), `ipc_msg_send`, `ipc_msg_list_remove`, `send_resource_notif`, `kpb_notifier_schedule` | +| Any user thread, including DP module threads | `cpu_get_id`, all module management syscalls (`mod_*`), `ipc_msg_send`, `ipc_msg_list_remove`, `send_resource_notif`, `kpb_request_draining` | | IPC user thread only | `ipc_compound_pre_start`, `ipc_compound_post_start`, `ipc_wait_for_compound_msg`, `lib_manager_free_module` | | LL user thread only | `scheduler_dp_ll_tick`, `scheduler_dp_kick` | | LL or IPC user thread | all other SOF syscalls | diff --git a/src/kernel/audio/CMakeLists.txt b/src/kernel/audio/CMakeLists.txt index ddd239e57420..d1a37158b2eb 100644 --- a/src/kernel/audio/CMakeLists.txt +++ b/src/kernel/audio/CMakeLists.txt @@ -4,7 +4,10 @@ if(CONFIG_COMP_BASEFW_IPC4 AND NOT CONFIG_LIBRARY) add_local_sources(sof base_fw.c) endif() add_local_sources_ifdef(CONFIG_IPC4_BASE_FW_INTEL sof base_fw_intel.c) -# The kernel part of src/audio/intel_uaol.c, built with it +# The kernel parts of src/audio/kpb.c and src/audio/intel_uaol.c, built with them +if(CONFIG_COMP_KPB AND NOT CONFIG_LIBRARY) + add_local_sources(sof kpb-kernel.c) +endif() add_local_sources_ifdef(CONFIG_UAOL_INTEL_ADSP sof intel_uaol-kernel.c) if(CONFIG_INTEL_ADSP_MIC_PRIVACY) add_subdirectory(mic_privacy_manager) diff --git a/src/kernel/audio/kpb-kernel.c b/src/kernel/audio/kpb-kernel.c new file mode 100644 index 000000000000..88b874093e83 --- /dev/null +++ b/src/kernel/audio/kpb-kernel.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. + +/* + * The kernel part of the KPB: the kernel half of the kpb_request_draining() + * syscall. The rest of the KPB is in src/audio/kpb.c. + */ + +#include +#include +#include +#include +#include +#include +#include + +/* + * Kernel code writes it, the KPB instances read it in the LL threads, which + * can be user threads. + */ +APP_SYSUSER_BSS atomic_t kpb_drain_request; + +int z_impl_kpb_request_draining(uint32_t id, uint32_t drain_req) +{ + atomic_val_t old; + atomic_val_t new; + + if (id >= KPB_MAX_NO_OF_CLIENTS || drain_req > KPB_MAX_DRAINING_REQ) + return -EINVAL; + + do { + old = atomic_get(&kpb_drain_request); + new = KPB_DRAIN_REQ(KPB_DRAIN_REQ_SEQ(old) + 1, id, drain_req); + } while (!atomic_cas(&kpb_drain_request, old, new)); + + return 0; +} +EXPORT_SYMBOL(z_impl_kpb_request_draining); diff --git a/src/samples/audio/detect_test.c b/src/samples/audio/detect_test.c index a37ee0cfa640..059f9c9a468f 100644 --- a/src/samples/audio/detect_test.c +++ b/src/samples/audio/detect_test.c @@ -44,8 +44,6 @@ #include #include #include -#else -#include #endif #define ACTIVATION_DEFAULT_SHIFT 3 @@ -105,8 +103,6 @@ struct comp_data { #if CONFIG_AMS uint32_t kpd_uuid_id; -#else - struct kpb_event_data event_data; #endif /* CONFIG_AMS */ }; @@ -178,22 +174,16 @@ static int ams_notify_kpb(const struct comp_dev *dev) static void notify_kpb(const struct comp_dev *dev) { struct comp_data *cd = comp_get_drvdata(dev); + /* time in milliseconds */ + uint32_t drain_req = cd->drain_req ? cd->drain_req : cd->config.drain_req; + int ret; comp_info(dev, "preamble: %u", cd->detect_preamble); - cd->client_data.r_ptr = NULL; - cd->client_data.sink = NULL; - cd->client_data.id = 0; /**< TODO: acquire proper id from kpb */ - /* time in milliseconds */ - cd->client_data.drain_req = (cd->drain_req != 0) ? - cd->drain_req : - cd->config.drain_req; - cd->event_data.event_id = KPB_EVENT_BEGIN_DRAINING; - cd->event_data.client_data = &cd->client_data; - - notifier_event(dev, NOTIFIER_ID_KPB_CLIENT_EVT, - NOTIFIER_TARGET_CORE_ALL_MASK, &cd->event_data, - sizeof(cd->event_data)); + /* TODO: acquire proper client id from kpb */ + ret = kpb_request_draining(0, drain_req); + if (ret < 0) + comp_err(dev, "KPB draining request failed: %d", ret); } #endif /* CONFIG_AMS */ diff --git a/test/ztest/unit/kpb/CMakeLists.txt b/test/ztest/unit/kpb/CMakeLists.txt new file mode 100644 index 000000000000..3ddaeb2c4c20 --- /dev/null +++ b/test/ztest/unit/kpb/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_kpb_request) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..") +set(sof_top_dir ${SOF_ROOT}) + +# Include SOF CMake functions +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/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${PROJECT_BINARY_DIR}/include/generated +) + +# Define SOF-specific configurations for unit testing +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_COMP_KPB=1 +) + +target_sources(app PRIVATE + test_kpb_request_ztest.c + ${SOF_ROOT}/src/kernel/audio/kpb-kernel.c +) + +# Add RELATIVE_FILE definitions for SOF trace functionality +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/kpb/prj.conf b/test/ztest/unit/kpb/prj.conf new file mode 100644 index 000000000000..d34c7781cd0a --- /dev/null +++ b/test/ztest/unit/kpb/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/kpb/test_kpb_request_ztest.c b/test/ztest/unit/kpb/test_kpb_request_ztest.c new file mode 100644 index 000000000000..7e456a53b6ff --- /dev/null +++ b/test/ztest/unit/kpb/test_kpb_request_ztest.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. + +/* Tests of the KPB draining request word, see kpb_request_draining() */ + +#include +#include +#include +#include +#include + +static uint32_t request(void) +{ + return atomic_get(&kpb_drain_request); +} + +ZTEST(kpb_request, test_request_fields) +{ + uint32_t before = request(); + uint32_t req; + + zassert_ok(kpb_request_draining(2, 1500)); + + req = request(); + zassert_equal(KPB_DRAIN_REQ_SEQ(req), (KPB_DRAIN_REQ_SEQ(before) + 1) & 0xff); + zassert_equal(KPB_DRAIN_REQ_ID(req), 2); + zassert_equal(KPB_DRAIN_REQ_LEN(req), 1500); +} + +ZTEST(kpb_request, test_request_limits) +{ + uint32_t req; + + zassert_ok(kpb_request_draining(KPB_MAX_NO_OF_CLIENTS - 1, KPB_MAX_DRAINING_REQ)); + + req = request(); + zassert_equal(KPB_DRAIN_REQ_ID(req), KPB_MAX_NO_OF_CLIENTS - 1); + zassert_equal(KPB_DRAIN_REQ_LEN(req), KPB_MAX_DRAINING_REQ); +} + +ZTEST(kpb_request, test_invalid_request) +{ + uint32_t before = request(); + + zassert_equal(kpb_request_draining(KPB_MAX_NO_OF_CLIENTS, 100), -EINVAL); + zassert_equal(kpb_request_draining(0, KPB_MAX_DRAINING_REQ + 1), -EINVAL); + + /* a rejected request is not posted */ + zassert_equal(request(), before); +} + +ZTEST(kpb_request, test_sequence_wraps) +{ + uint32_t seq = KPB_DRAIN_REQ_SEQ(request()); + uint32_t req; + int i; + + /* every request changes the sequence number, also when it wraps */ + for (i = 0; i < 300; i++) { + zassert_ok(kpb_request_draining(i % KPB_MAX_NO_OF_CLIENTS, i)); + + req = request(); + seq = (seq + 1) & 0xff; + zassert_equal(KPB_DRAIN_REQ_SEQ(req), seq); + zassert_equal(KPB_DRAIN_REQ_ID(req), i % KPB_MAX_NO_OF_CLIENTS); + zassert_equal(KPB_DRAIN_REQ_LEN(req), i); + } +} + +ZTEST_SUITE(kpb_request, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/kpb/testcase.yaml b/test/ztest/unit/kpb/testcase.yaml new file mode 100644 index 000000000000..571abad07a64 --- /dev/null +++ b/test/ztest/unit/kpb/testcase.yaml @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# KPB draining request unit tests for Ztest framework + +tests: + sof.unit.kpb_request: + tags: unit + platform_allow: native_sim + integration_platforms: + - native_sim + build_only: false diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index ad5533ab1310..3f3ab65cc1bf 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -620,7 +620,7 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/cpu.c) zephyr_library_sources_ifdef(CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK syscall/caller.c) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/audio/module_adapter/module/generic.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/audio/kpb.h) -if(CONFIG_USERSPACE AND CONFIG_KPB_CLI_Q) +if(CONFIG_USERSPACE AND CONFIG_COMP_KPB) zephyr_library_sources(syscall/kpb.c) endif() zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/lib/fast-get.h) diff --git a/zephyr/syscall/kpb.c b/zephyr/syscall/kpb.c index 11837baefcf2..1c608d736803 100644 --- a/zephyr/syscall/kpb.c +++ b/zephyr/syscall/kpb.c @@ -8,7 +8,7 @@ #include /** - * \brief Userspace verification wrapper for kpb_notifier_schedule(). + * \brief Userspace verification wrapper for kpb_request_draining(). * * Only scalar arguments are passed, their ranges are checked by the * implementation, so no memory access validation is required. @@ -17,8 +17,8 @@ * \param[in] drain_req Requested draining length in milliseconds. * \return 0 on success, negative error code otherwise. */ -static inline int z_vrfy_kpb_notifier_schedule(uint32_t id, uint32_t drain_req) +static inline int z_vrfy_kpb_request_draining(uint32_t id, uint32_t drain_req) { - return z_impl_kpb_notifier_schedule(id, drain_req); + return z_impl_kpb_request_draining(id, drain_req); } -#include +#include