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