^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SFI Performance States Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sfi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static struct cpufreq_frequency_table *freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct sfi_freq_table_entry *sfi_cpufreq_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int num_freq_table_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int sfi_parse_freq(struct sfi_table_header *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct sfi_table_simple *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct sfi_freq_table_entry *pentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int totallen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) sb = (struct sfi_table_simple *)table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) num_freq_table_entries = SFI_GET_NUM_ENTRIES(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct sfi_freq_table_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (num_freq_table_entries <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) pr_err("No p-states discovered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pentry = (struct sfi_freq_table_entry *)sb->pentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) totallen = num_freq_table_entries * sizeof(*pentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) sfi_cpufreq_array = kmemdup(pentry, totallen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!sfi_cpufreq_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int next_perf_state = 0; /* Index into perf table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u32 lo, hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) next_perf_state = policy->freq_table[index].driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) rdmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, &lo, &hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) lo = (lo & ~INTEL_PERF_CTL_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ((u32) sfi_cpufreq_array[next_perf_state].ctrl_val &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) INTEL_PERF_CTL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) policy->cpuinfo.transition_latency = 100000; /* 100us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) policy->freq_table = freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static struct cpufreq_driver sfi_cpufreq_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .flags = CPUFREQ_CONST_LOOPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .verify = cpufreq_generic_frequency_table_verify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .target_index = sfi_cpufreq_target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .init = sfi_cpufreq_cpu_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .name = "sfi-cpufreq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .attr = cpufreq_generic_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int __init sfi_cpufreq_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* parse the freq table from SFI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, sfi_parse_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) freq_table = kcalloc(num_freq_table_entries + 1, sizeof(*freq_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!freq_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto err_free_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (i = 0; i < num_freq_table_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) freq_table[i].driver_data = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) freq_table[i].frequency = sfi_cpufreq_array[i].freq_mhz * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) freq_table[i].frequency = CPUFREQ_TABLE_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ret = cpufreq_register_driver(&sfi_cpufreq_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto err_free_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) err_free_tbl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kfree(freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) err_free_array:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) kfree(sfi_cpufreq_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) late_initcall(sfi_cpufreq_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void __exit sfi_cpufreq_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) cpufreq_unregister_driver(&sfi_cpufreq_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) kfree(sfi_cpufreq_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) module_exit(sfi_cpufreq_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DESCRIPTION("SFI Performance-States Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_LICENSE("GPL");