UefiCpuPkg/MtrrLib: Make use of worker functions to get MTRRs count

Try to make use of worker functions to get MTRRs count. It could avoid invoking
IsMtrrSupported() for many times.

Cc: Feng Tian <feng.tian@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
Signed-off-by: Jeff Fan <jeff.fan@intel.com>
Reviewed-by: Feng Tian <feng.tian@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@19155 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Michael Kinney 2015-12-08 05:22:42 +00:00 committed by vanjeff
parent 85b7f65b39
commit acf431e6f7
1 changed files with 51 additions and 25 deletions

View File

@ -367,13 +367,12 @@ MtrrGetFixedMtrr (
**/ **/
MTRR_VARIABLE_SETTINGS* MTRR_VARIABLE_SETTINGS*
MtrrGetVariableMtrrWorker ( MtrrGetVariableMtrrWorker (
IN UINT32 VariableMtrrCount,
OUT MTRR_VARIABLE_SETTINGS *VariableSettings OUT MTRR_VARIABLE_SETTINGS *VariableSettings
) )
{ {
UINT32 Index; UINT32 Index;
UINT32 VariableMtrrCount;
VariableMtrrCount = GetVariableMtrrCount ();
ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);
for (Index = 0; Index < VariableMtrrCount; Index++) { for (Index = 0; Index < VariableMtrrCount; Index++) {
@ -405,6 +404,7 @@ MtrrGetVariableMtrr (
} }
return MtrrGetVariableMtrrWorker ( return MtrrGetVariableMtrrWorker (
GetVariableMtrrCountWorker (),
VariableSettings VariableSettings
); );
} }
@ -527,7 +527,7 @@ MtrrGetMemoryAttributeInVariableMtrr (
return 0; return 0;
} }
FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCountWorker ();
VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1; VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1;
ZeroMem (VariableMtrr, sizeof (VARIABLE_MTRR) * MTRR_NUMBER_OF_VARIABLE_MTRR); ZeroMem (VariableMtrr, sizeof (VARIABLE_MTRR) * MTRR_NUMBER_OF_VARIABLE_MTRR);
@ -563,6 +563,8 @@ MtrrGetMemoryAttributeInVariableMtrr (
/** /**
Checks overlap between given memory range and MTRRs. Checks overlap between given memory range and MTRRs.
@param[in] FirmwareVariableMtrrCount The number of variable MTRRs available
to firmware.
@param[in] Start The start address of memory range. @param[in] Start The start address of memory range.
@param[in] End The end address of memory range. @param[in] End The end address of memory range.
@param[in] VariableMtrr The array to shadow variable MTRRs content @param[in] VariableMtrr The array to shadow variable MTRRs content
@ -573,14 +575,15 @@ MtrrGetMemoryAttributeInVariableMtrr (
**/ **/
BOOLEAN BOOLEAN
CheckMemoryAttributeOverlap ( CheckMemoryAttributeOverlap (
IN PHYSICAL_ADDRESS Start, IN UINTN FirmwareVariableMtrrCount,
IN PHYSICAL_ADDRESS End, IN PHYSICAL_ADDRESS Start,
IN VARIABLE_MTRR *VariableMtrr IN PHYSICAL_ADDRESS End,
IN VARIABLE_MTRR *VariableMtrr
) )
{ {
UINT32 Index; UINT32 Index;
for (Index = 0; Index < 6; Index++) { for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
if ( if (
VariableMtrr[Index].Valid && VariableMtrr[Index].Valid &&
!( !(
@ -623,6 +626,8 @@ InvalidateShadowMtrr (
If overlap exists between given memory range and MTRRs, try to combine them. If overlap exists between given memory range and MTRRs, try to combine them.
@param[in] FirmwareVariableMtrrCount The number of variable MTRRs
available to firmware.
@param[in] Attributes The memory type to set. @param[in] Attributes The memory type to set.
@param[in, out] Base The base address of memory range. @param[in, out] Base The base address of memory range.
@param[in, out] Length The length of memory range. @param[in, out] Length The length of memory range.
@ -636,6 +641,7 @@ InvalidateShadowMtrr (
**/ **/
RETURN_STATUS RETURN_STATUS
CombineMemoryAttribute ( CombineMemoryAttribute (
IN UINT32 FirmwareVariableMtrrCount,
IN UINT64 Attributes, IN UINT64 Attributes,
IN OUT UINT64 *Base, IN OUT UINT64 *Base,
IN OUT UINT64 *Length, IN OUT UINT64 *Length,
@ -649,11 +655,8 @@ CombineMemoryAttribute (
UINT64 CombineEnd; UINT64 CombineEnd;
UINT64 MtrrEnd; UINT64 MtrrEnd;
UINT64 EndAddress; UINT64 EndAddress;
UINT32 FirmwareVariableMtrrCount;
BOOLEAN CoveredByExistingMtrr; BOOLEAN CoveredByExistingMtrr;
FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();
*OverwriteExistingMtrr = FALSE; *OverwriteExistingMtrr = FALSE;
CoveredByExistingMtrr = FALSE; CoveredByExistingMtrr = FALSE;
EndAddress = *Base +*Length - 1; EndAddress = *Base +*Length - 1;
@ -851,21 +854,21 @@ GetMtrrNumberAndDirection (
This function programs MTRRs according to the values specified This function programs MTRRs according to the values specified
in the shadow array. in the shadow array.
@param[in] VariableMtrrCount Number of variable MTRRs
@param[in, out] VariableMtrr Shadow of variable MTRR contents @param[in, out] VariableMtrr Shadow of variable MTRR contents
**/ **/
VOID VOID
InvalidateMtrr ( InvalidateMtrr (
IN UINTN VariableMtrrCount,
IN OUT VARIABLE_MTRR *VariableMtrr IN OUT VARIABLE_MTRR *VariableMtrr
) )
{ {
UINTN Index; UINTN Index;
UINTN VariableMtrrCount;
MTRR_CONTEXT MtrrContext; MTRR_CONTEXT MtrrContext;
PreMtrrChange (&MtrrContext); PreMtrrChange (&MtrrContext);
Index = 0; Index = 0;
VariableMtrrCount = GetVariableMtrrCount ();
while (Index < VariableMtrrCount) { while (Index < VariableMtrrCount) {
if (!VariableMtrr[Index].Valid && VariableMtrr[Index].Used) { if (!VariableMtrr[Index].Valid && VariableMtrr[Index].Used) {
AsmWriteMsr64 (VariableMtrr[Index].Msr, 0); AsmWriteMsr64 (VariableMtrr[Index].Msr, 0);
@ -1139,7 +1142,7 @@ MtrrGetMemoryAttribute (
// //
// Go through the variable MTRR // Go through the variable MTRR
// //
VariableMtrrCount = GetVariableMtrrCount (); VariableMtrrCount = GetVariableMtrrCountWorker ();
ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);
for (Index = 0; Index < VariableMtrrCount; Index++) { for (Index = 0; Index < VariableMtrrCount; Index++) {
@ -1354,6 +1357,7 @@ MtrrSetMemoryAttribute (
UINT32 FirmwareVariableMtrrCount; UINT32 FirmwareVariableMtrrCount;
UINT32 VariableMtrrEnd; UINT32 VariableMtrrEnd;
MTRR_CONTEXT MtrrContext; MTRR_CONTEXT MtrrContext;
UINT32 VariableMtrrCount;
DEBUG((DEBUG_CACHE, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, Length)); DEBUG((DEBUG_CACHE, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, Length));
@ -1362,7 +1366,7 @@ MtrrSetMemoryAttribute (
goto Done; goto Done;
} }
FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCountWorker ();
VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1; VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1;
MtrrLibInitializeMtrrMask(&MtrrValidBitsMask, &MtrrValidAddressMask); MtrrLibInitializeMtrrMask(&MtrrValidBitsMask, &MtrrValidAddressMask);
@ -1418,13 +1422,32 @@ MtrrSetMemoryAttribute (
Length += SIZE_1MB; Length += SIZE_1MB;
} }
//
// Read all variable MTRRs
//
VariableMtrrCount = GetVariableMtrrCountWorker ();
// //
// Check for overlap // Check for overlap
// //
UsedMtrr = MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask, MtrrValidAddressMask, VariableMtrr); UsedMtrr = MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask, MtrrValidAddressMask, VariableMtrr);
OverLap = CheckMemoryAttributeOverlap (BaseAddress, BaseAddress + Length - 1, VariableMtrr); OverLap = CheckMemoryAttributeOverlap (
FirmwareVariableMtrrCount,
BaseAddress,
BaseAddress + Length - 1,
VariableMtrr
);
if (OverLap) { if (OverLap) {
Status = CombineMemoryAttribute (MemoryType, &BaseAddress, &Length, VariableMtrr, &UsedMtrr, &OverwriteExistingMtrr); Status = CombineMemoryAttribute (
FirmwareVariableMtrrCount,
MemoryType,
&BaseAddress,
&Length,
VariableMtrr,
&UsedMtrr,
&OverwriteExistingMtrr
);
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status)) {
goto Done; goto Done;
} }
@ -1433,7 +1456,7 @@ MtrrSetMemoryAttribute (
// //
// Combined successfully, invalidate the now-unused MTRRs // Combined successfully, invalidate the now-unused MTRRs
// //
InvalidateMtrr(VariableMtrr); InvalidateMtrr(VariableMtrrCount, VariableMtrr);
Status = RETURN_SUCCESS; Status = RETURN_SUCCESS;
goto Done; goto Done;
} }
@ -1447,7 +1470,7 @@ MtrrSetMemoryAttribute (
// //
// Invalidate the now-unused MTRRs // Invalidate the now-unused MTRRs
// //
InvalidateMtrr(VariableMtrr); InvalidateMtrr(VariableMtrrCount, VariableMtrr);
goto Done; goto Done;
} }
@ -1461,7 +1484,7 @@ MtrrSetMemoryAttribute (
// //
// Invalidate the now-unused MTRRs // Invalidate the now-unused MTRRs
// //
InvalidateMtrr(VariableMtrr); InvalidateMtrr(VariableMtrrCount, VariableMtrr);
// //
// Find first unused MTRR // Find first unused MTRR
@ -1589,7 +1612,7 @@ MtrrSetVariableMtrrWorker (
UINT32 Index; UINT32 Index;
UINT32 VariableMtrrCount; UINT32 VariableMtrrCount;
VariableMtrrCount = GetVariableMtrrCount (); VariableMtrrCount = GetVariableMtrrCountWorker ();
ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);
for (Index = 0; Index < VariableMtrrCount; Index++) { for (Index = 0; Index < VariableMtrrCount; Index++) {
@ -1634,7 +1657,7 @@ MtrrSetVariableMtrr (
/** /**
Worker function setting fixed MTRRs Worker function setting fixed MTRRs
@param[in] FixedSettings A buffer to hold fixed Mtrrs content. @param[in] FixedSettings A buffer to hold fixed MTRRs content.
**/ **/
VOID VOID
@ -1656,7 +1679,7 @@ MtrrSetFixedMtrrWorker (
/** /**
This function sets fixed MTRRs This function sets fixed MTRRs
@param[in] FixedSettings A buffer to hold fixed Mtrrs content. @param[in] FixedSettings A buffer to hold fixed MTRRs content.
@retval The pointer of FixedSettings @retval The pointer of FixedSettings
@ -1684,7 +1707,7 @@ MtrrSetFixedMtrr (
/** /**
This function gets the content in all MTRRs (variable and fixed) This function gets the content in all MTRRs (variable and fixed)
@param[out] MtrrSetting A buffer to hold all Mtrrs content. @param[out] MtrrSetting A buffer to hold all MTRRs content.
@retval the pointer of MtrrSetting @retval the pointer of MtrrSetting
@ -1702,12 +1725,15 @@ MtrrGetAllMtrrs (
// //
// Get fixed MTRRs // Get fixed MTRRs
// //
MtrrGetFixedMtrr (&MtrrSetting->Fixed); MtrrGetFixedMtrrWorker (&MtrrSetting->Fixed);
// //
// Get variable MTRRs // Get variable MTRRs
// //
MtrrGetVariableMtrr (&MtrrSetting->Variables); MtrrGetVariableMtrrWorker (
GetVariableMtrrCountWorker (),
&MtrrSetting->Variables
);
// //
// Get MTRR_DEF_TYPE value // Get MTRR_DEF_TYPE value