forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_domain.c
More file actions
345 lines (280 loc) · 9.49 KB
/
Copy pathtimer_domain.c
File metadata and controls
345 lines (280 loc) · 9.49 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Tomasz Lauda <tomasz.lauda@linux.intel.com>
#include <sof/drivers/timer.h>
#include <sof/lib/alloc.h>
#include <sof/lib/cpu.h>
#include <sof/lib/memory.h>
#include <sof/math/numbers.h>
#include <sof/platform.h>
#include <sof/schedule/ll_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <sof/schedule/schedule.h>
#include <sof/schedule/task.h>
#include <ipc/topology.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __ZEPHYR__
#include <kernel.h>
#include <sys_clock.h>
/*
* Currently the Zephyr clock rate is part it's Kconfig known at build time.
* SOF on Intel CAVS platforms currently only aligns with Zephyr when both
* use the CAVS 19.2 MHz SSP clock. TODO - needs runtime alignment.
*/
#if CONFIG_XTENSA && !CONFIG_CAVS_TIMER
#error "Zephyr uses 19.2MHz clock derived from SSP which must be enabled."
#endif
#define ZEPHYR_LL_WORKQ_SIZE 8192
#define CYC_PER_TICK (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC \
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
/* zephyr alignment to nearest kernel tick */
#define ZEPHYR_SCHED_COST CYC_PER_TICK
K_THREAD_STACK_DEFINE(ll_workq_stack0, ZEPHYR_LL_WORKQ_SIZE);
#if CONFIG_CORE_COUNT > 1
K_THREAD_STACK_DEFINE(ll_workq_stack1, ZEPHYR_LL_WORKQ_SIZE);
#endif
#if CONFIG_CORE_COUNT > 2
K_THREAD_STACK_DEFINE(ll_workq_stack2, ZEPHYR_LL_WORKQ_SIZE);
#endif
#if CONFIG_CORE_COUNT > 3
K_THREAD_STACK_DEFINE(ll_workq_stack3, ZEPHYR_LL_WORKQ_SIZE);
#endif
#endif
#define LL_TIMER_SET_OVERHEAD_TICKS 1000 /* overhead/delay to set the tick, in ticks */
struct timer_domain {
#ifdef __ZEPHYR__
struct k_work_q ll_workq[CONFIG_CORE_COUNT];
int ll_workq_registered[CONFIG_CORE_COUNT];
#endif
struct timer *timer;
void *arg[CONFIG_CORE_COUNT];
uint64_t timeout; /* in ticks */
};
#ifdef __ZEPHYR__
struct timer_zdata {
struct k_delayed_work work;
void (*handler)(void *arg);
void *arg;
};
struct timer_zdata zdata[CONFIG_CORE_COUNT];
#endif
const struct ll_schedule_domain_ops timer_domain_ops;
static inline void timer_report_delay(int id, uint64_t delay)
{
uint32_t ll_delay_us = (delay * 1000) /
clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, 1);
#ifdef __ZEPHYR__
/* Zephyr schedules on nearest kernel tick not HW cycle */
if (delay <= CYC_PER_TICK)
return;
#endif
if (delay <= UINT_MAX)
tr_err(&ll_tr, "timer_report_delay(): timer %d delayed by %d uS %d ticks",
id, ll_delay_us, (unsigned int)delay);
else
tr_err(&ll_tr, "timer_report_delay(): timer %d delayed by %d uS, ticks > %u",
id, ll_delay_us, UINT_MAX);
/* Fix compile error when traces are disabled */
(void)ll_delay_us;
}
#ifdef __ZEPHYR__
static void timer_z_handler(struct k_work *work)
{
struct timer_zdata *zd = CONTAINER_OF(work, struct timer_zdata, work);
zd->handler(zd->arg);
}
#endif
static int timer_domain_register(struct ll_schedule_domain *domain,
uint64_t period, struct task *task,
void (*handler)(void *arg), void *arg)
{
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
int core = cpu_get_id();
int ret = 0;
#ifdef __ZEPHYR__
void *stack;
char *qname;
#endif
tr_dbg(&ll_tr, "timer_domain_register()");
#ifdef __ZEPHYR__
/* domain work only needs registered once */
if (timer_domain->ll_workq_registered[core])
goto out;
switch (core) {
case 0:
stack = ll_workq_stack0;
qname = "ll_workq0";
break;
#if CONFIG_CORE_COUNT > 1
case 1:
stack = ll_workq_stack1;
qname = "ll_workq1";
break;
#endif
#if CONFIG_CORE_COUNT > 2
case 2:
stack = ll_workq_stack2;
qname = "ll_workq2";
break;
#endif
#if CONFIG_CORE_COUNT > 3
case 3:
stack = ll_workq_stack3;
qname = "ll_workq3";
break;
#endif
default:
tr_err(&ll_tr, "invalid timer domain core %d\n", core);
return -EINVAL;
}
zdata[core].handler = handler;
zdata[core].arg = arg;
k_work_q_start(&timer_domain->ll_workq[core], stack,
ZEPHYR_LL_WORKQ_SIZE, -CONFIG_NUM_COOP_PRIORITIES);
k_thread_name_set(&timer_domain->ll_workq[core].thread, qname);
timer_domain->ll_workq_registered[core] = 1;
k_delayed_work_init(&zdata[core].work, timer_z_handler);
#else
/* tasks already registered on this core */
if (timer_domain->arg[core])
goto out;
timer_domain->arg[core] = arg;
ret = timer_register(timer_domain->timer, handler, arg);
#endif
tr_info(&ll_tr, "timer_domain_register domain->type %d domain->clk %d domain->ticks_per_ms %d period %d",
domain->type, domain->clk, domain->ticks_per_ms, (uint32_t)period);
out:
platform_shared_commit(timer_domain, sizeof(*timer_domain));
return ret;
}
static void timer_domain_unregister(struct ll_schedule_domain *domain,
struct task *task, uint32_t num_tasks)
{
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
int core = cpu_get_id();
tr_dbg(&ll_tr, "timer_domain_unregister()");
/* tasks still registered on this core */
if (!timer_domain->arg[core] || num_tasks)
goto out;
tr_info(&ll_tr, "timer_domain_unregister domain->type %d domain->clk %d",
domain->type, domain->clk);
#ifndef __ZEPHYR__
timer_unregister(timer_domain->timer, timer_domain->arg[core]);
#endif
timer_domain->arg[core] = NULL;
out:
platform_shared_commit(timer_domain, sizeof(*timer_domain));
}
static void timer_domain_enable(struct ll_schedule_domain *domain, int core)
{
#ifndef __ZEPHYR__
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
timer_enable(timer_domain->timer, timer_domain->arg[core], core);
platform_shared_commit(timer_domain, sizeof(*timer_domain));
#endif
}
static void timer_domain_disable(struct ll_schedule_domain *domain, int core)
{
#ifndef __ZEPHYR__
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
timer_disable(timer_domain->timer, timer_domain->arg[core], core);
platform_shared_commit(timer_domain, sizeof(*timer_domain));
#endif
}
static void timer_domain_set(struct ll_schedule_domain *domain, uint64_t start)
{
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
uint64_t ticks_tout = timer_domain->timeout;
uint64_t ticks_set;
#ifndef __ZEPHYR__
uint64_t ticks_req;
/* make sure to require for ticks later than tout from now */
ticks_req = MAX(start, platform_timer_get_atomic(timer_domain->timer) + ticks_tout);
ticks_set = platform_timer_set(timer_domain->timer, ticks_req);
#else
uint64_t current = platform_timer_get_atomic(timer_domain->timer);
uint64_t earliest_next = current + 1 + ZEPHYR_SCHED_COST;
uint64_t ticks_req = domain->next_tick ? start + ticks_tout :
MAX(start, earliest_next);
int ret, core = cpu_get_id();
uint64_t ticks_delta;
if (ticks_tout < CYC_PER_TICK)
ticks_tout = CYC_PER_TICK;
/* have we overshot the period length ?? */
if (ticks_req > current + ticks_tout)
ticks_req = current + ticks_tout;
ticks_req -= ticks_req % CYC_PER_TICK;
if (ticks_req < earliest_next) {
/* The earliest schedule point has to be rounded up */
ticks_req = earliest_next + CYC_PER_TICK - 1;
ticks_req -= ticks_req % CYC_PER_TICK;
}
/* work out next start time relative to start */
ticks_delta = ticks_req - current;
/* This actually sets the timer and the next call only reads it back */
/* using K_CYC(ticks_delta - 885) brings "requested - set" to about 180-700
* cycles, audio sounds very slow and distorted.
*/
ret = k_delayed_work_submit_to_queue(&timer_domain[core].ll_workq[core],
&zdata[core].work,
K_CYC(ticks_delta - ZEPHYR_SCHED_COST));
if (ret < 0) {
tr_err(&ll_tr, "queue submission error %d", ret);
return;
}
ticks_set = k_delayed_work_remaining_ticks(&zdata[core].work) * CYC_PER_TICK +
current - current % CYC_PER_TICK;
#endif
tr_dbg(&ll_tr, "timer_domain_set(): ticks_set %u ticks_req %u current %u",
(unsigned int)ticks_set, (unsigned int)ticks_req,
(unsigned int)platform_timer_get_atomic(timer_get()));
/* Was timer set to the value we requested? If no it means some
* delay occurred and we should report that in error log.
*/
if (ticks_req < ticks_set)
timer_report_delay(timer_domain->timer->id,
ticks_set - ticks_req);
domain->next_tick = ticks_set;
platform_shared_commit(timer_domain, sizeof(*timer_domain));
}
static void timer_domain_clear(struct ll_schedule_domain *domain)
{
#ifndef __ZEPHYR__
struct timer_domain *timer_domain = ll_sch_domain_get_pdata(domain);
platform_timer_clear(timer_domain->timer);
platform_shared_commit(timer_domain, sizeof(*timer_domain));
#endif
}
static bool timer_domain_is_pending(struct ll_schedule_domain *domain,
struct task *task, struct comp_dev **comp)
{
return task->start <= platform_timer_get_atomic(timer_get());
}
struct ll_schedule_domain *timer_domain_init(struct timer *timer, int clk)
{
struct ll_schedule_domain *domain;
struct timer_domain *timer_domain;
domain = domain_init(SOF_SCHEDULE_LL_TIMER, clk, false,
&timer_domain_ops);
timer_domain = rzalloc(SOF_MEM_ZONE_SYS_SHARED, 0, SOF_MEM_CAPS_RAM, sizeof(*timer_domain));
timer_domain->timer = timer;
timer_domain->timeout = LL_TIMER_SET_OVERHEAD_TICKS;
ll_sch_domain_set_pdata(domain, timer_domain);
platform_shared_commit(domain, sizeof(*domain));
platform_shared_commit(timer_domain, sizeof(*timer_domain));
return domain;
}
const struct ll_schedule_domain_ops timer_domain_ops = {
.domain_register = timer_domain_register,
.domain_unregister = timer_domain_unregister,
.domain_enable = timer_domain_enable,
.domain_disable = timer_domain_disable,
.domain_set = timer_domain_set,
.domain_clear = timer_domain_clear,
.domain_is_pending = timer_domain_is_pending
};