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 
174471a34fSViresh Kumar #ifndef _CPUFREQ_GOVERNER_H
184471a34fSViresh Kumar #define _CPUFREQ_GOVERNER_H
194471a34fSViresh Kumar 
204471a34fSViresh Kumar #include <linux/cpufreq.h>
214471a34fSViresh Kumar #include <linux/kobject.h>
224471a34fSViresh Kumar #include <linux/mutex.h>
234471a34fSViresh Kumar #include <linux/workqueue.h>
244471a34fSViresh Kumar #include <linux/sysfs.h>
254471a34fSViresh Kumar 
264471a34fSViresh Kumar /*
274471a34fSViresh Kumar  * The polling frequency depends on the capability of the processor. Default
284471a34fSViresh Kumar  * polling frequency is 1000 times the transition latency of the processor. The
294471a34fSViresh Kumar  * governor will work on any processor with transition latency <= 10mS, using
304471a34fSViresh Kumar  * appropriate sampling rate.
314471a34fSViresh Kumar  *
324471a34fSViresh Kumar  * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
334471a34fSViresh Kumar  * this governor will not work. All times here are in uS.
344471a34fSViresh Kumar  */
354471a34fSViresh Kumar #define MIN_SAMPLING_RATE_RATIO			(2)
364471a34fSViresh Kumar #define LATENCY_MULTIPLIER			(1000)
3798104ee2SViresh Kumar #define MIN_LATENCY_MULTIPLIER			(20)
384471a34fSViresh Kumar #define TRANSITION_LATENCY_LIMIT		(10 * 1000 * 1000)
394471a34fSViresh Kumar 
404471a34fSViresh Kumar /* Ondemand Sampling types */
414471a34fSViresh Kumar enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
424471a34fSViresh Kumar 
434d5dcc42SViresh Kumar /*
444d5dcc42SViresh Kumar  * Macro for creating governors sysfs routines
454d5dcc42SViresh Kumar  *
464d5dcc42SViresh Kumar  * - gov_sys: One governor instance per whole system
474d5dcc42SViresh Kumar  * - gov_pol: One governor instance per policy
484d5dcc42SViresh Kumar  */
494d5dcc42SViresh Kumar 
504d5dcc42SViresh Kumar /* Create attributes */
514d5dcc42SViresh Kumar #define gov_sys_attr_ro(_name)						\
524d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys =				\
534d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
544d5dcc42SViresh Kumar 
554d5dcc42SViresh Kumar #define gov_sys_attr_rw(_name)						\
564d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys =				\
574d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
584d5dcc42SViresh Kumar 
594d5dcc42SViresh Kumar #define gov_pol_attr_ro(_name)						\
604d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol =				\
614d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
624d5dcc42SViresh Kumar 
634d5dcc42SViresh Kumar #define gov_pol_attr_rw(_name)						\
644d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol =				\
654d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
664d5dcc42SViresh Kumar 
674d5dcc42SViresh Kumar #define gov_sys_pol_attr_rw(_name)					\
684d5dcc42SViresh Kumar 	gov_sys_attr_rw(_name);						\
694d5dcc42SViresh Kumar 	gov_pol_attr_rw(_name)
704d5dcc42SViresh Kumar 
714d5dcc42SViresh Kumar #define gov_sys_pol_attr_ro(_name)					\
724d5dcc42SViresh Kumar 	gov_sys_attr_ro(_name);						\
734d5dcc42SViresh Kumar 	gov_pol_attr_ro(_name)
744d5dcc42SViresh Kumar 
754d5dcc42SViresh Kumar /* Create show/store routines */
764d5dcc42SViresh Kumar #define show_one(_gov, file_name)					\
774d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_sys				\
784471a34fSViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf)		\
794471a34fSViresh Kumar {									\
804d5dcc42SViresh Kumar 	struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
814d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", tuners->file_name);			\
824d5dcc42SViresh Kumar }									\
834d5dcc42SViresh Kumar 									\
844d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_pol					\
854d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf)				\
864d5dcc42SViresh Kumar {									\
874d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
884d5dcc42SViresh Kumar 	struct _gov##_dbs_tuners *tuners = dbs_data->tuners;		\
894d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", tuners->file_name);			\
904471a34fSViresh Kumar }
914471a34fSViresh Kumar 
924d5dcc42SViresh Kumar #define store_one(_gov, file_name)					\
934d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_sys				\
944d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count)	\
954d5dcc42SViresh Kumar {									\
964d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data;		\
974d5dcc42SViresh Kumar 	return store_##file_name(dbs_data, buf, count);			\
984d5dcc42SViresh Kumar }									\
994d5dcc42SViresh Kumar 									\
1004d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_pol				\
1014d5dcc42SViresh Kumar (struct cpufreq_policy *policy, const char *buf, size_t count)		\
1024d5dcc42SViresh Kumar {									\
1034d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
1044d5dcc42SViresh Kumar 	return store_##file_name(dbs_data, buf, count);			\
1054d5dcc42SViresh Kumar }
1064d5dcc42SViresh Kumar 
1074d5dcc42SViresh Kumar #define show_store_one(_gov, file_name)					\
1084d5dcc42SViresh Kumar show_one(_gov, file_name);						\
1094d5dcc42SViresh Kumar store_one(_gov, file_name)
1104d5dcc42SViresh Kumar 
1114d5dcc42SViresh Kumar /* create helper routines */
1124471a34fSViresh Kumar #define define_get_cpu_dbs_routines(_dbs_info)				\
1134471a34fSViresh Kumar static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu)		\
1144471a34fSViresh Kumar {									\
1154471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu).cdbs;				\
1164471a34fSViresh Kumar }									\
1174471a34fSViresh Kumar 									\
1184471a34fSViresh Kumar static void *get_cpu_dbs_info_s(int cpu)				\
1194471a34fSViresh Kumar {									\
1204471a34fSViresh Kumar 	return &per_cpu(_dbs_info, cpu);				\
1214471a34fSViresh Kumar }
1224471a34fSViresh Kumar 
1234471a34fSViresh Kumar /*
1244471a34fSViresh Kumar  * Abbreviations:
1254471a34fSViresh Kumar  * dbs: used as a shortform for demand based switching It helps to keep variable
1264471a34fSViresh Kumar  *	names smaller, simpler
1274471a34fSViresh Kumar  * cdbs: common dbs
128e5dde92cSNamhyung Kim  * od_*: On-demand governor
1294471a34fSViresh Kumar  * cs_*: Conservative governor
1304471a34fSViresh Kumar  */
1314471a34fSViresh Kumar 
1324471a34fSViresh Kumar /* Per cpu structures */
1334471a34fSViresh Kumar struct cpu_dbs_common_info {
1344471a34fSViresh Kumar 	int cpu;
1351e7586a1SViresh Kumar 	u64 prev_cpu_idle;
1361e7586a1SViresh Kumar 	u64 prev_cpu_wall;
1371e7586a1SViresh Kumar 	u64 prev_cpu_nice;
1384471a34fSViresh Kumar 	struct cpufreq_policy *cur_policy;
1394471a34fSViresh Kumar 	struct delayed_work work;
1404471a34fSViresh Kumar 	/*
1414471a34fSViresh Kumar 	 * percpu mutex that serializes governor limit change with gov_dbs_timer
1424471a34fSViresh Kumar 	 * invocation. We do not want gov_dbs_timer to run when user is changing
1434471a34fSViresh Kumar 	 * the governor or limits.
1444471a34fSViresh Kumar 	 */
1454471a34fSViresh Kumar 	struct mutex timer_mutex;
146da53d61eSFabio Baltieri 	ktime_t time_stamp;
1474471a34fSViresh Kumar };
1484471a34fSViresh Kumar 
1494471a34fSViresh Kumar struct od_cpu_dbs_info_s {
1504471a34fSViresh Kumar 	struct cpu_dbs_common_info cdbs;
1511e7586a1SViresh Kumar 	u64 prev_cpu_iowait;
1524471a34fSViresh Kumar 	struct cpufreq_frequency_table *freq_table;
1534471a34fSViresh Kumar 	unsigned int freq_lo;
1544471a34fSViresh Kumar 	unsigned int freq_lo_jiffies;
1554471a34fSViresh Kumar 	unsigned int freq_hi_jiffies;
1564471a34fSViresh Kumar 	unsigned int rate_mult;
1574471a34fSViresh Kumar 	unsigned int sample_type:1;
1584471a34fSViresh Kumar };
1594471a34fSViresh Kumar 
1604471a34fSViresh Kumar struct cs_cpu_dbs_info_s {
1614471a34fSViresh Kumar 	struct cpu_dbs_common_info cdbs;
1624471a34fSViresh Kumar 	unsigned int down_skip;
1634471a34fSViresh Kumar 	unsigned int requested_freq;
1644471a34fSViresh Kumar 	unsigned int enable:1;
1654471a34fSViresh Kumar };
1664471a34fSViresh Kumar 
1674d5dcc42SViresh Kumar /* Per policy Governers sysfs tunables */
1684471a34fSViresh Kumar struct od_dbs_tuners {
1694471a34fSViresh Kumar 	unsigned int ignore_nice;
1704471a34fSViresh Kumar 	unsigned int sampling_rate;
1714471a34fSViresh Kumar 	unsigned int sampling_down_factor;
1724471a34fSViresh Kumar 	unsigned int up_threshold;
1734bd4e428SStratos Karafotis 	unsigned int adj_up_threshold;
1744471a34fSViresh Kumar 	unsigned int powersave_bias;
1754471a34fSViresh Kumar 	unsigned int io_is_busy;
1764471a34fSViresh Kumar };
1774471a34fSViresh Kumar 
1784471a34fSViresh Kumar struct cs_dbs_tuners {
1794471a34fSViresh Kumar 	unsigned int ignore_nice;
1804471a34fSViresh Kumar 	unsigned int sampling_rate;
1814471a34fSViresh Kumar 	unsigned int sampling_down_factor;
1824471a34fSViresh Kumar 	unsigned int up_threshold;
1834471a34fSViresh Kumar 	unsigned int down_threshold;
1844471a34fSViresh Kumar 	unsigned int freq_step;
1854471a34fSViresh Kumar };
1864471a34fSViresh Kumar 
1874d5dcc42SViresh Kumar /* Common Governer data across policies */
1884d5dcc42SViresh Kumar struct dbs_data;
1894d5dcc42SViresh Kumar struct common_dbs_data {
1904471a34fSViresh Kumar 	/* Common across governors */
1914471a34fSViresh Kumar 	#define GOV_ONDEMAND		0
1924471a34fSViresh Kumar 	#define GOV_CONSERVATIVE	1
1934471a34fSViresh Kumar 	int governor;
1944d5dcc42SViresh Kumar 	struct attribute_group *attr_group_gov_sys; /* one governor - system */
1954d5dcc42SViresh Kumar 	struct attribute_group *attr_group_gov_pol; /* one governor - policy */
1964471a34fSViresh Kumar 
1974d5dcc42SViresh Kumar 	/* Common data for platforms that don't set have_governor_per_policy */
1984d5dcc42SViresh Kumar 	struct dbs_data *gdbs_data;
1994471a34fSViresh Kumar 
2004471a34fSViresh Kumar 	struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
2014471a34fSViresh Kumar 	void *(*get_cpu_dbs_info_s)(int cpu);
2024471a34fSViresh Kumar 	void (*gov_dbs_timer)(struct work_struct *work);
2034471a34fSViresh Kumar 	void (*gov_check_cpu)(int cpu, unsigned int load);
2044d5dcc42SViresh Kumar 	int (*init)(struct dbs_data *dbs_data);
2054d5dcc42SViresh Kumar 	void (*exit)(struct dbs_data *dbs_data);
2064471a34fSViresh Kumar 
2074471a34fSViresh Kumar 	/* Governor specific ops, see below */
2084471a34fSViresh Kumar 	void *gov_ops;
2094471a34fSViresh Kumar };
2104471a34fSViresh Kumar 
2114d5dcc42SViresh Kumar /* Governer Per policy data */
2124d5dcc42SViresh Kumar struct dbs_data {
2134d5dcc42SViresh Kumar 	struct common_dbs_data *cdata;
2144d5dcc42SViresh Kumar 	unsigned int min_sampling_rate;
2154d5dcc42SViresh Kumar 	void *tuners;
2164d5dcc42SViresh Kumar 
2174d5dcc42SViresh Kumar 	/* dbs_mutex protects dbs_enable in governor start/stop */
2184d5dcc42SViresh Kumar 	struct mutex mutex;
2194d5dcc42SViresh Kumar };
2204d5dcc42SViresh Kumar 
2214471a34fSViresh Kumar /* Governor specific ops, will be passed to dbs_data->gov_ops */
2224471a34fSViresh Kumar struct od_ops {
2234471a34fSViresh Kumar 	void (*powersave_bias_init_cpu)(int cpu);
2244471a34fSViresh Kumar 	unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
2254471a34fSViresh Kumar 			unsigned int freq_next, unsigned int relation);
2264471a34fSViresh Kumar 	void (*freq_increase)(struct cpufreq_policy *p, unsigned int freq);
2274471a34fSViresh Kumar };
2284471a34fSViresh Kumar 
2294471a34fSViresh Kumar struct cs_ops {
2304471a34fSViresh Kumar 	struct notifier_block *notifier_block;
2314471a34fSViresh Kumar };
2324471a34fSViresh Kumar 
2334471a34fSViresh Kumar static inline int delay_for_sampling_rate(unsigned int sampling_rate)
2344471a34fSViresh Kumar {
2354471a34fSViresh Kumar 	int delay = usecs_to_jiffies(sampling_rate);
2364471a34fSViresh Kumar 
2374471a34fSViresh Kumar 	/* We want all CPUs to do sampling nearly on same jiffy */
2384471a34fSViresh Kumar 	if (num_online_cpus() > 1)
2394471a34fSViresh Kumar 		delay -= jiffies % delay;
2404471a34fSViresh Kumar 
2414471a34fSViresh Kumar 	return delay;
2424471a34fSViresh Kumar }
2434471a34fSViresh Kumar 
2444d5dcc42SViresh Kumar #define declare_show_sampling_rate_min(_gov)				\
2454d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_sys				\
2464d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf)		\
2474d5dcc42SViresh Kumar {									\
2484d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data;		\
2494d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", dbs_data->min_sampling_rate);	\
2504d5dcc42SViresh Kumar }									\
2514d5dcc42SViresh Kumar 									\
2524d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_pol				\
2534d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf)				\
2544d5dcc42SViresh Kumar {									\
2554d5dcc42SViresh Kumar 	struct dbs_data *dbs_data = policy->governor_data;		\
2564d5dcc42SViresh Kumar 	return sprintf(buf, "%u\n", dbs_data->min_sampling_rate);	\
2574d5dcc42SViresh Kumar }
2584d5dcc42SViresh Kumar 
2591e7586a1SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall);
2604471a34fSViresh Kumar void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
2614447266bSViresh Kumar bool need_load_eval(struct cpu_dbs_common_info *cdbs,
2624447266bSViresh Kumar 		unsigned int sampling_rate);
2634d5dcc42SViresh Kumar int cpufreq_governor_dbs(struct cpufreq_policy *policy,
2644d5dcc42SViresh Kumar 		struct common_dbs_data *cdata, unsigned int event);
265031299b3SViresh Kumar void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
266031299b3SViresh Kumar 		unsigned int delay, bool all_cpus);
2674471a34fSViresh Kumar #endif /* _CPUFREQ_GOVERNER_H */
268