]> git.proxmox.com Git - mirror_edk2.git/blob - EdkNt32Pkg/Dxe/WinNtThunk/Chipset/Timer/Timer.c
Initial import.
[mirror_edk2.git] / EdkNt32Pkg / Dxe / WinNtThunk / Chipset / Timer / Timer.c
1 /*++
2
3 Copyright (c) 2006, 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 NT Emulation Timer Architectural Protocol Driver as defined in DXE CIS
19
20 This Timer module uses an NT 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 WinNtTimerDriverRegisterHandler,
38 WinNtTimerDriverSetTimerPeriod,
39 WinNtTimerDriverGetTimerPeriod,
40 WinNtTimerDriverGenerateSoftInterrupt
41 };
42
43 //
44 // Define a global that we can use to shut down the NT timer thread when
45 // the timer is canceled.
46 //
47 BOOLEAN mCancelTimerThread = FALSE;
48
49 //
50 // The notification function to call on every timer interrupt
51 //
52 EFI_TIMER_NOTIFY mTimerNotifyFunction = NULL;
53
54 //
55 // The current period of the timer interrupt
56 //
57 UINT64 mTimerPeriod;
58
59 //
60 // The thread handle for this driver
61 //
62 HANDLE mNtMainThreadHandle;
63
64 //
65 // The timer value from the last timer interrupt
66 //
67 UINT32 mNtLastTick;
68
69 //
70 // Critical section used to update varibles shared between the main thread and
71 // the timer interrupt thread.
72 //
73 CRITICAL_SECTION mNtCriticalSection;
74
75 //
76 // Worker Functions
77 //
78 UINT mMMTimerThreadID = 0;
79
80 VOID
81 CALLBACK
82 MMTimerThread (
83 UINT wTimerID,
84 UINT msg,
85 DWORD dwUser,
86 DWORD dw1,
87 DWORD dw2
88 )
89 /*++
90
91 Routine Description:
92
93 TODO: Add function description
94
95 Arguments:
96
97 wTimerID - TODO: add argument description
98 msg - TODO: add argument description
99 dwUser - TODO: add argument description
100 dw1 - TODO: add argument description
101 dw2 - TODO: add argument description
102
103 Returns:
104
105 TODO: add return values
106
107 --*/
108 {
109 EFI_TPL OriginalTPL;
110 UINT32 CurrentTick;
111 UINT32 Delta;
112 EFI_TIMER_NOTIFY CallbackFunction;
113 BOOLEAN InterruptState;
114
115 if (!mCancelTimerThread) {
116
117 //
118 // Suspend the main thread until we are done
119 //
120
121 gWinNt->SuspendThread (mNtMainThreadHandle);
122
123 //
124 // If the timer thread is being canceled, then bail immediately.
125 // We check again here because there's a small window of time from when
126 // this thread was kicked off and when we suspended the main thread above.
127 //
128 if (mCancelTimerThread) {
129 gWinNt->ResumeThread (mNtMainThreadHandle);
130 gWinNt->timeKillEvent (wTimerID);
131 mMMTimerThreadID = 0;
132 return ;
133 }
134
135 mCpu->GetInterruptState (mCpu, &InterruptState);
136 while (!InterruptState) {
137 //
138 // Resume the main thread
139 //
140 gWinNt->ResumeThread (mNtMainThreadHandle);
141
142 //
143 // Wait for interrupts to be enabled.
144 //
145 mCpu->GetInterruptState (mCpu, &InterruptState);
146 while (!InterruptState) {
147 gWinNt->Sleep (0);
148 mCpu->GetInterruptState (mCpu, &InterruptState);
149 }
150
151 //
152 // Suspend the main thread until we are done
153 //
154 gWinNt->SuspendThread (mNtMainThreadHandle);
155 mCpu->GetInterruptState (mCpu, &InterruptState);
156 }
157
158 //
159 // Get the current system tick
160 //
161 CurrentTick = gWinNt->GetTickCount ();
162 Delta = CurrentTick - mNtLastTick;
163 mNtLastTick = CurrentTick;
164
165 //
166 // If delay was more then 1 second, ignore it (probably debugging case)
167 //
168 if (Delta < 1000) {
169
170 OriginalTPL = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
171
172 //
173 // Inform the firmware of an "timer interrupt". The time
174 // expired since the last call is 10,000 times the number
175 // of ms. (or 100ns units)
176 //
177 gWinNt->EnterCriticalSection (&mNtCriticalSection);
178 CallbackFunction = mTimerNotifyFunction;
179 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
180
181 //
182 // Only invoke the callback function if a Non-NULL handler has been
183 // registered. Assume all other handlers are legal.
184 //
185 if (CallbackFunction != NULL) {
186 CallbackFunction ((UINT64) (Delta * 10000));
187 }
188
189 gBS->RestoreTPL (OriginalTPL);
190
191 }
192
193 //
194 // Resume the main thread
195 //
196 gWinNt->ResumeThread (mNtMainThreadHandle);
197 } else {
198 gWinNt->timeKillEvent (wTimerID);
199 mMMTimerThreadID = 0;
200 }
201
202 }
203
204 UINT
205 CreateNtTimer (
206 VOID
207 )
208 /*++
209
210 Routine Description:
211
212 It is used to emulate a platform
213 timer-driver interrupt handler.
214
215 Returns:
216
217 Timer ID
218
219 --*/
220 // TODO: function comment is missing 'Arguments:'
221 {
222 UINT32 SleepCount;
223
224 //
225 // Set our thread priority higher than the "main" thread.
226 //
227 gWinNt->SetThreadPriority (
228 gWinNt->GetCurrentThread (),
229 THREAD_PRIORITY_HIGHEST
230 );
231
232 //
233 // Calc the appropriate interval
234 //
235 gWinNt->EnterCriticalSection (&mNtCriticalSection);
236 SleepCount = (UINT32) (mTimerPeriod + 5000) / 10000;
237 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
238
239 return gWinNt->timeSetEvent (
240 SleepCount,
241 0,
242 MMTimerThread,
243 (DWORD_PTR) NULL,
244 TIME_PERIODIC | TIME_KILL_SYNCHRONOUS | TIME_CALLBACK_FUNCTION
245 );
246
247 }
248
249 EFI_STATUS
250 EFIAPI
251 WinNtTimerDriverRegisterHandler (
252 IN EFI_TIMER_ARCH_PROTOCOL *This,
253 IN EFI_TIMER_NOTIFY NotifyFunction
254 )
255 /*++
256
257 Routine Description:
258
259 This function registers the handler NotifyFunction so it is called every time
260 the timer interrupt fires. It also passes the amount of time since the last
261 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
262 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
263 returned. If the CPU does not support registering a timer interrupt handler,
264 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
265 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
266 If an attempt is made to unregister a handler when a handler is not registered,
267 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
268 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
269 is returned.
270
271 Arguments:
272
273 This - The EFI_TIMER_ARCH_PROTOCOL instance.
274
275 NotifyFunction - The function to call when a timer interrupt fires. This
276 function executes at TPL_HIGH_LEVEL. The DXE Core will
277 register a handler for the timer interrupt, so it can know
278 how much time has passed. This information is used to
279 signal timer based events. NULL will unregister the handler.
280
281 Returns:
282
283 EFI_SUCCESS - The timer handler was registered.
284
285 EFI_UNSUPPORTED - The platform does not support timer interrupts.
286
287 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
288 registered.
289
290 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
291 previously registered.
292
293 EFI_DEVICE_ERROR - The timer handler could not be registered.
294
295 --*/
296 {
297 //
298 // Check for invalid parameters
299 //
300 if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {
301 return EFI_INVALID_PARAMETER;
302 }
303
304 if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {
305 return EFI_ALREADY_STARTED;
306 }
307
308 //
309 // Use Critical Section to update the notification function that is
310 // used from the timer interrupt thread.
311 //
312 gWinNt->EnterCriticalSection (&mNtCriticalSection);
313
314 mTimerNotifyFunction = NotifyFunction;
315
316 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
317
318 return EFI_SUCCESS;
319 }
320
321 EFI_STATUS
322 EFIAPI
323 WinNtTimerDriverSetTimerPeriod (
324 IN EFI_TIMER_ARCH_PROTOCOL *This,
325 IN UINT64 TimerPeriod
326 )
327 /*++
328
329 Routine Description:
330
331 This function adjusts the period of timer interrupts to the value specified
332 by TimerPeriod. If the timer period is updated, then the selected timer
333 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
334 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
335 If an error occurs while attempting to update the timer period, then the
336 timer hardware will be put back in its state prior to this call, and
337 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
338 is disabled. This is not the same as disabling the CPU's interrupts.
339 Instead, it must either turn off the timer hardware, or it must adjust the
340 interrupt controller so that a CPU interrupt is not generated when the timer
341 interrupt fires.
342
343 Arguments:
344
345 This - The EFI_TIMER_ARCH_PROTOCOL instance.
346
347 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
348 the timer hardware is not programmable, then EFI_UNSUPPORTED is
349 returned. If the timer is programmable, then the timer period
350 will be rounded up to the nearest timer period that is supported
351 by the timer hardware. If TimerPeriod is set to 0, then the
352 timer interrupts will be disabled.
353
354 Returns:
355
356 EFI_SUCCESS - The timer period was changed.
357
358 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
359
360 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
361
362 --*/
363 {
364
365 //
366 // If TimerPeriod is 0, then the timer thread should be canceled
367 //
368 if (TimerPeriod == 0) {
369 //
370 // Cancel the timer thread
371 //
372 gWinNt->EnterCriticalSection (&mNtCriticalSection);
373
374 mCancelTimerThread = TRUE;
375
376 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
377
378 //
379 // Wait for the timer thread to exit
380 //
381
382 if (mMMTimerThreadID) {
383 gWinNt->timeKillEvent (mMMTimerThreadID);
384 }
385
386 mMMTimerThreadID = 0;
387
388 //
389 // Update the timer period
390 //
391 gWinNt->EnterCriticalSection (&mNtCriticalSection);
392
393 mTimerPeriod = TimerPeriod;
394
395 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
396
397 //
398 // NULL out the thread handle so it will be re-created if the timer is enabled again
399 //
400
401 } else if ((TimerPeriod > TIMER_MINIMUM_VALUE) && (TimerPeriod < TIMER_MAXIMUM_VALUE)) {
402 //
403 // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
404 //
405 gWinNt->EnterCriticalSection (&mNtCriticalSection);
406
407 mTimerPeriod = TimerPeriod;
408
409 mCancelTimerThread = FALSE;
410
411 gWinNt->LeaveCriticalSection (&mNtCriticalSection);
412
413 //
414 // Get the starting tick location if we are just starting the timer thread
415 //
416 mNtLastTick = gWinNt->GetTickCount ();
417
418 if (mMMTimerThreadID) {
419 gWinNt->timeKillEvent (mMMTimerThreadID);
420 }
421
422 mMMTimerThreadID = 0;
423
424 mMMTimerThreadID = CreateNtTimer ();
425
426 }
427
428 return EFI_SUCCESS;
429 }
430
431 EFI_STATUS
432 EFIAPI
433 WinNtTimerDriverGetTimerPeriod (
434 IN EFI_TIMER_ARCH_PROTOCOL *This,
435 OUT UINT64 *TimerPeriod
436 )
437 /*++
438
439 Routine Description:
440
441 This function retrieves the period of timer interrupts in 100 ns units,
442 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
443 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
444 returned, then the timer is currently disabled.
445
446 Arguments:
447
448 This - The EFI_TIMER_ARCH_PROTOCOL instance.
449
450 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
451 0 is returned, then the timer is currently disabled.
452
453 Returns:
454
455 EFI_SUCCESS - The timer period was returned in TimerPeriod.
456
457 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
458
459 --*/
460 {
461 if (TimerPeriod == NULL) {
462 return EFI_INVALID_PARAMETER;
463 }
464
465 *TimerPeriod = mTimerPeriod;
466
467 return EFI_SUCCESS;
468 }
469
470 EFI_STATUS
471 EFIAPI
472 WinNtTimerDriverGenerateSoftInterrupt (
473 IN EFI_TIMER_ARCH_PROTOCOL *This
474 )
475 /*++
476
477 Routine Description:
478
479 This function generates a soft timer interrupt. If the platform does not support soft
480 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
481 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
482 service, then a soft timer interrupt will be generated. If the timer interrupt is
483 enabled when this service is called, then the registered handler will be invoked. The
484 registered handler should not be able to distinguish a hardware-generated timer
485 interrupt from a software-generated timer interrupt.
486
487 Arguments:
488
489 This - The EFI_TIMER_ARCH_PROTOCOL instance.
490
491 Returns:
492
493 EFI_SUCCESS - The soft timer interrupt was generated.
494
495 EFI_UNSUPPORTEDT - The platform does not support the generation of soft timer interrupts.
496
497 --*/
498 {
499 return EFI_UNSUPPORTED;
500 }
501
502
503 EFI_STATUS
504 EFIAPI
505 WinNtTimerDriverInitialize (
506 IN EFI_HANDLE ImageHandle,
507 IN EFI_SYSTEM_TABLE *SystemTable
508 )
509 /*++
510
511 Routine Description:
512
513 Initialize the Timer Architectural Protocol driver
514
515 Arguments:
516
517 ImageHandle - ImageHandle of the loaded driver
518
519 SystemTable - Pointer to the System Table
520
521 Returns:
522
523 EFI_SUCCESS - Timer Architectural Protocol created
524
525 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
526
527 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
528
529 --*/
530 {
531 EFI_STATUS Status;
532 UINTN Result;
533 EFI_HANDLE Handle;
534
535 //
536 // Make sure the Timer Architectural Protocol is not already installed in the system
537 //
538 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);
539
540 //
541 // Get the CPU Architectural Protocol instance
542 //
543 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, &mCpu);
544 ASSERT_EFI_ERROR (Status);
545
546 //
547 // Get our handle so the timer tick thread can suspend
548 //
549 Result = gWinNt->DuplicateHandle (
550 gWinNt->GetCurrentProcess (),
551 gWinNt->GetCurrentThread (),
552 gWinNt->GetCurrentProcess (),
553 &mNtMainThreadHandle,
554 0,
555 FALSE,
556 DUPLICATE_SAME_ACCESS
557 );
558 if (Result == 0) {
559 return EFI_DEVICE_ERROR;
560 }
561
562 //
563 // Initialize Critical Section used to update variables shared between the main
564 // thread and the timer interrupt thread.
565 //
566 gWinNt->InitializeCriticalSection (&mNtCriticalSection);
567
568 //
569 // Start the timer thread at the default timer period
570 //
571 Status = mTimer.SetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);
572 if (EFI_ERROR (Status)) {
573 gWinNt->DeleteCriticalSection (&mNtCriticalSection);
574 return Status;
575 }
576
577 //
578 // Install the Timer Architectural Protocol onto a new handle
579 //
580 Handle = NULL;
581 Status = gBS->InstallProtocolInterface (
582 &Handle,
583 &gEfiTimerArchProtocolGuid,
584 EFI_NATIVE_INTERFACE,
585 &mTimer
586 );
587 if (EFI_ERROR (Status)) {
588 //
589 // Cancel the timer
590 //
591 mTimer.SetTimerPeriod (&mTimer, 0);
592 gWinNt->DeleteCriticalSection (&mNtCriticalSection);
593 return Status;
594 }
595
596 return EFI_SUCCESS;
597 }