cpufreq_ondemand.c 15 KB

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