]>
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 |
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 | |
13 | Module 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 | |
1fceaddb | 22 | #include <IndustryStandard/E820.h>\r |
d5e06444 | 23 | #include <IndustryStandard/Q35MchIch9.h>\r |
49ba9447 | 24 | #include <PiPei.h>\r |
25 | \r | |
26 | //\r | |
27 | // The Library classes this module consumes\r | |
28 | //\r | |
d5e06444 | 29 | #include <Library/BaseLib.h>\r |
6a7cba79 | 30 | #include <Library/BaseMemoryLib.h>\r |
49ba9447 | 31 | #include <Library/DebugLib.h>\r |
32 | #include <Library/HobLib.h>\r | |
33 | #include <Library/IoLib.h>\r | |
c1c2669c | 34 | #include <Library/PcdLib.h>\r |
d5e06444 | 35 | #include <Library/PciLib.h>\r |
49ba9447 | 36 | #include <Library/PeimEntryPoint.h>\r |
37 | #include <Library/ResourcePublicationLib.h>\r | |
e8e5cd4a | 38 | #include <Library/MtrrLib.h>\r |
7e5b1b67 | 39 | #include <Library/QemuFwCfgLib.h>\r |
49ba9447 | 40 | \r |
41 | #include "Platform.h"\r | |
42 | #include "Cmos.h"\r | |
43 | \r | |
bc89fe48 LE |
44 | UINT8 mPhysMemAddressWidth;\r |
45 | \r | |
45d87081 LE |
46 | STATIC UINT32 mS3AcpiReservedMemoryBase;\r |
47 | STATIC UINT32 mS3AcpiReservedMemorySize;\r | |
48 | \r | |
23bfb5c0 LE |
49 | STATIC UINT16 mQ35TsegMbytes;\r |
50 | \r | |
51 | VOID\r | |
52 | Q35TsegMbytesInitialization (\r | |
53 | VOID\r | |
54 | )\r | |
55 | {\r | |
d5e06444 LE |
56 | UINT16 ExtendedTsegMbytes;\r |
57 | RETURN_STATUS PcdStatus;\r | |
58 | \r | |
59 | if (mHostBridgeDevId != INTEL_Q35_MCH_DEVICE_ID) {\r | |
60 | DEBUG ((\r | |
61 | DEBUG_ERROR,\r | |
62 | "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "\r | |
63 | "only DID=0x%04x (Q35) is supported\n",\r | |
64 | __FUNCTION__,\r | |
65 | mHostBridgeDevId,\r | |
66 | INTEL_Q35_MCH_DEVICE_ID\r | |
67 | ));\r | |
68 | ASSERT (FALSE);\r | |
69 | CpuDeadLoop ();\r | |
70 | }\r | |
71 | \r | |
72 | //\r | |
73 | // Check if QEMU offers an extended TSEG.\r | |
74 | //\r | |
75 | // This can be seen from writing MCH_EXT_TSEG_MB_QUERY to the MCH_EXT_TSEG_MB\r | |
76 | // register, and reading back the register.\r | |
77 | //\r | |
78 | // On a QEMU machine type that does not offer an extended TSEG, the initial\r | |
79 | // write overwrites whatever value a malicious guest OS may have placed in\r | |
80 | // the (unimplemented) register, before entering S3 or rebooting.\r | |
81 | // Subsequently, the read returns MCH_EXT_TSEG_MB_QUERY unchanged.\r | |
82 | //\r | |
83 | // On a QEMU machine type that offers an extended TSEG, the initial write\r | |
84 | // triggers an update to the register. Subsequently, the value read back\r | |
85 | // (which is guaranteed to differ from MCH_EXT_TSEG_MB_QUERY) tells us the\r | |
86 | // number of megabytes.\r | |
87 | //\r | |
88 | PciWrite16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB), MCH_EXT_TSEG_MB_QUERY);\r | |
89 | ExtendedTsegMbytes = PciRead16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB));\r | |
90 | if (ExtendedTsegMbytes == MCH_EXT_TSEG_MB_QUERY) {\r | |
91 | mQ35TsegMbytes = PcdGet16 (PcdQ35TsegMbytes);\r | |
92 | return;\r | |
93 | }\r | |
94 | \r | |
95 | DEBUG ((\r | |
96 | DEBUG_INFO,\r | |
97 | "%a: QEMU offers an extended TSEG (%d MB)\n",\r | |
98 | __FUNCTION__,\r | |
99 | ExtendedTsegMbytes\r | |
100 | ));\r | |
101 | PcdStatus = PcdSet16S (PcdQ35TsegMbytes, ExtendedTsegMbytes);\r | |
102 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
103 | mQ35TsegMbytes = ExtendedTsegMbytes;\r | |
23bfb5c0 LE |
104 | }\r |
105 | \r | |
106 | \r | |
1fceaddb LE |
107 | /**\r |
108 | Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r | |
109 | of the 32-bit address range.\r | |
110 | \r | |
111 | Find the highest exclusive >=4GB RAM address, or produce memory resource\r | |
112 | descriptor HOBs for RAM entries that start at or above 4GB.\r | |
113 | \r | |
114 | @param[out] MaxAddress If MaxAddress is NULL, then ScanOrAdd64BitE820Ram()\r | |
115 | produces memory resource descriptor HOBs for RAM\r | |
116 | entries that start at or above 4GB.\r | |
117 | \r | |
118 | Otherwise, MaxAddress holds the highest exclusive\r | |
119 | >=4GB RAM address on output. If QEMU's fw_cfg E820\r | |
120 | RAM map contains no RAM entry that starts outside of\r | |
121 | the 32-bit address range, then MaxAddress is exactly\r | |
122 | 4GB on output.\r | |
123 | \r | |
124 | @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r | |
125 | \r | |
126 | @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r | |
127 | whole multiple of sizeof(EFI_E820_ENTRY64). No\r | |
128 | RAM entry was processed.\r | |
129 | \r | |
130 | @return Error codes from QemuFwCfgFindFile(). No RAM\r | |
131 | entry was processed.\r | |
132 | **/\r | |
133 | STATIC\r | |
134 | EFI_STATUS\r | |
135 | ScanOrAdd64BitE820Ram (\r | |
136 | OUT UINT64 *MaxAddress OPTIONAL\r | |
137 | )\r | |
138 | {\r | |
139 | EFI_STATUS Status;\r | |
140 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
141 | UINTN FwCfgSize;\r | |
142 | EFI_E820_ENTRY64 E820Entry;\r | |
143 | UINTN Processed;\r | |
144 | \r | |
145 | Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r | |
146 | if (EFI_ERROR (Status)) {\r | |
147 | return Status;\r | |
148 | }\r | |
149 | if (FwCfgSize % sizeof E820Entry != 0) {\r | |
150 | return EFI_PROTOCOL_ERROR;\r | |
151 | }\r | |
152 | \r | |
153 | if (MaxAddress != NULL) {\r | |
154 | *MaxAddress = BASE_4GB;\r | |
155 | }\r | |
156 | \r | |
157 | QemuFwCfgSelectItem (FwCfgItem);\r | |
158 | for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r | |
159 | QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r | |
160 | DEBUG ((\r | |
161 | DEBUG_VERBOSE,\r | |
162 | "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r | |
163 | __FUNCTION__,\r | |
164 | E820Entry.BaseAddr,\r | |
165 | E820Entry.Length,\r | |
166 | E820Entry.Type\r | |
167 | ));\r | |
168 | if (E820Entry.Type == EfiAcpiAddressRangeMemory &&\r | |
169 | E820Entry.BaseAddr >= BASE_4GB) {\r | |
170 | if (MaxAddress == NULL) {\r | |
171 | UINT64 Base;\r | |
172 | UINT64 End;\r | |
173 | \r | |
174 | //\r | |
175 | // Round up the start address, and round down the end address.\r | |
176 | //\r | |
177 | Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r | |
178 | End = (E820Entry.BaseAddr + E820Entry.Length) &\r | |
179 | ~(UINT64)EFI_PAGE_MASK;\r | |
180 | if (Base < End) {\r | |
181 | AddMemoryRangeHob (Base, End);\r | |
182 | DEBUG ((\r | |
183 | DEBUG_VERBOSE,\r | |
184 | "%a: AddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r | |
185 | __FUNCTION__,\r | |
186 | Base,\r | |
187 | End\r | |
188 | ));\r | |
189 | }\r | |
190 | } else {\r | |
191 | UINT64 Candidate;\r | |
192 | \r | |
193 | Candidate = E820Entry.BaseAddr + E820Entry.Length;\r | |
194 | if (Candidate > *MaxAddress) {\r | |
195 | *MaxAddress = Candidate;\r | |
196 | DEBUG ((\r | |
197 | DEBUG_VERBOSE,\r | |
198 | "%a: MaxAddress=0x%Lx\n",\r | |
199 | __FUNCTION__,\r | |
200 | *MaxAddress\r | |
201 | ));\r | |
202 | }\r | |
203 | }\r | |
204 | }\r | |
205 | }\r | |
206 | return EFI_SUCCESS;\r | |
207 | }\r | |
208 | \r | |
209 | \r | |
4b455f7b | 210 | UINT32\r |
c0e10976 | 211 | GetSystemMemorySizeBelow4gb (\r |
4b455f7b | 212 | VOID\r |
49ba9447 | 213 | )\r |
214 | {\r | |
215 | UINT8 Cmos0x34;\r | |
216 | UINT8 Cmos0x35;\r | |
217 | \r | |
218 | //\r | |
219 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
220 | // * CMOS(0x35) is the high byte\r | |
221 | // * CMOS(0x34) is the low byte\r | |
222 | // * The size is specified in 64kb chunks\r | |
223 | // * Since this is memory above 16MB, the 16MB must be added\r | |
224 | // into the calculation to get the total memory size.\r | |
225 | //\r | |
226 | \r | |
227 | Cmos0x34 = (UINT8) CmosRead8 (0x34);\r | |
228 | Cmos0x35 = (UINT8) CmosRead8 (0x35);\r | |
229 | \r | |
c4046161 | 230 | return (UINT32) (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r |
49ba9447 | 231 | }\r |
232 | \r | |
233 | \r | |
c0e10976 | 234 | STATIC\r |
235 | UINT64\r | |
236 | GetSystemMemorySizeAbove4gb (\r | |
237 | )\r | |
238 | {\r | |
239 | UINT32 Size;\r | |
240 | UINTN CmosIndex;\r | |
241 | \r | |
242 | //\r | |
243 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
244 | // * CMOS(0x5d) is the most significant size byte\r | |
245 | // * CMOS(0x5c) is the middle size byte\r | |
246 | // * CMOS(0x5b) is the least significant size byte\r | |
247 | // * The size is specified in 64kb chunks\r | |
248 | //\r | |
249 | \r | |
250 | Size = 0;\r | |
251 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
252 | Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);\r | |
253 | }\r | |
254 | \r | |
255 | return LShiftU64 (Size, 16);\r | |
256 | }\r | |
257 | \r | |
bc89fe48 | 258 | \r |
d5371680 LE |
259 | /**\r |
260 | Return the highest address that DXE could possibly use, plus one.\r | |
261 | **/\r | |
262 | STATIC\r | |
263 | UINT64\r | |
264 | GetFirstNonAddress (\r | |
265 | VOID\r | |
266 | )\r | |
267 | {\r | |
268 | UINT64 FirstNonAddress;\r | |
7e5b1b67 LE |
269 | UINT64 Pci64Base, Pci64Size;\r |
270 | CHAR8 MbString[7 + 1];\r | |
271 | EFI_STATUS Status;\r | |
272 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
273 | UINTN FwCfgSize;\r | |
274 | UINT64 HotPlugMemoryEnd;\r | |
32e083c7 | 275 | RETURN_STATUS PcdStatus;\r |
d5371680 | 276 | \r |
1fceaddb LE |
277 | //\r |
278 | // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r | |
279 | //\r | |
280 | FirstNonAddress = 0;\r | |
281 | \r | |
282 | //\r | |
283 | // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r | |
284 | // address from it. This can express an address >= 4GB+1TB.\r | |
285 | //\r | |
286 | // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r | |
287 | // can only express a size smaller than 1TB), and add it to 4GB.\r | |
288 | //\r | |
289 | Status = ScanOrAdd64BitE820Ram (&FirstNonAddress);\r | |
290 | if (EFI_ERROR (Status)) {\r | |
291 | FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r | |
292 | }\r | |
7e5b1b67 LE |
293 | \r |
294 | //\r | |
295 | // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r | |
296 | // resources to 32-bit anyway. See DegradeResource() in\r | |
297 | // "PciResourceSupport.c".\r | |
298 | //\r | |
299 | #ifdef MDE_CPU_IA32\r | |
300 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r | |
301 | return FirstNonAddress;\r | |
302 | }\r | |
303 | #endif\r | |
304 | \r | |
305 | //\r | |
306 | // Otherwise, in order to calculate the highest address plus one, we must\r | |
307 | // consider the 64-bit PCI host aperture too. Fetch the default size.\r | |
308 | //\r | |
309 | Pci64Size = PcdGet64 (PcdPciMmio64Size);\r | |
310 | \r | |
311 | //\r | |
312 | // See if the user specified the number of megabytes for the 64-bit PCI host\r | |
313 | // aperture. The number of non-NUL characters in MbString allows for\r | |
314 | // 9,999,999 MB, which is approximately 10 TB.\r | |
315 | //\r | |
316 | // As signaled by the "X-" prefix, this knob is experimental, and might go\r | |
317 | // away at any time.\r | |
318 | //\r | |
319 | Status = QemuFwCfgFindFile ("opt/ovmf/X-PciMmio64Mb", &FwCfgItem,\r | |
320 | &FwCfgSize);\r | |
321 | if (!EFI_ERROR (Status)) {\r | |
322 | if (FwCfgSize >= sizeof MbString) {\r | |
323 | DEBUG ((EFI_D_WARN,\r | |
324 | "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r | |
325 | __FUNCTION__));\r | |
326 | } else {\r | |
327 | QemuFwCfgSelectItem (FwCfgItem);\r | |
328 | QemuFwCfgReadBytes (FwCfgSize, MbString);\r | |
329 | MbString[FwCfgSize] = '\0';\r | |
330 | Pci64Size = LShiftU64 (AsciiStrDecimalToUint64 (MbString), 20);\r | |
331 | }\r | |
332 | }\r | |
333 | \r | |
334 | if (Pci64Size == 0) {\r | |
335 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
336 | DEBUG ((EFI_D_INFO, "%a: disabling 64-bit PCI host aperture\n",\r | |
337 | __FUNCTION__));\r | |
32e083c7 LE |
338 | PcdStatus = PcdSet64S (PcdPciMmio64Size, 0);\r |
339 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
7e5b1b67 LE |
340 | }\r |
341 | \r | |
342 | //\r | |
343 | // There's nothing more to do; the amount of memory above 4GB fully\r | |
344 | // determines the highest address plus one. The memory hotplug area (see\r | |
345 | // below) plays no role for the firmware in this case.\r | |
346 | //\r | |
347 | return FirstNonAddress;\r | |
348 | }\r | |
349 | \r | |
350 | //\r | |
351 | // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r | |
352 | // absolute, exclusive end address for the memory hotplug area. This area\r | |
353 | // starts right at the end of the memory above 4GB. The 64-bit PCI host\r | |
354 | // aperture must be placed above it.\r | |
355 | //\r | |
356 | Status = QemuFwCfgFindFile ("etc/reserved-memory-end", &FwCfgItem,\r | |
357 | &FwCfgSize);\r | |
358 | if (!EFI_ERROR (Status) && FwCfgSize == sizeof HotPlugMemoryEnd) {\r | |
359 | QemuFwCfgSelectItem (FwCfgItem);\r | |
360 | QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r | |
361 | \r | |
362 | ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r | |
363 | FirstNonAddress = HotPlugMemoryEnd;\r | |
364 | }\r | |
365 | \r | |
366 | //\r | |
367 | // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r | |
368 | // that the host can map it with 1GB hugepages. Follow suit.\r | |
369 | //\r | |
370 | Pci64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r | |
371 | Pci64Size = ALIGN_VALUE (Pci64Size, (UINT64)SIZE_1GB);\r | |
372 | \r | |
373 | //\r | |
374 | // The 64-bit PCI host aperture should also be "naturally" aligned. The\r | |
375 | // alignment is determined by rounding the size of the aperture down to the\r | |
376 | // next smaller or equal power of two. That is, align the aperture by the\r | |
377 | // largest BAR size that can fit into it.\r | |
378 | //\r | |
379 | Pci64Base = ALIGN_VALUE (Pci64Base, GetPowerOfTwo64 (Pci64Size));\r | |
380 | \r | |
381 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
382 | //\r | |
383 | // The core PciHostBridgeDxe driver will automatically add this range to\r | |
384 | // the GCD memory space map through our PciHostBridgeLib instance; here we\r | |
385 | // only need to set the PCDs.\r | |
386 | //\r | |
32e083c7 LE |
387 | PcdStatus = PcdSet64S (PcdPciMmio64Base, Pci64Base);\r |
388 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
389 | PcdStatus = PcdSet64S (PcdPciMmio64Size, Pci64Size);\r | |
390 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
391 | \r | |
7e5b1b67 LE |
392 | DEBUG ((EFI_D_INFO, "%a: Pci64Base=0x%Lx Pci64Size=0x%Lx\n",\r |
393 | __FUNCTION__, Pci64Base, Pci64Size));\r | |
394 | }\r | |
395 | \r | |
396 | //\r | |
397 | // The useful address space ends with the 64-bit PCI host aperture.\r | |
398 | //\r | |
399 | FirstNonAddress = Pci64Base + Pci64Size;\r | |
d5371680 LE |
400 | return FirstNonAddress;\r |
401 | }\r | |
402 | \r | |
403 | \r | |
bc89fe48 LE |
404 | /**\r |
405 | Initialize the mPhysMemAddressWidth variable, based on guest RAM size.\r | |
406 | **/\r | |
407 | VOID\r | |
408 | AddressWidthInitialization (\r | |
409 | VOID\r | |
410 | )\r | |
411 | {\r | |
412 | UINT64 FirstNonAddress;\r | |
413 | \r | |
414 | //\r | |
415 | // As guest-physical memory size grows, the permanent PEI RAM requirements\r | |
416 | // are dominated by the identity-mapping page tables built by the DXE IPL.\r | |
417 | // The DXL IPL keys off of the physical address bits advertized in the CPU\r | |
418 | // HOB. To conserve memory, we calculate the minimum address width here.\r | |
419 | //\r | |
d5371680 | 420 | FirstNonAddress = GetFirstNonAddress ();\r |
bc89fe48 LE |
421 | mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r |
422 | \r | |
423 | //\r | |
424 | // If FirstNonAddress is not an integral power of two, then we need an\r | |
425 | // additional bit.\r | |
426 | //\r | |
427 | if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r | |
428 | ++mPhysMemAddressWidth;\r | |
429 | }\r | |
430 | \r | |
431 | //\r | |
432 | // The minimum address width is 36 (covers up to and excluding 64 GB, which\r | |
433 | // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r | |
434 | // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r | |
435 | // can simply assert that here, since 48 bits are good enough for 256 TB.\r | |
436 | //\r | |
437 | if (mPhysMemAddressWidth <= 36) {\r | |
438 | mPhysMemAddressWidth = 36;\r | |
439 | }\r | |
440 | ASSERT (mPhysMemAddressWidth <= 48);\r | |
441 | }\r | |
442 | \r | |
443 | \r | |
444 | /**\r | |
445 | Calculate the cap for the permanent PEI memory.\r | |
446 | **/\r | |
447 | STATIC\r | |
448 | UINT32\r | |
449 | GetPeiMemoryCap (\r | |
450 | VOID\r | |
451 | )\r | |
452 | {\r | |
453 | BOOLEAN Page1GSupport;\r | |
454 | UINT32 RegEax;\r | |
455 | UINT32 RegEdx;\r | |
456 | UINT32 Pml4Entries;\r | |
457 | UINT32 PdpEntries;\r | |
458 | UINTN TotalPages;\r | |
459 | \r | |
460 | //\r | |
461 | // If DXE is 32-bit, then just return the traditional 64 MB cap.\r | |
462 | //\r | |
463 | #ifdef MDE_CPU_IA32\r | |
464 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r | |
465 | return SIZE_64MB;\r | |
466 | }\r | |
467 | #endif\r | |
468 | \r | |
469 | //\r | |
470 | // Dependent on physical address width, PEI memory allocations can be\r | |
471 | // dominated by the page tables built for 64-bit DXE. So we key the cap off\r | |
472 | // of those. The code below is based on CreateIdentityMappingPageTables() in\r | |
473 | // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".\r | |
474 | //\r | |
475 | Page1GSupport = FALSE;\r | |
476 | if (PcdGetBool (PcdUse1GPageTable)) {\r | |
477 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r | |
478 | if (RegEax >= 0x80000001) {\r | |
479 | AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r | |
480 | if ((RegEdx & BIT26) != 0) {\r | |
481 | Page1GSupport = TRUE;\r | |
482 | }\r | |
483 | }\r | |
484 | }\r | |
485 | \r | |
486 | if (mPhysMemAddressWidth <= 39) {\r | |
487 | Pml4Entries = 1;\r | |
488 | PdpEntries = 1 << (mPhysMemAddressWidth - 30);\r | |
489 | ASSERT (PdpEntries <= 0x200);\r | |
490 | } else {\r | |
491 | Pml4Entries = 1 << (mPhysMemAddressWidth - 39);\r | |
492 | ASSERT (Pml4Entries <= 0x200);\r | |
493 | PdpEntries = 512;\r | |
494 | }\r | |
495 | \r | |
496 | TotalPages = Page1GSupport ? Pml4Entries + 1 :\r | |
497 | (PdpEntries + 1) * Pml4Entries + 1;\r | |
498 | ASSERT (TotalPages <= 0x40201);\r | |
499 | \r | |
500 | //\r | |
501 | // Add 64 MB for miscellaneous allocations. Note that for\r | |
502 | // mPhysMemAddressWidth values close to 36, the cap will actually be\r | |
503 | // dominated by this increment.\r | |
504 | //\r | |
505 | return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB);\r | |
506 | }\r | |
507 | \r | |
508 | \r | |
36658fff WL |
509 | /**\r |
510 | Publish PEI core memory\r | |
511 | \r | |
512 | @return EFI_SUCCESS The PEIM initialized successfully.\r | |
513 | \r | |
514 | **/\r | |
515 | EFI_STATUS\r | |
516 | PublishPeiMemory (\r | |
517 | VOID\r | |
518 | )\r | |
519 | {\r | |
520 | EFI_STATUS Status;\r | |
521 | EFI_PHYSICAL_ADDRESS MemoryBase;\r | |
522 | UINT64 MemorySize;\r | |
fc3f005a | 523 | UINT32 LowerMemorySize;\r |
bc89fe48 | 524 | UINT32 PeiMemoryCap;\r |
36658fff | 525 | \r |
45d87081 LE |
526 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
527 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
528 | //\r | |
529 | // TSEG is chipped from the end of low RAM\r | |
530 | //\r | |
23bfb5c0 | 531 | LowerMemorySize -= mQ35TsegMbytes * SIZE_1MB;\r |
45d87081 LE |
532 | }\r |
533 | \r | |
534 | //\r | |
535 | // If S3 is supported, then the S3 permanent PEI memory is placed next,\r | |
536 | // downwards. Its size is primarily dictated by CpuMpPei. The formula below\r | |
537 | // is an approximation.\r | |
538 | //\r | |
539 | if (mS3Supported) {\r | |
540 | mS3AcpiReservedMemorySize = SIZE_512KB +\r | |
45a70db3 | 541 | mMaxCpuCount *\r |
45d87081 LE |
542 | PcdGet32 (PcdCpuApStackSize);\r |
543 | mS3AcpiReservedMemoryBase = LowerMemorySize - mS3AcpiReservedMemorySize;\r | |
544 | LowerMemorySize = mS3AcpiReservedMemoryBase;\r | |
545 | }\r | |
546 | \r | |
8e54500f | 547 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
45d87081 LE |
548 | MemoryBase = mS3AcpiReservedMemoryBase;\r |
549 | MemorySize = mS3AcpiReservedMemorySize;\r | |
8e54500f | 550 | } else {\r |
bc89fe48 LE |
551 | PeiMemoryCap = GetPeiMemoryCap ();\r |
552 | DEBUG ((EFI_D_INFO, "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",\r | |
553 | __FUNCTION__, mPhysMemAddressWidth, PeiMemoryCap >> 10));\r | |
554 | \r | |
8e54500f JJ |
555 | //\r |
556 | // Determine the range of memory to use during PEI\r | |
557 | //\r | |
efb0f16e LE |
558 | // Technically we could lay the permanent PEI RAM over SEC's temporary\r |
559 | // decompression and scratch buffer even if "secure S3" is needed, since\r | |
560 | // their lifetimes don't overlap. However, PeiFvInitialization() will cover\r | |
561 | // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory\r | |
562 | // allocation HOB, and other allocations served from the permanent PEI RAM\r | |
563 | // shouldn't overlap with that HOB.\r | |
564 | //\r | |
565 | MemoryBase = mS3Supported && FeaturePcdGet (PcdSmmSmramRequire) ?\r | |
566 | PcdGet32 (PcdOvmfDecompressionScratchEnd) :\r | |
567 | PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r | |
8e54500f | 568 | MemorySize = LowerMemorySize - MemoryBase;\r |
bc89fe48 LE |
569 | if (MemorySize > PeiMemoryCap) {\r |
570 | MemoryBase = LowerMemorySize - PeiMemoryCap;\r | |
571 | MemorySize = PeiMemoryCap;\r | |
8e54500f | 572 | }\r |
36658fff WL |
573 | }\r |
574 | \r | |
575 | //\r | |
576 | // Publish this memory to the PEI Core\r | |
577 | //\r | |
578 | Status = PublishSystemMemory(MemoryBase, MemorySize);\r | |
579 | ASSERT_EFI_ERROR (Status);\r | |
580 | \r | |
581 | return Status;\r | |
582 | }\r | |
583 | \r | |
c0e10976 | 584 | \r |
49ba9447 | 585 | /**\r |
c034906e | 586 | Peform Memory Detection for QEMU / KVM\r |
49ba9447 | 587 | \r |
588 | **/\r | |
c034906e JJ |
589 | STATIC\r |
590 | VOID\r | |
591 | QemuInitializeRam (\r | |
592 | VOID\r | |
49ba9447 | 593 | )\r |
594 | {\r | |
c0e10976 | 595 | UINT64 LowerMemorySize;\r |
596 | UINT64 UpperMemorySize;\r | |
79d274b8 LE |
597 | MTRR_SETTINGS MtrrSettings;\r |
598 | EFI_STATUS Status;\r | |
49ba9447 | 599 | \r |
c034906e | 600 | DEBUG ((EFI_D_INFO, "%a called\n", __FUNCTION__));\r |
49ba9447 | 601 | \r |
602 | //\r | |
603 | // Determine total memory size available\r | |
604 | //\r | |
c0e10976 | 605 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
606 | UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r | |
49ba9447 | 607 | \r |
e3e3090a LE |
608 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
609 | //\r | |
610 | // Create the following memory HOB as an exception on the S3 boot path.\r | |
611 | //\r | |
612 | // Normally we'd create memory HOBs only on the normal boot path. However,\r | |
613 | // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r | |
614 | // well, for "borrowing" a subset of it temporarily, for the AP startup\r | |
615 | // vector.\r | |
616 | //\r | |
617 | // CpuMpPei saves the original contents of the borrowed area in permanent\r | |
618 | // PEI RAM, in a backup buffer allocated with the normal PEI services.\r | |
619 | // CpuMpPei restores the original contents ("returns" the borrowed area) at\r | |
620 | // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r | |
8c0b0b34 | 621 | // transferring control to the OS's wakeup vector in the FACS.\r |
e3e3090a LE |
622 | //\r |
623 | // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r | |
624 | // restore the original contents. Furthermore, we expect all such PEIMs\r | |
625 | // (CpuMpPei included) to claim the borrowed areas by producing memory\r | |
626 | // allocation HOBs, and to honor preexistent memory allocation HOBs when\r | |
627 | // looking for an area to borrow.\r | |
628 | //\r | |
629 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r | |
630 | } else {\r | |
bd386eaf JJ |
631 | //\r |
632 | // Create memory HOBs\r | |
633 | //\r | |
bd386eaf | 634 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r |
b09c1c6f LE |
635 | \r |
636 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
637 | UINT32 TsegSize;\r | |
638 | \r | |
23bfb5c0 | 639 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f LE |
640 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r |
641 | AddReservedMemoryBaseSizeHob (LowerMemorySize - TsegSize, TsegSize,\r | |
642 | TRUE);\r | |
643 | } else {\r | |
644 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r | |
645 | }\r | |
646 | \r | |
1fceaddb LE |
647 | //\r |
648 | // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r | |
649 | // entries. Otherwise, create a single memory HOB with the flat >=4GB\r | |
650 | // memory size read from the CMOS.\r | |
651 | //\r | |
652 | Status = ScanOrAdd64BitE820Ram (NULL);\r | |
653 | if (EFI_ERROR (Status) && UpperMemorySize != 0) {\r | |
035ce3b3 | 654 | AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r |
cfc80e2e | 655 | }\r |
bd386eaf | 656 | }\r |
49ba9447 | 657 | \r |
79d274b8 LE |
658 | //\r |
659 | // We'd like to keep the following ranges uncached:\r | |
660 | // - [640 KB, 1 MB)\r | |
661 | // - [LowerMemorySize, 4 GB)\r | |
662 | //\r | |
663 | // Everything else should be WB. Unfortunately, programming the inverse (ie.\r | |
664 | // keeping the default UC, and configuring the complement set of the above as\r | |
665 | // WB) is not reliable in general, because the end of the upper RAM can have\r | |
666 | // practically any alignment, and we may not have enough variable MTRRs to\r | |
667 | // cover it exactly.\r | |
668 | //\r | |
669 | if (IsMtrrSupported ()) {\r | |
670 | MtrrGetAllMtrrs (&MtrrSettings);\r | |
671 | \r | |
672 | //\r | |
673 | // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r | |
674 | //\r | |
675 | ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r | |
676 | ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r | |
677 | ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r | |
678 | \r | |
679 | //\r | |
680 | // flip default type to writeback\r | |
681 | //\r | |
682 | SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r | |
683 | ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r | |
684 | MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r | |
685 | MtrrSetAllMtrrs (&MtrrSettings);\r | |
e8e5cd4a | 686 | \r |
79d274b8 LE |
687 | //\r |
688 | // Set memory range from 640KB to 1MB to uncacheable\r | |
689 | //\r | |
690 | Status = MtrrSetMemoryAttribute (BASE_512KB + BASE_128KB,\r | |
691 | BASE_1MB - (BASE_512KB + BASE_128KB), CacheUncacheable);\r | |
692 | ASSERT_EFI_ERROR (Status);\r | |
e8e5cd4a | 693 | \r |
79d274b8 LE |
694 | //\r |
695 | // Set memory range from the "top of lower RAM" (RAM below 4GB) to 4GB as\r | |
696 | // uncacheable\r | |
697 | //\r | |
698 | Status = MtrrSetMemoryAttribute (LowerMemorySize,\r | |
699 | SIZE_4GB - LowerMemorySize, CacheUncacheable);\r | |
700 | ASSERT_EFI_ERROR (Status);\r | |
c0e10976 | 701 | }\r |
49ba9447 | 702 | }\r |
703 | \r | |
c034906e JJ |
704 | /**\r |
705 | Publish system RAM and reserve memory regions\r | |
706 | \r | |
707 | **/\r | |
708 | VOID\r | |
709 | InitializeRamRegions (\r | |
710 | VOID\r | |
711 | )\r | |
712 | {\r | |
2818c158 JJ |
713 | if (!mXen) {\r |
714 | QemuInitializeRam ();\r | |
715 | } else {\r | |
2818c158 JJ |
716 | XenPublishRamRegions ();\r |
717 | }\r | |
8e54500f JJ |
718 | \r |
719 | if (mS3Supported && mBootMode != BOOT_ON_S3_RESUME) {\r | |
720 | //\r | |
721 | // This is the memory range that will be used for PEI on S3 resume\r | |
722 | //\r | |
723 | BuildMemoryAllocationHob (\r | |
45d87081 LE |
724 | mS3AcpiReservedMemoryBase,\r |
725 | mS3AcpiReservedMemorySize,\r | |
8e54500f JJ |
726 | EfiACPIMemoryNVS\r |
727 | );\r | |
e249f906 LE |
728 | \r |
729 | //\r | |
730 | // Cover the initial RAM area used as stack and temporary PEI heap.\r | |
731 | //\r | |
732 | // This is reserved as ACPI NVS so it can be used on S3 resume.\r | |
733 | //\r | |
734 | BuildMemoryAllocationHob (\r | |
735 | PcdGet32 (PcdOvmfSecPeiTempRamBase),\r | |
736 | PcdGet32 (PcdOvmfSecPeiTempRamSize),\r | |
737 | EfiACPIMemoryNVS\r | |
738 | );\r | |
78a38b73 | 739 | \r |
ad43bc6b LE |
740 | //\r |
741 | // SEC stores its table of GUIDed section handlers here.\r | |
742 | //\r | |
743 | BuildMemoryAllocationHob (\r | |
744 | PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r | |
745 | PcdGet32 (PcdGuidedExtractHandlerTableSize),\r | |
746 | EfiACPIMemoryNVS\r | |
747 | );\r | |
748 | \r | |
78a38b73 LE |
749 | #ifdef MDE_CPU_X64\r |
750 | //\r | |
751 | // Reserve the initial page tables built by the reset vector code.\r | |
752 | //\r | |
753 | // Since this memory range will be used by the Reset Vector on S3\r | |
754 | // resume, it must be reserved as ACPI NVS.\r | |
755 | //\r | |
756 | BuildMemoryAllocationHob (\r | |
757 | (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfSecPageTablesBase),\r | |
758 | (UINT64)(UINTN) PcdGet32 (PcdOvmfSecPageTablesSize),\r | |
759 | EfiACPIMemoryNVS\r | |
760 | );\r | |
761 | #endif\r | |
0e8a31f5 | 762 | }\r |
6a7cba79 | 763 | \r |
0e8a31f5 | 764 | if (mBootMode != BOOT_ON_S3_RESUME) {\r |
1a7edbbc LE |
765 | if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r |
766 | //\r | |
767 | // Reserve the lock box storage area\r | |
768 | //\r | |
769 | // Since this memory range will be used on S3 resume, it must be\r | |
770 | // reserved as ACPI NVS.\r | |
771 | //\r | |
772 | // If S3 is unsupported, then various drivers might still write to the\r | |
773 | // LockBox area. We ought to prevent DXE from serving allocation requests\r | |
774 | // such that they would overlap the LockBox storage.\r | |
775 | //\r | |
776 | ZeroMem (\r | |
777 | (VOID*)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
778 | (UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize)\r | |
779 | );\r | |
780 | BuildMemoryAllocationHob (\r | |
781 | (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
782 | (UINT64)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize),\r | |
783 | mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r | |
784 | );\r | |
785 | }\r | |
b09c1c6f LE |
786 | \r |
787 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
788 | UINT32 TsegSize;\r | |
789 | \r | |
790 | //\r | |
791 | // Make sure the TSEG area that we reported as a reserved memory resource\r | |
792 | // cannot be used for reserved memory allocations.\r | |
793 | //\r | |
23bfb5c0 | 794 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f LE |
795 | BuildMemoryAllocationHob (\r |
796 | GetSystemMemorySizeBelow4gb() - TsegSize,\r | |
797 | TsegSize,\r | |
798 | EfiReservedMemoryType\r | |
799 | );\r | |
800 | }\r | |
8e54500f | 801 | }\r |
c034906e | 802 | }\r |