Thomas Gleixner | 2504ba9 | 2019-06-03 07:44:51 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 2 | /* |
| 3 | * SFI Performance States Driver |
| 4 | * |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 5 | * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com> |
| 6 | * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com> |
| 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 | |
Colin Ian King | 0370f0f | 2017-06-15 10:55:59 +0100 | [diff] [blame] | 19 | static struct cpufreq_frequency_table *freq_table; |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 20 | static struct sfi_freq_table_entry *sfi_cpufreq_array; |
| 21 | static int num_freq_table_entries; |
| 22 | |
| 23 | static 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 | |
Andrzej Hajda | a482e55 | 2015-08-07 09:59:17 +0200 | [diff] [blame] | 40 | sfi_cpufreq_array = kmemdup(pentry, totallen, GFP_KERNEL); |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 41 | if (!sfi_cpufreq_array) |
| 42 | return -ENOMEM; |
| 43 | |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index) |
| 48 | { |
| 49 | unsigned int next_perf_state = 0; /* Index into perf table */ |
| 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 | |
| 63 | static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy) |
| 64 | { |
| 65 | policy->shared_type = CPUFREQ_SHARED_TYPE_HW; |
| 66 | policy->cpuinfo.transition_latency = 100000; /* 100us */ |
Viresh Kumar | 54a277e | 2018-02-26 10:39:05 +0530 | [diff] [blame] | 67 | policy->freq_table = freq_table; |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 68 | |
Viresh Kumar | 54a277e | 2018-02-26 10:39:05 +0530 | [diff] [blame] | 69 | return 0; |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 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, |
| 79 | }; |
| 80 | |
| 81 | static int __init sfi_cpufreq_init(void) |
| 82 | { |
| 83 | int ret, i; |
| 84 | |
| 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; |
| 89 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 90 | freq_table = kcalloc(num_freq_table_entries + 1, sizeof(*freq_table), |
| 91 | GFP_KERNEL); |
Srinidhi Kasagar | e7ddf4b | 2014-12-19 23:13:51 +0530 | [diff] [blame] | 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 | |
| 109 | err_free_tbl: |
| 110 | kfree(freq_table); |
| 111 | err_free_array: |
| 112 | kfree(sfi_cpufreq_array); |
| 113 | return ret; |
| 114 | } |
| 115 | late_initcall(sfi_cpufreq_init); |
| 116 | |
| 117 | static void __exit sfi_cpufreq_exit(void) |
| 118 | { |
| 119 | cpufreq_unregister_driver(&sfi_cpufreq_driver); |
| 120 | kfree(freq_table); |
| 121 | kfree(sfi_cpufreq_array); |
| 122 | } |
| 123 | module_exit(sfi_cpufreq_exit); |
| 124 | |
| 125 | MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>"); |
| 126 | MODULE_DESCRIPTION("SFI Performance-States Driver"); |
| 127 | MODULE_LICENSE("GPL"); |