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