X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMtrrLib%2FMtrrLib.c;h=b1c12aa32e01026a4a2765b9f08a4bd13d51c39f;hp=a95a72bfa3c8933b88be3f526cf7790b2e9bd930;hb=31b3597ee22c431904476bacd1970fb1cb3f8fc5;hpb=3b9be4164b8714a4d866e822cfacb1ea6da6ef7b diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c index a95a72bfa3..b1c12aa32e 100644 --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c @@ -1,8 +1,8 @@ /** @file MTRR setting library - Copyright (c) 2008 - 2010, Intel Corporation - All rights reserved. This program and the accompanying materials + Copyright (c) 2008 - 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 @@ -20,11 +20,18 @@ #include #include +// +// Context to save and restore when MTRRs are programmed +// +typedef struct { + UINTN Cr4; + BOOLEAN InterruptState; +} MTRR_CONTEXT; + // // This table defines the offset, base and length of the fixed MTRRs // -STATIC -FIXED_MTRR MtrrLibFixedMtrrTable[] = { +CONST FIXED_MTRR mMtrrLibFixedMtrrTable[] = { { MTRR_LIB_IA32_MTRR_FIX64K_00000, 0, @@ -79,9 +86,41 @@ FIXED_MTRR MtrrLibFixedMtrrTable[] = { MTRR_LIB_IA32_MTRR_FIX4K_F8000, 0xF8000, SIZE_4KB - }, + } +}; + +// +// Lookup table used to print MTRRs +// +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mMtrrMemoryCacheTypeShortName[] = { + "UC", // CacheUncacheable + "WC", // CacheWriteCombining + "R*", // Invalid + "R*", // Invalid + "WT", // CacheWriteThrough + "WP", // CacheWriteProtected + "WB", // CacheWriteBack + "R*" // Invalid }; +/** + Worker function returns the variable MTRR count for the CPU. + + @return Variable MTRR count + +**/ +UINT32 +GetVariableMtrrCountWorker ( + VOID + ) +{ + UINT32 VariableMtrrCount; + + VariableMtrrCount = (UINT32)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK); + ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); + return VariableMtrrCount; +} + /** Returns the variable MTRR count for the CPU. @@ -89,11 +128,38 @@ FIXED_MTRR MtrrLibFixedMtrrTable[] = { **/ UINT32 +EFIAPI GetVariableMtrrCount ( VOID ) { - return (UINT32)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK); + if (!IsMtrrSupported ()) { + return 0; + } + return GetVariableMtrrCountWorker (); +} + +/** + Worker function returns the firmware usable variable MTRR count for the CPU. + + @return Firmware usable variable MTRR count + +**/ +UINT32 +GetFirmwareVariableMtrrCountWorker ( + VOID + ) +{ + UINT32 VariableMtrrCount; + UINT32 ReservedMtrrNumber; + + VariableMtrrCount = GetVariableMtrrCountWorker (); + ReservedMtrrNumber = PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs); + if (VariableMtrrCount < ReservedMtrrNumber) { + return 0; + } + + return VariableMtrrCount - ReservedMtrrNumber; } /** @@ -103,120 +169,155 @@ GetVariableMtrrCount ( **/ UINT32 +EFIAPI GetFirmwareVariableMtrrCount ( VOID ) { - return GetVariableMtrrCount () - RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER; + if (!IsMtrrSupported ()) { + return 0; + } + return GetFirmwareVariableMtrrCountWorker (); } /** - Returns the default MTRR cache type for the system. + Worker function returns the default MTRR cache type for the system. - @return MTRR default type + @return The default MTRR cache type. **/ -UINT64 -GetMtrrDefaultMemoryType ( +MTRR_MEMORY_CACHE_TYPE +MtrrGetDefaultMemoryTypeWorker ( VOID -) + ) { - return (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0xff); + return (MTRR_MEMORY_CACHE_TYPE) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0x7); } +/** + Returns the default MTRR cache type for the system. + + @return The default MTRR cache type. + +**/ +MTRR_MEMORY_CACHE_TYPE +EFIAPI +MtrrGetDefaultMemoryType ( + VOID + ) +{ + if (!IsMtrrSupported ()) { + return CacheUncacheable; + } + return MtrrGetDefaultMemoryTypeWorker (); +} + /** Preparation before programming MTRR. This function will do some preparation for programming MTRRs: disable cache, invalid cache and disable MTRR caching functionality - @return CR4 value before changing. + @param[out] MtrrContext Pointer to context to save **/ -UINTN +VOID PreMtrrChange ( - VOID + OUT MTRR_CONTEXT *MtrrContext ) { - UINTN Value; - // - // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29) + // Disable interrupts and save current interrupt state // - Value = AsmReadCr0 (); - Value = (UINTN) BitFieldWrite64 (Value, 30, 30, 1); - Value = (UINTN) BitFieldWrite64 (Value, 29, 29, 0); - AsmWriteCr0 (Value); + MtrrContext->InterruptState = SaveAndDisableInterrupts(); + // - // Flush cache + // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29) // - AsmWbinvd (); + AsmDisableCache (); + // - // Clear PGE flag Bit 7 + // Save original CR4 value and clear PGE flag (Bit 7) // - Value = AsmReadCr4 (); - AsmWriteCr4 ((UINTN) BitFieldWrite64 (Value, 7, 7, 0)); + MtrrContext->Cr4 = AsmReadCr4 (); + AsmWriteCr4 (MtrrContext->Cr4 & (~BIT7)); + // // Flush all TLBs // CpuFlushTlb (); + // - // Disable Mtrrs + // Disable MTRRs // AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 0); - - return Value; } - /** Cleaning up after programming MTRRs. This function will do some clean up after programming MTRRs: - enable MTRR caching functionality, and enable cache + Flush all TLBs, re-enable caching, restore CR4. - @param Cr4 CR4 value to restore + @param[in] MtrrContext Pointer to context to restore **/ VOID -PostMtrrChange ( - UINTN Cr4 +PostMtrrChangeEnableCache ( + IN MTRR_CONTEXT *MtrrContext ) { - UINTN Value; + // + // Flush all TLBs + // + CpuFlushTlb (); // - // Enable Cache MTRR + // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29) // - AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 3); + AsmEnableCache (); // - // Flush all TLBs and cache the second time + // Restore original CR4 value // - AsmWbinvd (); - CpuFlushTlb (); + AsmWriteCr4 (MtrrContext->Cr4); // - // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29) + // Restore original interrupt state // - Value = AsmReadCr0 (); - Value = (UINTN) BitFieldWrite64 (Value, 30, 30, 0); - Value = (UINTN) BitFieldWrite64 (Value, 29, 29, 0); - AsmWriteCr0 (Value); + SetInterruptState (MtrrContext->InterruptState); +} - AsmWriteCr4 (Cr4); +/** + Cleaning up after programming MTRRs. - return ; + This function will do some clean up after programming MTRRs: + enable MTRR caching functionality, and enable cache + + @param[in] MtrrContext Pointer to context to restore + +**/ +VOID +PostMtrrChange ( + IN MTRR_CONTEXT *MtrrContext + ) +{ + // + // Enable Cache MTRR + // + AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 3); + + PostMtrrChangeEnableCache (MtrrContext); } /** Programs fixed MTRRs registers. - @param MemoryCacheType The memory type to set. - @param Base The base address of memory range. - @param Length The length of memory range. + @param[in] MemoryCacheType The memory type to set. + @param[in, out] Base The base address of memory range. + @param[in, out] Length The length of memory range. @retval RETURN_SUCCESS The cache type was updated successfully @retval RETURN_UNSUPPORTED The requested range or cache type was invalid @@ -241,11 +342,11 @@ ProgramFixedMtrr ( ClearMask = 0; for (MsrNum = 0; MsrNum < MTRR_NUMBER_OF_FIXED_MTRR; MsrNum++) { - if ((*Base >= MtrrLibFixedMtrrTable[MsrNum].BaseAddress) && + if ((*Base >= mMtrrLibFixedMtrrTable[MsrNum].BaseAddress) && (*Base < ( - MtrrLibFixedMtrrTable[MsrNum].BaseAddress + - (8 * MtrrLibFixedMtrrTable[MsrNum].Length) + mMtrrLibFixedMtrrTable[MsrNum].BaseAddress + + (8 * mMtrrLibFixedMtrrTable[MsrNum].Length) ) ) ) { @@ -263,8 +364,8 @@ ProgramFixedMtrr ( for (ByteShift = 0; ByteShift < 8; ByteShift++) { if (*Base == ( - MtrrLibFixedMtrrTable[MsrNum].BaseAddress + - (ByteShift * MtrrLibFixedMtrrTable[MsrNum].Length) + mMtrrLibFixedMtrrTable[MsrNum].BaseAddress + + (ByteShift * mMtrrLibFixedMtrrTable[MsrNum].Length) ) ) { break; @@ -277,13 +378,13 @@ ProgramFixedMtrr ( for ( ; - ((ByteShift < 8) && (*Length >= MtrrLibFixedMtrrTable[MsrNum].Length)); + ((ByteShift < 8) && (*Length >= mMtrrLibFixedMtrrTable[MsrNum].Length)); ByteShift++ ) { OrMask |= LShiftU64 ((UINT64) MemoryCacheType, (UINT32) (ByteShift * 8)); ClearMask |= LShiftU64 ((UINT64) 0xFF, (UINT32) (ByteShift * 8)); - *Length -= MtrrLibFixedMtrrTable[MsrNum].Length; - *Base += MtrrLibFixedMtrrTable[MsrNum].Length; + *Length -= mMtrrLibFixedMtrrTable[MsrNum].Length; + *Base += mMtrrLibFixedMtrrTable[MsrNum].Length; } if (ByteShift < 8 && (*Length != 0)) { @@ -291,58 +392,48 @@ ProgramFixedMtrr ( } TempQword = - (AsmReadMsr64 (MtrrLibFixedMtrrTable[MsrNum].Msr) & ~ClearMask) | OrMask; - AsmWriteMsr64 (MtrrLibFixedMtrrTable[MsrNum].Msr, TempQword); + (AsmReadMsr64 (mMtrrLibFixedMtrrTable[MsrNum].Msr) & ~ClearMask) | OrMask; + AsmWriteMsr64 (mMtrrLibFixedMtrrTable[MsrNum].Msr, TempQword); return RETURN_SUCCESS; } /** - Get the attribute of variable MTRRs. + Gets the attribute of variable MTRRs. - This function shadows the content of variable MTRRs into - an internal array: VariableMtrr + This function shadows the content of variable MTRRs into an + internal array: VariableMtrr. - @param MtrrValidBitsMask The mask for the valid bit of the MTRR - @param MtrrValidAddressMask The valid address mask for MTRR since the base address in - MTRR must align to 4K, so valid address mask equal to - MtrrValidBitsMask & 0xfffffffffffff000ULL - @param VariableMtrrCount On input, it means the array number of variable MTRRs passed in. - On output, it means the number of MTRRs which has been used if EFI_SUCCESS, - or the number of MTRR required if BUFFER_TOO_SMALL. - @param VariableMtrr The array to shadow variable MTRRs content + @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR + @param[in] MtrrValidAddressMask The valid address mask for MTRR + @param[out] VariableMtrr The array to shadow variable MTRRs content - @retval RETURN_SUCCESS The variable MTRRs are returned. - @retval RETURN_BUFFER_TOO_SMALL The input buffer is too small to hold the variable MTRRs. + @return The return value of this paramter indicates the + number of MTRRs which has been used. **/ -RETURN_STATUS +UINT32 EFIAPI MtrrGetMemoryAttributeInVariableMtrr ( IN UINT64 MtrrValidBitsMask, IN UINT64 MtrrValidAddressMask, - IN OUT UINT32 *VariableMtrrCount, OUT VARIABLE_MTRR *VariableMtrr ) { UINTN Index; UINT32 MsrNum; UINT32 UsedMtrr; - UINTN FirmwareVariableMtrrCount; + UINT32 FirmwareVariableMtrrCount; UINT32 VariableMtrrEnd; - // - // Check if input buffer is large enough - // - FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); - if (*VariableMtrrCount < FirmwareVariableMtrrCount) { - *VariableMtrrCount = (UINT32)FirmwareVariableMtrrCount; - return RETURN_BUFFER_TOO_SMALL; + if (!IsMtrrSupported ()) { + return 0; } + FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1; - ZeroMem (VariableMtrr, sizeof (VARIABLE_MTRR) * (*VariableMtrrCount)); + ZeroMem (VariableMtrr, sizeof (VARIABLE_MTRR) * MTRR_NUMBER_OF_VARIABLE_MTRR); UsedMtrr = 0; for (MsrNum = MTRR_LIB_IA32_VARIABLE_MTRR_BASE, Index = 0; @@ -368,17 +459,16 @@ MtrrGetMemoryAttributeInVariableMtrr ( Index++; } } - *VariableMtrrCount = UsedMtrr; - return RETURN_SUCCESS; + return UsedMtrr; } /** Checks overlap between given memory range and MTRRs. - @param Start The start address of memory range. - @param End The end address of memory range. - @param VariableMtrr The array to shadow variable MTRRs content + @param[in] Start The start address of memory range. + @param[in] End The end address of memory range. + @param[in] VariableMtrr The array to shadow variable MTRRs content @retval TRUE Overlap exists. @retval FALSE No overlap. @@ -414,9 +504,9 @@ CheckMemoryAttributeOverlap ( /** Marks a variable MTRR as non-valid. - @param Index The index of the array VariableMtrr to be invalidated - @param VariableMtrr The array to shadow variable MTRRs content - @param UsedMtrr The number of MTRRs which has already been used + @param[in] Index The index of the array VariableMtrr to be invalidated + @param[in] VariableMtrr The array to shadow variable MTRRs content + @param[out] UsedMtrr The number of MTRRs which has already been used **/ VOID @@ -432,16 +522,16 @@ InvalidateShadowMtrr ( /** - Combine memory attributes. + Combines memory attributes. If overlap exists between given memory range and MTRRs, try to combine them. - @param Attributes The memory type to set. - @param Base The base address of memory range. - @param Length The length of memory range. - @param VariableMtrr The array to shadow variable MTRRs content - @param UsedMtrr The number of MTRRs which has already been used - @param OverwriteExistingMtrr Returns whether an existing MTRR was used + @param[in] Attributes The memory type to set. + @param[in, out] Base The base address of memory range. + @param[in, out] Length The length of memory range. + @param[in] VariableMtrr The array to shadow variable MTRRs content + @param[in, out] UsedMtrr The number of MTRRs which has already been used + @param[out] OverwriteExistingMtrr Returns whether an existing MTRR was used @retval EFI_SUCCESS Memory region successfully combined. @retval EFI_ACCESS_DENIED Memory region cannot be combined. @@ -463,10 +553,12 @@ CombineMemoryAttribute ( UINT64 MtrrEnd; UINT64 EndAddress; UINT32 FirmwareVariableMtrrCount; + BOOLEAN CoveredByExistingMtrr; FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); *OverwriteExistingMtrr = FALSE; + CoveredByExistingMtrr = FALSE; EndAddress = *Base +*Length - 1; for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) { @@ -487,11 +579,12 @@ CombineMemoryAttribute ( // if (Attributes == VariableMtrr[Index].Type) { // - // if the Mtrr range contain the request range, return RETURN_SUCCESS + // if the MTRR range contain the request range, set a flag, then continue to + // invalidate any MTRR of the same request range with higher priority cache type. // if (VariableMtrr[Index].BaseAddress <= *Base && MtrrEnd >= EndAddress) { - *Length = 0; - return RETURN_SUCCESS; + CoveredByExistingMtrr = TRUE; + continue; } // // invalid this MTRR, and program the combine range @@ -538,14 +631,19 @@ CombineMemoryAttribute ( return RETURN_ACCESS_DENIED; } + if (CoveredByExistingMtrr) { + *Length = 0; + } + return RETURN_SUCCESS; } /** - Calculate the maximum value which is a power of 2, but less the MemoryLength. + Calculates the maximum value which is a power of 2, but less the MemoryLength. + + @param[in] MemoryLength The number to pass in. - @param MemoryLength The number to pass in. @return The maximum value which is align to power of 2 and less the MemoryLength **/ @@ -556,7 +654,7 @@ Power2MaxMemory ( { UINT64 Result; - if (RShiftU64 (MemoryLength, 32)) { + if (RShiftU64 (MemoryLength, 32) != 0) { Result = LShiftU64 ( (UINT64) GetPowerOfTwo32 ( (UINT32) RShiftU64 (MemoryLength, 32) @@ -572,29 +670,60 @@ Power2MaxMemory ( /** - Check the direction to program variable MTRRs. + Determines the MTRR numbers used to program a memory range. + + This function first checks the alignment of the base address. + If the alignment of the base address <= Length, cover the memory range + (BaseAddress, alignment) by a MTRR, then BaseAddress += alignment and + Length -= alignment. Repeat the step until alignment > Length. - This function determines which direction of programming the variable - MTRRs will use fewer MTRRs. + Then this function determines which direction of programming the variable + MTRRs for the remaining length will use fewer MTRRs. - @param Input Length of Memory to program MTRR - @param MtrrNumber Pointer to the number of necessary MTRRs + @param[in] BaseAddress Length of Memory to program MTRR + @param[in] Length Length of Memory to program MTRR + @param[in] MtrrNumber Pointer to the number of necessary MTRRs @retval TRUE Positive direction is better. - FALSE Negtive direction is better. + FALSE Negative direction is better. **/ BOOLEAN -GetDirection ( - IN UINT64 Input, +GetMtrrNumberAndDirection ( + IN UINT64 BaseAddress, + IN UINT64 Length, IN UINTN *MtrrNumber ) { UINT64 TempQword; + UINT64 Alignment; UINT32 Positive; UINT32 Subtractive; - TempQword = Input; + *MtrrNumber = 0; + + if (BaseAddress != 0) { + do { + // + // Calculate the alignment of the base address. + // + Alignment = LShiftU64 (1, (UINTN)LowBitSet64 (BaseAddress)); + + if (Alignment > Length) { + break; + } + + (*MtrrNumber)++; + BaseAddress += Alignment; + Length -= Alignment; + } while (TRUE); + + if (Length == 0) { + return TRUE; + } + } + + TempQword = Length; Positive = 0; Subtractive = 0; @@ -603,7 +732,7 @@ GetDirection ( Positive++; } while (TempQword != 0); - TempQword = Power2MaxMemory (LShiftU64 (Input, 1)) - Input; + TempQword = Power2MaxMemory (LShiftU64 (Length, 1)) - Length; Subtractive++; do { TempQword -= Power2MaxMemory (TempQword); @@ -611,10 +740,10 @@ GetDirection ( } while (TempQword != 0); if (Positive <= Subtractive) { - *MtrrNumber = Positive; + *MtrrNumber += Positive; return TRUE; } else { - *MtrrNumber = Subtractive; + *MtrrNumber += Subtractive; return FALSE; } } @@ -625,31 +754,30 @@ GetDirection ( This function programs MTRRs according to the values specified in the shadow array. - @param VariableMtrr The array to shadow variable MTRRs content + @param[in, out] VariableMtrr Shadow of variable MTRR contents **/ -STATIC VOID InvalidateMtrr ( - IN VARIABLE_MTRR *VariableMtrr - ) + IN OUT VARIABLE_MTRR *VariableMtrr + ) { - UINTN Index; - UINTN Cr4; - UINTN VariableMtrrCount; + UINTN Index; + UINTN VariableMtrrCount; + MTRR_CONTEXT MtrrContext; - Cr4 = PreMtrrChange (); + PreMtrrChange (&MtrrContext); Index = 0; VariableMtrrCount = GetVariableMtrrCount (); while (Index < VariableMtrrCount) { - if (VariableMtrr[Index].Valid == FALSE && VariableMtrr[Index].Used == TRUE ) { + if (!VariableMtrr[Index].Valid && VariableMtrr[Index].Used) { AsmWriteMsr64 (VariableMtrr[Index].Msr, 0); AsmWriteMsr64 (VariableMtrr[Index].Msr + 1, 0); VariableMtrr[Index].Used = FALSE; } Index ++; } - PostMtrrChange (Cr4); + PostMtrrChange (&MtrrContext); } @@ -658,14 +786,13 @@ InvalidateMtrr ( This function programs variable MTRRs - @param MtrrNumber Index of MTRR to program. - @param BaseAddress Base address of memory region. - @param Length Length of memory region. - @param MemoryCacheType Memory type to set. - @param MtrrValidAddressMask The valid address mask for MTRR + @param[in] MtrrNumber Index of MTRR to program. + @param[in] BaseAddress Base address of memory region. + @param[in] Length Length of memory region. + @param[in] MemoryCacheType Memory type to set. + @param[in] MtrrValidAddressMask The valid address mask for MTRR **/ -STATIC VOID ProgramVariableMtrr ( IN UINTN MtrrNumber, @@ -675,10 +802,10 @@ ProgramVariableMtrr ( IN UINT64 MtrrValidAddressMask ) { - UINT64 TempQword; - UINTN Cr4; + UINT64 TempQword; + MTRR_CONTEXT MtrrContext; - Cr4 = PreMtrrChange (); + PreMtrrChange (&MtrrContext); // // MTRR Physical Base @@ -695,19 +822,18 @@ ProgramVariableMtrr ( (TempQword & MtrrValidAddressMask) | MTRR_LIB_CACHE_MTRR_ENABLED ); - PostMtrrChange (Cr4); + PostMtrrChange (&MtrrContext); } /** - Convert the Memory attibute value to MTRR_MEMORY_CACHE_TYPE. + Converts the Memory attribute value to MTRR_MEMORY_CACHE_TYPE. - @param MtrrType MTRR memory type + @param[in] MtrrType MTRR memory type @return The enum item in MTRR_MEMORY_CACHE_TYPE **/ -STATIC MTRR_MEMORY_CACHE_TYPE GetMemoryCacheTypeFromMtrrType ( IN UINT64 MtrrType @@ -727,9 +853,9 @@ GetMemoryCacheTypeFromMtrrType ( default: // // MtrrType is MTRR_CACHE_INVALID_TYPE, that means - // no mtrr covers the range + // no MTRR covers the range // - return CacheUncacheable; + return MtrrGetDefaultMemoryType (); } } @@ -738,19 +864,18 @@ GetMemoryCacheTypeFromMtrrType ( This function initializes the valid bits mask and valid address mask for MTRRs. - @param MtrrValidBitsMask The mask for the valid bit of the MTRR - @param MtrrValidAddressMask The valid address mask for the MTRR + @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR + @param[out] MtrrValidAddressMask The valid address mask for the MTRR **/ -STATIC VOID MtrrLibInitializeMtrrMask ( OUT UINT64 *MtrrValidBitsMask, OUT UINT64 *MtrrValidAddressMask ) { - UINT32 RegEax; - UINT8 PhysicalAddressBits; + UINT32 RegEax; + UINT8 PhysicalAddressBits; AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL); @@ -762,28 +887,28 @@ MtrrLibInitializeMtrrMask ( *MtrrValidBitsMask = LShiftU64 (1, PhysicalAddressBits) - 1; *MtrrValidAddressMask = *MtrrValidBitsMask & 0xfffffffffffff000ULL; } else { - *MtrrValidBitsMask = MTRR_LIB_CACHE_VALID_ADDRESS; - *MtrrValidAddressMask = 0xFFFFFFFF; + *MtrrValidBitsMask = MTRR_LIB_MSR_VALID_MASK; + *MtrrValidAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS; } } /** - Determing the real attribute of a memory range. + Determines the real attribute of a memory range. This function is to arbitrate the real attribute of the memory when - there are 2 MTRR covers the same memory range. For further details, + there are 2 MTRRs covers the same memory range. For further details, please refer the IA32 Software Developer's Manual, Volume 3, Section 10.11.4.1. - @param MtrrType1 the first kind of Memory type - @param MtrrType2 the second kind of memory type + @param[in] MtrrType1 The first kind of Memory type + @param[in] MtrrType2 The second kind of memory type **/ UINT64 MtrrPrecedence ( - UINT64 MtrrType1, - UINT64 MtrrType2 + IN UINT64 MtrrType1, + IN UINT64 MtrrType2 ) { UINT64 MtrrType; @@ -843,11 +968,11 @@ MtrrPrecedence ( /** This function attempts to set the attributes for a memory range. - @param BaseAddress The physical address that is the start - address of a memory region. - @param Length The size in bytes of the memory region. - @param Attributes The bit mask of attributes to set for the - memory region. + @param[in] BaseAddress The physical address that is the start + address of a memory region. + @param[in] Length The size in bytes of the memory region. + @param[in] Attribute The bit mask of attributes to set for the + memory region. @retval RETURN_SUCCESS The attributes were set for the memory region. @@ -877,19 +1002,26 @@ MtrrSetMemoryAttribute ( UINT64 TempQword; RETURN_STATUS Status; UINT64 MemoryType; - UINT64 Remainder; + UINT64 Alignment; BOOLEAN OverLap; BOOLEAN Positive; UINT32 MsrNum; UINTN MtrrNumber; - VARIABLE_MTRR VariableMtrr[MAX_MTRR_NUMBER_OF_VARIABLE_MTRR]; + VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR]; UINT32 UsedMtrr; UINT64 MtrrValidBitsMask; UINT64 MtrrValidAddressMask; - UINTN Cr4; BOOLEAN OverwriteExistingMtrr; UINT32 FirmwareVariableMtrrCount; UINT32 VariableMtrrEnd; + MTRR_CONTEXT MtrrContext; + + DEBUG((DEBUG_CACHE, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, Length)); + + if (!IsMtrrSupported ()) { + Status = RETURN_UNSUPPORTED; + goto Done; + } FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1; @@ -904,14 +1036,16 @@ MtrrSetMemoryAttribute ( // Check for an invalid parameter // if (Length == 0) { - return RETURN_INVALID_PARAMETER; + Status = RETURN_INVALID_PARAMETER; + goto Done; } if ( - (BaseAddress &~MtrrValidAddressMask) != 0 || - (Length &~MtrrValidAddressMask) != 0 + (BaseAddress & ~MtrrValidAddressMask) != 0 || + (Length & ~MtrrValidAddressMask) != 0 ) { - return RETURN_UNSUPPORTED; + Status = RETURN_UNSUPPORTED; + goto Done; } // @@ -919,11 +1053,11 @@ MtrrSetMemoryAttribute ( // Status = RETURN_SUCCESS; while ((BaseAddress < BASE_1MB) && (Length > 0) && Status == RETURN_SUCCESS) { - Cr4 = PreMtrrChange (); + PreMtrrChange (&MtrrContext); Status = ProgramFixedMtrr (MemoryType, &BaseAddress, &Length); - PostMtrrChange (Cr4); + PostMtrrChange (&MtrrContext); if (RETURN_ERROR (Status)) { - return Status; + goto Done; } } @@ -938,30 +1072,17 @@ MtrrSetMemoryAttribute ( // // Since memory ranges below 1MB will be overridden by the fixed MTRRs, - // we can set the bade to 0 to save variable MTRRs. + // we can set the base to 0 to save variable MTRRs. // if (BaseAddress == BASE_1MB) { BaseAddress = 0; Length += SIZE_1MB; } - // - // Check memory base address alignment - // - DivU64x64Remainder (BaseAddress, Power2MaxMemory (LShiftU64 (Length, 1)), &Remainder); - if (Remainder != 0) { - DivU64x64Remainder (BaseAddress, Power2MaxMemory (Length), &Remainder); - if (Remainder != 0) { - Status = RETURN_UNSUPPORTED; - goto Done; - } - } - // // Check for overlap // - UsedMtrr = MAX_MTRR_NUMBER_OF_VARIABLE_MTRR; - MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask, MtrrValidAddressMask, &UsedMtrr, VariableMtrr); + UsedMtrr = MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask, MtrrValidAddressMask, VariableMtrr); OverLap = CheckMemoryAttributeOverlap (BaseAddress, BaseAddress + Length - 1, VariableMtrr); if (OverLap) { Status = CombineMemoryAttribute (MemoryType, &BaseAddress, &Length, VariableMtrr, &UsedMtrr, &OverwriteExistingMtrr); @@ -971,28 +1092,19 @@ MtrrSetMemoryAttribute ( if (Length == 0) { // - // Combined successfully + // Combined successfully, invalidate the now-unused MTRRs // + InvalidateMtrr(VariableMtrr); Status = RETURN_SUCCESS; goto Done; } } - // - // Program Variable MTRRs - // - // Avoid hardcode here and read data dynamically - // - if (UsedMtrr >= FirmwareVariableMtrrCount) { - Status = RETURN_OUT_OF_RESOURCES; - goto Done; - } - // // The memory type is the same with the type specified by // MTRR_LIB_IA32_MTRR_DEF_TYPE. // - if ((!OverwriteExistingMtrr) && (Attribute == GetMtrrDefaultMemoryType ())) { + if ((!OverwriteExistingMtrr) && (Attribute == MtrrGetDefaultMemoryType ())) { // // Invalidate the now-unused MTRRs // @@ -1000,22 +1112,75 @@ MtrrSetMemoryAttribute ( goto Done; } - TempQword = Length; + Positive = GetMtrrNumberAndDirection (BaseAddress, Length, &MtrrNumber); + if ((UsedMtrr + MtrrNumber) > FirmwareVariableMtrrCount) { + Status = RETURN_OUT_OF_RESOURCES; + goto Done; + } - if (TempQword == Power2MaxMemory (TempQword)) { - // - // Invalidate the now-unused MTRRs - // - InvalidateMtrr(VariableMtrr); + // + // Invalidate the now-unused MTRRs + // + InvalidateMtrr(VariableMtrr); + + // + // Find first unused MTRR + // + for (MsrNum = MTRR_LIB_IA32_VARIABLE_MTRR_BASE; + MsrNum < VariableMtrrEnd; + MsrNum += 2 + ) { + if ((AsmReadMsr64 (MsrNum + 1) & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) { + break; + } + } + + if (BaseAddress != 0) { + do { + // + // Calculate the alignment of the base address. + // + Alignment = LShiftU64 (1, (UINTN)LowBitSet64 (BaseAddress)); + + if (Alignment > Length) { + break; + } + + // + // Find unused MTRR + // + for (; MsrNum < VariableMtrrEnd; MsrNum += 2) { + if ((AsmReadMsr64 (MsrNum + 1) & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) { + break; + } + } + + ProgramVariableMtrr ( + MsrNum, + BaseAddress, + Alignment, + MemoryType, + MtrrValidAddressMask + ); + BaseAddress += Alignment; + Length -= Alignment; + } while (TRUE); + + if (Length == 0) { + goto Done; + } + } + + TempQword = Length; + + if (!Positive) { + Length = Power2MaxMemory (LShiftU64 (TempQword, 1)); // - // Find first unused MTRR + // Find unused MTRR // - for (MsrNum = MTRR_LIB_IA32_VARIABLE_MTRR_BASE; - MsrNum < VariableMtrrEnd; - MsrNum += 2 - ) { + for (; MsrNum < VariableMtrrEnd; MsrNum += 2) { if ((AsmReadMsr64 (MsrNum + 1) & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) { break; } @@ -1028,80 +1193,48 @@ MtrrSetMemoryAttribute ( MemoryType, MtrrValidAddressMask ); - } else { - - Positive = GetDirection (TempQword, &MtrrNumber); - - if ((UsedMtrr + MtrrNumber) > FirmwareVariableMtrrCount) { - Status = RETURN_OUT_OF_RESOURCES; - goto Done; - } - - // - // Invalidate the now-unused MTRRs - // - InvalidateMtrr(VariableMtrr); + BaseAddress += Length; + TempQword = Length - TempQword; + MemoryType = MTRR_CACHE_UNCACHEABLE; + } + do { // - // Find first unused MTRR + // Find unused MTRR // - for (MsrNum = MTRR_LIB_IA32_VARIABLE_MTRR_BASE; - MsrNum < VariableMtrrEnd; - MsrNum += 2 - ) { + for (; MsrNum < VariableMtrrEnd; MsrNum += 2) { if ((AsmReadMsr64 (MsrNum + 1) & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) { break; } } + Length = Power2MaxMemory (TempQword); if (!Positive) { - Length = Power2MaxMemory (LShiftU64 (TempQword, 1)); - ProgramVariableMtrr ( - MsrNum, - BaseAddress, - Length, - MemoryType, - MtrrValidAddressMask - ); - BaseAddress += Length; - TempQword = Length - TempQword; - MemoryType = MTRR_CACHE_UNCACHEABLE; + BaseAddress -= Length; } - do { - // - // Find unused MTRR - // - for (; MsrNum < VariableMtrrEnd; MsrNum += 2) { - if ((AsmReadMsr64 (MsrNum + 1) & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) { - break; - } - } - - Length = Power2MaxMemory (TempQword); - if (!Positive) { - BaseAddress -= Length; - } + ProgramVariableMtrr ( + MsrNum, + BaseAddress, + Length, + MemoryType, + MtrrValidAddressMask + ); - ProgramVariableMtrr ( - MsrNum, - BaseAddress, - Length, - MemoryType, - MtrrValidAddressMask - ); + if (Positive) { + BaseAddress += Length; + } + TempQword -= Length; - if (Positive) { - BaseAddress += Length; - } - TempQword -= Length; + } while (TempQword > 0); - } while (TempQword > 0); +Done: + DEBUG((DEBUG_CACHE, " Status = %r\n", Status)); + if (!RETURN_ERROR (Status)) { + MtrrDebugPrintAllMtrrs (); } -Done: return Status; - } @@ -1110,9 +1243,9 @@ Done: This function is mainly for debug purpose. - @param Address The specific address + @param[in] Address The specific address - @return Memory cache type of the sepcific address + @return Memory cache type of the specific address **/ MTRR_MEMORY_CACHE_TYPE @@ -1127,11 +1260,14 @@ MtrrGetMemoryAttribute ( UINT64 MtrrType; UINT64 TempMtrrType; MTRR_MEMORY_CACHE_TYPE CacheType; - VARIABLE_MTRR VariableMtrr[MAX_MTRR_NUMBER_OF_VARIABLE_MTRR]; + VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR]; UINT64 MtrrValidBitsMask; UINT64 MtrrValidAddressMask; UINTN VariableMtrrCount; - UINT32 UsedMtrr; + + if (!IsMtrrSupported ()) { + return CacheUncacheable; + } // // Check if MTRR is enabled, if not, return UC as attribute @@ -1152,16 +1288,16 @@ MtrrGetMemoryAttribute ( // Go through the fixed MTRR // for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { - if (Address >= MtrrLibFixedMtrrTable[Index].BaseAddress && + if (Address >= mMtrrLibFixedMtrrTable[Index].BaseAddress && Address < ( - MtrrLibFixedMtrrTable[Index].BaseAddress + - (MtrrLibFixedMtrrTable[Index].Length * 8) + mMtrrLibFixedMtrrTable[Index].BaseAddress + + (mMtrrLibFixedMtrrTable[Index].Length * 8) ) ) { SubIndex = - ((UINTN)Address - MtrrLibFixedMtrrTable[Index].BaseAddress) / - MtrrLibFixedMtrrTable[Index].Length; - TempQword = AsmReadMsr64 (MtrrLibFixedMtrrTable[Index].Msr); + ((UINTN)Address - mMtrrLibFixedMtrrTable[Index].BaseAddress) / + mMtrrLibFixedMtrrTable[Index].Length; + TempQword = AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr); MtrrType = RShiftU64 (TempQword, SubIndex * 8) & 0xFF; return GetMemoryCacheTypeFromMtrrType (MtrrType); } @@ -1169,11 +1305,9 @@ MtrrGetMemoryAttribute ( } } MtrrLibInitializeMtrrMask(&MtrrValidBitsMask, &MtrrValidAddressMask); - UsedMtrr = MAX_MTRR_NUMBER_OF_VARIABLE_MTRR; MtrrGetMemoryAttributeInVariableMtrr( MtrrValidBitsMask, MtrrValidAddressMask, - &UsedMtrr, VariableMtrr ); @@ -1181,6 +1315,8 @@ MtrrGetMemoryAttribute ( // Go through the variable MTRR // VariableMtrrCount = GetVariableMtrrCount (); + ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); + for (Index = 0; Index < VariableMtrrCount; Index++) { if (VariableMtrr[Index].Valid) { if (Address >= VariableMtrr[Index].BaseAddress && @@ -1197,23 +1333,24 @@ MtrrGetMemoryAttribute ( /** - This function will get the raw value in variable MTRRs + Worker function will get the raw value in variable MTRRs - @param VariableSettings A buffer to hold variable MTRRs content. + @param[out] VariableSettings A buffer to hold variable MTRRs content. @return The VariableSettings input pointer **/ MTRR_VARIABLE_SETTINGS* -EFIAPI -MtrrGetVariableMtrr ( - OUT MTRR_VARIABLE_SETTINGS *VariableSettings +MtrrGetVariableMtrrWorker ( + OUT MTRR_VARIABLE_SETTINGS *VariableSettings ) { UINT32 Index; UINT32 VariableMtrrCount; VariableMtrrCount = GetVariableMtrrCount (); + ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); + for (Index = 0; Index < VariableMtrrCount; Index++) { VariableSettings->Mtrr[Index].Base = AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1)); @@ -1224,11 +1361,34 @@ MtrrGetVariableMtrr ( return VariableSettings; } +/** + This function will get the raw value in variable MTRRs + + @param[out] VariableSettings A buffer to hold variable MTRRs content. + + @return The VariableSettings input pointer + +**/ +MTRR_VARIABLE_SETTINGS* +EFIAPI +MtrrGetVariableMtrr ( + OUT MTRR_VARIABLE_SETTINGS *VariableSettings + ) +{ + if (!IsMtrrSupported ()) { + return VariableSettings; + } + + return MtrrGetVariableMtrrWorker ( + VariableSettings + ); +} + /** Worker function setting variable MTRRs - @param VariableSettings A buffer to hold variable MTRRs content. + @param[in] VariableSettings A buffer to hold variable MTRRs content. **/ VOID @@ -1240,6 +1400,8 @@ MtrrSetVariableMtrrWorker ( UINT32 VariableMtrrCount; VariableMtrrCount = GetVariableMtrrCount (); + ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); + for (Index = 0; Index < VariableMtrrCount; Index++) { AsmWriteMsr64 ( MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1), @@ -1256,7 +1418,7 @@ MtrrSetVariableMtrrWorker ( /** This function sets variable MTRRs - @param VariableSettings A buffer to hold variable MTRRs content. + @param[in] VariableSettings A buffer to hold variable MTRRs content. @return The pointer of VariableSettings @@ -1267,26 +1429,28 @@ MtrrSetVariableMtrr ( IN MTRR_VARIABLE_SETTINGS *VariableSettings ) { - UINTN Cr4; + MTRR_CONTEXT MtrrContext; + + if (!IsMtrrSupported ()) { + return VariableSettings; + } - Cr4 = PreMtrrChange (); + PreMtrrChange (&MtrrContext); MtrrSetVariableMtrrWorker (VariableSettings); - PostMtrrChange (Cr4); + PostMtrrChange (&MtrrContext); return VariableSettings; } - /** - This function gets the content in fixed MTRRs + Worker function gets the content in fixed MTRRs - @param FixedSettings A buffer to hold fixed Mtrrs content. + @param[out] FixedSettings A buffer to hold fixed MTRRs content. @retval The pointer of FixedSettings **/ MTRR_FIXED_SETTINGS* -EFIAPI -MtrrGetFixedMtrr ( +MtrrGetFixedMtrrWorker ( OUT MTRR_FIXED_SETTINGS *FixedSettings ) { @@ -1294,16 +1458,38 @@ MtrrGetFixedMtrr ( for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { FixedSettings->Mtrr[Index] = - AsmReadMsr64 (MtrrLibFixedMtrrTable[Index].Msr); - }; + AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr); + } return FixedSettings; } + +/** + This function gets the content in fixed MTRRs + + @param[out] FixedSettings A buffer to hold fixed MTRRs content. + + @retval The pointer of FixedSettings + +**/ +MTRR_FIXED_SETTINGS* +EFIAPI +MtrrGetFixedMtrr ( + OUT MTRR_FIXED_SETTINGS *FixedSettings + ) +{ + if (!IsMtrrSupported ()) { + return FixedSettings; + } + + return MtrrGetFixedMtrrWorker (FixedSettings); +} + /** Worker function setting fixed MTRRs - @param FixedSettings A buffer to hold fixed Mtrrs content. + @param[in] FixedSettings A buffer to hold fixed Mtrrs content. **/ VOID @@ -1315,7 +1501,7 @@ MtrrSetFixedMtrrWorker ( for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { AsmWriteMsr64 ( - MtrrLibFixedMtrrTable[Index].Msr, + mMtrrLibFixedMtrrTable[Index].Msr, FixedSettings->Mtrr[Index] ); } @@ -1325,7 +1511,7 @@ MtrrSetFixedMtrrWorker ( /** This function sets fixed MTRRs - @param FixedSettings A buffer to hold fixed Mtrrs content. + @param[in] FixedSettings A buffer to hold fixed Mtrrs content. @retval The pointer of FixedSettings @@ -1336,11 +1522,15 @@ MtrrSetFixedMtrr ( IN MTRR_FIXED_SETTINGS *FixedSettings ) { - UINTN Cr4; + MTRR_CONTEXT MtrrContext; - Cr4 = PreMtrrChange (); + if (!IsMtrrSupported ()) { + return FixedSettings; + } + + PreMtrrChange (&MtrrContext); MtrrSetFixedMtrrWorker (FixedSettings); - PostMtrrChange (Cr4); + PostMtrrChange (&MtrrContext); return FixedSettings; } @@ -1349,7 +1539,7 @@ MtrrSetFixedMtrr ( /** This function gets the content in all MTRRs (variable and fixed) - @param MtrrSetting A buffer to hold all Mtrrs content. + @param[out] MtrrSetting A buffer to hold all Mtrrs content. @retval the pointer of MtrrSetting @@ -1360,6 +1550,10 @@ MtrrGetAllMtrrs ( OUT MTRR_SETTINGS *MtrrSetting ) { + if (!IsMtrrSupported ()) { + return MtrrSetting; + } + // // Get fixed MTRRs // @@ -1382,7 +1576,7 @@ MtrrGetAllMtrrs ( /** This function sets all MTRRs (variable and fixed) - @param MtrrSetting A buffer holding all MTRRs content. + @param[in] MtrrSetting A buffer holding all MTRRs content. @retval The pointer of MtrrSetting @@ -1393,9 +1587,13 @@ MtrrSetAllMtrrs ( IN MTRR_SETTINGS *MtrrSetting ) { - UINTN Cr4; + MTRR_CONTEXT MtrrContext; + + if (!IsMtrrSupported ()) { + return MtrrSetting; + } - Cr4 = PreMtrrChange (); + PreMtrrChange (&MtrrContext); // // Set fixed MTRRs @@ -1412,45 +1610,191 @@ MtrrSetAllMtrrs ( // AsmWriteMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, MtrrSetting->MtrrDefType); - PostMtrrChange (Cr4); + PostMtrrChangeEnableCache (&MtrrContext); return MtrrSetting; } - /** This function prints all MTRRs for debugging. **/ VOID +EFIAPI MtrrDebugPrintAllMtrrs ( + VOID ) { DEBUG_CODE ( - { - MTRR_SETTINGS MtrrSettings; - UINTN Index; - UINTN VariableMtrrCount; + MTRR_SETTINGS MtrrSettings; + UINTN Index; + UINTN Index1; + UINTN VariableMtrrCount; + UINT64 Base; + UINT64 Limit; + UINT64 MtrrBase; + UINT64 MtrrLimit; + UINT64 RangeBase; + UINT64 RangeLimit; + UINT64 NoRangeBase; + UINT64 NoRangeLimit; + UINT32 RegEax; + UINTN MemoryType; + UINTN PreviousMemoryType; + BOOLEAN Found; + + if (!IsMtrrSupported ()) { + return; + } - MtrrGetAllMtrrs (&MtrrSettings); - DEBUG((EFI_D_ERROR, "DefaultType = %016lx\n", MtrrSettings.MtrrDefType)); - for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { - DEBUG(( - EFI_D_ERROR, "Fixed[%02d] = %016lx\n", - Index, - MtrrSettings.Fixed.Mtrr[Index] - )); - } + DEBUG((DEBUG_CACHE, "MTRR Settings\n")); + DEBUG((DEBUG_CACHE, "=============\n")); + + MtrrGetAllMtrrs (&MtrrSettings); + DEBUG((DEBUG_CACHE, "MTRR Default Type: %016lx\n", MtrrSettings.MtrrDefType)); + for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { + DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d] : %016lx\n", Index, MtrrSettings.Fixed.Mtrr[Index])); + } - VariableMtrrCount = GetVariableMtrrCount (); - for (Index = 0; Index < VariableMtrrCount; Index++) { - DEBUG(( - EFI_D_ERROR, "Variable[%02d] = %016lx, %016lx\n", - Index, - MtrrSettings.Variables.Mtrr[Index].Base, - MtrrSettings.Variables.Mtrr[Index].Mask - )); + VariableMtrrCount = GetVariableMtrrCount (); + for (Index = 0; Index < VariableMtrrCount; Index++) { + DEBUG((DEBUG_CACHE, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n", + Index, + MtrrSettings.Variables.Mtrr[Index].Base, + MtrrSettings.Variables.Mtrr[Index].Mask + )); + } + DEBUG((DEBUG_CACHE, "\n")); + DEBUG((DEBUG_CACHE, "MTRR Ranges\n")); + DEBUG((DEBUG_CACHE, "====================================\n")); + + Base = 0; + PreviousMemoryType = MTRR_CACHE_INVALID_TYPE; + for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) { + Base = mMtrrLibFixedMtrrTable[Index].BaseAddress; + for (Index1 = 0; Index1 < 8; Index1++) { + MemoryType = (UINTN)(RShiftU64 (MtrrSettings.Fixed.Mtrr[Index], Index1 * 8) & 0xff); + if (MemoryType > CacheWriteBack) { + MemoryType = MTRR_CACHE_INVALID_TYPE; + } + if (MemoryType != PreviousMemoryType) { + if (PreviousMemoryType != MTRR_CACHE_INVALID_TYPE) { + DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1)); + } + PreviousMemoryType = MemoryType; + DEBUG((DEBUG_CACHE, "%a:%016lx-", mMtrrMemoryCacheTypeShortName[MemoryType], Base)); + } + Base += mMtrrLibFixedMtrrTable[Index].Length; } } + DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1)); + + VariableMtrrCount = GetVariableMtrrCount (); + + Limit = BIT36 - 1; + AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL); + if (RegEax >= 0x80000008) { + AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL); + Limit = LShiftU64 (1, RegEax & 0xff) - 1; + } + Base = BASE_1MB; + PreviousMemoryType = MTRR_CACHE_INVALID_TYPE; + do { + MemoryType = MtrrGetMemoryAttribute (Base); + if (MemoryType > CacheWriteBack) { + MemoryType = MTRR_CACHE_INVALID_TYPE; + } + + if (MemoryType != PreviousMemoryType) { + if (PreviousMemoryType != MTRR_CACHE_INVALID_TYPE) { + DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1)); + } + PreviousMemoryType = MemoryType; + DEBUG((DEBUG_CACHE, "%a:%016lx-", mMtrrMemoryCacheTypeShortName[MemoryType], Base)); + } + + RangeBase = BASE_1MB; + NoRangeBase = BASE_1MB; + RangeLimit = Limit; + NoRangeLimit = Limit; + + for (Index = 0, Found = FALSE; Index < VariableMtrrCount; Index++) { + if ((MtrrSettings.Variables.Mtrr[Index].Mask & BIT11) == 0) { + // + // If mask is not valid, then do not display range + // + continue; + } + MtrrBase = (MtrrSettings.Variables.Mtrr[Index].Base & (~(SIZE_4KB - 1))); + MtrrLimit = MtrrBase + ((~(MtrrSettings.Variables.Mtrr[Index].Mask & (~(SIZE_4KB - 1)))) & Limit); + + if (Base >= MtrrBase && Base < MtrrLimit) { + Found = TRUE; + } + + if (Base >= MtrrBase && MtrrBase > RangeBase) { + RangeBase = MtrrBase; + } + if (Base > MtrrLimit && MtrrLimit > RangeBase) { + RangeBase = MtrrLimit + 1; + } + if (Base < MtrrBase && MtrrBase < RangeLimit) { + RangeLimit = MtrrBase - 1; + } + if (Base < MtrrLimit && MtrrLimit <= RangeLimit) { + RangeLimit = MtrrLimit; + } + + if (Base > MtrrLimit && NoRangeBase < MtrrLimit) { + NoRangeBase = MtrrLimit + 1; + } + if (Base < MtrrBase && NoRangeLimit > MtrrBase) { + NoRangeLimit = MtrrBase - 1; + } + } + + if (Found) { + Base = RangeLimit + 1; + } else { + Base = NoRangeLimit + 1; + } + } while (Base < Limit); + DEBUG((DEBUG_CACHE, "%016lx\n\n", Base - 1)); ); } +/** + Checks if MTRR is supported. + + @retval TRUE MTRR is supported. + @retval FALSE MTRR is not supported. + +**/ +BOOLEAN +EFIAPI +IsMtrrSupported ( + VOID + ) +{ + UINT32 RegEdx; + UINT64 MtrrCap; + + // + // Check CPUID(1).EDX[12] for MTRR capability + // + AsmCpuid (1, NULL, NULL, NULL, &RegEdx); + if (BitFieldRead32 (RegEdx, 12, 12) == 0) { + return FALSE; + } + + // + // Check IA32_MTRRCAP.[0..7] for number of variable MTRRs and IA32_MTRRCAP[8] for + // fixed MTRRs existence. If number of variable MTRRs is zero, or fixed MTRRs do not + // exist, return false. + // + MtrrCap = AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP); + if ((BitFieldRead64 (MtrrCap, 0, 7) == 0) || (BitFieldRead64 (MtrrCap, 8, 8) == 0)) { + return FALSE; + } + + return TRUE; +}