AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / Feat / ECC / mfecc.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mfecc.c
6  *
7  * Feature ECC initialization functions
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/Feat/ECC)
12  * @e \$Revision: 54543 $ @e \$Date: 2011-06-08 23:19:25 -0600 (Wed, 08 Jun 2011) $
13  *
14  **/
15 /*****************************************************************************
16   *
17  * Copyright (C) 2012 Advanced Micro Devices, Inc.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions are met:
22  *     * Redistributions of source code must retain the above copyright
23  *       notice, this list of conditions and the following disclaimer.
24  *     * Redistributions in binary form must reproduce the above copyright
25  *       notice, this list of conditions and the following disclaimer in the
26  *       documentation and/or other materials provided with the distribution.
27  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
28  *       its contributors may be used to endorse or promote products derived
29  *       from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41   *
42   * ***************************************************************************
43   *
44  */
45
46 #include "AGESA.h"
47 #include "amdlib.h"
48 #include "Ids.h"
49 #include "mm.h"
50 #include "mn.h"
51 #include "mfecc.h"
52 #include "Filecode.h"
53 #include "mfmemclr.h"
54 #include "GeneralServices.h"
55 CODE_GROUP (G2_PEI)
56 RDATA_GROUP (G2_PEI)
57
58 #define FILECODE PROC_MEM_FEAT_ECC_MFECC_FILECODE
59 /*----------------------------------------------------------------------------
60  *                          DEFINITIONS AND MACROS
61  *
62  *----------------------------------------------------------------------------
63  */
64
65 /*----------------------------------------------------------------------------
66  *                           TYPEDEFS AND STRUCTURES
67  *
68  *----------------------------------------------------------------------------
69  */
70
71 /*----------------------------------------------------------------------------
72  *                        PROTOTYPES OF LOCAL FUNCTIONS
73  *
74  *----------------------------------------------------------------------------
75  */
76
77 VOID
78 STATIC
79 InitECCOverriedeStruct (
80   IN OUT   MEM_NB_BLOCK *NBPtr,
81   IN OUT   ECC_OVERRIDE_STRUCT *pecc_override_struct
82   );
83
84 BOOLEAN
85 MemFCheckECC (
86   IN OUT   MEM_NB_BLOCK *NBPtr
87   );
88
89 /*
90  *-----------------------------------------------------------------------------
91  *                                EXPORTED FUNCTIONS
92  *
93  *-----------------------------------------------------------------------------
94  */
95
96 extern BUILD_OPT_CFG UserOptions;
97
98 /* -----------------------------------------------------------------------------*/
99 /**
100  *
101  *
102  *      This function checks to see if ECC can be enabled on all nodes
103  *
104  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
105  *
106  *     @return          TRUE -  This feature is enabled.
107  *     @return          FALSE - This feature is not enabled.
108  */
109
110 BOOLEAN
111 MemFCheckECC (
112   IN OUT   MEM_NB_BLOCK *NBPtr
113   )
114 {
115   DIE_STRUCT *MCTPtr;
116   MEM_SHARED_DATA *SharedPtr;
117   BOOLEAN ErrorRecovery;
118
119   ASSERT (NBPtr != NULL);
120
121   MCTPtr = NBPtr->MCTPtr;
122   SharedPtr = NBPtr->SharedPtr;
123
124   ErrorRecovery = TRUE;
125   IDS_OPTION_HOOK (IDS_MEM_ERROR_RECOVERY, &ErrorRecovery, &NBPtr->MemPtr->StdHeader);
126
127   if (MCTPtr->NodeMemSize != 0) {
128     if (SharedPtr->AllECC && MCTPtr->Status[SbEccDimms] && (ErrorRecovery || (MCTPtr->ErrCode < AGESA_ERROR))) {
129       // Clear all MCA reports before using scrubber
130       // to initialize ECC check bits
131       //
132       NBPtr->McaNbCtlReg = NBPtr->GetBitField (NBPtr, BFMcaNbCtlReg);
133       NBPtr->SetBitField (NBPtr, BFMcaNbCtlReg, 0);
134       NBPtr->SetBitField (NBPtr, BFSyncOnUcEccEn, 0);
135       // In unganged mode, set DctDctIntlv
136       if (!NBPtr->Ganged) {
137         NBPtr->SetBitField (NBPtr, BFDctDatIntLv, 1);
138       }
139       //
140       // Set Ecc Symbol Size
141       //
142       NBPtr->SetEccSymbolSize (NBPtr);
143       // If ECC can be enabled on this node,
144       // set the master ECCen bit (according to setup)
145       //
146       NBPtr->SetBitField (NBPtr, BFDramEccEn, 1);
147       // Do mem clear on current node
148       MemFMctMemClr_Init (NBPtr);
149       return TRUE;
150     } else {
151       if (SharedPtr->AllECC) {
152         SharedPtr->AllECC = FALSE;
153       }
154       // ECC requested but cannot be enabled
155       MCTPtr->Status[SbEccDimms] = FALSE;
156       MCTPtr->ErrStatus[EsbDramECCDis] = TRUE;
157       PutEventLog (AGESA_WARNING, MEM_WARNING_ECC_DIS, NBPtr->Node, 0, 0, 0, &NBPtr->MemPtr->StdHeader);
158       SetMemError (AGESA_WARNING, MCTPtr);
159     }
160   }
161   return FALSE;
162 }
163
164   /* -----------------------------------------------------------------------------*/
165 /**
166  *
167  *
168  *      This function initializes the ECC on all nodes
169  *
170  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
171  *
172  *     @return          TRUE -  This feature is enabled.
173  *     @return          FALSE - This feature is not enabled.
174  */
175
176 BOOLEAN
177 MemFInitECC (
178   IN OUT   MEM_NB_BLOCK *NBPtr
179   )
180 {
181   UINT8 Node;
182   UINT32 ScrubAddrRJ16;
183   DIE_STRUCT *MCTPtr;
184   MEM_SHARED_DATA *SharedPtr;
185   ECC_OVERRIDE_STRUCT  ecc_override_struct;
186   BOOLEAN Flag;
187
188   InitECCOverriedeStruct (NBPtr, &ecc_override_struct);
189   IDS_OPTION_HOOK (IDS_ECC, &ecc_override_struct, &(NBPtr->MemPtr->StdHeader));
190
191   ASSERT (NBPtr != NULL);
192
193   MCTPtr = NBPtr->MCTPtr;
194   Node = MCTPtr->NodeId;
195   SharedPtr = NBPtr->SharedPtr;
196   Flag = TRUE;
197
198   NBPtr->FamilySpecificHook[ScrubberErratum] (NBPtr, (VOID *) &Flag);
199
200   if ((MCTPtr->Status[SbEccDimms]) && (SharedPtr->AllECC)) {
201     // Check if the input dram scrub rate is supported or not
202     ASSERT (ecc_override_struct.CfgScrubDramRate <= 0x16);
203     if (ecc_override_struct.CfgScrubDramRate != 0) {
204     // Program scrub address,
205     // let the scrub Addr be the Base of this Node
206     // Only enable Dram scrubber when there is memory on current node
207     //
208       NBPtr->SetBitField (NBPtr, BFScrubReDirEn, 0);
209       ScrubAddrRJ16 = (NBPtr->GetBitField (NBPtr, BFDramBaseReg0 + Node) & 0xFFFF0000) >> 8;
210       ScrubAddrRJ16 |= NBPtr->GetBitField (NBPtr, BFDramBaseHiReg0 + Node) << 24;
211       NBPtr->SetBitField (NBPtr, BFScrubAddrLoReg, ScrubAddrRJ16 << 16);
212       NBPtr->SetBitField (NBPtr, BFScrubAddrHiReg, ScrubAddrRJ16 >> 16);
213       NBPtr->SetBitField (NBPtr, BFDramScrub, ecc_override_struct.CfgScrubDramRate);
214     }
215   }
216   // Scrub CTL for Dcache, L2, L3
217   // Check if the input L2 scrub rate is supported or not
218   ASSERT (ecc_override_struct.CfgScrubL2Rate <= 0x16);
219   NBPtr->SetBitField (NBPtr, BFL2Scrub, ecc_override_struct.CfgScrubL2Rate);
220   // Check if the input Dcache scrub rate is supported or not
221   ASSERT (ecc_override_struct.CfgScrubDcRate <= 0x16);
222   NBPtr->SetBitField (NBPtr, BFDcacheScrub, ecc_override_struct.CfgScrubDcRate);
223   // Do not enable L3 Scrub if F3xE8[L3Capable] is 0 or F3x188[BFReserved00B] is 1
224   if ((NBPtr->GetBitField (NBPtr, BFL3Capable) == 1) && (NBPtr->GetBitField (NBPtr, BFReserved00B) == 0)) {
225     // Check if input L3 scrub rate is supported or not
226     ASSERT (ecc_override_struct.CfgScrubL3Rate <= 0x16);
227     NBPtr->SetBitField (NBPtr, BFL3Scrub, ecc_override_struct.CfgScrubL3Rate);
228   }
229
230   // Check if Dcache scrubber or L2 scrubber is enabled
231   if ((ecc_override_struct.CfgScrubL2Rate != 0) || (ecc_override_struct.CfgScrubDcRate!= 0)) {
232     // If ClkDivisor is deeper than divide-by-16
233     if (NBPtr->GetBitField (NBPtr, BFC1ClkDivisor) > 4) {
234       // Set it to divide-by-16
235       NBPtr->SetBitField (NBPtr, BFC1ClkDivisor, 4);
236     }
237   }
238
239   NBPtr->SetBitField (NBPtr, BFScrubReDirEn, ecc_override_struct.CfgEccRedirection);
240   NBPtr->SetBitField (NBPtr, BFSyncOnUcEccEn, ecc_override_struct.CfgEccSyncFlood);
241   // Restore MCA reports after scrubber is done
242   // with initializing ECC check bits
243   NBPtr->SetBitField (NBPtr, BFMcaNbCtlReg, NBPtr->McaNbCtlReg);
244
245   Flag = FALSE;
246   NBPtr->FamilySpecificHook[ScrubberErratum] (NBPtr, (VOID *) &Flag);
247
248   return TRUE;
249 }
250
251 VOID
252 STATIC
253 InitECCOverriedeStruct (
254   IN OUT   MEM_NB_BLOCK *NBPtr,
255   IN OUT   ECC_OVERRIDE_STRUCT *pecc_override_struct
256   )
257 {
258   //
259   // If (D18F3x44[DramEccEn]==1) THEN 1 ELSE 0 ENDIF
260   //
261   if (NBPtr->GetBitField (NBPtr, BFDramEccEn) == 1) {
262     pecc_override_struct->CfgEccRedirection = 1;
263   } else {
264     pecc_override_struct->CfgEccRedirection = 0;
265   }
266
267   pecc_override_struct->CfgEccSyncFlood = UserOptions.CfgEccSyncFlood;
268   pecc_override_struct->CfgScrubDcRate = UserOptions.CfgScrubDcRate;
269
270   if (UserOptions.CfgScrubDramRate != 0xFF) {
271     pecc_override_struct->CfgScrubDramRate = UserOptions.CfgScrubDramRate;
272   } else {
273     if (NBPtr->MCTPtr->NodeMemSize <= 0x4000) {
274       pecc_override_struct->CfgScrubDramRate = 0x12; // 1 ~ 1 GB
275     } else if (NBPtr->MCTPtr->NodeMemSize <= 0x8000) {
276       pecc_override_struct->CfgScrubDramRate = 0x11; // 1 GB + 1 ~ 2 GB
277     } else if (NBPtr->MCTPtr->NodeMemSize <= 0x10000) {
278       pecc_override_struct->CfgScrubDramRate = 0x10; // 2 GB + 1 ~ 4 GB
279     } else if (NBPtr->MCTPtr->NodeMemSize <= 0x20000) {
280       pecc_override_struct->CfgScrubDramRate = 0x0F; // 4 GB + 1 ~ 8 GB
281     } else if (NBPtr->MCTPtr->NodeMemSize <= 0x40000) {
282       pecc_override_struct->CfgScrubDramRate = 0x0E; // 8 GB + 1 ~ 16 GB
283     } else {
284       pecc_override_struct->CfgScrubDramRate = 0x0D; //16 GB + 1 above
285     }
286   }
287
288   pecc_override_struct->CfgScrubL2Rate = UserOptions.CfgScrubL2Rate;
289   pecc_override_struct->CfgScrubL3Rate = UserOptions.CfgScrubL3Rate;
290 }