]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.c
OvmfPkg: Q35: Use correct ACPI PM control register:bit
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / BaseAcpiTimerLib.c
1 /** @file
2 Provide constructor and GetTick for Base instance of ACPI Timer Library
3
4 Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>
5
6 This program and the accompanying materials are licensed and made
7 available under the terms and conditions of the BSD License which
8 accompanies this distribution. The full text of the license may
9 be found at http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14
15 #include <Library/DebugLib.h>
16 #include <Library/IoLib.h>
17 #include <Library/PciLib.h>
18 #include <Library/PcdLib.h>
19 #include <OvmfPlatforms.h>
20
21 //
22 // Power Management PCI Configuration Register fields
23 //
24 #define PMBA_RTE BIT0
25 #define PIIX4_PMIOSE BIT0
26 #define Q35_ACPI_EN BIT7
27
28 //
29 // Offset in the Power Management Base Address to the ACPI Timer
30 //
31 #define ACPI_TIMER_OFFSET 0x8
32
33 //
34 // Cached ACPI Timer IO Address
35 //
36 STATIC UINT32 mAcpiTimerIoAddr;
37
38 /**
39 The constructor function caches the ACPI tick counter address, and,
40 if necessary, enables ACPI IO space.
41
42 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
43
44 **/
45 RETURN_STATUS
46 EFIAPI
47 AcpiTimerLibConstructor (
48 VOID
49 )
50 {
51 UINT16 HostBridgeDevId;
52 UINTN Pmba;
53 UINTN AcpiCtlReg;
54 UINT8 AcpiEnBit;
55
56 //
57 // Query Host Bridge DID to determine platform type
58 //
59 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
60 switch (HostBridgeDevId) {
61 case INTEL_82441_DEVICE_ID:
62 Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);
63 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (0x80); // PMREGMISC
64 AcpiEnBit = PIIX4_PMIOSE;
65 break;
66 case INTEL_Q35_MCH_DEVICE_ID:
67 Pmba = POWER_MGMT_REGISTER_Q35 (0x40);
68 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (0x44); // ACPI_CNTL
69 AcpiEnBit = Q35_ACPI_EN;
70 break;
71 default:
72 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
73 __FUNCTION__, HostBridgeDevId));
74 ASSERT (FALSE);
75 return RETURN_UNSUPPORTED;
76 }
77
78 mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;
79
80 //
81 // Check to see if the Power Management Base Address is already enabled
82 //
83 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {
84 //
85 // If the Power Management Base Address is not programmed,
86 // then program the Power Management Base Address from a PCD.
87 //
88 PciAndThenOr32 (Pmba, (UINT32) ~0xFFC0, PcdGet16 (PcdAcpiPmBaseAddress));
89
90 //
91 // Enable PMBA I/O port decodes
92 //
93 PciOr8 (AcpiCtlReg, AcpiEnBit);
94 }
95
96 return RETURN_SUCCESS;
97 }
98
99 /**
100 Internal function to read the current tick counter of ACPI.
101
102 Read the current ACPI tick counter using the counter address cached
103 by this instance's constructor.
104
105 @return The tick counter read.
106
107 **/
108 UINT32
109 InternalAcpiGetTimerTick (
110 VOID
111 )
112 {
113 //
114 // Return the current ACPI timer value.
115 //
116 return IoRead32 (mAcpiTimerIoAddr);
117 }