cpufreq_conservative.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_step(struct cs_dbs_tuners *cs_tuners,
  35. struct cpufreq_policy *policy)
  36. {
  37. unsigned int freq_step = (cs_tuners->freq_step * policy->max) / 100;
  38. /* max freq cannot be less than 100. But who knows... */
  39. if (unlikely(freq_step == 0))
  40. freq_step = DEF_FREQUENCY_STEP;
  41. return freq_step;
  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. * Frequency updates happen at minimum steps of 5% (default) of maximum
  50. * frequency
  51. */
  52. static unsigned int cs_dbs_update(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. unsigned int requested_freq = dbs_info->requested_freq;
  57. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  58. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  59. unsigned int load = dbs_update(policy);
  60. unsigned int freq_step;
  61. /*
  62. * break out if we 'cannot' reduce the speed as the user might
  63. * want freq_step to be zero
  64. */
  65. if (cs_tuners->freq_step == 0)
  66. goto out;
  67. /*
  68. * If requested_freq is out of range, it is likely that the limits
  69. * changed in the meantime, so fall back to current frequency in that
  70. * case.
  71. */
  72. if (requested_freq > policy->max || requested_freq < policy->min) {
  73. requested_freq = policy->cur;
  74. dbs_info->requested_freq = requested_freq;
  75. }
  76. freq_step = get_freq_step(cs_tuners, policy);
  77. /*
  78. * Decrease requested_freq one freq_step for each idle period that
  79. * we didn't update the frequency.
  80. */
  81. if (policy_dbs->idle_periods < UINT_MAX) {
  82. unsigned int freq_steps = policy_dbs->idle_periods * freq_step;
  83. if (requested_freq > policy->min + freq_steps)
  84. requested_freq -= freq_steps;
  85. else
  86. requested_freq = policy->min;
  87. policy_dbs->idle_periods = UINT_MAX;
  88. }
  89. /* Check for frequency increase */
  90. if (load > dbs_data->up_threshold) {
  91. dbs_info->down_skip = 0;
  92. /* if we are already at full speed then break out early */
  93. if (requested_freq == policy->max)
  94. goto out;
  95. requested_freq += freq_step;
  96. if (requested_freq > policy->max)
  97. requested_freq = policy->max;
  98. __cpufreq_driver_target(policy, requested_freq, CPUFREQ_RELATION_H);
  99. dbs_info->requested_freq = requested_freq;
  100. goto out;
  101. }
  102. /* if sampling_down_factor is active break out early */
  103. if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
  104. goto out;
  105. dbs_info->down_skip = 0;
  106. /* Check for frequency decrease */
  107. if (load < cs_tuners->down_threshold) {
  108. /*
  109. * if we cannot reduce the frequency anymore, break out early
  110. */
  111. if (requested_freq == policy->min)
  112. goto out;
  113. if (requested_freq > freq_step)
  114. requested_freq -= freq_step;
  115. else
  116. requested_freq = policy->min;
  117. __cpufreq_driver_target(policy, requested_freq, CPUFREQ_RELATION_L);
  118. dbs_info->requested_freq = requested_freq;
  119. }
  120. out:
  121. return dbs_data->sampling_rate;
  122. }
  123. /************************** sysfs interface ************************/
  124. static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
  125. const char *buf, size_t count)
  126. {
  127. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  128. unsigned int input;
  129. int ret;
  130. ret = sscanf(buf, "%u", &input);
  131. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  132. return -EINVAL;
  133. dbs_data->sampling_down_factor = input;
  134. return count;
  135. }
  136. static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
  137. const char *buf, size_t count)
  138. {
  139. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  140. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  141. unsigned int input;
  142. int ret;
  143. ret = sscanf(buf, "%u", &input);
  144. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  145. return -EINVAL;
  146. dbs_data->up_threshold = input;
  147. return count;
  148. }
  149. static ssize_t store_down_threshold(struct gov_attr_set *attr_set,
  150. const char *buf, size_t count)
  151. {
  152. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  153. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  154. unsigned int input;
  155. int ret;
  156. ret = sscanf(buf, "%u", &input);
  157. /* cannot be lower than 1 otherwise freq will not fall */
  158. if (ret != 1 || input < 1 || input > 100 ||
  159. input >= dbs_data->up_threshold)
  160. return -EINVAL;
  161. cs_tuners->down_threshold = input;
  162. return count;
  163. }
  164. static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
  165. const char *buf, size_t count)
  166. {
  167. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  168. unsigned int input;
  169. int ret;
  170. ret = sscanf(buf, "%u", &input);
  171. if (ret != 1)
  172. return -EINVAL;
  173. if (input > 1)
  174. input = 1;
  175. if (input == dbs_data->ignore_nice_load) /* nothing to do */
  176. return count;
  177. dbs_data->ignore_nice_load = input;
  178. /* we need to re-evaluate prev_cpu_idle */
  179. gov_update_cpu_data(dbs_data);
  180. return count;
  181. }
  182. static ssize_t store_freq_step(struct gov_attr_set *attr_set, const char *buf,
  183. size_t count)
  184. {
  185. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  186. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  187. unsigned int input;
  188. int ret;
  189. ret = sscanf(buf, "%u", &input);
  190. if (ret != 1)
  191. return -EINVAL;
  192. if (input > 100)
  193. input = 100;
  194. /*
  195. * no need to test here if freq_step is zero as the user might actually
  196. * want this, they would be crazy though :)
  197. */
  198. cs_tuners->freq_step = input;
  199. return count;
  200. }
  201. gov_show_one_common(sampling_rate);
  202. gov_show_one_common(sampling_down_factor);
  203. gov_show_one_common(up_threshold);
  204. gov_show_one_common(ignore_nice_load);
  205. gov_show_one(cs, down_threshold);
  206. gov_show_one(cs, freq_step);
  207. gov_attr_rw(sampling_rate);
  208. gov_attr_rw(sampling_down_factor);
  209. gov_attr_rw(up_threshold);
  210. gov_attr_rw(ignore_nice_load);
  211. gov_attr_rw(down_threshold);
  212. gov_attr_rw(freq_step);
  213. static struct attribute *cs_attributes[] = {
  214. &sampling_rate.attr,
  215. &sampling_down_factor.attr,
  216. &up_threshold.attr,
  217. &down_threshold.attr,
  218. &ignore_nice_load.attr,
  219. &freq_step.attr,
  220. NULL
  221. };
  222. /************************** sysfs end ************************/
  223. static struct policy_dbs_info *cs_alloc(void)
  224. {
  225. struct cs_policy_dbs_info *dbs_info;
  226. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  227. return dbs_info ? &dbs_info->policy_dbs : NULL;
  228. }
  229. static void cs_free(struct policy_dbs_info *policy_dbs)
  230. {
  231. kfree(to_dbs_info(policy_dbs));
  232. }
  233. static int cs_init(struct dbs_data *dbs_data)
  234. {
  235. struct cs_dbs_tuners *tuners;
  236. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  237. if (!tuners)
  238. return -ENOMEM;
  239. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  240. tuners->freq_step = DEF_FREQUENCY_STEP;
  241. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  242. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  243. dbs_data->ignore_nice_load = 0;
  244. dbs_data->tuners = tuners;
  245. return 0;
  246. }
  247. static void cs_exit(struct dbs_data *dbs_data)
  248. {
  249. kfree(dbs_data->tuners);
  250. }
  251. static void cs_start(struct cpufreq_policy *policy)
  252. {
  253. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  254. dbs_info->down_skip = 0;
  255. dbs_info->requested_freq = policy->cur;
  256. }
  257. static struct dbs_governor cs_governor = {
  258. .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("conservative"),
  259. .kobj_type = { .default_attrs = cs_attributes },
  260. .gov_dbs_update = cs_dbs_update,
  261. .alloc = cs_alloc,
  262. .free = cs_free,
  263. .init = cs_init,
  264. .exit = cs_exit,
  265. .start = cs_start,
  266. };
  267. #define CPU_FREQ_GOV_CONSERVATIVE (&cs_governor.gov)
  268. static int __init cpufreq_gov_dbs_init(void)
  269. {
  270. return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE);
  271. }
  272. static void __exit cpufreq_gov_dbs_exit(void)
  273. {
  274. cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE);
  275. }
  276. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  277. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  278. "Low Latency Frequency Transition capable processors "
  279. "optimised for use in a battery environment");
  280. MODULE_LICENSE("GPL");
  281. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  282. struct cpufreq_governor *cpufreq_default_governor(void)
  283. {
  284. return CPU_FREQ_GOV_CONSERVATIVE;
  285. }
  286. fs_initcall(cpufreq_gov_dbs_init);
  287. #else
  288. module_init(cpufreq_gov_dbs_init);
  289. #endif
  290. module_exit(cpufreq_gov_dbs_exit);