root / hw / hpet.c @ 7d932dfd
History | View | Annotate | Download (20.1 kB)
1 |
/*
|
---|---|
2 |
* High Precisition Event Timer emulation
|
3 |
*
|
4 |
* Copyright (c) 2007 Alexander Graf
|
5 |
* Copyright (c) 2008 IBM Corporation
|
6 |
*
|
7 |
* Authors: Beth Kon <bkon@us.ibm.com>
|
8 |
*
|
9 |
* This library is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU Lesser General Public
|
11 |
* License as published by the Free Software Foundation; either
|
12 |
* version 2 of the License, or (at your option) any later version.
|
13 |
*
|
14 |
* This library is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17 |
* Lesser General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU Lesser General Public
|
20 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
21 |
*
|
22 |
* *****************************************************************
|
23 |
*
|
24 |
* This driver attempts to emulate an HPET device in software.
|
25 |
*/
|
26 |
|
27 |
#include "hw.h" |
28 |
#include "pc.h" |
29 |
#include "console.h" |
30 |
#include "qemu-timer.h" |
31 |
#include "hpet_emul.h" |
32 |
#include "sysbus.h" |
33 |
#include "mc146818rtc.h" |
34 |
|
35 |
//#define HPET_DEBUG
|
36 |
#ifdef HPET_DEBUG
|
37 |
#define DPRINTF printf
|
38 |
#else
|
39 |
#define DPRINTF(...)
|
40 |
#endif
|
41 |
|
42 |
struct HPETState;
|
43 |
typedef struct HPETTimer { /* timers */ |
44 |
uint8_t tn; /*timer number*/
|
45 |
QEMUTimer *qemu_timer; |
46 |
struct HPETState *state;
|
47 |
/* Memory-mapped, software visible timer registers */
|
48 |
uint64_t config; /* configuration/cap */
|
49 |
uint64_t cmp; /* comparator */
|
50 |
uint64_t fsb; /* FSB route, not supported now */
|
51 |
/* Hidden register state */
|
52 |
uint64_t period; /* Last value written to comparator */
|
53 |
uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 32-bit
|
54 |
* mode. Next pop will be actual timer expiration.
|
55 |
*/
|
56 |
} HPETTimer; |
57 |
|
58 |
typedef struct HPETState { |
59 |
SysBusDevice busdev; |
60 |
uint64_t hpet_offset; |
61 |
qemu_irq irqs[HPET_NUM_IRQ_ROUTES]; |
62 |
uint8_t rtc_irq_level; |
63 |
HPETTimer timer[HPET_NUM_TIMERS]; |
64 |
|
65 |
/* Memory-mapped, software visible registers */
|
66 |
uint64_t capability; /* capabilities */
|
67 |
uint64_t config; /* configuration */
|
68 |
uint64_t isr; /* interrupt status reg */
|
69 |
uint64_t hpet_counter; /* main counter */
|
70 |
} HPETState; |
71 |
|
72 |
static HPETState *hpet_statep;
|
73 |
|
74 |
static uint32_t hpet_in_legacy_mode(HPETState *s)
|
75 |
{ |
76 |
return s->config & HPET_CFG_LEGACY;
|
77 |
} |
78 |
|
79 |
static uint32_t timer_int_route(struct HPETTimer *timer) |
80 |
{ |
81 |
return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
|
82 |
} |
83 |
|
84 |
static uint32_t hpet_enabled(void) |
85 |
{ |
86 |
return hpet_statep->config & HPET_CFG_ENABLE;
|
87 |
} |
88 |
|
89 |
static uint32_t timer_is_periodic(HPETTimer *t)
|
90 |
{ |
91 |
return t->config & HPET_TN_PERIODIC;
|
92 |
} |
93 |
|
94 |
static uint32_t timer_enabled(HPETTimer *t)
|
95 |
{ |
96 |
return t->config & HPET_TN_ENABLE;
|
97 |
} |
98 |
|
99 |
static uint32_t hpet_time_after(uint64_t a, uint64_t b)
|
100 |
{ |
101 |
return ((int32_t)(b) - (int32_t)(a) < 0); |
102 |
} |
103 |
|
104 |
static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
|
105 |
{ |
106 |
return ((int64_t)(b) - (int64_t)(a) < 0); |
107 |
} |
108 |
|
109 |
static uint64_t ticks_to_ns(uint64_t value)
|
110 |
{ |
111 |
return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
|
112 |
} |
113 |
|
114 |
static uint64_t ns_to_ticks(uint64_t value)
|
115 |
{ |
116 |
return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
|
117 |
} |
118 |
|
119 |
static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
|
120 |
{ |
121 |
new &= mask; |
122 |
new |= old & ~mask; |
123 |
return new;
|
124 |
} |
125 |
|
126 |
static int activating_bit(uint64_t old, uint64_t new, uint64_t mask) |
127 |
{ |
128 |
return (!(old & mask) && (new & mask));
|
129 |
} |
130 |
|
131 |
static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask) |
132 |
{ |
133 |
return ((old & mask) && !(new & mask));
|
134 |
} |
135 |
|
136 |
static uint64_t hpet_get_ticks(void) |
137 |
{ |
138 |
return ns_to_ticks(qemu_get_clock(vm_clock) + hpet_statep->hpet_offset);
|
139 |
} |
140 |
|
141 |
/*
|
142 |
* calculate diff between comparator value and current ticks
|
143 |
*/
|
144 |
static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current) |
145 |
{ |
146 |
|
147 |
if (t->config & HPET_TN_32BIT) {
|
148 |
uint32_t diff, cmp; |
149 |
|
150 |
cmp = (uint32_t)t->cmp; |
151 |
diff = cmp - (uint32_t)current; |
152 |
diff = (int32_t)diff > 0 ? diff : (uint32_t)0; |
153 |
return (uint64_t)diff;
|
154 |
} else {
|
155 |
uint64_t diff, cmp; |
156 |
|
157 |
cmp = t->cmp; |
158 |
diff = cmp - current; |
159 |
diff = (int64_t)diff > 0 ? diff : (uint64_t)0; |
160 |
return diff;
|
161 |
} |
162 |
} |
163 |
|
164 |
static void update_irq(struct HPETTimer *timer) |
165 |
{ |
166 |
int route;
|
167 |
|
168 |
if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) { |
169 |
/* if LegacyReplacementRoute bit is set, HPET specification requires
|
170 |
* timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
|
171 |
* timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
|
172 |
*/
|
173 |
route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ; |
174 |
} else {
|
175 |
route = timer_int_route(timer); |
176 |
} |
177 |
if (!timer_enabled(timer) || !hpet_enabled()) {
|
178 |
return;
|
179 |
} |
180 |
qemu_irq_pulse(timer->state->irqs[route]); |
181 |
} |
182 |
|
183 |
static void hpet_pre_save(void *opaque) |
184 |
{ |
185 |
HPETState *s = opaque; |
186 |
|
187 |
/* save current counter value */
|
188 |
s->hpet_counter = hpet_get_ticks(); |
189 |
} |
190 |
|
191 |
static int hpet_post_load(void *opaque, int version_id) |
192 |
{ |
193 |
HPETState *s = opaque; |
194 |
|
195 |
/* Recalculate the offset between the main counter and guest time */
|
196 |
s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock); |
197 |
return 0; |
198 |
} |
199 |
|
200 |
static const VMStateDescription vmstate_hpet_timer = { |
201 |
.name = "hpet_timer",
|
202 |
.version_id = 1,
|
203 |
.minimum_version_id = 1,
|
204 |
.minimum_version_id_old = 1,
|
205 |
.fields = (VMStateField []) { |
206 |
VMSTATE_UINT8(tn, HPETTimer), |
207 |
VMSTATE_UINT64(config, HPETTimer), |
208 |
VMSTATE_UINT64(cmp, HPETTimer), |
209 |
VMSTATE_UINT64(fsb, HPETTimer), |
210 |
VMSTATE_UINT64(period, HPETTimer), |
211 |
VMSTATE_UINT8(wrap_flag, HPETTimer), |
212 |
VMSTATE_TIMER(qemu_timer, HPETTimer), |
213 |
VMSTATE_END_OF_LIST() |
214 |
} |
215 |
}; |
216 |
|
217 |
static const VMStateDescription vmstate_hpet = { |
218 |
.name = "hpet",
|
219 |
.version_id = 1,
|
220 |
.minimum_version_id = 1,
|
221 |
.minimum_version_id_old = 1,
|
222 |
.pre_save = hpet_pre_save, |
223 |
.post_load = hpet_post_load, |
224 |
.fields = (VMStateField []) { |
225 |
VMSTATE_UINT64(config, HPETState), |
226 |
VMSTATE_UINT64(isr, HPETState), |
227 |
VMSTATE_UINT64(hpet_counter, HPETState), |
228 |
VMSTATE_STRUCT_ARRAY(timer, HPETState, HPET_NUM_TIMERS, 0,
|
229 |
vmstate_hpet_timer, HPETTimer), |
230 |
VMSTATE_END_OF_LIST() |
231 |
} |
232 |
}; |
233 |
|
234 |
/*
|
235 |
* timer expiration callback
|
236 |
*/
|
237 |
static void hpet_timer(void *opaque) |
238 |
{ |
239 |
HPETTimer *t = opaque; |
240 |
uint64_t diff; |
241 |
|
242 |
uint64_t period = t->period; |
243 |
uint64_t cur_tick = hpet_get_ticks(); |
244 |
|
245 |
if (timer_is_periodic(t) && period != 0) { |
246 |
if (t->config & HPET_TN_32BIT) {
|
247 |
while (hpet_time_after(cur_tick, t->cmp)) {
|
248 |
t->cmp = (uint32_t)(t->cmp + t->period); |
249 |
} |
250 |
} else {
|
251 |
while (hpet_time_after64(cur_tick, t->cmp)) {
|
252 |
t->cmp += period; |
253 |
} |
254 |
} |
255 |
diff = hpet_calculate_diff(t, cur_tick); |
256 |
qemu_mod_timer(t->qemu_timer, |
257 |
qemu_get_clock(vm_clock) + (int64_t)ticks_to_ns(diff)); |
258 |
} else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { |
259 |
if (t->wrap_flag) {
|
260 |
diff = hpet_calculate_diff(t, cur_tick); |
261 |
qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock) + |
262 |
(int64_t)ticks_to_ns(diff)); |
263 |
t->wrap_flag = 0;
|
264 |
} |
265 |
} |
266 |
update_irq(t); |
267 |
} |
268 |
|
269 |
static void hpet_set_timer(HPETTimer *t) |
270 |
{ |
271 |
uint64_t diff; |
272 |
uint32_t wrap_diff; /* how many ticks until we wrap? */
|
273 |
uint64_t cur_tick = hpet_get_ticks(); |
274 |
|
275 |
/* whenever new timer is being set up, make sure wrap_flag is 0 */
|
276 |
t->wrap_flag = 0;
|
277 |
diff = hpet_calculate_diff(t, cur_tick); |
278 |
|
279 |
/* hpet spec says in one-shot 32-bit mode, generate an interrupt when
|
280 |
* counter wraps in addition to an interrupt with comparator match.
|
281 |
*/
|
282 |
if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
|
283 |
wrap_diff = 0xffffffff - (uint32_t)cur_tick;
|
284 |
if (wrap_diff < (uint32_t)diff) {
|
285 |
diff = wrap_diff; |
286 |
t->wrap_flag = 1;
|
287 |
} |
288 |
} |
289 |
qemu_mod_timer(t->qemu_timer, |
290 |
qemu_get_clock(vm_clock) + (int64_t)ticks_to_ns(diff)); |
291 |
} |
292 |
|
293 |
static void hpet_del_timer(HPETTimer *t) |
294 |
{ |
295 |
qemu_del_timer(t->qemu_timer); |
296 |
} |
297 |
|
298 |
#ifdef HPET_DEBUG
|
299 |
static uint32_t hpet_ram_readb(void *opaque, target_phys_addr_t addr) |
300 |
{ |
301 |
printf("qemu: hpet_read b at %" PRIx64 "\n", addr); |
302 |
return 0; |
303 |
} |
304 |
|
305 |
static uint32_t hpet_ram_readw(void *opaque, target_phys_addr_t addr) |
306 |
{ |
307 |
printf("qemu: hpet_read w at %" PRIx64 "\n", addr); |
308 |
return 0; |
309 |
} |
310 |
#endif
|
311 |
|
312 |
static uint32_t hpet_ram_readl(void *opaque, target_phys_addr_t addr) |
313 |
{ |
314 |
HPETState *s = opaque; |
315 |
uint64_t cur_tick, index; |
316 |
|
317 |
DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr); |
318 |
index = addr; |
319 |
/*address range of all TN regs*/
|
320 |
if (index >= 0x100 && index <= 0x3ff) { |
321 |
uint8_t timer_id = (addr - 0x100) / 0x20; |
322 |
HPETTimer *timer = &s->timer[timer_id]; |
323 |
|
324 |
if (timer_id > HPET_NUM_TIMERS - 1) { |
325 |
DPRINTF("qemu: timer id out of range\n");
|
326 |
return 0; |
327 |
} |
328 |
|
329 |
switch ((addr - 0x100) % 0x20) { |
330 |
case HPET_TN_CFG:
|
331 |
return timer->config;
|
332 |
case HPET_TN_CFG + 4: // Interrupt capabilities |
333 |
return timer->config >> 32; |
334 |
case HPET_TN_CMP: // comparator register |
335 |
return timer->cmp;
|
336 |
case HPET_TN_CMP + 4: |
337 |
return timer->cmp >> 32; |
338 |
case HPET_TN_ROUTE:
|
339 |
return timer->fsb >> 32; |
340 |
default:
|
341 |
DPRINTF("qemu: invalid hpet_ram_readl\n");
|
342 |
break;
|
343 |
} |
344 |
} else {
|
345 |
switch (index) {
|
346 |
case HPET_ID:
|
347 |
return s->capability;
|
348 |
case HPET_PERIOD:
|
349 |
return s->capability >> 32; |
350 |
case HPET_CFG:
|
351 |
return s->config;
|
352 |
case HPET_CFG + 4: |
353 |
DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl \n");
|
354 |
return 0; |
355 |
case HPET_COUNTER:
|
356 |
if (hpet_enabled()) {
|
357 |
cur_tick = hpet_get_ticks(); |
358 |
} else {
|
359 |
cur_tick = s->hpet_counter; |
360 |
} |
361 |
DPRINTF("qemu: reading counter = %" PRIx64 "\n", cur_tick); |
362 |
return cur_tick;
|
363 |
case HPET_COUNTER + 4: |
364 |
if (hpet_enabled()) {
|
365 |
cur_tick = hpet_get_ticks(); |
366 |
} else {
|
367 |
cur_tick = s->hpet_counter; |
368 |
} |
369 |
DPRINTF("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick); |
370 |
return cur_tick >> 32; |
371 |
case HPET_STATUS:
|
372 |
return s->isr;
|
373 |
default:
|
374 |
DPRINTF("qemu: invalid hpet_ram_readl\n");
|
375 |
break;
|
376 |
} |
377 |
} |
378 |
return 0; |
379 |
} |
380 |
|
381 |
#ifdef HPET_DEBUG
|
382 |
static void hpet_ram_writeb(void *opaque, target_phys_addr_t addr, |
383 |
uint32_t value) |
384 |
{ |
385 |
printf("qemu: invalid hpet_write b at %" PRIx64 " = %#x\n", |
386 |
addr, value); |
387 |
} |
388 |
|
389 |
static void hpet_ram_writew(void *opaque, target_phys_addr_t addr, |
390 |
uint32_t value) |
391 |
{ |
392 |
printf("qemu: invalid hpet_write w at %" PRIx64 " = %#x\n", |
393 |
addr, value); |
394 |
} |
395 |
#endif
|
396 |
|
397 |
static void hpet_ram_writel(void *opaque, target_phys_addr_t addr, |
398 |
uint32_t value) |
399 |
{ |
400 |
int i;
|
401 |
HPETState *s = opaque; |
402 |
uint64_t old_val, new_val, val, index; |
403 |
|
404 |
DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value); |
405 |
index = addr; |
406 |
old_val = hpet_ram_readl(opaque, addr); |
407 |
new_val = value; |
408 |
|
409 |
/*address range of all TN regs*/
|
410 |
if (index >= 0x100 && index <= 0x3ff) { |
411 |
uint8_t timer_id = (addr - 0x100) / 0x20; |
412 |
HPETTimer *timer = &s->timer[timer_id]; |
413 |
|
414 |
DPRINTF("qemu: hpet_ram_writel timer_id = %#x \n", timer_id);
|
415 |
if (timer_id > HPET_NUM_TIMERS - 1) { |
416 |
DPRINTF("qemu: timer id out of range\n");
|
417 |
return;
|
418 |
} |
419 |
switch ((addr - 0x100) % 0x20) { |
420 |
case HPET_TN_CFG:
|
421 |
DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
|
422 |
val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK); |
423 |
timer->config = (timer->config & 0xffffffff00000000ULL) | val;
|
424 |
if (new_val & HPET_TN_32BIT) {
|
425 |
timer->cmp = (uint32_t)timer->cmp; |
426 |
timer->period = (uint32_t)timer->period; |
427 |
} |
428 |
if (new_val & HPET_TN_TYPE_LEVEL) {
|
429 |
printf("qemu: level-triggered hpet not supported\n");
|
430 |
exit (-1);
|
431 |
} |
432 |
if (activating_bit(old_val, new_val, HPET_TN_ENABLE)) {
|
433 |
hpet_set_timer(timer); |
434 |
} else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) { |
435 |
hpet_del_timer(timer); |
436 |
} |
437 |
break;
|
438 |
case HPET_TN_CFG + 4: // Interrupt capabilities |
439 |
DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
|
440 |
break;
|
441 |
case HPET_TN_CMP: // comparator register |
442 |
DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP \n");
|
443 |
if (timer->config & HPET_TN_32BIT) {
|
444 |
new_val = (uint32_t)new_val; |
445 |
} |
446 |
if (!timer_is_periodic(timer)
|
447 |
|| (timer->config & HPET_TN_SETVAL)) { |
448 |
timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val;
|
449 |
} |
450 |
if (timer_is_periodic(timer)) {
|
451 |
/*
|
452 |
* FIXME: Clamp period to reasonable min value?
|
453 |
* Clamp period to reasonable max value
|
454 |
*/
|
455 |
new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1; |
456 |
timer->period = |
457 |
(timer->period & 0xffffffff00000000ULL) | new_val;
|
458 |
} |
459 |
timer->config &= ~HPET_TN_SETVAL; |
460 |
if (hpet_enabled()) {
|
461 |
hpet_set_timer(timer); |
462 |
} |
463 |
break;
|
464 |
case HPET_TN_CMP + 4: // comparator register high order |
465 |
DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
|
466 |
if (!timer_is_periodic(timer)
|
467 |
|| (timer->config & HPET_TN_SETVAL)) { |
468 |
timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32; |
469 |
} else {
|
470 |
/*
|
471 |
* FIXME: Clamp period to reasonable min value?
|
472 |
* Clamp period to reasonable max value
|
473 |
*/
|
474 |
new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1; |
475 |
timer->period = |
476 |
(timer->period & 0xffffffffULL) | new_val << 32; |
477 |
} |
478 |
timer->config &= ~HPET_TN_SETVAL; |
479 |
if (hpet_enabled()) {
|
480 |
hpet_set_timer(timer); |
481 |
} |
482 |
break;
|
483 |
case HPET_TN_ROUTE + 4: |
484 |
DPRINTF("qemu: hpet_ram_writel HPET_TN_ROUTE + 4\n");
|
485 |
break;
|
486 |
default:
|
487 |
DPRINTF("qemu: invalid hpet_ram_writel\n");
|
488 |
break;
|
489 |
} |
490 |
return;
|
491 |
} else {
|
492 |
switch (index) {
|
493 |
case HPET_ID:
|
494 |
return;
|
495 |
case HPET_CFG:
|
496 |
val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK); |
497 |
s->config = (s->config & 0xffffffff00000000ULL) | val;
|
498 |
if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
|
499 |
/* Enable main counter and interrupt generation. */
|
500 |
s->hpet_offset = |
501 |
ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock); |
502 |
for (i = 0; i < HPET_NUM_TIMERS; i++) { |
503 |
if ((&s->timer[i])->cmp != ~0ULL) { |
504 |
hpet_set_timer(&s->timer[i]); |
505 |
} |
506 |
} |
507 |
} else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) { |
508 |
/* Halt main counter and disable interrupt generation. */
|
509 |
s->hpet_counter = hpet_get_ticks(); |
510 |
for (i = 0; i < HPET_NUM_TIMERS; i++) { |
511 |
hpet_del_timer(&s->timer[i]); |
512 |
} |
513 |
} |
514 |
/* i8254 and RTC are disabled when HPET is in legacy mode */
|
515 |
if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
|
516 |
hpet_pit_disable(); |
517 |
qemu_irq_lower(s->irqs[RTC_ISA_IRQ]); |
518 |
} else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) { |
519 |
hpet_pit_enable(); |
520 |
qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level); |
521 |
} |
522 |
break;
|
523 |
case HPET_CFG + 4: |
524 |
DPRINTF("qemu: invalid HPET_CFG+4 write \n");
|
525 |
break;
|
526 |
case HPET_STATUS:
|
527 |
/* FIXME: need to handle level-triggered interrupts */
|
528 |
break;
|
529 |
case HPET_COUNTER:
|
530 |
if (hpet_enabled()) {
|
531 |
DPRINTF("qemu: Writing counter while HPET enabled!\n");
|
532 |
} |
533 |
s->hpet_counter = |
534 |
(s->hpet_counter & 0xffffffff00000000ULL) | value;
|
535 |
DPRINTF("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n", |
536 |
value, s->hpet_counter); |
537 |
break;
|
538 |
case HPET_COUNTER + 4: |
539 |
if (hpet_enabled()) {
|
540 |
DPRINTF("qemu: Writing counter while HPET enabled!\n");
|
541 |
} |
542 |
s->hpet_counter = |
543 |
(s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32); |
544 |
DPRINTF("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n", |
545 |
value, s->hpet_counter); |
546 |
break;
|
547 |
default:
|
548 |
DPRINTF("qemu: invalid hpet_ram_writel\n");
|
549 |
break;
|
550 |
} |
551 |
} |
552 |
} |
553 |
|
554 |
static CPUReadMemoryFunc * const hpet_ram_read[] = { |
555 |
#ifdef HPET_DEBUG
|
556 |
hpet_ram_readb, |
557 |
hpet_ram_readw, |
558 |
#else
|
559 |
NULL,
|
560 |
NULL,
|
561 |
#endif
|
562 |
hpet_ram_readl, |
563 |
}; |
564 |
|
565 |
static CPUWriteMemoryFunc * const hpet_ram_write[] = { |
566 |
#ifdef HPET_DEBUG
|
567 |
hpet_ram_writeb, |
568 |
hpet_ram_writew, |
569 |
#else
|
570 |
NULL,
|
571 |
NULL,
|
572 |
#endif
|
573 |
hpet_ram_writel, |
574 |
}; |
575 |
|
576 |
static void hpet_reset(DeviceState *d) |
577 |
{ |
578 |
HPETState *s = FROM_SYSBUS(HPETState, sysbus_from_qdev(d)); |
579 |
int i;
|
580 |
static int count = 0; |
581 |
|
582 |
for (i = 0; i < HPET_NUM_TIMERS; i++) { |
583 |
HPETTimer *timer = &s->timer[i]; |
584 |
|
585 |
hpet_del_timer(timer); |
586 |
timer->cmp = ~0ULL;
|
587 |
timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP; |
588 |
/* advertise availability of ioapic inti2 */
|
589 |
timer->config |= 0x00000004ULL << 32; |
590 |
timer->period = 0ULL;
|
591 |
timer->wrap_flag = 0;
|
592 |
} |
593 |
|
594 |
s->hpet_counter = 0ULL;
|
595 |
s->hpet_offset = 0ULL;
|
596 |
/* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
|
597 |
s->capability = 0x8086a201ULL;
|
598 |
s->capability |= ((HPET_CLK_PERIOD) << 32);
|
599 |
s->config = 0ULL;
|
600 |
if (count > 0) { |
601 |
/* we don't enable pit when hpet_reset is first called (by hpet_init)
|
602 |
* because hpet is taking over for pit here. On subsequent invocations,
|
603 |
* hpet_reset is called due to system reset. At this point control must
|
604 |
* be returned to pit until SW reenables hpet.
|
605 |
*/
|
606 |
hpet_pit_enable(); |
607 |
} |
608 |
count = 1;
|
609 |
} |
610 |
|
611 |
static void hpet_handle_rtc_irq(void *opaque, int n, int level) |
612 |
{ |
613 |
HPETState *s = FROM_SYSBUS(HPETState, opaque); |
614 |
|
615 |
s->rtc_irq_level = level; |
616 |
if (!hpet_in_legacy_mode(s)) {
|
617 |
qemu_set_irq(s->irqs[RTC_ISA_IRQ], level); |
618 |
} |
619 |
} |
620 |
|
621 |
static int hpet_init(SysBusDevice *dev) |
622 |
{ |
623 |
HPETState *s = FROM_SYSBUS(HPETState, dev); |
624 |
int i, iomemtype;
|
625 |
HPETTimer *timer; |
626 |
|
627 |
assert(!hpet_statep); |
628 |
hpet_statep = s; |
629 |
for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) { |
630 |
sysbus_init_irq(dev, &s->irqs[i]); |
631 |
} |
632 |
for (i = 0; i < HPET_NUM_TIMERS; i++) { |
633 |
timer = &s->timer[i]; |
634 |
timer->qemu_timer = qemu_new_timer(vm_clock, hpet_timer, timer); |
635 |
timer->tn = i; |
636 |
timer->state = s; |
637 |
} |
638 |
|
639 |
isa_reserve_irq(RTC_ISA_IRQ); |
640 |
qdev_init_gpio_in(&dev->qdev, hpet_handle_rtc_irq, 1);
|
641 |
|
642 |
/* HPET Area */
|
643 |
iomemtype = cpu_register_io_memory(hpet_ram_read, |
644 |
hpet_ram_write, s); |
645 |
sysbus_init_mmio(dev, 0x400, iomemtype);
|
646 |
return 0; |
647 |
} |
648 |
|
649 |
static SysBusDeviceInfo hpet_device_info = {
|
650 |
.qdev.name = "hpet",
|
651 |
.qdev.size = sizeof(HPETState),
|
652 |
.qdev.no_user = 1,
|
653 |
.qdev.vmsd = &vmstate_hpet, |
654 |
.qdev.reset = hpet_reset, |
655 |
.init = hpet_init, |
656 |
}; |
657 |
|
658 |
static void hpet_register_device(void) |
659 |
{ |
660 |
sysbus_register_withprop(&hpet_device_info); |
661 |
} |
662 |
|
663 |
device_init(hpet_register_device) |