cpufreq_governor.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef _CPUFREQ_GOVERNOR_H
  17. #define _CPUFREQ_GOVERNOR_H
  18. #include <linux/cpufreq.h>
  19. #include <linux/kobject.h>
  20. #include <linux/mutex.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/sysfs.h>
  23. /*
  24. * The polling frequency depends on the capability of the processor. Default
  25. * polling frequency is 1000 times the transition latency of the processor. The
  26. * governor will work on any processor with transition latency <= 10mS, using
  27. * appropriate sampling rate.
  28. *
  29. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  30. * this governor will not work. All times here are in uS.
  31. */
  32. #define MIN_SAMPLING_RATE_RATIO (2)
  33. #define LATENCY_MULTIPLIER (1000)
  34. #define MIN_LATENCY_MULTIPLIER (20)
  35. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  36. /* Ondemand Sampling types */
  37. enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
  38. /*
  39. * Macro for creating governors sysfs routines
  40. *
  41. * - gov_sys: One governor instance per whole system
  42. * - gov_pol: One governor instance per policy
  43. */
  44. /* Create attributes */
  45. #define gov_sys_attr_ro(_name) \
  46. static struct global_attr _name##_gov_sys = \
  47. __ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
  48. #define gov_sys_attr_rw(_name) \
  49. static struct global_attr _name##_gov_sys = \
  50. __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
  51. #define gov_pol_attr_ro(_name) \
  52. static struct freq_attr _name##_gov_pol = \
  53. __ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
  54. #define gov_pol_attr_rw(_name) \
  55. static struct freq_attr _name##_gov_pol = \
  56. __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
  57. #define gov_sys_pol_attr_rw(_name) \
  58. gov_sys_attr_rw(_name); \
  59. gov_pol_attr_rw(_name)
  60. #define gov_sys_pol_attr_ro(_name) \
  61. gov_sys_attr_ro(_name); \
  62. gov_pol_attr_ro(_name)
  63. /* Create show/store routines */
  64. #define show_one(_gov, file_name) \
  65. static ssize_t show_##file_name##_gov_sys \
  66. (struct kobject *kobj, struct attribute *attr, char *buf) \
  67. { \
  68. struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
  69. return sprintf(buf, "%u\n", tuners->file_name); \
  70. } \
  71. \
  72. static ssize_t show_##file_name##_gov_pol \
  73. (struct cpufreq_policy *policy, char *buf) \
  74. { \
  75. struct dbs_data *dbs_data = policy->governor_data; \
  76. struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
  77. return sprintf(buf, "%u\n", tuners->file_name); \
  78. }
  79. #define store_one(_gov, file_name) \
  80. static ssize_t store_##file_name##_gov_sys \
  81. (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
  82. { \
  83. struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
  84. return store_##file_name(dbs_data, buf, count); \
  85. } \
  86. \
  87. static ssize_t store_##file_name##_gov_pol \
  88. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  89. { \
  90. struct dbs_data *dbs_data = policy->governor_data; \
  91. return store_##file_name(dbs_data, buf, count); \
  92. }
  93. #define show_store_one(_gov, file_name) \
  94. show_one(_gov, file_name); \
  95. store_one(_gov, file_name)
  96. /* create helper routines */
  97. #define define_get_cpu_dbs_routines(_dbs_info) \
  98. static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu) \
  99. { \
  100. return &per_cpu(_dbs_info, cpu).cdbs; \
  101. } \
  102. \
  103. static void *get_cpu_dbs_info_s(int cpu) \
  104. { \
  105. return &per_cpu(_dbs_info, cpu); \
  106. }
  107. /*
  108. * Abbreviations:
  109. * dbs: used as a shortform for demand based switching It helps to keep variable
  110. * names smaller, simpler
  111. * cdbs: common dbs
  112. * od_*: On-demand governor
  113. * cs_*: Conservative governor
  114. */
  115. /* Per cpu structures */
  116. struct cpu_dbs_common_info {
  117. int cpu;
  118. u64 prev_cpu_idle;
  119. u64 prev_cpu_wall;
  120. u64 prev_cpu_nice;
  121. struct cpufreq_policy *cur_policy;
  122. struct delayed_work work;
  123. /*
  124. * percpu mutex that serializes governor limit change with gov_dbs_timer
  125. * invocation. We do not want gov_dbs_timer to run when user is changing
  126. * the governor or limits.
  127. */
  128. struct mutex timer_mutex;
  129. ktime_t time_stamp;
  130. };
  131. struct od_cpu_dbs_info_s {
  132. struct cpu_dbs_common_info cdbs;
  133. struct cpufreq_frequency_table *freq_table;
  134. unsigned int freq_lo;
  135. unsigned int freq_lo_jiffies;
  136. unsigned int freq_hi_jiffies;
  137. unsigned int rate_mult;
  138. unsigned int sample_type:1;
  139. };
  140. struct cs_cpu_dbs_info_s {
  141. struct cpu_dbs_common_info cdbs;
  142. unsigned int down_skip;
  143. unsigned int requested_freq;
  144. unsigned int enable:1;
  145. };
  146. /* Per policy Governers sysfs tunables */
  147. struct od_dbs_tuners {
  148. unsigned int ignore_nice;
  149. unsigned int sampling_rate;
  150. unsigned int sampling_down_factor;
  151. unsigned int up_threshold;
  152. unsigned int adj_up_threshold;
  153. unsigned int powersave_bias;
  154. unsigned int io_is_busy;
  155. };
  156. struct cs_dbs_tuners {
  157. unsigned int ignore_nice;
  158. unsigned int sampling_rate;
  159. unsigned int sampling_down_factor;
  160. unsigned int up_threshold;
  161. unsigned int down_threshold;
  162. unsigned int freq_step;
  163. };
  164. /* Common Governer data across policies */
  165. struct dbs_data;
  166. struct common_dbs_data {
  167. /* Common across governors */
  168. #define GOV_ONDEMAND 0
  169. #define GOV_CONSERVATIVE 1
  170. int governor;
  171. struct attribute_group *attr_group_gov_sys; /* one governor - system */
  172. struct attribute_group *attr_group_gov_pol; /* one governor - policy */
  173. /* Common data for platforms that don't set have_governor_per_policy */
  174. struct dbs_data *gdbs_data;
  175. struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
  176. void *(*get_cpu_dbs_info_s)(int cpu);
  177. void (*gov_dbs_timer)(struct work_struct *work);
  178. void (*gov_check_cpu)(int cpu, unsigned int load);
  179. int (*init)(struct dbs_data *dbs_data);
  180. void (*exit)(struct dbs_data *dbs_data);
  181. /* Governor specific ops, see below */
  182. void *gov_ops;
  183. };
  184. /* Governer Per policy data */
  185. struct dbs_data {
  186. struct common_dbs_data *cdata;
  187. unsigned int min_sampling_rate;
  188. void *tuners;
  189. /* dbs_mutex protects dbs_enable in governor start/stop */
  190. struct mutex mutex;
  191. };
  192. /* Governor specific ops, will be passed to dbs_data->gov_ops */
  193. struct od_ops {
  194. void (*powersave_bias_init_cpu)(int cpu);
  195. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  196. unsigned int freq_next, unsigned int relation);
  197. void (*freq_increase)(struct cpufreq_policy *p, unsigned int freq);
  198. };
  199. struct cs_ops {
  200. struct notifier_block *notifier_block;
  201. };
  202. static inline int delay_for_sampling_rate(unsigned int sampling_rate)
  203. {
  204. int delay = usecs_to_jiffies(sampling_rate);
  205. /* We want all CPUs to do sampling nearly on same jiffy */
  206. if (num_online_cpus() > 1)
  207. delay -= jiffies % delay;
  208. return delay;
  209. }
  210. #define declare_show_sampling_rate_min(_gov) \
  211. static ssize_t show_sampling_rate_min_gov_sys \
  212. (struct kobject *kobj, struct attribute *attr, char *buf) \
  213. { \
  214. struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
  215. return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
  216. } \
  217. \
  218. static ssize_t show_sampling_rate_min_gov_pol \
  219. (struct cpufreq_policy *policy, char *buf) \
  220. { \
  221. struct dbs_data *dbs_data = policy->governor_data; \
  222. return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
  223. }
  224. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
  225. void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
  226. bool need_load_eval(struct cpu_dbs_common_info *cdbs,
  227. unsigned int sampling_rate);
  228. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  229. struct common_dbs_data *cdata, unsigned int event);
  230. void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
  231. unsigned int delay, bool all_cpus);
  232. void od_register_powersave_bias_handler(unsigned int (*f)
  233. (struct cpufreq_policy *, unsigned int, unsigned int),
  234. unsigned int powersave_bias);
  235. void od_unregister_powersave_bias_handler(void);
  236. #endif /* _CPUFREQ_GOVERNOR_H */