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
InitializeDebugTimer(OUT UINT32 * TimerFrequency,IN BOOLEAN DumpFlag)26 InitializeDebugTimer (
27   OUT UINT32     *TimerFrequency,
28   IN  BOOLEAN    DumpFlag
29   )
30 {
31   UINTN       ApicTimerDivisor;
32   UINT32      InitialCount;
33   UINT32      ApicTimerFrequency;
34 
35   InitializeLocalApicSoftwareEnable (TRUE);
36   GetApicTimerState (&ApicTimerDivisor, NULL, NULL);
37   ApicTimerFrequency = PcdGet32(PcdFSBClock) / (UINT32)ApicTimerDivisor;
38   //
39   // Cpu Local Apic timer interrupt frequency, it is set to 0.1s
40   //
41   InitialCount = (UINT32)DivU64x32 (
42                    MultU64x64 (
43                      ApicTimerFrequency,
44                      DEBUG_TIMER_INTERVAL
45                      ),
46                    1000000u
47                    );
48 
49   InitializeApicTimer (ApicTimerDivisor, InitialCount, TRUE, DEBUG_TIMER_VECTOR);
50   //
51   // Disable Debug Timer interrupt to avoid it is delivered before Debug Port
52   // is initialized
53   //
54   DisableApicTimerInterrupt ();
55 
56   if (DumpFlag) {
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));
61   }
62   if (TimerFrequency != NULL) {
63     *TimerFrequency = ApicTimerFrequency;
64   }
65   return InitialCount;
66 }
67 
68 /**
69   Enable/Disable the interrupt of debug timer and return the interrupt state
70   prior to the operation.
71 
72   If EnableStatus is TRUE, enable the interrupt of debug timer.
73   If EnableStatus is FALSE, disable the interrupt of debug timer.
74 
75   @param[in] EnableStatus    Enable/Disable.
76 
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.
79 
80 **/
81 BOOLEAN
82 EFIAPI
SaveAndSetDebugTimerInterrupt(IN BOOLEAN EnableStatus)83 SaveAndSetDebugTimerInterrupt (
84   IN BOOLEAN                EnableStatus
85   )
86 {
87   BOOLEAN     OldDebugTimerInterruptState;
88 
89   OldDebugTimerInterruptState = GetApicTimerInterruptState ();
90 
91   if (OldDebugTimerInterruptState != EnableStatus) {
92     if (EnableStatus) {
93       EnableApicTimerInterrupt ();
94     } else {
95       DisableApicTimerInterrupt ();
96     }
97     //
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.
101     //
102     while (GetApicTimerInterruptState () != EnableStatus) {
103       CpuPause ();
104     }
105   }
106 
107   return OldDebugTimerInterruptState;
108 }
109 
110 /**
111   Check if the timer is time out.
112 
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.
116 
117   @return TRUE  Timer time out occurs.
118   @retval FALSE Timer does not time out.
119 
120 **/
121 BOOLEAN
IsDebugTimerTimeout(IN UINT32 TimerCycle,IN UINT32 Timer,IN UINT32 TimeoutTicker)122 IsDebugTimerTimeout (
123   IN UINT32                     TimerCycle,
124   IN UINT32                     Timer,
125   IN UINT32                     TimeoutTicker
126   )
127 {
128   UINT64  CurrentTimer;
129   UINT64  Delta;
130 
131   CurrentTimer = GetApicTimerCurrentCount ();
132 
133   //
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
136   // happened.
137   //
138   if (CurrentTimer <= Timer) {
139     Delta = Timer - CurrentTimer;
140   } else {
141     //
142     // Handle one roll-over.
143     //
144     Delta = TimerCycle - (CurrentTimer - Timer) + 1;
145   }
146 
147   return (BOOLEAN) (Delta >= TimeoutTicker);
148 }
149 
150