]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/CpuRuntimeDxe/MpService.c
EmulatorPkg: Fix Visual Studio build for IA32 & X64
[mirror_edk2.git] / EmulatorPkg / 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 - 2012, 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 Number;
378 UINTN NextNumber;
379 PROCESSOR_STATE APInitialState;
380 PROCESSOR_STATE ProcessorState;
381 INTN Timeout;
382
383
384 if (!IsBSP ()) {
385 return EFI_DEVICE_ERROR;
386 }
387
388 if (gMPSystem.NumberOfProcessors == 1) {
389 return EFI_NOT_STARTED;
390 }
391
392 if (Procedure == NULL) {
393 return EFI_INVALID_PARAMETER;
394 }
395
396 if ((WaitEvent != NULL) && gReadToBoot) {
397 return EFI_UNSUPPORTED;
398 }
399
400
401 if (FailedCpuList != NULL) {
402 gMPSystem.FailedList = AllocatePool ((gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN));
403 if (gMPSystem.FailedList == NULL) {
404 return EFI_OUT_OF_RESOURCES;
405 }
406 SetMemN (gMPSystem.FailedList, (gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN), END_OF_CPU_LIST);
407 gMPSystem.FailedListIndex = 0;
408 *FailedCpuList = gMPSystem.FailedList;
409 }
410
411 Timeout = TimeoutInMicroseconds;
412
413 ProcessorData = NULL;
414
415 gMPSystem.FinishCount = 0;
416 gMPSystem.StartCount = 0;
417 gMPSystem.SingleThread = SingleThread;
418 APInitialState = CPU_STATE_READY;
419
420 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
421 ProcessorData = &gMPSystem.ProcessorData[Number];
422
423 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
424 // Skip BSP
425 continue;
426 }
427
428 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
429 // Skip Disabled processors
430 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Number;
431 continue;
432 }
433
434 //
435 // Get APs prepared, and put failing APs into FailedCpuList
436 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
437 // state 1 by 1, until the previous 1 finished its task
438 // if not "SingleThread", all APs are put to ready state from the beginning
439 //
440 if (ProcessorData->State == CPU_STATE_IDLE) {
441 gMPSystem.StartCount++;
442
443 gThread->MutexLock (&ProcessorData->StateLock);
444 ProcessorData->State = APInitialState;
445 gThread->MutexUnlock (&ProcessorData->StateLock);
446
447 if (SingleThread) {
448 APInitialState = CPU_STATE_BLOCKED;
449 }
450 } else {
451 return EFI_NOT_READY;
452 }
453 }
454
455 if (WaitEvent != NULL) {
456 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
457 ProcessorData = &gMPSystem.ProcessorData[Number];
458 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
459 // Skip BSP
460 continue;
461 }
462
463 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
464 // Skip Disabled processors
465 continue;
466 }
467
468 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);
469 }
470
471 //
472 // Save data into private data structure, and create timer to poll AP state before exiting
473 //
474 gMPSystem.Procedure = Procedure;
475 gMPSystem.ProcedureArgument = ProcedureArgument;
476 gMPSystem.WaitEvent = WaitEvent;
477 gMPSystem.Timeout = TimeoutInMicroseconds;
478 gMPSystem.TimeoutActive = (BOOLEAN)(TimeoutInMicroseconds != 0);
479 Status = gBS->SetTimer (
480 gMPSystem.CheckAllAPsEvent,
481 TimerPeriodic,
482 gPollInterval
483 );
484 return Status;
485
486 }
487
488 while (TRUE) {
489 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {
490 ProcessorData = &gMPSystem.ProcessorData[Number];
491 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
492 // Skip BSP
493 continue;
494 }
495
496 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
497 // Skip Disabled processors
498 continue;
499 }
500
501 gThread->MutexLock (ProcessorData->StateLock);
502 ProcessorState = ProcessorData->State;
503 gThread->MutexUnlock (ProcessorData->StateLock);
504
505 switch (ProcessorState) {
506 case CPU_STATE_READY:
507 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);
508 break;
509
510 case CPU_STATE_FINISHED:
511 gMPSystem.FinishCount++;
512 if (SingleThread) {
513 Status = GetNextBlockedNumber (&NextNumber);
514 if (!EFI_ERROR (Status)) {
515 gMPSystem.ProcessorData[NextNumber].State = CPU_STATE_READY;
516 }
517 }
518
519 ProcessorData->State = CPU_STATE_IDLE;
520 break;
521
522 default:
523 break;
524 }
525 }
526
527 if (gMPSystem.FinishCount == gMPSystem.StartCount) {
528 Status = EFI_SUCCESS;
529 goto Done;
530 }
531
532 if ((TimeoutInMicroseconds != 0) && (Timeout < 0)) {
533 Status = EFI_TIMEOUT;
534 goto Done;
535 }
536
537 gBS->Stall (gPollInterval);
538 Timeout -= gPollInterval;
539 }
540
541 Done:
542 if (FailedCpuList != NULL) {
543 if (gMPSystem.FailedListIndex == 0) {
544 FreePool (*FailedCpuList);
545 *FailedCpuList = NULL;
546 }
547 }
548
549 return EFI_SUCCESS;
550 }
551
552
553 /**
554 This service lets the caller get one enabled AP to execute a caller-provided
555 function. The caller can request the BSP to either wait for the completion
556 of the AP or just proceed with the next task by using the EFI event mechanism.
557 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
558 execution support. This service may only be called from the BSP.
559
560 This function is used to dispatch one enabled AP to the function specified by
561 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
562 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
563 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
564 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
565 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
566 then EFI_UNSUPPORTED must be returned.
567
568 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
569 from Procedure, then execution of Procedure by the AP is terminated. The AP is
570 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
571 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
572
573 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
574 instance.
575 @param[in] Procedure A pointer to the function to be run on
576 enabled APs of the system. See type
577 EFI_AP_PROCEDURE.
578 @param[in] ProcessorNumber The handle number of the AP. The range is
579 from 0 to the total number of logical
580 processors minus 1. The total number of
581 logical processors can be retrieved by
582 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
583 @param[in] WaitEvent The event created by the caller with CreateEvent()
584 service. If it is NULL, then execute in
585 blocking mode. BSP waits until all APs finish
586 or TimeoutInMicroseconds expires. If it's
587 not NULL, then execute in non-blocking mode.
588 BSP requests the function specified by
589 Procedure to be started on all the enabled
590 APs, and go on executing immediately. If
591 all return from Procedure or TimeoutInMicroseconds
592 expires, this event is signaled. The BSP
593 can use the CheckEvent() or WaitForEvent()
594 services to check the state of event. Type
595 EFI_EVENT is defined in CreateEvent() in
596 the Unified Extensible Firmware Interface
597 Specification.
598 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
599 APs to return from Procedure, either for
600 blocking or non-blocking mode. Zero means
601 infinity. If the timeout expires before
602 all APs return from Procedure, then Procedure
603 on the failed APs is terminated. All enabled
604 APs are available for next function assigned
605 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
606 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
607 If the timeout expires in blocking mode,
608 BSP returns EFI_TIMEOUT. If the timeout
609 expires in non-blocking mode, WaitEvent
610 is signaled with SignalEvent().
611 @param[in] ProcedureArgument The parameter passed into Procedure for
612 all APs.
613 @param[out] Finished If NULL, this parameter is ignored. In
614 blocking mode, this parameter is ignored.
615 In non-blocking mode, if AP returns from
616 Procedure before the timeout expires, its
617 content is set to TRUE. Otherwise, the
618 value is set to FALSE. The caller can
619 determine if the AP returned from Procedure
620 by evaluating this value.
621
622 @retval EFI_SUCCESS In blocking mode, specified AP finished before
623 the timeout expires.
624 @retval EFI_SUCCESS In non-blocking mode, the function has been
625 dispatched to specified AP.
626 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
627 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
628 signaled.
629 @retval EFI_DEVICE_ERROR The calling processor is an AP.
630 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
631 the specified AP has finished.
632 @retval EFI_NOT_READY The specified AP is busy.
633 @retval EFI_NOT_FOUND The processor with the handle specified by
634 ProcessorNumber does not exist.
635 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
636 @retval EFI_INVALID_PARAMETER Procedure is NULL.
637
638 **/
639 EFI_STATUS
640 EFIAPI
641 CpuMpServicesStartupThisAP (
642 IN EFI_MP_SERVICES_PROTOCOL *This,
643 IN EFI_AP_PROCEDURE Procedure,
644 IN UINTN ProcessorNumber,
645 IN EFI_EVENT WaitEvent OPTIONAL,
646 IN UINTN TimeoutInMicroseconds,
647 IN VOID *ProcedureArgument OPTIONAL,
648 OUT BOOLEAN *Finished OPTIONAL
649 )
650 {
651 INTN Timeout;
652
653 if (!IsBSP ()) {
654 return EFI_DEVICE_ERROR;
655 }
656
657 if (Procedure == NULL) {
658 return EFI_INVALID_PARAMETER;
659 }
660
661 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
662 return EFI_NOT_FOUND;
663 }
664
665 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
666 return EFI_INVALID_PARAMETER;
667 }
668
669 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
670 return EFI_NOT_READY;
671 }
672
673 if ((WaitEvent != NULL) && gReadToBoot) {
674 return EFI_UNSUPPORTED;
675 }
676
677 Timeout = TimeoutInMicroseconds;
678
679 gMPSystem.StartCount = 1;
680 gMPSystem.FinishCount = 0;
681
682 SetApProcedure (&gMPSystem.ProcessorData[ProcessorNumber], Procedure, ProcedureArgument);
683
684 if (WaitEvent != NULL) {
685 // Non Blocking
686 gMPSystem.WaitEvent = WaitEvent;
687 gBS->SetTimer (
688 gMPSystem.ProcessorData[ProcessorNumber].CheckThisAPEvent,
689 TimerPeriodic,
690 gPollInterval
691 );
692 return EFI_SUCCESS;
693 }
694
695 // Blocking
696 while (TRUE) {
697 gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
698 if (gMPSystem.ProcessorData[ProcessorNumber].State == CPU_STATE_FINISHED) {
699 gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;
700 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
701 break;
702 }
703
704 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
705
706 if ((TimeoutInMicroseconds != 0) && (Timeout < 0)) {
707 return EFI_TIMEOUT;
708 }
709
710 gBS->Stall (gPollInterval);
711 Timeout -= gPollInterval;
712 }
713
714 return EFI_SUCCESS;
715
716 }
717
718
719 /**
720 This service switches the requested AP to be the BSP from that point onward.
721 This service changes the BSP for all purposes. This call can only be performed
722 by the current BSP.
723
724 This service switches the requested AP to be the BSP from that point onward.
725 This service changes the BSP for all purposes. The new BSP can take over the
726 execution of the old BSP and continue seamlessly from where the old one left
727 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
728 is signaled.
729
730 If the BSP cannot be switched prior to the return from this service, then
731 EFI_UNSUPPORTED must be returned.
732
733 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
734 @param[in] ProcessorNumber The handle number of AP that is to become the new
735 BSP. The range is from 0 to the total number of
736 logical processors minus 1. The total number of
737 logical processors can be retrieved by
738 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
739 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
740 enabled AP. Otherwise, it will be disabled.
741
742 @retval EFI_SUCCESS BSP successfully switched.
743 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
744 this service returning.
745 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
746 @retval EFI_SUCCESS The calling processor is an AP.
747 @retval EFI_NOT_FOUND The processor with the handle specified by
748 ProcessorNumber does not exist.
749 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
750 a disabled AP.
751 @retval EFI_NOT_READY The specified AP is busy.
752
753 **/
754 EFI_STATUS
755 EFIAPI
756 CpuMpServicesSwitchBSP (
757 IN EFI_MP_SERVICES_PROTOCOL *This,
758 IN UINTN ProcessorNumber,
759 IN BOOLEAN EnableOldBSP
760 )
761 {
762 UINTN Index;
763
764 if (!IsBSP ()) {
765 return EFI_DEVICE_ERROR;
766 }
767
768 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
769 return EFI_NOT_FOUND;
770 }
771
772 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
773 return EFI_INVALID_PARAMETER;
774 }
775
776 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
777 return EFI_INVALID_PARAMETER;
778 }
779
780 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
781 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
782 break;
783 }
784 }
785 ASSERT (Index != gMPSystem.NumberOfProcessors);
786
787 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
788 return EFI_NOT_READY;
789 }
790
791 // Skip for now as we need switch a bunch of stack stuff around and it's complex
792 // May not be worth it?
793 return EFI_NOT_READY;
794 }
795
796
797 /**
798 This service lets the caller enable or disable an AP from this point onward.
799 This service may only be called from the BSP.
800
801 This service allows the caller enable or disable an AP from this point onward.
802 The caller can optionally specify the health status of the AP by Health. If
803 an AP is being disabled, then the state of the disabled AP is implementation
804 dependent. If an AP is enabled, then the implementation must guarantee that a
805 complete initialization sequence is performed on the AP, so the AP is in a state
806 that is compatible with an MP operating system. This service may not be supported
807 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
808
809 If the enable or disable AP operation cannot be completed prior to the return
810 from this service, then EFI_UNSUPPORTED must be returned.
811
812 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
813 @param[in] ProcessorNumber The handle number of AP that is to become the new
814 BSP. The range is from 0 to the total number of
815 logical processors minus 1. The total number of
816 logical processors can be retrieved by
817 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
818 @param[in] EnableAP Specifies the new state for the processor for
819 enabled, FALSE for disabled.
820 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
821 the new health status of the AP. This flag
822 corresponds to StatusFlag defined in
823 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
824 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
825 bits are ignored. If it is NULL, this parameter
826 is ignored.
827
828 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
829 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
830 prior to this service returning.
831 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
832 @retval EFI_DEVICE_ERROR The calling processor is an AP.
833 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
834 does not exist.
835 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
836
837 **/
838 EFI_STATUS
839 EFIAPI
840 CpuMpServicesEnableDisableAP (
841 IN EFI_MP_SERVICES_PROTOCOL *This,
842 IN UINTN ProcessorNumber,
843 IN BOOLEAN EnableAP,
844 IN UINT32 *HealthFlag OPTIONAL
845 )
846 {
847 if (!IsBSP ()) {
848 return EFI_DEVICE_ERROR;
849 }
850
851 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {
852 return EFI_NOT_FOUND;
853 }
854
855 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
856 return EFI_INVALID_PARAMETER;
857 }
858
859 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
860 return EFI_UNSUPPORTED;
861 }
862
863 gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
864
865 if (EnableAP) {
866 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0 ) {
867 gMPSystem.NumberOfEnabledProcessors++;
868 }
869 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_ENABLED_BIT;
870 } else {
871 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == PROCESSOR_ENABLED_BIT ) {
872 gMPSystem.NumberOfEnabledProcessors--;
873 }
874 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_ENABLED_BIT;
875 }
876
877 if (HealthFlag != NULL) {
878 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;
879 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT);
880 }
881
882 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
883
884 return EFI_SUCCESS;
885 }
886
887
888 /**
889 This return the handle number for the calling processor. This service may be
890 called from the BSP and APs.
891
892 This service returns the processor handle number for the calling processor.
893 The returned value is in the range from 0 to the total number of logical
894 processors minus 1. The total number of logical processors can be retrieved
895 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
896 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
897 is returned. Otherwise, the current processors handle number is returned in
898 ProcessorNumber, and EFI_SUCCESS is returned.
899
900 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
901 @param[in] ProcessorNumber The handle number of AP that is to become the new
902 BSP. The range is from 0 to the total number of
903 logical processors minus 1. The total number of
904 logical processors can be retrieved by
905 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
906
907 @retval EFI_SUCCESS The current processor handle number was returned
908 in ProcessorNumber.
909 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
910
911 **/
912 EFI_STATUS
913 EFIAPI
914 CpuMpServicesWhoAmI (
915 IN EFI_MP_SERVICES_PROTOCOL *This,
916 OUT UINTN *ProcessorNumber
917 )
918 {
919 UINTN Index;
920 UINT64 ProcessorId;
921
922 if (ProcessorNumber == NULL) {
923 return EFI_INVALID_PARAMETER;
924 }
925
926 ProcessorId = gThread->Self ();
927 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
928 if (gMPSystem.ProcessorData[Index].Info.ProcessorId == ProcessorId) {
929 break;
930 }
931 }
932
933 *ProcessorNumber = Index;
934 return EFI_SUCCESS;
935 }
936
937
938
939 EFI_MP_SERVICES_PROTOCOL mMpSercicesTemplate = {
940 CpuMpServicesGetNumberOfProcessors,
941 CpuMpServicesGetProcessorInfo,
942 CpuMpServicesStartupAllAps,
943 CpuMpServicesStartupThisAP,
944 CpuMpServicesSwitchBSP,
945 CpuMpServicesEnableDisableAP,
946 CpuMpServicesWhoAmI
947 };
948
949
950
951 /*++
952 If timeout occurs in StartupAllAps(), a timer is set, which invokes this
953 procedure periodically to check whether all APs have finished.
954
955
956 --*/
957 VOID
958 EFIAPI
959 CpuCheckAllAPsStatus (
960 IN EFI_EVENT Event,
961 IN VOID *Context
962 )
963 {
964 UINTN ProcessorNumber;
965 UINTN NextNumber;
966 PROCESSOR_DATA_BLOCK *ProcessorData;
967 PROCESSOR_DATA_BLOCK *NextData;
968 EFI_STATUS Status;
969 PROCESSOR_STATE ProcessorState;
970 UINTN Cpu;
971 BOOLEAN Found;
972
973 if (gMPSystem.TimeoutActive) {
974 gMPSystem.Timeout -= gPollInterval;
975 }
976
977 ProcessorData = (PROCESSOR_DATA_BLOCK *) Context;
978
979 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {
980 if ((ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
981 // Skip BSP
982 continue;
983 }
984
985 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
986 // Skip Disabled processors
987 continue;
988 }
989
990 // This is an Interrupt Service routine.
991 // This can grab a lock that is held in a non-interrupt
992 // context. Meaning deadlock. Which is a bad thing.
993 // So, try lock it. If we can get it, cool, do our thing.
994 // otherwise, just dump out & try again on the next iteration.
995 Status = gThread->MutexTryLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
996 if (EFI_ERROR(Status)) {
997 return;
998 }
999 ProcessorState = gMPSystem.ProcessorData[ProcessorNumber].State;
1000 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
1001
1002 switch (ProcessorState) {
1003 case CPU_STATE_READY:
1004 SetApProcedure (ProcessorData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);
1005 break;
1006
1007 case CPU_STATE_FINISHED:
1008 if (gMPSystem.SingleThread) {
1009 Status = GetNextBlockedNumber (&NextNumber);
1010 if (!EFI_ERROR (Status)) {
1011 NextData = &gMPSystem.ProcessorData[NextNumber];
1012
1013 gThread->MutexLock (&NextData->ProcedureLock);
1014 NextData->State = CPU_STATE_READY;
1015 gThread->MutexUnlock (&NextData->ProcedureLock);
1016
1017 SetApProcedure (NextData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);
1018 }
1019 }
1020
1021 gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;
1022 gMPSystem.FinishCount++;
1023 break;
1024
1025 default:
1026 break;
1027 }
1028 }
1029
1030 if (gMPSystem.TimeoutActive && gMPSystem.Timeout < 0) {
1031 //
1032 // Timeout
1033 //
1034 if (gMPSystem.FailedList != NULL) {
1035 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {
1036 if ((ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
1037 // Skip BSP
1038 continue;
1039 }
1040
1041 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {
1042 // Skip Disabled processors
1043 continue;
1044 }
1045
1046 // Mark the
1047 Status = gThread->MutexTryLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
1048 if (EFI_ERROR(Status)) {
1049 return;
1050 }
1051 ProcessorState = gMPSystem.ProcessorData[ProcessorNumber].State;
1052 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
1053
1054 if (ProcessorState != CPU_STATE_IDLE) {
1055 // If we are retrying make sure we don't double count
1056 for (Cpu = 0, Found = FALSE; Cpu < gMPSystem.NumberOfProcessors; Cpu++) {
1057 if (gMPSystem.FailedList[Cpu] == END_OF_CPU_LIST) {
1058 break;
1059 }
1060 if (gMPSystem.FailedList[ProcessorNumber] == Cpu) {
1061 Found = TRUE;
1062 break;
1063 }
1064 }
1065 if (!Found) {
1066 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Cpu;
1067 }
1068 }
1069 }
1070 }
1071 // Force terminal exit
1072 gMPSystem.FinishCount = gMPSystem.StartCount;
1073 }
1074
1075 if (gMPSystem.FinishCount != gMPSystem.StartCount) {
1076 return;
1077 }
1078
1079 gBS->SetTimer (
1080 gMPSystem.CheckAllAPsEvent,
1081 TimerCancel,
1082 0
1083 );
1084
1085 if (gMPSystem.FailedListIndex == 0) {
1086 if (gMPSystem.FailedList != NULL) {
1087 FreePool (gMPSystem.FailedList);
1088 gMPSystem.FailedList = NULL;
1089 }
1090 }
1091
1092 Status = gBS->SignalEvent (gMPSystem.WaitEvent);
1093
1094 return ;
1095 }
1096
1097 VOID
1098 EFIAPI
1099 CpuCheckThisAPStatus (
1100 IN EFI_EVENT Event,
1101 IN VOID *Context
1102 )
1103 {
1104 EFI_STATUS Status;
1105 PROCESSOR_DATA_BLOCK *ProcessorData;
1106 PROCESSOR_STATE ProcessorState;
1107
1108 ProcessorData = (PROCESSOR_DATA_BLOCK *) Context;
1109
1110 //
1111 // This is an Interrupt Service routine.
1112 // that can grab a lock that is held in a non-interrupt
1113 // context. Meaning deadlock. Which is a badddd thing.
1114 // So, try lock it. If we can get it, cool, do our thing.
1115 // otherwise, just dump out & try again on the next iteration.
1116 //
1117 Status = gThread->MutexTryLock (ProcessorData->StateLock);
1118 if (EFI_ERROR(Status)) {
1119 return;
1120 }
1121 ProcessorState = ProcessorData->State;
1122 gThread->MutexUnlock (ProcessorData->StateLock);
1123
1124 if (ProcessorState == CPU_STATE_FINISHED) {
1125 Status = gBS->SetTimer (ProcessorData->CheckThisAPEvent, TimerCancel, 0);
1126 ASSERT_EFI_ERROR (Status);
1127
1128 Status = gBS->SignalEvent (gMPSystem.WaitEvent);
1129 ASSERT_EFI_ERROR (Status);
1130
1131 gThread->MutexLock (ProcessorData->StateLock);
1132 ProcessorData->State = CPU_STATE_IDLE;
1133 gThread->MutexUnlock (ProcessorData->StateLock);
1134 }
1135
1136 return ;
1137 }
1138
1139
1140 /*++
1141 This function is called by all processors (both BSP and AP) once and collects MP related data
1142
1143 MPSystemData - Pointer to the data structure containing MP related data
1144 BSP - TRUE if the CPU is BSP
1145
1146 EFI_SUCCESS - Data for the processor collected and filled in
1147
1148 --*/
1149 EFI_STATUS
1150 FillInProcessorInformation (
1151 IN BOOLEAN BSP,
1152 IN UINTN ProcessorNumber
1153 )
1154 {
1155 gMPSystem.ProcessorData[ProcessorNumber].Info.ProcessorId = gThread->Self ();
1156 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
1157 if (BSP) {
1158 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
1159 }
1160
1161 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Package = (UINT32) ProcessorNumber;
1162 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Core = 0;
1163 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Thread = 0;
1164 gMPSystem.ProcessorData[ProcessorNumber].State = BSP ? CPU_STATE_BUSY : CPU_STATE_IDLE;
1165
1166 gMPSystem.ProcessorData[ProcessorNumber].Procedure = NULL;
1167 gMPSystem.ProcessorData[ProcessorNumber].Parameter = NULL;
1168 gMPSystem.ProcessorData[ProcessorNumber].StateLock = gThread->MutexInit ();
1169 gMPSystem.ProcessorData[ProcessorNumber].ProcedureLock = gThread->MutexInit ();
1170
1171 return EFI_SUCCESS;
1172 }
1173
1174 VOID *
1175 EFIAPI
1176 CpuDriverApIdolLoop (
1177 VOID *Context
1178 )
1179 {
1180 EFI_AP_PROCEDURE Procedure;
1181 VOID *Parameter;
1182 UINTN ProcessorNumber;
1183 PROCESSOR_DATA_BLOCK *ProcessorData;
1184
1185 ProcessorNumber = (UINTN)Context;
1186 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];
1187
1188 ProcessorData->Info.ProcessorId = gThread->Self ();
1189
1190 while (TRUE) {
1191 //
1192 // Make a local copy on the stack to be extra safe
1193 //
1194 gThread->MutexLock (ProcessorData->ProcedureLock);
1195 Procedure = ProcessorData->Procedure;
1196 Parameter = ProcessorData->Parameter;
1197 gThread->MutexUnlock (ProcessorData->ProcedureLock);
1198
1199 if (Procedure != NULL) {
1200 gThread->MutexLock (ProcessorData->StateLock);
1201 ProcessorData->State = CPU_STATE_BUSY;
1202 gThread->MutexUnlock (ProcessorData->StateLock);
1203
1204 Procedure (Parameter);
1205
1206 gThread->MutexLock (ProcessorData->ProcedureLock);
1207 ProcessorData->Procedure = NULL;
1208 gThread->MutexUnlock (ProcessorData->ProcedureLock);
1209
1210 gThread->MutexLock (ProcessorData->StateLock);
1211 ProcessorData->State = CPU_STATE_FINISHED;
1212 gThread->MutexUnlock (ProcessorData->StateLock);
1213 }
1214
1215 // Poll 5 times a seconds, 200ms
1216 // Don't want to burn too many system resources doing nothing.
1217 gEmuThunk->Sleep (200 * 1000);
1218 }
1219
1220 return 0;
1221 }
1222
1223
1224 EFI_STATUS
1225 InitializeMpSystemData (
1226 IN UINTN NumberOfProcessors
1227 )
1228 {
1229 EFI_STATUS Status;
1230 UINTN Index;
1231
1232
1233 //
1234 // Clear the data structure area first.
1235 //
1236 ZeroMem (&gMPSystem, sizeof (MP_SYSTEM_DATA));
1237
1238 //
1239 // First BSP fills and inits all known values, including it's own records.
1240 //
1241 gMPSystem.NumberOfProcessors = NumberOfProcessors;
1242 gMPSystem.NumberOfEnabledProcessors = NumberOfProcessors;
1243
1244 gMPSystem.ProcessorData = AllocateZeroPool (gMPSystem.NumberOfProcessors * sizeof (PROCESSOR_DATA_BLOCK));
1245 ASSERT (gMPSystem.ProcessorData != NULL);
1246
1247 FillInProcessorInformation (TRUE, 0);
1248
1249 Status = gBS->CreateEvent (
1250 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1251 TPL_CALLBACK,
1252 CpuCheckAllAPsStatus,
1253 NULL,
1254 &gMPSystem.CheckAllAPsEvent
1255 );
1256 ASSERT_EFI_ERROR (Status);
1257
1258
1259 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {
1260 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {
1261 // Skip BSP
1262 continue;
1263 }
1264
1265 FillInProcessorInformation (FALSE, Index);
1266
1267 Status = gThread->CreateThread (
1268 (VOID *)&gMPSystem.ProcessorData[Index].Info.ProcessorId,
1269 NULL,
1270 CpuDriverApIdolLoop,
1271 (VOID *)Index
1272 );
1273
1274
1275 Status = gBS->CreateEvent (
1276 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1277 TPL_CALLBACK,
1278 CpuCheckThisAPStatus,
1279 (VOID *) &gMPSystem.ProcessorData[Index],
1280 &gMPSystem.ProcessorData[Index].CheckThisAPEvent
1281 );
1282 }
1283
1284 return EFI_SUCCESS;
1285 }
1286
1287
1288
1289 /**
1290 Invoke a notification event
1291
1292 @param Event Event whose notification function is being invoked.
1293 @param Context The pointer to the notification function's context,
1294 which is implementation-dependent.
1295
1296 **/
1297 VOID
1298 EFIAPI
1299 CpuReadToBootFunction (
1300 IN EFI_EVENT Event,
1301 IN VOID *Context
1302 )
1303 {
1304 gReadToBoot = TRUE;
1305 }
1306
1307
1308
1309 EFI_STATUS
1310 CpuMpServicesInit (
1311 OUT UINTN *MaxCpus
1312 )
1313 {
1314 EFI_STATUS Status;
1315 EFI_HANDLE Handle;
1316 EMU_IO_THUNK_PROTOCOL *IoThunk;
1317
1318 *MaxCpus = 1; // BSP
1319 IoThunk = GetIoThunkInstance (&gEmuThreadThunkProtocolGuid, 0);
1320 if (IoThunk != NULL) {
1321 Status = IoThunk->Open (IoThunk);
1322 if (!EFI_ERROR (Status)) {
1323 if (IoThunk->ConfigString != NULL) {
1324 *MaxCpus += StrDecimalToUintn (IoThunk->ConfigString);
1325 gThread = IoThunk->Interface;
1326 }
1327 }
1328 }
1329
1330 if (*MaxCpus == 1) {
1331 // We are not MP so nothing to do
1332 return EFI_SUCCESS;
1333 }
1334
1335 gPollInterval = (UINTN) PcdGet64 (PcdEmuMpServicesPollingInterval);
1336
1337 Status = InitializeMpSystemData (*MaxCpus);
1338 if (EFI_ERROR (Status)) {
1339 return Status;
1340 }
1341
1342 Status = EfiCreateEventReadyToBootEx (TPL_CALLBACK, CpuReadToBootFunction, NULL, &gReadToBootEvent);
1343 ASSERT_EFI_ERROR (Status);
1344
1345 //
1346 // Now install the MP services protocol.
1347 //
1348 Handle = NULL;
1349 Status = gBS->InstallMultipleProtocolInterfaces (
1350 &Handle,
1351 &gEfiMpServiceProtocolGuid, &mMpSercicesTemplate,
1352 NULL
1353 );
1354 return Status;
1355 }
1356
1357