]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PeilessStartupLib/Hob.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / Library / PeilessStartupLib / Hob.c
1 /** @file
2 Main SEC phase code. Handles initial TDX Hob List Processing
3
4 Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiPei.h>
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/PciLib.h>
17 #include <Library/PrePiLib.h>
18 #include <Library/QemuFwCfgLib.h>
19 #include <IndustryStandard/Tdx.h>
20 #include <IndustryStandard/UefiTcgPlatform.h>
21 #include <Library/PlatformInitLib.h>
22 #include <OvmfPlatforms.h>
23 #include "PeilessStartupInternal.h"
24
25 #define EFI_RESOURCE_MEMORY_UNACCEPTED 7
26
27 /**
28 * Construct the HobList in SEC phase.
29 *
30 * @return EFI_SUCCESS Successfully construct the firmware hoblist.
31 * @return EFI_NOT_FOUND Cannot find a memory region to be the fw hoblist.
32 */
33 EFI_STATUS
34 EFIAPI
35 ConstructSecHobList (
36 )
37 {
38 UINT32 LowMemorySize;
39 UINT32 LowMemoryStart;
40
41 EFI_HOB_HANDOFF_INFO_TABLE *HobList;
42 EFI_HOB_PLATFORM_INFO PlatformInfoHob;
43
44 ZeroMem (&PlatformInfoHob, sizeof (PlatformInfoHob));
45 PlatformInfoHob.HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
46 LowMemorySize = PlatformGetSystemMemorySizeBelow4gb (&PlatformInfoHob);
47 ASSERT (LowMemorySize != 0);
48 LowMemoryStart = FixedPcdGet32 (PcdOvmfDxeMemFvBase) + FixedPcdGet32 (PcdOvmfDxeMemFvSize);
49 LowMemorySize -= LowMemoryStart;
50
51 DEBUG ((DEBUG_INFO, "LowMemory Start and End: %x, %x\n", LowMemoryStart, LowMemoryStart + LowMemorySize));
52 HobList = HobConstructor (
53 (VOID *)(UINTN)LowMemoryStart,
54 LowMemorySize,
55 (VOID *)(UINTN)LowMemoryStart,
56 (VOID *)(UINTN)(LowMemoryStart + LowMemorySize)
57 );
58
59 SetHobList ((VOID *)(UINT64)HobList);
60
61 return EFI_SUCCESS;
62 }
63
64 /**
65 * This function is to find a memory region which is the largest one below 4GB.
66 * It will be used as the firmware hoblist.
67 *
68 * @param VmmHobList Vmm passed hoblist which constains the memory information.
69 * @return EFI_SUCCESS Successfully construct the firmware hoblist.
70 */
71 EFI_STATUS
72 EFIAPI
73 ConstructFwHobList (
74 IN CONST VOID *VmmHobList
75 )
76 {
77 EFI_PEI_HOB_POINTERS Hob;
78 EFI_PHYSICAL_ADDRESS PhysicalEnd;
79 UINT64 ResourceLength;
80 EFI_PHYSICAL_ADDRESS LowMemoryStart;
81 UINT64 LowMemoryLength;
82
83 ASSERT (VmmHobList != NULL);
84
85 Hob.Raw = (UINT8 *)VmmHobList;
86
87 LowMemoryLength = 0;
88 LowMemoryStart = 0;
89
90 //
91 // Parse the HOB list until end of list or matching type is found.
92 //
93 while (!END_OF_HOB_LIST (Hob)) {
94 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
95 if (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_UNACCEPTED) {
96 PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;
97 ResourceLength = Hob.ResourceDescriptor->ResourceLength;
98
99 if (PhysicalEnd <= BASE_4GB) {
100 if (ResourceLength > LowMemoryLength) {
101 LowMemoryStart = Hob.ResourceDescriptor->PhysicalStart;
102 LowMemoryLength = ResourceLength;
103 }
104 } else {
105 break;
106 }
107 }
108 }
109
110 Hob.Raw = GET_NEXT_HOB (Hob);
111 }
112
113 if (LowMemoryLength == 0) {
114 DEBUG ((DEBUG_ERROR, "Cannot find a memory region under 4GB for Fw hoblist.\n"));
115 return EFI_NOT_FOUND;
116 }
117
118 //
119 // HobLib doesn't like HobStart at address 0 so adjust is needed
120 //
121 if (LowMemoryStart == 0) {
122 LowMemoryStart += EFI_PAGE_SIZE;
123 LowMemoryLength -= EFI_PAGE_SIZE;
124 }
125
126 DEBUG ((DEBUG_INFO, "LowMemory Start and End: %x, %x\n", LowMemoryStart, LowMemoryStart + LowMemoryLength));
127 HobConstructor (
128 (VOID *)LowMemoryStart,
129 LowMemoryLength,
130 (VOID *)LowMemoryStart,
131 (VOID *)(LowMemoryStart + LowMemoryLength)
132 );
133
134 SetHobList ((VOID *)(UINT64)LowMemoryStart);
135
136 return EFI_SUCCESS;
137 }