2 Code for debug timer to support debug agent library implementation.
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DebugAgent.h"
12 Initialize CPU local APIC timer.
14 @param[out] TimerFrequency Local APIC timer frequency returned.
15 @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.
17 @return 32-bit Local APIC timer init count.
20 InitializeDebugTimer (
21 OUT UINT32
*TimerFrequency
,
25 UINTN ApicTimerDivisor
;
27 UINT32 ApicTimerFrequency
;
29 InitializeLocalApicSoftwareEnable (TRUE
);
30 GetApicTimerState (&ApicTimerDivisor
, NULL
, NULL
);
31 ApicTimerFrequency
= PcdGet32(PcdFSBClock
) / (UINT32
)ApicTimerDivisor
;
33 // Cpu Local Apic timer interrupt frequency, it is set to 0.1s
35 InitialCount
= (UINT32
)DivU64x32 (
43 InitializeApicTimer (ApicTimerDivisor
, InitialCount
, TRUE
, DEBUG_TIMER_VECTOR
);
45 // Disable Debug Timer interrupt to avoid it is delivered before Debug Port
48 DisableApicTimerInterrupt ();
51 DEBUG ((DEBUG_INFO
, "Debug Timer: FSB Clock = %d\n", PcdGet32(PcdFSBClock
)));
52 DEBUG ((DEBUG_INFO
, "Debug Timer: Divisor = %d\n", ApicTimerDivisor
));
53 DEBUG ((DEBUG_INFO
, "Debug Timer: Frequency = %d\n", ApicTimerFrequency
));
54 DEBUG ((DEBUG_INFO
, "Debug Timer: InitialCount = %d\n", InitialCount
));
56 if (TimerFrequency
!= NULL
) {
57 *TimerFrequency
= ApicTimerFrequency
;
63 Enable/Disable the interrupt of debug timer and return the interrupt state
64 prior to the operation.
66 If EnableStatus is TRUE, enable the interrupt of debug timer.
67 If EnableStatus is FALSE, disable the interrupt of debug timer.
69 @param[in] EnableStatus Enable/Disable.
71 @retval TRUE Debug timer interrupt were enabled on entry to this call.
72 @retval FALSE Debug timer interrupt were disabled on entry to this call.
77 SaveAndSetDebugTimerInterrupt (
78 IN BOOLEAN EnableStatus
81 BOOLEAN OldDebugTimerInterruptState
;
83 OldDebugTimerInterruptState
= GetApicTimerInterruptState ();
85 if (OldDebugTimerInterruptState
!= EnableStatus
) {
87 EnableApicTimerInterrupt ();
89 DisableApicTimerInterrupt ();
92 // Validate the Debug Timer interrupt state
93 // This will make additional delay after Local Apic Timer interrupt state is changed.
94 // Thus, CPU could handle the potential pending interrupt of Local Apic timer.
96 while (GetApicTimerInterruptState () != EnableStatus
) {
101 return OldDebugTimerInterruptState
;
105 Check if the timer is time out.
107 @param[in] TimerCycle Timer initial count.
108 @param[in] Timer The start timer from the begin.
109 @param[in] TimeoutTicker Ticker number need time out.
111 @return TRUE Timer time out occurs.
112 @retval FALSE Timer does not time out.
116 IsDebugTimerTimeout (
117 IN UINT32 TimerCycle
,
119 IN UINT32 TimeoutTicker
125 CurrentTimer
= GetApicTimerCurrentCount ();
128 // This timer counter counts down. Check for roll over condition.
129 // If CurrentTimer is equal to Timer, it does not mean that roll over
132 if (CurrentTimer
<= Timer
) {
133 Delta
= Timer
- CurrentTimer
;
136 // Handle one roll-over.
138 Delta
= TimerCycle
- (CurrentTimer
- Timer
) + 1;
141 return (BOOLEAN
) (Delta
>= TimeoutTicker
);