4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
6 Copyright (c) 2019, Citrix Systems, Inc.
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/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/PciLib.h>
27 #include <Library/PeimEntryPoint.h>
28 #include <Library/PeiServicesLib.h>
29 #include <Library/QemuFwCfgS3Lib.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 }
50 EFI_PEI_PPI_DESCRIPTOR mPpiBootMode
[] = {
52 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
53 &gEfiPeiMasterBootModePpiGuid
,
58 UINT16 mHostBridgeDevId
;
60 EFI_BOOT_MODE mBootMode
= BOOT_WITH_FULL_CONFIGURATION
;
63 AddIoMemoryBaseSizeHob (
64 EFI_PHYSICAL_ADDRESS MemoryBase
,
68 BuildResourceDescriptorHob (
69 EFI_RESOURCE_MEMORY_MAPPED_IO
,
70 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
71 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
72 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
73 EFI_RESOURCE_ATTRIBUTE_TESTED
,
80 AddReservedMemoryBaseSizeHob (
81 EFI_PHYSICAL_ADDRESS MemoryBase
,
86 BuildResourceDescriptorHob (
87 EFI_RESOURCE_MEMORY_RESERVED
,
88 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
89 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
90 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
92 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
93 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
94 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
:
97 EFI_RESOURCE_ATTRIBUTE_TESTED
,
104 AddReservedMemoryRangeHob (
105 EFI_PHYSICAL_ADDRESS MemoryBase
,
106 EFI_PHYSICAL_ADDRESS MemoryLimit
,
110 AddReservedMemoryBaseSizeHob (
112 (UINT64
)(MemoryLimit
- MemoryBase
),
118 AddIoMemoryRangeHob (
119 EFI_PHYSICAL_ADDRESS MemoryBase
,
120 EFI_PHYSICAL_ADDRESS MemoryLimit
123 AddIoMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
127 AddMemoryBaseSizeHob (
128 EFI_PHYSICAL_ADDRESS MemoryBase
,
132 BuildResourceDescriptorHob (
133 EFI_RESOURCE_SYSTEM_MEMORY
,
134 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
135 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
136 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
137 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
138 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
139 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
|
140 EFI_RESOURCE_ATTRIBUTE_TESTED
,
148 EFI_PHYSICAL_ADDRESS MemoryBase
,
149 EFI_PHYSICAL_ADDRESS MemoryLimit
152 AddMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
156 MemMapInitialization (
162 RETURN_STATUS PcdStatus
;
168 // Create Memory Type Information HOB
171 &gEfiMemoryTypeInformationGuid
,
172 mDefaultMemoryTypeInformation
,
173 sizeof (mDefaultMemoryTypeInformation
)
177 // Video memory + Legacy BIOS region
179 AddIoMemoryRangeHob (0x0A0000, BASE_1MB
);
182 // Add PCI IO Port space available for PCI resource allocations.
184 BuildResourceDescriptorHob (
186 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
187 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
,
191 PcdStatus
= PcdSet64S (PcdPciIoBase
, PciIoBase
);
192 ASSERT_RETURN_ERROR (PcdStatus
);
193 PcdStatus
= PcdSet64S (PcdPciIoSize
, PciIoSize
);
194 ASSERT_RETURN_ERROR (PcdStatus
);
198 PciExBarInitialization (
208 // We only support the 256MB size for the MMCONFIG area:
209 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
211 // The masks used below enforce the Q35 requirements that the MMCONFIG area
212 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
214 // Note that (b) also ensures that the minimum address width we have
215 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
216 // for DXE's page tables to cover the MMCONFIG area.
218 PciExBarBase
.Uint64
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
219 ASSERT ((PciExBarBase
.Uint32
[1] & MCH_PCIEXBAR_HIGHMASK
) == 0);
220 ASSERT ((PciExBarBase
.Uint32
[0] & MCH_PCIEXBAR_LOWMASK
) == 0);
223 // Clear the PCIEXBAREN bit first, before programming the high register.
225 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
), 0);
228 // Program the high register. Then program the low register, setting the
229 // MMCONFIG area size and enabling decoding at once.
231 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH
), PciExBarBase
.Uint32
[1]);
233 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
),
234 PciExBarBase
.Uint32
[0] | MCH_PCIEXBAR_BUS_FF
| MCH_PCIEXBAR_EN
249 RETURN_STATUS PcdStatus
;
257 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
258 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
259 // S3 resume as well, so we build it unconditionally.)
261 BuildCpuHob (mPhysMemAddressWidth
, 16);
264 // Determine platform type and save Host Bridge DID to PCD
266 switch (mHostBridgeDevId
) {
267 case INTEL_82441_DEVICE_ID
:
268 PmCmd
= POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET
);
269 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
270 PmbaAndVal
= ~(UINT32
)PIIX4_PMBA_MASK
;
271 PmbaOrVal
= PIIX4_PMBA_VALUE
;
272 AcpiCtlReg
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC
);
273 AcpiEnBit
= PIIX4_PMREGMISC_PMIOSE
;
275 case INTEL_Q35_MCH_DEVICE_ID
:
276 PmCmd
= POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET
);
277 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
278 PmbaAndVal
= ~(UINT32
)ICH9_PMBASE_MASK
;
279 PmbaOrVal
= ICH9_PMBASE_VALUE
;
280 AcpiCtlReg
= POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL
);
281 AcpiEnBit
= ICH9_ACPI_CNTL_ACPI_EN
;
284 if (XenPvhDetected ()) {
286 // There is no PCI bus in this case
293 "%a: Unknown Host Bridge Device ID: 0x%04x\n",
301 PcdStatus
= PcdSet16S (PcdOvmfHostBridgePciDevId
, mHostBridgeDevId
);
302 ASSERT_RETURN_ERROR (PcdStatus
);
305 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
306 // has been configured (e.g., by Xen) and skip the setup here.
307 // This matches the logic in AcpiTimerLibConstructor ().
309 if ((PciRead8 (AcpiCtlReg
) & AcpiEnBit
) == 0) {
311 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
314 PciAndThenOr32 (Pmba
, PmbaAndVal
, PmbaOrVal
);
317 // 2. set PCICMD/IOSE
319 PciOr8 (PmCmd
, EFI_PCI_COMMAND_IO_SPACE
);
322 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
324 PciOr8 (AcpiCtlReg
, AcpiEnBit
);
327 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
329 // Set Root Complex Register Block BAR
332 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA
),
333 ICH9_ROOT_COMPLEX_BASE
| ICH9_RCBA_EN
337 // Set PCI Express Register Range Base Address
339 PciExBarInitialization ();
344 BootModeInitialization (
350 if (CmosRead8 (0xF) == 0xFE) {
351 mBootMode
= BOOT_ON_S3_RESUME
;
354 CmosWrite8 (0xF, 0x00);
356 Status
= PeiServicesSetBootMode (mBootMode
);
357 ASSERT_EFI_ERROR (Status
);
359 Status
= PeiServicesInstallPpi (mPpiBootMode
);
360 ASSERT_EFI_ERROR (Status
);
364 ReserveEmuVariableNvStore (
367 EFI_PHYSICAL_ADDRESS VariableStore
;
368 RETURN_STATUS PcdStatus
;
371 // Allocate storage for NV variables early on so it will be
372 // at a consistent address. Since VM memory is preserved
373 // across reboots, this allows the NV variable storage to survive
377 (EFI_PHYSICAL_ADDRESS
)(UINTN
)
378 AllocateRuntimePages (
379 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
))
383 "Reserved variable store memory: 0x%lX; size: %dkb\n",
385 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
)) / 1024
387 PcdStatus
= PcdSet64S (PcdEmuVariableNvStoreReserved
, VariableStore
);
388 ASSERT_RETURN_ERROR (PcdStatus
);
398 DEBUG ((DEBUG_INFO
, "CMOS:\n"));
400 for (Loop
= 0; Loop
< 0x80; Loop
++) {
401 if ((Loop
% 0x10) == 0) {
402 DEBUG ((DEBUG_INFO
, "%02x:", Loop
));
405 DEBUG ((DEBUG_INFO
, " %02x", CmosRead8 (Loop
)));
406 if ((Loop
% 0x10) == 0xf) {
407 DEBUG ((DEBUG_INFO
, "\n"));
413 Perform Platform PEI initialization.
415 @param FileHandle Handle of the file being invoked.
416 @param PeiServices Describes the list of possible PEI Services.
418 @return EFI_SUCCESS The PEIM initialized successfully.
423 InitializeXenPlatform (
424 IN EFI_PEI_FILE_HANDLE FileHandle
,
425 IN CONST EFI_PEI_SERVICES
**PeiServices
430 DEBUG ((DEBUG_INFO
, "Platform PEIM Loaded\n"));
435 DEBUG ((DEBUG_ERROR
, "ERROR: Xen isn't detected\n"));
441 // This S3 conditional test is mainly for HVM Direct Kernel Boot since
442 // QEMU fwcfg isn't really supported other than that.
444 if (QemuFwCfgS3Enabled ()) {
445 DEBUG ((DEBUG_INFO
, "S3 support was detected on QEMU\n"));
446 Status
= PcdSetBoolS (PcdAcpiS3Enable
, TRUE
);
447 ASSERT_EFI_ERROR (Status
);
452 BootModeInitialization ();
453 AddressWidthInitialization ();
456 // Query Host Bridge DID
458 mHostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
462 InitializeRamRegions ();
464 CalibrateLapicTimer ();
466 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
467 ReserveEmuVariableNvStore ();
468 PeiFvInitialization ();
469 MemMapInitialization ();
472 InstallClearCacheCallback ();
474 MiscInitialization ();