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