1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019 Intel Corporation. All rights reserved.
4  *
5  * Author: Marcin Rajwa <marcin.rajwa@linux.intel.com>
6  */
7 
8 #ifndef __SOF_AUDIO_KPB_H__
9 #define __SOF_AUDIO_KPB_H__
10 
11 #include <sof/trace/trace.h>
12 #include <user/trace.h>
13 #include <stdint.h>
14 
15 #if defined(__XCC__)
16 
17 #include <xtensa/config/core-isa.h>
18 #if XCHAL_HAVE_HIFI3 || XCHAL_HAVE_HIFI4
19 #define KPB_HIFI3
20 #endif
21 
22 #endif
23 struct comp_buffer;
24 
25 /* KPB internal defines */
26 
27 #if CONFIG_TIGERLAKE
28 #define KPB_MAX_BUFF_TIME 3000 /**< time of buffering in miliseconds */
29 #define HOST_WAKEUP_TIME 1000 /* aprox. time of host DMA wakup from suspend [ms] */
30 #else
31 /** Due to memory constraints on non-TGL platforms, the buffers are smaller. */
32 #define KPB_MAX_BUFF_TIME 2100 /**< time of buffering in miliseconds */
33 #define HOST_WAKEUP_TIME 0 /* aprox. time of host DMA wakup from suspend [ms] */
34 #endif
35 
36 #define KPB_MAX_DRAINING_REQ (KPB_MAX_BUFF_TIME - HOST_WAKEUP_TIME)
37 #define KPB_MAX_SUPPORTED_CHANNELS 6 /**< number of supported channels */
38 /**< number of samples taken each milisecond */
39 #define	KPB_SAMPLES_PER_MS (KPB_SAMPLNG_FREQUENCY / 1000)
40 #define	KPB_SAMPLNG_FREQUENCY 16000 /**< supported sampling frequency in Hz */
41 #define KPB_SAMPLE_CONTAINER_SIZE(sw) ((sw == 16) ? 16 : 32)
42 #define KPB_MAX_BUFFER_SIZE(sw, channels_number) ((KPB_SAMPLNG_FREQUENCY / 1000) * \
43 	(KPB_SAMPLE_CONTAINER_SIZE(sw) / 8) * KPB_MAX_BUFF_TIME * \
44 	 (channels_number))
45 #define KPB_MAX_NO_OF_CLIENTS 2
46 #define KPB_NO_OF_HISTORY_BUFFERS 2 /**< no of internal buffers */
47 #define KPB_ALLOCATION_STEP 0x100
48 #define KPB_NO_OF_MEM_POOLS 3
49 #define KPB_BYTES_TO_FRAMES(bytes, sample_width, channels_number) \
50 	((bytes) / ((KPB_SAMPLE_CONTAINER_SIZE(sample_width) / 8) * \
51 	 (channels_number)))
52 /**< Defines how much faster draining is in comparison to pipeline copy. */
53 #define KPB_DRAIN_NUM_OF_PPL_PERIODS_AT_ONCE 2
54 /**< Host buffer shall be at least two times bigger than history buffer. */
55 #define HOST_BUFFER_MIN_SIZE(hb, channels_number) ((hb) * (channels_number))
56 
57 /**< Convert with right shift a bytes count to samples count */
58 #define KPB_BYTES_TO_S16_SAMPLES(s)	((s) >> 1)
59 #define KPB_BYTES_TO_S32_SAMPLES(s)	((s) >> 2)
60 
61 /* Macro that returns mask with selected bits set */
62 #define KPB_COUNT_TO_BITMASK(cnt) (((0x1 << (cnt)) - 1))
63 #define KPB_IS_BIT_SET(value, idx) ((value) & (1 << (idx)))
64 #define KPB_REFERENCE_SUPPORT_CHANNELS 2
65 /* Maximum number of channels in the micsel mask is 4,
66  * i.e. number of max supported channels - reference channels)
67  */
68 #define KPB_MAX_MICSEL_CHANNELS 4
69 
70 /** All states below as well as relations between them are documented in
71  * the sof-dosc in [kpbm-state-diagram]
72  * Therefore any addition of new states or modification of existing ones
73  * should have a corresponding update in the sof-docs.
74  * [kpbm-state-diagram]:
75 https://thesofproject.github.io/latest/developer_guides/firmware/kd_integration/kd-integration.html#kpbm-state-diagram
76 "Keyphrase buffer manager state diagram"
77  */
78 enum kpb_state {
79 	KPB_STATE_DISABLED = 0,
80 	KPB_STATE_RESET_FINISHING,
81 	KPB_STATE_CREATED,
82 	KPB_STATE_PREPARING,
83 	KPB_STATE_RUN,
84 	KPB_STATE_BUFFERING,
85 	KPB_STATE_INIT_DRAINING,
86 	KPB_STATE_DRAINING,
87 	KPB_STATE_HOST_COPY,
88 	KPB_STATE_RESETTING,
89 };
90 
91 enum kpb_event {
92 	KPB_EVENT_REGISTER_CLIENT = 0,
93 	KPB_EVENT_UPDATE_PARAMS,
94 	KPB_EVENT_BEGIN_DRAINING,
95 	KPB_EVENT_STOP_DRAINING,
96 	KPB_EVENT_UNREGISTER_CLIENT,
97 };
98 
99 struct kpb_event_data {
100 	enum kpb_event event_id;
101 	struct kpb_client *client_data;
102 };
103 
104 enum kpb_client_state {
105 	KPB_CLIENT_UNREGISTERED = 0,
106 	KPB_CLIENT_BUFFERING,
107 	KPB_CLIENT_DRAINNING,
108 	KPB_CLIENT_DRAINNING_OD, /**< draining on demand */
109 };
110 
111 struct kpb_client {
112 	uint8_t id; /**< id associated with output sink */
113 	uint32_t drain_req; /**< normalized value of buffered bytes */
114 	enum kpb_client_state state; /**< current state of a client */
115 	void *r_ptr; /**< current read position */
116 	struct comp_buffer *sink; /**< client's sink */
117 };
118 
119 enum buffer_state {
120 	KPB_BUFFER_FREE = 0,
121 	KPB_BUFFER_FULL,
122 	KPB_BUFFER_OFF,
123 };
124 
125 enum kpb_id {
126 	KPB_LP = 0,
127 	KPB_HP,
128 };
129 
130 struct history_buffer {
131 	enum buffer_state state; /**< state of the buffer */
132 	void *start_addr; /**< buffer start address */
133 	void *end_addr; /**< buffer end address */
134 	void *w_ptr; /**< buffer write pointer */
135 	void *r_ptr; /**< buffer read pointer */
136 	struct history_buffer *next; /**< next history buffer */
137 	struct history_buffer *prev; /**< next history buffer */
138 };
139 
140 /* Draining task data */
141 struct draining_data {
142 	struct comp_buffer *sink;
143 	struct history_buffer *hb;
144 	size_t drain_req;
145 	uint8_t is_draining_active;
146 	size_t sample_width;
147 	size_t buffered_while_draining;
148 	size_t drain_interval;
149 	size_t pb_limit; /**< Period bytes limit */
150 	struct comp_dev *dev;
151 	bool sync_mode_on;
152 	enum comp_copy_type copy_type;
153 };
154 
155 struct history_data {
156 	size_t buffer_size; /**< size of internal history buffer */
157 	size_t buffered; /**< amount of buffered data */
158 	size_t free; /** spce we can use to write new data */
159 	struct history_buffer *c_hb; /**< current buffer used for writing */
160 };
161 
162 enum ipc4_kpb_module_config_params {
163 	/* Mic selector for client - sets microphone id for real time sink mic selector
164 	 * IPC4-compatible ID - please do not change the number
165 	 */
166 	KP_BUF_CLIENT_MIC_SELECT = 11,
167 };
168 
169 /* Stores KPB mic selector config */
170 struct kpb_micselector_config {
171 	/* channel bit set to 1 implies channel selection */
172 	uint32_t mask;
173 };
174 #ifdef UNIT_TEST
175 void sys_comp_kpb_init(void);
176 #endif
177 
178 #endif /* __SOF_AUDIO_KPB_H__ */
179