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 
204471a34fSViresh Kumar #include <linux/cpufreq.h>
215ff0a268SViresh Kumar #include <linux/kernel_stat.h>
225ff0a268SViresh Kumar #include <linux/module.h>
234471a34fSViresh Kumar #include <linux/mutex.h>
244471a34fSViresh Kumar 
254471a34fSViresh Kumar /*
264471a34fSViresh Kumar  * The polling frequency depends on the capability of the processor. Default
274471a34fSViresh Kumar  * polling frequency is 1000 times the transition latency of the processor. The
28c4afc410SStratos Karafotis  * governor will work on any processor with transition latency <= 10ms, using
294471a34fSViresh Kumar  * appropriate sampling rate.
304471a34fSViresh Kumar  *
31c4afc410SStratos Karafotis  * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
32c4afc410SStratos Karafotis  * this governor will not work. All times here are in us (micro seconds).
334471a34fSViresh Kumar  */
344471a34fSViresh Kumar #define MIN_SAMPLING_RATE_RATIO			(2)
354471a34fSViresh Kumar #define LATENCY_MULTIPLIER			(1000)
3698104ee2SViresh Kumar #define MIN_LATENCY_MULTIPLIER			(20)
374471a34fSViresh Kumar #define TRANSITION_LATENCY_LIMIT		(10 * 1000 * 1000)
384471a34fSViresh Kumar 
394471a34fSViresh Kumar /* Ondemand Sampling types */
404471a34fSViresh Kumar enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
414471a34fSViresh Kumar 
424d5dcc42SViresh Kumar /*
434d5dcc42SViresh Kumar  * Macro for creating governors sysfs routines
444d5dcc42SViresh Kumar  *
454d5dcc42SViresh Kumar  * - gov_sys: One governor instance per whole system
464d5dcc42SViresh Kumar  * - gov_pol: One governor instance per policy
474d5dcc42SViresh Kumar  */
484d5dcc42SViresh Kumar 
494d5dcc42SViresh Kumar /* Create attributes */
504d5dcc42SViresh Kumar #define gov_sys_attr_ro(_name)						\
514d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys =				\
524d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
534d5dcc42SViresh Kumar 
544d5dcc42SViresh Kumar #define gov_sys_attr_rw(_name)						\
554d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys =				\
564d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
574d5dcc42SViresh Kumar 
584d5dcc42SViresh Kumar #define gov_pol_attr_ro(_name)						\
594d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol =				\
604d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
614d5dcc42SViresh Kumar 
624d5dcc42SViresh Kumar #define gov_pol_attr_rw(_name)						\
634d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol =				\
644d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
654d5dcc42SViresh Kumar 
664d5dcc42SViresh Kumar #define gov_sys_pol_attr_rw(_name)					\
674d5dcc42SViresh Kumar 	gov_sys_attr_rw(_name);						\
684d5dcc42SViresh Kumar 	gov_pol_attr_rw(_name)
694d5dcc42SViresh Kumar 
704d5dcc42SViresh Kumar #define gov_sys_pol_attr_ro(_name)					\
714d5dcc42SViresh Kumar 	gov_sys_attr_ro(_name);						\
724d5dcc42SViresh Kumar 	gov_pol_attr_ro(_name)
734d5dcc42SViresh Kumar 
744d5dcc42SViresh Kumar /* Create show/store routines */
754d5dcc42SViresh Kumar #define show_one(_gov, file_name)					\
764d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_sys				\
774471a34fSViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf)		\
784471a34fSViresh Kumar {									\
794d5dcc42SViresh Kumar 	struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
804d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", tuners->file_name);			\
814d5dcc42SViresh Kumar }									\
824d5dcc42SViresh Kumar 									\
834d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_pol				\
844d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf)				\
854d5dcc42SViresh Kumar {									\
864d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
874d5dcc42SViresh Kumar 	struct _gov##_dbs_tuners *tuners = dbs_data->tuners;		\
884d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", tuners->file_name);			\
894471a34fSViresh Kumar }
904471a34fSViresh Kumar 
914d5dcc42SViresh Kumar #define store_one(_gov, file_name)					\
924d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_sys				\
934d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
944d5dcc42SViresh Kumar {									\
954d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data;		\
964d5dcc42SViresh Kumar 	return store_##file_name(dbs_data, buf, count);			\
974d5dcc42SViresh Kumar }									\
984d5dcc42SViresh Kumar 									\
994d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_pol				\
1004d5dcc42SViresh Kumar (struct cpufreq_policy *policy, const char *buf, size_t count)		\
1014d5dcc42SViresh Kumar {									\
1024d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
1034d5dcc42SViresh Kumar 	return store_##file_name(dbs_data, buf, count);			\
1044d5dcc42SViresh Kumar }
1054d5dcc42SViresh Kumar 
1064d5dcc42SViresh Kumar #define show_store_one(_gov, file_name)					\
1074d5dcc42SViresh Kumar show_one(_gov, file_name);						\
1084d5dcc42SViresh Kumar store_one(_gov, file_name)
1094d5dcc42SViresh Kumar 
1104d5dcc42SViresh Kumar /* create helper routines */
1114471a34fSViresh Kumar #define define_get_cpu_dbs_routines(_dbs_info)				\
112875b8508SViresh Kumar static struct cpu_dbs_info *get_cpu_cdbs(int cpu)			\
1134471a34fSViresh Kumar {									\
1144471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu).cdbs;				\
1154471a34fSViresh Kumar }									\
1164471a34fSViresh Kumar 									\
1174471a34fSViresh Kumar static void *get_cpu_dbs_info_s(int cpu)				\
1184471a34fSViresh Kumar {									\
1194471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu);				\
1204471a34fSViresh Kumar }
1214471a34fSViresh Kumar 
1224471a34fSViresh Kumar /*
1234471a34fSViresh Kumar  * Abbreviations:
1244471a34fSViresh Kumar  * dbs: used as a shortform for demand based switching It helps to keep variable
1254471a34fSViresh Kumar  *	names smaller, simpler
1264471a34fSViresh Kumar  * cdbs: common dbs
127e5dde92cSNamhyung Kim  * od_*: On-demand governor
1284471a34fSViresh Kumar  * cs_*: Conservative governor
1294471a34fSViresh Kumar  */
1304471a34fSViresh Kumar 
13144152cb8SViresh Kumar /* Common to all CPUs of a policy */
13244152cb8SViresh Kumar struct cpu_common_dbs_info {
13344152cb8SViresh Kumar 	struct cpufreq_policy *policy;
13444152cb8SViresh Kumar 	/*
13544152cb8SViresh Kumar 	 * percpu mutex that serializes governor limit change with gov_dbs_timer
13644152cb8SViresh Kumar 	 * invocation. We do not want gov_dbs_timer to run when user is changing
13744152cb8SViresh Kumar 	 * the governor or limits.
13844152cb8SViresh Kumar 	 */
13944152cb8SViresh Kumar 	struct mutex timer_mutex;
14044152cb8SViresh Kumar 	ktime_t time_stamp;
14144152cb8SViresh Kumar };
14244152cb8SViresh Kumar 
1434471a34fSViresh Kumar /* Per cpu structures */
144875b8508SViresh Kumar struct cpu_dbs_info {
1451e7586a1SViresh Kumar 	u64 prev_cpu_idle;
1461e7586a1SViresh Kumar 	u64 prev_cpu_wall;
1471e7586a1SViresh Kumar 	u64 prev_cpu_nice;
14818b46abdSSrivatsa S. Bhat 	/*
149c8ae481bSViresh Kumar 	 * Used to keep track of load in the previous interval. However, when
150c8ae481bSViresh Kumar 	 * explicitly set to zero, it is used as a flag to ensure that we copy
151c8ae481bSViresh Kumar 	 * the previous load to the current interval only once, upon the first
152c8ae481bSViresh Kumar 	 * wake-up from idle.
15318b46abdSSrivatsa S. Bhat 	 */
154c8ae481bSViresh Kumar 	unsigned int prev_load;
155386d46e6SViresh Kumar 	struct delayed_work dwork;
15644152cb8SViresh Kumar 	struct cpu_common_dbs_info *shared;
1574471a34fSViresh Kumar };
1584471a34fSViresh Kumar 
1594471a34fSViresh Kumar struct od_cpu_dbs_info_s {
160875b8508SViresh Kumar 	struct cpu_dbs_info cdbs;
1614471a34fSViresh Kumar 	struct cpufreq_frequency_table *freq_table;
1624471a34fSViresh Kumar 	unsigned int freq_lo;
1634471a34fSViresh Kumar 	unsigned int freq_lo_jiffies;
1644471a34fSViresh Kumar 	unsigned int freq_hi_jiffies;
1654471a34fSViresh Kumar 	unsigned int rate_mult;
1664471a34fSViresh Kumar 	unsigned int sample_type:1;
1674471a34fSViresh Kumar };
1684471a34fSViresh Kumar 
1694471a34fSViresh Kumar struct cs_cpu_dbs_info_s {
170875b8508SViresh Kumar 	struct cpu_dbs_info cdbs;
1714471a34fSViresh Kumar 	unsigned int down_skip;
1724471a34fSViresh Kumar 	unsigned int requested_freq;
1734471a34fSViresh Kumar 	unsigned int enable:1;
1744471a34fSViresh Kumar };
1754471a34fSViresh Kumar 
176c4afc410SStratos Karafotis /* Per policy Governors sysfs tunables */
1774471a34fSViresh Kumar struct od_dbs_tuners {
1786c4640c3SViresh Kumar 	unsigned int ignore_nice_load;
1794471a34fSViresh Kumar 	unsigned int sampling_rate;
1804471a34fSViresh Kumar 	unsigned int sampling_down_factor;
1814471a34fSViresh Kumar 	unsigned int up_threshold;
1824471a34fSViresh Kumar 	unsigned int powersave_bias;
1834471a34fSViresh Kumar 	unsigned int io_is_busy;
1844471a34fSViresh Kumar };
1854471a34fSViresh Kumar 
1864471a34fSViresh Kumar struct cs_dbs_tuners {
1876c4640c3SViresh Kumar 	unsigned int ignore_nice_load;
1884471a34fSViresh Kumar 	unsigned int sampling_rate;
1894471a34fSViresh Kumar 	unsigned int sampling_down_factor;
1904471a34fSViresh Kumar 	unsigned int up_threshold;
1914471a34fSViresh Kumar 	unsigned int down_threshold;
1924471a34fSViresh Kumar 	unsigned int freq_step;
1934471a34fSViresh Kumar };
1944471a34fSViresh Kumar 
195c4afc410SStratos Karafotis /* Common Governor data across policies */
1964d5dcc42SViresh Kumar struct dbs_data;
1974d5dcc42SViresh Kumar struct common_dbs_data {
1984471a34fSViresh Kumar 	/* Common across governors */
1994471a34fSViresh Kumar 	#define GOV_ONDEMAND		0
2004471a34fSViresh Kumar 	#define GOV_CONSERVATIVE	1
2014471a34fSViresh Kumar 	int governor;
2024d5dcc42SViresh Kumar 	struct attribute_group *attr_group_gov_sys; /* one governor - system */
2034d5dcc42SViresh Kumar 	struct attribute_group *attr_group_gov_pol; /* one governor - policy */
2044471a34fSViresh Kumar 
2050b981e70SViresh Kumar 	/*
2060b981e70SViresh Kumar 	 * Common data for platforms that don't set
2070b981e70SViresh Kumar 	 * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
2080b981e70SViresh Kumar 	 */
2094d5dcc42SViresh Kumar 	struct dbs_data *gdbs_data;
2104471a34fSViresh Kumar 
211875b8508SViresh Kumar 	struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu);
2124471a34fSViresh Kumar 	void *(*get_cpu_dbs_info_s)(int cpu);
2134471a34fSViresh Kumar 	void (*gov_dbs_timer)(struct work_struct *work);
2144471a34fSViresh Kumar 	void (*gov_check_cpu)(int cpu, unsigned int load);
2158e0484d2SViresh Kumar 	int (*init)(struct dbs_data *dbs_data, bool notify);
2168e0484d2SViresh Kumar 	void (*exit)(struct dbs_data *dbs_data, bool notify);
2174471a34fSViresh Kumar 
2184471a34fSViresh Kumar 	/* Governor specific ops, see below */
2194471a34fSViresh Kumar 	void *gov_ops;
220732b6d61SViresh Kumar 
221732b6d61SViresh Kumar 	/*
222732b6d61SViresh Kumar 	 * Protects governor's data (struct dbs_data and struct common_dbs_data)
223732b6d61SViresh Kumar 	 */
224732b6d61SViresh Kumar 	struct mutex mutex;
2254471a34fSViresh Kumar };
2264471a34fSViresh Kumar 
227c4afc410SStratos Karafotis /* Governor Per policy data */
2284d5dcc42SViresh Kumar struct dbs_data {
2294d5dcc42SViresh Kumar 	struct common_dbs_data *cdata;
2304d5dcc42SViresh Kumar 	unsigned int min_sampling_rate;
231a97c98adSViresh Kumar 	int usage_count;
2324d5dcc42SViresh Kumar 	void *tuners;
2334d5dcc42SViresh Kumar };
2344d5dcc42SViresh Kumar 
2354471a34fSViresh Kumar /* Governor specific ops, will be passed to dbs_data->gov_ops */
2364471a34fSViresh Kumar struct od_ops {
2374471a34fSViresh Kumar 	void (*powersave_bias_init_cpu)(int cpu);
2384471a34fSViresh Kumar 	unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
2394471a34fSViresh Kumar 			unsigned int freq_next, unsigned int relation);
2403a3e9e06SViresh Kumar 	void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
2414471a34fSViresh Kumar };
2424471a34fSViresh Kumar 
2434471a34fSViresh Kumar static inline int delay_for_sampling_rate(unsigned int sampling_rate)
2444471a34fSViresh Kumar {
2454471a34fSViresh Kumar 	int delay = usecs_to_jiffies(sampling_rate);
2464471a34fSViresh Kumar 
2474471a34fSViresh Kumar 	/* We want all CPUs to do sampling nearly on same jiffy */
2484471a34fSViresh Kumar 	if (num_online_cpus() > 1)
2494471a34fSViresh Kumar 		delay -= jiffies % delay;
2504471a34fSViresh Kumar 
2514471a34fSViresh Kumar 	return delay;
2524471a34fSViresh Kumar }
2534471a34fSViresh Kumar 
2544d5dcc42SViresh Kumar #define declare_show_sampling_rate_min(_gov)				\
2554d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_sys				\
2564d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf)		\
2574d5dcc42SViresh Kumar {									\
2584d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data;		\
2594d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", dbs_data->min_sampling_rate);	\
2604d5dcc42SViresh Kumar }									\
2614d5dcc42SViresh Kumar 									\
2624d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_pol				\
2634d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf)				\
2644d5dcc42SViresh Kumar {									\
2654d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
2664d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", dbs_data->min_sampling_rate);	\
2674d5dcc42SViresh Kumar }
2684d5dcc42SViresh Kumar 
2696f1e4efdSJane Li extern struct mutex cpufreq_governor_lock;
2706f1e4efdSJane Li 
2714471a34fSViresh Kumar void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
27244152cb8SViresh Kumar bool need_load_eval(struct cpu_common_dbs_info *shared,
27344152cb8SViresh Kumar 		    unsigned int sampling_rate);
2744d5dcc42SViresh Kumar int cpufreq_governor_dbs(struct cpufreq_policy *policy,
2754d5dcc42SViresh Kumar 		struct common_dbs_data *cdata, unsigned int event);
276031299b3SViresh Kumar void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
277031299b3SViresh Kumar 		unsigned int delay, bool all_cpus);
278fb30809eSJacob Shin void od_register_powersave_bias_handler(unsigned int (*f)
279fb30809eSJacob Shin 		(struct cpufreq_policy *, unsigned int, unsigned int),
280fb30809eSJacob Shin 		unsigned int powersave_bias);
281fb30809eSJacob Shin void od_unregister_powersave_bias_handler(void);
282beb0ff39SBorislav Petkov #endif /* _CPUFREQ_GOVERNOR_H */
283