AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / HT / NbCommon / htNbCoherent.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * Coherent Feature Northbridge routines.
6  *
7  * Provide access to hardware for routing, coherent discovery.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  HyperTransport
12  * @e \$Revision: 56279 $   @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 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
55
56 #include "AGESA.h"
57 #include "amdlib.h"
58 #include "Ids.h"
59 #include "Topology.h"
60 #include "htFeat.h"
61 #include "htNb.h"
62 #include "htNbCommonHardware.h"
63 #include "htNbCoherent.h"
64 #include "Filecode.h"
65 CODE_GROUP (G1_PEICC)
66 RDATA_GROUP (G2_PEI)
67
68 #define FILECODE PROC_HT_NBCOMMON_HTNBCOHERENT_FILECODE
69 /*----------------------------------------------------------------------------
70  *                          DEFINITIONS AND MACROS
71  *
72  *----------------------------------------------------------------------------
73  */
74
75 /*----------------------------------------------------------------------------
76  *                           TYPEDEFS AND STRUCTURES
77  *
78  *----------------------------------------------------------------------------
79  */
80 /*----------------------------------------------------------------------------
81  *                        PROTOTYPES OF LOCAL FUNCTIONS
82  *
83  *----------------------------------------------------------------------------
84  */
85
86 /***************************************************************************
87  ***               FAMILY/NORTHBRIDGE SPECIFIC FUNCTIONS                 ***
88  ***************************************************************************/
89
90 /*----------------------------------------------------------------------------------------*/
91 /**
92  * Establish a Temporary route from one Node to another.
93  *
94  * @HtNbMethod{::F_WRITE_ROUTING_TABLE}
95  *
96  * This routine will modify the routing tables on the
97  * SourceNode to cause it to route both request and response traffic to the
98  * targetNode through the specified Link.
99  *
100  * @note: This routine is to be used for early discovery and initialization.  The
101  * final routing tables must be loaded some other way because this
102  * routine does not address the issue of probes, or independent request
103  * response paths.
104  *
105  * @param[in]     Node     the Node that will have it's routing tables modified.
106  * @param[in]     Target   For routing to Node target
107  * @param[in]     Link     Link from Node to target
108  * @param[in]     Nb       this northbridge
109  */
110 VOID
111 WriteRoutingTable (
112   IN       UINT8       Node,
113   IN       UINT8       Target,
114   IN       UINT8       Link,
115   IN       NORTHBRIDGE *Nb
116   )
117 {
118   PCI_ADDR Reg;
119   UINT32 Temp;
120
121   ASSERT ((Node < MAX_NODES) && (Target < MAX_NODES) && (Link < Nb->MaxLinks));
122   Temp = (Nb->SelfRouteResponseMask | Nb->SelfRouteRequestMask) << (Link + 1);
123   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
124                                  MakePciBusFromNode (Node),
125                                  MakePciDeviceFromNode (Node),
126                                  CPU_HTNB_FUNC_00,
127                                  REG_ROUTE0_0X40 + (Target * 4));
128   LibAmdPciWrite (AccessWidth32, Reg, &Temp, Nb->ConfigHandle);
129 }
130
131 /*----------------------------------------------------------------------------------------*/
132 /**
133  * Modifies the NodeID register on the target Node
134  *
135  * @HtNbMethod{::F_WRITE_NODEID}
136  *
137  * @param[in]     Node   the Node that will have its NodeID altered.
138  * @param[in]     NodeID the new value for NodeID
139  * @param[in]     Nb     this northbridge
140  */
141 VOID
142 WriteNodeID (
143   IN       UINT8       Node,
144   IN       UINT8       NodeID,
145   IN       NORTHBRIDGE *Nb
146   )
147 {
148   PCI_ADDR Reg;
149   UINT32 Temp;
150   Temp = NodeID;
151   ASSERT ((Node < MAX_NODES) && (NodeID < MAX_NODES));
152   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
153                                  MakePciBusFromNode (Node),
154                                  MakePciDeviceFromNode (Node),
155                                  CPU_HTNB_FUNC_00,
156                                  REG_NODE_ID_0X60);
157   LibAmdPciWriteBits (Reg, 2, 0, &Temp, Nb->ConfigHandle);
158 }
159
160 /*----------------------------------------------------------------------------------------*/
161 /**
162  * Read the Default Link
163  *
164  * @HtNbMethod{::F_READ_DEFAULT_LINK}
165  *
166  * Read the DefLnk (the source Link of the current packet) from Node. Since this code
167  * is running on the BSP, this should be the Link pointing back towards the BSP.
168  *
169  * @param[in]     Node   the Node that will have its NodeID altered.
170  * @param[in]     Nb     this northbridge
171  *
172  * @return The HyperTransport Link where the request to
173  *         read the default Link came from.
174  */
175 UINT8
176 ReadDefaultLink (
177   IN       UINT8       Node,
178   IN       NORTHBRIDGE *Nb
179   )
180 {
181   UINT32 DefaultLink;
182   PCI_ADDR Reg;
183   UINT32 Temp;
184
185   DefaultLink = 0;
186   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
187                                  MakePciBusFromNode (Node),
188                                  MakePciDeviceFromNode (Node),
189                                  CPU_HTNB_FUNC_00,
190                                  REG_LINK_INIT_CONTROL_0X6C);
191
192   ASSERT ((Node < MAX_NODES));
193   LibAmdPciReadBits (Reg, 3, 2, &DefaultLink, Nb->ConfigHandle);
194   LibAmdPciReadBits (Reg, 8, 8, &Temp, Nb->ConfigHandle);
195   DefaultLink |= (Temp << 2);
196   return (UINT8)DefaultLink;
197 }
198
199 /*----------------------------------------------------------------------------------------*/
200 /**
201  * Turns routing tables on for a given Node
202  *
203  * @HtNbMethod{::F_ENABLE_ROUTING_TABLES}
204  *
205  * @param[in] Node the Node that will have it's routing tables enabled
206  * @param[in] Nb   this northbridge
207  */
208 VOID
209 EnableRoutingTables (
210   IN       UINT8       Node,
211   IN       NORTHBRIDGE *Nb
212   )
213 {
214   PCI_ADDR Reg;
215   UINT32 Temp;
216   Temp = 0;
217   ASSERT ((Node < MAX_NODES));
218   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
219                                  MakePciBusFromNode (Node),
220                                  MakePciDeviceFromNode (Node),
221                                  CPU_HTNB_FUNC_00,
222                                  REG_LINK_INIT_CONTROL_0X6C);
223   LibAmdPciWriteBits (Reg, 0, 0, &Temp, Nb->ConfigHandle);
224 }
225
226 /*----------------------------------------------------------------------------------------*/
227 /**
228  * Turns routing tables off for a given Node
229  *
230  * @HtNbMethod{::F_DISABLE_ROUTING_TABLES}
231  *
232  * @param[in] Node the Node that will have it's routing tables disabled
233  * @param[in] Nb   this northbridge
234  */
235 VOID
236 DisableRoutingTables (
237   IN       UINT8       Node,
238   IN       NORTHBRIDGE *Nb
239   )
240 {
241   PCI_ADDR Reg;
242   UINT32 Temp;
243   Temp = 1;
244   ASSERT ((Node < MAX_NODES));
245   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
246                                  MakePciBusFromNode (Node),
247                                  MakePciDeviceFromNode (Node),
248                                  CPU_HTNB_FUNC_00,
249                                  REG_LINK_INIT_CONTROL_0X6C);
250   LibAmdPciWriteBits (Reg, 0, 0, &Temp, Nb->ConfigHandle);
251 }
252
253 /*----------------------------------------------------------------------------------------*/
254 /**
255  * Verify that the Link is coherent, connected, and ready
256  *
257  * @HtNbMethod{::F_VERIFY_LINK_IS_COHERENT}
258  *
259  * @param[in]     Node    the Node that will be examined
260  * @param[in]     Link    the Link on that Node to examine
261  * @param[in]     Nb      this northbridge
262  *
263  * @retval        TRUE    The Link has the following status
264  *                        -  LinkCon=1,           Link is connected
265  *                        -  InitComplete=1,      Link initialization is complete
266  *                        -  NC=0,                Link is coherent
267  *                        -  UniP-cLDT=0,         Link is not Uniprocessor cLDT
268  *                        -  LinkConPend=0        Link connection is not pending
269  *  @retval       FALSE   The Link has some other status
270 */
271 BOOLEAN
272 VerifyLinkIsCoherent (
273   IN       UINT8       Node,
274   IN       UINT8       Link,
275   IN       NORTHBRIDGE *Nb
276   )
277 {
278   UINT32 LinkType;
279   PCI_ADDR LinkBase;
280
281   ASSERT ((Node < MAX_NODES) && (Link < Nb->MaxLinks));
282
283   LinkBase = Nb->MakeLinkBase (Node, Link, Nb);
284
285   //  FN0_98/A4/C4 = LDT Type Register
286   LinkBase.Address.Register += HTHOST_LINK_TYPE_REG;
287   LibAmdPciRead (AccessWidth32, LinkBase, &LinkType, Nb->ConfigHandle);
288
289   //  Verify LinkCon = 1, InitComplete = 1, NC = 0, UniP-cLDT = 0, LinkConPend = 0
290   return (BOOLEAN) ((LinkType & HTHOST_TYPE_MASK) ==  HTHOST_TYPE_COHERENT);
291 }
292
293 /*----------------------------------------------------------------------------------------*/
294 /**
295  * Read the token stored in the scratchpad register field.
296  *
297  * @HtNbMethod{::F_READ_TOKEN}
298  *
299  * Use the CPU core count as a scratch pad.
300  *
301  * @note The location used to store the token is arbitrary.  The only requirement is
302  * that the location warm resets to zero, and that using it will have no ill-effects
303  * during HyperTransport initialization.
304  *
305  * @param[in]     Node    the Node that will be examined
306  * @param[in]     Nb      this northbridge
307  *
308  * @return        the Token read from the Node
309  */
310 UINT8
311 ReadToken (
312   IN       UINT8       Node,
313   IN       NORTHBRIDGE *Nb
314   )
315 {
316   UINT32 Temp;
317   PCI_ADDR Reg;
318
319   ASSERT ((Node < MAX_NODES));
320   // Use CpuCnt as a scratch register
321   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
322                                  MakePciBusFromNode (Node),
323                                  MakePciDeviceFromNode (Node),
324                                  CPU_HTNB_FUNC_00,
325                                  REG_NODE_ID_0X60);
326   LibAmdPciReadBits (Reg, 19, 16, &Temp, Nb->ConfigHandle);
327
328   return (UINT8)Temp;
329 }
330
331
332 /*----------------------------------------------------------------------------------------*/
333 /**
334  * Write the token stored in the scratchpad register
335  *
336  * @HtNbMethod{::F_WRITE_TOKEN}
337  *
338  * Use the CPU core count as a scratch pad.
339  *
340  * @note The location used to store the token is arbitrary.  The only requirement is
341  * that the location warm resets to zero, and that using it will have no ill-effects
342  * during HyperTransport initialization.
343  *
344  * @param[in]     Node  the Node that marked with token
345  * @param[in]     Value the token Value
346  * @param[in]     Nb    this northbridge
347  */
348 VOID
349 WriteToken (
350   IN       UINT8       Node,
351   IN       UINT8       Value,
352   IN       NORTHBRIDGE *Nb
353   )
354 {
355   PCI_ADDR Reg;
356   UINT32 Temp;
357   Temp = Value;
358   ASSERT ((Node < MAX_NODES));
359   // Use CpuCnt as a scratch register
360   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
361                                  MakePciBusFromNode (Node),
362                                  MakePciDeviceFromNode (Node),
363                                  CPU_HTNB_FUNC_00,
364                                  REG_NODE_ID_0X60);
365   LibAmdPciWriteBits (Reg, 19, 16, &Temp, Nb->ConfigHandle);
366 }
367
368 /*----------------------------------------------------------------------------------------*/
369 /**
370  * Full Routing Table Register initialization
371  *
372  * @HtNbMethod{::F_WRITE_FULL_ROUTING_TABLE}
373  *
374  * Write the routing table entry for Node to target, using the request Link, response
375  * Link, and broadcast Links provided.
376  *
377  * @param[in]     Node            the Node that will be examined
378  * @param[in]     Target          the Target Node for these routes
379  * @param[in]     ReqLink         the Link for requests to Target
380  * @param[in]     RspLink         the Link for responses to Target
381  * @param[in]     BroadcastLinks  the broadcast Links
382  * @param[in]     Nb              this northbridge
383  */
384 VOID
385 WriteFullRoutingTable (
386   IN       UINT8       Node,
387   IN       UINT8       Target,
388   IN       UINT8       ReqLink,
389   IN       UINT8       RspLink,
390   IN       UINT32      BroadcastLinks,
391   IN       NORTHBRIDGE *Nb
392   )
393 {
394   PCI_ADDR Reg;
395   UINT32 Value;
396
397   Value = 0;
398   ASSERT ((Node < MAX_NODES) && (Target < MAX_NODES));
399   if (ReqLink == ROUTE_TO_SELF) {
400     Value |= Nb->SelfRouteRequestMask;
401   } else {
402     Value |= Nb->SelfRouteRequestMask << (ReqLink + 1);
403   }
404
405   if (RspLink == ROUTE_TO_SELF) {
406     Value |= Nb->SelfRouteResponseMask;
407   } else {
408     Value |= Nb->SelfRouteResponseMask << (RspLink + 1);
409   }
410
411   // Allow us to accept a Broadcast ourselves, then set broadcasts for routes
412   Value |= (UINT32)1 << Nb->BroadcastSelfBit;
413   Value |= (UINT32)BroadcastLinks << (Nb->BroadcastSelfBit + 1);
414
415   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
416                                  MakePciBusFromNode (Node),
417                                  MakePciDeviceFromNode (Node),
418                                  CPU_HTNB_FUNC_00,
419                                  REG_ROUTE0_0X40 + (Target * 4));
420   LibAmdPciWrite (AccessWidth32, Reg, &Value, Nb->ConfigHandle);
421 }
422
423 /*----------------------------------------------------------------------------------------*/
424 /**
425  * Determine whether a Node is compatible with the discovered configuration so far.
426  *
427  * @HtNbMethod{::F_IS_ILLEGAL_TYPE_MIX}.
428  *
429  * Currently, that means the family, extended family of the new Node are the
430  * same as the BSP's.
431  *
432  * @param[in] Node the Node
433  * @param[in] Nb   this northbridge
434  *
435  * @retval   TRUE    the new node is not compatible
436  * @retval   FALSE   the new node is compatible
437  */
438 BOOLEAN
439 IsIllegalTypeMix (
440   IN       UINT8       Node,
441   IN       NORTHBRIDGE *Nb
442   )
443 {
444   return ((BOOLEAN) ((Nb->MakeKey (Node, Nb) & Nb->CompatibleKey) == 0));
445 }
446
447 /*----------------------------------------------------------------------------------------*/
448 /**
449  * Fix (hopefully) exceptional conditions.
450  *
451  * @HtNbMethod{::F_HANDLE_SPECIAL_NODE_CASE}.
452  *
453  * Currently, this routine is implemented for all coherent HT families to check
454  * vendor ID of coherent Node. If the vendor ID is 0x1022 then return FALSE,
455  * or return TRUE.
456  *
457  * @param[in] Node  The Node which need to be checked.
458  * @param[in] Link  The link to check for special conditions.
459  * @param[in] State our global state.
460  * @param[in] Nb    this northbridge.
461  *
462  * @retval    TRUE  This node received special handling.
463  * @retval    FALSE This node was not handled specially, handle it normally.
464  *
465  */
466 BOOLEAN
467 HandleSpecialNodeCase (
468   IN       UINT8       Node,
469   IN       UINT8       Link,
470   IN       STATE_DATA  *State,
471   IN       NORTHBRIDGE *Nb
472   )
473 {
474   BOOLEAN Result;
475   PCI_ADDR Reg;
476   UINT32 VendorID;
477
478   Result = TRUE;
479
480   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
481                                  MakePciBusFromNode (Node),
482                                  MakePciDeviceFromNode (Node),
483                                  0,
484                                  0);
485
486   LibAmdPciReadBits (Reg, 15, 0, &VendorID, Nb->ConfigHandle);
487   if (VendorID == 0x1022) {
488     Result = FALSE;
489   }
490
491   return Result;
492 }