cpufreq_conservative.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 ||
  169. input < MIN_SAMPLING_RATE) {
  170. mutex_unlock(&dbs_mutex);
  171. return -EINVAL;
  172. }
  173. dbs_tuners_ins.sampling_rate = input;
  174. mutex_unlock(&dbs_mutex);
  175. return count;
  176. }
  177. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  178. const char *buf, size_t count)
  179. {
  180. unsigned int input;
  181. int ret;
  182. ret = sscanf(buf, "%u", &input);
  183. mutex_lock(&dbs_mutex);
  184. if (ret != 1 || input > 100 ||
  185. input <= dbs_tuners_ins.down_threshold) {
  186. mutex_unlock(&dbs_mutex);
  187. return -EINVAL;
  188. }
  189. dbs_tuners_ins.up_threshold = input;
  190. mutex_unlock(&dbs_mutex);
  191. return count;
  192. }
  193. static ssize_t store_down_threshold(struct cpufreq_policy *unused,
  194. const char *buf, size_t count)
  195. {
  196. unsigned int input;
  197. int ret;
  198. ret = sscanf(buf, "%u", &input);
  199. mutex_lock(&dbs_mutex);
  200. if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
  201. mutex_unlock(&dbs_mutex);
  202. return -EINVAL;
  203. }
  204. dbs_tuners_ins.down_threshold = input;
  205. mutex_unlock(&dbs_mutex);
  206. return count;
  207. }
  208. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  209. const char *buf, size_t count)
  210. {
  211. unsigned int input;
  212. int ret;
  213. unsigned int j;
  214. ret = sscanf(buf, "%u", &input);
  215. if (ret != 1)
  216. return -EINVAL;
  217. if (input > 1)
  218. input = 1;
  219. mutex_lock(&dbs_mutex);
  220. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  221. mutex_unlock(&dbs_mutex);
  222. return count;
  223. }
  224. dbs_tuners_ins.ignore_nice = input;
  225. /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
  226. for_each_online_cpu(j) {
  227. struct cpu_dbs_info_s *j_dbs_info;
  228. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  229. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  230. j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
  231. }
  232. mutex_unlock(&dbs_mutex);
  233. return count;
  234. }
  235. static ssize_t store_freq_step(struct cpufreq_policy *policy,
  236. const char *buf, size_t count)
  237. {
  238. unsigned int input;
  239. int ret;
  240. ret = sscanf(buf, "%u", &input);
  241. if (ret != 1)
  242. return -EINVAL;
  243. if (input > 100)
  244. input = 100;
  245. /* no need to test here if freq_step is zero as the user might actually
  246. * want this, they would be crazy though :) */
  247. mutex_lock(&dbs_mutex);
  248. dbs_tuners_ins.freq_step = input;
  249. mutex_unlock(&dbs_mutex);
  250. return count;
  251. }
  252. #define define_one_rw(_name) \
  253. static struct freq_attr _name = \
  254. __ATTR(_name, 0644, show_##_name, store_##_name)
  255. define_one_rw(sampling_rate);
  256. define_one_rw(sampling_down_factor);
  257. define_one_rw(up_threshold);
  258. define_one_rw(down_threshold);
  259. define_one_rw(ignore_nice_load);
  260. define_one_rw(freq_step);
  261. static struct attribute *dbs_attributes[] = {
  262. &sampling_rate_max.attr,
  263. &sampling_rate_min.attr,
  264. &sampling_rate.attr,
  265. &sampling_down_factor.attr,
  266. &up_threshold.attr,
  267. &down_threshold.attr,
  268. &ignore_nice_load.attr,
  269. &freq_step.attr,
  270. NULL
  271. };
  272. static struct attribute_group dbs_attr_group = {
  273. .attrs = dbs_attributes,
  274. .name = "conservative",
  275. };
  276. /************************** sysfs end ************************/
  277. static void dbs_check_cpu(int cpu)
  278. {
  279. unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
  280. unsigned int tmp_idle_ticks, total_idle_ticks;
  281. unsigned int freq_target;
  282. unsigned int freq_down_sampling_rate;
  283. struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  284. struct cpufreq_policy *policy;
  285. if (!this_dbs_info->enable)
  286. return;
  287. policy = this_dbs_info->cur_policy;
  288. /*
  289. * The default safe range is 20% to 80%
  290. * Every sampling_rate, we check
  291. * - If current idle time is less than 20%, then we try to
  292. * increase frequency
  293. * Every sampling_rate*sampling_down_factor, we check
  294. * - If current idle time is more than 80%, then we try to
  295. * decrease frequency
  296. *
  297. * Any frequency increase takes it to the maximum frequency.
  298. * Frequency reduction happens at minimum steps of
  299. * 5% (default) of max_frequency
  300. */
  301. /* Check for frequency increase */
  302. idle_ticks = UINT_MAX;
  303. /* Check for frequency increase */
  304. total_idle_ticks = get_cpu_idle_time(cpu);
  305. tmp_idle_ticks = total_idle_ticks -
  306. this_dbs_info->prev_cpu_idle_up;
  307. this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  308. if (tmp_idle_ticks < idle_ticks)
  309. idle_ticks = tmp_idle_ticks;
  310. /* Scale idle ticks by 100 and compare with up and down ticks */
  311. idle_ticks *= 100;
  312. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  313. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  314. if (idle_ticks < up_idle_ticks) {
  315. this_dbs_info->down_skip = 0;
  316. this_dbs_info->prev_cpu_idle_down =
  317. this_dbs_info->prev_cpu_idle_up;
  318. /* if we are already at full speed then break out early */
  319. if (this_dbs_info->requested_freq == policy->max)
  320. return;
  321. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  322. /* max freq cannot be less than 100. But who knows.... */
  323. if (unlikely(freq_target == 0))
  324. freq_target = 5;
  325. this_dbs_info->requested_freq += freq_target;
  326. if (this_dbs_info->requested_freq > policy->max)
  327. this_dbs_info->requested_freq = policy->max;
  328. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  329. CPUFREQ_RELATION_H);
  330. return;
  331. }
  332. /* Check for frequency decrease */
  333. this_dbs_info->down_skip++;
  334. if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
  335. return;
  336. /* Check for frequency decrease */
  337. total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
  338. tmp_idle_ticks = total_idle_ticks -
  339. this_dbs_info->prev_cpu_idle_down;
  340. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  341. if (tmp_idle_ticks < idle_ticks)
  342. idle_ticks = tmp_idle_ticks;
  343. /* Scale idle ticks by 100 and compare with up and down ticks */
  344. idle_ticks *= 100;
  345. this_dbs_info->down_skip = 0;
  346. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  347. dbs_tuners_ins.sampling_down_factor;
  348. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  349. usecs_to_jiffies(freq_down_sampling_rate);
  350. if (idle_ticks > down_idle_ticks) {
  351. /*
  352. * if we are already at the lowest speed then break out early
  353. * or if we 'cannot' reduce the speed as the user might want
  354. * freq_target to be zero
  355. */
  356. if (this_dbs_info->requested_freq == policy->min
  357. || dbs_tuners_ins.freq_step == 0)
  358. return;
  359. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  360. /* max freq cannot be less than 100. But who knows.... */
  361. if (unlikely(freq_target == 0))
  362. freq_target = 5;
  363. this_dbs_info->requested_freq -= freq_target;
  364. if (this_dbs_info->requested_freq < policy->min)
  365. this_dbs_info->requested_freq = policy->min;
  366. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  367. CPUFREQ_RELATION_H);
  368. return;
  369. }
  370. }
  371. static void do_dbs_timer(struct work_struct *work)
  372. {
  373. int i;
  374. mutex_lock(&dbs_mutex);
  375. for_each_online_cpu(i)
  376. dbs_check_cpu(i);
  377. schedule_delayed_work(&dbs_work,
  378. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  379. mutex_unlock(&dbs_mutex);
  380. }
  381. static inline void dbs_timer_init(void)
  382. {
  383. init_timer_deferrable(&dbs_work.timer);
  384. schedule_delayed_work(&dbs_work,
  385. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  386. return;
  387. }
  388. static inline void dbs_timer_exit(void)
  389. {
  390. cancel_delayed_work(&dbs_work);
  391. return;
  392. }
  393. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  394. unsigned int event)
  395. {
  396. unsigned int cpu = policy->cpu;
  397. struct cpu_dbs_info_s *this_dbs_info;
  398. unsigned int j;
  399. int rc;
  400. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  401. switch (event) {
  402. case CPUFREQ_GOV_START:
  403. if ((!cpu_online(cpu)) || (!policy->cur))
  404. return -EINVAL;
  405. if (this_dbs_info->enable) /* Already enabled */
  406. break;
  407. mutex_lock(&dbs_mutex);
  408. rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
  409. if (rc) {
  410. mutex_unlock(&dbs_mutex);
  411. return rc;
  412. }
  413. for_each_cpu(j, policy->cpus) {
  414. struct cpu_dbs_info_s *j_dbs_info;
  415. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  416. j_dbs_info->cur_policy = policy;
  417. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
  418. j_dbs_info->prev_cpu_idle_down
  419. = j_dbs_info->prev_cpu_idle_up;
  420. }
  421. this_dbs_info->enable = 1;
  422. this_dbs_info->down_skip = 0;
  423. this_dbs_info->requested_freq = policy->cur;
  424. dbs_enable++;
  425. /*
  426. * Start the timerschedule work, when this governor
  427. * is used for first time
  428. */
  429. if (dbs_enable == 1) {
  430. unsigned int latency;
  431. /* policy latency is in nS. Convert it to uS first */
  432. latency = policy->cpuinfo.transition_latency / 1000;
  433. if (latency == 0)
  434. latency = 1;
  435. def_sampling_rate = 10 * latency *
  436. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  437. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  438. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  439. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  440. dbs_timer_init();
  441. cpufreq_register_notifier(
  442. &dbs_cpufreq_notifier_block,
  443. CPUFREQ_TRANSITION_NOTIFIER);
  444. }
  445. mutex_unlock(&dbs_mutex);
  446. break;
  447. case CPUFREQ_GOV_STOP:
  448. mutex_lock(&dbs_mutex);
  449. this_dbs_info->enable = 0;
  450. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  451. dbs_enable--;
  452. /*
  453. * Stop the timerschedule work, when this governor
  454. * is used for first time
  455. */
  456. if (dbs_enable == 0) {
  457. dbs_timer_exit();
  458. cpufreq_unregister_notifier(
  459. &dbs_cpufreq_notifier_block,
  460. CPUFREQ_TRANSITION_NOTIFIER);
  461. }
  462. mutex_unlock(&dbs_mutex);
  463. break;
  464. case CPUFREQ_GOV_LIMITS:
  465. mutex_lock(&dbs_mutex);
  466. if (policy->max < this_dbs_info->cur_policy->cur)
  467. __cpufreq_driver_target(
  468. this_dbs_info->cur_policy,
  469. policy->max, CPUFREQ_RELATION_H);
  470. else if (policy->min > this_dbs_info->cur_policy->cur)
  471. __cpufreq_driver_target(
  472. this_dbs_info->cur_policy,
  473. policy->min, CPUFREQ_RELATION_L);
  474. mutex_unlock(&dbs_mutex);
  475. break;
  476. }
  477. return 0;
  478. }
  479. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  480. static
  481. #endif
  482. struct cpufreq_governor cpufreq_gov_conservative = {
  483. .name = "conservative",
  484. .governor = cpufreq_governor_dbs,
  485. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  486. .owner = THIS_MODULE,
  487. };
  488. static int __init cpufreq_gov_dbs_init(void)
  489. {
  490. return cpufreq_register_governor(&cpufreq_gov_conservative);
  491. }
  492. static void __exit cpufreq_gov_dbs_exit(void)
  493. {
  494. /* Make sure that the scheduled work is indeed not running */
  495. flush_scheduled_work();
  496. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  497. }
  498. MODULE_AUTHOR("Alexander Clouter <alex-kernel@digriz.org.uk>");
  499. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  500. "Low Latency Frequency Transition capable processors "
  501. "optimised for use in a battery environment");
  502. MODULE_LICENSE("GPL");
  503. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  504. fs_initcall(cpufreq_gov_dbs_init);
  505. #else
  506. module_init(cpufreq_gov_dbs_init);
  507. #endif
  508. module_exit(cpufreq_gov_dbs_exit);