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) {
862 SubtractiveBaseAddress
= 0;
863 SubtractiveLength
= 0;
865 // Get the MTRR number needed without left subtraction.
867 LeastLeftMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
870 // Left subtraction bit by bit, to find the optimal left subtraction solution.
872 for (SubtractiveMtrrNumber
= 0, SubtractiveCount
= 1; BaseAddress
!= 0; SubtractiveCount
++) {
873 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
876 // Check whether the memory type of [BaseAddress - Alignment, BaseAddress) can override Type.
877 // IA32 Manual defines the following override rules:
881 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
- Alignment
, Alignment
)) {
885 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
886 if ((VariableMtrr
[Index
].BaseAddress
== BaseAddress
- Alignment
) &&
887 (VariableMtrr
[Index
].Length
== Alignment
)) {
891 if (Index
== VariableMtrrCount
) {
893 // Increment SubtractiveMtrrNumber when [BaseAddress - Alignment, BaseAddress) is not be planed as a MTRR
895 SubtractiveMtrrNumber
++;
898 BaseAddress
-= Alignment
;
901 CurrentMtrrNumber
= SubtractiveMtrrNumber
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
902 if (CurrentMtrrNumber
<= LeastLeftMtrrNumber
) {
903 LeastLeftMtrrNumber
= CurrentMtrrNumber
;
904 LeastSubtractiveMtrrNumber
= SubtractiveMtrrNumber
;
905 *SubLeft
= SubtractiveCount
;
906 SubtractiveBaseAddress
= BaseAddress
;
907 SubtractiveLength
= Length
;
912 // If left subtraction is better, subtract BaseAddress to left, and enlarge Length
915 BaseAddress
= SubtractiveBaseAddress
;
916 Length
= SubtractiveLength
;
921 // Increment BaseAddress greedily until (BaseAddress + Alignment) exceeds (BaseAddress + Length)
923 MiddleMtrrNumber
= 0;
924 while (Length
!= 0) {
925 BaseAlignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
926 if (BaseAlignment
> Length
) {
929 BaseAddress
+= BaseAlignment
;
930 Length
-= BaseAlignment
;
936 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
;
941 // Get the optimal right subtraction solution.
945 // Get the MTRR number needed without right subtraction.
947 LeastRightMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
949 for (SubtractiveCount
= 1; Length
< BaseAlignment
; SubtractiveCount
++) {
950 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
951 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
+ Length
, Alignment
)) {
958 // SubtractiveCount = Number of MTRRs used for subtraction
960 CurrentMtrrNumber
= SubtractiveCount
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
961 if (CurrentMtrrNumber
<= LeastRightMtrrNumber
) {
962 LeastRightMtrrNumber
= CurrentMtrrNumber
;
963 *SubRight
= SubtractiveCount
;
964 SubtractiveLength
= Length
;
968 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
+ LeastRightMtrrNumber
;
972 Initializes the valid bits mask and valid address mask for MTRRs.
974 This function initializes the valid bits mask and valid address mask for MTRRs.
976 @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR
977 @param[out] MtrrValidAddressMask The valid address mask for the MTRR
981 MtrrLibInitializeMtrrMask (
982 OUT UINT64
*MtrrValidBitsMask
,
983 OUT UINT64
*MtrrValidAddressMask
986 UINT32 MaxExtendedFunction
;
987 CPUID_VIR_PHY_ADDRESS_SIZE_EAX VirPhyAddressSize
;
990 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &MaxExtendedFunction
, NULL
, NULL
, NULL
);
992 if (MaxExtendedFunction
>= CPUID_VIR_PHY_ADDRESS_SIZE
) {
993 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, &VirPhyAddressSize
.Uint32
, NULL
, NULL
, NULL
);
995 VirPhyAddressSize
.Bits
.PhysicalAddressBits
= 36;
998 *MtrrValidBitsMask
= LShiftU64 (1, VirPhyAddressSize
.Bits
.PhysicalAddressBits
) - 1;
999 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
1004 Determines the real attribute of a memory range.
1006 This function is to arbitrate the real attribute of the memory when
1007 there are 2 MTRRs covers the same memory range. For further details,
1008 please refer the IA32 Software Developer's Manual, Volume 3,
1009 Section "MTRR Precedences".
1011 @param[in] MtrrType1 The first kind of Memory type
1012 @param[in] MtrrType2 The second kind of memory type
1015 MTRR_MEMORY_CACHE_TYPE
1017 IN MTRR_MEMORY_CACHE_TYPE MtrrType1
,
1018 IN MTRR_MEMORY_CACHE_TYPE MtrrType2
1021 if (MtrrType1
== MtrrType2
) {
1026 MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
) ||
1027 MtrrLibTypeLeftPrecedeRight (MtrrType2
, MtrrType1
)
1030 if (MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
)) {
1038 Worker function will get the memory cache type of the specific address.
1040 If MtrrSetting is not NULL, gets the memory cache type from input
1041 MTRR settings buffer.
1042 If MtrrSetting is NULL, gets the memory cache type from MTRRs.
1044 @param[in] MtrrSetting A buffer holding all MTRRs content.
1045 @param[in] Address The specific address
1047 @return Memory cache type of the specific address
1050 MTRR_MEMORY_CACHE_TYPE
1051 MtrrGetMemoryAttributeByAddressWorker (
1052 IN MTRR_SETTINGS
*MtrrSetting
,
1053 IN PHYSICAL_ADDRESS Address
1056 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
1060 MTRR_MEMORY_CACHE_TYPE MtrrType
;
1061 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1062 UINT64 MtrrValidBitsMask
;
1063 UINT64 MtrrValidAddressMask
;
1064 UINT32 VariableMtrrCount
;
1065 MTRR_VARIABLE_SETTINGS VariableSettings
;
1068 // Check if MTRR is enabled, if not, return UC as attribute
1070 if (MtrrSetting
== NULL
) {
1071 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
1073 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
1076 if (DefType
.Bits
.E
== 0) {
1077 return CacheUncacheable
;
1081 // If address is less than 1M, then try to go through the fixed MTRR
1083 if (Address
< BASE_1MB
) {
1084 if (DefType
.Bits
.FE
!= 0) {
1086 // Go through the fixed MTRR
1088 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1089 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1090 Address
< mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1091 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)) {
1093 ((UINTN
) Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1094 mMtrrLibFixedMtrrTable
[Index
].Length
;
1095 if (MtrrSetting
== NULL
) {
1096 FixedMtrr
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1098 FixedMtrr
= MtrrSetting
->Fixed
.Mtrr
[Index
];
1100 return (MTRR_MEMORY_CACHE_TYPE
) (RShiftU64 (FixedMtrr
, SubIndex
* 8) & 0xFF);
1106 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1107 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1108 MtrrGetVariableMtrrWorker (MtrrSetting
, VariableMtrrCount
, &VariableSettings
);
1110 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1111 MtrrGetMemoryAttributeInVariableMtrrWorker (
1115 MtrrValidAddressMask
,
1120 // Go through the variable MTRR
1122 MtrrType
= CacheInvalid
;
1123 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1124 if (VariableMtrr
[Index
].Valid
) {
1125 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1126 Address
< VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
) {
1127 if (MtrrType
== CacheInvalid
) {
1128 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
;
1130 MtrrType
= MtrrLibPrecedence (MtrrType
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
);
1137 // If there is no MTRR which covers the Address, use the default MTRR type.
1139 if (MtrrType
== CacheInvalid
) {
1140 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
1148 This function will get the memory cache type of the specific address.
1150 This function is mainly for debug purpose.
1152 @param[in] Address The specific address
1154 @return Memory cache type of the specific address
1157 MTRR_MEMORY_CACHE_TYPE
1159 MtrrGetMemoryAttribute (
1160 IN PHYSICAL_ADDRESS Address
1163 if (!IsMtrrSupported ()) {
1164 return CacheUncacheable
;
1167 return MtrrGetMemoryAttributeByAddressWorker (NULL
, Address
);
1171 Worker function prints all MTRRs for debugging.
1173 If MtrrSetting is not NULL, print MTRR settings from input MTRR
1175 If MtrrSetting is NULL, print MTRR settings from MTRRs.
1177 @param MtrrSetting A buffer holding all MTRRs content.
1180 MtrrDebugPrintAllMtrrsWorker (
1181 IN MTRR_SETTINGS
*MtrrSetting
1185 MTRR_SETTINGS LocalMtrrs
;
1186 MTRR_SETTINGS
*Mtrrs
;
1189 UINTN VariableMtrrCount
;
1197 UINT64 NoRangeLimit
;
1200 UINTN PreviousMemoryType
;
1203 if (!IsMtrrSupported ()) {
1207 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1208 DEBUG((DEBUG_CACHE
, "=============\n"));
1210 if (MtrrSetting
!= NULL
) {
1211 Mtrrs
= MtrrSetting
;
1213 MtrrGetAllMtrrs (&LocalMtrrs
);
1214 Mtrrs
= &LocalMtrrs
;
1217 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", Mtrrs
->MtrrDefType
));
1218 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1219 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, Mtrrs
->Fixed
.Mtrr
[Index
]));
1222 VariableMtrrCount
= GetVariableMtrrCount ();
1223 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1224 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1226 Mtrrs
->Variables
.Mtrr
[Index
].Base
,
1227 Mtrrs
->Variables
.Mtrr
[Index
].Mask
1230 DEBUG((DEBUG_CACHE
, "\n"));
1231 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1232 DEBUG((DEBUG_CACHE
, "====================================\n"));
1235 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1236 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1237 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1238 for (Index1
= 0; Index1
< 8; Index1
++) {
1239 MemoryType
= (UINTN
)(RShiftU64 (Mtrrs
->Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff);
1240 if (MemoryType
> CacheWriteBack
) {
1241 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1243 if (MemoryType
!= PreviousMemoryType
) {
1244 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1245 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1247 PreviousMemoryType
= MemoryType
;
1248 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1250 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1253 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1255 VariableMtrrCount
= GetVariableMtrrCount ();
1258 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1259 if (RegEax
>= 0x80000008) {
1260 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1261 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1264 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1266 MemoryType
= MtrrGetMemoryAttributeByAddressWorker (Mtrrs
, Base
);
1267 if (MemoryType
> CacheWriteBack
) {
1268 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1271 if (MemoryType
!= PreviousMemoryType
) {
1272 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1273 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1275 PreviousMemoryType
= MemoryType
;
1276 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1279 RangeBase
= BASE_1MB
;
1280 NoRangeBase
= BASE_1MB
;
1282 NoRangeLimit
= Limit
;
1284 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1285 if ((Mtrrs
->Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1287 // If mask is not valid, then do not display range
1291 MtrrBase
= (Mtrrs
->Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1292 MtrrLimit
= MtrrBase
+ ((~(Mtrrs
->Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1294 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1298 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1299 RangeBase
= MtrrBase
;
1301 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1302 RangeBase
= MtrrLimit
+ 1;
1304 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1305 RangeLimit
= MtrrBase
- 1;
1307 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1308 RangeLimit
= MtrrLimit
;
1311 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1312 NoRangeBase
= MtrrLimit
+ 1;
1314 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1315 NoRangeLimit
= MtrrBase
- 1;
1320 Base
= RangeLimit
+ 1;
1322 Base
= NoRangeLimit
+ 1;
1324 } while (Base
< Limit
);
1325 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1331 This function prints all MTRRs for debugging.
1335 MtrrDebugPrintAllMtrrs (
1339 MtrrDebugPrintAllMtrrsWorker (NULL
);
1343 Update the Ranges array to change the specified range identified by
1344 BaseAddress and Length to Type.
1346 @param Ranges Array holding memory type settings for all memory regions.
1347 @param Capacity The maximum count of memory ranges the array can hold.
1348 @param Count Return the new memory range count in the array.
1349 @param BaseAddress The base address of the memory range to change type.
1350 @param Length The length of the memory range to change type.
1351 @param Type The new type of the specified memory range.
1353 @retval RETURN_SUCCESS The type of the specified memory range is
1354 changed successfully.
1355 @retval RETURN_OUT_OF_RESOURCES The new type set causes the count of memory
1356 range exceeds capacity.
1359 MtrrLibSetMemoryType (
1360 IN MEMORY_RANGE
*Ranges
,
1362 IN OUT UINT32
*Count
,
1363 IN UINT64 BaseAddress
,
1365 IN MTRR_MEMORY_CACHE_TYPE Type
1378 Limit
= BaseAddress
+ Length
;
1379 StartIndex
= *Count
;
1381 for (Index
= 0; Index
< *Count
; Index
++) {
1382 if ((StartIndex
== *Count
) &&
1383 (Ranges
[Index
].BaseAddress
<= BaseAddress
) &&
1384 (BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1386 LengthLeft
= BaseAddress
- Ranges
[Index
].BaseAddress
;
1389 if ((EndIndex
== *Count
) &&
1390 (Ranges
[Index
].BaseAddress
< Limit
) &&
1391 (Limit
<= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1393 LengthRight
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- Limit
;
1398 ASSERT (StartIndex
!= *Count
&& EndIndex
!= *Count
);
1399 if (StartIndex
== EndIndex
&& Ranges
[StartIndex
].Type
== Type
) {
1400 return RETURN_SUCCESS
;
1404 // The type change may cause merging with previous range or next range.
1405 // Update the StartIndex, EndIndex, BaseAddress, Length so that following
1406 // logic doesn't need to consider merging.
1408 if (StartIndex
!= 0) {
1409 if (LengthLeft
== 0 && Ranges
[StartIndex
- 1].Type
== Type
) {
1411 Length
+= Ranges
[StartIndex
].Length
;
1412 BaseAddress
-= Ranges
[StartIndex
].Length
;
1415 if (EndIndex
!= (*Count
) - 1) {
1416 if (LengthRight
== 0 && Ranges
[EndIndex
+ 1].Type
== Type
) {
1418 Length
+= Ranges
[EndIndex
].Length
;
1423 // |- 0 -|- 1 -|- 2 -|- 3 -| StartIndex EndIndex DeltaCount Count (Count = 4)
1424 // |++++++++++++++++++| 0 3 1=3-0-2 3
1425 // |+++++++| 0 1 -1=1-0-2 5
1426 // |+| 0 0 -2=0-0-2 6
1427 // |+++| 0 0 -1=0-0-2+1 5
1430 DeltaCount
= EndIndex
- StartIndex
- 2;
1431 if (LengthLeft
== 0) {
1434 if (LengthRight
== 0) {
1437 if (*Count
- DeltaCount
> Capacity
) {
1438 return RETURN_OUT_OF_RESOURCES
;
1442 // Reserve (-DeltaCount) space
1444 CopyMem (&Ranges
[EndIndex
+ 1 - DeltaCount
], &Ranges
[EndIndex
+ 1], (*Count
- EndIndex
- 1) * sizeof (Ranges
[0]));
1445 *Count
-= DeltaCount
;
1447 if (LengthLeft
!= 0) {
1448 Ranges
[StartIndex
].Length
= LengthLeft
;
1451 if (LengthRight
!= 0) {
1452 Ranges
[EndIndex
- DeltaCount
].BaseAddress
= BaseAddress
+ Length
;
1453 Ranges
[EndIndex
- DeltaCount
].Length
= LengthRight
;
1454 Ranges
[EndIndex
- DeltaCount
].Type
= Ranges
[EndIndex
].Type
;
1456 Ranges
[StartIndex
].BaseAddress
= BaseAddress
;
1457 Ranges
[StartIndex
].Length
= Length
;
1458 Ranges
[StartIndex
].Type
= Type
;
1459 return RETURN_SUCCESS
;
1463 Allocate one or more variable MTRR to cover the range identified by
1464 BaseAddress and Length.
1466 @param Ranges Memory range array holding the memory type
1467 settings for all memory address.
1468 @param RangeCount Count of memory ranges.
1469 @param VariableMtrr Variable MTRR array.
1470 @param VariableMtrrCapacity Capacity of variable MTRR array.
1471 @param VariableMtrrCount Count of variable MTRR.
1472 @param BaseAddress Base address of the memory range.
1473 @param Length Length of the memory range.
1474 @param Type MTRR type of the memory range.
1475 @param Alignment0 Alignment of 0.
1477 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1478 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1481 MtrrLibSetMemoryAttributeInVariableMtrr (
1482 IN CONST MEMORY_RANGE
*Ranges
,
1483 IN UINT32 RangeCount
,
1484 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1485 IN UINT32 VariableMtrrCapacity
,
1486 IN OUT UINT32
*VariableMtrrCount
,
1487 IN UINT64 BaseAddress
,
1489 IN MTRR_MEMORY_CACHE_TYPE Type
,
1490 IN UINT64 Alignment0
1494 Allocate one or more variable MTRR to cover the range identified by
1495 BaseAddress and Length.
1497 The routine recursively calls MtrrLibSetMemoryAttributeInVariableMtrr()
1498 to allocate variable MTRRs when the range contains several sub-ranges
1499 with different attributes.
1501 @param Ranges Memory range array holding the memory type
1502 settings for all memory address.
1503 @param RangeCount Count of memory ranges.
1504 @param VariableMtrr Variable MTRR array.
1505 @param VariableMtrrCapacity Capacity of variable MTRR array.
1506 @param VariableMtrrCount Count of variable MTRR.
1507 @param BaseAddress Base address of the memory range.
1508 @param Length Length of the memory range.
1509 @param Type MTRR type of the range.
1510 If it's CacheInvalid, the memory range may
1511 contains several sub-ranges with different
1513 @param Alignment0 Alignment of 0.
1515 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1516 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1519 MtrrLibAddVariableMtrr (
1520 IN CONST MEMORY_RANGE
*Ranges
,
1521 IN UINT32 RangeCount
,
1522 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1523 IN UINT32 VariableMtrrCapacity
,
1524 IN OUT UINT32
*VariableMtrrCount
,
1525 IN PHYSICAL_ADDRESS BaseAddress
,
1527 IN MTRR_MEMORY_CACHE_TYPE Type
,
1528 IN UINT64 Alignment0
1531 RETURN_STATUS Status
;
1535 MTRR_LIB_ASSERT_ALIGNED (BaseAddress
, Length
);
1536 if (Type
== CacheInvalid
) {
1537 for (Index
= 0; Index
< RangeCount
; Index
++) {
1538 if (Ranges
[Index
].BaseAddress
<= BaseAddress
&& BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1541 // Because the Length may not be aligned to BaseAddress, below code calls
1542 // MtrrLibSetMemoryAttributeInVariableMtrr() instead of itself.
1543 // MtrrLibSetMemoryAttributeInVariableMtrr() splits the range to several
1546 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
>= BaseAddress
+ Length
) {
1547 return MtrrLibSetMemoryAttributeInVariableMtrr (
1548 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1549 BaseAddress
, Length
, Ranges
[Index
].Type
, Alignment0
1552 SubLength
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- BaseAddress
;
1553 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1554 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1555 BaseAddress
, SubLength
, Ranges
[Index
].Type
, Alignment0
1557 if (RETURN_ERROR (Status
)) {
1560 BaseAddress
+= SubLength
;
1561 Length
-= SubLength
;
1567 // Because memory ranges cover all the memory addresses, it's impossible to be here.
1570 return RETURN_DEVICE_ERROR
;
1572 for (Index
= 0; Index
< *VariableMtrrCount
; Index
++) {
1573 if (VariableMtrr
[Index
].BaseAddress
== BaseAddress
&& VariableMtrr
[Index
].Length
== Length
) {
1574 ASSERT (VariableMtrr
[Index
].Type
== Type
);
1578 if (Index
== *VariableMtrrCount
) {
1579 if (*VariableMtrrCount
== VariableMtrrCapacity
) {
1580 return RETURN_OUT_OF_RESOURCES
;
1582 VariableMtrr
[Index
].BaseAddress
= BaseAddress
;
1583 VariableMtrr
[Index
].Length
= Length
;
1584 VariableMtrr
[Index
].Type
= Type
;
1585 VariableMtrr
[Index
].Valid
= TRUE
;
1586 VariableMtrr
[Index
].Used
= TRUE
;
1587 (*VariableMtrrCount
)++;
1589 return RETURN_SUCCESS
;
1594 Allocate one or more variable MTRR to cover the range identified by
1595 BaseAddress and Length.
1597 @param Ranges Memory range array holding the memory type
1598 settings for all memory address.
1599 @param RangeCount Count of memory ranges.
1600 @param VariableMtrr Variable MTRR array.
1601 @param VariableMtrrCapacity Capacity of variable MTRR array.
1602 @param VariableMtrrCount Count of variable MTRR.
1603 @param BaseAddress Base address of the memory range.
1604 @param Length Length of the memory range.
1605 @param Type MTRR type of the memory range.
1606 @param Alignment0 Alignment of 0.
1608 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1609 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1612 MtrrLibSetMemoryAttributeInVariableMtrr (
1613 IN CONST MEMORY_RANGE
*Ranges
,
1614 IN UINT32 RangeCount
,
1615 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1616 IN UINT32 VariableMtrrCapacity
,
1617 IN OUT UINT32
*VariableMtrrCount
,
1618 IN UINT64 BaseAddress
,
1620 IN MTRR_MEMORY_CACHE_TYPE Type
,
1621 IN UINT64 Alignment0
1626 UINT32 SubtractiveLeft
;
1627 UINT32 SubtractiveRight
;
1628 BOOLEAN UseLeastAlignment
;
1630 MtrrNumber
= MtrrLibGetMtrrNumber (Ranges
, RangeCount
, VariableMtrr
, *VariableMtrrCount
,
1631 BaseAddress
, Length
, Type
, Alignment0
, &SubtractiveLeft
, &SubtractiveRight
);
1633 if (MtrrNumber
+ *VariableMtrrCount
> VariableMtrrCapacity
) {
1634 return RETURN_OUT_OF_RESOURCES
;
1637 while (SubtractiveLeft
-- != 0) {
1638 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1639 ASSERT (Alignment
<= Length
);
1641 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1642 BaseAddress
- Alignment
, Alignment
, CacheInvalid
, Alignment0
);
1643 BaseAddress
-= Alignment
;
1644 Length
+= Alignment
;
1647 while (Length
!= 0) {
1648 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1649 if (Alignment
> Length
) {
1652 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1653 BaseAddress
, Alignment
, Type
, Alignment0
);
1654 BaseAddress
+= Alignment
;
1655 Length
-= Alignment
;
1658 while (SubtractiveRight
-- != 0) {
1659 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
1660 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1661 BaseAddress
+ Length
, Alignment
, CacheInvalid
, Alignment0
);
1662 Length
+= Alignment
;
1665 UseLeastAlignment
= TRUE
;
1666 while (Length
!= 0) {
1667 if (UseLeastAlignment
) {
1668 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1669 if (Alignment
> Length
) {
1670 UseLeastAlignment
= FALSE
;
1674 if (!UseLeastAlignment
) {
1675 Alignment
= GetPowerOfTwo64 (Length
);
1678 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1679 BaseAddress
, Alignment
, Type
, Alignment0
);
1680 BaseAddress
+= Alignment
;
1681 Length
-= Alignment
;
1683 return RETURN_SUCCESS
;
1687 Return an array of memory ranges holding memory type settings for all memory
1690 @param DefaultType The default memory type.
1691 @param TotalLength The total length of the memory.
1692 @param VariableMtrr The variable MTRR array.
1693 @param VariableMtrrCount The count of variable MTRRs.
1694 @param Ranges Return the memory range array holding memory type
1695 settings for all memory address.
1696 @param RangeCapacity The capacity of memory range array.
1697 @param RangeCount Return the count of memory range.
1699 @retval RETURN_SUCCESS The memory range array is returned successfully.
1700 @retval RETURN_OUT_OF_RESOURCES The count of memory ranges exceeds capacity.
1703 MtrrLibGetMemoryTypes (
1704 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1705 IN UINT64 TotalLength
,
1706 IN CONST VARIABLE_MTRR
*VariableMtrr
,
1707 IN UINT32 VariableMtrrCount
,
1708 OUT MEMORY_RANGE
*Ranges
,
1709 IN UINT32 RangeCapacity
,
1710 OUT UINT32
*RangeCount
1713 RETURN_STATUS Status
;
1719 // UC > * (except WB, UC) > WB
1723 // 0. Set whole range as DefaultType
1726 Ranges
[0].BaseAddress
= 0;
1727 Ranges
[0].Length
= TotalLength
;
1728 Ranges
[0].Type
= DefaultType
;
1733 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1734 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheWriteBack
) {
1735 Status
= MtrrLibSetMemoryType (
1736 Ranges
, RangeCapacity
, RangeCount
,
1737 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1739 if (RETURN_ERROR (Status
)) {
1746 // 2. Set other types than WB or UC
1748 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1749 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
!= CacheWriteBack
&& VariableMtrr
[Index
].Type
!= CacheUncacheable
) {
1750 Status
= MtrrLibSetMemoryType (
1751 Ranges
, RangeCapacity
, RangeCount
,
1752 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1754 if (RETURN_ERROR (Status
)) {
1763 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1764 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheUncacheable
) {
1765 Status
= MtrrLibSetMemoryType (
1766 Ranges
, RangeCapacity
, RangeCount
,
1767 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1769 if (RETURN_ERROR (Status
)) {
1774 return RETURN_SUCCESS
;
1778 Worker function attempts to set the attributes for a memory range.
1780 If MtrrSetting is not NULL, set the attributes into the input MTRR
1782 If MtrrSetting is NULL, set the attributes into MTRRs registers.
1784 @param[in, out] MtrrSetting A buffer holding all MTRRs content.
1785 @param[in] BaseAddress The physical address that is the start
1786 address of a memory range.
1787 @param[in] Length The size in bytes of the memory range.
1788 @param[in] Type The MTRR type to set for the memory range.
1790 @retval RETURN_SUCCESS The attributes were set for the memory
1792 @retval RETURN_INVALID_PARAMETER Length is zero.
1793 @retval RETURN_UNSUPPORTED The processor does not support one or
1794 more bytes of the memory resource range
1795 specified by BaseAddress and Length.
1796 @retval RETURN_UNSUPPORTED The MTRR type is not support for the
1797 memory resource range specified
1798 by BaseAddress and Length.
1799 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
1800 modify the attributes of the memory
1805 MtrrSetMemoryAttributeWorker (
1806 IN OUT MTRR_SETTINGS
*MtrrSetting
,
1807 IN PHYSICAL_ADDRESS BaseAddress
,
1809 IN MTRR_MEMORY_CACHE_TYPE Type
1812 RETURN_STATUS Status
;
1814 UINT32 WorkingIndex
;
1816 // N variable MTRRs can maximumly separate (2N + 1) Ranges, plus 1 range for [0, 1M).
1818 MEMORY_RANGE Ranges
[MTRR_NUMBER_OF_VARIABLE_MTRR
* 2 + 2];
1820 UINT64 MtrrValidBitsMask
;
1821 UINT64 MtrrValidAddressMask
;
1823 MTRR_CONTEXT MtrrContext
;
1824 BOOLEAN MtrrContextValid
;
1826 MTRR_MEMORY_CACHE_TYPE DefaultType
;
1832 BOOLEAN FixedSettingsValid
[MTRR_NUMBER_OF_FIXED_MTRR
];
1833 BOOLEAN FixedSettingsModified
[MTRR_NUMBER_OF_FIXED_MTRR
];
1834 MTRR_FIXED_SETTINGS WorkingFixedSettings
;
1836 UINT32 FirmwareVariableMtrrCount
;
1837 MTRR_VARIABLE_SETTINGS
*VariableSettings
;
1838 MTRR_VARIABLE_SETTINGS OriginalVariableSettings
;
1839 UINT32 OriginalVariableMtrrCount
;
1840 VARIABLE_MTRR OriginalVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1841 UINT32 WorkingVariableMtrrCount
;
1842 VARIABLE_MTRR WorkingVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1843 BOOLEAN VariableSettingModified
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1844 UINTN FreeVariableMtrrCount
;
1847 return RETURN_INVALID_PARAMETER
;
1850 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1851 if (((BaseAddress
& ~MtrrValidAddressMask
) != 0) || (Length
& ~MtrrValidAddressMask
) != 0) {
1852 return RETURN_UNSUPPORTED
;
1854 OriginalVariableMtrrCount
= 0;
1855 VariableSettings
= NULL
;
1857 ZeroMem (&WorkingFixedSettings
, sizeof (WorkingFixedSettings
));
1858 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1859 FixedSettingsValid
[Index
] = FALSE
;
1860 FixedSettingsModified
[Index
] = FALSE
;
1864 // Check if Fixed MTRR
1866 if (BaseAddress
< BASE_1MB
) {
1867 MsrIndex
= (UINT32
)-1;
1868 while ((BaseAddress
< BASE_1MB
) && (Length
!= 0)) {
1869 Status
= MtrrLibProgramFixedMtrr (Type
, &BaseAddress
, &Length
, &MsrIndex
, &ClearMask
, &OrMask
);
1870 if (RETURN_ERROR (Status
)) {
1873 if (MtrrSetting
!= NULL
) {
1874 MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] = (MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1875 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.FE
= 1;
1877 if (!FixedSettingsValid
[MsrIndex
]) {
1878 WorkingFixedSettings
.Mtrr
[MsrIndex
] = AsmReadMsr64 (mMtrrLibFixedMtrrTable
[MsrIndex
].Msr
);
1879 FixedSettingsValid
[MsrIndex
] = TRUE
;
1881 NewValue
= (WorkingFixedSettings
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1882 if (WorkingFixedSettings
.Mtrr
[MsrIndex
] != NewValue
) {
1883 WorkingFixedSettings
.Mtrr
[MsrIndex
] = NewValue
;
1884 FixedSettingsModified
[MsrIndex
] = TRUE
;
1891 // A Length of 0 can only make sense for fixed MTTR ranges.
1892 // Since we just handled the fixed MTRRs, we can skip the
1893 // variable MTRR section.
1900 // Read the default MTRR type
1902 DefaultType
= MtrrGetDefaultMemoryTypeWorker (MtrrSetting
);
1905 // Read all variable MTRRs and convert to Ranges.
1907 OriginalVariableMtrrCount
= GetVariableMtrrCountWorker ();
1908 if (MtrrSetting
== NULL
) {
1909 ZeroMem (&OriginalVariableSettings
, sizeof (OriginalVariableSettings
));
1910 MtrrGetVariableMtrrWorker (NULL
, OriginalVariableMtrrCount
, &OriginalVariableSettings
);
1911 VariableSettings
= &OriginalVariableSettings
;
1913 VariableSettings
= &MtrrSetting
->Variables
;
1915 MtrrGetMemoryAttributeInVariableMtrrWorker (VariableSettings
, OriginalVariableMtrrCount
, MtrrValidBitsMask
, MtrrValidAddressMask
, OriginalVariableMtrr
);
1917 Status
= MtrrLibGetMemoryTypes (
1918 DefaultType
, MtrrValidBitsMask
+ 1, OriginalVariableMtrr
, OriginalVariableMtrrCount
,
1919 Ranges
, 2 * OriginalVariableMtrrCount
+ 1, &RangeCount
1921 ASSERT (Status
== RETURN_SUCCESS
);
1923 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCountWorker ();
1924 ASSERT (RangeCount
<= 2 * FirmwareVariableMtrrCount
+ 1);
1927 // Force [0, 1M) to UC, so that it doesn't impact left subtraction algorithm.
1929 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, 0, SIZE_1MB
, CacheUncacheable
);
1930 ASSERT (Status
== RETURN_SUCCESS
);
1932 // Apply Type to [BaseAddress, BaseAddress + Length)
1934 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, BaseAddress
, Length
, Type
);
1935 if (RETURN_ERROR (Status
)) {
1939 Alignment0
= LShiftU64 (1, (UINTN
) HighBitSet64 (MtrrValidBitsMask
));
1940 WorkingVariableMtrrCount
= 0;
1941 ZeroMem (&WorkingVariableMtrr
, sizeof (WorkingVariableMtrr
));
1942 for (Index
= 0; Index
< RangeCount
; Index
++) {
1943 if (Ranges
[Index
].Type
!= DefaultType
) {
1945 // Maximum allowed MTRR count is (FirmwareVariableMtrrCount + 1)
1946 // Because potentially the range [0, 1MB) is not merged, but can be ignored because fixed MTRR covers that
1948 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1950 WorkingVariableMtrr
, FirmwareVariableMtrrCount
+ 1, &WorkingVariableMtrrCount
,
1951 Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
,
1952 Ranges
[Index
].Type
, Alignment0
1954 if (RETURN_ERROR (Status
)) {
1961 // Remove the [0, 1MB) MTRR if it still exists (not merged with other range)
1963 if (WorkingVariableMtrr
[0].BaseAddress
== 0 && WorkingVariableMtrr
[0].Length
== SIZE_1MB
) {
1964 ASSERT (WorkingVariableMtrr
[0].Type
== CacheUncacheable
);
1965 WorkingVariableMtrrCount
--;
1966 CopyMem (&WorkingVariableMtrr
[0], &WorkingVariableMtrr
[1], WorkingVariableMtrrCount
* sizeof (VARIABLE_MTRR
));
1969 if (WorkingVariableMtrrCount
> FirmwareVariableMtrrCount
) {
1970 return RETURN_OUT_OF_RESOURCES
;
1973 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
1974 VariableSettingModified
[Index
] = FALSE
;
1976 if (!OriginalVariableMtrr
[Index
].Valid
) {
1979 for (WorkingIndex
= 0; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
1980 if (OriginalVariableMtrr
[Index
].BaseAddress
== WorkingVariableMtrr
[WorkingIndex
].BaseAddress
&&
1981 OriginalVariableMtrr
[Index
].Length
== WorkingVariableMtrr
[WorkingIndex
].Length
&&
1982 OriginalVariableMtrr
[Index
].Type
== WorkingVariableMtrr
[WorkingIndex
].Type
) {
1987 if (WorkingIndex
== WorkingVariableMtrrCount
) {
1989 // Remove the one from OriginalVariableMtrr which is not in WorkingVariableMtrr
1991 OriginalVariableMtrr
[Index
].Valid
= FALSE
;
1992 VariableSettingModified
[Index
] = TRUE
;
1995 // Remove the one from WorkingVariableMtrr which is also in OriginalVariableMtrr
1997 WorkingVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
2000 // The above two operations cause that valid MTRR only exists in either OriginalVariableMtrr or WorkingVariableMtrr.
2005 // Merge remaining MTRRs from WorkingVariableMtrr to OriginalVariableMtrr
2007 for (FreeVariableMtrrCount
= 0, WorkingIndex
= 0, Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2008 if (!OriginalVariableMtrr
[Index
].Valid
) {
2009 for (; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
2010 if (WorkingVariableMtrr
[WorkingIndex
].Valid
) {
2014 if (WorkingIndex
== WorkingVariableMtrrCount
) {
2015 FreeVariableMtrrCount
++;
2017 CopyMem (&OriginalVariableMtrr
[Index
], &WorkingVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2018 VariableSettingModified
[Index
] = TRUE
;
2023 ASSERT (OriginalVariableMtrrCount
- FreeVariableMtrrCount
<= FirmwareVariableMtrrCount
);
2026 // Move MTRRs after the FirmwraeVariableMtrrCount position to beginning
2028 WorkingIndex
= FirmwareVariableMtrrCount
;
2029 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
2030 if (!OriginalVariableMtrr
[Index
].Valid
) {
2032 // Found an empty MTRR in WorkingIndex position
2034 for (; WorkingIndex
< OriginalVariableMtrrCount
; WorkingIndex
++) {
2035 if (OriginalVariableMtrr
[WorkingIndex
].Valid
) {
2040 if (WorkingIndex
!= OriginalVariableMtrrCount
) {
2041 CopyMem (&OriginalVariableMtrr
[Index
], &OriginalVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2042 VariableSettingModified
[Index
] = TRUE
;
2043 VariableSettingModified
[WorkingIndex
] = TRUE
;
2044 OriginalVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
2050 // Convert OriginalVariableMtrr to VariableSettings
2051 // NOTE: MTRR from FirmwareVariableMtrr to OriginalVariableMtrr need to update as well.
2053 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2054 if (VariableSettingModified
[Index
]) {
2055 if (OriginalVariableMtrr
[Index
].Valid
) {
2056 VariableSettings
->Mtrr
[Index
].Base
= (OriginalVariableMtrr
[Index
].BaseAddress
& MtrrValidAddressMask
) | (UINT8
) OriginalVariableMtrr
[Index
].Type
;
2057 VariableSettings
->Mtrr
[Index
].Mask
= ((~(OriginalVariableMtrr
[Index
].Length
- 1)) & MtrrValidAddressMask
) | BIT11
;
2059 VariableSettings
->Mtrr
[Index
].Base
= 0;
2060 VariableSettings
->Mtrr
[Index
].Mask
= 0;
2066 if (MtrrSetting
!= NULL
) {
2067 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.E
= 1;
2068 return RETURN_SUCCESS
;
2071 MtrrContextValid
= FALSE
;
2073 // Write fixed MTRRs that have been modified
2075 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2076 if (FixedSettingsModified
[Index
]) {
2077 if (!MtrrContextValid
) {
2078 MtrrLibPreMtrrChange (&MtrrContext
);
2079 MtrrContextValid
= TRUE
;
2082 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2083 WorkingFixedSettings
.Mtrr
[Index
]
2089 // Write variable MTRRs
2090 // When only fixed MTRRs were changed, below loop doesn't run
2091 // because OriginalVariableMtrrCount equals to 0.
2093 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2094 if (VariableSettingModified
[Index
]) {
2095 if (!MtrrContextValid
) {
2096 MtrrLibPreMtrrChange (&MtrrContext
);
2097 MtrrContextValid
= TRUE
;
2100 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2101 VariableSettings
->Mtrr
[Index
].Base
2104 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2105 VariableSettings
->Mtrr
[Index
].Mask
2109 if (MtrrContextValid
) {
2110 MtrrLibPostMtrrChange (&MtrrContext
);
2113 return RETURN_SUCCESS
;
2117 This function attempts to set the attributes for a memory range.
2119 @param[in] BaseAddress The physical address that is the start
2120 address of a memory range.
2121 @param[in] Length The size in bytes of the memory range.
2122 @param[in] Attributes The bit mask of attributes to set for the
2125 @retval RETURN_SUCCESS The attributes were set for the memory
2127 @retval RETURN_INVALID_PARAMETER Length is zero.
2128 @retval RETURN_UNSUPPORTED The processor does not support one or
2129 more bytes of the memory resource range
2130 specified by BaseAddress and Length.
2131 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
2132 for the memory resource range specified
2133 by BaseAddress and Length.
2134 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
2135 range specified by BaseAddress and Length
2137 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
2138 modify the attributes of the memory
2144 MtrrSetMemoryAttribute (
2145 IN PHYSICAL_ADDRESS BaseAddress
,
2147 IN MTRR_MEMORY_CACHE_TYPE Attribute
2150 RETURN_STATUS Status
;
2152 if (!IsMtrrSupported ()) {
2153 return RETURN_UNSUPPORTED
;
2156 Status
= MtrrSetMemoryAttributeWorker (NULL
, BaseAddress
, Length
, Attribute
);
2157 DEBUG ((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a: [%016lx, %016lx) - %r\n",
2158 mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2160 if (!RETURN_ERROR (Status
)) {
2161 MtrrDebugPrintAllMtrrsWorker (NULL
);
2167 This function attempts to set the attributes into MTRR setting buffer for a memory range.
2169 @param[in, out] MtrrSetting MTRR setting buffer to be set.
2170 @param[in] BaseAddress The physical address that is the start address
2172 @param[in] Length The size in bytes of the memory range.
2173 @param[in] Attribute The bit mask of attributes to set for the
2176 @retval RETURN_SUCCESS The attributes were set for the memory range.
2177 @retval RETURN_INVALID_PARAMETER Length is zero.
2178 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
2179 memory resource range specified by BaseAddress and Length.
2180 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
2181 range specified by BaseAddress and Length.
2182 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
2183 BaseAddress and Length cannot be modified.
2184 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
2185 the memory resource range.
2190 MtrrSetMemoryAttributeInMtrrSettings (
2191 IN OUT MTRR_SETTINGS
*MtrrSetting
,
2192 IN PHYSICAL_ADDRESS BaseAddress
,
2194 IN MTRR_MEMORY_CACHE_TYPE Attribute
2197 RETURN_STATUS Status
;
2198 Status
= MtrrSetMemoryAttributeWorker (MtrrSetting
, BaseAddress
, Length
, Attribute
);
2199 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttributeMtrrSettings(%p) %a: [%016lx, %016lx) - %r\n",
2200 MtrrSetting
, mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2202 if (!RETURN_ERROR (Status
)) {
2203 MtrrDebugPrintAllMtrrsWorker (MtrrSetting
);
2210 Worker function setting variable MTRRs
2212 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2216 MtrrSetVariableMtrrWorker (
2217 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2221 UINT32 VariableMtrrCount
;
2223 VariableMtrrCount
= GetVariableMtrrCountWorker ();
2224 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
2226 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
2228 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2229 VariableSettings
->Mtrr
[Index
].Base
2232 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2233 VariableSettings
->Mtrr
[Index
].Mask
2240 This function sets variable MTRRs
2242 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2244 @return The pointer of VariableSettings
2247 MTRR_VARIABLE_SETTINGS
*
2249 MtrrSetVariableMtrr (
2250 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2253 MTRR_CONTEXT MtrrContext
;
2255 if (!IsMtrrSupported ()) {
2256 return VariableSettings
;
2259 MtrrLibPreMtrrChange (&MtrrContext
);
2260 MtrrSetVariableMtrrWorker (VariableSettings
);
2261 MtrrLibPostMtrrChange (&MtrrContext
);
2262 MtrrDebugPrintAllMtrrs ();
2264 return VariableSettings
;
2268 Worker function setting fixed MTRRs
2270 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2274 MtrrSetFixedMtrrWorker (
2275 IN MTRR_FIXED_SETTINGS
*FixedSettings
2280 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2282 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2283 FixedSettings
->Mtrr
[Index
]
2290 This function sets fixed MTRRs
2292 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2294 @retval The pointer of FixedSettings
2297 MTRR_FIXED_SETTINGS
*
2300 IN MTRR_FIXED_SETTINGS
*FixedSettings
2303 MTRR_CONTEXT MtrrContext
;
2305 if (!IsMtrrSupported ()) {
2306 return FixedSettings
;
2309 MtrrLibPreMtrrChange (&MtrrContext
);
2310 MtrrSetFixedMtrrWorker (FixedSettings
);
2311 MtrrLibPostMtrrChange (&MtrrContext
);
2312 MtrrDebugPrintAllMtrrs ();
2314 return FixedSettings
;
2319 This function gets the content in all MTRRs (variable and fixed)
2321 @param[out] MtrrSetting A buffer to hold all MTRRs content.
2323 @retval the pointer of MtrrSetting
2329 OUT MTRR_SETTINGS
*MtrrSetting
2332 if (!IsMtrrSupported ()) {
2339 MtrrGetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2342 // Get variable MTRRs
2344 MtrrGetVariableMtrrWorker (
2346 GetVariableMtrrCountWorker (),
2347 &MtrrSetting
->Variables
2351 // Get MTRR_DEF_TYPE value
2353 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
2360 This function sets all MTRRs (variable and fixed)
2362 @param[in] MtrrSetting A buffer holding all MTRRs content.
2364 @retval The pointer of MtrrSetting
2370 IN MTRR_SETTINGS
*MtrrSetting
2373 MTRR_CONTEXT MtrrContext
;
2375 if (!IsMtrrSupported ()) {
2379 MtrrLibPreMtrrChange (&MtrrContext
);
2384 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2387 // Set variable MTRRs
2389 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
2392 // Set MTRR_DEF_TYPE value
2394 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
2396 MtrrLibPostMtrrChangeEnableCache (&MtrrContext
);
2403 Checks if MTRR is supported.
2405 @retval TRUE MTRR is supported.
2406 @retval FALSE MTRR is not supported.
2415 CPUID_VERSION_INFO_EDX Edx
;
2416 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
2419 // Check CPUID(1).EDX[12] for MTRR capability
2421 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
2422 if (Edx
.Bits
.MTRR
== 0) {
2427 // Check number of variable MTRRs and fixed MTRRs existence.
2428 // If number of variable MTRRs is zero, or fixed MTRRs do not
2429 // exist, return false.
2431 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
2432 if ((MtrrCap
.Bits
.VCNT
== 0) || (MtrrCap
.Bits
.FIX
== 0)) {