blob: 042023bbbf6214744070196ab5616abc58e81689 [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
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010024#include "cpufreq_ondemand.h"
Jacob Shin9c5320c2013-04-04 16:19:04 +000025
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;
Jacob Shin9c5320c2013-04-04 16:19:04 +000051
Viresh Kumar34ac5d72016-06-03 10:58:48 +053052 if (!policy->freq_table)
Jacob Shin9c5320c2013-04-04 16:19:04 +000053 return freq_next;
54
55 rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
56 &actual.l, &actual.h);
57 rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
58 &reference.l, &reference.h);
59 actual.h &= 0x00ffffff;
60 reference.h &= 0x00ffffff;
61
62 /* counter wrapped around, so stay on current frequency */
63 if (actual.q < data->actual || reference.q < data->reference) {
64 freq_next = policy->cur;
65 goto out;
66 }
67
68 d_actual = actual.q - data->actual;
69 d_reference = reference.q - data->reference;
70
71 /* divide by 0, so stay on current frequency as well */
72 if (d_reference == 0) {
73 freq_next = policy->cur;
74 goto out;
75 }
76
77 sensitivity = POWERSAVE_BIAS_MAX -
78 (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
79
80 clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
81
82 /* this workload is not CPU bound, so choose a lower freq */
83 if (sensitivity < od_tuners->powersave_bias) {
84 if (data->freq_prev == policy->cur)
85 freq_next = policy->cur;
86
87 if (freq_next > policy->cur)
88 freq_next = policy->cur;
89 else if (freq_next < policy->cur)
90 freq_next = policy->min;
91 else {
92 unsigned int index;
93
Viresh Kumar82577362016-06-27 09:59:34 +053094 index = cpufreq_table_find_index_h(policy,
95 policy->cur - 1);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053096 freq_next = policy->freq_table[index].frequency;
Jacob Shin9c5320c2013-04-04 16:19:04 +000097 }
98
99 data->freq_prev = freq_next;
100 } else
101 data->freq_prev = 0;
102
103out:
104 data->actual = actual.q;
105 data->reference = reference.q;
106 return freq_next;
107}
108
109static int __init amd_freq_sensitivity_init(void)
110{
111 u64 val;
112
113 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
114 return -ENODEV;
115
116 if (!static_cpu_has(X86_FEATURE_PROC_FEEDBACK))
117 return -ENODEV;
118
119 if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
120 return -ENODEV;
121
122 if (!(val >> CLASS_CODE_SHIFT))
123 return -ENODEV;
124
125 od_register_powersave_bias_handler(amd_powersave_bias_target,
126 POWERSAVE_BIAS_DEF);
127 return 0;
128}
129late_initcall(amd_freq_sensitivity_init);
130
131static void __exit amd_freq_sensitivity_exit(void)
132{
133 od_unregister_powersave_bias_handler();
134}
135module_exit(amd_freq_sensitivity_exit);
136
137static const struct x86_cpu_id amd_freq_sensitivity_ids[] = {
138 X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK),
139 {}
140};
141MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
142
143MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
144MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
145 "the ondemand governor.");
146MODULE_LICENSE("GPL");