cpufreq_conservative.c 9.0 KB

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