1 | /** @file
|
---|
2 | Provide constructor and GetTick for Dxe instance of ACPI Timer Library
|
---|
3 |
|
---|
4 | Copyright (C) 2014, Gabriel L. Somlo <[email protected]>
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include <Uefi/UefiBaseType.h>
|
---|
10 | #include <Uefi/UefiMultiPhase.h>
|
---|
11 | #include <Pi/PiBootMode.h>
|
---|
12 | #include <Pi/PiHob.h>
|
---|
13 | #include <Library/HobLib.h>
|
---|
14 | #include <Library/DebugLib.h>
|
---|
15 | #include <Library/IoLib.h>
|
---|
16 | #include <Library/PcdLib.h>
|
---|
17 | #include <Library/PciLib.h>
|
---|
18 | #include <Library/PlatformInitLib.h>
|
---|
19 | #include <OvmfPlatforms.h>
|
---|
20 |
|
---|
21 | //
|
---|
22 | // Cached ACPI Timer IO Address
|
---|
23 | //
|
---|
24 | STATIC UINT32 mAcpiTimerIoAddr;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | The constructor function caches the ACPI tick counter address
|
---|
28 |
|
---|
29 | At the time this constructor runs (DXE_CORE or later), ACPI IO space
|
---|
30 | has already been enabled by either PlatformPei or by the "Base"
|
---|
31 | instance of this library.
|
---|
32 | In order to avoid querying the underlying platform type during each
|
---|
33 | tick counter read operation, we cache the counter address during
|
---|
34 | initialization of this instance of the Timer Library.
|
---|
35 |
|
---|
36 | @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
|
---|
37 |
|
---|
38 | **/
|
---|
39 | RETURN_STATUS
|
---|
40 | EFIAPI
|
---|
41 | AcpiTimerLibConstructor (
|
---|
42 | VOID
|
---|
43 | )
|
---|
44 | {
|
---|
45 | UINT16 HostBridgeDevId;
|
---|
46 | UINTN Pmba;
|
---|
47 | EFI_HOB_GUID_TYPE *GuidHob;
|
---|
48 | EFI_HOB_PLATFORM_INFO *PlatformInfoHob = NULL;
|
---|
49 |
|
---|
50 | //
|
---|
51 | // Query Host Bridge DID to determine platform type
|
---|
52 | // Tdx guest stores the HostBridgePciDevId in a GuidHob.
|
---|
53 | // So we first check if this HOB exists
|
---|
54 | //
|
---|
55 | GuidHob = GetFirstGuidHob (&gUefiOvmfPkgPlatformInfoGuid);
|
---|
56 | if (GuidHob != NULL) {
|
---|
57 | PlatformInfoHob = (EFI_HOB_PLATFORM_INFO *)GET_GUID_HOB_DATA (GuidHob);
|
---|
58 | HostBridgeDevId = PlatformInfoHob->HostBridgeDevId;
|
---|
59 | } else {
|
---|
60 | DEBUG ((DEBUG_ERROR, "PlatformInfoHob is not found.\n"));
|
---|
61 | ASSERT (FALSE);
|
---|
62 | return RETURN_UNSUPPORTED;
|
---|
63 | }
|
---|
64 |
|
---|
65 | switch (HostBridgeDevId) {
|
---|
66 | #ifdef VBOX
|
---|
67 | // HACK ALERT! There is no host bridge device in the PCIe chipset, and the same PIIX4 PM device is used.
|
---|
68 | // But there might be some other device at 0:0.0.
|
---|
69 | default:
|
---|
70 | #endif
|
---|
71 | case INTEL_82441_DEVICE_ID:
|
---|
72 | Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);
|
---|
73 | break;
|
---|
74 | #ifndef VBOX
|
---|
75 | case INTEL_Q35_MCH_DEVICE_ID:
|
---|
76 | Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);
|
---|
77 | break;
|
---|
78 | case CLOUDHV_DEVICE_ID:
|
---|
79 | mAcpiTimerIoAddr = CLOUDHV_ACPI_TIMER_IO_ADDRESS;
|
---|
80 | return RETURN_SUCCESS;
|
---|
81 | default:
|
---|
82 | DEBUG ((
|
---|
83 | DEBUG_ERROR,
|
---|
84 | "%a: Unknown Host Bridge Device ID: 0x%04x\n",
|
---|
85 | __func__,
|
---|
86 | HostBridgeDevId
|
---|
87 | ));
|
---|
88 | ASSERT (FALSE);
|
---|
89 | return RETURN_UNSUPPORTED;
|
---|
90 | #endif
|
---|
91 | }
|
---|
92 |
|
---|
93 | mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;
|
---|
94 |
|
---|
95 | return RETURN_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | Internal function to read the current tick counter of ACPI.
|
---|
100 |
|
---|
101 | Read the current ACPI tick counter using the counter address cached
|
---|
102 | by this instance's constructor.
|
---|
103 |
|
---|
104 | @return The tick counter read.
|
---|
105 |
|
---|
106 | **/
|
---|
107 | UINT32
|
---|
108 | InternalAcpiGetTimerTick (
|
---|
109 | VOID
|
---|
110 | )
|
---|
111 | {
|
---|
112 | //
|
---|
113 | // Return the current ACPI timer value.
|
---|
114 | //
|
---|
115 | return IoRead32 (mAcpiTimerIoAddr);
|
---|
116 | }
|
---|