]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.c
Add support for e1000 NICs to OVMF
[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
de243ee4 120 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.\r
768e2a90 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
de243ee4 168\r
768e2a90 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
de243ee4 211 @retval EFI_SUCCESS In blocking mode, all APs have finished before the timeout expired.\r
768e2a90 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
de243ee4 278 //\r
768e2a90 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
de243ee4 321\r
768e2a90 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
de243ee4 602\r
768e2a90 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
de243ee4 664\r
768e2a90 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
de243ee4 724\r
768e2a90 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
de243ee4 856\r
768e2a90 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
de243ee4 923\r
768e2a90 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
de243ee4 1078/**\r
1079 Programs Local APIC registers for virtual wire mode.\r
1080\r
1081 This function programs Local APIC registers for virtual wire mode.\r
1082\r
1083 @param Bsp Indicates whether the programmed processor is going to be BSP\r
1084\r
1085**/\r
1086VOID\r
1087ProgramVirtualWireMode (\r
1088 BOOLEAN Bsp\r
1089 )\r
1090{\r
1091 UINTN ApicBase;\r
1092 UINT32 Value;\r
1093\r
1094 ApicBase = (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;\r
1095\r
1096 //\r
1097 // Program the Spurious Vector entry\r
1098 // Set bit 8 (APIC Software Enable/Disable) to enable local APIC,\r
1099 // and set Spurious Vector as 0x0F.\r
1100 //\r
1101 MmioBitFieldWrite32 (ApicBase + APIC_REGISTER_SPURIOUS_VECTOR_OFFSET, 0, 9, 0x10F);\r
1102\r
1103 //\r
1104 // Program the LINT0 vector entry as ExtInt\r
1105 // Set bits 8..10 to 7 as ExtInt Delivery Mode,\r
1106 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,\r
1107 // Trigger Mode, and Mask\r
1108 //\r
1109 if (!Bsp) {\r
1110 DisableInterrupts ();\r
1111 }\r
1112 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET);\r
1113 Value = BitFieldWrite32 (Value, 8, 10, 7);\r
1114 Value = BitFieldWrite32 (Value, 12, 16, 0);\r
1115 if (!Bsp) {\r
1116 //\r
1117 // For APs, LINT0 is masked\r
1118 //\r
1119 Value = BitFieldWrite32 (Value, 16, 16, 1);\r
1120 }\r
1121 MmioWrite32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET, Value);\r
1122\r
1123 //\r
1124 // Program the LINT1 vector entry as NMI\r
1125 // Set bits 8..10 to 4 as NMI Delivery Mode,\r
1126 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,\r
1127 // Trigger Mode.\r
1128 // For BSP clear Mask bit, and for AP set mask bit.\r
1129 //\r
1130 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET);\r
1131 Value = BitFieldWrite32 (Value, 8, 10, 4);\r
1132 Value = BitFieldWrite32 (Value, 12, 16, 0);\r
1133 if (!Bsp) {\r
1134 //\r
1135 // For APs, LINT1 is masked\r
1136 //\r
1137 Value = BitFieldWrite32 (Value, 16, 16, 1);\r
1138 }\r
1139 MmioWrite32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET, Value);\r
1140}\r
1141\r
1142\r
768e2a90 1143/**\r
1144 Wrapper function for all procedures assigned to AP.\r
1145\r
1146 Wrapper function for all procedures assigned to AP via MP service protocol.\r
1147 It controls states of AP and invokes assigned precedure.\r
1148\r
1149**/\r
1150VOID\r
1151ApProcWrapper (\r
1152 VOID\r
1153 )\r
1154{\r
1155 EFI_AP_PROCEDURE Procedure;\r
1156 VOID *Parameter;\r
1157 UINTN ProcessorNumber;\r
1158 CPU_DATA_BLOCK *CpuData;\r
1159\r
de243ee4 1160 ProgramVirtualWireMode (FALSE);\r
1161\r
768e2a90 1162 WhoAmI (&mMpService, &ProcessorNumber);\r
1163 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1164\r
1165 AcquireSpinLock (&CpuData->CpuDataLock);\r
1166 CpuData->State = CpuStateBusy;\r
1167 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1168\r
1169 //\r
1170 // Now let us check it out.\r
1171 //\r
1172 AcquireSpinLock (&CpuData->CpuDataLock);\r
1173 Procedure = CpuData->Procedure;\r
1174 Parameter = CpuData->Parameter;\r
1175 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1176\r
1177 if (Procedure != NULL) {\r
1178\r
1179 Procedure (Parameter);\r
1180\r
1181 //\r
1182 // if BSP is switched to AP, it continue execute from here, but it carries register state\r
1183 // of the old AP, so need to reload CpuData (might be stored in a register after compiler\r
1184 // optimization) to make sure it points to the right data\r
1185 //\r
1186 WhoAmI (&mMpService, &ProcessorNumber);\r
1187 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1188\r
1189 AcquireSpinLock (&CpuData->CpuDataLock);\r
1190 CpuData->Procedure = NULL;\r
1191 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1192 }\r
1193\r
1194 AcquireSpinLock (&CpuData->CpuDataLock);\r
1195 CpuData->State = CpuStateFinished;\r
1196 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1197}\r
1198\r
1199/**\r
1200 Sends INIT-SIPI-SIPI to AP.\r
1201\r
1202 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.\r
1203\r
1204 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.\r
1205 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.\r
1206 @param ApFunction The procedure for AP to work on.\r
1207\r
1208**/\r
1209VOID\r
1210SendInitSipiSipi (\r
1211 IN BOOLEAN Broadcast,\r
1212 IN UINT32 ApicID,\r
1213 IN VOID *ApFunction\r
1214 )\r
1215{\r
1216 UINTN ApicBase;\r
1217 UINT32 ICRLow;\r
1218 UINT32 ICRHigh;\r
de243ee4 1219\r
768e2a90 1220 UINT32 VectorNumber;\r
1221 UINT32 DeliveryMode;\r
1222\r
1223 mExchangeInfo->ApFunction = ApFunction;\r
1224 mExchangeInfo->StackStart = mStackStartAddress;\r
1225\r
1226 if (Broadcast) {\r
1227 ICRHigh = 0;\r
1228 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1229 } else {\r
1230 ICRHigh = ApicID << 24;\r
1231 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1232 }\r
1233\r
1234 VectorNumber = 0;\r
1235 DeliveryMode = DELIVERY_MODE_INIT;\r
1236 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1237\r
1238 ApicBase = 0xfee00000;\r
1239\r
1240 //\r
1241 // Write Interrupt Command Registers to send INIT IPI.\r
1242 //\r
1243 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_HIGH_OFFSET, ICRHigh);\r
1244 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1245\r
1246 MicroSecondDelay (10);\r
1247\r
1248 VectorNumber = (UINT32) RShiftU64 (mStartupVector, 12);\r
1249 DeliveryMode = DELIVERY_MODE_SIPI;\r
1250 if (Broadcast) {\r
1251 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1252 } else {\r
1253 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;\r
1254 }\r
1255\r
1256 ICRLow |= VectorNumber | (DeliveryMode << 8);\r
1257\r
1258 //\r
1259 // Write Interrupt Command Register to send first SIPI IPI.\r
1260 //\r
1261 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1262\r
1263 MicroSecondDelay (200);\r
1264\r
1265 //\r
1266 // Write Interrupt Command Register to send second SIPI IPI.\r
1267 //\r
1268 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);\r
1269}\r
1270\r
1271/**\r
1272 Function to wake up a specified AP and assign procedure to it.\r
de243ee4 1273\r
768e2a90 1274 @param ProcessorNumber Handle number of the specified processor.\r
1275 @param Procedure Procedure to assign.\r
1276 @param ProcArguments Argument for Procedure.\r
1277\r
1278**/\r
1279VOID\r
1280WakeUpAp (\r
1281 IN UINTN ProcessorNumber,\r
1282 IN EFI_AP_PROCEDURE Procedure,\r
1283 IN VOID *ProcArguments\r
1284 )\r
1285{\r
1286 EFI_STATUS Status;\r
1287 CPU_DATA_BLOCK *CpuData;\r
1288 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1289\r
2d7b3c03 1290 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
45590862 1291 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);\r
2d7b3c03 1292\r
768e2a90 1293 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1294\r
1295 AcquireSpinLock (&CpuData->CpuDataLock);\r
1296 CpuData->Parameter = ProcArguments;\r
1297 CpuData->Procedure = Procedure;\r
1298 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1299\r
1300 Status = GetProcessorInfo (\r
1301 &mMpService,\r
1302 ProcessorNumber,\r
1303 &ProcessorInfoBuffer\r
1304 );\r
1305 ASSERT_EFI_ERROR (Status);\r
1306\r
1307 SendInitSipiSipi (\r
1308 FALSE,\r
1309 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1310 (VOID *) (UINTN) ApProcWrapper\r
1311 );\r
1312}\r
1313\r
1314/**\r
1315 Terminate AP's task and set it to idle state.\r
de243ee4 1316\r
768e2a90 1317 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1318 and sends it to idle state.\r
1319\r
1320 @param ProcessorNumber Handle number of the specified processor.\r
1321\r
1322**/\r
1323VOID\r
1324ResetProcessorToIdleState (\r
1325 UINTN ProcessorNumber\r
1326 )\r
1327{\r
1328 EFI_STATUS Status;\r
1329 CPU_DATA_BLOCK *CpuData;\r
1330 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
1331\r
1332 Status = GetProcessorInfo (\r
1333 &mMpService,\r
1334 ProcessorNumber,\r
1335 &ProcessorInfoBuffer\r
1336 );\r
1337 ASSERT_EFI_ERROR (Status);\r
1338\r
1339 SendInitSipiSipi (\r
1340 FALSE,\r
1341 (UINT32) ProcessorInfoBuffer.ProcessorId,\r
1342 NULL\r
1343 );\r
1344\r
1345 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1346\r
1347 AcquireSpinLock (&CpuData->CpuDataLock);\r
1348 CpuData->State = CpuStateIdle;\r
1349 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1350}\r
1351\r
1352/**\r
1353 Worker function of EnableDisableAP ()\r
1354\r
1355 Worker function of EnableDisableAP (). Changes state of specified processor.\r
1356\r
1357 @param ProcessorNumber Processor number of specified AP.\r
1358 @param NewState Desired state of the specified AP.\r
1359\r
1360 @retval EFI_SUCCESS AP's state successfully changed.\r
1361\r
1362**/\r
1363EFI_STATUS\r
1364ChangeCpuState (\r
1365 IN UINTN ProcessorNumber,\r
1366 IN BOOLEAN NewState\r
1367 )\r
1368{\r
1369 CPU_DATA_BLOCK *CpuData;\r
1370\r
2d7b3c03 1371 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
45590862 1372 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);\r
2d7b3c03 1373\r
768e2a90 1374 CpuData = &mMPSystemData.CpuData[ProcessorNumber];\r
1375\r
1376 if (!NewState) {\r
1377 AcquireSpinLock (&CpuData->CpuDataLock);\r
1378 CpuData->State = CpuStateDisabled;\r
1379 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1380 } else {\r
1381 AcquireSpinLock (&CpuData->CpuDataLock);\r
1382 CpuData->State = CpuStateIdle;\r
1383 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1384 }\r
1385\r
1386 return EFI_SUCCESS;\r
1387}\r
1388\r
1389/**\r
1390 Test memory region of EfiGcdMemoryTypeReserved.\r
1391\r
1392 @param Length The length of memory region to test.\r
1393\r
1394 @retval EFI_SUCCESS The memory region passes test.\r
1395 @retval EFI_NOT_FOUND The memory region is not reserved memory.\r
1396 @retval EFI_DEVICE_ERROR The memory fails on test.\r
1397\r
1398**/\r
1399EFI_STATUS\r
1400TestReservedMemory (\r
1401 UINTN Length\r
1402 )\r
1403{\r
1404 EFI_STATUS Status;\r
1405 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
1406 EFI_PHYSICAL_ADDRESS Address;\r
1407 UINTN LengthCovered;\r
1408 UINTN RemainingLength;\r
1409\r
1410 //\r
1411 // Walk through the memory descriptors covering the memory range.\r
1412 //\r
1413 Address = mStartupVector;\r
1414 RemainingLength = Length;\r
1415 while (Address < mStartupVector + Length) {\r
1416 Status = gDS->GetMemorySpaceDescriptor(\r
1417 Address,\r
1418 &Descriptor\r
1419 );\r
1420 if (EFI_ERROR (Status)) {\r
1421 return EFI_NOT_FOUND;\r
1422 }\r
1423\r
1424 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {\r
1425 return EFI_NOT_FOUND;\r
1426 }\r
1427 //\r
1428 // Calculated the length of the intersected range.\r
1429 //\r
1430 LengthCovered = (UINTN) (Descriptor.BaseAddress + Descriptor.Length - Address);\r
1431 if (LengthCovered > RemainingLength) {\r
1432 LengthCovered = RemainingLength;\r
1433 }\r
1434\r
1435 Status = mGenMemoryTest->CompatibleRangeTest (\r
1436 mGenMemoryTest,\r
1437 Address,\r
1438 LengthCovered\r
1439 );\r
1440 if (EFI_ERROR (Status)) {\r
1441 return EFI_DEVICE_ERROR;\r
1442 }\r
1443\r
1444 Address += LengthCovered;\r
1445 RemainingLength -= LengthCovered;\r
1446 }\r
1447\r
1448 return EFI_SUCCESS;\r
1449}\r
1450\r
1451/**\r
1452 Allocates startup vector for APs.\r
1453\r
1454 This function allocates Startup vector for APs.\r
1455\r
1456 @param Size The size of startup vector.\r
1457\r
1458**/\r
1459VOID\r
1460AllocateStartupVector (\r
1461 UINTN Size\r
1462 )\r
1463{\r
1464 EFI_STATUS Status;\r
1465\r
1466 Status = gBS->LocateProtocol (\r
1467 &gEfiGenericMemTestProtocolGuid,\r
1468 NULL,\r
1469 (VOID **) &mGenMemoryTest\r
1470 );\r
1471 if (EFI_ERROR (Status)) {\r
1472 mGenMemoryTest = NULL;\r
1473 }\r
1474\r
1475 for (mStartupVector = 0x7F000; mStartupVector >= 0x2000; mStartupVector -= EFI_PAGE_SIZE) {\r
1476 if (mGenMemoryTest != NULL) {\r
1477 //\r
1478 // Test memory if it is EfiGcdMemoryTypeReserved.\r
1479 //\r
1480 Status = TestReservedMemory (EFI_SIZE_TO_PAGES (Size) * EFI_PAGE_SIZE);\r
1481 if (Status == EFI_DEVICE_ERROR) {\r
1482 continue;\r
1483 }\r
1484 }\r
1485\r
1486 Status = gBS->AllocatePages (\r
1487 AllocateAddress,\r
1488 EfiBootServicesCode,\r
1489 EFI_SIZE_TO_PAGES (Size),\r
1490 &mStartupVector\r
1491 );\r
1492\r
1493 if (!EFI_ERROR (Status)) {\r
1494 break;\r
1495 }\r
1496 }\r
1497\r
1498 ASSERT_EFI_ERROR (Status);\r
1499}\r
1500\r
1501/**\r
1502 Prepares Startup Vector for APs.\r
1503\r
1504 This function prepares Startup Vector for APs.\r
1505\r
1506**/\r
1507VOID\r
1508PrepareAPStartupVector (\r
1509 VOID\r
1510 )\r
1511{\r
1512 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
1513 IA32_DESCRIPTOR GdtrForBSP;\r
de243ee4 1514 IA32_DESCRIPTOR IdtrForBSP;\r
50684330 1515 EFI_PHYSICAL_ADDRESS GdtForAP;\r
de243ee4 1516 EFI_PHYSICAL_ADDRESS IdtForAP;\r
50684330 1517 EFI_STATUS Status;\r
768e2a90 1518\r
1519 //\r
1520 // Get the address map of startup code for AP,\r
1521 // including code size, and offset of long jump instructions to redirect.\r
1522 //\r
1523 AsmGetAddressMap (&AddressMap);\r
1524\r
1525 //\r
1526 // Allocate a 4K-aligned region under 1M for startup vector for AP.\r
1527 // The region contains AP startup code and exchange data between BSP and AP.\r
1528 //\r
1529 AllocateStartupVector (AddressMap.Size + sizeof (MP_CPU_EXCHANGE_INFO));\r
1530\r
1531 //\r
1532 // Copy AP startup code to startup vector, and then redirect the long jump\r
1533 // instructions for mode switching.\r
1534 //\r
1535 CopyMem ((VOID *) (UINTN) mStartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);\r
1536 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32) (mStartupVector + AddressMap.PModeEntryOffset);\r
1537 //\r
1538 // 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
1539 //\r
1540 if (AddressMap.LongJumpOffset != 0) {\r
1541 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.LongJumpOffset + 2) = (UINT32) (mStartupVector + AddressMap.LModeEntryOffset);\r
1542 }\r
1543\r
1544 //\r
1545 // Get the start address of exchange data between BSP and AP.\r
1546 //\r
1547 mExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (mStartupVector + AddressMap.Size);\r
1548\r
1549 ZeroMem ((VOID *) mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));\r
1550\r
1551 mStackStartAddress = AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER * AP_STACK_SIZE));\r
1552 mExchangeInfo->StackSize = AP_STACK_SIZE;\r
1553\r
1554 AsmReadGdtr (&GdtrForBSP);\r
de243ee4 1555 AsmReadIdtr (&IdtrForBSP);\r
50684330 1556\r
1557 //\r
1558 // Allocate memory under 4G to hold GDT for APs\r
1559 //\r
1560 GdtForAP = 0xffffffff;\r
1561 Status = gBS->AllocatePages (\r
1562 AllocateMaxAddress,\r
1563 EfiBootServicesData,\r
de243ee4 1564 EFI_SIZE_TO_PAGES ((GdtrForBSP.Limit + 1) + (IdtrForBSP.Limit + 1)),\r
50684330 1565 &GdtForAP\r
1566 );\r
1567 ASSERT_EFI_ERROR (Status);\r
1568\r
de243ee4 1569 IdtForAP = (UINTN) GdtForAP + GdtrForBSP.Limit + 1;\r
1570\r
50684330 1571 CopyMem ((VOID *) (UINTN) GdtForAP, (VOID *) GdtrForBSP.Base, GdtrForBSP.Limit + 1);\r
de243ee4 1572 CopyMem ((VOID *) (UINTN) IdtForAP, (VOID *) IdtrForBSP.Base, IdtrForBSP.Limit + 1);\r
50684330 1573\r
1574 mExchangeInfo->GdtrProfile.Base = (UINTN) GdtForAP;\r
768e2a90 1575 mExchangeInfo->GdtrProfile.Limit = GdtrForBSP.Limit;\r
de243ee4 1576 mExchangeInfo->IdtrProfile.Base = (UINTN) IdtForAP;\r
1577 mExchangeInfo->IdtrProfile.Limit = IdtrForBSP.Limit;\r
768e2a90 1578\r
1579 mExchangeInfo->BufferStart = (UINT32) mStartupVector;\r
1580 mExchangeInfo->Cr3 = (UINT32) (AsmReadCr3 ());\r
1581}\r
1582\r
1583/**\r
1584 Prepares memory region for processor configuration.\r
de243ee4 1585\r
768e2a90 1586 This function prepares memory region for processor configuration.\r
1587\r
1588**/\r
1589VOID\r
1590PrepareMemoryForConfiguration (\r
1591 VOID\r
1592 )\r
1593{\r
1594 UINTN Index;\r
1595\r
1596 //\r
1597 // Initialize Spin Locks for system\r
1598 //\r
1599 InitializeSpinLock (&mMPSystemData.APSerializeLock);\r
1600 for (Index = 0; Index < MAX_CPU_NUMBER; Index++) {\r
1601 InitializeSpinLock (&mMPSystemData.CpuData[Index].CpuDataLock);\r
1602 }\r
de243ee4 1603\r
768e2a90 1604 PrepareAPStartupVector ();\r
1605}\r
1606\r
1607/**\r
1608 Gets the processor number of BSP.\r
de243ee4 1609\r
768e2a90 1610 @return The processor number of BSP.\r
1611\r
1612**/\r
1613UINTN\r
1614GetBspNumber (\r
1615 VOID\r
1616 )\r
1617{\r
1618 UINTN ProcessorNumber;\r
1619 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;\r
1620 EFI_STATUS Status;\r
1621 UINTN BufferSize;\r
1622\r
1623 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);\r
1624\r
1625 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
1626 Status = mFrameworkMpService->GetProcessorContext (\r
1627 mFrameworkMpService,\r
1628 ProcessorNumber,\r
1629 &BufferSize,\r
1630 &ProcessorContextBuffer\r
1631 );\r
1632 ASSERT_EFI_ERROR (Status);\r
de243ee4 1633\r
768e2a90 1634 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {\r
1635 break;\r
1636 }\r
1637 }\r
1638 ASSERT (ProcessorNumber < mNumberOfProcessors);\r
1639\r
1640 return ProcessorNumber;\r
1641}\r
1642\r
1643/**\r
1644 Entrypoint of MP Services Protocol thunk driver.\r
1645\r
de243ee4 1646 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
768e2a90 1647 @param[in] SystemTable A pointer to the EFI System Table.\r
de243ee4 1648\r
768e2a90 1649 @retval EFI_SUCCESS The entry point is executed successfully.\r
1650\r
1651**/\r
1652EFI_STATUS\r
1653EFIAPI\r
1654InitializeMpServicesProtocol (\r
1655 IN EFI_HANDLE ImageHandle,\r
1656 IN EFI_SYSTEM_TABLE *SystemTable\r
1657 )\r
1658{\r
1659 EFI_STATUS Status;\r
1660\r
1661 PrepareMemoryForConfiguration ();\r
1662\r
1663 //\r
1664 // Locates Framework version MP Services Protocol\r
1665 //\r
1666 Status = gBS->LocateProtocol (\r
de243ee4 1667 &gFrameworkEfiMpServiceProtocolGuid,\r
1668 NULL,\r
768e2a90 1669 (VOID **) &mFrameworkMpService\r
1670 );\r
1671 ASSERT_EFI_ERROR (Status);\r
1672\r
1673 Status = mFrameworkMpService->GetGeneralMPInfo (\r
1674 mFrameworkMpService,\r
1675 &mNumberOfProcessors,\r
1676 NULL,\r
1677 NULL,\r
1678 NULL,\r
1679 NULL\r
1680 );\r
1681 ASSERT_EFI_ERROR (Status);\r
2d7b3c03 1682 ASSERT (mNumberOfProcessors < MAX_CPU_NUMBER);\r
768e2a90 1683\r
1684 //\r
1685 // Create timer event to check AP state for non-blocking execution.\r
1686 //\r
1687 Status = gBS->CreateEvent (\r
1688 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1689 TPL_CALLBACK,\r
1690 CheckAPsStatus,\r
1691 NULL,\r
1692 &mMPSystemData.CheckAPsEvent\r
1693 );\r
1694 ASSERT_EFI_ERROR (Status);\r
1695\r
1696 //\r
1697 // Now install the MP services protocol.\r
1698 //\r
1699 Status = gBS->InstallProtocolInterface (\r
1700 &mHandle,\r
1701 &gEfiMpServiceProtocolGuid,\r
1702 EFI_NATIVE_INTERFACE,\r
1703 &mMpService\r
1704 );\r
1705 ASSERT_EFI_ERROR (Status);\r
1706\r
1707 //\r
1708 // Launch the timer event to check AP state.\r
1709 //\r
1710 Status = gBS->SetTimer (\r
1711 mMPSystemData.CheckAPsEvent,\r
1712 TimerPeriodic,\r
1713 100000\r
1714 );\r
1715 ASSERT_EFI_ERROR (Status);\r
1716\r
1717 return EFI_SUCCESS;\r
1718}\r