Move includes to where they are needed. This allows to simplify
[coreboot.git] / src / cpu / amd / model_10xxx / init_cpus.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include "defaults.h"
21 #include <stdlib.h>
22 #include <cpu/x86/lapic.h>
23 #include <cpu/x86/mtrr.h>
24 #include <northbridge/amd/amdfam10/amdfam10.h>
25 #include <northbridge/amd/amdht/AsPsDefs.h>
26 #include <northbridge/amd/amdht/porting.h>
27
28 //it takes the CONFIG_ENABLE_APIC_EXT_ID and CONFIG_APIC_ID_OFFSET and CONFIG_LIFT_BSP_APIC_ID
29 #ifndef SET_FIDVID
30         #define SET_FIDVID 1
31 #endif
32
33 #ifndef SET_FIDVID_CORE0_ONLY
34         /* MSR FIDVID_CTL and FIDVID_STATUS are shared by cores,
35            Need to do every AP to set common FID/VID */
36         #define SET_FIDVID_CORE0_ONLY 0
37 #endif
38
39 static void prep_fid_change(void);
40 static void init_fidvid_stage2(u32 apicid, u32 nodeid);
41 void cpuSetAMDMSR(void);
42
43 #if CONFIG_PCI_IO_CFG_EXT == 1
44 static void set_EnableCf8ExtCfg(void)
45 {
46         // set the NB_CFG[46]=1;
47         msr_t msr;
48         msr = rdmsr(NB_CFG_MSR);
49         // EnableCf8ExtCfg: We need that to access CONFIG_PCI_IO_CFG_EXT 4K range
50         msr.hi |= (1 << (46 - 32));
51         wrmsr(NB_CFG_MSR, msr);
52 }
53 #else
54 static void set_EnableCf8ExtCfg(void) { }
55 #endif
56
57 /*[39:8] */
58 #define PCI_MMIO_BASE 0xfe000000
59 /* because we will use gs to store hi, so need to make sure lo can start
60    from 0, So PCI_MMIO_BASE & 0x00ffffff should be equal to 0*/
61
62 static void set_pci_mmio_conf_reg(void)
63 {
64 #if CONFIG_MMCONF_SUPPORT
65         msr_t msr;
66         msr = rdmsr(0xc0010058);
67         msr.lo &= ~(0xfff00000 | (0xf << 2));
68         // 256 bus per segment, MMIO reg will be 4G , enable MMIO Config space
69         msr.lo |= ((8 + CONFIG_PCI_BUS_SEGN_BITS) << 2) | (1 << 0);
70         msr.hi &= ~(0x0000ffff);
71         msr.hi |= (PCI_MMIO_BASE >> (32 - 8));
72         wrmsr(0xc0010058, msr); // MMIO Config Base Address Reg
73
74         //mtrr for that range?
75         // set_var_mtrr_x(7, PCI_MMIO_BASE<<8, PCI_MMIO_BASE>>(32-8), 0x00000000, 0x01, MTRR_TYPE_UNCACHEABLE);
76
77         set_wrap32dis();
78
79         msr.hi = (PCI_MMIO_BASE >> (32 - 8));
80         msr.lo = 0;
81         wrmsr(0xc0000101, msr); //GS_Base Reg
82
83 #endif
84 }
85
86 typedef void (*process_ap_t) (u32 apicid, void *gp);
87
88 //core_range = 0 : all cores
89 //core range = 1 : core 0 only
90 //core range = 2 : cores other than core0
91
92 static void for_each_ap(u32 bsp_apicid, u32 core_range, process_ap_t process_ap,
93                         void *gp)
94 {
95         // here assume the OS don't change our apicid
96         u32 ap_apicid;
97
98         u32 nodes;
99         u32 siblings;
100         u32 disable_siblings;
101         u32 cores_found;
102         u32 nb_cfg_54;
103         int i, j;
104         u32 ApicIdCoreIdSize;
105
106         /* get_nodes define in ht_wrapper.c */
107         nodes = get_nodes();
108
109         disable_siblings = !CONFIG_LOGICAL_CPUS;
110
111 #if CONFIG_LOGICAL_CPUS == 1 && CONFIG_HAVE_OPTION_TABLE == 1
112         if (read_option(CMOS_VSTART_multi_core, CMOS_VLEN_multi_core, 0) != 0) {        // 0 mean multi core
113                 disable_siblings = 1;
114         }
115 #endif
116
117         /* Assume that all node are same stepping, otherwise we can use use
118            nb_cfg_54 from bsp for all nodes */
119         nb_cfg_54 = read_nb_cfg_54();
120
121         ApicIdCoreIdSize = (cpuid_ecx(0x80000008) >> 12 & 0xf);
122         if (ApicIdCoreIdSize) {
123                 siblings = ((1 << ApicIdCoreIdSize) - 1);
124         } else {
125                 siblings = 3;   //quad core
126         }
127
128         for (i = 0; i < nodes; i++) {
129                 cores_found = get_core_num_in_bsp(i);
130
131                 u32 jstart, jend;
132
133                 if (core_range == 2) {
134                         jstart = 1;
135                 } else {
136                         jstart = 0;
137                 }
138
139                 if (disable_siblings || (core_range == 1)) {
140                         jend = 0;
141                 } else {
142                         jend = cores_found;
143                 }
144
145                 for (j = jstart; j <= jend; j++) {
146                         ap_apicid =
147                             i * (nb_cfg_54 ? (siblings + 1) : 1) +
148                             j * (nb_cfg_54 ? 1 : 64);
149
150 #if (CONFIG_ENABLE_APIC_EXT_ID == 1) && (CONFIG_APIC_ID_OFFSET > 0)
151 #if CONFIG_LIFT_BSP_APIC_ID == 0
152                         if ((i != 0) || (j != 0))       /* except bsp */
153 #endif
154                                 ap_apicid += CONFIG_APIC_ID_OFFSET;
155 #endif
156
157                         if (ap_apicid == bsp_apicid)
158                                 continue;
159
160                         process_ap(ap_apicid, gp);
161
162                 }
163         }
164 }
165
166 static inline int lapic_remote_read(int apicid, int reg, u32 *pvalue)
167 {
168         int timeout;
169         u32 status;
170         int result;
171         lapic_wait_icr_idle();
172         lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
173         lapic_write(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));
174
175 /* Extra busy check compared to lapic.h */
176         timeout = 0;
177         do {
178                 status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
179         } while (status == LAPIC_ICR_BUSY && timeout++ < 1000);
180
181         timeout = 0;
182         do {
183                 status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
184         } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);
185
186         result = -1;
187
188         if (status == LAPIC_ICR_RR_VALID) {
189                 *pvalue = lapic_read(LAPIC_RRR);
190                 result = 0;
191         }
192         return result;
193 }
194
195 /* Use the LAPIC timer count register to hold each cores init status */
196 #define LAPIC_MSG_REG 0x380
197
198 #if SET_FIDVID == 1
199 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid);
200 #endif
201
202 static inline __attribute__ ((always_inline))
203 void print_apicid_nodeid_coreid(u32 apicid, struct node_core_id id,
204                                 const char *str)
205 {
206         printk(BIOS_DEBUG,
207                "%s --- { APICID = %02x NODEID = %02x COREID = %02x} ---\n", str,
208                apicid, id.nodeid, id.coreid);
209 }
210
211 static u32 wait_cpu_state(u32 apicid, u32 state)
212 {
213         u32 readback = 0;
214         u32 timeout = 1;
215         int loop = 4000000;
216         while (--loop > 0) {
217                 if (lapic_remote_read(apicid, LAPIC_MSG_REG, &readback) != 0)
218                         continue;
219                 if ((readback & 0x3f) == state) {
220                         timeout = 0;
221                         break;  //target cpu is in stage started
222                 }
223         }
224         if (timeout) {
225                 if (readback) {
226                         timeout = readback;
227                 }
228         }
229
230         return timeout;
231 }
232
233 static void wait_ap_started(u32 ap_apicid, void *gp)
234 {
235         u32 timeout;
236         timeout = wait_cpu_state(ap_apicid, 0x13);      // started
237         printk(BIOS_DEBUG, "* AP %02x", ap_apicid);
238         if (timeout) {
239                 printk(BIOS_DEBUG, " timed out:%08x\n", timeout);
240         } else {
241                 printk(BIOS_DEBUG, "started\n");
242         }
243 }
244
245 static void wait_all_other_cores_started(u32 bsp_apicid)
246 {
247         // all aps other than core0
248         printk(BIOS_DEBUG, "started ap apicid: ");
249         for_each_ap(bsp_apicid, 2, wait_ap_started, (void *)0);
250         printk(BIOS_DEBUG, "\n");
251 }
252
253 static void allow_all_aps_stop(u32 bsp_apicid)
254 {
255         /* Called by the BSP to indicate AP can stop */
256
257         /* FIXME Do APs use this?
258            Looks like wait_till_sysinfo_in_ram is used instead. */
259
260         // allow aps to stop use 6 bits for state
261         lapic_write(LAPIC_MSG_REG, (bsp_apicid << 24) | 0x14);
262 }
263
264 static void enable_apic_ext_id(u32 node)
265 {
266         u32 val;
267
268         val = pci_read_config32(NODE_HT(node), 0x68);
269         val |= (HTTC_APIC_EXT_SPUR | HTTC_APIC_EXT_ID | HTTC_APIC_EXT_BRD_CST);
270         pci_write_config32(NODE_HT(node), 0x68, val);
271 }
272
273 static void STOP_CAR_AND_CPU(void)
274 {
275         msr_t msr;
276
277         /* Disable L2 IC to L3 connection (Only for CAR) */
278         msr = rdmsr(BU_CFG2);
279         msr.lo &= ~(1 << ClLinesToNbDis);
280         wrmsr(BU_CFG2, msr);
281
282         disable_cache_as_ram(); // inline
283         /* stop all cores except node0/core0 the bsp .... */
284         stop_this_cpu();
285 }
286
287 #if RAMINIT_SYSINFO == 1
288 static u32 init_cpus(u32 cpu_init_detectedx, struct sys_info *sysinfo)
289 #else
290 static u32 init_cpus(u32 cpu_init_detectedx)
291 #endif
292 {
293         u32 bsp_apicid = 0;
294         u32 apicid;
295         struct node_core_id id;
296
297         /*
298          * already set early mtrr in cache_as_ram.inc
299          */
300
301         /* enable access pci conf via mmio */
302         set_pci_mmio_conf_reg();
303
304         /* that is from initial apicid, we need nodeid and coreid
305            later */
306         id = get_node_core_id_x();
307
308         /* NB_CFG MSR is shared between cores, so we need make sure
309            core0 is done at first --- use wait_all_core0_started  */
310         if (id.coreid == 0) {
311                 set_apicid_cpuid_lo();  /* only set it on core0 */
312                 set_EnableCf8ExtCfg();  /* only set it on core0 */
313 #if (CONFIG_ENABLE_APIC_EXT_ID == 1)
314                 enable_apic_ext_id(id.nodeid);
315 #endif
316         }
317
318         enable_lapic();
319
320 #if (CONFIG_ENABLE_APIC_EXT_ID == 1) && (CONFIG_APIC_ID_OFFSET > 0)
321         u32 initial_apicid = get_initial_apicid();
322
323 #if CONFIG_LIFT_BSP_APIC_ID == 0
324         if (initial_apicid != 0)        // other than bsp
325 #endif
326         {
327                 /* use initial apic id to lift it */
328                 u32 dword = lapic_read(LAPIC_ID);
329                 dword &= ~(0xff << 24);
330                 dword |=
331                     (((initial_apicid + CONFIG_APIC_ID_OFFSET) & 0xff) << 24);
332
333                 lapic_write(LAPIC_ID, dword);
334         }
335 #if CONFIG_LIFT_BSP_APIC_ID == 1
336         bsp_apicid += CONFIG_APIC_ID_OFFSET;
337 #endif
338
339 #endif
340
341         /* get the apicid, it may be lifted already */
342         apicid = lapicid();
343
344         // show our apicid, nodeid, and coreid
345         if (id.coreid == 0) {
346                 if (id.nodeid != 0)     //all core0 except bsp
347                         print_apicid_nodeid_coreid(apicid, id, " core0: ");
348         } else {                //all other cores
349                 print_apicid_nodeid_coreid(apicid, id, " corex: ");
350         }
351
352         if (cpu_init_detectedx) {
353                 print_apicid_nodeid_coreid(apicid, id,
354                                            "\n\n\nINIT detected from ");
355                 printk(BIOS_DEBUG, "\nIssuing SOFT_RESET...\n");
356                 soft_reset();
357         }
358
359         if (id.coreid == 0) {
360                 if (!(warm_reset_detect(id.nodeid)))    //FIXME: INIT is checked above but check for more resets?
361                         distinguish_cpu_resets(id.nodeid);      // Also indicates we are started
362         }
363         // Mark the core as started.
364         lapic_write(LAPIC_MSG_REG, (apicid << 24) | 0x13);
365
366         if (apicid != bsp_apicid) {
367                 /* Setup each AP's cores MSRs.
368                  * This happens after HTinit.
369                  * The BSP runs this code in it's own path.
370                  */
371                 update_microcode(cpuid_eax(1));
372                 cpuSetAMDMSR();
373
374 #if SET_FIDVID == 1
375 #if (CONFIG_LOGICAL_CPUS == 1) && (SET_FIDVID_CORE0_ONLY == 1)
376                 // Run on all AP for proper FID/VID setup.
377                 if (id.coreid == 0)     // only need set fid for core0
378 #endif
379                 {
380                         // check warm(bios) reset to call stage2 otherwise do stage1
381                         if (warm_reset_detect(id.nodeid)) {
382                                 printk(BIOS_DEBUG,
383                                        "init_fidvid_stage2 apicid: %02x\n",
384                                        apicid);
385                                 init_fidvid_stage2(apicid, id.nodeid);
386                         } else {
387                                 printk(BIOS_DEBUG,
388                                        "init_fidvid_ap(stage1) apicid: %02x\n",
389                                        apicid);
390                                 init_fidvid_ap(bsp_apicid, apicid, id.nodeid,
391                                                id.coreid);
392                         }
393                 }
394 #endif
395
396                 /* AP is ready, Wait for the BSP to get memory configured */
397                 /* FIXME: many cores spinning on node0 pci register seems to be bad.
398                  * Why do we need to wait? These APs are just going to go sit in a hlt.
399                  */
400                 //wait_till_sysinfo_in_ram();
401
402                 set_var_mtrr(0, 0x00000000, CONFIG_RAMTOP, MTRR_TYPE_WRBACK);
403
404                 STOP_CAR_AND_CPU();
405                 printk(BIOS_DEBUG,
406                        "\nAP %02x should be halted but you are reading this....\n",
407                        apicid);
408         }
409
410         return bsp_apicid;
411 }
412
413 static u32 is_core0_started(u32 nodeid)
414 {
415         u32 htic;
416         device_t device;
417         device = NODE_PCI(nodeid, 0);
418         htic = pci_read_config32(device, HT_INIT_CONTROL);
419         htic &= HTIC_ColdR_Detect;
420         return htic;
421 }
422
423 static void wait_all_core0_started(void)
424 {
425         /* When core0 is started, it will distingush_cpu_resets
426          * So wait for that to finish */
427         u32 i;
428         u32 nodes = get_nodes();
429
430         printk(BIOS_DEBUG, "core0 started: ");
431         for (i = 1; i < nodes; i++) {   // skip bsp, because it is running on bsp
432                 while (!is_core0_started(i)) {
433                 }
434                 printk(BIOS_DEBUG, " %02x", i);
435         }
436         printk(BIOS_DEBUG, "\n");
437 }
438
439 #if CONFIG_MAX_PHYSICAL_CPUS > 1
440 /**
441  * void start_node(u32 node)
442  *
443  *  start the core0 in node, so it can generate HT packet to feature code.
444  *
445  * This function starts the AP nodes core0s. wait_all_core0_started() in
446  * romstage.c waits for all the AP to be finished before continuing
447  * system init.
448  */
449 static void start_node(u8 node)
450 {
451         u32 val;
452
453         /* Enable routing table */
454         printk(BIOS_DEBUG, "Start node %02x", node);
455
456 #if CONFIG_NORTHBRIDGE_AMD_AMDFAM10
457         /* For FAM10 support, we need to set Dram base/limit for the new node */
458         pci_write_config32(NODE_MP(node), 0x44, 0);
459         pci_write_config32(NODE_MP(node), 0x40, 3);
460 #endif
461
462         /* Allow APs to make requests (ROM fetch) */
463         val = pci_read_config32(NODE_HT(node), 0x6c);
464         val &= ~(1 << 1);
465         pci_write_config32(NODE_HT(node), 0x6c, val);
466
467         printk(BIOS_DEBUG, " done.\n");
468 }
469
470 /**
471  * static void setup_remote_node(u32 node)
472  *
473  * Copy the BSP Adress Map to each AP.
474  */
475 static void setup_remote_node(u8 node)
476 {
477         /* There registers can be used with F1x114_x Address Map at the
478            same time, So must set them even 32 node */
479         static const u16 pci_reg[] = {
480                 /* DRAM Base/Limits Registers */
481                 0x44, 0x4c, 0x54, 0x5c, 0x64, 0x6c, 0x74, 0x7c,
482                 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78,
483                 0x144, 0x14c, 0x154, 0x15c, 0x164, 0x16c, 0x174, 0x17c,
484                 0x140, 0x148, 0x150, 0x158, 0x160, 0x168, 0x170, 0x178,
485                 /* MMIO Base/Limits Registers */
486                 0x84, 0x8c, 0x94, 0x9c, 0xa4, 0xac, 0xb4, 0xbc,
487                 0x80, 0x88, 0x90, 0x98, 0xa0, 0xa8, 0xb0, 0xb8,
488                 /* IO Base/Limits Registers */
489                 0xc4, 0xcc, 0xd4, 0xdc,
490                 0xc0, 0xc8, 0xd0, 0xd8,
491                 /* Configuration Map Registers */
492                 0xe0, 0xe4, 0xe8, 0xec,
493         };
494         u16 i;
495
496         printk(BIOS_DEBUG, "setup_remote_node: %02x", node);
497
498         /* copy the default resource map from node 0 */
499         for (i = 0; i < ARRAY_SIZE(pci_reg); i++) {
500                 u32 value;
501                 u16 reg;
502                 reg = pci_reg[i];
503                 value = pci_read_config32(NODE_MP(0), reg);
504                 pci_write_config32(NODE_MP(node), reg, value);
505
506         }
507         printk(BIOS_DEBUG, " done\n");
508 }
509 #endif                          /* CONFIG_MAX_PHYSICAL_CPUS > 1 */
510
511 static void AMD_Errata281(u8 node, u32 revision, u32 platform)
512 {
513         /* Workaround for Transaction Scheduling Conflict in
514          * Northbridge Cross Bar.  Implement XCS Token adjustment
515          * for ganged links.  Also, perform fix up for the mixed
516          * revision case.
517          */
518
519         u32 reg, val;
520         u8 i;
521         u8 mixed = 0;
522         u8 nodes = get_nodes();
523
524         if (platform & AMD_PTYPE_SVR) {
525                 /* For each node we need to check for a "broken" node */
526                 if (!(revision & (AMD_DR_B0 | AMD_DR_B1))) {
527                         for (i = 0; i < nodes; i++) {
528                                 if (mctGetLogicalCPUID(i) &
529                                     (AMD_DR_B0 | AMD_DR_B1)) {
530                                         mixed = 1;
531                                         break;
532                                 }
533                         }
534                 }
535
536                 if ((revision & (AMD_DR_B0 | AMD_DR_B1)) || mixed) {
537
538                         /* F0X68[22:21] DsNpReqLmt0 = 01b */
539                         val = pci_read_config32(NODE_PCI(node, 0), 0x68);
540                         val &= ~0x00600000;
541                         val |= 0x00200000;
542                         pci_write_config32(NODE_PCI(node, 0), 0x68, val);
543
544                         /* F3X6C */
545                         val = pci_read_config32(NODE_PCI(node, 3), 0x6C);
546                         val &= ~0x700780F7;
547                         val |= 0x00010094;
548                         pci_write_config32(NODE_PCI(node, 3), 0x6C, val);
549
550                         /* F3X7C */
551                         val = pci_read_config32(NODE_PCI(node, 3), 0x7C);
552                         val &= ~0x707FFF1F;
553                         val |= 0x00144514;
554                         pci_write_config32(NODE_PCI(node, 3), 0x7C, val);
555
556                         /* F3X144[3:0] RspTok = 0001b */
557                         val = pci_read_config32(NODE_PCI(node, 3), 0x144);
558                         val &= ~0x0000000F;
559                         val |= 0x00000001;
560                         pci_write_config32(NODE_PCI(node, 3), 0x144, val);
561
562                         for (i = 0; i < 3; i++) {
563                                 reg = 0x148 + (i * 4);
564                                 val = pci_read_config32(NODE_PCI(node, 3), reg);
565                                 val &= ~0x000000FF;
566                                 val |= 0x000000DB;
567                                 pci_write_config32(NODE_PCI(node, 3), reg, val);
568                         }
569                 }
570         }
571 }
572
573 static void AMD_Errata298(void)
574 {
575         /* Workaround for L2 Eviction May Occur during operation to
576          * set Accessed or dirty bit.
577          */
578
579         msr_t msr;
580         u8 i;
581         u8 affectedRev = 0;
582         u8 nodes = get_nodes();
583
584         /* For each core we need to check for a "broken" node */
585         for (i = 0; i < nodes; i++) {
586                 if (mctGetLogicalCPUID(i) & (AMD_DR_B0 | AMD_DR_B1 | AMD_DR_B2)) {
587                         affectedRev = 1;
588                         break;
589                 }
590         }
591
592         if (affectedRev) {
593                 msr = rdmsr(HWCR);
594                 msr.lo |= 0x08; /* Set TlbCacheDis bit[3] */
595                 wrmsr(HWCR, msr);
596
597                 msr = rdmsr(BU_CFG);
598                 msr.lo |= 0x02; /* Set TlbForceMemTypeUc bit[1] */
599                 wrmsr(BU_CFG, msr);
600
601                 msr = rdmsr(OSVW_ID_Length);
602                 msr.lo |= 0x01; /* OS Visible Workaround - MSR */
603                 wrmsr(OSVW_ID_Length, msr);
604
605                 msr = rdmsr(OSVW_Status);
606                 msr.lo |= 0x01; /* OS Visible Workaround - MSR */
607                 wrmsr(OSVW_Status, msr);
608         }
609
610         if (!affectedRev && (mctGetLogicalCPUID(0xFF) & AMD_DR_B3)) {
611                 msr = rdmsr(OSVW_ID_Length);
612                 msr.lo |= 0x01; /* OS Visible Workaround - MSR */
613                 wrmsr(OSVW_ID_Length, msr);
614
615         }
616 }
617
618 static u32 get_platform_type(void)
619 {
620         u32 ret = 0;
621
622         switch (SYSTEM_TYPE) {
623         case 1:
624                 ret |= AMD_PTYPE_DSK;
625                 break;
626         case 2:
627                 ret |= AMD_PTYPE_MOB;
628                 break;
629         case 0:
630                 ret |= AMD_PTYPE_SVR;
631                 break;
632         default:
633                 break;
634         }
635
636         /* FIXME: add UMA support. */
637
638         /* All Fam10 are multi core */
639         ret |= AMD_PTYPE_MC;
640
641         return ret;
642 }
643
644 static void AMD_SetupPSIVID_d(u32 platform_type, u8 node)
645 {
646         u32 dword;
647         int i;
648         msr_t msr;
649
650         if (platform_type & (AMD_PTYPE_MOB | AMD_PTYPE_DSK)) {
651
652                 /* The following code sets the PSIVID to the lowest support P state
653                  * assuming that the VID for the lowest power state is below
654                  * the VDD voltage regulator threshold. (This also assumes that there
655                  * is a Pstate lower than P0)
656                  */
657
658                 for (i = 4; i >= 0; i--) {
659                         msr = rdmsr(PS_REG_BASE + i);
660                         /*  Pstate valid? */
661                         if (msr.hi & PS_EN_MASK) {
662                                 dword = pci_read_config32(NODE_PCI(i, 3), 0xA0);
663                                 dword &= ~0x7F;
664                                 dword |= (msr.lo >> 9) & 0x7F;
665                                 pci_write_config32(NODE_PCI(i, 3), 0xA0, dword);
666                                 break;
667                         }
668                 }
669         }
670 }
671
672 /**
673  * AMD_CpuFindCapability - Traverse PCI capability list to find host HT links.
674  *  HT Phy operations are not valid on links that aren't present, so this
675  *  prevents invalid accesses.
676  *
677  * Returns the offset of the link register.
678  */
679 static BOOL AMD_CpuFindCapability(u8 node, u8 cap_count, u8 * offset)
680 {
681         u32 reg;
682         u32 val;
683
684         /* get start of CPU HT Host Capabilities */
685         val = pci_read_config32(NODE_PCI(node, 0), 0x34);
686         val &= 0xFF;            //reg offset of first link
687
688         cap_count++;
689
690         /* Traverse through the capabilities. */
691         do {
692                 reg = pci_read_config32(NODE_PCI(node, 0), val);
693                 /* Is the capability block a HyperTransport capability block? */
694                 if ((reg & 0xFF) == 0x08) {
695                         /* Is the HT capability block an HT Host Capability? */
696                         if ((reg & 0xE0000000) == (1 << 29))
697                                 cap_count--;
698                 }
699
700                 if (cap_count)
701                         val = (reg >> 8) & 0xFF;        //update reg offset
702         } while (cap_count && val);
703
704         *offset = (u8) val;
705
706         /* If requested capability found val != 0 */
707         if (!cap_count)
708                 return TRUE;
709         else
710                 return FALSE;
711 }
712
713 /**
714  * AMD_checkLinkType - Compare desired link characteristics using a logical
715  *     link type mask.
716  *
717  * Returns the link characteristic mask.
718  */
719 static u32 AMD_checkLinkType(u8 node, u8 link, u8 regoff)
720 {
721         u32 val;
722         u32 linktype = 0;
723
724         /* Check connect, init and coherency */
725         val = pci_read_config32(NODE_PCI(node, 0), regoff + 0x18);
726         val &= 0x1F;
727
728         if (val == 3)
729                 linktype |= HTPHY_LINKTYPE_COHERENT;
730
731         if (val == 7)
732                 linktype |= HTPHY_LINKTYPE_NONCOHERENT;
733
734         if (linktype) {
735                 /* Check gen3 */
736                 val = pci_read_config32(NODE_PCI(node, 0), regoff + 0x08);
737
738                 if (((val >> 8) & 0x0F) > 6)
739                         linktype |= HTPHY_LINKTYPE_HT3;
740                 else
741                         linktype |= HTPHY_LINKTYPE_HT1;
742
743                 /* Check ganged */
744                 val = pci_read_config32(NODE_PCI(node, 0), (link << 2) + 0x170);
745
746                 if (val & 1)
747                         linktype |= HTPHY_LINKTYPE_GANGED;
748                 else
749                         linktype |= HTPHY_LINKTYPE_UNGANGED;
750         }
751         return linktype;
752 }
753
754 /**
755  * AMD_SetHtPhyRegister - Use the HT link's HT Phy portal registers to update
756  *   a phy setting for that link.
757  */
758 static void AMD_SetHtPhyRegister(u8 node, u8 link, u8 entry)
759 {
760         u32 phyReg;
761         u32 phyBase;
762         u32 val;
763
764         /* Determine this link's portal */
765         if (link > 3)
766                 link -= 4;
767
768         phyBase = ((u32) link << 3) | 0x180;
769
770         /* Get the portal control register's initial value
771          * and update it to access the desired phy register
772          */
773         phyReg = pci_read_config32(NODE_PCI(node, 4), phyBase);
774
775         if (fam10_htphy_default[entry].htreg > 0x1FF) {
776                 phyReg &= ~HTPHY_DIRECT_OFFSET_MASK;
777                 phyReg |= HTPHY_DIRECT_MAP;
778         } else {
779                 phyReg &= ~HTPHY_OFFSET_MASK;
780         }
781
782         /* Now get the current phy register data
783          * LinkPhyDone = 0, LinkPhyWrite = 0 is a read
784          */
785         phyReg |= fam10_htphy_default[entry].htreg;
786         pci_write_config32(NODE_PCI(node, 4), phyBase, phyReg);
787
788         do {
789                 val = pci_read_config32(NODE_PCI(node, 4), phyBase);
790         } while (!(val & HTPHY_IS_COMPLETE_MASK));
791
792         /* Now we have the phy register data, apply the change */
793         val = pci_read_config32(NODE_PCI(node, 4), phyBase + 4);
794         val &= ~fam10_htphy_default[entry].mask;
795         val |= fam10_htphy_default[entry].data;
796         pci_write_config32(NODE_PCI(node, 4), phyBase + 4, val);
797
798         /* write it through the portal to the phy
799          * LinkPhyDone = 0, LinkPhyWrite = 1 is a write
800          */
801         phyReg |= HTPHY_WRITE_CMD;
802         pci_write_config32(NODE_PCI(node, 4), phyBase, phyReg);
803
804         do {
805                 val = pci_read_config32(NODE_PCI(node, 4), phyBase);
806         } while (!(val & HTPHY_IS_COMPLETE_MASK));
807 }
808
809 void cpuSetAMDMSR(void)
810 {
811         /* This routine loads the CPU with default settings in fam10_msr_default
812          * table . It must be run after Cache-As-RAM has been enabled, and
813          * Hypertransport initialization has taken place.  Also note
814          * that it is run on the current processor only, and only for the current
815          * processor core.
816          */
817         msr_t msr;
818         u8 i;
819         u32 revision, platform;
820
821         printk(BIOS_DEBUG, "cpuSetAMDMSR ");
822
823         revision = mctGetLogicalCPUID(0xFF);
824         platform = get_platform_type();
825
826         for (i = 0; i < ARRAY_SIZE(fam10_msr_default); i++) {
827                 if ((fam10_msr_default[i].revision & revision) &&
828                     (fam10_msr_default[i].platform & platform)) {
829                         msr = rdmsr(fam10_msr_default[i].msr);
830                         msr.hi &= ~fam10_msr_default[i].mask_hi;
831                         msr.hi |= fam10_msr_default[i].data_hi;
832                         msr.lo &= ~fam10_msr_default[i].mask_lo;
833                         msr.lo |= fam10_msr_default[i].data_lo;
834                         wrmsr(fam10_msr_default[i].msr, msr);
835                 }
836         }
837         AMD_Errata298();
838
839         printk(BIOS_DEBUG, " done\n");
840 }
841
842 static void cpuSetAMDPCI(u8 node)
843 {
844         /* This routine loads the CPU with default settings in fam10_pci_default
845          * table . It must be run after Cache-As-RAM has been enabled, and
846          * Hypertransport initialization has taken place.  Also note
847          * that it is run for the first core on each node
848          */
849         u8 i, j;
850         u32 revision, platform;
851         u32 val;
852         u8 offset;
853
854         printk(BIOS_DEBUG, "cpuSetAMDPCI %02d", node);
855
856         revision = mctGetLogicalCPUID(node);
857         platform = get_platform_type();
858
859         AMD_SetupPSIVID_d(platform, node);      /* Set PSIVID offset which is not table driven */
860
861         for (i = 0; i < ARRAY_SIZE(fam10_pci_default); i++) {
862                 if ((fam10_pci_default[i].revision & revision) &&
863                     (fam10_pci_default[i].platform & platform)) {
864                         val = pci_read_config32(NODE_PCI(node,
865                                                          fam10_pci_default[i].
866                                                          function),
867                                                 fam10_pci_default[i].offset);
868                         val &= ~fam10_pci_default[i].mask;
869                         val |= fam10_pci_default[i].data;
870                         pci_write_config32(NODE_PCI(node,
871                                                     fam10_pci_default[i].
872                                                     function),
873                                            fam10_pci_default[i].offset, val);
874                 }
875         }
876
877         for (i = 0; i < ARRAY_SIZE(fam10_htphy_default); i++) {
878                 if ((fam10_htphy_default[i].revision & revision) &&
879                     (fam10_htphy_default[i].platform & platform)) {
880                         /* HT Phy settings either apply to both sublinks or have
881                          * separate registers for sublink zero and one, so there
882                          * will be two table entries. So, here we only loop
883                          cd t   * through the sublink zeros in function zero.
884                          */
885                         for (j = 0; j < 4; j++) {
886                                 if (AMD_CpuFindCapability(node, j, &offset)) {
887                                         if (AMD_checkLinkType(node, j, offset)
888                                             & fam10_htphy_default[i].linktype) {
889                                                 AMD_SetHtPhyRegister(node, j,
890                                                                      i);
891                                         }
892                                 } else {
893                                         /* No more capabilities,
894                                          * link not present
895                                          */
896                                         break;
897                                 }
898                         }
899                 }
900         }
901
902         /* FIXME: add UMA support and programXbarToSriReg(); */
903
904         AMD_Errata281(node, revision, platform);
905
906         /* FIXME: if the dct phy doesn't init correct it needs to reset.
907            if (revision & (AMD_DR_B2 | AMD_DR_B3))
908            dctPhyDiag(); */
909
910         printk(BIOS_DEBUG, " done\n");
911 }
912
913 static void cpuInitializeMCA(void)
914 {
915         /* Clears Machine Check Architecture (MCA) registers, which power on
916          * containing unknown data, on currently running processor.
917          * This routine should only be executed on initial power on (cold boot),
918          * not across a warm reset because valid data is present at that time.
919          */
920
921         msr_t msr;
922         u32 reg;
923         u8 i;
924
925         if (cpuid_edx(1) & 0x4080) {    /* MCE and MCA (edx[7] and edx[14]) */
926                 msr = rdmsr(MCG_CAP);
927                 if (msr.lo & MCG_CTL_P) {       /* MCG_CTL_P bit is set? */
928                         msr.lo &= 0xFF;
929                         msr.lo--;
930                         msr.lo <<= 2;   /* multiply the count by 4 */
931                         reg = MC0_STA + msr.lo;
932                         msr.lo = msr.hi = 0;
933                         for (i = 0; i < 4; i++) {
934                                 wrmsr(reg, msr);
935                                 reg -= 4;       /* Touch status regs for each bank */
936                         }
937                 }
938         }
939 }
940
941 /**
942  * finalize_node_setup()
943  *
944  * Do any additional post HT init
945  *
946  */
947 static void finalize_node_setup(struct sys_info *sysinfo)
948 {
949         u8 i;
950         u8 nodes = get_nodes();
951         u32 reg;
952
953 #if RAMINIT_SYSINFO == 1
954         /* read Node0 F0_0x64 bit [8:10] to find out SbLink # */
955         reg = pci_read_config32(NODE_HT(0), 0x64);
956         sysinfo->sblk = (reg >> 8) & 7;
957         sysinfo->sbbusn = 0;
958         sysinfo->nodes = nodes;
959         sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);
960 #endif
961
962         for (i = 0; i < nodes; i++) {
963                 cpuSetAMDPCI(i);
964         }
965
966 #if SET_FIDVID == 1
967         // Prep each node for FID/VID setup.
968         prep_fid_change();
969 #endif
970
971 #if CONFIG_MAX_PHYSICAL_CPUS > 1
972         /* Skip the BSP, start at node 1 */
973         for (i = 1; i < nodes; i++) {
974                 setup_remote_node(i);
975                 start_node(i);
976         }
977 #endif
978 }