cpufreq_ondemand.c 13 KB

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