cpufreq_ondemand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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(dbs_info->rate_mult);
  210. }
  211. }
  212. schedule_delayed_work_on(cpu, &dbs_info->cdbs.work, delay);
  213. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  214. }
  215. /************************** sysfs interface ************************/
  216. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  217. struct attribute *attr, char *buf)
  218. {
  219. return sprintf(buf, "%u\n", od_dbs_data.min_sampling_rate);
  220. }
  221. /**
  222. * update_sampling_rate - update sampling rate effective immediately if needed.
  223. * @new_rate: new sampling rate
  224. *
  225. * If new rate is smaller than the old, simply updaing
  226. * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
  227. * original sampling_rate was 1 second and the requested new sampling rate is 10
  228. * ms because the user needs immediate reaction from ondemand governor, but not
  229. * sure if higher frequency will be required or not, then, the governor may
  230. * change the sampling rate too late; up to 1 second later. Thus, if we are
  231. * reducing the sampling rate, we need to make the new value effective
  232. * immediately.
  233. */
  234. static void update_sampling_rate(unsigned int new_rate)
  235. {
  236. int cpu;
  237. od_tuners.sampling_rate = new_rate = max(new_rate,
  238. od_dbs_data.min_sampling_rate);
  239. for_each_online_cpu(cpu) {
  240. struct cpufreq_policy *policy;
  241. struct od_cpu_dbs_info_s *dbs_info;
  242. unsigned long next_sampling, appointed_at;
  243. policy = cpufreq_cpu_get(cpu);
  244. if (!policy)
  245. continue;
  246. dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
  247. cpufreq_cpu_put(policy);
  248. mutex_lock(&dbs_info->cdbs.timer_mutex);
  249. if (!delayed_work_pending(&dbs_info->cdbs.work)) {
  250. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  251. continue;
  252. }
  253. next_sampling = jiffies + usecs_to_jiffies(new_rate);
  254. appointed_at = dbs_info->cdbs.work.timer.expires;
  255. if (time_before(next_sampling, appointed_at)) {
  256. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  257. cancel_delayed_work_sync(&dbs_info->cdbs.work);
  258. mutex_lock(&dbs_info->cdbs.timer_mutex);
  259. schedule_delayed_work_on(dbs_info->cdbs.cpu,
  260. &dbs_info->cdbs.work,
  261. usecs_to_jiffies(new_rate));
  262. }
  263. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  264. }
  265. }
  266. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  267. const char *buf, size_t count)
  268. {
  269. unsigned int input;
  270. int ret;
  271. ret = sscanf(buf, "%u", &input);
  272. if (ret != 1)
  273. return -EINVAL;
  274. update_sampling_rate(input);
  275. return count;
  276. }
  277. static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
  278. const char *buf, size_t count)
  279. {
  280. unsigned int input;
  281. int ret;
  282. ret = sscanf(buf, "%u", &input);
  283. if (ret != 1)
  284. return -EINVAL;
  285. od_tuners.io_is_busy = !!input;
  286. return count;
  287. }
  288. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  289. const char *buf, size_t count)
  290. {
  291. unsigned int input;
  292. int ret;
  293. ret = sscanf(buf, "%u", &input);
  294. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  295. input < MIN_FREQUENCY_UP_THRESHOLD) {
  296. return -EINVAL;
  297. }
  298. od_tuners.up_threshold = input;
  299. return count;
  300. }
  301. static ssize_t store_sampling_down_factor(struct kobject *a,
  302. struct attribute *b, const char *buf, size_t count)
  303. {
  304. unsigned int input, j;
  305. int ret;
  306. ret = sscanf(buf, "%u", &input);
  307. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  308. return -EINVAL;
  309. od_tuners.sampling_down_factor = input;
  310. /* Reset down sampling multiplier in case it was active */
  311. for_each_online_cpu(j) {
  312. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  313. j);
  314. dbs_info->rate_mult = 1;
  315. }
  316. return count;
  317. }
  318. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  319. const char *buf, size_t count)
  320. {
  321. unsigned int input;
  322. int ret;
  323. unsigned int j;
  324. ret = sscanf(buf, "%u", &input);
  325. if (ret != 1)
  326. return -EINVAL;
  327. if (input > 1)
  328. input = 1;
  329. if (input == od_tuners.ignore_nice) { /* nothing to do */
  330. return count;
  331. }
  332. od_tuners.ignore_nice = input;
  333. /* we need to re-evaluate prev_cpu_idle */
  334. for_each_online_cpu(j) {
  335. struct od_cpu_dbs_info_s *dbs_info;
  336. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  337. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  338. &dbs_info->cdbs.prev_cpu_wall);
  339. if (od_tuners.ignore_nice)
  340. dbs_info->cdbs.prev_cpu_nice =
  341. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  342. }
  343. return count;
  344. }
  345. static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
  346. const char *buf, size_t count)
  347. {
  348. unsigned int input;
  349. int ret;
  350. ret = sscanf(buf, "%u", &input);
  351. if (ret != 1)
  352. return -EINVAL;
  353. if (input > 1000)
  354. input = 1000;
  355. od_tuners.powersave_bias = input;
  356. ondemand_powersave_bias_init();
  357. return count;
  358. }
  359. show_one(od, sampling_rate, sampling_rate);
  360. show_one(od, io_is_busy, io_is_busy);
  361. show_one(od, up_threshold, up_threshold);
  362. show_one(od, sampling_down_factor, sampling_down_factor);
  363. show_one(od, ignore_nice_load, ignore_nice);
  364. show_one(od, powersave_bias, powersave_bias);
  365. define_one_global_rw(sampling_rate);
  366. define_one_global_rw(io_is_busy);
  367. define_one_global_rw(up_threshold);
  368. define_one_global_rw(sampling_down_factor);
  369. define_one_global_rw(ignore_nice_load);
  370. define_one_global_rw(powersave_bias);
  371. define_one_global_ro(sampling_rate_min);
  372. static struct attribute *dbs_attributes[] = {
  373. &sampling_rate_min.attr,
  374. &sampling_rate.attr,
  375. &up_threshold.attr,
  376. &sampling_down_factor.attr,
  377. &ignore_nice_load.attr,
  378. &powersave_bias.attr,
  379. &io_is_busy.attr,
  380. NULL
  381. };
  382. static struct attribute_group od_attr_group = {
  383. .attrs = dbs_attributes,
  384. .name = "ondemand",
  385. };
  386. /************************** sysfs end ************************/
  387. define_get_cpu_dbs_routines(od_cpu_dbs_info);
  388. static struct od_ops od_ops = {
  389. .io_busy = should_io_be_busy,
  390. .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
  391. .powersave_bias_target = powersave_bias_target,
  392. .freq_increase = dbs_freq_increase,
  393. };
  394. static struct dbs_data od_dbs_data = {
  395. .governor = GOV_ONDEMAND,
  396. .attr_group = &od_attr_group,
  397. .tuners = &od_tuners,
  398. .get_cpu_cdbs = get_cpu_cdbs,
  399. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  400. .gov_dbs_timer = od_dbs_timer,
  401. .gov_check_cpu = od_check_cpu,
  402. .gov_ops = &od_ops,
  403. };
  404. static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  405. unsigned int event)
  406. {
  407. return cpufreq_governor_dbs(&od_dbs_data, policy, event);
  408. }
  409. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  410. static
  411. #endif
  412. struct cpufreq_governor cpufreq_gov_ondemand = {
  413. .name = "ondemand",
  414. .governor = od_cpufreq_governor_dbs,
  415. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  416. .owner = THIS_MODULE,
  417. };
  418. static int __init cpufreq_gov_dbs_init(void)
  419. {
  420. u64 idle_time;
  421. int cpu = get_cpu();
  422. mutex_init(&od_dbs_data.mutex);
  423. idle_time = get_cpu_idle_time_us(cpu, NULL);
  424. put_cpu();
  425. if (idle_time != -1ULL) {
  426. /* Idle micro accounting is supported. Use finer thresholds */
  427. od_tuners.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  428. od_tuners.down_differential = MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  429. /*
  430. * In nohz/micro accounting case we set the minimum frequency
  431. * not depending on HZ, but fixed (very low). The deferred
  432. * timer might skip some samples if idle/sleeping as needed.
  433. */
  434. od_dbs_data.min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  435. } else {
  436. /* For correct statistics, we need 10 ticks for each measure */
  437. od_dbs_data.min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  438. jiffies_to_usecs(10);
  439. }
  440. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  441. }
  442. static void __exit cpufreq_gov_dbs_exit(void)
  443. {
  444. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  445. }
  446. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  447. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  448. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  449. "Low Latency Frequency Transition capable processors");
  450. MODULE_LICENSE("GPL");
  451. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  452. fs_initcall(cpufreq_gov_dbs_init);
  453. #else
  454. module_init(cpufreq_gov_dbs_init);
  455. #endif
  456. module_exit(cpufreq_gov_dbs_exit);