]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
MdeModulePkg/MemoryProtection: split protect and unprotect paths
[mirror_edk2.git] / UefiCpuPkg / CpuS3DataDxe / CpuS3Data.c
CommitLineData
bfec5efa
MK
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
65b24ada 12Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
bfec5efa
MK
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\r
35#include <Protocol/MpService.h>\r
36#include <Guid/EventGroup.h>\r
37\r
38//\r
39// Data structure used to allocate ACPI_CPU_DATA and its supporting structures\r
40//\r
41typedef struct {\r
42 ACPI_CPU_DATA AcpiCpuData;\r
43 MTRR_SETTINGS MtrrTable;\r
44 IA32_DESCRIPTOR GdtrProfile;\r
45 IA32_DESCRIPTOR IdtrProfile;\r
46} ACPI_CPU_DATA_EX;\r
47\r
48/**\r
49 Allocate EfiACPIMemoryNVS below 4G memory address.\r
50\r
51 This function allocates EfiACPIMemoryNVS below 4G memory address.\r
52\r
53 @param[in] Size Size of memory to allocate.\r
54\r
55 @return Allocated address for output.\r
56\r
57**/\r
58VOID *\r
59AllocateAcpiNvsMemoryBelow4G (\r
60 IN UINTN Size\r
61 )\r
62{\r
63 EFI_PHYSICAL_ADDRESS Address;\r
64 EFI_STATUS Status;\r
65 VOID *Buffer;\r
66\r
67 Address = BASE_4GB - 1;\r
68 Status = gBS->AllocatePages (\r
69 AllocateMaxAddress,\r
70 EfiACPIMemoryNVS,\r
71 EFI_SIZE_TO_PAGES (Size),\r
72 &Address\r
73 );\r
74 if (EFI_ERROR (Status)) {\r
75 return NULL;\r
76 }\r
77\r
78 Buffer = (VOID *)(UINTN)Address;\r
79 ZeroMem (Buffer, Size);\r
80\r
81 return Buffer;\r
82}\r
83\r
84/**\r
85 Callback function executed when the EndOfDxe event group is signaled.\r
86\r
65b24ada 87 We delay allocating StartupVector and saving the MTRR settings until BDS signals EndOfDxe.\r
bfec5efa
MK
88\r
89 @param[in] Event Event whose notification function is being invoked.\r
90 @param[out] Context Pointer to the MTRR_SETTINGS buffer to fill in.\r
91**/\r
92VOID\r
93EFIAPI\r
65b24ada 94CpuS3DataOnEndOfDxe (\r
bfec5efa
MK
95 IN EFI_EVENT Event,\r
96 OUT VOID *Context\r
97 )\r
98{\r
65b24ada
JF
99 EFI_STATUS Status;\r
100 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
101\r
102 AcpiCpuDataEx = (ACPI_CPU_DATA_EX *) Context;\r
103 //\r
104 // Allocate a 4KB reserved page below 1MB\r
105 //\r
106 AcpiCpuDataEx->AcpiCpuData.StartupVector = BASE_1MB - 1;\r
107 Status = gBS->AllocatePages (\r
108 AllocateMaxAddress,\r
109 EfiReservedMemoryType,\r
110 1,\r
111 &AcpiCpuDataEx->AcpiCpuData.StartupVector\r
112 );\r
113 ASSERT_EFI_ERROR (Status);\r
114\r
bfec5efa 115 DEBUG ((EFI_D_VERBOSE, "%a\n", __FUNCTION__));\r
65b24ada 116 MtrrGetAllMtrrs (&AcpiCpuDataEx->MtrrTable);\r
bfec5efa
MK
117\r
118 //\r
119 // Close event, so it will not be invoked again.\r
120 //\r
121 gBS->CloseEvent (Event);\r
122}\r
123\r
124/**\r
125 The entry function of the CpuS3Data driver.\r
126\r
127 Allocate and initialize all fields of the ACPI_CPU_DATA structure except the\r
128 MTRR settings. Register an event notification on gEfiEndOfDxeEventGroupGuid\r
129 to capture the ACPI_CPU_DATA MTRR settings. The PcdCpuS3DataAddress is set\r
130 to the address that ACPI_CPU_DATA is allocated at.\r
131\r
132 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
133 @param[in] SystemTable A pointer to the EFI System Table.\r
134\r
135 @retval EFI_SUCCESS The entry point is executed successfully.\r
ca98f603 136 @retval EFI_UNSUPPORTED Do not support ACPI S3.\r
bfec5efa
MK
137 @retval other Some error occurs when executing this entry point.\r
138\r
139**/\r
140EFI_STATUS\r
141EFIAPI\r
142CpuS3DataInitialize (\r
143 IN EFI_HANDLE ImageHandle,\r
144 IN EFI_SYSTEM_TABLE *SystemTable\r
145 )\r
146{\r
147 EFI_STATUS Status;\r
148 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
149 ACPI_CPU_DATA *AcpiCpuData;\r
150 EFI_MP_SERVICES_PROTOCOL *MpServices;\r
151 UINTN NumberOfCpus;\r
152 UINTN NumberOfEnabledProcessors;\r
153 VOID *Stack;\r
154 UINTN TableSize;\r
155 CPU_REGISTER_TABLE *RegisterTable;\r
156 UINTN Index;\r
157 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
158 UINTN GdtSize;\r
159 UINTN IdtSize;\r
160 VOID *Gdt;\r
161 VOID *Idt;\r
162 EFI_EVENT Event;\r
163\r
ca98f603
SZ
164 if (!PcdGetBool (PcdAcpiS3Enable)) {\r
165 return EFI_UNSUPPORTED;\r
166 }\r
167\r
bfec5efa
MK
168 //\r
169 // Allocate ACPI NVS memory below 4G memory for use on ACPI S3 resume.\r
170 //\r
171 AcpiCpuDataEx = AllocateAcpiNvsMemoryBelow4G (sizeof (ACPI_CPU_DATA_EX));\r
172 ASSERT (AcpiCpuDataEx != NULL);\r
173 AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;\r
174\r
175 //\r
176 // Get MP Services Protocol\r
177 //\r
178 Status = gBS->LocateProtocol (\r
179 &gEfiMpServiceProtocolGuid,\r
180 NULL,\r
181 (VOID **)&MpServices\r
182 );\r
183 ASSERT_EFI_ERROR (Status);\r
184\r
bfec5efa
MK
185 //\r
186 // Get the number of CPUs\r
187 //\r
188 Status = MpServices->GetNumberOfProcessors (\r
189 MpServices,\r
190 &NumberOfCpus,\r
191 &NumberOfEnabledProcessors\r
192 );\r
193 ASSERT_EFI_ERROR (Status);\r
194 AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;\r
195\r
196 //\r
197 // Initialize ACPI_CPU_DATA fields\r
198 //\r
199 AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);\r
200 AcpiCpuData->ApMachineCheckHandlerBase = 0;\r
201 AcpiCpuData->ApMachineCheckHandlerSize = 0;\r
202 AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;\r
203 AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;\r
204 AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;\r
205\r
206 //\r
207 // Allocate stack space for all CPUs\r
208 //\r
209 Stack = AllocateAcpiNvsMemoryBelow4G (NumberOfCpus * AcpiCpuData->StackSize);\r
210 ASSERT (Stack != NULL);\r
211 AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;\r
212\r
213 //\r
214 // Get the boot processor's GDT and IDT\r
215 //\r
216 AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);\r
217 AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);\r
218\r
219 //\r
220 // Allocate GDT and IDT in ACPI NVS and copy current GDT and IDT contents\r
221 //\r
222 GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;\r
223 IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;\r
224 Gdt = AllocateAcpiNvsMemoryBelow4G (GdtSize + IdtSize);\r
225 ASSERT (Gdt != NULL);\r
226 Idt = (VOID *)((UINTN)Gdt + GdtSize);\r
227 CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);\r
228 CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);\r
229 AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;\r
230 AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;\r
231\r
232 //\r
233 // Allocate buffer for empty RegisterTable and PreSmmInitRegisterTable for all CPUs\r
234 //\r
235 TableSize = 2 * NumberOfCpus * sizeof (CPU_REGISTER_TABLE);\r
236 RegisterTable = (CPU_REGISTER_TABLE *)AllocateAcpiNvsMemoryBelow4G (TableSize);\r
237 ASSERT (RegisterTable != NULL);\r
238 for (Index = 0; Index < NumberOfCpus; Index++) {\r
239 Status = MpServices->GetProcessorInfo (\r
240 MpServices,\r
241 Index,\r
242 &ProcessorInfoBuffer\r
243 );\r
244 ASSERT_EFI_ERROR (Status);\r
245\r
246 RegisterTable[Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
247 RegisterTable[Index].TableLength = 0;\r
248 RegisterTable[Index].AllocatedSize = 0;\r
249 RegisterTable[Index].RegisterTableEntry = NULL;\r
250\r
251 RegisterTable[NumberOfCpus + Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
252 RegisterTable[NumberOfCpus + Index].TableLength = 0;\r
253 RegisterTable[NumberOfCpus + Index].AllocatedSize = 0;\r
254 RegisterTable[NumberOfCpus + Index].RegisterTableEntry = NULL;\r
255 }\r
256 AcpiCpuData->RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTable;\r
257 AcpiCpuData->PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)(RegisterTable + NumberOfCpus);\r
258\r
259 //\r
260 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure\r
261 //\r
262 Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);\r
263 ASSERT_EFI_ERROR (Status);\r
264\r
265 //\r
266 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.\r
65b24ada 267 // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA\r
bfec5efa
MK
268 //\r
269 Status = gBS->CreateEventEx (\r
270 EVT_NOTIFY_SIGNAL,\r
271 TPL_CALLBACK,\r
65b24ada
JF
272 CpuS3DataOnEndOfDxe,\r
273 AcpiCpuData,\r
bfec5efa
MK
274 &gEfiEndOfDxeEventGroupGuid,\r
275 &Event\r
276 );\r
277 ASSERT_EFI_ERROR (Status);\r
278\r
279 return EFI_SUCCESS;\r
280}\r