]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.c
Add checking whether FvCount is overflow when new unknown FvInfoPpi is dispatched.
[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
2d7b3c03 7Copyright (c) 2009 - 2010 Intel Corporation. <BR>\r
768e2a90 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
2d7b3c03 901 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
902\r
768e2a90 903 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
904\r
905 //\r
906 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
907 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
908 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
909 //\r
910 AcquireSpinLock (&CpuData->CpuDataLock);\r
911 CpuState = CpuData->State;\r
912 ReleaseSpinLock (&CpuData->CpuDataLock);\r
913\r
914 //\r
915 // If the APs finishes for StartupThisAP(), return EFI_SUCCESS.\r
916 //\r
917 if (CpuState == CpuStateFinished) {\r
918\r
919 AcquireSpinLock (&CpuData->CpuDataLock);\r
920 CpuData->State = CpuStateIdle;\r
921 ReleaseSpinLock (&CpuData->CpuDataLock);\r
922 \r
923 if (CpuData->Finished != NULL) {\r
924 *(CpuData->Finished) = TRUE;\r
925 }\r
926 return EFI_SUCCESS;\r
927 } else {\r
928 //\r
929 // If timeout expires for StartupThisAP(), report timeout.\r
930 //\r
931 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
932\r
933 if (CpuData->Finished != NULL) {\r
934 *(CpuData->Finished) = FALSE;\r
935 }\r
936 //\r
937 // Reset failed AP to idle state\r
938 //\r
939 ResetProcessorToIdleState (ProcessorNumber);\r
940\r
941 return EFI_TIMEOUT;\r
942 }\r
943 }\r
944 return EFI_NOT_READY;\r
945}\r
946\r
947/**\r
948 Calculate timeout value and return the current performance counter value.\r
949\r
950 Calculate the number of performance counter ticks required for a timeout.\r
951 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
952 as infinity.\r
953\r
954 @param TimeoutInMicroseconds Timeout value in microseconds.\r
955 @param CurrentTime Returns the current value of the performance counter.\r
956\r
957 @return Expected timestamp counter for timeout.\r
958 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
959 as infinity.\r
960\r
961**/\r
962UINT64\r
963CalculateTimeout (\r
964 IN UINTN TimeoutInMicroseconds,\r
965 OUT UINT64 *CurrentTime\r
966 )\r
967{\r
968 //\r
969 // Read the current value of the performance counter\r
970 //\r
971 *CurrentTime = GetPerformanceCounter ();\r
972\r
973 //\r
974 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
975 // as infinity.\r
976 //\r
977 if (TimeoutInMicroseconds == 0) {\r
978 return 0;\r
979 }\r
980\r
981 //\r
982 // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
983 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide\r
984 // it by 1,000,000, to get the number of ticks for the timeout value.\r
985 //\r
986 return DivU64x32 (\r
987 MultU64x64 (\r
988 GetPerformanceCounterProperties (NULL, NULL),\r
989 TimeoutInMicroseconds\r
990 ),\r
991 1000000\r
992 );\r
993}\r
994\r
995/**\r
996 Checks whether timeout expires.\r
997\r
998 Check whether the number of ellapsed performance counter ticks required for a timeout condition\r
999 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.\r
1000\r
1001 @param PreviousTime On input, the value of the performance counter when it was last read.\r
1002 On output, the current value of the performance counter\r
1003 @param TotalTime The total amount of ellapsed time in performance counter ticks.\r
1004 @param Timeout The number of performance counter ticks required to reach a timeout condition.\r
1005\r
1006 @retval TRUE A timeout condition has been reached.\r
1007 @retval FALSE A timeout condition has not been reached.\r
1008\r
1009**/\r
1010BOOLEAN\r
1011CheckTimeout (\r
1012 IN OUT UINT64 *PreviousTime,\r
1013 IN UINT64 *TotalTime,\r
1014 IN UINT64 Timeout\r
1015 )\r
1016{\r
1017 UINT64 Start;\r
1018 UINT64 End;\r
1019 UINT64 CurrentTime;\r
1020 INT64 Delta;\r
1021 INT64 Cycle;\r
1022\r
1023 if (Timeout == 0) {\r
1024 return FALSE;\r
1025 }\r
1026 GetPerformanceCounterProperties (&Start, &End);\r
1027 Cycle = End - Start;\r
1028 if (Cycle < 0) {\r
1029 Cycle = -Cycle;\r
1030 }\r
1031 Cycle++;\r
1032 CurrentTime = GetPerformanceCounter();\r
1033 Delta = (INT64) (CurrentTime - *PreviousTime);\r
1034 if (Start > End) {\r
1035 Delta = -Delta;\r
1036 }\r
1037 if (Delta < 0) {\r
1038 Delta += Cycle;\r
1039 }\r
1040 *TotalTime += Delta;\r
1041 *PreviousTime = CurrentTime;\r
1042 if (*TotalTime > Timeout) {\r
1043 return TRUE;\r
1044 }\r
1045 return FALSE;\r
1046}\r
1047\r
1048/**\r
1049 Searches for the next waiting AP.\r
1050\r
1051 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
1052\r
1053 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
1054\r
1055 @retval EFI_SUCCESS The next waiting AP has been found.\r
1056 @retval EFI_NOT_FOUND No waiting AP exists.\r
1057\r
1058**/\r
1059EFI_STATUS\r
1060GetNextWaitingProcessorNumber (\r
1061 OUT UINTN *NextProcessorNumber\r
1062 )\r
1063{\r
1064 UINTN ProcessorNumber;\r
1065\r
1066 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
1067\r
1068 if (mMPSystemData.CpuList[ProcessorNumber]) {\r
1069 *NextProcessorNumber = ProcessorNumber;\r
1070 return EFI_SUCCESS;\r
1071 }\r
1072 }\r
1073\r
1074 return EFI_NOT_FOUND;\r
1075}\r
1076\r
1077/**\r
1078 Wrapper function for all procedures assigned to AP.\r
1079\r
1080 Wrapper function for all procedures assigned to AP via MP service protocol.\r
1081 It controls states of AP and invokes assigned precedure.\r
1082\r
1083**/\r
1084VOID\r
1085ApProcWrapper (\r
1086 VOID\r
1087 )\r
1088{\r
1089 EFI_AP_PROCEDURE Procedure;\r
1090 VOID *Parameter;\r
1091 UINTN ProcessorNumber;\r
1092 CPU_DATA_BLOCK *CpuData;\r
1093\r
1094 WhoAmI (&mMpService, &ProcessorNumber);\r
1095 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1096\r
1097 AcquireSpinLock (&CpuData->CpuDataLock);\r
1098 CpuData->State = CpuStateBusy;\r
1099 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1100\r
1101 //\r
1102 // Now let us check it out.\r
1103 //\r
1104 AcquireSpinLock (&CpuData->CpuDataLock);\r
1105 Procedure = CpuData->Procedure;\r
1106 Parameter = CpuData->Parameter;\r
1107 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1108\r
1109 if (Procedure != NULL) {\r
1110\r
1111 Procedure (Parameter);\r
1112\r
1113 //\r
1114 // if BSP is switched to AP, it continue execute from here, but it carries register state\r
1115 // of the old AP, so need to reload CpuData (might be stored in a register after compiler\r
1116 // optimization) to make sure it points to the right data\r
1117 //\r
1118 WhoAmI (&mMpService, &ProcessorNumber);\r
1119 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1120\r
1121 AcquireSpinLock (&CpuData->CpuDataLock);\r
1122 CpuData->Procedure = NULL;\r
1123 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1124 }\r
1125\r
1126 AcquireSpinLock (&CpuData->CpuDataLock);\r
1127 CpuData->State = CpuStateFinished;\r
1128 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1129}\r
1130\r
1131/**\r
1132 Sends INIT-SIPI-SIPI to AP.\r
1133\r
1134 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.\r
1135\r
1136 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.\r
1137 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.\r
1138 @param ApFunction The procedure for AP to work on.\r
1139\r
1140**/\r
1141VOID\r
1142SendInitSipiSipi (\r
1143 IN BOOLEAN Broadcast,\r
1144 IN UINT32 ApicID,\r
1145 IN VOID *ApFunction\r
1146 )\r
1147{\r
1148 UINTN ApicBase;\r
1149 UINT32 ICRLow;\r
1150 UINT32 ICRHigh;\r
1151 \r
1152 UINT32 VectorNumber;\r
1153 UINT32 DeliveryMode;\r
1154\r
1155 mExchangeInfo->ApFunction = ApFunction;\r
1156 mExchangeInfo->StackStart = mStackStartAddress;\r
1157\r
1158 if (Broadcast) {\r
1159 ICRHigh = 0;\r
1160 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1161 } else {\r
1162 ICRHigh = ApicID << 24;\r
1163 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1164 }\r
1165\r
1166 VectorNumber = 0;\r
1167 DeliveryMode = DELIVERY_MODE_INIT;\r
1168 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1169\r
1170 ApicBase = 0xfee00000;\r
1171\r
1172 //\r
1173 // Write Interrupt Command Registers to send INIT IPI.\r
1174 //\r
1175 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_HIGH_OFFSET, ICRHigh);\r
1176 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1177\r
1178 MicroSecondDelay (10);\r
1179\r
1180 VectorNumber = (UINT32) RShiftU64 (mStartupVector, 12);\r
1181 DeliveryMode = DELIVERY_MODE_SIPI;\r
1182 if (Broadcast) {\r
1183 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1184 } else {\r
1185 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1186 }\r
1187\r
1188 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1189\r
1190 //\r
1191 // Write Interrupt Command Register to send first SIPI IPI.\r
1192 //\r
1193 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1194\r
1195 MicroSecondDelay (200);\r
1196\r
1197 //\r
1198 // Write Interrupt Command Register to send second SIPI IPI.\r
1199 //\r
1200 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1201}\r
1202\r
1203/**\r
1204 Function to wake up a specified AP and assign procedure to it.\r
1205 \r
1206 @param ProcessorNumber Handle number of the specified processor.\r
1207 @param Procedure Procedure to assign.\r
1208 @param ProcArguments Argument for Procedure.\r
1209\r
1210**/\r
1211VOID\r
1212WakeUpAp (\r
1213 IN UINTN ProcessorNumber,\r
1214 IN EFI_AP_PROCEDURE Procedure,\r
1215 IN VOID *ProcArguments\r
1216 )\r
1217{\r
1218 EFI_STATUS Status;\r
1219 CPU_DATA_BLOCK *CpuData;\r
1220 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1221\r
2d7b3c03 1222 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
1223\r
768e2a90 1224 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1225\r
1226 AcquireSpinLock (&CpuData->CpuDataLock);\r
1227 CpuData->Parameter = ProcArguments;\r
1228 CpuData->Procedure = Procedure;\r
1229 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1230\r
1231 Status = GetProcessorInfo (\r
1232 &mMpService,\r
1233 ProcessorNumber,\r
1234 &ProcessorInfoBuffer\r
1235 );\r
1236 ASSERT_EFI_ERROR (Status);\r
1237\r
1238 SendInitSipiSipi (\r
1239 FALSE,\r
1240 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1241 (VOID *) (UINTN) ApProcWrapper\r
1242 );\r
1243}\r
1244\r
1245/**\r
1246 Terminate AP's task and set it to idle state.\r
1247 \r
1248 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1249 and sends it to idle state.\r
1250\r
1251 @param ProcessorNumber Handle number of the specified processor.\r
1252\r
1253**/\r
1254VOID\r
1255ResetProcessorToIdleState (\r
1256 UINTN ProcessorNumber\r
1257 )\r
1258{\r
1259 EFI_STATUS Status;\r
1260 CPU_DATA_BLOCK *CpuData;\r
1261 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1262\r
1263 Status = GetProcessorInfo (\r
1264 &mMpService,\r
1265 ProcessorNumber,\r
1266 &ProcessorInfoBuffer\r
1267 );\r
1268 ASSERT_EFI_ERROR (Status);\r
1269\r
1270 SendInitSipiSipi (\r
1271 FALSE,\r
1272 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1273 NULL\r
1274 );\r
1275\r
1276 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1277\r
1278 AcquireSpinLock (&CpuData->CpuDataLock);\r
1279 CpuData->State = CpuStateIdle;\r
1280 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1281}\r
1282\r
1283/**\r
1284 Worker function of EnableDisableAP ()\r
1285\r
1286 Worker function of EnableDisableAP (). Changes state of specified processor.\r
1287\r
1288 @param ProcessorNumber Processor number of specified AP.\r
1289 @param NewState Desired state of the specified AP.\r
1290\r
1291 @retval EFI_SUCCESS AP's state successfully changed.\r
1292\r
1293**/\r
1294EFI_STATUS\r
1295ChangeCpuState (\r
1296 IN UINTN ProcessorNumber,\r
1297 IN BOOLEAN NewState\r
1298 )\r
1299{\r
1300 CPU_DATA_BLOCK *CpuData;\r
1301\r
2d7b3c03 1302 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
1303\r
768e2a90 1304 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1305\r
1306 if (!NewState) {\r
1307 AcquireSpinLock (&CpuData->CpuDataLock);\r
1308 CpuData->State = CpuStateDisabled;\r
1309 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1310 } else {\r
1311 AcquireSpinLock (&CpuData->CpuDataLock);\r
1312 CpuData->State = CpuStateIdle;\r
1313 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1314 }\r
1315\r
1316 return EFI_SUCCESS;\r
1317}\r
1318\r
1319/**\r
1320 Test memory region of EfiGcdMemoryTypeReserved.\r
1321\r
1322 @param Length The length of memory region to test.\r
1323\r
1324 @retval EFI_SUCCESS The memory region passes test.\r
1325 @retval EFI_NOT_FOUND The memory region is not reserved memory.\r
1326 @retval EFI_DEVICE_ERROR The memory fails on test.\r
1327\r
1328**/\r
1329EFI_STATUS\r
1330TestReservedMemory (\r
1331 UINTN Length\r
1332 )\r
1333{\r
1334 EFI_STATUS Status;\r
1335 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
1336 EFI_PHYSICAL_ADDRESS Address;\r
1337 UINTN LengthCovered;\r
1338 UINTN RemainingLength;\r
1339\r
1340 //\r
1341 // Walk through the memory descriptors covering the memory range.\r
1342 //\r
1343 Address = mStartupVector;\r
1344 RemainingLength = Length;\r
1345 while (Address < mStartupVector + Length) {\r
1346 Status = gDS->GetMemorySpaceDescriptor(\r
1347 Address,\r
1348 &Descriptor\r
1349 );\r
1350 if (EFI_ERROR (Status)) {\r
1351 return EFI_NOT_FOUND;\r
1352 }\r
1353\r
1354 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {\r
1355 return EFI_NOT_FOUND;\r
1356 }\r
1357 //\r
1358 // Calculated the length of the intersected range.\r
1359 //\r
1360 LengthCovered = (UINTN) (Descriptor.BaseAddress + Descriptor.Length - Address);\r
1361 if (LengthCovered > RemainingLength) {\r
1362 LengthCovered = RemainingLength;\r
1363 }\r
1364\r
1365 Status = mGenMemoryTest->CompatibleRangeTest (\r
1366 mGenMemoryTest,\r
1367 Address,\r
1368 LengthCovered\r
1369 );\r
1370 if (EFI_ERROR (Status)) {\r
1371 return EFI_DEVICE_ERROR;\r
1372 }\r
1373\r
1374 Address += LengthCovered;\r
1375 RemainingLength -= LengthCovered;\r
1376 }\r
1377\r
1378 return EFI_SUCCESS;\r
1379}\r
1380\r
1381/**\r
1382 Allocates startup vector for APs.\r
1383\r
1384 This function allocates Startup vector for APs.\r
1385\r
1386 @param Size The size of startup vector.\r
1387\r
1388**/\r
1389VOID\r
1390AllocateStartupVector (\r
1391 UINTN Size\r
1392 )\r
1393{\r
1394 EFI_STATUS Status;\r
1395\r
1396 Status = gBS->LocateProtocol (\r
1397 &gEfiGenericMemTestProtocolGuid,\r
1398 NULL,\r
1399 (VOID **) &mGenMemoryTest\r
1400 );\r
1401 if (EFI_ERROR (Status)) {\r
1402 mGenMemoryTest = NULL;\r
1403 }\r
1404\r
1405 for (mStartupVector = 0x7F000; mStartupVector >= 0x2000; mStartupVector -= EFI_PAGE_SIZE) {\r
1406 if (mGenMemoryTest != NULL) {\r
1407 //\r
1408 // Test memory if it is EfiGcdMemoryTypeReserved.\r
1409 //\r
1410 Status = TestReservedMemory (EFI_SIZE_TO_PAGES (Size) * EFI_PAGE_SIZE);\r
1411 if (Status == EFI_DEVICE_ERROR) {\r
1412 continue;\r
1413 }\r
1414 }\r
1415\r
1416 Status = gBS->AllocatePages (\r
1417 AllocateAddress,\r
1418 EfiBootServicesCode,\r
1419 EFI_SIZE_TO_PAGES (Size),\r
1420 &mStartupVector\r
1421 );\r
1422\r
1423 if (!EFI_ERROR (Status)) {\r
1424 break;\r
1425 }\r
1426 }\r
1427\r
1428 ASSERT_EFI_ERROR (Status);\r
1429}\r
1430\r
1431/**\r
1432 Prepares Startup Vector for APs.\r
1433\r
1434 This function prepares Startup Vector for APs.\r
1435\r
1436**/\r
1437VOID\r
1438PrepareAPStartupVector (\r
1439 VOID\r
1440 )\r
1441{\r
1442 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
1443 IA32_DESCRIPTOR GdtrForBSP;\r
1444\r
1445 //\r
1446 // Get the address map of startup code for AP,\r
1447 // including code size, and offset of long jump instructions to redirect.\r
1448 //\r
1449 AsmGetAddressMap (&AddressMap);\r
1450\r
1451 //\r
1452 // Allocate a 4K-aligned region under 1M for startup vector for AP.\r
1453 // The region contains AP startup code and exchange data between BSP and AP.\r
1454 //\r
1455 AllocateStartupVector (AddressMap.Size + sizeof (MP_CPU_EXCHANGE_INFO));\r
1456\r
1457 //\r
1458 // Copy AP startup code to startup vector, and then redirect the long jump\r
1459 // instructions for mode switching.\r
1460 //\r
1461 CopyMem ((VOID *) (UINTN) mStartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);\r
1462 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32) (mStartupVector + AddressMap.PModeEntryOffset);\r
1463 //\r
1464 // 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
1465 //\r
1466 if (AddressMap.LongJumpOffset != 0) {\r
1467 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.LongJumpOffset + 2) = (UINT32) (mStartupVector + AddressMap.LModeEntryOffset);\r
1468 }\r
1469\r
1470 //\r
1471 // Get the start address of exchange data between BSP and AP.\r
1472 //\r
1473 mExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (mStartupVector + AddressMap.Size);\r
1474\r
1475 ZeroMem ((VOID *) mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));\r
1476\r
1477 mStackStartAddress = AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER * AP_STACK_SIZE));\r
1478 mExchangeInfo->StackSize = AP_STACK_SIZE;\r
1479\r
1480 AsmReadGdtr (&GdtrForBSP);\r
1481 mExchangeInfo->GdtrProfile.Base = GdtrForBSP.Base;\r
1482 mExchangeInfo->GdtrProfile.Limit = GdtrForBSP.Limit;\r
1483\r
1484 mExchangeInfo->BufferStart = (UINT32) mStartupVector;\r
1485 mExchangeInfo->Cr3 = (UINT32) (AsmReadCr3 ());\r
1486}\r
1487\r
1488/**\r
1489 Prepares memory region for processor configuration.\r
1490 \r
1491 This function prepares memory region for processor configuration.\r
1492\r
1493**/\r
1494VOID\r
1495PrepareMemoryForConfiguration (\r
1496 VOID\r
1497 )\r
1498{\r
1499 UINTN Index;\r
1500\r
1501 //\r
1502 // Initialize Spin Locks for system\r
1503 //\r
1504 InitializeSpinLock (&mMPSystemData.APSerializeLock);\r
1505 for (Index = 0; Index < MAX_CPU_NUMBER; Index++) {\r
1506 InitializeSpinLock (&mMPSystemData.CpuData[Index].CpuDataLock);\r
1507 }\r
1508 \r
1509 PrepareAPStartupVector ();\r
1510}\r
1511\r
1512/**\r
1513 Gets the processor number of BSP.\r
1514 \r
1515 @return The processor number of BSP.\r
1516\r
1517**/\r
1518UINTN\r
1519GetBspNumber (\r
1520 VOID\r
1521 )\r
1522{\r
1523 UINTN ProcessorNumber;\r
1524 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;\r
1525 EFI_STATUS Status;\r
1526 UINTN BufferSize;\r
1527\r
1528 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);\r
1529\r
1530 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
1531 Status = mFrameworkMpService->GetProcessorContext (\r
1532 mFrameworkMpService,\r
1533 ProcessorNumber,\r
1534 &BufferSize,\r
1535 &ProcessorContextBuffer\r
1536 );\r
1537 ASSERT_EFI_ERROR (Status);\r
1538 \r
1539 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {\r
1540 break;\r
1541 }\r
1542 }\r
1543 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
1544\r
1545 return ProcessorNumber;\r
1546}\r
1547\r
1548/**\r
1549 Entrypoint of MP Services Protocol thunk driver.\r
1550\r
1551 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1552 @param[in] SystemTable A pointer to the EFI System Table.\r
1553 \r
1554 @retval EFI_SUCCESS The entry point is executed successfully.\r
1555\r
1556**/\r
1557EFI_STATUS\r
1558EFIAPI\r
1559InitializeMpServicesProtocol (\r
1560 IN EFI_HANDLE ImageHandle,\r
1561 IN EFI_SYSTEM_TABLE *SystemTable\r
1562 )\r
1563{\r
1564 EFI_STATUS Status;\r
1565\r
1566 PrepareMemoryForConfiguration ();\r
1567\r
1568 //\r
1569 // Locates Framework version MP Services Protocol\r
1570 //\r
1571 Status = gBS->LocateProtocol (\r
1572 &gFrameworkEfiMpServiceProtocolGuid, \r
1573 NULL, \r
1574 (VOID **) &mFrameworkMpService\r
1575 );\r
1576 ASSERT_EFI_ERROR (Status);\r
1577\r
1578 Status = mFrameworkMpService->GetGeneralMPInfo (\r
1579 mFrameworkMpService,\r
1580 &mNumberOfProcessors,\r
1581 NULL,\r
1582 NULL,\r
1583 NULL,\r
1584 NULL\r
1585 );\r
1586 ASSERT_EFI_ERROR (Status);\r
2d7b3c03 1587 ASSERT (mNumberOfProcessors < MAX_CPU_NUMBER);\r
768e2a90 1588\r
1589 //\r
1590 // Create timer event to check AP state for non-blocking execution.\r
1591 //\r
1592 Status = gBS->CreateEvent (\r
1593 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1594 TPL_CALLBACK,\r
1595 CheckAPsStatus,\r
1596 NULL,\r
1597 &mMPSystemData.CheckAPsEvent\r
1598 );\r
1599 ASSERT_EFI_ERROR (Status);\r
1600\r
1601 //\r
1602 // Now install the MP services protocol.\r
1603 //\r
1604 Status = gBS->InstallProtocolInterface (\r
1605 &mHandle,\r
1606 &gEfiMpServiceProtocolGuid,\r
1607 EFI_NATIVE_INTERFACE,\r
1608 &mMpService\r
1609 );\r
1610 ASSERT_EFI_ERROR (Status);\r
1611\r
1612 //\r
1613 // Launch the timer event to check AP state.\r
1614 //\r
1615 Status = gBS->SetTimer (\r
1616 mMPSystemData.CheckAPsEvent,\r
1617 TimerPeriodic,\r
1618 100000\r
1619 );\r
1620 ASSERT_EFI_ERROR (Status);\r
1621\r
1622 return EFI_SUCCESS;\r
1623}\r