DevHPET.cpp revision 6826c1a65f586b47c2abbbabab801950c9a0bb75
/* $Id$ */
/** @file
* HPET virtual device - high precision event timer emulation
*/
/*
* Copyright (C) 2009-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_HPET
#include <iprt/asm-math.h>
#include "VBoxDD.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/*
* Current limitations:
* - not entirely correct time of interrupt, i.e. never
* schedule interrupt earlier than in 1ms
* - statistics not implemented
* - level-triggered mode not implemented
*/
/** Base address for MMIO. */
#define HPET_BASE 0xfed00000
/** The number of timers for PIIX4 / PIIX3. */
#define HPET_NUM_TIMERS_PIIX 3
/** The number of timers for ICH9. */
#define HPET_NUM_TIMERS_ICH9 4
/** HPET clock period for PIIX4 / PIIX3.
* 10000000 femtoseconds == 10ns.
*/
/** HPET clock period for ICH9.
* 69841279 femtoseconds == 69.84 ns (1 / 14.31818MHz).
*/
/*
* Femptosecods in nanosecond
*/
#define FS_PER_NS 1000000
/*
* Interrupt type
*/
#define HPET_TIMER_TYPE_LEVEL 1
#define HPET_TIMER_TYPE_EDGE 0
/* Delivery mode */
/* Via APIC */
#define HPET_TIMER_DELIVERY_APIC 0
/* Via FSB */
#define HPET_TIMER_DELIVERY_FSB 1
#define HPET_ID 0x000
#define HPET_PERIOD 0x004
#define HPET_CFG 0x010
#define HPET_STATUS 0x020
#define HPET_COUNTER 0x0f0
#define HPET_TN_CFG 0x000
#define HPET_TN_CMP 0x008
#define HPET_TN_ROUTE 0x010
#define HPET_CFG_WRITE_MASK 0x3
#define HPET_TN_INT_ROUTE_SHIFT 9
#define HPET_TN_INT_ROUTE_CAP_SHIFT 32
#define HPET_TN_CFG_BITS_READONLY_OR_RESERVED 0xffff80b1U
/** Extract the timer count from the capabilities.
* @todo Check if the mask is correct. */
/** The version of the saved state. */
#define HPET_SAVED_STATE_VERSION 2
/** Empty saved state */
#define HPET_SAVED_STATE_VERSION_EMPTY 1
/**
* Acquires the HPET lock or returns.
*/
do { \
if (rcLock != VINF_SUCCESS) \
return rcLock; \
} while (0)
/**
* Releases the HPET lock.
*/
#define DEVHPET_UNLOCK(a_pThis) \
/**
* Acquires the TM lock and HPET lock, returns on failure.
*/
do { \
if (rcLock != VINF_SUCCESS) \
return rcLock; \
if (rcLock != VINF_SUCCESS) \
{ \
return rcLock; \
} \
} while (0)
/**
* Releases the HPET lock and TM lock.
*/
#define DEVHPET_UNLOCK_BOTH(a_pThis) \
do { \
} while (0)
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
struct HpetState;
typedef struct HpetTimer
{
/** The HPET timer - R3 Ptr. */
/** Pointer to the instance data - R3 Ptr. */
/** The HPET timer - R0 Ptr. */
/** Pointer to the instance data - R0 Ptr. */
/** The HPET timer - RC Ptr. */
/** Pointer to the instance data - RC Ptr. */
/** Timer index. */
/** Wrap. */
/** Alignment. */
/** @name Memory-mapped, software visible timer registers.
* @{ */
/** Configuration/capabilities. */
/** Comparator. */
/** FSB route, not supported now. */
/** @} */
/** @name Hidden register state.
* @{ */
/** Last value written to comparator. */
/** @} */
} HpetTimer;
typedef struct HpetState
{
/** Pointer to the device instance. - R3 ptr. */
/** The HPET helpers - R3 Ptr. */
/** Pointer to the device instance. - R0 ptr. */
/** The HPET helpers - R0 Ptr. */
/** Pointer to the device instance. - RC ptr. */
/** The HPET helpers - RC Ptr. */
/** Timer structures. */
/** Offset realtive to the virtual sync clock. */
/** @name Memory-mapped, software visible registers
* @{ */
/** Capabilities. */
/** HPET_PERIOD - . */
/** Configuration. */
/** Interrupt status register. */
/** Main counter. */
/** @} */
/** Global device lock. */
/** If we emulate ICH9 HPET (different frequency & timer count). */
bool fIch9;
} HpetState;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
{
}
{
}
{
}
{
}
{
}
{
}
{
/*
* We can use any timer to get current time, they all go
* with the same speed.
*/
return nsToHpetTicks(pThis,
+ pThis->u64HpetOffset);
}
{
u64NewValue &= u64Mask;
return u64NewValue;
}
{
return !(u64OldValue & u64Mask)
&& !!(u64NewValue & u64Mask);
}
{
return !!(u64OldValue & u64Mask)
&& !(u64NewValue & u64Mask);
}
{
if (hpet32bitTimer(pHpetTimer))
{
}
else
{
return u64Diff;
}
}
{
&& u64Period != 0)
{
/* While loop is suboptimal */
if (hpet32bitTimer(pHpetTimer))
{
}
else
{
}
}
}
/**
* Sets the frequency hint if it's a periodic timer.
*
* @param pThis The HPET state.
* @param pHpetTimer The timer.
*/
{
{
}
}
{
/* no wrapping on new timers */
pHpetTimer->u8Wrap = 0;
/*
* HPET spec says in one-shot 32-bit mode, generate an interrupt when
* counter wraps in addition to an interrupt with comparator match.
*/
if ( hpet32bitTimer(pHpetTimer)
{
{
Log(("wrap on timer %d: till=%u ticks=%lld diff64=%lld\n",
}
}
/*
* HACK ALERT! Avoid killing VM with interrupts.
*/
#if 1 /** @todo: HACK, rethink, may have negative impact on the guest */
if (u64Diff == 0)
#endif
Log4(("HPET: next IRQ in %lld ticks (%lld ns)\n", u64Diff, hpetTicksToNs(pHpetTimer->CTX_SUFF(pHpet), u64Diff)));
}
/* -=-=-=-=-=- Timer register accesses -=-=-=-=-=- */
/**
* Reads a HPET timer register.
*
* @returns VBox strict status code.
* @param pThis The HPET instance.
* @param iTimerNo The timer index.
* @param iTimerReg The index of the timer register to read.
* @param pu32Value Where to return the register value.
*
* @remarks ASSUMES the caller does holds the HPET lock.
*/
static int hpetTimerRegRead32(HpetState const *pThis, uint32_t iTimerNo, uint32_t iTimerReg, uint32_t *pu32Value)
{
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
*pu32Value = 0;
return VINF_SUCCESS;
}
switch (iTimerReg)
{
case HPET_TN_CFG:
break;
case HPET_TN_CFG + 4:
break;
case HPET_TN_CMP:
break;
case HPET_TN_CMP + 4:
Log(("read HPET_TN_CMP+4 on %d: %#x (%#llx)\n", pHpetTimer->idxTimer, u32Value, pHpetTimer->u64Cmp));
break;
case HPET_TN_ROUTE:
u32Value = (uint32_t)(pHpetTimer->u64Fsb >> 32); /** @todo Looks wrong, but since it's not supported, who cares. */
break;
default:
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
u32Value = 0;
break;
}
}
return VINF_SUCCESS;
}
/**
* 32-bit write to a HPET timer register.
*
* @returns Strict VBox status code.
*
* @param pThis The HPET state.
* @param idxReg The register being written to.
* @param u32NewValue The value being written.
*
* @remarks The caller should not hold the device lock, unless it also holds
* the TM lock.
*/
static int hpetTimerRegWrite32(HpetState *pThis, uint32_t iTimerNo, uint32_t iTimerReg, uint32_t u32NewValue)
{
Assert(!PDMCritSectIsOwner(&pThis->csLock) || TMTimerIsLockOwner(pThis->aTimers[0].CTX_SUFF(pTimer)));
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
return VINF_SUCCESS;
}
switch (iTimerReg)
{
case HPET_TN_CFG:
{
u64Mask |= HPET_TN_32BIT;
else
u32NewValue &= ~HPET_TN_32BIT;
if (u32NewValue & HPET_TN_32BIT)
{
}
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
LogRel(("level-triggered config not yet supported\n"));
AssertFailed();
}
/* We only care about lower 32-bits so far */
break;
}
{
Log(("write HPET_TN_CFG + 4, useless\n"));
break;
}
case HPET_TN_CMP: /* lower bits of comparator register */
{
{
u32NewValue &= hpetInvalidValue(pHpetTimer) >> 1; /** @todo check this in the docs and add a not why? */
}
break;
}
{
if (!hpet32bitTimer(pHpetTimer))
{
Log2(("after HPET_TN_CMP+4 cmp=%llx per=%llx tmr=%d\n", pHpetTimer->u64Cmp, pHpetTimer->u64Period, iTimerNo));
}
break;
}
case HPET_TN_ROUTE:
{
Log(("write HPET_TN_ROUTE\n"));
break;
}
case HPET_TN_ROUTE + 4:
{
Log(("write HPET_TN_ROUTE + 4\n"));
break;
}
default:
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
break;
}
}
return VINF_SUCCESS;
}
/* -=-=-=-=-=- Non-timer register accesses -=-=-=-=-=- */
/**
* Read a 32-bit HPET register.
*
* @returns Strict VBox status code.
* @param pThis The HPET state.
* @param idxReg The register to read.
* @param pu32Value Where to return the register value.
*
* @remarks The caller must not own the device lock if HPET_COUNTER is read.
*/
{
Assert(!PDMCritSectIsOwner(&pThis->csLock) || (idxReg != HPET_COUNTER && idxReg != HPET_COUNTER + 4));
switch (idxReg)
{
case HPET_ID:
break;
case HPET_PERIOD:
break;
case HPET_CFG:
break;
case HPET_CFG + 4:
break;
case HPET_COUNTER:
case HPET_COUNTER + 4:
{
else
/** @todo is it correct? */
Log(("read HPET_COUNTER: %s part value %x (%#llx)\n",
break;
}
case HPET_STATUS:
break;
default:
u32Value = 0;
break;
}
return VINF_SUCCESS;
}
/**
* 32-bit write to a config register.
*
* @returns Strict VBox status code.
*
* @param pThis The HPET state.
* @param idxReg The register being written to.
* @param u32NewValue The value being written.
*
* @remarks The caller should not hold the device lock, unless it also holds
* the TM lock.
*/
{
Assert(!PDMCritSectIsOwner(&pThis->csLock) || TMTimerIsLockOwner(pThis->aTimers[0].CTX_SUFF(pTimer)));
int rc = VINF_SUCCESS;
switch (idxReg)
{
case HPET_ID:
case HPET_ID + 4:
{
Log(("write HPET_ID, useless\n"));
break;
}
case HPET_CFG:
{
/*
* This check must be here, before actual update, as hpetLegacyMode
* may request retry in R3 - so we must keep state intact.
*/
{
#ifdef IN_RING3
if (rc != VINF_SUCCESS)
#else
#endif
{
break;
}
}
{
/** @todo Only get the time stamp once when reprogramming? */
/* Enable main counter and interrupt generation. */
}
{
/* Halt main counter and disable interrupt generation. */
}
break;
}
case HPET_CFG + 4:
{
UINT64_C(0xffffffff00000000));
break;
}
case HPET_STATUS:
{
/* Clear ISR for all set bits in u32NewValue, see p. 14 of the HPET spec. */
break;
}
case HPET_STATUS + 4:
{
if (u32NewValue != 0)
{
static unsigned s_cOccurrences = 0;
if (s_cOccurrences++ < 10)
LogRel(("Writing HPET_STATUS + 4 with non-zero, ignored\n"));
}
break;
}
case HPET_COUNTER:
{
break;
}
case HPET_COUNTER + 4:
{
break;
}
default:
{
static unsigned s_cOccurences = 0;
if (s_cOccurences++ < 10)
break;
}
}
return rc;
}
/* -=-=-=-=-=- MMIO callbacks -=-=-=-=-=- */
/**
* @callback_method_impl{FNIOMMMIOREAD}
*/
PDMBOTHCBDECL(int) hpetMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
int rc = VINF_SUCCESS;
switch (cb)
{
case 4:
{
}
else
break;
case 8:
{
/* Unaligned accesses not allowed */
{
break;
}
/* Split the access except for timing sensitive registers. The
others assume the protection of the lock. */
if (idxReg == HPET_COUNTER)
{
/* When reading HPET counter we must read it in a single read,
to avoid unexpected time jumps on 32-bit overflow. */
else
}
else
{
{
if (rc == VINF_SUCCESS)
}
else
{
/* for most 8-byte accesses we just split them, happens under lock anyway. */
if (rc == VINF_SUCCESS)
}
}
break;
}
case 1:
case 2:
rc = VINF_SUCCESS;
break;
default:
rc = VINF_SUCCESS;
}
return rc;
}
/**
* @callback_method_impl{FNIOMMMIOWRITE}
*/
PDMBOTHCBDECL(int) hpetMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
{
LogFlow(("hpetMMIOWrite: cb=%u reg=%03x (%RGp) val=%llx\n",
int rc;
switch (cb)
{
case 4:
else
break;
case 8:
{
/* Unaligned accesses are not allowed. */
{
break;
}
/* Split the access and rely on the locking to prevent trouble. */
{
/** @todo Consider handling iTimerReg == HPET_TN_CMP specially here */
}
else
{
}
break;
}
case 1:
case 2:
rc = VINF_SUCCESS;
break;
default:
break;
}
return rc;
}
#ifdef IN_RING3
/* -=-=-=-=-=- Timer Callback Processing -=-=-=-=-=- */
/**
* Gets the IRQ of an HPET timer.
*
* @returns IRQ number.
* @param pHpetTimer The HPET timer.
*/
{
/*
* Per spec, in legacy mode HPET timers wired as:
* timer 0: IRQ0 for PIC and IRQ2 for APIC
* timer 1: IRQ8 for both PIC and APIC
*
* ISA IRQ delivery logic will take care of correct delivery
* to the different ICs.
*/
}
/**
* Used by hpetTimerCb to update the IRQ status.
*
* @param pThis The HPET device state.
* @param pHpetTimer The HPET timer.
*/
{
/** @todo: is it correct? */
{
/* ISR bits are only set in level-triggered mode. */
level-triggered mode yet. */
else
AssertFailed();
/** @todo: implement IRQs in level-triggered mode */
}
}
/**
* Device timer callback function.
*
* @param pDevIns Device instance of the device which registered the timer.
* @param pTimer The timer handle.
* @param pvUser Pointer to the HPET timer state.
*/
{
{
}
else if ( hpet32bitTimer(pHpetTimer)
{
if (pHpetTimer->u8Wrap)
{
pHpetTimer->u8Wrap = 0;
}
}
/* Should it really be under lock, does it really matter? */
}
/* -=-=-=-=-=- DBGF Info Handlers -=-=-=-=-=- */
/**
* @callback_method_impl{FNDBGFHANDLERDEV}
*/
{
"HPET status:\n"
" config=%016RX64 isr=%016RX64\n"
" offset=%016RX64 counter=%016RX64 frequency=%08x\n"
" legacy-mode=%s timer-count=%u\n",
"Timers:\n");
{
}
}
/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
/**
* @callback_method_impl{FNSSMDEVLIVEEXEC}
*/
{
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* @callback_method_impl{FNSSMDEVSAVEEXEC}
*/
{
/*
* The config.
*/
/*
* The state.
*/
{
}
}
/**
* @callback_method_impl{FNSSMDEVLOADEXEC}
*/
static DECLCALLBACK(int) hpetLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
/*
* Version checks.
*/
return VINF_SUCCESS;
if (uVersion != HPET_SAVED_STATE_VERSION)
/*
* The config.
*/
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - too many timers: saved=%#x config=%#x"),
if (uPass != SSM_PASS_FINAL)
return VINF_SUCCESS;
/*
* The state.
*/
{
}
if (RT_FAILURE(rc))
return rc;
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Capabilities does not match timer count: cTimers=%#x caps=%#x"),
/*
* Set the timer frequency hints.
*/
{
}
return VINF_SUCCESS;
}
/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */
/**
* @interface_method_impl{PDMDEVREG,pfnRelocate}
*/
{
LogFlow(("hpetRelocate:\n"));
{
}
}
/**
* @interface_method_impl{PDMDEVREG,pfnReset}
*/
{
LogFlow(("hpetReset:\n"));
/*
* The timers first.
*/
{
/* capable of periodic operations and 64-bits */
pHpetTimer->u64Config = (i == 0)
: 0;
else
/* We can do all IRQs */
pHpetTimer->u64Period = 0;
pHpetTimer->u8Wrap = 0;
}
/*
* The HPET state.
*/
pThis->u64HpetConfig = 0;
pThis->u64HpetCounter = 0;
pThis->u64HpetOffset = 0;
/* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
| (1 << 13) /* COUNTER_SIZE_CAP - Main counter is 64-bit capable. */
| 1; /* REV_ID - Revision, must not be 0 */
else
/*
*/
if (pThis->pHpetHlpR3)
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
{
/* Only one HPET device now, as we use fixed MMIO region. */
/*
* Validate and read the configuration.
*/
bool fRCEnabled;
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"GCEnabled\" as a bool failed"));
bool fR0Enabled;
if (RT_FAILURE(rc))
N_("Configuration error: failed to read R0Enabled as boolean"));
if (RT_FAILURE(rc))
N_("Configuration error: failed to read ICH9 as boolean"));
/*
* Initialize the device state.
*/
/* No automatic locking. */
/* Init the HPET timers (init all regardless of how many we expose). */
{
pHpetTimer->idxTimer = i;
TMTIMER_FLAGS_NO_CRIT_SECT, "HPET Timer",
}
/* This must be done prior to registering the HPET, right? */
/*
* Register the HPET and get helpers.
*/
/*
* Register the MMIO range, PDM API requests page aligned
* addresses and sizes.
*/
if (fRCEnabled)
{
}
if (fR0Enabled)
{
}
/* Register SSM callbacks */
rc = PDMDevHlpSSMRegister3(pDevIns, HPET_SAVED_STATE_VERSION, sizeof(*pThis), hpetLiveExec, hpetSaveExec, hpetLoadExec);
/* Register an info callback. */
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceHPET =
{
/* u32Version */
/* szName */
"hpet",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
" High Precision Event Timer (HPET) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(HpetState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */