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