cpufreq_ondemand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/cpu.h>
  24. #include <linux/sched.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 MIN_FREQUENCY_UP_THRESHOLD (11)
  37. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  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 with CPUFREQ_ETERNAL)
  45. * 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 (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
  52. #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
  53. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  54. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  55. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  56. static void do_dbs_timer(void *data);
  57. struct cpu_dbs_info_s {
  58. cputime64_t prev_cpu_idle;
  59. cputime64_t prev_cpu_wall;
  60. struct cpufreq_policy *cur_policy;
  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. /*
  66. * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
  67. * lock and dbs_mutex. cpu_hotplug lock should always be held before
  68. * dbs_mutex. If any function that can potentially take cpu_hotplug lock
  69. * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
  70. * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
  71. * is recursive for the same process. -Venki
  72. */
  73. static DEFINE_MUTEX (dbs_mutex);
  74. static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
  75. static struct workqueue_struct *dbs_workq;
  76. struct dbs_tuners {
  77. unsigned int sampling_rate;
  78. unsigned int up_threshold;
  79. unsigned int ignore_nice;
  80. };
  81. static struct dbs_tuners dbs_tuners_ins = {
  82. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  83. .ignore_nice = 0,
  84. };
  85. static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
  86. {
  87. cputime64_t retval;
  88. retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
  89. kstat_cpu(cpu).cpustat.iowait);
  90. if (dbs_tuners_ins.ignore_nice)
  91. retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
  92. return retval;
  93. }
  94. /************************** sysfs interface ************************/
  95. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  96. {
  97. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  98. }
  99. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  100. {
  101. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  102. }
  103. #define define_one_ro(_name) \
  104. static struct freq_attr _name = \
  105. __ATTR(_name, 0444, show_##_name, NULL)
  106. define_one_ro(sampling_rate_max);
  107. define_one_ro(sampling_rate_min);
  108. /* cpufreq_ondemand Governor Tunables */
  109. #define show_one(file_name, object) \
  110. static ssize_t show_##file_name \
  111. (struct cpufreq_policy *unused, char *buf) \
  112. { \
  113. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  114. }
  115. show_one(sampling_rate, sampling_rate);
  116. show_one(up_threshold, up_threshold);
  117. show_one(ignore_nice_load, ignore_nice);
  118. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  119. const char *buf, size_t count)
  120. {
  121. unsigned int input;
  122. int ret;
  123. ret = sscanf (buf, "%u", &input);
  124. mutex_lock(&dbs_mutex);
  125. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  126. mutex_unlock(&dbs_mutex);
  127. return -EINVAL;
  128. }
  129. dbs_tuners_ins.sampling_rate = input;
  130. mutex_unlock(&dbs_mutex);
  131. return count;
  132. }
  133. static ssize_t store_up_threshold(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. mutex_lock(&dbs_mutex);
  140. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  141. input < MIN_FREQUENCY_UP_THRESHOLD) {
  142. mutex_unlock(&dbs_mutex);
  143. return -EINVAL;
  144. }
  145. dbs_tuners_ins.up_threshold = input;
  146. mutex_unlock(&dbs_mutex);
  147. return count;
  148. }
  149. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  150. const char *buf, size_t count)
  151. {
  152. unsigned int input;
  153. int ret;
  154. unsigned int j;
  155. ret = sscanf (buf, "%u", &input);
  156. if ( ret != 1 )
  157. return -EINVAL;
  158. if ( input > 1 )
  159. input = 1;
  160. mutex_lock(&dbs_mutex);
  161. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  162. mutex_unlock(&dbs_mutex);
  163. return count;
  164. }
  165. dbs_tuners_ins.ignore_nice = input;
  166. /* we need to re-evaluate prev_cpu_idle */
  167. for_each_online_cpu(j) {
  168. struct cpu_dbs_info_s *dbs_info;
  169. dbs_info = &per_cpu(cpu_dbs_info, j);
  170. dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  171. dbs_info->prev_cpu_wall = get_jiffies_64();
  172. }
  173. mutex_unlock(&dbs_mutex);
  174. return count;
  175. }
  176. #define define_one_rw(_name) \
  177. static struct freq_attr _name = \
  178. __ATTR(_name, 0644, show_##_name, store_##_name)
  179. define_one_rw(sampling_rate);
  180. define_one_rw(up_threshold);
  181. define_one_rw(ignore_nice_load);
  182. static struct attribute * dbs_attributes[] = {
  183. &sampling_rate_max.attr,
  184. &sampling_rate_min.attr,
  185. &sampling_rate.attr,
  186. &up_threshold.attr,
  187. &ignore_nice_load.attr,
  188. NULL
  189. };
  190. static struct attribute_group dbs_attr_group = {
  191. .attrs = dbs_attributes,
  192. .name = "ondemand",
  193. };
  194. /************************** sysfs end ************************/
  195. static void dbs_check_cpu(int cpu)
  196. {
  197. unsigned int idle_ticks, total_ticks;
  198. unsigned int load;
  199. struct cpu_dbs_info_s *this_dbs_info;
  200. cputime64_t cur_jiffies;
  201. struct cpufreq_policy *policy;
  202. unsigned int j;
  203. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  204. if (!this_dbs_info->enable)
  205. return;
  206. policy = this_dbs_info->cur_policy;
  207. cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
  208. total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
  209. this_dbs_info->prev_cpu_wall);
  210. this_dbs_info->prev_cpu_wall = cur_jiffies;
  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, 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. /* Get Idle Time */
  223. idle_ticks = UINT_MAX;
  224. for_each_cpu_mask(j, policy->cpus) {
  225. cputime64_t total_idle_ticks;
  226. unsigned int tmp_idle_ticks;
  227. struct cpu_dbs_info_s *j_dbs_info;
  228. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  229. total_idle_ticks = get_cpu_idle_time(j);
  230. tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
  231. j_dbs_info->prev_cpu_idle);
  232. j_dbs_info->prev_cpu_idle = total_idle_ticks;
  233. if (tmp_idle_ticks < idle_ticks)
  234. idle_ticks = tmp_idle_ticks;
  235. }
  236. load = (100 * (total_ticks - idle_ticks)) / total_ticks;
  237. /* Check for frequency increase */
  238. if (load > dbs_tuners_ins.up_threshold) {
  239. /* if we are already at full speed then break out early */
  240. if (policy->cur == policy->max)
  241. return;
  242. __cpufreq_driver_target(policy, policy->max,
  243. CPUFREQ_RELATION_H);
  244. return;
  245. }
  246. /* Check for frequency decrease */
  247. /* if we cannot reduce the frequency anymore, break out early */
  248. if (policy->cur == policy->min)
  249. return;
  250. /*
  251. * The optimal frequency is the frequency that is the lowest that
  252. * can support the current CPU usage without triggering the up
  253. * policy. To be safe, we focus 10 points under the threshold.
  254. */
  255. if (load < (dbs_tuners_ins.up_threshold - 10)) {
  256. unsigned int freq_next;
  257. freq_next = (policy->cur * load) /
  258. (dbs_tuners_ins.up_threshold - 10);
  259. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
  260. }
  261. }
  262. static void do_dbs_timer(void *data)
  263. {
  264. int i;
  265. lock_cpu_hotplug();
  266. mutex_lock(&dbs_mutex);
  267. for_each_online_cpu(i)
  268. dbs_check_cpu(i);
  269. queue_delayed_work(dbs_workq, &dbs_work,
  270. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  271. mutex_unlock(&dbs_mutex);
  272. unlock_cpu_hotplug();
  273. }
  274. static inline void dbs_timer_init(void)
  275. {
  276. INIT_WORK(&dbs_work, do_dbs_timer, NULL);
  277. if (!dbs_workq)
  278. dbs_workq = create_singlethread_workqueue("ondemand");
  279. if (!dbs_workq) {
  280. printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
  281. return;
  282. }
  283. queue_delayed_work(dbs_workq, &dbs_work,
  284. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  285. return;
  286. }
  287. static inline void dbs_timer_exit(void)
  288. {
  289. if (dbs_workq)
  290. cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
  291. }
  292. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  293. unsigned int event)
  294. {
  295. unsigned int cpu = policy->cpu;
  296. struct cpu_dbs_info_s *this_dbs_info;
  297. unsigned int j;
  298. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  299. switch (event) {
  300. case CPUFREQ_GOV_START:
  301. if ((!cpu_online(cpu)) ||
  302. (!policy->cur))
  303. return -EINVAL;
  304. if (policy->cpuinfo.transition_latency >
  305. (TRANSITION_LATENCY_LIMIT * 1000)) {
  306. printk(KERN_WARNING "ondemand governor failed to load "
  307. "due to too long transition latency\n");
  308. return -EINVAL;
  309. }
  310. if (this_dbs_info->enable) /* Already enabled */
  311. break;
  312. mutex_lock(&dbs_mutex);
  313. for_each_cpu_mask(j, policy->cpus) {
  314. struct cpu_dbs_info_s *j_dbs_info;
  315. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  316. j_dbs_info->cur_policy = policy;
  317. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  318. j_dbs_info->prev_cpu_wall = get_jiffies_64();
  319. }
  320. this_dbs_info->enable = 1;
  321. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  322. dbs_enable++;
  323. /*
  324. * Start the timerschedule work, when this governor
  325. * is used for first time
  326. */
  327. if (dbs_enable == 1) {
  328. unsigned int latency;
  329. /* policy latency is in nS. Convert it to uS first */
  330. latency = policy->cpuinfo.transition_latency / 1000;
  331. if (latency == 0)
  332. latency = 1;
  333. def_sampling_rate = latency *
  334. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  335. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  336. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  337. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  338. dbs_timer_init();
  339. }
  340. mutex_unlock(&dbs_mutex);
  341. break;
  342. case CPUFREQ_GOV_STOP:
  343. mutex_lock(&dbs_mutex);
  344. this_dbs_info->enable = 0;
  345. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  346. dbs_enable--;
  347. /*
  348. * Stop the timerschedule work, when this governor
  349. * is used for first time
  350. */
  351. if (dbs_enable == 0)
  352. dbs_timer_exit();
  353. mutex_unlock(&dbs_mutex);
  354. break;
  355. case CPUFREQ_GOV_LIMITS:
  356. lock_cpu_hotplug();
  357. mutex_lock(&dbs_mutex);
  358. if (policy->max < this_dbs_info->cur_policy->cur)
  359. __cpufreq_driver_target(
  360. this_dbs_info->cur_policy,
  361. policy->max, CPUFREQ_RELATION_H);
  362. else if (policy->min > this_dbs_info->cur_policy->cur)
  363. __cpufreq_driver_target(
  364. this_dbs_info->cur_policy,
  365. policy->min, CPUFREQ_RELATION_L);
  366. mutex_unlock(&dbs_mutex);
  367. unlock_cpu_hotplug();
  368. break;
  369. }
  370. return 0;
  371. }
  372. static struct cpufreq_governor cpufreq_gov_dbs = {
  373. .name = "ondemand",
  374. .governor = cpufreq_governor_dbs,
  375. .owner = THIS_MODULE,
  376. };
  377. static int __init cpufreq_gov_dbs_init(void)
  378. {
  379. return cpufreq_register_governor(&cpufreq_gov_dbs);
  380. }
  381. static void __exit cpufreq_gov_dbs_exit(void)
  382. {
  383. /* Make sure that the scheduled work is indeed not running.
  384. Assumes the timer has been cancelled first. */
  385. if (dbs_workq) {
  386. flush_workqueue(dbs_workq);
  387. destroy_workqueue(dbs_workq);
  388. }
  389. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  390. }
  391. MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  392. MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  393. "Low Latency Frequency Transition capable processors");
  394. MODULE_LICENSE ("GPL");
  395. module_init(cpufreq_gov_dbs_init);
  396. module_exit(cpufreq_gov_dbs_exit);