2008-12-17 23:28:44 +00:00
|
|
|
/*
|
|
|
|
* High Precisition Event Timer emulation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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-16 20:47:01 +00:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2008-12-17 23:28:44 +00:00
|
|
|
*
|
|
|
|
* *****************************************************************
|
|
|
|
*
|
|
|
|
* This driver attempts to emulate an HPET device in software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hw.h"
|
2008-12-18 22:42:34 +00:00
|
|
|
#include "pc.h"
|
2008-12-17 23:28:44 +00:00
|
|
|
#include "console.h"
|
|
|
|
#include "qemu-timer.h"
|
|
|
|
#include "hpet_emul.h"
|
2010-06-13 12:15:38 +00:00
|
|
|
#include "sysbus.h"
|
2010-06-13 12:15:40 +00:00
|
|
|
#include "mc146818rtc.h"
|
2008-12-17 23:28:44 +00:00
|
|
|
|
|
|
|
//#define HPET_DEBUG
|
|
|
|
#ifdef HPET_DEBUG
|
2010-02-06 23:03:50 +00:00
|
|
|
#define DPRINTF printf
|
2008-12-17 23:28:44 +00:00
|
|
|
#else
|
2010-02-06 23:03:50 +00:00
|
|
|
#define DPRINTF(...)
|
2008-12-17 23:28:44 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-13 12:15:35 +00: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 */
|
|
|
|
uint64_t fsb; /* FSB route, not supported now */
|
|
|
|
/* Hidden register state */
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
} HPETTimer;
|
|
|
|
|
|
|
|
typedef struct HPETState {
|
2010-06-13 12:15:38 +00:00
|
|
|
SysBusDevice busdev;
|
2010-06-13 12:15:35 +00:00
|
|
|
uint64_t hpet_offset;
|
2010-06-13 12:15:38 +00:00
|
|
|
qemu_irq irqs[HPET_NUM_IRQ_ROUTES];
|
2010-06-13 12:15:40 +00:00
|
|
|
uint8_t rtc_irq_level;
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETTimer timer[HPET_NUM_TIMERS];
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
} HPETState;
|
|
|
|
|
2010-06-13 12:15:40 +00:00
|
|
|
static uint32_t hpet_in_legacy_mode(HPETState *s)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2010-06-13 12:15:40 +00:00
|
|
|
return s->config & HPET_CFG_LEGACY;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
static uint32_t timer_int_route(struct HPETTimer *timer)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2010-06-13 12:15:35 +00:00
|
|
|
return (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2010-06-13 12:15:41 +00:00
|
|
|
static uint32_t hpet_enabled(HPETState *s)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2010-06-13 12:15:41 +00:00
|
|
|
return s->config & HPET_CFG_ENABLE;
|
2008-12-17 23:28:44 +00: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)
|
|
|
|
{
|
|
|
|
return ((int32_t)(b) - (int32_t)(a) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
return ((int64_t)(b) - (int64_t)(a) < 0);
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
static uint64_t ticks_to_ns(uint64_t value)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
|
|
|
return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
static uint64_t ns_to_ticks(uint64_t value)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
|
|
|
return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 22:42:43 +00:00
|
|
|
return (!(old & mask) && (new & mask));
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
|
|
|
|
{
|
2008-12-18 22:42:43 +00:00
|
|
|
return ((old & mask) && !(new & mask));
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2010-06-13 12:15:41 +00:00
|
|
|
static uint64_t hpet_get_ticks(HPETState *s)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2010-06-13 12:15:41 +00:00
|
|
|
return ns_to_ticks(qemu_get_clock(vm_clock) + s->hpet_offset);
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
/*
|
|
|
|
* calculate diff between comparator value and current ticks
|
2008-12-17 23:28:44 +00:00
|
|
|
*/
|
|
|
|
static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
|
|
|
|
{
|
2008-12-18 22:42:43 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
if (t->config & HPET_TN_32BIT) {
|
|
|
|
uint32_t diff, cmp;
|
2010-06-13 12:15:35 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
cmp = (uint32_t)t->cmp;
|
|
|
|
diff = cmp - (uint32_t)current;
|
|
|
|
diff = (int32_t)diff > 0 ? diff : (uint32_t)0;
|
|
|
|
return (uint64_t)diff;
|
|
|
|
} else {
|
|
|
|
uint64_t diff, cmp;
|
2010-06-13 12:15:35 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
cmp = t->cmp;
|
|
|
|
diff = cmp - current;
|
|
|
|
diff = (int64_t)diff > 0 ? diff : (uint64_t)0;
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_irq(struct HPETTimer *timer)
|
|
|
|
{
|
|
|
|
int route;
|
|
|
|
|
2010-06-13 12:15:40 +00:00
|
|
|
if (timer->tn <= 1 && hpet_in_legacy_mode(timer->state)) {
|
2008-12-17 23:28:44 +00: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-18 22:42:43 +00:00
|
|
|
* timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
|
2008-12-17 23:28:44 +00:00
|
|
|
*/
|
2010-06-13 12:15:40 +00:00
|
|
|
route = (timer->tn == 0) ? 0 : RTC_ISA_IRQ;
|
2008-12-17 23:28:44 +00:00
|
|
|
} else {
|
2010-06-13 12:15:35 +00:00
|
|
|
route = timer_int_route(timer);
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:41 +00:00
|
|
|
if (!timer_enabled(timer) || !hpet_enabled(timer->state)) {
|
2010-06-13 12:15:35 +00:00
|
|
|
return;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
qemu_irq_pulse(timer->state->irqs[route]);
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 20:48:22 +00:00
|
|
|
static void hpet_pre_save(void *opaque)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2009-09-29 20:48:22 +00:00
|
|
|
HPETState *s = opaque;
|
2010-06-13 12:15:35 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
/* save current counter value */
|
2010-06-13 12:15:41 +00:00
|
|
|
s->hpet_counter = hpet_get_ticks(s);
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 20:48:21 +00:00
|
|
|
static int hpet_post_load(void *opaque, int version_id)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
|
|
|
HPETState *s = opaque;
|
2008-12-18 22:42:43 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
/* Recalculate the offset between the main counter and guest time */
|
|
|
|
s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-10 01:04:45 +00:00
|
|
|
static const VMStateDescription vmstate_hpet_timer = {
|
|
|
|
.name = "hpet_timer",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
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),
|
|
|
|
VMSTATE_TIMER(qemu_timer, HPETTimer),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_hpet = {
|
|
|
|
.name = "hpet",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.pre_save = hpet_pre_save,
|
|
|
|
.post_load = hpet_post_load,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
VMSTATE_UINT64(config, HPETState),
|
|
|
|
VMSTATE_UINT64(isr, HPETState),
|
|
|
|
VMSTATE_UINT64(hpet_counter, HPETState),
|
|
|
|
VMSTATE_STRUCT_ARRAY(timer, HPETState, HPET_NUM_TIMERS, 0,
|
|
|
|
vmstate_hpet_timer, HPETTimer),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
/*
|
2008-12-17 23:28:44 +00:00
|
|
|
* timer expiration callback
|
|
|
|
*/
|
|
|
|
static void hpet_timer(void *opaque)
|
|
|
|
{
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETTimer *t = opaque;
|
2008-12-17 23:28:44 +00:00
|
|
|
uint64_t diff;
|
|
|
|
|
|
|
|
uint64_t period = t->period;
|
2010-06-13 12:15:41 +00:00
|
|
|
uint64_t cur_tick = hpet_get_ticks(t->state);
|
2008-12-17 23:28:44 +00:00
|
|
|
|
|
|
|
if (timer_is_periodic(t) && period != 0) {
|
|
|
|
if (t->config & HPET_TN_32BIT) {
|
2010-06-13 12:15:35 +00:00
|
|
|
while (hpet_time_after(cur_tick, t->cmp)) {
|
2008-12-17 23:28:44 +00:00
|
|
|
t->cmp = (uint32_t)(t->cmp + t->period);
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (hpet_time_after64(cur_tick, t->cmp)) {
|
2008-12-17 23:28:44 +00:00
|
|
|
t->cmp += period;
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-17 23:28:44 +00:00
|
|
|
diff = hpet_calculate_diff(t, cur_tick);
|
2010-06-13 12:15:35 +00:00
|
|
|
qemu_mod_timer(t->qemu_timer,
|
|
|
|
qemu_get_clock(vm_clock) + (int64_t)ticks_to_ns(diff));
|
2008-12-17 23:28:44 +00:00
|
|
|
} else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
|
|
|
|
if (t->wrap_flag) {
|
|
|
|
diff = hpet_calculate_diff(t, cur_tick);
|
2010-06-13 12:15:35 +00:00
|
|
|
qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock) +
|
|
|
|
(int64_t)ticks_to_ns(diff));
|
2008-12-17 23:28:44 +00:00
|
|
|
t->wrap_flag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_irq(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hpet_set_timer(HPETTimer *t)
|
|
|
|
{
|
|
|
|
uint64_t diff;
|
|
|
|
uint32_t wrap_diff; /* how many ticks until we wrap? */
|
2010-06-13 12:15:41 +00:00
|
|
|
uint64_t cur_tick = hpet_get_ticks(t->state);
|
2008-12-18 22:42:43 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
/* whenever new timer is being set up, make sure wrap_flag is 0 */
|
|
|
|
t->wrap_flag = 0;
|
|
|
|
diff = hpet_calculate_diff(t, cur_tick);
|
|
|
|
|
2008-12-18 22:42:43 +00:00
|
|
|
/* hpet spec says in one-shot 32-bit mode, generate an interrupt when
|
2008-12-17 23:28:44 +00:00
|
|
|
* counter wraps in addition to an interrupt with comparator match.
|
2008-12-18 22:42:43 +00:00
|
|
|
*/
|
2008-12-17 23:28:44 +00:00
|
|
|
if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
|
|
|
|
wrap_diff = 0xffffffff - (uint32_t)cur_tick;
|
|
|
|
if (wrap_diff < (uint32_t)diff) {
|
|
|
|
diff = wrap_diff;
|
2008-12-18 22:42:43 +00:00
|
|
|
t->wrap_flag = 1;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
qemu_mod_timer(t->qemu_timer,
|
|
|
|
qemu_get_clock(vm_clock) + (int64_t)ticks_to_ns(diff));
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hpet_del_timer(HPETTimer *t)
|
|
|
|
{
|
|
|
|
qemu_del_timer(t->qemu_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HPET_DEBUG
|
2009-10-01 21:12:16 +00:00
|
|
|
static uint32_t hpet_ram_readb(void *opaque, target_phys_addr_t addr)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
|
|
|
printf("qemu: hpet_read b at %" PRIx64 "\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static uint32_t hpet_ram_readw(void *opaque, target_phys_addr_t addr)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
|
|
|
printf("qemu: hpet_read w at %" PRIx64 "\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static uint32_t hpet_ram_readl(void *opaque, target_phys_addr_t addr)
|
2008-12-17 23:28:44 +00:00
|
|
|
{
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETState *s = opaque;
|
2008-12-17 23:28:44 +00:00
|
|
|
uint64_t cur_tick, index;
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
|
2008-12-17 23:28:44 +00:00
|
|
|
index = addr;
|
|
|
|
/*address range of all TN regs*/
|
|
|
|
if (index >= 0x100 && index <= 0x3ff) {
|
|
|
|
uint8_t timer_id = (addr - 0x100) / 0x20;
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETTimer *timer = &s->timer[timer_id];
|
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
if (timer_id > HPET_NUM_TIMERS - 1) {
|
2010-06-13 12:15:34 +00:00
|
|
|
DPRINTF("qemu: timer id out of range\n");
|
2008-12-17 23:28:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((addr - 0x100) % 0x20) {
|
2010-06-13 12:15:35 +00:00
|
|
|
case HPET_TN_CFG:
|
|
|
|
return timer->config;
|
|
|
|
case HPET_TN_CFG + 4: // Interrupt capabilities
|
|
|
|
return timer->config >> 32;
|
|
|
|
case HPET_TN_CMP: // comparator register
|
|
|
|
return timer->cmp;
|
|
|
|
case HPET_TN_CMP + 4:
|
|
|
|
return timer->cmp >> 32;
|
|
|
|
case HPET_TN_ROUTE:
|
|
|
|
return timer->fsb >> 32;
|
|
|
|
default:
|
|
|
|
DPRINTF("qemu: invalid hpet_ram_readl\n");
|
|
|
|
break;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (index) {
|
2010-06-13 12:15:35 +00:00
|
|
|
case HPET_ID:
|
|
|
|
return s->capability;
|
|
|
|
case HPET_PERIOD:
|
|
|
|
return s->capability >> 32;
|
|
|
|
case HPET_CFG:
|
|
|
|
return s->config;
|
|
|
|
case HPET_CFG + 4:
|
|
|
|
DPRINTF("qemu: invalid HPET_CFG + 4 hpet_ram_readl \n");
|
|
|
|
return 0;
|
|
|
|
case HPET_COUNTER:
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
|
|
|
cur_tick = hpet_get_ticks(s);
|
2010-06-13 12:15:35 +00:00
|
|
|
} else {
|
|
|
|
cur_tick = s->hpet_counter;
|
|
|
|
}
|
|
|
|
DPRINTF("qemu: reading counter = %" PRIx64 "\n", cur_tick);
|
|
|
|
return cur_tick;
|
|
|
|
case HPET_COUNTER + 4:
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
|
|
|
cur_tick = hpet_get_ticks(s);
|
2010-06-13 12:15:35 +00:00
|
|
|
} else {
|
|
|
|
cur_tick = s->hpet_counter;
|
|
|
|
}
|
|
|
|
DPRINTF("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick);
|
|
|
|
return cur_tick >> 32;
|
|
|
|
case HPET_STATUS:
|
|
|
|
return s->isr;
|
|
|
|
default:
|
|
|
|
DPRINTF("qemu: invalid hpet_ram_readl\n");
|
|
|
|
break;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HPET_DEBUG
|
2009-10-01 21:12:16 +00:00
|
|
|
static void hpet_ram_writeb(void *opaque, target_phys_addr_t addr,
|
2008-12-17 23:28:44 +00:00
|
|
|
uint32_t value)
|
|
|
|
{
|
2008-12-18 22:42:43 +00:00
|
|
|
printf("qemu: invalid hpet_write b at %" PRIx64 " = %#x\n",
|
2008-12-17 23:28:44 +00:00
|
|
|
addr, value);
|
|
|
|
}
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static void hpet_ram_writew(void *opaque, target_phys_addr_t addr,
|
2008-12-17 23:28:44 +00:00
|
|
|
uint32_t value)
|
|
|
|
{
|
2008-12-18 22:42:43 +00:00
|
|
|
printf("qemu: invalid hpet_write w at %" PRIx64 " = %#x\n",
|
2008-12-17 23:28:44 +00:00
|
|
|
addr, value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static void hpet_ram_writel(void *opaque, target_phys_addr_t addr,
|
2008-12-17 23:28:44 +00:00
|
|
|
uint32_t value)
|
|
|
|
{
|
|
|
|
int i;
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETState *s = opaque;
|
2009-07-24 16:26:59 +00:00
|
|
|
uint64_t old_val, new_val, val, index;
|
2008-12-17 23:28:44 +00:00
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value);
|
2008-12-17 23:28:44 +00:00
|
|
|
index = addr;
|
|
|
|
old_val = hpet_ram_readl(opaque, addr);
|
|
|
|
new_val = value;
|
|
|
|
|
|
|
|
/*address range of all TN regs*/
|
|
|
|
if (index >= 0x100 && index <= 0x3ff) {
|
|
|
|
uint8_t timer_id = (addr - 0x100) / 0x20;
|
|
|
|
HPETTimer *timer = &s->timer[timer_id];
|
2008-12-18 22:42:43 +00:00
|
|
|
|
2010-06-13 12:15:35 +00:00
|
|
|
DPRINTF("qemu: hpet_ram_writel timer_id = %#x \n", timer_id);
|
2010-06-13 12:15:34 +00:00
|
|
|
if (timer_id > HPET_NUM_TIMERS - 1) {
|
|
|
|
DPRINTF("qemu: timer id out of range\n");
|
|
|
|
return;
|
|
|
|
}
|
2008-12-17 23:28:44 +00:00
|
|
|
switch ((addr - 0x100) % 0x20) {
|
2010-06-13 12:15:35 +00:00
|
|
|
case HPET_TN_CFG:
|
|
|
|
DPRINTF("qemu: hpet_ram_writel HPET_TN_CFG\n");
|
|
|
|
val = hpet_fixup_reg(new_val, old_val, HPET_TN_CFG_WRITE_MASK);
|
|
|
|
timer->config = (timer->config & 0xffffffff00000000ULL) | val;
|
|
|
|
if (new_val & HPET_TN_32BIT) {
|
|
|
|
timer->cmp = (uint32_t)timer->cmp;
|
|
|
|
timer->period = (uint32_t)timer->period;
|
|
|
|
}
|
|
|
|
if (new_val & HPET_TN_TYPE_LEVEL) {
|
|
|
|
printf("qemu: level-triggered hpet not supported\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
2010-06-13 12:15:39 +00:00
|
|
|
if (activating_bit(old_val, new_val, HPET_TN_ENABLE)) {
|
|
|
|
hpet_set_timer(timer);
|
|
|
|
} else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) {
|
|
|
|
hpet_del_timer(timer);
|
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
break;
|
|
|
|
case HPET_TN_CFG + 4: // Interrupt capabilities
|
|
|
|
DPRINTF("qemu: invalid HPET_TN_CFG+4 write\n");
|
|
|
|
break;
|
|
|
|
case HPET_TN_CMP: // comparator register
|
|
|
|
DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP \n");
|
|
|
|
if (timer->config & HPET_TN_32BIT) {
|
|
|
|
new_val = (uint32_t)new_val;
|
|
|
|
}
|
|
|
|
if (!timer_is_periodic(timer)
|
|
|
|
|| (timer->config & HPET_TN_SETVAL)) {
|
|
|
|
timer->cmp = (timer->cmp & 0xffffffff00000000ULL) | new_val;
|
|
|
|
}
|
|
|
|
if (timer_is_periodic(timer)) {
|
|
|
|
/*
|
|
|
|
* FIXME: Clamp period to reasonable min value?
|
|
|
|
* Clamp period to reasonable max value
|
|
|
|
*/
|
|
|
|
new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
|
|
|
|
timer->period =
|
|
|
|
(timer->period & 0xffffffff00000000ULL) | new_val;
|
|
|
|
}
|
|
|
|
timer->config &= ~HPET_TN_SETVAL;
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
2010-06-13 12:15:35 +00:00
|
|
|
hpet_set_timer(timer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HPET_TN_CMP + 4: // comparator register high order
|
|
|
|
DPRINTF("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
|
|
|
|
if (!timer_is_periodic(timer)
|
|
|
|
|| (timer->config & HPET_TN_SETVAL)) {
|
|
|
|
timer->cmp = (timer->cmp & 0xffffffffULL) | new_val << 32;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* FIXME: Clamp period to reasonable min value?
|
|
|
|
* Clamp period to reasonable max value
|
|
|
|
*/
|
|
|
|
new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
|
|
|
|
timer->period =
|
|
|
|
(timer->period & 0xffffffffULL) | new_val << 32;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
timer->config &= ~HPET_TN_SETVAL;
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
2008-12-17 23:28:44 +00:00
|
|
|
hpet_set_timer(timer);
|
|
|
|
}
|
|
|
|
break;
|
2010-06-13 12:15:35 +00:00
|
|
|
case HPET_TN_ROUTE + 4:
|
|
|
|
DPRINTF("qemu: hpet_ram_writel HPET_TN_ROUTE + 4\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF("qemu: invalid hpet_ram_writel\n");
|
|
|
|
break;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
switch (index) {
|
2010-06-13 12:15:35 +00:00
|
|
|
case HPET_ID:
|
|
|
|
return;
|
|
|
|
case HPET_CFG:
|
|
|
|
val = hpet_fixup_reg(new_val, old_val, HPET_CFG_WRITE_MASK);
|
|
|
|
s->config = (s->config & 0xffffffff00000000ULL) | val;
|
|
|
|
if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
|
|
|
|
/* Enable main counter and interrupt generation. */
|
|
|
|
s->hpet_offset =
|
|
|
|
ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock);
|
|
|
|
for (i = 0; i < HPET_NUM_TIMERS; i++) {
|
|
|
|
if ((&s->timer[i])->cmp != ~0ULL) {
|
|
|
|
hpet_set_timer(&s->timer[i]);
|
|
|
|
}
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
} else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
|
|
|
|
/* Halt main counter and disable interrupt generation. */
|
2010-06-13 12:15:41 +00:00
|
|
|
s->hpet_counter = hpet_get_ticks(s);
|
2010-06-13 12:15:35 +00:00
|
|
|
for (i = 0; i < HPET_NUM_TIMERS; i++) {
|
|
|
|
hpet_del_timer(&s->timer[i]);
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
/* i8254 and RTC are disabled when HPET is in legacy mode */
|
|
|
|
if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
|
|
|
|
hpet_pit_disable();
|
2010-06-13 12:15:40 +00:00
|
|
|
qemu_irq_lower(s->irqs[RTC_ISA_IRQ]);
|
2010-06-13 12:15:35 +00:00
|
|
|
} else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
|
|
|
|
hpet_pit_enable();
|
2010-06-13 12:15:40 +00:00
|
|
|
qemu_set_irq(s->irqs[RTC_ISA_IRQ], s->rtc_irq_level);
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HPET_CFG + 4:
|
|
|
|
DPRINTF("qemu: invalid HPET_CFG+4 write \n");
|
|
|
|
break;
|
|
|
|
case HPET_STATUS:
|
|
|
|
/* FIXME: need to handle level-triggered interrupts */
|
|
|
|
break;
|
|
|
|
case HPET_COUNTER:
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
2010-06-13 12:15:36 +00:00
|
|
|
DPRINTF("qemu: Writing counter while HPET enabled!\n");
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
s->hpet_counter =
|
|
|
|
(s->hpet_counter & 0xffffffff00000000ULL) | value;
|
|
|
|
DPRINTF("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n",
|
|
|
|
value, s->hpet_counter);
|
|
|
|
break;
|
|
|
|
case HPET_COUNTER + 4:
|
2010-06-13 12:15:41 +00:00
|
|
|
if (hpet_enabled(s)) {
|
2010-06-13 12:15:36 +00:00
|
|
|
DPRINTF("qemu: Writing counter while HPET enabled!\n");
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
|
|
|
s->hpet_counter =
|
|
|
|
(s->hpet_counter & 0xffffffffULL) | (((uint64_t)value) << 32);
|
|
|
|
DPRINTF("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n",
|
|
|
|
value, s->hpet_counter);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF("qemu: invalid hpet_ram_writel\n");
|
|
|
|
break;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:29:31 +00:00
|
|
|
static CPUReadMemoryFunc * const hpet_ram_read[] = {
|
2008-12-17 23:28:44 +00:00
|
|
|
#ifdef HPET_DEBUG
|
|
|
|
hpet_ram_readb,
|
|
|
|
hpet_ram_readw,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
hpet_ram_readl,
|
|
|
|
};
|
|
|
|
|
2009-08-25 18:29:31 +00:00
|
|
|
static CPUWriteMemoryFunc * const hpet_ram_write[] = {
|
2008-12-17 23:28:44 +00:00
|
|
|
#ifdef HPET_DEBUG
|
|
|
|
hpet_ram_writeb,
|
|
|
|
hpet_ram_writew,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
hpet_ram_writel,
|
|
|
|
};
|
|
|
|
|
2010-06-13 12:15:38 +00:00
|
|
|
static void hpet_reset(DeviceState *d)
|
2010-06-13 12:15:35 +00:00
|
|
|
{
|
2010-06-13 12:15:38 +00:00
|
|
|
HPETState *s = FROM_SYSBUS(HPETState, sysbus_from_qdev(d));
|
2008-12-17 23:28:44 +00:00
|
|
|
int i;
|
|
|
|
static int count = 0;
|
|
|
|
|
2010-06-13 12:15:35 +00:00
|
|
|
for (i = 0; i < HPET_NUM_TIMERS; i++) {
|
2008-12-17 23:28:44 +00:00
|
|
|
HPETTimer *timer = &s->timer[i];
|
2010-06-13 12:15:35 +00:00
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
hpet_del_timer(timer);
|
|
|
|
timer->cmp = ~0ULL;
|
|
|
|
timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
|
2009-07-24 16:26:59 +00:00
|
|
|
/* advertise availability of ioapic inti2 */
|
|
|
|
timer->config |= 0x00000004ULL << 32;
|
2008-12-17 23:28:44 +00:00
|
|
|
timer->period = 0ULL;
|
|
|
|
timer->wrap_flag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->hpet_counter = 0ULL;
|
|
|
|
s->hpet_offset = 0ULL;
|
|
|
|
/* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
|
|
|
|
s->capability = 0x8086a201ULL;
|
|
|
|
s->capability |= ((HPET_CLK_PERIOD) << 32);
|
2009-07-13 23:43:13 +00:00
|
|
|
s->config = 0ULL;
|
2010-06-13 12:15:35 +00:00
|
|
|
if (count > 0) {
|
2008-12-18 22:42:43 +00:00
|
|
|
/* we don't enable pit when hpet_reset is first called (by hpet_init)
|
2008-12-17 23:28:44 +00:00
|
|
|
* because hpet is taking over for pit here. On subsequent invocations,
|
|
|
|
* hpet_reset is called due to system reset. At this point control must
|
2008-12-18 22:42:43 +00:00
|
|
|
* be returned to pit until SW reenables hpet.
|
2008-12-17 23:28:44 +00:00
|
|
|
*/
|
|
|
|
hpet_pit_enable();
|
2010-06-13 12:15:35 +00:00
|
|
|
}
|
2008-12-17 23:28:44 +00:00
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
|
2010-06-13 12:15:40 +00:00
|
|
|
static void hpet_handle_rtc_irq(void *opaque, int n, int level)
|
|
|
|
{
|
|
|
|
HPETState *s = FROM_SYSBUS(HPETState, opaque);
|
|
|
|
|
|
|
|
s->rtc_irq_level = level;
|
|
|
|
if (!hpet_in_legacy_mode(s)) {
|
|
|
|
qemu_set_irq(s->irqs[RTC_ISA_IRQ], level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 12:15:38 +00:00
|
|
|
static int hpet_init(SysBusDevice *dev)
|
2010-06-13 12:15:35 +00:00
|
|
|
{
|
2010-06-13 12:15:38 +00:00
|
|
|
HPETState *s = FROM_SYSBUS(HPETState, dev);
|
2008-12-17 23:28:44 +00:00
|
|
|
int i, iomemtype;
|
2010-06-13 12:15:35 +00:00
|
|
|
HPETTimer *timer;
|
2008-12-17 23:28:44 +00:00
|
|
|
|
2010-06-13 12:15:38 +00:00
|
|
|
for (i = 0; i < HPET_NUM_IRQ_ROUTES; i++) {
|
|
|
|
sysbus_init_irq(dev, &s->irqs[i]);
|
|
|
|
}
|
2010-06-13 12:15:35 +00:00
|
|
|
for (i = 0; i < HPET_NUM_TIMERS; i++) {
|
|
|
|
timer = &s->timer[i];
|
2008-12-17 23:28:44 +00:00
|
|
|
timer->qemu_timer = qemu_new_timer(vm_clock, hpet_timer, timer);
|
2010-06-13 12:15:37 +00:00
|
|
|
timer->tn = i;
|
|
|
|
timer->state = s;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:38 +00:00
|
|
|
|
2010-06-13 12:15:40 +00:00
|
|
|
isa_reserve_irq(RTC_ISA_IRQ);
|
|
|
|
qdev_init_gpio_in(&dev->qdev, hpet_handle_rtc_irq, 1);
|
|
|
|
|
2008-12-17 23:28:44 +00:00
|
|
|
/* HPET Area */
|
2009-06-14 08:38:51 +00:00
|
|
|
iomemtype = cpu_register_io_memory(hpet_ram_read,
|
2008-12-17 23:28:44 +00:00
|
|
|
hpet_ram_write, s);
|
2010-06-13 12:15:38 +00:00
|
|
|
sysbus_init_mmio(dev, 0x400, iomemtype);
|
|
|
|
return 0;
|
2008-12-17 23:28:44 +00:00
|
|
|
}
|
2010-06-13 12:15:38 +00:00
|
|
|
|
|
|
|
static SysBusDeviceInfo hpet_device_info = {
|
|
|
|
.qdev.name = "hpet",
|
|
|
|
.qdev.size = sizeof(HPETState),
|
|
|
|
.qdev.no_user = 1,
|
|
|
|
.qdev.vmsd = &vmstate_hpet,
|
|
|
|
.qdev.reset = hpet_reset,
|
|
|
|
.init = hpet_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void hpet_register_device(void)
|
|
|
|
{
|
|
|
|
sysbus_register_withprop(&hpet_device_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
device_init(hpet_register_device)
|