cpufreq_ondemand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. /*
  30. * dbs is used in this file as a shortform for demandbased switching
  31. * It helps to keep variable names smaller, simpler
  32. */
  33. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  34. #define MIN_FREQUENCY_UP_THRESHOLD (0)
  35. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  36. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  37. #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
  38. #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
  39. /*
  40. * The polling frequency of this governor depends on the capability of
  41. * the processor. Default polling frequency is 1000 times the transition
  42. * latency of the processor. The governor will work on any processor with
  43. * transition latency <= 10mS, using appropriate sampling
  44. * rate.
  45. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  46. * this governor will not work.
  47. * All times here are in uS.
  48. */
  49. static unsigned int def_sampling_rate;
  50. #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
  51. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  52. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  53. #define DEF_SAMPLING_DOWN_FACTOR (10)
  54. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  55. static void do_dbs_timer(void *data);
  56. struct cpu_dbs_info_s {
  57. struct cpufreq_policy *cur_policy;
  58. unsigned int prev_cpu_idle_up;
  59. unsigned int prev_cpu_idle_down;
  60. unsigned int enable;
  61. };
  62. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  63. static unsigned int dbs_enable; /* number of CPUs using this policy */
  64. static DECLARE_MUTEX (dbs_sem);
  65. static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
  66. struct dbs_tuners {
  67. unsigned int sampling_rate;
  68. unsigned int sampling_down_factor;
  69. unsigned int up_threshold;
  70. unsigned int down_threshold;
  71. unsigned int ignore_nice;
  72. unsigned int freq_step;
  73. };
  74. static struct dbs_tuners dbs_tuners_ins = {
  75. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  76. .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  77. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  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(down_threshold, down_threshold);
  112. show_one(ignore_nice, ignore_nice);
  113. show_one(freq_step, freq_step);
  114. static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
  115. const char *buf, size_t count)
  116. {
  117. unsigned int input;
  118. int ret;
  119. ret = sscanf (buf, "%u", &input);
  120. if (ret != 1 )
  121. return -EINVAL;
  122. down(&dbs_sem);
  123. dbs_tuners_ins.sampling_down_factor = input;
  124. up(&dbs_sem);
  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. down(&dbs_sem);
  134. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  135. up(&dbs_sem);
  136. return -EINVAL;
  137. }
  138. dbs_tuners_ins.sampling_rate = input;
  139. up(&dbs_sem);
  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. down(&dbs_sem);
  149. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  150. input < MIN_FREQUENCY_UP_THRESHOLD ||
  151. input <= dbs_tuners_ins.down_threshold) {
  152. up(&dbs_sem);
  153. return -EINVAL;
  154. }
  155. dbs_tuners_ins.up_threshold = input;
  156. up(&dbs_sem);
  157. return count;
  158. }
  159. static ssize_t store_down_threshold(struct cpufreq_policy *unused,
  160. const char *buf, size_t count)
  161. {
  162. unsigned int input;
  163. int ret;
  164. ret = sscanf (buf, "%u", &input);
  165. down(&dbs_sem);
  166. if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
  167. input < MIN_FREQUENCY_DOWN_THRESHOLD ||
  168. input >= dbs_tuners_ins.up_threshold) {
  169. up(&dbs_sem);
  170. return -EINVAL;
  171. }
  172. dbs_tuners_ins.down_threshold = input;
  173. up(&dbs_sem);
  174. return count;
  175. }
  176. static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
  177. const char *buf, size_t count)
  178. {
  179. unsigned int input;
  180. int ret;
  181. unsigned int j;
  182. ret = sscanf (buf, "%u", &input);
  183. if ( ret != 1 )
  184. return -EINVAL;
  185. if ( input > 1 )
  186. input = 1;
  187. down(&dbs_sem);
  188. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  189. up(&dbs_sem);
  190. return count;
  191. }
  192. dbs_tuners_ins.ignore_nice = input;
  193. /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
  194. for_each_online_cpu(j) {
  195. struct cpu_dbs_info_s *j_dbs_info;
  196. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  197. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  198. j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
  199. }
  200. up(&dbs_sem);
  201. return count;
  202. }
  203. static ssize_t store_freq_step(struct cpufreq_policy *policy,
  204. const char *buf, size_t count)
  205. {
  206. unsigned int input;
  207. int ret;
  208. ret = sscanf (buf, "%u", &input);
  209. if ( ret != 1 )
  210. return -EINVAL;
  211. if ( input > 100 )
  212. input = 100;
  213. /* no need to test here if freq_step is zero as the user might actually
  214. * want this, they would be crazy though :) */
  215. down(&dbs_sem);
  216. dbs_tuners_ins.freq_step = input;
  217. up(&dbs_sem);
  218. return count;
  219. }
  220. #define define_one_rw(_name) \
  221. static struct freq_attr _name = \
  222. __ATTR(_name, 0644, show_##_name, store_##_name)
  223. define_one_rw(sampling_rate);
  224. define_one_rw(sampling_down_factor);
  225. define_one_rw(up_threshold);
  226. define_one_rw(down_threshold);
  227. define_one_rw(ignore_nice);
  228. define_one_rw(freq_step);
  229. static struct attribute * dbs_attributes[] = {
  230. &sampling_rate_max.attr,
  231. &sampling_rate_min.attr,
  232. &sampling_rate.attr,
  233. &sampling_down_factor.attr,
  234. &up_threshold.attr,
  235. &down_threshold.attr,
  236. &ignore_nice.attr,
  237. &freq_step.attr,
  238. NULL
  239. };
  240. static struct attribute_group dbs_attr_group = {
  241. .attrs = dbs_attributes,
  242. .name = "ondemand",
  243. };
  244. /************************** sysfs end ************************/
  245. static void dbs_check_cpu(int cpu)
  246. {
  247. unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
  248. unsigned int total_idle_ticks;
  249. unsigned int freq_down_step;
  250. unsigned int freq_down_sampling_rate;
  251. static int down_skip[NR_CPUS];
  252. struct cpu_dbs_info_s *this_dbs_info;
  253. struct cpufreq_policy *policy;
  254. unsigned int j;
  255. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  256. if (!this_dbs_info->enable)
  257. return;
  258. policy = this_dbs_info->cur_policy;
  259. /*
  260. * The default safe range is 20% to 80%
  261. * Every sampling_rate, we check
  262. * - If current idle time is less than 20%, then we try to
  263. * increase frequency
  264. * Every sampling_rate*sampling_down_factor, we check
  265. * - If current idle time is more than 80%, then we try to
  266. * decrease frequency
  267. *
  268. * Any frequency increase takes it to the maximum frequency.
  269. * Frequency reduction happens at minimum steps of
  270. * 5% (default) of max_frequency
  271. */
  272. /* Check for frequency increase */
  273. total_idle_ticks = get_cpu_idle_time(cpu);
  274. idle_ticks = total_idle_ticks -
  275. this_dbs_info->prev_cpu_idle_up;
  276. this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  277. for_each_cpu_mask(j, policy->cpus) {
  278. unsigned int tmp_idle_ticks;
  279. struct cpu_dbs_info_s *j_dbs_info;
  280. if (j == cpu)
  281. continue;
  282. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  283. /* Check for frequency increase */
  284. total_idle_ticks = get_cpu_idle_time(j);
  285. tmp_idle_ticks = total_idle_ticks -
  286. j_dbs_info->prev_cpu_idle_up;
  287. j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  288. if (tmp_idle_ticks < idle_ticks)
  289. idle_ticks = tmp_idle_ticks;
  290. }
  291. /* Scale idle ticks by 100 and compare with up and down ticks */
  292. idle_ticks *= 100;
  293. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  294. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  295. if (idle_ticks < up_idle_ticks) {
  296. down_skip[cpu] = 0;
  297. for_each_cpu_mask(j, policy->cpus) {
  298. struct cpu_dbs_info_s *j_dbs_info;
  299. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  300. j_dbs_info->prev_cpu_idle_down =
  301. j_dbs_info->prev_cpu_idle_up;
  302. }
  303. /* if we are already at full speed then break out early */
  304. if (policy->cur == policy->max)
  305. return;
  306. __cpufreq_driver_target(policy, policy->max,
  307. CPUFREQ_RELATION_H);
  308. return;
  309. }
  310. /* Check for frequency decrease */
  311. down_skip[cpu]++;
  312. if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
  313. return;
  314. total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
  315. idle_ticks = total_idle_ticks -
  316. this_dbs_info->prev_cpu_idle_down;
  317. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  318. for_each_cpu_mask(j, policy->cpus) {
  319. unsigned int tmp_idle_ticks;
  320. struct cpu_dbs_info_s *j_dbs_info;
  321. if (j == cpu)
  322. continue;
  323. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  324. /* Check for frequency decrease */
  325. total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
  326. tmp_idle_ticks = total_idle_ticks -
  327. j_dbs_info->prev_cpu_idle_down;
  328. j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  329. if (tmp_idle_ticks < idle_ticks)
  330. idle_ticks = tmp_idle_ticks;
  331. }
  332. /* Scale idle ticks by 100 and compare with up and down ticks */
  333. idle_ticks *= 100;
  334. down_skip[cpu] = 0;
  335. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  336. dbs_tuners_ins.sampling_down_factor;
  337. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  338. usecs_to_jiffies(freq_down_sampling_rate);
  339. if (idle_ticks > down_idle_ticks ) {
  340. /* if we are already at the lowest speed then break out early
  341. * or if we 'cannot' reduce the speed as the user might want
  342. * freq_step to be zero */
  343. if (policy->cur == policy->min || dbs_tuners_ins.freq_step == 0)
  344. return;
  345. freq_down_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
  346. /* max freq cannot be less than 100. But who knows.... */
  347. if (unlikely(freq_down_step == 0))
  348. freq_down_step = 5;
  349. __cpufreq_driver_target(policy,
  350. policy->cur - freq_down_step,
  351. CPUFREQ_RELATION_H);
  352. return;
  353. }
  354. }
  355. static void do_dbs_timer(void *data)
  356. {
  357. int i;
  358. down(&dbs_sem);
  359. for_each_online_cpu(i)
  360. dbs_check_cpu(i);
  361. schedule_delayed_work(&dbs_work,
  362. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  363. up(&dbs_sem);
  364. }
  365. static inline void dbs_timer_init(void)
  366. {
  367. INIT_WORK(&dbs_work, do_dbs_timer, NULL);
  368. schedule_delayed_work(&dbs_work,
  369. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  370. return;
  371. }
  372. static inline void dbs_timer_exit(void)
  373. {
  374. cancel_delayed_work(&dbs_work);
  375. return;
  376. }
  377. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  378. unsigned int event)
  379. {
  380. unsigned int cpu = policy->cpu;
  381. struct cpu_dbs_info_s *this_dbs_info;
  382. unsigned int j;
  383. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  384. switch (event) {
  385. case CPUFREQ_GOV_START:
  386. if ((!cpu_online(cpu)) ||
  387. (!policy->cur))
  388. return -EINVAL;
  389. if (policy->cpuinfo.transition_latency >
  390. (TRANSITION_LATENCY_LIMIT * 1000))
  391. return -EINVAL;
  392. if (this_dbs_info->enable) /* Already enabled */
  393. break;
  394. down(&dbs_sem);
  395. for_each_cpu_mask(j, policy->cpus) {
  396. struct cpu_dbs_info_s *j_dbs_info;
  397. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  398. j_dbs_info->cur_policy = policy;
  399. j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
  400. j_dbs_info->prev_cpu_idle_down
  401. = j_dbs_info->prev_cpu_idle_up;
  402. }
  403. this_dbs_info->enable = 1;
  404. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  405. dbs_enable++;
  406. /*
  407. * Start the timerschedule work, when this governor
  408. * is used for first time
  409. */
  410. if (dbs_enable == 1) {
  411. unsigned int latency;
  412. /* policy latency is in nS. Convert it to uS first */
  413. latency = policy->cpuinfo.transition_latency;
  414. if (latency < 1000)
  415. latency = 1000;
  416. def_sampling_rate = (latency / 1000) *
  417. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  418. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  419. dbs_tuners_ins.ignore_nice = 0;
  420. dbs_tuners_ins.freq_step = 5;
  421. dbs_timer_init();
  422. }
  423. up(&dbs_sem);
  424. break;
  425. case CPUFREQ_GOV_STOP:
  426. down(&dbs_sem);
  427. this_dbs_info->enable = 0;
  428. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  429. dbs_enable--;
  430. /*
  431. * Stop the timerschedule work, when this governor
  432. * is used for first time
  433. */
  434. if (dbs_enable == 0)
  435. dbs_timer_exit();
  436. up(&dbs_sem);
  437. break;
  438. case CPUFREQ_GOV_LIMITS:
  439. down(&dbs_sem);
  440. if (policy->max < this_dbs_info->cur_policy->cur)
  441. __cpufreq_driver_target(
  442. this_dbs_info->cur_policy,
  443. policy->max, CPUFREQ_RELATION_H);
  444. else if (policy->min > this_dbs_info->cur_policy->cur)
  445. __cpufreq_driver_target(
  446. this_dbs_info->cur_policy,
  447. policy->min, CPUFREQ_RELATION_L);
  448. up(&dbs_sem);
  449. break;
  450. }
  451. return 0;
  452. }
  453. static struct cpufreq_governor cpufreq_gov_dbs = {
  454. .name = "ondemand",
  455. .governor = cpufreq_governor_dbs,
  456. .owner = THIS_MODULE,
  457. };
  458. static int __init cpufreq_gov_dbs_init(void)
  459. {
  460. return cpufreq_register_governor(&cpufreq_gov_dbs);
  461. }
  462. static void __exit cpufreq_gov_dbs_exit(void)
  463. {
  464. /* Make sure that the scheduled work is indeed not running */
  465. flush_scheduled_work();
  466. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  467. }
  468. MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  469. MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  470. "Low Latency Frequency Transition capable processors");
  471. MODULE_LICENSE ("GPL");
  472. module_init(cpufreq_gov_dbs_init);
  473. module_exit(cpufreq_gov_dbs_exit);