]>
Commit | Line | Data |
---|---|---|
1ef41207 | 1 | /** @file\r |
2 | A non-functional instance of the Timer Library.\r | |
3 | \r | |
4 | Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include <PiPei.h>\r | |
16 | #include <Library/BaseLib.h>\r | |
17 | #include <Library/TimerLib.h>\r | |
18 | #include <Library/DebugLib.h>\r | |
19 | #include <Library/EmuThunkLib.h>\r | |
20 | #include <Library/UefiBootServicesTableLib.h>\r | |
21 | #include <Library/UefiLib.h>\r | |
22 | \r | |
23 | #include <Protocol/Timer.h>\r | |
24 | \r | |
25 | \r | |
26 | STATIC UINT64 gTimerPeriod = 0;\r | |
27 | STATIC EFI_TIMER_ARCH_PROTOCOL *gTimerAp = NULL;\r | |
28 | STATIC EFI_EVENT gTimerEvent = NULL;\r | |
29 | STATIC VOID *gRegistration = NULL;\r | |
30 | \r | |
31 | VOID\r | |
32 | EFIAPI\r | |
33 | RegisterTimerArchProtocol (\r | |
34 | IN EFI_EVENT Event,\r | |
35 | IN VOID *Context\r | |
36 | )\r | |
37 | {\r | |
38 | EFI_STATUS Status;\r | |
d18d8a1d | 39 | \r |
1ef41207 | 40 | Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&gTimerAp);\r |
d18d8a1d | 41 | if (!EFI_ERROR (Status)) {\r |
1ef41207 | 42 | Status = gTimerAp->GetTimerPeriod (gTimerAp, &gTimerPeriod);\r |
43 | ASSERT_EFI_ERROR (Status);\r | |
44 | \r | |
45 | // Convert to Nanoseconds.\r | |
46 | gTimerPeriod = MultU64x32 (gTimerPeriod, 100);\r | |
d18d8a1d | 47 | \r |
1ef41207 | 48 | if (gTimerEvent == NULL) {\r |
49 | Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, (VOID **)&gTimerEvent);\r | |
50 | ASSERT_EFI_ERROR (Status);\r | |
51 | }\r | |
52 | }\r | |
53 | }\r | |
54 | \r | |
55 | \r | |
56 | \r | |
57 | /**\r | |
58 | Stalls the CPU for at least the given number of microseconds.\r | |
59 | \r | |
60 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
61 | \r | |
62 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
63 | \r | |
64 | @return The value of MicroSeconds inputted.\r | |
65 | \r | |
66 | **/\r | |
67 | UINTN\r | |
68 | EFIAPI\r | |
69 | MicroSecondDelay (\r | |
70 | IN UINTN MicroSeconds\r | |
71 | )\r | |
72 | {\r | |
73 | return NanoSecondDelay (MicroSeconds * 1000);\r | |
74 | }\r | |
75 | \r | |
76 | \r | |
77 | /**\r | |
78 | Stalls the CPU for at least the given number of nanoseconds.\r | |
79 | \r | |
80 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
81 | \r | |
82 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
83 | \r | |
84 | @return The value of NanoSeconds inputted.\r | |
85 | \r | |
86 | **/\r | |
87 | UINTN\r | |
88 | EFIAPI\r | |
89 | NanoSecondDelay (\r | |
90 | IN UINTN NanoSeconds\r | |
91 | )\r | |
92 | {\r | |
93 | EFI_STATUS Status;\r | |
94 | UINT64 HundredNanoseconds;\r | |
95 | UINTN Index;\r | |
d18d8a1d | 96 | \r |
97 | if ((gTimerPeriod != 0) &&\r | |
98 | ((UINT64)NanoSeconds > gTimerPeriod) &&\r | |
1ef41207 | 99 | (EfiGetCurrentTpl () == TPL_APPLICATION)) {\r |
100 | //\r | |
101 | // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core\r | |
102 | //\r | |
d18d8a1d | 103 | \r |
1ef41207 | 104 | HundredNanoseconds = DivU64x32 (NanoSeconds, 100);\r |
105 | Status = gBS->SetTimer (gTimerEvent, TimerRelative, HundredNanoseconds);\r | |
106 | ASSERT_EFI_ERROR (Status);\r | |
107 | \r | |
108 | Status = gBS->WaitForEvent (sizeof (gTimerEvent)/sizeof (EFI_EVENT), &gTimerEvent, &Index);\r | |
109 | ASSERT_EFI_ERROR (Status);\r | |
d18d8a1d | 110 | \r |
1ef41207 | 111 | } else {\r |
112 | gEmuThunk->Sleep (NanoSeconds);\r | |
113 | }\r | |
114 | return NanoSeconds;\r | |
115 | }\r | |
116 | \r | |
117 | \r | |
118 | /**\r | |
119 | Retrieves the current value of a 64-bit free running performance counter.\r | |
120 | \r | |
121 | The counter can either count up by 1 or count down by 1. If the physical\r | |
122 | performance counter counts by a larger increment, then the counter values\r | |
123 | must be translated. The properties of the counter can be retrieved from\r | |
124 | GetPerformanceCounterProperties().\r | |
125 | \r | |
126 | @return The current value of the free running performance counter.\r | |
127 | \r | |
128 | **/\r | |
129 | UINT64\r | |
130 | EFIAPI\r | |
131 | GetPerformanceCounter (\r | |
132 | VOID\r | |
133 | )\r | |
134 | {\r | |
135 | return gEmuThunk->QueryPerformanceCounter ();\r | |
136 | }\r | |
137 | \r | |
138 | /**\r | |
139 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
140 | values.\r | |
141 | \r | |
142 | If StartValue is not NULL, then the value that the performance counter starts\r | |
143 | with immediately after is it rolls over is returned in StartValue. If\r | |
144 | EndValue is not NULL, then the value that the performance counter end with\r | |
145 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
146 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
147 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
148 | is greater than EndValue, then the performance counter counts down. For\r | |
149 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
150 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
151 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
152 | \r | |
153 | @param StartValue The value the performance counter starts with when it\r | |
154 | rolls over.\r | |
155 | @param EndValue The value that the performance counter ends with before\r | |
156 | it rolls over.\r | |
157 | \r | |
158 | @return The frequency in Hz.\r | |
159 | \r | |
160 | **/\r | |
161 | UINT64\r | |
162 | EFIAPI\r | |
163 | GetPerformanceCounterProperties (\r | |
164 | OUT UINT64 *StartValue, OPTIONAL\r | |
165 | OUT UINT64 *EndValue OPTIONAL\r | |
166 | )\r | |
167 | {\r | |
168 | \r | |
169 | if (StartValue != NULL) {\r | |
170 | *StartValue = 0ULL;\r | |
171 | }\r | |
172 | if (EndValue != NULL) {\r | |
173 | *EndValue = (UINT64)-1LL;\r | |
174 | }\r | |
d18d8a1d | 175 | \r |
1ef41207 | 176 | return gEmuThunk->QueryPerformanceFrequency ();\r |
177 | }\r | |
178 | \r | |
179 | \r | |
180 | /**\r | |
181 | Register for the Timer AP protocol.\r | |
182 | \r | |
183 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
184 | @param SystemTable A pointer to the EFI System Table.\r | |
185 | \r | |
186 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
187 | \r | |
188 | **/\r | |
189 | EFI_STATUS\r | |
190 | EFIAPI\r | |
191 | DxeTimerLibConstructor (\r | |
192 | IN EFI_HANDLE ImageHandle,\r | |
193 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
194 | )\r | |
195 | {\r | |
196 | EfiCreateProtocolNotifyEvent (\r | |
197 | &gEfiTimerArchProtocolGuid,\r | |
198 | TPL_CALLBACK,\r | |
199 | RegisterTimerArchProtocol,\r | |
200 | NULL,\r | |
201 | &gRegistration\r | |
202 | );\r | |
203 | \r | |
204 | return EFI_SUCCESS;\r | |
205 | }\r | |
206 | \r |