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
;
708 // Calculate the alignment of the base address.
710 for (MtrrNumber
= 0; Length
!= 0; MtrrNumber
++) {
711 if (UseLeastAlignment
) {
712 SubLength
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
714 if (SubLength
> Length
) {
716 // Set a flag when remaining length is too small
717 // so that MtrrLibLeastAlignment() is not called in following loops.
719 UseLeastAlignment
= FALSE
;
723 if (!UseLeastAlignment
) {
724 SubLength
= GetPowerOfTwo64 (Length
);
727 BaseAddress
+= SubLength
;
735 Return whether the left MTRR type precedes the right MTRR type.
737 The MTRR type precedence rules are:
738 1. UC precedes any other type
740 For further details, please refer the IA32 Software Developer's Manual,
741 Volume 3, Section "MTRR Precedences".
743 @param Left The left MTRR type.
744 @param Right The right MTRR type.
746 @retval TRUE Left precedes Right.
747 @retval FALSE Left doesn't precede Right.
750 MtrrLibTypeLeftPrecedeRight (
751 IN MTRR_MEMORY_CACHE_TYPE Left
,
752 IN MTRR_MEMORY_CACHE_TYPE Right
755 return (BOOLEAN
) (Left
== CacheUncacheable
|| (Left
== CacheWriteThrough
&& Right
== CacheWriteBack
));
760 Return whether the type of the specified range can precede the specified type.
762 @param Ranges Memory range array holding memory type settings for all
764 @param RangeCount Count of memory ranges.
765 @param Type Type to check precedence.
766 @param SubBase Base address of the specified range.
767 @param SubLength Length of the specified range.
769 @retval TRUE The type of the specified range can precede the Type.
770 @retval FALSE The type of the specified range cannot precede the Type.
771 So the subtraction is not applicable.
774 MtrrLibSubstractable (
775 IN CONST MEMORY_RANGE
*Ranges
,
776 IN UINT32 RangeCount
,
777 IN MTRR_MEMORY_CACHE_TYPE Type
,
786 for (Index
= 0; Index
< RangeCount
; Index
++) {
787 if (Ranges
[Index
].BaseAddress
<= SubBase
&& SubBase
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
789 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
>= SubBase
+ SubLength
) {
790 return MtrrLibTypeLeftPrecedeRight (Ranges
[Index
].Type
, Type
);
793 if (!MtrrLibTypeLeftPrecedeRight (Ranges
[Index
].Type
, Type
)) {
797 Length
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- SubBase
;
809 Return the number of required variable MTRRs to cover the specified range.
811 The routine considers subtraction in the both side of the range to find out
812 the most optimal solution (which uses the least MTRRs).
814 @param Ranges Array holding memory type settings of all memory
816 @param RangeCount Count of memory ranges.
817 @param VariableMtrr Array holding allocated variable MTRRs.
818 @param VariableMtrrCount Count of allocated variable MTRRs.
819 @param BaseAddress Base address of the specified range.
820 @param Length Length of the specified range.
821 @param Type MTRR type of the specified range.
822 @param Alignment0 Alignment of 0.
823 @param SubLeft Return the count of left subtraction.
824 @param SubRight Return the count of right subtraction.
826 @return Number of required variable MTRRs.
829 MtrrLibGetMtrrNumber (
830 IN CONST MEMORY_RANGE
*Ranges
,
831 IN UINT32 RangeCount
,
832 IN CONST VARIABLE_MTRR
*VariableMtrr
,
833 IN UINT32 VariableMtrrCount
,
834 IN UINT64 BaseAddress
,
836 IN MTRR_MEMORY_CACHE_TYPE Type
,
837 IN UINT64 Alignment0
,
838 OUT UINT32
*SubLeft
, // subtractive from BaseAddress to get more aligned address, to save MTRR
839 OUT UINT32
*SubRight
// subtractive from BaseAddress + Length, to save MTRR
843 UINT32 LeastLeftMtrrNumber
;
844 UINT32 MiddleMtrrNumber
;
845 UINT32 LeastRightMtrrNumber
;
846 UINT32 CurrentMtrrNumber
;
847 UINT32 SubtractiveCount
;
848 UINT32 SubtractiveMtrrNumber
;
849 UINT32 LeastSubtractiveMtrrNumber
;
850 UINT64 SubtractiveBaseAddress
;
851 UINT64 SubtractiveLength
;
852 UINT64 BaseAlignment
;
857 LeastSubtractiveMtrrNumber
= 0;
861 // Get the optimal left subtraction solution.
863 if (BaseAddress
!= 0) {
864 SubtractiveBaseAddress
= 0;
865 SubtractiveLength
= 0;
867 // Get the MTRR number needed without left subtraction.
869 LeastLeftMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
872 // Left subtraction bit by bit, to find the optimal left subtraction solution.
874 for (SubtractiveMtrrNumber
= 0, SubtractiveCount
= 1; BaseAddress
!= 0; SubtractiveCount
++) {
875 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
878 // Check whether the memory type of [BaseAddress - Alignment, BaseAddress) can override Type.
879 // IA32 Manual defines the following override rules:
883 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
- Alignment
, Alignment
)) {
887 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
888 if ((VariableMtrr
[Index
].BaseAddress
== BaseAddress
- Alignment
) &&
889 (VariableMtrr
[Index
].Length
== Alignment
)) {
893 if (Index
== VariableMtrrCount
) {
895 // Increment SubtractiveMtrrNumber when [BaseAddress - Alignment, BaseAddress) is not be planed as a MTRR
897 SubtractiveMtrrNumber
++;
900 BaseAddress
-= Alignment
;
903 CurrentMtrrNumber
= SubtractiveMtrrNumber
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
904 if (CurrentMtrrNumber
<= LeastLeftMtrrNumber
) {
905 LeastLeftMtrrNumber
= CurrentMtrrNumber
;
906 LeastSubtractiveMtrrNumber
= SubtractiveMtrrNumber
;
907 *SubLeft
= SubtractiveCount
;
908 SubtractiveBaseAddress
= BaseAddress
;
909 SubtractiveLength
= Length
;
914 // If left subtraction is better, subtract BaseAddress to left, and enlarge Length
917 BaseAddress
= SubtractiveBaseAddress
;
918 Length
= SubtractiveLength
;
923 // Increment BaseAddress greedily until (BaseAddress + Alignment) exceeds (BaseAddress + Length)
925 MiddleMtrrNumber
= 0;
926 while (Length
!= 0) {
927 BaseAlignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
928 if (BaseAlignment
> Length
) {
931 BaseAddress
+= BaseAlignment
;
932 Length
-= BaseAlignment
;
938 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
;
943 // Get the optimal right subtraction solution.
947 // Get the MTRR number needed without right subtraction.
949 LeastRightMtrrNumber
= MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
951 for (SubtractiveCount
= 1; Length
< BaseAlignment
; SubtractiveCount
++) {
952 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
953 if (!MtrrLibSubstractable (Ranges
, RangeCount
, Type
, BaseAddress
+ Length
, Alignment
)) {
960 // SubtractiveCount = Number of MTRRs used for subtraction
962 CurrentMtrrNumber
= SubtractiveCount
+ MtrrLibGetPositiveMtrrNumber (BaseAddress
, Length
, Alignment0
);
963 if (CurrentMtrrNumber
<= LeastRightMtrrNumber
) {
964 LeastRightMtrrNumber
= CurrentMtrrNumber
;
965 *SubRight
= SubtractiveCount
;
966 SubtractiveLength
= Length
;
970 return LeastSubtractiveMtrrNumber
+ MiddleMtrrNumber
+ LeastRightMtrrNumber
;
974 Initializes the valid bits mask and valid address mask for MTRRs.
976 This function initializes the valid bits mask and valid address mask for MTRRs.
978 @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR
979 @param[out] MtrrValidAddressMask The valid address mask for the MTRR
983 MtrrLibInitializeMtrrMask (
984 OUT UINT64
*MtrrValidBitsMask
,
985 OUT UINT64
*MtrrValidAddressMask
988 UINT32 MaxExtendedFunction
;
989 CPUID_VIR_PHY_ADDRESS_SIZE_EAX VirPhyAddressSize
;
992 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &MaxExtendedFunction
, NULL
, NULL
, NULL
);
994 if (MaxExtendedFunction
>= CPUID_VIR_PHY_ADDRESS_SIZE
) {
995 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, &VirPhyAddressSize
.Uint32
, NULL
, NULL
, NULL
);
997 VirPhyAddressSize
.Bits
.PhysicalAddressBits
= 36;
1000 *MtrrValidBitsMask
= LShiftU64 (1, VirPhyAddressSize
.Bits
.PhysicalAddressBits
) - 1;
1001 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
1006 Determines the real attribute of a memory range.
1008 This function is to arbitrate the real attribute of the memory when
1009 there are 2 MTRRs covers the same memory range. For further details,
1010 please refer the IA32 Software Developer's Manual, Volume 3,
1011 Section "MTRR Precedences".
1013 @param[in] MtrrType1 The first kind of Memory type
1014 @param[in] MtrrType2 The second kind of memory type
1017 MTRR_MEMORY_CACHE_TYPE
1019 IN MTRR_MEMORY_CACHE_TYPE MtrrType1
,
1020 IN MTRR_MEMORY_CACHE_TYPE MtrrType2
1023 if (MtrrType1
== MtrrType2
) {
1028 MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
) ||
1029 MtrrLibTypeLeftPrecedeRight (MtrrType2
, MtrrType1
)
1032 if (MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
)) {
1040 Worker function will get the memory cache type of the specific address.
1042 If MtrrSetting is not NULL, gets the memory cache type from input
1043 MTRR settings buffer.
1044 If MtrrSetting is NULL, gets the memory cache type from MTRRs.
1046 @param[in] MtrrSetting A buffer holding all MTRRs content.
1047 @param[in] Address The specific address
1049 @return Memory cache type of the specific address
1052 MTRR_MEMORY_CACHE_TYPE
1053 MtrrGetMemoryAttributeByAddressWorker (
1054 IN MTRR_SETTINGS
*MtrrSetting
,
1055 IN PHYSICAL_ADDRESS Address
1058 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
1062 MTRR_MEMORY_CACHE_TYPE MtrrType
;
1063 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1064 UINT64 MtrrValidBitsMask
;
1065 UINT64 MtrrValidAddressMask
;
1066 UINT32 VariableMtrrCount
;
1067 MTRR_VARIABLE_SETTINGS VariableSettings
;
1070 // Check if MTRR is enabled, if not, return UC as attribute
1072 if (MtrrSetting
== NULL
) {
1073 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
1075 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
1078 if (DefType
.Bits
.E
== 0) {
1079 return CacheUncacheable
;
1083 // If address is less than 1M, then try to go through the fixed MTRR
1085 if (Address
< BASE_1MB
) {
1086 if (DefType
.Bits
.FE
!= 0) {
1088 // Go through the fixed MTRR
1090 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1091 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1092 Address
< mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1093 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)) {
1095 ((UINTN
) Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1096 mMtrrLibFixedMtrrTable
[Index
].Length
;
1097 if (MtrrSetting
== NULL
) {
1098 FixedMtrr
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1100 FixedMtrr
= MtrrSetting
->Fixed
.Mtrr
[Index
];
1102 return (MTRR_MEMORY_CACHE_TYPE
) (RShiftU64 (FixedMtrr
, SubIndex
* 8) & 0xFF);
1108 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1109 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1110 MtrrGetVariableMtrrWorker (MtrrSetting
, VariableMtrrCount
, &VariableSettings
);
1112 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1113 MtrrGetMemoryAttributeInVariableMtrrWorker (
1117 MtrrValidAddressMask
,
1122 // Go through the variable MTRR
1124 MtrrType
= CacheInvalid
;
1125 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1126 if (VariableMtrr
[Index
].Valid
) {
1127 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1128 Address
< VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
) {
1129 if (MtrrType
== CacheInvalid
) {
1130 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
;
1132 MtrrType
= MtrrLibPrecedence (MtrrType
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
);
1139 // If there is no MTRR which covers the Address, use the default MTRR type.
1141 if (MtrrType
== CacheInvalid
) {
1142 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
1150 This function will get the memory cache type of the specific address.
1152 This function is mainly for debug purpose.
1154 @param[in] Address The specific address
1156 @return Memory cache type of the specific address
1159 MTRR_MEMORY_CACHE_TYPE
1161 MtrrGetMemoryAttribute (
1162 IN PHYSICAL_ADDRESS Address
1165 if (!IsMtrrSupported ()) {
1166 return CacheUncacheable
;
1169 return MtrrGetMemoryAttributeByAddressWorker (NULL
, Address
);
1173 Worker function prints all MTRRs for debugging.
1175 If MtrrSetting is not NULL, print MTRR settings from input MTRR
1177 If MtrrSetting is NULL, print MTRR settings from MTRRs.
1179 @param MtrrSetting A buffer holding all MTRRs content.
1182 MtrrDebugPrintAllMtrrsWorker (
1183 IN MTRR_SETTINGS
*MtrrSetting
1187 MTRR_SETTINGS LocalMtrrs
;
1188 MTRR_SETTINGS
*Mtrrs
;
1191 UINTN VariableMtrrCount
;
1199 UINT64 NoRangeLimit
;
1202 UINTN PreviousMemoryType
;
1205 if (!IsMtrrSupported ()) {
1209 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1210 DEBUG((DEBUG_CACHE
, "=============\n"));
1212 if (MtrrSetting
!= NULL
) {
1213 Mtrrs
= MtrrSetting
;
1215 MtrrGetAllMtrrs (&LocalMtrrs
);
1216 Mtrrs
= &LocalMtrrs
;
1219 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", Mtrrs
->MtrrDefType
));
1220 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1221 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, Mtrrs
->Fixed
.Mtrr
[Index
]));
1224 VariableMtrrCount
= GetVariableMtrrCount ();
1225 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1226 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1228 Mtrrs
->Variables
.Mtrr
[Index
].Base
,
1229 Mtrrs
->Variables
.Mtrr
[Index
].Mask
1232 DEBUG((DEBUG_CACHE
, "\n"));
1233 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1234 DEBUG((DEBUG_CACHE
, "====================================\n"));
1237 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1238 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1239 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1240 for (Index1
= 0; Index1
< 8; Index1
++) {
1241 MemoryType
= (UINTN
)(RShiftU64 (Mtrrs
->Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff);
1242 if (MemoryType
> CacheWriteBack
) {
1243 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1245 if (MemoryType
!= PreviousMemoryType
) {
1246 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1247 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1249 PreviousMemoryType
= MemoryType
;
1250 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1252 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1255 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1257 VariableMtrrCount
= GetVariableMtrrCount ();
1260 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1261 if (RegEax
>= 0x80000008) {
1262 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1263 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1266 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1268 MemoryType
= MtrrGetMemoryAttributeByAddressWorker (Mtrrs
, Base
);
1269 if (MemoryType
> CacheWriteBack
) {
1270 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1273 if (MemoryType
!= PreviousMemoryType
) {
1274 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1275 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1277 PreviousMemoryType
= MemoryType
;
1278 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1281 RangeBase
= BASE_1MB
;
1282 NoRangeBase
= BASE_1MB
;
1284 NoRangeLimit
= Limit
;
1286 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1287 if ((Mtrrs
->Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1289 // If mask is not valid, then do not display range
1293 MtrrBase
= (Mtrrs
->Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1294 MtrrLimit
= MtrrBase
+ ((~(Mtrrs
->Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1296 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1300 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1301 RangeBase
= MtrrBase
;
1303 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1304 RangeBase
= MtrrLimit
+ 1;
1306 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1307 RangeLimit
= MtrrBase
- 1;
1309 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1310 RangeLimit
= MtrrLimit
;
1313 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1314 NoRangeBase
= MtrrLimit
+ 1;
1316 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1317 NoRangeLimit
= MtrrBase
- 1;
1322 Base
= RangeLimit
+ 1;
1324 Base
= NoRangeLimit
+ 1;
1326 } while (Base
< Limit
);
1327 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1333 This function prints all MTRRs for debugging.
1337 MtrrDebugPrintAllMtrrs (
1341 MtrrDebugPrintAllMtrrsWorker (NULL
);
1345 Update the Ranges array to change the specified range identified by
1346 BaseAddress and Length to Type.
1348 @param Ranges Array holding memory type settings for all memory regions.
1349 @param Capacity The maximum count of memory ranges the array can hold.
1350 @param Count Return the new memory range count in the array.
1351 @param BaseAddress The base address of the memory range to change type.
1352 @param Length The length of the memory range to change type.
1353 @param Type The new type of the specified memory range.
1355 @retval RETURN_SUCCESS The type of the specified memory range is
1356 changed successfully.
1357 @retval RETURN_OUT_OF_RESOURCES The new type set causes the count of memory
1358 range exceeds capacity.
1361 MtrrLibSetMemoryType (
1362 IN MEMORY_RANGE
*Ranges
,
1364 IN OUT UINT32
*Count
,
1365 IN UINT64 BaseAddress
,
1367 IN MTRR_MEMORY_CACHE_TYPE Type
1380 Limit
= BaseAddress
+ Length
;
1381 StartIndex
= *Count
;
1383 for (Index
= 0; Index
< *Count
; Index
++) {
1384 if ((StartIndex
== *Count
) &&
1385 (Ranges
[Index
].BaseAddress
<= BaseAddress
) &&
1386 (BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1388 LengthLeft
= BaseAddress
- Ranges
[Index
].BaseAddress
;
1391 if ((EndIndex
== *Count
) &&
1392 (Ranges
[Index
].BaseAddress
< Limit
) &&
1393 (Limit
<= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1395 LengthRight
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- Limit
;
1400 ASSERT (StartIndex
!= *Count
&& EndIndex
!= *Count
);
1401 if (StartIndex
== EndIndex
&& Ranges
[StartIndex
].Type
== Type
) {
1402 return RETURN_SUCCESS
;
1406 // The type change may cause merging with previous range or next range.
1407 // Update the StartIndex, EndIndex, BaseAddress, Length so that following
1408 // logic doesn't need to consider merging.
1410 if (StartIndex
!= 0) {
1411 if (LengthLeft
== 0 && Ranges
[StartIndex
- 1].Type
== Type
) {
1413 Length
+= Ranges
[StartIndex
].Length
;
1414 BaseAddress
-= Ranges
[StartIndex
].Length
;
1417 if (EndIndex
!= (*Count
) - 1) {
1418 if (LengthRight
== 0 && Ranges
[EndIndex
+ 1].Type
== Type
) {
1420 Length
+= Ranges
[EndIndex
].Length
;
1425 // |- 0 -|- 1 -|- 2 -|- 3 -| StartIndex EndIndex DeltaCount Count (Count = 4)
1426 // |++++++++++++++++++| 0 3 1=3-0-2 3
1427 // |+++++++| 0 1 -1=1-0-2 5
1428 // |+| 0 0 -2=0-0-2 6
1429 // |+++| 0 0 -1=0-0-2+1 5
1432 DeltaCount
= EndIndex
- StartIndex
- 2;
1433 if (LengthLeft
== 0) {
1436 if (LengthRight
== 0) {
1439 if (*Count
- DeltaCount
> Capacity
) {
1440 return RETURN_OUT_OF_RESOURCES
;
1444 // Reserve (-DeltaCount) space
1446 CopyMem (&Ranges
[EndIndex
+ 1 - DeltaCount
], &Ranges
[EndIndex
+ 1], (*Count
- EndIndex
- 1) * sizeof (Ranges
[0]));
1447 *Count
-= DeltaCount
;
1449 if (LengthLeft
!= 0) {
1450 Ranges
[StartIndex
].Length
= LengthLeft
;
1453 if (LengthRight
!= 0) {
1454 Ranges
[EndIndex
- DeltaCount
].BaseAddress
= BaseAddress
+ Length
;
1455 Ranges
[EndIndex
- DeltaCount
].Length
= LengthRight
;
1456 Ranges
[EndIndex
- DeltaCount
].Type
= Ranges
[EndIndex
].Type
;
1458 Ranges
[StartIndex
].BaseAddress
= BaseAddress
;
1459 Ranges
[StartIndex
].Length
= Length
;
1460 Ranges
[StartIndex
].Type
= Type
;
1461 return RETURN_SUCCESS
;
1465 Allocate one or more variable MTRR to cover the range identified by
1466 BaseAddress and Length.
1468 @param Ranges Memory range array holding the memory type
1469 settings for all memory address.
1470 @param RangeCount Count of memory ranges.
1471 @param VariableMtrr Variable MTRR array.
1472 @param VariableMtrrCapacity Capacity of variable MTRR array.
1473 @param VariableMtrrCount Count of variable MTRR.
1474 @param BaseAddress Base address of the memory range.
1475 @param Length Length of the memory range.
1476 @param Type MTRR type of the memory range.
1477 @param Alignment0 Alignment of 0.
1479 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1480 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1483 MtrrLibSetMemoryAttributeInVariableMtrr (
1484 IN CONST MEMORY_RANGE
*Ranges
,
1485 IN UINT32 RangeCount
,
1486 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1487 IN UINT32 VariableMtrrCapacity
,
1488 IN OUT UINT32
*VariableMtrrCount
,
1489 IN UINT64 BaseAddress
,
1491 IN MTRR_MEMORY_CACHE_TYPE Type
,
1492 IN UINT64 Alignment0
1496 Allocate one or more variable MTRR to cover the range identified by
1497 BaseAddress and Length.
1499 The routine recursively calls MtrrLibSetMemoryAttributeInVariableMtrr()
1500 to allocate variable MTRRs when the range contains several sub-ranges
1501 with different attributes.
1503 @param Ranges Memory range array holding the memory type
1504 settings for all memory address.
1505 @param RangeCount Count of memory ranges.
1506 @param VariableMtrr Variable MTRR array.
1507 @param VariableMtrrCapacity Capacity of variable MTRR array.
1508 @param VariableMtrrCount Count of variable MTRR.
1509 @param BaseAddress Base address of the memory range.
1510 @param Length Length of the memory range.
1511 @param Type MTRR type of the range.
1512 If it's CacheInvalid, the memory range may
1513 contains several sub-ranges with different
1515 @param Alignment0 Alignment of 0.
1517 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1518 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1521 MtrrLibAddVariableMtrr (
1522 IN CONST MEMORY_RANGE
*Ranges
,
1523 IN UINT32 RangeCount
,
1524 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1525 IN UINT32 VariableMtrrCapacity
,
1526 IN OUT UINT32
*VariableMtrrCount
,
1527 IN PHYSICAL_ADDRESS BaseAddress
,
1529 IN MTRR_MEMORY_CACHE_TYPE Type
,
1530 IN UINT64 Alignment0
1533 RETURN_STATUS Status
;
1537 MTRR_LIB_ASSERT_ALIGNED (BaseAddress
, Length
);
1538 if (Type
== CacheInvalid
) {
1539 ASSERT (Ranges
!= NULL
);
1540 for (Index
= 0; Index
< RangeCount
; Index
++) {
1541 if (Ranges
[Index
].BaseAddress
<= BaseAddress
&& BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1544 // Because the Length may not be aligned to BaseAddress, below code calls
1545 // MtrrLibSetMemoryAttributeInVariableMtrr() instead of itself.
1546 // MtrrLibSetMemoryAttributeInVariableMtrr() splits the range to several
1549 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
>= BaseAddress
+ Length
) {
1550 return MtrrLibSetMemoryAttributeInVariableMtrr (
1551 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1552 BaseAddress
, Length
, Ranges
[Index
].Type
, Alignment0
1555 SubLength
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- BaseAddress
;
1556 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1557 Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1558 BaseAddress
, SubLength
, Ranges
[Index
].Type
, Alignment0
1560 if (RETURN_ERROR (Status
)) {
1563 BaseAddress
+= SubLength
;
1564 Length
-= SubLength
;
1570 // Because memory ranges cover all the memory addresses, it's impossible to be here.
1573 return RETURN_DEVICE_ERROR
;
1575 for (Index
= 0; Index
< *VariableMtrrCount
; Index
++) {
1576 if (VariableMtrr
[Index
].BaseAddress
== BaseAddress
&& VariableMtrr
[Index
].Length
== Length
) {
1577 ASSERT (VariableMtrr
[Index
].Type
== Type
);
1581 if (Index
== *VariableMtrrCount
) {
1582 if (*VariableMtrrCount
== VariableMtrrCapacity
) {
1583 return RETURN_OUT_OF_RESOURCES
;
1585 VariableMtrr
[Index
].BaseAddress
= BaseAddress
;
1586 VariableMtrr
[Index
].Length
= Length
;
1587 VariableMtrr
[Index
].Type
= Type
;
1588 VariableMtrr
[Index
].Valid
= TRUE
;
1589 VariableMtrr
[Index
].Used
= TRUE
;
1590 (*VariableMtrrCount
)++;
1592 return RETURN_SUCCESS
;
1597 Allocate one or more variable MTRR to cover the range identified by
1598 BaseAddress and Length.
1600 @param Ranges Memory range array holding the memory type
1601 settings for all memory address.
1602 @param RangeCount Count of memory ranges.
1603 @param VariableMtrr Variable MTRR array.
1604 @param VariableMtrrCapacity Capacity of variable MTRR array.
1605 @param VariableMtrrCount Count of variable MTRR.
1606 @param BaseAddress Base address of the memory range.
1607 @param Length Length of the memory range.
1608 @param Type MTRR type of the memory range.
1609 @param Alignment0 Alignment of 0.
1611 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1612 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1615 MtrrLibSetMemoryAttributeInVariableMtrr (
1616 IN CONST MEMORY_RANGE
*Ranges
,
1617 IN UINT32 RangeCount
,
1618 IN OUT VARIABLE_MTRR
*VariableMtrr
,
1619 IN UINT32 VariableMtrrCapacity
,
1620 IN OUT UINT32
*VariableMtrrCount
,
1621 IN UINT64 BaseAddress
,
1623 IN MTRR_MEMORY_CACHE_TYPE Type
,
1624 IN UINT64 Alignment0
1629 UINT32 SubtractiveLeft
;
1630 UINT32 SubtractiveRight
;
1631 BOOLEAN UseLeastAlignment
;
1635 MtrrNumber
= MtrrLibGetMtrrNumber (Ranges
, RangeCount
, VariableMtrr
, *VariableMtrrCount
,
1636 BaseAddress
, Length
, Type
, Alignment0
, &SubtractiveLeft
, &SubtractiveRight
);
1638 if (MtrrNumber
+ *VariableMtrrCount
> VariableMtrrCapacity
) {
1639 return RETURN_OUT_OF_RESOURCES
;
1642 while (SubtractiveLeft
-- != 0) {
1643 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1644 ASSERT (Alignment
<= Length
);
1646 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1647 BaseAddress
- Alignment
, Alignment
, CacheInvalid
, Alignment0
);
1648 BaseAddress
-= Alignment
;
1649 Length
+= Alignment
;
1652 while (Length
!= 0) {
1653 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1654 if (Alignment
> Length
) {
1657 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1658 BaseAddress
, Alignment
, Type
, Alignment0
);
1659 BaseAddress
+= Alignment
;
1660 Length
-= Alignment
;
1663 while (SubtractiveRight
-- != 0) {
1664 Alignment
= MtrrLibLeastAlignment (BaseAddress
+ Length
, Alignment0
);
1665 MtrrLibAddVariableMtrr (Ranges
, RangeCount
, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1666 BaseAddress
+ Length
, Alignment
, CacheInvalid
, Alignment0
);
1667 Length
+= Alignment
;
1670 UseLeastAlignment
= TRUE
;
1671 while (Length
!= 0) {
1672 if (UseLeastAlignment
) {
1673 Alignment
= MtrrLibLeastAlignment (BaseAddress
, Alignment0
);
1674 if (Alignment
> Length
) {
1675 UseLeastAlignment
= FALSE
;
1679 if (!UseLeastAlignment
) {
1680 Alignment
= GetPowerOfTwo64 (Length
);
1683 MtrrLibAddVariableMtrr (NULL
, 0, VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1684 BaseAddress
, Alignment
, Type
, Alignment0
);
1685 BaseAddress
+= Alignment
;
1686 Length
-= Alignment
;
1688 return RETURN_SUCCESS
;
1692 Return an array of memory ranges holding memory type settings for all memory
1695 @param DefaultType The default memory type.
1696 @param TotalLength The total length of the memory.
1697 @param VariableMtrr The variable MTRR array.
1698 @param VariableMtrrCount The count of variable MTRRs.
1699 @param Ranges Return the memory range array holding memory type
1700 settings for all memory address.
1701 @param RangeCapacity The capacity of memory range array.
1702 @param RangeCount Return the count of memory range.
1704 @retval RETURN_SUCCESS The memory range array is returned successfully.
1705 @retval RETURN_OUT_OF_RESOURCES The count of memory ranges exceeds capacity.
1708 MtrrLibGetMemoryTypes (
1709 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1710 IN UINT64 TotalLength
,
1711 IN CONST VARIABLE_MTRR
*VariableMtrr
,
1712 IN UINT32 VariableMtrrCount
,
1713 OUT MEMORY_RANGE
*Ranges
,
1714 IN UINT32 RangeCapacity
,
1715 OUT UINT32
*RangeCount
1718 RETURN_STATUS Status
;
1724 // UC > * (except WB, UC) > WB
1728 // 0. Set whole range as DefaultType
1731 Ranges
[0].BaseAddress
= 0;
1732 Ranges
[0].Length
= TotalLength
;
1733 Ranges
[0].Type
= DefaultType
;
1738 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1739 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheWriteBack
) {
1740 Status
= MtrrLibSetMemoryType (
1741 Ranges
, RangeCapacity
, RangeCount
,
1742 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1744 if (RETURN_ERROR (Status
)) {
1751 // 2. Set other types than WB or UC
1753 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1754 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
!= CacheWriteBack
&& VariableMtrr
[Index
].Type
!= CacheUncacheable
) {
1755 Status
= MtrrLibSetMemoryType (
1756 Ranges
, RangeCapacity
, RangeCount
,
1757 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1759 if (RETURN_ERROR (Status
)) {
1768 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1769 if (VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Type
== CacheUncacheable
) {
1770 Status
= MtrrLibSetMemoryType (
1771 Ranges
, RangeCapacity
, RangeCount
,
1772 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
1774 if (RETURN_ERROR (Status
)) {
1779 return RETURN_SUCCESS
;
1783 Worker function attempts to set the attributes for a memory range.
1785 If MtrrSetting is not NULL, set the attributes into the input MTRR
1787 If MtrrSetting is NULL, set the attributes into MTRRs registers.
1789 @param[in, out] MtrrSetting A buffer holding all MTRRs content.
1790 @param[in] BaseAddress The physical address that is the start
1791 address of a memory range.
1792 @param[in] Length The size in bytes of the memory range.
1793 @param[in] Type The MTRR type to set for the memory range.
1795 @retval RETURN_SUCCESS The attributes were set for the memory
1797 @retval RETURN_INVALID_PARAMETER Length is zero.
1798 @retval RETURN_UNSUPPORTED The processor does not support one or
1799 more bytes of the memory resource range
1800 specified by BaseAddress and Length.
1801 @retval RETURN_UNSUPPORTED The MTRR type is not support for the
1802 memory resource range specified
1803 by BaseAddress and Length.
1804 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
1805 modify the attributes of the memory
1810 MtrrSetMemoryAttributeWorker (
1811 IN OUT MTRR_SETTINGS
*MtrrSetting
,
1812 IN PHYSICAL_ADDRESS BaseAddress
,
1814 IN MTRR_MEMORY_CACHE_TYPE Type
1817 RETURN_STATUS Status
;
1819 UINT32 WorkingIndex
;
1821 // N variable MTRRs can maximumly separate (2N + 1) Ranges, plus 1 range for [0, 1M).
1823 MEMORY_RANGE Ranges
[MTRR_NUMBER_OF_VARIABLE_MTRR
* 2 + 2];
1825 UINT64 MtrrValidBitsMask
;
1826 UINT64 MtrrValidAddressMask
;
1828 MTRR_CONTEXT MtrrContext
;
1829 BOOLEAN MtrrContextValid
;
1831 MTRR_MEMORY_CACHE_TYPE DefaultType
;
1837 BOOLEAN FixedSettingsValid
[MTRR_NUMBER_OF_FIXED_MTRR
];
1838 BOOLEAN FixedSettingsModified
[MTRR_NUMBER_OF_FIXED_MTRR
];
1839 MTRR_FIXED_SETTINGS WorkingFixedSettings
;
1841 UINT32 FirmwareVariableMtrrCount
;
1842 MTRR_VARIABLE_SETTINGS
*VariableSettings
;
1843 MTRR_VARIABLE_SETTINGS OriginalVariableSettings
;
1844 UINT32 OriginalVariableMtrrCount
;
1845 VARIABLE_MTRR OriginalVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1846 UINT32 WorkingVariableMtrrCount
;
1847 VARIABLE_MTRR WorkingVariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1848 BOOLEAN VariableSettingModified
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1849 UINTN FreeVariableMtrrCount
;
1852 return RETURN_INVALID_PARAMETER
;
1855 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1856 if (((BaseAddress
& ~MtrrValidAddressMask
) != 0) || (Length
& ~MtrrValidAddressMask
) != 0) {
1857 return RETURN_UNSUPPORTED
;
1859 OriginalVariableMtrrCount
= 0;
1860 VariableSettings
= NULL
;
1862 ZeroMem (&WorkingFixedSettings
, sizeof (WorkingFixedSettings
));
1863 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1864 FixedSettingsValid
[Index
] = FALSE
;
1865 FixedSettingsModified
[Index
] = FALSE
;
1869 // Check if Fixed MTRR
1871 if (BaseAddress
< BASE_1MB
) {
1872 MsrIndex
= (UINT32
)-1;
1873 while ((BaseAddress
< BASE_1MB
) && (Length
!= 0)) {
1874 Status
= MtrrLibProgramFixedMtrr (Type
, &BaseAddress
, &Length
, &MsrIndex
, &ClearMask
, &OrMask
);
1875 if (RETURN_ERROR (Status
)) {
1878 if (MtrrSetting
!= NULL
) {
1879 MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] = (MtrrSetting
->Fixed
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1880 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.FE
= 1;
1882 if (!FixedSettingsValid
[MsrIndex
]) {
1883 WorkingFixedSettings
.Mtrr
[MsrIndex
] = AsmReadMsr64 (mMtrrLibFixedMtrrTable
[MsrIndex
].Msr
);
1884 FixedSettingsValid
[MsrIndex
] = TRUE
;
1886 NewValue
= (WorkingFixedSettings
.Mtrr
[MsrIndex
] & ~ClearMask
) | OrMask
;
1887 if (WorkingFixedSettings
.Mtrr
[MsrIndex
] != NewValue
) {
1888 WorkingFixedSettings
.Mtrr
[MsrIndex
] = NewValue
;
1889 FixedSettingsModified
[MsrIndex
] = TRUE
;
1896 // A Length of 0 can only make sense for fixed MTTR ranges.
1897 // Since we just handled the fixed MTRRs, we can skip the
1898 // variable MTRR section.
1905 // Read the default MTRR type
1907 DefaultType
= MtrrGetDefaultMemoryTypeWorker (MtrrSetting
);
1910 // Read all variable MTRRs and convert to Ranges.
1912 OriginalVariableMtrrCount
= GetVariableMtrrCountWorker ();
1913 if (MtrrSetting
== NULL
) {
1914 ZeroMem (&OriginalVariableSettings
, sizeof (OriginalVariableSettings
));
1915 MtrrGetVariableMtrrWorker (NULL
, OriginalVariableMtrrCount
, &OriginalVariableSettings
);
1916 VariableSettings
= &OriginalVariableSettings
;
1918 VariableSettings
= &MtrrSetting
->Variables
;
1920 MtrrGetMemoryAttributeInVariableMtrrWorker (VariableSettings
, OriginalVariableMtrrCount
, MtrrValidBitsMask
, MtrrValidAddressMask
, OriginalVariableMtrr
);
1922 Status
= MtrrLibGetMemoryTypes (
1923 DefaultType
, MtrrValidBitsMask
+ 1, OriginalVariableMtrr
, OriginalVariableMtrrCount
,
1924 Ranges
, 2 * OriginalVariableMtrrCount
+ 1, &RangeCount
1926 ASSERT (Status
== RETURN_SUCCESS
);
1928 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCountWorker ();
1929 ASSERT (RangeCount
<= 2 * FirmwareVariableMtrrCount
+ 1);
1932 // Force [0, 1M) to UC, so that it doesn't impact left subtraction algorithm.
1934 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, 0, SIZE_1MB
, CacheUncacheable
);
1935 ASSERT (Status
== RETURN_SUCCESS
);
1937 // Apply Type to [BaseAddress, BaseAddress + Length)
1939 Status
= MtrrLibSetMemoryType (Ranges
, 2 * FirmwareVariableMtrrCount
+ 2, &RangeCount
, BaseAddress
, Length
, Type
);
1940 if (RETURN_ERROR (Status
)) {
1944 Alignment0
= LShiftU64 (1, (UINTN
) HighBitSet64 (MtrrValidBitsMask
));
1945 WorkingVariableMtrrCount
= 0;
1946 ZeroMem (&WorkingVariableMtrr
, sizeof (WorkingVariableMtrr
));
1947 for (Index
= 0; Index
< RangeCount
; Index
++) {
1948 if (Ranges
[Index
].Type
!= DefaultType
) {
1950 // Maximum allowed MTRR count is (FirmwareVariableMtrrCount + 1)
1951 // Because potentially the range [0, 1MB) is not merged, but can be ignored because fixed MTRR covers that
1953 Status
= MtrrLibSetMemoryAttributeInVariableMtrr (
1955 WorkingVariableMtrr
, FirmwareVariableMtrrCount
+ 1, &WorkingVariableMtrrCount
,
1956 Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
,
1957 Ranges
[Index
].Type
, Alignment0
1959 if (RETURN_ERROR (Status
)) {
1966 // Remove the [0, 1MB) MTRR if it still exists (not merged with other range)
1968 if (WorkingVariableMtrr
[0].BaseAddress
== 0 && WorkingVariableMtrr
[0].Length
== SIZE_1MB
) {
1969 ASSERT (WorkingVariableMtrr
[0].Type
== CacheUncacheable
);
1970 WorkingVariableMtrrCount
--;
1971 CopyMem (&WorkingVariableMtrr
[0], &WorkingVariableMtrr
[1], WorkingVariableMtrrCount
* sizeof (VARIABLE_MTRR
));
1974 if (WorkingVariableMtrrCount
> FirmwareVariableMtrrCount
) {
1975 return RETURN_OUT_OF_RESOURCES
;
1978 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
1979 VariableSettingModified
[Index
] = FALSE
;
1981 if (!OriginalVariableMtrr
[Index
].Valid
) {
1984 for (WorkingIndex
= 0; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
1985 if (OriginalVariableMtrr
[Index
].BaseAddress
== WorkingVariableMtrr
[WorkingIndex
].BaseAddress
&&
1986 OriginalVariableMtrr
[Index
].Length
== WorkingVariableMtrr
[WorkingIndex
].Length
&&
1987 OriginalVariableMtrr
[Index
].Type
== WorkingVariableMtrr
[WorkingIndex
].Type
) {
1992 if (WorkingIndex
== WorkingVariableMtrrCount
) {
1994 // Remove the one from OriginalVariableMtrr which is not in WorkingVariableMtrr
1996 OriginalVariableMtrr
[Index
].Valid
= FALSE
;
1997 VariableSettingModified
[Index
] = TRUE
;
2000 // Remove the one from WorkingVariableMtrr which is also in OriginalVariableMtrr
2002 WorkingVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
2005 // The above two operations cause that valid MTRR only exists in either OriginalVariableMtrr or WorkingVariableMtrr.
2010 // Merge remaining MTRRs from WorkingVariableMtrr to OriginalVariableMtrr
2012 for (FreeVariableMtrrCount
= 0, WorkingIndex
= 0, Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2013 if (!OriginalVariableMtrr
[Index
].Valid
) {
2014 for (; WorkingIndex
< WorkingVariableMtrrCount
; WorkingIndex
++) {
2015 if (WorkingVariableMtrr
[WorkingIndex
].Valid
) {
2019 if (WorkingIndex
== WorkingVariableMtrrCount
) {
2020 FreeVariableMtrrCount
++;
2022 CopyMem (&OriginalVariableMtrr
[Index
], &WorkingVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2023 VariableSettingModified
[Index
] = TRUE
;
2028 ASSERT (OriginalVariableMtrrCount
- FreeVariableMtrrCount
<= FirmwareVariableMtrrCount
);
2031 // Move MTRRs after the FirmwareVariableMtrrCount position to beginning
2033 if (FirmwareVariableMtrrCount
< OriginalVariableMtrrCount
) {
2034 WorkingIndex
= FirmwareVariableMtrrCount
;
2035 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
2036 if (!OriginalVariableMtrr
[Index
].Valid
) {
2038 // Found an empty MTRR in WorkingIndex position
2040 for (; WorkingIndex
< OriginalVariableMtrrCount
; WorkingIndex
++) {
2041 if (OriginalVariableMtrr
[WorkingIndex
].Valid
) {
2046 if (WorkingIndex
!= OriginalVariableMtrrCount
) {
2047 CopyMem (&OriginalVariableMtrr
[Index
], &OriginalVariableMtrr
[WorkingIndex
], sizeof (VARIABLE_MTRR
));
2048 VariableSettingModified
[Index
] = TRUE
;
2049 VariableSettingModified
[WorkingIndex
] = TRUE
;
2050 OriginalVariableMtrr
[WorkingIndex
].Valid
= FALSE
;
2057 // Convert OriginalVariableMtrr to VariableSettings
2058 // NOTE: MTRR from FirmwareVariableMtrr to OriginalVariableMtrr need to update as well.
2060 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2061 if (VariableSettingModified
[Index
]) {
2062 if (OriginalVariableMtrr
[Index
].Valid
) {
2063 VariableSettings
->Mtrr
[Index
].Base
= (OriginalVariableMtrr
[Index
].BaseAddress
& MtrrValidAddressMask
) | (UINT8
) OriginalVariableMtrr
[Index
].Type
;
2064 VariableSettings
->Mtrr
[Index
].Mask
= ((~(OriginalVariableMtrr
[Index
].Length
- 1)) & MtrrValidAddressMask
) | BIT11
;
2066 VariableSettings
->Mtrr
[Index
].Base
= 0;
2067 VariableSettings
->Mtrr
[Index
].Mask
= 0;
2073 if (MtrrSetting
!= NULL
) {
2074 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*) &MtrrSetting
->MtrrDefType
)->Bits
.E
= 1;
2075 return RETURN_SUCCESS
;
2078 MtrrContextValid
= FALSE
;
2080 // Write fixed MTRRs that have been modified
2082 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2083 if (FixedSettingsModified
[Index
]) {
2084 if (!MtrrContextValid
) {
2085 MtrrLibPreMtrrChange (&MtrrContext
);
2086 MtrrContextValid
= TRUE
;
2089 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2090 WorkingFixedSettings
.Mtrr
[Index
]
2096 // Write variable MTRRs
2097 // When only fixed MTRRs were changed, below loop doesn't run
2098 // because OriginalVariableMtrrCount equals to 0.
2100 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2101 if (VariableSettingModified
[Index
]) {
2102 if (!MtrrContextValid
) {
2103 MtrrLibPreMtrrChange (&MtrrContext
);
2104 MtrrContextValid
= TRUE
;
2107 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2108 VariableSettings
->Mtrr
[Index
].Base
2111 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2112 VariableSettings
->Mtrr
[Index
].Mask
2116 if (MtrrContextValid
) {
2117 MtrrLibPostMtrrChange (&MtrrContext
);
2120 return RETURN_SUCCESS
;
2124 This function attempts to set the attributes for a memory range.
2126 @param[in] BaseAddress The physical address that is the start
2127 address of a memory range.
2128 @param[in] Length The size in bytes of the memory range.
2129 @param[in] Attributes The bit mask of attributes to set for the
2132 @retval RETURN_SUCCESS The attributes were set for the memory
2134 @retval RETURN_INVALID_PARAMETER Length is zero.
2135 @retval RETURN_UNSUPPORTED The processor does not support one or
2136 more bytes of the memory resource range
2137 specified by BaseAddress and Length.
2138 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
2139 for the memory resource range specified
2140 by BaseAddress and Length.
2141 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
2142 range specified by BaseAddress and Length
2144 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
2145 modify the attributes of the memory
2151 MtrrSetMemoryAttribute (
2152 IN PHYSICAL_ADDRESS BaseAddress
,
2154 IN MTRR_MEMORY_CACHE_TYPE Attribute
2157 RETURN_STATUS Status
;
2159 if (!IsMtrrSupported ()) {
2160 return RETURN_UNSUPPORTED
;
2163 Status
= MtrrSetMemoryAttributeWorker (NULL
, BaseAddress
, Length
, Attribute
);
2164 DEBUG ((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a: [%016lx, %016lx) - %r\n",
2165 mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2167 if (!RETURN_ERROR (Status
)) {
2168 MtrrDebugPrintAllMtrrsWorker (NULL
);
2174 This function attempts to set the attributes into MTRR setting buffer for a memory range.
2176 @param[in, out] MtrrSetting MTRR setting buffer to be set.
2177 @param[in] BaseAddress The physical address that is the start address
2179 @param[in] Length The size in bytes of the memory range.
2180 @param[in] Attribute The bit mask of attributes to set for the
2183 @retval RETURN_SUCCESS The attributes were set for the memory range.
2184 @retval RETURN_INVALID_PARAMETER Length is zero.
2185 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
2186 memory resource range specified by BaseAddress and Length.
2187 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
2188 range specified by BaseAddress and Length.
2189 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
2190 BaseAddress and Length cannot be modified.
2191 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
2192 the memory resource range.
2197 MtrrSetMemoryAttributeInMtrrSettings (
2198 IN OUT MTRR_SETTINGS
*MtrrSetting
,
2199 IN PHYSICAL_ADDRESS BaseAddress
,
2201 IN MTRR_MEMORY_CACHE_TYPE Attribute
2204 RETURN_STATUS Status
;
2205 Status
= MtrrSetMemoryAttributeWorker (MtrrSetting
, BaseAddress
, Length
, Attribute
);
2206 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttributeMtrrSettings(%p) %a: [%016lx, %016lx) - %r\n",
2207 MtrrSetting
, mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2209 if (!RETURN_ERROR (Status
)) {
2210 MtrrDebugPrintAllMtrrsWorker (MtrrSetting
);
2217 Worker function setting variable MTRRs
2219 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2223 MtrrSetVariableMtrrWorker (
2224 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2228 UINT32 VariableMtrrCount
;
2230 VariableMtrrCount
= GetVariableMtrrCountWorker ();
2231 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
2233 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
2235 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2236 VariableSettings
->Mtrr
[Index
].Base
2239 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2240 VariableSettings
->Mtrr
[Index
].Mask
2247 This function sets variable MTRRs
2249 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2251 @return The pointer of VariableSettings
2254 MTRR_VARIABLE_SETTINGS
*
2256 MtrrSetVariableMtrr (
2257 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2260 MTRR_CONTEXT MtrrContext
;
2262 if (!IsMtrrSupported ()) {
2263 return VariableSettings
;
2266 MtrrLibPreMtrrChange (&MtrrContext
);
2267 MtrrSetVariableMtrrWorker (VariableSettings
);
2268 MtrrLibPostMtrrChange (&MtrrContext
);
2269 MtrrDebugPrintAllMtrrs ();
2271 return VariableSettings
;
2275 Worker function setting fixed MTRRs
2277 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2281 MtrrSetFixedMtrrWorker (
2282 IN MTRR_FIXED_SETTINGS
*FixedSettings
2287 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2289 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2290 FixedSettings
->Mtrr
[Index
]
2297 This function sets fixed MTRRs
2299 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2301 @retval The pointer of FixedSettings
2304 MTRR_FIXED_SETTINGS
*
2307 IN MTRR_FIXED_SETTINGS
*FixedSettings
2310 MTRR_CONTEXT MtrrContext
;
2312 if (!IsMtrrSupported ()) {
2313 return FixedSettings
;
2316 MtrrLibPreMtrrChange (&MtrrContext
);
2317 MtrrSetFixedMtrrWorker (FixedSettings
);
2318 MtrrLibPostMtrrChange (&MtrrContext
);
2319 MtrrDebugPrintAllMtrrs ();
2321 return FixedSettings
;
2326 This function gets the content in all MTRRs (variable and fixed)
2328 @param[out] MtrrSetting A buffer to hold all MTRRs content.
2330 @retval the pointer of MtrrSetting
2336 OUT MTRR_SETTINGS
*MtrrSetting
2339 if (!IsMtrrSupported ()) {
2346 MtrrGetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2349 // Get variable MTRRs
2351 MtrrGetVariableMtrrWorker (
2353 GetVariableMtrrCountWorker (),
2354 &MtrrSetting
->Variables
2358 // Get MTRR_DEF_TYPE value
2360 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
2367 This function sets all MTRRs (variable and fixed)
2369 @param[in] MtrrSetting A buffer holding all MTRRs content.
2371 @retval The pointer of MtrrSetting
2377 IN MTRR_SETTINGS
*MtrrSetting
2380 MTRR_CONTEXT MtrrContext
;
2382 if (!IsMtrrSupported ()) {
2386 MtrrLibPreMtrrChange (&MtrrContext
);
2391 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2394 // Set variable MTRRs
2396 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
2399 // Set MTRR_DEF_TYPE value
2401 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
2403 MtrrLibPostMtrrChangeEnableCache (&MtrrContext
);
2410 Checks if MTRR is supported.
2412 @retval TRUE MTRR is supported.
2413 @retval FALSE MTRR is not supported.
2422 CPUID_VERSION_INFO_EDX Edx
;
2423 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
2426 // Check CPUID(1).EDX[12] for MTRR capability
2428 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
2429 if (Edx
.Bits
.MTRR
== 0) {
2434 // Check number of variable MTRRs and fixed MTRRs existence.
2435 // If number of variable MTRRs is zero, or fixed MTRRs do not
2436 // exist, return false.
2438 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
2439 if ((MtrrCap
.Bits
.VCNT
== 0) || (MtrrCap
.Bits
.FIX
== 0)) {