AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Family / 0x15 / OR / F15OrUtilities.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Family_15 models 0 - 0Fh specific utility functions.
6  *
7  * Provides numerous utility functions specific to family 15h OR.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  CPU/Family/0x15/OR
12  * @e \$Revision: 58928 $   @e \$Date: 2011-09-08 16:43:14 -0600 (Thu, 08 Sep 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 "cpuFamilyTranslation.h"
53 #include "cpuF15PowerMgmt.h"
54 #include "cpuF15OrPowerMgmt.h"
55 #include "cpuApicUtilities.h"
56 #include "cpuServices.h"
57 #include "cpuEarlyInit.h"
58 #include "GeneralServices.h"
59 #include "OptionMultiSocket.h"
60 #include "F15OrUtilities.h"
61 #include "Filecode.h"
62 CODE_GROUP (G2_PEI)
63 RDATA_GROUP (G2_PEI)
64 #define FILECODE PROC_CPU_FAMILY_0X15_OR_F15ORUTILITIES_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  * Node ID MSR register fields.
78  * Provide the layout of fields in the Node ID MSR.
79  */
80 typedef struct {
81   UINT64    NodeId:3;                 ///< The core is on the node with this node id.
82   UINT64    NodesPerProcessor:3;      ///< The number of Nodes in this processor.
83   UINT64    BiosScratch:6;            ///< BiosScratch, use as the AP core heap index.
84   UINT64    :(63 - 11);               ///< Reserved.
85 } NODE_ID_MSR_FIELDS;
86
87 /// Node ID MSR.
88 typedef union {
89   NODE_ID_MSR_FIELDS   Fields;        ///< Access the register as individual fields
90   UINT64               Value;         ///< Access the register value.
91 } NODE_ID_MSR;
92
93 /*----------------------------------------------------------------------------------------
94  *           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
95  *----------------------------------------------------------------------------------------
96  */
97
98 VOID
99 STATIC
100 F15OrNbPstateDisCore (
101   IN       AMD_CONFIG_PARAMS *StdHeader
102   );
103
104 BOOLEAN
105 F15OrSetDownCoreRegister (
106   IN       CPU_CORE_LEVELING_FAMILY_SERVICES *FamilySpecificServices,
107   IN       UINT32 *Socket,
108   IN       UINT32 *Module,
109   IN       UINT32 *LeveledCores,
110   IN       CORE_LEVELING_TYPE CoreLevelMode,
111   IN       AMD_CONFIG_PARAMS *StdHeader
112   );
113
114
115 /*----------------------------------------------------------------------------------------
116  *                          E X P O R T E D    F U N C T I O N S
117  *----------------------------------------------------------------------------------------
118  */
119
120 /*---------------------------------------------------------------------------------------*/
121 /**
122  *  Get CPU pstate current.
123  *
124  *  @CpuServiceMethod{::F_CPU_GET_IDD_MAX}.
125  *
126  *    This function returns the ProcIddMax.
127  *
128  *  @param[in]     FamilySpecificServices    The current Family Specific Services.
129  *  @param[in]     Pstate                    The P-state to check.
130  *  @param[out]    ProcIddMax                P-state current in mA.
131  *  @param[in]     StdHeader                 Handle of Header for calling lib functions and services.
132  *
133  *  @retval        TRUE                      P-state is enabled
134  *  @retval        FALSE                     P-state is disabled
135  */
136 BOOLEAN
137 F15OrGetProcIddMax (
138   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
139   IN       UINT8                  Pstate,
140      OUT   UINT32                 *ProcIddMax,
141   IN       AMD_CONFIG_PARAMS      *StdHeader
142   )
143 {
144   UINT32       IddDiv;
145   UINT32       NumberOfPhysicalCores;
146   UINT32       MsrAddress;
147   UINT64       PstateMsr;
148   BOOLEAN      IsPstateEnabled;
149   CPUID_DATA   CpuId;
150
151   IsPstateEnabled = FALSE;
152
153   MsrAddress = (UINT32) (Pstate + PS_REG_BASE);
154   ASSERT (MsrAddress <= PS_MAX_REG);
155
156   LibAmdMsrRead (MsrAddress, &PstateMsr, StdHeader);
157   if (((PSTATE_MSR *) &PstateMsr)->PsEnable == 1) {
158     switch (((PSTATE_MSR *) &PstateMsr)->IddDiv) {
159     case 0:
160       IddDiv = 1000;
161       break;
162     case 1:
163       IddDiv = 100;
164       break;
165     case 2:
166       IddDiv = 10;
167       break;
168     default:  // IddDiv = 3 is reserved. Use 10
169       IddDiv = 10;
170       break;
171     }
172     LibAmdCpuidRead (AMD_CPUID_ASIZE_PCCOUNT, &CpuId, StdHeader);
173     NumberOfPhysicalCores = ((CpuId.ECX_Reg & 0xFF) + 1);
174
175     *ProcIddMax = (UINT32) ((PSTATE_MSR *) &PstateMsr)->IddValue * IddDiv * NumberOfPhysicalCores;
176     IsPstateEnabled = TRUE;
177   }
178   return IsPstateEnabled;
179 }
180
181 /*---------------------------------------------------------------------------------------*/
182 /**
183  * Set down core register on Orochi
184  *
185  * This function set F3x190 Downcore Control Register[5:0]
186  *
187  * @param[in]   FamilySpecificServices   The current Family Specific Services.
188  * @param[in]   Socket                   Socket ID.
189  * @param[in]   Module                   Module ID in socket.
190  * @param[in]   LeveledCores             Number of core.
191  * @param[in]   CoreLevelMode            Core level mode.
192  * @param[in]   StdHeader                Header for library and services.
193  *
194  * @retval      TRUE                     Down Core register is updated.
195  * @retval      FALSE                    Down Core register is not updated.
196  */
197 BOOLEAN
198 F15OrSetDownCoreRegister (
199   IN       CPU_CORE_LEVELING_FAMILY_SERVICES *FamilySpecificServices,
200   IN       UINT32 *Socket,
201   IN       UINT32 *Module,
202   IN       UINT32 *LeveledCores,
203   IN       CORE_LEVELING_TYPE CoreLevelMode,
204   IN       AMD_CONFIG_PARAMS *StdHeader
205   )
206 {
207   UINT8     Xbar2SriFreeListCBC;
208   UINT8     L3FreeListCBC;
209   UINT32    TempVar32_a;
210   UINT32    CoreDisableBits;
211   UINT32    NumberOfEnabledCores;
212   UINT32    NumberOfEnabledCU;
213   PCI_ADDR  PciAddress;
214   BOOLEAN   IsUpdated;
215   AGESA_STATUS                    AgesaStatus;
216   NB_CAPS_REGISTER                NbCaps;
217   FREE_LIST_BUFFER_COUNT_REGISTER FreeListBufferCount;
218   L3_BUFFER_COUNT_REGISTER        L3BufferCnt;
219
220   IsUpdated = FALSE;
221
222   if (CoreLevelMode == CORE_LEVEL_COMPUTE_UNIT) {
223     switch (*LeveledCores) {
224     case 1:
225       CoreDisableBits = DOWNCORE_MASK_SINGLE;
226       break;
227     case 2:
228       CoreDisableBits = DOWNCORE_MASK_DUAL_COMPUTE_UNIT;
229       break;
230     case 3:
231       CoreDisableBits = DOWNCORE_MASK_TRI_COMPUTE_UNIT;
232       break;
233     case 4:
234       CoreDisableBits = DOWNCORE_MASK_FOUR_COMPUTE_UNIT;
235       break;
236     default:
237       CoreDisableBits = 0;
238       break;
239     }
240
241   } else {
242     switch (*LeveledCores) {
243     case 1:
244       CoreDisableBits = DOWNCORE_MASK_SINGLE;
245       break;
246     case 2:
247       CoreDisableBits = DOWNCORE_MASK_DUAL;
248       break;
249     case 4:
250       CoreDisableBits = DOWNCORE_MASK_FOUR;
251       break;
252     case 6:
253       CoreDisableBits = DOWNCORE_MASK_SIX;
254       break;
255     default:
256       CoreDisableBits = 0;
257       break;
258     }
259   }
260
261   if (CoreDisableBits != 0) {
262     if (GetPciAddress (StdHeader, (UINT8) *Socket, (UINT8) *Module, &PciAddress, &AgesaStatus)) {
263       PciAddress.Address.Function = FUNC_5;
264       PciAddress.Address.Register = NORTH_BRIDGE_CAPABILITIES_2_REG;
265       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
266       TempVar32_a = (TempVar32_a & 0xFF) + 1;
267       TempVar32_a = (1 << TempVar32_a) - 1;
268       CoreDisableBits &= TempVar32_a;
269       NumberOfEnabledCores = ~(CoreDisableBits | ~(TempVar32_a));
270
271       PciAddress.Address.Function = FUNC_3;
272       PciAddress.Address.Register = DOWNCORE_CTRL;
273       LibAmdPciRead (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
274       if ((TempVar32_a | CoreDisableBits) != TempVar32_a) {
275         TempVar32_a |= CoreDisableBits;
276         LibAmdPciWrite (AccessWidth32, PciAddress, &TempVar32_a, StdHeader);
277         IsUpdated = TRUE;
278
279         for (NumberOfEnabledCU = 0; NumberOfEnabledCores != 0; NumberOfEnabledCores >>= 2) {
280           NumberOfEnabledCU += ((NumberOfEnabledCores & 3) != 0) ? 1 : 0;
281         }
282         switch (NumberOfEnabledCU) {
283         case 1:
284           Xbar2SriFreeListCBC = 0x16;
285           L3FreeListCBC = 0x1C;
286           break;
287         case 2:
288           Xbar2SriFreeListCBC = 0x14;
289           L3FreeListCBC = 0x18;
290           break;
291         case 3:
292           Xbar2SriFreeListCBC = 0x12;
293           L3FreeListCBC = 0x14;
294           break;
295         case 4:
296           Xbar2SriFreeListCBC = 0x10;
297           L3FreeListCBC = 0x10;
298           break;
299         default:
300           Xbar2SriFreeListCBC = 0x16;
301           L3FreeListCBC = 0xE;
302           break;
303         }
304         //D18F3x1A0[8:4] L3FreeListCBC:
305         //BIOS: IF (NumOfCompUnitsOnNode==1) THEN 1Ch ELSEIF (NumOfCompUnitsOnNode==2)
306         //THEN 18h ELSEIF (NumOfCompUnitsOnNode==3) THEN 14h ELSEIF
307         //(NumOfCompUnitsOnNode==4) THEN 10h ELSEIF (NumOfCompUnitsOnNode==5) THEN 11h
308         //ELSE 0Eh ENDIF.
309         PciAddress.Address.Function = FUNC_3;
310         PciAddress.Address.Register = L3_BUFFER_COUNT_REG;
311         LibAmdPciRead (AccessWidth32, PciAddress, &L3BufferCnt, StdHeader);
312         L3BufferCnt.L3FreeListCBC = L3FreeListCBC;
313         LibAmdPciWrite (AccessWidth32, PciAddress, &L3BufferCnt, StdHeader);
314
315         //D18F3x7C[4:0]Xbar2SriFreeListCBC:
316         //BIOS: IF (L3Enabled) THEN 16h ELSEIF (D18F5x80[Enabled[3]]==1) THEN 10h ELSEIF
317         //(D18F5x80[Enabled[2]]==1) THEN 12h ELSEIF (D18F5x80[Enabled[1]]==1) THEN 14h ELSE 16h ENDIF.
318         PciAddress.Address.Function = FUNC_3;
319         PciAddress.Address.Register = NB_CAPS_REG;
320         LibAmdPciRead (AccessWidth32, PciAddress, &NbCaps, StdHeader);
321         if (NbCaps.L3Capable == 0) {
322           PciAddress.Address.Function = FUNC_3;
323           PciAddress.Address.Register = FREE_LIST_BUFFER_COUNT_REG;
324           LibAmdPciRead (AccessWidth32, PciAddress, &FreeListBufferCount, StdHeader);
325           FreeListBufferCount.Xbar2SriFreeListCBC = Xbar2SriFreeListCBC;
326           LibAmdPciWrite (AccessWidth32, PciAddress, &FreeListBufferCount, StdHeader);
327         }
328       }
329     }
330   }
331
332   return IsUpdated;
333 }
334
335
336 CONST CPU_CORE_LEVELING_FAMILY_SERVICES ROMDATA F15OrCoreLeveling =
337 {
338   0,
339   F15OrSetDownCoreRegister
340 };
341
342
343 /*---------------------------------------------------------------------------------------*/
344 /**
345  *  Determines the NB clock on the desired node.
346  *
347  *  @CpuServiceMethod{::F_CPU_GET_NB_FREQ}.
348  *
349  *  @param[in]   FamilySpecificServices   The current Family Specific Services.
350  *  @param[out]  FrequencyInMHz           Northbridge clock frequency in MHz.
351  *  @param[in]   StdHeader                Header for library and services.
352  *
353  *  @return      AGESA_SUCCESS            FrequencyInMHz is valid.
354  */
355 AGESA_STATUS
356 F15OrGetCurrentNbFrequency (
357   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
358      OUT   UINT32 *FrequencyInMHz,
359   IN       AMD_CONFIG_PARAMS *StdHeader
360   )
361 {
362   UINT32  NbFid;
363   UINT32  NbDid;
364   UINT32  LocalPciRegister;
365   PCI_ADDR     PciAddress;
366
367   if (OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader)) {
368     PciAddress.Address.Function = FUNC_5;
369     PciAddress.Address.Register = NB_PSTATE_STATUS;
370     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
371     NbFid = ((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbFid;
372     NbDid = ((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbDid;
373     *FrequencyInMHz = (((NbFid + 4) * 200) / (1 << NbDid));
374   }
375   return AGESA_SUCCESS;
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_SUCCESS           Northbridge frequency is valid
392  */
393 AGESA_STATUS
394 F15OrGetMinMaxNbFrequency (
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   INT8         NbPsMaxVal;
404   UINT32       LocalPciRegister;
405   UINT32       FreqNumerator;
406   UINT32       FreqDivisor;
407   BOOLEAN      CustomNbPs;
408   AGESA_STATUS AgesaStatus;
409
410   CustomNbPs      = FALSE;
411   AgesaStatus     = AGESA_ERROR;
412
413   // Obtain the max NB frequency on the node
414   PciAddress->Address.Function = FUNC_5;
415   PciAddress->Address.Register = NB_PSTATE_0;
416   LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
417   if (((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbPstateEn == 1) {
418     FreqNumerator = ((((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
419     FreqDivisor   = (1 << ((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbDid);
420
421     *MaxFreqInMHz = (FreqNumerator / FreqDivisor);
422     AgesaStatus = AGESA_SUCCESS;
423   }
424
425   // If platform configuration disable NB P-states, return the NB P0 frequency
426   // as both the min and max frequency on the node.
427   if (PlatformConfig->PlatformProfile.PlatformPowerPolicy == Performance) {
428     *MinFreqInMHz = *MaxFreqInMHz;
429   } else {
430     PciAddress->Address.Function = FUNC_5;
431     PciAddress->Address.Register = NB_PSTATE_CTRL;
432     LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
433     NbPsMaxVal = (INT8) ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateMaxVal;
434
435     // Obtain the min NB frequency on the node, starting from NB Pmin
436     for (; NbPsMaxVal >= 0; NbPsMaxVal--) {
437       PciAddress->Address.Function = FUNC_5;
438       PciAddress->Address.Register = (NB_PSTATE_0 + (4 * NbPsMaxVal));
439       LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
440
441       // Ensure that the NB Pstate is enabled
442       if (((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbPstateEn == 1) {
443         FreqNumerator = ((((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
444         FreqDivisor   = (1 << ((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbDid);
445
446         *MinFreqInMHz = (FreqNumerator / FreqDivisor);
447         AgesaStatus = AGESA_SUCCESS;
448         break;
449       }
450     }
451   }
452   IDS_OPTION_HOOK (IDS_NBPS_MIN_FREQ, MinFreqInMHz, StdHeader);
453   return AgesaStatus;
454 }
455
456 /*---------------------------------------------------------------------------------------*/
457 /**
458  *  Determines the NB clock on the desired node.
459  *
460  *  @CpuServiceMethod{::F_CPU_GET_NB_PSTATE_INFO}.
461  *
462  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
463  *  @param[in]     PlatformConfig          Platform profile/build option config structure.
464  *  @param[in]     PciAddress              The segment, bus, and device numbers of the CPU in question.
465  *  @param[in]     NbPstate                The NB P-state number to check.
466  *  @param[out]    FreqNumeratorInMHz      The desired node's frequency numerator in megahertz.
467  *  @param[out]    FreqDivisor             The desired node's frequency divisor.
468  *  @param[out]    VoltageInuV             The desired node's voltage in microvolts.
469  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
470  *
471  *  @retval        TRUE                    NbPstate is valid
472  *  @retval        FALSE                   NbPstate is disabled or invalid
473  */
474 BOOLEAN
475 F15OrGetNbPstateInfo (
476   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
477   IN       PLATFORM_CONFIGURATION *PlatformConfig,
478   IN       PCI_ADDR               *PciAddress,
479   IN       UINT32                 NbPstate,
480      OUT   UINT32                 *FreqNumeratorInMHz,
481      OUT   UINT32                 *FreqDivisor,
482      OUT   UINT32                 *VoltageInuV,
483   IN       AMD_CONFIG_PARAMS      *StdHeader
484   )
485 {
486   UINT32   LocalPciRegister;
487   BOOLEAN  PstateIsValid;
488
489   PstateIsValid = FALSE;
490
491   // If NB P1, P2, or P3 is requested, make sure that NB Pstate is enabled
492   if ((NbPstate == 0) || (FamilySpecificServices->IsNbPstateEnabled (FamilySpecificServices, PlatformConfig, StdHeader))) {
493     PciAddress->Address.Function = FUNC_5;
494     PciAddress->Address.Register = NB_PSTATE_CTRL;
495     LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
496
497     if (NbPstate <= ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateMaxVal) {
498       PciAddress->Address.Register = (NB_PSTATE_0 + (4 * NbPstate));
499       LibAmdPciRead (AccessWidth32, *PciAddress, &LocalPciRegister, StdHeader);
500
501       // Ensure that requested NbPstate is enabled
502       if (((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbPstateEn == 1) {
503         *FreqNumeratorInMHz = ((((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbFid + 4) * 200);
504         *FreqDivisor = (1 << ((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbDid);
505         *VoltageInuV = (1550000 - (12500 * (((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbVid)));
506         PstateIsValid = TRUE;
507       }
508     }
509   }
510   return PstateIsValid;
511 }
512
513 /*---------------------------------------------------------------------------------------*/
514 /**
515  * Get the number of physical cores of current processor.
516  *
517  * @CpuServiceMethod{::F_CPU_NUMBER_OF_PHYSICAL_CORES}.
518  *
519  * @param[in]      FamilySpecificServices         The current Family Specific Services.
520  * @param[in]      StdHeader                      Handle of Header for calling lib functions and services.
521  *
522  * @return         The number of physical cores.
523  */
524 UINT8
525 F15OrGetNumberOfPhysicalCores (
526   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
527   IN       AMD_CONFIG_PARAMS      *StdHeader
528   )
529 {
530   UINT32       CmpCap;
531   UINT32       CmpCapOnNode;
532   UINT32       Socket;
533   UINT32       Module;
534   UINT32       Core;
535   UINT32       LocalPciRegister;
536   PCI_ADDR     PciAddress;
537   AGESA_STATUS IgnoredSts;
538
539   CmpCap = 0;
540   IdentifyCore (StdHeader, &Socket, &Module, &Core, &IgnoredSts);
541   for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
542     if (GetPciAddress (StdHeader, Socket, Module, &PciAddress, &IgnoredSts)) {
543       PciAddress.Address.Function = FUNC_5;
544       PciAddress.Address.Register = NORTH_BRIDGE_CAPABILITIES_2_REG;
545       LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
546       CmpCapOnNode = (UINT8) (LocalPciRegister & 0xFF);
547       CmpCapOnNode++;
548       CmpCap += CmpCapOnNode;
549     }
550   }
551   return ((UINT8) CmpCap);
552 }
553
554 /*---------------------------------------------------------------------------------------*/
555 /**
556  *  Use the Mailbox Register to get the Ap Mailbox info for the current core.
557  *
558  *  @CpuServiceMethod{::F_CPU_AMD_GET_AP_MAILBOX_FROM_HARDWARE}.
559  *
560  *  Access the mailbox register used with this NB family.  This is valid until the
561  *  point that some init code initializes the mailbox register for its normal use.
562  *  The Machine Check Misc (Thresholding) register is available as both a PCI config
563  *  register and a MSR, so it can be used as a mailbox from HT to other functions.
564  *
565  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
566  *  @param[out]    ApMailboxInfo           The AP Mailbox info
567  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
568  *
569  */
570 VOID
571 F15OrGetApMailboxFromHardware (
572   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
573      OUT   AP_MAILBOXES           *ApMailboxInfo,
574   IN       AMD_CONFIG_PARAMS      *StdHeader
575   )
576 {
577   UINT64 MailboxInfo;
578
579   LibAmdMsrRead (MSR_MC_MISC_LINK_THRESHOLD, &MailboxInfo, StdHeader);
580   // Mailbox info is in bits 32 thru 43, 12 bits.
581   ApMailboxInfo->ApMailInfo.Info = (((UINT32) (MailboxInfo >> 32)) & (UINT32)0x00000FFF);
582   LibAmdMsrRead (MSR_MC_MISC_L3_THRESHOLD, &MailboxInfo, StdHeader);
583   // Mailbox info is in bits 32 thru 43, 12 bits.
584   ApMailboxInfo->ApMailExtInfo.Info = (((UINT32) (MailboxInfo >> 32)) & (UINT32)0x00000FFF);
585 }
586
587
588 /*---------------------------------------------------------------------------------------*/
589 /**
590  *  Set the system AP core number in the AP's Mailbox.
591  *
592  *  @CpuServiceMethod{::F_CPU_SET_AP_CORE_NUMBER}.
593  *
594  *  Access the mailbox register used with this NB family.  This is only intended to
595  *  run on the BSC at the time of initial AP launch.
596  *
597  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
598  *  @param[in]     Socket                 The AP's socket
599  *  @param[in]     Module                 The AP's module
600  *  @param[in]     ApCoreNumber           The AP's unique core number
601  *  @param[in]     StdHeader              Handle of Header for calling lib functions and services.
602  *
603  */
604 VOID
605 F15OrSetApCoreNumber (
606   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
607   IN       UINT32                 Socket,
608   IN       UINT32                 Module,
609   IN       UINT32                 ApCoreNumber,
610   IN       AMD_CONFIG_PARAMS      *StdHeader
611   )
612 {
613   UINT32   LocalPciRegister;
614   PCI_ADDR PciAddress;
615   AGESA_STATUS IgnoredStatus;
616
617   GetPciAddress (StdHeader, Socket, Module, &PciAddress, &IgnoredStatus);
618   PciAddress.Address.Function = FUNC_3;
619   PciAddress.Address.Register = 0x170;
620   LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
621   ((AP_MAIL_EXT_INFO *) &LocalPciRegister)->Fields.HeapIndex = ApCoreNumber;
622   LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
623 }
624
625
626 /*---------------------------------------------------------------------------------------*/
627 /**
628  *  Get this AP's system core number from hardware.
629  *
630  *  @CpuServiceMethod{::F_CPU_GET_AP_CORE_NUMBER}.
631  *
632  *  Returns the system core number from the scratch MSR, where
633  *  it was saved at heap initialization.
634  *
635  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
636  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
637  *
638  *  @return        The AP's unique core number
639  */
640 UINT32
641 F15OrGetApCoreNumber (
642   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
643   IN       AMD_CONFIG_PARAMS      *StdHeader
644   )
645 {
646   NODE_ID_MSR NodeIdMsr;
647
648   LibAmdMsrRead (0xC001100C, &NodeIdMsr.Value, StdHeader);
649   return (UINT32) NodeIdMsr.Fields.BiosScratch;
650 }
651
652
653 /*---------------------------------------------------------------------------------------*/
654 /**
655  *  Move the AP's core number from the mailbox to hardware.
656  *
657  *  @CpuServiceMethod{::F_CPU_TRANSFER_AP_CORE_NUMBER}.
658  *
659  *  Transfers this AP's system core number from the mailbox to
660  *  the NodeId MSR and initializes the other NodeId fields.
661  *
662  *  @param[in]     FamilySpecificServices  The current Family Specific Services.
663  *  @param[in]     StdHeader               Handle of Header for calling lib functions and services.
664  *
665  */
666 VOID
667 F15OrTransferApCoreNumber (
668   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
669   IN       AMD_CONFIG_PARAMS      *StdHeader
670   )
671 {
672   AP_MAILBOXES Mailboxes;
673   NODE_ID_MSR NodeIdMsr;
674   UINT64 ExtFeatures;
675
676   NodeIdMsr.Value = 0;
677   FamilySpecificServices->GetApMailboxFromHardware (FamilySpecificServices, &Mailboxes, StdHeader);
678   NodeIdMsr.Fields.BiosScratch = Mailboxes.ApMailExtInfo.Fields.HeapIndex;
679   NodeIdMsr.Fields.NodeId = Mailboxes.ApMailInfo.Fields.Node;
680   NodeIdMsr.Fields.NodesPerProcessor = Mailboxes.ApMailInfo.Fields.ModuleType;
681   LibAmdMsrWrite (0xC001100C, &NodeIdMsr.Value, StdHeader);
682
683   // Indicate that the NodeId MSR is supported.
684   LibAmdMsrRead (MSR_CPUID_EXT_FEATS, &ExtFeatures, StdHeader);
685   ExtFeatures = (ExtFeatures | BIT51);
686   LibAmdMsrWrite (MSR_CPUID_EXT_FEATS, &ExtFeatures, StdHeader);
687 }
688
689
690 /*---------------------------------------------------------------------------------------*/
691 /**
692  * Disable NB P-state.
693  *   - clear F5x1[6C:64]
694  *   - clear F5x170[NbPstateMaxVal]
695  *   - set   F5x170[SwNbPstateLoDis]
696  *   - clear MSRC001_00[6B:64][NbPstate]
697  *
698  * @param[in]     FamilySpecificServices  The current Family Specific Services
699  * @param[in]     CpuEarlyParamsPtr       Service Parameters
700  * @param[in]     StdHeader               Handle of Header for calling lib functions and services.
701  */
702 VOID
703 F15OrNbPstateDis (
704   IN       CPU_SPECIFIC_SERVICES  *FamilySpecificServices,
705   IN       AMD_CPU_EARLY_PARAMS   *CpuEarlyParamsPtr,
706   IN       AMD_CONFIG_PARAMS      *StdHeader
707   )
708 {
709   UINT32       i;
710   UINT32       PciData;
711   UINT32       AndMask;
712   AP_TASK      TaskPtr;
713   PCI_ADDR     PciAddress;
714
715   // Check whether NB P-state is disabled
716   if (!FamilySpecificServices->IsNbPstateEnabled (FamilySpecificServices, &CpuEarlyParamsPtr->PlatformConfig, StdHeader)) {
717
718     IDS_HDT_CONSOLE (CPU_TRACE, "  NB Pstates disabled\n");
719
720     OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader);
721
722     AndMask = 0x00000000;
723     // If NbPstateHi is not NB P0, get the Pstate pointed to by NbPstateHi and copy it's value to NB P0
724     PciAddress.Address.Function = FUNC_5;
725     PciAddress.Address.Register = NB_PSTATE_CTRL;
726     LibAmdPciRead (AccessWidth32, PciAddress, &PciData, StdHeader);
727     if (((NB_PSTATE_CTRL_REGISTER *) &PciData)->NbPstateHi != 0) {
728       PciAddress.Address.Register = NB_PSTATE_0 + (((NB_PSTATE_CTRL_REGISTER *) &PciData)->NbPstateHi * 4);
729       LibAmdPciRead (AccessWidth32, PciAddress, &PciData, StdHeader);
730       PciAddress.Address.Register = NB_PSTATE_0;
731       OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, PciData, StdHeader);
732     }
733
734     // Clear F5x1[6C:64]
735     for (i = 1; i < NM_NB_PS_REG; i++) {
736       PciAddress.Address.Register = NB_PSTATE_0 + (i * 4);
737       OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, AndMask, StdHeader);
738     }
739
740     // Clear F5x170[NbPstateMaxVal] and set F5x170[SwNbPstateLoDis]
741     PciAddress.Address.Register = NB_PSTATE_CTRL;
742     AndMask = 0xFFFFFFFF;
743     PciData = 0x00000000;
744     ((NB_PSTATE_CTRL_REGISTER *) &AndMask)->NbPstateMaxVal = 0;
745     ((NB_PSTATE_CTRL_REGISTER *) &PciData)->SwNbPstateLoDis = 1;
746     OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, PciData, StdHeader);
747
748     // Clear MSRC001_00[6B:64][NbPstate] on cores
749     TaskPtr.FuncAddress.PfApTask = F15OrNbPstateDisCore;
750     TaskPtr.DataTransfer.DataSizeInDwords = 0;
751     TaskPtr.DataTransfer.DataPtr = NULL;
752     TaskPtr.ExeFlags = WAIT_FOR_CORE;
753     ApUtilRunCodeOnAllLocalCoresAtEarly (&TaskPtr, StdHeader, CpuEarlyParamsPtr);
754
755     // Once we are done disabling NB Pstates, clear F5x170[SwNbPstateLoDis]
756     AndMask = 0xFFFFFFFF;
757     PciData = 0x00000000;
758     ((NB_PSTATE_CTRL_REGISTER *) &AndMask)->SwNbPstateLoDis = 0;
759     OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, PciData, StdHeader);
760   }
761 }
762
763
764 /*---------------------------------------------------------------------------------------*/
765 /**
766  * Disable NB P-state on core.
767  *   - clear MSRC001_00[6B:64][NbPstate].
768  *
769  * @param[in]     StdHeader               Handle of Header for calling lib functions and services.
770  */
771 VOID
772 STATIC
773 F15OrNbPstateDisCore (
774   IN       AMD_CONFIG_PARAMS *StdHeader
775   )
776 {
777   UINT32 i;
778   UINT64 MsrData;
779
780   // Only one core per compute unit needs to clear NbPstate in P-state MSRs
781   if (IsCorePairPrimary (FirstCoreIsComputeUnitPrimary, StdHeader)) {
782     for (i = MSR_PSTATE_0; i <= MSR_PSTATE_7; i++) {
783       LibAmdMsrRead (i, &MsrData, StdHeader);
784       ((PSTATE_MSR *) &MsrData)->NbPstate = 0;
785       LibAmdMsrWrite (i, &MsrData, StdHeader);
786     }
787   }
788 }
789
790 /*---------------------------------------------------------------------------------------*/
791 /**
792  * A Family Specific Workaround method, to override CPU TDP Limit 2 setting.
793  *
794  * \@TableTypeFamSpecificInstances.
795  *
796  * @param[in]     Data       The table data value, for example to indicate which CPU and Platform types matched.
797  * @param[in]     StdHeader  Config params for library, services.
798  */
799 VOID
800 F15OrOverrideNodeTdpLimit (
801   IN       UINT32              Data,
802   IN       AMD_CONFIG_PARAMS   *StdHeader
803   )
804 {
805   UINT32                OrMask;
806   UINT32                LocalPciRegister;
807   BOOLEAN               IsMultiNodeCpu;
808   PCI_ADDR              PciAddress;
809
810   IsMultiNodeCpu = FALSE;
811   // check if it is MCM part
812   if (OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader)) {
813     PciAddress.Address.Function = FUNC_3;
814     PciAddress.Address.Register = NB_CAPS_REG;
815     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
816     IsMultiNodeCpu = (BOOLEAN) (((NB_CAPS_REGISTER *) &LocalPciRegister)->MultiNodeCpu == 1);
817   }
818
819   if (IsMultiNodeCpu) {
820     PciAddress.Address.Function = FUNC_4;
821     PciAddress.Address.Register = 0x10C;
822     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
823
824     // The correct value is the half of the fused value
825     OrMask = LocalPciRegister & 0xFFFFF000;
826     LocalPciRegister = ((LocalPciRegister & 0x00000FFF) >> 1) | OrMask;
827     LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
828   }
829 }
830
831 /*---------------------------------------------------------------------------------------*/
832 /**
833  * A Family Specific Workaround method, to override CPU Node TDP Accumulator Throttle Threshold setting.
834  *
835  * \@TableTypeFamSpecificInstances.
836  *
837  * @param[in]     Data       The table data value, for example to indicate which CPU and Platform types matched.
838  * @param[in]     StdHeader  Config params for library, services.
839  */
840 VOID
841 F15OrOverrideNodeTdpAccumulatorThrottleThreshold (
842   IN       UINT32              Data,
843   IN       AMD_CONFIG_PARAMS   *StdHeader
844   )
845 {
846   UINT8                 CmpCap;
847   UINT32                OrMask;
848   UINT32                CUStatus;
849   UINT32                LocalPciRegister;
850   PCI_ADDR              PciAddress;
851
852   if (OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader)) {
853     PciAddress.Address.Function = FUNC_5;
854     PciAddress.Address.Register = 0x84;
855     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
856     CmpCap = (UINT8) (LocalPciRegister & 0x000000FF);
857     CmpCap++;
858
859     // check if the part is fused with 1 core enabled per compute unit
860     PciAddress.Address.Register = 0x80;
861     LibAmdPciRead (AccessWidth32, PciAddress, &CUStatus, StdHeader);
862     if ((CUStatus & 0x000F0000) != 0) {
863       CmpCap = CmpCap >> 1;
864     }
865
866     PciAddress.Address.Register = 0xBC;
867     LibAmdPciRead (AccessWidth32, PciAddress, &OrMask, StdHeader);
868     OrMask = (UINT32) ((OrMask & 0x000FFFFF) * CmpCap);
869
870     PciAddress.Address.Register = 0xB4;
871     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
872     // The correct value is F5xBC[CmpUnitTdpAccThrottleThreshold] x ((F5x84[CmpCap] + 1) / 2).
873     LocalPciRegister = (LocalPciRegister & 0xFFF00000) | (OrMask & 0x000FFFFF);
874     LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
875   }
876 }
877
878 /*---------------------------------------------------------------------------------------*/
879 /**
880  * A Family Specific Workaround method, to sync internal node 1 SbiAddr setting.
881  *
882  * \@TableTypeFamSpecificInstances.
883  *
884  * @param[in]     Data       The table data value, for example to indicate which CPU and Platform types matched.
885  * @param[in]     StdHeader  Config params for library, services.
886  */
887 VOID
888 F15OrSyncInternalNode1SbiAddr (
889   IN       UINT32              Data,
890   IN       AMD_CONFIG_PARAMS   *StdHeader
891   )
892 {
893   UINT32       Socket;
894   UINT32       Module;
895   UINT32       DataOr;
896   UINT32       DataAnd;
897   UINT32       ModuleType;
898   PCI_ADDR     PciAddress;
899   AGESA_STATUS AgesaStatus;
900   UINT32       SyncToModule;
901   AP_MAIL_INFO ApMailboxInfo;
902   UINT32       LocalPciRegister;
903
904   ApMailboxInfo.Info = 0;
905
906   GetApMailbox (&ApMailboxInfo.Info, StdHeader);
907   ASSERT (ApMailboxInfo.Fields.Socket < MAX_SOCKETS);
908   ASSERT (ApMailboxInfo.Fields.Module < MAX_DIES);
909   Socket = ApMailboxInfo.Fields.Socket;
910   Module = ApMailboxInfo.Fields.Module;
911   ModuleType = ApMailboxInfo.Fields.ModuleType;
912
913   // sync is just needed on multinode cpu
914   if (ModuleType != 0) {
915     // check if it is internal node 0 of every socket
916     if (Module == 0) {
917       if (GetPciAddress (StdHeader, Socket, Module, &PciAddress, &AgesaStatus)) {
918         PciAddress.Address.Function = FUNC_3;
919         PciAddress.Address.Register = 0x1E4;
920         // read internal node 0 F3x1E4[6:4]
921         LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
922         DataOr = LocalPciRegister & ((UINT32) (7 << 4));
923         DataAnd = ~(UINT32) (7 << 4);
924         for (SyncToModule = 1; SyncToModule < GetPlatformNumberOfModules (); SyncToModule++) {
925           if (GetPciAddress (StdHeader, Socket, SyncToModule, &PciAddress, &AgesaStatus)) {
926             PciAddress.Address.Function = FUNC_3;
927             PciAddress.Address.Register = 0x1E4;
928             // sync the other internal node F3x1E4[6:4]
929             LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
930             LocalPciRegister &= DataAnd;
931             LocalPciRegister |= DataOr;
932             LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
933           }
934         }
935       }
936     }
937   }
938 }
939