]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
Arm Packages: Fix builds for XCODE32 toolchain
[mirror_edk2.git] / ArmPkg / Library / ArmArchTimerLib / ArmArchTimerLib.c
CommitLineData
da9675a2 1/** @file\r
2 Generic ARM implementation of TimerLib.h\r
3\r
4 Copyright (c) 2011, ARM Limited. All rights reserved.\r
5 \r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16\r
17#include <Base.h>\r
18#include <Library/BaseLib.h>\r
19#include <Library/TimerLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22#include <Library/ArmV7ArchTimerLib.h>\r
23#include <Chipset/ArmV7.h>\r
24\r
25#define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)\r
26\r
27RETURN_STATUS\r
28EFIAPI\r
29ArmArchTimerLibConstructor (\r
30 VOID\r
31 )\r
32{\r
33 // Check if the ARM Generic Timer Extension is implemented\r
34 if (ArmIsArchTimerImplemented ()) {\r
35\r
36 UINTN TimerFreq;\r
37\r
38 // Check if Architectural Timer frequency is valid number (should not be 0)\r
39 ASSERT (PcdGet32 (PcdArmArchTimerFreqInHz));\r
40\r
41 // Check if ticks/uS is not 0. The Architectural timer runs at constant\r
42 // frequency irrespective of CPU frequency. According to General Timer Ref\r
43 // manual lower bound of the frequency is in the range of 1-10MHz\r
44 ASSERT (TICKS_PER_MICRO_SEC);\r
45\r
46 // If the security extensions are not implemented set Timer Frequency\r
47 if ((ArmReadIdPfr1 () & 0xF0)) {\r
48 ArmArchTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));\r
49 }\r
50\r
51 // Architectural Timer Frequency must be set in the Secure privileged(if secure extensions are supported) mode.\r
52 // If the reset value (0) is returned just ASSERT.\r
53 TimerFreq = ArmArchTimerGetTimerFreq ();\r
54 ASSERT (TimerFreq);\r
55\r
56 } else {\r
57 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, Hence cann't use this library \n"));\r
58 ASSERT (0);\r
59 }\r
60\r
61 return RETURN_SUCCESS;\r
62}\r
63\r
64\r
65/**\r
66 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
67\r
68 @param MicroSeconds The minimum number of microseconds to delay.\r
69\r
70 @return The value of MicroSeconds inputted.\r
71\r
72**/\r
73UINTN\r
74EFIAPI\r
75MicroSecondDelay (\r
76 IN UINTN MicroSeconds\r
77 )\r
78{\r
79 UINT64 TimerTicks64;\r
80 UINT64 SystemCounterVal;\r
81\r
82 // Calculate counter ticks that can represent requsted delay\r
83 TimerTicks64 = MultU64x32 (MicroSeconds, TICKS_PER_MICRO_SEC);\r
84\r
85 // Read System Counter value\r
86 SystemCounterVal = ArmArchTimerGetSystemCount ();\r
87\r
88 TimerTicks64 += SystemCounterVal;\r
89\r
90 // Wait until delay count is expired.\r
91 while (SystemCounterVal < TimerTicks64) {\r
92 SystemCounterVal = ArmArchTimerGetSystemCount ();\r
93 }\r
94\r
95 return MicroSeconds;\r
96}\r
97\r
98\r
99/**\r
100 Stalls the CPU for at least the given number of nanoseconds.\r
101\r
102 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
103\r
104 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.\r
105 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.\r
106\r
107 @param NanoSeconds The minimum number of nanoseconds to delay.\r
108\r
109 @return The value of NanoSeconds inputted.\r
110\r
111**/\r
112UINTN\r
113EFIAPI\r
114NanoSecondDelay (\r
115 IN UINTN NanoSeconds\r
116 )\r
117{\r
118 UINTN MicroSeconds;\r
119\r
120 // Round up to 1us Tick Number\r
121 MicroSeconds = NanoSeconds / 1000;\r
122 MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;\r
123\r
124 MicroSecondDelay (MicroSeconds);\r
125\r
126 return NanoSeconds;\r
127}\r
128\r
129/**\r
130 Retrieves the current value of a 64-bit free running performance counter.\r
131\r
132 The counter can either count up by 1 or count down by 1. If the physical\r
133 performance counter counts by a larger increment, then the counter values\r
134 must be translated. The properties of the counter can be retrieved from\r
135 GetPerformanceCounterProperties().\r
136\r
137 @return The current value of the free running performance counter.\r
138\r
139**/\r
140UINT64\r
141EFIAPI\r
142GetPerformanceCounter (\r
143 VOID\r
144 )\r
145{\r
146 // Just return the value of system count\r
147 return ArmArchTimerGetSystemCount ();\r
148}\r
149\r
150/**\r
151 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
152 values.\r
153\r
154 If StartValue is not NULL, then the value that the performance counter starts\r
155 with immediately after is it rolls over is returned in StartValue. If\r
156 EndValue is not NULL, then the value that the performance counter end with\r
157 immediately before it rolls over is returned in EndValue. The 64-bit\r
158 frequency of the performance counter in Hz is always returned. If StartValue\r
159 is less than EndValue, then the performance counter counts up. If StartValue\r
160 is greater than EndValue, then the performance counter counts down. For\r
161 example, a 64-bit free running counter that counts up would have a StartValue\r
162 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
163 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
164\r
165 @param StartValue The value the performance counter starts with when it\r
166 rolls over.\r
167 @param EndValue The value that the performance counter ends with before\r
168 it rolls over.\r
169\r
170 @return The frequency in Hz.\r
171\r
172**/\r
173UINT64\r
174EFIAPI\r
175GetPerformanceCounterProperties (\r
176 OUT UINT64 *StartValue, OPTIONAL\r
177 OUT UINT64 *EndValue OPTIONAL\r
178 )\r
179{\r
180 if (StartValue != NULL) {\r
181 // Timer starts with the reload value\r
182 *StartValue = (UINT64)0ULL ;\r
183 }\r
184\r
185 if (EndValue != NULL) {\r
186 // Timer counts down to 0x0\r
89bbce11 187 *EndValue = 0xFFFFFFFFFFFFFFFFUL;\r
da9675a2 188 }\r
189\r
190 return (UINT64)ArmArchTimerGetTimerFreq ();\r
191}\r