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