cpufreq_governor.c 11 KB

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