cpufreq_ondemand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/cpufreq/cpufreq_ondemand.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. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/cpu.h>
  11. #include <linux/percpu-defs.h>
  12. #include <linux/slab.h>
  13. #include <linux/tick.h>
  14. #include <linux/sched/cpufreq.h>
  15. #include "cpufreq_ondemand.h"
  16. /* On-demand governor macros */
  17. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  18. #define DEF_SAMPLING_DOWN_FACTOR (1)
  19. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  20. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  21. #define MIN_FREQUENCY_UP_THRESHOLD (1)
  22. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  23. static struct od_ops od_ops;
  24. static unsigned int default_powersave_bias;
  25. /*
  26. * Find right freq to be set now with powersave_bias on.
  27. * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
  28. * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
  29. */
  30. static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
  31. unsigned int freq_next, unsigned int relation)
  32. {
  33. unsigned int freq_req, freq_reduc, freq_avg;
  34. unsigned int freq_hi, freq_lo;
  35. unsigned int index;
  36. unsigned int delay_hi_us;
  37. struct policy_dbs_info *policy_dbs = policy->governor_data;
  38. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  39. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  40. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  41. struct cpufreq_frequency_table *freq_table = policy->freq_table;
  42. if (!freq_table) {
  43. dbs_info->freq_lo = 0;
  44. dbs_info->freq_lo_delay_us = 0;
  45. return freq_next;
  46. }
  47. index = cpufreq_frequency_table_target(policy, freq_next, policy->min,
  48. policy->max, relation);
  49. freq_req = freq_table[index].frequency;
  50. freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
  51. freq_avg = freq_req - freq_reduc;
  52. /* Find freq bounds for freq_avg in freq_table */
  53. index = cpufreq_table_find_index_h(policy, freq_avg,
  54. relation & CPUFREQ_RELATION_E);
  55. freq_lo = freq_table[index].frequency;
  56. index = cpufreq_table_find_index_l(policy, freq_avg,
  57. relation & CPUFREQ_RELATION_E);
  58. freq_hi = freq_table[index].frequency;
  59. /* Find out how long we have to be in hi and lo freqs */
  60. if (freq_hi == freq_lo) {
  61. dbs_info->freq_lo = 0;
  62. dbs_info->freq_lo_delay_us = 0;
  63. return freq_lo;
  64. }
  65. delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
  66. delay_hi_us += (freq_hi - freq_lo) / 2;
  67. delay_hi_us /= freq_hi - freq_lo;
  68. dbs_info->freq_hi_delay_us = delay_hi_us;
  69. dbs_info->freq_lo = freq_lo;
  70. dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
  71. return freq_hi;
  72. }
  73. static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
  74. {
  75. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  76. dbs_info->freq_lo = 0;
  77. }
  78. static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
  79. {
  80. struct policy_dbs_info *policy_dbs = policy->governor_data;
  81. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  82. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  83. if (od_tuners->powersave_bias)
  84. freq = od_ops.powersave_bias_target(policy, freq,
  85. CPUFREQ_RELATION_HE);
  86. else if (policy->cur == policy->max)
  87. return;
  88. __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
  89. CPUFREQ_RELATION_LE : CPUFREQ_RELATION_HE);
  90. }
  91. /*
  92. * Every sampling_rate, we check, if current idle time is less than 20%
  93. * (default), then we try to increase frequency. Else, we adjust the frequency
  94. * proportional to load.
  95. */
  96. static void od_update(struct cpufreq_policy *policy)
  97. {
  98. struct policy_dbs_info *policy_dbs = policy->governor_data;
  99. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  100. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  101. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  102. unsigned int load = dbs_update(policy);
  103. dbs_info->freq_lo = 0;
  104. /* Check for frequency increase */
  105. if (load > dbs_data->up_threshold) {
  106. /* If switching to max speed, apply sampling_down_factor */
  107. if (policy->cur < policy->max)
  108. policy_dbs->rate_mult = dbs_data->sampling_down_factor;
  109. dbs_freq_increase(policy, policy->max);
  110. } else {
  111. /* Calculate the next frequency proportional to load */
  112. unsigned int freq_next, min_f, max_f;
  113. min_f = policy->cpuinfo.min_freq;
  114. max_f = policy->cpuinfo.max_freq;
  115. freq_next = min_f + load * (max_f - min_f) / 100;
  116. /* No longer fully busy, reset rate_mult */
  117. policy_dbs->rate_mult = 1;
  118. if (od_tuners->powersave_bias)
  119. freq_next = od_ops.powersave_bias_target(policy,
  120. freq_next,
  121. CPUFREQ_RELATION_LE);
  122. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_CE);
  123. }
  124. }
  125. static unsigned int od_dbs_update(struct cpufreq_policy *policy)
  126. {
  127. struct policy_dbs_info *policy_dbs = policy->governor_data;
  128. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  129. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  130. int sample_type = dbs_info->sample_type;
  131. /* Common NORMAL_SAMPLE setup */
  132. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  133. /*
  134. * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
  135. * it then.
  136. */
  137. if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
  138. __cpufreq_driver_target(policy, dbs_info->freq_lo,
  139. CPUFREQ_RELATION_HE);
  140. return dbs_info->freq_lo_delay_us;
  141. }
  142. od_update(policy);
  143. if (dbs_info->freq_lo) {
  144. /* Setup SUB_SAMPLE */
  145. dbs_info->sample_type = OD_SUB_SAMPLE;
  146. return dbs_info->freq_hi_delay_us;
  147. }
  148. return dbs_data->sampling_rate * policy_dbs->rate_mult;
  149. }
  150. /************************** sysfs interface ************************/
  151. static struct dbs_governor od_dbs_gov;
  152. static ssize_t io_is_busy_store(struct gov_attr_set *attr_set, const char *buf,
  153. size_t count)
  154. {
  155. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  156. unsigned int input;
  157. int ret;
  158. ret = sscanf(buf, "%u", &input);
  159. if (ret != 1)
  160. return -EINVAL;
  161. dbs_data->io_is_busy = !!input;
  162. /* we need to re-evaluate prev_cpu_idle */
  163. gov_update_cpu_data(dbs_data);
  164. return count;
  165. }
  166. static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
  167. const char *buf, size_t count)
  168. {
  169. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  170. unsigned int input;
  171. int ret;
  172. ret = sscanf(buf, "%u", &input);
  173. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  174. input < MIN_FREQUENCY_UP_THRESHOLD) {
  175. return -EINVAL;
  176. }
  177. dbs_data->up_threshold = input;
  178. return count;
  179. }
  180. static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
  181. const char *buf, size_t count)
  182. {
  183. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  184. struct policy_dbs_info *policy_dbs;
  185. unsigned int input;
  186. int ret;
  187. ret = sscanf(buf, "%u", &input);
  188. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  189. return -EINVAL;
  190. dbs_data->sampling_down_factor = input;
  191. /* Reset down sampling multiplier in case it was active */
  192. list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
  193. /*
  194. * Doing this without locking might lead to using different
  195. * rate_mult values in od_update() and od_dbs_update().
  196. */
  197. mutex_lock(&policy_dbs->update_mutex);
  198. policy_dbs->rate_mult = 1;
  199. mutex_unlock(&policy_dbs->update_mutex);
  200. }
  201. return count;
  202. }
  203. static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
  204. const char *buf, size_t count)
  205. {
  206. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  207. unsigned int input;
  208. int ret;
  209. ret = sscanf(buf, "%u", &input);
  210. if (ret != 1)
  211. return -EINVAL;
  212. if (input > 1)
  213. input = 1;
  214. if (input == dbs_data->ignore_nice_load) { /* nothing to do */
  215. return count;
  216. }
  217. dbs_data->ignore_nice_load = input;
  218. /* we need to re-evaluate prev_cpu_idle */
  219. gov_update_cpu_data(dbs_data);
  220. return count;
  221. }
  222. static ssize_t powersave_bias_store(struct gov_attr_set *attr_set,
  223. const char *buf, size_t count)
  224. {
  225. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  226. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  227. struct policy_dbs_info *policy_dbs;
  228. unsigned int input;
  229. int ret;
  230. ret = sscanf(buf, "%u", &input);
  231. if (ret != 1)
  232. return -EINVAL;
  233. if (input > 1000)
  234. input = 1000;
  235. od_tuners->powersave_bias = input;
  236. list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
  237. ondemand_powersave_bias_init(policy_dbs->policy);
  238. return count;
  239. }
  240. gov_show_one_common(sampling_rate);
  241. gov_show_one_common(up_threshold);
  242. gov_show_one_common(sampling_down_factor);
  243. gov_show_one_common(ignore_nice_load);
  244. gov_show_one_common(io_is_busy);
  245. gov_show_one(od, powersave_bias);
  246. gov_attr_rw(sampling_rate);
  247. gov_attr_rw(io_is_busy);
  248. gov_attr_rw(up_threshold);
  249. gov_attr_rw(sampling_down_factor);
  250. gov_attr_rw(ignore_nice_load);
  251. gov_attr_rw(powersave_bias);
  252. static struct attribute *od_attrs[] = {
  253. &sampling_rate.attr,
  254. &up_threshold.attr,
  255. &sampling_down_factor.attr,
  256. &ignore_nice_load.attr,
  257. &powersave_bias.attr,
  258. &io_is_busy.attr,
  259. NULL
  260. };
  261. ATTRIBUTE_GROUPS(od);
  262. /************************** sysfs end ************************/
  263. static struct policy_dbs_info *od_alloc(void)
  264. {
  265. struct od_policy_dbs_info *dbs_info;
  266. dbs_info = kzalloc_obj(*dbs_info);
  267. return dbs_info ? &dbs_info->policy_dbs : NULL;
  268. }
  269. static void od_free(struct policy_dbs_info *policy_dbs)
  270. {
  271. kfree(to_dbs_info(policy_dbs));
  272. }
  273. static int od_init(struct dbs_data *dbs_data)
  274. {
  275. struct od_dbs_tuners *tuners;
  276. tuners = kzalloc_obj(*tuners);
  277. if (!tuners)
  278. return -ENOMEM;
  279. if (tick_nohz_is_active()) {
  280. /* Idle micro accounting is supported. Use finer thresholds */
  281. dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  282. } else {
  283. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  284. }
  285. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  286. dbs_data->ignore_nice_load = 0;
  287. tuners->powersave_bias = default_powersave_bias;
  288. dbs_data->io_is_busy = od_should_io_be_busy();
  289. dbs_data->tuners = tuners;
  290. return 0;
  291. }
  292. static void od_exit(struct dbs_data *dbs_data)
  293. {
  294. kfree(dbs_data->tuners);
  295. }
  296. static void od_start(struct cpufreq_policy *policy)
  297. {
  298. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  299. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  300. ondemand_powersave_bias_init(policy);
  301. }
  302. static struct od_ops od_ops = {
  303. .powersave_bias_target = generic_powersave_bias_target,
  304. };
  305. static struct dbs_governor od_dbs_gov = {
  306. .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
  307. .kobj_type = { .default_groups = od_groups },
  308. .gov_dbs_update = od_dbs_update,
  309. .alloc = od_alloc,
  310. .free = od_free,
  311. .init = od_init,
  312. .exit = od_exit,
  313. .start = od_start,
  314. };
  315. #define CPU_FREQ_GOV_ONDEMAND (od_dbs_gov.gov)
  316. static void od_set_powersave_bias(unsigned int powersave_bias)
  317. {
  318. unsigned int cpu;
  319. cpumask_var_t done;
  320. if (!alloc_cpumask_var(&done, GFP_KERNEL))
  321. return;
  322. default_powersave_bias = powersave_bias;
  323. cpumask_clear(done);
  324. cpus_read_lock();
  325. for_each_online_cpu(cpu) {
  326. struct cpufreq_policy *policy;
  327. struct policy_dbs_info *policy_dbs;
  328. struct dbs_data *dbs_data;
  329. struct od_dbs_tuners *od_tuners;
  330. if (cpumask_test_cpu(cpu, done))
  331. continue;
  332. policy = cpufreq_cpu_get_raw(cpu);
  333. if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND)
  334. continue;
  335. policy_dbs = policy->governor_data;
  336. if (!policy_dbs)
  337. continue;
  338. cpumask_or(done, done, policy->cpus);
  339. dbs_data = policy_dbs->dbs_data;
  340. od_tuners = dbs_data->tuners;
  341. od_tuners->powersave_bias = default_powersave_bias;
  342. }
  343. cpus_read_unlock();
  344. free_cpumask_var(done);
  345. }
  346. void od_register_powersave_bias_handler(unsigned int (*f)
  347. (struct cpufreq_policy *, unsigned int, unsigned int),
  348. unsigned int powersave_bias)
  349. {
  350. od_ops.powersave_bias_target = f;
  351. od_set_powersave_bias(powersave_bias);
  352. }
  353. EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
  354. void od_unregister_powersave_bias_handler(void)
  355. {
  356. od_ops.powersave_bias_target = generic_powersave_bias_target;
  357. od_set_powersave_bias(0);
  358. }
  359. EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
  360. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  361. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  362. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  363. "Low Latency Frequency Transition capable processors");
  364. MODULE_LICENSE("GPL");
  365. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  366. struct cpufreq_governor *cpufreq_default_governor(void)
  367. {
  368. return &CPU_FREQ_GOV_ONDEMAND;
  369. }
  370. #endif
  371. cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND);
  372. cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND);