CpuTimerLib.c 5.2 KB

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