summaryrefslogtreecommitdiff
path: root/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
diff options
context:
space:
mode:
authorxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>2010-03-10 02:38:39 +0000
committerxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>2010-03-10 02:38:39 +0000
commit947a573ada499a7b1533d91f015327cc07605ff7 (patch)
tree7f2160b9eea2428fa9d735b0f803d16f2ecf97e4 /UefiCpuPkg/Library/MtrrLib/MtrrLib.c
parentc4ae2792f250bce176cf68ecdd64d4bf246c3fb0 (diff)
downloadedk2-platforms-947a573ada499a7b1533d91f015327cc07605ff7.tar.xz
Add Checking for MTRR existence.
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@10223 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'UefiCpuPkg/Library/MtrrLib/MtrrLib.c')
-rw-r--r--UefiCpuPkg/Library/MtrrLib/MtrrLib.c89
1 files changed, 88 insertions, 1 deletions
diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index 4e6878f282..dab0535fe9 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -93,6 +93,10 @@ GetVariableMtrrCount (
VOID
)
{
+ if (!IsMtrrSupported ()) {
+ return 0;
+ }
+
return (UINT32)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK);
}
@@ -107,7 +111,14 @@ GetFirmwareVariableMtrrCount (
VOID
)
{
- return GetVariableMtrrCount () - RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER;
+ UINT32 VariableMtrrCount;
+
+ VariableMtrrCount = GetVariableMtrrCount ();
+ if (VariableMtrrCount < RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER) {
+ return 0;
+ }
+
+ return VariableMtrrCount - RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER;
}
/**
@@ -319,6 +330,10 @@ MtrrGetMemoryAttributeInVariableMtrr (
UINT32 FirmwareVariableMtrrCount;
UINT32 VariableMtrrEnd;
+ if (!IsMtrrSupported ()) {
+ return 0;
+ }
+
FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();
VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1;
@@ -870,6 +885,10 @@ MtrrSetMemoryAttribute (
UINT32 FirmwareVariableMtrrCount;
UINT32 VariableMtrrEnd;
+ if (!IsMtrrSupported ()) {
+ return RETURN_UNSUPPORTED;
+ }
+
FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();
VariableMtrrEnd = MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (2 * GetVariableMtrrCount ()) - 1;
@@ -1110,6 +1129,10 @@ MtrrGetMemoryAttribute (
UINT64 MtrrValidAddressMask;
UINTN VariableMtrrCount;
+ if (!IsMtrrSupported ()) {
+ return CacheUncacheable;
+ }
+
//
// Check if MTRR is enabled, if not, return UC as attribute
//
@@ -1190,6 +1213,10 @@ MtrrGetVariableMtrr (
UINT32 Index;
UINT32 VariableMtrrCount;
+ if (!IsMtrrSupported ()) {
+ return VariableSettings;
+ }
+
VariableMtrrCount = GetVariableMtrrCount ();
ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);
@@ -1250,6 +1277,10 @@ MtrrSetVariableMtrr (
{
UINTN Cr4;
+ if (!IsMtrrSupported ()) {
+ return VariableSettings;
+ }
+
Cr4 = PreMtrrChange ();
MtrrSetVariableMtrrWorker (VariableSettings);
PostMtrrChange (Cr4);
@@ -1273,6 +1304,10 @@ MtrrGetFixedMtrr (
{
UINT32 Index;
+ if (!IsMtrrSupported ()) {
+ return FixedSettings;
+ }
+
for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {
FixedSettings->Mtrr[Index] =
AsmReadMsr64 (MtrrLibFixedMtrrTable[Index].Msr);
@@ -1319,6 +1354,10 @@ MtrrSetFixedMtrr (
{
UINTN Cr4;
+ if (!IsMtrrSupported ()) {
+ return FixedSettings;
+ }
+
Cr4 = PreMtrrChange ();
MtrrSetFixedMtrrWorker (FixedSettings);
PostMtrrChange (Cr4);
@@ -1341,6 +1380,10 @@ MtrrGetAllMtrrs (
OUT MTRR_SETTINGS *MtrrSetting
)
{
+ if (!IsMtrrSupported ()) {
+ return MtrrSetting;
+ }
+
//
// Get fixed MTRRs
//
@@ -1376,6 +1419,10 @@ MtrrSetAllMtrrs (
{
UINTN Cr4;
+ if (!IsMtrrSupported ()) {
+ return MtrrSetting;
+ }
+
Cr4 = PreMtrrChange ();
//
@@ -1412,6 +1459,10 @@ MtrrDebugPrintAllMtrrs (
UINTN Index;
UINTN VariableMtrrCount;
+ if (!IsMtrrSupported ()) {
+ return;
+ }
+
MtrrGetAllMtrrs (&MtrrSettings);
DEBUG((EFI_D_ERROR, "DefaultType = %016lx\n", MtrrSettings.MtrrDefType));
for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {
@@ -1435,3 +1486,39 @@ MtrrDebugPrintAllMtrrs (
);
}
+/**
+ Checks if MTRR is supported.
+
+ @retval TRUE MTRR is supported.
+ @retval FALSE MTRR is not supported.
+
+**/
+BOOLEAN
+EFIAPI
+IsMtrrSupported (
+ VOID
+ )
+{
+ UINT32 RegEdx;
+ UINT64 MtrrCap;
+
+ //
+ // Check CPUID(1).EDX[12] for MTRR capability
+ //
+ AsmCpuid (1, NULL, NULL, NULL, &RegEdx);
+ if (BitFieldRead32 (RegEdx, 12, 12) == 0) {
+ return FALSE;
+ }
+
+ //
+ // Check IA32_MTRRCAP.[0..7] for number of variable MTRRs and IA32_MTRRCAP[8] for
+ // fixed MTRRs existence. If number of variable MTRRs is zero, or fixed MTRRs do not
+ // exist, return false.
+ //
+ MtrrCap = AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP);
+ if ((BitFieldRead64 (MtrrCap, 0, 7) == 0) || (BitFieldRead64 (MtrrCap, 8, 8) == 0)) {
+ return FALSE;
+ }
+
+ return TRUE;
+}