]> git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
9ed10ef2e2973ffd239b5cfc622f2866a23184ab
[mirror_edk2.git] / PcAtChipsetPkg / Library / AcpiTimerLib / DxeAcpiTimerLib.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 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/HobLib.h>
19
20 extern GUID mFrequencyHobGuid;
21
22 /**
23 The constructor function enables ACPI IO space.
24
25 If ACPI I/O space not enabled, this function will enable it.
26 It will always return RETURN_SUCCESS.
27
28 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
29
30 **/
31 RETURN_STATUS
32 EFIAPI
33 AcpiTimerLibConstructor (
34 VOID
35 );
36
37 /**
38 Calculate TSC frequency.
39
40 The TSC counting frequency is determined by comparing how far it counts
41 during a 101.4 us period as determined by the ACPI timer.
42 The ACPI timer is used because it counts at a known frequency.
43 The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
44 or 101.4 us. The TSC is then sampled again. The difference multiplied by
45 9861 is the TSC frequency. There will be a small error because of the
46 overhead of reading the ACPI timer. An attempt is made to determine and
47 compensate for this error.
48
49 @return The number of TSC counts per second.
50
51 **/
52 UINT64
53 InternalCalculateTscFrequency (
54 VOID
55 );
56
57 //
58 // Cached performance counter frequency
59 //
60 UINT64 mPerformanceCounterFrequency = 0;
61
62 /**
63 Internal function to retrieves the 64-bit frequency in Hz.
64
65 Internal function to retrieves the 64-bit frequency in Hz.
66
67 @return The frequency in Hz.
68
69 **/
70 UINT64
71 InternalGetPerformanceCounterFrequency (
72 VOID
73 )
74 {
75 return mPerformanceCounterFrequency;
76 }
77
78 /**
79 The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
80
81 @param ImageHandle The firmware allocated handle for the EFI image.
82 @param SystemTable A pointer to the EFI System Table.
83
84 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
85
86 **/
87 EFI_STATUS
88 EFIAPI
89 DxeAcpiTimerLibConstructor (
90 IN EFI_HANDLE ImageHandle,
91 IN EFI_SYSTEM_TABLE *SystemTable
92 )
93 {
94 EFI_HOB_GUID_TYPE *GuidHob;
95
96 //
97 // Enable ACPI IO space.
98 //
99 AcpiTimerLibConstructor ();
100
101 //
102 // Initialize PerformanceCounterFrequency
103 //
104 GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
105 if (GuidHob != NULL) {
106 mPerformanceCounterFrequency = *(UINT64*)GET_GUID_HOB_DATA (GuidHob);
107 } else {
108 mPerformanceCounterFrequency = InternalCalculateTscFrequency ();
109 }
110
111 return EFI_SUCCESS;
112 }