cpufreq_ondemand.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/cpufreq.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/kobject.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/percpu-defs.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/tick.h>
  23. #include <linux/types.h>
  24. #include "cpufreq_governor.h"
  25. /* On-demand governor macros */
  26. #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
  27. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  28. #define DEF_SAMPLING_DOWN_FACTOR (1)
  29. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  30. #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
  31. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  32. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  33. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  34. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  35. static struct dbs_data od_dbs_data;
  36. static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
  37. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  38. static struct cpufreq_governor cpufreq_gov_ondemand;
  39. #endif
  40. static struct od_dbs_tuners od_tuners = {
  41. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  42. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  43. .adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD -
  44. DEF_FREQUENCY_DOWN_DIFFERENTIAL,
  45. .ignore_nice = 0,
  46. .powersave_bias = 0,
  47. };
  48. static void ondemand_powersave_bias_init_cpu(int cpu)
  49. {
  50. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  51. dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
  52. dbs_info->freq_lo = 0;
  53. }
  54. /*
  55. * Not all CPUs want IO time to be accounted as busy; this depends on how
  56. * efficient idling at a higher frequency/voltage is.
  57. * Pavel Machek says this is not so for various generations of AMD and old
  58. * Intel systems.
  59. * Mike Chan (android.com) claims this is also not true for ARM.
  60. * Because of this, whitelist specific known (series) of CPUs by default, and
  61. * leave all others up to the user.
  62. */
  63. static int should_io_be_busy(void)
  64. {
  65. #if defined(CONFIG_X86)
  66. /*
  67. * For Intel, Core 2 (model 15) and later have an efficient idle.
  68. */
  69. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  70. boot_cpu_data.x86 == 6 &&
  71. boot_cpu_data.x86_model >= 15)
  72. return 1;
  73. #endif
  74. return 0;
  75. }
  76. /*
  77. * Find right freq to be set now with powersave_bias on.
  78. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  79. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  80. */
  81. static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
  82. unsigned int freq_next, unsigned int relation)
  83. {
  84. unsigned int freq_req, freq_reduc, freq_avg;
  85. unsigned int freq_hi, freq_lo;
  86. unsigned int index = 0;
  87. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  88. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  89. policy->cpu);
  90. if (!dbs_info->freq_table) {
  91. dbs_info->freq_lo = 0;
  92. dbs_info->freq_lo_jiffies = 0;
  93. return freq_next;
  94. }
  95. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  96. relation, &index);
  97. freq_req = dbs_info->freq_table[index].frequency;
  98. freq_reduc = freq_req * od_tuners.powersave_bias / 1000;
  99. freq_avg = freq_req - freq_reduc;
  100. /* Find freq bounds for freq_avg in freq_table */
  101. index = 0;
  102. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  103. CPUFREQ_RELATION_H, &index);
  104. freq_lo = dbs_info->freq_table[index].frequency;
  105. index = 0;
  106. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  107. CPUFREQ_RELATION_L, &index);
  108. freq_hi = dbs_info->freq_table[index].frequency;
  109. /* Find out how long we have to be in hi and lo freqs */
  110. if (freq_hi == freq_lo) {
  111. dbs_info->freq_lo = 0;
  112. dbs_info->freq_lo_jiffies = 0;
  113. return freq_lo;
  114. }
  115. jiffies_total = usecs_to_jiffies(od_tuners.sampling_rate);
  116. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  117. jiffies_hi += ((freq_hi - freq_lo) / 2);
  118. jiffies_hi /= (freq_hi - freq_lo);
  119. jiffies_lo = jiffies_total - jiffies_hi;
  120. dbs_info->freq_lo = freq_lo;
  121. dbs_info->freq_lo_jiffies = jiffies_lo;
  122. dbs_info->freq_hi_jiffies = jiffies_hi;
  123. return freq_hi;
  124. }
  125. static void ondemand_powersave_bias_init(void)
  126. {
  127. int i;
  128. for_each_online_cpu(i) {
  129. ondemand_powersave_bias_init_cpu(i);
  130. }
  131. }
  132. static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
  133. {
  134. if (od_tuners.powersave_bias)
  135. freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
  136. else if (p->cur == p->max)
  137. return;
  138. __cpufreq_driver_target(p, freq, od_tuners.powersave_bias ?
  139. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  140. }
  141. /*
  142. * Every sampling_rate, we check, if current idle time is less than 20%
  143. * (default), then we try to increase frequency. Every sampling_rate, we look
  144. * for the lowest frequency which can sustain the load while keeping idle time
  145. * over 30%. If such a frequency exist, we try to decrease to this frequency.
  146. *
  147. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  148. * happens at minimum steps of 5% (default) of current frequency
  149. */
  150. static void od_check_cpu(int cpu, unsigned int load_freq)
  151. {
  152. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  153. struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
  154. dbs_info->freq_lo = 0;
  155. /* Check for frequency increase */
  156. if (load_freq > od_tuners.up_threshold * policy->cur) {
  157. /* If switching to max speed, apply sampling_down_factor */
  158. if (policy->cur < policy->max)
  159. dbs_info->rate_mult =
  160. od_tuners.sampling_down_factor;
  161. dbs_freq_increase(policy, policy->max);
  162. return;
  163. }
  164. /* Check for frequency decrease */
  165. /* if we cannot reduce the frequency anymore, break out early */
  166. if (policy->cur == policy->min)
  167. return;
  168. /*
  169. * The optimal frequency is the frequency that is the lowest that can
  170. * support the current CPU usage without triggering the up policy. To be
  171. * safe, we focus 10 points under the threshold.
  172. */
  173. if (load_freq < od_tuners.adj_up_threshold * policy->cur) {
  174. unsigned int freq_next;
  175. freq_next = load_freq / od_tuners.adj_up_threshold;
  176. /* No longer fully busy, reset rate_mult */
  177. dbs_info->rate_mult = 1;
  178. if (freq_next < policy->min)
  179. freq_next = policy->min;
  180. if (!od_tuners.powersave_bias) {
  181. __cpufreq_driver_target(policy, freq_next,
  182. CPUFREQ_RELATION_L);
  183. } else {
  184. int freq = powersave_bias_target(policy, freq_next,
  185. CPUFREQ_RELATION_L);
  186. __cpufreq_driver_target(policy, freq,
  187. CPUFREQ_RELATION_L);
  188. }
  189. }
  190. }
  191. static void od_dbs_timer(struct work_struct *work)
  192. {
  193. struct delayed_work *dw = to_delayed_work(work);
  194. struct od_cpu_dbs_info_s *dbs_info =
  195. container_of(work, struct od_cpu_dbs_info_s, cdbs.work.work);
  196. unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
  197. struct od_cpu_dbs_info_s *core_dbs_info = &per_cpu(od_cpu_dbs_info,
  198. cpu);
  199. int delay, sample_type = core_dbs_info->sample_type;
  200. bool eval_load;
  201. mutex_lock(&core_dbs_info->cdbs.timer_mutex);
  202. eval_load = need_load_eval(&core_dbs_info->cdbs,
  203. od_tuners.sampling_rate);
  204. /* Common NORMAL_SAMPLE setup */
  205. core_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  206. if (sample_type == OD_SUB_SAMPLE) {
  207. delay = core_dbs_info->freq_lo_jiffies;
  208. if (eval_load)
  209. __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
  210. core_dbs_info->freq_lo,
  211. CPUFREQ_RELATION_H);
  212. } else {
  213. if (eval_load)
  214. dbs_check_cpu(&od_dbs_data, cpu);
  215. if (core_dbs_info->freq_lo) {
  216. /* Setup timer for SUB_SAMPLE */
  217. core_dbs_info->sample_type = OD_SUB_SAMPLE;
  218. delay = core_dbs_info->freq_hi_jiffies;
  219. } else {
  220. delay = delay_for_sampling_rate(od_tuners.sampling_rate
  221. * core_dbs_info->rate_mult);
  222. }
  223. }
  224. schedule_delayed_work_on(smp_processor_id(), dw, delay);
  225. mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
  226. }
  227. /************************** sysfs interface ************************/
  228. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  229. struct attribute *attr, char *buf)
  230. {
  231. return sprintf(buf, "%u\n", od_dbs_data.min_sampling_rate);
  232. }
  233. /**
  234. * update_sampling_rate - update sampling rate effective immediately if needed.
  235. * @new_rate: new sampling rate
  236. *
  237. * If new rate is smaller than the old, simply updating
  238. * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
  239. * original sampling_rate was 1 second and the requested new sampling rate is 10
  240. * ms because the user needs immediate reaction from ondemand governor, but not
  241. * sure if higher frequency will be required or not, then, the governor may
  242. * change the sampling rate too late; up to 1 second later. Thus, if we are
  243. * reducing the sampling rate, we need to make the new value effective
  244. * immediately.
  245. */
  246. static void update_sampling_rate(unsigned int new_rate)
  247. {
  248. int cpu;
  249. od_tuners.sampling_rate = new_rate = max(new_rate,
  250. od_dbs_data.min_sampling_rate);
  251. for_each_online_cpu(cpu) {
  252. struct cpufreq_policy *policy;
  253. struct od_cpu_dbs_info_s *dbs_info;
  254. unsigned long next_sampling, appointed_at;
  255. policy = cpufreq_cpu_get(cpu);
  256. if (!policy)
  257. continue;
  258. if (policy->governor != &cpufreq_gov_ondemand) {
  259. cpufreq_cpu_put(policy);
  260. continue;
  261. }
  262. dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  263. cpufreq_cpu_put(policy);
  264. mutex_lock(&dbs_info->cdbs.timer_mutex);
  265. if (!delayed_work_pending(&dbs_info->cdbs.work)) {
  266. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  267. continue;
  268. }
  269. next_sampling = jiffies + usecs_to_jiffies(new_rate);
  270. appointed_at = dbs_info->cdbs.work.timer.expires;
  271. if (time_before(next_sampling, appointed_at)) {
  272. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  273. cancel_delayed_work_sync(&dbs_info->cdbs.work);
  274. mutex_lock(&dbs_info->cdbs.timer_mutex);
  275. schedule_delayed_work_on(cpu, &dbs_info->cdbs.work,
  276. usecs_to_jiffies(new_rate));
  277. }
  278. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  279. }
  280. }
  281. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  282. const char *buf, size_t count)
  283. {
  284. unsigned int input;
  285. int ret;
  286. ret = sscanf(buf, "%u", &input);
  287. if (ret != 1)
  288. return -EINVAL;
  289. update_sampling_rate(input);
  290. return count;
  291. }
  292. static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
  293. const char *buf, size_t count)
  294. {
  295. unsigned int input;
  296. int ret;
  297. ret = sscanf(buf, "%u", &input);
  298. if (ret != 1)
  299. return -EINVAL;
  300. od_tuners.io_is_busy = !!input;
  301. return count;
  302. }
  303. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  304. const char *buf, size_t count)
  305. {
  306. unsigned int input;
  307. int ret;
  308. ret = sscanf(buf, "%u", &input);
  309. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  310. input < MIN_FREQUENCY_UP_THRESHOLD) {
  311. return -EINVAL;
  312. }
  313. /* Calculate the new adj_up_threshold */
  314. od_tuners.adj_up_threshold += input;
  315. od_tuners.adj_up_threshold -= od_tuners.up_threshold;
  316. od_tuners.up_threshold = input;
  317. return count;
  318. }
  319. static ssize_t store_sampling_down_factor(struct kobject *a,
  320. struct attribute *b, const char *buf, size_t count)
  321. {
  322. unsigned int input, j;
  323. int ret;
  324. ret = sscanf(buf, "%u", &input);
  325. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  326. return -EINVAL;
  327. od_tuners.sampling_down_factor = input;
  328. /* Reset down sampling multiplier in case it was active */
  329. for_each_online_cpu(j) {
  330. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  331. j);
  332. dbs_info->rate_mult = 1;
  333. }
  334. return count;
  335. }
  336. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  337. const char *buf, size_t count)
  338. {
  339. unsigned int input;
  340. int ret;
  341. unsigned int j;
  342. ret = sscanf(buf, "%u", &input);
  343. if (ret != 1)
  344. return -EINVAL;
  345. if (input > 1)
  346. input = 1;
  347. if (input == od_tuners.ignore_nice) { /* nothing to do */
  348. return count;
  349. }
  350. od_tuners.ignore_nice = input;
  351. /* we need to re-evaluate prev_cpu_idle */
  352. for_each_online_cpu(j) {
  353. struct od_cpu_dbs_info_s *dbs_info;
  354. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  355. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  356. &dbs_info->cdbs.prev_cpu_wall);
  357. if (od_tuners.ignore_nice)
  358. dbs_info->cdbs.prev_cpu_nice =
  359. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  360. }
  361. return count;
  362. }
  363. static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
  364. const char *buf, size_t count)
  365. {
  366. unsigned int input;
  367. int ret;
  368. ret = sscanf(buf, "%u", &input);
  369. if (ret != 1)
  370. return -EINVAL;
  371. if (input > 1000)
  372. input = 1000;
  373. od_tuners.powersave_bias = input;
  374. ondemand_powersave_bias_init();
  375. return count;
  376. }
  377. show_one(od, sampling_rate, sampling_rate);
  378. show_one(od, io_is_busy, io_is_busy);
  379. show_one(od, up_threshold, up_threshold);
  380. show_one(od, sampling_down_factor, sampling_down_factor);
  381. show_one(od, ignore_nice_load, ignore_nice);
  382. show_one(od, powersave_bias, powersave_bias);
  383. define_one_global_rw(sampling_rate);
  384. define_one_global_rw(io_is_busy);
  385. define_one_global_rw(up_threshold);
  386. define_one_global_rw(sampling_down_factor);
  387. define_one_global_rw(ignore_nice_load);
  388. define_one_global_rw(powersave_bias);
  389. define_one_global_ro(sampling_rate_min);
  390. static struct attribute *dbs_attributes[] = {
  391. &sampling_rate_min.attr,
  392. &sampling_rate.attr,
  393. &up_threshold.attr,
  394. &sampling_down_factor.attr,
  395. &ignore_nice_load.attr,
  396. &powersave_bias.attr,
  397. &io_is_busy.attr,
  398. NULL
  399. };
  400. static struct attribute_group od_attr_group = {
  401. .attrs = dbs_attributes,
  402. .name = "ondemand",
  403. };
  404. /************************** sysfs end ************************/
  405. define_get_cpu_dbs_routines(od_cpu_dbs_info);
  406. static struct od_ops od_ops = {
  407. .io_busy = should_io_be_busy,
  408. .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
  409. .powersave_bias_target = powersave_bias_target,
  410. .freq_increase = dbs_freq_increase,
  411. };
  412. static struct dbs_data od_dbs_data = {
  413. .governor = GOV_ONDEMAND,
  414. .attr_group = &od_attr_group,
  415. .tuners = &od_tuners,
  416. .get_cpu_cdbs = get_cpu_cdbs,
  417. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  418. .gov_dbs_timer = od_dbs_timer,
  419. .gov_check_cpu = od_check_cpu,
  420. .gov_ops = &od_ops,
  421. };
  422. static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  423. unsigned int event)
  424. {
  425. return cpufreq_governor_dbs(&od_dbs_data, policy, event);
  426. }
  427. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  428. static
  429. #endif
  430. struct cpufreq_governor cpufreq_gov_ondemand = {
  431. .name = "ondemand",
  432. .governor = od_cpufreq_governor_dbs,
  433. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  434. .owner = THIS_MODULE,
  435. };
  436. static int __init cpufreq_gov_dbs_init(void)
  437. {
  438. u64 idle_time;
  439. int cpu = get_cpu();
  440. mutex_init(&od_dbs_data.mutex);
  441. idle_time = get_cpu_idle_time_us(cpu, NULL);
  442. put_cpu();
  443. if (idle_time != -1ULL) {
  444. /* Idle micro accounting is supported. Use finer thresholds */
  445. od_tuners.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  446. od_tuners.adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD -
  447. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  448. /*
  449. * In nohz/micro accounting case we set the minimum frequency
  450. * not depending on HZ, but fixed (very low). The deferred
  451. * timer might skip some samples if idle/sleeping as needed.
  452. */
  453. od_dbs_data.min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  454. } else {
  455. /* For correct statistics, we need 10 ticks for each measure */
  456. od_dbs_data.min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  457. jiffies_to_usecs(10);
  458. }
  459. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  460. }
  461. static void __exit cpufreq_gov_dbs_exit(void)
  462. {
  463. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  464. }
  465. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  466. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  467. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  468. "Low Latency Frequency Transition capable processors");
  469. MODULE_LICENSE("GPL");
  470. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  471. fs_initcall(cpufreq_gov_dbs_init);
  472. #else
  473. module_init(cpufreq_gov_dbs_init);
  474. #endif
  475. module_exit(cpufreq_gov_dbs_exit);