cpufreq_conservative.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/cpufreq/cpufreq_conservative.c
  4. *
  5. * Copyright (C) 2001 Russell King
  6. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  7. * Jun Nakajima <jun.nakajima@intel.com>
  8. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  9. */
  10. #include <linux/slab.h>
  11. #include "cpufreq_governor.h"
  12. struct cs_policy_dbs_info {
  13. struct policy_dbs_info policy_dbs;
  14. unsigned int down_skip;
  15. unsigned int requested_freq;
  16. };
  17. static inline struct cs_policy_dbs_info *to_dbs_info(struct policy_dbs_info *policy_dbs)
  18. {
  19. return container_of(policy_dbs, struct cs_policy_dbs_info, policy_dbs);
  20. }
  21. struct cs_dbs_tuners {
  22. unsigned int down_threshold;
  23. unsigned int freq_step;
  24. };
  25. /* Conservative governor macros */
  26. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  27. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  28. #define DEF_FREQUENCY_STEP (5)
  29. #define DEF_SAMPLING_DOWN_FACTOR (1)
  30. #define MAX_SAMPLING_DOWN_FACTOR (10)
  31. static inline unsigned int get_freq_step(struct cs_dbs_tuners *cs_tuners,
  32. struct cpufreq_policy *policy)
  33. {
  34. unsigned int freq_step = (cs_tuners->freq_step * policy->max) / 100;
  35. /* max freq cannot be less than 100. But who knows... */
  36. if (unlikely(freq_step == 0))
  37. freq_step = DEF_FREQUENCY_STEP;
  38. return freq_step;
  39. }
  40. /*
  41. * Every sampling_rate, we check, if current idle time is less than 20%
  42. * (default), then we try to increase frequency. Every sampling_rate *
  43. * sampling_down_factor, we check, if current idle time is more than 80%
  44. * (default), then we try to decrease frequency
  45. *
  46. * Frequency updates happen at minimum steps of 5% (default) of maximum
  47. * frequency
  48. */
  49. static unsigned int cs_dbs_update(struct cpufreq_policy *policy)
  50. {
  51. struct policy_dbs_info *policy_dbs = policy->governor_data;
  52. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  53. unsigned int requested_freq = dbs_info->requested_freq;
  54. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  55. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  56. unsigned int load = dbs_update(policy);
  57. unsigned int freq_step;
  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. /*
  65. * If requested_freq is out of range, it is likely that the limits
  66. * changed in the meantime, so fall back to current frequency in that
  67. * case.
  68. */
  69. if (requested_freq > policy->max || requested_freq < policy->min) {
  70. requested_freq = policy->cur;
  71. dbs_info->requested_freq = requested_freq;
  72. }
  73. freq_step = get_freq_step(cs_tuners, policy);
  74. /*
  75. * Decrease requested_freq one freq_step for each idle period that
  76. * we didn't update the frequency.
  77. */
  78. if (policy_dbs->idle_periods < UINT_MAX) {
  79. unsigned int freq_steps = policy_dbs->idle_periods * freq_step;
  80. if (requested_freq > policy->min + freq_steps)
  81. requested_freq -= freq_steps;
  82. else
  83. requested_freq = policy->min;
  84. policy_dbs->idle_periods = UINT_MAX;
  85. }
  86. /* Check for frequency increase */
  87. if (load > dbs_data->up_threshold) {
  88. dbs_info->down_skip = 0;
  89. /* if we are already at full speed then break out early */
  90. if (requested_freq == policy->max)
  91. goto out;
  92. requested_freq += freq_step;
  93. if (requested_freq > policy->max)
  94. requested_freq = policy->max;
  95. __cpufreq_driver_target(policy, requested_freq,
  96. CPUFREQ_RELATION_HE);
  97. dbs_info->requested_freq = requested_freq;
  98. goto out;
  99. }
  100. /* if sampling_down_factor is active break out early */
  101. if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
  102. goto out;
  103. dbs_info->down_skip = 0;
  104. /* Check for frequency decrease */
  105. if (load < cs_tuners->down_threshold) {
  106. /*
  107. * if we cannot reduce the frequency anymore, break out early
  108. */
  109. if (requested_freq == policy->min)
  110. goto out;
  111. if (requested_freq > freq_step)
  112. requested_freq -= freq_step;
  113. else
  114. requested_freq = policy->min;
  115. __cpufreq_driver_target(policy, requested_freq,
  116. CPUFREQ_RELATION_LE);
  117. dbs_info->requested_freq = requested_freq;
  118. }
  119. out:
  120. return dbs_data->sampling_rate;
  121. }
  122. /************************** sysfs interface ************************/
  123. static ssize_t sampling_down_factor_store(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. unsigned int input;
  128. int ret;
  129. ret = sscanf(buf, "%u", &input);
  130. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  131. return -EINVAL;
  132. dbs_data->sampling_down_factor = input;
  133. return count;
  134. }
  135. static ssize_t up_threshold_store(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. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  144. return -EINVAL;
  145. dbs_data->up_threshold = input;
  146. return count;
  147. }
  148. static ssize_t down_threshold_store(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. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  153. unsigned int input;
  154. int ret;
  155. ret = sscanf(buf, "%u", &input);
  156. /* cannot be lower than 1 otherwise freq will not fall */
  157. if (ret != 1 || input < 1 || input >= dbs_data->up_threshold)
  158. return -EINVAL;
  159. cs_tuners->down_threshold = input;
  160. return count;
  161. }
  162. static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
  163. const char *buf, size_t count)
  164. {
  165. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  166. unsigned int input;
  167. int ret;
  168. ret = sscanf(buf, "%u", &input);
  169. if (ret != 1)
  170. return -EINVAL;
  171. if (input > 1)
  172. input = 1;
  173. if (input == dbs_data->ignore_nice_load) /* nothing to do */
  174. return count;
  175. dbs_data->ignore_nice_load = input;
  176. /* we need to re-evaluate prev_cpu_idle */
  177. gov_update_cpu_data(dbs_data);
  178. return count;
  179. }
  180. static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
  181. size_t count)
  182. {
  183. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  184. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  185. unsigned int input;
  186. int ret;
  187. ret = sscanf(buf, "%u", &input);
  188. if (ret != 1)
  189. return -EINVAL;
  190. if (input > 100)
  191. input = 100;
  192. /*
  193. * no need to test here if freq_step is zero as the user might actually
  194. * want this, they would be crazy though :)
  195. */
  196. cs_tuners->freq_step = input;
  197. return count;
  198. }
  199. gov_show_one_common(sampling_rate);
  200. gov_show_one_common(sampling_down_factor);
  201. gov_show_one_common(up_threshold);
  202. gov_show_one_common(ignore_nice_load);
  203. gov_show_one(cs, down_threshold);
  204. gov_show_one(cs, freq_step);
  205. gov_attr_rw(sampling_rate);
  206. gov_attr_rw(sampling_down_factor);
  207. gov_attr_rw(up_threshold);
  208. gov_attr_rw(ignore_nice_load);
  209. gov_attr_rw(down_threshold);
  210. gov_attr_rw(freq_step);
  211. static struct attribute *cs_attrs[] = {
  212. &sampling_rate.attr,
  213. &sampling_down_factor.attr,
  214. &up_threshold.attr,
  215. &down_threshold.attr,
  216. &ignore_nice_load.attr,
  217. &freq_step.attr,
  218. NULL
  219. };
  220. ATTRIBUTE_GROUPS(cs);
  221. /************************** sysfs end ************************/
  222. static struct policy_dbs_info *cs_alloc(void)
  223. {
  224. struct cs_policy_dbs_info *dbs_info;
  225. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  226. return dbs_info ? &dbs_info->policy_dbs : NULL;
  227. }
  228. static void cs_free(struct policy_dbs_info *policy_dbs)
  229. {
  230. kfree(to_dbs_info(policy_dbs));
  231. }
  232. static int cs_init(struct dbs_data *dbs_data)
  233. {
  234. struct cs_dbs_tuners *tuners;
  235. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  236. if (!tuners)
  237. return -ENOMEM;
  238. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  239. tuners->freq_step = DEF_FREQUENCY_STEP;
  240. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  241. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  242. dbs_data->ignore_nice_load = 0;
  243. dbs_data->tuners = tuners;
  244. return 0;
  245. }
  246. static void cs_exit(struct dbs_data *dbs_data)
  247. {
  248. kfree(dbs_data->tuners);
  249. }
  250. static void cs_start(struct cpufreq_policy *policy)
  251. {
  252. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  253. dbs_info->down_skip = 0;
  254. dbs_info->requested_freq = policy->cur;
  255. }
  256. static struct dbs_governor cs_governor = {
  257. .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("conservative"),
  258. .kobj_type = { .default_groups = cs_groups },
  259. .gov_dbs_update = cs_dbs_update,
  260. .alloc = cs_alloc,
  261. .free = cs_free,
  262. .init = cs_init,
  263. .exit = cs_exit,
  264. .start = cs_start,
  265. };
  266. #define CPU_FREQ_GOV_CONSERVATIVE (cs_governor.gov)
  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. #endif
  278. cpufreq_governor_init(CPU_FREQ_GOV_CONSERVATIVE);
  279. cpufreq_governor_exit(CPU_FREQ_GOV_CONSERVATIVE);