ReactOS 0.4.16-dev-297-gc569aee
rtctimer.c File Reference
#include <hal.h>
#include "apicp.h"
#include <smp.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 0.1ns intervals.
 
VOID RtcSetClockRate (UCHAR ClockRate)
 
VOID NTAPI HalpInitializeClock (VOID)
 
VOID FASTCALL HalpClockInterruptHandler (IN PKTRAP_FRAME TrapFrame)
 
VOID FASTCALL HalpClockIpiHandler (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 17 of file rtctimer.c.

Function Documentation

◆ HalpClockInterruptHandler()

VOID FASTCALL HalpClockInterruptHandler ( IN PKTRAP_FRAME  TrapFrame)

Definition at line 143 of file rtctimer.c.

144{
145 ULONG LastIncrement;
146 KIRQL Irql;
147
148 /* Enter trap */
149 KiEnterInterruptTrap(TrapFrame);
150#ifdef _M_AMD64
151 /* This is for debugging */
152 TrapFrame->ErrorCode = 0xc10c4;
153#endif
154
155 /* Start the interrupt */
157 {
158 /* Spurious, just end the interrupt */
159 KiEoiHelper(TrapFrame);
160 }
161
162 /* Read register C, so that the next interrupt can happen */
164
165 /* Save increment */
166 LastIncrement = HalpCurrentTimeIncrement;
167
168 /* Check if the running fraction has accounted for 100 ns */
170 if (HalpRunningFraction >= 1000)
171 {
172 LastIncrement++;
173 HalpRunningFraction -= 1000;
174 }
175
176 /* Check if someone changed the time rate */
178 {
179 /* Set new clock rate */
181
182 /* We're done */
184 }
185
186 /* Send the clock IPI to all other CPUs */
188
189 /* Update the system time -- on x86 the kernel will exit this trap */
190 KeUpdateSystemTime(TrapFrame, LastIncrement, Irql);
191}
#define CLOCK_IPI_VECTOR
Definition: apicp.h:46
#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:30
static ULONG HalpCurrentFractionalIncrement
Definition: rtctimer.c:28
static UCHAR HalpNextClockRate
Definition: rtctimer.c:31
static ULONG HalpCurrentTimeIncrement
Definition: rtctimer.c:25
static ULONG HalpRunningFraction
Definition: rtctimer.c:29
VOID RtcSetClockRate(UCHAR ClockRate)
Definition: rtctimer.c:68
FORCEINLINE VOID KiEnterInterruptTrap(IN PKTRAP_FRAME TrapFrame)
Definition: trap_x.h:368
uint32_t ULONG
Definition: typedefs.h:59
VOID FASTCALL HalpBroadcastClockIpi(_In_ UCHAR Vector)
Definition: up.c:45

◆ HalpClockIpiHandler()

VOID FASTCALL HalpClockIpiHandler ( IN PKTRAP_FRAME  TrapFrame)

Definition at line 195 of file rtctimer.c.

196{
197 KIRQL Irql;
198
199 /* Enter trap */
200 KiEnterInterruptTrap(TrapFrame);
201#ifdef _M_AMD64
202 /* This is for debugging */
203 TrapFrame->ErrorCode = 0xc10c4;
204#endif
205
206 /* Start the interrupt */
208 {
209 /* Spurious, just end the interrupt */
210 KiEoiHelper(TrapFrame);
211 }
212
213 /* Call the kernel to update runtimes */
214 KeUpdateRunTime(TrapFrame, Irql);
215
216 /* End the interrupt */
217 KiEndInterrupt(Irql, TrapFrame);
218}
VOID NTAPI KeUpdateRunTime(_In_ PKTRAP_FRAME TrapFrame, _In_ KIRQL Irql)
#define KiEndInterrupt(x, y)
Definition: ke.h:175

◆ HalpInitializeClock()

VOID NTAPI HalpInitializeClock ( VOID  )

Definition at line 101 of file rtctimer.c.

102{
103 ULONG_PTR EFlags;
104 UCHAR RegisterB;
105
106 /* Save EFlags and disable interrupts */
107 EFlags = __readeflags();
108 _disable();
109
110 // TODO: disable NMI
111
112 /* Acquire CMOS lock */
114
115 /* Enable the periodic interrupt in the CMOS */
116 RegisterB = HalpReadCmos(RTC_REGISTER_B);
118
119 /* Release CMOS lock */
121
122 /* Set initial rate */
124
125 /* Restore interrupt state */
126 __writeeflags(EFlags);
127
128 /* Calculate minumum and maximum increment */
131
132 /* Notify the kernel about the maximum and minimum increment */
134
135 /* Enable the timer interrupt */
137
138 DPRINT1("Clock initialized\n");
139}
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI KeSetTimeIncrement(IN ULONG MaxIncrement, IN ULONG MinIncrement)
Definition: ntoskrnl.c:32
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:24
static const UCHAR RtcMinimumClockRate
Definition: rtctimer.c:22
static const UCHAR RtcMaximumClockRate
Definition: rtctimer.c:23
FORCEINLINE ULONG RtcClockRateToPreciseIncrement(UCHAR Rate)
Converts the CMOS RTC rate into the time increment in 0.1ns intervals.
Definition: rtctimer.c:58
static ULONG HalpMinimumTimeIncrement
Definition: rtctimer.c:26
static ULONG HalpMaximumTimeIncrement
Definition: rtctimer.c:27
@ 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 222 of file rtctimer.c.

223{
224 UCHAR Rate;
225 ULONG NextIncrement;
226
227 /* Lookup largest value below given Increment */
228 for (Rate = RtcMinimumClockRate; Rate < RtcMaximumClockRate; Rate++)
229 {
230 /* Check if this is the largest rate possible */
231 NextIncrement = RtcClockRateToPreciseIncrement(Rate + 1) / 1000;
232 if (NextIncrement > Increment)
233 break;
234 }
235
236 /* Set the rate and tell HAL we want to change it */
237 HalpNextClockRate = Rate;
239
240 /* Return the real increment */
241 return RtcClockRateToPreciseIncrement(Rate) / 1000;
242}
#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 0.1ns 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 58 of file rtctimer.c.

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

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

◆ RtcSetClockRate()

VOID RtcSetClockRate ( UCHAR  ClockRate)

Definition at line 68 of file rtctimer.c.

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

Referenced by HalpClockInterruptHandler(), and HalpInitializeClock().

Variable Documentation

◆ HalpCurrentClockRate

UCHAR HalpCurrentClockRate = 10
static

Definition at line 24 of file rtctimer.c.

Referenced by HalpInitializeClock(), and RtcSetClockRate().

◆ HalpCurrentFractionalIncrement

ULONG HalpCurrentFractionalIncrement
static

Definition at line 28 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and RtcSetClockRate().

◆ HalpCurrentTimeIncrement

ULONG HalpCurrentTimeIncrement
static

Definition at line 25 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and RtcSetClockRate().

◆ HalpMaximumTimeIncrement

ULONG HalpMaximumTimeIncrement
static

Definition at line 27 of file rtctimer.c.

Referenced by HalpInitializeClock().

◆ HalpMinimumTimeIncrement

ULONG HalpMinimumTimeIncrement
static

Definition at line 26 of file rtctimer.c.

Referenced by HalpInitializeClock().

◆ HalpNextClockRate

UCHAR HalpNextClockRate
static

Definition at line 31 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and HalSetTimeIncrement().

◆ HalpRunningFraction

ULONG HalpRunningFraction
static

Definition at line 29 of file rtctimer.c.

Referenced by HalpClockInterruptHandler().

◆ HalpSetClockRate

BOOLEAN HalpSetClockRate
static

Definition at line 30 of file rtctimer.c.

Referenced by HalpClockInterruptHandler(), and HalSetTimeIncrement().

◆ RtcMaximumClockRate

const UCHAR RtcMaximumClockRate = 10
static

Definition at line 23 of file rtctimer.c.

Referenced by HalpInitializeClock(), and HalSetTimeIncrement().

◆ RtcMinimumClockRate

const UCHAR RtcMinimumClockRate = 6
static

Definition at line 22 of file rtctimer.c.

Referenced by HalpInitializeClock(), and HalSetTimeIncrement().