forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.c
More file actions
929 lines (777 loc) · 23.9 KB
/
generic.c
File metadata and controls
929 lines (777 loc) · 23.9 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Marcin Rajwa <[email protected]>
/*
* \file generic.c
* \brief Generic Codec API
* \author Marcin Rajwa <[email protected]>
*
*/
#include <rtos/symbol.h>
#include <sof/compiler_attributes.h>
#include <sof/objpool.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/data_blob.h>
#include <sof/lib/fast-get.h>
#include <sof/schedule/dp_schedule.h>
#if CONFIG_IPC_MAJOR_4
#include <ipc4/header.h>
#include <ipc4/module.h>
#include <ipc4/pipeline.h>
#endif
/* The __ZEPHYR__ condition is to keep cmocka tests working */
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
#define MEM_API_CHECK_THREAD(res) do { \
if ((res)->rsrc_mngr != k_current_get()) \
LOG_WRN("mngr %p != cur %p", (res)->rsrc_mngr, k_current_get()); \
} while (0)
#else
#define MEM_API_CHECK_THREAD(res)
#endif
LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL);
int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
{
int ret;
struct module_config *dst;
/* loadable module must use module adapter */
struct processing_module *mod = comp_mod(dev);
struct module_data *md = &mod->priv;
comp_dbg(dev, "entry");
if (!cfg || !size) {
comp_err(dev, "wrong input params! dev %zx, cfg %zx size %zu",
(size_t)dev, (size_t)cfg, size);
return -EINVAL;
}
dst = &md->cfg;
if (!dst->data) {
/* No space for config available yet, allocate now */
dst->data = rballoc(SOF_MEM_FLAG_USER, size);
} else if (dst->size != size) {
/* The size allocated for previous config doesn't match the new one.
* Free old container and allocate new one.
*/
rfree(dst->data);
dst->data = rballoc(SOF_MEM_FLAG_USER, size);
}
if (!dst->data) {
comp_err(dev, "failed to allocate space for setup config.");
return -ENOMEM;
}
ret = memcpy_s(dst->data, size, cfg, size);
assert(!ret);
/* Config loaded, mark it as valid */
dst->size = size;
dst->avail = true;
comp_dbg(dev, "done");
return ret;
}
void mod_resource_init(struct processing_module *mod)
{
struct module_data *md = &mod->priv;
/* Init memory list */
list_init(&md->resources.objpool.list);
md->resources.objpool.heap = md->resources.heap;
md->resources.heap_usage = 0;
md->resources.heap_high_water_mark = 0;
}
int module_init(struct processing_module *mod)
{
int ret;
struct comp_dev *dev = mod->dev;
const struct module_interface *const interface = dev->drv->adapter_ops;
comp_dbg(dev, "entry");
#if CONFIG_IPC_MAJOR_3
if (mod->priv.state == MODULE_INITIALIZED)
return 0;
if (mod->priv.state > MODULE_INITIALIZED)
return -EPERM;
#endif
if (!interface) {
comp_err(dev, "module interface not defined");
return -EIO;
}
/* check interface, there must be one and only one of processing procedure */
if (!interface->init ||
(!!interface->process + !!interface->process_audio_stream +
!!interface->process_raw_data < 1)) {
comp_err(dev, "comp is missing mandatory interfaces");
return -EIO;
}
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
mod->priv.resources.rsrc_mngr = k_current_get();
#endif
/* Now we can proceed with module specific initialization */
#if CONFIG_SOF_USERSPACE_APPLICATION
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP)
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_MOD_INIT_INSTANCE, NULL);
else
#endif
ret = interface->init(mod);
if (ret) {
comp_err(dev, "error %d: module specific init failed", ret);
mod_free_all(mod);
return ret;
}
comp_dbg(dev, "done");
#if CONFIG_IPC_MAJOR_3
mod->priv.state = MODULE_INITIALIZED;
#endif
return 0;
}
static struct module_resource *container_get(struct processing_module *mod)
{
return objpool_alloc(&mod->priv.resources.objpool, sizeof(struct module_resource), 0);
}
static void container_put(struct processing_module *mod, struct module_resource *container)
{
objpool_free(&mod->priv.resources.objpool, container);
}
#if CONFIG_USERSPACE
void mod_heap_info(struct processing_module *mod, size_t *size, uintptr_t *start)
{
struct module_resources *res = &mod->priv.resources;
if (size)
*size = res->heap->heap.init_bytes;
if (start)
*start = (uintptr_t)container_of(res->heap, struct dp_heap_user, heap);
}
#endif
/**
* Allocates aligned buffer memory block for module.
* @param mod Pointer to the module this memory block is allocated for.
* @param bytes Size in bytes.
* @param alignment Alignment in bytes.
* @return Pointer to the allocated memory or NULL if failed.
*
* The allocated memory is automatically freed when the module is
* unloaded. The back-end, rballoc(), always aligns the memory to
* PLATFORM_DCACHE_ALIGN at the minimum.
*/
void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment)
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
MEM_API_CHECK_THREAD(res);
container = container_get(mod);
if (!container)
return NULL;
if (!size) {
comp_err(mod->dev, "requested allocation of 0 bytes.");
container_put(mod, container);
return NULL;
}
/* Allocate buffer memory for module */
void *ptr = sof_heap_alloc(res->heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_LARGE_BUFFER,
size, alignment);
if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
size, alignment, dev_comp_id(mod->dev));
container_put(mod, container);
return NULL;
}
/* Store reference to allocated memory */
container->ptr = ptr;
container->size = size;
container->type = MOD_RES_HEAP;
res->heap_usage += size;
if (res->heap_usage > res->heap_high_water_mark)
res->heap_high_water_mark = res->heap_usage;
return ptr;
}
EXPORT_SYMBOL(mod_balloc_align);
/**
* Allocates aligned memory block with flags for module.
* @param mod Pointer to the module this memory block is allocated for.
* @param flags Allocator flags.
* @param bytes Size in bytes.
* @param alignment Alignment in bytes.
* @return Pointer to the allocated memory or NULL if failed.
*
* The allocated memory is automatically freed when the module is unloaded.
*/
void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
size_t alignment)
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
MEM_API_CHECK_THREAD(res);
container = container_get(mod);
if (!container)
return NULL;
if (!size) {
comp_err(mod->dev, "requested allocation of 0 bytes.");
container_put(mod, container);
return NULL;
}
/* Allocate memory for module */
void *ptr = sof_heap_alloc(res->heap, flags, size, alignment);
if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
size, alignment, dev_comp_id(mod->dev));
container_put(mod, container);
return NULL;
}
/* Store reference to allocated memory */
container->ptr = ptr;
container->size = size;
container->type = MOD_RES_HEAP;
res->heap_usage += size;
if (res->heap_usage > res->heap_high_water_mark)
res->heap_high_water_mark = res->heap_usage;
return ptr;
}
EXPORT_SYMBOL(z_impl_mod_alloc_ext);
/**
* Creates a blob handler and releases it when the module is unloaded
* @param mod Pointer to module this memory block is allocated for.
* @return Pointer to the created data blob handler
*
* Like comp_data_blob_handler_new() but the handler is automatically freed.
*/
#if CONFIG_COMP_BLOB
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod)
{
struct module_resources * __maybe_unused res = &mod->priv.resources;
struct comp_data_blob_handler *bhp;
struct module_resource *container;
MEM_API_CHECK_THREAD(res);
container = container_get(mod);
if (!container)
return NULL;
bhp = comp_data_blob_handler_new_ext(mod->dev, false, NULL, NULL);
if (!bhp) {
container_put(mod, container);
return NULL;
}
container->bhp = bhp;
container->size = 0;
container->type = MOD_RES_BLOB_HANDLER;
return bhp;
}
EXPORT_SYMBOL(mod_data_blob_handler_new);
#endif
/**
* Make a module associated shared SRAM copy of DRAM read-only data.
* @param mod Pointer to module this copy is allocated for.
* @return Pointer to the SRAM copy.
*
* Like fast_get() but the handler is automatically freed.
*/
#if CONFIG_FAST_GET
const void *z_impl_mod_fast_get(struct processing_module *mod, const void * const dram_ptr,
size_t size)
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
const void *ptr;
MEM_API_CHECK_THREAD(res);
container = container_get(mod);
if (!container)
return NULL;
ptr = fast_get(res->heap, dram_ptr, size);
if (!ptr) {
container_put(mod, container);
return NULL;
}
container->sram_ptr = ptr;
container->size = 0;
container->type = MOD_RES_FAST_GET;
return ptr;
}
EXPORT_SYMBOL(z_impl_mod_fast_get);
#endif
static int free_contents(struct processing_module *mod, struct module_resource *container)
{
struct module_resources *res = &mod->priv.resources;
#if CONFIG_FAST_GET
struct k_mem_domain *mdom;
#endif
switch (container->type) {
case MOD_RES_HEAP:
sof_heap_free(res->heap, container->ptr);
res->heap_usage -= container->size;
return 0;
#if CONFIG_COMP_BLOB
case MOD_RES_BLOB_HANDLER:
comp_data_blob_handler_free(container->bhp);
return 0;
#endif
#if CONFIG_FAST_GET
case MOD_RES_FAST_GET:
#if CONFIG_USERSPACE
mdom = mod->mdom;
#else
mdom = NULL;
#endif
fast_put(res->heap, mdom, container->sram_ptr);
return 0;
#endif
default:
comp_err(mod->dev, "Unknown resource type: %d", container->type);
}
return -EINVAL;
}
struct mod_res_cb_arg {
struct processing_module *mod;
const void *ptr;
};
static bool mod_res_free(void *data, void *arg)
{
struct mod_res_cb_arg *cb_arg = arg;
struct module_resource *container = data;
if (cb_arg->ptr && container->ptr != cb_arg->ptr)
return false;
int ret = free_contents(cb_arg->mod, container);
if (ret < 0)
comp_err(cb_arg->mod->dev, "Cannot free allocation %p", cb_arg->ptr);
container_put(cb_arg->mod, container);
/* cb_arg->ptr == NULL means, that we're freeing all. Continue iterating */
return !!cb_arg->ptr;
}
/**
* Frees the memory block removes it from module's book keeping.
* @param mod Pointer to module this memory block was allocated for.
* @param ptr Pointer to the memory block.
*/
int z_impl_mod_free(struct processing_module *mod, const void *ptr)
{
struct module_resources *res = &mod->priv.resources;
MEM_API_CHECK_THREAD(res);
if (!ptr)
return 0;
/* Find which container holds this memory */
struct mod_res_cb_arg cb_arg = {mod, ptr};
int ret = objpool_iterate(&res->objpool, mod_res_free, &cb_arg);
if (ret < 0)
comp_err(mod->dev, "error: could not find memory pointed by %p", ptr);
return ret;
}
EXPORT_SYMBOL(z_impl_mod_free);
#ifdef CONFIG_USERSPACE
#include <zephyr/internal/syscall_handler.h>
#if CONFIG_FAST_GET
const void *z_vrfy_mod_fast_get(struct processing_module *mod, const void * const dram_ptr,
size_t size)
{
struct module_resources *res = &mod->priv.resources;
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->heap, sizeof(*res->heap)));
K_OOPS(K_SYSCALL_MEMORY_READ(dram_ptr, size));
return z_impl_mod_fast_get(mod, dram_ptr, size);
}
#include <zephyr/syscalls/mod_fast_get_mrsh.c>
#endif
void *z_vrfy_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
size_t alignment)
{
struct module_resources *res = &mod->priv.resources;
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->heap, sizeof(*res->heap)));
return z_impl_mod_alloc_ext(mod, flags, size, alignment);
}
#include <zephyr/syscalls/mod_alloc_ext_mrsh.c>
int z_vrfy_mod_free(struct processing_module *mod, const void *ptr)
{
struct module_resources *res = &mod->priv.resources;
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->heap, sizeof(*res->heap)));
return z_impl_mod_free(mod, ptr);
}
#include <zephyr/syscalls/mod_free_mrsh.c>
#endif
#if CONFIG_COMP_BLOB
void mod_data_blob_handler_free(struct processing_module *mod, struct comp_data_blob_handler *dbh)
{
mod_free(mod, (void *)dbh);
}
EXPORT_SYMBOL(mod_data_blob_handler_free);
#endif
#if CONFIG_FAST_GET
void mod_fast_put(struct processing_module *mod, const void *sram_ptr)
{
mod_free(mod, sram_ptr);
}
EXPORT_SYMBOL(mod_fast_put);
#endif
int module_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
comp_dbg(dev, "entry");
#if CONFIG_IPC_MAJOR_3
if (mod->priv.state == MODULE_IDLE)
return 0;
if (mod->priv.state < MODULE_INITIALIZED)
return -EPERM;
#endif
if (ops->prepare) {
int ret;
#if CONFIG_SOF_USERSPACE_APPLICATION
if (dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP) {
const union scheduler_dp_thread_ipc_param param = {
.pipeline_state = {
.trigger_cmd = COMP_TRIGGER_PREPARE,
.state = SOF_IPC4_PIPELINE_STATE_RUNNING,
.n_sources = num_of_sources,
.sources = sources,
.n_sinks = num_of_sinks,
.sinks = sinks,
},
};
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_GLB_SET_PIPELINE_STATE, ¶m);
} else
#endif
ret = ops->prepare(mod, sources, num_of_sources, sinks, num_of_sinks);
if (ret) {
comp_err(dev, "error %d: module specific prepare failed", ret);
return ret;
}
}
/* After prepare is done we no longer need runtime configuration
* as it has been applied during the procedure - it is safe to
* free it.
*/
rfree(md->cfg.data);
md->cfg.avail = false;
md->cfg.data = NULL;
#if CONFIG_IPC_MAJOR_3
md->state = MODULE_IDLE;
#endif
comp_dbg(dev, "done");
return 0;
}
int module_process_legacy(struct processing_module *mod,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers,
int num_output_buffers)
{
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
int ret;
comp_dbg(dev, "entry");
#if CONFIG_IPC_MAJOR_3
struct module_data *md = &mod->priv;
if (md->state != MODULE_IDLE) {
comp_err(dev, "wrong state %d", md->state);
return -EPERM;
}
/* set state to processing */
md->state = MODULE_PROCESSING;
#endif
if (IS_PROCESSING_MODE_AUDIO_STREAM(mod))
ret = ops->process_audio_stream(mod, input_buffers, num_input_buffers,
output_buffers, num_output_buffers);
else if (IS_PROCESSING_MODE_RAW_DATA(mod))
ret = ops->process_raw_data(mod, input_buffers, num_input_buffers,
output_buffers, num_output_buffers);
else
ret = -EOPNOTSUPP;
if (ret && ret != -ENOSPC && ret != -ENODATA) {
comp_err(dev, "error %d", ret);
return ret;
}
comp_dbg(dev, "done");
#if CONFIG_IPC_MAJOR_3
/* reset state to idle */
md->state = MODULE_IDLE;
#endif
return 0;
}
int module_process_sink_src(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct comp_dev *dev = mod->dev;
const struct module_interface *const ops = dev->drv->adapter_ops;
int ret;
comp_dbg(dev, "entry");
#if CONFIG_IPC_MAJOR_3
struct module_data *md = &mod->priv;
if (md->state != MODULE_IDLE) {
comp_err(dev, "wrong state %d", md->state);
return -EPERM;
}
/* set state to processing */
md->state = MODULE_PROCESSING;
#endif
assert(ops->process);
ret = ops->process(mod, sources, num_of_sources, sinks, num_of_sinks);
if (ret && ret != -ENOSPC && ret != -ENODATA) {
comp_err(dev, "error %d", ret);
return ret;
}
comp_dbg(dev, "done");
#if CONFIG_IPC_MAJOR_3
/* reset state to idle */
md->state = MODULE_IDLE;
#endif
return 0;
}
int module_reset(struct processing_module *mod)
{
int ret;
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
struct module_data *md = &mod->priv;
#if CONFIG_IPC_MAJOR_3
/* if the module was never prepared, no need to reset */
if (md->state < MODULE_IDLE)
return 0;
#endif
/* cancel task if DP task*/
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP && mod->dev->task &&
!IS_ENABLED(CONFIG_SOF_USERSPACE_APPLICATION) &&
!IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD))
schedule_task_cancel(mod->dev->task);
if (ops->reset) {
#if CONFIG_SOF_USERSPACE_APPLICATION
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP) {
const union scheduler_dp_thread_ipc_param param = {
.pipeline_state.trigger_cmd = COMP_TRIGGER_STOP,
};
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_GLB_SET_PIPELINE_STATE, ¶m);
} else
#endif
ret = ops->reset(mod);
if (ret) {
if (ret != PPL_STATUS_PATH_STOP)
comp_err(mod->dev,
"error %d: module specific reset() failed", ret);
return ret;
}
}
md->cfg.avail = false;
md->cfg.size = 0;
rfree(md->cfg.data);
md->cfg.data = NULL;
#if CONFIG_IPC_MAJOR_3
/*
* reset the state to allow the module's prepare callback to be invoked again for the
* subsequent triggers
*/
md->state = MODULE_INITIALIZED;
#endif
return 0;
}
/**
* Frees all the resources registered for this module
* @param mod Pointer to module that should have its resource freed.
*
* This function is called automatically when the module is unloaded.
*/
void mod_free_all(struct processing_module *mod)
{
struct module_resources *res = &mod->priv.resources;
MEM_API_CHECK_THREAD(res);
/* Free all contents found in used containers */
struct mod_res_cb_arg cb_arg = {mod, NULL};
objpool_iterate(&res->objpool, mod_res_free, &cb_arg);
objpool_prune(&res->objpool);
/* Make sure resource lists and accounting are reset */
mod_resource_init(mod);
}
int module_free(struct processing_module *mod)
{
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
struct module_data *md = &mod->priv;
int ret = 0;
if (ops->free && (mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP ||
!IS_ENABLED(CONFIG_SOF_USERSPACE_APPLICATION))) {
ret = ops->free(mod);
if (ret)
comp_warn(mod->dev, "error: %d", ret);
}
/* Free all memory shared by module_adapter & module */
md->cfg.avail = false;
md->cfg.size = 0;
rfree(md->cfg.data);
md->cfg.data = NULL;
if (md->runtime_params) {
rfree(md->runtime_params);
md->runtime_params = NULL;
}
#if CONFIG_IPC_MAJOR_3
md->state = MODULE_DISABLED;
#endif
return ret;
}
/*
* \brief Set module configuration - Common method to assemble large configuration message
* \param[in] mod - struct processing_module pointer
* \param[in] config_id - Configuration ID
* \param[in] pos - position of the fragment in the large message
* \param[in] data_offset_size: size of the whole configuration if it is the first fragment or the
* only fragment. Otherwise, it is the offset of the fragment in the
* whole configuration.
* \param[in] fragment: configuration fragment buffer
* \param[in] fragment_size: size of @fragment
* \params[in] response: optional response buffer to fill
* \params[in] response_size: size of @response
*
* \return: 0 upon success or error upon failure
*/
int module_set_configuration(struct processing_module *mod,
uint32_t config_id,
enum module_cfg_fragment_position pos, size_t data_offset_size,
const uint8_t *fragment_in, size_t fragment_size, uint8_t *response,
size_t response_size)
{
#if CONFIG_IPC_MAJOR_3
struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment_in;
const uint8_t *fragment = (const uint8_t *)cdata->data[0].data;
#elif CONFIG_IPC_MAJOR_4
const uint8_t *fragment = fragment_in;
#endif
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
size_t offset = 0;
uint8_t *dst;
int ret;
switch (pos) {
case MODULE_CFG_FRAGMENT_FIRST:
case MODULE_CFG_FRAGMENT_SINGLE:
/*
* verify input params & allocate memory for the config blob when the first
* fragment arrives
*/
md->new_cfg_size = data_offset_size;
/* Check that there is no previous request in progress */
if (md->runtime_params) {
comp_err(dev, "error: busy with previous request");
return -EBUSY;
}
if (!md->new_cfg_size)
return 0;
if (md->new_cfg_size > CONFIG_MODULE_MAX_BLOB_SIZE) {
comp_err(dev, "error: blob size is too big cfg size %zu, allowed %d",
md->new_cfg_size, CONFIG_MODULE_MAX_BLOB_SIZE);
return -EINVAL;
}
/* Allocate buffer for new params */
md->runtime_params = rballoc(SOF_MEM_FLAG_USER, md->new_cfg_size);
if (!md->runtime_params) {
comp_err(dev, "space allocation for new params failed");
return -ENOMEM;
}
memset(md->runtime_params, 0, md->new_cfg_size);
break;
default:
if (!md->runtime_params) {
comp_err(dev, "error: no memory available for runtime params in consecutive load");
return -EIO;
}
/* set offset for intermediate and last fragments */
offset = data_offset_size;
break;
}
dst = (uint8_t *)md->runtime_params + offset;
ret = memcpy_s(dst, md->new_cfg_size - offset, fragment, fragment_size);
if (ret < 0) {
comp_err(dev, "error: %d failed to copy fragment", ret);
return ret;
}
/* return as more fragments of config data expected */
if (pos == MODULE_CFG_FRAGMENT_MIDDLE || pos == MODULE_CFG_FRAGMENT_FIRST)
return 0;
/* config fully copied, now load it */
ret = module_load_config(dev, md->runtime_params, md->new_cfg_size);
if (ret)
comp_err(dev, "error %d: config failed", ret);
else
comp_dbg(dev, "config load successful");
md->new_cfg_size = 0;
if (md->runtime_params)
rfree(md->runtime_params);
md->runtime_params = NULL;
return ret;
}
EXPORT_SYMBOL(module_set_configuration);
int module_bind(struct processing_module *mod, struct bind_info *bind_data)
{
int ret;
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
switch (bind_data->bind_type) {
case COMP_BIND_TYPE_SINK:
ret = sink_bind(bind_data->sink, mod);
break;
case COMP_BIND_TYPE_SOURCE:
ret = source_bind(bind_data->source, mod);
break;
default:
ret = -EINVAL;
}
if (ret)
return ret;
if (ops->bind) {
#if CONFIG_SOF_USERSPACE_APPLICATION
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP) {
const union scheduler_dp_thread_ipc_param param = {
.bind_data = bind_data,
};
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_MOD_BIND, ¶m);
} else
#endif
ret = ops->bind(mod, bind_data);
}
return ret;
}
int module_unbind(struct processing_module *mod, struct bind_info *unbind_data)
{
int ret;
const struct module_interface *const ops = mod->dev->drv->adapter_ops;
switch (unbind_data->bind_type) {
case COMP_BIND_TYPE_SINK:
ret = sink_unbind(unbind_data->sink);
break;
case COMP_BIND_TYPE_SOURCE:
ret = source_unbind(unbind_data->source);
break;
default:
ret = -EINVAL;
}
if (ret)
return ret;
if (ops->unbind) {
#if CONFIG_SOF_USERSPACE_APPLICATION
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP) {
const union scheduler_dp_thread_ipc_param param = {
.bind_data = unbind_data,
};
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_MOD_UNBIND, ¶m);
} else
#endif
ret = ops->unbind(mod, unbind_data);
}
return ret;
}
void module_update_buffer_position(struct input_stream_buffer *input_buffers,
struct output_stream_buffer *output_buffers,
uint32_t frames)
{
struct audio_stream *source = input_buffers->data;
struct audio_stream *sink = output_buffers->data;
input_buffers->consumed += audio_stream_frame_bytes(source) * frames;
output_buffers->size += audio_stream_frame_bytes(sink) * frames;
}
EXPORT_SYMBOL(module_update_buffer_position);
uint32_t module_get_deadline(struct processing_module *mod)
{
uint32_t deadline;
/* LL modules have no deadline - it is always "now" */
if (mod->dev->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_LL)
return 0;
/* startup condition - set deadline to "unknown" */
if (mod->dp_startup_delay)
return UINT32_MAX / 2;
deadline = UINT32_MAX;
/* calculate the shortest LFT for all sinks */
for (size_t i = 0; i < mod->num_of_sinks; i++) {
uint32_t sink_lft = sink_get_last_feeding_time(mod->sinks[i]);
deadline = MIN(deadline, sink_lft);
}
return deadline;
}