cpufreq_conservative.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * drivers/cpufreq/cpufreq_conservative.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  6. * Jun Nakajima <jun.nakajima@intel.com>
  7. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/cpufreq.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/kobject.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/notifier.h>
  21. #include <linux/percpu-defs.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/types.h>
  24. #include "cpufreq_governor.h"
  25. /* Conservative governor macors */
  26. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  27. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  28. #define DEF_SAMPLING_DOWN_FACTOR (1)
  29. #define MAX_SAMPLING_DOWN_FACTOR (10)
  30. static struct dbs_data cs_dbs_data;
  31. static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info);
  32. static struct cs_dbs_tuners cs_tuners = {
  33. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  34. .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  35. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  36. .ignore_nice = 0,
  37. .freq_step = 5,
  38. };
  39. /*
  40. * Every sampling_rate, we check, if current idle time is less than 20%
  41. * (default), then we try to increase frequency Every sampling_rate *
  42. * sampling_down_factor, we check, if current idle time is more than 80%, then
  43. * we try to decrease frequency
  44. *
  45. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  46. * happens at minimum steps of 5% (default) of maximum frequency
  47. */
  48. static void cs_check_cpu(int cpu, unsigned int load)
  49. {
  50. struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
  51. struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
  52. unsigned int freq_target;
  53. /*
  54. * break out if we 'cannot' reduce the speed as the user might
  55. * want freq_step to be zero
  56. */
  57. if (cs_tuners.freq_step == 0)
  58. return;
  59. /* Check for frequency increase */
  60. if (load > cs_tuners.up_threshold) {
  61. dbs_info->down_skip = 0;
  62. /* if we are already at full speed then break out early */
  63. if (dbs_info->requested_freq == policy->max)
  64. return;
  65. freq_target = (cs_tuners.freq_step * policy->max) / 100;
  66. /* max freq cannot be less than 100. But who knows.... */
  67. if (unlikely(freq_target == 0))
  68. freq_target = 5;
  69. dbs_info->requested_freq += freq_target;
  70. if (dbs_info->requested_freq > policy->max)
  71. dbs_info->requested_freq = policy->max;
  72. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  73. CPUFREQ_RELATION_H);
  74. return;
  75. }
  76. /*
  77. * The optimal frequency is the frequency that is the lowest that can
  78. * support the current CPU usage without triggering the up policy. To be
  79. * safe, we focus 10 points under the threshold.
  80. */
  81. if (load < (cs_tuners.down_threshold - 10)) {
  82. freq_target = (cs_tuners.freq_step * policy->max) / 100;
  83. dbs_info->requested_freq -= freq_target;
  84. if (dbs_info->requested_freq < policy->min)
  85. dbs_info->requested_freq = policy->min;
  86. /*
  87. * if we cannot reduce the frequency anymore, break out early
  88. */
  89. if (policy->cur == policy->min)
  90. return;
  91. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  92. CPUFREQ_RELATION_H);
  93. return;
  94. }
  95. }
  96. static void cs_dbs_timer(struct work_struct *work)
  97. {
  98. struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
  99. struct cs_cpu_dbs_info_s, cdbs.work.work);
  100. unsigned int cpu = dbs_info->cdbs.cpu;
  101. int delay = delay_for_sampling_rate(cs_tuners.sampling_rate);
  102. mutex_lock(&dbs_info->cdbs.timer_mutex);
  103. dbs_check_cpu(&cs_dbs_data, cpu);
  104. schedule_delayed_work_on(cpu, &dbs_info->cdbs.work, delay);
  105. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  106. }
  107. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  108. void *data)
  109. {
  110. struct cpufreq_freqs *freq = data;
  111. struct cs_cpu_dbs_info_s *dbs_info =
  112. &per_cpu(cs_cpu_dbs_info, freq->cpu);
  113. struct cpufreq_policy *policy;
  114. if (!dbs_info->enable)
  115. return 0;
  116. policy = dbs_info->cdbs.cur_policy;
  117. /*
  118. * we only care if our internally tracked freq moves outside the 'valid'
  119. * ranges of freqency available to us otherwise we do not change it
  120. */
  121. if (dbs_info->requested_freq > policy->max
  122. || dbs_info->requested_freq < policy->min)
  123. dbs_info->requested_freq = freq->new;
  124. return 0;
  125. }
  126. /************************** sysfs interface ************************/
  127. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  128. struct attribute *attr, char *buf)
  129. {
  130. return sprintf(buf, "%u\n", cs_dbs_data.min_sampling_rate);
  131. }
  132. static ssize_t store_sampling_down_factor(struct kobject *a,
  133. struct attribute *b,
  134. const char *buf, size_t count)
  135. {
  136. unsigned int input;
  137. int ret;
  138. ret = sscanf(buf, "%u", &input);
  139. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  140. return -EINVAL;
  141. cs_tuners.sampling_down_factor = input;
  142. return count;
  143. }
  144. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  145. const char *buf, size_t count)
  146. {
  147. unsigned int input;
  148. int ret;
  149. ret = sscanf(buf, "%u", &input);
  150. if (ret != 1)
  151. return -EINVAL;
  152. cs_tuners.sampling_rate = max(input, cs_dbs_data.min_sampling_rate);
  153. return count;
  154. }
  155. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  156. const char *buf, size_t count)
  157. {
  158. unsigned int input;
  159. int ret;
  160. ret = sscanf(buf, "%u", &input);
  161. if (ret != 1 || input > 100 || input <= cs_tuners.down_threshold)
  162. return -EINVAL;
  163. cs_tuners.up_threshold = input;
  164. return count;
  165. }
  166. static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
  167. const char *buf, size_t count)
  168. {
  169. unsigned int input;
  170. int ret;
  171. ret = sscanf(buf, "%u", &input);
  172. /* cannot be lower than 11 otherwise freq will not fall */
  173. if (ret != 1 || input < 11 || input > 100 ||
  174. input >= cs_tuners.up_threshold)
  175. return -EINVAL;
  176. cs_tuners.down_threshold = input;
  177. return count;
  178. }
  179. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  180. const char *buf, size_t count)
  181. {
  182. unsigned int input, j;
  183. int ret;
  184. ret = sscanf(buf, "%u", &input);
  185. if (ret != 1)
  186. return -EINVAL;
  187. if (input > 1)
  188. input = 1;
  189. if (input == cs_tuners.ignore_nice) /* nothing to do */
  190. return count;
  191. cs_tuners.ignore_nice = input;
  192. /* we need to re-evaluate prev_cpu_idle */
  193. for_each_online_cpu(j) {
  194. struct cs_cpu_dbs_info_s *dbs_info;
  195. dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  196. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  197. &dbs_info->cdbs.prev_cpu_wall);
  198. if (cs_tuners.ignore_nice)
  199. dbs_info->cdbs.prev_cpu_nice =
  200. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  201. }
  202. return count;
  203. }
  204. static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
  205. const char *buf, size_t count)
  206. {
  207. unsigned int input;
  208. int ret;
  209. ret = sscanf(buf, "%u", &input);
  210. if (ret != 1)
  211. return -EINVAL;
  212. if (input > 100)
  213. input = 100;
  214. /*
  215. * no need to test here if freq_step is zero as the user might actually
  216. * want this, they would be crazy though :)
  217. */
  218. cs_tuners.freq_step = input;
  219. return count;
  220. }
  221. show_one(cs, sampling_rate, sampling_rate);
  222. show_one(cs, sampling_down_factor, sampling_down_factor);
  223. show_one(cs, up_threshold, up_threshold);
  224. show_one(cs, down_threshold, down_threshold);
  225. show_one(cs, ignore_nice_load, ignore_nice);
  226. show_one(cs, freq_step, freq_step);
  227. define_one_global_rw(sampling_rate);
  228. define_one_global_rw(sampling_down_factor);
  229. define_one_global_rw(up_threshold);
  230. define_one_global_rw(down_threshold);
  231. define_one_global_rw(ignore_nice_load);
  232. define_one_global_rw(freq_step);
  233. define_one_global_ro(sampling_rate_min);
  234. static struct attribute *dbs_attributes[] = {
  235. &sampling_rate_min.attr,
  236. &sampling_rate.attr,
  237. &sampling_down_factor.attr,
  238. &up_threshold.attr,
  239. &down_threshold.attr,
  240. &ignore_nice_load.attr,
  241. &freq_step.attr,
  242. NULL
  243. };
  244. static struct attribute_group cs_attr_group = {
  245. .attrs = dbs_attributes,
  246. .name = "conservative",
  247. };
  248. /************************** sysfs end ************************/
  249. define_get_cpu_dbs_routines(cs_cpu_dbs_info);
  250. static struct notifier_block cs_cpufreq_notifier_block = {
  251. .notifier_call = dbs_cpufreq_notifier,
  252. };
  253. static struct cs_ops cs_ops = {
  254. .notifier_block = &cs_cpufreq_notifier_block,
  255. };
  256. static struct dbs_data cs_dbs_data = {
  257. .governor = GOV_CONSERVATIVE,
  258. .attr_group = &cs_attr_group,
  259. .tuners = &cs_tuners,
  260. .get_cpu_cdbs = get_cpu_cdbs,
  261. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  262. .gov_dbs_timer = cs_dbs_timer,
  263. .gov_check_cpu = cs_check_cpu,
  264. .gov_ops = &cs_ops,
  265. };
  266. static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  267. unsigned int event)
  268. {
  269. return cpufreq_governor_dbs(&cs_dbs_data, policy, event);
  270. }
  271. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  272. static
  273. #endif
  274. struct cpufreq_governor cpufreq_gov_conservative = {
  275. .name = "conservative",
  276. .governor = cs_cpufreq_governor_dbs,
  277. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  278. .owner = THIS_MODULE,
  279. };
  280. static int __init cpufreq_gov_dbs_init(void)
  281. {
  282. mutex_init(&cs_dbs_data.mutex);
  283. return cpufreq_register_governor(&cpufreq_gov_conservative);
  284. }
  285. static void __exit cpufreq_gov_dbs_exit(void)
  286. {
  287. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  288. }
  289. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  290. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  291. "Low Latency Frequency Transition capable processors "
  292. "optimised for use in a battery environment");
  293. MODULE_LICENSE("GPL");
  294. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  295. fs_initcall(cpufreq_gov_dbs_init);
  296. #else
  297. module_init(cpufreq_gov_dbs_init);
  298. #endif
  299. module_exit(cpufreq_gov_dbs_exit);