CpuService.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /** @file
  2. Implementation of SMM CPU Services Protocol.
  3. Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PiSmmCpuDxeSmm.h"
  7. //
  8. // SMM CPU Service Protocol instance
  9. //
  10. EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService = {
  11. SmmGetProcessorInfo,
  12. SmmSwitchBsp,
  13. SmmAddProcessor,
  14. SmmRemoveProcessor,
  15. SmmWhoAmI,
  16. SmmRegisterExceptionHandler
  17. };
  18. //
  19. // EDKII SMM CPU Rendezvous Service Protocol instance
  20. //
  21. EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL mSmmCpuRendezvousService = {
  22. SmmCpuRendezvous
  23. };
  24. /**
  25. Gets processor information on the requested processor at the instant this call is made.
  26. @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
  27. @param[in] ProcessorNumber The handle number of processor.
  28. @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
  29. the requested processor is deposited.
  30. @retval EFI_SUCCESS Processor information was returned.
  31. @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
  32. @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
  33. @retval EFI_NOT_FOUND The processor with the handle specified by
  34. ProcessorNumber does not exist in the platform.
  35. **/
  36. EFI_STATUS
  37. EFIAPI
  38. SmmGetProcessorInfo (
  39. IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  40. IN UINTN ProcessorNumber,
  41. OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
  42. )
  43. {
  44. //
  45. // Check parameter
  46. //
  47. if ((ProcessorNumber >= mMaxNumberOfCpus) || (ProcessorInfoBuffer == NULL)) {
  48. return EFI_INVALID_PARAMETER;
  49. }
  50. if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
  51. return EFI_NOT_FOUND;
  52. }
  53. //
  54. // Fill in processor information
  55. //
  56. CopyMem (ProcessorInfoBuffer, &gSmmCpuPrivate->ProcessorInfo[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
  57. return EFI_SUCCESS;
  58. }
  59. /**
  60. This service switches the requested AP to be the BSP since the next SMI.
  61. @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
  62. @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
  63. @retval EFI_SUCCESS BSP will be switched in next SMI.
  64. @retval EFI_UNSUPPORTED Switching the BSP or a processor to be hot-removed is not supported.
  65. @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
  66. @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
  67. **/
  68. EFI_STATUS
  69. EFIAPI
  70. SmmSwitchBsp (
  71. IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  72. IN UINTN ProcessorNumber
  73. )
  74. {
  75. //
  76. // Check parameter
  77. //
  78. if (ProcessorNumber >= mMaxNumberOfCpus) {
  79. return EFI_INVALID_PARAMETER;
  80. }
  81. if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
  82. return EFI_NOT_FOUND;
  83. }
  84. if ((gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) ||
  85. (gSmst->CurrentlyExecutingCpu == ProcessorNumber))
  86. {
  87. return EFI_UNSUPPORTED;
  88. }
  89. //
  90. // Setting of the BSP for next SMI is pending until all SMI handlers are finished
  91. //
  92. gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuSwitchBsp;
  93. return EFI_SUCCESS;
  94. }
  95. /**
  96. Notify that a processor was hot-added.
  97. @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
  98. @param[in] ProcessorId Local APIC ID of the hot-added processor.
  99. @param[out] ProcessorNumber The handle number of the hot-added processor.
  100. @retval EFI_SUCCESS The hot-addition of the specified processors was successfully notified.
  101. @retval EFI_UNSUPPORTED Hot addition of processor is not supported.
  102. @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
  103. @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
  104. @retval EFI_ALREADY_STARTED The processor is already online in the system.
  105. **/
  106. EFI_STATUS
  107. EFIAPI
  108. SmmAddProcessor (
  109. IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  110. IN UINT64 ProcessorId,
  111. OUT UINTN *ProcessorNumber
  112. )
  113. {
  114. UINTN Index;
  115. if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
  116. return EFI_UNSUPPORTED;
  117. }
  118. //
  119. // Check parameter
  120. //
  121. if ((ProcessorNumber == NULL) || (ProcessorId == INVALID_APIC_ID)) {
  122. return EFI_INVALID_PARAMETER;
  123. }
  124. //
  125. // Check if the processor already exists
  126. //
  127. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  128. if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ProcessorId) {
  129. return EFI_ALREADY_STARTED;
  130. }
  131. }
  132. //
  133. // Check CPU hot plug data. The CPU RAS handler should have created the mapping
  134. // of the APIC ID to SMBASE.
  135. //
  136. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  137. if ((mCpuHotPlugData.ApicId[Index] == ProcessorId) &&
  138. (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == INVALID_APIC_ID))
  139. {
  140. gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = ProcessorId;
  141. gSmmCpuPrivate->ProcessorInfo[Index].StatusFlag = 0;
  142. GetProcessorLocationByApicId (
  143. (UINT32)ProcessorId,
  144. &gSmmCpuPrivate->ProcessorInfo[Index].Location.Package,
  145. &gSmmCpuPrivate->ProcessorInfo[Index].Location.Core,
  146. &gSmmCpuPrivate->ProcessorInfo[Index].Location.Thread
  147. );
  148. *ProcessorNumber = Index;
  149. gSmmCpuPrivate->Operation[Index] = SmmCpuAdd;
  150. return EFI_SUCCESS;
  151. }
  152. }
  153. return EFI_INVALID_PARAMETER;
  154. }
  155. /**
  156. Notify that a processor was hot-removed.
  157. @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
  158. @param[in] ProcessorNumber The handle number of the hot-added processor.
  159. @retval EFI_SUCCESS The hot-removal of the specified processors was successfully notified.
  160. @retval EFI_UNSUPPORTED Hot removal of processor is not supported.
  161. @retval EFI_UNSUPPORTED Hot removal of BSP is not supported.
  162. @retval EFI_UNSUPPORTED Hot removal of a processor with pending hot-plug operation is not supported.
  163. @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
  164. **/
  165. EFI_STATUS
  166. EFIAPI
  167. SmmRemoveProcessor (
  168. IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  169. IN UINTN ProcessorNumber
  170. )
  171. {
  172. if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
  173. return EFI_UNSUPPORTED;
  174. }
  175. //
  176. // Check parameter
  177. //
  178. if ((ProcessorNumber >= mMaxNumberOfCpus) ||
  179. (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID))
  180. {
  181. return EFI_INVALID_PARAMETER;
  182. }
  183. //
  184. // Can't remove BSP
  185. //
  186. if (ProcessorNumber == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {
  187. return EFI_UNSUPPORTED;
  188. }
  189. if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) {
  190. return EFI_UNSUPPORTED;
  191. }
  192. gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId = INVALID_APIC_ID;
  193. mCpuHotPlugData.ApicId[ProcessorNumber] = INVALID_APIC_ID;
  194. //
  195. // Removal of the processor from the CPU list is pending until all SMI handlers are finished
  196. //
  197. gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuRemove;
  198. return EFI_SUCCESS;
  199. }
  200. /**
  201. This return the handle number for the calling processor.
  202. @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
  203. @param[out] ProcessorNumber The handle number of currently executing processor.
  204. @retval EFI_SUCCESS The current processor handle number was returned
  205. in ProcessorNumber.
  206. @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
  207. **/
  208. EFI_STATUS
  209. EFIAPI
  210. SmmWhoAmI (
  211. IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  212. OUT UINTN *ProcessorNumber
  213. )
  214. {
  215. UINTN Index;
  216. UINT64 ApicId;
  217. //
  218. // Check parameter
  219. //
  220. if (ProcessorNumber == NULL) {
  221. return EFI_INVALID_PARAMETER;
  222. }
  223. ApicId = GetApicId ();
  224. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  225. if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {
  226. *ProcessorNumber = Index;
  227. return EFI_SUCCESS;
  228. }
  229. }
  230. //
  231. // This should not happen
  232. //
  233. ASSERT (FALSE);
  234. return EFI_NOT_FOUND;
  235. }
  236. /**
  237. Update the SMM CPU list per the pending operation.
  238. This function is called after return from SMI handlers.
  239. **/
  240. VOID
  241. SmmCpuUpdate (
  242. VOID
  243. )
  244. {
  245. UINTN Index;
  246. //
  247. // Handle pending BSP switch operations
  248. //
  249. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  250. if (gSmmCpuPrivate->Operation[Index] == SmmCpuSwitchBsp) {
  251. gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
  252. mSmmMpSyncData->SwitchBsp = TRUE;
  253. mSmmMpSyncData->CandidateBsp[Index] = TRUE;
  254. }
  255. }
  256. //
  257. // Handle pending hot-add operations
  258. //
  259. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  260. if (gSmmCpuPrivate->Operation[Index] == SmmCpuAdd) {
  261. gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
  262. mNumberOfCpus++;
  263. }
  264. }
  265. //
  266. // Handle pending hot-remove operations
  267. //
  268. for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
  269. if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {
  270. gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
  271. mNumberOfCpus--;
  272. }
  273. }
  274. }
  275. /**
  276. Register exception handler.
  277. @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
  278. @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
  279. the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
  280. of the UEFI 2.0 specification.
  281. @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
  282. that is called when a processor interrupt occurs.
  283. If this parameter is NULL, then the handler will be uninstalled.
  284. @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
  285. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
  286. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
  287. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
  288. **/
  289. EFI_STATUS
  290. EFIAPI
  291. SmmRegisterExceptionHandler (
  292. IN EFI_SMM_CPU_SERVICE_PROTOCOL *This,
  293. IN EFI_EXCEPTION_TYPE ExceptionType,
  294. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  295. )
  296. {
  297. return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);
  298. }
  299. /**
  300. Initialize SMM CPU Services.
  301. It installs EFI SMM CPU Services Protocol.
  302. @param ImageHandle The firmware allocated handle for the EFI image.
  303. @retval EFI_SUCCESS EFI SMM CPU Services Protocol was installed successfully.
  304. @retval OTHER Fail to install Protocol.
  305. **/
  306. EFI_STATUS
  307. InitializeSmmCpuServices (
  308. IN EFI_HANDLE Handle
  309. )
  310. {
  311. EFI_STATUS Status;
  312. Status = gSmst->SmmInstallProtocolInterface (
  313. &Handle,
  314. &gEfiSmmCpuServiceProtocolGuid,
  315. EFI_NATIVE_INTERFACE,
  316. &mSmmCpuService
  317. );
  318. ASSERT_EFI_ERROR (Status);
  319. if (EFI_ERROR (Status)) {
  320. return Status;
  321. }
  322. Status = gSmst->SmmInstallProtocolInterface (
  323. &Handle,
  324. &gEdkiiSmmCpuRendezvousProtocolGuid,
  325. EFI_NATIVE_INTERFACE,
  326. &mSmmCpuRendezvousService
  327. );
  328. ASSERT_EFI_ERROR (Status);
  329. return Status;
  330. }
  331. /**
  332. Wait for all processors enterring SMM until all CPUs are already synchronized or not.
  333. If BlockingMode is False, timeout value is zero.
  334. @param This A pointer to the EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL instance.
  335. @param BlockingMode Blocking mode or non-blocking mode.
  336. @retval EFI_SUCCESS All avaiable APs arrived.
  337. @retval EFI_TIMEOUT Wait for all APs until timeout.
  338. **/
  339. EFI_STATUS
  340. EFIAPI
  341. SmmCpuRendezvous (
  342. IN EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL *This,
  343. IN BOOLEAN BlockingMode
  344. )
  345. {
  346. EFI_STATUS Status;
  347. //
  348. // Return success immediately if all CPUs are already synchronized.
  349. //
  350. if (mSmmMpSyncData->AllApArrivedWithException) {
  351. Status = EFI_SUCCESS;
  352. goto ON_EXIT;
  353. }
  354. if (!BlockingMode) {
  355. Status = EFI_TIMEOUT;
  356. goto ON_EXIT;
  357. }
  358. //
  359. // There are some APs outside SMM, Wait for all avaiable APs to arrive.
  360. //
  361. SmmWaitForApArrival ();
  362. Status = mSmmMpSyncData->AllApArrivedWithException ? EFI_SUCCESS : EFI_TIMEOUT;
  363. ON_EXIT:
  364. if (!mSmmMpSyncData->AllApArrivedWithException) {
  365. DEBUG ((DEBUG_INFO, "EdkiiSmmWaitForAllApArrival: Timeout to wait all APs arrival\n"));
  366. }
  367. return Status;
  368. }