zbao | 7d94cf9 | 2012-07-02 14:19:14 +0800 | [diff] [blame] | 1 | /* $NoKeywords:$ */ |
| 2 | /** |
| 3 | * @file |
| 4 | * |
| 5 | * AMD CPU Reset API, and related functions. |
| 6 | * |
| 7 | * Contains code that initialized the CPU after early reset. |
| 8 | * |
| 9 | * @xrefitem bom "File Content Label" "Release Content" |
| 10 | * @e project: AGESA |
| 11 | * @e sub-project: CPU |
| 12 | * @e \$Revision: 63425 $ @e \$Date: 2011-12-22 11:24:10 -0600 (Thu, 22 Dec 2011) $ |
| 13 | * |
| 14 | */ |
| 15 | /* |
| 16 | ****************************************************************************** |
| 17 | * |
Siyuan Wang | 641f00c | 2013-06-08 11:50:55 +0800 | [diff] [blame] | 18 | * Copyright (c) 2008 - 2012, Advanced Micro Devices, Inc. |
| 19 | * All rights reserved. |
zbao | 7d94cf9 | 2012-07-02 14:19:14 +0800 | [diff] [blame] | 20 | * |
Siyuan Wang | 641f00c | 2013-06-08 11:50:55 +0800 | [diff] [blame] | 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions are met: |
| 23 | * * Redistributions of source code must retain the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer. |
| 25 | * * Redistributions in binary form must reproduce the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer in the |
| 27 | * documentation and/or other materials provided with the distribution. |
| 28 | * * Neither the name of Advanced Micro Devices, Inc. nor the names of |
| 29 | * its contributors may be used to endorse or promote products derived |
| 30 | * from this software without specific prior written permission. |
zbao | 7d94cf9 | 2012-07-02 14:19:14 +0800 | [diff] [blame] | 31 | * |
Siyuan Wang | 641f00c | 2013-06-08 11:50:55 +0800 | [diff] [blame] | 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 33 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 34 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 35 | * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY |
| 36 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 37 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 38 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 39 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 41 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
zbao | 7d94cf9 | 2012-07-02 14:19:14 +0800 | [diff] [blame] | 42 | ****************************************************************************** |
| 43 | */ |
| 44 | |
| 45 | /*---------------------------------------------------------------------------------------- |
| 46 | * M O D U L E S U S E D |
| 47 | *---------------------------------------------------------------------------------------- |
| 48 | */ |
| 49 | #include "AGESA.h" |
| 50 | #include "amdlib.h" |
| 51 | #include "Ids.h" |
| 52 | #include "cpuRegisters.h" |
| 53 | #include "cpuServices.h" |
| 54 | #include "GeneralServices.h" |
| 55 | #include "Table.h" |
| 56 | #include "cpuApicUtilities.h" |
| 57 | #include "cpuEarlyInit.h" |
| 58 | #include "Topology.h" |
| 59 | #include "cpuFamilyTranslation.h" |
| 60 | #include "cpuFeatures.h" |
| 61 | #include "Filecode.h" |
| 62 | CODE_GROUP (G1_PEICC) |
| 63 | RDATA_GROUP (G1_PEICC) |
| 64 | |
| 65 | #define FILECODE PROC_CPU_CPUEARLYINIT_FILECODE |
| 66 | |
| 67 | |
| 68 | /*---------------------------------------------------------------------------------------- |
| 69 | * D E F I N I T I O N S A N D M A C R O S |
| 70 | *---------------------------------------------------------------------------------------- |
| 71 | */ |
| 72 | extern BUILD_OPT_CFG UserOptions; |
| 73 | /*---------------------------------------------------------------------------------------- |
| 74 | * T Y P E D E F S A N D S T R U C T U R E S |
| 75 | *---------------------------------------------------------------------------------------- |
| 76 | */ |
| 77 | |
| 78 | /*---------------------------------------------------------------------------------------- |
| 79 | * P R O T O T Y P E S O F L O C A L F U N C T I O N S |
| 80 | *---------------------------------------------------------------------------------------- |
| 81 | */ |
| 82 | VOID |
| 83 | STATIC |
| 84 | GetPerformEarlyFlag ( |
| 85 | IN OUT UINT32 *PerformEarlyFlag, |
| 86 | IN AMD_CONFIG_PARAMS *StdHeader |
| 87 | ); |
| 88 | |
| 89 | VOID |
| 90 | McaInitializationAtEarly ( |
| 91 | IN CPU_SPECIFIC_SERVICES *FamilyServices, |
| 92 | IN AMD_CPU_EARLY_PARAMS *EarlyParams, |
| 93 | IN AMD_CONFIG_PARAMS *StdHeader |
| 94 | ); |
| 95 | |
| 96 | /*---------------------------------------------------------------------------------------- |
| 97 | * E X P O R T E D F U N C T I O N S |
| 98 | *---------------------------------------------------------------------------------------- |
| 99 | */ |
| 100 | /*------------------------------------------------------------------------------------*/ |
| 101 | /** |
| 102 | * Initializer routine that will be invoked by AmdCpuEarly to initialize the input |
| 103 | * structure for the Cpu Init @ Early routine. |
| 104 | * |
| 105 | * @param[in] StdHeader Opaque handle to standard config header |
| 106 | * @param[in] PlatformConfig Config handle for platform specific information |
| 107 | * @param[in,out] CpuEarlyParamsPtr Service Interface structure to initialize. |
| 108 | * |
| 109 | * @retval AGESA_SUCCESS Always Succeeds |
| 110 | */ |
| 111 | VOID |
| 112 | AmdCpuEarlyInitializer ( |
| 113 | IN AMD_CONFIG_PARAMS *StdHeader, |
| 114 | IN PLATFORM_CONFIGURATION *PlatformConfig, |
| 115 | IN OUT AMD_CPU_EARLY_PARAMS *CpuEarlyParamsPtr |
| 116 | ) |
| 117 | { |
| 118 | ASSERT (CpuEarlyParamsPtr != NULL); |
| 119 | |
| 120 | CpuEarlyParamsPtr->MemInitPState = (UINT8) UserOptions.CfgMemInitPstate; |
| 121 | CpuEarlyParamsPtr->PlatformConfig = *PlatformConfig; |
| 122 | } |
| 123 | /*---------------------------------------------------------------------------------------*/ |
| 124 | /** |
| 125 | * Performs CPU related initialization at the early entry point |
| 126 | * |
| 127 | * This function performs a large list of initialization items. These items |
| 128 | * include: |
| 129 | * |
| 130 | * -1 local APIC initialization |
| 131 | * -2 MSR table initialization |
| 132 | * -3 PCI table initialization |
| 133 | * -4 HT Phy PCI table initialization |
| 134 | * -5 microcode patch loading |
| 135 | * -6 namestring determination/programming |
| 136 | * -7 AP initialization |
| 137 | * -8 power management initialization |
| 138 | * -9 core leveling |
| 139 | * |
| 140 | * This routine must be run by all cores in the system. Please note that |
| 141 | * all APs that enter will never exit. |
| 142 | * |
| 143 | * @param[in] StdHeader Config handle for library and services |
| 144 | * @param[in] PlatformConfig Config handle for platform specific information |
| 145 | * |
| 146 | * @retval AGESA_SUCCESS |
| 147 | * |
| 148 | */ |
| 149 | AGESA_STATUS |
| 150 | AmdCpuEarly ( |
| 151 | IN AMD_CONFIG_PARAMS *StdHeader, |
| 152 | IN PLATFORM_CONFIGURATION *PlatformConfig |
| 153 | ) |
| 154 | { |
| 155 | UINT8 WaitStatus; |
| 156 | UINT8 i; |
| 157 | UINT8 StartCore; |
| 158 | UINT8 EndCore; |
| 159 | UINT32 NodeNum; |
| 160 | UINT32 PrimaryCore; |
| 161 | UINT32 SocketNum; |
| 162 | UINT32 ModuleNum; |
| 163 | UINT32 HighCore; |
| 164 | UINT32 ApHeapIndex; |
| 165 | UINT32 CurrentPerformEarlyFlag; |
| 166 | UINT32 TargetApicId; |
| 167 | AP_WAIT_FOR_STATUS WaitForStatus; |
| 168 | AGESA_STATUS Status; |
| 169 | AGESA_STATUS CalledStatus; |
| 170 | CPU_SPECIFIC_SERVICES *FamilySpecificServices; |
| 171 | AMD_CPU_EARLY_PARAMS CpuEarlyParams; |
| 172 | S_PERFORM_EARLY_INIT_ON_CORE *EarlyTableOnCore; |
| 173 | |
| 174 | Status = AGESA_SUCCESS; |
| 175 | CalledStatus = AGESA_SUCCESS; |
| 176 | |
| 177 | AmdCpuEarlyInitializer (StdHeader, PlatformConfig, &CpuEarlyParams); |
| 178 | |
| 179 | IDS_OPTION_HOOK (IDS_CPU_Early_Override, &CpuEarlyParams, StdHeader); |
| 180 | |
| 181 | GetCpuServicesOfCurrentCore ((CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader); |
| 182 | EarlyTableOnCore = NULL; |
| 183 | FamilySpecificServices->GetEarlyInitOnCoreTable (FamilySpecificServices, (CONST S_PERFORM_EARLY_INIT_ON_CORE **)&EarlyTableOnCore, &CpuEarlyParams, StdHeader); |
| 184 | if (EarlyTableOnCore != NULL) { |
| 185 | GetPerformEarlyFlag (&CurrentPerformEarlyFlag, StdHeader); |
| 186 | for (i = 0; EarlyTableOnCore[i].PerformEarlyInitOnCore != NULL; i++) { |
| 187 | if ((EarlyTableOnCore[i].PerformEarlyInitFlag & CurrentPerformEarlyFlag) != 0) { |
| 188 | IDS_HDT_CONSOLE (CPU_TRACE, " Perform core init step %d\n", i); |
| 189 | EarlyTableOnCore[i].PerformEarlyInitOnCore (FamilySpecificServices, &CpuEarlyParams, StdHeader); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // B S P C O D E T O I N I T I A L I Z E A Ps |
| 195 | // ------------------------------------------------------- |
| 196 | // ------------------------------------------------------- |
| 197 | // IMPORTANT: Here we determine if we are BSP or AP |
| 198 | if (IsBsp (StdHeader, &CalledStatus)) { |
| 199 | |
| 200 | // Even though the bsc does not need to send itself a heap index, this sequence performs other important initialization. |
| 201 | // Use '0' as a dummy heap index value. |
| 202 | GetSocketModuleOfNode (0, &SocketNum, &ModuleNum, StdHeader); |
| 203 | GetCpuServicesOfSocket (SocketNum, (CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader); |
| 204 | FamilySpecificServices->SetApCoreNumber (FamilySpecificServices, SocketNum, ModuleNum, 0, StdHeader); |
| 205 | FamilySpecificServices->TransferApCoreNumber (FamilySpecificServices, StdHeader); |
| 206 | |
| 207 | // Clear BSP's Status Byte |
| 208 | ApUtilWriteControlByte (CORE_ACTIVE, StdHeader); |
| 209 | |
| 210 | NodeNum = 0; |
| 211 | ApHeapIndex = 1; |
| 212 | while (NodeNum < MAX_NODES && |
| 213 | GetSocketModuleOfNode (NodeNum, &SocketNum, &ModuleNum, StdHeader)) { |
| 214 | GetCpuServicesOfSocket (SocketNum, (CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader); |
| 215 | GetGivenModuleCoreRange (SocketNum, ModuleNum, &PrimaryCore, &HighCore, StdHeader); |
| 216 | if (NodeNum == 0) { |
| 217 | StartCore = (UINT8) PrimaryCore + 1; |
| 218 | } else { |
| 219 | StartCore = (UINT8) PrimaryCore; |
| 220 | } |
| 221 | |
| 222 | EndCore = (UINT8) HighCore; |
| 223 | for (i = StartCore; i <= EndCore; i++) { |
| 224 | FamilySpecificServices->SetApCoreNumber (FamilySpecificServices, SocketNum, ModuleNum, ApHeapIndex, StdHeader); |
| 225 | IDS_HDT_CONSOLE (CPU_TRACE, " Launch socket %d core %d\n", SocketNum, i); |
| 226 | if (FamilySpecificServices->LaunchApCore (FamilySpecificServices, SocketNum, ModuleNum, i, PrimaryCore, StdHeader)) { |
| 227 | IDS_HDT_CONSOLE (CPU_TRACE, " Waiting for socket %d core %d\n", SocketNum, i); |
| 228 | GetLocalApicIdForCore (SocketNum, i, &TargetApicId, StdHeader); |
| 229 | WaitStatus = CORE_IDLE; |
| 230 | WaitForStatus.Status = &WaitStatus; |
| 231 | WaitForStatus.NumberOfElements = 1; |
| 232 | WaitForStatus.RetryCount = WAIT_INFINITELY; |
| 233 | WaitForStatus.WaitForStatusFlags = WAIT_STATUS_EQUALITY; |
| 234 | ApUtilWaitForCoreStatus (TargetApicId, &WaitForStatus, StdHeader); |
| 235 | ApHeapIndex++; |
| 236 | } |
| 237 | } |
| 238 | NodeNum++; |
| 239 | } |
| 240 | |
| 241 | // B S P P h a s e - 1 E N D |
| 242 | |
| 243 | IDS_OPTION_HOOK (IDS_BEFORE_PM_INIT, &CpuEarlyParams, StdHeader); |
| 244 | |
| 245 | AGESA_TESTPOINT (TpProcCpuBeforePMFeatureInit, StdHeader); |
| 246 | IDS_HDT_CONSOLE (CPU_TRACE, " Dispatch CPU features before early power mgmt init\n"); |
| 247 | CalledStatus = DispatchCpuFeatures (CPU_FEAT_BEFORE_PM_INIT, PlatformConfig, StdHeader); |
| 248 | if (CalledStatus > Status) { |
| 249 | Status = CalledStatus; |
| 250 | } |
| 251 | |
| 252 | AGESA_TESTPOINT (TpProcCpuPowerMgmtInit, StdHeader); |
| 253 | CalledStatus = PmInitializationAtEarly (&CpuEarlyParams, StdHeader); |
| 254 | if (CalledStatus > Status) { |
| 255 | Status = CalledStatus; |
| 256 | } |
| 257 | |
| 258 | AGESA_TESTPOINT (TpProcCpuEarlyFeatureInit, StdHeader); |
| 259 | IDS_HDT_CONSOLE (CPU_TRACE, " Dispatch CPU features after early power mgmt init\n"); |
| 260 | CalledStatus = DispatchCpuFeatures (CPU_FEAT_AFTER_PM_INIT, PlatformConfig, StdHeader); |
| 261 | |
| 262 | IDS_OPTION_HOOK (IDS_BEFORE_AP_EARLY_HALT, &CpuEarlyParams, StdHeader); |
| 263 | |
| 264 | // Sleep all APs |
| 265 | IDS_HDT_CONSOLE (CPU_TRACE, " Halting all APs\n"); |
| 266 | ApUtilWriteControlByte (CORE_IDLE_HLT, StdHeader); |
| 267 | } else { |
| 268 | ApEntry (StdHeader, &CpuEarlyParams); |
| 269 | } |
| 270 | |
| 271 | if (CalledStatus > Status) { |
| 272 | Status = CalledStatus; |
| 273 | } |
| 274 | |
| 275 | return (Status); |
| 276 | } |
| 277 | |
| 278 | /*--------------------------------------------------------------------------------------- |
| 279 | * L O C A L F U N C T I O N S |
| 280 | *--------------------------------------------------------------------------------------- |
| 281 | */ |
| 282 | |
| 283 | /*---------------------------------------------------------------------------------------*/ |
| 284 | /** |
| 285 | * Initialize Machine Check Architecture registers |
| 286 | * |
| 287 | * This function initializes the MCA MSRs. On cold reset, these registers |
| 288 | * have an invalid data that must be cleared on all cores. |
| 289 | * |
| 290 | * @param[in] StdHeader Config handle for library and services |
| 291 | * |
| 292 | *--------------------------------------------------------------------------------------- |
| 293 | */ |
| 294 | VOID |
| 295 | McaInitialization ( |
| 296 | IN AMD_CONFIG_PARAMS *StdHeader |
| 297 | ) |
| 298 | { |
| 299 | UINT16 TempVar16_a; |
| 300 | UINT32 MsrAddress; |
| 301 | UINT64 MsrData; |
| 302 | CPUID_DATA CpuIdDataStruct; |
| 303 | |
| 304 | if (!(IsWarmReset (StdHeader))) { |
| 305 | // Run CPUID to verify that the processor supports MCE and MCA |
| 306 | // i.e. edx[7], and edx[14] |
| 307 | // CPUID_MODEL = 1 |
| 308 | LibAmdCpuidRead (1, &CpuIdDataStruct, StdHeader); |
| 309 | if ((CpuIdDataStruct.EDX_Reg & 0x4080) != 0) { |
| 310 | // Check to see if the MCG_CTL_P bit is set |
| 311 | // MCG = Global Machine Check Exception Reporting Control Register |
| 312 | LibAmdMsrRead (MSR_MCG_CAP, &MsrData, StdHeader); |
| 313 | if ((MsrData & MCG_CTL_P) != 0) { |
| 314 | TempVar16_a = (UINT16) ((MsrData & 0x000000FF) << 2); |
| 315 | TempVar16_a += MSR_MC0_CTL; |
| 316 | |
| 317 | // Initialize the data |
| 318 | MsrData = 0; |
| 319 | for (MsrAddress = MSR_MC0_CTL; MsrAddress < TempVar16_a; MsrAddress++) { |
| 320 | LibAmdMsrWrite (MsrAddress, &MsrData, StdHeader); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /*---------------------------------------------------------------------------------------*/ |
| 329 | /** |
| 330 | * Initialize Machine Check Architecture registers |
| 331 | * |
| 332 | * This function acts as a wrapper for calling the McaInitialization |
| 333 | * routine at AmdInitEarly. |
| 334 | * |
| 335 | * @param[in] FamilyServices The current Family Specific Services. |
| 336 | * @param[in] EarlyParams Service parameters. |
| 337 | * @param[in] StdHeader Config handle for library and services. |
| 338 | * |
| 339 | */ |
| 340 | VOID |
| 341 | McaInitializationAtEarly ( |
| 342 | IN CPU_SPECIFIC_SERVICES *FamilyServices, |
| 343 | IN AMD_CPU_EARLY_PARAMS *EarlyParams, |
| 344 | IN AMD_CONFIG_PARAMS *StdHeader |
| 345 | ) |
| 346 | { |
| 347 | McaInitialization (StdHeader); |
| 348 | } |
| 349 | |
| 350 | /*---------------------------------------------------------------------------------------*/ |
| 351 | /** |
| 352 | * Runs the given task on all cores (including self) on the socket of the executing |
| 353 | * core 0. |
| 354 | * |
| 355 | * This function is used to invoke all APs on the socket of the executing core 0 to |
| 356 | * run a specified AGESA procedure. |
| 357 | * |
| 358 | * @param[in] TaskPtr Function descriptor |
| 359 | * @param[in] StdHeader Config handle for library and services |
| 360 | * @param[in] CpuEarlyParamsPtr Required input parameters for early CPU initialization |
| 361 | * |
| 362 | */ |
| 363 | VOID |
| 364 | ApUtilRunCodeOnAllLocalCoresAtEarly ( |
| 365 | IN AP_TASK *TaskPtr, |
| 366 | IN AMD_CONFIG_PARAMS *StdHeader, |
| 367 | IN AMD_CPU_EARLY_PARAMS *CpuEarlyParamsPtr |
| 368 | ) |
| 369 | { |
| 370 | UINT32 Core; |
| 371 | UINT32 Socket; |
| 372 | UINT32 IgnoredModule; |
| 373 | UINT32 IgnoredCore; |
| 374 | UINT32 ActiveCores; |
| 375 | AGESA_STATUS IgnoredSts; |
| 376 | |
| 377 | IdentifyCore (StdHeader, &Socket, &IgnoredModule, &IgnoredCore, &IgnoredSts); |
| 378 | GetActiveCoresInCurrentSocket (&ActiveCores, StdHeader); |
| 379 | |
| 380 | for (Core = 1; Core < (UINT8) ActiveCores; ++Core) { |
| 381 | ApUtilRunCodeOnSocketCore ((UINT8)Socket, (UINT8)Core, TaskPtr, StdHeader); |
| 382 | } |
| 383 | ApUtilTaskOnExecutingCore (TaskPtr, StdHeader, (VOID *) CpuEarlyParamsPtr); |
| 384 | } |
| 385 | |
| 386 | /*---------------------------------------------------------------------------------------*/ |
| 387 | /** |
| 388 | * Get current condition, such as warm/cold reset, to determine if related function |
| 389 | * need to be performed at early stage |
| 390 | * |
| 391 | * @param[in, out] PerformEarlyFlag Perform early flag. |
| 392 | * @param[in] StdHeader Config handle for library and services. |
| 393 | * |
| 394 | */ |
| 395 | VOID |
| 396 | STATIC |
| 397 | GetPerformEarlyFlag ( |
| 398 | IN OUT UINT32 *PerformEarlyFlag, |
| 399 | IN AMD_CONFIG_PARAMS *StdHeader |
| 400 | ) |
| 401 | { |
| 402 | *PerformEarlyFlag = 0; |
| 403 | if (IsWarmReset (StdHeader)) { |
| 404 | *PerformEarlyFlag |= PERFORM_EARLY_WARM_RESET; |
| 405 | } else { |
| 406 | *PerformEarlyFlag |= PERFORM_EARLY_COLD_BOOT; |
| 407 | } |
| 408 | return; |
| 409 | } |