1 // SPDX-License-Identifier: GPL-2.0-only
3 * SFI Performance States Driver
5 * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>
6 * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com>
9 #include <linux/cpufreq.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sfi.h>
14 #include <linux/slab.h>
15 #include <linux/smp.h>
19 static struct cpufreq_frequency_table
*freq_table
;
20 static struct sfi_freq_table_entry
*sfi_cpufreq_array
;
21 static int num_freq_table_entries
;
23 static int sfi_parse_freq(struct sfi_table_header
*table
)
25 struct sfi_table_simple
*sb
;
26 struct sfi_freq_table_entry
*pentry
;
29 sb
= (struct sfi_table_simple
*)table
;
30 num_freq_table_entries
= SFI_GET_NUM_ENTRIES(sb
,
31 struct sfi_freq_table_entry
);
32 if (num_freq_table_entries
<= 1) {
33 pr_err("No p-states discovered\n");
37 pentry
= (struct sfi_freq_table_entry
*)sb
->pentry
;
38 totallen
= num_freq_table_entries
* sizeof(*pentry
);
40 sfi_cpufreq_array
= kmemdup(pentry
, totallen
, GFP_KERNEL
);
41 if (!sfi_cpufreq_array
)
47 static int sfi_cpufreq_target(struct cpufreq_policy
*policy
, unsigned int index
)
49 unsigned int next_perf_state
= 0; /* Index into perf table */
52 next_perf_state
= policy
->freq_table
[index
].driver_data
;
54 rdmsr_on_cpu(policy
->cpu
, MSR_IA32_PERF_CTL
, &lo
, &hi
);
55 lo
= (lo
& ~INTEL_PERF_CTL_MASK
) |
56 ((u32
) sfi_cpufreq_array
[next_perf_state
].ctrl_val
&
58 wrmsr_on_cpu(policy
->cpu
, MSR_IA32_PERF_CTL
, lo
, hi
);
63 static int sfi_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
65 policy
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
66 policy
->cpuinfo
.transition_latency
= 100000; /* 100us */
67 policy
->freq_table
= freq_table
;
72 static struct cpufreq_driver sfi_cpufreq_driver
= {
73 .flags
= CPUFREQ_CONST_LOOPS
,
74 .verify
= cpufreq_generic_frequency_table_verify
,
75 .target_index
= sfi_cpufreq_target
,
76 .init
= sfi_cpufreq_cpu_init
,
77 .name
= "sfi-cpufreq",
78 .attr
= cpufreq_generic_attr
,
81 static int __init
sfi_cpufreq_init(void)
85 /* parse the freq table from SFI */
86 ret
= sfi_table_parse(SFI_SIG_FREQ
, NULL
, NULL
, sfi_parse_freq
);
90 freq_table
= kcalloc(num_freq_table_entries
+ 1, sizeof(*freq_table
),
97 for (i
= 0; i
< num_freq_table_entries
; i
++) {
98 freq_table
[i
].driver_data
= i
;
99 freq_table
[i
].frequency
= sfi_cpufreq_array
[i
].freq_mhz
* 1000;
101 freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
103 ret
= cpufreq_register_driver(&sfi_cpufreq_driver
);
112 kfree(sfi_cpufreq_array
);
115 late_initcall(sfi_cpufreq_init
);
117 static void __exit
sfi_cpufreq_exit(void)
119 cpufreq_unregister_driver(&sfi_cpufreq_driver
);
121 kfree(sfi_cpufreq_array
);
123 module_exit(sfi_cpufreq_exit
);
125 MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>");
126 MODULE_DESCRIPTION("SFI Performance-States Driver");
127 MODULE_LICENSE("GPL");