AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / HT / Fam15 / htNbUtilitiesFam15.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: 44324 $   @e \$Date: 2010-12-22 02:16:51 -0700 (Wed, 22 Dec 2010) $
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 "htNbUtilitiesFam15.h"
65 #include "Filecode.h"
66 CODE_GROUP (G2_PEI)
67 RDATA_GROUP (G2_PEI)
68
69 #define FILECODE PROC_HT_FAM15_HTNBUTILITIESFAM15_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 Fam15GetNumCoresOnNode (
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_05,
105                                  REG_NB_CAPABILITY_2_5X84);
106
107   LibAmdPciReadBits (Reg, 7, 0, &Result, Nb->ConfigHandle);
108
109   // Support Downcoring
110   Cores = Result;
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, 31, 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  * Get the next link for iterating over the links on a node in the correct order.
129  *
130  * @HtNbMethod{::F_GET_NEXT_LINK}
131  *
132  * Use the Internal Link field in the northbridge to prioritize internal links in the
133  * order.
134  *
135  * @param[in]     Node The node on which to iterate links.
136  * @param[in,out] Link IN: the current iteration context, OUT: the next link.
137  * @param[in]     Nb   This Northbridge, access to config pointer.
138  *
139  * @retval LinkIteratorExternal    The current Link is an external link.
140  * @retval LinkIteratorInternal    The current Link is an internal link.
141  * @retval LinkIteratorEnd         There is no next link (Link is back to BEGIN).
142  *
143  */
144 LINK_ITERATOR_STATUS
145 Fam15GetNextLink (
146   IN       UINT8       Node,
147   IN OUT   UINT8       *Link,
148   IN       NORTHBRIDGE *Nb
149   )
150 {
151   PCI_ADDR Reg;
152   UINT32   InternalLinks;
153   UINT32   ExternalLinks;
154   UINT32   HigherLinks;
155   BOOLEAN  IsInternalLink;
156   LINK_ITERATOR_STATUS Status;
157
158   ASSERT ((Node < MAX_NODES));
159   ASSERT ((*Link < Nb->MaxLinks) || (*Link == LINK_ITERATOR_BEGIN));
160   InternalLinks = 0;
161   ExternalLinks = 0;
162
163   // Read IntLnkRoute from the Link Initialization Status register.
164   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
165                                  MakePciBusFromNode (Node),
166                                  MakePciDeviceFromNode (Node),
167                                  CPU_HTNB_FUNC_00,
168                                  REG_HT_LINK_INITIALIZATION_0X1A0);
169
170   LibAmdPciReadBits (Reg, 23, 16, &InternalLinks, Nb->ConfigHandle);
171   // The external links are all possible links which are not Internal
172   ExternalLinks = (((1 << Nb->MaxLinks) - 1) ^ InternalLinks);
173   // Can't have no possible links!
174   ASSERT ((ExternalLinks != 0) || (InternalLinks != 0));
175
176
177   if (*Link == LINK_ITERATOR_BEGIN) {
178     // If the request is for the first link (BEGIN), get it
179     if (InternalLinks != 0) {
180       *Link = LibAmdBitScanForward (InternalLinks);
181       Status = LinkIteratorInternal;
182     } else {
183       *Link = LibAmdBitScanForward (ExternalLinks);
184       Status = LinkIteratorExternal;
185     }
186   } else {
187     // If the iterator is not at the beginning, search for the next Link starting from the
188     // current link.
189     HigherLinks = InternalLinks & ~((1 << (*Link + 1)) - 1);
190     IsInternalLink = (BOOLEAN) ((InternalLinks & (1 << *Link)) != 0);
191     if (IsInternalLink && (HigherLinks != 0)) {
192       // We are still on internal links and there are more to do.
193       *Link = LibAmdBitScanForward (HigherLinks);
194       Status = LinkIteratorInternal;
195     } else {
196       if (IsInternalLink) {
197         // We are transitioning now from internal to external, so get the first external link
198         HigherLinks = ExternalLinks;
199       } else {
200         // We are already iterating over external links, so get the next one
201         HigherLinks = ExternalLinks & ~((1 << (*Link + 1)) - 1);
202       }
203       if (HigherLinks != 0) {
204         *Link = LibAmdBitScanForward (HigherLinks);
205         Status = LinkIteratorExternal;
206       } else {
207         // The end of all links
208         *Link = LINK_ITERATOR_BEGIN;
209         Status = LinkIteratorEnd;
210       }
211     }
212   }
213   return Status;
214 }
215
216 /*----------------------------------------------------------------------------------------*/
217 /**
218  * Get Info about Module Type of this northbridge
219  *
220  * @HtNbMethod{::F_GET_MODULE_INFO}
221  *
222  * Provide the Processor module type, single or multi, and the node's module id.
223  *
224  * @param[in]     Node                the Node
225  * @param[out]    ModuleType          0 for Single, 1 for Multi
226  * @param[out]    Module              The module number of this node (0 if Single)
227  * @param[in]     Nb                  this northbridge
228  *
229  */
230 VOID
231 Fam15GetModuleInfo (
232   IN       UINT8       Node,
233      OUT   UINT8       *ModuleType,
234      OUT   UINT8       *Module,
235   IN       NORTHBRIDGE *Nb
236   )
237 {
238   PCI_ADDR Reg;
239   UINT32 MultNodeCpu;
240   UINT32 IntNodeNum;
241
242   ASSERT (Node < MAX_NODES);
243
244   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
245                                  MakePciBusFromNode (Node),
246                                  MakePciDeviceFromNode (Node),
247                                  CPU_NB_FUNC_03,
248                                  REG_NB_CAPABILITY_3XE8);
249   LibAmdPciReadBits (Reg, 29, 29, &MultNodeCpu, Nb->ConfigHandle);
250   LibAmdPciReadBits (Reg, 31, 30, &IntNodeNum, Nb->ConfigHandle);
251
252   *ModuleType = (UINT8) MultNodeCpu;
253   *Module = (UINT8) IntNodeNum;
254 }
255
256 /*----------------------------------------------------------------------------------------*/
257 /**
258  * Implement the hardware method of doing Socket Naming, by accessing this northbridge's Socket Id register.
259  *
260  * @HtNbMethod{::F_GET_SOCKET}
261  *
262  * The hardware socket naming method is not available for Family 15h in some packages.
263  *
264  * @param[in]   Node    The node for which we want the socket id.
265  * @param[in]   TempNode The temporary node id route where the node can be accessed.
266  * @param[in]   Nb      Our Northbridge.
267  *
268  * @return      The Socket Id
269  */
270 UINT8
271 Fam15GetSocket (
272   IN       UINT8       Node,
273   IN       UINT8       TempNode,
274   IN       NORTHBRIDGE *Nb
275   )
276 {
277   ASSERT ((Node < MAX_NODES));
278   ASSERT (TempNode < MAX_NODES);
279   ASSERT (Nb != NULL);
280   return (Node);
281 }
282
283 /*----------------------------------------------------------------------------------------*/
284 /**
285  * Implement the hardware method of doing Socket Naming, by accessing this northbridge's Socket Id register.
286  *
287  * @HtNbMethod{::F_GET_SOCKET}
288  *
289  * The Socket Id is strapped to the Sbi Control Register, F3X1E4[6:4]SbiAddr.
290  *
291  * @param[in]   Node    The node for which we want the socket id.
292  * @param[in]   TempNode The temporary node id route where the node can be accessed.
293  * @param[in]   Nb      Our Northbridge.
294  *
295  * @return      The Socket Id
296  */
297 UINT8
298 Fam15StrappedGetSocket (
299   IN       UINT8       Node,
300   IN       UINT8       TempNode,
301   IN       NORTHBRIDGE *Nb
302   )
303 {
304   UINT32 Socket;
305   PCI_ADDR Reg;
306
307   ASSERT ((TempNode < MAX_NODES));
308   ASSERT ((Node < MAX_NODES));
309   // Read SbiAddr
310   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (TempNode),
311                                  MakePciBusFromNode (TempNode),
312                                  MakePciDeviceFromNode (TempNode),
313                                  CPU_NB_FUNC_03,
314                                  REG_NB_SBI_CONTROL_3X1E4);
315   LibAmdPciReadBits (Reg, 6, 4, &Socket, Nb->ConfigHandle);
316   return ((UINT8) Socket);
317 }
318
319 /*----------------------------------------------------------------------------------------*/
320 /**
321  * Get the enable compute unit status for this node.
322  *
323  * @HtNbMethod{::F_GET_ENABLED_COMPUTE_UNITS}
324  *
325  * @param[in]   Node    The node for which we want the enabled compute units.
326  * @param[in]   Nb      Our Northbridge.
327  *
328  * @return      The Enabled Compute Unit value
329  */
330 UINT8
331 Fam15GetEnabledComputeUnits (
332   IN       UINT8       Node,
333   IN       NORTHBRIDGE *Nb
334   )
335 {
336   UINT32 Enabled;
337   PCI_ADDR Reg;
338
339   ASSERT ((Node < MAX_NODES));
340
341   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
342                                  MakePciBusFromNode (Node),
343                                  MakePciDeviceFromNode (Node),
344                                  CPU_NB_FUNC_05,
345                                  REG_NB_COMPUTE_UNIT_5X80);
346   LibAmdPciReadBits (Reg, 3, 0, &Enabled, Nb->ConfigHandle);
347   return ((UINT8) Enabled);
348 }
349
350 /*----------------------------------------------------------------------------------------*/
351 /**
352  * Get the dual core compute unit status for this node.
353  *
354  * @HtNbMethod{::PF_GET_DUALCORE_COMPUTE_UNITS}
355  *
356  * @param[in]   Node    The node for which we want the dual core status
357  * @param[in]   Nb      Our Northbridge.
358  *
359  * @return      The dual core compute unit status.
360  */
361 UINT8
362 Fam15GetDualcoreComputeUnits (
363   IN       UINT8       Node,
364   IN       NORTHBRIDGE *Nb
365   )
366 {
367   UINT32 Dual;
368   PCI_ADDR Reg;
369
370   ASSERT ((Node < MAX_NODES));
371
372   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
373                                  MakePciBusFromNode (Node),
374                                  MakePciDeviceFromNode (Node),
375                                  CPU_NB_FUNC_05,
376                                  REG_NB_COMPUTE_UNIT_5X80);
377   LibAmdPciReadBits (Reg, 19, 16, &Dual, Nb->ConfigHandle);
378   return ((UINT8) Dual);
379 }
380
381 /*----------------------------------------------------------------------------------------*/
382 /**
383  * Post info to AP cores via a mailbox.
384  *
385  * @HtNbMethod{::F_POST_MAILBOX}
386  *
387  * Use the link MCA counter register as a PCI -> MSR mailbox, for info such as node id,
388  * and module info.
389  *
390  * @param[in]     Node          the Node
391  * @param[in]     ApMailboxes   The info to post
392  * @param[in]     Nb            this northbridge
393  *
394  */
395 VOID
396 Fam15PostMailbox (
397   IN       UINT8 Node,
398   IN       AP_MAILBOXES ApMailboxes,
399   IN       NORTHBRIDGE *Nb
400   )
401 {
402   PCI_ADDR Reg;
403
404   ASSERT (Node < MAX_NODES);
405
406   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
407                                  MakePciBusFromNode (Node),
408                                  MakePciDeviceFromNode (Node),
409                                  CPU_NB_FUNC_03,
410                                  REG_NB_MCA_LINK_THRESHOLD_3X168);
411   LibAmdPciWriteBits (Reg, 11, 0, &ApMailboxes.ApMailInfo.Info, Nb->ConfigHandle);
412   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
413                                  MakePciBusFromNode (Node),
414                                  MakePciDeviceFromNode (Node),
415                                  CPU_NB_FUNC_03,
416                                  REG_NB_MCA_L3_THRESHOLD_3X170);
417   LibAmdPciWriteBits (Reg, 11, 0, &ApMailboxes.ApMailExtInfo.Info, Nb->ConfigHandle);
418 }
419
420 /*----------------------------------------------------------------------------------------*/
421 /**
422  * Retrieve info from a node's mailbox.
423  *
424  * @HtNbMethod{::F_RETRIEVE_MAILBOX}
425  *
426  * Use the link MCA counter register as a PCI -> MSR mailbox, for info such as node id,
427  * and module info.
428  *
429  * @param[in]     Node          the Node
430  * @param[in]     Nb            this northbridge
431  *
432  * @return        The ap mailbox info
433  *
434  */
435 AP_MAIL_INFO
436 Fam15RetrieveMailbox (
437   IN       UINT8 Node,
438   IN       NORTHBRIDGE *Nb
439   )
440 {
441   PCI_ADDR Reg;
442   AP_MAIL_INFO ApMailInfo;
443
444   ASSERT (Node < MAX_NODES);
445
446   Reg.AddressValue = MAKE_SBDFO (MakePciSegmentFromNode (Node),
447                                  MakePciBusFromNode (Node),
448                                  MakePciDeviceFromNode (Node),
449                                  CPU_NB_FUNC_03,
450                                  REG_NB_MCA_LINK_THRESHOLD_3X168);
451   LibAmdPciReadBits (Reg, 11, 0, &ApMailInfo.Info, Nb->ConfigHandle);
452   return ApMailInfo;
453 }