]>
Commit | Line | Data |
---|---|---|
949f388f | 1 | /*++ @file\r |
2 | Emu Emulation Timer Architectural Protocol Driver as defined in DXE CIS\r | |
3 | \r | |
4 | This Timer module uses an Emu Thread to simulate the timer-tick driven\r | |
5 | timer service. In the future, the Thread creation should possibly be\r | |
6 | abstracted by the CPU architectural protocol\r | |
7 | \r | |
daea123d | 8 | Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r |
949f388f | 9 | Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.\r |
e3ba31da | 10 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
949f388f | 11 | \r |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "PiDxe.h"\r | |
16 | #include <Protocol/Timer.h>\r | |
17 | #include <Protocol/Cpu.h>\r | |
18 | #include "Timer.h"\r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/UefiLib.h>\r | |
22 | #include <Library/UefiDriverEntryPoint.h>\r | |
23 | #include <Library/MemoryAllocationLib.h>\r | |
24 | #include <Library/UefiBootServicesTableLib.h>\r | |
25 | #include <Library/EmuThunkLib.h>\r | |
26 | \r | |
27 | //\r | |
28 | // Pointer to the CPU Architectural Protocol instance\r | |
29 | //\r | |
30 | EFI_CPU_ARCH_PROTOCOL *mCpu;\r | |
31 | \r | |
32 | //\r | |
33 | // The Timer Architectural Protocol that this driver produces\r | |
34 | //\r | |
35 | EFI_TIMER_ARCH_PROTOCOL mTimer = {\r | |
36 | EmuTimerDriverRegisterHandler,\r | |
37 | EmuTimerDriverSetTimerPeriod,\r | |
38 | EmuTimerDriverGetTimerPeriod,\r | |
39 | EmuTimerDriverGenerateSoftInterrupt\r | |
40 | };\r | |
41 | \r | |
42 | //\r | |
43 | // The notification function to call on every timer interrupt\r | |
44 | //\r | |
45 | EFI_TIMER_NOTIFY mTimerNotifyFunction = NULL;\r | |
46 | \r | |
47 | //\r | |
48 | // The current period of the timer interrupt\r | |
49 | //\r | |
50 | UINT64 mTimerPeriodMs;\r | |
51 | \r | |
52 | \r | |
53 | VOID\r | |
54 | EFIAPI\r | |
55 | TimerCallback (UINT64 DeltaMs)\r | |
56 | {\r | |
57 | EFI_TPL OriginalTPL;\r | |
58 | EFI_TIMER_NOTIFY CallbackFunction;\r | |
59 | \r | |
60 | \r | |
61 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
62 | \r | |
63 | if (OriginalTPL < TPL_HIGH_LEVEL) {\r | |
64 | CallbackFunction = mTimerNotifyFunction;\r | |
65 | \r | |
66 | //\r | |
67 | // Only invoke the callback function if a Non-NULL handler has been\r | |
68 | // registered. Assume all other handlers are legal.\r | |
69 | //\r | |
70 | if (CallbackFunction != NULL) {\r | |
e148512e | 71 | CallbackFunction (MultU64x32 (DeltaMs, 10000));\r |
949f388f | 72 | }\r |
73 | }\r | |
74 | \r | |
75 | gBS->RestoreTPL (OriginalTPL);\r | |
76 | \r | |
77 | }\r | |
78 | \r | |
79 | EFI_STATUS\r | |
80 | EFIAPI\r | |
81 | EmuTimerDriverRegisterHandler (\r | |
82 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
83 | IN EFI_TIMER_NOTIFY NotifyFunction\r | |
84 | )\r | |
85 | /*++\r | |
86 | \r | |
87 | Routine Description:\r | |
88 | \r | |
89 | This function registers the handler NotifyFunction so it is called every time\r | |
90 | the timer interrupt fires. It also passes the amount of time since the last\r | |
91 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r | |
92 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r | |
93 | returned. If the CPU does not support registering a timer interrupt handler,\r | |
94 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r | |
95 | when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r | |
96 | If an attempt is made to unregister a handler when a handler is not registered,\r | |
97 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r | |
98 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r | |
99 | is returned.\r | |
100 | \r | |
101 | Arguments:\r | |
102 | \r | |
103 | This - The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
104 | \r | |
105 | NotifyFunction - The function to call when a timer interrupt fires. This\r | |
106 | function executes at TPL_HIGH_LEVEL. The DXE Core will\r | |
107 | register a handler for the timer interrupt, so it can know\r | |
108 | how much time has passed. This information is used to\r | |
109 | signal timer based events. NULL will unregister the handler.\r | |
110 | \r | |
111 | Returns:\r | |
112 | \r | |
113 | EFI_SUCCESS - The timer handler was registered.\r | |
114 | \r | |
115 | EFI_UNSUPPORTED - The platform does not support timer interrupts.\r | |
116 | \r | |
117 | EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already\r | |
118 | registered.\r | |
119 | \r | |
120 | EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not\r | |
121 | previously registered.\r | |
122 | \r | |
123 | EFI_DEVICE_ERROR - The timer handler could not be registered.\r | |
124 | \r | |
125 | **/\r | |
126 | {\r | |
127 | //\r | |
128 | // Check for invalid parameters\r | |
129 | //\r | |
130 | if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r | |
131 | return EFI_INVALID_PARAMETER;\r | |
132 | }\r | |
133 | \r | |
134 | if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r | |
135 | return EFI_ALREADY_STARTED;\r | |
136 | }\r | |
137 | \r | |
138 | if (NotifyFunction == NULL) {\r | |
139 | /* Disable timer. */\r | |
140 | gEmuThunk->SetTimer (0, TimerCallback);\r | |
141 | } else if (mTimerNotifyFunction == NULL) {\r | |
142 | /* Enable Timer. */\r | |
143 | gEmuThunk->SetTimer (mTimerPeriodMs, TimerCallback);\r | |
144 | }\r | |
145 | mTimerNotifyFunction = NotifyFunction;\r | |
146 | \r | |
147 | return EFI_SUCCESS;\r | |
148 | }\r | |
149 | \r | |
150 | EFI_STATUS\r | |
151 | EFIAPI\r | |
152 | EmuTimerDriverSetTimerPeriod (\r | |
153 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
154 | IN UINT64 TimerPeriod\r | |
155 | )\r | |
156 | /*++\r | |
157 | \r | |
158 | Routine Description:\r | |
159 | \r | |
160 | This function adjusts the period of timer interrupts to the value specified\r | |
161 | by TimerPeriod. If the timer period is updated, then the selected timer\r | |
162 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r | |
163 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r | |
164 | If an error occurs while attempting to update the timer period, then the\r | |
165 | timer hardware will be put back in its state prior to this call, and\r | |
166 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r | |
167 | is disabled. This is not the same as disabling the CPU's interrupts.\r | |
168 | Instead, it must either turn off the timer hardware, or it must adjust the\r | |
169 | interrupt controller so that a CPU interrupt is not generated when the timer\r | |
170 | interrupt fires.\r | |
171 | \r | |
172 | Arguments:\r | |
173 | \r | |
174 | This - The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
175 | \r | |
176 | TimerPeriod - The rate to program the timer interrupt in 100 nS units. If\r | |
177 | the timer hardware is not programmable, then EFI_UNSUPPORTED is\r | |
178 | returned. If the timer is programmable, then the timer period\r | |
179 | will be rounded up to the nearest timer period that is supported\r | |
180 | by the timer hardware. If TimerPeriod is set to 0, then the\r | |
181 | timer interrupts will be disabled.\r | |
182 | \r | |
183 | Returns:\r | |
184 | \r | |
185 | EFI_SUCCESS - The timer period was changed.\r | |
186 | \r | |
187 | EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.\r | |
188 | \r | |
189 | EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.\r | |
190 | \r | |
191 | **/\r | |
192 | {\r | |
193 | \r | |
194 | //\r | |
195 | // If TimerPeriod is 0, then the timer thread should be canceled\r | |
196 | // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread\r | |
197 | //\r | |
198 | if (TimerPeriod == 0\r | |
199 | || ((TimerPeriod > TIMER_MINIMUM_VALUE)\r | |
200 | && (TimerPeriod < TIMER_MAXIMUM_VALUE))) {\r | |
201 | mTimerPeriodMs = DivU64x32 (TimerPeriod + 5000, 10000);\r | |
202 | \r | |
203 | gEmuThunk->SetTimer (mTimerPeriodMs, TimerCallback);\r | |
204 | }\r | |
205 | \r | |
206 | return EFI_SUCCESS;\r | |
207 | }\r | |
208 | \r | |
209 | EFI_STATUS\r | |
210 | EFIAPI\r | |
211 | EmuTimerDriverGetTimerPeriod (\r | |
212 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
213 | OUT UINT64 *TimerPeriod\r | |
214 | )\r | |
215 | /*++\r | |
216 | \r | |
217 | Routine Description:\r | |
218 | \r | |
219 | This function retrieves the period of timer interrupts in 100 ns units,\r | |
220 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r | |
221 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r | |
222 | returned, then the timer is currently disabled.\r | |
223 | \r | |
224 | Arguments:\r | |
225 | \r | |
226 | This - The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
227 | \r | |
228 | TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If\r | |
229 | 0 is returned, then the timer is currently disabled.\r | |
230 | \r | |
231 | Returns:\r | |
232 | \r | |
233 | EFI_SUCCESS - The timer period was returned in TimerPeriod.\r | |
234 | \r | |
235 | EFI_INVALID_PARAMETER - TimerPeriod is NULL.\r | |
236 | \r | |
237 | **/\r | |
238 | {\r | |
239 | if (TimerPeriod == NULL) {\r | |
240 | return EFI_INVALID_PARAMETER;\r | |
241 | }\r | |
242 | \r | |
e148512e | 243 | *TimerPeriod = MultU64x32 (mTimerPeriodMs, 10000);\r |
949f388f | 244 | \r |
245 | return EFI_SUCCESS;\r | |
246 | }\r | |
247 | \r | |
248 | EFI_STATUS\r | |
249 | EFIAPI\r | |
250 | EmuTimerDriverGenerateSoftInterrupt (\r | |
251 | IN EFI_TIMER_ARCH_PROTOCOL *This\r | |
252 | )\r | |
253 | /*++\r | |
254 | \r | |
255 | Routine Description:\r | |
256 | \r | |
257 | This function generates a soft timer interrupt. If the platform does not support soft\r | |
258 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r | |
259 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r | |
260 | service, then a soft timer interrupt will be generated. If the timer interrupt is\r | |
261 | enabled when this service is called, then the registered handler will be invoked. The\r | |
262 | registered handler should not be able to distinguish a hardware-generated timer\r | |
263 | interrupt from a software-generated timer interrupt.\r | |
264 | \r | |
265 | Arguments:\r | |
266 | \r | |
267 | This - The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
268 | \r | |
269 | Returns:\r | |
270 | \r | |
271 | EFI_SUCCESS - The soft timer interrupt was generated.\r | |
272 | \r | |
daea123d | 273 | EFI_UNSUPPORTED - The platform does not support the generation of soft timer interrupts.\r |
949f388f | 274 | \r |
275 | **/\r | |
276 | {\r | |
277 | return EFI_UNSUPPORTED;\r | |
278 | }\r | |
279 | \r | |
280 | EFI_STATUS\r | |
281 | EFIAPI\r | |
282 | EmuTimerDriverInitialize (\r | |
283 | IN EFI_HANDLE ImageHandle,\r | |
284 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
285 | )\r | |
286 | /*++\r | |
287 | \r | |
288 | Routine Description:\r | |
289 | \r | |
290 | Initialize the Timer Architectural Protocol driver\r | |
291 | \r | |
292 | Arguments:\r | |
293 | \r | |
294 | ImageHandle - ImageHandle of the loaded driver\r | |
295 | \r | |
296 | SystemTable - Pointer to the System Table\r | |
297 | \r | |
298 | Returns:\r | |
299 | \r | |
300 | EFI_SUCCESS - Timer Architectural Protocol created\r | |
301 | \r | |
302 | EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.\r | |
303 | \r | |
304 | EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.\r | |
305 | \r | |
306 | **/\r | |
307 | {\r | |
308 | EFI_STATUS Status;\r | |
309 | EFI_HANDLE Handle;\r | |
310 | \r | |
311 | //\r | |
312 | // Make sure the Timer Architectural Protocol is not already installed in the system\r | |
313 | //\r | |
314 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r | |
315 | \r | |
316 | //\r | |
317 | // Get the CPU Architectural Protocol instance\r | |
318 | //\r | |
319 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (void *)&mCpu);\r | |
320 | ASSERT_EFI_ERROR (Status);\r | |
321 | \r | |
1ef41207 | 322 | //\r |
323 | // Start the timer thread at the default timer period\r | |
324 | //\r | |
325 | Status = mTimer.SetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);\r | |
326 | if (EFI_ERROR (Status)) {\r | |
327 | return Status;\r | |
328 | }\r | |
329 | \r | |
949f388f | 330 | //\r |
331 | // Install the Timer Architectural Protocol onto a new handle\r | |
332 | //\r | |
333 | Handle = NULL;\r | |
334 | Status = gBS->InstallProtocolInterface (\r | |
335 | &Handle,\r | |
336 | &gEfiTimerArchProtocolGuid,\r | |
337 | EFI_NATIVE_INTERFACE,\r | |
338 | &mTimer\r | |
339 | );\r | |
340 | if (EFI_ERROR (Status)) {\r | |
341 | return Status;\r | |
342 | }\r | |
343 | \r | |
949f388f | 344 | \r |
345 | return EFI_SUCCESS;\r | |
346 | }\r |