]>
Commit | Line | Data |
---|---|---|
102cafed MX |
1 | /**@file\r |
2 | \r | |
3 | Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r | |
4 | Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r | |
5 | \r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | //\r | |
11 | // The package level header files this module uses\r | |
12 | //\r | |
13 | #include <PiPei.h>\r | |
14 | \r | |
15 | //\r | |
16 | // The Library classes this module consumes\r | |
17 | //\r | |
18 | #include <Library/BaseMemoryLib.h>\r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/HobLib.h>\r | |
96047b66 MX |
22 | #include <Library/IoLib.h>\r |
23 | #include <IndustryStandard/I440FxPiix4.h>\r | |
24 | #include <IndustryStandard/Microvm.h>\r | |
25 | #include <IndustryStandard/Pci22.h>\r | |
26 | #include <IndustryStandard/Q35MchIch9.h>\r | |
27 | #include <IndustryStandard/QemuCpuHotplug.h>\r | |
4f173db8 | 28 | #include <Library/MemoryAllocationLib.h>\r |
96047b66 MX |
29 | #include <Library/QemuFwCfgLib.h>\r |
30 | #include <Library/QemuFwCfgS3Lib.h>\r | |
31 | #include <Library/QemuFwCfgSimpleParserLib.h>\r | |
32 | #include <Library/PciLib.h>\r | |
4f173db8 MX |
33 | #include <Guid/SystemNvDataGuid.h>\r |
34 | #include <Guid/VariableFormat.h>\r | |
96047b66 MX |
35 | #include <OvmfPlatforms.h>\r |
36 | \r | |
102cafed MX |
37 | #include <Library/PlatformInitLib.h>\r |
38 | \r | |
39 | VOID\r | |
40 | EFIAPI\r | |
41 | PlatformAddIoMemoryBaseSizeHob (\r | |
42 | IN EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
43 | IN UINT64 MemorySize\r | |
44 | )\r | |
45 | {\r | |
46 | BuildResourceDescriptorHob (\r | |
47 | EFI_RESOURCE_MEMORY_MAPPED_IO,\r | |
48 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
49 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
50 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
51 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r | |
52 | MemoryBase,\r | |
53 | MemorySize\r | |
54 | );\r | |
55 | }\r | |
56 | \r | |
57 | VOID\r | |
58 | EFIAPI\r | |
59 | PlatformAddReservedMemoryBaseSizeHob (\r | |
60 | IN EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
61 | IN UINT64 MemorySize,\r | |
62 | IN BOOLEAN Cacheable\r | |
63 | )\r | |
64 | {\r | |
65 | BuildResourceDescriptorHob (\r | |
66 | EFI_RESOURCE_MEMORY_RESERVED,\r | |
67 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
68 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
69 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
70 | (Cacheable ?\r | |
71 | EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r | |
72 | EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r | |
73 | EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :\r | |
74 | 0\r | |
75 | ) |\r | |
76 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r | |
77 | MemoryBase,\r | |
78 | MemorySize\r | |
79 | );\r | |
80 | }\r | |
81 | \r | |
82 | VOID\r | |
83 | EFIAPI\r | |
84 | PlatformAddIoMemoryRangeHob (\r | |
85 | IN EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
86 | IN EFI_PHYSICAL_ADDRESS MemoryLimit\r | |
87 | )\r | |
88 | {\r | |
89 | PlatformAddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r | |
90 | }\r | |
91 | \r | |
92 | VOID\r | |
93 | EFIAPI\r | |
94 | PlatformAddMemoryBaseSizeHob (\r | |
95 | IN EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
96 | IN UINT64 MemorySize\r | |
97 | )\r | |
98 | {\r | |
99 | BuildResourceDescriptorHob (\r | |
100 | EFI_RESOURCE_SYSTEM_MEMORY,\r | |
101 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
102 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r | |
103 | EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r | |
104 | EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r | |
105 | EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r | |
106 | EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r | |
107 | EFI_RESOURCE_ATTRIBUTE_TESTED,\r | |
108 | MemoryBase,\r | |
109 | MemorySize\r | |
110 | );\r | |
111 | }\r | |
112 | \r | |
113 | VOID\r | |
114 | EFIAPI\r | |
115 | PlatformAddMemoryRangeHob (\r | |
116 | IN EFI_PHYSICAL_ADDRESS MemoryBase,\r | |
117 | IN EFI_PHYSICAL_ADDRESS MemoryLimit\r | |
118 | )\r | |
119 | {\r | |
120 | PlatformAddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r | |
121 | }\r | |
96047b66 MX |
122 | \r |
123 | VOID\r | |
124 | EFIAPI\r | |
125 | PlatformMemMapInitialization (\r | |
126 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
127 | )\r | |
128 | {\r | |
129 | UINT64 PciIoBase;\r | |
130 | UINT64 PciIoSize;\r | |
96047b66 MX |
131 | UINT64 PciExBarBase;\r |
132 | UINT32 PciBase;\r | |
133 | UINT32 PciSize;\r | |
134 | \r | |
135 | PciIoBase = 0xC000;\r | |
136 | PciIoSize = 0x4000;\r | |
137 | \r | |
138 | //\r | |
139 | // Video memory + Legacy BIOS region\r | |
140 | //\r | |
e23f8f52 MX |
141 | if (!TdIsEnabled ()) {\r |
142 | PlatformAddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r | |
143 | }\r | |
96047b66 MX |
144 | \r |
145 | if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r | |
146 | PlatformAddIoMemoryBaseSizeHob (MICROVM_GED_MMIO_BASE, SIZE_4KB);\r | |
147 | PlatformAddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB); /* ioapic #1 */\r | |
148 | PlatformAddIoMemoryBaseSizeHob (0xFEC10000, SIZE_4KB); /* ioapic #2 */\r | |
149 | return;\r | |
150 | }\r | |
151 | \r | |
124b7650 | 152 | PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r |
96047b66 MX |
153 | PciExBarBase = 0;\r |
154 | if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
155 | //\r | |
156 | // The MMCONFIG area is expected to fall between the top of low RAM and\r | |
157 | // the base of the 32-bit PCI host aperture.\r | |
158 | //\r | |
47f44097 | 159 | PciExBarBase = PcdGet64 (PcdPciExpressBaseAddress);\r |
124b7650 | 160 | ASSERT (PlatformInfoHob->LowMemory <= PciExBarBase);\r |
96047b66 MX |
161 | ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r |
162 | PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r | |
163 | } else {\r | |
124b7650 | 164 | ASSERT (PlatformInfoHob->LowMemory <= PlatformInfoHob->Uc32Base);\r |
96047b66 MX |
165 | PciBase = PlatformInfoHob->Uc32Base;\r |
166 | }\r | |
167 | \r | |
168 | //\r | |
169 | // address purpose size\r | |
170 | // ------------ -------- -------------------------\r | |
171 | // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r | |
172 | // 0xFC000000 gap 44 MB\r | |
173 | // 0xFEC00000 IO-APIC 4 KB\r | |
174 | // 0xFEC01000 gap 1020 KB\r | |
175 | // 0xFED00000 HPET 1 KB\r | |
176 | // 0xFED00400 gap 111 KB\r | |
177 | // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r | |
178 | // 0xFED20000 gap 896 KB\r | |
179 | // 0xFEE00000 LAPIC 1 MB\r | |
180 | //\r | |
181 | PciSize = 0xFC000000 - PciBase;\r | |
182 | PlatformAddIoMemoryBaseSizeHob (PciBase, PciSize);\r | |
183 | \r | |
184 | PlatformInfoHob->PcdPciMmio32Base = PciBase;\r | |
185 | PlatformInfoHob->PcdPciMmio32Size = PciSize;\r | |
186 | \r | |
187 | PlatformAddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r | |
188 | PlatformAddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r | |
189 | if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
190 | PlatformAddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r | |
191 | //\r | |
192 | // Note: there should be an\r | |
193 | //\r | |
194 | // PlatformAddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r | |
195 | //\r | |
196 | // call below, just like the one above for RCBA. However, Linux insists\r | |
197 | // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r | |
198 | // "reserved memory" -- Linux does not content itself with a simple gap\r | |
199 | // in the memory map wherever the MCFG ACPI table points to.\r | |
200 | //\r | |
201 | // This appears to be a safety measure. The PCI Firmware Specification\r | |
202 | // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r | |
203 | // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r | |
204 | // [...]". (Emphasis added here.)\r | |
205 | //\r | |
206 | // Normally we add memory resource descriptor HOBs in\r | |
207 | // QemuInitializeRam(), and pre-allocate from those with memory\r | |
208 | // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r | |
209 | // is most definitely not RAM; so, as an exception, cover it with\r | |
210 | // uncacheable reserved memory right here.\r | |
211 | //\r | |
212 | PlatformAddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r | |
213 | BuildMemoryAllocationHob (\r | |
214 | PciExBarBase,\r | |
215 | SIZE_256MB,\r | |
216 | EfiReservedMemoryType\r | |
217 | );\r | |
218 | }\r | |
219 | \r | |
220 | PlatformAddIoMemoryBaseSizeHob (PcdGet32 (PcdCpuLocalApicBaseAddress), SIZE_1MB);\r | |
221 | \r | |
222 | //\r | |
223 | // On Q35, the IO Port space is available for PCI resource allocations from\r | |
224 | // 0x6000 up.\r | |
225 | //\r | |
226 | if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
227 | PciIoBase = 0x6000;\r | |
228 | PciIoSize = 0xA000;\r | |
229 | ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r | |
230 | }\r | |
231 | \r | |
232 | //\r | |
233 | // Add PCI IO Port space available for PCI resource allocations.\r | |
234 | //\r | |
235 | BuildResourceDescriptorHob (\r | |
236 | EFI_RESOURCE_IO,\r | |
237 | EFI_RESOURCE_ATTRIBUTE_PRESENT |\r | |
238 | EFI_RESOURCE_ATTRIBUTE_INITIALIZED,\r | |
239 | PciIoBase,\r | |
240 | PciIoSize\r | |
241 | );\r | |
242 | \r | |
243 | PlatformInfoHob->PcdPciIoBase = PciIoBase;\r | |
244 | PlatformInfoHob->PcdPciIoSize = PciIoSize;\r | |
245 | }\r | |
246 | \r | |
247 | /**\r | |
248 | * Fetch "opt/ovmf/PcdSetNxForStack" from QEMU\r | |
249 | *\r | |
250 | * @param Setting The pointer to the setting of "/opt/ovmf/PcdSetNxForStack".\r | |
251 | * @return EFI_SUCCESS Successfully fetch the settings.\r | |
252 | */\r | |
253 | EFI_STATUS\r | |
254 | EFIAPI\r | |
255 | PlatformNoexecDxeInitialization (\r | |
256 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
257 | )\r | |
258 | {\r | |
259 | return QemuFwCfgParseBool ("opt/ovmf/PcdSetNxForStack", &PlatformInfoHob->PcdSetNxForStack);\r | |
260 | }\r | |
261 | \r | |
262 | VOID\r | |
263 | PciExBarInitialization (\r | |
264 | VOID\r | |
265 | )\r | |
266 | {\r | |
267 | union {\r | |
268 | UINT64 Uint64;\r | |
269 | UINT32 Uint32[2];\r | |
270 | } PciExBarBase;\r | |
271 | \r | |
272 | //\r | |
273 | // We only support the 256MB size for the MMCONFIG area:\r | |
274 | // 256 buses * 32 devices * 8 functions * 4096 bytes config space.\r | |
275 | //\r | |
276 | // The masks used below enforce the Q35 requirements that the MMCONFIG area\r | |
277 | // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.\r | |
278 | //\r | |
279 | // Note that (b) also ensures that the minimum address width we have\r | |
280 | // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice\r | |
281 | // for DXE's page tables to cover the MMCONFIG area.\r | |
282 | //\r | |
47f44097 | 283 | PciExBarBase.Uint64 = PcdGet64 (PcdPciExpressBaseAddress);\r |
96047b66 MX |
284 | ASSERT ((PciExBarBase.Uint32[1] & MCH_PCIEXBAR_HIGHMASK) == 0);\r |
285 | ASSERT ((PciExBarBase.Uint32[0] & MCH_PCIEXBAR_LOWMASK) == 0);\r | |
286 | \r | |
287 | //\r | |
288 | // Clear the PCIEXBAREN bit first, before programming the high register.\r | |
289 | //\r | |
290 | PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0);\r | |
291 | \r | |
292 | //\r | |
293 | // Program the high register. Then program the low register, setting the\r | |
294 | // MMCONFIG area size and enabling decoding at once.\r | |
295 | //\r | |
296 | PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[1]);\r | |
297 | PciWrite32 (\r | |
298 | DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW),\r | |
299 | PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN\r | |
300 | );\r | |
301 | }\r | |
302 | \r | |
303 | VOID\r | |
304 | EFIAPI\r | |
305 | PlatformMiscInitialization (\r | |
306 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
307 | )\r | |
308 | {\r | |
309 | UINTN PmCmd;\r | |
310 | UINTN Pmba;\r | |
311 | UINT32 PmbaAndVal;\r | |
312 | UINT32 PmbaOrVal;\r | |
313 | UINTN AcpiCtlReg;\r | |
314 | UINT8 AcpiEnBit;\r | |
315 | \r | |
316 | //\r | |
317 | // Disable A20 Mask\r | |
318 | //\r | |
bf25f27e SB |
319 | if (PlatformInfoHob->HostBridgeDevId != CLOUDHV_DEVICE_ID) {\r |
320 | IoOr8 (0x92, BIT1);\r | |
321 | }\r | |
96047b66 MX |
322 | \r |
323 | //\r | |
324 | // Build the CPU HOB with guest RAM size dependent address width and 16-bits\r | |
325 | // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r | |
326 | // S3 resume as well, so we build it unconditionally.)\r | |
327 | //\r | |
328 | BuildCpuHob (PlatformInfoHob->PhysMemAddressWidth, 16);\r | |
329 | \r | |
330 | //\r | |
331 | // Determine platform type and save Host Bridge DID to PCD\r | |
332 | //\r | |
333 | switch (PlatformInfoHob->HostBridgeDevId) {\r | |
334 | case INTEL_82441_DEVICE_ID:\r | |
335 | PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);\r | |
336 | Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r | |
337 | PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;\r | |
338 | PmbaOrVal = PIIX4_PMBA_VALUE;\r | |
339 | AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);\r | |
340 | AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;\r | |
341 | break;\r | |
342 | case INTEL_Q35_MCH_DEVICE_ID:\r | |
343 | PmCmd = POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET);\r | |
344 | Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r | |
345 | PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;\r | |
346 | PmbaOrVal = ICH9_PMBASE_VALUE;\r | |
347 | AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r | |
348 | AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r | |
349 | break;\r | |
350 | case CLOUDHV_DEVICE_ID:\r | |
351 | break;\r | |
352 | default:\r | |
353 | DEBUG ((\r | |
354 | DEBUG_ERROR,\r | |
355 | "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r | |
356 | __FUNCTION__,\r | |
357 | PlatformInfoHob->HostBridgeDevId\r | |
358 | ));\r | |
359 | ASSERT (FALSE);\r | |
360 | return;\r | |
361 | }\r | |
362 | \r | |
363 | if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {\r | |
364 | DEBUG ((DEBUG_INFO, "%a: Cloud Hypervisor is done.\n", __FUNCTION__));\r | |
365 | return;\r | |
366 | }\r | |
367 | \r | |
368 | //\r | |
369 | // If the appropriate IOspace enable bit is set, assume the ACPI PMBA has\r | |
370 | // been configured and skip the setup here. This matches the logic in\r | |
371 | // AcpiTimerLibConstructor ().\r | |
372 | //\r | |
373 | if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r | |
374 | //\r | |
375 | // The PEI phase should be exited with fully accessibe ACPI PM IO space:\r | |
376 | // 1. set PMBA\r | |
377 | //\r | |
378 | PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);\r | |
379 | \r | |
380 | //\r | |
381 | // 2. set PCICMD/IOSE\r | |
382 | //\r | |
383 | PciOr8 (PmCmd, EFI_PCI_COMMAND_IO_SPACE);\r | |
384 | \r | |
385 | //\r | |
386 | // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)\r | |
387 | //\r | |
388 | PciOr8 (AcpiCtlReg, AcpiEnBit);\r | |
389 | }\r | |
390 | \r | |
391 | if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
392 | //\r | |
393 | // Set Root Complex Register Block BAR\r | |
394 | //\r | |
395 | PciWrite32 (\r | |
396 | POWER_MGMT_REGISTER_Q35 (ICH9_RCBA),\r | |
397 | ICH9_ROOT_COMPLEX_BASE | ICH9_RCBA_EN\r | |
398 | );\r | |
399 | \r | |
400 | //\r | |
401 | // Set PCI Express Register Range Base Address\r | |
402 | //\r | |
403 | PciExBarInitialization ();\r | |
404 | }\r | |
405 | }\r | |
406 | \r | |
407 | /**\r | |
408 | Fetch the boot CPU count and the possible CPU count from QEMU, and expose\r | |
409 | them to UefiCpuPkg modules.\r | |
410 | **/\r | |
411 | VOID\r | |
412 | EFIAPI\r | |
413 | PlatformMaxCpuCountInitialization (\r | |
414 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
415 | )\r | |
416 | {\r | |
43f3cfce | 417 | UINT16 BootCpuCount = 0;\r |
96047b66 MX |
418 | UINT32 MaxCpuCount;\r |
419 | \r | |
420 | //\r | |
421 | // Try to fetch the boot CPU count.\r | |
422 | //\r | |
43f3cfce SB |
423 | if (QemuFwCfgIsAvailable ()) {\r |
424 | QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r | |
425 | BootCpuCount = QemuFwCfgRead16 ();\r | |
426 | }\r | |
427 | \r | |
96047b66 MX |
428 | if (BootCpuCount == 0) {\r |
429 | //\r | |
430 | // QEMU doesn't report the boot CPU count. (BootCpuCount == 0) will let\r | |
431 | // MpInitLib count APs up to (PcdCpuMaxLogicalProcessorNumber - 1), or\r | |
432 | // until PcdCpuApInitTimeOutInMicroSeconds elapses (whichever is reached\r | |
433 | // first).\r | |
434 | //\r | |
435 | DEBUG ((DEBUG_WARN, "%a: boot CPU count unavailable\n", __FUNCTION__));\r | |
436 | MaxCpuCount = PlatformInfoHob->DefaultMaxCpuNumber;\r | |
437 | } else {\r | |
438 | //\r | |
439 | // We will expose BootCpuCount to MpInitLib. MpInitLib will count APs up to\r | |
440 | // (BootCpuCount - 1) precisely, regardless of timeout.\r | |
441 | //\r | |
442 | // Now try to fetch the possible CPU count.\r | |
443 | //\r | |
444 | UINTN CpuHpBase;\r | |
445 | UINT32 CmdData2;\r | |
446 | \r | |
447 | CpuHpBase = ((PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) ?\r | |
448 | ICH9_CPU_HOTPLUG_BASE : PIIX4_CPU_HOTPLUG_BASE);\r | |
449 | \r | |
450 | //\r | |
451 | // If only legacy mode is available in the CPU hotplug register block, or\r | |
452 | // the register block is completely missing, then the writes below are\r | |
453 | // no-ops.\r | |
454 | //\r | |
455 | // 1. Switch the hotplug register block to modern mode.\r | |
456 | //\r | |
457 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r | |
458 | //\r | |
459 | // 2. Select a valid CPU for deterministic reading of\r | |
460 | // QEMU_CPUHP_R_CMD_DATA2.\r | |
461 | //\r | |
462 | // CPU#0 is always valid; it is the always present and non-removable\r | |
463 | // BSP.\r | |
464 | //\r | |
465 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r | |
466 | //\r | |
467 | // 3. Send a command after which QEMU_CPUHP_R_CMD_DATA2 is specified to\r | |
468 | // read as zero, and which does not invalidate the selector. (The\r | |
469 | // selector may change, but it must not become invalid.)\r | |
470 | //\r | |
471 | // Send QEMU_CPUHP_CMD_GET_PENDING, as it will prove useful later.\r | |
472 | //\r | |
473 | IoWrite8 (CpuHpBase + QEMU_CPUHP_W_CMD, QEMU_CPUHP_CMD_GET_PENDING);\r | |
474 | //\r | |
475 | // 4. Read QEMU_CPUHP_R_CMD_DATA2.\r | |
476 | //\r | |
477 | // If the register block is entirely missing, then this is an unassigned\r | |
478 | // IO read, returning all-bits-one.\r | |
479 | //\r | |
480 | // If only legacy mode is available, then bit#0 stands for CPU#0 in the\r | |
481 | // "CPU present bitmap". CPU#0 is always present.\r | |
482 | //\r | |
483 | // Otherwise, QEMU_CPUHP_R_CMD_DATA2 is either still reserved (returning\r | |
484 | // all-bits-zero), or it is specified to read as zero after the above\r | |
485 | // steps. Both cases confirm modern mode.\r | |
486 | //\r | |
487 | CmdData2 = IoRead32 (CpuHpBase + QEMU_CPUHP_R_CMD_DATA2);\r | |
488 | DEBUG ((DEBUG_VERBOSE, "%a: CmdData2=0x%x\n", __FUNCTION__, CmdData2));\r | |
489 | if (CmdData2 != 0) {\r | |
490 | //\r | |
491 | // QEMU doesn't support the modern CPU hotplug interface. Assume that the\r | |
492 | // possible CPU count equals the boot CPU count (precluding hotplug).\r | |
493 | //\r | |
494 | DEBUG ((\r | |
495 | DEBUG_WARN,\r | |
496 | "%a: modern CPU hotplug interface unavailable\n",\r | |
497 | __FUNCTION__\r | |
498 | ));\r | |
499 | MaxCpuCount = BootCpuCount;\r | |
500 | } else {\r | |
501 | //\r | |
502 | // Grab the possible CPU count from the modern CPU hotplug interface.\r | |
503 | //\r | |
504 | UINT32 Present, Possible, Selected;\r | |
505 | \r | |
506 | Present = 0;\r | |
507 | Possible = 0;\r | |
508 | \r | |
509 | //\r | |
510 | // We've sent QEMU_CPUHP_CMD_GET_PENDING last; this ensures\r | |
511 | // QEMU_CPUHP_RW_CMD_DATA can now be read usefully. However,\r | |
512 | // QEMU_CPUHP_CMD_GET_PENDING may have selected a CPU with actual pending\r | |
513 | // hotplug events; therefore, select CPU#0 forcibly.\r | |
514 | //\r | |
515 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r | |
516 | \r | |
517 | do {\r | |
518 | UINT8 CpuStatus;\r | |
519 | \r | |
520 | //\r | |
521 | // Read the status of the currently selected CPU. This will help with a\r | |
522 | // sanity check against "BootCpuCount".\r | |
523 | //\r | |
524 | CpuStatus = IoRead8 (CpuHpBase + QEMU_CPUHP_R_CPU_STAT);\r | |
525 | if ((CpuStatus & QEMU_CPUHP_STAT_ENABLED) != 0) {\r | |
526 | ++Present;\r | |
527 | }\r | |
528 | \r | |
529 | //\r | |
530 | // Attempt to select the next CPU.\r | |
531 | //\r | |
532 | ++Possible;\r | |
533 | IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r | |
534 | //\r | |
535 | // If the selection is successful, then the following read will return\r | |
536 | // the selector (which we know is positive at this point). Otherwise,\r | |
537 | // the read will return 0.\r | |
538 | //\r | |
539 | Selected = IoRead32 (CpuHpBase + QEMU_CPUHP_RW_CMD_DATA);\r | |
540 | ASSERT (Selected == Possible || Selected == 0);\r | |
541 | } while (Selected > 0);\r | |
542 | \r | |
543 | //\r | |
544 | // Sanity check: fw_cfg and the modern CPU hotplug interface should\r | |
545 | // return the same boot CPU count.\r | |
546 | //\r | |
547 | if (BootCpuCount != Present) {\r | |
548 | DEBUG ((\r | |
549 | DEBUG_WARN,\r | |
550 | "%a: QEMU v2.7 reset bug: BootCpuCount=%d "\r | |
551 | "Present=%u\n",\r | |
552 | __FUNCTION__,\r | |
553 | BootCpuCount,\r | |
554 | Present\r | |
555 | ));\r | |
556 | //\r | |
557 | // The handling of QemuFwCfgItemSmpCpuCount, across CPU hotplug plus\r | |
558 | // platform reset (including S3), was corrected in QEMU commit\r | |
559 | // e3cadac073a9 ("pc: fix FW_CFG_NB_CPUS to account for -device added\r | |
560 | // CPUs", 2016-11-16), part of release v2.8.0.\r | |
561 | //\r | |
562 | BootCpuCount = (UINT16)Present;\r | |
563 | }\r | |
564 | \r | |
565 | MaxCpuCount = Possible;\r | |
566 | }\r | |
567 | }\r | |
568 | \r | |
569 | DEBUG ((\r | |
570 | DEBUG_INFO,\r | |
571 | "%a: BootCpuCount=%d MaxCpuCount=%u\n",\r | |
572 | __FUNCTION__,\r | |
573 | BootCpuCount,\r | |
574 | MaxCpuCount\r | |
575 | ));\r | |
576 | ASSERT (BootCpuCount <= MaxCpuCount);\r | |
577 | \r | |
578 | PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber = MaxCpuCount;\r | |
579 | PlatformInfoHob->PcdCpuBootLogicalProcessorNumber = BootCpuCount;\r | |
580 | }\r | |
4f173db8 MX |
581 | \r |
582 | /**\r | |
583 | Check padding data all bit should be 1.\r | |
584 | \r | |
585 | @param[in] Buffer - A pointer to buffer header\r | |
586 | @param[in] BufferSize - Buffer size\r | |
587 | \r | |
588 | @retval TRUE - The padding data is valid.\r | |
589 | @retval TRUE - The padding data is invalid.\r | |
590 | \r | |
591 | **/\r | |
592 | BOOLEAN\r | |
593 | CheckPaddingData (\r | |
594 | IN UINT8 *Buffer,\r | |
595 | IN UINT32 BufferSize\r | |
596 | )\r | |
597 | {\r | |
598 | UINT32 index;\r | |
599 | \r | |
600 | for (index = 0; index < BufferSize; index++) {\r | |
601 | if (Buffer[index] != 0xFF) {\r | |
602 | return FALSE;\r | |
603 | }\r | |
604 | }\r | |
605 | \r | |
606 | return TRUE;\r | |
607 | }\r | |
608 | \r | |
609 | /**\r | |
610 | Check the integrity of NvVarStore.\r | |
611 | \r | |
612 | @param[in] NvVarStoreBase - A pointer to NvVarStore header\r | |
613 | @param[in] NvVarStoreSize - NvVarStore size\r | |
614 | \r | |
615 | @retval TRUE - The NvVarStore is valid.\r | |
616 | @retval FALSE - The NvVarStore is invalid.\r | |
617 | \r | |
618 | **/\r | |
619 | BOOLEAN\r | |
620 | EFIAPI\r | |
621 | PlatformValidateNvVarStore (\r | |
622 | IN UINT8 *NvVarStoreBase,\r | |
623 | IN UINT32 NvVarStoreSize\r | |
624 | )\r | |
625 | {\r | |
626 | UINT16 Checksum;\r | |
627 | UINTN VariableBase;\r | |
628 | UINT32 VariableOffset;\r | |
629 | UINT32 VariableOffsetBeforeAlign;\r | |
630 | EFI_FIRMWARE_VOLUME_HEADER *NvVarStoreFvHeader;\r | |
631 | VARIABLE_STORE_HEADER *NvVarStoreHeader;\r | |
632 | AUTHENTICATED_VARIABLE_HEADER *VariableHeader;\r | |
633 | \r | |
634 | static EFI_GUID FvHdrGUID = EFI_SYSTEM_NV_DATA_FV_GUID;\r | |
635 | static EFI_GUID VarStoreHdrGUID = EFI_AUTHENTICATED_VARIABLE_GUID;\r | |
636 | \r | |
637 | VariableOffset = 0;\r | |
638 | \r | |
639 | if (NvVarStoreBase == NULL) {\r | |
640 | DEBUG ((DEBUG_ERROR, "NvVarStore pointer is NULL.\n"));\r | |
641 | return FALSE;\r | |
642 | }\r | |
643 | \r | |
644 | //\r | |
645 | // Verify the header zerovetor, filesystemguid,\r | |
646 | // revision, signature, attributes, fvlength, checksum\r | |
647 | // HeaderLength cannot be an odd number\r | |
648 | //\r | |
649 | NvVarStoreFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)NvVarStoreBase;\r | |
650 | \r | |
651 | if ((!IsZeroBuffer (NvVarStoreFvHeader->ZeroVector, 16)) ||\r | |
652 | (!CompareGuid (&FvHdrGUID, &NvVarStoreFvHeader->FileSystemGuid)) ||\r | |
653 | (NvVarStoreFvHeader->Signature != EFI_FVH_SIGNATURE) ||\r | |
654 | (NvVarStoreFvHeader->Attributes != 0x4feff) ||\r | |
47d98838 | 655 | ((NvVarStoreFvHeader->HeaderLength & 0x01) != 0) ||\r |
4f173db8 MX |
656 | (NvVarStoreFvHeader->Revision != EFI_FVH_REVISION) ||\r |
657 | (NvVarStoreFvHeader->FvLength != NvVarStoreSize)\r | |
658 | )\r | |
659 | {\r | |
660 | DEBUG ((DEBUG_ERROR, "NvVarStore FV headers were invalid.\n"));\r | |
661 | return FALSE;\r | |
662 | }\r | |
663 | \r | |
664 | //\r | |
665 | // Verify the header checksum\r | |
666 | //\r | |
667 | Checksum = CalculateSum16 ((VOID *)NvVarStoreFvHeader, NvVarStoreFvHeader->HeaderLength);\r | |
668 | \r | |
669 | if (Checksum != 0) {\r | |
670 | DEBUG ((DEBUG_ERROR, "NvVarStore FV checksum was invalid.\n"));\r | |
671 | return FALSE;\r | |
672 | }\r | |
673 | \r | |
674 | //\r | |
675 | // Verify the header signature, size, format, state\r | |
676 | //\r | |
677 | NvVarStoreHeader = (VARIABLE_STORE_HEADER *)(NvVarStoreBase + NvVarStoreFvHeader->HeaderLength);\r | |
678 | if ((!CompareGuid (&VarStoreHdrGUID, &NvVarStoreHeader->Signature)) ||\r | |
679 | (NvVarStoreHeader->Format != VARIABLE_STORE_FORMATTED) ||\r | |
680 | (NvVarStoreHeader->State != VARIABLE_STORE_HEALTHY) ||\r | |
681 | (NvVarStoreHeader->Size > (NvVarStoreFvHeader->FvLength - NvVarStoreFvHeader->HeaderLength)) ||\r | |
682 | (NvVarStoreHeader->Size < sizeof (VARIABLE_STORE_HEADER))\r | |
683 | )\r | |
684 | {\r | |
685 | DEBUG ((DEBUG_ERROR, "NvVarStore header signature/size/format/state were invalid.\n"));\r | |
686 | return FALSE;\r | |
687 | }\r | |
688 | \r | |
689 | //\r | |
690 | // Verify the header startId, state\r | |
691 | // Verify data to the end\r | |
692 | //\r | |
693 | VariableBase = (UINTN)NvVarStoreBase + NvVarStoreFvHeader->HeaderLength + sizeof (VARIABLE_STORE_HEADER);\r | |
694 | while (VariableOffset < (NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {\r | |
695 | VariableHeader = (AUTHENTICATED_VARIABLE_HEADER *)(VariableBase + VariableOffset);\r | |
696 | if (VariableHeader->StartId != VARIABLE_DATA) {\r | |
697 | if (!CheckPaddingData ((UINT8 *)VariableHeader, NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - VariableOffset)) {\r | |
698 | DEBUG ((DEBUG_ERROR, "NvVarStore variable header StartId was invalid.\n"));\r | |
699 | return FALSE;\r | |
700 | }\r | |
701 | \r | |
702 | VariableOffset = NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
703 | } else {\r | |
ceb52713 CYL |
704 | if (!((VariableHeader->State == VAR_HEADER_VALID_ONLY) ||\r |
705 | (VariableHeader->State == VAR_ADDED) ||\r | |
706 | (VariableHeader->State == (VAR_ADDED & VAR_DELETED)) ||\r | |
707 | (VariableHeader->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) ||\r | |
708 | (VariableHeader->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION & VAR_DELETED))))\r | |
4f173db8 MX |
709 | {\r |
710 | DEBUG ((DEBUG_ERROR, "NvVarStore Variable header State was invalid.\n"));\r | |
711 | return FALSE;\r | |
712 | }\r | |
713 | \r | |
714 | VariableOffset += sizeof (AUTHENTICATED_VARIABLE_HEADER) + VariableHeader->NameSize + VariableHeader->DataSize;\r | |
715 | // Verify VariableOffset should be less than or equal NvVarStoreHeader->Size - sizeof(VARIABLE_STORE_HEADER)\r | |
716 | if (VariableOffset > (NvVarStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER))) {\r | |
717 | DEBUG ((DEBUG_ERROR, "NvVarStore Variable header VariableOffset was invalid.\n"));\r | |
718 | return FALSE;\r | |
719 | }\r | |
720 | \r | |
721 | VariableOffsetBeforeAlign = VariableOffset;\r | |
722 | // 4 byte align\r | |
723 | VariableOffset = (VariableOffset + 3) & (UINTN)(~3);\r | |
724 | \r | |
725 | if (!CheckPaddingData ((UINT8 *)(VariableBase + VariableOffsetBeforeAlign), VariableOffset - VariableOffsetBeforeAlign)) {\r | |
726 | DEBUG ((DEBUG_ERROR, "NvVarStore Variable header PaddingData was invalid.\n"));\r | |
727 | return FALSE;\r | |
728 | }\r | |
729 | }\r | |
730 | }\r | |
731 | \r | |
732 | return TRUE;\r | |
733 | }\r | |
734 | \r | |
735 | /**\r | |
736 | Allocate storage for NV variables early on so it will be\r | |
737 | at a consistent address. Since VM memory is preserved\r | |
738 | across reboots, this allows the NV variable storage to survive\r | |
739 | a VM reboot.\r | |
740 | \r | |
741 | *\r | |
742 | * @retval VOID* The pointer to the storage for NV Variables\r | |
743 | */\r | |
744 | VOID *\r | |
745 | EFIAPI\r | |
746 | PlatformReserveEmuVariableNvStore (\r | |
747 | VOID\r | |
748 | )\r | |
749 | {\r | |
750 | VOID *VariableStore;\r | |
751 | UINT32 VarStoreSize;\r | |
752 | \r | |
753 | VarStoreSize = 2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r | |
754 | //\r | |
755 | // Allocate storage for NV variables early on so it will be\r | |
756 | // at a consistent address. Since VM memory is preserved\r | |
757 | // across reboots, this allows the NV variable storage to survive\r | |
758 | // a VM reboot.\r | |
759 | //\r | |
760 | VariableStore =\r | |
761 | AllocateRuntimePages (\r | |
762 | EFI_SIZE_TO_PAGES (VarStoreSize)\r | |
763 | );\r | |
764 | DEBUG ((\r | |
765 | DEBUG_INFO,\r | |
766 | "Reserved variable store memory: 0x%p; size: %dkb\n",\r | |
767 | VariableStore,\r | |
768 | VarStoreSize / 1024\r | |
769 | ));\r | |
770 | \r | |
771 | return VariableStore;\r | |
772 | }\r | |
773 | \r | |
774 | /**\r | |
775 | When OVMF is lauched with -bios parameter, UEFI variables will be\r | |
776 | partially emulated, and non-volatile variables may lose their contents\r | |
777 | after a reboot. This makes the secure boot feature not working.\r | |
778 | \r | |
779 | This function is used to initialize the EmuVariableNvStore\r | |
780 | with the conent in PcdOvmfFlashNvStorageVariableBase.\r | |
781 | \r | |
782 | @param[in] EmuVariableNvStore - A pointer to EmuVariableNvStore\r | |
783 | \r | |
784 | @retval EFI_SUCCESS - Successfully init the EmuVariableNvStore\r | |
785 | @retval Others - As the error code indicates\r | |
786 | */\r | |
787 | EFI_STATUS\r | |
788 | EFIAPI\r | |
789 | PlatformInitEmuVariableNvStore (\r | |
790 | IN VOID *EmuVariableNvStore\r | |
791 | )\r | |
792 | {\r | |
793 | UINT8 *Base;\r | |
794 | UINT32 Size;\r | |
795 | UINT32 EmuVariableNvStoreSize;\r | |
796 | \r | |
797 | EmuVariableNvStoreSize = 2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r | |
798 | if ((EmuVariableNvStore == NULL) || (EmuVariableNvStoreSize == 0)) {\r | |
799 | DEBUG ((DEBUG_ERROR, "Invalid EmuVariableNvStore parameter.\n"));\r | |
800 | return EFI_INVALID_PARAMETER;\r | |
801 | }\r | |
802 | \r | |
803 | Base = (UINT8 *)(UINTN)PcdGet32 (PcdOvmfFlashNvStorageVariableBase);\r | |
804 | Size = (UINT32)PcdGet32 (PcdFlashNvStorageVariableSize);\r | |
805 | ASSERT (Size < EmuVariableNvStoreSize);\r | |
806 | \r | |
807 | if (!PlatformValidateNvVarStore (Base, PcdGet32 (PcdCfvRawDataSize))) {\r | |
808 | ASSERT (FALSE);\r | |
809 | return EFI_INVALID_PARAMETER;\r | |
810 | }\r | |
811 | \r | |
812 | DEBUG ((DEBUG_INFO, "Init EmuVariableNvStore with the content in FlashNvStorage\n"));\r | |
813 | \r | |
814 | CopyMem (EmuVariableNvStore, Base, Size);\r | |
815 | \r | |
816 | return EFI_SUCCESS;\r | |
817 | }\r |