Loading...
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * drivers/cpufreq/cpufreq_governor.h
4 *
5 * Header file for CPUFreq governors common code
6 *
7 * Copyright (C) 2001 Russell King
8 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
9 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
10 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
11 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
12 */
13
14#ifndef _CPUFREQ_GOVERNOR_H
15#define _CPUFREQ_GOVERNOR_H
16
17#include <linux/atomic.h>
18#include <linux/irq_work.h>
19#include <linux/cpufreq.h>
20#include <linux/sched/cpufreq.h>
21#include <linux/kernel_stat.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24
25/* Ondemand Sampling types */
26enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
27
28/*
29 * Abbreviations:
30 * dbs: used as a shortform for demand based switching It helps to keep variable
31 * names smaller, simpler
32 * cdbs: common dbs
33 * od_*: On-demand governor
34 * cs_*: Conservative governor
35 */
36
37/* Governor demand based switching data (per-policy or global). */
38struct dbs_data {
39 struct gov_attr_set attr_set;
40 struct dbs_governor *gov;
41 void *tuners;
42 unsigned int ignore_nice_load;
43 unsigned int sampling_rate;
44 unsigned int sampling_down_factor;
45 unsigned int up_threshold;
46 unsigned int io_is_busy;
47};
48
49static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
50{
51 return container_of(attr_set, struct dbs_data, attr_set);
52}
53
54#define gov_show_one(_gov, file_name) \
55static ssize_t file_name##_show \
56(struct gov_attr_set *attr_set, char *buf) \
57{ \
58 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
59 struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
60 return sprintf(buf, "%u\n", tuners->file_name); \
61}
62
63#define gov_show_one_common(file_name) \
64static ssize_t file_name##_show \
65(struct gov_attr_set *attr_set, char *buf) \
66{ \
67 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
68 return sprintf(buf, "%u\n", dbs_data->file_name); \
69}
70
71#define gov_attr_ro(_name) \
72static struct governor_attr _name = __ATTR_RO(_name)
73
74#define gov_attr_rw(_name) \
75static struct governor_attr _name = __ATTR_RW(_name)
76
77/* Common to all CPUs of a policy */
78struct policy_dbs_info {
79 struct cpufreq_policy *policy;
80 /*
81 * Per policy mutex that serializes load evaluation from limit-change
82 * and work-handler.
83 */
84 struct mutex update_mutex;
85
86 u64 last_sample_time;
87 s64 sample_delay_ns;
88 atomic_t work_count;
89 struct irq_work irq_work;
90 struct work_struct work;
91 /* dbs_data may be shared between multiple policy objects */
92 struct dbs_data *dbs_data;
93 struct list_head list;
94 /* Multiplier for increasing sample delay temporarily. */
95 unsigned int rate_mult;
96 unsigned int idle_periods; /* For conservative */
97 /* Status indicators */
98 bool is_shared; /* This object is used by multiple CPUs */
99 bool work_in_progress; /* Work is being queued up or in progress */
100};
101
102static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
103 unsigned int delay_us)
104{
105 policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
106}
107
108/* Per cpu structures */
109struct cpu_dbs_info {
110 u64 prev_cpu_idle;
111 u64 prev_update_time;
112 u64 prev_cpu_nice;
113 /*
114 * Used to keep track of load in the previous interval. However, when
115 * explicitly set to zero, it is used as a flag to ensure that we copy
116 * the previous load to the current interval only once, upon the first
117 * wake-up from idle.
118 */
119 unsigned int prev_load;
120 struct update_util_data update_util;
121 struct policy_dbs_info *policy_dbs;
122};
123
124/* Common Governor data across policies */
125struct dbs_governor {
126 struct cpufreq_governor gov;
127 struct kobj_type kobj_type;
128
129 /*
130 * Common data for platforms that don't set
131 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
132 */
133 struct dbs_data *gdbs_data;
134
135 unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
136 struct policy_dbs_info *(*alloc)(void);
137 void (*free)(struct policy_dbs_info *policy_dbs);
138 int (*init)(struct dbs_data *dbs_data);
139 void (*exit)(struct dbs_data *dbs_data);
140 void (*start)(struct cpufreq_policy *policy);
141};
142
143static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
144{
145 return container_of(policy->governor, struct dbs_governor, gov);
146}
147
148/* Governor callback routines */
149int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
150void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
151int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
152void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
153void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
154
155#define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
156 { \
157 .name = _name_, \
158 .flags = CPUFREQ_GOV_DYNAMIC_SWITCHING, \
159 .owner = THIS_MODULE, \
160 .init = cpufreq_dbs_governor_init, \
161 .exit = cpufreq_dbs_governor_exit, \
162 .start = cpufreq_dbs_governor_start, \
163 .stop = cpufreq_dbs_governor_stop, \
164 .limits = cpufreq_dbs_governor_limits, \
165 }
166
167/* Governor specific operations */
168struct od_ops {
169 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
170 unsigned int freq_next, unsigned int relation);
171};
172
173unsigned int dbs_update(struct cpufreq_policy *policy);
174void od_register_powersave_bias_handler(unsigned int (*f)
175 (struct cpufreq_policy *, unsigned int, unsigned int),
176 unsigned int powersave_bias);
177void od_unregister_powersave_bias_handler(void);
178ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf,
179 size_t count);
180void gov_update_cpu_data(struct dbs_data *dbs_data);
181#endif /* _CPUFREQ_GOVERNOR_H */
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
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 * 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.
30 *
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).
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/* Ondemand Sampling types */
40enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
41
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 \
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/* create helper routines */
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
127 * od_*: On-demand governor
128 * cs_*: Conservative governor
129 */
130
131/* Per cpu structures */
132struct cpu_dbs_common_info {
133 int cpu;
134 u64 prev_cpu_idle;
135 u64 prev_cpu_wall;
136 u64 prev_cpu_nice;
137 struct cpufreq_policy *cur_policy;
138 struct delayed_work work;
139 /*
140 * percpu mutex that serializes governor limit change with gov_dbs_timer
141 * invocation. We do not want gov_dbs_timer to run when user is changing
142 * the governor or limits.
143 */
144 struct mutex timer_mutex;
145 ktime_t time_stamp;
146};
147
148struct od_cpu_dbs_info_s {
149 struct cpu_dbs_common_info cdbs;
150 struct cpufreq_frequency_table *freq_table;
151 unsigned int freq_lo;
152 unsigned int freq_lo_jiffies;
153 unsigned int freq_hi_jiffies;
154 unsigned int rate_mult;
155 unsigned int sample_type:1;
156};
157
158struct cs_cpu_dbs_info_s {
159 struct cpu_dbs_common_info cdbs;
160 unsigned int down_skip;
161 unsigned int requested_freq;
162 unsigned int enable:1;
163};
164
165/* Per policy Governors sysfs tunables */
166struct od_dbs_tuners {
167 unsigned int ignore_nice_load;
168 unsigned int sampling_rate;
169 unsigned int sampling_down_factor;
170 unsigned int up_threshold;
171 unsigned int powersave_bias;
172 unsigned int io_is_busy;
173};
174
175struct cs_dbs_tuners {
176 unsigned int ignore_nice_load;
177 unsigned int sampling_rate;
178 unsigned int sampling_down_factor;
179 unsigned int up_threshold;
180 unsigned int down_threshold;
181 unsigned int freq_step;
182};
183
184/* Common Governor data across policies */
185struct dbs_data;
186struct common_dbs_data {
187 /* Common across governors */
188 #define GOV_ONDEMAND 0
189 #define GOV_CONSERVATIVE 1
190 int governor;
191 struct attribute_group *attr_group_gov_sys; /* one governor - system */
192 struct attribute_group *attr_group_gov_pol; /* one governor - policy */
193
194 /*
195 * Common data for platforms that don't set
196 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
197 */
198 struct dbs_data *gdbs_data;
199
200 struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
201 void *(*get_cpu_dbs_info_s)(int cpu);
202 void (*gov_dbs_timer)(struct work_struct *work);
203 void (*gov_check_cpu)(int cpu, unsigned int load);
204 int (*init)(struct dbs_data *dbs_data);
205 void (*exit)(struct dbs_data *dbs_data);
206
207 /* Governor specific ops, see below */
208 void *gov_ops;
209};
210
211/* Governor Per policy data */
212struct dbs_data {
213 struct common_dbs_data *cdata;
214 unsigned int min_sampling_rate;
215 int usage_count;
216 void *tuners;
217
218 /* dbs_mutex protects dbs_enable in governor start/stop */
219 struct mutex mutex;
220};
221
222/* Governor specific ops, will be passed to dbs_data->gov_ops */
223struct od_ops {
224 void (*powersave_bias_init_cpu)(int cpu);
225 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
226 unsigned int freq_next, unsigned int relation);
227 void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
228};
229
230struct cs_ops {
231 struct notifier_block *notifier_block;
232};
233
234static inline int delay_for_sampling_rate(unsigned int sampling_rate)
235{
236 int delay = usecs_to_jiffies(sampling_rate);
237
238 /* We want all CPUs to do sampling nearly on same jiffy */
239 if (num_online_cpus() > 1)
240 delay -= jiffies % delay;
241
242 return delay;
243}
244
245#define declare_show_sampling_rate_min(_gov) \
246static ssize_t show_sampling_rate_min_gov_sys \
247(struct kobject *kobj, struct attribute *attr, char *buf) \
248{ \
249 struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
250 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
251} \
252 \
253static ssize_t show_sampling_rate_min_gov_pol \
254(struct cpufreq_policy *policy, char *buf) \
255{ \
256 struct dbs_data *dbs_data = policy->governor_data; \
257 return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
258}
259
260extern struct mutex cpufreq_governor_lock;
261
262void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
263bool need_load_eval(struct cpu_dbs_common_info *cdbs,
264 unsigned int sampling_rate);
265int cpufreq_governor_dbs(struct cpufreq_policy *policy,
266 struct common_dbs_data *cdata, unsigned int event);
267void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
268 unsigned int delay, bool all_cpus);
269void od_register_powersave_bias_handler(unsigned int (*f)
270 (struct cpufreq_policy *, unsigned int, unsigned int),
271 unsigned int powersave_bias);
272void od_unregister_powersave_bias_handler(void);
273#endif /* _CPUFREQ_GOVERNOR_H */