]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenPlatformPei/Platform.c
OvmfPkg/XenPlatformPei: Rework memory detection
[mirror_edk2.git] / OvmfPkg / XenPlatformPei / Platform.c
1 /**@file
2 Platform PEI driver
3
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.
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 //
13 // The package level header files this module uses
14 //
15 #include <PiPei.h>
16
17 //
18 // The Library classes this module consumes
19 //
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>
34
35 #include "Platform.h"
36 #include "Cmos.h"
37
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 }
47 };
48
49
50 EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {
51 {
52 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
53 &gEfiPeiMasterBootModePpiGuid,
54 NULL
55 }
56 };
57
58
59 UINT16 mHostBridgeDevId;
60
61 EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;
62
63
64 VOID
65 AddIoMemoryBaseSizeHob (
66 EFI_PHYSICAL_ADDRESS MemoryBase,
67 UINT64 MemorySize
68 )
69 {
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,
76 MemoryBase,
77 MemorySize
78 );
79 }
80
81 VOID
82 AddReservedMemoryBaseSizeHob (
83 EFI_PHYSICAL_ADDRESS MemoryBase,
84 UINT64 MemorySize,
85 BOOLEAN Cacheable
86 )
87 {
88 BuildResourceDescriptorHob (
89 EFI_RESOURCE_MEMORY_RESERVED,
90 EFI_RESOURCE_ATTRIBUTE_PRESENT |
91 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
92 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
93 (Cacheable ?
94 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
95 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
96 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :
97 0
98 ) |
99 EFI_RESOURCE_ATTRIBUTE_TESTED,
100 MemoryBase,
101 MemorySize
102 );
103 }
104
105 VOID
106 AddReservedMemoryRangeHob (
107 EFI_PHYSICAL_ADDRESS MemoryBase,
108 EFI_PHYSICAL_ADDRESS MemoryLimit,
109 BOOLEAN Cacheable
110 )
111 {
112 AddReservedMemoryBaseSizeHob (MemoryBase,
113 (UINT64)(MemoryLimit - MemoryBase), Cacheable);
114 }
115
116 VOID
117 AddIoMemoryRangeHob (
118 EFI_PHYSICAL_ADDRESS MemoryBase,
119 EFI_PHYSICAL_ADDRESS MemoryLimit
120 )
121 {
122 AddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));
123 }
124
125
126 VOID
127 AddMemoryBaseSizeHob (
128 EFI_PHYSICAL_ADDRESS MemoryBase,
129 UINT64 MemorySize
130 )
131 {
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,
141 MemoryBase,
142 MemorySize
143 );
144 }
145
146
147 VOID
148 AddMemoryRangeHob (
149 EFI_PHYSICAL_ADDRESS MemoryBase,
150 EFI_PHYSICAL_ADDRESS MemoryLimit
151 )
152 {
153 AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));
154 }
155
156
157 VOID
158 MemMapInitialization (
159 VOID
160 )
161 {
162 UINT64 PciIoBase;
163 UINT64 PciIoSize;
164 RETURN_STATUS PcdStatus;
165
166 PciIoBase = 0xC000;
167 PciIoSize = 0x4000;
168
169 //
170 // Create Memory Type Information HOB
171 //
172 BuildGuidDataHob (
173 &gEfiMemoryTypeInformationGuid,
174 mDefaultMemoryTypeInformation,
175 sizeof(mDefaultMemoryTypeInformation)
176 );
177
178 //
179 // Video memory + Legacy BIOS region
180 //
181 AddIoMemoryRangeHob (0x0A0000, BASE_1MB);
182
183 //
184 // Add PCI IO Port space available for PCI resource allocations.
185 //
186 BuildResourceDescriptorHob (
187 EFI_RESOURCE_IO,
188 EFI_RESOURCE_ATTRIBUTE_PRESENT |
189 EFI_RESOURCE_ATTRIBUTE_INITIALIZED,
190 PciIoBase,
191 PciIoSize
192 );
193 PcdStatus = PcdSet64S (PcdPciIoBase, PciIoBase);
194 ASSERT_RETURN_ERROR (PcdStatus);
195 PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);
196 ASSERT_RETURN_ERROR (PcdStatus);
197 }
198
199 VOID
200 PciExBarInitialization (
201 VOID
202 )
203 {
204 union {
205 UINT64 Uint64;
206 UINT32 Uint32[2];
207 } PciExBarBase;
208
209 //
210 // We only support the 256MB size for the MMCONFIG area:
211 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.
212 //
213 // The masks used below enforce the Q35 requirements that the MMCONFIG area
214 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.
215 //
216 // Note that (b) also ensures that the minimum address width we have
217 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice
218 // for DXE's page tables to cover the MMCONFIG area.
219 //
220 PciExBarBase.Uint64 = FixedPcdGet64 (PcdPciExpressBaseAddress);
221 ASSERT ((PciExBarBase.Uint32[1] & MCH_PCIEXBAR_HIGHMASK) == 0);
222 ASSERT ((PciExBarBase.Uint32[0] & MCH_PCIEXBAR_LOWMASK) == 0);
223
224 //
225 // Clear the PCIEXBAREN bit first, before programming the high register.
226 //
227 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0);
228
229 //
230 // Program the high register. Then program the low register, setting the
231 // MMCONFIG area size and enabling decoding at once.
232 //
233 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[1]);
234 PciWrite32 (
235 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW),
236 PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN
237 );
238 }
239
240 VOID
241 MiscInitialization (
242 VOID
243 )
244 {
245 UINTN PmCmd;
246 UINTN Pmba;
247 UINT32 PmbaAndVal;
248 UINT32 PmbaOrVal;
249 UINTN AcpiCtlReg;
250 UINT8 AcpiEnBit;
251 RETURN_STATUS PcdStatus;
252
253 //
254 // Disable A20 Mask
255 //
256 IoOr8 (0x92, BIT1);
257
258 //
259 // Build the CPU HOB with guest RAM size dependent address width and 16-bits
260 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during
261 // S3 resume as well, so we build it unconditionally.)
262 //
263 BuildCpuHob (mPhysMemAddressWidth, 16);
264
265 //
266 // Determine platform type and save Host Bridge DID to PCD
267 //
268 switch (mHostBridgeDevId) {
269 case INTEL_82441_DEVICE_ID:
270 PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);
271 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);
272 PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;
273 PmbaOrVal = PIIX4_PMBA_VALUE;
274 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);
275 AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;
276 break;
277 case INTEL_Q35_MCH_DEVICE_ID:
278 PmCmd = POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET);
279 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);
280 PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;
281 PmbaOrVal = ICH9_PMBASE_VALUE;
282 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);
283 AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;
284 break;
285 default:
286 DEBUG ((DEBUG_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
287 __FUNCTION__, mHostBridgeDevId));
288 ASSERT (FALSE);
289 return;
290 }
291 PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);
292 ASSERT_RETURN_ERROR (PcdStatus);
293
294 //
295 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA
296 // has been configured (e.g., by Xen) and skip the setup here.
297 // This matches the logic in AcpiTimerLibConstructor ().
298 //
299 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {
300 //
301 // The PEI phase should be exited with fully accessibe ACPI PM IO space:
302 // 1. set PMBA
303 //
304 PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);
305
306 //
307 // 2. set PCICMD/IOSE
308 //
309 PciOr8 (PmCmd, EFI_PCI_COMMAND_IO_SPACE);
310
311 //
312 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)
313 //
314 PciOr8 (AcpiCtlReg, AcpiEnBit);
315 }
316
317 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {
318 //
319 // Set Root Complex Register Block BAR
320 //
321 PciWrite32 (
322 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA),
323 ICH9_ROOT_COMPLEX_BASE | ICH9_RCBA_EN
324 );
325
326 //
327 // Set PCI Express Register Range Base Address
328 //
329 PciExBarInitialization ();
330 }
331 }
332
333
334 VOID
335 BootModeInitialization (
336 VOID
337 )
338 {
339 EFI_STATUS Status;
340
341 if (CmosRead8 (0xF) == 0xFE) {
342 mBootMode = BOOT_ON_S3_RESUME;
343 }
344 CmosWrite8 (0xF, 0x00);
345
346 Status = PeiServicesSetBootMode (mBootMode);
347 ASSERT_EFI_ERROR (Status);
348
349 Status = PeiServicesInstallPpi (mPpiBootMode);
350 ASSERT_EFI_ERROR (Status);
351 }
352
353
354 VOID
355 ReserveEmuVariableNvStore (
356 )
357 {
358 EFI_PHYSICAL_ADDRESS VariableStore;
359 RETURN_STATUS PcdStatus;
360
361 //
362 // Allocate storage for NV variables early on so it will be
363 // at a consistent address. Since VM memory is preserved
364 // across reboots, this allows the NV variable storage to survive
365 // a VM reboot.
366 //
367 VariableStore =
368 (EFI_PHYSICAL_ADDRESS)(UINTN)
369 AllocateRuntimePages (
370 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))
371 );
372 DEBUG ((DEBUG_INFO,
373 "Reserved variable store memory: 0x%lX; size: %dkb\n",
374 VariableStore,
375 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024
376 ));
377 PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
378 ASSERT_RETURN_ERROR (PcdStatus);
379 }
380
381
382 VOID
383 DebugDumpCmos (
384 VOID
385 )
386 {
387 UINT32 Loop;
388
389 DEBUG ((DEBUG_INFO, "CMOS:\n"));
390
391 for (Loop = 0; Loop < 0x80; Loop++) {
392 if ((Loop % 0x10) == 0) {
393 DEBUG ((DEBUG_INFO, "%02x:", Loop));
394 }
395 DEBUG ((DEBUG_INFO, " %02x", CmosRead8 (Loop)));
396 if ((Loop % 0x10) == 0xf) {
397 DEBUG ((DEBUG_INFO, "\n"));
398 }
399 }
400 }
401
402
403
404 /**
405 Perform Platform PEI initialization.
406
407 @param FileHandle Handle of the file being invoked.
408 @param PeiServices Describes the list of possible PEI Services.
409
410 @return EFI_SUCCESS The PEIM initialized successfully.
411
412 **/
413 EFI_STATUS
414 EFIAPI
415 InitializeXenPlatform (
416 IN EFI_PEI_FILE_HANDLE FileHandle,
417 IN CONST EFI_PEI_SERVICES **PeiServices
418 )
419 {
420 DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));
421
422 DebugDumpCmos ();
423
424 if (!XenDetect ()) {
425 DEBUG ((DEBUG_ERROR, "ERROR: Xen isn't detected\n"));
426 ASSERT (FALSE);
427 CpuDeadLoop ();
428 }
429
430 XenConnect ();
431
432 BootModeInitialization ();
433 AddressWidthInitialization ();
434
435 //
436 // Query Host Bridge DID
437 //
438 mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
439
440 PublishPeiMemory ();
441
442 InitializeRamRegions ();
443
444 InitializeXen ();
445
446 if (mBootMode != BOOT_ON_S3_RESUME) {
447 ReserveEmuVariableNvStore ();
448 PeiFvInitialization ();
449 MemMapInitialization ();
450 }
451
452 InstallClearCacheCallback ();
453 AmdSevInitialize ();
454 MiscInitialization ();
455
456 return EFI_SUCCESS;
457 }