cpufreq_governor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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_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 = cdbs->shared->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. /* Get Absolute Load */
  55. for_each_cpu(j, policy->cpus) {
  56. struct cpu_dbs_info *j_cdbs;
  57. u64 cur_wall_time, cur_idle_time;
  58. unsigned int idle_time, wall_time;
  59. unsigned int load;
  60. int io_busy = 0;
  61. j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
  62. /*
  63. * For the purpose of ondemand, waiting for disk IO is
  64. * an indication that you're performance critical, and
  65. * not that the system is actually idle. So do not add
  66. * the iowait time to the cpu idle time.
  67. */
  68. if (dbs_data->cdata->governor == GOV_ONDEMAND)
  69. io_busy = od_tuners->io_is_busy;
  70. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
  71. wall_time = (unsigned int)
  72. (cur_wall_time - j_cdbs->prev_cpu_wall);
  73. j_cdbs->prev_cpu_wall = cur_wall_time;
  74. idle_time = (unsigned int)
  75. (cur_idle_time - j_cdbs->prev_cpu_idle);
  76. j_cdbs->prev_cpu_idle = cur_idle_time;
  77. if (ignore_nice) {
  78. u64 cur_nice;
  79. unsigned long cur_nice_jiffies;
  80. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  81. cdbs->prev_cpu_nice;
  82. /*
  83. * Assumption: nice time between sampling periods will
  84. * be less than 2^32 jiffies for 32 bit sys
  85. */
  86. cur_nice_jiffies = (unsigned long)
  87. cputime64_to_jiffies64(cur_nice);
  88. cdbs->prev_cpu_nice =
  89. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  90. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  91. }
  92. if (unlikely(!wall_time || wall_time < idle_time))
  93. continue;
  94. /*
  95. * If the CPU had gone completely idle, and a task just woke up
  96. * on this CPU now, it would be unfair to calculate 'load' the
  97. * usual way for this elapsed time-window, because it will show
  98. * near-zero load, irrespective of how CPU intensive that task
  99. * actually is. This is undesirable for latency-sensitive bursty
  100. * workloads.
  101. *
  102. * To avoid this, we reuse the 'load' from the previous
  103. * time-window and give this task a chance to start with a
  104. * reasonably high CPU frequency. (However, we shouldn't over-do
  105. * this copy, lest we get stuck at a high load (high frequency)
  106. * for too long, even when the current system load has actually
  107. * dropped down. So we perform the copy only once, upon the
  108. * first wake-up from idle.)
  109. *
  110. * Detecting this situation is easy: the governor's deferrable
  111. * timer would not have fired during CPU-idle periods. Hence
  112. * an unusually large 'wall_time' (as compared to the sampling
  113. * rate) indicates this scenario.
  114. *
  115. * prev_load can be zero in two cases and we must recalculate it
  116. * for both cases:
  117. * - during long idle intervals
  118. * - explicitly set to zero
  119. */
  120. if (unlikely(wall_time > (2 * sampling_rate) &&
  121. j_cdbs->prev_load)) {
  122. load = j_cdbs->prev_load;
  123. /*
  124. * Perform a destructive copy, to ensure that we copy
  125. * the previous load only once, upon the first wake-up
  126. * from idle.
  127. */
  128. j_cdbs->prev_load = 0;
  129. } else {
  130. load = 100 * (wall_time - idle_time) / wall_time;
  131. j_cdbs->prev_load = load;
  132. }
  133. if (load > max_load)
  134. max_load = load;
  135. }
  136. dbs_data->cdata->gov_check_cpu(cpu, max_load);
  137. }
  138. EXPORT_SYMBOL_GPL(dbs_check_cpu);
  139. static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data,
  140. unsigned int delay)
  141. {
  142. struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
  143. mod_delayed_work_on(cpu, system_wq, &cdbs->dwork, delay);
  144. }
  145. void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
  146. unsigned int delay, bool all_cpus)
  147. {
  148. int i;
  149. mutex_lock(&cpufreq_governor_lock);
  150. if (!policy->governor_enabled)
  151. goto out_unlock;
  152. if (!all_cpus) {
  153. /*
  154. * Use raw_smp_processor_id() to avoid preemptible warnings.
  155. * We know that this is only called with all_cpus == false from
  156. * works that have been queued with *_work_on() functions and
  157. * those works are canceled during CPU_DOWN_PREPARE so they
  158. * can't possibly run on any other CPU.
  159. */
  160. __gov_queue_work(raw_smp_processor_id(), dbs_data, delay);
  161. } else {
  162. for_each_cpu(i, policy->cpus)
  163. __gov_queue_work(i, dbs_data, delay);
  164. }
  165. out_unlock:
  166. mutex_unlock(&cpufreq_governor_lock);
  167. }
  168. EXPORT_SYMBOL_GPL(gov_queue_work);
  169. static inline void gov_cancel_work(struct dbs_data *dbs_data,
  170. struct cpufreq_policy *policy)
  171. {
  172. struct cpu_dbs_info *cdbs;
  173. int i;
  174. for_each_cpu(i, policy->cpus) {
  175. cdbs = dbs_data->cdata->get_cpu_cdbs(i);
  176. cancel_delayed_work_sync(&cdbs->dwork);
  177. }
  178. }
  179. /* Will return if we need to evaluate cpu load again or not */
  180. static bool need_load_eval(struct cpu_common_dbs_info *shared,
  181. unsigned int sampling_rate)
  182. {
  183. if (policy_is_shared(shared->policy)) {
  184. ktime_t time_now = ktime_get();
  185. s64 delta_us = ktime_us_delta(time_now, shared->time_stamp);
  186. /* Do nothing if we recently have sampled */
  187. if (delta_us < (s64)(sampling_rate / 2))
  188. return false;
  189. else
  190. shared->time_stamp = time_now;
  191. }
  192. return true;
  193. }
  194. static void dbs_timer(struct work_struct *work)
  195. {
  196. struct cpu_dbs_info *cdbs = container_of(work, struct cpu_dbs_info,
  197. dwork.work);
  198. struct cpu_common_dbs_info *shared = cdbs->shared;
  199. struct cpufreq_policy *policy = shared->policy;
  200. struct dbs_data *dbs_data = policy->governor_data;
  201. unsigned int sampling_rate, delay;
  202. bool modify_all = true;
  203. mutex_lock(&shared->timer_mutex);
  204. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  205. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  206. sampling_rate = cs_tuners->sampling_rate;
  207. } else {
  208. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  209. sampling_rate = od_tuners->sampling_rate;
  210. }
  211. if (!need_load_eval(cdbs->shared, sampling_rate))
  212. modify_all = false;
  213. delay = dbs_data->cdata->gov_dbs_timer(cdbs, dbs_data, modify_all);
  214. gov_queue_work(dbs_data, policy, delay, modify_all);
  215. mutex_unlock(&shared->timer_mutex);
  216. }
  217. static void set_sampling_rate(struct dbs_data *dbs_data,
  218. unsigned int sampling_rate)
  219. {
  220. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  221. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  222. cs_tuners->sampling_rate = sampling_rate;
  223. } else {
  224. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  225. od_tuners->sampling_rate = sampling_rate;
  226. }
  227. }
  228. static int alloc_common_dbs_info(struct cpufreq_policy *policy,
  229. struct common_dbs_data *cdata)
  230. {
  231. struct cpu_common_dbs_info *shared;
  232. int j;
  233. /* Allocate memory for the common information for policy->cpus */
  234. shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  235. if (!shared)
  236. return -ENOMEM;
  237. /* Set shared for all CPUs, online+offline */
  238. for_each_cpu(j, policy->related_cpus)
  239. cdata->get_cpu_cdbs(j)->shared = shared;
  240. return 0;
  241. }
  242. static void free_common_dbs_info(struct cpufreq_policy *policy,
  243. struct common_dbs_data *cdata)
  244. {
  245. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(policy->cpu);
  246. struct cpu_common_dbs_info *shared = cdbs->shared;
  247. int j;
  248. for_each_cpu(j, policy->cpus)
  249. cdata->get_cpu_cdbs(j)->shared = NULL;
  250. kfree(shared);
  251. }
  252. static int cpufreq_governor_init(struct cpufreq_policy *policy,
  253. struct dbs_data *dbs_data,
  254. struct common_dbs_data *cdata)
  255. {
  256. unsigned int latency;
  257. int ret;
  258. /* State should be equivalent to EXIT */
  259. if (policy->governor_data)
  260. return -EBUSY;
  261. if (dbs_data) {
  262. if (WARN_ON(have_governor_per_policy()))
  263. return -EINVAL;
  264. ret = alloc_common_dbs_info(policy, cdata);
  265. if (ret)
  266. return ret;
  267. dbs_data->usage_count++;
  268. policy->governor_data = dbs_data;
  269. return 0;
  270. }
  271. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  272. if (!dbs_data)
  273. return -ENOMEM;
  274. ret = alloc_common_dbs_info(policy, cdata);
  275. if (ret)
  276. goto free_dbs_data;
  277. dbs_data->cdata = cdata;
  278. dbs_data->usage_count = 1;
  279. ret = cdata->init(dbs_data, !policy->governor->initialized);
  280. if (ret)
  281. goto free_common_dbs_info;
  282. /* policy latency is in ns. Convert it to us first */
  283. latency = policy->cpuinfo.transition_latency / 1000;
  284. if (latency == 0)
  285. latency = 1;
  286. /* Bring kernel and HW constraints together */
  287. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  288. MIN_LATENCY_MULTIPLIER * latency);
  289. set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
  290. latency * LATENCY_MULTIPLIER));
  291. if (!have_governor_per_policy()) {
  292. if (WARN_ON(cpufreq_get_global_kobject())) {
  293. ret = -EINVAL;
  294. goto cdata_exit;
  295. }
  296. cdata->gdbs_data = dbs_data;
  297. }
  298. ret = sysfs_create_group(get_governor_parent_kobj(policy),
  299. get_sysfs_attr(dbs_data));
  300. if (ret)
  301. goto put_kobj;
  302. policy->governor_data = dbs_data;
  303. return 0;
  304. put_kobj:
  305. if (!have_governor_per_policy()) {
  306. cdata->gdbs_data = NULL;
  307. cpufreq_put_global_kobject();
  308. }
  309. cdata_exit:
  310. cdata->exit(dbs_data, !policy->governor->initialized);
  311. free_common_dbs_info:
  312. free_common_dbs_info(policy, cdata);
  313. free_dbs_data:
  314. kfree(dbs_data);
  315. return ret;
  316. }
  317. static int cpufreq_governor_exit(struct cpufreq_policy *policy,
  318. struct dbs_data *dbs_data)
  319. {
  320. struct common_dbs_data *cdata = dbs_data->cdata;
  321. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(policy->cpu);
  322. /* State should be equivalent to INIT */
  323. if (!cdbs->shared || cdbs->shared->policy)
  324. return -EBUSY;
  325. policy->governor_data = NULL;
  326. if (!--dbs_data->usage_count) {
  327. sysfs_remove_group(get_governor_parent_kobj(policy),
  328. get_sysfs_attr(dbs_data));
  329. if (!have_governor_per_policy()) {
  330. cdata->gdbs_data = NULL;
  331. cpufreq_put_global_kobject();
  332. }
  333. cdata->exit(dbs_data, policy->governor->initialized == 1);
  334. kfree(dbs_data);
  335. }
  336. free_common_dbs_info(policy, cdata);
  337. return 0;
  338. }
  339. static int cpufreq_governor_start(struct cpufreq_policy *policy,
  340. struct dbs_data *dbs_data)
  341. {
  342. struct common_dbs_data *cdata = dbs_data->cdata;
  343. unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
  344. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu);
  345. struct cpu_common_dbs_info *shared = cdbs->shared;
  346. int io_busy = 0;
  347. if (!policy->cur)
  348. return -EINVAL;
  349. /* State should be equivalent to INIT */
  350. if (!shared || shared->policy)
  351. return -EBUSY;
  352. if (cdata->governor == GOV_CONSERVATIVE) {
  353. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  354. sampling_rate = cs_tuners->sampling_rate;
  355. ignore_nice = cs_tuners->ignore_nice_load;
  356. } else {
  357. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  358. sampling_rate = od_tuners->sampling_rate;
  359. ignore_nice = od_tuners->ignore_nice_load;
  360. io_busy = od_tuners->io_is_busy;
  361. }
  362. shared->policy = policy;
  363. shared->time_stamp = ktime_get();
  364. mutex_init(&shared->timer_mutex);
  365. for_each_cpu(j, policy->cpus) {
  366. struct cpu_dbs_info *j_cdbs = cdata->get_cpu_cdbs(j);
  367. unsigned int prev_load;
  368. j_cdbs->prev_cpu_idle =
  369. get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
  370. prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
  371. j_cdbs->prev_cpu_idle);
  372. j_cdbs->prev_load = 100 * prev_load /
  373. (unsigned int)j_cdbs->prev_cpu_wall;
  374. if (ignore_nice)
  375. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  376. INIT_DEFERRABLE_WORK(&j_cdbs->dwork, dbs_timer);
  377. }
  378. if (cdata->governor == GOV_CONSERVATIVE) {
  379. struct cs_cpu_dbs_info_s *cs_dbs_info =
  380. cdata->get_cpu_dbs_info_s(cpu);
  381. cs_dbs_info->down_skip = 0;
  382. cs_dbs_info->enable = 1;
  383. cs_dbs_info->requested_freq = policy->cur;
  384. } else {
  385. struct od_ops *od_ops = cdata->gov_ops;
  386. struct od_cpu_dbs_info_s *od_dbs_info = cdata->get_cpu_dbs_info_s(cpu);
  387. od_dbs_info->rate_mult = 1;
  388. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  389. od_ops->powersave_bias_init_cpu(cpu);
  390. }
  391. gov_queue_work(dbs_data, policy, delay_for_sampling_rate(sampling_rate),
  392. true);
  393. return 0;
  394. }
  395. static int cpufreq_governor_stop(struct cpufreq_policy *policy,
  396. struct dbs_data *dbs_data)
  397. {
  398. struct common_dbs_data *cdata = dbs_data->cdata;
  399. unsigned int cpu = policy->cpu;
  400. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu);
  401. struct cpu_common_dbs_info *shared = cdbs->shared;
  402. /* State should be equivalent to START */
  403. if (!shared || !shared->policy)
  404. return -EBUSY;
  405. gov_cancel_work(dbs_data, policy);
  406. if (cdata->governor == GOV_CONSERVATIVE) {
  407. struct cs_cpu_dbs_info_s *cs_dbs_info =
  408. cdata->get_cpu_dbs_info_s(cpu);
  409. cs_dbs_info->enable = 0;
  410. }
  411. shared->policy = NULL;
  412. mutex_destroy(&shared->timer_mutex);
  413. return 0;
  414. }
  415. static int cpufreq_governor_limits(struct cpufreq_policy *policy,
  416. struct dbs_data *dbs_data)
  417. {
  418. struct common_dbs_data *cdata = dbs_data->cdata;
  419. unsigned int cpu = policy->cpu;
  420. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu);
  421. /* State should be equivalent to START */
  422. if (!cdbs->shared || !cdbs->shared->policy)
  423. return -EBUSY;
  424. mutex_lock(&cdbs->shared->timer_mutex);
  425. if (policy->max < cdbs->shared->policy->cur)
  426. __cpufreq_driver_target(cdbs->shared->policy, policy->max,
  427. CPUFREQ_RELATION_H);
  428. else if (policy->min > cdbs->shared->policy->cur)
  429. __cpufreq_driver_target(cdbs->shared->policy, policy->min,
  430. CPUFREQ_RELATION_L);
  431. dbs_check_cpu(dbs_data, cpu);
  432. mutex_unlock(&cdbs->shared->timer_mutex);
  433. return 0;
  434. }
  435. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  436. struct common_dbs_data *cdata, unsigned int event)
  437. {
  438. struct dbs_data *dbs_data;
  439. int ret;
  440. /* Lock governor to block concurrent initialization of governor */
  441. mutex_lock(&cdata->mutex);
  442. if (have_governor_per_policy())
  443. dbs_data = policy->governor_data;
  444. else
  445. dbs_data = cdata->gdbs_data;
  446. if (!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT)) {
  447. ret = -EINVAL;
  448. goto unlock;
  449. }
  450. switch (event) {
  451. case CPUFREQ_GOV_POLICY_INIT:
  452. ret = cpufreq_governor_init(policy, dbs_data, cdata);
  453. break;
  454. case CPUFREQ_GOV_POLICY_EXIT:
  455. ret = cpufreq_governor_exit(policy, dbs_data);
  456. break;
  457. case CPUFREQ_GOV_START:
  458. ret = cpufreq_governor_start(policy, dbs_data);
  459. break;
  460. case CPUFREQ_GOV_STOP:
  461. ret = cpufreq_governor_stop(policy, dbs_data);
  462. break;
  463. case CPUFREQ_GOV_LIMITS:
  464. ret = cpufreq_governor_limits(policy, dbs_data);
  465. break;
  466. default:
  467. ret = -EINVAL;
  468. }
  469. unlock:
  470. mutex_unlock(&cdata->mutex);
  471. return ret;
  472. }
  473. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);