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