cpufreq_ondemand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. /************************** sysfs interface ************************/
  80. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  81. {
  82. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  83. }
  84. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  85. {
  86. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  87. }
  88. #define define_one_ro(_name) \
  89. static struct freq_attr _name = \
  90. __ATTR(_name, 0444, show_##_name, NULL)
  91. define_one_ro(sampling_rate_max);
  92. define_one_ro(sampling_rate_min);
  93. /* cpufreq_ondemand Governor Tunables */
  94. #define show_one(file_name, object) \
  95. static ssize_t show_##file_name \
  96. (struct cpufreq_policy *unused, char *buf) \
  97. { \
  98. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  99. }
  100. show_one(sampling_rate, sampling_rate);
  101. show_one(sampling_down_factor, sampling_down_factor);
  102. show_one(up_threshold, up_threshold);
  103. show_one(down_threshold, down_threshold);
  104. show_one(ignore_nice, ignore_nice);
  105. show_one(freq_step, freq_step);
  106. static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
  107. const char *buf, size_t count)
  108. {
  109. unsigned int input;
  110. int ret;
  111. ret = sscanf (buf, "%u", &input);
  112. if (ret != 1 )
  113. return -EINVAL;
  114. down(&dbs_sem);
  115. dbs_tuners_ins.sampling_down_factor = input;
  116. up(&dbs_sem);
  117. return count;
  118. }
  119. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  120. const char *buf, size_t count)
  121. {
  122. unsigned int input;
  123. int ret;
  124. ret = sscanf (buf, "%u", &input);
  125. down(&dbs_sem);
  126. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  127. up(&dbs_sem);
  128. return -EINVAL;
  129. }
  130. dbs_tuners_ins.sampling_rate = input;
  131. up(&dbs_sem);
  132. return count;
  133. }
  134. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  135. const char *buf, size_t count)
  136. {
  137. unsigned int input;
  138. int ret;
  139. ret = sscanf (buf, "%u", &input);
  140. down(&dbs_sem);
  141. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  142. input < MIN_FREQUENCY_UP_THRESHOLD ||
  143. input <= dbs_tuners_ins.down_threshold) {
  144. up(&dbs_sem);
  145. return -EINVAL;
  146. }
  147. dbs_tuners_ins.up_threshold = input;
  148. up(&dbs_sem);
  149. return count;
  150. }
  151. static ssize_t store_down_threshold(struct cpufreq_policy *unused,
  152. const char *buf, size_t count)
  153. {
  154. unsigned int input;
  155. int ret;
  156. ret = sscanf (buf, "%u", &input);
  157. down(&dbs_sem);
  158. if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
  159. input < MIN_FREQUENCY_DOWN_THRESHOLD ||
  160. input >= dbs_tuners_ins.up_threshold) {
  161. up(&dbs_sem);
  162. return -EINVAL;
  163. }
  164. dbs_tuners_ins.down_threshold = input;
  165. up(&dbs_sem);
  166. return count;
  167. }
  168. static ssize_t store_ignore_nice(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. down(&dbs_sem);
  180. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  181. up(&dbs_sem);
  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_cpu_mask(j, policy->cpus) {
  187. struct cpu_dbs_info_s *j_dbs_info;
  188. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  189. j_dbs_info->cur_policy = policy;
  190. j_dbs_info->prev_cpu_idle_up =
  191. kstat_cpu(j).cpustat.idle +
  192. kstat_cpu(j).cpustat.iowait +
  193. ( !dbs_tuners_ins.ignore_nice
  194. ? kstat_cpu(j).cpustat.nice : 0 );
  195. j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
  196. }
  197. up(&dbs_sem);
  198. return count;
  199. }
  200. static ssize_t store_freq_step(struct cpufreq_policy *policy,
  201. const char *buf, size_t count)
  202. {
  203. unsigned int input;
  204. int ret;
  205. ret = sscanf (buf, "%u", &input);
  206. if ( ret != 1 )
  207. return -EINVAL;
  208. if ( input > 100 )
  209. input = 100;
  210. /* no need to test here if freq_step is zero as the user might actually
  211. * want this, they would be crazy though :) */
  212. down(&dbs_sem);
  213. dbs_tuners_ins.freq_step = input;
  214. up(&dbs_sem);
  215. return count;
  216. }
  217. #define define_one_rw(_name) \
  218. static struct freq_attr _name = \
  219. __ATTR(_name, 0644, show_##_name, store_##_name)
  220. define_one_rw(sampling_rate);
  221. define_one_rw(sampling_down_factor);
  222. define_one_rw(up_threshold);
  223. define_one_rw(down_threshold);
  224. define_one_rw(ignore_nice);
  225. define_one_rw(freq_step);
  226. static struct attribute * dbs_attributes[] = {
  227. &sampling_rate_max.attr,
  228. &sampling_rate_min.attr,
  229. &sampling_rate.attr,
  230. &sampling_down_factor.attr,
  231. &up_threshold.attr,
  232. &down_threshold.attr,
  233. &ignore_nice.attr,
  234. &freq_step.attr,
  235. NULL
  236. };
  237. static struct attribute_group dbs_attr_group = {
  238. .attrs = dbs_attributes,
  239. .name = "ondemand",
  240. };
  241. /************************** sysfs end ************************/
  242. static void dbs_check_cpu(int cpu)
  243. {
  244. unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
  245. unsigned int total_idle_ticks;
  246. unsigned int freq_down_step;
  247. unsigned int freq_down_sampling_rate;
  248. static int down_skip[NR_CPUS];
  249. struct cpu_dbs_info_s *this_dbs_info;
  250. struct cpufreq_policy *policy;
  251. unsigned int j;
  252. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  253. if (!this_dbs_info->enable)
  254. return;
  255. policy = this_dbs_info->cur_policy;
  256. /*
  257. * The default safe range is 20% to 80%
  258. * Every sampling_rate, we check
  259. * - If current idle time is less than 20%, then we try to
  260. * increase frequency
  261. * Every sampling_rate*sampling_down_factor, we check
  262. * - If current idle time is more than 80%, then we try to
  263. * decrease frequency
  264. *
  265. * Any frequency increase takes it to the maximum frequency.
  266. * Frequency reduction happens at minimum steps of
  267. * 5% (default) of max_frequency
  268. */
  269. /* Check for frequency increase */
  270. total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
  271. kstat_cpu(cpu).cpustat.iowait;
  272. /* consider 'nice' tasks as 'idle' time too if required */
  273. if (dbs_tuners_ins.ignore_nice == 0)
  274. total_idle_ticks += kstat_cpu(cpu).cpustat.nice;
  275. idle_ticks = total_idle_ticks -
  276. this_dbs_info->prev_cpu_idle_up;
  277. this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  278. for_each_cpu_mask(j, policy->cpus) {
  279. unsigned int tmp_idle_ticks;
  280. struct cpu_dbs_info_s *j_dbs_info;
  281. if (j == cpu)
  282. continue;
  283. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  284. /* Check for frequency increase */
  285. total_idle_ticks = kstat_cpu(j).cpustat.idle +
  286. kstat_cpu(j).cpustat.iowait;
  287. /* consider 'nice' too? */
  288. if (dbs_tuners_ins.ignore_nice == 0)
  289. total_idle_ticks += kstat_cpu(j).cpustat.nice;
  290. tmp_idle_ticks = total_idle_ticks -
  291. j_dbs_info->prev_cpu_idle_up;
  292. j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
  293. if (tmp_idle_ticks < idle_ticks)
  294. idle_ticks = tmp_idle_ticks;
  295. }
  296. /* Scale idle ticks by 100 and compare with up and down ticks */
  297. idle_ticks *= 100;
  298. up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
  299. usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  300. if (idle_ticks < up_idle_ticks) {
  301. /* if we are already at full speed then break out early */
  302. if (policy->cur == policy->max)
  303. return;
  304. __cpufreq_driver_target(policy, policy->max,
  305. CPUFREQ_RELATION_H);
  306. down_skip[cpu] = 0;
  307. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  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 = kstat_cpu(cpu).cpustat.idle +
  315. kstat_cpu(cpu).cpustat.iowait;
  316. /* consider 'nice' too? */
  317. if (dbs_tuners_ins.ignore_nice == 0)
  318. total_idle_ticks += kstat_cpu(cpu).cpustat.nice;
  319. idle_ticks = total_idle_ticks -
  320. this_dbs_info->prev_cpu_idle_down;
  321. this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  322. for_each_cpu_mask(j, policy->cpus) {
  323. unsigned int tmp_idle_ticks;
  324. struct cpu_dbs_info_s *j_dbs_info;
  325. if (j == cpu)
  326. continue;
  327. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  328. /* Check for frequency increase */
  329. total_idle_ticks = kstat_cpu(j).cpustat.idle +
  330. kstat_cpu(j).cpustat.iowait;
  331. /* consider 'nice' too? */
  332. if (dbs_tuners_ins.ignore_nice == 0)
  333. total_idle_ticks += kstat_cpu(j).cpustat.nice;
  334. tmp_idle_ticks = total_idle_ticks -
  335. j_dbs_info->prev_cpu_idle_down;
  336. j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
  337. if (tmp_idle_ticks < idle_ticks)
  338. idle_ticks = tmp_idle_ticks;
  339. }
  340. /* Scale idle ticks by 100 and compare with up and down ticks */
  341. idle_ticks *= 100;
  342. down_skip[cpu] = 0;
  343. freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
  344. dbs_tuners_ins.sampling_down_factor;
  345. down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
  346. usecs_to_jiffies(freq_down_sampling_rate);
  347. if (idle_ticks > down_idle_ticks ) {
  348. /* if we are already at the lowest speed then break out early
  349. * or if we 'cannot' reduce the speed as the user might want
  350. * freq_step to be zero */
  351. if (policy->cur == policy->min || dbs_tuners_ins.freq_step == 0)
  352. return;
  353. freq_down_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
  354. /* max freq cannot be less than 100. But who knows.... */
  355. if (unlikely(freq_down_step == 0))
  356. freq_down_step = 5;
  357. __cpufreq_driver_target(policy,
  358. policy->cur - freq_down_step,
  359. CPUFREQ_RELATION_H);
  360. return;
  361. }
  362. }
  363. static void do_dbs_timer(void *data)
  364. {
  365. int i;
  366. down(&dbs_sem);
  367. for_each_online_cpu(i)
  368. dbs_check_cpu(i);
  369. schedule_delayed_work(&dbs_work,
  370. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  371. up(&dbs_sem);
  372. }
  373. static inline void dbs_timer_init(void)
  374. {
  375. INIT_WORK(&dbs_work, do_dbs_timer, NULL);
  376. schedule_delayed_work(&dbs_work,
  377. usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
  378. return;
  379. }
  380. static inline void dbs_timer_exit(void)
  381. {
  382. cancel_delayed_work(&dbs_work);
  383. return;
  384. }
  385. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  386. unsigned int event)
  387. {
  388. unsigned int cpu = policy->cpu;
  389. struct cpu_dbs_info_s *this_dbs_info;
  390. unsigned int j;
  391. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  392. switch (event) {
  393. case CPUFREQ_GOV_START:
  394. if ((!cpu_online(cpu)) ||
  395. (!policy->cur))
  396. return -EINVAL;
  397. if (policy->cpuinfo.transition_latency >
  398. (TRANSITION_LATENCY_LIMIT * 1000))
  399. return -EINVAL;
  400. if (this_dbs_info->enable) /* Already enabled */
  401. break;
  402. down(&dbs_sem);
  403. for_each_cpu_mask(j, policy->cpus) {
  404. struct cpu_dbs_info_s *j_dbs_info;
  405. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  406. j_dbs_info->cur_policy = policy;
  407. j_dbs_info->prev_cpu_idle_up =
  408. kstat_cpu(j).cpustat.idle +
  409. kstat_cpu(j).cpustat.iowait +
  410. ( !dbs_tuners_ins.ignore_nice
  411. ? kstat_cpu(j).cpustat.nice : 0 );
  412. j_dbs_info->prev_cpu_idle_down
  413. = j_dbs_info->prev_cpu_idle_up;
  414. }
  415. this_dbs_info->enable = 1;
  416. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  417. dbs_enable++;
  418. /*
  419. * Start the timerschedule work, when this governor
  420. * is used for first time
  421. */
  422. if (dbs_enable == 1) {
  423. unsigned int latency;
  424. /* policy latency is in nS. Convert it to uS first */
  425. latency = policy->cpuinfo.transition_latency;
  426. if (latency < 1000)
  427. latency = 1000;
  428. def_sampling_rate = (latency / 1000) *
  429. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  430. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  431. dbs_tuners_ins.ignore_nice = 0;
  432. dbs_tuners_ins.freq_step = 5;
  433. dbs_timer_init();
  434. }
  435. up(&dbs_sem);
  436. break;
  437. case CPUFREQ_GOV_STOP:
  438. down(&dbs_sem);
  439. this_dbs_info->enable = 0;
  440. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  441. dbs_enable--;
  442. /*
  443. * Stop the timerschedule work, when this governor
  444. * is used for first time
  445. */
  446. if (dbs_enable == 0)
  447. dbs_timer_exit();
  448. up(&dbs_sem);
  449. break;
  450. case CPUFREQ_GOV_LIMITS:
  451. down(&dbs_sem);
  452. if (policy->max < this_dbs_info->cur_policy->cur)
  453. __cpufreq_driver_target(
  454. this_dbs_info->cur_policy,
  455. policy->max, CPUFREQ_RELATION_H);
  456. else if (policy->min > this_dbs_info->cur_policy->cur)
  457. __cpufreq_driver_target(
  458. this_dbs_info->cur_policy,
  459. policy->min, CPUFREQ_RELATION_L);
  460. up(&dbs_sem);
  461. break;
  462. }
  463. return 0;
  464. }
  465. static struct cpufreq_governor cpufreq_gov_dbs = {
  466. .name = "ondemand",
  467. .governor = cpufreq_governor_dbs,
  468. .owner = THIS_MODULE,
  469. };
  470. static int __init cpufreq_gov_dbs_init(void)
  471. {
  472. return cpufreq_register_governor(&cpufreq_gov_dbs);
  473. }
  474. static void __exit cpufreq_gov_dbs_exit(void)
  475. {
  476. /* Make sure that the scheduled work is indeed not running */
  477. flush_scheduled_work();
  478. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  479. }
  480. MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  481. MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  482. "Low Latency Frequency Transition capable processors");
  483. MODULE_LICENSE ("GPL");
  484. module_init(cpufreq_gov_dbs_init);
  485. module_exit(cpufreq_gov_dbs_exit);