-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtime.c
325 lines (281 loc) · 9.15 KB
/
time.c
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
/*
* Copyright (c) 2016 Leonid Yegoshin
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* tinyVP time keeper and timer IRQ service
*
* Wall time is in 64bit nanosecs, and counter is 64bit too -
* - the CP0 COUNT with 'generation' in high order bits.
* Generation is used to do a smooth adjustment of 32bit guest COUNT
* after COUNT rollover. 'lcount' is 64bit count value.
*
* Time interval is set in 'count's. To have an accurate timer measurement
* it is essential that wall time and CPU COUNT frequency can be multipliers.
*
* Guest CP0 COUNT/COMPARE doesn't work properly because PIC32MZEF has no
* a proper connect of guest COMPAREd signal line and IRQ never happens.
* So, guest time should be kept in tinyVP.
*
* 'need_time_update' flag means that guest/thread does someting which
* requires an accurate time/count and time must be updated to actual value
*/
#include "tinyVP.h"
#include "mipsasm.h"
#include "mips.h"
#include "irq.h"
#include "ic.h"
#include "time.h"
#include "thread.h"
#include <limits.h>
/* TEMPORARY */
#define cpu_get_frequency() (200000000)
// static unsigned int time_clock_adjust_current;
static unsigned int clock_time_multiplier;
volatile unsigned long long current_lcount;
unsigned long long initial_wall_time;
volatile unsigned long long current_wall_time;
int need_time_update = 0;
static struct timer timer_array[MAX_NUM_THREAD + 1];
static struct timer timerQ = {
.next = &timerQ,
};
// Get CP0 COUNT with a current 'generation'
//
static unsigned long long time_get_lcount(void)
{
unsigned int count;
count = read_cp0_count();
// count -= time_clock_adjust_current;
return time_extend_count(current_lcount,count);
}
// Set CP0 COMPARE
//
// If new COMPARE value is too close or behind CP0 COUNT -
// adjust COMPARE value to make timer IRQ _now_
//
static void time_set_timer(struct timer *timer)
{
unsigned long long lcnt;
unsigned long long lcount;
lcnt = time_get_lcount();
lcount = timer->lcount;
if ((long long)(lcount - lcnt) < 100)
lcount = lcnt + 100;
write_cp0_compare((unsigned int)lcount /* + time_clock_adjust_current */);
ehb();
}
// Update wall time and count
//
void time_update_wall_time(void)
{
unsigned int ie;
unsigned long long lclock;
unsigned long long lclock_delta;
/* minimum CPU frequency is around 20MHz to avoid exceeding 32bits */
unsigned long long time_delta;
im_up(ie);
lclock = time_get_lcount();
lclock_delta = lclock - current_lcount;
current_lcount = lclock;
time_delta = lclock_delta * clock_time_multiplier;
current_wall_time = current_wall_time + time_delta;
need_time_update = 0;
im_down(ie);
}
// Insert timer block into timer IRQ request queue
//
// Sorted insert into queue
//
static void time_insert_timer(struct timer *timer)
{
struct timer *tmr;
if (timer->flag & TIMER_FLAG_ACTIVE ) {
timer->prev->next = timer->next;
timer->next->prev = timer->prev;
}
if (timer->flag & TIMER_FLAG_COUNT) {
timer->time = timer->lcount * clock_time_multiplier - initial_wall_time;
} else
timer->lcount = (timer->time + initial_wall_time + clock_time_multiplier - 1) / clock_time_multiplier;
timer->flag |= TIMER_FLAG_ACTIVE;
if (timerQ.next == &timerQ) {
timerQ.next = timer;
timerQ.prev = timer;
timer->next = &timerQ;
timer->prev = &timerQ;
return;
}
for (tmr = timerQ.next; tmr != &timerQ; tmr = tmr->next) {
if (tmr->time > timer->time) {
timer->next = tmr;
timer->prev = tmr->prev;
timer->prev->next = timer;
tmr->prev = timer;
return;
}
}
timer->next = tmr;
timer->prev = tmr->prev;
timer->prev->next = timer;
tmr->prev = timer;
}
// Timer IRQ event handler
//
static void handle_event(struct exception_frame *exfr, struct timer *timer)
{
void (*func)(void);
if (timer->flag & TIMER_FLAG_TICK) {
timer->cnt = 0;
/* call to scheduler */
reschedule(exfr);
} else {
if (timer->flag & TIMER_FLAG_FUNC) {
func = (void *)timer->cnt;
func();
return;
}
if (timer->flag & TIMER_FLAG_TIMER_IRQ) {
execute_timer_IRQ(exfr, timer - timer_array);
return;
}
/* call to guest scheduler or whatever */
}
}
// Request a future IRQ after delay
//
// Execute a call to function or it should be some standard action
//
void timer_request(struct timer *timer, unsigned long long delay,
void (*func)(void))
{
unsigned int ie;
if (need_time_update)
time_update_wall_time();
timer->time = current_wall_time + delay;
if (func) {
timer->flag |= TIMER_FLAG_FUNC;
timer->flag &= ~TIMER_FLAG_COUNT;
timer->cnt = (unsigned int)func;
}
im_up(ie);
time_insert_timer(timer);
time_set_timer(timerQ.next);
im_down(ie);
}
// Request a guest IRQ at a specific 'count'
//
void timer_g_irq_reschedule(unsigned int timerno, unsigned long long clock)
{
unsigned int ie;
struct timer *timer;
if (need_time_update)
time_update_wall_time();
im_up(ie);
timer = &timer_array[timerno];
timer->flag |= TIMER_FLAG_TIMER_IRQ|TIMER_FLAG_COUNT;
timer->lcount = clock /* - time_clock_adjust_current */;
time_insert_timer(timer);
time_set_timer(timerQ.next);
im_down(ie);
}
extern struct timer tmp_timer;
// CP0 COMPARE IRQ processing
//
// Currently, there is check that IRQ happens at least at requested time or
// later. If it happens BEFORE a first requested time then it is a bug.
//
int time_irq(struct exception_frame *exfr, unsigned int irq)
{
struct timer *timer;
int many = 0;
if (irq != _TIMER_IRQ)
return 0;
/* IRQ disabled here... */
irq_ack(irq);
time_update_wall_time();
for (timer = timerQ.next; timer != &timerQ; timer = timer->next) {
if (timer->time > current_wall_time) {
if (!many) {
// Abnormal situation: unexpected CP0 COMPARE IRQ.
// It usually happens on PIC32MZEF if guest plays with Guest CP0 COMPARE
// because SoC has a signal buffer for CP0 COMPARE IRQ vs a plain MIPS CPU.
// However, it happens with time keeping bugs too.
int i;
unsigned gcount = read_g_cp0_count();
unsigned gcompare = read_g_cp0_compare();;
printf("time_irq: [%d] timer->time=%llx lcount=%llx current_wall_time=%llx current_lcount=%llx\n",timer-timer_array,timer->time,timer->lcount,current_wall_time,current_lcount);
printf("\tCOUNT=%08x COMPARE=%08x timerQ=%08x next=%08x prev=%08x G.COUNT=%08x G.COMPARE=%08x\n",read_cp0_count(),read_cp0_compare(),(void *)&timerQ, timerQ.next, timerQ.prev,gcount,gcompare);
for (i=0; i <= MAX_NUM_THREAD; i++)
printf("\t%08x: time=%llx lcount=%llx flag=%08x cnt=%d; next=%08x prev=%08x\n",
(void *)&timer_array[i],timer_array[i].time, timer_array[i].lcount, timer_array[i].flag, timer_array[i].cnt, timer_array[i].next, timer_array[i].prev);
printf("\t%08x: time=%llx lcount=%llx flag=%08x cnt=%d; next=%08x prev=%08x\n",
(void *)&tmp_timer,tmp_timer.time, tmp_timer.lcount, tmp_timer.flag, tmp_timer.cnt, tmp_timer.next, tmp_timer.prev);
}
break;
}
many = 1;
timer->prev->next = timer->next;
timer->next->prev = timer->prev;
timer->flag &= ~TIMER_FLAG_ACTIVE;
if (timer->flag & TIMER_FLAG_TICK) {
timer->time = timer->time + TIMER_TICK;
while (timer->time <= (current_wall_time + 1)) {
timer->time = timer->time + TIMER_TICK;
timer->cnt++;
}
time_insert_timer(timer);
}
handle_event(exfr, timer);
};
if (timerQ.next->flag & TIMER_FLAG_ACTIVE)
time_set_timer(timerQ.next);
return 1;
}
int init_time()
{
unsigned int freq;
unsigned int multiplier;
struct timer *timer;
/* IRQ disabled here... */
freq = cpu_get_frequency();
__asm__ __volatile__("rdhwr %0, $3": "=&r"(multiplier):);
clock_time_multiplier = TIME_SECOND / (freq / multiplier);
timer = &timer_array[0];
current_lcount = 0;
current_lcount = time_get_lcount();
initial_wall_time = current_lcount * clock_time_multiplier;
timer->time = TIMER_TICK;
timer->flag |= TIMER_FLAG_TICK;
timerQ.next = &timerQ;
time_insert_timer(timer);
clear_cp0_cause(CP0_CAUSE_DC);
time_set_timer(timerQ.next);
irq_set_prio_and_unmask(_TIMER_IRQ, _TIMER_PRIO);
return 1;
}
void write_time_values(void)
{
char str[128];
sprintf(str, "clock_time_multiplier=%x, initial_wall_time=%T current_lcount=%llx\n",
clock_time_multiplier, initial_wall_time, current_lcount);
uart_writeline(console_uart, str);
}