cpufreq_ondemand.c 14 KB

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