cpufreq_schedutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * CPUFreq governor based on scheduler-provided CPU utilization data.
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/cpufreq.h>
  13. #include <linux/kthread.h>
  14. #include <uapi/linux/sched/types.h>
  15. #include <linux/slab.h>
  16. #include <trace/events/power.h>
  17. #include "sched.h"
  18. #define SUGOV_KTHREAD_PRIORITY 50
  19. struct sugov_tunables {
  20. struct gov_attr_set attr_set;
  21. unsigned int rate_limit_us;
  22. };
  23. struct sugov_policy {
  24. struct cpufreq_policy *policy;
  25. struct sugov_tunables *tunables;
  26. struct list_head tunables_hook;
  27. raw_spinlock_t update_lock; /* For shared policies */
  28. u64 last_freq_update_time;
  29. s64 freq_update_delay_ns;
  30. unsigned int next_freq;
  31. unsigned int cached_raw_freq;
  32. /* The next fields are only needed if fast switch cannot be used. */
  33. struct irq_work irq_work;
  34. struct kthread_work work;
  35. struct mutex work_lock;
  36. struct kthread_worker worker;
  37. struct task_struct *thread;
  38. bool work_in_progress;
  39. bool need_freq_update;
  40. };
  41. struct sugov_cpu {
  42. struct update_util_data update_util;
  43. struct sugov_policy *sg_policy;
  44. unsigned int cpu;
  45. bool iowait_boost_pending;
  46. unsigned int iowait_boost;
  47. unsigned int iowait_boost_max;
  48. u64 last_update;
  49. /* The fields below are only needed when sharing a policy. */
  50. unsigned long util;
  51. unsigned long max;
  52. unsigned int flags;
  53. /* The field below is for single-CPU policies only. */
  54. #ifdef CONFIG_NO_HZ_COMMON
  55. unsigned long saved_idle_calls;
  56. #endif
  57. };
  58. static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
  59. /************************ Governor internals ***********************/
  60. static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
  61. {
  62. s64 delta_ns;
  63. /*
  64. * Since cpufreq_update_util() is called with rq->lock held for
  65. * the @target_cpu, our per-cpu data is fully serialized.
  66. *
  67. * However, drivers cannot in general deal with cross-cpu
  68. * requests, so while get_next_freq() will work, our
  69. * sugov_update_commit() call may not for the fast switching platforms.
  70. *
  71. * Hence stop here for remote requests if they aren't supported
  72. * by the hardware, as calculating the frequency is pointless if
  73. * we cannot in fact act on it.
  74. *
  75. * For the slow switching platforms, the kthread is always scheduled on
  76. * the right set of CPUs and any CPU can find the next frequency and
  77. * schedule the kthread.
  78. */
  79. if (sg_policy->policy->fast_switch_enabled &&
  80. !cpufreq_can_do_remote_dvfs(sg_policy->policy))
  81. return false;
  82. if (sg_policy->work_in_progress)
  83. return false;
  84. if (unlikely(sg_policy->need_freq_update)) {
  85. sg_policy->need_freq_update = false;
  86. /*
  87. * This happens when limits change, so forget the previous
  88. * next_freq value and force an update.
  89. */
  90. sg_policy->next_freq = UINT_MAX;
  91. return true;
  92. }
  93. delta_ns = time - sg_policy->last_freq_update_time;
  94. return delta_ns >= sg_policy->freq_update_delay_ns;
  95. }
  96. static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
  97. unsigned int next_freq)
  98. {
  99. struct cpufreq_policy *policy = sg_policy->policy;
  100. if (sg_policy->next_freq == next_freq)
  101. return;
  102. sg_policy->next_freq = next_freq;
  103. sg_policy->last_freq_update_time = time;
  104. if (policy->fast_switch_enabled) {
  105. next_freq = cpufreq_driver_fast_switch(policy, next_freq);
  106. if (!next_freq)
  107. return;
  108. policy->cur = next_freq;
  109. trace_cpu_frequency(next_freq, smp_processor_id());
  110. } else {
  111. sg_policy->work_in_progress = true;
  112. irq_work_queue(&sg_policy->irq_work);
  113. }
  114. }
  115. /**
  116. * get_next_freq - Compute a new frequency for a given cpufreq policy.
  117. * @sg_policy: schedutil policy object to compute the new frequency for.
  118. * @util: Current CPU utilization.
  119. * @max: CPU capacity.
  120. *
  121. * If the utilization is frequency-invariant, choose the new frequency to be
  122. * proportional to it, that is
  123. *
  124. * next_freq = C * max_freq * util / max
  125. *
  126. * Otherwise, approximate the would-be frequency-invariant utilization by
  127. * util_raw * (curr_freq / max_freq) which leads to
  128. *
  129. * next_freq = C * curr_freq * util_raw / max
  130. *
  131. * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
  132. *
  133. * The lowest driver-supported frequency which is equal or greater than the raw
  134. * next_freq (as calculated above) is returned, subject to policy min/max and
  135. * cpufreq driver limitations.
  136. */
  137. static unsigned int get_next_freq(struct sugov_policy *sg_policy,
  138. unsigned long util, unsigned long max)
  139. {
  140. struct cpufreq_policy *policy = sg_policy->policy;
  141. unsigned int freq = arch_scale_freq_invariant() ?
  142. policy->cpuinfo.max_freq : policy->cur;
  143. freq = (freq + (freq >> 2)) * util / max;
  144. if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
  145. return sg_policy->next_freq;
  146. sg_policy->cached_raw_freq = freq;
  147. return cpufreq_driver_resolve_freq(policy, freq);
  148. }
  149. static void sugov_get_util(unsigned long *util, unsigned long *max, int cpu)
  150. {
  151. struct rq *rq = cpu_rq(cpu);
  152. unsigned long cfs_max;
  153. cfs_max = arch_scale_cpu_capacity(NULL, cpu);
  154. *util = min(rq->cfs.avg.util_avg, cfs_max);
  155. *max = cfs_max;
  156. }
  157. static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
  158. unsigned int flags)
  159. {
  160. if (flags & SCHED_CPUFREQ_IOWAIT) {
  161. if (sg_cpu->iowait_boost_pending)
  162. return;
  163. sg_cpu->iowait_boost_pending = true;
  164. if (sg_cpu->iowait_boost) {
  165. sg_cpu->iowait_boost <<= 1;
  166. if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
  167. sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
  168. } else {
  169. sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
  170. }
  171. } else if (sg_cpu->iowait_boost) {
  172. s64 delta_ns = time - sg_cpu->last_update;
  173. /* Clear iowait_boost if the CPU apprears to have been idle. */
  174. if (delta_ns > TICK_NSEC) {
  175. sg_cpu->iowait_boost = 0;
  176. sg_cpu->iowait_boost_pending = false;
  177. }
  178. }
  179. }
  180. static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
  181. unsigned long *max)
  182. {
  183. unsigned int boost_util, boost_max;
  184. if (!sg_cpu->iowait_boost)
  185. return;
  186. if (sg_cpu->iowait_boost_pending) {
  187. sg_cpu->iowait_boost_pending = false;
  188. } else {
  189. sg_cpu->iowait_boost >>= 1;
  190. if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
  191. sg_cpu->iowait_boost = 0;
  192. return;
  193. }
  194. }
  195. boost_util = sg_cpu->iowait_boost;
  196. boost_max = sg_cpu->iowait_boost_max;
  197. if (*util * boost_max < *max * boost_util) {
  198. *util = boost_util;
  199. *max = boost_max;
  200. }
  201. }
  202. #ifdef CONFIG_NO_HZ_COMMON
  203. static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
  204. {
  205. unsigned long idle_calls = tick_nohz_get_idle_calls();
  206. bool ret = idle_calls == sg_cpu->saved_idle_calls;
  207. sg_cpu->saved_idle_calls = idle_calls;
  208. return ret;
  209. }
  210. #else
  211. static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
  212. #endif /* CONFIG_NO_HZ_COMMON */
  213. static void sugov_update_single(struct update_util_data *hook, u64 time,
  214. unsigned int flags)
  215. {
  216. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  217. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  218. struct cpufreq_policy *policy = sg_policy->policy;
  219. unsigned long util, max;
  220. unsigned int next_f;
  221. bool busy;
  222. sugov_set_iowait_boost(sg_cpu, time, flags);
  223. sg_cpu->last_update = time;
  224. if (!sugov_should_update_freq(sg_policy, time))
  225. return;
  226. busy = sugov_cpu_is_busy(sg_cpu);
  227. if (flags & SCHED_CPUFREQ_RT_DL) {
  228. next_f = policy->cpuinfo.max_freq;
  229. } else {
  230. sugov_get_util(&util, &max, sg_cpu->cpu);
  231. sugov_iowait_boost(sg_cpu, &util, &max);
  232. next_f = get_next_freq(sg_policy, util, max);
  233. /*
  234. * Do not reduce the frequency if the CPU has not been idle
  235. * recently, as the reduction is likely to be premature then.
  236. */
  237. if (busy && next_f < sg_policy->next_freq)
  238. next_f = sg_policy->next_freq;
  239. }
  240. sugov_update_commit(sg_policy, time, next_f);
  241. }
  242. static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
  243. {
  244. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  245. struct cpufreq_policy *policy = sg_policy->policy;
  246. unsigned long util = 0, max = 1;
  247. unsigned int j;
  248. for_each_cpu(j, policy->cpus) {
  249. struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
  250. unsigned long j_util, j_max;
  251. s64 delta_ns;
  252. /*
  253. * If the CPU utilization was last updated before the previous
  254. * frequency update and the time elapsed between the last update
  255. * of the CPU utilization and the last frequency update is long
  256. * enough, don't take the CPU into account as it probably is
  257. * idle now (and clear iowait_boost for it).
  258. */
  259. delta_ns = time - j_sg_cpu->last_update;
  260. if (delta_ns > TICK_NSEC) {
  261. j_sg_cpu->iowait_boost = 0;
  262. j_sg_cpu->iowait_boost_pending = false;
  263. continue;
  264. }
  265. if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
  266. return policy->cpuinfo.max_freq;
  267. j_util = j_sg_cpu->util;
  268. j_max = j_sg_cpu->max;
  269. if (j_util * max > j_max * util) {
  270. util = j_util;
  271. max = j_max;
  272. }
  273. sugov_iowait_boost(j_sg_cpu, &util, &max);
  274. }
  275. return get_next_freq(sg_policy, util, max);
  276. }
  277. static void sugov_update_shared(struct update_util_data *hook, u64 time,
  278. unsigned int flags)
  279. {
  280. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  281. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  282. unsigned long util, max;
  283. unsigned int next_f;
  284. sugov_get_util(&util, &max, sg_cpu->cpu);
  285. raw_spin_lock(&sg_policy->update_lock);
  286. sg_cpu->util = util;
  287. sg_cpu->max = max;
  288. sg_cpu->flags = flags;
  289. sugov_set_iowait_boost(sg_cpu, time, flags);
  290. sg_cpu->last_update = time;
  291. if (sugov_should_update_freq(sg_policy, time)) {
  292. if (flags & SCHED_CPUFREQ_RT_DL)
  293. next_f = sg_policy->policy->cpuinfo.max_freq;
  294. else
  295. next_f = sugov_next_freq_shared(sg_cpu, time);
  296. sugov_update_commit(sg_policy, time, next_f);
  297. }
  298. raw_spin_unlock(&sg_policy->update_lock);
  299. }
  300. static void sugov_work(struct kthread_work *work)
  301. {
  302. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  303. mutex_lock(&sg_policy->work_lock);
  304. __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
  305. CPUFREQ_RELATION_L);
  306. mutex_unlock(&sg_policy->work_lock);
  307. sg_policy->work_in_progress = false;
  308. }
  309. static void sugov_irq_work(struct irq_work *irq_work)
  310. {
  311. struct sugov_policy *sg_policy;
  312. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  313. /*
  314. * For RT and deadline tasks, the schedutil governor shoots the
  315. * frequency to maximum. Special care must be taken to ensure that this
  316. * kthread doesn't result in the same behavior.
  317. *
  318. * This is (mostly) guaranteed by the work_in_progress flag. The flag is
  319. * updated only at the end of the sugov_work() function and before that
  320. * the schedutil governor rejects all other frequency scaling requests.
  321. *
  322. * There is a very rare case though, where the RT thread yields right
  323. * after the work_in_progress flag is cleared. The effects of that are
  324. * neglected for now.
  325. */
  326. kthread_queue_work(&sg_policy->worker, &sg_policy->work);
  327. }
  328. /************************** sysfs interface ************************/
  329. static struct sugov_tunables *global_tunables;
  330. static DEFINE_MUTEX(global_tunables_lock);
  331. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  332. {
  333. return container_of(attr_set, struct sugov_tunables, attr_set);
  334. }
  335. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  336. {
  337. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  338. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  339. }
  340. static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
  341. size_t count)
  342. {
  343. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  344. struct sugov_policy *sg_policy;
  345. unsigned int rate_limit_us;
  346. if (kstrtouint(buf, 10, &rate_limit_us))
  347. return -EINVAL;
  348. tunables->rate_limit_us = rate_limit_us;
  349. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  350. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  351. return count;
  352. }
  353. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  354. static struct attribute *sugov_attributes[] = {
  355. &rate_limit_us.attr,
  356. NULL
  357. };
  358. static struct kobj_type sugov_tunables_ktype = {
  359. .default_attrs = sugov_attributes,
  360. .sysfs_ops = &governor_sysfs_ops,
  361. };
  362. /********************** cpufreq governor interface *********************/
  363. static struct cpufreq_governor schedutil_gov;
  364. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  365. {
  366. struct sugov_policy *sg_policy;
  367. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  368. if (!sg_policy)
  369. return NULL;
  370. sg_policy->policy = policy;
  371. raw_spin_lock_init(&sg_policy->update_lock);
  372. return sg_policy;
  373. }
  374. static void sugov_policy_free(struct sugov_policy *sg_policy)
  375. {
  376. kfree(sg_policy);
  377. }
  378. static int sugov_kthread_create(struct sugov_policy *sg_policy)
  379. {
  380. struct task_struct *thread;
  381. struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
  382. struct cpufreq_policy *policy = sg_policy->policy;
  383. int ret;
  384. /* kthread only required for slow path */
  385. if (policy->fast_switch_enabled)
  386. return 0;
  387. kthread_init_work(&sg_policy->work, sugov_work);
  388. kthread_init_worker(&sg_policy->worker);
  389. thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
  390. "sugov:%d",
  391. cpumask_first(policy->related_cpus));
  392. if (IS_ERR(thread)) {
  393. pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
  394. return PTR_ERR(thread);
  395. }
  396. ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
  397. if (ret) {
  398. kthread_stop(thread);
  399. pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
  400. return ret;
  401. }
  402. sg_policy->thread = thread;
  403. /* Kthread is bound to all CPUs by default */
  404. if (!policy->dvfs_possible_from_any_cpu)
  405. kthread_bind_mask(thread, policy->related_cpus);
  406. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  407. mutex_init(&sg_policy->work_lock);
  408. wake_up_process(thread);
  409. return 0;
  410. }
  411. static void sugov_kthread_stop(struct sugov_policy *sg_policy)
  412. {
  413. /* kthread only required for slow path */
  414. if (sg_policy->policy->fast_switch_enabled)
  415. return;
  416. kthread_flush_worker(&sg_policy->worker);
  417. kthread_stop(sg_policy->thread);
  418. mutex_destroy(&sg_policy->work_lock);
  419. }
  420. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  421. {
  422. struct sugov_tunables *tunables;
  423. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  424. if (tunables) {
  425. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  426. if (!have_governor_per_policy())
  427. global_tunables = tunables;
  428. }
  429. return tunables;
  430. }
  431. static void sugov_tunables_free(struct sugov_tunables *tunables)
  432. {
  433. if (!have_governor_per_policy())
  434. global_tunables = NULL;
  435. kfree(tunables);
  436. }
  437. static int sugov_init(struct cpufreq_policy *policy)
  438. {
  439. struct sugov_policy *sg_policy;
  440. struct sugov_tunables *tunables;
  441. int ret = 0;
  442. /* State should be equivalent to EXIT */
  443. if (policy->governor_data)
  444. return -EBUSY;
  445. cpufreq_enable_fast_switch(policy);
  446. sg_policy = sugov_policy_alloc(policy);
  447. if (!sg_policy) {
  448. ret = -ENOMEM;
  449. goto disable_fast_switch;
  450. }
  451. ret = sugov_kthread_create(sg_policy);
  452. if (ret)
  453. goto free_sg_policy;
  454. mutex_lock(&global_tunables_lock);
  455. if (global_tunables) {
  456. if (WARN_ON(have_governor_per_policy())) {
  457. ret = -EINVAL;
  458. goto stop_kthread;
  459. }
  460. policy->governor_data = sg_policy;
  461. sg_policy->tunables = global_tunables;
  462. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  463. goto out;
  464. }
  465. tunables = sugov_tunables_alloc(sg_policy);
  466. if (!tunables) {
  467. ret = -ENOMEM;
  468. goto stop_kthread;
  469. }
  470. if (policy->transition_delay_us) {
  471. tunables->rate_limit_us = policy->transition_delay_us;
  472. } else {
  473. unsigned int lat;
  474. tunables->rate_limit_us = LATENCY_MULTIPLIER;
  475. lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
  476. if (lat)
  477. tunables->rate_limit_us *= lat;
  478. }
  479. policy->governor_data = sg_policy;
  480. sg_policy->tunables = tunables;
  481. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  482. get_governor_parent_kobj(policy), "%s",
  483. schedutil_gov.name);
  484. if (ret)
  485. goto fail;
  486. out:
  487. mutex_unlock(&global_tunables_lock);
  488. return 0;
  489. fail:
  490. policy->governor_data = NULL;
  491. sugov_tunables_free(tunables);
  492. stop_kthread:
  493. sugov_kthread_stop(sg_policy);
  494. free_sg_policy:
  495. mutex_unlock(&global_tunables_lock);
  496. sugov_policy_free(sg_policy);
  497. disable_fast_switch:
  498. cpufreq_disable_fast_switch(policy);
  499. pr_err("initialization failed (error %d)\n", ret);
  500. return ret;
  501. }
  502. static void sugov_exit(struct cpufreq_policy *policy)
  503. {
  504. struct sugov_policy *sg_policy = policy->governor_data;
  505. struct sugov_tunables *tunables = sg_policy->tunables;
  506. unsigned int count;
  507. mutex_lock(&global_tunables_lock);
  508. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  509. policy->governor_data = NULL;
  510. if (!count)
  511. sugov_tunables_free(tunables);
  512. mutex_unlock(&global_tunables_lock);
  513. sugov_kthread_stop(sg_policy);
  514. sugov_policy_free(sg_policy);
  515. cpufreq_disable_fast_switch(policy);
  516. }
  517. static int sugov_start(struct cpufreq_policy *policy)
  518. {
  519. struct sugov_policy *sg_policy = policy->governor_data;
  520. unsigned int cpu;
  521. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  522. sg_policy->last_freq_update_time = 0;
  523. sg_policy->next_freq = UINT_MAX;
  524. sg_policy->work_in_progress = false;
  525. sg_policy->need_freq_update = false;
  526. sg_policy->cached_raw_freq = 0;
  527. for_each_cpu(cpu, policy->cpus) {
  528. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  529. memset(sg_cpu, 0, sizeof(*sg_cpu));
  530. sg_cpu->sg_policy = sg_policy;
  531. sg_cpu->flags = SCHED_CPUFREQ_RT;
  532. sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
  533. }
  534. for_each_cpu(cpu, policy->cpus) {
  535. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  536. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  537. policy_is_shared(policy) ?
  538. sugov_update_shared :
  539. sugov_update_single);
  540. }
  541. return 0;
  542. }
  543. static void sugov_stop(struct cpufreq_policy *policy)
  544. {
  545. struct sugov_policy *sg_policy = policy->governor_data;
  546. unsigned int cpu;
  547. for_each_cpu(cpu, policy->cpus)
  548. cpufreq_remove_update_util_hook(cpu);
  549. synchronize_sched();
  550. if (!policy->fast_switch_enabled) {
  551. irq_work_sync(&sg_policy->irq_work);
  552. kthread_cancel_work_sync(&sg_policy->work);
  553. }
  554. }
  555. static void sugov_limits(struct cpufreq_policy *policy)
  556. {
  557. struct sugov_policy *sg_policy = policy->governor_data;
  558. if (!policy->fast_switch_enabled) {
  559. mutex_lock(&sg_policy->work_lock);
  560. cpufreq_policy_apply_limits(policy);
  561. mutex_unlock(&sg_policy->work_lock);
  562. }
  563. sg_policy->need_freq_update = true;
  564. }
  565. static struct cpufreq_governor schedutil_gov = {
  566. .name = "schedutil",
  567. .owner = THIS_MODULE,
  568. .init = sugov_init,
  569. .exit = sugov_exit,
  570. .start = sugov_start,
  571. .stop = sugov_stop,
  572. .limits = sugov_limits,
  573. };
  574. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  575. struct cpufreq_governor *cpufreq_default_governor(void)
  576. {
  577. return &schedutil_gov;
  578. }
  579. #endif
  580. static int __init sugov_register(void)
  581. {
  582. int cpu;
  583. for_each_possible_cpu(cpu)
  584. per_cpu(sugov_cpu, cpu).cpu = cpu;
  585. return cpufreq_register_governor(&schedutil_gov);
  586. }
  587. fs_initcall(sugov_register);