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