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