]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugTimer.c
SourceLevelDebugPkg/DebugTimer: Dump Debug Timer parameter
[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 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.
9
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.
12
13 **/
14
15 #include "DebugAgent.h"
16
17 /**
18 Initialize CPU local APIC timer.
19
20 @param[out] TimerFrequency Local APIC timer frequency returned.
21 @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.
22
23 @return 32-bit Local APIC timer init count.
24 **/
25 UINT32
26 InitializeDebugTimer (
27 OUT UINT32 *TimerFrequency,
28 IN BOOLEAN DumpFlag
29 )
30 {
31 UINTN ApicTimerDivisor;
32 UINT32 InitialCount;
33 UINT32 ApicTimerFrequency;
34
35 GetApicTimerState (&ApicTimerDivisor, NULL, NULL);
36 ApicTimerFrequency = PcdGet32(PcdFSBClock) / (UINT32)ApicTimerDivisor;
37 //
38 // Cpu Local Apic timer interrupt frequency, it is set to 0.1s
39 //
40 InitialCount = (UINT32)DivU64x32 (
41 MultU64x64 (
42 ApicTimerFrequency,
43 DEBUG_TIMER_INTERVAL
44 ),
45 1000000u
46 );
47
48 InitializeApicTimer (ApicTimerDivisor, InitialCount, TRUE, DEBUG_TIMER_VECTOR);
49
50 if (DumpFlag) {
51 DEBUG ((EFI_D_INFO, "Debug Timer: FSB Clock = %d\n", PcdGet32(PcdFSBClock)));
52 DEBUG ((EFI_D_INFO, "Debug Timer: Divisor = %d\n", ApicTimerDivisor));
53 DEBUG ((EFI_D_INFO, "Debug Timer: Frequency = %d\n", ApicTimerFrequency));
54 DEBUG ((EFI_D_INFO, "Debug Timer: InitialCount = %d\n", InitialCount));
55 }
56 if (TimerFrequency != NULL) {
57 *TimerFrequency = ApicTimerFrequency;
58 }
59 return InitialCount;
60 }
61
62 /**
63 Enable/Disable the interrupt of debug timer and return the interrupt state
64 prior to the operation.
65
66 If EnableStatus is TRUE, enable the interrupt of debug timer.
67 If EnableStatus is FALSE, disable the interrupt of debug timer.
68
69 @param[in] EnableStatus Enable/Disable.
70
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.
73
74 **/
75 BOOLEAN
76 EFIAPI
77 SaveAndSetDebugTimerInterrupt (
78 IN BOOLEAN EnableStatus
79 )
80 {
81 BOOLEAN OldDebugTimerInterruptState;
82
83 OldDebugTimerInterruptState = GetApicTimerInterruptState ();
84
85 if (OldDebugTimerInterruptState != EnableStatus) {
86 if (EnableStatus) {
87 EnableApicTimerInterrupt ();
88 } else {
89 DisableApicTimerInterrupt ();
90 }
91 //
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.
95 //
96 while (GetApicTimerInterruptState () != EnableStatus) {
97 CpuPause ();
98 }
99 }
100
101 return OldDebugTimerInterruptState;
102 }
103
104 /**
105 Check if the timer is time out.
106
107 @param[in] TimerCycle Timer total count.
108 @param[in] Timer The start timer from the begin.
109 @param[in] TimeoutTicker Ticker number need time out.
110
111 @return TRUE Timer time out occurs.
112 @retval FALSE Timer does not time out.
113
114 **/
115 BOOLEAN
116 IsDebugTimerTimeout (
117 IN UINT32 TimerCycle,
118 IN UINT32 Timer,
119 IN UINT32 TimeoutTicker
120 )
121 {
122 UINT64 CurrentTimer;
123 UINT64 Delta;
124
125 CurrentTimer = GetApicTimerCurrentCount ();
126
127 //
128 // This timer counter counts down. Check for roll over condition.
129 //
130 if (CurrentTimer < Timer) {
131 Delta = Timer - CurrentTimer;
132 } else {
133 //
134 // Handle one roll-over.
135 //
136 Delta = TimerCycle - (CurrentTimer - Timer);
137 }
138
139 return (BOOLEAN) (Delta >= TimeoutTicker);
140 }
141