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