14471a34fSViresh Kumar /*
24471a34fSViresh Kumar  * drivers/cpufreq/cpufreq_governor.h
34471a34fSViresh Kumar  *
44471a34fSViresh Kumar  * Header file for CPUFreq governors common code
54471a34fSViresh Kumar  *
64471a34fSViresh Kumar  * Copyright	(C) 2001 Russell King
74471a34fSViresh Kumar  *		(C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
84471a34fSViresh Kumar  *		(C) 2003 Jun Nakajima <jun.nakajima@intel.com>
94471a34fSViresh Kumar  *		(C) 2009 Alexander Clouter <alex@digriz.org.uk>
104471a34fSViresh Kumar  *		(c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
114471a34fSViresh Kumar  *
124471a34fSViresh Kumar  * This program is free software; you can redistribute it and/or modify
134471a34fSViresh Kumar  * it under the terms of the GNU General Public License version 2 as
144471a34fSViresh Kumar  * published by the Free Software Foundation.
154471a34fSViresh Kumar  */
164471a34fSViresh Kumar 
17beb0ff39SBorislav Petkov #ifndef _CPUFREQ_GOVERNOR_H
18beb0ff39SBorislav Petkov #define _CPUFREQ_GOVERNOR_H
194471a34fSViresh Kumar 
202dd3e724SRafael J. Wysocki #include <linux/atomic.h>
219be4fd2cSRafael J. Wysocki #include <linux/irq_work.h>
224471a34fSViresh Kumar #include <linux/cpufreq.h>
235ff0a268SViresh Kumar #include <linux/kernel_stat.h>
245ff0a268SViresh Kumar #include <linux/module.h>
254471a34fSViresh Kumar #include <linux/mutex.h>
264471a34fSViresh Kumar 
274471a34fSViresh Kumar /*
284471a34fSViresh Kumar  * The polling frequency depends on the capability of the processor. Default
294471a34fSViresh Kumar  * polling frequency is 1000 times the transition latency of the processor. The
30c4afc410SStratos Karafotis  * governor will work on any processor with transition latency <= 10ms, using
314471a34fSViresh Kumar  * appropriate sampling rate.
324471a34fSViresh Kumar  *
33c4afc410SStratos Karafotis  * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
34c4afc410SStratos Karafotis  * this governor will not work. All times here are in us (micro seconds).
354471a34fSViresh Kumar  */
364471a34fSViresh Kumar #define MIN_SAMPLING_RATE_RATIO			(2)
374471a34fSViresh Kumar #define LATENCY_MULTIPLIER			(1000)
3898104ee2SViresh Kumar #define MIN_LATENCY_MULTIPLIER			(20)
394471a34fSViresh Kumar #define TRANSITION_LATENCY_LIMIT		(10 * 1000 * 1000)
404471a34fSViresh Kumar 
414471a34fSViresh Kumar /* Ondemand Sampling types */
424471a34fSViresh Kumar enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
434471a34fSViresh Kumar 
444d5dcc42SViresh Kumar /* create helper routines */
454471a34fSViresh Kumar #define define_get_cpu_dbs_routines(_dbs_info)				\
46875b8508SViresh Kumar static struct cpu_dbs_info *get_cpu_cdbs(int cpu)			\
474471a34fSViresh Kumar {									\
484471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu).cdbs;				\
494471a34fSViresh Kumar }									\
504471a34fSViresh Kumar 									\
514471a34fSViresh Kumar static void *get_cpu_dbs_info_s(int cpu)				\
524471a34fSViresh Kumar {									\
534471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu);				\
544471a34fSViresh Kumar }
554471a34fSViresh Kumar 
564471a34fSViresh Kumar /*
574471a34fSViresh Kumar  * Abbreviations:
584471a34fSViresh Kumar  * dbs: used as a shortform for demand based switching It helps to keep variable
594471a34fSViresh Kumar  *	names smaller, simpler
604471a34fSViresh Kumar  * cdbs: common dbs
61e5dde92cSNamhyung Kim  * od_*: On-demand governor
624471a34fSViresh Kumar  * cs_*: Conservative governor
634471a34fSViresh Kumar  */
644471a34fSViresh Kumar 
65bc505475SRafael J. Wysocki /* Governor demand based switching data (per-policy or global). */
66bc505475SRafael J. Wysocki struct dbs_data {
67bc505475SRafael J. Wysocki 	int usage_count;
68bc505475SRafael J. Wysocki 	void *tuners;
69ff4b1789SViresh Kumar 	unsigned int min_sampling_rate;
70ff4b1789SViresh Kumar 	unsigned int ignore_nice_load;
71ff4b1789SViresh Kumar 	unsigned int sampling_rate;
72ff4b1789SViresh Kumar 	unsigned int sampling_down_factor;
73ff4b1789SViresh Kumar 	unsigned int up_threshold;
74c4435630SViresh Kumar 
75c4435630SViresh Kumar 	struct kobject kobj;
76c54df071SViresh Kumar 	struct list_head policy_dbs_list;
77c54df071SViresh Kumar 	/*
78c54df071SViresh Kumar 	 * Protect concurrent updates to governor tunables from sysfs,
79c54df071SViresh Kumar 	 * policy_dbs_list and usage_count.
80c54df071SViresh Kumar 	 */
81c4435630SViresh Kumar 	struct mutex mutex;
82bc505475SRafael J. Wysocki };
83bc505475SRafael J. Wysocki 
84c4435630SViresh Kumar /* Governor's specific attributes */
85c4435630SViresh Kumar struct dbs_data;
86c4435630SViresh Kumar struct governor_attr {
87c4435630SViresh Kumar 	struct attribute attr;
88c4435630SViresh Kumar 	ssize_t (*show)(struct dbs_data *dbs_data, char *buf);
89c4435630SViresh Kumar 	ssize_t (*store)(struct dbs_data *dbs_data, const char *buf,
90c4435630SViresh Kumar 			 size_t count);
91c4435630SViresh Kumar };
92c4435630SViresh Kumar 
93c4435630SViresh Kumar #define gov_show_one(_gov, file_name)					\
94c4435630SViresh Kumar static ssize_t show_##file_name						\
95c4435630SViresh Kumar (struct dbs_data *dbs_data, char *buf)					\
96c4435630SViresh Kumar {									\
97c4435630SViresh Kumar 	struct _gov##_dbs_tuners *tuners = dbs_data->tuners;		\
98c4435630SViresh Kumar 	return sprintf(buf, "%u\n", tuners->file_name);			\
99c4435630SViresh Kumar }
100c4435630SViresh Kumar 
101c4435630SViresh Kumar #define gov_show_one_common(file_name)					\
102c4435630SViresh Kumar static ssize_t show_##file_name						\
103c4435630SViresh Kumar (struct dbs_data *dbs_data, char *buf)					\
104c4435630SViresh Kumar {									\
105c4435630SViresh Kumar 	return sprintf(buf, "%u\n", dbs_data->file_name);		\
106c4435630SViresh Kumar }
107c4435630SViresh Kumar 
108c4435630SViresh Kumar #define gov_attr_ro(_name)						\
109c4435630SViresh Kumar static struct governor_attr _name =					\
110c4435630SViresh Kumar __ATTR(_name, 0444, show_##_name, NULL)
111c4435630SViresh Kumar 
112c4435630SViresh Kumar #define gov_attr_rw(_name)						\
113c4435630SViresh Kumar static struct governor_attr _name =					\
114c4435630SViresh Kumar __ATTR(_name, 0644, show_##_name, store_##_name)
115c4435630SViresh Kumar 
11644152cb8SViresh Kumar /* Common to all CPUs of a policy */
117e40e7b25SRafael J. Wysocki struct policy_dbs_info {
11844152cb8SViresh Kumar 	struct cpufreq_policy *policy;
11944152cb8SViresh Kumar 	/*
12070f43e5eSViresh Kumar 	 * Per policy mutex that serializes load evaluation from limit-change
12170f43e5eSViresh Kumar 	 * and work-handler.
12244152cb8SViresh Kumar 	 */
12344152cb8SViresh Kumar 	struct mutex timer_mutex;
12470f43e5eSViresh Kumar 
1259be4fd2cSRafael J. Wysocki 	u64 last_sample_time;
1269be4fd2cSRafael J. Wysocki 	s64 sample_delay_ns;
127686cc637SRafael J. Wysocki 	atomic_t work_count;
1289be4fd2cSRafael J. Wysocki 	struct irq_work irq_work;
12970f43e5eSViresh Kumar 	struct work_struct work;
130bc505475SRafael J. Wysocki 	/* dbs_data may be shared between multiple policy objects */
131bc505475SRafael J. Wysocki 	struct dbs_data *dbs_data;
132c54df071SViresh Kumar 	struct list_head list;
13344152cb8SViresh Kumar };
13444152cb8SViresh Kumar 
135e40e7b25SRafael J. Wysocki static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
1369be4fd2cSRafael J. Wysocki 					   unsigned int delay_us)
1379be4fd2cSRafael J. Wysocki {
138e40e7b25SRafael J. Wysocki 	policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
1399be4fd2cSRafael J. Wysocki }
1409be4fd2cSRafael J. Wysocki 
1414471a34fSViresh Kumar /* Per cpu structures */
142875b8508SViresh Kumar struct cpu_dbs_info {
1431e7586a1SViresh Kumar 	u64 prev_cpu_idle;
1441e7586a1SViresh Kumar 	u64 prev_cpu_wall;
1451e7586a1SViresh Kumar 	u64 prev_cpu_nice;
14618b46abdSSrivatsa S. Bhat 	/*
147c8ae481bSViresh Kumar 	 * Used to keep track of load in the previous interval. However, when
148c8ae481bSViresh Kumar 	 * explicitly set to zero, it is used as a flag to ensure that we copy
149c8ae481bSViresh Kumar 	 * the previous load to the current interval only once, upon the first
150c8ae481bSViresh Kumar 	 * wake-up from idle.
15118b46abdSSrivatsa S. Bhat 	 */
152c8ae481bSViresh Kumar 	unsigned int prev_load;
1539be4fd2cSRafael J. Wysocki 	struct update_util_data update_util;
154e40e7b25SRafael J. Wysocki 	struct policy_dbs_info *policy_dbs;
1554471a34fSViresh Kumar };
1564471a34fSViresh Kumar 
1574471a34fSViresh Kumar struct od_cpu_dbs_info_s {
158875b8508SViresh Kumar 	struct cpu_dbs_info cdbs;
1594471a34fSViresh Kumar 	struct cpufreq_frequency_table *freq_table;
1604471a34fSViresh Kumar 	unsigned int freq_lo;
1614471a34fSViresh Kumar 	unsigned int freq_lo_jiffies;
1624471a34fSViresh Kumar 	unsigned int freq_hi_jiffies;
1634471a34fSViresh Kumar 	unsigned int rate_mult;
1644471a34fSViresh Kumar 	unsigned int sample_type:1;
1654471a34fSViresh Kumar };
1664471a34fSViresh Kumar 
1674471a34fSViresh Kumar struct cs_cpu_dbs_info_s {
168875b8508SViresh Kumar 	struct cpu_dbs_info cdbs;
1694471a34fSViresh Kumar 	unsigned int down_skip;
1704471a34fSViresh Kumar 	unsigned int requested_freq;
1714471a34fSViresh Kumar };
1724471a34fSViresh Kumar 
173c4afc410SStratos Karafotis /* Per policy Governors sysfs tunables */
1744471a34fSViresh Kumar struct od_dbs_tuners {
1754471a34fSViresh Kumar 	unsigned int powersave_bias;
1764471a34fSViresh Kumar 	unsigned int io_is_busy;
1774471a34fSViresh Kumar };
1784471a34fSViresh Kumar 
1794471a34fSViresh Kumar struct cs_dbs_tuners {
1804471a34fSViresh Kumar 	unsigned int down_threshold;
1814471a34fSViresh Kumar 	unsigned int freq_step;
1824471a34fSViresh Kumar };
1834471a34fSViresh Kumar 
184c4afc410SStratos Karafotis /* Common Governor data across policies */
1857bdad34dSRafael J. Wysocki struct dbs_governor {
186af926185SRafael J. Wysocki 	struct cpufreq_governor gov;
187af926185SRafael J. Wysocki 
1884471a34fSViresh Kumar 	#define GOV_ONDEMAND		0
1894471a34fSViresh Kumar 	#define GOV_CONSERVATIVE	1
1904471a34fSViresh Kumar 	int governor;
191c4435630SViresh Kumar 	struct kobj_type kobj_type;
1924471a34fSViresh Kumar 
1930b981e70SViresh Kumar 	/*
1940b981e70SViresh Kumar 	 * Common data for platforms that don't set
1950b981e70SViresh Kumar 	 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
1960b981e70SViresh Kumar 	 */
1974d5dcc42SViresh Kumar 	struct dbs_data *gdbs_data;
1984471a34fSViresh Kumar 
199875b8508SViresh Kumar 	struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu);
2004471a34fSViresh Kumar 	void *(*get_cpu_dbs_info_s)(int cpu);
2019be4fd2cSRafael J. Wysocki 	unsigned int (*gov_dbs_timer)(struct cpufreq_policy *policy);
2024471a34fSViresh Kumar 	void (*gov_check_cpu)(int cpu, unsigned int load);
2038e0484d2SViresh Kumar 	int (*init)(struct dbs_data *dbs_data, bool notify);
2048e0484d2SViresh Kumar 	void (*exit)(struct dbs_data *dbs_data, bool notify);
2054471a34fSViresh Kumar 
2064471a34fSViresh Kumar 	/* Governor specific ops, see below */
2074471a34fSViresh Kumar 	void *gov_ops;
2084471a34fSViresh Kumar };
2094471a34fSViresh Kumar 
210ea59ee0dSRafael J. Wysocki static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
211ea59ee0dSRafael J. Wysocki {
212ea59ee0dSRafael J. Wysocki 	return container_of(policy->governor, struct dbs_governor, gov);
213ea59ee0dSRafael J. Wysocki }
214ea59ee0dSRafael J. Wysocki 
2154471a34fSViresh Kumar /* Governor specific ops, will be passed to dbs_data->gov_ops */
2164471a34fSViresh Kumar struct od_ops {
2174471a34fSViresh Kumar 	void (*powersave_bias_init_cpu)(int cpu);
2184471a34fSViresh Kumar 	unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
2194471a34fSViresh Kumar 			unsigned int freq_next, unsigned int relation);
2203a3e9e06SViresh Kumar 	void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
2214471a34fSViresh Kumar };
2224471a34fSViresh Kumar 
2234471a34fSViresh Kumar static inline int delay_for_sampling_rate(unsigned int sampling_rate)
2244471a34fSViresh Kumar {
2254471a34fSViresh Kumar 	int delay = usecs_to_jiffies(sampling_rate);
2264471a34fSViresh Kumar 
2274471a34fSViresh Kumar 	/* We want all CPUs to do sampling nearly on same jiffy */
2284471a34fSViresh Kumar 	if (num_online_cpus() > 1)
2294471a34fSViresh Kumar 		delay -= jiffies % delay;
2304471a34fSViresh Kumar 
2314471a34fSViresh Kumar 	return delay;
2324471a34fSViresh Kumar }
2334471a34fSViresh Kumar 
2342bb8d94fSRafael J. Wysocki extern struct mutex dbs_data_mutex;
2356f1e4efdSJane Li extern struct mutex cpufreq_governor_lock;
236d10b5eb5SRafael J. Wysocki void dbs_check_cpu(struct cpufreq_policy *policy);
237906a6e5aSRafael J. Wysocki int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event);
238fb30809eSJacob Shin void od_register_powersave_bias_handler(unsigned int (*f)
239fb30809eSJacob Shin 		(struct cpufreq_policy *, unsigned int, unsigned int),
240fb30809eSJacob Shin 		unsigned int powersave_bias);
241fb30809eSJacob Shin void od_unregister_powersave_bias_handler(void);
242beb0ff39SBorislav Petkov #endif /* _CPUFREQ_GOVERNOR_H */
243