5 Most of services in this library instance are suggested to be invoked by BSP only,
6 except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.
8 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Register/Cpuid.h>
22 #include <Register/Msr.h>
24 #include <Library/MtrrLib.h>
25 #include <Library/BaseLib.h>
26 #include <Library/CpuLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/DebugLib.h>
30 #define OR_SEED 0x0101010101010101ull
31 #define CLEAR_SEED 0xFFFFFFFFFFFFFFFFull
33 #define MTRR_LIB_ASSERT_ALIGNED(B, L) ASSERT ((B & ~(L - 1)) == B);
35 // Context to save and restore when MTRRs are programmed
39 BOOLEAN InterruptState
;
45 MTRR_MEMORY_CACHE_TYPE Type
;
49 // This table defines the offset, base and length of the fixed MTRRs
51 CONST FIXED_MTRR mMtrrLibFixedMtrrTable
[] = {
53 MSR_IA32_MTRR_FIX64K_00000
,
58 MSR_IA32_MTRR_FIX16K_80000
,
63 MSR_IA32_MTRR_FIX16K_A0000
,
68 MSR_IA32_MTRR_FIX4K_C0000
,
73 MSR_IA32_MTRR_FIX4K_C8000
,
78 MSR_IA32_MTRR_FIX4K_D0000
,
83 MSR_IA32_MTRR_FIX4K_D8000
,
88 MSR_IA32_MTRR_FIX4K_E0000
,
93 MSR_IA32_MTRR_FIX4K_E8000
,
98 MSR_IA32_MTRR_FIX4K_F0000
,
103 MSR_IA32_MTRR_FIX4K_F8000
,
110 // Lookup table used to print MTRRs
112 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mMtrrMemoryCacheTypeShortName
[] = {
113 "UC", // CacheUncacheable
114 "WC", // CacheWriteCombining
117 "WT", // CacheWriteThrough
118 "WP", // CacheWriteProtected
119 "WB", // CacheWriteBack
124 Worker function returns the variable MTRR count for the CPU.
126 @return Variable MTRR count
130 GetVariableMtrrCountWorker (
134 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
136 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
137 ASSERT (MtrrCap
.Bits
.VCNT
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
138 return MtrrCap
.Bits
.VCNT
;
142 Returns the variable MTRR count for the CPU.
144 @return Variable MTRR count
149 GetVariableMtrrCount (
153 if (!IsMtrrSupported ()) {
156 return GetVariableMtrrCountWorker ();
160 Worker function returns the firmware usable variable MTRR count for the CPU.
162 @return Firmware usable variable MTRR count
166 GetFirmwareVariableMtrrCountWorker (
170 UINT32 VariableMtrrCount
;
171 UINT32 ReservedMtrrNumber
;
173 VariableMtrrCount
= GetVariableMtrrCountWorker ();
174 ReservedMtrrNumber
= PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
175 if (VariableMtrrCount
< ReservedMtrrNumber
) {
179 return VariableMtrrCount
- ReservedMtrrNumber
;
183 Returns the firmware usable variable MTRR count for the CPU.
185 @return Firmware usable variable MTRR count
190 GetFirmwareVariableMtrrCount (
194 if (!IsMtrrSupported ()) {
197 return GetFirmwareVariableMtrrCountWorker ();
201 Worker function returns the default MTRR cache type for the system.
203 If MtrrSetting is not NULL, returns the default MTRR cache type from input
204 MTRR settings buffer.
205 If MtrrSetting is NULL, returns the default MTRR cache type from MSR.
207 @param[in] MtrrSetting A buffer holding all MTRRs content.
209 @return The default MTRR cache type.
212 MTRR_MEMORY_CACHE_TYPE
213 MtrrGetDefaultMemoryTypeWorker (
214 IN MTRR_SETTINGS
*MtrrSetting
217 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
219 if (MtrrSetting
== NULL
) {
220 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
222 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
225 return (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
230 Returns the default MTRR cache type for the system.
232 @return The default MTRR cache type.
235 MTRR_MEMORY_CACHE_TYPE
237 MtrrGetDefaultMemoryType (
241 if (!IsMtrrSupported ()) {
242 return CacheUncacheable
;
244 return MtrrGetDefaultMemoryTypeWorker (NULL
);
248 Preparation before programming MTRR.
250 This function will do some preparation for programming MTRRs:
251 disable cache, invalid cache and disable MTRR caching functionality
253 @param[out] MtrrContext Pointer to context to save
257 MtrrLibPreMtrrChange (
258 OUT MTRR_CONTEXT
*MtrrContext
261 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
263 // Disable interrupts and save current interrupt state
265 MtrrContext
->InterruptState
= SaveAndDisableInterrupts();
268 // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29)
273 // Save original CR4 value and clear PGE flag (Bit 7)
275 MtrrContext
->Cr4
= AsmReadCr4 ();
276 AsmWriteCr4 (MtrrContext
->Cr4
& (~BIT7
));
286 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
288 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, DefType
.Uint64
);
292 Cleaning up after programming MTRRs.
294 This function will do some clean up after programming MTRRs:
295 Flush all TLBs, re-enable caching, restore CR4.
297 @param[in] MtrrContext Pointer to context to restore
301 MtrrLibPostMtrrChangeEnableCache (
302 IN MTRR_CONTEXT
*MtrrContext
311 // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29)
316 // Restore original CR4 value
318 AsmWriteCr4 (MtrrContext
->Cr4
);
321 // Restore original interrupt state
323 SetInterruptState (MtrrContext
->InterruptState
);
327 Cleaning up after programming MTRRs.
329 This function will do some clean up after programming MTRRs:
330 enable MTRR caching functionality, and enable cache
332 @param[in] MtrrContext Pointer to context to restore
336 MtrrLibPostMtrrChange (
337 IN MTRR_CONTEXT
*MtrrContext
340 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
344 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
347 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, DefType
.Uint64
);
349 MtrrLibPostMtrrChangeEnableCache (MtrrContext
);
353 Worker function gets the content in fixed MTRRs
355 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
357 @retval The pointer of FixedSettings
361 MtrrGetFixedMtrrWorker (
362 OUT MTRR_FIXED_SETTINGS
*FixedSettings
367 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
368 FixedSettings
->Mtrr
[Index
] =
369 AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
372 return FixedSettings
;
377 This function gets the content in fixed MTRRs
379 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
381 @retval The pointer of FixedSettings
387 OUT MTRR_FIXED_SETTINGS
*FixedSettings
390 if (!IsMtrrSupported ()) {
391 return FixedSettings
;
394 return MtrrGetFixedMtrrWorker (FixedSettings
);
399 Worker function will get the raw value in variable MTRRs
401 If MtrrSetting is not NULL, gets the variable MTRRs raw value from input
402 MTRR settings buffer.
403 If MtrrSetting is NULL, gets the variable MTRRs raw value from MTRRs.
405 @param[in] MtrrSetting A buffer holding all MTRRs content.
406 @param[in] VariableMtrrCount Number of variable MTRRs.
407 @param[out] VariableSettings A buffer to hold variable MTRRs content.
409 @return The VariableSettings input pointer
412 MTRR_VARIABLE_SETTINGS
*
413 MtrrGetVariableMtrrWorker (
414 IN MTRR_SETTINGS
*MtrrSetting
,
415 IN UINT32 VariableMtrrCount
,
416 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
421 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
423 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
424 if (MtrrSetting
== NULL
) {
425 VariableSettings
->Mtrr
[Index
].Base
=
426 AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1));
427 VariableSettings
->Mtrr
[Index
].Mask
=
428 AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1));
430 VariableSettings
->Mtrr
[Index
].Base
= MtrrSetting
->Variables
.Mtrr
[Index
].Base
;
431 VariableSettings
->Mtrr
[Index
].Mask
= MtrrSetting
->Variables
.Mtrr
[Index
].Mask
;
435 return VariableSettings
;
439 This function will get the raw value in variable MTRRs
441 @param[out] VariableSettings A buffer to hold variable MTRRs content.
443 @return The VariableSettings input pointer
446 MTRR_VARIABLE_SETTINGS
*
448 MtrrGetVariableMtrr (
449 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
452 if (!IsMtrrSupported ()) {
453 return VariableSettings
;
456 return MtrrGetVariableMtrrWorker (
458 GetVariableMtrrCountWorker (),
464 Programs fixed MTRRs registers.
466 @param[in] Type The memory type to set.
467 @param[in, out] Base The base address of memory range.
468 @param[in, out] Length The length of memory range.
469 @param[in, out] LastMsrNum On input, the last index of the fixed MTRR MSR to program.
470 On return, the current index of the fixed MTRR MSR to program.
471 @param[out] ReturnClearMask The bits to clear in the fixed MTRR MSR.
472 @param[out] ReturnOrMask The bits to set in the fixed MTRR MSR.
474 @retval RETURN_SUCCESS The cache type was updated successfully
475 @retval RETURN_UNSUPPORTED The requested range or cache type was invalid
480 MtrrLibProgramFixedMtrr (
481 IN MTRR_MEMORY_CACHE_TYPE Type
,
483 IN OUT UINT64
*Length
,
484 IN OUT UINT32
*LastMsrNum
,
485 OUT UINT64
*ReturnClearMask
,
486 OUT UINT64
*ReturnOrMask
490 UINT32 LeftByteShift
;
491 UINT32 RightByteShift
;
497 // Find the fixed MTRR index to be programmed
499 for (MsrNum
= *LastMsrNum
+ 1; MsrNum
< MTRR_NUMBER_OF_FIXED_MTRR
; MsrNum
++) {
500 if ((*Base
>= mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
) &&
503 mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
+
504 (8 * mMtrrLibFixedMtrrTable
[MsrNum
].Length
)
512 if (MsrNum
== MTRR_NUMBER_OF_FIXED_MTRR
) {
513 return RETURN_UNSUPPORTED
;
517 // Find the begin offset in fixed MTRR and calculate byte offset of left shift
519 LeftByteShift
= ((UINT32
)*Base
- mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
)
520 / mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
522 if (LeftByteShift
>= 8) {
523 return RETURN_UNSUPPORTED
;
527 // Find the end offset in fixed MTRR and calculate byte offset of right shift
529 SubLength
= mMtrrLibFixedMtrrTable
[MsrNum
].Length
* (8 - LeftByteShift
);
530 if (*Length
>= SubLength
) {
533 RightByteShift
= 8 - LeftByteShift
-
534 (UINT32
)(*Length
) / mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
535 if ((LeftByteShift
>= 8) ||
536 (((UINT32
)(*Length
) % mMtrrLibFixedMtrrTable
[MsrNum
].Length
) != 0)
538 return RETURN_UNSUPPORTED
;
541 // Update SubLength by actual length
546 ClearMask
= CLEAR_SEED
;
547 OrMask
= MultU64x32 (OR_SEED
, (UINT32
) Type
);
549 if (LeftByteShift
!= 0) {
551 // Clear the low bits by LeftByteShift
553 ClearMask
&= LShiftU64 (ClearMask
, LeftByteShift
* 8);
554 OrMask
&= LShiftU64 (OrMask
, LeftByteShift
* 8);
557 if (RightByteShift
!= 0) {
559 // Clear the high bits by RightByteShift
561 ClearMask
&= RShiftU64 (ClearMask
, RightByteShift
* 8);
562 OrMask
&= RShiftU64 (OrMask
, RightByteShift
* 8);
565 *Length
-= SubLength
;
568 *LastMsrNum
= MsrNum
;
569 *ReturnClearMask
= ClearMask
;
570 *ReturnOrMask
= OrMask
;
572 return RETURN_SUCCESS
;
577 Worker function gets the attribute of variable MTRRs.
579 This function shadows the content of variable MTRRs into an
580 internal array: VariableMtrr.
582 @param[in] VariableSettings The variable MTRR values to shadow
583 @param[in] VariableMtrrCount The number of variable MTRRs
584 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
585 @param[in] MtrrValidAddressMask The valid address mask for MTRR
586 @param[out] VariableMtrr The array to shadow variable MTRRs content
588 @return Number of MTRRs which has been used.
592 MtrrGetMemoryAttributeInVariableMtrrWorker (
593 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
,
594 IN UINTN VariableMtrrCount
,
595 IN UINT64 MtrrValidBitsMask
,
596 IN UINT64 MtrrValidAddressMask
,
597 OUT VARIABLE_MTRR
*VariableMtrr
603 ZeroMem (VariableMtrr
, sizeof (VARIABLE_MTRR
) * MTRR_NUMBER_OF_VARIABLE_MTRR
);
604 for (Index
= 0, UsedMtrr
= 0; Index
< VariableMtrrCount
; Index
++) {
605 if (((MSR_IA32_MTRR_PHYSMASK_REGISTER
*) &VariableSettings
->Mtrr
[Index
].Mask
)->Bits
.V
!= 0) {
606 VariableMtrr
[Index
].Msr
= (UINT32
)Index
;
607 VariableMtrr
[Index
].BaseAddress
= (VariableSettings
->Mtrr
[Index
].Base
& MtrrValidAddressMask
);
608 VariableMtrr
[Index
].Length
= ((~(VariableSettings
->Mtrr
[Index
].Mask
& MtrrValidAddressMask
)) & MtrrValidBitsMask
) + 1;
609 VariableMtrr
[Index
].Type
= (VariableSettings
->Mtrr
[Index
].Base
& 0x0ff);
610 VariableMtrr
[Index
].Valid
= TRUE
;
611 VariableMtrr
[Index
].Used
= TRUE
;
620 Gets the attribute of variable MTRRs.
622 This function shadows the content of variable MTRRs into an
623 internal array: VariableMtrr.
625 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
626 @param[in] MtrrValidAddressMask The valid address mask for MTRR
627 @param[out] VariableMtrr The array to shadow variable MTRRs content
629 @return The return value of this parameter indicates the
630 number of MTRRs which has been used.
635 MtrrGetMemoryAttributeInVariableMtrr (
636 IN UINT64 MtrrValidBitsMask
,
637 IN UINT64 MtrrValidAddressMask
,
638 OUT VARIABLE_MTRR
*VariableMtrr
641 MTRR_VARIABLE_SETTINGS VariableSettings
;
643 if (!IsMtrrSupported ()) {
647 MtrrGetVariableMtrrWorker (
649 GetVariableMtrrCountWorker (),
653 return MtrrGetMemoryAttributeInVariableMtrrWorker (
655 GetFirmwareVariableMtrrCountWorker (),
657 MtrrValidAddressMask
,
663 Return the least alignment of address.
665 @param Address The address to return the alignment.
666 @param Alignment0 The alignment to return when Address is 0.
668 @return The least alignment of the Address.
671 MtrrLibLeastAlignment (
680 return LShiftU64 (1, (UINTN
) LowBitSet64 (Address
));
684 Return the number of required variable MTRRs to positively cover the
687 @param BaseAddress Base address of the range.
688 @param Length Length of the range.
689 @param Alignment0 Alignment of 0.
691 @return The number of the required variable MTRRs.
694 MtrrLibGetPositiveMtrrNumber (
695 IN UINT64 BaseAddress
,
702 BOOLEAN UseLeastAlignment
;
704 UseLeastAlignment
= TRUE
;
707 // Calculate the alignment of the base address.
709 for (MtrrNumber
= 0; Length
!= 0; MtrrNumber
++) {
710 if (UseLeastAlignment
) {
711 SubLength
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
713 if (SubLength
> Length
) {
715 // Set a flag when remaining length is too small
716 // so that MtrrLibLeastAlignment() is not called in following loops.
718 UseLeastAlignment
= FALSE
;
722 if (!UseLeastAlignment
) {
723 SubLength
= GetPowerOfTwo64 (Length
);
726 BaseAddress
+= SubLength
;
734 Return whether the left MTRR type precedes the right MTRR type.
736 The MTRR type precedence rules are:
737 1. UC precedes any other type
739 For further details, please refer the IA32 Software Developer's Manual,
740 Volume 3, Section "MTRR Precedences".
742 @param Left The left MTRR type.
743 @param Right The right MTRR type.
745 @retval TRUE Left precedes Right.
746 @retval FALSE Left doesn't precede Right.
749 MtrrLibTypeLeftPrecedeRight (
750 IN MTRR_MEMORY_CACHE_TYPE Left
,
751 IN MTRR_MEMORY_CACHE_TYPE Right
754 return (BOOLEAN
) (Left
== CacheUncacheable
|| (Left
== CacheWriteThrough
&& Right
== CacheWriteBack
));
759 Return whether the type of the specified range can precede the specified type.
761 @param Ranges Memory range array holding memory type settings for all
763 @param RangeCount Count of memory ranges.
764 @param Type Type to check precedence.
765 @param SubBase Base address of the specified range.
766 @param SubLength Length of the specified range.
768 @retval TRUE The type of the specified range can precede the Type.
769 @retval FALSE The type of the specified range cannot precede the Type.
770 So the subtraction is not applicable.
773 MtrrLibSubstractable (
774 IN CONST MEMORY_RANGE
*Ranges
,
775 IN UINT32 RangeCount
,
776 IN MTRR_MEMORY_CACHE_TYPE Type
,
785 for (Index
= 0; Index
< RangeCount
; Index
++) {
786 if (Ranges
[Index
].BaseAddress
<= SubBase
&& SubBase
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
788 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
>= SubBase
+ SubLength
) {
789 return MtrrLibTypeLeftPrecedeRight (Ranges
[Index
].Type
, Type
);
792 if (!MtrrLibTypeLeftPrecedeRight (Ranges
[Index
].Type
, Type
)) {
796 Length
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- SubBase
;
808 Return the number of required variable MTRRs to cover the specified range.
810 The routine considers subtraction in the both side of the range to find out
811 the most optimal solution (which uses the least MTRRs).
813 @param Ranges Array holding memory type settings of all memory
815 @param RangeCount Count of memory ranges.
816 @param VariableMtrr Array holding allocated variable MTRRs.
817 @param VariableMtrrCount Count of allocated variable MTRRs.
818 @param BaseAddress Base address of the specified range.
819 @param Length Length of the specified range.
820 @param Type MTRR type of the specified range.
821 @param Alignment0 Alignment of 0.
822 @param SubLeft Return the count of left subtraction.
823 @param SubRight Return the count of right subtraction.
825 @return Number of required variable MTRRs.
828 MtrrLibGetMtrrNumber (
829 IN CONST MEMORY_RANGE
*Ranges
,
830 IN UINT32 RangeCount
,
831 IN CONST VARIABLE_MTRR
*VariableMtrr
,
832 IN UINT32 VariableMtrrCount
,
833 IN UINT64 BaseAddress
,
835 IN MTRR_MEMORY_CACHE_TYPE Type
,
836 IN UINT64 Alignment0
,
837 OUT UINT32
*SubLeft
, // subtractive from BaseAddress to get more aligned address, to save MTRR
838 OUT UINT32
*SubRight
// subtractive from BaseAddress + Length, to save MTRR
842 UINT32 LeastLeftMtrrNumber
;
843 UINT32 MiddleMtrrNumber
;
844 UINT32 LeastRightMtrrNumber
;
845 UINT32 CurrentMtrrNumber
;
846 UINT32 SubtractiveCount
;
847 UINT32 SubtractiveMtrrNumber
;
848 UINT32 LeastSubtractiveMtrrNumber
;
849 UINT64 SubtractiveBaseAddress
;
850 UINT64 SubtractiveLength
;
851 UINT64 BaseAlignment
;
856 LeastSubtractiveMtrrNumber
= 0;
859 // Get the optimal left subtraction solution.
861 if (BaseAddress
!= 0) {
863 // Get the MTRR number needed without left subtraction.
865 LeastLeftMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
868 // Left subtraction bit by bit, to find the optimal left subtraction solution.
870 for (SubtractiveMtrrNumber
= 0, SubtractiveCount
= 1; BaseAddress
!= 0; SubtractiveCount
++) {
871 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
874 // Check whether the memory type of [BaseAddress - Alignment, BaseAddress) can override Type.
875 // IA32 Manual defines the following override rules:
879 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
- Alignment
, Alignment
)) {
883 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
884 if ((VariableMtrr
[Index
].BaseAddress
== BaseAddress
- Alignment
) &&
885 (VariableMtrr
[Index
].Length
== Alignment
)) {
889 if (Index
== VariableMtrrCount
) {
891 // Increment SubtractiveMtrrNumber when [BaseAddress - Alignment, BaseAddress) is not be planed as a MTRR
893 SubtractiveMtrrNumber
++;
896 BaseAddress
-= Alignment
;
899 CurrentMtrrNumber
= SubtractiveMtrrNumber
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
900 if (CurrentMtrrNumber
<= LeastLeftMtrrNumber
) {
901 LeastLeftMtrrNumber
= CurrentMtrrNumber
;
902 LeastSubtractiveMtrrNumber
= SubtractiveMtrrNumber
;
903 *SubLeft
= SubtractiveCount
;
904 SubtractiveBaseAddress
= BaseAddress
;
905 SubtractiveLength
= Length
;
910 // If left subtraction is better, subtract BaseAddress to left, and enlarge Length
913 BaseAddress
= SubtractiveBaseAddress
;
914 Length
= SubtractiveLength
;
919 // Increment BaseAddress greedily until (BaseAddress + Alignment) exceeds (BaseAddress + Length)
921 MiddleMtrrNumber
= 0;
922 while (Length
!= 0) {
923 BaseAlignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
924 if (BaseAlignment
> Length
) {
927 BaseAddress
+= BaseAlignment
;
928 Length
-= BaseAlignment
;
934 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
;
939 // Get the optimal right subtraction solution.
943 // Get the MTRR number needed without right subtraction.
945 LeastRightMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
947 for (SubtractiveCount
= 1; Length
< BaseAlignment
; SubtractiveCount
++) {
948 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
949 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
+ Length
, Alignment
)) {
956 // SubtractiveCount = Number of MTRRs used for subtraction
958 CurrentMtrrNumber
= SubtractiveCount
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
959 if (CurrentMtrrNumber
<= LeastRightMtrrNumber
) {
960 LeastRightMtrrNumber
= CurrentMtrrNumber
;
961 *SubRight
= SubtractiveCount
;
962 SubtractiveLength
= Length
;
966 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
+ LeastRightMtrrNumber
;
970 Initializes the valid bits mask and valid address mask for MTRRs.
972 This function initializes the valid bits mask and valid address mask for MTRRs.
974 @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR
975 @param[out] MtrrValidAddressMask The valid address mask for the MTRR
979 MtrrLibInitializeMtrrMask (
980 OUT UINT64
*MtrrValidBitsMask
,
981 OUT UINT64
*MtrrValidAddressMask
984 UINT32 MaxExtendedFunction
;
985 CPUID_VIR_PHY_ADDRESS_SIZE_EAX VirPhyAddressSize
;
988 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &MaxExtendedFunction
, NULL
, NULL
, NULL
);
990 if (MaxExtendedFunction
>= CPUID_VIR_PHY_ADDRESS_SIZE
) {
991 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, &VirPhyAddressSize
.Uint32
, NULL
, NULL
, NULL
);
993 VirPhyAddressSize
.Bits
.PhysicalAddressBits
= 36;
996 *MtrrValidBitsMask
= LShiftU64 (1, VirPhyAddressSize
.Bits
.PhysicalAddressBits
) - 1;
997 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
1002 Determines the real attribute of a memory range.
1004 This function is to arbitrate the real attribute of the memory when
1005 there are 2 MTRRs covers the same memory range. For further details,
1006 please refer the IA32 Software Developer's Manual, Volume 3,
1007 Section "MTRR Precedences".
1009 @param[in] MtrrType1 The first kind of Memory type
1010 @param[in] MtrrType2 The second kind of memory type
1013 MTRR_MEMORY_CACHE_TYPE
1015 IN MTRR_MEMORY_CACHE_TYPE MtrrType1
,
1016 IN MTRR_MEMORY_CACHE_TYPE MtrrType2
1019 if (MtrrType1
== MtrrType2
) {
1024 MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
) ||
1025 MtrrLibTypeLeftPrecedeRight (MtrrType2
, MtrrType1
)
1028 if (MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
)) {
1036 Worker function will get the memory cache type of the specific address.
1038 If MtrrSetting is not NULL, gets the memory cache type from input
1039 MTRR settings buffer.
1040 If MtrrSetting is NULL, gets the memory cache type from MTRRs.
1042 @param[in] MtrrSetting A buffer holding all MTRRs content.
1043 @param[in] Address The specific address
1045 @return Memory cache type of the specific address
1048 MTRR_MEMORY_CACHE_TYPE
1049 MtrrGetMemoryAttributeByAddressWorker (
1050 IN MTRR_SETTINGS
*MtrrSetting
,
1051 IN PHYSICAL_ADDRESS Address
1054 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
1058 MTRR_MEMORY_CACHE_TYPE MtrrType
;
1059 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1060 UINT64 MtrrValidBitsMask
;
1061 UINT64 MtrrValidAddressMask
;
1062 UINT32 VariableMtrrCount
;
1063 MTRR_VARIABLE_SETTINGS VariableSettings
;
1066 // Check if MTRR is enabled, if not, return UC as attribute
1068 if (MtrrSetting
== NULL
) {
1069 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
1071 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
1074 if (DefType
.Bits
.E
== 0) {
1075 return CacheUncacheable
;
1079 // If address is less than 1M, then try to go through the fixed MTRR
1081 if (Address
< BASE_1MB
) {
1082 if (DefType
.Bits
.FE
!= 0) {
1084 // Go through the fixed MTRR
1086 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1087 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1088 Address
< mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1089 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)) {
1091 ((UINTN
) Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1092 mMtrrLibFixedMtrrTable
[Index
].Length
;
1093 if (MtrrSetting
== NULL
) {
1094 FixedMtrr
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1096 FixedMtrr
= MtrrSetting
->Fixed
.Mtrr
[Index
];
1098 return (MTRR_MEMORY_CACHE_TYPE
) (RShiftU64 (FixedMtrr
, SubIndex
* 8) & 0xFF);
1104 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1105 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1106 MtrrGetVariableMtrrWorker (MtrrSetting
, VariableMtrrCount
, &VariableSettings
);
1108 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1109 MtrrGetMemoryAttributeInVariableMtrrWorker (
1113 MtrrValidAddressMask
,
1118 // Go through the variable MTRR
1120 MtrrType
= CacheInvalid
;
1121 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1122 if (VariableMtrr
[Index
].Valid
) {
1123 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1124 Address
< VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
) {
1125 if (MtrrType
== CacheInvalid
) {
1126 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
;
1128 MtrrType
= MtrrLibPrecedence (MtrrType
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
);
1135 // If there is no MTRR which covers the Address, use the default MTRR type.
1137 if (MtrrType
== CacheInvalid
) {
1138 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
1146 This function will get the memory cache type of the specific address.
1148 This function is mainly for debug purpose.
1150 @param[in] Address The specific address
1152 @return Memory cache type of the specific address
1155 MTRR_MEMORY_CACHE_TYPE
1157 MtrrGetMemoryAttribute (
1158 IN PHYSICAL_ADDRESS Address
1161 if (!IsMtrrSupported ()) {
1162 return CacheUncacheable
;
1165 return MtrrGetMemoryAttributeByAddressWorker (NULL
, Address
);
1169 Worker function prints all MTRRs for debugging.
1171 If MtrrSetting is not NULL, print MTRR settings from input MTRR
1173 If MtrrSetting is NULL, print MTRR settings from MTRRs.
1175 @param MtrrSetting A buffer holding all MTRRs content.
1178 MtrrDebugPrintAllMtrrsWorker (
1179 IN MTRR_SETTINGS
*MtrrSetting
1183 MTRR_SETTINGS LocalMtrrs
;
1184 MTRR_SETTINGS
*Mtrrs
;
1187 UINTN VariableMtrrCount
;
1195 UINT64 NoRangeLimit
;
1198 UINTN PreviousMemoryType
;
1201 if (!IsMtrrSupported ()) {
1205 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1206 DEBUG((DEBUG_CACHE
, "=============\n"));
1208 if (MtrrSetting
!= NULL
) {
1209 Mtrrs
= MtrrSetting
;
1211 MtrrGetAllMtrrs (&LocalMtrrs
);
1212 Mtrrs
= &LocalMtrrs
;
1215 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", Mtrrs
->MtrrDefType
));
1216 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1217 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, Mtrrs
->Fixed
.Mtrr
[Index
]));
1220 VariableMtrrCount
= GetVariableMtrrCount ();
1221 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1222 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1224 Mtrrs
->Variables
.Mtrr
[Index
].Base
,
1225 Mtrrs
->Variables
.Mtrr
[Index
].Mask
1228 DEBUG((DEBUG_CACHE
, "\n"));
1229 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1230 DEBUG((DEBUG_CACHE
, "====================================\n"));
1233 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1234 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1235 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1236 for (Index1
= 0; Index1
< 8; Index1
++) {
1237 MemoryType
= (UINTN
)(RShiftU64 (Mtrrs
->Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff);
1238 if (MemoryType
> CacheWriteBack
) {
1239 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1241 if (MemoryType
!= PreviousMemoryType
) {
1242 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1243 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1245 PreviousMemoryType
= MemoryType
;
1246 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1248 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1251 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1253 VariableMtrrCount
= GetVariableMtrrCount ();
1256 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1257 if (RegEax
>= 0x80000008) {
1258 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1259 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1262 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1264 MemoryType
= MtrrGetMemoryAttributeByAddressWorker (Mtrrs
, Base
);
1265 if (MemoryType
> CacheWriteBack
) {
1266 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1269 if (MemoryType
!= PreviousMemoryType
) {
1270 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1271 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1273 PreviousMemoryType
= MemoryType
;
1274 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1277 RangeBase
= BASE_1MB
;
1278 NoRangeBase
= BASE_1MB
;
1280 NoRangeLimit
= Limit
;
1282 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1283 if ((Mtrrs
->Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1285 // If mask is not valid, then do not display range
1289 MtrrBase
= (Mtrrs
->Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1290 MtrrLimit
= MtrrBase
+ ((~(Mtrrs
->Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1292 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1296 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1297 RangeBase
= MtrrBase
;
1299 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1300 RangeBase
= MtrrLimit
+ 1;
1302 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1303 RangeLimit
= MtrrBase
- 1;
1305 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1306 RangeLimit
= MtrrLimit
;
1309 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1310 NoRangeBase
= MtrrLimit
+ 1;
1312 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1313 NoRangeLimit
= MtrrBase
- 1;
1318 Base
= RangeLimit
+ 1;
1320 Base
= NoRangeLimit
+ 1;
1322 } while (Base
< Limit
);
1323 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1329 This function prints all MTRRs for debugging.
1333 MtrrDebugPrintAllMtrrs (
1337 MtrrDebugPrintAllMtrrsWorker (NULL
);
1341 Update the Ranges array to change the specified range identified by
1342 BaseAddress and Length to Type.
1344 @param Ranges Array holding memory type settings for all memory regions.
1345 @param Capacity The maximum count of memory ranges the array can hold.
1346 @param Count Return the new memory range count in the array.
1347 @param BaseAddress The base address of the memory range to change type.
1348 @param Length The length of the memory range to change type.
1349 @param Type The new type of the specified memory range.
1351 @retval RETURN_SUCCESS The type of the specified memory range is
1352 changed successfully.
1353 @retval RETURN_OUT_OF_RESOURCES The new type set causes the count of memory
1354 range exceeds capacity.
1357 MtrrLibSetMemoryType (
1358 IN MEMORY_RANGE
*Ranges
,
1360 IN OUT UINT32
*Count
,
1361 IN UINT64 BaseAddress
,
1363 IN MTRR_MEMORY_CACHE_TYPE Type
1374 Limit
= BaseAddress
+ Length
;
1375 StartIndex
= *Count
;
1377 for (Index
= 0; Index
< *Count
; Index
++) {
1378 if ((StartIndex
== *Count
) &&
1379 (Ranges
[Index
].BaseAddress
<= BaseAddress
) &&
1380 (BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1382 LengthLeft
= BaseAddress
- Ranges
[Index
].BaseAddress
;
1385 if ((EndIndex
== *Count
) &&
1386 (Ranges
[Index
].BaseAddress
< Limit
) &&
1387 (Limit
<= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1389 LengthRight
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- Limit
;
1394 ASSERT (StartIndex
!= *Count
&& EndIndex
!= *Count
);
1395 if (StartIndex
== EndIndex
&& Ranges
[StartIndex
].Type
== Type
) {
1396 return RETURN_SUCCESS
;
1400 // The type change may cause merging with previous range or next range.
1401 // Update the StartIndex, EndIndex, BaseAddress, Length so that following
1402 // logic doesn't need to consider merging.
1404 if (StartIndex
!= 0) {
1405 if (LengthLeft
== 0 && Ranges
[StartIndex
- 1].Type
== Type
) {
1407 Length
+= Ranges
[StartIndex
].Length
;
1408 BaseAddress
-= Ranges
[StartIndex
].Length
;
1411 if (EndIndex
!= (*Count
) - 1) {
1412 if (LengthRight
== 0 && Ranges
[EndIndex
+ 1].Type
== Type
) {
1414 Length
+= Ranges
[EndIndex
].Length
;
1419 // |- 0 -|- 1 -|- 2 -|- 3 -| StartIndex EndIndex DeltaCount Count (Count = 4)
1420 // |++++++++++++++++++| 0 3 1=3-0-2 3
1421 // |+++++++| 0 1 -1=1-0-2 5
1422 // |+| 0 0 -2=0-0-2 6
1423 // |+++| 0 0 -1=0-0-2+1 5
1426 DeltaCount
= EndIndex
- StartIndex
- 2;
1427 if (LengthLeft
== 0) {
1430 if (LengthRight
== 0) {
1433 if (*Count
- DeltaCount
> Capacity
) {
1434 return RETURN_OUT_OF_RESOURCES
;
1438 // Reserve (-DeltaCount) space
1440 CopyMem (&Ranges
[EndIndex
+ 1 - DeltaCount
], &Ranges
[EndIndex
+ 1], (*Count
- EndIndex
- 1) * sizeof (Ranges
[0]));
1441 *Count
-= DeltaCount
;
1443 if (LengthLeft
!= 0) {
1444 Ranges
[StartIndex
].Length
= LengthLeft
;
1447 if (LengthRight
!= 0) {
1448 Ranges
[EndIndex
- DeltaCount
].BaseAddress
= BaseAddress
+ Length
;
1449 Ranges
[EndIndex
- DeltaCount
].Length
= LengthRight
;
1450 Ranges
[EndIndex
- DeltaCount
].Type
= Ranges
[EndIndex
].Type
;
1452 Ranges
[StartIndex
].BaseAddress
= BaseAddress
;
1453 Ranges
[StartIndex
].Length
= Length
;
1454 Ranges
[StartIndex
].Type
= Type
;
1455 return RETURN_SUCCESS
;
1459 Allocate one or more variable MTRR to cover the range identified by
1460 BaseAddress and Length.
1462 @param Ranges Memory range array holding the memory type
1463 settings for all memory address.
1464 @param RangeCount Count of memory ranges.
1465 @param VariableMtrr Variable MTRR array.
1466 @param VariableMtrrCapacity Capacity of variable MTRR array.
1467 @param VariableMtrrCount Count of variable MTRR.
1468 @param BaseAddress Base address of the memory range.
1469 @param Length Length of the memory range.
1470 @param Type MTRR type of the memory range.
1471 @param Alignment0 Alignment of 0.
1473 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1474 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1477 MtrrLibSetMemoryAttributeInVariableMtrr (
1478 IN CONST MEMORY_RANGE
*Ranges
,
1479 IN UINT32 RangeCount
,
1480 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1481 IN UINT32 VariableMtrrCapacity
,
1482 IN OUT UINT32
*VariableMtrrCount
,
1483 IN UINT64 BaseAddress
,
1485 IN MTRR_MEMORY_CACHE_TYPE Type
,
1486 IN UINT64 Alignment0
1490 Allocate one or more variable MTRR to cover the range identified by
1491 BaseAddress and Length.
1493 The routine recursively calls MtrrLibSetMemoryAttributeInVariableMtrr()
1494 to allocate variable MTRRs when the range contains several sub-ranges
1495 with different attributes.
1497 @param Ranges Memory range array holding the memory type
1498 settings for all memory address.
1499 @param RangeCount Count of memory ranges.
1500 @param VariableMtrr Variable MTRR array.
1501 @param VariableMtrrCapacity Capacity of variable MTRR array.
1502 @param VariableMtrrCount Count of variable MTRR.
1503 @param BaseAddress Base address of the memory range.
1504 @param Length Length of the memory range.
1505 @param Type MTRR type of the range.
1506 If it's CacheInvalid, the memory range may
1507 contains several sub-ranges with different
1509 @param Alignment0 Alignment of 0.
1511 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1512 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1515 MtrrLibAddVariableMtrr (
1516 IN CONST MEMORY_RANGE
*Ranges
,
1517 IN UINT32 RangeCount
,
1518 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1519 IN UINT32 VariableMtrrCapacity
,
1520 IN OUT UINT32
*VariableMtrrCount
,
1521 IN PHYSICAL_ADDRESS BaseAddress
,
1523 IN MTRR_MEMORY_CACHE_TYPE Type
,
1524 IN UINT64 Alignment0
1527 RETURN_STATUS Status
;
1531 MTRR_LIB_ASSERT_ALIGNED (BaseAddress
, Length
);
1532 if (Type
== CacheInvalid
) {
1533 for (Index
= 0; Index
< RangeCount
; Index
++) {
1534 if (Ranges
[Index
].BaseAddress
<= BaseAddress
&& BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1537 // Because the Length may not be aligned to BaseAddress, below code calls
1538 // MtrrLibSetMemoryAttributeInVariableMtrr() instead of itself.
1539 // MtrrLibSetMemoryAttributeInVariableMtrr() splits the range to several
1542 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
>= BaseAddress
+ Length
) {
1543 return MtrrLibSetMemoryAttributeInVariableMtrr (
1544 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1545 BaseAddress
, Length
, Ranges
[Index
].Type
, Alignment0
1548 SubLength
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- BaseAddress
;
1549 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1550 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1551 BaseAddress
, SubLength
, Ranges
[Index
].Type
, Alignment0
1553 if (RETURN_ERROR (Status
)) {
1556 BaseAddress
+= SubLength
;
1557 Length
-= SubLength
;
1563 // Because memory ranges cover all the memory addresses, it's impossible to be here.
1566 return RETURN_DEVICE_ERROR
;
1568 for (Index
= 0; Index
< *VariableMtrrCount
; Index
++) {
1569 if (VariableMtrr
[Index
].BaseAddress
== BaseAddress
&& VariableMtrr
[Index
].Length
== Length
) {
1570 ASSERT (VariableMtrr
[Index
].Type
== Type
);
1574 if (Index
== *VariableMtrrCount
) {
1575 if (*VariableMtrrCount
== VariableMtrrCapacity
) {
1576 return RETURN_OUT_OF_RESOURCES
;
1578 VariableMtrr
[Index
].BaseAddress
= BaseAddress
;
1579 VariableMtrr
[Index
].Length
= Length
;
1580 VariableMtrr
[Index
].Type
= Type
;
1581 VariableMtrr
[Index
].Valid
= TRUE
;
1582 VariableMtrr
[Index
].Used
= TRUE
;
1583 (*VariableMtrrCount
)++;
1585 return RETURN_SUCCESS
;
1590 Allocate one or more variable MTRR to cover the range identified by
1591 BaseAddress and Length.
1593 @param Ranges Memory range array holding the memory type
1594 settings for all memory address.
1595 @param RangeCount Count of memory ranges.
1596 @param VariableMtrr Variable MTRR array.
1597 @param VariableMtrrCapacity Capacity of variable MTRR array.
1598 @param VariableMtrrCount Count of variable MTRR.
1599 @param BaseAddress Base address of the memory range.
1600 @param Length Length of the memory range.
1601 @param Type MTRR type of the memory range.
1602 @param Alignment0 Alignment of 0.
1604 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1605 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1608 MtrrLibSetMemoryAttributeInVariableMtrr (
1609 IN CONST MEMORY_RANGE
*Ranges
,
1610 IN UINT32 RangeCount
,
1611 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1612 IN UINT32 VariableMtrrCapacity
,
1613 IN OUT UINT32
*VariableMtrrCount
,
1614 IN UINT64 BaseAddress
,
1616 IN MTRR_MEMORY_CACHE_TYPE Type
,
1617 IN UINT64 Alignment0
1622 UINT32 SubtractiveLeft
;
1623 UINT32 SubtractiveRight
;
1624 BOOLEAN UseLeastAlignment
;
1626 MtrrNumber
= MtrrLibGetMtrrNumber (Ranges
, RangeCount
, VariableMtrr
, *VariableMtrrCount
,
1627 BaseAddress
, Length
, Type
, Alignment0
, &SubtractiveLeft
, &SubtractiveRight
);
1629 if (MtrrNumber
+ *VariableMtrrCount
> VariableMtrrCapacity
) {
1630 return RETURN_OUT_OF_RESOURCES
;
1633 while (SubtractiveLeft
-- != 0) {
1634 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1635 ASSERT (Alignment
<= Length
);
1637 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1638 BaseAddress
- Alignment
, Alignment
, CacheInvalid
, Alignment0
);
1639 BaseAddress
-= Alignment
;
1640 Length
+= Alignment
;
1643 while (Length
!= 0) {
1644 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1645 if (Alignment
> Length
) {
1648 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1649 BaseAddress
, Alignment
, Type
, Alignment0
);
1650 BaseAddress
+= Alignment
;
1651 Length
-= Alignment
;
1654 while (SubtractiveRight
-- != 0) {
1655 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
1656 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1657 BaseAddress
+ Length
, Alignment
, CacheInvalid
, Alignment0
);
1658 Length
+= Alignment
;
1661 UseLeastAlignment
= TRUE
;
1662 while (Length
!= 0) {
1663 if (UseLeastAlignment
) {
1664 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1665 if (Alignment
> Length
) {
1666 UseLeastAlignment
= FALSE
;
1670 if (!UseLeastAlignment
) {
1671 Alignment
= GetPowerOfTwo64 (Length
);
1674 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1675 BaseAddress
, Alignment
, Type
, Alignment0
);
1676 BaseAddress
+= Alignment
;
1677 Length
-= Alignment
;
1679 return RETURN_SUCCESS
;
1683 Return an array of memory ranges holding memory type settings for all memory
1686 @param DefaultType The default memory type.
1687 @param TotalLength The total length of the memory.
1688 @param VariableMtrr The variable MTRR array.
1689 @param VariableMtrrCount The count of variable MTRRs.
1690 @param Ranges Return the memory range array holding memory type
1691 settings for all memory address.
1692 @param RangeCapacity The capacity of memory range array.
1693 @param RangeCount Return the count of memory range.
1695 @retval RETURN_SUCCESS The memory range array is returned successfully.
1696 @retval RETURN_OUT_OF_RESOURCES The count of memory ranges exceeds capacity.
1699 MtrrLibGetMemoryTypes (
1700 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1701 IN UINT64 TotalLength
,
1702 IN CONST VARIABLE_MTRR
*VariableMtrr
,
1703 IN UINT32 VariableMtrrCount
,
1704 OUT MEMORY_RANGE
*Ranges
,
1705 IN UINT32 RangeCapacity
,
1706 OUT UINT32
*RangeCount
1709 RETURN_STATUS Status
;
1715 // UC > * (except WB, UC) > WB
1719 // 0. Set whole range as DefaultType
1722 Ranges
[0].BaseAddress
= 0;
1723 Ranges
[0].Length
= TotalLength
;
1724 Ranges
[0].Type
= DefaultType
;
1729 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1730 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheWriteBack
) {
1731 Status
= MtrrLibSetMemoryType (
1732 Ranges
, RangeCapacity
, RangeCount
,
1733 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1735 if (RETURN_ERROR (Status
)) {
1742 // 2. Set other types than WB or UC
1744 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1745 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
!= CacheWriteBack
&& VariableMtrr
[Index
].Type
!= CacheUncacheable
) {
1746 Status
= MtrrLibSetMemoryType (
1747 Ranges
, RangeCapacity
, RangeCount
,
1748 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1750 if (RETURN_ERROR (Status
)) {
1759 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1760 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheUncacheable
) {
1761 Status
= MtrrLibSetMemoryType (
1762 Ranges
, RangeCapacity
, RangeCount
,
1763 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1765 if (RETURN_ERROR (Status
)) {
1770 return RETURN_SUCCESS
;
1774 Worker function attempts to set the attributes for a memory range.
1776 If MtrrSetting is not NULL, set the attributes into the input MTRR
1778 If MtrrSetting is NULL, set the attributes into MTRRs registers.
1780 @param[in, out] MtrrSetting A buffer holding all MTRRs content.
1781 @param[in] BaseAddress The physical address that is the start
1782 address of a memory range.
1783 @param[in] Length The size in bytes of the memory range.
1784 @param[in] Type The MTRR type to set for the memory range.
1786 @retval RETURN_SUCCESS The attributes were set for the memory
1788 @retval RETURN_INVALID_PARAMETER Length is zero.
1789 @retval RETURN_UNSUPPORTED The processor does not support one or
1790 more bytes of the memory resource range
1791 specified by BaseAddress and Length.
1792 @retval RETURN_UNSUPPORTED The MTRR type is not support for the
1793 memory resource range specified
1794 by BaseAddress and Length.
1795 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
1796 modify the attributes of the memory
1801 MtrrSetMemoryAttributeWorker (
1802 IN OUT MTRR_SETTINGS
*MtrrSetting
,
1803 IN PHYSICAL_ADDRESS BaseAddress
,
1805 IN MTRR_MEMORY_CACHE_TYPE Type
1808 RETURN_STATUS Status
;
1810 UINT32 WorkingIndex
;
1812 // N variable MTRRs can maximumly separate (2N + 1) Ranges, plus 1 range for [0, 1M).
1814 MEMORY_RANGE Ranges
[MTRR_NUMBER_OF_VARIABLE_MTRR
* 2 + 2];
1816 UINT64 MtrrValidBitsMask
;
1817 UINT64 MtrrValidAddressMask
;
1819 MTRR_CONTEXT MtrrContext
;
1820 BOOLEAN MtrrContextValid
;
1822 MTRR_MEMORY_CACHE_TYPE DefaultType
;
1828 BOOLEAN FixedSettingsValid
[MTRR_NUMBER_OF_FIXED_MTRR
];
1829 BOOLEAN FixedSettingsModified
[MTRR_NUMBER_OF_FIXED_MTRR
];
1830 MTRR_FIXED_SETTINGS WorkingFixedSettings
;
1832 UINT32 FirmwareVariableMtrrCount
;
1833 MTRR_VARIABLE_SETTINGS
*VariableSettings
;
1834 MTRR_VARIABLE_SETTINGS OriginalVariableSettings
;
1835 UINT32 OriginalVariableMtrrCount
;
1836 VARIABLE_MTRR OriginalVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1837 UINT32 WorkingVariableMtrrCount
;
1838 VARIABLE_MTRR WorkingVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1839 BOOLEAN VariableSettingModified
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1840 UINTN FreeVariableMtrrCount
;
1843 return RETURN_INVALID_PARAMETER
;
1846 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1847 if (((BaseAddress
& ~MtrrValidAddressMask
) != 0) || (Length
& ~MtrrValidAddressMask
) != 0) {
1848 return RETURN_UNSUPPORTED
;
1851 ZeroMem (&WorkingFixedSettings
, sizeof (WorkingFixedSettings
));
1852 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1853 FixedSettingsValid
[Index
] = FALSE
;
1854 FixedSettingsModified
[Index
] = FALSE
;
1858 // Check if Fixed MTRR
1860 if (BaseAddress
< BASE_1MB
) {
1861 MsrIndex
= (UINT32
)-1;
1862 while ((BaseAddress
< BASE_1MB
) && (Length
!= 0)) {
1863 Status
= MtrrLibProgramFixedMtrr (Type
, &BaseAddress
, &Length
, &MsrIndex
, &ClearMask
, &OrMask
);
1864 if (RETURN_ERROR (Status
)) {
1867 if (MtrrSetting
!= NULL
) {
1868 MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] = (MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1869 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.FE
= 1;
1871 if (!FixedSettingsValid
[MsrIndex
]) {
1872 WorkingFixedSettings
.Mtrr
[MsrIndex
] = AsmReadMsr64 (mMtrrLibFixedMtrrTable
[MsrIndex
].Msr
);
1873 FixedSettingsValid
[MsrIndex
] = TRUE
;
1875 NewValue
= (WorkingFixedSettings
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1876 if (WorkingFixedSettings
.Mtrr
[MsrIndex
] != NewValue
) {
1877 WorkingFixedSettings
.Mtrr
[MsrIndex
] = NewValue
;
1878 FixedSettingsModified
[MsrIndex
] = TRUE
;
1885 // A Length of 0 can only make sense for fixed MTTR ranges.
1886 // Since we just handled the fixed MTRRs, we can skip the
1887 // variable MTRR section.
1894 // Read the default MTRR type
1896 DefaultType
= MtrrGetDefaultMemoryTypeWorker (MtrrSetting
);
1899 // Read all variable MTRRs and convert to Ranges.
1901 OriginalVariableMtrrCount
= GetVariableMtrrCountWorker ();
1902 if (MtrrSetting
== NULL
) {
1903 ZeroMem (&OriginalVariableSettings
, sizeof (OriginalVariableSettings
));
1904 MtrrGetVariableMtrrWorker (NULL
, OriginalVariableMtrrCount
, &OriginalVariableSettings
);
1905 VariableSettings
= &OriginalVariableSettings
;
1907 VariableSettings
= &MtrrSetting
->Variables
;
1909 MtrrGetMemoryAttributeInVariableMtrrWorker (VariableSettings
, OriginalVariableMtrrCount
, MtrrValidBitsMask
, MtrrValidAddressMask
, OriginalVariableMtrr
);
1911 Status
= MtrrLibGetMemoryTypes (
1912 DefaultType
, MtrrValidBitsMask
+ 1, OriginalVariableMtrr
, OriginalVariableMtrrCount
,
1913 Ranges
, 2 * OriginalVariableMtrrCount
+ 1, &RangeCount
1915 ASSERT (Status
== RETURN_SUCCESS
);
1917 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCountWorker ();
1918 ASSERT (RangeCount
<= 2 * FirmwareVariableMtrrCount
+ 1);
1921 // Force [0, 1M) to UC, so that it doesn't impact left subtraction algorithm.
1923 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, 0, SIZE_1MB
, CacheUncacheable
);
1924 ASSERT (Status
== RETURN_SUCCESS
);
1926 // Apply Type to [BaseAddress, BaseAddress + Length)
1928 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, BaseAddress
, Length
, Type
);
1929 if (RETURN_ERROR (Status
)) {
1933 Alignment0
= LShiftU64 (1, (UINTN
) HighBitSet64 (MtrrValidBitsMask
));
1934 WorkingVariableMtrrCount
= 0;
1935 ZeroMem (&WorkingVariableMtrr
, sizeof (WorkingVariableMtrr
));
1936 for (Index
= 0; Index
< RangeCount
; Index
++) {
1937 if (Ranges
[Index
].Type
!= DefaultType
) {
1939 // Maximum allowed MTRR count is (FirmwareVariableMtrrCount + 1)
1940 // Because potentially the range [0, 1MB) is not merged, but can be ignored because fixed MTRR covers that
1942 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1944 WorkingVariableMtrr
, FirmwareVariableMtrrCount
+ 1, &WorkingVariableMtrrCount
,
1945 Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
,
1946 Ranges
[Index
].Type
, Alignment0
1948 if (RETURN_ERROR (Status
)) {
1955 // Remove the [0, 1MB) MTRR if it still exists (not merged with other range)
1957 if (WorkingVariableMtrr
[0].BaseAddress
== 0 && WorkingVariableMtrr
[0].Length
== SIZE_1MB
) {
1958 ASSERT (WorkingVariableMtrr
[0].Type
== CacheUncacheable
);
1959 WorkingVariableMtrrCount
--;
1960 CopyMem (&WorkingVariableMtrr
[0], &WorkingVariableMtrr
[1], WorkingVariableMtrrCount
* sizeof (VARIABLE_MTRR
));
1963 if (WorkingVariableMtrrCount
> FirmwareVariableMtrrCount
) {
1964 return RETURN_OUT_OF_RESOURCES
;
1967 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
1968 VariableSettingModified
[Index
] = FALSE
;
1970 if (!OriginalVariableMtrr
[Index
].Valid
) {
1973 for (WorkingIndex
= 0; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
1974 if (OriginalVariableMtrr
[Index
].BaseAddress
== WorkingVariableMtrr
[WorkingIndex
].BaseAddress
&&
1975 OriginalVariableMtrr
[Index
].Length
== WorkingVariableMtrr
[WorkingIndex
].Length
&&
1976 OriginalVariableMtrr
[Index
].Type
== WorkingVariableMtrr
[WorkingIndex
].Type
) {
1981 if (WorkingIndex
== WorkingVariableMtrrCount
) {
1983 // Remove the one from OriginalVariableMtrr which is not in WorkingVariableMtrr
1985 OriginalVariableMtrr
[Index
].Valid
= FALSE
;
1986 VariableSettingModified
[Index
] = TRUE
;
1989 // Remove the one from WorkingVariableMtrr which is also in OriginalVariableMtrr
1991 WorkingVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
1994 // The above two operations cause that valid MTRR only exists in either OriginalVariableMtrr or WorkingVariableMtrr.
1999 // Merge remaining MTRRs from WorkingVariableMtrr to OriginalVariableMtrr
2001 for (FreeVariableMtrrCount
= 0, WorkingIndex
= 0, Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2002 if (!OriginalVariableMtrr
[Index
].Valid
) {
2003 for (; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
2004 if (WorkingVariableMtrr
[WorkingIndex
].Valid
) {
2008 if (WorkingIndex
== WorkingVariableMtrrCount
) {
2009 FreeVariableMtrrCount
++;
2011 CopyMem (&OriginalVariableMtrr
[Index
], &WorkingVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2012 VariableSettingModified
[Index
] = TRUE
;
2017 ASSERT (OriginalVariableMtrrCount
- FreeVariableMtrrCount
<= FirmwareVariableMtrrCount
);
2020 // Move MTRRs after the FirmwraeVariableMtrrCount position to beginning
2022 WorkingIndex
= FirmwareVariableMtrrCount
;
2023 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
2024 if (!OriginalVariableMtrr
[Index
].Valid
) {
2026 // Found an empty MTRR in WorkingIndex position
2028 for (; WorkingIndex
< OriginalVariableMtrrCount
; WorkingIndex
++) {
2029 if (OriginalVariableMtrr
[WorkingIndex
].Valid
) {
2034 if (WorkingIndex
!= OriginalVariableMtrrCount
) {
2035 CopyMem (&OriginalVariableMtrr
[Index
], &OriginalVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2036 VariableSettingModified
[Index
] = TRUE
;
2037 VariableSettingModified
[WorkingIndex
] = TRUE
;
2038 OriginalVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
2044 // Convert OriginalVariableMtrr to VariableSettings
2045 // NOTE: MTRR from FirmwareVariableMtrr to OriginalVariableMtrr need to update as well.
2047 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2048 if (VariableSettingModified
[Index
]) {
2049 if (OriginalVariableMtrr
[Index
].Valid
) {
2050 VariableSettings
->Mtrr
[Index
].Base
= (OriginalVariableMtrr
[Index
].BaseAddress
& MtrrValidAddressMask
) | (UINT8
) OriginalVariableMtrr
[Index
].Type
;
2051 VariableSettings
->Mtrr
[Index
].Mask
= (~(OriginalVariableMtrr
[Index
].Length
- 1)) & MtrrValidAddressMask
| BIT11
;
2053 VariableSettings
->Mtrr
[Index
].Base
= 0;
2054 VariableSettings
->Mtrr
[Index
].Mask
= 0;
2060 if (MtrrSetting
!= NULL
) {
2061 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.E
= 1;
2062 return RETURN_SUCCESS
;
2065 MtrrContextValid
= FALSE
;
2067 // Write fixed MTRRs that have been modified
2069 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2070 if (FixedSettingsModified
[Index
]) {
2071 if (!MtrrContextValid
) {
2072 MtrrLibPreMtrrChange (&MtrrContext
);
2073 MtrrContextValid
= TRUE
;
2076 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2077 WorkingFixedSettings
.Mtrr
[Index
]
2083 // Write variable MTRRs
2085 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2086 if (VariableSettingModified
[Index
]) {
2087 if (!MtrrContextValid
) {
2088 MtrrLibPreMtrrChange (&MtrrContext
);
2089 MtrrContextValid
= TRUE
;
2092 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2093 VariableSettings
->Mtrr
[Index
].Base
2096 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2097 VariableSettings
->Mtrr
[Index
].Mask
2101 if (MtrrContextValid
) {
2102 MtrrLibPostMtrrChange (&MtrrContext
);
2109 This function attempts to set the attributes for a memory range.
2111 @param[in] BaseAddress The physical address that is the start
2112 address of a memory range.
2113 @param[in] Length The size in bytes of the memory range.
2114 @param[in] Attributes The bit mask of attributes to set for the
2117 @retval RETURN_SUCCESS The attributes were set for the memory
2119 @retval RETURN_INVALID_PARAMETER Length is zero.
2120 @retval RETURN_UNSUPPORTED The processor does not support one or
2121 more bytes of the memory resource range
2122 specified by BaseAddress and Length.
2123 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
2124 for the memory resource range specified
2125 by BaseAddress and Length.
2126 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
2127 range specified by BaseAddress and Length
2129 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
2130 modify the attributes of the memory
2136 MtrrSetMemoryAttribute (
2137 IN PHYSICAL_ADDRESS BaseAddress
,
2139 IN MTRR_MEMORY_CACHE_TYPE Attribute
2142 RETURN_STATUS Status
;
2144 if (!IsMtrrSupported ()) {
2145 return RETURN_UNSUPPORTED
;
2148 Status
= MtrrSetMemoryAttributeWorker (NULL
, BaseAddress
, Length
, Attribute
);
2149 DEBUG ((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a: [%016lx, %016lx) - %r\n",
2150 mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2152 if (!RETURN_ERROR (Status
)) {
2153 MtrrDebugPrintAllMtrrsWorker (NULL
);
2159 This function attempts to set the attributes into MTRR setting buffer for a memory range.
2161 @param[in, out] MtrrSetting MTRR setting buffer to be set.
2162 @param[in] BaseAddress The physical address that is the start address
2164 @param[in] Length The size in bytes of the memory range.
2165 @param[in] Attribute The bit mask of attributes to set for the
2168 @retval RETURN_SUCCESS The attributes were set for the memory range.
2169 @retval RETURN_INVALID_PARAMETER Length is zero.
2170 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
2171 memory resource range specified by BaseAddress and Length.
2172 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
2173 range specified by BaseAddress and Length.
2174 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
2175 BaseAddress and Length cannot be modified.
2176 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
2177 the memory resource range.
2182 MtrrSetMemoryAttributeInMtrrSettings (
2183 IN OUT MTRR_SETTINGS
*MtrrSetting
,
2184 IN PHYSICAL_ADDRESS BaseAddress
,
2186 IN MTRR_MEMORY_CACHE_TYPE Attribute
2189 RETURN_STATUS Status
;
2190 Status
= MtrrSetMemoryAttributeWorker (MtrrSetting
, BaseAddress
, Length
, Attribute
);
2191 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttributeMtrrSettings(%p) %a: [%016lx, %016lx) - %r\n",
2192 MtrrSetting
, mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2194 if (!RETURN_ERROR (Status
)) {
2195 MtrrDebugPrintAllMtrrsWorker (MtrrSetting
);
2202 Worker function setting variable MTRRs
2204 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2208 MtrrSetVariableMtrrWorker (
2209 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2213 UINT32 VariableMtrrCount
;
2215 VariableMtrrCount
= GetVariableMtrrCountWorker ();
2216 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
2218 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
2220 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2221 VariableSettings
->Mtrr
[Index
].Base
2224 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2225 VariableSettings
->Mtrr
[Index
].Mask
2232 This function sets variable MTRRs
2234 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2236 @return The pointer of VariableSettings
2239 MTRR_VARIABLE_SETTINGS
*
2241 MtrrSetVariableMtrr (
2242 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2245 MTRR_CONTEXT MtrrContext
;
2247 if (!IsMtrrSupported ()) {
2248 return VariableSettings
;
2251 MtrrLibPreMtrrChange (&MtrrContext
);
2252 MtrrSetVariableMtrrWorker (VariableSettings
);
2253 MtrrLibPostMtrrChange (&MtrrContext
);
2254 MtrrDebugPrintAllMtrrs ();
2256 return VariableSettings
;
2260 Worker function setting fixed MTRRs
2262 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2266 MtrrSetFixedMtrrWorker (
2267 IN MTRR_FIXED_SETTINGS
*FixedSettings
2272 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2274 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2275 FixedSettings
->Mtrr
[Index
]
2282 This function sets fixed MTRRs
2284 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2286 @retval The pointer of FixedSettings
2289 MTRR_FIXED_SETTINGS
*
2292 IN MTRR_FIXED_SETTINGS
*FixedSettings
2295 MTRR_CONTEXT MtrrContext
;
2297 if (!IsMtrrSupported ()) {
2298 return FixedSettings
;
2301 MtrrLibPreMtrrChange (&MtrrContext
);
2302 MtrrSetFixedMtrrWorker (FixedSettings
);
2303 MtrrLibPostMtrrChange (&MtrrContext
);
2304 MtrrDebugPrintAllMtrrs ();
2306 return FixedSettings
;
2311 This function gets the content in all MTRRs (variable and fixed)
2313 @param[out] MtrrSetting A buffer to hold all MTRRs content.
2315 @retval the pointer of MtrrSetting
2321 OUT MTRR_SETTINGS
*MtrrSetting
2324 if (!IsMtrrSupported ()) {
2331 MtrrGetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2334 // Get variable MTRRs
2336 MtrrGetVariableMtrrWorker (
2338 GetVariableMtrrCountWorker (),
2339 &MtrrSetting
->Variables
2343 // Get MTRR_DEF_TYPE value
2345 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
2352 This function sets all MTRRs (variable and fixed)
2354 @param[in] MtrrSetting A buffer holding all MTRRs content.
2356 @retval The pointer of MtrrSetting
2362 IN MTRR_SETTINGS
*MtrrSetting
2365 MTRR_CONTEXT MtrrContext
;
2367 if (!IsMtrrSupported ()) {
2371 MtrrLibPreMtrrChange (&MtrrContext
);
2376 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2379 // Set variable MTRRs
2381 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
2384 // Set MTRR_DEF_TYPE value
2386 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
2388 MtrrLibPostMtrrChangeEnableCache (&MtrrContext
);
2395 Checks if MTRR is supported.
2397 @retval TRUE MTRR is supported.
2398 @retval FALSE MTRR is not supported.
2407 CPUID_VERSION_INFO_EDX Edx
;
2408 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
2411 // Check CPUID(1).EDX[12] for MTRR capability
2413 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
2414 if (Edx
.Bits
.MTRR
== 0) {
2419 // Check number of variable MTRRs and fixed MTRRs existence.
2420 // If number of variable MTRRs is zero, or fixed MTRRs do not
2421 // exist, return false.
2423 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
2424 if ((MtrrCap
.Bits
.VCNT
== 0) || (MtrrCap
.Bits
.FIX
== 0)) {