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