AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Family / 0x10 / RevD / F10RevDUtilities.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Family_10 revision Dx specific utility functions.
6  *
7  * Provides numerous utility functions specific to family 10h rev D.
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_REVD_F10REVDUTILITIES_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 BOOLEAN
82 F10CommonRevDSetDownCoreRegister (
83   IN       CPU_CORE_LEVELING_FAMILY_SERVICES *FamilySpecificServices,
84   IN       UINT32 *Socket,
85   IN       UINT32 *Module,
86   IN       UINT32 *LeveledCores,
87   IN       CORE_LEVELING_TYPE CoreLevelMode,
88   IN       AMD_CONFIG_PARAMS *StdHeader
89   );
90
91 BOOLEAN
92 F10CommonRevDGetProcIddMax (
93   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
94   IN       UINT8                  Pstate,
95      OUT   UINT32                 *ProcIddMax,
96   IN       AMD_CONFIG_PARAMS      *StdHeader
97   );
98
99 BOOLEAN
100 F10CommonRevDGetNbCofVidUpdate (
101   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
102   IN       PCI_ADDR *PciAddress,
103      OUT   BOOLEAN *NbVidUpdateAll,
104   IN       AMD_CONFIG_PARAMS *StdHeader
105   );
106
107 BOOLEAN
108 F10CommonRevDGetNbPstateInfo (
109   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
110   IN       PLATFORM_CONFIGURATION *PlatformConfig,
111   IN       PCI_ADDR               *PciAddress,
112   IN       UINT32                 NbPstate,
113      OUT   UINT32                 *FreqNumeratorInMHz,
114      OUT   UINT32                 *FreqDivisor,
115      OUT   UINT32                 *VoltageInuV,
116   IN       AMD_CONFIG_PARAMS      *StdHeader
117   );
118
119 AGESA_STATUS
120 F10RevDGetMinMaxNbFrequency (
121   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
122   IN       PLATFORM_CONFIGURATION *PlatformConfig,
123   IN       PCI_ADDR               *PciAddress,
124      OUT   UINT32                 *MinFreqInMHz,
125      OUT   UINT32                 *MaxFreqInMHz,
126   IN       AMD_CONFIG_PARAMS      *StdHeader
127   );
128
129 UINT8
130 F10CommonRevDGetNumberOfPhysicalCores (
131   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
132   IN       AMD_CONFIG_PARAMS      *StdHeader
133   );
134
135 /*----------------------------------------------------------------------------------------
136  *                          E X P O R T E D    F U N C T I O N S
137  *----------------------------------------------------------------------------------------
138  */
139
140 /*---------------------------------------------------------------------------------------*/
141 /**
142  * Set down core register on a revision D processor.
143  *
144  * This function set F3x190 Downcore Control Register[5:0]
145  *
146  * @param[in]   FamilySpecificServices   The current Family Specific Services.
147  * @param[in]   Socket                   Socket ID.
148  * @param[in]   Module                   Module ID in socket.
149  * @param[in]   LeveledCores             Number of core.
150  * @param[in]   CoreLevelMode            Core level mode.
151  * @param[in]   StdHeader                Header for library and services.
152  *
153  * @retval      TRUE                     Down Core register is updated.
154  * @retval      FALSE                    Down Core register is not updated.
155  */
156 BOOLEAN
157 F10CommonRevDSetDownCoreRegister (
158   IN       CPU_CORE_LEVELING_FAMILY_SERVICES *FamilySpecificServices,
159   IN       UINT32 *Socket,
160   IN       UINT32 *Module,
161   IN       UINT32 *LeveledCores,
162   IN       CORE_LEVELING_TYPE CoreLevelMode,
163   IN       AMD_CONFIG_PARAMS *StdHeader
164   )
165 {
166   UINT32    TempVar32_a;
167   UINT32    CoreDisableBits;
168   PCI_ADDR  PciAddress;
169   BOOLEAN   IsUpdated;
170   AGESA_STATUS AgesaStatus;
171
172   IsUpdated = FALSE;
173
174   switch (*LeveledCores) {
175   case 1:
176     CoreDisableBits = DOWNCORE_MASK_SINGLE;
177     break;
178   case 2:
179     CoreDisableBits = DOWNCORE_MASK_DUAL;
180     break;
181   case 3:
182     CoreDisableBits = DOWNCORE_MASK_TRI;
183     break;
184   case 4:
185     CoreDisableBits = DOWNCORE_MASK_FOUR;
186     break;
187   case 5:
188     CoreDisableBits = DOWNCORE_MASK_FIVE;
189     break;
190   default:
191     CoreDisableBits = 0;
192     break;
193   }
194
195   if (CoreDisableBits != 0) {
196     if (GetPciAddress (StdHeader, (UINT8) *Socket, (UINT8) *Module, &PciAddress, &AgesaStatus)) {
197       PciAddress.Address.Function = FUNC_3;
198       PciAddress.Address.Register = NORTH_BRIDGE_CAPABILITIES_REG;
199
200       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
201       TempVar32_a = ((TempVar32_a >> 12) & 0x3) | ((TempVar32_a >> 13) & 0x4);
202       if (TempVar32_a == 0) {
203         CoreDisableBits &= 0x1;
204       } else if (TempVar32_a == 1) {
205         CoreDisableBits &= 0x3;
206       } else if (TempVar32_a == 2) {
207         CoreDisableBits &= 0x7;
208       } else if (TempVar32_a == 3) {
209         CoreDisableBits &= 0x0F;
210       } else if (TempVar32_a == 4) {
211         CoreDisableBits &= 0x1F;
212       } else if (TempVar32_a == 5) {
213         CoreDisableBits &= 0x3F;
214       }
215       PciAddress.Address.Register = DOWNCORE_CTRL;
216       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
217       if ((TempVar32_a | CoreDisableBits) != TempVar32_a) {
218         TempVar32_a |= CoreDisableBits;
219         LibAmdPciWrite (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
220         IsUpdated = TRUE;
221       }
222     }
223   }
224
225   return IsUpdated;
226 }
227
228
229 CONST CPU_CORE_LEVELING_FAMILY_SERVICES ROMDATA F10RevDCoreLeveling =
230 {
231   0,
232   F10CommonRevDSetDownCoreRegister
233 };
234
235 /*---------------------------------------------------------------------------------------*/
236 /**
237  *  Get CPU pstate current on a revision D processor.
238  *
239  *  @CpuServiceMethod{::F_CPU_GET_IDD_MAX}.
240  *
241  *    This function returns the ProcIddMax.
242  *
243  *  @param[in]     FamilySpecificServices    The current Family Specific Services.
244  *  @param[in]     Pstate                    The P-state to check.
245  *  @param[out]    ProcIddMax                P-state current in mA.
246  *  @param[in]     StdHeader                 Handle of Header for calling lib functions and services.
247  *
248  *  @retval        TRUE                      P-state is enabled
249  *  @retval        FALSE                     P-state is disabled
250  */
251 BOOLEAN
252 F10CommonRevDGetProcIddMax (
253   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
254   IN       UINT8                  Pstate,
255      OUT   UINT32                 *ProcIddMax,
256   IN       AMD_CONFIG_PARAMS      *StdHeader
257   )
258 {
259   UINT32       IddDiv;
260   UINT32       CmpCap;
261   UINT32       MultiNodeCpu;
262   UINT32       NbCaps;
263   UINT32       MsrAddress;
264   UINT64       PstateMsr;
265   BOOLEAN      IsPstateEnabled;
266   PCI_ADDR     PciAddress;
267
268   IsPstateEnabled = FALSE;
269
270   MsrAddress = (UINT32) (Pstate + PS_REG_BASE);
271   ASSERT (MsrAddress <= PS_MAX_REG);
272
273   LibAmdMsrRead (MsrAddress, &PstateMsr, StdHeader);
274   if (((PSTATE_MSR *) &PstateMsr)->PsEnable == 1) {
275     OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader);
276     PciAddress.Address.Function = FUNC_3;
277     PciAddress.Address.Register = NB_CAPS_REG;
278     LibAmdPciRead (AccessWidth32, PciAddress, &NbCaps, StdHeader); // F3xE8
279
280     switch (((PSTATE_MSR *) &PstateMsr)->IddDiv) {
281     case 0:
282       IddDiv = 1000;
283       break;
284     case 1:
285       IddDiv = 100;
286       break;
287     case 2:
288       IddDiv = 10;
289       break;
290     default:  // IddDiv = 3 is reserved. Use 10
291       IddDiv = 10;
292       break;
293     }
294     MultiNodeCpu = (UINT32) (((NB_CAPS_REGISTER *) &NbCaps)->MultiNodeCpu + 1);
295     CmpCap = (UINT32) (((NB_CAPS_REGISTER *) &NbCaps)->CmpCapHi << 2);
296     CmpCap |= (UINT32) (((NB_CAPS_REGISTER *) &NbCaps)->CmpCapLo);
297     CmpCap++;
298     *ProcIddMax = (UINT32) ((PSTATE_MSR *) &PstateMsr)->IddValue * IddDiv * CmpCap * MultiNodeCpu;
299     IsPstateEnabled = TRUE;
300   }
301   return IsPstateEnabled;
302 }
303
304 /*---------------------------------------------------------------------------------------*/
305 /**
306  *  Returns whether or not BIOS is responsible for configuring the NB COFVID.
307  *
308  *  @CpuServiceMethod{::F_CPU_IS_NBCOF_INIT_NEEDED}.
309  *
310  *  @param[in]   FamilySpecificServices   The current Family Specific Services.
311  *  @param[in]   PciAddress               The northbridge to query by pci base address.
312  *  @param[out]  NbVidUpdateAll           Do all NbVids need to be updated
313  *  @param[in]   StdHeader                Header for library and services
314  *
315  *  @retval      TRUE                    Perform northbridge frequency and voltage config.
316  *  @retval      FALSE                   Do not configure them.
317  */
318 BOOLEAN
319 F10CommonRevDGetNbCofVidUpdate (
320   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
321   IN       PCI_ADDR *PciAddress,
322      OUT   BOOLEAN *NbVidUpdateAll,
323   IN       AMD_CONFIG_PARAMS *StdHeader
324   )
325 {
326   *NbVidUpdateAll = FALSE;
327   return FALSE;
328 }
329
330 /*---------------------------------------------------------------------------------------*/
331 /**
332  *  Determines the NB clock on the desired node.
333  *
334  *  @CpuServiceMethod{::F_CPU_GET_NB_PSTATE_INFO}.
335  *
336  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
337  *  @param[in]     PlatformConfig          Platform profile/build option config structure.
338  *  @param[in]     PciAddress              The segment, bus, and device numbers of the CPU in question.
339  *  @param[in]     NbPstate                The NB P-state number to check.
340  *  @param[out]    FreqNumeratorInMHz      The desired node's frequency numerator in megahertz.
341  *  @param[out]    FreqDivisor             The desired node's frequency divisor.
342  *  @param[out]    VoltageInuV             The desired node's voltage in microvolts.
343  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
344  *
345  *  @retval        TRUE                    NbPstate is valid
346  *  @retval        FALSE                   NbPstate is disabled or invalid
347  */
348 BOOLEAN
349 F10CommonRevDGetNbPstateInfo (
350   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
351   IN       PLATFORM_CONFIGURATION *PlatformConfig,
352   IN       PCI_ADDR               *PciAddress,
353   IN       UINT32                 NbPstate,
354      OUT   UINT32                 *FreqNumeratorInMHz,
355      OUT   UINT32                 *FreqDivisor,
356      OUT   UINT32                 *VoltageInuV,
357   IN       AMD_CONFIG_PARAMS      *StdHeader
358   )
359 {
360   UINT32   LocalPciRegister;
361   UINT64   LocalMsrRegister;
362   BOOLEAN  PstateIsValid;
363
364   PstateIsValid = FALSE;
365   if (NbPstate == 0) {
366     PciAddress->Address.Function = FUNC_3;
367     PciAddress->Address.Register = CPTC0_REG;
368     LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
369     *FreqNumeratorInMHz = ((((CLK_PWR_TIMING_CTRL_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
370     *FreqDivisor = 1;
371     LibAmdMsrRead (MSR_COFVID_STS, &LocalMsrRegister, StdHeader);
372     *VoltageInuV = (1550000 - (12500 * ((UINT32) ((COFVID_STS_MSR *) &LocalMsrRegister)->CurNbVid)));
373     PstateIsValid = TRUE;
374   }
375   return PstateIsValid;
376 }
377
378 /*---------------------------------------------------------------------------------------*/
379 /**
380  *  Returns the node's minimum and maximum northbridge frequency.
381  *
382  *  @CpuServiceMethod{::F_CPU_GET_MIN_MAX_NB_FREQ}.
383  *
384  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
385  *  @param[in]     PlatformConfig          Platform profile/build option config structure.
386  *  @param[in]     PciAddress              The segment, bus, and device numbers of the CPU in question.
387  *  @param[out]    MinFreqInMHz            The node's minimum northbridge frequency.
388  *  @param[out]    MaxFreqInMHz            The node's maximum northbridge frequency.
389  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
390  *
391  *  @retval        AGESA_STATUS            Northbridge frequency is valid
392  */
393 AGESA_STATUS
394 F10RevDGetMinMaxNbFrequency (
395   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
396   IN       PLATFORM_CONFIGURATION *PlatformConfig,
397   IN       PCI_ADDR               *PciAddress,
398      OUT   UINT32                 *MinFreqInMHz,
399      OUT   UINT32                 *MaxFreqInMHz,
400   IN       AMD_CONFIG_PARAMS      *StdHeader
401   )
402 {
403   UINT32       LocalPciRegister;
404
405   PciAddress->Address.Function = FUNC_3;
406   PciAddress->Address.Register = CPTC0_REG;
407   LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
408   *MinFreqInMHz = ((((CLK_PWR_TIMING_CTRL_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
409   *MaxFreqInMHz = *MinFreqInMHz;
410
411   return AGESA_SUCCESS;
412 }
413
414 /*---------------------------------------------------------------------------------------*/
415 /**
416  * Get the number of physical cores of current processor.
417  *
418  * @CpuServiceMethod{::F_CPU_NUMBER_OF_PHYSICAL_CORES}.
419  *
420  * @param[in]      FamilySpecificServices         The current Family Specific Services.
421  * @param[in]      StdHeader                      Handle of Header for calling lib functions and services.
422  *
423  * @return         The number of physical cores.
424  */
425 UINT8
426 F10CommonRevDGetNumberOfPhysicalCores (
427   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
428   IN       AMD_CONFIG_PARAMS      *StdHeader
429   )
430 {
431   UINT32       CmpCap;
432   UINT32       CmpCapOnNode;
433   UINT32       Socket;
434   UINT32       Module;
435   UINT32       Core;
436   UINT32       LocalPciRegister;
437   PCI_ADDR     PciAddress;
438   AGESA_STATUS IgnoredSts;
439
440   CmpCap = 0;
441   IdentifyCore (StdHeader, &Socket, &Module, &Core, &IgnoredSts);
442   for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
443     if (GetPciAddress (StdHeader, Socket, Module, &PciAddress, &IgnoredSts)) {
444       PciAddress.Address.Function = FUNC_3;
445       PciAddress.Address.Register = NB_CAPS_REG;
446       LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
447       CmpCapOnNode = (UINT8) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapHi << 2);
448       CmpCapOnNode |= (UINT8) (((NB_CAPS_REGISTER *) &LocalPciRegister)->CmpCapLo);
449       CmpCapOnNode++;
450       CmpCap += CmpCapOnNode;
451     }
452   }
453   return ((UINT8) CmpCap);
454 }
455