2011-09-06 21:01:49 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS HAL
|
|
|
|
* LICENSE: GNU GPL - See COPYING in the top level directory
|
|
|
|
* FILE: hal/halx86/generic/apic.c
|
|
|
|
* PURPOSE: HAL APIC Management and Control Code
|
|
|
|
* PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
|
|
|
|
* REFERENCES:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include <hal.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
|
|
|
const UCHAR HalpClockVector = 0xD1;
|
|
|
|
BOOLEAN HalpClockSetMSRate;
|
|
|
|
UCHAR HalpNextMSRate;
|
2012-02-06 00:24:31 +00:00
|
|
|
UCHAR HalpCurrentRate = 9; /* Initial rate 9: 128 Hz / 7.8 ms */
|
2011-09-06 21:01:49 +00:00
|
|
|
ULONG HalpCurrentTimeIncrement;
|
2012-02-06 00:24:31 +00:00
|
|
|
static UCHAR RtcMinimumClockRate = 6; /* Minimum rate 6: 16 Hz / 62.5 ms */
|
|
|
|
static UCHAR RtcMaximumClockRate = 10; /* Maximum rate 10: 256 Hz / 3.9 ms */
|
2011-09-06 21:01:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
FORCEINLINE
|
2014-05-04 09:39:44 +00:00
|
|
|
ULONG
|
2011-09-06 21:01:49 +00:00
|
|
|
RtcClockRateToIncrement(UCHAR Rate)
|
|
|
|
{
|
|
|
|
ULONG Freqency = ((32768 << 1) >> Rate);
|
|
|
|
return (1000000 + (Freqency/2)) / Freqency;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
RtcSetClockRate(UCHAR ClockRate)
|
|
|
|
{
|
|
|
|
UCHAR RegisterA;
|
|
|
|
|
2011-09-09 15:42:59 +00:00
|
|
|
/* Update the global values */
|
|
|
|
HalpCurrentRate = ClockRate;
|
|
|
|
HalpCurrentTimeIncrement = RtcClockRateToIncrement(ClockRate);
|
|
|
|
|
2011-09-07 21:41:50 +00:00
|
|
|
/* Acquire CMOS lock */
|
|
|
|
HalpAcquireCmosSpinLock();
|
|
|
|
|
2011-09-06 21:01:49 +00:00
|
|
|
// TODO: disable NMI
|
|
|
|
|
|
|
|
/* Read value of register A */
|
|
|
|
RegisterA = HalpReadCmos(RTC_REGISTER_A);
|
|
|
|
|
|
|
|
/* Change lower 4 bits to new rate */
|
|
|
|
RegisterA &= 0xF0;
|
|
|
|
RegisterA |= ClockRate;
|
|
|
|
|
|
|
|
/* Write the new value */
|
|
|
|
HalpWriteCmos(RTC_REGISTER_A, RegisterA);
|
|
|
|
|
2011-09-07 21:41:50 +00:00
|
|
|
/* Release CMOS lock */
|
|
|
|
HalpReleaseCmosSpinLock();
|
2011-09-06 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
INIT_FUNCTION
|
|
|
|
HalpInitializeClock(VOID)
|
|
|
|
{
|
2011-09-11 09:34:50 +00:00
|
|
|
ULONG_PTR EFlags;
|
2011-09-06 21:01:49 +00:00
|
|
|
UCHAR RegisterB;
|
2011-09-11 09:34:50 +00:00
|
|
|
|
|
|
|
/* Save EFlags and disable interrupts */
|
|
|
|
EFlags = __readeflags();
|
|
|
|
_disable();
|
|
|
|
|
2011-09-06 21:01:49 +00:00
|
|
|
// TODO: disable NMI
|
|
|
|
|
2011-09-07 21:41:50 +00:00
|
|
|
/* Acquire CMOS lock */
|
|
|
|
HalpAcquireCmosSpinLock();
|
|
|
|
|
2011-09-06 21:01:49 +00:00
|
|
|
/* Enable the periodic interrupt in the CMOS */
|
|
|
|
RegisterB = HalpReadCmos(RTC_REGISTER_B);
|
|
|
|
HalpWriteCmos(RTC_REGISTER_B, RegisterB | RTC_REG_B_PI);
|
|
|
|
|
2011-09-07 21:41:50 +00:00
|
|
|
/* Release CMOS lock */
|
|
|
|
HalpReleaseCmosSpinLock();
|
|
|
|
|
2011-09-09 15:42:59 +00:00
|
|
|
/* Set initial rate */
|
2011-09-08 08:15:39 +00:00
|
|
|
RtcSetClockRate(HalpCurrentRate);
|
|
|
|
|
2011-09-11 09:34:50 +00:00
|
|
|
/* Restore interrupt state */
|
|
|
|
__writeeflags(EFlags);
|
|
|
|
|
2011-09-09 15:42:59 +00:00
|
|
|
/* Notify the kernel about the maximum and minimum increment */
|
|
|
|
KeSetTimeIncrement(RtcClockRateToIncrement(RtcMaximumClockRate),
|
|
|
|
RtcClockRateToIncrement(RtcMinimumClockRate));
|
|
|
|
|
2011-09-11 09:34:50 +00:00
|
|
|
|
2011-09-08 08:15:39 +00:00
|
|
|
DPRINT1("Clock initialized\n");
|
2011-09-06 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
HalpClockInterruptHandler(IN PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
|
|
|
ULONG LastIncrement;
|
|
|
|
KIRQL Irql;
|
|
|
|
|
|
|
|
/* Enter trap */
|
|
|
|
KiEnterInterruptTrap(TrapFrame);
|
2011-09-16 09:39:41 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
/* This is for debugging */
|
|
|
|
TrapFrame->ErrorCode = 0xc10c4;
|
|
|
|
#endif
|
2011-09-07 21:41:50 +00:00
|
|
|
|
2011-09-06 21:01:49 +00:00
|
|
|
/* Start the interrupt */
|
2011-09-16 09:39:41 +00:00
|
|
|
if (!HalBeginSystemInterrupt(CLOCK_LEVEL, HalpClockVector, &Irql))
|
2011-09-06 21:01:49 +00:00
|
|
|
{
|
2011-09-16 09:39:41 +00:00
|
|
|
/* Spurious, just end the interrupt */
|
|
|
|
KiEoiHelper(TrapFrame);
|
|
|
|
}
|
2011-09-06 21:01:49 +00:00
|
|
|
|
2011-09-16 09:39:41 +00:00
|
|
|
/* Read register C, so that the next interrupt can happen */
|
|
|
|
HalpReadCmos(RTC_REGISTER_C);;
|
2011-09-06 21:01:49 +00:00
|
|
|
|
2011-09-16 09:39:41 +00:00
|
|
|
/* Save increment */
|
|
|
|
LastIncrement = HalpCurrentTimeIncrement;
|
2011-09-06 21:01:49 +00:00
|
|
|
|
2011-09-16 09:39:41 +00:00
|
|
|
/* Check if someone changed the time rate */
|
|
|
|
if (HalpClockSetMSRate)
|
|
|
|
{
|
|
|
|
/* Set new clock rate */
|
2012-02-06 00:24:31 +00:00
|
|
|
RtcSetClockRate(HalpNextMSRate);
|
2011-09-06 21:01:49 +00:00
|
|
|
|
2011-09-16 09:39:41 +00:00
|
|
|
/* We're done */
|
|
|
|
HalpClockSetMSRate = FALSE;
|
2011-09-06 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
2011-09-16 09:39:41 +00:00
|
|
|
/* Update the system time -- on x86 the kernel will exit this trap */
|
|
|
|
KeUpdateSystemTime(TrapFrame, LastIncrement, Irql);
|
2011-09-06 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
HalpProfileInterruptHandler(IN PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
|
|
|
__debugbreak();
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG
|
|
|
|
NTAPI
|
|
|
|
HalSetTimeIncrement(IN ULONG Increment)
|
|
|
|
{
|
|
|
|
UCHAR Rate;
|
|
|
|
|
|
|
|
/* Lookup largest value below given Increment */
|
2011-09-09 15:42:59 +00:00
|
|
|
for (Rate = RtcMinimumClockRate; Rate <= RtcMaximumClockRate; Rate++)
|
2011-09-06 21:01:49 +00:00
|
|
|
{
|
|
|
|
/* Check if this is the largest rate possible */
|
|
|
|
if (RtcClockRateToIncrement(Rate + 1) > Increment) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the rate and tell HAL we want to change it */
|
|
|
|
HalpNextMSRate = Rate;
|
|
|
|
HalpClockSetMSRate = TRUE;
|
|
|
|
|
|
|
|
/* Return the real increment */
|
|
|
|
return RtcClockRateToIncrement(Rate);
|
|
|
|
}
|