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