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