root / hw / ptimer.c @ 682aea0e
History | View | Annotate | Download (5.6 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 423f0742 | pbrook | * General purpose implementation of a simple periodic countdown timer.
|
3 | 423f0742 | pbrook | *
|
4 | 423f0742 | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | 423f0742 | pbrook | *
|
6 | 423f0742 | pbrook | * This code is licenced under the GNU LGPL.
|
7 | 423f0742 | pbrook | */
|
8 | 87ecb68b | pbrook | #include "hw.h" |
9 | 87ecb68b | pbrook | #include "qemu-timer.h" |
10 | d0a981b2 | pbrook | #include "host-utils.h" |
11 | 423f0742 | pbrook | |
12 | 423f0742 | pbrook | struct ptimer_state
|
13 | 423f0742 | pbrook | { |
14 | 423f0742 | pbrook | int enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */ |
15 | 8d05ea8a | blueswir1 | uint64_t limit; |
16 | 8d05ea8a | blueswir1 | uint64_t delta; |
17 | 423f0742 | pbrook | uint32_t period_frac; |
18 | 423f0742 | pbrook | int64_t period; |
19 | 423f0742 | pbrook | int64_t last_event; |
20 | 423f0742 | pbrook | int64_t next_event; |
21 | 423f0742 | pbrook | QEMUBH *bh; |
22 | 423f0742 | pbrook | QEMUTimer *timer; |
23 | 423f0742 | pbrook | }; |
24 | 423f0742 | pbrook | |
25 | 423f0742 | pbrook | /* Use a bottom-half routine to avoid reentrancy issues. */
|
26 | 423f0742 | pbrook | static void ptimer_trigger(ptimer_state *s) |
27 | 423f0742 | pbrook | { |
28 | 423f0742 | pbrook | if (s->bh) {
|
29 | 423f0742 | pbrook | qemu_bh_schedule(s->bh); |
30 | 423f0742 | pbrook | } |
31 | 423f0742 | pbrook | } |
32 | 423f0742 | pbrook | |
33 | 423f0742 | pbrook | static void ptimer_reload(ptimer_state *s) |
34 | 423f0742 | pbrook | { |
35 | 423f0742 | pbrook | if (s->delta == 0) { |
36 | 423f0742 | pbrook | ptimer_trigger(s); |
37 | 423f0742 | pbrook | s->delta = s->limit; |
38 | 423f0742 | pbrook | } |
39 | 423f0742 | pbrook | if (s->delta == 0 || s->period == 0) { |
40 | 423f0742 | pbrook | fprintf(stderr, "Timer with period zero, disabling\n");
|
41 | 423f0742 | pbrook | s->enabled = 0;
|
42 | 423f0742 | pbrook | return;
|
43 | 423f0742 | pbrook | } |
44 | 423f0742 | pbrook | |
45 | 423f0742 | pbrook | s->last_event = s->next_event; |
46 | 423f0742 | pbrook | s->next_event = s->last_event + s->delta * s->period; |
47 | 423f0742 | pbrook | if (s->period_frac) {
|
48 | 423f0742 | pbrook | s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
|
49 | 423f0742 | pbrook | } |
50 | 423f0742 | pbrook | qemu_mod_timer(s->timer, s->next_event); |
51 | 423f0742 | pbrook | } |
52 | 423f0742 | pbrook | |
53 | 423f0742 | pbrook | static void ptimer_tick(void *opaque) |
54 | 423f0742 | pbrook | { |
55 | 423f0742 | pbrook | ptimer_state *s = (ptimer_state *)opaque; |
56 | 423f0742 | pbrook | ptimer_trigger(s); |
57 | 423f0742 | pbrook | s->delta = 0;
|
58 | 423f0742 | pbrook | if (s->enabled == 2) { |
59 | 423f0742 | pbrook | s->enabled = 0;
|
60 | 423f0742 | pbrook | } else {
|
61 | 423f0742 | pbrook | ptimer_reload(s); |
62 | 423f0742 | pbrook | } |
63 | 423f0742 | pbrook | } |
64 | 423f0742 | pbrook | |
65 | 8d05ea8a | blueswir1 | uint64_t ptimer_get_count(ptimer_state *s) |
66 | 423f0742 | pbrook | { |
67 | 423f0742 | pbrook | int64_t now; |
68 | 8d05ea8a | blueswir1 | uint64_t counter; |
69 | 423f0742 | pbrook | |
70 | 423f0742 | pbrook | if (s->enabled) {
|
71 | 423f0742 | pbrook | now = qemu_get_clock(vm_clock); |
72 | 423f0742 | pbrook | /* Figure out the current counter value. */
|
73 | 423f0742 | pbrook | if (now - s->next_event > 0 |
74 | 423f0742 | pbrook | || s->period == 0) {
|
75 | 423f0742 | pbrook | /* Prevent timer underflowing if it should already have
|
76 | 423f0742 | pbrook | triggered. */
|
77 | 423f0742 | pbrook | counter = 0;
|
78 | 423f0742 | pbrook | } else {
|
79 | 8d05ea8a | blueswir1 | uint64_t rem; |
80 | 8d05ea8a | blueswir1 | uint64_t div; |
81 | d0a981b2 | pbrook | int clz1, clz2;
|
82 | d0a981b2 | pbrook | int shift;
|
83 | d0a981b2 | pbrook | |
84 | d0a981b2 | pbrook | /* We need to divide time by period, where time is stored in
|
85 | d0a981b2 | pbrook | rem (64-bit integer) and period is stored in period/period_frac
|
86 | d0a981b2 | pbrook | (64.32 fixed point).
|
87 | d0a981b2 | pbrook |
|
88 | d0a981b2 | pbrook | Doing full precision division is hard, so scale values and
|
89 | d0a981b2 | pbrook | do a 64-bit division. The result should be rounded down,
|
90 | d0a981b2 | pbrook | so that the rounding error never causes the timer to go
|
91 | d0a981b2 | pbrook | backwards.
|
92 | d0a981b2 | pbrook | */
|
93 | 423f0742 | pbrook | |
94 | 423f0742 | pbrook | rem = s->next_event - now; |
95 | 423f0742 | pbrook | div = s->period; |
96 | d0a981b2 | pbrook | |
97 | d0a981b2 | pbrook | clz1 = clz64(rem); |
98 | d0a981b2 | pbrook | clz2 = clz64(div); |
99 | d0a981b2 | pbrook | shift = clz1 < clz2 ? clz1 : clz2; |
100 | d0a981b2 | pbrook | |
101 | d0a981b2 | pbrook | rem <<= shift; |
102 | d0a981b2 | pbrook | div <<= shift; |
103 | d0a981b2 | pbrook | if (shift >= 32) { |
104 | d0a981b2 | pbrook | div |= ((uint64_t)s->period_frac << (shift - 32));
|
105 | d0a981b2 | pbrook | } else {
|
106 | d0a981b2 | pbrook | if (shift != 0) |
107 | d0a981b2 | pbrook | div |= (s->period_frac >> (32 - shift));
|
108 | d0a981b2 | pbrook | /* Look at remaining bits of period_frac and round div up if
|
109 | d0a981b2 | pbrook | necessary. */
|
110 | d0a981b2 | pbrook | if ((uint32_t)(s->period_frac << shift))
|
111 | d0a981b2 | pbrook | div += 1;
|
112 | d0a981b2 | pbrook | } |
113 | 423f0742 | pbrook | counter = rem / div; |
114 | 423f0742 | pbrook | } |
115 | 423f0742 | pbrook | } else {
|
116 | 423f0742 | pbrook | counter = s->delta; |
117 | 423f0742 | pbrook | } |
118 | 423f0742 | pbrook | return counter;
|
119 | 423f0742 | pbrook | } |
120 | 423f0742 | pbrook | |
121 | 8d05ea8a | blueswir1 | void ptimer_set_count(ptimer_state *s, uint64_t count)
|
122 | 423f0742 | pbrook | { |
123 | 423f0742 | pbrook | s->delta = count; |
124 | 423f0742 | pbrook | if (s->enabled) {
|
125 | 423f0742 | pbrook | s->next_event = qemu_get_clock(vm_clock); |
126 | 423f0742 | pbrook | ptimer_reload(s); |
127 | 423f0742 | pbrook | } |
128 | 423f0742 | pbrook | } |
129 | 423f0742 | pbrook | |
130 | 423f0742 | pbrook | void ptimer_run(ptimer_state *s, int oneshot) |
131 | 423f0742 | pbrook | { |
132 | 98fc5614 | pbrook | if (s->enabled) {
|
133 | 98fc5614 | pbrook | return;
|
134 | 98fc5614 | pbrook | } |
135 | 423f0742 | pbrook | if (s->period == 0) { |
136 | 423f0742 | pbrook | fprintf(stderr, "Timer with period zero, disabling\n");
|
137 | 423f0742 | pbrook | return;
|
138 | 423f0742 | pbrook | } |
139 | 423f0742 | pbrook | s->enabled = oneshot ? 2 : 1; |
140 | 423f0742 | pbrook | s->next_event = qemu_get_clock(vm_clock); |
141 | 423f0742 | pbrook | ptimer_reload(s); |
142 | 423f0742 | pbrook | } |
143 | 423f0742 | pbrook | |
144 | 8d05ea8a | blueswir1 | /* Pause a timer. Note that this may cause it to "lose" time, even if it
|
145 | 423f0742 | pbrook | is immediately restarted. */
|
146 | 423f0742 | pbrook | void ptimer_stop(ptimer_state *s)
|
147 | 423f0742 | pbrook | { |
148 | 423f0742 | pbrook | if (!s->enabled)
|
149 | 423f0742 | pbrook | return;
|
150 | 423f0742 | pbrook | |
151 | 423f0742 | pbrook | s->delta = ptimer_get_count(s); |
152 | 423f0742 | pbrook | qemu_del_timer(s->timer); |
153 | 423f0742 | pbrook | s->enabled = 0;
|
154 | 423f0742 | pbrook | } |
155 | 423f0742 | pbrook | |
156 | 423f0742 | pbrook | /* Set counter increment interval in nanoseconds. */
|
157 | 423f0742 | pbrook | void ptimer_set_period(ptimer_state *s, int64_t period)
|
158 | 423f0742 | pbrook | { |
159 | 423f0742 | pbrook | s->period = period; |
160 | 423f0742 | pbrook | s->period_frac = 0;
|
161 | 8d05ea8a | blueswir1 | if (s->enabled) {
|
162 | 8d05ea8a | blueswir1 | s->next_event = qemu_get_clock(vm_clock); |
163 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
164 | 8d05ea8a | blueswir1 | } |
165 | 423f0742 | pbrook | } |
166 | 423f0742 | pbrook | |
167 | 423f0742 | pbrook | /* Set counter frequency in Hz. */
|
168 | 423f0742 | pbrook | void ptimer_set_freq(ptimer_state *s, uint32_t freq)
|
169 | 423f0742 | pbrook | { |
170 | 423f0742 | pbrook | s->period = 1000000000ll / freq;
|
171 | 423f0742 | pbrook | s->period_frac = (1000000000ll << 32) / freq; |
172 | 8d05ea8a | blueswir1 | if (s->enabled) {
|
173 | 8d05ea8a | blueswir1 | s->next_event = qemu_get_clock(vm_clock); |
174 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
175 | 8d05ea8a | blueswir1 | } |
176 | 423f0742 | pbrook | } |
177 | 423f0742 | pbrook | |
178 | 423f0742 | pbrook | /* Set the initial countdown value. If reload is nonzero then also set
|
179 | 423f0742 | pbrook | count = limit. */
|
180 | 8d05ea8a | blueswir1 | void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) |
181 | 423f0742 | pbrook | { |
182 | 423f0742 | pbrook | s->limit = limit; |
183 | 423f0742 | pbrook | if (reload)
|
184 | 423f0742 | pbrook | s->delta = limit; |
185 | 62ea5b0b | pbrook | if (s->enabled && reload) {
|
186 | 8d05ea8a | blueswir1 | s->next_event = qemu_get_clock(vm_clock); |
187 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
188 | 8d05ea8a | blueswir1 | } |
189 | 8d05ea8a | blueswir1 | } |
190 | 8d05ea8a | blueswir1 | |
191 | 8d05ea8a | blueswir1 | void qemu_put_ptimer(QEMUFile *f, ptimer_state *s)
|
192 | 8d05ea8a | blueswir1 | { |
193 | 8d05ea8a | blueswir1 | qemu_put_byte(f, s->enabled); |
194 | 8d05ea8a | blueswir1 | qemu_put_be64s(f, &s->limit); |
195 | 8d05ea8a | blueswir1 | qemu_put_be64s(f, &s->delta); |
196 | 8d05ea8a | blueswir1 | qemu_put_be32s(f, &s->period_frac); |
197 | b6c4f71f | blueswir1 | qemu_put_sbe64s(f, &s->period); |
198 | b6c4f71f | blueswir1 | qemu_put_sbe64s(f, &s->last_event); |
199 | b6c4f71f | blueswir1 | qemu_put_sbe64s(f, &s->next_event); |
200 | 8d05ea8a | blueswir1 | qemu_put_timer(f, s->timer); |
201 | 8d05ea8a | blueswir1 | } |
202 | 8d05ea8a | blueswir1 | |
203 | 8d05ea8a | blueswir1 | void qemu_get_ptimer(QEMUFile *f, ptimer_state *s)
|
204 | 8d05ea8a | blueswir1 | { |
205 | 8d05ea8a | blueswir1 | s->enabled = qemu_get_byte(f); |
206 | 8d05ea8a | blueswir1 | qemu_get_be64s(f, &s->limit); |
207 | 8d05ea8a | blueswir1 | qemu_get_be64s(f, &s->delta); |
208 | 8d05ea8a | blueswir1 | qemu_get_be32s(f, &s->period_frac); |
209 | b6c4f71f | blueswir1 | qemu_get_sbe64s(f, &s->period); |
210 | b6c4f71f | blueswir1 | qemu_get_sbe64s(f, &s->last_event); |
211 | b6c4f71f | blueswir1 | qemu_get_sbe64s(f, &s->next_event); |
212 | 8d05ea8a | blueswir1 | qemu_get_timer(f, s->timer); |
213 | 423f0742 | pbrook | } |
214 | 423f0742 | pbrook | |
215 | 423f0742 | pbrook | ptimer_state *ptimer_init(QEMUBH *bh) |
216 | 423f0742 | pbrook | { |
217 | 423f0742 | pbrook | ptimer_state *s; |
218 | 423f0742 | pbrook | |
219 | 423f0742 | pbrook | s = (ptimer_state *)qemu_mallocz(sizeof(ptimer_state));
|
220 | 423f0742 | pbrook | s->bh = bh; |
221 | 423f0742 | pbrook | s->timer = qemu_new_timer(vm_clock, ptimer_tick, s); |
222 | 423f0742 | pbrook | return s;
|
223 | 423f0742 | pbrook | } |