AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / Main / mmParallelTraining.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mmNodeInterleave.c
6  *
7  * Main Memory Feature implementation file for Node Interleaving
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/Main)
12  * @e \$Revision: 53955 $ @e \$Date: 2011-05-29 20:54:54 -0600 (Sun, 29 May 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 /*
47  *----------------------------------------------------------------------------
48  *                                MODULES USED
49  *
50  *----------------------------------------------------------------------------
51  */
52
53
54 #include "Porting.h"
55 #include "AGESA.h"
56 #include "amdlib.h"
57 #include "Ids.h"
58 #include "cpuRegisters.h"
59 #include "cpuServices.h"
60 #include "cpuApicUtilities.h"
61 #include "GeneralServices.h"
62 #include "OptionMemory.h"
63 #include "mm.h"
64 #include "mn.h"
65 #include "mt.h"
66 #include "ma.h"
67 #include "mu.h"
68 #include "mfParallelTraining.h"
69 #include "GeneralServices.h"
70 #include "heapManager.h"
71 #include "merrhdl.h"
72 #include "Filecode.h"
73 CODE_GROUP (G2_PEI)
74 RDATA_GROUP (G2_PEI)
75
76 #define FILECODE PROC_MEM_MAIN_MMPARALLELTRAINING_FILECODE
77
78 extern MEM_FEAT_BLOCK_MAIN MemFeatMain;
79 /*----------------------------------------------------------------------------
80  *                        PROTOTYPES OF LOCAL FUNCTIONS
81  *
82  *----------------------------------------------------------------------------
83  */
84
85 BOOLEAN
86 MemMParallelTraining (
87   IN OUT   MEM_MAIN_DATA_BLOCK *mmPtr
88   );
89
90 /*-----------------------------------------------------------------------------
91 *                                EXPORTED FUNCTIONS
92 *
93 *-----------------------------------------------------------------------------
94 */
95 /* -----------------------------------------------------------------------------*/
96 /**
97  *
98  *
99  *
100  *
101  *     @param[in,out]   *mmPtr   - Pointer to the MEM_MAIN_DATA_BLOCK
102  *
103  *     @return          TRUE -  No fatal error occurs.
104  *     @return          FALSE - Fatal error occurs.
105  */
106 BOOLEAN
107 MemMParallelTraining (
108   IN OUT   MEM_MAIN_DATA_BLOCK *mmPtr
109   )
110 {
111   AMD_CONFIG_PARAMS *StdHeader;
112   MEM_DATA_STRUCT *MemPtr;
113   MEM_NB_BLOCK *NBPtr;
114   DIE_INFO TrainInfo[MAX_NODES_SUPPORTED];
115   AP_DATA_TRANSFER ReturnData;
116   AGESA_STATUS Status;
117   UINT8 ApSts;
118   UINT8 Die;
119   UINT8 Socket;
120   UINT32 Module;
121   UINT32 LowCore;
122   UINT32 HighCore;
123   UINT32 Time;
124   UINT32 TimeOut;
125   UINT32 TargetApicId;
126   BOOLEAN StillTraining;
127   ALLOCATE_HEAP_PARAMS AllocHeapParams;
128   UINT8 *BufferPtr;
129   BOOLEAN TimeoutEn;
130
131   NBPtr = mmPtr->NBPtr;
132   MemPtr = mmPtr->MemPtr;
133   StdHeader = &(mmPtr->MemPtr->StdHeader);
134   Time = 0;
135   TimeOut = PARALLEL_TRAINING_TIMEOUT;
136   TimeoutEn = TRUE;
137   IDS_TIMEOUT_CTL (&TimeoutEn);
138
139   IDS_HDT_CONSOLE (MEM_STATUS, "\nStart parallel training\n");
140   AGESA_TESTPOINT (TpProcMemBeforeAnyTraining, StdHeader);
141   //
142   // Initialize Training Info Array
143   //
144   for (Die = 0; Die < mmPtr->DieCount; Die ++) {
145     Socket = TrainInfo[Die].Socket = NBPtr[Die].MCTPtr->SocketId;
146     Module = NBPtr[Die].MCTPtr->DieId;
147     GetGivenModuleCoreRange (Socket, Module, &LowCore, &HighCore, StdHeader);
148     TrainInfo[Die].Core = (UINT8) (LowCore & 0x000000FF);
149     IDS_HDT_CONSOLE (MEM_FLOW, "\tLaunch core %d of socket %d\n", LowCore, Socket);
150     TrainInfo[Die].Training = FALSE;
151   }
152   //
153   // Start Training on Each remote die.
154   //
155   for (Die = 0; Die < mmPtr->DieCount; Die ++ ) {
156     if (Die != BSP_DIE) {
157       NBPtr[Die].BeforeDqsTraining (&(mmPtr->NBPtr[Die]));
158       if (NBPtr[Die].MCTPtr->NodeMemSize != 0) {
159         if (!NBPtr[Die].FeatPtr->Training (&(mmPtr->NBPtr[Die]))) {
160           // Fail to launch code on AP
161           PutEventLog (AGESA_ERROR, MEM_ERROR_PARALLEL_TRAINING_LAUNCH_FAIL, NBPtr->Node, NBPtr->Dct, NBPtr->Channel, 0, &NBPtr->MemPtr->StdHeader);
162           SetMemError (AGESA_ERROR, NBPtr[Die].MCTPtr);
163           if (!MemPtr->ErrorHandling (NBPtr[Die].MCTPtr, EXCLUDE_ALL_DCT, EXCLUDE_ALL_CHIPSEL, &MemPtr->StdHeader)) {
164             ASSERT (FALSE);
165           }
166         } else {
167           TrainInfo[Die].Training = TRUE;
168         }
169       }
170     }
171   }
172   //
173   // Call training on BSP
174   //
175   IDS_HDT_CONSOLE (MEM_STATUS, "Node %d\n", NBPtr[BSP_DIE].Node);
176   NBPtr[BSP_DIE].BeforeDqsTraining (&(mmPtr->NBPtr[BSP_DIE]));
177   NBPtr[BSP_DIE].TrainingFlow (&(mmPtr->NBPtr[BSP_DIE]));
178   NBPtr[BSP_DIE].AfterDqsTraining (&(mmPtr->NBPtr[BSP_DIE]));
179
180   //
181   // Get Results from remote processors training
182   //
183   do {
184     StillTraining = FALSE;
185     for (Die = 0; Die < mmPtr->DieCount; Die ++ ) {
186       //
187       // For each Die that is training, read the status
188       //
189       if (TrainInfo[Die].Training == TRUE) {
190         GetLocalApicIdForCore (TrainInfo[Die].Socket, TrainInfo[Die].Core, &TargetApicId, StdHeader);
191         ApSts = ApUtilReadRemoteControlByte (TargetApicId, StdHeader);
192         if ((ApSts & 0x80) == 0) {
193           //
194           // Allocate buffer for received data
195           //
196           AllocHeapParams.RequestedBufferSize = (
197             sizeof (DIE_STRUCT) +
198             NBPtr[Die].DctCount * (
199               sizeof (DCT_STRUCT) + (
200                 NBPtr[Die].ChannelCount * (
201                   sizeof (CH_DEF_STRUCT) + sizeof (MEM_PS_BLOCK) + (
202                    (NBPtr[Die].MCTPtr->DctData[0].ChData[0].RowCount *
203                     NBPtr[Die].MCTPtr->DctData[0].ChData[0].ColumnCount *
204                     NUMBER_OF_DELAY_TABLES) +
205                     (MAX_BYTELANES_PER_CHANNEL * MAX_CS_PER_CHANNEL * NUMBER_OF_FAILURE_MASK_TABLES) +
206                     (MAX_DIMMS_PER_CHANNEL * MAX_NUMBER_LANES)
207                   )
208                 )
209               )
210             )
211           ) + 3;
212           AllocHeapParams.BufferHandle = GENERATE_MEM_HANDLE (ALLOC_PAR_TRN_HANDLE, Die, 0, 0);
213           AllocHeapParams.Persist = HEAP_LOCAL_CACHE;
214           if (HeapAllocateBuffer (&AllocHeapParams, StdHeader) == AGESA_SUCCESS) {
215             //
216             // Receive Training Results
217             //
218
219             ReturnData.DataPtr = AllocHeapParams.BufferPtr;
220             ReturnData.DataSizeInDwords = (UINT16) AllocHeapParams.RequestedBufferSize / 4;
221             ReturnData.DataTransferFlags = 0;
222             Status = ApUtilReceiveBuffer (TrainInfo[Die].Socket, TrainInfo[Die].Core, &ReturnData, StdHeader);
223             if (Status != AGESA_SUCCESS) {
224               SetMemError (Status, NBPtr[Die].MCTPtr);
225             }
226
227             BufferPtr = AllocHeapParams.BufferPtr;
228             LibAmdMemCopy (NBPtr[Die].MCTPtr, BufferPtr, sizeof (DIE_STRUCT), StdHeader);
229             BufferPtr += sizeof (DIE_STRUCT);
230             LibAmdMemCopy ( NBPtr[Die].MCTPtr->DctData,
231                             BufferPtr,
232                             NBPtr[Die].DctCount * (sizeof (DCT_STRUCT) + NBPtr[Die].ChannelCount * sizeof (CH_DEF_STRUCT)),
233                             StdHeader);
234             BufferPtr += NBPtr[Die].DctCount * (sizeof (DCT_STRUCT) + NBPtr[Die].ChannelCount * sizeof (CH_DEF_STRUCT));
235             LibAmdMemCopy ( NBPtr[Die].PSBlock,
236                             BufferPtr,
237                             NBPtr[Die].DctCount * NBPtr[Die].ChannelCount * sizeof (MEM_PS_BLOCK),
238                             StdHeader);
239             BufferPtr += NBPtr[Die].DctCount * NBPtr[Die].ChannelCount * sizeof (MEM_PS_BLOCK);
240             LibAmdMemCopy ( NBPtr[Die].MCTPtr->DctData[0].ChData[0].RcvEnDlys,
241                             BufferPtr,
242                             (NBPtr[Die].DctCount * NBPtr[Die].ChannelCount) *
243                             ((NBPtr[Die].MCTPtr->DctData[0].ChData[0].RowCount *
244                               NBPtr[Die].MCTPtr->DctData[0].ChData[0].ColumnCount *
245                               NUMBER_OF_DELAY_TABLES) +
246                               (MAX_BYTELANES_PER_CHANNEL * MAX_CS_PER_CHANNEL * NUMBER_OF_FAILURE_MASK_TABLES) +
247                               (MAX_DIMMS_PER_CHANNEL * MAX_NUMBER_LANES)
248                             ),
249                             StdHeader);
250
251             HeapDeallocateBuffer (AllocHeapParams.BufferHandle, StdHeader);
252
253             NBPtr[Die].AfterDqsTraining (&(mmPtr->NBPtr[Die]));
254             TrainInfo[Die].Training = FALSE;
255           } else {
256             PutEventLog (AGESA_FATAL, MEM_ERROR_HEAP_ALLOCATE_FOR_RECEIVED_DATA, NBPtr[Die].Node, 0, 0, 0, StdHeader);
257             SetMemError (AGESA_FATAL, NBPtr[Die].MCTPtr);
258             ASSERT(FALSE); // Insufficient Heap Space allocation for parallel training buffer
259           }
260         } else if (ApSts == CORE_IDLE) {
261           // AP does not have buffer to transmit to BSP
262           // AP fails to locate a buffer for data transfer
263           TrainInfo[Die].Training = FALSE;
264         } else {
265           // Signal to loop through again
266           StillTraining = TRUE;
267         }
268       }
269     }
270     // Wait for 1 us
271     MemUWait10ns (100, NBPtr->MemPtr);
272     Time ++;
273   } while ((StillTraining) && ((Time < TimeOut) || !TimeoutEn)); // Continue until all Dies are finished
274                                                 // if cannot finish in 1 s, do fatal exit
275
276   if (StillTraining && TimeoutEn) {
277     // Parallel training time out, do fatal exit, as there is at least one AP hangs.
278     PutEventLog (AGESA_FATAL, MEM_ERROR_PARALLEL_TRAINING_TIME_OUT, 0, 0, 0, 0, &NBPtr->MemPtr->StdHeader);
279     SetMemError (AGESA_FATAL, NBPtr[BSP_DIE].MCTPtr);
280     ASSERT(FALSE); // Timeout occurred while still training
281   }
282
283   for (Die = 0; Die < mmPtr->DieCount; Die ++ ) {
284     if (NBPtr[Die].MCTPtr->ErrCode == AGESA_FATAL) {
285       return FALSE;
286     }
287   }
288   return TRUE;
289 }