]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PlatformInitLib/IntelTdx.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / Library / PlatformInitLib / IntelTdx.c
1 /** @file
2 Initialize Intel TDX support.
3
4 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Base.h>
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/MemoryAllocationLib.h>
17 #include <IndustryStandard/Tdx.h>
18 #include <IndustryStandard/IntelTdx.h>
19 #include <Library/PeiServicesLib.h>
20 #include <Pi/PrePiHob.h>
21 #include <WorkArea.h>
22 #include <ConfidentialComputingGuestAttr.h>
23
24 /**
25 * Build ResourceDescriptorHob for the unaccepted memory region.
26 * This memory region may be splitted into 2 parts because of lazy accept.
27 *
28 * @param Hob Point to the EFI_HOB_RESOURCE_DESCRIPTOR
29 * @return VOID
30 */
31 VOID
32 BuildResourceDescriptorHobForUnacceptedMemory (
33 IN EFI_HOB_RESOURCE_DESCRIPTOR *Hob
34 )
35 {
36 EFI_PHYSICAL_ADDRESS PhysicalStart;
37 EFI_PHYSICAL_ADDRESS PhysicalEnd;
38 UINT64 ResourceLength;
39 EFI_RESOURCE_TYPE ResourceType;
40 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute;
41 UINT64 MaxAcceptedMemoryAddress;
42
43 ASSERT (Hob->ResourceType == BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED);
44
45 ResourceType = BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED;
46 ResourceAttribute = Hob->ResourceAttribute;
47 PhysicalStart = Hob->PhysicalStart;
48 ResourceLength = Hob->ResourceLength;
49 PhysicalEnd = PhysicalStart + ResourceLength;
50
51 //
52 // In the first stage of lazy-accept, all the memory under 4G will be accepted.
53 // The memory above 4G will not be accepted.
54 //
55 MaxAcceptedMemoryAddress = BASE_4GB;
56
57 if (PhysicalEnd <= MaxAcceptedMemoryAddress) {
58 //
59 // This memory region has been accepted.
60 //
61 ResourceType = EFI_RESOURCE_SYSTEM_MEMORY;
62 ResourceAttribute |= (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED);
63 } else if (PhysicalStart >= MaxAcceptedMemoryAddress) {
64 //
65 // This memory region hasn't been accepted.
66 // So keep the ResourceType and ResourceAttribute unchange.
67 //
68 }
69
70 BuildResourceDescriptorHob (
71 ResourceType,
72 ResourceAttribute,
73 PhysicalStart,
74 ResourceLength
75 );
76 }
77
78 /**
79 Transfer the incoming HobList for the TD to the final HobList for Dxe.
80 The Hobs transferred in this function are ResourceDescriptor hob and
81 MemoryAllocation hob.
82
83 @param[in] VmmHobList The Hoblist pass the firmware
84
85 **/
86 VOID
87 EFIAPI
88 TransferTdxHobList (
89 VOID
90 )
91 {
92 EFI_PEI_HOB_POINTERS Hob;
93 EFI_RESOURCE_TYPE ResourceType;
94 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute;
95 VOID *GuidedData;
96
97 //
98 // PcdOvmfSecGhcbBase is used as the TD_HOB in Tdx guest.
99 //
100 Hob.Raw = (UINT8 *)(UINTN)FixedPcdGet32 (PcdOvmfSecGhcbBase);
101 while (!END_OF_HOB_LIST (Hob)) {
102 switch (Hob.Header->HobType) {
103 case EFI_HOB_TYPE_RESOURCE_DESCRIPTOR:
104 ResourceType = Hob.ResourceDescriptor->ResourceType;
105 ResourceAttribute = Hob.ResourceDescriptor->ResourceAttribute;
106
107 if (ResourceType == BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED) {
108 BuildResourceDescriptorHobForUnacceptedMemory (Hob.ResourceDescriptor);
109 } else {
110 BuildResourceDescriptorHob (
111 ResourceType,
112 ResourceAttribute,
113 Hob.ResourceDescriptor->PhysicalStart,
114 Hob.ResourceDescriptor->ResourceLength
115 );
116 }
117
118 break;
119 case EFI_HOB_TYPE_MEMORY_ALLOCATION:
120 BuildMemoryAllocationHob (
121 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress,
122 Hob.MemoryAllocation->AllocDescriptor.MemoryLength,
123 Hob.MemoryAllocation->AllocDescriptor.MemoryType
124 );
125 break;
126 case EFI_HOB_TYPE_GUID_EXTENSION:
127 GuidedData = (VOID *)(&Hob.Guid->Name + 1);
128 BuildGuidDataHob (&Hob.Guid->Name, GuidedData, Hob.Guid->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));
129 break;
130 }
131
132 Hob.Raw = GET_NEXT_HOB (Hob);
133 }
134 }
135
136 /**
137 In Tdx guest, the system memory is passed in TdHob by host VMM. So
138 the major task of PlatformTdxPublishRamRegions is to walk thru the
139 TdHob list and transfer the ResourceDescriptorHob and MemoryAllocationHob
140 to the hobs in DXE phase.
141
142 MemoryAllocationHob should also be created for Mailbox and Ovmf work area.
143 **/
144 VOID
145 EFIAPI
146 PlatformTdxPublishRamRegions (
147 VOID
148 )
149 {
150 if (!TdIsEnabled ()) {
151 return;
152 }
153
154 TransferTdxHobList ();
155
156 //
157 // The memory region defined by PcdOvmfSecGhcbBackupBase is pre-allocated by
158 // host VMM and used as the td mailbox at the beginning of system boot.
159 //
160 BuildMemoryAllocationHob (
161 FixedPcdGet32 (PcdOvmfSecGhcbBackupBase),
162 FixedPcdGet32 (PcdOvmfSecGhcbBackupSize),
163 EfiACPIMemoryNVS
164 );
165
166 if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {
167 //
168 // Reserve the work area.
169 //
170 // Since this memory range will be used by the Reset Vector on S3
171 // resume, it must be reserved as ACPI NVS.
172 //
173 // If S3 is unsupported, then various drivers might still write to the
174 // work area. We ought to prevent DXE from serving allocation requests
175 // such that they would overlap the work area.
176 //
177 BuildMemoryAllocationHob (
178 (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),
179 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),
180 EfiBootServicesData
181 );
182 }
183 }