cpufreq_conservative.c 15 KB

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