1
2
3
4
5
6
7
8
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>
16
17#include <asm/msr.h>
18
19static struct cpufreq_frequency_table *freq_table;
20static struct sfi_freq_table_entry *sfi_cpufreq_array;
21static int num_freq_table_entries;
22
23static int sfi_parse_freq(struct sfi_table_header *table)
24{
25 struct sfi_table_simple *sb;
26 struct sfi_freq_table_entry *pentry;
27 int totallen;
28
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");
34 return -ENODEV;
35 }
36
37 pentry = (struct sfi_freq_table_entry *)sb->pentry;
38 totallen = num_freq_table_entries * sizeof(*pentry);
39
40 sfi_cpufreq_array = kmemdup(pentry, totallen, GFP_KERNEL);
41 if (!sfi_cpufreq_array)
42 return -ENOMEM;
43
44 return 0;
45}
46
47static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
48{
49 unsigned int next_perf_state = 0;
50 u32 lo, hi;
51
52 next_perf_state = policy->freq_table[index].driver_data;
53
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 &
57 INTEL_PERF_CTL_MASK);
58 wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
59
60 return 0;
61}
62
63static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
64{
65 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
66 policy->cpuinfo.transition_latency = 100000;
67 policy->freq_table = freq_table;
68
69 return 0;
70}
71
72static 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,
79};
80
81static int __init sfi_cpufreq_init(void)
82{
83 int ret, i;
84
85
86 ret = sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, sfi_parse_freq);
87 if (ret)
88 return ret;
89
90 freq_table = kcalloc(num_freq_table_entries + 1, sizeof(*freq_table),
91 GFP_KERNEL);
92 if (!freq_table) {
93 ret = -ENOMEM;
94 goto err_free_array;
95 }
96
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;
100 }
101 freq_table[i].frequency = CPUFREQ_TABLE_END;
102
103 ret = cpufreq_register_driver(&sfi_cpufreq_driver);
104 if (ret)
105 goto err_free_tbl;
106
107 return ret;
108
109err_free_tbl:
110 kfree(freq_table);
111err_free_array:
112 kfree(sfi_cpufreq_array);
113 return ret;
114}
115late_initcall(sfi_cpufreq_init);
116
117static void __exit sfi_cpufreq_exit(void)
118{
119 cpufreq_unregister_driver(&sfi_cpufreq_driver);
120 kfree(freq_table);
121 kfree(sfi_cpufreq_array);
122}
123module_exit(sfi_cpufreq_exit);
124
125MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>");
126MODULE_DESCRIPTION("SFI Performance-States Driver");
127MODULE_LICENSE("GPL");
128