4 Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com>
5 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 // The package level header files this module uses
18 // The Library classes this module consumes
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/LocalApicLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/PciLib.h>
28 #include <Library/PeimEntryPoint.h>
29 #include <Library/PeiServicesLib.h>
30 #include <Library/ResourcePublicationLib.h>
31 #include <Guid/MemoryTypeInformation.h>
32 #include <Ppi/MasterBootMode.h>
33 #include <IndustryStandard/Pci22.h>
34 #include <OvmfPlatforms.h>
39 EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation
[] = {
40 { EfiACPIMemoryNVS
, 0x004 },
41 { EfiACPIReclaimMemory
, 0x008 },
42 { EfiReservedMemoryType
, 0x004 },
43 { EfiRuntimeServicesData
, 0x024 },
44 { EfiRuntimeServicesCode
, 0x030 },
45 { EfiBootServicesCode
, 0x180 },
46 { EfiBootServicesData
, 0xF00 },
47 { EfiMaxMemoryType
, 0x000 }
51 EFI_PEI_PPI_DESCRIPTOR mPpiBootMode
[] = {
53 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
54 &gEfiPeiMasterBootModePpiGuid
,
60 UINT16 mHostBridgeDevId
;
62 EFI_BOOT_MODE mBootMode
= BOOT_WITH_FULL_CONFIGURATION
;
64 BOOLEAN mS3Supported
= FALSE
;
69 AddIoMemoryBaseSizeHob (
70 EFI_PHYSICAL_ADDRESS MemoryBase
,
74 BuildResourceDescriptorHob (
75 EFI_RESOURCE_MEMORY_MAPPED_IO
,
76 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
77 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
78 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
79 EFI_RESOURCE_ATTRIBUTE_TESTED
,
86 AddReservedMemoryBaseSizeHob (
87 EFI_PHYSICAL_ADDRESS MemoryBase
,
92 BuildResourceDescriptorHob (
93 EFI_RESOURCE_MEMORY_RESERVED
,
94 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
95 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
96 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
98 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
99 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
100 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
:
103 EFI_RESOURCE_ATTRIBUTE_TESTED
,
110 AddIoMemoryRangeHob (
111 EFI_PHYSICAL_ADDRESS MemoryBase
,
112 EFI_PHYSICAL_ADDRESS MemoryLimit
115 AddIoMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
120 AddMemoryBaseSizeHob (
121 EFI_PHYSICAL_ADDRESS MemoryBase
,
125 BuildResourceDescriptorHob (
126 EFI_RESOURCE_SYSTEM_MEMORY
,
127 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
128 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
129 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
130 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
131 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
132 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
|
133 EFI_RESOURCE_ATTRIBUTE_TESTED
,
142 EFI_PHYSICAL_ADDRESS MemoryBase
,
143 EFI_PHYSICAL_ADDRESS MemoryLimit
146 AddMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
151 MemMapInitialization (
157 RETURN_STATUS PcdStatus
;
163 // Create Memory Type Information HOB
166 &gEfiMemoryTypeInformationGuid
,
167 mDefaultMemoryTypeInformation
,
168 sizeof(mDefaultMemoryTypeInformation
)
172 // Video memory + Legacy BIOS region
174 AddIoMemoryRangeHob (0x0A0000, BASE_1MB
);
182 TopOfLowRam
= GetSystemMemorySizeBelow4gb ();
184 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
186 // The MMCONFIG area is expected to fall between the top of low RAM and
187 // the base of the 32-bit PCI host aperture.
189 PciExBarBase
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
190 ASSERT (TopOfLowRam
<= PciExBarBase
);
191 ASSERT (PciExBarBase
<= MAX_UINT32
- SIZE_256MB
);
192 PciBase
= (UINT32
)(PciExBarBase
+ SIZE_256MB
);
194 PciBase
= (UINT32
)PcdGet64 (PcdPciMmio32Base
);
196 PciBase
= (TopOfLowRam
< BASE_2GB
) ? BASE_2GB
: TopOfLowRam
;
200 // address purpose size
201 // ------------ -------- -------------------------
202 // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)
203 // 0xFC000000 gap 44 MB
204 // 0xFEC00000 IO-APIC 4 KB
205 // 0xFEC01000 gap 1020 KB
206 // 0xFED00000 HPET 1 KB
207 // 0xFED00400 gap 111 KB
208 // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB
209 // 0xFED20000 gap 896 KB
210 // 0xFEE00000 LAPIC 1 MB
212 PciSize
= 0xFC000000 - PciBase
;
213 AddIoMemoryBaseSizeHob (PciBase
, PciSize
);
214 PcdStatus
= PcdSet64S (PcdPciMmio32Base
, PciBase
);
215 ASSERT_RETURN_ERROR (PcdStatus
);
216 PcdStatus
= PcdSet64S (PcdPciMmio32Size
, PciSize
);
217 ASSERT_RETURN_ERROR (PcdStatus
);
219 AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB
);
220 AddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB
);
221 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
222 AddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE
, SIZE_16KB
);
224 // Note: there should be an
226 // AddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);
228 // call below, just like the one above for RCBA. However, Linux insists
229 // that the MMCONFIG area be marked in the E820 or UEFI memory map as
230 // "reserved memory" -- Linux does not content itself with a simple gap
231 // in the memory map wherever the MCFG ACPI table points to.
233 // This appears to be a safety measure. The PCI Firmware Specification
234 // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can
235 // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory
236 // [...]". (Emphasis added here.)
238 // Normally we add memory resource descriptor HOBs in
239 // QemuInitializeRam(), and pre-allocate from those with memory
240 // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area
241 // is most definitely not RAM; so, as an exception, cover it with
242 // uncacheable reserved memory right here.
244 AddReservedMemoryBaseSizeHob (PciExBarBase
, SIZE_256MB
, FALSE
);
245 BuildMemoryAllocationHob (PciExBarBase
, SIZE_256MB
,
246 EfiReservedMemoryType
);
248 AddIoMemoryBaseSizeHob (PcdGet32(PcdCpuLocalApicBaseAddress
), SIZE_1MB
);
251 // On Q35, the IO Port space is available for PCI resource allocations from
254 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
257 ASSERT ((ICH9_PMBASE_VALUE
& 0xF000) < PciIoBase
);
262 // Add PCI IO Port space available for PCI resource allocations.
264 BuildResourceDescriptorHob (
266 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
267 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
,
271 PcdStatus
= PcdSet64S (PcdPciIoBase
, PciIoBase
);
272 ASSERT_RETURN_ERROR (PcdStatus
);
273 PcdStatus
= PcdSet64S (PcdPciIoSize
, PciIoSize
);
274 ASSERT_RETURN_ERROR (PcdStatus
);
278 NoexecDxeInitialization (
285 PciExBarInitialization (
295 // We only support the 256MB size for the MMCONFIG area:
296 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
298 // The masks used below enforce the Q35 requirements that the MMCONFIG area
299 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
301 // Note that (b) also ensures that the minimum address width we have
302 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
303 // for DXE's page tables to cover the MMCONFIG area.
305 PciExBarBase
.Uint64
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
306 ASSERT ((PciExBarBase
.Uint32
[1] & MCH_PCIEXBAR_HIGHMASK
) == 0);
307 ASSERT ((PciExBarBase
.Uint32
[0] & MCH_PCIEXBAR_LOWMASK
) == 0);
310 // Clear the PCIEXBAREN bit first, before programming the high register.
312 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
), 0);
315 // Program the high register. Then program the low register, setting the
316 // MMCONFIG area size and enabling decoding at once.
318 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH
), PciExBarBase
.Uint32
[1]);
320 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
),
321 PciExBarBase
.Uint32
[0] | MCH_PCIEXBAR_BUS_FF
| MCH_PCIEXBAR_EN
336 RETURN_STATUS PcdStatus
;
344 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
345 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
346 // S3 resume as well, so we build it unconditionally.)
348 BuildCpuHob (mPhysMemAddressWidth
, 16);
351 // Determine platform type and save Host Bridge DID to PCD
353 switch (mHostBridgeDevId
) {
354 case 0x7432: // BHYVE (AMD hostbridge)
355 case 0x1275: // BHYVE (Intel hostbridge)
356 case INTEL_82441_DEVICE_ID
:
357 PmCmd
= POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET
);
358 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
359 PmbaAndVal
= ~(UINT32
)PIIX4_PMBA_MASK
;
360 PmbaOrVal
= PIIX4_PMBA_VALUE
;
361 AcpiCtlReg
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC
);
362 AcpiEnBit
= PIIX4_PMREGMISC_PMIOSE
;
364 case INTEL_Q35_MCH_DEVICE_ID
:
365 PmCmd
= POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET
);
366 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
367 PmbaAndVal
= ~(UINT32
)ICH9_PMBASE_MASK
;
368 PmbaOrVal
= ICH9_PMBASE_VALUE
;
369 AcpiCtlReg
= POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL
);
370 AcpiEnBit
= ICH9_ACPI_CNTL_ACPI_EN
;
373 DEBUG ((DEBUG_ERROR
, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
374 __FUNCTION__
, mHostBridgeDevId
));
378 PcdStatus
= PcdSet16S (PcdOvmfHostBridgePciDevId
, mHostBridgeDevId
);
379 ASSERT_RETURN_ERROR (PcdStatus
);
382 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
383 // has been configured (e.g., by Xen) and skip the setup here.
384 // This matches the logic in AcpiTimerLibConstructor ().
386 if ((PciRead8 (AcpiCtlReg
) & AcpiEnBit
) == 0) {
388 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
391 PciAndThenOr32 (Pmba
, PmbaAndVal
, PmbaOrVal
);
394 // 2. set PCICMD/IOSE
396 PciOr8 (PmCmd
, EFI_PCI_COMMAND_IO_SPACE
);
399 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
401 PciOr8 (AcpiCtlReg
, AcpiEnBit
);
404 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
406 // Set Root Complex Register Block BAR
409 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA
),
410 ICH9_ROOT_COMPLEX_BASE
| ICH9_RCBA_EN
414 // Set PCI Express Register Range Base Address
416 PciExBarInitialization ();
422 BootModeInitialization (
428 if (CmosRead8 (0xF) == 0xFE) {
429 mBootMode
= BOOT_ON_S3_RESUME
;
431 CmosWrite8 (0xF, 0x00);
433 Status
= PeiServicesSetBootMode (mBootMode
);
434 ASSERT_EFI_ERROR (Status
);
436 Status
= PeiServicesInstallPpi (mPpiBootMode
);
437 ASSERT_EFI_ERROR (Status
);
442 ReserveEmuVariableNvStore (
445 EFI_PHYSICAL_ADDRESS VariableStore
;
446 RETURN_STATUS PcdStatus
;
449 // Allocate storage for NV variables early on so it will be
450 // at a consistent address. Since VM memory is preserved
451 // across reboots, this allows the NV variable storage to survive
455 (EFI_PHYSICAL_ADDRESS
)(UINTN
)
456 AllocateRuntimePages (
457 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
))
460 "Reserved variable store memory: 0x%lX; size: %dkb\n",
462 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
)) / 1024
464 PcdStatus
= PcdSet64S (PcdEmuVariableNvStoreReserved
, VariableStore
);
465 ASSERT_RETURN_ERROR (PcdStatus
);
476 DEBUG ((DEBUG_INFO
, "CMOS:\n"));
478 for (Loop
= 0; Loop
< 0x80; Loop
++) {
479 if ((Loop
% 0x10) == 0) {
480 DEBUG ((DEBUG_INFO
, "%02x:", Loop
));
482 DEBUG ((DEBUG_INFO
, " %02x", CmosRead8 (Loop
)));
483 if ((Loop
% 0x10) == 0xf) {
484 DEBUG ((DEBUG_INFO
, "\n"));
495 #if defined (MDE_CPU_X64)
496 if (FeaturePcdGet (PcdSmmSmramRequire
) && mS3Supported
) {
498 "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n", __FUNCTION__
));
500 "%a: Please disable S3 on the QEMU command line (see the README),\n",
503 "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n", __FUNCTION__
));
512 Fetch the number of boot CPUs from QEMU and expose it to UefiCpuPkg modules.
513 Set the mMaxCpuCount variable.
516 MaxCpuCountInitialization (
520 UINT16 ProcessorCount
= 0;
521 RETURN_STATUS PcdStatus
;
524 // If the fw_cfg key or fw_cfg entirely is unavailable, load mMaxCpuCount
525 // from the PCD default. No change to PCDs.
527 if (ProcessorCount
== 0) {
528 mMaxCpuCount
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
532 // Otherwise, set mMaxCpuCount to the value reported by QEMU.
534 mMaxCpuCount
= ProcessorCount
;
536 // Additionally, tell UefiCpuPkg modules (a) the exact number of VCPUs, (b)
537 // to wait, in the initial AP bringup, exactly as long as it takes for all of
538 // the APs to report in. For this, we set the longest representable timeout
539 // (approx. 71 minutes).
541 PcdStatus
= PcdSet32S (PcdCpuMaxLogicalProcessorNumber
, ProcessorCount
);
542 ASSERT_RETURN_ERROR (PcdStatus
);
543 PcdStatus
= PcdSet32S (PcdCpuApInitTimeOutInMicroSeconds
, MAX_UINT32
);
544 ASSERT_RETURN_ERROR (PcdStatus
);
545 DEBUG ((DEBUG_INFO
, "%a: QEMU reports %d processor(s)\n", __FUNCTION__
,
551 Perform Platform PEI initialization.
553 @param FileHandle Handle of the file being invoked.
554 @param PeiServices Describes the list of possible PEI Services.
556 @return EFI_SUCCESS The PEIM initialized successfully.
562 IN EFI_PEI_FILE_HANDLE FileHandle
,
563 IN CONST EFI_PEI_SERVICES
**PeiServices
566 DEBUG ((DEBUG_INFO
, "Platform PEIM Loaded\n"));
569 // Initialize Local APIC Timer hardware and disable Local APIC Timer
570 // interrupts before initializing the Debug Agent and the debug timer is
573 InitializeApicTimer (0, MAX_UINT32
, TRUE
, 5);
574 DisableApicTimerInterrupt ();
578 BootModeInitialization ();
579 AddressWidthInitialization ();
580 MaxCpuCountInitialization ();
583 // Query Host Bridge DID
585 mHostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
587 if (FeaturePcdGet (PcdSmmSmramRequire
)) {
588 Q35TsegMbytesInitialization ();
593 InitializeRamRegions ();
595 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
596 if (!FeaturePcdGet (PcdSmmSmramRequire
)) {
597 ReserveEmuVariableNvStore ();
599 PeiFvInitialization ();
600 MemMapInitialization ();
601 NoexecDxeInitialization ();
604 InstallClearCacheCallback ();
606 MiscInitialization ();
607 InstallFeatureControlCallback ();