cpufreq_conservative.c 15 KB

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