File ovmf-Revert-UefiCpuPkg-BaseRiscV64CpuTimerLib-Add-constru.patch of Package ovmf

From 756560b2ec49a63ef86808615cf1746266c1144f Mon Sep 17 00:00:00 2001
From: Fabian Vogt <fvogt@suse.de>
Date: Fri, 27 Feb 2026 12:36:31 +0100
Subject: [PATCH] Revert "UefiCpuPkg/BaseRiscV64CpuTimerLib: Add constructor to
 initialize mTimeBase"

This reverts commit f2c63dca1bc5201903b75a8551afdf2c7ab2fc4d.
---
 .../BaseRiscV64CpuTimerLib.inf                |   1 -
 .../BaseRiscV64CpuTimerLib/CpuTimerLib.c      | 113 ++++++++----------
 2 files changed, 50 insertions(+), 64 deletions(-)

diff --git a/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/BaseRiscV64CpuTimerLib.inf b/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/BaseRiscV64CpuTimerLib.inf
index a3d25cda41..b7ae1e86b7 100644
--- a/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/BaseRiscV64CpuTimerLib.inf
+++ b/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/BaseRiscV64CpuTimerLib.inf
@@ -16,7 +16,6 @@
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = TimerLib
   MODULE_UNI_FILE                = BaseRisV64CpuTimerLib.uni
-  CONSTRUCTOR                    = BaseRiscV64CpuTimerLibConstructor
 
 [Sources]
   CpuTimerLib.c
diff --git a/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/CpuTimerLib.c b/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/CpuTimerLib.c
index f93e841a45..bb07200754 100644
--- a/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/CpuTimerLib.c
+++ b/UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/CpuTimerLib.c
@@ -21,6 +21,8 @@
 
 STATIC UINT64  mTimeBase;
 
+#define GET_TIME_BASE()  (mTimeBase ?: GetPerformanceCounterProperties(NULL, NULL))
+
 /**
   Stalls the CPU for at least the given number of ticks.
 
@@ -65,7 +67,7 @@ MicroSecondDelay (
     DivU64x32 (
       MultU64x32 (
         MicroSeconds,
-        mTimeBase
+        GET_TIME_BASE ()
         ),
       1000000u
       )
@@ -93,7 +95,7 @@ NanoSecondDelay (
     DivU64x32 (
       MultU64x32 (
         NanoSeconds,
-        mTimeBase
+        GET_TIME_BASE ()
         ),
       1000000000u
       )
@@ -152,6 +154,12 @@ GetPerformanceCounterProperties (
   OUT      UINT64                    *EndValue     OPTIONAL
   )
 {
+  VOID                    *Hob;
+  RISCV_SEC_HANDOFF_DATA  *SecData;
+  CONST EFI_GUID          SecHobDataGuid = RISCV_SEC_HANDOFF_HOB_GUID;
+  UINT64                  TimeBase;
+  CONST VOID              *FdtBase;
+
   if (StartValue != NULL) {
     *StartValue = 0;
   }
@@ -160,66 +168,9 @@ GetPerformanceCounterProperties (
     *EndValue = 32 - 1;
   }
 
-  return mTimeBase;
-}
-
-/**
-  Converts elapsed ticks of performance counter to time in nanoseconds.
-
-  This function converts the elapsed ticks of running performance counter to
-  time value in unit of nanoseconds.
-
-  @param  Ticks     The number of elapsed ticks of running performance counter.
-
-  @return The elapsed time in nanoseconds.
-
-**/
-UINT64
-EFIAPI
-GetTimeInNanoSecond (
-  IN      UINT64  Ticks
-  )
-{
-  UINT64  NanoSeconds;
-  UINT32  Remainder;
-
-  //
-  //          Ticks
-  // Time = --------- x 1,000,000,000
-  //        Frequency
-  //
-  NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, mTimeBase, &Remainder), 1000000000u);
-
-  //
-  // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
-  // will not overflow 64-bit.
-  //
-  NanoSeconds += DivU64x32 (MultU64x32 ((UINT64)Remainder, 1000000000u), mTimeBase);
-
-  return NanoSeconds;
-}
-
-/**
-  Constructor function for the Timer Library.
-
-  This constructor function is called early during DXE phase to ensure that
-  GetPerformanceCounterProperties() is invoked and mTimeBase is initialized
-  before any code that depends on it.
-
-  @retval EFI_SUCCESS   The constructor always returns success.
-
-**/
-EFI_STATUS
-EFIAPI
-BaseRiscV64CpuTimerLibConstructor (
-  VOID
-  )
-{
-  VOID                    *Hob;
-  RISCV_SEC_HANDOFF_DATA  *SecData;
-  CONST EFI_GUID          SecHobDataGuid = RISCV_SEC_HANDOFF_HOB_GUID;
-  UINT64                  TimeBase;
-  CONST VOID              *FdtBase;
+  if (mTimeBase != 0) {
+    return mTimeBase;
+  }
 
   //
   // Locate the FDT HOB and validate header
@@ -273,5 +224,41 @@ BaseRiscV64CpuTimerLibConstructor (
   //
   mTimeBase = TimeBase;
 
-  return EFI_SUCCESS;
+  return TimeBase;
+}
+
+/**
+  Converts elapsed ticks of performance counter to time in nanoseconds.
+
+  This function converts the elapsed ticks of running performance counter to
+  time value in unit of nanoseconds.
+
+  @param  Ticks     The number of elapsed ticks of running performance counter.
+
+  @return The elapsed time in nanoseconds.
+
+**/
+UINT64
+EFIAPI
+GetTimeInNanoSecond (
+  IN      UINT64  Ticks
+  )
+{
+  UINT64  NanoSeconds;
+  UINT32  Remainder;
+
+  //
+  //          Ticks
+  // Time = --------- x 1,000,000,000
+  //        Frequency
+  //
+  NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, GET_TIME_BASE (), &Remainder), 1000000000u);
+
+  //
+  // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
+  // will not overflow 64-bit.
+  //
+  NanoSeconds += DivU64x32 (MultU64x32 ((UINT64)Remainder, 1000000000u), GET_TIME_BASE ());
+
+  return NanoSeconds;
 }
-- 
2.52.0

openSUSE Build Service is sponsored by