1 | /** @file
|
---|
2 | ACPI Timer implements one instance of Timer Library.
|
---|
3 |
|
---|
4 | Copyright (c) 2013 - 2023, 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 | //
|
---|
15 | // Cached performance counter frequency
|
---|
16 | //
|
---|
17 | static UINT64 mAcpiTimerLibTscFrequency = 0;
|
---|
18 |
|
---|
19 | extern GUID mFrequencyHobGuid;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | The constructor function enables ACPI IO space.
|
---|
23 |
|
---|
24 | If ACPI I/O space not enabled, this function will enable it.
|
---|
25 | It will always return RETURN_SUCCESS.
|
---|
26 |
|
---|
27 | @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
|
---|
28 |
|
---|
29 | **/
|
---|
30 | RETURN_STATUS
|
---|
31 | EFIAPI
|
---|
32 | AcpiTimerLibConstructor (
|
---|
33 | VOID
|
---|
34 | );
|
---|
35 |
|
---|
36 | /**
|
---|
37 | Calculate TSC frequency.
|
---|
38 |
|
---|
39 | The TSC counting frequency is determined by comparing how far it counts
|
---|
40 | during a 101.4 us period as determined by the ACPI timer.
|
---|
41 | The ACPI timer is used because it counts at a known frequency.
|
---|
42 | The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
|
---|
43 | or 101.4 us. The TSC is then sampled again. The difference multiplied by
|
---|
44 | 9861 is the TSC frequency. There will be a small error because of the
|
---|
45 | overhead of reading the ACPI timer. An attempt is made to determine and
|
---|
46 | compensate for this error.
|
---|
47 |
|
---|
48 | @return The number of TSC counts per second.
|
---|
49 |
|
---|
50 | **/
|
---|
51 | UINT64
|
---|
52 | InternalCalculateTscFrequency (
|
---|
53 | VOID
|
---|
54 | );
|
---|
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 mAcpiTimerLibTscFrequency;
|
---|
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 | mAcpiTimerLibTscFrequency = *(UINT64 *)GET_GUID_HOB_DATA (GuidHob);
|
---|
96 | } else {
|
---|
97 | mAcpiTimerLibTscFrequency = InternalCalculateTscFrequency ();
|
---|
98 | }
|
---|
99 |
|
---|
100 | return EFI_SUCCESS;
|
---|
101 | }
|
---|