root / hw / ptimer.c @ b6741956
History | View | Annotate | Download (4.4 kB)
1 |
/*
|
---|---|
2 |
* General purpose implementation of a simple periodic countdown timer.
|
3 |
*
|
4 |
* Copyright (c) 2007 CodeSourcery.
|
5 |
*
|
6 |
* This code is licenced under the GNU LGPL.
|
7 |
*/
|
8 |
#include "vl.h" |
9 |
|
10 |
|
11 |
struct ptimer_state
|
12 |
{ |
13 |
int enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ |
14 |
uint64_t limit; |
15 |
uint64_t delta; |
16 |
uint32_t period_frac; |
17 |
int64_t period; |
18 |
int64_t last_event; |
19 |
int64_t next_event; |
20 |
QEMUBH *bh; |
21 |
QEMUTimer *timer; |
22 |
}; |
23 |
|
24 |
/* Use a bottom-half routine to avoid reentrancy issues. */
|
25 |
static void ptimer_trigger(ptimer_state *s) |
26 |
{ |
27 |
if (s->bh) {
|
28 |
qemu_bh_schedule(s->bh); |
29 |
} |
30 |
} |
31 |
|
32 |
static void ptimer_reload(ptimer_state *s) |
33 |
{ |
34 |
if (s->delta == 0) { |
35 |
ptimer_trigger(s); |
36 |
s->delta = s->limit; |
37 |
} |
38 |
if (s->delta == 0 || s->period == 0) { |
39 |
fprintf(stderr, "Timer with period zero, disabling\n");
|
40 |
s->enabled = 0;
|
41 |
return;
|
42 |
} |
43 |
|
44 |
s->last_event = s->next_event; |
45 |
s->next_event = s->last_event + s->delta * s->period; |
46 |
if (s->period_frac) {
|
47 |
s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
|
48 |
} |
49 |
qemu_mod_timer(s->timer, s->next_event); |
50 |
} |
51 |
|
52 |
static void ptimer_tick(void *opaque) |
53 |
{ |
54 |
ptimer_state *s = (ptimer_state *)opaque; |
55 |
ptimer_trigger(s); |
56 |
s->delta = 0;
|
57 |
if (s->enabled == 2) { |
58 |
s->enabled = 0;
|
59 |
} else {
|
60 |
ptimer_reload(s); |
61 |
} |
62 |
} |
63 |
|
64 |
uint64_t ptimer_get_count(ptimer_state *s) |
65 |
{ |
66 |
int64_t now; |
67 |
uint64_t counter; |
68 |
|
69 |
if (s->enabled) {
|
70 |
now = qemu_get_clock(vm_clock); |
71 |
/* Figure out the current counter value. */
|
72 |
if (now - s->next_event > 0 |
73 |
|| s->period == 0) {
|
74 |
/* Prevent timer underflowing if it should already have
|
75 |
triggered. */
|
76 |
counter = 0;
|
77 |
} else {
|
78 |
uint64_t rem; |
79 |
uint64_t div; |
80 |
|
81 |
rem = s->next_event - now; |
82 |
div = s->period; |
83 |
counter = rem / div; |
84 |
} |
85 |
} else {
|
86 |
counter = s->delta; |
87 |
} |
88 |
return counter;
|
89 |
} |
90 |
|
91 |
void ptimer_set_count(ptimer_state *s, uint64_t count)
|
92 |
{ |
93 |
s->delta = count; |
94 |
if (s->enabled) {
|
95 |
s->next_event = qemu_get_clock(vm_clock); |
96 |
ptimer_reload(s); |
97 |
} |
98 |
} |
99 |
|
100 |
void ptimer_run(ptimer_state *s, int oneshot) |
101 |
{ |
102 |
if (s->period == 0) { |
103 |
fprintf(stderr, "Timer with period zero, disabling\n");
|
104 |
return;
|
105 |
} |
106 |
s->enabled = oneshot ? 2 : 1; |
107 |
s->next_event = qemu_get_clock(vm_clock); |
108 |
ptimer_reload(s); |
109 |
} |
110 |
|
111 |
/* Pause a timer. Note that this may cause it to "lose" time, even if it
|
112 |
is immediately restarted. */
|
113 |
void ptimer_stop(ptimer_state *s)
|
114 |
{ |
115 |
if (!s->enabled)
|
116 |
return;
|
117 |
|
118 |
s->delta = ptimer_get_count(s); |
119 |
qemu_del_timer(s->timer); |
120 |
s->enabled = 0;
|
121 |
} |
122 |
|
123 |
/* Set counter increment interval in nanoseconds. */
|
124 |
void ptimer_set_period(ptimer_state *s, int64_t period)
|
125 |
{ |
126 |
s->period = period; |
127 |
s->period_frac = 0;
|
128 |
if (s->enabled) {
|
129 |
s->next_event = qemu_get_clock(vm_clock); |
130 |
ptimer_reload(s); |
131 |
} |
132 |
} |
133 |
|
134 |
/* Set counter frequency in Hz. */
|
135 |
void ptimer_set_freq(ptimer_state *s, uint32_t freq)
|
136 |
{ |
137 |
s->period = 1000000000ll / freq;
|
138 |
s->period_frac = (1000000000ll << 32) / freq; |
139 |
if (s->enabled) {
|
140 |
s->next_event = qemu_get_clock(vm_clock); |
141 |
ptimer_reload(s); |
142 |
} |
143 |
} |
144 |
|
145 |
/* Set the initial countdown value. If reload is nonzero then also set
|
146 |
count = limit. */
|
147 |
void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) |
148 |
{ |
149 |
s->limit = limit; |
150 |
if (reload)
|
151 |
s->delta = limit; |
152 |
if (s->enabled && reload) {
|
153 |
s->next_event = qemu_get_clock(vm_clock); |
154 |
ptimer_reload(s); |
155 |
} |
156 |
} |
157 |
|
158 |
void qemu_put_ptimer(QEMUFile *f, ptimer_state *s)
|
159 |
{ |
160 |
qemu_put_byte(f, s->enabled); |
161 |
qemu_put_be64s(f, &s->limit); |
162 |
qemu_put_be64s(f, &s->delta); |
163 |
qemu_put_be32s(f, &s->period_frac); |
164 |
qemu_put_be64s(f, &s->period); |
165 |
qemu_put_be64s(f, &s->last_event); |
166 |
qemu_put_be64s(f, &s->next_event); |
167 |
qemu_put_timer(f, s->timer); |
168 |
} |
169 |
|
170 |
void qemu_get_ptimer(QEMUFile *f, ptimer_state *s)
|
171 |
{ |
172 |
s->enabled = qemu_get_byte(f); |
173 |
qemu_get_be64s(f, &s->limit); |
174 |
qemu_get_be64s(f, &s->delta); |
175 |
qemu_get_be32s(f, &s->period_frac); |
176 |
qemu_get_be64s(f, &s->period); |
177 |
qemu_get_be64s(f, &s->last_event); |
178 |
qemu_get_be64s(f, &s->next_event); |
179 |
qemu_get_timer(f, s->timer); |
180 |
} |
181 |
|
182 |
ptimer_state *ptimer_init(QEMUBH *bh) |
183 |
{ |
184 |
ptimer_state *s; |
185 |
|
186 |
s = (ptimer_state *)qemu_mallocz(sizeof(ptimer_state));
|
187 |
s->bh = bh; |
188 |
s->timer = qemu_new_timer(vm_clock, ptimer_tick, s); |
189 |
return s;
|
190 |
} |
191 |
|