cpufreq_conservative.c 11 KB

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