cpufreq_conservative.c 15 KB

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