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 Determine the MTRR numbers used to program a memory range.
591 This function first checks the alignment of the base address. If the alignment of the base address <= Length,
592 cover the memory range (BaseAddress, alignment) by a MTRR, then BaseAddress += alignment and Length -= alignment.
593 Repeat the step until alignment > Length.
595 Then this function determines which direction of programming the variable MTRRs for the remaining length
596 will use fewer MTRRs.
598 @param BaseAddress Length of Memory to program MTRR
599 @param Length Length of Memory to program MTRR
600 @param MtrrNumber Pointer to the number of necessary MTRRs
602 @retval TRUE Positive direction is better.
603 FALSE Negtive direction is better.
607 GetMtrrNumberAndDirection (
608 IN UINT64 BaseAddress
,
620 if (BaseAddress
!= 0) {
623 // Calculate the alignment of the base address.
625 Alignment
= LShiftU64 (1, (UINTN
)LowBitSet64 (BaseAddress
));
627 if (Alignment
> Length
) {
632 BaseAddress
+= Alignment
;
646 TempQword
-= Power2MaxMemory (TempQword
);
648 } while (TempQword
!= 0);
650 TempQword
= Power2MaxMemory (LShiftU64 (Length
, 1)) - Length
;
653 TempQword
-= Power2MaxMemory (TempQword
);
655 } while (TempQword
!= 0);
657 if (Positive
<= Subtractive
) {
658 *MtrrNumber
+= Positive
;
661 *MtrrNumber
+= Subtractive
;
667 Invalid variable MTRRs according to the value in the shadow array.
669 This function programs MTRRs according to the values specified
672 @param VariableMtrr The array to shadow variable MTRRs content
677 IN VARIABLE_MTRR
*VariableMtrr
682 UINTN VariableMtrrCount
;
684 Cr4
= PreMtrrChange ();
686 VariableMtrrCount
= GetVariableMtrrCount ();
687 while (Index
< VariableMtrrCount
) {
688 if (!VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Used
) {
689 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
, 0);
690 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
+ 1, 0);
691 VariableMtrr
[Index
].Used
= FALSE
;
695 PostMtrrChange (Cr4
);
700 Programs variable MTRRs
702 This function programs variable MTRRs
704 @param MtrrNumber Index of MTRR to program.
705 @param BaseAddress Base address of memory region.
706 @param Length Length of memory region.
707 @param MemoryCacheType Memory type to set.
708 @param MtrrValidAddressMask The valid address mask for MTRR
712 ProgramVariableMtrr (
714 IN PHYSICAL_ADDRESS BaseAddress
,
716 IN UINT64 MemoryCacheType
,
717 IN UINT64 MtrrValidAddressMask
723 Cr4
= PreMtrrChange ();
726 // MTRR Physical Base
728 TempQword
= (BaseAddress
& MtrrValidAddressMask
) | MemoryCacheType
;
729 AsmWriteMsr64 ((UINT32
) MtrrNumber
, TempQword
);
732 // MTRR Physical Mask
734 TempQword
= ~(Length
- 1);
736 (UINT32
) (MtrrNumber
+ 1),
737 (TempQword
& MtrrValidAddressMask
) | MTRR_LIB_CACHE_MTRR_ENABLED
740 PostMtrrChange (Cr4
);
745 Convert the Memory attibute value to MTRR_MEMORY_CACHE_TYPE.
747 @param MtrrType MTRR memory type
749 @return The enum item in MTRR_MEMORY_CACHE_TYPE
752 MTRR_MEMORY_CACHE_TYPE
753 GetMemoryCacheTypeFromMtrrType (
758 case MTRR_CACHE_UNCACHEABLE
:
759 return CacheUncacheable
;
760 case MTRR_CACHE_WRITE_COMBINING
:
761 return CacheWriteCombining
;
762 case MTRR_CACHE_WRITE_THROUGH
:
763 return CacheWriteThrough
;
764 case MTRR_CACHE_WRITE_PROTECTED
:
765 return CacheWriteProtected
;
766 case MTRR_CACHE_WRITE_BACK
:
767 return CacheWriteBack
;
770 // MtrrType is MTRR_CACHE_INVALID_TYPE, that means
771 // no mtrr covers the range
773 return CacheUncacheable
;
778 Initializes the valid bits mask and valid address mask for MTRRs.
780 This function initializes the valid bits mask and valid address mask for MTRRs.
782 @param MtrrValidBitsMask The mask for the valid bit of the MTRR
783 @param MtrrValidAddressMask The valid address mask for the MTRR
787 MtrrLibInitializeMtrrMask (
788 OUT UINT64
*MtrrValidBitsMask
,
789 OUT UINT64
*MtrrValidAddressMask
793 UINT8 PhysicalAddressBits
;
795 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
797 if (RegEax
>= 0x80000008) {
798 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
800 PhysicalAddressBits
= (UINT8
) RegEax
;
802 *MtrrValidBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
803 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
805 *MtrrValidBitsMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
806 *MtrrValidAddressMask
= 0xFFFFFFFF;
812 Determing the real attribute of a memory range.
814 This function is to arbitrate the real attribute of the memory when
815 there are 2 MTRR covers the same memory range. For further details,
816 please refer the IA32 Software Developer's Manual, Volume 3,
819 @param MtrrType1 the first kind of Memory type
820 @param MtrrType2 the second kind of memory type
831 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
833 case MTRR_CACHE_UNCACHEABLE
:
834 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
836 case MTRR_CACHE_WRITE_COMBINING
:
838 MtrrType2
==MTRR_CACHE_WRITE_COMBINING
||
839 MtrrType2
==MTRR_CACHE_UNCACHEABLE
841 MtrrType
= MtrrType2
;
844 case MTRR_CACHE_WRITE_THROUGH
:
846 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
847 MtrrType2
==MTRR_CACHE_WRITE_BACK
849 MtrrType
= MTRR_CACHE_WRITE_THROUGH
;
850 } else if(MtrrType2
==MTRR_CACHE_UNCACHEABLE
) {
851 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
854 case MTRR_CACHE_WRITE_PROTECTED
:
855 if (MtrrType2
== MTRR_CACHE_WRITE_PROTECTED
||
856 MtrrType2
== MTRR_CACHE_UNCACHEABLE
) {
857 MtrrType
= MtrrType2
;
860 case MTRR_CACHE_WRITE_BACK
:
862 MtrrType2
== MTRR_CACHE_UNCACHEABLE
||
863 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
864 MtrrType2
== MTRR_CACHE_WRITE_BACK
866 MtrrType
= MtrrType2
;
869 case MTRR_CACHE_INVALID_TYPE
:
870 MtrrType
= MtrrType2
;
876 if (MtrrType2
== MTRR_CACHE_INVALID_TYPE
) {
877 MtrrType
= MtrrType1
;
884 This function attempts to set the attributes for a memory range.
886 @param BaseAddress The physical address that is the start
887 address of a memory region.
888 @param Length The size in bytes of the memory region.
889 @param Attributes The bit mask of attributes to set for the
892 @retval RETURN_SUCCESS The attributes were set for the memory
894 @retval RETURN_INVALID_PARAMETER Length is zero.
895 @retval RETURN_UNSUPPORTED The processor does not support one or
896 more bytes of the memory resource range
897 specified by BaseAddress and Length.
898 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
899 for the memory resource range specified
900 by BaseAddress and Length.
901 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
902 range specified by BaseAddress and Length
904 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
905 modify the attributes of the memory
911 MtrrSetMemoryAttribute (
912 IN PHYSICAL_ADDRESS BaseAddress
,
914 IN MTRR_MEMORY_CACHE_TYPE Attribute
918 RETURN_STATUS Status
;
925 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
927 UINT64 MtrrValidBitsMask
;
928 UINT64 MtrrValidAddressMask
;
930 BOOLEAN OverwriteExistingMtrr
;
931 UINT32 FirmwareVariableMtrrCount
;
932 UINT32 VariableMtrrEnd
;
934 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, Length
));
936 if (!IsMtrrSupported ()) {
937 Status
= RETURN_UNSUPPORTED
;
941 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCount ();
942 VariableMtrrEnd
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (2 * GetVariableMtrrCount ()) - 1;
944 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
947 MemoryType
= (UINT64
)Attribute
;
948 OverwriteExistingMtrr
= FALSE
;
951 // Check for an invalid parameter
954 Status
= RETURN_INVALID_PARAMETER
;
959 (BaseAddress
& ~MtrrValidAddressMask
) != 0 ||
960 (Length
& ~MtrrValidAddressMask
) != 0
962 Status
= RETURN_UNSUPPORTED
;
967 // Check if Fixed MTRR
969 Status
= RETURN_SUCCESS
;
970 while ((BaseAddress
< BASE_1MB
) && (Length
> 0) && Status
== RETURN_SUCCESS
) {
971 Cr4
= PreMtrrChange ();
972 Status
= ProgramFixedMtrr (MemoryType
, &BaseAddress
, &Length
);
973 PostMtrrChange (Cr4
);
974 if (RETURN_ERROR (Status
)) {
981 // A Length of 0 can only make sense for fixed MTTR ranges.
982 // Since we just handled the fixed MTRRs, we can skip the
983 // variable MTRR section.
989 // Since memory ranges below 1MB will be overridden by the fixed MTRRs,
990 // we can set the base to 0 to save variable MTRRs.
992 if (BaseAddress
== BASE_1MB
) {
1000 UsedMtrr
= MtrrGetMemoryAttributeInVariableMtrr (MtrrValidBitsMask
, MtrrValidAddressMask
, VariableMtrr
);
1001 OverLap
= CheckMemoryAttributeOverlap (BaseAddress
, BaseAddress
+ Length
- 1, VariableMtrr
);
1003 Status
= CombineMemoryAttribute (MemoryType
, &BaseAddress
, &Length
, VariableMtrr
, &UsedMtrr
, &OverwriteExistingMtrr
);
1004 if (RETURN_ERROR (Status
)) {
1010 // Combined successfully
1012 Status
= RETURN_SUCCESS
;
1018 // Program Variable MTRRs
1020 // Avoid hardcode here and read data dynamically
1022 if (UsedMtrr
>= FirmwareVariableMtrrCount
) {
1023 Status
= RETURN_OUT_OF_RESOURCES
;
1028 // The memory type is the same with the type specified by
1029 // MTRR_LIB_IA32_MTRR_DEF_TYPE.
1031 if ((!OverwriteExistingMtrr
) && (Attribute
== GetMtrrDefaultMemoryType ())) {
1033 // Invalidate the now-unused MTRRs
1035 InvalidateMtrr(VariableMtrr
);
1039 Positive
= GetMtrrNumberAndDirection (BaseAddress
, Length
, &MtrrNumber
);
1041 if ((UsedMtrr
+ MtrrNumber
) > FirmwareVariableMtrrCount
) {
1042 Status
= RETURN_OUT_OF_RESOURCES
;
1047 // Invalidate the now-unused MTRRs
1049 InvalidateMtrr(VariableMtrr
);
1052 // Find first unused MTRR
1054 for (MsrNum
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
;
1055 MsrNum
< VariableMtrrEnd
;
1058 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1063 if (BaseAddress
!= 0) {
1066 // Calculate the alignment of the base address.
1068 Alignment
= LShiftU64 (1, (UINTN
)LowBitSet64 (BaseAddress
));
1070 if (Alignment
> Length
) {
1077 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1078 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1083 ProgramVariableMtrr (
1088 MtrrValidAddressMask
1090 BaseAddress
+= Alignment
;
1091 Length
-= Alignment
;
1102 Length
= Power2MaxMemory (LShiftU64 (TempQword
, 1));
1107 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1108 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1113 ProgramVariableMtrr (
1118 MtrrValidAddressMask
1120 BaseAddress
+= Length
;
1121 TempQword
= Length
- TempQword
;
1122 MemoryType
= MTRR_CACHE_UNCACHEABLE
;
1129 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1130 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1135 Length
= Power2MaxMemory (TempQword
);
1137 BaseAddress
-= Length
;
1140 ProgramVariableMtrr (
1145 MtrrValidAddressMask
1149 BaseAddress
+= Length
;
1151 TempQword
-= Length
;
1153 } while (TempQword
> 0);
1156 DEBUG((DEBUG_CACHE
, " Status = %r\n", Status
));
1157 if (!RETURN_ERROR (Status
)) {
1158 MtrrDebugPrintAllMtrrs ();
1166 This function will get the memory cache type of the specific address.
1168 This function is mainly for debug purpose.
1170 @param Address The specific address
1172 @return Memory cache type of the sepcific address
1175 MTRR_MEMORY_CACHE_TYPE
1177 MtrrGetMemoryAttribute (
1178 IN PHYSICAL_ADDRESS Address
1185 UINT64 TempMtrrType
;
1186 MTRR_MEMORY_CACHE_TYPE CacheType
;
1187 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1188 UINT64 MtrrValidBitsMask
;
1189 UINT64 MtrrValidAddressMask
;
1190 UINTN VariableMtrrCount
;
1192 if (!IsMtrrSupported ()) {
1193 return CacheUncacheable
;
1197 // Check if MTRR is enabled, if not, return UC as attribute
1199 TempQword
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1200 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
1202 if ((TempQword
& MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1203 return CacheUncacheable
;
1207 // If address is less than 1M, then try to go through the fixed MTRR
1209 if (Address
< BASE_1MB
) {
1210 if ((TempQword
& MTRR_LIB_CACHE_FIXED_MTRR_ENABLED
) != 0) {
1212 // Go through the fixed MTRR
1214 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1215 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1217 mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1218 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)
1222 ((UINTN
)Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1223 mMtrrLibFixedMtrrTable
[Index
].Length
;
1224 TempQword
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1225 MtrrType
= RShiftU64 (TempQword
, SubIndex
* 8) & 0xFF;
1226 return GetMemoryCacheTypeFromMtrrType (MtrrType
);
1231 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1232 MtrrGetMemoryAttributeInVariableMtrr(
1234 MtrrValidAddressMask
,
1239 // Go through the variable MTRR
1241 VariableMtrrCount
= GetVariableMtrrCount ();
1242 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1244 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1245 if (VariableMtrr
[Index
].Valid
) {
1246 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1247 Address
< VariableMtrr
[Index
].BaseAddress
+VariableMtrr
[Index
].Length
) {
1248 TempMtrrType
= VariableMtrr
[Index
].Type
;
1249 MtrrType
= MtrrPrecedence (MtrrType
, TempMtrrType
);
1253 CacheType
= GetMemoryCacheTypeFromMtrrType (MtrrType
);
1260 This function will get the raw value in variable MTRRs
1262 @param VariableSettings A buffer to hold variable MTRRs content.
1264 @return The VariableSettings input pointer
1267 MTRR_VARIABLE_SETTINGS
*
1269 MtrrGetVariableMtrr (
1270 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
1274 UINT32 VariableMtrrCount
;
1276 if (!IsMtrrSupported ()) {
1277 return VariableSettings
;
1280 VariableMtrrCount
= GetVariableMtrrCount ();
1281 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1283 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1284 VariableSettings
->Mtrr
[Index
].Base
=
1285 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1));
1286 VariableSettings
->Mtrr
[Index
].Mask
=
1287 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1);
1290 return VariableSettings
;
1295 Worker function setting variable MTRRs
1297 @param VariableSettings A buffer to hold variable MTRRs content.
1301 MtrrSetVariableMtrrWorker (
1302 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1306 UINT32 VariableMtrrCount
;
1308 VariableMtrrCount
= GetVariableMtrrCount ();
1309 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1311 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1313 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1),
1314 VariableSettings
->Mtrr
[Index
].Base
1317 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1,
1318 VariableSettings
->Mtrr
[Index
].Mask
1325 This function sets variable MTRRs
1327 @param VariableSettings A buffer to hold variable MTRRs content.
1329 @return The pointer of VariableSettings
1332 MTRR_VARIABLE_SETTINGS
*
1334 MtrrSetVariableMtrr (
1335 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1340 if (!IsMtrrSupported ()) {
1341 return VariableSettings
;
1344 Cr4
= PreMtrrChange ();
1345 MtrrSetVariableMtrrWorker (VariableSettings
);
1346 PostMtrrChange (Cr4
);
1347 return VariableSettings
;
1352 This function gets the content in fixed MTRRs
1354 @param FixedSettings A buffer to hold fixed Mtrrs content.
1356 @retval The pointer of FixedSettings
1359 MTRR_FIXED_SETTINGS
*
1362 OUT MTRR_FIXED_SETTINGS
*FixedSettings
1367 if (!IsMtrrSupported ()) {
1368 return FixedSettings
;
1371 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1372 FixedSettings
->Mtrr
[Index
] =
1373 AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1376 return FixedSettings
;
1380 Worker function setting fixed MTRRs
1382 @param FixedSettings A buffer to hold fixed Mtrrs content.
1386 MtrrSetFixedMtrrWorker (
1387 IN MTRR_FIXED_SETTINGS
*FixedSettings
1392 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1394 mMtrrLibFixedMtrrTable
[Index
].Msr
,
1395 FixedSettings
->Mtrr
[Index
]
1402 This function sets fixed MTRRs
1404 @param FixedSettings A buffer to hold fixed Mtrrs content.
1406 @retval The pointer of FixedSettings
1409 MTRR_FIXED_SETTINGS
*
1412 IN MTRR_FIXED_SETTINGS
*FixedSettings
1417 if (!IsMtrrSupported ()) {
1418 return FixedSettings
;
1421 Cr4
= PreMtrrChange ();
1422 MtrrSetFixedMtrrWorker (FixedSettings
);
1423 PostMtrrChange (Cr4
);
1425 return FixedSettings
;
1430 This function gets the content in all MTRRs (variable and fixed)
1432 @param MtrrSetting A buffer to hold all Mtrrs content.
1434 @retval the pointer of MtrrSetting
1440 OUT MTRR_SETTINGS
*MtrrSetting
1443 if (!IsMtrrSupported ()) {
1450 MtrrGetFixedMtrr (&MtrrSetting
->Fixed
);
1453 // Get variable MTRRs
1455 MtrrGetVariableMtrr (&MtrrSetting
->Variables
);
1458 // Get MTRR_DEF_TYPE value
1460 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1467 This function sets all MTRRs (variable and fixed)
1469 @param MtrrSetting A buffer holding all MTRRs content.
1471 @retval The pointer of MtrrSetting
1477 IN MTRR_SETTINGS
*MtrrSetting
1482 if (!IsMtrrSupported ()) {
1486 Cr4
= PreMtrrChange ();
1491 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
1494 // Set variable MTRRs
1496 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
1499 // Set MTRR_DEF_TYPE value
1501 AsmWriteMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
1503 PostMtrrChange (Cr4
);
1509 This function prints all MTRRs for debugging.
1513 MtrrDebugPrintAllMtrrs (
1518 MTRR_SETTINGS MtrrSettings
;
1521 UINTN VariableMtrrCount
;
1529 UINT64 NoRangeLimit
;
1532 UINTN PreviousMemoryType
;
1535 if (!IsMtrrSupported ()) {
1539 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1540 DEBUG((DEBUG_CACHE
, "=============\n"));
1542 MtrrGetAllMtrrs (&MtrrSettings
);
1543 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", MtrrSettings
.MtrrDefType
));
1544 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1545 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, MtrrSettings
.Fixed
.Mtrr
[Index
]));
1548 VariableMtrrCount
= GetVariableMtrrCount ();
1549 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1550 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1552 MtrrSettings
.Variables
.Mtrr
[Index
].Base
,
1553 MtrrSettings
.Variables
.Mtrr
[Index
].Mask
1556 DEBUG((DEBUG_CACHE
, "\n"));
1557 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1558 DEBUG((DEBUG_CACHE
, "====================================\n"));
1561 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1562 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1563 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1564 for (Index1
= 0; Index1
< 8; Index1
++) {
1565 MemoryType
= (UINTN
)(RShiftU64 (MtrrSettings
.Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff);
1566 if (MemoryType
> CacheWriteBack
) {
1567 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1569 if (MemoryType
!= PreviousMemoryType
) {
1570 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1571 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1573 PreviousMemoryType
= MemoryType
;
1574 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1576 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1579 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1581 VariableMtrrCount
= GetVariableMtrrCount ();
1584 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1586 MemoryType
= MtrrGetMemoryAttribute (Base
);
1587 if (MemoryType
> CacheWriteBack
) {
1588 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1591 if (MemoryType
!= PreviousMemoryType
) {
1592 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1593 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1595 PreviousMemoryType
= MemoryType
;
1596 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1599 RangeBase
= BASE_1MB
;
1600 NoRangeBase
= BASE_1MB
;
1602 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1603 if (RegEax
>= 0x80000008) {
1604 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1605 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1608 NoRangeLimit
= Limit
;
1610 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1611 if ((MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1613 // If mask is not valid, then do not display range
1617 MtrrBase
= (MtrrSettings
.Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1618 MtrrLimit
= MtrrBase
+ ((~(MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1620 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1624 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1625 RangeBase
= MtrrBase
;
1627 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1628 RangeBase
= MtrrLimit
+ 1;
1630 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1631 RangeLimit
= MtrrBase
- 1;
1633 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1634 RangeLimit
= MtrrLimit
;
1637 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1638 NoRangeBase
= MtrrLimit
+ 1;
1640 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1641 NoRangeLimit
= MtrrBase
- 1;
1646 Base
= RangeLimit
+ 1;
1648 Base
= NoRangeLimit
+ 1;
1651 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1656 Checks if MTRR is supported.
1658 @retval TRUE MTRR is supported.
1659 @retval FALSE MTRR is not supported.
1672 // Check CPUID(1).EDX[12] for MTRR capability
1674 AsmCpuid (1, NULL
, NULL
, NULL
, &RegEdx
);
1675 if (BitFieldRead32 (RegEdx
, 12, 12) == 0) {
1680 // Check IA32_MTRRCAP.[0..7] for number of variable MTRRs and IA32_MTRRCAP[8] for
1681 // fixed MTRRs existence. If number of variable MTRRs is zero, or fixed MTRRs do not
1682 // exist, return false.
1684 MtrrCap
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP
);
1685 if ((BitFieldRead64 (MtrrCap
, 0, 7) == 0) || (BitFieldRead64 (MtrrCap
, 8, 8) == 0)) {