ReactOS 0.4.15-dev-5836-g942b022
rtctimer.c File Reference
#include <hal.h>
#include "apicp.h"
#include <debug.h>
Include dependency graph for rtctimer.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

FORCEINLINE ULONG RtcClockRateToPreciseIncrement (UCHAR Rate)
 Converts the CMOS RTC rate into the time increment in 100ns intervals.
 
VOID RtcSetClockRate (UCHAR ClockRate)
 
VOID NTAPI HalpInitializeClock (VOID)
 
VOID FASTCALL HalpClockInterruptHandler (IN PKTRAP_FRAME TrapFrame)
 
ULONG NTAPI HalSetTimeIncrement (IN ULONG Increment)
 

Variables

static const UCHAR RtcMinimumClockRate = 6
 
static const UCHAR RtcMaximumClockRate = 10
 
static UCHAR HalpCurrentClockRate = 10
 
static ULONG HalpCurrentTimeIncrement
 
static ULONG HalpMinimumTimeIncrement
 
static ULONG HalpMaximumTimeIncrement
 
static ULONG HalpCurrentFractionalIncrement
 
static ULONG HalpRunningFraction
 
static BOOLEAN HalpSetClockRate
 
static UCHAR HalpNextClockRate
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 16 of file rtctimer.c.

Function Documentation

◆ HalpClockInterruptHandler()

VOID FASTCALL HalpClockInterruptHandler ( IN PKTRAP_FRAME  TrapFrame)

Definition at line 142 of file rtctimer.c.

143{
144 ULONG LastIncrement;
145 KIRQL Irql;
146
147 /* Enter trap */
148 KiEnterInterruptTrap(TrapFrame);
149#ifdef _M_AMD64
150 /* This is for debugging */
151 TrapFrame->ErrorCode = 0xc10c4;
152#endif
153
154 /* Start the interrupt */
156 {
157 /* Spurious, just end the interrupt */
158 KiEoiHelper(TrapFrame);
159 }
160
161 /* Read register C, so that the next interrupt can happen */
163
164 /* Save increment */
165 LastIncrement = HalpCurrentTimeIncrement;
166
167 /* Check if the running fraction has accounted for 100 ns */
169 if (HalpRunningFraction >= 1000)
170 {
171 LastIncrement++;
172 HalpRunningFraction -= 1000;
173 }
174
175 /* Check if someone changed the time rate */
177 {
178 /* Set new clock rate */
180
181 /* We're done */
183 }
184
185 /* Update the system time -- on x86 the kernel will exit this trap */
186 KeUpdateSystemTime(TrapFrame, LastIncrement, Irql);
187}
#define APIC_CLOCK_VECTOR
Definition: apicp.h:44
_Out_ PKIRQL Irql
Definition: csq.h:179
#define FALSE
Definition: types.h:117
UCHAR KIRQL
Definition: env_spec_w32.h:591
BOOLEAN NTAPI HalBeginSystemInterrupt(IN KIRQL Irql, IN ULONG Vector, OUT PKIRQL OldIrql)
Definition: pic.c:321
VOID FASTCALL KeUpdateSystemTime(IN PKTRAP_FRAME TrapFrame, IN ULONG Increment, IN KIRQL OldIrql)
Definition: time.c:64
UCHAR NTAPI HalpReadCmos(_In_ UCHAR Reg)
Definition: cmos.c:123
#define RTC_REGISTER_C
Definition: halhw.h:17
#define CLOCK_LEVEL
Definition: ketypes.h:16
DECLSPEC_NORETURN VOID FASTCALL KiEoiHelper(IN PKTRAP_FRAME TrapFrame)
Definition: traphdlr.c:126
static BOOLEAN HalpSetClockRate
Definition: rtctimer.c:29
static ULONG HalpCurrentFractionalIncrement
Definition: rtctimer.c:27
static UCHAR HalpNextClockRate
Definition: rtctimer.c:30
static ULONG HalpCurrentTimeIncrement
Definition: rtctimer.c:24
static ULONG HalpRunningFraction
Definition: rtctimer.c:28
VOID RtcSetClockRate(UCHAR ClockRate)
Definition: rtctimer.c:67
FORCEINLINE VOID KiEnterInterruptTrap(IN PKTRAP_FRAME TrapFrame)
Definition: trap_x.h:368
uint32_t ULONG
Definition: typedefs.h:59

◆ HalpInitializeClock()

VOID NTAPI HalpInitializeClock ( VOID  )

Definition at line 100 of file rtctimer.c.

101{
102 ULONG_PTR EFlags;
103 UCHAR RegisterB;
104
105 /* Save EFlags and disable interrupts */
106 EFlags = __readeflags();
107 _disable();
108
109 // TODO: disable NMI
110
111 /* Acquire CMOS lock */
113
114 /* Enable the periodic interrupt in the CMOS */
115 RegisterB = HalpReadCmos(RTC_REGISTER_B);
117
118 /* Release CMOS lock */
120
121 /* Set initial rate */
123
124 /* Restore interrupt state */
125 __writeeflags(EFlags);
126
127 /* Calculate minumum and maximum increment */
130
131 /* Notify the kernel about the maximum and minimum increment */
133
134 /* Enable the timer interrupt */
136
137 DPRINT1("Clock initialized\n");
138}
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI KeSetTimeIncrement(IN ULONG MaxIncrement, IN ULONG MinIncrement)
Definition: ntoskrnl.c:28
BOOLEAN NTAPI HalEnableSystemInterrupt(IN ULONG Vector, IN KIRQL Irql, IN KINTERRUPT_MODE InterruptMode)
Definition: pic.c:295
VOID NTAPI HalpReleaseCmosSpinLock(VOID)
Definition: spinlock.c:243
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:226
VOID NTAPI HalpWriteCmos(_In_ UCHAR Reg, _In_ UCHAR Value)
Definition: cmos.c:132
#define RTC_REG_B_PI
Definition: halhw.h:16
#define RTC_REGISTER_B
Definition: halhw.h:15
void __cdecl _disable(void)
Definition: intrin_arm.h:365
__INTRIN_INLINE void __writeeflags(uintptr_t Value)
Definition: intrin_x86.h:1669
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1674
static UCHAR HalpCurrentClockRate
Definition: rtctimer.c:23
static const UCHAR RtcMinimumClockRate
Definition: rtctimer.c:21
static const UCHAR RtcMaximumClockRate
Definition: rtctimer.c:22
FORCEINLINE ULONG RtcClockRateToPreciseIncrement(UCHAR Rate)
Converts the CMOS RTC rate into the time increment in 100ns intervals.
Definition: rtctimer.c:57
static ULONG HalpMinimumTimeIncrement
Definition: rtctimer.c:25
static ULONG HalpMaximumTimeIncrement
Definition: rtctimer.c:26
@ Latched
Definition: miniport.h:81
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ HalSetTimeIncrement()

ULONG NTAPI HalSetTimeIncrement ( IN ULONG  Increment)

Definition at line 191 of file rtctimer.c.

192{
193 UCHAR Rate;
194 ULONG CurrentIncrement;
195
196 /* Lookup largest value below given Increment */
197 for (Rate = RtcMinimumClockRate; Rate <= RtcMaximumClockRate; Rate++)
198 {
199 /* Check if this is the largest rate possible */
200 CurrentIncrement = RtcClockRateToPreciseIncrement(Rate + 1) / 1000;
201 if (Increment > CurrentIncrement) break;
202 }
203
204 /* Set the rate and tell HAL we want to change it */
205 HalpNextClockRate = Rate;
207
208 /* Return the real increment */
209 return RtcClockRateToPreciseIncrement(Rate) / 1000;
210}
#define TRUE
Definition: types.h:120
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG Increment
Definition: CrNtStubs.h:46

◆ RtcClockRateToPreciseIncrement()

FORCEINLINE ULONG RtcClockRateToPreciseIncrement ( UCHAR  Rate)

Converts the CMOS RTC rate into the time increment in 100ns intervals.

Rate Frequency Interval (ms) Precise increment (0.1ns)

0 disabled 1 32768 0.03052 305,175 2 16384 0.06103 610,351 3 8192 0.12207 1,220,703 4 4096 0.24414 2,441,406 5 2048 0.48828 4,882,812 6 1024 0.97656 9,765,625 <- minimum 7 512 1.95313 19,531,250 8 256 3.90625 39,062,500 9 128 7.8125 78,125,000 10 64 15.6250 156,250,000 <- maximum / default 11 32 31.25 312,500,000 12 16 62.5 625,000,000 13 8 125 1,250,000,000 14 4 250 2,500,000,000 15 2 500 5,000,000,000

Definition at line 57 of file rtctimer.c.

58{
59 /* Calculate frequency */
60 ULONG Frequency = 32768 >> (Rate - 1);
61
62 /* Calculate interval in 0.1ns interval: Interval = (1 / Frequency) * 10,000,000,000 */
63 return 10000000000ULL / Frequency;
64}
static LARGE_INTEGER Frequency
Definition: clock.c:41

Referenced by HalpInitializeClock(), HalSetTimeIncrement(), and RtcSetClockRate().

◆ RtcSetClockRate()

VOID RtcSetClockRate ( UCHAR  ClockRate)

Definition at line 67 of file rtctimer.c.

68{
69 UCHAR RegisterA;
70 ULONG PreciseIncrement;
71
72 /* Update the global values */
73 HalpCurrentClockRate = ClockRate;
74 PreciseIncrement = RtcClockRateToPreciseIncrement(ClockRate);
75 HalpCurrentTimeIncrement = PreciseIncrement / 1000;
76 HalpCurrentFractionalIncrement = PreciseIncrement % 1000;
77
78 /* Acquire CMOS lock */
80
81 // TODO: disable NMI
82
83 /* Read value of register A */
84 RegisterA = HalpReadCmos(RTC_REGISTER_A);
85
86 /* Change lower 4 bits to new rate */
87 RegisterA &= 0xF0;
88 RegisterA |= ClockRate;
89
90 /* Write the new value */
91 HalpWriteCmos(RTC_REGISTER_A, RegisterA);
92
93 /* Release CMOS lock */
95}
#define RTC_REGISTER_A
Definition: xboxrtc.c:21

Referenced by HalpClockInterruptHandler(), and HalpInitializeClock().

Variable Documentation

◆ HalpCurrentClockRate

UCHAR HalpCurrentClockRate = 10
static

Definition at line 23 of file rtctimer.c.

Referenced by HalpInitializeClock(), and RtcSetClockRate().

◆ HalpCurrentFractionalIncrement

ULONG HalpCurrentFractionalIncrement
static

Definition at line 27 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and RtcSetClockRate().

◆ HalpCurrentTimeIncrement

ULONG HalpCurrentTimeIncrement
static

Definition at line 24 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and RtcSetClockRate().

◆ HalpMaximumTimeIncrement

ULONG HalpMaximumTimeIncrement
static

Definition at line 26 of file rtctimer.c.

Referenced by HalpInitializeClock().

◆ HalpMinimumTimeIncrement

ULONG HalpMinimumTimeIncrement
static

Definition at line 25 of file rtctimer.c.

Referenced by HalpInitializeClock().

◆ HalpNextClockRate

UCHAR HalpNextClockRate
static

Definition at line 30 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and HalSetTimeIncrement().

◆ HalpRunningFraction

ULONG HalpRunningFraction
static

Definition at line 28 of file rtctimer.c.

Referenced by HalpClockInterruptHandler().

◆ HalpSetClockRate

BOOLEAN HalpSetClockRate
static

Definition at line 29 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and HalSetTimeIncrement().

◆ RtcMaximumClockRate

const UCHAR RtcMaximumClockRate = 10
static

Definition at line 22 of file rtctimer.c.

Referenced by HalpInitializeClock(), and HalSetTimeIncrement().

◆ RtcMinimumClockRate

const UCHAR RtcMinimumClockRate = 6
static

Definition at line 21 of file rtctimer.c.

Referenced by HalpInitializeClock(), and HalSetTimeIncrement().