AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Family / 0x15 / OR / F15OrPmNbCofVidInit.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Family_15 Orochi NB COF VID Initialization
6  *
7  * Performs the "BIOS Northbridge COF and VID Configuration" 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/OR
13  * @e \$Revision: 51891 $   @e \$Date: 2011-04-28 12:39:55 -0600 (Thu, 28 Apr 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 "cpuRegisters.h"
54 #include "cpuF15PowerMgmt.h"
55 #include "cpuF15OrPowerMgmt.h"
56 #include "cpuApicUtilities.h"
57 #include "OptionMultiSocket.h"
58 #include "cpuServices.h"
59 #include "GeneralServices.h"
60 #include "cpuFamilyTranslation.h"
61 #include "F15OrPmNbCofVidInit.h"
62 #include "Filecode.h"
63 CODE_GROUP (G3_DXE)
64 RDATA_GROUP (G3_DXE)
65
66 #define FILECODE PROC_CPU_FAMILY_0X15_OR_F15ORPMNBCOFVIDINIT_FILECODE
67
68
69 /*----------------------------------------------------------------------------------------
70  *                   D E F I N I T I O N S    A N D    M A C R O S
71  *----------------------------------------------------------------------------------------
72  */
73
74 /*----------------------------------------------------------------------------------------
75  *                  T Y P E D E F S     A N D     S T R U C T U R E S
76  *----------------------------------------------------------------------------------------
77  */
78
79 /*----------------------------------------------------------------------------------------
80  *           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
81  *----------------------------------------------------------------------------------------
82  */
83 VOID
84 STATIC
85 F15OrPmNbCofVidInitOnCore (
86   IN       AMD_CONFIG_PARAMS *StdHeader
87   );
88
89 /*----------------------------------------------------------------------------------------
90  *                          E X P O R T E D    F U N C T I O N S
91  *----------------------------------------------------------------------------------------
92  */
93 extern OPTION_MULTISOCKET_CONFIGURATION OptionMultiSocketConfiguration;
94
95 /*---------------------------------------------------------------------------------------*/
96 /**
97  * Family 15h Orochi core 0 entry point for performing the "Mixed Northbridge Frequency
98  * Configuration Sequence"
99  *
100  * BIOS must match F5x1[6C:60][NbFid, NbDid, NbPstateEn] between all
101  * processors of a multi-socket system.  The lowest setting from all
102  * processors is used as the common F5x1[6C:60][NbFid, NbDid].  All
103  * processors must have the same number of NB P-states.
104  *
105  * For each node in the system {
106  *   For (i = 0; i <= F5x170[NbPstateMaxVal]; i++) {
107  *     NewNbFreq = the lowest NBCOF from all processors for NB P-state i
108  *     NewNbFid = F5x1[6C:60][NbFid] that corresponds to NewNbFreq
109  *     NewNbDid = F5x1[6C:60][NbDid] that corresponds to NewNbFreq
110  *     Write NewNbFid and NewNbDid to F5x1[6C:60][NbFid, NbDid] indexed
111  *       by NB P-state i
112  *   }
113  *   If (F5x170[NbPstateMaxVal] == 0) {
114  *     Save F5x170 and F5x1[6C:60] indexed by NB P-state 1
115  *     Copy F5x1[6C:60] indexed by NB P-state 0 to F5x1[6C:60] indexed by NB P-state 1
116  *     Write 1 to F5x170[NbPstateMaxVal, NbPstateLo]
117  *     Write 0 to F5x170[SwNbPstateLoDis, NbPstateDisOnP0, NbPstateThreshold]
118  *     Wait for F5x174[CurNbPstate] = F5x170[NbPstateLo] and F5x174[CurNbFid, CurNb-
119  *       Did]=[NbFid, NbDid] from F5x1[6C:60] indexed by F5x170[NbPstateLo]
120  *     Restore F5x170 and F5x1[6C:60] indexed by NB P-state 1
121  *     Wait for F5x174[CurNbPstate] = F5x170[NbPstateHi]
122  *   }
123  * }
124  *
125  * @param[in]  FamilySpecificServices  The current Family Specific Services.
126  * @param[in]  CpuEarlyParamsPtr       Service related parameters (unused).
127  * @param[in]  StdHeader               Config handle for library and services.
128  *
129  */
130 VOID
131 F15OrPmNbCofVidInit (
132   IN       CPU_SPECIFIC_SERVICES *FamilySpecificServices,
133   IN       AMD_CPU_EARLY_PARAMS  *CpuEarlyParamsPtr,
134   IN       AMD_CONFIG_PARAMS     *StdHeader
135   )
136 {
137   UINT32        Socket;
138   UINT32        Module;
139   UINT32        Core;
140   UINT32        i;
141   UINT32        NbFreq;
142   UINT32        NbDiv;
143   UINT32        LocalPciRegister;
144   UINT32        AndMask;
145   UINT32        OrMask;
146   UINT32        Ignored;
147   UINT32        NbPsCtrl;
148   UINT32        TaskedCore;
149   BOOLEAN       PstateSettingsChanged;
150   BOOLEAN       PstatesMatch;
151   BOOLEAN       PstateEnabledAll;
152   AP_TASK       TaskPtr;
153   PCI_ADDR      PciAddress;
154   AGESA_STATUS  IgnoredSts;
155
156   // Get the local node ID
157   IdentifyCore (StdHeader, &Socket, &Module, &Core, &IgnoredSts);
158
159   ASSERT (Core == 0);
160
161   PstateSettingsChanged = FALSE;
162   GetPciAddress (StdHeader, Socket, Module, &PciAddress, &IgnoredSts);
163   PciAddress.Address.Function = FUNC_5;
164   PciAddress.Address.Register = NB_PSTATE_CTRL;
165   LibAmdPciRead (AccessWidth32, PciAddress, &NbPsCtrl, StdHeader);
166   for (i = 0; i <= ((NB_PSTATE_CTRL_REGISTER *) &NbPsCtrl)->NbPstateMaxVal; i++) {
167     if (OptionMultiSocketConfiguration.GetSystemNbPstateSettings (i, &CpuEarlyParamsPtr->PlatformConfig, &NbFreq, &NbDiv, &PstatesMatch, &PstateEnabledAll, StdHeader)) {
168       if (PstateEnabledAll) {
169         // Valid system-wide NB P-state
170         if (!PstatesMatch) {
171           // Configure NbPstate[i] to match the slowest
172           PciAddress.Address.Register = (NB_PSTATE_0 + (4 * i));
173           LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
174           OrMask = 0x00000000;
175           ((NB_PSTATE_REGISTER *) &OrMask)->NbFid = ((NbFreq / 200) - 4);
176           ((NB_PSTATE_REGISTER *) &OrMask)->NbDid = (UINT32) LibAmdBitScanForward (NbDiv);
177           if ((((NB_PSTATE_REGISTER *) &OrMask)->NbFid != ((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbFid) ||
178               (((NB_PSTATE_REGISTER *) &OrMask)->NbDid != ((NB_PSTATE_REGISTER *) &LocalPciRegister)->NbDid)) {
179             AndMask = 0xFFFFFFFF;
180             ((NB_PSTATE_REGISTER *) &AndMask)->NbFid = 0;
181             ((NB_PSTATE_REGISTER *) &AndMask)->NbDid = 0;
182             OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader);
183             PstateSettingsChanged = TRUE;
184           }
185         }
186       } else {
187         // At least one processor in the system does not have NbPstate[i]
188         PciAddress.Address.Register = NB_PSTATE_CTRL;
189         AndMask = 0xFFFFFFFF;
190         ((NB_PSTATE_CTRL_REGISTER *) &AndMask)->NbPstateMaxVal = 0;
191         OrMask = 0;
192         if (i != 0) {
193           ((NB_PSTATE_CTRL_REGISTER *) &OrMask)->NbPstateMaxVal = (i - 1);
194         }
195         // Modify NbPstateMaxVal to reflect the system value
196         OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader);
197
198         // Disable this NB P-state
199         PciAddress.Address.Register = (NB_PSTATE_0 + (4 * i));
200         AndMask = 0xFFFFFFFF;
201         ((NB_PSTATE_REGISTER *) &AndMask)->NbPstateEn = 0;
202         OrMask = 0;
203         OptionMultiSocketConfiguration.ModifyCurrSocketPci (&PciAddress, AndMask, OrMask, StdHeader);
204
205         // Log error for the invalid configuration
206         PutEventLog (AGESA_ERROR,
207                      CPU_ERROR_PM_NB_PSTATE_MISMATCH,
208                      Socket, i, 0, 0, StdHeader);
209         break;
210       }
211     }
212   }
213
214   if (PstateSettingsChanged) {
215     PciAddress.Address.Register = NB_PSTATE_CTRL;
216     LibAmdPciRead (AccessWidth32, PciAddress, &NbPsCtrl, StdHeader);
217     if (((NB_PSTATE_CTRL_REGISTER *) &NbPsCtrl)->NbPstateMaxVal == 0) {
218       // Launch one core per node.
219       TaskPtr.FuncAddress.PfApTask = F15OrPmNbCofVidInitOnCore;
220       TaskPtr.DataTransfer.DataSizeInDwords = 0;
221       TaskPtr.ExeFlags = WAIT_FOR_CORE;
222       for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
223         if (GetGivenModuleCoreRange (Socket, Module, &TaskedCore, &Ignored, StdHeader)) {
224           if (TaskedCore != 0) {
225             ApUtilRunCodeOnSocketCore ((UINT8) Socket, (UINT8) TaskedCore, &TaskPtr, StdHeader);
226           }
227         }
228       }
229       ApUtilTaskOnExecutingCore (&TaskPtr, StdHeader, (VOID *) CpuEarlyParamsPtr);
230     }
231   }
232 }
233
234 /*---------------------------------------------------------------------------------------
235  *                          L O C A L    F U N C T I O N S
236  *---------------------------------------------------------------------------------------
237  */
238
239 /*---------------------------------------------------------------------------------------*/
240 /**
241  * Support routine for F15OrPmNbCofVidInit to perform the actual NB P-state transition
242  * to the leveled NB P-state settings on one core of each die in a family 15h socket.
243  *
244  * The following steps are performed:
245  *   1. Save F5x170 and F5x1[6C:60] indexed by NB P-state 1
246  *   2. Copy F5x1[6C:60] indexed by NB P-state 0 to F5x1[6C:60] indexed by NB P-state 1
247  *   3, Write 1 to F5x170[NbPstateMaxVal, NbPstateLo]
248  *   4. Write 0 to F5x170[SwNbPstateLoDis, NbPstateDisOnP0, NbPstateThreshold]
249  *   5. Wait for F5x174[CurNbPstate] = F5x170[NbPstateLo] and F5x174[CurNbFid, CurNb-
250  *      Did]=[NbFid, NbDid] from F5x1[6C:60] indexed by F5x170[NbPstateLo]
251  *   6. Restore F5x170 and F5x1[6C:60] indexed by NB P-state 1
252  *   7. Wait for F5x174[CurNbPstate] = F5x170[NbPstateHi]
253  *
254  * @param[in]  StdHeader          Config handle for library and services.
255  *
256  */
257 VOID
258 STATIC
259 F15OrPmNbCofVidInitOnCore (
260   IN       AMD_CONFIG_PARAMS *StdHeader
261   )
262 {
263   UINT32        NbPsCtrl;
264   UINT32        NbPs0;
265   UINT32        NbPs1;
266   UINT32        LocalPciRegister;
267   PCI_ADDR      PciAddress;
268
269   OptionMultiSocketConfiguration.GetCurrPciAddr (&PciAddress, StdHeader);
270
271   // Save F5x170 and F5x164
272   PciAddress.Address.Function = FUNC_5;
273   PciAddress.Address.Register = NB_PSTATE_CTRL;
274   LibAmdPciRead (AccessWidth32, PciAddress, &NbPsCtrl, StdHeader);
275
276   PciAddress.Address.Register = NB_PSTATE_0;
277   LibAmdPciRead (AccessWidth32, PciAddress, &NbPs0, StdHeader);
278   PciAddress.Address.Register = NB_PSTATE_1;
279   LibAmdPciRead (AccessWidth32, PciAddress, &NbPs1, StdHeader);
280
281   // Copy F5x160 to F5x164
282   LibAmdPciWrite (AccessWidth32, PciAddress, &NbPs0, StdHeader);
283
284   // Write 1 to F5x170[NbPstateMaxVal, NbPstateLo]
285   PciAddress.Address.Register = NB_PSTATE_CTRL;
286   LocalPciRegister = NbPsCtrl;
287   ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateMaxVal = 1;
288   ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateLo = 1;
289   LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
290
291   // Write 0 to F5x170[SwNbPstateLoDis, NbPstateDisOnP0, NbPstateThreshold]
292   ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->SwNbPstateLoDis = 0;
293   ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateDisOnP0 = 0;
294   ((NB_PSTATE_CTRL_REGISTER *) &LocalPciRegister)->NbPstateThreshold = 0;
295   LibAmdPciWrite (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
296
297   // Wait for F5x174[CurNbPstate] = F5x170[NbPstateLo] (written to 1 above) and
298   // F5x174[CurNbFid, CurNbDid] = F5x164[NbFid, NbDid]
299   PciAddress.Address.Register = NB_PSTATE_STATUS;
300   do {
301     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
302   } while ((((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbPstate != 1) &&
303            (((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbFid != ((NB_PSTATE_REGISTER *) &NbPs0)->NbFid) &&
304            (((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbDid != ((NB_PSTATE_REGISTER *) &NbPs0)->NbDid));
305
306   // Restore F5x170 and F5x164
307   PciAddress.Address.Register = NB_PSTATE_CTRL;
308   LibAmdPciWrite (AccessWidth32, PciAddress, &NbPsCtrl, StdHeader);
309   PciAddress.Address.Register = NB_PSTATE_1;
310   LibAmdPciWrite (AccessWidth32, PciAddress, &NbPs1, StdHeader);
311
312   // Wait for F5x174[CurNbPstate] = F5x170[NbPstateHi]
313   PciAddress.Address.Register = NB_PSTATE_STATUS;
314   do {
315     LibAmdPciRead (AccessWidth32, PciAddress, &LocalPciRegister, StdHeader);
316   } while (((NB_PSTATE_STS_REGISTER *) &LocalPciRegister)->CurNbPstate != ((NB_PSTATE_CTRL_REGISTER *) &NbPsCtrl)->NbPstateHi);
317 }