cpufreq_governor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 sampling_rate;
  35. unsigned int max_load = 0;
  36. unsigned int ignore_nice;
  37. unsigned int j;
  38. if (dbs_data->cdata->governor == GOV_ONDEMAND) {
  39. struct od_cpu_dbs_info_s *od_dbs_info =
  40. dbs_data->cdata->get_cpu_dbs_info_s(cpu);
  41. /*
  42. * Sometimes, the ondemand governor uses an additional
  43. * multiplier to give long delays. So apply this multiplier to
  44. * the 'sampling_rate', so as to keep the wake-up-from-idle
  45. * detection logic a bit conservative.
  46. */
  47. sampling_rate = od_tuners->sampling_rate;
  48. sampling_rate *= od_dbs_info->rate_mult;
  49. ignore_nice = od_tuners->ignore_nice_load;
  50. } else {
  51. sampling_rate = cs_tuners->sampling_rate;
  52. ignore_nice = cs_tuners->ignore_nice_load;
  53. }
  54. policy = cdbs->cur_policy;
  55. /* Get Absolute Load */
  56. for_each_cpu(j, policy->cpus) {
  57. struct cpu_dbs_common_info *j_cdbs;
  58. u64 cur_wall_time, cur_idle_time;
  59. unsigned int idle_time, wall_time;
  60. unsigned int load;
  61. int io_busy = 0;
  62. j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
  63. /*
  64. * For the purpose of ondemand, waiting for disk IO is
  65. * an indication that you're performance critical, and
  66. * not that the system is actually idle. So do not add
  67. * the iowait time to the cpu idle time.
  68. */
  69. if (dbs_data->cdata->governor == GOV_ONDEMAND)
  70. io_busy = od_tuners->io_is_busy;
  71. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
  72. wall_time = (unsigned int)
  73. (cur_wall_time - j_cdbs->prev_cpu_wall);
  74. j_cdbs->prev_cpu_wall = cur_wall_time;
  75. idle_time = (unsigned int)
  76. (cur_idle_time - j_cdbs->prev_cpu_idle);
  77. j_cdbs->prev_cpu_idle = cur_idle_time;
  78. if (ignore_nice) {
  79. u64 cur_nice;
  80. unsigned long cur_nice_jiffies;
  81. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  82. cdbs->prev_cpu_nice;
  83. /*
  84. * Assumption: nice time between sampling periods will
  85. * be less than 2^32 jiffies for 32 bit sys
  86. */
  87. cur_nice_jiffies = (unsigned long)
  88. cputime64_to_jiffies64(cur_nice);
  89. cdbs->prev_cpu_nice =
  90. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  91. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  92. }
  93. if (unlikely(!wall_time || wall_time < idle_time))
  94. continue;
  95. /*
  96. * If the CPU had gone completely idle, and a task just woke up
  97. * on this CPU now, it would be unfair to calculate 'load' the
  98. * usual way for this elapsed time-window, because it will show
  99. * near-zero load, irrespective of how CPU intensive that task
  100. * actually is. This is undesirable for latency-sensitive bursty
  101. * workloads.
  102. *
  103. * To avoid this, we reuse the 'load' from the previous
  104. * time-window and give this task a chance to start with a
  105. * reasonably high CPU frequency. (However, we shouldn't over-do
  106. * this copy, lest we get stuck at a high load (high frequency)
  107. * for too long, even when the current system load has actually
  108. * dropped down. So we perform the copy only once, upon the
  109. * first wake-up from idle.)
  110. *
  111. * Detecting this situation is easy: the governor's deferrable
  112. * timer would not have fired during CPU-idle periods. Hence
  113. * an unusually large 'wall_time' (as compared to the sampling
  114. * rate) indicates this scenario.
  115. *
  116. * prev_load can be zero in two cases and we must recalculate it
  117. * for both cases:
  118. * - during long idle intervals
  119. * - explicitly set to zero
  120. */
  121. if (unlikely(wall_time > (2 * sampling_rate) &&
  122. j_cdbs->prev_load)) {
  123. load = j_cdbs->prev_load;
  124. /*
  125. * Perform a destructive copy, to ensure that we copy
  126. * the previous load only once, upon the first wake-up
  127. * from idle.
  128. */
  129. j_cdbs->prev_load = 0;
  130. } else {
  131. load = 100 * (wall_time - idle_time) / wall_time;
  132. j_cdbs->prev_load = load;
  133. }
  134. if (load > max_load)
  135. max_load = load;
  136. }
  137. dbs_data->cdata->gov_check_cpu(cpu, max_load);
  138. }
  139. EXPORT_SYMBOL_GPL(dbs_check_cpu);
  140. static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data,
  141. unsigned int delay)
  142. {
  143. struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  144. mod_delayed_work_on(cpu, system_wq, &cdbs->work, delay);
  145. }
  146. void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
  147. unsigned int delay, bool all_cpus)
  148. {
  149. int i;
  150. mutex_lock(&cpufreq_governor_lock);
  151. if (!policy->governor_enabled)
  152. goto out_unlock;
  153. if (!all_cpus) {
  154. /*
  155. * Use raw_smp_processor_id() to avoid preemptible warnings.
  156. * We know that this is only called with all_cpus == false from
  157. * works that have been queued with *_work_on() functions and
  158. * those works are canceled during CPU_DOWN_PREPARE so they
  159. * can't possibly run on any other CPU.
  160. */
  161. __gov_queue_work(raw_smp_processor_id(), dbs_data, delay);
  162. } else {
  163. for_each_cpu(i, policy->cpus)
  164. __gov_queue_work(i, dbs_data, delay);
  165. }
  166. out_unlock:
  167. mutex_unlock(&cpufreq_governor_lock);
  168. }
  169. EXPORT_SYMBOL_GPL(gov_queue_work);
  170. static inline void gov_cancel_work(struct dbs_data *dbs_data,
  171. struct cpufreq_policy *policy)
  172. {
  173. struct cpu_dbs_common_info *cdbs;
  174. int i;
  175. for_each_cpu(i, policy->cpus) {
  176. cdbs = dbs_data->cdata->get_cpu_cdbs(i);
  177. cancel_delayed_work_sync(&cdbs->work);
  178. }
  179. }
  180. /* Will return if we need to evaluate cpu load again or not */
  181. bool need_load_eval(struct cpu_dbs_common_info *cdbs,
  182. unsigned int sampling_rate)
  183. {
  184. if (policy_is_shared(cdbs->cur_policy)) {
  185. ktime_t time_now = ktime_get();
  186. s64 delta_us = ktime_us_delta(time_now, cdbs->time_stamp);
  187. /* Do nothing if we recently have sampled */
  188. if (delta_us < (s64)(sampling_rate / 2))
  189. return false;
  190. else
  191. cdbs->time_stamp = time_now;
  192. }
  193. return true;
  194. }
  195. EXPORT_SYMBOL_GPL(need_load_eval);
  196. static void set_sampling_rate(struct dbs_data *dbs_data,
  197. unsigned int sampling_rate)
  198. {
  199. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  200. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  201. cs_tuners->sampling_rate = sampling_rate;
  202. } else {
  203. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  204. od_tuners->sampling_rate = sampling_rate;
  205. }
  206. }
  207. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  208. struct common_dbs_data *cdata, unsigned int event)
  209. {
  210. struct dbs_data *dbs_data;
  211. struct od_cpu_dbs_info_s *od_dbs_info = NULL;
  212. struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
  213. struct od_ops *od_ops = NULL;
  214. struct od_dbs_tuners *od_tuners = NULL;
  215. struct cs_dbs_tuners *cs_tuners = NULL;
  216. struct cpu_dbs_common_info *cpu_cdbs;
  217. unsigned int sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
  218. int io_busy = 0;
  219. int rc;
  220. if (have_governor_per_policy())
  221. dbs_data = policy->governor_data;
  222. else
  223. dbs_data = cdata->gdbs_data;
  224. WARN_ON(!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT));
  225. switch (event) {
  226. case CPUFREQ_GOV_POLICY_INIT:
  227. if (have_governor_per_policy()) {
  228. WARN_ON(dbs_data);
  229. } else if (dbs_data) {
  230. dbs_data->usage_count++;
  231. policy->governor_data = dbs_data;
  232. return 0;
  233. }
  234. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  235. if (!dbs_data) {
  236. pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
  237. return -ENOMEM;
  238. }
  239. dbs_data->cdata = cdata;
  240. dbs_data->usage_count = 1;
  241. rc = cdata->init(dbs_data);
  242. if (rc) {
  243. pr_err("%s: POLICY_INIT: init() failed\n", __func__);
  244. kfree(dbs_data);
  245. return rc;
  246. }
  247. if (!have_governor_per_policy())
  248. WARN_ON(cpufreq_get_global_kobject());
  249. rc = sysfs_create_group(get_governor_parent_kobj(policy),
  250. get_sysfs_attr(dbs_data));
  251. if (rc) {
  252. cdata->exit(dbs_data);
  253. kfree(dbs_data);
  254. return rc;
  255. }
  256. policy->governor_data = dbs_data;
  257. /* policy latency is in ns. Convert it to us first */
  258. latency = policy->cpuinfo.transition_latency / 1000;
  259. if (latency == 0)
  260. latency = 1;
  261. /* Bring kernel and HW constraints together */
  262. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  263. MIN_LATENCY_MULTIPLIER * latency);
  264. set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
  265. latency * LATENCY_MULTIPLIER));
  266. if ((cdata->governor == GOV_CONSERVATIVE) &&
  267. (!policy->governor->initialized)) {
  268. struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
  269. cpufreq_register_notifier(cs_ops->notifier_block,
  270. CPUFREQ_TRANSITION_NOTIFIER);
  271. }
  272. if (!have_governor_per_policy())
  273. cdata->gdbs_data = dbs_data;
  274. return 0;
  275. case CPUFREQ_GOV_POLICY_EXIT:
  276. if (!--dbs_data->usage_count) {
  277. sysfs_remove_group(get_governor_parent_kobj(policy),
  278. get_sysfs_attr(dbs_data));
  279. if (!have_governor_per_policy())
  280. cpufreq_put_global_kobject();
  281. if ((dbs_data->cdata->governor == GOV_CONSERVATIVE) &&
  282. (policy->governor->initialized == 1)) {
  283. struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
  284. cpufreq_unregister_notifier(cs_ops->notifier_block,
  285. CPUFREQ_TRANSITION_NOTIFIER);
  286. }
  287. cdata->exit(dbs_data);
  288. kfree(dbs_data);
  289. cdata->gdbs_data = NULL;
  290. }
  291. policy->governor_data = NULL;
  292. return 0;
  293. }
  294. cpu_cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  295. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  296. cs_tuners = dbs_data->tuners;
  297. cs_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
  298. sampling_rate = cs_tuners->sampling_rate;
  299. ignore_nice = cs_tuners->ignore_nice_load;
  300. } else {
  301. od_tuners = dbs_data->tuners;
  302. od_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
  303. sampling_rate = od_tuners->sampling_rate;
  304. ignore_nice = od_tuners->ignore_nice_load;
  305. od_ops = dbs_data->cdata->gov_ops;
  306. io_busy = od_tuners->io_is_busy;
  307. }
  308. switch (event) {
  309. case CPUFREQ_GOV_START:
  310. if (!policy->cur)
  311. return -EINVAL;
  312. mutex_lock(&dbs_data->mutex);
  313. for_each_cpu(j, policy->cpus) {
  314. struct cpu_dbs_common_info *j_cdbs =
  315. dbs_data->cdata->get_cpu_cdbs(j);
  316. unsigned int prev_load;
  317. j_cdbs->cpu = j;
  318. j_cdbs->cur_policy = policy;
  319. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
  320. &j_cdbs->prev_cpu_wall, io_busy);
  321. prev_load = (unsigned int)
  322. (j_cdbs->prev_cpu_wall - j_cdbs->prev_cpu_idle);
  323. j_cdbs->prev_load = 100 * prev_load /
  324. (unsigned int) j_cdbs->prev_cpu_wall;
  325. if (ignore_nice)
  326. j_cdbs->prev_cpu_nice =
  327. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  328. mutex_init(&j_cdbs->timer_mutex);
  329. INIT_DEFERRABLE_WORK(&j_cdbs->work,
  330. dbs_data->cdata->gov_dbs_timer);
  331. }
  332. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  333. cs_dbs_info->down_skip = 0;
  334. cs_dbs_info->enable = 1;
  335. cs_dbs_info->requested_freq = policy->cur;
  336. } else {
  337. od_dbs_info->rate_mult = 1;
  338. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  339. od_ops->powersave_bias_init_cpu(cpu);
  340. }
  341. mutex_unlock(&dbs_data->mutex);
  342. /* Initiate timer time stamp */
  343. cpu_cdbs->time_stamp = ktime_get();
  344. gov_queue_work(dbs_data, policy,
  345. delay_for_sampling_rate(sampling_rate), true);
  346. break;
  347. case CPUFREQ_GOV_STOP:
  348. if (dbs_data->cdata->governor == GOV_CONSERVATIVE)
  349. cs_dbs_info->enable = 0;
  350. gov_cancel_work(dbs_data, policy);
  351. mutex_lock(&dbs_data->mutex);
  352. mutex_destroy(&cpu_cdbs->timer_mutex);
  353. cpu_cdbs->cur_policy = NULL;
  354. mutex_unlock(&dbs_data->mutex);
  355. break;
  356. case CPUFREQ_GOV_LIMITS:
  357. mutex_lock(&dbs_data->mutex);
  358. if (!cpu_cdbs->cur_policy) {
  359. mutex_unlock(&dbs_data->mutex);
  360. break;
  361. }
  362. mutex_lock(&cpu_cdbs->timer_mutex);
  363. if (policy->max < cpu_cdbs->cur_policy->cur)
  364. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  365. policy->max, CPUFREQ_RELATION_H);
  366. else if (policy->min > cpu_cdbs->cur_policy->cur)
  367. __cpufreq_driver_target(cpu_cdbs->cur_policy,
  368. policy->min, CPUFREQ_RELATION_L);
  369. dbs_check_cpu(dbs_data, cpu);
  370. mutex_unlock(&cpu_cdbs->timer_mutex);
  371. mutex_unlock(&dbs_data->mutex);
  372. break;
  373. }
  374. return 0;
  375. }
  376. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);