]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/CpuS3DataDxe/CpuS3Data.c
5ffe1f3cd74e49f6e052bc2d6d628f0ae409db03
[mirror_edk2.git] / OvmfPkg / CpuS3DataDxe / CpuS3Data.c
1 /** @file
2 ACPI CPU Data initialization module
3
4 This module initializes the ACPI_CPU_DATA structure and registers the address
5 of this structure in the PcdCpuS3DataAddress PCD. This is a generic/simple
6 version of this module. It does not provide a machine check handler or CPU
7 register initialization tables for ACPI S3 resume. It also only supports the
8 number of CPUs reported by the MP Services Protocol, so this module does not
9 support hot plug CPUs. This module can be copied into a CPU specific package
10 and customized if these additional features are required.
11
12 Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
13 Copyright (c) 2015 - 2020, Red Hat, Inc.
14
15 SPDX-License-Identifier: BSD-2-Clause-Patent
16
17 **/
18
19 #include <PiDxe.h>
20
21 #include <AcpiCpuData.h>
22
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/MtrrLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29
30 #include <Protocol/MpService.h>
31 #include <Guid/EventGroup.h>
32
33 //
34 // Data structure used to allocate ACPI_CPU_DATA and its supporting structures
35 //
36 typedef struct {
37 ACPI_CPU_DATA AcpiCpuData;
38 MTRR_SETTINGS MtrrTable;
39 IA32_DESCRIPTOR GdtrProfile;
40 IA32_DESCRIPTOR IdtrProfile;
41 } ACPI_CPU_DATA_EX;
42
43 /**
44 Allocate EfiACPIMemoryNVS memory.
45
46 @param[in] Size Size of memory to allocate.
47
48 @return Allocated address for output.
49
50 **/
51 VOID *
52 AllocateAcpiNvsMemory (
53 IN UINTN Size
54 )
55 {
56 EFI_PHYSICAL_ADDRESS Address;
57 EFI_STATUS Status;
58 VOID *Buffer;
59
60 Status = gBS->AllocatePages (
61 AllocateAnyPages,
62 EfiACPIMemoryNVS,
63 EFI_SIZE_TO_PAGES (Size),
64 &Address
65 );
66 if (EFI_ERROR (Status)) {
67 return NULL;
68 }
69
70 Buffer = (VOID *)(UINTN)Address;
71 ZeroMem (Buffer, Size);
72
73 return Buffer;
74 }
75
76 /**
77 Allocate memory and clean it with zero.
78
79 @param[in] Size Size of memory to allocate.
80
81 @return Allocated address for output.
82
83 **/
84 VOID *
85 AllocateZeroPages (
86 IN UINTN Size
87 )
88 {
89 VOID *Buffer;
90
91 Buffer = AllocatePages (EFI_SIZE_TO_PAGES (Size));
92 if (Buffer != NULL) {
93 ZeroMem (Buffer, Size);
94 }
95
96 return Buffer;
97 }
98 /**
99 Callback function executed when the EndOfDxe event group is signaled.
100
101 We delay allocating StartupVector and saving the MTRR settings until BDS signals EndOfDxe.
102
103 @param[in] Event Event whose notification function is being invoked.
104 @param[out] Context Pointer to the MTRR_SETTINGS buffer to fill in.
105 **/
106 VOID
107 EFIAPI
108 CpuS3DataOnEndOfDxe (
109 IN EFI_EVENT Event,
110 OUT VOID *Context
111 )
112 {
113 EFI_STATUS Status;
114 ACPI_CPU_DATA_EX *AcpiCpuDataEx;
115
116 AcpiCpuDataEx = (ACPI_CPU_DATA_EX *) Context;
117 //
118 // Allocate a 4KB reserved page below 1MB
119 //
120 AcpiCpuDataEx->AcpiCpuData.StartupVector = BASE_1MB - 1;
121 Status = gBS->AllocatePages (
122 AllocateMaxAddress,
123 EfiReservedMemoryType,
124 1,
125 &AcpiCpuDataEx->AcpiCpuData.StartupVector
126 );
127 ASSERT_EFI_ERROR (Status);
128
129 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
130 MtrrGetAllMtrrs (&AcpiCpuDataEx->MtrrTable);
131
132 //
133 // Close event, so it will not be invoked again.
134 //
135 gBS->CloseEvent (Event);
136 }
137
138 /**
139 The entry function of the CpuS3Data driver.
140
141 Allocate and initialize all fields of the ACPI_CPU_DATA structure except the
142 MTRR settings. Register an event notification on gEfiEndOfDxeEventGroupGuid
143 to capture the ACPI_CPU_DATA MTRR settings. The PcdCpuS3DataAddress is set
144 to the address that ACPI_CPU_DATA is allocated at.
145
146 @param[in] ImageHandle The firmware allocated handle for the EFI image.
147 @param[in] SystemTable A pointer to the EFI System Table.
148
149 @retval EFI_SUCCESS The entry point is executed successfully.
150 @retval EFI_UNSUPPORTED Do not support ACPI S3.
151 @retval other Some error occurs when executing this entry point.
152
153 **/
154 EFI_STATUS
155 EFIAPI
156 CpuS3DataInitialize (
157 IN EFI_HANDLE ImageHandle,
158 IN EFI_SYSTEM_TABLE *SystemTable
159 )
160 {
161 EFI_STATUS Status;
162 ACPI_CPU_DATA_EX *AcpiCpuDataEx;
163 ACPI_CPU_DATA *AcpiCpuData;
164 EFI_MP_SERVICES_PROTOCOL *MpServices;
165 UINTN NumberOfCpus;
166 VOID *Stack;
167 UINTN GdtSize;
168 UINTN IdtSize;
169 VOID *Gdt;
170 VOID *Idt;
171 EFI_EVENT Event;
172 ACPI_CPU_DATA *OldAcpiCpuData;
173
174 if (!PcdGetBool (PcdAcpiS3Enable)) {
175 return EFI_UNSUPPORTED;
176 }
177
178 //
179 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure
180 //
181 OldAcpiCpuData = (ACPI_CPU_DATA *) (UINTN) PcdGet64 (PcdCpuS3DataAddress);
182
183 AcpiCpuDataEx = AllocateZeroPages (sizeof (ACPI_CPU_DATA_EX));
184 ASSERT (AcpiCpuDataEx != NULL);
185 AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;
186
187 if (PcdGetBool (PcdQ35SmramAtDefaultSmbase)) {
188 NumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
189 } else {
190 UINTN NumberOfEnabledProcessors;
191
192 //
193 // Get MP Services Protocol
194 //
195 Status = gBS->LocateProtocol (
196 &gEfiMpServiceProtocolGuid,
197 NULL,
198 (VOID **)&MpServices
199 );
200 ASSERT_EFI_ERROR (Status);
201
202 //
203 // Get the number of CPUs
204 //
205 Status = MpServices->GetNumberOfProcessors (
206 MpServices,
207 &NumberOfCpus,
208 &NumberOfEnabledProcessors
209 );
210 ASSERT_EFI_ERROR (Status);
211 }
212 AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;
213
214 //
215 // Initialize ACPI_CPU_DATA fields
216 //
217 AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);
218 AcpiCpuData->ApMachineCheckHandlerBase = 0;
219 AcpiCpuData->ApMachineCheckHandlerSize = 0;
220 AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;
221 AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;
222 AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;
223
224 //
225 // Allocate stack space for all CPUs.
226 // Use ACPI NVS memory type because this data will be directly used by APs
227 // in S3 resume phase in long mode. Also during S3 resume, the stack buffer
228 // will only be used as scratch space. i.e. we won't read anything from it
229 // before we write to it, in PiSmmCpuDxeSmm.
230 //
231 Stack = AllocateAcpiNvsMemory (NumberOfCpus * AcpiCpuData->StackSize);
232 ASSERT (Stack != NULL);
233 AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;
234
235 //
236 // Get the boot processor's GDT and IDT
237 //
238 AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);
239 AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);
240
241 //
242 // Allocate GDT and IDT and copy current GDT and IDT contents
243 //
244 GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;
245 IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;
246 Gdt = AllocateZeroPages (GdtSize + IdtSize);
247 ASSERT (Gdt != NULL);
248 Idt = (VOID *)((UINTN)Gdt + GdtSize);
249 CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);
250 CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);
251 AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;
252 AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;
253
254 if (OldAcpiCpuData != NULL) {
255 AcpiCpuData->RegisterTable = OldAcpiCpuData->RegisterTable;
256 AcpiCpuData->PreSmmInitRegisterTable = OldAcpiCpuData->PreSmmInitRegisterTable;
257 AcpiCpuData->ApLocation = OldAcpiCpuData->ApLocation;
258 CopyMem (&AcpiCpuData->CpuStatus, &OldAcpiCpuData->CpuStatus, sizeof (CPU_STATUS_INFORMATION));
259 }
260
261 //
262 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure
263 //
264 Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);
265 ASSERT_EFI_ERROR (Status);
266
267 //
268 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
269 // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA
270 //
271 Status = gBS->CreateEventEx (
272 EVT_NOTIFY_SIGNAL,
273 TPL_CALLBACK,
274 CpuS3DataOnEndOfDxe,
275 AcpiCpuData,
276 &gEfiEndOfDxeEventGroupGuid,
277 &Event
278 );
279 ASSERT_EFI_ERROR (Status);
280
281 return EFI_SUCCESS;
282 }