cpufreq_conservative.c 8.5 KB

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