]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PlatformPei/Platform.c
OvmfPkg: raise the PCIEXBAR base to 2816 MB on Q35
[mirror_edk2.git] / OvmfPkg / PlatformPei / Platform.c
CommitLineData
49ba9447 1/**@file\r
2 Platform PEI driver\r
3\r
a1726e30 4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
eec7d420 5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r
6\r
b26f0cf9 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
49ba9447 8\r
9**/\r
10\r
11//\r
12// The package level header files this module uses\r
13//\r
14#include <PiPei.h>\r
15\r
16//\r
17// The Library classes this module consumes\r
18//\r
5133d1f1 19#include <Library/BaseLib.h>\r
49ba9447 20#include <Library/DebugLib.h>\r
21#include <Library/HobLib.h>\r
22#include <Library/IoLib.h>\r
77ba993c 23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/PcdLib.h>\r
49ba9447 25#include <Library/PciLib.h>\r
26#include <Library/PeimEntryPoint.h>\r
9ed65b10 27#include <Library/PeiServicesLib.h>\r
7cdba634 28#include <Library/QemuFwCfgLib.h>\r
687f7521 29#include <Library/QemuFwCfgS3Lib.h>\r
49ba9447 30#include <Library/ResourcePublicationLib.h>\r
31#include <Guid/MemoryTypeInformation.h>\r
9ed65b10 32#include <Ppi/MasterBootMode.h>\r
931a0c74 33#include <IndustryStandard/Pci22.h>\r
97380beb 34#include <OvmfPlatforms.h>\r
49ba9447 35\r
36#include "Platform.h"\r
3ca15914 37#include "Cmos.h"\r
49ba9447 38\r
39EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {\r
40 { EfiACPIMemoryNVS, 0x004 },\r
991d9563 41 { EfiACPIReclaimMemory, 0x008 },\r
55cdb67a 42 { EfiReservedMemoryType, 0x004 },\r
991d9563 43 { EfiRuntimeServicesData, 0x024 },\r
44 { EfiRuntimeServicesCode, 0x030 },\r
45 { EfiBootServicesCode, 0x180 },\r
46 { EfiBootServicesData, 0xF00 },\r
49ba9447 47 { EfiMaxMemoryType, 0x000 }\r
48};\r
49\r
50\r
9ed65b10 51EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {\r
52 {\r
53 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
54 &gEfiPeiMasterBootModePpiGuid,\r
55 NULL\r
56 }\r
57};\r
58\r
59\r
589756c7
PA
60UINT16 mHostBridgeDevId;\r
61\r
979420df
JJ
62EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;\r
63\r
7cdba634
JJ
64BOOLEAN mS3Supported = FALSE;\r
65\r
45a70db3 66UINT32 mMaxCpuCount;\r
979420df 67\r
49ba9447 68VOID\r
69AddIoMemoryBaseSizeHob (\r
70 EFI_PHYSICAL_ADDRESS MemoryBase,\r
71 UINT64 MemorySize\r
72 )\r
73{\r
991d9563 74 BuildResourceDescriptorHob (\r
75 EFI_RESOURCE_MEMORY_MAPPED_IO,\r
49ba9447 76 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
77 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
78 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
991d9563 79 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
49ba9447 80 MemoryBase,\r
81 MemorySize\r
82 );\r
83}\r
84\r
eec7d420 85VOID\r
86AddReservedMemoryBaseSizeHob (\r
87 EFI_PHYSICAL_ADDRESS MemoryBase,\r
cdef34ec
LE
88 UINT64 MemorySize,\r
89 BOOLEAN Cacheable\r
eec7d420 90 )\r
91{\r
92 BuildResourceDescriptorHob (\r
93 EFI_RESOURCE_MEMORY_RESERVED,\r
94 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
95 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
96 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
cdef34ec
LE
97 (Cacheable ?\r
98 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
99 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
100 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :\r
101 0\r
102 ) |\r
eec7d420 103 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
104 MemoryBase,\r
105 MemorySize\r
106 );\r
107}\r
49ba9447 108\r
109VOID\r
110AddIoMemoryRangeHob (\r
111 EFI_PHYSICAL_ADDRESS MemoryBase,\r
112 EFI_PHYSICAL_ADDRESS MemoryLimit\r
113 )\r
114{\r
115 AddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
116}\r
117\r
118\r
119VOID\r
120AddMemoryBaseSizeHob (\r
121 EFI_PHYSICAL_ADDRESS MemoryBase,\r
122 UINT64 MemorySize\r
123 )\r
124{\r
991d9563 125 BuildResourceDescriptorHob (\r
126 EFI_RESOURCE_SYSTEM_MEMORY,\r
49ba9447 127 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
128 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
129 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
130 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
131 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
132 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
991d9563 133 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
49ba9447 134 MemoryBase,\r
135 MemorySize\r
136 );\r
137}\r
138\r
139\r
140VOID\r
141AddMemoryRangeHob (\r
142 EFI_PHYSICAL_ADDRESS MemoryBase,\r
143 EFI_PHYSICAL_ADDRESS MemoryLimit\r
144 )\r
145{\r
146 AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
147}\r
148\r
c0e10976 149\r
bb6a9a93 150VOID\r
4b455f7b 151MemMapInitialization (\r
bb6a9a93
WL
152 VOID\r
153 )\r
154{\r
32e083c7
LE
155 UINT64 PciIoBase;\r
156 UINT64 PciIoSize;\r
157 RETURN_STATUS PcdStatus;\r
c4df7fd0
LE
158\r
159 PciIoBase = 0xC000;\r
160 PciIoSize = 0x4000;\r
161\r
bb6a9a93
WL
162 //\r
163 // Create Memory Type Information HOB\r
164 //\r
165 BuildGuidDataHob (\r
166 &gEfiMemoryTypeInformationGuid,\r
167 mDefaultMemoryTypeInformation,\r
168 sizeof(mDefaultMemoryTypeInformation)\r
169 );\r
170\r
bb6a9a93
WL
171 //\r
172 // Video memory + Legacy BIOS region\r
173 //\r
174 AddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r
175\r
4b455f7b 176 if (!mXen) {\r
305cd4f7 177 UINT32 TopOfLowRam;\r
7b8fe635 178 UINT64 PciExBarBase;\r
c68d3a69 179 UINT32 PciBase;\r
03845e90 180 UINT32 PciSize;\r
c68d3a69 181\r
305cd4f7 182 TopOfLowRam = GetSystemMemorySizeBelow4gb ();\r
02d6f4ce 183 PciExBarBase = 0;\r
c68d3a69
LE
184 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
185 //\r
eb4d62b0
LE
186 // The MMCONFIG area is expected to fall between the top of low RAM and\r
187 // the base of the 32-bit PCI host aperture.\r
c68d3a69 188 //\r
7b8fe635 189 PciExBarBase = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
eb4d62b0 190 ASSERT (TopOfLowRam <= PciExBarBase);\r
7b8fe635 191 ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r
eb4d62b0 192 PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r
c68d3a69 193 } else {\r
753d3d6f 194 PciBase = (TopOfLowRam < BASE_2GB) ? BASE_2GB : TopOfLowRam;\r
c68d3a69 195 }\r
49ba9447 196\r
4b455f7b
JJ
197 //\r
198 // address purpose size\r
199 // ------------ -------- -------------------------\r
200 // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r
201 // 0xFC000000 gap 44 MB\r
202 // 0xFEC00000 IO-APIC 4 KB\r
203 // 0xFEC01000 gap 1020 KB\r
204 // 0xFED00000 HPET 1 KB\r
90721ba5
PA
205 // 0xFED00400 gap 111 KB\r
206 // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r
207 // 0xFED20000 gap 896 KB\r
4b455f7b
JJ
208 // 0xFEE00000 LAPIC 1 MB\r
209 //\r
d4534984 210 PciSize = 0xFC000000 - PciBase;\r
03845e90 211 AddIoMemoryBaseSizeHob (PciBase, PciSize);\r
32e083c7
LE
212 PcdStatus = PcdSet64S (PcdPciMmio32Base, PciBase);\r
213 ASSERT_RETURN_ERROR (PcdStatus);\r
214 PcdStatus = PcdSet64S (PcdPciMmio32Size, PciSize);\r
215 ASSERT_RETURN_ERROR (PcdStatus);\r
216\r
4b455f7b
JJ
217 AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r
218 AddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r
90721ba5
PA
219 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
220 AddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r
7b8fe635
LE
221 //\r
222 // Note: there should be an\r
223 //\r
224 // AddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r
225 //\r
226 // call below, just like the one above for RCBA. However, Linux insists\r
227 // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r
228 // "reserved memory" -- Linux does not content itself with a simple gap\r
229 // in the memory map wherever the MCFG ACPI table points to.\r
230 //\r
231 // This appears to be a safety measure. The PCI Firmware Specification\r
232 // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r
233 // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r
234 // [...]". (Emphasis added here.)\r
235 //\r
236 // Normally we add memory resource descriptor HOBs in\r
237 // QemuInitializeRam(), and pre-allocate from those with memory\r
238 // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r
239 // is most definitely not RAM; so, as an exception, cover it with\r
240 // uncacheable reserved memory right here.\r
241 //\r
242 AddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r
243 BuildMemoryAllocationHob (PciExBarBase, SIZE_256MB,\r
244 EfiReservedMemoryType);\r
90721ba5 245 }\r
4b455f7b 246 AddIoMemoryBaseSizeHob (PcdGet32(PcdCpuLocalApicBaseAddress), SIZE_1MB);\r
bba734ab
LE
247\r
248 //\r
249 // On Q35, the IO Port space is available for PCI resource allocations from\r
250 // 0x6000 up.\r
251 //\r
252 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
253 PciIoBase = 0x6000;\r
254 PciIoSize = 0xA000;\r
255 ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r
256 }\r
4b455f7b 257 }\r
c4df7fd0
LE
258\r
259 //\r
260 // Add PCI IO Port space available for PCI resource allocations.\r
261 //\r
262 BuildResourceDescriptorHob (\r
263 EFI_RESOURCE_IO,\r
264 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
265 EFI_RESOURCE_ATTRIBUTE_INITIALIZED,\r
266 PciIoBase,\r
267 PciIoSize\r
268 );\r
32e083c7
LE
269 PcdStatus = PcdSet64S (PcdPciIoBase, PciIoBase);\r
270 ASSERT_RETURN_ERROR (PcdStatus);\r
271 PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);\r
272 ASSERT_RETURN_ERROR (PcdStatus);\r
49ba9447 273}\r
274\r
ab081a50
LE
275EFI_STATUS\r
276GetNamedFwCfgBoolean (\r
277 IN CHAR8 *FwCfgFileName,\r
278 OUT BOOLEAN *Setting\r
279 )\r
280{\r
281 EFI_STATUS Status;\r
282 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
283 UINTN FwCfgSize;\r
284 UINT8 Value[3];\r
285\r
286 Status = QemuFwCfgFindFile (FwCfgFileName, &FwCfgItem, &FwCfgSize);\r
287 if (EFI_ERROR (Status)) {\r
288 return Status;\r
289 }\r
290 if (FwCfgSize > sizeof Value) {\r
291 return EFI_BAD_BUFFER_SIZE;\r
292 }\r
293 QemuFwCfgSelectItem (FwCfgItem);\r
294 QemuFwCfgReadBytes (FwCfgSize, Value);\r
295\r
296 if ((FwCfgSize == 1) ||\r
297 (FwCfgSize == 2 && Value[1] == '\n') ||\r
298 (FwCfgSize == 3 && Value[1] == '\r' && Value[2] == '\n')) {\r
299 switch (Value[0]) {\r
300 case '0':\r
301 case 'n':\r
302 case 'N':\r
303 *Setting = FALSE;\r
304 return EFI_SUCCESS;\r
305\r
306 case '1':\r
307 case 'y':\r
308 case 'Y':\r
309 *Setting = TRUE;\r
310 return EFI_SUCCESS;\r
311\r
312 default:\r
313 break;\r
314 }\r
315 }\r
316 return EFI_PROTOCOL_ERROR;\r
317}\r
318\r
319#define UPDATE_BOOLEAN_PCD_FROM_FW_CFG(TokenName) \\r
320 do { \\r
32e083c7
LE
321 BOOLEAN Setting; \\r
322 RETURN_STATUS PcdStatus; \\r
ab081a50
LE
323 \\r
324 if (!EFI_ERROR (GetNamedFwCfgBoolean ( \\r
325 "opt/ovmf/" #TokenName, &Setting))) { \\r
32e083c7
LE
326 PcdStatus = PcdSetBoolS (TokenName, Setting); \\r
327 ASSERT_RETURN_ERROR (PcdStatus); \\r
ab081a50
LE
328 } \\r
329 } while (0)\r
330\r
331VOID\r
332NoexecDxeInitialization (\r
333 VOID\r
334 )\r
335{\r
336 UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdPropertiesTableEnable);\r
337 UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdSetNxForStack);\r
338}\r
49ba9447 339\r
7b8fe635
LE
340VOID\r
341PciExBarInitialization (\r
342 VOID\r
343 )\r
344{\r
345 union {\r
346 UINT64 Uint64;\r
347 UINT32 Uint32[2];\r
348 } PciExBarBase;\r
349\r
350 //\r
351 // We only support the 256MB size for the MMCONFIG area:\r
352 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.\r
353 //\r
354 // The masks used below enforce the Q35 requirements that the MMCONFIG area\r
355 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.\r
356 //\r
357 // Note that (b) also ensures that the minimum address width we have\r
358 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice\r
359 // for DXE's page tables to cover the MMCONFIG area.\r
360 //\r
361 PciExBarBase.Uint64 = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
362 ASSERT ((PciExBarBase.Uint32[1] & MCH_PCIEXBAR_HIGHMASK) == 0);\r
363 ASSERT ((PciExBarBase.Uint32[0] & MCH_PCIEXBAR_LOWMASK) == 0);\r
364\r
365 //\r
366 // Clear the PCIEXBAREN bit first, before programming the high register.\r
367 //\r
368 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0);\r
369\r
370 //\r
371 // Program the high register. Then program the low register, setting the\r
372 // MMCONFIG area size and enabling decoding at once.\r
373 //\r
374 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[1]);\r
375 PciWrite32 (\r
376 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW),\r
377 PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN\r
378 );\r
379}\r
380\r
49ba9447 381VOID\r
382MiscInitialization (\r
0e20a186 383 VOID\r
49ba9447 384 )\r
385{\r
32e083c7
LE
386 UINTN PmCmd;\r
387 UINTN Pmba;\r
388 UINT32 PmbaAndVal;\r
389 UINT32 PmbaOrVal;\r
390 UINTN AcpiCtlReg;\r
391 UINT8 AcpiEnBit;\r
392 RETURN_STATUS PcdStatus;\r
97380beb 393\r
49ba9447 394 //\r
395 // Disable A20 Mask\r
396 //\r
55cdb67a 397 IoOr8 (0x92, BIT1);\r
49ba9447 398\r
399 //\r
86a14b0a
LE
400 // Build the CPU HOB with guest RAM size dependent address width and 16-bits\r
401 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r
402 // S3 resume as well, so we build it unconditionally.)\r
49ba9447 403 //\r
86a14b0a 404 BuildCpuHob (mPhysMemAddressWidth, 16);\r
c756b2ab 405\r
97380beb 406 //\r
589756c7 407 // Determine platform type and save Host Bridge DID to PCD\r
97380beb 408 //\r
589756c7 409 switch (mHostBridgeDevId) {\r
97380beb 410 case INTEL_82441_DEVICE_ID:\r
e2ab3f81 411 PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);\r
da372167 412 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r
1466b76f
LE
413 PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;\r
414 PmbaOrVal = PIIX4_PMBA_VALUE;\r
da372167
LE
415 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);\r
416 AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;\r
97380beb
GS
417 break;\r
418 case INTEL_Q35_MCH_DEVICE_ID:\r
e2ab3f81 419 PmCmd = POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET);\r
bc9d05d6 420 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
1466b76f
LE
421 PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;\r
422 PmbaOrVal = ICH9_PMBASE_VALUE;\r
bc9d05d6
LE
423 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r
424 AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r
97380beb
GS
425 break;\r
426 default:\r
427 DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
589756c7 428 __FUNCTION__, mHostBridgeDevId));\r
97380beb
GS
429 ASSERT (FALSE);\r
430 return;\r
431 }\r
32e083c7
LE
432 PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r
433 ASSERT_RETURN_ERROR (PcdStatus);\r
97380beb 434\r
0e20a186 435 //\r
e2ab3f81
GS
436 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA\r
437 // has been configured (e.g., by Xen) and skip the setup here.\r
438 // This matches the logic in AcpiTimerLibConstructor ().\r
0e20a186 439 //\r
e2ab3f81 440 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r
eec7d420 441 //\r
e2ab3f81 442 // The PEI phase should be exited with fully accessibe ACPI PM IO space:\r
931a0c74 443 // 1. set PMBA\r
eec7d420 444 //\r
1466b76f 445 PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);\r
931a0c74 446\r
447 //\r
448 // 2. set PCICMD/IOSE\r
449 //\r
97380beb 450 PciOr8 (PmCmd, EFI_PCI_COMMAND_IO_SPACE);\r
931a0c74 451\r
452 //\r
e2ab3f81 453 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)\r
931a0c74 454 //\r
e2ab3f81 455 PciOr8 (AcpiCtlReg, AcpiEnBit);\r
eec7d420 456 }\r
90721ba5
PA
457\r
458 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
459 //\r
460 // Set Root Complex Register Block BAR\r
461 //\r
462 PciWrite32 (\r
463 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA),\r
464 ICH9_ROOT_COMPLEX_BASE | ICH9_RCBA_EN\r
465 );\r
7b8fe635
LE
466\r
467 //\r
468 // Set PCI Express Register Range Base Address\r
469 //\r
470 PciExBarInitialization ();\r
90721ba5 471 }\r
49ba9447 472}\r
473\r
474\r
9ed65b10 475VOID\r
476BootModeInitialization (\r
8f5ca05b 477 VOID\r
9ed65b10 478 )\r
479{\r
8f5ca05b
LE
480 EFI_STATUS Status;\r
481\r
482 if (CmosRead8 (0xF) == 0xFE) {\r
979420df 483 mBootMode = BOOT_ON_S3_RESUME;\r
8f5ca05b 484 }\r
9be75189 485 CmosWrite8 (0xF, 0x00);\r
667bf1e4 486\r
979420df 487 Status = PeiServicesSetBootMode (mBootMode);\r
667bf1e4 488 ASSERT_EFI_ERROR (Status);\r
489\r
490 Status = PeiServicesInstallPpi (mPpiBootMode);\r
491 ASSERT_EFI_ERROR (Status);\r
9ed65b10 492}\r
493\r
494\r
77ba993c 495VOID\r
496ReserveEmuVariableNvStore (\r
497 )\r
498{\r
499 EFI_PHYSICAL_ADDRESS VariableStore;\r
32e083c7 500 RETURN_STATUS PcdStatus;\r
77ba993c 501\r
502 //\r
503 // Allocate storage for NV variables early on so it will be\r
504 // at a consistent address. Since VM memory is preserved\r
505 // across reboots, this allows the NV variable storage to survive\r
506 // a VM reboot.\r
507 //\r
508 VariableStore =\r
509 (EFI_PHYSICAL_ADDRESS)(UINTN)\r
c9e7907d
LE
510 AllocateRuntimePages (\r
511 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
27f58ea1 512 );\r
77ba993c 513 DEBUG ((EFI_D_INFO,\r
c9e7907d 514 "Reserved variable store memory: 0x%lX; size: %dkb\n",\r
77ba993c 515 VariableStore,\r
c9e7907d 516 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024\r
77ba993c 517 ));\r
32e083c7
LE
518 PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);\r
519 ASSERT_RETURN_ERROR (PcdStatus);\r
77ba993c 520}\r
521\r
522\r
3ca15914 523VOID\r
524DebugDumpCmos (\r
525 VOID\r
526 )\r
527{\r
6394c35a 528 UINT32 Loop;\r
3ca15914 529\r
530 DEBUG ((EFI_D_INFO, "CMOS:\n"));\r
531\r
532 for (Loop = 0; Loop < 0x80; Loop++) {\r
533 if ((Loop % 0x10) == 0) {\r
534 DEBUG ((EFI_D_INFO, "%02x:", Loop));\r
535 }\r
536 DEBUG ((EFI_D_INFO, " %02x", CmosRead8 (Loop)));\r
537 if ((Loop % 0x10) == 0xf) {\r
538 DEBUG ((EFI_D_INFO, "\n"));\r
539 }\r
540 }\r
541}\r
542\r
543\r
5133d1f1
LE
544VOID\r
545S3Verification (\r
546 VOID\r
547 )\r
548{\r
549#if defined (MDE_CPU_X64)\r
550 if (FeaturePcdGet (PcdSmmSmramRequire) && mS3Supported) {\r
551 DEBUG ((EFI_D_ERROR,\r
552 "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n", __FUNCTION__));\r
553 DEBUG ((EFI_D_ERROR,\r
554 "%a: Please disable S3 on the QEMU command line (see the README),\n",\r
555 __FUNCTION__));\r
556 DEBUG ((EFI_D_ERROR,\r
557 "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n", __FUNCTION__));\r
558 ASSERT (FALSE);\r
559 CpuDeadLoop ();\r
560 }\r
561#endif\r
562}\r
563\r
564\r
45a70db3
LE
565/**\r
566 Fetch the number of boot CPUs from QEMU and expose it to UefiCpuPkg modules.\r
567 Set the mMaxCpuCount variable.\r
568**/\r
569VOID\r
570MaxCpuCountInitialization (\r
571 VOID\r
572 )\r
573{\r
574 UINT16 ProcessorCount;\r
575 RETURN_STATUS PcdStatus;\r
576\r
577 QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
578 ProcessorCount = QemuFwCfgRead16 ();\r
579 //\r
580 // If the fw_cfg key or fw_cfg entirely is unavailable, load mMaxCpuCount\r
581 // from the PCD default. No change to PCDs.\r
582 //\r
583 if (ProcessorCount == 0) {\r
584 mMaxCpuCount = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
585 return;\r
586 }\r
587 //\r
588 // Otherwise, set mMaxCpuCount to the value reported by QEMU.\r
589 //\r
590 mMaxCpuCount = ProcessorCount;\r
591 //\r
592 // Additionally, tell UefiCpuPkg modules (a) the exact number of VCPUs, (b)\r
593 // to wait, in the initial AP bringup, exactly as long as it takes for all of\r
594 // the APs to report in. For this, we set the longest representable timeout\r
595 // (approx. 71 minutes).\r
596 //\r
597 PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, ProcessorCount);\r
598 ASSERT_RETURN_ERROR (PcdStatus);\r
599 PcdStatus = PcdSet32S (PcdCpuApInitTimeOutInMicroSeconds, MAX_UINT32);\r
600 ASSERT_RETURN_ERROR (PcdStatus);\r
601 DEBUG ((DEBUG_INFO, "%a: QEMU reports %d processor(s)\n", __FUNCTION__,\r
602 ProcessorCount));\r
603}\r
604\r
605\r
49ba9447 606/**\r
607 Perform Platform PEI initialization.\r
608\r
609 @param FileHandle Handle of the file being invoked.\r
610 @param PeiServices Describes the list of possible PEI Services.\r
611\r
612 @return EFI_SUCCESS The PEIM initialized successfully.\r
613\r
614**/\r
615EFI_STATUS\r
616EFIAPI\r
617InitializePlatform (\r
618 IN EFI_PEI_FILE_HANDLE FileHandle,\r
619 IN CONST EFI_PEI_SERVICES **PeiServices\r
620 )\r
621{\r
a1726e30
SZ
622 EFI_STATUS Status;\r
623\r
7707c9fd 624 DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));\r
49ba9447 625\r
3ca15914 626 DebugDumpCmos ();\r
627\r
b98b4941 628 XenDetect ();\r
c7ea55b9 629\r
7cdba634
JJ
630 if (QemuFwCfgS3Enabled ()) {\r
631 DEBUG ((EFI_D_INFO, "S3 support was detected on QEMU\n"));\r
632 mS3Supported = TRUE;\r
a1726e30
SZ
633 Status = PcdSetBoolS (PcdAcpiS3Enable, TRUE);\r
634 ASSERT_EFI_ERROR (Status);\r
7cdba634
JJ
635 }\r
636\r
5133d1f1 637 S3Verification ();\r
869b17cc 638 BootModeInitialization ();\r
bc89fe48 639 AddressWidthInitialization ();\r
45a70db3 640 MaxCpuCountInitialization ();\r
869b17cc 641\r
d5e06444
LE
642 //\r
643 // Query Host Bridge DID\r
644 //\r
645 mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
646\r
23bfb5c0
LE
647 if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
648 Q35TsegMbytesInitialization ();\r
649 }\r
650\r
f76e9eba
JJ
651 PublishPeiMemory ();\r
652\r
2818c158 653 InitializeRamRegions ();\r
49ba9447 654\r
b621bb0a 655 if (mXen) {\r
c7ea55b9 656 DEBUG ((EFI_D_INFO, "Xen was detected\n"));\r
b98b4941 657 InitializeXen ();\r
c7ea55b9 658 }\r
eec7d420 659\r
bd386eaf 660 if (mBootMode != BOOT_ON_S3_RESUME) {\r
5e167d7e
LE
661 if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
662 ReserveEmuVariableNvStore ();\r
663 }\r
bd386eaf 664 PeiFvInitialization ();\r
bd386eaf 665 MemMapInitialization ();\r
ab081a50 666 NoexecDxeInitialization ();\r
bd386eaf 667 }\r
49ba9447 668\r
d20ae95a 669 InstallClearCacheCallback ();\r
13b5d743 670 AmdSevInitialize ();\r
0e20a186 671 MiscInitialization ();\r
dbab9949 672 InstallFeatureControlCallback ();\r
49ba9447 673\r
674 return EFI_SUCCESS;\r
675}\r