]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.c
OvmfPkg: consolidate POWER_MGMT_REGISTER_PIIX4() on "I440FxPiix4.h" macros
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / BaseRomAcpiTimerLib.c
CommitLineData
170ef2d9
GS
1/** @file\r
2 Provide constructor and GetTick for BaseRom instance of ACPI Timer Library\r
3\r
4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.\r
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r
6\r
7 This program and the accompanying materials are licensed and made\r
8 available under the terms and conditions of the BSD License which\r
9 accompanies this distribution. The full text of the license may\r
10 be found at http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14**/\r
15\r
16#include <Library/DebugLib.h>\r
17#include <Library/IoLib.h>\r
18#include <Library/PciLib.h>\r
19#include <Library/PcdLib.h>\r
20#include <OvmfPlatforms.h>\r
21\r
22//\r
23// Power Management PCI Configuration Register fields\r
24//\r
e2ab3f81 25#define PMBA_RTE BIT0\r
170ef2d9
GS
26\r
27//\r
28// Offset in the Power Management Base Address to the ACPI Timer\r
29//\r
30#define ACPI_TIMER_OFFSET 0x8\r
31\r
32/**\r
33 The constructor function enables ACPI IO space.\r
34\r
35 If ACPI I/O space not enabled, this function will enable it.\r
36 It will always return RETURN_SUCCESS.\r
37\r
38 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r
39\r
40**/\r
41RETURN_STATUS\r
42EFIAPI\r
43AcpiTimerLibConstructor (\r
44 VOID\r
45 )\r
46{\r
47 UINT16 HostBridgeDevId;\r
48 UINTN Pmba;\r
e2ab3f81
GS
49 UINTN AcpiCtlReg;\r
50 UINT8 AcpiEnBit;\r
170ef2d9
GS
51\r
52 //\r
53 // Query Host Bridge DID to determine platform type\r
54 //\r
55 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
56 switch (HostBridgeDevId) {\r
57 case INTEL_82441_DEVICE_ID:\r
da372167
LE
58 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r
59 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);\r
60 AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;\r
170ef2d9
GS
61 break;\r
62 case INTEL_Q35_MCH_DEVICE_ID:\r
bc9d05d6
LE
63 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
64 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r
65 AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r
170ef2d9
GS
66 break;\r
67 default:\r
68 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
69 __FUNCTION__, HostBridgeDevId));\r
70 ASSERT (FALSE);\r
71 return RETURN_UNSUPPORTED;\r
72 }\r
73\r
74 //\r
75 // Check to see if the Power Management Base Address is already enabled\r
76 //\r
e2ab3f81 77 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r
170ef2d9
GS
78 //\r
79 // If the Power Management Base Address is not programmed,\r
80 // then program the Power Management Base Address from a PCD.\r
81 //\r
82 PciAndThenOr32 (Pmba, (UINT32) ~0xFFC0, PcdGet16 (PcdAcpiPmBaseAddress));\r
83\r
84 //\r
e2ab3f81 85 // Enable PMBA I/O port decodes\r
170ef2d9 86 //\r
e2ab3f81 87 PciOr8 (AcpiCtlReg, AcpiEnBit);\r
170ef2d9
GS
88 }\r
89\r
90 return RETURN_SUCCESS;\r
91}\r
92\r
93/**\r
94 Internal function to read the current tick counter of ACPI.\r
95\r
96 Dynamically compute the address of the ACPI tick counter based on the\r
97 properties of the underlying platform, to avoid relying on global variables.\r
98\r
99 @return The tick counter read.\r
100\r
101**/\r
102UINT32\r
103InternalAcpiGetTimerTick (\r
104 VOID\r
105 )\r
106{\r
107 UINT16 HostBridgeDevId;\r
108 UINTN Pmba;\r
109\r
110 //\r
111 // Query Host Bridge DID to determine platform type\r
112 //\r
113 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
114 switch (HostBridgeDevId) {\r
115 case INTEL_82441_DEVICE_ID:\r
da372167 116 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r
170ef2d9
GS
117 break;\r
118 case INTEL_Q35_MCH_DEVICE_ID:\r
bc9d05d6 119 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
170ef2d9
GS
120 break;\r
121 default:\r
122 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
123 __FUNCTION__, HostBridgeDevId));\r
124 ASSERT (FALSE);\r
125 return 0;\r
126 }\r
127\r
128 //\r
129 // Read PMBA to read and return the current ACPI timer value.\r
130 //\r
131 return IoRead32 ((PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET);\r
132}\r