cpufreq_ondemand.c 14 KB

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