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 }
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
;
66 AddIoMemoryBaseSizeHob (
67 EFI_PHYSICAL_ADDRESS MemoryBase
,
71 BuildResourceDescriptorHob (
72 EFI_RESOURCE_MEMORY_MAPPED_IO
,
73 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
74 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
75 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
76 EFI_RESOURCE_ATTRIBUTE_TESTED
,
83 AddReservedMemoryBaseSizeHob (
84 EFI_PHYSICAL_ADDRESS MemoryBase
,
89 BuildResourceDescriptorHob (
90 EFI_RESOURCE_MEMORY_RESERVED
,
91 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
92 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
93 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
95 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
96 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
97 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
:
100 EFI_RESOURCE_ATTRIBUTE_TESTED
,
107 AddReservedMemoryRangeHob (
108 EFI_PHYSICAL_ADDRESS MemoryBase
,
109 EFI_PHYSICAL_ADDRESS MemoryLimit
,
113 AddReservedMemoryBaseSizeHob (MemoryBase
,
114 (UINT64
)(MemoryLimit
- MemoryBase
), Cacheable
);
118 AddIoMemoryRangeHob (
119 EFI_PHYSICAL_ADDRESS MemoryBase
,
120 EFI_PHYSICAL_ADDRESS MemoryLimit
123 AddIoMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
128 AddMemoryBaseSizeHob (
129 EFI_PHYSICAL_ADDRESS MemoryBase
,
133 BuildResourceDescriptorHob (
134 EFI_RESOURCE_SYSTEM_MEMORY
,
135 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
136 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
137 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
138 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
139 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
140 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
|
141 EFI_RESOURCE_ATTRIBUTE_TESTED
,
150 EFI_PHYSICAL_ADDRESS MemoryBase
,
151 EFI_PHYSICAL_ADDRESS MemoryLimit
154 AddMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
159 MemMapInitialization (
165 RETURN_STATUS PcdStatus
;
171 // Create Memory Type Information HOB
174 &gEfiMemoryTypeInformationGuid
,
175 mDefaultMemoryTypeInformation
,
176 sizeof(mDefaultMemoryTypeInformation
)
180 // Video memory + Legacy BIOS region
182 AddIoMemoryRangeHob (0x0A0000, BASE_1MB
);
185 // Add PCI IO Port space available for PCI resource allocations.
187 BuildResourceDescriptorHob (
189 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
190 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
,
194 PcdStatus
= PcdSet64S (PcdPciIoBase
, PciIoBase
);
195 ASSERT_RETURN_ERROR (PcdStatus
);
196 PcdStatus
= PcdSet64S (PcdPciIoSize
, PciIoSize
);
197 ASSERT_RETURN_ERROR (PcdStatus
);
201 PciExBarInitialization (
211 // We only support the 256MB size for the MMCONFIG area:
212 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
214 // The masks used below enforce the Q35 requirements that the MMCONFIG area
215 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
217 // Note that (b) also ensures that the minimum address width we have
218 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
219 // for DXE's page tables to cover the MMCONFIG area.
221 PciExBarBase
.Uint64
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
222 ASSERT ((PciExBarBase
.Uint32
[1] & MCH_PCIEXBAR_HIGHMASK
) == 0);
223 ASSERT ((PciExBarBase
.Uint32
[0] & MCH_PCIEXBAR_LOWMASK
) == 0);
226 // Clear the PCIEXBAREN bit first, before programming the high register.
228 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
), 0);
231 // Program the high register. Then program the low register, setting the
232 // MMCONFIG area size and enabling decoding at once.
234 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH
), PciExBarBase
.Uint32
[1]);
236 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
),
237 PciExBarBase
.Uint32
[0] | MCH_PCIEXBAR_BUS_FF
| MCH_PCIEXBAR_EN
252 RETURN_STATUS PcdStatus
;
260 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
261 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
262 // S3 resume as well, so we build it unconditionally.)
264 BuildCpuHob (mPhysMemAddressWidth
, 16);
267 // Determine platform type and save Host Bridge DID to PCD
269 switch (mHostBridgeDevId
) {
270 case INTEL_82441_DEVICE_ID
:
271 PmCmd
= POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET
);
272 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
273 PmbaAndVal
= ~(UINT32
)PIIX4_PMBA_MASK
;
274 PmbaOrVal
= PIIX4_PMBA_VALUE
;
275 AcpiCtlReg
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC
);
276 AcpiEnBit
= PIIX4_PMREGMISC_PMIOSE
;
278 case INTEL_Q35_MCH_DEVICE_ID
:
279 PmCmd
= POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET
);
280 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
281 PmbaAndVal
= ~(UINT32
)ICH9_PMBASE_MASK
;
282 PmbaOrVal
= ICH9_PMBASE_VALUE
;
283 AcpiCtlReg
= POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL
);
284 AcpiEnBit
= ICH9_ACPI_CNTL_ACPI_EN
;
287 if (XenPvhDetected ()) {
289 // There is no PCI bus in this case
293 DEBUG ((DEBUG_ERROR
, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
294 __FUNCTION__
, mHostBridgeDevId
));
298 PcdStatus
= PcdSet16S (PcdOvmfHostBridgePciDevId
, mHostBridgeDevId
);
299 ASSERT_RETURN_ERROR (PcdStatus
);
302 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
303 // has been configured (e.g., by Xen) and skip the setup here.
304 // This matches the logic in AcpiTimerLibConstructor ().
306 if ((PciRead8 (AcpiCtlReg
) & AcpiEnBit
) == 0) {
308 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
311 PciAndThenOr32 (Pmba
, PmbaAndVal
, PmbaOrVal
);
314 // 2. set PCICMD/IOSE
316 PciOr8 (PmCmd
, EFI_PCI_COMMAND_IO_SPACE
);
319 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
321 PciOr8 (AcpiCtlReg
, AcpiEnBit
);
324 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
326 // Set Root Complex Register Block BAR
329 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA
),
330 ICH9_ROOT_COMPLEX_BASE
| ICH9_RCBA_EN
334 // Set PCI Express Register Range Base Address
336 PciExBarInitialization ();
342 BootModeInitialization (
348 if (CmosRead8 (0xF) == 0xFE) {
349 mBootMode
= BOOT_ON_S3_RESUME
;
351 CmosWrite8 (0xF, 0x00);
353 Status
= PeiServicesSetBootMode (mBootMode
);
354 ASSERT_EFI_ERROR (Status
);
356 Status
= PeiServicesInstallPpi (mPpiBootMode
);
357 ASSERT_EFI_ERROR (Status
);
362 ReserveEmuVariableNvStore (
365 EFI_PHYSICAL_ADDRESS VariableStore
;
366 RETURN_STATUS PcdStatus
;
369 // Allocate storage for NV variables early on so it will be
370 // at a consistent address. Since VM memory is preserved
371 // across reboots, this allows the NV variable storage to survive
375 (EFI_PHYSICAL_ADDRESS
)(UINTN
)
376 AllocateRuntimePages (
377 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
))
380 "Reserved variable store memory: 0x%lX; size: %dkb\n",
382 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
)) / 1024
384 PcdStatus
= PcdSet64S (PcdEmuVariableNvStoreReserved
, VariableStore
);
385 ASSERT_RETURN_ERROR (PcdStatus
);
396 DEBUG ((DEBUG_INFO
, "CMOS:\n"));
398 for (Loop
= 0; Loop
< 0x80; Loop
++) {
399 if ((Loop
% 0x10) == 0) {
400 DEBUG ((DEBUG_INFO
, "%02x:", Loop
));
402 DEBUG ((DEBUG_INFO
, " %02x", CmosRead8 (Loop
)));
403 if ((Loop
% 0x10) == 0xf) {
404 DEBUG ((DEBUG_INFO
, "\n"));
412 Perform Platform PEI initialization.
414 @param FileHandle Handle of the file being invoked.
415 @param PeiServices Describes the list of possible PEI Services.
417 @return EFI_SUCCESS The PEIM initialized successfully.
422 InitializeXenPlatform (
423 IN EFI_PEI_FILE_HANDLE FileHandle
,
424 IN CONST EFI_PEI_SERVICES
**PeiServices
429 DEBUG ((DEBUG_INFO
, "Platform PEIM Loaded\n"));
434 DEBUG ((DEBUG_ERROR
, "ERROR: Xen isn't detected\n"));
440 // This S3 conditional test is mainly for HVM Direct Kernel Boot since
441 // QEMU fwcfg isn't really supported other than that.
443 if (QemuFwCfgS3Enabled ()) {
444 DEBUG ((DEBUG_INFO
, "S3 support was detected on QEMU\n"));
445 Status
= PcdSetBoolS (PcdAcpiS3Enable
, TRUE
);
446 ASSERT_EFI_ERROR (Status
);
451 BootModeInitialization ();
452 AddressWidthInitialization ();
455 // Query Host Bridge DID
457 mHostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
461 InitializeRamRegions ();
463 CalibrateLapicTimer ();
465 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
466 ReserveEmuVariableNvStore ();
467 PeiFvInitialization ();
468 MemMapInitialization ();
471 InstallClearCacheCallback ();
473 MiscInitialization ();