Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/cpufreq/cpufreq.c | |
3 | * | |
4 | * Copyright (C) 2001 Russell King | |
5 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> | |
bb176f7d | 6 | * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> |
1da177e4 | 7 | * |
c32b6b8e | 8 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
32ee8c3e | 9 | * Added handling for CPU hotplug |
8ff69732 DJ |
10 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
11 | * Fix handling for CPU hotplug -- affected CPUs | |
c32b6b8e | 12 | * |
1da177e4 LT |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License version 2 as | |
15 | * published by the Free Software Foundation. | |
1da177e4 LT |
16 | */ |
17 | ||
db701151 VK |
18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
19 | ||
5ff0a268 | 20 | #include <linux/cpu.h> |
1da177e4 | 21 | #include <linux/cpufreq.h> |
5c238a8b | 22 | #include <linux/cpu_cooling.h> |
1da177e4 | 23 | #include <linux/delay.h> |
1da177e4 | 24 | #include <linux/device.h> |
5ff0a268 VK |
25 | #include <linux/init.h> |
26 | #include <linux/kernel_stat.h> | |
27 | #include <linux/module.h> | |
3fc54d37 | 28 | #include <linux/mutex.h> |
5ff0a268 | 29 | #include <linux/slab.h> |
2f0aea93 | 30 | #include <linux/suspend.h> |
90de2a4a | 31 | #include <linux/syscore_ops.h> |
5ff0a268 | 32 | #include <linux/tick.h> |
6f4f2723 TR |
33 | #include <trace/events/power.h> |
34 | ||
b4f0676f | 35 | static LIST_HEAD(cpufreq_policy_list); |
f963735a VK |
36 | |
37 | static inline bool policy_is_inactive(struct cpufreq_policy *policy) | |
38 | { | |
39 | return cpumask_empty(policy->cpus); | |
40 | } | |
41 | ||
f963735a | 42 | /* Macros to iterate over CPU policies */ |
fd7dc7e6 EB |
43 | #define for_each_suitable_policy(__policy, __active) \ |
44 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ | |
45 | if ((__active) == !policy_is_inactive(__policy)) | |
f963735a VK |
46 | |
47 | #define for_each_active_policy(__policy) \ | |
48 | for_each_suitable_policy(__policy, true) | |
49 | #define for_each_inactive_policy(__policy) \ | |
50 | for_each_suitable_policy(__policy, false) | |
51 | ||
52 | #define for_each_policy(__policy) \ | |
b4f0676f VK |
53 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) |
54 | ||
f7b27061 VK |
55 | /* Iterate over governors */ |
56 | static LIST_HEAD(cpufreq_governor_list); | |
57 | #define for_each_governor(__governor) \ | |
58 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) | |
59 | ||
1da177e4 | 60 | /** |
cd878479 | 61 | * The "cpufreq driver" - the arch- or hardware-dependent low |
1da177e4 LT |
62 | * level driver of CPUFreq support, and its spinlock. This lock |
63 | * also protects the cpufreq_cpu_data array. | |
64 | */ | |
1c3d85dd | 65 | static struct cpufreq_driver *cpufreq_driver; |
7a6aedfa | 66 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
bb176f7d | 67 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
bb176f7d | 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 | ||
1da177e4 | 77 | /* internal prototypes */ |
d92d50a4 | 78 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
a92604b4 RW |
79 | static int cpufreq_init_governor(struct cpufreq_policy *policy); |
80 | static void cpufreq_exit_governor(struct cpufreq_policy *policy); | |
0a300767 | 81 | static int cpufreq_start_governor(struct cpufreq_policy *policy); |
a92604b4 RW |
82 | static void cpufreq_stop_governor(struct cpufreq_policy *policy); |
83 | static void cpufreq_governor_limits(struct cpufreq_policy *policy); | |
45482c70 | 84 | |
1da177e4 | 85 | /** |
32ee8c3e DJ |
86 | * Two notifier lists: the "policy" list is involved in the |
87 | * validation process for a new CPU frequency policy; the | |
1da177e4 LT |
88 | * "transition" list for kernel code that needs to handle |
89 | * changes to devices when the CPU clock speed changes. | |
90 | * The mutex locks both lists. | |
91 | */ | |
e041c683 | 92 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
cc85de36 | 93 | SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); |
1da177e4 | 94 | |
a7b422cd | 95 | static int off __read_mostly; |
da584455 | 96 | static int cpufreq_disabled(void) |
a7b422cd KRW |
97 | { |
98 | return off; | |
99 | } | |
100 | void disable_cpufreq(void) | |
101 | { | |
102 | off = 1; | |
103 | } | |
29464f28 | 104 | static DEFINE_MUTEX(cpufreq_governor_mutex); |
1da177e4 | 105 | |
4d5dcc42 VK |
106 | bool have_governor_per_policy(void) |
107 | { | |
0b981e70 | 108 | return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); |
4d5dcc42 | 109 | } |
3f869d6d | 110 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
4d5dcc42 | 111 | |
944e9a03 VK |
112 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
113 | { | |
114 | if (have_governor_per_policy()) | |
115 | return &policy->kobj; | |
116 | else | |
117 | return cpufreq_global_kobject; | |
118 | } | |
119 | EXPORT_SYMBOL_GPL(get_governor_parent_kobj); | |
120 | ||
72a4ce34 VK |
121 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
122 | { | |
123 | u64 idle_time; | |
124 | u64 cur_wall_time; | |
125 | u64 busy_time; | |
126 | ||
7fb1327e | 127 | cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); |
72a4ce34 VK |
128 | |
129 | busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; | |
130 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; | |
131 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; | |
132 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; | |
133 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; | |
134 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; | |
135 | ||
136 | idle_time = cur_wall_time - busy_time; | |
137 | if (wall) | |
7fb1327e | 138 | *wall = div_u64(cur_wall_time, NSEC_PER_USEC); |
72a4ce34 | 139 | |
7fb1327e | 140 | return div_u64(idle_time, NSEC_PER_USEC); |
72a4ce34 VK |
141 | } |
142 | ||
143 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) | |
144 | { | |
145 | u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); | |
146 | ||
147 | if (idle_time == -1ULL) | |
148 | return get_cpu_idle_time_jiffy(cpu, wall); | |
149 | else if (!io_busy) | |
150 | idle_time += get_cpu_iowait_time_us(cpu, wall); | |
151 | ||
152 | return idle_time; | |
153 | } | |
154 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); | |
155 | ||
e7d5459d DE |
156 | __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
157 | unsigned long max_freq) | |
158 | { | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(arch_set_freq_scale); | |
161 | ||
70e9e778 VK |
162 | /* |
163 | * This is a generic cpufreq init() routine which can be used by cpufreq | |
164 | * drivers of SMP systems. It will do following: | |
165 | * - validate & show freq table passed | |
166 | * - set policies transition latency | |
167 | * - policy->cpus with all possible CPUs | |
168 | */ | |
169 | int cpufreq_generic_init(struct cpufreq_policy *policy, | |
170 | struct cpufreq_frequency_table *table, | |
171 | unsigned int transition_latency) | |
172 | { | |
92c99d15 | 173 | policy->freq_table = table; |
70e9e778 VK |
174 | policy->cpuinfo.transition_latency = transition_latency; |
175 | ||
176 | /* | |
58405af6 | 177 | * The driver only supports the SMP configuration where all processors |
70e9e778 VK |
178 | * share the clock and voltage and clock. |
179 | */ | |
180 | cpumask_setall(policy->cpus); | |
181 | ||
182 | return 0; | |
183 | } | |
184 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); | |
185 | ||
1f0bd44e | 186 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
652ed95d VK |
187 | { |
188 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); | |
189 | ||
988bed09 VK |
190 | return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; |
191 | } | |
1f0bd44e | 192 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); |
988bed09 VK |
193 | |
194 | unsigned int cpufreq_generic_get(unsigned int cpu) | |
195 | { | |
196 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); | |
197 | ||
652ed95d | 198 | if (!policy || IS_ERR(policy->clk)) { |
e837f9b5 JP |
199 | pr_err("%s: No %s associated to cpu: %d\n", |
200 | __func__, policy ? "clk" : "policy", cpu); | |
652ed95d VK |
201 | return 0; |
202 | } | |
203 | ||
204 | return clk_get_rate(policy->clk) / 1000; | |
205 | } | |
206 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); | |
207 | ||
50e9c852 | 208 | /** |
5d094fea RW |
209 | * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. |
210 | * @cpu: CPU to find the policy for. | |
50e9c852 | 211 | * |
5d094fea RW |
212 | * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment |
213 | * the kobject reference counter of that policy. Return a valid policy on | |
214 | * success or NULL on failure. | |
50e9c852 | 215 | * |
5d094fea RW |
216 | * The policy returned by this function has to be released with the help of |
217 | * cpufreq_cpu_put() to balance its kobject reference counter properly. | |
50e9c852 | 218 | */ |
6eed9404 | 219 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
1da177e4 | 220 | { |
6eed9404 | 221 | struct cpufreq_policy *policy = NULL; |
1da177e4 LT |
222 | unsigned long flags; |
223 | ||
1b947c90 | 224 | if (WARN_ON(cpu >= nr_cpu_ids)) |
6eed9404 VK |
225 | return NULL; |
226 | ||
1da177e4 | 227 | /* get the cpufreq driver */ |
1c3d85dd | 228 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1da177e4 | 229 | |
6eed9404 VK |
230 | if (cpufreq_driver) { |
231 | /* get the CPU */ | |
988bed09 | 232 | policy = cpufreq_cpu_get_raw(cpu); |
6eed9404 VK |
233 | if (policy) |
234 | kobject_get(&policy->kobj); | |
235 | } | |
1da177e4 | 236 | |
6eed9404 | 237 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 238 | |
3a3e9e06 | 239 | return policy; |
a9144436 | 240 | } |
1da177e4 LT |
241 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
242 | ||
50e9c852 | 243 | /** |
5d094fea RW |
244 | * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. |
245 | * @policy: cpufreq policy returned by cpufreq_cpu_get(). | |
50e9c852 | 246 | */ |
3a3e9e06 | 247 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
1da177e4 | 248 | { |
6eed9404 | 249 | kobject_put(&policy->kobj); |
1da177e4 LT |
250 | } |
251 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); | |
252 | ||
1da177e4 LT |
253 | /********************************************************************* |
254 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * | |
255 | *********************************************************************/ | |
256 | ||
257 | /** | |
258 | * adjust_jiffies - adjust the system "loops_per_jiffy" | |
259 | * | |
260 | * This function alters the system "loops_per_jiffy" for the clock | |
261 | * speed change. Note that loops_per_jiffy cannot be updated on SMP | |
32ee8c3e | 262 | * systems as each CPU might be scaled differently. So, use the arch |
1da177e4 LT |
263 | * per-CPU loops_per_jiffy value wherever possible. |
264 | */ | |
858119e1 | 265 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
1da177e4 | 266 | { |
39c132ee VK |
267 | #ifndef CONFIG_SMP |
268 | static unsigned long l_p_j_ref; | |
269 | static unsigned int l_p_j_ref_freq; | |
270 | ||
1da177e4 LT |
271 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
272 | return; | |
273 | ||
274 | if (!l_p_j_ref_freq) { | |
275 | l_p_j_ref = loops_per_jiffy; | |
276 | l_p_j_ref_freq = ci->old; | |
e837f9b5 JP |
277 | pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", |
278 | l_p_j_ref, l_p_j_ref_freq); | |
1da177e4 | 279 | } |
0b443ead | 280 | if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { |
e08f5f5b GS |
281 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
282 | ci->new); | |
e837f9b5 JP |
283 | pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", |
284 | loops_per_jiffy, ci->new); | |
1da177e4 | 285 | } |
1da177e4 | 286 | #endif |
39c132ee | 287 | } |
1da177e4 | 288 | |
20b5324d VK |
289 | /** |
290 | * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies. | |
291 | * @policy: cpufreq policy to enable fast frequency switching for. | |
292 | * @freqs: contain details of the frequency update. | |
293 | * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. | |
294 | * | |
295 | * This function calls the transition notifiers and the "adjust_jiffies" | |
296 | * function. It is called twice on all CPU frequency changes that have | |
297 | * external effects. | |
298 | */ | |
299 | static void cpufreq_notify_transition(struct cpufreq_policy *policy, | |
300 | struct cpufreq_freqs *freqs, | |
301 | unsigned int state) | |
1da177e4 LT |
302 | { |
303 | BUG_ON(irqs_disabled()); | |
304 | ||
d5aaffa9 DB |
305 | if (cpufreq_disabled()) |
306 | return; | |
307 | ||
1c3d85dd | 308 | freqs->flags = cpufreq_driver->flags; |
2d06d8c4 | 309 | pr_debug("notification %u of frequency transition to %u kHz\n", |
e837f9b5 | 310 | state, freqs->new); |
1da177e4 | 311 | |
1da177e4 LT |
312 | switch (state) { |
313 | case CPUFREQ_PRECHANGE: | |
20b5324d VK |
314 | /* |
315 | * Detect if the driver reported a value as "old frequency" | |
e4472cb3 DJ |
316 | * which is not equal to what the cpufreq core thinks is |
317 | * "old frequency". | |
1da177e4 | 318 | */ |
1c3d85dd | 319 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
20b5324d | 320 | if (policy->cur && (policy->cur != freqs->old)) { |
e837f9b5 JP |
321 | pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", |
322 | freqs->old, policy->cur); | |
e4472cb3 | 323 | freqs->old = policy->cur; |
1da177e4 LT |
324 | } |
325 | } | |
20b5324d VK |
326 | |
327 | for_each_cpu(freqs->cpu, policy->cpus) { | |
328 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, | |
329 | CPUFREQ_PRECHANGE, freqs); | |
330 | } | |
331 | ||
1da177e4 LT |
332 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
333 | break; | |
e4472cb3 | 334 | |
1da177e4 LT |
335 | case CPUFREQ_POSTCHANGE: |
336 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); | |
20b5324d VK |
337 | pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, |
338 | cpumask_pr_args(policy->cpus)); | |
339 | ||
340 | for_each_cpu(freqs->cpu, policy->cpus) { | |
341 | trace_cpu_frequency(freqs->new, freqs->cpu); | |
342 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, | |
343 | CPUFREQ_POSTCHANGE, freqs); | |
344 | } | |
345 | ||
1aefc75b | 346 | cpufreq_stats_record_transition(policy, freqs->new); |
20b5324d | 347 | policy->cur = freqs->new; |
1da177e4 | 348 | } |
1da177e4 | 349 | } |
bb176f7d | 350 | |
f7ba3b41 | 351 | /* Do post notifications when there are chances that transition has failed */ |
236a9800 | 352 | static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, |
f7ba3b41 VK |
353 | struct cpufreq_freqs *freqs, int transition_failed) |
354 | { | |
355 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
356 | if (!transition_failed) | |
357 | return; | |
358 | ||
359 | swap(freqs->old, freqs->new); | |
360 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
361 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
362 | } | |
f7ba3b41 | 363 | |
12478cf0 SB |
364 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
365 | struct cpufreq_freqs *freqs) | |
366 | { | |
ca654dc3 SB |
367 | |
368 | /* | |
369 | * Catch double invocations of _begin() which lead to self-deadlock. | |
370 | * ASYNC_NOTIFICATION drivers are left out because the cpufreq core | |
371 | * doesn't invoke _begin() on their behalf, and hence the chances of | |
372 | * double invocations are very low. Moreover, there are scenarios | |
373 | * where these checks can emit false-positive warnings in these | |
374 | * drivers; so we avoid that by skipping them altogether. | |
375 | */ | |
376 | WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) | |
377 | && current == policy->transition_task); | |
378 | ||
12478cf0 SB |
379 | wait: |
380 | wait_event(policy->transition_wait, !policy->transition_ongoing); | |
381 | ||
382 | spin_lock(&policy->transition_lock); | |
383 | ||
384 | if (unlikely(policy->transition_ongoing)) { | |
385 | spin_unlock(&policy->transition_lock); | |
386 | goto wait; | |
387 | } | |
388 | ||
389 | policy->transition_ongoing = true; | |
ca654dc3 | 390 | policy->transition_task = current; |
12478cf0 SB |
391 | |
392 | spin_unlock(&policy->transition_lock); | |
393 | ||
394 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
395 | } | |
396 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); | |
397 | ||
398 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, | |
399 | struct cpufreq_freqs *freqs, int transition_failed) | |
400 | { | |
0e7ea2f3 | 401 | if (WARN_ON(!policy->transition_ongoing)) |
12478cf0 SB |
402 | return; |
403 | ||
404 | cpufreq_notify_post_transition(policy, freqs, transition_failed); | |
405 | ||
406 | policy->transition_ongoing = false; | |
ca654dc3 | 407 | policy->transition_task = NULL; |
12478cf0 SB |
408 | |
409 | wake_up(&policy->transition_wait); | |
410 | } | |
411 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); | |
412 | ||
b7898fda RW |
413 | /* |
414 | * Fast frequency switching status count. Positive means "enabled", negative | |
415 | * means "disabled" and 0 means "not decided yet". | |
416 | */ | |
417 | static int cpufreq_fast_switch_count; | |
418 | static DEFINE_MUTEX(cpufreq_fast_switch_lock); | |
419 | ||
420 | static void cpufreq_list_transition_notifiers(void) | |
421 | { | |
422 | struct notifier_block *nb; | |
423 | ||
424 | pr_info("Registered transition notifiers:\n"); | |
425 | ||
426 | mutex_lock(&cpufreq_transition_notifier_list.mutex); | |
427 | ||
428 | for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) | |
429 | pr_info("%pF\n", nb->notifier_call); | |
430 | ||
431 | mutex_unlock(&cpufreq_transition_notifier_list.mutex); | |
432 | } | |
433 | ||
434 | /** | |
435 | * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. | |
436 | * @policy: cpufreq policy to enable fast frequency switching for. | |
437 | * | |
438 | * Try to enable fast frequency switching for @policy. | |
439 | * | |
440 | * The attempt will fail if there is at least one transition notifier registered | |
441 | * at this point, as fast frequency switching is quite fundamentally at odds | |
442 | * with transition notifiers. Thus if successful, it will make registration of | |
443 | * transition notifiers fail going forward. | |
444 | */ | |
445 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) | |
446 | { | |
447 | lockdep_assert_held(&policy->rwsem); | |
448 | ||
449 | if (!policy->fast_switch_possible) | |
450 | return; | |
451 | ||
452 | mutex_lock(&cpufreq_fast_switch_lock); | |
453 | if (cpufreq_fast_switch_count >= 0) { | |
454 | cpufreq_fast_switch_count++; | |
455 | policy->fast_switch_enabled = true; | |
456 | } else { | |
457 | pr_warn("CPU%u: Fast frequency switching not enabled\n", | |
458 | policy->cpu); | |
459 | cpufreq_list_transition_notifiers(); | |
460 | } | |
461 | mutex_unlock(&cpufreq_fast_switch_lock); | |
462 | } | |
463 | EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); | |
464 | ||
6c9d9c81 RW |
465 | /** |
466 | * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. | |
467 | * @policy: cpufreq policy to disable fast frequency switching for. | |
468 | */ | |
469 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) | |
b7898fda RW |
470 | { |
471 | mutex_lock(&cpufreq_fast_switch_lock); | |
472 | if (policy->fast_switch_enabled) { | |
473 | policy->fast_switch_enabled = false; | |
474 | if (!WARN_ON(cpufreq_fast_switch_count <= 0)) | |
475 | cpufreq_fast_switch_count--; | |
476 | } | |
477 | mutex_unlock(&cpufreq_fast_switch_lock); | |
478 | } | |
6c9d9c81 | 479 | EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); |
1da177e4 | 480 | |
e3c06236 SM |
481 | /** |
482 | * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported | |
483 | * one. | |
484 | * @target_freq: target frequency to resolve. | |
485 | * | |
486 | * The target to driver frequency mapping is cached in the policy. | |
487 | * | |
488 | * Return: Lowest driver-supported frequency greater than or equal to the | |
489 | * given target_freq, subject to policy (min/max) and driver limitations. | |
490 | */ | |
491 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, | |
492 | unsigned int target_freq) | |
493 | { | |
494 | target_freq = clamp_val(target_freq, policy->min, policy->max); | |
495 | policy->cached_target_freq = target_freq; | |
abe8bd02 VK |
496 | |
497 | if (cpufreq_driver->target_index) { | |
498 | int idx; | |
499 | ||
500 | idx = cpufreq_frequency_table_target(policy, target_freq, | |
501 | CPUFREQ_RELATION_L); | |
502 | policy->cached_resolved_idx = idx; | |
503 | return policy->freq_table[idx].frequency; | |
504 | } | |
505 | ||
e3c06236 SM |
506 | if (cpufreq_driver->resolve_freq) |
507 | return cpufreq_driver->resolve_freq(policy, target_freq); | |
abe8bd02 VK |
508 | |
509 | return target_freq; | |
e3c06236 | 510 | } |
ae2c1ca6 | 511 | EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); |
e3c06236 | 512 | |
aa7519af VK |
513 | unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) |
514 | { | |
515 | unsigned int latency; | |
516 | ||
517 | if (policy->transition_delay_us) | |
518 | return policy->transition_delay_us; | |
519 | ||
520 | latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; | |
e948bc8f VK |
521 | if (latency) { |
522 | /* | |
523 | * For platforms that can change the frequency very fast (< 10 | |
524 | * us), the above formula gives a decent transition delay. But | |
525 | * for platforms where transition_latency is in milliseconds, it | |
526 | * ends up giving unrealistic values. | |
527 | * | |
528 | * Cap the default transition delay to 10 ms, which seems to be | |
529 | * a reasonable amount of time after which we should reevaluate | |
530 | * the frequency. | |
531 | */ | |
532 | return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); | |
533 | } | |
aa7519af VK |
534 | |
535 | return LATENCY_MULTIPLIER; | |
536 | } | |
537 | EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); | |
538 | ||
1da177e4 LT |
539 | /********************************************************************* |
540 | * SYSFS INTERFACE * | |
541 | *********************************************************************/ | |
8a5c74a1 | 542 | static ssize_t show_boost(struct kobject *kobj, |
625c85a6 | 543 | struct kobj_attribute *attr, char *buf) |
6f19efc0 LM |
544 | { |
545 | return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); | |
546 | } | |
547 | ||
625c85a6 VK |
548 | static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, |
549 | const char *buf, size_t count) | |
6f19efc0 LM |
550 | { |
551 | int ret, enable; | |
552 | ||
553 | ret = sscanf(buf, "%d", &enable); | |
554 | if (ret != 1 || enable < 0 || enable > 1) | |
555 | return -EINVAL; | |
556 | ||
557 | if (cpufreq_boost_trigger_state(enable)) { | |
e837f9b5 JP |
558 | pr_err("%s: Cannot %s BOOST!\n", |
559 | __func__, enable ? "enable" : "disable"); | |
6f19efc0 LM |
560 | return -EINVAL; |
561 | } | |
562 | ||
e837f9b5 JP |
563 | pr_debug("%s: cpufreq BOOST %s\n", |
564 | __func__, enable ? "enabled" : "disabled"); | |
6f19efc0 LM |
565 | |
566 | return count; | |
567 | } | |
568 | define_one_global_rw(boost); | |
1da177e4 | 569 | |
42f91fa1 | 570 | static struct cpufreq_governor *find_governor(const char *str_governor) |
3bcb09a3 JF |
571 | { |
572 | struct cpufreq_governor *t; | |
573 | ||
f7b27061 | 574 | for_each_governor(t) |
7c4f4539 | 575 | if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) |
3bcb09a3 JF |
576 | return t; |
577 | ||
578 | return NULL; | |
579 | } | |
580 | ||
1da177e4 LT |
581 | /** |
582 | * cpufreq_parse_governor - parse a governor string | |
583 | */ | |
ae0ff89f RW |
584 | static int cpufreq_parse_governor(char *str_governor, |
585 | struct cpufreq_policy *policy) | |
1da177e4 | 586 | { |
1c3d85dd | 587 | if (cpufreq_driver->setpolicy) { |
7c4f4539 | 588 | if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { |
ae0ff89f | 589 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
045149e6 RW |
590 | return 0; |
591 | } | |
592 | ||
593 | if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { | |
ae0ff89f | 594 | policy->policy = CPUFREQ_POLICY_POWERSAVE; |
045149e6 | 595 | return 0; |
1da177e4 | 596 | } |
2e1cc3a5 | 597 | } else { |
1da177e4 | 598 | struct cpufreq_governor *t; |
3bcb09a3 | 599 | |
3fc54d37 | 600 | mutex_lock(&cpufreq_governor_mutex); |
3bcb09a3 | 601 | |
42f91fa1 | 602 | t = find_governor(str_governor); |
045149e6 | 603 | if (!t) { |
1a8e1463 | 604 | int ret; |
ea714970 | 605 | |
1a8e1463 | 606 | mutex_unlock(&cpufreq_governor_mutex); |
045149e6 | 607 | |
1a8e1463 | 608 | ret = request_module("cpufreq_%s", str_governor); |
045149e6 RW |
609 | if (ret) |
610 | return -EINVAL; | |
611 | ||
1a8e1463 | 612 | mutex_lock(&cpufreq_governor_mutex); |
ea714970 | 613 | |
045149e6 | 614 | t = find_governor(str_governor); |
ea714970 | 615 | } |
a8b149d3 RW |
616 | if (t && !try_module_get(t->owner)) |
617 | t = NULL; | |
ea714970 | 618 | |
045149e6 RW |
619 | mutex_unlock(&cpufreq_governor_mutex); |
620 | ||
621 | if (t) { | |
ae0ff89f | 622 | policy->governor = t; |
045149e6 | 623 | return 0; |
1da177e4 | 624 | } |
1da177e4 | 625 | } |
045149e6 RW |
626 | |
627 | return -EINVAL; | |
1da177e4 | 628 | } |
1da177e4 | 629 | |
1da177e4 | 630 | /** |
e08f5f5b GS |
631 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
632 | * print out cpufreq information | |
1da177e4 LT |
633 | * |
634 | * Write out information from cpufreq_driver->policy[cpu]; object must be | |
635 | * "unsigned int". | |
636 | */ | |
637 | ||
32ee8c3e DJ |
638 | #define show_one(file_name, object) \ |
639 | static ssize_t show_##file_name \ | |
905d77cd | 640 | (struct cpufreq_policy *policy, char *buf) \ |
32ee8c3e | 641 | { \ |
29464f28 | 642 | return sprintf(buf, "%u\n", policy->object); \ |
1da177e4 LT |
643 | } |
644 | ||
645 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); | |
646 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); | |
ed129784 | 647 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
1da177e4 LT |
648 | show_one(scaling_min_freq, min); |
649 | show_one(scaling_max_freq, max); | |
c034b02e | 650 | |
f8475cef LB |
651 | __weak unsigned int arch_freq_get_on_cpu(int cpu) |
652 | { | |
653 | return 0; | |
654 | } | |
655 | ||
09347b29 | 656 | static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) |
c034b02e DB |
657 | { |
658 | ssize_t ret; | |
f8475cef | 659 | unsigned int freq; |
c034b02e | 660 | |
f8475cef LB |
661 | freq = arch_freq_get_on_cpu(policy->cpu); |
662 | if (freq) | |
663 | ret = sprintf(buf, "%u\n", freq); | |
664 | else if (cpufreq_driver && cpufreq_driver->setpolicy && | |
665 | cpufreq_driver->get) | |
c034b02e DB |
666 | ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
667 | else | |
668 | ret = sprintf(buf, "%u\n", policy->cur); | |
669 | return ret; | |
670 | } | |
1da177e4 | 671 | |
037ce839 | 672 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
3a3e9e06 | 673 | struct cpufreq_policy *new_policy); |
7970e08b | 674 | |
1da177e4 LT |
675 | /** |
676 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access | |
677 | */ | |
678 | #define store_one(file_name, object) \ | |
679 | static ssize_t store_##file_name \ | |
905d77cd | 680 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
1da177e4 | 681 | { \ |
619c144c | 682 | int ret, temp; \ |
1da177e4 LT |
683 | struct cpufreq_policy new_policy; \ |
684 | \ | |
8fa5b631 | 685 | memcpy(&new_policy, policy, sizeof(*policy)); \ |
c7d1f119 TW |
686 | new_policy.min = policy->user_policy.min; \ |
687 | new_policy.max = policy->user_policy.max; \ | |
1da177e4 | 688 | \ |
29464f28 | 689 | ret = sscanf(buf, "%u", &new_policy.object); \ |
1da177e4 LT |
690 | if (ret != 1) \ |
691 | return -EINVAL; \ | |
692 | \ | |
619c144c | 693 | temp = new_policy.object; \ |
037ce839 | 694 | ret = cpufreq_set_policy(policy, &new_policy); \ |
619c144c VH |
695 | if (!ret) \ |
696 | policy->user_policy.object = temp; \ | |
1da177e4 LT |
697 | \ |
698 | return ret ? ret : count; \ | |
699 | } | |
700 | ||
29464f28 DJ |
701 | store_one(scaling_min_freq, min); |
702 | store_one(scaling_max_freq, max); | |
1da177e4 LT |
703 | |
704 | /** | |
705 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware | |
706 | */ | |
905d77cd DJ |
707 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
708 | char *buf) | |
1da177e4 | 709 | { |
d92d50a4 | 710 | unsigned int cur_freq = __cpufreq_get(policy); |
9b4f603e RW |
711 | |
712 | if (cur_freq) | |
713 | return sprintf(buf, "%u\n", cur_freq); | |
714 | ||
715 | return sprintf(buf, "<unknown>\n"); | |
1da177e4 LT |
716 | } |
717 | ||
1da177e4 LT |
718 | /** |
719 | * show_scaling_governor - show the current policy for the specified CPU | |
720 | */ | |
905d77cd | 721 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 722 | { |
29464f28 | 723 | if (policy->policy == CPUFREQ_POLICY_POWERSAVE) |
1da177e4 LT |
724 | return sprintf(buf, "powersave\n"); |
725 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) | |
726 | return sprintf(buf, "performance\n"); | |
727 | else if (policy->governor) | |
4b972f0b | 728 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", |
29464f28 | 729 | policy->governor->name); |
1da177e4 LT |
730 | return -EINVAL; |
731 | } | |
732 | ||
1da177e4 LT |
733 | /** |
734 | * store_scaling_governor - store policy for the specified CPU | |
735 | */ | |
905d77cd DJ |
736 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
737 | const char *buf, size_t count) | |
1da177e4 | 738 | { |
5136fa56 | 739 | int ret; |
1da177e4 LT |
740 | char str_governor[16]; |
741 | struct cpufreq_policy new_policy; | |
742 | ||
8fa5b631 | 743 | memcpy(&new_policy, policy, sizeof(*policy)); |
1da177e4 | 744 | |
29464f28 | 745 | ret = sscanf(buf, "%15s", str_governor); |
1da177e4 LT |
746 | if (ret != 1) |
747 | return -EINVAL; | |
748 | ||
ae0ff89f | 749 | if (cpufreq_parse_governor(str_governor, &new_policy)) |
1da177e4 LT |
750 | return -EINVAL; |
751 | ||
037ce839 | 752 | ret = cpufreq_set_policy(policy, &new_policy); |
a8b149d3 RW |
753 | |
754 | if (new_policy.governor) | |
755 | module_put(new_policy.governor->owner); | |
756 | ||
88dc4384 | 757 | return ret ? ret : count; |
1da177e4 LT |
758 | } |
759 | ||
760 | /** | |
761 | * show_scaling_driver - show the cpufreq driver currently loaded | |
762 | */ | |
905d77cd | 763 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 764 | { |
1c3d85dd | 765 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
1da177e4 LT |
766 | } |
767 | ||
768 | /** | |
769 | * show_scaling_available_governors - show the available CPUfreq governors | |
770 | */ | |
905d77cd DJ |
771 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
772 | char *buf) | |
1da177e4 LT |
773 | { |
774 | ssize_t i = 0; | |
775 | struct cpufreq_governor *t; | |
776 | ||
9c0ebcf7 | 777 | if (!has_target()) { |
1da177e4 LT |
778 | i += sprintf(buf, "performance powersave"); |
779 | goto out; | |
780 | } | |
781 | ||
f7b27061 | 782 | for_each_governor(t) { |
29464f28 DJ |
783 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
784 | - (CPUFREQ_NAME_LEN + 2))) | |
1da177e4 | 785 | goto out; |
4b972f0b | 786 | i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); |
1da177e4 | 787 | } |
7d5e350f | 788 | out: |
1da177e4 LT |
789 | i += sprintf(&buf[i], "\n"); |
790 | return i; | |
791 | } | |
e8628dd0 | 792 | |
f4fd3797 | 793 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) |
1da177e4 LT |
794 | { |
795 | ssize_t i = 0; | |
796 | unsigned int cpu; | |
797 | ||
835481d9 | 798 | for_each_cpu(cpu, mask) { |
1da177e4 LT |
799 | if (i) |
800 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); | |
801 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); | |
802 | if (i >= (PAGE_SIZE - 5)) | |
29464f28 | 803 | break; |
1da177e4 LT |
804 | } |
805 | i += sprintf(&buf[i], "\n"); | |
806 | return i; | |
807 | } | |
f4fd3797 | 808 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
1da177e4 | 809 | |
e8628dd0 DW |
810 | /** |
811 | * show_related_cpus - show the CPUs affected by each transition even if | |
812 | * hw coordination is in use | |
813 | */ | |
814 | static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) | |
815 | { | |
f4fd3797 | 816 | return cpufreq_show_cpus(policy->related_cpus, buf); |
e8628dd0 DW |
817 | } |
818 | ||
819 | /** | |
820 | * show_affected_cpus - show the CPUs affected by each transition | |
821 | */ | |
822 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) | |
823 | { | |
f4fd3797 | 824 | return cpufreq_show_cpus(policy->cpus, buf); |
e8628dd0 DW |
825 | } |
826 | ||
9e76988e | 827 | static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, |
905d77cd | 828 | const char *buf, size_t count) |
9e76988e VP |
829 | { |
830 | unsigned int freq = 0; | |
831 | unsigned int ret; | |
832 | ||
879000f9 | 833 | if (!policy->governor || !policy->governor->store_setspeed) |
9e76988e VP |
834 | return -EINVAL; |
835 | ||
836 | ret = sscanf(buf, "%u", &freq); | |
837 | if (ret != 1) | |
838 | return -EINVAL; | |
839 | ||
840 | policy->governor->store_setspeed(policy, freq); | |
841 | ||
842 | return count; | |
843 | } | |
844 | ||
845 | static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) | |
846 | { | |
879000f9 | 847 | if (!policy->governor || !policy->governor->show_setspeed) |
9e76988e VP |
848 | return sprintf(buf, "<unsupported>\n"); |
849 | ||
850 | return policy->governor->show_setspeed(policy, buf); | |
851 | } | |
1da177e4 | 852 | |
e2f74f35 | 853 | /** |
8bf1ac72 | 854 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
e2f74f35 TR |
855 | */ |
856 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) | |
857 | { | |
858 | unsigned int limit; | |
859 | int ret; | |
1c3d85dd RW |
860 | if (cpufreq_driver->bios_limit) { |
861 | ret = cpufreq_driver->bios_limit(policy->cpu, &limit); | |
e2f74f35 TR |
862 | if (!ret) |
863 | return sprintf(buf, "%u\n", limit); | |
864 | } | |
865 | return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); | |
866 | } | |
867 | ||
6dad2a29 BP |
868 | cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); |
869 | cpufreq_freq_attr_ro(cpuinfo_min_freq); | |
870 | cpufreq_freq_attr_ro(cpuinfo_max_freq); | |
871 | cpufreq_freq_attr_ro(cpuinfo_transition_latency); | |
872 | cpufreq_freq_attr_ro(scaling_available_governors); | |
873 | cpufreq_freq_attr_ro(scaling_driver); | |
874 | cpufreq_freq_attr_ro(scaling_cur_freq); | |
875 | cpufreq_freq_attr_ro(bios_limit); | |
876 | cpufreq_freq_attr_ro(related_cpus); | |
877 | cpufreq_freq_attr_ro(affected_cpus); | |
878 | cpufreq_freq_attr_rw(scaling_min_freq); | |
879 | cpufreq_freq_attr_rw(scaling_max_freq); | |
880 | cpufreq_freq_attr_rw(scaling_governor); | |
881 | cpufreq_freq_attr_rw(scaling_setspeed); | |
1da177e4 | 882 | |
905d77cd | 883 | static struct attribute *default_attrs[] = { |
1da177e4 LT |
884 | &cpuinfo_min_freq.attr, |
885 | &cpuinfo_max_freq.attr, | |
ed129784 | 886 | &cpuinfo_transition_latency.attr, |
1da177e4 LT |
887 | &scaling_min_freq.attr, |
888 | &scaling_max_freq.attr, | |
889 | &affected_cpus.attr, | |
e8628dd0 | 890 | &related_cpus.attr, |
1da177e4 LT |
891 | &scaling_governor.attr, |
892 | &scaling_driver.attr, | |
893 | &scaling_available_governors.attr, | |
9e76988e | 894 | &scaling_setspeed.attr, |
1da177e4 LT |
895 | NULL |
896 | }; | |
897 | ||
29464f28 DJ |
898 | #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) |
899 | #define to_attr(a) container_of(a, struct freq_attr, attr) | |
1da177e4 | 900 | |
29464f28 | 901 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
1da177e4 | 902 | { |
905d77cd DJ |
903 | struct cpufreq_policy *policy = to_policy(kobj); |
904 | struct freq_attr *fattr = to_attr(attr); | |
1b750e3b | 905 | ssize_t ret; |
6eed9404 | 906 | |
ad7722da | 907 | down_read(&policy->rwsem); |
6541aef0 | 908 | ret = fattr->show(policy, buf); |
ad7722da | 909 | up_read(&policy->rwsem); |
1b750e3b | 910 | |
1da177e4 LT |
911 | return ret; |
912 | } | |
913 | ||
905d77cd DJ |
914 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
915 | const char *buf, size_t count) | |
1da177e4 | 916 | { |
905d77cd DJ |
917 | struct cpufreq_policy *policy = to_policy(kobj); |
918 | struct freq_attr *fattr = to_attr(attr); | |
a07530b4 | 919 | ssize_t ret = -EINVAL; |
6eed9404 | 920 | |
9b3d9bb3 WL |
921 | /* |
922 | * cpus_read_trylock() is used here to work around a circular lock | |
923 | * dependency problem with respect to the cpufreq_register_driver(). | |
924 | */ | |
925 | if (!cpus_read_trylock()) | |
926 | return -EBUSY; | |
4f750c93 | 927 | |
6541aef0 RW |
928 | if (cpu_online(policy->cpu)) { |
929 | down_write(&policy->rwsem); | |
e08f5f5b | 930 | ret = fattr->store(policy, buf, count); |
6541aef0 RW |
931 | up_write(&policy->rwsem); |
932 | } | |
e08f5f5b | 933 | |
a92551e4 | 934 | cpus_read_unlock(); |
4f750c93 | 935 | |
1da177e4 LT |
936 | return ret; |
937 | } | |
938 | ||
905d77cd | 939 | static void cpufreq_sysfs_release(struct kobject *kobj) |
1da177e4 | 940 | { |
905d77cd | 941 | struct cpufreq_policy *policy = to_policy(kobj); |
2d06d8c4 | 942 | pr_debug("last reference is dropped\n"); |
1da177e4 LT |
943 | complete(&policy->kobj_unregister); |
944 | } | |
945 | ||
52cf25d0 | 946 | static const struct sysfs_ops sysfs_ops = { |
1da177e4 LT |
947 | .show = show, |
948 | .store = store, | |
949 | }; | |
950 | ||
951 | static struct kobj_type ktype_cpufreq = { | |
952 | .sysfs_ops = &sysfs_ops, | |
953 | .default_attrs = default_attrs, | |
954 | .release = cpufreq_sysfs_release, | |
955 | }; | |
956 | ||
2f0ba790 | 957 | static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) |
87549141 | 958 | { |
2f0ba790 RW |
959 | struct device *dev = get_cpu_device(cpu); |
960 | ||
961 | if (!dev) | |
962 | return; | |
963 | ||
964 | if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) | |
965 | return; | |
966 | ||
26619804 | 967 | dev_dbg(dev, "%s: Adding symlink\n", __func__); |
2f0ba790 RW |
968 | if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) |
969 | dev_err(dev, "cpufreq symlink creation failed\n"); | |
87549141 VK |
970 | } |
971 | ||
26619804 VK |
972 | static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, |
973 | struct device *dev) | |
87549141 | 974 | { |
26619804 VK |
975 | dev_dbg(dev, "%s: Removing symlink\n", __func__); |
976 | sysfs_remove_link(&dev->kobj, "cpufreq"); | |
87549141 VK |
977 | } |
978 | ||
d9612a49 | 979 | static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) |
909a694e DJ |
980 | { |
981 | struct freq_attr **drv_attr; | |
909a694e | 982 | int ret = 0; |
909a694e | 983 | |
909a694e | 984 | /* set up files for this cpu device */ |
1c3d85dd | 985 | drv_attr = cpufreq_driver->attr; |
f13f1184 | 986 | while (drv_attr && *drv_attr) { |
909a694e DJ |
987 | ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
988 | if (ret) | |
6d4e81ed | 989 | return ret; |
909a694e DJ |
990 | drv_attr++; |
991 | } | |
1c3d85dd | 992 | if (cpufreq_driver->get) { |
909a694e DJ |
993 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
994 | if (ret) | |
6d4e81ed | 995 | return ret; |
909a694e | 996 | } |
c034b02e DB |
997 | |
998 | ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); | |
999 | if (ret) | |
6d4e81ed | 1000 | return ret; |
c034b02e | 1001 | |
1c3d85dd | 1002 | if (cpufreq_driver->bios_limit) { |
e2f74f35 TR |
1003 | ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); |
1004 | if (ret) | |
6d4e81ed | 1005 | return ret; |
e2f74f35 | 1006 | } |
909a694e | 1007 | |
26619804 | 1008 | return 0; |
e18f1682 SB |
1009 | } |
1010 | ||
de1df26b RW |
1011 | __weak struct cpufreq_governor *cpufreq_default_governor(void) |
1012 | { | |
1013 | return NULL; | |
1014 | } | |
1015 | ||
7f0fa40f | 1016 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
e18f1682 | 1017 | { |
6e2c89d1 | 1018 | struct cpufreq_governor *gov = NULL; |
e18f1682 | 1019 | struct cpufreq_policy new_policy; |
e18f1682 | 1020 | |
d5b73cd8 | 1021 | memcpy(&new_policy, policy, sizeof(*policy)); |
a27a9ab7 | 1022 | |
6e2c89d1 | 1023 | /* Update governor of new_policy to the governor used before hotplug */ |
4573237b | 1024 | gov = find_governor(policy->last_governor); |
de1df26b | 1025 | if (gov) { |
6e2c89d1 | 1026 | pr_debug("Restoring governor %s for cpu %d\n", |
1027 | policy->governor->name, policy->cpu); | |
de1df26b RW |
1028 | } else { |
1029 | gov = cpufreq_default_governor(); | |
1030 | if (!gov) | |
1031 | return -ENODATA; | |
1032 | } | |
6e2c89d1 | 1033 | |
1034 | new_policy.governor = gov; | |
1035 | ||
69030dd1 SP |
1036 | /* Use the default policy if there is no last_policy. */ |
1037 | if (cpufreq_driver->setpolicy) { | |
1038 | if (policy->last_policy) | |
1039 | new_policy.policy = policy->last_policy; | |
1040 | else | |
ae0ff89f | 1041 | cpufreq_parse_governor(gov->name, &new_policy); |
69030dd1 | 1042 | } |
ecf7e461 | 1043 | /* set default policy */ |
7f0fa40f | 1044 | return cpufreq_set_policy(policy, &new_policy); |
909a694e DJ |
1045 | } |
1046 | ||
d9612a49 | 1047 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
fcf80582 | 1048 | { |
9c0ebcf7 | 1049 | int ret = 0; |
fcf80582 | 1050 | |
bb29ae15 VK |
1051 | /* Has this CPU been taken care of already? */ |
1052 | if (cpumask_test_cpu(cpu, policy->cpus)) | |
1053 | return 0; | |
1054 | ||
49f18560 | 1055 | down_write(&policy->rwsem); |
45482c70 RW |
1056 | if (has_target()) |
1057 | cpufreq_stop_governor(policy); | |
fcf80582 | 1058 | |
fcf80582 | 1059 | cpumask_set_cpu(cpu, policy->cpus); |
2eaa3e2d | 1060 | |
9c0ebcf7 | 1061 | if (has_target()) { |
0a300767 | 1062 | ret = cpufreq_start_governor(policy); |
49f18560 | 1063 | if (ret) |
3de9bdeb | 1064 | pr_err("%s: Failed to start governor\n", __func__); |
820c6ca2 | 1065 | } |
49f18560 VK |
1066 | up_write(&policy->rwsem); |
1067 | return ret; | |
fcf80582 | 1068 | } |
1da177e4 | 1069 | |
11eb69b9 VK |
1070 | static void handle_update(struct work_struct *work) |
1071 | { | |
1072 | struct cpufreq_policy *policy = | |
1073 | container_of(work, struct cpufreq_policy, update); | |
1074 | unsigned int cpu = policy->cpu; | |
1075 | pr_debug("handle_update for cpu %u called\n", cpu); | |
1076 | cpufreq_update_policy(cpu); | |
fcf80582 | 1077 | } |
1da177e4 | 1078 | |
a34e63b1 | 1079 | static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
e9698cc5 SB |
1080 | { |
1081 | struct cpufreq_policy *policy; | |
edd4a893 | 1082 | int ret; |
e9698cc5 SB |
1083 | |
1084 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); | |
1085 | if (!policy) | |
1086 | return NULL; | |
1087 | ||
1088 | if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) | |
1089 | goto err_free_policy; | |
1090 | ||
1091 | if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) | |
1092 | goto err_free_cpumask; | |
1093 | ||
559ed407 RW |
1094 | if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
1095 | goto err_free_rcpumask; | |
1096 | ||
edd4a893 VK |
1097 | ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
1098 | cpufreq_global_kobject, "policy%u", cpu); | |
1099 | if (ret) { | |
1100 | pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); | |
1101 | goto err_free_real_cpus; | |
1102 | } | |
1103 | ||
c88a1f8b | 1104 | INIT_LIST_HEAD(&policy->policy_list); |
ad7722da | 1105 | init_rwsem(&policy->rwsem); |
12478cf0 SB |
1106 | spin_lock_init(&policy->transition_lock); |
1107 | init_waitqueue_head(&policy->transition_wait); | |
818c5712 VK |
1108 | init_completion(&policy->kobj_unregister); |
1109 | INIT_WORK(&policy->update, handle_update); | |
ad7722da | 1110 | |
a34e63b1 | 1111 | policy->cpu = cpu; |
e9698cc5 SB |
1112 | return policy; |
1113 | ||
edd4a893 VK |
1114 | err_free_real_cpus: |
1115 | free_cpumask_var(policy->real_cpus); | |
2fc3384d VK |
1116 | err_free_rcpumask: |
1117 | free_cpumask_var(policy->related_cpus); | |
e9698cc5 SB |
1118 | err_free_cpumask: |
1119 | free_cpumask_var(policy->cpus); | |
1120 | err_free_policy: | |
1121 | kfree(policy); | |
1122 | ||
1123 | return NULL; | |
1124 | } | |
1125 | ||
f9f41e3e | 1126 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) |
42f921a6 VK |
1127 | { |
1128 | struct kobject *kobj; | |
1129 | struct completion *cmp; | |
1130 | ||
87549141 | 1131 | down_write(&policy->rwsem); |
1aefc75b | 1132 | cpufreq_stats_free_table(policy); |
42f921a6 VK |
1133 | kobj = &policy->kobj; |
1134 | cmp = &policy->kobj_unregister; | |
87549141 | 1135 | up_write(&policy->rwsem); |
42f921a6 VK |
1136 | kobject_put(kobj); |
1137 | ||
1138 | /* | |
1139 | * We need to make sure that the underlying kobj is | |
1140 | * actually not referenced anymore by anybody before we | |
1141 | * proceed with unloading. | |
1142 | */ | |
1143 | pr_debug("waiting for dropping of refcount\n"); | |
1144 | wait_for_completion(cmp); | |
1145 | pr_debug("wait complete\n"); | |
1146 | } | |
1147 | ||
f9f41e3e | 1148 | static void cpufreq_policy_free(struct cpufreq_policy *policy) |
e9698cc5 | 1149 | { |
988bed09 VK |
1150 | unsigned long flags; |
1151 | int cpu; | |
1152 | ||
1153 | /* Remove policy from list */ | |
1154 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
1155 | list_del(&policy->policy_list); | |
1156 | ||
1157 | for_each_cpu(cpu, policy->related_cpus) | |
1158 | per_cpu(cpufreq_cpu_data, cpu) = NULL; | |
1159 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1160 | ||
f9f41e3e | 1161 | cpufreq_policy_put_kobj(policy); |
559ed407 | 1162 | free_cpumask_var(policy->real_cpus); |
e9698cc5 SB |
1163 | free_cpumask_var(policy->related_cpus); |
1164 | free_cpumask_var(policy->cpus); | |
1165 | kfree(policy); | |
1166 | } | |
1167 | ||
0b275352 | 1168 | static int cpufreq_online(unsigned int cpu) |
1da177e4 | 1169 | { |
7f0c020a | 1170 | struct cpufreq_policy *policy; |
194d99c7 | 1171 | bool new_policy; |
1da177e4 | 1172 | unsigned long flags; |
0b275352 RW |
1173 | unsigned int j; |
1174 | int ret; | |
87549141 | 1175 | |
0b275352 | 1176 | pr_debug("%s: bringing CPU%u online\n", __func__, cpu); |
6eed9404 | 1177 | |
bb29ae15 | 1178 | /* Check if this CPU already has a policy to manage it */ |
9104bb26 | 1179 | policy = per_cpu(cpufreq_cpu_data, cpu); |
11ce707e | 1180 | if (policy) { |
9104bb26 | 1181 | WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); |
11ce707e | 1182 | if (!policy_is_inactive(policy)) |
d9612a49 | 1183 | return cpufreq_add_policy_cpu(policy, cpu); |
1da177e4 | 1184 | |
11ce707e | 1185 | /* This is the only online CPU for the policy. Start over. */ |
194d99c7 | 1186 | new_policy = false; |
11ce707e RW |
1187 | down_write(&policy->rwsem); |
1188 | policy->cpu = cpu; | |
1189 | policy->governor = NULL; | |
1190 | up_write(&policy->rwsem); | |
1191 | } else { | |
194d99c7 | 1192 | new_policy = true; |
a34e63b1 | 1193 | policy = cpufreq_policy_alloc(cpu); |
72368d12 | 1194 | if (!policy) |
d4d854d6 | 1195 | return -ENOMEM; |
72368d12 | 1196 | } |
0d66b91e | 1197 | |
91a12e91 VK |
1198 | if (!new_policy && cpufreq_driver->online) { |
1199 | ret = cpufreq_driver->online(policy); | |
1200 | if (ret) { | |
1201 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1202 | __LINE__); | |
1203 | goto out_exit_policy; | |
1204 | } | |
1da177e4 | 1205 | |
91a12e91 VK |
1206 | /* Recover policy->cpus using related_cpus */ |
1207 | cpumask_copy(policy->cpus, policy->related_cpus); | |
1208 | } else { | |
1209 | cpumask_copy(policy->cpus, cpumask_of(cpu)); | |
643ae6e8 | 1210 | |
91a12e91 VK |
1211 | /* |
1212 | * Call driver. From then on the cpufreq must be able | |
1213 | * to accept all calls to ->verify and ->setpolicy for this CPU. | |
1214 | */ | |
1215 | ret = cpufreq_driver->init(policy); | |
1216 | if (ret) { | |
1217 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1218 | __LINE__); | |
1219 | goto out_free_policy; | |
1220 | } | |
d417e069 | 1221 | |
91a12e91 VK |
1222 | ret = cpufreq_table_validate_and_sort(policy); |
1223 | if (ret) | |
1224 | goto out_exit_policy; | |
6d4e81ed | 1225 | |
4d1f3a5b | 1226 | /* related_cpus should at least include policy->cpus. */ |
0998a03a | 1227 | cpumask_copy(policy->related_cpus, policy->cpus); |
4d1f3a5b | 1228 | } |
559ed407 | 1229 | |
91a12e91 | 1230 | down_write(&policy->rwsem); |
5a7e56a5 VK |
1231 | /* |
1232 | * affected cpus must always be the one, which are online. We aren't | |
1233 | * managing offline cpus here. | |
1234 | */ | |
1235 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); | |
1236 | ||
194d99c7 | 1237 | if (new_policy) { |
5a7e56a5 VK |
1238 | policy->user_policy.min = policy->min; |
1239 | policy->user_policy.max = policy->max; | |
6d4e81ed | 1240 | |
2f0ba790 | 1241 | for_each_cpu(j, policy->related_cpus) { |
988bed09 | 1242 | per_cpu(cpufreq_cpu_data, j) = policy; |
2f0ba790 RW |
1243 | add_cpu_dev_symlink(policy, j); |
1244 | } | |
ff010472 VK |
1245 | } else { |
1246 | policy->min = policy->user_policy.min; | |
1247 | policy->max = policy->user_policy.max; | |
988bed09 | 1248 | } |
652ed95d | 1249 | |
2ed99e39 | 1250 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
da60ce9f VK |
1251 | policy->cur = cpufreq_driver->get(policy->cpu); |
1252 | if (!policy->cur) { | |
1253 | pr_err("%s: ->get() failed\n", __func__); | |
d417e069 | 1254 | goto out_destroy_policy; |
da60ce9f VK |
1255 | } |
1256 | } | |
1257 | ||
d3916691 VK |
1258 | /* |
1259 | * Sometimes boot loaders set CPU frequency to a value outside of | |
1260 | * frequency table present with cpufreq core. In such cases CPU might be | |
1261 | * unstable if it has to run on that frequency for long duration of time | |
1262 | * and so its better to set it to a frequency which is specified in | |
1263 | * freq-table. This also makes cpufreq stats inconsistent as | |
1264 | * cpufreq-stats would fail to register because current frequency of CPU | |
1265 | * isn't found in freq-table. | |
1266 | * | |
1267 | * Because we don't want this change to effect boot process badly, we go | |
1268 | * for the next freq which is >= policy->cur ('cur' must be set by now, | |
1269 | * otherwise we will end up setting freq to lowest of the table as 'cur' | |
1270 | * is initialized to zero). | |
1271 | * | |
1272 | * We are passing target-freq as "policy->cur - 1" otherwise | |
1273 | * __cpufreq_driver_target() would simply fail, as policy->cur will be | |
1274 | * equal to target-freq. | |
1275 | */ | |
1276 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) | |
1277 | && has_target()) { | |
1278 | /* Are we running at unknown frequency ? */ | |
1279 | ret = cpufreq_frequency_table_get_index(policy, policy->cur); | |
1280 | if (ret == -EINVAL) { | |
1281 | /* Warn user and fix it */ | |
1282 | pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", | |
1283 | __func__, policy->cpu, policy->cur); | |
1284 | ret = __cpufreq_driver_target(policy, policy->cur - 1, | |
1285 | CPUFREQ_RELATION_L); | |
1286 | ||
1287 | /* | |
1288 | * Reaching here after boot in a few seconds may not | |
1289 | * mean that system will remain stable at "unknown" | |
1290 | * frequency for longer duration. Hence, a BUG_ON(). | |
1291 | */ | |
1292 | BUG_ON(ret); | |
1293 | pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", | |
1294 | __func__, policy->cpu, policy->cur); | |
1295 | } | |
1296 | } | |
1297 | ||
194d99c7 | 1298 | if (new_policy) { |
d9612a49 | 1299 | ret = cpufreq_add_dev_interface(policy); |
a82fab29 | 1300 | if (ret) |
d417e069 | 1301 | goto out_destroy_policy; |
1aefc75b RW |
1302 | |
1303 | cpufreq_stats_create_table(policy); | |
8ff69732 | 1304 | |
988bed09 VK |
1305 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1306 | list_add(&policy->policy_list, &cpufreq_policy_list); | |
1307 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1308 | } | |
9515f4d6 | 1309 | |
7f0fa40f VK |
1310 | ret = cpufreq_init_policy(policy); |
1311 | if (ret) { | |
1312 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", | |
1313 | __func__, cpu, ret); | |
d417e069 | 1314 | goto out_destroy_policy; |
08fd8c1c | 1315 | } |
e18f1682 | 1316 | |
4e97b631 | 1317 | up_write(&policy->rwsem); |
08fd8c1c | 1318 | |
038c5b3e | 1319 | kobject_uevent(&policy->kobj, KOBJ_ADD); |
7c45cf31 | 1320 | |
7c45cf31 VK |
1321 | /* Callback for handling stuff after policy is ready */ |
1322 | if (cpufreq_driver->ready) | |
1323 | cpufreq_driver->ready(policy); | |
1324 | ||
5c238a8b AK |
1325 | if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
1326 | cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) | |
1327 | policy->cdev = of_cpufreq_cooling_register(policy); | |
1328 | ||
2d06d8c4 | 1329 | pr_debug("initialization complete\n"); |
87c32271 | 1330 | |
1da177e4 LT |
1331 | return 0; |
1332 | ||
d417e069 | 1333 | out_destroy_policy: |
b24b6478 VK |
1334 | for_each_cpu(j, policy->real_cpus) |
1335 | remove_cpu_dev_symlink(policy, get_cpu_device(j)); | |
1336 | ||
7106e02b PB |
1337 | up_write(&policy->rwsem); |
1338 | ||
d417e069 | 1339 | out_exit_policy: |
da60ce9f VK |
1340 | if (cpufreq_driver->exit) |
1341 | cpufreq_driver->exit(policy); | |
2f0ba790 | 1342 | |
8101f997 | 1343 | out_free_policy: |
f9f41e3e | 1344 | cpufreq_policy_free(policy); |
1da177e4 LT |
1345 | return ret; |
1346 | } | |
1347 | ||
0b275352 RW |
1348 | /** |
1349 | * cpufreq_add_dev - the cpufreq interface for a CPU device. | |
1350 | * @dev: CPU device. | |
1351 | * @sif: Subsystem interface structure pointer (not used) | |
1352 | */ | |
1353 | static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) | |
1354 | { | |
a794d613 | 1355 | struct cpufreq_policy *policy; |
0b275352 | 1356 | unsigned cpu = dev->id; |
26619804 | 1357 | int ret; |
0b275352 RW |
1358 | |
1359 | dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); | |
1360 | ||
26619804 VK |
1361 | if (cpu_online(cpu)) { |
1362 | ret = cpufreq_online(cpu); | |
1363 | if (ret) | |
1364 | return ret; | |
1365 | } | |
0b275352 | 1366 | |
26619804 | 1367 | /* Create sysfs link on CPU registration */ |
a794d613 | 1368 | policy = per_cpu(cpufreq_cpu_data, cpu); |
2f0ba790 RW |
1369 | if (policy) |
1370 | add_cpu_dev_symlink(policy, cpu); | |
26619804 | 1371 | |
2f0ba790 | 1372 | return 0; |
1da177e4 LT |
1373 | } |
1374 | ||
27622b06 | 1375 | static int cpufreq_offline(unsigned int cpu) |
1da177e4 | 1376 | { |
3a3e9e06 | 1377 | struct cpufreq_policy *policy; |
69cee714 | 1378 | int ret; |
1da177e4 | 1379 | |
b8eed8af | 1380 | pr_debug("%s: unregistering CPU %u\n", __func__, cpu); |
1da177e4 | 1381 | |
988bed09 | 1382 | policy = cpufreq_cpu_get_raw(cpu); |
3a3e9e06 | 1383 | if (!policy) { |
b8eed8af | 1384 | pr_debug("%s: No cpu_data found\n", __func__); |
27622b06 | 1385 | return 0; |
1da177e4 | 1386 | } |
1da177e4 | 1387 | |
49f18560 | 1388 | down_write(&policy->rwsem); |
45482c70 RW |
1389 | if (has_target()) |
1390 | cpufreq_stop_governor(policy); | |
1da177e4 | 1391 | |
9591becb | 1392 | cpumask_clear_cpu(cpu, policy->cpus); |
4573237b | 1393 | |
9591becb VK |
1394 | if (policy_is_inactive(policy)) { |
1395 | if (has_target()) | |
1396 | strncpy(policy->last_governor, policy->governor->name, | |
1397 | CPUFREQ_NAME_LEN); | |
69030dd1 SP |
1398 | else |
1399 | policy->last_policy = policy->policy; | |
9591becb VK |
1400 | } else if (cpu == policy->cpu) { |
1401 | /* Nominate new CPU */ | |
1402 | policy->cpu = cpumask_any(policy->cpus); | |
1403 | } | |
084f3493 | 1404 | |
9591becb VK |
1405 | /* Start governor again for active policy */ |
1406 | if (!policy_is_inactive(policy)) { | |
1407 | if (has_target()) { | |
0a300767 | 1408 | ret = cpufreq_start_governor(policy); |
9591becb VK |
1409 | if (ret) |
1410 | pr_err("%s: Failed to start governor\n", __func__); | |
1411 | } | |
cedb70af | 1412 | |
49f18560 | 1413 | goto unlock; |
cedb70af SB |
1414 | } |
1415 | ||
5c238a8b AK |
1416 | if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
1417 | cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) { | |
1418 | cpufreq_cooling_unregister(policy->cdev); | |
1419 | policy->cdev = NULL; | |
1420 | } | |
1421 | ||
69cee714 VK |
1422 | if (cpufreq_driver->stop_cpu) |
1423 | cpufreq_driver->stop_cpu(policy); | |
87549141 | 1424 | |
36be3418 RW |
1425 | if (has_target()) |
1426 | cpufreq_exit_governor(policy); | |
1da177e4 | 1427 | |
87549141 | 1428 | /* |
91a12e91 VK |
1429 | * Perform the ->offline() during light-weight tear-down, as |
1430 | * that allows fast recovery when the CPU comes back. | |
87549141 | 1431 | */ |
91a12e91 VK |
1432 | if (cpufreq_driver->offline) { |
1433 | cpufreq_driver->offline(policy); | |
1434 | } else if (cpufreq_driver->exit) { | |
87549141 | 1435 | cpufreq_driver->exit(policy); |
55582bcc SP |
1436 | policy->freq_table = NULL; |
1437 | } | |
49f18560 VK |
1438 | |
1439 | unlock: | |
1440 | up_write(&policy->rwsem); | |
27622b06 | 1441 | return 0; |
1da177e4 LT |
1442 | } |
1443 | ||
cedb70af | 1444 | /** |
27a862e9 | 1445 | * cpufreq_remove_dev - remove a CPU device |
cedb70af SB |
1446 | * |
1447 | * Removes the cpufreq interface for a CPU device. | |
cedb70af | 1448 | */ |
71db87ba | 1449 | static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) |
5a01f2e8 | 1450 | { |
8a25a2fd | 1451 | unsigned int cpu = dev->id; |
559ed407 | 1452 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
87549141 | 1453 | |
559ed407 | 1454 | if (!policy) |
1af115d6 | 1455 | return; |
87549141 | 1456 | |
69cee714 VK |
1457 | if (cpu_online(cpu)) |
1458 | cpufreq_offline(cpu); | |
87549141 | 1459 | |
559ed407 | 1460 | cpumask_clear_cpu(cpu, policy->real_cpus); |
26619804 | 1461 | remove_cpu_dev_symlink(policy, dev); |
87549141 | 1462 | |
91a12e91 VK |
1463 | if (cpumask_empty(policy->real_cpus)) { |
1464 | /* We did light-weight exit earlier, do full tear down now */ | |
1465 | if (cpufreq_driver->offline) | |
1466 | cpufreq_driver->exit(policy); | |
1467 | ||
f9f41e3e | 1468 | cpufreq_policy_free(policy); |
91a12e91 | 1469 | } |
5a01f2e8 VP |
1470 | } |
1471 | ||
1da177e4 | 1472 | /** |
bb176f7d VK |
1473 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're |
1474 | * in deep trouble. | |
a1e1dc41 | 1475 | * @policy: policy managing CPUs |
1da177e4 LT |
1476 | * @new_freq: CPU frequency the CPU actually runs at |
1477 | * | |
29464f28 DJ |
1478 | * We adjust to current frequency first, and need to clean up later. |
1479 | * So either call to cpufreq_update_policy() or schedule handle_update()). | |
1da177e4 | 1480 | */ |
a1e1dc41 | 1481 | static void cpufreq_out_of_sync(struct cpufreq_policy *policy, |
e08f5f5b | 1482 | unsigned int new_freq) |
1da177e4 LT |
1483 | { |
1484 | struct cpufreq_freqs freqs; | |
b43a7ffb | 1485 | |
e837f9b5 | 1486 | pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", |
a1e1dc41 | 1487 | policy->cur, new_freq); |
1da177e4 | 1488 | |
a1e1dc41 | 1489 | freqs.old = policy->cur; |
1da177e4 | 1490 | freqs.new = new_freq; |
b43a7ffb | 1491 | |
8fec051e VK |
1492 | cpufreq_freq_transition_begin(policy, &freqs); |
1493 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
1da177e4 LT |
1494 | } |
1495 | ||
32ee8c3e | 1496 | /** |
4ab70df4 | 1497 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
95235ca2 VP |
1498 | * @cpu: CPU number |
1499 | * | |
1500 | * This is the last known freq, without actually getting it from the driver. | |
1501 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. | |
1502 | */ | |
1503 | unsigned int cpufreq_quick_get(unsigned int cpu) | |
1504 | { | |
9e21ba8b | 1505 | struct cpufreq_policy *policy; |
e08f5f5b | 1506 | unsigned int ret_freq = 0; |
c75361c0 | 1507 | unsigned long flags; |
95235ca2 | 1508 | |
c75361c0 RC |
1509 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1510 | ||
1511 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { | |
1512 | ret_freq = cpufreq_driver->get(cpu); | |
1513 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1514 | return ret_freq; | |
1515 | } | |
1516 | ||
1517 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
9e21ba8b DB |
1518 | |
1519 | policy = cpufreq_cpu_get(cpu); | |
95235ca2 | 1520 | if (policy) { |
e08f5f5b | 1521 | ret_freq = policy->cur; |
95235ca2 VP |
1522 | cpufreq_cpu_put(policy); |
1523 | } | |
1524 | ||
4d34a67d | 1525 | return ret_freq; |
95235ca2 VP |
1526 | } |
1527 | EXPORT_SYMBOL(cpufreq_quick_get); | |
1528 | ||
3d737108 JB |
1529 | /** |
1530 | * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU | |
1531 | * @cpu: CPU number | |
1532 | * | |
1533 | * Just return the max possible frequency for a given CPU. | |
1534 | */ | |
1535 | unsigned int cpufreq_quick_get_max(unsigned int cpu) | |
1536 | { | |
1537 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); | |
1538 | unsigned int ret_freq = 0; | |
1539 | ||
1540 | if (policy) { | |
1541 | ret_freq = policy->max; | |
1542 | cpufreq_cpu_put(policy); | |
1543 | } | |
1544 | ||
1545 | return ret_freq; | |
1546 | } | |
1547 | EXPORT_SYMBOL(cpufreq_quick_get_max); | |
1548 | ||
d92d50a4 | 1549 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
1da177e4 | 1550 | { |
e08f5f5b | 1551 | unsigned int ret_freq = 0; |
5800043b | 1552 | |
2f661962 | 1553 | if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get) |
4d34a67d | 1554 | return ret_freq; |
1da177e4 | 1555 | |
d92d50a4 | 1556 | ret_freq = cpufreq_driver->get(policy->cpu); |
1da177e4 | 1557 | |
b7898fda | 1558 | /* |
2f661962 | 1559 | * If fast frequency switching is used with the given policy, the check |
b7898fda RW |
1560 | * against policy->cur is pointless, so skip it in that case too. |
1561 | */ | |
2f661962 | 1562 | if (policy->fast_switch_enabled) |
11e584cf VK |
1563 | return ret_freq; |
1564 | ||
e08f5f5b | 1565 | if (ret_freq && policy->cur && |
1c3d85dd | 1566 | !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
e08f5f5b GS |
1567 | /* verify no discrepancy between actual and |
1568 | saved value exists */ | |
1569 | if (unlikely(ret_freq != policy->cur)) { | |
a1e1dc41 | 1570 | cpufreq_out_of_sync(policy, ret_freq); |
1da177e4 LT |
1571 | schedule_work(&policy->update); |
1572 | } | |
1573 | } | |
1574 | ||
4d34a67d | 1575 | return ret_freq; |
5a01f2e8 | 1576 | } |
1da177e4 | 1577 | |
5a01f2e8 VP |
1578 | /** |
1579 | * cpufreq_get - get the current CPU frequency (in kHz) | |
1580 | * @cpu: CPU number | |
1581 | * | |
1582 | * Get the CPU current (static) CPU frequency | |
1583 | */ | |
1584 | unsigned int cpufreq_get(unsigned int cpu) | |
1585 | { | |
999976e0 | 1586 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
5a01f2e8 | 1587 | unsigned int ret_freq = 0; |
5a01f2e8 | 1588 | |
999976e0 AP |
1589 | if (policy) { |
1590 | down_read(&policy->rwsem); | |
2f661962 | 1591 | ret_freq = __cpufreq_get(policy); |
999976e0 | 1592 | up_read(&policy->rwsem); |
5a01f2e8 | 1593 | |
999976e0 AP |
1594 | cpufreq_cpu_put(policy); |
1595 | } | |
6eed9404 | 1596 | |
4d34a67d | 1597 | return ret_freq; |
1da177e4 LT |
1598 | } |
1599 | EXPORT_SYMBOL(cpufreq_get); | |
1600 | ||
999f5729 RW |
1601 | static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy) |
1602 | { | |
1603 | unsigned int new_freq; | |
1604 | ||
1605 | new_freq = cpufreq_driver->get(policy->cpu); | |
1606 | if (!new_freq) | |
1607 | return 0; | |
1608 | ||
1609 | if (!policy->cur) { | |
1610 | pr_debug("cpufreq: Driver did not initialize current freq\n"); | |
1611 | policy->cur = new_freq; | |
1612 | } else if (policy->cur != new_freq && has_target()) { | |
1613 | cpufreq_out_of_sync(policy, new_freq); | |
1614 | } | |
1615 | ||
1616 | return new_freq; | |
1617 | } | |
1618 | ||
8a25a2fd KS |
1619 | static struct subsys_interface cpufreq_interface = { |
1620 | .name = "cpufreq", | |
1621 | .subsys = &cpu_subsys, | |
1622 | .add_dev = cpufreq_add_dev, | |
1623 | .remove_dev = cpufreq_remove_dev, | |
e00e56df RW |
1624 | }; |
1625 | ||
e28867ea VK |
1626 | /* |
1627 | * In case platform wants some specific frequency to be configured | |
1628 | * during suspend.. | |
1629 | */ | |
1630 | int cpufreq_generic_suspend(struct cpufreq_policy *policy) | |
1631 | { | |
1632 | int ret; | |
1633 | ||
1634 | if (!policy->suspend_freq) { | |
201f3716 BZ |
1635 | pr_debug("%s: suspend_freq not defined\n", __func__); |
1636 | return 0; | |
e28867ea VK |
1637 | } |
1638 | ||
1639 | pr_debug("%s: Setting suspend-freq: %u\n", __func__, | |
1640 | policy->suspend_freq); | |
1641 | ||
1642 | ret = __cpufreq_driver_target(policy, policy->suspend_freq, | |
1643 | CPUFREQ_RELATION_H); | |
1644 | if (ret) | |
1645 | pr_err("%s: unable to set suspend-freq: %u. err: %d\n", | |
1646 | __func__, policy->suspend_freq, ret); | |
1647 | ||
1648 | return ret; | |
1649 | } | |
1650 | EXPORT_SYMBOL(cpufreq_generic_suspend); | |
1651 | ||
42d4dc3f | 1652 | /** |
2f0aea93 | 1653 | * cpufreq_suspend() - Suspend CPUFreq governors |
e00e56df | 1654 | * |
2f0aea93 VK |
1655 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
1656 | * as some platforms can't change frequency after this point in suspend cycle. | |
1657 | * Because some of the devices (like: i2c, regulators, etc) they use for | |
1658 | * changing frequency are suspended quickly after this point. | |
42d4dc3f | 1659 | */ |
2f0aea93 | 1660 | void cpufreq_suspend(void) |
42d4dc3f | 1661 | { |
3a3e9e06 | 1662 | struct cpufreq_policy *policy; |
42d4dc3f | 1663 | |
2f0aea93 VK |
1664 | if (!cpufreq_driver) |
1665 | return; | |
42d4dc3f | 1666 | |
ba41e1bc | 1667 | if (!has_target() && !cpufreq_driver->suspend) |
b1b12bab | 1668 | goto suspend; |
42d4dc3f | 1669 | |
2f0aea93 VK |
1670 | pr_debug("%s: Suspending Governors\n", __func__); |
1671 | ||
f963735a | 1672 | for_each_active_policy(policy) { |
ba41e1bc RW |
1673 | if (has_target()) { |
1674 | down_write(&policy->rwsem); | |
45482c70 | 1675 | cpufreq_stop_governor(policy); |
ba41e1bc | 1676 | up_write(&policy->rwsem); |
ba41e1bc RW |
1677 | } |
1678 | ||
1679 | if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) | |
2f0aea93 VK |
1680 | pr_err("%s: Failed to suspend driver: %p\n", __func__, |
1681 | policy); | |
42d4dc3f | 1682 | } |
b1b12bab VK |
1683 | |
1684 | suspend: | |
1685 | cpufreq_suspended = true; | |
42d4dc3f BH |
1686 | } |
1687 | ||
1da177e4 | 1688 | /** |
2f0aea93 | 1689 | * cpufreq_resume() - Resume CPUFreq governors |
1da177e4 | 1690 | * |
2f0aea93 VK |
1691 | * Called during system wide Suspend/Hibernate cycle for resuming governors that |
1692 | * are suspended with cpufreq_suspend(). | |
1da177e4 | 1693 | */ |
2f0aea93 | 1694 | void cpufreq_resume(void) |
1da177e4 | 1695 | { |
3a3e9e06 | 1696 | struct cpufreq_policy *policy; |
49f18560 | 1697 | int ret; |
1da177e4 | 1698 | |
2f0aea93 | 1699 | if (!cpufreq_driver) |
703cbaa6 BY |
1700 | return; |
1701 | ||
1702 | if (unlikely(!cpufreq_suspended)) | |
2f0aea93 | 1703 | return; |
1da177e4 | 1704 | |
8e30444e LT |
1705 | cpufreq_suspended = false; |
1706 | ||
ba41e1bc | 1707 | if (!has_target() && !cpufreq_driver->resume) |
e00e56df | 1708 | return; |
1da177e4 | 1709 | |
2f0aea93 | 1710 | pr_debug("%s: Resuming Governors\n", __func__); |
1da177e4 | 1711 | |
f963735a | 1712 | for_each_active_policy(policy) { |
49f18560 | 1713 | if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { |
0c5aa405 VK |
1714 | pr_err("%s: Failed to resume driver: %p\n", __func__, |
1715 | policy); | |
ba41e1bc | 1716 | } else if (has_target()) { |
49f18560 | 1717 | down_write(&policy->rwsem); |
0a300767 | 1718 | ret = cpufreq_start_governor(policy); |
49f18560 VK |
1719 | up_write(&policy->rwsem); |
1720 | ||
1721 | if (ret) | |
1722 | pr_err("%s: Failed to start governor for policy: %p\n", | |
1723 | __func__, policy); | |
1724 | } | |
2f0aea93 VK |
1725 | } |
1726 | } | |
1da177e4 | 1727 | |
9d95046e BP |
1728 | /** |
1729 | * cpufreq_get_current_driver - return current driver's name | |
1730 | * | |
1731 | * Return the name string of the currently loaded cpufreq driver | |
1732 | * or NULL, if none. | |
1733 | */ | |
1734 | const char *cpufreq_get_current_driver(void) | |
1735 | { | |
1c3d85dd RW |
1736 | if (cpufreq_driver) |
1737 | return cpufreq_driver->name; | |
1738 | ||
1739 | return NULL; | |
9d95046e BP |
1740 | } |
1741 | EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); | |
1da177e4 | 1742 | |
51315cdf TP |
1743 | /** |
1744 | * cpufreq_get_driver_data - return current driver data | |
1745 | * | |
1746 | * Return the private data of the currently loaded cpufreq | |
1747 | * driver, or NULL if no cpufreq driver is loaded. | |
1748 | */ | |
1749 | void *cpufreq_get_driver_data(void) | |
1750 | { | |
1751 | if (cpufreq_driver) | |
1752 | return cpufreq_driver->driver_data; | |
1753 | ||
1754 | return NULL; | |
1755 | } | |
1756 | EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); | |
1757 | ||
1da177e4 LT |
1758 | /********************************************************************* |
1759 | * NOTIFIER LISTS INTERFACE * | |
1760 | *********************************************************************/ | |
1761 | ||
1762 | /** | |
1763 | * cpufreq_register_notifier - register a driver with cpufreq | |
1764 | * @nb: notifier function to register | |
1765 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER | |
1766 | * | |
32ee8c3e | 1767 | * Add a driver to one of two lists: either a list of drivers that |
1da177e4 LT |
1768 | * are notified about clock rate changes (once before and once after |
1769 | * the transition), or a list of drivers that are notified about | |
1770 | * changes in cpufreq policy. | |
1771 | * | |
1772 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1773 | * blocking_notifier_chain_register. |
1da177e4 LT |
1774 | */ |
1775 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) | |
1776 | { | |
1777 | int ret; | |
1778 | ||
d5aaffa9 DB |
1779 | if (cpufreq_disabled()) |
1780 | return -EINVAL; | |
1781 | ||
1da177e4 LT |
1782 | switch (list) { |
1783 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
1784 | mutex_lock(&cpufreq_fast_switch_lock); |
1785 | ||
1786 | if (cpufreq_fast_switch_count > 0) { | |
1787 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1788 | return -EBUSY; | |
1789 | } | |
b4dfdbb3 | 1790 | ret = srcu_notifier_chain_register( |
e041c683 | 1791 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
1792 | if (!ret) |
1793 | cpufreq_fast_switch_count--; | |
1794 | ||
1795 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
1796 | break; |
1797 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1798 | ret = blocking_notifier_chain_register( |
1799 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1800 | break; |
1801 | default: | |
1802 | ret = -EINVAL; | |
1803 | } | |
1da177e4 LT |
1804 | |
1805 | return ret; | |
1806 | } | |
1807 | EXPORT_SYMBOL(cpufreq_register_notifier); | |
1808 | ||
1da177e4 LT |
1809 | /** |
1810 | * cpufreq_unregister_notifier - unregister a driver with cpufreq | |
1811 | * @nb: notifier block to be unregistered | |
bb176f7d | 1812 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
1da177e4 LT |
1813 | * |
1814 | * Remove a driver from the CPU frequency notifier list. | |
1815 | * | |
1816 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1817 | * blocking_notifier_chain_unregister. |
1da177e4 LT |
1818 | */ |
1819 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) | |
1820 | { | |
1821 | int ret; | |
1822 | ||
d5aaffa9 DB |
1823 | if (cpufreq_disabled()) |
1824 | return -EINVAL; | |
1825 | ||
1da177e4 LT |
1826 | switch (list) { |
1827 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
1828 | mutex_lock(&cpufreq_fast_switch_lock); |
1829 | ||
b4dfdbb3 | 1830 | ret = srcu_notifier_chain_unregister( |
e041c683 | 1831 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
1832 | if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) |
1833 | cpufreq_fast_switch_count++; | |
1834 | ||
1835 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
1836 | break; |
1837 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1838 | ret = blocking_notifier_chain_unregister( |
1839 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1840 | break; |
1841 | default: | |
1842 | ret = -EINVAL; | |
1843 | } | |
1da177e4 LT |
1844 | |
1845 | return ret; | |
1846 | } | |
1847 | EXPORT_SYMBOL(cpufreq_unregister_notifier); | |
1848 | ||
1849 | ||
1850 | /********************************************************************* | |
1851 | * GOVERNORS * | |
1852 | *********************************************************************/ | |
1853 | ||
b7898fda RW |
1854 | /** |
1855 | * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. | |
1856 | * @policy: cpufreq policy to switch the frequency for. | |
1857 | * @target_freq: New frequency to set (may be approximate). | |
1858 | * | |
1859 | * Carry out a fast frequency switch without sleeping. | |
1860 | * | |
1861 | * The driver's ->fast_switch() callback invoked by this function must be | |
1862 | * suitable for being called from within RCU-sched read-side critical sections | |
1863 | * and it is expected to select the minimum available frequency greater than or | |
1864 | * equal to @target_freq (CPUFREQ_RELATION_L). | |
1865 | * | |
1866 | * This function must not be called if policy->fast_switch_enabled is unset. | |
1867 | * | |
1868 | * Governors calling this function must guarantee that it will never be invoked | |
1869 | * twice in parallel for the same policy and that it will never be called in | |
1870 | * parallel with either ->target() or ->target_index() for the same policy. | |
1871 | * | |
209887e6 VK |
1872 | * Returns the actual frequency set for the CPU. |
1873 | * | |
1874 | * If 0 is returned by the driver's ->fast_switch() callback to indicate an | |
1875 | * error condition, the hardware configuration must be preserved. | |
b7898fda RW |
1876 | */ |
1877 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, | |
1878 | unsigned int target_freq) | |
1879 | { | |
b9af6948 | 1880 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
b7898fda RW |
1881 | |
1882 | return cpufreq_driver->fast_switch(policy, target_freq); | |
1883 | } | |
1884 | EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); | |
1885 | ||
1c03a2d0 VK |
1886 | /* Must set freqs->new to intermediate frequency */ |
1887 | static int __target_intermediate(struct cpufreq_policy *policy, | |
1888 | struct cpufreq_freqs *freqs, int index) | |
1889 | { | |
1890 | int ret; | |
1891 | ||
1892 | freqs->new = cpufreq_driver->get_intermediate(policy, index); | |
1893 | ||
1894 | /* We don't need to switch to intermediate freq */ | |
1895 | if (!freqs->new) | |
1896 | return 0; | |
1897 | ||
1898 | pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", | |
1899 | __func__, policy->cpu, freqs->old, freqs->new); | |
1900 | ||
1901 | cpufreq_freq_transition_begin(policy, freqs); | |
1902 | ret = cpufreq_driver->target_intermediate(policy, index); | |
1903 | cpufreq_freq_transition_end(policy, freqs, ret); | |
1904 | ||
1905 | if (ret) | |
1906 | pr_err("%s: Failed to change to intermediate frequency: %d\n", | |
1907 | __func__, ret); | |
1908 | ||
1909 | return ret; | |
1910 | } | |
1911 | ||
23727845 | 1912 | static int __target_index(struct cpufreq_policy *policy, int index) |
8d65775d | 1913 | { |
1c03a2d0 VK |
1914 | struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; |
1915 | unsigned int intermediate_freq = 0; | |
23727845 | 1916 | unsigned int newfreq = policy->freq_table[index].frequency; |
8d65775d VK |
1917 | int retval = -EINVAL; |
1918 | bool notify; | |
1919 | ||
23727845 VK |
1920 | if (newfreq == policy->cur) |
1921 | return 0; | |
1922 | ||
8d65775d | 1923 | notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); |
8d65775d | 1924 | if (notify) { |
1c03a2d0 VK |
1925 | /* Handle switching to intermediate frequency */ |
1926 | if (cpufreq_driver->get_intermediate) { | |
1927 | retval = __target_intermediate(policy, &freqs, index); | |
1928 | if (retval) | |
1929 | return retval; | |
1930 | ||
1931 | intermediate_freq = freqs.new; | |
1932 | /* Set old freq to intermediate */ | |
1933 | if (intermediate_freq) | |
1934 | freqs.old = freqs.new; | |
1935 | } | |
8d65775d | 1936 | |
23727845 | 1937 | freqs.new = newfreq; |
8d65775d VK |
1938 | pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", |
1939 | __func__, policy->cpu, freqs.old, freqs.new); | |
1940 | ||
1941 | cpufreq_freq_transition_begin(policy, &freqs); | |
1942 | } | |
1943 | ||
1944 | retval = cpufreq_driver->target_index(policy, index); | |
1945 | if (retval) | |
1946 | pr_err("%s: Failed to change cpu frequency: %d\n", __func__, | |
1947 | retval); | |
1948 | ||
1c03a2d0 | 1949 | if (notify) { |
8d65775d VK |
1950 | cpufreq_freq_transition_end(policy, &freqs, retval); |
1951 | ||
1c03a2d0 VK |
1952 | /* |
1953 | * Failed after setting to intermediate freq? Driver should have | |
1954 | * reverted back to initial frequency and so should we. Check | |
1955 | * here for intermediate_freq instead of get_intermediate, in | |
58405af6 | 1956 | * case we haven't switched to intermediate freq at all. |
1c03a2d0 VK |
1957 | */ |
1958 | if (unlikely(retval && intermediate_freq)) { | |
1959 | freqs.old = intermediate_freq; | |
1960 | freqs.new = policy->restore_freq; | |
1961 | cpufreq_freq_transition_begin(policy, &freqs); | |
1962 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
1963 | } | |
1964 | } | |
1965 | ||
8d65775d VK |
1966 | return retval; |
1967 | } | |
1968 | ||
1da177e4 LT |
1969 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
1970 | unsigned int target_freq, | |
1971 | unsigned int relation) | |
1972 | { | |
7249924e | 1973 | unsigned int old_target_freq = target_freq; |
d218ed77 | 1974 | int index; |
c32b6b8e | 1975 | |
a7b422cd KRW |
1976 | if (cpufreq_disabled()) |
1977 | return -ENODEV; | |
1978 | ||
7249924e | 1979 | /* Make sure that target_freq is within supported range */ |
910c6e88 | 1980 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
7249924e VK |
1981 | |
1982 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", | |
e837f9b5 | 1983 | policy->cpu, target_freq, relation, old_target_freq); |
5a1c0228 | 1984 | |
9c0ebcf7 VK |
1985 | /* |
1986 | * This might look like a redundant call as we are checking it again | |
1987 | * after finding index. But it is left intentionally for cases where | |
1988 | * exactly same freq is called again and so we can save on few function | |
1989 | * calls. | |
1990 | */ | |
5a1c0228 VK |
1991 | if (target_freq == policy->cur) |
1992 | return 0; | |
1993 | ||
1c03a2d0 VK |
1994 | /* Save last value to restore later on errors */ |
1995 | policy->restore_freq = policy->cur; | |
1996 | ||
1c3d85dd | 1997 | if (cpufreq_driver->target) |
6019d23a | 1998 | return cpufreq_driver->target(policy, target_freq, relation); |
9c0ebcf7 | 1999 | |
6019d23a RW |
2000 | if (!cpufreq_driver->target_index) |
2001 | return -EINVAL; | |
9c0ebcf7 | 2002 | |
d218ed77 | 2003 | index = cpufreq_frequency_table_target(policy, target_freq, relation); |
6019d23a | 2004 | |
23727845 | 2005 | return __target_index(policy, index); |
1da177e4 LT |
2006 | } |
2007 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); | |
2008 | ||
1da177e4 LT |
2009 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
2010 | unsigned int target_freq, | |
2011 | unsigned int relation) | |
2012 | { | |
f1829e4a | 2013 | int ret = -EINVAL; |
1da177e4 | 2014 | |
ad7722da | 2015 | down_write(&policy->rwsem); |
1da177e4 LT |
2016 | |
2017 | ret = __cpufreq_driver_target(policy, target_freq, relation); | |
2018 | ||
ad7722da | 2019 | up_write(&policy->rwsem); |
1da177e4 | 2020 | |
1da177e4 LT |
2021 | return ret; |
2022 | } | |
2023 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); | |
2024 | ||
de1df26b RW |
2025 | __weak struct cpufreq_governor *cpufreq_fallback_governor(void) |
2026 | { | |
2027 | return NULL; | |
2028 | } | |
2029 | ||
a92604b4 | 2030 | static int cpufreq_init_governor(struct cpufreq_policy *policy) |
1da177e4 | 2031 | { |
cc993cab | 2032 | int ret; |
6afde10c | 2033 | |
2f0aea93 VK |
2034 | /* Don't start any governor operations if we are entering suspend */ |
2035 | if (cpufreq_suspended) | |
2036 | return 0; | |
cb57720b EZ |
2037 | /* |
2038 | * Governor might not be initiated here if ACPI _PPC changed | |
2039 | * notification happened, so check it. | |
2040 | */ | |
2041 | if (!policy->governor) | |
2042 | return -EINVAL; | |
2f0aea93 | 2043 | |
ed4676e2 VK |
2044 | /* Platform doesn't want dynamic frequency switching ? */ |
2045 | if (policy->governor->dynamic_switching && | |
fc4c709f | 2046 | cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { |
de1df26b RW |
2047 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
2048 | ||
2049 | if (gov) { | |
fe829ed8 | 2050 | pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", |
e837f9b5 | 2051 | policy->governor->name, gov->name); |
6afde10c | 2052 | policy->governor = gov; |
de1df26b RW |
2053 | } else { |
2054 | return -EINVAL; | |
6afde10c | 2055 | } |
1c256245 | 2056 | } |
1da177e4 | 2057 | |
a92604b4 RW |
2058 | if (!try_module_get(policy->governor->owner)) |
2059 | return -EINVAL; | |
95731ebb | 2060 | |
a92604b4 | 2061 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
1da177e4 | 2062 | |
e788892b RW |
2063 | if (policy->governor->init) { |
2064 | ret = policy->governor->init(policy); | |
2065 | if (ret) { | |
36be3418 | 2066 | module_put(policy->governor->owner); |
e788892b RW |
2067 | return ret; |
2068 | } | |
36be3418 | 2069 | } |
1da177e4 | 2070 | |
a92604b4 RW |
2071 | return 0; |
2072 | } | |
2073 | ||
2074 | static void cpufreq_exit_governor(struct cpufreq_policy *policy) | |
2075 | { | |
2076 | if (cpufreq_suspended || !policy->governor) | |
2077 | return; | |
2078 | ||
2079 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2080 | ||
e788892b RW |
2081 | if (policy->governor->exit) |
2082 | policy->governor->exit(policy); | |
a92604b4 | 2083 | |
a92604b4 | 2084 | module_put(policy->governor->owner); |
1da177e4 LT |
2085 | } |
2086 | ||
0a300767 RW |
2087 | static int cpufreq_start_governor(struct cpufreq_policy *policy) |
2088 | { | |
2089 | int ret; | |
2090 | ||
a92604b4 RW |
2091 | if (cpufreq_suspended) |
2092 | return 0; | |
2093 | ||
2094 | if (!policy->governor) | |
2095 | return -EINVAL; | |
2096 | ||
2097 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2098 | ||
3bbf8fe3 RW |
2099 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) |
2100 | cpufreq_update_current_freq(policy); | |
2101 | ||
e788892b RW |
2102 | if (policy->governor->start) { |
2103 | ret = policy->governor->start(policy); | |
2104 | if (ret) | |
2105 | return ret; | |
2106 | } | |
2107 | ||
2108 | if (policy->governor->limits) | |
2109 | policy->governor->limits(policy); | |
d6ff44d6 | 2110 | |
d6ff44d6 | 2111 | return 0; |
0a300767 RW |
2112 | } |
2113 | ||
a92604b4 RW |
2114 | static void cpufreq_stop_governor(struct cpufreq_policy *policy) |
2115 | { | |
2116 | if (cpufreq_suspended || !policy->governor) | |
2117 | return; | |
2118 | ||
2119 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2120 | ||
e788892b RW |
2121 | if (policy->governor->stop) |
2122 | policy->governor->stop(policy); | |
a92604b4 RW |
2123 | } |
2124 | ||
2125 | static void cpufreq_governor_limits(struct cpufreq_policy *policy) | |
2126 | { | |
2127 | if (cpufreq_suspended || !policy->governor) | |
2128 | return; | |
2129 | ||
2130 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2131 | ||
e788892b RW |
2132 | if (policy->governor->limits) |
2133 | policy->governor->limits(policy); | |
0a300767 RW |
2134 | } |
2135 | ||
1da177e4 LT |
2136 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
2137 | { | |
3bcb09a3 | 2138 | int err; |
1da177e4 LT |
2139 | |
2140 | if (!governor) | |
2141 | return -EINVAL; | |
2142 | ||
a7b422cd KRW |
2143 | if (cpufreq_disabled()) |
2144 | return -ENODEV; | |
2145 | ||
3fc54d37 | 2146 | mutex_lock(&cpufreq_governor_mutex); |
32ee8c3e | 2147 | |
3bcb09a3 | 2148 | err = -EBUSY; |
42f91fa1 | 2149 | if (!find_governor(governor->name)) { |
3bcb09a3 JF |
2150 | err = 0; |
2151 | list_add(&governor->governor_list, &cpufreq_governor_list); | |
1da177e4 | 2152 | } |
1da177e4 | 2153 | |
32ee8c3e | 2154 | mutex_unlock(&cpufreq_governor_mutex); |
3bcb09a3 | 2155 | return err; |
1da177e4 LT |
2156 | } |
2157 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); | |
2158 | ||
1da177e4 LT |
2159 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
2160 | { | |
4573237b VK |
2161 | struct cpufreq_policy *policy; |
2162 | unsigned long flags; | |
90e41bac | 2163 | |
1da177e4 LT |
2164 | if (!governor) |
2165 | return; | |
2166 | ||
a7b422cd KRW |
2167 | if (cpufreq_disabled()) |
2168 | return; | |
2169 | ||
4573237b VK |
2170 | /* clear last_governor for all inactive policies */ |
2171 | read_lock_irqsave(&cpufreq_driver_lock, flags); | |
2172 | for_each_inactive_policy(policy) { | |
18bf3a12 VK |
2173 | if (!strcmp(policy->last_governor, governor->name)) { |
2174 | policy->governor = NULL; | |
4573237b | 2175 | strcpy(policy->last_governor, "\0"); |
18bf3a12 | 2176 | } |
90e41bac | 2177 | } |
4573237b | 2178 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
90e41bac | 2179 | |
3fc54d37 | 2180 | mutex_lock(&cpufreq_governor_mutex); |
1da177e4 | 2181 | list_del(&governor->governor_list); |
3fc54d37 | 2182 | mutex_unlock(&cpufreq_governor_mutex); |
1da177e4 LT |
2183 | } |
2184 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); | |
2185 | ||
2186 | ||
1da177e4 LT |
2187 | /********************************************************************* |
2188 | * POLICY INTERFACE * | |
2189 | *********************************************************************/ | |
2190 | ||
2191 | /** | |
2192 | * cpufreq_get_policy - get the current cpufreq_policy | |
29464f28 DJ |
2193 | * @policy: struct cpufreq_policy into which the current cpufreq_policy |
2194 | * is written | |
1da177e4 LT |
2195 | * |
2196 | * Reads the current cpufreq policy. | |
2197 | */ | |
2198 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) | |
2199 | { | |
2200 | struct cpufreq_policy *cpu_policy; | |
2201 | if (!policy) | |
2202 | return -EINVAL; | |
2203 | ||
2204 | cpu_policy = cpufreq_cpu_get(cpu); | |
2205 | if (!cpu_policy) | |
2206 | return -EINVAL; | |
2207 | ||
d5b73cd8 | 2208 | memcpy(policy, cpu_policy, sizeof(*policy)); |
1da177e4 LT |
2209 | |
2210 | cpufreq_cpu_put(cpu_policy); | |
1da177e4 LT |
2211 | return 0; |
2212 | } | |
2213 | EXPORT_SYMBOL(cpufreq_get_policy); | |
2214 | ||
a0dbb819 RW |
2215 | /** |
2216 | * cpufreq_set_policy - Modify cpufreq policy parameters. | |
2217 | * @policy: Policy object to modify. | |
2218 | * @new_policy: New policy data. | |
2219 | * | |
2220 | * Pass @new_policy to the cpufreq driver's ->verify() callback, run the | |
2221 | * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to | |
2222 | * the driver's ->verify() callback again and run the notifiers for it again | |
2223 | * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters | |
2224 | * of @new_policy to @policy and either invoke the driver's ->setpolicy() | |
2225 | * callback (if present) or carry out a governor update for @policy. That is, | |
2226 | * run the current governor's ->limits() callback (if the governor field in | |
2227 | * @new_policy points to the same object as the one in @policy) or replace the | |
2228 | * governor for @policy with the new one stored in @new_policy. | |
2229 | * | |
2230 | * The cpuinfo part of @policy is not updated by this function. | |
153d7f3f | 2231 | */ |
037ce839 | 2232 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
a0dbb819 | 2233 | struct cpufreq_policy *new_policy) |
1da177e4 | 2234 | { |
d9a789c7 RW |
2235 | struct cpufreq_governor *old_gov; |
2236 | int ret; | |
1da177e4 | 2237 | |
e837f9b5 JP |
2238 | pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
2239 | new_policy->cpu, new_policy->min, new_policy->max); | |
1da177e4 | 2240 | |
d5b73cd8 | 2241 | memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
1da177e4 | 2242 | |
fba9573b PX |
2243 | /* |
2244 | * This check works well when we store new min/max freq attributes, | |
2245 | * because new_policy is a copy of policy with one field updated. | |
2246 | */ | |
2247 | if (new_policy->min > new_policy->max) | |
d9a789c7 | 2248 | return -EINVAL; |
9c9a43ed | 2249 | |
1da177e4 | 2250 | /* verify the cpu speed can be set within this limit */ |
3a3e9e06 | 2251 | ret = cpufreq_driver->verify(new_policy); |
1da177e4 | 2252 | if (ret) |
d9a789c7 | 2253 | return ret; |
1da177e4 | 2254 | |
1da177e4 | 2255 | /* adjust if necessary - all reasons */ |
e041c683 | 2256 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
3a3e9e06 | 2257 | CPUFREQ_ADJUST, new_policy); |
1da177e4 | 2258 | |
bb176f7d VK |
2259 | /* |
2260 | * verify the cpu speed can be set within this limit, which might be | |
2261 | * different to the first one | |
2262 | */ | |
3a3e9e06 | 2263 | ret = cpufreq_driver->verify(new_policy); |
e041c683 | 2264 | if (ret) |
d9a789c7 | 2265 | return ret; |
1da177e4 LT |
2266 | |
2267 | /* notification of the new policy */ | |
e041c683 | 2268 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
3a3e9e06 | 2269 | CPUFREQ_NOTIFY, new_policy); |
1da177e4 | 2270 | |
3a3e9e06 VK |
2271 | policy->min = new_policy->min; |
2272 | policy->max = new_policy->max; | |
601b2185 | 2273 | trace_cpu_frequency_limits(policy); |
1da177e4 | 2274 | |
e3c06236 SM |
2275 | policy->cached_target_freq = UINT_MAX; |
2276 | ||
2d06d8c4 | 2277 | pr_debug("new min and max freqs are %u - %u kHz\n", |
e837f9b5 | 2278 | policy->min, policy->max); |
1da177e4 | 2279 | |
1c3d85dd | 2280 | if (cpufreq_driver->setpolicy) { |
3a3e9e06 | 2281 | policy->policy = new_policy->policy; |
2d06d8c4 | 2282 | pr_debug("setting range\n"); |
167a38dc | 2283 | return cpufreq_driver->setpolicy(policy); |
d9a789c7 | 2284 | } |
1da177e4 | 2285 | |
0a300767 | 2286 | if (new_policy->governor == policy->governor) { |
2bb4059e | 2287 | pr_debug("governor limits update\n"); |
a92604b4 | 2288 | cpufreq_governor_limits(policy); |
d6ff44d6 | 2289 | return 0; |
0a300767 | 2290 | } |
7bd353a9 | 2291 | |
d9a789c7 RW |
2292 | pr_debug("governor switch\n"); |
2293 | ||
2294 | /* save old, working values */ | |
2295 | old_gov = policy->governor; | |
2296 | /* end old governor */ | |
2297 | if (old_gov) { | |
45482c70 | 2298 | cpufreq_stop_governor(policy); |
36be3418 | 2299 | cpufreq_exit_governor(policy); |
1da177e4 LT |
2300 | } |
2301 | ||
d9a789c7 RW |
2302 | /* start new governor */ |
2303 | policy->governor = new_policy->governor; | |
a92604b4 | 2304 | ret = cpufreq_init_governor(policy); |
4bc384ae | 2305 | if (!ret) { |
0a300767 RW |
2306 | ret = cpufreq_start_governor(policy); |
2307 | if (!ret) { | |
2bb4059e | 2308 | pr_debug("governor change\n"); |
531b5c9f | 2309 | sched_cpufreq_governor_change(policy, old_gov); |
0a300767 RW |
2310 | return 0; |
2311 | } | |
b7898fda | 2312 | cpufreq_exit_governor(policy); |
d9a789c7 RW |
2313 | } |
2314 | ||
2315 | /* new governor failed, so re-start old one */ | |
2316 | pr_debug("starting governor %s failed\n", policy->governor->name); | |
2317 | if (old_gov) { | |
2318 | policy->governor = old_gov; | |
a92604b4 | 2319 | if (cpufreq_init_governor(policy)) |
4bc384ae VK |
2320 | policy->governor = NULL; |
2321 | else | |
0a300767 | 2322 | cpufreq_start_governor(policy); |
d9a789c7 RW |
2323 | } |
2324 | ||
4bc384ae | 2325 | return ret; |
1da177e4 LT |
2326 | } |
2327 | ||
1da177e4 | 2328 | /** |
a0dbb819 RW |
2329 | * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. |
2330 | * @cpu: CPU to re-evaluate the policy for. | |
1da177e4 | 2331 | * |
a0dbb819 RW |
2332 | * Update the current frequency for the cpufreq policy of @cpu and use |
2333 | * cpufreq_set_policy() to re-apply the min and max limits saved in the | |
2334 | * user_policy sub-structure of that policy, which triggers the evaluation | |
2335 | * of policy notifiers and the cpufreq driver's ->verify() callback for the | |
2336 | * policy in question, among other things. | |
1da177e4 | 2337 | */ |
30248fef | 2338 | void cpufreq_update_policy(unsigned int cpu) |
1da177e4 | 2339 | { |
3a3e9e06 VK |
2340 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
2341 | struct cpufreq_policy new_policy; | |
1da177e4 | 2342 | |
fefa8ff8 | 2343 | if (!policy) |
30248fef | 2344 | return; |
1da177e4 | 2345 | |
ad7722da | 2346 | down_write(&policy->rwsem); |
1da177e4 | 2347 | |
30248fef | 2348 | if (policy_is_inactive(policy)) |
182e36af | 2349 | goto unlock; |
182e36af | 2350 | |
bb176f7d VK |
2351 | /* |
2352 | * BIOS might change freq behind our back | |
2353 | * -> ask driver for current freq and notify governors about a change | |
2354 | */ | |
348a2ec5 RW |
2355 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy && |
2356 | (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy)))) | |
2357 | goto unlock; | |
30248fef | 2358 | |
348a2ec5 RW |
2359 | pr_debug("updating policy for CPU %u\n", cpu); |
2360 | memcpy(&new_policy, policy, sizeof(*policy)); | |
2361 | new_policy.min = policy->user_policy.min; | |
2362 | new_policy.max = policy->user_policy.max; | |
0961dd0d | 2363 | |
30248fef | 2364 | cpufreq_set_policy(policy, &new_policy); |
1da177e4 | 2365 | |
fefa8ff8 | 2366 | unlock: |
ad7722da | 2367 | up_write(&policy->rwsem); |
5a01f2e8 | 2368 | |
3a3e9e06 | 2369 | cpufreq_cpu_put(policy); |
1da177e4 LT |
2370 | } |
2371 | EXPORT_SYMBOL(cpufreq_update_policy); | |
2372 | ||
6f19efc0 LM |
2373 | /********************************************************************* |
2374 | * BOOST * | |
2375 | *********************************************************************/ | |
2376 | static int cpufreq_boost_set_sw(int state) | |
2377 | { | |
6f19efc0 LM |
2378 | struct cpufreq_policy *policy; |
2379 | int ret = -EINVAL; | |
2380 | ||
f963735a | 2381 | for_each_active_policy(policy) { |
f8bfc116 VK |
2382 | if (!policy->freq_table) |
2383 | continue; | |
49f18560 | 2384 | |
f8bfc116 VK |
2385 | ret = cpufreq_frequency_table_cpuinfo(policy, |
2386 | policy->freq_table); | |
2387 | if (ret) { | |
2388 | pr_err("%s: Policy frequency update failed\n", | |
2389 | __func__); | |
2390 | break; | |
6f19efc0 | 2391 | } |
f8bfc116 VK |
2392 | |
2393 | down_write(&policy->rwsem); | |
2394 | policy->user_policy.max = policy->max; | |
2395 | cpufreq_governor_limits(policy); | |
2396 | up_write(&policy->rwsem); | |
6f19efc0 LM |
2397 | } |
2398 | ||
2399 | return ret; | |
2400 | } | |
2401 | ||
2402 | int cpufreq_boost_trigger_state(int state) | |
2403 | { | |
2404 | unsigned long flags; | |
2405 | int ret = 0; | |
2406 | ||
2407 | if (cpufreq_driver->boost_enabled == state) | |
2408 | return 0; | |
2409 | ||
2410 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2411 | cpufreq_driver->boost_enabled = state; | |
2412 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2413 | ||
2414 | ret = cpufreq_driver->set_boost(state); | |
2415 | if (ret) { | |
2416 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2417 | cpufreq_driver->boost_enabled = !state; | |
2418 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2419 | ||
e837f9b5 JP |
2420 | pr_err("%s: Cannot %s BOOST\n", |
2421 | __func__, state ? "enable" : "disable"); | |
6f19efc0 LM |
2422 | } |
2423 | ||
2424 | return ret; | |
2425 | } | |
2426 | ||
41669da0 | 2427 | static bool cpufreq_boost_supported(void) |
6f19efc0 | 2428 | { |
7a6c79f2 | 2429 | return likely(cpufreq_driver) && cpufreq_driver->set_boost; |
6f19efc0 | 2430 | } |
6f19efc0 | 2431 | |
44139ed4 VK |
2432 | static int create_boost_sysfs_file(void) |
2433 | { | |
2434 | int ret; | |
2435 | ||
c82bd444 | 2436 | ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2437 | if (ret) |
2438 | pr_err("%s: cannot register global BOOST sysfs file\n", | |
2439 | __func__); | |
2440 | ||
2441 | return ret; | |
2442 | } | |
2443 | ||
2444 | static void remove_boost_sysfs_file(void) | |
2445 | { | |
2446 | if (cpufreq_boost_supported()) | |
c82bd444 | 2447 | sysfs_remove_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2448 | } |
2449 | ||
2450 | int cpufreq_enable_boost_support(void) | |
2451 | { | |
2452 | if (!cpufreq_driver) | |
2453 | return -EINVAL; | |
2454 | ||
2455 | if (cpufreq_boost_supported()) | |
2456 | return 0; | |
2457 | ||
7a6c79f2 | 2458 | cpufreq_driver->set_boost = cpufreq_boost_set_sw; |
44139ed4 VK |
2459 | |
2460 | /* This will get removed on driver unregister */ | |
2461 | return create_boost_sysfs_file(); | |
2462 | } | |
2463 | EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); | |
2464 | ||
6f19efc0 LM |
2465 | int cpufreq_boost_enabled(void) |
2466 | { | |
2467 | return cpufreq_driver->boost_enabled; | |
2468 | } | |
2469 | EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); | |
2470 | ||
1da177e4 LT |
2471 | /********************************************************************* |
2472 | * REGISTER / UNREGISTER CPUFREQ DRIVER * | |
2473 | *********************************************************************/ | |
27622b06 | 2474 | static enum cpuhp_state hp_online; |
1da177e4 | 2475 | |
c4a3fa26 CY |
2476 | static int cpuhp_cpufreq_online(unsigned int cpu) |
2477 | { | |
2478 | cpufreq_online(cpu); | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | ||
2483 | static int cpuhp_cpufreq_offline(unsigned int cpu) | |
2484 | { | |
2485 | cpufreq_offline(cpu); | |
2486 | ||
2487 | return 0; | |
2488 | } | |
2489 | ||
1da177e4 LT |
2490 | /** |
2491 | * cpufreq_register_driver - register a CPU Frequency driver | |
2492 | * @driver_data: A struct cpufreq_driver containing the values# | |
2493 | * submitted by the CPU Frequency driver. | |
2494 | * | |
bb176f7d | 2495 | * Registers a CPU Frequency driver to this core code. This code |
63af4055 | 2496 | * returns zero on success, -EEXIST when another driver got here first |
32ee8c3e | 2497 | * (and isn't unregistered in the meantime). |
1da177e4 LT |
2498 | * |
2499 | */ | |
221dee28 | 2500 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
1da177e4 LT |
2501 | { |
2502 | unsigned long flags; | |
2503 | int ret; | |
2504 | ||
a7b422cd KRW |
2505 | if (cpufreq_disabled()) |
2506 | return -ENODEV; | |
2507 | ||
1da177e4 | 2508 | if (!driver_data || !driver_data->verify || !driver_data->init || |
9c0ebcf7 | 2509 | !(driver_data->setpolicy || driver_data->target_index || |
9832235f RW |
2510 | driver_data->target) || |
2511 | (driver_data->setpolicy && (driver_data->target_index || | |
1c03a2d0 | 2512 | driver_data->target)) || |
a9a22b57 | 2513 | (!driver_data->get_intermediate != !driver_data->target_intermediate) || |
91a12e91 | 2514 | (!driver_data->online != !driver_data->offline)) |
1da177e4 LT |
2515 | return -EINVAL; |
2516 | ||
2d06d8c4 | 2517 | pr_debug("trying to register driver %s\n", driver_data->name); |
1da177e4 | 2518 | |
fdd320da | 2519 | /* Protect against concurrent CPU online/offline. */ |
a92551e4 | 2520 | cpus_read_lock(); |
fdd320da | 2521 | |
0d1857a1 | 2522 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2523 | if (cpufreq_driver) { |
0d1857a1 | 2524 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
fdd320da RW |
2525 | ret = -EEXIST; |
2526 | goto out; | |
1da177e4 | 2527 | } |
1c3d85dd | 2528 | cpufreq_driver = driver_data; |
0d1857a1 | 2529 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 2530 | |
bc68b7df VK |
2531 | if (driver_data->setpolicy) |
2532 | driver_data->flags |= CPUFREQ_CONST_LOOPS; | |
2533 | ||
7a6c79f2 RW |
2534 | if (cpufreq_boost_supported()) { |
2535 | ret = create_boost_sysfs_file(); | |
2536 | if (ret) | |
2537 | goto err_null_driver; | |
2538 | } | |
6f19efc0 | 2539 | |
8a25a2fd | 2540 | ret = subsys_interface_register(&cpufreq_interface); |
8f5bc2ab | 2541 | if (ret) |
6f19efc0 | 2542 | goto err_boost_unreg; |
1da177e4 | 2543 | |
ce1bcfe9 VK |
2544 | if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && |
2545 | list_empty(&cpufreq_policy_list)) { | |
1da177e4 | 2546 | /* if all ->init() calls failed, unregister */ |
6c770036 | 2547 | ret = -ENODEV; |
ce1bcfe9 VK |
2548 | pr_debug("%s: No CPU initialized for driver %s\n", __func__, |
2549 | driver_data->name); | |
2550 | goto err_if_unreg; | |
1da177e4 LT |
2551 | } |
2552 | ||
a92551e4 SAS |
2553 | ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, |
2554 | "cpufreq:online", | |
2555 | cpuhp_cpufreq_online, | |
2556 | cpuhp_cpufreq_offline); | |
27622b06 SAS |
2557 | if (ret < 0) |
2558 | goto err_if_unreg; | |
2559 | hp_online = ret; | |
5372e054 | 2560 | ret = 0; |
27622b06 | 2561 | |
2d06d8c4 | 2562 | pr_debug("driver %s up and running\n", driver_data->name); |
3834abb4 | 2563 | goto out; |
fdd320da | 2564 | |
8a25a2fd KS |
2565 | err_if_unreg: |
2566 | subsys_interface_unregister(&cpufreq_interface); | |
6f19efc0 | 2567 | err_boost_unreg: |
44139ed4 | 2568 | remove_boost_sysfs_file(); |
8f5bc2ab | 2569 | err_null_driver: |
0d1857a1 | 2570 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2571 | cpufreq_driver = NULL; |
0d1857a1 | 2572 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
3834abb4 | 2573 | out: |
a92551e4 | 2574 | cpus_read_unlock(); |
3834abb4 | 2575 | return ret; |
1da177e4 LT |
2576 | } |
2577 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); | |
2578 | ||
1da177e4 LT |
2579 | /** |
2580 | * cpufreq_unregister_driver - unregister the current CPUFreq driver | |
2581 | * | |
bb176f7d | 2582 | * Unregister the current CPUFreq driver. Only call this if you have |
1da177e4 LT |
2583 | * the right to do so, i.e. if you have succeeded in initialising before! |
2584 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is | |
2585 | * currently not initialised. | |
2586 | */ | |
221dee28 | 2587 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) |
1da177e4 LT |
2588 | { |
2589 | unsigned long flags; | |
2590 | ||
1c3d85dd | 2591 | if (!cpufreq_driver || (driver != cpufreq_driver)) |
1da177e4 | 2592 | return -EINVAL; |
1da177e4 | 2593 | |
2d06d8c4 | 2594 | pr_debug("unregistering driver %s\n", driver->name); |
1da177e4 | 2595 | |
454d3a25 | 2596 | /* Protect against concurrent cpu hotplug */ |
a92551e4 | 2597 | cpus_read_lock(); |
8a25a2fd | 2598 | subsys_interface_unregister(&cpufreq_interface); |
44139ed4 | 2599 | remove_boost_sysfs_file(); |
a92551e4 | 2600 | cpuhp_remove_state_nocalls_cpuslocked(hp_online); |
1da177e4 | 2601 | |
0d1857a1 | 2602 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
6eed9404 | 2603 | |
1c3d85dd | 2604 | cpufreq_driver = NULL; |
6eed9404 | 2605 | |
0d1857a1 | 2606 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
a92551e4 | 2607 | cpus_read_unlock(); |
1da177e4 LT |
2608 | |
2609 | return 0; | |
2610 | } | |
2611 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); | |
5a01f2e8 | 2612 | |
90de2a4a DA |
2613 | /* |
2614 | * Stop cpufreq at shutdown to make sure it isn't holding any locks | |
2615 | * or mutexes when secondary CPUs are halted. | |
2616 | */ | |
2617 | static struct syscore_ops cpufreq_syscore_ops = { | |
2618 | .shutdown = cpufreq_suspend, | |
2619 | }; | |
2620 | ||
c82bd444 VK |
2621 | struct kobject *cpufreq_global_kobject; |
2622 | EXPORT_SYMBOL(cpufreq_global_kobject); | |
2623 | ||
5a01f2e8 VP |
2624 | static int __init cpufreq_core_init(void) |
2625 | { | |
a7b422cd KRW |
2626 | if (cpufreq_disabled()) |
2627 | return -ENODEV; | |
2628 | ||
8eec1020 | 2629 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); |
8aa84ad8 TR |
2630 | BUG_ON(!cpufreq_global_kobject); |
2631 | ||
90de2a4a DA |
2632 | register_syscore_ops(&cpufreq_syscore_ops); |
2633 | ||
5a01f2e8 VP |
2634 | return 0; |
2635 | } | |
d82f2692 | 2636 | module_param(off, int, 0444); |
5a01f2e8 | 2637 | core_initcall(cpufreq_core_init); |