cpufreq_conservative.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 macros */
  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 delayed_work *dw = to_delayed_work(work);
  99. struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
  100. struct cs_cpu_dbs_info_s, cdbs.work.work);
  101. unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
  102. struct cs_cpu_dbs_info_s *core_dbs_info = &per_cpu(cs_cpu_dbs_info,
  103. cpu);
  104. int delay = delay_for_sampling_rate(cs_tuners.sampling_rate);
  105. mutex_lock(&core_dbs_info->cdbs.timer_mutex);
  106. if (need_load_eval(&core_dbs_info->cdbs, cs_tuners.sampling_rate))
  107. dbs_check_cpu(&cs_dbs_data, cpu);
  108. schedule_delayed_work_on(smp_processor_id(), dw, delay);
  109. mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
  110. }
  111. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  112. void *data)
  113. {
  114. struct cpufreq_freqs *freq = data;
  115. struct cs_cpu_dbs_info_s *dbs_info =
  116. &per_cpu(cs_cpu_dbs_info, freq->cpu);
  117. struct cpufreq_policy *policy;
  118. if (!dbs_info->enable)
  119. return 0;
  120. policy = dbs_info->cdbs.cur_policy;
  121. /*
  122. * we only care if our internally tracked freq moves outside the 'valid'
  123. * ranges of frequency available to us otherwise we do not change it
  124. */
  125. if (dbs_info->requested_freq > policy->max
  126. || dbs_info->requested_freq < policy->min)
  127. dbs_info->requested_freq = freq->new;
  128. return 0;
  129. }
  130. /************************** sysfs interface ************************/
  131. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  132. struct attribute *attr, char *buf)
  133. {
  134. return sprintf(buf, "%u\n", cs_dbs_data.min_sampling_rate);
  135. }
  136. static ssize_t store_sampling_down_factor(struct kobject *a,
  137. struct attribute *b,
  138. const char *buf, size_t count)
  139. {
  140. unsigned int input;
  141. int ret;
  142. ret = sscanf(buf, "%u", &input);
  143. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  144. return -EINVAL;
  145. cs_tuners.sampling_down_factor = input;
  146. return count;
  147. }
  148. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  149. const char *buf, size_t count)
  150. {
  151. unsigned int input;
  152. int ret;
  153. ret = sscanf(buf, "%u", &input);
  154. if (ret != 1)
  155. return -EINVAL;
  156. cs_tuners.sampling_rate = max(input, cs_dbs_data.min_sampling_rate);
  157. return count;
  158. }
  159. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  160. const char *buf, size_t count)
  161. {
  162. unsigned int input;
  163. int ret;
  164. ret = sscanf(buf, "%u", &input);
  165. if (ret != 1 || input > 100 || input <= cs_tuners.down_threshold)
  166. return -EINVAL;
  167. cs_tuners.up_threshold = input;
  168. return count;
  169. }
  170. static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
  171. const char *buf, size_t count)
  172. {
  173. unsigned int input;
  174. int ret;
  175. ret = sscanf(buf, "%u", &input);
  176. /* cannot be lower than 11 otherwise freq will not fall */
  177. if (ret != 1 || input < 11 || input > 100 ||
  178. input >= cs_tuners.up_threshold)
  179. return -EINVAL;
  180. cs_tuners.down_threshold = input;
  181. return count;
  182. }
  183. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  184. const char *buf, size_t count)
  185. {
  186. unsigned int input, j;
  187. int ret;
  188. ret = sscanf(buf, "%u", &input);
  189. if (ret != 1)
  190. return -EINVAL;
  191. if (input > 1)
  192. input = 1;
  193. if (input == cs_tuners.ignore_nice) /* nothing to do */
  194. return count;
  195. cs_tuners.ignore_nice = input;
  196. /* we need to re-evaluate prev_cpu_idle */
  197. for_each_online_cpu(j) {
  198. struct cs_cpu_dbs_info_s *dbs_info;
  199. dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  200. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  201. &dbs_info->cdbs.prev_cpu_wall);
  202. if (cs_tuners.ignore_nice)
  203. dbs_info->cdbs.prev_cpu_nice =
  204. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  205. }
  206. return count;
  207. }
  208. static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
  209. const char *buf, size_t count)
  210. {
  211. unsigned int input;
  212. int ret;
  213. ret = sscanf(buf, "%u", &input);
  214. if (ret != 1)
  215. return -EINVAL;
  216. if (input > 100)
  217. input = 100;
  218. /*
  219. * no need to test here if freq_step is zero as the user might actually
  220. * want this, they would be crazy though :)
  221. */
  222. cs_tuners.freq_step = input;
  223. return count;
  224. }
  225. show_one(cs, sampling_rate, sampling_rate);
  226. show_one(cs, sampling_down_factor, sampling_down_factor);
  227. show_one(cs, up_threshold, up_threshold);
  228. show_one(cs, down_threshold, down_threshold);
  229. show_one(cs, ignore_nice_load, ignore_nice);
  230. show_one(cs, freq_step, freq_step);
  231. define_one_global_rw(sampling_rate);
  232. define_one_global_rw(sampling_down_factor);
  233. define_one_global_rw(up_threshold);
  234. define_one_global_rw(down_threshold);
  235. define_one_global_rw(ignore_nice_load);
  236. define_one_global_rw(freq_step);
  237. define_one_global_ro(sampling_rate_min);
  238. static struct attribute *dbs_attributes[] = {
  239. &sampling_rate_min.attr,
  240. &sampling_rate.attr,
  241. &sampling_down_factor.attr,
  242. &up_threshold.attr,
  243. &down_threshold.attr,
  244. &ignore_nice_load.attr,
  245. &freq_step.attr,
  246. NULL
  247. };
  248. static struct attribute_group cs_attr_group = {
  249. .attrs = dbs_attributes,
  250. .name = "conservative",
  251. };
  252. /************************** sysfs end ************************/
  253. define_get_cpu_dbs_routines(cs_cpu_dbs_info);
  254. static struct notifier_block cs_cpufreq_notifier_block = {
  255. .notifier_call = dbs_cpufreq_notifier,
  256. };
  257. static struct cs_ops cs_ops = {
  258. .notifier_block = &cs_cpufreq_notifier_block,
  259. };
  260. static struct dbs_data cs_dbs_data = {
  261. .governor = GOV_CONSERVATIVE,
  262. .attr_group = &cs_attr_group,
  263. .tuners = &cs_tuners,
  264. .get_cpu_cdbs = get_cpu_cdbs,
  265. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  266. .gov_dbs_timer = cs_dbs_timer,
  267. .gov_check_cpu = cs_check_cpu,
  268. .gov_ops = &cs_ops,
  269. };
  270. static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  271. unsigned int event)
  272. {
  273. return cpufreq_governor_dbs(&cs_dbs_data, policy, event);
  274. }
  275. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  276. static
  277. #endif
  278. struct cpufreq_governor cpufreq_gov_conservative = {
  279. .name = "conservative",
  280. .governor = cs_cpufreq_governor_dbs,
  281. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  282. .owner = THIS_MODULE,
  283. };
  284. static int __init cpufreq_gov_dbs_init(void)
  285. {
  286. mutex_init(&cs_dbs_data.mutex);
  287. return cpufreq_register_governor(&cpufreq_gov_conservative);
  288. }
  289. static void __exit cpufreq_gov_dbs_exit(void)
  290. {
  291. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  292. }
  293. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  294. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  295. "Low Latency Frequency Transition capable processors "
  296. "optimised for use in a battery environment");
  297. MODULE_LICENSE("GPL");
  298. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  299. fs_initcall(cpufreq_gov_dbs_init);
  300. #else
  301. module_init(cpufreq_gov_dbs_init);
  302. #endif
  303. module_exit(cpufreq_gov_dbs_exit);