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