blob: 82ae1002def1889ceedf2e17dc1f4628b929fea5 [file] [log] [blame]
Jacob Shin9c5320c2013-04-04 16:19:04 +00001/*
2 * amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
3 * for the ondemand governor.
4 *
5 * Copyright (C) 2013 Advanced Micro Devices, Inc.
6 *
7 * Author: Jacob Shin <jacob.shin@amd.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/percpu-defs.h>
18#include <linux/init.h>
19#include <linux/mod_devicetable.h>
20
21#include <asm/msr.h>
22#include <asm/cpufeature.h>
23
24#include "cpufreq_governor.h"
25
26#define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
27#define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
28#define CLASS_CODE_SHIFT 56
29#define POWERSAVE_BIAS_MAX 1000
30#define POWERSAVE_BIAS_DEF 400
31
32struct cpu_data_t {
33 u64 actual;
34 u64 reference;
35 unsigned int freq_prev;
36};
37
38static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
39
40static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
41 unsigned int freq_next,
42 unsigned int relation)
43{
44 int sensitivity;
45 long d_actual, d_reference;
46 struct msr actual, reference;
47 struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010048 struct policy_dbs_info *policy_dbs = policy->governor_data;
49 struct dbs_data *od_data = policy_dbs->dbs_data;
Jacob Shin9c5320c2013-04-04 16:19:04 +000050 struct od_dbs_tuners *od_tuners = od_data->tuners;
51 struct od_cpu_dbs_info_s *od_info =
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010052 dbs_governor_of(policy)->get_cpu_dbs_info_s(policy->cpu);
Jacob Shin9c5320c2013-04-04 16:19:04 +000053
54 if (!od_info->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 /* counter wrapped around, so stay on current frequency */
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 /* divide by 0, so stay on current frequency as well */
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 /* this workload is not CPU bound, so choose a lower freq */
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 cpufreq_frequency_table_target(policy,
97 od_info->freq_table, policy->cur - 1,
98 CPUFREQ_RELATION_H, &index);
99 freq_next = od_info->freq_table[index].frequency;
100 }
101
102 data->freq_prev = freq_next;
103 } else
104 data->freq_prev = 0;
105
106out:
107 data->actual = actual.q;
108 data->reference = reference.q;
109 return freq_next;
110}
111
112static int __init amd_freq_sensitivity_init(void)
113{
114 u64 val;
115
116 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
117 return -ENODEV;
118
119 if (!static_cpu_has(X86_FEATURE_PROC_FEEDBACK))
120 return -ENODEV;
121
122 if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
123 return -ENODEV;
124
125 if (!(val >> CLASS_CODE_SHIFT))
126 return -ENODEV;
127
128 od_register_powersave_bias_handler(amd_powersave_bias_target,
129 POWERSAVE_BIAS_DEF);
130 return 0;
131}
132late_initcall(amd_freq_sensitivity_init);
133
134static void __exit amd_freq_sensitivity_exit(void)
135{
136 od_unregister_powersave_bias_handler();
137}
138module_exit(amd_freq_sensitivity_exit);
139
140static const struct x86_cpu_id amd_freq_sensitivity_ids[] = {
141 X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK),
142 {}
143};
144MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
145
146MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
147MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
148 "the ondemand governor.");
149MODULE_LICENSE("GPL");