0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _CPUFREQ_GOVERNOR_H
0015 #define _CPUFREQ_GOVERNOR_H
0016
0017 #include <linux/atomic.h>
0018 #include <linux/irq_work.h>
0019 #include <linux/cpufreq.h>
0020 #include <linux/sched/cpufreq.h>
0021 #include <linux/kernel_stat.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024
0025
0026 enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 struct dbs_data {
0039 struct gov_attr_set attr_set;
0040 struct dbs_governor *gov;
0041 void *tuners;
0042 unsigned int ignore_nice_load;
0043 unsigned int sampling_rate;
0044 unsigned int sampling_down_factor;
0045 unsigned int up_threshold;
0046 unsigned int io_is_busy;
0047 };
0048
0049 static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
0050 {
0051 return container_of(attr_set, struct dbs_data, attr_set);
0052 }
0053
0054 #define gov_show_one(_gov, file_name) \
0055 static ssize_t file_name##_show \
0056 (struct gov_attr_set *attr_set, char *buf) \
0057 { \
0058 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
0059 struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
0060 return sprintf(buf, "%u\n", tuners->file_name); \
0061 }
0062
0063 #define gov_show_one_common(file_name) \
0064 static ssize_t file_name##_show \
0065 (struct gov_attr_set *attr_set, char *buf) \
0066 { \
0067 struct dbs_data *dbs_data = to_dbs_data(attr_set); \
0068 return sprintf(buf, "%u\n", dbs_data->file_name); \
0069 }
0070
0071 #define gov_attr_ro(_name) \
0072 static struct governor_attr _name = __ATTR_RO(_name)
0073
0074 #define gov_attr_rw(_name) \
0075 static struct governor_attr _name = __ATTR_RW(_name)
0076
0077
0078 struct policy_dbs_info {
0079 struct cpufreq_policy *policy;
0080
0081
0082
0083
0084 struct mutex update_mutex;
0085
0086 u64 last_sample_time;
0087 s64 sample_delay_ns;
0088 atomic_t work_count;
0089 struct irq_work irq_work;
0090 struct work_struct work;
0091
0092 struct dbs_data *dbs_data;
0093 struct list_head list;
0094
0095 unsigned int rate_mult;
0096 unsigned int idle_periods;
0097
0098 bool is_shared;
0099 bool work_in_progress;
0100 };
0101
0102 static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
0103 unsigned int delay_us)
0104 {
0105 policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
0106 }
0107
0108
0109 struct cpu_dbs_info {
0110 u64 prev_cpu_idle;
0111 u64 prev_update_time;
0112 u64 prev_cpu_nice;
0113
0114
0115
0116
0117
0118
0119 unsigned int prev_load;
0120 struct update_util_data update_util;
0121 struct policy_dbs_info *policy_dbs;
0122 };
0123
0124
0125 struct dbs_governor {
0126 struct cpufreq_governor gov;
0127 struct kobj_type kobj_type;
0128
0129
0130
0131
0132
0133 struct dbs_data *gdbs_data;
0134
0135 unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
0136 struct policy_dbs_info *(*alloc)(void);
0137 void (*free)(struct policy_dbs_info *policy_dbs);
0138 int (*init)(struct dbs_data *dbs_data);
0139 void (*exit)(struct dbs_data *dbs_data);
0140 void (*start)(struct cpufreq_policy *policy);
0141 };
0142
0143 static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
0144 {
0145 return container_of(policy->governor, struct dbs_governor, gov);
0146 }
0147
0148
0149 int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
0150 void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
0151 int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
0152 void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
0153 void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
0154
0155 #define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
0156 { \
0157 .name = _name_, \
0158 .flags = CPUFREQ_GOV_DYNAMIC_SWITCHING, \
0159 .owner = THIS_MODULE, \
0160 .init = cpufreq_dbs_governor_init, \
0161 .exit = cpufreq_dbs_governor_exit, \
0162 .start = cpufreq_dbs_governor_start, \
0163 .stop = cpufreq_dbs_governor_stop, \
0164 .limits = cpufreq_dbs_governor_limits, \
0165 }
0166
0167
0168 struct od_ops {
0169 unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
0170 unsigned int freq_next, unsigned int relation);
0171 };
0172
0173 unsigned int dbs_update(struct cpufreq_policy *policy);
0174 void od_register_powersave_bias_handler(unsigned int (*f)
0175 (struct cpufreq_policy *, unsigned int, unsigned int),
0176 unsigned int powersave_bias);
0177 void od_unregister_powersave_bias_handler(void);
0178 ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf,
0179 size_t count);
0180 void gov_update_cpu_data(struct dbs_data *dbs_data);
0181 #endif