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