AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Feature / cpuL3Features.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD CPU L3 Features Initialization functions.
6  *
7  * Contains code for initializing L3 features.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  CPU
12  * @e \$Revision: 60552 $   @e \$Date: 2011-10-17 18:50:55 -0600 (Mon, 17 Oct 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 /*
48  *----------------------------------------------------------------------------
49  *                                MODULES USED
50  *
51  *----------------------------------------------------------------------------
52  */
53
54 #include "AGESA.h"
55 #include "amdlib.h"
56 #include "Ids.h"
57 #include "cpuRegisters.h"
58 #include "cpuLateInit.h"
59 #include "cpuFamilyTranslation.h"
60 #include "cpuServices.h"
61 #include "GeneralServices.h"
62 #include "cpuFeatures.h"
63 #include "cpuL3Features.h"
64 #include "Filecode.h"
65 CODE_GROUP (G2_PEI)
66 RDATA_GROUP (G2_PEI)
67
68 #define FILECODE PROC_CPU_FEATURE_CPUL3FEATURES_FILECODE
69 /*----------------------------------------------------------------------------
70  *                          DEFINITIONS AND MACROS
71  *
72  *----------------------------------------------------------------------------
73  */
74
75 /*----------------------------------------------------------------------------
76  *                           TYPEDEFS AND STRUCTURES
77  *
78  *----------------------------------------------------------------------------
79  */
80
81 /*----------------------------------------------------------------------------
82  *                        PROTOTYPES OF LOCAL FUNCTIONS
83  *
84  *----------------------------------------------------------------------------
85  */
86
87 /*----------------------------------------------------------------------------
88  *                            EXPORTED FUNCTIONS
89  *
90  *----------------------------------------------------------------------------
91  */
92 extern CPU_FAMILY_SUPPORT_TABLE L3FeatureFamilyServiceTable;
93
94 /*---------------------------------------------------------------------------------------*/
95 /**
96  *  Should L3 features be enabled
97  *
98  * @param[in]    PlatformConfig     Contains the runtime modifiable feature input data.
99  * @param[in]    StdHeader          Config Handle for library, services.
100  *
101  * @retval       TRUE               L3 Features are supported
102  * @retval       FALSE              L3 Features are not supported
103  *
104  */
105 BOOLEAN
106 STATIC
107 IsL3FeatureEnabled (
108   IN       PLATFORM_CONFIGURATION *PlatformConfig,
109   IN       AMD_CONFIG_PARAMS      *StdHeader
110   )
111 {
112   BOOLEAN          IsEnabled;
113   UINT32           Socket;
114   L3_FEATURE_FAMILY_SERVICES *FamilyServices;
115
116   IsEnabled = FALSE;
117   if (PlatformConfig->PlatformProfile.UseHtAssist ||
118       PlatformConfig->PlatformProfile.UseAtmMode) {
119     IsEnabled = TRUE;
120     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
121       if (IsProcessorPresent (Socket, StdHeader)) {
122         GetFeatureServicesOfSocket (&L3FeatureFamilyServiceTable, Socket, (CONST VOID **)&FamilyServices, StdHeader);
123         if ((FamilyServices == NULL) || !FamilyServices->IsL3FeatureSupported (FamilyServices, Socket, StdHeader, PlatformConfig)) {
124           IsEnabled = FALSE;
125           break;
126         }
127       }
128     }
129   }
130   return IsEnabled;
131 }
132
133 /*---------------------------------------------------------------------------------------*/
134 /**
135  *  Enable L3 dependent features.
136  *
137  * L3 features initialization requires the following series of steps.
138  *  1. Disable L3 and DRAM scrubbers on all nodes
139  *  2. Wait 40us for outstanding scrub results to complete
140  *  3. Disable all cache activity in the system
141  *  4. Issue WBINVD on all active cores
142  *  5. Initialize Probe Filter, if supported
143  *  6. Initialize ATM Mode, if supported
144  *  7. Enable all cache activity in the system
145  *  8. Restore L3 and DRAM scrubber register values
146  *
147  * @param[in]    EntryPoint         Timepoint designator.
148  * @param[in]    PlatformConfig     Contains the runtime modifiable feature input data.
149  * @param[in]    StdHeader          Config Handle for library, services.
150  *
151  * @retval       AGESA_SUCCESS      Always succeeds.
152  *
153  */
154 AGESA_STATUS
155 STATIC
156 InitializeL3Feature (
157   IN       UINT64                 EntryPoint,
158   IN       PLATFORM_CONFIGURATION *PlatformConfig,
159   IN       AMD_CONFIG_PARAMS      *StdHeader
160   )
161 {
162   UINT32                      CpuCount;
163   UINT32                      Socket;
164   BOOLEAN                     HtAssistEnabled;
165   BOOLEAN                     AtmModeEnabled;
166   AGESA_STATUS                AgesaStatus;
167   AP_MAILBOXES                ApMailboxes;
168   AP_EXE_PARAMS               ApParams;
169   UINT32                      Scrubbers[MAX_SOCKETS_SUPPORTED][L3_SCRUBBER_CONTEXT_ARRAY_SIZE];
170   L3_FEATURE_FAMILY_SERVICES  *FamilyServices[MAX_SOCKETS_SUPPORTED];
171
172   AgesaStatus     = AGESA_SUCCESS;
173   HtAssistEnabled = TRUE;
174   AtmModeEnabled  = TRUE;
175
176   IDS_HDT_CONSOLE (CPU_TRACE, "    Enabling L3 dependent features\n");
177
178   // There are many family service call outs.  Initialize the family service array while
179   // cache is still enabled.
180   for (Socket = 0; Socket < MAX_SOCKETS_SUPPORTED; Socket++) {
181     if (IsProcessorPresent (Socket, StdHeader)) {
182       GetFeatureServicesOfSocket (&L3FeatureFamilyServiceTable, Socket, (CONST VOID **)&FamilyServices[Socket], StdHeader);
183     } else {
184       FamilyServices[Socket] = NULL;
185     }
186   }
187
188   if (EntryPoint == CPU_FEAT_AFTER_POST_MTRR_SYNC) {
189     // Check for optimal settings
190     GetApMailbox (&ApMailboxes.ApMailInfo.Info, StdHeader);
191     CpuCount = GetNumberOfProcessors (StdHeader);
192     if (((CpuCount == 1) && (ApMailboxes.ApMailInfo.Fields.ModuleType == 1)) ||
193         ((CpuCount == 2) && (ApMailboxes.ApMailInfo.Fields.ModuleType == 0))) {
194       for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
195         // Only check for non-optimal HT Assist setting is if's supported.
196         if ((FamilyServices[Socket] != NULL) &&
197             (FamilyServices[Socket]->IsHtAssistSupported (FamilyServices[Socket], PlatformConfig, StdHeader))) {
198           if (FamilyServices[Socket]->IsNonOptimalConfig (FamilyServices[Socket], Socket, StdHeader)) {
199             // Non-optimal settings.  Log an event.
200             AgesaStatus = AGESA_WARNING;
201             PutEventLog (AgesaStatus, CPU_WARNING_NONOPTIMAL_HT_ASSIST_CFG, 0, 0, 0, 0, StdHeader);
202             break;
203           }
204         }
205       }
206     }
207   } else {
208     // Disable the scrubbers.
209     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
210       if (FamilyServices[Socket] != NULL) {
211         FamilyServices[Socket]->GetL3ScrubCtrl (FamilyServices[Socket], Socket, &Scrubbers[Socket][0], StdHeader);
212
213         // If any node in the system does not support Probe Filter, disable it on the system
214         if (!FamilyServices[Socket]->IsHtAssistSupported (FamilyServices[Socket], PlatformConfig, StdHeader)) {
215           HtAssistEnabled = FALSE;
216         }
217         // If any node in the system does not support ATM mode, disable it on the system
218         if (!FamilyServices[Socket]->IsAtmModeSupported (FamilyServices[Socket], PlatformConfig, StdHeader)) {
219           AtmModeEnabled = FALSE;
220         }
221       }
222     }
223
224     // Wait for 40us
225     WaitMicroseconds ((UINT32) 40, StdHeader);
226
227     // Run DisableAllCaches on AP cores.
228     ApParams.StdHeader = *StdHeader;
229     ApParams.FunctionNumber = AP_LATE_TASK_DISABLE_CACHE;
230     ApParams.RelatedDataBlock = (VOID *) &HtAssistEnabled;
231     ApParams.RelatedBlockLength = sizeof (BOOLEAN);
232     RunLateApTaskOnAllAPs (&ApParams, StdHeader);
233
234     // Run DisableAllCaches on core 0.
235     DisableAllCaches (&ApParams);
236
237     // Family hook before initialization.
238     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
239       if (FamilyServices[Socket] != NULL) {
240         FamilyServices[Socket]->HookBeforeInit (FamilyServices[Socket], Socket, StdHeader);
241       }
242     }
243
244     // Activate Probe Filter & ATM mode.
245     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
246       if (FamilyServices[Socket] != NULL) {
247         if (HtAssistEnabled) {
248           FamilyServices[Socket]->HtAssistInit (FamilyServices[Socket], Socket, StdHeader);
249         }
250         if (AtmModeEnabled) {
251           FamilyServices[Socket]->AtmModeInit (FamilyServices[Socket], Socket, StdHeader);
252         }
253       }
254     }
255
256     // Family hook after initialization.
257     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
258       if (FamilyServices[Socket] != NULL) {
259         FamilyServices[Socket]->HookAfterInit (FamilyServices[Socket], Socket, StdHeader);
260       }
261     }
262
263     // Run EnableAllCaches on core 0.
264     EnableAllCaches (&ApParams);
265
266     // Run EnableAllCaches on every core.
267     ApParams.FunctionNumber = AP_LATE_TASK_ENABLE_CACHE;
268     RunLateApTaskOnAllAPs (&ApParams, StdHeader);
269
270     // Restore the scrubbers.
271     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
272       if (FamilyServices[Socket] != NULL) {
273         FamilyServices[Socket]->SetL3ScrubCtrl (FamilyServices[Socket], Socket, &Scrubbers[Socket][0], StdHeader);
274       }
275     }
276   }
277
278   return AgesaStatus;
279 }
280
281 /*---------------------------------------------------------------------------------------*/
282 /**
283  *
284  * Disable all the caches on current core.
285  *
286  * @param[in] ApExeParams   Handle to config for library and services.
287  *
288  * @retval    AGESA_SUCCESS  Always succeeds.
289  *
290  */
291 AGESA_STATUS
292 DisableAllCaches (
293   IN       AP_EXE_PARAMS *ApExeParams
294   )
295 {
296   UINT32   CR0Data;
297   L3_FEATURE_FAMILY_SERVICES  *FamilyServices;
298
299   // Disable cache through CR0.
300   LibAmdReadCpuReg (0, &CR0Data);
301   CR0Data |= (0x60000000);
302   LibAmdWriteCpuReg (0, CR0Data);
303
304   // Execute wbinvd
305   LibAmdWriteBackInvalidateCache ();
306
307   GetFeatureServicesOfCurrentCore (&L3FeatureFamilyServiceTable, (CONST VOID **)&FamilyServices, &ApExeParams->StdHeader);
308
309   FamilyServices->HookDisableCache (FamilyServices, *(BOOLEAN *) ApExeParams->RelatedDataBlock, &ApExeParams->StdHeader);
310
311   return AGESA_SUCCESS;
312 }
313
314 /*---------------------------------------------------------------------------------------*/
315 /**
316  *
317  * Enable all the caches on current core.
318  *
319  * @param[in] ApExeParams    Handle to config for library and services.
320  *
321  * @retval    AGESA_SUCCESS  Always succeeds.
322  *
323  */
324 AGESA_STATUS
325 EnableAllCaches (
326   IN       AP_EXE_PARAMS *ApExeParams
327   )
328 {
329   UINT32   CR0Data;
330   L3_FEATURE_FAMILY_SERVICES  *FamilyServices;
331
332   // Enable cache through CR0.
333   LibAmdReadCpuReg (0, &CR0Data);
334   CR0Data &= ~(0x60000000);
335   LibAmdWriteCpuReg (0, CR0Data);
336
337   GetFeatureServicesOfCurrentCore (&L3FeatureFamilyServiceTable, (CONST VOID **)&FamilyServices, &ApExeParams->StdHeader);
338
339   FamilyServices->HookEnableCache (FamilyServices, &ApExeParams->StdHeader);
340
341   return AGESA_SUCCESS;
342 }
343
344 CONST CPU_FEATURE_DESCRIPTOR ROMDATA CpuL3Features =
345 {
346   L3Features,
347   (CPU_FEAT_AFTER_POST_MTRR_SYNC | CPU_FEAT_INIT_MID_END | CPU_FEAT_S3_LATE_RESTORE_END),
348   IsL3FeatureEnabled,
349   InitializeL3Feature
350 };