94ee3bc010ad162f7c08ca1f87372406fd523f71
[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 FAM10_SET_FIDVID == 1
21
22 #define FAM10_SET_FIDVID_DEBUG 1
23
24 // if we are tight of CAR stack, disable it
25 #define FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST 1
26
27 static inline void print_debug_fv(const char *str, u32 val)
28 {
29 #if FAM10_SET_FIDVID_DEBUG == 1
30                 printk_debug("%s%x\n", str, val);
31 #endif
32 }
33
34 static inline void print_debug_fv_8(const char *str, u8 val)
35 {
36 #if FAM10_SET_FIDVID_DEBUG == 1
37                 printk_debug("%s%02x\n", str, val);
38 #endif
39 }
40
41 static inline void print_debug_fv_64(const char *str, u32 val, u32 val2)
42 {
43 #if FAM10_SET_FIDVID_DEBUG == 1
44                 printk_debug("%s%x%x\n", str, val, val2);
45 #endif
46 }
47
48
49 static void enable_fid_change(u8 fid)
50 {
51         u32 dword;
52         u32 nodes;
53         device_t dev;
54         int i;
55
56         nodes = ((pci_read_config32(PCI_DEV(CBB, CDB, 0), 0x60) >> 4) & 7) + 1;
57
58         for(i = 0; i < nodes; i++) {
59                 dev = NODE_PCI(i,3);
60                 dword = pci_read_config32(dev, 0xd4);
61                 dword &= ~0x1F;
62                 dword |= (u32) fid & 0x1F;
63                 dword |= 1 << 5;        // enable
64                 pci_write_config32(dev, 0xd4, dword);
65                 printk_debug("FID Change Node:%02x, F3xD4: %08x \n", i, dword);
66         }
67 }
68
69 static void prep_fid_change(void)
70 {
71         u32 dword;
72         u32 nodes;
73         device_t dev;
74         int i;
75
76         /* This needs to be run before any Pstate changes are requested */
77
78         nodes = ((pci_read_config32(PCI_DEV(CBB, CDB, 0), 0x60) >> 4) & 7) + 1;
79
80         for(i = 0; i < nodes; i++) {
81                 printk_debug("Node:%02x \n", i);
82                 dev = NODE_PCI(i,3);
83
84                 dword = pci_read_config32(dev, 0xa0);
85                 dword &= ~(1<<29);
86                 dword |= ((~dword >> 8) & 1) << 29; // SlamVidMode is the inverse to the PviMode
87                 dword |= PLLLOCK_DFT_L; /* Force per BKDG */
88                 pci_write_config32(dev, 0xa0, dword);
89                 printk_debug("  F3xA0: %08x \n", dword);
90
91                 dword = pci_read_config32(dev, 0xd8);
92                 dword &= ~0x77;
93                 dword |= (1<<4) | 6; // VSRampTime, and VSSlamTime
94                 dword |= 3 << 24; // ReConDel set to 3 per BKDG
95                 pci_write_config32(dev, 0xd8, dword);
96                 printk_debug("  F3xD8: %08x \n", dword);
97
98                 dword = pci_read_config32(dev, 0xd4);
99                 dword &= 0x1F;
100                 dword |= 0xC331AF00; // per BKDG
101                 pci_write_config32(dev, 0xd4, dword);
102                 printk_debug("  F3xD4: %08x \n", dword);
103
104                 dword = pci_read_config32(dev, 0xdc);
105                 dword |= 0x5 << 12; // NbsynPtrAdj set to 0x5 per BKDG (needs reset)
106                 pci_write_config32(dev, 0xdc, dword);
107                 printk_debug("  F3xDC: %08x \n", dword);
108
109                 // Rev B settings - FIXME: support other revs.
110                 dword = 0xA0E641E6;
111                 pci_write_config32(dev, 0x84, dword);
112                 printk_debug("  F3x84: %08x \n", dword);
113
114                 dword = 0xE600A681;
115                 pci_write_config32(dev, 0x80, dword);
116                 printk_debug("  F3x80: %08x \n", dword);
117
118         }
119 }
120
121
122 #include "fidvid_common.c"
123
124
125
126 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid)
127 {
128
129         msr_t msr;
130         device_t dev;
131         u8 vid_max;
132         u8 fid_max;
133         u8 startup_pstate;
134         u8 nb_cof_vid_update;
135         u8 pvimode;
136         u32 reg1fc;
137         u32 dword;
138         u32 send;
139
140         printk_debug("FIDVID on AP: %02x\n", apicid);
141
142         /* Only support single plane system at this time. */
143         /* Steps 1-6 of BIOS NB COF and VID Configuration
144          * for Single-Plane PVI Systems
145          */
146         dev = NODE_PCI(nodeid,3);
147         reg1fc = pci_read_config32(dev, 0x1FC);
148         nb_cof_vid_update = reg1fc & 1;
149         if (nb_cof_vid_update) {
150                 /* Get fused settings */
151                 dword = pci_read_config32(dev, 0xa0);
152                         pvimode = (dword >> 8) & 1;
153
154                 vid_max = (reg1fc >> 7) & 0x7F; // per node
155                 fid_max = (reg1fc >> 2) & 0x1F; // per system
156
157                 if (pvimode) {
158                 /* FIXME: support daul plane mode */
159                         die("PVImode not supported\n");
160                         /* fidmax = vidmax - (reg1fc >> 17) & 0x1F;
161                         fidmax = fidmax + (reg1fc >> 14) & 0x03;
162                         */
163                 }
164
165         } else {
166                 /* Use current values */
167                 msr = rdmsr(0xc0010071);
168                 fid_max = ((msr.hi >> (59-32)) & 0x1f);         //max nb fid
169                 vid_max = ((msr.hi >> (35-32)) & 0x7f);         //max vid
170         }
171
172         /* Note this is the single plane setup. Need to add dual plane path */
173         msr = rdmsr(0xc0010071);
174         startup_pstate = (msr.hi >> (32-32)) & 0x07;
175
176
177         /* Copy startup pstate to P1 and P0 MSRs. Set the maxvid for this node in P0.
178            Then transition to P1 for corex and P0 for core0. */
179         msr = rdmsr(0xC0010064 + startup_pstate);
180         wrmsr(0xC0010065, msr);
181         wrmsr(0xC0010064, msr);
182
183         msr.lo &= ~0xFE000000;  // clear nbvid
184         msr.lo |= vid_max << 25;
185         wrmsr(0xC0010064, msr);
186
187         // Transition to P1 for all APs and P0 for core0.
188         msr = rdmsr(0xC0010062);
189         msr.lo = (msr.lo & ~0x07) | 1;
190         wrmsr(0xC0010062, msr);
191
192         // Wait for P1 to set.
193         do {
194                 msr = rdmsr(0xC0010063);
195         } while (msr.lo != 1);
196
197         if (coreid == 0) {
198                 msr.lo = msr.lo & ~0x07;
199                 wrmsr(0xC0010062, msr);
200                 // Wait for P0 to set.
201                 do {
202                         msr = rdmsr(0xC0010063);
203                 } while (msr.lo != 0);
204         }
205
206
207         send = (nb_cof_vid_update << 16) | (fid_max << 8);
208         send |= (apicid << 24); // ap apicid
209
210         // Send signal to BSP about this AP max fid
211         // This also indicates this AP is ready for warm reset (if required).
212         lapic_write(LAPIC_MSG_REG, send | 1);
213 }
214
215 static u32 calc_common_fid(u32 fid_packed, u32 fid_packed_new)
216 {
217         u32 fidmax;
218         u32 fidmax_new;
219
220         fidmax = (fid_packed >> 8) & 0xFF;
221
222         fidmax_new = (fid_packed_new >> 8) & 0xFF;
223
224         if(fidmax > fidmax_new) {
225                 fidmax = fidmax_new;
226         }
227
228         fid_packed &= 0xFF << 16;
229         fid_packed |= (fidmax << 8);
230         fid_packed |= fid_packed_new & (0xFF << 16); // set nb_cof_vid_update
231
232         return fid_packed;
233 }
234
235 struct fidvid_st {
236         u32 common_fid;
237 };
238
239 static void init_fidvid_bsp_stage1(u32 ap_apicid, void *gp )
240 {
241                 u32 readback = 0;
242                 u32 timeout = 1;
243
244                 struct fidvid_st *fvp = gp;
245                 int loop;
246
247                 print_debug_fv("Wait for AP stage 1: ap_apicid = ", ap_apicid);
248
249                 loop = 100000;
250                 while(--loop > 0) {
251                         if(lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback) != 0) continue;
252                         if((readback & 0x3f) == 1) {
253                                 timeout = 0;
254                                 break; //target ap is in stage 1
255                         }
256                 }
257
258                 if(timeout) {
259                         print_initcpu8("fidvid_bsp_stage1: time out while reading from ap ", ap_apicid);
260                         return;
261                 }
262
263                 print_debug_fv("\treadback = ", readback);
264
265                 fvp->common_fid = calc_common_fid(fvp->common_fid, readback);
266
267                 print_debug_fv("\tcommon_fid(packed) = ", fvp->common_fid);
268
269 }
270
271
272 static void init_fidvid_stage2(u32 apicid, u32 nodeid)
273 {
274         msr_t msr;
275         device_t dev;
276         u32 reg1fc;
277         u8 StartupPstate;
278         u8 nbvid;
279         int i;
280
281         /* After warm reset finish the fid/vid setup for all cores. */
282         dev = NODE_PCI(nodeid,3);
283         reg1fc = pci_read_config32(dev, 0x1FC);
284         nbvid = (reg1fc >> 7) & 0x7F;
285
286         if (reg1fc & 0x02) { // NbVidUpdateAll ?
287                 for( i = 0; i < 5; i++) {
288                         msr = rdmsr(0xC0010064 + i);
289                         if ((msr.hi >> 31) & 1) { // PstateEn?
290                                 msr.lo &= ~(0x7F << 25);
291                                 msr.lo |= (nbvid & 0x7F) << 25;
292                         }
293                 }
294         } else {
295                 for( i = 0; i < 5; i++) {
296                         msr = rdmsr(0xC0010064 + i);
297                         if (((msr.hi >> 31) & 1) && (((msr.lo >> 22) & 1) == 0)) { // PstateEn and PDid == 0?
298                                 msr.lo &= ~(0x7F << 25);
299                                 msr.lo |= (nbvid & 0x7F) << 25;
300                         }
301                 }
302         }
303
304         // For each processor in the system, transition all cores to StartupPstate
305         msr = rdmsr(0xC0010071);
306         StartupPstate = msr.hi >> (32-32) & 0x03;
307         msr = rdmsr(0xC0010062);
308         msr.lo = StartupPstate;
309         wrmsr(0xC0010062, msr);
310 }
311
312
313 #if FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
314 struct ap_apicid_st {
315         u32 num;
316         // it could use 256 bytes for 64 node quad core system
317         u8 apicid[NODE_NUMS * 4];
318 };
319
320 static void store_ap_apicid(unsigned ap_apicid, void *gp)
321 {
322         struct ap_apicid_st *p = gp;
323
324         p->apicid[p->num++] = ap_apicid;
325
326 }
327 #endif
328
329
330 static int init_fidvid_bsp(u32 bsp_apicid, u32 nodes)
331 {
332 #if FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
333         struct ap_apicid_st ap_apicidx;
334         u32 i;
335 #endif
336         struct fidvid_st fv;
337         msr_t msr;
338         device_t dev;
339         u8 vid_max;
340         u8 fid_max;
341         u8 startup_pstate;
342         u8 nb_cof_vid_update;
343         u32 reg1fc;
344         u32 dword;
345         u8 pvimode;
346
347         printk_debug("FIDVID on BSP, APIC_id: %02x\n", bsp_apicid);
348
349         /* FIXME: Only support single plane system at this time. */
350         /* Steps 1-6 of BIOS NB COF and VID Configuration
351          * for Single-Plane PVI Systems
352          */
353         dev = NODE_PCI(0,3);    // nodeid for the BSP is 0
354         reg1fc = pci_read_config32(dev, 0x1FC);
355         nb_cof_vid_update = reg1fc & 1;
356         if (nb_cof_vid_update) {
357                 /* Get fused settings */
358                 dword = pci_read_config32(dev, 0xa0);
359                         pvimode = (dword >> 8) & 1;
360
361                 vid_max = (reg1fc >> 7) & 0x7F; // per node
362                 fid_max = (reg1fc >> 2) & 0x1F; // per system
363
364                 if (pvimode) {
365                 /* FIXME: support daul plane mode */
366                         die("PVImode not supported\n");
367                         /* fidmax = vidmax - (reg1fc >> 17) & 0x1F;
368                         fidmax = fidmax + (reg1fc >> 14) & 0x03;
369                         */
370                 }
371
372         } else {
373                 /* Use current values */
374                 msr = rdmsr(0xc0010071);
375                 fid_max = ((msr.hi >> (59-32)) & 0x1f);         //max nb fid
376                 vid_max = ((msr.hi >> (35-32)) & 0x7f);         //max vid
377         }
378
379         /* Note this is the single plane setup. Need to add dual plane path */
380         msr = rdmsr(0xc0010071);
381         startup_pstate = (msr.hi >> (32-32)) & 0x07;
382
383
384         /* Copy startup pstate to P1 and P0 MSRs. Set the maxvid for this node in P0.
385            Then transition to P1 for corex and P0 for core0. */
386         msr = rdmsr(0xC0010064 + startup_pstate);
387         wrmsr(0xC0010065, msr);
388         wrmsr(0xC0010064, msr);
389
390         msr.lo &= ~0xFE000000;  // clear nbvid
391         msr.lo |= vid_max << 25;
392         wrmsr(0xC0010064, msr);
393
394         // Transition to P1 and then P0 for core0.
395         msr = rdmsr(0xC0010062);
396         msr.lo = (msr.lo & ~0x07) | 1;
397         wrmsr(0xC0010062, msr);
398
399         // Wait for P1 to set.
400         do {
401                 msr = rdmsr(0xC0010063);
402         } while (msr.lo != 1);
403
404         msr.lo = msr.lo & ~0x07;
405         wrmsr(0xC0010062, msr);
406         // Wait for P0 to set.
407         do {
408                 msr = rdmsr(0xC0010063);
409         } while (msr.lo != 0);
410
411
412         fv.common_fid = (nb_cof_vid_update << 16) | (fid_max << 8) ;
413         print_debug_fv("BSP fid = ", fv.common_fid);
414
415 #if FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1 && FAM10_SET_FIDVID_CORE0_ONLY == 0
416         /* For all APs (We know the APIC ID of all APs even when the APIC ID
417            is lifted) remote read from AP LAPIC_MSG_REG about max fid.
418            Then calculate the common max fid that can be used for all
419            APs and BSP */
420         ap_apicidx.num = 0;
421
422         for_each_ap(bsp_apicid, FAM10_SET_FIDVID_CORE_RANGE, store_ap_apicid, &ap_apicidx);
423
424         for(i = 0; i < ap_apicidx.num; i++) {
425                 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
426         }
427 #else
428         for_each_ap(bsp_apicid, FAM10_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
429 #endif
430
431         print_debug_fv("common_fid = ", fv.common_fid);
432
433         if (fv.common_fid & ~(0xFF << 16)) {    // check nb_cof_vid_update
434
435                 // Enable the common fid and other settings.
436                 enable_fid_change((fv.common_fid >> 8) & 0x1F);
437
438                 // nbfid change need warm reset, so reset at first
439                 return 1;
440         }
441
442         return 0; // No FID/VID changes. Don't reset
443 }
444 static void set_p0(void)
445 {
446         msr_t msr;
447
448         // Transition P0 for calling core.
449         msr = rdmsr(0xC0010062);
450         msr.lo = (msr.lo & ~0x07);
451         wrmsr(0xC0010062, msr);
452
453         // Don't bother to wait around for the P state to change.
454 }
455 #endif