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