X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMtrrLib%2FMtrrLib.c;h=1f85ac7e65232bd1294cf2f9a9e1f1b4dd4bc909;hp=9c66f93ac2c64fcb9468bb42fd1c9b1e12f90d2d;hb=ffb4c72d7b637d4ac377fc7da7575069f15c288e;hpb=f6194f5a64c299cb33c96fc153aa83ca31531a06 diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c index 9c66f93ac2..1f85ac7e65 100644 --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c @@ -5,7 +5,7 @@ Most of services in this library instance are suggested to be invoked by BSP only, except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs. - Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.
+ Copyright (c) 2008 - 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 @@ -1570,7 +1570,7 @@ MtrrLibCalculateMtrrs ( // VectorCount = VectorIndex + 1; DEBUG (( - DEBUG_CACHE, "VectorCount (%016lx - %016lx) = %d\n", + DEBUG_CACHE, " VectorCount (%016lx - %016lx) = %d\n", Ranges[0].BaseAddress, Ranges[RangeCount - 1].BaseAddress + Ranges[RangeCount - 1].Length, VectorCount )); ASSERT (VectorCount < MAX_UINT16); @@ -1583,20 +1583,33 @@ MtrrLibCalculateMtrrs ( Vector[VectorCount - 1].Address = Base1; Weight = (UINT8 *) &Vector[VectorCount]; - // - // Set mandatory weight between any vector to max - // Set optional weight and between any vector and self->self to 0 - // E.g.: - // 00 FF FF FF - // 00 00 FF FF - // 00 00 00 FF - // 00 00 00 00 - // for (VectorIndex = 0; VectorIndex < VectorCount; VectorIndex++) { + // + // Set optional weight between vertices and self->self to 0 + // SetMem (&Weight[M(VectorIndex, 0)], VectorIndex + 1, 0); - if (VectorIndex != VectorCount - 1) { - Weight[M (VectorIndex, VectorIndex + 1)] = (DefaultType == Vector[VectorIndex].Type) ? 0 : 1; - SetMem (&Weight[M (VectorIndex, VectorIndex + 2)], VectorCount - VectorIndex - 2, MAX_WEIGHT); + // + // Set mandatory weight between vectors to MAX_WEIGHT + // + SetMem (&Weight[M (VectorIndex, VectorIndex + 1)], VectorCount - VectorIndex - 1, MAX_WEIGHT); + + // Final result looks like: + // 00 FF FF FF + // 00 00 FF FF + // 00 00 00 FF + // 00 00 00 00 + } + + // + // Set mandatory weight and optional weight for adjacent vertices + // + for (VectorIndex = 0; VectorIndex < VectorCount - 1; VectorIndex++) { + if (Vector[VectorIndex].Type != DefaultType) { + Weight[M (VectorIndex, VectorIndex + 1)] = 1; + Weight[O (VectorIndex, VectorIndex + 1)] = 0; + } else { + Weight[M (VectorIndex, VectorIndex + 1)] = 0; + Weight[O (VectorIndex, VectorIndex + 1)] = 1; } } @@ -1683,7 +1696,7 @@ MtrrLibCalculateMtrrs ( /** - Apply the variable MTRR settings to memory range array. + Apply the fixed MTRR settings to memory range array. @param Fixed The fixed MTRR settings. @param Ranges Return the memory range array holding memory type @@ -2209,6 +2222,7 @@ MtrrSetMemoryAttributesInMtrrSettings ( MTRR_CONTEXT MtrrContext; BOOLEAN MtrrContextValid; + Status = RETURN_SUCCESS; MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask); // @@ -2226,24 +2240,48 @@ MtrrSetMemoryAttributesInMtrrSettings ( Above1MbExist = FALSE; OriginalVariableMtrrCount = 0; + // + // 0. Dump the requests. + // + DEBUG_CODE ( + DEBUG ((DEBUG_CACHE, "Mtrr: Set Mem Attribute to %a, ScratchSize = %x%a", + (MtrrSetting == NULL) ? "Hardware" : "Buffer", *ScratchSize, + (RangeCount <= 1) ? "," : "\n" + )); + for (Index = 0; Index < RangeCount; Index++) { + DEBUG ((DEBUG_CACHE, " %a: [%016lx, %016lx)\n", + mMtrrMemoryCacheTypeShortName[MIN (Ranges[Index].Type, CacheInvalid)], + Ranges[Index].BaseAddress, Ranges[Index].BaseAddress + Ranges[Index].Length + )); + } + ); + // // 1. Validate the parameters. // + if (!IsMtrrSupported ()) { + Status = RETURN_UNSUPPORTED; + goto Exit; + } + for (Index = 0; Index < RangeCount; Index++) { if (Ranges[Index].Length == 0) { - return RETURN_INVALID_PARAMETER; + Status = RETURN_INVALID_PARAMETER; + goto Exit; } if (((Ranges[Index].BaseAddress & ~MtrrValidAddressMask) != 0) || ((Ranges[Index].Length & ~MtrrValidAddressMask) != 0) ) { - return RETURN_UNSUPPORTED; + Status = RETURN_UNSUPPORTED; + goto Exit; } if ((Ranges[Index].Type != CacheUncacheable) && (Ranges[Index].Type != CacheWriteCombining) && (Ranges[Index].Type != CacheWriteThrough) && (Ranges[Index].Type != CacheWriteProtected) && (Ranges[Index].Type != CacheWriteBack)) { - return RETURN_INVALID_PARAMETER; + Status = RETURN_INVALID_PARAMETER; + goto Exit; } if (Ranges[Index].BaseAddress + Ranges[Index].Length > BASE_1MB) { Above1MbExist = TRUE; @@ -2309,7 +2347,7 @@ MtrrSetMemoryAttributesInMtrrSettings ( if (Status == RETURN_ALREADY_STARTED) { Status = RETURN_SUCCESS; } else if (Status == RETURN_OUT_OF_RESOURCES) { - return Status; + goto Exit; } else { ASSERT_RETURN_ERROR (Status); Modified = TRUE; @@ -2327,7 +2365,7 @@ MtrrSetMemoryAttributesInMtrrSettings ( WorkingVariableMtrr, FirmwareVariableMtrrCount + 1, &WorkingVariableMtrrCount ); if (RETURN_ERROR (Status)) { - return Status; + goto Exit; } // @@ -2346,7 +2384,8 @@ MtrrSetMemoryAttributesInMtrrSettings ( } if (WorkingVariableMtrrCount > FirmwareVariableMtrrCount) { - return RETURN_OUT_OF_RESOURCES; + Status = RETURN_OUT_OF_RESOURCES; + goto Exit; } // @@ -2375,7 +2414,7 @@ MtrrSetMemoryAttributesInMtrrSettings ( Ranges[Index].BaseAddress, Ranges[Index].Length, Ranges[Index].Type ); if (RETURN_ERROR (Status)) { - return Status; + goto Exit; } } @@ -2441,7 +2480,12 @@ MtrrSetMemoryAttributesInMtrrSettings ( } } - return RETURN_SUCCESS; +Exit: + DEBUG ((DEBUG_CACHE, " Result = %r\n", Status)); + if (!RETURN_ERROR (Status)) { + MtrrDebugPrintAllMtrrsWorker (MtrrSetting); + } + return Status; } /** @@ -2475,28 +2519,15 @@ MtrrSetMemoryAttributeInMtrrSettings ( IN MTRR_MEMORY_CACHE_TYPE Attribute ) { - RETURN_STATUS Status; UINT8 Scratch[SCRATCH_BUFFER_SIZE]; UINTN ScratchSize; MTRR_MEMORY_RANGE Range; - if (!IsMtrrSupported ()) { - return RETURN_UNSUPPORTED; - } - Range.BaseAddress = BaseAddress; Range.Length = Length; Range.Type = Attribute; ScratchSize = sizeof (Scratch); - Status = MtrrSetMemoryAttributesInMtrrSettings (MtrrSetting, Scratch, &ScratchSize, &Range, 1); - DEBUG ((DEBUG_CACHE, "MtrrSetMemoryAttribute(MtrrSettings = %p) %s: [%016lx, %016lx) - %x\n", - MtrrSetting, - mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, BaseAddress + Length, Status)); - - if (!RETURN_ERROR (Status)) { - MtrrDebugPrintAllMtrrsWorker (MtrrSetting); - } - return Status; + return MtrrSetMemoryAttributesInMtrrSettings (MtrrSetting, Scratch, &ScratchSize, &Range, 1); } /** @@ -2788,6 +2819,7 @@ MtrrDebugPrintAllMtrrsWorker ( UINT64 MtrrValidBitsMask; UINT64 MtrrValidAddressMask; UINT32 VariableMtrrCount; + BOOLEAN ContainVariableMtrr; MTRR_MEMORY_RANGE Ranges[ ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1 ]; @@ -2809,13 +2841,13 @@ MtrrDebugPrintAllMtrrsWorker ( // // Dump RAW MTRR contents // - DEBUG((DEBUG_CACHE, "MTRR Settings\n")); - DEBUG((DEBUG_CACHE, "=============\n")); - DEBUG((DEBUG_CACHE, "MTRR Default Type: %016lx\n", Mtrrs->MtrrDefType)); + DEBUG ((DEBUG_CACHE, "MTRR Settings:\n")); + DEBUG ((DEBUG_CACHE, "=============\n")); + DEBUG ((DEBUG_CACHE, "MTRR Default Type: %016lx\n", Mtrrs->MtrrDefType)); for (Index = 0; Index < ARRAY_SIZE (mMtrrLibFixedMtrrTable); Index++) { - DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d] : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index])); + DEBUG ((DEBUG_CACHE, "Fixed MTRR[%02d] : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index])); } - + ContainVariableMtrr = FALSE; for (Index = 0; Index < VariableMtrrCount; Index++) { if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&Mtrrs->Variables.Mtrr[Index].Mask)->Bits.V == 0) { // @@ -2823,18 +2855,22 @@ MtrrDebugPrintAllMtrrsWorker ( // continue; } + ContainVariableMtrr = TRUE; DEBUG ((DEBUG_CACHE, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n", Index, Mtrrs->Variables.Mtrr[Index].Base, Mtrrs->Variables.Mtrr[Index].Mask )); } + if (!ContainVariableMtrr) { + DEBUG ((DEBUG_CACHE, "Variable MTRR : None.\n")); + } DEBUG((DEBUG_CACHE, "\n")); // // Dump MTRR setting in ranges // - DEBUG((DEBUG_CACHE, "MTRR Ranges\n")); + DEBUG((DEBUG_CACHE, "Memory Ranges:\n")); DEBUG((DEBUG_CACHE, "====================================\n")); MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask); Ranges[0].BaseAddress = 0;