OvmfPkg/PlatformPei: Skip various items for S3 resume
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemDetect.c
CommitLineData
49ba9447 1/**@file\r
2 Memory Detection for Virtual Machines.\r
3\r
4b455f7b 4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
56d7640a 5 This program and the accompanying materials\r
49ba9447 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13Module Name:\r
14\r
15 MemDetect.c\r
16\r
17**/\r
18\r
19//\r
20// The package level header files this module uses\r
21//\r
22#include <PiPei.h>\r
23\r
24//\r
25// The Library classes this module consumes\r
26//\r
27#include <Library/DebugLib.h>\r
28#include <Library/HobLib.h>\r
29#include <Library/IoLib.h>\r
c1c2669c 30#include <Library/PcdLib.h>\r
49ba9447 31#include <Library/PeimEntryPoint.h>\r
32#include <Library/ResourcePublicationLib.h>\r
e8e5cd4a 33#include <Library/MtrrLib.h>\r
49ba9447 34\r
35#include "Platform.h"\r
36#include "Cmos.h"\r
37\r
4b455f7b 38UINT32\r
c0e10976 39GetSystemMemorySizeBelow4gb (\r
4b455f7b 40 VOID\r
49ba9447 41 )\r
42{\r
43 UINT8 Cmos0x34;\r
44 UINT8 Cmos0x35;\r
45\r
46 //\r
47 // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r
48 // * CMOS(0x35) is the high byte\r
49 // * CMOS(0x34) is the low byte\r
50 // * The size is specified in 64kb chunks\r
51 // * Since this is memory above 16MB, the 16MB must be added\r
52 // into the calculation to get the total memory size.\r
53 //\r
54\r
55 Cmos0x34 = (UINT8) CmosRead8 (0x34);\r
56 Cmos0x35 = (UINT8) CmosRead8 (0x35);\r
57\r
55cdb67a 58 return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
49ba9447 59}\r
60\r
61\r
c0e10976 62STATIC\r
63UINT64\r
64GetSystemMemorySizeAbove4gb (\r
65 )\r
66{\r
67 UINT32 Size;\r
68 UINTN CmosIndex;\r
69\r
70 //\r
71 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r
72 // * CMOS(0x5d) is the most significant size byte\r
73 // * CMOS(0x5c) is the middle size byte\r
74 // * CMOS(0x5b) is the least significant size byte\r
75 // * The size is specified in 64kb chunks\r
76 //\r
77\r
78 Size = 0;\r
79 for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r
80 Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);\r
81 }\r
82\r
83 return LShiftU64 (Size, 16);\r
84}\r
85\r
36658fff
WL
86/**\r
87 Publish PEI core memory\r
88\r
89 @return EFI_SUCCESS The PEIM initialized successfully.\r
90\r
91**/\r
92EFI_STATUS\r
93PublishPeiMemory (\r
94 VOID\r
95 )\r
96{\r
97 EFI_STATUS Status;\r
98 EFI_PHYSICAL_ADDRESS MemoryBase;\r
99 UINT64 MemorySize;\r
100 UINT64 LowerMemorySize;\r
101\r
8e54500f
JJ
102 if (mBootMode == BOOT_ON_S3_RESUME) {\r
103 MemoryBase = PcdGet32 (PcdS3AcpiReservedMemoryBase);\r
104 MemorySize = PcdGet32 (PcdS3AcpiReservedMemorySize);\r
105 } else {\r
106 LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
107\r
108 //\r
109 // Determine the range of memory to use during PEI\r
110 //\r
111 MemoryBase = PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r
112 MemorySize = LowerMemorySize - MemoryBase;\r
113 if (MemorySize > SIZE_64MB) {\r
114 MemoryBase = LowerMemorySize - SIZE_64MB;\r
115 MemorySize = SIZE_64MB;\r
116 }\r
36658fff
WL
117 }\r
118\r
119 //\r
120 // Publish this memory to the PEI Core\r
121 //\r
122 Status = PublishSystemMemory(MemoryBase, MemorySize);\r
123 ASSERT_EFI_ERROR (Status);\r
124\r
125 return Status;\r
126}\r
127\r
c0e10976 128\r
49ba9447 129/**\r
c034906e 130 Peform Memory Detection for QEMU / KVM\r
49ba9447 131\r
132**/\r
c034906e
JJ
133STATIC\r
134VOID\r
135QemuInitializeRam (\r
136 VOID\r
49ba9447 137 )\r
138{\r
c0e10976 139 UINT64 LowerMemorySize;\r
140 UINT64 UpperMemorySize;\r
49ba9447 141\r
c034906e 142 DEBUG ((EFI_D_INFO, "%a called\n", __FUNCTION__));\r
49ba9447 143\r
144 //\r
145 // Determine total memory size available\r
146 //\r
c0e10976 147 LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
148 UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r
49ba9447 149\r
bd386eaf
JJ
150 if (mBootMode != BOOT_ON_S3_RESUME) {\r
151 //\r
152 // Create memory HOBs\r
153 //\r
154 AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
155 AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r
156 }\r
49ba9447 157\r
9ab36385 158 MtrrSetMemoryAttribute (BASE_1MB, LowerMemorySize - BASE_1MB, CacheWriteBack);\r
e8e5cd4a 159\r
27d7e63f 160 MtrrSetMemoryAttribute (0, BASE_512KB + BASE_128KB, CacheWriteBack);\r
e8e5cd4a 161\r
c0e10976 162 if (UpperMemorySize != 0) {\r
bd386eaf
JJ
163 if (mBootMode != BOOT_ON_S3_RESUME) {\r
164 AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
165 }\r
e8e5cd4a 166\r
27d7e63f 167 MtrrSetMemoryAttribute (BASE_4GB, UpperMemorySize, CacheWriteBack);\r
c0e10976 168 }\r
49ba9447 169}\r
170\r
c034906e
JJ
171/**\r
172 Publish system RAM and reserve memory regions\r
173\r
174**/\r
175VOID\r
176InitializeRamRegions (\r
177 VOID\r
178 )\r
179{\r
2818c158
JJ
180 if (!mXen) {\r
181 QemuInitializeRam ();\r
182 } else {\r
2818c158
JJ
183 XenPublishRamRegions ();\r
184 }\r
8e54500f
JJ
185\r
186 if (mS3Supported && mBootMode != BOOT_ON_S3_RESUME) {\r
187 //\r
188 // This is the memory range that will be used for PEI on S3 resume\r
189 //\r
190 BuildMemoryAllocationHob (\r
191 (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdS3AcpiReservedMemoryBase),\r
192 (UINT64)(UINTN) PcdGet32 (PcdS3AcpiReservedMemorySize),\r
193 EfiACPIMemoryNVS\r
194 );\r
195 }\r
c034906e 196}\r