cpufreq_conservative.c 15 KB

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