LiteX: driver for MMCM
[linux/fpc-iii.git] / drivers / cpufreq / sfi-cpufreq.c
blob45cfdf67cf03d7c2932bf63e24cf08aee643f2d7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SFI Performance States Driver
5 * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>
6 * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com>
7 */
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>
17 #include <asm/msr.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;
27 int totallen;
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;
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)
42 return -ENOMEM;
44 return 0;
47 static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
49 unsigned int next_perf_state = 0; /* Index into perf table */
50 u32 lo, hi;
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 &
57 INTEL_PERF_CTL_MASK);
58 wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
60 return 0;
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;
69 return 0;
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)
83 int ret, i;
85 /* parse the freq table from SFI */
86 ret = sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, sfi_parse_freq);
87 if (ret)
88 return ret;
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;
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);
104 if (ret)
105 goto err_free_tbl;
107 return ret;
109 err_free_tbl:
110 kfree(freq_table);
111 err_free_array:
112 kfree(sfi_cpufreq_array);
113 return ret;
115 late_initcall(sfi_cpufreq_init);
117 static void __exit sfi_cpufreq_exit(void)
119 cpufreq_unregister_driver(&sfi_cpufreq_driver);
120 kfree(freq_table);
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");