DxeTimerLib.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /** @file
  2. A non-functional instance of the Timer Library.
  3. Copyright (c) 2007 - 2019, 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/EmuThunkLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Library/UefiLib.h>
  13. #include <Protocol/Timer.h>
  14. STATIC UINT64 gTimerPeriod = 0;
  15. STATIC EFI_TIMER_ARCH_PROTOCOL *gTimerAp = NULL;
  16. STATIC EFI_EVENT gTimerEvent = NULL;
  17. STATIC VOID *gRegistration = NULL;
  18. VOID
  19. EFIAPI
  20. RegisterTimerArchProtocol (
  21. IN EFI_EVENT Event,
  22. IN VOID *Context
  23. )
  24. {
  25. EFI_STATUS Status;
  26. Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&gTimerAp);
  27. if (!EFI_ERROR (Status)) {
  28. Status = gTimerAp->GetTimerPeriod (gTimerAp, &gTimerPeriod);
  29. ASSERT_EFI_ERROR (Status);
  30. // Convert to Nanoseconds.
  31. gTimerPeriod = MultU64x32 (gTimerPeriod, 100);
  32. if (gTimerEvent == NULL) {
  33. Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &gTimerEvent);
  34. ASSERT_EFI_ERROR (Status);
  35. }
  36. }
  37. }
  38. /**
  39. Stalls the CPU for at least the given number of microseconds.
  40. Stalls the CPU for the number of microseconds specified by MicroSeconds.
  41. @param MicroSeconds The minimum number of microseconds to delay.
  42. @return The value of MicroSeconds inputted.
  43. **/
  44. UINTN
  45. EFIAPI
  46. MicroSecondDelay (
  47. IN UINTN MicroSeconds
  48. )
  49. {
  50. return NanoSecondDelay (MicroSeconds * 1000);
  51. }
  52. /**
  53. Stalls the CPU for at least the given number of nanoseconds.
  54. Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
  55. @param NanoSeconds The minimum number of nanoseconds to delay.
  56. @return The value of NanoSeconds inputted.
  57. **/
  58. UINTN
  59. EFIAPI
  60. NanoSecondDelay (
  61. IN UINTN NanoSeconds
  62. )
  63. {
  64. EFI_STATUS Status;
  65. UINT64 HundredNanoseconds;
  66. UINTN Index;
  67. if ((gTimerPeriod != 0) &&
  68. ((UINT64)NanoSeconds > gTimerPeriod) &&
  69. (EfiGetCurrentTpl () == TPL_APPLICATION))
  70. {
  71. //
  72. // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core
  73. //
  74. HundredNanoseconds = DivU64x32 (NanoSeconds, 100);
  75. Status = gBS->SetTimer (gTimerEvent, TimerRelative, HundredNanoseconds);
  76. ASSERT_EFI_ERROR (Status);
  77. Status = gBS->WaitForEvent (sizeof (gTimerEvent)/sizeof (EFI_EVENT), &gTimerEvent, &Index);
  78. ASSERT_EFI_ERROR (Status);
  79. } else {
  80. gEmuThunk->Sleep (NanoSeconds);
  81. }
  82. return NanoSeconds;
  83. }
  84. /**
  85. Retrieves the current value of a 64-bit free running performance counter.
  86. The 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 gEmuThunk->QueryPerformanceCounter ();
  99. }
  100. /**
  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 = 0ULL;
  128. }
  129. if (EndValue != NULL) {
  130. *EndValue = (UINT64)-1LL;
  131. }
  132. return gEmuThunk->QueryPerformanceFrequency ();
  133. }
  134. /**
  135. Register for the Timer AP protocol.
  136. @param ImageHandle The firmware allocated handle for the EFI image.
  137. @param SystemTable A pointer to the EFI System Table.
  138. @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
  139. **/
  140. EFI_STATUS
  141. EFIAPI
  142. DxeTimerLibConstructor (
  143. IN EFI_HANDLE ImageHandle,
  144. IN EFI_SYSTEM_TABLE *SystemTable
  145. )
  146. {
  147. EfiCreateProtocolNotifyEvent (
  148. &gEfiTimerArchProtocolGuid,
  149. TPL_CALLBACK,
  150. RegisterTimerArchProtocol,
  151. NULL,
  152. &gRegistration
  153. );
  154. return EFI_SUCCESS;
  155. }
  156. /**
  157. Converts elapsed ticks of performance counter to time in nanoseconds.
  158. This function converts the elapsed ticks of running performance counter to
  159. time value in unit of nanoseconds.
  160. @param Ticks The number of elapsed ticks of running performance counter.
  161. @return The elapsed time in nanoseconds.
  162. **/
  163. UINT64
  164. EFIAPI
  165. GetTimeInNanoSecond (
  166. IN UINT64 Ticks
  167. )
  168. {
  169. UINT64 Frequency;
  170. UINT64 NanoSeconds;
  171. UINT64 Remainder;
  172. INTN Shift;
  173. Frequency = GetPerformanceCounterProperties (NULL, NULL);
  174. //
  175. // Ticks
  176. // Time = --------- x 1,000,000,000
  177. // Frequency
  178. //
  179. NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
  180. //
  181. // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
  182. // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
  183. // i.e. highest bit set in Remainder should <= 33.
  184. //
  185. Shift = MAX (0, HighBitSet64 (Remainder) - 33);
  186. Remainder = RShiftU64 (Remainder, (UINTN)Shift);
  187. Frequency = RShiftU64 (Frequency, (UINTN)Shift);
  188. NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
  189. return NanoSeconds;
  190. }