cpufreq_conservative.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/slab.h>
  14. #include "cpufreq_governor.h"
  15. /* Conservative governor macros */
  16. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  17. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  18. #define DEF_FREQUENCY_STEP (5)
  19. #define DEF_SAMPLING_DOWN_FACTOR (1)
  20. #define MAX_SAMPLING_DOWN_FACTOR (10)
  21. static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info);
  22. static inline unsigned int get_freq_target(struct cs_dbs_tuners *cs_tuners,
  23. struct cpufreq_policy *policy)
  24. {
  25. unsigned int freq_target = (cs_tuners->freq_step * policy->max) / 100;
  26. /* max freq cannot be less than 100. But who knows... */
  27. if (unlikely(freq_target == 0))
  28. freq_target = DEF_FREQUENCY_STEP;
  29. return freq_target;
  30. }
  31. /*
  32. * Every sampling_rate, we check, if current idle time is less than 20%
  33. * (default), then we try to increase frequency. Every sampling_rate *
  34. * sampling_down_factor, we check, if current idle time is more than 80%
  35. * (default), then we try to decrease frequency
  36. *
  37. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  38. * happens at minimum steps of 5% (default) of maximum frequency
  39. */
  40. static void cs_check_cpu(int cpu, unsigned int load)
  41. {
  42. struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
  43. struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
  44. struct dbs_data *dbs_data = policy->governor_data;
  45. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  46. /*
  47. * break out if we 'cannot' reduce the speed as the user might
  48. * want freq_step to be zero
  49. */
  50. if (cs_tuners->freq_step == 0)
  51. return;
  52. /* Check for frequency increase */
  53. if (load > cs_tuners->up_threshold) {
  54. dbs_info->down_skip = 0;
  55. /* if we are already at full speed then break out early */
  56. if (dbs_info->requested_freq == policy->max)
  57. return;
  58. dbs_info->requested_freq += get_freq_target(cs_tuners, policy);
  59. if (dbs_info->requested_freq > policy->max)
  60. dbs_info->requested_freq = policy->max;
  61. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  62. CPUFREQ_RELATION_H);
  63. return;
  64. }
  65. /* if sampling_down_factor is active break out early */
  66. if (++dbs_info->down_skip < cs_tuners->sampling_down_factor)
  67. return;
  68. dbs_info->down_skip = 0;
  69. /* Check for frequency decrease */
  70. if (load < cs_tuners->down_threshold) {
  71. unsigned int freq_target;
  72. /*
  73. * if we cannot reduce the frequency anymore, break out early
  74. */
  75. if (policy->cur == policy->min)
  76. return;
  77. freq_target = get_freq_target(cs_tuners, policy);
  78. if (dbs_info->requested_freq > freq_target)
  79. dbs_info->requested_freq -= freq_target;
  80. else
  81. dbs_info->requested_freq = policy->min;
  82. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  83. CPUFREQ_RELATION_L);
  84. return;
  85. }
  86. }
  87. static void cs_dbs_timer(struct work_struct *work)
  88. {
  89. struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
  90. struct cs_cpu_dbs_info_s, cdbs.work.work);
  91. unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
  92. struct cs_cpu_dbs_info_s *core_dbs_info = &per_cpu(cs_cpu_dbs_info,
  93. cpu);
  94. struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
  95. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  96. int delay = delay_for_sampling_rate(cs_tuners->sampling_rate);
  97. bool modify_all = true;
  98. mutex_lock(&core_dbs_info->cdbs.timer_mutex);
  99. if (!need_load_eval(&core_dbs_info->cdbs, cs_tuners->sampling_rate))
  100. modify_all = false;
  101. else
  102. dbs_check_cpu(dbs_data, cpu);
  103. gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all);
  104. mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
  105. }
  106. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  107. void *data)
  108. {
  109. struct cpufreq_freqs *freq = data;
  110. struct cs_cpu_dbs_info_s *dbs_info =
  111. &per_cpu(cs_cpu_dbs_info, freq->cpu);
  112. struct cpufreq_policy *policy;
  113. if (!dbs_info->enable)
  114. return 0;
  115. policy = dbs_info->cdbs.cur_policy;
  116. /*
  117. * we only care if our internally tracked freq moves outside the 'valid'
  118. * ranges of frequency available to us otherwise we do not change it
  119. */
  120. if (dbs_info->requested_freq > policy->max
  121. || dbs_info->requested_freq < policy->min)
  122. dbs_info->requested_freq = freq->new;
  123. return 0;
  124. }
  125. static struct notifier_block cs_cpufreq_notifier_block = {
  126. .notifier_call = dbs_cpufreq_notifier,
  127. };
  128. /************************** sysfs interface ************************/
  129. static struct common_dbs_data cs_dbs_cdata;
  130. static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
  131. const char *buf, size_t count)
  132. {
  133. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  134. unsigned int input;
  135. int ret;
  136. ret = sscanf(buf, "%u", &input);
  137. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  138. return -EINVAL;
  139. cs_tuners->sampling_down_factor = input;
  140. return count;
  141. }
  142. static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
  143. size_t count)
  144. {
  145. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  146. unsigned int input;
  147. int ret;
  148. ret = sscanf(buf, "%u", &input);
  149. if (ret != 1)
  150. return -EINVAL;
  151. cs_tuners->sampling_rate = max(input, dbs_data->min_sampling_rate);
  152. return count;
  153. }
  154. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  155. size_t count)
  156. {
  157. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  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 dbs_data *dbs_data, const char *buf,
  167. size_t count)
  168. {
  169. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  170. unsigned int input;
  171. int ret;
  172. ret = sscanf(buf, "%u", &input);
  173. /* cannot be lower than 11 otherwise freq will not fall */
  174. if (ret != 1 || input < 11 || input > 100 ||
  175. input >= cs_tuners->up_threshold)
  176. return -EINVAL;
  177. cs_tuners->down_threshold = input;
  178. return count;
  179. }
  180. static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
  181. const char *buf, size_t count)
  182. {
  183. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  184. unsigned int input, j;
  185. int ret;
  186. ret = sscanf(buf, "%u", &input);
  187. if (ret != 1)
  188. return -EINVAL;
  189. if (input > 1)
  190. input = 1;
  191. if (input == cs_tuners->ignore_nice_load) /* nothing to do */
  192. return count;
  193. cs_tuners->ignore_nice_load = input;
  194. /* we need to re-evaluate prev_cpu_idle */
  195. for_each_online_cpu(j) {
  196. struct cs_cpu_dbs_info_s *dbs_info;
  197. dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  198. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  199. &dbs_info->cdbs.prev_cpu_wall, 0);
  200. if (cs_tuners->ignore_nice_load)
  201. dbs_info->cdbs.prev_cpu_nice =
  202. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  203. }
  204. return count;
  205. }
  206. static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf,
  207. size_t count)
  208. {
  209. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  210. unsigned int input;
  211. int ret;
  212. ret = sscanf(buf, "%u", &input);
  213. if (ret != 1)
  214. return -EINVAL;
  215. if (input > 100)
  216. input = 100;
  217. /*
  218. * no need to test here if freq_step is zero as the user might actually
  219. * want this, they would be crazy though :)
  220. */
  221. cs_tuners->freq_step = input;
  222. return count;
  223. }
  224. show_store_one(cs, sampling_rate);
  225. show_store_one(cs, sampling_down_factor);
  226. show_store_one(cs, up_threshold);
  227. show_store_one(cs, down_threshold);
  228. show_store_one(cs, ignore_nice_load);
  229. show_store_one(cs, freq_step);
  230. declare_show_sampling_rate_min(cs);
  231. gov_sys_pol_attr_rw(sampling_rate);
  232. gov_sys_pol_attr_rw(sampling_down_factor);
  233. gov_sys_pol_attr_rw(up_threshold);
  234. gov_sys_pol_attr_rw(down_threshold);
  235. gov_sys_pol_attr_rw(ignore_nice_load);
  236. gov_sys_pol_attr_rw(freq_step);
  237. gov_sys_pol_attr_ro(sampling_rate_min);
  238. static struct attribute *dbs_attributes_gov_sys[] = {
  239. &sampling_rate_min_gov_sys.attr,
  240. &sampling_rate_gov_sys.attr,
  241. &sampling_down_factor_gov_sys.attr,
  242. &up_threshold_gov_sys.attr,
  243. &down_threshold_gov_sys.attr,
  244. &ignore_nice_load_gov_sys.attr,
  245. &freq_step_gov_sys.attr,
  246. NULL
  247. };
  248. static struct attribute_group cs_attr_group_gov_sys = {
  249. .attrs = dbs_attributes_gov_sys,
  250. .name = "conservative",
  251. };
  252. static struct attribute *dbs_attributes_gov_pol[] = {
  253. &sampling_rate_min_gov_pol.attr,
  254. &sampling_rate_gov_pol.attr,
  255. &sampling_down_factor_gov_pol.attr,
  256. &up_threshold_gov_pol.attr,
  257. &down_threshold_gov_pol.attr,
  258. &ignore_nice_load_gov_pol.attr,
  259. &freq_step_gov_pol.attr,
  260. NULL
  261. };
  262. static struct attribute_group cs_attr_group_gov_pol = {
  263. .attrs = dbs_attributes_gov_pol,
  264. .name = "conservative",
  265. };
  266. /************************** sysfs end ************************/
  267. static int cs_init(struct dbs_data *dbs_data, bool notify)
  268. {
  269. struct cs_dbs_tuners *tuners;
  270. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  271. if (!tuners) {
  272. pr_err("%s: kzalloc failed\n", __func__);
  273. return -ENOMEM;
  274. }
  275. tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  276. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  277. tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  278. tuners->ignore_nice_load = 0;
  279. tuners->freq_step = DEF_FREQUENCY_STEP;
  280. dbs_data->tuners = tuners;
  281. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  282. jiffies_to_usecs(10);
  283. if (notify)
  284. cpufreq_register_notifier(&cs_cpufreq_notifier_block,
  285. CPUFREQ_TRANSITION_NOTIFIER);
  286. return 0;
  287. }
  288. static void cs_exit(struct dbs_data *dbs_data, bool notify)
  289. {
  290. if (notify)
  291. cpufreq_unregister_notifier(&cs_cpufreq_notifier_block,
  292. CPUFREQ_TRANSITION_NOTIFIER);
  293. kfree(dbs_data->tuners);
  294. }
  295. define_get_cpu_dbs_routines(cs_cpu_dbs_info);
  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. .init = cs_init,
  305. .exit = cs_exit,
  306. .mutex = __MUTEX_INITIALIZER(cs_dbs_cdata.mutex),
  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);