]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
UefiCpuPkg/MpInitLib: Add microcode definitions defined in IA32 SDM
[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
12Copyright (c) 2013 - 2015, 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\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
87 We delay saving the MTRR settings until BDS signals EndOfDxe.\r
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
94SaveMtrrsOnEndOfDxe (\r
95 IN EFI_EVENT Event,\r
96 OUT VOID *Context\r
97 )\r
98{\r
99 DEBUG ((EFI_D_VERBOSE, "%a\n", __FUNCTION__));\r
100 MtrrGetAllMtrrs (Context);\r
101\r
102 //\r
103 // Close event, so it will not be invoked again.\r
104 //\r
105 gBS->CloseEvent (Event);\r
106}\r
107\r
108/**\r
109 The entry function of the CpuS3Data driver.\r
110\r
111 Allocate and initialize all fields of the ACPI_CPU_DATA structure except the\r
112 MTRR settings. Register an event notification on gEfiEndOfDxeEventGroupGuid\r
113 to capture the ACPI_CPU_DATA MTRR settings. The PcdCpuS3DataAddress is set\r
114 to the address that ACPI_CPU_DATA is allocated at.\r
115\r
116 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
117 @param[in] SystemTable A pointer to the EFI System Table.\r
118\r
119 @retval EFI_SUCCESS The entry point is executed successfully.\r
120 @retval other Some error occurs when executing this entry point.\r
121\r
122**/\r
123EFI_STATUS\r
124EFIAPI\r
125CpuS3DataInitialize (\r
126 IN EFI_HANDLE ImageHandle,\r
127 IN EFI_SYSTEM_TABLE *SystemTable\r
128 )\r
129{\r
130 EFI_STATUS Status;\r
131 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
132 ACPI_CPU_DATA *AcpiCpuData;\r
133 EFI_MP_SERVICES_PROTOCOL *MpServices;\r
134 UINTN NumberOfCpus;\r
135 UINTN NumberOfEnabledProcessors;\r
136 VOID *Stack;\r
137 UINTN TableSize;\r
138 CPU_REGISTER_TABLE *RegisterTable;\r
139 UINTN Index;\r
140 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
141 UINTN GdtSize;\r
142 UINTN IdtSize;\r
143 VOID *Gdt;\r
144 VOID *Idt;\r
145 EFI_EVENT Event;\r
146\r
147 //\r
148 // Allocate ACPI NVS memory below 4G memory for use on ACPI S3 resume.\r
149 //\r
150 AcpiCpuDataEx = AllocateAcpiNvsMemoryBelow4G (sizeof (ACPI_CPU_DATA_EX));\r
151 ASSERT (AcpiCpuDataEx != NULL);\r
152 AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;\r
153\r
154 //\r
155 // Get MP Services Protocol\r
156 //\r
157 Status = gBS->LocateProtocol (\r
158 &gEfiMpServiceProtocolGuid,\r
159 NULL,\r
160 (VOID **)&MpServices\r
161 );\r
162 ASSERT_EFI_ERROR (Status);\r
163\r
164 //\r
165 // Allocate a 4KB reserved page below 1MB\r
166 //\r
167 AcpiCpuData->StartupVector = BASE_1MB - 1;\r
168 Status = gBS->AllocatePages (\r
169 AllocateMaxAddress,\r
170 EfiReservedMemoryType,\r
171 1,\r
172 &AcpiCpuData->StartupVector\r
173 );\r
174 ASSERT_EFI_ERROR (Status);\r
175\r
176 //\r
177 // Get the number of CPUs\r
178 //\r
179 Status = MpServices->GetNumberOfProcessors (\r
180 MpServices,\r
181 &NumberOfCpus,\r
182 &NumberOfEnabledProcessors\r
183 );\r
184 ASSERT_EFI_ERROR (Status);\r
185 AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;\r
186\r
187 //\r
188 // Initialize ACPI_CPU_DATA fields\r
189 //\r
190 AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);\r
191 AcpiCpuData->ApMachineCheckHandlerBase = 0;\r
192 AcpiCpuData->ApMachineCheckHandlerSize = 0;\r
193 AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;\r
194 AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;\r
195 AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;\r
196\r
197 //\r
198 // Allocate stack space for all CPUs\r
199 //\r
200 Stack = AllocateAcpiNvsMemoryBelow4G (NumberOfCpus * AcpiCpuData->StackSize);\r
201 ASSERT (Stack != NULL);\r
202 AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;\r
203\r
204 //\r
205 // Get the boot processor's GDT and IDT\r
206 //\r
207 AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);\r
208 AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);\r
209\r
210 //\r
211 // Allocate GDT and IDT in ACPI NVS and copy current GDT and IDT contents\r
212 //\r
213 GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;\r
214 IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;\r
215 Gdt = AllocateAcpiNvsMemoryBelow4G (GdtSize + IdtSize);\r
216 ASSERT (Gdt != NULL);\r
217 Idt = (VOID *)((UINTN)Gdt + GdtSize);\r
218 CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);\r
219 CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);\r
220 AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;\r
221 AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;\r
222\r
223 //\r
224 // Allocate buffer for empty RegisterTable and PreSmmInitRegisterTable for all CPUs\r
225 //\r
226 TableSize = 2 * NumberOfCpus * sizeof (CPU_REGISTER_TABLE);\r
227 RegisterTable = (CPU_REGISTER_TABLE *)AllocateAcpiNvsMemoryBelow4G (TableSize);\r
228 ASSERT (RegisterTable != NULL);\r
229 for (Index = 0; Index < NumberOfCpus; Index++) {\r
230 Status = MpServices->GetProcessorInfo (\r
231 MpServices,\r
232 Index,\r
233 &ProcessorInfoBuffer\r
234 );\r
235 ASSERT_EFI_ERROR (Status);\r
236\r
237 RegisterTable[Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
238 RegisterTable[Index].TableLength = 0;\r
239 RegisterTable[Index].AllocatedSize = 0;\r
240 RegisterTable[Index].RegisterTableEntry = NULL;\r
241\r
242 RegisterTable[NumberOfCpus + Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
243 RegisterTable[NumberOfCpus + Index].TableLength = 0;\r
244 RegisterTable[NumberOfCpus + Index].AllocatedSize = 0;\r
245 RegisterTable[NumberOfCpus + Index].RegisterTableEntry = NULL;\r
246 }\r
247 AcpiCpuData->RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTable;\r
248 AcpiCpuData->PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)(RegisterTable + NumberOfCpus);\r
249\r
250 //\r
251 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure\r
252 //\r
253 Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);\r
254 ASSERT_EFI_ERROR (Status);\r
255\r
256 //\r
257 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.\r
258 // The notification function saves MTRRs for ACPI_CPU_DATA\r
259 //\r
260 Status = gBS->CreateEventEx (\r
261 EVT_NOTIFY_SIGNAL,\r
262 TPL_CALLBACK,\r
263 SaveMtrrsOnEndOfDxe,\r
264 &AcpiCpuDataEx->MtrrTable,\r
265 &gEfiEndOfDxeEventGroupGuid,\r
266 &Event\r
267 );\r
268 ASSERT_EFI_ERROR (Status);\r
269\r
270 return EFI_SUCCESS;\r
271}\r