cpufreq_ondemand.c 15 KB

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