X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FSecPeiDxeTimerLibCpu%2FX86TimerLib.c;h=76c66fbce6fb9f1e710185fbe5e899903ef42f1e;hb=32eb6739b961a5b4cc204b94220fa5ad15f79a9b;hp=aff51932f6d9643347ecfafb900d58f1c207d816;hpb=1553d073632e1307950f8648444071a0ec27759f;p=mirror_edk2.git diff --git a/MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c b/MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c index aff51932f6..76c66fbce6 100644 --- a/MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c +++ b/MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c @@ -1,11 +1,11 @@ /** @file Timer Library functions built upon local APIC on IA32/x64. - Copyright (c) 2006 - 2008, Intel Corporation
- All rights reserved. This program and the accompanying materials + Copyright (c) 2006 - 2015, 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 + 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. @@ -19,6 +19,7 @@ #include #include +#define APIC_SVR 0x0f0 #define APIC_LVTERR 0x370 #define APIC_TMICT 0x380 #define APIC_TMCCT 0x390 @@ -39,6 +40,11 @@ CONST UINT8 mTimerLibLocalApicDivisor[] = { /** Internal function to retrieve the base address of local APIC. + This function will ASSERT if: + The local APIC is not globally enabled. + The local APIC is not working under XAPIC mode. + The local APIC is not software enabled. + @return The base address of local APIC **/ @@ -48,7 +54,32 @@ InternalX86GetApicBase ( VOID ) { - return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12; + UINTN MsrValue; + UINTN ApicBase; + + MsrValue = (UINTN) AsmReadMsr64 (27); + ApicBase = MsrValue & 0xffffff000ULL; + + // + // Check the APIC Global Enable bit (bit 11) in IA32_APIC_BASE MSR. + // This bit will be 1, if local APIC is globally enabled. + // + ASSERT ((MsrValue & BIT11) != 0); + + // + // Check the APIC Extended Mode bit (bit 10) in IA32_APIC_BASE MSR. + // This bit will be 0, if local APIC is under XAPIC mode. + // + ASSERT ((MsrValue & BIT10) == 0); + + // + // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt + // Vector Register. + // This bit will be 1, if local APIC is software enabled. + // + ASSERT ((MmioRead32 (ApicBase + APIC_SVR) & BIT8) != 0); + + return ApicBase; } /** @@ -87,12 +118,31 @@ InternalX86GetTimerTick ( return MmioRead32 (ApicBase + APIC_TMCCT); } +/** + Internal function to read the initial timer count of local APIC. + + @param ApicBase The base address of memory mapped registers of local APIC. + + @return The initial timer count read. + +**/ +UINT32 +InternalX86GetInitTimerCount ( + IN UINTN ApicBase + ) +{ + return MmioRead32 (ApicBase + APIC_TMICT); +} + /** Stalls the CPU for at least the given number of ticks. 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 + InternalX86GetInitTimerCount() is zero. + @param ApicBase The base address of memory mapped registers of local APIC. @param Delay A period of time to delay in ticks. @@ -105,20 +155,50 @@ InternalX86Delay ( ) { INT32 Ticks; + 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 = InternalX86GetTimerTick (ApicBase) - Delay; + InitCount = InternalX86GetInitTimerCount (ApicBase); + 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 // - while (((UINT32)(InternalX86GetTimerTick (ApicBase) - Ticks) & GetPowerOfTwo32 ((MmioRead32 (ApicBase + APIC_TMICT)))) == 0) { - CpuPause (); - } + StartTick = InternalX86GetTimerTick (ApicBase); + do { + // + // Wait until time out by Delay value + // + do { + CpuPause (); + // + // Get Ticks from Start to Current. + // + Ticks = StartTick - InternalX86GetTimerTick (ApicBase); + // + // 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); } /** @@ -240,11 +320,7 @@ GetPerformanceCounterProperties ( ApicBase = InternalX86GetApicBase (); if (StartValue != NULL) { - *StartValue = MmioRead32 (ApicBase + APIC_TMICT); - // - // make sure StartValue is all 1s from High Bit - // - ASSERT ((*StartValue & (*StartValue + 1)) == 0); + *StartValue = (UINT64)InternalX86GetInitTimerCount (ApicBase); } if (EndValue != NULL) { @@ -253,3 +329,47 @@ GetPerformanceCounterProperties ( return (UINT64) InternalX86GetTimerFrequency (ApicBase); } + +/** + 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; +}