CpuS3Data.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /** @file
  2. ACPI CPU Data initialization module
  3. This module initializes the ACPI_CPU_DATA structure and registers the address
  4. of this structure in the PcdCpuS3DataAddress PCD. This is a generic/simple
  5. version of this module. It does not provide a machine check handler or CPU
  6. register initialization tables for ACPI S3 resume. It also only supports the
  7. number of CPUs reported by the MP Services Protocol, so this module does not
  8. support hot plug CPUs. This module can be copied into a CPU specific package
  9. and customized if these additional features are required.
  10. Copyright (c) 2013 - 2021, Intel Corporation. All rights reserved.<BR>
  11. Copyright (c) 2015 - 2020, Red Hat, Inc.
  12. SPDX-License-Identifier: BSD-2-Clause-Patent
  13. **/
  14. #include <PiDxe.h>
  15. #include <AcpiCpuData.h>
  16. #include <Library/BaseLib.h>
  17. #include <Library/BaseMemoryLib.h>
  18. #include <Library/DebugLib.h>
  19. #include <Library/MemoryAllocationLib.h>
  20. #include <Library/MtrrLib.h>
  21. #include <Library/UefiBootServicesTableLib.h>
  22. #include <Protocol/MpService.h>
  23. #include <Guid/EventGroup.h>
  24. //
  25. // Data structure used to allocate ACPI_CPU_DATA and its supporting structures
  26. //
  27. typedef struct {
  28. ACPI_CPU_DATA AcpiCpuData;
  29. MTRR_SETTINGS MtrrTable;
  30. IA32_DESCRIPTOR GdtrProfile;
  31. IA32_DESCRIPTOR IdtrProfile;
  32. } ACPI_CPU_DATA_EX;
  33. /**
  34. Allocate EfiACPIMemoryNVS memory.
  35. @param[in] Size Size of memory to allocate.
  36. @return Allocated address for output.
  37. **/
  38. VOID *
  39. AllocateAcpiNvsMemory (
  40. IN UINTN Size
  41. )
  42. {
  43. EFI_PHYSICAL_ADDRESS Address;
  44. EFI_STATUS Status;
  45. VOID *Buffer;
  46. Status = gBS->AllocatePages (
  47. AllocateAnyPages,
  48. EfiACPIMemoryNVS,
  49. EFI_SIZE_TO_PAGES (Size),
  50. &Address
  51. );
  52. if (EFI_ERROR (Status)) {
  53. return NULL;
  54. }
  55. Buffer = (VOID *)(UINTN)Address;
  56. ZeroMem (Buffer, Size);
  57. return Buffer;
  58. }
  59. /**
  60. Allocate memory and clean it with zero.
  61. @param[in] Size Size of memory to allocate.
  62. @return Allocated address for output.
  63. **/
  64. VOID *
  65. AllocateZeroPages (
  66. IN UINTN Size
  67. )
  68. {
  69. VOID *Buffer;
  70. Buffer = AllocatePages (EFI_SIZE_TO_PAGES (Size));
  71. if (Buffer != NULL) {
  72. ZeroMem (Buffer, Size);
  73. }
  74. return Buffer;
  75. }
  76. /**
  77. Callback function executed when the EndOfDxe event group is signaled.
  78. We delay allocating StartupVector and saving the MTRR settings until BDS signals EndOfDxe.
  79. @param[in] Event Event whose notification function is being invoked.
  80. @param[out] Context Pointer to the MTRR_SETTINGS buffer to fill in.
  81. **/
  82. VOID
  83. EFIAPI
  84. CpuS3DataOnEndOfDxe (
  85. IN EFI_EVENT Event,
  86. OUT VOID *Context
  87. )
  88. {
  89. EFI_STATUS Status;
  90. ACPI_CPU_DATA_EX *AcpiCpuDataEx;
  91. AcpiCpuDataEx = (ACPI_CPU_DATA_EX *)Context;
  92. //
  93. // Allocate a 4KB reserved page below 1MB
  94. //
  95. AcpiCpuDataEx->AcpiCpuData.StartupVector = BASE_1MB - 1;
  96. Status = gBS->AllocatePages (
  97. AllocateMaxAddress,
  98. EfiReservedMemoryType,
  99. 1,
  100. &AcpiCpuDataEx->AcpiCpuData.StartupVector
  101. );
  102. ASSERT_EFI_ERROR (Status);
  103. DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
  104. MtrrGetAllMtrrs (&AcpiCpuDataEx->MtrrTable);
  105. //
  106. // Close event, so it will not be invoked again.
  107. //
  108. gBS->CloseEvent (Event);
  109. }
  110. /**
  111. The entry function of the CpuS3Data driver.
  112. Allocate and initialize all fields of the ACPI_CPU_DATA structure except the
  113. MTRR settings. Register an event notification on gEfiEndOfDxeEventGroupGuid
  114. to capture the ACPI_CPU_DATA MTRR settings. The PcdCpuS3DataAddress is set
  115. to the address that ACPI_CPU_DATA is allocated at.
  116. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  117. @param[in] SystemTable A pointer to the EFI System Table.
  118. @retval EFI_SUCCESS The entry point is executed successfully.
  119. @retval EFI_UNSUPPORTED Do not support ACPI S3.
  120. @retval other Some error occurs when executing this entry point.
  121. **/
  122. EFI_STATUS
  123. EFIAPI
  124. CpuS3DataInitialize (
  125. IN EFI_HANDLE ImageHandle,
  126. IN EFI_SYSTEM_TABLE *SystemTable
  127. )
  128. {
  129. EFI_STATUS Status;
  130. ACPI_CPU_DATA_EX *AcpiCpuDataEx;
  131. ACPI_CPU_DATA *AcpiCpuData;
  132. EFI_MP_SERVICES_PROTOCOL *MpServices;
  133. UINTN NumberOfCpus;
  134. VOID *Stack;
  135. UINTN GdtSize;
  136. UINTN IdtSize;
  137. VOID *Gdt;
  138. VOID *Idt;
  139. EFI_EVENT Event;
  140. ACPI_CPU_DATA *OldAcpiCpuData;
  141. if (!PcdGetBool (PcdAcpiS3Enable)) {
  142. return EFI_UNSUPPORTED;
  143. }
  144. //
  145. // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure
  146. //
  147. OldAcpiCpuData = (ACPI_CPU_DATA *)(UINTN)PcdGet64 (PcdCpuS3DataAddress);
  148. AcpiCpuDataEx = AllocateZeroPages (sizeof (ACPI_CPU_DATA_EX));
  149. ASSERT (AcpiCpuDataEx != NULL);
  150. AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;
  151. if (PcdGetBool (PcdQ35SmramAtDefaultSmbase)) {
  152. NumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
  153. } else {
  154. UINTN NumberOfEnabledProcessors;
  155. //
  156. // Get MP Services Protocol
  157. //
  158. Status = gBS->LocateProtocol (
  159. &gEfiMpServiceProtocolGuid,
  160. NULL,
  161. (VOID **)&MpServices
  162. );
  163. ASSERT_EFI_ERROR (Status);
  164. //
  165. // Get the number of CPUs
  166. //
  167. Status = MpServices->GetNumberOfProcessors (
  168. MpServices,
  169. &NumberOfCpus,
  170. &NumberOfEnabledProcessors
  171. );
  172. ASSERT_EFI_ERROR (Status);
  173. }
  174. AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;
  175. //
  176. // Initialize ACPI_CPU_DATA fields
  177. //
  178. AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);
  179. AcpiCpuData->ApMachineCheckHandlerBase = 0;
  180. AcpiCpuData->ApMachineCheckHandlerSize = 0;
  181. AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;
  182. AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;
  183. AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;
  184. //
  185. // Allocate stack space for all CPUs.
  186. // Use ACPI NVS memory type because this data will be directly used by APs
  187. // in S3 resume phase in long mode. Also during S3 resume, the stack buffer
  188. // will only be used as scratch space. i.e. we won't read anything from it
  189. // before we write to it, in PiSmmCpuDxeSmm.
  190. //
  191. Stack = AllocateAcpiNvsMemory (NumberOfCpus * AcpiCpuData->StackSize);
  192. ASSERT (Stack != NULL);
  193. AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;
  194. //
  195. // Get the boot processor's GDT and IDT
  196. //
  197. AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);
  198. AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);
  199. //
  200. // Allocate GDT and IDT and copy current GDT and IDT contents
  201. //
  202. GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;
  203. IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;
  204. Gdt = AllocateZeroPages (GdtSize + IdtSize);
  205. ASSERT (Gdt != NULL);
  206. Idt = (VOID *)((UINTN)Gdt + GdtSize);
  207. CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);
  208. CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);
  209. AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;
  210. AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;
  211. if (OldAcpiCpuData != NULL) {
  212. CopyMem (&AcpiCpuData->CpuFeatureInitData, &OldAcpiCpuData->CpuFeatureInitData, sizeof (CPU_FEATURE_INIT_DATA));
  213. }
  214. //
  215. // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure
  216. //
  217. Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);
  218. ASSERT_EFI_ERROR (Status);
  219. //
  220. // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
  221. // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA
  222. //
  223. Status = gBS->CreateEventEx (
  224. EVT_NOTIFY_SIGNAL,
  225. TPL_CALLBACK,
  226. CpuS3DataOnEndOfDxe,
  227. AcpiCpuData,
  228. &gEfiEndOfDxeEventGroupGuid,
  229. &Event
  230. );
  231. ASSERT_EFI_ERROR (Status);
  232. return EFI_SUCCESS;
  233. }