cpufreq_governor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <linux/export.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/slab.h>
  20. #include "cpufreq_governor.h"
  21. static struct attribute_group *get_sysfs_attr(struct dbs_data *dbs_data)
  22. {
  23. if (have_governor_per_policy())
  24. return dbs_data->cdata->attr_group_gov_pol;
  25. else
  26. return dbs_data->cdata->attr_group_gov_sys;
  27. }
  28. void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
  29. {
  30. struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  31. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  32. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  33. struct cpufreq_policy *policy;
  34. unsigned int max_load = 0;
  35. unsigned int ignore_nice;
  36. unsigned int j;
  37. if (dbs_data->cdata->governor == GOV_ONDEMAND)
  38. ignore_nice = od_tuners->ignore_nice_load;
  39. else
  40. ignore_nice = cs_tuners->ignore_nice_load;
  41. policy = cdbs->cur_policy;
  42. /* Get Absolute Load */
  43. for_each_cpu(j, policy->cpus) {
  44. struct cpu_dbs_common_info *j_cdbs;
  45. u64 cur_wall_time, cur_idle_time;
  46. unsigned int idle_time, wall_time;
  47. unsigned int load;
  48. int io_busy = 0;
  49. j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
  50. /*
  51. * For the purpose of ondemand, waiting for disk IO is
  52. * an indication that you're performance critical, and
  53. * not that the system is actually idle. So do not add
  54. * the iowait time to the cpu idle time.
  55. */
  56. if (dbs_data->cdata->governor == GOV_ONDEMAND)
  57. io_busy = od_tuners->io_is_busy;
  58. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
  59. wall_time = (unsigned int)
  60. (cur_wall_time - j_cdbs->prev_cpu_wall);
  61. j_cdbs->prev_cpu_wall = cur_wall_time;
  62. idle_time = (unsigned int)
  63. (cur_idle_time - j_cdbs->prev_cpu_idle);
  64. j_cdbs->prev_cpu_idle = cur_idle_time;
  65. if (ignore_nice) {
  66. u64 cur_nice;
  67. unsigned long cur_nice_jiffies;
  68. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  69. cdbs->prev_cpu_nice;
  70. /*
  71. * Assumption: nice time between sampling periods will
  72. * be less than 2^32 jiffies for 32 bit sys
  73. */
  74. cur_nice_jiffies = (unsigned long)
  75. cputime64_to_jiffies64(cur_nice);
  76. cdbs->prev_cpu_nice =
  77. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  78. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  79. }
  80. if (unlikely(!wall_time || wall_time < idle_time))
  81. continue;
  82. load = 100 * (wall_time - idle_time) / wall_time;
  83. if (load > max_load)
  84. max_load = load;
  85. }
  86. dbs_data->cdata->gov_check_cpu(cpu, max_load);
  87. }
  88. EXPORT_SYMBOL_GPL(dbs_check_cpu);
  89. static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data,
  90. unsigned int delay)
  91. {
  92. struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  93. mod_delayed_work_on(cpu, system_wq, &cdbs->work, delay);
  94. }
  95. void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
  96. unsigned int delay, bool all_cpus)
  97. {
  98. int i;
  99. if (!policy->governor_enabled)
  100. return;
  101. if (!all_cpus) {
  102. /*
  103. * Use raw_smp_processor_id() to avoid preemptible warnings.
  104. * We know that this is only called with all_cpus == false from
  105. * works that have been queued with *_work_on() functions and
  106. * those works are canceled during CPU_DOWN_PREPARE so they
  107. * can't possibly run on any other CPU.
  108. */
  109. __gov_queue_work(raw_smp_processor_id(), dbs_data, delay);
  110. } else {
  111. for_each_cpu(i, policy->cpus)
  112. __gov_queue_work(i, dbs_data, delay);
  113. }
  114. }
  115. EXPORT_SYMBOL_GPL(gov_queue_work);
  116. static inline void gov_cancel_work(struct dbs_data *dbs_data,
  117. struct cpufreq_policy *policy)
  118. {
  119. struct cpu_dbs_common_info *cdbs;
  120. int i;
  121. for_each_cpu(i, policy->cpus) {
  122. cdbs = dbs_data->cdata->get_cpu_cdbs(i);
  123. cancel_delayed_work_sync(&cdbs->work);
  124. }
  125. }
  126. /* Will return if we need to evaluate cpu load again or not */
  127. bool need_load_eval(struct cpu_dbs_common_info *cdbs,
  128. unsigned int sampling_rate)
  129. {
  130. if (policy_is_shared(cdbs->cur_policy)) {
  131. ktime_t time_now = ktime_get();
  132. s64 delta_us = ktime_us_delta(time_now, cdbs->time_stamp);
  133. /* Do nothing if we recently have sampled */
  134. if (delta_us < (s64)(sampling_rate / 2))
  135. return false;
  136. else
  137. cdbs->time_stamp = time_now;
  138. }
  139. return true;
  140. }
  141. EXPORT_SYMBOL_GPL(need_load_eval);
  142. static void set_sampling_rate(struct dbs_data *dbs_data,
  143. unsigned int sampling_rate)
  144. {
  145. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  146. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  147. cs_tuners->sampling_rate = sampling_rate;
  148. } else {
  149. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  150. od_tuners->sampling_rate = sampling_rate;
  151. }
  152. }
  153. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  154. struct common_dbs_data *cdata, unsigned int event)
  155. {
  156. struct dbs_data *dbs_data;
  157. struct od_cpu_dbs_info_s *od_dbs_info = NULL;
  158. struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
  159. struct od_ops *od_ops = NULL;
  160. struct od_dbs_tuners *od_tuners = NULL;
  161. struct cs_dbs_tuners *cs_tuners = NULL;
  162. struct cpu_dbs_common_info *cpu_cdbs;
  163. unsigned int sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
  164. int io_busy = 0;
  165. int rc;
  166. if (have_governor_per_policy())
  167. dbs_data = policy->governor_data;
  168. else
  169. dbs_data = cdata->gdbs_data;
  170. WARN_ON(!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT));
  171. switch (event) {
  172. case CPUFREQ_GOV_POLICY_INIT:
  173. if (have_governor_per_policy()) {
  174. WARN_ON(dbs_data);
  175. } else if (dbs_data) {
  176. dbs_data->usage_count++;
  177. policy->governor_data = dbs_data;
  178. return 0;
  179. }
  180. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  181. if (!dbs_data) {
  182. pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
  183. return -ENOMEM;
  184. }
  185. dbs_data->cdata = cdata;
  186. dbs_data->usage_count = 1;
  187. rc = cdata->init(dbs_data);
  188. if (rc) {
  189. pr_err("%s: POLICY_INIT: init() failed\n", __func__);
  190. kfree(dbs_data);
  191. return rc;
  192. }
  193. if (!have_governor_per_policy())
  194. WARN_ON(cpufreq_get_global_kobject());
  195. rc = sysfs_create_group(get_governor_parent_kobj(policy),
  196. get_sysfs_attr(dbs_data));
  197. if (rc) {
  198. cdata->exit(dbs_data);
  199. kfree(dbs_data);
  200. return rc;
  201. }
  202. policy->governor_data = dbs_data;
  203. /* policy latency is in ns. Convert it to us first */
  204. latency = policy->cpuinfo.transition_latency / 1000;
  205. if (latency == 0)
  206. latency = 1;
  207. /* Bring kernel and HW constraints together */
  208. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  209. MIN_LATENCY_MULTIPLIER * latency);
  210. set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
  211. latency * LATENCY_MULTIPLIER));
  212. if ((cdata->governor == GOV_CONSERVATIVE) &&
  213. (!policy->governor->initialized)) {
  214. struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
  215. cpufreq_register_notifier(cs_ops->notifier_block,
  216. CPUFREQ_TRANSITION_NOTIFIER);
  217. }
  218. if (!have_governor_per_policy())
  219. cdata->gdbs_data = dbs_data;
  220. return 0;
  221. case CPUFREQ_GOV_POLICY_EXIT:
  222. if (!--dbs_data->usage_count) {
  223. sysfs_remove_group(get_governor_parent_kobj(policy),
  224. get_sysfs_attr(dbs_data));
  225. if (!have_governor_per_policy())
  226. cpufreq_put_global_kobject();
  227. if ((dbs_data->cdata->governor == GOV_CONSERVATIVE) &&
  228. (policy->governor->initialized == 1)) {
  229. struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
  230. cpufreq_unregister_notifier(cs_ops->notifier_block,
  231. CPUFREQ_TRANSITION_NOTIFIER);
  232. }
  233. cdata->exit(dbs_data);
  234. kfree(dbs_data);
  235. cdata->gdbs_data = NULL;
  236. }
  237. policy->governor_data = NULL;
  238. return 0;
  239. }
  240. cpu_cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  241. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  242. cs_tuners = dbs_data->tuners;
  243. cs_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
  244. sampling_rate = cs_tuners->sampling_rate;
  245. ignore_nice = cs_tuners->ignore_nice_load;
  246. } else {
  247. od_tuners = dbs_data->tuners;
  248. od_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
  249. sampling_rate = od_tuners->sampling_rate;
  250. ignore_nice = od_tuners->ignore_nice_load;
  251. od_ops = dbs_data->cdata->gov_ops;
  252. io_busy = od_tuners->io_is_busy;
  253. }
  254. switch (event) {
  255. case CPUFREQ_GOV_START:
  256. if (!policy->cur)
  257. return -EINVAL;
  258. mutex_lock(&dbs_data->mutex);
  259. for_each_cpu(j, policy->cpus) {
  260. struct cpu_dbs_common_info *j_cdbs =
  261. dbs_data->cdata->get_cpu_cdbs(j);
  262. j_cdbs->cpu = j;
  263. j_cdbs->cur_policy = policy;
  264. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
  265. &j_cdbs->prev_cpu_wall, io_busy);
  266. if (ignore_nice)
  267. j_cdbs->prev_cpu_nice =
  268. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  269. mutex_init(&j_cdbs->timer_mutex);
  270. INIT_DEFERRABLE_WORK(&j_cdbs->work,
  271. dbs_data->cdata->gov_dbs_timer);
  272. }
  273. /*
  274. * conservative does not implement micro like ondemand
  275. * governor, thus we are bound to jiffes/HZ
  276. */
  277. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  278. cs_dbs_info->down_skip = 0;
  279. cs_dbs_info->enable = 1;
  280. cs_dbs_info->requested_freq = policy->cur;
  281. } else {
  282. od_dbs_info->rate_mult = 1;
  283. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  284. od_ops->powersave_bias_init_cpu(cpu);
  285. }
  286. mutex_unlock(&dbs_data->mutex);
  287. /* Initiate timer time stamp */
  288. cpu_cdbs->time_stamp = ktime_get();
  289. gov_queue_work(dbs_data, policy,
  290. delay_for_sampling_rate(sampling_rate), true);
  291. break;
  292. case CPUFREQ_GOV_STOP:
  293. if (dbs_data->cdata->governor == GOV_CONSERVATIVE)
  294. cs_dbs_info->enable = 0;
  295. gov_cancel_work(dbs_data, policy);
  296. mutex_lock(&dbs_data->mutex);
  297. mutex_destroy(&cpu_cdbs->timer_mutex);
  298. cpu_cdbs->cur_policy = NULL;
  299. mutex_unlock(&dbs_data->mutex);
  300. break;
  301. case CPUFREQ_GOV_LIMITS:
  302. mutex_lock(&cpu_cdbs->timer_mutex);
  303. if (policy->max < cpu_cdbs->cur_policy->cur)
  304. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  305. policy->max, CPUFREQ_RELATION_H);
  306. else if (policy->min > cpu_cdbs->cur_policy->cur)
  307. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  308. policy->min, CPUFREQ_RELATION_L);
  309. dbs_check_cpu(dbs_data, cpu);
  310. mutex_unlock(&cpu_cdbs->timer_mutex);
  311. break;
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);