]>
Commit | Line | Data |
---|---|---|
49ba9447 | 1 | /**@file\r |
2 | Memory Detection for Virtual Machines.\r | |
3 | \r | |
035ce3b3 | 4 | Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r |
b26f0cf9 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
49ba9447 | 6 | \r |
7 | Module Name:\r | |
8 | \r | |
9 | MemDetect.c\r | |
10 | \r | |
11 | **/\r | |
12 | \r | |
13 | //\r | |
14 | // The package level header files this module uses\r | |
15 | //\r | |
1fceaddb | 16 | #include <IndustryStandard/E820.h>\r |
49edde15 | 17 | #include <IndustryStandard/I440FxPiix4.h>\r |
d5e06444 | 18 | #include <IndustryStandard/Q35MchIch9.h>\r |
9afcd48a | 19 | #include <IndustryStandard/CloudHv.h>\r |
49ba9447 | 20 | #include <PiPei.h>\r |
adec2bd5 | 21 | #include <Register/Intel/SmramSaveStateMap.h>\r |
49ba9447 | 22 | \r |
23 | //\r | |
24 | // The Library classes this module consumes\r | |
25 | //\r | |
d5e06444 | 26 | #include <Library/BaseLib.h>\r |
6a7cba79 | 27 | #include <Library/BaseMemoryLib.h>\r |
49ba9447 | 28 | #include <Library/DebugLib.h>\r |
29 | #include <Library/HobLib.h>\r | |
30 | #include <Library/IoLib.h>\r | |
0bbed066 | 31 | #include <Library/MemEncryptSevLib.h>\r |
c1c2669c | 32 | #include <Library/PcdLib.h>\r |
d5e06444 | 33 | #include <Library/PciLib.h>\r |
49ba9447 | 34 | #include <Library/PeimEntryPoint.h>\r |
35 | #include <Library/ResourcePublicationLib.h>\r | |
e8e5cd4a | 36 | #include <Library/MtrrLib.h>\r |
7e5b1b67 | 37 | #include <Library/QemuFwCfgLib.h>\r |
98800cce | 38 | #include <Library/QemuFwCfgSimpleParserLib.h>\r |
49ba9447 | 39 | \r |
40 | #include "Platform.h"\r | |
41 | #include "Cmos.h"\r | |
42 | \r | |
ac0a286f | 43 | UINT8 mPhysMemAddressWidth;\r |
bc89fe48 | 44 | \r |
ac0a286f MK |
45 | STATIC UINT32 mS3AcpiReservedMemoryBase;\r |
46 | STATIC UINT32 mS3AcpiReservedMemorySize;\r | |
45d87081 | 47 | \r |
ac0a286f | 48 | STATIC UINT16 mQ35TsegMbytes;\r |
23bfb5c0 | 49 | \r |
ac0a286f | 50 | BOOLEAN mQ35SmramAtDefaultSmbase;\r |
73974f80 | 51 | \r |
ac0a286f | 52 | UINT32 mQemuUc32Base;\r |
49edde15 | 53 | \r |
23bfb5c0 LE |
54 | VOID\r |
55 | Q35TsegMbytesInitialization (\r | |
56 | VOID\r | |
57 | )\r | |
58 | {\r | |
ac0a286f MK |
59 | UINT16 ExtendedTsegMbytes;\r |
60 | RETURN_STATUS PcdStatus;\r | |
d5e06444 | 61 | \r |
e0ed7a9b | 62 | ASSERT (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID);\r |
d5e06444 LE |
63 | \r |
64 | //\r | |
65 | // Check if QEMU offers an extended TSEG.\r | |
66 | //\r | |
67 | // This can be seen from writing MCH_EXT_TSEG_MB_QUERY to the MCH_EXT_TSEG_MB\r | |
68 | // register, and reading back the register.\r | |
69 | //\r | |
70 | // On a QEMU machine type that does not offer an extended TSEG, the initial\r | |
71 | // write overwrites whatever value a malicious guest OS may have placed in\r | |
72 | // the (unimplemented) register, before entering S3 or rebooting.\r | |
73 | // Subsequently, the read returns MCH_EXT_TSEG_MB_QUERY unchanged.\r | |
74 | //\r | |
75 | // On a QEMU machine type that offers an extended TSEG, the initial write\r | |
76 | // triggers an update to the register. Subsequently, the value read back\r | |
77 | // (which is guaranteed to differ from MCH_EXT_TSEG_MB_QUERY) tells us the\r | |
78 | // number of megabytes.\r | |
79 | //\r | |
80 | PciWrite16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB), MCH_EXT_TSEG_MB_QUERY);\r | |
81 | ExtendedTsegMbytes = PciRead16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB));\r | |
82 | if (ExtendedTsegMbytes == MCH_EXT_TSEG_MB_QUERY) {\r | |
83 | mQ35TsegMbytes = PcdGet16 (PcdQ35TsegMbytes);\r | |
84 | return;\r | |
85 | }\r | |
86 | \r | |
87 | DEBUG ((\r | |
88 | DEBUG_INFO,\r | |
89 | "%a: QEMU offers an extended TSEG (%d MB)\n",\r | |
90 | __FUNCTION__,\r | |
91 | ExtendedTsegMbytes\r | |
92 | ));\r | |
93 | PcdStatus = PcdSet16S (PcdQ35TsegMbytes, ExtendedTsegMbytes);\r | |
94 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
95 | mQ35TsegMbytes = ExtendedTsegMbytes;\r | |
23bfb5c0 LE |
96 | }\r |
97 | \r | |
73974f80 LE |
98 | VOID\r |
99 | Q35SmramAtDefaultSmbaseInitialization (\r | |
100 | VOID\r | |
101 | )\r | |
102 | {\r | |
ac0a286f | 103 | RETURN_STATUS PcdStatus;\r |
73974f80 LE |
104 | \r |
105 | ASSERT (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID);\r | |
106 | \r | |
107 | mQ35SmramAtDefaultSmbase = FALSE;\r | |
75839f97 | 108 | if (FeaturePcdGet (PcdCsmEnable)) {\r |
ac0a286f MK |
109 | DEBUG ((\r |
110 | DEBUG_INFO,\r | |
111 | "%a: SMRAM at default SMBASE not checked due to CSM\n",\r | |
112 | __FUNCTION__\r | |
113 | ));\r | |
75839f97 | 114 | } else {\r |
ac0a286f MK |
115 | UINTN CtlReg;\r |
116 | UINT8 CtlRegVal;\r | |
75839f97 LE |
117 | \r |
118 | CtlReg = DRAMC_REGISTER_Q35 (MCH_DEFAULT_SMBASE_CTL);\r | |
119 | PciWrite8 (CtlReg, MCH_DEFAULT_SMBASE_QUERY);\r | |
ac0a286f | 120 | CtlRegVal = PciRead8 (CtlReg);\r |
75839f97 LE |
121 | mQ35SmramAtDefaultSmbase = (BOOLEAN)(CtlRegVal ==\r |
122 | MCH_DEFAULT_SMBASE_IN_RAM);\r | |
ac0a286f MK |
123 | DEBUG ((\r |
124 | DEBUG_INFO,\r | |
125 | "%a: SMRAM at default SMBASE %a\n",\r | |
126 | __FUNCTION__,\r | |
127 | mQ35SmramAtDefaultSmbase ? "found" : "not found"\r | |
128 | ));\r | |
75839f97 LE |
129 | }\r |
130 | \r | |
ac0a286f MK |
131 | PcdStatus = PcdSetBoolS (\r |
132 | PcdQ35SmramAtDefaultSmbase,\r | |
133 | mQ35SmramAtDefaultSmbase\r | |
134 | );\r | |
73974f80 LE |
135 | ASSERT_RETURN_ERROR (PcdStatus);\r |
136 | }\r | |
137 | \r | |
49edde15 LE |
138 | VOID\r |
139 | QemuUc32BaseInitialization (\r | |
140 | VOID\r | |
141 | )\r | |
142 | {\r | |
ac0a286f MK |
143 | UINT32 LowerMemorySize;\r |
144 | UINT32 Uc32Size;\r | |
49edde15 | 145 | \r |
6a8e9ad2 GH |
146 | if (mHostBridgeDevId == 0xffff /* microvm */) {\r |
147 | return;\r | |
148 | }\r | |
149 | \r | |
49edde15 LE |
150 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r |
151 | //\r | |
152 | // On q35, the 32-bit area that we'll mark as UC, through variable MTRRs,\r | |
153 | // starts at PcdPciExpressBaseAddress. The platform DSC is responsible for\r | |
154 | // setting PcdPciExpressBaseAddress such that describing the\r | |
155 | // [PcdPciExpressBaseAddress, 4GB) range require a very small number of\r | |
156 | // variable MTRRs (preferably 1 or 2).\r | |
157 | //\r | |
158 | ASSERT (FixedPcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);\r | |
159 | mQemuUc32Base = (UINT32)FixedPcdGet64 (PcdPciExpressBaseAddress);\r | |
160 | return;\r | |
161 | }\r | |
162 | \r | |
9afcd48a SB |
163 | if (mHostBridgeDevId == CLOUDHV_DEVICE_ID) {\r |
164 | Uc32Size = CLOUDHV_MMIO_HOLE_SIZE;\r | |
165 | mQemuUc32Base = CLOUDHV_MMIO_HOLE_ADDRESS;\r | |
166 | return;\r | |
167 | }\r | |
168 | \r | |
49edde15 LE |
169 | ASSERT (mHostBridgeDevId == INTEL_82441_DEVICE_ID);\r |
170 | //\r | |
171 | // On i440fx, start with the [LowerMemorySize, 4GB) range. Make sure one\r | |
172 | // variable MTRR suffices by truncating the size to a whole power of two,\r | |
173 | // while keeping the end affixed to 4GB. This will round the base up.\r | |
174 | //\r | |
175 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r | |
ac0a286f MK |
176 | Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - LowerMemorySize));\r |
177 | mQemuUc32Base = (UINT32)(SIZE_4GB - Uc32Size);\r | |
49edde15 LE |
178 | //\r |
179 | // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.\r | |
180 | // Therefore mQemuUc32Base is at least 2GB.\r | |
181 | //\r | |
182 | ASSERT (mQemuUc32Base >= BASE_2GB);\r | |
183 | \r | |
184 | if (mQemuUc32Base != LowerMemorySize) {\r | |
ac0a286f MK |
185 | DEBUG ((\r |
186 | DEBUG_VERBOSE,\r | |
187 | "%a: rounded UC32 base from 0x%x up to 0x%x, for "\r | |
188 | "an UC32 size of 0x%x\n",\r | |
189 | __FUNCTION__,\r | |
190 | LowerMemorySize,\r | |
191 | mQemuUc32Base,\r | |
192 | Uc32Size\r | |
193 | ));\r | |
49edde15 LE |
194 | }\r |
195 | }\r | |
196 | \r | |
1fceaddb LE |
197 | /**\r |
198 | Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r | |
199 | of the 32-bit address range.\r | |
200 | \r | |
201 | Find the highest exclusive >=4GB RAM address, or produce memory resource\r | |
202 | descriptor HOBs for RAM entries that start at or above 4GB.\r | |
203 | \r | |
204 | @param[out] MaxAddress If MaxAddress is NULL, then ScanOrAdd64BitE820Ram()\r | |
205 | produces memory resource descriptor HOBs for RAM\r | |
206 | entries that start at or above 4GB.\r | |
207 | \r | |
208 | Otherwise, MaxAddress holds the highest exclusive\r | |
209 | >=4GB RAM address on output. If QEMU's fw_cfg E820\r | |
210 | RAM map contains no RAM entry that starts outside of\r | |
211 | the 32-bit address range, then MaxAddress is exactly\r | |
212 | 4GB on output.\r | |
213 | \r | |
214 | @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r | |
215 | \r | |
216 | @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r | |
217 | whole multiple of sizeof(EFI_E820_ENTRY64). No\r | |
218 | RAM entry was processed.\r | |
219 | \r | |
220 | @return Error codes from QemuFwCfgFindFile(). No RAM\r | |
221 | entry was processed.\r | |
222 | **/\r | |
223 | STATIC\r | |
224 | EFI_STATUS\r | |
225 | ScanOrAdd64BitE820Ram (\r | |
557dede8 GH |
226 | IN BOOLEAN AddHighHob,\r |
227 | OUT UINT64 *LowMemory OPTIONAL,\r | |
ac0a286f | 228 | OUT UINT64 *MaxAddress OPTIONAL\r |
1fceaddb LE |
229 | )\r |
230 | {\r | |
ac0a286f MK |
231 | EFI_STATUS Status;\r |
232 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
233 | UINTN FwCfgSize;\r | |
234 | EFI_E820_ENTRY64 E820Entry;\r | |
235 | UINTN Processed;\r | |
1fceaddb LE |
236 | \r |
237 | Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r | |
238 | if (EFI_ERROR (Status)) {\r | |
239 | return Status;\r | |
240 | }\r | |
ac0a286f | 241 | \r |
1fceaddb LE |
242 | if (FwCfgSize % sizeof E820Entry != 0) {\r |
243 | return EFI_PROTOCOL_ERROR;\r | |
244 | }\r | |
245 | \r | |
557dede8 GH |
246 | if (LowMemory != NULL) {\r |
247 | *LowMemory = 0;\r | |
248 | }\r | |
249 | \r | |
1fceaddb LE |
250 | if (MaxAddress != NULL) {\r |
251 | *MaxAddress = BASE_4GB;\r | |
252 | }\r | |
253 | \r | |
254 | QemuFwCfgSelectItem (FwCfgItem);\r | |
255 | for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r | |
256 | QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r | |
257 | DEBUG ((\r | |
258 | DEBUG_VERBOSE,\r | |
259 | "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r | |
260 | __FUNCTION__,\r | |
261 | E820Entry.BaseAddr,\r | |
262 | E820Entry.Length,\r | |
263 | E820Entry.Type\r | |
264 | ));\r | |
557dede8 GH |
265 | if (E820Entry.Type == EfiAcpiAddressRangeMemory) {\r |
266 | if (AddHighHob && (E820Entry.BaseAddr >= BASE_4GB)) {\r | |
ac0a286f MK |
267 | UINT64 Base;\r |
268 | UINT64 End;\r | |
1fceaddb LE |
269 | \r |
270 | //\r | |
271 | // Round up the start address, and round down the end address.\r | |
272 | //\r | |
273 | Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r | |
ac0a286f MK |
274 | End = (E820Entry.BaseAddr + E820Entry.Length) &\r |
275 | ~(UINT64)EFI_PAGE_MASK;\r | |
1fceaddb LE |
276 | if (Base < End) {\r |
277 | AddMemoryRangeHob (Base, End);\r | |
278 | DEBUG ((\r | |
279 | DEBUG_VERBOSE,\r | |
280 | "%a: AddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r | |
281 | __FUNCTION__,\r | |
282 | Base,\r | |
283 | End\r | |
284 | ));\r | |
285 | }\r | |
557dede8 GH |
286 | }\r |
287 | \r | |
288 | if (MaxAddress || LowMemory) {\r | |
ac0a286f | 289 | UINT64 Candidate;\r |
1fceaddb LE |
290 | \r |
291 | Candidate = E820Entry.BaseAddr + E820Entry.Length;\r | |
557dede8 | 292 | if (MaxAddress && (Candidate > *MaxAddress)) {\r |
1fceaddb LE |
293 | *MaxAddress = Candidate;\r |
294 | DEBUG ((\r | |
295 | DEBUG_VERBOSE,\r | |
296 | "%a: MaxAddress=0x%Lx\n",\r | |
297 | __FUNCTION__,\r | |
298 | *MaxAddress\r | |
299 | ));\r | |
300 | }\r | |
557dede8 GH |
301 | \r |
302 | if (LowMemory && (Candidate > *LowMemory) && (Candidate < BASE_4GB)) {\r | |
303 | *LowMemory = Candidate;\r | |
304 | DEBUG ((\r | |
305 | DEBUG_VERBOSE,\r | |
306 | "%a: LowMemory=0x%Lx\n",\r | |
307 | __FUNCTION__,\r | |
308 | *LowMemory\r | |
309 | ));\r | |
310 | }\r | |
1fceaddb LE |
311 | }\r |
312 | }\r | |
313 | }\r | |
ac0a286f | 314 | \r |
1fceaddb LE |
315 | return EFI_SUCCESS;\r |
316 | }\r | |
317 | \r | |
4b455f7b | 318 | UINT32\r |
c0e10976 | 319 | GetSystemMemorySizeBelow4gb (\r |
4b455f7b | 320 | VOID\r |
49ba9447 | 321 | )\r |
322 | {\r | |
ac0a286f MK |
323 | UINT8 Cmos0x34;\r |
324 | UINT8 Cmos0x35;\r | |
49ba9447 | 325 | \r |
326 | //\r | |
327 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
328 | // * CMOS(0x35) is the high byte\r | |
329 | // * CMOS(0x34) is the low byte\r | |
330 | // * The size is specified in 64kb chunks\r | |
331 | // * Since this is memory above 16MB, the 16MB must be added\r | |
332 | // into the calculation to get the total memory size.\r | |
333 | //\r | |
334 | \r | |
ac0a286f MK |
335 | Cmos0x34 = (UINT8)CmosRead8 (0x34);\r |
336 | Cmos0x35 = (UINT8)CmosRead8 (0x35);\r | |
49ba9447 | 337 | \r |
ac0a286f | 338 | return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r |
49ba9447 | 339 | }\r |
340 | \r | |
c0e10976 | 341 | STATIC\r |
342 | UINT64\r | |
343 | GetSystemMemorySizeAbove4gb (\r | |
344 | )\r | |
345 | {\r | |
ac0a286f MK |
346 | UINT32 Size;\r |
347 | UINTN CmosIndex;\r | |
c0e10976 | 348 | \r |
349 | //\r | |
350 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
351 | // * CMOS(0x5d) is the most significant size byte\r | |
352 | // * CMOS(0x5c) is the middle size byte\r | |
353 | // * CMOS(0x5b) is the least significant size byte\r | |
354 | // * The size is specified in 64kb chunks\r | |
355 | //\r | |
356 | \r | |
357 | Size = 0;\r | |
358 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
ac0a286f | 359 | Size = (UINT32)(Size << 8) + (UINT32)CmosRead8 (CmosIndex);\r |
c0e10976 | 360 | }\r |
361 | \r | |
362 | return LShiftU64 (Size, 16);\r | |
363 | }\r | |
364 | \r | |
d5371680 LE |
365 | /**\r |
366 | Return the highest address that DXE could possibly use, plus one.\r | |
367 | **/\r | |
368 | STATIC\r | |
369 | UINT64\r | |
370 | GetFirstNonAddress (\r | |
371 | VOID\r | |
372 | )\r | |
373 | {\r | |
ac0a286f MK |
374 | UINT64 FirstNonAddress;\r |
375 | UINT64 Pci64Base, Pci64Size;\r | |
376 | UINT32 FwCfgPciMmio64Mb;\r | |
377 | EFI_STATUS Status;\r | |
378 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
379 | UINTN FwCfgSize;\r | |
380 | UINT64 HotPlugMemoryEnd;\r | |
381 | RETURN_STATUS PcdStatus;\r | |
d5371680 | 382 | \r |
1fceaddb LE |
383 | //\r |
384 | // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r | |
385 | //\r | |
386 | FirstNonAddress = 0;\r | |
387 | \r | |
388 | //\r | |
389 | // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r | |
390 | // address from it. This can express an address >= 4GB+1TB.\r | |
391 | //\r | |
392 | // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r | |
393 | // can only express a size smaller than 1TB), and add it to 4GB.\r | |
394 | //\r | |
557dede8 | 395 | Status = ScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);\r |
1fceaddb LE |
396 | if (EFI_ERROR (Status)) {\r |
397 | FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r | |
398 | }\r | |
7e5b1b67 LE |
399 | \r |
400 | //\r | |
401 | // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r | |
402 | // resources to 32-bit anyway. See DegradeResource() in\r | |
403 | // "PciResourceSupport.c".\r | |
404 | //\r | |
ac0a286f | 405 | #ifdef MDE_CPU_IA32\r |
7e5b1b67 LE |
406 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r |
407 | return FirstNonAddress;\r | |
408 | }\r | |
ac0a286f MK |
409 | \r |
410 | #endif\r | |
7e5b1b67 LE |
411 | \r |
412 | //\r | |
413 | // Otherwise, in order to calculate the highest address plus one, we must\r | |
414 | // consider the 64-bit PCI host aperture too. Fetch the default size.\r | |
415 | //\r | |
416 | Pci64Size = PcdGet64 (PcdPciMmio64Size);\r | |
417 | \r | |
418 | //\r | |
419 | // See if the user specified the number of megabytes for the 64-bit PCI host\r | |
98800cce | 420 | // aperture. Accept an aperture size up to 16TB.\r |
7e5b1b67 LE |
421 | //\r |
422 | // As signaled by the "X-" prefix, this knob is experimental, and might go\r | |
423 | // away at any time.\r | |
424 | //\r | |
ac0a286f MK |
425 | Status = QemuFwCfgParseUint32 (\r |
426 | "opt/ovmf/X-PciMmio64Mb",\r | |
427 | FALSE,\r | |
428 | &FwCfgPciMmio64Mb\r | |
429 | );\r | |
98800cce | 430 | switch (Status) {\r |
ac0a286f MK |
431 | case EFI_UNSUPPORTED:\r |
432 | case EFI_NOT_FOUND:\r | |
98800cce | 433 | break;\r |
ac0a286f MK |
434 | case EFI_SUCCESS:\r |
435 | if (FwCfgPciMmio64Mb <= 0x1000000) {\r | |
436 | Pci64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);\r | |
437 | break;\r | |
438 | }\r | |
439 | \r | |
98800cce LE |
440 | //\r |
441 | // fall through\r | |
442 | //\r | |
ac0a286f MK |
443 | default:\r |
444 | DEBUG ((\r | |
445 | DEBUG_WARN,\r | |
446 | "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r | |
447 | __FUNCTION__\r | |
448 | ));\r | |
449 | break;\r | |
7e5b1b67 LE |
450 | }\r |
451 | \r | |
452 | if (Pci64Size == 0) {\r | |
453 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
ac0a286f MK |
454 | DEBUG ((\r |
455 | DEBUG_INFO,\r | |
456 | "%a: disabling 64-bit PCI host aperture\n",\r | |
457 | __FUNCTION__\r | |
458 | ));\r | |
32e083c7 LE |
459 | PcdStatus = PcdSet64S (PcdPciMmio64Size, 0);\r |
460 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
7e5b1b67 LE |
461 | }\r |
462 | \r | |
463 | //\r | |
464 | // There's nothing more to do; the amount of memory above 4GB fully\r | |
465 | // determines the highest address plus one. The memory hotplug area (see\r | |
466 | // below) plays no role for the firmware in this case.\r | |
467 | //\r | |
468 | return FirstNonAddress;\r | |
469 | }\r | |
470 | \r | |
471 | //\r | |
472 | // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r | |
473 | // absolute, exclusive end address for the memory hotplug area. This area\r | |
474 | // starts right at the end of the memory above 4GB. The 64-bit PCI host\r | |
475 | // aperture must be placed above it.\r | |
476 | //\r | |
ac0a286f MK |
477 | Status = QemuFwCfgFindFile (\r |
478 | "etc/reserved-memory-end",\r | |
479 | &FwCfgItem,\r | |
480 | &FwCfgSize\r | |
481 | );\r | |
482 | if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {\r | |
7e5b1b67 LE |
483 | QemuFwCfgSelectItem (FwCfgItem);\r |
484 | QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r | |
ac0a286f MK |
485 | DEBUG ((\r |
486 | DEBUG_VERBOSE,\r | |
487 | "%a: HotPlugMemoryEnd=0x%Lx\n",\r | |
488 | __FUNCTION__,\r | |
489 | HotPlugMemoryEnd\r | |
490 | ));\r | |
7e5b1b67 LE |
491 | \r |
492 | ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r | |
493 | FirstNonAddress = HotPlugMemoryEnd;\r | |
494 | }\r | |
495 | \r | |
496 | //\r | |
497 | // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r | |
498 | // that the host can map it with 1GB hugepages. Follow suit.\r | |
499 | //\r | |
500 | Pci64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r | |
501 | Pci64Size = ALIGN_VALUE (Pci64Size, (UINT64)SIZE_1GB);\r | |
502 | \r | |
503 | //\r | |
504 | // The 64-bit PCI host aperture should also be "naturally" aligned. The\r | |
505 | // alignment is determined by rounding the size of the aperture down to the\r | |
506 | // next smaller or equal power of two. That is, align the aperture by the\r | |
507 | // largest BAR size that can fit into it.\r | |
508 | //\r | |
509 | Pci64Base = ALIGN_VALUE (Pci64Base, GetPowerOfTwo64 (Pci64Size));\r | |
510 | \r | |
511 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
512 | //\r | |
513 | // The core PciHostBridgeDxe driver will automatically add this range to\r | |
514 | // the GCD memory space map through our PciHostBridgeLib instance; here we\r | |
515 | // only need to set the PCDs.\r | |
516 | //\r | |
32e083c7 LE |
517 | PcdStatus = PcdSet64S (PcdPciMmio64Base, Pci64Base);\r |
518 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
519 | PcdStatus = PcdSet64S (PcdPciMmio64Size, Pci64Size);\r | |
520 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
521 | \r | |
ac0a286f MK |
522 | DEBUG ((\r |
523 | DEBUG_INFO,\r | |
524 | "%a: Pci64Base=0x%Lx Pci64Size=0x%Lx\n",\r | |
525 | __FUNCTION__,\r | |
526 | Pci64Base,\r | |
527 | Pci64Size\r | |
528 | ));\r | |
7e5b1b67 LE |
529 | }\r |
530 | \r | |
531 | //\r | |
532 | // The useful address space ends with the 64-bit PCI host aperture.\r | |
533 | //\r | |
534 | FirstNonAddress = Pci64Base + Pci64Size;\r | |
d5371680 LE |
535 | return FirstNonAddress;\r |
536 | }\r | |
537 | \r | |
bc89fe48 LE |
538 | /**\r |
539 | Initialize the mPhysMemAddressWidth variable, based on guest RAM size.\r | |
540 | **/\r | |
541 | VOID\r | |
542 | AddressWidthInitialization (\r | |
543 | VOID\r | |
544 | )\r | |
545 | {\r | |
ac0a286f | 546 | UINT64 FirstNonAddress;\r |
bc89fe48 LE |
547 | \r |
548 | //\r | |
549 | // As guest-physical memory size grows, the permanent PEI RAM requirements\r | |
550 | // are dominated by the identity-mapping page tables built by the DXE IPL.\r | |
551 | // The DXL IPL keys off of the physical address bits advertized in the CPU\r | |
552 | // HOB. To conserve memory, we calculate the minimum address width here.\r | |
553 | //\r | |
d5371680 | 554 | FirstNonAddress = GetFirstNonAddress ();\r |
bc89fe48 LE |
555 | mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r |
556 | \r | |
557 | //\r | |
558 | // If FirstNonAddress is not an integral power of two, then we need an\r | |
559 | // additional bit.\r | |
560 | //\r | |
561 | if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r | |
562 | ++mPhysMemAddressWidth;\r | |
563 | }\r | |
564 | \r | |
565 | //\r | |
566 | // The minimum address width is 36 (covers up to and excluding 64 GB, which\r | |
567 | // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r | |
568 | // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r | |
569 | // can simply assert that here, since 48 bits are good enough for 256 TB.\r | |
570 | //\r | |
571 | if (mPhysMemAddressWidth <= 36) {\r | |
572 | mPhysMemAddressWidth = 36;\r | |
573 | }\r | |
ac0a286f | 574 | \r |
bc89fe48 LE |
575 | ASSERT (mPhysMemAddressWidth <= 48);\r |
576 | }\r | |
577 | \r | |
bc89fe48 LE |
578 | /**\r |
579 | Calculate the cap for the permanent PEI memory.\r | |
580 | **/\r | |
581 | STATIC\r | |
582 | UINT32\r | |
583 | GetPeiMemoryCap (\r | |
584 | VOID\r | |
585 | )\r | |
586 | {\r | |
ac0a286f MK |
587 | BOOLEAN Page1GSupport;\r |
588 | UINT32 RegEax;\r | |
589 | UINT32 RegEdx;\r | |
590 | UINT32 Pml4Entries;\r | |
591 | UINT32 PdpEntries;\r | |
592 | UINTN TotalPages;\r | |
bc89fe48 LE |
593 | \r |
594 | //\r | |
595 | // If DXE is 32-bit, then just return the traditional 64 MB cap.\r | |
596 | //\r | |
ac0a286f | 597 | #ifdef MDE_CPU_IA32\r |
bc89fe48 LE |
598 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r |
599 | return SIZE_64MB;\r | |
600 | }\r | |
ac0a286f MK |
601 | \r |
602 | #endif\r | |
bc89fe48 LE |
603 | \r |
604 | //\r | |
605 | // Dependent on physical address width, PEI memory allocations can be\r | |
606 | // dominated by the page tables built for 64-bit DXE. So we key the cap off\r | |
607 | // of those. The code below is based on CreateIdentityMappingPageTables() in\r | |
608 | // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".\r | |
609 | //\r | |
610 | Page1GSupport = FALSE;\r | |
611 | if (PcdGetBool (PcdUse1GPageTable)) {\r | |
612 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r | |
613 | if (RegEax >= 0x80000001) {\r | |
614 | AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r | |
615 | if ((RegEdx & BIT26) != 0) {\r | |
616 | Page1GSupport = TRUE;\r | |
617 | }\r | |
618 | }\r | |
619 | }\r | |
620 | \r | |
621 | if (mPhysMemAddressWidth <= 39) {\r | |
622 | Pml4Entries = 1;\r | |
ac0a286f | 623 | PdpEntries = 1 << (mPhysMemAddressWidth - 30);\r |
bc89fe48 LE |
624 | ASSERT (PdpEntries <= 0x200);\r |
625 | } else {\r | |
626 | Pml4Entries = 1 << (mPhysMemAddressWidth - 39);\r | |
627 | ASSERT (Pml4Entries <= 0x200);\r | |
628 | PdpEntries = 512;\r | |
629 | }\r | |
630 | \r | |
631 | TotalPages = Page1GSupport ? Pml4Entries + 1 :\r | |
ac0a286f | 632 | (PdpEntries + 1) * Pml4Entries + 1;\r |
bc89fe48 LE |
633 | ASSERT (TotalPages <= 0x40201);\r |
634 | \r | |
635 | //\r | |
636 | // Add 64 MB for miscellaneous allocations. Note that for\r | |
637 | // mPhysMemAddressWidth values close to 36, the cap will actually be\r | |
638 | // dominated by this increment.\r | |
639 | //\r | |
640 | return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB);\r | |
641 | }\r | |
642 | \r | |
36658fff WL |
643 | /**\r |
644 | Publish PEI core memory\r | |
645 | \r | |
646 | @return EFI_SUCCESS The PEIM initialized successfully.\r | |
647 | \r | |
648 | **/\r | |
649 | EFI_STATUS\r | |
650 | PublishPeiMemory (\r | |
651 | VOID\r | |
652 | )\r | |
653 | {\r | |
ac0a286f MK |
654 | EFI_STATUS Status;\r |
655 | EFI_PHYSICAL_ADDRESS MemoryBase;\r | |
656 | UINT64 MemorySize;\r | |
657 | UINT32 LowerMemorySize;\r | |
658 | UINT32 PeiMemoryCap;\r | |
36658fff | 659 | \r |
45d87081 LE |
660 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
661 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
662 | //\r | |
663 | // TSEG is chipped from the end of low RAM\r | |
664 | //\r | |
23bfb5c0 | 665 | LowerMemorySize -= mQ35TsegMbytes * SIZE_1MB;\r |
45d87081 LE |
666 | }\r |
667 | \r | |
668 | //\r | |
669 | // If S3 is supported, then the S3 permanent PEI memory is placed next,\r | |
670 | // downwards. Its size is primarily dictated by CpuMpPei. The formula below\r | |
671 | // is an approximation.\r | |
672 | //\r | |
673 | if (mS3Supported) {\r | |
674 | mS3AcpiReservedMemorySize = SIZE_512KB +\r | |
ac0a286f MK |
675 | mMaxCpuCount *\r |
676 | PcdGet32 (PcdCpuApStackSize);\r | |
45d87081 | 677 | mS3AcpiReservedMemoryBase = LowerMemorySize - mS3AcpiReservedMemorySize;\r |
ac0a286f | 678 | LowerMemorySize = mS3AcpiReservedMemoryBase;\r |
45d87081 LE |
679 | }\r |
680 | \r | |
8e54500f | 681 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
45d87081 LE |
682 | MemoryBase = mS3AcpiReservedMemoryBase;\r |
683 | MemorySize = mS3AcpiReservedMemorySize;\r | |
8e54500f | 684 | } else {\r |
bc89fe48 | 685 | PeiMemoryCap = GetPeiMemoryCap ();\r |
ac0a286f MK |
686 | DEBUG ((\r |
687 | DEBUG_INFO,\r | |
688 | "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",\r | |
689 | __FUNCTION__,\r | |
690 | mPhysMemAddressWidth,\r | |
691 | PeiMemoryCap >> 10\r | |
692 | ));\r | |
bc89fe48 | 693 | \r |
8e54500f JJ |
694 | //\r |
695 | // Determine the range of memory to use during PEI\r | |
696 | //\r | |
efb0f16e LE |
697 | // Technically we could lay the permanent PEI RAM over SEC's temporary\r |
698 | // decompression and scratch buffer even if "secure S3" is needed, since\r | |
699 | // their lifetimes don't overlap. However, PeiFvInitialization() will cover\r | |
700 | // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory\r | |
701 | // allocation HOB, and other allocations served from the permanent PEI RAM\r | |
702 | // shouldn't overlap with that HOB.\r | |
703 | //\r | |
704 | MemoryBase = mS3Supported && FeaturePcdGet (PcdSmmSmramRequire) ?\r | |
ac0a286f MK |
705 | PcdGet32 (PcdOvmfDecompressionScratchEnd) :\r |
706 | PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r | |
8e54500f | 707 | MemorySize = LowerMemorySize - MemoryBase;\r |
bc89fe48 LE |
708 | if (MemorySize > PeiMemoryCap) {\r |
709 | MemoryBase = LowerMemorySize - PeiMemoryCap;\r | |
710 | MemorySize = PeiMemoryCap;\r | |
8e54500f | 711 | }\r |
36658fff WL |
712 | }\r |
713 | \r | |
adec2bd5 LE |
714 | //\r |
715 | // MEMFD_BASE_ADDRESS separates the SMRAM at the default SMBASE from the\r | |
716 | // normal boot permanent PEI RAM. Regarding the S3 boot path, the S3\r | |
717 | // permanent PEI RAM is located even higher.\r | |
718 | //\r | |
719 | if (FeaturePcdGet (PcdSmmSmramRequire) && mQ35SmramAtDefaultSmbase) {\r | |
720 | ASSERT (SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE <= MemoryBase);\r | |
721 | }\r | |
722 | \r | |
36658fff WL |
723 | //\r |
724 | // Publish this memory to the PEI Core\r | |
725 | //\r | |
ac0a286f | 726 | Status = PublishSystemMemory (MemoryBase, MemorySize);\r |
36658fff WL |
727 | ASSERT_EFI_ERROR (Status);\r |
728 | \r | |
729 | return Status;\r | |
730 | }\r | |
731 | \r | |
84b223c1 LE |
732 | STATIC\r |
733 | VOID\r | |
734 | QemuInitializeRamBelow1gb (\r | |
735 | VOID\r | |
736 | )\r | |
737 | {\r | |
738 | if (FeaturePcdGet (PcdSmmSmramRequire) && mQ35SmramAtDefaultSmbase) {\r | |
739 | AddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);\r | |
ac0a286f MK |
740 | AddReservedMemoryBaseSizeHob (\r |
741 | SMM_DEFAULT_SMBASE,\r | |
742 | MCH_DEFAULT_SMBASE_SIZE,\r | |
743 | TRUE /* Cacheable */\r | |
744 | );\r | |
84b223c1 LE |
745 | STATIC_ASSERT (\r |
746 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,\r | |
747 | "end of SMRAM at default SMBASE ends at, or exceeds, 640KB"\r | |
748 | );\r | |
ac0a286f MK |
749 | AddMemoryRangeHob (\r |
750 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,\r | |
751 | BASE_512KB + BASE_128KB\r | |
752 | );\r | |
84b223c1 LE |
753 | } else {\r |
754 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r | |
755 | }\r | |
756 | }\r | |
757 | \r | |
49ba9447 | 758 | /**\r |
c034906e | 759 | Peform Memory Detection for QEMU / KVM\r |
49ba9447 | 760 | \r |
761 | **/\r | |
c034906e JJ |
762 | STATIC\r |
763 | VOID\r | |
764 | QemuInitializeRam (\r | |
765 | VOID\r | |
49ba9447 | 766 | )\r |
767 | {\r | |
ac0a286f MK |
768 | UINT64 LowerMemorySize;\r |
769 | UINT64 UpperMemorySize;\r | |
770 | MTRR_SETTINGS MtrrSettings;\r | |
771 | EFI_STATUS Status;\r | |
49ba9447 | 772 | \r |
70d5086c | 773 | DEBUG ((DEBUG_INFO, "%a called\n", __FUNCTION__));\r |
49ba9447 | 774 | \r |
775 | //\r | |
776 | // Determine total memory size available\r | |
777 | //\r | |
c0e10976 | 778 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
779 | UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r | |
49ba9447 | 780 | \r |
e3e3090a LE |
781 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
782 | //\r | |
783 | // Create the following memory HOB as an exception on the S3 boot path.\r | |
784 | //\r | |
785 | // Normally we'd create memory HOBs only on the normal boot path. However,\r | |
786 | // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r | |
787 | // well, for "borrowing" a subset of it temporarily, for the AP startup\r | |
788 | // vector.\r | |
789 | //\r | |
790 | // CpuMpPei saves the original contents of the borrowed area in permanent\r | |
791 | // PEI RAM, in a backup buffer allocated with the normal PEI services.\r | |
792 | // CpuMpPei restores the original contents ("returns" the borrowed area) at\r | |
793 | // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r | |
8c0b0b34 | 794 | // transferring control to the OS's wakeup vector in the FACS.\r |
e3e3090a LE |
795 | //\r |
796 | // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r | |
797 | // restore the original contents. Furthermore, we expect all such PEIMs\r | |
798 | // (CpuMpPei included) to claim the borrowed areas by producing memory\r | |
799 | // allocation HOBs, and to honor preexistent memory allocation HOBs when\r | |
800 | // looking for an area to borrow.\r | |
801 | //\r | |
84b223c1 | 802 | QemuInitializeRamBelow1gb ();\r |
e3e3090a | 803 | } else {\r |
bd386eaf JJ |
804 | //\r |
805 | // Create memory HOBs\r | |
806 | //\r | |
84b223c1 | 807 | QemuInitializeRamBelow1gb ();\r |
b09c1c6f LE |
808 | \r |
809 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
ac0a286f | 810 | UINT32 TsegSize;\r |
b09c1c6f | 811 | \r |
23bfb5c0 | 812 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f | 813 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r |
ac0a286f MK |
814 | AddReservedMemoryBaseSizeHob (\r |
815 | LowerMemorySize - TsegSize,\r | |
816 | TsegSize,\r | |
817 | TRUE\r | |
818 | );\r | |
b09c1c6f LE |
819 | } else {\r |
820 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r | |
821 | }\r | |
822 | \r | |
1fceaddb LE |
823 | //\r |
824 | // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r | |
825 | // entries. Otherwise, create a single memory HOB with the flat >=4GB\r | |
826 | // memory size read from the CMOS.\r | |
827 | //\r | |
557dede8 | 828 | Status = ScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r |
ac0a286f | 829 | if (EFI_ERROR (Status) && (UpperMemorySize != 0)) {\r |
035ce3b3 | 830 | AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r |
cfc80e2e | 831 | }\r |
bd386eaf | 832 | }\r |
49ba9447 | 833 | \r |
79d274b8 LE |
834 | //\r |
835 | // We'd like to keep the following ranges uncached:\r | |
836 | // - [640 KB, 1 MB)\r | |
837 | // - [LowerMemorySize, 4 GB)\r | |
838 | //\r | |
839 | // Everything else should be WB. Unfortunately, programming the inverse (ie.\r | |
840 | // keeping the default UC, and configuring the complement set of the above as\r | |
841 | // WB) is not reliable in general, because the end of the upper RAM can have\r | |
842 | // practically any alignment, and we may not have enough variable MTRRs to\r | |
843 | // cover it exactly.\r | |
844 | //\r | |
9afcd48a | 845 | if (IsMtrrSupported () && (mHostBridgeDevId != CLOUDHV_DEVICE_ID)) {\r |
79d274b8 LE |
846 | MtrrGetAllMtrrs (&MtrrSettings);\r |
847 | \r | |
848 | //\r | |
849 | // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r | |
850 | //\r | |
851 | ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r | |
852 | ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r | |
853 | ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r | |
854 | \r | |
855 | //\r | |
856 | // flip default type to writeback\r | |
857 | //\r | |
858 | SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r | |
859 | ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r | |
860 | MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r | |
861 | MtrrSetAllMtrrs (&MtrrSettings);\r | |
e8e5cd4a | 862 | \r |
79d274b8 LE |
863 | //\r |
864 | // Set memory range from 640KB to 1MB to uncacheable\r | |
865 | //\r | |
ac0a286f MK |
866 | Status = MtrrSetMemoryAttribute (\r |
867 | BASE_512KB + BASE_128KB,\r | |
868 | BASE_1MB - (BASE_512KB + BASE_128KB),\r | |
869 | CacheUncacheable\r | |
870 | );\r | |
79d274b8 | 871 | ASSERT_EFI_ERROR (Status);\r |
e8e5cd4a | 872 | \r |
79d274b8 | 873 | //\r |
49edde15 LE |
874 | // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI\r |
875 | // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.\r | |
79d274b8 | 876 | //\r |
ac0a286f MK |
877 | Status = MtrrSetMemoryAttribute (\r |
878 | mQemuUc32Base,\r | |
879 | SIZE_4GB - mQemuUc32Base,\r | |
880 | CacheUncacheable\r | |
881 | );\r | |
79d274b8 | 882 | ASSERT_EFI_ERROR (Status);\r |
c0e10976 | 883 | }\r |
49ba9447 | 884 | }\r |
885 | \r | |
c034906e JJ |
886 | /**\r |
887 | Publish system RAM and reserve memory regions\r | |
888 | \r | |
889 | **/\r | |
890 | VOID\r | |
891 | InitializeRamRegions (\r | |
892 | VOID\r | |
893 | )\r | |
894 | {\r | |
d06eb2d1 | 895 | QemuInitializeRam ();\r |
8e54500f | 896 | \r |
ea3a12d9 BS |
897 | SevInitializeRam ();\r |
898 | \r | |
ac0a286f | 899 | if (mS3Supported && (mBootMode != BOOT_ON_S3_RESUME)) {\r |
8e54500f JJ |
900 | //\r |
901 | // This is the memory range that will be used for PEI on S3 resume\r | |
902 | //\r | |
903 | BuildMemoryAllocationHob (\r | |
45d87081 LE |
904 | mS3AcpiReservedMemoryBase,\r |
905 | mS3AcpiReservedMemorySize,\r | |
8e54500f JJ |
906 | EfiACPIMemoryNVS\r |
907 | );\r | |
e249f906 LE |
908 | \r |
909 | //\r | |
910 | // Cover the initial RAM area used as stack and temporary PEI heap.\r | |
911 | //\r | |
912 | // This is reserved as ACPI NVS so it can be used on S3 resume.\r | |
913 | //\r | |
914 | BuildMemoryAllocationHob (\r | |
915 | PcdGet32 (PcdOvmfSecPeiTempRamBase),\r | |
916 | PcdGet32 (PcdOvmfSecPeiTempRamSize),\r | |
917 | EfiACPIMemoryNVS\r | |
918 | );\r | |
78a38b73 | 919 | \r |
ad43bc6b LE |
920 | //\r |
921 | // SEC stores its table of GUIDed section handlers here.\r | |
922 | //\r | |
923 | BuildMemoryAllocationHob (\r | |
924 | PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r | |
925 | PcdGet32 (PcdGuidedExtractHandlerTableSize),\r | |
926 | EfiACPIMemoryNVS\r | |
927 | );\r | |
928 | \r | |
ac0a286f | 929 | #ifdef MDE_CPU_X64\r |
78a38b73 LE |
930 | //\r |
931 | // Reserve the initial page tables built by the reset vector code.\r | |
932 | //\r | |
933 | // Since this memory range will be used by the Reset Vector on S3\r | |
934 | // resume, it must be reserved as ACPI NVS.\r | |
935 | //\r | |
936 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
937 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),\r |
938 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),\r | |
78a38b73 LE |
939 | EfiACPIMemoryNVS\r |
940 | );\r | |
0bbed066 TL |
941 | \r |
942 | if (MemEncryptSevEsIsEnabled ()) {\r | |
943 | //\r | |
944 | // If SEV-ES is enabled, reserve the GHCB-related memory area. This\r | |
945 | // includes the extra page table used to break down the 2MB page\r | |
946 | // mapping into 4KB page entries where the GHCB resides and the\r | |
947 | // GHCB area itself.\r | |
948 | //\r | |
949 | // Since this memory range will be used by the Reset Vector on S3\r | |
950 | // resume, it must be reserved as ACPI NVS.\r | |
951 | //\r | |
952 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
953 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),\r |
954 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),\r | |
0bbed066 TL |
955 | EfiACPIMemoryNVS\r |
956 | );\r | |
957 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
958 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),\r |
959 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),\r | |
0bbed066 TL |
960 | EfiACPIMemoryNVS\r |
961 | );\r | |
36265424 | 962 | BuildMemoryAllocationHob (\r |
ac0a286f MK |
963 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),\r |
964 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),\r | |
36265424 TL |
965 | EfiACPIMemoryNVS\r |
966 | );\r | |
0bbed066 | 967 | }\r |
ac0a286f MK |
968 | \r |
969 | #endif\r | |
0e8a31f5 | 970 | }\r |
6a7cba79 | 971 | \r |
0e8a31f5 | 972 | if (mBootMode != BOOT_ON_S3_RESUME) {\r |
1a7edbbc LE |
973 | if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r |
974 | //\r | |
975 | // Reserve the lock box storage area\r | |
976 | //\r | |
977 | // Since this memory range will be used on S3 resume, it must be\r | |
978 | // reserved as ACPI NVS.\r | |
979 | //\r | |
980 | // If S3 is unsupported, then various drivers might still write to the\r | |
981 | // LockBox area. We ought to prevent DXE from serving allocation requests\r | |
982 | // such that they would overlap the LockBox storage.\r | |
983 | //\r | |
984 | ZeroMem (\r | |
ac0a286f MK |
985 | (VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r |
986 | (UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)\r | |
1a7edbbc LE |
987 | );\r |
988 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
989 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r |
990 | (UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),\r | |
1a7edbbc LE |
991 | mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r |
992 | );\r | |
993 | }\r | |
b09c1c6f LE |
994 | \r |
995 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
ac0a286f | 996 | UINT32 TsegSize;\r |
b09c1c6f LE |
997 | \r |
998 | //\r | |
999 | // Make sure the TSEG area that we reported as a reserved memory resource\r | |
1000 | // cannot be used for reserved memory allocations.\r | |
1001 | //\r | |
23bfb5c0 | 1002 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f | 1003 | BuildMemoryAllocationHob (\r |
ac0a286f | 1004 | GetSystemMemorySizeBelow4gb () - TsegSize,\r |
b09c1c6f LE |
1005 | TsegSize,\r |
1006 | EfiReservedMemoryType\r | |
1007 | );\r | |
84b223c1 LE |
1008 | //\r |
1009 | // Similarly, allocate away the (already reserved) SMRAM at the default\r | |
1010 | // SMBASE, if it exists.\r | |
1011 | //\r | |
1012 | if (mQ35SmramAtDefaultSmbase) {\r | |
1013 | BuildMemoryAllocationHob (\r | |
1014 | SMM_DEFAULT_SMBASE,\r | |
1015 | MCH_DEFAULT_SMBASE_SIZE,\r | |
1016 | EfiReservedMemoryType\r | |
1017 | );\r | |
1018 | }\r | |
b09c1c6f | 1019 | }\r |
0731236f | 1020 | \r |
ac0a286f | 1021 | #ifdef MDE_CPU_X64\r |
80e67af9 | 1022 | if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {\r |
0731236f | 1023 | //\r |
80e67af9 | 1024 | // Reserve the work area.\r |
0731236f TL |
1025 | //\r |
1026 | // Since this memory range will be used by the Reset Vector on S3\r | |
1027 | // resume, it must be reserved as ACPI NVS.\r | |
1028 | //\r | |
1029 | // If S3 is unsupported, then various drivers might still write to the\r | |
1030 | // work area. We ought to prevent DXE from serving allocation requests\r | |
1031 | // such that they would overlap the work area.\r | |
1032 | //\r | |
1033 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
1034 | (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),\r |
1035 | (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),\r | |
0731236f TL |
1036 | mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r |
1037 | );\r | |
1038 | }\r | |
ac0a286f MK |
1039 | \r |
1040 | #endif\r | |
8e54500f | 1041 | }\r |
c034906e | 1042 | }\r |