Fixed tracker #52
[mirror_edk2.git] / MdePkg / Library / BaseTimerLibLocalApic / x86TimerLib.c
1 /** @file
2 Timer Library functions built upon local APIC on IA32/x64.
3
4 @bug Should use PCD to retrieve all the constants including index of
5 the IA32_APIC_BASE MSR, the offsets of InitialCount, CorrentCount
6 and DivideConfiguration.
7
8 Copyright (c) 2006, Intel Corporation<BR>
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 Module Name: x86TimerLib.c
18
19 **/
20
21 static
22 UINTN
23 EFIAPI
24 DelayWorker (
25 IN UINT64 NDelay
26 )
27 {
28 UINT64 Ticks;
29
30 Ticks = GetPerformanceCounter ();
31 Ticks -= DivU64x32 (
32 MultU64x64 (
33 GetPerformanceCounterProperties (NULL, NULL),
34 NDelay
35 ),
36 1000000000u
37 );
38 while (Ticks <= GetPerformanceCounter ());
39 return (UINTN)Ticks;
40 }
41
42 /**
43 Stalls the CPU for at least the given number of microseconds.
44
45 Stalls the CPU for the number of microseconds specified by MicroSeconds.
46
47 @param MicroSeconds The minimum number of microseconds to delay.
48
49 @return Return value depends on implementation.
50
51 **/
52 UINTN
53 EFIAPI
54 MicroSecondDelay (
55 IN UINTN MicroSeconds
56 )
57 {
58 return DelayWorker (MicroSeconds * 1000ull);
59 }
60
61 /**
62 Stalls the CPU for at least the given number of nanoseconds.
63
64 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
65
66 @param NanoSeconds The minimum number of nanoseconds to delay.
67
68 @return Return value depends on implementation.
69
70 **/
71 UINTN
72 EFIAPI
73 NanoSecondDelay (
74 IN UINTN NanoSeconds
75 )
76 {
77 return DelayWorker (NanoSeconds);
78 }
79
80 static
81 UINTN
82 EFIAPI
83 GetApicBase (
84 VOID
85 )
86 {
87 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;
88 }
89
90 /**
91 Retrieves the current value of a 64-bit free running performance counter.
92
93 Retrieves the current value of a 64-bit free running performance counter. The
94 counter can either count up by 1 or count down by 1. If the physical
95 performance counter counts by a larger increment, then the counter values
96 must be translated. The properties of the counter can be retrieved from
97 GetPerformanceCounterProperties().
98
99 @return The current value of the free running performance counter.
100
101 **/
102 UINT64
103 EFIAPI
104 GetPerformanceCounter (
105 VOID
106 )
107 {
108 return MmioRead32 (GetApicBase () + 0x390);
109 }
110
111 /**
112 Retrieves the 64-bit frequency in Hz and the range of performance counter
113 values.
114
115 If StartValue is not NULL, then the value that the performance counter starts
116 with immediately after is it rolls over is returned in StartValue. If
117 EndValue is not NULL, then the value that the performance counter end with
118 immediately before it rolls over is returned in EndValue. The 64-bit
119 frequency of the performance counter in Hz is always returned. If StartValue
120 is less than EndValue, then the performance counter counts up. If StartValue
121 is greater than EndValue, then the performance counter counts down. For
122 example, a 64-bit free running counter that counts up would have a StartValue
123 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
124 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
125
126 @param StartValue The value the performance counter starts with when it
127 rolls over.
128 @param EndValue The value that the performance counter ends with before
129 it rolls over.
130
131 @return The frequency in Hz.
132
133 **/
134 UINT64
135 EFIAPI
136 GetPerformanceCounterProperties (
137 IN UINT64 *StartValue,
138 IN UINT64 *EndValue
139 )
140 {
141 static const UINT32 mFrequencies[] = {
142 100000000,
143 133000000,
144 200000000,
145 166000000
146 };
147 static const UINT8 mDivisor[] = {
148 0x02, 0x04, 0x08, 0x10,
149 0x02, 0x04, 0x08, 0x10,
150 0x20, 0x40, 0x80, 0x01,
151 0x20, 0x40, 0x80, 0x01
152 };
153
154 UINTN ApicBase;
155
156 ApicBase = GetApicBase ();
157
158 if (StartValue != NULL) {
159 *StartValue = MmioRead32 (ApicBase + 0x380);
160 }
161
162 if (EndValue != NULL) {
163 *EndValue = 0;
164 }
165
166 return mFrequencies[AsmMsrBitFieldRead32 (44, 16, 18)] /
167 mDivisor[MmioBitFieldRead32 (ApicBase + 0x3e0, 0, 3)];
168 }