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