DevPit-i8254.cpp revision 3614117c1132a61599e6190939e775cafe549411
/* $Id$ */
/** @file
* DevPIT-i8254 - Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
* --------------------------------------------------------------------
*
* This code is based on:
*
* QEMU 8253/8254 interval timer emulation
*
* Copyright (c) 2003-2004 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_PIT
#ifdef IN_RING3
#endif /* IN_RING3 */
#include "../Builtins.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The PIT frequency. */
#define PIT_FREQ 1193182
#define RW_STATE_LSB 1
#define RW_STATE_MSB 2
#define RW_STATE_WORD0 3
#define RW_STATE_WORD1 4
/** The current saved state version. */
#define PIT_SAVED_STATE_VERSION 4
/** The saved state version used by VirtualBox 3.1 and earlier.
* This did not include disable by HPET flag. */
#define PIT_SAVED_STATE_VERSION_VBOX_31 3
/** The saved state version used by VirtualBox 3.0 and earlier.
* This did not include the config part. */
#define PIT_SAVED_STATE_VERSION_VBOX_30 2
/** @def FAKE_REFRESH_CLOCK
* Define this to flip the 15usec refresh bit on every read.
* If not defined, it will be flipped correctly. */
/* #define FAKE_REFRESH_CLOCK */
#ifdef DOXYGEN_RUNNING
# define FAKE_REFRESH_CLOCK
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct PITChannelState
{
/** Pointer to the instance data - R3 Ptr. */
/** The timer - R3 Ptr. */
/** Pointer to the instance data - R0 Ptr. */
/** The timer - R0 Ptr. */
/** Pointer to the instance data - RC Ptr. */
/** The timer - RC Ptr. */
/** The virtual time stamp at the last reload. (only used in mode 2 for now) */
/** The actual time of the next tick.
* As apposed to the next_transition_time which contains the correct time of the next tick. */
/** (count_load_time is only set by TMTimerGet() which returns uint64_t) */
/* irq handling */
/** Number of release log entries. Used to prevent floading. */
typedef struct PITState
{
/** Speaker data. */
#ifdef FAKE_REFRESH_CLOCK
/** Speaker dummy. */
#else
#endif
/** Config: I/O port base. */
/** Config: Speaker enabled. */
bool fSpeakerCfg;
#if HC_ARCH_BITS == 64
bool afAlignment0[4];
#endif
/** PIT port interface. */
/** Pointer to the device instance. */
/** Number of IRQs that's been raised. */
/** Profiling the timer callback handler. */
} PITState;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
PDMBOTHCBDECL(int) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
PDMBOTHCBDECL(int) pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
#ifdef IN_RING3
PDMBOTHCBDECL(int) pitIOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#endif
static int pit_get_count(PITChannelState *s)
{
uint64_t d;
int counter;
if (s->mode == 2)
{
if (s->u64NextTS == UINT64_MAX)
{
d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
}
if (!Interval)
return s->count - 1; /** @todo This is WRONG! But I'm too tired to fix it properly and just want to shut up a DIV/0 trap now. */
d = TMTimerGet(pTimer);
if (d >= s->count)
return 1;
return s->count - d;
}
d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
switch(s->mode) {
case 0:
case 1:
case 4:
case 5:
break;
case 3:
/* XXX: may be incorrect for odd counts */
break;
default:
break;
}
/** @todo check that we don't return 0, in most modes (all?) the counter shouldn't be zero. */
return counter;
}
/* get pit output bit */
{
uint64_t d;
int out;
switch(s->mode) {
default:
case 0:
break;
case 1:
break;
case 2:
if ((d % s->count) == 0 && d != 0)
out = 1;
else
out = 0;
break;
case 3:
break;
case 4:
case 5:
break;
}
return out;
}
{
return pit_get_out1(s, current_time);
}
{
return s->gate;
}
/* if already latched, do not latch again */
static void pit_latch_count(PITChannelState *s)
{
if (!s->count_latched) {
s->latched_count = pit_get_count(s);
s->count_latched = s->rw_mode;
LogFlow(("pit_latch_count: latched_count=%#06x / %10RU64 ns (c=%#06x m=%d)\n",
s->latched_count, ASMMultU64ByU32DivByU32(s->count - s->latched_count, 1000000000, PIT_FREQ), s->count, s->mode));
}
}
#ifdef IN_RING3
/* val must be 0 or 1 */
{
switch(s->mode) {
default:
case 0:
case 4:
break;
case 1:
case 5:
/* restart counting on rising edge */
}
break;
case 2:
case 3:
/* restart counting on rising edge */
}
break;
}
}
{
if (val == 0)
val = 0x10000;
/* log the new rate (ch 0 only). */
if ( s->pTimerR3 /* ch 0 */
&& s->cRelLogEntries++ < 32)
LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
else
Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
}
/* return -1 if no transition will occur. */
{
switch(s->mode) {
default:
case 0:
case 1:
if (d < s->count)
else
return -1;
break;
/*
* Mode 2: The period is count + 1 PIT ticks.
* When the counter reaches 1 we sent the output low (for channel 0 that
* means raise an irq). On the next tick, where we should be decrementing
* from 1 to 0, the count is loaded and the output goes high (channel 0
* means clearing the irq).
*
* In VBox we simplify the tick cycle between 1 and 0 and immediately clears
* the irq. We also don't set it until we reach 0, which is a tick late - will
* try fix that later some day.
*/
case 2:
#ifndef VBOX /* see above */
if ((d - base) == 0 && d != 0)
else
#endif
break;
case 3:
else
break;
case 4:
case 5:
if (d < s->count)
else if (d == s->count)
else
return -1;
break;
}
/* convert to timer units */
next_time = s->count_load_time + ASMMultU64ByU32DivByU32(next_time, TMTimerGetFreq(pTimer), PIT_FREQ);
/* fix potential rounding problems */
/* XXX: better solution: use a clock at PIT_FREQ Hz */
if (next_time <= current_time)
return next_time;
}
{
int irq_level;
return;
/* We just flip-flop the irq level to save that extra timer call, which isn't generally required (we haven't served it for months). */
/* If PIT disabled by HPET - just disconnect ticks from interrupt controllers, and not modify
* other moments of device functioning.
* @todo: is it correct?
*/
if (!s->pPitR3->fDisabledByHpet)
{
if (irq_level)
}
if (irq_level)
{
s->u64ReloadTS = now;
}
if (expire_time != -1)
{
s->u64NextTS = expire_time;
}
else
{
s->u64NextTS = UINT64_MAX;
}
}
#endif /* IN_RING3 */
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
Port &= 3;
{
return VERR_IOM_IOPORT_UNUSED;
}
int ret;
if (s->status_latched)
{
s->status_latched = 0;
}
else if (s->count_latched)
{
switch (s->count_latched)
{
default:
case RW_STATE_LSB:
s->count_latched = 0;
break;
case RW_STATE_MSB:
s->count_latched = 0;
break;
case RW_STATE_WORD0:
s->count_latched = RW_STATE_MSB;
break;
}
}
else
{
int count;
switch (s->read_state)
{
default:
case RW_STATE_LSB:
count = pit_get_count(s);
break;
case RW_STATE_MSB:
count = pit_get_count(s);
break;
case RW_STATE_WORD0:
count = pit_get_count(s);
s->read_state = RW_STATE_WORD1;
break;
case RW_STATE_WORD1:
count = pit_get_count(s);
s->read_state = RW_STATE_WORD0;
break;
}
}
return VINF_SUCCESS;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
if (cb != 1)
return VINF_SUCCESS;
Port &= 3;
if (Port == 3)
{
/*
* 7 6 5 4 3 2 1 0
* * * . . . . . . Select channel: 0 0 = Channel 0
* 0 1 = Channel 1
* 1 0 = Channel 2
* 1 1 = Read-back command (8254 only)
* (Illegal on 8253)
* (Illegal on PS/2 {JAM})
* 0 1 = Access mode: lobyte only
* 1 0 = Access mode: hibyte only
* . . . . * * * . Operating mode: 0 0 0 = Mode 0, 0 0 1 = Mode 1,
* 0 1 0 = Mode 2, 0 1 1 = Mode 3,
* 1 0 0 = Mode 4, 1 0 1 = Mode 5,
* 1 1 0 = Mode 2, 1 1 1 = Mode 3
*/
if (channel == 3)
{
/* read-back command */
{
if (!(u32 & 0x20))
pit_latch_count(s);
{
/* status latch */
/* XXX: add BCD and null count */
| (s->rw_mode << 4)
| (s->mode << 1)
| s->bcd;
s->status_latched = 1;
}
}
}
}
else
{
if (access == 0)
pit_latch_count(s);
else
{
s->read_state = access;
s->write_state = access;
/* XXX: update irq timer ? */
}
}
}
else
{
#ifndef IN_RING3
return VINF_IOM_HC_IOPORT_WRITE;
#else /* IN_RING3 */
/*
* Port 40-42h - Channel Data Ports.
*/
switch(s->write_state)
{
default:
case RW_STATE_LSB:
pit_load_count(s, u32);
break;
case RW_STATE_MSB:
break;
case RW_STATE_WORD0:
s->write_latch = u32;
s->write_state = RW_STATE_WORD1;
break;
case RW_STATE_WORD1:
s->write_state = RW_STATE_WORD0;
break;
}
#endif /* !IN_RING3 */
}
return VINF_SUCCESS;
}
/**
* Port I/O Handler for speaker IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
/* bit 6,7 Parity error stuff. */
/* bit 5 - mirrors timer 2 output condition. */
/* bit 4 - toggled with each (DRAM?) refresh request, every 15.085 �s.
ASSUMES ns timer freq, see assertion above. */
#ifndef FAKE_REFRESH_CLOCK
#else
#endif
/* bit 2,3 NMI / parity status stuff. */
/* bit 1 - speaker data status */
/* bit 0 - timer 2 clock gate to speaker status. */
| (fSpeakerStatus << 1)
| (fRefresh << 4)
| (fOut << 5);
return VINF_SUCCESS;
}
return VERR_IOM_IOPORT_UNUSED;
}
#ifdef IN_RING3
/**
* Port I/O Handler for speaker OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) pitIOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
if (cb == 1)
{
}
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLIVEEXEC
*/
{
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
unsigned i;
/* The config. */
/* The state. */
{
}
#ifdef FAKE_REFRESH_CLOCK
#else
SSMR3PutS32(pSSM, 0);
#endif
return 0;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) pitLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
int rc;
if ( uVersion != PIT_SAVED_STATE_VERSION
/* The config. */
{
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - IOPortBaseCfg: saved=%RTiop config=%RTiop"),
bool fSpeakerCfg;
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fSpeakerCfg: saved=%RTbool config=%RTbool"),
}
if (uPass != SSM_PASS_FINAL)
return VINF_SUCCESS;
/* The state. */
{
{
LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d) (restore)\n",
}
}
#ifdef FAKE_REFRESH_CLOCK
#else
#endif
return 0;
}
/**
* 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 PIT channel state.
*/
{
Log(("pitTimer\n"));
}
/**
* Relocation notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
* @param offDelta The delta relative to the old address.
*/
{
unsigned i;
LogFlow(("pitRelocate: \n"));
{
}
}
/** @todo remove this! */
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
unsigned i;
LogFlow(("pitReset: \n"));
pThis->fDisabledByHpet = false;
{
#if 1 /* Set everything back to virgin state. (might not be strictly correct) */
s->latched_count = 0;
s->count_latched = 0;
s->status_latched = 0;
s->status = 0;
s->read_state = 0;
s->write_state = 0;
s->write_latch = 0;
s->rw_mode = 0;
s->bcd = 0;
#endif
s->u64NextTS = UINT64_MAX;
s->cRelLogEntries = 0;
s->mode = 3;
s->gate = (i != 2);
pit_load_count(s, 0);
}
}
/**
* Info handler, device version.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
{
unsigned i;
{
"PIT (i8254) channel %d status: irq=%#x\n"
" count=%08x" " latched_count=%04x count_latched=%02x\n"
" status=%02x status_latched=%02x read_state=%02x\n"
" write_state=%02x write_latch=%02x rw_mode=%02x\n"
" mode=%02x bcd=%02x gate=%02x\n"
" count_load_time=%016RX64 next_transition_time=%016RX64\n"
" u64ReloadTS=%016RX64 u64NextTS=%016RX64\n"
,
}
#ifdef FAKE_REFRESH_CLOCK
#else
#endif
if (pThis->fDisabledByHpet)
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* @interface_method_impl{PDMIPITPORT,pfnNotifyHpetLegacy}
*
* @returns VBox status code
* @param pInterface Pointer to the interface structure containing the called function pointer.
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
{
int rc;
bool fSpeaker;
bool fGCEnabled;
bool fR0Enabled;
unsigned i;
/*
* Validate configuration.
*/
/*
* Init the data.
*/
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"Base\" as a uint16_t failed"));
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"SpeakerEnabled\" as a bool failed"));
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"GCEnabled\" as a bool failed"));
if (RT_FAILURE(rc))
N_("Configuration error: failed to read R0Enabled as boolean"));
{
}
/*
* Create timer, register I/O Ports and save state.
*/
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "i8254 Programmable Interval Timer",
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegister(pDevIns, u16Base, 4, NULL, pitIOPortWrite, pitIOPortRead, NULL, NULL, "i8254 Programmable Interval Timer");
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
rc = PDMDevHlpIOPortRegisterRC(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled)
{
rc = PDMDevHlpIOPortRegisterR0(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
if (RT_FAILURE(rc))
return rc;
}
if (fSpeaker)
{
rc = PDMDevHlpIOPortRegister(pDevIns, 0x61, 1, NULL, pitIOPortSpeakerWrite, pitIOPortSpeakerRead, NULL, NULL, "PC Speaker");
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x61, 1, 0, NULL, "pitIOPortSpeakerRead", NULL, NULL, "PC Speaker");
if (RT_FAILURE(rc))
return rc;
}
}
rc = PDMDevHlpSSMRegister3(pDevIns, PIT_SAVED_STATE_VERSION, sizeof(*pThis), pitLiveExec, pitSaveExec, pitLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Interfaces
*/
/* IBase */
/* IPITPort */
/*
* Initialize the device state.
*/
/*
* Register statistics and debug info.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITIrq, STAMTYPE_COUNTER, "/TM/PIT/Irq", STAMUNIT_OCCURENCES, "The number of times a timer interrupt was triggered.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITHandler, STAMTYPE_PROFILE, "/TM/PIT/Handler", STAMUNIT_TICKS_PER_CALL, "Profiling timer callback handler.");
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceI8254 =
{
/* u32Version */
/* szName */
"i8254",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker 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(PITState),
/* 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 */