Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/drivers/cpufreq/cpufreq.c | |
4 | * | |
5 | * Copyright (C) 2001 Russell King | |
6 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> | |
bb176f7d | 7 | * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> |
1da177e4 | 8 | * |
c32b6b8e | 9 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
32ee8c3e | 10 | * Added handling for CPU hotplug |
8ff69732 DJ |
11 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
12 | * Fix handling for CPU hotplug -- affected CPUs | |
1da177e4 LT |
13 | */ |
14 | ||
db701151 VK |
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
16 | ||
5ff0a268 | 17 | #include <linux/cpu.h> |
1da177e4 | 18 | #include <linux/cpufreq.h> |
5c238a8b | 19 | #include <linux/cpu_cooling.h> |
1da177e4 | 20 | #include <linux/delay.h> |
1da177e4 | 21 | #include <linux/device.h> |
5ff0a268 VK |
22 | #include <linux/init.h> |
23 | #include <linux/kernel_stat.h> | |
24 | #include <linux/module.h> | |
3fc54d37 | 25 | #include <linux/mutex.h> |
67d874c3 | 26 | #include <linux/pm_qos.h> |
5ff0a268 | 27 | #include <linux/slab.h> |
f994c1cb | 28 | #include <linux/string_choices.h> |
2f0aea93 | 29 | #include <linux/suspend.h> |
90de2a4a | 30 | #include <linux/syscore_ops.h> |
5ff0a268 | 31 | #include <linux/tick.h> |
f55ae08c | 32 | #include <linux/units.h> |
6f4f2723 TR |
33 | #include <trace/events/power.h> |
34 | ||
b4f0676f | 35 | static LIST_HEAD(cpufreq_policy_list); |
f963735a | 36 | |
f963735a | 37 | /* Macros to iterate over CPU policies */ |
fd7dc7e6 EB |
38 | #define for_each_suitable_policy(__policy, __active) \ |
39 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ | |
40 | if ((__active) == !policy_is_inactive(__policy)) | |
f963735a VK |
41 | |
42 | #define for_each_active_policy(__policy) \ | |
43 | for_each_suitable_policy(__policy, true) | |
44 | #define for_each_inactive_policy(__policy) \ | |
45 | for_each_suitable_policy(__policy, false) | |
46 | ||
f7b27061 VK |
47 | /* Iterate over governors */ |
48 | static LIST_HEAD(cpufreq_governor_list); | |
49 | #define for_each_governor(__governor) \ | |
50 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) | |
51 | ||
8412b456 QP |
52 | static char default_governor[CPUFREQ_NAME_LEN]; |
53 | ||
a9909c21 | 54 | /* |
cd878479 | 55 | * The "cpufreq driver" - the arch- or hardware-dependent low |
1da177e4 LT |
56 | * level driver of CPUFreq support, and its spinlock. This lock |
57 | * also protects the cpufreq_cpu_data array. | |
58 | */ | |
1c3d85dd | 59 | static struct cpufreq_driver *cpufreq_driver; |
7a6aedfa | 60 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
bb176f7d | 61 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
bb176f7d | 62 | |
874f6353 IV |
63 | static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance); |
64 | bool cpufreq_supports_freq_invariance(void) | |
65 | { | |
66 | return static_branch_likely(&cpufreq_freq_invariance); | |
67 | } | |
68 | ||
2f0aea93 VK |
69 | /* Flag to suspend/resume CPUFreq governors */ |
70 | static bool cpufreq_suspended; | |
1da177e4 | 71 | |
9c0ebcf7 VK |
72 | static inline bool has_target(void) |
73 | { | |
74 | return cpufreq_driver->target_index || cpufreq_driver->target; | |
75 | } | |
76 | ||
a038895e VK |
77 | bool has_target_index(void) |
78 | { | |
79 | return !!cpufreq_driver->target_index; | |
80 | } | |
81 | ||
1da177e4 | 82 | /* internal prototypes */ |
d92d50a4 | 83 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
a92604b4 RW |
84 | static int cpufreq_init_governor(struct cpufreq_policy *policy); |
85 | static void cpufreq_exit_governor(struct cpufreq_policy *policy); | |
a92604b4 | 86 | static void cpufreq_governor_limits(struct cpufreq_policy *policy); |
1e4f63ae RW |
87 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
88 | struct cpufreq_governor *new_gov, | |
89 | unsigned int new_pol); | |
218a06a7 | 90 | static bool cpufreq_boost_supported(void); |
1f048150 | 91 | static int cpufreq_boost_trigger_state(int state); |
45482c70 | 92 | |
a9909c21 | 93 | /* |
32ee8c3e DJ |
94 | * Two notifier lists: the "policy" list is involved in the |
95 | * validation process for a new CPU frequency policy; the | |
1da177e4 LT |
96 | * "transition" list for kernel code that needs to handle |
97 | * changes to devices when the CPU clock speed changes. | |
98 | * The mutex locks both lists. | |
99 | */ | |
e041c683 | 100 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
cc85de36 | 101 | SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); |
1da177e4 | 102 | |
a7b422cd | 103 | static int off __read_mostly; |
da584455 | 104 | static int cpufreq_disabled(void) |
a7b422cd KRW |
105 | { |
106 | return off; | |
107 | } | |
108 | void disable_cpufreq(void) | |
109 | { | |
110 | off = 1; | |
111 | } | |
29464f28 | 112 | static DEFINE_MUTEX(cpufreq_governor_mutex); |
1da177e4 | 113 | |
4d5dcc42 VK |
114 | bool have_governor_per_policy(void) |
115 | { | |
0b981e70 | 116 | return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); |
4d5dcc42 | 117 | } |
3f869d6d | 118 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
4d5dcc42 | 119 | |
183edb20 YL |
120 | static struct kobject *cpufreq_global_kobject; |
121 | ||
944e9a03 VK |
122 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
123 | { | |
124 | if (have_governor_per_policy()) | |
125 | return &policy->kobj; | |
126 | else | |
127 | return cpufreq_global_kobject; | |
128 | } | |
129 | EXPORT_SYMBOL_GPL(get_governor_parent_kobj); | |
130 | ||
72a4ce34 VK |
131 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
132 | { | |
5720821b | 133 | struct kernel_cpustat kcpustat; |
72a4ce34 | 134 | u64 cur_wall_time; |
5720821b | 135 | u64 idle_time; |
72a4ce34 VK |
136 | u64 busy_time; |
137 | ||
7fb1327e | 138 | cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); |
72a4ce34 | 139 | |
5720821b FW |
140 | kcpustat_cpu_fetch(&kcpustat, cpu); |
141 | ||
142 | busy_time = kcpustat.cpustat[CPUTIME_USER]; | |
143 | busy_time += kcpustat.cpustat[CPUTIME_SYSTEM]; | |
144 | busy_time += kcpustat.cpustat[CPUTIME_IRQ]; | |
145 | busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ]; | |
146 | busy_time += kcpustat.cpustat[CPUTIME_STEAL]; | |
147 | busy_time += kcpustat.cpustat[CPUTIME_NICE]; | |
72a4ce34 VK |
148 | |
149 | idle_time = cur_wall_time - busy_time; | |
150 | if (wall) | |
7fb1327e | 151 | *wall = div_u64(cur_wall_time, NSEC_PER_USEC); |
72a4ce34 | 152 | |
7fb1327e | 153 | return div_u64(idle_time, NSEC_PER_USEC); |
72a4ce34 VK |
154 | } |
155 | ||
156 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) | |
157 | { | |
158 | u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); | |
159 | ||
160 | if (idle_time == -1ULL) | |
161 | return get_cpu_idle_time_jiffy(cpu, wall); | |
162 | else if (!io_busy) | |
163 | idle_time += get_cpu_iowait_time_us(cpu, wall); | |
164 | ||
165 | return idle_time; | |
166 | } | |
167 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); | |
168 | ||
70e9e778 VK |
169 | /* |
170 | * This is a generic cpufreq init() routine which can be used by cpufreq | |
171 | * drivers of SMP systems. It will do following: | |
172 | * - validate & show freq table passed | |
173 | * - set policies transition latency | |
174 | * - policy->cpus with all possible CPUs | |
175 | */ | |
c4dcc8a1 | 176 | void cpufreq_generic_init(struct cpufreq_policy *policy, |
70e9e778 VK |
177 | struct cpufreq_frequency_table *table, |
178 | unsigned int transition_latency) | |
179 | { | |
92c99d15 | 180 | policy->freq_table = table; |
70e9e778 VK |
181 | policy->cpuinfo.transition_latency = transition_latency; |
182 | ||
183 | /* | |
58405af6 | 184 | * The driver only supports the SMP configuration where all processors |
70e9e778 VK |
185 | * share the clock and voltage and clock. |
186 | */ | |
187 | cpumask_setall(policy->cpus); | |
70e9e778 VK |
188 | } |
189 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); | |
190 | ||
1f0bd44e | 191 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
652ed95d VK |
192 | { |
193 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); | |
194 | ||
988bed09 VK |
195 | return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; |
196 | } | |
1f0bd44e | 197 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); |
988bed09 VK |
198 | |
199 | unsigned int cpufreq_generic_get(unsigned int cpu) | |
200 | { | |
201 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); | |
202 | ||
652ed95d | 203 | if (!policy || IS_ERR(policy->clk)) { |
e837f9b5 JP |
204 | pr_err("%s: No %s associated to cpu: %d\n", |
205 | __func__, policy ? "clk" : "policy", cpu); | |
652ed95d VK |
206 | return 0; |
207 | } | |
208 | ||
209 | return clk_get_rate(policy->clk) / 1000; | |
210 | } | |
211 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); | |
212 | ||
50e9c852 | 213 | /** |
5d094fea RW |
214 | * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. |
215 | * @cpu: CPU to find the policy for. | |
50e9c852 | 216 | * |
5d094fea RW |
217 | * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment |
218 | * the kobject reference counter of that policy. Return a valid policy on | |
219 | * success or NULL on failure. | |
50e9c852 | 220 | * |
5d094fea RW |
221 | * The policy returned by this function has to be released with the help of |
222 | * cpufreq_cpu_put() to balance its kobject reference counter properly. | |
50e9c852 | 223 | */ |
6eed9404 | 224 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
1da177e4 | 225 | { |
6eed9404 | 226 | struct cpufreq_policy *policy = NULL; |
1da177e4 LT |
227 | unsigned long flags; |
228 | ||
1b947c90 | 229 | if (WARN_ON(cpu >= nr_cpu_ids)) |
6eed9404 VK |
230 | return NULL; |
231 | ||
1da177e4 | 232 | /* get the cpufreq driver */ |
1c3d85dd | 233 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1da177e4 | 234 | |
6eed9404 VK |
235 | if (cpufreq_driver) { |
236 | /* get the CPU */ | |
988bed09 | 237 | policy = cpufreq_cpu_get_raw(cpu); |
6eed9404 VK |
238 | if (policy) |
239 | kobject_get(&policy->kobj); | |
240 | } | |
1da177e4 | 241 | |
6eed9404 | 242 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 243 | |
3a3e9e06 | 244 | return policy; |
a9144436 | 245 | } |
1da177e4 LT |
246 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
247 | ||
50e9c852 | 248 | /** |
5d094fea RW |
249 | * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. |
250 | * @policy: cpufreq policy returned by cpufreq_cpu_get(). | |
50e9c852 | 251 | */ |
3a3e9e06 | 252 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
1da177e4 | 253 | { |
6eed9404 | 254 | kobject_put(&policy->kobj); |
1da177e4 LT |
255 | } |
256 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); | |
257 | ||
1da177e4 LT |
258 | /********************************************************************* |
259 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * | |
260 | *********************************************************************/ | |
261 | ||
ec06e586 RW |
262 | /** |
263 | * adjust_jiffies - Adjust the system "loops_per_jiffy". | |
264 | * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. | |
265 | * @ci: Frequency change information. | |
1da177e4 LT |
266 | * |
267 | * This function alters the system "loops_per_jiffy" for the clock | |
268 | * speed change. Note that loops_per_jiffy cannot be updated on SMP | |
32ee8c3e | 269 | * systems as each CPU might be scaled differently. So, use the arch |
1da177e4 LT |
270 | * per-CPU loops_per_jiffy value wherever possible. |
271 | */ | |
858119e1 | 272 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
1da177e4 | 273 | { |
39c132ee VK |
274 | #ifndef CONFIG_SMP |
275 | static unsigned long l_p_j_ref; | |
276 | static unsigned int l_p_j_ref_freq; | |
277 | ||
1da177e4 LT |
278 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
279 | return; | |
280 | ||
281 | if (!l_p_j_ref_freq) { | |
282 | l_p_j_ref = loops_per_jiffy; | |
283 | l_p_j_ref_freq = ci->old; | |
e837f9b5 JP |
284 | pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", |
285 | l_p_j_ref, l_p_j_ref_freq); | |
1da177e4 | 286 | } |
0b443ead | 287 | if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { |
e08f5f5b GS |
288 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
289 | ci->new); | |
e837f9b5 JP |
290 | pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", |
291 | loops_per_jiffy, ci->new); | |
1da177e4 | 292 | } |
1da177e4 | 293 | #endif |
39c132ee | 294 | } |
1da177e4 | 295 | |
20b5324d | 296 | /** |
ec06e586 | 297 | * cpufreq_notify_transition - Notify frequency transition and adjust jiffies. |
20b5324d VK |
298 | * @policy: cpufreq policy to enable fast frequency switching for. |
299 | * @freqs: contain details of the frequency update. | |
300 | * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. | |
301 | * | |
ec06e586 RW |
302 | * This function calls the transition notifiers and adjust_jiffies(). |
303 | * | |
304 | * It is called twice on all CPU frequency changes that have external effects. | |
20b5324d VK |
305 | */ |
306 | static void cpufreq_notify_transition(struct cpufreq_policy *policy, | |
307 | struct cpufreq_freqs *freqs, | |
308 | unsigned int state) | |
1da177e4 | 309 | { |
df24014a VK |
310 | int cpu; |
311 | ||
1da177e4 LT |
312 | BUG_ON(irqs_disabled()); |
313 | ||
d5aaffa9 DB |
314 | if (cpufreq_disabled()) |
315 | return; | |
316 | ||
df24014a | 317 | freqs->policy = policy; |
1c3d85dd | 318 | freqs->flags = cpufreq_driver->flags; |
2d06d8c4 | 319 | pr_debug("notification %u of frequency transition to %u kHz\n", |
e837f9b5 | 320 | state, freqs->new); |
1da177e4 | 321 | |
1da177e4 LT |
322 | switch (state) { |
323 | case CPUFREQ_PRECHANGE: | |
20b5324d VK |
324 | /* |
325 | * Detect if the driver reported a value as "old frequency" | |
e4472cb3 DJ |
326 | * which is not equal to what the cpufreq core thinks is |
327 | * "old frequency". | |
1da177e4 | 328 | */ |
98015228 VK |
329 | if (policy->cur && policy->cur != freqs->old) { |
330 | pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", | |
331 | freqs->old, policy->cur); | |
332 | freqs->old = policy->cur; | |
1da177e4 | 333 | } |
20b5324d | 334 | |
df24014a VK |
335 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
336 | CPUFREQ_PRECHANGE, freqs); | |
20b5324d | 337 | |
1da177e4 LT |
338 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
339 | break; | |
e4472cb3 | 340 | |
1da177e4 LT |
341 | case CPUFREQ_POSTCHANGE: |
342 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); | |
20b5324d VK |
343 | pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, |
344 | cpumask_pr_args(policy->cpus)); | |
345 | ||
df24014a VK |
346 | for_each_cpu(cpu, policy->cpus) |
347 | trace_cpu_frequency(freqs->new, cpu); | |
348 | ||
349 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, | |
350 | CPUFREQ_POSTCHANGE, freqs); | |
20b5324d | 351 | |
1aefc75b | 352 | cpufreq_stats_record_transition(policy, freqs->new); |
20b5324d | 353 | policy->cur = freqs->new; |
1da177e4 | 354 | } |
1da177e4 | 355 | } |
bb176f7d | 356 | |
f7ba3b41 | 357 | /* Do post notifications when there are chances that transition has failed */ |
236a9800 | 358 | static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, |
f7ba3b41 VK |
359 | struct cpufreq_freqs *freqs, int transition_failed) |
360 | { | |
361 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
362 | if (!transition_failed) | |
363 | return; | |
364 | ||
365 | swap(freqs->old, freqs->new); | |
366 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
367 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
368 | } | |
f7ba3b41 | 369 | |
12478cf0 SB |
370 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
371 | struct cpufreq_freqs *freqs) | |
372 | { | |
ca654dc3 SB |
373 | |
374 | /* | |
375 | * Catch double invocations of _begin() which lead to self-deadlock. | |
376 | * ASYNC_NOTIFICATION drivers are left out because the cpufreq core | |
377 | * doesn't invoke _begin() on their behalf, and hence the chances of | |
378 | * double invocations are very low. Moreover, there are scenarios | |
379 | * where these checks can emit false-positive warnings in these | |
380 | * drivers; so we avoid that by skipping them altogether. | |
381 | */ | |
382 | WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) | |
383 | && current == policy->transition_task); | |
384 | ||
12478cf0 SB |
385 | wait: |
386 | wait_event(policy->transition_wait, !policy->transition_ongoing); | |
387 | ||
388 | spin_lock(&policy->transition_lock); | |
389 | ||
390 | if (unlikely(policy->transition_ongoing)) { | |
391 | spin_unlock(&policy->transition_lock); | |
392 | goto wait; | |
393 | } | |
394 | ||
395 | policy->transition_ongoing = true; | |
ca654dc3 | 396 | policy->transition_task = current; |
12478cf0 SB |
397 | |
398 | spin_unlock(&policy->transition_lock); | |
399 | ||
400 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
401 | } | |
402 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); | |
403 | ||
404 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, | |
405 | struct cpufreq_freqs *freqs, int transition_failed) | |
406 | { | |
0e7ea2f3 | 407 | if (WARN_ON(!policy->transition_ongoing)) |
12478cf0 SB |
408 | return; |
409 | ||
410 | cpufreq_notify_post_transition(policy, freqs, transition_failed); | |
411 | ||
1a0419b0 IV |
412 | arch_set_freq_scale(policy->related_cpus, |
413 | policy->cur, | |
599457ba | 414 | arch_scale_freq_ref(policy->cpu)); |
1a0419b0 | 415 | |
61bfbf79 | 416 | spin_lock(&policy->transition_lock); |
12478cf0 | 417 | policy->transition_ongoing = false; |
ca654dc3 | 418 | policy->transition_task = NULL; |
61bfbf79 | 419 | spin_unlock(&policy->transition_lock); |
12478cf0 SB |
420 | |
421 | wake_up(&policy->transition_wait); | |
422 | } | |
423 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); | |
424 | ||
b7898fda RW |
425 | /* |
426 | * Fast frequency switching status count. Positive means "enabled", negative | |
427 | * means "disabled" and 0 means "not decided yet". | |
428 | */ | |
429 | static int cpufreq_fast_switch_count; | |
430 | static DEFINE_MUTEX(cpufreq_fast_switch_lock); | |
431 | ||
432 | static void cpufreq_list_transition_notifiers(void) | |
433 | { | |
434 | struct notifier_block *nb; | |
435 | ||
436 | pr_info("Registered transition notifiers:\n"); | |
437 | ||
438 | mutex_lock(&cpufreq_transition_notifier_list.mutex); | |
439 | ||
440 | for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) | |
d75f773c | 441 | pr_info("%pS\n", nb->notifier_call); |
b7898fda RW |
442 | |
443 | mutex_unlock(&cpufreq_transition_notifier_list.mutex); | |
444 | } | |
445 | ||
446 | /** | |
447 | * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. | |
448 | * @policy: cpufreq policy to enable fast frequency switching for. | |
449 | * | |
450 | * Try to enable fast frequency switching for @policy. | |
451 | * | |
452 | * The attempt will fail if there is at least one transition notifier registered | |
453 | * at this point, as fast frequency switching is quite fundamentally at odds | |
454 | * with transition notifiers. Thus if successful, it will make registration of | |
455 | * transition notifiers fail going forward. | |
456 | */ | |
457 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) | |
458 | { | |
459 | lockdep_assert_held(&policy->rwsem); | |
460 | ||
461 | if (!policy->fast_switch_possible) | |
462 | return; | |
463 | ||
464 | mutex_lock(&cpufreq_fast_switch_lock); | |
465 | if (cpufreq_fast_switch_count >= 0) { | |
466 | cpufreq_fast_switch_count++; | |
467 | policy->fast_switch_enabled = true; | |
468 | } else { | |
469 | pr_warn("CPU%u: Fast frequency switching not enabled\n", | |
470 | policy->cpu); | |
471 | cpufreq_list_transition_notifiers(); | |
472 | } | |
473 | mutex_unlock(&cpufreq_fast_switch_lock); | |
474 | } | |
475 | EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); | |
476 | ||
6c9d9c81 RW |
477 | /** |
478 | * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. | |
479 | * @policy: cpufreq policy to disable fast frequency switching for. | |
480 | */ | |
481 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) | |
b7898fda RW |
482 | { |
483 | mutex_lock(&cpufreq_fast_switch_lock); | |
484 | if (policy->fast_switch_enabled) { | |
485 | policy->fast_switch_enabled = false; | |
486 | if (!WARN_ON(cpufreq_fast_switch_count <= 0)) | |
487 | cpufreq_fast_switch_count--; | |
488 | } | |
489 | mutex_unlock(&cpufreq_fast_switch_lock); | |
490 | } | |
6c9d9c81 | 491 | EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); |
1da177e4 | 492 | |
f9ccdec2 | 493 | static unsigned int __resolve_freq(struct cpufreq_policy *policy, |
b7902803 RW |
494 | unsigned int target_freq, |
495 | unsigned int min, unsigned int max, | |
496 | unsigned int relation) | |
e3c06236 | 497 | { |
b3beca76 | 498 | unsigned int idx; |
abe8bd02 | 499 | |
b7902803 RW |
500 | target_freq = clamp_val(target_freq, min, max); |
501 | ||
6ca7076f | 502 | if (!policy->freq_table) |
b3beca76 | 503 | return target_freq; |
abe8bd02 | 504 | |
b7902803 | 505 | idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation); |
b3beca76 VK |
506 | policy->cached_resolved_idx = idx; |
507 | policy->cached_target_freq = target_freq; | |
508 | return policy->freq_table[idx].frequency; | |
e3c06236 | 509 | } |
f9ccdec2 VK |
510 | |
511 | /** | |
512 | * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported | |
513 | * one. | |
514 | * @policy: associated policy to interrogate | |
515 | * @target_freq: target frequency to resolve. | |
516 | * | |
517 | * The target to driver frequency mapping is cached in the policy. | |
518 | * | |
519 | * Return: Lowest driver-supported frequency greater than or equal to the | |
520 | * given target_freq, subject to policy (min/max) and driver limitations. | |
521 | */ | |
522 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, | |
523 | unsigned int target_freq) | |
524 | { | |
7491cdf4 RW |
525 | unsigned int min = READ_ONCE(policy->min); |
526 | unsigned int max = READ_ONCE(policy->max); | |
527 | ||
528 | /* | |
529 | * If this function runs in parallel with cpufreq_set_policy(), it may | |
530 | * read policy->min before the update and policy->max after the update | |
531 | * or the other way around, so there is no ordering guarantee. | |
532 | * | |
533 | * Resolve this by always honoring the max (in case it comes from | |
534 | * thermal throttling or similar). | |
535 | */ | |
536 | if (unlikely(min > max)) | |
537 | min = max; | |
538 | ||
b7902803 | 539 | return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE); |
f9ccdec2 | 540 | } |
ae2c1ca6 | 541 | EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); |
e3c06236 | 542 | |
aa7519af VK |
543 | unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) |
544 | { | |
545 | unsigned int latency; | |
546 | ||
547 | if (policy->transition_delay_us) | |
548 | return policy->transition_delay_us; | |
549 | ||
550 | latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; | |
37c6dccd QY |
551 | if (latency) |
552 | /* Give a 50% breathing room between updates */ | |
553 | return latency + (latency >> 1); | |
a755d0e2 | 554 | |
37c6dccd | 555 | return USEC_PER_MSEC; |
aa7519af VK |
556 | } |
557 | EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); | |
558 | ||
1da177e4 LT |
559 | /********************************************************************* |
560 | * SYSFS INTERFACE * | |
561 | *********************************************************************/ | |
8a5c74a1 | 562 | static ssize_t show_boost(struct kobject *kobj, |
625c85a6 | 563 | struct kobj_attribute *attr, char *buf) |
6f19efc0 | 564 | { |
5e62d53c | 565 | return sysfs_emit(buf, "%d\n", cpufreq_driver->boost_enabled); |
6f19efc0 LM |
566 | } |
567 | ||
625c85a6 VK |
568 | static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, |
569 | const char *buf, size_t count) | |
6f19efc0 | 570 | { |
2240d3e6 | 571 | bool enable; |
6f19efc0 | 572 | |
2240d3e6 | 573 | if (kstrtobool(buf, &enable)) |
6f19efc0 LM |
574 | return -EINVAL; |
575 | ||
576 | if (cpufreq_boost_trigger_state(enable)) { | |
e837f9b5 | 577 | pr_err("%s: Cannot %s BOOST!\n", |
f994c1cb | 578 | __func__, str_enable_disable(enable)); |
6f19efc0 LM |
579 | return -EINVAL; |
580 | } | |
581 | ||
e837f9b5 | 582 | pr_debug("%s: cpufreq BOOST %s\n", |
f994c1cb | 583 | __func__, str_enabled_disabled(enable)); |
6f19efc0 LM |
584 | |
585 | return count; | |
586 | } | |
587 | define_one_global_rw(boost); | |
1da177e4 | 588 | |
218a06a7 JZ |
589 | static ssize_t show_local_boost(struct cpufreq_policy *policy, char *buf) |
590 | { | |
591 | return sysfs_emit(buf, "%d\n", policy->boost_enabled); | |
592 | } | |
593 | ||
27241c8b VK |
594 | static int policy_set_boost(struct cpufreq_policy *policy, bool enable) |
595 | { | |
596 | int ret; | |
597 | ||
598 | if (policy->boost_enabled == enable) | |
599 | return 0; | |
600 | ||
601 | policy->boost_enabled = enable; | |
602 | ||
603 | ret = cpufreq_driver->set_boost(policy, enable); | |
604 | if (ret) | |
605 | policy->boost_enabled = !policy->boost_enabled; | |
606 | ||
607 | return ret; | |
608 | } | |
609 | ||
218a06a7 JZ |
610 | static ssize_t store_local_boost(struct cpufreq_policy *policy, |
611 | const char *buf, size_t count) | |
612 | { | |
2240d3e6 PY |
613 | int ret; |
614 | bool enable; | |
218a06a7 | 615 | |
2240d3e6 | 616 | if (kstrtobool(buf, &enable)) |
218a06a7 JZ |
617 | return -EINVAL; |
618 | ||
619 | if (!cpufreq_driver->boost_enabled) | |
620 | return -EINVAL; | |
621 | ||
691b3212 VK |
622 | if (!policy->boost_supported) |
623 | return -EINVAL; | |
624 | ||
27241c8b | 625 | ret = policy_set_boost(policy, enable); |
27241c8b VK |
626 | if (!ret) |
627 | return count; | |
218a06a7 | 628 | |
27241c8b | 629 | return ret; |
218a06a7 JZ |
630 | } |
631 | ||
632 | static struct freq_attr local_boost = __ATTR(boost, 0644, show_local_boost, store_local_boost); | |
633 | ||
42f91fa1 | 634 | static struct cpufreq_governor *find_governor(const char *str_governor) |
3bcb09a3 JF |
635 | { |
636 | struct cpufreq_governor *t; | |
637 | ||
f7b27061 | 638 | for_each_governor(t) |
7c4f4539 | 639 | if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) |
3bcb09a3 JF |
640 | return t; |
641 | ||
642 | return NULL; | |
643 | } | |
644 | ||
8cc46ae5 VK |
645 | static struct cpufreq_governor *get_governor(const char *str_governor) |
646 | { | |
647 | struct cpufreq_governor *t; | |
648 | ||
649 | mutex_lock(&cpufreq_governor_mutex); | |
650 | t = find_governor(str_governor); | |
651 | if (!t) | |
652 | goto unlock; | |
653 | ||
654 | if (!try_module_get(t->owner)) | |
655 | t = NULL; | |
656 | ||
657 | unlock: | |
658 | mutex_unlock(&cpufreq_governor_mutex); | |
659 | ||
660 | return t; | |
661 | } | |
662 | ||
1e4f63ae | 663 | static unsigned int cpufreq_parse_policy(char *str_governor) |
ab05d97a | 664 | { |
1e4f63ae RW |
665 | if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) |
666 | return CPUFREQ_POLICY_PERFORMANCE; | |
667 | ||
668 | if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) | |
669 | return CPUFREQ_POLICY_POWERSAVE; | |
670 | ||
671 | return CPUFREQ_POLICY_UNKNOWN; | |
ab05d97a YH |
672 | } |
673 | ||
1da177e4 | 674 | /** |
5ddc6d4e | 675 | * cpufreq_parse_governor - parse a governor string only for has_target() |
1e4f63ae | 676 | * @str_governor: Governor name. |
1da177e4 | 677 | */ |
1e4f63ae | 678 | static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor) |
1da177e4 | 679 | { |
ab05d97a | 680 | struct cpufreq_governor *t; |
045149e6 | 681 | |
8cc46ae5 VK |
682 | t = get_governor(str_governor); |
683 | if (t) | |
684 | return t; | |
ea714970 | 685 | |
8cc46ae5 VK |
686 | if (request_module("cpufreq_%s", str_governor)) |
687 | return NULL; | |
045149e6 | 688 | |
8cc46ae5 | 689 | return get_governor(str_governor); |
1da177e4 | 690 | } |
1da177e4 | 691 | |
a9909c21 | 692 | /* |
e08f5f5b GS |
693 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
694 | * print out cpufreq information | |
1da177e4 LT |
695 | * |
696 | * Write out information from cpufreq_driver->policy[cpu]; object must be | |
697 | * "unsigned int". | |
698 | */ | |
699 | ||
32ee8c3e DJ |
700 | #define show_one(file_name, object) \ |
701 | static ssize_t show_##file_name \ | |
905d77cd | 702 | (struct cpufreq_policy *policy, char *buf) \ |
32ee8c3e | 703 | { \ |
5e62d53c | 704 | return sysfs_emit(buf, "%u\n", policy->object); \ |
1da177e4 LT |
705 | } |
706 | ||
707 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); | |
708 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); | |
ed129784 | 709 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
1da177e4 LT |
710 | show_one(scaling_min_freq, min); |
711 | show_one(scaling_max_freq, max); | |
c034b02e | 712 | |
38e480d4 | 713 | __weak int arch_freq_get_on_cpu(int cpu) |
f8475cef | 714 | { |
38e480d4 | 715 | return -EOPNOTSUPP; |
f8475cef LB |
716 | } |
717 | ||
fbb4a475 BM |
718 | static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *policy) |
719 | { | |
720 | return arch_freq_get_on_cpu(policy->cpu) != -EOPNOTSUPP; | |
721 | } | |
722 | ||
09347b29 | 723 | static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) |
c034b02e DB |
724 | { |
725 | ssize_t ret; | |
38e480d4 | 726 | int freq; |
c034b02e | 727 | |
fbb4a475 BM |
728 | freq = IS_ENABLED(CONFIG_CPUFREQ_ARCH_CUR_FREQ) |
729 | ? arch_freq_get_on_cpu(policy->cpu) | |
730 | : 0; | |
731 | ||
38e480d4 | 732 | if (freq > 0) |
5e62d53c | 733 | ret = sysfs_emit(buf, "%u\n", freq); |
681fe684 | 734 | else if (cpufreq_driver->setpolicy && cpufreq_driver->get) |
5e62d53c | 735 | ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
c034b02e | 736 | else |
5e62d53c | 737 | ret = sysfs_emit(buf, "%u\n", policy->cur); |
c034b02e DB |
738 | return ret; |
739 | } | |
1da177e4 | 740 | |
a9909c21 | 741 | /* |
1da177e4 LT |
742 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access |
743 | */ | |
744 | #define store_one(file_name, object) \ | |
745 | static ssize_t store_##file_name \ | |
905d77cd | 746 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
1da177e4 | 747 | { \ |
18c49926 VK |
748 | unsigned long val; \ |
749 | int ret; \ | |
1da177e4 | 750 | \ |
877d5cd2 | 751 | ret = kstrtoul(buf, 0, &val); \ |
752 | if (ret) \ | |
753 | return ret; \ | |
1da177e4 | 754 | \ |
3000ce3c | 755 | ret = freq_qos_update_request(policy->object##_freq_req, val);\ |
18c49926 | 756 | return ret >= 0 ? count : ret; \ |
1da177e4 LT |
757 | } |
758 | ||
29464f28 DJ |
759 | store_one(scaling_min_freq, min); |
760 | store_one(scaling_max_freq, max); | |
1da177e4 | 761 | |
a9909c21 | 762 | /* |
1da177e4 LT |
763 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware |
764 | */ | |
905d77cd DJ |
765 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
766 | char *buf) | |
1da177e4 | 767 | { |
d92d50a4 | 768 | unsigned int cur_freq = __cpufreq_get(policy); |
9b4f603e RW |
769 | |
770 | if (cur_freq) | |
5e62d53c | 771 | return sysfs_emit(buf, "%u\n", cur_freq); |
9b4f603e | 772 | |
5e62d53c | 773 | return sysfs_emit(buf, "<unknown>\n"); |
1da177e4 LT |
774 | } |
775 | ||
fbb4a475 BM |
776 | /* |
777 | * show_cpuinfo_avg_freq - average CPU frequency as detected by hardware | |
778 | */ | |
779 | static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy, | |
780 | char *buf) | |
781 | { | |
782 | int avg_freq = arch_freq_get_on_cpu(policy->cpu); | |
783 | ||
784 | if (avg_freq > 0) | |
785 | return sysfs_emit(buf, "%u\n", avg_freq); | |
786 | return avg_freq != 0 ? avg_freq : -EINVAL; | |
787 | } | |
788 | ||
a9909c21 | 789 | /* |
1da177e4 LT |
790 | * show_scaling_governor - show the current policy for the specified CPU |
791 | */ | |
905d77cd | 792 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 793 | { |
29464f28 | 794 | if (policy->policy == CPUFREQ_POLICY_POWERSAVE) |
5e62d53c | 795 | return sysfs_emit(buf, "powersave\n"); |
1da177e4 | 796 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) |
5e62d53c | 797 | return sysfs_emit(buf, "performance\n"); |
1da177e4 | 798 | else if (policy->governor) |
5e62d53c | 799 | return sysfs_emit(buf, "%s\n", policy->governor->name); |
1da177e4 LT |
800 | return -EINVAL; |
801 | } | |
802 | ||
a9909c21 | 803 | /* |
1da177e4 LT |
804 | * store_scaling_governor - store policy for the specified CPU |
805 | */ | |
905d77cd DJ |
806 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
807 | const char *buf, size_t count) | |
1da177e4 | 808 | { |
9c5075fc | 809 | char str_governor[CPUFREQ_NAME_LEN]; |
5136fa56 | 810 | int ret; |
1da177e4 | 811 | |
29464f28 | 812 | ret = sscanf(buf, "%15s", str_governor); |
1da177e4 LT |
813 | if (ret != 1) |
814 | return -EINVAL; | |
815 | ||
ab05d97a | 816 | if (cpufreq_driver->setpolicy) { |
1e4f63ae RW |
817 | unsigned int new_pol; |
818 | ||
819 | new_pol = cpufreq_parse_policy(str_governor); | |
820 | if (!new_pol) | |
ab05d97a | 821 | return -EINVAL; |
1e4f63ae RW |
822 | |
823 | ret = cpufreq_set_policy(policy, NULL, new_pol); | |
ab05d97a | 824 | } else { |
1e4f63ae RW |
825 | struct cpufreq_governor *new_gov; |
826 | ||
827 | new_gov = cpufreq_parse_governor(str_governor); | |
828 | if (!new_gov) | |
ab05d97a | 829 | return -EINVAL; |
1da177e4 | 830 | |
1e4f63ae RW |
831 | ret = cpufreq_set_policy(policy, new_gov, |
832 | CPUFREQ_POLICY_UNKNOWN); | |
a8b149d3 | 833 | |
1e4f63ae RW |
834 | module_put(new_gov->owner); |
835 | } | |
a8b149d3 | 836 | |
88dc4384 | 837 | return ret ? ret : count; |
1da177e4 LT |
838 | } |
839 | ||
a9909c21 | 840 | /* |
1da177e4 LT |
841 | * show_scaling_driver - show the cpufreq driver currently loaded |
842 | */ | |
905d77cd | 843 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 844 | { |
1c3d85dd | 845 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
1da177e4 LT |
846 | } |
847 | ||
a9909c21 | 848 | /* |
1da177e4 LT |
849 | * show_scaling_available_governors - show the available CPUfreq governors |
850 | */ | |
905d77cd DJ |
851 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
852 | char *buf) | |
1da177e4 LT |
853 | { |
854 | ssize_t i = 0; | |
855 | struct cpufreq_governor *t; | |
856 | ||
9c0ebcf7 | 857 | if (!has_target()) { |
5e62d53c | 858 | i += sysfs_emit(buf, "performance powersave"); |
1da177e4 LT |
859 | goto out; |
860 | } | |
861 | ||
8cc46ae5 | 862 | mutex_lock(&cpufreq_governor_mutex); |
f7b27061 | 863 | for_each_governor(t) { |
29464f28 DJ |
864 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
865 | - (CPUFREQ_NAME_LEN + 2))) | |
8cc46ae5 | 866 | break; |
5e62d53c | 867 | i += sysfs_emit_at(buf, i, "%s ", t->name); |
1da177e4 | 868 | } |
8cc46ae5 | 869 | mutex_unlock(&cpufreq_governor_mutex); |
7d5e350f | 870 | out: |
5e62d53c | 871 | i += sysfs_emit_at(buf, i, "\n"); |
1da177e4 LT |
872 | return i; |
873 | } | |
e8628dd0 | 874 | |
f4fd3797 | 875 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) |
1da177e4 LT |
876 | { |
877 | ssize_t i = 0; | |
878 | unsigned int cpu; | |
879 | ||
835481d9 | 880 | for_each_cpu(cpu, mask) { |
5e62d53c | 881 | i += sysfs_emit_at(buf, i, "%u ", cpu); |
1da177e4 | 882 | if (i >= (PAGE_SIZE - 5)) |
29464f28 | 883 | break; |
1da177e4 | 884 | } |
336e5128 VK |
885 | |
886 | /* Remove the extra space at the end */ | |
887 | i--; | |
888 | ||
5e62d53c | 889 | i += sysfs_emit_at(buf, i, "\n"); |
1da177e4 LT |
890 | return i; |
891 | } | |
f4fd3797 | 892 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
1da177e4 | 893 | |
a9909c21 | 894 | /* |
e8628dd0 DW |
895 | * show_related_cpus - show the CPUs affected by each transition even if |
896 | * hw coordination is in use | |
897 | */ | |
898 | static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) | |
899 | { | |
f4fd3797 | 900 | return cpufreq_show_cpus(policy->related_cpus, buf); |
e8628dd0 DW |
901 | } |
902 | ||
a9909c21 | 903 | /* |
e8628dd0 DW |
904 | * show_affected_cpus - show the CPUs affected by each transition |
905 | */ | |
906 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) | |
907 | { | |
f4fd3797 | 908 | return cpufreq_show_cpus(policy->cpus, buf); |
e8628dd0 DW |
909 | } |
910 | ||
9e76988e | 911 | static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, |
905d77cd | 912 | const char *buf, size_t count) |
9e76988e VP |
913 | { |
914 | unsigned int freq = 0; | |
915 | unsigned int ret; | |
916 | ||
879000f9 | 917 | if (!policy->governor || !policy->governor->store_setspeed) |
9e76988e VP |
918 | return -EINVAL; |
919 | ||
1da98dc5 BY |
920 | ret = kstrtouint(buf, 0, &freq); |
921 | if (ret) | |
922 | return ret; | |
9e76988e VP |
923 | |
924 | policy->governor->store_setspeed(policy, freq); | |
925 | ||
926 | return count; | |
927 | } | |
928 | ||
929 | static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) | |
930 | { | |
879000f9 | 931 | if (!policy->governor || !policy->governor->show_setspeed) |
5e62d53c | 932 | return sysfs_emit(buf, "<unsupported>\n"); |
9e76988e VP |
933 | |
934 | return policy->governor->show_setspeed(policy, buf); | |
935 | } | |
1da177e4 | 936 | |
a9909c21 | 937 | /* |
8bf1ac72 | 938 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
e2f74f35 TR |
939 | */ |
940 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) | |
941 | { | |
942 | unsigned int limit; | |
943 | int ret; | |
b23aa311 YH |
944 | ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
945 | if (!ret) | |
5e62d53c PY |
946 | return sysfs_emit(buf, "%u\n", limit); |
947 | return sysfs_emit(buf, "%u\n", policy->cpuinfo.max_freq); | |
e2f74f35 TR |
948 | } |
949 | ||
6dad2a29 | 950 | cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); |
fbb4a475 | 951 | cpufreq_freq_attr_ro(cpuinfo_avg_freq); |
6dad2a29 BP |
952 | cpufreq_freq_attr_ro(cpuinfo_min_freq); |
953 | cpufreq_freq_attr_ro(cpuinfo_max_freq); | |
954 | cpufreq_freq_attr_ro(cpuinfo_transition_latency); | |
955 | cpufreq_freq_attr_ro(scaling_available_governors); | |
956 | cpufreq_freq_attr_ro(scaling_driver); | |
957 | cpufreq_freq_attr_ro(scaling_cur_freq); | |
958 | cpufreq_freq_attr_ro(bios_limit); | |
959 | cpufreq_freq_attr_ro(related_cpus); | |
960 | cpufreq_freq_attr_ro(affected_cpus); | |
961 | cpufreq_freq_attr_rw(scaling_min_freq); | |
962 | cpufreq_freq_attr_rw(scaling_max_freq); | |
963 | cpufreq_freq_attr_rw(scaling_governor); | |
964 | cpufreq_freq_attr_rw(scaling_setspeed); | |
1da177e4 | 965 | |
fe262d5c | 966 | static struct attribute *cpufreq_attrs[] = { |
1da177e4 LT |
967 | &cpuinfo_min_freq.attr, |
968 | &cpuinfo_max_freq.attr, | |
ed129784 | 969 | &cpuinfo_transition_latency.attr, |
1da177e4 LT |
970 | &scaling_min_freq.attr, |
971 | &scaling_max_freq.attr, | |
972 | &affected_cpus.attr, | |
e8628dd0 | 973 | &related_cpus.attr, |
1da177e4 LT |
974 | &scaling_governor.attr, |
975 | &scaling_driver.attr, | |
976 | &scaling_available_governors.attr, | |
9e76988e | 977 | &scaling_setspeed.attr, |
1da177e4 LT |
978 | NULL |
979 | }; | |
fe262d5c | 980 | ATTRIBUTE_GROUPS(cpufreq); |
1da177e4 | 981 | |
29464f28 DJ |
982 | #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) |
983 | #define to_attr(a) container_of(a, struct freq_attr, attr) | |
1da177e4 | 984 | |
29464f28 | 985 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
1da177e4 | 986 | { |
905d77cd DJ |
987 | struct cpufreq_policy *policy = to_policy(kobj); |
988 | struct freq_attr *fattr = to_attr(attr); | |
6eed9404 | 989 | |
e6e8df07 KS |
990 | if (!fattr->show) |
991 | return -EIO; | |
992 | ||
6fec833b RW |
993 | guard(cpufreq_policy_read)(policy); |
994 | ||
d4627a28 | 995 | if (likely(!policy_is_inactive(policy))) |
6fec833b | 996 | return fattr->show(policy, buf); |
1b750e3b | 997 | |
6fec833b | 998 | return -EBUSY; |
1da177e4 LT |
999 | } |
1000 | ||
905d77cd DJ |
1001 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
1002 | const char *buf, size_t count) | |
1da177e4 | 1003 | { |
905d77cd DJ |
1004 | struct cpufreq_policy *policy = to_policy(kobj); |
1005 | struct freq_attr *fattr = to_attr(attr); | |
6eed9404 | 1006 | |
e6e8df07 KS |
1007 | if (!fattr->store) |
1008 | return -EIO; | |
1009 | ||
6fec833b RW |
1010 | guard(cpufreq_policy_write)(policy); |
1011 | ||
9ab9b9d3 | 1012 | if (likely(!policy_is_inactive(policy))) |
6fec833b | 1013 | return fattr->store(policy, buf, count); |
4f750c93 | 1014 | |
6fec833b | 1015 | return -EBUSY; |
1da177e4 LT |
1016 | } |
1017 | ||
905d77cd | 1018 | static void cpufreq_sysfs_release(struct kobject *kobj) |
1da177e4 | 1019 | { |
905d77cd | 1020 | struct cpufreq_policy *policy = to_policy(kobj); |
2d06d8c4 | 1021 | pr_debug("last reference is dropped\n"); |
1da177e4 LT |
1022 | complete(&policy->kobj_unregister); |
1023 | } | |
1024 | ||
52cf25d0 | 1025 | static const struct sysfs_ops sysfs_ops = { |
1da177e4 LT |
1026 | .show = show, |
1027 | .store = store, | |
1028 | }; | |
1029 | ||
108fcad9 | 1030 | static const struct kobj_type ktype_cpufreq = { |
1da177e4 | 1031 | .sysfs_ops = &sysfs_ops, |
fe262d5c | 1032 | .default_groups = cpufreq_groups, |
1da177e4 LT |
1033 | .release = cpufreq_sysfs_release, |
1034 | }; | |
1035 | ||
2c1b5a84 XW |
1036 | static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu, |
1037 | struct device *dev) | |
87549141 | 1038 | { |
67d874c3 | 1039 | if (unlikely(!dev)) |
2f0ba790 RW |
1040 | return; |
1041 | ||
1042 | if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) | |
1043 | return; | |
1044 | ||
26619804 | 1045 | dev_dbg(dev, "%s: Adding symlink\n", __func__); |
2f0ba790 RW |
1046 | if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) |
1047 | dev_err(dev, "cpufreq symlink creation failed\n"); | |
87549141 VK |
1048 | } |
1049 | ||
5c84c1b8 | 1050 | static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu, |
26619804 | 1051 | struct device *dev) |
87549141 | 1052 | { |
26619804 VK |
1053 | dev_dbg(dev, "%s: Removing symlink\n", __func__); |
1054 | sysfs_remove_link(&dev->kobj, "cpufreq"); | |
5c84c1b8 | 1055 | cpumask_clear_cpu(cpu, policy->real_cpus); |
87549141 VK |
1056 | } |
1057 | ||
d9612a49 | 1058 | static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) |
909a694e DJ |
1059 | { |
1060 | struct freq_attr **drv_attr; | |
909a694e | 1061 | int ret = 0; |
909a694e | 1062 | |
dc47f23f VK |
1063 | /* Attributes that need freq_table */ |
1064 | if (policy->freq_table) { | |
1065 | ret = sysfs_create_file(&policy->kobj, | |
1066 | &cpufreq_freq_attr_scaling_available_freqs.attr); | |
1067 | if (ret) | |
1068 | return ret; | |
1069 | ||
1070 | if (cpufreq_boost_supported()) { | |
1071 | ret = sysfs_create_file(&policy->kobj, | |
1072 | &cpufreq_freq_attr_scaling_boost_freqs.attr); | |
1073 | if (ret) | |
1074 | return ret; | |
1075 | } | |
1076 | } | |
1077 | ||
909a694e | 1078 | /* set up files for this cpu device */ |
1c3d85dd | 1079 | drv_attr = cpufreq_driver->attr; |
f13f1184 | 1080 | while (drv_attr && *drv_attr) { |
909a694e DJ |
1081 | ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
1082 | if (ret) | |
6d4e81ed | 1083 | return ret; |
909a694e DJ |
1084 | drv_attr++; |
1085 | } | |
1c3d85dd | 1086 | if (cpufreq_driver->get) { |
909a694e DJ |
1087 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
1088 | if (ret) | |
6d4e81ed | 1089 | return ret; |
909a694e | 1090 | } |
c034b02e | 1091 | |
fbb4a475 BM |
1092 | if (cpufreq_avg_freq_supported(policy)) { |
1093 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_avg_freq.attr); | |
1094 | if (ret) | |
1095 | return ret; | |
1096 | } | |
1097 | ||
c034b02e DB |
1098 | ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); |
1099 | if (ret) | |
6d4e81ed | 1100 | return ret; |
c034b02e | 1101 | |
1c3d85dd | 1102 | if (cpufreq_driver->bios_limit) { |
e2f74f35 TR |
1103 | ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); |
1104 | if (ret) | |
6d4e81ed | 1105 | return ret; |
e2f74f35 | 1106 | } |
909a694e | 1107 | |
218a06a7 JZ |
1108 | if (cpufreq_boost_supported()) { |
1109 | ret = sysfs_create_file(&policy->kobj, &local_boost.attr); | |
1110 | if (ret) | |
1111 | return ret; | |
1112 | } | |
1113 | ||
26619804 | 1114 | return 0; |
e18f1682 SB |
1115 | } |
1116 | ||
7f0fa40f | 1117 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
e18f1682 | 1118 | { |
1e4f63ae RW |
1119 | struct cpufreq_governor *gov = NULL; |
1120 | unsigned int pol = CPUFREQ_POLICY_UNKNOWN; | |
8cc46ae5 | 1121 | int ret; |
ab05d97a YH |
1122 | |
1123 | if (has_target()) { | |
1e4f63ae | 1124 | /* Update policy governor to the one used before hotplug. */ |
8cc46ae5 | 1125 | gov = get_governor(policy->last_governor); |
ab05d97a YH |
1126 | if (gov) { |
1127 | pr_debug("Restoring governor %s for cpu %d\n", | |
8412b456 | 1128 | gov->name, policy->cpu); |
1e4f63ae | 1129 | } else { |
8412b456 QP |
1130 | gov = get_governor(default_governor); |
1131 | } | |
1132 | ||
1133 | if (!gov) { | |
1134 | gov = cpufreq_default_governor(); | |
8412b456 | 1135 | __module_get(gov->owner); |
ab05d97a | 1136 | } |
8412b456 | 1137 | |
de1df26b | 1138 | } else { |
8412b456 | 1139 | |
ab05d97a YH |
1140 | /* Use the default policy if there is no last_policy. */ |
1141 | if (policy->last_policy) { | |
1e4f63ae | 1142 | pol = policy->last_policy; |
8412b456 QP |
1143 | } else { |
1144 | pol = cpufreq_parse_policy(default_governor); | |
f5739cb0 | 1145 | /* |
8412b456 | 1146 | * In case the default governor is neither "performance" |
f5739cb0 RW |
1147 | * nor "powersave", fall back to the initial policy |
1148 | * value set by the driver. | |
1149 | */ | |
1150 | if (pol == CPUFREQ_POLICY_UNKNOWN) | |
1151 | pol = policy->policy; | |
ab05d97a | 1152 | } |
f5739cb0 RW |
1153 | if (pol != CPUFREQ_POLICY_PERFORMANCE && |
1154 | pol != CPUFREQ_POLICY_POWERSAVE) | |
1155 | return -ENODATA; | |
69030dd1 | 1156 | } |
ab05d97a | 1157 | |
8cc46ae5 VK |
1158 | ret = cpufreq_set_policy(policy, gov, pol); |
1159 | if (gov) | |
1160 | module_put(gov->owner); | |
1161 | ||
1162 | return ret; | |
909a694e DJ |
1163 | } |
1164 | ||
d9612a49 | 1165 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
fcf80582 | 1166 | { |
9c0ebcf7 | 1167 | int ret = 0; |
fcf80582 | 1168 | |
bb29ae15 VK |
1169 | /* Has this CPU been taken care of already? */ |
1170 | if (cpumask_test_cpu(cpu, policy->cpus)) | |
1171 | return 0; | |
1172 | ||
6fec833b RW |
1173 | guard(cpufreq_policy_write)(policy); |
1174 | ||
45482c70 RW |
1175 | if (has_target()) |
1176 | cpufreq_stop_governor(policy); | |
fcf80582 | 1177 | |
fcf80582 | 1178 | cpumask_set_cpu(cpu, policy->cpus); |
2eaa3e2d | 1179 | |
9c0ebcf7 | 1180 | if (has_target()) { |
0a300767 | 1181 | ret = cpufreq_start_governor(policy); |
49f18560 | 1182 | if (ret) |
3de9bdeb | 1183 | pr_err("%s: Failed to start governor\n", __func__); |
820c6ca2 | 1184 | } |
6fec833b | 1185 | |
49f18560 | 1186 | return ret; |
fcf80582 | 1187 | } |
1da177e4 | 1188 | |
c57b25bd | 1189 | void refresh_frequency_limits(struct cpufreq_policy *policy) |
70a59fde | 1190 | { |
67d874c3 | 1191 | if (!policy_is_inactive(policy)) { |
67d874c3 | 1192 | pr_debug("updating policy for CPU %u\n", policy->cpu); |
70a59fde | 1193 | |
1e4f63ae | 1194 | cpufreq_set_policy(policy, policy->governor, policy->policy); |
67d874c3 | 1195 | } |
70a59fde | 1196 | } |
c57b25bd | 1197 | EXPORT_SYMBOL(refresh_frequency_limits); |
70a59fde | 1198 | |
11eb69b9 VK |
1199 | static void handle_update(struct work_struct *work) |
1200 | { | |
1201 | struct cpufreq_policy *policy = | |
1202 | container_of(work, struct cpufreq_policy, update); | |
70a59fde VK |
1203 | |
1204 | pr_debug("handle_update for cpu %u called\n", policy->cpu); | |
6fec833b RW |
1205 | |
1206 | guard(cpufreq_policy_write)(policy); | |
1207 | ||
70a59fde | 1208 | refresh_frequency_limits(policy); |
67d874c3 VK |
1209 | } |
1210 | ||
1211 | static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, | |
1212 | void *data) | |
1213 | { | |
1214 | struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); | |
1215 | ||
1216 | schedule_work(&policy->update); | |
1217 | return 0; | |
1218 | } | |
1219 | ||
1220 | static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, | |
1221 | void *data) | |
1222 | { | |
1223 | struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); | |
1224 | ||
1225 | schedule_work(&policy->update); | |
1226 | return 0; | |
1227 | } | |
1228 | ||
1229 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) | |
1230 | { | |
1231 | struct kobject *kobj; | |
1232 | struct completion *cmp; | |
1233 | ||
6fec833b RW |
1234 | scoped_guard(cpufreq_policy_write, policy) { |
1235 | cpufreq_stats_free_table(policy); | |
1236 | kobj = &policy->kobj; | |
1237 | cmp = &policy->kobj_unregister; | |
1238 | } | |
67d874c3 VK |
1239 | kobject_put(kobj); |
1240 | ||
1241 | /* | |
1242 | * We need to make sure that the underlying kobj is | |
1243 | * actually not referenced anymore by anybody before we | |
1244 | * proceed with unloading. | |
1245 | */ | |
1246 | pr_debug("waiting for dropping of refcount\n"); | |
1247 | wait_for_completion(cmp); | |
1248 | pr_debug("wait complete\n"); | |
fcf80582 | 1249 | } |
1da177e4 | 1250 | |
a34e63b1 | 1251 | static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
e9698cc5 SB |
1252 | { |
1253 | struct cpufreq_policy *policy; | |
67d874c3 | 1254 | struct device *dev = get_cpu_device(cpu); |
edd4a893 | 1255 | int ret; |
e9698cc5 | 1256 | |
67d874c3 VK |
1257 | if (!dev) |
1258 | return NULL; | |
1259 | ||
e9698cc5 SB |
1260 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
1261 | if (!policy) | |
1262 | return NULL; | |
1263 | ||
1264 | if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) | |
1265 | goto err_free_policy; | |
1266 | ||
1267 | if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) | |
1268 | goto err_free_cpumask; | |
1269 | ||
559ed407 RW |
1270 | if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
1271 | goto err_free_rcpumask; | |
1272 | ||
5c510548 | 1273 | init_completion(&policy->kobj_unregister); |
edd4a893 VK |
1274 | ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
1275 | cpufreq_global_kobject, "policy%u", cpu); | |
1276 | if (ret) { | |
67d874c3 | 1277 | dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); |
2acb9bda RW |
1278 | /* |
1279 | * The entire policy object will be freed below, but the extra | |
1280 | * memory allocated for the kobject name needs to be freed by | |
1281 | * releasing the kobject. | |
1282 | */ | |
4ebe36c9 | 1283 | kobject_put(&policy->kobj); |
edd4a893 VK |
1284 | goto err_free_real_cpus; |
1285 | } | |
1286 | ||
3000ce3c RW |
1287 | freq_constraints_init(&policy->constraints); |
1288 | ||
67d874c3 VK |
1289 | policy->nb_min.notifier_call = cpufreq_notifier_min; |
1290 | policy->nb_max.notifier_call = cpufreq_notifier_max; | |
1291 | ||
3000ce3c RW |
1292 | ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, |
1293 | &policy->nb_min); | |
67d874c3 | 1294 | if (ret) { |
ba6ea77d LC |
1295 | dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n", |
1296 | ret, cpu); | |
67d874c3 VK |
1297 | goto err_kobj_remove; |
1298 | } | |
1299 | ||
3000ce3c RW |
1300 | ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, |
1301 | &policy->nb_max); | |
67d874c3 | 1302 | if (ret) { |
ba6ea77d LC |
1303 | dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n", |
1304 | ret, cpu); | |
67d874c3 VK |
1305 | goto err_min_qos_notifier; |
1306 | } | |
1307 | ||
c88a1f8b | 1308 | INIT_LIST_HEAD(&policy->policy_list); |
ad7722da | 1309 | init_rwsem(&policy->rwsem); |
12478cf0 SB |
1310 | spin_lock_init(&policy->transition_lock); |
1311 | init_waitqueue_head(&policy->transition_wait); | |
818c5712 | 1312 | INIT_WORK(&policy->update, handle_update); |
ad7722da | 1313 | |
e9698cc5 SB |
1314 | return policy; |
1315 | ||
67d874c3 | 1316 | err_min_qos_notifier: |
3000ce3c RW |
1317 | freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, |
1318 | &policy->nb_min); | |
67d874c3 VK |
1319 | err_kobj_remove: |
1320 | cpufreq_policy_put_kobj(policy); | |
edd4a893 VK |
1321 | err_free_real_cpus: |
1322 | free_cpumask_var(policy->real_cpus); | |
2fc3384d VK |
1323 | err_free_rcpumask: |
1324 | free_cpumask_var(policy->related_cpus); | |
e9698cc5 SB |
1325 | err_free_cpumask: |
1326 | free_cpumask_var(policy->cpus); | |
1327 | err_free_policy: | |
1328 | kfree(policy); | |
1329 | ||
1330 | return NULL; | |
1331 | } | |
1332 | ||
f9f41e3e | 1333 | static void cpufreq_policy_free(struct cpufreq_policy *policy) |
e9698cc5 | 1334 | { |
988bed09 VK |
1335 | unsigned long flags; |
1336 | int cpu; | |
1337 | ||
a2f6a7ac VK |
1338 | /* |
1339 | * The callers must ensure the policy is inactive by now, to avoid any | |
1340 | * races with show()/store() callbacks. | |
1341 | */ | |
1342 | if (unlikely(!policy_is_inactive(policy))) | |
1343 | pr_warn("%s: Freeing active policy\n", __func__); | |
1344 | ||
988bed09 VK |
1345 | /* Remove policy from list */ |
1346 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
1347 | list_del(&policy->policy_list); | |
1348 | ||
1349 | for_each_cpu(cpu, policy->related_cpus) | |
1350 | per_cpu(cpufreq_cpu_data, cpu) = NULL; | |
1351 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1352 | ||
3000ce3c RW |
1353 | freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, |
1354 | &policy->nb_max); | |
1355 | freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, | |
1356 | &policy->nb_min); | |
6a149036 | 1357 | |
6941051d SH |
1358 | /* Cancel any pending policy->update work before freeing the policy. */ |
1359 | cancel_work_sync(&policy->update); | |
6a149036 VK |
1360 | |
1361 | if (policy->max_freq_req) { | |
1362 | /* | |
1e81d3e0 TY |
1363 | * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY |
1364 | * notification, since CPUFREQ_CREATE_POLICY notification was | |
1365 | * sent after adding max_freq_req earlier. | |
6a149036 VK |
1366 | */ |
1367 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, | |
1368 | CPUFREQ_REMOVE_POLICY, policy); | |
3000ce3c | 1369 | freq_qos_remove_request(policy->max_freq_req); |
6a149036 VK |
1370 | } |
1371 | ||
3000ce3c | 1372 | freq_qos_remove_request(policy->min_freq_req); |
18c49926 | 1373 | kfree(policy->min_freq_req); |
67d874c3 | 1374 | |
f9f41e3e | 1375 | cpufreq_policy_put_kobj(policy); |
559ed407 | 1376 | free_cpumask_var(policy->real_cpus); |
e9698cc5 SB |
1377 | free_cpumask_var(policy->related_cpus); |
1378 | free_cpumask_var(policy->cpus); | |
1379 | kfree(policy); | |
1380 | } | |
1381 | ||
68974e3a RW |
1382 | static int cpufreq_policy_online(struct cpufreq_policy *policy, |
1383 | unsigned int cpu, bool new_policy) | |
1da177e4 | 1384 | { |
1da177e4 | 1385 | unsigned long flags; |
0b275352 RW |
1386 | unsigned int j; |
1387 | int ret; | |
87549141 | 1388 | |
6fec833b | 1389 | guard(cpufreq_policy_write)(policy); |
1da177e4 | 1390 | |
387b5170 RW |
1391 | policy->cpu = cpu; |
1392 | policy->governor = NULL; | |
0d66b91e | 1393 | |
91a12e91 | 1394 | if (!new_policy && cpufreq_driver->online) { |
68315f1a PG |
1395 | /* Recover policy->cpus using related_cpus */ |
1396 | cpumask_copy(policy->cpus, policy->related_cpus); | |
1397 | ||
91a12e91 VK |
1398 | ret = cpufreq_driver->online(policy); |
1399 | if (ret) { | |
1400 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1401 | __LINE__); | |
1402 | goto out_exit_policy; | |
1403 | } | |
91a12e91 VK |
1404 | } else { |
1405 | cpumask_copy(policy->cpus, cpumask_of(cpu)); | |
643ae6e8 | 1406 | |
91a12e91 VK |
1407 | /* |
1408 | * Call driver. From then on the cpufreq must be able | |
1409 | * to accept all calls to ->verify and ->setpolicy for this CPU. | |
1410 | */ | |
1411 | ret = cpufreq_driver->init(policy); | |
1412 | if (ret) { | |
1413 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1414 | __LINE__); | |
68974e3a | 1415 | goto out_clear_policy; |
91a12e91 | 1416 | } |
d417e069 | 1417 | |
3b718057 RW |
1418 | /* |
1419 | * The initialization has succeeded and the policy is online. | |
1420 | * If there is a problem with its frequency table, take it | |
1421 | * offline and drop it. | |
1422 | */ | |
91a12e91 VK |
1423 | ret = cpufreq_table_validate_and_sort(policy); |
1424 | if (ret) | |
3b718057 | 1425 | goto out_offline_policy; |
6d4e81ed | 1426 | |
4d1f3a5b | 1427 | /* related_cpus should at least include policy->cpus. */ |
0998a03a | 1428 | cpumask_copy(policy->related_cpus, policy->cpus); |
4d1f3a5b | 1429 | } |
559ed407 | 1430 | |
5a7e56a5 VK |
1431 | /* |
1432 | * affected cpus must always be the one, which are online. We aren't | |
1433 | * managing offline cpus here. | |
1434 | */ | |
1435 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); | |
1436 | ||
194d99c7 | 1437 | if (new_policy) { |
2f0ba790 | 1438 | for_each_cpu(j, policy->related_cpus) { |
988bed09 | 1439 | per_cpu(cpufreq_cpu_data, j) = policy; |
2c1b5a84 | 1440 | add_cpu_dev_symlink(policy, j, get_cpu_device(j)); |
2f0ba790 | 1441 | } |
18c49926 VK |
1442 | |
1443 | policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), | |
1444 | GFP_KERNEL); | |
b96f0384 WS |
1445 | if (!policy->min_freq_req) { |
1446 | ret = -ENOMEM; | |
18c49926 | 1447 | goto out_destroy_policy; |
b96f0384 | 1448 | } |
18c49926 | 1449 | |
3000ce3c RW |
1450 | ret = freq_qos_add_request(&policy->constraints, |
1451 | policy->min_freq_req, FREQ_QOS_MIN, | |
521223d8 | 1452 | FREQ_QOS_MIN_DEFAULT_VALUE); |
18c49926 VK |
1453 | if (ret < 0) { |
1454 | /* | |
3000ce3c | 1455 | * So we don't call freq_qos_remove_request() for an |
18c49926 VK |
1456 | * uninitialized request. |
1457 | */ | |
1458 | kfree(policy->min_freq_req); | |
1459 | policy->min_freq_req = NULL; | |
18c49926 VK |
1460 | goto out_destroy_policy; |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * This must be initialized right here to avoid calling | |
3000ce3c | 1465 | * freq_qos_remove_request() on uninitialized request in case |
18c49926 VK |
1466 | * of errors. |
1467 | */ | |
1468 | policy->max_freq_req = policy->min_freq_req + 1; | |
1469 | ||
3000ce3c RW |
1470 | ret = freq_qos_add_request(&policy->constraints, |
1471 | policy->max_freq_req, FREQ_QOS_MAX, | |
521223d8 | 1472 | FREQ_QOS_MAX_DEFAULT_VALUE); |
18c49926 VK |
1473 | if (ret < 0) { |
1474 | policy->max_freq_req = NULL; | |
18c49926 VK |
1475 | goto out_destroy_policy; |
1476 | } | |
6a149036 VK |
1477 | |
1478 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, | |
1479 | CPUFREQ_CREATE_POLICY, policy); | |
1608f023 LZ |
1480 | } else { |
1481 | ret = freq_qos_update_request(policy->max_freq_req, policy->max); | |
1482 | if (ret < 0) | |
1483 | goto out_destroy_policy; | |
988bed09 | 1484 | } |
652ed95d | 1485 | |
5ddc6d4e | 1486 | if (cpufreq_driver->get && has_target()) { |
da60ce9f VK |
1487 | policy->cur = cpufreq_driver->get(policy->cpu); |
1488 | if (!policy->cur) { | |
b96f0384 | 1489 | ret = -EIO; |
da60ce9f | 1490 | pr_err("%s: ->get() failed\n", __func__); |
d417e069 | 1491 | goto out_destroy_policy; |
da60ce9f VK |
1492 | } |
1493 | } | |
1494 | ||
d3916691 VK |
1495 | /* |
1496 | * Sometimes boot loaders set CPU frequency to a value outside of | |
1497 | * frequency table present with cpufreq core. In such cases CPU might be | |
1498 | * unstable if it has to run on that frequency for long duration of time | |
1499 | * and so its better to set it to a frequency which is specified in | |
1500 | * freq-table. This also makes cpufreq stats inconsistent as | |
1501 | * cpufreq-stats would fail to register because current frequency of CPU | |
1502 | * isn't found in freq-table. | |
1503 | * | |
1504 | * Because we don't want this change to effect boot process badly, we go | |
1505 | * for the next freq which is >= policy->cur ('cur' must be set by now, | |
1506 | * otherwise we will end up setting freq to lowest of the table as 'cur' | |
1507 | * is initialized to zero). | |
1508 | * | |
1509 | * We are passing target-freq as "policy->cur - 1" otherwise | |
1510 | * __cpufreq_driver_target() would simply fail, as policy->cur will be | |
1511 | * equal to target-freq. | |
1512 | */ | |
1513 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) | |
1514 | && has_target()) { | |
97148d0a VK |
1515 | unsigned int old_freq = policy->cur; |
1516 | ||
d3916691 | 1517 | /* Are we running at unknown frequency ? */ |
97148d0a | 1518 | ret = cpufreq_frequency_table_get_index(policy, old_freq); |
d3916691 | 1519 | if (ret == -EINVAL) { |
97148d0a VK |
1520 | ret = __cpufreq_driver_target(policy, old_freq - 1, |
1521 | CPUFREQ_RELATION_L); | |
d3916691 VK |
1522 | |
1523 | /* | |
1524 | * Reaching here after boot in a few seconds may not | |
1525 | * mean that system will remain stable at "unknown" | |
1526 | * frequency for longer duration. Hence, a BUG_ON(). | |
1527 | */ | |
1528 | BUG_ON(ret); | |
8d5459f1 | 1529 | pr_info("%s: CPU%d: Running at unlisted initial frequency: %u kHz, changing to: %u kHz\n", |
97148d0a | 1530 | __func__, policy->cpu, old_freq, policy->cur); |
d3916691 VK |
1531 | } |
1532 | } | |
1533 | ||
194d99c7 | 1534 | if (new_policy) { |
d9612a49 | 1535 | ret = cpufreq_add_dev_interface(policy); |
a82fab29 | 1536 | if (ret) |
d417e069 | 1537 | goto out_destroy_policy; |
1aefc75b RW |
1538 | |
1539 | cpufreq_stats_create_table(policy); | |
8ff69732 | 1540 | |
988bed09 VK |
1541 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1542 | list_add(&policy->policy_list, &cpufreq_policy_list); | |
1543 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
c17495b0 VK |
1544 | |
1545 | /* | |
1546 | * Register with the energy model before | |
ebeeee39 | 1547 | * em_rebuild_sched_domains() is called, which will result |
c17495b0 VK |
1548 | * in rebuilding of the sched domains, which should only be done |
1549 | * once the energy model is properly initialized for the policy | |
1550 | * first. | |
1551 | * | |
1552 | * Also, this should be called before the policy is registered | |
1553 | * with cooling framework. | |
1554 | */ | |
1555 | if (cpufreq_driver->register_em) | |
1556 | cpufreq_driver->register_em(policy); | |
988bed09 | 1557 | } |
9515f4d6 | 1558 | |
7f0fa40f VK |
1559 | ret = cpufreq_init_policy(policy); |
1560 | if (ret) { | |
1561 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", | |
1562 | __func__, cpu, ret); | |
d417e069 | 1563 | goto out_destroy_policy; |
08fd8c1c | 1564 | } |
e18f1682 | 1565 | |
6fec833b | 1566 | return 0; |
68974e3a RW |
1567 | |
1568 | out_destroy_policy: | |
1569 | for_each_cpu(j, policy->real_cpus) | |
1570 | remove_cpu_dev_symlink(policy, j, get_cpu_device(j)); | |
1571 | ||
1572 | out_offline_policy: | |
1573 | if (cpufreq_driver->offline) | |
1574 | cpufreq_driver->offline(policy); | |
1575 | ||
1576 | out_exit_policy: | |
1577 | if (cpufreq_driver->exit) | |
1578 | cpufreq_driver->exit(policy); | |
1579 | ||
1580 | out_clear_policy: | |
1581 | cpumask_clear(policy->cpus); | |
1582 | ||
6fec833b | 1583 | return ret; |
68974e3a RW |
1584 | } |
1585 | ||
1586 | static int cpufreq_online(unsigned int cpu) | |
1587 | { | |
1588 | struct cpufreq_policy *policy; | |
1589 | bool new_policy; | |
1590 | int ret; | |
1591 | ||
1592 | pr_debug("%s: bringing CPU%u online\n", __func__, cpu); | |
1593 | ||
1594 | /* Check if this CPU already has a policy to manage it */ | |
1595 | policy = per_cpu(cpufreq_cpu_data, cpu); | |
1596 | if (policy) { | |
1597 | WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); | |
1598 | if (!policy_is_inactive(policy)) | |
1599 | return cpufreq_add_policy_cpu(policy, cpu); | |
1600 | ||
1601 | /* This is the only online CPU for the policy. Start over. */ | |
1602 | new_policy = false; | |
1603 | } else { | |
1604 | new_policy = true; | |
1605 | policy = cpufreq_policy_alloc(cpu); | |
1606 | if (!policy) | |
1607 | return -ENOMEM; | |
1608 | } | |
1609 | ||
1610 | ret = cpufreq_policy_online(policy, cpu, new_policy); | |
1611 | if (ret) { | |
1612 | cpufreq_policy_free(policy); | |
1613 | return ret; | |
1614 | } | |
08fd8c1c | 1615 | |
038c5b3e | 1616 | kobject_uevent(&policy->kobj, KOBJ_ADD); |
7c45cf31 | 1617 | |
4f774c4a BA |
1618 | /* Callback for handling stuff after policy is ready */ |
1619 | if (cpufreq_driver->ready) | |
1620 | cpufreq_driver->ready(policy); | |
1621 | ||
c4d61a52 VK |
1622 | /* Register cpufreq cooling only for a new policy */ |
1623 | if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver)) | |
5c238a8b AK |
1624 | policy->cdev = of_cpufreq_cooling_register(policy); |
1625 | ||
0e8d8560 VK |
1626 | /* |
1627 | * Let the per-policy boost flag mirror the cpufreq_driver boost during | |
1628 | * initialization for a new policy. For an existing policy, maintain the | |
1629 | * previous boost value unless global boost is disabled. | |
1630 | */ | |
691b3212 | 1631 | if (cpufreq_driver->set_boost && policy->boost_supported && |
0e8d8560 | 1632 | (new_policy || !cpufreq_boost_enabled())) { |
27241c8b | 1633 | ret = policy_set_boost(policy, cpufreq_boost_enabled()); |
dd016f37 LZ |
1634 | if (ret) { |
1635 | /* If the set_boost fails, the online operation is not affected */ | |
1636 | pr_info("%s: CPU%d: Cannot %s BOOST\n", __func__, policy->cpu, | |
27241c8b | 1637 | str_enable_disable(cpufreq_boost_enabled())); |
dd016f37 LZ |
1638 | } |
1639 | } | |
1640 | ||
2d06d8c4 | 1641 | pr_debug("initialization complete\n"); |
87c32271 | 1642 | |
1da177e4 | 1643 | return 0; |
1da177e4 LT |
1644 | } |
1645 | ||
0b275352 RW |
1646 | /** |
1647 | * cpufreq_add_dev - the cpufreq interface for a CPU device. | |
1648 | * @dev: CPU device. | |
1649 | * @sif: Subsystem interface structure pointer (not used) | |
1650 | */ | |
1651 | static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) | |
1652 | { | |
a794d613 | 1653 | struct cpufreq_policy *policy; |
0b275352 | 1654 | unsigned cpu = dev->id; |
26619804 | 1655 | int ret; |
0b275352 RW |
1656 | |
1657 | dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); | |
1658 | ||
26619804 VK |
1659 | if (cpu_online(cpu)) { |
1660 | ret = cpufreq_online(cpu); | |
1661 | if (ret) | |
1662 | return ret; | |
1663 | } | |
0b275352 | 1664 | |
26619804 | 1665 | /* Create sysfs link on CPU registration */ |
a794d613 | 1666 | policy = per_cpu(cpufreq_cpu_data, cpu); |
2f0ba790 | 1667 | if (policy) |
2c1b5a84 | 1668 | add_cpu_dev_symlink(policy, cpu, dev); |
26619804 | 1669 | |
2f0ba790 | 1670 | return 0; |
1da177e4 LT |
1671 | } |
1672 | ||
fddd8f86 | 1673 | static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy) |
1da177e4 | 1674 | { |
69cee714 | 1675 | int ret; |
1da177e4 | 1676 | |
45482c70 RW |
1677 | if (has_target()) |
1678 | cpufreq_stop_governor(policy); | |
1da177e4 | 1679 | |
9591becb | 1680 | cpumask_clear_cpu(cpu, policy->cpus); |
4573237b | 1681 | |
9591becb | 1682 | if (!policy_is_inactive(policy)) { |
e1e962c5 RW |
1683 | /* Nominate a new CPU if necessary. */ |
1684 | if (cpu == policy->cpu) | |
1685 | policy->cpu = cpumask_any(policy->cpus); | |
1686 | ||
1687 | /* Start the governor again for the active policy. */ | |
9591becb | 1688 | if (has_target()) { |
0a300767 | 1689 | ret = cpufreq_start_governor(policy); |
9591becb VK |
1690 | if (ret) |
1691 | pr_err("%s: Failed to start governor\n", __func__); | |
1692 | } | |
cedb70af | 1693 | |
fddd8f86 | 1694 | return; |
cedb70af SB |
1695 | } |
1696 | ||
e1e962c5 | 1697 | if (has_target()) |
0faf84ca | 1698 | strscpy(policy->last_governor, policy->governor->name, |
e1e962c5 RW |
1699 | CPUFREQ_NAME_LEN); |
1700 | else | |
1701 | policy->last_policy = policy->policy; | |
1702 | ||
36be3418 RW |
1703 | if (has_target()) |
1704 | cpufreq_exit_governor(policy); | |
1da177e4 | 1705 | |
87549141 | 1706 | /* |
91a12e91 VK |
1707 | * Perform the ->offline() during light-weight tear-down, as |
1708 | * that allows fast recovery when the CPU comes back. | |
87549141 | 1709 | */ |
91a12e91 VK |
1710 | if (cpufreq_driver->offline) { |
1711 | cpufreq_driver->offline(policy); | |
b8f85833 | 1712 | return; |
55582bcc | 1713 | } |
b8f85833 VK |
1714 | |
1715 | if (cpufreq_driver->exit) | |
1716 | cpufreq_driver->exit(policy); | |
1717 | ||
1718 | policy->freq_table = NULL; | |
fddd8f86 RW |
1719 | } |
1720 | ||
1721 | static int cpufreq_offline(unsigned int cpu) | |
1722 | { | |
1723 | struct cpufreq_policy *policy; | |
1724 | ||
1725 | pr_debug("%s: unregistering CPU %u\n", __func__, cpu); | |
1726 | ||
1727 | policy = cpufreq_cpu_get_raw(cpu); | |
1728 | if (!policy) { | |
1729 | pr_debug("%s: No cpu_data found\n", __func__); | |
1730 | return 0; | |
1731 | } | |
1732 | ||
6fec833b | 1733 | guard(cpufreq_policy_write)(policy); |
fddd8f86 RW |
1734 | |
1735 | __cpufreq_offline(cpu, policy); | |
49f18560 | 1736 | |
27622b06 | 1737 | return 0; |
1da177e4 LT |
1738 | } |
1739 | ||
a9909c21 | 1740 | /* |
27a862e9 | 1741 | * cpufreq_remove_dev - remove a CPU device |
cedb70af SB |
1742 | * |
1743 | * Removes the cpufreq interface for a CPU device. | |
cedb70af | 1744 | */ |
71db87ba | 1745 | static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) |
5a01f2e8 | 1746 | { |
8a25a2fd | 1747 | unsigned int cpu = dev->id; |
559ed407 | 1748 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
87549141 | 1749 | |
559ed407 | 1750 | if (!policy) |
1af115d6 | 1751 | return; |
87549141 | 1752 | |
6fec833b RW |
1753 | scoped_guard(cpufreq_policy_write, policy) { |
1754 | if (cpu_online(cpu)) | |
1755 | __cpufreq_offline(cpu, policy); | |
f339f354 | 1756 | |
6fec833b | 1757 | remove_cpu_dev_symlink(policy, cpu, dev); |
87549141 | 1758 | |
6fec833b RW |
1759 | if (!cpumask_empty(policy->real_cpus)) |
1760 | return; | |
87549141 | 1761 | |
6fec833b RW |
1762 | /* |
1763 | * Unregister cpufreq cooling once all the CPUs of the policy | |
1764 | * are removed. | |
1765 | */ | |
1766 | if (cpufreq_thermal_control_enabled(cpufreq_driver)) { | |
1767 | cpufreq_cooling_unregister(policy->cdev); | |
1768 | policy->cdev = NULL; | |
1769 | } | |
f339f354 | 1770 | |
6fec833b RW |
1771 | /* We did light-weight exit earlier, do full tear down now */ |
1772 | if (cpufreq_driver->offline && cpufreq_driver->exit) | |
1773 | cpufreq_driver->exit(policy); | |
c4d61a52 VK |
1774 | } |
1775 | ||
f339f354 | 1776 | cpufreq_policy_free(policy); |
5a01f2e8 VP |
1777 | } |
1778 | ||
1da177e4 | 1779 | /** |
ec06e586 RW |
1780 | * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference. |
1781 | * @policy: Policy managing CPUs. | |
1782 | * @new_freq: New CPU frequency. | |
1da177e4 | 1783 | * |
ec06e586 RW |
1784 | * Adjust to the current frequency first and clean up later by either calling |
1785 | * cpufreq_update_policy(), or scheduling handle_update(). | |
1da177e4 | 1786 | */ |
a1e1dc41 | 1787 | static void cpufreq_out_of_sync(struct cpufreq_policy *policy, |
e08f5f5b | 1788 | unsigned int new_freq) |
1da177e4 LT |
1789 | { |
1790 | struct cpufreq_freqs freqs; | |
b43a7ffb | 1791 | |
e837f9b5 | 1792 | pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", |
a1e1dc41 | 1793 | policy->cur, new_freq); |
1da177e4 | 1794 | |
a1e1dc41 | 1795 | freqs.old = policy->cur; |
1da177e4 | 1796 | freqs.new = new_freq; |
b43a7ffb | 1797 | |
8fec051e VK |
1798 | cpufreq_freq_transition_begin(policy, &freqs); |
1799 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
1da177e4 LT |
1800 | } |
1801 | ||
5980752e VK |
1802 | static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) |
1803 | { | |
1804 | unsigned int new_freq; | |
1805 | ||
1806 | new_freq = cpufreq_driver->get(policy->cpu); | |
1807 | if (!new_freq) | |
1808 | return 0; | |
1809 | ||
1810 | /* | |
1811 | * If fast frequency switching is used with the given policy, the check | |
1812 | * against policy->cur is pointless, so skip it in that case. | |
1813 | */ | |
1814 | if (policy->fast_switch_enabled || !has_target()) | |
1815 | return new_freq; | |
1816 | ||
1817 | if (policy->cur != new_freq) { | |
f55ae08c VK |
1818 | /* |
1819 | * For some platforms, the frequency returned by hardware may be | |
1820 | * slightly different from what is provided in the frequency | |
1821 | * table, for example hardware may return 499 MHz instead of 500 | |
1822 | * MHz. In such cases it is better to avoid getting into | |
1823 | * unnecessary frequency updates. | |
1824 | */ | |
44295af5 | 1825 | if (abs(policy->cur - new_freq) < KHZ_PER_MHZ) |
f55ae08c VK |
1826 | return policy->cur; |
1827 | ||
5980752e VK |
1828 | cpufreq_out_of_sync(policy, new_freq); |
1829 | if (update) | |
1830 | schedule_work(&policy->update); | |
1831 | } | |
1832 | ||
1833 | return new_freq; | |
1834 | } | |
1835 | ||
32ee8c3e | 1836 | /** |
4ab70df4 | 1837 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
95235ca2 VP |
1838 | * @cpu: CPU number |
1839 | * | |
1840 | * This is the last known freq, without actually getting it from the driver. | |
1841 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. | |
1842 | */ | |
1843 | unsigned int cpufreq_quick_get(unsigned int cpu) | |
1844 | { | |
ece898da | 1845 | struct cpufreq_policy *policy __free(put_cpufreq_policy) = NULL; |
c75361c0 | 1846 | unsigned long flags; |
95235ca2 | 1847 | |
c75361c0 RC |
1848 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1849 | ||
1850 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { | |
ece898da RW |
1851 | unsigned int ret_freq = cpufreq_driver->get(cpu); |
1852 | ||
c75361c0 | 1853 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
ece898da | 1854 | |
c75361c0 RC |
1855 | return ret_freq; |
1856 | } | |
1857 | ||
1858 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
9e21ba8b DB |
1859 | |
1860 | policy = cpufreq_cpu_get(cpu); | |
ece898da RW |
1861 | if (policy) |
1862 | return policy->cur; | |
95235ca2 | 1863 | |
ece898da | 1864 | return 0; |
95235ca2 VP |
1865 | } |
1866 | EXPORT_SYMBOL(cpufreq_quick_get); | |
1867 | ||
3d737108 JB |
1868 | /** |
1869 | * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU | |
1870 | * @cpu: CPU number | |
1871 | * | |
1872 | * Just return the max possible frequency for a given CPU. | |
1873 | */ | |
1874 | unsigned int cpufreq_quick_get_max(unsigned int cpu) | |
1875 | { | |
ece898da | 1876 | struct cpufreq_policy *policy __free(put_cpufreq_policy); |
3d737108 | 1877 | |
ece898da RW |
1878 | policy = cpufreq_cpu_get(cpu); |
1879 | if (policy) | |
1880 | return policy->max; | |
3d737108 | 1881 | |
ece898da | 1882 | return 0; |
3d737108 JB |
1883 | } |
1884 | EXPORT_SYMBOL(cpufreq_quick_get_max); | |
1885 | ||
bbce8eaa IV |
1886 | /** |
1887 | * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU | |
1888 | * @cpu: CPU number | |
1889 | * | |
1890 | * The default return value is the max_freq field of cpuinfo. | |
1891 | */ | |
1892 | __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) | |
1893 | { | |
ece898da | 1894 | struct cpufreq_policy *policy __free(put_cpufreq_policy); |
bbce8eaa | 1895 | |
ece898da RW |
1896 | policy = cpufreq_cpu_get(cpu); |
1897 | if (policy) | |
1898 | return policy->cpuinfo.max_freq; | |
bbce8eaa | 1899 | |
ece898da | 1900 | return 0; |
bbce8eaa IV |
1901 | } |
1902 | EXPORT_SYMBOL(cpufreq_get_hw_max_freq); | |
1903 | ||
d92d50a4 | 1904 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
1da177e4 | 1905 | { |
4db7c34c | 1906 | if (unlikely(policy_is_inactive(policy))) |
5980752e | 1907 | return 0; |
1da177e4 | 1908 | |
5980752e | 1909 | return cpufreq_verify_current_freq(policy, true); |
5a01f2e8 | 1910 | } |
1da177e4 | 1911 | |
5a01f2e8 VP |
1912 | /** |
1913 | * cpufreq_get - get the current CPU frequency (in kHz) | |
1914 | * @cpu: CPU number | |
1915 | * | |
1916 | * Get the CPU current (static) CPU frequency | |
1917 | */ | |
1918 | unsigned int cpufreq_get(unsigned int cpu) | |
1919 | { | |
ece898da | 1920 | struct cpufreq_policy *policy __free(put_cpufreq_policy); |
5a01f2e8 | 1921 | |
ece898da | 1922 | policy = cpufreq_cpu_get(cpu); |
6fec833b RW |
1923 | if (!policy) |
1924 | return 0; | |
5a01f2e8 | 1925 | |
ece898da | 1926 | guard(cpufreq_policy_read)(policy); |
6eed9404 | 1927 | |
ece898da RW |
1928 | if (cpufreq_driver->get) |
1929 | return __cpufreq_get(policy); | |
6eed9404 | 1930 | |
ece898da | 1931 | return 0; |
1da177e4 LT |
1932 | } |
1933 | EXPORT_SYMBOL(cpufreq_get); | |
1934 | ||
8a25a2fd KS |
1935 | static struct subsys_interface cpufreq_interface = { |
1936 | .name = "cpufreq", | |
1937 | .subsys = &cpu_subsys, | |
1938 | .add_dev = cpufreq_add_dev, | |
1939 | .remove_dev = cpufreq_remove_dev, | |
e00e56df RW |
1940 | }; |
1941 | ||
e28867ea VK |
1942 | /* |
1943 | * In case platform wants some specific frequency to be configured | |
1944 | * during suspend.. | |
1945 | */ | |
1946 | int cpufreq_generic_suspend(struct cpufreq_policy *policy) | |
1947 | { | |
1948 | int ret; | |
1949 | ||
1950 | if (!policy->suspend_freq) { | |
201f3716 BZ |
1951 | pr_debug("%s: suspend_freq not defined\n", __func__); |
1952 | return 0; | |
e28867ea VK |
1953 | } |
1954 | ||
1955 | pr_debug("%s: Setting suspend-freq: %u\n", __func__, | |
1956 | policy->suspend_freq); | |
1957 | ||
1958 | ret = __cpufreq_driver_target(policy, policy->suspend_freq, | |
1959 | CPUFREQ_RELATION_H); | |
1960 | if (ret) | |
1961 | pr_err("%s: unable to set suspend-freq: %u. err: %d\n", | |
1962 | __func__, policy->suspend_freq, ret); | |
1963 | ||
1964 | return ret; | |
1965 | } | |
1966 | EXPORT_SYMBOL(cpufreq_generic_suspend); | |
1967 | ||
42d4dc3f | 1968 | /** |
ec06e586 | 1969 | * cpufreq_suspend() - Suspend CPUFreq governors. |
e00e56df | 1970 | * |
2f0aea93 VK |
1971 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
1972 | * as some platforms can't change frequency after this point in suspend cycle. | |
1973 | * Because some of the devices (like: i2c, regulators, etc) they use for | |
1974 | * changing frequency are suspended quickly after this point. | |
42d4dc3f | 1975 | */ |
2f0aea93 | 1976 | void cpufreq_suspend(void) |
42d4dc3f | 1977 | { |
3a3e9e06 | 1978 | struct cpufreq_policy *policy; |
42d4dc3f | 1979 | |
2f0aea93 VK |
1980 | if (!cpufreq_driver) |
1981 | return; | |
42d4dc3f | 1982 | |
ba41e1bc | 1983 | if (!has_target() && !cpufreq_driver->suspend) |
b1b12bab | 1984 | goto suspend; |
42d4dc3f | 1985 | |
2f0aea93 VK |
1986 | pr_debug("%s: Suspending Governors\n", __func__); |
1987 | ||
f963735a | 1988 | for_each_active_policy(policy) { |
ba41e1bc | 1989 | if (has_target()) { |
6fec833b RW |
1990 | scoped_guard(cpufreq_policy_write, policy) { |
1991 | cpufreq_stop_governor(policy); | |
1992 | } | |
ba41e1bc RW |
1993 | } |
1994 | ||
1995 | if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) | |
e9a7cc1d FF |
1996 | pr_err("%s: Failed to suspend driver: %s\n", __func__, |
1997 | cpufreq_driver->name); | |
42d4dc3f | 1998 | } |
b1b12bab VK |
1999 | |
2000 | suspend: | |
2001 | cpufreq_suspended = true; | |
42d4dc3f BH |
2002 | } |
2003 | ||
1da177e4 | 2004 | /** |
ec06e586 | 2005 | * cpufreq_resume() - Resume CPUFreq governors. |
1da177e4 | 2006 | * |
2f0aea93 VK |
2007 | * Called during system wide Suspend/Hibernate cycle for resuming governors that |
2008 | * are suspended with cpufreq_suspend(). | |
1da177e4 | 2009 | */ |
2f0aea93 | 2010 | void cpufreq_resume(void) |
1da177e4 | 2011 | { |
3a3e9e06 | 2012 | struct cpufreq_policy *policy; |
49f18560 | 2013 | int ret; |
1da177e4 | 2014 | |
2f0aea93 | 2015 | if (!cpufreq_driver) |
703cbaa6 BY |
2016 | return; |
2017 | ||
2018 | if (unlikely(!cpufreq_suspended)) | |
2f0aea93 | 2019 | return; |
1da177e4 | 2020 | |
8e30444e LT |
2021 | cpufreq_suspended = false; |
2022 | ||
ba41e1bc | 2023 | if (!has_target() && !cpufreq_driver->resume) |
e00e56df | 2024 | return; |
1da177e4 | 2025 | |
2f0aea93 | 2026 | pr_debug("%s: Resuming Governors\n", __func__); |
1da177e4 | 2027 | |
f963735a | 2028 | for_each_active_policy(policy) { |
49f18560 | 2029 | if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { |
1f464cb4 LC |
2030 | pr_err("%s: Failed to resume driver: %s\n", __func__, |
2031 | cpufreq_driver->name); | |
ba41e1bc | 2032 | } else if (has_target()) { |
6fec833b RW |
2033 | scoped_guard(cpufreq_policy_write, policy) { |
2034 | ret = cpufreq_start_governor(policy); | |
2035 | } | |
49f18560 VK |
2036 | |
2037 | if (ret) | |
1f464cb4 LC |
2038 | pr_err("%s: Failed to start governor for CPU%u's policy\n", |
2039 | __func__, policy->cpu); | |
49f18560 | 2040 | } |
2f0aea93 VK |
2041 | } |
2042 | } | |
1da177e4 | 2043 | |
a62f68f5 RW |
2044 | /** |
2045 | * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones. | |
2046 | * @flags: Flags to test against the current cpufreq driver's flags. | |
2047 | * | |
2048 | * Assumes that the driver is there, so callers must ensure that this is the | |
2049 | * case. | |
2050 | */ | |
2051 | bool cpufreq_driver_test_flags(u16 flags) | |
2052 | { | |
2053 | return !!(cpufreq_driver->flags & flags); | |
2054 | } | |
2055 | ||
9d95046e | 2056 | /** |
ec06e586 | 2057 | * cpufreq_get_current_driver - Return the current driver's name. |
9d95046e | 2058 | * |
ec06e586 RW |
2059 | * Return the name string of the currently registered cpufreq driver or NULL if |
2060 | * none. | |
9d95046e BP |
2061 | */ |
2062 | const char *cpufreq_get_current_driver(void) | |
2063 | { | |
1c3d85dd RW |
2064 | if (cpufreq_driver) |
2065 | return cpufreq_driver->name; | |
2066 | ||
2067 | return NULL; | |
9d95046e BP |
2068 | } |
2069 | EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); | |
1da177e4 | 2070 | |
51315cdf | 2071 | /** |
ec06e586 | 2072 | * cpufreq_get_driver_data - Return current driver data. |
51315cdf | 2073 | * |
ec06e586 RW |
2074 | * Return the private data of the currently registered cpufreq driver, or NULL |
2075 | * if no cpufreq driver has been registered. | |
51315cdf TP |
2076 | */ |
2077 | void *cpufreq_get_driver_data(void) | |
2078 | { | |
2079 | if (cpufreq_driver) | |
2080 | return cpufreq_driver->driver_data; | |
2081 | ||
2082 | return NULL; | |
2083 | } | |
2084 | EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); | |
2085 | ||
1da177e4 LT |
2086 | /********************************************************************* |
2087 | * NOTIFIER LISTS INTERFACE * | |
2088 | *********************************************************************/ | |
2089 | ||
2090 | /** | |
ec06e586 RW |
2091 | * cpufreq_register_notifier - Register a notifier with cpufreq. |
2092 | * @nb: notifier function to register. | |
2093 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. | |
1da177e4 | 2094 | * |
ec06e586 RW |
2095 | * Add a notifier to one of two lists: either a list of notifiers that run on |
2096 | * clock rate changes (once before and once after every transition), or a list | |
2097 | * of notifiers that ron on cpufreq policy changes. | |
1da177e4 | 2098 | * |
ec06e586 RW |
2099 | * This function may sleep and it has the same return values as |
2100 | * blocking_notifier_chain_register(). | |
1da177e4 LT |
2101 | */ |
2102 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) | |
2103 | { | |
2104 | int ret; | |
2105 | ||
d5aaffa9 DB |
2106 | if (cpufreq_disabled()) |
2107 | return -EINVAL; | |
2108 | ||
1da177e4 LT |
2109 | switch (list) { |
2110 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
2111 | mutex_lock(&cpufreq_fast_switch_lock); |
2112 | ||
2113 | if (cpufreq_fast_switch_count > 0) { | |
2114 | mutex_unlock(&cpufreq_fast_switch_lock); | |
2115 | return -EBUSY; | |
2116 | } | |
b4dfdbb3 | 2117 | ret = srcu_notifier_chain_register( |
e041c683 | 2118 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
2119 | if (!ret) |
2120 | cpufreq_fast_switch_count--; | |
2121 | ||
2122 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
2123 | break; |
2124 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
2125 | ret = blocking_notifier_chain_register( |
2126 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
2127 | break; |
2128 | default: | |
2129 | ret = -EINVAL; | |
2130 | } | |
1da177e4 LT |
2131 | |
2132 | return ret; | |
2133 | } | |
2134 | EXPORT_SYMBOL(cpufreq_register_notifier); | |
2135 | ||
1da177e4 | 2136 | /** |
ec06e586 RW |
2137 | * cpufreq_unregister_notifier - Unregister a notifier from cpufreq. |
2138 | * @nb: notifier block to be unregistered. | |
2139 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. | |
1da177e4 | 2140 | * |
ec06e586 | 2141 | * Remove a notifier from one of the cpufreq notifier lists. |
1da177e4 | 2142 | * |
ec06e586 RW |
2143 | * This function may sleep and it has the same return values as |
2144 | * blocking_notifier_chain_unregister(). | |
1da177e4 LT |
2145 | */ |
2146 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) | |
2147 | { | |
2148 | int ret; | |
2149 | ||
d5aaffa9 DB |
2150 | if (cpufreq_disabled()) |
2151 | return -EINVAL; | |
2152 | ||
1da177e4 LT |
2153 | switch (list) { |
2154 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
2155 | mutex_lock(&cpufreq_fast_switch_lock); |
2156 | ||
b4dfdbb3 | 2157 | ret = srcu_notifier_chain_unregister( |
e041c683 | 2158 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
2159 | if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) |
2160 | cpufreq_fast_switch_count++; | |
2161 | ||
2162 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
2163 | break; |
2164 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
2165 | ret = blocking_notifier_chain_unregister( |
2166 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
2167 | break; |
2168 | default: | |
2169 | ret = -EINVAL; | |
2170 | } | |
1da177e4 LT |
2171 | |
2172 | return ret; | |
2173 | } | |
2174 | EXPORT_SYMBOL(cpufreq_unregister_notifier); | |
2175 | ||
2176 | ||
2177 | /********************************************************************* | |
2178 | * GOVERNORS * | |
2179 | *********************************************************************/ | |
2180 | ||
b7898fda RW |
2181 | /** |
2182 | * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. | |
2183 | * @policy: cpufreq policy to switch the frequency for. | |
2184 | * @target_freq: New frequency to set (may be approximate). | |
2185 | * | |
2186 | * Carry out a fast frequency switch without sleeping. | |
2187 | * | |
2188 | * The driver's ->fast_switch() callback invoked by this function must be | |
2189 | * suitable for being called from within RCU-sched read-side critical sections | |
2190 | * and it is expected to select the minimum available frequency greater than or | |
2191 | * equal to @target_freq (CPUFREQ_RELATION_L). | |
2192 | * | |
2193 | * This function must not be called if policy->fast_switch_enabled is unset. | |
2194 | * | |
2195 | * Governors calling this function must guarantee that it will never be invoked | |
2196 | * twice in parallel for the same policy and that it will never be called in | |
2197 | * parallel with either ->target() or ->target_index() for the same policy. | |
2198 | * | |
209887e6 VK |
2199 | * Returns the actual frequency set for the CPU. |
2200 | * | |
2201 | * If 0 is returned by the driver's ->fast_switch() callback to indicate an | |
2202 | * error condition, the hardware configuration must be preserved. | |
b7898fda RW |
2203 | */ |
2204 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, | |
2205 | unsigned int target_freq) | |
2206 | { | |
1a0419b0 | 2207 | unsigned int freq; |
08d8c65e | 2208 | int cpu; |
1a0419b0 | 2209 | |
b9af6948 | 2210 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
1a0419b0 IV |
2211 | freq = cpufreq_driver->fast_switch(policy, target_freq); |
2212 | ||
96f60cdd VK |
2213 | if (!freq) |
2214 | return 0; | |
2215 | ||
08d8c65e | 2216 | policy->cur = freq; |
1a0419b0 | 2217 | arch_set_freq_scale(policy->related_cpus, freq, |
599457ba | 2218 | arch_scale_freq_ref(policy->cpu)); |
96f60cdd | 2219 | cpufreq_stats_record_transition(policy, freq); |
b7898fda | 2220 | |
08d8c65e VK |
2221 | if (trace_cpu_frequency_enabled()) { |
2222 | for_each_cpu(cpu, policy->cpus) | |
2223 | trace_cpu_frequency(freq, cpu); | |
2224 | } | |
2225 | ||
1a0419b0 | 2226 | return freq; |
b7898fda RW |
2227 | } |
2228 | EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); | |
2229 | ||
ee2cc427 RW |
2230 | /** |
2231 | * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. | |
2232 | * @cpu: Target CPU. | |
2233 | * @min_perf: Minimum (required) performance level (units of @capacity). | |
4e6df217 | 2234 | * @target_perf: Target (desired) performance level (units of @capacity). |
ee2cc427 RW |
2235 | * @capacity: Capacity of the target CPU. |
2236 | * | |
2237 | * Carry out a fast performance level switch of @cpu without sleeping. | |
2238 | * | |
2239 | * The driver's ->adjust_perf() callback invoked by this function must be | |
2240 | * suitable for being called from within RCU-sched read-side critical sections | |
2241 | * and it is expected to select a suitable performance level equal to or above | |
2242 | * @min_perf and preferably equal to or below @target_perf. | |
2243 | * | |
2244 | * This function must not be called if policy->fast_switch_enabled is unset. | |
2245 | * | |
2246 | * Governors calling this function must guarantee that it will never be invoked | |
2247 | * twice in parallel for the same CPU and that it will never be called in | |
2248 | * parallel with either ->target() or ->target_index() or ->fast_switch() for | |
2249 | * the same CPU. | |
2250 | */ | |
2251 | void cpufreq_driver_adjust_perf(unsigned int cpu, | |
2252 | unsigned long min_perf, | |
2253 | unsigned long target_perf, | |
2254 | unsigned long capacity) | |
2255 | { | |
2256 | cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity); | |
2257 | } | |
2258 | ||
2259 | /** | |
2260 | * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback. | |
2261 | * | |
2262 | * Return 'true' if the ->adjust_perf callback is present for the | |
2263 | * current driver or 'false' otherwise. | |
2264 | */ | |
2265 | bool cpufreq_driver_has_adjust_perf(void) | |
2266 | { | |
2267 | return !!cpufreq_driver->adjust_perf; | |
2268 | } | |
2269 | ||
1c03a2d0 VK |
2270 | /* Must set freqs->new to intermediate frequency */ |
2271 | static int __target_intermediate(struct cpufreq_policy *policy, | |
2272 | struct cpufreq_freqs *freqs, int index) | |
2273 | { | |
2274 | int ret; | |
2275 | ||
2276 | freqs->new = cpufreq_driver->get_intermediate(policy, index); | |
2277 | ||
2278 | /* We don't need to switch to intermediate freq */ | |
2279 | if (!freqs->new) | |
2280 | return 0; | |
2281 | ||
2282 | pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", | |
2283 | __func__, policy->cpu, freqs->old, freqs->new); | |
2284 | ||
2285 | cpufreq_freq_transition_begin(policy, freqs); | |
2286 | ret = cpufreq_driver->target_intermediate(policy, index); | |
2287 | cpufreq_freq_transition_end(policy, freqs, ret); | |
2288 | ||
2289 | if (ret) | |
2290 | pr_err("%s: Failed to change to intermediate frequency: %d\n", | |
2291 | __func__, ret); | |
2292 | ||
2293 | return ret; | |
2294 | } | |
2295 | ||
23727845 | 2296 | static int __target_index(struct cpufreq_policy *policy, int index) |
8d65775d | 2297 | { |
1c03a2d0 | 2298 | struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; |
56a7ff75 | 2299 | unsigned int restore_freq, intermediate_freq = 0; |
23727845 | 2300 | unsigned int newfreq = policy->freq_table[index].frequency; |
8d65775d VK |
2301 | int retval = -EINVAL; |
2302 | bool notify; | |
2303 | ||
23727845 VK |
2304 | if (newfreq == policy->cur) |
2305 | return 0; | |
2306 | ||
56a7ff75 RW |
2307 | /* Save last value to restore later on errors */ |
2308 | restore_freq = policy->cur; | |
2309 | ||
8d65775d | 2310 | notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); |
8d65775d | 2311 | if (notify) { |
1c03a2d0 VK |
2312 | /* Handle switching to intermediate frequency */ |
2313 | if (cpufreq_driver->get_intermediate) { | |
2314 | retval = __target_intermediate(policy, &freqs, index); | |
2315 | if (retval) | |
2316 | return retval; | |
2317 | ||
2318 | intermediate_freq = freqs.new; | |
2319 | /* Set old freq to intermediate */ | |
2320 | if (intermediate_freq) | |
2321 | freqs.old = freqs.new; | |
2322 | } | |
8d65775d | 2323 | |
23727845 | 2324 | freqs.new = newfreq; |
8d65775d VK |
2325 | pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", |
2326 | __func__, policy->cpu, freqs.old, freqs.new); | |
2327 | ||
2328 | cpufreq_freq_transition_begin(policy, &freqs); | |
2329 | } | |
2330 | ||
2331 | retval = cpufreq_driver->target_index(policy, index); | |
2332 | if (retval) | |
2333 | pr_err("%s: Failed to change cpu frequency: %d\n", __func__, | |
2334 | retval); | |
2335 | ||
1c03a2d0 | 2336 | if (notify) { |
8d65775d VK |
2337 | cpufreq_freq_transition_end(policy, &freqs, retval); |
2338 | ||
1c03a2d0 VK |
2339 | /* |
2340 | * Failed after setting to intermediate freq? Driver should have | |
2341 | * reverted back to initial frequency and so should we. Check | |
2342 | * here for intermediate_freq instead of get_intermediate, in | |
58405af6 | 2343 | * case we haven't switched to intermediate freq at all. |
1c03a2d0 VK |
2344 | */ |
2345 | if (unlikely(retval && intermediate_freq)) { | |
2346 | freqs.old = intermediate_freq; | |
56a7ff75 | 2347 | freqs.new = restore_freq; |
1c03a2d0 VK |
2348 | cpufreq_freq_transition_begin(policy, &freqs); |
2349 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
2350 | } | |
2351 | } | |
2352 | ||
8d65775d VK |
2353 | return retval; |
2354 | } | |
2355 | ||
1da177e4 LT |
2356 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
2357 | unsigned int target_freq, | |
2358 | unsigned int relation) | |
2359 | { | |
7249924e | 2360 | unsigned int old_target_freq = target_freq; |
c32b6b8e | 2361 | |
a7b422cd KRW |
2362 | if (cpufreq_disabled()) |
2363 | return -ENODEV; | |
2364 | ||
b7902803 RW |
2365 | target_freq = __resolve_freq(policy, target_freq, policy->min, |
2366 | policy->max, relation); | |
7249924e VK |
2367 | |
2368 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", | |
e837f9b5 | 2369 | policy->cpu, target_freq, relation, old_target_freq); |
5a1c0228 | 2370 | |
9c0ebcf7 VK |
2371 | /* |
2372 | * This might look like a redundant call as we are checking it again | |
2373 | * after finding index. But it is left intentionally for cases where | |
2374 | * exactly same freq is called again and so we can save on few function | |
2375 | * calls. | |
2376 | */ | |
1c534352 RW |
2377 | if (target_freq == policy->cur && |
2378 | !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) | |
5a1c0228 VK |
2379 | return 0; |
2380 | ||
1f39fa0d VD |
2381 | if (cpufreq_driver->target) { |
2382 | /* | |
2383 | * If the driver hasn't setup a single inefficient frequency, | |
2384 | * it's unlikely it knows how to decode CPUFREQ_RELATION_E. | |
2385 | */ | |
2386 | if (!policy->efficiencies_available) | |
2387 | relation &= ~CPUFREQ_RELATION_E; | |
2388 | ||
6019d23a | 2389 | return cpufreq_driver->target(policy, target_freq, relation); |
1f39fa0d | 2390 | } |
9c0ebcf7 | 2391 | |
6019d23a RW |
2392 | if (!cpufreq_driver->target_index) |
2393 | return -EINVAL; | |
9c0ebcf7 | 2394 | |
f9ccdec2 | 2395 | return __target_index(policy, policy->cached_resolved_idx); |
1da177e4 LT |
2396 | } |
2397 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); | |
2398 | ||
1da177e4 LT |
2399 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
2400 | unsigned int target_freq, | |
2401 | unsigned int relation) | |
2402 | { | |
6fec833b | 2403 | guard(cpufreq_policy_write)(policy); |
1da177e4 | 2404 | |
6fec833b | 2405 | return __cpufreq_driver_target(policy, target_freq, relation); |
1da177e4 LT |
2406 | } |
2407 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); | |
2408 | ||
de1df26b RW |
2409 | __weak struct cpufreq_governor *cpufreq_fallback_governor(void) |
2410 | { | |
2411 | return NULL; | |
2412 | } | |
2413 | ||
a92604b4 | 2414 | static int cpufreq_init_governor(struct cpufreq_policy *policy) |
1da177e4 | 2415 | { |
cc993cab | 2416 | int ret; |
6afde10c | 2417 | |
2f0aea93 VK |
2418 | /* Don't start any governor operations if we are entering suspend */ |
2419 | if (cpufreq_suspended) | |
2420 | return 0; | |
cb57720b EZ |
2421 | /* |
2422 | * Governor might not be initiated here if ACPI _PPC changed | |
2423 | * notification happened, so check it. | |
2424 | */ | |
2425 | if (!policy->governor) | |
2426 | return -EINVAL; | |
2f0aea93 | 2427 | |
ed4676e2 | 2428 | /* Platform doesn't want dynamic frequency switching ? */ |
9a2a9ebc | 2429 | if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && |
fc4c709f | 2430 | cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { |
de1df26b RW |
2431 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
2432 | ||
2433 | if (gov) { | |
fe829ed8 | 2434 | pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", |
e837f9b5 | 2435 | policy->governor->name, gov->name); |
6afde10c | 2436 | policy->governor = gov; |
de1df26b RW |
2437 | } else { |
2438 | return -EINVAL; | |
6afde10c | 2439 | } |
1c256245 | 2440 | } |
1da177e4 | 2441 | |
a92604b4 RW |
2442 | if (!try_module_get(policy->governor->owner)) |
2443 | return -EINVAL; | |
95731ebb | 2444 | |
a92604b4 | 2445 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
1da177e4 | 2446 | |
e788892b RW |
2447 | if (policy->governor->init) { |
2448 | ret = policy->governor->init(policy); | |
2449 | if (ret) { | |
36be3418 | 2450 | module_put(policy->governor->owner); |
e788892b RW |
2451 | return ret; |
2452 | } | |
36be3418 | 2453 | } |
1da177e4 | 2454 | |
ea9364bb RW |
2455 | policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET); |
2456 | ||
a92604b4 RW |
2457 | return 0; |
2458 | } | |
2459 | ||
2460 | static void cpufreq_exit_governor(struct cpufreq_policy *policy) | |
2461 | { | |
2462 | if (cpufreq_suspended || !policy->governor) | |
2463 | return; | |
2464 | ||
2465 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2466 | ||
e788892b RW |
2467 | if (policy->governor->exit) |
2468 | policy->governor->exit(policy); | |
a92604b4 | 2469 | |
a92604b4 | 2470 | module_put(policy->governor->owner); |
1da177e4 LT |
2471 | } |
2472 | ||
f6ebbcf0 | 2473 | int cpufreq_start_governor(struct cpufreq_policy *policy) |
0a300767 RW |
2474 | { |
2475 | int ret; | |
2476 | ||
a92604b4 RW |
2477 | if (cpufreq_suspended) |
2478 | return 0; | |
2479 | ||
2480 | if (!policy->governor) | |
2481 | return -EINVAL; | |
2482 | ||
2483 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2484 | ||
407d0fff | 2485 | if (cpufreq_driver->get) |
5980752e | 2486 | cpufreq_verify_current_freq(policy, false); |
3bbf8fe3 | 2487 | |
e788892b RW |
2488 | if (policy->governor->start) { |
2489 | ret = policy->governor->start(policy); | |
2490 | if (ret) | |
2491 | return ret; | |
2492 | } | |
2493 | ||
2494 | if (policy->governor->limits) | |
2495 | policy->governor->limits(policy); | |
d6ff44d6 | 2496 | |
d6ff44d6 | 2497 | return 0; |
0a300767 RW |
2498 | } |
2499 | ||
f6ebbcf0 | 2500 | void cpufreq_stop_governor(struct cpufreq_policy *policy) |
a92604b4 RW |
2501 | { |
2502 | if (cpufreq_suspended || !policy->governor) | |
2503 | return; | |
2504 | ||
2505 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2506 | ||
e788892b RW |
2507 | if (policy->governor->stop) |
2508 | policy->governor->stop(policy); | |
a92604b4 RW |
2509 | } |
2510 | ||
2511 | static void cpufreq_governor_limits(struct cpufreq_policy *policy) | |
2512 | { | |
2513 | if (cpufreq_suspended || !policy->governor) | |
2514 | return; | |
2515 | ||
2516 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2517 | ||
e788892b RW |
2518 | if (policy->governor->limits) |
2519 | policy->governor->limits(policy); | |
0a300767 RW |
2520 | } |
2521 | ||
1da177e4 LT |
2522 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
2523 | { | |
3bcb09a3 | 2524 | int err; |
1da177e4 LT |
2525 | |
2526 | if (!governor) | |
2527 | return -EINVAL; | |
2528 | ||
a7b422cd KRW |
2529 | if (cpufreq_disabled()) |
2530 | return -ENODEV; | |
2531 | ||
3fc54d37 | 2532 | mutex_lock(&cpufreq_governor_mutex); |
32ee8c3e | 2533 | |
3bcb09a3 | 2534 | err = -EBUSY; |
42f91fa1 | 2535 | if (!find_governor(governor->name)) { |
3bcb09a3 JF |
2536 | err = 0; |
2537 | list_add(&governor->governor_list, &cpufreq_governor_list); | |
1da177e4 | 2538 | } |
1da177e4 | 2539 | |
32ee8c3e | 2540 | mutex_unlock(&cpufreq_governor_mutex); |
3bcb09a3 | 2541 | return err; |
1da177e4 LT |
2542 | } |
2543 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); | |
2544 | ||
1da177e4 LT |
2545 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
2546 | { | |
4573237b VK |
2547 | struct cpufreq_policy *policy; |
2548 | unsigned long flags; | |
90e41bac | 2549 | |
1da177e4 LT |
2550 | if (!governor) |
2551 | return; | |
2552 | ||
a7b422cd KRW |
2553 | if (cpufreq_disabled()) |
2554 | return; | |
2555 | ||
4573237b VK |
2556 | /* clear last_governor for all inactive policies */ |
2557 | read_lock_irqsave(&cpufreq_driver_lock, flags); | |
2558 | for_each_inactive_policy(policy) { | |
18bf3a12 VK |
2559 | if (!strcmp(policy->last_governor, governor->name)) { |
2560 | policy->governor = NULL; | |
4573237b | 2561 | strcpy(policy->last_governor, "\0"); |
18bf3a12 | 2562 | } |
90e41bac | 2563 | } |
4573237b | 2564 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
90e41bac | 2565 | |
3fc54d37 | 2566 | mutex_lock(&cpufreq_governor_mutex); |
1da177e4 | 2567 | list_del(&governor->governor_list); |
3fc54d37 | 2568 | mutex_unlock(&cpufreq_governor_mutex); |
1da177e4 LT |
2569 | } |
2570 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); | |
2571 | ||
2572 | ||
1da177e4 LT |
2573 | /********************************************************************* |
2574 | * POLICY INTERFACE * | |
2575 | *********************************************************************/ | |
2576 | ||
75d65931 VG |
2577 | DEFINE_PER_CPU(unsigned long, cpufreq_pressure); |
2578 | ||
2579 | /** | |
2580 | * cpufreq_update_pressure() - Update cpufreq pressure for CPUs | |
2581 | * @policy: cpufreq policy of the CPUs. | |
2582 | * | |
2583 | * Update the value of cpufreq pressure for all @cpus in the policy. | |
2584 | */ | |
2585 | static void cpufreq_update_pressure(struct cpufreq_policy *policy) | |
2586 | { | |
2587 | unsigned long max_capacity, capped_freq, pressure; | |
2588 | u32 max_freq; | |
2589 | int cpu; | |
2590 | ||
2591 | cpu = cpumask_first(policy->related_cpus); | |
2592 | max_freq = arch_scale_freq_ref(cpu); | |
2593 | capped_freq = policy->max; | |
2594 | ||
2595 | /* | |
2596 | * Handle properly the boost frequencies, which should simply clean | |
2597 | * the cpufreq pressure value. | |
2598 | */ | |
2599 | if (max_freq <= capped_freq) { | |
2600 | pressure = 0; | |
2601 | } else { | |
2602 | max_capacity = arch_scale_cpu_capacity(cpu); | |
2603 | pressure = max_capacity - | |
2604 | mult_frac(max_capacity, capped_freq, max_freq); | |
2605 | } | |
2606 | ||
2607 | for_each_cpu(cpu, policy->related_cpus) | |
2608 | WRITE_ONCE(per_cpu(cpufreq_pressure, cpu), pressure); | |
2609 | } | |
2610 | ||
a0dbb819 RW |
2611 | /** |
2612 | * cpufreq_set_policy - Modify cpufreq policy parameters. | |
2613 | * @policy: Policy object to modify. | |
1e4f63ae RW |
2614 | * @new_gov: Policy governor pointer. |
2615 | * @new_pol: Policy value (for drivers with built-in governors). | |
a0dbb819 | 2616 | * |
1e4f63ae RW |
2617 | * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency |
2618 | * limits to be set for the policy, update @policy with the verified limits | |
2619 | * values and either invoke the driver's ->setpolicy() callback (if present) or | |
2620 | * carry out a governor update for @policy. That is, run the current governor's | |
2621 | * ->limits() callback (if @new_gov points to the same object as the one in | |
2622 | * @policy) or replace the governor for @policy with @new_gov. | |
a0dbb819 RW |
2623 | * |
2624 | * The cpuinfo part of @policy is not updated by this function. | |
153d7f3f | 2625 | */ |
1e4f63ae RW |
2626 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
2627 | struct cpufreq_governor *new_gov, | |
2628 | unsigned int new_pol) | |
1da177e4 | 2629 | { |
1e4f63ae | 2630 | struct cpufreq_policy_data new_data; |
d9a789c7 RW |
2631 | struct cpufreq_governor *old_gov; |
2632 | int ret; | |
1da177e4 | 2633 | |
1e4f63ae RW |
2634 | memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
2635 | new_data.freq_table = policy->freq_table; | |
2636 | new_data.cpu = policy->cpu; | |
fba9573b | 2637 | /* |
67d874c3 VK |
2638 | * PM QoS framework collects all the requests from users and provide us |
2639 | * the final aggregated value here. | |
2640 | */ | |
1e4f63ae RW |
2641 | new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); |
2642 | new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); | |
2643 | ||
2644 | pr_debug("setting new policy for CPU %u: %u - %u kHz\n", | |
2645 | new_data.cpu, new_data.min, new_data.max); | |
9c9a43ed | 2646 | |
737ffb27 VK |
2647 | /* |
2648 | * Verify that the CPU speed can be set within these limits and make sure | |
2649 | * that min <= max. | |
2650 | */ | |
1e4f63ae | 2651 | ret = cpufreq_driver->verify(&new_data); |
1da177e4 | 2652 | if (ret) |
d9a789c7 | 2653 | return ret; |
1da177e4 | 2654 | |
15171769 VD |
2655 | /* |
2656 | * Resolve policy min/max to available frequencies. It ensures | |
2657 | * no frequency resolution will neither overshoot the requested maximum | |
2658 | * nor undershoot the requested minimum. | |
7491cdf4 RW |
2659 | * |
2660 | * Avoid storing intermediate values in policy->max or policy->min and | |
2661 | * compiler optimizations around them because they may be accessed | |
2662 | * concurrently by cpufreq_driver_resolve_freq() during the update. | |
15171769 | 2663 | */ |
b7902803 RW |
2664 | WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, |
2665 | new_data.min, new_data.max, | |
2666 | CPUFREQ_RELATION_H)); | |
2667 | new_data.min = __resolve_freq(policy, new_data.min, new_data.min, | |
2668 | new_data.max, CPUFREQ_RELATION_L); | |
7491cdf4 RW |
2669 | WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min); |
2670 | ||
601b2185 | 2671 | trace_cpu_frequency_limits(policy); |
1da177e4 | 2672 | |
75d65931 VG |
2673 | cpufreq_update_pressure(policy); |
2674 | ||
e3c06236 SM |
2675 | policy->cached_target_freq = UINT_MAX; |
2676 | ||
2d06d8c4 | 2677 | pr_debug("new min and max freqs are %u - %u kHz\n", |
e837f9b5 | 2678 | policy->min, policy->max); |
1da177e4 | 2679 | |
1c3d85dd | 2680 | if (cpufreq_driver->setpolicy) { |
1e4f63ae | 2681 | policy->policy = new_pol; |
2d06d8c4 | 2682 | pr_debug("setting range\n"); |
167a38dc | 2683 | return cpufreq_driver->setpolicy(policy); |
d9a789c7 | 2684 | } |
1da177e4 | 2685 | |
1e4f63ae | 2686 | if (new_gov == policy->governor) { |
2bb4059e | 2687 | pr_debug("governor limits update\n"); |
a92604b4 | 2688 | cpufreq_governor_limits(policy); |
d6ff44d6 | 2689 | return 0; |
0a300767 | 2690 | } |
7bd353a9 | 2691 | |
d9a789c7 RW |
2692 | pr_debug("governor switch\n"); |
2693 | ||
2694 | /* save old, working values */ | |
2695 | old_gov = policy->governor; | |
2696 | /* end old governor */ | |
2697 | if (old_gov) { | |
45482c70 | 2698 | cpufreq_stop_governor(policy); |
36be3418 | 2699 | cpufreq_exit_governor(policy); |
1da177e4 LT |
2700 | } |
2701 | ||
d9a789c7 | 2702 | /* start new governor */ |
1e4f63ae | 2703 | policy->governor = new_gov; |
a92604b4 | 2704 | ret = cpufreq_init_governor(policy); |
4bc384ae | 2705 | if (!ret) { |
0a300767 RW |
2706 | ret = cpufreq_start_governor(policy); |
2707 | if (!ret) { | |
2bb4059e | 2708 | pr_debug("governor change\n"); |
0a300767 RW |
2709 | return 0; |
2710 | } | |
b7898fda | 2711 | cpufreq_exit_governor(policy); |
d9a789c7 RW |
2712 | } |
2713 | ||
2714 | /* new governor failed, so re-start old one */ | |
2715 | pr_debug("starting governor %s failed\n", policy->governor->name); | |
2716 | if (old_gov) { | |
2717 | policy->governor = old_gov; | |
a92604b4 | 2718 | if (cpufreq_init_governor(policy)) |
4bc384ae VK |
2719 | policy->governor = NULL; |
2720 | else | |
0a300767 | 2721 | cpufreq_start_governor(policy); |
d9a789c7 RW |
2722 | } |
2723 | ||
4bc384ae | 2724 | return ret; |
1da177e4 LT |
2725 | } |
2726 | ||
684e1855 RW |
2727 | static void cpufreq_policy_refresh(struct cpufreq_policy *policy) |
2728 | { | |
2729 | guard(cpufreq_policy_write)(policy); | |
2730 | ||
2731 | /* | |
2732 | * BIOS might change freq behind our back | |
2733 | * -> ask driver for current freq and notify governors about a change | |
2734 | */ | |
2735 | if (cpufreq_driver->get && has_target() && | |
2736 | (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) | |
2737 | return; | |
2738 | ||
2739 | refresh_frequency_limits(policy); | |
2740 | } | |
2741 | ||
1da177e4 | 2742 | /** |
a0dbb819 RW |
2743 | * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. |
2744 | * @cpu: CPU to re-evaluate the policy for. | |
1da177e4 | 2745 | * |
a0dbb819 | 2746 | * Update the current frequency for the cpufreq policy of @cpu and use |
18c49926 VK |
2747 | * cpufreq_set_policy() to re-apply the min and max limits, which triggers the |
2748 | * evaluation of policy notifiers and the cpufreq driver's ->verify() callback | |
2749 | * for the policy in question, among other things. | |
1da177e4 | 2750 | */ |
30248fef | 2751 | void cpufreq_update_policy(unsigned int cpu) |
1da177e4 | 2752 | { |
9a74bfdf | 2753 | struct cpufreq_policy *policy __free(put_cpufreq_policy); |
1da177e4 | 2754 | |
9a74bfdf | 2755 | policy = cpufreq_cpu_get(cpu); |
fefa8ff8 | 2756 | if (!policy) |
30248fef | 2757 | return; |
1da177e4 | 2758 | |
684e1855 | 2759 | cpufreq_policy_refresh(policy); |
1da177e4 LT |
2760 | } |
2761 | EXPORT_SYMBOL(cpufreq_update_policy); | |
2762 | ||
5a25e3f7 RW |
2763 | /** |
2764 | * cpufreq_update_limits - Update policy limits for a given CPU. | |
2765 | * @cpu: CPU to update the policy limits for. | |
2766 | * | |
2767 | * Invoke the driver's ->update_limits callback if present or call | |
684e1855 | 2768 | * cpufreq_policy_refresh() for @cpu. |
5a25e3f7 RW |
2769 | */ |
2770 | void cpufreq_update_limits(unsigned int cpu) | |
2771 | { | |
9e4e2490 RW |
2772 | struct cpufreq_policy *policy __free(put_cpufreq_policy); |
2773 | ||
2774 | policy = cpufreq_cpu_get(cpu); | |
2775 | if (!policy) | |
2776 | return; | |
2777 | ||
5a25e3f7 | 2778 | if (cpufreq_driver->update_limits) |
eaff6b62 | 2779 | cpufreq_driver->update_limits(policy); |
5a25e3f7 | 2780 | else |
684e1855 | 2781 | cpufreq_policy_refresh(policy); |
5a25e3f7 RW |
2782 | } |
2783 | EXPORT_SYMBOL_GPL(cpufreq_update_limits); | |
2784 | ||
6f19efc0 LM |
2785 | /********************************************************************* |
2786 | * BOOST * | |
2787 | *********************************************************************/ | |
9a23eb8b | 2788 | int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) |
6f19efc0 | 2789 | { |
cf6fada7 | 2790 | int ret; |
49f18560 | 2791 | |
cf6fada7 XW |
2792 | if (!policy->freq_table) |
2793 | return -ENXIO; | |
f8bfc116 | 2794 | |
cf6fada7 XW |
2795 | ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); |
2796 | if (ret) { | |
2797 | pr_err("%s: Policy frequency update failed\n", __func__); | |
2798 | return ret; | |
6f19efc0 LM |
2799 | } |
2800 | ||
cf6fada7 XW |
2801 | ret = freq_qos_update_request(policy->max_freq_req, policy->max); |
2802 | if (ret < 0) | |
2803 | return ret; | |
2804 | ||
552abb88 | 2805 | return 0; |
6f19efc0 | 2806 | } |
9a23eb8b | 2807 | EXPORT_SYMBOL_GPL(cpufreq_boost_set_sw); |
6f19efc0 | 2808 | |
1f048150 | 2809 | static int cpufreq_boost_trigger_state(int state) |
6f19efc0 | 2810 | { |
cf6fada7 | 2811 | struct cpufreq_policy *policy; |
6f19efc0 LM |
2812 | unsigned long flags; |
2813 | int ret = 0; | |
2814 | ||
121baab7 VK |
2815 | /* |
2816 | * Don't compare 'cpufreq_driver->boost_enabled' with 'state' here to | |
2817 | * make sure all policies are in sync with global boost flag. | |
2818 | */ | |
6f19efc0 LM |
2819 | |
2820 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2821 | cpufreq_driver->boost_enabled = state; | |
2822 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2823 | ||
09681a07 | 2824 | cpus_read_lock(); |
cf6fada7 | 2825 | for_each_active_policy(policy) { |
691b3212 VK |
2826 | if (!policy->boost_supported) |
2827 | continue; | |
2828 | ||
27241c8b VK |
2829 | ret = policy_set_boost(policy, state); |
2830 | if (ret) | |
cf6fada7 | 2831 | goto err_reset_state; |
6f19efc0 | 2832 | } |
09681a07 | 2833 | cpus_read_unlock(); |
cf6fada7 XW |
2834 | |
2835 | return 0; | |
2836 | ||
2837 | err_reset_state: | |
09681a07 | 2838 | cpus_read_unlock(); |
cf6fada7 XW |
2839 | |
2840 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2841 | cpufreq_driver->boost_enabled = !state; | |
2842 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2843 | ||
2844 | pr_err("%s: Cannot %s BOOST\n", | |
f994c1cb | 2845 | __func__, str_enable_disable(state)); |
6f19efc0 LM |
2846 | |
2847 | return ret; | |
2848 | } | |
2849 | ||
41669da0 | 2850 | static bool cpufreq_boost_supported(void) |
6f19efc0 | 2851 | { |
89f98d7e | 2852 | return cpufreq_driver->set_boost; |
6f19efc0 | 2853 | } |
6f19efc0 | 2854 | |
44139ed4 VK |
2855 | static int create_boost_sysfs_file(void) |
2856 | { | |
2857 | int ret; | |
2858 | ||
c82bd444 | 2859 | ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2860 | if (ret) |
2861 | pr_err("%s: cannot register global BOOST sysfs file\n", | |
2862 | __func__); | |
2863 | ||
2864 | return ret; | |
2865 | } | |
2866 | ||
2867 | static void remove_boost_sysfs_file(void) | |
2868 | { | |
2869 | if (cpufreq_boost_supported()) | |
c82bd444 | 2870 | sysfs_remove_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2871 | } |
2872 | ||
43c0226c | 2873 | bool cpufreq_boost_enabled(void) |
6f19efc0 LM |
2874 | { |
2875 | return cpufreq_driver->boost_enabled; | |
2876 | } | |
2877 | EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); | |
2878 | ||
1da177e4 LT |
2879 | /********************************************************************* |
2880 | * REGISTER / UNREGISTER CPUFREQ DRIVER * | |
2881 | *********************************************************************/ | |
27622b06 | 2882 | static enum cpuhp_state hp_online; |
1da177e4 | 2883 | |
c4a3fa26 CY |
2884 | static int cpuhp_cpufreq_online(unsigned int cpu) |
2885 | { | |
2886 | cpufreq_online(cpu); | |
2887 | ||
2888 | return 0; | |
2889 | } | |
2890 | ||
2891 | static int cpuhp_cpufreq_offline(unsigned int cpu) | |
2892 | { | |
2893 | cpufreq_offline(cpu); | |
2894 | ||
2895 | return 0; | |
2896 | } | |
2897 | ||
1da177e4 LT |
2898 | /** |
2899 | * cpufreq_register_driver - register a CPU Frequency driver | |
2900 | * @driver_data: A struct cpufreq_driver containing the values# | |
2901 | * submitted by the CPU Frequency driver. | |
2902 | * | |
bb176f7d | 2903 | * Registers a CPU Frequency driver to this core code. This code |
63af4055 | 2904 | * returns zero on success, -EEXIST when another driver got here first |
32ee8c3e | 2905 | * (and isn't unregistered in the meantime). |
1da177e4 LT |
2906 | * |
2907 | */ | |
221dee28 | 2908 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
1da177e4 LT |
2909 | { |
2910 | unsigned long flags; | |
2911 | int ret; | |
2912 | ||
a7b422cd KRW |
2913 | if (cpufreq_disabled()) |
2914 | return -ENODEV; | |
2915 | ||
46770be0 VK |
2916 | /* |
2917 | * The cpufreq core depends heavily on the availability of device | |
2918 | * structure, make sure they are available before proceeding further. | |
2919 | */ | |
2920 | if (!get_cpu_device(0)) | |
2921 | return -EPROBE_DEFER; | |
2922 | ||
1da177e4 | 2923 | if (!driver_data || !driver_data->verify || !driver_data->init || |
9c0ebcf7 | 2924 | !(driver_data->setpolicy || driver_data->target_index || |
9832235f RW |
2925 | driver_data->target) || |
2926 | (driver_data->setpolicy && (driver_data->target_index || | |
1c03a2d0 | 2927 | driver_data->target)) || |
a9a22b57 | 2928 | (!driver_data->get_intermediate != !driver_data->target_intermediate) || |
b4a11fa3 WK |
2929 | (!driver_data->online != !driver_data->offline) || |
2930 | (driver_data->adjust_perf && !driver_data->fast_switch)) | |
1da177e4 LT |
2931 | return -EINVAL; |
2932 | ||
2d06d8c4 | 2933 | pr_debug("trying to register driver %s\n", driver_data->name); |
1da177e4 | 2934 | |
fdd320da | 2935 | /* Protect against concurrent CPU online/offline. */ |
a92551e4 | 2936 | cpus_read_lock(); |
fdd320da | 2937 | |
0d1857a1 | 2938 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2939 | if (cpufreq_driver) { |
0d1857a1 | 2940 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
fdd320da RW |
2941 | ret = -EEXIST; |
2942 | goto out; | |
1da177e4 | 2943 | } |
1c3d85dd | 2944 | cpufreq_driver = driver_data; |
0d1857a1 | 2945 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 2946 | |
874f6353 IV |
2947 | /* |
2948 | * Mark support for the scheduler's frequency invariance engine for | |
2949 | * drivers that implement target(), target_index() or fast_switch(). | |
2950 | */ | |
2951 | if (!cpufreq_driver->setpolicy) { | |
2952 | static_branch_enable_cpuslocked(&cpufreq_freq_invariance); | |
2953 | pr_debug("supports frequency invariance"); | |
2954 | } | |
2955 | ||
bc68b7df VK |
2956 | if (driver_data->setpolicy) |
2957 | driver_data->flags |= CPUFREQ_CONST_LOOPS; | |
2958 | ||
7a6c79f2 RW |
2959 | if (cpufreq_boost_supported()) { |
2960 | ret = create_boost_sysfs_file(); | |
2961 | if (ret) | |
2962 | goto err_null_driver; | |
2963 | } | |
6f19efc0 | 2964 | |
8a25a2fd | 2965 | ret = subsys_interface_register(&cpufreq_interface); |
8f5bc2ab | 2966 | if (ret) |
6f19efc0 | 2967 | goto err_boost_unreg; |
1da177e4 | 2968 | |
5ae4a4b4 | 2969 | if (unlikely(list_empty(&cpufreq_policy_list))) { |
1da177e4 | 2970 | /* if all ->init() calls failed, unregister */ |
6c770036 | 2971 | ret = -ENODEV; |
ce1bcfe9 VK |
2972 | pr_debug("%s: No CPU initialized for driver %s\n", __func__, |
2973 | driver_data->name); | |
2974 | goto err_if_unreg; | |
1da177e4 LT |
2975 | } |
2976 | ||
a92551e4 SAS |
2977 | ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, |
2978 | "cpufreq:online", | |
2979 | cpuhp_cpufreq_online, | |
2980 | cpuhp_cpufreq_offline); | |
27622b06 SAS |
2981 | if (ret < 0) |
2982 | goto err_if_unreg; | |
2983 | hp_online = ret; | |
5372e054 | 2984 | ret = 0; |
27622b06 | 2985 | |
2d06d8c4 | 2986 | pr_debug("driver %s up and running\n", driver_data->name); |
3834abb4 | 2987 | goto out; |
fdd320da | 2988 | |
8a25a2fd KS |
2989 | err_if_unreg: |
2990 | subsys_interface_unregister(&cpufreq_interface); | |
6f19efc0 | 2991 | err_boost_unreg: |
44139ed4 | 2992 | remove_boost_sysfs_file(); |
8f5bc2ab | 2993 | err_null_driver: |
0d1857a1 | 2994 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2995 | cpufreq_driver = NULL; |
0d1857a1 | 2996 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
3834abb4 | 2997 | out: |
a92551e4 | 2998 | cpus_read_unlock(); |
3834abb4 | 2999 | return ret; |
1da177e4 LT |
3000 | } |
3001 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); | |
3002 | ||
a9909c21 | 3003 | /* |
1da177e4 LT |
3004 | * cpufreq_unregister_driver - unregister the current CPUFreq driver |
3005 | * | |
bb176f7d | 3006 | * Unregister the current CPUFreq driver. Only call this if you have |
1da177e4 LT |
3007 | * the right to do so, i.e. if you have succeeded in initialising before! |
3008 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is | |
3009 | * currently not initialised. | |
3010 | */ | |
dd329e1e | 3011 | void cpufreq_unregister_driver(struct cpufreq_driver *driver) |
1da177e4 LT |
3012 | { |
3013 | unsigned long flags; | |
3014 | ||
dd329e1e UKK |
3015 | if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver))) |
3016 | return; | |
1da177e4 | 3017 | |
2d06d8c4 | 3018 | pr_debug("unregistering driver %s\n", driver->name); |
1da177e4 | 3019 | |
454d3a25 | 3020 | /* Protect against concurrent cpu hotplug */ |
a92551e4 | 3021 | cpus_read_lock(); |
8a25a2fd | 3022 | subsys_interface_unregister(&cpufreq_interface); |
44139ed4 | 3023 | remove_boost_sysfs_file(); |
874f6353 | 3024 | static_branch_disable_cpuslocked(&cpufreq_freq_invariance); |
a92551e4 | 3025 | cpuhp_remove_state_nocalls_cpuslocked(hp_online); |
1da177e4 | 3026 | |
0d1857a1 | 3027 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
6eed9404 | 3028 | |
1c3d85dd | 3029 | cpufreq_driver = NULL; |
6eed9404 | 3030 | |
0d1857a1 | 3031 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
a92551e4 | 3032 | cpus_read_unlock(); |
1da177e4 LT |
3033 | } |
3034 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); | |
5a01f2e8 VP |
3035 | |
3036 | static int __init cpufreq_core_init(void) | |
3037 | { | |
8412b456 | 3038 | struct cpufreq_governor *gov = cpufreq_default_governor(); |
2744a63c | 3039 | struct device *dev_root; |
8412b456 | 3040 | |
a7b422cd KRW |
3041 | if (cpufreq_disabled()) |
3042 | return -ENODEV; | |
3043 | ||
2744a63c GKH |
3044 | dev_root = bus_get_dev_root(&cpu_subsys); |
3045 | if (dev_root) { | |
3046 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj); | |
3047 | put_device(dev_root); | |
3048 | } | |
8aa84ad8 TR |
3049 | BUG_ON(!cpufreq_global_kobject); |
3050 | ||
8412b456 | 3051 | if (!strlen(default_governor)) |
0faf84ca | 3052 | strscpy(default_governor, gov->name, CPUFREQ_NAME_LEN); |
8412b456 | 3053 | |
5a01f2e8 VP |
3054 | return 0; |
3055 | } | |
4854649b RW |
3056 | |
3057 | static bool cpufreq_policy_is_good_for_eas(unsigned int cpu) | |
3058 | { | |
3059 | struct cpufreq_policy *policy __free(put_cpufreq_policy); | |
3060 | ||
3061 | policy = cpufreq_cpu_get(cpu); | |
3062 | if (!policy) { | |
3063 | pr_debug("cpufreq policy not set for CPU: %d\n", cpu); | |
3064 | return false; | |
3065 | } | |
3066 | ||
4854649b RW |
3067 | return sugov_is_governor(policy); |
3068 | } | |
3069 | ||
3070 | bool cpufreq_ready_for_eas(const struct cpumask *cpu_mask) | |
3071 | { | |
3072 | unsigned int cpu; | |
3073 | ||
3074 | /* Do not attempt EAS if schedutil is not being used. */ | |
3075 | for_each_cpu(cpu, cpu_mask) { | |
3076 | if (!cpufreq_policy_is_good_for_eas(cpu)) { | |
3077 | pr_debug("rd %*pbl: schedutil is mandatory for EAS\n", | |
3078 | cpumask_pr_args(cpu_mask)); | |
3079 | return false; | |
3080 | } | |
3081 | } | |
3082 | ||
3083 | return true; | |
3084 | } | |
3085 | ||
d82f2692 | 3086 | module_param(off, int, 0444); |
8412b456 | 3087 | module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); |
5a01f2e8 | 3088 | core_initcall(cpufreq_core_init); |