30 IN UINT64 MemoryLength,
31 IN UINT64 ValidMtrrAddressMask,
32 OUT UINT32 *UsedMsrNum,
33 OUT EFI_MEMORY_CACHE_TYPE *MemoryCacheType
46 IN EFI_MEMORY_CACHE_TYPE MemoryCacheType
61 IN UINT64 BaseAddress,
72 { EFI_MSR_IA32_MTRR_FIX64K_00000, 0, 0x10000 },
73 { EFI_MSR_IA32_MTRR_FIX16K_80000, 0x80000, 0x4000 },
74 { EFI_MSR_IA32_MTRR_FIX16K_A0000, 0xA0000, 0x4000 },
75 { EFI_MSR_IA32_MTRR_FIX4K_C0000, 0xC0000, 0x1000 },
76 { EFI_MSR_IA32_MTRR_FIX4K_C8000, 0xC8000, 0x1000 },
77 { EFI_MSR_IA32_MTRR_FIX4K_D0000, 0xD0000, 0x1000 },
78 { EFI_MSR_IA32_MTRR_FIX4K_D8000, 0xD8000, 0x1000 },
79 { EFI_MSR_IA32_MTRR_FIX4K_E0000, 0xE0000, 0x1000 },
80 { EFI_MSR_IA32_MTRR_FIX4K_E8000, 0xE8000, 0x1000 },
81 { EFI_MSR_IA32_MTRR_FIX4K_F0000, 0xF0000, 0x1000 },
82 { EFI_MSR_IA32_MTRR_FIX4K_F8000, 0xF8000, 0x1000 }
119 *OldMtrr =
AsmReadMsr64 (EFI_MSR_CACHE_IA32_MTRR_DEF_TYPE);
120 TempQword = (*OldMtrr) & ~B_EFI_MSR_GLOBAL_MTRR_ENABLE & ~B_EFI_MSR_FIXED_MTRR_ENABLE;
134 IN BOOLEAN EnableMtrr,
144 TempQword =
AsmReadMsr64 (EFI_MSR_CACHE_IA32_MTRR_DEF_TYPE);
145 TempQword |= (UINT64)(B_EFI_MSR_GLOBAL_MTRR_ENABLE | B_EFI_MSR_FIXED_MTRR_ENABLE);
167 IN UINT32 MtrrNumber,
169 IN UINT64 MemoryLength,
170 IN EFI_MEMORY_CACHE_TYPE MemoryCacheType,
171 IN UINT64 ValidMtrrAddressMask
177 if (MemoryLength == 0) {
186 TempQword = (MemoryAddress & ValidMtrrAddressMask) | MemoryCacheType;
192 TempQword = ~(MemoryLength - 1);
193 AsmWriteMsr64 (MtrrNumber + 1, (TempQword & ValidMtrrAddressMask) | B_EFI_MSR_CACHE_MTRR_VALID);
209 IN UINT64 MemoryAddress,
210 IN UINT64 MemoryLength
215 if (MemoryLength == 0) {
216 return EFI_INVALID_PARAMETER;
227 if (MemoryAddress == 0) {
256 IN UINT64 BaseAddress,
268 ShiftedBase = (UINT32)
RShiftU64 (BaseAddress, 12);
269 ShiftedSize = (UINT32)
RShiftU64 (Size, 12);
274 return ShiftedBase % ShiftedSize;
291 IN EFI_MEMORY_CACHE_TYPE MemoryCacheType,
306 for (MsrNum = 0; MsrNum < V_EFI_FIXED_MTRR_NUMBER; MsrNum++) {
307 if ((*Base >= mFixedMtrrTable[MsrNum].BaseAddress) &&
308 (*Base < (mFixedMtrrTable[MsrNum].BaseAddress + 8 * mFixedMtrrTable[MsrNum].Length)))
314 if (MsrNum == V_EFI_FIXED_MTRR_NUMBER ) {
315 return EFI_DEVICE_ERROR;
321 for (ByteShift = 0; ByteShift < 8; ByteShift++) {
322 if ( *Base == (mFixedMtrrTable[MsrNum].BaseAddress + ByteShift * mFixedMtrrTable[MsrNum].Length)) {
327 if (ByteShift == 8 ) {
328 return EFI_DEVICE_ERROR;
331 for ( ; ((ByteShift < 8) && (*Len >= mFixedMtrrTable[MsrNum].Length)); ByteShift++) {
332 OrMask |=
LShiftU64 ((UINT64)MemoryCacheType, (UINT32)(ByteShift* 8));
333 ClearMask |=
LShiftU64 ((UINT64)0xFF, (UINT32)(ByteShift * 8));
334 *Len -= mFixedMtrrTable[MsrNum].Length;
335 *Base += mFixedMtrrTable[MsrNum].Length;
338 TempQword = (
AsmReadMsr64 (mFixedMtrrTable[MsrNum].Msr) & (~ClearMask)) | OrMask;
379 IN UINT64 MemoryLength,
380 IN EFI_MEMORY_CACHE_TYPE MemoryCacheType
384 UINT32 MsrNum, MsrNumEnd;
386 UINT32 LastVariableMtrrForBios;
389 EFI_MEMORY_CACHE_TYPE UsedMemoryCacheType;
390 UINT64 ValidMtrrAddressMask;
394 if (Cpuid_RegEax >= CPUID_VIR_PHY_ADDRESS_SIZE) {
396 ValidMtrrAddressMask = (
LShiftU64 ((UINT64)1, (Cpuid_RegEax & 0xFF)) - 1) & (~(UINT64)0x0FFF);
398 ValidMtrrAddressMask = (
LShiftU64 ((UINT64)1, 36) - 1) & (~(UINT64)0x0FFF);
404 if (((MemoryAddress & ~ValidMtrrAddressMask) != 0) || ((MemoryLength & ~ValidMtrrAddressMask) != 0)) {
405 return EFI_INVALID_PARAMETER;
408 if (MemoryLength == 0) {
409 return EFI_INVALID_PARAMETER;
412 switch (MemoryCacheType) {
413 case EFI_CACHE_UNCACHEABLE:
414 case EFI_CACHE_WRITECOMBINING:
415 case EFI_CACHE_WRITETHROUGH:
416 case EFI_CACHE_WRITEPROTECTED:
417 case EFI_CACHE_WRITEBACK:
421 return EFI_INVALID_PARAMETER;
427 if ((MemoryAddress + MemoryLength) <= (1 << 20)) {
430 while ((MemoryLength > 0) && (Status ==
EFI_SUCCESS)) {
444 ValidMtrrAddressMask,
449 if (!EFI_ERROR (Status)) {
453 if (UsedMemoryCacheType == MemoryCacheType) {
494 if (MemoryCacheType == (
AsmReadMsr64 (EFI_MSR_CACHE_IA32_MTRR_DEF_TYPE) & B_EFI_MSR_CACHE_MEMORY_TYPE)) {
505 MsrNumEnd = EFI_MSR_CACHE_VARIABLE_MTRR_BASE + (2 * (UINT32)(
AsmReadMsr64 (EFI_MSR_IA32_MTRR_CAP) & B_EFI_MSR_IA32_MTRR_CAP_VARIABLE_SUPPORT));
506 for (MsrNum = EFI_MSR_CACHE_VARIABLE_MTRR_BASE; MsrNum < MsrNumEnd; MsrNum += 2) {
507 if ((
AsmReadMsr64 (MsrNum+1) & B_EFI_MSR_CACHE_MTRR_VALID) == 0 ) {
515 LastVariableMtrrForBios = MsrNumEnd - 1 - (EFI_CACHE_NUM_VAR_MTRR_PAIRS_FOR_OS * 2);
516 if (MsrNum > LastVariableMtrrForBios) {
517 return EFI_LOAD_ERROR;
523 if (MemoryAddress == BASE_1MB) {
530 TempQword = MemoryLength;
550 if (MsrNum > LastVariableMtrrForBios) {
551 return EFI_LOAD_ERROR;
565 MemoryAddress += MemoryLength;
566 TempQword -= MemoryLength;
568 }
while (TempQword != 0);
586 UINT32 MsrNum, MsrNumEnd;
598 CacheType = EFI_CACHE_UNCACHEABLE;
615 for (Index = 0; Index < V_EFI_FIXED_MTRR_NUMBER; Index++) {
622 MsrNumEnd = EFI_MSR_CACHE_VARIABLE_MTRR_BASE + (2 * (UINT32)(
AsmReadMsr64 (EFI_MSR_IA32_MTRR_CAP) & B_EFI_MSR_IA32_MTRR_CAP_VARIABLE_SUPPORT));
623 for (MsrNum = EFI_MSR_CACHE_VARIABLE_MTRR_BASE; MsrNum < MsrNumEnd; MsrNum++) {
651 IN UINT64 MemoryLength,
652 IN UINT64 ValidMtrrAddressMask,
653 OUT UINT32 *UsedMsrNum,
654 OUT EFI_MEMORY_CACHE_TYPE *UsedMemoryCacheType
657 UINT32 MsrNum, MsrNumEnd;
660 if (MemoryLength == 0) {
661 return EFI_INVALID_PARAMETER;
664 MsrNumEnd = EFI_MSR_CACHE_VARIABLE_MTRR_BASE + (2 * (UINT32)(
AsmReadMsr64 (EFI_MSR_IA32_MTRR_CAP) & B_EFI_MSR_IA32_MTRR_CAP_VARIABLE_SUPPORT));
665 for (MsrNum = EFI_MSR_CACHE_VARIABLE_MTRR_BASE; MsrNum < MsrNumEnd; MsrNum += 2) {
667 if ((TempQword & B_EFI_MSR_CACHE_MTRR_VALID) == 0) {
671 if ((TempQword & ValidMtrrAddressMask) != ((~(MemoryLength - 1)) & ValidMtrrAddressMask)) {
676 if ((TempQword & ValidMtrrAddressMask) != (MemoryAddress & ValidMtrrAddressMask)) {
680 *UsedMemoryCacheType = (EFI_MEMORY_CACHE_TYPE)(TempQword & B_EFI_MSR_CACHE_MEMORY_TYPE);
681 *UsedMsrNum = MsrNum;
686 return EFI_NOT_FOUND;
699 IN EFI_MEMORY_CACHE_TYPE MemoryCacheType
702 if ((
AsmReadMsr64 (EFI_MSR_CACHE_IA32_MTRR_DEF_TYPE) & B_EFI_MSR_CACHE_MEMORY_TYPE) != MemoryCacheType) {
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
UINT64 EFIAPI GetPowerOfTwo64(IN UINT64 Operand)
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
VOID EFIAPI DisableCacheAsRam(IN BOOLEAN DisableCar)
EFI_STATUS EFIAPI SetCacheAttributes(IN EFI_PHYSICAL_ADDRESS MemoryAddress, IN UINT64 MemoryLength, IN EFI_MEMORY_CACHE_TYPE MemoryCacheType)
VOID EfiDisableCacheMtrr(OUT UINT64 *OldMtrr)
EFI_STATUS EFIAPI ResetCacheAttributes(VOID)
VOID EfiProgramMtrr(IN UINT32 MtrrNumber, IN EFI_PHYSICAL_ADDRESS MemoryAddress, IN UINT64 MemoryLength, IN EFI_MEMORY_CACHE_TYPE MemoryCacheType, IN UINT64 ValidMtrrAddressMask)
UINT32 CheckMtrrAlignment(IN UINT64 BaseAddress, IN UINT64 Size)
EFI_STATUS SearchForExactMtrr(IN EFI_PHYSICAL_ADDRESS MemoryAddress, IN UINT64 MemoryLength, IN UINT64 ValidMtrrAddressMask, OUT UINT32 *UsedMsrNum, OUT EFI_MEMORY_CACHE_TYPE *MemoryCacheType)
BOOLEAN CheckMtrrOverlap(IN EFI_PHYSICAL_ADDRESS Start, IN EFI_PHYSICAL_ADDRESS End)
INT8 CheckDirection(IN UINT64 Input)
BOOLEAN IsDefaultType(IN EFI_MEMORY_CACHE_TYPE MemoryCacheType)
EFI_STATUS ProgramFixedMtrr(IN EFI_MEMORY_CACHE_TYPE MemoryCacheType, IN UINT64 *Base, IN UINT64 *Len)
VOID EfiRecoverCacheMtrr(IN BOOLEAN EnableMtrr, IN UINT64 OldMtrr)
UINT64 Power2MaxMemory(IN UINT64 MemoryAddress, IN UINT64 MemoryLength)
VOID EFIAPI AsmDisableCache(VOID)
VOID EFIAPI Input(IN CHAR16 *Prompt OPTIONAL, OUT CHAR16 *InStr, IN UINTN StrLen)
VOID EFIAPI AsmEnableCache(VOID)
UINT64 EFIAPI AsmReadMsr64(IN UINT32 Index)
UINT64 EFIAPI AsmWriteMsr64(IN UINT32 Index, IN UINT64 Value)
UINT32 EFIAPI AsmCpuid(IN UINT32 Index, OUT UINT32 *RegisterEax OPTIONAL, OUT UINT32 *RegisterEbx OPTIONAL, OUT UINT32 *RegisterEcx OPTIONAL, OUT UINT32 *RegisterEdx OPTIONAL)
UINT64 EFI_PHYSICAL_ADDRESS