forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzephyr_dp_schedule.c
More file actions
519 lines (458 loc) · 15.8 KB
/
zephyr_dp_schedule.c
File metadata and controls
519 lines (458 loc) · 15.8 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
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright(c) 2023 Intel Corporation. All rights reserved.
*
* Author: Marcin Szkudlinski
*/
#include <sof/audio/component.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <rtos/task.h>
#include <stdint.h>
#include <sof/schedule/dp_schedule.h>
#include <sof/schedule/ll_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <sof/trace/trace.h>
#include <rtos/wait.h>
#include <rtos/interrupt.h>
#include <zephyr/kernel.h>
#include <zephyr/sys_clock.h>
#include <sof/lib/notifier.h>
#include <zephyr/kernel/thread.h>
LOG_MODULE_REGISTER(dp_schedule, CONFIG_SOF_LOG_LEVEL);
/* 87858bc2-baa9-40b6-8e4c-2c95ba8b1545 */
DECLARE_SOF_UUID("dp-schedule", dp_sched_uuid, 0x87858bc2, 0xbaa9, 0x40b6,
0x8e, 0x4c, 0x2c, 0x95, 0xba, 0x8b, 0x15, 0x45);
DECLARE_TR_CTX(dp_tr, SOF_UUID(dp_sched_uuid), LOG_LEVEL_INFO);
struct scheduler_dp_data {
struct list_item tasks; /* list of active dp tasks */
struct task ll_tick_src; /* LL task - source of DP tick */
};
struct task_dp_pdata {
k_tid_t thread_id; /* zephyr thread ID */
uint32_t period_clock_ticks; /* period the task should be scheduled in Zephyr ticks */
k_thread_stack_t __sparse_cache *p_stack; /* pointer to thread stack */
struct k_sem sem; /* semaphore for task scheduling */
struct processing_module *mod; /* the module to be scheduled */
};
/* Single CPU-wide lock
* as each per-core instance if dp-scheduler has separate structures, it is enough to
* use irq_lock instead of cross-core spinlocks
*/
static inline unsigned int scheduler_dp_lock(void)
{
return irq_lock();
}
static inline void scheduler_dp_unlock(unsigned int key)
{
irq_unlock(key);
}
/* dummy LL task - to start LL on secondary cores */
static enum task_state scheduler_dp_ll_tick_dummy(void *data)
{
return SOF_TASK_STATE_RESCHEDULE;
}
/*
* function called after every LL tick
*
* This function checks if the queued DP tasks are ready to processing (meaning
* the module run by the task has enough data at all sources and enough free space
* on all sinks)
*
* if the task becomes ready, a deadline is set allowing Zephyr to schedule threads
* in right order
*
* TODO: currently there's a limitation - DP module must be surrounded by LL modules.
* it simplifies algorithm - there's no need to browse through DP chains calculating
* deadlines for each module in function of all modules execution status.
* Now is simple - modules deadline is its start + tick time.
*
* example:
* Lets assume we do have a pipeline:
*
* LL1 -> DP1 -> LL2 -> DP2 -> LL3 -> DP3 -> LL4
*
* all LLs starts in 1ms tick
*
* for simplification lets assume
* - all LLs are on primary core, all DPs on secondary (100% CPU is for DP)
* - context switching requires 0 cycles
*
* DP1 - starts every 1ms, needs 0.5ms to finish processing
* DP2 - starts every 2ms, needs 0.6ms to finish processing
* DP3 - starts every 10ms, needs 0.3ms to finish processing
*
* TICK0
* only LL1 is ready to run
* LL1 processing (producing data chunk for DP1)
*
* TICK1
* LL1 is ready to run
* DP1 is ready tu run (has data from LL1) set deadline to TICK2
* LL1 processing (producing second data chunk for DP1)
* DP1 processing for 0.5ms (consuming first data chunk, producing data chunk for LL2)
* CPU is idle for 0.5ms
*
* TICK2
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK3
* LL2 is ready to run
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing 50% data chunk for DP2)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* CPU is idle for 0.5ms
*
* TICK3
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK4
* LL2 is ready to run
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing rest of data chunk for DP2)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* CPU is idle for 0.5ms
*
* TICK4
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK5
* LL2 is ready to run
* DP2 is ready to run set deadline to TICK6
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing 50% of second data chunk for DP2)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.5ms (no data produced as DP2 has 0.1ms to go)
* 100% CPU used
*
* !!!!!! Note here - DP1 must do before DP2 as it MUST finish in this tick. DP2 can wait
* >>>>>>> this is what we call EDF - EARIEST DEADLINE FIRST <<<<<<
*
* TICK5
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK6
* LL2 is ready to run
* DP2 is in progress, deadline is set to TICK6
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing rest of second data chunk for DP2)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.1ms (producing TWO data chunks for LL3)
* CPU is idle for 0.4ms (60% used)
*
* TICK6
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK7
* LL2 is ready to run
* DP2 is ready to run set deadline to TICK8
* LL3 is ready to run
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing 50% of second data chunk for DP2)
* LL3 processing (producing 10% of first data chunk for DP3)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.5ms (no data produced as DP2 has 0.1ms to go)
* 100% CPU used
*
*
*
* (........ 9 more cycles - LL3 procuces 100% of data for DP3......)
*
*
* TICK15
* LL1 is ready to run
* DP1 is ready tu run set deadline to TICK16
* LL2 is ready to run
* DP2 is ready to run set deadline to TICK17
* LL3 is ready to run
* DP3 is ready to run set deadline to TICK25
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing 50% of data chunk for DP2)
* LL3 processing (producing 10% of second data chunk for DP3)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.5ms (no data produced as DP2 has 0.1ms to go)
* 100% CPU used -
* !!! note that DP3 is ready but has no chance to get CPU in this cycle
*
* TICK16
* LL1 is ready to run set deadline to TICK17
* DP1 is ready tu run
* LL2 is ready to run
* DP2 is in progress, deadline is set to TICK17
* LL3 is ready to run
* DP3 is in progress, deadline is set to TICK25
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing rest of data chunk for DP2)
* LL3 processing (producing 10% of second data chunk for DP3)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.1ms (producing data)
* DP3 processing for 0.2ms (producing 10 data chunks for LL4)
* 90% CPU used
*
* TICK17
* LL1 is ready to run
* DP1 is ready tu run
* LL2 is ready to run
* DP2 is ready to run
* LL3 is ready to run
* LL4 is ready to run
* !! NOTE that DP3 is not ready - it will be ready again in TICK25
* LL1 processing (producing data chunk for DP1)
* LL2 processing (producing rest of data chunk for DP2)
* LL3 processing (producing next 10% of second data chunk for DP3)
* LL4 processing (consuming 10% of data prepared by DP3)
* DP1 processing for 0.5ms (producing data chunk for LL2)
* DP2 processing for 0.5ms (no data produced as DP2 has 0.1ms to go)
* 100% CPU used
*
*
* Now - pipeline is in stable state, CPU used almost in 100% (it would be 100% if DP3
* needed 1.2ms for processing - but the example would be too complicated)
*/
void scheduler_dp_ll_tick(void *receiver_data, enum notify_id event_type, void *caller_data)
{
(void)receiver_data;
(void)event_type;
(void)caller_data;
struct list_item *tlist;
struct task *curr_task;
struct task_dp_pdata *pdata;
unsigned int lock_key;
struct scheduler_dp_data *dp_sch = scheduler_get_data(SOF_SCHEDULE_DP);
lock_key = scheduler_dp_lock();
list_for_item(tlist, &dp_sch->tasks) {
curr_task = container_of(tlist, struct task, list);
/* step 1 - check if the module is ready for processing */
if (curr_task->state == SOF_TASK_STATE_QUEUED) {
pdata = curr_task->priv_data;
struct processing_module *mod = pdata->mod;
bool mod_ready;
mod_ready = module_is_ready_to_process(mod, mod->sources,
mod->num_of_sources,
mod->sinks,
mod->num_of_sinks);
if (mod_ready) {
/* set a deadline for given num of ticks, starting now */
k_thread_deadline_set(pdata->thread_id, pdata->period_clock_ticks);
/* trigger the task */
curr_task->state = SOF_TASK_STATE_RUNNING;
k_sem_give(&pdata->sem);
}
}
}
scheduler_dp_unlock(lock_key);
}
static int scheduler_dp_task_cancel(void *data, struct task *task)
{
unsigned int lock_key;
struct scheduler_dp_data *dp_sch = (struct scheduler_dp_data *)data;
/* this is asyn cancel - mark the task as canceled and remove it from scheduling */
lock_key = scheduler_dp_lock();
task->state = SOF_TASK_STATE_CANCEL;
list_item_del(&task->list);
/* if there're no more DP task, stop LL tick source */
if (list_is_empty(&dp_sch->tasks))
schedule_task_cancel(&dp_sch->ll_tick_src);
scheduler_dp_unlock(lock_key);
return 0;
}
static int scheduler_dp_task_free(void *data, struct task *task)
{
struct task_dp_pdata *pdata = task->priv_data;
scheduler_dp_task_cancel(data, task);
/* abort the execution of the thread */
k_thread_abort(pdata->thread_id);
/* free task stack */
rfree((__sparse_force void *)pdata->p_stack);
/* all other memory has been allocated as a single malloc, will be freed later by caller */
return 0;
}
/* Thread function called in component context, on target core */
static void dp_thread_fn(void *p1, void *p2, void *p3)
{
struct task *task = p1;
(void)p2;
(void)p3;
struct task_dp_pdata *task_pdata = task->priv_data;
unsigned int lock_key;
enum task_state state;
while (1) {
/*
* the thread is started immediately after creation, it will stop on semaphore
* Semaphore will be released once the task is ready to process
*/
k_sem_take(&task_pdata->sem, K_FOREVER);
if (task->state == SOF_TASK_STATE_RUNNING)
state = task_run(task);
else
state = task->state; /* to avoid undefined variable warning */
lock_key = scheduler_dp_lock();
/*
* check if task is still running, may have been canceled by external call
* if not, set the state returned by run procedure
*/
if (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:
/* remove from scheduling */
list_item_del(&task->list);
break;
default:
/* illegal state, serious defect, won't happen */
k_panic();
}
}
/* call task_complete */
if (task->state == SOF_TASK_STATE_COMPLETED) {
/* call task_complete out of lock, it may eventually call schedule again */
scheduler_dp_unlock(lock_key);
task_complete(task);
} else {
scheduler_dp_unlock(lock_key);
}
};
/* never be here */
}
static int scheduler_dp_task_shedule(void *data, struct task *task, uint64_t start,
uint64_t period)
{
struct scheduler_dp_data *dp_sch = (struct scheduler_dp_data *)data;
struct task_dp_pdata *pdata = task->priv_data;
unsigned int lock_key;
uint64_t period_clock_ticks;
lock_key = scheduler_dp_lock();
if (task->state != SOF_TASK_STATE_INIT &&
task->state != SOF_TASK_STATE_CANCEL &&
task->state != SOF_TASK_STATE_COMPLETED) {
scheduler_dp_unlock(lock_key);
return -EINVAL;
}
/* if there's no DP tasks scheduled yet, run ll tick source task */
if (list_is_empty(&dp_sch->tasks))
schedule_task(&dp_sch->ll_tick_src, 0, 0);
/* add a task to DP scheduler list */
task->state = SOF_TASK_STATE_QUEUED;
list_item_prepend(&task->list, &dp_sch->tasks);
period_clock_ticks = period * CONFIG_SYS_CLOCK_TICKS_PER_SEC;
/* period is in us - convert to seconds in next step
* or it always will be zero because of fixed point calculation
*/
period_clock_ticks /= 1000000;
pdata->period_clock_ticks = period_clock_ticks;
scheduler_dp_unlock(lock_key);
tr_dbg(&dp_tr, "DP task scheduled with period %u [us]", (uint32_t)period);
return 0;
}
static struct scheduler_ops schedule_dp_ops = {
.schedule_task = scheduler_dp_task_shedule,
.schedule_task_cancel = scheduler_dp_task_cancel,
.schedule_task_free = scheduler_dp_task_free,
};
int scheduler_dp_init(void)
{
int ret;
struct scheduler_dp_data *dp_sch = rzalloc(SOF_MEM_ZONE_SYS_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(struct scheduler_dp_data));
if (!dp_sch)
return -ENOMEM;
list_init(&dp_sch->tasks);
scheduler_init(SOF_SCHEDULE_DP, &schedule_dp_ops, dp_sch);
/* init src of DP tick */
ret = schedule_task_init_ll(&dp_sch->ll_tick_src,
SOF_UUID(dp_sched_uuid),
SOF_SCHEDULE_LL_TIMER,
0, scheduler_dp_ll_tick_dummy, dp_sch,
cpu_get_id(), 0);
if (ret)
return ret;
notifier_register(NULL, NULL, NOTIFIER_ID_LL_POST_RUN, scheduler_dp_ll_tick, 0);
return 0;
}
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 task_priority)
{
void __sparse_cache *p_stack = NULL;
/* memory allocation helper structure */
struct {
struct task task;
struct task_dp_pdata pdata;
struct k_thread thread;
} *task_memory;
k_tid_t thread_id = NULL;
int ret;
/* must be called on the same core the task will be binded 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 = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
sizeof(*task_memory));
if (!task_memory) {
tr_err(&dp_tr, "zephyr_dp_task_init(): memory alloc failed");
return -ENOMEM;
}
/* allocate stack - must be aligned and cached so a separate alloc */
stack_size = Z_KERNEL_STACK_SIZE_ADJUST(stack_size);
p_stack = (__sparse_force void __sparse_cache *)
rballoc_align(0, SOF_MEM_CAPS_RAM, stack_size, Z_KERNEL_STACK_OBJ_ALIGN);
if (!p_stack) {
tr_err(&dp_tr, "zephyr_dp_task_init(): stack alloc failed");
ret = -ENOMEM;
goto err;
}
/* create a zephyr thread for the task */
thread_id = k_thread_create(&task_memory->thread, (__sparse_force void *)p_stack,
stack_size, dp_thread_fn, &task_memory->task, NULL, NULL,
task_priority, K_USER, K_FOREVER);
if (!thread_id) {
ret = -EFAULT;
tr_err(&dp_tr, "zephyr_dp_task_init(): zephyr thread create failed");
goto err;
}
/* pin the thread to specific core */
ret = k_thread_cpu_pin(thread_id, core);
if (ret < 0) {
ret = -EFAULT;
tr_err(&dp_tr, "zephyr_dp_task_init(): zephyr task pin to core failed");
goto err;
}
/* internal SOF task init */
ret = schedule_task_init(&task_memory->task, uid, SOF_SCHEDULE_DP, 0, ops->run,
mod, core, 0);
if (ret < 0) {
tr_err(&dp_tr, "zephyr_dp_task_init(): schedule_task_init failed");
goto err;
}
/* initialize other task structures */
task_memory->task.ops.complete = ops->complete;
task_memory->task.ops.get_deadline = ops->get_deadline;
task_memory->task.state = SOF_TASK_STATE_INIT;
task_memory->task.core = core;
/* initialize semaprhore */
k_sem_init(&task_memory->pdata.sem, 0, 1);
/* success, fill the structures */
task_memory->task.priv_data = &task_memory->pdata;
task_memory->pdata.thread_id = thread_id;
task_memory->pdata.p_stack = p_stack;
task_memory->pdata.mod = mod;
*task = &task_memory->task;
/* start the thread - it will immediately stop at a semaphore */
k_thread_start(thread_id);
return 0;
err:
/* cleanup - free all allocated resources */
if (thread_id)
k_thread_abort(thread_id);
rfree((__sparse_force void *)p_stack);
rfree(task_memory);
return ret;
}