cpufreq_governor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. static int cpufreq_governor_init(struct cpufreq_policy *policy,
  208. struct dbs_data *dbs_data,
  209. struct common_dbs_data *cdata)
  210. {
  211. unsigned int latency;
  212. int ret;
  213. if (dbs_data) {
  214. if (WARN_ON(have_governor_per_policy()))
  215. return -EINVAL;
  216. dbs_data->usage_count++;
  217. policy->governor_data = dbs_data;
  218. return 0;
  219. }
  220. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  221. if (!dbs_data)
  222. return -ENOMEM;
  223. dbs_data->cdata = cdata;
  224. dbs_data->usage_count = 1;
  225. ret = cdata->init(dbs_data, !policy->governor->initialized);
  226. if (ret)
  227. goto free_dbs_data;
  228. /* policy latency is in ns. Convert it to us first */
  229. latency = policy->cpuinfo.transition_latency / 1000;
  230. if (latency == 0)
  231. latency = 1;
  232. /* Bring kernel and HW constraints together */
  233. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  234. MIN_LATENCY_MULTIPLIER * latency);
  235. set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
  236. latency * LATENCY_MULTIPLIER));
  237. if (!have_governor_per_policy()) {
  238. if (WARN_ON(cpufreq_get_global_kobject())) {
  239. ret = -EINVAL;
  240. goto cdata_exit;
  241. }
  242. cdata->gdbs_data = dbs_data;
  243. }
  244. ret = sysfs_create_group(get_governor_parent_kobj(policy),
  245. get_sysfs_attr(dbs_data));
  246. if (ret)
  247. goto put_kobj;
  248. policy->governor_data = dbs_data;
  249. return 0;
  250. put_kobj:
  251. if (!have_governor_per_policy()) {
  252. cdata->gdbs_data = NULL;
  253. cpufreq_put_global_kobject();
  254. }
  255. cdata_exit:
  256. cdata->exit(dbs_data, !policy->governor->initialized);
  257. free_dbs_data:
  258. kfree(dbs_data);
  259. return ret;
  260. }
  261. static void cpufreq_governor_exit(struct cpufreq_policy *policy,
  262. struct dbs_data *dbs_data)
  263. {
  264. struct common_dbs_data *cdata = dbs_data->cdata;
  265. policy->governor_data = NULL;
  266. if (!--dbs_data->usage_count) {
  267. sysfs_remove_group(get_governor_parent_kobj(policy),
  268. get_sysfs_attr(dbs_data));
  269. if (!have_governor_per_policy()) {
  270. cdata->gdbs_data = NULL;
  271. cpufreq_put_global_kobject();
  272. }
  273. cdata->exit(dbs_data, policy->governor->initialized == 1);
  274. kfree(dbs_data);
  275. }
  276. }
  277. static int cpufreq_governor_start(struct cpufreq_policy *policy,
  278. struct dbs_data *dbs_data)
  279. {
  280. struct common_dbs_data *cdata = dbs_data->cdata;
  281. unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
  282. struct cpu_dbs_common_info *cpu_cdbs = cdata->get_cpu_cdbs(cpu);
  283. int io_busy = 0;
  284. if (!policy->cur)
  285. return -EINVAL;
  286. if (cdata->governor == GOV_CONSERVATIVE) {
  287. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  288. sampling_rate = cs_tuners->sampling_rate;
  289. ignore_nice = cs_tuners->ignore_nice_load;
  290. } else {
  291. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  292. sampling_rate = od_tuners->sampling_rate;
  293. ignore_nice = od_tuners->ignore_nice_load;
  294. io_busy = od_tuners->io_is_busy;
  295. }
  296. for_each_cpu(j, policy->cpus) {
  297. struct cpu_dbs_common_info *j_cdbs = cdata->get_cpu_cdbs(j);
  298. unsigned int prev_load;
  299. j_cdbs->cpu = j;
  300. j_cdbs->cur_policy = policy;
  301. j_cdbs->prev_cpu_idle =
  302. get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
  303. prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
  304. j_cdbs->prev_cpu_idle);
  305. j_cdbs->prev_load = 100 * prev_load /
  306. (unsigned int)j_cdbs->prev_cpu_wall;
  307. if (ignore_nice)
  308. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  309. mutex_init(&j_cdbs->timer_mutex);
  310. INIT_DEFERRABLE_WORK(&j_cdbs->work, cdata->gov_dbs_timer);
  311. }
  312. if (cdata->governor == GOV_CONSERVATIVE) {
  313. struct cs_cpu_dbs_info_s *cs_dbs_info =
  314. cdata->get_cpu_dbs_info_s(cpu);
  315. cs_dbs_info->down_skip = 0;
  316. cs_dbs_info->enable = 1;
  317. cs_dbs_info->requested_freq = policy->cur;
  318. } else {
  319. struct od_ops *od_ops = cdata->gov_ops;
  320. struct od_cpu_dbs_info_s *od_dbs_info = cdata->get_cpu_dbs_info_s(cpu);
  321. od_dbs_info->rate_mult = 1;
  322. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  323. od_ops->powersave_bias_init_cpu(cpu);
  324. }
  325. /* Initiate timer time stamp */
  326. cpu_cdbs->time_stamp = ktime_get();
  327. gov_queue_work(dbs_data, policy, delay_for_sampling_rate(sampling_rate),
  328. true);
  329. return 0;
  330. }
  331. static void cpufreq_governor_stop(struct cpufreq_policy *policy,
  332. struct dbs_data *dbs_data)
  333. {
  334. struct common_dbs_data *cdata = dbs_data->cdata;
  335. unsigned int cpu = policy->cpu;
  336. struct cpu_dbs_common_info *cpu_cdbs = cdata->get_cpu_cdbs(cpu);
  337. if (cdata->governor == GOV_CONSERVATIVE) {
  338. struct cs_cpu_dbs_info_s *cs_dbs_info =
  339. cdata->get_cpu_dbs_info_s(cpu);
  340. cs_dbs_info->enable = 0;
  341. }
  342. gov_cancel_work(dbs_data, policy);
  343. mutex_destroy(&cpu_cdbs->timer_mutex);
  344. cpu_cdbs->cur_policy = NULL;
  345. }
  346. static void cpufreq_governor_limits(struct cpufreq_policy *policy,
  347. struct dbs_data *dbs_data)
  348. {
  349. struct common_dbs_data *cdata = dbs_data->cdata;
  350. unsigned int cpu = policy->cpu;
  351. struct cpu_dbs_common_info *cpu_cdbs = cdata->get_cpu_cdbs(cpu);
  352. if (!cpu_cdbs->cur_policy)
  353. return;
  354. mutex_lock(&cpu_cdbs->timer_mutex);
  355. if (policy->max < cpu_cdbs->cur_policy->cur)
  356. __cpufreq_driver_target(cpu_cdbs->cur_policy, policy->max,
  357. CPUFREQ_RELATION_H);
  358. else if (policy->min > cpu_cdbs->cur_policy->cur)
  359. __cpufreq_driver_target(cpu_cdbs->cur_policy, policy->min,
  360. CPUFREQ_RELATION_L);
  361. dbs_check_cpu(dbs_data, cpu);
  362. mutex_unlock(&cpu_cdbs->timer_mutex);
  363. }
  364. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  365. struct common_dbs_data *cdata, unsigned int event)
  366. {
  367. struct dbs_data *dbs_data;
  368. int ret = 0;
  369. /* Lock governor to block concurrent initialization of governor */
  370. mutex_lock(&cdata->mutex);
  371. if (have_governor_per_policy())
  372. dbs_data = policy->governor_data;
  373. else
  374. dbs_data = cdata->gdbs_data;
  375. if (WARN_ON(!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT))) {
  376. ret = -EINVAL;
  377. goto unlock;
  378. }
  379. switch (event) {
  380. case CPUFREQ_GOV_POLICY_INIT:
  381. ret = cpufreq_governor_init(policy, dbs_data, cdata);
  382. break;
  383. case CPUFREQ_GOV_POLICY_EXIT:
  384. cpufreq_governor_exit(policy, dbs_data);
  385. break;
  386. case CPUFREQ_GOV_START:
  387. ret = cpufreq_governor_start(policy, dbs_data);
  388. break;
  389. case CPUFREQ_GOV_STOP:
  390. cpufreq_governor_stop(policy, dbs_data);
  391. break;
  392. case CPUFREQ_GOV_LIMITS:
  393. cpufreq_governor_limits(policy, dbs_data);
  394. break;
  395. }
  396. unlock:
  397. mutex_unlock(&cdata->mutex);
  398. return ret;
  399. }
  400. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);