cpufreq_ondemand.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 (11)
  35. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  36. /*
  37. * The polling frequency of this governor depends on the capability of
  38. * the processor. Default polling frequency is 1000 times the transition
  39. * latency of the processor. The governor will work on any processor with
  40. * transition latency <= 10mS, using appropriate sampling
  41. * rate.
  42. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  43. * this governor will not work.
  44. * All times here are in uS.
  45. */
  46. static unsigned int def_sampling_rate;
  47. #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
  48. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  49. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  50. #define DEF_SAMPLING_DOWN_FACTOR (10)
  51. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  52. static void do_dbs_timer(void *data);
  53. struct cpu_dbs_info_s {
  54. struct cpufreq_policy *cur_policy;
  55. unsigned int prev_cpu_idle_up;
  56. unsigned int prev_cpu_idle_down;
  57. unsigned int enable;
  58. };
  59. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  60. static unsigned int dbs_enable; /* number of CPUs using this policy */
  61. static DECLARE_MUTEX (dbs_sem);
  62. static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
  63. struct dbs_tuners {
  64. unsigned int sampling_rate;
  65. unsigned int sampling_down_factor;
  66. unsigned int up_threshold;
  67. unsigned int ignore_nice;
  68. };
  69. static struct dbs_tuners dbs_tuners_ins = {
  70. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  71. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  72. };
  73. static inline unsigned int get_cpu_idle_time(unsigned int cpu)
  74. {
  75. return kstat_cpu(cpu).cpustat.idle +
  76. kstat_cpu(cpu).cpustat.iowait +
  77. ( !dbs_tuners_ins.ignore_nice ?
  78. kstat_cpu(cpu).cpustat.nice :
  79. 0);
  80. }
  81. /************************** sysfs interface ************************/
  82. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  83. {
  84. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  85. }
  86. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  87. {
  88. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  89. }
  90. #define define_one_ro(_name) \
  91. static struct freq_attr _name = \
  92. __ATTR(_name, 0444, show_##_name, NULL)
  93. define_one_ro(sampling_rate_max);
  94. define_one_ro(sampling_rate_min);
  95. /* cpufreq_ondemand Governor Tunables */
  96. #define show_one(file_name, object) \
  97. static ssize_t show_##file_name \
  98. (struct cpufreq_policy *unused, char *buf) \
  99. { \
  100. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  101. }
  102. show_one(sampling_rate, sampling_rate);
  103. show_one(sampling_down_factor, sampling_down_factor);
  104. show_one(up_threshold, up_threshold);
  105. show_one(ignore_nice, ignore_nice);
  106. static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
  107. const char *buf, size_t count)
  108. {
  109. unsigned int input;
  110. int ret;
  111. ret = sscanf (buf, "%u", &input);
  112. if (ret != 1 )
  113. return -EINVAL;
  114. down(&dbs_sem);
  115. dbs_tuners_ins.sampling_down_factor = input;
  116. up(&dbs_sem);
  117. return count;
  118. }
  119. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  120. const char *buf, size_t count)
  121. {
  122. unsigned int input;
  123. int ret;
  124. ret = sscanf (buf, "%u", &input);
  125. down(&dbs_sem);
  126. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  127. up(&dbs_sem);
  128. return -EINVAL;
  129. }
  130. dbs_tuners_ins.sampling_rate = input;
  131. up(&dbs_sem);
  132. return count;
  133. }
  134. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  135. const char *buf, size_t count)
  136. {
  137. unsigned int input;
  138. int ret;
  139. ret = sscanf (buf, "%u", &input);
  140. down(&dbs_sem);
  141. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  142. input < MIN_FREQUENCY_UP_THRESHOLD) {
  143. up(&dbs_sem);
  144. return -EINVAL;
  145. }
  146. dbs_tuners_ins.up_threshold = input;
  147. up(&dbs_sem);
  148. return count;
  149. }
  150. static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
  151. const char *buf, size_t count)
  152. {
  153. unsigned int input;
  154. int ret;
  155. unsigned int j;
  156. ret = sscanf (buf, "%u", &input);
  157. if ( ret != 1 )
  158. return -EINVAL;
  159. if ( input > 1 )
  160. input = 1;
  161. down(&dbs_sem);
  162. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  163. up(&dbs_sem);
  164. return count;
  165. }
  166. dbs_tuners_ins.ignore_nice = input;
  167. /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
  168. for_each_online_cpu(j) {
  169. struct cpu_dbs_info_s *j_dbs_info;
  170. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  171. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  172. j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
  173. }
  174. up(&dbs_sem);
  175. return count;
  176. }
  177. #define define_one_rw(_name) \
  178. static struct freq_attr _name = \
  179. __ATTR(_name, 0644, show_##_name, store_##_name)
  180. define_one_rw(sampling_rate);
  181. define_one_rw(sampling_down_factor);
  182. define_one_rw(up_threshold);
  183. define_one_rw(ignore_nice);
  184. static struct attribute * dbs_attributes[] = {
  185. &sampling_rate_max.attr,
  186. &sampling_rate_min.attr,
  187. &sampling_rate.attr,
  188. &sampling_down_factor.attr,
  189. &up_threshold.attr,
  190. &ignore_nice.attr,
  191. NULL
  192. };
  193. static struct attribute_group dbs_attr_group = {
  194. .attrs = dbs_attributes,
  195. .name = "ondemand",
  196. };
  197. /************************** sysfs end ************************/
  198. static void dbs_check_cpu(int cpu)
  199. {
  200. unsigned int idle_ticks, up_idle_ticks, total_ticks;
  201. unsigned int freq_next;
  202. unsigned int freq_down_sampling_rate;
  203. static int down_skip[NR_CPUS];
  204. struct cpu_dbs_info_s *this_dbs_info;
  205. struct cpufreq_policy *policy;
  206. unsigned int j;
  207. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  208. if (!this_dbs_info->enable)
  209. return;
  210. policy = this_dbs_info->cur_policy;
  211. /*
  212. * Every sampling_rate, we check, if current idle time is less
  213. * than 20% (default), then we try to increase frequency
  214. * Every sampling_rate*sampling_down_factor, we look for a the lowest
  215. * frequency which can sustain the load while keeping idle time over
  216. * 30%. If such a frequency exist, we try to decrease to this frequency.
  217. *
  218. * Any frequency increase takes it to the maximum frequency.
  219. * Frequency reduction happens at minimum steps of
  220. * 5% (default) of current frequency
  221. */
  222. /* Check for frequency increase */
  223. idle_ticks = UINT_MAX;
  224. for_each_cpu_mask(j, policy->cpus) {
  225. unsigned int tmp_idle_ticks, total_idle_ticks;
  226. struct cpu_dbs_info_s *j_dbs_info;
  227. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  228. total_idle_ticks = get_cpu_idle_time(j);
  229. tmp_idle_ticks = total_idle_ticks -
  230. j_dbs_info->prev_cpu_idle_up;
  231. j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  232. if (tmp_idle_ticks < idle_ticks)
  233. idle_ticks = tmp_idle_ticks;
  234. }
  235. /* Scale idle ticks by 100 and compare with up and down ticks */
  236. idle_ticks *= 100;
  237. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  238. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  239. if (idle_ticks < up_idle_ticks) {
  240. down_skip[cpu] = 0;
  241. for_each_cpu_mask(j, policy->cpus) {
  242. struct cpu_dbs_info_s *j_dbs_info;
  243. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  244. j_dbs_info->prev_cpu_idle_down =
  245. j_dbs_info->prev_cpu_idle_up;
  246. }
  247. /* if we are already at full speed then break out early */
  248. if (policy->cur == policy->max)
  249. return;
  250. __cpufreq_driver_target(policy, policy->max,
  251. CPUFREQ_RELATION_H);
  252. return;
  253. }
  254. /* Check for frequency decrease */
  255. down_skip[cpu]++;
  256. if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
  257. return;
  258. idle_ticks = UINT_MAX;
  259. for_each_cpu_mask(j, policy->cpus) {
  260. unsigned int tmp_idle_ticks, total_idle_ticks;
  261. struct cpu_dbs_info_s *j_dbs_info;
  262. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  263. /* Check for frequency decrease */
  264. total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
  265. tmp_idle_ticks = total_idle_ticks -
  266. j_dbs_info->prev_cpu_idle_down;
  267. j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  268. if (tmp_idle_ticks < idle_ticks)
  269. idle_ticks = tmp_idle_ticks;
  270. }
  271. down_skip[cpu] = 0;
  272. /* if we cannot reduce the frequency anymore, break out early */
  273. if (policy->cur == policy->min)
  274. return;
  275. /* Compute how many ticks there are between two measurements */
  276. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  277. dbs_tuners_ins.sampling_down_factor;
  278. total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
  279. /*
  280. * The optimal frequency is the frequency that is the lowest that
  281. * can support the current CPU usage without triggering the up
  282. * policy. To be safe, we focus 10 points under the threshold.
  283. */
  284. freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
  285. freq_next = (freq_next * policy->cur) /
  286. (dbs_tuners_ins.up_threshold - 10);
  287. if (freq_next <= ((policy->cur * 95) / 100))
  288. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
  289. }
  290. static void do_dbs_timer(void *data)
  291. {
  292. int i;
  293. down(&dbs_sem);
  294. for_each_online_cpu(i)
  295. dbs_check_cpu(i);
  296. schedule_delayed_work(&dbs_work,
  297. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  298. up(&dbs_sem);
  299. }
  300. static inline void dbs_timer_init(void)
  301. {
  302. INIT_WORK(&dbs_work, do_dbs_timer, NULL);
  303. schedule_delayed_work(&dbs_work,
  304. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  305. return;
  306. }
  307. static inline void dbs_timer_exit(void)
  308. {
  309. cancel_delayed_work(&dbs_work);
  310. return;
  311. }
  312. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  313. unsigned int event)
  314. {
  315. unsigned int cpu = policy->cpu;
  316. struct cpu_dbs_info_s *this_dbs_info;
  317. unsigned int j;
  318. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  319. switch (event) {
  320. case CPUFREQ_GOV_START:
  321. if ((!cpu_online(cpu)) ||
  322. (!policy->cur))
  323. return -EINVAL;
  324. if (policy->cpuinfo.transition_latency >
  325. (TRANSITION_LATENCY_LIMIT * 1000))
  326. return -EINVAL;
  327. if (this_dbs_info->enable) /* Already enabled */
  328. break;
  329. down(&dbs_sem);
  330. for_each_cpu_mask(j, policy->cpus) {
  331. struct cpu_dbs_info_s *j_dbs_info;
  332. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  333. j_dbs_info->cur_policy = policy;
  334. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  335. j_dbs_info->prev_cpu_idle_down
  336. = j_dbs_info->prev_cpu_idle_up;
  337. }
  338. this_dbs_info->enable = 1;
  339. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  340. dbs_enable++;
  341. /*
  342. * Start the timerschedule work, when this governor
  343. * is used for first time
  344. */
  345. if (dbs_enable == 1) {
  346. unsigned int latency;
  347. /* policy latency is in nS. Convert it to uS first */
  348. latency = policy->cpuinfo.transition_latency;
  349. if (latency < 1000)
  350. latency = 1000;
  351. def_sampling_rate = (latency / 1000) *
  352. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  353. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  354. dbs_tuners_ins.ignore_nice = 0;
  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. static struct cpufreq_governor cpufreq_gov_dbs = {
  388. .name = "ondemand",
  389. .governor = cpufreq_governor_dbs,
  390. .owner = THIS_MODULE,
  391. };
  392. static int __init cpufreq_gov_dbs_init(void)
  393. {
  394. return cpufreq_register_governor(&cpufreq_gov_dbs);
  395. }
  396. static void __exit cpufreq_gov_dbs_exit(void)
  397. {
  398. /* Make sure that the scheduled work is indeed not running */
  399. flush_scheduled_work();
  400. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  401. }
  402. MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  403. MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  404. "Low Latency Frequency Transition capable processors");
  405. MODULE_LICENSE ("GPL");
  406. module_init(cpufreq_gov_dbs_init);
  407. module_exit(cpufreq_gov_dbs_exit);