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