]>
Commit | Line | Data |
---|---|---|
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 |
b3c1bc1c | 30 | #include <Library/QemuFwCfgSimpleParserLib.h>\r |
49ba9447 | 31 | #include <Library/ResourcePublicationLib.h>\r |
9ed65b10 | 32 | #include <Ppi/MasterBootMode.h>\r |
83357313 | 33 | #include <IndustryStandard/I440FxPiix4.h>\r |
931a0c74 | 34 | #include <IndustryStandard/Pci22.h>\r |
83357313 LE |
35 | #include <IndustryStandard/Q35MchIch9.h>\r |
36 | #include <IndustryStandard/QemuCpuHotplug.h>\r | |
97380beb | 37 | #include <OvmfPlatforms.h>\r |
49ba9447 | 38 | \r |
39 | #include "Platform.h"\r | |
3ca15914 | 40 | #include "Cmos.h"\r |
49ba9447 | 41 | \r |
9ed65b10 | 42 | EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {\r |
43 | {\r | |
44 | EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r | |
45 | &gEfiPeiMasterBootModePpiGuid,\r | |
46 | NULL\r | |
47 | }\r | |
48 | };\r | |
49 | \r | |
50 | \r | |
589756c7 PA |
51 | UINT16 mHostBridgeDevId;\r |
52 | \r | |
979420df JJ |
53 | EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;\r |
54 | \r | |
7cdba634 JJ |
55 | BOOLEAN mS3Supported = FALSE;\r |
56 | \r | |
45a70db3 | 57 | UINT32 mMaxCpuCount;\r |
979420df | 58 | \r |
49ba9447 | 59 | VOID\r |
60 | AddIoMemoryBaseSizeHob (\r | |
61 | EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
62 | UINT64 MemorySize\r | |
63 | )\r | |
64 | {\r | |
991d9563 | 65 | BuildResourceDescriptorHob (\r |
66 | EFI_RESOURCE_MEMORY_MAPPED_IO,\r | |
49ba9447 | 67 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r |
68 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
69 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
991d9563 | 70 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r |
49ba9447 | 71 | MemoryBase,\r |
72 | MemorySize\r | |
73 | );\r | |
74 | }\r | |
75 | \r | |
eec7d420 | 76 | VOID\r |
77 | AddReservedMemoryBaseSizeHob (\r | |
78 | EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
cdef34ec LE |
79 | UINT64 MemorySize,\r |
80 | BOOLEAN Cacheable\r | |
eec7d420 | 81 | )\r |
82 | {\r | |
83 | BuildResourceDescriptorHob (\r | |
84 | EFI_RESOURCE_MEMORY_RESERVED,\r | |
85 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
86 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
87 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
cdef34ec LE |
88 | (Cacheable ?\r |
89 | EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r | |
90 | EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r | |
91 | EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :\r | |
92 | 0\r | |
93 | ) |\r | |
eec7d420 | 94 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r |
95 | MemoryBase,\r | |
96 | MemorySize\r | |
97 | );\r | |
98 | }\r | |
49ba9447 | 99 | \r |
100 | VOID\r | |
101 | AddIoMemoryRangeHob (\r | |
102 | EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
103 | EFI_PHYSICAL_ADDRESS MemoryLimit\r | |
104 | )\r | |
105 | {\r | |
106 | AddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r | |
107 | }\r | |
108 | \r | |
109 | \r | |
110 | VOID\r | |
111 | AddMemoryBaseSizeHob (\r | |
112 | EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
113 | UINT64 MemorySize\r | |
114 | )\r | |
115 | {\r | |
991d9563 | 116 | BuildResourceDescriptorHob (\r |
117 | EFI_RESOURCE_SYSTEM_MEMORY,\r | |
49ba9447 | 118 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r |
119 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
120 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
121 | EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r | |
122 | EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r | |
123 | EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r | |
991d9563 | 124 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r |
49ba9447 | 125 | MemoryBase,\r |
126 | MemorySize\r | |
127 | );\r | |
128 | }\r | |
129 | \r | |
130 | \r | |
131 | VOID\r | |
132 | AddMemoryRangeHob (\r | |
133 | EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
134 | EFI_PHYSICAL_ADDRESS MemoryLimit\r | |
135 | )\r | |
136 | {\r | |
137 | AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r | |
138 | }\r | |
139 | \r | |
c0e10976 | 140 | \r |
bb6a9a93 | 141 | VOID\r |
4b455f7b | 142 | MemMapInitialization (\r |
bb6a9a93 WL |
143 | VOID\r |
144 | )\r | |
145 | {\r | |
32e083c7 LE |
146 | UINT64 PciIoBase;\r |
147 | UINT64 PciIoSize;\r | |
148 | RETURN_STATUS PcdStatus;\r | |
d06eb2d1 LE |
149 | UINT32 TopOfLowRam;\r |
150 | UINT64 PciExBarBase;\r | |
151 | UINT32 PciBase;\r | |
152 | UINT32 PciSize;\r | |
c4df7fd0 LE |
153 | \r |
154 | PciIoBase = 0xC000;\r | |
155 | PciIoSize = 0x4000;\r | |
156 | \r | |
bb6a9a93 WL |
157 | //\r |
158 | // Video memory + Legacy BIOS region\r | |
159 | //\r | |
160 | AddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r | |
161 | \r | |
8583b57c GH |
162 | if (mHostBridgeDevId == 0xffff /* microvm */) {\r |
163 | AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB); /* ioapic #1 */\r | |
164 | AddIoMemoryBaseSizeHob (0xFEC10000, SIZE_4KB); /* ioapic #2 */\r | |
165 | return;\r | |
166 | }\r | |
167 | \r | |
d06eb2d1 LE |
168 | TopOfLowRam = GetSystemMemorySizeBelow4gb ();\r |
169 | PciExBarBase = 0;\r | |
170 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
171 | //\r | |
172 | // The MMCONFIG area is expected to fall between the top of low RAM and\r | |
173 | // the base of the 32-bit PCI host aperture.\r | |
174 | //\r | |
175 | PciExBarBase = FixedPcdGet64 (PcdPciExpressBaseAddress);\r | |
176 | ASSERT (TopOfLowRam <= PciExBarBase);\r | |
177 | ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r | |
178 | PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r | |
179 | } else {\r | |
180 | ASSERT (TopOfLowRam <= mQemuUc32Base);\r | |
181 | PciBase = mQemuUc32Base;\r | |
182 | }\r | |
c68d3a69 | 183 | \r |
d06eb2d1 LE |
184 | //\r |
185 | // address purpose size\r | |
186 | // ------------ -------- -------------------------\r | |
187 | // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r | |
188 | // 0xFC000000 gap 44 MB\r | |
189 | // 0xFEC00000 IO-APIC 4 KB\r | |
190 | // 0xFEC01000 gap 1020 KB\r | |
191 | // 0xFED00000 HPET 1 KB\r | |
192 | // 0xFED00400 gap 111 KB\r | |
193 | // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r | |
194 | // 0xFED20000 gap 896 KB\r | |
195 | // 0xFEE00000 LAPIC 1 MB\r | |
196 | //\r | |
197 | PciSize = 0xFC000000 - PciBase;\r | |
198 | AddIoMemoryBaseSizeHob (PciBase, PciSize);\r | |
199 | PcdStatus = PcdSet64S (PcdPciMmio32Base, PciBase);\r | |
200 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
201 | PcdStatus = PcdSet64S (PcdPciMmio32Size, PciSize);\r | |
202 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
49ba9447 | 203 | \r |
d06eb2d1 LE |
204 | AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r |
205 | AddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r | |
206 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
207 | AddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r | |
4b455f7b | 208 | //\r |
d06eb2d1 | 209 | // Note: there should be an\r |
bba734ab | 210 | //\r |
d06eb2d1 | 211 | // AddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r |
bba734ab | 212 | //\r |
d06eb2d1 LE |
213 | // call below, just like the one above for RCBA. However, Linux insists\r |
214 | // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r | |
215 | // "reserved memory" -- Linux does not content itself with a simple gap\r | |
216 | // in the memory map wherever the MCFG ACPI table points to.\r | |
217 | //\r | |
218 | // This appears to be a safety measure. The PCI Firmware Specification\r | |
219 | // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r | |
220 | // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r | |
221 | // [...]". (Emphasis added here.)\r | |
222 | //\r | |
223 | // Normally we add memory resource descriptor HOBs in\r | |
224 | // QemuInitializeRam(), and pre-allocate from those with memory\r | |
225 | // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r | |
226 | // is most definitely not RAM; so, as an exception, cover it with\r | |
227 | // uncacheable reserved memory right here.\r | |
228 | //\r | |
229 | AddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r | |
230 | BuildMemoryAllocationHob (PciExBarBase, SIZE_256MB,\r | |
231 | EfiReservedMemoryType);\r | |
232 | }\r | |
233 | AddIoMemoryBaseSizeHob (PcdGet32(PcdCpuLocalApicBaseAddress), SIZE_1MB);\r | |
234 | \r | |
235 | //\r | |
236 | // On Q35, the IO Port space is available for PCI resource allocations from\r | |
237 | // 0x6000 up.\r | |
238 | //\r | |
239 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
240 | PciIoBase = 0x6000;\r | |
241 | PciIoSize = 0xA000;\r | |
242 | ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r | |
4b455f7b | 243 | }\r |
c4df7fd0 LE |
244 | \r |
245 | //\r | |
246 | // Add PCI IO Port space available for PCI resource allocations.\r | |
247 | //\r | |
248 | BuildResourceDescriptorHob (\r | |
249 | EFI_RESOURCE_IO,\r | |
250 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
251 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED,\r | |
252 | PciIoBase,\r | |
253 | PciIoSize\r | |
254 | );\r | |
32e083c7 LE |
255 | PcdStatus = PcdSet64S (PcdPciIoBase, PciIoBase);\r |
256 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
257 | PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);\r | |
258 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
49ba9447 | 259 | }\r |
260 | \r | |
ab081a50 LE |
261 | #define UPDATE_BOOLEAN_PCD_FROM_FW_CFG(TokenName) \\r |
262 | do { \\r | |
32e083c7 LE |
263 | BOOLEAN Setting; \\r |
264 | RETURN_STATUS PcdStatus; \\r | |
ab081a50 | 265 | \\r |
b3c1bc1c | 266 | if (!RETURN_ERROR (QemuFwCfgParseBool ( \\r |
ab081a50 | 267 | "opt/ovmf/" #TokenName, &Setting))) { \\r |
32e083c7 LE |
268 | PcdStatus = PcdSetBoolS (TokenName, Setting); \\r |
269 | ASSERT_RETURN_ERROR (PcdStatus); \\r | |
ab081a50 LE |
270 | } \\r |
271 | } while (0)\r | |
272 | \r | |
273 | VOID\r | |
274 | NoexecDxeInitialization (\r | |
275 | VOID\r | |
276 | )\r | |
277 | {\r | |
ab081a50 LE |
278 | UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdSetNxForStack);\r |
279 | }\r | |
49ba9447 | 280 | \r |
7b8fe635 LE |
281 | VOID\r |
282 | PciExBarInitialization (\r | |
283 | VOID\r | |
284 | )\r | |
285 | {\r | |
286 | union {\r | |
287 | UINT64 Uint64;\r | |
288 | UINT32 Uint32[2];\r | |
289 | } PciExBarBase;\r | |
290 | \r | |
291 | //\r | |
292 | // We only support the 256MB size for the MMCONFIG area:\r | |
293 | // 256 buses * 32 devices * 8 functions * 4096 bytes config space.\r | |
294 | //\r | |
295 | // The masks used below enforce the Q35 requirements that the MMCONFIG area\r | |
296 | // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.\r | |
297 | //\r | |
298 | // Note that (b) also ensures that the minimum address width we have\r | |
299 | // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice\r | |
300 | // for DXE's page tables to cover the MMCONFIG area.\r | |
301 | //\r | |
302 | PciExBarBase.Uint64 = FixedPcdGet64 (PcdPciExpressBaseAddress);\r | |
303 | ASSERT ((PciExBarBase.Uint32[1] & MCH_PCIEXBAR_HIGHMASK) == 0);\r | |
304 | ASSERT ((PciExBarBase.Uint32[0] & MCH_PCIEXBAR_LOWMASK) == 0);\r | |
305 | \r | |
306 | //\r | |
307 | // Clear the PCIEXBAREN bit first, before programming the high register.\r | |
308 | //\r | |
309 | PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0);\r | |
310 | \r | |
311 | //\r | |
312 | // Program the high register. Then program the low register, setting the\r | |
313 | // MMCONFIG area size and enabling decoding at once.\r | |
314 | //\r | |
315 | PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[1]);\r | |
316 | PciWrite32 (\r | |
317 | DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW),\r | |
318 | PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN\r | |
319 | );\r | |
320 | }\r | |
321 | \r | |
49ba9447 | 322 | VOID\r |
323 | MiscInitialization (\r | |
0e20a186 | 324 | VOID\r |
49ba9447 | 325 | )\r |
326 | {\r | |
32e083c7 LE |
327 | UINTN PmCmd;\r |
328 | UINTN Pmba;\r | |
329 | UINT32 PmbaAndVal;\r | |
330 | UINT32 PmbaOrVal;\r | |
331 | UINTN AcpiCtlReg;\r | |
332 | UINT8 AcpiEnBit;\r | |
333 | RETURN_STATUS PcdStatus;\r | |
97380beb | 334 | \r |
49ba9447 | 335 | //\r |
336 | // Disable A20 Mask\r | |
337 | //\r | |
55cdb67a | 338 | IoOr8 (0x92, BIT1);\r |
49ba9447 | 339 | \r |
340 | //\r | |
86a14b0a LE |
341 | // Build the CPU HOB with guest RAM size dependent address width and 16-bits\r |
342 | // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r | |
343 | // S3 resume as well, so we build it unconditionally.)\r | |
49ba9447 | 344 | //\r |
86a14b0a | 345 | BuildCpuHob (mPhysMemAddressWidth, 16);\r |
c756b2ab | 346 | \r |
97380beb | 347 | //\r |
589756c7 | 348 | // Determine platform type and save Host Bridge DID to PCD\r |
97380beb | 349 | //\r |
589756c7 | 350 | switch (mHostBridgeDevId) {\r |
97380beb | 351 | case INTEL_82441_DEVICE_ID:\r |
e2ab3f81 | 352 | PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);\r |
da372167 | 353 | Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r |
1466b76f LE |
354 | PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;\r |
355 | PmbaOrVal = PIIX4_PMBA_VALUE;\r | |
da372167 LE |
356 | AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);\r |
357 | AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;\r | |
97380beb GS |
358 | break;\r |
359 | case INTEL_Q35_MCH_DEVICE_ID:\r | |
e2ab3f81 | 360 | PmCmd = POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET);\r |
bc9d05d6 | 361 | Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r |
1466b76f LE |
362 | PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;\r |
363 | PmbaOrVal = ICH9_PMBASE_VALUE;\r | |
bc9d05d6 LE |
364 | AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r |
365 | AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r | |
97380beb GS |
366 | break;\r |
367 | default:\r | |
70d5086c | 368 | DEBUG ((DEBUG_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r |
589756c7 | 369 | __FUNCTION__, mHostBridgeDevId));\r |
97380beb GS |
370 | ASSERT (FALSE);\r |
371 | return;\r | |
372 | }\r | |
32e083c7 LE |
373 | PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r |
374 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
97380beb | 375 | \r |
0e20a186 | 376 | //\r |
d06eb2d1 LE |
377 | // If the appropriate IOspace enable bit is set, assume the ACPI PMBA has\r |
378 | // been configured and skip the setup here. This matches the logic in\r | |
379 | // AcpiTimerLibConstructor ().\r | |
0e20a186 | 380 | //\r |
e2ab3f81 | 381 | if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r |
eec7d420 | 382 | //\r |
e2ab3f81 | 383 | // The PEI phase should be exited with fully accessibe ACPI PM IO space:\r |
931a0c74 | 384 | // 1. set PMBA\r |
eec7d420 | 385 | //\r |
1466b76f | 386 | PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);\r |
931a0c74 | 387 | \r |
388 | //\r | |
389 | // 2. set PCICMD/IOSE\r | |
390 | //\r | |
97380beb | 391 | PciOr8 (PmCmd, EFI_PCI_COMMAND_IO_SPACE);\r |
931a0c74 | 392 | \r |
393 | //\r | |
e2ab3f81 | 394 | // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)\r |
931a0c74 | 395 | //\r |
e2ab3f81 | 396 | PciOr8 (AcpiCtlReg, AcpiEnBit);\r |
eec7d420 | 397 | }\r |
90721ba5 PA |
398 | \r |
399 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
400 | //\r | |
401 | // Set Root Complex Register Block BAR\r | |
402 | //\r | |
403 | PciWrite32 (\r | |
404 | POWER_MGMT_REGISTER_Q35 (ICH9_RCBA),\r | |
405 | ICH9_ROOT_COMPLEX_BASE | ICH9_RCBA_EN\r | |
406 | );\r | |
7b8fe635 LE |
407 | \r |
408 | //\r | |
409 | // Set PCI Express Register Range Base Address\r | |
410 | //\r | |
411 | PciExBarInitialization ();\r | |
90721ba5 | 412 | }\r |
49ba9447 | 413 | }\r |
414 | \r | |
415 | \r | |
9ed65b10 | 416 | VOID\r |
417 | BootModeInitialization (\r | |
8f5ca05b | 418 | VOID\r |
9ed65b10 | 419 | )\r |
420 | {\r | |
8f5ca05b LE |
421 | EFI_STATUS Status;\r |
422 | \r | |
423 | if (CmosRead8 (0xF) == 0xFE) {\r | |
979420df | 424 | mBootMode = BOOT_ON_S3_RESUME;\r |
8f5ca05b | 425 | }\r |
9be75189 | 426 | CmosWrite8 (0xF, 0x00);\r |
667bf1e4 | 427 | \r |
979420df | 428 | Status = PeiServicesSetBootMode (mBootMode);\r |
667bf1e4 | 429 | ASSERT_EFI_ERROR (Status);\r |
430 | \r | |
431 | Status = PeiServicesInstallPpi (mPpiBootMode);\r | |
432 | ASSERT_EFI_ERROR (Status);\r | |
9ed65b10 | 433 | }\r |
434 | \r | |
435 | \r | |
77ba993c | 436 | VOID\r |
437 | ReserveEmuVariableNvStore (\r | |
438 | )\r | |
439 | {\r | |
440 | EFI_PHYSICAL_ADDRESS VariableStore;\r | |
32e083c7 | 441 | RETURN_STATUS PcdStatus;\r |
77ba993c | 442 | \r |
443 | //\r | |
444 | // Allocate storage for NV variables early on so it will be\r | |
445 | // at a consistent address. Since VM memory is preserved\r | |
446 | // across reboots, this allows the NV variable storage to survive\r | |
447 | // a VM reboot.\r | |
448 | //\r | |
449 | VariableStore =\r | |
450 | (EFI_PHYSICAL_ADDRESS)(UINTN)\r | |
c9e7907d LE |
451 | AllocateRuntimePages (\r |
452 | EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))\r | |
27f58ea1 | 453 | );\r |
70d5086c | 454 | DEBUG ((DEBUG_INFO,\r |
c9e7907d | 455 | "Reserved variable store memory: 0x%lX; size: %dkb\n",\r |
77ba993c | 456 | VariableStore,\r |
c9e7907d | 457 | (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024\r |
77ba993c | 458 | ));\r |
32e083c7 LE |
459 | PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);\r |
460 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
77ba993c | 461 | }\r |
462 | \r | |
463 | \r | |
3ca15914 | 464 | VOID\r |
465 | DebugDumpCmos (\r | |
466 | VOID\r | |
467 | )\r | |
468 | {\r | |
6394c35a | 469 | UINT32 Loop;\r |
3ca15914 | 470 | \r |
70d5086c | 471 | DEBUG ((DEBUG_INFO, "CMOS:\n"));\r |
3ca15914 | 472 | \r |
473 | for (Loop = 0; Loop < 0x80; Loop++) {\r | |
474 | if ((Loop % 0x10) == 0) {\r | |
70d5086c | 475 | DEBUG ((DEBUG_INFO, "%02x:", Loop));\r |
3ca15914 | 476 | }\r |
70d5086c | 477 | DEBUG ((DEBUG_INFO, " %02x", CmosRead8 (Loop)));\r |
3ca15914 | 478 | if ((Loop % 0x10) == 0xf) {\r |
70d5086c | 479 | DEBUG ((DEBUG_INFO, "\n"));\r |
3ca15914 | 480 | }\r |
481 | }\r | |
482 | }\r | |
483 | \r | |
484 | \r | |
5133d1f1 LE |
485 | VOID\r |
486 | S3Verification (\r | |
487 | VOID\r | |
488 | )\r | |
489 | {\r | |
490 | #if defined (MDE_CPU_X64)\r | |
491 | if (FeaturePcdGet (PcdSmmSmramRequire) && mS3Supported) {\r | |
70d5086c | 492 | DEBUG ((DEBUG_ERROR,\r |
5133d1f1 | 493 | "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n", __FUNCTION__));\r |
70d5086c | 494 | DEBUG ((DEBUG_ERROR,\r |
5133d1f1 LE |
495 | "%a: Please disable S3 on the QEMU command line (see the README),\n",\r |
496 | __FUNCTION__));\r | |
70d5086c | 497 | DEBUG ((DEBUG_ERROR,\r |
5133d1f1 LE |
498 | "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n", __FUNCTION__));\r |
499 | ASSERT (FALSE);\r | |
500 | CpuDeadLoop ();\r | |
501 | }\r | |
502 | #endif\r | |
503 | }\r | |
504 | \r | |
505 | \r | |
e0ed7a9b LE |
506 | VOID\r |
507 | Q35BoardVerification (\r | |
508 | VOID\r | |
509 | )\r | |
510 | {\r | |
511 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
512 | return;\r | |
513 | }\r | |
514 | \r | |
515 | DEBUG ((\r | |
516 | DEBUG_ERROR,\r | |
517 | "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "\r | |
518 | "only DID=0x%04x (Q35) is supported\n",\r | |
519 | __FUNCTION__,\r | |
520 | mHostBridgeDevId,\r | |
521 | INTEL_Q35_MCH_DEVICE_ID\r | |
522 | ));\r | |
523 | ASSERT (FALSE);\r | |
524 | CpuDeadLoop ();\r | |
525 | }\r | |
526 | \r | |
527 | \r | |
45a70db3 | 528 | /**\r |
83357313 LE |
529 | Fetch the boot CPU count and the possible CPU count from QEMU, and expose\r |
530 | them to UefiCpuPkg modules. Set the mMaxCpuCount variable.\r | |
45a70db3 LE |
531 | **/\r |
532 | VOID\r | |
533 | MaxCpuCountInitialization (\r | |
534 | VOID\r | |
535 | )\r | |
536 | {\r | |
83357313 | 537 | UINT16 BootCpuCount;\r |
45a70db3 LE |
538 | RETURN_STATUS PcdStatus;\r |
539 | \r | |
45a70db3 | 540 | //\r |
83357313 | 541 | // Try to fetch the boot CPU count.\r |
45a70db3 | 542 | //\r |
83357313 LE |
543 | QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r |
544 | BootCpuCount = QemuFwCfgRead16 ();\r | |
545 | if (BootCpuCount == 0) {\r | |
546 | //\r | |
547 | // QEMU doesn't report the boot CPU count. (BootCpuCount == 0) will let\r | |
548 | // MpInitLib count APs up to (PcdCpuMaxLogicalProcessorNumber - 1), or\r | |
549 | // until PcdCpuApInitTimeOutInMicroSeconds elapses (whichever is reached\r | |
550 | // first).\r | |
551 | //\r | |
552 | DEBUG ((DEBUG_WARN, "%a: boot CPU count unavailable\n", __FUNCTION__));\r | |
45a70db3 | 553 | mMaxCpuCount = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r |
83357313 LE |
554 | } else {\r |
555 | //\r | |
556 | // We will expose BootCpuCount to MpInitLib. MpInitLib will count APs up to\r | |
557 | // (BootCpuCount - 1) precisely, regardless of timeout.\r | |
558 | //\r | |
559 | // Now try to fetch the possible CPU count.\r | |
560 | //\r | |
561 | UINTN CpuHpBase;\r | |
562 | UINT32 CmdData2;\r | |
563 | \r | |
564 | CpuHpBase = ((mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) ?\r | |
565 | ICH9_CPU_HOTPLUG_BASE : PIIX4_CPU_HOTPLUG_BASE);\r | |
566 | \r | |
567 | //\r | |
568 | // If only legacy mode is available in the CPU hotplug register block, or\r | |
569 | // the register block is completely missing, then the writes below are\r | |
570 | // no-ops.\r | |
571 | //\r | |
572 | // 1. Switch the hotplug register block to modern mode.\r | |
573 | //\r | |
574 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r | |
575 | //\r | |
576 | // 2. Select a valid CPU for deterministic reading of\r | |
577 | // QEMU_CPUHP_R_CMD_DATA2.\r | |
578 | //\r | |
579 | // CPU#0 is always valid; it is the always present and non-removable\r | |
580 | // BSP.\r | |
581 | //\r | |
582 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r | |
583 | //\r | |
584 | // 3. Send a command after which QEMU_CPUHP_R_CMD_DATA2 is specified to\r | |
585 | // read as zero, and which does not invalidate the selector. (The\r | |
586 | // selector may change, but it must not become invalid.)\r | |
587 | //\r | |
588 | // Send QEMU_CPUHP_CMD_GET_PENDING, as it will prove useful later.\r | |
589 | //\r | |
590 | IoWrite8 (CpuHpBase + QEMU_CPUHP_W_CMD, QEMU_CPUHP_CMD_GET_PENDING);\r | |
591 | //\r | |
592 | // 4. Read QEMU_CPUHP_R_CMD_DATA2.\r | |
593 | //\r | |
594 | // If the register block is entirely missing, then this is an unassigned\r | |
595 | // IO read, returning all-bits-one.\r | |
596 | //\r | |
597 | // If only legacy mode is available, then bit#0 stands for CPU#0 in the\r | |
598 | // "CPU present bitmap". CPU#0 is always present.\r | |
599 | //\r | |
600 | // Otherwise, QEMU_CPUHP_R_CMD_DATA2 is either still reserved (returning\r | |
601 | // all-bits-zero), or it is specified to read as zero after the above\r | |
602 | // steps. Both cases confirm modern mode.\r | |
603 | //\r | |
604 | CmdData2 = IoRead32 (CpuHpBase + QEMU_CPUHP_R_CMD_DATA2);\r | |
605 | DEBUG ((DEBUG_VERBOSE, "%a: CmdData2=0x%x\n", __FUNCTION__, CmdData2));\r | |
606 | if (CmdData2 != 0) {\r | |
607 | //\r | |
608 | // QEMU doesn't support the modern CPU hotplug interface. Assume that the\r | |
609 | // possible CPU count equals the boot CPU count (precluding hotplug).\r | |
610 | //\r | |
611 | DEBUG ((DEBUG_WARN, "%a: modern CPU hotplug interface unavailable\n",\r | |
612 | __FUNCTION__));\r | |
613 | mMaxCpuCount = BootCpuCount;\r | |
614 | } else {\r | |
615 | //\r | |
616 | // Grab the possible CPU count from the modern CPU hotplug interface.\r | |
617 | //\r | |
618 | UINT32 Present, Possible, Selected;\r | |
619 | \r | |
620 | Present = 0;\r | |
621 | Possible = 0;\r | |
622 | \r | |
623 | //\r | |
624 | // We've sent QEMU_CPUHP_CMD_GET_PENDING last; this ensures\r | |
625 | // QEMU_CPUHP_RW_CMD_DATA can now be read usefully. However,\r | |
626 | // QEMU_CPUHP_CMD_GET_PENDING may have selected a CPU with actual pending\r | |
627 | // hotplug events; therefore, select CPU#0 forcibly.\r | |
628 | //\r | |
629 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r | |
630 | \r | |
631 | do {\r | |
632 | UINT8 CpuStatus;\r | |
633 | \r | |
634 | //\r | |
635 | // Read the status of the currently selected CPU. This will help with a\r | |
636 | // sanity check against "BootCpuCount".\r | |
637 | //\r | |
638 | CpuStatus = IoRead8 (CpuHpBase + QEMU_CPUHP_R_CPU_STAT);\r | |
639 | if ((CpuStatus & QEMU_CPUHP_STAT_ENABLED) != 0) {\r | |
640 | ++Present;\r | |
641 | }\r | |
642 | //\r | |
643 | // Attempt to select the next CPU.\r | |
644 | //\r | |
645 | ++Possible;\r | |
646 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r | |
647 | //\r | |
648 | // If the selection is successful, then the following read will return\r | |
649 | // the selector (which we know is positive at this point). Otherwise,\r | |
650 | // the read will return 0.\r | |
651 | //\r | |
652 | Selected = IoRead32 (CpuHpBase + QEMU_CPUHP_RW_CMD_DATA);\r | |
653 | ASSERT (Selected == Possible || Selected == 0);\r | |
654 | } while (Selected > 0);\r | |
655 | \r | |
656 | //\r | |
657 | // Sanity check: fw_cfg and the modern CPU hotplug interface should\r | |
658 | // return the same boot CPU count.\r | |
659 | //\r | |
660 | if (BootCpuCount != Present) {\r | |
661 | DEBUG ((DEBUG_WARN, "%a: QEMU v2.7 reset bug: BootCpuCount=%d "\r | |
662 | "Present=%u\n", __FUNCTION__, BootCpuCount, Present));\r | |
663 | //\r | |
664 | // The handling of QemuFwCfgItemSmpCpuCount, across CPU hotplug plus\r | |
665 | // platform reset (including S3), was corrected in QEMU commit\r | |
666 | // e3cadac073a9 ("pc: fix FW_CFG_NB_CPUS to account for -device added\r | |
667 | // CPUs", 2016-11-16), part of release v2.8.0.\r | |
668 | //\r | |
669 | BootCpuCount = (UINT16)Present;\r | |
670 | }\r | |
671 | \r | |
672 | mMaxCpuCount = Possible;\r | |
673 | }\r | |
45a70db3 | 674 | }\r |
83357313 LE |
675 | \r |
676 | DEBUG ((DEBUG_INFO, "%a: BootCpuCount=%d mMaxCpuCount=%u\n", __FUNCTION__,\r | |
677 | BootCpuCount, mMaxCpuCount));\r | |
678 | ASSERT (BootCpuCount <= mMaxCpuCount);\r | |
679 | \r | |
680 | PcdStatus = PcdSet32S (PcdCpuBootLogicalProcessorNumber, BootCpuCount);\r | |
45a70db3 | 681 | ASSERT_RETURN_ERROR (PcdStatus);\r |
83357313 | 682 | PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, mMaxCpuCount);\r |
45a70db3 | 683 | ASSERT_RETURN_ERROR (PcdStatus);\r |
45a70db3 LE |
684 | }\r |
685 | \r | |
686 | \r | |
49ba9447 | 687 | /**\r |
688 | Perform Platform PEI initialization.\r | |
689 | \r | |
690 | @param FileHandle Handle of the file being invoked.\r | |
691 | @param PeiServices Describes the list of possible PEI Services.\r | |
692 | \r | |
693 | @return EFI_SUCCESS The PEIM initialized successfully.\r | |
694 | \r | |
695 | **/\r | |
696 | EFI_STATUS\r | |
697 | EFIAPI\r | |
698 | InitializePlatform (\r | |
699 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
700 | IN CONST EFI_PEI_SERVICES **PeiServices\r | |
701 | )\r | |
702 | {\r | |
a1726e30 SZ |
703 | EFI_STATUS Status;\r |
704 | \r | |
7707c9fd | 705 | DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));\r |
49ba9447 | 706 | \r |
3ca15914 | 707 | DebugDumpCmos ();\r |
708 | \r | |
7cdba634 | 709 | if (QemuFwCfgS3Enabled ()) {\r |
70d5086c | 710 | DEBUG ((DEBUG_INFO, "S3 support was detected on QEMU\n"));\r |
7cdba634 | 711 | mS3Supported = TRUE;\r |
a1726e30 SZ |
712 | Status = PcdSetBoolS (PcdAcpiS3Enable, TRUE);\r |
713 | ASSERT_EFI_ERROR (Status);\r | |
7cdba634 JJ |
714 | }\r |
715 | \r | |
5133d1f1 | 716 | S3Verification ();\r |
869b17cc | 717 | BootModeInitialization ();\r |
bc89fe48 | 718 | AddressWidthInitialization ();\r |
869b17cc | 719 | \r |
d5e06444 LE |
720 | //\r |
721 | // Query Host Bridge DID\r | |
722 | //\r | |
723 | mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r | |
724 | \r | |
83357313 LE |
725 | MaxCpuCountInitialization ();\r |
726 | \r | |
23bfb5c0 | 727 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r |
e0ed7a9b | 728 | Q35BoardVerification ();\r |
23bfb5c0 | 729 | Q35TsegMbytesInitialization ();\r |
73974f80 | 730 | Q35SmramAtDefaultSmbaseInitialization ();\r |
23bfb5c0 LE |
731 | }\r |
732 | \r | |
f76e9eba JJ |
733 | PublishPeiMemory ();\r |
734 | \r | |
49edde15 LE |
735 | QemuUc32BaseInitialization ();\r |
736 | \r | |
2818c158 | 737 | InitializeRamRegions ();\r |
49ba9447 | 738 | \r |
bd386eaf | 739 | if (mBootMode != BOOT_ON_S3_RESUME) {\r |
5e167d7e LE |
740 | if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r |
741 | ReserveEmuVariableNvStore ();\r | |
742 | }\r | |
bd386eaf | 743 | PeiFvInitialization ();\r |
d42fdd6f | 744 | MemTypeInfoInitialization ();\r |
bd386eaf | 745 | MemMapInitialization ();\r |
ab081a50 | 746 | NoexecDxeInitialization ();\r |
bd386eaf | 747 | }\r |
49ba9447 | 748 | \r |
d20ae95a | 749 | InstallClearCacheCallback ();\r |
13b5d743 | 750 | AmdSevInitialize ();\r |
0e20a186 | 751 | MiscInitialization ();\r |
dbab9949 | 752 | InstallFeatureControlCallback ();\r |
49ba9447 | 753 | \r |
754 | return EFI_SUCCESS;\r | |
755 | }\r |