cpufreq_schedutil.c 19 KB

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