cpufreq_conservative.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. struct cs_policy_dbs_info {
  16. struct policy_dbs_info policy_dbs;
  17. unsigned int down_skip;
  18. unsigned int requested_freq;
  19. };
  20. static inline struct cs_policy_dbs_info *to_dbs_info(struct policy_dbs_info *policy_dbs)
  21. {
  22. return container_of(policy_dbs, struct cs_policy_dbs_info, policy_dbs);
  23. }
  24. struct cs_dbs_tuners {
  25. unsigned int down_threshold;
  26. unsigned int freq_step;
  27. };
  28. /* Conservative governor macros */
  29. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  30. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  31. #define DEF_FREQUENCY_STEP (5)
  32. #define DEF_SAMPLING_DOWN_FACTOR (1)
  33. #define MAX_SAMPLING_DOWN_FACTOR (10)
  34. static inline unsigned int get_freq_target(struct cs_dbs_tuners *cs_tuners,
  35. struct cpufreq_policy *policy)
  36. {
  37. unsigned int freq_target = (cs_tuners->freq_step * policy->max) / 100;
  38. /* max freq cannot be less than 100. But who knows... */
  39. if (unlikely(freq_target == 0))
  40. freq_target = DEF_FREQUENCY_STEP;
  41. return freq_target;
  42. }
  43. /*
  44. * Every sampling_rate, we check, if current idle time is less than 20%
  45. * (default), then we try to increase frequency. Every sampling_rate *
  46. * sampling_down_factor, we check, if current idle time is more than 80%
  47. * (default), then we try to decrease frequency
  48. *
  49. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  50. * happens at minimum steps of 5% (default) of maximum frequency
  51. */
  52. static unsigned int cs_dbs_timer(struct cpufreq_policy *policy)
  53. {
  54. struct policy_dbs_info *policy_dbs = policy->governor_data;
  55. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  56. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  57. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  58. unsigned int load = dbs_update(policy);
  59. /*
  60. * break out if we 'cannot' reduce the speed as the user might
  61. * want freq_step to be zero
  62. */
  63. if (cs_tuners->freq_step == 0)
  64. goto out;
  65. /* Check for frequency increase */
  66. if (load > dbs_data->up_threshold) {
  67. dbs_info->down_skip = 0;
  68. /* if we are already at full speed then break out early */
  69. if (dbs_info->requested_freq == policy->max)
  70. goto out;
  71. dbs_info->requested_freq += get_freq_target(cs_tuners, policy);
  72. if (dbs_info->requested_freq > policy->max)
  73. dbs_info->requested_freq = policy->max;
  74. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  75. CPUFREQ_RELATION_H);
  76. goto out;
  77. }
  78. /* if sampling_down_factor is active break out early */
  79. if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
  80. goto out;
  81. dbs_info->down_skip = 0;
  82. /* Check for frequency decrease */
  83. if (load < cs_tuners->down_threshold) {
  84. unsigned int freq_target;
  85. /*
  86. * if we cannot reduce the frequency anymore, break out early
  87. */
  88. if (policy->cur == policy->min)
  89. goto out;
  90. freq_target = get_freq_target(cs_tuners, policy);
  91. if (dbs_info->requested_freq > freq_target)
  92. dbs_info->requested_freq -= freq_target;
  93. else
  94. dbs_info->requested_freq = policy->min;
  95. __cpufreq_driver_target(policy, dbs_info->requested_freq,
  96. CPUFREQ_RELATION_L);
  97. }
  98. out:
  99. return dbs_data->sampling_rate;
  100. }
  101. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  102. void *data);
  103. static struct notifier_block cs_cpufreq_notifier_block = {
  104. .notifier_call = dbs_cpufreq_notifier,
  105. };
  106. /************************** sysfs interface ************************/
  107. static struct dbs_governor cs_dbs_gov;
  108. static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
  109. const char *buf, size_t count)
  110. {
  111. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  112. unsigned int input;
  113. int ret;
  114. ret = sscanf(buf, "%u", &input);
  115. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  116. return -EINVAL;
  117. dbs_data->sampling_down_factor = input;
  118. return count;
  119. }
  120. static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
  121. const char *buf, size_t count)
  122. {
  123. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  124. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  125. unsigned int input;
  126. int ret;
  127. ret = sscanf(buf, "%u", &input);
  128. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  129. return -EINVAL;
  130. dbs_data->up_threshold = input;
  131. return count;
  132. }
  133. static ssize_t store_down_threshold(struct gov_attr_set *attr_set,
  134. const char *buf, size_t count)
  135. {
  136. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  137. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  138. unsigned int input;
  139. int ret;
  140. ret = sscanf(buf, "%u", &input);
  141. /* cannot be lower than 11 otherwise freq will not fall */
  142. if (ret != 1 || input < 11 || input > 100 ||
  143. input >= dbs_data->up_threshold)
  144. return -EINVAL;
  145. cs_tuners->down_threshold = input;
  146. return count;
  147. }
  148. static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
  149. const char *buf, size_t count)
  150. {
  151. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  152. unsigned int input;
  153. int ret;
  154. ret = sscanf(buf, "%u", &input);
  155. if (ret != 1)
  156. return -EINVAL;
  157. if (input > 1)
  158. input = 1;
  159. if (input == dbs_data->ignore_nice_load) /* nothing to do */
  160. return count;
  161. dbs_data->ignore_nice_load = input;
  162. /* we need to re-evaluate prev_cpu_idle */
  163. gov_update_cpu_data(dbs_data);
  164. return count;
  165. }
  166. static ssize_t store_freq_step(struct gov_attr_set *attr_set, const char *buf,
  167. size_t count)
  168. {
  169. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  170. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  171. unsigned int input;
  172. int ret;
  173. ret = sscanf(buf, "%u", &input);
  174. if (ret != 1)
  175. return -EINVAL;
  176. if (input > 100)
  177. input = 100;
  178. /*
  179. * no need to test here if freq_step is zero as the user might actually
  180. * want this, they would be crazy though :)
  181. */
  182. cs_tuners->freq_step = input;
  183. return count;
  184. }
  185. gov_show_one_common(sampling_rate);
  186. gov_show_one_common(sampling_down_factor);
  187. gov_show_one_common(up_threshold);
  188. gov_show_one_common(ignore_nice_load);
  189. gov_show_one_common(min_sampling_rate);
  190. gov_show_one(cs, down_threshold);
  191. gov_show_one(cs, freq_step);
  192. gov_attr_rw(sampling_rate);
  193. gov_attr_rw(sampling_down_factor);
  194. gov_attr_rw(up_threshold);
  195. gov_attr_rw(ignore_nice_load);
  196. gov_attr_ro(min_sampling_rate);
  197. gov_attr_rw(down_threshold);
  198. gov_attr_rw(freq_step);
  199. static struct attribute *cs_attributes[] = {
  200. &min_sampling_rate.attr,
  201. &sampling_rate.attr,
  202. &sampling_down_factor.attr,
  203. &up_threshold.attr,
  204. &down_threshold.attr,
  205. &ignore_nice_load.attr,
  206. &freq_step.attr,
  207. NULL
  208. };
  209. /************************** sysfs end ************************/
  210. static struct policy_dbs_info *cs_alloc(void)
  211. {
  212. struct cs_policy_dbs_info *dbs_info;
  213. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  214. return dbs_info ? &dbs_info->policy_dbs : NULL;
  215. }
  216. static void cs_free(struct policy_dbs_info *policy_dbs)
  217. {
  218. kfree(to_dbs_info(policy_dbs));
  219. }
  220. static int cs_init(struct dbs_data *dbs_data, bool notify)
  221. {
  222. struct cs_dbs_tuners *tuners;
  223. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  224. if (!tuners) {
  225. pr_err("%s: kzalloc failed\n", __func__);
  226. return -ENOMEM;
  227. }
  228. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  229. tuners->freq_step = DEF_FREQUENCY_STEP;
  230. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  231. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  232. dbs_data->ignore_nice_load = 0;
  233. dbs_data->tuners = tuners;
  234. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  235. jiffies_to_usecs(10);
  236. if (notify)
  237. cpufreq_register_notifier(&cs_cpufreq_notifier_block,
  238. CPUFREQ_TRANSITION_NOTIFIER);
  239. return 0;
  240. }
  241. static void cs_exit(struct dbs_data *dbs_data, bool notify)
  242. {
  243. if (notify)
  244. cpufreq_unregister_notifier(&cs_cpufreq_notifier_block,
  245. CPUFREQ_TRANSITION_NOTIFIER);
  246. kfree(dbs_data->tuners);
  247. }
  248. static void cs_start(struct cpufreq_policy *policy)
  249. {
  250. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  251. dbs_info->down_skip = 0;
  252. dbs_info->requested_freq = policy->cur;
  253. }
  254. static struct dbs_governor cs_dbs_gov = {
  255. .gov = {
  256. .name = "conservative",
  257. .governor = cpufreq_governor_dbs,
  258. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  259. .owner = THIS_MODULE,
  260. },
  261. .kobj_type = { .default_attrs = cs_attributes },
  262. .gov_dbs_timer = cs_dbs_timer,
  263. .alloc = cs_alloc,
  264. .free = cs_free,
  265. .init = cs_init,
  266. .exit = cs_exit,
  267. .start = cs_start,
  268. };
  269. #define CPU_FREQ_GOV_CONSERVATIVE (&cs_dbs_gov.gov)
  270. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  271. void *data)
  272. {
  273. struct cpufreq_freqs *freq = data;
  274. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(freq->cpu);
  275. struct cs_policy_dbs_info *dbs_info;
  276. if (!policy)
  277. return 0;
  278. /* policy isn't governed by conservative governor */
  279. if (policy->governor != CPU_FREQ_GOV_CONSERVATIVE)
  280. return 0;
  281. dbs_info = to_dbs_info(policy->governor_data);
  282. /*
  283. * we only care if our internally tracked freq moves outside the 'valid'
  284. * ranges of frequency available to us otherwise we do not change it
  285. */
  286. if (dbs_info->requested_freq > policy->max
  287. || dbs_info->requested_freq < policy->min)
  288. dbs_info->requested_freq = freq->new;
  289. return 0;
  290. }
  291. static int __init cpufreq_gov_dbs_init(void)
  292. {
  293. return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE);
  294. }
  295. static void __exit cpufreq_gov_dbs_exit(void)
  296. {
  297. cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE);
  298. }
  299. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  300. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  301. "Low Latency Frequency Transition capable processors "
  302. "optimised for use in a battery environment");
  303. MODULE_LICENSE("GPL");
  304. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  305. struct cpufreq_governor *cpufreq_default_governor(void)
  306. {
  307. return CPU_FREQ_GOV_CONSERVATIVE;
  308. }
  309. fs_initcall(cpufreq_gov_dbs_init);
  310. #else
  311. module_init(cpufreq_gov_dbs_init);
  312. #endif
  313. module_exit(cpufreq_gov_dbs_exit);