cpufreq_conservative.c 15 KB

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