MpService.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /** @file
  2. Construct MP Services Protocol on top of the EMU Thread protocol.
  3. This code makes APs show up in the emulator. PcdEmuApCount is the
  4. number of APs the emulator should produce.
  5. The MP Services Protocol provides a generalized way of performing following tasks:
  6. - Retrieving information of multi-processor environment and MP-related status of
  7. specific processors.
  8. - Dispatching user-provided function to APs.
  9. - Maintain MP-related processor status.
  10. The MP Services Protocol must be produced on any system with more than one logical
  11. processor.
  12. The Protocol is available only during boot time.
  13. MP Services Protocol is hardware-independent. Most of the logic of this protocol
  14. is architecturally neutral. It abstracts the multi-processor environment and
  15. status of processors, and provides interfaces to retrieve information, maintain,
  16. and dispatch.
  17. MP Services Protocol may be consumed by ACPI module. The ACPI module may use this
  18. protocol to retrieve data that are needed for an MP platform and report them to OS.
  19. MP Services Protocol may also be used to program and configure processors, such
  20. as MTRR synchronization for memory space attributes setting in DXE Services.
  21. MP Services Protocol may be used by non-CPU DXE drivers to speed up platform boot
  22. by taking advantage of the processing capabilities of the APs, for example, using
  23. APs to help test system memory in parallel with other device initialization.
  24. Diagnostics applications may also use this protocol for multi-processor.
  25. Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
  26. Portitions Copyright (c) 2011, Apple Inc. All rights reserved.
  27. SPDX-License-Identifier: BSD-2-Clause-Patent
  28. **/
  29. #include "CpuDriver.h"
  30. MP_SYSTEM_DATA gMPSystem;
  31. EMU_THREAD_THUNK_PROTOCOL *gThread = NULL;
  32. EFI_EVENT gReadToBootEvent;
  33. BOOLEAN gReadToBoot = FALSE;
  34. UINTN gPollInterval;
  35. BOOLEAN
  36. IsBSP (
  37. VOID
  38. )
  39. {
  40. EFI_STATUS Status;
  41. UINTN ProcessorNumber;
  42. Status = CpuMpServicesWhoAmI (&mMpServicesTemplate, &ProcessorNumber);
  43. if (EFI_ERROR (Status)) {
  44. return FALSE;
  45. }
  46. return (gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0;
  47. }
  48. VOID
  49. SetApProcedure (
  50. IN PROCESSOR_DATA_BLOCK *Processor,
  51. IN EFI_AP_PROCEDURE Procedure,
  52. IN VOID *ProcedureArgument
  53. )
  54. {
  55. gThread->MutexLock (Processor->ProcedureLock);
  56. Processor->Parameter = ProcedureArgument;
  57. Processor->Procedure = Procedure;
  58. gThread->MutexUnlock (Processor->ProcedureLock);
  59. }
  60. EFI_STATUS
  61. GetNextBlockedNumber (
  62. OUT UINTN *NextNumber
  63. )
  64. {
  65. UINTN Number;
  66. PROCESSOR_STATE ProcessorState;
  67. PROCESSOR_DATA_BLOCK *Data;
  68. for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
  69. Data = &gMPSystem.ProcessorData[Number];
  70. if ((Data->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
  71. // Skip BSP
  72. continue;
  73. }
  74. gThread->MutexLock (Data->StateLock);
  75. ProcessorState = Data->State;
  76. gThread->MutexUnlock (Data->StateLock);
  77. if (ProcessorState == CPU_STATE_BLOCKED) {
  78. *NextNumber = Number;
  79. return EFI_SUCCESS;
  80. }
  81. }
  82. return EFI_NOT_FOUND;
  83. }
  84. /**
  85. * Calculated and stalled the interval time by BSP to check whether
  86. * the APs have finished.
  87. *
  88. * @param[in] Timeout The time limit in microseconds for
  89. * APs to return from Procedure.
  90. *
  91. * @retval StallTime Time of execution stall.
  92. **/
  93. UINTN
  94. CalculateAndStallInterval (
  95. IN UINTN Timeout
  96. )
  97. {
  98. UINTN StallTime;
  99. if ((Timeout < gPollInterval) && (Timeout != 0)) {
  100. StallTime = Timeout;
  101. } else {
  102. StallTime = gPollInterval;
  103. }
  104. gBS->Stall (StallTime);
  105. return StallTime;
  106. }
  107. /**
  108. This service retrieves the number of logical processor in the platform
  109. and the number of those logical processors that are enabled on this boot.
  110. This service may only be called from the BSP.
  111. This function is used to retrieve the following information:
  112. - The number of logical processors that are present in the system.
  113. - The number of enabled logical processors in the system at the instant
  114. this call is made.
  115. Because MP Service Protocol provides services to enable and disable processors
  116. dynamically, the number of enabled logical processors may vary during the
  117. course of a boot session.
  118. If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
  119. If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
  120. EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
  121. is returned in NumberOfProcessors, the number of currently enabled processor
  122. is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
  123. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  124. instance.
  125. @param[out] NumberOfProcessors Pointer to the total number of logical
  126. processors in the system, including the BSP
  127. and disabled APs.
  128. @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
  129. processors that exist in system, including
  130. the BSP.
  131. @retval EFI_SUCCESS The number of logical processors and enabled
  132. logical processors was retrieved.
  133. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  134. @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
  135. @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
  136. **/
  137. EFI_STATUS
  138. EFIAPI
  139. CpuMpServicesGetNumberOfProcessors (
  140. IN EFI_MP_SERVICES_PROTOCOL *This,
  141. OUT UINTN *NumberOfProcessors,
  142. OUT UINTN *NumberOfEnabledProcessors
  143. )
  144. {
  145. if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
  146. return EFI_INVALID_PARAMETER;
  147. }
  148. if (!IsBSP ()) {
  149. return EFI_DEVICE_ERROR;
  150. }
  151. *NumberOfProcessors = gMPSystem.NumberOfProcessors;
  152. *NumberOfEnabledProcessors = gMPSystem.NumberOfEnabledProcessors;
  153. return EFI_SUCCESS;
  154. }
  155. /**
  156. Gets detailed MP-related information on the requested processor at the
  157. instant this call is made. This service may only be called from the BSP.
  158. This service retrieves detailed MP-related information about any processor
  159. on the platform. Note the following:
  160. - The processor information may change during the course of a boot session.
  161. - The information presented here is entirely MP related.
  162. Information regarding the number of caches and their sizes, frequency of operation,
  163. slot numbers is all considered platform-related information and is not provided
  164. by this service.
  165. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  166. instance.
  167. @param[in] ProcessorNumber The handle number of processor.
  168. @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
  169. the requested processor is deposited.
  170. @retval EFI_SUCCESS Processor information was returned.
  171. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  172. @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
  173. @retval EFI_NOT_FOUND The processor with the handle specified by
  174. ProcessorNumber does not exist in the platform.
  175. **/
  176. EFI_STATUS
  177. EFIAPI
  178. CpuMpServicesGetProcessorInfo (
  179. IN EFI_MP_SERVICES_PROTOCOL *This,
  180. IN UINTN ProcessorNumber,
  181. OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
  182. )
  183. {
  184. if (ProcessorInfoBuffer == NULL) {
  185. return EFI_INVALID_PARAMETER;
  186. }
  187. if (!IsBSP ()) {
  188. return EFI_DEVICE_ERROR;
  189. }
  190. if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
  191. return EFI_NOT_FOUND;
  192. }
  193. CopyMem (ProcessorInfoBuffer, &gMPSystem.ProcessorData[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
  194. return EFI_SUCCESS;
  195. }
  196. /**
  197. This service executes a caller provided function on all enabled APs. APs can
  198. run either simultaneously or one at a time in sequence. This service supports
  199. both blocking and non-blocking requests. The non-blocking requests use EFI
  200. events so the BSP can detect when the APs have finished. This service may only
  201. be called from the BSP.
  202. This function is used to dispatch all the enabled APs to the function specified
  203. by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
  204. immediately and Procedure is not started on any AP.
  205. If SingleThread is TRUE, all the enabled APs execute the function specified by
  206. Procedure one by one, in ascending order of processor handle number. Otherwise,
  207. all the enabled APs execute the function specified by Procedure simultaneously.
  208. If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
  209. APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
  210. mode, and the BSP returns from this service without waiting for APs. If a
  211. non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
  212. is signaled, then EFI_UNSUPPORTED must be returned.
  213. If the timeout specified by TimeoutInMicroseconds expires before all APs return
  214. from Procedure, then Procedure on the failed APs is terminated. All enabled APs
  215. are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  216. and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
  217. content points to the list of processor handle numbers in which Procedure was
  218. terminated.
  219. Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  220. to make sure that the nature of the code that is executed on the BSP and the
  221. dispatched APs is well controlled. The MP Services Protocol does not guarantee
  222. that the Procedure function is MP-safe. Hence, the tasks that can be run in
  223. parallel are limited to certain independent tasks and well-controlled exclusive
  224. code. EFI services and protocols may not be called by APs unless otherwise
  225. specified.
  226. In blocking execution mode, BSP waits until all APs finish or
  227. TimeoutInMicroseconds expires.
  228. In non-blocking execution mode, BSP is freed to return to the caller and then
  229. proceed to the next task without having to wait for APs. The following
  230. sequence needs to occur in a non-blocking execution mode:
  231. -# The caller that intends to use this MP Services Protocol in non-blocking
  232. mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
  233. invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
  234. is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
  235. the function specified by Procedure to be started on all the enabled APs,
  236. and releases the BSP to continue with other tasks.
  237. -# The caller can use the CheckEvent() and WaitForEvent() services to check
  238. the state of the WaitEvent created in step 1.
  239. -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
  240. Service signals WaitEvent by calling the EFI SignalEvent() function. If
  241. FailedCpuList is not NULL, its content is available when WaitEvent is
  242. signaled. If all APs returned from Procedure prior to the timeout, then
  243. FailedCpuList is set to NULL. If not all APs return from Procedure before
  244. the timeout, then FailedCpuList is filled in with the list of the failed
  245. APs. The buffer is allocated by MP Service Protocol using AllocatePool().
  246. It is the caller's responsibility to free the buffer with FreePool() service.
  247. -# This invocation of SignalEvent() function informs the caller that invoked
  248. EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
  249. the specified task or a timeout occurred. The contents of FailedCpuList
  250. can be examined to determine which APs did not complete the specified task
  251. prior to the timeout.
  252. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  253. instance.
  254. @param[in] Procedure A pointer to the function to be run on
  255. enabled APs of the system. See type
  256. EFI_AP_PROCEDURE.
  257. @param[in] SingleThread If TRUE, then all the enabled APs execute
  258. the function specified by Procedure one by
  259. one, in ascending order of processor handle
  260. number. If FALSE, then all the enabled APs
  261. execute the function specified by Procedure
  262. simultaneously.
  263. @param[in] WaitEvent The event created by the caller with CreateEvent()
  264. service. If it is NULL, then execute in
  265. blocking mode. BSP waits until all APs finish
  266. or TimeoutInMicroseconds expires. If it's
  267. not NULL, then execute in non-blocking mode.
  268. BSP requests the function specified by
  269. Procedure to be started on all the enabled
  270. APs, and go on executing immediately. If
  271. all return from Procedure, or TimeoutInMicroseconds
  272. expires, this event is signaled. The BSP
  273. can use the CheckEvent() or WaitForEvent()
  274. services to check the state of event. Type
  275. EFI_EVENT is defined in CreateEvent() in
  276. the Unified Extensible Firmware Interface
  277. Specification.
  278. @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
  279. APs to return from Procedure, either for
  280. blocking or non-blocking mode. Zero means
  281. infinity. If the timeout expires before
  282. all APs return from Procedure, then Procedure
  283. on the failed APs is terminated. All enabled
  284. APs are available for next function assigned
  285. by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  286. or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  287. If the timeout expires in blocking mode,
  288. BSP returns EFI_TIMEOUT. If the timeout
  289. expires in non-blocking mode, WaitEvent
  290. is signaled with SignalEvent().
  291. @param[in] ProcedureArgument The parameter passed into Procedure for
  292. all APs.
  293. @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
  294. if all APs finish successfully, then its
  295. content is set to NULL. If not all APs
  296. finish before timeout expires, then its
  297. content is set to address of the buffer
  298. holding handle numbers of the failed APs.
  299. The buffer is allocated by MP Service Protocol,
  300. and it's the caller's responsibility to
  301. free the buffer with FreePool() service.
  302. In blocking mode, it is ready for consumption
  303. when the call returns. In non-blocking mode,
  304. it is ready when WaitEvent is signaled. The
  305. list of failed CPU is terminated by
  306. END_OF_CPU_LIST.
  307. @retval EFI_SUCCESS In blocking mode, all APs have finished before
  308. the timeout expired.
  309. @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
  310. to all enabled APs.
  311. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  312. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  313. signaled.
  314. @retval EFI_DEVICE_ERROR Caller processor is AP.
  315. @retval EFI_NOT_STARTED No enabled APs exist in the system.
  316. @retval EFI_NOT_READY Any enabled APs are busy.
  317. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  318. all enabled APs have finished.
  319. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  320. **/
  321. EFI_STATUS
  322. EFIAPI
  323. CpuMpServicesStartupAllAps (
  324. IN EFI_MP_SERVICES_PROTOCOL *This,
  325. IN EFI_AP_PROCEDURE Procedure,
  326. IN BOOLEAN SingleThread,
  327. IN EFI_EVENT WaitEvent OPTIONAL,
  328. IN UINTN TimeoutInMicroseconds,
  329. IN VOID *ProcedureArgument OPTIONAL,
  330. OUT UINTN **FailedCpuList OPTIONAL
  331. )
  332. {
  333. EFI_STATUS Status;
  334. PROCESSOR_DATA_BLOCK *ProcessorData;
  335. UINTN Number;
  336. UINTN NextNumber;
  337. PROCESSOR_STATE APInitialState;
  338. PROCESSOR_STATE ProcessorState;
  339. UINTN Timeout;
  340. if (!IsBSP ()) {
  341. return EFI_DEVICE_ERROR;
  342. }
  343. if (gMPSystem.NumberOfProcessors == 1) {
  344. return EFI_NOT_STARTED;
  345. }
  346. if (Procedure == NULL) {
  347. return EFI_INVALID_PARAMETER;
  348. }
  349. if ((WaitEvent != NULL) && gReadToBoot) {
  350. return EFI_UNSUPPORTED;
  351. }
  352. for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
  353. ProcessorData = &gMPSystem.ProcessorData[Number];
  354. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  355. // Skip BSP
  356. continue;
  357. }
  358. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  359. // Skip Disabled processors
  360. continue;
  361. }
  362. gThread->MutexLock (ProcessorData->StateLock);
  363. if (ProcessorData->State != CPU_STATE_IDLE) {
  364. gThread->MutexUnlock (ProcessorData->StateLock);
  365. return EFI_NOT_READY;
  366. }
  367. gThread->MutexUnlock (ProcessorData->StateLock);
  368. }
  369. if (FailedCpuList != NULL) {
  370. gMPSystem.FailedList = AllocatePool ((gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN));
  371. if (gMPSystem.FailedList == NULL) {
  372. return EFI_OUT_OF_RESOURCES;
  373. }
  374. SetMemN (gMPSystem.FailedList, (gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN), END_OF_CPU_LIST);
  375. gMPSystem.FailedListIndex = 0;
  376. *FailedCpuList = gMPSystem.FailedList;
  377. }
  378. Timeout = TimeoutInMicroseconds;
  379. ProcessorData = NULL;
  380. gMPSystem.FinishCount = 0;
  381. gMPSystem.StartCount = 0;
  382. gMPSystem.SingleThread = SingleThread;
  383. APInitialState = CPU_STATE_READY;
  384. for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
  385. ProcessorData = &gMPSystem.ProcessorData[Number];
  386. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  387. // Skip BSP
  388. continue;
  389. }
  390. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  391. // Skip Disabled processors
  392. gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Number;
  393. continue;
  394. }
  395. //
  396. // Get APs prepared, and put failing APs into FailedCpuList
  397. // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
  398. // state 1 by 1, until the previous 1 finished its task
  399. // if not "SingleThread", all APs are put to ready state from the beginning
  400. //
  401. gThread->MutexLock (ProcessorData->StateLock);
  402. ASSERT (ProcessorData->State == CPU_STATE_IDLE);
  403. ProcessorData->State = APInitialState;
  404. gThread->MutexUnlock (ProcessorData->StateLock);
  405. gMPSystem.StartCount++;
  406. if (SingleThread) {
  407. APInitialState = CPU_STATE_BLOCKED;
  408. }
  409. }
  410. if (WaitEvent != NULL) {
  411. for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
  412. ProcessorData = &gMPSystem.ProcessorData[Number];
  413. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  414. // Skip BSP
  415. continue;
  416. }
  417. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  418. // Skip Disabled processors
  419. continue;
  420. }
  421. gThread->MutexLock (ProcessorData->StateLock);
  422. ProcessorState = ProcessorData->State;
  423. gThread->MutexUnlock (ProcessorData->StateLock);
  424. if (ProcessorState == CPU_STATE_READY) {
  425. SetApProcedure (ProcessorData, Procedure, ProcedureArgument);
  426. }
  427. }
  428. //
  429. // Save data into private data structure, and create timer to poll AP state before exiting
  430. //
  431. gMPSystem.Procedure = Procedure;
  432. gMPSystem.ProcedureArgument = ProcedureArgument;
  433. gMPSystem.WaitEvent = WaitEvent;
  434. gMPSystem.Timeout = TimeoutInMicroseconds;
  435. gMPSystem.TimeoutActive = (BOOLEAN)(TimeoutInMicroseconds != 0);
  436. Status = gBS->SetTimer (
  437. gMPSystem.CheckAllAPsEvent,
  438. TimerPeriodic,
  439. gPollInterval
  440. );
  441. return Status;
  442. }
  443. while (TRUE) {
  444. for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
  445. ProcessorData = &gMPSystem.ProcessorData[Number];
  446. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  447. // Skip BSP
  448. continue;
  449. }
  450. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  451. // Skip Disabled processors
  452. continue;
  453. }
  454. gThread->MutexLock (ProcessorData->StateLock);
  455. ProcessorState = ProcessorData->State;
  456. gThread->MutexUnlock (ProcessorData->StateLock);
  457. switch (ProcessorState) {
  458. case CPU_STATE_READY:
  459. SetApProcedure (ProcessorData, Procedure, ProcedureArgument);
  460. break;
  461. case CPU_STATE_FINISHED:
  462. gMPSystem.FinishCount++;
  463. if (SingleThread) {
  464. Status = GetNextBlockedNumber (&NextNumber);
  465. if (!EFI_ERROR (Status)) {
  466. gThread->MutexLock (gMPSystem.ProcessorData[NextNumber].StateLock);
  467. gMPSystem.ProcessorData[NextNumber].State = CPU_STATE_READY;
  468. gThread->MutexUnlock (gMPSystem.ProcessorData[NextNumber].StateLock);
  469. }
  470. }
  471. gThread->MutexLock (ProcessorData->StateLock);
  472. ProcessorData->State = CPU_STATE_IDLE;
  473. gThread->MutexUnlock (ProcessorData->StateLock);
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. if (gMPSystem.FinishCount == gMPSystem.StartCount) {
  480. Status = EFI_SUCCESS;
  481. goto Done;
  482. }
  483. if ((TimeoutInMicroseconds != 0) && (Timeout == 0)) {
  484. Status = EFI_TIMEOUT;
  485. goto Done;
  486. }
  487. Timeout -= CalculateAndStallInterval (Timeout);
  488. }
  489. Done:
  490. if (FailedCpuList != NULL) {
  491. if (gMPSystem.FailedListIndex == 0) {
  492. FreePool (*FailedCpuList);
  493. *FailedCpuList = NULL;
  494. }
  495. }
  496. return EFI_SUCCESS;
  497. }
  498. /**
  499. This service lets the caller get one enabled AP to execute a caller-provided
  500. function. The caller can request the BSP to either wait for the completion
  501. of the AP or just proceed with the next task by using the EFI event mechanism.
  502. See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
  503. execution support. This service may only be called from the BSP.
  504. This function is used to dispatch one enabled AP to the function specified by
  505. Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
  506. is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
  507. TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
  508. BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
  509. is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
  510. then EFI_UNSUPPORTED must be returned.
  511. If the timeout specified by TimeoutInMicroseconds expires before the AP returns
  512. from Procedure, then execution of Procedure by the AP is terminated. The AP is
  513. available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
  514. EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  515. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  516. instance.
  517. @param[in] Procedure A pointer to the function to be run on
  518. enabled APs of the system. See type
  519. EFI_AP_PROCEDURE.
  520. @param[in] ProcessorNumber The handle number of the AP. The range is
  521. from 0 to the total number of logical
  522. processors minus 1. The total number of
  523. logical processors can be retrieved by
  524. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  525. @param[in] WaitEvent The event created by the caller with CreateEvent()
  526. service. If it is NULL, then execute in
  527. blocking mode. BSP waits until all APs finish
  528. or TimeoutInMicroseconds expires. If it's
  529. not NULL, then execute in non-blocking mode.
  530. BSP requests the function specified by
  531. Procedure to be started on all the enabled
  532. APs, and go on executing immediately. If
  533. all return from Procedure or TimeoutInMicroseconds
  534. expires, this event is signaled. The BSP
  535. can use the CheckEvent() or WaitForEvent()
  536. services to check the state of event. Type
  537. EFI_EVENT is defined in CreateEvent() in
  538. the Unified Extensible Firmware Interface
  539. Specification.
  540. @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
  541. APs to return from Procedure, either for
  542. blocking or non-blocking mode. Zero means
  543. infinity. If the timeout expires before
  544. all APs return from Procedure, then Procedure
  545. on the failed APs is terminated. All enabled
  546. APs are available for next function assigned
  547. by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  548. or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  549. If the timeout expires in blocking mode,
  550. BSP returns EFI_TIMEOUT. If the timeout
  551. expires in non-blocking mode, WaitEvent
  552. is signaled with SignalEvent().
  553. @param[in] ProcedureArgument The parameter passed into Procedure for
  554. all APs.
  555. @param[out] Finished If NULL, this parameter is ignored. In
  556. blocking mode, this parameter is ignored.
  557. In non-blocking mode, if AP returns from
  558. Procedure before the timeout expires, its
  559. content is set to TRUE. Otherwise, the
  560. value is set to FALSE. The caller can
  561. determine if the AP returned from Procedure
  562. by evaluating this value.
  563. @retval EFI_SUCCESS In blocking mode, specified AP finished before
  564. the timeout expires.
  565. @retval EFI_SUCCESS In non-blocking mode, the function has been
  566. dispatched to specified AP.
  567. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  568. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  569. signaled.
  570. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  571. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  572. the specified AP has finished.
  573. @retval EFI_NOT_READY The specified AP is busy.
  574. @retval EFI_NOT_FOUND The processor with the handle specified by
  575. ProcessorNumber does not exist.
  576. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
  577. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  578. **/
  579. EFI_STATUS
  580. EFIAPI
  581. CpuMpServicesStartupThisAP (
  582. IN EFI_MP_SERVICES_PROTOCOL *This,
  583. IN EFI_AP_PROCEDURE Procedure,
  584. IN UINTN ProcessorNumber,
  585. IN EFI_EVENT WaitEvent OPTIONAL,
  586. IN UINTN TimeoutInMicroseconds,
  587. IN VOID *ProcedureArgument OPTIONAL,
  588. OUT BOOLEAN *Finished OPTIONAL
  589. )
  590. {
  591. UINTN Timeout;
  592. if (!IsBSP ()) {
  593. return EFI_DEVICE_ERROR;
  594. }
  595. if (Procedure == NULL) {
  596. return EFI_INVALID_PARAMETER;
  597. }
  598. if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
  599. return EFI_NOT_FOUND;
  600. }
  601. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
  602. return EFI_INVALID_PARAMETER;
  603. }
  604. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  605. return EFI_INVALID_PARAMETER;
  606. }
  607. gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  608. if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
  609. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  610. return EFI_NOT_READY;
  611. }
  612. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  613. if ((WaitEvent != NULL) && gReadToBoot) {
  614. return EFI_UNSUPPORTED;
  615. }
  616. Timeout = TimeoutInMicroseconds;
  617. gMPSystem.StartCount = 1;
  618. gMPSystem.FinishCount = 0;
  619. SetApProcedure (&gMPSystem.ProcessorData[ProcessorNumber], Procedure, ProcedureArgument);
  620. if (WaitEvent != NULL) {
  621. // Non Blocking
  622. gMPSystem.WaitEvent = WaitEvent;
  623. gBS->SetTimer (
  624. gMPSystem.ProcessorData[ProcessorNumber].CheckThisAPEvent,
  625. TimerPeriodic,
  626. gPollInterval
  627. );
  628. return EFI_SUCCESS;
  629. }
  630. // Blocking
  631. while (TRUE) {
  632. gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  633. if (gMPSystem.ProcessorData[ProcessorNumber].State == CPU_STATE_FINISHED) {
  634. gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;
  635. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  636. break;
  637. }
  638. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  639. if ((TimeoutInMicroseconds != 0) && (Timeout == 0)) {
  640. return EFI_TIMEOUT;
  641. }
  642. Timeout -= CalculateAndStallInterval (Timeout);
  643. }
  644. return EFI_SUCCESS;
  645. }
  646. /**
  647. This service switches the requested AP to be the BSP from that point onward.
  648. This service changes the BSP for all purposes. This call can only be performed
  649. by the current BSP.
  650. This service switches the requested AP to be the BSP from that point onward.
  651. This service changes the BSP for all purposes. The new BSP can take over the
  652. execution of the old BSP and continue seamlessly from where the old one left
  653. off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
  654. is signaled.
  655. If the BSP cannot be switched prior to the return from this service, then
  656. EFI_UNSUPPORTED must be returned.
  657. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  658. @param[in] ProcessorNumber The handle number of AP that is to become the new
  659. BSP. The range is from 0 to the total number of
  660. logical processors minus 1. The total number of
  661. logical processors can be retrieved by
  662. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  663. @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
  664. enabled AP. Otherwise, it will be disabled.
  665. @retval EFI_SUCCESS BSP successfully switched.
  666. @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
  667. this service returning.
  668. @retval EFI_UNSUPPORTED Switching the BSP is not supported.
  669. @retval EFI_SUCCESS The calling processor is an AP.
  670. @retval EFI_NOT_FOUND The processor with the handle specified by
  671. ProcessorNumber does not exist.
  672. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
  673. a disabled AP.
  674. @retval EFI_NOT_READY The specified AP is busy.
  675. **/
  676. EFI_STATUS
  677. EFIAPI
  678. CpuMpServicesSwitchBSP (
  679. IN EFI_MP_SERVICES_PROTOCOL *This,
  680. IN UINTN ProcessorNumber,
  681. IN BOOLEAN EnableOldBSP
  682. )
  683. {
  684. UINTN Index;
  685. if (!IsBSP ()) {
  686. return EFI_DEVICE_ERROR;
  687. }
  688. if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
  689. return EFI_NOT_FOUND;
  690. }
  691. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  692. return EFI_INVALID_PARAMETER;
  693. }
  694. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
  695. return EFI_INVALID_PARAMETER;
  696. }
  697. for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
  698. if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
  699. break;
  700. }
  701. }
  702. ASSERT (Index != gMPSystem.NumberOfProcessors);
  703. gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  704. if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
  705. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  706. return EFI_NOT_READY;
  707. }
  708. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  709. // Skip for now as we need switch a bunch of stack stuff around and it's complex
  710. // May not be worth it?
  711. return EFI_NOT_READY;
  712. }
  713. /**
  714. This service lets the caller enable or disable an AP from this point onward.
  715. This service may only be called from the BSP.
  716. This service allows the caller enable or disable an AP from this point onward.
  717. The caller can optionally specify the health status of the AP by Health. If
  718. an AP is being disabled, then the state of the disabled AP is implementation
  719. dependent. If an AP is enabled, then the implementation must guarantee that a
  720. complete initialization sequence is performed on the AP, so the AP is in a state
  721. that is compatible with an MP operating system. This service may not be supported
  722. after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
  723. If the enable or disable AP operation cannot be completed prior to the return
  724. from this service, then EFI_UNSUPPORTED must be returned.
  725. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  726. @param[in] ProcessorNumber The handle number of AP that is to become the new
  727. BSP. The range is from 0 to the total number of
  728. logical processors minus 1. The total number of
  729. logical processors can be retrieved by
  730. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  731. @param[in] EnableAP Specifies the new state for the processor for
  732. enabled, FALSE for disabled.
  733. @param[in] HealthFlag If not NULL, a pointer to a value that specifies
  734. the new health status of the AP. This flag
  735. corresponds to StatusFlag defined in
  736. EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
  737. the PROCESSOR_HEALTH_STATUS_BIT is used. All other
  738. bits are ignored. If it is NULL, this parameter
  739. is ignored.
  740. @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
  741. @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
  742. prior to this service returning.
  743. @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
  744. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  745. @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
  746. does not exist.
  747. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
  748. **/
  749. EFI_STATUS
  750. EFIAPI
  751. CpuMpServicesEnableDisableAP (
  752. IN EFI_MP_SERVICES_PROTOCOL *This,
  753. IN UINTN ProcessorNumber,
  754. IN BOOLEAN EnableAP,
  755. IN UINT32 *HealthFlag OPTIONAL
  756. )
  757. {
  758. if (!IsBSP ()) {
  759. return EFI_DEVICE_ERROR;
  760. }
  761. if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
  762. return EFI_NOT_FOUND;
  763. }
  764. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
  765. return EFI_INVALID_PARAMETER;
  766. }
  767. gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  768. if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
  769. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  770. return EFI_UNSUPPORTED;
  771. }
  772. gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
  773. if (EnableAP) {
  774. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0 ) {
  775. gMPSystem.NumberOfEnabledProcessors++;
  776. }
  777. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_ENABLED_BIT;
  778. } else {
  779. if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == PROCESSOR_ENABLED_BIT ) {
  780. gMPSystem.NumberOfEnabledProcessors--;
  781. }
  782. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_ENABLED_BIT;
  783. }
  784. if (HealthFlag != NULL) {
  785. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;
  786. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT);
  787. }
  788. return EFI_SUCCESS;
  789. }
  790. /**
  791. This return the handle number for the calling processor. This service may be
  792. called from the BSP and APs.
  793. This service returns the processor handle number for the calling processor.
  794. The returned value is in the range from 0 to the total number of logical
  795. processors minus 1. The total number of logical processors can be retrieved
  796. with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
  797. called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
  798. is returned. Otherwise, the current processors handle number is returned in
  799. ProcessorNumber, and EFI_SUCCESS is returned.
  800. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  801. @param[in] ProcessorNumber The handle number of AP that is to become the new
  802. BSP. The range is from 0 to the total number of
  803. logical processors minus 1. The total number of
  804. logical processors can be retrieved by
  805. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  806. @retval EFI_SUCCESS The current processor handle number was returned
  807. in ProcessorNumber.
  808. @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
  809. **/
  810. EFI_STATUS
  811. EFIAPI
  812. CpuMpServicesWhoAmI (
  813. IN EFI_MP_SERVICES_PROTOCOL *This,
  814. OUT UINTN *ProcessorNumber
  815. )
  816. {
  817. UINTN Index;
  818. UINT64 ProcessorId;
  819. if (ProcessorNumber == NULL) {
  820. return EFI_INVALID_PARAMETER;
  821. }
  822. ProcessorId = gThread->Self ();
  823. for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
  824. if (gMPSystem.ProcessorData[Index].Info.ProcessorId == ProcessorId) {
  825. break;
  826. }
  827. }
  828. *ProcessorNumber = Index;
  829. return EFI_SUCCESS;
  830. }
  831. EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {
  832. CpuMpServicesGetNumberOfProcessors,
  833. CpuMpServicesGetProcessorInfo,
  834. CpuMpServicesStartupAllAps,
  835. CpuMpServicesStartupThisAP,
  836. CpuMpServicesSwitchBSP,
  837. CpuMpServicesEnableDisableAP,
  838. CpuMpServicesWhoAmI
  839. };
  840. /*++
  841. If timeout occurs in StartupAllAps(), a timer is set, which invokes this
  842. procedure periodically to check whether all APs have finished.
  843. --*/
  844. VOID
  845. EFIAPI
  846. CpuCheckAllAPsStatus (
  847. IN EFI_EVENT Event,
  848. IN VOID *Context
  849. )
  850. {
  851. UINTN ProcessorNumber;
  852. UINTN NextNumber;
  853. PROCESSOR_DATA_BLOCK *ProcessorData;
  854. PROCESSOR_DATA_BLOCK *NextData;
  855. EFI_STATUS Status;
  856. PROCESSOR_STATE ProcessorState;
  857. UINTN Cpu;
  858. BOOLEAN Found;
  859. if (gMPSystem.TimeoutActive) {
  860. gMPSystem.Timeout -= CalculateAndStallInterval (gMPSystem.Timeout);
  861. }
  862. for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {
  863. ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];
  864. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  865. // Skip BSP
  866. continue;
  867. }
  868. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  869. // Skip Disabled processors
  870. continue;
  871. }
  872. // This is an Interrupt Service routine.
  873. // This can grab a lock that is held in a non-interrupt
  874. // context. Meaning deadlock. Which is a bad thing.
  875. // So, try lock it. If we can get it, cool, do our thing.
  876. // otherwise, just dump out & try again on the next iteration.
  877. Status = gThread->MutexTryLock (ProcessorData->StateLock);
  878. if (EFI_ERROR (Status)) {
  879. return;
  880. }
  881. ProcessorState = ProcessorData->State;
  882. gThread->MutexUnlock (ProcessorData->StateLock);
  883. switch (ProcessorState) {
  884. case CPU_STATE_FINISHED:
  885. if (gMPSystem.SingleThread) {
  886. Status = GetNextBlockedNumber (&NextNumber);
  887. if (!EFI_ERROR (Status)) {
  888. NextData = &gMPSystem.ProcessorData[NextNumber];
  889. gThread->MutexLock (NextData->StateLock);
  890. NextData->State = CPU_STATE_READY;
  891. gThread->MutexUnlock (NextData->StateLock);
  892. SetApProcedure (NextData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);
  893. }
  894. }
  895. gThread->MutexLock (ProcessorData->StateLock);
  896. ProcessorData->State = CPU_STATE_IDLE;
  897. gThread->MutexUnlock (ProcessorData->StateLock);
  898. gMPSystem.FinishCount++;
  899. break;
  900. default:
  901. break;
  902. }
  903. }
  904. if (gMPSystem.TimeoutActive && (gMPSystem.Timeout == 0)) {
  905. //
  906. // Timeout
  907. //
  908. if (gMPSystem.FailedList != NULL) {
  909. for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {
  910. ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];
  911. if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  912. // Skip BSP
  913. continue;
  914. }
  915. if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
  916. // Skip Disabled processors
  917. continue;
  918. }
  919. // Mark the
  920. Status = gThread->MutexTryLock (ProcessorData->StateLock);
  921. if (EFI_ERROR (Status)) {
  922. return;
  923. }
  924. ProcessorState = ProcessorData->State;
  925. gThread->MutexUnlock (ProcessorData->StateLock);
  926. if (ProcessorState != CPU_STATE_IDLE) {
  927. // If we are retrying make sure we don't double count
  928. for (Cpu = 0, Found = FALSE; Cpu < gMPSystem.NumberOfProcessors; Cpu++) {
  929. if (gMPSystem.FailedList[Cpu] == END_OF_CPU_LIST) {
  930. break;
  931. }
  932. if (gMPSystem.FailedList[ProcessorNumber] == Cpu) {
  933. Found = TRUE;
  934. break;
  935. }
  936. }
  937. if (!Found) {
  938. gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Cpu;
  939. }
  940. }
  941. }
  942. }
  943. // Force terminal exit
  944. gMPSystem.FinishCount = gMPSystem.StartCount;
  945. }
  946. if (gMPSystem.FinishCount != gMPSystem.StartCount) {
  947. return;
  948. }
  949. gBS->SetTimer (
  950. gMPSystem.CheckAllAPsEvent,
  951. TimerCancel,
  952. 0
  953. );
  954. if (gMPSystem.FailedListIndex == 0) {
  955. if (gMPSystem.FailedList != NULL) {
  956. FreePool (gMPSystem.FailedList);
  957. gMPSystem.FailedList = NULL;
  958. }
  959. }
  960. Status = gBS->SignalEvent (gMPSystem.WaitEvent);
  961. return;
  962. }
  963. VOID
  964. EFIAPI
  965. CpuCheckThisAPStatus (
  966. IN EFI_EVENT Event,
  967. IN VOID *Context
  968. )
  969. {
  970. EFI_STATUS Status;
  971. PROCESSOR_DATA_BLOCK *ProcessorData;
  972. PROCESSOR_STATE ProcessorState;
  973. ProcessorData = (PROCESSOR_DATA_BLOCK *)Context;
  974. //
  975. // This is an Interrupt Service routine.
  976. // that can grab a lock that is held in a non-interrupt
  977. // context. Meaning deadlock. Which is a badddd thing.
  978. // So, try lock it. If we can get it, cool, do our thing.
  979. // otherwise, just dump out & try again on the next iteration.
  980. //
  981. Status = gThread->MutexTryLock (ProcessorData->StateLock);
  982. if (EFI_ERROR (Status)) {
  983. return;
  984. }
  985. ProcessorState = ProcessorData->State;
  986. gThread->MutexUnlock (ProcessorData->StateLock);
  987. if (ProcessorState == CPU_STATE_FINISHED) {
  988. Status = gBS->SetTimer (ProcessorData->CheckThisAPEvent, TimerCancel, 0);
  989. ASSERT_EFI_ERROR (Status);
  990. Status = gBS->SignalEvent (gMPSystem.WaitEvent);
  991. ASSERT_EFI_ERROR (Status);
  992. gThread->MutexLock (ProcessorData->StateLock);
  993. ProcessorData->State = CPU_STATE_IDLE;
  994. gThread->MutexUnlock (ProcessorData->StateLock);
  995. }
  996. return;
  997. }
  998. /*++
  999. This function is called by all processors (both BSP and AP) once and collects MP related data
  1000. MPSystemData - Pointer to the data structure containing MP related data
  1001. BSP - TRUE if the CPU is BSP
  1002. EFI_SUCCESS - Data for the processor collected and filled in
  1003. --*/
  1004. EFI_STATUS
  1005. FillInProcessorInformation (
  1006. IN BOOLEAN BSP,
  1007. IN UINTN ProcessorNumber
  1008. )
  1009. {
  1010. gMPSystem.ProcessorData[ProcessorNumber].Info.ProcessorId = gThread->Self ();
  1011. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
  1012. if (BSP) {
  1013. gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
  1014. }
  1015. gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Package = (UINT32)ProcessorNumber;
  1016. gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Core = 0;
  1017. gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Thread = 0;
  1018. gMPSystem.ProcessorData[ProcessorNumber].State = BSP ? CPU_STATE_BUSY : CPU_STATE_IDLE;
  1019. gMPSystem.ProcessorData[ProcessorNumber].Procedure = NULL;
  1020. gMPSystem.ProcessorData[ProcessorNumber].Parameter = NULL;
  1021. gMPSystem.ProcessorData[ProcessorNumber].StateLock = gThread->MutexInit ();
  1022. gMPSystem.ProcessorData[ProcessorNumber].ProcedureLock = gThread->MutexInit ();
  1023. return EFI_SUCCESS;
  1024. }
  1025. VOID *
  1026. EFIAPI
  1027. CpuDriverApIdolLoop (
  1028. VOID *Context
  1029. )
  1030. {
  1031. EFI_AP_PROCEDURE Procedure;
  1032. VOID *Parameter;
  1033. UINTN ProcessorNumber;
  1034. PROCESSOR_DATA_BLOCK *ProcessorData;
  1035. ProcessorNumber = (UINTN)Context;
  1036. ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];
  1037. ProcessorData->Info.ProcessorId = gThread->Self ();
  1038. while (TRUE) {
  1039. //
  1040. // Make a local copy on the stack to be extra safe
  1041. //
  1042. gThread->MutexLock (ProcessorData->ProcedureLock);
  1043. Procedure = ProcessorData->Procedure;
  1044. Parameter = ProcessorData->Parameter;
  1045. gThread->MutexUnlock (ProcessorData->ProcedureLock);
  1046. if (Procedure != NULL) {
  1047. gThread->MutexLock (ProcessorData->StateLock);
  1048. ProcessorData->State = CPU_STATE_BUSY;
  1049. gThread->MutexUnlock (ProcessorData->StateLock);
  1050. Procedure (Parameter);
  1051. gThread->MutexLock (ProcessorData->ProcedureLock);
  1052. ProcessorData->Procedure = NULL;
  1053. gThread->MutexUnlock (ProcessorData->ProcedureLock);
  1054. gThread->MutexLock (ProcessorData->StateLock);
  1055. ProcessorData->State = CPU_STATE_FINISHED;
  1056. gThread->MutexUnlock (ProcessorData->StateLock);
  1057. }
  1058. // Poll 5 times a seconds, 200ms
  1059. // Don't want to burn too many system resources doing nothing.
  1060. gEmuThunk->Sleep (200 * 1000);
  1061. }
  1062. return 0;
  1063. }
  1064. EFI_STATUS
  1065. InitializeMpSystemData (
  1066. IN UINTN NumberOfProcessors
  1067. )
  1068. {
  1069. EFI_STATUS Status;
  1070. UINTN Index;
  1071. //
  1072. // Clear the data structure area first.
  1073. //
  1074. ZeroMem (&gMPSystem, sizeof (MP_SYSTEM_DATA));
  1075. //
  1076. // First BSP fills and inits all known values, including it's own records.
  1077. //
  1078. gMPSystem.NumberOfProcessors = NumberOfProcessors;
  1079. gMPSystem.NumberOfEnabledProcessors = NumberOfProcessors;
  1080. gMPSystem.ProcessorData = AllocateZeroPool (gMPSystem.NumberOfProcessors * sizeof (PROCESSOR_DATA_BLOCK));
  1081. ASSERT (gMPSystem.ProcessorData != NULL);
  1082. FillInProcessorInformation (TRUE, 0);
  1083. Status = gBS->CreateEvent (
  1084. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  1085. TPL_CALLBACK,
  1086. CpuCheckAllAPsStatus,
  1087. NULL,
  1088. &gMPSystem.CheckAllAPsEvent
  1089. );
  1090. ASSERT_EFI_ERROR (Status);
  1091. for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
  1092. if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
  1093. // Skip BSP
  1094. continue;
  1095. }
  1096. FillInProcessorInformation (FALSE, Index);
  1097. Status = gThread->CreateThread (
  1098. (VOID *)&gMPSystem.ProcessorData[Index].Info.ProcessorId,
  1099. NULL,
  1100. CpuDriverApIdolLoop,
  1101. (VOID *)Index
  1102. );
  1103. Status = gBS->CreateEvent (
  1104. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  1105. TPL_CALLBACK,
  1106. CpuCheckThisAPStatus,
  1107. (VOID *)&gMPSystem.ProcessorData[Index],
  1108. &gMPSystem.ProcessorData[Index].CheckThisAPEvent
  1109. );
  1110. }
  1111. return EFI_SUCCESS;
  1112. }
  1113. /**
  1114. Invoke a notification event
  1115. @param Event Event whose notification function is being invoked.
  1116. @param Context The pointer to the notification function's context,
  1117. which is implementation-dependent.
  1118. **/
  1119. VOID
  1120. EFIAPI
  1121. CpuReadToBootFunction (
  1122. IN EFI_EVENT Event,
  1123. IN VOID *Context
  1124. )
  1125. {
  1126. gReadToBoot = TRUE;
  1127. }
  1128. EFI_STATUS
  1129. CpuMpServicesInit (
  1130. OUT UINTN *MaxCpus
  1131. )
  1132. {
  1133. EFI_STATUS Status;
  1134. EFI_HANDLE Handle;
  1135. EMU_IO_THUNK_PROTOCOL *IoThunk;
  1136. *MaxCpus = 1; // BSP
  1137. IoThunk = GetIoThunkInstance (&gEmuThreadThunkProtocolGuid, 0);
  1138. if (IoThunk != NULL) {
  1139. Status = IoThunk->Open (IoThunk);
  1140. if (!EFI_ERROR (Status)) {
  1141. if (IoThunk->ConfigString != NULL) {
  1142. *MaxCpus += StrDecimalToUintn (IoThunk->ConfigString);
  1143. gThread = IoThunk->Interface;
  1144. }
  1145. }
  1146. }
  1147. if (*MaxCpus == 1) {
  1148. // We are not MP so nothing to do
  1149. return EFI_SUCCESS;
  1150. }
  1151. gPollInterval = (UINTN)PcdGet64 (PcdEmuMpServicesPollingInterval);
  1152. Status = InitializeMpSystemData (*MaxCpus);
  1153. if (EFI_ERROR (Status)) {
  1154. return Status;
  1155. }
  1156. Status = EfiCreateEventReadyToBootEx (TPL_CALLBACK, CpuReadToBootFunction, NULL, &gReadToBootEvent);
  1157. ASSERT_EFI_ERROR (Status);
  1158. //
  1159. // Now install the MP services protocol.
  1160. //
  1161. Handle = NULL;
  1162. Status = gBS->InstallMultipleProtocolInterfaces (
  1163. &Handle,
  1164. &gEfiMpServiceProtocolGuid,
  1165. &mMpServicesTemplate,
  1166. NULL
  1167. );
  1168. return Status;
  1169. }