]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.c
OvmfPkg: consolidate POWER_MGMT_REGISTER_Q35() on "Q35MchIch9.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
GS
25#define PMBA_RTE BIT0\r
26#define PIIX4_PMIOSE BIT0\r
170ef2d9
GS
27\r
28//\r
29// Offset in the Power Management Base Address to the ACPI Timer\r
30//\r
31#define ACPI_TIMER_OFFSET 0x8\r
32\r
33/**\r
34 The constructor function enables ACPI IO space.\r
35\r
36 If ACPI I/O space not enabled, this function will enable it.\r
37 It will always return RETURN_SUCCESS.\r
38\r
39 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r
40\r
41**/\r
42RETURN_STATUS\r
43EFIAPI\r
44AcpiTimerLibConstructor (\r
45 VOID\r
46 )\r
47{\r
48 UINT16 HostBridgeDevId;\r
49 UINTN Pmba;\r
e2ab3f81
GS
50 UINTN AcpiCtlReg;\r
51 UINT8 AcpiEnBit;\r
170ef2d9
GS
52\r
53 //\r
54 // Query Host Bridge DID to determine platform type\r
55 //\r
56 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
57 switch (HostBridgeDevId) {\r
58 case INTEL_82441_DEVICE_ID:\r
e2ab3f81
GS
59 Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);\r
60 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (0x80); // PMREGMISC\r
61 AcpiEnBit = PIIX4_PMIOSE;\r
170ef2d9
GS
62 break;\r
63 case INTEL_Q35_MCH_DEVICE_ID:\r
bc9d05d6
LE
64 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
65 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r
66 AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r
170ef2d9
GS
67 break;\r
68 default:\r
69 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
70 __FUNCTION__, HostBridgeDevId));\r
71 ASSERT (FALSE);\r
72 return RETURN_UNSUPPORTED;\r
73 }\r
74\r
75 //\r
76 // Check to see if the Power Management Base Address is already enabled\r
77 //\r
e2ab3f81 78 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r
170ef2d9
GS
79 //\r
80 // If the Power Management Base Address is not programmed,\r
81 // then program the Power Management Base Address from a PCD.\r
82 //\r
83 PciAndThenOr32 (Pmba, (UINT32) ~0xFFC0, PcdGet16 (PcdAcpiPmBaseAddress));\r
84\r
85 //\r
e2ab3f81 86 // Enable PMBA I/O port decodes\r
170ef2d9 87 //\r
e2ab3f81 88 PciOr8 (AcpiCtlReg, AcpiEnBit);\r
170ef2d9
GS
89 }\r
90\r
91 return RETURN_SUCCESS;\r
92}\r
93\r
94/**\r
95 Internal function to read the current tick counter of ACPI.\r
96\r
97 Dynamically compute the address of the ACPI tick counter based on the\r
98 properties of the underlying platform, to avoid relying on global variables.\r
99\r
100 @return The tick counter read.\r
101\r
102**/\r
103UINT32\r
104InternalAcpiGetTimerTick (\r
105 VOID\r
106 )\r
107{\r
108 UINT16 HostBridgeDevId;\r
109 UINTN Pmba;\r
110\r
111 //\r
112 // Query Host Bridge DID to determine platform type\r
113 //\r
114 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
115 switch (HostBridgeDevId) {\r
116 case INTEL_82441_DEVICE_ID:\r
117 Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);\r
118 break;\r
119 case INTEL_Q35_MCH_DEVICE_ID:\r
bc9d05d6 120 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
170ef2d9
GS
121 break;\r
122 default:\r
123 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
124 __FUNCTION__, HostBridgeDevId));\r
125 ASSERT (FALSE);\r
126 return 0;\r
127 }\r
128\r
129 //\r
130 // Read PMBA to read and return the current ACPI timer value.\r
131 //\r
132 return IoRead32 ((PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET);\r
133}\r