DxeMpLib.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /** @file
  2. MP initialize support functions for DXE phase.
  3. Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "MpLib.h"
  7. #include <Library/UefiLib.h>
  8. #include <Library/UefiBootServicesTableLib.h>
  9. #include <Library/DebugAgentLib.h>
  10. #include <Library/DxeServicesTableLib.h>
  11. #include <Library/VmgExitLib.h>
  12. #include <Register/Amd/Fam17Msr.h>
  13. #include <Register/Amd/Ghcb.h>
  14. #include <Protocol/Timer.h>
  15. #define AP_SAFE_STACK_SIZE 128
  16. CPU_MP_DATA *mCpuMpData = NULL;
  17. EFI_EVENT mCheckAllApsEvent = NULL;
  18. EFI_EVENT mMpInitExitBootServicesEvent = NULL;
  19. EFI_EVENT mLegacyBootEvent = NULL;
  20. volatile BOOLEAN mStopCheckAllApsStatus = TRUE;
  21. VOID *mReservedApLoopFunc = NULL;
  22. UINTN mReservedTopOfApStack;
  23. volatile UINT32 mNumberToFinish = 0;
  24. //
  25. // Begin wakeup buffer allocation below 0x88000
  26. //
  27. STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;
  28. /**
  29. Enable Debug Agent to support source debugging on AP function.
  30. **/
  31. VOID
  32. EnableDebugAgent (
  33. VOID
  34. )
  35. {
  36. //
  37. // Initialize Debug Agent to support source level debug in DXE phase
  38. //
  39. InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);
  40. }
  41. /**
  42. Get the pointer to CPU MP Data structure.
  43. @return The pointer to CPU MP Data structure.
  44. **/
  45. CPU_MP_DATA *
  46. GetCpuMpData (
  47. VOID
  48. )
  49. {
  50. ASSERT (mCpuMpData != NULL);
  51. return mCpuMpData;
  52. }
  53. /**
  54. Save the pointer to CPU MP Data structure.
  55. @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
  56. **/
  57. VOID
  58. SaveCpuMpData (
  59. IN CPU_MP_DATA *CpuMpData
  60. )
  61. {
  62. mCpuMpData = CpuMpData;
  63. }
  64. /**
  65. Get available system memory below 0x88000 by specified size.
  66. @param[in] WakeupBufferSize Wakeup buffer size required
  67. @retval other Return wakeup buffer address below 1MB.
  68. @retval -1 Cannot find free memory below 1MB.
  69. **/
  70. UINTN
  71. GetWakeupBuffer (
  72. IN UINTN WakeupBufferSize
  73. )
  74. {
  75. EFI_STATUS Status;
  76. EFI_PHYSICAL_ADDRESS StartAddress;
  77. EFI_MEMORY_TYPE MemoryType;
  78. if (PcdGetBool (PcdSevEsIsEnabled)) {
  79. MemoryType = EfiReservedMemoryType;
  80. } else {
  81. MemoryType = EfiBootServicesData;
  82. }
  83. //
  84. // Try to allocate buffer below 1M for waking vector.
  85. // LegacyBios driver only reports warning when page allocation in range
  86. // [0x60000, 0x88000) fails.
  87. // This library is consumed by CpuDxe driver to produce CPU Arch protocol.
  88. // LagacyBios driver depends on CPU Arch protocol which guarantees below
  89. // allocation runs earlier than LegacyBios driver.
  90. //
  91. if (PcdGetBool (PcdSevEsIsEnabled)) {
  92. //
  93. // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one
  94. //
  95. StartAddress = mSevEsDxeWakeupBuffer;
  96. } else {
  97. StartAddress = 0x88000;
  98. }
  99. Status = gBS->AllocatePages (
  100. AllocateMaxAddress,
  101. MemoryType,
  102. EFI_SIZE_TO_PAGES (WakeupBufferSize),
  103. &StartAddress
  104. );
  105. ASSERT_EFI_ERROR (Status);
  106. if (EFI_ERROR (Status)) {
  107. StartAddress = (EFI_PHYSICAL_ADDRESS)-1;
  108. } else if (PcdGetBool (PcdSevEsIsEnabled)) {
  109. //
  110. // Next SEV-ES wakeup buffer allocation must be below this allocation
  111. //
  112. mSevEsDxeWakeupBuffer = StartAddress;
  113. }
  114. DEBUG ((
  115. DEBUG_INFO,
  116. "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
  117. (UINTN)StartAddress,
  118. WakeupBufferSize
  119. ));
  120. return (UINTN)StartAddress;
  121. }
  122. /**
  123. Get available EfiBootServicesCode memory below 4GB by specified size.
  124. This buffer is required to safely transfer AP from real address mode to
  125. protected mode or long mode, due to the fact that the buffer returned by
  126. GetWakeupBuffer() may be marked as non-executable.
  127. @param[in] BufferSize Wakeup transition buffer size.
  128. @retval other Return wakeup transition buffer address below 4GB.
  129. @retval 0 Cannot find free memory below 4GB.
  130. **/
  131. UINTN
  132. GetModeTransitionBuffer (
  133. IN UINTN BufferSize
  134. )
  135. {
  136. EFI_STATUS Status;
  137. EFI_PHYSICAL_ADDRESS StartAddress;
  138. StartAddress = BASE_4GB - 1;
  139. Status = gBS->AllocatePages (
  140. AllocateMaxAddress,
  141. EfiBootServicesCode,
  142. EFI_SIZE_TO_PAGES (BufferSize),
  143. &StartAddress
  144. );
  145. if (EFI_ERROR (Status)) {
  146. StartAddress = 0;
  147. }
  148. return (UINTN)StartAddress;
  149. }
  150. /**
  151. Return the address of the SEV-ES AP jump table.
  152. This buffer is required in order for an SEV-ES guest to transition from
  153. UEFI into an OS.
  154. @return Return SEV-ES AP jump table buffer
  155. **/
  156. UINTN
  157. GetSevEsAPMemory (
  158. VOID
  159. )
  160. {
  161. EFI_STATUS Status;
  162. EFI_PHYSICAL_ADDRESS StartAddress;
  163. MSR_SEV_ES_GHCB_REGISTER Msr;
  164. GHCB *Ghcb;
  165. BOOLEAN InterruptState;
  166. //
  167. // Allocate 1 page for AP jump table page
  168. //
  169. StartAddress = BASE_4GB - 1;
  170. Status = gBS->AllocatePages (
  171. AllocateMaxAddress,
  172. EfiReservedMemoryType,
  173. 1,
  174. &StartAddress
  175. );
  176. ASSERT_EFI_ERROR (Status);
  177. DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN)StartAddress));
  178. //
  179. // Save the SevEsAPMemory as the AP jump table.
  180. //
  181. Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
  182. Ghcb = Msr.Ghcb;
  183. VmgInit (Ghcb, &InterruptState);
  184. VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64)(UINTN)StartAddress);
  185. VmgDone (Ghcb, InterruptState);
  186. return (UINTN)StartAddress;
  187. }
  188. /**
  189. Checks APs status and updates APs status if needed.
  190. **/
  191. VOID
  192. CheckAndUpdateApsStatus (
  193. VOID
  194. )
  195. {
  196. UINTN ProcessorNumber;
  197. EFI_STATUS Status;
  198. CPU_MP_DATA *CpuMpData;
  199. CpuMpData = GetCpuMpData ();
  200. //
  201. // First, check whether pending StartupAllAPs() exists.
  202. //
  203. if (CpuMpData->WaitEvent != NULL) {
  204. Status = CheckAllAPs ();
  205. //
  206. // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
  207. //
  208. if (Status != EFI_NOT_READY) {
  209. Status = gBS->SignalEvent (CpuMpData->WaitEvent);
  210. CpuMpData->WaitEvent = NULL;
  211. }
  212. }
  213. //
  214. // Second, check whether pending StartupThisAPs() callings exist.
  215. //
  216. for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
  217. if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {
  218. continue;
  219. }
  220. Status = CheckThisAP (ProcessorNumber);
  221. if (Status != EFI_NOT_READY) {
  222. gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);
  223. CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;
  224. }
  225. }
  226. }
  227. /**
  228. Checks APs' status periodically.
  229. This function is triggered by timer periodically to check the
  230. state of APs for StartupAllAPs() and StartupThisAP() executed
  231. in non-blocking mode.
  232. @param[in] Event Event triggered.
  233. @param[in] Context Parameter passed with the event.
  234. **/
  235. VOID
  236. EFIAPI
  237. CheckApsStatus (
  238. IN EFI_EVENT Event,
  239. IN VOID *Context
  240. )
  241. {
  242. //
  243. // If CheckApsStatus() is not stopped, otherwise return immediately.
  244. //
  245. if (!mStopCheckAllApsStatus) {
  246. CheckAndUpdateApsStatus ();
  247. }
  248. }
  249. /**
  250. Get Protected mode code segment with 16-bit default addressing
  251. from current GDT table.
  252. @return Protected mode 16-bit code segment value.
  253. **/
  254. UINT16
  255. GetProtectedMode16CS (
  256. VOID
  257. )
  258. {
  259. IA32_DESCRIPTOR GdtrDesc;
  260. IA32_SEGMENT_DESCRIPTOR *GdtEntry;
  261. UINTN GdtEntryCount;
  262. UINT16 Index;
  263. Index = (UINT16)-1;
  264. AsmReadGdtr (&GdtrDesc);
  265. GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
  266. GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;
  267. for (Index = 0; Index < GdtEntryCount; Index++) {
  268. if (GdtEntry->Bits.L == 0) {
  269. if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 0)) {
  270. break;
  271. }
  272. }
  273. GdtEntry++;
  274. }
  275. ASSERT (Index != GdtEntryCount);
  276. return Index * 8;
  277. }
  278. /**
  279. Get Protected mode code segment from current GDT table.
  280. @return Protected mode code segment value.
  281. **/
  282. UINT16
  283. GetProtectedModeCS (
  284. VOID
  285. )
  286. {
  287. IA32_DESCRIPTOR GdtrDesc;
  288. IA32_SEGMENT_DESCRIPTOR *GdtEntry;
  289. UINTN GdtEntryCount;
  290. UINT16 Index;
  291. AsmReadGdtr (&GdtrDesc);
  292. GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
  293. GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;
  294. for (Index = 0; Index < GdtEntryCount; Index++) {
  295. if (GdtEntry->Bits.L == 0) {
  296. if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {
  297. break;
  298. }
  299. }
  300. GdtEntry++;
  301. }
  302. ASSERT (Index != GdtEntryCount);
  303. return Index * 8;
  304. }
  305. /**
  306. Do sync on APs.
  307. @param[in, out] Buffer Pointer to private data buffer.
  308. **/
  309. VOID
  310. EFIAPI
  311. RelocateApLoop (
  312. IN OUT VOID *Buffer
  313. )
  314. {
  315. CPU_MP_DATA *CpuMpData;
  316. BOOLEAN MwaitSupport;
  317. ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;
  318. UINTN ProcessorNumber;
  319. UINTN StackStart;
  320. MpInitLibWhoAmI (&ProcessorNumber);
  321. CpuMpData = GetCpuMpData ();
  322. MwaitSupport = IsMwaitSupport ();
  323. if (CpuMpData->SevEsIsEnabled) {
  324. StackStart = CpuMpData->SevEsAPResetStackStart;
  325. } else {
  326. StackStart = mReservedTopOfApStack;
  327. }
  328. AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc;
  329. AsmRelocateApLoopFunc (
  330. MwaitSupport,
  331. CpuMpData->ApTargetCState,
  332. CpuMpData->PmCodeSegment,
  333. StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
  334. (UINTN)&mNumberToFinish,
  335. CpuMpData->Pm16CodeSegment,
  336. CpuMpData->SevEsAPBuffer,
  337. CpuMpData->WakeupBuffer
  338. );
  339. //
  340. // It should never reach here
  341. //
  342. ASSERT (FALSE);
  343. }
  344. /**
  345. Callback function for ExitBootServices.
  346. @param[in] Event Event whose notification function is being invoked.
  347. @param[in] Context The pointer to the notification function's context,
  348. which is implementation-dependent.
  349. **/
  350. VOID
  351. EFIAPI
  352. MpInitChangeApLoopCallback (
  353. IN EFI_EVENT Event,
  354. IN VOID *Context
  355. )
  356. {
  357. CPU_MP_DATA *CpuMpData;
  358. CpuMpData = GetCpuMpData ();
  359. CpuMpData->PmCodeSegment = GetProtectedModeCS ();
  360. CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();
  361. CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
  362. mNumberToFinish = CpuMpData->CpuCount - 1;
  363. WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
  364. while (mNumberToFinish > 0) {
  365. CpuPause ();
  366. }
  367. if (CpuMpData->SevEsIsEnabled && (CpuMpData->WakeupBuffer != (UINTN)-1)) {
  368. //
  369. // There are APs present. Re-use reserved memory area below 1MB from
  370. // WakeupBuffer as the area to be used for transitioning to 16-bit mode
  371. // in support of booting of the AP by an OS.
  372. //
  373. CopyMem (
  374. (VOID *)CpuMpData->WakeupBuffer,
  375. (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +
  376. CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),
  377. CpuMpData->AddressMap.SwitchToRealPM16ModeSize
  378. );
  379. }
  380. DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));
  381. }
  382. /**
  383. Initialize global data for MP support.
  384. @param[in] CpuMpData The pointer to CPU MP Data structure.
  385. **/
  386. VOID
  387. InitMpGlobalData (
  388. IN CPU_MP_DATA *CpuMpData
  389. )
  390. {
  391. EFI_STATUS Status;
  392. EFI_PHYSICAL_ADDRESS Address;
  393. UINTN ApSafeBufferSize;
  394. UINTN Index;
  395. EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
  396. UINTN StackBase;
  397. CPU_INFO_IN_HOB *CpuInfoInHob;
  398. SaveCpuMpData (CpuMpData);
  399. if (CpuMpData->CpuCount == 1) {
  400. //
  401. // If only BSP exists, return
  402. //
  403. return;
  404. }
  405. if (PcdGetBool (PcdCpuStackGuard)) {
  406. //
  407. // One extra page at the bottom of the stack is needed for Guard page.
  408. //
  409. if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {
  410. DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));
  411. ASSERT (FALSE);
  412. }
  413. //
  414. // DXE will reuse stack allocated for APs at PEI phase if it's available.
  415. // Let's check it here.
  416. //
  417. // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of
  418. // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be
  419. // set here.
  420. //
  421. CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
  422. for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {
  423. if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {
  424. StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;
  425. } else {
  426. StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;
  427. }
  428. Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);
  429. ASSERT_EFI_ERROR (Status);
  430. Status = gDS->SetMemorySpaceAttributes (
  431. StackBase,
  432. EFI_PAGES_TO_SIZE (1),
  433. MemDesc.Attributes | EFI_MEMORY_RP
  434. );
  435. ASSERT_EFI_ERROR (Status);
  436. DEBUG ((
  437. DEBUG_INFO,
  438. "Stack Guard set at %lx [cpu%lu]!\n",
  439. (UINT64)StackBase,
  440. (UINT64)Index
  441. ));
  442. }
  443. }
  444. //
  445. // Avoid APs access invalid buffer data which allocated by BootServices,
  446. // so we will allocate reserved data for AP loop code. We also need to
  447. // allocate this buffer below 4GB due to APs may be transferred to 32bit
  448. // protected mode on long mode DXE.
  449. // Allocating it in advance since memory services are not available in
  450. // Exit Boot Services callback function.
  451. //
  452. ApSafeBufferSize = EFI_PAGES_TO_SIZE (
  453. EFI_SIZE_TO_PAGES (
  454. CpuMpData->AddressMap.RelocateApLoopFuncSize
  455. )
  456. );
  457. Address = BASE_4GB - 1;
  458. Status = gBS->AllocatePages (
  459. AllocateMaxAddress,
  460. EfiReservedMemoryType,
  461. EFI_SIZE_TO_PAGES (ApSafeBufferSize),
  462. &Address
  463. );
  464. ASSERT_EFI_ERROR (Status);
  465. mReservedApLoopFunc = (VOID *)(UINTN)Address;
  466. ASSERT (mReservedApLoopFunc != NULL);
  467. //
  468. // Make sure that the buffer memory is executable if NX protection is enabled
  469. // for EfiReservedMemoryType.
  470. //
  471. // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD
  472. // service.
  473. //
  474. Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);
  475. if (!EFI_ERROR (Status)) {
  476. gDS->SetMemorySpaceAttributes (
  477. Address,
  478. ApSafeBufferSize,
  479. MemDesc.Attributes & (~EFI_MEMORY_XP)
  480. );
  481. }
  482. ApSafeBufferSize = EFI_PAGES_TO_SIZE (
  483. EFI_SIZE_TO_PAGES (
  484. CpuMpData->CpuCount * AP_SAFE_STACK_SIZE
  485. )
  486. );
  487. Address = BASE_4GB - 1;
  488. Status = gBS->AllocatePages (
  489. AllocateMaxAddress,
  490. EfiReservedMemoryType,
  491. EFI_SIZE_TO_PAGES (ApSafeBufferSize),
  492. &Address
  493. );
  494. ASSERT_EFI_ERROR (Status);
  495. mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize;
  496. ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);
  497. CopyMem (
  498. mReservedApLoopFunc,
  499. CpuMpData->AddressMap.RelocateApLoopFuncAddress,
  500. CpuMpData->AddressMap.RelocateApLoopFuncSize
  501. );
  502. Status = gBS->CreateEvent (
  503. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  504. TPL_NOTIFY,
  505. CheckApsStatus,
  506. NULL,
  507. &mCheckAllApsEvent
  508. );
  509. ASSERT_EFI_ERROR (Status);
  510. //
  511. // Set timer to check all APs status.
  512. //
  513. Status = gBS->SetTimer (
  514. mCheckAllApsEvent,
  515. TimerPeriodic,
  516. EFI_TIMER_PERIOD_MICROSECONDS (
  517. PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)
  518. )
  519. );
  520. ASSERT_EFI_ERROR (Status);
  521. Status = gBS->CreateEvent (
  522. EVT_SIGNAL_EXIT_BOOT_SERVICES,
  523. TPL_CALLBACK,
  524. MpInitChangeApLoopCallback,
  525. NULL,
  526. &mMpInitExitBootServicesEvent
  527. );
  528. ASSERT_EFI_ERROR (Status);
  529. Status = gBS->CreateEventEx (
  530. EVT_NOTIFY_SIGNAL,
  531. TPL_CALLBACK,
  532. MpInitChangeApLoopCallback,
  533. NULL,
  534. &gEfiEventLegacyBootGuid,
  535. &mLegacyBootEvent
  536. );
  537. ASSERT_EFI_ERROR (Status);
  538. }
  539. /**
  540. This service executes a caller provided function on all enabled APs.
  541. @param[in] Procedure A pointer to the function to be run on
  542. enabled APs of the system. See type
  543. EFI_AP_PROCEDURE.
  544. @param[in] SingleThread If TRUE, then all the enabled APs execute
  545. the function specified by Procedure one by
  546. one, in ascending order of processor handle
  547. number. If FALSE, then all the enabled APs
  548. execute the function specified by Procedure
  549. simultaneously.
  550. @param[in] WaitEvent The event created by the caller with CreateEvent()
  551. service. If it is NULL, then execute in
  552. blocking mode. BSP waits until all APs finish
  553. or TimeoutInMicroSeconds expires. If it's
  554. not NULL, then execute in non-blocking mode.
  555. BSP requests the function specified by
  556. Procedure to be started on all the enabled
  557. APs, and go on executing immediately. If
  558. all return from Procedure, or TimeoutInMicroSeconds
  559. expires, this event is signaled. The BSP
  560. can use the CheckEvent() or WaitForEvent()
  561. services to check the state of event. Type
  562. EFI_EVENT is defined in CreateEvent() in
  563. the Unified Extensible Firmware Interface
  564. Specification.
  565. @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
  566. APs to return from Procedure, either for
  567. blocking or non-blocking mode. Zero means
  568. infinity. If the timeout expires before
  569. all APs return from Procedure, then Procedure
  570. on the failed APs is terminated. All enabled
  571. APs are available for next function assigned
  572. by MpInitLibStartupAllAPs() or
  573. MPInitLibStartupThisAP().
  574. If the timeout expires in blocking mode,
  575. BSP returns EFI_TIMEOUT. If the timeout
  576. expires in non-blocking mode, WaitEvent
  577. is signaled with SignalEvent().
  578. @param[in] ProcedureArgument The parameter passed into Procedure for
  579. all APs.
  580. @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
  581. if all APs finish successfully, then its
  582. content is set to NULL. If not all APs
  583. finish before timeout expires, then its
  584. content is set to address of the buffer
  585. holding handle numbers of the failed APs.
  586. The buffer is allocated by MP Initialization
  587. library, and it's the caller's responsibility to
  588. free the buffer with FreePool() service.
  589. In blocking mode, it is ready for consumption
  590. when the call returns. In non-blocking mode,
  591. it is ready when WaitEvent is signaled. The
  592. list of failed CPU is terminated by
  593. END_OF_CPU_LIST.
  594. @retval EFI_SUCCESS In blocking mode, all APs have finished before
  595. the timeout expired.
  596. @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
  597. to all enabled APs.
  598. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  599. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  600. signaled.
  601. @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
  602. supported.
  603. @retval EFI_DEVICE_ERROR Caller processor is AP.
  604. @retval EFI_NOT_STARTED No enabled APs exist in the system.
  605. @retval EFI_NOT_READY Any enabled APs are busy.
  606. @retval EFI_NOT_READY MP Initialize Library is not initialized.
  607. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  608. all enabled APs have finished.
  609. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  610. **/
  611. EFI_STATUS
  612. EFIAPI
  613. MpInitLibStartupAllAPs (
  614. IN EFI_AP_PROCEDURE Procedure,
  615. IN BOOLEAN SingleThread,
  616. IN EFI_EVENT WaitEvent OPTIONAL,
  617. IN UINTN TimeoutInMicroseconds,
  618. IN VOID *ProcedureArgument OPTIONAL,
  619. OUT UINTN **FailedCpuList OPTIONAL
  620. )
  621. {
  622. EFI_STATUS Status;
  623. //
  624. // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
  625. //
  626. mStopCheckAllApsStatus = TRUE;
  627. Status = StartupAllCPUsWorker (
  628. Procedure,
  629. SingleThread,
  630. TRUE,
  631. WaitEvent,
  632. TimeoutInMicroseconds,
  633. ProcedureArgument,
  634. FailedCpuList
  635. );
  636. //
  637. // Start checkAllApsStatus
  638. //
  639. mStopCheckAllApsStatus = FALSE;
  640. return Status;
  641. }
  642. /**
  643. This service lets the caller get one enabled AP to execute a caller-provided
  644. function.
  645. @param[in] Procedure A pointer to the function to be run on the
  646. designated AP of the system. See type
  647. EFI_AP_PROCEDURE.
  648. @param[in] ProcessorNumber The handle number of the AP. The range is
  649. from 0 to the total number of logical
  650. processors minus 1. The total number of
  651. logical processors can be retrieved by
  652. MpInitLibGetNumberOfProcessors().
  653. @param[in] WaitEvent The event created by the caller with CreateEvent()
  654. service. If it is NULL, then execute in
  655. blocking mode. BSP waits until this AP finish
  656. or TimeoutInMicroSeconds expires. If it's
  657. not NULL, then execute in non-blocking mode.
  658. BSP requests the function specified by
  659. Procedure to be started on this AP,
  660. and go on executing immediately. If this AP
  661. return from Procedure or TimeoutInMicroSeconds
  662. expires, this event is signaled. The BSP
  663. can use the CheckEvent() or WaitForEvent()
  664. services to check the state of event. Type
  665. EFI_EVENT is defined in CreateEvent() in
  666. the Unified Extensible Firmware Interface
  667. Specification.
  668. @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
  669. this AP to finish this Procedure, either for
  670. blocking or non-blocking mode. Zero means
  671. infinity. If the timeout expires before
  672. this AP returns from Procedure, then Procedure
  673. on the AP is terminated. The
  674. AP is available for next function assigned
  675. by MpInitLibStartupAllAPs() or
  676. MpInitLibStartupThisAP().
  677. If the timeout expires in blocking mode,
  678. BSP returns EFI_TIMEOUT. If the timeout
  679. expires in non-blocking mode, WaitEvent
  680. is signaled with SignalEvent().
  681. @param[in] ProcedureArgument The parameter passed into Procedure on the
  682. specified AP.
  683. @param[out] Finished If NULL, this parameter is ignored. In
  684. blocking mode, this parameter is ignored.
  685. In non-blocking mode, if AP returns from
  686. Procedure before the timeout expires, its
  687. content is set to TRUE. Otherwise, the
  688. value is set to FALSE. The caller can
  689. determine if the AP returned from Procedure
  690. by evaluating this value.
  691. @retval EFI_SUCCESS In blocking mode, specified AP finished before
  692. the timeout expires.
  693. @retval EFI_SUCCESS In non-blocking mode, the function has been
  694. dispatched to specified AP.
  695. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  696. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  697. signaled.
  698. @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
  699. supported.
  700. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  701. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  702. the specified AP has finished.
  703. @retval EFI_NOT_READY The specified AP is busy.
  704. @retval EFI_NOT_READY MP Initialize Library is not initialized.
  705. @retval EFI_NOT_FOUND The processor with the handle specified by
  706. ProcessorNumber does not exist.
  707. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
  708. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  709. **/
  710. EFI_STATUS
  711. EFIAPI
  712. MpInitLibStartupThisAP (
  713. IN EFI_AP_PROCEDURE Procedure,
  714. IN UINTN ProcessorNumber,
  715. IN EFI_EVENT WaitEvent OPTIONAL,
  716. IN UINTN TimeoutInMicroseconds,
  717. IN VOID *ProcedureArgument OPTIONAL,
  718. OUT BOOLEAN *Finished OPTIONAL
  719. )
  720. {
  721. EFI_STATUS Status;
  722. //
  723. // temporarily stop checkAllApsStatus for avoid resource dead-lock.
  724. //
  725. mStopCheckAllApsStatus = TRUE;
  726. Status = StartupThisAPWorker (
  727. Procedure,
  728. ProcessorNumber,
  729. WaitEvent,
  730. TimeoutInMicroseconds,
  731. ProcedureArgument,
  732. Finished
  733. );
  734. mStopCheckAllApsStatus = FALSE;
  735. return Status;
  736. }
  737. /**
  738. This service switches the requested AP to be the BSP from that point onward.
  739. This service changes the BSP for all purposes. This call can only be performed
  740. by the current BSP.
  741. @param[in] ProcessorNumber The handle number of AP that is to become the new
  742. BSP. The range is from 0 to the total number of
  743. logical processors minus 1. The total number of
  744. logical processors can be retrieved by
  745. MpInitLibGetNumberOfProcessors().
  746. @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
  747. enabled AP. Otherwise, it will be disabled.
  748. @retval EFI_SUCCESS BSP successfully switched.
  749. @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
  750. this service returning.
  751. @retval EFI_UNSUPPORTED Switching the BSP is not supported.
  752. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  753. @retval EFI_NOT_FOUND The processor with the handle specified by
  754. ProcessorNumber does not exist.
  755. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
  756. a disabled AP.
  757. @retval EFI_NOT_READY The specified AP is busy.
  758. @retval EFI_NOT_READY MP Initialize Library is not initialized.
  759. **/
  760. EFI_STATUS
  761. EFIAPI
  762. MpInitLibSwitchBSP (
  763. IN UINTN ProcessorNumber,
  764. IN BOOLEAN EnableOldBSP
  765. )
  766. {
  767. EFI_STATUS Status;
  768. EFI_TIMER_ARCH_PROTOCOL *Timer;
  769. UINT64 TimerPeriod;
  770. TimerPeriod = 0;
  771. //
  772. // Locate Timer Arch Protocol
  773. //
  774. Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);
  775. if (EFI_ERROR (Status)) {
  776. Timer = NULL;
  777. }
  778. if (Timer != NULL) {
  779. //
  780. // Save current rate of DXE Timer
  781. //
  782. Timer->GetTimerPeriod (Timer, &TimerPeriod);
  783. //
  784. // Disable DXE Timer and drain pending interrupts
  785. //
  786. Timer->SetTimerPeriod (Timer, 0);
  787. }
  788. Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
  789. if (Timer != NULL) {
  790. //
  791. // Enable and restore rate of DXE Timer
  792. //
  793. Timer->SetTimerPeriod (Timer, TimerPeriod);
  794. }
  795. return Status;
  796. }
  797. /**
  798. This service lets the caller enable or disable an AP from this point onward.
  799. This service may only be called from the BSP.
  800. @param[in] ProcessorNumber The handle number of AP.
  801. The range is from 0 to the total number of
  802. logical processors minus 1. The total number of
  803. logical processors can be retrieved by
  804. MpInitLibGetNumberOfProcessors().
  805. @param[in] EnableAP Specifies the new state for the processor for
  806. enabled, FALSE for disabled.
  807. @param[in] HealthFlag If not NULL, a pointer to a value that specifies
  808. the new health status of the AP. This flag
  809. corresponds to StatusFlag defined in
  810. EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
  811. the PROCESSOR_HEALTH_STATUS_BIT is used. All other
  812. bits are ignored. If it is NULL, this parameter
  813. is ignored.
  814. @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
  815. @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
  816. prior to this service returning.
  817. @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
  818. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  819. @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
  820. does not exist.
  821. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
  822. @retval EFI_NOT_READY MP Initialize Library is not initialized.
  823. **/
  824. EFI_STATUS
  825. EFIAPI
  826. MpInitLibEnableDisableAP (
  827. IN UINTN ProcessorNumber,
  828. IN BOOLEAN EnableAP,
  829. IN UINT32 *HealthFlag OPTIONAL
  830. )
  831. {
  832. EFI_STATUS Status;
  833. BOOLEAN TempStopCheckState;
  834. TempStopCheckState = FALSE;
  835. //
  836. // temporarily stop checkAllAPsStatus for initialize parameters.
  837. //
  838. if (!mStopCheckAllApsStatus) {
  839. mStopCheckAllApsStatus = TRUE;
  840. TempStopCheckState = TRUE;
  841. }
  842. Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
  843. if (TempStopCheckState) {
  844. mStopCheckAllApsStatus = FALSE;
  845. }
  846. return Status;
  847. }
  848. /**
  849. This funtion will try to invoke platform specific microcode shadow logic to
  850. relocate microcode update patches into memory.
  851. @param[in, out] CpuMpData The pointer to CPU MP Data structure.
  852. @retval EFI_SUCCESS Shadow microcode success.
  853. @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
  854. @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
  855. PPI/Protocol.
  856. **/
  857. EFI_STATUS
  858. PlatformShadowMicrocode (
  859. IN OUT CPU_MP_DATA *CpuMpData
  860. )
  861. {
  862. //
  863. // There is no DXE version of platform shadow microcode protocol so far.
  864. // A platform which only uses DxeMpInitLib instance could only supports
  865. // the PCD based microcode shadowing.
  866. //
  867. return EFI_UNSUPPORTED;
  868. }