UefiCpuPkg/MtrrLib: Optimize MtrrLibLeastAlignment()

The patch changes MtrrLibLeastAlignment() to
MtrrLibBiggestAlignment() and optimizes the implementation
to be more efficient.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
This commit is contained in:
Ruiyu Ni 2017-09-27 17:47:37 +08:00
parent 5fbb5adea8
commit 1416ecb4a9
1 changed files with 13 additions and 12 deletions

View File

@ -656,7 +656,8 @@ MtrrGetMemoryAttributeInVariableMtrr (
} }
/** /**
Return the least alignment of address. Return the biggest alignment (lowest set bit) of address.
The function is equivalent to: 1 << LowBitSet64 (Address).
@param Address The address to return the alignment. @param Address The address to return the alignment.
@param Alignment0 The alignment to return when Address is 0. @param Alignment0 The alignment to return when Address is 0.
@ -664,7 +665,7 @@ MtrrGetMemoryAttributeInVariableMtrr (
@return The least alignment of the Address. @return The least alignment of the Address.
**/ **/
UINT64 UINT64
MtrrLibLeastAlignment ( MtrrLibBiggestAlignment (
UINT64 Address, UINT64 Address,
UINT64 Alignment0 UINT64 Alignment0
) )
@ -673,7 +674,7 @@ MtrrLibLeastAlignment (
return Alignment0; return Alignment0;
} }
return LShiftU64 (1, (UINTN) LowBitSet64 (Address)); return Address & ((~Address) + 1);
} }
/** /**
@ -705,12 +706,12 @@ MtrrLibGetPositiveMtrrNumber (
// //
for (MtrrNumber = 0; Length != 0; MtrrNumber++) { for (MtrrNumber = 0; Length != 0; MtrrNumber++) {
if (UseLeastAlignment) { if (UseLeastAlignment) {
SubLength = MtrrLibLeastAlignment (BaseAddress, Alignment0); SubLength = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
if (SubLength > Length) { if (SubLength > Length) {
// //
// Set a flag when remaining length is too small // Set a flag when remaining length is too small
// so that MtrrLibLeastAlignment() is not called in following loops. // so that MtrrLibBiggestAlignment() is not called in following loops.
// //
UseLeastAlignment = FALSE; UseLeastAlignment = FALSE;
} }
@ -873,7 +874,7 @@ MtrrLibGetMtrrNumber (
// Left subtraction bit by bit, to find the optimal left subtraction solution. // Left subtraction bit by bit, to find the optimal left subtraction solution.
// //
for (SubtractiveMtrrNumber = 0, SubtractiveCount = 1; BaseAddress != 0; SubtractiveCount++) { for (SubtractiveMtrrNumber = 0, SubtractiveCount = 1; BaseAddress != 0; SubtractiveCount++) {
Alignment = MtrrLibLeastAlignment (BaseAddress, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
// //
// Check whether the memory type of [BaseAddress - Alignment, BaseAddress) can override Type. // Check whether the memory type of [BaseAddress - Alignment, BaseAddress) can override Type.
@ -928,7 +929,7 @@ MtrrLibGetMtrrNumber (
// //
MiddleMtrrNumber = 0; MiddleMtrrNumber = 0;
while (Length != 0) { while (Length != 0) {
BaseAlignment = MtrrLibLeastAlignment (BaseAddress, Alignment0); BaseAlignment = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
if (BaseAlignment > Length) { if (BaseAlignment > Length) {
break; break;
} }
@ -953,7 +954,7 @@ MtrrLibGetMtrrNumber (
LeastRightMtrrNumber = MtrrLibGetPositiveMtrrNumber (BaseAddress, Length, Alignment0); LeastRightMtrrNumber = MtrrLibGetPositiveMtrrNumber (BaseAddress, Length, Alignment0);
for (SubtractiveCount = 1; Length < BaseAlignment; SubtractiveCount++) { for (SubtractiveCount = 1; Length < BaseAlignment; SubtractiveCount++) {
Alignment = MtrrLibLeastAlignment (BaseAddress + Length, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress + Length, Alignment0);
if (!MtrrLibSubstractable (Ranges, RangeCount, Type, BaseAddress + Length, Alignment)) { if (!MtrrLibSubstractable (Ranges, RangeCount, Type, BaseAddress + Length, Alignment)) {
break; break;
} }
@ -1644,7 +1645,7 @@ MtrrLibSetMemoryAttributeInVariableMtrr (
} }
while (SubtractiveLeft-- != 0) { while (SubtractiveLeft-- != 0) {
Alignment = MtrrLibLeastAlignment (BaseAddress, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
ASSERT (Alignment <= Length); ASSERT (Alignment <= Length);
MtrrLibAddVariableMtrr (Ranges, RangeCount, VariableMtrr, VariableMtrrCapacity, VariableMtrrCount, MtrrLibAddVariableMtrr (Ranges, RangeCount, VariableMtrr, VariableMtrrCapacity, VariableMtrrCount,
@ -1654,7 +1655,7 @@ MtrrLibSetMemoryAttributeInVariableMtrr (
} }
while (Length != 0) { while (Length != 0) {
Alignment = MtrrLibLeastAlignment (BaseAddress, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
if (Alignment > Length) { if (Alignment > Length) {
break; break;
} }
@ -1665,7 +1666,7 @@ MtrrLibSetMemoryAttributeInVariableMtrr (
} }
while (SubtractiveRight-- != 0) { while (SubtractiveRight-- != 0) {
Alignment = MtrrLibLeastAlignment (BaseAddress + Length, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress + Length, Alignment0);
MtrrLibAddVariableMtrr (Ranges, RangeCount, VariableMtrr, VariableMtrrCapacity, VariableMtrrCount, MtrrLibAddVariableMtrr (Ranges, RangeCount, VariableMtrr, VariableMtrrCapacity, VariableMtrrCount,
BaseAddress + Length, Alignment, CacheInvalid, Alignment0); BaseAddress + Length, Alignment, CacheInvalid, Alignment0);
Length += Alignment; Length += Alignment;
@ -1674,7 +1675,7 @@ MtrrLibSetMemoryAttributeInVariableMtrr (
UseLeastAlignment = TRUE; UseLeastAlignment = TRUE;
while (Length != 0) { while (Length != 0) {
if (UseLeastAlignment) { if (UseLeastAlignment) {
Alignment = MtrrLibLeastAlignment (BaseAddress, Alignment0); Alignment = MtrrLibBiggestAlignment (BaseAddress, Alignment0);
if (Alignment > Length) { if (Alignment > Length) {
UseLeastAlignment = FALSE; UseLeastAlignment = FALSE;
} }