forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkpb.c
More file actions
2606 lines (2288 loc) · 73.2 KB
/
kpb.c
File metadata and controls
2606 lines (2288 loc) · 73.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Marcin Rajwa <marcin.rajwa@linux.intel.com>
/*
* A key phrase buffer component.
*/
/**
* \file audio/kpb.c
* \brief Key phrase buffer component implementation
* \author Marcin Rajwa <marcin.rajwa@linux.intel.com>
*/
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/kpb.h>
#include <sof/audio/ipc-config.h>
#include <sof/common.h>
#include <rtos/panic.h>
#include <sof/ipc/msg.h>
#include <rtos/timer.h>
#include <rtos/alloc.h>
#include <rtos/clk.h>
#include <rtos/init.h>
#include <sof/lib/memory.h>
#include <sof/lib/pm_runtime.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/math/numbers.h>
#include <sof/platform.h>
#include <sof/schedule/edf_schedule.h>
#include <sof/schedule/schedule.h>
#include <rtos/task.h>
#include <rtos/string.h>
#include <sof/ut.h>
#include <ipc/topology.h>
#include <ipc4/module.h>
#include <ipc4/kpb.h>
#include <user/kpb.h>
#include <user/trace.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#if CONFIG_AMS
#include <sof/lib/ams.h>
#include <sof/lib/ams_msg.h>
#include <ipc4/ams_helpers.h>
#else
#include <sof/lib/notifier.h>
#endif
static const struct comp_driver comp_kpb;
LOG_MODULE_REGISTER(kpb, CONFIG_SOF_LOG_LEVEL);
#if CONFIG_IPC_MAJOR_4
/* A8A0CB32-4A77-4DB1-85C7-53D7EE07BCE6 */
DECLARE_SOF_RT_UUID("kpb", kpb_uuid, 0xA8A0CB32, 0x4A77, 0x4DB1,
0x85, 0xC7, 0x53, 0xD7, 0xEE, 0x07, 0xBC, 0xE6);
#else
/* d8218443-5ff3-4a4c-b388-6cfe07b9562e */
DECLARE_SOF_RT_UUID("kpb", kpb_uuid, 0xd8218443, 0x5ff3, 0x4a4c,
0xb3, 0x88, 0x6c, 0xfe, 0x07, 0xb9, 0x56, 0x2e);
#endif
DECLARE_TR_CTX(kpb_tr, SOF_UUID(kpb_uuid), LOG_LEVEL_INFO);
/* e50057a5-8b27-4db4-bd79-9a639cee5f50 */
DECLARE_SOF_UUID("kpb-task", kpb_task_uuid, 0xe50057a5, 0x8b27, 0x4db4,
0xbd, 0x79, 0x9a, 0x63, 0x9c, 0xee, 0x5f, 0x50);
/* KPB private data, runtime data */
struct comp_data {
enum kpb_state state; /**< current state of KPB component */
uint32_t state_log; /**< keeps record of KPB recent states */
#ifndef __ZEPHYR__
struct k_spinlock lock; /**< locking mechanism for read pointer calculations */
k_spinlock_key_t key;
#else
struct k_mutex lock;
#endif
struct sof_kpb_config config; /**< component configuration data */
struct history_data hd; /** data related to history buffer */
struct task draining_task;
struct draining_data draining_task_data;
struct kpb_client clients[KPB_MAX_NO_OF_CLIENTS];
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 */
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 */
size_t host_period_size; /**< size of history period */
bool sync_draining_mode; /**< should we synchronize draining with
* host?
*/
enum comp_copy_type force_copy_type; /**< should we force copy_type on kpb sink? */
#ifdef CONFIG_IPC_MAJOR_4
struct ipc4_kpb_module_cfg ipc4_cfg;
#endif /* CONFIG_IPC_MAJOR_4 */
uint32_t num_of_sel_mic;
uint32_t num_of_in_channels;
uint32_t offsets[KPB_MAX_MICSEL_CHANNELS];
struct kpb_micselector_config mic_sel;
struct kpb_fmt_dev_list fmt_device_list;
struct fast_mode_task fmt;
#if CONFIG_AMS
uint32_t kpd_uuid_id;
#endif
};
/*! KPB private functions */
#ifndef CONFIG_AMS
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,
const struct comp_buffer *source, size_t size);
static size_t kpb_allocate_history_buffer(struct comp_data *kpb,
size_t hb_size_req);
static void kpb_clear_history_buffer(struct history_buffer *buff);
static void kpb_free_history_buffer(struct history_buffer *buff);
static inline bool kpb_is_sample_width_supported(uint32_t sampling_width);
static void kpb_copy_samples(struct comp_buffer *sink,
struct comp_buffer *source, size_t size,
size_t sample_width, uint32_t channels);
static void kpb_drain_samples(void *source, struct audio_stream *sink,
size_t size, size_t sample_width);
static void kpb_buffer_samples(const struct audio_stream *source,
int offset, void *sink, size_t size,
size_t sample_width);
static void kpb_reset_history_buffer(struct history_buffer *buff);
static inline bool validate_host_params(struct comp_dev *dev,
size_t host_period_size,
size_t host_buffer_size,
size_t hb_size_req);
static inline void kpb_change_state(struct comp_data *kpb,
enum kpb_state state);
#ifdef CONFIG_IPC_MAJOR_4
/* KpbFastModeTaskModulesList Namespace */
static inline int alloc_fmt_module_list_item(struct kpb_fmt_dev_list *fmt_device_list,
struct comp_dev *mi_ptr, struct comp_dev ***item);
static int clear_fmt_modules_list(struct kpb_fmt_dev_list *fmt_device_list,
uint32_t outpin_idx);
static int prepare_fmt_modules_list(struct comp_dev *kpb_dev, uint32_t outpin_idx,
const struct kpb_task_params *modules_to_prepare);
/* FMT Namespace */
static int register_modules_list(struct fast_mode_task *fmt,
struct device_list *new_list, size_t list_idx);
static int unregister_modules_list(struct fast_mode_task *fmt,
struct device_list *list_to_remove, size_t list_idx);
/* Devicelist */
static int devicelist_push(struct device_list *devlist, struct comp_dev **dev);
static void devicelist_reset(struct device_list *devlist, bool remove_items);
#endif
static uint64_t kpb_task_deadline(void *data)
{
return SOF_TASK_DEADLINE_ALMOST_IDLE;
}
#if CONFIG_AMS
/* Key-phrase detected message*/
static const ams_uuid_t ams_kpd_msg_uuid = AMS_KPD_MSG_UUID;
/* Key-phrase detected notification handler*/
static void kpb_ams_kpd_notification(const struct ams_message_payload *const ams_message_payload,
void *ctx)
{
struct kpb_client *cli_data = (struct kpb_client *)ams_message_payload->message;
struct comp_dev *dev = ctx;
comp_dbg(dev, "kpb_ams_kpd_notification()");
kpb_init_draining(dev, cli_data);
}
#endif /* CONFIG_AMS */
#ifdef __ZEPHYR__
static void kpb_lock(struct comp_data *kpb)
{
k_mutex_lock(&kpb->lock, K_FOREVER);
}
static void kpb_unlock(struct comp_data *kpb)
{
k_mutex_unlock(&kpb->lock);
}
static void kpb_lock_init(struct comp_data *kpb)
{
k_mutex_init(&kpb->lock);
}
#else /* __ZEPHYR__ */
static void kpb_lock(struct comp_data *kpb)
{
kpb->key = k_spin_lock(&kpb->lock);
}
static void kpb_unlock(struct comp_data *kpb)
{
k_spin_unlock(&kpb->lock, kpb->key);
}
static void kpb_lock_init(struct comp_data *kpb)
{
k_spinlock_init(&kpb->lock);
}
#endif /* __ZEPHYR__ */
#if CONFIG_IPC_MAJOR_4
/**
* \brief Set and verify ipc params.
* \param[in] dev - component device pointer.
* \param[in] ipc_config - ipc config pointer.
* \return: none.
*/
static int kpb_set_verify_ipc_params(struct comp_dev *dev,
const struct ipc4_kpb_module_cfg *ipc_config)
{
struct comp_data *kpb = comp_get_drvdata(dev);
kpb->config.channels = ipc_config->base_cfg.audio_fmt.channels_count;
kpb->config.sampling_freq =
ipc_config->base_cfg.audio_fmt.sampling_frequency;
kpb->config.sampling_width =
ipc_config->base_cfg.audio_fmt.valid_bit_depth;
kpb->ipc4_cfg.base_cfg = ipc_config->base_cfg;
/* Initialize sinks */
kpb->sel_sink = NULL;
kpb->host_sink = NULL;
if (!kpb_is_sample_width_supported(kpb->config.sampling_width)) {
comp_err(dev, "kpb_set_verify_ipc_params(): requested sampling width not supported");
return -EINVAL;
}
if (kpb->config.channels > KPB_MAX_SUPPORTED_CHANNELS) {
comp_err(dev, "kpb_set_verify_ipc_params(): no of channels exceeded the limit");
return -EINVAL;
}
if (kpb->config.sampling_freq != KPB_SAMPLNG_FREQUENCY) {
comp_err(dev, "kpb_set_verify_ipc_params(): requested sampling frequency not supported");
return -EINVAL;
}
return 0;
}
/**
* \brief Set KPB component stream params.
* \param[in] dev - component device pointer.
* \param[in] params - sof ipc stream params pointer.
* \return: none.
*/
static void kpb_set_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct comp_data *kpb = comp_get_drvdata(dev);
enum sof_ipc_frame frame_fmt, valid_fmt;
comp_dbg(dev, "kpb_set_params()");
memset_s(params, sizeof(*params), 0, sizeof(*params));
params->channels = kpb->ipc4_cfg.base_cfg.audio_fmt.channels_count;
params->rate = kpb->ipc4_cfg.base_cfg.audio_fmt.sampling_frequency;
params->sample_container_bytes = kpb->ipc4_cfg.base_cfg.audio_fmt.depth / 8;
params->sample_valid_bytes =
kpb->ipc4_cfg.base_cfg.audio_fmt.valid_bit_depth / 8;
params->buffer_fmt = kpb->ipc4_cfg.base_cfg.audio_fmt.interleaving_style;
params->buffer.size = kpb->ipc4_cfg.base_cfg.ibs * KPB_MAX_BUFF_TIME * params->channels;
params->host_period_bytes = params->channels *
params->sample_container_bytes *
(params->rate / 1000);
audio_stream_fmt_conversion(kpb->ipc4_cfg.base_cfg.audio_fmt.depth,
kpb->ipc4_cfg.base_cfg.audio_fmt.valid_bit_depth,
&frame_fmt, &valid_fmt,
kpb->ipc4_cfg.base_cfg.audio_fmt.s_type);
params->frame_fmt = valid_fmt;
}
/**
* \brief Set KPB component stream params.
* \param[in] dev - component device pointer.
* \param[in] type - sof ipc stream params pointer.
* \param[in] value - ipc4 base module config pointer.
* \return: none.
*/
static int kpb_get_attribute(struct comp_dev *dev,
uint32_t type,
void *value)
{
struct comp_data *kpb = comp_get_drvdata(dev);
switch (type) {
case COMP_ATTR_BASE_CONFIG:
*(struct ipc4_base_module_cfg *)value = kpb->ipc4_cfg.base_cfg;
break;
default:
return -EINVAL;
}
return 0;
}
/**
* \brief Initialize KPB sinks when binding.
* \param[in] dev - component device pointer.
* \param[in] data - ipc4 bind/unbind data.
* \return: none.
*/
static int kpb_bind(struct comp_dev *dev, void *data)
{
struct comp_data *kpb = comp_get_drvdata(dev);
struct ipc4_module_bind_unbind *bu;
struct list_item *blist;
int buf_id;
int ret = 0;
comp_dbg(dev, "kpb_bind()");
bu = (struct ipc4_module_bind_unbind *)data;
buf_id = IPC4_COMP_ID(bu->extension.r.src_queue, bu->extension.r.dst_queue);
/* We're assuming here that KPB Real Time sink (kpb->sel_sink) is
* always connected to input pin of Detector pipeline so during IPC4
* Bind operation both src_queue and dst_queue will have id = 0
* (Detector/MicSel has one input pin). To properly connect KPB sink
* with Detector source we're looking for buffer with id=0.
*/
list_for_item(blist, &dev->bsink_list) {
struct comp_buffer *sink = container_of(blist, struct comp_buffer, source_list);
int sink_buf_id;
if (!sink->sink) {
ret = -EINVAL;
break;
}
sink_buf_id = buf_get_id(sink);
if (sink_buf_id == buf_id) {
if (sink_buf_id == 0)
kpb->sel_sink = sink;
else
kpb->host_sink = sink;
}
}
return ret;
}
/**
* \brief Reset KPB sinks when unbinding.
* \param[in] dev - component device pointer.
* \param[in] data - ipc4 bind/unbind data.
* \return: none.
*/
static int kpb_unbind(struct comp_dev *dev, void *data)
{
struct comp_data *kpb = comp_get_drvdata(dev);
struct ipc4_module_bind_unbind *bu;
int buf_id;
comp_dbg(dev, "kpb_bind()");
bu = (struct ipc4_module_bind_unbind *)data;
buf_id = IPC4_COMP_ID(bu->extension.r.src_queue, bu->extension.r.dst_queue);
/* Reset sinks when unbinding */
if (buf_id == 0)
kpb->sel_sink = NULL;
else
kpb->host_sink = NULL;
/* Clear fmt config */
return clear_fmt_modules_list(&kpb->fmt_device_list, bu->extension.r.src_queue);
}
#else /* CONFIG_IPC_MAJOR_4 */
/**
* \brief Set and verify ipc params.
* \param[in] dev - component device pointer.
* \param[in] ipc_config - ipc config pointer type.
* \return: none.
*/
static int kpb_set_verify_ipc_params(struct comp_dev *dev,
const struct ipc_config_process *ipc_config)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int ret;
ret = memcpy_s(&kpb->config, sizeof(kpb->config), ipc_config->data,
ipc_config->size);
if (ret) {
comp_err(dev, "kpb_new(): cannot memcpy_s %d bytes into sof_kpb_config (%d)\n",
ipc_config->size, sizeof(kpb->config));
return -EINVAL;
}
/* Initialize sinks */
kpb->sel_sink = NULL;
kpb->host_sink = NULL;
if (!kpb_is_sample_width_supported(kpb->config.sampling_width)) {
comp_err(dev, "kpb_set_verify_ipc_params(): requested sampling width not supported");
return -EINVAL;
}
if (kpb->config.channels > KPB_MAX_SUPPORTED_CHANNELS) {
comp_err(dev, "kpb_set_verify_ipc_params(): no of channels exceeded the limit");
return -EINVAL;
}
if (kpb->config.sampling_freq != KPB_SAMPLNG_FREQUENCY) {
comp_err(dev, "kpb_set_verify_ipc_params(): requested sampling frequency not supported");
return -EINVAL;
}
return 0;
}
static void kpb_set_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{}
#endif /* CONFIG_IPC_MAJOR_4 */
/*
* \brief Create a key phrase buffer component.
* \param[in] config - generic ipc component pointer.
*
* \return: a pointer to newly created KPB component.
*/
static struct comp_dev *kpb_new(const struct comp_driver *drv,
const struct comp_ipc_config *config,
const void *spec)
{
#if CONFIG_IPC_MAJOR_4
const struct ipc4_kpb_module_cfg *ipc_process = spec;
size_t ipc_config_size = sizeof(*ipc_process);
size_t kpb_config_size = sizeof(struct ipc4_kpb_module_cfg);
#else
const struct ipc_config_process *ipc_process = spec;
size_t ipc_config_size = ipc_process->size;
size_t kpb_config_size = sizeof(struct sof_kpb_config);
#endif
struct task_ops ops = {
.run = kpb_draining_task,
.get_deadline = kpb_task_deadline,
};
struct comp_dev *dev;
struct comp_data *kpb;
int ret;
comp_cl_info(&comp_kpb, "kpb_new()");
/* make sure data size is not bigger than config space */
if (ipc_config_size > kpb_config_size) {
comp_cl_err(&comp_kpb, "kpb_new(): ipc config size %u too big",
ipc_config_size);
return NULL;
}
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
kpb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*kpb));
if (!kpb) {
rfree(dev);
return NULL;
}
comp_set_drvdata(dev, kpb);
ret = kpb_set_verify_ipc_params(dev, ipc_process);
if (ret) {
rfree(dev);
return NULL;
}
kpb_lock_init(kpb);
/* Initialize draining task */
schedule_task_init_edf(&kpb->draining_task, /* task structure */
SOF_UUID(kpb_task_uuid), /* task uuid */
&ops, /* task ops */
&kpb->draining_task_data, /* task private data */
0, /* core on which we should run */
0); /* no flags */
/* Init basic component data */
kpb->hd.c_hb = NULL;
kpb->kpb_no_of_clients = 0;
kpb->state_log = 0;
#ifdef CONFIG_KPB_FORCE_COPY_TYPE_NORMAL
kpb->force_copy_type = COMP_COPY_NORMAL;
#else
kpb->force_copy_type = COMP_COPY_INVALID; /* do not change kpb sink copy type */
#endif
/* Kpb has been created successfully */
dev->state = COMP_STATE_READY;
kpb_change_state(kpb, KPB_STATE_CREATED);
return dev;
}
/**
* \brief Allocate history buffer.
* \param[in] kpb - KPB component data pointer.
*
* \return: none.
*/
static size_t kpb_allocate_history_buffer(struct comp_data *kpb,
size_t hb_size_req)
{
struct history_buffer *hb;
struct history_buffer *new_hb = NULL;
/*! Total allocation size */
size_t hb_size = hb_size_req;
/*! Current allocation size */
size_t ca_size = hb_size;
/*! Memory caps priorites for history buffer */
int hb_mcp[KPB_NO_OF_MEM_POOLS] = {SOF_MEM_CAPS_LP, SOF_MEM_CAPS_HP,
SOF_MEM_CAPS_RAM };
void *new_mem_block = NULL;
size_t temp_ca_size;
int i = 0;
size_t allocated_size = 0;
comp_cl_info(&comp_kpb, "kpb_allocate_history_buffer()");
/* Initialize history buffer */
kpb->hd.c_hb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(struct history_buffer));
if (!kpb->hd.c_hb)
return 0;
kpb->hd.c_hb->next = kpb->hd.c_hb;
kpb->hd.c_hb->prev = kpb->hd.c_hb;
hb = kpb->hd.c_hb;
/* Allocate history buffer/s. KPB history buffer has a size of
* KPB_MAX_BUFFER_SIZE, since there is no single memory block
* that big, we need to allocate couple smaller blocks which
* linked together will form history buffer.
*/
while (hb_size > 0 && i < ARRAY_SIZE(hb_mcp)) {
/* Try to allocate ca_size (current allocation size). At first
* attempt it will be equal to hb_size (history buffer size).
*/
new_mem_block = rballoc(0, hb_mcp[i], ca_size);
if (new_mem_block) {
/* We managed to allocate a block of ca_size.
* Now we initialize it.
*/
comp_cl_info(&comp_kpb, "kpb new memory block: %d",
ca_size);
allocated_size += ca_size;
hb->start_addr = new_mem_block;
hb->end_addr = (char *)new_mem_block +
ca_size;
hb->w_ptr = new_mem_block;
hb->r_ptr = new_mem_block;
hb->state = KPB_BUFFER_FREE;
hb_size -= ca_size;
hb->next = kpb->hd.c_hb;
/* Do we need another buffer? */
if (hb_size > 0) {
/* Yes, we still need at least one more buffer.
* Let's first create new container for it.
*/
new_hb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0,
SOF_MEM_CAPS_RAM,
sizeof(struct history_buffer));
if (!new_hb)
return 0;
hb->next = new_hb;
new_hb->next = kpb->hd.c_hb;
new_hb->state = KPB_BUFFER_OFF;
new_hb->prev = hb;
hb = new_hb;
kpb->hd.c_hb->prev = new_hb;
ca_size = hb_size;
i++;
}
} else {
/* We've failed to allocate ca_size of that hb_mcp
* let's try again with some smaller size.
* NOTE! If we decrement by some small value,
* the allocation will take significant time.
* However, bigger values like
* HEAP_HP_BUFFER_BLOCK_SIZE will result in lower
* accuracy of allocation.
*/
temp_ca_size = ca_size - KPB_ALLOCATION_STEP;
ca_size = (ca_size < temp_ca_size) ? 0 : temp_ca_size;
if (ca_size == 0) {
ca_size = hb_size;
i++;
}
continue;
}
}
comp_cl_info(&comp_kpb, "kpb_allocate_history_buffer(): allocated %d bytes",
allocated_size);
return allocated_size;
}
/**
* \brief Reclaim memory of a history buffer.
* \param[in] buff - pointer to current history buffer.
*
* \return none.
*/
static void kpb_free_history_buffer(struct history_buffer *buff)
{
struct history_buffer *_buff;
struct history_buffer *first_buff = buff;
comp_cl_info(&comp_kpb, "kpb_free_history_buffer()");
if (!buff)
return;
/* Free history buffer/s */
do {
/* First reclaim HB internal memory, then HB itself */
if (buff->start_addr)
rfree(buff->start_addr);
_buff = buff->next;
rfree(buff);
buff = _buff;
} while (buff && buff != first_buff);
}
/**
* \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);
comp_info(dev, "kpb_free()");
#if CONFIG_AMS
/* Unregister KPB as AMS consumer */
int ret;
ret = ams_helper_unregister_consumer(dev, kpb->kpd_uuid_id,
kpb_ams_kpd_notification);
if (ret)
comp_err(dev, "kpb_free(): AMS unregister error %d", ret);
#else
/* Unregister KPB from notifications */
notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT);
#endif/* CONFIG_AMS */
/* Reclaim memory occupied by history buffer */
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
kpb->hd.buffer_size = 0;
/* remove scheduling */
schedule_task_free(&kpb->draining_task);
/* change state */
kpb_change_state(kpb, KPB_STATE_DISABLED);
/* Free KPB */
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)
{
comp_info(dev, "kpb_trigger()");
return comp_set_state(dev, cmd);
}
static int kbp_verify_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
int ret;
comp_dbg(dev, "kbp_verify_params()");
ret = comp_verify_params(dev, 0, params);
if (ret < 0) {
comp_err(dev, "kpb_verify_params(): comp_verify_params() failed");
return ret;
}
return 0;
}
/**
* \brief KPB params.
* \param[in] dev - component device pointer.
* \param[in] params - pcm params.
* \return none.
*/
static int kpb_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int err;
if (dev->state == COMP_STATE_PREPARE) {
comp_err(dev, "kpb_params(): kpb has been already configured.");
return PPL_STATUS_PATH_STOP;
}
kpb_set_params(dev, params);
err = kbp_verify_params(dev, params);
if (err < 0) {
comp_err(dev, "kpb_params(): pcm params verification failed");
return -EINVAL;
}
kpb->host_buffer_size = params->buffer.size;
kpb->host_period_size = params->host_period_bytes;
kpb->config.sampling_width = params->sample_container_bytes * 8;
#if CONFIG_AMS
kpb->kpd_uuid_id = AMS_INVALID_MSG_TYPE;
#endif
return 0;
}
/**
* \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 *kpb = comp_get_drvdata(dev);
int ret = 0;
int i;
size_t hb_size_req = KPB_MAX_BUFFER_SIZE(kpb->config.sampling_width, kpb->config.channels);
comp_dbg(dev, "kpb_prepare()");
if (kpb->state == KPB_STATE_RESETTING ||
kpb->state == KPB_STATE_RESET_FINISHING) {
comp_cl_err(&comp_kpb, "kpb_prepare(): can not prepare KPB due to ongoing reset, state log %x",
kpb->state_log);
return -EBUSY;
}
ret = comp_set_state(dev, COMP_TRIGGER_PREPARE);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
if (!validate_host_params(dev, kpb->host_period_size,
kpb->host_buffer_size, hb_size_req)) {
return -EINVAL;
}
kpb_change_state(kpb, KPB_STATE_PREPARING);
/* Init private data */
kpb->kpb_no_of_clients = 0;
kpb->hd.buffered = 0;
if (kpb->hd.c_hb && kpb->hd.buffer_size < hb_size_req) {
/* Host params has changed, we need to allocate new buffer */
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
}
if (!kpb->hd.c_hb) {
/* Allocate history buffer */
kpb->hd.buffer_size = kpb_allocate_history_buffer(kpb,
hb_size_req);
/* Have we allocated what we requested? */
if (kpb->hd.buffer_size < hb_size_req) {
comp_cl_err(&comp_kpb, "kpb_prepare(): failed to allocate space for KPB buffer");
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
kpb->hd.buffer_size = 0;
return -EINVAL;
}
}
/* Init history buffer */
kpb_reset_history_buffer(kpb->hd.c_hb);
kpb->hd.free = kpb->hd.buffer_size;
/* Initialize clients data */
for (i = 0; i < KPB_MAX_NO_OF_CLIENTS; i++) {
kpb->clients[i].state = KPB_CLIENT_UNREGISTERED;
kpb->clients[i].r_ptr = NULL;
}
#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);
#else
/* Register KPB for notification */
ret = notifier_register(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT,
kpb_event_handler, 0);
#endif /* CONFIG_AMS */
if (ret < 0) {
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
return -ENOMEM;
}
#ifndef CONFIG_IPC_MAJOR_4
/* Search for KPB related sinks.
* NOTE! We assume here that channel selector component device
* is connected to the KPB sinks as well as host device.
*/
struct list_item *blist;
list_for_item(blist, &dev->bsink_list) {
struct comp_buffer *sink = container_of(blist, struct comp_buffer, source_list);
enum sof_comp_type type;
if (!sink->sink) {
ret = -EINVAL;
break;
}
type = dev_comp_type(sink->sink);
switch (type) {
case SOF_COMP_SELECTOR:
/* We found proper real time sink */
kpb->sel_sink = sink;
break;
case SOF_COMP_HOST:
/* We found proper host sink */
kpb->host_sink = sink;
break;
default:
break;
}
}
#else
/* Update number of sel_sink channels.
* If OBS is not equal to IBS it means that KPB will work in micselector mode.
*/
if (kpb->ipc4_cfg.base_cfg.ibs != kpb->ipc4_cfg.base_cfg.obs) {
struct list_item *sink_list;
const uint32_t byte_align = 1;
const uint32_t frame_align_req = 1;
uint32_t sink_id;
list_for_item(sink_list, &dev->bsink_list) {
struct comp_buffer *sink =
container_of(sink_list, struct comp_buffer, source_list);
audio_stream_init_alignment_constants(byte_align, frame_align_req,
&sink->stream);
sink_id = buf_get_id(sink);
if (sink_id == 0)
audio_stream_set_channels(&sink->stream, kpb->num_of_sel_mic);
else
audio_stream_set_channels(&sink->stream, kpb->config.channels);
}
}
#endif /* CONFIG_IPC_MAJOR_4 */
if (!kpb->sel_sink) {
comp_err(dev, "kpb_prepare(): could not find sink: sel_sink %p",
kpb->sel_sink);
ret = -EIO;
}
kpb->sync_draining_mode = true;
kpb_change_state(kpb, KPB_STATE_RUN);
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)
{
return 0;
}
/**
* \brief Resets KPB component.
* \param[in,out] dev KPB base component device.
* \return Error code.
*/
static int kpb_reset(struct comp_dev *dev)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int ret = 0;
int i;
comp_cl_info(&comp_kpb, "kpb_reset(): resetting from state %d, state log %x",
kpb->state, kpb->state_log);
switch (kpb->state) {
case KPB_STATE_BUFFERING:
case KPB_STATE_DRAINING:
/* KPB is performing some task now,
* terminate it gently.
*/
kpb_change_state(kpb, KPB_STATE_RESETTING);
ret = -EBUSY;
break;
case KPB_STATE_DISABLED:
case KPB_STATE_CREATED:
/* Nothing to reset */
ret = comp_set_state(dev, COMP_TRIGGER_RESET);
break;
default:
kpb->hd.buffered = 0;
kpb->sel_sink = NULL;
kpb->host_sink = NULL;
kpb->host_buffer_size = 0;
kpb->host_period_size = 0;
for (i = 0; i < KPB_MAX_NO_OF_CLIENTS; i++) {
kpb->clients[i].state = KPB_CLIENT_UNREGISTERED;
kpb->clients[i].r_ptr = NULL;
}
if (kpb->hd.c_hb) {
/* Reset history buffer - zero its data, reset pointers
* and states.
*/
kpb_reset_history_buffer(kpb->hd.c_hb);
}
#ifndef CONFIG_AMS
/* 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);
ret = comp_set_state(dev, COMP_TRIGGER_RESET);
break;
}
return ret;