TimerLib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /** @file
  2. Generic LoongArch implementation of TimerLib.h
  3. Copyright (c) 2022 Loongson Technology Corporation Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Glossary:
  6. - Freq - Frequency
  7. - Csr - Cpu Status Register
  8. - calc - calculate
  9. **/
  10. #include <Base.h>
  11. #include <Library/TimerLib.h>
  12. #include <Library/BaseLib.h>
  13. #include <Library/DebugLib.h>
  14. #include "Library/StableTimer.h"
  15. #include "Library/Cpu.h"
  16. UINT32 StableTimerFreq = 0;
  17. /**
  18. Calculate the timer frequency.
  19. @param[] VOID
  20. @retval Timer frequency.
  21. **/
  22. UINT32
  23. EFIAPI
  24. CalcConstFreq (
  25. VOID
  26. )
  27. {
  28. UINT32 Result;
  29. UINT32 BaseFreq;
  30. UINT32 ClockMultiplier;
  31. UINT32 ClockDivide;
  32. UINT64 Val;
  33. LoongArchReadCpuCfg (&Val, LOONGARCH_CPUCFG4);
  34. BaseFreq = (UINT32)Val;
  35. LoongArchReadCpuCfg (&Val, LOONGARCH_CPUCFG5);
  36. Result = (UINT32)Val;
  37. ClockMultiplier = Result & 0xffff;
  38. ClockDivide = (Result >> 16) & 0xffff;
  39. if ((!BaseFreq) || (!ClockMultiplier) || (!ClockDivide)) {
  40. return 0;
  41. } else {
  42. return (BaseFreq * ClockMultiplier / ClockDivide);
  43. }
  44. }
  45. /**
  46. Get the timer frequency.
  47. @param[] VOID
  48. @retval Timer frequency.
  49. **/
  50. UINT32
  51. EFIAPI
  52. GetFreq (
  53. VOID
  54. )
  55. {
  56. if (StableTimerFreq) {
  57. } else {
  58. StableTimerFreq = CalcConstFreq ();
  59. }
  60. return StableTimerFreq;
  61. }
  62. /**
  63. Stalls the CPU for at least the given number of microseconds.
  64. Stalls the CPU for the number of microseconds specified by MicroSeconds.
  65. @param MicroSeconds The minimum number of microseconds to delay.
  66. @return MicroSeconds
  67. **/
  68. UINTN
  69. EFIAPI
  70. MicroSecondDelay (
  71. IN UINTN MicroSeconds
  72. )
  73. {
  74. UINTN Count;
  75. UINTN Ticks;
  76. UINTN Start;
  77. UINTN End;
  78. Count = GetFreq ();
  79. Count = (Count * MicroSeconds) / 1000000;
  80. Start = LoongArchReadTime ();
  81. End = Start + Count;
  82. do {
  83. Ticks = LoongArchReadTime ();
  84. } while (Ticks < End);
  85. return MicroSeconds;
  86. }
  87. /**
  88. Stalls the CPU for at least the given number of nanoseconds.
  89. Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
  90. @param NanoSeconds The minimum number of nanoseconds to delay.
  91. @return NanoSeconds
  92. **/
  93. UINTN
  94. EFIAPI
  95. NanoSecondDelay (
  96. IN UINTN NanoSeconds
  97. )
  98. {
  99. UINT32 MicroSeconds;
  100. if (NanoSeconds % 1000 == 0) {
  101. MicroSeconds = NanoSeconds/1000;
  102. } else {
  103. MicroSeconds = NanoSeconds/1000 + 1;
  104. }
  105. MicroSecondDelay (MicroSeconds);
  106. return NanoSeconds;
  107. }
  108. /**
  109. Retrieves the current value of a 64-bit free running performance counter.
  110. Retrieves the current value of a 64-bit free running performance counter. The
  111. counter can either count up by 1 or count down by 1. If the physical
  112. performance counter counts by a larger increment, then the counter values
  113. must be translated. The properties of the counter can be retrieved from
  114. GetPerformanceCounterProperties ().
  115. @return The current value of the free running performance counter.
  116. **/
  117. UINT64
  118. EFIAPI
  119. GetPerformanceCounter (
  120. VOID
  121. )
  122. {
  123. return LoongArchReadTime ();
  124. }
  125. /**
  126. Retrieves the 64-bit frequency in Hz and the range of performance counter
  127. values.
  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. @param StartValue The value the performance counter starts with when it
  139. rolls over.
  140. @param EndValue The value that the performance counter ends with before
  141. it rolls over.
  142. @return The frequency in Hz.
  143. **/
  144. UINT64
  145. EFIAPI
  146. GetPerformanceCounterProperties (
  147. OUT UINT64 *StartValue, OPTIONAL
  148. OUT UINT64 *EndValue OPTIONAL
  149. )
  150. {
  151. if (StartValue != NULL) {
  152. *StartValue = BIT2;
  153. }
  154. if (EndValue != NULL) {
  155. *EndValue = BIT48 - 1;
  156. }
  157. return GetFreq ();
  158. }
  159. /**
  160. Converts elapsed ticks of performance counter to time in nanoseconds.
  161. This function converts the elapsed ticks of running performance counter to
  162. time value in unit of nanoseconds.
  163. @param Ticks The number of elapsed ticks of running performance counter.
  164. @return The elapsed time in nanoseconds.
  165. **/
  166. UINT64
  167. EFIAPI
  168. GetTimeInNanoSecond (
  169. IN UINT64 Ticks
  170. )
  171. {
  172. UINT64 Frequency;
  173. UINT64 NanoSeconds;
  174. UINT64 Remainder;
  175. INTN Shift;
  176. Frequency = GetPerformanceCounterProperties (NULL, NULL);
  177. //
  178. // Ticks
  179. // Time = --------- x 1,000,000,000
  180. // Frequency
  181. //
  182. NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
  183. //
  184. // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
  185. // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
  186. // i.e. highest bit set in Remainder should <= 33.
  187. //
  188. Shift = MAX (0, HighBitSet64 (Remainder) - 33);
  189. Remainder = RShiftU64 (Remainder, (UINTN) Shift);
  190. Frequency = RShiftU64 (Frequency, (UINTN) Shift);
  191. NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
  192. return NanoSeconds;
  193. }