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/ResourcePublicationLib.h>
30 #include <Guid/MemoryTypeInformation.h>
31 #include <Ppi/MasterBootMode.h>
32 #include <IndustryStandard/Pci22.h>
33 #include <OvmfPlatforms.h>
38 EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation
[] = {
39 { EfiACPIMemoryNVS
, 0x004 },
40 { EfiACPIReclaimMemory
, 0x008 },
41 { EfiReservedMemoryType
, 0x004 },
42 { EfiRuntimeServicesData
, 0x024 },
43 { EfiRuntimeServicesCode
, 0x030 },
44 { EfiBootServicesCode
, 0x180 },
45 { EfiBootServicesData
, 0xF00 },
46 { EfiMaxMemoryType
, 0x000 }
50 EFI_PEI_PPI_DESCRIPTOR mPpiBootMode
[] = {
52 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
53 &gEfiPeiMasterBootModePpiGuid
,
59 UINT16 mHostBridgeDevId
;
61 EFI_BOOT_MODE mBootMode
= BOOT_WITH_FULL_CONFIGURATION
;
65 AddIoMemoryBaseSizeHob (
66 EFI_PHYSICAL_ADDRESS MemoryBase
,
70 BuildResourceDescriptorHob (
71 EFI_RESOURCE_MEMORY_MAPPED_IO
,
72 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
73 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
74 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
75 EFI_RESOURCE_ATTRIBUTE_TESTED
,
82 AddReservedMemoryBaseSizeHob (
83 EFI_PHYSICAL_ADDRESS MemoryBase
,
88 BuildResourceDescriptorHob (
89 EFI_RESOURCE_MEMORY_RESERVED
,
90 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
91 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
92 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
94 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
95 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
96 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
:
99 EFI_RESOURCE_ATTRIBUTE_TESTED
,
106 AddIoMemoryRangeHob (
107 EFI_PHYSICAL_ADDRESS MemoryBase
,
108 EFI_PHYSICAL_ADDRESS MemoryLimit
111 AddIoMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
116 AddMemoryBaseSizeHob (
117 EFI_PHYSICAL_ADDRESS MemoryBase
,
121 BuildResourceDescriptorHob (
122 EFI_RESOURCE_SYSTEM_MEMORY
,
123 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
124 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
125 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
|
126 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
|
127 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
|
128 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
|
129 EFI_RESOURCE_ATTRIBUTE_TESTED
,
138 EFI_PHYSICAL_ADDRESS MemoryBase
,
139 EFI_PHYSICAL_ADDRESS MemoryLimit
142 AddMemoryBaseSizeHob (MemoryBase
, (UINT64
)(MemoryLimit
- MemoryBase
));
147 MemMapInitialization (
153 RETURN_STATUS PcdStatus
;
159 // Create Memory Type Information HOB
162 &gEfiMemoryTypeInformationGuid
,
163 mDefaultMemoryTypeInformation
,
164 sizeof(mDefaultMemoryTypeInformation
)
168 // Video memory + Legacy BIOS region
170 AddIoMemoryRangeHob (0x0A0000, BASE_1MB
);
173 // Add PCI IO Port space available for PCI resource allocations.
175 BuildResourceDescriptorHob (
177 EFI_RESOURCE_ATTRIBUTE_PRESENT
|
178 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
,
182 PcdStatus
= PcdSet64S (PcdPciIoBase
, PciIoBase
);
183 ASSERT_RETURN_ERROR (PcdStatus
);
184 PcdStatus
= PcdSet64S (PcdPciIoSize
, PciIoSize
);
185 ASSERT_RETURN_ERROR (PcdStatus
);
189 PciExBarInitialization (
199 // We only support the 256MB size for the MMCONFIG area:
200 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
202 // The masks used below enforce the Q35 requirements that the MMCONFIG area
203 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
205 // Note that (b) also ensures that the minimum address width we have
206 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
207 // for DXE's page tables to cover the MMCONFIG area.
209 PciExBarBase
.Uint64
= FixedPcdGet64 (PcdPciExpressBaseAddress
);
210 ASSERT ((PciExBarBase
.Uint32
[1] & MCH_PCIEXBAR_HIGHMASK
) == 0);
211 ASSERT ((PciExBarBase
.Uint32
[0] & MCH_PCIEXBAR_LOWMASK
) == 0);
214 // Clear the PCIEXBAREN bit first, before programming the high register.
216 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
), 0);
219 // Program the high register. Then program the low register, setting the
220 // MMCONFIG area size and enabling decoding at once.
222 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH
), PciExBarBase
.Uint32
[1]);
224 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW
),
225 PciExBarBase
.Uint32
[0] | MCH_PCIEXBAR_BUS_FF
| MCH_PCIEXBAR_EN
240 RETURN_STATUS PcdStatus
;
248 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
249 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
250 // S3 resume as well, so we build it unconditionally.)
252 BuildCpuHob (mPhysMemAddressWidth
, 16);
255 // Determine platform type and save Host Bridge DID to PCD
257 switch (mHostBridgeDevId
) {
258 case INTEL_82441_DEVICE_ID
:
259 PmCmd
= POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET
);
260 Pmba
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA
);
261 PmbaAndVal
= ~(UINT32
)PIIX4_PMBA_MASK
;
262 PmbaOrVal
= PIIX4_PMBA_VALUE
;
263 AcpiCtlReg
= POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC
);
264 AcpiEnBit
= PIIX4_PMREGMISC_PMIOSE
;
266 case INTEL_Q35_MCH_DEVICE_ID
:
267 PmCmd
= POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET
);
268 Pmba
= POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE
);
269 PmbaAndVal
= ~(UINT32
)ICH9_PMBASE_MASK
;
270 PmbaOrVal
= ICH9_PMBASE_VALUE
;
271 AcpiCtlReg
= POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL
);
272 AcpiEnBit
= ICH9_ACPI_CNTL_ACPI_EN
;
275 DEBUG ((DEBUG_ERROR
, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
276 __FUNCTION__
, mHostBridgeDevId
));
280 PcdStatus
= PcdSet16S (PcdOvmfHostBridgePciDevId
, mHostBridgeDevId
);
281 ASSERT_RETURN_ERROR (PcdStatus
);
284 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
285 // has been configured (e.g., by Xen) and skip the setup here.
286 // This matches the logic in AcpiTimerLibConstructor ().
288 if ((PciRead8 (AcpiCtlReg
) & AcpiEnBit
) == 0) {
290 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
293 PciAndThenOr32 (Pmba
, PmbaAndVal
, PmbaOrVal
);
296 // 2. set PCICMD/IOSE
298 PciOr8 (PmCmd
, EFI_PCI_COMMAND_IO_SPACE
);
301 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
303 PciOr8 (AcpiCtlReg
, AcpiEnBit
);
306 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
308 // Set Root Complex Register Block BAR
311 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA
),
312 ICH9_ROOT_COMPLEX_BASE
| ICH9_RCBA_EN
316 // Set PCI Express Register Range Base Address
318 PciExBarInitialization ();
324 BootModeInitialization (
330 if (CmosRead8 (0xF) == 0xFE) {
331 mBootMode
= BOOT_ON_S3_RESUME
;
333 CmosWrite8 (0xF, 0x00);
335 Status
= PeiServicesSetBootMode (mBootMode
);
336 ASSERT_EFI_ERROR (Status
);
338 Status
= PeiServicesInstallPpi (mPpiBootMode
);
339 ASSERT_EFI_ERROR (Status
);
344 ReserveEmuVariableNvStore (
347 EFI_PHYSICAL_ADDRESS VariableStore
;
348 RETURN_STATUS PcdStatus
;
351 // Allocate storage for NV variables early on so it will be
352 // at a consistent address. Since VM memory is preserved
353 // across reboots, this allows the NV variable storage to survive
357 (EFI_PHYSICAL_ADDRESS
)(UINTN
)
358 AllocateRuntimePages (
359 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
))
362 "Reserved variable store memory: 0x%lX; size: %dkb\n",
364 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize
)) / 1024
366 PcdStatus
= PcdSet64S (PcdEmuVariableNvStoreReserved
, VariableStore
);
367 ASSERT_RETURN_ERROR (PcdStatus
);
378 DEBUG ((DEBUG_INFO
, "CMOS:\n"));
380 for (Loop
= 0; Loop
< 0x80; Loop
++) {
381 if ((Loop
% 0x10) == 0) {
382 DEBUG ((DEBUG_INFO
, "%02x:", Loop
));
384 DEBUG ((DEBUG_INFO
, " %02x", CmosRead8 (Loop
)));
385 if ((Loop
% 0x10) == 0xf) {
386 DEBUG ((DEBUG_INFO
, "\n"));
394 Perform Platform PEI initialization.
396 @param FileHandle Handle of the file being invoked.
397 @param PeiServices Describes the list of possible PEI Services.
399 @return EFI_SUCCESS The PEIM initialized successfully.
404 InitializeXenPlatform (
405 IN EFI_PEI_FILE_HANDLE FileHandle
,
406 IN CONST EFI_PEI_SERVICES
**PeiServices
409 DEBUG ((DEBUG_INFO
, "Platform PEIM Loaded\n"));
414 DEBUG ((DEBUG_ERROR
, "ERROR: Xen isn't detected\n"));
421 BootModeInitialization ();
422 AddressWidthInitialization ();
425 // Query Host Bridge DID
427 mHostBridgeDevId
= PciRead16 (OVMF_HOSTBRIDGE_DID
);
431 InitializeRamRegions ();
435 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
436 ReserveEmuVariableNvStore ();
437 PeiFvInitialization ();
438 MemMapInitialization ();
441 InstallClearCacheCallback ();
443 MiscInitialization ();