2 * This file is part of the coreboot project.
4 * Copyright (C) 2007 Advanced Micro Devices, Inc.
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.
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.
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
20 #if FAM10_SET_FIDVID == 1
22 #define FAM10_SET_FIDVID_DEBUG 1
24 // if we are tight of CAR stack, disable it
25 #define FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST 1
27 static inline void print_debug_fv(const char *str, u32 val)
29 #if FAM10_SET_FIDVID_DEBUG == 1
30 printk_debug("%s%x\n", str, val);
34 static inline void print_debug_fv_8(const char *str, u8 val)
36 #if FAM10_SET_FIDVID_DEBUG == 1
37 printk_debug("%s%02x\n", str, val);
41 static inline void print_debug_fv_64(const char *str, u32 val, u32 val2)
43 #if FAM10_SET_FIDVID_DEBUG == 1
44 printk_debug("%s%x%x\n", str, val, val2);
49 static void enable_fid_change(u8 fid)
56 nodes = ((pci_read_config32(PCI_DEV(CBB, CDB, 0), 0x60) >> 4) & 7) + 1;
58 for(i = 0; i < nodes; i++) {
60 dword = pci_read_config32(dev, 0xd4);
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);
69 static void prep_fid_change(void)
76 /* This needs to be run before any Pstate changes are requested */
78 nodes = ((pci_read_config32(PCI_DEV(CBB, CDB, 0), 0x60) >> 4) & 7) + 1;
80 for(i = 0; i < nodes; i++) {
81 printk_debug("Node:%02x \n", i);
84 dword = pci_read_config32(dev, 0xa0);
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);
91 dword = pci_read_config32(dev, 0xd8);
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);
98 dword = pci_read_config32(dev, 0xd4);
100 dword |= 0xC331AF00; // per BKDG
101 pci_write_config32(dev, 0xd4, dword);
102 printk_debug(" F3xD4: %08x \n", dword);
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);
109 // Rev B settings - FIXME: support other revs.
111 pci_write_config32(dev, 0x84, dword);
112 printk_debug(" F3x84: %08x \n", dword);
115 pci_write_config32(dev, 0x80, dword);
116 printk_debug(" F3x80: %08x \n", dword);
122 #include "fidvid_common.c"
126 static void init_fidvid_ap(u32 bsp_apicid, u32 apicid, u32 nodeid, u32 coreid)
134 u8 nb_cof_vid_update;
140 printk_debug("FIDVID on AP: %02x\n", apicid);
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
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;
154 vid_max = (reg1fc >> 7) & 0x7F; // per node
155 fid_max = (reg1fc >> 2) & 0x1F; // per system
158 /* FIXME: support daul plane mode */
159 die("PVImode not supported\n");
160 /* fidmax = vidmax - (reg1fc >> 17) & 0x1F;
161 fidmax = fidmax + (reg1fc >> 14) & 0x03;
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
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;
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);
183 msr.lo &= ~0xFE000000; // clear nbvid
184 msr.lo |= vid_max << 25;
185 wrmsr(0xC0010064, msr);
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);
192 // Wait for P1 to set.
194 msr = rdmsr(0xC0010063);
195 } while (msr.lo != 1);
198 msr.lo = msr.lo & ~0x07;
199 wrmsr(0xC0010062, msr);
200 // Wait for P0 to set.
202 msr = rdmsr(0xC0010063);
203 } while (msr.lo != 0);
207 send = (nb_cof_vid_update << 16) | (fid_max << 8);
208 send |= (apicid << 24); // ap apicid
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);
215 static u32 calc_common_fid(u32 fid_packed, u32 fid_packed_new)
220 fidmax = (fid_packed >> 8) & 0xFF;
222 fidmax_new = (fid_packed_new >> 8) & 0xFF;
224 if(fidmax > fidmax_new) {
228 fid_packed &= 0xFF << 16;
229 fid_packed |= (fidmax << 8);
230 fid_packed |= fid_packed_new & (0xFF << 16); // set nb_cof_vid_update
239 static void init_fidvid_bsp_stage1(u32 ap_apicid, void *gp )
244 struct fidvid_st *fvp = gp;
247 print_debug_fv("Wait for AP stage 1: ap_apicid = ", ap_apicid);
251 if(lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback) != 0) continue;
252 if((readback & 0x3f) == 1) {
254 break; //target ap is in stage 1
259 print_initcpu8("fidvid_bsp_stage1: time out while reading from ap ", ap_apicid);
263 print_debug_fv("\treadback = ", readback);
265 fvp->common_fid = calc_common_fid(fvp->common_fid, readback);
267 print_debug_fv("\tcommon_fid(packed) = ", fvp->common_fid);
272 static void init_fidvid_stage2(u32 apicid, u32 nodeid)
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;
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;
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;
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);
313 #if FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
314 struct ap_apicid_st {
316 // it could use 256 bytes for 64 node quad core system
317 u8 apicid[NODE_NUMS * 4];
320 static void store_ap_apicid(unsigned ap_apicid, void *gp)
322 struct ap_apicid_st *p = gp;
324 p->apicid[p->num++] = ap_apicid;
330 static int init_fidvid_bsp(u32 bsp_apicid, u32 nodes)
332 #if FAM10_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
333 struct ap_apicid_st ap_apicidx;
342 u8 nb_cof_vid_update;
347 printk_debug("FIDVID on BSP, APIC_id: %02x\n", bsp_apicid);
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
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;
361 vid_max = (reg1fc >> 7) & 0x7F; // per node
362 fid_max = (reg1fc >> 2) & 0x1F; // per system
365 /* FIXME: support daul plane mode */
366 die("PVImode not supported\n");
367 /* fidmax = vidmax - (reg1fc >> 17) & 0x1F;
368 fidmax = fidmax + (reg1fc >> 14) & 0x03;
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
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;
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);
390 msr.lo &= ~0xFE000000; // clear nbvid
391 msr.lo |= vid_max << 25;
392 wrmsr(0xC0010064, msr);
394 // Transition to P1 and then P0 for core0.
395 msr = rdmsr(0xC0010062);
396 msr.lo = (msr.lo & ~0x07) | 1;
397 wrmsr(0xC0010062, msr);
399 // Wait for P1 to set.
401 msr = rdmsr(0xC0010063);
402 } while (msr.lo != 1);
404 msr.lo = msr.lo & ~0x07;
405 wrmsr(0xC0010062, msr);
406 // Wait for P0 to set.
408 msr = rdmsr(0xC0010063);
409 } while (msr.lo != 0);
412 fv.common_fid = (nb_cof_vid_update << 16) | (fid_max << 8) ;
413 print_debug_fv("BSP fid = ", fv.common_fid);
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
422 for_each_ap(bsp_apicid, FAM10_SET_FIDVID_CORE_RANGE, store_ap_apicid, &ap_apicidx);
424 for(i = 0; i < ap_apicidx.num; i++) {
425 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
428 for_each_ap(bsp_apicid, FAM10_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
431 print_debug_fv("common_fid = ", fv.common_fid);
433 if (fv.common_fid & ~(0xFF << 16)) { // check nb_cof_vid_update
435 // Enable the common fid and other settings.
436 enable_fid_change((fv.common_fid >> 8) & 0x1F);
438 // nbfid change need warm reset, so reset at first
442 return 0; // No FID/VID changes. Don't reset
444 static void set_p0(void)
448 // Transition P0 for calling core.
449 msr = rdmsr(0xC0010062);
450 msr.lo = (msr.lo & ~0x07);
451 wrmsr(0xC0010062, msr);
453 // Don't bother to wait around for the P state to change.