]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugTimer.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugTimer.c
1 /** @file
2 Code for debug timer to support debug agent library implementation.
3
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "DebugAgent.h"
10
11 /**
12 Initialize CPU local APIC timer.
13
14 @param[out] TimerFrequency Local APIC timer frequency returned.
15 @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.
16
17 @return 32-bit Local APIC timer init count.
18 **/
19 UINT32
20 InitializeDebugTimer (
21 OUT UINT32 *TimerFrequency,
22 IN BOOLEAN DumpFlag
23 )
24 {
25 UINTN ApicTimerDivisor;
26 UINT32 InitialCount;
27 UINT32 ApicTimerFrequency;
28
29 InitializeLocalApicSoftwareEnable (TRUE);
30 GetApicTimerState (&ApicTimerDivisor, NULL, NULL);
31 ApicTimerFrequency = PcdGet32 (PcdFSBClock) / (UINT32)ApicTimerDivisor;
32 //
33 // Cpu Local Apic timer interrupt frequency, it is set to 0.1s
34 //
35 InitialCount = (UINT32)DivU64x32 (
36 MultU64x64 (
37 ApicTimerFrequency,
38 DEBUG_TIMER_INTERVAL
39 ),
40 1000000u
41 );
42
43 InitializeApicTimer (ApicTimerDivisor, InitialCount, TRUE, DEBUG_TIMER_VECTOR);
44 //
45 // Disable Debug Timer interrupt to avoid it is delivered before Debug Port
46 // is initialized
47 //
48 DisableApicTimerInterrupt ();
49
50 if (DumpFlag) {
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));
55 }
56
57 if (TimerFrequency != NULL) {
58 *TimerFrequency = ApicTimerFrequency;
59 }
60
61 return InitialCount;
62 }
63
64 /**
65 Enable/Disable the interrupt of debug timer and return the interrupt state
66 prior to the operation.
67
68 If EnableStatus is TRUE, enable the interrupt of debug timer.
69 If EnableStatus is FALSE, disable the interrupt of debug timer.
70
71 @param[in] EnableStatus Enable/Disable.
72
73 @retval TRUE Debug timer interrupt were enabled on entry to this call.
74 @retval FALSE Debug timer interrupt were disabled on entry to this call.
75
76 **/
77 BOOLEAN
78 EFIAPI
79 SaveAndSetDebugTimerInterrupt (
80 IN BOOLEAN EnableStatus
81 )
82 {
83 BOOLEAN OldDebugTimerInterruptState;
84
85 OldDebugTimerInterruptState = GetApicTimerInterruptState ();
86
87 if (OldDebugTimerInterruptState != EnableStatus) {
88 if (EnableStatus) {
89 EnableApicTimerInterrupt ();
90 } else {
91 DisableApicTimerInterrupt ();
92 }
93
94 //
95 // Validate the Debug Timer interrupt state
96 // This will make additional delay after Local Apic Timer interrupt state is changed.
97 // Thus, CPU could handle the potential pending interrupt of Local Apic timer.
98 //
99 while (GetApicTimerInterruptState () != EnableStatus) {
100 CpuPause ();
101 }
102 }
103
104 return OldDebugTimerInterruptState;
105 }
106
107 /**
108 Check if the timer is time out.
109
110 @param[in] TimerCycle Timer initial count.
111 @param[in] Timer The start timer from the begin.
112 @param[in] TimeoutTicker Ticker number need time out.
113
114 @return TRUE Timer time out occurs.
115 @retval FALSE Timer does not time out.
116
117 **/
118 BOOLEAN
119 IsDebugTimerTimeout (
120 IN UINT32 TimerCycle,
121 IN UINT32 Timer,
122 IN UINT32 TimeoutTicker
123 )
124 {
125 UINT64 CurrentTimer;
126 UINT64 Delta;
127
128 CurrentTimer = GetApicTimerCurrentCount ();
129
130 //
131 // This timer counter counts down. Check for roll over condition.
132 // If CurrentTimer is equal to Timer, it does not mean that roll over
133 // happened.
134 //
135 if (CurrentTimer <= Timer) {
136 Delta = Timer - CurrentTimer;
137 } else {
138 //
139 // Handle one roll-over.
140 //
141 Delta = TimerCycle - (CurrentTimer - Timer) + 1;
142 }
143
144 return (BOOLEAN)(Delta >= TimeoutTicker);
145 }