cpufreq_conservative.c 15 KB

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