2 * drivers/cpufreq/cpufreq_governor.h
4 * Header file for CPUFreq governors common code
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>
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.
17 #ifndef _CPUFREQ_GOVERNOR_H
18 #define _CPUFREQ_GOVERNOR_H
20 #include <linux/cpufreq.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
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
28 * governor will work on any processor with transition latency <= 10ms, using
29 * appropriate sampling rate.
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).
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)
39 /* Ondemand Sampling types */
40 enum {OD_NORMAL_SAMPLE
, OD_SUB_SAMPLE
};
43 * Macro for creating governors sysfs routines
45 * - gov_sys: One governor instance per whole system
46 * - gov_pol: One governor instance per policy
49 /* Create attributes */
50 #define gov_sys_attr_ro(_name) \
51 static struct global_attr _name##_gov_sys = \
52 __ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
54 #define gov_sys_attr_rw(_name) \
55 static struct global_attr _name##_gov_sys = \
56 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
58 #define gov_pol_attr_ro(_name) \
59 static struct freq_attr _name##_gov_pol = \
60 __ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
62 #define gov_pol_attr_rw(_name) \
63 static struct freq_attr _name##_gov_pol = \
64 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
66 #define gov_sys_pol_attr_rw(_name) \
67 gov_sys_attr_rw(_name); \
68 gov_pol_attr_rw(_name)
70 #define gov_sys_pol_attr_ro(_name) \
71 gov_sys_attr_ro(_name); \
72 gov_pol_attr_ro(_name)
74 /* Create show/store routines */
75 #define show_one(_gov, file_name) \
76 static ssize_t show_##file_name##_gov_sys \
77 (struct kobject *kobj, struct attribute *attr, char *buf) \
79 struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
80 return sprintf(buf, "%u\n", tuners->file_name); \
83 static ssize_t show_##file_name##_gov_pol \
84 (struct cpufreq_policy *policy, char *buf) \
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); \
91 #define store_one(_gov, file_name) \
92 static ssize_t store_##file_name##_gov_sys \
93 (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
95 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
96 return store_##file_name(dbs_data, buf, count); \
99 static ssize_t store_##file_name##_gov_pol \
100 (struct cpufreq_policy *policy, const char *buf, size_t count) \
102 struct dbs_data *dbs_data = policy->governor_data; \
103 return store_##file_name(dbs_data, buf, count); \
106 #define show_store_one(_gov, file_name) \
107 show_one(_gov, file_name); \
108 store_one(_gov, file_name)
110 /* create helper routines */
111 #define define_get_cpu_dbs_routines(_dbs_info) \
112 static struct cpu_dbs_info *get_cpu_cdbs(int cpu) \
114 return &per_cpu(_dbs_info, cpu).cdbs; \
117 static void *get_cpu_dbs_info_s(int cpu) \
119 return &per_cpu(_dbs_info, cpu); \
124 * dbs: used as a shortform for demand based switching It helps to keep variable
125 * names smaller, simpler
127 * od_*: On-demand governor
128 * cs_*: Conservative governor
131 /* Common to all CPUs of a policy */
132 struct cpu_common_dbs_info
{
133 struct cpufreq_policy
*policy
;
135 * percpu mutex that serializes governor limit change with dbs_timer
136 * invocation. We do not want dbs_timer to run when user is changing
137 * the governor or limits.
139 struct mutex timer_mutex
;
143 /* Per cpu structures */
144 struct cpu_dbs_info
{
149 * Used to keep track of load in the previous interval. However, when
150 * explicitly set to zero, it is used as a flag to ensure that we copy
151 * the previous load to the current interval only once, upon the first
154 unsigned int prev_load
;
155 struct delayed_work dwork
;
156 struct cpu_common_dbs_info
*shared
;
159 struct od_cpu_dbs_info_s
{
160 struct cpu_dbs_info cdbs
;
161 struct cpufreq_frequency_table
*freq_table
;
162 unsigned int freq_lo
;
163 unsigned int freq_lo_jiffies
;
164 unsigned int freq_hi_jiffies
;
165 unsigned int rate_mult
;
166 unsigned int sample_type
:1;
169 struct cs_cpu_dbs_info_s
{
170 struct cpu_dbs_info cdbs
;
171 unsigned int down_skip
;
172 unsigned int requested_freq
;
175 /* Per policy Governors sysfs tunables */
176 struct od_dbs_tuners
{
177 unsigned int ignore_nice_load
;
178 unsigned int sampling_rate
;
179 unsigned int sampling_down_factor
;
180 unsigned int up_threshold
;
181 unsigned int powersave_bias
;
182 unsigned int io_is_busy
;
185 struct cs_dbs_tuners
{
186 unsigned int ignore_nice_load
;
187 unsigned int sampling_rate
;
188 unsigned int sampling_down_factor
;
189 unsigned int up_threshold
;
190 unsigned int down_threshold
;
191 unsigned int freq_step
;
194 /* Common Governor data across policies */
196 struct common_dbs_data
{
197 /* Common across governors */
198 #define GOV_ONDEMAND 0
199 #define GOV_CONSERVATIVE 1
201 struct attribute_group
*attr_group_gov_sys
; /* one governor - system */
202 struct attribute_group
*attr_group_gov_pol
; /* one governor - policy */
205 * Common data for platforms that don't set
206 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
208 struct dbs_data
*gdbs_data
;
210 struct cpu_dbs_info
*(*get_cpu_cdbs
)(int cpu
);
211 void *(*get_cpu_dbs_info_s
)(int cpu
);
212 unsigned int (*gov_dbs_timer
)(struct cpu_dbs_info
*cdbs
,
213 struct dbs_data
*dbs_data
,
215 void (*gov_check_cpu
)(int cpu
, unsigned int load
);
216 int (*init
)(struct dbs_data
*dbs_data
, bool notify
);
217 void (*exit
)(struct dbs_data
*dbs_data
, bool notify
);
219 /* Governor specific ops, see below */
223 * Protects governor's data (struct dbs_data and struct common_dbs_data)
228 /* Governor Per policy data */
230 struct common_dbs_data
*cdata
;
231 unsigned int min_sampling_rate
;
236 /* Governor specific ops, will be passed to dbs_data->gov_ops */
238 void (*powersave_bias_init_cpu
)(int cpu
);
239 unsigned int (*powersave_bias_target
)(struct cpufreq_policy
*policy
,
240 unsigned int freq_next
, unsigned int relation
);
241 void (*freq_increase
)(struct cpufreq_policy
*policy
, unsigned int freq
);
244 static inline int delay_for_sampling_rate(unsigned int sampling_rate
)
246 int delay
= usecs_to_jiffies(sampling_rate
);
248 /* We want all CPUs to do sampling nearly on same jiffy */
249 if (num_online_cpus() > 1)
250 delay
-= jiffies
% delay
;
255 #define declare_show_sampling_rate_min(_gov) \
256 static ssize_t show_sampling_rate_min_gov_sys \
257 (struct kobject *kobj, struct attribute *attr, char *buf) \
259 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
260 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
263 static ssize_t show_sampling_rate_min_gov_pol \
264 (struct cpufreq_policy *policy, char *buf) \
266 struct dbs_data *dbs_data = policy->governor_data; \
267 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
270 extern struct mutex cpufreq_governor_lock
;
272 void dbs_check_cpu(struct dbs_data
*dbs_data
, int cpu
);
273 int cpufreq_governor_dbs(struct cpufreq_policy
*policy
,
274 struct common_dbs_data
*cdata
, unsigned int event
);
275 void gov_queue_work(struct dbs_data
*dbs_data
, struct cpufreq_policy
*policy
,
276 unsigned int delay
, bool all_cpus
);
277 void od_register_powersave_bias_handler(unsigned int (*f
)
278 (struct cpufreq_policy
*, unsigned int, unsigned int),
279 unsigned int powersave_bias
);
280 void od_unregister_powersave_bias_handler(void);
281 #endif /* _CPUFREQ_GOVERNOR_H */