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