From 07e889209034ba94bfac9e765b8a50ef344daef2 Mon Sep 17 00:00:00 2001 From: Jeff Fan Date: Thu, 21 Apr 2016 10:28:17 +0800 Subject: UefiCpuPkg/MtrrLib: Remove the loop of calculating Fixed-MTRR Mask Introduce the 32bit mask seeds to calculate Fixed-MTRR or&and mask values. It could avoid the loop operation and 64bit shift operations. Cc: Feng Tian Cc: Michael Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jeff Fan Reviewed-by: Feng Tian --- UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 39 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'UefiCpuPkg') diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c index 06a361bd84..c4a39b554d 100644 --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c @@ -20,6 +20,9 @@ #include #include +#define OR_SEED 0x01010101 +#define CLEAR_SEED 0xFFFFFFFF + // // Context to save and restore when MTRRs are programmed // @@ -460,11 +463,12 @@ ProgramFixedMtrr ( { UINT32 MsrNum; UINT32 ByteShift; - UINT64 OrMask; - UINT64 ClearMask; + UINT32 OrMask[2]; + UINT32 ClearMask[2]; + UINT64 SubLength; - OrMask = 0; - ClearMask = 0; + *(UINT64 *)OrMask = 0; + *(UINT64 *)ClearMask = 0; for (MsrNum = *LastMsrNum + 1; MsrNum < MTRR_NUMBER_OF_FIXED_MTRR; MsrNum++) { if ((*Base >= mMtrrLibFixedMtrrTable[MsrNum].BaseAddress) && @@ -493,24 +497,27 @@ ProgramFixedMtrr ( return RETURN_UNSUPPORTED; } - for ( - ; - ((ByteShift < 8) && (*Length >= mMtrrLibFixedMtrrTable[MsrNum].Length)); - ByteShift++ - ) { - OrMask |= LShiftU64 ((UINT64) MemoryCacheType, (UINT32) (ByteShift * 8)); - ClearMask |= LShiftU64 ((UINT64) 0xFF, (UINT32) (ByteShift * 8)); - *Length -= mMtrrLibFixedMtrrTable[MsrNum].Length; - *Base += mMtrrLibFixedMtrrTable[MsrNum].Length; + if (ByteShift < 4) { + OrMask[0] = OR_SEED * (UINT32)MemoryCacheType; + ClearMask[0] = CLEAR_SEED; + OrMask[1] = (OR_SEED * (UINT32)MemoryCacheType) >> ((4 - ByteShift) * 8); + ClearMask[1] = CLEAR_SEED >> ((4 - ByteShift) * 8); + } else { + OrMask[0] = (OR_SEED * (UINT32)MemoryCacheType) >> ((8 - ByteShift) * 8); + ClearMask[0] = CLEAR_SEED >> ((8 - ByteShift) * 8); } - if (ByteShift < 8 && (*Length != 0)) { + SubLength = mMtrrLibFixedMtrrTable[MsrNum].Length * (8 - ByteShift); + if (*Length < SubLength) { return RETURN_UNSUPPORTED; } + *Length -= SubLength; + *Base += SubLength; + *LastMsrNum = MsrNum; - *ReturnClearMask = ClearMask; - *ReturnOrMask = OrMask; + *ReturnClearMask = *(UINT64 *)ClearMask; + *ReturnOrMask = *(UINT64 *)OrMask; return RETURN_SUCCESS; } -- cgit v1.2.3