2 ACPI CPU Data initialization module
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.
12 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
13 Copyright (c) 2015, Red Hat, Inc.
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
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.
27 #include <AcpiCpuData.h>
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>
35 #include <Protocol/MpService.h>
36 #include <Guid/EventGroup.h>
39 // Data structure used to allocate ACPI_CPU_DATA and its supporting structures
42 ACPI_CPU_DATA AcpiCpuData
;
43 MTRR_SETTINGS MtrrTable
;
44 IA32_DESCRIPTOR GdtrProfile
;
45 IA32_DESCRIPTOR IdtrProfile
;
49 Allocate EfiACPIMemoryNVS below 4G memory address.
51 This function allocates EfiACPIMemoryNVS below 4G memory address.
53 @param[in] Size Size of memory to allocate.
55 @return Allocated address for output.
59 AllocateAcpiNvsMemoryBelow4G (
63 EFI_PHYSICAL_ADDRESS Address
;
67 Address
= BASE_4GB
- 1;
68 Status
= gBS
->AllocatePages (
71 EFI_SIZE_TO_PAGES (Size
),
74 if (EFI_ERROR (Status
)) {
78 Buffer
= (VOID
*)(UINTN
)Address
;
79 ZeroMem (Buffer
, Size
);
85 Callback function executed when the EndOfDxe event group is signaled.
87 We delay allocating StartupVector and saving the MTRR settings until BDS signals EndOfDxe.
89 @param[in] Event Event whose notification function is being invoked.
90 @param[out] Context Pointer to the MTRR_SETTINGS buffer to fill in.
100 ACPI_CPU_DATA_EX
*AcpiCpuDataEx
;
102 AcpiCpuDataEx
= (ACPI_CPU_DATA_EX
*) Context
;
104 // Allocate a 4KB reserved page below 1MB
106 AcpiCpuDataEx
->AcpiCpuData
.StartupVector
= BASE_1MB
- 1;
107 Status
= gBS
->AllocatePages (
109 EfiReservedMemoryType
,
111 &AcpiCpuDataEx
->AcpiCpuData
.StartupVector
113 ASSERT_EFI_ERROR (Status
);
115 DEBUG ((EFI_D_VERBOSE
, "%a\n", __FUNCTION__
));
116 MtrrGetAllMtrrs (&AcpiCpuDataEx
->MtrrTable
);
119 // Close event, so it will not be invoked again.
121 gBS
->CloseEvent (Event
);
125 The entry function of the CpuS3Data driver.
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.
132 @param[in] ImageHandle The firmware allocated handle for the EFI image.
133 @param[in] SystemTable A pointer to the EFI System Table.
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.
142 CpuS3DataInitialize (
143 IN EFI_HANDLE ImageHandle
,
144 IN EFI_SYSTEM_TABLE
*SystemTable
148 ACPI_CPU_DATA_EX
*AcpiCpuDataEx
;
149 ACPI_CPU_DATA
*AcpiCpuData
;
150 EFI_MP_SERVICES_PROTOCOL
*MpServices
;
152 UINTN NumberOfEnabledProcessors
;
155 CPU_REGISTER_TABLE
*RegisterTable
;
157 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer
;
164 if (!PcdGetBool (PcdAcpiS3Enable
)) {
165 return EFI_UNSUPPORTED
;
169 // Allocate ACPI NVS memory below 4G memory for use on ACPI S3 resume.
171 AcpiCpuDataEx
= AllocateAcpiNvsMemoryBelow4G (sizeof (ACPI_CPU_DATA_EX
));
172 ASSERT (AcpiCpuDataEx
!= NULL
);
173 AcpiCpuData
= &AcpiCpuDataEx
->AcpiCpuData
;
176 // Get MP Services Protocol
178 Status
= gBS
->LocateProtocol (
179 &gEfiMpServiceProtocolGuid
,
183 ASSERT_EFI_ERROR (Status
);
186 // Get the number of CPUs
188 Status
= MpServices
->GetNumberOfProcessors (
191 &NumberOfEnabledProcessors
193 ASSERT_EFI_ERROR (Status
);
194 AcpiCpuData
->NumberOfCpus
= (UINT32
)NumberOfCpus
;
197 // Initialize ACPI_CPU_DATA fields
199 AcpiCpuData
->StackSize
= PcdGet32 (PcdCpuApStackSize
);
200 AcpiCpuData
->ApMachineCheckHandlerBase
= 0;
201 AcpiCpuData
->ApMachineCheckHandlerSize
= 0;
202 AcpiCpuData
->GdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&AcpiCpuDataEx
->GdtrProfile
;
203 AcpiCpuData
->IdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&AcpiCpuDataEx
->IdtrProfile
;
204 AcpiCpuData
->MtrrTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&AcpiCpuDataEx
->MtrrTable
;
207 // Allocate stack space for all CPUs
209 Stack
= AllocateAcpiNvsMemoryBelow4G (NumberOfCpus
* AcpiCpuData
->StackSize
);
210 ASSERT (Stack
!= NULL
);
211 AcpiCpuData
->StackAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Stack
;
214 // Get the boot processor's GDT and IDT
216 AsmReadGdtr (&AcpiCpuDataEx
->GdtrProfile
);
217 AsmReadIdtr (&AcpiCpuDataEx
->IdtrProfile
);
220 // Allocate GDT and IDT in ACPI NVS and copy current GDT and IDT contents
222 GdtSize
= AcpiCpuDataEx
->GdtrProfile
.Limit
+ 1;
223 IdtSize
= AcpiCpuDataEx
->IdtrProfile
.Limit
+ 1;
224 Gdt
= AllocateAcpiNvsMemoryBelow4G (GdtSize
+ IdtSize
);
225 ASSERT (Gdt
!= NULL
);
226 Idt
= (VOID
*)((UINTN
)Gdt
+ GdtSize
);
227 CopyMem (Gdt
, (VOID
*)AcpiCpuDataEx
->GdtrProfile
.Base
, GdtSize
);
228 CopyMem (Idt
, (VOID
*)AcpiCpuDataEx
->IdtrProfile
.Base
, IdtSize
);
229 AcpiCpuDataEx
->GdtrProfile
.Base
= (UINTN
)Gdt
;
230 AcpiCpuDataEx
->IdtrProfile
.Base
= (UINTN
)Idt
;
233 // Allocate buffer for empty RegisterTable and PreSmmInitRegisterTable for all CPUs
235 TableSize
= 2 * NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
);
236 RegisterTable
= (CPU_REGISTER_TABLE
*)AllocateAcpiNvsMemoryBelow4G (TableSize
);
237 ASSERT (RegisterTable
!= NULL
);
238 for (Index
= 0; Index
< NumberOfCpus
; Index
++) {
239 Status
= MpServices
->GetProcessorInfo (
244 ASSERT_EFI_ERROR (Status
);
246 RegisterTable
[Index
].InitialApicId
= (UINT32
)ProcessorInfoBuffer
.ProcessorId
;
247 RegisterTable
[Index
].TableLength
= 0;
248 RegisterTable
[Index
].AllocatedSize
= 0;
249 RegisterTable
[Index
].RegisterTableEntry
= NULL
;
251 RegisterTable
[NumberOfCpus
+ Index
].InitialApicId
= (UINT32
)ProcessorInfoBuffer
.ProcessorId
;
252 RegisterTable
[NumberOfCpus
+ Index
].TableLength
= 0;
253 RegisterTable
[NumberOfCpus
+ Index
].AllocatedSize
= 0;
254 RegisterTable
[NumberOfCpus
+ Index
].RegisterTableEntry
= NULL
;
256 AcpiCpuData
->RegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RegisterTable
;
257 AcpiCpuData
->PreSmmInitRegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(RegisterTable
+ NumberOfCpus
);
260 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure
262 Status
= PcdSet64S (PcdCpuS3DataAddress
, (UINT64
)(UINTN
)AcpiCpuData
);
263 ASSERT_EFI_ERROR (Status
);
266 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
267 // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA
269 Status
= gBS
->CreateEventEx (
274 &gEfiEndOfDxeEventGroupGuid
,
277 ASSERT_EFI_ERROR (Status
);