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/BaseMemoryLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/HobLib.h>
24 #include <Library/IoLib.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/QemuFwCfgS3Lib.h>
31 #include <Library/ResourcePublicationLib.h>
32 #include <Guid/MemoryTypeInformation.h>
33 #include <Ppi/MasterBootMode.h>
34 #include <IndustryStandard/Pci22.h>
35 #include <OvmfPlatforms.h>
40 EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation
[] = {
41 { EfiACPIMemoryNVS
, 0x004 },
42 { EfiACPIReclaimMemory
, 0x008 },
43 { EfiReservedMemoryType
, 0x004 },
44 { EfiRuntimeServicesData
, 0x024 },
45 { EfiRuntimeServicesCode
, 0x030 },
46 { EfiBootServicesCode
, 0x180 },
47 { EfiBootServicesData
, 0xF00 },
48 { EfiMaxMemoryType
, 0x000 }
51 EFI_PEI_PPI_DESCRIPTOR mPpiBootMode
[] = {
53 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
54 &gEfiPeiMasterBootModePpiGuid
,
59 UINT16 mHostBridgeDevId
;
61 EFI_BOOT_MODE mBootMode
= BOOT_WITH_FULL_CONFIGURATION
;
64 AddIoMemoryBaseSizeHob (
65 EFI_PHYSICAL_ADDRESS MemoryBase
,
69 BuildResourceDescriptorHob (
70 EFI_RESOURCE_MEMORY_MAPPED_IO
,
71 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
72 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
73 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
74 EFI_RESOURCE_ATTRIBUTE_TESTED
,
81 AddReservedMemoryBaseSizeHob (
82 EFI_PHYSICAL_ADDRESS MemoryBase
,
87 BuildResourceDescriptorHob (
88 EFI_RESOURCE_MEMORY_RESERVED
,
89 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
90 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
91 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
93 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
94 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
95 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
:
98 EFI_RESOURCE_ATTRIBUTE_TESTED
,
105 AddReservedMemoryRangeHob (
106 EFI_PHYSICAL_ADDRESS MemoryBase
,
107 EFI_PHYSICAL_ADDRESS MemoryLimit
,
111 AddReservedMemoryBaseSizeHob (
113 (UINT64
)(MemoryLimit
- MemoryBase
),
119 AddIoMemoryRangeHob (
120 EFI_PHYSICAL_ADDRESS MemoryBase
,
121 EFI_PHYSICAL_ADDRESS MemoryLimit
124 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
,
149 EFI_PHYSICAL_ADDRESS MemoryBase
,
150 EFI_PHYSICAL_ADDRESS MemoryLimit
153 AddMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
157 MemMapInitialization (
163 RETURN_STATUS PcdStatus
;
169 // Create Memory Type Information HOB
172 &gEfiMemoryTypeInformationGuid
,
173 mDefaultMemoryTypeInformation
,
174 sizeof (mDefaultMemoryTypeInformation
)
178 // Video memory + Legacy BIOS region
180 AddIoMemoryRangeHob (0x0A0000, BASE_1MB
);
183 // Add PCI IO Port space available for PCI resource allocations.
185 BuildResourceDescriptorHob (
187 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
188 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
,
192 PcdStatus
= PcdSet64S (PcdPciIoBase
, PciIoBase
);
193 ASSERT_RETURN_ERROR (PcdStatus
);
194 PcdStatus
= PcdSet64S (PcdPciIoSize
, PciIoSize
);
195 ASSERT_RETURN_ERROR (PcdStatus
);
199 PciExBarInitialization (
209 // We only support the 256MB size for the MMCONFIG area:
210 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
212 // The masks used below enforce the Q35 requirements that the MMCONFIG area
213 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
215 // Note that (b) also ensures that the minimum address width we have
216 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
217 // for DXE's page tables to cover the MMCONFIG area.
219 PciExBarBase
.Uint64
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
220 ASSERT ((PciExBarBase
.Uint32
[1] & MCH_PCIEXBAR_HIGHMASK
) == 0);
221 ASSERT ((PciExBarBase
.Uint32
[0] & MCH_PCIEXBAR_LOWMASK
) == 0);
224 // Clear the PCIEXBAREN bit first, before programming the high register.
226 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
), 0);
229 // Program the high register. Then program the low register, setting the
230 // MMCONFIG area size and enabling decoding at once.
232 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH
), PciExBarBase
.Uint32
[1]);
234 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
),
235 PciExBarBase
.Uint32
[0] | MCH_PCIEXBAR_BUS_FF
| MCH_PCIEXBAR_EN
250 RETURN_STATUS PcdStatus
;
258 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
259 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
260 // S3 resume as well, so we build it unconditionally.)
262 BuildCpuHob (mPhysMemAddressWidth
, 16);
265 // Determine platform type and save Host Bridge DID to PCD
267 switch (mHostBridgeDevId
) {
268 case INTEL_82441_DEVICE_ID
:
269 PmCmd
= POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET
);
270 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
271 PmbaAndVal
= ~(UINT32
)PIIX4_PMBA_MASK
;
272 PmbaOrVal
= PIIX4_PMBA_VALUE
;
273 AcpiCtlReg
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC
);
274 AcpiEnBit
= PIIX4_PMREGMISC_PMIOSE
;
276 case INTEL_Q35_MCH_DEVICE_ID
:
277 PmCmd
= POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET
);
278 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
279 PmbaAndVal
= ~(UINT32
)ICH9_PMBASE_MASK
;
280 PmbaOrVal
= ICH9_PMBASE_VALUE
;
281 AcpiCtlReg
= POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL
);
282 AcpiEnBit
= ICH9_ACPI_CNTL_ACPI_EN
;
285 if (XenPvhDetected ()) {
287 // There is no PCI bus in this case
294 "%a: Unknown Host Bridge Device ID: 0x%04x\n",
302 PcdStatus
= PcdSet16S (PcdOvmfHostBridgePciDevId
, mHostBridgeDevId
);
303 ASSERT_RETURN_ERROR (PcdStatus
);
306 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
307 // has been configured (e.g., by Xen) and skip the setup here.
308 // This matches the logic in AcpiTimerLibConstructor ().
310 if ((PciRead8 (AcpiCtlReg
) & AcpiEnBit
) == 0) {
312 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
315 PciAndThenOr32 (Pmba
, PmbaAndVal
, PmbaOrVal
);
318 // 2. set PCICMD/IOSE
320 PciOr8 (PmCmd
, EFI_PCI_COMMAND_IO_SPACE
);
323 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
325 PciOr8 (AcpiCtlReg
, AcpiEnBit
);
328 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
330 // Set Root Complex Register Block BAR
333 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA
),
334 ICH9_ROOT_COMPLEX_BASE
| ICH9_RCBA_EN
338 // Set PCI Express Register Range Base Address
340 PciExBarInitialization ();
345 BootModeInitialization (
351 if (CmosRead8 (0xF) == 0xFE) {
352 mBootMode
= BOOT_ON_S3_RESUME
;
355 CmosWrite8 (0xF, 0x00);
357 Status
= PeiServicesSetBootMode (mBootMode
);
358 ASSERT_EFI_ERROR (Status
);
360 Status
= PeiServicesInstallPpi (mPpiBootMode
);
361 ASSERT_EFI_ERROR (Status
);
365 ReserveEmuVariableNvStore (
368 EFI_PHYSICAL_ADDRESS VariableStore
;
369 RETURN_STATUS PcdStatus
;
372 // Allocate storage for NV variables early on so it will be
373 // at a consistent address. Since VM memory is preserved
374 // across reboots, this allows the NV variable storage to survive
378 (EFI_PHYSICAL_ADDRESS
)(UINTN
)
379 AllocateRuntimePages (
380 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
))
384 "Reserved variable store memory: 0x%lX; size: %dkb\n",
386 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
)) / 1024
388 PcdStatus
= PcdSet64S (PcdEmuVariableNvStoreReserved
, VariableStore
);
389 ASSERT_RETURN_ERROR (PcdStatus
);
399 DEBUG ((DEBUG_INFO
, "CMOS:\n"));
401 for (Loop
= 0; Loop
< 0x80; Loop
++) {
402 if ((Loop
% 0x10) == 0) {
403 DEBUG ((DEBUG_INFO
, "%02x:", Loop
));
406 DEBUG ((DEBUG_INFO
, " %02x", CmosRead8 (Loop
)));
407 if ((Loop
% 0x10) == 0xf) {
408 DEBUG ((DEBUG_INFO
, "\n"));
413 EFI_HOB_PLATFORM_INFO
*
414 BuildPlatformInfoHob (
418 EFI_HOB_PLATFORM_INFO PlatformInfoHob
;
419 EFI_HOB_GUID_TYPE
*GuidHob
;
421 ZeroMem (&PlatformInfoHob
, sizeof PlatformInfoHob
);
422 BuildGuidDataHob (&gUefiOvmfPkgPlatformInfoGuid
, &PlatformInfoHob
, sizeof (EFI_HOB_PLATFORM_INFO
));
423 GuidHob
= GetFirstGuidHob (&gUefiOvmfPkgPlatformInfoGuid
);
424 return (EFI_HOB_PLATFORM_INFO
*)GET_GUID_HOB_DATA (GuidHob
);
428 Perform Platform PEI initialization.
430 @param FileHandle Handle of the file being invoked.
431 @param PeiServices Describes the list of possible PEI Services.
433 @return EFI_SUCCESS The PEIM initialized successfully.
438 InitializeXenPlatform (
439 IN EFI_PEI_FILE_HANDLE FileHandle
,
440 IN CONST EFI_PEI_SERVICES
**PeiServices
445 DEBUG ((DEBUG_INFO
, "Platform PEIM Loaded\n"));
448 // Platform Info HOB used by QemuFw libraries
450 BuildPlatformInfoHob ();
455 DEBUG ((DEBUG_ERROR
, "ERROR: Xen isn't detected\n"));
461 // This S3 conditional test is mainly for HVM Direct Kernel Boot since
462 // QEMU fwcfg isn't really supported other than that.
464 if (QemuFwCfgS3Enabled ()) {
465 DEBUG ((DEBUG_INFO
, "S3 support was detected on QEMU\n"));
466 Status
= PcdSetBoolS (PcdAcpiS3Enable
, TRUE
);
467 ASSERT_EFI_ERROR (Status
);
472 BootModeInitialization ();
473 AddressWidthInitialization ();
476 // Query Host Bridge DID
478 mHostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
482 InitializeRamRegions ();
484 CalibrateLapicTimer ();
486 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
487 ReserveEmuVariableNvStore ();
488 PeiFvInitialization ();
489 MemMapInitialization ();
492 InstallClearCacheCallback ();
494 MiscInitialization ();