-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathedf_schedule.c
More file actions
344 lines (261 loc) · 7.77 KB
/
edf_schedule.c
File metadata and controls
344 lines (261 loc) · 7.77 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2017 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Tomasz Lauda <tomasz.lauda@linux.intel.com>
#include <sof/common.h>
#include <rtos/panic.h>
#include <rtos/interrupt.h>
#include <rtos/timer.h>
#include <rtos/alloc.h>
#include <rtos/clk.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/platform.h>
#include <sof/schedule/edf_schedule.h>
#include <sof/schedule/schedule.h>
#include <rtos/task.h>
#include <rtos/sof.h>
#include <ipc/topology.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
SOF_DEFINE_REG_UUID(edf_sched);
DECLARE_TR_CTX(edf_tr, SOF_UUID(edf_sched_uuid), LOG_LEVEL_INFO);
struct edf_schedule_data {
struct list_item list; /* list of tasks in priority queue */
uint32_t clock;
int irq;
};
static const struct scheduler_ops schedule_edf_ops;
static int schedule_edf_task_complete(void *data, struct task *task);
static int schedule_edf_task_running(void *data, struct task *task);
static void schedule_edf(struct edf_schedule_data *edf_sch);
static void schedule_edf_task_run(struct task *task, void *data)
{
while (1) {
/* execute task run function and remove task from the list
* only if completed
*/
if (task_run(task) == SOF_TASK_STATE_COMPLETED)
schedule_edf_task_complete(data, task);
/* find new task for execution */
schedule_edf(data);
}
}
static void edf_scheduler_run(void *data)
{
struct edf_schedule_data *edf_sch = data;
uint64_t deadline_next = SOF_TASK_DEADLINE_IDLE;
struct task *task_next = NULL;
struct list_item *tlist;
struct task *task;
uint64_t deadline;
uint32_t flags;
tr_dbg(&edf_tr, "edf_scheduler_run()");
irq_local_disable(flags);
/* find next task to run */
list_for_item(tlist, &edf_sch->list) {
task = container_of(tlist, struct task, list);
if (task->state != SOF_TASK_STATE_QUEUED &&
task->state != SOF_TASK_STATE_RUNNING)
continue;
deadline = task_get_deadline(task);
if (deadline == SOF_TASK_DEADLINE_NOW) {
/* task needs to be scheduled ASAP */
task_next = task;
break;
}
/* get earliest deadline */
if (deadline <= deadline_next) {
deadline_next = deadline;
task_next = task;
}
}
irq_local_enable(flags);
/* schedule next pending task */
if (task_next)
schedule_edf_task_running(data, task_next);
}
static int schedule_edf_task(void *data, struct task *task, uint64_t start,
uint64_t period)
{
struct edf_schedule_data *edf_sch = data;
uint32_t flags;
(void) period; /* not used */
(void) start; /* not used */
irq_local_disable(flags);
/* not enough MCPS to complete */
if (task->state == SOF_TASK_STATE_QUEUED ||
task->state == SOF_TASK_STATE_RUNNING) {
tr_err(&edf_tr, "schedule_edf_task(), task already queued or running %d",
task->state);
irq_local_enable(flags);
return -EALREADY;
}
/* add task to the list */
list_item_append(&task->list, &edf_sch->list);
task->state = SOF_TASK_STATE_QUEUED;
irq_local_enable(flags);
schedule_edf(edf_sch);
return 0;
}
int schedule_task_init_edf(struct task *task, const struct sof_uuid_entry *uid,
const struct task_ops *ops,
void *data, uint16_t core, uint32_t flags)
{
struct edf_task_pdata *edf_pdata = NULL;
int ret;
ret = schedule_task_init(task, uid, SOF_SCHEDULE_EDF, 0, ops->run, data,
core, flags);
if (ret < 0)
return ret;
if (edf_sch_get_pdata(task))
return -EEXIST;
edf_pdata = rzalloc(SOF_MEM_FLAG_KERNEL,
sizeof(*edf_pdata));
if (!edf_pdata) {
tr_err(&edf_tr, "schedule_task_init_edf(): alloc failed");
return -ENOMEM;
}
edf_sch_set_pdata(task, edf_pdata);
task->ops.complete = ops->complete;
task->ops.get_deadline = ops->get_deadline;
if (task_context_alloc(&edf_pdata->ctx) < 0)
goto error;
if (task_context_init(edf_pdata->ctx, &schedule_edf_task_run,
task, scheduler_get_data(SOF_SCHEDULE_EDF),
task->core, NULL, 0) < 0)
goto error;
/* flush for secondary core */
if (!cpu_is_primary(task->core))
dcache_writeback_invalidate_region(edf_pdata,
sizeof(*edf_pdata));
return 0;
error:
tr_err(&edf_tr, "schedule_task_init_edf(): init context failed");
if (edf_pdata->ctx)
task_context_free(edf_pdata->ctx);
rfree(edf_pdata);
edf_sch_set_pdata(task, NULL);
return -EINVAL;
}
static int schedule_edf_task_running(void *data, struct task *task)
{
struct edf_task_pdata *edf_pdata = edf_sch_get_pdata(task);
uint32_t flags;
tr_dbg(&edf_tr, "schedule_edf_task_running()");
irq_local_disable(flags);
task_context_set(edf_pdata->ctx);
task->state = SOF_TASK_STATE_RUNNING;
irq_local_enable(flags);
return 0;
}
static int schedule_edf_task_complete(void *data, struct task *task)
{
uint32_t flags;
tr_dbg(&edf_tr, "schedule_edf_task_complete()");
irq_local_disable(flags);
task_complete(task);
task->state = SOF_TASK_STATE_COMPLETED;
list_item_del(&task->list);
irq_local_enable(flags);
return 0;
}
static int schedule_edf_task_cancel(void *data, struct task *task)
{
uint32_t flags;
tr_dbg(&edf_tr, "schedule_edf_task_cancel()");
irq_local_disable(flags);
/* cancel and delete only if queued */
if (task->state == SOF_TASK_STATE_QUEUED) {
task->state = SOF_TASK_STATE_CANCEL;
list_item_del(&task->list);
}
irq_local_enable(flags);
return 0;
}
static int schedule_edf_task_free(void *data, struct task *task)
{
struct edf_task_pdata *edf_pdata = edf_sch_get_pdata(task);
uint32_t flags;
irq_local_disable(flags);
task->state = SOF_TASK_STATE_FREE;
task_context_free(edf_pdata->ctx);
edf_pdata->ctx = NULL;
rfree(edf_pdata);
edf_sch_set_pdata(task, NULL);
irq_local_enable(flags);
return 0;
}
int scheduler_init_edf(void)
{
struct edf_schedule_data *edf_sch;
int ret;
tr_info(&edf_tr, "edf_scheduler_init()");
edf_sch = rzalloc(SOF_MEM_FLAG_KERNEL,
sizeof(*edf_sch));
if (!edf_sch) {
tr_err(&edf_tr, "scheduler_init_edf(): allocation failed");
return -ENOMEM;
}
list_init(&edf_sch->list);
edf_sch->clock = PLATFORM_DEFAULT_CLOCK;
scheduler_init(SOF_SCHEDULE_EDF, &schedule_edf_ops, edf_sch);
/* initialize main task context before enabling interrupt */
task_main_init();
/* configure EDF scheduler interrupt */
ret = interrupt_get_irq(PLATFORM_SCHEDULE_IRQ,
PLATFORM_SCHEDULE_IRQ_NAME);
if (ret < 0) {
free(edf_sch);
return ret;
}
edf_sch->irq = ret;
interrupt_register(edf_sch->irq, edf_scheduler_run, edf_sch);
interrupt_enable(edf_sch->irq, edf_sch);
return ret;
}
static void scheduler_free_edf(void *data, uint32_t flags)
{
struct edf_schedule_data *edf_sch = data;
uint32_t irq_flags;
irq_local_disable(irq_flags);
/* disable and unregister EDF scheduler interrupt */
interrupt_disable(edf_sch->irq, edf_sch);
interrupt_unregister(edf_sch->irq, edf_sch);
if (!(flags & SOF_SCHEDULER_FREE_IRQ_ONLY))
/* free main task context */
task_main_free();
irq_local_enable(irq_flags);
}
static int scheduler_restore_edf(void *data)
{
struct edf_schedule_data *edf_sch = data;
uint32_t flags;
irq_local_disable(flags);
edf_sch->irq = interrupt_get_irq(PLATFORM_SCHEDULE_IRQ,
PLATFORM_SCHEDULE_IRQ_NAME);
if (edf_sch->irq < 0) {
tr_err(&edf_tr, "scheduler_restore_edf(): getting irq failed.");
return edf_sch->irq;
}
interrupt_register(edf_sch->irq, edf_scheduler_run, edf_sch);
interrupt_enable(edf_sch->irq, edf_sch);
irq_local_enable(flags);
return 0;
}
static void schedule_edf(struct edf_schedule_data *edf_sch)
{
interrupt_set(edf_sch->irq);
}
static const struct scheduler_ops schedule_edf_ops = {
.schedule_task = schedule_edf_task,
.schedule_task_running = schedule_edf_task_running,
.reschedule_task = NULL,
.schedule_task_cancel = schedule_edf_task_cancel,
.schedule_task_free = schedule_edf_task_free,
.scheduler_free = scheduler_free_edf,
.scheduler_restore = scheduler_restore_edf,
};