]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.c
Remove dependency on include file in package outside of EKD2.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / MpServicesOnFrameworkMpServicesThunk / MpServicesOnFrameworkMpServicesThunk.c
CommitLineData
768e2a90 1/** @file\r
2Produces PI MP Services Protocol on top of Framework MP Services Protocol.\r
3\r
4Intel's Framework MP Services Protocol is replaced by EFI_MP_SERVICES_PROTOCOL in PI 1.1.\r
5This module produces PI MP Services Protocol on top of Framework MP Services Protocol.\r
6\r
7Copyright (c) 2009 Intel Corporation. <BR>\r
8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15Module Name:\r
16\r
17**/\r
18\r
19#include "MpServicesOnFrameworkMpServicesThunk.h"\r
20\r
21EFI_HANDLE mHandle = NULL;\r
22MP_SYSTEM_DATA mMPSystemData;\r
23EFI_PHYSICAL_ADDRESS mStartupVector;\r
24MP_CPU_EXCHANGE_INFO *mExchangeInfo;\r
25VOID *mStackStartAddress;\r
26BOOLEAN mStopCheckAPsStatus = FALSE;\r
27UINTN mNumberOfProcessors;\r
28EFI_GENERIC_MEMORY_TEST_PROTOCOL *mGenMemoryTest;\r
29\r
30FRAMEWORK_EFI_MP_SERVICES_PROTOCOL *mFrameworkMpService;\r
31EFI_MP_SERVICES_PROTOCOL mMpService = {\r
32 GetNumberOfProcessors,\r
33 GetProcessorInfo,\r
34 StartupAllAPs,\r
35 StartupThisAP,\r
36 SwitchBSP,\r
37 EnableDisableAP,\r
38 WhoAmI\r
39};\r
40\r
41\r
42/**\r
43 Implementation of GetNumberOfProcessors() service of MP Services Protocol.\r
44\r
45 This service retrieves the number of logical processor in the platform\r
46 and the number of those logical processors that are enabled on this boot.\r
47 This service may only be called from the BSP.\r
48\r
49 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
50 @param NumberOfProcessors Pointer to the total number of logical processors in the system,\r
51 including the BSP and disabled APs.\r
52 @param NumberOfEnabledProcessors Pointer to the number of enabled logical processors that exist\r
53 in system, including the BSP.\r
54\r
55 @retval EFI_SUCCESS Number of logical processors and enabled logical processors retrieved..\r
56 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
57 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL\r
58 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL\r
59\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63GetNumberOfProcessors (\r
64 IN EFI_MP_SERVICES_PROTOCOL *This,\r
65 OUT UINTN *NumberOfProcessors,\r
66 OUT UINTN *NumberOfEnabledProcessors\r
67 )\r
68{\r
69 EFI_STATUS Status;\r
70 UINTN CallerNumber;\r
71\r
72 //\r
73 // Check whether caller processor is BSP\r
74 //\r
75 WhoAmI (This, &CallerNumber);\r
76 if (CallerNumber != GetBspNumber ()) {\r
77 return EFI_DEVICE_ERROR;\r
78 }\r
79\r
80 //\r
81 // Check parameter NumberOfProcessors\r
82 //\r
83 if (NumberOfProcessors == NULL) {\r
84 return EFI_INVALID_PARAMETER;\r
85 }\r
86\r
87 //\r
88 // Check parameter NumberOfEnabledProcessors\r
89 //\r
90 if (NumberOfEnabledProcessors == NULL) {\r
91 return EFI_INVALID_PARAMETER;\r
92 }\r
93\r
94 Status = mFrameworkMpService->GetGeneralMPInfo (\r
95 mFrameworkMpService,\r
96 NumberOfProcessors,\r
97 NULL,\r
98 NumberOfEnabledProcessors,\r
99 NULL,\r
100 NULL\r
101 );\r
102 ASSERT_EFI_ERROR (Status);\r
103\r
104 return EFI_SUCCESS;\r
105}\r
106\r
107/**\r
108 Implementation of GetNumberOfProcessors() service of MP Services Protocol.\r
109\r
110 Gets detailed MP-related information on the requested processor at the\r
111 instant this call is made. This service may only be called from the BSP.\r
112\r
113 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
114 @param ProcessorNumber The handle number of processor.\r
115 @param ProcessorInfoBuffer A pointer to the buffer where information for the requested processor is deposited.\r
116\r
117 @retval EFI_SUCCESS Processor information successfully returned.\r
118 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
119 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL\r
120 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist. \r
121\r
122**/\r
123EFI_STATUS\r
124EFIAPI\r
125GetProcessorInfo (\r
126 IN EFI_MP_SERVICES_PROTOCOL *This,\r
127 IN UINTN ProcessorNumber,\r
128 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
129 )\r
130{\r
131 EFI_STATUS Status;\r
132 UINTN CallerNumber;\r
133 UINTN BufferSize;\r
134 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;\r
135\r
136 //\r
137 // Check whether caller processor is BSP\r
138 //\r
139 WhoAmI (This, &CallerNumber);\r
140 if (CallerNumber != GetBspNumber ()) {\r
141 return EFI_DEVICE_ERROR;\r
142 }\r
143\r
144 //\r
145 // Check parameter ProcessorInfoBuffer\r
146 //\r
147 if (ProcessorInfoBuffer == NULL) {\r
148 return EFI_INVALID_PARAMETER;\r
149 }\r
150\r
151 //\r
152 // Check whether processor with the handle specified by ProcessorNumber exists\r
153 //\r
154 if (ProcessorNumber >= mNumberOfProcessors) {\r
155 return EFI_NOT_FOUND;\r
156 }\r
157\r
158 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);\r
159 Status = mFrameworkMpService->GetProcessorContext (\r
160 mFrameworkMpService,\r
161 ProcessorNumber,\r
162 &BufferSize,\r
163 &ProcessorContextBuffer\r
164 );\r
165 ASSERT_EFI_ERROR (Status);\r
166\r
167 ProcessorInfoBuffer->ProcessorId = (UINT64) ProcessorContextBuffer.ApicID;\r
168 \r
169 //\r
170 // Get Status Flag of specified processor\r
171 //\r
172 ProcessorInfoBuffer->StatusFlag = 0;\r
173\r
174 if (ProcessorContextBuffer.Enabled) {\r
175 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
176 }\r
177\r
178 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {\r
179 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
180 }\r
181\r
182 if (ProcessorContextBuffer.Health.Flags.Uint32 == 0) {\r
183 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
184 }\r
185\r
186 ProcessorInfoBuffer->Location.Package = (UINT32) ProcessorContextBuffer.PackageNumber;\r
187 ProcessorInfoBuffer->Location.Core = (UINT32) ProcessorContextBuffer.NumberOfCores;\r
188 ProcessorInfoBuffer->Location.Thread = (UINT32) ProcessorContextBuffer.NumberOfThreads;\r
189\r
190 return EFI_SUCCESS;\r
191}\r
192\r
193/**\r
194 Implementation of StartupAllAPs() service of MP Services Protocol.\r
195\r
196 This service lets the caller get all enabled APs to execute a caller-provided function.\r
197 This service may only be called from the BSP.\r
198\r
199 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
200 @param Procedure A pointer to the function to be run on enabled APs of the system.\r
201 @param SingleThread Indicates whether to execute the function simultaneously or one by one..\r
202 @param WaitEvent The event created by the caller.\r
203 If it is NULL, then execute in blocking mode.\r
204 If it is not NULL, then execute in non-blocking mode.\r
205 @param TimeoutInMicroSeconds The time limit in microseconds for this AP to finish the function.\r
206 Zero means infinity.\r
207 @param ProcedureArgument Pointer to the optional parameter of the assigned function.\r
208 @param FailedCpuList The list of processor numbers that fail to finish the function before\r
209 TimeoutInMicrosecsond expires.\r
210\r
211 @retval EFI_SUCCESS In blocking mode, all APs have finished before the timeout expired. \r
212 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to all enabled APs.\r
213 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
214 @retval EFI_NOT_STARTED No enabled AP exists in the system.\r
215 @retval EFI_NOT_READY Any enabled AP is busy.\r
216 @retval EFI_TIMEOUT In blocking mode, The timeout expired before all enabled APs have finished.\r
217 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
218\r
219**/\r
220EFI_STATUS\r
221EFIAPI\r
222StartupAllAPs (\r
223 IN EFI_MP_SERVICES_PROTOCOL *This,\r
224 IN EFI_AP_PROCEDURE Procedure,\r
225 IN BOOLEAN SingleThread,\r
226 IN EFI_EVENT WaitEvent OPTIONAL,\r
227 IN UINTN TimeoutInMicroSeconds,\r
228 IN VOID *ProcedureArgument OPTIONAL,\r
229 OUT UINTN **FailedCpuList OPTIONAL\r
230 )\r
231{\r
232 EFI_STATUS Status;\r
233 UINTN ProcessorNumber;\r
234 CPU_DATA_BLOCK *CpuData;\r
235 BOOLEAN Blocking;\r
236 UINTN BspNumber;\r
237\r
238 if (FailedCpuList != NULL) {\r
239 *FailedCpuList = NULL;\r
240 }\r
241\r
242 //\r
243 // Check whether caller processor is BSP\r
244 //\r
245 BspNumber = GetBspNumber ();\r
246 WhoAmI (This, &ProcessorNumber);\r
247 if (ProcessorNumber != BspNumber) {\r
248 return EFI_DEVICE_ERROR;\r
249 }\r
250\r
251 //\r
252 // Check parameter Procedure\r
253 //\r
254 if (Procedure == NULL) {\r
255 return EFI_INVALID_PARAMETER;\r
256 }\r
257\r
258 //\r
259 // Temporarily suppress CheckAPsStatus()\r
260 //\r
261 mStopCheckAPsStatus = TRUE;\r
262\r
263 //\r
264 // Check whether all enabled APs are idle.\r
265 // If any enabled AP is not idle, return EFI_NOT_READY.\r
266 //\r
267 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
268\r
269 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
270\r
271 mMPSystemData.CpuList[ProcessorNumber] = FALSE;\r
272 if (ProcessorNumber != BspNumber) {\r
273 if (CpuData->State != CpuStateDisabled) {\r
274 if (CpuData->State != CpuStateIdle) {\r
275 mStopCheckAPsStatus = FALSE;\r
276 return EFI_NOT_READY;\r
277 } else {\r
278 // \r
279 // Mark this processor as responsible for current calling.\r
280 //\r
281 mMPSystemData.CpuList[ProcessorNumber] = TRUE;\r
282 }\r
283 }\r
284 }\r
285 }\r
286\r
287 mMPSystemData.FinishCount = 0;\r
288 mMPSystemData.StartCount = 0;\r
289 Blocking = FALSE;\r
290 //\r
291 // Go through all enabled APs to wakeup them for Procedure.\r
292 // If in Single Thread mode, then only one AP is woken up, and others are waiting.\r
293 //\r
294 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
295\r
296 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
297 //\r
298 // Check whether this processor is responsible for current calling.\r
299 //\r
300 if (mMPSystemData.CpuList[ProcessorNumber]) {\r
301\r
302 mMPSystemData.StartCount++;\r
303\r
304 AcquireSpinLock (&CpuData->CpuDataLock);\r
305 CpuData->State = CpuStateReady;\r
306 ReleaseSpinLock (&CpuData->CpuDataLock);\r
307\r
308 if (!Blocking) {\r
309 WakeUpAp (\r
310 ProcessorNumber,\r
311 Procedure,\r
312 ProcedureArgument\r
313 );\r
314 }\r
315\r
316 if (SingleThread) {\r
317 Blocking = TRUE;\r
318 }\r
319 }\r
320 }\r
321 \r
322 //\r
323 // If no enabled AP exists, return EFI_NOT_STARTED.\r
324 //\r
325 if (mMPSystemData.StartCount == 0) {\r
326 mStopCheckAPsStatus = FALSE;\r
327 return EFI_NOT_STARTED;\r
328 }\r
329\r
330 //\r
331 // If WaitEvent is not NULL, execute in non-blocking mode.\r
332 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
333 // CheckAPsStatus() will check completion and timeout periodically.\r
334 //\r
335 mMPSystemData.Procedure = Procedure;\r
336 mMPSystemData.ProcArguments = ProcedureArgument;\r
337 mMPSystemData.SingleThread = SingleThread;\r
338 mMPSystemData.FailedCpuList = FailedCpuList;\r
339 mMPSystemData.ExpectedTime = CalculateTimeout (TimeoutInMicroSeconds, &mMPSystemData.CurrentTime);\r
340 mMPSystemData.WaitEvent = WaitEvent;\r
341\r
342 //\r
343 // Allow CheckAPsStatus()\r
344 //\r
345 mStopCheckAPsStatus = FALSE;\r
346\r
347 if (WaitEvent != NULL) {\r
348 return EFI_SUCCESS;\r
349 }\r
350\r
351 //\r
352 // If WaitEvent is NULL, execute in blocking mode.\r
353 // BSP checks APs'state until all APs finish or TimeoutInMicrosecsond expires.\r
354 //\r
355 do {\r
356 Status = CheckAllAPs ();\r
357 } while (Status == EFI_NOT_READY);\r
358\r
359 return Status;\r
360}\r
361\r
362/**\r
363 Implementation of StartupThisAP() service of MP Services Protocol.\r
364\r
365 This service lets the caller get one enabled AP to execute a caller-provided function.\r
366 This service may only be called from the BSP.\r
367\r
368 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
369 @param Procedure A pointer to the function to be run on the designated AP.\r
370 @param ProcessorNumber The handle number of AP..\r
371 @param WaitEvent The event created by the caller.\r
372 If it is NULL, then execute in blocking mode.\r
373 If it is not NULL, then execute in non-blocking mode.\r
374 @param TimeoutInMicroseconds The time limit in microseconds for this AP to finish the function.\r
375 Zero means infinity.\r
376 @param ProcedureArgument Pointer to the optional parameter of the assigned function.\r
377 @param Finished Indicates whether AP has finished assigned function.\r
378 In blocking mode, it is ignored.\r
379\r
380 @retval EFI_SUCCESS In blocking mode, specified AP has finished before the timeout expires.\r
381 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to specified AP.\r
382 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
383 @retval EFI_TIMEOUT In blocking mode, the timeout expires before specified AP has finished.\r
384 @retval EFI_NOT_READY Specified AP is busy.\r
385 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.\r
386 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
387 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
388\r
389**/\r
390EFI_STATUS\r
391EFIAPI\r
392StartupThisAP (\r
393 IN EFI_MP_SERVICES_PROTOCOL *This,\r
394 IN EFI_AP_PROCEDURE Procedure,\r
395 IN UINTN ProcessorNumber,\r
396 IN EFI_EVENT WaitEvent OPTIONAL,\r
397 IN UINTN TimeoutInMicroseconds,\r
398 IN VOID *ProcedureArgument OPTIONAL,\r
399 OUT BOOLEAN *Finished OPTIONAL\r
400 )\r
401{\r
402 CPU_DATA_BLOCK *CpuData;\r
403 UINTN CallerNumber;\r
404 EFI_STATUS Status;\r
405 UINTN BspNumber;\r
406\r
407 if (Finished != NULL) {\r
408 *Finished = TRUE;\r
409 }\r
410\r
411 //\r
412 // Check whether caller processor is BSP\r
413 //\r
414 BspNumber = GetBspNumber ();\r
415 WhoAmI (This, &CallerNumber);\r
416 if (CallerNumber != BspNumber) {\r
417 return EFI_DEVICE_ERROR;\r
418 }\r
419\r
420 //\r
421 // Check whether processor with the handle specified by ProcessorNumber exists\r
422 //\r
423 if (ProcessorNumber >= mNumberOfProcessors) {\r
424 return EFI_NOT_FOUND;\r
425 }\r
426\r
427 //\r
428 // Check whether specified processor is BSP\r
429 //\r
430 if (ProcessorNumber == BspNumber) {\r
431 return EFI_INVALID_PARAMETER;\r
432 }\r
433\r
434 //\r
435 // Check parameter Procedure\r
436 //\r
437 if (Procedure == NULL) {\r
438 return EFI_INVALID_PARAMETER;\r
439 }\r
440\r
441 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
442\r
443 //\r
444 // Temporarily suppress CheckAPsStatus()\r
445 //\r
446 mStopCheckAPsStatus = TRUE;\r
447\r
448 //\r
449 // Check whether specified AP is disabled\r
450 //\r
451 if (CpuData->State == CpuStateDisabled) {\r
452 mStopCheckAPsStatus = FALSE;\r
453 return EFI_INVALID_PARAMETER;\r
454 }\r
455\r
456 //\r
457 // Check whether specified AP is busy\r
458 //\r
459 if (CpuData->State != CpuStateIdle) {\r
460 mStopCheckAPsStatus = FALSE;\r
461 return EFI_NOT_READY;\r
462 }\r
463\r
464 //\r
465 // Wakeup specified AP for Procedure.\r
466 //\r
467 AcquireSpinLock (&CpuData->CpuDataLock);\r
468 CpuData->State = CpuStateReady;\r
469 ReleaseSpinLock (&CpuData->CpuDataLock);\r
470\r
471 WakeUpAp (\r
472 ProcessorNumber,\r
473 Procedure,\r
474 ProcedureArgument\r
475 );\r
476\r
477 //\r
478 // If WaitEvent is not NULL, execute in non-blocking mode.\r
479 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
480 // CheckAPsStatus() will check completion and timeout periodically.\r
481 //\r
482 CpuData->WaitEvent = WaitEvent;\r
483 CpuData->Finished = Finished;\r
484 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
485\r
486 //\r
487 // Allow CheckAPsStatus()\r
488 //\r
489 mStopCheckAPsStatus = FALSE;\r
490\r
491 if (WaitEvent != NULL) {\r
492 return EFI_SUCCESS;\r
493 }\r
494\r
495 //\r
496 // If WaitEvent is NULL, execute in blocking mode.\r
497 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
498 //\r
499 do {\r
500 Status = CheckThisAP (ProcessorNumber);\r
501 } while (Status == EFI_NOT_READY);\r
502\r
503 return Status;\r
504}\r
505\r
506/**\r
507 Implementation of SwitchBSP() service of MP Services Protocol.\r
508\r
509 This service switches the requested AP to be the BSP from that point onward.\r
510 This service may only be called from the current BSP.\r
511\r
512 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
513 @param ProcessorNumber The handle number of processor.\r
514 @param EnableOldBSP Whether to enable or disable the original BSP.\r
515\r
516 @retval EFI_SUCCESS BSP successfully switched.\r
517 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
518 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.\r
519 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
520 @retval EFI_NOT_READY Specified AP is busy.\r
521\r
522**/\r
523EFI_STATUS\r
524EFIAPI\r
525SwitchBSP (\r
526 IN EFI_MP_SERVICES_PROTOCOL *This,\r
527 IN UINTN ProcessorNumber,\r
528 IN BOOLEAN EnableOldBSP\r
529 )\r
530{\r
531 EFI_STATUS Status;\r
532 CPU_DATA_BLOCK *CpuData;\r
533 UINTN CallerNumber;\r
534 UINTN BspNumber;\r
535\r
536 //\r
537 // Check whether caller processor is BSP\r
538 //\r
539 BspNumber = GetBspNumber ();\r
540 WhoAmI (This, &CallerNumber);\r
541 if (CallerNumber != BspNumber) {\r
542 return EFI_DEVICE_ERROR;\r
543 }\r
544\r
545 //\r
546 // Check whether processor with the handle specified by ProcessorNumber exists\r
547 //\r
548 if (ProcessorNumber >= mNumberOfProcessors) {\r
549 return EFI_NOT_FOUND;\r
550 }\r
551\r
552 //\r
553 // Check whether specified processor is BSP\r
554 //\r
555 if (ProcessorNumber == BspNumber) {\r
556 return EFI_INVALID_PARAMETER;\r
557 }\r
558\r
559 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
560\r
561 //\r
562 // Check whether specified AP is disabled\r
563 //\r
564 if (CpuData->State == CpuStateDisabled) {\r
565 return EFI_INVALID_PARAMETER;\r
566 }\r
567\r
568 //\r
569 // Check whether specified AP is busy\r
570 //\r
571 if (CpuData->State != CpuStateIdle) {\r
572 return EFI_NOT_READY;\r
573 }\r
574\r
575 Status = mFrameworkMpService->SwitchBSP (\r
576 mFrameworkMpService,\r
577 ProcessorNumber,\r
578 EnableOldBSP\r
579 );\r
580 ASSERT_EFI_ERROR (Status);\r
581\r
582 ChangeCpuState (BspNumber, EnableOldBSP);\r
583\r
584 return EFI_SUCCESS;\r
585}\r
586\r
587/**\r
588 Implementation of EnableDisableAP() service of MP Services Protocol.\r
589\r
590 This service lets the caller enable or disable an AP.\r
591 This service may only be called from the BSP.\r
592\r
593 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
594 @param ProcessorNumber The handle number of processor.\r
595 @param EnableAP Indicates whether the newstate of the AP is enabled or disabled.\r
596 @param HealthFlag Indicates new health state of the AP..\r
597\r
598 @retval EFI_SUCCESS AP successfully enabled or disabled.\r
599 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
600 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.\r
601 @retval EFI_INVALID_PARAMETERS ProcessorNumber specifies the BSP.\r
602 \r
603**/\r
604EFI_STATUS\r
605EFIAPI\r
606EnableDisableAP (\r
607 IN EFI_MP_SERVICES_PROTOCOL *This,\r
608 IN UINTN ProcessorNumber,\r
609 IN BOOLEAN EnableAP,\r
610 IN UINT32 *HealthFlag OPTIONAL\r
611 )\r
612{\r
613 EFI_STATUS Status;\r
614 UINTN CallerNumber;\r
615 EFI_MP_HEALTH HealthState;\r
616 EFI_MP_HEALTH *HealthStatePointer;\r
617 UINTN BspNumber;\r
618\r
619 //\r
620 // Check whether caller processor is BSP\r
621 //\r
622 BspNumber = GetBspNumber ();\r
623 WhoAmI (This, &CallerNumber);\r
624 if (CallerNumber != BspNumber) {\r
625 return EFI_DEVICE_ERROR;\r
626 }\r
627\r
628 //\r
629 // Check whether processor with the handle specified by ProcessorNumber exists\r
630 //\r
631 if (ProcessorNumber >= mNumberOfProcessors) {\r
632 return EFI_NOT_FOUND;\r
633 }\r
634\r
635 //\r
636 // Check whether specified processor is BSP\r
637 //\r
638 if (ProcessorNumber == BspNumber) {\r
639 return EFI_INVALID_PARAMETER;\r
640 }\r
641\r
642 if (HealthFlag == NULL) {\r
643 HealthStatePointer = NULL;\r
644 } else {\r
645 if ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) == 0) {\r
646 HealthState.Flags.Uint32 = 1;\r
647 } else {\r
648 HealthState.Flags.Uint32 = 0;\r
649 }\r
650 HealthState.TestStatus = 0;\r
651\r
652 HealthStatePointer = &HealthState;\r
653 }\r
654\r
655 Status = mFrameworkMpService->EnableDisableAP (\r
656 mFrameworkMpService,\r
657 ProcessorNumber,\r
658 EnableAP,\r
659 HealthStatePointer\r
660 );\r
661 ASSERT_EFI_ERROR (Status);\r
662\r
663 ChangeCpuState (ProcessorNumber, EnableAP);\r
664 \r
665 return EFI_SUCCESS;\r
666}\r
667\r
668/**\r
669 Implementation of WhoAmI() service of MP Services Protocol.\r
670\r
671 This service lets the caller processor get its handle number.\r
672 This service may be called from the BSP and APs.\r
673\r
674 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
675 @param ProcessorNumber Pointer to the handle number of AP.\r
676\r
677 @retval EFI_SUCCESS Processor number successfully returned.\r
678 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL\r
679\r
680**/\r
681EFI_STATUS\r
682EFIAPI\r
683WhoAmI (\r
684 IN EFI_MP_SERVICES_PROTOCOL *This,\r
685 OUT UINTN *ProcessorNumber\r
686 )\r
687{\r
688 EFI_STATUS Status;\r
689\r
690 if (ProcessorNumber == NULL) {\r
691 return EFI_INVALID_PARAMETER;\r
692 }\r
693\r
694 Status = mFrameworkMpService->WhoAmI (\r
695 mFrameworkMpService,\r
696 ProcessorNumber\r
697 );\r
698 ASSERT_EFI_ERROR (Status);\r
699\r
700 return EFI_SUCCESS;\r
701}\r
702\r
703/**\r
704 Checks APs' status periodically.\r
705\r
706 This function is triggerred by timer perodically to check the\r
707 state of APs for StartupAllAPs() and StartupThisAP() executed\r
708 in non-blocking mode.\r
709\r
710 @param Event Event triggered.\r
711 @param Context Parameter passed with the event.\r
712\r
713**/\r
714VOID\r
715EFIAPI\r
716CheckAPsStatus (\r
717 IN EFI_EVENT Event,\r
718 IN VOID *Context\r
719 )\r
720{\r
721 UINTN ProcessorNumber;\r
722 CPU_DATA_BLOCK *CpuData;\r
723 EFI_STATUS Status;\r
724 \r
725 //\r
726 // If CheckAPsStatus() is stopped, then return immediately.\r
727 //\r
728 if (mStopCheckAPsStatus) {\r
729 return;\r
730 }\r
731\r
732 //\r
733 // First, check whether pending StartupAllAPs() exists.\r
734 //\r
735 if (mMPSystemData.WaitEvent != NULL) {\r
736\r
737 Status = CheckAllAPs ();\r
738 //\r
739 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it..\r
740 //\r
741 if (Status != EFI_NOT_READY) {\r
742 Status = gBS->SignalEvent (mMPSystemData.WaitEvent);\r
743 mMPSystemData.WaitEvent = NULL;\r
744 }\r
745 }\r
746\r
747 //\r
748 // Second, check whether pending StartupThisAPs() callings exist.\r
749 //\r
750 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
751\r
752 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
753\r
754 if (CpuData->WaitEvent == NULL) {\r
755 continue;\r
756 }\r
757\r
758 Status = CheckThisAP (ProcessorNumber);\r
759\r
760 if (Status != EFI_NOT_READY) {\r
761 gBS->SignalEvent (CpuData->WaitEvent);\r
762 CpuData->WaitEvent = NULL;\r
763 }\r
764 }\r
765 return ;\r
766}\r
767\r
768/**\r
769 Checks status of all APs.\r
770\r
771 This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
772 and whether timeout expires.\r
773\r
774 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
775 @retval EFI_TIMEOUT The timeout expires.\r
776 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
777\r
778**/\r
779EFI_STATUS\r
780CheckAllAPs (\r
781 VOID\r
782 )\r
783{\r
784 UINTN ProcessorNumber;\r
785 UINTN NextProcessorNumber;\r
786 UINTN ListIndex;\r
787 EFI_STATUS Status;\r
788 CPU_STATE CpuState;\r
789 CPU_DATA_BLOCK *CpuData;\r
790\r
791 NextProcessorNumber = 0;\r
792\r
793 //\r
794 // Go through all APs that are responsible for the StartupAllAPs().\r
795 //\r
796 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
797 if (!mMPSystemData.CpuList[ProcessorNumber]) {\r
798 continue;\r
799 }\r
800\r
801 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
802\r
803 //\r
804 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
805 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
806 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
807 //\r
808 AcquireSpinLock (&CpuData->CpuDataLock);\r
809 CpuState = CpuData->State;\r
810 ReleaseSpinLock (&CpuData->CpuDataLock);\r
811\r
812 if (CpuState == CpuStateFinished) {\r
813 mMPSystemData.FinishCount++;\r
814 mMPSystemData.CpuList[ProcessorNumber] = FALSE;\r
815\r
816 AcquireSpinLock (&CpuData->CpuDataLock);\r
817 CpuData->State = CpuStateIdle;\r
818 ReleaseSpinLock (&CpuData->CpuDataLock);\r
819\r
820 //\r
821 // If in Single Thread mode, then search for the next waiting AP for execution.\r
822 //\r
823 if (mMPSystemData.SingleThread) {\r
824 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
825\r
826 if (!EFI_ERROR (Status)) {\r
827 WakeUpAp (\r
828 NextProcessorNumber,\r
829 mMPSystemData.Procedure,\r
830 mMPSystemData.ProcArguments\r
831 );\r
832 }\r
833 }\r
834 }\r
835 }\r
836\r
837 //\r
838 // If all APs finish, return EFI_SUCCESS.\r
839 //\r
840 if (mMPSystemData.FinishCount == mMPSystemData.StartCount) {\r
841 return EFI_SUCCESS;\r
842 }\r
843\r
844 //\r
845 // If timeout expires, report timeout.\r
846 //\r
847 if (CheckTimeout (&mMPSystemData.CurrentTime, &mMPSystemData.TotalTime, mMPSystemData.ExpectedTime)) {\r
848 //\r
849 // If FailedCpuList is not NULL, record all failed APs in it.\r
850 //\r
851 if (mMPSystemData.FailedCpuList != NULL) {\r
852 *mMPSystemData.FailedCpuList = AllocatePool ((mMPSystemData.StartCount - mMPSystemData.FinishCount + 1) * sizeof(UINTN));\r
853 ASSERT (*mMPSystemData.FailedCpuList != NULL);\r
854 }\r
855 ListIndex = 0;\r
856 \r
857 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
858 //\r
859 // Check whether this processor is responsible for StartupAllAPs().\r
860 //\r
861 if (mMPSystemData.CpuList[ProcessorNumber]) {\r
862 //\r
863 // Reset failed APs to idle state\r
864 //\r
865 ResetProcessorToIdleState (ProcessorNumber);\r
866 mMPSystemData.CpuList[ProcessorNumber] = FALSE;\r
867 if (mMPSystemData.FailedCpuList != NULL) {\r
868 (*mMPSystemData.FailedCpuList)[ListIndex++] = ProcessorNumber;\r
869 }\r
870 }\r
871 }\r
872 if (mMPSystemData.FailedCpuList != NULL) {\r
873 (*mMPSystemData.FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
874 }\r
875 return EFI_TIMEOUT;\r
876 }\r
877 return EFI_NOT_READY;\r
878}\r
879\r
880/**\r
881 Checks status of specified AP.\r
882\r
883 This function checks whether specified AP has finished task assigned by StartupThisAP(),\r
884 and whether timeout expires.\r
885\r
886 @param ProcessorNumber The handle number of processor.\r
887\r
888 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
889 @retval EFI_TIMEOUT The timeout expires.\r
890 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
891\r
892**/\r
893EFI_STATUS\r
894CheckThisAP (\r
895 UINTN ProcessorNumber\r
896 )\r
897{\r
898 CPU_DATA_BLOCK *CpuData;\r
899 CPU_STATE CpuState;\r
900\r
901 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
902\r
903 //\r
904 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
905 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
906 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
907 //\r
908 AcquireSpinLock (&CpuData->CpuDataLock);\r
909 CpuState = CpuData->State;\r
910 ReleaseSpinLock (&CpuData->CpuDataLock);\r
911\r
912 //\r
913 // If the APs finishes for StartupThisAP(), return EFI_SUCCESS.\r
914 //\r
915 if (CpuState == CpuStateFinished) {\r
916\r
917 AcquireSpinLock (&CpuData->CpuDataLock);\r
918 CpuData->State = CpuStateIdle;\r
919 ReleaseSpinLock (&CpuData->CpuDataLock);\r
920 \r
921 if (CpuData->Finished != NULL) {\r
922 *(CpuData->Finished) = TRUE;\r
923 }\r
924 return EFI_SUCCESS;\r
925 } else {\r
926 //\r
927 // If timeout expires for StartupThisAP(), report timeout.\r
928 //\r
929 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
930\r
931 if (CpuData->Finished != NULL) {\r
932 *(CpuData->Finished) = FALSE;\r
933 }\r
934 //\r
935 // Reset failed AP to idle state\r
936 //\r
937 ResetProcessorToIdleState (ProcessorNumber);\r
938\r
939 return EFI_TIMEOUT;\r
940 }\r
941 }\r
942 return EFI_NOT_READY;\r
943}\r
944\r
945/**\r
946 Calculate timeout value and return the current performance counter value.\r
947\r
948 Calculate the number of performance counter ticks required for a timeout.\r
949 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
950 as infinity.\r
951\r
952 @param TimeoutInMicroseconds Timeout value in microseconds.\r
953 @param CurrentTime Returns the current value of the performance counter.\r
954\r
955 @return Expected timestamp counter for timeout.\r
956 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
957 as infinity.\r
958\r
959**/\r
960UINT64\r
961CalculateTimeout (\r
962 IN UINTN TimeoutInMicroseconds,\r
963 OUT UINT64 *CurrentTime\r
964 )\r
965{\r
966 //\r
967 // Read the current value of the performance counter\r
968 //\r
969 *CurrentTime = GetPerformanceCounter ();\r
970\r
971 //\r
972 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
973 // as infinity.\r
974 //\r
975 if (TimeoutInMicroseconds == 0) {\r
976 return 0;\r
977 }\r
978\r
979 //\r
980 // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
981 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide\r
982 // it by 1,000,000, to get the number of ticks for the timeout value.\r
983 //\r
984 return DivU64x32 (\r
985 MultU64x64 (\r
986 GetPerformanceCounterProperties (NULL, NULL),\r
987 TimeoutInMicroseconds\r
988 ),\r
989 1000000\r
990 );\r
991}\r
992\r
993/**\r
994 Checks whether timeout expires.\r
995\r
996 Check whether the number of ellapsed performance counter ticks required for a timeout condition\r
997 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.\r
998\r
999 @param PreviousTime On input, the value of the performance counter when it was last read.\r
1000 On output, the current value of the performance counter\r
1001 @param TotalTime The total amount of ellapsed time in performance counter ticks.\r
1002 @param Timeout The number of performance counter ticks required to reach a timeout condition.\r
1003\r
1004 @retval TRUE A timeout condition has been reached.\r
1005 @retval FALSE A timeout condition has not been reached.\r
1006\r
1007**/\r
1008BOOLEAN\r
1009CheckTimeout (\r
1010 IN OUT UINT64 *PreviousTime,\r
1011 IN UINT64 *TotalTime,\r
1012 IN UINT64 Timeout\r
1013 )\r
1014{\r
1015 UINT64 Start;\r
1016 UINT64 End;\r
1017 UINT64 CurrentTime;\r
1018 INT64 Delta;\r
1019 INT64 Cycle;\r
1020\r
1021 if (Timeout == 0) {\r
1022 return FALSE;\r
1023 }\r
1024 GetPerformanceCounterProperties (&Start, &End);\r
1025 Cycle = End - Start;\r
1026 if (Cycle < 0) {\r
1027 Cycle = -Cycle;\r
1028 }\r
1029 Cycle++;\r
1030 CurrentTime = GetPerformanceCounter();\r
1031 Delta = (INT64) (CurrentTime - *PreviousTime);\r
1032 if (Start > End) {\r
1033 Delta = -Delta;\r
1034 }\r
1035 if (Delta < 0) {\r
1036 Delta += Cycle;\r
1037 }\r
1038 *TotalTime += Delta;\r
1039 *PreviousTime = CurrentTime;\r
1040 if (*TotalTime > Timeout) {\r
1041 return TRUE;\r
1042 }\r
1043 return FALSE;\r
1044}\r
1045\r
1046/**\r
1047 Searches for the next waiting AP.\r
1048\r
1049 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
1050\r
1051 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
1052\r
1053 @retval EFI_SUCCESS The next waiting AP has been found.\r
1054 @retval EFI_NOT_FOUND No waiting AP exists.\r
1055\r
1056**/\r
1057EFI_STATUS\r
1058GetNextWaitingProcessorNumber (\r
1059 OUT UINTN *NextProcessorNumber\r
1060 )\r
1061{\r
1062 UINTN ProcessorNumber;\r
1063\r
1064 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
1065\r
1066 if (mMPSystemData.CpuList[ProcessorNumber]) {\r
1067 *NextProcessorNumber = ProcessorNumber;\r
1068 return EFI_SUCCESS;\r
1069 }\r
1070 }\r
1071\r
1072 return EFI_NOT_FOUND;\r
1073}\r
1074\r
1075/**\r
1076 Wrapper function for all procedures assigned to AP.\r
1077\r
1078 Wrapper function for all procedures assigned to AP via MP service protocol.\r
1079 It controls states of AP and invokes assigned precedure.\r
1080\r
1081**/\r
1082VOID\r
1083ApProcWrapper (\r
1084 VOID\r
1085 )\r
1086{\r
1087 EFI_AP_PROCEDURE Procedure;\r
1088 VOID *Parameter;\r
1089 UINTN ProcessorNumber;\r
1090 CPU_DATA_BLOCK *CpuData;\r
1091\r
1092 WhoAmI (&mMpService, &ProcessorNumber);\r
1093 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1094\r
1095 AcquireSpinLock (&CpuData->CpuDataLock);\r
1096 CpuData->State = CpuStateBusy;\r
1097 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1098\r
1099 //\r
1100 // Now let us check it out.\r
1101 //\r
1102 AcquireSpinLock (&CpuData->CpuDataLock);\r
1103 Procedure = CpuData->Procedure;\r
1104 Parameter = CpuData->Parameter;\r
1105 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1106\r
1107 if (Procedure != NULL) {\r
1108\r
1109 Procedure (Parameter);\r
1110\r
1111 //\r
1112 // if BSP is switched to AP, it continue execute from here, but it carries register state\r
1113 // of the old AP, so need to reload CpuData (might be stored in a register after compiler\r
1114 // optimization) to make sure it points to the right data\r
1115 //\r
1116 WhoAmI (&mMpService, &ProcessorNumber);\r
1117 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1118\r
1119 AcquireSpinLock (&CpuData->CpuDataLock);\r
1120 CpuData->Procedure = NULL;\r
1121 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1122 }\r
1123\r
1124 AcquireSpinLock (&CpuData->CpuDataLock);\r
1125 CpuData->State = CpuStateFinished;\r
1126 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1127}\r
1128\r
1129/**\r
1130 Sends INIT-SIPI-SIPI to AP.\r
1131\r
1132 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.\r
1133\r
1134 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.\r
1135 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.\r
1136 @param ApFunction The procedure for AP to work on.\r
1137\r
1138**/\r
1139VOID\r
1140SendInitSipiSipi (\r
1141 IN BOOLEAN Broadcast,\r
1142 IN UINT32 ApicID,\r
1143 IN VOID *ApFunction\r
1144 )\r
1145{\r
1146 UINTN ApicBase;\r
1147 UINT32 ICRLow;\r
1148 UINT32 ICRHigh;\r
1149 \r
1150 UINT32 VectorNumber;\r
1151 UINT32 DeliveryMode;\r
1152\r
1153 mExchangeInfo->ApFunction = ApFunction;\r
1154 mExchangeInfo->StackStart = mStackStartAddress;\r
1155\r
1156 if (Broadcast) {\r
1157 ICRHigh = 0;\r
1158 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1159 } else {\r
1160 ICRHigh = ApicID << 24;\r
1161 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1162 }\r
1163\r
1164 VectorNumber = 0;\r
1165 DeliveryMode = DELIVERY_MODE_INIT;\r
1166 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1167\r
1168 ApicBase = 0xfee00000;\r
1169\r
1170 //\r
1171 // Write Interrupt Command Registers to send INIT IPI.\r
1172 //\r
1173 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_HIGH_OFFSET, ICRHigh);\r
1174 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1175\r
1176 MicroSecondDelay (10);\r
1177\r
1178 VectorNumber = (UINT32) RShiftU64 (mStartupVector, 12);\r
1179 DeliveryMode = DELIVERY_MODE_SIPI;\r
1180 if (Broadcast) {\r
1181 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1182 } else {\r
1183 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1184 }\r
1185\r
1186 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1187\r
1188 //\r
1189 // Write Interrupt Command Register to send first SIPI IPI.\r
1190 //\r
1191 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1192\r
1193 MicroSecondDelay (200);\r
1194\r
1195 //\r
1196 // Write Interrupt Command Register to send second SIPI IPI.\r
1197 //\r
1198 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1199}\r
1200\r
1201/**\r
1202 Function to wake up a specified AP and assign procedure to it.\r
1203 \r
1204 @param ProcessorNumber Handle number of the specified processor.\r
1205 @param Procedure Procedure to assign.\r
1206 @param ProcArguments Argument for Procedure.\r
1207\r
1208**/\r
1209VOID\r
1210WakeUpAp (\r
1211 IN UINTN ProcessorNumber,\r
1212 IN EFI_AP_PROCEDURE Procedure,\r
1213 IN VOID *ProcArguments\r
1214 )\r
1215{\r
1216 EFI_STATUS Status;\r
1217 CPU_DATA_BLOCK *CpuData;\r
1218 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1219\r
1220 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1221\r
1222 AcquireSpinLock (&CpuData->CpuDataLock);\r
1223 CpuData->Parameter = ProcArguments;\r
1224 CpuData->Procedure = Procedure;\r
1225 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1226\r
1227 Status = GetProcessorInfo (\r
1228 &mMpService,\r
1229 ProcessorNumber,\r
1230 &ProcessorInfoBuffer\r
1231 );\r
1232 ASSERT_EFI_ERROR (Status);\r
1233\r
1234 SendInitSipiSipi (\r
1235 FALSE,\r
1236 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1237 (VOID *) (UINTN) ApProcWrapper\r
1238 );\r
1239}\r
1240\r
1241/**\r
1242 Terminate AP's task and set it to idle state.\r
1243 \r
1244 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1245 and sends it to idle state.\r
1246\r
1247 @param ProcessorNumber Handle number of the specified processor.\r
1248\r
1249**/\r
1250VOID\r
1251ResetProcessorToIdleState (\r
1252 UINTN ProcessorNumber\r
1253 )\r
1254{\r
1255 EFI_STATUS Status;\r
1256 CPU_DATA_BLOCK *CpuData;\r
1257 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1258\r
1259 Status = GetProcessorInfo (\r
1260 &mMpService,\r
1261 ProcessorNumber,\r
1262 &ProcessorInfoBuffer\r
1263 );\r
1264 ASSERT_EFI_ERROR (Status);\r
1265\r
1266 SendInitSipiSipi (\r
1267 FALSE,\r
1268 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1269 NULL\r
1270 );\r
1271\r
1272 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1273\r
1274 AcquireSpinLock (&CpuData->CpuDataLock);\r
1275 CpuData->State = CpuStateIdle;\r
1276 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1277}\r
1278\r
1279/**\r
1280 Worker function of EnableDisableAP ()\r
1281\r
1282 Worker function of EnableDisableAP (). Changes state of specified processor.\r
1283\r
1284 @param ProcessorNumber Processor number of specified AP.\r
1285 @param NewState Desired state of the specified AP.\r
1286\r
1287 @retval EFI_SUCCESS AP's state successfully changed.\r
1288\r
1289**/\r
1290EFI_STATUS\r
1291ChangeCpuState (\r
1292 IN UINTN ProcessorNumber,\r
1293 IN BOOLEAN NewState\r
1294 )\r
1295{\r
1296 CPU_DATA_BLOCK *CpuData;\r
1297\r
1298 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1299\r
1300 if (!NewState) {\r
1301 AcquireSpinLock (&CpuData->CpuDataLock);\r
1302 CpuData->State = CpuStateDisabled;\r
1303 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1304 } else {\r
1305 AcquireSpinLock (&CpuData->CpuDataLock);\r
1306 CpuData->State = CpuStateIdle;\r
1307 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1308 }\r
1309\r
1310 return EFI_SUCCESS;\r
1311}\r
1312\r
1313/**\r
1314 Test memory region of EfiGcdMemoryTypeReserved.\r
1315\r
1316 @param Length The length of memory region to test.\r
1317\r
1318 @retval EFI_SUCCESS The memory region passes test.\r
1319 @retval EFI_NOT_FOUND The memory region is not reserved memory.\r
1320 @retval EFI_DEVICE_ERROR The memory fails on test.\r
1321\r
1322**/\r
1323EFI_STATUS\r
1324TestReservedMemory (\r
1325 UINTN Length\r
1326 )\r
1327{\r
1328 EFI_STATUS Status;\r
1329 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
1330 EFI_PHYSICAL_ADDRESS Address;\r
1331 UINTN LengthCovered;\r
1332 UINTN RemainingLength;\r
1333\r
1334 //\r
1335 // Walk through the memory descriptors covering the memory range.\r
1336 //\r
1337 Address = mStartupVector;\r
1338 RemainingLength = Length;\r
1339 while (Address < mStartupVector + Length) {\r
1340 Status = gDS->GetMemorySpaceDescriptor(\r
1341 Address,\r
1342 &Descriptor\r
1343 );\r
1344 if (EFI_ERROR (Status)) {\r
1345 return EFI_NOT_FOUND;\r
1346 }\r
1347\r
1348 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {\r
1349 return EFI_NOT_FOUND;\r
1350 }\r
1351 //\r
1352 // Calculated the length of the intersected range.\r
1353 //\r
1354 LengthCovered = (UINTN) (Descriptor.BaseAddress + Descriptor.Length - Address);\r
1355 if (LengthCovered > RemainingLength) {\r
1356 LengthCovered = RemainingLength;\r
1357 }\r
1358\r
1359 Status = mGenMemoryTest->CompatibleRangeTest (\r
1360 mGenMemoryTest,\r
1361 Address,\r
1362 LengthCovered\r
1363 );\r
1364 if (EFI_ERROR (Status)) {\r
1365 return EFI_DEVICE_ERROR;\r
1366 }\r
1367\r
1368 Address += LengthCovered;\r
1369 RemainingLength -= LengthCovered;\r
1370 }\r
1371\r
1372 return EFI_SUCCESS;\r
1373}\r
1374\r
1375/**\r
1376 Allocates startup vector for APs.\r
1377\r
1378 This function allocates Startup vector for APs.\r
1379\r
1380 @param Size The size of startup vector.\r
1381\r
1382**/\r
1383VOID\r
1384AllocateStartupVector (\r
1385 UINTN Size\r
1386 )\r
1387{\r
1388 EFI_STATUS Status;\r
1389\r
1390 Status = gBS->LocateProtocol (\r
1391 &gEfiGenericMemTestProtocolGuid,\r
1392 NULL,\r
1393 (VOID **) &mGenMemoryTest\r
1394 );\r
1395 if (EFI_ERROR (Status)) {\r
1396 mGenMemoryTest = NULL;\r
1397 }\r
1398\r
1399 for (mStartupVector = 0x7F000; mStartupVector >= 0x2000; mStartupVector -= EFI_PAGE_SIZE) {\r
1400 if (mGenMemoryTest != NULL) {\r
1401 //\r
1402 // Test memory if it is EfiGcdMemoryTypeReserved.\r
1403 //\r
1404 Status = TestReservedMemory (EFI_SIZE_TO_PAGES (Size) * EFI_PAGE_SIZE);\r
1405 if (Status == EFI_DEVICE_ERROR) {\r
1406 continue;\r
1407 }\r
1408 }\r
1409\r
1410 Status = gBS->AllocatePages (\r
1411 AllocateAddress,\r
1412 EfiBootServicesCode,\r
1413 EFI_SIZE_TO_PAGES (Size),\r
1414 &mStartupVector\r
1415 );\r
1416\r
1417 if (!EFI_ERROR (Status)) {\r
1418 break;\r
1419 }\r
1420 }\r
1421\r
1422 ASSERT_EFI_ERROR (Status);\r
1423}\r
1424\r
1425/**\r
1426 Prepares Startup Vector for APs.\r
1427\r
1428 This function prepares Startup Vector for APs.\r
1429\r
1430**/\r
1431VOID\r
1432PrepareAPStartupVector (\r
1433 VOID\r
1434 )\r
1435{\r
1436 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
1437 IA32_DESCRIPTOR GdtrForBSP;\r
1438\r
1439 //\r
1440 // Get the address map of startup code for AP,\r
1441 // including code size, and offset of long jump instructions to redirect.\r
1442 //\r
1443 AsmGetAddressMap (&AddressMap);\r
1444\r
1445 //\r
1446 // Allocate a 4K-aligned region under 1M for startup vector for AP.\r
1447 // The region contains AP startup code and exchange data between BSP and AP.\r
1448 //\r
1449 AllocateStartupVector (AddressMap.Size + sizeof (MP_CPU_EXCHANGE_INFO));\r
1450\r
1451 //\r
1452 // Copy AP startup code to startup vector, and then redirect the long jump\r
1453 // instructions for mode switching.\r
1454 //\r
1455 CopyMem ((VOID *) (UINTN) mStartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);\r
1456 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32) (mStartupVector + AddressMap.PModeEntryOffset);\r
1457 //\r
1458 // For IA32 mode, LongJumpOffset is filled with zero. If non-zero, then we are in X64 mode, so further redirect for long mode switch.\r
1459 //\r
1460 if (AddressMap.LongJumpOffset != 0) {\r
1461 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.LongJumpOffset + 2) = (UINT32) (mStartupVector + AddressMap.LModeEntryOffset);\r
1462 }\r
1463\r
1464 //\r
1465 // Get the start address of exchange data between BSP and AP.\r
1466 //\r
1467 mExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (mStartupVector + AddressMap.Size);\r
1468\r
1469 ZeroMem ((VOID *) mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));\r
1470\r
1471 mStackStartAddress = AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER * AP_STACK_SIZE));\r
1472 mExchangeInfo->StackSize = AP_STACK_SIZE;\r
1473\r
1474 AsmReadGdtr (&GdtrForBSP);\r
1475 mExchangeInfo->GdtrProfile.Base = GdtrForBSP.Base;\r
1476 mExchangeInfo->GdtrProfile.Limit = GdtrForBSP.Limit;\r
1477\r
1478 mExchangeInfo->BufferStart = (UINT32) mStartupVector;\r
1479 mExchangeInfo->Cr3 = (UINT32) (AsmReadCr3 ());\r
1480}\r
1481\r
1482/**\r
1483 Prepares memory region for processor configuration.\r
1484 \r
1485 This function prepares memory region for processor configuration.\r
1486\r
1487**/\r
1488VOID\r
1489PrepareMemoryForConfiguration (\r
1490 VOID\r
1491 )\r
1492{\r
1493 UINTN Index;\r
1494\r
1495 //\r
1496 // Initialize Spin Locks for system\r
1497 //\r
1498 InitializeSpinLock (&mMPSystemData.APSerializeLock);\r
1499 for (Index = 0; Index < MAX_CPU_NUMBER; Index++) {\r
1500 InitializeSpinLock (&mMPSystemData.CpuData[Index].CpuDataLock);\r
1501 }\r
1502 \r
1503 PrepareAPStartupVector ();\r
1504}\r
1505\r
1506/**\r
1507 Gets the processor number of BSP.\r
1508 \r
1509 @return The processor number of BSP.\r
1510\r
1511**/\r
1512UINTN\r
1513GetBspNumber (\r
1514 VOID\r
1515 )\r
1516{\r
1517 UINTN ProcessorNumber;\r
1518 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;\r
1519 EFI_STATUS Status;\r
1520 UINTN BufferSize;\r
1521\r
1522 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);\r
1523\r
1524 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
1525 Status = mFrameworkMpService->GetProcessorContext (\r
1526 mFrameworkMpService,\r
1527 ProcessorNumber,\r
1528 &BufferSize,\r
1529 &ProcessorContextBuffer\r
1530 );\r
1531 ASSERT_EFI_ERROR (Status);\r
1532 \r
1533 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {\r
1534 break;\r
1535 }\r
1536 }\r
1537 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
1538\r
1539 return ProcessorNumber;\r
1540}\r
1541\r
1542/**\r
1543 Entrypoint of MP Services Protocol thunk driver.\r
1544\r
1545 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1546 @param[in] SystemTable A pointer to the EFI System Table.\r
1547 \r
1548 @retval EFI_SUCCESS The entry point is executed successfully.\r
1549\r
1550**/\r
1551EFI_STATUS\r
1552EFIAPI\r
1553InitializeMpServicesProtocol (\r
1554 IN EFI_HANDLE ImageHandle,\r
1555 IN EFI_SYSTEM_TABLE *SystemTable\r
1556 )\r
1557{\r
1558 EFI_STATUS Status;\r
1559\r
1560 PrepareMemoryForConfiguration ();\r
1561\r
1562 //\r
1563 // Locates Framework version MP Services Protocol\r
1564 //\r
1565 Status = gBS->LocateProtocol (\r
1566 &gFrameworkEfiMpServiceProtocolGuid, \r
1567 NULL, \r
1568 (VOID **) &mFrameworkMpService\r
1569 );\r
1570 ASSERT_EFI_ERROR (Status);\r
1571\r
1572 Status = mFrameworkMpService->GetGeneralMPInfo (\r
1573 mFrameworkMpService,\r
1574 &mNumberOfProcessors,\r
1575 NULL,\r
1576 NULL,\r
1577 NULL,\r
1578 NULL\r
1579 );\r
1580 ASSERT_EFI_ERROR (Status);\r
1581\r
1582 //\r
1583 // Create timer event to check AP state for non-blocking execution.\r
1584 //\r
1585 Status = gBS->CreateEvent (\r
1586 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1587 TPL_CALLBACK,\r
1588 CheckAPsStatus,\r
1589 NULL,\r
1590 &mMPSystemData.CheckAPsEvent\r
1591 );\r
1592 ASSERT_EFI_ERROR (Status);\r
1593\r
1594 //\r
1595 // Now install the MP services protocol.\r
1596 //\r
1597 Status = gBS->InstallProtocolInterface (\r
1598 &mHandle,\r
1599 &gEfiMpServiceProtocolGuid,\r
1600 EFI_NATIVE_INTERFACE,\r
1601 &mMpService\r
1602 );\r
1603 ASSERT_EFI_ERROR (Status);\r
1604\r
1605 //\r
1606 // Launch the timer event to check AP state.\r
1607 //\r
1608 Status = gBS->SetTimer (\r
1609 mMPSystemData.CheckAPsEvent,\r
1610 TimerPeriodic,\r
1611 100000\r
1612 );\r
1613 ASSERT_EFI_ERROR (Status);\r
1614\r
1615 return EFI_SUCCESS;\r
1616}\r