a4bc26a42090ee86296c0922f785b3045a270529
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / PeiMpServices.c
1 /** @file
2 Implementation of Multiple Processor PPI services.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PeiMpServices.h"
16
17
18 /**
19 Get CPU Package/Core/Thread location information.
20
21 @param InitialApicId CPU APIC ID
22 @param Location Pointer to CPU location information
23 **/
24 VOID
25 ExtractProcessorLocation (
26 IN UINT32 InitialApicId,
27 OUT EFI_CPU_PHYSICAL_LOCATION *Location
28 )
29 {
30 BOOLEAN TopologyLeafSupported;
31 UINTN ThreadBits;
32 UINTN CoreBits;
33 UINT32 RegEax;
34 UINT32 RegEbx;
35 UINT32 RegEcx;
36 UINT32 RegEdx;
37 UINT32 MaxCpuIdIndex;
38 UINT32 SubIndex;
39 UINTN LevelType;
40 UINT32 MaxLogicProcessorsPerPackage;
41 UINT32 MaxCoresPerPackage;
42
43 //
44 // Check if the processor is capable of supporting more than one logical processor.
45 //
46 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &RegEdx);
47 if ((RegEdx & BIT28) == 0) {
48 Location->Thread = 0;
49 Location->Core = 0;
50 Location->Package = 0;
51 return;
52 }
53
54 ThreadBits = 0;
55 CoreBits = 0;
56
57 //
58 // Assume three-level mapping of APIC ID: Package:Core:SMT.
59 //
60
61 TopologyLeafSupported = FALSE;
62 //
63 // Get the max index of basic CPUID
64 //
65 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);
66
67 //
68 // If the extended topology enumeration leaf is available, it
69 // is the preferred mechanism for enumerating topology.
70 //
71 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {
72 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, &RegEax, &RegEbx, &RegEcx, NULL);
73 //
74 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for
75 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not
76 // supported on that processor.
77 //
78 if (RegEbx != 0) {
79 TopologyLeafSupported = TRUE;
80
81 //
82 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract
83 // the SMT sub-field of x2APIC ID.
84 //
85 LevelType = (RegEcx >> 8) & 0xff;
86 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);
87 ThreadBits = RegEax & 0x1f;
88
89 //
90 // Software must not assume any "level type" encoding
91 // value to be related to any sub-leaf index, except sub-leaf 0.
92 //
93 SubIndex = 1;
94 do {
95 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, SubIndex, &RegEax, NULL, &RegEcx, NULL);
96 LevelType = (RegEcx >> 8) & 0xff;
97 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {
98 CoreBits = (RegEax & 0x1f) - ThreadBits;
99 break;
100 }
101 SubIndex++;
102 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);
103 }
104 }
105
106 if (!TopologyLeafSupported) {
107 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);
108 MaxLogicProcessorsPerPackage = (RegEbx >> 16) & 0xff;
109 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {
110 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &RegEax, NULL, NULL, NULL);
111 MaxCoresPerPackage = (RegEax >> 26) + 1;
112 } else {
113 //
114 // Must be a single-core processor.
115 //
116 MaxCoresPerPackage = 1;
117 }
118
119 ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);
120 CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);
121 }
122
123 Location->Thread = InitialApicId & ~((-1) << ThreadBits);
124 Location->Core = (InitialApicId >> ThreadBits) & ~((-1) << CoreBits);
125 Location->Package = (InitialApicId >> (ThreadBits + CoreBits));
126 }
127
128 /**
129 Find the current Processor number by APIC ID.
130
131 @param PeiCpuMpData Pointer to PEI CPU MP Data
132 @param ProcessorNumber Return the pocessor number found
133
134 @retval EFI_SUCCESS ProcessorNumber is found and returned.
135 @retval EFI_NOT_FOUND ProcessorNumber is not found.
136 **/
137 EFI_STATUS
138 GetProcessorNumber (
139 IN PEI_CPU_MP_DATA *PeiCpuMpData,
140 OUT UINTN *ProcessorNumber
141 )
142 {
143 UINTN TotalProcessorNumber;
144 UINTN Index;
145
146 TotalProcessorNumber = PeiCpuMpData->CpuCount;
147 for (Index = 0; Index < TotalProcessorNumber; Index ++) {
148 if (PeiCpuMpData->CpuData[Index].ApicId == GetInitialApicId ()) {
149 *ProcessorNumber = Index;
150 return EFI_SUCCESS;
151 }
152 }
153 return EFI_NOT_FOUND;
154 }
155
156 /**
157 This service retrieves the number of logical processor in the platform
158 and the number of those logical processors that are enabled on this boot.
159 This service may only be called from the BSP.
160
161 This function is used to retrieve the following information:
162 - The number of logical processors that are present in the system.
163 - The number of enabled logical processors in the system at the instant
164 this call is made.
165
166 Because MP Service Ppi provides services to enable and disable processors
167 dynamically, the number of enabled logical processors may vary during the
168 course of a boot session.
169
170 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
171 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
172 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
173 is returned in NumberOfProcessors, the number of currently enabled processor
174 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
175
176 @param[in] PeiServices An indirect pointer to the PEI Services Table
177 published by the PEI Foundation.
178 @param[in] This Pointer to this instance of the PPI.
179 @param[out] NumberOfProcessors Pointer to the total number of logical processors in
180 the system, including the BSP and disabled APs.
181 @param[out] NumberOfEnabledProcessors
182 Number of processors in the system that are enabled.
183
184 @retval EFI_SUCCESS The number of logical processors and enabled
185 logical processors was retrieved.
186 @retval EFI_DEVICE_ERROR The calling processor is an AP.
187 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
188 NumberOfEnabledProcessors is NULL.
189 **/
190 EFI_STATUS
191 EFIAPI
192 PeiGetNumberOfProcessors (
193 IN CONST EFI_PEI_SERVICES **PeiServices,
194 IN EFI_PEI_MP_SERVICES_PPI *This,
195 OUT UINTN *NumberOfProcessors,
196 OUT UINTN *NumberOfEnabledProcessors
197 )
198 {
199 PEI_CPU_MP_DATA *PeiCpuMpData;
200 UINTN CallerNumber;
201 UINTN ProcessorNumber;
202 UINTN EnabledProcessorNumber;
203 UINTN Index;
204
205 PeiCpuMpData = GetMpHobData ();
206 if (PeiCpuMpData == NULL) {
207 return EFI_NOT_FOUND;
208 }
209
210 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
211 return EFI_INVALID_PARAMETER;
212 }
213
214 //
215 // Check whether caller processor is BSP
216 //
217 PeiWhoAmI (PeiServices, This, &CallerNumber);
218 if (CallerNumber != PeiCpuMpData->BspNumber) {
219 return EFI_DEVICE_ERROR;
220 }
221
222 ProcessorNumber = PeiCpuMpData->CpuCount;
223 EnabledProcessorNumber = 0;
224 for (Index = 0; Index < ProcessorNumber; Index++) {
225 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {
226 EnabledProcessorNumber ++;
227 }
228 }
229
230 *NumberOfProcessors = ProcessorNumber;
231 *NumberOfEnabledProcessors = EnabledProcessorNumber;
232
233 return EFI_SUCCESS;
234 }
235
236 /**
237 Gets detailed MP-related information on the requested processor at the
238 instant this call is made. This service may only be called from the BSP.
239
240 This service retrieves detailed MP-related information about any processor
241 on the platform. Note the following:
242 - The processor information may change during the course of a boot session.
243 - The information presented here is entirely MP related.
244
245 Information regarding the number of caches and their sizes, frequency of operation,
246 slot numbers is all considered platform-related information and is not provided
247 by this service.
248
249 @param[in] PeiServices An indirect pointer to the PEI Services Table
250 published by the PEI Foundation.
251 @param[in] This Pointer to this instance of the PPI.
252 @param[in] ProcessorNumber Pointer to the total number of logical processors in
253 the system, including the BSP and disabled APs.
254 @param[out] ProcessorInfoBuffer Number of processors in the system that are enabled.
255
256 @retval EFI_SUCCESS Processor information was returned.
257 @retval EFI_DEVICE_ERROR The calling processor is an AP.
258 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
259 @retval EFI_NOT_FOUND The processor with the handle specified by
260 ProcessorNumber does not exist in the platform.
261 **/
262 EFI_STATUS
263 EFIAPI
264 PeiGetProcessorInfo (
265 IN CONST EFI_PEI_SERVICES **PeiServices,
266 IN EFI_PEI_MP_SERVICES_PPI *This,
267 IN UINTN ProcessorNumber,
268 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
269 )
270 {
271 PEI_CPU_MP_DATA *PeiCpuMpData;
272 UINTN CallerNumber;
273
274 PeiCpuMpData = GetMpHobData ();
275 if (PeiCpuMpData == NULL) {
276 return EFI_NOT_FOUND;
277 }
278
279 //
280 // Check whether caller processor is BSP
281 //
282 PeiWhoAmI (PeiServices, This, &CallerNumber);
283 if (CallerNumber != PeiCpuMpData->BspNumber) {
284 return EFI_DEVICE_ERROR;
285 }
286
287 if (ProcessorInfoBuffer == NULL) {
288 return EFI_INVALID_PARAMETER;
289 }
290
291 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {
292 return EFI_NOT_FOUND;
293 }
294
295 ProcessorInfoBuffer->ProcessorId = (UINT64) PeiCpuMpData->CpuData[ProcessorNumber].ApicId;
296 ProcessorInfoBuffer->StatusFlag = 0;
297 if (PeiCpuMpData->CpuData[ProcessorNumber].ApicId == GetInitialApicId()) {
298 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
299 }
300 if (PeiCpuMpData->CpuData[ProcessorNumber].Health.Uint32 == 0) {
301 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
302 }
303 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {
304 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
305 } else {
306 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
307 }
308
309 //
310 // Get processor location information
311 //
312 ExtractProcessorLocation (PeiCpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);
313
314 return EFI_SUCCESS;
315 }
316
317 /**
318 This service executes a caller provided function on all enabled APs. APs can
319 run either simultaneously or one at a time in sequence. This service supports
320 both blocking requests only. This service may only
321 be called from the BSP.
322
323 This function is used to dispatch all the enabled APs to the function specified
324 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
325 immediately and Procedure is not started on any AP.
326
327 If SingleThread is TRUE, all the enabled APs execute the function specified by
328 Procedure one by one, in ascending order of processor handle number. Otherwise,
329 all the enabled APs execute the function specified by Procedure simultaneously.
330
331 If the timeout specified by TimeoutInMicroSeconds expires before all APs return
332 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
333 are always available for further calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
334 and EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If FailedCpuList is not NULL, its
335 content points to the list of processor handle numbers in which Procedure was
336 terminated.
337
338 Note: It is the responsibility of the consumer of the EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
339 to make sure that the nature of the code that is executed on the BSP and the
340 dispatched APs is well controlled. The MP Services Ppi does not guarantee
341 that the Procedure function is MP-safe. Hence, the tasks that can be run in
342 parallel are limited to certain independent tasks and well-controlled exclusive
343 code. PEI services and Ppis may not be called by APs unless otherwise
344 specified.
345
346 In blocking execution mode, BSP waits until all APs finish or
347 TimeoutInMicroSeconds expires.
348
349 @param[in] PeiServices An indirect pointer to the PEI Services Table
350 published by the PEI Foundation.
351 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
352 @param[in] Procedure A pointer to the function to be run on enabled APs of
353 the system.
354 @param[in] SingleThread If TRUE, then all the enabled APs execute the function
355 specified by Procedure one by one, in ascending order
356 of processor handle number. If FALSE, then all the
357 enabled APs execute the function specified by Procedure
358 simultaneously.
359 @param[in] TimeoutInMicroSeconds
360 Indicates the time limit in microseconds for APs to
361 return from Procedure, for blocking mode only. Zero
362 means infinity. If the timeout expires before all APs
363 return from Procedure, then Procedure on the failed APs
364 is terminated. All enabled APs are available for next
365 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
366 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the
367 timeout expires in blocking mode, BSP returns
368 EFI_TIMEOUT.
369 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
370
371 @retval EFI_SUCCESS In blocking mode, all APs have finished before the
372 timeout expired.
373 @retval EFI_DEVICE_ERROR Caller processor is AP.
374 @retval EFI_NOT_STARTED No enabled APs exist in the system.
375 @retval EFI_NOT_READY Any enabled APs are busy.
376 @retval EFI_TIMEOUT In blocking mode, the timeout expired before all
377 enabled APs have finished.
378 @retval EFI_INVALID_PARAMETER Procedure is NULL.
379 **/
380 EFI_STATUS
381 EFIAPI
382 PeiStartupAllAPs (
383 IN CONST EFI_PEI_SERVICES **PeiServices,
384 IN EFI_PEI_MP_SERVICES_PPI *This,
385 IN EFI_AP_PROCEDURE Procedure,
386 IN BOOLEAN SingleThread,
387 IN UINTN TimeoutInMicroSeconds,
388 IN VOID *ProcedureArgument OPTIONAL
389 )
390 {
391 PEI_CPU_MP_DATA *PeiCpuMpData;
392 UINTN ProcessorNumber;
393 UINTN Index;
394 UINTN CallerNumber;
395 BOOLEAN HasEnabledAp;
396 BOOLEAN HasEnabledIdleAp;
397 volatile UINT32 *FinishedCount;
398 EFI_STATUS Status;
399 UINTN WaitCountIndex;
400 UINTN WaitCountNumber;
401
402 PeiCpuMpData = GetMpHobData ();
403 if (PeiCpuMpData == NULL) {
404 return EFI_NOT_FOUND;
405 }
406
407 //
408 // Check whether caller processor is BSP
409 //
410 PeiWhoAmI (PeiServices, This, &CallerNumber);
411 if (CallerNumber != PeiCpuMpData->BspNumber) {
412 return EFI_DEVICE_ERROR;
413 }
414
415 ProcessorNumber = PeiCpuMpData->CpuCount;
416
417 HasEnabledAp = FALSE;
418 HasEnabledIdleAp = FALSE;
419 for (Index = 0; Index < ProcessorNumber; Index ++) {
420 if (Index == CallerNumber) {
421 //
422 // Skip BSP
423 //
424 continue;
425 }
426 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {
427 HasEnabledAp = TRUE;
428 if (PeiCpuMpData->CpuData[Index].State != CpuStateBusy) {
429 HasEnabledIdleAp = TRUE;
430 }
431 }
432 }
433 if (!HasEnabledAp) {
434 //
435 // If no enabled AP exists, return EFI_NOT_STARTED.
436 //
437 return EFI_NOT_STARTED;
438 }
439 if (!HasEnabledIdleAp) {
440 //
441 // If any enabled APs are busy, return EFI_NOT_READY.
442 //
443 return EFI_NOT_READY;
444 }
445
446 WaitCountNumber = TimeoutInMicroSeconds / CPU_CHECK_AP_INTERVAL + 1;
447 WaitCountIndex = 0;
448 FinishedCount = &PeiCpuMpData->FinishedCount;
449 if (!SingleThread) {
450 WakeUpAP (PeiCpuMpData, TRUE, 0, Procedure, ProcedureArgument);
451 //
452 // Wait to finish
453 //
454 if (TimeoutInMicroSeconds == 0) {
455 while (*FinishedCount < ProcessorNumber - 1) {
456 CpuPause ();
457 }
458 Status = EFI_SUCCESS;
459 } else {
460 Status = EFI_TIMEOUT;
461 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {
462 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);
463 if (*FinishedCount >= ProcessorNumber - 1) {
464 Status = EFI_SUCCESS;
465 break;
466 }
467 }
468 }
469 } else {
470 Status = EFI_SUCCESS;
471 for (Index = 0; Index < ProcessorNumber; Index++) {
472 if (Index == CallerNumber) {
473 continue;
474 }
475 WakeUpAP (PeiCpuMpData, FALSE, PeiCpuMpData->CpuData[Index].ApicId, Procedure, ProcedureArgument);
476 //
477 // Wait to finish
478 //
479 if (TimeoutInMicroSeconds == 0) {
480 while (*FinishedCount < 1) {
481 CpuPause ();
482 }
483 } else {
484 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {
485 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);
486 if (*FinishedCount >= 1) {
487 break;
488 }
489 }
490 if (WaitCountIndex == WaitCountNumber) {
491 Status = EFI_TIMEOUT;
492 }
493 }
494 }
495 }
496
497 return Status;
498 }
499
500 /**
501 This return the handle number for the calling processor. This service may be
502 called from the BSP and APs.
503
504 This service returns the processor handle number for the calling processor.
505 The returned value is in the range from 0 to the total number of logical
506 processors minus 1. The total number of logical processors can be retrieved
507 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be
508 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
509 is returned. Otherwise, the current processors handle number is returned in
510 ProcessorNumber, and EFI_SUCCESS is returned.
511
512 @param[in] PeiServices An indirect pointer to the PEI Services Table
513 published by the PEI Foundation.
514 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
515 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the
516 total number of logical processors minus 1. The total
517 number of logical processors can be retrieved by
518 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
519
520 @retval EFI_SUCCESS The current processor handle number was returned in
521 ProcessorNumber.
522 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
523 **/
524 EFI_STATUS
525 EFIAPI
526 PeiWhoAmI (
527 IN CONST EFI_PEI_SERVICES **PeiServices,
528 IN EFI_PEI_MP_SERVICES_PPI *This,
529 OUT UINTN *ProcessorNumber
530 )
531 {
532 PEI_CPU_MP_DATA *PeiCpuMpData;
533
534 PeiCpuMpData = GetMpHobData ();
535 if (PeiCpuMpData == NULL) {
536 return EFI_NOT_FOUND;
537 }
538
539 if (ProcessorNumber == NULL) {
540 return EFI_INVALID_PARAMETER;
541 }
542
543 return GetProcessorNumber (PeiCpuMpData, ProcessorNumber);
544 }
545