]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
OvmfPkg: Update DxeAcpiTimerLib to read HostBridgeDevId in PlatformInfoHob
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / DxeAcpiTimerLib.c
1 /** @file
2 Provide constructor and GetTick for Dxe instance of ACPI Timer Library
3
4 Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>
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 case INTEL_82441_DEVICE_ID:
67 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);
68 break;
69 case INTEL_Q35_MCH_DEVICE_ID:
70 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);
71 break;
72 case CLOUDHV_DEVICE_ID:
73 mAcpiTimerIoAddr = CLOUDHV_ACPI_TIMER_IO_ADDRESS;
74 return RETURN_SUCCESS;
75 default:
76 DEBUG ((
77 DEBUG_ERROR,
78 "%a: Unknown Host Bridge Device ID: 0x%04x\n",
79 __FUNCTION__,
80 HostBridgeDevId
81 ));
82 ASSERT (FALSE);
83 return RETURN_UNSUPPORTED;
84 }
85
86 mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;
87
88 return RETURN_SUCCESS;
89 }
90
91 /**
92 Internal function to read the current tick counter of ACPI.
93
94 Read the current ACPI tick counter using the counter address cached
95 by this instance's constructor.
96
97 @return The tick counter read.
98
99 **/
100 UINT32
101 InternalAcpiGetTimerTick (
102 VOID
103 )
104 {
105 //
106 // Return the current ACPI timer value.
107 //
108 return IoRead32 (mAcpiTimerIoAddr);
109 }