cpufreq_governor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. if (!all_cpus) {
  150. /*
  151. * Use raw_smp_processor_id() to avoid preemptible warnings.
  152. * We know that this is only called with all_cpus == false from
  153. * works that have been queued with *_work_on() functions and
  154. * those works are canceled during CPU_DOWN_PREPARE so they
  155. * can't possibly run on any other CPU.
  156. */
  157. __gov_queue_work(raw_smp_processor_id(), dbs_data, delay);
  158. } else {
  159. for_each_cpu(i, policy->cpus)
  160. __gov_queue_work(i, dbs_data, delay);
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(gov_queue_work);
  164. static inline void gov_cancel_work(struct dbs_data *dbs_data,
  165. struct cpufreq_policy *policy)
  166. {
  167. struct cpu_dbs_info *cdbs;
  168. int i;
  169. for_each_cpu(i, policy->cpus) {
  170. cdbs = dbs_data->cdata->get_cpu_cdbs(i);
  171. cancel_delayed_work_sync(&cdbs->dwork);
  172. }
  173. }
  174. /* Will return if we need to evaluate cpu load again or not */
  175. static bool need_load_eval(struct cpu_common_dbs_info *shared,
  176. unsigned int sampling_rate)
  177. {
  178. if (policy_is_shared(shared->policy)) {
  179. ktime_t time_now = ktime_get();
  180. s64 delta_us = ktime_us_delta(time_now, shared->time_stamp);
  181. /* Do nothing if we recently have sampled */
  182. if (delta_us < (s64)(sampling_rate / 2))
  183. return false;
  184. else
  185. shared->time_stamp = time_now;
  186. }
  187. return true;
  188. }
  189. static void dbs_timer(struct work_struct *work)
  190. {
  191. struct cpu_dbs_info *cdbs = container_of(work, struct cpu_dbs_info,
  192. dwork.work);
  193. struct cpu_common_dbs_info *shared = cdbs->shared;
  194. struct cpufreq_policy *policy;
  195. struct dbs_data *dbs_data;
  196. unsigned int sampling_rate, delay;
  197. bool modify_all = true;
  198. mutex_lock(&shared->timer_mutex);
  199. policy = shared->policy;
  200. /*
  201. * Governor might already be disabled and there is no point continuing
  202. * with the work-handler.
  203. */
  204. if (!policy)
  205. goto unlock;
  206. dbs_data = policy->governor_data;
  207. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  208. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  209. sampling_rate = cs_tuners->sampling_rate;
  210. } else {
  211. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  212. sampling_rate = od_tuners->sampling_rate;
  213. }
  214. if (!need_load_eval(cdbs->shared, sampling_rate))
  215. modify_all = false;
  216. delay = dbs_data->cdata->gov_dbs_timer(cdbs, dbs_data, modify_all);
  217. gov_queue_work(dbs_data, policy, delay, modify_all);
  218. unlock:
  219. mutex_unlock(&shared->timer_mutex);
  220. }
  221. static void set_sampling_rate(struct dbs_data *dbs_data,
  222. unsigned int sampling_rate)
  223. {
  224. if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
  225. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  226. cs_tuners->sampling_rate = sampling_rate;
  227. } else {
  228. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  229. od_tuners->sampling_rate = sampling_rate;
  230. }
  231. }
  232. static int alloc_common_dbs_info(struct cpufreq_policy *policy,
  233. struct common_dbs_data *cdata)
  234. {
  235. struct cpu_common_dbs_info *shared;
  236. int j;
  237. /* Allocate memory for the common information for policy->cpus */
  238. shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  239. if (!shared)
  240. return -ENOMEM;
  241. /* Set shared for all CPUs, online+offline */
  242. for_each_cpu(j, policy->related_cpus)
  243. cdata->get_cpu_cdbs(j)->shared = shared;
  244. return 0;
  245. }
  246. static void free_common_dbs_info(struct cpufreq_policy *policy,
  247. struct common_dbs_data *cdata)
  248. {
  249. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(policy->cpu);
  250. struct cpu_common_dbs_info *shared = cdbs->shared;
  251. int j;
  252. for_each_cpu(j, policy->cpus)
  253. cdata->get_cpu_cdbs(j)->shared = NULL;
  254. kfree(shared);
  255. }
  256. static int cpufreq_governor_init(struct cpufreq_policy *policy,
  257. struct dbs_data *dbs_data,
  258. struct common_dbs_data *cdata)
  259. {
  260. unsigned int latency;
  261. int ret;
  262. /* State should be equivalent to EXIT */
  263. if (policy->governor_data)
  264. return -EBUSY;
  265. if (dbs_data) {
  266. if (WARN_ON(have_governor_per_policy()))
  267. return -EINVAL;
  268. ret = alloc_common_dbs_info(policy, cdata);
  269. if (ret)
  270. return ret;
  271. dbs_data->usage_count++;
  272. policy->governor_data = dbs_data;
  273. return 0;
  274. }
  275. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  276. if (!dbs_data)
  277. return -ENOMEM;
  278. ret = alloc_common_dbs_info(policy, cdata);
  279. if (ret)
  280. goto free_dbs_data;
  281. dbs_data->cdata = cdata;
  282. dbs_data->usage_count = 1;
  283. ret = cdata->init(dbs_data, !policy->governor->initialized);
  284. if (ret)
  285. goto free_common_dbs_info;
  286. /* policy latency is in ns. Convert it to us first */
  287. latency = policy->cpuinfo.transition_latency / 1000;
  288. if (latency == 0)
  289. latency = 1;
  290. /* Bring kernel and HW constraints together */
  291. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  292. MIN_LATENCY_MULTIPLIER * latency);
  293. set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
  294. latency * LATENCY_MULTIPLIER));
  295. if (!have_governor_per_policy())
  296. cdata->gdbs_data = dbs_data;
  297. policy->governor_data = dbs_data;
  298. ret = sysfs_create_group(get_governor_parent_kobj(policy),
  299. get_sysfs_attr(dbs_data));
  300. if (ret)
  301. goto reset_gdbs_data;
  302. return 0;
  303. reset_gdbs_data:
  304. policy->governor_data = NULL;
  305. if (!have_governor_per_policy())
  306. cdata->gdbs_data = NULL;
  307. cdata->exit(dbs_data, !policy->governor->initialized);
  308. free_common_dbs_info:
  309. free_common_dbs_info(policy, cdata);
  310. free_dbs_data:
  311. kfree(dbs_data);
  312. return ret;
  313. }
  314. static int cpufreq_governor_exit(struct cpufreq_policy *policy,
  315. struct dbs_data *dbs_data)
  316. {
  317. struct common_dbs_data *cdata = dbs_data->cdata;
  318. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(policy->cpu);
  319. /* State should be equivalent to INIT */
  320. if (!cdbs->shared || cdbs->shared->policy)
  321. return -EBUSY;
  322. if (!--dbs_data->usage_count) {
  323. sysfs_remove_group(get_governor_parent_kobj(policy),
  324. get_sysfs_attr(dbs_data));
  325. policy->governor_data = NULL;
  326. if (!have_governor_per_policy())
  327. cdata->gdbs_data = NULL;
  328. cdata->exit(dbs_data, policy->governor->initialized == 1);
  329. kfree(dbs_data);
  330. } else {
  331. policy->governor_data = NULL;
  332. }
  333. free_common_dbs_info(policy, cdata);
  334. return 0;
  335. }
  336. static int cpufreq_governor_start(struct cpufreq_policy *policy,
  337. struct dbs_data *dbs_data)
  338. {
  339. struct common_dbs_data *cdata = dbs_data->cdata;
  340. unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
  341. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu);
  342. struct cpu_common_dbs_info *shared = cdbs->shared;
  343. int io_busy = 0;
  344. if (!policy->cur)
  345. return -EINVAL;
  346. /* State should be equivalent to INIT */
  347. if (!shared || shared->policy)
  348. return -EBUSY;
  349. if (cdata->governor == GOV_CONSERVATIVE) {
  350. struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  351. sampling_rate = cs_tuners->sampling_rate;
  352. ignore_nice = cs_tuners->ignore_nice_load;
  353. } else {
  354. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  355. sampling_rate = od_tuners->sampling_rate;
  356. ignore_nice = od_tuners->ignore_nice_load;
  357. io_busy = od_tuners->io_is_busy;
  358. }
  359. shared->policy = policy;
  360. shared->time_stamp = ktime_get();
  361. mutex_init(&shared->timer_mutex);
  362. for_each_cpu(j, policy->cpus) {
  363. struct cpu_dbs_info *j_cdbs = cdata->get_cpu_cdbs(j);
  364. unsigned int prev_load;
  365. j_cdbs->prev_cpu_idle =
  366. get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
  367. prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
  368. j_cdbs->prev_cpu_idle);
  369. j_cdbs->prev_load = 100 * prev_load /
  370. (unsigned int)j_cdbs->prev_cpu_wall;
  371. if (ignore_nice)
  372. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  373. INIT_DEFERRABLE_WORK(&j_cdbs->dwork, dbs_timer);
  374. }
  375. if (cdata->governor == GOV_CONSERVATIVE) {
  376. struct cs_cpu_dbs_info_s *cs_dbs_info =
  377. cdata->get_cpu_dbs_info_s(cpu);
  378. cs_dbs_info->down_skip = 0;
  379. cs_dbs_info->requested_freq = policy->cur;
  380. } else {
  381. struct od_ops *od_ops = cdata->gov_ops;
  382. struct od_cpu_dbs_info_s *od_dbs_info = cdata->get_cpu_dbs_info_s(cpu);
  383. od_dbs_info->rate_mult = 1;
  384. od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  385. od_ops->powersave_bias_init_cpu(cpu);
  386. }
  387. gov_queue_work(dbs_data, policy, delay_for_sampling_rate(sampling_rate),
  388. true);
  389. return 0;
  390. }
  391. static int cpufreq_governor_stop(struct cpufreq_policy *policy,
  392. struct dbs_data *dbs_data)
  393. {
  394. struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs(policy->cpu);
  395. struct cpu_common_dbs_info *shared = cdbs->shared;
  396. /* State should be equivalent to START */
  397. if (!shared || !shared->policy)
  398. return -EBUSY;
  399. /*
  400. * Work-handler must see this updated, as it should not proceed any
  401. * further after governor is disabled. And so timer_mutex is taken while
  402. * updating this value.
  403. */
  404. mutex_lock(&shared->timer_mutex);
  405. shared->policy = NULL;
  406. mutex_unlock(&shared->timer_mutex);
  407. gov_cancel_work(dbs_data, policy);
  408. mutex_destroy(&shared->timer_mutex);
  409. return 0;
  410. }
  411. static int cpufreq_governor_limits(struct cpufreq_policy *policy,
  412. struct dbs_data *dbs_data)
  413. {
  414. struct common_dbs_data *cdata = dbs_data->cdata;
  415. unsigned int cpu = policy->cpu;
  416. struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu);
  417. /* State should be equivalent to START */
  418. if (!cdbs->shared || !cdbs->shared->policy)
  419. return -EBUSY;
  420. mutex_lock(&cdbs->shared->timer_mutex);
  421. if (policy->max < cdbs->shared->policy->cur)
  422. __cpufreq_driver_target(cdbs->shared->policy, policy->max,
  423. CPUFREQ_RELATION_H);
  424. else if (policy->min > cdbs->shared->policy->cur)
  425. __cpufreq_driver_target(cdbs->shared->policy, policy->min,
  426. CPUFREQ_RELATION_L);
  427. dbs_check_cpu(dbs_data, cpu);
  428. mutex_unlock(&cdbs->shared->timer_mutex);
  429. return 0;
  430. }
  431. int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  432. struct common_dbs_data *cdata, unsigned int event)
  433. {
  434. struct dbs_data *dbs_data;
  435. int ret;
  436. /* Lock governor to block concurrent initialization of governor */
  437. mutex_lock(&cdata->mutex);
  438. if (have_governor_per_policy())
  439. dbs_data = policy->governor_data;
  440. else
  441. dbs_data = cdata->gdbs_data;
  442. if (!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT)) {
  443. ret = -EINVAL;
  444. goto unlock;
  445. }
  446. switch (event) {
  447. case CPUFREQ_GOV_POLICY_INIT:
  448. ret = cpufreq_governor_init(policy, dbs_data, cdata);
  449. break;
  450. case CPUFREQ_GOV_POLICY_EXIT:
  451. ret = cpufreq_governor_exit(policy, dbs_data);
  452. break;
  453. case CPUFREQ_GOV_START:
  454. ret = cpufreq_governor_start(policy, dbs_data);
  455. break;
  456. case CPUFREQ_GOV_STOP:
  457. ret = cpufreq_governor_stop(policy, dbs_data);
  458. break;
  459. case CPUFREQ_GOV_LIMITS:
  460. ret = cpufreq_governor_limits(policy, dbs_data);
  461. break;
  462. default:
  463. ret = -EINVAL;
  464. }
  465. unlock:
  466. mutex_unlock(&cdata->mutex);
  467. return ret;
  468. }
  469. EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);