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