2 Code for debug timer to support debug agent library implementation.
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "DebugAgent.h"
18 Initialize CPU local APIC timer.
20 @param[out] TimerFrequency Local APIC timer frequency returned.
21 @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.
23 @return 32-bit Local APIC timer init count.
26 InitializeDebugTimer (
27 OUT UINT32
*TimerFrequency
,
31 UINTN ApicTimerDivisor
;
33 UINT32 ApicTimerFrequency
;
35 InitializeLocalApicSoftwareEnable (TRUE
);
36 GetApicTimerState (&ApicTimerDivisor
, NULL
, NULL
);
37 ApicTimerFrequency
= PcdGet32(PcdFSBClock
) / (UINT32
)ApicTimerDivisor
;
39 // Cpu Local Apic timer interrupt frequency, it is set to 0.1s
41 InitialCount
= (UINT32
)DivU64x32 (
49 InitializeApicTimer (ApicTimerDivisor
, InitialCount
, TRUE
, DEBUG_TIMER_VECTOR
);
51 // Disable Debug Timer interrupt to avoid it is delivered before Debug Port
54 DisableApicTimerInterrupt ();
57 DEBUG ((EFI_D_INFO
, "Debug Timer: FSB Clock = %d\n", PcdGet32(PcdFSBClock
)));
58 DEBUG ((EFI_D_INFO
, "Debug Timer: Divisor = %d\n", ApicTimerDivisor
));
59 DEBUG ((EFI_D_INFO
, "Debug Timer: Frequency = %d\n", ApicTimerFrequency
));
60 DEBUG ((EFI_D_INFO
, "Debug Timer: InitialCount = %d\n", InitialCount
));
62 if (TimerFrequency
!= NULL
) {
63 *TimerFrequency
= ApicTimerFrequency
;
69 Enable/Disable the interrupt of debug timer and return the interrupt state
70 prior to the operation.
72 If EnableStatus is TRUE, enable the interrupt of debug timer.
73 If EnableStatus is FALSE, disable the interrupt of debug timer.
75 @param[in] EnableStatus Enable/Disable.
77 @retval TRUE Debug timer interrupt were enabled on entry to this call.
78 @retval FALSE Debug timer interrupt were disabled on entry to this call.
83 SaveAndSetDebugTimerInterrupt (
84 IN BOOLEAN EnableStatus
87 BOOLEAN OldDebugTimerInterruptState
;
89 OldDebugTimerInterruptState
= GetApicTimerInterruptState ();
91 if (OldDebugTimerInterruptState
!= EnableStatus
) {
93 EnableApicTimerInterrupt ();
95 DisableApicTimerInterrupt ();
98 // Validate the Debug Timer interrupt state
99 // This will make additional delay after Local Apic Timer interrupt state is changed.
100 // Thus, CPU could handle the potential pending interrupt of Local Apic timer.
102 while (GetApicTimerInterruptState () != EnableStatus
) {
107 return OldDebugTimerInterruptState
;
111 Check if the timer is time out.
113 @param[in] TimerCycle Timer initial count.
114 @param[in] Timer The start timer from the begin.
115 @param[in] TimeoutTicker Ticker number need time out.
117 @return TRUE Timer time out occurs.
118 @retval FALSE Timer does not time out.
122 IsDebugTimerTimeout (
123 IN UINT32 TimerCycle
,
125 IN UINT32 TimeoutTicker
131 CurrentTimer
= GetApicTimerCurrentCount ();
134 // This timer counter counts down. Check for roll over condition.
135 // If CurrentTimer is equal to Timer, it does not mean that roll over
138 if (CurrentTimer
<= Timer
) {
139 Delta
= Timer
- CurrentTimer
;
142 // Handle one roll-over.
144 Delta
= TimerCycle
- (CurrentTimer
- Timer
) + 1;
147 return (BOOLEAN
) (Delta
>= TimeoutTicker
);