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_GOVERNER_H
18 #define _CPUFREQ_GOVERNER_H
19 
20 #include <linux/cpufreq.h>
21 #include <linux/kobject.h>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
24 #include <linux/sysfs.h>
25 
26 /*
27  * The polling frequency depends on the capability of the processor. Default
28  * polling frequency is 1000 times the transition latency of the processor. The
29  * governor will work on any processor with transition latency <= 10mS, using
30  * appropriate sampling rate.
31  *
32  * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
33  * this governor will not work. All times here are in uS.
34  */
35 #define MIN_SAMPLING_RATE_RATIO			(2)
36 #define LATENCY_MULTIPLIER			(1000)
37 #define MIN_LATENCY_MULTIPLIER			(100)
38 #define TRANSITION_LATENCY_LIMIT		(10 * 1000 * 1000)
39 
40 /* Ondemand Sampling types */
41 enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
42 
43 /* Macro creating sysfs show routines */
44 #define show_one(_gov, file_name, object)				\
45 static ssize_t show_##file_name						\
46 (struct kobject *kobj, struct attribute *attr, char *buf)		\
47 {									\
48 	return sprintf(buf, "%u\n", _gov##_tuners.object);		\
49 }
50 
51 #define define_get_cpu_dbs_routines(_dbs_info)				\
52 static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu)		\
53 {									\
54 	return &per_cpu(_dbs_info, cpu).cdbs;				\
55 }									\
56 									\
57 static void *get_cpu_dbs_info_s(int cpu)				\
58 {									\
59 	return &per_cpu(_dbs_info, cpu);				\
60 }
61 
62 /*
63  * Abbreviations:
64  * dbs: used as a shortform for demand based switching It helps to keep variable
65  *	names smaller, simpler
66  * cdbs: common dbs
67  * on_*: On-demand governor
68  * cs_*: Conservative governor
69  */
70 
71 /* Per cpu structures */
72 struct cpu_dbs_common_info {
73 	int cpu;
74 	u64 prev_cpu_idle;
75 	u64 prev_cpu_wall;
76 	u64 prev_cpu_nice;
77 	struct cpufreq_policy *cur_policy;
78 	struct delayed_work work;
79 	/*
80 	 * percpu mutex that serializes governor limit change with gov_dbs_timer
81 	 * invocation. We do not want gov_dbs_timer to run when user is changing
82 	 * the governor or limits.
83 	 */
84 	struct mutex timer_mutex;
85 };
86 
87 struct od_cpu_dbs_info_s {
88 	struct cpu_dbs_common_info cdbs;
89 	u64 prev_cpu_iowait;
90 	struct cpufreq_frequency_table *freq_table;
91 	unsigned int freq_lo;
92 	unsigned int freq_lo_jiffies;
93 	unsigned int freq_hi_jiffies;
94 	unsigned int rate_mult;
95 	unsigned int sample_type:1;
96 };
97 
98 struct cs_cpu_dbs_info_s {
99 	struct cpu_dbs_common_info cdbs;
100 	unsigned int down_skip;
101 	unsigned int requested_freq;
102 	unsigned int enable:1;
103 };
104 
105 /* Governers sysfs tunables */
106 struct od_dbs_tuners {
107 	unsigned int ignore_nice;
108 	unsigned int sampling_rate;
109 	unsigned int sampling_down_factor;
110 	unsigned int up_threshold;
111 	unsigned int down_differential;
112 	unsigned int powersave_bias;
113 	unsigned int io_is_busy;
114 };
115 
116 struct cs_dbs_tuners {
117 	unsigned int ignore_nice;
118 	unsigned int sampling_rate;
119 	unsigned int sampling_down_factor;
120 	unsigned int up_threshold;
121 	unsigned int down_threshold;
122 	unsigned int freq_step;
123 };
124 
125 /* Per Governer data */
126 struct dbs_data {
127 	/* Common across governors */
128 	#define GOV_ONDEMAND		0
129 	#define GOV_CONSERVATIVE	1
130 	int governor;
131 	unsigned int min_sampling_rate;
132 	unsigned int enable; /* number of CPUs using this policy */
133 	struct attribute_group *attr_group;
134 	void *tuners;
135 
136 	/* dbs_mutex protects dbs_enable in governor start/stop */
137 	struct mutex mutex;
138 
139 	struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
140 	void *(*get_cpu_dbs_info_s)(int cpu);
141 	void (*gov_dbs_timer)(struct work_struct *work);
142 	void (*gov_check_cpu)(int cpu, unsigned int load);
143 
144 	/* Governor specific ops, see below */
145 	void *gov_ops;
146 };
147 
148 /* Governor specific ops, will be passed to dbs_data->gov_ops */
149 struct od_ops {
150 	int (*io_busy)(void);
151 	void (*powersave_bias_init_cpu)(int cpu);
152 	unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
153 			unsigned int freq_next, unsigned int relation);
154 	void (*freq_increase)(struct cpufreq_policy *p, unsigned int freq);
155 };
156 
157 struct cs_ops {
158 	struct notifier_block *notifier_block;
159 };
160 
161 static inline int delay_for_sampling_rate(unsigned int sampling_rate)
162 {
163 	int delay = usecs_to_jiffies(sampling_rate);
164 
165 	/* We want all CPUs to do sampling nearly on same jiffy */
166 	if (num_online_cpus() > 1)
167 		delay -= jiffies % delay;
168 
169 	return delay;
170 }
171 
172 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall);
173 void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
174 int cpufreq_governor_dbs(struct dbs_data *dbs_data,
175 		struct cpufreq_policy *policy, unsigned int event);
176 #endif /* _CPUFREQ_GOVERNER_H */
177