4 Copyright (c) 2008 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/MtrrLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/CpuLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
24 // This table defines the offset, base and length of the fixed MTRRs
26 CONST FIXED_MTRR mMtrrLibFixedMtrrTable
[] = {
28 MTRR_LIB_IA32_MTRR_FIX64K_00000
,
33 MTRR_LIB_IA32_MTRR_FIX16K_80000
,
38 MTRR_LIB_IA32_MTRR_FIX16K_A0000
,
43 MTRR_LIB_IA32_MTRR_FIX4K_C0000
,
48 MTRR_LIB_IA32_MTRR_FIX4K_C8000
,
53 MTRR_LIB_IA32_MTRR_FIX4K_D0000
,
58 MTRR_LIB_IA32_MTRR_FIX4K_D8000
,
63 MTRR_LIB_IA32_MTRR_FIX4K_E0000
,
68 MTRR_LIB_IA32_MTRR_FIX4K_E8000
,
73 MTRR_LIB_IA32_MTRR_FIX4K_F0000
,
78 MTRR_LIB_IA32_MTRR_FIX4K_F8000
,
85 // Lookup table used to print MTRRs
87 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mMtrrMemoryCacheTypeShortName
[] = {
88 "UC", // CacheUncacheable
89 "WC", // CacheWriteCombining
92 "WT", // CacheWriteThrough
93 "WP", // CacheWriteProtected
94 "WB", // CacheWriteBack
99 Returns the variable MTRR count for the CPU.
101 @return Variable MTRR count
106 GetVariableMtrrCount (
110 UINT32 VariableMtrrCount
;
112 if (!IsMtrrSupported ()) {
116 VariableMtrrCount
= (UINT32
)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP
) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK
);
117 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
119 return VariableMtrrCount
;
123 Returns the firmware usable variable MTRR count for the CPU.
125 @return Firmware usable variable MTRR count
130 GetFirmwareVariableMtrrCount (
134 UINT32 VariableMtrrCount
;
136 VariableMtrrCount
= GetVariableMtrrCount ();
137 if (VariableMtrrCount
< RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER
) {
141 return VariableMtrrCount
- RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER
;
145 Returns the default MTRR cache type for the system.
147 @return MTRR default type
151 GetMtrrDefaultMemoryType (
155 return (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
) & 0xff);
160 Preparation before programming MTRR.
162 This function will do some preparation for programming MTRRs:
163 disable cache, invalid cache and disable MTRR caching functionality
165 @return CR4 value before changing.
176 // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29)
181 // Save original CR4 value and clear PGE flag (Bit 7)
183 Value
= AsmReadCr4 ();
184 AsmWriteCr4 (Value
& (~BIT7
));
194 AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, 10, 11, 0);
197 // Return original CR4 value
204 Cleaning up after programming MTRRs.
206 This function will do some clean up after programming MTRRs:
207 enable MTRR caching functionality, and enable cache
209 @param Cr4 CR4 value to restore
220 AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, 10, 11, 3);
228 // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29)
233 // Restore original CR4 value
240 Programs fixed MTRRs registers.
242 @param MemoryCacheType The memory type to set.
243 @param Base The base address of memory range.
244 @param Length The length of memory range.
246 @retval RETURN_SUCCESS The cache type was updated successfully
247 @retval RETURN_UNSUPPORTED The requested range or cache type was invalid
253 IN UINT64 MemoryCacheType
,
255 IN OUT UINT64
*Length
268 for (MsrNum
= 0; MsrNum
< MTRR_NUMBER_OF_FIXED_MTRR
; MsrNum
++) {
269 if ((*Base
>= mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
) &&
272 mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
+
273 (8 * mMtrrLibFixedMtrrTable
[MsrNum
].Length
)
281 if (MsrNum
== MTRR_NUMBER_OF_FIXED_MTRR
) {
282 return RETURN_UNSUPPORTED
;
286 // We found the fixed MTRR to be programmed
288 for (ByteShift
= 0; ByteShift
< 8; ByteShift
++) {
291 mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
+
292 (ByteShift
* mMtrrLibFixedMtrrTable
[MsrNum
].Length
)
299 if (ByteShift
== 8) {
300 return RETURN_UNSUPPORTED
;
305 ((ByteShift
< 8) && (*Length
>= mMtrrLibFixedMtrrTable
[MsrNum
].Length
));
308 OrMask
|= LShiftU64 ((UINT64
) MemoryCacheType
, (UINT32
) (ByteShift
* 8));
309 ClearMask
|= LShiftU64 ((UINT64
) 0xFF, (UINT32
) (ByteShift
* 8));
310 *Length
-= mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
311 *Base
+= mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
314 if (ByteShift
< 8 && (*Length
!= 0)) {
315 return RETURN_UNSUPPORTED
;
319 (AsmReadMsr64 (mMtrrLibFixedMtrrTable
[MsrNum
].Msr
) & ~ClearMask
) | OrMask
;
320 AsmWriteMsr64 (mMtrrLibFixedMtrrTable
[MsrNum
].Msr
, TempQword
);
321 return RETURN_SUCCESS
;
326 Get the attribute of variable MTRRs.
328 This function shadows the content of variable MTRRs into an
329 internal array: VariableMtrr.
331 @param MtrrValidBitsMask The mask for the valid bit of the MTRR
332 @param MtrrValidAddressMask The valid address mask for MTRR
333 @param VariableMtrr The array to shadow variable MTRRs content
335 @return The return value of this paramter indicates the
336 number of MTRRs which has been used.
341 MtrrGetMemoryAttributeInVariableMtrr (
342 IN UINT64 MtrrValidBitsMask
,
343 IN UINT64 MtrrValidAddressMask
,
344 OUT VARIABLE_MTRR
*VariableMtrr
350 UINT32 FirmwareVariableMtrrCount
;
351 UINT32 VariableMtrrEnd
;
353 if (!IsMtrrSupported ()) {
357 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
358 VariableMtrrEnd
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (2 * GetVariableMtrrCount ()) - 1;
360 ZeroMem (VariableMtrr
, sizeof (VARIABLE_MTRR
) * MTRR_NUMBER_OF_VARIABLE_MTRR
);
363 for (MsrNum
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
, Index
= 0;
365 (MsrNum
< VariableMtrrEnd
) &&
366 (Index
< FirmwareVariableMtrrCount
)
370 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) != 0) {
371 VariableMtrr
[Index
].Msr
= MsrNum
;
372 VariableMtrr
[Index
].BaseAddress
= (AsmReadMsr64 (MsrNum
) &
373 MtrrValidAddressMask
);
374 VariableMtrr
[Index
].Length
= ((~(AsmReadMsr64 (MsrNum
+ 1) &
375 MtrrValidAddressMask
)
379 VariableMtrr
[Index
].Type
= (AsmReadMsr64 (MsrNum
) & 0x0ff);
380 VariableMtrr
[Index
].Valid
= TRUE
;
381 VariableMtrr
[Index
].Used
= TRUE
;
382 UsedMtrr
= UsedMtrr
+ 1;
391 Checks overlap between given memory range and MTRRs.
393 @param Start The start address of memory range.
394 @param End The end address of memory range.
395 @param VariableMtrr The array to shadow variable MTRRs content
397 @retval TRUE Overlap exists.
398 @retval FALSE No overlap.
402 CheckMemoryAttributeOverlap (
403 IN PHYSICAL_ADDRESS Start
,
404 IN PHYSICAL_ADDRESS End
,
405 IN VARIABLE_MTRR
*VariableMtrr
410 for (Index
= 0; Index
< 6; Index
++) {
412 VariableMtrr
[Index
].Valid
&&
414 (Start
> (VariableMtrr
[Index
].BaseAddress
+
415 VariableMtrr
[Index
].Length
- 1)
417 (End
< VariableMtrr
[Index
].BaseAddress
)
429 Marks a variable MTRR as non-valid.
431 @param Index The index of the array VariableMtrr to be invalidated
432 @param VariableMtrr The array to shadow variable MTRRs content
433 @param UsedMtrr The number of MTRRs which has already been used
437 InvalidateShadowMtrr (
439 IN VARIABLE_MTRR
*VariableMtrr
,
443 VariableMtrr
[Index
].Valid
= FALSE
;
444 *UsedMtrr
= *UsedMtrr
- 1;
449 Combine memory attributes.
451 If overlap exists between given memory range and MTRRs, try to combine them.
453 @param Attributes The memory type to set.
454 @param Base The base address of memory range.
455 @param Length The length of memory range.
456 @param VariableMtrr The array to shadow variable MTRRs content
457 @param UsedMtrr The number of MTRRs which has already been used
458 @param OverwriteExistingMtrr Returns whether an existing MTRR was used
460 @retval EFI_SUCCESS Memory region successfully combined.
461 @retval EFI_ACCESS_DENIED Memory region cannot be combined.
465 CombineMemoryAttribute (
466 IN UINT64 Attributes
,
468 IN OUT UINT64
*Length
,
469 IN VARIABLE_MTRR
*VariableMtrr
,
470 IN OUT UINT32
*UsedMtrr
,
471 OUT BOOLEAN
*OverwriteExistingMtrr
479 UINT32 FirmwareVariableMtrrCount
;
481 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
483 *OverwriteExistingMtrr
= FALSE
;
484 EndAddress
= *Base
+*Length
- 1;
486 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
488 MtrrEnd
= VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
- 1;
490 !VariableMtrr
[Index
].Valid
||
493 (EndAddress
< VariableMtrr
[Index
].BaseAddress
)
500 // Combine same attribute MTRR range
502 if (Attributes
== VariableMtrr
[Index
].Type
) {
504 // if the Mtrr range contain the request range, return RETURN_SUCCESS
506 if (VariableMtrr
[Index
].BaseAddress
<= *Base
&& MtrrEnd
>= EndAddress
) {
508 return RETURN_SUCCESS
;
511 // invalid this MTRR, and program the combine range
514 (*Base
) < VariableMtrr
[Index
].BaseAddress
?
516 VariableMtrr
[Index
].BaseAddress
;
517 CombineEnd
= EndAddress
> MtrrEnd
? EndAddress
: MtrrEnd
;
520 // Record the MTRR usage status in VariableMtrr array.
522 InvalidateShadowMtrr (Index
, VariableMtrr
, UsedMtrr
);
523 *Base
= CombineStart
;
524 *Length
= CombineEnd
- CombineStart
+ 1;
525 EndAddress
= CombineEnd
;
526 *OverwriteExistingMtrr
= TRUE
;
530 // The cache type is different, but the range is convered by one MTRR
532 if (VariableMtrr
[Index
].BaseAddress
== *Base
&& MtrrEnd
== EndAddress
) {
533 InvalidateShadowMtrr (Index
, VariableMtrr
, UsedMtrr
);
539 if ((Attributes
== MTRR_CACHE_WRITE_THROUGH
&&
540 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_BACK
) ||
541 (Attributes
== MTRR_CACHE_WRITE_BACK
&&
542 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_THROUGH
) ||
543 (Attributes
== MTRR_CACHE_UNCACHEABLE
) ||
544 (VariableMtrr
[Index
].Type
== MTRR_CACHE_UNCACHEABLE
)
546 *OverwriteExistingMtrr
= TRUE
;
550 // Other type memory overlap is invalid
552 return RETURN_ACCESS_DENIED
;
555 return RETURN_SUCCESS
;
560 Calculate the maximum value which is a power of 2, but less the MemoryLength.
562 @param MemoryLength The number to pass in.
563 @return The maximum value which is align to power of 2 and less the MemoryLength
568 IN UINT64 MemoryLength
573 if (RShiftU64 (MemoryLength
, 32) != 0) {
575 (UINT64
) GetPowerOfTwo32 (
576 (UINT32
) RShiftU64 (MemoryLength
, 32)
581 Result
= (UINT64
) GetPowerOfTwo32 ((UINT32
) MemoryLength
);
589 Check the direction to program variable MTRRs.
591 This function determines which direction of programming the variable
592 MTRRs will use fewer MTRRs.
594 @param Input Length of Memory to program MTRR
595 @param MtrrNumber Pointer to the number of necessary MTRRs
597 @retval TRUE Positive direction is better.
598 FALSE Negtive direction is better.
616 TempQword
-= Power2MaxMemory (TempQword
);
618 } while (TempQword
!= 0);
620 TempQword
= Power2MaxMemory (LShiftU64 (Input
, 1)) - Input
;
623 TempQword
-= Power2MaxMemory (TempQword
);
625 } while (TempQword
!= 0);
627 if (Positive
<= Subtractive
) {
628 *MtrrNumber
= Positive
;
631 *MtrrNumber
= Subtractive
;
637 Invalid variable MTRRs according to the value in the shadow array.
639 This function programs MTRRs according to the values specified
642 @param VariableMtrr The array to shadow variable MTRRs content
647 IN VARIABLE_MTRR
*VariableMtrr
652 UINTN VariableMtrrCount
;
654 Cr4
= PreMtrrChange ();
656 VariableMtrrCount
= GetVariableMtrrCount ();
657 while (Index
< VariableMtrrCount
) {
658 if (!VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Used
) {
659 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
, 0);
660 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
+ 1, 0);
661 VariableMtrr
[Index
].Used
= FALSE
;
665 PostMtrrChange (Cr4
);
670 Programs variable MTRRs
672 This function programs variable MTRRs
674 @param MtrrNumber Index of MTRR to program.
675 @param BaseAddress Base address of memory region.
676 @param Length Length of memory region.
677 @param MemoryCacheType Memory type to set.
678 @param MtrrValidAddressMask The valid address mask for MTRR
682 ProgramVariableMtrr (
684 IN PHYSICAL_ADDRESS BaseAddress
,
686 IN UINT64 MemoryCacheType
,
687 IN UINT64 MtrrValidAddressMask
693 Cr4
= PreMtrrChange ();
696 // MTRR Physical Base
698 TempQword
= (BaseAddress
& MtrrValidAddressMask
) | MemoryCacheType
;
699 AsmWriteMsr64 ((UINT32
) MtrrNumber
, TempQword
);
702 // MTRR Physical Mask
704 TempQword
= ~(Length
- 1);
706 (UINT32
) (MtrrNumber
+ 1),
707 (TempQword
& MtrrValidAddressMask
) | MTRR_LIB_CACHE_MTRR_ENABLED
710 PostMtrrChange (Cr4
);
715 Convert the Memory attibute value to MTRR_MEMORY_CACHE_TYPE.
717 @param MtrrType MTRR memory type
719 @return The enum item in MTRR_MEMORY_CACHE_TYPE
722 MTRR_MEMORY_CACHE_TYPE
723 GetMemoryCacheTypeFromMtrrType (
728 case MTRR_CACHE_UNCACHEABLE
:
729 return CacheUncacheable
;
730 case MTRR_CACHE_WRITE_COMBINING
:
731 return CacheWriteCombining
;
732 case MTRR_CACHE_WRITE_THROUGH
:
733 return CacheWriteThrough
;
734 case MTRR_CACHE_WRITE_PROTECTED
:
735 return CacheWriteProtected
;
736 case MTRR_CACHE_WRITE_BACK
:
737 return CacheWriteBack
;
740 // MtrrType is MTRR_CACHE_INVALID_TYPE, that means
741 // no mtrr covers the range
743 return CacheUncacheable
;
748 Initializes the valid bits mask and valid address mask for MTRRs.
750 This function initializes the valid bits mask and valid address mask for MTRRs.
752 @param MtrrValidBitsMask The mask for the valid bit of the MTRR
753 @param MtrrValidAddressMask The valid address mask for the MTRR
757 MtrrLibInitializeMtrrMask (
758 OUT UINT64
*MtrrValidBitsMask
,
759 OUT UINT64
*MtrrValidAddressMask
763 UINT8 PhysicalAddressBits
;
765 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
767 if (RegEax
>= 0x80000008) {
768 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
770 PhysicalAddressBits
= (UINT8
) RegEax
;
772 *MtrrValidBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
773 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
775 *MtrrValidBitsMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
776 *MtrrValidAddressMask
= 0xFFFFFFFF;
782 Determing the real attribute of a memory range.
784 This function is to arbitrate the real attribute of the memory when
785 there are 2 MTRR covers the same memory range. For further details,
786 please refer the IA32 Software Developer's Manual, Volume 3,
789 @param MtrrType1 the first kind of Memory type
790 @param MtrrType2 the second kind of memory type
801 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
803 case MTRR_CACHE_UNCACHEABLE
:
804 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
806 case MTRR_CACHE_WRITE_COMBINING
:
808 MtrrType2
==MTRR_CACHE_WRITE_COMBINING
||
809 MtrrType2
==MTRR_CACHE_UNCACHEABLE
811 MtrrType
= MtrrType2
;
814 case MTRR_CACHE_WRITE_THROUGH
:
816 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
817 MtrrType2
==MTRR_CACHE_WRITE_BACK
819 MtrrType
= MTRR_CACHE_WRITE_THROUGH
;
820 } else if(MtrrType2
==MTRR_CACHE_UNCACHEABLE
) {
821 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
824 case MTRR_CACHE_WRITE_PROTECTED
:
825 if (MtrrType2
== MTRR_CACHE_WRITE_PROTECTED
||
826 MtrrType2
== MTRR_CACHE_UNCACHEABLE
) {
827 MtrrType
= MtrrType2
;
830 case MTRR_CACHE_WRITE_BACK
:
832 MtrrType2
== MTRR_CACHE_UNCACHEABLE
||
833 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
834 MtrrType2
== MTRR_CACHE_WRITE_BACK
836 MtrrType
= MtrrType2
;
839 case MTRR_CACHE_INVALID_TYPE
:
840 MtrrType
= MtrrType2
;
846 if (MtrrType2
== MTRR_CACHE_INVALID_TYPE
) {
847 MtrrType
= MtrrType1
;
854 This function attempts to set the attributes for a memory range.
856 @param BaseAddress The physical address that is the start
857 address of a memory region.
858 @param Length The size in bytes of the memory region.
859 @param Attributes The bit mask of attributes to set for the
862 @retval RETURN_SUCCESS The attributes were set for the memory
864 @retval RETURN_INVALID_PARAMETER Length is zero.
865 @retval RETURN_UNSUPPORTED The processor does not support one or
866 more bytes of the memory resource range
867 specified by BaseAddress and Length.
868 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
869 for the memory resource range specified
870 by BaseAddress and Length.
871 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
872 range specified by BaseAddress and Length
874 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
875 modify the attributes of the memory
881 MtrrSetMemoryAttribute (
882 IN PHYSICAL_ADDRESS BaseAddress
,
884 IN MTRR_MEMORY_CACHE_TYPE Attribute
888 RETURN_STATUS Status
;
895 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
897 UINT64 MtrrValidBitsMask
;
898 UINT64 MtrrValidAddressMask
;
900 BOOLEAN OverwriteExistingMtrr
;
901 UINT32 FirmwareVariableMtrrCount
;
902 UINT32 VariableMtrrEnd
;
904 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, Length
));
906 if (!IsMtrrSupported ()) {
907 Status
= RETURN_UNSUPPORTED
;
911 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
912 VariableMtrrEnd
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (2 * GetVariableMtrrCount ()) - 1;
914 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
917 MemoryType
= (UINT64
)Attribute
;
918 OverwriteExistingMtrr
= FALSE
;
921 // Check for an invalid parameter
924 Status
= RETURN_INVALID_PARAMETER
;
929 (BaseAddress
& ~MtrrValidAddressMask
) != 0 ||
930 (Length
& ~MtrrValidAddressMask
) != 0
932 Status
= RETURN_UNSUPPORTED
;
937 // Check if Fixed MTRR
939 Status
= RETURN_SUCCESS
;
940 while ((BaseAddress
< BASE_1MB
) && (Length
> 0) && Status
== RETURN_SUCCESS
) {
941 Cr4
= PreMtrrChange ();
942 Status
= ProgramFixedMtrr (MemoryType
, &BaseAddress
, &Length
);
943 PostMtrrChange (Cr4
);
944 if (RETURN_ERROR (Status
)) {
951 // A Length of 0 can only make sense for fixed MTTR ranges.
952 // Since we just handled the fixed MTRRs, we can skip the
953 // variable MTRR section.
959 // Since memory ranges below 1MB will be overridden by the fixed MTRRs,
960 // we can set the base to 0 to save variable MTRRs.
962 if (BaseAddress
== BASE_1MB
) {
968 // Check memory base address alignment
970 DivU64x64Remainder (BaseAddress
, Power2MaxMemory (LShiftU64 (Length
, 1)), &Remainder
);
971 if (Remainder
!= 0) {
972 DivU64x64Remainder (BaseAddress
, Power2MaxMemory (Length
), &Remainder
);
973 if (Remainder
!= 0) {
974 Status
= RETURN_UNSUPPORTED
;
982 UsedMtrr
= MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask
, MtrrValidAddressMask
, VariableMtrr
);
983 OverLap
= CheckMemoryAttributeOverlap (BaseAddress
, BaseAddress
+ Length
- 1, VariableMtrr
);
985 Status
= CombineMemoryAttribute (MemoryType
, &BaseAddress
, &Length
, VariableMtrr
, &UsedMtrr
, &OverwriteExistingMtrr
);
986 if (RETURN_ERROR (Status
)) {
992 // Combined successfully
994 Status
= RETURN_SUCCESS
;
1000 // Program Variable MTRRs
1002 // Avoid hardcode here and read data dynamically
1004 if (UsedMtrr
>= FirmwareVariableMtrrCount
) {
1005 Status
= RETURN_OUT_OF_RESOURCES
;
1010 // The memory type is the same with the type specified by
1011 // MTRR_LIB_IA32_MTRR_DEF_TYPE.
1013 if ((!OverwriteExistingMtrr
) && (Attribute
== GetMtrrDefaultMemoryType ())) {
1015 // Invalidate the now-unused MTRRs
1017 InvalidateMtrr(VariableMtrr
);
1024 if (TempQword
== Power2MaxMemory (TempQword
)) {
1026 // Invalidate the now-unused MTRRs
1028 InvalidateMtrr(VariableMtrr
);
1031 // Find first unused MTRR
1033 for (MsrNum
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
;
1034 MsrNum
< VariableMtrrEnd
;
1037 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1042 ProgramVariableMtrr (
1047 MtrrValidAddressMask
1051 Positive
= GetDirection (TempQword
, &MtrrNumber
);
1053 if ((UsedMtrr
+ MtrrNumber
) > FirmwareVariableMtrrCount
) {
1054 Status
= RETURN_OUT_OF_RESOURCES
;
1059 // Invalidate the now-unused MTRRs
1061 InvalidateMtrr(VariableMtrr
);
1064 // Find first unused MTRR
1066 for (MsrNum
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
;
1067 MsrNum
< VariableMtrrEnd
;
1070 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1076 Length
= Power2MaxMemory (LShiftU64 (TempQword
, 1));
1077 ProgramVariableMtrr (
1082 MtrrValidAddressMask
1084 BaseAddress
+= Length
;
1085 TempQword
= Length
- TempQword
;
1086 MemoryType
= MTRR_CACHE_UNCACHEABLE
;
1093 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1094 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1099 Length
= Power2MaxMemory (TempQword
);
1101 BaseAddress
-= Length
;
1104 ProgramVariableMtrr (
1109 MtrrValidAddressMask
1113 BaseAddress
+= Length
;
1115 TempQword
-= Length
;
1117 } while (TempQword
> 0);
1121 DEBUG((DEBUG_CACHE
, " Status = %r\n", Status
));
1122 if (!RETURN_ERROR (Status
)) {
1123 MtrrDebugPrintAllMtrrs ();
1131 This function will get the memory cache type of the specific address.
1133 This function is mainly for debug purpose.
1135 @param Address The specific address
1137 @return Memory cache type of the sepcific address
1140 MTRR_MEMORY_CACHE_TYPE
1142 MtrrGetMemoryAttribute (
1143 IN PHYSICAL_ADDRESS Address
1150 UINT64 TempMtrrType
;
1151 MTRR_MEMORY_CACHE_TYPE CacheType
;
1152 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1153 UINT64 MtrrValidBitsMask
;
1154 UINT64 MtrrValidAddressMask
;
1155 UINTN VariableMtrrCount
;
1157 if (!IsMtrrSupported ()) {
1158 return CacheUncacheable
;
1162 // Check if MTRR is enabled, if not, return UC as attribute
1164 TempQword
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1165 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
1167 if ((TempQword
& MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1168 return CacheUncacheable
;
1172 // If address is less than 1M, then try to go through the fixed MTRR
1174 if (Address
< BASE_1MB
) {
1175 if ((TempQword
& MTRR_LIB_CACHE_FIXED_MTRR_ENABLED
) != 0) {
1177 // Go through the fixed MTRR
1179 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1180 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1182 mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1183 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)
1187 ((UINTN
)Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1188 mMtrrLibFixedMtrrTable
[Index
].Length
;
1189 TempQword
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1190 MtrrType
= RShiftU64 (TempQword
, SubIndex
* 8) & 0xFF;
1191 return GetMemoryCacheTypeFromMtrrType (MtrrType
);
1196 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1197 MtrrGetMemoryAttributeInVariableMtrr(
1199 MtrrValidAddressMask
,
1204 // Go through the variable MTRR
1206 VariableMtrrCount
= GetVariableMtrrCount ();
1207 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1209 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1210 if (VariableMtrr
[Index
].Valid
) {
1211 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1212 Address
< VariableMtrr
[Index
].BaseAddress
+VariableMtrr
[Index
].Length
) {
1213 TempMtrrType
= VariableMtrr
[Index
].Type
;
1214 MtrrType
= MtrrPrecedence (MtrrType
, TempMtrrType
);
1218 CacheType
= GetMemoryCacheTypeFromMtrrType (MtrrType
);
1225 This function will get the raw value in variable MTRRs
1227 @param VariableSettings A buffer to hold variable MTRRs content.
1229 @return The VariableSettings input pointer
1232 MTRR_VARIABLE_SETTINGS
*
1234 MtrrGetVariableMtrr (
1235 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
1239 UINT32 VariableMtrrCount
;
1241 if (!IsMtrrSupported ()) {
1242 return VariableSettings
;
1245 VariableMtrrCount
= GetVariableMtrrCount ();
1246 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1248 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1249 VariableSettings
->Mtrr
[Index
].Base
=
1250 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1));
1251 VariableSettings
->Mtrr
[Index
].Mask
=
1252 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1);
1255 return VariableSettings
;
1260 Worker function setting variable MTRRs
1262 @param VariableSettings A buffer to hold variable MTRRs content.
1266 MtrrSetVariableMtrrWorker (
1267 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1271 UINT32 VariableMtrrCount
;
1273 VariableMtrrCount
= GetVariableMtrrCount ();
1274 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1276 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1278 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1),
1279 VariableSettings
->Mtrr
[Index
].Base
1282 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1,
1283 VariableSettings
->Mtrr
[Index
].Mask
1290 This function sets variable MTRRs
1292 @param VariableSettings A buffer to hold variable MTRRs content.
1294 @return The pointer of VariableSettings
1297 MTRR_VARIABLE_SETTINGS
*
1299 MtrrSetVariableMtrr (
1300 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1305 if (!IsMtrrSupported ()) {
1306 return VariableSettings
;
1309 Cr4
= PreMtrrChange ();
1310 MtrrSetVariableMtrrWorker (VariableSettings
);
1311 PostMtrrChange (Cr4
);
1312 return VariableSettings
;
1317 This function gets the content in fixed MTRRs
1319 @param FixedSettings A buffer to hold fixed Mtrrs content.
1321 @retval The pointer of FixedSettings
1324 MTRR_FIXED_SETTINGS
*
1327 OUT MTRR_FIXED_SETTINGS
*FixedSettings
1332 if (!IsMtrrSupported ()) {
1333 return FixedSettings
;
1336 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1337 FixedSettings
->Mtrr
[Index
] =
1338 AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1341 return FixedSettings
;
1345 Worker function setting fixed MTRRs
1347 @param FixedSettings A buffer to hold fixed Mtrrs content.
1351 MtrrSetFixedMtrrWorker (
1352 IN MTRR_FIXED_SETTINGS
*FixedSettings
1357 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1359 mMtrrLibFixedMtrrTable
[Index
].Msr
,
1360 FixedSettings
->Mtrr
[Index
]
1367 This function sets fixed MTRRs
1369 @param FixedSettings A buffer to hold fixed Mtrrs content.
1371 @retval The pointer of FixedSettings
1374 MTRR_FIXED_SETTINGS
*
1377 IN MTRR_FIXED_SETTINGS
*FixedSettings
1382 if (!IsMtrrSupported ()) {
1383 return FixedSettings
;
1386 Cr4
= PreMtrrChange ();
1387 MtrrSetFixedMtrrWorker (FixedSettings
);
1388 PostMtrrChange (Cr4
);
1390 return FixedSettings
;
1395 This function gets the content in all MTRRs (variable and fixed)
1397 @param MtrrSetting A buffer to hold all Mtrrs content.
1399 @retval the pointer of MtrrSetting
1405 OUT MTRR_SETTINGS
*MtrrSetting
1408 if (!IsMtrrSupported ()) {
1415 MtrrGetFixedMtrr (&MtrrSetting
->Fixed
);
1418 // Get variable MTRRs
1420 MtrrGetVariableMtrr (&MtrrSetting
->Variables
);
1423 // Get MTRR_DEF_TYPE value
1425 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1432 This function sets all MTRRs (variable and fixed)
1434 @param MtrrSetting A buffer holding all MTRRs content.
1436 @retval The pointer of MtrrSetting
1442 IN MTRR_SETTINGS
*MtrrSetting
1447 if (!IsMtrrSupported ()) {
1451 Cr4
= PreMtrrChange ();
1456 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
1459 // Set variable MTRRs
1461 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
1464 // Set MTRR_DEF_TYPE value
1466 AsmWriteMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
1468 PostMtrrChange (Cr4
);
1474 This function prints all MTRRs for debugging.
1478 MtrrDebugPrintAllMtrrs (
1484 MTRR_SETTINGS MtrrSettings
;
1487 UINTN VariableMtrrCount
;
1495 UINT64 NoRangeLimit
;
1498 UINTN PreviousMemoryType
;
1501 if (!IsMtrrSupported ()) {
1505 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1506 DEBUG((DEBUG_CACHE
, "=============\n"));
1508 MtrrGetAllMtrrs (&MtrrSettings
);
1509 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", MtrrSettings
.MtrrDefType
));
1510 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1511 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, MtrrSettings
.Fixed
.Mtrr
[Index
]));
1514 VariableMtrrCount
= GetVariableMtrrCount ();
1515 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1516 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1518 MtrrSettings
.Variables
.Mtrr
[Index
].Base
,
1519 MtrrSettings
.Variables
.Mtrr
[Index
].Mask
1522 DEBUG((DEBUG_CACHE
, "\n"));
1523 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1524 DEBUG((DEBUG_CACHE
, "====================================\n"));
1527 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1528 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1529 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1530 for (Index1
= 0; Index1
< 8; Index1
++) {
1531 MemoryType
= (UINTN
)RShiftU64 (MtrrSettings
.Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff;
1532 if (MemoryType
> CacheWriteBack
) {
1533 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1535 if (MemoryType
!= PreviousMemoryType
) {
1536 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1537 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1539 PreviousMemoryType
= MemoryType
;
1540 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1542 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1545 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1547 VariableMtrrCount
= GetVariableMtrrCount ();
1550 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1552 MemoryType
= MtrrGetMemoryAttribute (Base
);
1553 if (MemoryType
> CacheWriteBack
) {
1554 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1557 if (MemoryType
!= PreviousMemoryType
) {
1558 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1559 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1561 PreviousMemoryType
= MemoryType
;
1562 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1565 RangeBase
= BASE_1MB
;
1566 NoRangeBase
= BASE_1MB
;
1568 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1569 if (RegEax
>= 0x80000008) {
1570 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1571 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1574 NoRangeLimit
= Limit
;
1576 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1577 if ((MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1579 // If mask is not valid, then do not display range
1583 MtrrBase
= (MtrrSettings
.Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1584 MtrrLimit
= MtrrBase
+ ((~(MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1586 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1590 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1591 RangeBase
= MtrrBase
;
1593 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1594 RangeBase
= MtrrLimit
+ 1;
1596 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1597 RangeLimit
= MtrrBase
- 1;
1599 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1600 RangeLimit
= MtrrLimit
;
1603 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1604 NoRangeBase
= MtrrLimit
+ 1;
1606 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1607 NoRangeLimit
= MtrrBase
- 1;
1612 Base
= RangeLimit
+ 1;
1614 Base
= NoRangeLimit
+ 1;
1617 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1623 Checks if MTRR is supported.
1625 @retval TRUE MTRR is supported.
1626 @retval FALSE MTRR is not supported.
1639 // Check CPUID(1).EDX[12] for MTRR capability
1641 AsmCpuid (1, NULL
, NULL
, NULL
, &RegEdx
);
1642 if (BitFieldRead32 (RegEdx
, 12, 12) == 0) {
1647 // Check IA32_MTRRCAP.[0..7] for number of variable MTRRs and IA32_MTRRCAP[8] for
1648 // fixed MTRRs existence. If number of variable MTRRs is zero, or fixed MTRRs do not
1649 // exist, return false.
1651 MtrrCap
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP
);
1652 if ((BitFieldRead64 (MtrrCap
, 0, 7) == 0) || (BitFieldRead64 (MtrrCap
, 8, 8) == 0)) {