ReactOS 0.4.16-dev-570-g1868985
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#ifdef _M_IX86
160 KiEoiHelper(TrapFrame);
161#endif
162 return;
163 }
164
165 /* Read register C, so that the next interrupt can happen */
167
168 /* Save increment */
169 LastIncrement = HalpCurrentTimeIncrement;
170
171 /* Check if the running fraction has accounted for 100 ns */
173 if (HalpRunningFraction >= 1000)
174 {
175 LastIncrement++;
176 HalpRunningFraction -= 1000;
177 }
178
179 /* Check if someone changed the time rate */
181 {
182 /* Set new clock rate */
184
185 /* We're done */
187 }
188
189 /* Send the clock IPI to all other CPUs */
191
192 /* Update the system time -- on x86 the kernel will exit this trap */
193 KeUpdateSystemTime(TrapFrame, LastIncrement, Irql);
194
195 /* End the interrupt */
196 KiEndInterrupt(Irql, TrapFrame);
197}
#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
#define KiEndInterrupt(x, y)
Definition: ke.h:175
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 201 of file rtctimer.c.

202{
203 KIRQL Irql;
204
205 /* Enter trap */
206 KiEnterInterruptTrap(TrapFrame);
207#ifdef _M_AMD64
208 /* This is for debugging */
209 TrapFrame->ErrorCode = 0xc10c4;
210#endif
211
212 /* Start the interrupt */
214 {
215 /* Spurious, just end the interrupt */
216#ifdef _M_IX86
217 KiEoiHelper(TrapFrame);
218#endif
219 return;
220 }
221
222 /* Call the kernel to update runtimes */
223 KeUpdateRunTime(TrapFrame, Irql);
224
225 /* End the interrupt */
226 KiEndInterrupt(Irql, TrapFrame);
227}
VOID NTAPI KeUpdateRunTime(_In_ PKTRAP_FRAME TrapFrame, _In_ KIRQL Irql)

◆ 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:1683
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1688
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 231 of file rtctimer.c.

232{
233 UCHAR Rate;
234 ULONG NextIncrement;
235
236 /* Lookup largest value below given Increment */
237 for (Rate = RtcMinimumClockRate; Rate < RtcMaximumClockRate; Rate++)
238 {
239 /* Check if this is the largest rate possible */
240 NextIncrement = RtcClockRateToPreciseIncrement(Rate + 1) / 1000;
241 if (NextIncrement > Increment)
242 break;
243 }
244
245 /* Set the rate and tell HAL we want to change it */
246 HalpNextClockRate = Rate;
248
249 /* Return the real increment */
250 return RtcClockRateToPreciseIncrement(Rate) / 1000;
251}
#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().