5 Most of services in this library instance are suggested to be invoked by BSP only,
6 except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.
8 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Register/Cpuid.h>
21 #include <Register/Msr.h>
23 #include <Library/MtrrLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/CpuLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/DebugLib.h>
29 #define OR_SEED 0x0101010101010101ull
30 #define CLEAR_SEED 0xFFFFFFFFFFFFFFFFull
31 #define MAX_WEIGHT MAX_UINT8
32 #define SCRATCH_BUFFER_SIZE (4 * SIZE_4KB)
33 #define MTRR_LIB_ASSERT_ALIGNED(B, L) ASSERT ((B & ~(L - 1)) == B);
35 #define M(x,y) ((x) * VectorCount + (y))
36 #define O(x,y) ((y) * VectorCount + (x))
39 // Context to save and restore when MTRRs are programmed
43 BOOLEAN InterruptState
;
53 // Temprary use for calculating the best MTRR settings.
61 // This table defines the offset, base and length of the fixed MTRRs
63 CONST FIXED_MTRR mMtrrLibFixedMtrrTable
[] = {
65 MSR_IA32_MTRR_FIX64K_00000
,
70 MSR_IA32_MTRR_FIX16K_80000
,
75 MSR_IA32_MTRR_FIX16K_A0000
,
80 MSR_IA32_MTRR_FIX4K_C0000
,
85 MSR_IA32_MTRR_FIX4K_C8000
,
90 MSR_IA32_MTRR_FIX4K_D0000
,
95 MSR_IA32_MTRR_FIX4K_D8000
,
100 MSR_IA32_MTRR_FIX4K_E0000
,
105 MSR_IA32_MTRR_FIX4K_E8000
,
110 MSR_IA32_MTRR_FIX4K_F0000
,
115 MSR_IA32_MTRR_FIX4K_F8000
,
122 // Lookup table used to print MTRRs
124 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mMtrrMemoryCacheTypeShortName
[] = {
125 "UC", // CacheUncacheable
126 "WC", // CacheWriteCombining
129 "WT", // CacheWriteThrough
130 "WP", // CacheWriteProtected
131 "WB", // CacheWriteBack
137 Worker function prints all MTRRs for debugging.
139 If MtrrSetting is not NULL, print MTRR settings from input MTRR
141 If MtrrSetting is NULL, print MTRR settings from MTRRs.
143 @param MtrrSetting A buffer holding all MTRRs content.
146 MtrrDebugPrintAllMtrrsWorker (
147 IN MTRR_SETTINGS
*MtrrSetting
151 Worker function returns the variable MTRR count for the CPU.
153 @return Variable MTRR count
157 GetVariableMtrrCountWorker (
161 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
163 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
164 ASSERT (MtrrCap
.Bits
.VCNT
<= ARRAY_SIZE (((MTRR_VARIABLE_SETTINGS
*) 0)->Mtrr
));
165 return MtrrCap
.Bits
.VCNT
;
169 Returns the variable MTRR count for the CPU.
171 @return Variable MTRR count
176 GetVariableMtrrCount (
180 if (!IsMtrrSupported ()) {
183 return GetVariableMtrrCountWorker ();
187 Worker function returns the firmware usable variable MTRR count for the CPU.
189 @return Firmware usable variable MTRR count
193 GetFirmwareVariableMtrrCountWorker (
197 UINT32 VariableMtrrCount
;
198 UINT32 ReservedMtrrNumber
;
200 VariableMtrrCount
= GetVariableMtrrCountWorker ();
201 ReservedMtrrNumber
= PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
202 if (VariableMtrrCount
< ReservedMtrrNumber
) {
206 return VariableMtrrCount
- ReservedMtrrNumber
;
210 Returns the firmware usable variable MTRR count for the CPU.
212 @return Firmware usable variable MTRR count
217 GetFirmwareVariableMtrrCount (
221 if (!IsMtrrSupported ()) {
224 return GetFirmwareVariableMtrrCountWorker ();
228 Worker function returns the default MTRR cache type for the system.
230 If MtrrSetting is not NULL, returns the default MTRR cache type from input
231 MTRR settings buffer.
232 If MtrrSetting is NULL, returns the default MTRR cache type from MSR.
234 @param[in] MtrrSetting A buffer holding all MTRRs content.
236 @return The default MTRR cache type.
239 MTRR_MEMORY_CACHE_TYPE
240 MtrrGetDefaultMemoryTypeWorker (
241 IN MTRR_SETTINGS
*MtrrSetting
244 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
246 if (MtrrSetting
== NULL
) {
247 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
249 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
252 return (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
257 Returns the default MTRR cache type for the system.
259 @return The default MTRR cache type.
262 MTRR_MEMORY_CACHE_TYPE
264 MtrrGetDefaultMemoryType (
268 if (!IsMtrrSupported ()) {
269 return CacheUncacheable
;
271 return MtrrGetDefaultMemoryTypeWorker (NULL
);
275 Preparation before programming MTRR.
277 This function will do some preparation for programming MTRRs:
278 disable cache, invalid cache and disable MTRR caching functionality
280 @param[out] MtrrContext Pointer to context to save
284 MtrrLibPreMtrrChange (
285 OUT MTRR_CONTEXT
*MtrrContext
288 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
290 // Disable interrupts and save current interrupt state
292 MtrrContext
->InterruptState
= SaveAndDisableInterrupts();
295 // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29)
300 // Save original CR4 value and clear PGE flag (Bit 7)
302 MtrrContext
->Cr4
= AsmReadCr4 ();
303 AsmWriteCr4 (MtrrContext
->Cr4
& (~BIT7
));
313 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
315 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, DefType
.Uint64
);
319 Cleaning up after programming MTRRs.
321 This function will do some clean up after programming MTRRs:
322 Flush all TLBs, re-enable caching, restore CR4.
324 @param[in] MtrrContext Pointer to context to restore
328 MtrrLibPostMtrrChangeEnableCache (
329 IN MTRR_CONTEXT
*MtrrContext
338 // Enable Normal Mode caching CD=NW=0, CD(Bit30), NW(Bit29)
343 // Restore original CR4 value
345 AsmWriteCr4 (MtrrContext
->Cr4
);
348 // Restore original interrupt state
350 SetInterruptState (MtrrContext
->InterruptState
);
354 Cleaning up after programming MTRRs.
356 This function will do some clean up after programming MTRRs:
357 enable MTRR caching functionality, and enable cache
359 @param[in] MtrrContext Pointer to context to restore
363 MtrrLibPostMtrrChange (
364 IN MTRR_CONTEXT
*MtrrContext
367 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
371 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
374 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, DefType
.Uint64
);
376 MtrrLibPostMtrrChangeEnableCache (MtrrContext
);
380 Worker function gets the content in fixed MTRRs
382 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
384 @retval The pointer of FixedSettings
388 MtrrGetFixedMtrrWorker (
389 OUT MTRR_FIXED_SETTINGS
*FixedSettings
394 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
395 FixedSettings
->Mtrr
[Index
] =
396 AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
399 return FixedSettings
;
404 This function gets the content in fixed MTRRs
406 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
408 @retval The pointer of FixedSettings
414 OUT MTRR_FIXED_SETTINGS
*FixedSettings
417 if (!IsMtrrSupported ()) {
418 return FixedSettings
;
421 return MtrrGetFixedMtrrWorker (FixedSettings
);
426 Worker function will get the raw value in variable MTRRs
428 If MtrrSetting is not NULL, gets the variable MTRRs raw value from input
429 MTRR settings buffer.
430 If MtrrSetting is NULL, gets the variable MTRRs raw value from MTRRs.
432 @param[in] MtrrSetting A buffer holding all MTRRs content.
433 @param[in] VariableMtrrCount Number of variable MTRRs.
434 @param[out] VariableSettings A buffer to hold variable MTRRs content.
436 @return The VariableSettings input pointer
439 MTRR_VARIABLE_SETTINGS
*
440 MtrrGetVariableMtrrWorker (
441 IN MTRR_SETTINGS
*MtrrSetting
,
442 IN UINT32 VariableMtrrCount
,
443 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
448 ASSERT (VariableMtrrCount
<= ARRAY_SIZE (VariableSettings
->Mtrr
));
450 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
451 if (MtrrSetting
== NULL
) {
452 VariableSettings
->Mtrr
[Index
].Base
=
453 AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1));
454 VariableSettings
->Mtrr
[Index
].Mask
=
455 AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1));
457 VariableSettings
->Mtrr
[Index
].Base
= MtrrSetting
->Variables
.Mtrr
[Index
].Base
;
458 VariableSettings
->Mtrr
[Index
].Mask
= MtrrSetting
->Variables
.Mtrr
[Index
].Mask
;
462 return VariableSettings
;
466 This function will get the raw value in variable MTRRs
468 @param[out] VariableSettings A buffer to hold variable MTRRs content.
470 @return The VariableSettings input pointer
473 MTRR_VARIABLE_SETTINGS
*
475 MtrrGetVariableMtrr (
476 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
479 if (!IsMtrrSupported ()) {
480 return VariableSettings
;
483 return MtrrGetVariableMtrrWorker (
485 GetVariableMtrrCountWorker (),
491 Programs fixed MTRRs registers.
493 @param[in] Type The memory type to set.
494 @param[in, out] Base The base address of memory range.
495 @param[in, out] Length The length of memory range.
496 @param[in, out] LastMsrIndex On input, the last index of the fixed MTRR MSR to program.
497 On return, the current index of the fixed MTRR MSR to program.
498 @param[out] ClearMask The bits to clear in the fixed MTRR MSR.
499 @param[out] OrMask The bits to set in the fixed MTRR MSR.
501 @retval RETURN_SUCCESS The cache type was updated successfully
502 @retval RETURN_UNSUPPORTED The requested range or cache type was invalid
507 MtrrLibProgramFixedMtrr (
508 IN MTRR_MEMORY_CACHE_TYPE Type
,
510 IN OUT UINT64
*Length
,
511 IN OUT UINT32
*LastMsrIndex
,
512 OUT UINT64
*ClearMask
,
517 UINT32 LeftByteShift
;
518 UINT32 RightByteShift
;
522 // Find the fixed MTRR index to be programmed
524 for (MsrIndex
= *LastMsrIndex
+ 1; MsrIndex
< ARRAY_SIZE (mMtrrLibFixedMtrrTable
); MsrIndex
++) {
525 if ((*Base
>= mMtrrLibFixedMtrrTable
[MsrIndex
].BaseAddress
) &&
528 mMtrrLibFixedMtrrTable
[MsrIndex
].BaseAddress
+
529 (8 * mMtrrLibFixedMtrrTable
[MsrIndex
].Length
)
537 ASSERT (MsrIndex
!= ARRAY_SIZE (mMtrrLibFixedMtrrTable
));
540 // Find the begin offset in fixed MTRR and calculate byte offset of left shift
542 if ((((UINT32
)*Base
- mMtrrLibFixedMtrrTable
[MsrIndex
].BaseAddress
) % mMtrrLibFixedMtrrTable
[MsrIndex
].Length
) != 0) {
544 // Base address should be aligned to the begin of a certain Fixed MTRR range.
546 return RETURN_UNSUPPORTED
;
548 LeftByteShift
= ((UINT32
)*Base
- mMtrrLibFixedMtrrTable
[MsrIndex
].BaseAddress
) / mMtrrLibFixedMtrrTable
[MsrIndex
].Length
;
549 ASSERT (LeftByteShift
< 8);
552 // Find the end offset in fixed MTRR and calculate byte offset of right shift
554 SubLength
= mMtrrLibFixedMtrrTable
[MsrIndex
].Length
* (8 - LeftByteShift
);
555 if (*Length
>= SubLength
) {
558 if (((UINT32
)(*Length
) % mMtrrLibFixedMtrrTable
[MsrIndex
].Length
) != 0) {
560 // Length should be aligned to the end of a certain Fixed MTRR range.
562 return RETURN_UNSUPPORTED
;
564 RightByteShift
= 8 - LeftByteShift
- (UINT32
)(*Length
) / mMtrrLibFixedMtrrTable
[MsrIndex
].Length
;
566 // Update SubLength by actual length
571 *ClearMask
= CLEAR_SEED
;
572 *OrMask
= MultU64x32 (OR_SEED
, (UINT32
) Type
);
574 if (LeftByteShift
!= 0) {
576 // Clear the low bits by LeftByteShift
578 *ClearMask
&= LShiftU64 (*ClearMask
, LeftByteShift
* 8);
579 *OrMask
&= LShiftU64 (*OrMask
, LeftByteShift
* 8);
582 if (RightByteShift
!= 0) {
584 // Clear the high bits by RightByteShift
586 *ClearMask
&= RShiftU64 (*ClearMask
, RightByteShift
* 8);
587 *OrMask
&= RShiftU64 (*OrMask
, RightByteShift
* 8);
590 *Length
-= SubLength
;
593 *LastMsrIndex
= MsrIndex
;
595 return RETURN_SUCCESS
;
600 Worker function gets the attribute of variable MTRRs.
602 This function shadows the content of variable MTRRs into an
603 internal array: VariableMtrr.
605 @param[in] VariableSettings The variable MTRR values to shadow
606 @param[in] VariableMtrrCount The number of variable MTRRs
607 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
608 @param[in] MtrrValidAddressMask The valid address mask for MTRR
609 @param[out] VariableMtrr The array to shadow variable MTRRs content
611 @return Number of MTRRs which has been used.
615 MtrrGetMemoryAttributeInVariableMtrrWorker (
616 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
,
617 IN UINTN VariableMtrrCount
,
618 IN UINT64 MtrrValidBitsMask
,
619 IN UINT64 MtrrValidAddressMask
,
620 OUT VARIABLE_MTRR
*VariableMtrr
626 ZeroMem (VariableMtrr
, sizeof (VARIABLE_MTRR
) * ARRAY_SIZE (VariableSettings
->Mtrr
));
627 for (Index
= 0, UsedMtrr
= 0; Index
< VariableMtrrCount
; Index
++) {
628 if (((MSR_IA32_MTRR_PHYSMASK_REGISTER
*) &VariableSettings
->Mtrr
[Index
].Mask
)->Bits
.V
!= 0) {
629 VariableMtrr
[Index
].Msr
= (UINT32
)Index
;
630 VariableMtrr
[Index
].BaseAddress
= (VariableSettings
->Mtrr
[Index
].Base
& MtrrValidAddressMask
);
631 VariableMtrr
[Index
].Length
=
632 ((~(VariableSettings
->Mtrr
[Index
].Mask
& MtrrValidAddressMask
)) & MtrrValidBitsMask
) + 1;
633 VariableMtrr
[Index
].Type
= (VariableSettings
->Mtrr
[Index
].Base
& 0x0ff);
634 VariableMtrr
[Index
].Valid
= TRUE
;
635 VariableMtrr
[Index
].Used
= TRUE
;
643 Convert variable MTRRs to a RAW MTRR_MEMORY_RANGE array.
644 One MTRR_MEMORY_RANGE element is created for each MTRR setting.
645 The routine doesn't remove the overlap or combine the near-by region.
647 @param[in] VariableSettings The variable MTRR values to shadow
648 @param[in] VariableMtrrCount The number of variable MTRRs
649 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
650 @param[in] MtrrValidAddressMask The valid address mask for MTRR
651 @param[out] VariableMtrr The array to shadow variable MTRRs content
653 @return Number of MTRRs which has been used.
657 MtrrLibGetRawVariableRanges (
658 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
,
659 IN UINTN VariableMtrrCount
,
660 IN UINT64 MtrrValidBitsMask
,
661 IN UINT64 MtrrValidAddressMask
,
662 OUT MTRR_MEMORY_RANGE
*VariableMtrr
668 ZeroMem (VariableMtrr
, sizeof (MTRR_MEMORY_RANGE
) * ARRAY_SIZE (VariableSettings
->Mtrr
));
669 for (Index
= 0, UsedMtrr
= 0; Index
< VariableMtrrCount
; Index
++) {
670 if (((MSR_IA32_MTRR_PHYSMASK_REGISTER
*) &VariableSettings
->Mtrr
[Index
].Mask
)->Bits
.V
!= 0) {
671 VariableMtrr
[Index
].BaseAddress
= (VariableSettings
->Mtrr
[Index
].Base
& MtrrValidAddressMask
);
672 VariableMtrr
[Index
].Length
=
673 ((~(VariableSettings
->Mtrr
[Index
].Mask
& MtrrValidAddressMask
)) & MtrrValidBitsMask
) + 1;
674 VariableMtrr
[Index
].Type
= (MTRR_MEMORY_CACHE_TYPE
)(VariableSettings
->Mtrr
[Index
].Base
& 0x0ff);
682 Gets the attribute of variable MTRRs.
684 This function shadows the content of variable MTRRs into an
685 internal array: VariableMtrr.
687 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
688 @param[in] MtrrValidAddressMask The valid address mask for MTRR
689 @param[out] VariableMtrr The array to shadow variable MTRRs content
691 @return The return value of this parameter indicates the
692 number of MTRRs which has been used.
697 MtrrGetMemoryAttributeInVariableMtrr (
698 IN UINT64 MtrrValidBitsMask
,
699 IN UINT64 MtrrValidAddressMask
,
700 OUT VARIABLE_MTRR
*VariableMtrr
703 MTRR_VARIABLE_SETTINGS VariableSettings
;
705 if (!IsMtrrSupported ()) {
709 MtrrGetVariableMtrrWorker (
711 GetVariableMtrrCountWorker (),
715 return MtrrGetMemoryAttributeInVariableMtrrWorker (
717 GetFirmwareVariableMtrrCountWorker (),
719 MtrrValidAddressMask
,
725 Return the biggest alignment (lowest set bit) of address.
726 The function is equivalent to: 1 << LowBitSet64 (Address).
728 @param Address The address to return the alignment.
729 @param Alignment0 The alignment to return when Address is 0.
731 @return The least alignment of the Address.
734 MtrrLibBiggestAlignment (
743 return Address
& ((~Address
) + 1);
747 Return whether the left MTRR type precedes the right MTRR type.
749 The MTRR type precedence rules are:
750 1. UC precedes any other type
752 For further details, please refer the IA32 Software Developer's Manual,
753 Volume 3, Section "MTRR Precedences".
755 @param Left The left MTRR type.
756 @param Right The right MTRR type.
758 @retval TRUE Left precedes Right.
759 @retval FALSE Left doesn't precede Right.
762 MtrrLibTypeLeftPrecedeRight (
763 IN MTRR_MEMORY_CACHE_TYPE Left
,
764 IN MTRR_MEMORY_CACHE_TYPE Right
767 return (BOOLEAN
) (Left
== CacheUncacheable
|| (Left
== CacheWriteThrough
&& Right
== CacheWriteBack
));
771 Initializes the valid bits mask and valid address mask for MTRRs.
773 This function initializes the valid bits mask and valid address mask for MTRRs.
775 @param[out] MtrrValidBitsMask The mask for the valid bit of the MTRR
776 @param[out] MtrrValidAddressMask The valid address mask for the MTRR
780 MtrrLibInitializeMtrrMask (
781 OUT UINT64
*MtrrValidBitsMask
,
782 OUT UINT64
*MtrrValidAddressMask
785 UINT32 MaxExtendedFunction
;
786 CPUID_VIR_PHY_ADDRESS_SIZE_EAX VirPhyAddressSize
;
789 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &MaxExtendedFunction
, NULL
, NULL
, NULL
);
791 if (MaxExtendedFunction
>= CPUID_VIR_PHY_ADDRESS_SIZE
) {
792 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, &VirPhyAddressSize
.Uint32
, NULL
, NULL
, NULL
);
794 VirPhyAddressSize
.Bits
.PhysicalAddressBits
= 36;
797 *MtrrValidBitsMask
= LShiftU64 (1, VirPhyAddressSize
.Bits
.PhysicalAddressBits
) - 1;
798 *MtrrValidAddressMask
= *MtrrValidBitsMask
& 0xfffffffffffff000ULL
;
803 Determines the real attribute of a memory range.
805 This function is to arbitrate the real attribute of the memory when
806 there are 2 MTRRs covers the same memory range. For further details,
807 please refer the IA32 Software Developer's Manual, Volume 3,
808 Section "MTRR Precedences".
810 @param[in] MtrrType1 The first kind of Memory type
811 @param[in] MtrrType2 The second kind of memory type
814 MTRR_MEMORY_CACHE_TYPE
816 IN MTRR_MEMORY_CACHE_TYPE MtrrType1
,
817 IN MTRR_MEMORY_CACHE_TYPE MtrrType2
820 if (MtrrType1
== MtrrType2
) {
825 MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
) ||
826 MtrrLibTypeLeftPrecedeRight (MtrrType2
, MtrrType1
)
829 if (MtrrLibTypeLeftPrecedeRight (MtrrType1
, MtrrType2
)) {
837 Worker function will get the memory cache type of the specific address.
839 If MtrrSetting is not NULL, gets the memory cache type from input
840 MTRR settings buffer.
841 If MtrrSetting is NULL, gets the memory cache type from MTRRs.
843 @param[in] MtrrSetting A buffer holding all MTRRs content.
844 @param[in] Address The specific address
846 @return Memory cache type of the specific address
849 MTRR_MEMORY_CACHE_TYPE
850 MtrrGetMemoryAttributeByAddressWorker (
851 IN MTRR_SETTINGS
*MtrrSetting
,
852 IN PHYSICAL_ADDRESS Address
855 MSR_IA32_MTRR_DEF_TYPE_REGISTER DefType
;
859 MTRR_MEMORY_CACHE_TYPE MtrrType
;
860 MTRR_MEMORY_RANGE VariableMtrr
[ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
)];
861 UINT64 MtrrValidBitsMask
;
862 UINT64 MtrrValidAddressMask
;
863 UINT32 VariableMtrrCount
;
864 MTRR_VARIABLE_SETTINGS VariableSettings
;
867 // Check if MTRR is enabled, if not, return UC as attribute
869 if (MtrrSetting
== NULL
) {
870 DefType
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
872 DefType
.Uint64
= MtrrSetting
->MtrrDefType
;
875 if (DefType
.Bits
.E
== 0) {
876 return CacheUncacheable
;
880 // If address is less than 1M, then try to go through the fixed MTRR
882 if (Address
< BASE_1MB
) {
883 if (DefType
.Bits
.FE
!= 0) {
885 // Go through the fixed MTRR
887 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
888 if (Address
>= mMtrrLibFixedMtrrTable
[Index
].BaseAddress
&&
889 Address
< mMtrrLibFixedMtrrTable
[Index
].BaseAddress
+
890 (mMtrrLibFixedMtrrTable
[Index
].Length
* 8)) {
892 ((UINTN
) Address
- mMtrrLibFixedMtrrTable
[Index
].BaseAddress
) /
893 mMtrrLibFixedMtrrTable
[Index
].Length
;
894 if (MtrrSetting
== NULL
) {
895 FixedMtrr
= AsmReadMsr64 (mMtrrLibFixedMtrrTable
[Index
].Msr
);
897 FixedMtrr
= MtrrSetting
->Fixed
.Mtrr
[Index
];
899 return (MTRR_MEMORY_CACHE_TYPE
) (RShiftU64 (FixedMtrr
, SubIndex
* 8) & 0xFF);
905 VariableMtrrCount
= GetVariableMtrrCountWorker ();
906 ASSERT (VariableMtrrCount
<= ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
));
907 MtrrGetVariableMtrrWorker (MtrrSetting
, VariableMtrrCount
, &VariableSettings
);
909 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
910 MtrrLibGetRawVariableRanges (
914 MtrrValidAddressMask
,
919 // Go through the variable MTRR
921 MtrrType
= CacheInvalid
;
922 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
923 if (VariableMtrr
[Index
].Length
!= 0) {
924 if (Address
>= VariableMtrr
[Index
].BaseAddress
&&
925 Address
< VariableMtrr
[Index
].BaseAddress
+ VariableMtrr
[Index
].Length
) {
926 if (MtrrType
== CacheInvalid
) {
927 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
;
929 MtrrType
= MtrrLibPrecedence (MtrrType
, (MTRR_MEMORY_CACHE_TYPE
) VariableMtrr
[Index
].Type
);
936 // If there is no MTRR which covers the Address, use the default MTRR type.
938 if (MtrrType
== CacheInvalid
) {
939 MtrrType
= (MTRR_MEMORY_CACHE_TYPE
) DefType
.Bits
.Type
;
947 This function will get the memory cache type of the specific address.
949 This function is mainly for debug purpose.
951 @param[in] Address The specific address
953 @return Memory cache type of the specific address
956 MTRR_MEMORY_CACHE_TYPE
958 MtrrGetMemoryAttribute (
959 IN PHYSICAL_ADDRESS Address
962 if (!IsMtrrSupported ()) {
963 return CacheUncacheable
;
966 return MtrrGetMemoryAttributeByAddressWorker (NULL
, Address
);
970 Update the Ranges array to change the specified range identified by
971 BaseAddress and Length to Type.
973 @param Ranges Array holding memory type settings for all memory regions.
974 @param Capacity The maximum count of memory ranges the array can hold.
975 @param Count Return the new memory range count in the array.
976 @param BaseAddress The base address of the memory range to change type.
977 @param Length The length of the memory range to change type.
978 @param Type The new type of the specified memory range.
980 @retval RETURN_SUCCESS The type of the specified memory range is
981 changed successfully.
982 @retval RETURN_ALREADY_STARTED The type of the specified memory range equals
984 @retval RETURN_OUT_OF_RESOURCES The new type set causes the count of memory
985 range exceeds capacity.
988 MtrrLibSetMemoryType (
989 IN MTRR_MEMORY_RANGE
*Ranges
,
992 IN UINT64 BaseAddress
,
994 IN MTRR_MEMORY_CACHE_TYPE Type
1007 Limit
= BaseAddress
+ Length
;
1008 StartIndex
= *Count
;
1010 for (Index
= 0; Index
< *Count
; Index
++) {
1011 if ((StartIndex
== *Count
) &&
1012 (Ranges
[Index
].BaseAddress
<= BaseAddress
) &&
1013 (BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1015 LengthLeft
= BaseAddress
- Ranges
[Index
].BaseAddress
;
1018 if ((EndIndex
== *Count
) &&
1019 (Ranges
[Index
].BaseAddress
< Limit
) &&
1020 (Limit
<= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)) {
1022 LengthRight
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- Limit
;
1027 ASSERT (StartIndex
!= *Count
&& EndIndex
!= *Count
);
1028 if (StartIndex
== EndIndex
&& Ranges
[StartIndex
].Type
== Type
) {
1029 return RETURN_ALREADY_STARTED
;
1033 // The type change may cause merging with previous range or next range.
1034 // Update the StartIndex, EndIndex, BaseAddress, Length so that following
1035 // logic doesn't need to consider merging.
1037 if (StartIndex
!= 0) {
1038 if (LengthLeft
== 0 && Ranges
[StartIndex
- 1].Type
== Type
) {
1040 Length
+= Ranges
[StartIndex
].Length
;
1041 BaseAddress
-= Ranges
[StartIndex
].Length
;
1044 if (EndIndex
!= (*Count
) - 1) {
1045 if (LengthRight
== 0 && Ranges
[EndIndex
+ 1].Type
== Type
) {
1047 Length
+= Ranges
[EndIndex
].Length
;
1052 // |- 0 -|- 1 -|- 2 -|- 3 -| StartIndex EndIndex DeltaCount Count (Count = 4)
1053 // |++++++++++++++++++| 0 3 1=3-0-2 3
1054 // |+++++++| 0 1 -1=1-0-2 5
1055 // |+| 0 0 -2=0-0-2 6
1056 // |+++| 0 0 -1=0-0-2+1 5
1059 DeltaCount
= EndIndex
- StartIndex
- 2;
1060 if (LengthLeft
== 0) {
1063 if (LengthRight
== 0) {
1066 if (*Count
- DeltaCount
> Capacity
) {
1067 return RETURN_OUT_OF_RESOURCES
;
1071 // Reserve (-DeltaCount) space
1073 CopyMem (&Ranges
[EndIndex
+ 1 - DeltaCount
], &Ranges
[EndIndex
+ 1], (*Count
- EndIndex
- 1) * sizeof (Ranges
[0]));
1074 *Count
-= DeltaCount
;
1076 if (LengthLeft
!= 0) {
1077 Ranges
[StartIndex
].Length
= LengthLeft
;
1080 if (LengthRight
!= 0) {
1081 Ranges
[EndIndex
- DeltaCount
].BaseAddress
= BaseAddress
+ Length
;
1082 Ranges
[EndIndex
- DeltaCount
].Length
= LengthRight
;
1083 Ranges
[EndIndex
- DeltaCount
].Type
= Ranges
[EndIndex
].Type
;
1085 Ranges
[StartIndex
].BaseAddress
= BaseAddress
;
1086 Ranges
[StartIndex
].Length
= Length
;
1087 Ranges
[StartIndex
].Type
= Type
;
1088 return RETURN_SUCCESS
;
1092 Return the number of memory types in range [BaseAddress, BaseAddress + Length).
1094 @param Ranges Array holding memory type settings for all memory regions.
1095 @param RangeCount The count of memory ranges the array holds.
1096 @param BaseAddress Base address.
1097 @param Length Length.
1098 @param Types Return bit mask to indicate all memory types in the specified range.
1100 @retval Number of memory types.
1103 MtrrLibGetNumberOfTypes (
1104 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
1105 IN UINTN RangeCount
,
1106 IN UINT64 BaseAddress
,
1108 IN OUT UINT8
*Types OPTIONAL
1117 for (Index
= 0; Index
< RangeCount
; Index
++) {
1118 if ((Ranges
[Index
].BaseAddress
<= BaseAddress
) &&
1119 (BaseAddress
< Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
)
1121 if ((LocalTypes
& (1 << Ranges
[Index
].Type
)) == 0) {
1122 LocalTypes
|= (UINT8
)(1 << Ranges
[Index
].Type
);
1126 if (BaseAddress
+ Length
> Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1127 Length
-= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- BaseAddress
;
1128 BaseAddress
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
;
1135 if (Types
!= NULL
) {
1136 *Types
= LocalTypes
;
1142 Calculate the least MTRR number from vector Start to Stop and update
1143 the Previous of all vectors from Start to Stop is updated to reflect
1144 how the memory range is covered by MTRR.
1146 @param VectorCount The count of vectors in the graph.
1147 @param Vector Array holding all vectors.
1148 @param Weight 2-dimention array holding weights between vectors.
1149 @param Start Start vector.
1150 @param Stop Stop vector.
1151 @param IncludeOptional TRUE to count the optional weight.
1154 MtrrLibCalculateLeastMtrrs (
1155 IN UINT16 VectorCount
,
1156 IN MTRR_LIB_ADDRESS
*Vector
,
1157 IN OUT CONST UINT8
*Weight
,
1160 IN BOOLEAN IncludeOptional
1169 for (Index
= Start
; Index
<= Stop
; Index
++) {
1170 Vector
[Index
].Visited
= FALSE
;
1171 Vector
[Index
].Previous
= VectorCount
;
1172 Mandatory
= Weight
[M(Start
,Index
)];
1173 Vector
[Index
].Weight
= Mandatory
;
1174 if (Mandatory
!= MAX_WEIGHT
) {
1175 Optional
= IncludeOptional
? Weight
[O(Start
, Index
)] : 0;
1176 Vector
[Index
].Weight
+= Optional
;
1177 ASSERT (Vector
[Index
].Weight
>= Optional
);
1183 while (!Vector
[Stop
].Visited
) {
1185 // Update the weight from the shortest vector to other unvisited vectors
1187 for (Index
= Start
+ 1; Index
<= Stop
; Index
++) {
1188 if (!Vector
[Index
].Visited
) {
1189 Mandatory
= Weight
[M(MinI
, Index
)];
1190 if (Mandatory
!= MAX_WEIGHT
) {
1191 Optional
= IncludeOptional
? Weight
[O(MinI
, Index
)] : 0;
1192 if (MinWeight
+ Mandatory
+ Optional
<= Vector
[Index
].Weight
) {
1193 Vector
[Index
].Weight
= MinWeight
+ Mandatory
+ Optional
;
1194 Vector
[Index
].Previous
= MinI
; // Previous is Start based.
1201 // Find the shortest vector from Start
1204 MinWeight
= MAX_WEIGHT
;
1205 for (Index
= Start
+ 1; Index
<= Stop
; Index
++) {
1206 if (!Vector
[Index
].Visited
&& MinWeight
> Vector
[Index
].Weight
) {
1208 MinWeight
= Vector
[Index
].Weight
;
1213 // Mark the shortest vector from Start as visited
1215 Vector
[MinI
].Visited
= TRUE
;
1220 Append the MTRR setting to MTRR setting array.
1222 @param Mtrrs Array holding all MTRR settings.
1223 @param MtrrCapacity Capacity of the MTRR array.
1224 @param MtrrCount The count of MTRR settings in array.
1225 @param BaseAddress Base address.
1226 @param Length Length.
1227 @param Type Memory type.
1229 @retval RETURN_SUCCESS MTRR setting is appended to array.
1230 @retval RETURN_OUT_OF_RESOURCES Array is full.
1233 MtrrLibAppendVariableMtrr (
1234 IN OUT MTRR_MEMORY_RANGE
*Mtrrs
,
1235 IN UINT32 MtrrCapacity
,
1236 IN OUT UINT32
*MtrrCount
,
1237 IN UINT64 BaseAddress
,
1239 IN MTRR_MEMORY_CACHE_TYPE Type
1242 if (*MtrrCount
== MtrrCapacity
) {
1243 return RETURN_OUT_OF_RESOURCES
;
1246 Mtrrs
[*MtrrCount
].BaseAddress
= BaseAddress
;
1247 Mtrrs
[*MtrrCount
].Length
= Length
;
1248 Mtrrs
[*MtrrCount
].Type
= Type
;
1250 return RETURN_SUCCESS
;
1254 Return the memory type that has the least precedence.
1256 @param TypeBits Bit mask of memory type.
1258 @retval Memory type that has the least precedence.
1260 MTRR_MEMORY_CACHE_TYPE
1267 ASSERT (TypeBits
!= 0);
1268 for (Type
= 7; (INT8
)TypeBits
> 0; Type
--, TypeBits
<<= 1);
1269 return (MTRR_MEMORY_CACHE_TYPE
)Type
;
1273 Return TRUE when the Operand is exactly power of 2.
1275 @retval TRUE Operand is exactly power of 2.
1276 @retval FALSE Operand is not power of 2.
1279 MtrrLibIsPowerOfTwo (
1283 ASSERT (Operand
!= 0);
1284 return (BOOLEAN
) ((Operand
& (Operand
- 1)) == 0);
1288 Calculate the subtractive path from vector Start to Stop.
1290 @param DefaultType Default memory type.
1291 @param A0 Alignment to use when base address is 0.
1292 @param Ranges Array holding memory type settings for all memory regions.
1293 @param RangeCount The count of memory ranges the array holds.
1294 @param VectorCount The count of vectors in the graph.
1295 @param Vector Array holding all vectors.
1296 @param Weight 2-dimention array holding weights between vectors.
1297 @param Start Start vector.
1298 @param Stop Stop vector.
1299 @param Types Type bit mask of memory range from Start to Stop.
1300 @param TypeCount Number of different memory types from Start to Stop.
1301 @param Mtrrs Array holding all MTRR settings.
1302 @param MtrrCapacity Capacity of the MTRR array.
1303 @param MtrrCount The count of MTRR settings in array.
1305 @retval RETURN_SUCCESS The subtractive path is calculated successfully.
1306 @retval RETURN_OUT_OF_RESOURCES The MTRR setting array is full.
1310 MtrrLibCalculateSubtractivePath (
1311 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1313 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
1314 IN UINTN RangeCount
,
1315 IN UINT16 VectorCount
,
1316 IN MTRR_LIB_ADDRESS
*Vector
,
1317 IN OUT UINT8
*Weight
,
1322 IN OUT MTRR_MEMORY_RANGE
*Mtrrs
, OPTIONAL
1323 IN UINT32 MtrrCapacity
, OPTIONAL
1324 IN OUT UINT32
*MtrrCount OPTIONAL
1327 RETURN_STATUS Status
;
1330 UINT8 PrecedentTypes
;
1339 MTRR_MEMORY_CACHE_TYPE LowestType
;
1340 MTRR_MEMORY_CACHE_TYPE LowestPrecedentType
;
1342 Base
= Vector
[Start
].Address
;
1343 Length
= Vector
[Stop
].Address
- Base
;
1345 LowestType
= MtrrLibLowestType (Types
);
1348 // Clear the lowest type (highest bit) to get the precedent types
1350 PrecedentTypes
= ~(1 << LowestType
) & Types
;
1351 LowestPrecedentType
= MtrrLibLowestType (PrecedentTypes
);
1353 if (Mtrrs
== NULL
) {
1354 Weight
[M(Start
, Stop
)] = ((LowestType
== DefaultType
) ? 0 : 1);
1355 Weight
[O(Start
, Stop
)] = ((LowestType
== DefaultType
) ? 1 : 0);
1358 // Add all high level ranges
1361 for (Index
= 0; Index
< RangeCount
; Index
++) {
1365 if ((Base
< Ranges
[Index
].BaseAddress
) || (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
<= Base
)) {
1370 // Base is in the Range[Index]
1372 if (Base
+ Length
> Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1373 SubLength
= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- Base
;
1377 if (((1 << Ranges
[Index
].Type
) & PrecedentTypes
) != 0) {
1379 // Meet a range whose types take precedence.
1380 // Update the [HBase, HBase + HLength) to include the range,
1381 // [HBase, HBase + HLength) may contain sub ranges with 2 different types, and both take precedence.
1383 if (HBase
== MAX_UINT64
) {
1386 HLength
+= SubLength
;
1390 Length
-= SubLength
;
1396 if ((Ranges
[Index
].Type
== LowestType
) || (Length
== 0)) { // meet low type or end
1399 // Add the MTRRs for each high priority type range
1400 // the range[HBase, HBase + HLength) contains only two types.
1401 // We might use positive or subtractive, depending on which way uses less MTRR
1403 for (SubStart
= Start
; SubStart
<= Stop
; SubStart
++) {
1404 if (Vector
[SubStart
].Address
== HBase
) {
1409 for (SubStop
= SubStart
; SubStop
<= Stop
; SubStop
++) {
1410 if (Vector
[SubStop
].Address
== HBase
+ HLength
) {
1414 ASSERT (Vector
[SubStart
].Address
== HBase
);
1415 ASSERT (Vector
[SubStop
].Address
== HBase
+ HLength
);
1417 if ((TypeCount
== 2) || (SubStart
== SubStop
- 1)) {
1419 // add subtractive MTRRs for [HBase, HBase + HLength)
1420 // [HBase, HBase + HLength) contains only one type.
1421 // while - loop is to split the range to MTRR - compliant aligned range.
1423 if (Mtrrs
== NULL
) {
1424 Weight
[M (Start
, Stop
)] += (UINT8
)(SubStop
- SubStart
);
1426 while (SubStart
!= SubStop
) {
1427 Status
= MtrrLibAppendVariableMtrr (
1428 Mtrrs
, MtrrCapacity
, MtrrCount
,
1429 Vector
[SubStart
].Address
, Vector
[SubStart
].Length
, (MTRR_MEMORY_CACHE_TYPE
) Vector
[SubStart
].Type
1431 if (RETURN_ERROR (Status
)) {
1438 ASSERT (TypeCount
== 3);
1439 MtrrLibCalculateLeastMtrrs (VectorCount
, Vector
, Weight
, SubStart
, SubStop
, TRUE
);
1441 if (Mtrrs
== NULL
) {
1442 Weight
[M (Start
, Stop
)] += Vector
[SubStop
].Weight
;
1444 // When we need to collect the optimal path from SubStart to SubStop
1445 while (SubStop
!= SubStart
) {
1447 Pre
= Vector
[Cur
].Previous
;
1450 if (Weight
[M (Pre
, Cur
)] != 0) {
1451 Status
= MtrrLibAppendVariableMtrr (
1452 Mtrrs
, MtrrCapacity
, MtrrCount
,
1453 Vector
[Pre
].Address
, Vector
[Cur
].Address
- Vector
[Pre
].Address
, LowestPrecedentType
1455 if (RETURN_ERROR (Status
)) {
1459 if (Pre
!= Cur
- 1) {
1460 Status
= MtrrLibCalculateSubtractivePath (
1463 VectorCount
, Vector
, Weight
,
1464 Pre
, Cur
, PrecedentTypes
, 2,
1465 Mtrrs
, MtrrCapacity
, MtrrCount
1467 if (RETURN_ERROR (Status
)) {
1476 // Reset HBase, HLength
1482 return RETURN_SUCCESS
;
1486 Calculate MTRR settings to cover the specified memory ranges.
1488 @param DefaultType Default memory type.
1489 @param A0 Alignment to use when base address is 0.
1490 @param Ranges Memory range array holding the memory type
1491 settings for all memory address.
1492 @param RangeCount Count of memory ranges.
1493 @param Scratch A temporary scratch buffer that is used to perform the calculation.
1494 This is an optional parameter that may be NULL.
1495 @param ScratchSize Pointer to the size in bytes of the scratch buffer.
1496 It may be updated to the actual required size when the calculation
1497 needs more scratch buffer.
1498 @param Mtrrs Array holding all MTRR settings.
1499 @param MtrrCapacity Capacity of the MTRR array.
1500 @param MtrrCount The count of MTRR settings in array.
1502 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1503 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1504 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
1507 MtrrLibCalculateMtrrs (
1508 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1510 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
1511 IN UINTN RangeCount
,
1513 IN OUT UINTN
*ScratchSize
,
1514 IN OUT MTRR_MEMORY_RANGE
*Mtrrs
,
1515 IN UINT32 MtrrCapacity
,
1516 IN OUT UINT32
*MtrrCount
1526 MTRR_LIB_ADDRESS
*Vector
;
1530 UINTN RequiredScratchSize
;
1535 RETURN_STATUS Status
;
1537 Base0
= Ranges
[0].BaseAddress
;
1538 Base1
= Ranges
[RangeCount
- 1].BaseAddress
+ Ranges
[RangeCount
- 1].Length
;
1539 MTRR_LIB_ASSERT_ALIGNED (Base0
, Base1
- Base0
);
1542 // Count the number of vectors.
1544 Vector
= (MTRR_LIB_ADDRESS
*)Scratch
;
1545 for (VectorIndex
= 0, Index
= 0; Index
< RangeCount
; Index
++) {
1546 Base
= Ranges
[Index
].BaseAddress
;
1547 Length
= Ranges
[Index
].Length
;
1548 while (Length
!= 0) {
1549 Alignment
= MtrrLibBiggestAlignment (Base
, A0
);
1550 SubLength
= Alignment
;
1551 if (SubLength
> Length
) {
1552 SubLength
= GetPowerOfTwo64 (Length
);
1554 if (VectorIndex
< *ScratchSize
/ sizeof (*Vector
)) {
1555 Vector
[VectorIndex
].Address
= Base
;
1556 Vector
[VectorIndex
].Alignment
= Alignment
;
1557 Vector
[VectorIndex
].Type
= Ranges
[Index
].Type
;
1558 Vector
[VectorIndex
].Length
= SubLength
;
1561 Length
-= SubLength
;
1566 // Vector[VectorIndex] = Base1, so whole vector count is (VectorIndex + 1).
1568 VectorCount
= VectorIndex
+ 1;
1570 DEBUG_CACHE
, "VectorCount (%016lx - %016lx) = %d\n",
1571 Ranges
[0].BaseAddress
, Ranges
[RangeCount
- 1].BaseAddress
+ Ranges
[RangeCount
- 1].Length
, VectorCount
1573 ASSERT (VectorCount
< MAX_UINT16
);
1575 RequiredScratchSize
= VectorCount
* sizeof (*Vector
) + VectorCount
* VectorCount
* sizeof (*Weight
);
1576 if (*ScratchSize
< RequiredScratchSize
) {
1577 *ScratchSize
= RequiredScratchSize
;
1578 return RETURN_BUFFER_TOO_SMALL
;
1580 Vector
[VectorCount
- 1].Address
= Base1
;
1582 Weight
= (UINT8
*) &Vector
[VectorCount
];
1584 // Set mandatory weight between any vector to max
1585 // Set optional weight and between any vector and self->self to 0
1592 for (VectorIndex
= 0; VectorIndex
< VectorCount
; VectorIndex
++) {
1593 SetMem (&Weight
[M(VectorIndex
, 0)], VectorIndex
+ 1, 0);
1594 if (VectorIndex
!= VectorCount
- 1) {
1595 Weight
[M (VectorIndex
, VectorIndex
+ 1)] = (DefaultType
== Vector
[VectorIndex
].Type
) ? 0 : 1;
1596 SetMem (&Weight
[M (VectorIndex
, VectorIndex
+ 2)], VectorCount
- VectorIndex
- 2, MAX_WEIGHT
);
1600 for (TypeCount
= 2; TypeCount
<= 3; TypeCount
++) {
1601 for (Start
= 0; Start
< VectorCount
; Start
++) {
1602 for (Stop
= Start
+ 2; Stop
< VectorCount
; Stop
++) {
1603 ASSERT (Vector
[Stop
].Address
> Vector
[Start
].Address
);
1604 Length
= Vector
[Stop
].Address
- Vector
[Start
].Address
;
1605 if (Length
> Vector
[Start
].Alignment
) {
1607 // Pickup a new Start when [Start, Stop) cannot be described by one MTRR.
1611 if ((Weight
[M(Start
, Stop
)] == MAX_WEIGHT
) && MtrrLibIsPowerOfTwo (Length
)) {
1612 if (MtrrLibGetNumberOfTypes (
1613 Ranges
, RangeCount
, Vector
[Start
].Address
, Vector
[Stop
].Address
- Vector
[Start
].Address
, &Type
1616 // Update the Weight[Start, Stop] using subtractive path.
1618 MtrrLibCalculateSubtractivePath (
1621 (UINT16
)VectorCount
, Vector
, Weight
,
1622 Start
, Stop
, Type
, TypeCount
,
1625 } else if (TypeCount
== 2) {
1627 // Pick up a new Start when we expect 2-type range, but 3-type range is met.
1628 // Because no matter how Stop is increased, we always meet 3-type range.
1637 Status
= RETURN_SUCCESS
;
1638 MtrrLibCalculateLeastMtrrs ((UINT16
) VectorCount
, Vector
, Weight
, 0, (UINT16
) VectorCount
- 1, FALSE
);
1639 Stop
= (UINT16
) VectorCount
- 1;
1641 Start
= Vector
[Stop
].Previous
;
1642 TypeCount
= MAX_UINT8
;
1644 if (Weight
[M(Start
, Stop
)] != 0) {
1645 TypeCount
= MtrrLibGetNumberOfTypes (Ranges
, RangeCount
, Vector
[Start
].Address
, Vector
[Stop
].Address
- Vector
[Start
].Address
, &Type
);
1646 Status
= MtrrLibAppendVariableMtrr (
1647 Mtrrs
, MtrrCapacity
, MtrrCount
,
1648 Vector
[Start
].Address
, Vector
[Stop
].Address
- Vector
[Start
].Address
,
1649 MtrrLibLowestType (Type
)
1651 if (RETURN_ERROR (Status
)) {
1656 if (Start
!= Stop
- 1) {
1658 // substractive path
1660 if (TypeCount
== MAX_UINT8
) {
1661 TypeCount
= MtrrLibGetNumberOfTypes (
1662 Ranges
, RangeCount
, Vector
[Start
].Address
, Vector
[Stop
].Address
- Vector
[Start
].Address
, &Type
1665 Status
= MtrrLibCalculateSubtractivePath (
1668 (UINT16
) VectorCount
, Vector
, Weight
, Start
, Stop
,
1670 Mtrrs
, MtrrCapacity
, MtrrCount
1672 if (RETURN_ERROR (Status
)) {
1683 Apply the variable MTRR settings to memory range array.
1685 @param Fixed The fixed MTRR settings.
1686 @param Ranges Return the memory range array holding memory type
1687 settings for all memory address.
1688 @param RangeCapacity The capacity of memory range array.
1689 @param RangeCount Return the count of memory range.
1691 @retval RETURN_SUCCESS The memory range array is returned successfully.
1692 @retval RETURN_OUT_OF_RESOURCES The count of memory ranges exceeds capacity.
1695 MtrrLibApplyFixedMtrrs (
1696 IN MTRR_FIXED_SETTINGS
*Fixed
,
1697 IN OUT MTRR_MEMORY_RANGE
*Ranges
,
1698 IN UINTN RangeCapacity
,
1699 IN OUT UINTN
*RangeCount
1702 RETURN_STATUS Status
;
1705 MTRR_MEMORY_CACHE_TYPE MemoryType
;
1709 for (MsrIndex
= 0; MsrIndex
< ARRAY_SIZE (mMtrrLibFixedMtrrTable
); MsrIndex
++) {
1710 ASSERT (Base
== mMtrrLibFixedMtrrTable
[MsrIndex
].BaseAddress
);
1711 for (Index
= 0; Index
< sizeof (UINT64
); Index
++) {
1712 MemoryType
= (MTRR_MEMORY_CACHE_TYPE
)((UINT8
*)(&Fixed
->Mtrr
[MsrIndex
]))[Index
];
1713 Status
= MtrrLibSetMemoryType (
1714 Ranges
, RangeCapacity
, RangeCount
, Base
, mMtrrLibFixedMtrrTable
[MsrIndex
].Length
, MemoryType
1716 if (Status
== RETURN_OUT_OF_RESOURCES
) {
1719 Base
+= mMtrrLibFixedMtrrTable
[MsrIndex
].Length
;
1722 ASSERT (Base
== BASE_1MB
);
1723 return RETURN_SUCCESS
;
1727 Apply the variable MTRR settings to memory range array.
1729 @param VariableMtrr The variable MTRR array.
1730 @param VariableMtrrCount The count of variable MTRRs.
1731 @param Ranges Return the memory range array with new MTRR settings applied.
1732 @param RangeCapacity The capacity of memory range array.
1733 @param RangeCount Return the count of memory range.
1735 @retval RETURN_SUCCESS The memory range array is returned successfully.
1736 @retval RETURN_OUT_OF_RESOURCES The count of memory ranges exceeds capacity.
1739 MtrrLibApplyVariableMtrrs (
1740 IN CONST MTRR_MEMORY_RANGE
*VariableMtrr
,
1741 IN UINT32 VariableMtrrCount
,
1742 IN OUT MTRR_MEMORY_RANGE
*Ranges
,
1743 IN UINTN RangeCapacity
,
1744 IN OUT UINTN
*RangeCount
1747 RETURN_STATUS Status
;
1753 // UC > * (except WB, UC) > WB
1759 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1760 if ((VariableMtrr
[Index
].Length
!= 0) && (VariableMtrr
[Index
].Type
== CacheWriteBack
)) {
1761 Status
= MtrrLibSetMemoryType (
1762 Ranges
, RangeCapacity
, RangeCount
,
1763 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, VariableMtrr
[Index
].Type
1765 if (Status
== RETURN_OUT_OF_RESOURCES
) {
1772 // 2. Set other types than WB or UC
1774 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1775 if ((VariableMtrr
[Index
].Length
!= 0) &&
1776 (VariableMtrr
[Index
].Type
!= CacheWriteBack
) && (VariableMtrr
[Index
].Type
!= CacheUncacheable
)) {
1777 Status
= MtrrLibSetMemoryType (
1778 Ranges
, RangeCapacity
, RangeCount
,
1779 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, VariableMtrr
[Index
].Type
1781 if (Status
== RETURN_OUT_OF_RESOURCES
) {
1790 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
1791 if (VariableMtrr
[Index
].Length
!= 0 && VariableMtrr
[Index
].Type
== CacheUncacheable
) {
1792 Status
= MtrrLibSetMemoryType (
1793 Ranges
, RangeCapacity
, RangeCount
,
1794 VariableMtrr
[Index
].BaseAddress
, VariableMtrr
[Index
].Length
, VariableMtrr
[Index
].Type
1796 if (Status
== RETURN_OUT_OF_RESOURCES
) {
1801 return RETURN_SUCCESS
;
1805 Return the memory type bit mask that's compatible to first type in the Ranges.
1807 @param Ranges Memory range array holding the memory type
1808 settings for all memory address.
1809 @param RangeCount Count of memory ranges.
1811 @return Compatible memory type bit mask.
1814 MtrrLibGetCompatibleTypes (
1815 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
1819 ASSERT (RangeCount
!= 0);
1821 switch (Ranges
[0].Type
) {
1822 case CacheWriteBack
:
1823 case CacheWriteThrough
:
1824 return (1 << CacheWriteBack
) | (1 << CacheWriteThrough
) | (1 << CacheUncacheable
);
1827 case CacheWriteCombining
:
1828 case CacheWriteProtected
:
1829 return (1 << Ranges
[0].Type
) | (1 << CacheUncacheable
);
1832 case CacheUncacheable
:
1833 if (RangeCount
== 1) {
1834 return (1 << CacheUncacheable
);
1836 return MtrrLibGetCompatibleTypes (&Ranges
[1], RangeCount
- 1);
1848 Overwrite the destination MTRR settings with the source MTRR settings.
1849 This routine is to make sure the modification to destination MTRR settings
1850 is as small as possible.
1852 @param DstMtrrs Destination MTRR settings.
1853 @param DstMtrrCount Count of destination MTRR settings.
1854 @param SrcMtrrs Source MTRR settings.
1855 @param SrcMtrrCount Count of source MTRR settings.
1856 @param Modified Flag array to indicate which destination MTRR setting is modified.
1859 MtrrLibMergeVariableMtrr (
1860 MTRR_MEMORY_RANGE
*DstMtrrs
,
1861 UINT32 DstMtrrCount
,
1862 MTRR_MEMORY_RANGE
*SrcMtrrs
,
1863 UINT32 SrcMtrrCount
,
1870 ASSERT (SrcMtrrCount
<= DstMtrrCount
);
1872 for (DstIndex
= 0; DstIndex
< DstMtrrCount
; DstIndex
++) {
1873 Modified
[DstIndex
] = FALSE
;
1875 if (DstMtrrs
[DstIndex
].Length
== 0) {
1878 for (SrcIndex
= 0; SrcIndex
< SrcMtrrCount
; SrcIndex
++) {
1879 if (DstMtrrs
[DstIndex
].BaseAddress
== SrcMtrrs
[SrcIndex
].BaseAddress
&&
1880 DstMtrrs
[DstIndex
].Length
== SrcMtrrs
[SrcIndex
].Length
&&
1881 DstMtrrs
[DstIndex
].Type
== SrcMtrrs
[SrcIndex
].Type
) {
1886 if (SrcIndex
== SrcMtrrCount
) {
1888 // Remove the one from DstMtrrs which is not in SrcMtrrs
1890 DstMtrrs
[DstIndex
].Length
= 0;
1891 Modified
[DstIndex
] = TRUE
;
1894 // Remove the one from SrcMtrrs which is also in DstMtrrs
1896 SrcMtrrs
[SrcIndex
].Length
= 0;
1901 // Now valid MTRR only exists in either DstMtrrs or SrcMtrrs.
1902 // Merge MTRRs from SrcMtrrs to DstMtrrs
1905 for (SrcIndex
= 0; SrcIndex
< SrcMtrrCount
; SrcIndex
++) {
1906 if (SrcMtrrs
[SrcIndex
].Length
!= 0) {
1909 // Find the empty slot in DstMtrrs
1911 while (DstIndex
< DstMtrrCount
) {
1912 if (DstMtrrs
[DstIndex
].Length
== 0) {
1917 ASSERT (DstIndex
< DstMtrrCount
);
1918 CopyMem (&DstMtrrs
[DstIndex
], &SrcMtrrs
[SrcIndex
], sizeof (SrcMtrrs
[0]));
1919 Modified
[DstIndex
] = TRUE
;
1925 Calculate the variable MTRR settings for all memory ranges.
1927 @param DefaultType Default memory type.
1928 @param A0 Alignment to use when base address is 0.
1929 @param Ranges Memory range array holding the memory type
1930 settings for all memory address.
1931 @param RangeCount Count of memory ranges.
1932 @param Scratch Scratch buffer to be used in MTRR calculation.
1933 @param ScratchSize Pointer to the size of scratch buffer.
1934 @param VariableMtrr Array holding all MTRR settings.
1935 @param VariableMtrrCapacity Capacity of the MTRR array.
1936 @param VariableMtrrCount The count of MTRR settings in array.
1938 @retval RETURN_SUCCESS Variable MTRRs are allocated successfully.
1939 @retval RETURN_OUT_OF_RESOURCES Count of variable MTRRs exceeds capacity.
1940 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
1941 The required scratch buffer size is returned through ScratchSize.
1944 MtrrLibSetMemoryRanges (
1945 IN MTRR_MEMORY_CACHE_TYPE DefaultType
,
1947 IN MTRR_MEMORY_RANGE
*Ranges
,
1948 IN UINTN RangeCount
,
1950 IN OUT UINTN
*ScratchSize
,
1951 OUT MTRR_MEMORY_RANGE
*VariableMtrr
,
1952 IN UINT32 VariableMtrrCapacity
,
1953 OUT UINT32
*VariableMtrrCount
1956 RETURN_STATUS Status
;
1961 UINT8 CompatibleTypes
;
1964 UINTN ActualScratchSize
;
1965 UINTN BiggestScratchSize
;
1967 *VariableMtrrCount
= 0;
1970 // Since the whole ranges need multiple calls of MtrrLibCalculateMtrrs().
1971 // Each call needs different scratch buffer size.
1972 // When the provided scratch buffer size is not sufficient in any call,
1973 // set the GetActualScratchSize to TRUE, and following calls will only
1974 // calculate the actual scratch size for the caller.
1976 BiggestScratchSize
= 0;
1978 for (Index
= 0; Index
< RangeCount
;) {
1979 Base0
= Ranges
[Index
].BaseAddress
;
1982 // Full step is optimal
1984 while (Index
< RangeCount
) {
1985 ASSERT (Ranges
[Index
].BaseAddress
== Base0
);
1986 Alignment
= MtrrLibBiggestAlignment (Base0
, A0
);
1987 while (Base0
+ Alignment
<= Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
) {
1988 if ((BiggestScratchSize
<= *ScratchSize
) && (Ranges
[Index
].Type
!= DefaultType
)) {
1989 Status
= MtrrLibAppendVariableMtrr (
1990 VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
,
1991 Base0
, Alignment
, Ranges
[Index
].Type
1993 if (RETURN_ERROR (Status
)) {
1998 Alignment
= MtrrLibBiggestAlignment (Base0
, A0
);
2002 // Remove the above range from Ranges[Index]
2004 Ranges
[Index
].Length
-= Base0
- Ranges
[Index
].BaseAddress
;
2005 Ranges
[Index
].BaseAddress
= Base0
;
2006 if (Ranges
[Index
].Length
!= 0) {
2013 if (Index
== RangeCount
) {
2018 // Find continous ranges [Base0, Base1) which could be combined by MTRR.
2019 // Per SDM, the compatible types between[B0, B1) are:
2024 CompatibleTypes
= MtrrLibGetCompatibleTypes (&Ranges
[Index
], RangeCount
- Index
);
2026 End
= Index
; // End points to last one that matches the CompatibleTypes.
2027 while (End
+ 1 < RangeCount
) {
2028 if (((1 << Ranges
[End
+ 1].Type
) & CompatibleTypes
) == 0) {
2033 Alignment
= MtrrLibBiggestAlignment (Base0
, A0
);
2034 Length
= GetPowerOfTwo64 (Ranges
[End
].BaseAddress
+ Ranges
[End
].Length
- Base0
);
2035 Base1
= Base0
+ MIN (Alignment
, Length
);
2038 // Base1 may not in Ranges[End]. Update End to the range Base1 belongs to.
2041 while (End
+ 1 < RangeCount
) {
2042 if (Base1
<= Ranges
[End
+ 1].BaseAddress
) {
2048 Length
= Ranges
[End
].Length
;
2049 Ranges
[End
].Length
= Base1
- Ranges
[End
].BaseAddress
;
2050 ActualScratchSize
= *ScratchSize
;
2051 Status
= MtrrLibCalculateMtrrs (
2053 &Ranges
[Index
], End
+ 1 - Index
,
2054 Scratch
, &ActualScratchSize
,
2055 VariableMtrr
, VariableMtrrCapacity
, VariableMtrrCount
2057 if (Status
== RETURN_BUFFER_TOO_SMALL
) {
2058 BiggestScratchSize
= MAX (BiggestScratchSize
, ActualScratchSize
);
2060 // Ignore this error, because we need to calculate the biggest
2061 // scratch buffer size.
2063 Status
= RETURN_SUCCESS
;
2065 if (RETURN_ERROR (Status
)) {
2069 if (Length
!= Ranges
[End
].Length
) {
2070 Ranges
[End
].BaseAddress
= Base1
;
2071 Ranges
[End
].Length
= Length
- Ranges
[End
].Length
;
2078 if (*ScratchSize
< BiggestScratchSize
) {
2079 *ScratchSize
= BiggestScratchSize
;
2080 return RETURN_BUFFER_TOO_SMALL
;
2082 return RETURN_SUCCESS
;
2086 Set the below-1MB memory attribute to fixed MTRR buffer.
2087 Modified flag array indicates which fixed MTRR is modified.
2089 @param [in, out] FixedSettings Fixed MTRR buffer.
2090 @param [out] Modified Flag array indicating which MTRR is modified.
2091 @param [in] BaseAddress Base address.
2092 @param [in] Length Length.
2093 @param [in] Type Memory type.
2095 @retval RETURN_SUCCESS The memory attribute is set successfully.
2096 @retval RETURN_UNSUPPORTED The requested range or cache type was invalid
2097 for the fixed MTRRs.
2100 MtrrLibSetBelow1MBMemoryAttribute (
2101 IN OUT MTRR_FIXED_SETTINGS
*FixedSettings
,
2102 OUT BOOLEAN
*Modified
,
2103 IN PHYSICAL_ADDRESS BaseAddress
,
2105 IN MTRR_MEMORY_CACHE_TYPE Type
2108 RETURN_STATUS Status
;
2112 UINT64 ClearMasks
[ARRAY_SIZE (mMtrrLibFixedMtrrTable
)];
2113 UINT64 OrMasks
[ARRAY_SIZE (mMtrrLibFixedMtrrTable
)];
2115 ASSERT (BaseAddress
< BASE_1MB
);
2117 MsrIndex
= (UINT32
)-1;
2118 while ((BaseAddress
< BASE_1MB
) && (Length
!= 0)) {
2119 Status
= MtrrLibProgramFixedMtrr (Type
, &BaseAddress
, &Length
, &MsrIndex
, &ClearMask
, &OrMask
);
2120 if (RETURN_ERROR (Status
)) {
2123 ClearMasks
[MsrIndex
] = ClearMask
;
2124 OrMasks
[MsrIndex
] = OrMask
;
2125 Modified
[MsrIndex
] = TRUE
;
2128 for (MsrIndex
= 0; MsrIndex
< ARRAY_SIZE (mMtrrLibFixedMtrrTable
); MsrIndex
++) {
2129 if (Modified
[MsrIndex
]) {
2130 FixedSettings
->Mtrr
[MsrIndex
] = (FixedSettings
->Mtrr
[MsrIndex
] & ~ClearMasks
[MsrIndex
]) | OrMasks
[MsrIndex
];
2133 return RETURN_SUCCESS
;
2137 This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.
2139 @param[in, out] MtrrSetting MTRR setting buffer to be set.
2140 @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.
2141 @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.
2142 It may be updated to the actual required size when the calculation
2143 needs more scratch buffer.
2144 @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.
2145 When range overlap happens, the last one takes higher priority.
2146 When the function returns, either all the attributes are set successfully,
2147 or none of them is set.
2148 @param[in] Count of MTRR_MEMORY_RANGE.
2150 @retval RETURN_SUCCESS The attributes were set for all the memory ranges.
2151 @retval RETURN_INVALID_PARAMETER Length in any range is zero.
2152 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
2153 memory resource range specified by BaseAddress and Length in any range.
2154 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
2155 range specified by BaseAddress and Length in any range.
2156 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
2157 the memory resource ranges.
2158 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
2159 BaseAddress and Length cannot be modified.
2160 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
2164 MtrrSetMemoryAttributesInMtrrSettings (
2165 IN OUT MTRR_SETTINGS
*MtrrSetting
,
2167 IN OUT UINTN
*ScratchSize
,
2168 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
2172 RETURN_STATUS Status
;
2176 BOOLEAN Above1MbExist
;
2178 UINT64 MtrrValidBitsMask
;
2179 UINT64 MtrrValidAddressMask
;
2180 MTRR_MEMORY_CACHE_TYPE DefaultType
;
2181 MTRR_VARIABLE_SETTINGS VariableSettings
;
2182 MTRR_MEMORY_RANGE WorkingRanges
[2 * ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
) + 2];
2183 UINTN WorkingRangeCount
;
2185 MTRR_VARIABLE_SETTING VariableSetting
;
2186 UINT32 OriginalVariableMtrrCount
;
2187 UINT32 FirmwareVariableMtrrCount
;
2188 UINT32 WorkingVariableMtrrCount
;
2189 MTRR_MEMORY_RANGE OriginalVariableMtrr
[ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
)];
2190 MTRR_MEMORY_RANGE WorkingVariableMtrr
[ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
)];
2191 BOOLEAN VariableSettingModified
[ARRAY_SIZE (MtrrSetting
->Variables
.Mtrr
)];
2193 BOOLEAN FixedSettingsModified
[ARRAY_SIZE (mMtrrLibFixedMtrrTable
)];
2194 MTRR_FIXED_SETTINGS WorkingFixedSettings
;
2196 MTRR_CONTEXT MtrrContext
;
2197 BOOLEAN MtrrContextValid
;
2199 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
2202 // TRUE indicating the accordingly Variable setting needs modificaiton in OriginalVariableMtrr.
2204 SetMem (VariableSettingModified
, ARRAY_SIZE (VariableSettingModified
), FALSE
);
2206 // TRUE indicating the accordingly Fixed setting needs modification in WorkingFixedSettings.
2208 SetMem (FixedSettingsModified
, ARRAY_SIZE (FixedSettingsModified
), FALSE
);
2211 // TRUE indicating the caller requests to set variable MTRRs.
2213 Above1MbExist
= FALSE
;
2214 OriginalVariableMtrrCount
= 0;
2217 // 1. Validate the parameters.
2219 for (Index
= 0; Index
< RangeCount
; Index
++) {
2220 if (Ranges
[Index
].Length
== 0) {
2221 return RETURN_INVALID_PARAMETER
;
2223 if (((Ranges
[Index
].BaseAddress
& ~MtrrValidAddressMask
) != 0) ||
2224 ((Ranges
[Index
].Length
& ~MtrrValidAddressMask
) != 0)
2226 return RETURN_UNSUPPORTED
;
2228 if ((Ranges
[Index
].Type
!= CacheUncacheable
) &&
2229 (Ranges
[Index
].Type
!= CacheWriteCombining
) &&
2230 (Ranges
[Index
].Type
!= CacheWriteThrough
) &&
2231 (Ranges
[Index
].Type
!= CacheWriteProtected
) &&
2232 (Ranges
[Index
].Type
!= CacheWriteBack
)) {
2233 return RETURN_INVALID_PARAMETER
;
2235 if (Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
> BASE_1MB
) {
2236 Above1MbExist
= TRUE
;
2241 // 2. Apply the above-1MB memory attribute settings.
2243 if (Above1MbExist
) {
2245 // 2.1. Read all variable MTRRs and convert to Ranges.
2247 OriginalVariableMtrrCount
= GetVariableMtrrCountWorker ();
2248 MtrrGetVariableMtrrWorker (MtrrSetting
, OriginalVariableMtrrCount
, &VariableSettings
);
2249 MtrrLibGetRawVariableRanges (
2250 &VariableSettings
, OriginalVariableMtrrCount
,
2251 MtrrValidBitsMask
, MtrrValidAddressMask
, OriginalVariableMtrr
2254 DefaultType
= MtrrGetDefaultMemoryTypeWorker (MtrrSetting
);
2255 WorkingRangeCount
= 1;
2256 WorkingRanges
[0].BaseAddress
= 0;
2257 WorkingRanges
[0].Length
= MtrrValidBitsMask
+ 1;
2258 WorkingRanges
[0].Type
= DefaultType
;
2260 Status
= MtrrLibApplyVariableMtrrs (
2261 OriginalVariableMtrr
, OriginalVariableMtrrCount
,
2262 WorkingRanges
, ARRAY_SIZE (WorkingRanges
), &WorkingRangeCount
);
2263 ASSERT_RETURN_ERROR (Status
);
2265 ASSERT (OriginalVariableMtrrCount
>= PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
));
2266 FirmwareVariableMtrrCount
= OriginalVariableMtrrCount
- PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
2267 ASSERT (WorkingRangeCount
<= 2 * FirmwareVariableMtrrCount
+ 1);
2270 // 2.2. Force [0, 1M) to UC, so that it doesn't impact subtraction algorithm.
2272 Status
= MtrrLibSetMemoryType (
2273 WorkingRanges
, ARRAY_SIZE (WorkingRanges
), &WorkingRangeCount
,
2274 0, SIZE_1MB
, CacheUncacheable
2276 ASSERT (Status
!= RETURN_OUT_OF_RESOURCES
);
2279 // 2.3. Apply the new memory attribute settings to Ranges.
2282 for (Index
= 0; Index
< RangeCount
; Index
++) {
2283 BaseAddress
= Ranges
[Index
].BaseAddress
;
2284 Length
= Ranges
[Index
].Length
;
2285 if (BaseAddress
< BASE_1MB
) {
2286 if (Length
<= BASE_1MB
- BaseAddress
) {
2289 Length
-= BASE_1MB
- BaseAddress
;
2290 BaseAddress
= BASE_1MB
;
2292 Status
= MtrrLibSetMemoryType (
2293 WorkingRanges
, ARRAY_SIZE (WorkingRanges
), &WorkingRangeCount
,
2294 BaseAddress
, Length
, Ranges
[Index
].Type
2296 if (Status
== RETURN_ALREADY_STARTED
) {
2297 Status
= RETURN_SUCCESS
;
2298 } else if (Status
== RETURN_OUT_OF_RESOURCES
) {
2301 ASSERT_RETURN_ERROR (Status
);
2308 // 2.4. Calculate the Variable MTRR settings based on the Ranges.
2309 // Buffer Too Small may be returned if the scratch buffer size is insufficient.
2311 Status
= MtrrLibSetMemoryRanges (
2312 DefaultType
, LShiftU64 (1, (UINTN
)HighBitSet64 (MtrrValidBitsMask
)), WorkingRanges
, WorkingRangeCount
,
2313 Scratch
, ScratchSize
,
2314 WorkingVariableMtrr
, FirmwareVariableMtrrCount
+ 1, &WorkingVariableMtrrCount
2316 if (RETURN_ERROR (Status
)) {
2321 // 2.5. Remove the [0, 1MB) MTRR if it still exists (not merged with other range)
2323 for (Index
= 0; Index
< WorkingVariableMtrrCount
; Index
++) {
2324 if (WorkingVariableMtrr
[Index
].BaseAddress
== 0 && WorkingVariableMtrr
[Index
].Length
== SIZE_1MB
) {
2325 ASSERT (WorkingVariableMtrr
[Index
].Type
== CacheUncacheable
);
2326 WorkingVariableMtrrCount
--;
2328 &WorkingVariableMtrr
[Index
], &WorkingVariableMtrr
[Index
+ 1],
2329 (WorkingVariableMtrrCount
- Index
) * sizeof (WorkingVariableMtrr
[0])
2335 if (WorkingVariableMtrrCount
> FirmwareVariableMtrrCount
) {
2336 return RETURN_OUT_OF_RESOURCES
;
2340 // 2.6. Merge the WorkingVariableMtrr to OriginalVariableMtrr
2341 // Make sure least modification is made to OriginalVariableMtrr.
2343 MtrrLibMergeVariableMtrr (
2344 OriginalVariableMtrr
, OriginalVariableMtrrCount
,
2345 WorkingVariableMtrr
, WorkingVariableMtrrCount
,
2346 VariableSettingModified
2352 // 3. Apply the below-1MB memory attribute settings.
2354 for (Index
= 0; Index
< RangeCount
; Index
++) {
2355 if (Ranges
[Index
].BaseAddress
>= BASE_1MB
) {
2359 Status
= MtrrLibSetBelow1MBMemoryAttribute (
2360 &WorkingFixedSettings
, FixedSettingsModified
,
2361 Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
, Ranges
[Index
].Type
2363 if (RETURN_ERROR (Status
)) {
2368 MtrrContextValid
= FALSE
;
2370 // 4. Write fixed MTRRs that have been modified
2372 for (Index
= 0; Index
< ARRAY_SIZE (FixedSettingsModified
); Index
++) {
2373 if (FixedSettingsModified
[Index
]) {
2374 if (MtrrSetting
!= NULL
) {
2375 MtrrSetting
->Fixed
.Mtrr
[Index
] = WorkingFixedSettings
.Mtrr
[Index
];
2377 if (!MtrrContextValid
) {
2378 MtrrLibPreMtrrChange (&MtrrContext
);
2379 MtrrContextValid
= TRUE
;
2382 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2383 WorkingFixedSettings
.Mtrr
[Index
]
2390 // 5. Write variable MTRRs that have been modified
2392 for (Index
= 0; Index
< OriginalVariableMtrrCount
; Index
++) {
2393 if (VariableSettingModified
[Index
]) {
2394 if (OriginalVariableMtrr
[Index
].Length
!= 0) {
2395 VariableSetting
.Base
= (OriginalVariableMtrr
[Index
].BaseAddress
& MtrrValidAddressMask
)
2396 | (UINT8
)OriginalVariableMtrr
[Index
].Type
;
2397 VariableSetting
.Mask
= ((~(OriginalVariableMtrr
[Index
].Length
- 1)) & MtrrValidAddressMask
) | BIT11
;
2399 VariableSetting
.Base
= 0;
2400 VariableSetting
.Mask
= 0;
2402 if (MtrrSetting
!= NULL
) {
2403 CopyMem (&MtrrSetting
->Variables
.Mtrr
[Index
], &VariableSetting
, sizeof (VariableSetting
));
2405 if (!MtrrContextValid
) {
2406 MtrrLibPreMtrrChange (&MtrrContext
);
2407 MtrrContextValid
= TRUE
;
2410 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2411 VariableSetting
.Base
2414 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2415 VariableSetting
.Mask
2421 if (MtrrSetting
!= NULL
) {
2422 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*)&MtrrSetting
->MtrrDefType
)->Bits
.E
= 1;
2423 ((MSR_IA32_MTRR_DEF_TYPE_REGISTER
*)&MtrrSetting
->MtrrDefType
)->Bits
.FE
= 1;
2425 if (MtrrContextValid
) {
2426 MtrrLibPostMtrrChange (&MtrrContext
);
2430 return RETURN_SUCCESS
;
2434 This function attempts to set the attributes into MTRR setting buffer for a memory range.
2436 @param[in, out] MtrrSetting MTRR setting buffer to be set.
2437 @param[in] BaseAddress The physical address that is the start address
2439 @param[in] Length The size in bytes of the memory range.
2440 @param[in] Attribute The bit mask of attributes to set for the
2443 @retval RETURN_SUCCESS The attributes were set for the memory range.
2444 @retval RETURN_INVALID_PARAMETER Length is zero.
2445 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
2446 memory resource range specified by BaseAddress and Length.
2447 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
2448 range specified by BaseAddress and Length.
2449 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
2450 BaseAddress and Length cannot be modified.
2451 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
2452 the memory resource range.
2453 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
2457 MtrrSetMemoryAttributeInMtrrSettings (
2458 IN OUT MTRR_SETTINGS
*MtrrSetting
,
2459 IN PHYSICAL_ADDRESS BaseAddress
,
2461 IN MTRR_MEMORY_CACHE_TYPE Attribute
2464 RETURN_STATUS Status
;
2465 UINT8 Scratch
[SCRATCH_BUFFER_SIZE
];
2467 MTRR_MEMORY_RANGE Range
;
2469 if (!IsMtrrSupported ()) {
2470 return RETURN_UNSUPPORTED
;
2473 Range
.BaseAddress
= BaseAddress
;
2474 Range
.Length
= Length
;
2475 Range
.Type
= Attribute
;
2476 ScratchSize
= sizeof (Scratch
);
2477 Status
= MtrrSetMemoryAttributesInMtrrSettings (MtrrSetting
, Scratch
, &ScratchSize
, &Range
, 1);
2478 DEBUG ((DEBUG_CACHE
, "MtrrSetMemoryAttribute(MtrrSettings = %p) %s: [%016lx, %016lx) - %x\n",
2480 mMtrrMemoryCacheTypeShortName
[Attribute
], BaseAddress
, BaseAddress
+ Length
, Status
));
2482 if (!RETURN_ERROR (Status
)) {
2483 MtrrDebugPrintAllMtrrsWorker (MtrrSetting
);
2489 This function attempts to set the attributes for a memory range.
2491 @param[in] BaseAddress The physical address that is the start
2492 address of a memory range.
2493 @param[in] Length The size in bytes of the memory range.
2494 @param[in] Attributes The bit mask of attributes to set for the
2497 @retval RETURN_SUCCESS The attributes were set for the memory
2499 @retval RETURN_INVALID_PARAMETER Length is zero.
2500 @retval RETURN_UNSUPPORTED The processor does not support one or
2501 more bytes of the memory resource range
2502 specified by BaseAddress and Length.
2503 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
2504 for the memory resource range specified
2505 by BaseAddress and Length.
2506 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
2507 range specified by BaseAddress and Length
2509 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
2510 modify the attributes of the memory
2512 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
2516 MtrrSetMemoryAttribute (
2517 IN PHYSICAL_ADDRESS BaseAddress
,
2519 IN MTRR_MEMORY_CACHE_TYPE Attribute
2522 return MtrrSetMemoryAttributeInMtrrSettings (NULL
, BaseAddress
, Length
, Attribute
);
2526 Worker function setting variable MTRRs
2528 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2532 MtrrSetVariableMtrrWorker (
2533 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2537 UINT32 VariableMtrrCount
;
2539 VariableMtrrCount
= GetVariableMtrrCountWorker ();
2540 ASSERT (VariableMtrrCount
<= ARRAY_SIZE (VariableSettings
->Mtrr
));
2542 for (Index
= 0; Index
< VariableMtrrCount
; Index
++) {
2544 MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1),
2545 VariableSettings
->Mtrr
[Index
].Base
2548 MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1),
2549 VariableSettings
->Mtrr
[Index
].Mask
2556 This function sets variable MTRRs
2558 @param[in] VariableSettings A buffer to hold variable MTRRs content.
2560 @return The pointer of VariableSettings
2563 MTRR_VARIABLE_SETTINGS
*
2565 MtrrSetVariableMtrr (
2566 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
2569 MTRR_CONTEXT MtrrContext
;
2571 if (!IsMtrrSupported ()) {
2572 return VariableSettings
;
2575 MtrrLibPreMtrrChange (&MtrrContext
);
2576 MtrrSetVariableMtrrWorker (VariableSettings
);
2577 MtrrLibPostMtrrChange (&MtrrContext
);
2578 MtrrDebugPrintAllMtrrs ();
2580 return VariableSettings
;
2584 Worker function setting fixed MTRRs
2586 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2590 MtrrSetFixedMtrrWorker (
2591 IN MTRR_FIXED_SETTINGS
*FixedSettings
2596 for (Index
= 0; Index
< MTRR_NUMBER_OF_FIXED_MTRR
; Index
++) {
2598 mMtrrLibFixedMtrrTable
[Index
].Msr
,
2599 FixedSettings
->Mtrr
[Index
]
2606 This function sets fixed MTRRs
2608 @param[in] FixedSettings A buffer to hold fixed MTRRs content.
2610 @retval The pointer of FixedSettings
2613 MTRR_FIXED_SETTINGS
*
2616 IN MTRR_FIXED_SETTINGS
*FixedSettings
2619 MTRR_CONTEXT MtrrContext
;
2621 if (!IsMtrrSupported ()) {
2622 return FixedSettings
;
2625 MtrrLibPreMtrrChange (&MtrrContext
);
2626 MtrrSetFixedMtrrWorker (FixedSettings
);
2627 MtrrLibPostMtrrChange (&MtrrContext
);
2628 MtrrDebugPrintAllMtrrs ();
2630 return FixedSettings
;
2635 This function gets the content in all MTRRs (variable and fixed)
2637 @param[out] MtrrSetting A buffer to hold all MTRRs content.
2639 @retval the pointer of MtrrSetting
2645 OUT MTRR_SETTINGS
*MtrrSetting
2648 if (!IsMtrrSupported ()) {
2655 MtrrGetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2658 // Get variable MTRRs
2660 MtrrGetVariableMtrrWorker (
2662 GetVariableMtrrCountWorker (),
2663 &MtrrSetting
->Variables
2667 // Get MTRR_DEF_TYPE value
2669 MtrrSetting
->MtrrDefType
= AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
);
2676 This function sets all MTRRs (variable and fixed)
2678 @param[in] MtrrSetting A buffer holding all MTRRs content.
2680 @retval The pointer of MtrrSetting
2686 IN MTRR_SETTINGS
*MtrrSetting
2689 MTRR_CONTEXT MtrrContext
;
2691 if (!IsMtrrSupported ()) {
2695 MtrrLibPreMtrrChange (&MtrrContext
);
2700 MtrrSetFixedMtrrWorker (&MtrrSetting
->Fixed
);
2703 // Set variable MTRRs
2705 MtrrSetVariableMtrrWorker (&MtrrSetting
->Variables
);
2708 // Set MTRR_DEF_TYPE value
2710 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE
, MtrrSetting
->MtrrDefType
);
2712 MtrrLibPostMtrrChangeEnableCache (&MtrrContext
);
2719 Checks if MTRR is supported.
2721 @retval TRUE MTRR is supported.
2722 @retval FALSE MTRR is not supported.
2731 CPUID_VERSION_INFO_EDX Edx
;
2732 MSR_IA32_MTRRCAP_REGISTER MtrrCap
;
2735 // Check CPUID(1).EDX[12] for MTRR capability
2737 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
2738 if (Edx
.Bits
.MTRR
== 0) {
2743 // Check number of variable MTRRs and fixed MTRRs existence.
2744 // If number of variable MTRRs is zero, or fixed MTRRs do not
2745 // exist, return false.
2747 MtrrCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MTRRCAP
);
2748 if ((MtrrCap
.Bits
.VCNT
== 0) || (MtrrCap
.Bits
.FIX
== 0)) {
2756 Worker function prints all MTRRs for debugging.
2758 If MtrrSetting is not NULL, print MTRR settings from input MTRR
2760 If MtrrSetting is NULL, print MTRR settings from MTRRs.
2762 @param MtrrSetting A buffer holding all MTRRs content.
2765 MtrrDebugPrintAllMtrrsWorker (
2766 IN MTRR_SETTINGS
*MtrrSetting
2770 MTRR_SETTINGS LocalMtrrs
;
2771 MTRR_SETTINGS
*Mtrrs
;
2774 UINT64 MtrrValidBitsMask
;
2775 UINT64 MtrrValidAddressMask
;
2776 MTRR_MEMORY_RANGE Ranges
[
2777 ARRAY_SIZE (mMtrrLibFixedMtrrTable
) * sizeof (UINT64
) + 2 * ARRAY_SIZE (Mtrrs
->Variables
.Mtrr
) + 1
2779 MTRR_MEMORY_RANGE RawVariableRanges
[ARRAY_SIZE (Mtrrs
->Variables
.Mtrr
)];
2781 if (!IsMtrrSupported ()) {
2785 if (MtrrSetting
!= NULL
) {
2786 Mtrrs
= MtrrSetting
;
2788 MtrrGetAllMtrrs (&LocalMtrrs
);
2789 Mtrrs
= &LocalMtrrs
;
2793 // Dump RAW MTRR contents
2795 DEBUG((DEBUG_CACHE
, "MTRR Settings\n"));
2796 DEBUG((DEBUG_CACHE
, "=============\n"));
2797 DEBUG((DEBUG_CACHE
, "MTRR Default Type: %016lx\n", Mtrrs
->MtrrDefType
));
2798 for (Index
= 0; Index
< ARRAY_SIZE (mMtrrLibFixedMtrrTable
); Index
++) {
2799 DEBUG((DEBUG_CACHE
, "Fixed MTRR[%02d] : %016lx\n", Index
, Mtrrs
->Fixed
.Mtrr
[Index
]));
2802 for (Index
= 0; Index
< ARRAY_SIZE (Mtrrs
->Variables
.Mtrr
); Index
++) {
2803 if ((Mtrrs
->Variables
.Mtrr
[Index
].Mask
& BIT11
) == 0) {
2805 // If mask is not valid, then do not display range
2809 DEBUG ((DEBUG_CACHE
, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",
2811 Mtrrs
->Variables
.Mtrr
[Index
].Base
,
2812 Mtrrs
->Variables
.Mtrr
[Index
].Mask
2815 DEBUG((DEBUG_CACHE
, "\n"));
2818 // Dump MTRR setting in ranges
2820 DEBUG((DEBUG_CACHE
, "MTRR Ranges\n"));
2821 DEBUG((DEBUG_CACHE
, "====================================\n"));
2822 MtrrLibInitializeMtrrMask (&MtrrValidBitsMask
, &MtrrValidAddressMask
);
2823 Ranges
[0].BaseAddress
= 0;
2824 Ranges
[0].Length
= MtrrValidBitsMask
+ 1;
2825 Ranges
[0].Type
= MtrrGetDefaultMemoryTypeWorker (Mtrrs
);
2828 MtrrLibGetRawVariableRanges (
2829 &Mtrrs
->Variables
, ARRAY_SIZE (Mtrrs
->Variables
.Mtrr
),
2830 MtrrValidBitsMask
, MtrrValidAddressMask
, RawVariableRanges
2832 MtrrLibApplyVariableMtrrs (
2833 RawVariableRanges
, ARRAY_SIZE (RawVariableRanges
),
2834 Ranges
, ARRAY_SIZE (Ranges
), &RangeCount
2837 MtrrLibApplyFixedMtrrs (&Mtrrs
->Fixed
, Ranges
, ARRAY_SIZE (Ranges
), &RangeCount
);
2839 for (Index
= 0; Index
< RangeCount
; Index
++) {
2840 DEBUG ((DEBUG_CACHE
, "%a:%016lx-%016lx\n",
2841 mMtrrMemoryCacheTypeShortName
[Ranges
[Index
].Type
],
2842 Ranges
[Index
].BaseAddress
, Ranges
[Index
].BaseAddress
+ Ranges
[Index
].Length
- 1
2849 This function prints all MTRRs for debugging.
2853 MtrrDebugPrintAllMtrrs (
2857 MtrrDebugPrintAllMtrrsWorker (NULL
);