DxeAcpiTimerLib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** @file
  2. ACPI Timer implements one instance of Timer Library.
  3. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/TimerLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/HobLib.h>
  10. extern GUID mFrequencyHobGuid;
  11. /**
  12. The constructor function enables ACPI IO space.
  13. If ACPI I/O space not enabled, this function will enable it.
  14. It will always return RETURN_SUCCESS.
  15. @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
  16. **/
  17. RETURN_STATUS
  18. EFIAPI
  19. AcpiTimerLibConstructor (
  20. VOID
  21. );
  22. /**
  23. Calculate TSC frequency.
  24. The TSC counting frequency is determined by comparing how far it counts
  25. during a 101.4 us period as determined by the ACPI timer.
  26. The ACPI timer is used because it counts at a known frequency.
  27. The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
  28. or 101.4 us. The TSC is then sampled again. The difference multiplied by
  29. 9861 is the TSC frequency. There will be a small error because of the
  30. overhead of reading the ACPI timer. An attempt is made to determine and
  31. compensate for this error.
  32. @return The number of TSC counts per second.
  33. **/
  34. UINT64
  35. InternalCalculateTscFrequency (
  36. VOID
  37. );
  38. //
  39. // Cached performance counter frequency
  40. //
  41. UINT64 mPerformanceCounterFrequency = 0;
  42. /**
  43. Internal function to retrieves the 64-bit frequency in Hz.
  44. Internal function to retrieves the 64-bit frequency in Hz.
  45. @return The frequency in Hz.
  46. **/
  47. UINT64
  48. InternalGetPerformanceCounterFrequency (
  49. VOID
  50. )
  51. {
  52. return mPerformanceCounterFrequency;
  53. }
  54. /**
  55. The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
  56. @param ImageHandle The firmware allocated handle for the EFI image.
  57. @param SystemTable A pointer to the EFI System Table.
  58. @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
  59. **/
  60. EFI_STATUS
  61. EFIAPI
  62. DxeAcpiTimerLibConstructor (
  63. IN EFI_HANDLE ImageHandle,
  64. IN EFI_SYSTEM_TABLE *SystemTable
  65. )
  66. {
  67. EFI_HOB_GUID_TYPE *GuidHob;
  68. //
  69. // Enable ACPI IO space.
  70. //
  71. AcpiTimerLibConstructor ();
  72. //
  73. // Initialize PerformanceCounterFrequency
  74. //
  75. GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
  76. if (GuidHob != NULL) {
  77. mPerformanceCounterFrequency = *(UINT64*)GET_GUID_HOB_DATA (GuidHob);
  78. } else {
  79. mPerformanceCounterFrequency = InternalCalculateTscFrequency ();
  80. }
  81. return EFI_SUCCESS;
  82. }