]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
OvmfPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / DxeAcpiTimerLib.c
1 /** @file
2 Provide constructor and GetTick for Dxe instance of ACPI Timer Library
3
4 Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include <Library/DebugLib.h>
10 #include <Library/IoLib.h>
11 #include <Library/PcdLib.h>
12 #include <Library/PciLib.h>
13 #include <OvmfPlatforms.h>
14
15 //
16 // Cached ACPI Timer IO Address
17 //
18 STATIC UINT32 mAcpiTimerIoAddr;
19
20 /**
21 The constructor function caches the ACPI tick counter address
22
23 At the time this constructor runs (DXE_CORE or later), ACPI IO space
24 has already been enabled by either PlatformPei or by the "Base"
25 instance of this library.
26 In order to avoid querying the underlying platform type during each
27 tick counter read operation, we cache the counter address during
28 initialization of this instance of the Timer Library.
29
30 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
31
32 **/
33 RETURN_STATUS
34 EFIAPI
35 AcpiTimerLibConstructor (
36 VOID
37 )
38 {
39 UINT16 HostBridgeDevId;
40 UINTN Pmba;
41
42 //
43 // Query Host Bridge DID to determine platform type
44 //
45 HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);
46 switch (HostBridgeDevId) {
47 case INTEL_82441_DEVICE_ID:
48 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);
49 break;
50 case INTEL_Q35_MCH_DEVICE_ID:
51 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);
52 break;
53 default:
54 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
55 __FUNCTION__, HostBridgeDevId));
56 ASSERT (FALSE);
57 return RETURN_UNSUPPORTED;
58 }
59
60 mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;
61
62 return RETURN_SUCCESS;
63 }
64
65 /**
66 Internal function to read the current tick counter of ACPI.
67
68 Read the current ACPI tick counter using the counter address cached
69 by this instance's constructor.
70
71 @return The tick counter read.
72
73 **/
74 UINT32
75 InternalAcpiGetTimerTick (
76 VOID
77 )
78 {
79 //
80 // Return the current ACPI timer value.
81 //
82 return IoRead32 (mAcpiTimerIoAddr);
83 }