]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/PlatformPei/MemDetect.c
MdeModulePkg: Remove variables that are set, but not used
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemDetect.c
index daa83a00141795b65a38dec68b00b86735563d8b..5070c79c0da9be00a1e6165b7721568db8e55271 100644 (file)
@@ -1,8 +1,8 @@
 /**@file\r
   Memory Detection for Virtual Machines.\r
 \r
-  Copyright (c) 2006 - 2009, Intel Corporation\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
@@ -27,15 +27,17 @@ Module Name:
 #include <Library/DebugLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Library/IoLib.h>\r
+#include <Library/PcdLib.h>\r
 #include <Library/PeimEntryPoint.h>\r
 #include <Library/ResourcePublicationLib.h>\r
+#include <Library/MtrrLib.h>\r
 \r
 #include "Platform.h"\r
 #include "Cmos.h"\r
 \r
 STATIC\r
 UINTN\r
-GetSystemMemorySize (\r
+GetSystemMemorySizeBelow4gb (\r
   )\r
 {\r
   UINT8 Cmos0x34;\r
@@ -53,7 +55,32 @@ GetSystemMemorySize (
   Cmos0x34 = (UINT8) CmosRead8 (0x34);\r
   Cmos0x35 = (UINT8) CmosRead8 (0x35);\r
 \r
-  return ((((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
+  return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
+}\r
+\r
+\r
+STATIC\r
+UINT64\r
+GetSystemMemorySizeAbove4gb (\r
+  )\r
+{\r
+  UINT32 Size;\r
+  UINTN  CmosIndex;\r
+\r
+  //\r
+  // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r
+  // * CMOS(0x5d) is the most significant size byte\r
+  // * CMOS(0x5c) is the middle size byte\r
+  // * CMOS(0x5b) is the least significant size byte\r
+  // * The size is specified in 64kb chunks\r
+  //\r
+\r
+  Size = 0;\r
+  for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r
+    Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);\r
+  }\r
+\r
+  return LShiftU64 (Size, 16);\r
 }\r
 \r
 \r
@@ -63,24 +90,33 @@ GetSystemMemorySize (
   @return EFI_SUCCESS     The PEIM initialized successfully.\r
 \r
 **/\r
-EFI_STATUS\r
+EFI_PHYSICAL_ADDRESS\r
 MemDetect (\r
   )\r
 {\r
   EFI_STATUS                  Status;\r
   EFI_PHYSICAL_ADDRESS        MemoryBase;\r
   UINT64                      MemorySize;\r
-  UINT64                      TotalMemorySize;\r
+  UINT64                      LowerMemorySize;\r
+  UINT64                      UpperMemorySize;\r
 \r
   DEBUG ((EFI_D_ERROR, "MemDetect called\n"));\r
 \r
   //\r
   // Determine total memory size available\r
   //\r
-  TotalMemorySize = (UINT64)GetSystemMemorySize ();\r
+  LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
+  UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r
 \r
-  MemoryBase = 0x800000;\r
-  MemorySize = TotalMemorySize - MemoryBase - 0x100000;\r
+  //\r
+  // Determine the range of memory to use during PEI\r
+  //\r
+  MemoryBase = PcdGet32 (PcdOvmfMemFvBase) + PcdGet32 (PcdOvmfMemFvSize);\r
+  MemorySize = LowerMemorySize - MemoryBase;\r
+  if (MemorySize > SIZE_64MB) {\r
+    MemoryBase = LowerMemorySize - SIZE_64MB;\r
+    MemorySize = SIZE_64MB;\r
+  }\r
 \r
   //\r
   // Publish this memory to the PEI Core\r
@@ -92,9 +128,22 @@ MemDetect (
   // Create memory HOBs\r
   //\r
   AddMemoryBaseSizeHob (MemoryBase, MemorySize);\r
-  AddMemoryRangeHob (0x100000, 0x800000);\r
-  AddMemoryRangeHob (0x000000, 0x0A0000);\r
+  AddMemoryRangeHob (BASE_1MB, MemoryBase);\r
+  AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r
+\r
+  Status = MtrrSetMemoryAttribute (BASE_1MB, MemoryBase + MemorySize - BASE_1MB, CacheWriteBack);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
+  Status = MtrrSetMemoryAttribute (0, BASE_512KB + BASE_128KB, CacheWriteBack);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
+  if (UpperMemorySize != 0) {\r
+    AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
+\r
+    Status = MtrrSetMemoryAttribute (BASE_4GB, UpperMemorySize, CacheWriteBack);\r
+    ASSERT_EFI_ERROR(Status);\r
+  }\r
 \r
-  return EFI_SUCCESS;\r
+  return MemoryBase + MemorySize;\r
 }\r
 \r