cpufreq_conservative.c 15 KB

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