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