blob: 8e4a25f0730cd8b65792d3bfe72e0fb349680d79 [file] [log] [blame]
Viresh Kumar4471a342012-10-26 00:47:42 +02001/*
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
Borislav Petkovbeb0ff32013-04-02 12:26:15 +000017#ifndef _CPUFREQ_GOVERNOR_H
18#define _CPUFREQ_GOVERNOR_H
Viresh Kumar4471a342012-10-26 00:47:42 +020019
Viresh Kumar4471a342012-10-26 00:47:42 +020020#include <linux/cpufreq.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053021#include <linux/kernel_stat.h>
22#include <linux/module.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020023#include <linux/mutex.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020024
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
Stratos Karafotisc4afc412013-08-26 21:42:21 +030028 * governor will work on any processor with transition latency <= 10ms, using
Viresh Kumar4471a342012-10-26 00:47:42 +020029 * appropriate sampling rate.
30 *
Stratos Karafotisc4afc412013-08-26 21:42:21 +030031 * 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).
Viresh Kumar4471a342012-10-26 00:47:42 +020033 */
34#define MIN_SAMPLING_RATE_RATIO (2)
35#define LATENCY_MULTIPLIER (1000)
Viresh Kumar98104ee2013-02-26 15:07:24 +053036#define MIN_LATENCY_MULTIPLIER (20)
Viresh Kumar4471a342012-10-26 00:47:42 +020037#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
38
39/* Ondemand Sampling types */
40enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
41
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000042/*
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 \
Viresh Kumar4471a342012-10-26 00:47:42 +020077(struct kobject *kobj, struct attribute *attr, char *buf) \
78{ \
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000079 struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
80 return sprintf(buf, "%u\n", tuners->file_name); \
81} \
82 \
Viresh Kumarbb176f72013-06-19 14:19:33 +053083static ssize_t show_##file_name##_gov_pol \
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000084(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); \
Viresh Kumar4471a342012-10-26 00:47:42 +020089}
90
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000091#define store_one(_gov, file_name) \
92static ssize_t store_##file_name##_gov_sys \
Viresh Kumarbb176f72013-06-19 14:19:33 +053093(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000094{ \
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/* create helper routines */
Viresh Kumar4471a342012-10-26 00:47:42 +0200111#define define_get_cpu_dbs_routines(_dbs_info) \
Viresh Kumar875b8502015-06-19 17:18:03 +0530112static struct cpu_dbs_info *get_cpu_cdbs(int cpu) \
Viresh Kumar4471a342012-10-26 00:47:42 +0200113{ \
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
Namhyung Kime5dde922013-02-28 05:38:00 +0000127 * od_*: On-demand governor
Viresh Kumar4471a342012-10-26 00:47:42 +0200128 * cs_*: Conservative governor
129 */
130
Viresh Kumar44152cb2015-07-18 11:30:59 +0530131/* Common to all CPUs of a policy */
132struct cpu_common_dbs_info {
133 struct cpufreq_policy *policy;
134 /*
135 * percpu mutex that serializes governor limit change with gov_dbs_timer
136 * invocation. We do not want gov_dbs_timer to run when user is changing
137 * the governor or limits.
138 */
139 struct mutex timer_mutex;
140 ktime_t time_stamp;
141};
142
Viresh Kumar4471a342012-10-26 00:47:42 +0200143/* Per cpu structures */
Viresh Kumar875b8502015-06-19 17:18:03 +0530144struct cpu_dbs_info {
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200145 u64 prev_cpu_idle;
146 u64 prev_cpu_wall;
147 u64 prev_cpu_nice;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530148 /*
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530149 * 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
152 * wake-up from idle.
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530153 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530154 unsigned int prev_load;
Viresh Kumar386d46e2015-06-19 17:18:01 +0530155 struct delayed_work dwork;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530156 struct cpu_common_dbs_info *shared;
Viresh Kumar4471a342012-10-26 00:47:42 +0200157};
158
159struct od_cpu_dbs_info_s {
Viresh Kumar875b8502015-06-19 17:18:03 +0530160 struct cpu_dbs_info cdbs;
Viresh Kumar4471a342012-10-26 00:47:42 +0200161 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;
167};
168
169struct cs_cpu_dbs_info_s {
Viresh Kumar875b8502015-06-19 17:18:03 +0530170 struct cpu_dbs_info cdbs;
Viresh Kumar4471a342012-10-26 00:47:42 +0200171 unsigned int down_skip;
172 unsigned int requested_freq;
173 unsigned int enable:1;
174};
175
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300176/* Per policy Governors sysfs tunables */
Viresh Kumar4471a342012-10-26 00:47:42 +0200177struct od_dbs_tuners {
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530178 unsigned int ignore_nice_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200179 unsigned int sampling_rate;
180 unsigned int sampling_down_factor;
181 unsigned int up_threshold;
Viresh Kumar4471a342012-10-26 00:47:42 +0200182 unsigned int powersave_bias;
183 unsigned int io_is_busy;
184};
185
186struct cs_dbs_tuners {
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530187 unsigned int ignore_nice_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200188 unsigned int sampling_rate;
189 unsigned int sampling_down_factor;
190 unsigned int up_threshold;
191 unsigned int down_threshold;
192 unsigned int freq_step;
193};
194
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300195/* Common Governor data across policies */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000196struct dbs_data;
197struct common_dbs_data {
Viresh Kumar4471a342012-10-26 00:47:42 +0200198 /* Common across governors */
199 #define GOV_ONDEMAND 0
200 #define GOV_CONSERVATIVE 1
201 int governor;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000202 struct attribute_group *attr_group_gov_sys; /* one governor - system */
203 struct attribute_group *attr_group_gov_pol; /* one governor - policy */
Viresh Kumar4471a342012-10-26 00:47:42 +0200204
Viresh Kumar0b981e72013-10-02 14:13:18 +0530205 /*
206 * Common data for platforms that don't set
207 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
208 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000209 struct dbs_data *gdbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +0200210
Viresh Kumar875b8502015-06-19 17:18:03 +0530211 struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu);
Viresh Kumar4471a342012-10-26 00:47:42 +0200212 void *(*get_cpu_dbs_info_s)(int cpu);
213 void (*gov_dbs_timer)(struct work_struct *work);
214 void (*gov_check_cpu)(int cpu, unsigned int load);
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530215 int (*init)(struct dbs_data *dbs_data, bool notify);
216 void (*exit)(struct dbs_data *dbs_data, bool notify);
Viresh Kumar4471a342012-10-26 00:47:42 +0200217
218 /* Governor specific ops, see below */
219 void *gov_ops;
Viresh Kumar732b6d62015-06-03 15:57:13 +0530220
221 /*
222 * Protects governor's data (struct dbs_data and struct common_dbs_data)
223 */
224 struct mutex mutex;
Viresh Kumar4471a342012-10-26 00:47:42 +0200225};
226
Stratos Karafotisc4afc412013-08-26 21:42:21 +0300227/* Governor Per policy data */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000228struct dbs_data {
229 struct common_dbs_data *cdata;
230 unsigned int min_sampling_rate;
Viresh Kumara97c98a2013-04-30 14:32:17 +0000231 int usage_count;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000232 void *tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000233};
234
Viresh Kumar4471a342012-10-26 00:47:42 +0200235/* Governor specific ops, will be passed to dbs_data->gov_ops */
236struct od_ops {
Viresh Kumar4471a342012-10-26 00:47:42 +0200237 void (*powersave_bias_init_cpu)(int cpu);
238 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
239 unsigned int freq_next, unsigned int relation);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530240 void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
Viresh Kumar4471a342012-10-26 00:47:42 +0200241};
242
Viresh Kumar4471a342012-10-26 00:47:42 +0200243static inline int delay_for_sampling_rate(unsigned int sampling_rate)
244{
245 int delay = usecs_to_jiffies(sampling_rate);
246
247 /* We want all CPUs to do sampling nearly on same jiffy */
248 if (num_online_cpus() > 1)
249 delay -= jiffies % delay;
250
251 return delay;
252}
253
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000254#define declare_show_sampling_rate_min(_gov) \
255static ssize_t show_sampling_rate_min_gov_sys \
256(struct kobject *kobj, struct attribute *attr, char *buf) \
257{ \
258 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
259 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
260} \
261 \
262static ssize_t show_sampling_rate_min_gov_pol \
263(struct cpufreq_policy *policy, char *buf) \
264{ \
265 struct dbs_data *dbs_data = policy->governor_data; \
266 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
267}
268
Jane Li6f1e4ef2014-01-03 17:17:41 +0800269extern struct mutex cpufreq_governor_lock;
270
Viresh Kumar4471a342012-10-26 00:47:42 +0200271void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530272bool need_load_eval(struct cpu_common_dbs_info *shared,
273 unsigned int sampling_rate);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000274int cpufreq_governor_dbs(struct cpufreq_policy *policy,
275 struct common_dbs_data *cdata, unsigned int event);
Viresh Kumar031299b2013-02-27 12:24:03 +0530276void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
277 unsigned int delay, bool all_cpus);
Jacob Shinfb308092013-04-02 09:56:56 -0500278void od_register_powersave_bias_handler(unsigned int (*f)
279 (struct cpufreq_policy *, unsigned int, unsigned int),
280 unsigned int powersave_bias);
281void od_unregister_powersave_bias_handler(void);
Borislav Petkovbeb0ff32013-04-02 12:26:15 +0000282#endif /* _CPUFREQ_GOVERNOR_H */