cpufreq_ondemand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /*
  199. * Every sampling_rate, we check, if current idle time is less
  200. * than 20% (default), then we try to increase frequency
  201. * Every sampling_rate, we look for a the lowest
  202. * frequency which can sustain the load while keeping idle time over
  203. * 30%. If such a frequency exist, we try to decrease to this frequency.
  204. *
  205. * Any frequency increase takes it to the maximum frequency.
  206. * Frequency reduction happens at minimum steps of
  207. * 5% (default) of current frequency
  208. */
  209. /* Get Idle Time */
  210. idle_ticks = UINT_MAX;
  211. for_each_cpu_mask(j, policy->cpus) {
  212. cputime64_t total_idle_ticks;
  213. unsigned int tmp_idle_ticks;
  214. struct cpu_dbs_info_s *j_dbs_info;
  215. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  216. total_idle_ticks = get_cpu_idle_time(j);
  217. tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
  218. j_dbs_info->prev_cpu_idle);
  219. j_dbs_info->prev_cpu_idle = total_idle_ticks;
  220. if (tmp_idle_ticks < idle_ticks)
  221. idle_ticks = tmp_idle_ticks;
  222. }
  223. load = (100 * (total_ticks - idle_ticks)) / total_ticks;
  224. /* Check for frequency increase */
  225. if (load > dbs_tuners_ins.up_threshold) {
  226. /* if we are already at full speed then break out early */
  227. if (policy->cur == policy->max)
  228. return;
  229. __cpufreq_driver_target(policy, policy->max,
  230. CPUFREQ_RELATION_H);
  231. return;
  232. }
  233. /* Check for frequency decrease */
  234. /* if we cannot reduce the frequency anymore, break out early */
  235. if (policy->cur == policy->min)
  236. return;
  237. /*
  238. * The optimal frequency is the frequency that is the lowest that
  239. * can support the current CPU usage without triggering the up
  240. * policy. To be safe, we focus 10 points under the threshold.
  241. */
  242. if (load < (dbs_tuners_ins.up_threshold - 10)) {
  243. unsigned int freq_next;
  244. freq_next = (policy->cur * load) /
  245. (dbs_tuners_ins.up_threshold - 10);
  246. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
  247. }
  248. }
  249. static void do_dbs_timer(void *data)
  250. {
  251. unsigned int cpu = smp_processor_id();
  252. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  253. dbs_check_cpu(dbs_info);
  254. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
  255. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  256. }
  257. static inline void dbs_timer_init(unsigned int cpu)
  258. {
  259. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  260. INIT_WORK(&dbs_info->work, do_dbs_timer, 0);
  261. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
  262. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  263. return;
  264. }
  265. static inline void dbs_timer_exit(unsigned int cpu)
  266. {
  267. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  268. cancel_rearming_delayed_workqueue(kondemand_wq, &dbs_info->work);
  269. }
  270. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  271. unsigned int event)
  272. {
  273. unsigned int cpu = policy->cpu;
  274. struct cpu_dbs_info_s *this_dbs_info;
  275. unsigned int j;
  276. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  277. switch (event) {
  278. case CPUFREQ_GOV_START:
  279. if ((!cpu_online(cpu)) || (!policy->cur))
  280. return -EINVAL;
  281. if (policy->cpuinfo.transition_latency >
  282. (TRANSITION_LATENCY_LIMIT * 1000)) {
  283. printk(KERN_WARNING "ondemand governor failed to load "
  284. "due to too long transition latency\n");
  285. return -EINVAL;
  286. }
  287. if (this_dbs_info->enable) /* Already enabled */
  288. break;
  289. mutex_lock(&dbs_mutex);
  290. dbs_enable++;
  291. if (dbs_enable == 1) {
  292. kondemand_wq = create_workqueue("kondemand");
  293. if (!kondemand_wq) {
  294. printk(KERN_ERR "Creation of kondemand failed\n");
  295. dbs_enable--;
  296. mutex_unlock(&dbs_mutex);
  297. return -ENOSPC;
  298. }
  299. }
  300. for_each_cpu_mask(j, policy->cpus) {
  301. struct cpu_dbs_info_s *j_dbs_info;
  302. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  303. j_dbs_info->cur_policy = policy;
  304. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  305. j_dbs_info->prev_cpu_wall = get_jiffies_64();
  306. }
  307. this_dbs_info->enable = 1;
  308. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  309. /*
  310. * Start the timerschedule work, when this governor
  311. * is used for first time
  312. */
  313. if (dbs_enable == 1) {
  314. unsigned int latency;
  315. /* policy latency is in nS. Convert it to uS first */
  316. latency = policy->cpuinfo.transition_latency / 1000;
  317. if (latency == 0)
  318. latency = 1;
  319. def_sampling_rate = latency *
  320. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  321. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  322. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  323. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  324. }
  325. dbs_timer_init(policy->cpu);
  326. mutex_unlock(&dbs_mutex);
  327. break;
  328. case CPUFREQ_GOV_STOP:
  329. mutex_lock(&dbs_mutex);
  330. dbs_timer_exit(policy->cpu);
  331. this_dbs_info->enable = 0;
  332. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  333. dbs_enable--;
  334. if (dbs_enable == 0)
  335. destroy_workqueue(kondemand_wq);
  336. mutex_unlock(&dbs_mutex);
  337. break;
  338. case CPUFREQ_GOV_LIMITS:
  339. lock_cpu_hotplug();
  340. mutex_lock(&dbs_mutex);
  341. if (policy->max < this_dbs_info->cur_policy->cur)
  342. __cpufreq_driver_target(this_dbs_info->cur_policy,
  343. policy->max,
  344. CPUFREQ_RELATION_H);
  345. else if (policy->min > this_dbs_info->cur_policy->cur)
  346. __cpufreq_driver_target(this_dbs_info->cur_policy,
  347. policy->min,
  348. CPUFREQ_RELATION_L);
  349. mutex_unlock(&dbs_mutex);
  350. unlock_cpu_hotplug();
  351. break;
  352. }
  353. return 0;
  354. }
  355. static struct cpufreq_governor cpufreq_gov_dbs = {
  356. .name = "ondemand",
  357. .governor = cpufreq_governor_dbs,
  358. .owner = THIS_MODULE,
  359. };
  360. static int __init cpufreq_gov_dbs_init(void)
  361. {
  362. return cpufreq_register_governor(&cpufreq_gov_dbs);
  363. }
  364. static void __exit cpufreq_gov_dbs_exit(void)
  365. {
  366. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  367. }
  368. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  369. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  370. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  371. "Low Latency Frequency Transition capable processors");
  372. MODULE_LICENSE("GPL");
  373. module_init(cpufreq_gov_dbs_init);
  374. module_exit(cpufreq_gov_dbs_exit);