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. #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 MIN_FREQUENCY_UP_THRESHOLD (0)
  37. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  38. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  39. #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
  40. #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
  41. /*
  42. * The polling frequency of this governor depends on the capability of
  43. * the processor. Default polling frequency is 1000 times the transition
  44. * latency of the processor. The governor will work on any processor with
  45. * transition latency <= 10mS, using appropriate sampling
  46. * rate.
  47. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  48. * this governor will not work.
  49. * All times here are in uS.
  50. */
  51. static unsigned int def_sampling_rate;
  52. #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
  53. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  54. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (100000)
  55. #define DEF_SAMPLING_DOWN_FACTOR (5)
  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 )
  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 > MAX_FREQUENCY_UP_THRESHOLD ||
  152. input < MIN_FREQUENCY_UP_THRESHOLD ||
  153. input <= dbs_tuners_ins.down_threshold) {
  154. mutex_unlock(&dbs_mutex);
  155. return -EINVAL;
  156. }
  157. dbs_tuners_ins.up_threshold = input;
  158. mutex_unlock(&dbs_mutex);
  159. return count;
  160. }
  161. static ssize_t store_down_threshold(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_FREQUENCY_DOWN_THRESHOLD ||
  169. input < MIN_FREQUENCY_DOWN_THRESHOLD ||
  170. 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 freq_step;
  251. unsigned int freq_down_sampling_rate;
  252. static int down_skip[NR_CPUS];
  253. static int requested_freq[NR_CPUS];
  254. static unsigned short init_flag = 0;
  255. struct cpu_dbs_info_s *this_dbs_info;
  256. struct cpu_dbs_info_s *dbs_info;
  257. struct cpufreq_policy *policy;
  258. unsigned int j;
  259. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  260. if (!this_dbs_info->enable)
  261. return;
  262. policy = this_dbs_info->cur_policy;
  263. if ( init_flag == 0 ) {
  264. for_each_online_cpu(j) {
  265. dbs_info = &per_cpu(cpu_dbs_info, j);
  266. requested_freq[j] = dbs_info->cur_policy->cur;
  267. }
  268. init_flag = 1;
  269. }
  270. /*
  271. * The default safe range is 20% to 80%
  272. * Every sampling_rate, we check
  273. * - If current idle time is less than 20%, then we try to
  274. * increase frequency
  275. * Every sampling_rate*sampling_down_factor, we check
  276. * - If current idle time is more than 80%, then we try to
  277. * decrease frequency
  278. *
  279. * Any frequency increase takes it to the maximum frequency.
  280. * Frequency reduction happens at minimum steps of
  281. * 5% (default) of max_frequency
  282. */
  283. /* Check for frequency increase */
  284. idle_ticks = UINT_MAX;
  285. for_each_cpu_mask(j, policy->cpus) {
  286. unsigned int tmp_idle_ticks, total_idle_ticks;
  287. struct cpu_dbs_info_s *j_dbs_info;
  288. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  289. /* Check for frequency increase */
  290. total_idle_ticks = get_cpu_idle_time(j);
  291. tmp_idle_ticks = total_idle_ticks -
  292. j_dbs_info->prev_cpu_idle_up;
  293. j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  294. if (tmp_idle_ticks < idle_ticks)
  295. idle_ticks = tmp_idle_ticks;
  296. }
  297. /* Scale idle ticks by 100 and compare with up and down ticks */
  298. idle_ticks *= 100;
  299. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  300. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  301. if (idle_ticks < up_idle_ticks) {
  302. down_skip[cpu] = 0;
  303. for_each_cpu_mask(j, policy->cpus) {
  304. struct cpu_dbs_info_s *j_dbs_info;
  305. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  306. j_dbs_info->prev_cpu_idle_down =
  307. j_dbs_info->prev_cpu_idle_up;
  308. }
  309. /* if we are already at full speed then break out early */
  310. if (requested_freq[cpu] == policy->max)
  311. return;
  312. freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
  313. /* max freq cannot be less than 100. But who knows.... */
  314. if (unlikely(freq_step == 0))
  315. freq_step = 5;
  316. requested_freq[cpu] += freq_step;
  317. if (requested_freq[cpu] > policy->max)
  318. requested_freq[cpu] = policy->max;
  319. __cpufreq_driver_target(policy, requested_freq[cpu],
  320. CPUFREQ_RELATION_H);
  321. return;
  322. }
  323. /* Check for frequency decrease */
  324. down_skip[cpu]++;
  325. if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
  326. return;
  327. idle_ticks = UINT_MAX;
  328. for_each_cpu_mask(j, policy->cpus) {
  329. unsigned int tmp_idle_ticks, total_idle_ticks;
  330. struct cpu_dbs_info_s *j_dbs_info;
  331. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  332. total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
  333. tmp_idle_ticks = total_idle_ticks -
  334. j_dbs_info->prev_cpu_idle_down;
  335. j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  336. if (tmp_idle_ticks < idle_ticks)
  337. idle_ticks = tmp_idle_ticks;
  338. }
  339. /* Scale idle ticks by 100 and compare with up and down ticks */
  340. idle_ticks *= 100;
  341. down_skip[cpu] = 0;
  342. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  343. dbs_tuners_ins.sampling_down_factor;
  344. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  345. usecs_to_jiffies(freq_down_sampling_rate);
  346. if (idle_ticks > down_idle_ticks) {
  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. if (requested_freq[cpu] == policy->min
  351. || dbs_tuners_ins.freq_step == 0)
  352. return;
  353. freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
  354. /* max freq cannot be less than 100. But who knows.... */
  355. if (unlikely(freq_step == 0))
  356. freq_step = 5;
  357. requested_freq[cpu] -= freq_step;
  358. if (requested_freq[cpu] < policy->min)
  359. requested_freq[cpu] = policy->min;
  360. __cpufreq_driver_target(policy,
  361. 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;
  425. if (latency < 1000)
  426. latency = 1000;
  427. def_sampling_rate = (latency / 1000) *
  428. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  429. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  430. dbs_tuners_ins.ignore_nice = 0;
  431. dbs_tuners_ins.freq_step = 5;
  432. dbs_timer_init();
  433. }
  434. mutex_unlock(&dbs_mutex);
  435. break;
  436. case CPUFREQ_GOV_STOP:
  437. mutex_lock(&dbs_mutex);
  438. this_dbs_info->enable = 0;
  439. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  440. dbs_enable--;
  441. /*
  442. * Stop the timerschedule work, when this governor
  443. * is used for first time
  444. */
  445. if (dbs_enable == 0)
  446. dbs_timer_exit();
  447. mutex_unlock(&dbs_mutex);
  448. break;
  449. case CPUFREQ_GOV_LIMITS:
  450. mutex_lock(&dbs_mutex);
  451. if (policy->max < this_dbs_info->cur_policy->cur)
  452. __cpufreq_driver_target(
  453. this_dbs_info->cur_policy,
  454. policy->max, CPUFREQ_RELATION_H);
  455. else if (policy->min > this_dbs_info->cur_policy->cur)
  456. __cpufreq_driver_target(
  457. this_dbs_info->cur_policy,
  458. policy->min, CPUFREQ_RELATION_L);
  459. mutex_unlock(&dbs_mutex);
  460. break;
  461. }
  462. return 0;
  463. }
  464. static struct cpufreq_governor cpufreq_gov_dbs = {
  465. .name = "conservative",
  466. .governor = cpufreq_governor_dbs,
  467. .owner = THIS_MODULE,
  468. };
  469. static int __init cpufreq_gov_dbs_init(void)
  470. {
  471. return cpufreq_register_governor(&cpufreq_gov_dbs);
  472. }
  473. static void __exit cpufreq_gov_dbs_exit(void)
  474. {
  475. /* Make sure that the scheduled work is indeed not running */
  476. flush_scheduled_work();
  477. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  478. }
  479. MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
  480. MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
  481. "Low Latency Frequency Transition capable processors "
  482. "optimised for use in a battery environment");
  483. MODULE_LICENSE ("GPL");
  484. module_init(cpufreq_gov_dbs_init);
  485. module_exit(cpufreq_gov_dbs_exit);