AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / Feat / CHINTLV / mfchi.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mfchi.c
6  *
7  * Feature Channel interleaving support
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/Feat/Chintlv)
12  * @e \$Revision: 44324 $ @e \$Date: 2010-12-22 02:16:51 -0700 (Wed, 22 Dec 2010) $
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
47 /*
48  *----------------------------------------------------------------------------
49  *                                MODULES USED
50  *
51  *----------------------------------------------------------------------------
52  */
53
54
55
56 #include "AGESA.h"
57 #include "mm.h"
58 #include "mn.h"
59 #include "mfchi.h"
60 #include "Ids.h"
61 #include "GeneralServices.h"
62 #include "Filecode.h"
63 CODE_GROUP (G2_PEI)
64 RDATA_GROUP (G2_PEI)
65
66 #define FILECODE PROC_MEM_FEAT_CHINTLV_MFCHI_FILECODE
67 /*----------------------------------------------------------------------------
68  *                          DEFINITIONS AND MACROS
69  *
70  *----------------------------------------------------------------------------
71  */
72 #define _4GB_ (0x10000 >> 10)
73
74 /*----------------------------------------------------------------------------
75  *                           TYPEDEFS AND STRUCTURES
76  *
77  *----------------------------------------------------------------------------
78  */
79
80 /*----------------------------------------------------------------------------
81  *                        PROTOTYPES OF LOCAL FUNCTIONS
82  *
83  *----------------------------------------------------------------------------
84  */
85
86 /*----------------------------------------------------------------------------
87  *                            EXPORTED FUNCTIONS
88  *
89  *----------------------------------------------------------------------------
90  */
91
92 /* -----------------------------------------------------------------------------*/
93 /**
94  *
95  *   MemFInterleaveChannels:
96  *
97  *  Applies DIMM channel interleaving if enabled, if not ganged mode, and
98  *  there are valid dimms in both channels.  Called once per Node.
99  *
100  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
101  *
102  *     @return          TRUE -  This feature is enabled.
103  *     @return          FALSE - This feature is not enabled.
104  */
105
106 BOOLEAN
107 MemFInterleaveChannels (
108   IN OUT   MEM_NB_BLOCK *NBPtr
109   )
110 {
111   UINT32 DramBase;
112   UINT32 DctSelBase;
113   UINT32 HoleSize;
114   UINT32 HoleBase;
115   UINT32 HoleOffset;
116   UINT32 Dct0Size;
117   UINT32 Dct1Size;
118   UINT32 SmallerDct;
119   UINT8 DctSelIntLvAddr;
120   UINT8 DctSelHi;
121   UINT8 DctSelHiRngEn;
122   UINT32 HoleValid;
123
124   MEM_PARAMETER_STRUCT *RefPtr;
125   DIE_STRUCT *MCTPtr;
126
127   ASSERT (NBPtr != NULL);
128
129   RefPtr = NBPtr->RefPtr;
130
131   DctSelIntLvAddr = NBPtr->DefDctSelIntLvAddr;
132   if (RefPtr->EnableChannelIntlv) {
133     HoleSize = 0;
134     HoleBase = 0;
135     if (RefPtr->GStatus[GsbSoftHole] || RefPtr->GStatus[GsbHWHole]) {
136       // HoleBase scaled from [47:16] to [47:26]
137       HoleBase = RefPtr->HoleBase >> 10;
138       HoleSize = _4GB_ - HoleBase;
139     }
140
141     MCTPtr = NBPtr->MCTPtr;
142
143     HoleValid = NBPtr->GetBitField (NBPtr, BFDramHoleValid);
144     if ((!MCTPtr->GangedMode) &&
145         (MCTPtr->DctData[0].Timings.DctMemSize != 0) &&
146         (MCTPtr->DctData[1].Timings.DctMemSize != 0)) {
147       // DramBase scaled [47:16] to [47:26]
148       DramBase = MCTPtr->NodeSysBase >> 10;
149       // Scale NodeSysLimit [47:16] to [47:26]
150       Dct1Size = (MCTPtr->NodeSysLimit + 1) >> 10;
151       Dct0Size = NBPtr->GetBitField (NBPtr, BFDctSelBaseOffset);
152       if ((Dct0Size >= _4GB_) && (DramBase < HoleBase)) {
153         Dct0Size -= HoleSize;
154       }
155       if ((Dct1Size >= _4GB_) && (DramBase < HoleBase)) {
156         Dct1Size -= HoleSize;
157       }
158       Dct1Size -= Dct0Size;
159       Dct0Size -= DramBase;
160
161        // Select the bigger size DCT to put in DctSelHi
162       DctSelHiRngEn = 1;
163       DctSelHi = 0;
164       SmallerDct = Dct1Size;
165       if (Dct1Size == Dct0Size) {
166         SmallerDct = 0;
167         DctSelHiRngEn = 0;
168       } else if (Dct1Size > Dct0Size) {
169         SmallerDct = Dct0Size;
170         DctSelHi = 1;
171       }
172
173       if (SmallerDct != 0) {
174         DctSelBase = (SmallerDct * 2) + DramBase;
175       } else {
176         DctSelBase = 0;
177       }
178       if ((DctSelBase >= HoleBase) && (DramBase < HoleBase)) {
179         DctSelBase += HoleSize;
180       }
181       IDS_OPTION_HOOK (IDS_CHANNEL_INTERLEAVE, &DctSelIntLvAddr, &(NBPtr->MemPtr->StdHeader));
182       NBPtr->SetBitField (NBPtr, BFDctSelBaseAddr, DctSelBase >> 1);
183       NBPtr->SetBitField (NBPtr, BFDctSelHiRngEn, DctSelHiRngEn);
184       NBPtr->SetBitField (NBPtr, BFDctSelHi, DctSelHi);
185       NBPtr->SetBitField (NBPtr, BFDctSelIntLvAddr, DctSelIntLvAddr);
186       NBPtr->SetBitField (NBPtr, BFDctSelIntLvEn, 1);
187
188        // DctSelBaseOffset = DctSelBaseAddr - Interleaved region
189       NBPtr->SetBitField (NBPtr, BFDctSelBaseOffset, DctSelBase - SmallerDct);
190
191        // Adjust DramHoleOffset
192       if (HoleValid != 0) {
193         HoleOffset = DramBase;
194         if ((DctSelBase < HoleBase) && (DctSelBase != 0)) {
195           HoleOffset += (DctSelBase - DramBase) >> 1;
196         }
197         HoleOffset += HoleSize;
198         NBPtr->SetBitField (NBPtr, BFDramHoleOffset, HoleOffset << 3);
199       }
200     } else {
201       //
202       // Channel Interleaving is requested but cannot be enabled
203       //
204       PutEventLog (AGESA_WARNING, MEM_WARNING_CHANNEL_INTERLEAVING_NOT_ENABLED, NBPtr->Node, 0, 0, 0, &NBPtr->MemPtr->StdHeader);
205       SetMemError (AGESA_WARNING, MCTPtr);
206     }
207
208     return TRUE;
209   } else {
210     return FALSE;
211   }
212 }