pcihp: acpi: ignore coldplugged bridges when composing hotpluggable slots
[qemu.git] / hw / timer / hpet.c
blob9520471be2c8e5df1ef356da75e18a6c28fd4228
1 /*
2 * High Precision Event Timer emulation
4 * Copyright (c) 2007 Alexander Graf
5 * Copyright (c) 2008 IBM Corporation
7 * Authors: Beth Kon <bkon@us.ibm.com>
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.1 of the License, or (at your option) any later version.
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.
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/>.
22 * *****************************************************************
24 * This driver attempts to emulate an HPET device in software.
27 #include "qemu/osdep.h"
28 #include "hw/i386/pc.h"
29 #include "hw/irq.h"
30 #include "qapi/error.h"
31 #include "qemu/error-report.h"
32 #include "qemu/timer.h"
33 #include "hw/timer/hpet.h"
34 #include "hw/sysbus.h"
35 #include "hw/rtc/mc146818rtc.h"
36 #include "hw/rtc/mc146818rtc_regs.h"
37 #include "migration/vmstate.h"
38 #include "hw/timer/i8254.h"
39 #include "exec/address-spaces.h"
40 #include "qom/object.h"
42 //#define HPET_DEBUG
43 #ifdef HPET_DEBUG
44 #define DPRINTF printf
45 #else
46 #define DPRINTF(...)
47 #endif
49 #define HPET_MSI_SUPPORT 0
51 OBJECT_DECLARE_SIMPLE_TYPE(HPETState, HPET)
53 struct HPETState;
54 typedef struct HPETTimer { /* timers */
55 uint8_t tn; /*timer number*/
56 QEMUTimer *qemu_timer;
57 struct HPETState *state;
58 /* Memory-mapped, software visible timer registers */
59 uint64_t config; /* configuration/cap */
60 uint64_t cmp; /* comparator */
61 uint64_t fsb; /* FSB route */
62 /* Hidden register state */
63 uint64_t period; /* Last value written to comparator */
64 uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 32-bit
65 * mode. Next pop will be actual timer expiration.
67 } HPETTimer;
69 struct HPETState {
70 /*< private >*/
71 SysBusDevice parent_obj;
72 /*< public >*/
74 MemoryRegion iomem;
75 uint64_t hpet_offset;
76 bool hpet_offset_saved;
77 qemu_irq irqs[HPET_NUM_IRQ_ROUTES];
78 uint32_t flags;
79 uint8_t rtc_irq_level;
80 qemu_irq pit_enabled;
81 uint8_t num_timers;
82 uint32_t intcap;
83 HPETTimer timer[HPET_MAX_TIMERS];
85 /* Memory-mapped, software visible registers */
86 uint64_t capability; /* capabilities */
87 uint64_t config; /* configuration */
88 uint64_t isr; /* interrupt status reg */
89 uint64_t hpet_counter; /* main counter */
90 uint8_t hpet_id; /* instance id */
93 static uint32_t hpet_in_legacy_mode(HPETState *s)
95 return s->config & HPET_CFG_LEGACY;
98 static uint32_t timer_int_route(struct HPETTimer *timer)
100 return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
103 static uint32_t timer_fsb_route(HPETTimer *t)
105 return t->config & HPET_TN_FSB_ENABLE;
108 static uint32_t hpet_enabled(HPETState *s)
110 return s->config & HPET_CFG_ENABLE;
113 static uint32_t timer_is_periodic(HPETTimer *t)
115 return t->config & HPET_TN_PERIODIC;
118 static uint32_t timer_enabled(HPETTimer *t)
120 return t->config & HPET_TN_ENABLE;
123 static uint32_t hpet_time_after(uint64_t a, uint64_t b)
125 return ((int32_t)(b - a) < 0);
128 static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
130 return ((int64_t)(b - a) < 0);
133 static uint64_t ticks_to_ns(uint64_t value)
135 return value * HPET_CLK_PERIOD;
138 static uint64_t ns_to_ticks(uint64_t value)
140 return value / HPET_CLK_PERIOD;
143 static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
145 new &= mask;
146 new |= old & ~mask;
147 return new;
150 static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
152 return (!(old & mask) && (new & mask));
155 static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
157 return ((old & mask) && !(new & mask));
160 static uint64_t hpet_get_ticks(HPETState *s)
162 return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hpet_offset);
166 * calculate diff between comparator value and current ticks
168 static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
171 if (t->config & HPET_TN_32BIT) {
172 uint32_t diff, cmp;
174 cmp = (uint32_t)t->cmp;
175 diff = cmp - (uint32_t)current;
176 diff = (int32_t)diff > 0 ? diff : (uint32_t)1;
177 return (uint64_t)diff;
178 } else {
179 uint64_t diff, cmp;
181 cmp = t->cmp;
182 diff = cmp - current;
183 diff = (int64_t)diff > 0 ? diff : (uint64_t)1;
184 return diff;
188 static void update_irq(struct HPETTimer *timer, int set)
190 uint64_t mask;
191 HPETState *s;
192 int route;
194 if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) {
195 /* if LegacyReplacementRoute bit is set, HPET specification requires
196 * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
197 * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
199 route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ;
200 } else {
201 route = timer_int_route(timer);
203 s = timer->state;
204 mask = 1 << timer->tn;
205 if (!set || !timer_enabled(timer) || !hpet_enabled(timer->state)) {
206 s->isr &= ~mask;
207 if (!timer_fsb_route(timer)) {
208 qemu_irq_lower(s->irqs[route]);
210 } else if (timer_fsb_route(timer)) {
211 address_space_stl_le(&address_space_memory, timer->fsb >> 32,
212 timer->fsb & 0xffffffff, MEMTXATTRS_UNSPECIFIED,
213 NULL);
214 } else if (timer->config & HPET_TN_TYPE_LEVEL) {
215 s->isr |= mask;
216 qemu_irq_raise(s->irqs[route]);
217 } else {
218 s->isr &= ~mask;
219 qemu_irq_pulse(s->irqs[route]);
223 static int hpet_pre_save(void *opaque)
225 HPETState *s = opaque;
227 /* save current counter value */
228 if (hpet_enabled(s)) {
229 s->hpet_counter = hpet_get_ticks(s);
232 return 0;
235 static int hpet_pre_load(void *opaque)
237 HPETState *s = opaque;
239 /* version 1 only supports 3, later versions will load the actual value */
240 s->num_timers = HPET_MIN_TIMERS;
241 return 0;
244 static bool hpet_validate_num_timers(void *opaque, int version_id)
246 HPETState *s = opaque;
248 if (s->num_timers < HPET_MIN_TIMERS) {
249 return false;
250 } else if (s->num_timers > HPET_MAX_TIMERS) {
251 return false;
253 return true;
256 static int hpet_post_load(void *opaque, int version_id)
258 HPETState *s = opaque;
260 /* Recalculate the offset between the main counter and guest time */
261 if (!s->hpet_offset_saved) {
262 s->hpet_offset = ticks_to_ns(s->hpet_counter)
263 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
266 /* Push number of timers into capability returned via HPET_ID */
267 s->capability &= ~HPET_ID_NUM_TIM_MASK;
268 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
269 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
271 /* Derive HPET_MSI_SUPPORT from the capability of the first timer. */
272 s->flags &= ~(1 << HPET_MSI_SUPPORT);
273 if (s->timer[0].config & HPET_TN_FSB_CAP) {
274 s->flags |= 1 << HPET_MSI_SUPPORT;
276 return 0;
279 static bool hpet_offset_needed(void *opaque)
281 HPETState *s = opaque;
283 return hpet_enabled(s) && s->hpet_offset_saved;
286 static bool hpet_rtc_irq_level_needed(void *opaque)
288 HPETState *s = opaque;
290 return s->rtc_irq_level != 0;
293 static const VMStateDescription vmstate_hpet_rtc_irq_level = {
294 .name = "hpet/rtc_irq_level",
295 .version_id = 1,
296 .minimum_version_id = 1,
297 .needed = hpet_rtc_irq_level_needed,
298 .fields = (VMStateField[]) {
299 VMSTATE_UINT8(rtc_irq_level, HPETState),
300 VMSTATE_END_OF_LIST()
304 static const VMStateDescription vmstate_hpet_offset = {
305 .name = "hpet/offset",
306 .version_id = 1,
307 .minimum_version_id = 1,
308 .needed = hpet_offset_needed,
309 .fields = (VMStateField[]) {
310 VMSTATE_UINT64(hpet_offset, HPETState),
311 VMSTATE_END_OF_LIST()
315 static const VMStateDescription vmstate_hpet_timer = {
316 .name = "hpet_timer",
317 .version_id = 1,
318 .minimum_version_id = 1,
319 .fields = (VMStateField[]) {
320 VMSTATE_UINT8(tn, HPETTimer),
321 VMSTATE_UINT64(config, HPETTimer),
322 VMSTATE_UINT64(cmp, HPETTimer),
323 VMSTATE_UINT64(fsb, HPETTimer),
324 VMSTATE_UINT64(period, HPETTimer),
325 VMSTATE_UINT8(wrap_flag, HPETTimer),
326 VMSTATE_TIMER_PTR(qemu_timer, HPETTimer),
327 VMSTATE_END_OF_LIST()
331 static const VMStateDescription vmstate_hpet = {
332 .name = "hpet",
333 .version_id = 2,
334 .minimum_version_id = 1,
335 .pre_save = hpet_pre_save,
336 .pre_load = hpet_pre_load,
337 .post_load = hpet_post_load,
338 .fields = (VMStateField[]) {
339 VMSTATE_UINT64(config, HPETState),
340 VMSTATE_UINT64(isr, HPETState),
341 VMSTATE_UINT64(hpet_counter, HPETState),
342 VMSTATE_UINT8_V(num_timers, HPETState, 2),
343 VMSTATE_VALIDATE("num_timers in range", hpet_validate_num_timers),
344 VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
345 vmstate_hpet_timer, HPETTimer),
346 VMSTATE_END_OF_LIST()
348 .subsections = (const VMStateDescription*[]) {
349 &vmstate_hpet_rtc_irq_level,
350 &vmstate_hpet_offset,
351 NULL
356 * timer expiration callback
358 static void hpet_timer(void *opaque)
360 HPETTimer *t = opaque;
361 uint64_t diff;
363 uint64_t period = t->period;
364 uint64_t cur_tick = hpet_get_ticks(t->state);
366 if (timer_is_periodic(t) && period != 0) {
367 if (t->config & HPET_TN_32BIT) {
368 while (hpet_time_after(cur_tick, t->cmp)) {
369 t->cmp = (uint32_t)(t->cmp + t->period);
371 } else {
372 while (hpet_time_after64(cur_tick, t->cmp)) {
373 t->cmp += period;
376 diff = hpet_calculate_diff(t, cur_tick);
377 timer_mod(t->qemu_timer,
378 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (int64_t)ticks_to_ns(diff));
379 } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
380 if (t->wrap_flag) {
381 diff = hpet_calculate_diff(t, cur_tick);
382 timer_mod(t->qemu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
383 (int64_t)ticks_to_ns(diff));
384 t->wrap_flag = 0;
387 update_irq(t, 1);
390 static void hpet_set_timer(HPETTimer *t)
392 uint64_t diff;
393 uint32_t wrap_diff; /* how many ticks until we wrap? */
394 uint64_t cur_tick = hpet_get_ticks(t->state);
396 /* whenever new timer is being set up, make sure wrap_flag is 0 */
397 t->wrap_flag = 0;
398 diff = hpet_calculate_diff(t, cur_tick);
400 /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
401 * counter wraps in addition to an interrupt with comparator match.
403 if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
404 wrap_diff = 0xffffffff - (uint32_t)cur_tick;
405 if (wrap_diff < (uint32_t)diff) {
406 diff = wrap_diff;
407 t->wrap_flag = 1;
410 timer_mod(t->qemu_timer,
411 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (int64_t)ticks_to_ns(diff));
414 static void hpet_del_timer(HPETTimer *t)
416 timer_del(t->qemu_timer);
417 update_irq(t, 0);
420 static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
421 unsigned size)
423 HPETState *s = opaque;
424 uint64_t cur_tick, index;
426 DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
427 index = addr;
428 /*address range of all TN regs*/
429 if (index >= 0x100 && index <= 0x3ff) {
430 uint8_t timer_id = (addr - 0x100) / 0x20;
431 HPETTimer *timer = &s->timer[timer_id];
433 if (timer_id > s->num_timers) {
434 DPRINTF("qemu: timer id out of range\n");
435 return 0;
438 switch ((addr - 0x100) % 0x20) {
439 case HPET_TN_CFG:
440 return timer->config;
441 case HPET_TN_CFG + 4: // Interrupt capabilities
442 return timer->config >> 32;
443 case HPET_TN_CMP: // comparator register
444 return timer->cmp;
445 case HPET_TN_CMP + 4:
446 return timer->cmp >> 32;
447 case HPET_TN_ROUTE:
448 return timer->fsb;
449 case HPET_TN_ROUTE + 4:
450 return timer->fsb >> 32;
451 default:
452 DPRINTF("qemu: invalid hpet_ram_readl\n");
453 break;
455 } else {
456 switch (index) {
457 case HPET_ID:
458 return s->capability;
459 case HPET_PERIOD:
460 return s->capability >> 32;
461 case HPET_CFG:
462 return s->config;
463 case HPET_CFG + 4:
464 DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl\n");
465 return 0;
466 case HPET_COUNTER:
467 if (hpet_enabled(s)) {
468 cur_tick = hpet_get_ticks(s);
469 } else {
470 cur_tick = s->hpet_counter;
472 DPRINTF("qemu: reading counter = %" PRIx64 "\n", cur_tick);
473 return cur_tick;
474 case HPET_COUNTER + 4:
475 if (hpet_enabled(s)) {
476 cur_tick = hpet_get_ticks(s);
477 } else {
478 cur_tick = s->hpet_counter;
480 DPRINTF("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick);
481 return cur_tick >> 32;
482 case HPET_STATUS:
483 return s->isr;
484 default:
485 DPRINTF("qemu: invalid hpet_ram_readl\n");
486 break;
489 return 0;
492 static void hpet_ram_write(void *opaque, hwaddr addr,
493 uint64_t value, unsigned size)
495 int i;
496 HPETState *s = opaque;
497 uint64_t old_val, new_val, val, index;
499 DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = 0x%" PRIx64 "\n",
500 addr, value);
501 index = addr;
502 old_val = hpet_ram_read(opaque, addr, 4);
503 new_val = value;
505 /*address range of all TN regs*/
506 if (index >= 0x100 && index <= 0x3ff) {
507 uint8_t timer_id = (addr - 0x100) / 0x20;
508 HPETTimer *timer = &s->timer[timer_id];
510 DPRINTF("qemu: hpet_ram_writel timer_id = 0x%x\n", timer_id);
511 if (timer_id > s->num_timers) {
512 DPRINTF("qemu: timer id out of range\n");
513 return;
515 switch ((addr - 0x100) % 0x20) {
516 case HPET_TN_CFG:
517 DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
518 if (activating_bit(old_val, new_val, HPET_TN_FSB_ENABLE)) {
519 update_irq(timer, 0);
521 val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
522 timer->config = (timer->config & 0xffffffff00000000ULL) | val;
523 if (new_val & HPET_TN_32BIT) {
524 timer->cmp = (uint32_t)timer->cmp;
525 timer->period = (uint32_t)timer->period;
527 if (activating_bit(old_val, new_val, HPET_TN_ENABLE) &&
528 hpet_enabled(s)) {
529 hpet_set_timer(timer);
530 } else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) {
531 hpet_del_timer(timer);
533 break;
534 case HPET_TN_CFG + 4: // Interrupt capabilities
535 DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
536 break;
537 case HPET_TN_CMP: // comparator register
538 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP\n");
539 if (timer->config & HPET_TN_32BIT) {
540 new_val = (uint32_t)new_val;
542 if (!timer_is_periodic(timer)
543 || (timer->config & HPET_TN_SETVAL)) {
544 timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val;
546 if (timer_is_periodic(timer)) {
548 * FIXME: Clamp period to reasonable min value?
549 * Clamp period to reasonable max value
551 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
552 timer->period =
553 (timer->period & 0xffffffff00000000ULL) | new_val;
555 timer->config &= ~HPET_TN_SETVAL;
556 if (hpet_enabled(s)) {
557 hpet_set_timer(timer);
559 break;
560 case HPET_TN_CMP + 4: // comparator register high order
561 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
562 if (!timer_is_periodic(timer)
563 || (timer->config & HPET_TN_SETVAL)) {
564 timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32;
565 } else {
567 * FIXME: Clamp period to reasonable min value?
568 * Clamp period to reasonable max value
570 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
571 timer->period =
572 (timer->period & 0xffffffffULL) | new_val << 32;
574 timer->config &= ~HPET_TN_SETVAL;
575 if (hpet_enabled(s)) {
576 hpet_set_timer(timer);
578 break;
579 case HPET_TN_ROUTE:
580 timer->fsb = (timer->fsb & 0xffffffff00000000ULL) | new_val;
581 break;
582 case HPET_TN_ROUTE + 4:
583 timer->fsb = (new_val << 32) | (timer->fsb & 0xffffffff);
584 break;
585 default:
586 DPRINTF("qemu: invalid hpet_ram_writel\n");
587 break;
589 return;
590 } else {
591 switch (index) {
592 case HPET_ID:
593 return;
594 case HPET_CFG:
595 val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
596 s->config = (s->config & 0xffffffff00000000ULL) | val;
597 if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
598 /* Enable main counter and interrupt generation. */
599 s->hpet_offset =
600 ticks_to_ns(s->hpet_counter) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
601 for (i = 0; i < s->num_timers; i++) {
602 if ((&s->timer[i])->cmp != ~0ULL) {
603 hpet_set_timer(&s->timer[i]);
606 } else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
607 /* Halt main counter and disable interrupt generation. */
608 s->hpet_counter = hpet_get_ticks(s);
609 for (i = 0; i < s->num_timers; i++) {
610 hpet_del_timer(&s->timer[i]);
613 /* i8254 and RTC output pins are disabled
614 * when HPET is in legacy mode */
615 if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
616 qemu_set_irq(s->pit_enabled, 0);
617 qemu_irq_lower(s->irqs[0]);
618 qemu_irq_lower(s->irqs[RTC_ISA_IRQ]);
619 } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
620 qemu_irq_lower(s->irqs[0]);
621 qemu_set_irq(s->pit_enabled, 1);
622 qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level);
624 break;
625 case HPET_CFG + 4:
626 DPRINTF("qemu: invalid HPET_CFG+4 write\n");
627 break;
628 case HPET_STATUS:
629 val = new_val & s->isr;
630 for (i = 0; i < s->num_timers; i++) {
631 if (val & (1 << i)) {
632 update_irq(&s->timer[i], 0);
635 break;
636 case HPET_COUNTER:
637 if (hpet_enabled(s)) {
638 DPRINTF("qemu: Writing counter while HPET enabled!\n");
640 s->hpet_counter =
641 (s->hpet_counter & 0xffffffff00000000ULL) | value;
642 DPRINTF("qemu: HPET counter written. ctr = 0x%" PRIx64 " -> "
643 "%" PRIx64 "\n", value, s->hpet_counter);
644 break;
645 case HPET_COUNTER + 4:
646 if (hpet_enabled(s)) {
647 DPRINTF("qemu: Writing counter while HPET enabled!\n");
649 s->hpet_counter =
650 (s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32);
651 DPRINTF("qemu: HPET counter + 4 written. ctr = 0x%" PRIx64 " -> "
652 "%" PRIx64 "\n", value, s->hpet_counter);
653 break;
654 default:
655 DPRINTF("qemu: invalid hpet_ram_writel\n");
656 break;
661 static const MemoryRegionOps hpet_ram_ops = {
662 .read = hpet_ram_read,
663 .write = hpet_ram_write,
664 .valid = {
665 .min_access_size = 4,
666 .max_access_size = 4,
668 .endianness = DEVICE_NATIVE_ENDIAN,
671 static void hpet_reset(DeviceState *d)
673 HPETState *s = HPET(d);
674 SysBusDevice *sbd = SYS_BUS_DEVICE(d);
675 int i;
677 for (i = 0; i < s->num_timers; i++) {
678 HPETTimer *timer = &s->timer[i];
680 hpet_del_timer(timer);
681 timer->cmp = ~0ULL;
682 timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
683 if (s->flags & (1 << HPET_MSI_SUPPORT)) {
684 timer->config |= HPET_TN_FSB_CAP;
686 /* advertise availability of ioapic int */
687 timer->config |= (uint64_t)s->intcap << 32;
688 timer->period = 0ULL;
689 timer->wrap_flag = 0;
692 qemu_set_irq(s->pit_enabled, 1);
693 s->hpet_counter = 0ULL;
694 s->hpet_offset = 0ULL;
695 s->config = 0ULL;
696 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
697 hpet_cfg.hpet[s->hpet_id].address = sbd->mmio[0].addr;
699 /* to document that the RTC lowers its output on reset as well */
700 s->rtc_irq_level = 0;
703 static void hpet_handle_legacy_irq(void *opaque, int n, int level)
705 HPETState *s = HPET(opaque);
707 if (n == HPET_LEGACY_PIT_INT) {
708 if (!hpet_in_legacy_mode(s)) {
709 qemu_set_irq(s->irqs[0], level);
711 } else {
712 s->rtc_irq_level = level;
713 if (!hpet_in_legacy_mode(s)) {
714 qemu_set_irq(s->irqs[RTC_ISA_IRQ], level);
719 static void hpet_init(Object *obj)
721 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
722 HPETState *s = HPET(obj);
724 /* HPET Area */
725 memory_region_init_io(&s->iomem, obj, &hpet_ram_ops, s, "hpet", HPET_LEN);
726 sysbus_init_mmio(sbd, &s->iomem);
729 static void hpet_realize(DeviceState *dev, Error **errp)
731 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
732 HPETState *s = HPET(dev);
733 int i;
734 HPETTimer *timer;
736 if (!s->intcap) {
737 warn_report("Hpet's intcap not initialized");
739 if (hpet_cfg.count == UINT8_MAX) {
740 /* first instance */
741 hpet_cfg.count = 0;
744 if (hpet_cfg.count == 8) {
745 error_setg(errp, "Only 8 instances of HPET is allowed");
746 return;
749 s->hpet_id = hpet_cfg.count++;
751 for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) {
752 sysbus_init_irq(sbd, &s->irqs[i]);
755 if (s->num_timers < HPET_MIN_TIMERS) {
756 s->num_timers = HPET_MIN_TIMERS;
757 } else if (s->num_timers > HPET_MAX_TIMERS) {
758 s->num_timers = HPET_MAX_TIMERS;
760 for (i = 0; i < HPET_MAX_TIMERS; i++) {
761 timer = &s->timer[i];
762 timer->qemu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hpet_timer, timer);
763 timer->tn = i;
764 timer->state = s;
767 /* 64-bit main counter; LegacyReplacementRoute. */
768 s->capability = 0x8086a001ULL;
769 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
770 s->capability |= ((uint64_t)(HPET_CLK_PERIOD * FS_PER_NS) << 32);
772 qdev_init_gpio_in(dev, hpet_handle_legacy_irq, 2);
773 qdev_init_gpio_out(dev, &s->pit_enabled, 1);
776 static Property hpet_device_properties[] = {
777 DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
778 DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false),
779 DEFINE_PROP_UINT32(HPET_INTCAP, HPETState, intcap, 0),
780 DEFINE_PROP_BOOL("hpet-offset-saved", HPETState, hpet_offset_saved, true),
781 DEFINE_PROP_END_OF_LIST(),
784 static void hpet_device_class_init(ObjectClass *klass, void *data)
786 DeviceClass *dc = DEVICE_CLASS(klass);
788 dc->realize = hpet_realize;
789 dc->reset = hpet_reset;
790 dc->vmsd = &vmstate_hpet;
791 device_class_set_props(dc, hpet_device_properties);
794 static const TypeInfo hpet_device_info = {
795 .name = TYPE_HPET,
796 .parent = TYPE_SYS_BUS_DEVICE,
797 .instance_size = sizeof(HPETState),
798 .instance_init = hpet_init,
799 .class_init = hpet_device_class_init,
802 static void hpet_register_types(void)
804 type_register_static(&hpet_device_info);
807 type_init(hpet_register_types)