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