cpufreq_governor.h 8.3 KB

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