]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
ArmPkg: Added Aarch64 support
[mirror_edk2.git] / ArmPkg / Library / ArmArchTimerLib / ArmArchTimerLib.c
CommitLineData
da9675a2 1/** @file\r
2 Generic ARM implementation of TimerLib.h\r
3\r
31d196c1 4 Copyright (c) 2011-2012, ARM Limited. All rights reserved.\r
da9675a2 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
25402f5d 22#include <Library/ArmArchTimerLib.h>\r
da9675a2 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
31d196c1 29TimerConstructor (\r
da9675a2 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
25402f5d
HL
46#ifdef MDE_CPU_ARM\r
47 // Only set the frequency for ARMv7. We expect the secure firmware to have already do it\r
da9675a2 48 // If the security extensions are not implemented set Timer Frequency\r
ce4b2bd4 49 if ((ArmReadIdPfr1 () & 0xF0) == 0x0) {\r
da9675a2 50 ArmArchTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));\r
51 }\r
25402f5d 52#endif\r
da9675a2 53\r
54 // Architectural Timer Frequency must be set in the Secure privileged(if secure extensions are supported) mode.\r
55 // If the reset value (0) is returned just ASSERT.\r
56 TimerFreq = ArmArchTimerGetTimerFreq ();\r
25402f5d 57 ASSERT (TimerFreq != 0);\r
da9675a2 58\r
59 } else {\r
ce88684e 60 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence this library can not be used.\n"));\r
da9675a2 61 ASSERT (0);\r
62 }\r
63\r
64 return RETURN_SUCCESS;\r
65}\r
66\r
67\r
68/**\r
69 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
70\r
71 @param MicroSeconds The minimum number of microseconds to delay.\r
72\r
73 @return The value of MicroSeconds inputted.\r
74\r
75**/\r
76UINTN\r
77EFIAPI\r
78MicroSecondDelay (\r
79 IN UINTN MicroSeconds\r
80 )\r
81{\r
82 UINT64 TimerTicks64;\r
83 UINT64 SystemCounterVal;\r
84\r
ce88684e 85 // Calculate counter ticks that can represent requested delay\r
da9675a2 86 TimerTicks64 = MultU64x32 (MicroSeconds, TICKS_PER_MICRO_SEC);\r
87\r
88 // Read System Counter value\r
89 SystemCounterVal = ArmArchTimerGetSystemCount ();\r
90\r
91 TimerTicks64 += SystemCounterVal;\r
92\r
93 // Wait until delay count is expired.\r
94 while (SystemCounterVal < TimerTicks64) {\r
95 SystemCounterVal = ArmArchTimerGetSystemCount ();\r
96 }\r
97\r
98 return MicroSeconds;\r
99}\r
100\r
101\r
102/**\r
103 Stalls the CPU for at least the given number of nanoseconds.\r
104\r
105 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
106\r
107 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.\r
108 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.\r
109\r
110 @param NanoSeconds The minimum number of nanoseconds to delay.\r
111\r
ce88684e 112 @return The value of NanoSeconds inputed.\r
da9675a2 113\r
114**/\r
115UINTN\r
116EFIAPI\r
117NanoSecondDelay (\r
118 IN UINTN NanoSeconds\r
119 )\r
120{\r
121 UINTN MicroSeconds;\r
122\r
123 // Round up to 1us Tick Number\r
124 MicroSeconds = NanoSeconds / 1000;\r
125 MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;\r
126\r
127 MicroSecondDelay (MicroSeconds);\r
128\r
129 return NanoSeconds;\r
130}\r
131\r
132/**\r
133 Retrieves the current value of a 64-bit free running performance counter.\r
134\r
135 The counter can either count up by 1 or count down by 1. If the physical\r
136 performance counter counts by a larger increment, then the counter values\r
137 must be translated. The properties of the counter can be retrieved from\r
138 GetPerformanceCounterProperties().\r
139\r
140 @return The current value of the free running performance counter.\r
141\r
142**/\r
143UINT64\r
144EFIAPI\r
145GetPerformanceCounter (\r
146 VOID\r
147 )\r
148{\r
149 // Just return the value of system count\r
150 return ArmArchTimerGetSystemCount ();\r
151}\r
152\r
153/**\r
154 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
155 values.\r
156\r
157 If StartValue is not NULL, then the value that the performance counter starts\r
158 with immediately after is it rolls over is returned in StartValue. If\r
159 EndValue is not NULL, then the value that the performance counter end with\r
160 immediately before it rolls over is returned in EndValue. The 64-bit\r
161 frequency of the performance counter in Hz is always returned. If StartValue\r
162 is less than EndValue, then the performance counter counts up. If StartValue\r
163 is greater than EndValue, then the performance counter counts down. For\r
164 example, a 64-bit free running counter that counts up would have a StartValue\r
165 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
166 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
167\r
168 @param StartValue The value the performance counter starts with when it\r
169 rolls over.\r
170 @param EndValue The value that the performance counter ends with before\r
171 it rolls over.\r
172\r
173 @return The frequency in Hz.\r
174\r
175**/\r
176UINT64\r
177EFIAPI\r
178GetPerformanceCounterProperties (\r
179 OUT UINT64 *StartValue, OPTIONAL\r
180 OUT UINT64 *EndValue OPTIONAL\r
181 )\r
182{\r
183 if (StartValue != NULL) {\r
184 // Timer starts with the reload value\r
185 *StartValue = (UINT64)0ULL ;\r
186 }\r
187\r
188 if (EndValue != NULL) {\r
189 // Timer counts down to 0x0\r
89bbce11 190 *EndValue = 0xFFFFFFFFFFFFFFFFUL;\r
da9675a2 191 }\r
192\r
193 return (UINT64)ArmArchTimerGetTimerFreq ();\r
194}\r