2 Provide constructor and GetTick for Dxe instance of ACPI Timer Library
4 Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Uefi/UefiBaseType.h>
10 #include <Uefi/UefiMultiPhase.h>
11 #include <Pi/PiBootMode.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>
22 // Cached ACPI Timer IO Address
24 STATIC UINT32 mAcpiTimerIoAddr
;
27 The constructor function caches the ACPI tick counter address
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.
36 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
41 AcpiTimerLibConstructor (
45 UINT16 HostBridgeDevId
;
47 EFI_HOB_GUID_TYPE
*GuidHob
;
48 EFI_HOB_PLATFORM_INFO
*PlatformInfoHob
= NULL
;
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
55 GuidHob
= GetFirstGuidHob (&gUefiOvmfPkgPlatformInfoGuid
);
56 if (GuidHob
!= NULL
) {
57 PlatformInfoHob
= (EFI_HOB_PLATFORM_INFO
*)GET_GUID_HOB_DATA (GuidHob
);
58 HostBridgeDevId
= PlatformInfoHob
->HostBridgeDevId
;
60 DEBUG ((DEBUG_ERROR
, "PlatformInfoHob is not found.\n"));
62 return RETURN_UNSUPPORTED
;
65 switch (HostBridgeDevId
) {
66 case INTEL_82441_DEVICE_ID
:
67 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
69 case INTEL_Q35_MCH_DEVICE_ID
:
70 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
72 case CLOUDHV_DEVICE_ID
:
73 mAcpiTimerIoAddr
= CLOUDHV_ACPI_TIMER_IO_ADDRESS
;
74 return RETURN_SUCCESS
;
78 "%a: Unknown Host Bridge Device ID: 0x%04x\n",
83 return RETURN_UNSUPPORTED
;
86 mAcpiTimerIoAddr
= (PciRead32 (Pmba
) & ~PMBA_RTE
) + ACPI_TIMER_OFFSET
;
88 return RETURN_SUCCESS
;
92 Internal function to read the current tick counter of ACPI.
94 Read the current ACPI tick counter using the counter address cached
95 by this instance's constructor.
97 @return The tick counter read.
101 InternalAcpiGetTimerTick (
106 // Return the current ACPI timer value.
108 return IoRead32 (mAcpiTimerIoAddr
);