forked from teawater/libhermit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
229 lines (190 loc) · 5.23 KB
/
timer.c
File metadata and controls
229 lines (190 loc) · 5.23 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
/*
* Copyright (c) 2010-2017, Stefan Lankes, RWTH Aachen University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <hermit/stdio.h>
#include <hermit/string.h>
#include <hermit/processor.h>
#include <hermit/time.h>
#include <hermit/tasks.h>
#include <hermit/errno.h>
#include <hermit/spinlock.h>
#include <hermit/logging.h>
#include <asm/irq.h>
/*
* This will keep track of how many ticks the system
* has been running for
*/
DEFINE_PER_CORE(uint64_t, timer_ticks, 0);
static uint32_t freq_hz; /* frequency in Hz (updates per second) */
#if 0
extern int32_t boot_processor;
#endif
#define MHZ 1000000
#ifdef DYNAMIC_TICKS
DEFINE_PER_CORE(uint64_t, last_tsc, 0);
static uint64_t boot_tsc __attribute__ ((section(".data"))) = 0;
void check_ticks(void)
{
// do we already know the timer frequency? => if not, ignore this check
if (!freq_hz)
return;
const uint64_t curr_tsc = get_cntpct();
mb();
const uint64_t diff_tsc = curr_tsc - per_core(last_tsc);
const uint64_t diff_ticks = (diff_tsc * (uint64_t) TIMER_FREQ) / freq_hz;
if (diff_ticks > 0) {
set_per_core(timer_ticks, per_core(timer_ticks) + diff_ticks);
set_per_core(last_tsc, curr_tsc);
rmb();
}
}
uint64_t get_uptime(void)
{
// do we already know the timer frequency?
if (!freq_hz)
return 0;
const uint64_t curr_tsc = get_cntpct();
mb();
uint64_t diff = curr_tsc - boot_tsc;
return (1000ULL*diff) / freq_hz;
}
#else
uint64_t get_uptime(void)
{
return (get_clock_tick() * 1000) / TIMER_FREQ;
}
static void restart_periodic_timer(void)
{
set_cntp_tval(freq_hz / TIMER_FREQ);
set_cntp_ctl(1);
}
#endif
int timer_deadline(uint32_t ticks)
{
set_cntp_tval(ticks * freq_hz / TIMER_FREQ);
set_cntp_ctl(1);
return 0;
}
void timer_disable(void)
{
/* stop timer */
set_cntp_ctl(0);
}
int timer_is_running(void)
{
uint32_t v = get_cntp_ctl();
return (v & 0x1);
}
/*
* Handles the timer. In this case, it's very simple: We
* increment the 'timer_ticks' variable every time the
* timer fires.
*/
static void timer_handler(struct state *s)
{
#ifndef DYNAMIC_TICKS
/* Increment our 'tick counter' */
set_per_core(timer_ticks, per_core(timer_ticks)+1);
restart_periodic_timer();
#else
timer_disable();
#endif
#if 0
/*
* Every TIMER_FREQ clocks (approximately 1 second), we will
* display a message on the screen
*/
if (timer_ticks % TIMER_FREQ == 0) {
LOG_INFO("One second has passed %d\n", CORE_ID);
}
#endif
}
void udelay(uint32_t usecs)
{
uint64_t diff, end, start = get_cntpct();
uint64_t deadline = (usecs * freq_hz) / 1000000;
do {
end = get_cntpct();
rmb();
diff = end > start ? end - start : start - end;
if ((diff < deadline) && (deadline - diff > 50000))
check_workqueues();
} while(diff < deadline);
}
int timer_wait(unsigned int ticks)
{
uint64_t eticks = per_core(timer_ticks) + ticks;
task_t* curr_task = per_core(current_task);
if (curr_task->status == TASK_IDLE)
{
/*
* This will continuously loop until the given time has
* been reached
*/
while (per_core(timer_ticks) < eticks) {
check_workqueues();
// recheck break condition
if (per_core(timer_ticks) >= eticks)
break;
PAUSE;
}
} else if (per_core(timer_ticks) < eticks) {
check_workqueues();
if (per_core(timer_ticks) < eticks) {
set_timer(eticks);
reschedule();
}
}
return 0;
}
int clock_init(void)
{
#ifdef DYNAMIC_TICKS
if (!boot_tsc)
boot_tsc = get_cntpct();
#endif
return 0;
}
int timer_init(void)
{
#ifdef DYNAMIC_TICKS
set_per_core(last_tsc, boot_tsc);
#endif
return 0;
}
/*
* Sets up the system clock
*/
int timer_calibration(void)
{
freq_hz = get_cntfrq();
LOG_INFO("aarch64_timer: frequency %d KHz\n", freq_hz / 1000);
irq_install_handler(INT_PPI_NSPHYS_TIMER, timer_handler);
#ifndef DYNAMIC_TICKS
restart_periodic_timer();
#endif
return 0;
}