qapi: Improve specificity of type/member descriptions
[qemu/armbru.git] / hw / timer / hpet.c
blob6998094233a7dbd3d3faa9ad1ea114c330fdc04e
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/qdev-properties.h"
34 #include "hw/timer/hpet.h"
35 #include "hw/sysbus.h"
36 #include "hw/rtc/mc146818rtc.h"
37 #include "hw/rtc/mc146818rtc_regs.h"
38 #include "migration/vmstate.h"
39 #include "hw/timer/i8254.h"
40 #include "exec/address-spaces.h"
41 #include "qom/object.h"
43 //#define HPET_DEBUG
44 #ifdef HPET_DEBUG
45 #define DPRINTF printf
46 #else
47 #define DPRINTF(...)
48 #endif
50 #define HPET_MSI_SUPPORT 0
52 OBJECT_DECLARE_SIMPLE_TYPE(HPETState, HPET)
54 struct HPETState;
55 typedef struct HPETTimer { /* timers */
56 uint8_t tn; /*timer number*/
57 QEMUTimer *qemu_timer;
58 struct HPETState *state;
59 /* Memory-mapped, software visible timer registers */
60 uint64_t config; /* configuration/cap */
61 uint64_t cmp; /* comparator */
62 uint64_t fsb; /* FSB route */
63 /* Hidden register state */
64 uint64_t period; /* Last value written to comparator */
65 uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 32-bit
66 * mode. Next pop will be actual timer expiration.
68 } HPETTimer;
70 struct HPETState {
71 /*< private >*/
72 SysBusDevice parent_obj;
73 /*< public >*/
75 MemoryRegion iomem;
76 uint64_t hpet_offset;
77 bool hpet_offset_saved;
78 qemu_irq irqs[HPET_NUM_IRQ_ROUTES];
79 uint32_t flags;
80 uint8_t rtc_irq_level;
81 qemu_irq pit_enabled;
82 uint8_t num_timers;
83 uint32_t intcap;
84 HPETTimer timer[HPET_MAX_TIMERS];
86 /* Memory-mapped, software visible registers */
87 uint64_t capability; /* capabilities */
88 uint64_t config; /* configuration */
89 uint64_t isr; /* interrupt status reg */
90 uint64_t hpet_counter; /* main counter */
91 uint8_t hpet_id; /* instance id */
94 static uint32_t hpet_in_legacy_mode(HPETState *s)
96 return s->config & HPET_CFG_LEGACY;
99 static uint32_t timer_int_route(struct HPETTimer *timer)
101 return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
104 static uint32_t timer_fsb_route(HPETTimer *t)
106 return t->config & HPET_TN_FSB_ENABLE;
109 static uint32_t hpet_enabled(HPETState *s)
111 return s->config & HPET_CFG_ENABLE;
114 static uint32_t timer_is_periodic(HPETTimer *t)
116 return t->config & HPET_TN_PERIODIC;
119 static uint32_t timer_enabled(HPETTimer *t)
121 return t->config & HPET_TN_ENABLE;
124 static uint32_t hpet_time_after(uint64_t a, uint64_t b)
126 return ((int32_t)(b - a) < 0);
129 static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
131 return ((int64_t)(b - a) < 0);
134 static uint64_t ticks_to_ns(uint64_t value)
136 return value * HPET_CLK_PERIOD;
139 static uint64_t ns_to_ticks(uint64_t value)
141 return value / HPET_CLK_PERIOD;
144 static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
146 new &= mask;
147 new |= old & ~mask;
148 return new;
151 static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
153 return (!(old & mask) && (new & mask));
156 static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
158 return ((old & mask) && !(new & mask));
161 static uint64_t hpet_get_ticks(HPETState *s)
163 return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hpet_offset);
167 * calculate diff between comparator value and current ticks
169 static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
172 if (t->config & HPET_TN_32BIT) {
173 uint32_t diff, cmp;
175 cmp = (uint32_t)t->cmp;
176 diff = cmp - (uint32_t)current;
177 diff = (int32_t)diff > 0 ? diff : (uint32_t)1;
178 return (uint64_t)diff;
179 } else {
180 uint64_t diff, cmp;
182 cmp = t->cmp;
183 diff = cmp - current;
184 diff = (int64_t)diff > 0 ? diff : (uint64_t)1;
185 return diff;
189 static void update_irq(struct HPETTimer *timer, int set)
191 uint64_t mask;
192 HPETState *s;
193 int route;
195 if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) {
196 /* if LegacyReplacementRoute bit is set, HPET specification requires
197 * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
198 * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
200 route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ;
201 } else {
202 route = timer_int_route(timer);
204 s = timer->state;
205 mask = 1 << timer->tn;
206 if (!set || !timer_enabled(timer) || !hpet_enabled(timer->state)) {
207 s->isr &= ~mask;
208 if (!timer_fsb_route(timer)) {
209 qemu_irq_lower(s->irqs[route]);
211 } else if (timer_fsb_route(timer)) {
212 address_space_stl_le(&address_space_memory, timer->fsb >> 32,
213 timer->fsb & 0xffffffff, MEMTXATTRS_UNSPECIFIED,
214 NULL);
215 } else if (timer->config & HPET_TN_TYPE_LEVEL) {
216 s->isr |= mask;
217 qemu_irq_raise(s->irqs[route]);
218 } else {
219 s->isr &= ~mask;
220 qemu_irq_pulse(s->irqs[route]);
224 static int hpet_pre_save(void *opaque)
226 HPETState *s = opaque;
228 /* save current counter value */
229 if (hpet_enabled(s)) {
230 s->hpet_counter = hpet_get_ticks(s);
233 return 0;
236 static int hpet_pre_load(void *opaque)
238 HPETState *s = opaque;
240 /* version 1 only supports 3, later versions will load the actual value */
241 s->num_timers = HPET_MIN_TIMERS;
242 return 0;
245 static bool hpet_validate_num_timers(void *opaque, int version_id)
247 HPETState *s = opaque;
249 if (s->num_timers < HPET_MIN_TIMERS) {
250 return false;
251 } else if (s->num_timers > HPET_MAX_TIMERS) {
252 return false;
254 return true;
257 static int hpet_post_load(void *opaque, int version_id)
259 HPETState *s = opaque;
261 /* Recalculate the offset between the main counter and guest time */
262 if (!s->hpet_offset_saved) {
263 s->hpet_offset = ticks_to_ns(s->hpet_counter)
264 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
267 /* Push number of timers into capability returned via HPET_ID */
268 s->capability &= ~HPET_ID_NUM_TIM_MASK;
269 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
270 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
272 /* Derive HPET_MSI_SUPPORT from the capability of the first timer. */
273 s->flags &= ~(1 << HPET_MSI_SUPPORT);
274 if (s->timer[0].config & HPET_TN_FSB_CAP) {
275 s->flags |= 1 << HPET_MSI_SUPPORT;
277 return 0;
280 static bool hpet_offset_needed(void *opaque)
282 HPETState *s = opaque;
284 return hpet_enabled(s) && s->hpet_offset_saved;
287 static bool hpet_rtc_irq_level_needed(void *opaque)
289 HPETState *s = opaque;
291 return s->rtc_irq_level != 0;
294 static const VMStateDescription vmstate_hpet_rtc_irq_level = {
295 .name = "hpet/rtc_irq_level",
296 .version_id = 1,
297 .minimum_version_id = 1,
298 .needed = hpet_rtc_irq_level_needed,
299 .fields = (VMStateField[]) {
300 VMSTATE_UINT8(rtc_irq_level, HPETState),
301 VMSTATE_END_OF_LIST()
305 static const VMStateDescription vmstate_hpet_offset = {
306 .name = "hpet/offset",
307 .version_id = 1,
308 .minimum_version_id = 1,
309 .needed = hpet_offset_needed,
310 .fields = (VMStateField[]) {
311 VMSTATE_UINT64(hpet_offset, HPETState),
312 VMSTATE_END_OF_LIST()
316 static const VMStateDescription vmstate_hpet_timer = {
317 .name = "hpet_timer",
318 .version_id = 1,
319 .minimum_version_id = 1,
320 .fields = (VMStateField[]) {
321 VMSTATE_UINT8(tn, HPETTimer),
322 VMSTATE_UINT64(config, HPETTimer),
323 VMSTATE_UINT64(cmp, HPETTimer),
324 VMSTATE_UINT64(fsb, HPETTimer),
325 VMSTATE_UINT64(period, HPETTimer),
326 VMSTATE_UINT8(wrap_flag, HPETTimer),
327 VMSTATE_TIMER_PTR(qemu_timer, HPETTimer),
328 VMSTATE_END_OF_LIST()
332 static const VMStateDescription vmstate_hpet = {
333 .name = "hpet",
334 .version_id = 2,
335 .minimum_version_id = 1,
336 .pre_save = hpet_pre_save,
337 .pre_load = hpet_pre_load,
338 .post_load = hpet_post_load,
339 .fields = (VMStateField[]) {
340 VMSTATE_UINT64(config, HPETState),
341 VMSTATE_UINT64(isr, HPETState),
342 VMSTATE_UINT64(hpet_counter, HPETState),
343 VMSTATE_UINT8_V(num_timers, HPETState, 2),
344 VMSTATE_VALIDATE("num_timers in range", hpet_validate_num_timers),
345 VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
346 vmstate_hpet_timer, HPETTimer),
347 VMSTATE_END_OF_LIST()
349 .subsections = (const VMStateDescription*[]) {
350 &vmstate_hpet_rtc_irq_level,
351 &vmstate_hpet_offset,
352 NULL
356 static void hpet_arm(HPETTimer *t, uint64_t ticks)
358 if (ticks < ns_to_ticks(INT64_MAX / 2)) {
359 timer_mod(t->qemu_timer,
360 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ticks_to_ns(ticks));
361 } else {
362 timer_del(t->qemu_timer);
367 * timer expiration callback
369 static void hpet_timer(void *opaque)
371 HPETTimer *t = opaque;
372 uint64_t diff;
374 uint64_t period = t->period;
375 uint64_t cur_tick = hpet_get_ticks(t->state);
377 if (timer_is_periodic(t) && period != 0) {
378 if (t->config & HPET_TN_32BIT) {
379 while (hpet_time_after(cur_tick, t->cmp)) {
380 t->cmp = (uint32_t)(t->cmp + t->period);
382 } else {
383 while (hpet_time_after64(cur_tick, t->cmp)) {
384 t->cmp += period;
387 diff = hpet_calculate_diff(t, cur_tick);
388 hpet_arm(t, diff);
389 } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
390 if (t->wrap_flag) {
391 diff = hpet_calculate_diff(t, cur_tick);
392 hpet_arm(t, diff);
393 t->wrap_flag = 0;
396 update_irq(t, 1);
399 static void hpet_set_timer(HPETTimer *t)
401 uint64_t diff;
402 uint32_t wrap_diff; /* how many ticks until we wrap? */
403 uint64_t cur_tick = hpet_get_ticks(t->state);
405 /* whenever new timer is being set up, make sure wrap_flag is 0 */
406 t->wrap_flag = 0;
407 diff = hpet_calculate_diff(t, cur_tick);
409 /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
410 * counter wraps in addition to an interrupt with comparator match.
412 if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
413 wrap_diff = 0xffffffff - (uint32_t)cur_tick;
414 if (wrap_diff < (uint32_t)diff) {
415 diff = wrap_diff;
416 t->wrap_flag = 1;
419 hpet_arm(t, diff);
422 static void hpet_del_timer(HPETTimer *t)
424 timer_del(t->qemu_timer);
425 update_irq(t, 0);
428 static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
429 unsigned size)
431 HPETState *s = opaque;
432 uint64_t cur_tick, index;
434 DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
435 index = addr;
436 /*address range of all TN regs*/
437 if (index >= 0x100 && index <= 0x3ff) {
438 uint8_t timer_id = (addr - 0x100) / 0x20;
439 HPETTimer *timer = &s->timer[timer_id];
441 if (timer_id > s->num_timers) {
442 DPRINTF("qemu: timer id out of range\n");
443 return 0;
446 switch ((addr - 0x100) % 0x20) {
447 case HPET_TN_CFG:
448 return timer->config;
449 case HPET_TN_CFG + 4: // Interrupt capabilities
450 return timer->config >> 32;
451 case HPET_TN_CMP: // comparator register
452 return timer->cmp;
453 case HPET_TN_CMP + 4:
454 return timer->cmp >> 32;
455 case HPET_TN_ROUTE:
456 return timer->fsb;
457 case HPET_TN_ROUTE + 4:
458 return timer->fsb >> 32;
459 default:
460 DPRINTF("qemu: invalid hpet_ram_readl\n");
461 break;
463 } else {
464 switch (index) {
465 case HPET_ID:
466 return s->capability;
467 case HPET_PERIOD:
468 return s->capability >> 32;
469 case HPET_CFG:
470 return s->config;
471 case HPET_CFG + 4:
472 DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl\n");
473 return 0;
474 case HPET_COUNTER:
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 = %" PRIx64 "\n", cur_tick);
481 return cur_tick;
482 case HPET_COUNTER + 4:
483 if (hpet_enabled(s)) {
484 cur_tick = hpet_get_ticks(s);
485 } else {
486 cur_tick = s->hpet_counter;
488 DPRINTF("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick);
489 return cur_tick >> 32;
490 case HPET_STATUS:
491 return s->isr;
492 default:
493 DPRINTF("qemu: invalid hpet_ram_readl\n");
494 break;
497 return 0;
500 static void hpet_ram_write(void *opaque, hwaddr addr,
501 uint64_t value, unsigned size)
503 int i;
504 HPETState *s = opaque;
505 uint64_t old_val, new_val, val, index;
507 DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = 0x%" PRIx64 "\n",
508 addr, value);
509 index = addr;
510 old_val = hpet_ram_read(opaque, addr, 4);
511 new_val = value;
513 /*address range of all TN regs*/
514 if (index >= 0x100 && index <= 0x3ff) {
515 uint8_t timer_id = (addr - 0x100) / 0x20;
516 HPETTimer *timer = &s->timer[timer_id];
518 DPRINTF("qemu: hpet_ram_writel timer_id = 0x%x\n", timer_id);
519 if (timer_id > s->num_timers) {
520 DPRINTF("qemu: timer id out of range\n");
521 return;
523 switch ((addr - 0x100) % 0x20) {
524 case HPET_TN_CFG:
525 DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
526 if (activating_bit(old_val, new_val, HPET_TN_FSB_ENABLE)) {
527 update_irq(timer, 0);
529 val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
530 timer->config = (timer->config & 0xffffffff00000000ULL) | val;
531 if (new_val & HPET_TN_32BIT) {
532 timer->cmp = (uint32_t)timer->cmp;
533 timer->period = (uint32_t)timer->period;
535 if (activating_bit(old_val, new_val, HPET_TN_ENABLE) &&
536 hpet_enabled(s)) {
537 hpet_set_timer(timer);
538 } else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) {
539 hpet_del_timer(timer);
541 break;
542 case HPET_TN_CFG + 4: // Interrupt capabilities
543 DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
544 break;
545 case HPET_TN_CMP: // comparator register
546 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP\n");
547 if (timer->config & HPET_TN_32BIT) {
548 new_val = (uint32_t)new_val;
550 if (!timer_is_periodic(timer)
551 || (timer->config & HPET_TN_SETVAL)) {
552 timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val;
554 if (timer_is_periodic(timer)) {
556 * FIXME: Clamp period to reasonable min value?
557 * Clamp period to reasonable max value
559 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
560 timer->period =
561 (timer->period & 0xffffffff00000000ULL) | new_val;
563 timer->config &= ~HPET_TN_SETVAL;
564 if (hpet_enabled(s)) {
565 hpet_set_timer(timer);
567 break;
568 case HPET_TN_CMP + 4: // comparator register high order
569 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
570 if (!timer_is_periodic(timer)
571 || (timer->config & HPET_TN_SETVAL)) {
572 timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32;
573 } else {
575 * FIXME: Clamp period to reasonable min value?
576 * Clamp period to reasonable max value
578 new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
579 timer->period =
580 (timer->period & 0xffffffffULL) | new_val << 32;
582 timer->config &= ~HPET_TN_SETVAL;
583 if (hpet_enabled(s)) {
584 hpet_set_timer(timer);
586 break;
587 case HPET_TN_ROUTE:
588 timer->fsb = (timer->fsb & 0xffffffff00000000ULL) | new_val;
589 break;
590 case HPET_TN_ROUTE + 4:
591 timer->fsb = (new_val << 32) | (timer->fsb & 0xffffffff);
592 break;
593 default:
594 DPRINTF("qemu: invalid hpet_ram_writel\n");
595 break;
597 return;
598 } else {
599 switch (index) {
600 case HPET_ID:
601 return;
602 case HPET_CFG:
603 val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
604 s->config = (s->config & 0xffffffff00000000ULL) | val;
605 if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
606 /* Enable main counter and interrupt generation. */
607 s->hpet_offset =
608 ticks_to_ns(s->hpet_counter) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
609 for (i = 0; i < s->num_timers; i++) {
610 if ((&s->timer[i])->cmp != ~0ULL) {
611 hpet_set_timer(&s->timer[i]);
614 } else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
615 /* Halt main counter and disable interrupt generation. */
616 s->hpet_counter = hpet_get_ticks(s);
617 for (i = 0; i < s->num_timers; i++) {
618 hpet_del_timer(&s->timer[i]);
621 /* i8254 and RTC output pins are disabled
622 * when HPET is in legacy mode */
623 if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
624 qemu_set_irq(s->pit_enabled, 0);
625 qemu_irq_lower(s->irqs[0]);
626 qemu_irq_lower(s->irqs[RTC_ISA_IRQ]);
627 } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
628 qemu_irq_lower(s->irqs[0]);
629 qemu_set_irq(s->pit_enabled, 1);
630 qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level);
632 break;
633 case HPET_CFG + 4:
634 DPRINTF("qemu: invalid HPET_CFG+4 write\n");
635 break;
636 case HPET_STATUS:
637 val = new_val & s->isr;
638 for (i = 0; i < s->num_timers; i++) {
639 if (val & (1 << i)) {
640 update_irq(&s->timer[i], 0);
643 break;
644 case HPET_COUNTER:
645 if (hpet_enabled(s)) {
646 DPRINTF("qemu: Writing counter while HPET enabled!\n");
648 s->hpet_counter =
649 (s->hpet_counter & 0xffffffff00000000ULL) | value;
650 DPRINTF("qemu: HPET counter written. ctr = 0x%" PRIx64 " -> "
651 "%" PRIx64 "\n", value, s->hpet_counter);
652 break;
653 case HPET_COUNTER + 4:
654 if (hpet_enabled(s)) {
655 DPRINTF("qemu: Writing counter while HPET enabled!\n");
657 s->hpet_counter =
658 (s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32);
659 DPRINTF("qemu: HPET counter + 4 written. ctr = 0x%" PRIx64 " -> "
660 "%" PRIx64 "\n", value, s->hpet_counter);
661 break;
662 default:
663 DPRINTF("qemu: invalid hpet_ram_writel\n");
664 break;
669 static const MemoryRegionOps hpet_ram_ops = {
670 .read = hpet_ram_read,
671 .write = hpet_ram_write,
672 .valid = {
673 .min_access_size = 4,
674 .max_access_size = 4,
676 .endianness = DEVICE_NATIVE_ENDIAN,
679 static void hpet_reset(DeviceState *d)
681 HPETState *s = HPET(d);
682 SysBusDevice *sbd = SYS_BUS_DEVICE(d);
683 int i;
685 for (i = 0; i < s->num_timers; i++) {
686 HPETTimer *timer = &s->timer[i];
688 hpet_del_timer(timer);
689 timer->cmp = ~0ULL;
690 timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
691 if (s->flags & (1 << HPET_MSI_SUPPORT)) {
692 timer->config |= HPET_TN_FSB_CAP;
694 /* advertise availability of ioapic int */
695 timer->config |= (uint64_t)s->intcap << 32;
696 timer->period = 0ULL;
697 timer->wrap_flag = 0;
700 qemu_set_irq(s->pit_enabled, 1);
701 s->hpet_counter = 0ULL;
702 s->hpet_offset = 0ULL;
703 s->config = 0ULL;
704 hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
705 hpet_cfg.hpet[s->hpet_id].address = sbd->mmio[0].addr;
707 /* to document that the RTC lowers its output on reset as well */
708 s->rtc_irq_level = 0;
711 static void hpet_handle_legacy_irq(void *opaque, int n, int level)
713 HPETState *s = HPET(opaque);
715 if (n == HPET_LEGACY_PIT_INT) {
716 if (!hpet_in_legacy_mode(s)) {
717 qemu_set_irq(s->irqs[0], level);
719 } else {
720 s->rtc_irq_level = level;
721 if (!hpet_in_legacy_mode(s)) {
722 qemu_set_irq(s->irqs[RTC_ISA_IRQ], level);
727 static void hpet_init(Object *obj)
729 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
730 HPETState *s = HPET(obj);
732 /* HPET Area */
733 memory_region_init_io(&s->iomem, obj, &hpet_ram_ops, s, "hpet", HPET_LEN);
734 sysbus_init_mmio(sbd, &s->iomem);
737 static void hpet_realize(DeviceState *dev, Error **errp)
739 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
740 HPETState *s = HPET(dev);
741 int i;
742 HPETTimer *timer;
744 if (!s->intcap) {
745 warn_report("Hpet's intcap not initialized");
747 if (hpet_cfg.count == UINT8_MAX) {
748 /* first instance */
749 hpet_cfg.count = 0;
752 if (hpet_cfg.count == 8) {
753 error_setg(errp, "Only 8 instances of HPET is allowed");
754 return;
757 s->hpet_id = hpet_cfg.count++;
759 for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) {
760 sysbus_init_irq(sbd, &s->irqs[i]);
763 if (s->num_timers < HPET_MIN_TIMERS) {
764 s->num_timers = HPET_MIN_TIMERS;
765 } else if (s->num_timers > HPET_MAX_TIMERS) {
766 s->num_timers = HPET_MAX_TIMERS;
768 for (i = 0; i < HPET_MAX_TIMERS; i++) {
769 timer = &s->timer[i];
770 timer->qemu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hpet_timer, timer);
771 timer->tn = i;
772 timer->state = s;
775 /* 64-bit main counter; LegacyReplacementRoute. */
776 s->capability = 0x8086a001ULL;
777 s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
778 s->capability |= ((uint64_t)(HPET_CLK_PERIOD * FS_PER_NS) << 32);
780 qdev_init_gpio_in(dev, hpet_handle_legacy_irq, 2);
781 qdev_init_gpio_out(dev, &s->pit_enabled, 1);
784 static Property hpet_device_properties[] = {
785 DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
786 DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false),
787 DEFINE_PROP_UINT32(HPET_INTCAP, HPETState, intcap, 0),
788 DEFINE_PROP_BOOL("hpet-offset-saved", HPETState, hpet_offset_saved, true),
789 DEFINE_PROP_END_OF_LIST(),
792 static void hpet_device_class_init(ObjectClass *klass, void *data)
794 DeviceClass *dc = DEVICE_CLASS(klass);
796 dc->realize = hpet_realize;
797 dc->reset = hpet_reset;
798 dc->vmsd = &vmstate_hpet;
799 device_class_set_props(dc, hpet_device_properties);
802 static const TypeInfo hpet_device_info = {
803 .name = TYPE_HPET,
804 .parent = TYPE_SYS_BUS_DEVICE,
805 .instance_size = sizeof(HPETState),
806 .instance_init = hpet_init,
807 .class_init = hpet_device_class_init,
810 static void hpet_register_types(void)
812 type_register_static(&hpet_device_info);
815 type_init(hpet_register_types)