]>
Commit | Line | Data |
---|---|---|
18b144ea | 1 | /** @file\r |
2 | Code for debug timer to support debug agent library implementation.\r | |
3 | \r | |
08021523 | 4 | Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r |
18b144ea | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "DebugAgent.h"\r | |
16 | \r | |
17 | /**\r | |
18 | Initialize CPU local APIC timer.\r | |
19 | \r | |
99b98734 | 20 | @param[out] TimerFrequency Local APIC timer frequency returned.\r |
86d13652 | 21 | @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.\r |
8a079ef2 | 22 | \r |
4fe43eb3 | 23 | @return 32-bit Local APIC timer init count.\r |
18b144ea | 24 | **/\r |
4fe43eb3 | 25 | UINT32\r |
18b144ea | 26 | InitializeDebugTimer (\r |
86d13652 JF |
27 | OUT UINT32 *TimerFrequency,\r |
28 | IN BOOLEAN DumpFlag\r | |
18b144ea | 29 | )\r |
30 | {\r | |
31 | UINTN ApicTimerDivisor;\r | |
32 | UINT32 InitialCount;\r | |
08021523 | 33 | UINT32 ApicTimerFrequency;\r |
18b144ea | 34 | \r |
df60fb4c | 35 | InitializeLocalApicSoftwareEnable (TRUE);\r |
18b144ea | 36 | GetApicTimerState (&ApicTimerDivisor, NULL, NULL);\r |
08021523 | 37 | ApicTimerFrequency = PcdGet32(PcdFSBClock) / (UINT32)ApicTimerDivisor;\r |
18b144ea | 38 | //\r |
39 | // Cpu Local Apic timer interrupt frequency, it is set to 0.1s\r | |
40 | //\r | |
41 | InitialCount = (UINT32)DivU64x32 (\r | |
42 | MultU64x64 (\r | |
08021523 JF |
43 | ApicTimerFrequency,\r |
44 | DEBUG_TIMER_INTERVAL\r | |
18b144ea | 45 | ),\r |
08021523 | 46 | 1000000u\r |
18b144ea | 47 | );\r |
48 | \r | |
49 | InitializeApicTimer (ApicTimerDivisor, InitialCount, TRUE, DEBUG_TIMER_VECTOR);\r | |
2befbc82 JF |
50 | //\r |
51 | // Disable Debug Timer interrupt to avoid it is delivered before Debug Port\r | |
52 | // is initialized\r | |
53 | //\r | |
54 | DisableApicTimerInterrupt ();\r | |
18b144ea | 55 | \r |
86d13652 JF |
56 | if (DumpFlag) {\r |
57 | DEBUG ((EFI_D_INFO, "Debug Timer: FSB Clock = %d\n", PcdGet32(PcdFSBClock)));\r | |
58 | DEBUG ((EFI_D_INFO, "Debug Timer: Divisor = %d\n", ApicTimerDivisor));\r | |
59 | DEBUG ((EFI_D_INFO, "Debug Timer: Frequency = %d\n", ApicTimerFrequency));\r | |
60 | DEBUG ((EFI_D_INFO, "Debug Timer: InitialCount = %d\n", InitialCount));\r | |
61 | }\r | |
08021523 JF |
62 | if (TimerFrequency != NULL) {\r |
63 | *TimerFrequency = ApicTimerFrequency;\r | |
64 | }\r | |
4fe43eb3 | 65 | return InitialCount;\r |
18b144ea | 66 | }\r |
67 | \r | |
68 | /**\r | |
69 | Enable/Disable the interrupt of debug timer and return the interrupt state\r | |
70 | prior to the operation.\r | |
71 | \r | |
72 | If EnableStatus is TRUE, enable the interrupt of debug timer.\r | |
73 | If EnableStatus is FALSE, disable the interrupt of debug timer.\r | |
74 | \r | |
75 | @param[in] EnableStatus Enable/Disable.\r | |
76 | \r | |
77 | @retval TRUE Debug timer interrupt were enabled on entry to this call.\r | |
78 | @retval FALSE Debug timer interrupt were disabled on entry to this call.\r | |
79 | \r | |
80 | **/\r | |
81 | BOOLEAN\r | |
82 | EFIAPI\r | |
83 | SaveAndSetDebugTimerInterrupt (\r | |
84 | IN BOOLEAN EnableStatus\r | |
85 | )\r | |
86 | {\r | |
18b144ea | 87 | BOOLEAN OldDebugTimerInterruptState;\r |
88 | \r | |
18b144ea | 89 | OldDebugTimerInterruptState = GetApicTimerInterruptState ();\r |
4fe43eb3 | 90 | \r |
b422b62c | 91 | if (OldDebugTimerInterruptState != EnableStatus) {\r |
92 | if (EnableStatus) {\r | |
93 | EnableApicTimerInterrupt ();\r | |
94 | } else {\r | |
95 | DisableApicTimerInterrupt ();\r | |
96 | }\r | |
97 | //\r | |
98 | // Validate the Debug Timer interrupt state\r | |
99 | // This will make additional delay after Local Apic Timer interrupt state is changed.\r | |
100 | // Thus, CPU could handle the potential pending interrupt of Local Apic timer.\r | |
101 | //\r | |
102 | while (GetApicTimerInterruptState () != EnableStatus) {\r | |
103 | CpuPause ();\r | |
104 | }\r | |
18b144ea | 105 | }\r |
4fe43eb3 | 106 | \r |
18b144ea | 107 | return OldDebugTimerInterruptState;\r |
108 | }\r | |
109 | \r | |
08021523 JF |
110 | /**\r |
111 | Check if the timer is time out.\r | |
8a079ef2 JF |
112 | \r |
113 | @param[in] TimerCycle Timer initial count.\r | |
08021523 JF |
114 | @param[in] Timer The start timer from the begin.\r |
115 | @param[in] TimeoutTicker Ticker number need time out.\r | |
116 | \r | |
117 | @return TRUE Timer time out occurs.\r | |
118 | @retval FALSE Timer does not time out.\r | |
119 | \r | |
120 | **/\r | |
121 | BOOLEAN\r | |
122 | IsDebugTimerTimeout (\r | |
123 | IN UINT32 TimerCycle,\r | |
124 | IN UINT32 Timer,\r | |
125 | IN UINT32 TimeoutTicker\r | |
126 | )\r | |
127 | {\r | |
128 | UINT64 CurrentTimer;\r | |
129 | UINT64 Delta;\r | |
130 | \r | |
131 | CurrentTimer = GetApicTimerCurrentCount ();\r | |
132 | \r | |
133 | //\r | |
134 | // This timer counter counts down. Check for roll over condition.\r | |
3b9468ef JF |
135 | // If CurrentTimer is equal to Timer, it does not mean that roll over\r |
136 | // happened.\r | |
08021523 | 137 | //\r |
3b9468ef | 138 | if (CurrentTimer <= Timer) {\r |
08021523 JF |
139 | Delta = Timer - CurrentTimer;\r |
140 | } else {\r | |
141 | //\r | |
8a079ef2 | 142 | // Handle one roll-over.\r |
08021523 | 143 | //\r |
8a079ef2 | 144 | Delta = TimerCycle - (CurrentTimer - Timer) + 1;\r |
08021523 | 145 | }\r |
8a079ef2 | 146 | \r |
08021523 JF |
147 | return (BOOLEAN) (Delta >= TimeoutTicker);\r |
148 | }\r | |
149 | \r |