cpufreq_governor.c 10 KB

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