cpufreq_ondemand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/cpu.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mutex.h>
  20. /*
  21. * dbs is used in this file as a shortform for demandbased switching
  22. * It helps to keep variable names smaller, simpler
  23. */
  24. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  25. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  26. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  27. /*
  28. * The polling frequency of this governor depends on the capability of
  29. * the processor. Default polling frequency is 1000 times the transition
  30. * latency of the processor. The governor will work on any processor with
  31. * transition latency <= 10mS, using appropriate sampling
  32. * rate.
  33. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  34. * this governor will not work.
  35. * All times here are in uS.
  36. */
  37. static unsigned int def_sampling_rate;
  38. #define MIN_SAMPLING_RATE_RATIO (2)
  39. /* for correct statistics, we need at least 10 ticks between each measure */
  40. #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
  41. #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
  42. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  43. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  44. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  45. static void do_dbs_timer(void *data);
  46. struct cpu_dbs_info_s {
  47. cputime64_t prev_cpu_idle;
  48. cputime64_t prev_cpu_wall;
  49. struct cpufreq_policy *cur_policy;
  50. struct work_struct work;
  51. unsigned int enable;
  52. };
  53. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  54. static unsigned int dbs_enable; /* number of CPUs using this policy */
  55. /*
  56. * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
  57. * lock and dbs_mutex. cpu_hotplug lock should always be held before
  58. * dbs_mutex. If any function that can potentially take cpu_hotplug lock
  59. * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
  60. * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
  61. * is recursive for the same process. -Venki
  62. */
  63. static DEFINE_MUTEX(dbs_mutex);
  64. static struct workqueue_struct *kondemand_wq;
  65. struct dbs_tuners {
  66. unsigned int sampling_rate;
  67. unsigned int up_threshold;
  68. unsigned int ignore_nice;
  69. };
  70. static struct dbs_tuners dbs_tuners_ins = {
  71. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  72. .ignore_nice = 0,
  73. };
  74. static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
  75. {
  76. cputime64_t retval;
  77. retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
  78. kstat_cpu(cpu).cpustat.iowait);
  79. if (dbs_tuners_ins.ignore_nice)
  80. retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
  81. return retval;
  82. }
  83. /************************** sysfs interface ************************/
  84. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  85. {
  86. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  87. }
  88. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  89. {
  90. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  91. }
  92. #define define_one_ro(_name) \
  93. static struct freq_attr _name = \
  94. __ATTR(_name, 0444, show_##_name, NULL)
  95. define_one_ro(sampling_rate_max);
  96. define_one_ro(sampling_rate_min);
  97. /* cpufreq_ondemand Governor Tunables */
  98. #define show_one(file_name, object) \
  99. static ssize_t show_##file_name \
  100. (struct cpufreq_policy *unused, char *buf) \
  101. { \
  102. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  103. }
  104. show_one(sampling_rate, sampling_rate);
  105. show_one(up_threshold, up_threshold);
  106. show_one(ignore_nice_load, ignore_nice);
  107. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  108. const char *buf, size_t count)
  109. {
  110. unsigned int input;
  111. int ret;
  112. ret = sscanf(buf, "%u", &input);
  113. mutex_lock(&dbs_mutex);
  114. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  115. mutex_unlock(&dbs_mutex);
  116. return -EINVAL;
  117. }
  118. dbs_tuners_ins.sampling_rate = input;
  119. mutex_unlock(&dbs_mutex);
  120. return count;
  121. }
  122. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  123. const char *buf, size_t count)
  124. {
  125. unsigned int input;
  126. int ret;
  127. ret = sscanf(buf, "%u", &input);
  128. mutex_lock(&dbs_mutex);
  129. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  130. input < MIN_FREQUENCY_UP_THRESHOLD) {
  131. mutex_unlock(&dbs_mutex);
  132. return -EINVAL;
  133. }
  134. dbs_tuners_ins.up_threshold = input;
  135. mutex_unlock(&dbs_mutex);
  136. return count;
  137. }
  138. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  139. const char *buf, size_t count)
  140. {
  141. unsigned int input;
  142. int ret;
  143. unsigned int j;
  144. ret = sscanf(buf, "%u", &input);
  145. if ( ret != 1 )
  146. return -EINVAL;
  147. if ( input > 1 )
  148. input = 1;
  149. mutex_lock(&dbs_mutex);
  150. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  151. mutex_unlock(&dbs_mutex);
  152. return count;
  153. }
  154. dbs_tuners_ins.ignore_nice = input;
  155. /* we need to re-evaluate prev_cpu_idle */
  156. for_each_online_cpu(j) {
  157. struct cpu_dbs_info_s *dbs_info;
  158. dbs_info = &per_cpu(cpu_dbs_info, j);
  159. dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  160. dbs_info->prev_cpu_wall = get_jiffies_64();
  161. }
  162. mutex_unlock(&dbs_mutex);
  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(up_threshold);
  170. define_one_rw(ignore_nice_load);
  171. static struct attribute * dbs_attributes[] = {
  172. &sampling_rate_max.attr,
  173. &sampling_rate_min.attr,
  174. &sampling_rate.attr,
  175. &up_threshold.attr,
  176. &ignore_nice_load.attr,
  177. NULL
  178. };
  179. static struct attribute_group dbs_attr_group = {
  180. .attrs = dbs_attributes,
  181. .name = "ondemand",
  182. };
  183. /************************** sysfs end ************************/
  184. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  185. {
  186. unsigned int idle_ticks, total_ticks;
  187. unsigned int load;
  188. cputime64_t cur_jiffies;
  189. struct cpufreq_policy *policy;
  190. unsigned int j;
  191. if (!this_dbs_info->enable)
  192. return;
  193. policy = this_dbs_info->cur_policy;
  194. cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
  195. total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
  196. this_dbs_info->prev_cpu_wall);
  197. this_dbs_info->prev_cpu_wall = cur_jiffies;
  198. if (!total_ticks)
  199. return;
  200. /*
  201. * Every sampling_rate, we check, if current idle time is less
  202. * than 20% (default), then we try to increase frequency
  203. * Every sampling_rate, we look for a the lowest
  204. * frequency which can sustain the load while keeping idle time over
  205. * 30%. If such a frequency exist, we try to decrease to this frequency.
  206. *
  207. * Any frequency increase takes it to the maximum frequency.
  208. * Frequency reduction happens at minimum steps of
  209. * 5% (default) of current frequency
  210. */
  211. /* Get Idle Time */
  212. idle_ticks = UINT_MAX;
  213. for_each_cpu_mask(j, policy->cpus) {
  214. cputime64_t total_idle_ticks;
  215. unsigned int tmp_idle_ticks;
  216. struct cpu_dbs_info_s *j_dbs_info;
  217. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  218. total_idle_ticks = get_cpu_idle_time(j);
  219. tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
  220. j_dbs_info->prev_cpu_idle);
  221. j_dbs_info->prev_cpu_idle = total_idle_ticks;
  222. if (tmp_idle_ticks < idle_ticks)
  223. idle_ticks = tmp_idle_ticks;
  224. }
  225. load = (100 * (total_ticks - idle_ticks)) / total_ticks;
  226. /* Check for frequency increase */
  227. if (load > dbs_tuners_ins.up_threshold) {
  228. /* if we are already at full speed then break out early */
  229. if (policy->cur == policy->max)
  230. return;
  231. __cpufreq_driver_target(policy, policy->max,
  232. CPUFREQ_RELATION_H);
  233. return;
  234. }
  235. /* Check for frequency decrease */
  236. /* if we cannot reduce the frequency anymore, break out early */
  237. if (policy->cur == policy->min)
  238. return;
  239. /*
  240. * The optimal frequency is the frequency that is the lowest that
  241. * can support the current CPU usage without triggering the up
  242. * policy. To be safe, we focus 10 points under the threshold.
  243. */
  244. if (load < (dbs_tuners_ins.up_threshold - 10)) {
  245. unsigned int freq_next;
  246. freq_next = (policy->cur * load) /
  247. (dbs_tuners_ins.up_threshold - 10);
  248. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
  249. }
  250. }
  251. static void do_dbs_timer(void *data)
  252. {
  253. unsigned int cpu = smp_processor_id();
  254. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  255. if (!dbs_info->enable)
  256. return;
  257. lock_cpu_hotplug();
  258. dbs_check_cpu(dbs_info);
  259. unlock_cpu_hotplug();
  260. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
  261. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  262. }
  263. static inline void dbs_timer_init(unsigned int cpu)
  264. {
  265. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  266. INIT_WORK(&dbs_info->work, do_dbs_timer, 0);
  267. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
  268. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  269. return;
  270. }
  271. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  272. {
  273. dbs_info->enable = 0;
  274. cancel_delayed_work(&dbs_info->work);
  275. flush_workqueue(kondemand_wq);
  276. }
  277. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  278. unsigned int event)
  279. {
  280. unsigned int cpu = policy->cpu;
  281. struct cpu_dbs_info_s *this_dbs_info;
  282. unsigned int j;
  283. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  284. switch (event) {
  285. case CPUFREQ_GOV_START:
  286. if ((!cpu_online(cpu)) || (!policy->cur))
  287. return -EINVAL;
  288. if (policy->cpuinfo.transition_latency >
  289. (TRANSITION_LATENCY_LIMIT * 1000)) {
  290. printk(KERN_WARNING "ondemand governor failed to load "
  291. "due to too long transition latency\n");
  292. return -EINVAL;
  293. }
  294. if (this_dbs_info->enable) /* Already enabled */
  295. break;
  296. mutex_lock(&dbs_mutex);
  297. dbs_enable++;
  298. if (dbs_enable == 1) {
  299. kondemand_wq = create_workqueue("kondemand");
  300. if (!kondemand_wq) {
  301. printk(KERN_ERR "Creation of kondemand failed\n");
  302. dbs_enable--;
  303. mutex_unlock(&dbs_mutex);
  304. return -ENOSPC;
  305. }
  306. }
  307. for_each_cpu_mask(j, policy->cpus) {
  308. struct cpu_dbs_info_s *j_dbs_info;
  309. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  310. j_dbs_info->cur_policy = policy;
  311. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  312. j_dbs_info->prev_cpu_wall = get_jiffies_64();
  313. }
  314. this_dbs_info->enable = 1;
  315. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  316. /*
  317. * Start the timerschedule work, when this governor
  318. * is used for first time
  319. */
  320. if (dbs_enable == 1) {
  321. unsigned int latency;
  322. /* policy latency is in nS. Convert it to uS first */
  323. latency = policy->cpuinfo.transition_latency / 1000;
  324. if (latency == 0)
  325. latency = 1;
  326. def_sampling_rate = latency *
  327. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  328. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  329. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  330. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  331. }
  332. dbs_timer_init(policy->cpu);
  333. mutex_unlock(&dbs_mutex);
  334. break;
  335. case CPUFREQ_GOV_STOP:
  336. mutex_lock(&dbs_mutex);
  337. dbs_timer_exit(this_dbs_info);
  338. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  339. dbs_enable--;
  340. if (dbs_enable == 0)
  341. destroy_workqueue(kondemand_wq);
  342. mutex_unlock(&dbs_mutex);
  343. break;
  344. case CPUFREQ_GOV_LIMITS:
  345. mutex_lock(&dbs_mutex);
  346. if (policy->max < this_dbs_info->cur_policy->cur)
  347. __cpufreq_driver_target(this_dbs_info->cur_policy,
  348. policy->max,
  349. CPUFREQ_RELATION_H);
  350. else if (policy->min > this_dbs_info->cur_policy->cur)
  351. __cpufreq_driver_target(this_dbs_info->cur_policy,
  352. policy->min,
  353. CPUFREQ_RELATION_L);
  354. mutex_unlock(&dbs_mutex);
  355. break;
  356. }
  357. return 0;
  358. }
  359. static struct cpufreq_governor cpufreq_gov_dbs = {
  360. .name = "ondemand",
  361. .governor = cpufreq_governor_dbs,
  362. .owner = THIS_MODULE,
  363. };
  364. static int __init cpufreq_gov_dbs_init(void)
  365. {
  366. return cpufreq_register_governor(&cpufreq_gov_dbs);
  367. }
  368. static void __exit cpufreq_gov_dbs_exit(void)
  369. {
  370. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  371. }
  372. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  373. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  374. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  375. "Low Latency Frequency Transition capable processors");
  376. MODULE_LICENSE("GPL");
  377. module_init(cpufreq_gov_dbs_init);
  378. module_exit(cpufreq_gov_dbs_exit);