4 Copyright (c) 2008 - 2015, 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 // Context to save and restore when MTRRs are programmed
28 BOOLEAN InterruptState
;
32 // This table defines the offset, base and length of the fixed MTRRs
34 CONST FIXED_MTRR mMtrrLibFixedMtrrTable
[] = {
36 MTRR_LIB_IA32_MTRR_FIX64K_00000
,
41 MTRR_LIB_IA32_MTRR_FIX16K_80000
,
46 MTRR_LIB_IA32_MTRR_FIX16K_A0000
,
51 MTRR_LIB_IA32_MTRR_FIX4K_C0000
,
56 MTRR_LIB_IA32_MTRR_FIX4K_C8000
,
61 MTRR_LIB_IA32_MTRR_FIX4K_D0000
,
66 MTRR_LIB_IA32_MTRR_FIX4K_D8000
,
71 MTRR_LIB_IA32_MTRR_FIX4K_E0000
,
76 MTRR_LIB_IA32_MTRR_FIX4K_E8000
,
81 MTRR_LIB_IA32_MTRR_FIX4K_F0000
,
86 MTRR_LIB_IA32_MTRR_FIX4K_F8000
,
93 // Lookup table used to print MTRRs
95 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mMtrrMemoryCacheTypeShortName
[] = {
96 "UC", // CacheUncacheable
97 "WC", // CacheWriteCombining
100 "WT", // CacheWriteThrough
101 "WP", // CacheWriteProtected
102 "WB", // CacheWriteBack
107 Worker function returns the variable MTRR count for the CPU.
109 @return Variable MTRR count
113 GetVariableMtrrCountWorker (
117 UINT32 VariableMtrrCount
;
119 VariableMtrrCount
= (UINT32
)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP
) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK
);
120 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
121 return VariableMtrrCount
;
125 Returns the variable MTRR count for the CPU.
127 @return Variable MTRR count
132 GetVariableMtrrCount (
136 if (!IsMtrrSupported ()) {
139 return GetVariableMtrrCountWorker ();
143 Worker function returns the firmware usable variable MTRR count for the CPU.
145 @return Firmware usable variable MTRR count
149 GetFirmwareVariableMtrrCountWorker (
153 UINT32 VariableMtrrCount
;
154 UINT32 ReservedMtrrNumber
;
156 VariableMtrrCount
= GetVariableMtrrCountWorker ();
157 ReservedMtrrNumber
= PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
158 if (VariableMtrrCount
< ReservedMtrrNumber
) {
162 return VariableMtrrCount
- ReservedMtrrNumber
;
166 Returns the firmware usable variable MTRR count for the CPU.
168 @return Firmware usable variable MTRR count
173 GetFirmwareVariableMtrrCount (
177 if (!IsMtrrSupported ()) {
180 return GetFirmwareVariableMtrrCountWorker ();
184 Worker function returns the default MTRR cache type for the system.
186 @return The default MTRR cache type.
189 MTRR_MEMORY_CACHE_TYPE
190 MtrrGetDefaultMemoryTypeWorker (
194 return (MTRR_MEMORY_CACHE_TYPE
) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
) & 0x7);
199 Returns the default MTRR cache type for the system.
201 @return The default MTRR cache type.
204 MTRR_MEMORY_CACHE_TYPE
206 MtrrGetDefaultMemoryType (
210 if (!IsMtrrSupported ()) {
211 return CacheUncacheable
;
213 return MtrrGetDefaultMemoryTypeWorker ();
217 Preparation before programming MTRR.
219 This function will do some preparation for programming MTRRs:
220 disable cache, invalid cache and disable MTRR caching functionality
222 @param[out] MtrrContext Pointer to context to save
227 OUT MTRR_CONTEXT
*MtrrContext
231 // Disable interrupts and save current interrupt state
233 MtrrContext
->InterruptState
= SaveAndDisableInterrupts();
236 // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29)
241 // Save original CR4 value and clear PGE flag (Bit 7)
243 MtrrContext
->Cr4
= AsmReadCr4 ();
244 AsmWriteCr4 (MtrrContext
->Cr4
& (~BIT7
));
254 AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, 10, 11, 0);
258 Cleaning up after programming MTRRs.
260 This function will do some clean up after programming MTRRs:
261 Flush all TLBs, re-enable caching, restore CR4.
263 @param[in] MtrrContext Pointer to context to restore
267 PostMtrrChangeEnableCache (
268 IN MTRR_CONTEXT
*MtrrContext
277 // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29)
282 // Restore original CR4 value
284 AsmWriteCr4 (MtrrContext
->Cr4
);
287 // Restore original interrupt state
289 SetInterruptState (MtrrContext
->InterruptState
);
293 Cleaning up after programming MTRRs.
295 This function will do some clean up after programming MTRRs:
296 enable MTRR caching functionality, and enable cache
298 @param[in] MtrrContext Pointer to context to restore
303 IN MTRR_CONTEXT
*MtrrContext
309 AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, 10, 11, 3);
311 PostMtrrChangeEnableCache (MtrrContext
);
315 Worker function gets the content in fixed MTRRs
317 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
319 @retval The pointer of FixedSettings
323 MtrrGetFixedMtrrWorker (
324 OUT MTRR_FIXED_SETTINGS
*FixedSettings
329 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
330 FixedSettings
->Mtrr
[Index
] =
331 AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
334 return FixedSettings
;
339 This function gets the content in fixed MTRRs
341 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
343 @retval The pointer of FixedSettings
349 OUT MTRR_FIXED_SETTINGS
*FixedSettings
352 if (!IsMtrrSupported ()) {
353 return FixedSettings
;
356 return MtrrGetFixedMtrrWorker (FixedSettings
);
361 Worker function will get the raw value in variable MTRRs
363 @param[out] VariableSettings A buffer to hold variable MTRRs content.
365 @return The VariableSettings input pointer
368 MTRR_VARIABLE_SETTINGS
*
369 MtrrGetVariableMtrrWorker (
370 IN UINT32 VariableMtrrCount
,
371 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
376 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
378 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
379 VariableSettings
->Mtrr
[Index
].Base
=
380 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1));
381 VariableSettings
->Mtrr
[Index
].Mask
=
382 AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1);
385 return VariableSettings
;
389 This function will get the raw value in variable MTRRs
391 @param[out] VariableSettings A buffer to hold variable MTRRs content.
393 @return The VariableSettings input pointer
396 MTRR_VARIABLE_SETTINGS
*
398 MtrrGetVariableMtrr (
399 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
402 if (!IsMtrrSupported ()) {
403 return VariableSettings
;
406 return MtrrGetVariableMtrrWorker (
407 GetVariableMtrrCountWorker (),
413 Programs fixed MTRRs registers.
415 @param[in] MemoryCacheType The memory type to set.
416 @param[in, out] Base The base address of memory range.
417 @param[in, out] Length The length of memory range.
418 @param[out] ReturnMsrNum The index of the fixed MTRR MSR to program.
419 @param[out] ReturnClearMask The bits to clear in the fixed MTRR MSR.
420 @param[out] ReturnOrMask The bits to set in the fixed MTRR MSR.
422 @retval RETURN_SUCCESS The cache type was updated successfully
423 @retval RETURN_UNSUPPORTED The requested range or cache type was invalid
429 IN UINT64 MemoryCacheType
,
431 IN OUT UINT64
*Length
,
432 OUT UINT32
*ReturnMsrNum
,
433 OUT UINT64
*ReturnClearMask
,
434 OUT UINT64
*ReturnOrMask
447 for (MsrNum
= 0; MsrNum
< MTRR_NUMBER_OF_FIXED_MTRR
; MsrNum
++) {
448 if ((*Base
>= mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
) &&
451 mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
+
452 (8 * mMtrrLibFixedMtrrTable
[MsrNum
].Length
)
460 if (MsrNum
== MTRR_NUMBER_OF_FIXED_MTRR
) {
461 return RETURN_UNSUPPORTED
;
465 // We found the fixed MTRR to be programmed
467 for (ByteShift
= 0; ByteShift
< 8; ByteShift
++) {
470 mMtrrLibFixedMtrrTable
[MsrNum
].BaseAddress
+
471 (ByteShift
* mMtrrLibFixedMtrrTable
[MsrNum
].Length
)
478 if (ByteShift
== 8) {
479 return RETURN_UNSUPPORTED
;
484 ((ByteShift
< 8) && (*Length
>= mMtrrLibFixedMtrrTable
[MsrNum
].Length
));
487 OrMask
|= LShiftU64 ((UINT64
) MemoryCacheType
, (UINT32
) (ByteShift
* 8));
488 ClearMask
|= LShiftU64 ((UINT64
) 0xFF, (UINT32
) (ByteShift
* 8));
489 *Length
-= mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
490 *Base
+= mMtrrLibFixedMtrrTable
[MsrNum
].Length
;
493 if (ByteShift
< 8 && (*Length
!= 0)) {
494 return RETURN_UNSUPPORTED
;
497 *ReturnMsrNum
= MsrNum
;
498 *ReturnClearMask
= ClearMask
;
499 *ReturnOrMask
= OrMask
;
501 return RETURN_SUCCESS
;
506 Worker function gets the attribute of variable MTRRs.
508 This function shadows the content of variable MTRRs into an
509 internal array: VariableMtrr.
511 @param[in] VariableSettings The variable MTRR values to shadow
512 @param[in] FirmwareVariableMtrrCount The number of variable MTRRs available to firmware
513 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
514 @param[in] MtrrValidAddressMask The valid address mask for MTRR
515 @param[out] VariableMtrr The array to shadow variable MTRRs content
517 @return The return value of this parameter indicates the
518 number of MTRRs which has been used.
522 MtrrGetMemoryAttributeInVariableMtrrWorker (
523 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
,
524 IN UINTN FirmwareVariableMtrrCount
,
525 IN UINT64 MtrrValidBitsMask
,
526 IN UINT64 MtrrValidAddressMask
,
527 OUT VARIABLE_MTRR
*VariableMtrr
533 ZeroMem (VariableMtrr
, sizeof (VARIABLE_MTRR
) * MTRR_NUMBER_OF_VARIABLE_MTRR
);
534 for (Index
= 0, UsedMtrr
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
535 if ((VariableSettings
->Mtrr
[Index
].Mask
& MTRR_LIB_CACHE_MTRR_ENABLED
) != 0) {
536 VariableMtrr
[Index
].Msr
= (UINT32
)Index
;
537 VariableMtrr
[Index
].BaseAddress
= (VariableSettings
->Mtrr
[Index
].Base
& MtrrValidAddressMask
);
538 VariableMtrr
[Index
].Length
= ((~(VariableSettings
->Mtrr
[Index
].Mask
& MtrrValidAddressMask
)) & MtrrValidBitsMask
) + 1;
539 VariableMtrr
[Index
].Type
= (VariableSettings
->Mtrr
[Index
].Base
& 0x0ff);
540 VariableMtrr
[Index
].Valid
= TRUE
;
541 VariableMtrr
[Index
].Used
= TRUE
;
550 Gets the attribute of variable MTRRs.
552 This function shadows the content of variable MTRRs into an
553 internal array: VariableMtrr.
555 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
556 @param[in] MtrrValidAddressMask The valid address mask for MTRR
557 @param[out] VariableMtrr The array to shadow variable MTRRs content
559 @return The return value of this paramter indicates the
560 number of MTRRs which has been used.
565 MtrrGetMemoryAttributeInVariableMtrr (
566 IN UINT64 MtrrValidBitsMask
,
567 IN UINT64 MtrrValidAddressMask
,
568 OUT VARIABLE_MTRR
*VariableMtrr
571 MTRR_VARIABLE_SETTINGS VariableSettings
;
573 if (!IsMtrrSupported ()) {
577 MtrrGetVariableMtrrWorker (
578 GetVariableMtrrCountWorker (),
582 return MtrrGetMemoryAttributeInVariableMtrrWorker (
584 GetFirmwareVariableMtrrCountWorker (),
586 MtrrValidAddressMask
,
593 Checks overlap between given memory range and MTRRs.
595 @param[in] FirmwareVariableMtrrCount The number of variable MTRRs available
597 @param[in] Start The start address of memory range.
598 @param[in] End The end address of memory range.
599 @param[in] VariableMtrr The array to shadow variable MTRRs content
601 @retval TRUE Overlap exists.
602 @retval FALSE No overlap.
606 CheckMemoryAttributeOverlap (
607 IN UINTN FirmwareVariableMtrrCount
,
608 IN PHYSICAL_ADDRESS Start
,
609 IN PHYSICAL_ADDRESS End
,
610 IN VARIABLE_MTRR
*VariableMtrr
615 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
617 VariableMtrr
[Index
].Valid
&&
619 (Start
> (VariableMtrr
[Index
].BaseAddress
+
620 VariableMtrr
[Index
].Length
- 1)
622 (End
< VariableMtrr
[Index
].BaseAddress
)
634 Marks a variable MTRR as non-valid.
636 @param[in] Index The index of the array VariableMtrr to be invalidated
637 @param[in] VariableMtrr The array to shadow variable MTRRs content
638 @param[out] UsedMtrr The number of MTRRs which has already been used
642 InvalidateShadowMtrr (
644 IN VARIABLE_MTRR
*VariableMtrr
,
648 VariableMtrr
[Index
].Valid
= FALSE
;
649 *UsedMtrr
= *UsedMtrr
- 1;
654 Combines memory attributes.
656 If overlap exists between given memory range and MTRRs, try to combine them.
658 @param[in] FirmwareVariableMtrrCount The number of variable MTRRs
659 available to firmware.
660 @param[in] Attributes The memory type to set.
661 @param[in, out] Base The base address of memory range.
662 @param[in, out] Length The length of memory range.
663 @param[in] VariableMtrr The array to shadow variable MTRRs content
664 @param[in, out] UsedMtrr The number of MTRRs which has already been used
665 @param[out] OverwriteExistingMtrr Returns whether an existing MTRR was used
667 @retval EFI_SUCCESS Memory region successfully combined.
668 @retval EFI_ACCESS_DENIED Memory region cannot be combined.
672 CombineMemoryAttribute (
673 IN UINT32 FirmwareVariableMtrrCount
,
674 IN UINT64 Attributes
,
676 IN OUT UINT64
*Length
,
677 IN VARIABLE_MTRR
*VariableMtrr
,
678 IN OUT UINT32
*UsedMtrr
,
679 OUT BOOLEAN
*OverwriteExistingMtrr
687 BOOLEAN CoveredByExistingMtrr
;
689 *OverwriteExistingMtrr
= FALSE
;
690 CoveredByExistingMtrr
= FALSE
;
691 EndAddress
= *Base
+*Length
- 1;
693 for (Index
= 0; Index
< FirmwareVariableMtrrCount
; Index
++) {
695 MtrrEnd
= VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
- 1;
697 !VariableMtrr
[Index
].Valid
||
700 (EndAddress
< VariableMtrr
[Index
].BaseAddress
)
707 // Combine same attribute MTRR range
709 if (Attributes
== VariableMtrr
[Index
].Type
) {
711 // if the MTRR range contain the request range, set a flag, then continue to
712 // invalidate any MTRR of the same request range with higher priority cache type.
714 if (VariableMtrr
[Index
].BaseAddress
<= *Base
&& MtrrEnd
>= EndAddress
) {
715 CoveredByExistingMtrr
= TRUE
;
719 // invalid this MTRR, and program the combine range
722 (*Base
) < VariableMtrr
[Index
].BaseAddress
?
724 VariableMtrr
[Index
].BaseAddress
;
725 CombineEnd
= EndAddress
> MtrrEnd
? EndAddress
: MtrrEnd
;
728 // Record the MTRR usage status in VariableMtrr array.
730 InvalidateShadowMtrr (Index
, VariableMtrr
, UsedMtrr
);
731 *Base
= CombineStart
;
732 *Length
= CombineEnd
- CombineStart
+ 1;
733 EndAddress
= CombineEnd
;
734 *OverwriteExistingMtrr
= TRUE
;
738 // The cache type is different, but the range is convered by one MTRR
740 if (VariableMtrr
[Index
].BaseAddress
== *Base
&& MtrrEnd
== EndAddress
) {
741 InvalidateShadowMtrr (Index
, VariableMtrr
, UsedMtrr
);
747 if ((Attributes
== MTRR_CACHE_WRITE_THROUGH
&&
748 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_BACK
) ||
749 (Attributes
== MTRR_CACHE_WRITE_BACK
&&
750 VariableMtrr
[Index
].Type
== MTRR_CACHE_WRITE_THROUGH
) ||
751 (Attributes
== MTRR_CACHE_UNCACHEABLE
) ||
752 (VariableMtrr
[Index
].Type
== MTRR_CACHE_UNCACHEABLE
)
754 *OverwriteExistingMtrr
= TRUE
;
758 // Other type memory overlap is invalid
760 return RETURN_ACCESS_DENIED
;
763 if (CoveredByExistingMtrr
) {
767 return RETURN_SUCCESS
;
772 Calculates the maximum value which is a power of 2, but less the MemoryLength.
774 @param[in] MemoryLength The number to pass in.
776 @return The maximum value which is align to power of 2 and less the MemoryLength
781 IN UINT64 MemoryLength
786 if (RShiftU64 (MemoryLength
, 32) != 0) {
788 (UINT64
) GetPowerOfTwo32 (
789 (UINT32
) RShiftU64 (MemoryLength
, 32)
794 Result
= (UINT64
) GetPowerOfTwo32 ((UINT32
) MemoryLength
);
802 Determines the MTRR numbers used to program a memory range.
804 This function first checks the alignment of the base address.
805 If the alignment of the base address <= Length, cover the memory range
806 (BaseAddress, alignment) by a MTRR, then BaseAddress += alignment and
807 Length -= alignment. Repeat the step until alignment > Length.
809 Then this function determines which direction of programming the variable
810 MTRRs for the remaining length will use fewer MTRRs.
812 @param[in] BaseAddress Length of Memory to program MTRR
813 @param[in] Length Length of Memory to program MTRR
814 @param[in] MtrrNumber Pointer to the number of necessary MTRRs
816 @retval TRUE Positive direction is better.
817 FALSE Negative direction is better.
821 GetMtrrNumberAndDirection (
822 IN UINT64 BaseAddress
,
834 if (BaseAddress
!= 0) {
837 // Calculate the alignment of the base address.
839 Alignment
= LShiftU64 (1, (UINTN
)LowBitSet64 (BaseAddress
));
841 if (Alignment
> Length
) {
846 BaseAddress
+= Alignment
;
860 TempQword
-= Power2MaxMemory (TempQword
);
862 } while (TempQword
!= 0);
864 TempQword
= Power2MaxMemory (LShiftU64 (Length
, 1)) - Length
;
867 TempQword
-= Power2MaxMemory (TempQword
);
869 } while (TempQword
!= 0);
871 if (Positive
<= Subtractive
) {
872 *MtrrNumber
+= Positive
;
875 *MtrrNumber
+= Subtractive
;
881 Invalid variable MTRRs according to the value in the shadow array.
883 This function programs MTRRs according to the values specified
886 @param[in] VariableMtrrCount Number of variable MTRRs
887 @param[in, out] VariableMtrr Shadow of variable MTRR contents
892 IN UINTN VariableMtrrCount
,
893 IN OUT VARIABLE_MTRR
*VariableMtrr
897 MTRR_CONTEXT MtrrContext
;
899 PreMtrrChange (&MtrrContext
);
901 while (Index
< VariableMtrrCount
) {
902 if (!VariableMtrr
[Index
].Valid
&& VariableMtrr
[Index
].Used
) {
903 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
, 0);
904 AsmWriteMsr64 (VariableMtrr
[Index
].Msr
+ 1, 0);
905 VariableMtrr
[Index
].Used
= FALSE
;
909 PostMtrrChange (&MtrrContext
);
914 Programs variable MTRRs
916 This function programs variable MTRRs
918 @param[in] MtrrNumber Index of MTRR to program.
919 @param[in] BaseAddress Base address of memory region.
920 @param[in] Length Length of memory region.
921 @param[in] MemoryCacheType Memory type to set.
922 @param[in] MtrrValidAddressMask The valid address mask for MTRR
926 ProgramVariableMtrr (
928 IN PHYSICAL_ADDRESS BaseAddress
,
930 IN UINT64 MemoryCacheType
,
931 IN UINT64 MtrrValidAddressMask
935 MTRR_CONTEXT MtrrContext
;
937 PreMtrrChange (&MtrrContext
);
940 // MTRR Physical Base
942 TempQword
= (BaseAddress
& MtrrValidAddressMask
) | MemoryCacheType
;
943 AsmWriteMsr64 ((UINT32
) MtrrNumber
, TempQword
);
946 // MTRR Physical Mask
948 TempQword
= ~(Length
- 1);
950 (UINT32
) (MtrrNumber
+ 1),
951 (TempQword
& MtrrValidAddressMask
) | MTRR_LIB_CACHE_MTRR_ENABLED
954 PostMtrrChange (&MtrrContext
);
959 Converts the Memory attribute value to MTRR_MEMORY_CACHE_TYPE.
961 @param[in] MtrrType MTRR memory type
963 @return The enum item in MTRR_MEMORY_CACHE_TYPE
966 MTRR_MEMORY_CACHE_TYPE
967 GetMemoryCacheTypeFromMtrrType (
972 case MTRR_CACHE_UNCACHEABLE
:
973 return CacheUncacheable
;
974 case MTRR_CACHE_WRITE_COMBINING
:
975 return CacheWriteCombining
;
976 case MTRR_CACHE_WRITE_THROUGH
:
977 return CacheWriteThrough
;
978 case MTRR_CACHE_WRITE_PROTECTED
:
979 return CacheWriteProtected
;
980 case MTRR_CACHE_WRITE_BACK
:
981 return CacheWriteBack
;
984 // MtrrType is MTRR_CACHE_INVALID_TYPE, that means
985 // no MTRR covers the range
987 return MtrrGetDefaultMemoryType ();
992 Initializes the valid bits mask and valid address mask for MTRRs.
994 This function initializes the valid bits mask and valid address mask for MTRRs.
996 @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR
997 @param[out] MtrrValidAddressMask The valid address mask for the MTRR
1001 MtrrLibInitializeMtrrMask (
1002 OUT UINT64
*MtrrValidBitsMask
,
1003 OUT UINT64
*MtrrValidAddressMask
1007 UINT8 PhysicalAddressBits
;
1009 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1011 if (RegEax
>= 0x80000008) {
1012 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1014 PhysicalAddressBits
= (UINT8
) RegEax
;
1016 *MtrrValidBitsMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
1017 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
1019 *MtrrValidBitsMask
= MTRR_LIB_MSR_VALID_MASK
;
1020 *MtrrValidAddressMask
= MTRR_LIB_CACHE_VALID_ADDRESS
;
1026 Determines the real attribute of a memory range.
1028 This function is to arbitrate the real attribute of the memory when
1029 there are 2 MTRRs covers the same memory range. For further details,
1030 please refer the IA32 Software Developer's Manual, Volume 3,
1033 @param[in] MtrrType1 The first kind of Memory type
1034 @param[in] MtrrType2 The second kind of memory type
1039 IN UINT64 MtrrType1
,
1045 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
1046 switch (MtrrType1
) {
1047 case MTRR_CACHE_UNCACHEABLE
:
1048 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
1050 case MTRR_CACHE_WRITE_COMBINING
:
1052 MtrrType2
==MTRR_CACHE_WRITE_COMBINING
||
1053 MtrrType2
==MTRR_CACHE_UNCACHEABLE
1055 MtrrType
= MtrrType2
;
1058 case MTRR_CACHE_WRITE_THROUGH
:
1060 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
1061 MtrrType2
==MTRR_CACHE_WRITE_BACK
1063 MtrrType
= MTRR_CACHE_WRITE_THROUGH
;
1064 } else if(MtrrType2
==MTRR_CACHE_UNCACHEABLE
) {
1065 MtrrType
= MTRR_CACHE_UNCACHEABLE
;
1068 case MTRR_CACHE_WRITE_PROTECTED
:
1069 if (MtrrType2
== MTRR_CACHE_WRITE_PROTECTED
||
1070 MtrrType2
== MTRR_CACHE_UNCACHEABLE
) {
1071 MtrrType
= MtrrType2
;
1074 case MTRR_CACHE_WRITE_BACK
:
1076 MtrrType2
== MTRR_CACHE_UNCACHEABLE
||
1077 MtrrType2
==MTRR_CACHE_WRITE_THROUGH
||
1078 MtrrType2
== MTRR_CACHE_WRITE_BACK
1080 MtrrType
= MtrrType2
;
1083 case MTRR_CACHE_INVALID_TYPE
:
1084 MtrrType
= MtrrType2
;
1090 if (MtrrType2
== MTRR_CACHE_INVALID_TYPE
) {
1091 MtrrType
= MtrrType1
;
1099 This function will get the memory cache type of the specific address.
1101 This function is mainly for debug purpose.
1103 @param[in] Address The specific address
1105 @return Memory cache type of the specific address
1108 MTRR_MEMORY_CACHE_TYPE
1110 MtrrGetMemoryAttribute (
1111 IN PHYSICAL_ADDRESS Address
1118 UINT64 TempMtrrType
;
1119 MTRR_MEMORY_CACHE_TYPE CacheType
;
1120 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1121 UINT64 MtrrValidBitsMask
;
1122 UINT64 MtrrValidAddressMask
;
1123 UINTN VariableMtrrCount
;
1124 MTRR_VARIABLE_SETTINGS VariableSettings
;
1126 if (!IsMtrrSupported ()) {
1127 return CacheUncacheable
;
1131 // Check if MTRR is enabled, if not, return UC as attribute
1133 TempQword
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1134 MtrrType
= MTRR_CACHE_INVALID_TYPE
;
1136 if ((TempQword
& MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1137 return CacheUncacheable
;
1141 // If address is less than 1M, then try to go through the fixed MTRR
1143 if (Address
< BASE_1MB
) {
1144 if ((TempQword
& MTRR_LIB_CACHE_FIXED_MTRR_ENABLED
) != 0) {
1146 // Go through the fixed MTRR
1148 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1149 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
1151 mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
1152 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)
1156 ((UINTN
)Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
1157 mMtrrLibFixedMtrrTable
[Index
].Length
;
1158 TempQword
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
1159 MtrrType
= RShiftU64 (TempQword
, SubIndex
* 8) & 0xFF;
1160 return GetMemoryCacheTypeFromMtrrType (MtrrType
);
1165 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1167 MtrrGetVariableMtrrWorker (
1168 GetVariableMtrrCountWorker (),
1172 MtrrGetMemoryAttributeInVariableMtrrWorker (
1174 GetFirmwareVariableMtrrCountWorker (),
1176 MtrrValidAddressMask
,
1181 // Go through the variable MTRR
1183 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1184 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1186 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1187 if (VariableMtrr
[Index
].Valid
) {
1188 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
1189 Address
< VariableMtrr
[Index
].BaseAddress
+VariableMtrr
[Index
].Length
) {
1190 TempMtrrType
= VariableMtrr
[Index
].Type
;
1191 MtrrType
= MtrrPrecedence (MtrrType
, TempMtrrType
);
1195 CacheType
= GetMemoryCacheTypeFromMtrrType (MtrrType
);
1203 This function prints all MTRRs for debugging.
1207 MtrrDebugPrintAllMtrrs (
1212 MTRR_SETTINGS MtrrSettings
;
1215 UINTN VariableMtrrCount
;
1223 UINT64 NoRangeLimit
;
1226 UINTN PreviousMemoryType
;
1229 if (!IsMtrrSupported ()) {
1233 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
1234 DEBUG((DEBUG_CACHE
, "=============\n"));
1236 MtrrGetAllMtrrs (&MtrrSettings
);
1237 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", MtrrSettings
.MtrrDefType
));
1238 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1239 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, MtrrSettings
.Fixed
.Mtrr
[Index
]));
1242 VariableMtrrCount
= GetVariableMtrrCount ();
1243 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1244 DEBUG((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
1246 MtrrSettings
.Variables
.Mtrr
[Index
].Base
,
1247 MtrrSettings
.Variables
.Mtrr
[Index
].Mask
1250 DEBUG((DEBUG_CACHE
, "\n"));
1251 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
1252 DEBUG((DEBUG_CACHE
, "====================================\n"));
1255 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1256 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1257 Base
= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
;
1258 for (Index1
= 0; Index1
< 8; Index1
++) {
1259 MemoryType
= (UINTN
)(RShiftU64 (MtrrSettings
.Fixed
.Mtrr
[Index
], Index1
* 8) & 0xff);
1260 if (MemoryType
> CacheWriteBack
) {
1261 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1263 if (MemoryType
!= PreviousMemoryType
) {
1264 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1265 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1267 PreviousMemoryType
= MemoryType
;
1268 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1270 Base
+= mMtrrLibFixedMtrrTable
[Index
].Length
;
1273 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1275 VariableMtrrCount
= GetVariableMtrrCount ();
1278 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
1279 if (RegEax
>= 0x80000008) {
1280 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
1281 Limit
= LShiftU64 (1, RegEax
& 0xff) - 1;
1284 PreviousMemoryType
= MTRR_CACHE_INVALID_TYPE
;
1286 MemoryType
= MtrrGetMemoryAttribute (Base
);
1287 if (MemoryType
> CacheWriteBack
) {
1288 MemoryType
= MTRR_CACHE_INVALID_TYPE
;
1291 if (MemoryType
!= PreviousMemoryType
) {
1292 if (PreviousMemoryType
!= MTRR_CACHE_INVALID_TYPE
) {
1293 DEBUG((DEBUG_CACHE
, "%016lx\n", Base
- 1));
1295 PreviousMemoryType
= MemoryType
;
1296 DEBUG((DEBUG_CACHE
, "%a:%016lx-", mMtrrMemoryCacheTypeShortName
[MemoryType
], Base
));
1299 RangeBase
= BASE_1MB
;
1300 NoRangeBase
= BASE_1MB
;
1302 NoRangeLimit
= Limit
;
1304 for (Index
= 0, Found
= FALSE
; Index
< VariableMtrrCount
; Index
++) {
1305 if ((MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
1307 // If mask is not valid, then do not display range
1311 MtrrBase
= (MtrrSettings
.Variables
.Mtrr
[Index
].Base
& (~(SIZE_4KB
- 1)));
1312 MtrrLimit
= MtrrBase
+ ((~(MtrrSettings
.Variables
.Mtrr
[Index
].Mask
& (~(SIZE_4KB
- 1)))) & Limit
);
1314 if (Base
>= MtrrBase
&& Base
< MtrrLimit
) {
1318 if (Base
>= MtrrBase
&& MtrrBase
> RangeBase
) {
1319 RangeBase
= MtrrBase
;
1321 if (Base
> MtrrLimit
&& MtrrLimit
> RangeBase
) {
1322 RangeBase
= MtrrLimit
+ 1;
1324 if (Base
< MtrrBase
&& MtrrBase
< RangeLimit
) {
1325 RangeLimit
= MtrrBase
- 1;
1327 if (Base
< MtrrLimit
&& MtrrLimit
<= RangeLimit
) {
1328 RangeLimit
= MtrrLimit
;
1331 if (Base
> MtrrLimit
&& NoRangeBase
< MtrrLimit
) {
1332 NoRangeBase
= MtrrLimit
+ 1;
1334 if (Base
< MtrrBase
&& NoRangeLimit
> MtrrBase
) {
1335 NoRangeLimit
= MtrrBase
- 1;
1340 Base
= RangeLimit
+ 1;
1342 Base
= NoRangeLimit
+ 1;
1344 } while (Base
< Limit
);
1345 DEBUG((DEBUG_CACHE
, "%016lx\n\n", Base
- 1));
1349 This function attempts to set the attributes for a memory range.
1351 @param[in] BaseAddress The physical address that is the start
1352 address of a memory region.
1353 @param[in] Length The size in bytes of the memory region.
1354 @param[in] Attribute The bit mask of attributes to set for the
1357 @retval RETURN_SUCCESS The attributes were set for the memory
1359 @retval RETURN_INVALID_PARAMETER Length is zero.
1360 @retval RETURN_UNSUPPORTED The processor does not support one or
1361 more bytes of the memory resource range
1362 specified by BaseAddress and Length.
1363 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
1364 for the memory resource range specified
1365 by BaseAddress and Length.
1366 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
1367 range specified by BaseAddress and Length
1369 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
1370 modify the attributes of the memory
1376 MtrrSetMemoryAttribute (
1377 IN PHYSICAL_ADDRESS BaseAddress
,
1379 IN MTRR_MEMORY_CACHE_TYPE Attribute
1383 RETURN_STATUS Status
;
1390 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
1392 UINT64 MtrrValidBitsMask
;
1393 UINT64 MtrrValidAddressMask
;
1394 BOOLEAN OverwriteExistingMtrr
;
1395 UINT32 FirmwareVariableMtrrCount
;
1396 UINT32 VariableMtrrEnd
;
1397 MTRR_CONTEXT MtrrContext
;
1398 BOOLEAN MtrrContextValid
;
1399 BOOLEAN FixedSettingsValid
[MTRR_NUMBER_OF_FIXED_MTRR
];
1400 BOOLEAN FixedSettingsModified
[MTRR_NUMBER_OF_FIXED_MTRR
];
1401 MTRR_FIXED_SETTINGS WorkingFixedSettings
;
1402 UINT32 VariableMtrrCount
;
1403 MTRR_VARIABLE_SETTINGS OriginalVariableSettings
;
1404 MTRR_VARIABLE_SETTINGS WorkingVariableSettings
;
1409 MTRR_VARIABLE_SETTINGS
*VariableSettings
;
1411 DEBUG((DEBUG_CACHE
, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, Length
));
1412 MtrrContextValid
= FALSE
;
1413 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1414 FixedSettingsValid
[Index
] = FALSE
;
1415 FixedSettingsModified
[Index
] = FALSE
;
1418 if (!IsMtrrSupported ()) {
1419 Status
= RETURN_UNSUPPORTED
;
1423 FirmwareVariableMtrrCount
= GetFirmwareVariableMtrrCountWorker ();
1424 VariableMtrrEnd
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (2 * GetVariableMtrrCount ()) - 1;
1426 MtrrLibInitializeMtrrMask(&MtrrValidBitsMask
, &MtrrValidAddressMask
);
1429 MemoryType
= (UINT64
)Attribute
;
1430 OverwriteExistingMtrr
= FALSE
;
1433 // Check for an invalid parameter
1436 Status
= RETURN_INVALID_PARAMETER
;
1441 (BaseAddress
& ~MtrrValidAddressMask
) != 0 ||
1442 (Length
& ~MtrrValidAddressMask
) != 0
1444 Status
= RETURN_UNSUPPORTED
;
1449 // Check if Fixed MTRR
1451 Status
= RETURN_SUCCESS
;
1452 if (BaseAddress
< BASE_1MB
) {
1453 while ((BaseAddress
< BASE_1MB
) && (Length
> 0) && Status
== RETURN_SUCCESS
) {
1454 Status
= ProgramFixedMtrr (MemoryType
, &BaseAddress
, &Length
, &MsrNum
, &ClearMask
, &OrMask
);
1455 if (RETURN_ERROR (Status
)) {
1458 if (!FixedSettingsValid
[MsrNum
]) {
1459 WorkingFixedSettings
.Mtrr
[MsrNum
] = AsmReadMsr64 (mMtrrLibFixedMtrrTable
[MsrNum
].Msr
);
1460 FixedSettingsValid
[MsrNum
] = TRUE
;
1462 NewValue
= (WorkingFixedSettings
.Mtrr
[MsrNum
] & ~ClearMask
) | OrMask
;
1463 if (WorkingFixedSettings
.Mtrr
[MsrNum
] != NewValue
) {
1464 WorkingFixedSettings
.Mtrr
[MsrNum
] = NewValue
;
1465 FixedSettingsModified
[MsrNum
] = TRUE
;
1471 // A Length of 0 can only make sense for fixed MTTR ranges.
1472 // Since we just handled the fixed MTRRs, we can skip the
1473 // variable MTRR section.
1480 // Since memory ranges below 1MB will be overridden by the fixed MTRRs,
1481 // we can set the base to 0 to save variable MTRRs.
1483 if (BaseAddress
== BASE_1MB
) {
1489 // Read all variable MTRRs
1491 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1492 MtrrGetVariableMtrrWorker (VariableMtrrCount
, &OriginalVariableSettings
);
1493 CopyMem (&WorkingVariableSettings
, &OriginalVariableSettings
, sizeof (WorkingVariableSettings
));
1494 VariableSettings
= &WorkingVariableSettings
;
1497 // Check for overlap
1499 UsedMtrr
= MtrrGetMemoryAttributeInVariableMtrrWorker (
1501 FirmwareVariableMtrrCount
,
1503 MtrrValidAddressMask
,
1506 OverLap
= CheckMemoryAttributeOverlap (
1507 FirmwareVariableMtrrCount
,
1509 BaseAddress
+ Length
- 1,
1514 Status
= CombineMemoryAttribute (
1515 FirmwareVariableMtrrCount
,
1521 &OverwriteExistingMtrr
1523 if (RETURN_ERROR (Status
)) {
1529 // Combined successfully, invalidate the now-unused MTRRs
1531 InvalidateMtrr(VariableMtrrCount
, VariableMtrr
);
1532 Status
= RETURN_SUCCESS
;
1538 // The memory type is the same with the type specified by
1539 // MTRR_LIB_IA32_MTRR_DEF_TYPE.
1541 if ((!OverwriteExistingMtrr
) && (Attribute
== MtrrGetDefaultMemoryType ())) {
1543 // Invalidate the now-unused MTRRs
1545 InvalidateMtrr(VariableMtrrCount
, VariableMtrr
);
1549 Positive
= GetMtrrNumberAndDirection (BaseAddress
, Length
, &MtrrNumber
);
1551 if ((UsedMtrr
+ MtrrNumber
) > FirmwareVariableMtrrCount
) {
1552 Status
= RETURN_OUT_OF_RESOURCES
;
1557 // Invalidate the now-unused MTRRs
1559 InvalidateMtrr(VariableMtrrCount
, VariableMtrr
);
1562 // Find first unused MTRR
1564 for (MsrNum
= MTRR_LIB_IA32_VARIABLE_MTRR_BASE
;
1565 MsrNum
< VariableMtrrEnd
;
1568 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1573 if (BaseAddress
!= 0) {
1576 // Calculate the alignment of the base address.
1578 Alignment
= LShiftU64 (1, (UINTN
)LowBitSet64 (BaseAddress
));
1580 if (Alignment
> Length
) {
1587 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1588 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1593 ProgramVariableMtrr (
1598 MtrrValidAddressMask
1600 BaseAddress
+= Alignment
;
1601 Length
-= Alignment
;
1612 Length
= Power2MaxMemory (LShiftU64 (TempQword
, 1));
1617 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1618 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1623 ProgramVariableMtrr (
1628 MtrrValidAddressMask
1630 BaseAddress
+= Length
;
1631 TempQword
= Length
- TempQword
;
1632 MemoryType
= MTRR_CACHE_UNCACHEABLE
;
1639 for (; MsrNum
< VariableMtrrEnd
; MsrNum
+= 2) {
1640 if ((AsmReadMsr64 (MsrNum
+ 1) & MTRR_LIB_CACHE_MTRR_ENABLED
) == 0) {
1645 Length
= Power2MaxMemory (TempQword
);
1647 BaseAddress
-= Length
;
1650 ProgramVariableMtrr (
1655 MtrrValidAddressMask
1659 BaseAddress
+= Length
;
1661 TempQword
-= Length
;
1663 } while (TempQword
> 0);
1668 // Write fixed MTRRs that have been modified
1670 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1671 if (FixedSettingsModified
[Index
]) {
1672 if (!MtrrContextValid
) {
1673 PreMtrrChange (&MtrrContext
);
1674 MtrrContextValid
= TRUE
;
1677 mMtrrLibFixedMtrrTable
[Index
].Msr
,
1678 WorkingFixedSettings
.Mtrr
[Index
]
1683 if (MtrrContextValid
) {
1684 PostMtrrChange (&MtrrContext
);
1687 DEBUG((DEBUG_CACHE
, " Status = %r\n", Status
));
1688 if (!RETURN_ERROR (Status
)) {
1689 MtrrDebugPrintAllMtrrs ();
1695 Worker function setting variable MTRRs
1697 @param[in] VariableSettings A buffer to hold variable MTRRs content.
1701 MtrrSetVariableMtrrWorker (
1702 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1706 UINT32 VariableMtrrCount
;
1708 VariableMtrrCount
= GetVariableMtrrCountWorker ();
1709 ASSERT (VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
);
1711 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1713 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1),
1714 VariableSettings
->Mtrr
[Index
].Base
1717 MTRR_LIB_IA32_VARIABLE_MTRR_BASE
+ (Index
<< 1) + 1,
1718 VariableSettings
->Mtrr
[Index
].Mask
1725 This function sets variable MTRRs
1727 @param[in] VariableSettings A buffer to hold variable MTRRs content.
1729 @return The pointer of VariableSettings
1732 MTRR_VARIABLE_SETTINGS
*
1734 MtrrSetVariableMtrr (
1735 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
1738 MTRR_CONTEXT MtrrContext
;
1740 if (!IsMtrrSupported ()) {
1741 return VariableSettings
;
1744 PreMtrrChange (&MtrrContext
);
1745 MtrrSetVariableMtrrWorker (VariableSettings
);
1746 PostMtrrChange (&MtrrContext
);
1747 return VariableSettings
;
1751 Worker function setting fixed MTRRs
1753 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
1757 MtrrSetFixedMtrrWorker (
1758 IN MTRR_FIXED_SETTINGS
*FixedSettings
1763 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
1765 mMtrrLibFixedMtrrTable
[Index
].Msr
,
1766 FixedSettings
->Mtrr
[Index
]
1773 This function sets fixed MTRRs
1775 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
1777 @retval The pointer of FixedSettings
1780 MTRR_FIXED_SETTINGS
*
1783 IN MTRR_FIXED_SETTINGS
*FixedSettings
1786 MTRR_CONTEXT MtrrContext
;
1788 if (!IsMtrrSupported ()) {
1789 return FixedSettings
;
1792 PreMtrrChange (&MtrrContext
);
1793 MtrrSetFixedMtrrWorker (FixedSettings
);
1794 PostMtrrChange (&MtrrContext
);
1796 return FixedSettings
;
1801 This function gets the content in all MTRRs (variable and fixed)
1803 @param[out] MtrrSetting A buffer to hold all MTRRs content.
1805 @retval the pointer of MtrrSetting
1811 OUT MTRR_SETTINGS
*MtrrSetting
1814 if (!IsMtrrSupported ()) {
1821 MtrrGetFixedMtrrWorker (&MtrrSetting
->Fixed
);
1824 // Get variable MTRRs
1826 MtrrGetVariableMtrrWorker (
1827 GetVariableMtrrCountWorker (),
1828 &MtrrSetting
->Variables
1832 // Get MTRR_DEF_TYPE value
1834 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
);
1841 This function sets all MTRRs (variable and fixed)
1843 @param[in] MtrrSetting A buffer holding all MTRRs content.
1845 @retval The pointer of MtrrSetting
1851 IN MTRR_SETTINGS
*MtrrSetting
1854 MTRR_CONTEXT MtrrContext
;
1856 if (!IsMtrrSupported ()) {
1860 PreMtrrChange (&MtrrContext
);
1865 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
1868 // Set variable MTRRs
1870 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
1873 // Set MTRR_DEF_TYPE value
1875 AsmWriteMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
1877 PostMtrrChangeEnableCache (&MtrrContext
);
1883 Checks if MTRR is supported.
1885 @retval TRUE MTRR is supported.
1886 @retval FALSE MTRR is not supported.
1899 // Check CPUID(1).EDX[12] for MTRR capability
1901 AsmCpuid (1, NULL
, NULL
, NULL
, &RegEdx
);
1902 if (BitFieldRead32 (RegEdx
, 12, 12) == 0) {
1907 // Check IA32_MTRRCAP.[0..7] for number of variable MTRRs and IA32_MTRRCAP[8] for
1908 // fixed MTRRs existence. If number of variable MTRRs is zero, or fixed MTRRs do not
1909 // exist, return false.
1911 MtrrCap
= AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP
);
1912 if ((BitFieldRead64 (MtrrCap
, 0, 7) == 0) || (BitFieldRead64 (MtrrCap
, 8, 8) == 0)) {