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