cpufreq_conservative.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, CPUFREQ_RELATION_H);
  96. dbs_info->requested_freq = requested_freq;
  97. goto out;
  98. }
  99. /* if sampling_down_factor is active break out early */
  100. if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
  101. goto out;
  102. dbs_info->down_skip = 0;
  103. /* Check for frequency decrease */
  104. if (load < cs_tuners->down_threshold) {
  105. /*
  106. * if we cannot reduce the frequency anymore, break out early
  107. */
  108. if (requested_freq == policy->min)
  109. goto out;
  110. if (requested_freq > freq_step)
  111. requested_freq -= freq_step;
  112. else
  113. requested_freq = policy->min;
  114. __cpufreq_driver_target(policy, requested_freq, CPUFREQ_RELATION_L);
  115. dbs_info->requested_freq = requested_freq;
  116. }
  117. out:
  118. return dbs_data->sampling_rate;
  119. }
  120. /************************** sysfs interface ************************/
  121. static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
  122. const char *buf, size_t count)
  123. {
  124. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  125. unsigned int input;
  126. int ret;
  127. ret = sscanf(buf, "%u", &input);
  128. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  129. return -EINVAL;
  130. dbs_data->sampling_down_factor = input;
  131. return count;
  132. }
  133. static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
  134. const char *buf, size_t count)
  135. {
  136. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  137. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  138. unsigned int input;
  139. int ret;
  140. ret = sscanf(buf, "%u", &input);
  141. if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
  142. return -EINVAL;
  143. dbs_data->up_threshold = input;
  144. return count;
  145. }
  146. static ssize_t store_down_threshold(struct gov_attr_set *attr_set,
  147. const char *buf, size_t count)
  148. {
  149. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  150. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  151. unsigned int input;
  152. int ret;
  153. ret = sscanf(buf, "%u", &input);
  154. /* cannot be lower than 1 otherwise freq will not fall */
  155. if (ret != 1 || input < 1 || input > 100 ||
  156. input >= dbs_data->up_threshold)
  157. return -EINVAL;
  158. cs_tuners->down_threshold = input;
  159. return count;
  160. }
  161. static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
  162. const char *buf, size_t count)
  163. {
  164. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  165. unsigned int input;
  166. int ret;
  167. ret = sscanf(buf, "%u", &input);
  168. if (ret != 1)
  169. return -EINVAL;
  170. if (input > 1)
  171. input = 1;
  172. if (input == dbs_data->ignore_nice_load) /* nothing to do */
  173. return count;
  174. dbs_data->ignore_nice_load = input;
  175. /* we need to re-evaluate prev_cpu_idle */
  176. gov_update_cpu_data(dbs_data);
  177. return count;
  178. }
  179. static ssize_t store_freq_step(struct gov_attr_set *attr_set, const char *buf,
  180. size_t count)
  181. {
  182. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  183. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  184. unsigned int input;
  185. int ret;
  186. ret = sscanf(buf, "%u", &input);
  187. if (ret != 1)
  188. return -EINVAL;
  189. if (input > 100)
  190. input = 100;
  191. /*
  192. * no need to test here if freq_step is zero as the user might actually
  193. * want this, they would be crazy though :)
  194. */
  195. cs_tuners->freq_step = input;
  196. return count;
  197. }
  198. gov_show_one_common(sampling_rate);
  199. gov_show_one_common(sampling_down_factor);
  200. gov_show_one_common(up_threshold);
  201. gov_show_one_common(ignore_nice_load);
  202. gov_show_one(cs, down_threshold);
  203. gov_show_one(cs, freq_step);
  204. gov_attr_rw(sampling_rate);
  205. gov_attr_rw(sampling_down_factor);
  206. gov_attr_rw(up_threshold);
  207. gov_attr_rw(ignore_nice_load);
  208. gov_attr_rw(down_threshold);
  209. gov_attr_rw(freq_step);
  210. static struct attribute *cs_attributes[] = {
  211. &sampling_rate.attr,
  212. &sampling_down_factor.attr,
  213. &up_threshold.attr,
  214. &down_threshold.attr,
  215. &ignore_nice_load.attr,
  216. &freq_step.attr,
  217. NULL
  218. };
  219. /************************** sysfs end ************************/
  220. static struct policy_dbs_info *cs_alloc(void)
  221. {
  222. struct cs_policy_dbs_info *dbs_info;
  223. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  224. return dbs_info ? &dbs_info->policy_dbs : NULL;
  225. }
  226. static void cs_free(struct policy_dbs_info *policy_dbs)
  227. {
  228. kfree(to_dbs_info(policy_dbs));
  229. }
  230. static int cs_init(struct dbs_data *dbs_data)
  231. {
  232. struct cs_dbs_tuners *tuners;
  233. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  234. if (!tuners)
  235. return -ENOMEM;
  236. tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
  237. tuners->freq_step = DEF_FREQUENCY_STEP;
  238. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  239. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  240. dbs_data->ignore_nice_load = 0;
  241. dbs_data->tuners = tuners;
  242. return 0;
  243. }
  244. static void cs_exit(struct dbs_data *dbs_data)
  245. {
  246. kfree(dbs_data->tuners);
  247. }
  248. static void cs_start(struct cpufreq_policy *policy)
  249. {
  250. struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  251. dbs_info->down_skip = 0;
  252. dbs_info->requested_freq = policy->cur;
  253. }
  254. static struct dbs_governor cs_governor = {
  255. .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("conservative"),
  256. .kobj_type = { .default_attrs = cs_attributes },
  257. .gov_dbs_update = cs_dbs_update,
  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_governor.gov)
  265. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  266. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  267. "Low Latency Frequency Transition capable processors "
  268. "optimised for use in a battery environment");
  269. MODULE_LICENSE("GPL");
  270. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  271. struct cpufreq_governor *cpufreq_default_governor(void)
  272. {
  273. return &CPU_FREQ_GOV_CONSERVATIVE;
  274. }
  275. #endif
  276. cpufreq_governor_init(CPU_FREQ_GOV_CONSERVATIVE);
  277. cpufreq_governor_exit(CPU_FREQ_GOV_CONSERVATIVE);