cpufreq_governor.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 kobj_attribute _name##_gov_sys = \
  46. __ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
  47. #define gov_sys_attr_rw(_name) \
  48. static struct kobj_attribute _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 kobj_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 kobj_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_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. /* Common to all CPUs of a policy */
  115. struct cpu_common_dbs_info {
  116. struct cpufreq_policy *policy;
  117. /*
  118. * percpu mutex that serializes governor limit change with dbs_timer
  119. * invocation. We do not want dbs_timer to run when user is changing
  120. * the governor or limits.
  121. */
  122. struct mutex timer_mutex;
  123. ktime_t time_stamp;
  124. };
  125. /* Per cpu structures */
  126. struct cpu_dbs_info {
  127. u64 prev_cpu_idle;
  128. u64 prev_cpu_wall;
  129. u64 prev_cpu_nice;
  130. /*
  131. * Used to keep track of load in the previous interval. However, when
  132. * explicitly set to zero, it is used as a flag to ensure that we copy
  133. * the previous load to the current interval only once, upon the first
  134. * wake-up from idle.
  135. */
  136. unsigned int prev_load;
  137. struct delayed_work dwork;
  138. struct cpu_common_dbs_info *shared;
  139. };
  140. struct od_cpu_dbs_info_s {
  141. struct cpu_dbs_info cdbs;
  142. struct cpufreq_frequency_table *freq_table;
  143. unsigned int freq_lo;
  144. unsigned int freq_lo_jiffies;
  145. unsigned int freq_hi_jiffies;
  146. unsigned int rate_mult;
  147. unsigned int sample_type:1;
  148. };
  149. struct cs_cpu_dbs_info_s {
  150. struct cpu_dbs_info cdbs;
  151. unsigned int down_skip;
  152. unsigned int requested_freq;
  153. };
  154. /* Per policy Governors sysfs tunables */
  155. struct od_dbs_tuners {
  156. unsigned int ignore_nice_load;
  157. unsigned int sampling_rate;
  158. unsigned int sampling_down_factor;
  159. unsigned int up_threshold;
  160. unsigned int powersave_bias;
  161. unsigned int io_is_busy;
  162. };
  163. struct cs_dbs_tuners {
  164. unsigned int ignore_nice_load;
  165. unsigned int sampling_rate;
  166. unsigned int sampling_down_factor;
  167. unsigned int up_threshold;
  168. unsigned int down_threshold;
  169. unsigned int freq_step;
  170. };
  171. /* Common Governor data across policies */
  172. struct dbs_data;
  173. struct common_dbs_data {
  174. /* Common across governors */
  175. #define GOV_ONDEMAND 0
  176. #define GOV_CONSERVATIVE 1
  177. int governor;
  178. struct attribute_group *attr_group_gov_sys; /* one governor - system */
  179. struct attribute_group *attr_group_gov_pol; /* one governor - policy */
  180. /*
  181. * Common data for platforms that don't set
  182. * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
  183. */
  184. struct dbs_data *gdbs_data;
  185. struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu);
  186. void *(*get_cpu_dbs_info_s)(int cpu);
  187. unsigned int (*gov_dbs_timer)(struct cpu_dbs_info *cdbs,
  188. struct dbs_data *dbs_data,
  189. bool modify_all);
  190. void (*gov_check_cpu)(int cpu, unsigned int load);
  191. int (*init)(struct dbs_data *dbs_data, bool notify);
  192. void (*exit)(struct dbs_data *dbs_data, bool notify);
  193. /* Governor specific ops, see below */
  194. void *gov_ops;
  195. /*
  196. * Protects governor's data (struct dbs_data and struct common_dbs_data)
  197. */
  198. struct mutex mutex;
  199. };
  200. /* Governor Per policy data */
  201. struct dbs_data {
  202. struct common_dbs_data *cdata;
  203. unsigned int min_sampling_rate;
  204. int usage_count;
  205. void *tuners;
  206. };
  207. /* Governor specific ops, will be passed to dbs_data->gov_ops */
  208. struct od_ops {
  209. void (*powersave_bias_init_cpu)(int cpu);
  210. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  211. unsigned int freq_next, unsigned int relation);
  212. void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
  213. };
  214. static inline int delay_for_sampling_rate(unsigned int sampling_rate)
  215. {
  216. int delay = usecs_to_jiffies(sampling_rate);
  217. /* We want all CPUs to do sampling nearly on same jiffy */
  218. if (num_online_cpus() > 1)
  219. delay -= jiffies % delay;
  220. return delay;
  221. }
  222. #define declare_show_sampling_rate_min(_gov) \
  223. static ssize_t show_sampling_rate_min_gov_sys \
  224. (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
  225. { \
  226. struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
  227. return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
  228. } \
  229. \
  230. static ssize_t show_sampling_rate_min_gov_pol \
  231. (struct cpufreq_policy *policy, char *buf) \
  232. { \
  233. struct dbs_data *dbs_data = policy->governor_data; \
  234. return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
  235. }
  236. extern struct mutex cpufreq_governor_lock;
  237. void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
  238. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  239. struct common_dbs_data *cdata, unsigned int event);
  240. void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
  241. unsigned int delay, bool all_cpus);
  242. void od_register_powersave_bias_handler(unsigned int (*f)
  243. (struct cpufreq_policy *, unsigned int, unsigned int),
  244. unsigned int powersave_bias);
  245. void od_unregister_powersave_bias_handler(void);
  246. #endif /* _CPUFREQ_GOVERNOR_H */