PeiAcpiTimerLib.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <PiPei.h>
  7. #include <Library/TimerLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/HobLib.h>
  10. #include <Library/DebugLib.h>
  11. extern GUID mFrequencyHobGuid;
  12. /**
  13. Calculate TSC frequency.
  14. The TSC counting frequency is determined by comparing how far it counts
  15. during a 101.4 us period as determined by the ACPI timer.
  16. The ACPI timer is used because it counts at a known frequency.
  17. The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
  18. or 101.4 us. The TSC is then sampled again. The difference multiplied by
  19. 9861 is the TSC frequency. There will be a small error because of the
  20. overhead of reading the ACPI timer. An attempt is made to determine and
  21. compensate for this error.
  22. @return The number of TSC counts per second.
  23. **/
  24. UINT64
  25. InternalCalculateTscFrequency (
  26. VOID
  27. );
  28. /**
  29. Internal function to retrieves the 64-bit frequency in Hz.
  30. Internal function to retrieves the 64-bit frequency in Hz.
  31. @return The frequency in Hz.
  32. **/
  33. UINT64
  34. InternalGetPerformanceCounterFrequency (
  35. VOID
  36. )
  37. {
  38. UINT64 *PerformanceCounterFrequency;
  39. EFI_HOB_GUID_TYPE *GuidHob;
  40. PerformanceCounterFrequency = NULL;
  41. GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
  42. if (GuidHob == NULL) {
  43. PerformanceCounterFrequency = (UINT64*)BuildGuidHob(&mFrequencyHobGuid, sizeof (*PerformanceCounterFrequency));
  44. ASSERT (PerformanceCounterFrequency != NULL);
  45. *PerformanceCounterFrequency = InternalCalculateTscFrequency ();
  46. } else {
  47. PerformanceCounterFrequency = (UINT64*)GET_GUID_HOB_DATA (GuidHob);
  48. }
  49. return *PerformanceCounterFrequency;
  50. }