2008-12-18 02:28:44 +03:00
|
|
|
/*
|
2014-12-11 12:18:29 +03:00
|
|
|
* High Precision Event Timer emulation
|
2008-12-18 02:28:44 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Alexander Graf
|
|
|
|
* Copyright (c) 2008 IBM Corporation
|
|
|
|
*
|
|
|
|
* Authors: Beth Kon <bkon@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 15:44:24 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-12-18 02:28:44 +03:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-07-17 00:47:01 +04:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 02:28:44 +03:00
|
|
|
*
|
|
|
|
* *****************************************************************
|
|
|
|
*
|
|
|
|
* This driver attempts to emulate an HPET device in software.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2015-03-17 20:29:20 +03:00
|
|
|
#include "qemu/error-report.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2023-02-15 19:23:49 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/timer/hpet.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-10-04 02:03:53 +03:00
|
|
|
#include "hw/rtc/mc146818rtc.h"
|
2019-10-04 02:04:02 +03:00
|
|
|
#include "hw/rtc/mc146818rtc_regs.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/timer/i8254.h"
|
2020-02-28 14:46:42 +03:00
|
|
|
#include "exec/address-spaces.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2023-11-19 02:11:29 +03:00
|
|
|
#include "trace.h"
|
2008-12-18 02:28:44 +03:00
|
|
|
|
2010-06-13 16:15:45 +04:00
|
|
|
#define HPET_MSI_SUPPORT 0
|
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(HPETState, HPET)
|
2013-07-01 14:18:36 +04:00
|
|
|
|
2010-06-13 16:15:35 +04:00
|
|
|
struct HPETState;
|
|
|
|
typedef struct HPETTimer { /* timers */
|
|
|
|
uint8_t tn; /*timer number*/
|
|
|
|
QEMUTimer *qemu_timer;
|
|
|
|
struct HPETState *state;
|
|
|
|
/* Memory-mapped, software visible timer registers */
|
|
|
|
uint64_t config; /* configuration/cap */
|
|
|
|
uint64_t cmp; /* comparator */
|
2010-06-13 16:15:45 +04:00
|
|
|
uint64_t fsb; /* FSB route */
|
2010-06-13 16:15:35 +04:00
|
|
|
/* Hidden register state */
|
2024-07-16 13:26:36 +03:00
|
|
|
uint64_t cmp64; /* comparator (extended to counter width) */
|
2010-06-13 16:15:35 +04:00
|
|
|
uint64_t period; /* Last value written to comparator */
|
|
|
|
uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 32-bit
|
|
|
|
* mode. Next pop will be actual timer expiration.
|
|
|
|
*/
|
2024-07-16 13:35:39 +03:00
|
|
|
uint64_t last; /* last value armed, to avoid timer storms */
|
2010-06-13 16:15:35 +04:00
|
|
|
} HPETTimer;
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct HPETState {
|
2013-07-01 14:18:36 +04:00
|
|
|
/*< private >*/
|
|
|
|
SysBusDevice parent_obj;
|
|
|
|
/*< public >*/
|
|
|
|
|
2011-11-09 18:10:07 +04:00
|
|
|
MemoryRegion iomem;
|
2010-06-13 16:15:35 +04:00
|
|
|
uint64_t hpet_offset;
|
2018-01-11 11:24:58 +03:00
|
|
|
bool hpet_offset_saved;
|
2010-06-13 16:15:38 +04:00
|
|
|
qemu_irq irqs[HPET_NUM_IRQ_ROUTES];
|
2010-06-13 16:15:45 +04:00
|
|
|
uint32_t flags;
|
2010-06-13 16:15:40 +04:00
|
|
|
uint8_t rtc_irq_level;
|
2012-02-01 23:31:41 +04:00
|
|
|
qemu_irq pit_enabled;
|
2010-06-13 16:15:44 +04:00
|
|
|
uint8_t num_timers;
|
2013-12-08 13:38:17 +04:00
|
|
|
uint32_t intcap;
|
2010-06-13 16:15:44 +04:00
|
|
|
HPETTimer timer[HPET_MAX_TIMERS];
|
2010-06-13 16:15:35 +04:00
|
|
|
|
|
|
|
/* Memory-mapped, software visible registers */
|
|
|
|
uint64_t capability; /* capabilities */
|
|
|
|
uint64_t config; /* configuration */
|
|
|
|
uint64_t isr; /* interrupt status reg */
|
|
|
|
uint64_t hpet_counter; /* main counter */
|
2010-06-14 12:29:28 +04:00
|
|
|
uint8_t hpet_id; /* instance id */
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2010-06-13 16:15:35 +04:00
|
|
|
|
2010-06-13 16:15:40 +04:00
|
|
|
static uint32_t hpet_in_legacy_mode(HPETState *s)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2010-06-13 16:15:40 +04:00
|
|
|
return s->config & HPET_CFG_LEGACY;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2008-12-19 01:42:43 +03:00
|
|
|
static uint32_t timer_int_route(struct HPETTimer *timer)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2010-06-13 16:15:35 +04:00
|
|
|
return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2010-06-13 16:15:45 +04:00
|
|
|
static uint32_t timer_fsb_route(HPETTimer *t)
|
|
|
|
{
|
|
|
|
return t->config & HPET_TN_FSB_ENABLE;
|
|
|
|
}
|
|
|
|
|
2010-06-13 16:15:41 +04:00
|
|
|
static uint32_t hpet_enabled(HPETState *s)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2010-06-13 16:15:41 +04:00
|
|
|
return s->config & HPET_CFG_ENABLE;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t timer_is_periodic(HPETTimer *t)
|
|
|
|
{
|
|
|
|
return t->config & HPET_TN_PERIODIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t timer_enabled(HPETTimer *t)
|
|
|
|
{
|
|
|
|
return t->config & HPET_TN_ENABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t hpet_time_after(uint64_t a, uint64_t b)
|
|
|
|
{
|
2015-11-09 17:56:31 +03:00
|
|
|
return ((int64_t)(b - a) < 0);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2008-12-19 01:42:43 +03:00
|
|
|
static uint64_t ticks_to_ns(uint64_t value)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2015-08-25 18:13:01 +03:00
|
|
|
return value * HPET_CLK_PERIOD;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2008-12-19 01:42:43 +03:00
|
|
|
static uint64_t ns_to_ticks(uint64_t value)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2015-08-25 18:13:01 +03:00
|
|
|
return value / HPET_CLK_PERIOD;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
|
|
|
|
{
|
|
|
|
new &= mask;
|
|
|
|
new |= old & ~mask;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
|
|
|
|
{
|
2008-12-19 01:42:43 +03:00
|
|
|
return (!(old & mask) && (new & mask));
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
|
|
|
|
{
|
2008-12-19 01:42:43 +03:00
|
|
|
return ((old & mask) && !(new & mask));
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2010-06-13 16:15:41 +04:00
|
|
|
static uint64_t hpet_get_ticks(HPETState *s)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2013-08-21 19:03:08 +04:00
|
|
|
return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hpet_offset);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2024-07-16 13:26:36 +03:00
|
|
|
static uint64_t hpet_get_ns(HPETState *s, uint64_t tick)
|
|
|
|
{
|
|
|
|
return ticks_to_ns(tick) - s->hpet_offset;
|
|
|
|
}
|
|
|
|
|
2008-12-19 01:42:43 +03:00
|
|
|
/*
|
2024-07-16 13:26:36 +03:00
|
|
|
* calculate next value of the general counter that matches the
|
|
|
|
* target (either entirely, or the low 32-bit only depending on
|
|
|
|
* the timer mode).
|
2008-12-18 02:28:44 +03:00
|
|
|
*/
|
2024-07-16 13:26:36 +03:00
|
|
|
static uint64_t hpet_calculate_cmp64(HPETTimer *t, uint64_t cur_tick, uint64_t target)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
|
|
|
if (t->config & HPET_TN_32BIT) {
|
2024-07-16 13:26:36 +03:00
|
|
|
uint64_t result = deposit64(cur_tick, 0, 32, target);
|
|
|
|
if (result < cur_tick) {
|
|
|
|
result += 0x100000000ULL;
|
|
|
|
}
|
|
|
|
return result;
|
2008-12-18 02:28:44 +03:00
|
|
|
} else {
|
2024-07-16 13:26:36 +03:00
|
|
|
return target;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 13:26:36 +03:00
|
|
|
static uint64_t hpet_next_wrap(uint64_t cur_tick)
|
|
|
|
{
|
|
|
|
return (cur_tick | 0xffffffffU) + 1;
|
|
|
|
}
|
|
|
|
|
2010-06-13 16:15:42 +04:00
|
|
|
static void update_irq(struct HPETTimer *timer, int set)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2010-06-13 16:15:42 +04:00
|
|
|
uint64_t mask;
|
|
|
|
HPETState *s;
|
2008-12-18 02:28:44 +03:00
|
|
|
int route;
|
|
|
|
|
2010-06-13 16:15:40 +04:00
|
|
|
if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) {
|
2008-12-18 02:28:44 +03:00
|
|
|
/* if LegacyReplacementRoute bit is set, HPET specification requires
|
|
|
|
* timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
|
2008-12-19 01:42:43 +03:00
|
|
|
* timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
|
2008-12-18 02:28:44 +03:00
|
|
|
*/
|
2010-06-13 16:15:40 +04:00
|
|
|
route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ;
|
2008-12-18 02:28:44 +03:00
|
|
|
} else {
|
2010-06-13 16:15:35 +04:00
|
|
|
route = timer_int_route(timer);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:42 +04:00
|
|
|
s = timer->state;
|
|
|
|
mask = 1 << timer->tn;
|
2024-07-16 12:36:44 +03:00
|
|
|
|
|
|
|
if (set && (timer->config & HPET_TN_TYPE_LEVEL)) {
|
|
|
|
/*
|
|
|
|
* If HPET_TN_ENABLE bit is 0, "the timer will still operate and
|
|
|
|
* generate appropriate status bits, but will not cause an interrupt"
|
|
|
|
*/
|
|
|
|
s->isr |= mask;
|
|
|
|
} else {
|
2010-06-13 16:15:42 +04:00
|
|
|
s->isr &= ~mask;
|
2024-07-16 12:36:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (set && timer_enabled(timer) && hpet_enabled(s)) {
|
|
|
|
if (timer_fsb_route(timer)) {
|
|
|
|
address_space_stl_le(&address_space_memory, timer->fsb >> 32,
|
|
|
|
timer->fsb & 0xffffffff, MEMTXATTRS_UNSPECIFIED,
|
|
|
|
NULL);
|
|
|
|
} else if (timer->config & HPET_TN_TYPE_LEVEL) {
|
|
|
|
qemu_irq_raise(s->irqs[route]);
|
|
|
|
} else {
|
|
|
|
qemu_irq_pulse(s->irqs[route]);
|
|
|
|
}
|
|
|
|
} else {
|
2010-06-13 16:15:45 +04:00
|
|
|
if (!timer_fsb_route(timer)) {
|
2016-04-06 01:58:19 +03:00
|
|
|
qemu_irq_lower(s->irqs[route]);
|
2010-06-13 16:15:45 +04:00
|
|
|
}
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 14:29:12 +03:00
|
|
|
static int hpet_pre_save(void *opaque)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2009-09-30 00:48:22 +04:00
|
|
|
HPETState *s = opaque;
|
2010-06-13 16:15:35 +04:00
|
|
|
|
2008-12-18 02:28:44 +03:00
|
|
|
/* save current counter value */
|
2018-01-11 11:24:58 +03:00
|
|
|
if (hpet_enabled(s)) {
|
|
|
|
s->hpet_counter = hpet_get_ticks(s);
|
|
|
|
}
|
2017-09-25 14:29:12 +03:00
|
|
|
|
|
|
|
return 0;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2010-06-13 16:15:44 +04:00
|
|
|
static int hpet_pre_load(void *opaque)
|
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
|
|
|
|
|
|
|
/* version 1 only supports 3, later versions will load the actual value */
|
|
|
|
s->num_timers = HPET_MIN_TIMERS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:51:23 +04:00
|
|
|
static bool hpet_validate_num_timers(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
|
|
|
|
|
|
|
if (s->num_timers < HPET_MIN_TIMERS) {
|
|
|
|
return false;
|
|
|
|
} else if (s->num_timers > HPET_MAX_TIMERS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 00:48:21 +04:00
|
|
|
static int hpet_post_load(void *opaque, int version_id)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
2024-07-16 13:26:36 +03:00
|
|
|
int i;
|
2008-12-19 01:42:43 +03:00
|
|
|
|
2024-07-16 13:26:36 +03:00
|
|
|
for (i = 0; i < s->num_timers; i++) {
|
|
|
|
HPETTimer *t = &s->timer[i];
|
|
|
|
t->cmp64 = hpet_calculate_cmp64(t, s->hpet_counter, t->cmp);
|
2024-07-16 13:35:39 +03:00
|
|
|
t->last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - NANOSECONDS_PER_SECOND;
|
2024-07-16 13:26:36 +03:00
|
|
|
}
|
2008-12-18 02:28:44 +03:00
|
|
|
/* Recalculate the offset between the main counter and guest time */
|
2018-01-11 11:24:58 +03:00
|
|
|
if (!s->hpet_offset_saved) {
|
|
|
|
s->hpet_offset = ticks_to_ns(s->hpet_counter)
|
|
|
|
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
|
|
|
}
|
2010-06-13 16:15:44 +04:00
|
|
|
|
|
|
|
/* Push number of timers into capability returned via HPET_ID */
|
|
|
|
s->capability &= ~HPET_ID_NUM_TIM_MASK;
|
|
|
|
s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
|
2010-06-14 12:29:28 +04:00
|
|
|
hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
|
2010-06-13 16:15:45 +04:00
|
|
|
|
|
|
|
/* Derive HPET_MSI_SUPPORT from the capability of the first timer. */
|
|
|
|
s->flags &= ~(1 << HPET_MSI_SUPPORT);
|
|
|
|
if (s->timer[0].config & HPET_TN_FSB_CAP) {
|
|
|
|
s->flags |= 1 << HPET_MSI_SUPPORT;
|
|
|
|
}
|
2008-12-18 02:28:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:24:58 +03:00
|
|
|
static bool hpet_offset_needed(void *opaque)
|
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
|
|
|
|
|
|
|
return hpet_enabled(s) && s->hpet_offset_saved;
|
|
|
|
}
|
|
|
|
|
2012-02-01 23:31:38 +04:00
|
|
|
static bool hpet_rtc_irq_level_needed(void *opaque)
|
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
|
|
|
|
|
|
|
return s->rtc_irq_level != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_hpet_rtc_irq_level = {
|
|
|
|
.name = "hpet/rtc_irq_level",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 16:09:54 +04:00
|
|
|
.needed = hpet_rtc_irq_level_needed,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2012-02-01 23:31:38 +04:00
|
|
|
VMSTATE_UINT8(rtc_irq_level, HPETState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-11 11:24:58 +03:00
|
|
|
static const VMStateDescription vmstate_hpet_offset = {
|
|
|
|
.name = "hpet/offset",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.needed = hpet_offset_needed,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2018-01-11 11:24:58 +03:00
|
|
|
VMSTATE_UINT64(hpet_offset, HPETState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-10 05:04:45 +04:00
|
|
|
static const VMStateDescription vmstate_hpet_timer = {
|
|
|
|
.name = "hpet_timer",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2009-09-10 05:04:45 +04:00
|
|
|
VMSTATE_UINT8(tn, HPETTimer),
|
|
|
|
VMSTATE_UINT64(config, HPETTimer),
|
|
|
|
VMSTATE_UINT64(cmp, HPETTimer),
|
|
|
|
VMSTATE_UINT64(fsb, HPETTimer),
|
|
|
|
VMSTATE_UINT64(period, HPETTimer),
|
|
|
|
VMSTATE_UINT8(wrap_flag, HPETTimer),
|
2015-01-08 12:18:59 +03:00
|
|
|
VMSTATE_TIMER_PTR(qemu_timer, HPETTimer),
|
2009-09-10 05:04:45 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_hpet = {
|
|
|
|
.name = "hpet",
|
2010-06-13 16:15:44 +04:00
|
|
|
.version_id = 2,
|
2009-09-10 05:04:45 +04:00
|
|
|
.minimum_version_id = 1,
|
|
|
|
.pre_save = hpet_pre_save,
|
2010-06-13 16:15:44 +04:00
|
|
|
.pre_load = hpet_pre_load,
|
2009-09-10 05:04:45 +04:00
|
|
|
.post_load = hpet_post_load,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2009-09-10 05:04:45 +04:00
|
|
|
VMSTATE_UINT64(config, HPETState),
|
|
|
|
VMSTATE_UINT64(isr, HPETState),
|
|
|
|
VMSTATE_UINT64(hpet_counter, HPETState),
|
2010-06-13 16:15:44 +04:00
|
|
|
VMSTATE_UINT8_V(num_timers, HPETState, 2),
|
2014-04-03 20:51:23 +04:00
|
|
|
VMSTATE_VALIDATE("num_timers in range", hpet_validate_num_timers),
|
2010-06-13 16:15:44 +04:00
|
|
|
VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
|
|
|
|
vmstate_hpet_timer, HPETTimer),
|
2009-09-10 05:04:45 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
2012-02-01 23:31:38 +04:00
|
|
|
},
|
2023-12-21 06:16:37 +03:00
|
|
|
.subsections = (const VMStateDescription * const []) {
|
2014-09-23 16:09:54 +04:00
|
|
|
&vmstate_hpet_rtc_irq_level,
|
2018-01-11 11:24:58 +03:00
|
|
|
&vmstate_hpet_offset,
|
2014-09-23 16:09:54 +04:00
|
|
|
NULL
|
2009-09-10 05:04:45 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-16 13:26:36 +03:00
|
|
|
static void hpet_arm(HPETTimer *t, uint64_t tick)
|
2023-01-31 06:00:37 +03:00
|
|
|
{
|
2024-07-16 13:35:39 +03:00
|
|
|
uint64_t ns = hpet_get_ns(t->state, tick);
|
|
|
|
|
|
|
|
/* Clamp period to reasonable min value (1 us) */
|
|
|
|
if (timer_is_periodic(t) && ns - t->last < 1000) {
|
|
|
|
ns = t->last + 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->last = ns;
|
|
|
|
timer_mod(t->qemu_timer, ns);
|
2023-01-31 06:00:37 +03:00
|
|
|
}
|
|
|
|
|
2008-12-19 01:42:43 +03:00
|
|
|
/*
|
2008-12-18 02:28:44 +03:00
|
|
|
* timer expiration callback
|
|
|
|
*/
|
|
|
|
static void hpet_timer(void *opaque)
|
|
|
|
{
|
2010-06-13 16:15:35 +04:00
|
|
|
HPETTimer *t = opaque;
|
2008-12-18 02:28:44 +03:00
|
|
|
uint64_t period = t->period;
|
2010-06-13 16:15:41 +04:00
|
|
|
uint64_t cur_tick = hpet_get_ticks(t->state);
|
2008-12-18 02:28:44 +03:00
|
|
|
|
|
|
|
if (timer_is_periodic(t) && period != 0) {
|
2024-07-16 13:26:36 +03:00
|
|
|
while (hpet_time_after(cur_tick, t->cmp64)) {
|
|
|
|
t->cmp64 += period;
|
|
|
|
}
|
2008-12-18 02:28:44 +03:00
|
|
|
if (t->config & HPET_TN_32BIT) {
|
2024-07-16 13:26:36 +03:00
|
|
|
t->cmp = (uint32_t)t->cmp64;
|
2010-06-13 16:15:35 +04:00
|
|
|
} else {
|
2024-07-16 13:26:36 +03:00
|
|
|
t->cmp = t->cmp64;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2024-07-16 13:26:36 +03:00
|
|
|
hpet_arm(t, t->cmp64);
|
|
|
|
} else if (t->wrap_flag) {
|
|
|
|
t->wrap_flag = 0;
|
|
|
|
hpet_arm(t, t->cmp64);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:42 +04:00
|
|
|
update_irq(t, 1);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hpet_set_timer(HPETTimer *t)
|
|
|
|
{
|
2010-06-13 16:15:41 +04:00
|
|
|
uint64_t cur_tick = hpet_get_ticks(t->state);
|
2008-12-19 01:42:43 +03:00
|
|
|
|
2008-12-18 02:28:44 +03:00
|
|
|
t->wrap_flag = 0;
|
2024-07-16 13:26:36 +03:00
|
|
|
t->cmp64 = hpet_calculate_cmp64(t, cur_tick, t->cmp);
|
|
|
|
if (t->config & HPET_TN_32BIT) {
|
|
|
|
|
|
|
|
/* hpet spec says in one-shot 32-bit mode, generate an interrupt when
|
|
|
|
* counter wraps in addition to an interrupt with comparator match.
|
|
|
|
*/
|
|
|
|
if (!timer_is_periodic(t) && t->cmp64 > hpet_next_wrap(cur_tick)) {
|
2008-12-19 01:42:43 +03:00
|
|
|
t->wrap_flag = 1;
|
2024-07-16 13:26:36 +03:00
|
|
|
hpet_arm(t, hpet_next_wrap(cur_tick));
|
|
|
|
return;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
}
|
2024-07-16 13:26:36 +03:00
|
|
|
hpet_arm(t, t->cmp64);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hpet_del_timer(HPETTimer *t)
|
|
|
|
{
|
2024-07-16 12:36:44 +03:00
|
|
|
HPETState *s = t->state;
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_del(t->qemu_timer);
|
2024-07-16 12:36:44 +03:00
|
|
|
|
|
|
|
if (s->isr & (1 << t->tn)) {
|
|
|
|
/* For level-triggered interrupt, this leaves ISR set but lowers irq. */
|
|
|
|
update_irq(t, 1);
|
|
|
|
}
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
|
2011-11-09 18:10:07 +04:00
|
|
|
unsigned size)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
2010-06-13 16:15:35 +04:00
|
|
|
HPETState *s = opaque;
|
2024-07-10 10:58:01 +03:00
|
|
|
int shift = (addr & 4) * 8;
|
2024-07-10 11:31:48 +03:00
|
|
|
uint64_t cur_tick;
|
2008-12-18 02:28:44 +03:00
|
|
|
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_read(addr);
|
2024-07-10 11:31:48 +03:00
|
|
|
|
2008-12-18 02:28:44 +03:00
|
|
|
/*address range of all TN regs*/
|
2024-07-10 11:31:48 +03:00
|
|
|
if (addr >= 0x100 && addr <= 0x3ff) {
|
2008-12-18 02:28:44 +03:00
|
|
|
uint8_t timer_id = (addr - 0x100) / 0x20;
|
2010-06-13 16:15:35 +04:00
|
|
|
HPETTimer *timer = &s->timer[timer_id];
|
|
|
|
|
2010-06-13 16:15:44 +04:00
|
|
|
if (timer_id > s->num_timers) {
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_timer_id_out_of_range(timer_id);
|
2008-12-18 02:28:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-10 10:58:01 +03:00
|
|
|
switch (addr & 0x18) {
|
|
|
|
case HPET_TN_CFG: // including interrupt capabilities
|
|
|
|
return timer->config >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_TN_CMP: // comparator register
|
2024-07-10 10:58:01 +03:00
|
|
|
return timer->cmp >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_TN_ROUTE:
|
2024-07-10 10:58:01 +03:00
|
|
|
return timer->fsb >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
default:
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_read_invalid();
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-10 10:58:01 +03:00
|
|
|
switch (addr & ~4) {
|
|
|
|
case HPET_ID: // including HPET_PERIOD
|
|
|
|
return s->capability >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_CFG:
|
2024-07-10 10:58:01 +03:00
|
|
|
return s->config >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_COUNTER:
|
2010-06-13 16:15:41 +04:00
|
|
|
if (hpet_enabled(s)) {
|
|
|
|
cur_tick = hpet_get_ticks(s);
|
2010-06-13 16:15:35 +04:00
|
|
|
} else {
|
|
|
|
cur_tick = s->hpet_counter;
|
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
trace_hpet_ram_read_reading_counter(addr & 4, cur_tick);
|
|
|
|
return cur_tick >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_STATUS:
|
2024-07-10 10:58:01 +03:00
|
|
|
return s->isr >> shift;
|
2010-06-13 16:15:35 +04:00
|
|
|
default:
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_read_invalid();
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void hpet_ram_write(void *opaque, hwaddr addr,
|
2011-11-09 18:10:07 +04:00
|
|
|
uint64_t value, unsigned size)
|
2008-12-18 02:28:44 +03:00
|
|
|
{
|
|
|
|
int i;
|
2010-06-13 16:15:35 +04:00
|
|
|
HPETState *s = opaque;
|
2024-07-10 10:58:01 +03:00
|
|
|
int shift = (addr & 4) * 8;
|
|
|
|
int len = MIN(size * 8, 64 - shift);
|
2024-07-10 11:55:13 +03:00
|
|
|
uint64_t old_val, new_val, cleared;
|
2008-12-18 02:28:44 +03:00
|
|
|
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_write(addr, value);
|
2008-12-18 02:28:44 +03:00
|
|
|
|
|
|
|
/*address range of all TN regs*/
|
2024-07-10 11:31:48 +03:00
|
|
|
if (addr >= 0x100 && addr <= 0x3ff) {
|
2008-12-18 02:28:44 +03:00
|
|
|
uint8_t timer_id = (addr - 0x100) / 0x20;
|
|
|
|
HPETTimer *timer = &s->timer[timer_id];
|
2008-12-19 01:42:43 +03:00
|
|
|
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_write_timer_id(timer_id);
|
2010-06-13 16:15:44 +04:00
|
|
|
if (timer_id > s->num_timers) {
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_timer_id_out_of_range(timer_id);
|
2010-06-13 16:15:34 +04:00
|
|
|
return;
|
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
switch (addr & 0x18) {
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_TN_CFG:
|
2024-07-10 10:58:01 +03:00
|
|
|
trace_hpet_ram_write_tn_cfg(addr & 4);
|
|
|
|
old_val = timer->config;
|
|
|
|
new_val = deposit64(old_val, shift, len, value);
|
|
|
|
new_val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
|
2024-07-16 12:36:44 +03:00
|
|
|
if (deactivating_bit(old_val, new_val, HPET_TN_TYPE_LEVEL)) {
|
|
|
|
/*
|
|
|
|
* Do this before changing timer->config; otherwise, if
|
|
|
|
* HPET_TN_FSB is set, update_irq will not lower the qemu_irq.
|
|
|
|
*/
|
2010-06-13 16:15:45 +04:00
|
|
|
update_irq(timer, 0);
|
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
timer->config = new_val;
|
2024-07-16 12:36:44 +03:00
|
|
|
if (activating_bit(old_val, new_val, HPET_TN_ENABLE)
|
|
|
|
&& (s->isr & (1 << timer_id))) {
|
|
|
|
update_irq(timer, 1);
|
|
|
|
}
|
2010-06-13 16:15:35 +04:00
|
|
|
if (new_val & HPET_TN_32BIT) {
|
|
|
|
timer->cmp = (uint32_t)timer->cmp;
|
|
|
|
timer->period = (uint32_t)timer->period;
|
|
|
|
}
|
2024-07-16 12:36:44 +03:00
|
|
|
if (hpet_enabled(s)) {
|
2010-06-13 16:15:39 +04:00
|
|
|
hpet_set_timer(timer);
|
|
|
|
}
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
|
|
|
case HPET_TN_CMP: // comparator register
|
|
|
|
if (timer->config & HPET_TN_32BIT) {
|
2024-07-10 10:58:01 +03:00
|
|
|
/* High 32-bits are zero, leave them untouched. */
|
|
|
|
if (shift) {
|
|
|
|
trace_hpet_ram_write_invalid_tn_cmp();
|
|
|
|
break;
|
2024-07-10 12:56:35 +03:00
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
len = 64;
|
|
|
|
value = (uint32_t) value;
|
2010-06-13 16:15:35 +04:00
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
trace_hpet_ram_write_tn_cmp(addr & 4);
|
2010-06-13 16:15:35 +04:00
|
|
|
if (!timer_is_periodic(timer)
|
|
|
|
|| (timer->config & HPET_TN_SETVAL)) {
|
2024-07-10 10:58:01 +03:00
|
|
|
timer->cmp = deposit64(timer->cmp, shift, len, value);
|
2024-07-10 12:27:28 +03:00
|
|
|
}
|
|
|
|
if (timer_is_periodic(timer)) {
|
2024-07-16 13:26:36 +03:00
|
|
|
timer->period = deposit64(timer->period, shift, len, value);
|
2024-07-10 12:27:28 +03:00
|
|
|
}
|
|
|
|
timer->config &= ~HPET_TN_SETVAL;
|
|
|
|
if (hpet_enabled(s)) {
|
|
|
|
hpet_set_timer(timer);
|
|
|
|
}
|
|
|
|
break;
|
2010-06-13 16:15:45 +04:00
|
|
|
case HPET_TN_ROUTE:
|
2024-07-10 10:58:01 +03:00
|
|
|
timer->fsb = deposit64(timer->fsb, shift, len, value);
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
|
|
|
default:
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_write_invalid();
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
2024-07-10 10:58:01 +03:00
|
|
|
switch (addr & ~4) {
|
2010-06-13 16:15:35 +04:00
|
|
|
case HPET_ID:
|
|
|
|
return;
|
|
|
|
case HPET_CFG:
|
2024-07-10 10:58:01 +03:00
|
|
|
old_val = s->config;
|
|
|
|
new_val = deposit64(old_val, shift, len, value);
|
2024-07-10 11:55:13 +03:00
|
|
|
new_val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
|
2024-07-10 10:58:01 +03:00
|
|
|
s->config = new_val;
|
2010-06-13 16:15:35 +04:00
|
|
|
if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
|
|
|
|
/* Enable main counter and interrupt generation. */
|
|
|
|
s->hpet_offset =
|
2013-08-21 19:03:08 +04:00
|
|
|
ticks_to_ns(s->hpet_counter) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2010-06-13 16:15:44 +04:00
|
|
|
for (i = 0; i < s->num_timers; i++) {
|
2024-07-16 12:36:44 +03:00
|
|
|
if (timer_enabled(&s->timer[i]) && (s->isr & (1 << i))) {
|
|
|
|
update_irq(&s->timer[i], 1);
|
2010-06-13 16:15:35 +04:00
|
|
|
}
|
2024-07-16 12:36:44 +03:00
|
|
|
hpet_set_timer(&s->timer[i]);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:35 +04:00
|
|
|
} else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
|
|
|
|
/* Halt main counter and disable interrupt generation. */
|
2010-06-13 16:15:41 +04:00
|
|
|
s->hpet_counter = hpet_get_ticks(s);
|
2010-06-13 16:15:44 +04:00
|
|
|
for (i = 0; i < s->num_timers; i++) {
|
2010-06-13 16:15:35 +04:00
|
|
|
hpet_del_timer(&s->timer[i]);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:35 +04:00
|
|
|
}
|
2012-02-01 23:31:41 +04:00
|
|
|
/* i8254 and RTC output pins are disabled
|
|
|
|
* when HPET is in legacy mode */
|
2010-06-13 16:15:35 +04:00
|
|
|
if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
|
2012-02-01 23:31:41 +04:00
|
|
|
qemu_set_irq(s->pit_enabled, 0);
|
|
|
|
qemu_irq_lower(s->irqs[0]);
|
2010-06-13 16:15:40 +04:00
|
|
|
qemu_irq_lower(s->irqs[RTC_ISA_IRQ]);
|
2010-06-13 16:15:35 +04:00
|
|
|
} else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
|
2012-02-01 23:31:41 +04:00
|
|
|
qemu_irq_lower(s->irqs[0]);
|
|
|
|
qemu_set_irq(s->pit_enabled, 1);
|
2010-06-13 16:15:40 +04:00
|
|
|
qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level);
|
2010-06-13 16:15:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HPET_STATUS:
|
2024-07-10 10:58:01 +03:00
|
|
|
new_val = value << shift;
|
2024-07-10 11:55:13 +03:00
|
|
|
cleared = new_val & s->isr;
|
2010-06-13 16:15:44 +04:00
|
|
|
for (i = 0; i < s->num_timers; i++) {
|
2024-07-10 11:55:13 +03:00
|
|
|
if (cleared & (1 << i)) {
|
2010-06-13 16:15:42 +04:00
|
|
|
update_irq(&s->timer[i], 0);
|
|
|
|
}
|
|
|
|
}
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
|
|
|
case HPET_COUNTER:
|
2010-06-13 16:15:41 +04:00
|
|
|
if (hpet_enabled(s)) {
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_write_counter_write_while_enabled();
|
2010-06-13 16:15:35 +04:00
|
|
|
}
|
2024-07-10 10:58:01 +03:00
|
|
|
s->hpet_counter = deposit64(s->hpet_counter, shift, len, value);
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
|
|
|
default:
|
2023-11-19 02:11:29 +03:00
|
|
|
trace_hpet_ram_write_invalid();
|
2010-06-13 16:15:35 +04:00
|
|
|
break;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 18:10:07 +04:00
|
|
|
static const MemoryRegionOps hpet_ram_ops = {
|
|
|
|
.read = hpet_ram_read,
|
|
|
|
.write = hpet_ram_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
2024-07-10 10:58:01 +03:00
|
|
|
.max_access_size = 8,
|
|
|
|
},
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 8,
|
2011-11-09 18:10:07 +04:00
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2008-12-18 02:28:44 +03:00
|
|
|
};
|
|
|
|
|
2010-06-13 16:15:38 +04:00
|
|
|
static void hpet_reset(DeviceState *d)
|
2010-06-13 16:15:35 +04:00
|
|
|
{
|
2013-07-01 14:18:36 +04:00
|
|
|
HPETState *s = HPET(d);
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(d);
|
2008-12-18 02:28:44 +03:00
|
|
|
int i;
|
|
|
|
|
2010-06-13 16:15:44 +04:00
|
|
|
for (i = 0; i < s->num_timers; i++) {
|
2008-12-18 02:28:44 +03:00
|
|
|
HPETTimer *timer = &s->timer[i];
|
2010-06-13 16:15:35 +04:00
|
|
|
|
2008-12-18 02:28:44 +03:00
|
|
|
hpet_del_timer(timer);
|
|
|
|
timer->cmp = ~0ULL;
|
2010-06-13 16:15:45 +04:00
|
|
|
timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
|
|
|
|
if (s->flags & (1 << HPET_MSI_SUPPORT)) {
|
|
|
|
timer->config |= HPET_TN_FSB_CAP;
|
|
|
|
}
|
2013-12-08 13:38:17 +04:00
|
|
|
/* advertise availability of ioapic int */
|
|
|
|
timer->config |= (uint64_t)s->intcap << 32;
|
2008-12-18 02:28:44 +03:00
|
|
|
timer->period = 0ULL;
|
|
|
|
timer->wrap_flag = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 23:31:41 +04:00
|
|
|
qemu_set_irq(s->pit_enabled, 1);
|
2008-12-18 02:28:44 +03:00
|
|
|
s->hpet_counter = 0ULL;
|
|
|
|
s->hpet_offset = 0ULL;
|
2009-07-14 03:43:13 +04:00
|
|
|
s->config = 0ULL;
|
2010-06-14 12:29:28 +04:00
|
|
|
hpet_cfg.hpet[s->hpet_id].event_timer_block_id = (uint32_t)s->capability;
|
2013-07-01 14:18:36 +04:00
|
|
|
hpet_cfg.hpet[s->hpet_id].address = sbd->mmio[0].addr;
|
2012-02-01 23:31:38 +04:00
|
|
|
|
|
|
|
/* to document that the RTC lowers its output on reset as well */
|
|
|
|
s->rtc_irq_level = 0;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
|
|
|
|
2012-02-01 23:31:41 +04:00
|
|
|
static void hpet_handle_legacy_irq(void *opaque, int n, int level)
|
2010-06-13 16:15:40 +04:00
|
|
|
{
|
2013-07-01 14:18:36 +04:00
|
|
|
HPETState *s = HPET(opaque);
|
2010-06-13 16:15:40 +04:00
|
|
|
|
2012-02-01 23:31:41 +04:00
|
|
|
if (n == HPET_LEGACY_PIT_INT) {
|
|
|
|
if (!hpet_in_legacy_mode(s)) {
|
|
|
|
qemu_set_irq(s->irqs[0], level);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->rtc_irq_level = level;
|
|
|
|
if (!hpet_in_legacy_mode(s)) {
|
|
|
|
qemu_set_irq(s->irqs[RTC_ISA_IRQ], level);
|
|
|
|
}
|
2010-06-13 16:15:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:18:37 +04:00
|
|
|
static void hpet_init(Object *obj)
|
2010-06-13 16:15:35 +04:00
|
|
|
{
|
2013-07-01 14:18:37 +04:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
|
|
|
HPETState *s = HPET(obj);
|
|
|
|
|
|
|
|
/* HPET Area */
|
2015-12-28 20:02:29 +03:00
|
|
|
memory_region_init_io(&s->iomem, obj, &hpet_ram_ops, s, "hpet", HPET_LEN);
|
2013-07-01 14:18:37 +04:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hpet_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
2013-07-01 14:18:36 +04:00
|
|
|
HPETState *s = HPET(dev);
|
2011-11-09 18:10:07 +04:00
|
|
|
int i;
|
2010-06-13 16:15:35 +04:00
|
|
|
HPETTimer *timer;
|
2008-12-18 02:28:44 +03:00
|
|
|
|
2013-12-08 13:38:17 +04:00
|
|
|
if (!s->intcap) {
|
2019-04-17 22:06:32 +03:00
|
|
|
warn_report("Hpet's intcap not initialized");
|
2013-12-08 13:38:17 +04:00
|
|
|
}
|
2010-06-16 01:03:28 +04:00
|
|
|
if (hpet_cfg.count == UINT8_MAX) {
|
|
|
|
/* first instance */
|
2010-06-14 12:29:28 +04:00
|
|
|
hpet_cfg.count = 0;
|
2010-06-16 01:03:28 +04:00
|
|
|
}
|
2010-06-14 12:29:28 +04:00
|
|
|
|
|
|
|
if (hpet_cfg.count == 8) {
|
2013-07-01 14:18:37 +04:00
|
|
|
error_setg(errp, "Only 8 instances of HPET is allowed");
|
|
|
|
return;
|
2010-06-14 12:29:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
s->hpet_id = hpet_cfg.count++;
|
|
|
|
|
2010-06-13 16:15:38 +04:00
|
|
|
for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) {
|
2013-07-01 14:18:37 +04:00
|
|
|
sysbus_init_irq(sbd, &s->irqs[i]);
|
2010-06-13 16:15:38 +04:00
|
|
|
}
|
2010-06-13 16:15:44 +04:00
|
|
|
|
|
|
|
if (s->num_timers < HPET_MIN_TIMERS) {
|
|
|
|
s->num_timers = HPET_MIN_TIMERS;
|
|
|
|
} else if (s->num_timers > HPET_MAX_TIMERS) {
|
|
|
|
s->num_timers = HPET_MAX_TIMERS;
|
|
|
|
}
|
|
|
|
for (i = 0; i < HPET_MAX_TIMERS; i++) {
|
2010-06-13 16:15:35 +04:00
|
|
|
timer = &s->timer[i];
|
2013-08-21 19:03:08 +04:00
|
|
|
timer->qemu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hpet_timer, timer);
|
2010-06-13 16:15:37 +04:00
|
|
|
timer->tn = i;
|
|
|
|
timer->state = s;
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:38 +04:00
|
|
|
|
2010-06-14 10:40:29 +04:00
|
|
|
/* 64-bit main counter; LegacyReplacementRoute. */
|
|
|
|
s->capability = 0x8086a001ULL;
|
|
|
|
s->capability |= (s->num_timers - 1) << HPET_ID_NUM_TIM_SHIFT;
|
2015-08-25 18:13:01 +03:00
|
|
|
s->capability |= ((uint64_t)(HPET_CLK_PERIOD * FS_PER_NS) << 32);
|
2010-06-14 10:40:29 +04:00
|
|
|
|
2013-07-01 14:18:37 +04:00
|
|
|
qdev_init_gpio_in(dev, hpet_handle_legacy_irq, 2);
|
|
|
|
qdev_init_gpio_out(dev, &s->pit_enabled, 1);
|
2008-12-18 02:28:44 +03:00
|
|
|
}
|
2010-06-13 16:15:38 +04:00
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property hpet_device_properties[] = {
|
|
|
|
DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
|
|
|
|
DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false),
|
2013-12-08 13:38:17 +04:00
|
|
|
DEFINE_PROP_UINT32(HPET_INTCAP, HPETState, intcap, 0),
|
2018-01-11 11:24:58 +03:00
|
|
|
DEFINE_PROP_BOOL("hpet-offset-saved", HPETState, hpet_offset_saved, true),
|
2012-01-24 23:12:29 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void hpet_device_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-07-01 14:18:37 +04:00
|
|
|
dc->realize = hpet_realize;
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, hpet_reset);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_hpet;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, hpet_device_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo hpet_device_info = {
|
2013-07-01 14:18:36 +04:00
|
|
|
.name = TYPE_HPET,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(HPETState),
|
2013-07-01 14:18:37 +04:00
|
|
|
.instance_init = hpet_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = hpet_device_class_init,
|
2010-06-13 16:15:38 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void hpet_register_types(void)
|
2010-06-13 16:15:38 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&hpet_device_info);
|
2010-06-13 16:15:38 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(hpet_register_types)
|