-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathuserspace_proxy.c
More file actions
929 lines (769 loc) · 29.1 KB
/
userspace_proxy.c
File metadata and controls
929 lines (769 loc) · 29.1 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) 2023 Intel Corporation. All rights reserved.
//
// Author: Jaroslaw Stelter <jaroslaw.stelter@intel.com>
// Author: Adrian Warecki <adrian.warecki@intel.com>
/**
* \file audio/module_adapter/library/userspace_proxy.c
* \brief Userspace proxy. Acts as an intermediary between SOF and a userspace module.
* \brief Responsible for preparing the memory domain required for userspace execution
* \brief and forwarding API calls. The proxy invokes corresponding module methods
* \brief in userspace context. Enables execution of any module implementing module_interface
* \brief as a userspace module.
* \authors Adrian Warecki
*/
#include <sof/common.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/memory.h>
#include <rtos/string.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sof/lib_manager.h>
#include <sof/llext_manager.h>
#include <sof/audio/component.h>
#include <sof/schedule/dp_schedule.h>
#include <rtos/userspace_helper.h>
#include <utilities/array.h>
#include <zephyr/sys/sem.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/module_adapter/library/userspace_proxy.h>
#include <sof/audio/module_adapter/library/userspace_proxy_user.h>
#include <rimage/sof/user/manifest.h>
/* Assume that all the code runs in supervisor mode and don't make system calls. */
#define __ZEPHYR_SUPERVISOR__
LOG_MODULE_REGISTER(userspace_proxy, CONFIG_SOF_LOG_LEVEL);
/* 6f6b6f4b-6f73-7466-20e1e62b9779f003 */
SOF_DEFINE_REG_UUID(userspace_proxy);
DECLARE_TR_CTX(userspace_proxy_tr, SOF_UUID(userspace_proxy_uuid), LOG_LEVEL_INFO);
static const struct module_interface userspace_proxy_interface;
#if IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
#include <sof/audio/module_adapter/iadk/system_agent.h>
#include <sof/schedule/dp_schedule.h>
static inline int user_worker_get(void)
{
return 0;
}
static inline void user_worker_put(void) { }
struct k_work_user *userspace_proxy_register_ipc_handler(struct processing_module *mod,
struct k_event *event)
{
struct userspace_context * const user_ctx = mod->user_ctx;
if (user_ctx) {
tr_dbg(&userspace_proxy_tr, "Set DP event %p for module %p",
(void *)event, (void *)mod);
assert(user_ctx->work_item);
user_ctx->dp_event = event;
user_ctx->work_item->event = event;
return &user_ctx->work_item->work_item;
}
return NULL;
}
#else
/* IPC requests targeting userspace modules are handled through a user work queue.
* Each userspace module provides its own work item that carries the IPC request parameters.
* The worker thread is switched into the module's memory domain and receives the work item.
* It invokes the appropriate module function in userspace context and writes the operation
* result back into the work item.
*
* There is only a single work queue, which is shared by all userspace modules. It is created
* dynamically when needed. Because SOF uses a single dedicated thread for handling IPC, there
* is no need to perform any additional serialization when accessing the worker.
*/
struct user_worker {
k_tid_t thread_id; /* ipc worker thread ID */
uint32_t reference_count; /* module reference count */
void *stack_ptr; /* pointer to worker stack */
struct k_work_user_q work_queue;
struct k_event event;
};
static struct user_worker worker;
static int user_worker_get(void)
{
if (worker.reference_count) {
worker.reference_count++;
return 0;
}
worker.stack_ptr = user_stack_allocate(CONFIG_SOF_USERSPACE_PROXY_WORKER_STACK_SIZE,
K_USER);
if (!worker.stack_ptr) {
tr_err(&userspace_proxy_tr, "Userspace worker stack allocation failed.");
return -ENOMEM;
}
k_event_init(&worker.event);
k_work_user_queue_start(&worker.work_queue, worker.stack_ptr,
CONFIG_SOF_USERSPACE_PROXY_WORKER_STACK_SIZE, 0, NULL);
worker.thread_id = k_work_user_queue_thread_get(&worker.work_queue);
k_thread_access_grant(worker.thread_id, &worker.event);
worker.reference_count++;
return 0;
}
static void user_worker_put(void)
{
/* Module removed so decrement counter */
worker.reference_count--;
/* Free worker resources if no more active user space modules */
if (worker.reference_count == 0) {
k_thread_abort(worker.thread_id);
user_stack_free(worker.stack_ptr);
}
}
#endif
static int user_work_item_init(struct userspace_context *user_ctx, struct k_heap *user_heap)
{
struct user_work_item *work_item = NULL;
int ret;
ret = user_worker_get();
if (ret)
return ret;
/* We have only a single userspace IPC worker. It handles requests for all userspace
* modules, which may run on different cores. Because the worker processes work items
* coming from any core, the work item must be allocated in coherent memory.
*/
work_item = sof_heap_alloc(user_heap, SOF_MEM_FLAG_COHERENT, sizeof(*work_item), 0);
if (!work_item) {
user_worker_put();
return -ENOMEM;
}
k_work_user_init(&work_item->work_item, userspace_proxy_worker_handler);
#if !IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
work_item->event = &worker.event;
#endif
work_item->params.context = user_ctx;
work_item->params.mod = NULL;
user_ctx->work_item = work_item;
return 0;
}
static void user_work_item_free(struct userspace_context *user_ctx, struct k_heap *user_heap)
{
sof_heap_free(user_heap, user_ctx->work_item);
user_worker_put();
}
static inline struct module_params *user_work_get_params(struct userspace_context *user_ctx)
{
return &user_ctx->work_item->params;
}
BUILD_ASSERT(IS_ALIGNED(MAILBOX_HOSTBOX_BASE, CONFIG_MMU_PAGE_SIZE),
"MAILBOX_HOSTBOX_BASE is not page aligned");
BUILD_ASSERT(IS_ALIGNED(MAILBOX_HOSTBOX_SIZE, CONFIG_MMU_PAGE_SIZE),
"MAILBOX_HOSTBOX_SIZE is not page aligned");
static int userspace_proxy_invoke(struct userspace_context *user_ctx, uint32_t cmd,
bool ipc_payload_access)
{
#if IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
struct k_event * const event = user_ctx->dp_event;
#else
struct k_event * const event = &worker.event;
#endif
struct module_params *params = user_work_get_params(user_ctx);
const uintptr_t ipc_req_buf = (uintptr_t)MAILBOX_HOSTBOX_BASE;
struct k_mem_partition ipc_part = {
.start = ipc_req_buf,
.size = MAILBOX_HOSTBOX_SIZE,
.attr = user_get_partition_attr(ipc_req_buf) | K_MEM_PARTITION_P_RO_U_RO,
};
int ret = 0, ret2;
params->cmd = cmd;
if (ipc_payload_access) {
ret = k_mem_domain_add_partition(user_ctx->comp_dom, &ipc_part);
if (ret < 0) {
tr_err(&userspace_proxy_tr, "Add mailbox to domain error: %d", ret);
return ret;
}
}
#if !IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
/* Switch worker thread to module memory domain */
ret = k_mem_domain_add_thread(user_ctx->comp_dom, worker.thread_id);
if (ret < 0) {
tr_err(&userspace_proxy_tr, "Failed to switch memory domain, error: %d", ret);
goto done;
}
/* Pin worker thread to the same core as the module */
ret = k_thread_cpu_pin(worker.thread_id, cpu_get_id());
if (ret < 0) {
tr_err(&userspace_proxy_tr, "Failed to pin cpu, error: %d", ret);
goto done;
}
ret = k_work_user_submit_to_queue(&worker.work_queue, &user_ctx->work_item->work_item);
if (ret < 0) {
tr_err(&userspace_proxy_tr, "Submit to queue error: %d", ret);
goto done;
}
#else
assert(event);
k_event_post(event, DP_TASK_EVENT_IPC);
#endif
/* Timeout value is aligned with the ipc_wait_for_compound_msg function */
if (!k_event_wait_safe(event, DP_TASK_EVENT_IPC_DONE, false,
Z_TIMEOUT_US(250 * 20))) {
tr_err(&userspace_proxy_tr, "IPC processing timedout.");
ret = -ETIMEDOUT;
}
done:
if (ipc_payload_access) {
ret2 = k_mem_domain_remove_partition(user_ctx->comp_dom, &ipc_part);
if (ret2 < 0) {
tr_err(&userspace_proxy_tr, "Mailbox remove from domain error: %d", ret);
if (!ret)
ret = ret2;
}
}
return ret;
}
extern struct k_mem_partition common_partition;
static int userspace_proxy_memory_init(struct userspace_context *user_ctx,
const struct comp_driver *drv)
{
/* Add module private heap to memory partitions */
struct k_mem_partition heap_part = { .attr = K_MEM_PARTITION_P_RW_U_RW };
struct sys_heap *heap = &drv->user_heap->heap;
k_mem_region_align(&heap_part.start, &heap_part.size,
POINTER_TO_UINT(heap->init_mem),
heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);
tr_dbg(&userspace_proxy_tr, "Heap partition %#lx + %zx, attr = %u",
heap_part.start, heap_part.size, heap_part.attr);
/* When a new memory domain is created, only the "factory" entries from the L2 page
* tables are copied. Memory that was dynamically mapped during firmware execution
* will not be accessible from the new domain. The k_heap structure (drv->user_heap)
* resides in such dynamically mapped memory, so we must explicitly add a partition
* for it to ensure that syscalls can access this structure from the userspace domain.
*/
struct k_mem_partition heap_struct_part;
k_mem_region_align(&heap_struct_part.start, &heap_struct_part.size,
POINTER_TO_UINT(drv->user_heap),
sizeof(*drv->user_heap), CONFIG_MM_DRV_PAGE_SIZE);
heap_struct_part.attr = K_MEM_PARTITION_P_RW_U_NA |
user_get_partition_attr(heap_struct_part.start);
tr_err(&userspace_proxy_tr, "Heap struct partition %#lx + %zx, attr = %u",
heap_struct_part.start, heap_struct_part.size, heap_struct_part.attr);
#if defined(CONFIG_SOF_ZEPHYR_HEAP_CACHED)
/* Add cached module private heap to memory partitions */
struct k_mem_partition heap_cached_part = {
.attr = K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB
};
k_mem_region_align(&heap_cached_part.start, &heap_cached_part.size,
POINTER_TO_UINT(sys_cache_cached_ptr_get(heap->init_mem)),
heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);
tr_dbg(&userspace_proxy_tr, "Cached heap partition %#lx + %zx, attr = %u",
heap_cached_part.start, heap_cached_part.size, heap_cached_part.attr);
#endif
struct k_mem_partition *parts_ptr[] = {
/* The common partition contains sof components accessible to the userspace module.
* These include ops structures marked with APP_TASK_DATA.
*/
&common_partition,
#ifdef CONFIG_SOF_ZEPHYR_HEAP_CACHED
&heap_cached_part,
#endif
&heap_part,
&heap_struct_part
};
tr_dbg(&userspace_proxy_tr, "Common partition %#lx + %zx, attr = %u",
common_partition.start, common_partition.size, common_partition.attr);
return k_mem_domain_init(user_ctx->comp_dom, ARRAY_SIZE(parts_ptr), parts_ptr);
}
static int userspace_proxy_add_sections(struct userspace_context *user_ctx, uint32_t instance_id,
const struct sof_man_module *const mod)
{
struct k_mem_partition mem_partition;
void *va_base;
int idx, ret;
for (idx = 0; idx < ARRAY_SIZE(mod->segment); ++idx) {
if (!mod->segment[idx].flags.r.load)
continue;
if (mod->segment[idx].flags.r.code)
mem_partition.attr = K_MEM_PARTITION_P_RX_U_RX;
else if (!mod->segment[idx].flags.r.readonly)
mem_partition.attr = K_MEM_PARTITION_P_RW_U_RW;
else
mem_partition.attr = K_MEM_PARTITION_P_RO_U_RO;
mem_partition.start = mod->segment[idx].v_base_addr;
mem_partition.size = mod->segment[idx].flags.r.length * CONFIG_MM_DRV_PAGE_SIZE;
mem_partition.attr |= user_get_partition_attr(mem_partition.start);
ret = k_mem_domain_add_partition(user_ctx->comp_dom, &mem_partition);
tr_dbg(&userspace_proxy_tr, "Add mod partition %#lx + %zx, attr = %u, ret = %d",
mem_partition.start, mem_partition.size, mem_partition.attr, ret);
if (ret < 0)
return ret;
}
lib_manager_get_instance_bss_address(instance_id, mod, &va_base, &mem_partition.size);
mem_partition.start = POINTER_TO_UINT(va_base);
mem_partition.attr = user_get_partition_attr(mem_partition.start) |
K_MEM_PARTITION_P_RW_U_RW;
ret = k_mem_domain_add_partition(user_ctx->comp_dom, &mem_partition);
tr_dbg(&userspace_proxy_tr, "Add bss partition %#lx + %zx, attr = %u, ret = %d",
mem_partition.start, mem_partition.size, mem_partition.attr, ret);
return ret;
}
static int userspace_proxy_start_agent(struct userspace_context *user_ctx,
system_agent_start_fn start_fn,
const struct system_agent_params *agent_params,
const void **agent_interface)
{
const byte_array_t * const mod_cfg = (byte_array_t *)agent_params->mod_cfg;
struct module_params *params = user_work_get_params(user_ctx);
params->ext.agent.start_fn = start_fn;
/* Start the system agent, if provided. */
if (start_fn) {
params->ext.agent.params = *agent_params;
params->ext.agent.params.mod_cfg = ¶ms->ext.agent.mod_cfg;
params->ext.agent.mod_cfg = *mod_cfg;
/* In case of processing modules ipc in the DP thread, the agent will be started in
* the init function. At this point the DP thread does not exist yet.
*/
#if !IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
int ret = userspace_proxy_invoke(user_ctx, USER_PROXY_MOD_CMD_AGENT_START, true);
if (ret)
return ret;
*agent_interface = params->ext.agent.out_interface;
return params->status;
#endif
}
return 0;
}
int userspace_proxy_create(struct userspace_context **user_ctx, const struct comp_driver *drv,
const struct sof_man_module *manifest, system_agent_start_fn agent_fn,
const struct system_agent_params *agent_params,
const void **agent_interface, const struct module_interface **ops)
{
struct userspace_context *context;
struct k_mem_domain *domain;
int ret;
tr_dbg(&userspace_proxy_tr, "userspace create");
context = k_heap_alloc(drv->user_heap, sizeof(struct userspace_context), K_FOREVER);
if (!context)
return -ENOMEM;
context->dp_event = NULL;
/* Allocate memory domain struct */
domain = rzalloc(SOF_MEM_FLAG_KERNEL, sizeof(*domain));
if (!domain) {
ret = -ENOMEM;
goto error;
}
context->comp_dom = domain;
ret = userspace_proxy_memory_init(context, drv);
if (ret)
goto error_dom;
if (agent_fn)
ret = userspace_proxy_add_sections(context, agent_params->instance_id, manifest);
else
/* llext modules do not use the system agent. */
ret = llext_manager_add_domain(agent_params->module_id, domain);
if (ret)
goto error_dom;
ret = user_work_item_init(context, drv->user_heap);
if (ret)
goto error_dom;
ret = userspace_proxy_start_agent(context, agent_fn, agent_params, agent_interface);
if (ret) {
tr_err(&userspace_proxy_tr, "System agent failed with error %d.", ret);
goto error_work_item;
}
*user_ctx = context;
/* Store a pointer to the module's interface. For the LMDK modules, the agent places a
* pointer to the module interface at the address specified by agent_interface. Since this
* points to ops, the assignment of the module interface used by this proxy must occur
* after the agent has been started. For other module types, the ops parameter points to a
* valid module interface.
*/
context->interface = *ops;
/* All calls to the module interface must pass through the proxy. Set up our own interface.
*/
*ops = &userspace_proxy_interface;
return 0;
error_work_item:
user_work_item_free(context, drv->user_heap);
error_dom:
rfree(domain);
error:
k_heap_free(drv->user_heap, context);
return ret;
}
void userspace_proxy_destroy(const struct comp_driver *drv, struct userspace_context *user_ctx)
{
tr_dbg(&userspace_proxy_tr, "userspace proxy destroy");
user_work_item_free(user_ctx, drv->user_heap);
rfree(user_ctx->comp_dom);
k_heap_free(drv->user_heap, user_ctx);
}
/**
* Copy parameters to user worker accessible space.
* Queue module init() operation and return its result.
* Module init() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_init(struct processing_module *mod)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
#if IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD)
/* Start the system agent, if provided. Params is already filled by
* the userspace_proxy_start_agent function.
*/
if (params->ext.agent.start_fn) {
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_AGENT_START, true);
if (ret)
return ret;
if (params->ext.agent.start_fn == system_agent_start)
module_set_private_data(mod, (void *)params->ext.agent.out_interface);
else
mod->user_ctx->interface = params->ext.agent.out_interface;
}
#endif
params->mod = mod;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_INIT, true);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module prepare() operation and return its result.
* Module prepare() code is performed in user workqueue.
*
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->prepare)
return 0;
params->ext.proc.sources = sources;
params->ext.proc.num_of_sources = num_of_sources;
params->ext.proc.sinks = sinks;
params->ext.proc.num_of_sinks = num_of_sinks;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_PREPARE, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Forward processing request to the module's process() implementation.
*
* It is invoked by the DP thread running in userspace, so no
* additional queuing or context switching is performed here.
*
* @param mod Pointer to the processing module instance.
* @param sources Array of input sources for the module.
* @param num_of_sources Number of input sources.
* @param sinks Array of output sinks for the module.
* @param num_of_sinks Number of output sinks.
*
* @return 0 on success, negative error code on failure.
*/
static int userspace_proxy_process(struct processing_module *mod, struct sof_source **sources,
int num_of_sources, struct sof_sink **sinks, int num_of_sinks)
{
return mod->user_ctx->interface->process(mod, sources, num_of_sources, sinks, num_of_sinks);
}
/**
* Copy parameters to user worker accessible space.
* Queue module reset() operation and return its result.
* Module reset() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_reset(struct processing_module *mod)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
if (!mod->user_ctx->interface->reset)
return 0;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_RESET, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module free() operation and return its result.
* Module free() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_free(struct processing_module *mod)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret = 0;
comp_dbg(mod->dev, "start");
if (mod->user_ctx->interface->free) {
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_FREE, false);
if (ret)
return ret;
ret = params->status;
}
/* Destroy workqueue if this was last active userspace module */
userspace_proxy_destroy(mod->dev->drv, mod->user_ctx);
mod->user_ctx = NULL;
/* Return status from module code operation. */
return ret;
}
/**
* Copy parameters to user worker accessible space.
* Queue module set_configuration() operation and return its result.
* Module set_configuration() code is performed in user workqueue.
*
* @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 for success, error otherwise.
*/
static int userspace_proxy_set_configuration(struct processing_module *mod, uint32_t config_id,
enum module_cfg_fragment_position pos,
uint32_t data_offset_size, const uint8_t *fragment,
size_t fragment_size, uint8_t *response,
size_t response_size)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->set_configuration)
return 0;
params->ext.set_conf.config_id = config_id;
params->ext.set_conf.pos = pos;
params->ext.set_conf.data_off_size = data_offset_size;
params->ext.set_conf.fragment = fragment;
params->ext.set_conf.fragment_size = fragment_size;
params->ext.set_conf.response = response;
params->ext.set_conf.response_size = response_size;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_SET_CONF, true);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module get_configuration() operation and return its result.
* Module get_configuration() code is performed in user workqueue.
*
* @param[in] mod - struct processing_module pointer
* @param[in] config_id - Configuration ID
* @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
*
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_get_configuration(struct processing_module *mod, uint32_t config_id,
uint32_t *data_offset_size, uint8_t *fragment,
size_t fragment_size)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
struct k_mem_domain *domain = mod->user_ctx->comp_dom;
const uintptr_t ipc_resp_buf = POINTER_TO_UINT(ipc_get()->comp_data);
/* Memory partition exposing the IPC response buffer. This buffer is allocated
* by the IPC driver and contains the payload of IPC replies sent to the host.
*/
struct k_mem_partition ipc_resp_part = {
.start = ipc_resp_buf,
.size = SOF_IPC_MSG_MAX_SIZE,
.attr = user_get_partition_attr(ipc_resp_buf) | K_MEM_PARTITION_P_RW_U_RW,
};
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->get_configuration)
return -EIO;
params->ext.get_conf.config_id = config_id;
params->ext.get_conf.data_off_size = data_offset_size;
params->ext.get_conf.fragment = fragment;
params->ext.get_conf.fragment_size = fragment_size;
ret = k_mem_domain_add_partition(domain, &ipc_resp_part);
if (ret < 0) {
comp_err(mod->dev, "add response buffer to domain error: %d", ret);
return ret;
}
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_GET_CONF, true);
k_mem_domain_remove_partition(domain, &ipc_resp_part);
/* Return status from module code operation. */
return ret ? ret : params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module set_processing_mode() operation and return its result.
* Module set_processing_mode() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @param mode - processing mode to be set.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_set_processing_mode(struct processing_module *mod,
enum module_processing_mode mode)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->set_processing_mode)
return 0;
params->ext.proc_mode.mode = mode;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_SET_PROCMOD, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module get_processing_mode() operation and return its result.
* Module get_processing_mode() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return processing mode.
*/
static
enum module_processing_mode userspace_proxy_get_processing_mode(struct processing_module *mod)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->get_processing_mode)
return -EIO;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_GET_PROCMOD, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->ext.proc_mode.mode;
}
/**
* Copy parameters to user worker accessible space.
* Queue module is_ready_to_process() operation and return its result.
* Module is_ready_to_process() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return true if the module is ready to process
*/
static bool userspace_proxy_is_ready_to_process(struct processing_module *mod,
struct sof_source **sources,
int num_of_sources,
struct sof_sink **sinks,
int num_of_sinks)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->is_ready_to_process)
return generic_module_is_ready_to_process(mod, sources, num_of_sources, sinks,
num_of_sinks);
params->ext.proc.sources = sources;
params->ext.proc.num_of_sources = num_of_sources;
params->ext.proc.sinks = sinks;
params->ext.proc.num_of_sinks = num_of_sinks;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_PROC_READY, false);
if (ret)
return generic_module_is_ready_to_process(mod, sources, num_of_sources, sinks,
num_of_sinks);
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module bind() operation and return its result.
* Module bind() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @param bind_data - pointer to bind_info structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_bind(struct processing_module *mod, struct bind_info *bind_data)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->bind)
return 0;
params->ext.bind_data = bind_data;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_BIND, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module unbind() operation and return its result.
* Module unbind() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @param unbind_data - pointer to bind_info structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_unbind(struct processing_module *mod, struct bind_info *unbind_data)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret;
comp_dbg(mod->dev, "start");
if (!mod->user_ctx->interface->unbind)
return 0;
params->ext.bind_data = unbind_data;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_UNBIND, false);
if (ret)
return ret;
/* Return status from module code operation. */
return params->status;
}
/**
* Copy parameters to user worker accessible space.
* Queue module trigger() operation and return its result.
* Module trigger() code is performed in user workqueue.
*
* @param mod - pointer to processing module structure.
* @return 0 for success, error otherwise.
*/
static int userspace_proxy_trigger(struct processing_module *mod, int cmd)
{
struct module_params *params = user_work_get_params(mod->user_ctx);
int ret = 0;
comp_dbg(mod->dev, "start");
if (mod->user_ctx->interface->trigger) {
params->ext.trigger_data = cmd;
ret = userspace_proxy_invoke(mod->user_ctx, USER_PROXY_MOD_CMD_TRIGGER, false);
if (ret)
return ret;
ret = params->status;
}
if (!ret)
ret = module_adapter_set_state(mod, mod->dev, cmd);
/* Return status from module code operation. */
return ret;
}
/* Userspace Proxy Module API */
APP_TASK_DATA static const struct module_interface userspace_proxy_interface = {
.init = userspace_proxy_init,
.is_ready_to_process = userspace_proxy_is_ready_to_process,
.prepare = userspace_proxy_prepare,
.process = userspace_proxy_process,
.set_configuration = userspace_proxy_set_configuration,
.get_configuration = userspace_proxy_get_configuration,
.set_processing_mode = userspace_proxy_set_processing_mode,
.get_processing_mode = userspace_proxy_get_processing_mode,
.reset = userspace_proxy_reset,
.free = userspace_proxy_free,
.bind = userspace_proxy_bind,
.unbind = userspace_proxy_unbind,
.trigger = userspace_proxy_trigger,
};