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