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