2 * Copyright (C) 2015 Michael Turquette <mturquette@linaro.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/cpufreq.h>
10 #include <linux/module.h>
11 #include <linux/kthread.h>
12 #include <linux/percpu.h>
13 #include <linux/irq_work.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
19 #define THROTTLE_NSEC 50000000 /* 50ms default */
21 struct static_key __read_mostly __sched_freq = STATIC_KEY_INIT_FALSE;
22 static bool __read_mostly cpufreq_driver_slow;
24 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHED
25 static struct cpufreq_governor cpufreq_gov_sched;
28 static DEFINE_PER_CPU(unsigned long, enabled);
29 DEFINE_PER_CPU(struct sched_capacity_reqs, cpu_sched_capacity_reqs);
32 * gov_data - per-policy data internal to the governor
33 * @throttle: next throttling period expiry. Derived from throttle_nsec
34 * @throttle_nsec: throttle period length in nanoseconds
35 * @task: worker thread for dvfs transition that may block/sleep
36 * @irq_work: callback used to wake up worker thread
37 * @requested_freq: last frequency requested by the sched governor
39 * struct gov_data is the per-policy cpufreq_sched-specific data structure. A
40 * per-policy instance of it is created when the cpufreq_sched governor receives
41 * the CPUFREQ_GOV_START condition and a pointer to it exists in the gov_data
42 * member of struct cpufreq_policy.
44 * Readers of this data must call down_read(policy->rwsem). Writers must
45 * call down_write(policy->rwsem).
49 unsigned int throttle_nsec;
50 struct task_struct *task;
51 struct irq_work irq_work;
52 unsigned int requested_freq;
55 static void cpufreq_sched_try_driver_target(struct cpufreq_policy *policy,
58 struct gov_data *gd = policy->governor_data;
60 /* avoid race with cpufreq_sched_stop */
61 if (!down_write_trylock(&policy->rwsem))
64 __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
66 gd->throttle = ktime_add_ns(ktime_get(), gd->throttle_nsec);
67 up_write(&policy->rwsem);
70 static bool finish_last_request(struct gov_data *gd)
72 ktime_t now = ktime_get();
74 if (ktime_after(now, gd->throttle))
78 int usec_left = ktime_to_ns(ktime_sub(gd->throttle, now));
80 usec_left /= NSEC_PER_USEC;
81 usleep_range(usec_left, usec_left + 100);
83 if (ktime_after(now, gd->throttle))
89 * we pass in struct cpufreq_policy. This is safe because changing out the
90 * policy requires a call to __cpufreq_governor(policy, CPUFREQ_GOV_STOP),
91 * which tears down all of the data structures and __cpufreq_governor(policy,
92 * CPUFREQ_GOV_START) will do a full rebuild, including this kthread with the
95 static int cpufreq_sched_thread(void *data)
97 struct sched_param param;
98 struct cpufreq_policy *policy;
100 unsigned int new_request = 0;
101 unsigned int last_request = 0;
104 policy = (struct cpufreq_policy *) data;
105 gd = policy->governor_data;
107 param.sched_priority = 50;
108 ret = sched_setscheduler_nocheck(gd->task, SCHED_FIFO, ¶m);
110 pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
113 pr_debug("%s: kthread (%d) set to SCHED_FIFO\n",
114 __func__, gd->task->pid);
118 set_current_state(TASK_INTERRUPTIBLE);
119 new_request = gd->requested_freq;
120 if (new_request == last_request) {
124 * if the frequency thread sleeps while waiting to be
125 * unthrottled, start over to check for a newer request
127 if (finish_last_request(gd))
129 last_request = new_request;
130 cpufreq_sched_try_driver_target(policy, new_request);
132 } while (!kthread_should_stop());
137 static void cpufreq_sched_irq_work(struct irq_work *irq_work)
141 gd = container_of(irq_work, struct gov_data, irq_work);
145 wake_up_process(gd->task);
148 static void update_fdomain_capacity_request(int cpu)
150 unsigned int freq_new, index_new, cpu_tmp;
151 struct cpufreq_policy *policy;
153 unsigned long capacity = 0;
156 * Avoid grabbing the policy if possible. A test is still
157 * required after locking the CPU's policy to avoid racing
158 * with the governor changing.
160 if (!per_cpu(enabled, cpu))
163 policy = cpufreq_cpu_get(cpu);
164 if (IS_ERR_OR_NULL(policy))
167 if (policy->governor != &cpufreq_gov_sched ||
168 !policy->governor_data)
171 gd = policy->governor_data;
173 /* find max capacity requested by cpus in this policy */
174 for_each_cpu(cpu_tmp, policy->cpus) {
175 struct sched_capacity_reqs *scr;
177 scr = &per_cpu(cpu_sched_capacity_reqs, cpu_tmp);
178 capacity = max(capacity, scr->total);
181 /* Convert the new maximum capacity request into a cpu frequency */
182 freq_new = capacity * policy->max >> SCHED_CAPACITY_SHIFT;
183 if (cpufreq_frequency_table_target(policy, policy->freq_table,
184 freq_new, CPUFREQ_RELATION_L,
187 freq_new = policy->freq_table[index_new].frequency;
189 if (freq_new == gd->requested_freq)
192 gd->requested_freq = freq_new;
195 * Throttling is not yet supported on platforms with fast cpufreq
198 if (cpufreq_driver_slow)
199 irq_work_queue_on(&gd->irq_work, cpu);
201 cpufreq_sched_try_driver_target(policy, freq_new);
204 cpufreq_cpu_put(policy);
207 void update_cpu_capacity_request(int cpu, bool request)
209 unsigned long new_capacity;
210 struct sched_capacity_reqs *scr;
212 /* The rq lock serializes access to the CPU's sched_capacity_reqs. */
213 lockdep_assert_held(&cpu_rq(cpu)->lock);
215 scr = &per_cpu(cpu_sched_capacity_reqs, cpu);
217 new_capacity = scr->cfs + scr->rt;
218 new_capacity = new_capacity * capacity_margin
219 / SCHED_CAPACITY_SCALE;
220 new_capacity += scr->dl;
222 if (new_capacity == scr->total)
225 scr->total = new_capacity;
227 update_fdomain_capacity_request(cpu);
230 static inline void set_sched_freq(void)
232 static_key_slow_inc(&__sched_freq);
235 static inline void clear_sched_freq(void)
237 static_key_slow_dec(&__sched_freq);
240 static int cpufreq_sched_policy_init(struct cpufreq_policy *policy)
245 for_each_cpu(cpu, policy->cpus)
246 memset(&per_cpu(cpu_sched_capacity_reqs, cpu), 0,
247 sizeof(struct sched_capacity_reqs));
249 gd = kzalloc(sizeof(*gd), GFP_KERNEL);
253 gd->throttle_nsec = policy->cpuinfo.transition_latency ?
254 policy->cpuinfo.transition_latency :
256 pr_debug("%s: throttle threshold = %u [ns]\n",
257 __func__, gd->throttle_nsec);
259 if (cpufreq_driver_is_slow()) {
260 cpufreq_driver_slow = true;
261 gd->task = kthread_create(cpufreq_sched_thread, policy,
263 cpumask_first(policy->related_cpus));
264 if (IS_ERR_OR_NULL(gd->task)) {
265 pr_err("%s: failed to create kschedfreq thread\n",
269 get_task_struct(gd->task);
270 kthread_bind_mask(gd->task, policy->related_cpus);
271 wake_up_process(gd->task);
272 init_irq_work(&gd->irq_work, cpufreq_sched_irq_work);
275 policy->governor_data = gd;
285 static int cpufreq_sched_policy_exit(struct cpufreq_policy *policy)
287 struct gov_data *gd = policy->governor_data;
290 if (cpufreq_driver_slow) {
291 kthread_stop(gd->task);
292 put_task_struct(gd->task);
295 policy->governor_data = NULL;
301 static int cpufreq_sched_start(struct cpufreq_policy *policy)
305 for_each_cpu(cpu, policy->cpus)
306 per_cpu(enabled, cpu) = 1;
311 static int cpufreq_sched_stop(struct cpufreq_policy *policy)
315 for_each_cpu(cpu, policy->cpus)
316 per_cpu(enabled, cpu) = 0;
321 static int cpufreq_sched_setup(struct cpufreq_policy *policy,
325 case CPUFREQ_GOV_POLICY_INIT:
326 return cpufreq_sched_policy_init(policy);
327 case CPUFREQ_GOV_POLICY_EXIT:
328 return cpufreq_sched_policy_exit(policy);
329 case CPUFREQ_GOV_START:
330 return cpufreq_sched_start(policy);
331 case CPUFREQ_GOV_STOP:
332 return cpufreq_sched_stop(policy);
333 case CPUFREQ_GOV_LIMITS:
339 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHED
342 struct cpufreq_governor cpufreq_gov_sched = {
344 .governor = cpufreq_sched_setup,
345 .owner = THIS_MODULE,
348 static int __init cpufreq_sched_init(void)
352 for_each_cpu(cpu, cpu_possible_mask)
353 per_cpu(enabled, cpu) = 0;
354 return cpufreq_register_governor(&cpufreq_gov_sched);
357 /* Try to make this the default governor */
358 fs_initcall(cpufreq_sched_init);