From 7883dd3da206787426c152cb3b9a5c90b0e26af7 Mon Sep 17 00:00:00 2001 From: Vishal Oliyil Kunnil Date: Tue, 10 Mar 2026 11:27:01 -0700 Subject: [PATCH] DTBExtnLib: Remove QcomBaseLib dependency Remove QcomBaseLib dependency and move to upstream edk2 API. Signed-off-by: Vishal Oliyil Kunnil --- libs/DTBExtnLib/DTBExtnLib_node.c | 51 ++++++++++++++++++-- libs/DTSelectLib/Environment/dt_select_env.h | 2 +- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/libs/DTBExtnLib/DTBExtnLib_node.c b/libs/DTBExtnLib/DTBExtnLib_node.c index 79edd1f..7ae88e7 100644 --- a/libs/DTBExtnLib/DTBExtnLib_node.c +++ b/libs/DTBExtnLib/DTBExtnLib_node.c @@ -15,10 +15,12 @@ #include "DTBDefs.h" #if defined (TARGET_UEFI) + #include + #include #include - #include + #include + #include #include - #include #elif defined (TARGET_XBL) #include #include @@ -138,7 +140,50 @@ __dtb_get_time_us ( ) { #if defined (TARGET_UEFI) - return GetTimerCountus (); + STATIC UINT64 CachedFrequency = 0; + STATIC UINT32 FactoruS = 0; + STATIC BOOLEAN Initialized = FALSE; + UINT64 TimeTicks = 0; + UINT64 StartVal = 0; + UINT64 EndVal = 0; + + // Initialize on first call (matches QcomBaseLib approach) + if (!Initialized) { + CachedFrequency = GetPerformanceCounterProperties (&StartVal, &EndVal); + + // Validate frequency is reasonable (>1MHz) + if (CachedFrequency <= 1000000) { + DEBUG ((DEBUG_ERROR, "__dtb_get_time_us: Frequency too low\n")); + return 0; + } + + // Validate frequency is not too high (<4GHz) + if (CachedFrequency >= 0x100000000ULL) { + DEBUG ((DEBUG_ERROR, "__dtb_get_time_us: Frequency too high\n")); + return 0; + } + + // Validate counter counts up (not down) + if (StartVal >= EndVal) { + DEBUG ((DEBUG_ERROR, "__dtb_get_time_us: Down-counter not supported\n")); + return 0; + } + + // Pre-calculate conversion factor: Frequency / 1,000,000 + // This avoids overflow in the main calculation + FactoruS = (UINT32)DivU64x32 (CachedFrequency, 1000000); + + Initialized = TRUE; + } + + // Get current performance counter value + TimeTicks = GetPerformanceCounter(); + + // Convert to microseconds using cached factor + // Formula: TimeUs = TimeTicks / (Frequency / 1,000,000) + // This is equivalent to: TimeUs = (TimeTicks * 1,000,000) / Frequency + // But avoids integer overflow by doing division first + return DivU64x32 (TimeTicks, FactoruS); #elif defined (TARGET_XBL) return timer_if->get_apss_qtimer_counter_us (); #elif defined (PORT_Q6) diff --git a/libs/DTSelectLib/Environment/dt_select_env.h b/libs/DTSelectLib/Environment/dt_select_env.h index cc20513..5983508 100644 --- a/libs/DTSelectLib/Environment/dt_select_env.h +++ b/libs/DTSelectLib/Environment/dt_select_env.h @@ -6,7 +6,7 @@ #define DT_SELECT_CUSTOM_ENV_H_ #ifdef TARGET_UEFI #include -#include +#include #else /* Use below header file for using standard datatype */ #include