]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/cpufreq/sfi-cpufreq.c
Merge tag 'armsoc-fixes-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[mirror_ubuntu-eoan-kernel.git] / drivers / cpufreq / sfi-cpufreq.c
1 /*
2 * SFI Performance States Driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>
14 * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com>
15 */
16
17 #include <linux/cpufreq.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/sfi.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24
25 #include <asm/msr.h>
26
27 static struct cpufreq_frequency_table *freq_table;
28 static struct sfi_freq_table_entry *sfi_cpufreq_array;
29 static int num_freq_table_entries;
30
31 static int sfi_parse_freq(struct sfi_table_header *table)
32 {
33 struct sfi_table_simple *sb;
34 struct sfi_freq_table_entry *pentry;
35 int totallen;
36
37 sb = (struct sfi_table_simple *)table;
38 num_freq_table_entries = SFI_GET_NUM_ENTRIES(sb,
39 struct sfi_freq_table_entry);
40 if (num_freq_table_entries <= 1) {
41 pr_err("No p-states discovered\n");
42 return -ENODEV;
43 }
44
45 pentry = (struct sfi_freq_table_entry *)sb->pentry;
46 totallen = num_freq_table_entries * sizeof(*pentry);
47
48 sfi_cpufreq_array = kmemdup(pentry, totallen, GFP_KERNEL);
49 if (!sfi_cpufreq_array)
50 return -ENOMEM;
51
52 return 0;
53 }
54
55 static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
56 {
57 unsigned int next_perf_state = 0; /* Index into perf table */
58 u32 lo, hi;
59
60 next_perf_state = policy->freq_table[index].driver_data;
61
62 rdmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, &lo, &hi);
63 lo = (lo & ~INTEL_PERF_CTL_MASK) |
64 ((u32) sfi_cpufreq_array[next_perf_state].ctrl_val &
65 INTEL_PERF_CTL_MASK);
66 wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
67
68 return 0;
69 }
70
71 static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
72 {
73 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
74 policy->cpuinfo.transition_latency = 100000; /* 100us */
75 policy->freq_table = freq_table;
76
77 return 0;
78 }
79
80 static struct cpufreq_driver sfi_cpufreq_driver = {
81 .flags = CPUFREQ_CONST_LOOPS,
82 .verify = cpufreq_generic_frequency_table_verify,
83 .target_index = sfi_cpufreq_target,
84 .init = sfi_cpufreq_cpu_init,
85 .name = "sfi-cpufreq",
86 .attr = cpufreq_generic_attr,
87 };
88
89 static int __init sfi_cpufreq_init(void)
90 {
91 int ret, i;
92
93 /* parse the freq table from SFI */
94 ret = sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, sfi_parse_freq);
95 if (ret)
96 return ret;
97
98 freq_table = kcalloc(num_freq_table_entries + 1, sizeof(*freq_table),
99 GFP_KERNEL);
100 if (!freq_table) {
101 ret = -ENOMEM;
102 goto err_free_array;
103 }
104
105 for (i = 0; i < num_freq_table_entries; i++) {
106 freq_table[i].driver_data = i;
107 freq_table[i].frequency = sfi_cpufreq_array[i].freq_mhz * 1000;
108 }
109 freq_table[i].frequency = CPUFREQ_TABLE_END;
110
111 ret = cpufreq_register_driver(&sfi_cpufreq_driver);
112 if (ret)
113 goto err_free_tbl;
114
115 return ret;
116
117 err_free_tbl:
118 kfree(freq_table);
119 err_free_array:
120 kfree(sfi_cpufreq_array);
121 return ret;
122 }
123 late_initcall(sfi_cpufreq_init);
124
125 static void __exit sfi_cpufreq_exit(void)
126 {
127 cpufreq_unregister_driver(&sfi_cpufreq_driver);
128 kfree(freq_table);
129 kfree(sfi_cpufreq_array);
130 }
131 module_exit(sfi_cpufreq_exit);
132
133 MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>");
134 MODULE_DESCRIPTION("SFI Performance-States Driver");
135 MODULE_LICENSE("GPL");