-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathzephyr_dp_schedule_application.c
More file actions
592 lines (498 loc) · 16.4 KB
/
zephyr_dp_schedule_application.c
File metadata and controls
592 lines (498 loc) · 16.4 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
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright(c) 2025 Intel Corporation. All rights reserved.
*
* Author: Marcin Szkudlinski
*/
#include <rtos/task.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/common.h>
#include <sof/list.h>
#include <sof/llext_manager.h>
#include <sof/objpool.h>
#include <sof/schedule/dp_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <zephyr/app_memory/mem_domain.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/slist.h>
#include <stdbool.h>
#include <stdint.h>
#include "zephyr_dp_schedule.h"
LOG_MODULE_DECLARE(dp_schedule, CONFIG_SOF_LOG_LEVEL);
extern struct tr_ctx dp_tr;
static struct objpool_head dp_mdom_head = {.list = LIST_INIT(dp_mdom_head.list)};
/* Synchronization semaphore for the scheduler thread to wait for DP startup */
#define DP_SYNC_INIT(i, _) Z_SEM_INITIALIZER(dp_sync[i], 0, 1)
#define DP_SYNC_INIT_LIST LISTIFY(CONFIG_CORE_COUNT, DP_SYNC_INIT, (,))
static STRUCT_SECTION_ITERABLE_ARRAY(k_sem, dp_sync, CONFIG_CORE_COUNT) = { DP_SYNC_INIT_LIST };
struct ipc4_flat {
unsigned int cmd;
int ret;
union {
struct {
struct ipc4_module_bind_unbind bu;
enum bind_type type;
} bind;
struct {
unsigned int trigger_cmd;
enum ipc4_pipeline_state state;
int n_sources;
int n_sinks;
struct sof_source *source[CONFIG_MODULE_MAX_CONNECTIONS];
struct sof_sink *sink[CONFIG_MODULE_MAX_CONNECTIONS];
} pipeline_state;
};
};
/* Pack IPC input data */
static int ipc_thread_flatten(unsigned int cmd, const union scheduler_dp_thread_ipc_param *param,
struct ipc4_flat *flat)
{
flat->cmd = cmd;
/*
* FIXME: SOF_IPC4_MOD_* and SOF_IPC4_GLB_* aren't fully orthogonal, but
* so far none of the used ones overlap
*/
switch (cmd) {
case SOF_IPC4_MOD_BIND:
case SOF_IPC4_MOD_UNBIND:
flat->bind.bu = *param->bind_data->ipc4_data;
flat->bind.type = param->bind_data->bind_type;
break;
case SOF_IPC4_GLB_SET_PIPELINE_STATE:
flat->pipeline_state.trigger_cmd = param->pipeline_state.trigger_cmd;
switch (param->pipeline_state.trigger_cmd) {
case COMP_TRIGGER_STOP:
break;
case COMP_TRIGGER_PREPARE:
if (param->pipeline_state.n_sources > CONFIG_MODULE_MAX_CONNECTIONS ||
param->pipeline_state.n_sinks > CONFIG_MODULE_MAX_CONNECTIONS)
return -ENOMEM;
flat->pipeline_state.state = param->pipeline_state.state;
flat->pipeline_state.n_sources = param->pipeline_state.n_sources;
flat->pipeline_state.n_sinks = param->pipeline_state.n_sinks;
/* Up to 2 * CONFIG_MODULE_MAX_CONNECTIONS */
memcpy(flat->pipeline_state.source, param->pipeline_state.sources,
flat->pipeline_state.n_sources *
sizeof(flat->pipeline_state.source[0]));
memcpy(flat->pipeline_state.sink, param->pipeline_state.sinks,
flat->pipeline_state.n_sinks *
sizeof(flat->pipeline_state.sink[0]));
}
}
return 0;
}
/* Unpack IPC data and execute a callback */
static void ipc_thread_unflatten_run(struct processing_module *pmod, struct ipc4_flat *flat)
{
const struct module_interface *const ops = pmod->dev->drv->adapter_ops;
switch (flat->cmd) {
case SOF_IPC4_MOD_BIND:
if (ops->bind) {
struct bind_info bind_data = {
.ipc4_data = &flat->bind.bu,
.bind_type = flat->bind.type,
};
flat->ret = ops->bind(pmod, &bind_data);
} else {
flat->ret = 0;
}
break;
case SOF_IPC4_MOD_UNBIND:
if (ops->unbind) {
struct bind_info bind_data = {
.ipc4_data = &flat->bind.bu,
.bind_type = flat->bind.type,
};
flat->ret = ops->unbind(pmod, &bind_data);
} else {
flat->ret = 0;
}
break;
case SOF_IPC4_MOD_DELETE_INSTANCE:
flat->ret = ops->free(pmod);
break;
case SOF_IPC4_MOD_INIT_INSTANCE:
flat->ret = ops->init(pmod);
break;
case SOF_IPC4_GLB_SET_PIPELINE_STATE:
switch (flat->pipeline_state.trigger_cmd) {
case COMP_TRIGGER_STOP:
flat->ret = ops->reset(pmod);
break;
case COMP_TRIGGER_PREPARE:
flat->ret = ops->prepare(pmod,
flat->pipeline_state.source,
flat->pipeline_state.n_sources,
flat->pipeline_state.sink,
flat->pipeline_state.n_sinks);
}
}
}
#define DP_THREAD_IPC_TIMEOUT K_MSEC(100)
/* Signal an IPC and wait for processing completion */
int scheduler_dp_thread_ipc(struct processing_module *pmod, unsigned int cmd,
const union scheduler_dp_thread_ipc_param *param)
{
struct task_dp_pdata *pdata = pmod->dev->task->priv_data;
int ret;
if (!pmod) {
tr_err(&dp_tr, "no thread module");
return -EINVAL;
}
if (cmd == SOF_IPC4_MOD_INIT_INSTANCE) {
/* Wait for the DP thread to start */
ret = k_sem_take(&dp_sync[pmod->dev->task->core], DP_THREAD_IPC_TIMEOUT);
if (ret < 0) {
tr_err(&dp_tr, "Failed waiting for DP thread to start: %d", ret);
return ret;
}
}
unsigned int lock_key = scheduler_dp_lock(pmod->dev->task->core);
/* IPCs are serialised */
pdata->flat->ret = -ENOSYS;
ret = ipc_thread_flatten(cmd, param, pdata->flat);
if (!ret)
k_event_post(pdata->event, DP_TASK_EVENT_IPC);
scheduler_dp_unlock(lock_key);
if (!ret) {
/* Wait for completion */
ret = k_sem_take(&dp_sync[cpu_get_id()], DP_THREAD_IPC_TIMEOUT);
if (ret < 0)
tr_err(&dp_tr, "Failed waiting for DP thread: %d", ret);
else
ret = pdata->flat->ret;
}
return ret;
}
/* Go through all DP tasks and recalculate their readiness and deadlines
* NOT REENTRANT, called with scheduler_dp_lock() held
*/
void scheduler_dp_recalculate(struct scheduler_dp_data *dp_sch, bool is_ll_post_run)
{
struct list_item *tlist;
struct task *curr_task;
struct task_dp_pdata *pdata;
list_for_item(tlist, &dp_sch->tasks) {
curr_task = container_of(tlist, struct task, list);
pdata = curr_task->priv_data;
struct processing_module *mod = pdata->mod;
bool trigger_task = false;
/* decrease number of LL ticks/cycles left till the module reaches its deadline */
if (mod->dp_startup_delay && is_ll_post_run && pdata->ll_cycles_to_start) {
pdata->ll_cycles_to_start--;
if (!pdata->ll_cycles_to_start)
/* delayed start complete, clear startup delay flag.
* see dp_startup_delay comment for details
*/
mod->dp_startup_delay = false;
}
if (curr_task->state == SOF_TASK_STATE_QUEUED &&
mod->dev->state >= COMP_STATE_ACTIVE) {
/* trigger the task */
curr_task->state = SOF_TASK_STATE_RUNNING;
trigger_task = true;
k_event_post(pdata->event, DP_TASK_EVENT_PROCESS);
}
if (curr_task->state == SOF_TASK_STATE_RUNNING) {
/* (re) calculate deadline for all running tasks */
/* get module deadline in us */
uint32_t deadline = module_get_deadline(mod);
/* if a deadline cannot be calculated, use a fixed value relative to its
* first start
*/
if (deadline >= UINT32_MAX / 2 && trigger_task)
deadline = module_get_lpt(mod);
if (deadline < UINT32_MAX) {
/* round down to 1ms */
deadline = deadline / 1000;
/* calculate number of ticks */
deadline = deadline * (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC / 1000);
/* add to "NOW", overflows are OK */
deadline = dp_sch->last_ll_tick_timestamp + deadline;
/* set in Zephyr. Note that it may be in past, it does not matter,
* Zephyr still will schedule the thread with earlier deadline
* first
*/
k_thread_absolute_deadline_set(pdata->thread_id, deadline);
}
}
}
}
/* Thread function called in component context, on target core */
void dp_thread_fn(void *p1, void *p2, void *p3)
{
struct task *task = p1;
struct task_dp_pdata *task_pdata = task->priv_data;
struct processing_module *pmod = task_pdata->mod;
unsigned int lock_key;
enum task_state state;
bool task_stop;
ARG_UNUSED(p2);
ARG_UNUSED(p3);
/* The IPC thread is waiting for the thread to be started, it can proceed now. */
k_sem_give(&dp_sync[task->core]);
comp_info(pmod->dev, "userspace thread started");
do {
uint32_t mask = k_event_wait_safe(task_pdata->event,
DP_TASK_EVENT_PROCESS | DP_TASK_EVENT_CANCEL |
DP_TASK_EVENT_IPC, false, K_FOREVER);
if (mask & DP_TASK_EVENT_IPC) {
/* handle IPC */
tr_dbg(&dp_tr, "got IPC wake up for %p state %d", pmod, task->state);
ipc_thread_unflatten_run(pmod, task_pdata->flat);
k_sem_give(&dp_sync[task->core]);
}
if (mask & DP_TASK_EVENT_PROCESS) {
bool ready;
if (task->state == SOF_TASK_STATE_RUNNING) {
ready = module_is_ready_to_process(pmod, pmod->sources,
pmod->num_of_sources,
pmod->sinks, pmod->num_of_sinks);
} else {
state = task->state; /* to avoid undefined variable warning */
ready = false;
}
if (ready) {
if (pmod->dp_startup_delay && !task_pdata->ll_cycles_to_start) {
/* first time run - use delayed start */
task_pdata->ll_cycles_to_start =
module_get_lpt(pmod) / LL_TIMER_PERIOD_US;
/* in case LPT < LL cycle - delay at least cycle */
if (!task_pdata->ll_cycles_to_start)
task_pdata->ll_cycles_to_start = 1;
}
state = task_run(task);
}
lock_key = scheduler_dp_lock(task->core);
/*
* check if task is still running, may have been canceled by external call
* if not, set the state returned by run procedure
*/
if (ready && task->state == SOF_TASK_STATE_RUNNING) {
task->state = state;
switch (state) {
case SOF_TASK_STATE_RESCHEDULE:
/* mark to reschedule, schedule time is already calculated */
task->state = SOF_TASK_STATE_QUEUED;
break;
case SOF_TASK_STATE_CANCEL:
case SOF_TASK_STATE_COMPLETED:
/* task already removed from scheduling */
break;
default:
/* illegal state, serious defect, won't happen */
k_oops();
}
} else {
task->state = SOF_TASK_STATE_QUEUED;
}
} else {
lock_key = scheduler_dp_lock(task->core);
}
/* if true exit the while loop, terminate the thread */
task_stop = task->state == SOF_TASK_STATE_COMPLETED ||
task->state == SOF_TASK_STATE_CANCEL;
scheduler_dp_unlock(lock_key);
} while (!task_stop);
/* call task_complete */
if (task->state == SOF_TASK_STATE_COMPLETED)
task_complete(task);
}
/*
* Safe to call with partial successful initialisation,
* k_mem_domain_remove_partition() then just returns -ENOENT
*/
static void scheduler_dp_domain_free(struct task_dp_pdata *pdata)
{
struct processing_module *pmod = pdata->mod;
struct k_mem_domain *mdom = pmod->mdom;
llext_manager_rm_domain(pmod->dev->ipc_config.id, mdom);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_HEAP);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_HEAP_CACHE);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_CFG);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_CFG_CACHE);
if (mdom->num_partitions) {
/* Violation: all partitions should be freed by now */
unsigned int n = mdom->num_partitions;
LOG_WRN("%u domain partition(s) not freed, force-freeing them all", n);
for (unsigned int i = 0; i < arch_mem_domain_max_partitions_get() && n; i++) {
struct k_mem_partition *dpart = &mdom->partitions[i];
if (dpart->size) {
k_mem_domain_remove_partition(mdom, dpart);
n--;
}
}
}
/* All partitions removed, the domain can be freed now */
pmod->mdom = NULL;
k_mem_domain_deinit(mdom);
objpool_free(&dp_mdom_head, mdom);
}
/* memory allocation helper structure */
struct scheduler_dp_task_memory {
struct task task;
struct task_dp_pdata pdata;
struct comp_driver drv;
struct ipc4_flat flat;
};
void scheduler_dp_internal_free(struct task *task)
{
struct task_dp_pdata *pdata = task->priv_data;
k_object_free(pdata->event);
k_object_free(pdata->thread);
scheduler_dp_domain_free(pdata);
mod_free(pdata->mod, container_of(task, struct scheduler_dp_task_memory, task));
}
/* Called only in IPC context */
int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
const struct task_ops *ops, struct processing_module *mod,
uint16_t core, size_t stack_size, uint32_t options)
{
k_thread_stack_t *p_stack;
struct scheduler_dp_task_memory *task_memory;
int ret;
/* must be called on the same core the task will be bound to */
assert(cpu_get_id() == core);
/*
* allocate memory
* to avoid multiple malloc operations allocate all required memory as a single structure
* and return pointer to task_memory->task
* As the structure contains zephyr kernel specific data, it must be located in
* shared, non cached memory
*/
task_memory = mod_alloc_ext(mod, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(*task_memory), 0);
if (!task_memory) {
tr_err(&dp_tr, "memory alloc failed");
return -ENOMEM;
}
memset(task_memory, 0, sizeof(*task_memory));
task_memory->drv = *mod->dev->drv;
mod->dev->drv = &task_memory->drv;
/* allocate stack - must be aligned and cached so a separate alloc */
p_stack = user_stack_allocate(stack_size, options);
if (!p_stack) {
tr_err(&dp_tr, "stack alloc failed");
ret = -ENOMEM;
goto e_tmem;
}
struct task *ptask = &task_memory->task;
/* internal SOF task init */
ret = schedule_task_init(ptask, uid, SOF_SCHEDULE_DP, 0, ops->run, mod, core, options);
if (ret < 0) {
tr_err(&dp_tr, "schedule_task_init failed");
goto e_stack;
}
struct task_dp_pdata *pdata = &task_memory->pdata;
pdata->flat = &task_memory->flat;
pdata->event = k_object_alloc(K_OBJ_EVENT);
if (!pdata->event) {
tr_err(&dp_tr, "Event object allocation failed");
ret = -ENOMEM;
goto e_stack;
}
pdata->thread = k_object_alloc(K_OBJ_THREAD);
if (!pdata->thread) {
tr_err(&dp_tr, "Thread object allocation failed");
ret = -ENOMEM;
goto e_kobj;
}
memset(&pdata->thread->arch, 0, sizeof(pdata->thread->arch));
/* success, fill the structures */
pdata->p_stack = p_stack;
pdata->mod = mod;
/* initialize other task structures */
ptask->ops.complete = ops->complete;
ptask->ops.get_deadline = ops->get_deadline;
ptask->priv_data = pdata;
list_init(&ptask->list);
*task = ptask;
/* create a zephyr thread for the task */
pdata->thread_id = k_thread_create(pdata->thread, p_stack,
stack_size, dp_thread_fn, ptask, NULL, NULL,
CONFIG_DP_THREAD_PRIORITY, ptask->flags, K_FOREVER);
/* pin the thread to specific core */
ret = k_thread_cpu_pin(pdata->thread_id, core);
if (ret < 0) {
tr_err(&dp_tr, "zephyr task pin to core failed");
goto e_thread;
}
k_thread_access_grant(pdata->thread_id, pdata->event, &dp_sync[core]);
scheduler_dp_grant(pdata->thread_id, core);
unsigned int pidx;
size_t size;
uintptr_t start;
struct k_mem_domain *mdom = objpool_alloc(&dp_mdom_head, sizeof(*mdom),
SOF_MEM_FLAG_COHERENT);
if (!mdom) {
tr_err(&dp_tr, "objpool allocation failed");
ret = -ENOMEM;
goto e_thread;
}
mod->mdom = mdom;
if (!mdom->arch.ptables) {
ret = k_mem_domain_init(mdom, 0, NULL);
if (ret < 0)
goto e_dom;
}
/* Module heap partition */
mod_heap_info(mod, &size, &start);
pdata->mpart[SOF_DP_PART_HEAP] = (struct k_mem_partition){
.start = start,
.size = size,
.attr = K_MEM_PARTITION_P_RW_U_RW,
};
pdata->mpart[SOF_DP_PART_HEAP_CACHE] = (struct k_mem_partition){
.start = (uintptr_t)sys_cache_cached_ptr_get((void *)start),
.size = size,
.attr = K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB,
};
/* Host mailbox partition for additional IPC parameters: read-only */
pdata->mpart[SOF_DP_PART_CFG] = (struct k_mem_partition){
.start = (uintptr_t)sys_cache_uncached_ptr_get((void *)MAILBOX_HOSTBOX_BASE),
.size = 4096,
.attr = K_MEM_PARTITION_P_RO_U_RO,
};
pdata->mpart[SOF_DP_PART_CFG_CACHE] = (struct k_mem_partition){
.start = (uintptr_t)MAILBOX_HOSTBOX_BASE,
.size = 4096,
.attr = K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB,
};
for (pidx = 0; pidx < SOF_DP_PART_TYPE_COUNT; pidx++) {
ret = k_mem_domain_add_partition(mdom, pdata->mpart + pidx);
if (ret < 0)
goto e_dom;
}
ret = llext_manager_add_domain(mod->dev->ipc_config.id, mdom);
if (ret < 0) {
tr_err(&dp_tr, "failed to add LLEXT to domain %d", ret);
goto e_dom;
}
/*
* Keep this call last, able to fail, otherwise domain will be removed
* before its thread
*/
ret = k_mem_domain_add_thread(mdom, pdata->thread_id);
if (ret < 0) {
tr_err(&dp_tr, "failed to add thread to domain %d", ret);
goto e_dom;
}
/* start the thread, it should immediately stop at the semaphore */
k_event_init(pdata->event);
k_thread_start(pdata->thread_id);
return 0;
e_dom:
scheduler_dp_domain_free(pdata);
e_thread:
k_thread_abort(pdata->thread_id);
e_kobj:
/* k_object_free looks for a pointer in the list, any invalid value can be passed */
k_object_free(pdata->thread);
k_object_free(pdata->event);
e_stack:
user_stack_free(p_stack);
e_tmem:
mod_free(mod, task_memory);
return ret;
}