blob: d0b10baf039ab3b54b2b5dc488be9d68aa9ceeaf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jacob Shin9c5320c2013-04-04 16:19:04 +00002/*
3 * amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
4 * for the ondemand governor.
5 *
6 * Copyright (C) 2013 Advanced Micro Devices, Inc.
7 *
8 * Author: Jacob Shin <jacob.shin@amd.com>
Jacob Shin9c5320c2013-04-04 16:19:04 +00009 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/types.h>
Akshu Agrawal59a3b3a2018-01-18 15:51:30 +053014#include <linux/pci.h>
Jacob Shin9c5320c2013-04-04 16:19:04 +000015#include <linux/percpu-defs.h>
16#include <linux/init.h>
17#include <linux/mod_devicetable.h>
18
19#include <asm/msr.h>
20#include <asm/cpufeature.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010021#include <asm/cpu_device_id.h>
Jacob Shin9c5320c2013-04-04 16:19:04 +000022
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010023#include "cpufreq_ondemand.h"
Jacob Shin9c5320c2013-04-04 16:19:04 +000024
25#define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
26#define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
27#define CLASS_CODE_SHIFT 56
28#define POWERSAVE_BIAS_MAX 1000
29#define POWERSAVE_BIAS_DEF 400
30
31struct cpu_data_t {
32 u64 actual;
33 u64 reference;
34 unsigned int freq_prev;
35};
36
37static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
38
39static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
40 unsigned int freq_next,
41 unsigned int relation)
42{
43 int sensitivity;
44 long d_actual, d_reference;
45 struct msr actual, reference;
46 struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010047 struct policy_dbs_info *policy_dbs = policy->governor_data;
48 struct dbs_data *od_data = policy_dbs->dbs_data;
Jacob Shin9c5320c2013-04-04 16:19:04 +000049 struct od_dbs_tuners *od_tuners = od_data->tuners;
Jacob Shin9c5320c2013-04-04 16:19:04 +000050
Viresh Kumar34ac5d72016-06-03 10:58:48 +053051 if (!policy->freq_table)
Jacob Shin9c5320c2013-04-04 16:19:04 +000052 return freq_next;
53
54 rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
55 &actual.l, &actual.h);
56 rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
57 &reference.l, &reference.h);
58 actual.h &= 0x00ffffff;
59 reference.h &= 0x00ffffff;
60
61 /* counter wrapped around, so stay on current frequency */
62 if (actual.q < data->actual || reference.q < data->reference) {
63 freq_next = policy->cur;
64 goto out;
65 }
66
67 d_actual = actual.q - data->actual;
68 d_reference = reference.q - data->reference;
69
70 /* divide by 0, so stay on current frequency as well */
71 if (d_reference == 0) {
72 freq_next = policy->cur;
73 goto out;
74 }
75
76 sensitivity = POWERSAVE_BIAS_MAX -
77 (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
78
79 clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
80
81 /* this workload is not CPU bound, so choose a lower freq */
82 if (sensitivity < od_tuners->powersave_bias) {
83 if (data->freq_prev == policy->cur)
84 freq_next = policy->cur;
85
86 if (freq_next > policy->cur)
87 freq_next = policy->cur;
88 else if (freq_next < policy->cur)
89 freq_next = policy->min;
90 else {
91 unsigned int index;
92
Viresh Kumar82577362016-06-27 09:59:34 +053093 index = cpufreq_table_find_index_h(policy,
94 policy->cur - 1);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053095 freq_next = policy->freq_table[index].frequency;
Jacob Shin9c5320c2013-04-04 16:19:04 +000096 }
97
98 data->freq_prev = freq_next;
99 } else
100 data->freq_prev = 0;
101
102out:
103 data->actual = actual.q;
104 data->reference = reference.q;
105 return freq_next;
106}
107
108static int __init amd_freq_sensitivity_init(void)
109{
110 u64 val;
Akshu Agrawal59a3b3a2018-01-18 15:51:30 +0530111 struct pci_dev *pcidev;
Pu Wencc9690c2018-09-23 17:37:38 +0800112 unsigned int pci_vendor;
Jacob Shin9c5320c2013-04-04 16:19:04 +0000113
Pu Wencc9690c2018-09-23 17:37:38 +0800114 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
115 pci_vendor = PCI_VENDOR_ID_AMD;
116 else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
117 pci_vendor = PCI_VENDOR_ID_HYGON;
118 else
Jacob Shin9c5320c2013-04-04 16:19:04 +0000119 return -ENODEV;
120
Pu Wencc9690c2018-09-23 17:37:38 +0800121 pcidev = pci_get_device(pci_vendor,
Akshu Agrawal59a3b3a2018-01-18 15:51:30 +0530122 PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
123
124 if (!pcidev) {
Borislav Petkov108ec362019-03-30 12:20:22 +0100125 if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
Akshu Agrawal59a3b3a2018-01-18 15:51:30 +0530126 return -ENODEV;
127 }
Jacob Shin9c5320c2013-04-04 16:19:04 +0000128
129 if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
130 return -ENODEV;
131
132 if (!(val >> CLASS_CODE_SHIFT))
133 return -ENODEV;
134
135 od_register_powersave_bias_handler(amd_powersave_bias_target,
136 POWERSAVE_BIAS_DEF);
137 return 0;
138}
139late_initcall(amd_freq_sensitivity_init);
140
141static void __exit amd_freq_sensitivity_exit(void)
142{
143 od_unregister_powersave_bias_handler();
144}
145module_exit(amd_freq_sensitivity_exit);
146
Lee Jones52fe0b12020-07-15 09:26:34 +0100147static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
Thomas Gleixnerb11d77f2020-03-24 14:51:51 +0100148 X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
Jacob Shin9c5320c2013-04-04 16:19:04 +0000149 {}
150};
151MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
152
153MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
154MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
155 "the ondemand governor.");
156MODULE_LICENSE("GPL");