]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/XenPlatformPei/MemDetect.c
OvmfPkg/XenPlatformPei: Rework memory detection
[mirror_edk2.git] / OvmfPkg / XenPlatformPei / MemDetect.c
CommitLineData
3b96221f
AP
1/**@file\r
2 Memory Detection for Virtual Machines.\r
3\r
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
5 Copyright (c) 2019, Citrix Systems, Inc.\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9Module Name:\r
10\r
11 MemDetect.c\r
12\r
13**/\r
14\r
15//\r
16// The package level header files this module uses\r
17//\r
18#include <IndustryStandard/Q35MchIch9.h>\r
19#include <PiPei.h>\r
20\r
21//\r
22// The Library classes this module consumes\r
23//\r
24#include <Library/BaseLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/DebugLib.h>\r
27#include <Library/HobLib.h>\r
28#include <Library/IoLib.h>\r
29#include <Library/PcdLib.h>\r
30#include <Library/PciLib.h>\r
31#include <Library/PeimEntryPoint.h>\r
32#include <Library/ResourcePublicationLib.h>\r
33\r
34#include "Platform.h"\r
35#include "Cmos.h"\r
36\r
37UINT8 mPhysMemAddressWidth;\r
38\r
39STATIC UINT32 mS3AcpiReservedMemoryBase;\r
40STATIC UINT32 mS3AcpiReservedMemorySize;\r
41\r
42STATIC UINT16 mQ35TsegMbytes;\r
43\r
44VOID\r
45Q35TsegMbytesInitialization (\r
46 VOID\r
47 )\r
48{\r
49 UINT16 ExtendedTsegMbytes;\r
50 RETURN_STATUS PcdStatus;\r
51\r
52 if (mHostBridgeDevId != INTEL_Q35_MCH_DEVICE_ID) {\r
53 DEBUG ((\r
54 DEBUG_ERROR,\r
55 "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "\r
56 "only DID=0x%04x (Q35) is supported\n",\r
57 __FUNCTION__,\r
58 mHostBridgeDevId,\r
59 INTEL_Q35_MCH_DEVICE_ID\r
60 ));\r
61 ASSERT (FALSE);\r
62 CpuDeadLoop ();\r
63 }\r
64\r
65 //\r
66 // Check if QEMU offers an extended TSEG.\r
67 //\r
68 // This can be seen from writing MCH_EXT_TSEG_MB_QUERY to the MCH_EXT_TSEG_MB\r
69 // register, and reading back the register.\r
70 //\r
71 // On a QEMU machine type that does not offer an extended TSEG, the initial\r
72 // write overwrites whatever value a malicious guest OS may have placed in\r
73 // the (unimplemented) register, before entering S3 or rebooting.\r
74 // Subsequently, the read returns MCH_EXT_TSEG_MB_QUERY unchanged.\r
75 //\r
76 // On a QEMU machine type that offers an extended TSEG, the initial write\r
77 // triggers an update to the register. Subsequently, the value read back\r
78 // (which is guaranteed to differ from MCH_EXT_TSEG_MB_QUERY) tells us the\r
79 // number of megabytes.\r
80 //\r
81 PciWrite16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB), MCH_EXT_TSEG_MB_QUERY);\r
82 ExtendedTsegMbytes = PciRead16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB));\r
83 if (ExtendedTsegMbytes == MCH_EXT_TSEG_MB_QUERY) {\r
84 mQ35TsegMbytes = PcdGet16 (PcdQ35TsegMbytes);\r
85 return;\r
86 }\r
87\r
88 DEBUG ((\r
89 DEBUG_INFO,\r
90 "%a: QEMU offers an extended TSEG (%d MB)\n",\r
91 __FUNCTION__,\r
92 ExtendedTsegMbytes\r
93 ));\r
94 PcdStatus = PcdSet16S (PcdQ35TsegMbytes, ExtendedTsegMbytes);\r
95 ASSERT_RETURN_ERROR (PcdStatus);\r
96 mQ35TsegMbytes = ExtendedTsegMbytes;\r
97}\r
98\r
24465c38
AP
99STATIC\r
100UINT64\r
101GetHighestSystemMemoryAddress (\r
102 BOOLEAN Below4gb\r
103 )\r
104{\r
105 EFI_E820_ENTRY64 *E820Map;\r
106 UINT32 E820EntriesCount;\r
107 EFI_E820_ENTRY64 *Entry;\r
108 EFI_STATUS Status;\r
109 UINT32 Loop;\r
110 UINT64 HighestAddress;\r
111 UINT64 EntryEnd;\r
112\r
113 HighestAddress = 0;\r
114\r
115 Status = XenGetE820Map (&E820Map, &E820EntriesCount);\r
116 ASSERT_EFI_ERROR (Status);\r
117\r
118 for (Loop = 0; Loop < E820EntriesCount; Loop++) {\r
119 Entry = E820Map + Loop;\r
120 EntryEnd = Entry->BaseAddr + Entry->Length;\r
121\r
122 if (Entry->Type == EfiAcpiAddressRangeMemory &&\r
123 EntryEnd > HighestAddress) {\r
124\r
125 if (Below4gb && (EntryEnd <= BASE_4GB)) {\r
126 HighestAddress = EntryEnd;\r
127 } else if (!Below4gb && (EntryEnd >= BASE_4GB)) {\r
128 HighestAddress = EntryEnd;\r
129 }\r
130 }\r
131 }\r
132\r
133 //\r
134 // Round down the end address.\r
135 //\r
136 return HighestAddress & ~(UINT64)EFI_PAGE_MASK;\r
137}\r
3b96221f
AP
138\r
139UINT32\r
140GetSystemMemorySizeBelow4gb (\r
141 VOID\r
142 )\r
143{\r
144 UINT8 Cmos0x34;\r
145 UINT8 Cmos0x35;\r
146\r
24465c38
AP
147 //\r
148 // In PVH case, there is no CMOS, we have to calculate the memory size\r
149 // from parsing the E820\r
150 //\r
151 if (XenPvhDetected ()) {\r
152 UINT64 HighestAddress;\r
153\r
154 HighestAddress = GetHighestSystemMemoryAddress (TRUE);\r
155 ASSERT (HighestAddress > 0 && HighestAddress <= BASE_4GB);\r
156\r
157 return HighestAddress;\r
158 }\r
159\r
3b96221f
AP
160 //\r
161 // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r
162 // * CMOS(0x35) is the high byte\r
163 // * CMOS(0x34) is the low byte\r
164 // * The size is specified in 64kb chunks\r
165 // * Since this is memory above 16MB, the 16MB must be added\r
166 // into the calculation to get the total memory size.\r
167 //\r
168\r
169 Cmos0x34 = (UINT8) CmosRead8 (0x34);\r
170 Cmos0x35 = (UINT8) CmosRead8 (0x35);\r
171\r
172 return (UINT32) (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
173}\r
174\r
175\r
176STATIC\r
177UINT64\r
178GetSystemMemorySizeAbove4gb (\r
179 )\r
180{\r
181 UINT32 Size;\r
182 UINTN CmosIndex;\r
183\r
24465c38
AP
184 //\r
185 // In PVH case, there is no CMOS, we have to calculate the memory size\r
186 // from parsing the E820\r
187 //\r
188 if (XenPvhDetected ()) {\r
189 UINT64 HighestAddress;\r
190\r
191 HighestAddress = GetHighestSystemMemoryAddress (FALSE);\r
192 ASSERT (HighestAddress == 0 || HighestAddress >= BASE_4GB);\r
193\r
194 if (HighestAddress >= BASE_4GB) {\r
195 HighestAddress -= BASE_4GB;\r
196 }\r
197\r
198 return HighestAddress;\r
199 }\r
200\r
3b96221f
AP
201 //\r
202 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r
203 // * CMOS(0x5d) is the most significant size byte\r
204 // * CMOS(0x5c) is the middle size byte\r
205 // * CMOS(0x5b) is the least significant size byte\r
206 // * The size is specified in 64kb chunks\r
207 //\r
208\r
209 Size = 0;\r
210 for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r
211 Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);\r
212 }\r
213\r
214 return LShiftU64 (Size, 16);\r
215}\r
216\r
217\r
218/**\r
219 Return the highest address that DXE could possibly use, plus one.\r
220**/\r
221STATIC\r
222UINT64\r
223GetFirstNonAddress (\r
224 VOID\r
225 )\r
226{\r
227 UINT64 FirstNonAddress;\r
228 UINT64 Pci64Base, Pci64Size;\r
229 RETURN_STATUS PcdStatus;\r
230\r
231 FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r
232\r
233 //\r
234 // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r
235 // resources to 32-bit anyway. See DegradeResource() in\r
236 // "PciResourceSupport.c".\r
237 //\r
238#ifdef MDE_CPU_IA32\r
239 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
240 return FirstNonAddress;\r
241 }\r
242#endif\r
243\r
244 //\r
245 // Otherwise, in order to calculate the highest address plus one, we must\r
246 // consider the 64-bit PCI host aperture too. Fetch the default size.\r
247 //\r
248 Pci64Size = PcdGet64 (PcdPciMmio64Size);\r
249\r
250 if (Pci64Size == 0) {\r
251 if (mBootMode != BOOT_ON_S3_RESUME) {\r
252 DEBUG ((DEBUG_INFO, "%a: disabling 64-bit PCI host aperture\n",\r
253 __FUNCTION__));\r
254 PcdStatus = PcdSet64S (PcdPciMmio64Size, 0);\r
255 ASSERT_RETURN_ERROR (PcdStatus);\r
256 }\r
257\r
258 //\r
259 // There's nothing more to do; the amount of memory above 4GB fully\r
260 // determines the highest address plus one. The memory hotplug area (see\r
261 // below) plays no role for the firmware in this case.\r
262 //\r
263 return FirstNonAddress;\r
264 }\r
265\r
266 //\r
267 // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r
268 // that the host can map it with 1GB hugepages. Follow suit.\r
269 //\r
270 Pci64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r
271 Pci64Size = ALIGN_VALUE (Pci64Size, (UINT64)SIZE_1GB);\r
272\r
273 //\r
274 // The 64-bit PCI host aperture should also be "naturally" aligned. The\r
275 // alignment is determined by rounding the size of the aperture down to the\r
276 // next smaller or equal power of two. That is, align the aperture by the\r
277 // largest BAR size that can fit into it.\r
278 //\r
279 Pci64Base = ALIGN_VALUE (Pci64Base, GetPowerOfTwo64 (Pci64Size));\r
280\r
281 if (mBootMode != BOOT_ON_S3_RESUME) {\r
282 //\r
283 // The core PciHostBridgeDxe driver will automatically add this range to\r
284 // the GCD memory space map through our PciHostBridgeLib instance; here we\r
285 // only need to set the PCDs.\r
286 //\r
287 PcdStatus = PcdSet64S (PcdPciMmio64Base, Pci64Base);\r
288 ASSERT_RETURN_ERROR (PcdStatus);\r
289 PcdStatus = PcdSet64S (PcdPciMmio64Size, Pci64Size);\r
290 ASSERT_RETURN_ERROR (PcdStatus);\r
291\r
292 DEBUG ((DEBUG_INFO, "%a: Pci64Base=0x%Lx Pci64Size=0x%Lx\n",\r
293 __FUNCTION__, Pci64Base, Pci64Size));\r
294 }\r
295\r
296 //\r
297 // The useful address space ends with the 64-bit PCI host aperture.\r
298 //\r
299 FirstNonAddress = Pci64Base + Pci64Size;\r
300 return FirstNonAddress;\r
301}\r
302\r
303\r
304/**\r
305 Initialize the mPhysMemAddressWidth variable, based on guest RAM size.\r
306**/\r
307VOID\r
308AddressWidthInitialization (\r
309 VOID\r
310 )\r
311{\r
312 UINT64 FirstNonAddress;\r
313\r
314 //\r
315 // As guest-physical memory size grows, the permanent PEI RAM requirements\r
316 // are dominated by the identity-mapping page tables built by the DXE IPL.\r
317 // The DXL IPL keys off of the physical address bits advertized in the CPU\r
318 // HOB. To conserve memory, we calculate the minimum address width here.\r
319 //\r
320 FirstNonAddress = GetFirstNonAddress ();\r
321 mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r
322\r
323 //\r
324 // If FirstNonAddress is not an integral power of two, then we need an\r
325 // additional bit.\r
326 //\r
327 if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r
328 ++mPhysMemAddressWidth;\r
329 }\r
330\r
331 //\r
332 // The minimum address width is 36 (covers up to and excluding 64 GB, which\r
333 // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r
334 // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r
335 // can simply assert that here, since 48 bits are good enough for 256 TB.\r
336 //\r
337 if (mPhysMemAddressWidth <= 36) {\r
338 mPhysMemAddressWidth = 36;\r
339 }\r
340 ASSERT (mPhysMemAddressWidth <= 48);\r
341}\r
342\r
343\r
344/**\r
345 Calculate the cap for the permanent PEI memory.\r
346**/\r
347STATIC\r
348UINT32\r
349GetPeiMemoryCap (\r
350 VOID\r
351 )\r
352{\r
353 BOOLEAN Page1GSupport;\r
354 UINT32 RegEax;\r
355 UINT32 RegEdx;\r
356 UINT32 Pml4Entries;\r
357 UINT32 PdpEntries;\r
358 UINTN TotalPages;\r
359\r
360 //\r
361 // If DXE is 32-bit, then just return the traditional 64 MB cap.\r
362 //\r
363#ifdef MDE_CPU_IA32\r
364 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
365 return SIZE_64MB;\r
366 }\r
367#endif\r
368\r
369 //\r
370 // Dependent on physical address width, PEI memory allocations can be\r
371 // dominated by the page tables built for 64-bit DXE. So we key the cap off\r
372 // of those. The code below is based on CreateIdentityMappingPageTables() in\r
373 // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".\r
374 //\r
375 Page1GSupport = FALSE;\r
376 if (PcdGetBool (PcdUse1GPageTable)) {\r
377 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
378 if (RegEax >= 0x80000001) {\r
379 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
380 if ((RegEdx & BIT26) != 0) {\r
381 Page1GSupport = TRUE;\r
382 }\r
383 }\r
384 }\r
385\r
386 if (mPhysMemAddressWidth <= 39) {\r
387 Pml4Entries = 1;\r
388 PdpEntries = 1 << (mPhysMemAddressWidth - 30);\r
389 ASSERT (PdpEntries <= 0x200);\r
390 } else {\r
391 Pml4Entries = 1 << (mPhysMemAddressWidth - 39);\r
392 ASSERT (Pml4Entries <= 0x200);\r
393 PdpEntries = 512;\r
394 }\r
395\r
396 TotalPages = Page1GSupport ? Pml4Entries + 1 :\r
397 (PdpEntries + 1) * Pml4Entries + 1;\r
398 ASSERT (TotalPages <= 0x40201);\r
399\r
400 //\r
401 // Add 64 MB for miscellaneous allocations. Note that for\r
402 // mPhysMemAddressWidth values close to 36, the cap will actually be\r
403 // dominated by this increment.\r
404 //\r
405 return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB);\r
406}\r
407\r
408\r
409/**\r
410 Publish PEI core memory\r
411\r
412 @return EFI_SUCCESS The PEIM initialized successfully.\r
413\r
414**/\r
415EFI_STATUS\r
416PublishPeiMemory (\r
417 VOID\r
418 )\r
419{\r
420 EFI_STATUS Status;\r
421 EFI_PHYSICAL_ADDRESS MemoryBase;\r
422 UINT64 MemorySize;\r
423 UINT32 LowerMemorySize;\r
424 UINT32 PeiMemoryCap;\r
425\r
426 LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
427\r
428 if (mBootMode == BOOT_ON_S3_RESUME) {\r
429 MemoryBase = mS3AcpiReservedMemoryBase;\r
430 MemorySize = mS3AcpiReservedMemorySize;\r
431 } else {\r
432 PeiMemoryCap = GetPeiMemoryCap ();\r
433 DEBUG ((DEBUG_INFO, "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",\r
434 __FUNCTION__, mPhysMemAddressWidth, PeiMemoryCap >> 10));\r
435\r
436 //\r
437 // Determine the range of memory to use during PEI\r
438 //\r
439 MemoryBase =\r
440 PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r
441 MemorySize = LowerMemorySize - MemoryBase;\r
442 if (MemorySize > PeiMemoryCap) {\r
443 MemoryBase = LowerMemorySize - PeiMemoryCap;\r
444 MemorySize = PeiMemoryCap;\r
445 }\r
446 }\r
447\r
448 //\r
449 // Publish this memory to the PEI Core\r
450 //\r
451 Status = PublishSystemMemory(MemoryBase, MemorySize);\r
452 ASSERT_EFI_ERROR (Status);\r
453\r
454 return Status;\r
455}\r
456\r
457\r
458/**\r
459 Publish system RAM and reserve memory regions\r
460\r
461**/\r
462VOID\r
463InitializeRamRegions (\r
464 VOID\r
465 )\r
466{\r
467 XenPublishRamRegions ();\r
468\r
469 if (mBootMode != BOOT_ON_S3_RESUME) {\r
470 //\r
471 // Reserve the lock box storage area\r
472 //\r
473 // Since this memory range will be used on S3 resume, it must be\r
474 // reserved as ACPI NVS.\r
475 //\r
476 // If S3 is unsupported, then various drivers might still write to the\r
477 // LockBox area. We ought to prevent DXE from serving allocation requests\r
478 // such that they would overlap the LockBox storage.\r
479 //\r
480 ZeroMem (\r
481 (VOID*)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
482 (UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize)\r
483 );\r
484 BuildMemoryAllocationHob (\r
485 (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
486 (UINT64)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize),\r
487 EfiBootServicesData\r
488 );\r
489 }\r
490}\r