30 #include <linux/kernel.h>
32 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
48 #define PFX "powernow-k8: "
49 #define VERSION "version 2.20.00"
60 static inline const struct cpumask *cpu_core_mask(
int cpu)
69 return 800 + (fid * 100);
73 static u32 find_khz_freq_from_fid(
u32 fid)
75 return 1000 * find_freq_from_fid(fid);
84 static u32 convert_fid_to_vco_fid(
u32 fid)
96 static int pending_bit_stuck(
void)
115 pr_debug(
"detected change pending stuck\n");
142 static void fidvid_msr_init(
void)
172 pr_debug(
"writing fid 0x%x, lo 0x%x, hi 0x%x\n",
179 "Hardware error - pending bit very stuck - "
180 "no further pstate changes possible\n");
183 }
while (query_current_values_with_pending_wait(data));
187 if (savevid != data->
currvid) {
189 "vid change on fid trans, old 0x%x, new 0x%x\n",
196 "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
220 pr_debug(
"writing vid 0x%x, lo 0x%x, hi 0x%x\n",
227 "very stuck - no further pstate "
228 "changes possible\n");
231 }
while (query_current_values_with_pending_wait(data));
233 if (savefid != data->
currfid) {
258 if ((data->
currvid - reqvid) > step)
261 if (write_new_vid(data, reqvid))
273 if (core_voltage_pre_transition(data, reqvid, reqfid))
276 if (core_frequency_transition(data, reqfid))
279 if (core_voltage_post_transition(data, reqvid))
282 if (query_current_values_with_pending_wait(data))
293 pr_debug(
"transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
305 u32 maxvid, lo, rvomult = 1;
307 pr_debug(
"ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
308 "reqvid 0x%x, rvo 0x%x\n",
316 maxvid = 0x1f & (maxvid >> 16);
317 pr_debug(
"ph1 maxvid=0x%x\n", maxvid);
321 while (data->
currvid > reqvid) {
322 pr_debug(
"ph1: curr 0x%x, req vid 0x%x\n",
324 if (decrease_vid_code_by_step(data, reqvid, data->
vidmvs))
328 while ((rvosteps > 0) &&
329 ((rvomult * data->
rvo + data->
currvid) > reqvid)) {
333 pr_debug(
"ph1: changing vid for rvo, req 0x%x\n",
335 if (decrease_vid_code_by_step(data, data->
currvid-1, 1))
341 if (query_current_values_with_pending_wait(data))
344 if (savefid != data->
currfid) {
350 pr_debug(
"ph1 complete, currfid 0x%x, currvid 0x%x\n",
359 u32 vcoreqfid, vcocurrfid, vcofiddiff;
368 pr_debug(
"ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
373 vcoreqfid = convert_fid_to_vco_fid(reqfid);
374 vcocurrfid = convert_fid_to_vco_fid(data->
currfid);
375 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
376 : vcoreqfid - vcocurrfid;
381 while (vcofiddiff > 2) {
382 (data->
currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
386 if (write_new_fid(data,
392 2 + convert_fid_to_vco_fid(data->
currfid)))
396 if (write_new_fid(data, data->
currfid - fid_interval))
400 vcocurrfid = convert_fid_to_vco_fid(data->
currfid);
401 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
402 : vcoreqfid - vcocurrfid;
405 if (write_new_fid(data, reqfid))
408 if (query_current_values_with_pending_wait(data))
413 "ph2: mismatch, failed fid transition, "
414 "curr 0x%x, req 0x%x\n",
419 if (savevid != data->
currvid) {
425 pr_debug(
"ph2 complete, currfid 0x%x, currvid 0x%x\n",
436 u32 savereqvid = reqvid;
438 pr_debug(
"ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
443 if (write_new_vid(data, reqvid))
446 if (savefid != data->
currfid) {
448 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
455 "ph3: failed vid transition\n, "
456 "req 0x%x, curr 0x%x",
462 if (query_current_values_with_pending_wait(data))
465 if (savereqvid != data->
currvid) {
470 if (savefid != data->
currfid) {
471 pr_debug(
"ph3 failed, currfid changed 0x%x\n",
476 pr_debug(
"ph3 complete, currfid 0x%x, currvid 0x%x\n",
482 static const struct x86_cpu_id powernow_k8_ids[] = {
489 static void check_supported_cpu(
void *_rc)
502 "Processor cpuid %x not supported\n", eax);
509 "No frequency change capabilities detected\n");
515 != P_STATE_TRANSITION_CAPABLE) {
517 "Power state transitions not supported\n");
530 for (j = 0; j < data->
numps; j++) {
536 if (pst[j].vid < data->rvo) {
542 if (pst[j].vid < maxvid + data->rvo) {
556 "0x%x\n", j, pst[j].fid);
559 if (pst[j].fid < lastfid)
560 lastfid = pst[
j].
fid;
568 "first fid not from lo freq table\n");
582 for (j = 0; j < data->
numps; j++) {
586 "fid 0x%x (%d MHz), vid 0x%x\n",
606 "Only %d pstates usable (use ACPI driver for full "
607 "range\n", data->
batps);
611 for (j = 1; j < data->
numps; j++) {
612 if (pst[j-1].fid >= pst[j].fid) {
618 if (data->
numps < 2) {
623 if (check_pst_table(data, pst, maxvid))
628 if (!powernow_table) {
633 for (j = 0; j < data->
numps; j++) {
637 freq = find_khz_freq_from_fid(pst[j].fid);
643 if (query_current_values_with_pending_wait(data)) {
644 kfree(powernow_table);
650 if (cpumask_first(cpu_core_mask(data->
cpu)) == data->
cpu)
653 for (j = 0; j < data->
numps; j++)
654 if ((pst[j].fid == data->
currfid) &&
658 pr_debug(
"currfid/vid do not match PST, ignoring\n");
672 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
680 pr_debug(
"found PSB header at 0x%p\n", psb);
695 pr_debug(
"voltage stabilization time: %d(*20us)\n",
701 mvs = ((psb->
flags2) >> 4) & 3;
706 pr_debug(
"isochronous relief time: %d\n", data->
irt);
711 if ((psb->
cpuid == 0x00000fc0) ||
712 (psb->
cpuid == 0x00000fe0)) {
714 if ((thiscpuid == 0x00000fc0) ||
715 (thiscpuid == 0x00000fe0))
731 return fill_powernow_table(data,
732 (
struct pst_s *)(psb+1), maxvid);
747 " and Cool'N'Quiet support is enabled in BIOS setup\n");
775 pr_debug(
"register performance failed: bad ACPI data\n");
785 control = data->
acpi_data.control_register.space_id;
786 status = data->
acpi_data.status_register.space_id;
790 pr_debug(
"Invalid control/status registers (%llx - %llx)\n",
798 if (!powernow_table) {
799 pr_debug(
"powernow_table memory alloc failure\n");
805 powernow_k8_acpi_pst_values(data, 0);
807 ret_val = fill_powernow_table_fidvid(data, powernow_table);
816 if (cpumask_first(cpu_core_mask(data->
cpu)) == data->
cpu)
824 "unable to alloc powernow_k8_data cpumask\n");
832 kfree(powernow_table);
849 for (i = 0; i < data->
acpi_data.state_count; i++) {
865 pr_debug(
" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
867 index = fid | (vid<<8);
870 freq = find_khz_freq_from_fid(fid);
875 pr_debug(
"invalid freq %u kHz, ignoring\n", freq);
876 invalidate_entry(powernow_table, i);
883 pr_debug(
"invalid vid %u, ignoring\n", vid);
884 invalidate_entry(powernow_table, i);
888 if (freq != (data->
acpi_data.states[i].core_frequency * 1000)) {
890 "%u kHz vs. %u kHz\n", freq,
892 (data->
acpi_data.states[i].core_frequency
894 invalidate_entry(powernow_table, i);
906 free_cpumask_var(data->
acpi_data.shared_cpu_map);
913 for (i = 0; i < data->
acpi_data.state_count; i++) {
914 int cur_latency = data->
acpi_data.states[
i].transition_latency
915 + data->
acpi_data.states[
i].bus_master_latency;
916 if (cur_latency > max_latency)
917 max_latency = cur_latency;
919 if (max_latency == 0) {
924 return 1000 * max_latency;
946 pr_debug(
"table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
948 if (query_current_values_with_pending_wait(data))
952 pr_debug(
"target matches current values (fid 0x%x, vid 0x%x)\n",
957 pr_debug(
"cpu %d, changing to fid 0x%x, vid 0x%x\n",
959 freqs.old = find_khz_freq_from_fid(data->
currfid);
960 freqs.new = find_khz_freq_from_fid(fid);
967 res = transition_fid_vid(data, fid, vid);
971 freqs.new = find_khz_freq_from_fid(data->
currfid);
986 static long powernowk8_target_fn(
void *
arg)
995 unsigned int newstate;
1004 if (pending_bit_stuck()) {
1009 pr_debug(
"targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1010 pol->
cpu, targfreq, pol->
min, pol->
max, relation);
1012 if (query_current_values_with_pending_wait(data))
1015 pr_debug(
"targ: curr fid 0x%x, vid 0x%x\n",
1018 if ((checkvid != data->
currvid) ||
1019 (checkfid != data->
currfid)) {
1021 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1027 targfreq, relation, &newstate))
1032 powernow_k8_acpi_pst_values(data, newstate);
1034 ret = transition_frequency_fidvid(data, newstate);
1043 pol->
cur = find_khz_freq_from_fid(data->
currfid);
1050 unsigned targfreq,
unsigned relation)
1053 .relation = relation };
1055 return work_on_cpu(pol->
cpu, powernowk8_target_fn, &pta);
1074 static void __cpuinit powernowk8_cpu_init_on_cpu(
void *_init_on_cpu)
1078 if (pending_bit_stuck()) {
1084 if (query_current_values_with_pending_wait(init_on_cpu->
data)) {
1091 init_on_cpu->
rc = 0;
1094 static const char missing_pss_msg[] =
1096 FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
1097 FW_BUG PFX "First, make sure Cool'N'Quiet is enabled in the BIOS.\n"
1098 FW_BUG PFX "If that doesn't help, try upgrading your BIOS.\n";
1104 struct init_on_cpu init_on_cpu;
1122 if (powernow_k8_cpu_init_acpi(data)) {
1131 if (pol->
cpu != 0) {
1133 "CPU other than CPU0. Complain to your BIOS "
1137 rc = find_psb_table(data);
1143 pol->
cpuinfo.transition_latency = (
1145 ((1 << data->
irt) * 30)) * 1000;
1147 pol->
cpuinfo.transition_latency = get_transition_latency(data);
1153 rc = init_on_cpu.
rc;
1155 goto err_out_exit_acpi;
1157 cpumask_copy(pol->
cpus, cpu_core_mask(pol->
cpu));
1160 pol->
cur = find_khz_freq_from_fid(data->
currfid);
1161 pr_debug(
"policy current frequency %d kHz\n", pol->
cur);
1166 powernow_k8_cpu_exit_acpi(data);
1174 pr_debug(
"cpu_init done, current fid 0x%x, vid 0x%x\n",
1182 powernow_k8_cpu_exit_acpi(data);
1196 powernow_k8_cpu_exit_acpi(data);
1207 static void query_values_on_cpu(
void *_err)
1212 *err = query_current_values_with_pending_wait(data);
1215 static unsigned int powernowk8_get(
unsigned int cpu)
1218 unsigned int khz = 0;
1228 khz = find_khz_freq_from_fid(data->
currfid);
1235 static struct freq_attr *powernow_k8_attr[] = {
1241 .verify = powernowk8_verify,
1242 .target = powernowk8_target,
1244 .init = powernowk8_cpu_init,
1246 .get = powernowk8_get,
1247 .name =
"powernow-k8",
1249 .attr = powernow_k8_attr,
1253 static int __cpuinit powernowk8_init(
void)
1255 unsigned int i, supported_cpus = 0;
1259 pr_warn(PFX
"this CPU is not supported anymore, using acpi-cpufreq instead.\n");
1260 request_module(
"acpi-cpufreq");
1288 static void __exit powernowk8_exit(
void)