cpufreq_conservative.c 15 KB

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