cpufreq_ondemand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 "cpufreq_ondemand.h"
  18. /* On-demand governor macros */
  19. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  20. #define DEF_SAMPLING_DOWN_FACTOR (1)
  21. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  22. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  23. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  24. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  25. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  26. static struct od_ops od_ops;
  27. static unsigned int default_powersave_bias;
  28. /*
  29. * Not all CPUs want IO time to be accounted as busy; this depends on how
  30. * efficient idling at a higher frequency/voltage is.
  31. * Pavel Machek says this is not so for various generations of AMD and old
  32. * Intel systems.
  33. * Mike Chan (android.com) claims this is also not true for ARM.
  34. * Because of this, whitelist specific known (series) of CPUs by default, and
  35. * leave all others up to the user.
  36. */
  37. static int should_io_be_busy(void)
  38. {
  39. #if defined(CONFIG_X86)
  40. /*
  41. * For Intel, Core 2 (model 15) and later have an efficient idle.
  42. */
  43. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  44. boot_cpu_data.x86 == 6 &&
  45. boot_cpu_data.x86_model >= 15)
  46. return 1;
  47. #endif
  48. return 0;
  49. }
  50. /*
  51. * Find right freq to be set now with powersave_bias on.
  52. * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
  53. * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
  54. */
  55. static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
  56. unsigned int freq_next, unsigned int relation)
  57. {
  58. unsigned int freq_req, freq_reduc, freq_avg;
  59. unsigned int freq_hi, freq_lo;
  60. unsigned int index = 0;
  61. unsigned int delay_hi_us;
  62. struct policy_dbs_info *policy_dbs = policy->governor_data;
  63. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  64. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  65. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  66. if (!dbs_info->freq_table) {
  67. dbs_info->freq_lo = 0;
  68. dbs_info->freq_lo_delay_us = 0;
  69. return freq_next;
  70. }
  71. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  72. relation, &index);
  73. freq_req = dbs_info->freq_table[index].frequency;
  74. freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
  75. freq_avg = freq_req - freq_reduc;
  76. /* Find freq bounds for freq_avg in freq_table */
  77. index = 0;
  78. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  79. CPUFREQ_RELATION_H, &index);
  80. freq_lo = dbs_info->freq_table[index].frequency;
  81. index = 0;
  82. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  83. CPUFREQ_RELATION_L, &index);
  84. freq_hi = dbs_info->freq_table[index].frequency;
  85. /* Find out how long we have to be in hi and lo freqs */
  86. if (freq_hi == freq_lo) {
  87. dbs_info->freq_lo = 0;
  88. dbs_info->freq_lo_delay_us = 0;
  89. return freq_lo;
  90. }
  91. delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
  92. delay_hi_us += (freq_hi - freq_lo) / 2;
  93. delay_hi_us /= freq_hi - freq_lo;
  94. dbs_info->freq_hi_delay_us = delay_hi_us;
  95. dbs_info->freq_lo = freq_lo;
  96. dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
  97. return freq_hi;
  98. }
  99. static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
  100. {
  101. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  102. dbs_info->freq_table = cpufreq_frequency_get_table(policy->cpu);
  103. dbs_info->freq_lo = 0;
  104. }
  105. static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
  106. {
  107. struct policy_dbs_info *policy_dbs = policy->governor_data;
  108. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  109. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  110. if (od_tuners->powersave_bias)
  111. freq = od_ops.powersave_bias_target(policy, freq,
  112. CPUFREQ_RELATION_H);
  113. else if (policy->cur == policy->max)
  114. return;
  115. __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
  116. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  117. }
  118. /*
  119. * Every sampling_rate, we check, if current idle time is less than 20%
  120. * (default), then we try to increase frequency. Else, we adjust the frequency
  121. * proportional to load.
  122. */
  123. static void od_update(struct cpufreq_policy *policy)
  124. {
  125. struct policy_dbs_info *policy_dbs = policy->governor_data;
  126. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  127. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  128. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  129. unsigned int load = dbs_update(policy);
  130. dbs_info->freq_lo = 0;
  131. /* Check for frequency increase */
  132. if (load > dbs_data->up_threshold) {
  133. /* If switching to max speed, apply sampling_down_factor */
  134. if (policy->cur < policy->max)
  135. policy_dbs->rate_mult = dbs_data->sampling_down_factor;
  136. dbs_freq_increase(policy, policy->max);
  137. } else {
  138. /* Calculate the next frequency proportional to load */
  139. unsigned int freq_next, min_f, max_f;
  140. min_f = policy->cpuinfo.min_freq;
  141. max_f = policy->cpuinfo.max_freq;
  142. freq_next = min_f + load * (max_f - min_f) / 100;
  143. /* No longer fully busy, reset rate_mult */
  144. policy_dbs->rate_mult = 1;
  145. if (od_tuners->powersave_bias)
  146. freq_next = od_ops.powersave_bias_target(policy,
  147. freq_next,
  148. CPUFREQ_RELATION_L);
  149. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
  150. }
  151. }
  152. static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
  153. {
  154. struct policy_dbs_info *policy_dbs = policy->governor_data;
  155. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  156. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
  157. int sample_type = dbs_info->sample_type;
  158. /* Common NORMAL_SAMPLE setup */
  159. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  160. /*
  161. * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
  162. * it then.
  163. */
  164. if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
  165. __cpufreq_driver_target(policy, dbs_info->freq_lo,
  166. CPUFREQ_RELATION_H);
  167. return dbs_info->freq_lo_delay_us;
  168. }
  169. od_update(policy);
  170. if (dbs_info->freq_lo) {
  171. /* Setup timer for SUB_SAMPLE */
  172. dbs_info->sample_type = OD_SUB_SAMPLE;
  173. return dbs_info->freq_hi_delay_us;
  174. }
  175. return dbs_data->sampling_rate * policy_dbs->rate_mult;
  176. }
  177. /************************** sysfs interface ************************/
  178. static struct dbs_governor od_dbs_gov;
  179. static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
  180. size_t count)
  181. {
  182. unsigned int input;
  183. int ret;
  184. ret = sscanf(buf, "%u", &input);
  185. if (ret != 1)
  186. return -EINVAL;
  187. dbs_data->io_is_busy = !!input;
  188. /* we need to re-evaluate prev_cpu_idle */
  189. gov_update_cpu_data(dbs_data);
  190. return count;
  191. }
  192. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  193. size_t count)
  194. {
  195. unsigned int input;
  196. int ret;
  197. ret = sscanf(buf, "%u", &input);
  198. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  199. input < MIN_FREQUENCY_UP_THRESHOLD) {
  200. return -EINVAL;
  201. }
  202. dbs_data->up_threshold = input;
  203. return count;
  204. }
  205. static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
  206. const char *buf, size_t count)
  207. {
  208. struct policy_dbs_info *policy_dbs;
  209. unsigned int input;
  210. int ret;
  211. ret = sscanf(buf, "%u", &input);
  212. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  213. return -EINVAL;
  214. dbs_data->sampling_down_factor = input;
  215. /* Reset down sampling multiplier in case it was active */
  216. list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
  217. /*
  218. * Doing this without locking might lead to using different
  219. * rate_mult values in od_update() and od_dbs_timer().
  220. */
  221. mutex_lock(&policy_dbs->timer_mutex);
  222. policy_dbs->rate_mult = 1;
  223. mutex_unlock(&policy_dbs->timer_mutex);
  224. }
  225. return count;
  226. }
  227. static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
  228. const char *buf, size_t count)
  229. {
  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 dbs_data *dbs_data, const char *buf,
  246. size_t count)
  247. {
  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, &dbs_data->policy_dbs_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(min_sampling_rate);
  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. gov_attr_ro(min_sampling_rate);
  276. static struct attribute *od_attributes[] = {
  277. &min_sampling_rate.attr,
  278. &sampling_rate.attr,
  279. &up_threshold.attr,
  280. &sampling_down_factor.attr,
  281. &ignore_nice_load.attr,
  282. &powersave_bias.attr,
  283. &io_is_busy.attr,
  284. NULL
  285. };
  286. /************************** sysfs end ************************/
  287. static struct policy_dbs_info *od_alloc(void)
  288. {
  289. struct od_policy_dbs_info *dbs_info;
  290. dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
  291. return dbs_info ? &dbs_info->policy_dbs : NULL;
  292. }
  293. static void od_free(struct policy_dbs_info *policy_dbs)
  294. {
  295. kfree(to_dbs_info(policy_dbs));
  296. }
  297. static int od_init(struct dbs_data *dbs_data, bool notify)
  298. {
  299. struct od_dbs_tuners *tuners;
  300. u64 idle_time;
  301. int cpu;
  302. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  303. if (!tuners) {
  304. pr_err("%s: kzalloc failed\n", __func__);
  305. return -ENOMEM;
  306. }
  307. cpu = get_cpu();
  308. idle_time = get_cpu_idle_time_us(cpu, NULL);
  309. put_cpu();
  310. if (idle_time != -1ULL) {
  311. /* Idle micro accounting is supported. Use finer thresholds */
  312. dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  313. /*
  314. * In nohz/micro accounting case we set the minimum frequency
  315. * not depending on HZ, but fixed (very low). The deferred
  316. * timer might skip some samples if idle/sleeping as needed.
  317. */
  318. dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  319. } else {
  320. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  321. /* For correct statistics, we need 10 ticks for each measure */
  322. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  323. jiffies_to_usecs(10);
  324. }
  325. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  326. dbs_data->ignore_nice_load = 0;
  327. tuners->powersave_bias = default_powersave_bias;
  328. dbs_data->io_is_busy = should_io_be_busy();
  329. dbs_data->tuners = tuners;
  330. return 0;
  331. }
  332. static void od_exit(struct dbs_data *dbs_data, bool notify)
  333. {
  334. kfree(dbs_data->tuners);
  335. }
  336. static void od_start(struct cpufreq_policy *policy)
  337. {
  338. struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
  339. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  340. ondemand_powersave_bias_init(policy);
  341. }
  342. static struct od_ops od_ops = {
  343. .powersave_bias_target = generic_powersave_bias_target,
  344. };
  345. static struct dbs_governor od_dbs_gov = {
  346. .gov = {
  347. .name = "ondemand",
  348. .governor = cpufreq_governor_dbs,
  349. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  350. .owner = THIS_MODULE,
  351. },
  352. .kobj_type = { .default_attrs = od_attributes },
  353. .gov_dbs_timer = od_dbs_timer,
  354. .alloc = od_alloc,
  355. .free = od_free,
  356. .init = od_init,
  357. .exit = od_exit,
  358. .start = od_start,
  359. };
  360. #define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
  361. static void od_set_powersave_bias(unsigned int powersave_bias)
  362. {
  363. unsigned int cpu;
  364. cpumask_t done;
  365. default_powersave_bias = powersave_bias;
  366. cpumask_clear(&done);
  367. get_online_cpus();
  368. for_each_online_cpu(cpu) {
  369. struct cpufreq_policy *policy;
  370. struct policy_dbs_info *policy_dbs;
  371. struct dbs_data *dbs_data;
  372. struct od_dbs_tuners *od_tuners;
  373. if (cpumask_test_cpu(cpu, &done))
  374. continue;
  375. policy = cpufreq_cpu_get_raw(cpu);
  376. if (!policy || policy->governor != CPU_FREQ_GOV_ONDEMAND)
  377. continue;
  378. policy_dbs = policy->governor_data;
  379. if (!policy_dbs)
  380. continue;
  381. cpumask_or(&done, &done, policy->cpus);
  382. dbs_data = policy_dbs->dbs_data;
  383. od_tuners = dbs_data->tuners;
  384. od_tuners->powersave_bias = default_powersave_bias;
  385. }
  386. put_online_cpus();
  387. }
  388. void od_register_powersave_bias_handler(unsigned int (*f)
  389. (struct cpufreq_policy *, unsigned int, unsigned int),
  390. unsigned int powersave_bias)
  391. {
  392. od_ops.powersave_bias_target = f;
  393. od_set_powersave_bias(powersave_bias);
  394. }
  395. EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
  396. void od_unregister_powersave_bias_handler(void)
  397. {
  398. od_ops.powersave_bias_target = generic_powersave_bias_target;
  399. od_set_powersave_bias(0);
  400. }
  401. EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
  402. static int __init cpufreq_gov_dbs_init(void)
  403. {
  404. return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
  405. }
  406. static void __exit cpufreq_gov_dbs_exit(void)
  407. {
  408. cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
  409. }
  410. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  411. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  412. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  413. "Low Latency Frequency Transition capable processors");
  414. MODULE_LICENSE("GPL");
  415. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  416. struct cpufreq_governor *cpufreq_default_governor(void)
  417. {
  418. return CPU_FREQ_GOV_ONDEMAND;
  419. }
  420. fs_initcall(cpufreq_gov_dbs_init);
  421. #else
  422. module_init(cpufreq_gov_dbs_init);
  423. #endif
  424. module_exit(cpufreq_gov_dbs_exit);