X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FSecPeiDxeTimerLibUefiCpu%2FX86TimerLib.c;h=801ebdb1917b98651aa768cfeb3448274187381e;hp=3ca3ca8008f705adac92cd25738e2c0151e8fabe;hb=7367cc6c24d01b400d2370ffd58ae02854a56b32;hpb=2057d8c84353dd9d20a782ffa5ba883e31a78f46 diff --git a/UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c b/UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c index 3ca3ca8008..801ebdb191 100644 --- a/UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c +++ b/UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c @@ -2,8 +2,8 @@ Timer Library functions built upon local APIC on IA32/x64. This library uses the local APIC library so that it supports x2APIC mode. - - Copyright (c) 2010, Intel Corporation. All rights reserved.
+ + Copyright (c) 2010 - 2018, 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 @@ -45,6 +45,9 @@ InternalX86GetTimerFrequency ( Stalls the CPU for at least the given number of ticks. It's invoked by MicroSecondDelay() and NanoSecondDelay(). + This function will ASSERT if the APIC timer intial count returned from + GetApicTimerInitCount() is zero. + @param Delay A period of time to delay in ticks. **/ @@ -55,22 +58,50 @@ InternalX86Delay ( ) { INT32 Ticks; - UINT32 PowerOfTwoCounter; + UINT32 Times; + UINT32 InitCount; + UINT32 StartTick; // - // The target timer count is calculated here + // In case Delay is too larger, separate it into several small delay slot. + // Devided Delay by half value of Init Count is to avoid Delay close to + // the Init Count, timeout maybe missing if the time consuming between 2 + // GetApicTimerCurrentCount() invoking is larger than the time gap between + // Delay and the Init Count. // - Ticks = GetApicTimerCurrentCount () - Delay; + InitCount = GetApicTimerInitCount (); + ASSERT (InitCount != 0); + Times = Delay / (InitCount / 2); + Delay = Delay % (InitCount / 2); // - // Wait until time out - // Delay > 2^31 could not be handled by this function - // Timer wrap-arounds are handled correctly by this function + // Get Start Tick and do delay // - PowerOfTwoCounter = GetPowerOfTwo32 (GetApicTimerInitCount ()); - while (((UINT32)(GetApicTimerCurrentCount () - Ticks) & PowerOfTwoCounter) == 0) { - CpuPause (); - } + StartTick = GetApicTimerCurrentCount (); + do { + // + // Wait until time out by Delay value + // + do { + CpuPause (); + // + // Get Ticks from Start to Current. + // + Ticks = StartTick - GetApicTimerCurrentCount (); + // + // Ticks < 0 means Timer wrap-arounds happens. + // + if (Ticks < 0) { + Ticks += InitCount; + } + } while ((UINT32)Ticks < Delay); + + // + // Update StartTick and Delay for next delay slot + // + StartTick -= (StartTick > Delay) ? Delay : (Delay - InitCount); + Delay = InitCount / 2; + } while (Times-- > 0); } /** @@ -181,10 +212,6 @@ GetPerformanceCounterProperties ( { if (StartValue != NULL) { *StartValue = (UINT64)GetApicTimerInitCount (); - // - // make sure StartValue is all 1s from High Bit - // - ASSERT ((*StartValue & (*StartValue + 1)) == 0); } if (EndValue != NULL) { @@ -193,3 +220,47 @@ GetPerformanceCounterProperties ( return (UINT64) InternalX86GetTimerFrequency (); } + +/** + 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 Frequency; + UINT64 NanoSeconds; + UINT64 Remainder; + INTN Shift; + + Frequency = GetPerformanceCounterProperties (NULL, NULL); + + // + // Ticks + // Time = --------- x 1,000,000,000 + // Frequency + // + NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u); + + // + // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit. + // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34, + // i.e. highest bit set in Remainder should <= 33. + // + Shift = MAX (0, HighBitSet64 (Remainder) - 33); + Remainder = RShiftU64 (Remainder, (UINTN) Shift); + Frequency = RShiftU64 (Frequency, (UINTN) Shift); + NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL); + + return NanoSeconds; +}