cpufreq_schedutil.c 19 KB

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