cpufreq_ondemand.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * drivers/cpufreq/cpufreq_ondemand.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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/smp.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ctype.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/types.h>
  21. #include <linux/fs.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/sched.h>
  24. #include <linux/kmod.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/kernel_stat.h>
  28. #include <linux/percpu.h>
  29. /*
  30. * dbs is used in this file as a shortform for demandbased switching
  31. * It helps to keep variable names smaller, simpler
  32. */
  33. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  34. #define MIN_FREQUENCY_UP_THRESHOLD (0)
  35. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  36. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  37. #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
  38. #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
  39. /*
  40. * The polling frequency of this governor depends on the capability of
  41. * the processor. Default polling frequency is 1000 times the transition
  42. * latency of the processor. The governor will work on any processor with
  43. * transition latency <= 10mS, using appropriate sampling
  44. * rate.
  45. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  46. * this governor will not work.
  47. * All times here are in uS.
  48. */
  49. static unsigned int def_sampling_rate;
  50. #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
  51. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  52. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  53. #define DEF_SAMPLING_DOWN_FACTOR (10)
  54. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  55. #define sampling_rate_in_HZ(x) (((x * HZ) < (1000 * 1000))?1:((x * HZ) / (1000 * 1000)))
  56. static void do_dbs_timer(void *data);
  57. struct cpu_dbs_info_s {
  58. struct cpufreq_policy *cur_policy;
  59. unsigned int prev_cpu_idle_up;
  60. unsigned int prev_cpu_idle_down;
  61. unsigned int enable;
  62. };
  63. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  64. static unsigned int dbs_enable; /* number of CPUs using this policy */
  65. static DECLARE_MUTEX (dbs_sem);
  66. static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
  67. struct dbs_tuners {
  68. unsigned int sampling_rate;
  69. unsigned int sampling_down_factor;
  70. unsigned int up_threshold;
  71. unsigned int down_threshold;
  72. };
  73. static struct dbs_tuners dbs_tuners_ins = {
  74. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  75. .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  76. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  77. };
  78. /************************** sysfs interface ************************/
  79. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  80. {
  81. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  82. }
  83. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  84. {
  85. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  86. }
  87. #define define_one_ro(_name) \
  88. static struct freq_attr _name = \
  89. __ATTR(_name, 0444, show_##_name, NULL)
  90. define_one_ro(sampling_rate_max);
  91. define_one_ro(sampling_rate_min);
  92. /* cpufreq_ondemand Governor Tunables */
  93. #define show_one(file_name, object) \
  94. static ssize_t show_##file_name \
  95. (struct cpufreq_policy *unused, char *buf) \
  96. { \
  97. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  98. }
  99. show_one(sampling_rate, sampling_rate);
  100. show_one(sampling_down_factor, sampling_down_factor);
  101. show_one(up_threshold, up_threshold);
  102. show_one(down_threshold, down_threshold);
  103. static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
  104. const char *buf, size_t count)
  105. {
  106. unsigned int input;
  107. int ret;
  108. ret = sscanf (buf, "%u", &input);
  109. if (ret != 1 )
  110. return -EINVAL;
  111. down(&dbs_sem);
  112. dbs_tuners_ins.sampling_down_factor = input;
  113. up(&dbs_sem);
  114. return count;
  115. }
  116. static ssize_t store_sampling_rate(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. down(&dbs_sem);
  123. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  124. up(&dbs_sem);
  125. return -EINVAL;
  126. }
  127. dbs_tuners_ins.sampling_rate = input;
  128. up(&dbs_sem);
  129. return count;
  130. }
  131. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  132. const char *buf, size_t count)
  133. {
  134. unsigned int input;
  135. int ret;
  136. ret = sscanf (buf, "%u", &input);
  137. down(&dbs_sem);
  138. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  139. input < MIN_FREQUENCY_UP_THRESHOLD ||
  140. input <= dbs_tuners_ins.down_threshold) {
  141. up(&dbs_sem);
  142. return -EINVAL;
  143. }
  144. dbs_tuners_ins.up_threshold = input;
  145. up(&dbs_sem);
  146. return count;
  147. }
  148. static ssize_t store_down_threshold(struct cpufreq_policy *unused,
  149. const char *buf, size_t count)
  150. {
  151. unsigned int input;
  152. int ret;
  153. ret = sscanf (buf, "%u", &input);
  154. down(&dbs_sem);
  155. if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
  156. input < MIN_FREQUENCY_DOWN_THRESHOLD ||
  157. input >= dbs_tuners_ins.up_threshold) {
  158. up(&dbs_sem);
  159. return -EINVAL;
  160. }
  161. dbs_tuners_ins.down_threshold = input;
  162. up(&dbs_sem);
  163. return count;
  164. }
  165. #define define_one_rw(_name) \
  166. static struct freq_attr _name = \
  167. __ATTR(_name, 0644, show_##_name, store_##_name)
  168. define_one_rw(sampling_rate);
  169. define_one_rw(sampling_down_factor);
  170. define_one_rw(up_threshold);
  171. define_one_rw(down_threshold);
  172. static struct attribute * dbs_attributes[] = {
  173. &sampling_rate_max.attr,
  174. &sampling_rate_min.attr,
  175. &sampling_rate.attr,
  176. &sampling_down_factor.attr,
  177. &up_threshold.attr,
  178. &down_threshold.attr,
  179. NULL
  180. };
  181. static struct attribute_group dbs_attr_group = {
  182. .attrs = dbs_attributes,
  183. .name = "ondemand",
  184. };
  185. /************************** sysfs end ************************/
  186. static void dbs_check_cpu(int cpu)
  187. {
  188. unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
  189. unsigned int total_idle_ticks;
  190. unsigned int freq_down_step;
  191. unsigned int freq_down_sampling_rate;
  192. static int down_skip[NR_CPUS];
  193. struct cpu_dbs_info_s *this_dbs_info;
  194. struct cpufreq_policy *policy;
  195. unsigned int j;
  196. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  197. if (!this_dbs_info->enable)
  198. return;
  199. policy = this_dbs_info->cur_policy;
  200. /*
  201. * The default safe range is 20% to 80%
  202. * Every sampling_rate, we check
  203. * - If current idle time is less than 20%, then we try to
  204. * increase frequency
  205. * Every sampling_rate*sampling_down_factor, we check
  206. * - If current idle time is more than 80%, then we try to
  207. * decrease frequency
  208. *
  209. * Any frequency increase takes it to the maximum frequency.
  210. * Frequency reduction happens at minimum steps of
  211. * 5% of max_frequency
  212. */
  213. /* Check for frequency increase */
  214. total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
  215. kstat_cpu(cpu).cpustat.iowait;
  216. idle_ticks = total_idle_ticks -
  217. this_dbs_info->prev_cpu_idle_up;
  218. this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  219. for_each_cpu_mask(j, policy->cpus) {
  220. unsigned int tmp_idle_ticks;
  221. struct cpu_dbs_info_s *j_dbs_info;
  222. if (j == cpu)
  223. continue;
  224. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  225. /* Check for frequency increase */
  226. total_idle_ticks = kstat_cpu(j).cpustat.idle +
  227. kstat_cpu(j).cpustat.iowait;
  228. tmp_idle_ticks = total_idle_ticks -
  229. j_dbs_info->prev_cpu_idle_up;
  230. j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  231. if (tmp_idle_ticks < idle_ticks)
  232. idle_ticks = tmp_idle_ticks;
  233. }
  234. /* Scale idle ticks by 100 and compare with up and down ticks */
  235. idle_ticks *= 100;
  236. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  237. sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate);
  238. if (idle_ticks < up_idle_ticks) {
  239. __cpufreq_driver_target(policy, policy->max,
  240. CPUFREQ_RELATION_H);
  241. down_skip[cpu] = 0;
  242. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  243. return;
  244. }
  245. /* Check for frequency decrease */
  246. down_skip[cpu]++;
  247. if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
  248. return;
  249. total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
  250. kstat_cpu(cpu).cpustat.iowait;
  251. idle_ticks = total_idle_ticks -
  252. this_dbs_info->prev_cpu_idle_down;
  253. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  254. for_each_cpu_mask(j, policy->cpus) {
  255. unsigned int tmp_idle_ticks;
  256. struct cpu_dbs_info_s *j_dbs_info;
  257. if (j == cpu)
  258. continue;
  259. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  260. /* Check for frequency increase */
  261. total_idle_ticks = kstat_cpu(j).cpustat.idle +
  262. kstat_cpu(j).cpustat.iowait;
  263. tmp_idle_ticks = total_idle_ticks -
  264. j_dbs_info->prev_cpu_idle_down;
  265. j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  266. if (tmp_idle_ticks < idle_ticks)
  267. idle_ticks = tmp_idle_ticks;
  268. }
  269. /* Scale idle ticks by 100 and compare with up and down ticks */
  270. idle_ticks *= 100;
  271. down_skip[cpu] = 0;
  272. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  273. dbs_tuners_ins.sampling_down_factor;
  274. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  275. sampling_rate_in_HZ(freq_down_sampling_rate);
  276. if (idle_ticks > down_idle_ticks ) {
  277. freq_down_step = (5 * policy->max) / 100;
  278. /* max freq cannot be less than 100. But who knows.... */
  279. if (unlikely(freq_down_step == 0))
  280. freq_down_step = 5;
  281. __cpufreq_driver_target(policy,
  282. policy->cur - freq_down_step,
  283. CPUFREQ_RELATION_H);
  284. return;
  285. }
  286. }
  287. static void do_dbs_timer(void *data)
  288. {
  289. int i;
  290. down(&dbs_sem);
  291. for (i = 0; i < NR_CPUS; i++)
  292. if (cpu_online(i))
  293. dbs_check_cpu(i);
  294. schedule_delayed_work(&dbs_work,
  295. sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate));
  296. up(&dbs_sem);
  297. }
  298. static inline void dbs_timer_init(void)
  299. {
  300. INIT_WORK(&dbs_work, do_dbs_timer, NULL);
  301. schedule_delayed_work(&dbs_work,
  302. sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate));
  303. return;
  304. }
  305. static inline void dbs_timer_exit(void)
  306. {
  307. cancel_delayed_work(&dbs_work);
  308. return;
  309. }
  310. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  311. unsigned int event)
  312. {
  313. unsigned int cpu = policy->cpu;
  314. struct cpu_dbs_info_s *this_dbs_info;
  315. unsigned int j;
  316. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  317. switch (event) {
  318. case CPUFREQ_GOV_START:
  319. if ((!cpu_online(cpu)) ||
  320. (!policy->cur))
  321. return -EINVAL;
  322. if (policy->cpuinfo.transition_latency >
  323. (TRANSITION_LATENCY_LIMIT * 1000))
  324. return -EINVAL;
  325. if (this_dbs_info->enable) /* Already enabled */
  326. break;
  327. down(&dbs_sem);
  328. for_each_cpu_mask(j, policy->cpus) {
  329. struct cpu_dbs_info_s *j_dbs_info;
  330. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  331. j_dbs_info->cur_policy = policy;
  332. j_dbs_info->prev_cpu_idle_up =
  333. kstat_cpu(j).cpustat.idle +
  334. kstat_cpu(j).cpustat.iowait;
  335. j_dbs_info->prev_cpu_idle_down =
  336. kstat_cpu(j).cpustat.idle +
  337. kstat_cpu(j).cpustat.iowait;
  338. }
  339. this_dbs_info->enable = 1;
  340. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  341. dbs_enable++;
  342. /*
  343. * Start the timerschedule work, when this governor
  344. * is used for first time
  345. */
  346. if (dbs_enable == 1) {
  347. unsigned int latency;
  348. /* policy latency is in nS. Convert it to uS first */
  349. latency = policy->cpuinfo.transition_latency;
  350. if (latency < 1000)
  351. latency = 1000;
  352. def_sampling_rate = (latency / 1000) *
  353. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  354. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  355. dbs_timer_init();
  356. }
  357. up(&dbs_sem);
  358. break;
  359. case CPUFREQ_GOV_STOP:
  360. down(&dbs_sem);
  361. this_dbs_info->enable = 0;
  362. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  363. dbs_enable--;
  364. /*
  365. * Stop the timerschedule work, when this governor
  366. * is used for first time
  367. */
  368. if (dbs_enable == 0)
  369. dbs_timer_exit();
  370. up(&dbs_sem);
  371. break;
  372. case CPUFREQ_GOV_LIMITS:
  373. down(&dbs_sem);
  374. if (policy->max < this_dbs_info->cur_policy->cur)
  375. __cpufreq_driver_target(
  376. this_dbs_info->cur_policy,
  377. policy->max, CPUFREQ_RELATION_H);
  378. else if (policy->min > this_dbs_info->cur_policy->cur)
  379. __cpufreq_driver_target(
  380. this_dbs_info->cur_policy,
  381. policy->min, CPUFREQ_RELATION_L);
  382. up(&dbs_sem);
  383. break;
  384. }
  385. return 0;
  386. }
  387. struct cpufreq_governor cpufreq_gov_dbs = {
  388. .name = "ondemand",
  389. .governor = cpufreq_governor_dbs,
  390. .owner = THIS_MODULE,
  391. };
  392. EXPORT_SYMBOL(cpufreq_gov_dbs);
  393. static int __init cpufreq_gov_dbs_init(void)
  394. {
  395. return cpufreq_register_governor(&cpufreq_gov_dbs);
  396. }
  397. static void __exit cpufreq_gov_dbs_exit(void)
  398. {
  399. /* Make sure that the scheduled work is indeed not running */
  400. flush_scheduled_work();
  401. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  402. }
  403. MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  404. MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  405. "Low Latency Frequency Transition capable processors");
  406. MODULE_LICENSE ("GPL");
  407. module_init(cpufreq_gov_dbs_init);
  408. module_exit(cpufreq_gov_dbs_exit);