1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _CPUFREQ_GOVERNOR_H
18#define _CPUFREQ_GOVERNOR_H
19
20#include <linux/cpufreq.h>
21#include <linux/kernel_stat.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24
25
26
27
28
29
30
31
32
33
34#define MIN_SAMPLING_RATE_RATIO (2)
35#define LATENCY_MULTIPLIER (1000)
36#define MIN_LATENCY_MULTIPLIER (20)
37#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
38
39
40enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
41
42
43
44
45
46
47
48
49
50#define gov_sys_attr_ro(_name) \
51static struct global_attr _name##_gov_sys = \
52__ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
53
54#define gov_sys_attr_rw(_name) \
55static struct global_attr _name##_gov_sys = \
56__ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
57
58#define gov_pol_attr_ro(_name) \
59static struct freq_attr _name##_gov_pol = \
60__ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
61
62#define gov_pol_attr_rw(_name) \
63static struct freq_attr _name##_gov_pol = \
64__ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
65
66#define gov_sys_pol_attr_rw(_name) \
67 gov_sys_attr_rw(_name); \
68 gov_pol_attr_rw(_name)
69
70#define gov_sys_pol_attr_ro(_name) \
71 gov_sys_attr_ro(_name); \
72 gov_pol_attr_ro(_name)
73
74
75#define show_one(_gov, file_name) \
76static ssize_t show_##file_name##_gov_sys \
77(struct kobject *kobj, struct attribute *attr, char *buf) \
78{ \
79 struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
80 return sprintf(buf, "%u\n", tuners->file_name); \
81} \
82 \
83static ssize_t show_##file_name##_gov_pol \
84(struct cpufreq_policy *policy, char *buf) \
85{ \
86 struct dbs_data *dbs_data = policy->governor_data; \
87 struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
88 return sprintf(buf, "%u\n", tuners->file_name); \
89}
90
91#define store_one(_gov, file_name) \
92static ssize_t store_##file_name##_gov_sys \
93(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
94{ \
95 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
96 return store_##file_name(dbs_data, buf, count); \
97} \
98 \
99static ssize_t store_##file_name##_gov_pol \
100(struct cpufreq_policy *policy, const char *buf, size_t count) \
101{ \
102 struct dbs_data *dbs_data = policy->governor_data; \
103 return store_##file_name(dbs_data, buf, count); \
104}
105
106#define show_store_one(_gov, file_name) \
107show_one(_gov, file_name); \
108store_one(_gov, file_name)
109
110
111#define define_get_cpu_dbs_routines(_dbs_info) \
112static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu) \
113{ \
114 return &per_cpu(_dbs_info, cpu).cdbs; \
115} \
116 \
117static void *get_cpu_dbs_info_s(int cpu) \
118{ \
119 return &per_cpu(_dbs_info, cpu); \
120}
121
122
123
124
125
126
127
128
129
130
131
132struct cpu_dbs_common_info {
133 int cpu;
134 u64 prev_cpu_idle;
135 u64 prev_cpu_wall;
136 u64 prev_cpu_nice;
137
138
139
140
141
142
143 unsigned int prev_load;
144 struct cpufreq_policy *cur_policy;
145 struct delayed_work work;
146
147
148
149
150
151 struct mutex timer_mutex;
152 ktime_t time_stamp;
153};
154
155struct od_cpu_dbs_info_s {
156 struct cpu_dbs_common_info cdbs;
157 struct cpufreq_frequency_table *freq_table;
158 unsigned int freq_lo;
159 unsigned int freq_lo_jiffies;
160 unsigned int freq_hi_jiffies;
161 unsigned int rate_mult;
162 unsigned int sample_type:1;
163};
164
165struct cs_cpu_dbs_info_s {
166 struct cpu_dbs_common_info cdbs;
167 unsigned int down_skip;
168 unsigned int requested_freq;
169 unsigned int enable:1;
170};
171
172
173struct od_dbs_tuners {
174 unsigned int ignore_nice_load;
175 unsigned int sampling_rate;
176 unsigned int sampling_down_factor;
177 unsigned int up_threshold;
178 unsigned int powersave_bias;
179 unsigned int io_is_busy;
180};
181
182struct cs_dbs_tuners {
183 unsigned int ignore_nice_load;
184 unsigned int sampling_rate;
185 unsigned int sampling_down_factor;
186 unsigned int up_threshold;
187 unsigned int down_threshold;
188 unsigned int freq_step;
189};
190
191
192struct dbs_data;
193struct common_dbs_data {
194
195 #define GOV_ONDEMAND 0
196 #define GOV_CONSERVATIVE 1
197 int governor;
198 struct attribute_group *attr_group_gov_sys;
199 struct attribute_group *attr_group_gov_pol;
200
201
202
203
204
205 struct dbs_data *gdbs_data;
206
207 struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
208 void *(*get_cpu_dbs_info_s)(int cpu);
209 void (*gov_dbs_timer)(struct work_struct *work);
210 void (*gov_check_cpu)(int cpu, unsigned int load);
211 int (*init)(struct dbs_data *dbs_data);
212 void (*exit)(struct dbs_data *dbs_data);
213
214
215 void *gov_ops;
216};
217
218
219struct dbs_data {
220 struct common_dbs_data *cdata;
221 unsigned int min_sampling_rate;
222 int usage_count;
223 void *tuners;
224
225
226 struct mutex mutex;
227};
228
229
230struct od_ops {
231 void (*powersave_bias_init_cpu)(int cpu);
232 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
233 unsigned int freq_next, unsigned int relation);
234 void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
235};
236
237struct cs_ops {
238 struct notifier_block *notifier_block;
239};
240
241static inline int delay_for_sampling_rate(unsigned int sampling_rate)
242{
243 int delay = usecs_to_jiffies(sampling_rate);
244
245
246 if (num_online_cpus() > 1)
247 delay -= jiffies % delay;
248
249 return delay;
250}
251
252#define declare_show_sampling_rate_min(_gov) \
253static ssize_t show_sampling_rate_min_gov_sys \
254(struct kobject *kobj, struct attribute *attr, char *buf) \
255{ \
256 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
257 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
258} \
259 \
260static ssize_t show_sampling_rate_min_gov_pol \
261(struct cpufreq_policy *policy, char *buf) \
262{ \
263 struct dbs_data *dbs_data = policy->governor_data; \
264 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
265}
266
267extern struct mutex cpufreq_governor_lock;
268
269void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
270bool need_load_eval(struct cpu_dbs_common_info *cdbs,
271 unsigned int sampling_rate);
272int cpufreq_governor_dbs(struct cpufreq_policy *policy,
273 struct common_dbs_data *cdata, unsigned int event);
274void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
275 unsigned int delay, bool all_cpus);
276void od_register_powersave_bias_handler(unsigned int (*f)
277 (struct cpufreq_policy *, unsigned int, unsigned int),
278 unsigned int powersave_bias);
279void od_unregister_powersave_bias_handler(void);
280#endif
281