cpufreq_conservative.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/slab.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/types.h>
  25. #include "cpufreq_governor.h"
  26. /* Conservative governor macros */
  27. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  28. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  29. #define DEF_SAMPLING_DOWN_FACTOR (1)
  30. #define MAX_SAMPLING_DOWN_FACTOR (10)
  31. static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info);
  32. /*
  33. * Every sampling_rate, we check, if current idle time is less than 20%
  34. * (default), then we try to increase frequency Every sampling_rate *
  35. * sampling_down_factor, we check, if current idle time is more than 80%, then
  36. * we try to decrease frequency
  37. *
  38. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  39. * happens at minimum steps of 5% (default) of maximum frequency
  40. */
  41. static void cs_check_cpu(int cpu, unsigned int load)
  42. {
  43. struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
  44. struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
  45. struct dbs_data *dbs_data = policy->governor_data;
  46. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  47. unsigned int freq_target;
  48. /*
  49. * break out if we 'cannot' reduce the speed as the user might
  50. * want freq_step to be zero
  51. */
  52. if (cs_tuners->freq_step == 0)
  53. return;
  54. /* Check for frequency increase */
  55. if (load > cs_tuners->up_threshold) {
  56. dbs_info->down_skip = 0;
  57. /* if we are already at full speed then break out early */
  58. if (dbs_info->requested_freq == policy->max)
  59. return;
  60. freq_target = (cs_tuners->freq_step * policy->max) / 100;
  61. /* max freq cannot be less than 100. But who knows.... */
  62. if (unlikely(freq_target == 0))
  63. freq_target = 5;
  64. dbs_info->requested_freq += freq_target;
  65. if (dbs_info->requested_freq > policy->max)
  66. dbs_info->requested_freq = policy->max;
  67. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  68. CPUFREQ_RELATION_H);
  69. return;
  70. }
  71. /*
  72. * The optimal frequency is the frequency that is the lowest that can
  73. * support the current CPU usage without triggering the up policy. To be
  74. * safe, we focus 10 points under the threshold.
  75. */
  76. if (load < (cs_tuners->down_threshold - 10)) {
  77. freq_target = (cs_tuners->freq_step * policy->max) / 100;
  78. dbs_info->requested_freq -= freq_target;
  79. if (dbs_info->requested_freq < policy->min)
  80. dbs_info->requested_freq = policy->min;
  81. /*
  82. * if we cannot reduce the frequency anymore, break out early
  83. */
  84. if (policy->cur == policy->min)
  85. return;
  86. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  87. CPUFREQ_RELATION_H);
  88. return;
  89. }
  90. }
  91. static void cs_dbs_timer(struct work_struct *work)
  92. {
  93. struct delayed_work *dw = to_delayed_work(work);
  94. struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
  95. struct cs_cpu_dbs_info_s, cdbs.work.work);
  96. unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
  97. struct cs_cpu_dbs_info_s *core_dbs_info = &per_cpu(cs_cpu_dbs_info,
  98. cpu);
  99. struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
  100. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  101. int delay = delay_for_sampling_rate(cs_tuners->sampling_rate);
  102. mutex_lock(&core_dbs_info->cdbs.timer_mutex);
  103. if (need_load_eval(&core_dbs_info->cdbs, cs_tuners->sampling_rate))
  104. dbs_check_cpu(dbs_data, cpu);
  105. schedule_delayed_work_on(smp_processor_id(), dw, delay);
  106. mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
  107. }
  108. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  109. void *data)
  110. {
  111. struct cpufreq_freqs *freq = data;
  112. struct cs_cpu_dbs_info_s *dbs_info =
  113. &per_cpu(cs_cpu_dbs_info, freq->cpu);
  114. struct cpufreq_policy *policy;
  115. if (!dbs_info->enable)
  116. return 0;
  117. policy = dbs_info->cdbs.cur_policy;
  118. /*
  119. * we only care if our internally tracked freq moves outside the 'valid'
  120. * ranges of frequency available to us otherwise we do not change it
  121. */
  122. if (dbs_info->requested_freq > policy->max
  123. || dbs_info->requested_freq < policy->min)
  124. dbs_info->requested_freq = freq->new;
  125. return 0;
  126. }
  127. /************************** sysfs interface ************************/
  128. static struct common_dbs_data cs_dbs_cdata;
  129. static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
  130. const char *buf, size_t count)
  131. {
  132. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  133. unsigned int input;
  134. int ret;
  135. ret = sscanf(buf, "%u", &input);
  136. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  137. return -EINVAL;
  138. cs_tuners->sampling_down_factor = input;
  139. return count;
  140. }
  141. static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
  142. size_t count)
  143. {
  144. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  145. unsigned int input;
  146. int ret;
  147. ret = sscanf(buf, "%u", &input);
  148. if (ret != 1)
  149. return -EINVAL;
  150. cs_tuners->sampling_rate = max(input, dbs_data->min_sampling_rate);
  151. return count;
  152. }
  153. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  154. size_t count)
  155. {
  156. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  157. unsigned int input;
  158. int ret;
  159. ret = sscanf(buf, "%u", &input);
  160. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  161. return -EINVAL;
  162. cs_tuners->up_threshold = input;
  163. return count;
  164. }
  165. static ssize_t store_down_threshold(struct dbs_data *dbs_data, const char *buf,
  166. size_t count)
  167. {
  168. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  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(struct dbs_data *dbs_data, const char *buf,
  180. size_t count)
  181. {
  182. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  183. unsigned int input, j;
  184. int ret;
  185. ret = sscanf(buf, "%u", &input);
  186. if (ret != 1)
  187. return -EINVAL;
  188. if (input > 1)
  189. input = 1;
  190. if (input == cs_tuners->ignore_nice) /* nothing to do */
  191. return count;
  192. cs_tuners->ignore_nice = input;
  193. /* we need to re-evaluate prev_cpu_idle */
  194. for_each_online_cpu(j) {
  195. struct cs_cpu_dbs_info_s *dbs_info;
  196. dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  197. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  198. &dbs_info->cdbs.prev_cpu_wall);
  199. if (cs_tuners->ignore_nice)
  200. dbs_info->cdbs.prev_cpu_nice =
  201. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  202. }
  203. return count;
  204. }
  205. static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf,
  206. size_t count)
  207. {
  208. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  209. unsigned int input;
  210. int ret;
  211. ret = sscanf(buf, "%u", &input);
  212. if (ret != 1)
  213. return -EINVAL;
  214. if (input > 100)
  215. input = 100;
  216. /*
  217. * no need to test here if freq_step is zero as the user might actually
  218. * want this, they would be crazy though :)
  219. */
  220. cs_tuners->freq_step = input;
  221. return count;
  222. }
  223. show_store_one(cs, sampling_rate);
  224. show_store_one(cs, sampling_down_factor);
  225. show_store_one(cs, up_threshold);
  226. show_store_one(cs, down_threshold);
  227. show_store_one(cs, ignore_nice);
  228. show_store_one(cs, freq_step);
  229. declare_show_sampling_rate_min(cs);
  230. gov_sys_pol_attr_rw(sampling_rate);
  231. gov_sys_pol_attr_rw(sampling_down_factor);
  232. gov_sys_pol_attr_rw(up_threshold);
  233. gov_sys_pol_attr_rw(down_threshold);
  234. gov_sys_pol_attr_rw(ignore_nice);
  235. gov_sys_pol_attr_rw(freq_step);
  236. gov_sys_pol_attr_ro(sampling_rate_min);
  237. static struct attribute *dbs_attributes_gov_sys[] = {
  238. &sampling_rate_min_gov_sys.attr,
  239. &sampling_rate_gov_sys.attr,
  240. &sampling_down_factor_gov_sys.attr,
  241. &up_threshold_gov_sys.attr,
  242. &down_threshold_gov_sys.attr,
  243. &ignore_nice_gov_sys.attr,
  244. &freq_step_gov_sys.attr,
  245. NULL
  246. };
  247. static struct attribute_group cs_attr_group_gov_sys = {
  248. .attrs = dbs_attributes_gov_sys,
  249. .name = "conservative",
  250. };
  251. static struct attribute *dbs_attributes_gov_pol[] = {
  252. &sampling_rate_min_gov_pol.attr,
  253. &sampling_rate_gov_pol.attr,
  254. &sampling_down_factor_gov_pol.attr,
  255. &up_threshold_gov_pol.attr,
  256. &down_threshold_gov_pol.attr,
  257. &ignore_nice_gov_pol.attr,
  258. &freq_step_gov_pol.attr,
  259. NULL
  260. };
  261. static struct attribute_group cs_attr_group_gov_pol = {
  262. .attrs = dbs_attributes_gov_pol,
  263. .name = "conservative",
  264. };
  265. /************************** sysfs end ************************/
  266. static int cs_init(struct dbs_data *dbs_data)
  267. {
  268. struct cs_dbs_tuners *tuners;
  269. tuners = kzalloc(sizeof(struct cs_dbs_tuners), GFP_KERNEL);
  270. if (!tuners) {
  271. pr_err("%s: kzalloc failed\n", __func__);
  272. return -ENOMEM;
  273. }
  274. tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  275. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  276. tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  277. tuners->ignore_nice = 0;
  278. tuners->freq_step = 5;
  279. dbs_data->tuners = tuners;
  280. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  281. jiffies_to_usecs(10);
  282. mutex_init(&dbs_data->mutex);
  283. return 0;
  284. }
  285. static void cs_exit(struct dbs_data *dbs_data)
  286. {
  287. kfree(dbs_data->tuners);
  288. }
  289. define_get_cpu_dbs_routines(cs_cpu_dbs_info);
  290. static struct notifier_block cs_cpufreq_notifier_block = {
  291. .notifier_call = dbs_cpufreq_notifier,
  292. };
  293. static struct cs_ops cs_ops = {
  294. .notifier_block = &cs_cpufreq_notifier_block,
  295. };
  296. static struct common_dbs_data cs_dbs_cdata = {
  297. .governor = GOV_CONSERVATIVE,
  298. .attr_group_gov_sys = &cs_attr_group_gov_sys,
  299. .attr_group_gov_pol = &cs_attr_group_gov_pol,
  300. .get_cpu_cdbs = get_cpu_cdbs,
  301. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  302. .gov_dbs_timer = cs_dbs_timer,
  303. .gov_check_cpu = cs_check_cpu,
  304. .gov_ops = &cs_ops,
  305. .init = cs_init,
  306. .exit = cs_exit,
  307. };
  308. static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  309. unsigned int event)
  310. {
  311. return cpufreq_governor_dbs(policy, &cs_dbs_cdata, event);
  312. }
  313. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  314. static
  315. #endif
  316. struct cpufreq_governor cpufreq_gov_conservative = {
  317. .name = "conservative",
  318. .governor = cs_cpufreq_governor_dbs,
  319. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  320. .owner = THIS_MODULE,
  321. };
  322. static int __init cpufreq_gov_dbs_init(void)
  323. {
  324. return cpufreq_register_governor(&cpufreq_gov_conservative);
  325. }
  326. static void __exit cpufreq_gov_dbs_exit(void)
  327. {
  328. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  329. }
  330. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  331. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  332. "Low Latency Frequency Transition capable processors "
  333. "optimised for use in a battery environment");
  334. MODULE_LICENSE("GPL");
  335. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  336. fs_initcall(cpufreq_gov_dbs_init);
  337. #else
  338. module_init(cpufreq_gov_dbs_init);
  339. #endif
  340. module_exit(cpufreq_gov_dbs_exit);