cpufreq_conservative.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 dbs_data *dbs_data,
  109. const char *buf, size_t count)
  110. {
  111. unsigned int input;
  112. int ret;
  113. ret = sscanf(buf, "%u", &input);
  114. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  115. return -EINVAL;
  116. dbs_data->sampling_down_factor = input;
  117. return count;
  118. }
  119. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  120. size_t count)
  121. {
  122. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  123. unsigned int input;
  124. int ret;
  125. ret = sscanf(buf, "%u", &input);
  126. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  127. return -EINVAL;
  128. dbs_data->up_threshold = input;
  129. return count;
  130. }
  131. static ssize_t store_down_threshold(struct dbs_data *dbs_data, const char *buf,
  132. size_t count)
  133. {
  134. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  135. unsigned int input;
  136. int ret;
  137. ret = sscanf(buf, "%u", &input);
  138. /* cannot be lower than 11 otherwise freq will not fall */
  139. if (ret != 1 || input < 11 || input > 100 ||
  140. input >= dbs_data->up_threshold)
  141. return -EINVAL;
  142. cs_tuners->down_threshold = input;
  143. return count;
  144. }
  145. static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
  146. const char *buf, size_t count)
  147. {
  148. unsigned int input;
  149. int ret;
  150. ret = sscanf(buf, "%u", &input);
  151. if (ret != 1)
  152. return -EINVAL;
  153. if (input > 1)
  154. input = 1;
  155. if (input == dbs_data->ignore_nice_load) /* nothing to do */
  156. return count;
  157. dbs_data->ignore_nice_load = input;
  158. /* we need to re-evaluate prev_cpu_idle */
  159. gov_update_cpu_data(dbs_data);
  160. return count;
  161. }
  162. static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf,
  163. size_t count)
  164. {
  165. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  166. unsigned int input;
  167. int ret;
  168. ret = sscanf(buf, "%u", &input);
  169. if (ret != 1)
  170. return -EINVAL;
  171. if (input > 100)
  172. input = 100;
  173. /*
  174. * no need to test here if freq_step is zero as the user might actually
  175. * want this, they would be crazy though :)
  176. */
  177. cs_tuners->freq_step = input;
  178. return count;
  179. }
  180. gov_show_one_common(sampling_rate);
  181. gov_show_one_common(sampling_down_factor);
  182. gov_show_one_common(up_threshold);
  183. gov_show_one_common(ignore_nice_load);
  184. gov_show_one_common(min_sampling_rate);
  185. gov_show_one(cs, down_threshold);
  186. gov_show_one(cs, freq_step);
  187. gov_attr_rw(sampling_rate);
  188. gov_attr_rw(sampling_down_factor);
  189. gov_attr_rw(up_threshold);
  190. gov_attr_rw(ignore_nice_load);
  191. gov_attr_ro(min_sampling_rate);
  192. gov_attr_rw(down_threshold);
  193. gov_attr_rw(freq_step);
  194. static struct attribute *cs_attributes[] = {
  195. &min_sampling_rate.attr,
  196. &sampling_rate.attr,
  197. &sampling_down_factor.attr,
  198. &up_threshold.attr,
  199. &down_threshold.attr,
  200. &ignore_nice_load.attr,
  201. &freq_step.attr,
  202. NULL
  203. };
  204. /************************** sysfs end ************************/
  205. static struct policy_dbs_info *cs_alloc(void)
  206. {
  207. struct cs_policy_dbs_info *dbs_info;
  208. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  209. return dbs_info ? &dbs_info->policy_dbs : NULL;
  210. }
  211. static void cs_free(struct policy_dbs_info *policy_dbs)
  212. {
  213. kfree(to_dbs_info(policy_dbs));
  214. }
  215. static int cs_init(struct dbs_data *dbs_data, bool notify)
  216. {
  217. struct cs_dbs_tuners *tuners;
  218. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  219. if (!tuners) {
  220. pr_err("%s: kzalloc failed\n", __func__);
  221. return -ENOMEM;
  222. }
  223. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  224. tuners->freq_step = DEF_FREQUENCY_STEP;
  225. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  226. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  227. dbs_data->ignore_nice_load = 0;
  228. dbs_data->tuners = tuners;
  229. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  230. jiffies_to_usecs(10);
  231. if (notify)
  232. cpufreq_register_notifier(&cs_cpufreq_notifier_block,
  233. CPUFREQ_TRANSITION_NOTIFIER);
  234. return 0;
  235. }
  236. static void cs_exit(struct dbs_data *dbs_data, bool notify)
  237. {
  238. if (notify)
  239. cpufreq_unregister_notifier(&cs_cpufreq_notifier_block,
  240. CPUFREQ_TRANSITION_NOTIFIER);
  241. kfree(dbs_data->tuners);
  242. }
  243. static void cs_start(struct cpufreq_policy *policy)
  244. {
  245. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  246. dbs_info->down_skip = 0;
  247. dbs_info->requested_freq = policy->cur;
  248. }
  249. static struct dbs_governor cs_dbs_gov = {
  250. .gov = {
  251. .name = "conservative",
  252. .governor = cpufreq_governor_dbs,
  253. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  254. .owner = THIS_MODULE,
  255. },
  256. .kobj_type = { .default_attrs = cs_attributes },
  257. .gov_dbs_timer = cs_dbs_timer,
  258. .alloc = cs_alloc,
  259. .free = cs_free,
  260. .init = cs_init,
  261. .exit = cs_exit,
  262. .start = cs_start,
  263. };
  264. #define CPU_FREQ_GOV_CONSERVATIVE (&cs_dbs_gov.gov)
  265. static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  266. void *data)
  267. {
  268. struct cpufreq_freqs *freq = data;
  269. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(freq->cpu);
  270. struct cs_policy_dbs_info *dbs_info;
  271. if (!policy)
  272. return 0;
  273. /* policy isn't governed by conservative governor */
  274. if (policy->governor != CPU_FREQ_GOV_CONSERVATIVE)
  275. return 0;
  276. dbs_info = to_dbs_info(policy->governor_data);
  277. /*
  278. * we only care if our internally tracked freq moves outside the 'valid'
  279. * ranges of frequency available to us otherwise we do not change it
  280. */
  281. if (dbs_info->requested_freq > policy->max
  282. || dbs_info->requested_freq < policy->min)
  283. dbs_info->requested_freq = freq->new;
  284. return 0;
  285. }
  286. static int __init cpufreq_gov_dbs_init(void)
  287. {
  288. return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE);
  289. }
  290. static void __exit cpufreq_gov_dbs_exit(void)
  291. {
  292. cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE);
  293. }
  294. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  295. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  296. "Low Latency Frequency Transition capable processors "
  297. "optimised for use in a battery environment");
  298. MODULE_LICENSE("GPL");
  299. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  300. struct cpufreq_governor *cpufreq_default_governor(void)
  301. {
  302. return CPU_FREQ_GOV_CONSERVATIVE;
  303. }
  304. fs_initcall(cpufreq_gov_dbs_init);
  305. #else
  306. module_init(cpufreq_gov_dbs_init);
  307. #endif
  308. module_exit(cpufreq_gov_dbs_exit);