cpufreq_governor.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * drivers/cpufreq/cpufreq_governor.c
  3. *
  4. * CPUFREQ governors common code
  5. *
  6. * Copyright (C) 2001 Russell King
  7. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  8. * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
  9. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  10. * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <asm/cputime.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/export.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/mutex.h>
  23. #include <linux/tick.h>
  24. #include <linux/types.h>
  25. #include <linux/workqueue.h>
  26. #include "cpufreq_governor.h"
  27. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  28. {
  29. u64 idle_time;
  30. u64 cur_wall_time;
  31. u64 busy_time;
  32. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  33. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  34. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  35. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  36. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  37. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  38. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  39. idle_time = cur_wall_time - busy_time;
  40. if (wall)
  41. *wall = cputime_to_usecs(cur_wall_time);
  42. return cputime_to_usecs(idle_time);
  43. }
  44. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall)
  45. {
  46. u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
  47. if (idle_time == -1ULL)
  48. return get_cpu_idle_time_jiffy(cpu, wall);
  49. else
  50. idle_time += get_cpu_iowait_time_us(cpu, wall);
  51. return idle_time;
  52. }
  53. EXPORT_SYMBOL_GPL(get_cpu_idle_time);
  54. void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
  55. {
  56. struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
  57. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  58. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  59. struct cpufreq_policy *policy;
  60. unsigned int max_load = 0;
  61. unsigned int ignore_nice;
  62. unsigned int j;
  63. if (dbs_data->governor == GOV_ONDEMAND)
  64. ignore_nice = od_tuners->ignore_nice;
  65. else
  66. ignore_nice = cs_tuners->ignore_nice;
  67. policy = cdbs->cur_policy;
  68. /* Get Absolute Load (in terms of freq for ondemand gov) */
  69. for_each_cpu(j, policy->cpus) {
  70. struct cpu_dbs_common_info *j_cdbs;
  71. u64 cur_wall_time, cur_idle_time, cur_iowait_time;
  72. unsigned int idle_time, wall_time, iowait_time;
  73. unsigned int load;
  74. j_cdbs = dbs_data->get_cpu_cdbs(j);
  75. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  76. wall_time = (unsigned int)
  77. (cur_wall_time - j_cdbs->prev_cpu_wall);
  78. j_cdbs->prev_cpu_wall = cur_wall_time;
  79. idle_time = (unsigned int)
  80. (cur_idle_time - j_cdbs->prev_cpu_idle);
  81. j_cdbs->prev_cpu_idle = cur_idle_time;
  82. if (ignore_nice) {
  83. u64 cur_nice;
  84. unsigned long cur_nice_jiffies;
  85. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  86. cdbs->prev_cpu_nice;
  87. /*
  88. * Assumption: nice time between sampling periods will
  89. * be less than 2^32 jiffies for 32 bit sys
  90. */
  91. cur_nice_jiffies = (unsigned long)
  92. cputime64_to_jiffies64(cur_nice);
  93. cdbs->prev_cpu_nice =
  94. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  95. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  96. }
  97. if (dbs_data->governor == GOV_ONDEMAND) {
  98. struct od_cpu_dbs_info_s *od_j_dbs_info =
  99. dbs_data->get_cpu_dbs_info_s(cpu);
  100. cur_iowait_time = get_cpu_iowait_time_us(j,
  101. &cur_wall_time);
  102. if (cur_iowait_time == -1ULL)
  103. cur_iowait_time = 0;
  104. iowait_time = (unsigned int) (cur_iowait_time -
  105. od_j_dbs_info->prev_cpu_iowait);
  106. od_j_dbs_info->prev_cpu_iowait = cur_iowait_time;
  107. /*
  108. * For the purpose of ondemand, waiting for disk IO is
  109. * an indication that you're performance critical, and
  110. * not that the system is actually idle. So subtract the
  111. * iowait time from the cpu idle time.
  112. */
  113. if (od_tuners->io_is_busy && idle_time >= iowait_time)
  114. idle_time -= iowait_time;
  115. }
  116. if (unlikely(!wall_time || wall_time < idle_time))
  117. continue;
  118. load = 100 * (wall_time - idle_time) / wall_time;
  119. if (dbs_data->governor == GOV_ONDEMAND) {
  120. int freq_avg = __cpufreq_driver_getavg(policy, j);
  121. if (freq_avg <= 0)
  122. freq_avg = policy->cur;
  123. load *= freq_avg;
  124. }
  125. if (load > max_load)
  126. max_load = load;
  127. }
  128. dbs_data->gov_check_cpu(cpu, max_load);
  129. }
  130. EXPORT_SYMBOL_GPL(dbs_check_cpu);
  131. static inline void dbs_timer_init(struct dbs_data *dbs_data,
  132. struct cpu_dbs_common_info *cdbs, unsigned int sampling_rate)
  133. {
  134. int delay = delay_for_sampling_rate(sampling_rate);
  135. INIT_DEFERRABLE_WORK(&cdbs->work, dbs_data->gov_dbs_timer);
  136. schedule_delayed_work_on(cdbs->cpu, &cdbs->work, delay);
  137. }
  138. static inline void dbs_timer_exit(struct cpu_dbs_common_info *cdbs)
  139. {
  140. cancel_delayed_work_sync(&cdbs->work);
  141. }
  142. int cpufreq_governor_dbs(struct dbs_data *dbs_data,
  143. struct cpufreq_policy *policy, unsigned int event)
  144. {
  145. struct od_cpu_dbs_info_s *od_dbs_info = NULL;
  146. struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
  147. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  148. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  149. struct cpu_dbs_common_info *cpu_cdbs;
  150. unsigned int *sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
  151. int rc;
  152. cpu_cdbs = dbs_data->get_cpu_cdbs(cpu);
  153. if (dbs_data->governor == GOV_CONSERVATIVE) {
  154. cs_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
  155. sampling_rate = &cs_tuners->sampling_rate;
  156. ignore_nice = cs_tuners->ignore_nice;
  157. } else {
  158. od_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
  159. sampling_rate = &od_tuners->sampling_rate;
  160. ignore_nice = od_tuners->ignore_nice;
  161. }
  162. switch (event) {
  163. case CPUFREQ_GOV_START:
  164. if ((!cpu_online(cpu)) || (!policy->cur))
  165. return -EINVAL;
  166. mutex_lock(&dbs_data->mutex);
  167. dbs_data->enable++;
  168. cpu_cdbs->cpu = cpu;
  169. for_each_cpu(j, policy->cpus) {
  170. struct cpu_dbs_common_info *j_cdbs;
  171. j_cdbs = dbs_data->get_cpu_cdbs(j);
  172. j_cdbs->cur_policy = policy;
  173. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
  174. &j_cdbs->prev_cpu_wall);
  175. if (ignore_nice)
  176. j_cdbs->prev_cpu_nice =
  177. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  178. }
  179. /*
  180. * Start the timerschedule work, when this governor is used for
  181. * first time
  182. */
  183. if (dbs_data->enable != 1)
  184. goto second_time;
  185. rc = sysfs_create_group(cpufreq_global_kobject,
  186. dbs_data->attr_group);
  187. if (rc) {
  188. mutex_unlock(&dbs_data->mutex);
  189. return rc;
  190. }
  191. /* policy latency is in nS. Convert it to uS first */
  192. latency = policy->cpuinfo.transition_latency / 1000;
  193. if (latency == 0)
  194. latency = 1;
  195. /*
  196. * conservative does not implement micro like ondemand
  197. * governor, thus we are bound to jiffes/HZ
  198. */
  199. if (dbs_data->governor == GOV_CONSERVATIVE) {
  200. struct cs_ops *ops = dbs_data->gov_ops;
  201. cpufreq_register_notifier(ops->notifier_block,
  202. CPUFREQ_TRANSITION_NOTIFIER);
  203. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  204. jiffies_to_usecs(10);
  205. } else {
  206. struct od_ops *ops = dbs_data->gov_ops;
  207. od_tuners->io_is_busy = ops->io_busy();
  208. }
  209. /* Bring kernel and HW constraints together */
  210. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  211. MIN_LATENCY_MULTIPLIER * latency);
  212. *sampling_rate = max(dbs_data->min_sampling_rate, latency *
  213. LATENCY_MULTIPLIER);
  214. second_time:
  215. if (dbs_data->governor == GOV_CONSERVATIVE) {
  216. cs_dbs_info->down_skip = 0;
  217. cs_dbs_info->enable = 1;
  218. cs_dbs_info->requested_freq = policy->cur;
  219. } else {
  220. struct od_ops *ops = dbs_data->gov_ops;
  221. od_dbs_info->rate_mult = 1;
  222. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  223. ops->powersave_bias_init_cpu(cpu);
  224. }
  225. mutex_unlock(&dbs_data->mutex);
  226. mutex_init(&cpu_cdbs->timer_mutex);
  227. dbs_timer_init(dbs_data, cpu_cdbs, *sampling_rate);
  228. break;
  229. case CPUFREQ_GOV_STOP:
  230. if (dbs_data->governor == GOV_CONSERVATIVE)
  231. cs_dbs_info->enable = 0;
  232. dbs_timer_exit(cpu_cdbs);
  233. mutex_lock(&dbs_data->mutex);
  234. mutex_destroy(&cpu_cdbs->timer_mutex);
  235. dbs_data->enable--;
  236. if (!dbs_data->enable) {
  237. struct cs_ops *ops = dbs_data->gov_ops;
  238. sysfs_remove_group(cpufreq_global_kobject,
  239. dbs_data->attr_group);
  240. if (dbs_data->governor == GOV_CONSERVATIVE)
  241. cpufreq_unregister_notifier(ops->notifier_block,
  242. CPUFREQ_TRANSITION_NOTIFIER);
  243. }
  244. mutex_unlock(&dbs_data->mutex);
  245. break;
  246. case CPUFREQ_GOV_LIMITS:
  247. mutex_lock(&cpu_cdbs->timer_mutex);
  248. if (policy->max < cpu_cdbs->cur_policy->cur)
  249. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  250. policy->max, CPUFREQ_RELATION_H);
  251. else if (policy->min > cpu_cdbs->cur_policy->cur)
  252. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  253. policy->min, CPUFREQ_RELATION_L);
  254. dbs_check_cpu(dbs_data, cpu);
  255. mutex_unlock(&cpu_cdbs->timer_mutex);
  256. break;
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);