cpufreq_governor.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, int cpu,
  132. unsigned int sampling_rate)
  133. {
  134. int delay = delay_for_sampling_rate(sampling_rate);
  135. struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
  136. schedule_delayed_work_on(cpu, &cdbs->work, delay);
  137. }
  138. static inline void dbs_timer_exit(struct dbs_data *dbs_data, int cpu)
  139. {
  140. struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
  141. cancel_delayed_work_sync(&cdbs->work);
  142. }
  143. /* Will return if we need to evaluate cpu load again or not */
  144. bool need_load_eval(struct cpu_dbs_common_info *cdbs,
  145. unsigned int sampling_rate)
  146. {
  147. if (policy_is_shared(cdbs->cur_policy)) {
  148. ktime_t time_now = ktime_get();
  149. s64 delta_us = ktime_us_delta(time_now, cdbs->time_stamp);
  150. /* Do nothing if we recently have sampled */
  151. if (delta_us < (s64)(sampling_rate / 2))
  152. return false;
  153. else
  154. cdbs->time_stamp = time_now;
  155. }
  156. return true;
  157. }
  158. EXPORT_SYMBOL_GPL(need_load_eval);
  159. int cpufreq_governor_dbs(struct dbs_data *dbs_data,
  160. struct cpufreq_policy *policy, unsigned int event)
  161. {
  162. struct od_cpu_dbs_info_s *od_dbs_info = NULL;
  163. struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
  164. struct cs_ops *cs_ops = NULL;
  165. struct od_ops *od_ops = NULL;
  166. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  167. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  168. struct cpu_dbs_common_info *cpu_cdbs;
  169. unsigned int *sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
  170. int rc;
  171. cpu_cdbs = dbs_data->get_cpu_cdbs(cpu);
  172. if (dbs_data->governor == GOV_CONSERVATIVE) {
  173. cs_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
  174. sampling_rate = &cs_tuners->sampling_rate;
  175. ignore_nice = cs_tuners->ignore_nice;
  176. cs_ops = dbs_data->gov_ops;
  177. } else {
  178. od_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
  179. sampling_rate = &od_tuners->sampling_rate;
  180. ignore_nice = od_tuners->ignore_nice;
  181. od_ops = dbs_data->gov_ops;
  182. }
  183. switch (event) {
  184. case CPUFREQ_GOV_START:
  185. if (!policy->cur)
  186. return -EINVAL;
  187. mutex_lock(&dbs_data->mutex);
  188. for_each_cpu(j, policy->cpus) {
  189. struct cpu_dbs_common_info *j_cdbs =
  190. dbs_data->get_cpu_cdbs(j);
  191. j_cdbs->cpu = j;
  192. j_cdbs->cur_policy = policy;
  193. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
  194. &j_cdbs->prev_cpu_wall);
  195. if (ignore_nice)
  196. j_cdbs->prev_cpu_nice =
  197. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  198. mutex_init(&j_cdbs->timer_mutex);
  199. INIT_DEFERRABLE_WORK(&j_cdbs->work,
  200. dbs_data->gov_dbs_timer);
  201. }
  202. if (!policy->governor->initialized) {
  203. rc = sysfs_create_group(cpufreq_global_kobject,
  204. dbs_data->attr_group);
  205. if (rc) {
  206. mutex_unlock(&dbs_data->mutex);
  207. return rc;
  208. }
  209. }
  210. /*
  211. * conservative does not implement micro like ondemand
  212. * governor, thus we are bound to jiffes/HZ
  213. */
  214. if (dbs_data->governor == GOV_CONSERVATIVE) {
  215. cs_dbs_info->down_skip = 0;
  216. cs_dbs_info->enable = 1;
  217. cs_dbs_info->requested_freq = policy->cur;
  218. if (!policy->governor->initialized) {
  219. cpufreq_register_notifier(cs_ops->notifier_block,
  220. CPUFREQ_TRANSITION_NOTIFIER);
  221. dbs_data->min_sampling_rate =
  222. MIN_SAMPLING_RATE_RATIO *
  223. jiffies_to_usecs(10);
  224. }
  225. } else {
  226. od_dbs_info->rate_mult = 1;
  227. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  228. od_ops->powersave_bias_init_cpu(cpu);
  229. if (!policy->governor->initialized)
  230. od_tuners->io_is_busy = od_ops->io_busy();
  231. }
  232. if (policy->governor->initialized)
  233. goto unlock;
  234. /* policy latency is in nS. Convert it to uS first */
  235. latency = policy->cpuinfo.transition_latency / 1000;
  236. if (latency == 0)
  237. latency = 1;
  238. /* Bring kernel and HW constraints together */
  239. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  240. MIN_LATENCY_MULTIPLIER * latency);
  241. *sampling_rate = max(dbs_data->min_sampling_rate, latency *
  242. LATENCY_MULTIPLIER);
  243. unlock:
  244. mutex_unlock(&dbs_data->mutex);
  245. /* Initiate timer time stamp */
  246. cpu_cdbs->time_stamp = ktime_get();
  247. for_each_cpu(j, policy->cpus)
  248. dbs_timer_init(dbs_data, j, *sampling_rate);
  249. break;
  250. case CPUFREQ_GOV_STOP:
  251. if (dbs_data->governor == GOV_CONSERVATIVE)
  252. cs_dbs_info->enable = 0;
  253. for_each_cpu(j, policy->cpus)
  254. dbs_timer_exit(dbs_data, j);
  255. mutex_lock(&dbs_data->mutex);
  256. mutex_destroy(&cpu_cdbs->timer_mutex);
  257. if (policy->governor->initialized == 1) {
  258. sysfs_remove_group(cpufreq_global_kobject,
  259. dbs_data->attr_group);
  260. if (dbs_data->governor == GOV_CONSERVATIVE)
  261. cpufreq_unregister_notifier(cs_ops->notifier_block,
  262. CPUFREQ_TRANSITION_NOTIFIER);
  263. }
  264. mutex_unlock(&dbs_data->mutex);
  265. break;
  266. case CPUFREQ_GOV_LIMITS:
  267. mutex_lock(&cpu_cdbs->timer_mutex);
  268. if (policy->max < cpu_cdbs->cur_policy->cur)
  269. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  270. policy->max, CPUFREQ_RELATION_H);
  271. else if (policy->min > cpu_cdbs->cur_policy->cur)
  272. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  273. policy->min, CPUFREQ_RELATION_L);
  274. dbs_check_cpu(dbs_data, cpu);
  275. mutex_unlock(&cpu_cdbs->timer_mutex);
  276. break;
  277. }
  278. return 0;
  279. }
  280. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);