]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
ArmPkg: remove BaseMemoryLibVstm implementation of BaseMemoryLib
[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
136 @retval other Some error occurs when executing this entry point.\r
137\r
138**/\r
139EFI_STATUS\r
140EFIAPI\r
141CpuS3DataInitialize (\r
142 IN EFI_HANDLE ImageHandle,\r
143 IN EFI_SYSTEM_TABLE *SystemTable\r
144 )\r
145{\r
146 EFI_STATUS Status;\r
147 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
148 ACPI_CPU_DATA *AcpiCpuData;\r
149 EFI_MP_SERVICES_PROTOCOL *MpServices;\r
150 UINTN NumberOfCpus;\r
151 UINTN NumberOfEnabledProcessors;\r
152 VOID *Stack;\r
153 UINTN TableSize;\r
154 CPU_REGISTER_TABLE *RegisterTable;\r
155 UINTN Index;\r
156 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
157 UINTN GdtSize;\r
158 UINTN IdtSize;\r
159 VOID *Gdt;\r
160 VOID *Idt;\r
161 EFI_EVENT Event;\r
162\r
163 //\r
164 // Allocate ACPI NVS memory below 4G memory for use on ACPI S3 resume.\r
165 //\r
166 AcpiCpuDataEx = AllocateAcpiNvsMemoryBelow4G (sizeof (ACPI_CPU_DATA_EX));\r
167 ASSERT (AcpiCpuDataEx != NULL);\r
168 AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;\r
169\r
170 //\r
171 // Get MP Services Protocol\r
172 //\r
173 Status = gBS->LocateProtocol (\r
174 &gEfiMpServiceProtocolGuid,\r
175 NULL,\r
176 (VOID **)&MpServices\r
177 );\r
178 ASSERT_EFI_ERROR (Status);\r
179\r
bfec5efa
MK
180 //\r
181 // Get the number of CPUs\r
182 //\r
183 Status = MpServices->GetNumberOfProcessors (\r
184 MpServices,\r
185 &NumberOfCpus,\r
186 &NumberOfEnabledProcessors\r
187 );\r
188 ASSERT_EFI_ERROR (Status);\r
189 AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;\r
190\r
191 //\r
192 // Initialize ACPI_CPU_DATA fields\r
193 //\r
194 AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);\r
195 AcpiCpuData->ApMachineCheckHandlerBase = 0;\r
196 AcpiCpuData->ApMachineCheckHandlerSize = 0;\r
197 AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;\r
198 AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;\r
199 AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;\r
200\r
201 //\r
202 // Allocate stack space for all CPUs\r
203 //\r
204 Stack = AllocateAcpiNvsMemoryBelow4G (NumberOfCpus * AcpiCpuData->StackSize);\r
205 ASSERT (Stack != NULL);\r
206 AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;\r
207\r
208 //\r
209 // Get the boot processor's GDT and IDT\r
210 //\r
211 AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);\r
212 AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);\r
213\r
214 //\r
215 // Allocate GDT and IDT in ACPI NVS and copy current GDT and IDT contents\r
216 //\r
217 GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;\r
218 IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;\r
219 Gdt = AllocateAcpiNvsMemoryBelow4G (GdtSize + IdtSize);\r
220 ASSERT (Gdt != NULL);\r
221 Idt = (VOID *)((UINTN)Gdt + GdtSize);\r
222 CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);\r
223 CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);\r
224 AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;\r
225 AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;\r
226\r
227 //\r
228 // Allocate buffer for empty RegisterTable and PreSmmInitRegisterTable for all CPUs\r
229 //\r
230 TableSize = 2 * NumberOfCpus * sizeof (CPU_REGISTER_TABLE);\r
231 RegisterTable = (CPU_REGISTER_TABLE *)AllocateAcpiNvsMemoryBelow4G (TableSize);\r
232 ASSERT (RegisterTable != NULL);\r
233 for (Index = 0; Index < NumberOfCpus; Index++) {\r
234 Status = MpServices->GetProcessorInfo (\r
235 MpServices,\r
236 Index,\r
237 &ProcessorInfoBuffer\r
238 );\r
239 ASSERT_EFI_ERROR (Status);\r
240\r
241 RegisterTable[Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
242 RegisterTable[Index].TableLength = 0;\r
243 RegisterTable[Index].AllocatedSize = 0;\r
244 RegisterTable[Index].RegisterTableEntry = NULL;\r
245\r
246 RegisterTable[NumberOfCpus + Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
247 RegisterTable[NumberOfCpus + Index].TableLength = 0;\r
248 RegisterTable[NumberOfCpus + Index].AllocatedSize = 0;\r
249 RegisterTable[NumberOfCpus + Index].RegisterTableEntry = NULL;\r
250 }\r
251 AcpiCpuData->RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTable;\r
252 AcpiCpuData->PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)(RegisterTable + NumberOfCpus);\r
253\r
254 //\r
255 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure\r
256 //\r
257 Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);\r
258 ASSERT_EFI_ERROR (Status);\r
259\r
260 //\r
261 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.\r
65b24ada 262 // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA\r
bfec5efa
MK
263 //\r
264 Status = gBS->CreateEventEx (\r
265 EVT_NOTIFY_SIGNAL,\r
266 TPL_CALLBACK,\r
65b24ada
JF
267 CpuS3DataOnEndOfDxe,\r
268 AcpiCpuData,\r
bfec5efa
MK
269 &gEfiEndOfDxeEventGroupGuid,\r
270 &Event\r
271 );\r
272 ASSERT_EFI_ERROR (Status);\r
273\r
274 return EFI_SUCCESS;\r
275}\r