cpufreq_governor.c 10 KB

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