^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) * amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * for the ondemand governor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2013 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Jacob Shin <jacob.shin@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^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/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/percpu-defs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/cpu_device_id.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "cpufreq_ondemand.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define CLASS_CODE_SHIFT 56
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define POWERSAVE_BIAS_MAX 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define POWERSAVE_BIAS_DEF 400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct cpu_data_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u64 actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u64 reference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int freq_prev;
^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) static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int freq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int relation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int sensitivity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) long d_actual, d_reference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct msr actual, reference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct policy_dbs_info *policy_dbs = policy->governor_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct dbs_data *od_data = policy_dbs->dbs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct od_dbs_tuners *od_tuners = od_data->tuners;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!policy->freq_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return freq_next;
^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_AMD64_FREQ_SENSITIVITY_ACTUAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) &actual.l, &actual.h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) &reference.l, &reference.h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) actual.h &= 0x00ffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) reference.h &= 0x00ffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* counter wrapped around, so stay on current frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (actual.q < data->actual || reference.q < data->reference) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) freq_next = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) d_actual = actual.q - data->actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) d_reference = reference.q - data->reference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* divide by 0, so stay on current frequency as well */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (d_reference == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) freq_next = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sensitivity = POWERSAVE_BIAS_MAX -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* this workload is not CPU bound, so choose a lower freq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (sensitivity < od_tuners->powersave_bias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (data->freq_prev == policy->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) freq_next = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (freq_next > policy->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) freq_next = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) else if (freq_next < policy->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) freq_next = policy->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) index = cpufreq_table_find_index_h(policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) policy->cur - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) freq_next = policy->freq_table[index].frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) data->freq_prev = freq_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) data->freq_prev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) data->actual = actual.q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) data->reference = reference.q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return freq_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int __init amd_freq_sensitivity_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct pci_dev *pcidev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned int pci_vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pci_vendor = PCI_VENDOR_ID_AMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pci_vendor = PCI_VENDOR_ID_HYGON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pcidev = pci_get_device(pci_vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!pcidev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!(val >> CLASS_CODE_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) od_register_powersave_bias_handler(amd_powersave_bias_target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) POWERSAVE_BIAS_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) late_initcall(amd_freq_sensitivity_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void __exit amd_freq_sensitivity_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) od_unregister_powersave_bias_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) module_exit(amd_freq_sensitivity_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) "the ondemand governor.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) MODULE_LICENSE("GPL");