cpufreq_ondemand.c 13 KB

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