1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * (c) 2003-2012 Advanced Micro Devices, Inc. 4 * 5 * Maintainer: 6 * Andreas Herrmann <herrmann.der.user@googlemail.com> 7 * 8 * Based on the powernow-k7.c module written by Dave Jones. 9 * (C) 2003 Dave Jones on behalf of SuSE Labs 10 * (C) 2004 Dominik Brodowski <linux@brodo.de> 11 * (C) 2004 Pavel Machek <pavel@ucw.cz> 12 * Based upon datasheets & sample CPUs kindly provided by AMD. 13 * 14 * Valuable input gratefully received from Dave Jones, Pavel Machek, 15 * Dominik Brodowski, Jacob Shin, and others. 16 * Originally developed by Paul Devriendt. 17 * 18 * Processor information obtained from Chapter 9 (Power and Thermal 19 * Management) of the "BIOS and Kernel Developer's Guide (BKDG) for 20 * the AMD Athlon 64 and AMD Opteron Processors" and section "2.x 21 * Power Management" in BKDGs for newer AMD CPU families. 22 * 23 * Tables for specific CPUs can be inferred from AMD's processor 24 * power and thermal data sheets, (e.g. 30417.pdf, 30430.pdf, 43375.pdf) 25 */ 26 27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 28 29 #include <linux/kernel.h> 30 #include <linux/smp.h> 31 #include <linux/module.h> 32 #include <linux/init.h> 33 #include <linux/cpufreq.h> 34 #include <linux/slab.h> 35 #include <linux/string.h> 36 #include <linux/cpumask.h> 37 #include <linux/io.h> 38 #include <linux/delay.h> 39 40 #include <asm/msr.h> 41 #include <asm/cpu_device_id.h> 42 #include <asm/cpuid/api.h> 43 44 #include <linux/acpi.h> 45 #include <linux/mutex.h> 46 #include <acpi/processor.h> 47 48 #define VERSION "version 2.20.00" 49 #include "powernow-k8.h" 50 51 /* serialize freq changes */ 52 static DEFINE_MUTEX(fidvid_mutex); 53 54 static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data); 55 56 static struct cpufreq_driver cpufreq_amd64_driver; 57 58 /* Return a frequency in MHz, given an input fid */ 59 static u32 find_freq_from_fid(u32 fid) 60 { 61 return 800 + (fid * 100); 62 } 63 64 /* Return a frequency in KHz, given an input fid */ 65 static u32 find_khz_freq_from_fid(u32 fid) 66 { 67 return 1000 * find_freq_from_fid(fid); 68 } 69 70 /* Return the vco fid for an input fid 71 * 72 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids 73 * only from corresponding high fids. This returns "high" fid corresponding to 74 * "low" one. 75 */ 76 static u32 convert_fid_to_vco_fid(u32 fid) 77 { 78 if (fid < HI_FID_TABLE_BOTTOM) 79 return 8 + (2 * fid); 80 else 81 return fid; 82 } 83 84 /* 85 * Return 1 if the pending bit is set. Unless we just instructed the processor 86 * to transition to a new state, seeing this bit set is really bad news. 87 */ 88 static int pending_bit_stuck(void) 89 { 90 u32 lo, hi __always_unused; 91 92 rdmsr(MSR_FIDVID_STATUS, lo, hi); 93 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0; 94 } 95 96 /* 97 * Update the global current fid / vid values from the status msr. 98 * Returns 1 on error. 99 */ 100 static int query_current_values_with_pending_wait(struct powernow_k8_data *data) 101 { 102 u32 lo, hi; 103 u32 i = 0; 104 105 do { 106 if (i++ > 10000) { 107 pr_debug("detected change pending stuck\n"); 108 return 1; 109 } 110 rdmsr(MSR_FIDVID_STATUS, lo, hi); 111 } while (lo & MSR_S_LO_CHANGE_PENDING); 112 113 data->currvid = hi & MSR_S_HI_CURRENT_VID; 114 data->currfid = lo & MSR_S_LO_CURRENT_FID; 115 116 return 0; 117 } 118 119 /* the isochronous relief time */ 120 static void count_off_irt(struct powernow_k8_data *data) 121 { 122 udelay((1 << data->irt) * 10); 123 } 124 125 /* the voltage stabilization time */ 126 static void count_off_vst(struct powernow_k8_data *data) 127 { 128 udelay(data->vstable * VST_UNITS_20US); 129 } 130 131 /* need to init the control msr to a safe value (for each cpu) */ 132 static void fidvid_msr_init(void) 133 { 134 u32 lo, hi; 135 u8 fid, vid; 136 137 rdmsr(MSR_FIDVID_STATUS, lo, hi); 138 vid = hi & MSR_S_HI_CURRENT_VID; 139 fid = lo & MSR_S_LO_CURRENT_FID; 140 lo = fid | (vid << MSR_C_LO_VID_SHIFT); 141 hi = MSR_C_HI_STP_GNT_BENIGN; 142 pr_debug("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi); 143 wrmsr(MSR_FIDVID_CTL, lo, hi); 144 } 145 146 /* write the new fid value along with the other control fields to the msr */ 147 static int write_new_fid(struct powernow_k8_data *data, u32 fid) 148 { 149 u32 lo; 150 u32 savevid = data->currvid; 151 u32 i = 0; 152 153 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) { 154 pr_err("internal error - overflow on fid write\n"); 155 return 1; 156 } 157 158 lo = fid; 159 lo |= (data->currvid << MSR_C_LO_VID_SHIFT); 160 lo |= MSR_C_LO_INIT_FID_VID; 161 162 pr_debug("writing fid 0x%x, lo 0x%x, hi 0x%x\n", 163 fid, lo, data->plllock * PLL_LOCK_CONVERSION); 164 165 do { 166 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION); 167 if (i++ > 100) { 168 pr_err("Hardware error - pending bit very stuck - no further pstate changes possible\n"); 169 return 1; 170 } 171 } while (query_current_values_with_pending_wait(data)); 172 173 count_off_irt(data); 174 175 if (savevid != data->currvid) { 176 pr_err("vid change on fid trans, old 0x%x, new 0x%x\n", 177 savevid, data->currvid); 178 return 1; 179 } 180 181 if (fid != data->currfid) { 182 pr_err("fid trans failed, fid 0x%x, curr 0x%x\n", fid, 183 data->currfid); 184 return 1; 185 } 186 187 return 0; 188 } 189 190 /* Write a new vid to the hardware */ 191 static int write_new_vid(struct powernow_k8_data *data, u32 vid) 192 { 193 u32 lo; 194 u32 savefid = data->currfid; 195 int i = 0; 196 197 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) { 198 pr_err("internal error - overflow on vid write\n"); 199 return 1; 200 } 201 202 lo = data->currfid; 203 lo |= (vid << MSR_C_LO_VID_SHIFT); 204 lo |= MSR_C_LO_INIT_FID_VID; 205 206 pr_debug("writing vid 0x%x, lo 0x%x, hi 0x%x\n", 207 vid, lo, STOP_GRANT_5NS); 208 209 do { 210 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS); 211 if (i++ > 100) { 212 pr_err("internal error - pending bit very stuck - no further pstate changes possible\n"); 213 return 1; 214 } 215 } while (query_current_values_with_pending_wait(data)); 216 217 if (savefid != data->currfid) { 218 pr_err("fid changed on vid trans, old 0x%x new 0x%x\n", 219 savefid, data->currfid); 220 return 1; 221 } 222 223 if (vid != data->currvid) { 224 pr_err("vid trans failed, vid 0x%x, curr 0x%x\n", 225 vid, data->currvid); 226 return 1; 227 } 228 229 return 0; 230 } 231 232 /* 233 * Reduce the vid by the max of step or reqvid. 234 * Decreasing vid codes represent increasing voltages: 235 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off. 236 */ 237 static int decrease_vid_code_by_step(struct powernow_k8_data *data, 238 u32 reqvid, u32 step) 239 { 240 if ((data->currvid - reqvid) > step) 241 reqvid = data->currvid - step; 242 243 if (write_new_vid(data, reqvid)) 244 return 1; 245 246 count_off_vst(data); 247 248 return 0; 249 } 250 251 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */ 252 static int transition_fid_vid(struct powernow_k8_data *data, 253 u32 reqfid, u32 reqvid) 254 { 255 if (core_voltage_pre_transition(data, reqvid, reqfid)) 256 return 1; 257 258 if (core_frequency_transition(data, reqfid)) 259 return 1; 260 261 if (core_voltage_post_transition(data, reqvid)) 262 return 1; 263 264 if (query_current_values_with_pending_wait(data)) 265 return 1; 266 267 if ((reqfid != data->currfid) || (reqvid != data->currvid)) { 268 pr_err("failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n", 269 smp_processor_id(), 270 reqfid, reqvid, data->currfid, data->currvid); 271 return 1; 272 } 273 274 pr_debug("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n", 275 smp_processor_id(), data->currfid, data->currvid); 276 277 return 0; 278 } 279 280 /* Phase 1 - core voltage transition ... setup voltage */ 281 static int core_voltage_pre_transition(struct powernow_k8_data *data, 282 u32 reqvid, u32 reqfid) 283 { 284 u32 rvosteps = data->rvo; 285 u32 savefid = data->currfid; 286 u32 maxvid, lo __always_unused, rvomult = 1; 287 288 pr_debug("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n", 289 smp_processor_id(), 290 data->currfid, data->currvid, reqvid, data->rvo); 291 292 if ((savefid < LO_FID_TABLE_TOP) && (reqfid < LO_FID_TABLE_TOP)) 293 rvomult = 2; 294 rvosteps *= rvomult; 295 rdmsr(MSR_FIDVID_STATUS, lo, maxvid); 296 maxvid = 0x1f & (maxvid >> 16); 297 pr_debug("ph1 maxvid=0x%x\n", maxvid); 298 if (reqvid < maxvid) /* lower numbers are higher voltages */ 299 reqvid = maxvid; 300 301 while (data->currvid > reqvid) { 302 pr_debug("ph1: curr 0x%x, req vid 0x%x\n", 303 data->currvid, reqvid); 304 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs)) 305 return 1; 306 } 307 308 while ((rvosteps > 0) && 309 ((rvomult * data->rvo + data->currvid) > reqvid)) { 310 if (data->currvid == maxvid) { 311 rvosteps = 0; 312 } else { 313 pr_debug("ph1: changing vid for rvo, req 0x%x\n", 314 data->currvid - 1); 315 if (decrease_vid_code_by_step(data, data->currvid-1, 1)) 316 return 1; 317 rvosteps--; 318 } 319 } 320 321 if (query_current_values_with_pending_wait(data)) 322 return 1; 323 324 if (savefid != data->currfid) { 325 pr_err("ph1 err, currfid changed 0x%x\n", data->currfid); 326 return 1; 327 } 328 329 pr_debug("ph1 complete, currfid 0x%x, currvid 0x%x\n", 330 data->currfid, data->currvid); 331 332 return 0; 333 } 334 335 /* Phase 2 - core frequency transition */ 336 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid) 337 { 338 u32 vcoreqfid, vcocurrfid, vcofiddiff; 339 u32 fid_interval, savevid = data->currvid; 340 341 if (data->currfid == reqfid) { 342 pr_err("ph2 null fid transition 0x%x\n", data->currfid); 343 return 0; 344 } 345 346 pr_debug("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n", 347 smp_processor_id(), 348 data->currfid, data->currvid, reqfid); 349 350 vcoreqfid = convert_fid_to_vco_fid(reqfid); 351 vcocurrfid = convert_fid_to_vco_fid(data->currfid); 352 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid 353 : vcoreqfid - vcocurrfid; 354 355 if ((reqfid <= LO_FID_TABLE_TOP) && (data->currfid <= LO_FID_TABLE_TOP)) 356 vcofiddiff = 0; 357 358 while (vcofiddiff > 2) { 359 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2); 360 361 if (reqfid > data->currfid) { 362 if (data->currfid > LO_FID_TABLE_TOP) { 363 if (write_new_fid(data, 364 data->currfid + fid_interval)) 365 return 1; 366 } else { 367 if (write_new_fid 368 (data, 369 2 + convert_fid_to_vco_fid(data->currfid))) 370 return 1; 371 } 372 } else { 373 if (write_new_fid(data, data->currfid - fid_interval)) 374 return 1; 375 } 376 377 vcocurrfid = convert_fid_to_vco_fid(data->currfid); 378 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid 379 : vcoreqfid - vcocurrfid; 380 } 381 382 if (write_new_fid(data, reqfid)) 383 return 1; 384 385 if (query_current_values_with_pending_wait(data)) 386 return 1; 387 388 if (data->currfid != reqfid) { 389 pr_err("ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n", 390 data->currfid, reqfid); 391 return 1; 392 } 393 394 if (savevid != data->currvid) { 395 pr_err("ph2: vid changed, save 0x%x, curr 0x%x\n", 396 savevid, data->currvid); 397 return 1; 398 } 399 400 pr_debug("ph2 complete, currfid 0x%x, currvid 0x%x\n", 401 data->currfid, data->currvid); 402 403 return 0; 404 } 405 406 /* Phase 3 - core voltage transition flow ... jump to the final vid. */ 407 static int core_voltage_post_transition(struct powernow_k8_data *data, 408 u32 reqvid) 409 { 410 u32 savefid = data->currfid; 411 u32 savereqvid = reqvid; 412 413 pr_debug("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n", 414 smp_processor_id(), 415 data->currfid, data->currvid); 416 417 if (reqvid != data->currvid) { 418 if (write_new_vid(data, reqvid)) 419 return 1; 420 421 if (savefid != data->currfid) { 422 pr_err("ph3: bad fid change, save 0x%x, curr 0x%x\n", 423 savefid, data->currfid); 424 return 1; 425 } 426 427 if (data->currvid != reqvid) { 428 pr_err("ph3: failed vid transition\n, req 0x%x, curr 0x%x", 429 reqvid, data->currvid); 430 return 1; 431 } 432 } 433 434 if (query_current_values_with_pending_wait(data)) 435 return 1; 436 437 if (savereqvid != data->currvid) { 438 pr_debug("ph3 failed, currvid 0x%x\n", data->currvid); 439 return 1; 440 } 441 442 if (savefid != data->currfid) { 443 pr_debug("ph3 failed, currfid changed 0x%x\n", 444 data->currfid); 445 return 1; 446 } 447 448 pr_debug("ph3 complete, currfid 0x%x, currvid 0x%x\n", 449 data->currfid, data->currvid); 450 451 return 0; 452 } 453 454 static const struct x86_cpu_id powernow_k8_ids[] = { 455 /* IO based frequency switching */ 456 X86_MATCH_VENDOR_FAM(AMD, 0xf, NULL), 457 {} 458 }; 459 MODULE_DEVICE_TABLE(x86cpu, powernow_k8_ids); 460 461 static void check_supported_cpu(void *_rc) 462 { 463 u32 eax, ebx, ecx, edx; 464 int *rc = _rc; 465 466 *rc = -ENODEV; 467 468 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE); 469 470 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) { 471 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) || 472 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) { 473 pr_info("Processor cpuid %x not supported\n", eax); 474 return; 475 } 476 477 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES); 478 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) { 479 pr_info("No frequency change capabilities detected\n"); 480 return; 481 } 482 483 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx); 484 if ((edx & P_STATE_TRANSITION_CAPABLE) 485 != P_STATE_TRANSITION_CAPABLE) { 486 pr_info_once("Power state transitions not supported\n"); 487 return; 488 } 489 *rc = 0; 490 } 491 } 492 493 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, 494 u8 maxvid) 495 { 496 unsigned int j; 497 u8 lastfid = 0xff; 498 499 for (j = 0; j < data->numps; j++) { 500 if (pst[j].vid > LEAST_VID) { 501 pr_err(FW_BUG "vid %d invalid : 0x%x\n", j, 502 pst[j].vid); 503 return -EINVAL; 504 } 505 if (pst[j].vid < data->rvo) { 506 /* vid + rvo >= 0 */ 507 pr_err(FW_BUG "0 vid exceeded with pstate %d\n", j); 508 return -ENODEV; 509 } 510 if (pst[j].vid < maxvid + data->rvo) { 511 /* vid + rvo >= maxvid */ 512 pr_err(FW_BUG "maxvid exceeded with pstate %d\n", j); 513 return -ENODEV; 514 } 515 if (pst[j].fid > MAX_FID) { 516 pr_err(FW_BUG "maxfid exceeded with pstate %d\n", j); 517 return -ENODEV; 518 } 519 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) { 520 /* Only first fid is allowed to be in "low" range */ 521 pr_err(FW_BUG "two low fids - %d : 0x%x\n", j, 522 pst[j].fid); 523 return -EINVAL; 524 } 525 if (pst[j].fid < lastfid) 526 lastfid = pst[j].fid; 527 } 528 if (lastfid & 1) { 529 pr_err(FW_BUG "lastfid invalid\n"); 530 return -EINVAL; 531 } 532 if (lastfid > LO_FID_TABLE_TOP) 533 pr_info(FW_BUG "first fid not from lo freq table\n"); 534 535 return 0; 536 } 537 538 static void invalidate_entry(struct cpufreq_frequency_table *powernow_table, 539 unsigned int entry) 540 { 541 powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID; 542 } 543 544 static void print_basics(struct powernow_k8_data *data) 545 { 546 int j; 547 for (j = 0; j < data->numps; j++) { 548 if (data->powernow_table[j].frequency != 549 CPUFREQ_ENTRY_INVALID) { 550 pr_info("fid 0x%x (%d MHz), vid 0x%x\n", 551 data->powernow_table[j].driver_data & 0xff, 552 data->powernow_table[j].frequency/1000, 553 data->powernow_table[j].driver_data >> 8); 554 } 555 } 556 if (data->batps) 557 pr_info("Only %d pstates on battery\n", data->batps); 558 } 559 560 static int fill_powernow_table(struct powernow_k8_data *data, 561 struct pst_s *pst, u8 maxvid) 562 { 563 struct cpufreq_frequency_table *powernow_table; 564 unsigned int j; 565 566 if (data->batps) { 567 /* use ACPI support to get full speed on mains power */ 568 pr_warn("Only %d pstates usable (use ACPI driver for full range\n", 569 data->batps); 570 data->numps = data->batps; 571 } 572 573 for (j = 1; j < data->numps; j++) { 574 if (pst[j-1].fid >= pst[j].fid) { 575 pr_err("PST out of sequence\n"); 576 return -EINVAL; 577 } 578 } 579 580 if (data->numps < 2) { 581 pr_err("no p states to transition\n"); 582 return -ENODEV; 583 } 584 585 if (check_pst_table(data, pst, maxvid)) 586 return -EINVAL; 587 588 powernow_table = kzalloc((sizeof(*powernow_table) 589 * (data->numps + 1)), GFP_KERNEL); 590 if (!powernow_table) 591 return -ENOMEM; 592 593 for (j = 0; j < data->numps; j++) { 594 int freq; 595 powernow_table[j].driver_data = pst[j].fid; /* lower 8 bits */ 596 powernow_table[j].driver_data |= (pst[j].vid << 8); /* upper 8 bits */ 597 freq = find_khz_freq_from_fid(pst[j].fid); 598 powernow_table[j].frequency = freq; 599 } 600 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END; 601 powernow_table[data->numps].driver_data = 0; 602 603 if (query_current_values_with_pending_wait(data)) { 604 kfree(powernow_table); 605 return -EIO; 606 } 607 608 pr_debug("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid); 609 data->powernow_table = powernow_table; 610 if (cpumask_first(topology_core_cpumask(data->cpu)) == data->cpu) 611 print_basics(data); 612 613 for (j = 0; j < data->numps; j++) 614 if ((pst[j].fid == data->currfid) && 615 (pst[j].vid == data->currvid)) 616 return 0; 617 618 pr_debug("currfid/vid do not match PST, ignoring\n"); 619 return 0; 620 } 621 622 /* Find and validate the PSB/PST table in BIOS. */ 623 static int find_psb_table(struct powernow_k8_data *data) 624 { 625 struct psb_s *psb; 626 unsigned int i; 627 u32 mvs; 628 u8 maxvid; 629 u32 cpst = 0; 630 u32 thiscpuid; 631 632 for (i = 0xc0000; i < 0xffff0; i += 0x10) { 633 /* Scan BIOS looking for the signature. */ 634 /* It can not be at ffff0 - it is too big. */ 635 636 psb = phys_to_virt(i); 637 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0) 638 continue; 639 640 pr_debug("found PSB header at 0x%p\n", psb); 641 642 pr_debug("table vers: 0x%x\n", psb->tableversion); 643 if (psb->tableversion != PSB_VERSION_1_4) { 644 pr_err(FW_BUG "PSB table is not v1.4\n"); 645 return -ENODEV; 646 } 647 648 pr_debug("flags: 0x%x\n", psb->flags1); 649 if (psb->flags1) { 650 pr_err(FW_BUG "unknown flags\n"); 651 return -ENODEV; 652 } 653 654 data->vstable = psb->vstable; 655 pr_debug("voltage stabilization time: %d(*20us)\n", 656 data->vstable); 657 658 pr_debug("flags2: 0x%x\n", psb->flags2); 659 data->rvo = psb->flags2 & 3; 660 data->irt = ((psb->flags2) >> 2) & 3; 661 mvs = ((psb->flags2) >> 4) & 3; 662 data->vidmvs = 1 << mvs; 663 data->batps = ((psb->flags2) >> 6) & 3; 664 665 pr_debug("ramp voltage offset: %d\n", data->rvo); 666 pr_debug("isochronous relief time: %d\n", data->irt); 667 pr_debug("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs); 668 669 pr_debug("numpst: 0x%x\n", psb->num_tables); 670 cpst = psb->num_tables; 671 if ((psb->cpuid == 0x00000fc0) || 672 (psb->cpuid == 0x00000fe0)) { 673 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE); 674 if ((thiscpuid == 0x00000fc0) || 675 (thiscpuid == 0x00000fe0)) 676 cpst = 1; 677 } 678 if (cpst != 1) { 679 pr_err(FW_BUG "numpst must be 1\n"); 680 return -ENODEV; 681 } 682 683 data->plllock = psb->plllocktime; 684 pr_debug("plllocktime: 0x%x (units 1us)\n", psb->plllocktime); 685 pr_debug("maxfid: 0x%x\n", psb->maxfid); 686 pr_debug("maxvid: 0x%x\n", psb->maxvid); 687 maxvid = psb->maxvid; 688 689 data->numps = psb->numps; 690 pr_debug("numpstates: 0x%x\n", data->numps); 691 return fill_powernow_table(data, 692 (struct pst_s *)(psb+1), maxvid); 693 } 694 /* 695 * If you see this message, complain to BIOS manufacturer. If 696 * he tells you "we do not support Linux" or some similar 697 * nonsense, remember that Windows 2000 uses the same legacy 698 * mechanism that the old Linux PSB driver uses. Tell them it 699 * is broken with Windows 2000. 700 * 701 * The reference to the AMD documentation is chapter 9 in the 702 * BIOS and Kernel Developer's Guide, which is available on 703 * www.amd.com 704 */ 705 pr_err(FW_BUG "No PSB or ACPI _PSS objects\n"); 706 pr_err("Make sure that your BIOS is up to date and Cool'N'Quiet support is enabled in BIOS setup\n"); 707 return -ENODEV; 708 } 709 710 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, 711 unsigned int index) 712 { 713 u64 control; 714 715 if (!data->acpi_data.state_count) 716 return; 717 718 control = data->acpi_data.states[index].control; 719 data->irt = (control >> IRT_SHIFT) & IRT_MASK; 720 data->rvo = (control >> RVO_SHIFT) & RVO_MASK; 721 data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK; 722 data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK; 723 data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK); 724 data->vstable = (control >> VST_SHIFT) & VST_MASK; 725 } 726 727 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) 728 { 729 struct cpufreq_frequency_table *powernow_table; 730 int ret_val = -ENODEV; 731 u64 control, status; 732 733 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) { 734 pr_debug("register performance failed: bad ACPI data\n"); 735 return -EIO; 736 } 737 738 /* verify the data contained in the ACPI structures */ 739 if (data->acpi_data.state_count <= 1) { 740 pr_debug("No ACPI P-States\n"); 741 goto err_out; 742 } 743 744 control = data->acpi_data.control_register.space_id; 745 status = data->acpi_data.status_register.space_id; 746 747 if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) || 748 (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) { 749 pr_debug("Invalid control/status registers (%llx - %llx)\n", 750 control, status); 751 goto err_out; 752 } 753 754 /* fill in data->powernow_table */ 755 powernow_table = kzalloc((sizeof(*powernow_table) 756 * (data->acpi_data.state_count + 1)), GFP_KERNEL); 757 if (!powernow_table) 758 goto err_out; 759 760 /* fill in data */ 761 data->numps = data->acpi_data.state_count; 762 powernow_k8_acpi_pst_values(data, 0); 763 764 ret_val = fill_powernow_table_fidvid(data, powernow_table); 765 if (ret_val) 766 goto err_out_mem; 767 768 powernow_table[data->acpi_data.state_count].frequency = 769 CPUFREQ_TABLE_END; 770 data->powernow_table = powernow_table; 771 772 if (cpumask_first(topology_core_cpumask(data->cpu)) == data->cpu) 773 print_basics(data); 774 775 /* notify BIOS that we exist */ 776 acpi_processor_notify_smm(THIS_MODULE); 777 778 if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) { 779 pr_err("unable to alloc powernow_k8_data cpumask\n"); 780 ret_val = -ENOMEM; 781 goto err_out_mem; 782 } 783 784 return 0; 785 786 err_out_mem: 787 kfree(powernow_table); 788 789 err_out: 790 acpi_processor_unregister_performance(data->cpu); 791 792 /* data->acpi_data.state_count informs us at ->exit() 793 * whether ACPI was used */ 794 data->acpi_data.state_count = 0; 795 796 return ret_val; 797 } 798 799 static int fill_powernow_table_fidvid(struct powernow_k8_data *data, 800 struct cpufreq_frequency_table *powernow_table) 801 { 802 int i; 803 804 for (i = 0; i < data->acpi_data.state_count; i++) { 805 u32 fid; 806 u32 vid; 807 u32 freq, index; 808 u64 status, control; 809 810 if (data->exttype) { 811 status = data->acpi_data.states[i].status; 812 fid = status & EXT_FID_MASK; 813 vid = (status >> VID_SHIFT) & EXT_VID_MASK; 814 } else { 815 control = data->acpi_data.states[i].control; 816 fid = control & FID_MASK; 817 vid = (control >> VID_SHIFT) & VID_MASK; 818 } 819 820 pr_debug(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid); 821 822 index = fid | (vid<<8); 823 powernow_table[i].driver_data = index; 824 825 freq = find_khz_freq_from_fid(fid); 826 powernow_table[i].frequency = freq; 827 828 /* verify frequency is OK */ 829 if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) { 830 pr_debug("invalid freq %u kHz, ignoring\n", freq); 831 invalidate_entry(powernow_table, i); 832 continue; 833 } 834 835 /* verify voltage is OK - 836 * BIOSs are using "off" to indicate invalid */ 837 if (vid == VID_OFF) { 838 pr_debug("invalid vid %u, ignoring\n", vid); 839 invalidate_entry(powernow_table, i); 840 continue; 841 } 842 843 if (freq != (data->acpi_data.states[i].core_frequency * 1000)) { 844 pr_info("invalid freq entries %u kHz vs. %u kHz\n", 845 freq, (unsigned int) 846 (data->acpi_data.states[i].core_frequency 847 * 1000)); 848 invalidate_entry(powernow_table, i); 849 continue; 850 } 851 } 852 return 0; 853 } 854 855 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) 856 { 857 if (data->acpi_data.state_count) 858 acpi_processor_unregister_performance(data->cpu); 859 free_cpumask_var(data->acpi_data.shared_cpu_map); 860 } 861 862 static int get_transition_latency(struct powernow_k8_data *data) 863 { 864 int max_latency = 0; 865 int i; 866 for (i = 0; i < data->acpi_data.state_count; i++) { 867 int cur_latency = data->acpi_data.states[i].transition_latency 868 + data->acpi_data.states[i].bus_master_latency; 869 if (cur_latency > max_latency) 870 max_latency = cur_latency; 871 } 872 if (max_latency == 0) { 873 pr_err(FW_WARN "Invalid zero transition latency\n"); 874 max_latency = 1; 875 } 876 /* value in usecs, needs to be in nanoseconds */ 877 return 1000 * max_latency; 878 } 879 880 /* Take a frequency, and issue the fid/vid transition command */ 881 static int transition_frequency_fidvid(struct powernow_k8_data *data, 882 unsigned int index, 883 struct cpufreq_policy *policy) 884 { 885 u32 fid = 0; 886 u32 vid = 0; 887 int res; 888 struct cpufreq_freqs freqs; 889 890 pr_debug("cpu %d transition to index %u\n", smp_processor_id(), index); 891 892 /* fid/vid correctness check for k8 */ 893 /* fid are the lower 8 bits of the index we stored into 894 * the cpufreq frequency table in find_psb_table, vid 895 * are the upper 8 bits. 896 */ 897 fid = data->powernow_table[index].driver_data & 0xFF; 898 vid = (data->powernow_table[index].driver_data & 0xFF00) >> 8; 899 900 pr_debug("table matched fid 0x%x, giving vid 0x%x\n", fid, vid); 901 902 if (query_current_values_with_pending_wait(data)) 903 return 1; 904 905 if ((data->currvid == vid) && (data->currfid == fid)) { 906 pr_debug("target matches current values (fid 0x%x, vid 0x%x)\n", 907 fid, vid); 908 return 0; 909 } 910 911 pr_debug("cpu %d, changing to fid 0x%x, vid 0x%x\n", 912 smp_processor_id(), fid, vid); 913 freqs.old = find_khz_freq_from_fid(data->currfid); 914 freqs.new = find_khz_freq_from_fid(fid); 915 916 cpufreq_freq_transition_begin(policy, &freqs); 917 res = transition_fid_vid(data, fid, vid); 918 cpufreq_freq_transition_end(policy, &freqs, res); 919 920 return res; 921 } 922 923 struct powernowk8_target_arg { 924 struct cpufreq_policy *pol; 925 unsigned newstate; 926 }; 927 928 static long powernowk8_target_fn(void *arg) 929 { 930 struct powernowk8_target_arg *pta = arg; 931 struct cpufreq_policy *pol = pta->pol; 932 unsigned newstate = pta->newstate; 933 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu); 934 u32 checkfid; 935 u32 checkvid; 936 int ret; 937 938 if (!data) 939 return -EINVAL; 940 941 checkfid = data->currfid; 942 checkvid = data->currvid; 943 944 if (pending_bit_stuck()) { 945 pr_err("failing targ, change pending bit set\n"); 946 return -EIO; 947 } 948 949 pr_debug("targ: cpu %d, %d kHz, min %d, max %d\n", 950 pol->cpu, data->powernow_table[newstate].frequency, pol->min, 951 pol->max); 952 953 if (query_current_values_with_pending_wait(data)) 954 return -EIO; 955 956 pr_debug("targ: curr fid 0x%x, vid 0x%x\n", 957 data->currfid, data->currvid); 958 959 if ((checkvid != data->currvid) || 960 (checkfid != data->currfid)) { 961 pr_info("error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n", 962 checkfid, data->currfid, 963 checkvid, data->currvid); 964 } 965 966 mutex_lock(&fidvid_mutex); 967 968 powernow_k8_acpi_pst_values(data, newstate); 969 970 ret = transition_frequency_fidvid(data, newstate, pol); 971 972 if (ret) { 973 pr_err("transition frequency failed\n"); 974 mutex_unlock(&fidvid_mutex); 975 return 1; 976 } 977 mutex_unlock(&fidvid_mutex); 978 979 pol->cur = find_khz_freq_from_fid(data->currfid); 980 981 return 0; 982 } 983 984 /* Driver entry point to switch to the target frequency */ 985 static int powernowk8_target(struct cpufreq_policy *pol, unsigned index) 986 { 987 struct powernowk8_target_arg pta = { .pol = pol, .newstate = index }; 988 989 return work_on_cpu(pol->cpu, powernowk8_target_fn, &pta); 990 } 991 992 struct init_on_cpu { 993 struct powernow_k8_data *data; 994 int rc; 995 }; 996 997 static void powernowk8_cpu_init_on_cpu(void *_init_on_cpu) 998 { 999 struct init_on_cpu *init_on_cpu = _init_on_cpu; 1000 1001 if (pending_bit_stuck()) { 1002 pr_err("failing init, change pending bit set\n"); 1003 init_on_cpu->rc = -ENODEV; 1004 return; 1005 } 1006 1007 if (query_current_values_with_pending_wait(init_on_cpu->data)) { 1008 init_on_cpu->rc = -ENODEV; 1009 return; 1010 } 1011 1012 fidvid_msr_init(); 1013 1014 init_on_cpu->rc = 0; 1015 } 1016 1017 #define MISSING_PSS_MSG \ 1018 FW_BUG "No compatible ACPI _PSS objects found.\n" \ 1019 FW_BUG "First, make sure Cool'N'Quiet is enabled in the BIOS.\n" \ 1020 FW_BUG "If that doesn't help, try upgrading your BIOS.\n" 1021 1022 /* per CPU init entry point to the driver */ 1023 static int powernowk8_cpu_init(struct cpufreq_policy *pol) 1024 { 1025 struct powernow_k8_data *data; 1026 struct init_on_cpu init_on_cpu; 1027 int rc, cpu; 1028 1029 smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1); 1030 if (rc) 1031 return -ENODEV; 1032 1033 data = kzalloc_obj(*data); 1034 if (!data) 1035 return -ENOMEM; 1036 1037 data->cpu = pol->cpu; 1038 1039 if (powernow_k8_cpu_init_acpi(data)) { 1040 /* 1041 * Use the PSB BIOS structure. This is only available on 1042 * an UP version, and is deprecated by AMD. 1043 */ 1044 if (num_online_cpus() != 1) { 1045 pr_err_once(MISSING_PSS_MSG); 1046 goto err_out; 1047 } 1048 if (pol->cpu != 0) { 1049 pr_err(FW_BUG "No ACPI _PSS objects for CPU other than CPU0. Complain to your BIOS vendor.\n"); 1050 goto err_out; 1051 } 1052 rc = find_psb_table(data); 1053 if (rc) 1054 goto err_out; 1055 1056 /* Take a crude guess here. 1057 * That guess was in microseconds, so multiply with 1000 */ 1058 pol->cpuinfo.transition_latency = ( 1059 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) + 1060 ((1 << data->irt) * 30)) * 1000; 1061 } else /* ACPI _PSS objects available */ 1062 pol->cpuinfo.transition_latency = get_transition_latency(data); 1063 1064 /* only run on specific CPU from here on */ 1065 init_on_cpu.data = data; 1066 smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu, 1067 &init_on_cpu, 1); 1068 rc = init_on_cpu.rc; 1069 if (rc != 0) 1070 goto err_out_exit_acpi; 1071 1072 cpumask_copy(pol->cpus, topology_core_cpumask(pol->cpu)); 1073 data->available_cores = pol->cpus; 1074 pol->freq_table = data->powernow_table; 1075 1076 pr_debug("cpu_init done, current fid 0x%x, vid 0x%x\n", 1077 data->currfid, data->currvid); 1078 1079 /* Point all the CPUs in this policy to the same data */ 1080 for_each_cpu(cpu, pol->cpus) 1081 per_cpu(powernow_data, cpu) = data; 1082 1083 return 0; 1084 1085 err_out_exit_acpi: 1086 powernow_k8_cpu_exit_acpi(data); 1087 1088 err_out: 1089 kfree(data); 1090 return -ENODEV; 1091 } 1092 1093 static void powernowk8_cpu_exit(struct cpufreq_policy *pol) 1094 { 1095 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu); 1096 int cpu; 1097 1098 if (!data) 1099 return; 1100 1101 powernow_k8_cpu_exit_acpi(data); 1102 1103 kfree(data->powernow_table); 1104 kfree(data); 1105 /* pol->cpus will be empty here, use related_cpus instead. */ 1106 for_each_cpu(cpu, pol->related_cpus) 1107 per_cpu(powernow_data, cpu) = NULL; 1108 } 1109 1110 static void query_values_on_cpu(void *_err) 1111 { 1112 int *err = _err; 1113 struct powernow_k8_data *data = __this_cpu_read(powernow_data); 1114 1115 *err = query_current_values_with_pending_wait(data); 1116 } 1117 1118 static unsigned int powernowk8_get(unsigned int cpu) 1119 { 1120 struct powernow_k8_data *data = per_cpu(powernow_data, cpu); 1121 unsigned int khz = 0; 1122 int err; 1123 1124 if (!data) 1125 return 0; 1126 1127 smp_call_function_single(cpu, query_values_on_cpu, &err, true); 1128 if (err) 1129 goto out; 1130 1131 khz = find_khz_freq_from_fid(data->currfid); 1132 1133 1134 out: 1135 return khz; 1136 } 1137 1138 static struct cpufreq_driver cpufreq_amd64_driver = { 1139 .flags = CPUFREQ_ASYNC_NOTIFICATION, 1140 .verify = cpufreq_generic_frequency_table_verify, 1141 .target_index = powernowk8_target, 1142 .bios_limit = acpi_processor_get_bios_limit, 1143 .init = powernowk8_cpu_init, 1144 .exit = powernowk8_cpu_exit, 1145 .get = powernowk8_get, 1146 .name = "powernow-k8", 1147 }; 1148 1149 static void __request_acpi_cpufreq(void) 1150 { 1151 const char drv[] = "acpi-cpufreq"; 1152 const char *cur_drv; 1153 1154 cur_drv = cpufreq_get_current_driver(); 1155 if (!cur_drv) 1156 goto request; 1157 1158 if (strncmp(cur_drv, drv, min_t(size_t, strlen(cur_drv), strlen(drv)))) 1159 pr_warn("WTF driver: %s\n", cur_drv); 1160 1161 return; 1162 1163 request: 1164 pr_warn("This CPU is not supported anymore, using acpi-cpufreq instead.\n"); 1165 request_module(drv); 1166 } 1167 1168 /* driver entry point for init */ 1169 static int powernowk8_init(void) 1170 { 1171 unsigned int i, supported_cpus = 0; 1172 int ret; 1173 1174 if (!x86_match_cpu(powernow_k8_ids)) 1175 return -ENODEV; 1176 1177 if (boot_cpu_has(X86_FEATURE_HW_PSTATE)) { 1178 __request_acpi_cpufreq(); 1179 return -ENODEV; 1180 } 1181 1182 cpus_read_lock(); 1183 for_each_online_cpu(i) { 1184 smp_call_function_single(i, check_supported_cpu, &ret, 1); 1185 if (!ret) 1186 supported_cpus++; 1187 } 1188 1189 if (supported_cpus != num_online_cpus()) { 1190 cpus_read_unlock(); 1191 return -ENODEV; 1192 } 1193 cpus_read_unlock(); 1194 1195 ret = cpufreq_register_driver(&cpufreq_amd64_driver); 1196 if (ret) 1197 return ret; 1198 1199 pr_info("Found %d %s (%d cpu cores) (" VERSION ")\n", 1200 num_online_nodes(), boot_cpu_data.x86_model_id, supported_cpus); 1201 1202 return ret; 1203 } 1204 1205 /* driver entry point for term */ 1206 static void __exit powernowk8_exit(void) 1207 { 1208 pr_debug("exit\n"); 1209 1210 cpufreq_unregister_driver(&cpufreq_amd64_driver); 1211 } 1212 1213 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com>"); 1214 MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@amd.com>"); 1215 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver."); 1216 MODULE_LICENSE("GPL"); 1217 1218 late_initcall(powernowk8_init); 1219 module_exit(powernowk8_exit); 1220