AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / HT / Fam10 / htNbUtilitiesFam10.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * Northbridge utility routines.
6  *
7  * These routines are needed for support of more than one feature area.
8  * Collect them in this file so build options don't remove them.
9  *
10  * @xrefitem bom "File Content Label" "Release Content"
11  * @e project:      AGESA
12  * @e sub-project:  HyperTransport
13  * @e \$Revision: 56279 $   @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 2011) $
14  *
15  */
16 /*
17 *****************************************************************************
18 *
19 * Copyright (C) 2012 Advanced Micro Devices, Inc.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are met:
24 *     * Redistributions of source code must retain the above copyright
25 *       notice, this list of conditions and the following disclaimer.
26 *     * Redistributions in binary form must reproduce the above copyright
27 *       notice, this list of conditions and the following disclaimer in the
28 *       documentation and/or other materials provided with the distribution.
29 *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
30 *       its contributors may be used to endorse or promote products derived
31 *       from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
37 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 *
44 * ***************************************************************************
45 *
46 */
47
48 /*
49  *----------------------------------------------------------------------------
50  *                                MODULES USED
51  *
52  *----------------------------------------------------------------------------
53  */
54
55
56
57 #include "AGESA.h"
58 #include "amdlib.h"
59 #include "Ids.h"
60 #include "Topology.h"
61 #include "htFeat.h"
62 #include "htNb.h"
63 #include "htNbCommonHardware.h"
64 #include "htNbUtilitiesFam10.h"
65 #include "Filecode.h"
66 CODE_GROUP (G1_PEICC)
67 RDATA_GROUP (G2_PEI)
68
69 #define FILECODE PROC_HT_FAM10_HTNBUTILITIESFAM10_FILECODE
70 /*----------------------------------------------------------------------------
71  *                          DEFINITIONS AND MACROS
72  *
73  *----------------------------------------------------------------------------
74  */
75
76 /*----------------------------------------------------------------------------------------*/
77 /**
78  * Return the number of cores (1 based count) on Node.
79  *
80  * @HtNbMethod{::F_GET_NUM_CORES_ON_NODE}
81  *
82  * @param[in]     Node   the Node that will be examined
83  * @param[in]     Nb     this northbridge
84  *
85  * @return        the number of cores
86  */
87 UINT8
88 Fam10GetNumCoresOnNode (
89   IN       UINT8       Node,
90   IN       NORTHBRIDGE *Nb
91   )
92 {
93   UINT32 Result;
94   UINT32 Leveling;
95   UINT32 Cores;
96   UINT8 i;
97   PCI_ADDR Reg;
98
99   ASSERT ((Node < MAX_NODES));
100   // Read CmpCap
101   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
102                                  MakePciBusFromNode (Node),
103                                  MakePciDeviceFromNode (Node),
104                                  CPU_NB_FUNC_03,
105                                  REG_NB_CAPABILITY_3XE8);
106
107   LibAmdPciReadBits (Reg, 13, 12, &Cores, Nb->ConfigHandle);
108
109   // Support Downcoring
110   Result = Cores;
111   Cores++;
112   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
113                                  MakePciBusFromNode (Node),
114                                  MakePciDeviceFromNode (Node),
115                                  CPU_NB_FUNC_03,
116                                  REG_NB_DOWNCORE_3X190);
117   LibAmdPciReadBits (Reg, 3, 0, &Leveling, Nb->ConfigHandle);
118   for (i = 0; i < Cores; i++) {
119     if ((Leveling & ((UINT32) 1 << i)) != 0) {
120       Result--;
121     }
122   }
123   return (UINT8) (Result + 1);
124 }
125
126 /*----------------------------------------------------------------------------------------*/
127 /**
128  * Return the number of cores (1 based count) on Node.
129  *
130  * @HtNbMethod{::F_GET_NUM_CORES_ON_NODE}.
131  *
132  * @param[in]     Node   the Node that will be examined
133  * @param[in]     Nb     this northbridge
134  *
135  * @return        the number of cores
136  */
137 UINT8
138 Fam10RevDGetNumCoresOnNode (
139   IN       UINT8       Node,
140   IN       NORTHBRIDGE *Nb
141   )
142 {
143   UINT32 Result;
144   UINT32 Leveling;
145   UINT32 Cores;
146   UINT32 Cores2;
147   UINT8 i;
148   PCI_ADDR Reg;
149
150   ASSERT ((Node < MAX_NODES));
151   // Read CmpCap
152   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
153                                  MakePciBusFromNode (Node),
154                                  MakePciDeviceFromNode (Node),
155                                  CPU_NB_FUNC_03,
156                                  REG_NB_CAPABILITY_3XE8);
157
158   LibAmdPciReadBits (Reg, 13, 12, &Cores, Nb->ConfigHandle);
159   LibAmdPciReadBits (Reg, 15, 15, &Cores2, Nb->ConfigHandle);
160   Cores = Cores + (Cores2 << 2);
161
162   // Support Downcoring
163   Result = Cores;
164   Cores++;
165   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
166                                  MakePciBusFromNode (Node),
167                                  MakePciDeviceFromNode (Node),
168                                  CPU_NB_FUNC_03,
169                                  REG_NB_DOWNCORE_3X190);
170   LibAmdPciReadBits (Reg, 5, 0, &Leveling, Nb->ConfigHandle);
171   for (i = 0; i < Cores; i++) {
172     if ((Leveling & ((UINT32) 1 << i)) != 0) {
173       Result--;
174     }
175   }
176   return (UINT8) (Result + 1);
177 }
178
179 /*----------------------------------------------------------------------------------------*/
180 /**
181  * Get the next link for iterating over the links on a node in the correct order.
182  *
183  * @HtNbMethod{::F_GET_NEXT_LINK}
184  *
185  * Family 10h specific implementation use the Internal Link field in
186  * the northbridge to prioritize internal links in the order.
187  *
188  * @param[in]     Node The node on which to iterate links.
189  * @param[in,out] Link IN: the current iteration context, OUT: the next link.
190  * @param[in]     Nb   This Northbridge, access to config pointer.
191  *
192  * @retval LinkIteratorExternal    The current Link is an external link.
193  * @retval LinkIteratorInternal    The current Link is an internal link.
194  * @retval LinkIteratorEnd         There is no next link (Link is back to BEGIN).
195  *
196  */
197 LINK_ITERATOR_STATUS
198 Fam10GetNextLink (
199   IN       UINT8       Node,
200   IN OUT   UINT8       *Link,
201   IN       NORTHBRIDGE *Nb
202   )
203 {
204   PCI_ADDR Reg;
205   UINT32   InternalLinks;
206   UINT32   ExternalLinks;
207   UINT32   HigherLinks;
208   BOOLEAN  IsInternalLink;
209   LINK_ITERATOR_STATUS Status;
210
211   ASSERT ((Node < MAX_NODES));
212   ASSERT ((*Link < Nb->MaxLinks) || (*Link == LINK_ITERATOR_BEGIN));
213   InternalLinks = 0;
214   ExternalLinks = 0;
215
216   // Read IntLnkRoute from the Link Initialization Status register.
217   // (Note that this register field is not reserved prior to rev D, but should be zero.)
218   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
219                                  MakePciBusFromNode (Node),
220                                  MakePciDeviceFromNode (Node),
221                                  CPU_HTNB_FUNC_00,
222                                  REG_HT_LINK_INITIALIZATION_0X1A0);
223
224   LibAmdPciReadBits (Reg, 23, 16, &InternalLinks, Nb->ConfigHandle);
225   // The external links are all possible links which are not Internal
226   ExternalLinks = (((1 << Nb->MaxLinks) - 1) ^ InternalLinks);
227   // Can't have no possible links!
228   ASSERT ((ExternalLinks != 0) || (InternalLinks != 0));
229
230
231   if (*Link == LINK_ITERATOR_BEGIN) {
232     // If the request is for the first link (BEGIN), get it
233     if (InternalLinks != 0) {
234       *Link = LibAmdBitScanForward (InternalLinks);
235       Status = LinkIteratorInternal;
236     } else {
237       *Link = LibAmdBitScanForward (ExternalLinks);
238       Status = LinkIteratorExternal;
239     }
240   } else {
241     // If the iterator is not at the beginning, search for the next Link starting from the
242     // current link.
243     HigherLinks = InternalLinks & ~((1 << (*Link + 1)) - 1);
244     IsInternalLink = (BOOLEAN) ((InternalLinks & (1 << *Link)) != 0);
245     if (IsInternalLink && (HigherLinks != 0)) {
246       // We are still on internal links and there are more to do.
247       *Link = LibAmdBitScanForward (HigherLinks);
248       Status = LinkIteratorInternal;
249     } else {
250       if (IsInternalLink) {
251         // We are transitioning now from internal to external, so get the first external link
252         HigherLinks = ExternalLinks;
253       } else {
254         // We are already iterating over external links, so get the next one
255         HigherLinks = ExternalLinks & ~((1 << (*Link + 1)) - 1);
256       }
257       if (HigherLinks != 0) {
258         *Link = LibAmdBitScanForward (HigherLinks);
259         Status = LinkIteratorExternal;
260       } else {
261         // The end of all links
262         *Link = LINK_ITERATOR_BEGIN;
263         Status = LinkIteratorEnd;
264       }
265     }
266   }
267   return Status;
268 }
269
270 /*----------------------------------------------------------------------------------------*/
271 /**
272  * Get Info about Module Type of this northbridge
273  *
274  * @HtNbMethod{::F_GET_MODULE_INFO}
275  *
276  * Provide the Processor module type, single or multi, and the node's module id.
277  *
278  * @param[in]     Node                the Node
279  * @param[out]    ModuleType          0 for Single, 1 for Multi
280  * @param[out]    Module              The module number of this node (0 if Single)
281  * @param[in]     Nb                  this northbridge
282  *
283  */
284 VOID
285 Fam10GetModuleInfo (
286   IN       UINT8       Node,
287      OUT   UINT8       *ModuleType,
288      OUT   UINT8       *Module,
289   IN       NORTHBRIDGE *Nb
290   )
291 {
292   PCI_ADDR Reg;
293   UINT32 MultNodeCpu;
294   UINT32 IntNodeNum;
295
296   ASSERT (Node < MAX_NODES);
297
298   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
299                                  MakePciBusFromNode (Node),
300                                  MakePciDeviceFromNode (Node),
301                                  CPU_NB_FUNC_03,
302                                  REG_NB_CAPABILITY_3XE8);
303   LibAmdPciReadBits (Reg, 29, 29, &MultNodeCpu, Nb->ConfigHandle);
304   LibAmdPciReadBits (Reg, 31, 30, &IntNodeNum, Nb->ConfigHandle);
305
306   *ModuleType = (UINT8) MultNodeCpu;
307   *Module = (UINT8) IntNodeNum;
308 }
309
310 /*----------------------------------------------------------------------------------------*/
311 /**
312  * Implement the hardware method of doing Socket Naming, by accessing this northbridge's Socket Id register.
313  *
314  * @HtNbMethod{::F_GET_SOCKET}
315  *
316  * The hardware socket naming method is not available for Family 10h prior to rev D.
317  *
318  * @param[in]   Node    The node for which we want the socket id.
319  * @param[in]   TempNode The temporary node id route where the node can be accessed.
320  * @param[in]   Nb      Our Northbridge.
321  *
322  * @return      The Socket Id
323  */
324 UINT8
325 Fam10GetSocket (
326   IN       UINT8       Node,
327   IN       UINT8       TempNode,
328   IN       NORTHBRIDGE *Nb
329   )
330 {
331   ASSERT ((Node < MAX_NODES));
332   ASSERT (TempNode < MAX_NODES);
333   ASSERT (Nb != NULL);
334   return (Node);
335 }
336
337 /*----------------------------------------------------------------------------------------*/
338 /**
339  * Implement the hardware method of doing Socket Naming, by accessing this northbridge's Socket Id register.
340  *
341  * @HtNbMethod{::F_GET_SOCKET}
342  *
343  * The Socket Id is strapped to the Sbi Control Register, F3X1E4[6:4]SbiAddr.
344  *
345  * @param[in]   Node    The node for which we want the socket id.
346  * @param[in]   TempNode The temporary node id route where the node can be accessed.
347  * @param[in]   Nb      Our Northbridge.
348  *
349  * @return      The Socket Id
350  */
351 UINT8
352 Fam10RevDGetSocket (
353   IN       UINT8       Node,
354   IN       UINT8       TempNode,
355   IN       NORTHBRIDGE *Nb
356   )
357 {
358   UINT32 Socket;
359   PCI_ADDR Reg;
360
361   ASSERT ((TempNode < MAX_NODES));
362   ASSERT ((Node < MAX_NODES));
363   // Read SbiAddr
364   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (TempNode),
365                                  MakePciBusFromNode (TempNode),
366                                  MakePciDeviceFromNode (TempNode),
367                                  CPU_NB_FUNC_03,
368                                  REG_NB_SBI_CONTROL_3X1E4);
369   LibAmdPciReadBits (Reg, 6, 4, &Socket, Nb->ConfigHandle);
370   return ((UINT8) Socket);
371 }
372
373 /*----------------------------------------------------------------------------------------*/
374 /**
375  * Post info to AP cores via a mailbox.
376  *
377  * @HtNbMethod{::F_POST_MAILBOX}
378  *
379  * Use the link MCA counter register as a PCI -> MSR mailbox, for info such as node id,
380  * and module info.
381  *
382  * @param[in]     Node          the Node
383  * @param[in]     ApMailboxes   The info to post
384  * @param[in]     Nb            this northbridge
385  *
386  */
387 VOID
388 Fam10PostMailbox (
389   IN       UINT8 Node,
390   IN       AP_MAILBOXES ApMailboxes,
391   IN       NORTHBRIDGE *Nb
392   )
393 {
394   PCI_ADDR Reg;
395
396   ASSERT (Node < MAX_NODES);
397
398   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
399                                  MakePciBusFromNode (Node),
400                                  MakePciDeviceFromNode (Node),
401                                  CPU_NB_FUNC_03,
402                                  REG_NB_MCA_LINK_THRESHOLD_3X168);
403   LibAmdPciWriteBits (Reg, 11, 0, &ApMailboxes.ApMailInfo.Info, Nb->ConfigHandle);
404   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
405                                  MakePciBusFromNode (Node),
406                                  MakePciDeviceFromNode (Node),
407                                  CPU_NB_FUNC_03,
408                                  REG_NB_MCA_L3_THRESHOLD_3X170);
409   LibAmdPciWriteBits (Reg, 11, 0, &ApMailboxes.ApMailExtInfo.Info, Nb->ConfigHandle);
410 }
411
412 /*----------------------------------------------------------------------------------------*/
413 /**
414  * Retrieve info from a node's mailbox.
415  *
416  * @HtNbMethod{::F_RETRIEVE_MAILBOX}
417  *
418  * Use the link MCA counter register as a PCI -> MSR mailbox, for info such as node id,
419  * and module info.
420  *
421  * @param[in]     Node          the Node
422  * @param[in]     Nb            this northbridge
423  *
424  * @return        The ap mailbox info
425  *
426  */
427 AP_MAIL_INFO
428 Fam10RetrieveMailbox (
429   IN       UINT8 Node,
430   IN       NORTHBRIDGE *Nb
431   )
432 {
433   PCI_ADDR Reg;
434   AP_MAIL_INFO ApMailInfo;
435
436   ASSERT (Node < MAX_NODES);
437
438   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
439                                  MakePciBusFromNode (Node),
440                                  MakePciDeviceFromNode (Node),
441                                  CPU_NB_FUNC_03,
442                                  REG_NB_MCA_LINK_THRESHOLD_3X168);
443   LibAmdPciReadBits (Reg, 11, 0, &ApMailInfo.Info, Nb->ConfigHandle);
444   return ApMailInfo;
445 }