cpufreq_ondemand.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/cpu.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mutex.h>
  20. /*
  21. * dbs is used in this file as a shortform for demandbased switching
  22. * It helps to keep variable names smaller, simpler
  23. */
  24. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  25. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  26. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  27. /*
  28. * The polling frequency of this governor depends on the capability of
  29. * the processor. Default polling frequency is 1000 times the transition
  30. * latency of the processor. The governor will work on any processor with
  31. * transition latency <= 10mS, using appropriate sampling
  32. * rate.
  33. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  34. * this governor will not work.
  35. * All times here are in uS.
  36. */
  37. static unsigned int def_sampling_rate;
  38. #define MIN_SAMPLING_RATE_RATIO (2)
  39. /* for correct statistics, we need at least 10 ticks between each measure */
  40. #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
  41. #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
  42. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  43. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  44. #define TRANSITION_LATENCY_LIMIT (10 * 1000)
  45. static void do_dbs_timer(void *data);
  46. struct cpu_dbs_info_s {
  47. cputime64_t prev_cpu_idle;
  48. cputime64_t prev_cpu_wall;
  49. struct cpufreq_policy *cur_policy;
  50. struct work_struct work;
  51. unsigned int enable;
  52. struct cpufreq_frequency_table *freq_table;
  53. unsigned int freq_lo;
  54. unsigned int freq_lo_jiffies;
  55. unsigned int freq_hi_jiffies;
  56. };
  57. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  58. static unsigned int dbs_enable; /* number of CPUs using this policy */
  59. /*
  60. * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
  61. * lock and dbs_mutex. cpu_hotplug lock should always be held before
  62. * dbs_mutex. If any function that can potentially take cpu_hotplug lock
  63. * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
  64. * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
  65. * is recursive for the same process. -Venki
  66. */
  67. static DEFINE_MUTEX(dbs_mutex);
  68. static struct workqueue_struct *kondemand_wq;
  69. static struct dbs_tuners {
  70. unsigned int sampling_rate;
  71. unsigned int up_threshold;
  72. unsigned int ignore_nice;
  73. unsigned int powersave_bias;
  74. } dbs_tuners_ins = {
  75. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  76. .ignore_nice = 0,
  77. .powersave_bias = 0,
  78. };
  79. static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
  80. {
  81. cputime64_t retval;
  82. retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
  83. kstat_cpu(cpu).cpustat.iowait);
  84. if (dbs_tuners_ins.ignore_nice)
  85. retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
  86. return retval;
  87. }
  88. /*
  89. * Find right freq to be set now with powersave_bias on.
  90. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  91. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  92. */
  93. static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
  94. unsigned int freq_next,
  95. unsigned int relation)
  96. {
  97. unsigned int freq_req, freq_reduc, freq_avg;
  98. unsigned int freq_hi, freq_lo;
  99. unsigned int index = 0;
  100. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  101. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
  102. if (!dbs_info->freq_table) {
  103. dbs_info->freq_lo = 0;
  104. dbs_info->freq_lo_jiffies = 0;
  105. return freq_next;
  106. }
  107. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  108. relation, &index);
  109. freq_req = dbs_info->freq_table[index].frequency;
  110. freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
  111. freq_avg = freq_req - freq_reduc;
  112. /* Find freq bounds for freq_avg in freq_table */
  113. index = 0;
  114. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  115. CPUFREQ_RELATION_H, &index);
  116. freq_lo = dbs_info->freq_table[index].frequency;
  117. index = 0;
  118. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  119. CPUFREQ_RELATION_L, &index);
  120. freq_hi = dbs_info->freq_table[index].frequency;
  121. /* Find out how long we have to be in hi and lo freqs */
  122. if (freq_hi == freq_lo) {
  123. dbs_info->freq_lo = 0;
  124. dbs_info->freq_lo_jiffies = 0;
  125. return freq_lo;
  126. }
  127. jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  128. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  129. jiffies_hi += ((freq_hi - freq_lo) / 2);
  130. jiffies_hi /= (freq_hi - freq_lo);
  131. jiffies_lo = jiffies_total - jiffies_hi;
  132. dbs_info->freq_lo = freq_lo;
  133. dbs_info->freq_lo_jiffies = jiffies_lo;
  134. dbs_info->freq_hi_jiffies = jiffies_hi;
  135. return freq_hi;
  136. }
  137. static void ondemand_powersave_bias_init(void)
  138. {
  139. int i;
  140. for_each_online_cpu(i) {
  141. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
  142. dbs_info->freq_table = cpufreq_frequency_get_table(i);
  143. dbs_info->freq_lo = 0;
  144. }
  145. }
  146. /************************** sysfs interface ************************/
  147. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  148. {
  149. return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
  150. }
  151. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  152. {
  153. return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
  154. }
  155. #define define_one_ro(_name) \
  156. static struct freq_attr _name = \
  157. __ATTR(_name, 0444, show_##_name, NULL)
  158. define_one_ro(sampling_rate_max);
  159. define_one_ro(sampling_rate_min);
  160. /* cpufreq_ondemand Governor Tunables */
  161. #define show_one(file_name, object) \
  162. static ssize_t show_##file_name \
  163. (struct cpufreq_policy *unused, char *buf) \
  164. { \
  165. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  166. }
  167. show_one(sampling_rate, sampling_rate);
  168. show_one(up_threshold, up_threshold);
  169. show_one(ignore_nice_load, ignore_nice);
  170. show_one(powersave_bias, powersave_bias);
  171. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  172. const char *buf, size_t count)
  173. {
  174. unsigned int input;
  175. int ret;
  176. ret = sscanf(buf, "%u", &input);
  177. mutex_lock(&dbs_mutex);
  178. if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
  179. mutex_unlock(&dbs_mutex);
  180. return -EINVAL;
  181. }
  182. dbs_tuners_ins.sampling_rate = input;
  183. mutex_unlock(&dbs_mutex);
  184. return count;
  185. }
  186. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  187. const char *buf, size_t count)
  188. {
  189. unsigned int input;
  190. int ret;
  191. ret = sscanf(buf, "%u", &input);
  192. mutex_lock(&dbs_mutex);
  193. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  194. input < MIN_FREQUENCY_UP_THRESHOLD) {
  195. mutex_unlock(&dbs_mutex);
  196. return -EINVAL;
  197. }
  198. dbs_tuners_ins.up_threshold = input;
  199. mutex_unlock(&dbs_mutex);
  200. return count;
  201. }
  202. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  203. const char *buf, size_t count)
  204. {
  205. unsigned int input;
  206. int ret;
  207. unsigned int j;
  208. ret = sscanf(buf, "%u", &input);
  209. if ( ret != 1 )
  210. return -EINVAL;
  211. if ( input > 1 )
  212. input = 1;
  213. mutex_lock(&dbs_mutex);
  214. if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
  215. mutex_unlock(&dbs_mutex);
  216. return count;
  217. }
  218. dbs_tuners_ins.ignore_nice = input;
  219. /* we need to re-evaluate prev_cpu_idle */
  220. for_each_online_cpu(j) {
  221. struct cpu_dbs_info_s *dbs_info;
  222. dbs_info = &per_cpu(cpu_dbs_info, j);
  223. dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  224. dbs_info->prev_cpu_wall = get_jiffies_64();
  225. }
  226. mutex_unlock(&dbs_mutex);
  227. return count;
  228. }
  229. static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
  230. const char *buf, size_t count)
  231. {
  232. unsigned int input;
  233. int ret;
  234. ret = sscanf(buf, "%u", &input);
  235. if (ret != 1)
  236. return -EINVAL;
  237. if (input > 1000)
  238. input = 1000;
  239. mutex_lock(&dbs_mutex);
  240. dbs_tuners_ins.powersave_bias = input;
  241. ondemand_powersave_bias_init();
  242. mutex_unlock(&dbs_mutex);
  243. return count;
  244. }
  245. #define define_one_rw(_name) \
  246. static struct freq_attr _name = \
  247. __ATTR(_name, 0644, show_##_name, store_##_name)
  248. define_one_rw(sampling_rate);
  249. define_one_rw(up_threshold);
  250. define_one_rw(ignore_nice_load);
  251. define_one_rw(powersave_bias);
  252. static struct attribute * dbs_attributes[] = {
  253. &sampling_rate_max.attr,
  254. &sampling_rate_min.attr,
  255. &sampling_rate.attr,
  256. &up_threshold.attr,
  257. &ignore_nice_load.attr,
  258. &powersave_bias.attr,
  259. NULL
  260. };
  261. static struct attribute_group dbs_attr_group = {
  262. .attrs = dbs_attributes,
  263. .name = "ondemand",
  264. };
  265. /************************** sysfs end ************************/
  266. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  267. {
  268. unsigned int idle_ticks, total_ticks;
  269. unsigned int load;
  270. cputime64_t cur_jiffies;
  271. struct cpufreq_policy *policy;
  272. unsigned int j;
  273. if (!this_dbs_info->enable)
  274. return;
  275. this_dbs_info->freq_lo = 0;
  276. policy = this_dbs_info->cur_policy;
  277. cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
  278. total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
  279. this_dbs_info->prev_cpu_wall);
  280. this_dbs_info->prev_cpu_wall = cur_jiffies;
  281. if (!total_ticks)
  282. return;
  283. /*
  284. * Every sampling_rate, we check, if current idle time is less
  285. * than 20% (default), then we try to increase frequency
  286. * Every sampling_rate, we look for a the lowest
  287. * frequency which can sustain the load while keeping idle time over
  288. * 30%. If such a frequency exist, we try to decrease to this frequency.
  289. *
  290. * Any frequency increase takes it to the maximum frequency.
  291. * Frequency reduction happens at minimum steps of
  292. * 5% (default) of current frequency
  293. */
  294. /* Get Idle Time */
  295. idle_ticks = UINT_MAX;
  296. for_each_cpu_mask(j, policy->cpus) {
  297. cputime64_t total_idle_ticks;
  298. unsigned int tmp_idle_ticks;
  299. struct cpu_dbs_info_s *j_dbs_info;
  300. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  301. total_idle_ticks = get_cpu_idle_time(j);
  302. tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
  303. j_dbs_info->prev_cpu_idle);
  304. j_dbs_info->prev_cpu_idle = total_idle_ticks;
  305. if (tmp_idle_ticks < idle_ticks)
  306. idle_ticks = tmp_idle_ticks;
  307. }
  308. load = (100 * (total_ticks - idle_ticks)) / total_ticks;
  309. /* Check for frequency increase */
  310. if (load > dbs_tuners_ins.up_threshold) {
  311. /* if we are already at full speed then break out early */
  312. if (!dbs_tuners_ins.powersave_bias) {
  313. if (policy->cur == policy->max)
  314. return;
  315. __cpufreq_driver_target(policy, policy->max,
  316. CPUFREQ_RELATION_H);
  317. } else {
  318. int freq = powersave_bias_target(policy, policy->max,
  319. CPUFREQ_RELATION_H);
  320. __cpufreq_driver_target(policy, freq,
  321. CPUFREQ_RELATION_L);
  322. }
  323. return;
  324. }
  325. /* Check for frequency decrease */
  326. /* if we cannot reduce the frequency anymore, break out early */
  327. if (policy->cur == policy->min)
  328. return;
  329. /*
  330. * The optimal frequency is the frequency that is the lowest that
  331. * can support the current CPU usage without triggering the up
  332. * policy. To be safe, we focus 10 points under the threshold.
  333. */
  334. if (load < (dbs_tuners_ins.up_threshold - 10)) {
  335. unsigned int freq_next = (policy->cur * load) /
  336. (dbs_tuners_ins.up_threshold - 10);
  337. if (!dbs_tuners_ins.powersave_bias) {
  338. __cpufreq_driver_target(policy, freq_next,
  339. CPUFREQ_RELATION_L);
  340. } else {
  341. int freq = powersave_bias_target(policy, freq_next,
  342. CPUFREQ_RELATION_L);
  343. __cpufreq_driver_target(policy, freq,
  344. CPUFREQ_RELATION_L);
  345. }
  346. }
  347. }
  348. /* Sampling types */
  349. enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
  350. static void do_dbs_timer(void *data)
  351. {
  352. unsigned int cpu = smp_processor_id();
  353. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  354. /* We want all CPUs to do sampling nearly on same jiffy */
  355. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  356. delay -= jiffies % delay;
  357. if (!dbs_info->enable)
  358. return;
  359. /* Common NORMAL_SAMPLE setup */
  360. INIT_WORK(&dbs_info->work, do_dbs_timer, (void *)DBS_NORMAL_SAMPLE);
  361. if (!dbs_tuners_ins.powersave_bias ||
  362. (unsigned long) data == DBS_NORMAL_SAMPLE) {
  363. lock_cpu_hotplug();
  364. dbs_check_cpu(dbs_info);
  365. unlock_cpu_hotplug();
  366. if (dbs_info->freq_lo) {
  367. /* Setup timer for SUB_SAMPLE */
  368. INIT_WORK(&dbs_info->work, do_dbs_timer,
  369. (void *)DBS_SUB_SAMPLE);
  370. delay = dbs_info->freq_hi_jiffies;
  371. }
  372. } else {
  373. __cpufreq_driver_target(dbs_info->cur_policy,
  374. dbs_info->freq_lo,
  375. CPUFREQ_RELATION_H);
  376. }
  377. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
  378. }
  379. static inline void dbs_timer_init(unsigned int cpu)
  380. {
  381. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
  382. /* We want all CPUs to do sampling nearly on same jiffy */
  383. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  384. delay -= jiffies % delay;
  385. ondemand_powersave_bias_init();
  386. INIT_WORK(&dbs_info->work, do_dbs_timer, NULL);
  387. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
  388. }
  389. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  390. {
  391. dbs_info->enable = 0;
  392. cancel_delayed_work(&dbs_info->work);
  393. flush_workqueue(kondemand_wq);
  394. }
  395. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  396. unsigned int event)
  397. {
  398. unsigned int cpu = policy->cpu;
  399. struct cpu_dbs_info_s *this_dbs_info;
  400. unsigned int j;
  401. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  402. switch (event) {
  403. case CPUFREQ_GOV_START:
  404. if ((!cpu_online(cpu)) || (!policy->cur))
  405. return -EINVAL;
  406. if (policy->cpuinfo.transition_latency >
  407. (TRANSITION_LATENCY_LIMIT * 1000)) {
  408. printk(KERN_WARNING "ondemand governor failed to load "
  409. "due to too long transition latency\n");
  410. return -EINVAL;
  411. }
  412. if (this_dbs_info->enable) /* Already enabled */
  413. break;
  414. mutex_lock(&dbs_mutex);
  415. dbs_enable++;
  416. if (dbs_enable == 1) {
  417. kondemand_wq = create_workqueue("kondemand");
  418. if (!kondemand_wq) {
  419. printk(KERN_ERR "Creation of kondemand failed\n");
  420. dbs_enable--;
  421. mutex_unlock(&dbs_mutex);
  422. return -ENOSPC;
  423. }
  424. }
  425. for_each_cpu_mask(j, policy->cpus) {
  426. struct cpu_dbs_info_s *j_dbs_info;
  427. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  428. j_dbs_info->cur_policy = policy;
  429. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
  430. j_dbs_info->prev_cpu_wall = get_jiffies_64();
  431. }
  432. this_dbs_info->enable = 1;
  433. sysfs_create_group(&policy->kobj, &dbs_attr_group);
  434. /*
  435. * Start the timerschedule work, when this governor
  436. * is used for first time
  437. */
  438. if (dbs_enable == 1) {
  439. unsigned int latency;
  440. /* policy latency is in nS. Convert it to uS first */
  441. latency = policy->cpuinfo.transition_latency / 1000;
  442. if (latency == 0)
  443. latency = 1;
  444. def_sampling_rate = latency *
  445. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  446. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  447. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  448. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  449. }
  450. dbs_timer_init(policy->cpu);
  451. mutex_unlock(&dbs_mutex);
  452. break;
  453. case CPUFREQ_GOV_STOP:
  454. mutex_lock(&dbs_mutex);
  455. dbs_timer_exit(this_dbs_info);
  456. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  457. dbs_enable--;
  458. if (dbs_enable == 0)
  459. destroy_workqueue(kondemand_wq);
  460. mutex_unlock(&dbs_mutex);
  461. break;
  462. case CPUFREQ_GOV_LIMITS:
  463. mutex_lock(&dbs_mutex);
  464. if (policy->max < this_dbs_info->cur_policy->cur)
  465. __cpufreq_driver_target(this_dbs_info->cur_policy,
  466. policy->max,
  467. CPUFREQ_RELATION_H);
  468. else if (policy->min > this_dbs_info->cur_policy->cur)
  469. __cpufreq_driver_target(this_dbs_info->cur_policy,
  470. policy->min,
  471. CPUFREQ_RELATION_L);
  472. mutex_unlock(&dbs_mutex);
  473. break;
  474. }
  475. return 0;
  476. }
  477. static struct cpufreq_governor cpufreq_gov_dbs = {
  478. .name = "ondemand",
  479. .governor = cpufreq_governor_dbs,
  480. .owner = THIS_MODULE,
  481. };
  482. static int __init cpufreq_gov_dbs_init(void)
  483. {
  484. return cpufreq_register_governor(&cpufreq_gov_dbs);
  485. }
  486. static void __exit cpufreq_gov_dbs_exit(void)
  487. {
  488. cpufreq_unregister_governor(&cpufreq_gov_dbs);
  489. }
  490. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  491. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  492. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  493. "Low Latency Frequency Transition capable processors");
  494. MODULE_LICENSE("GPL");
  495. module_init(cpufreq_gov_dbs_init);
  496. module_exit(cpufreq_gov_dbs_exit);