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