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