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