cpufreq_ondemand.c 13 KB

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