diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 0093d5cbd851..4fcd8717b7bb 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -5,6 +5,7 @@ if(NOT BUILD_HOST) pipeline_static.c component.c buffer.c + kpb.c ) if(CONFIG_COMP_VOLUME) add_local_sources(sof diff --git a/src/audio/Kconfig b/src/audio/Kconfig index 34a83efa00e8..9e485994b83d 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -54,4 +54,10 @@ config COMP_SWITCH help Select for Switch component + config COMP_KPB + bool "KPB component" + default y + help + Select for KPB component + endmenu diff --git a/src/audio/kpb.c b/src/audio/kpb.c new file mode 100644 index 000000000000..ff16af9b788a --- /dev/null +++ b/src/audio/kpb.c @@ -0,0 +1,530 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Marcin Rajwa + * + * A key phrase buffer component. + */ + +/** + * \file audio/kpb.c + * \brief Key phrase buffer component implementation + * \author Marcin Rajwa + */ + +#include +#include +#include +#include +#include +#include +#include + +static void kpb_event_handler(int message, void *cb_data, void *event_data); +static int kpb_register_client(struct comp_data *kpb, struct kpb_client *cli); +static void kpb_init_draining(struct comp_data *kpb, struct kpb_client *cli); +static void kpb_draining_task(void *arg); +static void kpb_buffer_data(struct comp_data *kpb, struct comp_buffer *source); + +/** + * \brief Create a key phrase buffer component. + * \param[in] comp - generic ipc component pointer. + * + * \return: a pointer to newly created KPB component. + */ +static struct comp_dev *kpb_new(struct sof_ipc_comp *comp) +{ + struct comp_dev *dev; + struct sof_ipc_comp_kpb *kpb; + struct sof_ipc_comp_kpb *ipc_kpb = (struct sof_ipc_comp_kpb *)comp; + struct comp_data *cd; + + trace_kpb("kpb_new()"); + + /* Validate input parameters */ + if (IPC_IS_SIZE_INVALID(ipc_kpb->config)) { + IPC_SIZE_ERROR_TRACE(TRACE_CLASS_KPB, ipc_kpb->config); + return NULL; + } + + if (ipc_kpb->no_channels > KPB_MAX_SUPPORTED_CHANNELS) { + trace_kpb_error("kpb_new() error: " + "no of channels exceeded the limit"); + return NULL; + } + + if (ipc_kpb->history_depth > KPB_MAX_BUFFER_SIZE) { + trace_kpb_error("kpb_new() error: " + "history depth exceeded the limit"); + return NULL; + } + + if (ipc_kpb->sampling_freq != KPB_SAMPLNG_FREQUENCY) { + trace_kpb_error("kpb_new() error: " + "requested sampling frequency not supported"); + return NULL; + } + + if (ipc_kpb->sampling_width != KPB_SAMPLING_WIDTH) { + trace_kpb_error("kpb_new() error: " + "requested sampling width not supported"); + return NULL; + } + + dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, + COMP_SIZE(struct sof_ipc_comp_kpb)); + if (!dev) + return NULL; + + kpb = (struct sof_ipc_comp_kpb *)&dev->comp; + memcpy(kpb, ipc_kpb, sizeof(struct sof_ipc_comp_kpb)); + + cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd)); + if (!cd) { + rfree(dev); + return NULL; + } + comp_set_drvdata(dev, cd); + dev->state = COMP_STATE_READY; + + return dev; +} + +/** + * \brief Reclaim memory of a key phrase buffer. + * \param[in] dev - component device pointer. + * + * \return none. + */ +static void kpb_free(struct comp_dev *dev) +{ + struct comp_data *kpb = comp_get_drvdata(dev); + + trace_kpb("kpb_free()"); + + /* reclaim internal buffer memory */ + rfree(kpb->his_buf_lp.sta_addr); + rfree(kpb->his_buf_lp.sta_addr); + /* reclaim device & component data memory */ + rfree(kpb); + rfree(dev); +} + +/** + * \brief Trigger a change of KPB state. + * \param[in] dev - component device pointer. + * \param[in] cmd - command type. + * \return none. + */ +static int kpb_trigger(struct comp_dev *dev, int cmd) +{ + trace_kpb("kpb_trigger()"); + + return comp_set_state(dev, cmd); +} + +/** + * \brief Prepare key phrase buffer. + * \param[in] dev - kpb component device pointer. + * + * \return integer representing either: + * 0 -> success + * -EINVAL -> failure. + */ +static int kpb_prepare(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int ret = 0; + int i; + + trace_kpb("kpb_prepare()"); + + ret = comp_set_state(dev, COMP_TRIGGER_PREPARE); + if (ret) + return ret; + + cd->no_of_clients = 0; + + /* allocate history_buffer/s */ +#if KPB_NO_OF_HISTORY_BUFFERS == 2 + + cd->his_buf_hp.sta_addr = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, + KPB_MAX_BUFFER_SIZE - LPSRAM_SIZE); + cd->his_buf_lp.sta_addr = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_LP, + LPSRAM_SIZE); + + if (!cd->his_buf_hp.sta_addr || !cd->his_buf_lp.sta_addr) { + trace_kpb_error("Failed to allocate space for " + "KPB bufefrs"); + return -ENOMEM; + } + + cd->his_buf_hp.end_addr = cd->his_buf_hp.sta_addr + + (KPB_MAX_BUFFER_SIZE - LPSRAM_SIZE); + + cd->his_buf_lp.end_addr = cd->his_buf_lp.sta_addr + + LPSRAM_SIZE; + +#elif KPB_NO_OF_HISTORY_BUFFERS == 1 + + cd->his_buf_hp.sta_addr = rballoc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, + KPB_MAX_BUFFER_SIZE); + + if (!cd->his_buf_hp.sta_addr) { + trace_kpb_error("Failed to allocate space for " + "KPB bufefrs"); + return -ENOMEM; + } + + cd->his_buf_hp.end_addr = cd->his_buf_hp.sta_addr + KPB_MAX_BUFFER_SIZE; + +#else +#error "Wrong number of key phrase buffers configured" +#endif + + /* TODO: zeroes both buffers */ + + /* initialize clients data */ + for (i = 0; i < KPB_MAX_NO_OF_CLIENTS; i++) { + cd->clients[i].state = KPB_CLIENT_UNREGISTERED; + cd->clients[i].r_ptr = NULL; + } + + /* initialize KPB events */ + cd->kpb_events.id = NOTIFIER_ID_KPB_CLIENT_EVT; + cd->kpb_events.cb_data = cd; + cd->kpb_events.cb = kpb_event_handler; + + /* register KPB for async notification */ + notifier_register(&cd->kpb_events); + + /* initialie draining task */ + schedule_task_init(&cd->draining_task, kpb_draining_task, cd); + + return ret; +} + +/** + * \brief Used to pass standard and bespoke commands (with data) to component. + * \param[in,out] dev - Volume base component device. + * \param[in] cmd - Command type. + * \param[in,out] data - Control command data. + * \return Error code. + */ +static int kpb_cmd(struct comp_dev *dev, int cmd, void *data, + int max_data_size) +{ + int ret = 0; + + return ret; +} + +static void kpb_cache(struct comp_dev *dev, int cmd) +{ + /* TODO: writeback history buffer */ +} + +static int kpb_reset(struct comp_dev *dev) +{ + /* TODO: what data of KPB should we reset here? */ + return 0; +} + +/** + * \brief Copy real time input stream into sink buffer, + * and in the same time buffers that input for + * later use by some of clients. + * + *\param[in] dev - kpb component device pointer. + * + * \return integer representing either: + * 0 - success + * -EINVAL - failure. + */ +static int kpb_copy(struct comp_dev *dev) +{ + int ret = 0; + int update_buffers = 0; + struct comp_data *kpb = comp_get_drvdata(dev); + struct comp_buffer *source; + struct comp_buffer *sink; + + tracev_kpb("kpb_copy()"); + + /* get source and sink buffers */ + source = list_first_item(&dev->bsource_list, struct comp_buffer, + sink_list); + sink = list_first_item(&dev->bsink_list, struct comp_buffer, + source_list); + /* process source data */ + /* check if there are valid pointers */ + if (source && sink) { + if (!source->r_ptr || !sink->w_ptr) { + return -EINVAL; + } else if (sink->free < kpb->sink_period_bytes) { + trace_kpb_error("kpb_copy() error: " + "sink component buffer" + " has not enough free bytes for copy"); + comp_overrun(dev, sink, kpb->sink_period_bytes, 0); + /* xrun */ + return -EIO; + } else if (source->avail < kpb->source_period_bytes) { + /* xrun */ + trace_kpb_error("kpb_copy() error: " + "source component buffer" + " has not enough data available"); + comp_underrun(dev, source, kpb->source_period_bytes, + 0); + return -EIO; + } else { + /* sink and source are both ready and have space */ + /* TODO: copy sink or source period data here? */ + memcpy(sink->w_ptr, source->r_ptr, + kpb->sink_period_bytes); + /* signal update source & sink data */ + update_buffers = 1; + } + /* buffer source data internally for future use by clients */ + if (source->avail <= KPB_MAX_BUFFER_SIZE) { + /* TODO: should we copy what is available or just + * a small portion of it? + */ + kpb_buffer_data(kpb, source); + } + } else { + ret = -EIO; + } + + if (update_buffers) { + comp_update_buffer_produce(sink, kpb->sink_period_bytes); + comp_update_buffer_consume(source, kpb->sink_period_bytes); + } + return ret; +} + +/** + * \brief Buffer real time data stream in + * the internal buffer. + * + * \param[in] kpb - KPB component data pointer. + * \param[in] source pointer to the buffer source. + * + * \return none + */ +static void kpb_buffer_data(struct comp_data *kpb, struct comp_buffer *source) +{ + trace_kpb("kpb_buffer_data()"); + int size_to_copy = kpb->source_period_bytes; + int space_avail; + struct history_buffer *hb; + +#if KPB_NO_OF_HISTORY_BUFFERS == 2 + + while (size_to_copy) { + hb = (kpb->his_buf_lp.state == KPB_BUFFER_FREE) ? + &kpb->his_buf_lp : &kpb->his_buf_hp; + space_avail = (int)hb->end_addr - (int)hb->w_ptr; + + if (size_to_copy > space_avail) { + memcpy(hb->w_ptr, source->r_ptr, space_avail); + size_to_copy = size_to_copy - space_avail; + hb->w_ptr = hb->sta_addr; + hb->state = KPB_BUFFER_FULL; + + if (hb->id == KPB_LP) + kpb->his_buf_hp.state = KPB_BUFFER_FREE; + else + kpb->his_buf_lp.state = KPB_BUFFER_FREE; + } else { + memcpy(hb->w_ptr, source->r_ptr, size_to_copy); + hb->w_ptr += size_to_copy; + size_to_copy = 0; + } + } +#elif KPB_NO_OF_HISTORY_BUFFERS == 1 + hb = &kpb->his_buf_hp; + space_avail = (int)hb->end_addr - (int)hb->w_ptr; + + if (size_to_copy > space_avail) { + /* We need to split copying into two parts + * and wrap buffer pointer + */ + memcpy(hb->w_ptr, source->r_ptr, space_avail); + size_to_copy = size_to_copy - space_avail; + hb->w_ptr = hb->sta_addr; + memcpy(hb->w_ptr, source->r_ptr, size_to_copy); + hb->w_ptr += size_to_copy; + size_to_copy = 0; + + } else { + memcpy(kpb->w_ptr, source->data_ptr, size_to_copy); + kpb->w_ptr += size_to_copy; + size_to_copy = 0; + } +#else +#error "Wrong number of key phrase buffers configured" +#endif +} + +/** + * \brief Main event dispatcher. + * \param[in] message - not used. + * \param[in] cb_data - KPB component internal data. + * \param[in] event_data - event specific data. + * \return none. + */ +static void kpb_event_handler(int message, void *cb_data, void *event_data) +{ + (void)message; + struct comp_data *kpb = (struct comp_data *)cb_data; + struct kpb_event_data *evd = (struct kpb_event_data *)event_data; + struct kpb_client *cli = (struct kpb_client *)evd->client_data; + + switch (evd->event_id) { + case KPB_EVENT_REGISTER_CLIENT: + kpb_register_client(kpb, cli); + break; + case KPB_EVENT_UNREGISTER_CLIENT: + /*TODO*/ + break; + case KPB_EVENT_BEGIN_DRAINING: + kpb_init_draining(kpb, cli); + break; + case KPB_EVENT_STOP_DRAINING: + /*TODO*/ + break; + default: + trace_kpb_error("kpb_cmd() error: " + "unsupported command"); + break; + } +} + +/** + * \brief Register clients in the system. + * + * \param[in] dev - 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; + + trace_kpb("kpb_register_client()"); + + if (!cli) { + trace_kpb_error("kpb_register_client() error: " + "no client data"); + return -EINVAL; + } + /* Do we have a room for a new client? */ + if (kpb->no_of_clients >= KPB_MAX_NO_OF_CLIENTS || + cli->id >= KPB_MAX_NO_OF_CLIENTS) { + trace_kpb_error("kpb_register_client() error: " + "no free room for client = %u ", + cli->id); + ret = -EINVAL; + } else if (kpb->clients[cli->id].state != KPB_CLIENT_UNREGISTERED) { + trace_kpb_error("kpb_register_client() error: " + "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].history_depth = cli->history_depth; + kpb->clients[cli->id].sink = cli->sink; + kpb->clients[cli->id].r_ptr = NULL; + kpb->clients[cli->id].state = KPB_CLIENT_BUFFERING; + kpb->no_of_clients++; + ret = 0; + } + + return ret; +} + +/** + * \brief Drain internal buffer into client's sink buffer. + * + * \param[in] kpb - kpb component data. + * \param[in] cli - client's data. + * + * \return integer representing either: + * 0 - success + * -EINVAL - failure. + */ +static void kpb_init_draining(struct comp_data *kpb, struct kpb_client *cli) +{ + uint8_t is_sink_ready = (kpb->clients[cli->id].sink->sink->state + == COMP_STATE_ACTIVE) ? 1 : 0; + + if (kpb->clients[cli->id].state == KPB_CLIENT_UNREGISTERED) { + /* TODO: possibly move this check to draining task + * the doubt is if HOST managed to change the sink state + * at notofication time + */ + trace_kpb_error("kpb_init_draining() error: " + "requested draining for unregistered client"); + } else if (!is_sink_ready) { + trace_kpb_error("kpb_init_draining() error: " + "sink not ready for draining"); + } else { + /* add one-time draining task into the scheduler */ + schedule_task(&kpb->draining_task, 0, 0); + } +} + +static void kpb_draining_task(void *arg) +{ + /* TODO: while loop drainning history buffer accoriding to + * clients request + */ +} + +struct comp_driver comp_kpb = { + .type = SOF_COMP_KPB, + .ops = { + .new = kpb_new, + .free = kpb_free, + .cmd = kpb_cmd, + .trigger = kpb_trigger, + .copy = kpb_copy, + .prepare = kpb_prepare, + .reset = kpb_reset, + .cache = kpb_cache, + .params = NULL, + }, +}; + +void sys_comp_kpb_init(void) +{ + comp_register(&comp_kpb); +} diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index c69e0db31c30..b7c92122d320 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -541,7 +541,11 @@ void sys_comp_eq_fir_init(void); #else static inline void sys_comp_eq_fir_init(void) {} #endif - +#ifdef CONFIG_COMP_KPB +void sys_comp_kpb_init(void); +#else +static inline void sys_comp_kpb_init(void) {} +#endif /** @}*/ /** \name Helpers. diff --git a/src/include/sof/audio/kpb.h b/src/include/sof/audio/kpb.h new file mode 100644 index 000000000000..8eb70ae93ada --- /dev/null +++ b/src/include/sof/audio/kpb.h @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Marcin Rajwa + * + */ + +#ifndef __INCLUDE_AUDIO_KPB_H__ +#define __INCLUDE_AUDIO_KPB_H__ + +#include +#include +#include +#include + +/* KPB tracing */ +#define trace_kpb(__e, ...) trace_event(TRACE_CLASS_KPB, __e, ##__VA_ARGS__) +#define trace_kpb_error(__e, ...) (trace_error(TRACE_CLASS_KPB, __e, \ + ##__VA_ARGS__)) +#define tracev_kpb(__e, ...) tracev_event(TRACE_CLASS_KPB, __e, ##__VA_ARGS__) +/* KPB internal defines */ +#define KPB_MAX_BUFF_TIME 2100 /**< time of buffering in miliseconds */ +#define KPB_MAX_SUPPORTED_CHANNELS 2 +#define KPB_SAMPLING_WIDTH 16 /**< number of bits */ +#define KPB_SAMPLNG_FREQUENCY 16000 /* max sampling frequency in Hz */ +#define KPB_NR_OF_CHANNELS 2 +#define KPB_SAMPLE_CONTAINER_SIZE ((KPB_SAMPLING_WIDTH == 32) ? 64 : 32) +#define KPB_MAX_BUFFER_SIZE ((KPB_SAMPLNG_FREQUENCY / 1000) * \ + (KPB_SAMPLE_CONTAINER_SIZE / 8) * KPB_MAX_BUFF_TIME * \ + KPB_NR_OF_CHANNELS) +#define KPB_MAX_NO_OF_CLIENTS 2 +#define KPB_NO_OF_HISTORY_BUFFERS 2 /**< no of internal buffers */ + +enum kpb_sink_state { + KPB_SINK_BUSY = 0, + KPB_SINK_READY, +}; + +struct kpb_sink { + uint32_t *data_ptr; + uint32_t data_size; + enum kpb_sink_state state; +}; + +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, + KPB_CLIENT_DRAINNING, + KPB_CLIENT_DRAINNING_OD, /**< draining on demand */ +}; + +struct kpb_client { + uint8_t id; /**< id associated with output sink */ + uint32_t history_depth; /**< normalized value of buffered bytes */ + uint32_t history_begin; /**< place where key phrase begins */ + uint32_t history_end; /**< place where key phrase ends */ + enum kpb_client_state state; /**< current state of a client */ + void *r_ptr; /**< current read position */ + struct comp_buffer *sink; /**< client's sink */ +}; + +enum buffer_state { + KPB_BUFFER_FREE = 0, + KPB_BUFFER_FULL, + KPB_BUFFER_OFF, +}; + +enum kpb_id { + KPB_LP = 0, + KPB_HP, +}; + +struct history_buffer { + enum kpb_id id; + enum buffer_state state; + void *w_ptr; /**< buffer write pointer */ + void *r_ptr; /**< buffer read pointer */ + void *sta_addr; + void *end_addr; /**< buffer end address */ + +}; + +/*! Key phrase buffer component */ +struct comp_data { + /* runtime data */ + uint8_t no_of_clients; /**< number of registered clients */ + struct kpb_client clients[KPB_MAX_NO_OF_CLIENTS]; + struct history_buffer his_buf_lp; + struct history_buffer his_buf_hp; + struct notifier kpb_events; /**< KPB events object */ + struct task draining_task; + uint32_t source_period_bytes; /**< source number of period bytes */ + uint32_t sink_period_bytes; /**< sink number of period bytes */ + +}; + +#endif diff --git a/src/include/sof/list.h b/src/include/sof/list.h index 8a90b771e70a..1cada86fe742 100644 --- a/src/include/sof/list.h +++ b/src/include/sof/list.h @@ -121,5 +121,4 @@ static inline int list_item_is_last(struct list_item *item, for (item = (list)->next, tmp = item->next;\ item != (list); \ item = tmp, tmp = item->next) - #endif diff --git a/src/include/sof/notifier.h b/src/include/sof/notifier.h index 57d8408775fb..67744d9757bb 100644 --- a/src/include/sof/notifier.h +++ b/src/include/sof/notifier.h @@ -37,21 +37,23 @@ struct sof; -/* notifier general IDs */ -#define NOTIFIER_ID_CPU_FREQ 0 -#define NOTIFIER_ID_SSP_FREQ 1 - /* notifier target core masks */ #define NOTIFIER_TARGET_CORE_MASK(x) (1 << x) #define NOTIFIER_TARGET_CORE_ALL_MASK 0xFFFFFFFF +enum notify_id { + NOTIFIER_ID_CPU_FREQ = 0, + NOTIFIER_ID_SSP_FREQ, + NOTIFIER_ID_KPB_CLIENT_EVT, +}; + struct notify { spinlock_t lock; /* notifier lock */ struct list_item list; /* list of notifiers */ }; struct notify_data { - uint32_t id; + enum notify_id id; uint32_t message; uint32_t target_core_mask; uint32_t data_size; @@ -59,7 +61,7 @@ struct notify_data { }; struct notifier { - uint32_t id; + enum notify_id id; struct list_item list; void *cb_data; void (*cb)(int message, void *cb_data, void *event_data); diff --git a/src/include/sof/trace.h b/src/include/sof/trace.h index 708789d4058b..43b4978274fe 100644 --- a/src/include/sof/trace.h +++ b/src/include/sof/trace.h @@ -110,6 +110,7 @@ #define TRACE_CLASS_CPU (25 << 24) #define TRACE_CLASS_CLK (26 << 24) #define TRACE_CLASS_SCHEDULE (27 << 24) +#define TRACE_CLASS_KPB (28 << 24) /* move to config.h */ #define TRACE 1 diff --git a/src/include/uapi/ipc/topology.h b/src/include/uapi/ipc/topology.h index c85283151813..0e26b14cb4ab 100644 --- a/src/include/uapi/ipc/topology.h +++ b/src/include/uapi/ipc/topology.h @@ -64,6 +64,7 @@ enum sof_comp_type { SOF_COMP_EQ_FIR, SOF_COMP_FILEREAD, /**< host test based file IO */ SOF_COMP_FILEWRITE, /**< host test based file IO */ + SOF_COMP_KPB, /* A key phrase buffer component */ }; /* XRUN action for component */ @@ -283,4 +284,16 @@ struct sof_ipc_pipe_comp_connect { uint32_t sink_id; } __attribute__((packed)); +/* create new component kpb - SOF_IPC_TPLG_KPB_NEW */ +struct sof_ipc_comp_kpb { + struct sof_ipc_comp comp; + struct sof_ipc_comp_config config; + uint32_t size; /**< kpb size in bytes */ + uint32_t caps; /**< SOF_MEM_CAPS_ */ + uint8_t no_channels; /**< no of channels */ + uint32_t history_depth; /**< time of buffering in milliseconds */ + uint32_t sampling_freq; /**< frequency in hertz */ + uint8_t sampling_width; /**< number of bits */ +} __attribute__((packed)); + #endif diff --git a/src/platform/apollolake/include/platform/platcfg.h b/src/platform/apollolake/include/platform/platcfg.h index 2e63b7349068..3252f5af572a 100644 --- a/src/platform/apollolake/include/platform/platcfg.h +++ b/src/platform/apollolake/include/platform/platcfg.h @@ -47,5 +47,7 @@ #define HPSRAM_MASK(ignored) ((1 << PLATFORM_HPSRAM_EBB_COUNT) - 1) #define LPSRAM_MASK(ignored) ((1 << PLATFORM_LPSRAM_EBB_COUNT) - 1) +#define LPSRAM_BANK_SIZE (64 * 1024) +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) #endif diff --git a/src/platform/baytrail/include/platform/platcfg.h b/src/platform/baytrail/include/platform/platcfg.h index d41be5a02649..9aa0d6299e2f 100644 --- a/src/platform/baytrail/include/platform/platcfg.h +++ b/src/platform/baytrail/include/platform/platcfg.h @@ -35,4 +35,10 @@ #define PLATFORM_MASTER_CORE_ID 0 +#define PLATFORM_LPSRAM_EBB_COUNT 1 + +#define LPSRAM_BANK_SIZE (64 * 1024) + +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) + #endif diff --git a/src/platform/cannonlake/include/platform/platcfg.h b/src/platform/cannonlake/include/platform/platcfg.h index 986e7903e49f..0802d45a03e9 100644 --- a/src/platform/cannonlake/include/platform/platcfg.h +++ b/src/platform/cannonlake/include/platform/platcfg.h @@ -55,5 +55,7 @@ #define LPSRAM_MASK ((1 << PLATFORM_LPSRAM_EBB_COUNT) - 1) #define MAX_MEMORY_SEGMENTS ((PLATFORM_HPSRAM_EBB_COUNT + \ MAX_EBB_BANKS_IN_SEGMENT - 1) / MAX_EBB_BANKS_IN_SEGMENT) +#define LPSRAM_BANK_SIZE (64 * 1024) +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) #endif diff --git a/src/platform/haswell/include/platform/platcfg.h b/src/platform/haswell/include/platform/platcfg.h index d41be5a02649..41ea07fcaba0 100644 --- a/src/platform/haswell/include/platform/platcfg.h +++ b/src/platform/haswell/include/platform/platcfg.h @@ -34,5 +34,10 @@ #define PLATFORM_CORE_COUNT 1 #define PLATFORM_MASTER_CORE_ID 0 +#define PLATFORM_LPSRAM_EBB_COUNT 1 + +#define LPSRAM_BANK_SIZE (64 * 1024) + +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) #endif diff --git a/src/platform/icelake/include/platform/platcfg.h b/src/platform/icelake/include/platform/platcfg.h index f455ff20331c..b33ed27e992a 100644 --- a/src/platform/icelake/include/platform/platcfg.h +++ b/src/platform/icelake/include/platform/platcfg.h @@ -37,4 +37,10 @@ #define PLATFORM_MASTER_CORE_ID 0 +#define PLATFORM_LPSRAM_EBB_COUNT 1 + +#define LPSRAM_BANK_SIZE (64 * 1024) + +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) + #endif diff --git a/src/platform/suecreek/include/platform/platcfg.h b/src/platform/suecreek/include/platform/platcfg.h index f455ff20331c..b5622fbae71b 100644 --- a/src/platform/suecreek/include/platform/platcfg.h +++ b/src/platform/suecreek/include/platform/platcfg.h @@ -36,5 +36,10 @@ #define PLATFORM_CORE_COUNT 4 #define PLATFORM_MASTER_CORE_ID 0 +#define PLATFORM_LPSRAM_EBB_COUNT 1 + +#define LPSRAM_BANK_SIZE (64 * 1024) + +#define LPSRAM_SIZE (PLATFORM_LPSRAM_EBB_COUNT * LPSRAM_BANK_SIZE) #endif diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index 4e9fd26650f6..179a3332fd56 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -7,3 +7,6 @@ add_subdirectory(pipeline) if(CONFIG_COMP_VOLUME) add_subdirectory(volume) endif() +if(CONFIG_COMP_VOLUME) + add_subdirectory(kpb) +endif() \ No newline at end of file diff --git a/test/cmocka/src/audio/kpb/CMakeLists.txt b/test/cmocka/src/audio/kpb/CMakeLists.txt new file mode 100644 index 000000000000..e8524a39ef2b --- /dev/null +++ b/test/cmocka/src/audio/kpb/CMakeLists.txt @@ -0,0 +1,8 @@ +cmocka_test(kpb + kpb_buffer.c + kpb_mock.c + ${PROJECT_SOURCE_DIR}/src/audio/kpb.c + ${PROJECT_SOURCE_DIR}/src/audio/buffer.c + #${PROJECT_SOURCE_DIR}/src/audio/component.c +) +target_link_libraries(kpb PRIVATE -lm) diff --git a/test/cmocka/src/audio/kpb/kpb_buffer.c b/test/cmocka/src/audio/kpb/kpb_buffer.c new file mode 100644 index 000000000000..908ae57349c1 --- /dev/null +++ b/test/cmocka/src/audio/kpb/kpb_buffer.c @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Marcin Rajwa + * + * Test KPB internal buffering mechanism. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "kpb_mock.h" +#include + +/* local data types */ +enum kpb_test_type { + KPB_TEST_COPY_TWO_BUFFERS = 0, + KPB_TEST_COPY_ONE_BUFFER, +}; + +enum kpb_test_buff_type { + KPB_SOURCE_BUFFER = 0, + KPB_SINK_BUFFER, +}; + +struct test_case { + enum kpb_test_type type; + int no_buffers; + int lp_buff_size; + int hp_buff_size; + int source_period_bytes; + int sink_period_bytes; +}; + +/* dummy component driver & device */ +struct comp_driver kpb_drv_mock; +struct comp_dev *kpb_dev_mock; +struct comp_buffer *source; +struct comp_buffer *sink; + +/* dummy memory buffers and data pointers */ +void *source_data; +void *sink_data; +void *his_buf_lp; +void *his_buf_hp; + +/* local function declarations */ +static struct comp_buffer *mock_comp_buffer(void **state, + enum kpb_test_buff_type buff_type); +static struct comp_data *mock_comp_kpb(void **state, void *hp_buf, + void *lp_buf); +static struct comp_dev *mock_comp_dev(void); + +static int buffering_test_setup(void **state) +{ + struct test_case *test_case_data = (struct test_case *)*state; + int lp_buff_size = test_case_data->lp_buff_size; + int hp_buff_size = test_case_data->hp_buff_size; + int entire_buff_size = hp_buff_size + lp_buff_size; + /* allocate memory for components */ + if (test_case_data->no_buffers == 2) { + his_buf_lp = test_malloc(lp_buff_size); + his_buf_hp = test_malloc(hp_buff_size); + } else if (test_case_data->type == KPB_TEST_COPY_ONE_BUFFER) { + his_buf_hp = test_malloc(hp_buff_size); + } else { + return -1; + } + + source_data = test_malloc(entire_buff_size); + sink_data = test_malloc(entire_buff_size); + kpb_dev_mock = mock_comp_dev(); + + /* create mocks */ + kpb_dev_mock->private = mock_comp_kpb(state, his_buf_hp, his_buf_lp); + source = mock_comp_buffer(state, KPB_SOURCE_BUFFER); + sink = mock_comp_buffer(state, KPB_SINK_BUFFER); + + /* mount coponents for test */ + source->source = kpb_dev_mock; + source->sink = kpb_dev_mock; + source->r_ptr = source_data; + sink->w_ptr = sink_data; + kpb_dev_mock->bsource_list.next = &source->sink_list; + kpb_dev_mock->bsink_list.next = &sink->source_list; + + return 0; +} + +static int buffering_test_teardown(void **state) +{ + /* free components memory */ + struct test_case *test_case_data = (struct test_case *)*state; + struct comp_data *cd = comp_get_drvdata(kpb_dev_mock); + + test_free(cd); + test_free(source_data); + test_free(sink_data); + test_free(source); + test_free(sink); + + if (test_case_data->type == KPB_TEST_COPY_TWO_BUFFERS) { + test_free(his_buf_lp); + test_free(his_buf_hp); + } else if (test_case_data->type == KPB_TEST_COPY_ONE_BUFFER) { + test_free(his_buf_hp); + } else { + return -1; + } + + test_free(kpb_dev_mock); + return 0; +} + +static struct comp_dev *mock_comp_dev(void) +{ + struct comp_dev *dev = test_malloc(sizeof(struct comp_dev)); + + dev->is_dma_connected = 0; + return dev; +} + +static struct comp_data *mock_comp_kpb(void **state, void *hp_buf, + void *lp_buf) +{ + struct comp_data *kpb = test_malloc(sizeof(struct comp_data)); + struct test_case *test_case_data = (struct test_case *)*state; + + kpb->source_period_bytes = test_case_data->source_period_bytes; + kpb->sink_period_bytes = test_case_data->sink_period_bytes; + + if (test_case_data->type == KPB_TEST_COPY_TWO_BUFFERS) { + int lp_buff_size = test_case_data->lp_buff_size; + int hp_buff_size = test_case_data->hp_buff_size; + /* LPSRAM buffer init */ + kpb->his_buf_lp.id = KPB_LP; + kpb->his_buf_lp.state = KPB_BUFFER_FREE; + kpb->his_buf_lp.w_ptr = lp_buf; + kpb->his_buf_lp.end_addr = kpb->his_buf_lp.w_ptr + lp_buff_size; + kpb->his_buf_lp.sta_addr = lp_buf; + /* HPSRAM buffer init */ + kpb->his_buf_hp.id = KPB_HP; + kpb->his_buf_hp.state = KPB_BUFFER_FREE; + kpb->his_buf_hp.w_ptr = hp_buf; + kpb->his_buf_hp.end_addr = kpb->his_buf_hp.w_ptr + hp_buff_size; + kpb->his_buf_hp.sta_addr = hp_buf; + } else if (test_case_data->type == KPB_TEST_COPY_ONE_BUFFER) { + int hp_buff_size = test_case_data->hp_buff_size; + /* HPSRAM buffer init only */ + kpb->his_buf_hp.id = KPB_HP; + kpb->his_buf_hp.state = KPB_BUFFER_FREE; + kpb->his_buf_hp.w_ptr = hp_buf; + kpb->his_buf_hp.end_addr = kpb->his_buf_hp.w_ptr + hp_buff_size; + kpb->his_buf_hp.sta_addr = hp_buf; + } else { + return NULL; + } + + return kpb; +} + +static struct comp_buffer *mock_comp_buffer(void **state, + enum kpb_test_buff_type buff_type) +{ + struct test_case *test_case_data = (struct test_case *)*state; + struct comp_buffer *buffer = test_malloc(sizeof(struct comp_buffer)); + + buffer->size = test_case_data->lp_buff_size + + test_case_data->hp_buff_size; + + switch (buff_type) { + case KPB_SOURCE_BUFFER: + buffer->avail = buffer->size; + buffer->w_ptr = &buffer->w_ptr; /* setup to NULL */ + buffer->addr = buffer->r_ptr; + buffer->end_addr = buffer->r_ptr + buffer->size; + break; + case KPB_SINK_BUFFER: + buffer->free = buffer->size; + buffer->r_ptr = &buffer->r_ptr; + buffer->addr = buffer->w_ptr; + buffer->end_addr = buffer->w_ptr + buffer->size; + break; + } + return buffer; +} + +/* Mocking comp_register here so we can register our components properly */ +int comp_register(struct comp_driver *drv) +{ + void *dst; + + switch (drv->type) { + case SOF_COMP_KPB: + dst = &kpb_drv_mock; + memcpy(dst, drv, sizeof(struct comp_driver)); + break; + default: + return -1; + } + return 0; +} + +/* A test case that copies real time stream into KPB internal buffer + * and to real time sink. + */ +static void kpb_test_buffer_real_time_stream(void **state) +{ + int i, ret = 0; + struct test_case *test_case_data = (struct test_case *)*state; + int buffer_size = test_case_data->hp_buff_size + + test_case_data->lp_buff_size; + char pattern = 0xAA; + struct comp_buffer *source_test; + struct comp_buffer *sink_test; + char *r_ptr; + /* register KPB component to use its internal functions */ + sys_comp_kpb_init(); + assert_int_equal(kpb_drv_mock.type, SOF_COMP_KPB); + assert_non_null(kpb_drv_mock.ops.copy); + + source_test = list_first_item(&kpb_dev_mock->bsource_list, + struct comp_buffer, + sink_list); + sink_test = list_first_item(&kpb_dev_mock->bsink_list, + struct comp_buffer, + source_list); + assert_ptr_equal(source, source_test); + assert_ptr_equal(sink, sink_test); + + /* fiil source buffer with test data */ + r_ptr = (char *)source_test->r_ptr; + for (i = 0; i < buffer_size; i++) + (*r_ptr++) = pattern; + + /* perform kpb_copy test */ + ret = kpb_drv_mock.ops.copy(kpb_dev_mock); + + assert_int_equal(ret, 0); + /* verify if source was copied to sink */ + assert_memory_equal(source_data, + sink_data, + test_case_data->sink_period_bytes); + + if (test_case_data->type == KPB_TEST_COPY_TWO_BUFFERS) { + /* verify if LPSRAM internal buffer was filled properly */ + assert_memory_equal(source_data, + his_buf_lp, + test_case_data->lp_buff_size); + /* verify if HPSRAM internal buffer was filled properly */ + assert_memory_equal(source_data, + his_buf_hp, + test_case_data->hp_buff_size); + } else if (test_case_data->type == KPB_TEST_COPY_ONE_BUFFER) { + /* verify if HPSRAM internal buffer was filled properly */ + assert_memory_equal(source_data, + his_buf_hp, + test_case_data->hp_buff_size); + } +} + +/* always successful test */ +static void null_test_success(void **state) +{ + (void)state; +} + +/* test main function */ +int main(void) +{ + struct CMUnitTest tests[3]; + struct test_case internal_double_buffering = { + .type = KPB_TEST_COPY_TWO_BUFFERS, + .no_buffers = 2, + .lp_buff_size = 100, + .hp_buff_size = 20, + .source_period_bytes = 120, + .sink_period_bytes = 120, + }; + struct test_case internal_single_buffering = { + .type = KPB_TEST_COPY_ONE_BUFFER, + .no_buffers = 1, + .lp_buff_size = 0, + .hp_buff_size = 120, + .source_period_bytes = 120, + .sink_period_bytes = 120, + }; + + tests[0].name = "dummy, always successful test"; + tests[0].test_func = null_test_success; + tests[0].initial_state = NULL; + tests[0].setup_func = NULL; + tests[0].teardown_func = NULL; + + tests[1].name = "KPB real time copy and buffering (Double Buffer)"; + tests[1].test_func = kpb_test_buffer_real_time_stream; + tests[1].initial_state = &internal_double_buffering; + tests[1].setup_func = buffering_test_setup; + tests[1].teardown_func = buffering_test_teardown; + + tests[2].name = "KPB real time copy and buffering (Single Buffer)"; + tests[2].test_func = kpb_test_buffer_real_time_stream; + tests[2].initial_state = &internal_single_buffering; + tests[2].setup_func = buffering_test_setup; + tests[2].teardown_func = buffering_test_teardown; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/src/audio/kpb/kpb_mock.c b/test/cmocka/src/audio/kpb/kpb_mock.c new file mode 100644 index 000000000000..ea66bfcec6a9 --- /dev/null +++ b/test/cmocka/src/audio/kpb/kpb_mock.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include +#include +#include + +TRACE_IMPL() + +void *rballoc(int zone, uint32_t caps, size_t bytes) +{ + (void)zone; + (void)caps; + + return malloc(bytes); +} + +void *rzalloc(int zone, uint32_t caps, size_t bytes) +{ + (void)zone; + (void)caps; + + return calloc(bytes, 1); +} + +void rfree(void *ptr) +{ + free(ptr); +} + +void pipeline_xrun(struct pipeline *p, struct comp_dev *dev, int32_t bytes) +{ +} + +int comp_set_state(struct comp_dev *dev, int cmd) +{ + return 0; +} + +void work_schedule_default(struct work *w, uint64_t timeout) +{ +} + +void notifier_register(struct notifier *notifier) +{ +} + +void schedule_task(struct task *task, uint64_t start, uint64_t deadline) +{ +} diff --git a/test/cmocka/src/audio/kpb/kpb_mock.h b/test/cmocka/src/audio/kpb/kpb_mock.h new file mode 100644 index 000000000000..0a186a1dd3b3 --- /dev/null +++ b/test/cmocka/src/audio/kpb/kpb_mock.h @@ -0,0 +1 @@ +int comp_set_state(struct comp_dev *dev, int cmd);