AcpiTimerLib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /** @file
  2. ACPI Timer implements one instance of Timer Library.
  3. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Library/TimerLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/PciLib.h>
  11. #include <Library/IoLib.h>
  12. #include <Library/DebugLib.h>
  13. #include <IndustryStandard/Acpi.h>
  14. //
  15. // OVERRIDE: OverrideBegin
  16. //
  17. #include <Register/Cpuid.h>
  18. //
  19. // OVERRIDE: OverrideEnd
  20. //
  21. /**
  22. Internal function to retrieves the 64-bit frequency in Hz.
  23. Internal function to retrieves the 64-bit frequency in Hz.
  24. @return The frequency in Hz.
  25. **/
  26. UINT64
  27. InternalGetPerformanceCounterFrequency (
  28. VOID
  29. );
  30. /**
  31. The constructor function enables ACPI IO space.
  32. If ACPI I/O space not enabled, this function will enable it.
  33. It will always return RETURN_SUCCESS.
  34. @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
  35. **/
  36. RETURN_STATUS
  37. EFIAPI
  38. AcpiTimerLibConstructor (
  39. VOID
  40. )
  41. {
  42. UINTN Bus;
  43. UINTN Device;
  44. UINTN Function;
  45. UINTN EnableRegister;
  46. UINT8 EnableMask;
  47. //
  48. // ASSERT for the invalid PCD values. They must be configured to the real value.
  49. //
  50. ASSERT (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0xFFFF);
  51. ASSERT (PcdGet16 (PcdAcpiIoPortBaseAddress) != 0xFFFF);
  52. //
  53. // If the register offset to the BAR for the ACPI I/O Port Base Address is 0x0000, then
  54. // no PCI register programming is required to enable access to the the ACPI registers
  55. // specified by PcdAcpiIoPortBaseAddress
  56. //
  57. if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) == 0x0000) {
  58. return RETURN_SUCCESS;
  59. }
  60. //
  61. // ASSERT for the invalid PCD values. They must be configured to the real value.
  62. //
  63. ASSERT (PcdGet8 (PcdAcpiIoPciDeviceNumber) != 0xFF);
  64. ASSERT (PcdGet8 (PcdAcpiIoPciFunctionNumber) != 0xFF);
  65. ASSERT (PcdGet16 (PcdAcpiIoPciEnableRegisterOffset) != 0xFFFF);
  66. //
  67. // Retrieve the PCD values for the PCI configuration space required to program the ACPI I/O Port Base Address
  68. //
  69. Bus = PcdGet8 (PcdAcpiIoPciBusNumber);
  70. Device = PcdGet8 (PcdAcpiIoPciDeviceNumber);
  71. Function = PcdGet8 (PcdAcpiIoPciFunctionNumber);
  72. EnableRegister = PcdGet16 (PcdAcpiIoPciEnableRegisterOffset);
  73. EnableMask = PcdGet8 (PcdAcpiIoBarEnableMask);
  74. //
  75. // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.
  76. //
  77. if ((PciRead8 (PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister)) & EnableMask) != EnableMask) {
  78. PciWrite16 (
  79. PCI_LIB_ADDRESS (Bus, Device, Function, PcdGet16 (PcdAcpiIoPciBarRegisterOffset)),
  80. PcdGet16 (PcdAcpiIoPortBaseAddress)
  81. );
  82. PciOr8 (
  83. PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister),
  84. EnableMask
  85. );
  86. }
  87. return RETURN_SUCCESS;
  88. }
  89. /**
  90. Internal function to retrieve the ACPI I/O Port Base Address.
  91. Internal function to retrieve the ACPI I/O Port Base Address.
  92. @return The 16-bit ACPI I/O Port Base Address.
  93. **/
  94. UINT16
  95. InternalAcpiGetAcpiTimerIoPort (
  96. VOID
  97. )
  98. {
  99. UINT16 Port;
  100. Port = PcdGet16 (PcdAcpiIoPortBaseAddress);
  101. //
  102. // If the register offset to the BAR for the ACPI I/O Port Base Address is not 0x0000, then
  103. // read the PCI register for the ACPI BAR value in case the BAR has been programmed to a
  104. // value other than PcdAcpiIoPortBaseAddress
  105. //
  106. if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0x0000) {
  107. Port = PciRead16 (PCI_LIB_ADDRESS (
  108. PcdGet8 (PcdAcpiIoPciBusNumber),
  109. PcdGet8 (PcdAcpiIoPciDeviceNumber),
  110. PcdGet8 (PcdAcpiIoPciFunctionNumber),
  111. PcdGet16 (PcdAcpiIoPciBarRegisterOffset)
  112. ));
  113. }
  114. return (Port & PcdGet16 (PcdAcpiIoPortBaseAddressMask)) + PcdGet16 (PcdAcpiPm1TmrOffset);
  115. }
  116. /**
  117. Stalls the CPU for at least the given number of ticks.
  118. Stalls the CPU for at least the given number of ticks. It's invoked by
  119. MicroSecondDelay() and NanoSecondDelay().
  120. @param Delay A period of time to delay in ticks.
  121. **/
  122. VOID
  123. InternalAcpiDelay (
  124. IN UINT32 Delay
  125. )
  126. {
  127. UINT16 Port;
  128. UINT32 Ticks;
  129. UINT32 Times;
  130. Port = InternalAcpiGetAcpiTimerIoPort ();
  131. Times = Delay >> 22;
  132. Delay &= BIT22 - 1;
  133. do {
  134. //
  135. // The target timer count is calculated here
  136. //
  137. Ticks = IoBitFieldRead32 (Port, 0, 23) + Delay;
  138. Delay = BIT22;
  139. //
  140. // Wait until time out
  141. // Delay >= 2^23 could not be handled by this function
  142. // Timer wrap-arounds are handled correctly by this function
  143. //
  144. while (((Ticks - IoBitFieldRead32 (Port, 0, 23)) & BIT23) == 0) {
  145. CpuPause ();
  146. }
  147. } while (Times-- > 0);
  148. }
  149. /**
  150. Stalls the CPU for at least the given number of microseconds.
  151. Stalls the CPU for the number of microseconds specified by MicroSeconds.
  152. @param MicroSeconds The minimum number of microseconds to delay.
  153. @return MicroSeconds
  154. **/
  155. UINTN
  156. EFIAPI
  157. MicroSecondDelay (
  158. IN UINTN MicroSeconds
  159. )
  160. {
  161. InternalAcpiDelay (
  162. (UINT32)DivU64x32 (
  163. MultU64x32 (
  164. MicroSeconds,
  165. ACPI_TIMER_FREQUENCY
  166. ),
  167. 1000000u
  168. )
  169. );
  170. return MicroSeconds;
  171. }
  172. /**
  173. Stalls the CPU for at least the given number of nanoseconds.
  174. Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
  175. @param NanoSeconds The minimum number of nanoseconds to delay.
  176. @return NanoSeconds
  177. **/
  178. UINTN
  179. EFIAPI
  180. NanoSecondDelay (
  181. IN UINTN NanoSeconds
  182. )
  183. {
  184. InternalAcpiDelay (
  185. (UINT32)DivU64x32 (
  186. MultU64x32 (
  187. NanoSeconds,
  188. ACPI_TIMER_FREQUENCY
  189. ),
  190. 1000000000u
  191. )
  192. );
  193. return NanoSeconds;
  194. }
  195. /**
  196. Retrieves the current value of a 64-bit free running performance counter.
  197. Retrieves the current value of a 64-bit free running performance counter. The
  198. counter can either count up by 1 or count down by 1. If the physical
  199. performance counter counts by a larger increment, then the counter values
  200. must be translated. The properties of the counter can be retrieved from
  201. GetPerformanceCounterProperties().
  202. @return The current value of the free running performance counter.
  203. **/
  204. UINT64
  205. EFIAPI
  206. GetPerformanceCounter (
  207. VOID
  208. )
  209. {
  210. return AsmReadTsc ();
  211. }
  212. /**
  213. Retrieves the 64-bit frequency in Hz and the range of performance counter
  214. values.
  215. If StartValue is not NULL, then the value that the performance counter starts
  216. with immediately after is it rolls over is returned in StartValue. If
  217. EndValue is not NULL, then the value that the performance counter end with
  218. immediately before it rolls over is returned in EndValue. The 64-bit
  219. frequency of the performance counter in Hz is always returned. If StartValue
  220. is less than EndValue, then the performance counter counts up. If StartValue
  221. is greater than EndValue, then the performance counter counts down. For
  222. example, a 64-bit free running counter that counts up would have a StartValue
  223. of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
  224. that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
  225. @param StartValue The value the performance counter starts with when it
  226. rolls over.
  227. @param EndValue The value that the performance counter ends with before
  228. it rolls over.
  229. @return The frequency in Hz.
  230. **/
  231. UINT64
  232. EFIAPI
  233. GetPerformanceCounterProperties (
  234. OUT UINT64 *StartValue, OPTIONAL
  235. OUT UINT64 *EndValue OPTIONAL
  236. )
  237. {
  238. if (StartValue != NULL) {
  239. *StartValue = 0;
  240. }
  241. if (EndValue != NULL) {
  242. *EndValue = 0xffffffffffffffffULL;
  243. }
  244. return InternalGetPerformanceCounterFrequency ();
  245. }
  246. /**
  247. Converts elapsed ticks of performance counter to time in nanoseconds.
  248. This function converts the elapsed ticks of running performance counter to
  249. time value in unit of nanoseconds.
  250. @param Ticks The number of elapsed ticks of running performance counter.
  251. @return The elapsed time in nanoseconds.
  252. **/
  253. UINT64
  254. EFIAPI
  255. GetTimeInNanoSecond (
  256. IN UINT64 Ticks
  257. )
  258. {
  259. UINT64 Frequency;
  260. UINT64 NanoSeconds;
  261. UINT64 Remainder;
  262. INTN Shift;
  263. Frequency = GetPerformanceCounterProperties (NULL, NULL);
  264. //
  265. // Ticks
  266. // Time = --------- x 1,000,000,000
  267. // Frequency
  268. //
  269. NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
  270. //
  271. // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
  272. // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
  273. // i.e. highest bit set in Remainder should <= 33.
  274. //
  275. Shift = MAX (0, HighBitSet64 (Remainder) - 33);
  276. Remainder = RShiftU64 (Remainder, (UINTN) Shift);
  277. Frequency = RShiftU64 (Frequency, (UINTN) Shift);
  278. NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
  279. return NanoSeconds;
  280. }
  281. //
  282. // OVERRIDE: OverrideBegin
  283. //
  284. /**
  285. Calculate TSC frequency.
  286. The TSC counting frequency is determined by using CPUID leaf 0x15 that is the preferred
  287. method for Skylake and beyond. Frequency in MHz = Core XTAL frequency * EBX/EAX.
  288. In newer flavors of the CPU, core xtal frequency is returned in ECX (or 0 if not
  289. supported). If ECX is 0, 24MHz is assumed.
  290. @return The number of TSC counts per second.
  291. **/
  292. UINT64
  293. InternalCalculateTscFrequency (
  294. VOID
  295. )
  296. {
  297. UINT64 TscFrequency;
  298. UINT64 CoreXtalFrequency;
  299. UINT32 RegEax;
  300. UINT32 RegEbx;
  301. UINT32 RegEcx;
  302. //
  303. // Use CPUID leaf 0x15.
  304. // TSC frequency = (Core Xtal Frequency) * EBX/EAX. EBX returns 0 if not
  305. // supported. ECX, if non zero, provides Core Xtal Frequency in hertz
  306. // (SDM Dec 2016).
  307. //
  308. AsmCpuid (CPUID_TIME_STAMP_COUNTER, &RegEax, &RegEbx, &RegEcx, NULL);
  309. ASSERT (RegEbx != 0);
  310. //
  311. // If core xtal frequency (ECX) returns 0, it is safe to use 24MHz for post
  312. // Skylake client CPU's.
  313. //
  314. if (RegEcx == 0) {
  315. CoreXtalFrequency = 24000000ul;
  316. } else {
  317. CoreXtalFrequency = (UINT64)RegEcx;
  318. }
  319. //
  320. // Calculate frequency. For integer division, round up/down result
  321. // correctly by adding denominator/2 to the numerator prior to division.
  322. //
  323. TscFrequency = DivU64x32 (MultU64x32 (CoreXtalFrequency, RegEbx) + (UINT64)(RegEax >> 1), RegEax);
  324. return TscFrequency;
  325. }
  326. //
  327. // OVERRIDE: OverrideEnd
  328. //