Improving BKDG implementation of P-states,
[coreboot.git] / src / cpu / amd / model_10xxx / fidvid.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 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 #if CONFIG_SET_FIDVID
21 #include <northbridge/amd/amdht/AsPsDefs.h>
22
23 static inline void print_debug_fv(const char *str, u32 val)
24 {
25 #if CONFIG_SET_FIDVID_DEBUG
26         printk(BIOS_DEBUG, "%s%x\n", str, val);
27 #endif
28 }
29
30 static inline void print_debug_fv_8(const char *str, u8 val)
31 {
32 #if CONFIG_SET_FIDVID_DEBUG
33         printk(BIOS_DEBUG, "%s%02x\n", str, val);
34 #endif
35 }
36
37 static inline void print_debug_fv_64(const char *str, u32 val, u32 val2)
38 {
39 #if CONFIG_SET_FIDVID_DEBUG
40         printk(BIOS_DEBUG, "%s%x%x\n", str, val, val2);
41 #endif
42 }
43
44 struct fidvid_st {
45         u32 common_fid;
46 };
47
48 static void enable_fid_change(u8 fid)
49 {
50         u32 dword;
51         u32 nodes;
52         device_t dev;
53         int i;
54
55         nodes = get_nodes();
56
57         for (i = 0; i < nodes; i++) {
58                 dev = NODE_PCI(i, 3);
59                 dword = pci_read_config32(dev, 0xd4);
60                 dword &= ~0x1F;
61                 dword |= (u32) fid & 0x1F;
62                 dword |= 1 << 5;        // enable
63                 pci_write_config32(dev, 0xd4, dword);
64                 printk(BIOS_DEBUG, "FID Change Node:%02x, F3xD4: %08x \n", i,
65                        dword);
66         }
67 }
68
69 static void setVSRamp(device_t dev) {
70         /* BKDG r31116 2010-04-22  2.4.1.7 step b F3xD8[VSRampTime]
71          * If this field accepts 8 values between 10 and 500 us why
72          * does page 324 say "BIOS should set this field to 001b."
73          * (20 us) ?
74          * Shouldn't it depend on the voltage regulators, mainboard
75          * or something ?
76          */
77         u32 dword;
78         dword = pci_read_config32(dev, 0xd8);
79         dword &= VSRAMP_MASK;
80         dword |= VSRAMP_VALUE;
81         pci_write_config32(dev, 0xd8, dword);
82 }
83
84 static void recalculateVsSlamTimeSettingOnCorePre(device_t dev)
85 {
86         u8 pviModeFlag;
87         u8 highVoltageVid, lowVoltageVid, bValue;
88         u16 minimumSlamTime;
89         u16 vSlamTimes[7] = { 1000, 2000, 3000, 4000, 6000, 10000, 20000 };     /* Reg settings scaled by 100 */
90         u32 dtemp;
91         msr_t msr;
92
93         /* This function calculates the VsSlamTime using the range of possible
94          * voltages instead of a hardcoded 200us.
95          * Note:This function is called from setFidVidRegs and setUserPs after
96          * programming a custom Pstate.
97          */
98
99         /* Calculate Slam Time
100          * Vslam = 0.4us/mV * Vp0 - (lowest out of Vpmin or Valt)
101          * In our case, we will scale the values by 100 to avoid
102          * decimals.
103          */
104
105         /* Determine if this is a PVI or SVI system */
106         dtemp = pci_read_config32(dev, 0xA0);
107
108         if (dtemp & PVI_MODE)
109                 pviModeFlag = 1;
110         else
111                 pviModeFlag = 0;
112
113         /* Get P0's voltage */
114         msr = rdmsr(0xC0010064);
115         highVoltageVid = (u8) ((msr.lo >> PS_CPU_VID_SHFT) & 0x7F);
116
117         /* If SVI, we only care about CPU VID.
118          * If PVI, determine the higher voltage b/t NB and CPU
119          */
120         if (pviModeFlag) {
121                 bValue = (u8) ((msr.lo >> PS_NB_VID_SHFT) & 0x7F);
122                 if (highVoltageVid > bValue)
123                         highVoltageVid = bValue;
124         }
125
126         /* Get Pmin's index */
127         msr = rdmsr(0xC0010061);
128         bValue = (u8) ((msr.lo >> PS_CUR_LIM_SHFT) & BIT_MASK_3);
129
130         /* Get Pmin's VID */
131         msr = rdmsr(0xC0010064 + bValue);
132         lowVoltageVid = (u8) ((msr.lo >> PS_CPU_VID_SHFT) & 0x7F);
133
134         /* If SVI, we only care about CPU VID.
135          * If PVI, determine the higher voltage b/t NB and CPU
136          */
137         if (pviModeFlag) {
138                 bValue = (u8) ((msr.lo >> PS_NB_VID_SHFT) & 0x7F);
139                 if (lowVoltageVid > bValue)
140                         lowVoltageVid = bValue;
141         }
142
143         /* Get AltVID */
144         dtemp = pci_read_config32(dev, 0xDC);
145         bValue = (u8) (dtemp & BIT_MASK_7);
146
147         /* Use the VID with the lowest voltage (higher VID) */
148         if (lowVoltageVid < bValue)
149                 lowVoltageVid = bValue;
150
151         /* If Vids are 7Dh - 7Fh, force 7Ch to keep calculations linear */
152         if (lowVoltageVid > 0x7C) {
153                 lowVoltageVid = 0x7C;
154                 if (highVoltageVid > 0x7C)
155                         highVoltageVid = 0x7C;
156         }
157
158         bValue = (u8) (lowVoltageVid - highVoltageVid);
159
160         /* Each Vid increment is 12.5 mV.  The minimum slam time is:
161          * vidCodeDelta * 12.5mV * 0.4us/mV
162          * Scale by 100 to avoid decimals.
163          */
164         minimumSlamTime = bValue * (125 * 4);
165
166         /* Now round up to nearest register setting.
167          * Note that if we don't find a value, we
168          * will fall through to a value of 7
169          */
170         for (bValue = 0; bValue < 7; bValue++) {
171                 if (minimumSlamTime <= vSlamTimes[bValue])
172                         break;
173         }
174
175         /* Apply the value */
176         dtemp = pci_read_config32(dev, 0xD8);
177         dtemp &= VSSLAM_MASK;
178         dtemp |= bValue;
179         pci_write_config32(dev, 0xd8, dtemp);
180 }
181
182 static u32 nb_clk_did(int node, u32 cpuRev,u8 procPkg) {
183         u8 link0isGen3 = 0;
184         u8 offset;
185         if (AMD_CpuFindCapability(node, 0, &offset)) {
186           link0isGen3 = (AMD_checkLinkType(node, 0, offset) & HTPHY_LINKTYPE_HT3 );
187         }
188         /* FIXME: NB_CLKDID should be 101b for AMD_DA_C2 in package
189            S1g3 in link Gen3 mode, but I don't know how to tell
190            package S1g3 from S1g4 */
191         if ((cpuRev & AMD_DA_C2) && (procPkg & AMD_PKGTYPE_S1gX)
192            && link0isGen3) {
193           return 5 ; /* divide clk by 128*/
194         } else {
195           return 4 ; /* divide clk by 16 */
196         }
197 }
198
199
200 static u32 power_up_down(int node, u8 procPkg) {
201        u32 dword=0;
202         /* from CPU rev guide #41322 rev 3.74 June 2010 Table 26 */
203         u8 singleLinkFlag = ((procPkg == AMD_PKGTYPE_AM3_2r2)
204                              || (procPkg == AMD_PKGTYPE_S1gX)
205                              || (procPkg == AMD_PKGTYPE_ASB2));
206
207         if (singleLinkFlag) {
208           /*
209            * PowerStepUp=01000b - 50nS
210            * PowerStepDown=01000b - 50ns
211            */
212           dword |= PW_STP_UP50 | PW_STP_DN50;
213         } else {
214           u32 dispRefModeEn = (pci_read_config32(NODE_PCI(node,0),0x68) >> 24) & 1;
215           u32 isocEn = 0;
216           int j;
217           for(j=0 ; (j<4) && (!isocEn) ; j++ ) {
218             u8 offset;
219             if (AMD_CpuFindCapability(node, j, &offset)) {
220               isocEn = (pci_read_config32(NODE_PCI(node,0),offset+4) >>12) & 1;
221             }
222           }
223
224           if (dispRefModeEn || isocEn) {
225                 dword |= PW_STP_UP50 | PW_STP_DN50 ;
226           } else {
227                 /* get number of cores for PowerStepUp & PowerStepDown in server
228                    1 core - 400nS  - 0000b
229                    2 cores - 200nS - 0010b
230                    3 cores - 133nS -> 100nS - 0011b
231                    4 cores - 100nS - 0011b
232                  */
233                 switch (get_core_num_in_bsp(node)) {
234                 case 0:
235                         dword |= PW_STP_UP400 | PW_STP_DN400;
236                         break;
237                 case 1:
238                 case 2:
239                         dword |= PW_STP_UP200 | PW_STP_DN200;
240                         break;
241                 case 3:
242                         dword |= PW_STP_UP100 | PW_STP_DN100;
243                         break;
244                 default:
245                         dword |= PW_STP_UP100 | PW_STP_DN100;
246                         break;
247                 }
248           }
249         }
250         return dword;
251 }
252
253 static void config_clk_power_ctrl_reg0(int node, u32 cpuRev, u8 procPkg) {
254         device_t dev = NODE_PCI(node, 3);
255
256         /* Program fields in Clock Power/Control register0 (F3xD4) */
257
258         /* set F3xD4 Clock Power/Timing Control 0 Register
259          * NbClkDidApplyAll=1b
260          * NbClkDid=100b or 101b
261          * PowerStepUp= "platform dependent"
262          * PowerStepDown= "platform dependent"
263          * LinkPllLink=01b
264          * ClkRampHystCtl=HW default
265          * ClkRampHystSel=1111b
266          */
267         u32 dword= pci_read_config32(dev, 0xd4);
268         dword &= CPTC0_MASK;
269         dword |= NB_CLKDID_ALL | LNK_PLL_LOCK | CLK_RAMP_HYST_SEL_VAL;
270         dword |= (nb_clk_did(node,cpuRev,procPkg) <<  NB_CLKDID_SHIFT);
271
272         dword |= power_up_down(node, procPkg);
273
274         pci_write_config32(dev, 0xd4, dword);
275
276 }
277
278 static void config_power_ctrl_misc_reg(device_t dev,u32 cpuRev, u8 procPkg) {
279         /* check PVI/SVI */
280         u32 dword = pci_read_config32(dev, 0xA0);
281
282         /* BKDG r31116 2010-04-22  2.4.1.7 step b F3xA0[VSSlamVidMod] */
283         /* PllLockTime and PsiVidEn set in ruleset in defaults.h */
284         if (dword & PVI_MODE) { /* PVI */
285                 /* set slamVidMode to 0 for PVI */
286                 dword &= VID_SLAM_OFF ;
287         } else {        /* SVI */
288                 /* set slamVidMode to 1 for SVI */
289                 dword |= VID_SLAM_ON;
290
291                 u32 dtemp = dword;
292
293                 /* Program F3xD8[PwrPlanes] according F3xA0[DulaVdd]  */
294                 dword = pci_read_config32(dev, 0xD8);
295
296                 if (dtemp & DUAL_VDD_BIT)
297                         dword |= PWR_PLN_ON;
298                 else
299                         dword &= PWR_PLN_OFF;
300                 pci_write_config32(dev, 0xD8, dword);
301
302                 dword = dtemp;
303         }
304         /* set the rest of A0 since we're at it... */
305
306         if (cpuRev & (AMD_DA_Cx | AMD_RB_C3 )) {
307              dword |= NB_PSTATE_FORCE_ON;
308         } // else should we clear it ?
309
310
311         if ((procPkg == AMD_PKGTYPE_G34) || (procPkg == AMD_PKGTYPE_C32) ) {
312           dword |= BP_INS_TRI_EN_ON ;
313         }
314
315            /* TODO: look into C1E state and F3xA0[IdleExitEn]*/
316         #if CONFIG_SVI_HIGH_FREQ
317            if (cpuRev & AMD_FAM10_C3) {
318              dword |= SVI_HIGH_FREQ_ON;
319            }
320         #endif
321         pci_write_config32(dev, 0xA0, dword);
322 }
323
324 static void config_nb_syn_ptr_adj(device_t dev, u32 cpuRev) {
325         /* Note the following settings are additional from the ported
326          * function setFidVidRegs()
327          */
328         /* adjust FIFO between nb and core clocks to max allowed
329            values (min latency) */
330         u32 nbPstate = pci_read_config32(dev,0x1f0) & NB_PSTATE_MASK;
331         u8 nbSynPtrAdj;
332         if ((cpuRev & (AMD_DR_Bx|AMD_DA_Cx) )
333             || ( (cpuRev & AMD_RB_C3) && (nbPstate!=0)))  {
334           nbSynPtrAdj = 5;
335         } else {
336           nbSynPtrAdj = 6;
337         }
338
339         u32 dword = pci_read_config32(dev, 0xDc);
340         dword &= ~ NB_SYN_PTR_ADJ_MASK;
341         dword |= nbSynPtrAdj << NB_SYN_PTR_ADJ_POS;
342         /* NbsynPtrAdj set to 5 or 6 per BKDG (needs reset) */
343         pci_write_config32(dev, 0xdc, dword);
344 }
345
346 static void config_acpi_pwr_state_ctrl_regs(device_t dev, u32 cpuRev, u8 procPkg) {
347                 /* step 1, chapter 2.4.2.6 of AMD Fam 10 BKDG #31116 Rev 3.48 22.4.2010 */
348         u32 dword;
349         u32 c1= 1;
350         if (cpuRev & (AMD_DR_Bx)) {
351             // will coreboot ever enable cache scrubbing ?
352             // if it does, will it be enough to check the current state
353             // or should we configure for what we'll set up later ?
354             dword = pci_read_config32(dev, 0x58);
355             u32 scrubbingCache = dword &
356                                 ( (0x1F << 16) // DCacheScrub
357                                   | (0x1F << 8) ); // L2Scrub
358             if (scrubbingCache) {
359                 c1 = 0x80;
360             } else {
361                 c1 = 0xA0;
362             }
363         } else { // rev C or later
364             // same doubt as cache scrubbing: ok to check current state ?
365             dword = pci_read_config32(dev, 0xDC);
366             u32 cacheFlushOnHalt = dword & (7 << 16);
367             if (!cacheFlushOnHalt) {
368                c1 = 0x80;
369             }
370         }
371         dword = (c1 << 24) | (0xE641E6);
372         pci_write_config32(dev, 0x84, dword);
373
374
375         /* FIXME: BKDG Table 100 says if the link is at a Gen1
376 frequency and the chipset does not support a 10us minimum LDTSTOP
377 assertion time, then { If ASB2 && SVI then smaf001 = F6h else
378 smaf001=87h. } else ...  I hardly know what it means or how to check
379 it from here, so I bluntly assume it is false and code here the else,
380 which is easier  */
381
382         u32 smaf001 = 0xE6;
383         if (cpuRev & AMD_DR_Bx ) {
384             smaf001 = 0xA6;
385         } else {
386             #if CONFIG_SVI_HIGH_FREQ
387                 if (cpuRev & (AMD_RB_C3 | AMD_DA_C3)) {
388                        smaf001 = 0xF6;
389                 }
390             #endif
391         }
392         u32 fidvidChange = 0;
393         if (((cpuRev & AMD_DA_Cx) && (procPkg & AMD_PKGTYPE_S1gX))
394                     || (cpuRev & AMD_RB_C3) ) {
395                        fidvidChange=0x0B;
396         }
397         dword = (0xE6 << 24) | (fidvidChange << 16)
398                         | (smaf001 << 8) | 0x81;
399         pci_write_config32(dev, 0x80, dword);
400 }
401
402 static void prep_fid_change(void)
403 {
404         u32 dword;
405         u32 nodes;
406         device_t dev;
407         int i;
408
409         /* This needs to be run before any Pstate changes are requested */
410
411         nodes = get_nodes();
412
413         for (i = 0; i < nodes; i++) {
414                 printk(BIOS_DEBUG, "Prep FID/VID Node:%02x \n", i);
415                 dev = NODE_PCI(i, 3);
416                 u32 cpuRev = mctGetLogicalCPUID(0xFF) ;
417                 u8 procPkg =  mctGetProcessorPackageType();
418
419                 setVSRamp(dev);
420                 /* BKDG r31116 2010-04-22  2.4.1.7 step b F3xD8[VSSlamTime] */
421                 /* Figure out the value for VsSlamTime and program it */
422                 recalculateVsSlamTimeSettingOnCorePre(dev);
423
424                 config_clk_power_ctrl_reg0(i,cpuRev,procPkg);
425
426                 config_power_ctrl_misc_reg(dev,cpuRev,procPkg);
427                 config_nb_syn_ptr_adj(dev,cpuRev);
428
429                 config_acpi_pwr_state_ctrl_regs(dev,cpuRev,procPkg);
430
431                 dword = pci_read_config32(dev, 0x80);
432                 printk(BIOS_DEBUG, "  F3x80: %08x \n", dword);
433                 dword = pci_read_config32(dev, 0x84);
434                 printk(BIOS_DEBUG, "  F3x84: %08x \n", dword);
435                 dword = pci_read_config32(dev, 0xD4);
436                 printk(BIOS_DEBUG, "  F3xD4: %08x \n", dword);
437                 dword = pci_read_config32(dev, 0xD8);
438                 printk(BIOS_DEBUG, "  F3xD8: %08x \n", dword);
439                 dword = pci_read_config32(dev, 0xDC);
440                 printk(BIOS_DEBUG, "  F3xDC: %08x \n", dword);
441
442
443         }
444 }
445
446 static void waitCurrentPstate(u32 target_pstate){
447   msr_t initial_msr = rdmsr(TSC_MSR);
448   msr_t pstate_msr = rdmsr(CUR_PSTATE_MSR);
449   msr_t tsc_msr;
450   u8 timedout ;
451
452   /* paranoia ? I fear when we run fixPsNbVidBeforeWR we can enter a
453    * P1 that is a copy of P0, therefore has the same NB DID but the
454    * TSC will count twice per tick, so we have to wait for twice the
455    * count to achieve the desired timeout. But I'm likely to
456    * misunderstand this...
457    */
458   u32 corrected_timeout = (    (pstate_msr.lo==1)
459                             && (!(rdmsr(0xC0010065).lo & NB_DID_M_ON)) ) ?
460                           WAIT_PSTATE_TIMEOUT*2 : WAIT_PSTATE_TIMEOUT  ;
461   msr_t timeout;
462
463   timeout.lo = initial_msr.lo + corrected_timeout ;
464   timeout.hi = initial_msr.hi;
465   if ( (((u32)0xffffffff) - initial_msr.lo) < corrected_timeout ) {
466      timeout.hi++;
467   }
468
469   // assuming TSC ticks at 1.25 ns per tick (800 MHz)
470   do {
471       pstate_msr = rdmsr(CUR_PSTATE_MSR);
472       tsc_msr = rdmsr(TSC_MSR);
473       timedout = (tsc_msr.hi > timeout.hi)
474                 || ((tsc_msr.hi == timeout.hi) && (tsc_msr.lo > timeout.lo ));
475   } while ( (pstate_msr.lo != target_pstate) && (! timedout) ) ;
476
477   if (pstate_msr.lo != target_pstate) {
478     msr_t limit_msr = rdmsr(0xc0010061);
479     printk(BIOS_ERR, "*** Time out waiting for P-state %01x. Current P-state %01x P-state current limit MSRC001_0061=%02x\n", target_pstate, pstate_msr.lo, limit_msr.lo);
480
481     do { // should we just go on instead ?
482       pstate_msr = rdmsr(CUR_PSTATE_MSR);
483     } while ( pstate_msr.lo != target_pstate  ) ;
484   }
485 }
486
487 static void set_pstate(u32 nonBoostedPState) {
488         msr_t msr;
489
490         // Transition P0 for calling core.
491         msr = rdmsr(0xC0010062);
492
493         msr.lo = nonBoostedPState;
494         wrmsr(0xC0010062, msr);
495
496         /* Wait for P0 to set. */
497         waitCurrentPstate(nonBoostedPState);
498 }
499
500
501
502
503 static void UpdateSinglePlaneNbVid(void)
504 {
505         u32 nbVid, cpuVid;
506         u8 i;
507         msr_t msr;
508
509         /* copy higher voltage (lower VID) of NBVID & CPUVID to both */
510         for (i = 0; i < 5; i++) {
511                 msr = rdmsr(PS_REG_BASE + i);
512                 nbVid = (msr.lo & PS_CPU_VID_M_ON) >> PS_CPU_VID_SHFT;
513                 cpuVid = (msr.lo & PS_NB_VID_M_ON) >> PS_NB_VID_SHFT;
514
515                 if (nbVid != cpuVid) {
516                         if (nbVid > cpuVid)
517                                 nbVid = cpuVid;
518
519                         msr.lo = msr.lo & PS_BOTH_VID_OFF;
520                         msr.lo = msr.lo | (u32) ((nbVid) << PS_NB_VID_SHFT);
521                         msr.lo = msr.lo | (u32) ((nbVid) << PS_CPU_VID_SHFT);
522                         wrmsr(PS_REG_BASE + i, msr);
523                 }
524         }
525 }
526
527 static void fixPsNbVidBeforeWR(u32 newNbVid, u32 coreid, u32 dev, u8 pviMode)
528  {
529         msr_t msr;
530         u8 startup_pstate;
531
532         /* This function sets NbVid before the warm reset.
533          *       Get StartupPstate from MSRC001_0071.
534          *       Read Pstate register pointed by [StartupPstate].
535          *       and copy its content to P0 and P1 registers.
536          *       Copy newNbVid to P0[NbVid].
537          *       transition to P1 on all cores,
538          *       then transition to P0 on core 0.
539          *       Wait for MSRC001_0063[CurPstate] = 000b on core 0.
540          * see BKDG rev 3.48  2.4.2.9.1 BIOS NB COF and VID Configuration
541          *                              for SVI and Single-Plane PVI Systems
542          */
543
544         msr = rdmsr(0xc0010071);
545         startup_pstate = (msr.hi >> (32 - 32)) & 0x07;
546
547         /* Copy startup pstate to P1 and P0 MSRs. Set the maxvid for
548          * this node in P0.  Then transition to P1 for corex and P0
549          * for core0.  These setting will be cleared by the warm reset
550          */
551         msr = rdmsr(0xC0010064 + startup_pstate);
552         wrmsr(0xC0010065, msr);
553         wrmsr(0xC0010064, msr);
554
555         /* missing step 2 from BDKG , F3xDC[PstateMaxVal] =
556          * max(1,F3xDC[PstateMaxVal] ) because it would take
557          * synchronization between cores and we don't think
558          * PstatMaxVal is going to be 0 on cold reset anyway ?
559          */
560         if ( ! (pci_read_config32(dev, 0xDC) & (~ PS_MAX_VAL_MASK)) ) {
561            printk(BIOS_ERR,"F3xDC[PstateMaxVal] is zero. Northbridge voltage setting will fail. fixPsNbVidBeforeWR in fidvid.c needs fixing. See AMD # 31116 rev 3.48 BKDG 2.4.2.9.1 \n");
562         };
563
564         msr.lo &= ~0xFE000000;  // clear nbvid
565         msr.lo |= (newNbVid << 25);
566         wrmsr(0xC0010064, msr);
567
568         if (pviMode) { /* single plane*/
569           UpdateSinglePlaneNbVid();
570         }
571
572         // Transition to P1 for all APs and P0 for core0.
573         set_pstate(1);
574
575         if (coreid == 0) {
576              set_pstate(0);
577         }
578
579         /* missing step 7 (restore PstateMax to 0 if needed) because
580          * we skipped step 2
581          */
582
583 }
584
585 static u32 needs_NB_COF_VID_update(void)
586 {
587         u8 nb_cof_vid_update;
588         u8 nodes;
589         u8 i;
590
591         /* If any node has nb_cof_vid_update set all nodes need an update. */
592         nodes = get_nodes();
593         nb_cof_vid_update = 0;
594         for (i = 0; i < nodes; i++) {
595                 u32 cpuRev = mctGetLogicalCPUID(i) ;
596                 u32 nbCofVidUpdateDefined = (cpuRev & (AMD_FAM10_LT_D));
597                 if (nbCofVidUpdateDefined
598                     && (pci_read_config32(NODE_PCI(i, 3), 0x1FC)
599                         & NB_COF_VID_UPDATE_MASK)) {
600                         nb_cof_vid_update = 1;
601                         break;
602                 }
603         }
604         return nb_cof_vid_update;
605 }
606
607 static u32 init_fidvid_core(u32 nodeid, u32 coreid)
608 {
609         device_t dev;
610         u32 vid_max;
611         u32 fid_max = 0;
612         u8 nb_cof_vid_update = needs_NB_COF_VID_update();
613         u8 pvimode;
614         u32 reg1fc;
615
616         /* Steps 1-6 of BIOS NB COF and VID Configuration
617          * for SVI and Single-Plane PVI Systems. BKDG 2.4.2.9 #31116 rev 3.48
618          */
619
620         dev = NODE_PCI(nodeid, 3);
621         pvimode = pci_read_config32(dev, PW_CTL_MISC) & PVI_MODE;
622         reg1fc = pci_read_config32(dev, 0x1FC);
623
624         if (nb_cof_vid_update) {
625                 vid_max = (reg1fc &  SINGLE_PLANE_NB_VID_MASK ) >>  SINGLE_PLANE_NB_VID_SHIFT ;
626                 fid_max = (reg1fc &  SINGLE_PLANE_NB_FID_MASK ) >>  SINGLE_PLANE_NB_FID_SHIFT ;
627
628                 if (!pvimode) { /* SVI, dual power plane */
629                         vid_max = vid_max - ((reg1fc &  DUAL_PLANE_NB_VID_OFF_MASK ) >>  DUAL_PLANE_NB_VID_SHIFT );
630                         fid_max = fid_max +  ((reg1fc &  DUAL_PLANE_NB_FID_OFF_MASK ) >>  DUAL_PLANE_NB_FID_SHIFT );
631                 }
632                 /* write newNbVid to P-state Reg's NbVid always if NbVidUpdatedAll=1 */
633                 fixPsNbVidBeforeWR(vid_max, coreid,dev,pvimode);
634
635                 /* fid setup is handled by the BSP at the end. */
636
637         } else {        /* ! nb_cof_vid_update */
638                 /* Use max values */
639                 if (pvimode)
640                         UpdateSinglePlaneNbVid();
641         }
642
643         return ((nb_cof_vid_update << 16) | (fid_max << 8));
644
645 }
646
647 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid)
648 {
649         u32 send;
650
651         printk(BIOS_DEBUG, "FIDVID on AP: %02x\n", apicid);
652
653         send = init_fidvid_core(nodeid,coreid);
654         send |= (apicid << 24); // ap apicid
655
656         // Send signal to BSP about this AP max fid
657         // This also indicates this AP is ready for warm reset (if required).
658         lapic_write(LAPIC_MSG_REG, send | F10_APSTATE_RESET);
659 }
660
661 static u32 calc_common_fid(u32 fid_packed, u32 fid_packed_new)
662 {
663         u32 fidmax;
664         u32 fidmax_new;
665
666         fidmax = (fid_packed >> 8) & 0xFF;
667
668         fidmax_new = (fid_packed_new >> 8) & 0xFF;
669
670         if (fidmax > fidmax_new) {
671                 fidmax = fidmax_new;
672         }
673
674         fid_packed &= 0xFF << 16;
675         fid_packed |= (fidmax << 8);
676         fid_packed |= fid_packed_new & (0xFF << 16);    // set nb_cof_vid_update
677
678         return fid_packed;
679 }
680
681 static void init_fidvid_bsp_stage1(u32 ap_apicid, void *gp)
682 {
683         u32 readback = 0;
684         u32 timeout = 1;
685
686         struct fidvid_st *fvp = gp;
687         int loop;
688
689         print_debug_fv("Wait for AP stage 1: ap_apicid = ", ap_apicid);
690
691         loop = 100000;
692         while (--loop > 0) {
693                 if (lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback) != 0)
694                         continue;
695                 if ((readback & 0x3f) == 1) {
696                         timeout = 0;
697                         break;  /* target ap is in stage 1 */
698                 }
699         }
700
701         if (timeout) {
702                 printk(BIOS_DEBUG, "%s: timed out reading from ap %02x\n",
703                        __func__, ap_apicid);
704                 return;
705         }
706
707         print_debug_fv("\treadback = ", readback);
708
709         fvp->common_fid = calc_common_fid(fvp->common_fid, readback);
710
711         print_debug_fv("\tcommon_fid(packed) = ", fvp->common_fid);
712
713 }
714
715 static void fixPsNbVidAfterWR(u32 newNbVid, u8 NbVidUpdatedAll,u8 pviMode) 
716 {
717         msr_t msr;
718         u8 i;
719         u8 StartupPstate;
720
721         /* BKDG 2.4.2.9.1 11-12
722          * This function copies newNbVid to NbVid bits in P-state
723          * Registers[4:0] if its NbDid bit=0, and IddValue!=0 in case of
724          * NbVidUpdatedAll =0 or copies newNbVid to NbVid bits in
725          * P-state Registers[4:0] if its IddValue!=0 in case of
726          * NbVidUpdatedAll=1. Then transition to StartPstate.
727          */
728
729         /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
730         for (i = 0; i < 5; i++) {
731                 msr = rdmsr(0xC0010064 + i);
732                 /*  NbDid (bit 22 of P-state Reg) == 0  or NbVidUpdatedAll = 1 */
733                 if (   (msr.hi & PS_IDD_VALUE_MASK) 
734                     && (msr.hi & PS_EN_MASK)
735                     &&(((msr.lo & PS_NB_DID_MASK) == 0) || NbVidUpdatedAll)) {
736                         msr.lo &= PS_NB_VID_M_OFF;
737                         msr.lo |= (newNbVid & 0x7F) << PS_NB_VID_SHFT;
738                         wrmsr(0xC0010064 + i, msr);
739                 }
740         }
741
742         /* Not documented. Would overwrite Nb_Vids just copied
743          * should we just update cpu_vid or nothing at all ?
744          */
745         if (pviMode) { //single plane
746             UpdateSinglePlaneNbVid();
747         }
748         /* For each core in the system, transition all cores to StartupPstate */
749         msr = rdmsr(0xC0010071);
750         StartupPstate = msr.hi & 0x07;
751        
752         /* Set and wait for StartupPstate to set. */
753         set_pstate(StartupPstate);
754
755 }
756
757 static void finalPstateChange(void)
758 {
759         /* Enble P0 on all cores for best performance.
760          * Linux can slow them down later if need be.
761          * It is safe since they will be in C1 halt
762          * most of the time anyway.
763          */
764         set_pstate(0);
765 }
766
767 static void init_fidvid_stage2(u32 apicid, u32 nodeid)
768 {
769         msr_t msr;
770         device_t dev;
771         u32 reg1fc;
772         u32 dtemp;
773         u32 nbvid;
774         u8 nb_cof_vid_update = needs_NB_COF_VID_update();
775         u8 NbVidUpdateAll;
776         u8 pvimode;
777
778         /* After warm reset finish the fid/vid setup for all cores. */
779
780         /* If any node has nb_cof_vid_update set all nodes need an update. */
781
782         dev = NODE_PCI(nodeid, 3);
783         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
784         reg1fc = pci_read_config32(dev, 0x1FC);
785         nbvid = (reg1fc >> 7) & 0x7F;
786         NbVidUpdateAll = (reg1fc >> 1) & 1;
787
788         if (nb_cof_vid_update) {
789                 if (!pvimode) { /* SVI */
790                         nbvid = nbvid - ((reg1fc >> 17) & 0x1F);
791                 }
792                 /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
793                 fixPsNbVidAfterWR(nbvid, NbVidUpdateAll,pvimode);
794         } else {                /* !nb_cof_vid_update */
795                 if (pvimode)
796                         UpdateSinglePlaneNbVid();
797         }
798         dtemp = pci_read_config32(dev, 0xA0);
799         dtemp &= PLLLOCK_OFF;
800         dtemp |= PLLLOCK_DFT_L;
801         pci_write_config32(dev, 0xA0, dtemp);
802
803         finalPstateChange();
804
805         /* Set TSC to tick at the P0 ndfid rate */
806         msr = rdmsr(HWCR);
807         msr.lo |= 1 << 24;
808         wrmsr(HWCR, msr);
809 }
810
811
812 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
813 struct ap_apicid_st {
814         u32 num;
815         // it could use 256 bytes for 64 node quad core system
816         u8 apicid[NODE_NUMS * 4];
817 };
818
819 static void store_ap_apicid(unsigned ap_apicid, void *gp)
820 {
821         struct ap_apicid_st *p = gp;
822
823         p->apicid[p->num++] = ap_apicid;
824
825 }
826 #endif
827
828
829 static int init_fidvid_bsp(u32 bsp_apicid, u32 nodes)
830 {
831 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
832         struct ap_apicid_st ap_apicidx;
833         u32 i;
834 #endif
835         struct fidvid_st fv;
836
837         printk(BIOS_DEBUG, "FIDVID on BSP, APIC_id: %02x\n", bsp_apicid);
838
839         /* Steps 1-6 of BIOS NB COF and VID Configuration
840          * for SVI and Single-Plane PVI Systems.
841          */
842
843         fv.common_fid = init_fidvid_core(0,0);
844
845         print_debug_fv("BSP fid = ", fv.common_fid);
846
847 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST && !CONFIG_SET_FIDVID_CORE0_ONLY
848         /* For all APs (We know the APIC ID of all APs even when the APIC ID
849            is lifted) remote read from AP LAPIC_MSG_REG about max fid.
850            Then calculate the common max fid that can be used for all
851            APs and BSP */
852         ap_apicidx.num = 0;
853
854         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE_RANGE, store_ap_apicid, &ap_apicidx);
855
856         for (i = 0; i < ap_apicidx.num; i++) {
857                 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
858         }
859 #else
860         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
861 #endif
862
863         print_debug_fv("common_fid = ", fv.common_fid);
864
865         if (fv.common_fid & (1 << 16)) {        /* check nb_cof_vid_update */
866
867                 // Enable the common fid and other settings.
868                 enable_fid_change((fv.common_fid >> 8) & 0x1F);
869
870                 // nbfid change need warm reset, so reset at first
871                 return 1;
872         }
873
874         return 0;               // No FID/VID changes. Don't reset
875 }
876 #endif