blob: f05db7c2a5dc7420b5e63f4a4db4b9e370fb8b86 [file] [log] [blame]
Jeff Fan3e8ad6b2016-07-20 21:56:58 +08001/** @file
2 CPU MP Initialize Library common functions.
3
4 Copyright (c) 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 "MpLib.h"
16
17
18/**
Jeff Fan9ebcf0f2016-07-20 23:32:17 +080019 Detect whether Mwait-monitor feature is supported.
20
21 @retval TRUE Mwait-monitor feature is supported.
22 @retval FALSE Mwait-monitor feature is not supported.
23**/
24BOOLEAN
25IsMwaitSupport (
26 VOID
27 )
28{
29 CPUID_VERSION_INFO_ECX VersionInfoEcx;
30
31 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);
32 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;
33}
34
35/**
36 Get AP loop mode.
37
38 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
39
40 @return The AP loop mode.
41**/
42UINT8
43GetApLoopMode (
44 OUT UINT32 *MonitorFilterSize
45 )
46{
47 UINT8 ApLoopMode;
48 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;
49
50 ASSERT (MonitorFilterSize != NULL);
51
52 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
53 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);
54 if (ApLoopMode == ApInMwaitLoop) {
55 if (!IsMwaitSupport ()) {
56 //
57 // If processor does not support MONITOR/MWAIT feature,
58 // force AP in Hlt-loop mode
59 //
60 ApLoopMode = ApInHltLoop;
61 }
62 }
63
64 if (ApLoopMode != ApInMwaitLoop) {
65 *MonitorFilterSize = sizeof (UINT32);
66 } else {
67 //
68 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
69 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
70 //
71 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);
72 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;
73 }
74
75 return ApLoopMode;
76}
77/**
Jeff Fan3e8ad6b2016-07-20 21:56:58 +080078 MP Initialize Library initialization.
79
80 This service will allocate AP reset vector and wakeup all APs to do APs
81 initialization.
82
83 This service must be invoked before all other MP Initialize Library
84 service are invoked.
85
86 @retval EFI_SUCCESS MP initialization succeeds.
87 @retval Others MP initialization fails.
88
89**/
90EFI_STATUS
91EFIAPI
92MpInitLibInitialize (
93 VOID
94 )
95{
Jeff Fane59f8f62016-07-20 23:33:25 +080096 UINT32 MaxLogicalProcessorNumber;
97 UINT32 ApStackSize;
Jeff Fanf7f85d82016-07-20 22:56:09 +080098 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
Jeff Fane59f8f62016-07-20 23:33:25 +080099 UINTN BufferSize;
Jeff Fan9ebcf0f2016-07-20 23:32:17 +0800100 UINT32 MonitorFilterSize;
Jeff Fane59f8f62016-07-20 23:33:25 +0800101 VOID *MpBuffer;
102 UINTN Buffer;
103 CPU_MP_DATA *CpuMpData;
Jeff Fan9ebcf0f2016-07-20 23:32:17 +0800104 UINT8 ApLoopMode;
Jeff Fane59f8f62016-07-20 23:33:25 +0800105 UINT8 *MonitorBuffer;
Jeff Fanf7f85d82016-07-20 22:56:09 +0800106 UINTN ApResetVectorSize;
Jeff Fane59f8f62016-07-20 23:33:25 +0800107 UINTN BackupBufferAddr;
108 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
Jeff Fanf7f85d82016-07-20 22:56:09 +0800109
110 AsmGetAddressMap (&AddressMap);
111 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
Jeff Fane59f8f62016-07-20 23:33:25 +0800112 ApStackSize = PcdGet32(PcdCpuApStackSize);
Jeff Fan9ebcf0f2016-07-20 23:32:17 +0800113 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
114
Jeff Fane59f8f62016-07-20 23:33:25 +0800115 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
116 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
117 BufferSize += sizeof (CPU_MP_DATA);
118 BufferSize += ApResetVectorSize;
119 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
120 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
121 ASSERT (MpBuffer != NULL);
122 ZeroMem (MpBuffer, BufferSize);
123 Buffer = (UINTN) MpBuffer;
124
125 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);
126 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
127 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);
128 CpuMpData->Buffer = Buffer;
129 CpuMpData->CpuApStackSize = ApStackSize;
130 CpuMpData->BackupBuffer = BackupBufferAddr;
131 CpuMpData->BackupBufferSize = ApResetVectorSize;
132 CpuMpData->EndOfPeiFlag = FALSE;
133 CpuMpData->WakeupBuffer = (UINTN) -1;
134 CpuMpData->CpuCount = 1;
135 CpuMpData->BspNumber = 0;
136 CpuMpData->WaitEvent = NULL;
137 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);
138 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);
139 InitializeSpinLock(&CpuMpData->MpLock);
140 //
141 // Save assembly code information
142 //
143 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
144 //
145 // Finally set AP loop mode
146 //
147 CpuMpData->ApLoopMode = ApLoopMode;
148 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
149 //
150 // Store BSP's MTRR setting
151 //
152 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
153
Jeff Fanf7f85d82016-07-20 22:56:09 +0800154 return EFI_SUCCESS;
Jeff Fan3e8ad6b2016-07-20 21:56:58 +0800155}
156
157/**
158 Gets detailed MP-related information on the requested processor at the
159 instant this call is made. This service may only be called from the BSP.
160
161 @param[in] ProcessorNumber The handle number of processor.
162 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
163 the requested processor is deposited.
164 @param[out] HealthData Return processor health data.
165
166 @retval EFI_SUCCESS Processor information was returned.
167 @retval EFI_DEVICE_ERROR The calling processor is an AP.
168 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
169 @retval EFI_NOT_FOUND The processor with the handle specified by
170 ProcessorNumber does not exist in the platform.
171 @retval EFI_NOT_READY MP Initialize Library is not initialized.
172
173**/
174EFI_STATUS
175EFIAPI
176MpInitLibGetProcessorInfo (
177 IN UINTN ProcessorNumber,
178 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
179 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
180 )
181{
182 return EFI_UNSUPPORTED;
183}
184/**
185 This return the handle number for the calling processor. This service may be
186 called from the BSP and APs.
187
188 @param[out] ProcessorNumber Pointer to the handle number of AP.
189 The range is from 0 to the total number of
190 logical processors minus 1. The total number of
191 logical processors can be retrieved by
192 MpInitLibGetNumberOfProcessors().
193
194 @retval EFI_SUCCESS The current processor handle number was returned
195 in ProcessorNumber.
196 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
197 @retval EFI_NOT_READY MP Initialize Library is not initialized.
198
199**/
200EFI_STATUS
201EFIAPI
202MpInitLibWhoAmI (
203 OUT UINTN *ProcessorNumber
204 )
205{
206 return EFI_UNSUPPORTED;
207}
208/**
209 Retrieves the number of logical processor in the platform and the number of
210 those logical processors that are enabled on this boot. This service may only
211 be called from the BSP.
212
213 @param[out] NumberOfProcessors Pointer to the total number of logical
214 processors in the system, including the BSP
215 and disabled APs.
216 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
217 processors that exist in system, including
218 the BSP.
219
220 @retval EFI_SUCCESS The number of logical processors and enabled
221 logical processors was retrieved.
222 @retval EFI_DEVICE_ERROR The calling processor is an AP.
223 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
224 is NULL.
225 @retval EFI_NOT_READY MP Initialize Library is not initialized.
226
227**/
228EFI_STATUS
229EFIAPI
230MpInitLibGetNumberOfProcessors (
231 OUT UINTN *NumberOfProcessors, OPTIONAL
232 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
233 )
234{
235 return EFI_UNSUPPORTED;
236}