cpufreq_ondemand.c 13 KB

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