OvmfPkg: AcpiTimerLib: Split into multiple phase-specific instances

Remove local power management register access macros in favor of
factored-out ones in OvmfPkg/Include/OvmfPlatforms.h

Next, AcpiTimerLib is split out into three instances, for use during
various stages:

  - BaseRom: used during SEC, PEI_CORE, and PEIM;
  - Dxe:     used during DXE_DRIVER and DXE_RUNTIME_DRIVER;
  - Base:    used by default during all other stages.

Most of the code remains in AcpiTimerLib.c, to be shared by all
instances. The two platform-dependent methods (constructor and
InternalAcpiGetTimerTick) are provided separately by source files
specific to each instance, namely [BaseRom|Base|Dxe]AcpiTimerLib.c.

Since pre-DXE stages can't rely on storing data in global variables,
methods specific to the "BaseRom" instance will call platform
detection macros each time they're invoked.

The "Base" instance calls platform detection macros only from its
constructor, and caches the address required by InternalAcpiTimerTick
in a global variable.

The "Dxe" instance is very similar to "Base", except no platform
detection macros are called at all; instead, the platform type is
read via a dynamic PCD set from PlatformPei.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Gerd Hoffmann <kraxel@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16376 6f19259b-4bc3-4df7-8a09-765794883524
diff --git a/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c b/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
index 7d324cb..938b77c 100644
--- a/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
+++ b/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
@@ -14,160 +14,17 @@
 

 **/

 

-#include <Base.h>

-#include <Library/TimerLib.h>

-#include <Library/BaseLib.h>

-#include <Library/IoLib.h>

-#include <Library/PciLib.h>

 #include <Library/DebugLib.h>

-#include <Library/PcdLib.h>

-#include <IndustryStandard/Pci22.h>

+#include <Library/BaseLib.h>

 #include <IndustryStandard/Acpi.h>

 

-//

-// PCI Location of PIIX4 Power Management PCI Configuration Registers

-//

-#define PIIX4_POWER_MANAGEMENT_BUS       0x00

-#define PIIX4_POWER_MANAGEMENT_DEVICE    0x01

-#define PIIX4_POWER_MANAGEMENT_FUNCTION  0x03

-

-//

-// Macro to access PIIX4 Power Management PCI Configuration Registers

-//

-#define PIIX4_PCI_POWER_MANAGEMENT_REGISTER(Register) \

-  PCI_LIB_ADDRESS (                                   \

-    PIIX4_POWER_MANAGEMENT_BUS,                       \

-    PIIX4_POWER_MANAGEMENT_DEVICE,                    \

-    PIIX4_POWER_MANAGEMENT_FUNCTION,                  \

-    Register                                          \

-    )

-

-//

-// PCI Location of Q35 Power Management PCI Configuration Registers

-//

-#define Q35_POWER_MANAGEMENT_BUS       0x00

-#define Q35_POWER_MANAGEMENT_DEVICE    0x1f

-#define Q35_POWER_MANAGEMENT_FUNCTION  0x00

-

-//

-// Macro to access Q35 Power Management PCI Configuration Registers

-//

-#define Q35_PCI_POWER_MANAGEMENT_REGISTER(Register) \

-  PCI_LIB_ADDRESS (                                 \

-    Q35_POWER_MANAGEMENT_BUS,                       \

-    Q35_POWER_MANAGEMENT_DEVICE,                    \

-    Q35_POWER_MANAGEMENT_FUNCTION,                  \

-    Register                                        \

-    )

-

-//

-// PCI Location of Host Bridge PCI Configuration Registers

-//

-#define HOST_BRIDGE_BUS       0x00

-#define HOST_BRIDGE_DEVICE    0x00

-#define HOST_BRIDGE_FUNCTION  0x00

-

-//

-// Macro to access Host Bridge Configuration Registers

-//

-#define HOST_BRIDGE_REGISTER(Register) \

-  PCI_LIB_ADDRESS (                    \

-    HOST_BRIDGE_BUS,                   \

-    HOST_BRIDGE_DEVICE,                \

-    HOST_BRIDGE_FUNCTION,              \

-    Register                           \

-    )

-

-//

-// Host Bridge Device ID (DID) Register

-//

-#define HOST_BRIDGE_DID  HOST_BRIDGE_REGISTER (0x02)

-

-//

-// Host Bridge DID Register values

-//

-#define PCI_DEVICE_ID_INTEL_82441    0x1237  // DID value for PIIX4

-#define PCI_DEVICE_ID_INTEL_Q35_MCH  0x29C0  // DID value for Q35

-

-//

-// Access Power Management PCI Config Regs based on Host Bridge type

-//

-#define PCI_POWER_MANAGEMENT_REGISTER(Register)                   \

-  ((PciRead16 (HOST_BRIDGE_DID) == PCI_DEVICE_ID_INTEL_Q35_MCH) ? \

-    Q35_PCI_POWER_MANAGEMENT_REGISTER (Register) :                \

-    PIIX4_PCI_POWER_MANAGEMENT_REGISTER (Register))

-

-//

-// Power Management PCI Configuration Registers

-//

-#define PMBA                PCI_POWER_MANAGEMENT_REGISTER (0x40)

-#define   PMBA_RTE          BIT0

-#define PMREGMISC           PCI_POWER_MANAGEMENT_REGISTER (0x80)

-#define   PMIOSE            BIT0

+#include "AcpiTimerLib.h"

 

 //

 // The ACPI Time is a 24-bit counter

 //

 #define ACPI_TIMER_COUNT_SIZE  BIT24

 

-//

-// Offset in the Power Management Base Address to the ACPI Timer

-//

-#define ACPI_TIMER_OFFSET      0x8

-

-/**

-  The constructor function enables ACPI IO space.

-

-  If ACPI I/O space not enabled, this function will enable it.

-  It will always return RETURN_SUCCESS.

-

-  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

-

-**/

-RETURN_STATUS

-EFIAPI

-AcpiTimerLibConstructor (

-  VOID

-  )

-{

-  //

-  // Check to see if the Power Management Base Address is already enabled

-  //

-  if ((PciRead8 (PMREGMISC) & PMIOSE) == 0) {

-    //

-    // If the Power Management Base Address is not programmed,

-    // then program the Power Management Base Address from a PCD.

-    //

-    PciAndThenOr32 (PMBA, (UINT32)(~0x0000FFC0), PcdGet16 (PcdAcpiPmBaseAddress));

-

-    //

-    // Enable PMBA I/O port decodes in PMREGMISC

-    //

-    PciOr8 (PMREGMISC, PMIOSE);

-  }

-  

-  return RETURN_SUCCESS;

-}

-

-/**

-  Internal function to read the current tick counter of ACPI.

-

-  Internal function to read the current tick counter of ACPI.

-

-  @return The tick counter read.

-

-**/

-UINT32

-InternalAcpiGetTimerTick (

-  VOID

-  )

-{

-  //

-  //   Read PMBA to read and return the current ACPI timer value.

-  //

-  return IoRead32 ((PciRead32 (PMBA) & ~PMBA_RTE) + ACPI_TIMER_OFFSET);

-}

-

 /**

   Stalls the CPU for at least the given number of ticks.

 

diff --git a/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.h b/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.h
new file mode 100644
index 0000000..475da70
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.h
@@ -0,0 +1,29 @@
+/** @file

+  Internal definitions for ACPI Timer Library

+

+  Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>

+

+  This program and the accompanying materials are licensed and made

+  available under the terms and conditions of the BSD License which

+  accompanies this distribution.   The full text of the license may

+  be found at http://opensource.org/licenses/bsd-license.php

+

+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+**/

+

+#ifndef _ACPI_TIMER_LIB_INTERNAL_H_

+#define _ACPI_TIMER_LIB_INTERNAL_H_

+

+/**

+  Internal function to read the current tick counter of ACPI.

+

+  @return The tick counter read.

+

+**/

+UINT32

+InternalAcpiGetTimerTick (

+  VOID

+  );

+

+#endif // _ACPI_TIMER_LIB_INTERNAL_H_

diff --git a/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf b/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf
deleted file mode 100644
index da88668..0000000
--- a/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf
+++ /dev/null
@@ -1,44 +0,0 @@
-## @file

-#  ACPI Timer Library Instance.

-#

-#  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>

-#  This program and the accompanying materials

-#  are licensed and made available under the terms and conditions of the BSD License

-#  which accompanies this distribution.  The full text of the license may be found at

-#  http://opensource.org/licenses/bsd-license.php

-#

-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

-#

-##

-

-[Defines]

-  INF_VERSION                    = 0x00010005

-  BASE_NAME                      = AcpiTimerLib

-  FILE_GUID                      = CDD9D74F-213E-4c28-98F7-8B4A167DB936

-  MODULE_TYPE                    = BASE

-  VERSION_STRING                 = 1.0

-  LIBRARY_CLASS                  = TimerLib

-

-  CONSTRUCTOR                    = AcpiTimerLibConstructor

-

-#

-# The following information is for reference only and not required by the build tools.

-#

-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC

-#

-

-[Sources]

-  AcpiTimerLib.c

-

-[Packages]

-  MdePkg/MdePkg.dec

-  OvmfPkg/OvmfPkg.dec

-

-[Pcd]

-  gUefiOvmfPkgTokenSpaceGuid.PcdAcpiPmBaseAddress

-

-[LibraryClasses]

-  BaseLib

-  PciLib

-  IoLib

diff --git a/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.c b/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.c
new file mode 100644
index 0000000..05b0257
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.c
@@ -0,0 +1,98 @@
+/** @file

+  Provide constructor and GetTick for Base instance of ACPI Timer Library

+

+  Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>

+

+  This program and the accompanying materials are licensed and made

+  available under the terms and conditions of the BSD License which

+  accompanies this distribution.   The full text of the license may

+  be found at http://opensource.org/licenses/bsd-license.php

+

+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+**/

+

+#include <Library/DebugLib.h>

+#include <Library/IoLib.h>

+#include <Library/PciLib.h>

+#include <OvmfPlatforms.h>

+

+//

+// Power Management PCI Configuration Register fields

+//

+#define PMBA_RTE  BIT0

+

+//

+// Offset in the Power Management Base Address to the ACPI Timer

+//

+#define ACPI_TIMER_OFFSET  0x8

+

+//

+// Cached ACPI Timer IO Address

+//

+STATIC UINT32 mAcpiTimerIoAddr;

+

+/**

+  The constructor function caches the ACPI tick counter address

+

+  At the time this constructor runs (DXE_CORE or later), ACPI IO space

+  has already been enabled by either PlatformPei or by the "Base"

+  instance of this library.

+  In order to avoid querying the underlying platform type during each

+  tick counter read operation, we cache the counter address during

+  initialization of this instance of the Timer Library.

+

+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

+

+**/

+RETURN_STATUS

+EFIAPI

+AcpiTimerLibConstructor (

+  VOID

+  )

+{

+  UINT16 HostBridgeDevId;

+  UINTN Pmba;

+

+  //

+  // Query Host Bridge DID to determine platform type

+  //

+  HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);

+  switch (HostBridgeDevId) {

+    case INTEL_82441_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);

+      break;

+    case INTEL_Q35_MCH_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_Q35 (0x40);

+      break;

+    default:

+      DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",

+        __FUNCTION__, HostBridgeDevId));

+      ASSERT (FALSE);

+      return RETURN_UNSUPPORTED;

+  }

+

+  mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;

+

+  return RETURN_SUCCESS;

+}

+

+/**

+  Internal function to read the current tick counter of ACPI.

+

+  Read the current ACPI tick counter using the counter address cached

+  by this instance's constructor.

+

+  @return The tick counter read.

+

+**/

+UINT32

+InternalAcpiGetTimerTick (

+  VOID

+  )

+{

+  //

+  //   Return the current ACPI timer value.

+  //

+  return IoRead32 (mAcpiTimerIoAddr);

+}

diff --git a/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf b/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf
new file mode 100644
index 0000000..35c6edc
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf
@@ -0,0 +1,37 @@
+## @file

+#  Base ACPI Timer Library Instance.

+#

+#  Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>

+#  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.

+#

+#  This program and the accompanying materials are licensed and made

+#  available under the terms and conditions of the BSD License which

+#  accompanies this distribution.   The full text of the license may

+#  be found at http://opensource.org/licenses/bsd-license.php

+#

+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+#

+##

+

+[Defines]

+  INF_VERSION    = 0x00010005

+  BASE_NAME      = BaseAcpiTimerLib

+  FILE_GUID      = FB648CF5-91BE-4737-9023-FD807AC6D96D

+  MODULE_TYPE    = BASE

+  VERSION_STRING = 1.0

+  LIBRARY_CLASS  = TimerLib|DXE_CORE DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION SMM_CORE

+  CONSTRUCTOR    = AcpiTimerLibConstructor

+

+[Sources]

+  AcpiTimerLib.c

+  BaseAcpiTimerLib.c

+

+[Packages]

+  MdePkg/MdePkg.dec

+  OvmfPkg/OvmfPkg.dec

+

+[LibraryClasses]

+  BaseLib

+  PciLib

+  IoLib

diff --git a/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.c b/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.c
new file mode 100644
index 0000000..05b14d7
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.c
@@ -0,0 +1,130 @@
+/** @file

+  Provide constructor and GetTick for BaseRom instance of ACPI Timer Library

+

+  Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.

+  Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>

+

+  This program and the accompanying materials are licensed and made

+  available under the terms and conditions of the BSD License which

+  accompanies this distribution.   The full text of the license may

+  be found at http://opensource.org/licenses/bsd-license.php

+

+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+**/

+

+#include <Library/DebugLib.h>

+#include <Library/IoLib.h>

+#include <Library/PciLib.h>

+#include <Library/PcdLib.h>

+#include <OvmfPlatforms.h>

+

+//

+// Power Management PCI Configuration Register fields

+//

+#define PMBA_RTE  BIT0

+#define PMIOSE    BIT0

+

+//

+// Offset in the Power Management Base Address to the ACPI Timer

+//

+#define ACPI_TIMER_OFFSET      0x8

+

+/**

+  The constructor function enables ACPI IO space.

+

+  If ACPI I/O space not enabled, this function will enable it.

+  It will always return RETURN_SUCCESS.

+

+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

+

+**/

+RETURN_STATUS

+EFIAPI

+AcpiTimerLibConstructor (

+  VOID

+  )

+{

+  UINT16 HostBridgeDevId;

+  UINTN Pmba;

+  UINTN PmRegMisc;

+

+  //

+  // Query Host Bridge DID to determine platform type

+  //

+  HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);

+  switch (HostBridgeDevId) {

+    case INTEL_82441_DEVICE_ID:

+      Pmba      = POWER_MGMT_REGISTER_PIIX4 (0x40);

+      PmRegMisc = POWER_MGMT_REGISTER_PIIX4 (0x80);

+      break;

+    case INTEL_Q35_MCH_DEVICE_ID:

+      Pmba      = POWER_MGMT_REGISTER_Q35 (0x40);

+      PmRegMisc = POWER_MGMT_REGISTER_Q35 (0x80);

+      break;

+    default:

+      DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",

+        __FUNCTION__, HostBridgeDevId));

+      ASSERT (FALSE);

+      return RETURN_UNSUPPORTED;

+  }

+

+  //

+  // Check to see if the Power Management Base Address is already enabled

+  //

+  if ((PciRead8 (PmRegMisc) & PMIOSE) == 0) {

+    //

+    // If the Power Management Base Address is not programmed,

+    // then program the Power Management Base Address from a PCD.

+    //

+    PciAndThenOr32 (Pmba, (UINT32) ~0xFFC0, PcdGet16 (PcdAcpiPmBaseAddress));

+

+    //

+    // Enable PMBA I/O port decodes in PMREGMISC

+    //

+    PciOr8 (PmRegMisc, PMIOSE);

+  }

+

+  return RETURN_SUCCESS;

+}

+

+/**

+  Internal function to read the current tick counter of ACPI.

+

+  Dynamically compute the address of the ACPI tick counter based on the

+  properties of the underlying platform, to avoid relying on global variables.

+

+  @return The tick counter read.

+

+**/

+UINT32

+InternalAcpiGetTimerTick (

+  VOID

+  )

+{

+  UINT16 HostBridgeDevId;

+  UINTN Pmba;

+

+  //

+  // Query Host Bridge DID to determine platform type

+  //

+  HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);

+  switch (HostBridgeDevId) {

+    case INTEL_82441_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);

+      break;

+    case INTEL_Q35_MCH_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_Q35 (0x40);

+      break;

+    default:

+      DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",

+        __FUNCTION__, HostBridgeDevId));

+      ASSERT (FALSE);

+      return 0;

+  }

+

+  //

+  //   Read PMBA to read and return the current ACPI timer value.

+  //

+  return IoRead32 ((PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET);

+}

diff --git a/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf b/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf
new file mode 100644
index 0000000..2a3016f
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf
@@ -0,0 +1,39 @@
+## @file

+#  BaseRom ACPI Timer Library Instance.

+#

+#  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.

+#

+#  This program and the accompanying materials are licensed and made

+#  available under the terms and conditions of the BSD License which

+#  accompanies this distribution.   The full text of the license may

+#  be found at http://opensource.org/licenses/bsd-license.php

+#

+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+#

+##

+

+[Defines]

+  INF_VERSION    = 0x00010005

+  BASE_NAME      = BaseRomAcpiTimerLib

+  FILE_GUID      = CDD9D74F-213E-4c28-98F7-8B4A167DB936

+  MODULE_TYPE    = BASE

+  VERSION_STRING = 1.0

+  LIBRARY_CLASS  = TimerLib|SEC PEI_CORE PEIM

+  CONSTRUCTOR    = AcpiTimerLibConstructor

+

+[Sources]

+  AcpiTimerLib.c

+  BaseRomAcpiTimerLib.c

+

+[Packages]

+  MdePkg/MdePkg.dec

+  OvmfPkg/OvmfPkg.dec

+

+[Pcd]

+  gUefiOvmfPkgTokenSpaceGuid.PcdAcpiPmBaseAddress

+

+[LibraryClasses]

+  BaseLib

+  PciLib

+  IoLib

diff --git a/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c b/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
new file mode 100644
index 0000000..2be0793
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
@@ -0,0 +1,99 @@
+/** @file

+  Provide constructor and GetTick for Dxe instance of ACPI Timer Library

+

+  Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>

+

+  This program and the accompanying materials are licensed and made

+  available under the terms and conditions of the BSD License which

+  accompanies this distribution.   The full text of the license may

+  be found at http://opensource.org/licenses/bsd-license.php

+

+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+**/

+

+#include <Library/DebugLib.h>

+#include <Library/IoLib.h>

+#include <Library/PcdLib.h>

+#include <Library/PciLib.h>

+#include <OvmfPlatforms.h>

+

+//

+// Power Management PCI Configuration Register fields

+//

+#define PMBA_RTE  BIT0

+

+//

+// Offset in the Power Management Base Address to the ACPI Timer

+//

+#define ACPI_TIMER_OFFSET  0x8

+

+//

+// Cached ACPI Timer IO Address

+//

+STATIC UINT32 mAcpiTimerIoAddr;

+

+/**

+  The constructor function caches the ACPI tick counter address

+

+  At the time this constructor runs (DXE_CORE or later), ACPI IO space

+  has already been enabled by either PlatformPei or by the "Base"

+  instance of this library.

+  In order to avoid querying the underlying platform type during each

+  tick counter read operation, we cache the counter address during

+  initialization of this instance of the Timer Library.

+

+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

+

+**/

+RETURN_STATUS

+EFIAPI

+AcpiTimerLibConstructor (

+  VOID

+  )

+{

+  UINT16 HostBridgeDevId;

+  UINTN Pmba;

+

+  //

+  // Query Host Bridge DID to determine platform type

+  //

+  HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);

+  switch (HostBridgeDevId) {

+    case INTEL_82441_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_PIIX4 (0x40);

+      break;

+    case INTEL_Q35_MCH_DEVICE_ID:

+      Pmba = POWER_MGMT_REGISTER_Q35 (0x40);

+      break;

+    default:

+      DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",

+        __FUNCTION__, HostBridgeDevId));

+      ASSERT (FALSE);

+      return RETURN_UNSUPPORTED;

+  }

+

+  mAcpiTimerIoAddr = (PciRead32 (Pmba) & ~PMBA_RTE) + ACPI_TIMER_OFFSET;

+

+  return RETURN_SUCCESS;

+}

+

+/**

+  Internal function to read the current tick counter of ACPI.

+

+  Read the current ACPI tick counter using the counter address cached

+  by this instance's constructor.

+

+  @return The tick counter read.

+

+**/

+UINT32

+InternalAcpiGetTimerTick (

+  VOID

+  )

+{

+  //

+  //   Return the current ACPI timer value.

+  //

+  return IoRead32 (mAcpiTimerIoAddr);

+}

diff --git a/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf b/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
new file mode 100644
index 0000000..b7e8bfc
--- /dev/null
+++ b/OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
@@ -0,0 +1,40 @@
+## @file

+#  DXE ACPI Timer Library Instance.

+#

+#  Copyright (C) 2014, Gabriel L. Somlo <somlo@cmu.edu>

+#  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.

+#

+#  This program and the accompanying materials are licensed and made

+#  available under the terms and conditions of the BSD License which

+#  accompanies this distribution.   The full text of the license may

+#  be found at http://opensource.org/licenses/bsd-license.php

+#

+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+#

+##

+

+[Defines]

+  INF_VERSION    = 0x00010005

+  BASE_NAME      = DxeAcpiTimerLib

+  FILE_GUID      = 52DECA02-2EE8-4EAA-8EAD-1AB83F8A5955

+  MODULE_TYPE    = BASE

+  VERSION_STRING = 1.0

+  LIBRARY_CLASS  = TimerLib|DXE_DRIVER DXE_RUNTIME_DRIVER

+  CONSTRUCTOR    = AcpiTimerLibConstructor

+

+[Sources]

+  AcpiTimerLib.c

+  DxeAcpiTimerLib.c

+

+[Packages]

+  MdePkg/MdePkg.dec

+  OvmfPkg/OvmfPkg.dec

+

+[Pcd]

+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfHostBridgePciDevId

+

+[LibraryClasses]

+  BaseLib

+  PciLib

+  IoLib

diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index 59f0fce..5baec1c 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -57,7 +57,7 @@
 ################################################################################

 [LibraryClasses]

   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf

-  TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf

   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf

   BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf

   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf

@@ -131,6 +131,7 @@
 !endif

 

 [LibraryClasses.common.SEC]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgSecLib.inf

 !ifdef $(DEBUG_ON_SERIAL_PORT)

   DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf

@@ -149,6 +150,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.PEI_CORE]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -165,6 +167,7 @@
   PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf

 

 [LibraryClasses.common.PEIM]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -203,6 +206,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.DXE_RUNTIME_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -232,6 +236,7 @@
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf

 

 [LibraryClasses.common.DXE_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -411,19 +416,10 @@
       PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   }

   PcAtChipsetPkg/KbcResetDxe/Reset.inf

-  MdeModulePkg/Universal/Metronome/Metronome.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

-  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

+  MdeModulePkg/Universal/Metronome/Metronome.inf

+  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf

   IntelFrameworkModulePkg/Universal/BdsDxe/BdsDxe.inf {

     <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

 !ifdef $(CSM_ENABLE)

       NULL|OvmfPkg/Csm/CsmSupportLib/CsmSupportLib.inf

 !endif

diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index ab6feae..c7a18d0 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -62,7 +62,7 @@
 ################################################################################

 [LibraryClasses]

   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf

-  TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf

   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf

   BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf

   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf

@@ -136,6 +136,7 @@
 !endif

 

 [LibraryClasses.common.SEC]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgSecLib.inf

 !ifdef $(DEBUG_ON_SERIAL_PORT)

   DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf

@@ -154,6 +155,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.PEI_CORE]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -170,6 +172,7 @@
   PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf

 

 [LibraryClasses.common.PEIM]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -208,6 +211,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.DXE_RUNTIME_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -237,6 +241,7 @@
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf

 

 [LibraryClasses.common.DXE_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -418,19 +423,10 @@
       PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   }

   PcAtChipsetPkg/KbcResetDxe/Reset.inf

-  MdeModulePkg/Universal/Metronome/Metronome.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

-  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

+  MdeModulePkg/Universal/Metronome/Metronome.inf

+  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf

   IntelFrameworkModulePkg/Universal/BdsDxe/BdsDxe.inf {

     <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

 !ifdef $(CSM_ENABLE)

       NULL|OvmfPkg/Csm/CsmSupportLib/CsmSupportLib.inf

 !endif

diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index 26d5655..7cafcac 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -62,7 +62,7 @@
 ################################################################################

 [LibraryClasses]

   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf

-  TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf

   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf

   BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf

   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf

@@ -136,6 +136,7 @@
 !endif

 

 [LibraryClasses.common.SEC]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgSecLib.inf

 !ifdef $(DEBUG_ON_SERIAL_PORT)

   DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf

@@ -154,6 +155,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.PEI_CORE]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -170,6 +172,7 @@
   PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf

 

 [LibraryClasses.common.PEIM]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

   HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf

   PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf

   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf

@@ -208,6 +211,7 @@
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf

 

 [LibraryClasses.common.DXE_RUNTIME_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -237,6 +241,7 @@
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf

 

 [LibraryClasses.common.DXE_DRIVER]

+  TimerLib|OvmfPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf

   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

@@ -416,19 +421,10 @@
       PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

   }

   PcAtChipsetPkg/KbcResetDxe/Reset.inf

-  MdeModulePkg/Universal/Metronome/Metronome.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

-  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf {

-    <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

-  }

-

+  MdeModulePkg/Universal/Metronome/Metronome.inf

+  PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf

   IntelFrameworkModulePkg/Universal/BdsDxe/BdsDxe.inf {

     <LibraryClasses>

-      TimerLib|OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.inf

 !ifdef $(CSM_ENABLE)

       NULL|OvmfPkg/Csm/CsmSupportLib/CsmSupportLib.inf

 !endif