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) {
347         /* Rev B settings - FIXME: support other revs. */
348         u32 dword = 0xA0E641E6;
349         pci_write_config32(dev, 0x84, dword);
350         dword = 0xE600A681;
351         pci_write_config32(dev, 0x80, dword);
352 }
353
354 static void prep_fid_change(void)
355 {
356         u32 dword;
357         u32 nodes;
358         device_t dev;
359         int i;
360
361         /* This needs to be run before any Pstate changes are requested */
362
363         nodes = get_nodes();
364
365         for (i = 0; i < nodes; i++) {
366                 printk(BIOS_DEBUG, "Prep FID/VID Node:%02x \n", i);
367                 dev = NODE_PCI(i, 3);
368                 u32 cpuRev = mctGetLogicalCPUID(0xFF) ;
369                 u8 procPkg =  mctGetProcessorPackageType();
370
371                 setVSRamp(dev);
372                 /* BKDG r31116 2010-04-22  2.4.1.7 step b F3xD8[VSSlamTime] */
373                 /* Figure out the value for VsSlamTime and program it */
374                 recalculateVsSlamTimeSettingOnCorePre(dev);
375
376                 config_clk_power_ctrl_reg0(i,cpuRev,procPkg);
377
378                 config_power_ctrl_misc_reg(dev,cpuRev,procPkg);                       
379                 config_nb_syn_ptr_adj(dev,cpuRev);
380
381                 config_acpi_pwr_state_ctrl_regs(dev);
382
383                 dword = pci_read_config32(dev, 0x80);
384                 printk(BIOS_DEBUG, "  F3x80: %08x \n", dword);
385                 dword = pci_read_config32(dev, 0x84);
386                 printk(BIOS_DEBUG, "  F3x84: %08x \n", dword);
387                 dword = pci_read_config32(dev, 0xD4);
388                 printk(BIOS_DEBUG, "  F3xD4: %08x \n", dword);
389                 dword = pci_read_config32(dev, 0xD8);
390                 printk(BIOS_DEBUG, "  F3xD8: %08x \n", dword);
391                 dword = pci_read_config32(dev, 0xDC);
392                 printk(BIOS_DEBUG, "  F3xDC: %08x \n", dword);
393
394
395         }
396 }
397
398
399 static void UpdateSinglePlaneNbVid(void)
400 {
401         u32 nbVid, cpuVid;
402         u8 i;
403         msr_t msr;
404
405         /* copy higher voltage (lower VID) of NBVID & CPUVID to both */
406         for (i = 0; i < 5; i++) {
407                 msr = rdmsr(PS_REG_BASE + i);
408                 nbVid = (msr.lo & PS_CPU_VID_M_ON) >> PS_CPU_VID_SHFT;
409                 cpuVid = (msr.lo & PS_NB_VID_M_ON) >> PS_NB_VID_SHFT;
410
411                 if (nbVid != cpuVid) {
412                         if (nbVid > cpuVid)
413                                 nbVid = cpuVid;
414
415                         msr.lo = msr.lo & PS_BOTH_VID_OFF;
416                         msr.lo = msr.lo | (u32) ((nbVid) << PS_NB_VID_SHFT);
417                         msr.lo = msr.lo | (u32) ((nbVid) << PS_CPU_VID_SHFT);
418                         wrmsr(PS_REG_BASE + i, msr);
419                 }
420         }
421 }
422
423 static void fixPsNbVidBeforeWR(u32 newNbVid, u32 coreid)
424 {
425         msr_t msr;
426         u8 startup_pstate;
427
428         /* This function sets NbVid before the warm reset.
429          *       Get StartupPstate from MSRC001_0071.
430          *       Read Pstate register pionted by [StartupPstate].
431          *       and copy its content to P0 and P1 registers.
432          *       Copy newNbVid to P0[NbVid].
433          *       transition to P1 on all cores,
434          *       then transition to P0 on core 0.
435          *       Wait for MSRC001_0063[CurPstate] = 000b on core 0.
436          */
437
438         msr = rdmsr(0xc0010071);
439         startup_pstate = (msr.hi >> (32 - 32)) & 0x07;
440
441         /* Copy startup pstate to P1 and P0 MSRs. Set the maxvid for this node in P0.
442          * Then transition to P1 for corex and P0 for core0.
443          * These setting will be cleared by the warm reset
444          */
445         msr = rdmsr(0xC0010064 + startup_pstate);
446         wrmsr(0xC0010065, msr);
447         wrmsr(0xC0010064, msr);
448
449         msr.lo &= ~0xFE000000;  // clear nbvid
450         msr.lo |= newNbVid << 25;
451         wrmsr(0xC0010064, msr);
452
453         UpdateSinglePlaneNbVid();
454
455         // Transition to P1 for all APs and P0 for core0.
456         msr = rdmsr(0xC0010062);
457         msr.lo = (msr.lo & ~0x07) | 1;
458         wrmsr(0xC0010062, msr);
459
460         // Wait for P1 to set.
461         do {
462                 msr = rdmsr(0xC0010063);
463         } while (msr.lo != 1);
464
465         if (coreid == 0) {
466                 msr.lo = msr.lo & ~0x07;
467                 wrmsr(0xC0010062, msr);
468                 // Wait for P0 to set.
469                 do {
470                         msr = rdmsr(0xC0010063);
471                 } while (msr.lo != 0);
472         }
473 }
474
475 static void coreDelay(void)
476 {
477         u32 saved;
478         u32 hi, lo, msr;
479         u32 cycles;
480
481         /* delay ~40us
482            This seems like a hack to me...
483            It would be nice to have a central delay function. */
484
485         cycles = 8000 << 3;     /* x8 (number of 1.25ns ticks) */
486
487         msr = 0x10;             /* TSC */
488         _RDMSR(msr, &lo, &hi);
489         saved = lo;
490         do {
491                 _RDMSR(msr, &lo, &hi);
492         } while (lo - saved < cycles);
493 }
494
495 static void transitionVid(u32 targetVid, u8 dev, u8 isNb)
496 {
497         u32 currentVid, dtemp;
498         msr_t msr;
499         u8 vsTimecode;
500         u16 timeTable[8] = { 10, 20, 30, 40, 60, 100, 200, 500 };
501         int vsTime;
502
503         /* This function steps or slam the Nb VID to the target VID.
504          * It uses VSRampTime for [SlamVidMode]=0 ([PviMode]=1)
505          * or VSSlamTime for [SlamVidMode]=1 ([PviMode]=0)to time period.
506          */
507
508         /* get the current VID */
509         msr = rdmsr(0xC0010071);
510         if (isNb)
511                 currentVid = (msr.lo >> NB_VID_POS) & BIT_MASK_7;
512         else
513                 currentVid = (msr.lo >> CPU_VID_POS) & BIT_MASK_7;
514
515         /* Read MSRC001_0070 COFVID Control Register */
516         msr = rdmsr(0xC0010070);
517
518         /* check PVI/SPI */
519         dtemp = pci_read_config32(dev, 0xA0);
520         if (dtemp & PVI_MODE) { /* PVI, step VID */
521                 if (currentVid < targetVid) {
522                         while (currentVid < targetVid) {
523                                 currentVid++;
524                                 if (isNb)
525                                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (currentVid << NB_VID_POS);
526                                 else
527                                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (currentVid << CPU_VID_POS);
528                                 wrmsr(0xC0010070, msr);
529
530                                 /* read F3xD8[VSRampTime]  */
531                                 dtemp = pci_read_config32(dev, 0xD8);
532                                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
533                                 vsTime = (int)timeTable[vsTimecode];
534                                 do {
535                                         coreDelay();
536                                         vsTime -= 40;
537                                 } while (vsTime > 0);
538                         }
539                 } else if (currentVid > targetVid) {
540                         while (currentVid > targetVid) {
541                                 currentVid--;
542                                 if (isNb)
543                                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (currentVid << NB_VID_POS);
544                                 else
545                                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (currentVid << CPU_VID_POS);
546                                 wrmsr(0xC0010070, msr);
547
548                                 /* read F3xD8[VSRampTime]  */
549                                 dtemp = pci_read_config32(dev, 0xD8);
550                                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
551                                 vsTime = (int)timeTable[vsTimecode];
552                                 do {
553                                         coreDelay();
554                                         vsTime -= 40;
555                                 } while (vsTime > 0);
556                         }
557                 }
558         } else {                /* SVI, slam VID */
559                 if (isNb)
560                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (targetVid << NB_VID_POS);
561                 else
562                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (targetVid << CPU_VID_POS);
563                 wrmsr(0xC0010070, msr);
564
565                 /* read F3xD8[VSRampTime]  */
566                 dtemp = pci_read_config32(dev, 0xD8);
567                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
568                 vsTime = (int)timeTable[vsTimecode];
569                 do {
570                         coreDelay();
571                         vsTime -= 40;
572                 } while (vsTime > 0);
573         }
574 }
575
576 static u32 needs_NB_COF_VID_update(void)
577 {
578         u8 nb_cof_vid_update;
579         u8 nodes;
580         u8 i;
581
582         /* If any node has nb_cof_vid_update set all nodes need an update. */
583         nodes = get_nodes();
584         nb_cof_vid_update = 0;
585         for (i = 0; i < nodes; i++) {
586                 if (pci_read_config32(NODE_PCI(i, 3), 0x1FC) & 1) {
587                         nb_cof_vid_update = 1;
588                         break;
589                 }
590         }
591         return nb_cof_vid_update;
592 }
593
594 static u32 init_fidvid_core(u32 nodeid, u32 coreid)
595 {
596         device_t dev;
597         u32 vid_max;
598         u32 fid_max=0;
599         u8 nb_cof_vid_update = needs_NB_COF_VID_update();
600         u8 pvimode;
601         u32 reg1fc;
602
603         /* Steps 1-6 of BIOS NB COF and VID Configuration
604          * for SVI and Single-Plane PVI Systems.
605          */
606
607         dev = NODE_PCI(nodeid, 3);
608         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
609         reg1fc = pci_read_config32(dev, 0x1FC);
610
611         if (nb_cof_vid_update) {
612                 if (pvimode) {
613                         vid_max = (reg1fc >> 7) & 0x7F;
614                         fid_max = (reg1fc >> 2) & 0x1F;
615
616                         /* write newNbVid to P-state Reg's NbVid always if NbVidUpdatedAll=1 */
617                         fixPsNbVidBeforeWR(vid_max, coreid);
618                 } else {        /* SVI */
619                         vid_max = ((reg1fc >> 7) & 0x7F) - ((reg1fc >> 17) & 0x1F);
620                         fid_max = ((reg1fc >> 2) & 0x1F) + ((reg1fc >> 14) & 0x7);
621                         transitionVid(vid_max, dev, IS_NB);
622                 }
623
624                 /* fid setup is handled by the BSP at the end. */
625
626         } else {        /* ! nb_cof_vid_update */
627                 /* Use max values */
628                 if (pvimode)
629                         UpdateSinglePlaneNbVid();
630         }
631
632         return ((nb_cof_vid_update << 16) | (fid_max << 8));
633
634 }
635
636 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid)
637 {
638         u32 send;
639
640         printk(BIOS_DEBUG, "FIDVID on AP: %02x\n", apicid);
641
642         send = init_fidvid_core(nodeid,coreid); 
643         send |= (apicid << 24); // ap apicid
644
645         // Send signal to BSP about this AP max fid
646         // This also indicates this AP is ready for warm reset (if required).
647         lapic_write(LAPIC_MSG_REG, send | F10_APSTATE_RESET);
648 }
649
650 static u32 calc_common_fid(u32 fid_packed, u32 fid_packed_new)
651 {
652         u32 fidmax;
653         u32 fidmax_new;
654
655         fidmax = (fid_packed >> 8) & 0xFF;
656
657         fidmax_new = (fid_packed_new >> 8) & 0xFF;
658
659         if (fidmax > fidmax_new) {
660                 fidmax = fidmax_new;
661         }
662
663         fid_packed &= 0xFF << 16;
664         fid_packed |= (fidmax << 8);
665         fid_packed |= fid_packed_new & (0xFF << 16);    // set nb_cof_vid_update
666
667         return fid_packed;
668 }
669
670 static void init_fidvid_bsp_stage1(u32 ap_apicid, void *gp)
671 {
672         u32 readback = 0;
673         u32 timeout = 1;
674
675         struct fidvid_st *fvp = gp;
676         int loop;
677
678         print_debug_fv("Wait for AP stage 1: ap_apicid = ", ap_apicid);
679
680         loop = 100000;
681         while (--loop > 0) {
682                 if (lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback) != 0)
683                         continue;
684                 if ((readback & 0x3f) == 1) {
685                         timeout = 0;
686                         break;  /* target ap is in stage 1 */
687                 }
688         }
689
690         if (timeout) {
691                 printk(BIOS_DEBUG, "%s: timed out reading from ap %02x\n",
692                        __func__, ap_apicid);
693                 return;
694         }
695
696         print_debug_fv("\treadback = ", readback);
697
698         fvp->common_fid = calc_common_fid(fvp->common_fid, readback);
699
700         print_debug_fv("\tcommon_fid(packed) = ", fvp->common_fid);
701
702 }
703
704 static void updateSviPsNbVidAfterWR(u32 newNbVid)
705 {
706         msr_t msr;
707         u8 i;
708
709         /* This function copies newNbVid to NbVid bits in P-state Registers[4:0]
710          * for SVI mode.
711          */
712
713         for (i = 0; i < 5; i++) {
714                 msr = rdmsr(0xC0010064 + i);
715                 if ((msr.hi >> 31) & 1) {       /* PstateEn? */
716                         msr.lo &= ~(0x7F << 25);
717                         msr.lo |= (newNbVid & 0x7F) << 25;
718                         wrmsr(0xC0010064 + i, msr);
719                 }
720         }
721 }
722
723
724 static void fixPsNbVidAfterWR(u32 newNbVid, u8 NbVidUpdatedAll)
725 {
726         msr_t msr;
727         u8 i;
728         u8 StartupPstate;
729
730         /* This function copies newNbVid to NbVid bits in P-state
731          * Registers[4:0] if its NbDid bit=0 and PstateEn bit =1 in case of
732          * NbVidUpdatedAll =0 or copies copies newNbVid to NbVid bits in
733          * P-state Registers[4:0] if its and PstateEn bit =1 in case of
734          * NbVidUpdatedAll=1. Then transition to StartPstate.
735          */
736
737         /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
738         for (i = 0; i < 5; i++) {
739                 msr = rdmsr(0xC0010064 + i);
740                 /*  NbDid (bit 22 of P-state Reg) == 0  or NbVidUpdatedAll = 1 */
741                 if ((((msr.lo >> 22) & 1) == 0) || NbVidUpdatedAll) {
742                         msr.lo &= ~(0x7F << 25);
743                         msr.lo |= (newNbVid & 0x7F) << 25;
744                         wrmsr(0xC0010064 + i, msr);
745                 }
746         }
747
748         UpdateSinglePlaneNbVid();
749
750         /* For each core in the system, transition all cores to StartupPstate */
751         msr = rdmsr(0xC0010071);
752         StartupPstate = msr.hi & 0x07;
753         msr = rdmsr(0xC0010062);
754         msr.lo = StartupPstate;
755         wrmsr(0xC0010062, msr);
756
757         /* Wait for StartupPstate to set. */
758         do {
759                 msr = rdmsr(0xC0010063);
760         } while (msr.lo != StartupPstate);
761 }
762
763 static void set_p0(void)
764 {
765         msr_t msr;
766
767         // Transition P0 for calling core.
768         msr = rdmsr(0xC0010062);
769         msr.lo = (msr.lo & ~0x07);
770         wrmsr(0xC0010062, msr);
771
772         /* Wait for P0 to set. */
773         do {
774                 msr = rdmsr(0xC0010063);
775         } while (msr.lo != 0);
776 }
777
778 static void finalPstateChange(void)
779 {
780         /* Enble P0 on all cores for best performance.
781          * Linux can slow them down later if need be.
782          * It is safe since they will be in C1 halt
783          * most of the time anyway.
784          */
785         set_p0();
786 }
787
788 static void init_fidvid_stage2(u32 apicid, u32 nodeid)
789 {
790         msr_t msr;
791         device_t dev;
792         u32 reg1fc;
793         u32 dtemp;
794         u32 nbvid;
795         u8 nb_cof_vid_update = needs_NB_COF_VID_update();
796         u8 NbVidUpdateAll;
797         u8 pvimode;
798
799         /* After warm reset finish the fid/vid setup for all cores. */
800
801         /* If any node has nb_cof_vid_update set all nodes need an update. */
802
803         dev = NODE_PCI(nodeid, 3);
804         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
805         reg1fc = pci_read_config32(dev, 0x1FC);
806         nbvid = (reg1fc >> 7) & 0x7F;
807         NbVidUpdateAll = (reg1fc >> 1) & 1;
808
809         if (nb_cof_vid_update) {
810                 if (pvimode) {
811                         nbvid = (reg1fc >> 7) & 0x7F;
812                         /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
813                         fixPsNbVidAfterWR(nbvid, NbVidUpdateAll);
814                 } else {        /* SVI */
815                         nbvid = ((reg1fc >> 7) & 0x7F) - ((reg1fc >> 17) & 0x1F);
816                         updateSviPsNbVidAfterWR(nbvid);
817                 }
818         } else {                /* !nb_cof_vid_update */
819                 if (pvimode)
820                         UpdateSinglePlaneNbVid();
821         }
822         dtemp = pci_read_config32(dev, 0xA0);
823         dtemp &= PLLLOCK_OFF;
824         dtemp |= PLLLOCK_DFT_L;
825         pci_write_config32(dev, 0xA0, dtemp);
826
827         finalPstateChange();
828
829         /* Set TSC to tick at the P0 ndfid rate */
830         msr = rdmsr(HWCR);
831         msr.lo |= 1 << 24;
832         wrmsr(HWCR, msr);
833 }
834
835
836 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
837 struct ap_apicid_st {
838         u32 num;
839         // it could use 256 bytes for 64 node quad core system
840         u8 apicid[NODE_NUMS * 4];
841 };
842
843 static void store_ap_apicid(unsigned ap_apicid, void *gp)
844 {
845         struct ap_apicid_st *p = gp;
846
847         p->apicid[p->num++] = ap_apicid;
848
849 }
850 #endif
851
852
853 static int init_fidvid_bsp(u32 bsp_apicid, u32 nodes)
854 {
855 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
856         struct ap_apicid_st ap_apicidx;
857         u32 i;
858 #endif
859         struct fidvid_st fv;
860
861         printk(BIOS_DEBUG, "FIDVID on BSP, APIC_id: %02x\n", bsp_apicid);
862
863         /* Steps 1-6 of BIOS NB COF and VID Configuration
864          * for SVI and Single-Plane PVI Systems.
865          */
866
867         fv.common_fid = init_fidvid_core(0,0);
868
869         print_debug_fv("BSP fid = ", fv.common_fid);
870
871 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST && !CONFIG_SET_FIDVID_CORE0_ONLY
872         /* For all APs (We know the APIC ID of all APs even when the APIC ID
873            is lifted) remote read from AP LAPIC_MSG_REG about max fid.
874            Then calculate the common max fid that can be used for all
875            APs and BSP */
876         ap_apicidx.num = 0;
877
878         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE_RANGE, store_ap_apicid, &ap_apicidx);
879
880         for (i = 0; i < ap_apicidx.num; i++) {
881                 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
882         }
883 #else
884         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
885 #endif
886
887         print_debug_fv("common_fid = ", fv.common_fid);
888
889         if (fv.common_fid & (1 << 16)) {        /* check nb_cof_vid_update */
890
891                 // Enable the common fid and other settings.
892                 enable_fid_change((fv.common_fid >> 8) & 0x1F);
893
894                 // nbfid change need warm reset, so reset at first
895                 return 1;
896         }
897
898         return 0;               // No FID/VID changes. Don't reset
899 }
900 #endif