cpufreq: governor: Be friendly towards latency-sensitive bursty workloads
[deliverable/linux.git] / drivers / cpufreq / cpufreq_governor.h
CommitLineData
4471a34f
VK
1/*
2 * drivers/cpufreq/cpufreq_governor.h
3 *
4 * Header file for CPUFreq governors common code
5 *
6 * Copyright (C) 2001 Russell King
7 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
beb0ff39
BP
17#ifndef _CPUFREQ_GOVERNOR_H
18#define _CPUFREQ_GOVERNOR_H
4471a34f 19
4471a34f 20#include <linux/cpufreq.h>
5ff0a268
VK
21#include <linux/kernel_stat.h>
22#include <linux/module.h>
4471a34f 23#include <linux/mutex.h>
4471a34f
VK
24
25/*
26 * The polling frequency depends on the capability of the processor. Default
27 * polling frequency is 1000 times the transition latency of the processor. The
c4afc410 28 * governor will work on any processor with transition latency <= 10ms, using
4471a34f
VK
29 * appropriate sampling rate.
30 *
c4afc410
SK
31 * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
32 * this governor will not work. All times here are in us (micro seconds).
4471a34f
VK
33 */
34#define MIN_SAMPLING_RATE_RATIO (2)
35#define LATENCY_MULTIPLIER (1000)
98104ee2 36#define MIN_LATENCY_MULTIPLIER (20)
4471a34f
VK
37#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
38
39/* Ondemand Sampling types */
40enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
41
4d5dcc42
VK
42/*
43 * Macro for creating governors sysfs routines
44 *
45 * - gov_sys: One governor instance per whole system
46 * - gov_pol: One governor instance per policy
47 */
48
49/* Create attributes */
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/* Create show/store routines */
75#define show_one(_gov, file_name) \
76static ssize_t show_##file_name##_gov_sys \
4471a34f
VK
77(struct kobject *kobj, struct attribute *attr, char *buf) \
78{ \
4d5dcc42
VK
79 struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
80 return sprintf(buf, "%u\n", tuners->file_name); \
81} \
82 \
bb176f7d 83static ssize_t show_##file_name##_gov_pol \
4d5dcc42
VK
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 \
bb176f7d 93(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
4d5dcc42
VK
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); \
4471a34f
VK
104}
105
4d5dcc42
VK
106#define show_store_one(_gov, file_name) \
107show_one(_gov, file_name); \
108store_one(_gov, file_name)
109
110/* create helper routines */
4471a34f
VK
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 * Abbreviations:
124 * dbs: used as a shortform for demand based switching It helps to keep variable
125 * names smaller, simpler
126 * cdbs: common dbs
e5dde92c 127 * od_*: On-demand governor
4471a34f
VK
128 * cs_*: Conservative governor
129 */
130
131/* Per cpu structures */
132struct cpu_dbs_common_info {
133 int cpu;
1e7586a1
VK
134 u64 prev_cpu_idle;
135 u64 prev_cpu_wall;
136 u64 prev_cpu_nice;
18b46abd
SB
137 unsigned int prev_load;
138 /*
139 * Flag to ensure that we copy the previous load only once, upon the
140 * first wake-up from idle.
141 */
142 bool copy_prev_load;
4471a34f
VK
143 struct cpufreq_policy *cur_policy;
144 struct delayed_work work;
145 /*
146 * percpu mutex that serializes governor limit change with gov_dbs_timer
147 * invocation. We do not want gov_dbs_timer to run when user is changing
148 * the governor or limits.
149 */
150 struct mutex timer_mutex;
da53d61e 151 ktime_t time_stamp;
4471a34f
VK
152};
153
154struct od_cpu_dbs_info_s {
155 struct cpu_dbs_common_info cdbs;
4471a34f
VK
156 struct cpufreq_frequency_table *freq_table;
157 unsigned int freq_lo;
158 unsigned int freq_lo_jiffies;
159 unsigned int freq_hi_jiffies;
160 unsigned int rate_mult;
161 unsigned int sample_type:1;
162};
163
164struct cs_cpu_dbs_info_s {
165 struct cpu_dbs_common_info cdbs;
166 unsigned int down_skip;
167 unsigned int requested_freq;
168 unsigned int enable:1;
169};
170
c4afc410 171/* Per policy Governors sysfs tunables */
4471a34f 172struct od_dbs_tuners {
6c4640c3 173 unsigned int ignore_nice_load;
4471a34f
VK
174 unsigned int sampling_rate;
175 unsigned int sampling_down_factor;
176 unsigned int up_threshold;
4471a34f
VK
177 unsigned int powersave_bias;
178 unsigned int io_is_busy;
179};
180
181struct cs_dbs_tuners {
6c4640c3 182 unsigned int ignore_nice_load;
4471a34f
VK
183 unsigned int sampling_rate;
184 unsigned int sampling_down_factor;
185 unsigned int up_threshold;
186 unsigned int down_threshold;
187 unsigned int freq_step;
188};
189
c4afc410 190/* Common Governor data across policies */
4d5dcc42
VK
191struct dbs_data;
192struct common_dbs_data {
4471a34f
VK
193 /* Common across governors */
194 #define GOV_ONDEMAND 0
195 #define GOV_CONSERVATIVE 1
196 int governor;
4d5dcc42
VK
197 struct attribute_group *attr_group_gov_sys; /* one governor - system */
198 struct attribute_group *attr_group_gov_pol; /* one governor - policy */
4471a34f 199
0b981e70
VK
200 /*
201 * Common data for platforms that don't set
202 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
203 */
4d5dcc42 204 struct dbs_data *gdbs_data;
4471a34f
VK
205
206 struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
207 void *(*get_cpu_dbs_info_s)(int cpu);
208 void (*gov_dbs_timer)(struct work_struct *work);
209 void (*gov_check_cpu)(int cpu, unsigned int load);
4d5dcc42
VK
210 int (*init)(struct dbs_data *dbs_data);
211 void (*exit)(struct dbs_data *dbs_data);
4471a34f
VK
212
213 /* Governor specific ops, see below */
214 void *gov_ops;
215};
216
c4afc410 217/* Governor Per policy data */
4d5dcc42
VK
218struct dbs_data {
219 struct common_dbs_data *cdata;
220 unsigned int min_sampling_rate;
a97c98ad 221 int usage_count;
4d5dcc42
VK
222 void *tuners;
223
224 /* dbs_mutex protects dbs_enable in governor start/stop */
225 struct mutex mutex;
226};
227
4471a34f
VK
228/* Governor specific ops, will be passed to dbs_data->gov_ops */
229struct od_ops {
4471a34f
VK
230 void (*powersave_bias_init_cpu)(int cpu);
231 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
232 unsigned int freq_next, unsigned int relation);
3a3e9e06 233 void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
4471a34f
VK
234};
235
236struct cs_ops {
237 struct notifier_block *notifier_block;
238};
239
240static inline int delay_for_sampling_rate(unsigned int sampling_rate)
241{
242 int delay = usecs_to_jiffies(sampling_rate);
243
244 /* We want all CPUs to do sampling nearly on same jiffy */
245 if (num_online_cpus() > 1)
246 delay -= jiffies % delay;
247
248 return delay;
249}
250
4d5dcc42
VK
251#define declare_show_sampling_rate_min(_gov) \
252static ssize_t show_sampling_rate_min_gov_sys \
253(struct kobject *kobj, struct attribute *attr, char *buf) \
254{ \
255 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
256 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
257} \
258 \
259static ssize_t show_sampling_rate_min_gov_pol \
260(struct cpufreq_policy *policy, char *buf) \
261{ \
262 struct dbs_data *dbs_data = policy->governor_data; \
263 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
264}
265
6f1e4efd
JL
266extern struct mutex cpufreq_governor_lock;
267
4471a34f 268void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
4447266b
VK
269bool need_load_eval(struct cpu_dbs_common_info *cdbs,
270 unsigned int sampling_rate);
4d5dcc42
VK
271int cpufreq_governor_dbs(struct cpufreq_policy *policy,
272 struct common_dbs_data *cdata, unsigned int event);
031299b3
VK
273void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
274 unsigned int delay, bool all_cpus);
fb30809e
JS
275void od_register_powersave_bias_handler(unsigned int (*f)
276 (struct cpufreq_policy *, unsigned int, unsigned int),
277 unsigned int powersave_bias);
278void od_unregister_powersave_bias_handler(void);
beb0ff39 279#endif /* _CPUFREQ_GOVERNOR_H */
This page took 0.114041 seconds and 5 git commands to generate.