cpufreq_schedutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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_cpu(sg_cpu->cpu);
  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. /* Reset cached freq as next_freq has changed */
  240. sg_policy->cached_raw_freq = 0;
  241. }
  242. }
  243. sugov_update_commit(sg_policy, time, next_f);
  244. }
  245. static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
  246. {
  247. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  248. struct cpufreq_policy *policy = sg_policy->policy;
  249. unsigned long util = 0, max = 1;
  250. unsigned int j;
  251. for_each_cpu(j, policy->cpus) {
  252. struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
  253. unsigned long j_util, j_max;
  254. s64 delta_ns;
  255. /*
  256. * If the CPU utilization was last updated before the previous
  257. * frequency update and the time elapsed between the last update
  258. * of the CPU utilization and the last frequency update is long
  259. * enough, don't take the CPU into account as it probably is
  260. * idle now (and clear iowait_boost for it).
  261. */
  262. delta_ns = time - j_sg_cpu->last_update;
  263. if (delta_ns > TICK_NSEC) {
  264. j_sg_cpu->iowait_boost = 0;
  265. j_sg_cpu->iowait_boost_pending = false;
  266. continue;
  267. }
  268. if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
  269. return policy->cpuinfo.max_freq;
  270. j_util = j_sg_cpu->util;
  271. j_max = j_sg_cpu->max;
  272. if (j_util * max > j_max * util) {
  273. util = j_util;
  274. max = j_max;
  275. }
  276. sugov_iowait_boost(j_sg_cpu, &util, &max);
  277. }
  278. return get_next_freq(sg_policy, util, max);
  279. }
  280. static void sugov_update_shared(struct update_util_data *hook, u64 time,
  281. unsigned int flags)
  282. {
  283. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  284. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  285. unsigned long util, max;
  286. unsigned int next_f;
  287. sugov_get_util(&util, &max, sg_cpu->cpu);
  288. raw_spin_lock(&sg_policy->update_lock);
  289. sg_cpu->util = util;
  290. sg_cpu->max = max;
  291. sg_cpu->flags = flags;
  292. sugov_set_iowait_boost(sg_cpu, time, flags);
  293. sg_cpu->last_update = time;
  294. if (sugov_should_update_freq(sg_policy, time)) {
  295. if (flags & SCHED_CPUFREQ_RT_DL)
  296. next_f = sg_policy->policy->cpuinfo.max_freq;
  297. else
  298. next_f = sugov_next_freq_shared(sg_cpu, time);
  299. sugov_update_commit(sg_policy, time, next_f);
  300. }
  301. raw_spin_unlock(&sg_policy->update_lock);
  302. }
  303. static void sugov_work(struct kthread_work *work)
  304. {
  305. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  306. mutex_lock(&sg_policy->work_lock);
  307. __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
  308. CPUFREQ_RELATION_L);
  309. mutex_unlock(&sg_policy->work_lock);
  310. sg_policy->work_in_progress = false;
  311. }
  312. static void sugov_irq_work(struct irq_work *irq_work)
  313. {
  314. struct sugov_policy *sg_policy;
  315. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  316. /*
  317. * For RT and deadline tasks, the schedutil governor shoots the
  318. * frequency to maximum. Special care must be taken to ensure that this
  319. * kthread doesn't result in the same behavior.
  320. *
  321. * This is (mostly) guaranteed by the work_in_progress flag. The flag is
  322. * updated only at the end of the sugov_work() function and before that
  323. * the schedutil governor rejects all other frequency scaling requests.
  324. *
  325. * There is a very rare case though, where the RT thread yields right
  326. * after the work_in_progress flag is cleared. The effects of that are
  327. * neglected for now.
  328. */
  329. kthread_queue_work(&sg_policy->worker, &sg_policy->work);
  330. }
  331. /************************** sysfs interface ************************/
  332. static struct sugov_tunables *global_tunables;
  333. static DEFINE_MUTEX(global_tunables_lock);
  334. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  335. {
  336. return container_of(attr_set, struct sugov_tunables, attr_set);
  337. }
  338. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  339. {
  340. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  341. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  342. }
  343. static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
  344. size_t count)
  345. {
  346. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  347. struct sugov_policy *sg_policy;
  348. unsigned int rate_limit_us;
  349. if (kstrtouint(buf, 10, &rate_limit_us))
  350. return -EINVAL;
  351. tunables->rate_limit_us = rate_limit_us;
  352. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  353. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  354. return count;
  355. }
  356. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  357. static struct attribute *sugov_attributes[] = {
  358. &rate_limit_us.attr,
  359. NULL
  360. };
  361. static struct kobj_type sugov_tunables_ktype = {
  362. .default_attrs = sugov_attributes,
  363. .sysfs_ops = &governor_sysfs_ops,
  364. };
  365. /********************** cpufreq governor interface *********************/
  366. static struct cpufreq_governor schedutil_gov;
  367. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  368. {
  369. struct sugov_policy *sg_policy;
  370. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  371. if (!sg_policy)
  372. return NULL;
  373. sg_policy->policy = policy;
  374. raw_spin_lock_init(&sg_policy->update_lock);
  375. return sg_policy;
  376. }
  377. static void sugov_policy_free(struct sugov_policy *sg_policy)
  378. {
  379. kfree(sg_policy);
  380. }
  381. static int sugov_kthread_create(struct sugov_policy *sg_policy)
  382. {
  383. struct task_struct *thread;
  384. struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
  385. struct cpufreq_policy *policy = sg_policy->policy;
  386. int ret;
  387. /* kthread only required for slow path */
  388. if (policy->fast_switch_enabled)
  389. return 0;
  390. kthread_init_work(&sg_policy->work, sugov_work);
  391. kthread_init_worker(&sg_policy->worker);
  392. thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
  393. "sugov:%d",
  394. cpumask_first(policy->related_cpus));
  395. if (IS_ERR(thread)) {
  396. pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
  397. return PTR_ERR(thread);
  398. }
  399. ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
  400. if (ret) {
  401. kthread_stop(thread);
  402. pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
  403. return ret;
  404. }
  405. sg_policy->thread = thread;
  406. /* Kthread is bound to all CPUs by default */
  407. if (!policy->dvfs_possible_from_any_cpu)
  408. kthread_bind_mask(thread, policy->related_cpus);
  409. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  410. mutex_init(&sg_policy->work_lock);
  411. wake_up_process(thread);
  412. return 0;
  413. }
  414. static void sugov_kthread_stop(struct sugov_policy *sg_policy)
  415. {
  416. /* kthread only required for slow path */
  417. if (sg_policy->policy->fast_switch_enabled)
  418. return;
  419. kthread_flush_worker(&sg_policy->worker);
  420. kthread_stop(sg_policy->thread);
  421. mutex_destroy(&sg_policy->work_lock);
  422. }
  423. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  424. {
  425. struct sugov_tunables *tunables;
  426. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  427. if (tunables) {
  428. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  429. if (!have_governor_per_policy())
  430. global_tunables = tunables;
  431. }
  432. return tunables;
  433. }
  434. static void sugov_tunables_free(struct sugov_tunables *tunables)
  435. {
  436. if (!have_governor_per_policy())
  437. global_tunables = NULL;
  438. kfree(tunables);
  439. }
  440. static int sugov_init(struct cpufreq_policy *policy)
  441. {
  442. struct sugov_policy *sg_policy;
  443. struct sugov_tunables *tunables;
  444. int ret = 0;
  445. /* State should be equivalent to EXIT */
  446. if (policy->governor_data)
  447. return -EBUSY;
  448. cpufreq_enable_fast_switch(policy);
  449. sg_policy = sugov_policy_alloc(policy);
  450. if (!sg_policy) {
  451. ret = -ENOMEM;
  452. goto disable_fast_switch;
  453. }
  454. ret = sugov_kthread_create(sg_policy);
  455. if (ret)
  456. goto free_sg_policy;
  457. mutex_lock(&global_tunables_lock);
  458. if (global_tunables) {
  459. if (WARN_ON(have_governor_per_policy())) {
  460. ret = -EINVAL;
  461. goto stop_kthread;
  462. }
  463. policy->governor_data = sg_policy;
  464. sg_policy->tunables = global_tunables;
  465. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  466. goto out;
  467. }
  468. tunables = sugov_tunables_alloc(sg_policy);
  469. if (!tunables) {
  470. ret = -ENOMEM;
  471. goto stop_kthread;
  472. }
  473. tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
  474. policy->governor_data = sg_policy;
  475. sg_policy->tunables = tunables;
  476. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  477. get_governor_parent_kobj(policy), "%s",
  478. schedutil_gov.name);
  479. if (ret)
  480. goto fail;
  481. out:
  482. mutex_unlock(&global_tunables_lock);
  483. return 0;
  484. fail:
  485. policy->governor_data = NULL;
  486. sugov_tunables_free(tunables);
  487. stop_kthread:
  488. sugov_kthread_stop(sg_policy);
  489. free_sg_policy:
  490. mutex_unlock(&global_tunables_lock);
  491. sugov_policy_free(sg_policy);
  492. disable_fast_switch:
  493. cpufreq_disable_fast_switch(policy);
  494. pr_err("initialization failed (error %d)\n", ret);
  495. return ret;
  496. }
  497. static void sugov_exit(struct cpufreq_policy *policy)
  498. {
  499. struct sugov_policy *sg_policy = policy->governor_data;
  500. struct sugov_tunables *tunables = sg_policy->tunables;
  501. unsigned int count;
  502. mutex_lock(&global_tunables_lock);
  503. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  504. policy->governor_data = NULL;
  505. if (!count)
  506. sugov_tunables_free(tunables);
  507. mutex_unlock(&global_tunables_lock);
  508. sugov_kthread_stop(sg_policy);
  509. sugov_policy_free(sg_policy);
  510. cpufreq_disable_fast_switch(policy);
  511. }
  512. static int sugov_start(struct cpufreq_policy *policy)
  513. {
  514. struct sugov_policy *sg_policy = policy->governor_data;
  515. unsigned int cpu;
  516. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  517. sg_policy->last_freq_update_time = 0;
  518. sg_policy->next_freq = UINT_MAX;
  519. sg_policy->work_in_progress = false;
  520. sg_policy->need_freq_update = false;
  521. sg_policy->cached_raw_freq = 0;
  522. for_each_cpu(cpu, policy->cpus) {
  523. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  524. memset(sg_cpu, 0, sizeof(*sg_cpu));
  525. sg_cpu->cpu = cpu;
  526. sg_cpu->sg_policy = sg_policy;
  527. sg_cpu->flags = SCHED_CPUFREQ_RT;
  528. sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
  529. }
  530. for_each_cpu(cpu, policy->cpus) {
  531. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  532. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  533. policy_is_shared(policy) ?
  534. sugov_update_shared :
  535. sugov_update_single);
  536. }
  537. return 0;
  538. }
  539. static void sugov_stop(struct cpufreq_policy *policy)
  540. {
  541. struct sugov_policy *sg_policy = policy->governor_data;
  542. unsigned int cpu;
  543. for_each_cpu(cpu, policy->cpus)
  544. cpufreq_remove_update_util_hook(cpu);
  545. synchronize_sched();
  546. if (!policy->fast_switch_enabled) {
  547. irq_work_sync(&sg_policy->irq_work);
  548. kthread_cancel_work_sync(&sg_policy->work);
  549. }
  550. }
  551. static void sugov_limits(struct cpufreq_policy *policy)
  552. {
  553. struct sugov_policy *sg_policy = policy->governor_data;
  554. if (!policy->fast_switch_enabled) {
  555. mutex_lock(&sg_policy->work_lock);
  556. cpufreq_policy_apply_limits(policy);
  557. mutex_unlock(&sg_policy->work_lock);
  558. }
  559. sg_policy->need_freq_update = true;
  560. }
  561. static struct cpufreq_governor schedutil_gov = {
  562. .name = "schedutil",
  563. .owner = THIS_MODULE,
  564. .dynamic_switching = true,
  565. .init = sugov_init,
  566. .exit = sugov_exit,
  567. .start = sugov_start,
  568. .stop = sugov_stop,
  569. .limits = sugov_limits,
  570. };
  571. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  572. struct cpufreq_governor *cpufreq_default_governor(void)
  573. {
  574. return &schedutil_gov;
  575. }
  576. #endif
  577. static int __init sugov_register(void)
  578. {
  579. return cpufreq_register_governor(&schedutil_gov);
  580. }
  581. fs_initcall(sugov_register);