]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
UefiCpuPkg/MpInitLib: split wake up buffer into two parts
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / PeiMpLib.c
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 MP initialize support functions for PEI phase.\r
3\r
9293d6e4 4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
3e8ad6bd
JF
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 "MpLib.h"\r
43c9fdcc
JF
16\r
17/**\r
18 Enable Debug Agent to support source debugging on AP function.\r
19\r
20**/\r
21VOID\r
22EnableDebugAgent (\r
23 VOID\r
24 )\r
25{\r
26}\r
27\r
93ca4c0f
JF
28/**\r
29 Get pointer to CPU MP Data structure.\r
30\r
31 @return The pointer to CPU MP Data structure.\r
32**/\r
33CPU_MP_DATA *\r
34GetCpuMpData (\r
35 VOID\r
36 )\r
37{\r
38 CPU_MP_DATA *CpuMpData;\r
39\r
40 CpuMpData = GetCpuMpDataFromGuidedHob ();\r
41 ASSERT (CpuMpData != NULL);\r
42 return CpuMpData;\r
43}\r
44\r
45/**\r
46 Save the pointer to CPU MP Data structure.\r
47\r
48 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
49**/\r
50VOID\r
51SaveCpuMpData (\r
52 IN CPU_MP_DATA *CpuMpData\r
53 )\r
54{\r
55 UINT64 Data64;\r
56 //\r
57 // Build location of CPU MP DATA buffer in HOB\r
58 //\r
59 Data64 = (UINT64) (UINTN) CpuMpData;\r
60 BuildGuidDataHob (\r
61 &mCpuInitMpLibHobGuid,\r
62 (VOID *) &Data64,\r
63 sizeof (UINT64)\r
64 );\r
65}\r
66\r
ed66e0e3
JF
67/**\r
68 Check if AP wakeup buffer is overlapped with existing allocated buffer.\r
69\r
70 @param[in] WakeupBufferStart AP wakeup buffer start address.\r
71 @param[in] WakeupBufferEnd AP wakeup buffer end address.\r
72\r
73 @retval TRUE There is overlap.\r
74 @retval FALSE There is no overlap.\r
75**/\r
76BOOLEAN\r
77CheckOverlapWithAllocatedBuffer (\r
78 IN UINTN WakeupBufferStart,\r
79 IN UINTN WakeupBufferEnd\r
80 )\r
81{\r
82 EFI_PEI_HOB_POINTERS Hob;\r
83 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
84 BOOLEAN Overlapped;\r
85 UINTN MemoryStart;\r
86 UINTN MemoryEnd;\r
87\r
88 Overlapped = FALSE;\r
89 //\r
90 // Get the HOB list for processing\r
91 //\r
92 Hob.Raw = GetHobList ();\r
93 //\r
94 // Collect memory ranges\r
95 //\r
96 while (!END_OF_HOB_LIST (Hob)) {\r
97 if (Hob.Header->HobType == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
98 MemoryHob = Hob.MemoryAllocation;\r
99 MemoryStart = (UINTN) MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
100 MemoryEnd = (UINTN) (MemoryHob->AllocDescriptor.MemoryBaseAddress +\r
101 MemoryHob->AllocDescriptor.MemoryLength);\r
102 if (!((WakeupBufferStart >= MemoryEnd) || (WakeupBufferEnd <= MemoryStart))) {\r
103 Overlapped = TRUE;\r
104 break;\r
105 }\r
106 }\r
107 Hob.Raw = GET_NEXT_HOB (Hob);\r
108 }\r
109 return Overlapped;\r
110}\r
111\r
112/**\r
113 Get available system memory below 1MB by specified size.\r
114\r
115 @param[in] WakeupBufferSize Wakeup buffer size required\r
116\r
117 @retval other Return wakeup buffer address below 1MB.\r
118 @retval -1 Cannot find free memory below 1MB.\r
119**/\r
120UINTN\r
121GetWakeupBuffer (\r
122 IN UINTN WakeupBufferSize\r
123 )\r
124{\r
125 EFI_PEI_HOB_POINTERS Hob;\r
126 UINTN WakeupBufferStart;\r
127 UINTN WakeupBufferEnd;\r
128\r
129 WakeupBufferSize = (WakeupBufferSize + SIZE_4KB - 1) & ~(SIZE_4KB - 1);\r
130\r
131 //\r
132 // Get the HOB list for processing\r
133 //\r
134 Hob.Raw = GetHobList ();\r
135\r
136 //\r
137 // Collect memory ranges\r
138 //\r
139 while (!END_OF_HOB_LIST (Hob)) {\r
140 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
141 if ((Hob.ResourceDescriptor->PhysicalStart < BASE_1MB) &&\r
142 (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
143 ((Hob.ResourceDescriptor->ResourceAttribute &\r
144 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED |\r
145 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED |\r
146 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED\r
147 )) == 0)\r
148 ) {\r
149 //\r
150 // Need memory under 1MB to be collected here\r
151 //\r
152 WakeupBufferEnd = (UINTN) (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength);\r
153 if (WakeupBufferEnd > BASE_1MB) {\r
154 //\r
155 // Wakeup buffer should be under 1MB\r
156 //\r
157 WakeupBufferEnd = BASE_1MB;\r
158 }\r
159 while (WakeupBufferEnd > WakeupBufferSize) {\r
160 //\r
161 // Wakeup buffer should be aligned on 4KB\r
162 //\r
163 WakeupBufferStart = (WakeupBufferEnd - WakeupBufferSize) & ~(SIZE_4KB - 1);\r
164 if (WakeupBufferStart < Hob.ResourceDescriptor->PhysicalStart) {\r
165 break;\r
166 }\r
167 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart, WakeupBufferEnd)) {\r
168 //\r
169 // If this range is overlapped with existing allocated buffer, skip it\r
170 // and find the next range\r
171 //\r
172 WakeupBufferEnd -= WakeupBufferSize;\r
173 continue;\r
174 }\r
175 DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
176 WakeupBufferStart, WakeupBufferSize));\r
ed66e0e3
JF
177 return WakeupBufferStart;\r
178 }\r
179 }\r
180 }\r
181 //\r
182 // Find the next HOB\r
183 //\r
184 Hob.Raw = GET_NEXT_HOB (Hob);\r
185 }\r
186\r
187 return (UINTN) -1;\r
188}\r
189\r
f32bfe6d
JW
190/**\r
191 Get available EfiBootServicesCode memory below 4GB by specified size.\r
192\r
193 This buffer is required to safely transfer AP from real address mode to\r
194 protected mode or long mode, due to the fact that the buffer returned by\r
195 GetWakeupBuffer() may be marked as non-executable.\r
196\r
197 @param[in] BufferSize Wakeup transition buffer size.\r
198\r
199 @retval other Return wakeup transition buffer address below 4GB.\r
200 @retval 0 Cannot find free memory below 4GB.\r
201**/\r
202UINTN\r
203GetModeTransitionBuffer (\r
204 IN UINTN BufferSize\r
205 )\r
206{\r
207 //\r
208 // PEI phase doesn't need to do such transition. So simply return 0.\r
209 //\r
210 return 0;\r
211}\r
212\r
08085f08
JF
213/**\r
214 Checks APs status and updates APs status if needed.\r
215\r
216**/\r
217VOID\r
218CheckAndUpdateApsStatus (\r
219 VOID\r
220 )\r
221{\r
222}\r
223\r
93ca4c0f
JF
224/**\r
225 Initialize global data for MP support.\r
226\r
227 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
228**/\r
229VOID\r
230InitMpGlobalData (\r
231 IN CPU_MP_DATA *CpuMpData\r
232 )\r
233{\r
93ca4c0f
JF
234 SaveCpuMpData (CpuMpData);\r
235}\r
236\r
3e8ad6bd
JF
237/**\r
238 This service executes a caller provided function on all enabled APs.\r
239\r
240 @param[in] Procedure A pointer to the function to be run on\r
241 enabled APs of the system. See type\r
242 EFI_AP_PROCEDURE.\r
243 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
244 the function specified by Procedure one by\r
245 one, in ascending order of processor handle\r
246 number. If FALSE, then all the enabled APs\r
247 execute the function specified by Procedure\r
248 simultaneously.\r
249 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
250 service. If it is NULL, then execute in\r
251 blocking mode. BSP waits until all APs finish\r
252 or TimeoutInMicroSeconds expires. If it's\r
253 not NULL, then execute in non-blocking mode.\r
254 BSP requests the function specified by\r
255 Procedure to be started on all the enabled\r
256 APs, and go on executing immediately. If\r
257 all return from Procedure, or TimeoutInMicroSeconds\r
258 expires, this event is signaled. The BSP\r
259 can use the CheckEvent() or WaitForEvent()\r
260 services to check the state of event. Type\r
261 EFI_EVENT is defined in CreateEvent() in\r
262 the Unified Extensible Firmware Interface\r
263 Specification.\r
367284e7 264 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
265 APs to return from Procedure, either for\r
266 blocking or non-blocking mode. Zero means\r
267 infinity. If the timeout expires before\r
268 all APs return from Procedure, then Procedure\r
269 on the failed APs is terminated. All enabled\r
270 APs are available for next function assigned\r
271 by MpInitLibStartupAllAPs() or\r
272 MPInitLibStartupThisAP().\r
273 If the timeout expires in blocking mode,\r
274 BSP returns EFI_TIMEOUT. If the timeout\r
275 expires in non-blocking mode, WaitEvent\r
276 is signaled with SignalEvent().\r
277 @param[in] ProcedureArgument The parameter passed into Procedure for\r
278 all APs.\r
279 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
280 if all APs finish successfully, then its\r
281 content is set to NULL. If not all APs\r
282 finish before timeout expires, then its\r
283 content is set to address of the buffer\r
284 holding handle numbers of the failed APs.\r
285 The buffer is allocated by MP Initialization\r
286 library, and it's the caller's responsibility to\r
287 free the buffer with FreePool() service.\r
288 In blocking mode, it is ready for consumption\r
289 when the call returns. In non-blocking mode,\r
290 it is ready when WaitEvent is signaled. The\r
291 list of failed CPU is terminated by\r
292 END_OF_CPU_LIST.\r
293\r
294 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
295 the timeout expired.\r
296 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
297 to all enabled APs.\r
298 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
299 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
300 signaled.\r
301 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
302 supported.\r
303 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
304 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
305 @retval EFI_NOT_READY Any enabled APs are busy.\r
306 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
307 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
308 all enabled APs have finished.\r
309 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
310\r
311**/\r
312EFI_STATUS\r
313EFIAPI\r
314MpInitLibStartupAllAPs (\r
315 IN EFI_AP_PROCEDURE Procedure,\r
316 IN BOOLEAN SingleThread,\r
317 IN EFI_EVENT WaitEvent OPTIONAL,\r
318 IN UINTN TimeoutInMicroseconds,\r
319 IN VOID *ProcedureArgument OPTIONAL,\r
320 OUT UINTN **FailedCpuList OPTIONAL\r
321 )\r
322{\r
86efe976
JF
323 if (WaitEvent != NULL) {\r
324 return EFI_UNSUPPORTED;\r
325 }\r
326\r
327 return StartupAllAPsWorker (\r
328 Procedure,\r
329 SingleThread,\r
330 NULL,\r
331 TimeoutInMicroseconds,\r
332 ProcedureArgument,\r
333 FailedCpuList\r
334 );\r
3e8ad6bd
JF
335}\r
336\r
337/**\r
338 This service lets the caller get one enabled AP to execute a caller-provided\r
339 function.\r
340\r
341 @param[in] Procedure A pointer to the function to be run on the\r
342 designated AP of the system. See type\r
343 EFI_AP_PROCEDURE.\r
344 @param[in] ProcessorNumber The handle number of the AP. The range is\r
345 from 0 to the total number of logical\r
346 processors minus 1. The total number of\r
347 logical processors can be retrieved by\r
348 MpInitLibGetNumberOfProcessors().\r
349 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
350 service. If it is NULL, then execute in\r
351 blocking mode. BSP waits until this AP finish\r
352 or TimeoutInMicroSeconds expires. If it's\r
353 not NULL, then execute in non-blocking mode.\r
354 BSP requests the function specified by\r
355 Procedure to be started on this AP,\r
356 and go on executing immediately. If this AP\r
357 return from Procedure or TimeoutInMicroSeconds\r
358 expires, this event is signaled. The BSP\r
359 can use the CheckEvent() or WaitForEvent()\r
360 services to check the state of event. Type\r
361 EFI_EVENT is defined in CreateEvent() in\r
362 the Unified Extensible Firmware Interface\r
363 Specification.\r
367284e7 364 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
365 this AP to finish this Procedure, either for\r
366 blocking or non-blocking mode. Zero means\r
367 infinity. If the timeout expires before\r
368 this AP returns from Procedure, then Procedure\r
369 on the AP is terminated. The\r
370 AP is available for next function assigned\r
371 by MpInitLibStartupAllAPs() or\r
372 MpInitLibStartupThisAP().\r
373 If the timeout expires in blocking mode,\r
374 BSP returns EFI_TIMEOUT. If the timeout\r
375 expires in non-blocking mode, WaitEvent\r
376 is signaled with SignalEvent().\r
377 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
378 specified AP.\r
379 @param[out] Finished If NULL, this parameter is ignored. In\r
380 blocking mode, this parameter is ignored.\r
381 In non-blocking mode, if AP returns from\r
382 Procedure before the timeout expires, its\r
383 content is set to TRUE. Otherwise, the\r
384 value is set to FALSE. The caller can\r
385 determine if the AP returned from Procedure\r
386 by evaluating this value.\r
387\r
388 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
389 the timeout expires.\r
390 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
391 dispatched to specified AP.\r
392 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
393 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
394 signaled.\r
395 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
396 supported.\r
397 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
398 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
399 the specified AP has finished.\r
400 @retval EFI_NOT_READY The specified AP is busy.\r
401 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
402 @retval EFI_NOT_FOUND The processor with the handle specified by\r
403 ProcessorNumber does not exist.\r
404 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
405 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
406\r
407**/\r
408EFI_STATUS\r
409EFIAPI\r
410MpInitLibStartupThisAP (\r
411 IN EFI_AP_PROCEDURE Procedure,\r
412 IN UINTN ProcessorNumber,\r
413 IN EFI_EVENT WaitEvent OPTIONAL,\r
414 IN UINTN TimeoutInMicroseconds,\r
415 IN VOID *ProcedureArgument OPTIONAL,\r
416 OUT BOOLEAN *Finished OPTIONAL\r
417 )\r
418{\r
20ae5774
JF
419 if (WaitEvent != NULL) {\r
420 return EFI_UNSUPPORTED;\r
421 }\r
422\r
423 return StartupThisAPWorker (\r
424 Procedure,\r
425 ProcessorNumber,\r
426 NULL,\r
427 TimeoutInMicroseconds,\r
428 ProcedureArgument,\r
429 Finished\r
430 );\r
3e8ad6bd
JF
431}\r
432\r
433/**\r
434 This service switches the requested AP to be the BSP from that point onward.\r
435 This service changes the BSP for all purposes. This call can only be performed\r
436 by the current BSP.\r
437\r
438 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
439 BSP. The range is from 0 to the total number of\r
440 logical processors minus 1. The total number of\r
441 logical processors can be retrieved by\r
442 MpInitLibGetNumberOfProcessors().\r
443 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
444 enabled AP. Otherwise, it will be disabled.\r
445\r
446 @retval EFI_SUCCESS BSP successfully switched.\r
447 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
448 this service returning.\r
449 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
450 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
451 @retval EFI_NOT_FOUND The processor with the handle specified by\r
452 ProcessorNumber does not exist.\r
453 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
454 a disabled AP.\r
455 @retval EFI_NOT_READY The specified AP is busy.\r
456 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
457\r
458**/\r
459EFI_STATUS\r
460EFIAPI\r
461MpInitLibSwitchBSP (\r
462 IN UINTN ProcessorNumber,\r
463 IN BOOLEAN EnableOldBSP\r
464 )\r
465{\r
41be0da5 466 return SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
3e8ad6bd
JF
467}\r
468\r
469/**\r
470 This service lets the caller enable or disable an AP from this point onward.\r
471 This service may only be called from the BSP.\r
472\r
473 @param[in] ProcessorNumber The handle number of AP.\r
474 The range is from 0 to the total number of\r
475 logical processors minus 1. The total number of\r
476 logical processors can be retrieved by\r
477 MpInitLibGetNumberOfProcessors().\r
478 @param[in] EnableAP Specifies the new state for the processor for\r
479 enabled, FALSE for disabled.\r
480 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
481 the new health status of the AP. This flag\r
482 corresponds to StatusFlag defined in\r
483 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
484 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
485 bits are ignored. If it is NULL, this parameter\r
486 is ignored.\r
487\r
488 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
489 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
490 prior to this service returning.\r
491 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
492 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
493 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
494 does not exist.\r
495 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
496 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
497\r
498**/\r
499EFI_STATUS\r
500EFIAPI\r
501MpInitLibEnableDisableAP (\r
502 IN UINTN ProcessorNumber,\r
503 IN BOOLEAN EnableAP,\r
504 IN UINT32 *HealthFlag OPTIONAL\r
505 )\r
506{\r
e37109bc 507 return EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
3e8ad6bd
JF
508}\r
509\r
510\r