AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Family / 0x15 / cpuF15PowerCheck.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Family_15 P-State power check
6  *
7  * Performs the "Processor-Systemboard Power Delivery Compatibility Check" as
8  * described in the BKDG.
9  *
10  * @xrefitem bom "File Content Label" "Release Content"
11  * @e project:      AGESA
12  * @e sub-project:  CPU/Family/0x15
13  * @e \$Revision: 56273 $   @e \$Date: 2011-07-11 12:53:52 -0600 (Mon, 11 Jul 2011) $
14  *
15  */
16 /*
17  ******************************************************************************
18  *
19  * Copyright (C) 2012 Advanced Micro Devices, Inc.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions are met:
24  *     * Redistributions of source code must retain the above copyright
25  *       notice, this list of conditions and the following disclaimer.
26  *     * Redistributions in binary form must reproduce the above copyright
27  *       notice, this list of conditions and the following disclaimer in the
28  *       documentation and/or other materials provided with the distribution.
29  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
30  *       its contributors may be used to endorse or promote products derived
31  *       from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
37  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  ******************************************************************************
45  */
46
47 /*----------------------------------------------------------------------------------------
48  *                             M O D U L E S    U S E D
49  *----------------------------------------------------------------------------------------
50  */
51 #include "AGESA.h"
52 #include "amdlib.h"
53 #include "cpuF15PowerMgmt.h"
54 #include "cpuRegisters.h"
55 #include "cpuApicUtilities.h"
56 #include "cpuFamilyTranslation.h"
57 #include "cpuF15PowerCheck.h"
58 #include "cpuServices.h"
59 #include "GeneralServices.h"
60 #include "OptionMultiSocket.h"
61 #include "Filecode.h"
62 CODE_GROUP (G3_DXE)
63 RDATA_GROUP (G3_DXE)
64
65 #define FILECODE PROC_CPU_FAMILY_0X15_CPUF15POWERCHECK_FILECODE
66
67 /*----------------------------------------------------------------------------------------
68  *                   D E F I N I T I O N S    A N D    M A C R O S
69  *----------------------------------------------------------------------------------------
70  */
71
72 /*----------------------------------------------------------------------------------------
73  *                  T Y P E D E F S     A N D     S T R U C T U R E S
74  *----------------------------------------------------------------------------------------
75  */
76
77 /*----------------------------------------------------------------------------------------
78  *           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
79  *----------------------------------------------------------------------------------------
80  */
81 VOID
82 STATIC
83 F15PwrCheckAllCoresGoToLegalPstate (
84   IN       VOID *ErrorData,
85   IN       AMD_CONFIG_PARAMS *StdHeader
86   );
87
88 VOID
89 STATIC
90 F15PwrCheckPrimaryCoresAdjustPstates (
91   IN       VOID *ErrorData,
92   IN       AMD_CONFIG_PARAMS *StdHeader
93   );
94
95 VOID
96 STATIC
97 F15PwrCheckAllCoresGoToCurrentPs (
98   IN     VOID *ErrorData,
99   IN     AMD_CONFIG_PARAMS *StdHeader
100   );
101
102 VOID
103 STATIC
104 F15PmPwrChkCopyPstate (
105   IN     UINT8 Dest,
106   IN     UINT8 Src,
107   IN     AMD_CONFIG_PARAMS *StdHeader
108   );
109
110 /*----------------------------------------------------------------------------------------
111  *                          E X P O R T E D    F U N C T I O N S
112  *----------------------------------------------------------------------------------------
113  */
114 extern OPTION_MULTISOCKET_CONFIGURATION OptionMultiSocketConfiguration;
115 /*---------------------------------------------------------------------------------------*/
116 /**
117  * Family 15h core 0 entry point for performing the family 15h Processor-
118  * Systemboard Power Delivery Check.
119  *
120  * The steps are as follows:
121  *    1. Starting with SW P0, loop through all P-states until a passing state
122  *       is found.  A passing state is one in which the current required by
123  *       the CPU is less than the maximum amount of current that the system
124  *       can provide to the CPU.  If P0 is under the limit, no further action
125  *       is necessary.
126  *    2. If at least one P-State is under the limit & at least one P-State is
127  *       over the limit, the BIOS must:
128  *       a. If the processor's current P-State is disabled by the power check,
129  *          then the BIOS must request a transition to an enabled P-state
130  *          using MSRC001_0062[PstateCmd] and wait for MSRC001_0063[CurPstate]
131  *          to reflect the new value.
132  *       b. Program D18F4x15C[BoostSrc] to zero.
133  *       c. Copy the contents of the enabled P-state MSRs to the highest
134  *          performance P-state locations.
135  *       d. Request a P-state transition to the P-state MSR containing the
136  *          COF/VID values currently applied.
137  *       e. Adjust the following P-state parameters affected by the P-state
138  *          MSR copy by subtracting the number of P-states that are disabled
139  *          by the power check.
140  *          1. F3x64[HtcPstateLimit]
141  *          2. F3x68[SwPstateLimit]
142  *          3. F3xDC[PstateMaxVal]
143  *    3. If all P-States are over the limit, the BIOS must:
144  *       a. If the processor's current P-State is !=F3xDC[PstateMaxVal], then
145  *          write F3xDC[PstateMaxVal] to MSRC001_0062[PstateCmd] and wait for
146  *          MSRC001_0063[CurPstate] to reflect the new value.
147  *       b. If MSRC001_0061[PstateMaxVal]!=000b, copy the contents of the P-state
148  *          MSR pointed to by F3xDC[PstateMaxVal] to the software P0 MSR.
149  *          Write 000b to MSRC001_0062[PstateCmd] and wait for MSRC001_0063
150  *          [CurPstate] to reflect the new value.
151  *       c. Adjust the following P-state parameters to zero:
152  *          1. F3x64[HtcPstateLimit]
153  *          2. F3x68[SwPstateLimit]
154  *          3. F3xDC[PstateMaxVal]
155  *       d. Program D18F4x15C[BoostSrc] to zero.
156  *
157  * @param[in]  FamilySpecificServices  The current Family Specific Services.
158  * @param[in]  CpuEarlyParams          Service parameters
159  * @param[in]  StdHeader               Config handle for library and services.
160  *
161  */
162 VOID
163 F15PmPwrCheck (
164   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
165   IN       AMD_CPU_EARLY_PARAMS  *CpuEarlyParams,
166   IN       AMD_CONFIG_PARAMS     *StdHeader
167   )
168 {
169   UINT8       DisPsNum;
170   UINT8       PsMaxVal;
171   UINT8       Pstate;
172   UINT32      ProcIddMax;
173   UINT32      LocalPciRegister;
174   UINT32      Socket;
175   UINT32      Module;
176   UINT32      Core;
177   UINT32      AndMask;
178   UINT32      OrMask;
179   UINT32      PstateLimit;
180   UINT32            HighCore;
181   UINT32            LowCore;
182   UINT32            ModuleIndex;
183   UINT64      LocalMsrRegister;
184   BOOLEAN           AllPstatesDisabled;
185   AP_TASK     TaskPtr;
186   PCI_ADDR          PciAddress;
187   AGESA_STATUS IgnoredSts;
188   PWRCHK_ERROR_DATA ErrorData;
189
190   // get the socket number
191   IdentifyCore (StdHeader, &Socket, &Module, &Core, &IgnoredSts);
192
193   ASSERT (Core == 0);
194
195   // get the Max P-state value
196   for (PsMaxVal = NM_PS_REG - 1; PsMaxVal != 0; --PsMaxVal) {
197     LibAmdMsrRead (PS_REG_BASE + PsMaxVal, &LocalMsrRegister, StdHeader);
198     if (((F15_PSTATE_MSR *) &LocalMsrRegister)->PsEnable == 1) {
199       break;
200     }
201   }
202
203   ErrorData.HwPstateNumber = (UINT8) (PsMaxVal + 1);
204   GetPciAddress (StdHeader, Socket, Module, &PciAddress, &IgnoredSts);
205   PciAddress.Address.Function = FUNC_4;
206   PciAddress.Address.Register = CPB_CTRL_REG;
207   LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader); // F4x15C
208   ErrorData.NumberOfBoostStates = (UINT8) ((F15_CPB_CTRL_REGISTER *) &LocalPciRegister)->NumBoostStates;
209
210   // Starting with SW P0, loop through all P-states until a passing state
211   // is found.  A passing state is one in which the current required by
212   // the CPU is less than the maximum amount of current that the system
213   // can provide to the CPU.  If P0 is under the limit, no further action
214   // is necessary.
215   DisPsNum = 0;
216   AllPstatesDisabled = TRUE;
217
218   for (Pstate = ErrorData.NumberOfBoostStates; Pstate < ErrorData.HwPstateNumber; Pstate++) {
219     if (FamilySpecificServices->GetProcIddMax (FamilySpecificServices, Pstate, &ProcIddMax, StdHeader)) {
220       if (ProcIddMax > CpuEarlyParams->PlatformConfig.VrmProperties[CoreVrm].CurrentLimit) {
221         // Add to event log the Pstate that exceeded the current limit
222         PutEventLog (AGESA_WARNING,
223                      CPU_EVENT_PM_PSTATE_OVERCURRENT,
224                      Socket, Pstate, 0, 0, StdHeader);
225         DisPsNum++;
226       } else {
227         AllPstatesDisabled = FALSE;
228         break;
229       }
230     }
231   }
232
233   if (DisPsNum != 0) {
234     ErrorData.NumberOfSwPstatesDisabled = DisPsNum;
235
236     if (AllPstatesDisabled) {
237       // All P-states are over the limit
238     PutEventLog (AGESA_FATAL,
239                  CPU_EVENT_PM_ALL_PSTATE_OVERCURRENT,
240                  Socket, 0, 0, 0, StdHeader);
241       ErrorData.NumberOfSwPstatesDisabled--;
242   }
243
244     // Launch APs to transition to a valid P-state
245     TaskPtr.FuncAddress.PfApTaskI = F15PwrCheckAllCoresGoToLegalPstate;
246     TaskPtr.DataTransfer.DataSizeInDwords = SIZE_IN_DWORDS (PWRCHK_ERROR_DATA);
247     TaskPtr.DataTransfer.DataPtr = &ErrorData;
248     TaskPtr.DataTransfer.DataTransferFlags = 0;
249     TaskPtr.ExeFlags = WAIT_FOR_CORE;
250     ApUtilRunCodeOnAllLocalCoresAtEarly (&TaskPtr, StdHeader, CpuEarlyParams);
251
252     // If any software P-states are disabled, then program D18F4x15C[BoostSrc] to zero.
253       AndMask = 0xFFFFFFFF;
254       ((F15_CPB_CTRL_REGISTER *) &AndMask)->BoostSrc = 0;
255       OrMask = 0x00000000;
256       OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader); // F4x15C
257
258     // Modify P-state MSRs on one core per die
259     TaskPtr.FuncAddress.PfApTaskI = F15PwrCheckPrimaryCoresAdjustPstates;
260
261     for (ModuleIndex = 0; ModuleIndex < GetPlatformNumberOfModules (); ModuleIndex++) {
262         if (ModuleIndex != Module) {
263           if (GetGivenModuleCoreRange (Socket, ModuleIndex, &LowCore, &HighCore, StdHeader)) {
264             ApUtilRunCodeOnSocketCore ((UINT8)Socket, (UINT8)LowCore, &TaskPtr, StdHeader);
265           }
266         }
267       }
268     F15PwrCheckPrimaryCoresAdjustPstates (&ErrorData, StdHeader);
269
270     // Launch APs to transition to the current P-state at its new location
271     TaskPtr.FuncAddress.PfApTaskI = F15PwrCheckAllCoresGoToCurrentPs;
272     ApUtilRunCodeOnAllLocalCoresAtEarly (&TaskPtr, StdHeader, CpuEarlyParams);
273
274     // Final Step
275     //    F3x64[HtPstatelimit] -= disPsNum
276     //    F3x68[SwPstateLimit] -= disPsNum
277     //    F3xDC[PstateMaxVal] -= disPsNum
278
279     PciAddress.Address.Function = FUNC_3;
280     PciAddress.Address.Register = HTC_REG;
281     AndMask = 0xFFFFFFFF;
282     ((HTC_REGISTER *) &AndMask)->HtcPstateLimit = 0;
283     OrMask = 0x00000000;
284     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader); // F3x64
285     PstateLimit = ((HTC_REGISTER *) &LocalPciRegister)->HtcPstateLimit;
286     if (PstateLimit > ErrorData.NumberOfSwPstatesDisabled) {
287       PstateLimit -= ErrorData.NumberOfSwPstatesDisabled;
288       ((HTC_REGISTER *) &OrMask)->HtcPstateLimit = PstateLimit;
289     }
290     OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader); // F3x64
291
292     PciAddress.Address.Register = SW_PS_LIMIT_REG;
293     AndMask = 0xFFFFFFFF;
294     ((SW_PS_LIMIT_REGISTER *) &AndMask)->SwPstateLimit = 0;
295     OrMask = 0x00000000;
296     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader); // F3x68
297     PstateLimit = ((SW_PS_LIMIT_REGISTER *) &LocalPciRegister)->SwPstateLimit;
298     if (PstateLimit > ErrorData.NumberOfSwPstatesDisabled) {
299       PstateLimit -= ErrorData.NumberOfSwPstatesDisabled;
300       ((SW_PS_LIMIT_REGISTER *) &OrMask)->SwPstateLimit = PstateLimit;
301     }
302     OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader); // F3x68
303
304     PciAddress.Address.Register = CPTC2_REG;
305     AndMask = 0xFFFFFFFF;
306     ((CLK_PWR_TIMING_CTRL2_REGISTER *) &AndMask)->PstateMaxVal = 0;
307     OrMask = 0x00000000;
308     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader); // F3xDC
309     PstateLimit = ((CLK_PWR_TIMING_CTRL2_REGISTER *) &LocalPciRegister)->PstateMaxVal;
310     if (PstateLimit > ErrorData.NumberOfSwPstatesDisabled) {
311       PstateLimit -= ErrorData.NumberOfSwPstatesDisabled;
312       ((CLK_PWR_TIMING_CTRL2_REGISTER *) &OrMask)->PstateMaxVal = PstateLimit;
313     }
314     OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader); // F3xDC
315   }
316 }
317
318 /*---------------------------------------------------------------------------------------*/
319 /**
320  * First phase core-level error handler called if any p-states were determined
321  * to be out of range for the mother board.
322  *
323  * Transitions to a legal P-state if necessary (steps 2a and 3a) on each core.
324  *
325  * @param[in]  ErrorData            Details about the error condition.
326  * @param[in]  StdHeader            Config handle for library and services.
327  *
328  */
329 VOID
330 STATIC
331 F15PwrCheckAllCoresGoToLegalPstate (
332   IN       VOID *ErrorData,
333   IN       AMD_CONFIG_PARAMS *StdHeader
334   )
335 {
336   UINT8                 CurrentPs;
337   UINT64 LocalMsrRegister;
338   CPU_SPECIFIC_SERVICES *FamilySpecificServices;
339
340   if (IsCorePairPrimary (FirstCoreIsComputeUnitPrimary, StdHeader)) {
341     LibAmdMsrRead (MSR_PSTATE_STS, &LocalMsrRegister, StdHeader);
342     CurrentPs = (UINT8) (((PSTATE_STS_MSR *) &LocalMsrRegister)->CurPstate);
343
344     if (CurrentPs < ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled) {
345       GetCpuServicesOfCurrentCore ((CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader);
346           FamilySpecificServices->TransitionPstate (FamilySpecificServices, ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled, (BOOLEAN) TRUE, StdHeader);
347         }
348   }
349 }
350
351 /*---------------------------------------------------------------------------------------*/
352 /**
353  * Core-level error handler called if any p-states were determined to be out
354  * of range for the mother board.
355  *
356  * This function implements steps 2c and the first half of 3b on one core per die.
357  *
358  * @param[in]  ErrorData            Details about the error condition.
359  * @param[in]  StdHeader            Config handle for library and services.
360  *
361  */
362 VOID
363 STATIC
364 F15PwrCheckPrimaryCoresAdjustPstates (
365   IN       VOID *ErrorData,
366   IN       AMD_CONFIG_PARAMS *StdHeader
367   )
368 {
369   UINT8  i;
370   UINT8  HwPsMaxVal;
371   CPU_SPECIFIC_SERVICES *FamilySpecificServices;
372
373   GetCpuServicesOfCurrentCore ((CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader);
374
375   HwPsMaxVal = (((PWRCHK_ERROR_DATA *) ErrorData)->HwPstateNumber - 1);
376         for (i = ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfBoostStates; (i + ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled) <= HwPsMaxVal; i++) {
377           F15PmPwrChkCopyPstate (i, (i + ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled), StdHeader);
378         }
379
380     // Disable the appropriate P-states if any, starting from HW Pmin
381   for (i = 0; i < ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled; i++) {
382       FamilySpecificServices->DisablePstate (FamilySpecificServices, (HwPsMaxVal - i), StdHeader);
383     }
384 }
385
386 /*---------------------------------------------------------------------------------------*/
387 /**
388  * Second phase core-level error handler called if any p-states were determined
389  * to be out of range for the mother board.
390  *
391  * Transitions to the core's current P-state in its new location (steps 2d and
392  * the second half of 3b) on each core.
393  *
394  * @param[in]  ErrorData            Details about the error condition.
395  * @param[in]  StdHeader            Config handle for library and services.
396  *
397  */
398 VOID
399 STATIC
400 F15PwrCheckAllCoresGoToCurrentPs (
401   IN       VOID *ErrorData,
402   IN       AMD_CONFIG_PARAMS *StdHeader
403   )
404 {
405   UINT8                 CurrentPs;
406   UINT64                LocalMsrRegister;
407   CPU_SPECIFIC_SERVICES *FamilySpecificServices;
408
409   if (IsCorePairPrimary (FirstCoreIsComputeUnitPrimary, StdHeader)) {
410     GetCpuServicesOfCurrentCore ((CONST CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, StdHeader);
411
412     LibAmdMsrRead (MSR_PSTATE_STS, &LocalMsrRegister, StdHeader);
413     CurrentPs = (UINT8) (((PSTATE_STS_MSR *) &LocalMsrRegister)->CurPstate) - ((PWRCHK_ERROR_DATA *) ErrorData)->NumberOfSwPstatesDisabled;
414
415     FamilySpecificServices->TransitionPstate (FamilySpecificServices, CurrentPs, (BOOLEAN) TRUE, StdHeader);
416   }
417 }
418
419 /*---------------------------------------------------------------------------------------*/
420 /**
421  * Copies the contents of one P-State MSR to another.
422  *
423  * @param[in]  Dest              Destination p-state number
424  * @param[in]  Src               Source p-state number
425  * @param[in]  StdHeader         Config handle for library and services
426  *
427  */
428 VOID
429 STATIC
430 F15PmPwrChkCopyPstate (
431   IN       UINT8 Dest,
432   IN       UINT8 Src,
433   IN       AMD_CONFIG_PARAMS *StdHeader
434   )
435 {
436   UINT64 LocalMsrRegister;
437
438   LibAmdMsrRead ((UINT32) (PS_REG_BASE + Src), &LocalMsrRegister, StdHeader);
439   LibAmdMsrWrite ((UINT32) (PS_REG_BASE + Dest), &LocalMsrRegister, StdHeader);
440 }
441