cpufreq_ondemand.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * drivers/cpufreq/cpufreq_ondemand.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  6. * Jun Nakajima <jun.nakajima@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/cpu.h>
  14. #include <linux/percpu-defs.h>
  15. #include <linux/slab.h>
  16. #include <linux/tick.h>
  17. #include <linux/sched/cpufreq.h>
  18. #include "cpufreq_ondemand.h"
  19. /* On-demand governor macros */
  20. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  21. #define DEF_SAMPLING_DOWN_FACTOR (1)
  22. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  23. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  24. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  25. #define MIN_FREQUENCY_UP_THRESHOLD (1)
  26. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  27. static struct od_ops od_ops;
  28. static unsigned int default_powersave_bias;
  29. /*
  30. * Not all CPUs want IO time to be accounted as busy; this depends on how
  31. * efficient idling at a higher frequency/voltage is.
  32. * Pavel Machek says this is not so for various generations of AMD and old
  33. * Intel systems.
  34. * Mike Chan (android.com) claims this is also not true for ARM.
  35. * Because of this, whitelist specific known (series) of CPUs by default, and
  36. * leave all others up to the user.
  37. */
  38. static int should_io_be_busy(void)
  39. {
  40. #if defined(CONFIG_X86)
  41. /*
  42. * For Intel, Core 2 (model 15) and later have an efficient idle.
  43. */
  44. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  45. boot_cpu_data.x86 == 6 &&
  46. boot_cpu_data.x86_model >= 15)
  47. return 1;
  48. #endif
  49. return 0;
  50. }
  51. /*
  52. * Find right freq to be set now with powersave_bias on.
  53. * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
  54. * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
  55. */
  56. static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
  57. unsigned int freq_next, unsigned int relation)
  58. {
  59. unsigned int freq_req, freq_reduc, freq_avg;
  60. unsigned int freq_hi, freq_lo;
  61. unsigned int index;
  62. unsigned int delay_hi_us;
  63. struct policy_dbs_info *policy_dbs = policy->governor_data;
  64. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  65. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  66. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  67. struct cpufreq_frequency_table *freq_table = policy->freq_table;
  68. if (!freq_table) {
  69. dbs_info->freq_lo = 0;
  70. dbs_info->freq_lo_delay_us = 0;
  71. return freq_next;
  72. }
  73. index = cpufreq_frequency_table_target(policy, freq_next, relation);
  74. freq_req = freq_table[index].frequency;
  75. freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
  76. freq_avg = freq_req - freq_reduc;
  77. /* Find freq bounds for freq_avg in freq_table */
  78. index = cpufreq_table_find_index_h(policy, freq_avg);
  79. freq_lo = freq_table[index].frequency;
  80. index = cpufreq_table_find_index_l(policy, freq_avg);
  81. freq_hi = freq_table[index].frequency;
  82. /* Find out how long we have to be in hi and lo freqs */
  83. if (freq_hi == freq_lo) {
  84. dbs_info->freq_lo = 0;
  85. dbs_info->freq_lo_delay_us = 0;
  86. return freq_lo;
  87. }
  88. delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
  89. delay_hi_us += (freq_hi - freq_lo) / 2;
  90. delay_hi_us /= freq_hi - freq_lo;
  91. dbs_info->freq_hi_delay_us = delay_hi_us;
  92. dbs_info->freq_lo = freq_lo;
  93. dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
  94. return freq_hi;
  95. }
  96. static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
  97. {
  98. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  99. dbs_info->freq_lo = 0;
  100. }
  101. static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
  102. {
  103. struct policy_dbs_info *policy_dbs = policy->governor_data;
  104. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  105. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  106. if (od_tuners->powersave_bias)
  107. freq = od_ops.powersave_bias_target(policy, freq,
  108. CPUFREQ_RELATION_H);
  109. else if (policy->cur == policy->max)
  110. return;
  111. __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
  112. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  113. }
  114. /*
  115. * Every sampling_rate, we check, if current idle time is less than 20%
  116. * (default), then we try to increase frequency. Else, we adjust the frequency
  117. * proportional to load.
  118. */
  119. static void od_update(struct cpufreq_policy *policy)
  120. {
  121. struct policy_dbs_info *policy_dbs = policy->governor_data;
  122. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  123. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  124. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  125. unsigned int load = dbs_update(policy);
  126. dbs_info->freq_lo = 0;
  127. /* Check for frequency increase */
  128. if (load > dbs_data->up_threshold) {
  129. /* If switching to max speed, apply sampling_down_factor */
  130. if (policy->cur < policy->max)
  131. policy_dbs->rate_mult = dbs_data->sampling_down_factor;
  132. dbs_freq_increase(policy, policy->max);
  133. } else {
  134. /* Calculate the next frequency proportional to load */
  135. unsigned int freq_next, min_f, max_f;
  136. min_f = policy->cpuinfo.min_freq;
  137. max_f = policy->cpuinfo.max_freq;
  138. freq_next = min_f + load * (max_f - min_f) / 100;
  139. /* No longer fully busy, reset rate_mult */
  140. policy_dbs->rate_mult = 1;
  141. if (od_tuners->powersave_bias)
  142. freq_next = od_ops.powersave_bias_target(policy,
  143. freq_next,
  144. CPUFREQ_RELATION_L);
  145. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
  146. }
  147. }
  148. static unsigned int od_dbs_update(struct cpufreq_policy *policy)
  149. {
  150. struct policy_dbs_info *policy_dbs = policy->governor_data;
  151. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  152. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  153. int sample_type = dbs_info->sample_type;
  154. /* Common NORMAL_SAMPLE setup */
  155. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  156. /*
  157. * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
  158. * it then.
  159. */
  160. if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
  161. __cpufreq_driver_target(policy, dbs_info->freq_lo,
  162. CPUFREQ_RELATION_H);
  163. return dbs_info->freq_lo_delay_us;
  164. }
  165. od_update(policy);
  166. if (dbs_info->freq_lo) {
  167. /* Setup SUB_SAMPLE */
  168. dbs_info->sample_type = OD_SUB_SAMPLE;
  169. return dbs_info->freq_hi_delay_us;
  170. }
  171. return dbs_data->sampling_rate * policy_dbs->rate_mult;
  172. }
  173. /************************** sysfs interface ************************/
  174. static struct dbs_governor od_dbs_gov;
  175. static ssize_t store_io_is_busy(struct gov_attr_set *attr_set, const char *buf,
  176. size_t count)
  177. {
  178. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  179. unsigned int input;
  180. int ret;
  181. ret = sscanf(buf, "%u", &input);
  182. if (ret != 1)
  183. return -EINVAL;
  184. dbs_data->io_is_busy = !!input;
  185. /* we need to re-evaluate prev_cpu_idle */
  186. gov_update_cpu_data(dbs_data);
  187. return count;
  188. }
  189. static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
  190. const char *buf, size_t count)
  191. {
  192. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  193. unsigned int input;
  194. int ret;
  195. ret = sscanf(buf, "%u", &input);
  196. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  197. input < MIN_FREQUENCY_UP_THRESHOLD) {
  198. return -EINVAL;
  199. }
  200. dbs_data->up_threshold = input;
  201. return count;
  202. }
  203. static ssize_t store_sampling_down_factor(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. struct policy_dbs_info *policy_dbs;
  208. unsigned int input;
  209. int ret;
  210. ret = sscanf(buf, "%u", &input);
  211. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  212. return -EINVAL;
  213. dbs_data->sampling_down_factor = input;
  214. /* Reset down sampling multiplier in case it was active */
  215. list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
  216. /*
  217. * Doing this without locking might lead to using different
  218. * rate_mult values in od_update() and od_dbs_update().
  219. */
  220. mutex_lock(&policy_dbs->update_mutex);
  221. policy_dbs->rate_mult = 1;
  222. mutex_unlock(&policy_dbs->update_mutex);
  223. }
  224. return count;
  225. }
  226. static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
  227. const char *buf, size_t count)
  228. {
  229. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  230. unsigned int input;
  231. int ret;
  232. ret = sscanf(buf, "%u", &input);
  233. if (ret != 1)
  234. return -EINVAL;
  235. if (input > 1)
  236. input = 1;
  237. if (input == dbs_data->ignore_nice_load) { /* nothing to do */
  238. return count;
  239. }
  240. dbs_data->ignore_nice_load = input;
  241. /* we need to re-evaluate prev_cpu_idle */
  242. gov_update_cpu_data(dbs_data);
  243. return count;
  244. }
  245. static ssize_t store_powersave_bias(struct gov_attr_set *attr_set,
  246. const char *buf, size_t count)
  247. {
  248. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  249. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  250. struct policy_dbs_info *policy_dbs;
  251. unsigned int input;
  252. int ret;
  253. ret = sscanf(buf, "%u", &input);
  254. if (ret != 1)
  255. return -EINVAL;
  256. if (input > 1000)
  257. input = 1000;
  258. od_tuners->powersave_bias = input;
  259. list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
  260. ondemand_powersave_bias_init(policy_dbs->policy);
  261. return count;
  262. }
  263. gov_show_one_common(sampling_rate);
  264. gov_show_one_common(up_threshold);
  265. gov_show_one_common(sampling_down_factor);
  266. gov_show_one_common(ignore_nice_load);
  267. gov_show_one_common(io_is_busy);
  268. gov_show_one(od, powersave_bias);
  269. gov_attr_rw(sampling_rate);
  270. gov_attr_rw(io_is_busy);
  271. gov_attr_rw(up_threshold);
  272. gov_attr_rw(sampling_down_factor);
  273. gov_attr_rw(ignore_nice_load);
  274. gov_attr_rw(powersave_bias);
  275. static struct attribute *od_attributes[] = {
  276. &sampling_rate.attr,
  277. &up_threshold.attr,
  278. &sampling_down_factor.attr,
  279. &ignore_nice_load.attr,
  280. &powersave_bias.attr,
  281. &io_is_busy.attr,
  282. NULL
  283. };
  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_attrs = od_attributes },
  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_t done;
  347. default_powersave_bias = powersave_bias;
  348. cpumask_clear(&done);
  349. get_online_cpus();
  350. for_each_online_cpu(cpu) {
  351. struct cpufreq_policy *policy;
  352. struct policy_dbs_info *policy_dbs;
  353. struct dbs_data *dbs_data;
  354. struct od_dbs_tuners *od_tuners;
  355. if (cpumask_test_cpu(cpu, &done))
  356. continue;
  357. policy = cpufreq_cpu_get_raw(cpu);
  358. if (!policy || policy->governor != CPU_FREQ_GOV_ONDEMAND)
  359. continue;
  360. policy_dbs = policy->governor_data;
  361. if (!policy_dbs)
  362. continue;
  363. cpumask_or(&done, &done, policy->cpus);
  364. dbs_data = policy_dbs->dbs_data;
  365. od_tuners = dbs_data->tuners;
  366. od_tuners->powersave_bias = default_powersave_bias;
  367. }
  368. put_online_cpus();
  369. }
  370. void od_register_powersave_bias_handler(unsigned int (*f)
  371. (struct cpufreq_policy *, unsigned int, unsigned int),
  372. unsigned int powersave_bias)
  373. {
  374. od_ops.powersave_bias_target = f;
  375. od_set_powersave_bias(powersave_bias);
  376. }
  377. EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
  378. void od_unregister_powersave_bias_handler(void)
  379. {
  380. od_ops.powersave_bias_target = generic_powersave_bias_target;
  381. od_set_powersave_bias(0);
  382. }
  383. EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
  384. static int __init cpufreq_gov_dbs_init(void)
  385. {
  386. return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
  387. }
  388. static void __exit cpufreq_gov_dbs_exit(void)
  389. {
  390. cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
  391. }
  392. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  393. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  394. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  395. "Low Latency Frequency Transition capable processors");
  396. MODULE_LICENSE("GPL");
  397. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  398. struct cpufreq_governor *cpufreq_default_governor(void)
  399. {
  400. return CPU_FREQ_GOV_ONDEMAND;
  401. }
  402. fs_initcall(cpufreq_gov_dbs_init);
  403. #else
  404. module_init(cpufreq_gov_dbs_init);
  405. #endif
  406. module_exit(cpufreq_gov_dbs_exit);