2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-04-05 00:19:04 +08:00
|
|
|
/*
|
|
|
|
* amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
|
|
|
|
* for the ondemand governor.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Author: Jacob Shin <jacob.shin@amd.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
2018-01-18 18:21:30 +08:00
|
|
|
#include <linux/pci.h>
|
2013-04-05 00:19:04 +08:00
|
|
|
#include <linux/percpu-defs.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mod_devicetable.h>
|
|
|
|
|
|
|
|
#include <asm/msr.h>
|
|
|
|
#include <asm/cpufeature.h>
|
2020-03-20 21:13:46 +08:00
|
|
|
#include <asm/cpu_device_id.h>
|
2013-04-05 00:19:04 +08:00
|
|
|
|
2016-02-19 01:40:14 +08:00
|
|
|
#include "cpufreq_ondemand.h"
|
2013-04-05 00:19:04 +08:00
|
|
|
|
|
|
|
#define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
|
|
|
|
#define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
|
|
|
|
#define CLASS_CODE_SHIFT 56
|
|
|
|
#define POWERSAVE_BIAS_MAX 1000
|
|
|
|
#define POWERSAVE_BIAS_DEF 400
|
|
|
|
|
|
|
|
struct cpu_data_t {
|
|
|
|
u64 actual;
|
|
|
|
u64 reference;
|
|
|
|
unsigned int freq_prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
|
|
|
|
|
|
|
|
static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
|
|
|
|
unsigned int freq_next,
|
|
|
|
unsigned int relation)
|
|
|
|
{
|
|
|
|
int sensitivity;
|
|
|
|
long d_actual, d_reference;
|
|
|
|
struct msr actual, reference;
|
|
|
|
struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
|
2016-02-07 23:24:26 +08:00
|
|
|
struct policy_dbs_info *policy_dbs = policy->governor_data;
|
|
|
|
struct dbs_data *od_data = policy_dbs->dbs_data;
|
2013-04-05 00:19:04 +08:00
|
|
|
struct od_dbs_tuners *od_tuners = od_data->tuners;
|
|
|
|
|
2016-06-03 13:28:48 +08:00
|
|
|
if (!policy->freq_table)
|
2013-04-05 00:19:04 +08:00
|
|
|
return freq_next;
|
|
|
|
|
|
|
|
rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
|
|
|
|
&actual.l, &actual.h);
|
|
|
|
rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
|
|
|
|
&reference.l, &reference.h);
|
|
|
|
actual.h &= 0x00ffffff;
|
|
|
|
reference.h &= 0x00ffffff;
|
|
|
|
|
|
|
|
/* counter wrapped around, so stay on current frequency */
|
|
|
|
if (actual.q < data->actual || reference.q < data->reference) {
|
|
|
|
freq_next = policy->cur;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
d_actual = actual.q - data->actual;
|
|
|
|
d_reference = reference.q - data->reference;
|
|
|
|
|
|
|
|
/* divide by 0, so stay on current frequency as well */
|
|
|
|
if (d_reference == 0) {
|
|
|
|
freq_next = policy->cur;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sensitivity = POWERSAVE_BIAS_MAX -
|
|
|
|
(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
|
|
|
|
|
|
|
|
clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
|
|
|
|
|
|
|
|
/* this workload is not CPU bound, so choose a lower freq */
|
|
|
|
if (sensitivity < od_tuners->powersave_bias) {
|
|
|
|
if (data->freq_prev == policy->cur)
|
|
|
|
freq_next = policy->cur;
|
|
|
|
|
|
|
|
if (freq_next > policy->cur)
|
|
|
|
freq_next = policy->cur;
|
|
|
|
else if (freq_next < policy->cur)
|
|
|
|
freq_next = policy->min;
|
|
|
|
else {
|
|
|
|
unsigned int index;
|
|
|
|
|
2016-06-27 12:29:34 +08:00
|
|
|
index = cpufreq_table_find_index_h(policy,
|
2021-09-08 22:05:28 +08:00
|
|
|
policy->cur - 1,
|
|
|
|
relation & CPUFREQ_RELATION_E);
|
2016-06-03 13:28:48 +08:00
|
|
|
freq_next = policy->freq_table[index].frequency;
|
2013-04-05 00:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data->freq_prev = freq_next;
|
|
|
|
} else
|
|
|
|
data->freq_prev = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
data->actual = actual.q;
|
|
|
|
data->reference = reference.q;
|
|
|
|
return freq_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init amd_freq_sensitivity_init(void)
|
|
|
|
{
|
|
|
|
u64 val;
|
2018-01-18 18:21:30 +08:00
|
|
|
struct pci_dev *pcidev;
|
2018-09-23 17:37:38 +08:00
|
|
|
unsigned int pci_vendor;
|
2013-04-05 00:19:04 +08:00
|
|
|
|
2018-09-23 17:37:38 +08:00
|
|
|
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
|
|
|
|
pci_vendor = PCI_VENDOR_ID_AMD;
|
|
|
|
else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
|
|
|
|
pci_vendor = PCI_VENDOR_ID_HYGON;
|
|
|
|
else
|
2013-04-05 00:19:04 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2018-09-23 17:37:38 +08:00
|
|
|
pcidev = pci_get_device(pci_vendor,
|
2018-01-18 18:21:30 +08:00
|
|
|
PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
|
|
|
|
|
|
|
|
if (!pcidev) {
|
2019-03-30 19:20:22 +08:00
|
|
|
if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
|
2018-01-18 18:21:30 +08:00
|
|
|
return -ENODEV;
|
2022-11-16 19:33:39 +08:00
|
|
|
} else {
|
|
|
|
pci_dev_put(pcidev);
|
2018-01-18 18:21:30 +08:00
|
|
|
}
|
2013-04-05 00:19:04 +08:00
|
|
|
|
|
|
|
if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!(val >> CLASS_CODE_SHIFT))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
od_register_powersave_bias_handler(amd_powersave_bias_target,
|
|
|
|
POWERSAVE_BIAS_DEF);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
late_initcall(amd_freq_sensitivity_init);
|
|
|
|
|
|
|
|
static void __exit amd_freq_sensitivity_exit(void)
|
|
|
|
{
|
|
|
|
od_unregister_powersave_bias_handler();
|
|
|
|
}
|
|
|
|
module_exit(amd_freq_sensitivity_exit);
|
|
|
|
|
2020-07-15 16:26:34 +08:00
|
|
|
static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
|
2020-03-24 21:51:51 +08:00
|
|
|
X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
|
2013-04-05 00:19:04 +08:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
|
|
|
|
MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
|
|
|
|
"the ondemand governor.");
|
|
|
|
MODULE_LICENSE("GPL");
|