]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/CpuTimerLib.c
UefiCpuPkg: Add BaseRiscV64CpuTimerLib library
[mirror_edk2.git] / UefiCpuPkg / Library / BaseRiscV64CpuTimerLib / CpuTimerLib.c
1 /** @file
2 RISC-V instance of Timer Library.
3
4 Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/PcdLib.h>
14 #include <Register/RiscV64/RiscVImpl.h>
15
16 /**
17 Stalls the CPU for at least the given number of ticks.
18
19 Stalls the CPU for at least the given number of ticks. It's invoked by
20 MicroSecondDelay() and NanoSecondDelay().
21
22 @param Delay A period of time to delay in ticks.
23
24 **/
25 VOID
26 InternalRiscVTimerDelay (
27 IN UINT32 Delay
28 )
29 {
30 UINT32 Ticks;
31 UINT32 Times;
32
33 Times = Delay >> (RISCV_TIMER_COMPARE_BITS - 2);
34 Delay &= ((1 << (RISCV_TIMER_COMPARE_BITS - 2)) - 1);
35 do {
36 //
37 // The target timer count is calculated here
38 //
39 Ticks = RiscVReadTimer () + Delay;
40 Delay = 1 << (RISCV_TIMER_COMPARE_BITS - 2);
41 while (((Ticks - RiscVReadTimer ()) & (1 << (RISCV_TIMER_COMPARE_BITS - 1))) == 0) {
42 CpuPause ();
43 }
44 } while (Times-- > 0);
45 }
46
47 /**
48 Stalls the CPU for at least the given number of microseconds.
49
50 Stalls the CPU for the number of microseconds specified by MicroSeconds.
51
52 @param MicroSeconds The minimum number of microseconds to delay.
53
54 @return MicroSeconds
55
56 **/
57 UINTN
58 EFIAPI
59 MicroSecondDelay (
60 IN UINTN MicroSeconds
61 )
62 {
63 InternalRiscVTimerDelay (
64 (UINT32)DivU64x32 (
65 MultU64x32 (
66 MicroSeconds,
67 PcdGet64 (PcdCpuCoreCrystalClockFrequency)
68 ),
69 1000000u
70 )
71 );
72 return MicroSeconds;
73 }
74
75 /**
76 Stalls the CPU for at least the given number of nanoseconds.
77
78 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
79
80 @param NanoSeconds The minimum number of nanoseconds to delay.
81
82 @return NanoSeconds
83
84 **/
85 UINTN
86 EFIAPI
87 NanoSecondDelay (
88 IN UINTN NanoSeconds
89 )
90 {
91 InternalRiscVTimerDelay (
92 (UINT32)DivU64x32 (
93 MultU64x32 (
94 NanoSeconds,
95 PcdGet64 (PcdCpuCoreCrystalClockFrequency)
96 ),
97 1000000000u
98 )
99 );
100 return NanoSeconds;
101 }
102
103 /**
104 Retrieves the current value of a 64-bit free running performance counter.
105
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().
111
112 @return The current value of the free running performance counter.
113
114 **/
115 UINT64
116 EFIAPI
117 GetPerformanceCounter (
118 VOID
119 )
120 {
121 return (UINT64)RiscVReadTimer ();
122 }
123
124 /**return
125 Retrieves the 64-bit frequency in Hz and the range of performance counter
126 values.
127
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.
138
139 @param StartValue The value the performance counter starts with when it
140 rolls over.
141 @param EndValue The value that the performance counter ends with before
142 it rolls over.
143
144 @return The frequency in Hz.
145
146 **/
147 UINT64
148 EFIAPI
149 GetPerformanceCounterProperties (
150 OUT UINT64 *StartValue, OPTIONAL
151 OUT UINT64 *EndValue OPTIONAL
152 )
153 {
154 if (StartValue != NULL) {
155 *StartValue = 0;
156 }
157
158 if (EndValue != NULL) {
159 *EndValue = 32 - 1;
160 }
161
162 return PcdGet64 (PcdCpuCoreCrystalClockFrequency);
163 }
164
165 /**
166 Converts elapsed ticks of performance counter to time in nanoseconds.
167
168 This function converts the elapsed ticks of running performance counter to
169 time value in unit of nanoseconds.
170
171 @param Ticks The number of elapsed ticks of running performance counter.
172
173 @return The elapsed time in nanoseconds.
174
175 **/
176 UINT64
177 EFIAPI
178 GetTimeInNanoSecond (
179 IN UINT64 Ticks
180 )
181 {
182 UINT64 NanoSeconds;
183 UINT32 Remainder;
184
185 //
186 // Ticks
187 // Time = --------- x 1,000,000,000
188 // Frequency
189 //
190 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, PcdGet64 (PcdCpuCoreCrystalClockFrequency), &Remainder), 1000000000u);
191
192 //
193 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
194 // will not overflow 64-bit.
195 //
196 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64)Remainder, 1000000000u), PcdGet64 (PcdCpuCoreCrystalClockFrequency));
197
198 return NanoSeconds;
199 }