2 RISC-V instance of Timer Library.
4 Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/PcdLib.h>
14 #include <Register/RiscV64/RiscVImpl.h>
17 Stalls the CPU for at least the given number of ticks.
19 Stalls the CPU for at least the given number of ticks. It's invoked by
20 MicroSecondDelay() and NanoSecondDelay().
22 @param Delay A period of time to delay in ticks.
26 InternalRiscVTimerDelay (
33 Times
= Delay
>> (RISCV_TIMER_COMPARE_BITS
- 2);
34 Delay
&= ((1 << (RISCV_TIMER_COMPARE_BITS
- 2)) - 1);
37 // The target timer count is calculated here
39 Ticks
= RiscVReadTimer () + Delay
;
40 Delay
= 1 << (RISCV_TIMER_COMPARE_BITS
- 2);
41 while (((Ticks
- RiscVReadTimer ()) & (1 << (RISCV_TIMER_COMPARE_BITS
- 1))) == 0) {
44 } while (Times
-- > 0);
48 Stalls the CPU for at least the given number of microseconds.
50 Stalls the CPU for the number of microseconds specified by MicroSeconds.
52 @param MicroSeconds The minimum number of microseconds to delay.
63 InternalRiscVTimerDelay (
67 PcdGet64 (PcdCpuCoreCrystalClockFrequency
)
76 Stalls the CPU for at least the given number of nanoseconds.
78 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
80 @param NanoSeconds The minimum number of nanoseconds to delay.
91 InternalRiscVTimerDelay (
95 PcdGet64 (PcdCpuCoreCrystalClockFrequency
)
104 Retrieves the current value of a 64-bit free running performance counter.
106 Retrieves the current value of a 64-bit free running performance counter. The
107 counter can either count up by 1 or count down by 1. If the physical
108 performance counter counts by a larger increment, then the counter values
109 must be translated. The properties of the counter can be retrieved from
110 GetPerformanceCounterProperties().
112 @return The current value of the free running performance counter.
117 GetPerformanceCounter (
121 return (UINT64
)RiscVReadTimer ();
125 Retrieves the 64-bit frequency in Hz and the range of performance counter
128 If StartValue is not NULL, then the value that the performance counter starts
129 with immediately after is it rolls over is returned in StartValue. If
130 EndValue is not NULL, then the value that the performance counter end with
131 immediately before it rolls over is returned in EndValue. The 64-bit
132 frequency of the performance counter in Hz is always returned. If StartValue
133 is less than EndValue, then the performance counter counts up. If StartValue
134 is greater than EndValue, then the performance counter counts down. For
135 example, a 64-bit free running counter that counts up would have a StartValue
136 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
137 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
139 @param StartValue The value the performance counter starts with when it
141 @param EndValue The value that the performance counter ends with before
144 @return The frequency in Hz.
149 GetPerformanceCounterProperties (
150 OUT UINT64
*StartValue
, OPTIONAL
151 OUT UINT64
*EndValue OPTIONAL
154 if (StartValue
!= NULL
) {
158 if (EndValue
!= NULL
) {
162 return PcdGet64 (PcdCpuCoreCrystalClockFrequency
);
166 Converts elapsed ticks of performance counter to time in nanoseconds.
168 This function converts the elapsed ticks of running performance counter to
169 time value in unit of nanoseconds.
171 @param Ticks The number of elapsed ticks of running performance counter.
173 @return The elapsed time in nanoseconds.
178 GetTimeInNanoSecond (
187 // Time = --------- x 1,000,000,000
190 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, PcdGet64 (PcdCpuCoreCrystalClockFrequency
), &Remainder
), 1000000000u);
193 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
194 // will not overflow 64-bit.
196 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
)Remainder
, 1000000000u), PcdGet64 (PcdCpuCoreCrystalClockFrequency
));