DxeCoreTimerLib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /**
  15. Stalls the CPU for at least the given number of microseconds.
  16. Stalls the CPU for the number of microseconds specified by MicroSeconds.
  17. @param MicroSeconds The minimum number of microseconds to delay.
  18. @return The value of MicroSeconds inputted.
  19. **/
  20. UINTN
  21. EFIAPI
  22. MicroSecondDelay (
  23. IN UINTN MicroSeconds
  24. )
  25. {
  26. return NanoSecondDelay (MicroSeconds * 1000);
  27. }
  28. /**
  29. Stalls the CPU for at least the given number of nanoseconds.
  30. Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
  31. @param NanoSeconds The minimum number of nanoseconds to delay.
  32. @return The value of NanoSeconds inputted.
  33. **/
  34. UINTN
  35. EFIAPI
  36. NanoSecondDelay (
  37. IN UINTN NanoSeconds
  38. )
  39. {
  40. gEmuThunk->Sleep (NanoSeconds);
  41. return NanoSeconds;
  42. }
  43. /**
  44. Retrieves the current value of a 64-bit free running performance counter.
  45. The counter can either count up by 1 or count down by 1. If the physical
  46. performance counter counts by a larger increment, then the counter values
  47. must be translated. The properties of the counter can be retrieved from
  48. GetPerformanceCounterProperties().
  49. @return The current value of the free running performance counter.
  50. **/
  51. UINT64
  52. EFIAPI
  53. GetPerformanceCounter (
  54. VOID
  55. )
  56. {
  57. return gEmuThunk->QueryPerformanceCounter ();
  58. }
  59. /**
  60. Retrieves the 64-bit frequency in Hz and the range of performance counter
  61. values.
  62. If StartValue is not NULL, then the value that the performance counter starts
  63. with immediately after is it rolls over is returned in StartValue. If
  64. EndValue is not NULL, then the value that the performance counter end with
  65. immediately before it rolls over is returned in EndValue. The 64-bit
  66. frequency of the performance counter in Hz is always returned. If StartValue
  67. is less than EndValue, then the performance counter counts up. If StartValue
  68. is greater than EndValue, then the performance counter counts down. For
  69. example, a 64-bit free running counter that counts up would have a StartValue
  70. of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
  71. that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
  72. @param StartValue The value the performance counter starts with when it
  73. rolls over.
  74. @param EndValue The value that the performance counter ends with before
  75. it rolls over.
  76. @return The frequency in Hz.
  77. **/
  78. UINT64
  79. EFIAPI
  80. GetPerformanceCounterProperties (
  81. OUT UINT64 *StartValue OPTIONAL,
  82. OUT UINT64 *EndValue OPTIONAL
  83. )
  84. {
  85. if (StartValue != NULL) {
  86. *StartValue = 0ULL;
  87. }
  88. if (EndValue != NULL) {
  89. *EndValue = (UINT64)-1LL;
  90. }
  91. return gEmuThunk->QueryPerformanceFrequency ();
  92. }
  93. /**
  94. Converts elapsed ticks of performance counter to time in nanoseconds.
  95. This function converts the elapsed ticks of running performance counter to
  96. time value in unit of nanoseconds.
  97. @param Ticks The number of elapsed ticks of running performance counter.
  98. @return The elapsed time in nanoseconds.
  99. **/
  100. UINT64
  101. EFIAPI
  102. GetTimeInNanoSecond (
  103. IN UINT64 Ticks
  104. )
  105. {
  106. UINT64 Frequency;
  107. UINT64 NanoSeconds;
  108. UINT64 Remainder;
  109. INTN Shift;
  110. Frequency = GetPerformanceCounterProperties (NULL, NULL);
  111. //
  112. // Ticks
  113. // Time = --------- x 1,000,000,000
  114. // Frequency
  115. //
  116. NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
  117. //
  118. // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
  119. // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
  120. // i.e. highest bit set in Remainder should <= 33.
  121. //
  122. Shift = MAX (0, HighBitSet64 (Remainder) - 33);
  123. Remainder = RShiftU64 (Remainder, (UINTN)Shift);
  124. Frequency = RShiftU64 (Frequency, (UINTN)Shift);
  125. NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
  126. return NanoSeconds;
  127. }