ReactOS  0.4.14-dev-41-g31d7680
tsc.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS HAL
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: hal/halx86/apic/tsc.c
5  * PURPOSE: HAL Routines for TSC handling
6  * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <hal.h>
12 #define NDEBUG
13 #include <debug.h>
14 
15 #include "tsc.h"
16 
18 
22 
23 #define RTC_MODE 6 /* Mode 6 is 1024 Hz */
24 #define SAMPLE_FREQENCY ((32768 << 1) >> RTC_MODE)
25 
26 /* PRIVATE FUNCTIONS *********************************************************/
27 
28 static
29 ULONG64
31  ULONG XMax,
32  ULONG64 *ArrayY)
33 {
34  ULONG X, SumXX;
35  ULONG64 SumXY;
36 
37  /* Calculate the sum of the squares of X */
38  SumXX = (XMax * (XMax + 1) * (2*XMax + 1)) / 6;
39 
40  /* Calculate the sum of the differences to the first value
41  weighted by x */
42  for (SumXY = 0, X = 1; X <= XMax; X++)
43  {
44  SumXY += X * (ArrayY[X] - ArrayY[0]);
45  }
46 
47  /* Account for sample frequency */
48  SumXY *= SAMPLE_FREQENCY;
49 
50  /* Return the quotient of the sums */
51  return (SumXY + (SumXX/2)) / SumXX;
52 }
53 
54 VOID
55 NTAPI
57 {
59  KIDTENTRY OldIdtEntry, *IdtPointer;
60  PKPCR Pcr = KeGetPcr();
61  UCHAR RegisterA, RegisterB;
62 
63  /* Check if the CPU supports RDTSC */
64  if (!(KeGetCurrentPrcb()->FeatureBits & KF_RDTSC))
65  {
66  KeBugCheck(HAL_INITIALIZATION_FAILED);
67  }
68 
69  /* Save flags and disable interrupts */
70  Flags = __readeflags();
71  _disable();
72 
73  /* Enable the periodic interrupt in the CMOS */
74  RegisterB = HalpReadCmos(RTC_REGISTER_B);
76 
77  /* Modify register A to RTC_MODE to get SAMPLE_FREQENCY */
78  RegisterA = HalpReadCmos(RTC_REGISTER_A);
79  RegisterA = (RegisterA & 0xF0) | RTC_MODE;
80  HalpWriteCmos(RTC_REGISTER_A, RegisterA);
81 
82  /* Save old IDT entry */
83  IdtPointer = KiGetIdtEntry(Pcr, HalpRtcClockVector);
84  OldIdtEntry = *IdtPointer;
85 
86  /* Set the calibration ISR */
88 
89  /* Reset TSC value to 0 */
91 
92  /* Enable the timer interrupt */
94 
95  /* Read register C, so that the next interrupt can happen */
97 
98  /* Wait for completion */
99  _enable();
101  _disable();
102 
103  /* Disable the periodic interrupt in the CMOS */
105 
106  /* Disable the timer interrupt */
108 
109  /* Restore old IDT entry */
110  *IdtPointer = OldIdtEntry;
111 
112  /* Calculate an average, using simplified linear regression */
115 
116  /* Restore flags */
118 
119 }
120 
121 VOID
122 NTAPI
124 {
125  // Timer interrupt is now active
126 
128 
129  KeGetPcr()->StallScaleFactor = (ULONG)(HalpCpuClockFrequency.QuadPart / 1000000);
130 }
131 
132 /* PUBLIC FUNCTIONS ***********************************************************/
133 
135 NTAPI
137  OUT PLARGE_INTEGER PerformanceFrequency OPTIONAL)
138 {
140 
141  /* Make sure it's calibrated */
143 
144  /* Does the caller want the frequency? */
145  if (PerformanceFrequency)
146  {
147  /* Return tsc frequency */
148  *PerformanceFrequency = HalpCpuClockFrequency;
149  }
150 
151  /* Return the current value */
152  Result.QuadPart = __rdtsc();
153  return Result;
154 }
155 
156 VOID
157 NTAPI
159 {
160  ULONG64 StartTime, EndTime;
161 
162  /* Get the initial time */
163  StartTime = __rdtsc();
164 
165  /* Calculate the ending time */
166  EndTime = StartTime + KeGetPcr()->StallScaleFactor * MicroSeconds;
167 
168  /* Loop until time is elapsed */
169  while (__rdtsc() < EndTime);
170 }
171 
172 VOID
173 NTAPI
175  IN volatile PLONG Count,
176  IN ULONGLONG NewCount)
177 {
179  ASSERT(FALSE);
180 }
181 
#define INITIAL_STALL_COUNT
Definition: processor.c:22
#define IN
Definition: typedefs.h:38
VOID FORCEINLINE HalpWriteCmos(IN UCHAR Reg, IN UCHAR Value)
Definition: cmos.c:35
BOOLEAN NTAPI HalEnableSystemInterrupt(IN ULONG Vector, IN KIRQL Irql, IN KINTERRUPT_MODE InterruptMode)
Definition: pic.c:295
VOID NTAPI HalpInitializeTsc(VOID)
Definition: tsc.c:56
#define RTC_MODE
Definition: tsc.c:23
#define _ReadWriteBarrier()
Definition: intrin_arm.h:36
#define RTC_REG_B_PI
Definition: halp.h:55
VOID NTAPI KeStallExecutionProcessor(ULONG MicroSeconds)
Definition: tsc.c:158
UCHAR HalpRtcClockVector
Definition: tsc.c:21
void __cdecl _enable(void)
Definition: intrin_arm.h:373
#define KF_RDTSC
Definition: ketypes.h:144
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1062
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define MSR_RDTSC
Definition: tsc.h:6
VOID NTAPI HalCalibratePerformanceCounter(IN volatile PLONG Count, IN ULONGLONG NewCount)
Definition: tsc.c:174
Definition: ke.h:280
static LARGE_INTEGER StartTime
Definition: sys_arch.c:18
#define SAMPLE_FREQENCY
Definition: tsc.c:24
#define KeGetPcr()
Definition: ke.h:25
UCHAR TscCalibrationPhase
Definition: tsc.c:19
VOID NTAPI HalDisableSystemInterrupt(IN ULONG Vector, IN KIRQL Irql)
Definition: pic.c:309
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define CLOCK_LEVEL
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1555
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define NUM_SAMPLES
Definition: tsc.h:5
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
UCHAR FORCEINLINE HalpReadCmos(IN UCHAR Reg)
Definition: cmos.c:24
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1507
uint64_t ULONGLONG
Definition: typedefs.h:65
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define RTC_REGISTER_B
Definition: halp.h:17
void __cdecl TscCalibrationISR(void)
unsigned __int64 ULONG64
Definition: imports.h:198
unsigned char UCHAR
Definition: xmlstorage.h:181
__INTRIN_INLINE void __writeeflags(uintptr_t Value)
Definition: intrin_x86.h:1550
PPC_QUAL void __writemsr(const unsigned long Value)
Definition: intrin_ppc.h:748
FORCEINLINE VOID KeRegisterInterruptHandler(IN ULONG Vector, IN PVOID Handler)
Definition: ke.h:242
#define RTC_REGISTER_C
Definition: halp.h:56
ULONG64 TscCalibrationArray[NUM_SAMPLES]
Definition: tsc.c:20
#define KiGetIdtEntry(Pcr, Vector)
Definition: tsc.h:17
#define RTC_REGISTER_A
Definition: xboxrtc.c:21
#define OUT
Definition: typedefs.h:39
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
#define UNIMPLEMENTED
Definition: debug.h:114
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(OUT PLARGE_INTEGER PerformanceFrequency OPTIONAL)
Definition: tsc.c:136
LARGE_INTEGER HalpCpuClockFrequency
Definition: tsc.c:17
static ULONG64 DoLinearRegression(ULONG XMax, ULONG64 *ArrayY)
Definition: tsc.c:30
VOID NTAPI HalpCalibrateStallExecution(VOID)
Definition: tsc.c:123
signed int * PLONG
Definition: retypes.h:5
#define X(b, s)
LONGLONG QuadPart
Definition: typedefs.h:112
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68