3 #define K8_SET_FIDVID_DEBUG 0
5 #define K8_SET_FIDVID_ONE_BY_ONE 1
7 #define K8_SET_FIDVID_STORE_AP_APICID_AT_FIRST 1
15 static inline void print_debug_fv(const char *str, unsigned val)
17 #if K8_SET_FIDVID_DEBUG == 1
18 #if CONFIG_USE_PRINTK_IN_CAR
19 printk_debug("%s%x\r\n", str, val);
21 print_debug(str); print_debug_hex32(val); print_debug("\r\n");
26 static inline void print_debug_fv_8(const char *str, unsigned val)
28 #if K8_SET_FIDVID_DEBUG == 1
29 #if CONFIG_USE_PRINTK_IN_CAR
30 printk_debug("%s%02x\r\n", str, val);
32 print_debug(str); print_debug_hex8(val); print_debug("\r\n");
37 static inline void print_debug_fv_64(const char *str, unsigned val, unsigned val2)
39 #if K8_SET_FIDVID_DEBUG == 1
40 #if CONFIG_USE_PRINTK_IN_CAR
41 printk_debug("%s%x%x\r\n", str, val, val2);
43 print_debug(str); print_debug_hex32(val); print_debug_hex32(val2); print_debug("\r\n");
49 static void enable_fid_change(void)
55 nodes = ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60) >> 4) & 7) + 1;
57 for (i = 0; i < nodes; i++) {
58 dword = pci_read_config32(PCI_DEV(0, 0x18+i, 3), 0xd8);
60 dword |= (2 << 28) | (0x02710);
61 pci_write_config32(PCI_DEV(0, 0x18+i, 3), 0xd8, dword);
64 pci_write_config32(PCI_DEV(0, 0x18+i, 3), 0xd4, dword);
66 /* disable the DRAM interface at first, it will be enabled
68 dword = pci_read_config32(PCI_DEV(0, 0x18+i, 2), 0x94);
70 pci_write_config32(PCI_DEV(0, 0x18+i, 2), 0x94, dword);
72 dword = 0x23070700; /* enable FID/VID change */
73 // dword = 0x00070000; /* enable FID/VID change */
74 pci_write_config32(PCI_DEV(0, 0x18+i, 3), 0x80, dword);
77 pci_write_config32(PCI_DEV(0, 0x18+i, 3), 0x84, dword);
82 #if K8_SET_FIDVID_ONE_BY_ONE == 0
83 static unsigned set_fidvid_without_init(unsigned fidvid)
89 fid = (fidvid >> 8) & 0x3f;
90 vid = (fidvid >> 16) & 0x3f;
94 msr.lo = (vid<<8) | fid;
95 wrmsr(0xc0010041, msr);
100 static unsigned set_fidvid(unsigned apicid, unsigned fidvid, int showmessage)
102 /* for (cur, new) there is one <1600MHz x8 to find out next_fid */
103 static const uint8_t next_fid_a[] = {
104 /* x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 */
105 /* x4 */ 0, 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9,
106 /* x5 */ 9, 0, 11, 11, 9, 9, 10, 11, 11, 11, 11, 11,
107 /* x6 */ 11, 11, 0, 13, 11, 11, 11, 11, 12, 13, 13, 13,
108 /* x7 */ 13, 13, 13, 0, 13, 13, 13, 13, 13, 13, 14, 15,
109 /* x8 */ 4, 9, 9, 9, 0, 9, 9, 9, 9, 9, 9, 9,
110 /* x9 */ 4, 5, 10, 10, 8, 0, 0, 0, 0, 0, 0, 0,
111 /*x10 */ 9, 5, 11, 11, 9, 0, 0, 0, 0, 0, 0, 0,
112 /*x11 */ 10, 5, 6, 12, 10, 0, 0, 0, 0, 0, 0, 0,
113 /*x12 */ 11, 11, 6, 13, 11, 0, 0, 0, 0, 0, 0, 0,
114 /*x13 */ 12, 12, 6, 7, 12, 0, 0, 0, 0, 0, 0, 0,
115 /*x14 */ 13, 13, 13, 7, 13, 0, 0, 0, 0, 0, 0, 0,
116 /*x15 */ 14, 14, 14, 7, 14, 0, 0, 0, 0, 0, 0, 0,
117 /* 0:x4, 2:x5....BASE=4, MIN=4, MAX=25, INC=2 result = (xX-BASE)*INC */
134 if (apicid != apicidx) {
135 #if CONFIG_USE_PRINTK_IN_CAR
136 printk_err("wrong apicid, we want change %x, but it is %x\r\n", apicid, apicidx);
138 print_err("wrong apicid, we want change "); print_err_hex8(apicid); print_err(" but it is "); print_err_hex8(apicidx); print_err("\r\n");
143 fid = (fidvid >> 8) & 0x3f;
144 vid = (fidvid >> 16) & 0x3f;
146 msr = rdmsr(0xc0010042);
148 vid_cur = msr.hi & 0x3f;
149 fid_cur = msr.lo & 0x3f;
151 if ((vid_cur==vid) && (fid_cur==fid))
154 vid_max = (msr.hi>>(48-32)) & 0x3f;
155 fid_max = ((msr.lo>>16) & 0x3f); /* max fid */
157 if (fid_max>=((25-4)*2)) { /* FX max fid is 5G */
158 fid_max = ((msr.lo >> 8) & 0x3f) + 5 * 2; /* max FID is min fid + 1G */
159 if (fid_max >= ((25-4) * 2)) {
160 fid_max = (10-4) * 2; /* hard set to 2G */
167 msr.lo = (vid_max << 8) | (fid_cur);
169 msr.lo |= (1<<16); /* init changes */
171 wrmsr(0xc0010041, msr);
177 for (loop=0;loop<100000;loop++){
178 msr = rdmsr(0xc0010042);
179 if (!(msr.lo & (1<<31)))
182 vid_cur = msr.hi & 0x3f;
185 while ((fid_cur != fid) && (steps-- > 0)) {
187 if ((fid_cur > (8-4)*2) && (fid> (8-4)*2)) {
189 fid_temp = fid_cur + 2;
191 fid_temp = fid_cur - 2;
194 /* there is one < 8, So we need to lookup the table to
195 * find the fid_cur */
197 temp = next_fid_a[(fid_cur/2)*12+(fid/2)];
198 if (temp <= 0) break;
199 fid_temp = (temp-4) * 2;
201 if (fid_temp > fid_max)
208 msr.lo = (vid_cur << 8) | fid_cur;
210 msr.lo |= (1 << 16); /* init changes */
212 wrmsr(0xc0010041, msr);
218 #if K8_SET_FIDVID_DEBUG == 1
220 print_debug_fv_8("\tapicid in set_fidvid = ", apicid);
221 print_debug_fv_64("ctrl msr fid, vid ", msr.hi, msr.lo);
225 for (loop = 0; loop < 100000; loop++){
226 msr = rdmsr(0xc0010042);
227 if (!(msr.lo & (1 << 31)))
230 fid_cur = msr.lo & 0x3f;
232 #if K8_SET_FIDVID_DEBUG == 1
234 print_debug_fv_64("status msr fid, vid ", msr.hi, msr.lo);
239 /* set vid to final */
241 msr.lo = (vid << 8) | (fid_cur);
243 msr.lo |= (1 << 16); // init changes
245 wrmsr(0xc0010041, msr);
250 for (loop = 0; loop < 100000; loop++){
251 msr = rdmsr(0xc0010042);
252 if (!(msr.lo & (1 << 31)))
255 vid_cur = msr.hi & 0x3f;
257 fidvid = (vid_cur << 16) | (fid_cur << 8);
261 print_err("set vid failed for apicid ="); print_err_hex8(apicidx); print_err("\r\n");
264 print_err("set fid failed for apicid ="); print_err_hex8(apicidx); print_err("\r\n");
272 static void init_fidvid_ap(unsigned bsp_apicid, unsigned apicid)
275 uint32_t readback = 0;
276 unsigned timeout = 1;
283 msr = rdmsr(0xc0010042);
284 fid_max = ((msr.lo >> 16) & 0x3f); /* max fid */
286 if (fid_max >= ((25-4) * 2)) { /* FX max fid is 5G */
287 fid_max = ((msr.lo>>8) & 0x3f) + 5*2; /* maxFID = minFID + 1G */
288 if (fid_max >= ((25-4) * 2)) {
289 fid_max = (10-4) * 2; // hard set to 2G
295 send |= ((msr.hi >> (48-32)) & 0x3f) << 16; /* max vid */
296 send |= (apicid << 24); /* ap apicid */
298 #if K8_SET_FIDVID_ONE_BY_ONE == 1
299 vid_cur = msr.hi & 0x3f;
300 fid_cur = msr.lo & 0x3f;
304 msr.lo = (vid_cur << 8) | (fid_cur);
305 wrmsr(0xc0010041, msr);
308 timeout = wait_cpu_state(bsp_apicid, 1);
310 print_initcpu8("fidvid_ap_stage1: time out while reading from BSP on ", apicid);
312 /* send signal to BSP about this AP max fid and vid */
313 /* AP at state 1 that sent our fid and vid */
314 lapic_write(LAPIC_MSG_REG, send | 1);
316 // wait_cpu_state(bsp_apicid, 2); /* don't need we can use apicid directly */
319 /* remote read BSP signal that include vid/fid that need to set */
320 if (lapic_remote_read(bsp_apicid, LAPIC_MSG_REG, &readback)!=0)
322 if (((readback >> 24) & 0xff) == apicid)
323 break; /* it is this cpu turn */
327 #if K8_SET_FIDVID_ONE_BY_ONE == 1
328 readback = set_fidvid(apicid, readback & 0xffff00, 1); // this AP
330 readback = set_fidvid_without_init(readback & 0xffff00); // this AP
332 /* send signal to BSP that this AP fid/vid is set */
333 /* allow to change state2 is together with apicid */
334 /* AP at state that We set the requested fid/vid */
335 send = (apicid<<24) | (readback & 0x00ffff00);
337 print_initcpu8("fidvid_ap_stage2: time out while reading from BSP on ", apicid);
340 lapic_write(LAPIC_MSG_REG, send | 2);
342 timeout = wait_cpu_state(bsp_apicid, 3);
344 print_initcpu8("fidvid_ap_stage3: time out while reading from BSP on ", apicid);
348 static unsigned calc_common_fidvid(unsigned fidvid, unsigned fidvidx)
350 /* FIXME: need to check the change path to verify if it is reachable
351 * when common fid is small than 1.6G */
352 if ((fidvid & 0xff00) <= (fidvidx & 0xff00)) {
361 unsigned common_fidvid;
364 static void init_fidvid_bsp_stage1(unsigned ap_apicid, void *gp )
366 unsigned readback = 0;
367 unsigned timeout = 1;
369 struct fidvid_st *fvp = gp;
372 print_debug_fv("state 1: ap_apicid=", ap_apicid);
376 if (lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback)!=0)
378 if ((readback & 0xff) == 1) {
380 break; /* target ap is in stage 1 */
384 print_initcpu8("fidvid_bsp_stage1: time out while reading from ap ", ap_apicid);
388 print_debug_fv("\treadback=", readback);
390 fvp->common_fidvid = calc_common_fidvid(fvp->common_fidvid, readback & 0xffff00);
392 print_debug_fv("\tcommon_fidvid=", fvp->common_fidvid);
395 static void init_fidvid_bsp_stage2(unsigned ap_apicid, void *gp)
397 unsigned readback = 0;
398 unsigned timeout = 1;
400 struct fidvid_st *fvp = gp;
403 print_debug_fv("state 2: ap_apicid=", ap_apicid);
405 /* all set to state2 */
406 lapic_write(LAPIC_MSG_REG, fvp->common_fidvid | (ap_apicid<<24) | 2);
410 if (lapic_remote_read(ap_apicid, LAPIC_MSG_REG, &readback)!=0)
412 if ((readback & 0xff) == 2) {
414 break; /* target ap is stage 2, it's FID has beed set */
419 print_initcpu8("fidvid_bsp_stage2: time out while reading from ap ", ap_apicid);
423 print_debug_fv("\treadback=", readback);
426 #if K8_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
427 struct ap_apicid_st {
429 unsigned apicid[16]; /* 8 way dual core need 16 */
430 /* FIXME: 32 node quad core, may need 128 */
433 static void store_ap_apicid(unsigned ap_apicid, void *gp)
435 struct ap_apicid_st *p = gp;
437 p->apicid[p->num++] = ap_apicid;
442 static void init_fidvid_bsp(unsigned bsp_apicid)
449 #if K8_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
450 struct ap_apicid_st ap_apicidx;
455 msr = rdmsr(0xc0010042);
456 fid_max = ((msr.lo >> 16) & 0x3f); /* max fid */
458 if (fid_max >= ((25-4) * 2)) { /* FX max fid is 5G */
459 fid_max = ((msr.lo >> 8) & 0x3f) + 5*2; /* maxFID = minFID + 1G */
460 if (fid_max >= ((25-4) * 2)) {
461 fid_max = (10-4) * 2; /* hard set to 2G */
465 vid_max = ((msr.hi>>(48-32)) & 0x3f); //max vid
466 fv.common_fidvid = (fid_max << 8)|(vid_max << 16);
469 /* for all APs (We know the APIC ID of all APs even the APIC ID is lifted)
470 * remote read from AP about max fid/vid */
472 /* let all ap trains to state 1 */
473 lapic_write(LAPIC_MSG_REG, (bsp_apicid << 24) | 1);
475 /* calculate the common max fid/vid that could be used for
477 #if K8_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
480 for_each_ap(bsp_apicid, K8_SET_FIDVID_CORE0_ONLY, store_ap_apicid, &ap_apicidx);
482 for (i = 0; i < ap_apicidx.num; i++) {
483 init_fidvid_bsp_stage1(ap_apicidx.apicid[i], &fv);
486 for_each_ap(bsp_apicid, K8_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage1, &fv);
492 /* Can we use max only? So we can only set fid in one around,
493 * otherwise we need to set that to max after raminit */
494 /* set fid vid to DQS training required */
495 fid = (fv.common_fidvid >> 8) & 0x3f;
496 vid = (fv.common_fidvid >> 16) & 0x3f;
498 if (fid > (10-4) * 2) {
499 fid = (10-4) * 2; // x10
503 vid += 4; /* unit is 12.5mV */
505 vid += 2; /* unit is 25mV */
508 fv.common_fidvid = (fid<<8) | (vid<<16);
510 print_debug_fv("common_fidvid=", fv.common_fidvid);
514 #if K8_SET_FIDVID_ONE_BY_ONE == 1
515 /* set BSP fid and vid */
516 print_debug_fv("bsp apicid=", bsp_apicid);
517 fv.common_fidvid = set_fidvid(bsp_apicid, fv.common_fidvid, 1);
518 print_debug_fv("common_fidvid=", fv.common_fidvid);
521 /* For all APs ( We know the APIC ID of all AP even the APIC ID is lifted)
522 * send signal to the AP it could change it's fid/vid */
523 /* remote read singnal from AP that AP is done */
525 fv.common_fidvid &= 0xffff00;
527 /* set state 2 allow is in init_fidvid_bsp_stage2 */
528 #if K8_SET_FIDVID_STORE_AP_APICID_AT_FIRST == 1
529 for (i = 0; i < ap_apicidx.num; i++) {
530 init_fidvid_bsp_stage2(ap_apicidx.apicid[i], &fv);
533 for_each_ap(bsp_apicid, K8_SET_FIDVID_CORE0_ONLY, init_fidvid_bsp_stage2, &fv);
536 #if K8_SET_FIDVID_ONE_BY_ONE == 0
537 /* set BSP fid and vid */
538 print_debug_fv("bsp apicid=", bsp_apicid);
539 fv.common_fidvid = set_fidvid(bsp_apicid, fv.common_fidvid, 1);
540 print_debug_fv("common_fidvid=", fv.common_fidvid);
544 /* clear the state */
545 lapic_write(LAPIC_MSG_REG, fv.common_fidvid | (bsp_apicid << 24) | 3);
547 /* here wait a while, so last ap could read pack, and stop it, don't
548 * call init_timer too early or just don't use init_timer */