Prepare for next patches (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 void prep_fid_change(void)
183 {
184         u32 dword, dtemp;
185         u32 nodes;
186         device_t dev;
187         int i;
188
189         /* This needs to be run before any Pstate changes are requested */
190
191         nodes = get_nodes();
192
193         for (i = 0; i < nodes; i++) {
194                 printk(BIOS_DEBUG, "Prep FID/VID Node:%02x \n", i);
195                 dev = NODE_PCI(i, 3);
196
197                 setVSRamp(dev);
198                 /* BKDG r31116 2010-04-22  2.4.1.7 step b F3xD8[VSSlamTime] */
199                 /* Figure out the value for VsSlamTime and program it */
200                 recalculateVsSlamTimeSettingOnCorePre(dev);
201
202                 /* Program fields in Clock Power/Control register0 (F3xD4) */
203                 /* set F3xD4 Clock Power/Timing Control 0 Register
204                  * NbClkDidApplyAll=1b
205                  * NbClkDid=100b
206                  * PowerStepUp= "platform dependent"
207                  * PowerStepDown= "platform dependent"
208                  * LinkPllLink=01b
209                  * ClkRampHystSel=HW default
210                  */
211                 /* check platform type */
212                 if (!(get_platform_type() & AMD_PTYPE_SVR)) {
213                         /* For non-server platform
214                          * PowerStepUp=01000b - 50nS
215                          * PowerStepDown=01000b - 50ns
216                          */
217                         dword = pci_read_config32(dev, 0xd4);
218                         dword &= CPTC0_MASK;
219                         dword |= NB_CLKDID_ALL | NB_CLKDID | PW_STP_UP50 | PW_STP_DN50 | LNK_PLL_LOCK;  /* per BKDG */
220                         pci_write_config32(dev, 0xd4, dword);
221                 } else {
222                         dword = pci_read_config32(dev, 0xd4);
223                         dword &= CPTC0_MASK;
224                         /* get number of cores for PowerStepUp & PowerStepDown in server
225                            1 core - 400nS  - 0000b
226                            2 cores - 200nS - 0010b
227                            3 cores - 133nS -> 100nS - 0011b
228                            4 cores - 100nS - 0011b
229                          */
230                         switch (get_core_num_in_bsp(i)) {
231                         case 0:
232                                 dword |= PW_STP_UP400 | PW_STP_DN400;
233                                 break;
234                         case 1:
235                         case 2:
236                                 dword |= PW_STP_UP200 | PW_STP_DN200;
237                                 break;
238                         case 3:
239                                 dword |= PW_STP_UP100 | PW_STP_DN100;
240                                 break;
241                         default:
242                                 dword |= PW_STP_UP100 | PW_STP_DN100;
243                                 break;
244                         }
245                         dword |= NB_CLKDID_ALL | NB_CLKDID | LNK_PLL_LOCK;
246                         pci_write_config32(dev, 0xd4, dword);
247                 }
248
249                 /* check PVI/SVI */
250                 dword = pci_read_config32(dev, 0xA0);
251                 if (dword & PVI_MODE) { /* PVI */
252                         /* set slamVidMode to 0 for PVI */
253                         dword &= VID_SLAM_OFF | PLLLOCK_OFF;
254                         dword |= PLLLOCK_DFT_L;
255                         pci_write_config32(dev, 0xA0, dword);
256                 } else {        /* SVI */
257                         /* set slamVidMode to 1 for SVI */
258                         dword &= PLLLOCK_OFF;
259                         dword |= PLLLOCK_DFT_L | VID_SLAM_ON;
260                         pci_write_config32(dev, 0xA0, dword);
261
262                         dtemp = dword;
263
264                         /* Program F3xD8[PwrPlanes] according F3xA0[DulaVdd]  */
265                         dword = pci_read_config32(dev, 0xD8);
266
267                         if (dtemp & DUAL_VDD_BIT)
268                                 dword |= PWR_PLN_ON;
269                         else
270                                 dword &= PWR_PLN_OFF;
271                         pci_write_config32(dev, 0xD8, dword);
272                 }
273
274                 /* Note the following settings are additional from the ported
275                  * function setFidVidRegs()
276                  */
277                 dword = pci_read_config32(dev, 0xDc);
278                 dword |= 0x5 << 12;     /* NbsynPtrAdj set to 0x5 per BKDG (needs reset) */
279                 pci_write_config32(dev, 0xdc, dword);
280
281                 /* Rev B settings - FIXME: support other revs. */
282                 dword = 0xA0E641E6;
283                 pci_write_config32(dev, 0x84, dword);
284
285                 dword = 0xE600A681;
286                 pci_write_config32(dev, 0x80, dword);
287
288                 dword = pci_read_config32(dev, 0x80);
289                 printk(BIOS_DEBUG, "  F3x80: %08x \n", dword);
290                 dword = pci_read_config32(dev, 0x84);
291                 printk(BIOS_DEBUG, "  F3x84: %08x \n", dword);
292                 dword = pci_read_config32(dev, 0xD4);
293                 printk(BIOS_DEBUG, "  F3xD4: %08x \n", dword);
294                 dword = pci_read_config32(dev, 0xD8);
295                 printk(BIOS_DEBUG, "  F3xD8: %08x \n", dword);
296                 dword = pci_read_config32(dev, 0xDC);
297                 printk(BIOS_DEBUG, "  F3xDC: %08x \n", dword);
298
299
300         }
301 }
302
303
304 static void UpdateSinglePlaneNbVid(void)
305 {
306         u32 nbVid, cpuVid;
307         u8 i;
308         msr_t msr;
309
310         /* copy higher voltage (lower VID) of NBVID & CPUVID to both */
311         for (i = 0; i < 5; i++) {
312                 msr = rdmsr(PS_REG_BASE + i);
313                 nbVid = (msr.lo & PS_CPU_VID_M_ON) >> PS_CPU_VID_SHFT;
314                 cpuVid = (msr.lo & PS_NB_VID_M_ON) >> PS_NB_VID_SHFT;
315
316                 if (nbVid != cpuVid) {
317                         if (nbVid > cpuVid)
318                                 nbVid = cpuVid;
319
320                         msr.lo = msr.lo & PS_BOTH_VID_OFF;
321                         msr.lo = msr.lo | (u32) ((nbVid) << PS_NB_VID_SHFT);
322                         msr.lo = msr.lo | (u32) ((nbVid) << PS_CPU_VID_SHFT);
323                         wrmsr(PS_REG_BASE + i, msr);
324                 }
325         }
326 }
327
328 static void fixPsNbVidBeforeWR(u32 newNbVid, u32 coreid)
329 {
330         msr_t msr;
331         u8 startup_pstate;
332
333         /* This function sets NbVid before the warm reset.
334          *       Get StartupPstate from MSRC001_0071.
335          *       Read Pstate register pionted by [StartupPstate].
336          *       and copy its content to P0 and P1 registers.
337          *       Copy newNbVid to P0[NbVid].
338          *       transition to P1 on all cores,
339          *       then transition to P0 on core 0.
340          *       Wait for MSRC001_0063[CurPstate] = 000b on core 0.
341          */
342
343         msr = rdmsr(0xc0010071);
344         startup_pstate = (msr.hi >> (32 - 32)) & 0x07;
345
346         /* Copy startup pstate to P1 and P0 MSRs. Set the maxvid for this node in P0.
347          * Then transition to P1 for corex and P0 for core0.
348          * These setting will be cleared by the warm reset
349          */
350         msr = rdmsr(0xC0010064 + startup_pstate);
351         wrmsr(0xC0010065, msr);
352         wrmsr(0xC0010064, msr);
353
354         msr.lo &= ~0xFE000000;  // clear nbvid
355         msr.lo |= newNbVid << 25;
356         wrmsr(0xC0010064, msr);
357
358         UpdateSinglePlaneNbVid();
359
360         // Transition to P1 for all APs and P0 for core0.
361         msr = rdmsr(0xC0010062);
362         msr.lo = (msr.lo & ~0x07) | 1;
363         wrmsr(0xC0010062, msr);
364
365         // Wait for P1 to set.
366         do {
367                 msr = rdmsr(0xC0010063);
368         } while (msr.lo != 1);
369
370         if (coreid == 0) {
371                 msr.lo = msr.lo & ~0x07;
372                 wrmsr(0xC0010062, msr);
373                 // Wait for P0 to set.
374                 do {
375                         msr = rdmsr(0xC0010063);
376                 } while (msr.lo != 0);
377         }
378 }
379
380 static void coreDelay(void)
381 {
382         u32 saved;
383         u32 hi, lo, msr;
384         u32 cycles;
385
386         /* delay ~40us
387            This seems like a hack to me...
388            It would be nice to have a central delay function. */
389
390         cycles = 8000 << 3;     /* x8 (number of 1.25ns ticks) */
391
392         msr = 0x10;             /* TSC */
393         _RDMSR(msr, &lo, &hi);
394         saved = lo;
395         do {
396                 _RDMSR(msr, &lo, &hi);
397         } while (lo - saved < cycles);
398 }
399
400 static void transitionVid(u32 targetVid, u8 dev, u8 isNb)
401 {
402         u32 currentVid, dtemp;
403         msr_t msr;
404         u8 vsTimecode;
405         u16 timeTable[8] = { 10, 20, 30, 40, 60, 100, 200, 500 };
406         int vsTime;
407
408         /* This function steps or slam the Nb VID to the target VID.
409          * It uses VSRampTime for [SlamVidMode]=0 ([PviMode]=1)
410          * or VSSlamTime for [SlamVidMode]=1 ([PviMode]=0)to time period.
411          */
412
413         /* get the current VID */
414         msr = rdmsr(0xC0010071);
415         if (isNb)
416                 currentVid = (msr.lo >> NB_VID_POS) & BIT_MASK_7;
417         else
418                 currentVid = (msr.lo >> CPU_VID_POS) & BIT_MASK_7;
419
420         /* Read MSRC001_0070 COFVID Control Register */
421         msr = rdmsr(0xC0010070);
422
423         /* check PVI/SPI */
424         dtemp = pci_read_config32(dev, 0xA0);
425         if (dtemp & PVI_MODE) { /* PVI, step VID */
426                 if (currentVid < targetVid) {
427                         while (currentVid < targetVid) {
428                                 currentVid++;
429                                 if (isNb)
430                                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (currentVid << NB_VID_POS);
431                                 else
432                                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (currentVid << CPU_VID_POS);
433                                 wrmsr(0xC0010070, msr);
434
435                                 /* read F3xD8[VSRampTime]  */
436                                 dtemp = pci_read_config32(dev, 0xD8);
437                                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
438                                 vsTime = (int)timeTable[vsTimecode];
439                                 do {
440                                         coreDelay();
441                                         vsTime -= 40;
442                                 } while (vsTime > 0);
443                         }
444                 } else if (currentVid > targetVid) {
445                         while (currentVid > targetVid) {
446                                 currentVid--;
447                                 if (isNb)
448                                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (currentVid << NB_VID_POS);
449                                 else
450                                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (currentVid << CPU_VID_POS);
451                                 wrmsr(0xC0010070, msr);
452
453                                 /* read F3xD8[VSRampTime]  */
454                                 dtemp = pci_read_config32(dev, 0xD8);
455                                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
456                                 vsTime = (int)timeTable[vsTimecode];
457                                 do {
458                                         coreDelay();
459                                         vsTime -= 40;
460                                 } while (vsTime > 0);
461                         }
462                 }
463         } else {                /* SVI, slam VID */
464                 if (isNb)
465                         msr.lo = (msr.lo & NB_VID_MASK_OFF) | (targetVid << NB_VID_POS);
466                 else
467                         msr.lo = (msr.lo & CPU_VID_MASK_OFF) | (targetVid << CPU_VID_POS);
468                 wrmsr(0xC0010070, msr);
469
470                 /* read F3xD8[VSRampTime]  */
471                 dtemp = pci_read_config32(dev, 0xD8);
472                 vsTimecode = (u8) ((dtemp >> VS_RAMP_T) & 0x7);
473                 vsTime = (int)timeTable[vsTimecode];
474                 do {
475                         coreDelay();
476                         vsTime -= 40;
477                 } while (vsTime > 0);
478         }
479 }
480
481
482 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid)
483 {
484         device_t dev;
485         u32 vid_max;
486         u32 fid_max;
487         u8 nb_cof_vid_update;
488         u8 pvimode;
489         u32 reg1fc;
490         u32 send;
491         u8 nodes;
492         u8 i;
493
494         printk(BIOS_DEBUG, "FIDVID on AP: %02x\n", apicid);
495
496         /* Steps 1-6 of BIOS NB COF and VID Configuration
497          * for SVI and Single-Plane PVI Systems.
498          */
499
500         /* If any node has nb_cof_vid_update set all nodes need an update. */
501         nodes = get_nodes();
502         nb_cof_vid_update = 0;
503         for (i = 0; i < nodes; i++) {
504                 if (pci_read_config32(NODE_PCI(i, 3), 0x1FC) & 1) {
505                         nb_cof_vid_update = 1;
506                         break;
507                 }
508         }
509
510         dev = NODE_PCI(nodeid, 3);
511         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
512         reg1fc = pci_read_config32(dev, 0x1FC);
513
514         if (nb_cof_vid_update) {
515                 if (pvimode) {
516                         vid_max = (reg1fc >> 7) & 0x7F;
517                         fid_max = (reg1fc >> 2) & 0x1F;
518
519                         /* write newNbVid to P-state Reg's NbVid always if NbVidUpdatedAll=1 */
520                         fixPsNbVidBeforeWR(vid_max, coreid);
521                 } else {        /* SVI */
522                         vid_max = ((reg1fc >> 7) & 0x7F) - ((reg1fc >> 17) & 0x1F);
523                         fid_max = ((reg1fc >> 2) & 0x1F) + ((reg1fc >> 14) & 0x7);
524                         transitionVid(vid_max, dev, IS_NB);
525                 }
526
527                 /* fid setup is handled by the BSP at the end. */
528
529         } else {        /* ! nb_cof_vid_update */
530                 /* Use max values */
531                 if (pvimode)
532                         UpdateSinglePlaneNbVid();
533         }
534
535         send = (nb_cof_vid_update << 16) | (fid_max << 8);
536         send |= (apicid << 24); // ap apicid
537
538         // Send signal to BSP about this AP max fid
539         // This also indicates this AP is ready for warm reset (if required).
540         lapic_write(LAPIC_MSG_REG, send | F10_APSTATE_RESET);
541 }
542
543 static u32 calc_common_fid(u32 fid_packed, u32 fid_packed_new)
544 {
545         u32 fidmax;
546         u32 fidmax_new;
547
548         fidmax = (fid_packed >> 8) & 0xFF;
549
550         fidmax_new = (fid_packed_new >> 8) & 0xFF;
551
552         if (fidmax > fidmax_new) {
553                 fidmax = fidmax_new;
554         }
555
556         fid_packed &= 0xFF << 16;
557         fid_packed |= (fidmax << 8);
558         fid_packed |= fid_packed_new & (0xFF << 16);    // set nb_cof_vid_update
559
560         return fid_packed;
561 }
562
563 static void init_fidvid_bsp_stage1(u32 ap_apicid, void *gp)
564 {
565         u32 readback = 0;
566         u32 timeout = 1;
567
568         struct fidvid_st *fvp = gp;
569         int loop;
570
571         print_debug_fv("Wait for AP stage 1: ap_apicid = ", ap_apicid);
572
573         loop = 100000;
574         while (--loop > 0) {
575                 if (lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback) != 0)
576                         continue;
577                 if ((readback & 0x3f) == 1) {
578                         timeout = 0;
579                         break;  /* target ap is in stage 1 */
580                 }
581         }
582
583         if (timeout) {
584                 printk(BIOS_DEBUG, "%s: timed out reading from ap %02x\n",
585                        __func__, ap_apicid);
586                 return;
587         }
588
589         print_debug_fv("\treadback = ", readback);
590
591         fvp->common_fid = calc_common_fid(fvp->common_fid, readback);
592
593         print_debug_fv("\tcommon_fid(packed) = ", fvp->common_fid);
594
595 }
596
597 static void updateSviPsNbVidAfterWR(u32 newNbVid)
598 {
599         msr_t msr;
600         u8 i;
601
602         /* This function copies newNbVid to NbVid bits in P-state Registers[4:0]
603          * for SVI mode.
604          */
605
606         for (i = 0; i < 5; i++) {
607                 msr = rdmsr(0xC0010064 + i);
608                 if ((msr.hi >> 31) & 1) {       /* PstateEn? */
609                         msr.lo &= ~(0x7F << 25);
610                         msr.lo |= (newNbVid & 0x7F) << 25;
611                         wrmsr(0xC0010064 + i, msr);
612                 }
613         }
614 }
615
616
617 static void fixPsNbVidAfterWR(u32 newNbVid, u8 NbVidUpdatedAll)
618 {
619         msr_t msr;
620         u8 i;
621         u8 StartupPstate;
622
623         /* This function copies newNbVid to NbVid bits in P-state
624          * Registers[4:0] if its NbDid bit=0 and PstateEn bit =1 in case of
625          * NbVidUpdatedAll =0 or copies copies newNbVid to NbVid bits in
626          * P-state Registers[4:0] if its and PstateEn bit =1 in case of
627          * NbVidUpdatedAll=1. Then transition to StartPstate.
628          */
629
630         /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
631         for (i = 0; i < 5; i++) {
632                 msr = rdmsr(0xC0010064 + i);
633                 /*  NbDid (bit 22 of P-state Reg) == 0  or NbVidUpdatedAll = 1 */
634                 if ((((msr.lo >> 22) & 1) == 0) || NbVidUpdatedAll) {
635                         msr.lo &= ~(0x7F << 25);
636                         msr.lo |= (newNbVid & 0x7F) << 25;
637                         wrmsr(0xC0010064 + i, msr);
638                 }
639         }
640
641         UpdateSinglePlaneNbVid();
642
643         /* For each core in the system, transition all cores to StartupPstate */
644         msr = rdmsr(0xC0010071);
645         StartupPstate = msr.hi & 0x07;
646         msr = rdmsr(0xC0010062);
647         msr.lo = StartupPstate;
648         wrmsr(0xC0010062, msr);
649
650         /* Wait for StartupPstate to set. */
651         do {
652                 msr = rdmsr(0xC0010063);
653         } while (msr.lo != StartupPstate);
654 }
655
656 static void set_p0(void)
657 {
658         msr_t msr;
659
660         // Transition P0 for calling core.
661         msr = rdmsr(0xC0010062);
662         msr.lo = (msr.lo & ~0x07);
663         wrmsr(0xC0010062, msr);
664
665         /* Wait for P0 to set. */
666         do {
667                 msr = rdmsr(0xC0010063);
668         } while (msr.lo != 0);
669 }
670
671 static void finalPstateChange(void)
672 {
673         /* Enble P0 on all cores for best performance.
674          * Linux can slow them down later if need be.
675          * It is safe since they will be in C1 halt
676          * most of the time anyway.
677          */
678         set_p0();
679 }
680
681 static void init_fidvid_stage2(u32 apicid, u32 nodeid)
682 {
683         msr_t msr;
684         device_t dev;
685         u32 reg1fc;
686         u32 dtemp;
687         u32 nbvid;
688         u8 nb_cof_vid_update;
689         u8 nodes;
690         u8 NbVidUpdateAll;
691         u8 i;
692         u8 pvimode;
693
694         /* After warm reset finish the fid/vid setup for all cores. */
695
696         /* If any node has nb_cof_vid_update set all nodes need an update. */
697         nodes = get_nodes();
698         nb_cof_vid_update = 0;
699         for (i = 0; i < nodes; i++) {
700                 if (pci_read_config32(NODE_PCI(i, 3), 0x1FC) & 1) {
701                         nb_cof_vid_update = 1;
702                         break;
703                 }
704         }
705
706         dev = NODE_PCI(nodeid, 3);
707         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
708         reg1fc = pci_read_config32(dev, 0x1FC);
709         nbvid = (reg1fc >> 7) & 0x7F;
710         NbVidUpdateAll = (reg1fc >> 1) & 1;
711
712         if (nb_cof_vid_update) {
713                 if (pvimode) {
714                         nbvid = (reg1fc >> 7) & 0x7F;
715                         /* write newNbVid to P-state Reg's NbVid if its NbDid=0 */
716                         fixPsNbVidAfterWR(nbvid, NbVidUpdateAll);
717                 } else {        /* SVI */
718                         nbvid = ((reg1fc >> 7) & 0x7F) - ((reg1fc >> 17) & 0x1F);
719                         updateSviPsNbVidAfterWR(nbvid);
720                 }
721         } else {                /* !nb_cof_vid_update */
722                 if (pvimode)
723                         UpdateSinglePlaneNbVid();
724         }
725         dtemp = pci_read_config32(dev, 0xA0);
726         dtemp &= PLLLOCK_OFF;
727         dtemp |= PLLLOCK_DFT_L;
728         pci_write_config32(dev, 0xA0, dtemp);
729
730         finalPstateChange();
731
732         /* Set TSC to tick at the P0 ndfid rate */
733         msr = rdmsr(HWCR);
734         msr.lo |= 1 << 24;
735         wrmsr(HWCR, msr);
736 }
737
738
739 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
740 struct ap_apicid_st {
741         u32 num;
742         // it could use 256 bytes for 64 node quad core system
743         u8 apicid[NODE_NUMS * 4];
744 };
745
746 static void store_ap_apicid(unsigned ap_apicid, void *gp)
747 {
748         struct ap_apicid_st *p = gp;
749
750         p->apicid[p->num++] = ap_apicid;
751
752 }
753 #endif
754
755
756 static int init_fidvid_bsp(u32 bsp_apicid, u32 nodes)
757 {
758 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST
759         struct ap_apicid_st ap_apicidx;
760         u32 i;
761 #endif
762         struct fidvid_st fv;
763         device_t dev;
764         u32 vid_max;
765         u32 fid_max=0;
766         u8 nb_cof_vid_update;
767         u32 reg1fc;
768         u8 pvimode;
769
770         printk(BIOS_DEBUG, "FIDVID on BSP, APIC_id: %02x\n", bsp_apicid);
771         /* FIXME: The first half of this function is nearly the same as
772          * init_fidvid_bsp() and the code could be combined.
773          */
774
775         /* Steps 1-6 of BIOS NB COF and VID Configuration
776          * for SVI and Single-Plane PVI Systems.
777          */
778
779         /* If any node has nb_cof_vid_update set all nodes need an update. */
780         nb_cof_vid_update = 0;
781         for (i = 0; i < nodes; i++) {
782                 if (pci_read_config32(NODE_PCI(i, 3), 0x1FC) & 1) {
783                         nb_cof_vid_update = 1;
784                         break;
785                 }
786         }
787
788         dev = NODE_PCI(0, 3);
789         pvimode = (pci_read_config32(dev, 0xA0) >> 8) & 1;
790         reg1fc = pci_read_config32(dev, 0x1FC);
791
792         if (nb_cof_vid_update) {
793                 if (pvimode) {
794                         vid_max = (reg1fc >> 7) & 0x7F;
795                         fid_max = (reg1fc >> 2) & 0x1F;
796
797                         /* write newNbVid to P-state Reg's NbVid always if NbVidUpdatedAll=1 */
798                         fixPsNbVidBeforeWR(vid_max, 0);
799                 } else {        /* SVI */
800                         vid_max = ((reg1fc >> 7) & 0x7F) - ((reg1fc >> 17) & 0x1F);
801                         fid_max = ((reg1fc >> 2) & 0x1F) + ((reg1fc >> 14) & 0x7);
802                         transitionVid(vid_max, dev, IS_NB);
803                 }
804
805                 /*  fid setup is handled by the BSP at the end. */
806
807         } else {                /* ! nb_cof_vid_update */
808                 /* Use max values */
809                 if (pvimode)
810                         UpdateSinglePlaneNbVid();
811         }
812
813         fv.common_fid = (nb_cof_vid_update << 16) | (fid_max << 8);
814         print_debug_fv("BSP fid = ", fv.common_fid);
815
816 #if CONFIG_SET_FIDVID_STORE_AP_APICID_AT_FIRST && !CONFIG_SET_FIDVID_CORE0_ONLY
817         /* For all APs (We know the APIC ID of all APs even when the APIC ID
818            is lifted) remote read from AP LAPIC_MSG_REG about max fid.
819            Then calculate the common max fid that can be used for all
820            APs and BSP */
821         ap_apicidx.num = 0;
822
823         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE_RANGE, store_ap_apicid, &ap_apicidx);
824
825         for (i = 0; i < ap_apicidx.num; i++) {
826                 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
827         }
828 #else
829         for_each_ap(bsp_apicid, CONFIG_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
830 #endif
831
832         print_debug_fv("common_fid = ", fv.common_fid);
833
834         if (fv.common_fid & (1 << 16)) {        /* check nb_cof_vid_update */
835
836                 // Enable the common fid and other settings.
837                 enable_fid_change((fv.common_fid >> 8) & 0x1F);
838
839                 // nbfid change need warm reset, so reset at first
840                 return 1;
841         }
842
843         return 0;               // No FID/VID changes. Don't reset
844 }
845 #endif