cpufreq_conservative.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * drivers/cpufreq/cpufreq_conservative.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. * (C) 2004 Alexander Clouter <alex-kernel@digriz.org.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/smp.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/ctype.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/types.h>
  22. #include <linux/fs.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/cpu.h>
  25. #include <linux/kmod.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/percpu.h>
  30. #include <linux/mutex.h>
  31. /*
  32. * dbs is used in this file as a shortform for demandbased switching
  33. * It helps to keep variable names smaller, simpler
  34. */
  35. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  36. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  37. /*
  38. * The polling frequency of this governor depends on the capability of
  39. * the processor. Default polling frequency is 1000 times the transition
  40. * latency of the processor. The governor will work on any processor with
  41. * transition latency <= 10mS, using appropriate sampling
  42. * rate.
  43. * For CPUs with transition latency > 10mS (mostly drivers
  44. * with CPUFREQ_ETERNAL), this governor will not work.
  45. * All times here are in uS.
  46. */
  47. static unsigned int def_sampling_rate;
  48. #define MIN_SAMPLING_RATE_RATIO (2)
  49. /* for correct statistics, we need at least 10 ticks between each measure */
  50. #define MIN_STAT_SAMPLING_RATE \
  51. (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
  52. #define MIN_SAMPLING_RATE \
  53. (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
  54. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  55. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  56. #define DEF_SAMPLING_DOWN_FACTOR (1)
  57. #define MAX_SAMPLING_DOWN_FACTOR (10)
  58. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  59. static void do_dbs_timer(struct work_struct *work);
  60. struct cpu_dbs_info_s {
  61. struct cpufreq_policy *cur_policy;
  62. unsigned int prev_cpu_idle_up;
  63. unsigned int prev_cpu_idle_down;
  64. unsigned int enable;
  65. unsigned int down_skip;
  66. unsigned int requested_freq;
  67. };
  68. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  69. static unsigned int dbs_enable; /* number of CPUs using this policy */
  70. /*
  71. * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
  72. * lock and dbs_mutex. cpu_hotplug lock should always be held before
  73. * dbs_mutex. If any function that can potentially take cpu_hotplug lock
  74. * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
  75. * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
  76. * is recursive for the same process. -Venki
  77. */
  78. static DEFINE_MUTEX (dbs_mutex);
  79. static DECLARE_DELAYED_WORK(dbs_work, do_dbs_timer);
  80. struct dbs_tuners {
  81. unsigned int sampling_rate;
  82. unsigned int sampling_down_factor;
  83. unsigned int up_threshold;
  84. unsigned int down_threshold;
  85. unsigned int ignore_nice;
  86. unsigned int freq_step;
  87. };
  88. static struct dbs_tuners dbs_tuners_ins = {
  89. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  90. .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  91. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  92. .ignore_nice = 0,
  93. .freq_step = 5,
  94. };
  95. static inline unsigned int get_cpu_idle_time(unsigned int cpu)
  96. {
  97. unsigned int add_nice = 0, ret;
  98. if (dbs_tuners_ins.ignore_nice)
  99. add_nice = kstat_cpu(cpu).cpustat.nice;
  100. ret = kstat_cpu(cpu).cpustat.idle +
  101. kstat_cpu(cpu).cpustat.iowait +
  102. add_nice;
  103. return ret;
  104. }
  105. /* keep track of frequency transitions */
  106. static int
  107. dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  108. void *data)
  109. {
  110. struct cpufreq_freqs *freq = data;
  111. struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info,
  112. freq->cpu);
  113. if (!this_dbs_info->enable)
  114. return 0;
  115. this_dbs_info->requested_freq = freq->new;
  116. return 0;
  117. }
  118. static struct notifier_block dbs_cpufreq_notifier_block = {
  119. .notifier_call = dbs_cpufreq_notifier
  120. };
  121. /************************** sysfs interface ************************/
  122. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  123. {
  124. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  125. }
  126. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  127. {
  128. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  129. }
  130. #define define_one_ro(_name) \
  131. static struct freq_attr _name = \
  132. __ATTR(_name, 0444, show_##_name, NULL)
  133. define_one_ro(sampling_rate_max);
  134. define_one_ro(sampling_rate_min);
  135. /* cpufreq_conservative Governor Tunables */
  136. #define show_one(file_name, object) \
  137. static ssize_t show_##file_name \
  138. (struct cpufreq_policy *unused, char *buf) \
  139. { \
  140. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  141. }
  142. show_one(sampling_rate, sampling_rate);
  143. show_one(sampling_down_factor, sampling_down_factor);
  144. show_one(up_threshold, up_threshold);
  145. show_one(down_threshold, down_threshold);
  146. show_one(ignore_nice_load, ignore_nice);
  147. show_one(freq_step, freq_step);
  148. static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
  149. const char *buf, size_t count)
  150. {
  151. unsigned int input;
  152. int ret;
  153. ret = sscanf (buf, "%u", &input);
  154. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  155. return -EINVAL;
  156. mutex_lock(&dbs_mutex);
  157. dbs_tuners_ins.sampling_down_factor = input;
  158. mutex_unlock(&dbs_mutex);
  159. return count;
  160. }
  161. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  162. const char *buf, size_t count)
  163. {
  164. unsigned int input;
  165. int ret;
  166. ret = sscanf (buf, "%u", &input);
  167. mutex_lock(&dbs_mutex);
  168. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  169. mutex_unlock(&dbs_mutex);
  170. return -EINVAL;
  171. }
  172. dbs_tuners_ins.sampling_rate = input;
  173. mutex_unlock(&dbs_mutex);
  174. return count;
  175. }
  176. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  177. const char *buf, size_t count)
  178. {
  179. unsigned int input;
  180. int ret;
  181. ret = sscanf (buf, "%u", &input);
  182. mutex_lock(&dbs_mutex);
  183. if (ret != 1 || input > 100 || input <= dbs_tuners_ins.down_threshold) {
  184. mutex_unlock(&dbs_mutex);
  185. return -EINVAL;
  186. }
  187. dbs_tuners_ins.up_threshold = input;
  188. mutex_unlock(&dbs_mutex);
  189. return count;
  190. }
  191. static ssize_t store_down_threshold(struct cpufreq_policy *unused,
  192. const char *buf, size_t count)
  193. {
  194. unsigned int input;
  195. int ret;
  196. ret = sscanf (buf, "%u", &input);
  197. mutex_lock(&dbs_mutex);
  198. if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
  199. mutex_unlock(&dbs_mutex);
  200. return -EINVAL;
  201. }
  202. dbs_tuners_ins.down_threshold = input;
  203. mutex_unlock(&dbs_mutex);
  204. return count;
  205. }
  206. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  207. const char *buf, size_t count)
  208. {
  209. unsigned int input;
  210. int ret;
  211. unsigned int j;
  212. ret = sscanf(buf, "%u", &input);
  213. if (ret != 1)
  214. return -EINVAL;
  215. if (input > 1)
  216. input = 1;
  217. mutex_lock(&dbs_mutex);
  218. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  219. mutex_unlock(&dbs_mutex);
  220. return count;
  221. }
  222. dbs_tuners_ins.ignore_nice = input;
  223. /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
  224. for_each_online_cpu(j) {
  225. struct cpu_dbs_info_s *j_dbs_info;
  226. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  227. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  228. j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
  229. }
  230. mutex_unlock(&dbs_mutex);
  231. return count;
  232. }
  233. static ssize_t store_freq_step(struct cpufreq_policy *policy,
  234. const char *buf, size_t count)
  235. {
  236. unsigned int input;
  237. int ret;
  238. ret = sscanf(buf, "%u", &input);
  239. if (ret != 1)
  240. return -EINVAL;
  241. if (input > 100)
  242. input = 100;
  243. /* no need to test here if freq_step is zero as the user might actually
  244. * want this, they would be crazy though :) */
  245. mutex_lock(&dbs_mutex);
  246. dbs_tuners_ins.freq_step = input;
  247. mutex_unlock(&dbs_mutex);
  248. return count;
  249. }
  250. #define define_one_rw(_name) \
  251. static struct freq_attr _name = \
  252. __ATTR(_name, 0644, show_##_name, store_##_name)
  253. define_one_rw(sampling_rate);
  254. define_one_rw(sampling_down_factor);
  255. define_one_rw(up_threshold);
  256. define_one_rw(down_threshold);
  257. define_one_rw(ignore_nice_load);
  258. define_one_rw(freq_step);
  259. static struct attribute * dbs_attributes[] = {
  260. &sampling_rate_max.attr,
  261. &sampling_rate_min.attr,
  262. &sampling_rate.attr,
  263. &sampling_down_factor.attr,
  264. &up_threshold.attr,
  265. &down_threshold.attr,
  266. &ignore_nice_load.attr,
  267. &freq_step.attr,
  268. NULL
  269. };
  270. static struct attribute_group dbs_attr_group = {
  271. .attrs = dbs_attributes,
  272. .name = "conservative",
  273. };
  274. /************************** sysfs end ************************/
  275. static void dbs_check_cpu(int cpu)
  276. {
  277. unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
  278. unsigned int tmp_idle_ticks, total_idle_ticks;
  279. unsigned int freq_target;
  280. unsigned int freq_down_sampling_rate;
  281. struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  282. struct cpufreq_policy *policy;
  283. if (!this_dbs_info->enable)
  284. return;
  285. policy = this_dbs_info->cur_policy;
  286. /*
  287. * The default safe range is 20% to 80%
  288. * Every sampling_rate, we check
  289. * - If current idle time is less than 20%, then we try to
  290. * increase frequency
  291. * Every sampling_rate*sampling_down_factor, we check
  292. * - If current idle time is more than 80%, then we try to
  293. * decrease frequency
  294. *
  295. * Any frequency increase takes it to the maximum frequency.
  296. * Frequency reduction happens at minimum steps of
  297. * 5% (default) of max_frequency
  298. */
  299. /* Check for frequency increase */
  300. idle_ticks = UINT_MAX;
  301. /* Check for frequency increase */
  302. total_idle_ticks = get_cpu_idle_time(cpu);
  303. tmp_idle_ticks = total_idle_ticks -
  304. this_dbs_info->prev_cpu_idle_up;
  305. this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  306. if (tmp_idle_ticks < idle_ticks)
  307. idle_ticks = tmp_idle_ticks;
  308. /* Scale idle ticks by 100 and compare with up and down ticks */
  309. idle_ticks *= 100;
  310. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  311. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  312. if (idle_ticks < up_idle_ticks) {
  313. this_dbs_info->down_skip = 0;
  314. this_dbs_info->prev_cpu_idle_down =
  315. this_dbs_info->prev_cpu_idle_up;
  316. /* if we are already at full speed then break out early */
  317. if (this_dbs_info->requested_freq == policy->max)
  318. return;
  319. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  320. /* max freq cannot be less than 100. But who knows.... */
  321. if (unlikely(freq_target == 0))
  322. freq_target = 5;
  323. this_dbs_info->requested_freq += freq_target;
  324. if (this_dbs_info->requested_freq > policy->max)
  325. this_dbs_info->requested_freq = policy->max;
  326. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  327. CPUFREQ_RELATION_H);
  328. return;
  329. }
  330. /* Check for frequency decrease */
  331. this_dbs_info->down_skip++;
  332. if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
  333. return;
  334. /* Check for frequency decrease */
  335. total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
  336. tmp_idle_ticks = total_idle_ticks -
  337. this_dbs_info->prev_cpu_idle_down;
  338. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  339. if (tmp_idle_ticks < idle_ticks)
  340. idle_ticks = tmp_idle_ticks;
  341. /* Scale idle ticks by 100 and compare with up and down ticks */
  342. idle_ticks *= 100;
  343. this_dbs_info->down_skip = 0;
  344. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  345. dbs_tuners_ins.sampling_down_factor;
  346. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  347. usecs_to_jiffies(freq_down_sampling_rate);
  348. if (idle_ticks > down_idle_ticks) {
  349. /*
  350. * if we are already at the lowest speed then break out early
  351. * or if we 'cannot' reduce the speed as the user might want
  352. * freq_target to be zero
  353. */
  354. if (this_dbs_info->requested_freq == policy->min
  355. || dbs_tuners_ins.freq_step == 0)
  356. return;
  357. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  358. /* max freq cannot be less than 100. But who knows.... */
  359. if (unlikely(freq_target == 0))
  360. freq_target = 5;
  361. this_dbs_info->requested_freq -= freq_target;
  362. if (this_dbs_info->requested_freq < policy->min)
  363. this_dbs_info->requested_freq = policy->min;
  364. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  365. CPUFREQ_RELATION_H);
  366. return;
  367. }
  368. }
  369. static void do_dbs_timer(struct work_struct *work)
  370. {
  371. int i;
  372. mutex_lock(&dbs_mutex);
  373. for_each_online_cpu(i)
  374. dbs_check_cpu(i);
  375. schedule_delayed_work(&dbs_work,
  376. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  377. mutex_unlock(&dbs_mutex);
  378. }
  379. static inline void dbs_timer_init(void)
  380. {
  381. init_timer_deferrable(&dbs_work.timer);
  382. schedule_delayed_work(&dbs_work,
  383. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  384. return;
  385. }
  386. static inline void dbs_timer_exit(void)
  387. {
  388. cancel_delayed_work(&dbs_work);
  389. return;
  390. }
  391. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  392. unsigned int event)
  393. {
  394. unsigned int cpu = policy->cpu;
  395. struct cpu_dbs_info_s *this_dbs_info;
  396. unsigned int j;
  397. int rc;
  398. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  399. switch (event) {
  400. case CPUFREQ_GOV_START:
  401. if ((!cpu_online(cpu)) || (!policy->cur))
  402. return -EINVAL;
  403. if (this_dbs_info->enable) /* Already enabled */
  404. break;
  405. mutex_lock(&dbs_mutex);
  406. rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
  407. if (rc) {
  408. mutex_unlock(&dbs_mutex);
  409. return rc;
  410. }
  411. for_each_cpu_mask_nr(j, policy->cpus) {
  412. struct cpu_dbs_info_s *j_dbs_info;
  413. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  414. j_dbs_info->cur_policy = policy;
  415. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
  416. j_dbs_info->prev_cpu_idle_down
  417. = j_dbs_info->prev_cpu_idle_up;
  418. }
  419. this_dbs_info->enable = 1;
  420. this_dbs_info->down_skip = 0;
  421. this_dbs_info->requested_freq = policy->cur;
  422. dbs_enable++;
  423. /*
  424. * Start the timerschedule work, when this governor
  425. * is used for first time
  426. */
  427. if (dbs_enable == 1) {
  428. unsigned int latency;
  429. /* policy latency is in nS. Convert it to uS first */
  430. latency = policy->cpuinfo.transition_latency / 1000;
  431. if (latency == 0)
  432. latency = 1;
  433. def_sampling_rate = 10 * latency *
  434. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  435. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  436. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  437. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  438. dbs_timer_init();
  439. cpufreq_register_notifier(
  440. &dbs_cpufreq_notifier_block,
  441. CPUFREQ_TRANSITION_NOTIFIER);
  442. }
  443. mutex_unlock(&dbs_mutex);
  444. break;
  445. case CPUFREQ_GOV_STOP:
  446. mutex_lock(&dbs_mutex);
  447. this_dbs_info->enable = 0;
  448. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  449. dbs_enable--;
  450. /*
  451. * Stop the timerschedule work, when this governor
  452. * is used for first time
  453. */
  454. if (dbs_enable == 0) {
  455. dbs_timer_exit();
  456. cpufreq_unregister_notifier(
  457. &dbs_cpufreq_notifier_block,
  458. CPUFREQ_TRANSITION_NOTIFIER);
  459. }
  460. mutex_unlock(&dbs_mutex);
  461. break;
  462. case CPUFREQ_GOV_LIMITS:
  463. mutex_lock(&dbs_mutex);
  464. if (policy->max < this_dbs_info->cur_policy->cur)
  465. __cpufreq_driver_target(
  466. this_dbs_info->cur_policy,
  467. policy->max, CPUFREQ_RELATION_H);
  468. else if (policy->min > this_dbs_info->cur_policy->cur)
  469. __cpufreq_driver_target(
  470. this_dbs_info->cur_policy,
  471. policy->min, CPUFREQ_RELATION_L);
  472. mutex_unlock(&dbs_mutex);
  473. break;
  474. }
  475. return 0;
  476. }
  477. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  478. static
  479. #endif
  480. struct cpufreq_governor cpufreq_gov_conservative = {
  481. .name = "conservative",
  482. .governor = cpufreq_governor_dbs,
  483. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  484. .owner = THIS_MODULE,
  485. };
  486. static int __init cpufreq_gov_dbs_init(void)
  487. {
  488. return cpufreq_register_governor(&cpufreq_gov_conservative);
  489. }
  490. static void __exit cpufreq_gov_dbs_exit(void)
  491. {
  492. /* Make sure that the scheduled work is indeed not running */
  493. flush_scheduled_work();
  494. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  495. }
  496. MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
  497. MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
  498. "Low Latency Frequency Transition capable processors "
  499. "optimised for use in a battery environment");
  500. MODULE_LICENSE ("GPL");
  501. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  502. fs_initcall(cpufreq_gov_dbs_init);
  503. #else
  504. module_init(cpufreq_gov_dbs_init);
  505. #endif
  506. module_exit(cpufreq_gov_dbs_exit);