PcAtChipsetPkg: Add PeiAcpiTimerLib to save Frequency in HOB
[mirror_edk2.git] / PcAtChipsetPkg / Library / AcpiTimerLib / PeiAcpiTimerLib.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 <PiPei.h>
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/HobLib.h>
19
20 extern GUID mFrequencyHobGuid;
21
22 /**
23 Calculate TSC frequency.
24
25 The TSC counting frequency is determined by comparing how far it counts
26 during a 101.4 us period as determined by the ACPI timer.
27 The ACPI timer is used because it counts at a known frequency.
28 The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
29 or 101.4 us. The TSC is then sampled again. The difference multiplied by
30 9861 is the TSC frequency. There will be a small error because of the
31 overhead of reading the ACPI timer. An attempt is made to determine and
32 compensate for this error.
33
34 @return The number of TSC counts per second.
35
36 **/
37 UINT64
38 InternalCalculateTscFrequency (
39 VOID
40 );
41
42 /**
43 Internal function to retrieves the 64-bit frequency in Hz.
44
45 Internal function to retrieves the 64-bit frequency in Hz.
46
47 @return The frequency in Hz.
48
49 **/
50 UINT64
51 InternalGetPerformanceCounterFrequency (
52 VOID
53 )
54 {
55 UINT64 *PerformanceCounterFrequency;
56 EFI_HOB_GUID_TYPE *GuidHob;
57
58 PerformanceCounterFrequency = NULL;
59 GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
60 if (GuidHob == NULL) {
61 PerformanceCounterFrequency = (UINT64*)BuildGuidHob(&mFrequencyHobGuid, sizeof (*PerformanceCounterFrequency));
62 ASSERT (PerformanceCounterFrequency != NULL);
63 *PerformanceCounterFrequency = InternalCalculateTscFrequency ();
64 } else {
65 PerformanceCounterFrequency = (UINT64*)GET_GUID_HOB_DATA (GuidHob);
66 }
67
68 return *PerformanceCounterFrequency;
69 }