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