PeiTimerLib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /** @file
  2. A non-functional instance of the Timer Library.
  3. Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/TimerLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/PeiServicesLib.h>
  11. #include <Ppi/EmuThunk.h>
  12. #include <Protocol/EmuThunk.h>
  13. /**
  14. Stalls the CPU for at least the given number of microseconds.
  15. Stalls the CPU for the number of microseconds specified by MicroSeconds.
  16. @param MicroSeconds The minimum number of microseconds to delay.
  17. @return The value of MicroSeconds inputted.
  18. **/
  19. UINTN
  20. EFIAPI
  21. MicroSecondDelay (
  22. IN UINTN MicroSeconds
  23. )
  24. {
  25. return NanoSecondDelay (MicroSeconds * 1000);
  26. }
  27. /**
  28. Stalls the CPU for at least the given number of nanoseconds.
  29. Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
  30. @param NanoSeconds The minimum number of nanoseconds to delay.
  31. @return The value of NanoSeconds inputted.
  32. **/
  33. UINTN
  34. EFIAPI
  35. NanoSecondDelay (
  36. IN UINTN NanoSeconds
  37. )
  38. {
  39. EMU_THUNK_PPI *ThunkPpi;
  40. EFI_STATUS Status;
  41. EMU_THUNK_PROTOCOL *Thunk;
  42. //
  43. // Locate EmuThunkPpi for
  44. //
  45. Status = PeiServicesLocatePpi (
  46. &gEmuThunkPpiGuid,
  47. 0,
  48. NULL,
  49. (VOID **)&ThunkPpi
  50. );
  51. if (!EFI_ERROR (Status)) {
  52. Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
  53. Thunk->Sleep (NanoSeconds);
  54. return NanoSeconds;
  55. }
  56. return 0;
  57. }
  58. /**
  59. Retrieves the current value of a 64-bit free running performance counter.
  60. The counter can either count up by 1 or count down by 1. If the physical
  61. performance counter counts by a larger increment, then the counter values
  62. must be translated. The properties of the counter can be retrieved from
  63. GetPerformanceCounterProperties().
  64. @return The current value of the free running performance counter.
  65. **/
  66. UINT64
  67. EFIAPI
  68. GetPerformanceCounter (
  69. VOID
  70. )
  71. {
  72. EMU_THUNK_PPI *ThunkPpi;
  73. EFI_STATUS Status;
  74. EMU_THUNK_PROTOCOL *Thunk;
  75. //
  76. // Locate EmuThunkPpi for
  77. //
  78. Status = PeiServicesLocatePpi (
  79. &gEmuThunkPpiGuid,
  80. 0,
  81. NULL,
  82. (VOID **)&ThunkPpi
  83. );
  84. if (!EFI_ERROR (Status)) {
  85. Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
  86. return Thunk->QueryPerformanceCounter ();
  87. }
  88. return 0;
  89. }
  90. /**
  91. Retrieves the 64-bit frequency in Hz and the range of performance counter
  92. values.
  93. If StartValue is not NULL, then the value that the performance counter starts
  94. with immediately after is it rolls over is returned in StartValue. If
  95. EndValue is not NULL, then the value that the performance counter end with
  96. immediately before it rolls over is returned in EndValue. The 64-bit
  97. frequency of the performance counter in Hz is always returned. If StartValue
  98. is less than EndValue, then the performance counter counts up. If StartValue
  99. is greater than EndValue, then the performance counter counts down. For
  100. example, a 64-bit free running counter that counts up would have a StartValue
  101. of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
  102. that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
  103. @param StartValue The value the performance counter starts with when it
  104. rolls over.
  105. @param EndValue The value that the performance counter ends with before
  106. it rolls over.
  107. @return The frequency in Hz.
  108. **/
  109. UINT64
  110. EFIAPI
  111. GetPerformanceCounterProperties (
  112. OUT UINT64 *StartValue OPTIONAL,
  113. OUT UINT64 *EndValue OPTIONAL
  114. )
  115. {
  116. EMU_THUNK_PPI *ThunkPpi;
  117. EFI_STATUS Status;
  118. EMU_THUNK_PROTOCOL *Thunk;
  119. //
  120. // Locate EmuThunkPpi for
  121. //
  122. Status = PeiServicesLocatePpi (
  123. &gEmuThunkPpiGuid,
  124. 0,
  125. NULL,
  126. (VOID **)&ThunkPpi
  127. );
  128. if (!EFI_ERROR (Status)) {
  129. if (StartValue != NULL) {
  130. *StartValue = 0ULL;
  131. }
  132. if (EndValue != NULL) {
  133. *EndValue = (UINT64)-1LL;
  134. }
  135. Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
  136. return Thunk->QueryPerformanceFrequency ();
  137. }
  138. return 0;
  139. }
  140. /**
  141. Converts elapsed ticks of performance counter to time in nanoseconds.
  142. This function converts the elapsed ticks of running performance counter to
  143. time value in unit of nanoseconds.
  144. @param Ticks The number of elapsed ticks of running performance counter.
  145. @return The elapsed time in nanoseconds.
  146. **/
  147. UINT64
  148. EFIAPI
  149. GetTimeInNanoSecond (
  150. IN UINT64 Ticks
  151. )
  152. {
  153. UINT64 Frequency;
  154. UINT64 NanoSeconds;
  155. UINT64 Remainder;
  156. INTN Shift;
  157. Frequency = GetPerformanceCounterProperties (NULL, NULL);
  158. //
  159. // Ticks
  160. // Time = --------- x 1,000,000,000
  161. // Frequency
  162. //
  163. NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
  164. //
  165. // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
  166. // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
  167. // i.e. highest bit set in Remainder should <= 33.
  168. //
  169. Shift = MAX (0, HighBitSet64 (Remainder) - 33);
  170. Remainder = RShiftU64 (Remainder, (UINTN)Shift);
  171. Frequency = RShiftU64 (Frequency, (UINTN)Shift);
  172. NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
  173. return NanoSeconds;
  174. }