]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.c
OvmfPkg: AcpiTimerLib: Split into multiple phase-specific instances
[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 <OvmfPlatforms.h>
19
20 //
21 // Power Management PCI Configuration Register fields
22 //
23 #define PMBA_RTE BIT0
24
25 //
26 // Offset in the Power Management Base Address to the ACPI Timer
27 //
28 #define ACPI_TIMER_OFFSET 0x8
29
30 //
31 // Cached ACPI Timer IO Address
32 //
33 STATIC UINT32 mAcpiTimerIoAddr;
34
35 /**
36 The constructor function caches the ACPI tick counter address
37
38 At the time this constructor runs (DXE_CORE or later), ACPI IO space
39 has already been enabled by either PlatformPei or by the "Base"
40 instance of this library.
41 In order to avoid querying the underlying platform type during each
42 tick counter read operation, we cache the counter address during
43 initialization of this instance of the Timer Library.
44
45 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
46
47 **/
48 RETURN_STATUS
49 EFIAPI
50 AcpiTimerLibConstructor (
51 VOID
52 )
53 {
54 UINT16 HostBridgeDevId;
55 UINTN Pmba;
56
57 //
58 // Query Host Bridge DID to determine platform type
59 //
60 HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
61 switch (HostBridgeDevId) {
62 case INTEL_82441_DEVICE_ID:
63 Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);
64 break;
65 case INTEL_Q35_MCH_DEVICE_ID:
66 Pmba = POWER_MGMT_REGISTER_Q35 (0x40);
67 break;
68 default:
69 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
70 __FUNCTION__, HostBridgeDevId));
71 ASSERT (FALSE);
72 return RETURN_UNSUPPORTED;
73 }
74
75 mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;
76
77 return RETURN_SUCCESS;
78 }
79
80 /**
81 Internal function to read the current tick counter of ACPI.
82
83 Read the current ACPI tick counter using the counter address cached
84 by this instance's constructor.
85
86 @return The tick counter read.
87
88 **/
89 UINT32
90 InternalAcpiGetTimerTick (
91 VOID
92 )
93 {
94 //
95 // Return the current ACPI timer value.
96 //
97 return IoRead32 (mAcpiTimerIoAddr);
98 }