UefiCpuPkg/CpuMpPei: Load microcode if found newer revision
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / PeiMpServices.c
CommitLineData
ea0f431c
JF
1/** @file\r
2 Implementation of Multiple Processor PPI services.\r
3\r
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PeiMpServices.h"\r
16\r
17//\r
18// CPU MP PPI to be installed\r
19//\r
20EFI_PEI_MP_SERVICES_PPI mMpServicesPpi = {\r
21 PeiGetNumberOfProcessors,\r
22 PeiGetProcessorInfo,\r
23 PeiStartupAllAPs,\r
24 PeiStartupThisAP,\r
25 PeiSwitchBSP,\r
26 PeiEnableDisableAP,\r
27 PeiWhoAmI,\r
28};\r
29\r
30EFI_PEI_PPI_DESCRIPTOR mPeiCpuMpPpiDesc = {\r
31 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
32 &gEfiPeiMpServicesPpiGuid,\r
33 &mMpServicesPpi\r
34};\r
35\r
36/**\r
37 Get CPU Package/Core/Thread location information.\r
38\r
39 @param InitialApicId CPU APIC ID\r
40 @param Location Pointer to CPU location information\r
41**/\r
42VOID\r
43ExtractProcessorLocation (\r
44 IN UINT32 InitialApicId,\r
45 OUT EFI_CPU_PHYSICAL_LOCATION *Location\r
46 )\r
47{\r
48 BOOLEAN TopologyLeafSupported;\r
49 UINTN ThreadBits;\r
50 UINTN CoreBits;\r
51 UINT32 RegEax;\r
52 UINT32 RegEbx;\r
53 UINT32 RegEcx;\r
54 UINT32 RegEdx;\r
55 UINT32 MaxCpuIdIndex;\r
56 UINT32 SubIndex;\r
57 UINTN LevelType;\r
58 UINT32 MaxLogicProcessorsPerPackage;\r
59 UINT32 MaxCoresPerPackage;\r
60\r
61 //\r
62 // Check if the processor is capable of supporting more than one logical processor.\r
63 //\r
64 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &RegEdx);\r
65 if ((RegEdx & BIT28) == 0) {\r
66 Location->Thread = 0;\r
67 Location->Core = 0;\r
68 Location->Package = 0;\r
69 return;\r
70 }\r
71\r
72 ThreadBits = 0;\r
73 CoreBits = 0;\r
74\r
75 //\r
76 // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
77 //\r
78\r
79 TopologyLeafSupported = FALSE;\r
80 //\r
81 // Get the max index of basic CPUID\r
82 //\r
83 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
84\r
85 //\r
86 // If the extended topology enumeration leaf is available, it\r
87 // is the preferred mechanism for enumerating topology.\r
88 //\r
89 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
90 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, &RegEax, &RegEbx, &RegEcx, NULL);\r
91 //\r
92 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
93 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
94 // supported on that processor.\r
95 //\r
96 if (RegEbx != 0) {\r
97 TopologyLeafSupported = TRUE;\r
98\r
99 //\r
100 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
101 // the SMT sub-field of x2APIC ID.\r
102 //\r
103 LevelType = (RegEcx >> 8) & 0xff;\r
104 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
105 ThreadBits = RegEax & 0x1f;\r
106\r
107 //\r
108 // Software must not assume any "level type" encoding\r
109 // value to be related to any sub-leaf index, except sub-leaf 0.\r
110 //\r
111 SubIndex = 1;\r
112 do {\r
113 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, SubIndex, &RegEax, NULL, &RegEcx, NULL);\r
114 LevelType = (RegEcx >> 8) & 0xff;\r
115 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
116 CoreBits = (RegEax & 0x1f) - ThreadBits;\r
117 break;\r
118 }\r
119 SubIndex++;\r
120 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
121 }\r
122 }\r
123\r
124 if (!TopologyLeafSupported) {\r
125 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
126 MaxLogicProcessorsPerPackage = (RegEbx >> 16) & 0xff;\r
127 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
128 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &RegEax, NULL, NULL, NULL);\r
129 MaxCoresPerPackage = (RegEax >> 26) + 1;\r
130 } else {\r
131 //\r
132 // Must be a single-core processor.\r
133 //\r
134 MaxCoresPerPackage = 1;\r
135 }\r
136\r
137 ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
138 CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);\r
139 }\r
140\r
141 Location->Thread = InitialApicId & ~((-1) << ThreadBits);\r
142 Location->Core = (InitialApicId >> ThreadBits) & ~((-1) << CoreBits);\r
143 Location->Package = (InitialApicId >> (ThreadBits + CoreBits));\r
144}\r
145\r
146/**\r
147 Find the current Processor number by APIC ID.\r
148\r
149 @param PeiCpuMpData Pointer to PEI CPU MP Data\r
150 @param ProcessorNumber Return the pocessor number found\r
151\r
152 @retval EFI_SUCCESS ProcessorNumber is found and returned.\r
153 @retval EFI_NOT_FOUND ProcessorNumber is not found.\r
154**/\r
155EFI_STATUS\r
156GetProcessorNumber (\r
157 IN PEI_CPU_MP_DATA *PeiCpuMpData,\r
158 OUT UINTN *ProcessorNumber\r
159 )\r
160{\r
161 UINTN TotalProcessorNumber;\r
162 UINTN Index;\r
163\r
164 TotalProcessorNumber = PeiCpuMpData->CpuCount;\r
165 for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
166 if (PeiCpuMpData->CpuData[Index].ApicId == GetInitialApicId ()) {\r
167 *ProcessorNumber = Index;\r
168 return EFI_SUCCESS;\r
169 }\r
170 }\r
171 return EFI_NOT_FOUND;\r
172}\r
173\r
174/**\r
175 Worker function for SwitchBSP().\r
176\r
177 Worker function for SwitchBSP(), assigned to the AP which is intended to become BSP.\r
178\r
179 @param Buffer Pointer to CPU MP Data\r
180**/\r
181VOID\r
182EFIAPI\r
183FutureBSPProc (\r
184 IN VOID *Buffer\r
185 )\r
186{\r
187 PEI_CPU_MP_DATA *DataInHob;\r
188\r
189 DataInHob = (PEI_CPU_MP_DATA *) Buffer;\r
190 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);\r
191}\r
192\r
193/**\r
194 This service retrieves the number of logical processor in the platform\r
195 and the number of those logical processors that are enabled on this boot.\r
196 This service may only be called from the BSP.\r
197\r
198 This function is used to retrieve the following information:\r
199 - The number of logical processors that are present in the system.\r
200 - The number of enabled logical processors in the system at the instant\r
201 this call is made.\r
202\r
203 Because MP Service Ppi provides services to enable and disable processors\r
204 dynamically, the number of enabled logical processors may vary during the\r
205 course of a boot session.\r
206\r
207 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
208 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
209 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
210 is returned in NumberOfProcessors, the number of currently enabled processor\r
211 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
212\r
213 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
214 published by the PEI Foundation.\r
215 @param[in] This Pointer to this instance of the PPI.\r
216 @param[out] NumberOfProcessors Pointer to the total number of logical processors in\r
217 the system, including the BSP and disabled APs.\r
218 @param[out] NumberOfEnabledProcessors\r
219 Number of processors in the system that are enabled.\r
220\r
221 @retval EFI_SUCCESS The number of logical processors and enabled\r
222 logical processors was retrieved.\r
223 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
224 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
225 NumberOfEnabledProcessors is NULL.\r
226**/\r
227EFI_STATUS\r
228EFIAPI\r
229PeiGetNumberOfProcessors (\r
230 IN CONST EFI_PEI_SERVICES **PeiServices,\r
231 IN EFI_PEI_MP_SERVICES_PPI *This,\r
232 OUT UINTN *NumberOfProcessors,\r
233 OUT UINTN *NumberOfEnabledProcessors\r
234 )\r
235{\r
236 PEI_CPU_MP_DATA *PeiCpuMpData;\r
237 UINTN CallerNumber;\r
238 UINTN ProcessorNumber;\r
239 UINTN EnabledProcessorNumber;\r
240 UINTN Index;\r
241\r
242 PeiCpuMpData = GetMpHobData ();\r
243 if (PeiCpuMpData == NULL) {\r
244 return EFI_NOT_FOUND;\r
245 }\r
246\r
247 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
248 return EFI_INVALID_PARAMETER;\r
249 }\r
250\r
251 //\r
252 // Check whether caller processor is BSP\r
253 //\r
254 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
255 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
256 return EFI_DEVICE_ERROR;\r
257 }\r
258\r
259 ProcessorNumber = PeiCpuMpData->CpuCount;\r
260 EnabledProcessorNumber = 0;\r
261 for (Index = 0; Index < ProcessorNumber; Index++) {\r
262 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {\r
263 EnabledProcessorNumber ++;\r
264 }\r
265 }\r
266\r
267 *NumberOfProcessors = ProcessorNumber;\r
268 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
269\r
270 return EFI_SUCCESS;\r
271}\r
272\r
273/**\r
274 Gets detailed MP-related information on the requested processor at the\r
275 instant this call is made. This service may only be called from the BSP.\r
276\r
277 This service retrieves detailed MP-related information about any processor\r
278 on the platform. Note the following:\r
279 - The processor information may change during the course of a boot session.\r
280 - The information presented here is entirely MP related.\r
281\r
282 Information regarding the number of caches and their sizes, frequency of operation,\r
283 slot numbers is all considered platform-related information and is not provided\r
284 by this service.\r
285\r
286 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
287 published by the PEI Foundation.\r
288 @param[in] This Pointer to this instance of the PPI.\r
289 @param[in] ProcessorNumber Pointer to the total number of logical processors in\r
290 the system, including the BSP and disabled APs.\r
291 @param[out] ProcessorInfoBuffer Number of processors in the system that are enabled.\r
292\r
293 @retval EFI_SUCCESS Processor information was returned.\r
294 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
295 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
296 @retval EFI_NOT_FOUND The processor with the handle specified by\r
297 ProcessorNumber does not exist in the platform.\r
298**/\r
299EFI_STATUS\r
300EFIAPI\r
301PeiGetProcessorInfo (\r
302 IN CONST EFI_PEI_SERVICES **PeiServices,\r
303 IN EFI_PEI_MP_SERVICES_PPI *This,\r
304 IN UINTN ProcessorNumber,\r
305 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
306 )\r
307{\r
308 PEI_CPU_MP_DATA *PeiCpuMpData;\r
309 UINTN CallerNumber;\r
310\r
311 PeiCpuMpData = GetMpHobData ();\r
312 if (PeiCpuMpData == NULL) {\r
313 return EFI_NOT_FOUND;\r
314 }\r
315\r
316 //\r
317 // Check whether caller processor is BSP\r
318 //\r
319 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
320 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
321 return EFI_DEVICE_ERROR;\r
322 }\r
323\r
324 if (ProcessorInfoBuffer == NULL) {\r
325 return EFI_INVALID_PARAMETER;\r
326 }\r
327\r
328 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {\r
329 return EFI_NOT_FOUND;\r
330 }\r
331\r
332 ProcessorInfoBuffer->ProcessorId = (UINT64) PeiCpuMpData->CpuData[ProcessorNumber].ApicId;\r
333 ProcessorInfoBuffer->StatusFlag = 0;\r
334 if (PeiCpuMpData->CpuData[ProcessorNumber].ApicId == GetInitialApicId()) {\r
335 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
336 }\r
8b4c856c 337 if (PeiCpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
ea0f431c
JF
338 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
339 }\r
340 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {\r
341 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
342 } else {\r
343 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
344 }\r
345\r
346 //\r
347 // Get processor location information\r
348 //\r
349 ExtractProcessorLocation (PeiCpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);\r
350\r
351 return EFI_SUCCESS;\r
352}\r
353\r
354/**\r
355 This service executes a caller provided function on all enabled APs. APs can\r
356 run either simultaneously or one at a time in sequence. This service supports\r
357 both blocking requests only. This service may only\r
358 be called from the BSP.\r
359\r
360 This function is used to dispatch all the enabled APs to the function specified\r
361 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
362 immediately and Procedure is not started on any AP.\r
363\r
364 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
365 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
366 all the enabled APs execute the function specified by Procedure simultaneously.\r
367\r
368 If the timeout specified by TimeoutInMicroSeconds expires before all APs return\r
369 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
370 are always available for further calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
371 and EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If FailedCpuList is not NULL, its\r
372 content points to the list of processor handle numbers in which Procedure was\r
373 terminated.\r
374\r
375 Note: It is the responsibility of the consumer of the EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
376 to make sure that the nature of the code that is executed on the BSP and the\r
377 dispatched APs is well controlled. The MP Services Ppi does not guarantee\r
378 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
379 parallel are limited to certain independent tasks and well-controlled exclusive\r
380 code. PEI services and Ppis may not be called by APs unless otherwise\r
381 specified.\r
382\r
383 In blocking execution mode, BSP waits until all APs finish or\r
384 TimeoutInMicroSeconds expires.\r
385\r
386 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
387 published by the PEI Foundation.\r
388 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
389 @param[in] Procedure A pointer to the function to be run on enabled APs of\r
390 the system.\r
391 @param[in] SingleThread If TRUE, then all the enabled APs execute the function\r
392 specified by Procedure one by one, in ascending order\r
393 of processor handle number. If FALSE, then all the\r
394 enabled APs execute the function specified by Procedure\r
395 simultaneously.\r
396 @param[in] TimeoutInMicroSeconds\r
397 Indicates the time limit in microseconds for APs to\r
398 return from Procedure, for blocking mode only. Zero\r
399 means infinity. If the timeout expires before all APs\r
400 return from Procedure, then Procedure on the failed APs\r
401 is terminated. All enabled APs are available for next\r
402 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
403 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the\r
404 timeout expires in blocking mode, BSP returns\r
405 EFI_TIMEOUT.\r
406 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.\r
407\r
408 @retval EFI_SUCCESS In blocking mode, all APs have finished before the\r
409 timeout expired.\r
410 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
411 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
412 @retval EFI_NOT_READY Any enabled APs are busy.\r
413 @retval EFI_TIMEOUT In blocking mode, the timeout expired before all\r
414 enabled APs have finished.\r
415 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
416**/\r
417EFI_STATUS\r
418EFIAPI\r
419PeiStartupAllAPs (\r
420 IN CONST EFI_PEI_SERVICES **PeiServices,\r
421 IN EFI_PEI_MP_SERVICES_PPI *This,\r
422 IN EFI_AP_PROCEDURE Procedure,\r
423 IN BOOLEAN SingleThread,\r
424 IN UINTN TimeoutInMicroSeconds,\r
425 IN VOID *ProcedureArgument OPTIONAL\r
426 )\r
427{\r
428 PEI_CPU_MP_DATA *PeiCpuMpData;\r
429 UINTN ProcessorNumber;\r
430 UINTN Index;\r
431 UINTN CallerNumber;\r
432 BOOLEAN HasEnabledAp;\r
433 BOOLEAN HasEnabledIdleAp;\r
434 volatile UINT32 *FinishedCount;\r
435 EFI_STATUS Status;\r
436 UINTN WaitCountIndex;\r
437 UINTN WaitCountNumber;\r
438\r
439 PeiCpuMpData = GetMpHobData ();\r
440 if (PeiCpuMpData == NULL) {\r
441 return EFI_NOT_FOUND;\r
442 }\r
443\r
f1c97bb3
JF
444 if (Procedure == NULL) {\r
445 return EFI_INVALID_PARAMETER;\r
446 }\r
447\r
ea0f431c
JF
448 //\r
449 // Check whether caller processor is BSP\r
450 //\r
451 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
452 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
453 return EFI_DEVICE_ERROR;\r
454 }\r
455\r
456 ProcessorNumber = PeiCpuMpData->CpuCount;\r
457\r
458 HasEnabledAp = FALSE;\r
459 HasEnabledIdleAp = FALSE;\r
460 for (Index = 0; Index < ProcessorNumber; Index ++) {\r
461 if (Index == CallerNumber) {\r
462 //\r
463 // Skip BSP\r
464 //\r
465 continue;\r
466 }\r
467 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {\r
468 HasEnabledAp = TRUE;\r
469 if (PeiCpuMpData->CpuData[Index].State != CpuStateBusy) {\r
470 HasEnabledIdleAp = TRUE;\r
471 }\r
472 }\r
473 }\r
474 if (!HasEnabledAp) {\r
475 //\r
476 // If no enabled AP exists, return EFI_NOT_STARTED.\r
477 //\r
478 return EFI_NOT_STARTED;\r
479 }\r
480 if (!HasEnabledIdleAp) {\r
481 //\r
482 // If any enabled APs are busy, return EFI_NOT_READY.\r
483 //\r
484 return EFI_NOT_READY;\r
485 }\r
486\r
487 if (PeiCpuMpData->EndOfPeiFlag) {\r
488 //\r
489 // Backup original data and copy AP reset vector in it\r
490 //\r
491 BackupAndPrepareWakeupBuffer(PeiCpuMpData);\r
492 }\r
493\r
494 WaitCountNumber = TimeoutInMicroSeconds / CPU_CHECK_AP_INTERVAL + 1;\r
495 WaitCountIndex = 0;\r
496 FinishedCount = &PeiCpuMpData->FinishedCount;\r
497 if (!SingleThread) {\r
498 WakeUpAP (PeiCpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
499 //\r
500 // Wait to finish\r
501 //\r
502 if (TimeoutInMicroSeconds == 0) {\r
503 while (*FinishedCount < ProcessorNumber - 1) {\r
504 CpuPause ();\r
505 }\r
506 Status = EFI_SUCCESS;\r
507 } else {\r
508 Status = EFI_TIMEOUT;\r
509 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {\r
510 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);\r
511 if (*FinishedCount >= ProcessorNumber - 1) {\r
512 Status = EFI_SUCCESS;\r
513 break;\r
514 }\r
515 }\r
516 }\r
517 } else {\r
518 Status = EFI_SUCCESS;\r
519 for (Index = 0; Index < ProcessorNumber; Index++) {\r
520 if (Index == CallerNumber) {\r
521 continue;\r
522 }\r
a09647f3 523 WakeUpAP (PeiCpuMpData, FALSE, Index, Procedure, ProcedureArgument);\r
ea0f431c
JF
524 //\r
525 // Wait to finish\r
526 //\r
527 if (TimeoutInMicroSeconds == 0) {\r
528 while (*FinishedCount < 1) {\r
529 CpuPause ();\r
530 }\r
531 } else {\r
532 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {\r
533 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);\r
534 if (*FinishedCount >= 1) {\r
535 break;\r
536 }\r
537 }\r
538 if (WaitCountIndex == WaitCountNumber) {\r
539 Status = EFI_TIMEOUT;\r
540 }\r
541 }\r
542 }\r
543 }\r
544\r
545 if (PeiCpuMpData->EndOfPeiFlag) {\r
546 //\r
547 // Restore original data\r
548 //\r
549 RestoreWakeupBuffer(PeiCpuMpData);\r
550 }\r
551\r
552 return Status;\r
553}\r
554\r
555/**\r
556 This service lets the caller get one enabled AP to execute a caller-provided\r
557 function. The caller can request the BSP to wait for the completion\r
558 of the AP. This service may only be called from the BSP.\r
559\r
560 This function is used to dispatch one enabled AP to the function specified by\r
561 Procedure passing in the argument specified by ProcedureArgument.\r
562 The execution is in blocking mode. The BSP waits until the AP finishes or\r
563 TimeoutInMicroSecondss expires.\r
564\r
565 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
566 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
567 available for subsequent calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs() and\r
568 EFI_PEI_MP_SERVICES_PPI.StartupThisAP().\r
569\r
570 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
571 published by the PEI Foundation.\r
572 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
573 @param[in] Procedure A pointer to the function to be run on enabled APs of\r
574 the system.\r
575 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
576 total number of logical processors minus 1. The total\r
577 number of logical processors can be retrieved by\r
578 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
579 @param[in] TimeoutInMicroseconds\r
580 Indicates the time limit in microseconds for APs to\r
581 return from Procedure, for blocking mode only. Zero\r
582 means infinity. If the timeout expires before all APs\r
583 return from Procedure, then Procedure on the failed APs\r
584 is terminated. All enabled APs are available for next\r
585 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
586 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the\r
587 timeout expires in blocking mode, BSP returns\r
588 EFI_TIMEOUT.\r
589 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.\r
590\r
591 @retval EFI_SUCCESS In blocking mode, specified AP finished before the\r
592 timeout expires.\r
593 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
594 @retval EFI_TIMEOUT In blocking mode, the timeout expired before the\r
595 specified AP has finished.\r
596 @retval EFI_NOT_FOUND The processor with the handle specified by\r
597 ProcessorNumber does not exist.\r
598 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
599 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
600**/\r
601EFI_STATUS\r
602EFIAPI\r
603PeiStartupThisAP (\r
604 IN CONST EFI_PEI_SERVICES **PeiServices,\r
605 IN EFI_PEI_MP_SERVICES_PPI *This,\r
606 IN EFI_AP_PROCEDURE Procedure,\r
607 IN UINTN ProcessorNumber,\r
608 IN UINTN TimeoutInMicroseconds,\r
609 IN VOID *ProcedureArgument OPTIONAL\r
610 )\r
611{\r
612 PEI_CPU_MP_DATA *PeiCpuMpData;\r
613 UINTN CallerNumber;\r
614 volatile UINT32 *FinishedCount;\r
615 EFI_STATUS Status;\r
616 UINTN WaitCountIndex;\r
617 UINTN WaitCountNumber;\r
618\r
619 PeiCpuMpData = GetMpHobData ();\r
620 if (PeiCpuMpData == NULL) {\r
621 return EFI_NOT_FOUND;\r
622 }\r
623\r
624 //\r
625 // Check whether caller processor is BSP\r
626 //\r
627 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
628 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
629 return EFI_DEVICE_ERROR;\r
630 }\r
631\r
632 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {\r
633 return EFI_NOT_FOUND;\r
634 }\r
635\r
636 if (ProcessorNumber == PeiCpuMpData->BspNumber || Procedure == NULL) {\r
637 return EFI_INVALID_PARAMETER;\r
638 }\r
639\r
640 //\r
641 // Check whether specified AP is disabled\r
642 //\r
643 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {\r
644 return EFI_INVALID_PARAMETER;\r
645 }\r
646\r
647 if (PeiCpuMpData->EndOfPeiFlag) {\r
648 //\r
649 // Backup original data and copy AP reset vector in it\r
650 //\r
651 BackupAndPrepareWakeupBuffer(PeiCpuMpData);\r
652 }\r
653\r
654 WaitCountNumber = TimeoutInMicroseconds / CPU_CHECK_AP_INTERVAL + 1;\r
655 WaitCountIndex = 0;\r
656 FinishedCount = &PeiCpuMpData->FinishedCount;\r
657\r
a09647f3 658 WakeUpAP (PeiCpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
ea0f431c
JF
659\r
660 //\r
661 // Wait to finish\r
662 //\r
663 if (TimeoutInMicroseconds == 0) {\r
664 while (*FinishedCount < 1) {\r
665 CpuPause() ;\r
666 }\r
667 Status = EFI_SUCCESS;\r
668 } else {\r
669 Status = EFI_TIMEOUT;\r
670 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {\r
671 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);\r
672 if (*FinishedCount >= 1) {\r
673 Status = EFI_SUCCESS;\r
674 break;\r
675 }\r
676 }\r
677 }\r
678\r
679 if (PeiCpuMpData->EndOfPeiFlag) {\r
680 //\r
681 // Backup original data and copy AP reset vector in it\r
682 //\r
683 RestoreWakeupBuffer(PeiCpuMpData);\r
684 }\r
685\r
686 return Status;\r
687}\r
688\r
689/**\r
690 This service switches the requested AP to be the BSP from that point onward.\r
691 This service changes the BSP for all purposes. This call can only be performed\r
692 by the current BSP.\r
693\r
694 This service switches the requested AP to be the BSP from that point onward.\r
695 This service changes the BSP for all purposes. The new BSP can take over the\r
696 execution of the old BSP and continue seamlessly from where the old one left\r
697 off.\r
698\r
699 If the BSP cannot be switched prior to the return from this service, then\r
700 EFI_UNSUPPORTED must be returned.\r
701\r
702 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
703 published by the PEI Foundation.\r
704 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
705 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
706 total number of logical processors minus 1. The total\r
707 number of logical processors can be retrieved by\r
708 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
709 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an enabled\r
710 AP. Otherwise, it will be disabled.\r
711\r
712 @retval EFI_SUCCESS BSP successfully switched.\r
713 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to this\r
714 service returning.\r
715 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
716 @retval EFI_SUCCESS The calling processor is an AP.\r
717 @retval EFI_NOT_FOUND The processor with the handle specified by\r
718 ProcessorNumber does not exist.\r
719 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or a disabled\r
720 AP.\r
721 @retval EFI_NOT_READY The specified AP is busy.\r
722**/\r
723EFI_STATUS\r
724EFIAPI\r
725PeiSwitchBSP (\r
726 IN CONST EFI_PEI_SERVICES **PeiServices,\r
727 IN EFI_PEI_MP_SERVICES_PPI *This,\r
728 IN UINTN ProcessorNumber,\r
729 IN BOOLEAN EnableOldBSP\r
730 )\r
731{\r
732 PEI_CPU_MP_DATA *PeiCpuMpData;\r
733 UINTN CallerNumber;\r
734 MSR_IA32_APIC_BASE ApicBaseMsr;\r
735\r
736 PeiCpuMpData = GetMpHobData ();\r
737 if (PeiCpuMpData == NULL) {\r
738 return EFI_NOT_FOUND;\r
739 }\r
740\r
741 //\r
742 // Check whether caller processor is BSP\r
743 //\r
744 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
745 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
746 return EFI_SUCCESS;\r
747 }\r
748\r
749 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {\r
750 return EFI_NOT_FOUND;\r
751 }\r
752\r
753 //\r
754 // Check whether specified AP is disabled\r
755 //\r
756 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {\r
757 return EFI_INVALID_PARAMETER;\r
758 }\r
759\r
760 //\r
761 // Check whether ProcessorNumber specifies the current BSP\r
762 //\r
763 if (ProcessorNumber == PeiCpuMpData->BspNumber) {\r
764 return EFI_INVALID_PARAMETER;\r
765 }\r
766\r
767 //\r
768 // Check whether specified AP is busy\r
769 //\r
770 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateBusy) {\r
771 return EFI_NOT_READY;\r
772 }\r
773\r
774 //\r
775 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
776 //\r
777 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
778 ApicBaseMsr.Bits.Bsp = 0;\r
779 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS, ApicBaseMsr.Uint64);\r
780\r
781 PeiCpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
782 PeiCpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
783\r
784 if (PeiCpuMpData->EndOfPeiFlag) {\r
785 //\r
786 // Backup original data and copy AP reset vector in it\r
787 //\r
788 BackupAndPrepareWakeupBuffer(PeiCpuMpData);\r
789 }\r
790\r
791 //\r
792 // Need to wakeUp AP (future BSP).\r
793 //\r
a09647f3 794 WakeUpAP (PeiCpuMpData, FALSE, ProcessorNumber, FutureBSPProc, PeiCpuMpData);\r
ea0f431c
JF
795\r
796 AsmExchangeRole (&PeiCpuMpData->BSPInfo, &PeiCpuMpData->APInfo);\r
797\r
798 if (PeiCpuMpData->EndOfPeiFlag) {\r
799 //\r
800 // Backup original data and copy AP reset vector in it\r
801 //\r
802 RestoreWakeupBuffer(PeiCpuMpData);\r
803 }\r
804\r
805 //\r
806 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
807 //\r
808 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
809 ApicBaseMsr.Bits.Bsp = 1;\r
810 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS, ApicBaseMsr.Uint64);\r
44d2ec14
JF
811 //\r
812 // Set old BSP enable state\r
813 //\r
814 if (!EnableOldBSP) {\r
815 PeiCpuMpData->CpuData[PeiCpuMpData->BspNumber].State = CpuStateDisabled;\r
816 }\r
817 //\r
818 // Save new BSP number\r
819 //\r
820 PeiCpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
ea0f431c
JF
821\r
822 return EFI_SUCCESS;\r
823}\r
824\r
825/**\r
826 This service lets the caller enable or disable an AP from this point onward.\r
827 This service may only be called from the BSP.\r
828\r
829 This service allows the caller enable or disable an AP from this point onward.\r
830 The caller can optionally specify the health status of the AP by Health. If\r
831 an AP is being disabled, then the state of the disabled AP is implementation\r
832 dependent. If an AP is enabled, then the implementation must guarantee that a\r
833 complete initialization sequence is performed on the AP, so the AP is in a state\r
834 that is compatible with an MP operating system.\r
835\r
836 If the enable or disable AP operation cannot be completed prior to the return\r
837 from this service, then EFI_UNSUPPORTED must be returned.\r
838\r
839 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
840 published by the PEI Foundation.\r
841 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
842 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
843 total number of logical processors minus 1. The total\r
844 number of logical processors can be retrieved by\r
845 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
846 @param[in] EnableAP Specifies the new state for the processor for enabled,\r
847 FALSE for disabled.\r
848 @param[in] HealthFlag If not NULL, a pointer to a value that specifies the\r
849 new health status of the AP. This flag corresponds to\r
850 StatusFlag defined in EFI_PEI_MP_SERVICES_PPI.GetProcessorInfo().\r
851 Only the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
852 bits are ignored. If it is NULL, this parameter is\r
853 ignored.\r
854\r
855 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
856 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed prior\r
857 to this service returning.\r
858 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
859 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
860 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
861 does not exist.\r
862 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
863**/\r
864EFI_STATUS\r
865EFIAPI\r
866PeiEnableDisableAP (\r
867 IN CONST EFI_PEI_SERVICES **PeiServices,\r
868 IN EFI_PEI_MP_SERVICES_PPI *This,\r
869 IN UINTN ProcessorNumber,\r
870 IN BOOLEAN EnableAP,\r
871 IN UINT32 *HealthFlag OPTIONAL\r
872 )\r
873{\r
874 PEI_CPU_MP_DATA *PeiCpuMpData;\r
875 UINTN CallerNumber;\r
876\r
877 PeiCpuMpData = GetMpHobData ();\r
878 if (PeiCpuMpData == NULL) {\r
879 return EFI_NOT_FOUND;\r
880 }\r
881\r
882 //\r
883 // Check whether caller processor is BSP\r
884 //\r
885 PeiWhoAmI (PeiServices, This, &CallerNumber);\r
886 if (CallerNumber != PeiCpuMpData->BspNumber) {\r
887 return EFI_DEVICE_ERROR;\r
888 }\r
889\r
890 if (ProcessorNumber == PeiCpuMpData->BspNumber) {\r
891 return EFI_INVALID_PARAMETER;\r
892 }\r
893\r
894 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {\r
895 return EFI_NOT_FOUND;\r
896 }\r
897\r
898 if (!EnableAP) {\r
899 PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateDisabled;\r
900 } else {\r
901 PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle;\r
902 }\r
903\r
904 if (HealthFlag != NULL) {\r
905 PeiCpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
906 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
907 }\r
908 return EFI_SUCCESS;\r
909}\r
910\r
911/**\r
912 This return the handle number for the calling processor. This service may be\r
913 called from the BSP and APs.\r
914\r
915 This service returns the processor handle number for the calling processor.\r
916 The returned value is in the range from 0 to the total number of logical\r
917 processors minus 1. The total number of logical processors can be retrieved\r
918 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be\r
919 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
920 is returned. Otherwise, the current processors handle number is returned in\r
921 ProcessorNumber, and EFI_SUCCESS is returned.\r
922\r
923 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
924 published by the PEI Foundation.\r
925 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
926 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
927 total number of logical processors minus 1. The total\r
928 number of logical processors can be retrieved by\r
929 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
930\r
931 @retval EFI_SUCCESS The current processor handle number was returned in\r
932 ProcessorNumber.\r
933 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
934**/\r
935EFI_STATUS\r
936EFIAPI\r
937PeiWhoAmI (\r
938 IN CONST EFI_PEI_SERVICES **PeiServices,\r
939 IN EFI_PEI_MP_SERVICES_PPI *This,\r
940 OUT UINTN *ProcessorNumber\r
941 )\r
942{\r
943 PEI_CPU_MP_DATA *PeiCpuMpData;\r
944\r
945 PeiCpuMpData = GetMpHobData ();\r
946 if (PeiCpuMpData == NULL) {\r
947 return EFI_NOT_FOUND;\r
948 }\r
949\r
950 if (ProcessorNumber == NULL) {\r
951 return EFI_INVALID_PARAMETER;\r
952 }\r
953\r
954 return GetProcessorNumber (PeiCpuMpData, ProcessorNumber);\r
955}\r
956\r