AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Family / 0x10 / RevE / F10RevEUtilities.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Family_10 revision Ex specific utility functions.
6  *
7  * Provides numerous utility functions specific to family 10h rev E.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  CPU/F10
12  * @e \$Revision: 56279 $   @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 2011) $
13  *
14  */
15 /*
16  ******************************************************************************
17  *
18  * Copyright (C) 2012 Advanced Micro Devices, Inc.
19  * All rights reserved.
20  *
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.
31  *
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.
42  *
43  ******************************************************************************
44  */
45
46 /*----------------------------------------------------------------------------------------
47  *                             M O D U L E S    U S E D
48  *----------------------------------------------------------------------------------------
49  */
50 #include "AGESA.h"
51 #include "amdlib.h"
52 #include "Ids.h"
53 #include "cpuRegisters.h"
54 #include "cpuFamilyTranslation.h"
55 #include "cpuF10PowerMgmt.h"
56 #include "GeneralServices.h"
57 #include "cpuEarlyInit.h"
58 #include "cpuRegisters.h"
59 #include "OptionMultiSocket.h"
60 #include "Filecode.h"
61 CODE_GROUP (G1_PEICC)
62 RDATA_GROUP (G2_PEI)
63
64 #define FILECODE PROC_CPU_FAMILY_0X10_REVE_F10REVEUTILITIES_FILECODE
65
66 /*----------------------------------------------------------------------------------------
67  *                   D E F I N I T I O N S    A N D    M A C R O S
68  *----------------------------------------------------------------------------------------
69  */
70 extern OPTION_MULTISOCKET_CONFIGURATION OptionMultiSocketConfiguration;
71 /*----------------------------------------------------------------------------------------
72  *                  T Y P E D E F S     A N D     S T R U C T U R E S
73  *----------------------------------------------------------------------------------------
74  */
75
76 /*----------------------------------------------------------------------------------------
77  *           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
78  *----------------------------------------------------------------------------------------
79  */
80
81 /*----------------------------------------------------------------------------------------
82  *                          E X P O R T E D    F U N C T I O N S
83  *----------------------------------------------------------------------------------------
84  */
85
86 /*---------------------------------------------------------------------------------------*/
87 /**
88  * Set down core register on a revision E processor.
89  *
90  * This function set F3x190 Downcore Control Register[5:0]
91  *
92  * @param[in]   FamilySpecificServices   The current Family Specific Services.
93  * @param[in]   Socket                   Socket ID.
94  * @param[in]   Module                   Module ID in socket.
95  * @param[in]   LeveledCores             Number of core.
96  * @param[in]   CoreLevelMode            Core level mode.
97  * @param[in]   StdHeader                Header for library and services.
98  *
99  * @retval      TRUE                     Down Core register is updated.
100  * @retval      FALSE                    Down Core register is not updated.
101  */
102 BOOLEAN
103 F10CommonRevESetDownCoreRegister (
104   IN       CPU_CORE_LEVELING_FAMILY_SERVICES *FamilySpecificServices,
105   IN       UINT32 *Socket,
106   IN       UINT32 *Module,
107   IN       UINT32 *LeveledCores,
108   IN       CORE_LEVELING_TYPE CoreLevelMode,
109   IN       AMD_CONFIG_PARAMS *StdHeader
110   )
111 {
112   UINT32    TempVar32_a;
113   UINT32    CoreDisableBits;
114   PCI_ADDR  PciAddress;
115   BOOLEAN   IsUpdated;
116   AGESA_STATUS AgesaStatus;
117
118   IsUpdated = FALSE;
119
120   switch (*LeveledCores) {
121   case 1:
122     CoreDisableBits = DOWNCORE_MASK_SINGLE;
123     break;
124   case 2:
125     CoreDisableBits = DOWNCORE_MASK_DUAL;
126     break;
127   case 3:
128     CoreDisableBits = DOWNCORE_MASK_TRI;
129     break;
130   case 4:
131     CoreDisableBits = DOWNCORE_MASK_FOUR;
132     break;
133   case 5:
134     CoreDisableBits = DOWNCORE_MASK_FIVE;
135     break;
136   default:
137     CoreDisableBits = 0;
138     break;
139   }
140
141   if (CoreDisableBits != 0) {
142     if (GetPciAddress (StdHeader, (UINT8) *Socket, (UINT8) *Module, &PciAddress, &AgesaStatus)) {
143       PciAddress.Address.Function = FUNC_3;
144       PciAddress.Address.Register = NORTH_BRIDGE_CAPABILITIES_REG;
145
146       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
147       TempVar32_a = ((TempVar32_a >> 12) & 0x3) | ((TempVar32_a >> 13) & 0x4);
148       if (TempVar32_a == 0) {
149         CoreDisableBits &= 0x1;
150       } else if (TempVar32_a == 1) {
151         CoreDisableBits &= 0x3;
152       } else if (TempVar32_a == 2) {
153         CoreDisableBits &= 0x7;
154       } else if (TempVar32_a == 3) {
155         CoreDisableBits &= 0x0F;
156       } else if (TempVar32_a == 4) {
157         CoreDisableBits &= 0x1F;
158       } else if (TempVar32_a == 5) {
159         CoreDisableBits &= 0x3F;
160       }
161       PciAddress.Address.Register = DOWNCORE_CTRL;
162       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
163       if ((TempVar32_a | CoreDisableBits) != TempVar32_a) {
164         TempVar32_a |= CoreDisableBits;
165         LibAmdPciWrite (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
166         IsUpdated = TRUE;
167       }
168     }
169   }
170
171   return IsUpdated;
172 }
173
174
175 CONST CPU_CORE_LEVELING_FAMILY_SERVICES ROMDATA F10RevECoreLeveling =
176 {
177   0,
178   F10CommonRevESetDownCoreRegister
179 };
180
181 /*---------------------------------------------------------------------------------------*/
182 /**
183  *  Get CPU pstate current on a revision E processor.
184  *
185  *  @CpuServiceMethod{::F_CPU_GET_IDD_MAX}.
186  *
187  *    This function returns the ProcIddMax.
188  *
189  *  @param[in]     FamilySpecificServices    The current Family Specific Services.
190  *  @param[in]     Pstate                    The P-state to check.
191  *  @param[out]    ProcIddMax                P-state current in mA.
192  *  @param[in]     StdHeader                 Handle of Header for calling lib functions and services.
193  *
194  *  @retval        TRUE                      P-state is enabled
195  *  @retval        FALSE                     P-state is disabled
196  */
197 BOOLEAN
198 F10CommonRevEGetProcIddMax (
199   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
200   IN       UINT8                  Pstate,
201      OUT   UINT32                 *ProcIddMax,
202   IN       AMD_CONFIG_PARAMS      *StdHeader
203   )
204 {
205   UINT32       IddDiv;
206   UINT32       CmpCap;
207   UINT32       LocalPciRegister;
208   UINT32       MsrAddress;
209   UINT32       MultiNodeCpu;
210   UINT64       PstateMsr;
211   BOOLEAN      IsPstateEnabled;
212   PCI_ADDR     PciAddress;
213
214   IsPstateEnabled = FALSE;
215
216   MsrAddress = (UINT32) (Pstate + PS_REG_BASE);
217   ASSERT (MsrAddress <= PS_MAX_REG);
218
219   LibAmdMsrRead (MsrAddress, &PstateMsr, StdHeader);
220   if (((PSTATE_MSR *) &PstateMsr)->PsEnable == 1) {
221     OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader);
222
223     PciAddress.Address.Function = FUNC_3;
224     PciAddress.Address.Register = NB_CAPS_REG;
225     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader); // F3xE8
226
227     switch (((PSTATE_MSR *) &PstateMsr)->IddDiv) {
228     case 0:
229       IddDiv = 1000;
230       break;
231     case 1:
232       IddDiv = 100;
233       break;
234     case 2:
235       IddDiv = 10;
236       break;
237     default:  // IddDiv = 3 is reserved. Use 10
238       ASSERT (FALSE);
239       IddDiv = 10;
240       break;
241     }
242     MultiNodeCpu = (UINT32) (((NB_CAPS_REGISTER *) &LocalPciRegister)->MultiNodeCpu + 1);
243     CmpCap = (UINT32) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapHi << 2);
244     CmpCap |= (UINT32) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapLo);
245     CmpCap++;
246     *ProcIddMax = (UINT32) ((PSTATE_MSR *) &PstateMsr)->IddValue * IddDiv * CmpCap * MultiNodeCpu;
247     IsPstateEnabled = TRUE;
248   }
249   return IsPstateEnabled;
250 }
251
252 /*---------------------------------------------------------------------------------------*/
253 /**
254  *  Determines the NB clock on the desired node.
255  *
256  *  @CpuServiceMethod{::F_CPU_GET_NB_PSTATE_INFO}.
257  *
258  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
259  *  @param[in]     PlatformConfig          Platform profile/build option config structure.
260  *  @param[in]     PciAddress              The segment, bus, and device numbers of the CPU in question.
261  *  @param[in]     NbPstate                The NB P-state number to check.
262  *  @param[out]    FreqNumeratorInMHz      The desired node's frequency numerator in megahertz.
263  *  @param[out]    FreqDivisor             The desired node's frequency divisor.
264  *  @param[out]    VoltageInuV             The desired node's voltage in microvolts.
265  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
266  *
267  *  @retval        TRUE                    NbPstate is valid
268  *  @retval        FALSE                   NbPstate is disabled or invalid
269  */
270 BOOLEAN
271 F10CommonRevEGetNbPstateInfo (
272   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
273   IN       PLATFORM_CONFIGURATION *PlatformConfig,
274   IN       PCI_ADDR               *PciAddress,
275   IN       UINT32                 NbPstate,
276      OUT   UINT32                 *FreqNumeratorInMHz,
277      OUT   UINT32                 *FreqDivisor,
278      OUT   UINT32                 *VoltageInuV,
279   IN       AMD_CONFIG_PARAMS      *StdHeader
280   )
281 {
282   UINT32   LocalPciRegister;
283   UINT64   LocalMsrRegister;
284   BOOLEAN  PstateIsValid;
285
286   PstateIsValid = FALSE;
287   if (NbPstate == 0) {
288     PciAddress->Address.Function = FUNC_3;
289     PciAddress->Address.Register = CPTC0_REG;
290     LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
291     *FreqNumeratorInMHz = ((((CLK_PWR_TIMING_CTRL_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
292     *FreqDivisor = 1;
293     LibAmdMsrRead (MSR_COFVID_STS, &LocalMsrRegister, StdHeader);
294     *VoltageInuV = (1550000 - (12500 * ((UINT32) ((COFVID_STS_MSR *) &LocalMsrRegister)->CurNbVid)));
295     PstateIsValid = TRUE;
296   }
297   return PstateIsValid;
298 }
299
300 /*---------------------------------------------------------------------------------------*/
301 /**
302  *  Returns the node's minimum and maximum northbridge frequency.
303  *
304  *  @CpuServiceMethod{::F_CPU_GET_MIN_MAX_NB_FREQ}.
305  *
306  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
307  *  @param[in]     PlatformConfig          Platform profile/build option config structure.
308  *  @param[in]     PciAddress              The segment, bus, and device numbers of the CPU in question.
309  *  @param[out]    MinFreqInMHz            The node's minimum northbridge frequency.
310  *  @param[out]    MaxFreqInMHz            The node's maximum northbridge frequency.
311  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
312  *
313  *  @retval        AGESA_STATUS            Northbridge frequency is valid
314  */
315 AGESA_STATUS
316 F10RevEGetMinMaxNbFrequency (
317   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
318   IN       PLATFORM_CONFIGURATION *PlatformConfig,
319   IN       PCI_ADDR               *PciAddress,
320      OUT   UINT32                 *MinFreqInMHz,
321      OUT   UINT32                 *MaxFreqInMHz,
322   IN       AMD_CONFIG_PARAMS      *StdHeader
323   )
324 {
325   UINT32       LocalPciRegister;
326
327   PciAddress->Address.Function = FUNC_3;
328   PciAddress->Address.Register = CPTC0_REG;
329   LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
330   *MinFreqInMHz = ((((CLK_PWR_TIMING_CTRL_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
331   *MaxFreqInMHz = *MinFreqInMHz;
332
333   return AGESA_SUCCESS;
334 }
335
336 /*---------------------------------------------------------------------------------------*/
337 /**
338  *  Returns whether or not BIOS is responsible for configuring the NB COFVID.
339  *
340  *  @CpuServiceMethod{::F_CPU_IS_NBCOF_INIT_NEEDED}.
341  *
342  *  @param[in]   FamilySpecificServices   The current Family Specific Services.
343  *  @param[in]   PciAddress               The northbridge to query by pci base address.
344  *  @param[out]  NbVidUpdateAll           Do all NbVids need to be updated
345  *  @param[in]   StdHeader                Header for library and services
346  *
347  *  @retval      TRUE                    Perform northbridge frequency and voltage config.
348  *  @retval      FALSE                   Do not configure them.
349  */
350 BOOLEAN
351 F10CommonRevEGetNbCofVidUpdate (
352   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
353   IN       PCI_ADDR *PciAddress,
354      OUT   BOOLEAN *NbVidUpdateAll,
355   IN       AMD_CONFIG_PARAMS *StdHeader
356   )
357 {
358   UINT32 ProductInfoRegister;
359
360   PciAddress->Address.Register = PRCT_INFO_REG;
361   PciAddress->Address.Function = FUNC_3;
362   LibAmdPciRead (AccessWidth32, *PciAddress, &ProductInfoRegister, StdHeader);
363   *NbVidUpdateAll = (BOOLEAN) (((PRODUCT_INFO_REGISTER *) &ProductInfoRegister)->NbVidUpdateAll == 1);
364   return (BOOLEAN) (((PRODUCT_INFO_REGISTER *) &ProductInfoRegister)->NbCofVidUpdate == 1);
365 }
366
367 /*---------------------------------------------------------------------------------------*/
368 /**
369  * Get the number of physical cores of current processor.
370  *
371  * @CpuServiceMethod{::F_CPU_NUMBER_OF_PHYSICAL_CORES}.
372  *
373  * @param[in]      FamilySpecificServices         The current Family Specific Services.
374  * @param[in]      StdHeader                      Handle of Header for calling lib functions and services.
375  *
376  * @return         The number of physical cores.
377  */
378 UINT8
379 F10CommonRevEGetNumberOfPhysicalCores (
380   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
381   IN       AMD_CONFIG_PARAMS      *StdHeader
382   )
383 {
384   UINT32       CmpCap;
385   UINT32       LocalPciRegister;
386   PCI_ADDR     PciAddress;
387
388   OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader);
389   PciAddress.Address.Function = FUNC_3;
390   PciAddress.Address.Register = NB_CAPS_REG;
391   LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
392   CmpCap = (UINT8) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapHi << 2);
393   CmpCap |= (UINT8) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapLo);
394
395   return (UINT8) (CmpCap + 1);
396 }