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 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"
29 #include "hw/i386/pc.h"
30 #include "ui/console.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "hw/timer/hpet.h"
35 #include "hw/sysbus.h"
36 #include "hw/timer/mc146818rtc.h"
37 #include "hw/timer/i8254.h"
41 #define DPRINTF printf
46 #define HPET_MSI_SUPPORT 0
48 #define HPET(obj) OBJECT_CHECK(HPETState, (obj), TYPE_HPET)
51 typedef struct HPETTimer
{ /* timers */
52 uint8_t tn
; /*timer number*/
53 QEMUTimer
*qemu_timer
;
54 struct HPETState
*state
;
55 /* Memory-mapped, software visible timer registers */
56 uint64_t config
; /* configuration/cap */
57 uint64_t cmp
; /* comparator */
58 uint64_t fsb
; /* FSB route */
59 /* Hidden register state */
60 uint64_t period
; /* Last value written to comparator */
61 uint8_t wrap_flag
; /* timer pop will indicate wrap for one-shot 32-bit
62 * mode. Next pop will be actual timer expiration.
66 typedef struct HPETState
{
68 SysBusDevice parent_obj
;
73 bool hpet_offset_saved
;
74 qemu_irq irqs
[HPET_NUM_IRQ_ROUTES
];
76 uint8_t rtc_irq_level
;
80 HPETTimer timer
[HPET_MAX_TIMERS
];
82 /* Memory-mapped, software visible registers */
83 uint64_t capability
; /* capabilities */
84 uint64_t config
; /* configuration */
85 uint64_t isr
; /* interrupt status reg */
86 uint64_t hpet_counter
; /* main counter */
87 uint8_t hpet_id
; /* instance id */
90 static uint32_t hpet_in_legacy_mode(HPETState
*s
)
92 return s
->config
& HPET_CFG_LEGACY
;
95 static uint32_t timer_int_route(struct HPETTimer
*timer
)
97 return (timer
->config
& HPET_TN_INT_ROUTE_MASK
) >> HPET_TN_INT_ROUTE_SHIFT
;
100 static uint32_t timer_fsb_route(HPETTimer
*t
)
102 return t
->config
& HPET_TN_FSB_ENABLE
;
105 static uint32_t hpet_enabled(HPETState
*s
)
107 return s
->config
& HPET_CFG_ENABLE
;
110 static uint32_t timer_is_periodic(HPETTimer
*t
)
112 return t
->config
& HPET_TN_PERIODIC
;
115 static uint32_t timer_enabled(HPETTimer
*t
)
117 return t
->config
& HPET_TN_ENABLE
;
120 static uint32_t hpet_time_after(uint64_t a
, uint64_t b
)
122 return ((int32_t)(b
- a
) < 0);
125 static uint32_t hpet_time_after64(uint64_t a
, uint64_t b
)
127 return ((int64_t)(b
- a
) < 0);
130 static uint64_t ticks_to_ns(uint64_t value
)
132 return value
* HPET_CLK_PERIOD
;
135 static uint64_t ns_to_ticks(uint64_t value
)
137 return value
/ HPET_CLK_PERIOD
;
140 static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old
, uint64_t mask
)
147 static int activating_bit(uint64_t old
, uint64_t new, uint64_t mask
)
149 return (!(old
& mask
) && (new & mask
));
152 static int deactivating_bit(uint64_t old
, uint64_t new, uint64_t mask
)
154 return ((old
& mask
) && !(new & mask
));
157 static uint64_t hpet_get_ticks(HPETState
*s
)
159 return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->hpet_offset
);
163 * calculate diff between comparator value and current ticks
165 static inline uint64_t hpet_calculate_diff(HPETTimer
*t
, uint64_t current
)
168 if (t
->config
& HPET_TN_32BIT
) {
171 cmp
= (uint32_t)t
->cmp
;
172 diff
= cmp
- (uint32_t)current
;
173 diff
= (int32_t)diff
> 0 ? diff
: (uint32_t)1;
174 return (uint64_t)diff
;
179 diff
= cmp
- current
;
180 diff
= (int64_t)diff
> 0 ? diff
: (uint64_t)1;
185 static void update_irq(struct HPETTimer
*timer
, int set
)
191 if (timer
->tn
<= 1 && hpet_in_legacy_mode(timer
->state
)) {
192 /* if LegacyReplacementRoute bit is set, HPET specification requires
193 * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
194 * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
196 route
= (timer
->tn
== 0) ? 0 : RTC_ISA_IRQ
;
198 route
= timer_int_route(timer
);
201 mask
= 1 << timer
->tn
;
202 if (!set
|| !timer_enabled(timer
) || !hpet_enabled(timer
->state
)) {
204 if (!timer_fsb_route(timer
)) {
205 qemu_irq_lower(s
->irqs
[route
]);
207 } else if (timer_fsb_route(timer
)) {
208 address_space_stl_le(&address_space_memory
, timer
->fsb
>> 32,
209 timer
->fsb
& 0xffffffff, MEMTXATTRS_UNSPECIFIED
,
211 } else if (timer
->config
& HPET_TN_TYPE_LEVEL
) {
213 qemu_irq_raise(s
->irqs
[route
]);
216 qemu_irq_pulse(s
->irqs
[route
]);
220 static int hpet_pre_save(void *opaque
)
222 HPETState
*s
= opaque
;
224 /* save current counter value */
225 if (hpet_enabled(s
)) {
226 s
->hpet_counter
= hpet_get_ticks(s
);
232 static int hpet_pre_load(void *opaque
)
234 HPETState
*s
= opaque
;
236 /* version 1 only supports 3, later versions will load the actual value */
237 s
->num_timers
= HPET_MIN_TIMERS
;
241 static bool hpet_validate_num_timers(void *opaque
, int version_id
)
243 HPETState
*s
= opaque
;
245 if (s
->num_timers
< HPET_MIN_TIMERS
) {
247 } else if (s
->num_timers
> HPET_MAX_TIMERS
) {
253 static int hpet_post_load(void *opaque
, int version_id
)
255 HPETState
*s
= opaque
;
257 /* Recalculate the offset between the main counter and guest time */
258 if (!s
->hpet_offset_saved
) {
259 s
->hpet_offset
= ticks_to_ns(s
->hpet_counter
)
260 - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
263 /* Push number of timers into capability returned via HPET_ID */
264 s
->capability
&= ~HPET_ID_NUM_TIM_MASK
;
265 s
->capability
|= (s
->num_timers
- 1) << HPET_ID_NUM_TIM_SHIFT
;
266 hpet_cfg
.hpet
[s
->hpet_id
].event_timer_block_id
= (uint32_t)s
->capability
;
268 /* Derive HPET_MSI_SUPPORT from the capability of the first timer. */
269 s
->flags
&= ~(1 << HPET_MSI_SUPPORT
);
270 if (s
->timer
[0].config
& HPET_TN_FSB_CAP
) {
271 s
->flags
|= 1 << HPET_MSI_SUPPORT
;
276 static bool hpet_offset_needed(void *opaque
)
278 HPETState
*s
= opaque
;
280 return hpet_enabled(s
) && s
->hpet_offset_saved
;
283 static bool hpet_rtc_irq_level_needed(void *opaque
)
285 HPETState
*s
= opaque
;
287 return s
->rtc_irq_level
!= 0;
290 static const VMStateDescription vmstate_hpet_rtc_irq_level
= {
291 .name
= "hpet/rtc_irq_level",
293 .minimum_version_id
= 1,
294 .needed
= hpet_rtc_irq_level_needed
,
295 .fields
= (VMStateField
[]) {
296 VMSTATE_UINT8(rtc_irq_level
, HPETState
),
297 VMSTATE_END_OF_LIST()
301 static const VMStateDescription vmstate_hpet_offset
= {
302 .name
= "hpet/offset",
304 .minimum_version_id
= 1,
305 .needed
= hpet_offset_needed
,
306 .fields
= (VMStateField
[]) {
307 VMSTATE_UINT64(hpet_offset
, HPETState
),
308 VMSTATE_END_OF_LIST()
312 static const VMStateDescription vmstate_hpet_timer
= {
313 .name
= "hpet_timer",
315 .minimum_version_id
= 1,
316 .fields
= (VMStateField
[]) {
317 VMSTATE_UINT8(tn
, HPETTimer
),
318 VMSTATE_UINT64(config
, HPETTimer
),
319 VMSTATE_UINT64(cmp
, HPETTimer
),
320 VMSTATE_UINT64(fsb
, HPETTimer
),
321 VMSTATE_UINT64(period
, HPETTimer
),
322 VMSTATE_UINT8(wrap_flag
, HPETTimer
),
323 VMSTATE_TIMER_PTR(qemu_timer
, HPETTimer
),
324 VMSTATE_END_OF_LIST()
328 static const VMStateDescription vmstate_hpet
= {
331 .minimum_version_id
= 1,
332 .pre_save
= hpet_pre_save
,
333 .pre_load
= hpet_pre_load
,
334 .post_load
= hpet_post_load
,
335 .fields
= (VMStateField
[]) {
336 VMSTATE_UINT64(config
, HPETState
),
337 VMSTATE_UINT64(isr
, HPETState
),
338 VMSTATE_UINT64(hpet_counter
, HPETState
),
339 VMSTATE_UINT8_V(num_timers
, HPETState
, 2),
340 VMSTATE_VALIDATE("num_timers in range", hpet_validate_num_timers
),
341 VMSTATE_STRUCT_VARRAY_UINT8(timer
, HPETState
, num_timers
, 0,
342 vmstate_hpet_timer
, HPETTimer
),
343 VMSTATE_END_OF_LIST()
345 .subsections
= (const VMStateDescription
*[]) {
346 &vmstate_hpet_rtc_irq_level
,
347 &vmstate_hpet_offset
,
353 * timer expiration callback
355 static void hpet_timer(void *opaque
)
357 HPETTimer
*t
= opaque
;
360 uint64_t period
= t
->period
;
361 uint64_t cur_tick
= hpet_get_ticks(t
->state
);
363 if (timer_is_periodic(t
) && period
!= 0) {
364 if (t
->config
& HPET_TN_32BIT
) {
365 while (hpet_time_after(cur_tick
, t
->cmp
)) {
366 t
->cmp
= (uint32_t)(t
->cmp
+ t
->period
);
369 while (hpet_time_after64(cur_tick
, t
->cmp
)) {
373 diff
= hpet_calculate_diff(t
, cur_tick
);
374 timer_mod(t
->qemu_timer
,
375 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + (int64_t)ticks_to_ns(diff
));
376 } else if (t
->config
& HPET_TN_32BIT
&& !timer_is_periodic(t
)) {
378 diff
= hpet_calculate_diff(t
, cur_tick
);
379 timer_mod(t
->qemu_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
380 (int64_t)ticks_to_ns(diff
));
387 static void hpet_set_timer(HPETTimer
*t
)
390 uint32_t wrap_diff
; /* how many ticks until we wrap? */
391 uint64_t cur_tick
= hpet_get_ticks(t
->state
);
393 /* whenever new timer is being set up, make sure wrap_flag is 0 */
395 diff
= hpet_calculate_diff(t
, cur_tick
);
397 /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
398 * counter wraps in addition to an interrupt with comparator match.
400 if (t
->config
& HPET_TN_32BIT
&& !timer_is_periodic(t
)) {
401 wrap_diff
= 0xffffffff - (uint32_t)cur_tick
;
402 if (wrap_diff
< (uint32_t)diff
) {
407 timer_mod(t
->qemu_timer
,
408 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + (int64_t)ticks_to_ns(diff
));
411 static void hpet_del_timer(HPETTimer
*t
)
413 timer_del(t
->qemu_timer
);
418 static uint32_t hpet_ram_readb(void *opaque
, hwaddr addr
)
420 printf("qemu: hpet_read b at %" PRIx64
"\n", addr
);
424 static uint32_t hpet_ram_readw(void *opaque
, hwaddr addr
)
426 printf("qemu: hpet_read w at %" PRIx64
"\n", addr
);
431 static uint64_t hpet_ram_read(void *opaque
, hwaddr addr
,
434 HPETState
*s
= opaque
;
435 uint64_t cur_tick
, index
;
437 DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64
"\n", addr
);
439 /*address range of all TN regs*/
440 if (index
>= 0x100 && index
<= 0x3ff) {
441 uint8_t timer_id
= (addr
- 0x100) / 0x20;
442 HPETTimer
*timer
= &s
->timer
[timer_id
];
444 if (timer_id
> s
->num_timers
) {
445 DPRINTF("qemu: timer id out of range\n");
449 switch ((addr
- 0x100) % 0x20) {
451 return timer
->config
;
452 case HPET_TN_CFG
+ 4: // Interrupt capabilities
453 return timer
->config
>> 32;
454 case HPET_TN_CMP
: // comparator register
456 case HPET_TN_CMP
+ 4:
457 return timer
->cmp
>> 32;
460 case HPET_TN_ROUTE
+ 4:
461 return timer
->fsb
>> 32;
463 DPRINTF("qemu: invalid hpet_ram_readl\n");
469 return s
->capability
;
471 return s
->capability
>> 32;
475 DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl\n");
478 if (hpet_enabled(s
)) {
479 cur_tick
= hpet_get_ticks(s
);
481 cur_tick
= s
->hpet_counter
;
483 DPRINTF("qemu: reading counter = %" PRIx64
"\n", cur_tick
);
485 case HPET_COUNTER
+ 4:
486 if (hpet_enabled(s
)) {
487 cur_tick
= hpet_get_ticks(s
);
489 cur_tick
= s
->hpet_counter
;
491 DPRINTF("qemu: reading counter + 4 = %" PRIx64
"\n", cur_tick
);
492 return cur_tick
>> 32;
496 DPRINTF("qemu: invalid hpet_ram_readl\n");
503 static void hpet_ram_write(void *opaque
, hwaddr addr
,
504 uint64_t value
, unsigned size
)
507 HPETState
*s
= opaque
;
508 uint64_t old_val
, new_val
, val
, index
;
510 DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64
" = %#x\n", addr
, value
);
512 old_val
= hpet_ram_read(opaque
, addr
, 4);
515 /*address range of all TN regs*/
516 if (index
>= 0x100 && index
<= 0x3ff) {
517 uint8_t timer_id
= (addr
- 0x100) / 0x20;
518 HPETTimer
*timer
= &s
->timer
[timer_id
];
520 DPRINTF("qemu: hpet_ram_writel timer_id = %#x\n", timer_id
);
521 if (timer_id
> s
->num_timers
) {
522 DPRINTF("qemu: timer id out of range\n");
525 switch ((addr
- 0x100) % 0x20) {
527 DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
528 if (activating_bit(old_val
, new_val
, HPET_TN_FSB_ENABLE
)) {
529 update_irq(timer
, 0);
531 val
= hpet_fixup_reg(new_val
, old_val
, HPET_TN_CFG_WRITE_MASK
);
532 timer
->config
= (timer
->config
& 0xffffffff00000000ULL
) | val
;
533 if (new_val
& HPET_TN_32BIT
) {
534 timer
->cmp
= (uint32_t)timer
->cmp
;
535 timer
->period
= (uint32_t)timer
->period
;
537 if (activating_bit(old_val
, new_val
, HPET_TN_ENABLE
) &&
539 hpet_set_timer(timer
);
540 } else if (deactivating_bit(old_val
, new_val
, HPET_TN_ENABLE
)) {
541 hpet_del_timer(timer
);
544 case HPET_TN_CFG
+ 4: // Interrupt capabilities
545 DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
547 case HPET_TN_CMP
: // comparator register
548 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP\n");
549 if (timer
->config
& HPET_TN_32BIT
) {
550 new_val
= (uint32_t)new_val
;
552 if (!timer_is_periodic(timer
)
553 || (timer
->config
& HPET_TN_SETVAL
)) {
554 timer
->cmp
= (timer
->cmp
& 0xffffffff00000000ULL
) | new_val
;
556 if (timer_is_periodic(timer
)) {
558 * FIXME: Clamp period to reasonable min value?
559 * Clamp period to reasonable max value
561 new_val
&= (timer
->config
& HPET_TN_32BIT
? ~0u : ~0ull) >> 1;
563 (timer
->period
& 0xffffffff00000000ULL
) | new_val
;
565 timer
->config
&= ~HPET_TN_SETVAL
;
566 if (hpet_enabled(s
)) {
567 hpet_set_timer(timer
);
570 case HPET_TN_CMP
+ 4: // comparator register high order
571 DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
572 if (!timer_is_periodic(timer
)
573 || (timer
->config
& HPET_TN_SETVAL
)) {
574 timer
->cmp
= (timer
->cmp
& 0xffffffffULL
) | new_val
<< 32;
577 * FIXME: Clamp period to reasonable min value?
578 * Clamp period to reasonable max value
580 new_val
&= (timer
->config
& HPET_TN_32BIT
? ~0u : ~0ull) >> 1;
582 (timer
->period
& 0xffffffffULL
) | new_val
<< 32;
584 timer
->config
&= ~HPET_TN_SETVAL
;
585 if (hpet_enabled(s
)) {
586 hpet_set_timer(timer
);
590 timer
->fsb
= (timer
->fsb
& 0xffffffff00000000ULL
) | new_val
;
592 case HPET_TN_ROUTE
+ 4:
593 timer
->fsb
= (new_val
<< 32) | (timer
->fsb
& 0xffffffff);
596 DPRINTF("qemu: invalid hpet_ram_writel\n");
605 val
= hpet_fixup_reg(new_val
, old_val
, HPET_CFG_WRITE_MASK
);
606 s
->config
= (s
->config
& 0xffffffff00000000ULL
) | val
;
607 if (activating_bit(old_val
, new_val
, HPET_CFG_ENABLE
)) {
608 /* Enable main counter and interrupt generation. */
610 ticks_to_ns(s
->hpet_counter
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
611 for (i
= 0; i
< s
->num_timers
; i
++) {
612 if ((&s
->timer
[i
])->cmp
!= ~0ULL) {
613 hpet_set_timer(&s
->timer
[i
]);
616 } else if (deactivating_bit(old_val
, new_val
, HPET_CFG_ENABLE
)) {
617 /* Halt main counter and disable interrupt generation. */
618 s
->hpet_counter
= hpet_get_ticks(s
);
619 for (i
= 0; i
< s
->num_timers
; i
++) {
620 hpet_del_timer(&s
->timer
[i
]);
623 /* i8254 and RTC output pins are disabled
624 * when HPET is in legacy mode */
625 if (activating_bit(old_val
, new_val
, HPET_CFG_LEGACY
)) {
626 qemu_set_irq(s
->pit_enabled
, 0);
627 qemu_irq_lower(s
->irqs
[0]);
628 qemu_irq_lower(s
->irqs
[RTC_ISA_IRQ
]);
629 } else if (deactivating_bit(old_val
, new_val
, HPET_CFG_LEGACY
)) {
630 qemu_irq_lower(s
->irqs
[0]);
631 qemu_set_irq(s
->pit_enabled
, 1);
632 qemu_set_irq(s
->irqs
[RTC_ISA_IRQ
], s
->rtc_irq_level
);
636 DPRINTF("qemu: invalid HPET_CFG+4 write\n");
639 val
= new_val
& s
->isr
;
640 for (i
= 0; i
< s
->num_timers
; i
++) {
641 if (val
& (1 << i
)) {
642 update_irq(&s
->timer
[i
], 0);
647 if (hpet_enabled(s
)) {
648 DPRINTF("qemu: Writing counter while HPET enabled!\n");
651 (s
->hpet_counter
& 0xffffffff00000000ULL
) | value
;
652 DPRINTF("qemu: HPET counter written. ctr = %#x -> %" PRIx64
"\n",
653 value
, s
->hpet_counter
);
655 case HPET_COUNTER
+ 4:
656 if (hpet_enabled(s
)) {
657 DPRINTF("qemu: Writing counter while HPET enabled!\n");
660 (s
->hpet_counter
& 0xffffffffULL
) | (((uint64_t)value
) << 32);
661 DPRINTF("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64
"\n",
662 value
, s
->hpet_counter
);
665 DPRINTF("qemu: invalid hpet_ram_writel\n");
671 static const MemoryRegionOps hpet_ram_ops
= {
672 .read
= hpet_ram_read
,
673 .write
= hpet_ram_write
,
675 .min_access_size
= 4,
676 .max_access_size
= 4,
678 .endianness
= DEVICE_NATIVE_ENDIAN
,
681 static void hpet_reset(DeviceState
*d
)
683 HPETState
*s
= HPET(d
);
684 SysBusDevice
*sbd
= SYS_BUS_DEVICE(d
);
687 for (i
= 0; i
< s
->num_timers
; i
++) {
688 HPETTimer
*timer
= &s
->timer
[i
];
690 hpet_del_timer(timer
);
692 timer
->config
= HPET_TN_PERIODIC_CAP
| HPET_TN_SIZE_CAP
;
693 if (s
->flags
& (1 << HPET_MSI_SUPPORT
)) {
694 timer
->config
|= HPET_TN_FSB_CAP
;
696 /* advertise availability of ioapic int */
697 timer
->config
|= (uint64_t)s
->intcap
<< 32;
698 timer
->period
= 0ULL;
699 timer
->wrap_flag
= 0;
702 qemu_set_irq(s
->pit_enabled
, 1);
703 s
->hpet_counter
= 0ULL;
704 s
->hpet_offset
= 0ULL;
706 hpet_cfg
.hpet
[s
->hpet_id
].event_timer_block_id
= (uint32_t)s
->capability
;
707 hpet_cfg
.hpet
[s
->hpet_id
].address
= sbd
->mmio
[0].addr
;
709 /* to document that the RTC lowers its output on reset as well */
710 s
->rtc_irq_level
= 0;
713 static void hpet_handle_legacy_irq(void *opaque
, int n
, int level
)
715 HPETState
*s
= HPET(opaque
);
717 if (n
== HPET_LEGACY_PIT_INT
) {
718 if (!hpet_in_legacy_mode(s
)) {
719 qemu_set_irq(s
->irqs
[0], level
);
722 s
->rtc_irq_level
= level
;
723 if (!hpet_in_legacy_mode(s
)) {
724 qemu_set_irq(s
->irqs
[RTC_ISA_IRQ
], level
);
729 static void hpet_init(Object
*obj
)
731 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
732 HPETState
*s
= HPET(obj
);
735 memory_region_init_io(&s
->iomem
, obj
, &hpet_ram_ops
, s
, "hpet", HPET_LEN
);
736 sysbus_init_mmio(sbd
, &s
->iomem
);
739 static void hpet_realize(DeviceState
*dev
, Error
**errp
)
741 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
742 HPETState
*s
= HPET(dev
);
747 error_printf("Hpet's intcap not initialized.\n");
749 if (hpet_cfg
.count
== UINT8_MAX
) {
754 if (hpet_cfg
.count
== 8) {
755 error_setg(errp
, "Only 8 instances of HPET is allowed");
759 s
->hpet_id
= hpet_cfg
.count
++;
761 for (i
= 0; i
< HPET_NUM_IRQ_ROUTES
; i
++) {
762 sysbus_init_irq(sbd
, &s
->irqs
[i
]);
765 if (s
->num_timers
< HPET_MIN_TIMERS
) {
766 s
->num_timers
= HPET_MIN_TIMERS
;
767 } else if (s
->num_timers
> HPET_MAX_TIMERS
) {
768 s
->num_timers
= HPET_MAX_TIMERS
;
770 for (i
= 0; i
< HPET_MAX_TIMERS
; i
++) {
771 timer
= &s
->timer
[i
];
772 timer
->qemu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, hpet_timer
, timer
);
777 /* 64-bit main counter; LegacyReplacementRoute. */
778 s
->capability
= 0x8086a001ULL
;
779 s
->capability
|= (s
->num_timers
- 1) << HPET_ID_NUM_TIM_SHIFT
;
780 s
->capability
|= ((uint64_t)(HPET_CLK_PERIOD
* FS_PER_NS
) << 32);
782 qdev_init_gpio_in(dev
, hpet_handle_legacy_irq
, 2);
783 qdev_init_gpio_out(dev
, &s
->pit_enabled
, 1);
786 static Property hpet_device_properties
[] = {
787 DEFINE_PROP_UINT8("timers", HPETState
, num_timers
, HPET_MIN_TIMERS
),
788 DEFINE_PROP_BIT("msi", HPETState
, flags
, HPET_MSI_SUPPORT
, false),
789 DEFINE_PROP_UINT32(HPET_INTCAP
, HPETState
, intcap
, 0),
790 DEFINE_PROP_BOOL("hpet-offset-saved", HPETState
, hpet_offset_saved
, true),
791 DEFINE_PROP_END_OF_LIST(),
794 static void hpet_device_class_init(ObjectClass
*klass
, void *data
)
796 DeviceClass
*dc
= DEVICE_CLASS(klass
);
798 dc
->realize
= hpet_realize
;
799 dc
->reset
= hpet_reset
;
800 dc
->vmsd
= &vmstate_hpet
;
801 dc
->props
= hpet_device_properties
;
804 static const TypeInfo hpet_device_info
= {
806 .parent
= TYPE_SYS_BUS_DEVICE
,
807 .instance_size
= sizeof(HPETState
),
808 .instance_init
= hpet_init
,
809 .class_init
= hpet_device_class_init
,
812 static void hpet_register_types(void)
814 type_register_static(&hpet_device_info
);
817 type_init(hpet_register_types
)