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