]>
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> | |
6 | * | |
c32b6b8e | 7 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
32ee8c3e | 8 | * Added handling for CPU hotplug |
8ff69732 DJ |
9 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
10 | * Fix handling for CPU hotplug -- affected CPUs | |
c32b6b8e | 11 | * |
1da177e4 LT |
12 | * This program is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | * | |
16 | */ | |
17 | ||
1da177e4 LT |
18 | #include <linux/kernel.h> |
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/notifier.h> | |
22 | #include <linux/cpufreq.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/device.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/cpu.h> | |
29 | #include <linux/completion.h> | |
3fc54d37 | 30 | #include <linux/mutex.h> |
1da177e4 | 31 | |
e08f5f5b GS |
32 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \ |
33 | "cpufreq-core", msg) | |
1da177e4 LT |
34 | |
35 | /** | |
cd878479 | 36 | * The "cpufreq driver" - the arch- or hardware-dependent low |
1da177e4 LT |
37 | * level driver of CPUFreq support, and its spinlock. This lock |
38 | * also protects the cpufreq_cpu_data array. | |
39 | */ | |
7d5e350f DJ |
40 | static struct cpufreq_driver *cpufreq_driver; |
41 | static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS]; | |
1da177e4 LT |
42 | static DEFINE_SPINLOCK(cpufreq_driver_lock); |
43 | ||
1da177e4 LT |
44 | /* internal prototypes */ |
45 | static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); | |
65f27f38 | 46 | static void handle_update(struct work_struct *work); |
1da177e4 LT |
47 | |
48 | /** | |
32ee8c3e DJ |
49 | * Two notifier lists: the "policy" list is involved in the |
50 | * validation process for a new CPU frequency policy; the | |
1da177e4 LT |
51 | * "transition" list for kernel code that needs to handle |
52 | * changes to devices when the CPU clock speed changes. | |
53 | * The mutex locks both lists. | |
54 | */ | |
e041c683 | 55 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
b4dfdbb3 | 56 | static struct srcu_notifier_head cpufreq_transition_notifier_list; |
1da177e4 | 57 | |
b4dfdbb3 AS |
58 | static int __init init_cpufreq_transition_notifier_list(void) |
59 | { | |
60 | srcu_init_notifier_head(&cpufreq_transition_notifier_list); | |
61 | return 0; | |
62 | } | |
b3438f82 | 63 | pure_initcall(init_cpufreq_transition_notifier_list); |
1da177e4 LT |
64 | |
65 | static LIST_HEAD(cpufreq_governor_list); | |
7d5e350f | 66 | static DEFINE_MUTEX (cpufreq_governor_mutex); |
1da177e4 | 67 | |
7d5e350f | 68 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
1da177e4 LT |
69 | { |
70 | struct cpufreq_policy *data; | |
71 | unsigned long flags; | |
72 | ||
73 | if (cpu >= NR_CPUS) | |
74 | goto err_out; | |
75 | ||
76 | /* get the cpufreq driver */ | |
77 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
78 | ||
79 | if (!cpufreq_driver) | |
80 | goto err_out_unlock; | |
81 | ||
82 | if (!try_module_get(cpufreq_driver->owner)) | |
83 | goto err_out_unlock; | |
84 | ||
85 | ||
86 | /* get the CPU */ | |
87 | data = cpufreq_cpu_data[cpu]; | |
88 | ||
89 | if (!data) | |
90 | goto err_out_put_module; | |
91 | ||
92 | if (!kobject_get(&data->kobj)) | |
93 | goto err_out_put_module; | |
94 | ||
1da177e4 | 95 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 LT |
96 | return data; |
97 | ||
7d5e350f | 98 | err_out_put_module: |
1da177e4 | 99 | module_put(cpufreq_driver->owner); |
7d5e350f | 100 | err_out_unlock: |
1da177e4 | 101 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
7d5e350f | 102 | err_out: |
1da177e4 LT |
103 | return NULL; |
104 | } | |
105 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); | |
106 | ||
7d5e350f | 107 | |
1da177e4 LT |
108 | void cpufreq_cpu_put(struct cpufreq_policy *data) |
109 | { | |
110 | kobject_put(&data->kobj); | |
111 | module_put(cpufreq_driver->owner); | |
112 | } | |
113 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); | |
114 | ||
115 | ||
116 | /********************************************************************* | |
117 | * UNIFIED DEBUG HELPERS * | |
118 | *********************************************************************/ | |
119 | #ifdef CONFIG_CPU_FREQ_DEBUG | |
120 | ||
121 | /* what part(s) of the CPUfreq subsystem are debugged? */ | |
122 | static unsigned int debug; | |
123 | ||
124 | /* is the debug output ratelimit'ed using printk_ratelimit? User can | |
125 | * set or modify this value. | |
126 | */ | |
127 | static unsigned int debug_ratelimit = 1; | |
128 | ||
129 | /* is the printk_ratelimit'ing enabled? It's enabled after a successful | |
130 | * loading of a cpufreq driver, temporarily disabled when a new policy | |
131 | * is set, and disabled upon cpufreq driver removal | |
132 | */ | |
133 | static unsigned int disable_ratelimit = 1; | |
134 | static DEFINE_SPINLOCK(disable_ratelimit_lock); | |
135 | ||
858119e1 | 136 | static void cpufreq_debug_enable_ratelimit(void) |
1da177e4 LT |
137 | { |
138 | unsigned long flags; | |
139 | ||
140 | spin_lock_irqsave(&disable_ratelimit_lock, flags); | |
141 | if (disable_ratelimit) | |
142 | disable_ratelimit--; | |
143 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); | |
144 | } | |
145 | ||
858119e1 | 146 | static void cpufreq_debug_disable_ratelimit(void) |
1da177e4 LT |
147 | { |
148 | unsigned long flags; | |
149 | ||
150 | spin_lock_irqsave(&disable_ratelimit_lock, flags); | |
151 | disable_ratelimit++; | |
152 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); | |
153 | } | |
154 | ||
e08f5f5b GS |
155 | void cpufreq_debug_printk(unsigned int type, const char *prefix, |
156 | const char *fmt, ...) | |
1da177e4 LT |
157 | { |
158 | char s[256]; | |
159 | va_list args; | |
160 | unsigned int len; | |
161 | unsigned long flags; | |
32ee8c3e | 162 | |
1da177e4 LT |
163 | WARN_ON(!prefix); |
164 | if (type & debug) { | |
165 | spin_lock_irqsave(&disable_ratelimit_lock, flags); | |
e08f5f5b GS |
166 | if (!disable_ratelimit && debug_ratelimit |
167 | && !printk_ratelimit()) { | |
1da177e4 LT |
168 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); |
169 | return; | |
170 | } | |
171 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); | |
172 | ||
173 | len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); | |
174 | ||
175 | va_start(args, fmt); | |
176 | len += vsnprintf(&s[len], (256 - len), fmt, args); | |
177 | va_end(args); | |
178 | ||
179 | printk(s); | |
180 | ||
181 | WARN_ON(len < 5); | |
182 | } | |
183 | } | |
184 | EXPORT_SYMBOL(cpufreq_debug_printk); | |
185 | ||
186 | ||
187 | module_param(debug, uint, 0644); | |
e08f5f5b GS |
188 | MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," |
189 | " 2 to debug drivers, and 4 to debug governors."); | |
1da177e4 LT |
190 | |
191 | module_param(debug_ratelimit, uint, 0644); | |
e08f5f5b GS |
192 | MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" |
193 | " set to 0 to disable ratelimiting."); | |
1da177e4 LT |
194 | |
195 | #else /* !CONFIG_CPU_FREQ_DEBUG */ | |
196 | ||
197 | static inline void cpufreq_debug_enable_ratelimit(void) { return; } | |
198 | static inline void cpufreq_debug_disable_ratelimit(void) { return; } | |
199 | ||
200 | #endif /* CONFIG_CPU_FREQ_DEBUG */ | |
201 | ||
202 | ||
203 | /********************************************************************* | |
204 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * | |
205 | *********************************************************************/ | |
206 | ||
207 | /** | |
208 | * adjust_jiffies - adjust the system "loops_per_jiffy" | |
209 | * | |
210 | * This function alters the system "loops_per_jiffy" for the clock | |
211 | * speed change. Note that loops_per_jiffy cannot be updated on SMP | |
32ee8c3e | 212 | * systems as each CPU might be scaled differently. So, use the arch |
1da177e4 LT |
213 | * per-CPU loops_per_jiffy value wherever possible. |
214 | */ | |
215 | #ifndef CONFIG_SMP | |
216 | static unsigned long l_p_j_ref; | |
217 | static unsigned int l_p_j_ref_freq; | |
218 | ||
858119e1 | 219 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
1da177e4 LT |
220 | { |
221 | if (ci->flags & CPUFREQ_CONST_LOOPS) | |
222 | return; | |
223 | ||
224 | if (!l_p_j_ref_freq) { | |
225 | l_p_j_ref = loops_per_jiffy; | |
226 | l_p_j_ref_freq = ci->old; | |
e08f5f5b GS |
227 | dprintk("saving %lu as reference value for loops_per_jiffy;" |
228 | "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); | |
1da177e4 LT |
229 | } |
230 | if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || | |
231 | (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || | |
42d4dc3f | 232 | (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { |
e08f5f5b GS |
233 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
234 | ci->new); | |
235 | dprintk("scaling loops_per_jiffy to %lu" | |
236 | "for frequency %u kHz\n", loops_per_jiffy, ci->new); | |
1da177e4 LT |
237 | } |
238 | } | |
239 | #else | |
e08f5f5b GS |
240 | static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
241 | { | |
242 | return; | |
243 | } | |
1da177e4 LT |
244 | #endif |
245 | ||
246 | ||
247 | /** | |
e4472cb3 DJ |
248 | * cpufreq_notify_transition - call notifier chain and adjust_jiffies |
249 | * on frequency transition. | |
1da177e4 | 250 | * |
e4472cb3 DJ |
251 | * This function calls the transition notifiers and the "adjust_jiffies" |
252 | * function. It is called twice on all CPU frequency changes that have | |
32ee8c3e | 253 | * external effects. |
1da177e4 LT |
254 | */ |
255 | void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) | |
256 | { | |
e4472cb3 DJ |
257 | struct cpufreq_policy *policy; |
258 | ||
1da177e4 LT |
259 | BUG_ON(irqs_disabled()); |
260 | ||
261 | freqs->flags = cpufreq_driver->flags; | |
e4472cb3 DJ |
262 | dprintk("notification %u of frequency transition to %u kHz\n", |
263 | state, freqs->new); | |
1da177e4 | 264 | |
e4472cb3 | 265 | policy = cpufreq_cpu_data[freqs->cpu]; |
1da177e4 | 266 | switch (state) { |
e4472cb3 | 267 | |
1da177e4 | 268 | case CPUFREQ_PRECHANGE: |
32ee8c3e | 269 | /* detect if the driver reported a value as "old frequency" |
e4472cb3 DJ |
270 | * which is not equal to what the cpufreq core thinks is |
271 | * "old frequency". | |
1da177e4 LT |
272 | */ |
273 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { | |
e4472cb3 DJ |
274 | if ((policy) && (policy->cpu == freqs->cpu) && |
275 | (policy->cur) && (policy->cur != freqs->old)) { | |
b10eec22 | 276 | dprintk("Warning: CPU frequency is" |
e4472cb3 DJ |
277 | " %u, cpufreq assumed %u kHz.\n", |
278 | freqs->old, policy->cur); | |
279 | freqs->old = policy->cur; | |
1da177e4 LT |
280 | } |
281 | } | |
b4dfdbb3 | 282 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
e041c683 | 283 | CPUFREQ_PRECHANGE, freqs); |
1da177e4 LT |
284 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
285 | break; | |
e4472cb3 | 286 | |
1da177e4 LT |
287 | case CPUFREQ_POSTCHANGE: |
288 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); | |
b4dfdbb3 | 289 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
e041c683 | 290 | CPUFREQ_POSTCHANGE, freqs); |
e4472cb3 DJ |
291 | if (likely(policy) && likely(policy->cpu == freqs->cpu)) |
292 | policy->cur = freqs->new; | |
1da177e4 LT |
293 | break; |
294 | } | |
1da177e4 LT |
295 | } |
296 | EXPORT_SYMBOL_GPL(cpufreq_notify_transition); | |
297 | ||
298 | ||
299 | ||
300 | /********************************************************************* | |
301 | * SYSFS INTERFACE * | |
302 | *********************************************************************/ | |
303 | ||
3bcb09a3 JF |
304 | static struct cpufreq_governor *__find_governor(const char *str_governor) |
305 | { | |
306 | struct cpufreq_governor *t; | |
307 | ||
308 | list_for_each_entry(t, &cpufreq_governor_list, governor_list) | |
309 | if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) | |
310 | return t; | |
311 | ||
312 | return NULL; | |
313 | } | |
314 | ||
1da177e4 LT |
315 | /** |
316 | * cpufreq_parse_governor - parse a governor string | |
317 | */ | |
318 | static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, | |
319 | struct cpufreq_governor **governor) | |
320 | { | |
3bcb09a3 JF |
321 | int err = -EINVAL; |
322 | ||
1da177e4 | 323 | if (!cpufreq_driver) |
3bcb09a3 JF |
324 | goto out; |
325 | ||
1da177e4 LT |
326 | if (cpufreq_driver->setpolicy) { |
327 | if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { | |
328 | *policy = CPUFREQ_POLICY_PERFORMANCE; | |
3bcb09a3 | 329 | err = 0; |
e08f5f5b GS |
330 | } else if (!strnicmp(str_governor, "powersave", |
331 | CPUFREQ_NAME_LEN)) { | |
1da177e4 | 332 | *policy = CPUFREQ_POLICY_POWERSAVE; |
3bcb09a3 | 333 | err = 0; |
1da177e4 | 334 | } |
3bcb09a3 | 335 | } else if (cpufreq_driver->target) { |
1da177e4 | 336 | struct cpufreq_governor *t; |
3bcb09a3 | 337 | |
3fc54d37 | 338 | mutex_lock(&cpufreq_governor_mutex); |
3bcb09a3 JF |
339 | |
340 | t = __find_governor(str_governor); | |
341 | ||
ea714970 | 342 | if (t == NULL) { |
e08f5f5b GS |
343 | char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", |
344 | str_governor); | |
ea714970 JF |
345 | |
346 | if (name) { | |
347 | int ret; | |
348 | ||
349 | mutex_unlock(&cpufreq_governor_mutex); | |
350 | ret = request_module(name); | |
351 | mutex_lock(&cpufreq_governor_mutex); | |
352 | ||
353 | if (ret == 0) | |
354 | t = __find_governor(str_governor); | |
355 | } | |
356 | ||
357 | kfree(name); | |
358 | } | |
359 | ||
3bcb09a3 JF |
360 | if (t != NULL) { |
361 | *governor = t; | |
362 | err = 0; | |
1da177e4 | 363 | } |
3bcb09a3 | 364 | |
3fc54d37 | 365 | mutex_unlock(&cpufreq_governor_mutex); |
1da177e4 | 366 | } |
3bcb09a3 JF |
367 | out: |
368 | return err; | |
1da177e4 | 369 | } |
1da177e4 LT |
370 | |
371 | ||
372 | /* drivers/base/cpu.c */ | |
373 | extern struct sysdev_class cpu_sysdev_class; | |
374 | ||
375 | ||
376 | /** | |
e08f5f5b GS |
377 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
378 | * print out cpufreq information | |
1da177e4 LT |
379 | * |
380 | * Write out information from cpufreq_driver->policy[cpu]; object must be | |
381 | * "unsigned int". | |
382 | */ | |
383 | ||
32ee8c3e DJ |
384 | #define show_one(file_name, object) \ |
385 | static ssize_t show_##file_name \ | |
386 | (struct cpufreq_policy * policy, char *buf) \ | |
387 | { \ | |
388 | return sprintf (buf, "%u\n", policy->object); \ | |
1da177e4 LT |
389 | } |
390 | ||
391 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); | |
392 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); | |
393 | show_one(scaling_min_freq, min); | |
394 | show_one(scaling_max_freq, max); | |
395 | show_one(scaling_cur_freq, cur); | |
396 | ||
e08f5f5b GS |
397 | static int __cpufreq_set_policy(struct cpufreq_policy *data, |
398 | struct cpufreq_policy *policy); | |
7970e08b | 399 | |
1da177e4 LT |
400 | /** |
401 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access | |
402 | */ | |
403 | #define store_one(file_name, object) \ | |
404 | static ssize_t store_##file_name \ | |
405 | (struct cpufreq_policy * policy, const char *buf, size_t count) \ | |
406 | { \ | |
407 | unsigned int ret = -EINVAL; \ | |
408 | struct cpufreq_policy new_policy; \ | |
409 | \ | |
410 | ret = cpufreq_get_policy(&new_policy, policy->cpu); \ | |
411 | if (ret) \ | |
412 | return -EINVAL; \ | |
413 | \ | |
414 | ret = sscanf (buf, "%u", &new_policy.object); \ | |
415 | if (ret != 1) \ | |
416 | return -EINVAL; \ | |
417 | \ | |
7970e08b TR |
418 | mutex_lock(&policy->lock); \ |
419 | ret = __cpufreq_set_policy(policy, &new_policy); \ | |
420 | policy->user_policy.object = policy->object; \ | |
421 | mutex_unlock(&policy->lock); \ | |
1da177e4 LT |
422 | \ |
423 | return ret ? ret : count; \ | |
424 | } | |
425 | ||
426 | store_one(scaling_min_freq,min); | |
427 | store_one(scaling_max_freq,max); | |
428 | ||
429 | /** | |
430 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware | |
431 | */ | |
e08f5f5b GS |
432 | static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, |
433 | char *buf) | |
1da177e4 LT |
434 | { |
435 | unsigned int cur_freq = cpufreq_get(policy->cpu); | |
436 | if (!cur_freq) | |
437 | return sprintf(buf, "<unknown>"); | |
438 | return sprintf(buf, "%u\n", cur_freq); | |
439 | } | |
440 | ||
441 | ||
442 | /** | |
443 | * show_scaling_governor - show the current policy for the specified CPU | |
444 | */ | |
e08f5f5b GS |
445 | static ssize_t show_scaling_governor (struct cpufreq_policy * policy, |
446 | char *buf) | |
1da177e4 LT |
447 | { |
448 | if(policy->policy == CPUFREQ_POLICY_POWERSAVE) | |
449 | return sprintf(buf, "powersave\n"); | |
450 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) | |
451 | return sprintf(buf, "performance\n"); | |
452 | else if (policy->governor) | |
453 | return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); | |
454 | return -EINVAL; | |
455 | } | |
456 | ||
457 | ||
458 | /** | |
459 | * store_scaling_governor - store policy for the specified CPU | |
460 | */ | |
32ee8c3e DJ |
461 | static ssize_t store_scaling_governor (struct cpufreq_policy * policy, |
462 | const char *buf, size_t count) | |
1da177e4 LT |
463 | { |
464 | unsigned int ret = -EINVAL; | |
465 | char str_governor[16]; | |
466 | struct cpufreq_policy new_policy; | |
467 | ||
468 | ret = cpufreq_get_policy(&new_policy, policy->cpu); | |
469 | if (ret) | |
470 | return ret; | |
471 | ||
472 | ret = sscanf (buf, "%15s", str_governor); | |
473 | if (ret != 1) | |
474 | return -EINVAL; | |
475 | ||
e08f5f5b GS |
476 | if (cpufreq_parse_governor(str_governor, &new_policy.policy, |
477 | &new_policy.governor)) | |
1da177e4 LT |
478 | return -EINVAL; |
479 | ||
7970e08b TR |
480 | /* Do not use cpufreq_set_policy here or the user_policy.max |
481 | will be wrongly overridden */ | |
482 | mutex_lock(&policy->lock); | |
483 | ret = __cpufreq_set_policy(policy, &new_policy); | |
484 | ||
485 | policy->user_policy.policy = policy->policy; | |
486 | policy->user_policy.governor = policy->governor; | |
487 | mutex_unlock(&policy->lock); | |
488 | ||
e08f5f5b GS |
489 | if (ret) |
490 | return ret; | |
491 | else | |
492 | return count; | |
1da177e4 LT |
493 | } |
494 | ||
495 | /** | |
496 | * show_scaling_driver - show the cpufreq driver currently loaded | |
497 | */ | |
498 | static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf) | |
499 | { | |
500 | return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); | |
501 | } | |
502 | ||
503 | /** | |
504 | * show_scaling_available_governors - show the available CPUfreq governors | |
505 | */ | |
e08f5f5b | 506 | static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy, |
1da177e4 LT |
507 | char *buf) |
508 | { | |
509 | ssize_t i = 0; | |
510 | struct cpufreq_governor *t; | |
511 | ||
512 | if (!cpufreq_driver->target) { | |
513 | i += sprintf(buf, "performance powersave"); | |
514 | goto out; | |
515 | } | |
516 | ||
517 | list_for_each_entry(t, &cpufreq_governor_list, governor_list) { | |
518 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) | |
519 | goto out; | |
520 | i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); | |
521 | } | |
7d5e350f | 522 | out: |
1da177e4 LT |
523 | i += sprintf(&buf[i], "\n"); |
524 | return i; | |
525 | } | |
526 | /** | |
527 | * show_affected_cpus - show the CPUs affected by each transition | |
528 | */ | |
529 | static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf) | |
530 | { | |
531 | ssize_t i = 0; | |
532 | unsigned int cpu; | |
533 | ||
534 | for_each_cpu_mask(cpu, policy->cpus) { | |
535 | if (i) | |
536 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); | |
537 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); | |
538 | if (i >= (PAGE_SIZE - 5)) | |
539 | break; | |
540 | } | |
541 | i += sprintf(&buf[i], "\n"); | |
542 | return i; | |
543 | } | |
544 | ||
545 | ||
546 | #define define_one_ro(_name) \ | |
547 | static struct freq_attr _name = \ | |
548 | __ATTR(_name, 0444, show_##_name, NULL) | |
549 | ||
550 | #define define_one_ro0400(_name) \ | |
551 | static struct freq_attr _name = \ | |
552 | __ATTR(_name, 0400, show_##_name, NULL) | |
553 | ||
554 | #define define_one_rw(_name) \ | |
555 | static struct freq_attr _name = \ | |
556 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
557 | ||
558 | define_one_ro0400(cpuinfo_cur_freq); | |
559 | define_one_ro(cpuinfo_min_freq); | |
560 | define_one_ro(cpuinfo_max_freq); | |
561 | define_one_ro(scaling_available_governors); | |
562 | define_one_ro(scaling_driver); | |
563 | define_one_ro(scaling_cur_freq); | |
564 | define_one_ro(affected_cpus); | |
565 | define_one_rw(scaling_min_freq); | |
566 | define_one_rw(scaling_max_freq); | |
567 | define_one_rw(scaling_governor); | |
568 | ||
569 | static struct attribute * default_attrs[] = { | |
570 | &cpuinfo_min_freq.attr, | |
571 | &cpuinfo_max_freq.attr, | |
572 | &scaling_min_freq.attr, | |
573 | &scaling_max_freq.attr, | |
574 | &affected_cpus.attr, | |
575 | &scaling_governor.attr, | |
576 | &scaling_driver.attr, | |
577 | &scaling_available_governors.attr, | |
578 | NULL | |
579 | }; | |
580 | ||
581 | #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) | |
582 | #define to_attr(a) container_of(a,struct freq_attr,attr) | |
583 | ||
584 | static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf) | |
585 | { | |
586 | struct cpufreq_policy * policy = to_policy(kobj); | |
587 | struct freq_attr * fattr = to_attr(attr); | |
588 | ssize_t ret; | |
589 | policy = cpufreq_cpu_get(policy->cpu); | |
590 | if (!policy) | |
591 | return -EINVAL; | |
e08f5f5b GS |
592 | if (fattr->show) |
593 | ret = fattr->show(policy, buf); | |
594 | else | |
595 | ret = -EIO; | |
596 | ||
1da177e4 LT |
597 | cpufreq_cpu_put(policy); |
598 | return ret; | |
599 | } | |
600 | ||
32ee8c3e | 601 | static ssize_t store(struct kobject * kobj, struct attribute * attr, |
1da177e4 LT |
602 | const char * buf, size_t count) |
603 | { | |
604 | struct cpufreq_policy * policy = to_policy(kobj); | |
605 | struct freq_attr * fattr = to_attr(attr); | |
606 | ssize_t ret; | |
607 | policy = cpufreq_cpu_get(policy->cpu); | |
608 | if (!policy) | |
609 | return -EINVAL; | |
e08f5f5b GS |
610 | if (fattr->store) |
611 | ret = fattr->store(policy, buf, count); | |
612 | else | |
613 | ret = -EIO; | |
614 | ||
1da177e4 LT |
615 | cpufreq_cpu_put(policy); |
616 | return ret; | |
617 | } | |
618 | ||
619 | static void cpufreq_sysfs_release(struct kobject * kobj) | |
620 | { | |
621 | struct cpufreq_policy * policy = to_policy(kobj); | |
622 | dprintk("last reference is dropped\n"); | |
623 | complete(&policy->kobj_unregister); | |
624 | } | |
625 | ||
626 | static struct sysfs_ops sysfs_ops = { | |
627 | .show = show, | |
628 | .store = store, | |
629 | }; | |
630 | ||
631 | static struct kobj_type ktype_cpufreq = { | |
632 | .sysfs_ops = &sysfs_ops, | |
633 | .default_attrs = default_attrs, | |
634 | .release = cpufreq_sysfs_release, | |
635 | }; | |
636 | ||
637 | ||
638 | /** | |
639 | * cpufreq_add_dev - add a CPU device | |
640 | * | |
32ee8c3e | 641 | * Adds the cpufreq interface for a CPU device. |
1da177e4 LT |
642 | */ |
643 | static int cpufreq_add_dev (struct sys_device * sys_dev) | |
644 | { | |
645 | unsigned int cpu = sys_dev->id; | |
646 | int ret = 0; | |
647 | struct cpufreq_policy new_policy; | |
648 | struct cpufreq_policy *policy; | |
649 | struct freq_attr **drv_attr; | |
8ff69732 | 650 | struct sys_device *cpu_sys_dev; |
1da177e4 LT |
651 | unsigned long flags; |
652 | unsigned int j; | |
8ff69732 DJ |
653 | #ifdef CONFIG_SMP |
654 | struct cpufreq_policy *managed_policy; | |
655 | #endif | |
1da177e4 | 656 | |
c32b6b8e AR |
657 | if (cpu_is_offline(cpu)) |
658 | return 0; | |
659 | ||
1da177e4 LT |
660 | cpufreq_debug_disable_ratelimit(); |
661 | dprintk("adding CPU %u\n", cpu); | |
662 | ||
663 | #ifdef CONFIG_SMP | |
664 | /* check whether a different CPU already registered this | |
665 | * CPU because it is in the same boat. */ | |
666 | policy = cpufreq_cpu_get(cpu); | |
667 | if (unlikely(policy)) { | |
8ff69732 | 668 | cpufreq_cpu_put(policy); |
1da177e4 LT |
669 | cpufreq_debug_enable_ratelimit(); |
670 | return 0; | |
671 | } | |
672 | #endif | |
673 | ||
674 | if (!try_module_get(cpufreq_driver->owner)) { | |
675 | ret = -EINVAL; | |
676 | goto module_out; | |
677 | } | |
678 | ||
e98df50c | 679 | policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); |
1da177e4 LT |
680 | if (!policy) { |
681 | ret = -ENOMEM; | |
682 | goto nomem_out; | |
683 | } | |
1da177e4 LT |
684 | |
685 | policy->cpu = cpu; | |
686 | policy->cpus = cpumask_of_cpu(cpu); | |
687 | ||
83933af4 AV |
688 | mutex_init(&policy->lock); |
689 | mutex_lock(&policy->lock); | |
1da177e4 | 690 | init_completion(&policy->kobj_unregister); |
65f27f38 | 691 | INIT_WORK(&policy->update, handle_update); |
1da177e4 LT |
692 | |
693 | /* call driver. From then on the cpufreq must be able | |
694 | * to accept all calls to ->verify and ->setpolicy for this CPU | |
695 | */ | |
696 | ret = cpufreq_driver->init(policy); | |
697 | if (ret) { | |
698 | dprintk("initialization failed\n"); | |
f3876c1b | 699 | mutex_unlock(&policy->lock); |
1da177e4 LT |
700 | goto err_out; |
701 | } | |
702 | ||
8ff69732 DJ |
703 | #ifdef CONFIG_SMP |
704 | for_each_cpu_mask(j, policy->cpus) { | |
705 | if (cpu == j) | |
706 | continue; | |
707 | ||
708 | /* check for existing affected CPUs. They may not be aware | |
709 | * of it due to CPU Hotplug. | |
710 | */ | |
711 | managed_policy = cpufreq_cpu_get(j); | |
712 | if (unlikely(managed_policy)) { | |
713 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
714 | managed_policy->cpus = policy->cpus; | |
715 | cpufreq_cpu_data[cpu] = managed_policy; | |
716 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
717 | ||
718 | dprintk("CPU already managed, adding link\n"); | |
0142f9dc AD |
719 | ret = sysfs_create_link(&sys_dev->kobj, |
720 | &managed_policy->kobj, | |
721 | "cpufreq"); | |
722 | if (ret) { | |
723 | mutex_unlock(&policy->lock); | |
724 | goto err_out_driver_exit; | |
725 | } | |
8ff69732 DJ |
726 | |
727 | cpufreq_debug_enable_ratelimit(); | |
728 | mutex_unlock(&policy->lock); | |
729 | ret = 0; | |
730 | goto err_out_driver_exit; /* call driver->exit() */ | |
731 | } | |
732 | } | |
733 | #endif | |
1da177e4 LT |
734 | memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); |
735 | ||
736 | /* prepare interface data */ | |
737 | policy->kobj.parent = &sys_dev->kobj; | |
738 | policy->kobj.ktype = &ktype_cpufreq; | |
739 | strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN); | |
740 | ||
741 | ret = kobject_register(&policy->kobj); | |
f3876c1b AM |
742 | if (ret) { |
743 | mutex_unlock(&policy->lock); | |
8085e1f1 | 744 | goto err_out_driver_exit; |
f3876c1b | 745 | } |
1da177e4 LT |
746 | /* set up files for this cpu device */ |
747 | drv_attr = cpufreq_driver->attr; | |
748 | while ((drv_attr) && (*drv_attr)) { | |
749 | sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); | |
750 | drv_attr++; | |
751 | } | |
752 | if (cpufreq_driver->get) | |
753 | sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); | |
754 | if (cpufreq_driver->target) | |
755 | sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); | |
756 | ||
757 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
758 | for_each_cpu_mask(j, policy->cpus) | |
759 | cpufreq_cpu_data[j] = policy; | |
760 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
8ff69732 DJ |
761 | |
762 | /* symlink affected CPUs */ | |
763 | for_each_cpu_mask(j, policy->cpus) { | |
764 | if (j == cpu) | |
765 | continue; | |
766 | if (!cpu_online(j)) | |
767 | continue; | |
768 | ||
1f8b2c9d | 769 | dprintk("CPU %u already managed, adding link\n", j); |
8ff69732 DJ |
770 | cpufreq_cpu_get(cpu); |
771 | cpu_sys_dev = get_cpu_sysdev(j); | |
0142f9dc AD |
772 | ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, |
773 | "cpufreq"); | |
774 | if (ret) { | |
775 | mutex_unlock(&policy->lock); | |
776 | goto err_out_unregister; | |
777 | } | |
8ff69732 DJ |
778 | } |
779 | ||
1da177e4 LT |
780 | policy->governor = NULL; /* to assure that the starting sequence is |
781 | * run in cpufreq_set_policy */ | |
83933af4 | 782 | mutex_unlock(&policy->lock); |
87c32271 | 783 | |
1da177e4 | 784 | /* set default policy */ |
1da177e4 LT |
785 | ret = cpufreq_set_policy(&new_policy); |
786 | if (ret) { | |
787 | dprintk("setting policy failed\n"); | |
788 | goto err_out_unregister; | |
789 | } | |
790 | ||
791 | module_put(cpufreq_driver->owner); | |
1da177e4 LT |
792 | dprintk("initialization complete\n"); |
793 | cpufreq_debug_enable_ratelimit(); | |
87c32271 | 794 | |
1da177e4 LT |
795 | return 0; |
796 | ||
797 | ||
798 | err_out_unregister: | |
799 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
800 | for_each_cpu_mask(j, policy->cpus) | |
801 | cpufreq_cpu_data[j] = NULL; | |
802 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
803 | ||
804 | kobject_unregister(&policy->kobj); | |
805 | wait_for_completion(&policy->kobj_unregister); | |
806 | ||
8085e1f1 VP |
807 | err_out_driver_exit: |
808 | if (cpufreq_driver->exit) | |
809 | cpufreq_driver->exit(policy); | |
810 | ||
1da177e4 LT |
811 | err_out: |
812 | kfree(policy); | |
813 | ||
814 | nomem_out: | |
815 | module_put(cpufreq_driver->owner); | |
c32b6b8e | 816 | module_out: |
1da177e4 LT |
817 | cpufreq_debug_enable_ratelimit(); |
818 | return ret; | |
819 | } | |
820 | ||
821 | ||
822 | /** | |
823 | * cpufreq_remove_dev - remove a CPU device | |
824 | * | |
825 | * Removes the cpufreq interface for a CPU device. | |
826 | */ | |
827 | static int cpufreq_remove_dev (struct sys_device * sys_dev) | |
828 | { | |
829 | unsigned int cpu = sys_dev->id; | |
830 | unsigned long flags; | |
831 | struct cpufreq_policy *data; | |
832 | #ifdef CONFIG_SMP | |
e738cf6d | 833 | struct sys_device *cpu_sys_dev; |
1da177e4 LT |
834 | unsigned int j; |
835 | #endif | |
836 | ||
837 | cpufreq_debug_disable_ratelimit(); | |
838 | dprintk("unregistering CPU %u\n", cpu); | |
839 | ||
840 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
841 | data = cpufreq_cpu_data[cpu]; | |
842 | ||
843 | if (!data) { | |
844 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1da177e4 LT |
845 | cpufreq_debug_enable_ratelimit(); |
846 | return -EINVAL; | |
847 | } | |
848 | cpufreq_cpu_data[cpu] = NULL; | |
849 | ||
850 | ||
851 | #ifdef CONFIG_SMP | |
852 | /* if this isn't the CPU which is the parent of the kobj, we | |
32ee8c3e | 853 | * only need to unlink, put and exit |
1da177e4 LT |
854 | */ |
855 | if (unlikely(cpu != data->cpu)) { | |
856 | dprintk("removing link\n"); | |
8ff69732 | 857 | cpu_clear(cpu, data->cpus); |
1da177e4 LT |
858 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
859 | sysfs_remove_link(&sys_dev->kobj, "cpufreq"); | |
1da177e4 LT |
860 | cpufreq_cpu_put(data); |
861 | cpufreq_debug_enable_ratelimit(); | |
862 | return 0; | |
863 | } | |
864 | #endif | |
865 | ||
1da177e4 LT |
866 | |
867 | if (!kobject_get(&data->kobj)) { | |
868 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
869 | cpufreq_debug_enable_ratelimit(); | |
32ee8c3e | 870 | return -EFAULT; |
1da177e4 LT |
871 | } |
872 | ||
873 | #ifdef CONFIG_SMP | |
874 | /* if we have other CPUs still registered, we need to unlink them, | |
875 | * or else wait_for_completion below will lock up. Clean the | |
876 | * cpufreq_cpu_data[] while holding the lock, and remove the sysfs | |
877 | * links afterwards. | |
878 | */ | |
879 | if (unlikely(cpus_weight(data->cpus) > 1)) { | |
880 | for_each_cpu_mask(j, data->cpus) { | |
881 | if (j == cpu) | |
882 | continue; | |
883 | cpufreq_cpu_data[j] = NULL; | |
884 | } | |
885 | } | |
886 | ||
887 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
888 | ||
889 | if (unlikely(cpus_weight(data->cpus) > 1)) { | |
890 | for_each_cpu_mask(j, data->cpus) { | |
891 | if (j == cpu) | |
892 | continue; | |
893 | dprintk("removing link for cpu %u\n", j); | |
d434fca7 AR |
894 | cpu_sys_dev = get_cpu_sysdev(j); |
895 | sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); | |
1da177e4 LT |
896 | cpufreq_cpu_put(data); |
897 | } | |
898 | } | |
899 | #else | |
900 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
901 | #endif | |
902 | ||
83933af4 | 903 | mutex_lock(&data->lock); |
1da177e4 LT |
904 | if (cpufreq_driver->target) |
905 | __cpufreq_governor(data, CPUFREQ_GOV_STOP); | |
83933af4 | 906 | mutex_unlock(&data->lock); |
1da177e4 LT |
907 | |
908 | kobject_unregister(&data->kobj); | |
909 | ||
910 | kobject_put(&data->kobj); | |
911 | ||
912 | /* we need to make sure that the underlying kobj is actually | |
32ee8c3e | 913 | * not referenced anymore by anybody before we proceed with |
1da177e4 LT |
914 | * unloading. |
915 | */ | |
916 | dprintk("waiting for dropping of refcount\n"); | |
917 | wait_for_completion(&data->kobj_unregister); | |
918 | dprintk("wait complete\n"); | |
919 | ||
920 | if (cpufreq_driver->exit) | |
921 | cpufreq_driver->exit(data); | |
922 | ||
923 | kfree(data); | |
924 | ||
925 | cpufreq_debug_enable_ratelimit(); | |
1da177e4 LT |
926 | return 0; |
927 | } | |
928 | ||
929 | ||
65f27f38 | 930 | static void handle_update(struct work_struct *work) |
1da177e4 | 931 | { |
65f27f38 DH |
932 | struct cpufreq_policy *policy = |
933 | container_of(work, struct cpufreq_policy, update); | |
934 | unsigned int cpu = policy->cpu; | |
1da177e4 LT |
935 | dprintk("handle_update for cpu %u called\n", cpu); |
936 | cpufreq_update_policy(cpu); | |
937 | } | |
938 | ||
939 | /** | |
940 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. | |
941 | * @cpu: cpu number | |
942 | * @old_freq: CPU frequency the kernel thinks the CPU runs at | |
943 | * @new_freq: CPU frequency the CPU actually runs at | |
944 | * | |
945 | * We adjust to current frequency first, and need to clean up later. So either call | |
946 | * to cpufreq_update_policy() or schedule handle_update()). | |
947 | */ | |
e08f5f5b GS |
948 | static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, |
949 | unsigned int new_freq) | |
1da177e4 LT |
950 | { |
951 | struct cpufreq_freqs freqs; | |
952 | ||
b10eec22 | 953 | dprintk("Warning: CPU frequency out of sync: cpufreq and timing " |
1da177e4 LT |
954 | "core thinks of %u, is %u kHz.\n", old_freq, new_freq); |
955 | ||
956 | freqs.cpu = cpu; | |
957 | freqs.old = old_freq; | |
958 | freqs.new = new_freq; | |
959 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); | |
960 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); | |
961 | } | |
962 | ||
963 | ||
32ee8c3e | 964 | /** |
4ab70df4 | 965 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
95235ca2 VP |
966 | * @cpu: CPU number |
967 | * | |
968 | * This is the last known freq, without actually getting it from the driver. | |
969 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. | |
970 | */ | |
971 | unsigned int cpufreq_quick_get(unsigned int cpu) | |
972 | { | |
973 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); | |
e08f5f5b | 974 | unsigned int ret_freq = 0; |
95235ca2 VP |
975 | |
976 | if (policy) { | |
83933af4 | 977 | mutex_lock(&policy->lock); |
e08f5f5b | 978 | ret_freq = policy->cur; |
83933af4 | 979 | mutex_unlock(&policy->lock); |
95235ca2 VP |
980 | cpufreq_cpu_put(policy); |
981 | } | |
982 | ||
e08f5f5b | 983 | return (ret_freq); |
95235ca2 VP |
984 | } |
985 | EXPORT_SYMBOL(cpufreq_quick_get); | |
986 | ||
987 | ||
32ee8c3e | 988 | /** |
1da177e4 LT |
989 | * cpufreq_get - get the current CPU frequency (in kHz) |
990 | * @cpu: CPU number | |
991 | * | |
992 | * Get the CPU current (static) CPU frequency | |
993 | */ | |
994 | unsigned int cpufreq_get(unsigned int cpu) | |
995 | { | |
996 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); | |
e08f5f5b | 997 | unsigned int ret_freq = 0; |
1da177e4 LT |
998 | |
999 | if (!policy) | |
1000 | return 0; | |
1001 | ||
1002 | if (!cpufreq_driver->get) | |
1003 | goto out; | |
1004 | ||
83933af4 | 1005 | mutex_lock(&policy->lock); |
1da177e4 | 1006 | |
e08f5f5b | 1007 | ret_freq = cpufreq_driver->get(cpu); |
1da177e4 | 1008 | |
e08f5f5b GS |
1009 | if (ret_freq && policy->cur && |
1010 | !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { | |
1011 | /* verify no discrepancy between actual and | |
1012 | saved value exists */ | |
1013 | if (unlikely(ret_freq != policy->cur)) { | |
1014 | cpufreq_out_of_sync(cpu, policy->cur, ret_freq); | |
1da177e4 LT |
1015 | schedule_work(&policy->update); |
1016 | } | |
1017 | } | |
1018 | ||
83933af4 | 1019 | mutex_unlock(&policy->lock); |
1da177e4 | 1020 | |
7d5e350f | 1021 | out: |
1da177e4 LT |
1022 | cpufreq_cpu_put(policy); |
1023 | ||
e08f5f5b | 1024 | return (ret_freq); |
1da177e4 LT |
1025 | } |
1026 | EXPORT_SYMBOL(cpufreq_get); | |
1027 | ||
1028 | ||
42d4dc3f BH |
1029 | /** |
1030 | * cpufreq_suspend - let the low level driver prepare for suspend | |
1031 | */ | |
1032 | ||
e00d9967 | 1033 | static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) |
42d4dc3f BH |
1034 | { |
1035 | int cpu = sysdev->id; | |
e08f5f5b | 1036 | int ret = 0; |
42d4dc3f BH |
1037 | unsigned int cur_freq = 0; |
1038 | struct cpufreq_policy *cpu_policy; | |
1039 | ||
0e37b159 | 1040 | dprintk("suspending cpu %u\n", cpu); |
42d4dc3f BH |
1041 | |
1042 | if (!cpu_online(cpu)) | |
1043 | return 0; | |
1044 | ||
1045 | /* we may be lax here as interrupts are off. Nonetheless | |
1046 | * we need to grab the correct cpu policy, as to check | |
1047 | * whether we really run on this CPU. | |
1048 | */ | |
1049 | ||
1050 | cpu_policy = cpufreq_cpu_get(cpu); | |
1051 | if (!cpu_policy) | |
1052 | return -EINVAL; | |
1053 | ||
1054 | /* only handle each CPU group once */ | |
1055 | if (unlikely(cpu_policy->cpu != cpu)) { | |
1056 | cpufreq_cpu_put(cpu_policy); | |
1057 | return 0; | |
1058 | } | |
1059 | ||
1060 | if (cpufreq_driver->suspend) { | |
e00d9967 | 1061 | ret = cpufreq_driver->suspend(cpu_policy, pmsg); |
42d4dc3f BH |
1062 | if (ret) { |
1063 | printk(KERN_ERR "cpufreq: suspend failed in ->suspend " | |
1064 | "step on CPU %u\n", cpu_policy->cpu); | |
1065 | cpufreq_cpu_put(cpu_policy); | |
1066 | return ret; | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | ||
1071 | if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) | |
1072 | goto out; | |
1073 | ||
1074 | if (cpufreq_driver->get) | |
1075 | cur_freq = cpufreq_driver->get(cpu_policy->cpu); | |
1076 | ||
1077 | if (!cur_freq || !cpu_policy->cur) { | |
1078 | printk(KERN_ERR "cpufreq: suspend failed to assert current " | |
1079 | "frequency is what timing core thinks it is.\n"); | |
1080 | goto out; | |
1081 | } | |
1082 | ||
1083 | if (unlikely(cur_freq != cpu_policy->cur)) { | |
1084 | struct cpufreq_freqs freqs; | |
1085 | ||
1086 | if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) | |
b10eec22 | 1087 | dprintk("Warning: CPU frequency is %u, " |
42d4dc3f BH |
1088 | "cpufreq assumed %u kHz.\n", |
1089 | cur_freq, cpu_policy->cur); | |
1090 | ||
1091 | freqs.cpu = cpu; | |
1092 | freqs.old = cpu_policy->cur; | |
1093 | freqs.new = cur_freq; | |
1094 | ||
b4dfdbb3 | 1095 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
42d4dc3f BH |
1096 | CPUFREQ_SUSPENDCHANGE, &freqs); |
1097 | adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); | |
1098 | ||
1099 | cpu_policy->cur = cur_freq; | |
1100 | } | |
1101 | ||
7d5e350f | 1102 | out: |
42d4dc3f BH |
1103 | cpufreq_cpu_put(cpu_policy); |
1104 | return 0; | |
1105 | } | |
1106 | ||
1da177e4 LT |
1107 | /** |
1108 | * cpufreq_resume - restore proper CPU frequency handling after resume | |
1109 | * | |
1110 | * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) | |
1111 | * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync | |
42d4dc3f BH |
1112 | * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are |
1113 | * restored. | |
1da177e4 LT |
1114 | */ |
1115 | static int cpufreq_resume(struct sys_device * sysdev) | |
1116 | { | |
1117 | int cpu = sysdev->id; | |
e08f5f5b | 1118 | int ret = 0; |
1da177e4 LT |
1119 | struct cpufreq_policy *cpu_policy; |
1120 | ||
1121 | dprintk("resuming cpu %u\n", cpu); | |
1122 | ||
1123 | if (!cpu_online(cpu)) | |
1124 | return 0; | |
1125 | ||
1126 | /* we may be lax here as interrupts are off. Nonetheless | |
1127 | * we need to grab the correct cpu policy, as to check | |
1128 | * whether we really run on this CPU. | |
1129 | */ | |
1130 | ||
1131 | cpu_policy = cpufreq_cpu_get(cpu); | |
1132 | if (!cpu_policy) | |
1133 | return -EINVAL; | |
1134 | ||
1135 | /* only handle each CPU group once */ | |
1136 | if (unlikely(cpu_policy->cpu != cpu)) { | |
1137 | cpufreq_cpu_put(cpu_policy); | |
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | if (cpufreq_driver->resume) { | |
1142 | ret = cpufreq_driver->resume(cpu_policy); | |
1143 | if (ret) { | |
1144 | printk(KERN_ERR "cpufreq: resume failed in ->resume " | |
1145 | "step on CPU %u\n", cpu_policy->cpu); | |
1146 | cpufreq_cpu_put(cpu_policy); | |
1147 | return ret; | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { | |
1152 | unsigned int cur_freq = 0; | |
1153 | ||
1154 | if (cpufreq_driver->get) | |
1155 | cur_freq = cpufreq_driver->get(cpu_policy->cpu); | |
1156 | ||
1157 | if (!cur_freq || !cpu_policy->cur) { | |
42d4dc3f BH |
1158 | printk(KERN_ERR "cpufreq: resume failed to assert " |
1159 | "current frequency is what timing core " | |
1160 | "thinks it is.\n"); | |
1da177e4 LT |
1161 | goto out; |
1162 | } | |
1163 | ||
1164 | if (unlikely(cur_freq != cpu_policy->cur)) { | |
1165 | struct cpufreq_freqs freqs; | |
1166 | ||
ac09f698 | 1167 | if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) |
b10eec22 | 1168 | dprintk("Warning: CPU frequency" |
ac09f698 BH |
1169 | "is %u, cpufreq assumed %u kHz.\n", |
1170 | cur_freq, cpu_policy->cur); | |
1da177e4 LT |
1171 | |
1172 | freqs.cpu = cpu; | |
1173 | freqs.old = cpu_policy->cur; | |
1174 | freqs.new = cur_freq; | |
1175 | ||
b4dfdbb3 | 1176 | srcu_notifier_call_chain( |
e041c683 | 1177 | &cpufreq_transition_notifier_list, |
42d4dc3f | 1178 | CPUFREQ_RESUMECHANGE, &freqs); |
1da177e4 LT |
1179 | adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); |
1180 | ||
1181 | cpu_policy->cur = cur_freq; | |
1182 | } | |
1183 | } | |
1184 | ||
1185 | out: | |
1186 | schedule_work(&cpu_policy->update); | |
1187 | cpufreq_cpu_put(cpu_policy); | |
1188 | return ret; | |
1189 | } | |
1190 | ||
1191 | static struct sysdev_driver cpufreq_sysdev_driver = { | |
1192 | .add = cpufreq_add_dev, | |
1193 | .remove = cpufreq_remove_dev, | |
42d4dc3f | 1194 | .suspend = cpufreq_suspend, |
1da177e4 LT |
1195 | .resume = cpufreq_resume, |
1196 | }; | |
1197 | ||
1198 | ||
1199 | /********************************************************************* | |
1200 | * NOTIFIER LISTS INTERFACE * | |
1201 | *********************************************************************/ | |
1202 | ||
1203 | /** | |
1204 | * cpufreq_register_notifier - register a driver with cpufreq | |
1205 | * @nb: notifier function to register | |
1206 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER | |
1207 | * | |
32ee8c3e | 1208 | * Add a driver to one of two lists: either a list of drivers that |
1da177e4 LT |
1209 | * are notified about clock rate changes (once before and once after |
1210 | * the transition), or a list of drivers that are notified about | |
1211 | * changes in cpufreq policy. | |
1212 | * | |
1213 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1214 | * blocking_notifier_chain_register. |
1da177e4 LT |
1215 | */ |
1216 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) | |
1217 | { | |
1218 | int ret; | |
1219 | ||
1da177e4 LT |
1220 | switch (list) { |
1221 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b4dfdbb3 | 1222 | ret = srcu_notifier_chain_register( |
e041c683 | 1223 | &cpufreq_transition_notifier_list, nb); |
1da177e4 LT |
1224 | break; |
1225 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1226 | ret = blocking_notifier_chain_register( |
1227 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1228 | break; |
1229 | default: | |
1230 | ret = -EINVAL; | |
1231 | } | |
1da177e4 LT |
1232 | |
1233 | return ret; | |
1234 | } | |
1235 | EXPORT_SYMBOL(cpufreq_register_notifier); | |
1236 | ||
1237 | ||
1238 | /** | |
1239 | * cpufreq_unregister_notifier - unregister a driver with cpufreq | |
1240 | * @nb: notifier block to be unregistered | |
1241 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER | |
1242 | * | |
1243 | * Remove a driver from the CPU frequency notifier list. | |
1244 | * | |
1245 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1246 | * blocking_notifier_chain_unregister. |
1da177e4 LT |
1247 | */ |
1248 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) | |
1249 | { | |
1250 | int ret; | |
1251 | ||
1da177e4 LT |
1252 | switch (list) { |
1253 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b4dfdbb3 | 1254 | ret = srcu_notifier_chain_unregister( |
e041c683 | 1255 | &cpufreq_transition_notifier_list, nb); |
1da177e4 LT |
1256 | break; |
1257 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1258 | ret = blocking_notifier_chain_unregister( |
1259 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1260 | break; |
1261 | default: | |
1262 | ret = -EINVAL; | |
1263 | } | |
1da177e4 LT |
1264 | |
1265 | return ret; | |
1266 | } | |
1267 | EXPORT_SYMBOL(cpufreq_unregister_notifier); | |
1268 | ||
1269 | ||
1270 | /********************************************************************* | |
1271 | * GOVERNORS * | |
1272 | *********************************************************************/ | |
1273 | ||
1274 | ||
1275 | int __cpufreq_driver_target(struct cpufreq_policy *policy, | |
1276 | unsigned int target_freq, | |
1277 | unsigned int relation) | |
1278 | { | |
1279 | int retval = -EINVAL; | |
c32b6b8e | 1280 | |
1da177e4 LT |
1281 | dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, |
1282 | target_freq, relation); | |
1283 | if (cpu_online(policy->cpu) && cpufreq_driver->target) | |
1284 | retval = cpufreq_driver->target(policy, target_freq, relation); | |
90d45d17 | 1285 | |
1da177e4 LT |
1286 | return retval; |
1287 | } | |
1288 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); | |
1289 | ||
1da177e4 LT |
1290 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
1291 | unsigned int target_freq, | |
1292 | unsigned int relation) | |
1293 | { | |
cc993cab | 1294 | int ret; |
1da177e4 LT |
1295 | |
1296 | policy = cpufreq_cpu_get(policy->cpu); | |
1297 | if (!policy) | |
1298 | return -EINVAL; | |
1299 | ||
83933af4 | 1300 | mutex_lock(&policy->lock); |
1da177e4 LT |
1301 | |
1302 | ret = __cpufreq_driver_target(policy, target_freq, relation); | |
1303 | ||
83933af4 | 1304 | mutex_unlock(&policy->lock); |
1da177e4 LT |
1305 | |
1306 | cpufreq_cpu_put(policy); | |
1da177e4 LT |
1307 | return ret; |
1308 | } | |
1309 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); | |
1310 | ||
dfde5d62 VP |
1311 | int cpufreq_driver_getavg(struct cpufreq_policy *policy) |
1312 | { | |
1313 | int ret = 0; | |
1314 | ||
1315 | policy = cpufreq_cpu_get(policy->cpu); | |
1316 | if (!policy) | |
1317 | return -EINVAL; | |
1318 | ||
1319 | mutex_lock(&policy->lock); | |
1320 | ||
1321 | if (cpu_online(policy->cpu) && cpufreq_driver->getavg) | |
1322 | ret = cpufreq_driver->getavg(policy->cpu); | |
1323 | ||
1324 | mutex_unlock(&policy->lock); | |
1325 | ||
1326 | cpufreq_cpu_put(policy); | |
1327 | return ret; | |
1328 | } | |
1329 | EXPORT_SYMBOL_GPL(cpufreq_driver_getavg); | |
1330 | ||
153d7f3f | 1331 | /* |
153d7f3f AV |
1332 | * when "event" is CPUFREQ_GOV_LIMITS |
1333 | */ | |
1da177e4 | 1334 | |
e08f5f5b GS |
1335 | static int __cpufreq_governor(struct cpufreq_policy *policy, |
1336 | unsigned int event) | |
1da177e4 | 1337 | { |
cc993cab | 1338 | int ret; |
1da177e4 LT |
1339 | |
1340 | if (!try_module_get(policy->governor->owner)) | |
1341 | return -EINVAL; | |
1342 | ||
e08f5f5b GS |
1343 | dprintk("__cpufreq_governor for CPU %u, event %u\n", |
1344 | policy->cpu, event); | |
1da177e4 LT |
1345 | ret = policy->governor->governor(policy, event); |
1346 | ||
e08f5f5b GS |
1347 | /* we keep one module reference alive for |
1348 | each CPU governed by this CPU */ | |
1da177e4 LT |
1349 | if ((event != CPUFREQ_GOV_START) || ret) |
1350 | module_put(policy->governor->owner); | |
1351 | if ((event == CPUFREQ_GOV_STOP) && !ret) | |
1352 | module_put(policy->governor->owner); | |
1353 | ||
1354 | return ret; | |
1355 | } | |
1356 | ||
1357 | ||
1da177e4 LT |
1358 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
1359 | { | |
3bcb09a3 | 1360 | int err; |
1da177e4 LT |
1361 | |
1362 | if (!governor) | |
1363 | return -EINVAL; | |
1364 | ||
3fc54d37 | 1365 | mutex_lock(&cpufreq_governor_mutex); |
32ee8c3e | 1366 | |
3bcb09a3 JF |
1367 | err = -EBUSY; |
1368 | if (__find_governor(governor->name) == NULL) { | |
1369 | err = 0; | |
1370 | list_add(&governor->governor_list, &cpufreq_governor_list); | |
1da177e4 | 1371 | } |
1da177e4 | 1372 | |
32ee8c3e | 1373 | mutex_unlock(&cpufreq_governor_mutex); |
3bcb09a3 | 1374 | return err; |
1da177e4 LT |
1375 | } |
1376 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); | |
1377 | ||
1378 | ||
1379 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) | |
1380 | { | |
1381 | if (!governor) | |
1382 | return; | |
1383 | ||
3fc54d37 | 1384 | mutex_lock(&cpufreq_governor_mutex); |
1da177e4 | 1385 | list_del(&governor->governor_list); |
3fc54d37 | 1386 | mutex_unlock(&cpufreq_governor_mutex); |
1da177e4 LT |
1387 | return; |
1388 | } | |
1389 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); | |
1390 | ||
1391 | ||
1392 | ||
1393 | /********************************************************************* | |
1394 | * POLICY INTERFACE * | |
1395 | *********************************************************************/ | |
1396 | ||
1397 | /** | |
1398 | * cpufreq_get_policy - get the current cpufreq_policy | |
1399 | * @policy: struct cpufreq_policy into which the current cpufreq_policy is written | |
1400 | * | |
1401 | * Reads the current cpufreq policy. | |
1402 | */ | |
1403 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) | |
1404 | { | |
1405 | struct cpufreq_policy *cpu_policy; | |
1406 | if (!policy) | |
1407 | return -EINVAL; | |
1408 | ||
1409 | cpu_policy = cpufreq_cpu_get(cpu); | |
1410 | if (!cpu_policy) | |
1411 | return -EINVAL; | |
1412 | ||
83933af4 | 1413 | mutex_lock(&cpu_policy->lock); |
1da177e4 | 1414 | memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); |
83933af4 | 1415 | mutex_unlock(&cpu_policy->lock); |
1da177e4 LT |
1416 | |
1417 | cpufreq_cpu_put(cpu_policy); | |
1da177e4 LT |
1418 | return 0; |
1419 | } | |
1420 | EXPORT_SYMBOL(cpufreq_get_policy); | |
1421 | ||
1422 | ||
153d7f3f | 1423 | /* |
e08f5f5b GS |
1424 | * data : current policy. |
1425 | * policy : policy to be set. | |
153d7f3f | 1426 | */ |
e08f5f5b GS |
1427 | static int __cpufreq_set_policy(struct cpufreq_policy *data, |
1428 | struct cpufreq_policy *policy) | |
1da177e4 LT |
1429 | { |
1430 | int ret = 0; | |
1431 | ||
1432 | cpufreq_debug_disable_ratelimit(); | |
1433 | dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, | |
1434 | policy->min, policy->max); | |
1435 | ||
e08f5f5b GS |
1436 | memcpy(&policy->cpuinfo, &data->cpuinfo, |
1437 | sizeof(struct cpufreq_cpuinfo)); | |
1da177e4 | 1438 | |
9c9a43ed MD |
1439 | if (policy->min > data->min && policy->min > policy->max) { |
1440 | ret = -EINVAL; | |
1441 | goto error_out; | |
1442 | } | |
1443 | ||
1da177e4 LT |
1444 | /* verify the cpu speed can be set within this limit */ |
1445 | ret = cpufreq_driver->verify(policy); | |
1446 | if (ret) | |
1447 | goto error_out; | |
1448 | ||
1da177e4 | 1449 | /* adjust if necessary - all reasons */ |
e041c683 AS |
1450 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1451 | CPUFREQ_ADJUST, policy); | |
1da177e4 LT |
1452 | |
1453 | /* adjust if necessary - hardware incompatibility*/ | |
e041c683 AS |
1454 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1455 | CPUFREQ_INCOMPATIBLE, policy); | |
1da177e4 LT |
1456 | |
1457 | /* verify the cpu speed can be set within this limit, | |
1458 | which might be different to the first one */ | |
1459 | ret = cpufreq_driver->verify(policy); | |
e041c683 | 1460 | if (ret) |
1da177e4 | 1461 | goto error_out; |
1da177e4 LT |
1462 | |
1463 | /* notification of the new policy */ | |
e041c683 AS |
1464 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1465 | CPUFREQ_NOTIFY, policy); | |
1da177e4 | 1466 | |
7d5e350f DJ |
1467 | data->min = policy->min; |
1468 | data->max = policy->max; | |
1da177e4 | 1469 | |
e08f5f5b GS |
1470 | dprintk("new min and max freqs are %u - %u kHz\n", |
1471 | data->min, data->max); | |
1da177e4 LT |
1472 | |
1473 | if (cpufreq_driver->setpolicy) { | |
1474 | data->policy = policy->policy; | |
1475 | dprintk("setting range\n"); | |
1476 | ret = cpufreq_driver->setpolicy(policy); | |
1477 | } else { | |
1478 | if (policy->governor != data->governor) { | |
1479 | /* save old, working values */ | |
1480 | struct cpufreq_governor *old_gov = data->governor; | |
1481 | ||
1482 | dprintk("governor switch\n"); | |
1483 | ||
1484 | /* end old governor */ | |
1485 | if (data->governor) | |
1486 | __cpufreq_governor(data, CPUFREQ_GOV_STOP); | |
1487 | ||
1488 | /* start new governor */ | |
1489 | data->governor = policy->governor; | |
1490 | if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { | |
1491 | /* new governor failed, so re-start old one */ | |
e08f5f5b GS |
1492 | dprintk("starting governor %s failed\n", |
1493 | data->governor->name); | |
1da177e4 LT |
1494 | if (old_gov) { |
1495 | data->governor = old_gov; | |
e08f5f5b GS |
1496 | __cpufreq_governor(data, |
1497 | CPUFREQ_GOV_START); | |
1da177e4 LT |
1498 | } |
1499 | ret = -EINVAL; | |
1500 | goto error_out; | |
1501 | } | |
1502 | /* might be a policy change, too, so fall through */ | |
1503 | } | |
1504 | dprintk("governor: change or update limits\n"); | |
1505 | __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); | |
1506 | } | |
1507 | ||
7d5e350f | 1508 | error_out: |
1da177e4 LT |
1509 | cpufreq_debug_enable_ratelimit(); |
1510 | return ret; | |
1511 | } | |
1512 | ||
1513 | /** | |
1514 | * cpufreq_set_policy - set a new CPUFreq policy | |
1515 | * @policy: policy to be set. | |
1516 | * | |
1517 | * Sets a new CPU frequency and voltage scaling policy. | |
1518 | */ | |
1519 | int cpufreq_set_policy(struct cpufreq_policy *policy) | |
1520 | { | |
1521 | int ret = 0; | |
1522 | struct cpufreq_policy *data; | |
1523 | ||
1524 | if (!policy) | |
1525 | return -EINVAL; | |
1526 | ||
1527 | data = cpufreq_cpu_get(policy->cpu); | |
1528 | if (!data) | |
1529 | return -EINVAL; | |
1530 | ||
1531 | /* lock this CPU */ | |
83933af4 | 1532 | mutex_lock(&data->lock); |
1da177e4 LT |
1533 | |
1534 | ret = __cpufreq_set_policy(data, policy); | |
1535 | data->user_policy.min = data->min; | |
1536 | data->user_policy.max = data->max; | |
1537 | data->user_policy.policy = data->policy; | |
1538 | data->user_policy.governor = data->governor; | |
1539 | ||
83933af4 | 1540 | mutex_unlock(&data->lock); |
153d7f3f | 1541 | |
1da177e4 LT |
1542 | cpufreq_cpu_put(data); |
1543 | ||
1544 | return ret; | |
1545 | } | |
1546 | EXPORT_SYMBOL(cpufreq_set_policy); | |
1547 | ||
1548 | ||
1549 | /** | |
1550 | * cpufreq_update_policy - re-evaluate an existing cpufreq policy | |
1551 | * @cpu: CPU which shall be re-evaluated | |
1552 | * | |
1553 | * Usefull for policy notifiers which have different necessities | |
1554 | * at different times. | |
1555 | */ | |
1556 | int cpufreq_update_policy(unsigned int cpu) | |
1557 | { | |
1558 | struct cpufreq_policy *data = cpufreq_cpu_get(cpu); | |
1559 | struct cpufreq_policy policy; | |
1560 | int ret = 0; | |
1561 | ||
1562 | if (!data) | |
1563 | return -ENODEV; | |
1564 | ||
83933af4 | 1565 | mutex_lock(&data->lock); |
1da177e4 LT |
1566 | |
1567 | dprintk("updating policy for CPU %u\n", cpu); | |
7d5e350f | 1568 | memcpy(&policy, data, sizeof(struct cpufreq_policy)); |
1da177e4 LT |
1569 | policy.min = data->user_policy.min; |
1570 | policy.max = data->user_policy.max; | |
1571 | policy.policy = data->user_policy.policy; | |
1572 | policy.governor = data->user_policy.governor; | |
1573 | ||
0961dd0d TR |
1574 | /* BIOS might change freq behind our back |
1575 | -> ask driver for current freq and notify governors about a change */ | |
1576 | if (cpufreq_driver->get) { | |
1577 | policy.cur = cpufreq_driver->get(cpu); | |
a85f7bd3 TR |
1578 | if (!data->cur) { |
1579 | dprintk("Driver did not initialize current freq"); | |
1580 | data->cur = policy.cur; | |
1581 | } else { | |
1582 | if (data->cur != policy.cur) | |
e08f5f5b GS |
1583 | cpufreq_out_of_sync(cpu, data->cur, |
1584 | policy.cur); | |
a85f7bd3 | 1585 | } |
0961dd0d TR |
1586 | } |
1587 | ||
1da177e4 LT |
1588 | ret = __cpufreq_set_policy(data, &policy); |
1589 | ||
83933af4 | 1590 | mutex_unlock(&data->lock); |
1da177e4 LT |
1591 | cpufreq_cpu_put(data); |
1592 | return ret; | |
1593 | } | |
1594 | EXPORT_SYMBOL(cpufreq_update_policy); | |
1595 | ||
65edc68c | 1596 | static int cpufreq_cpu_callback(struct notifier_block *nfb, |
c32b6b8e AR |
1597 | unsigned long action, void *hcpu) |
1598 | { | |
1599 | unsigned int cpu = (unsigned long)hcpu; | |
1600 | struct cpufreq_policy *policy; | |
1601 | struct sys_device *sys_dev; | |
1602 | ||
1603 | sys_dev = get_cpu_sysdev(cpu); | |
1604 | ||
1605 | if (sys_dev) { | |
1606 | switch (action) { | |
1607 | case CPU_ONLINE: | |
1608 | cpufreq_add_dev(sys_dev); | |
1609 | break; | |
1610 | case CPU_DOWN_PREPARE: | |
1611 | /* | |
1612 | * We attempt to put this cpu in lowest frequency | |
1613 | * possible before going down. This will permit | |
1614 | * hardware-managed P-State to switch other related | |
1615 | * threads to min or higher speeds if possible. | |
1616 | */ | |
1617 | policy = cpufreq_cpu_data[cpu]; | |
1618 | if (policy) { | |
1619 | cpufreq_driver_target(policy, policy->min, | |
1620 | CPUFREQ_RELATION_H); | |
1621 | } | |
1622 | break; | |
1623 | case CPU_DEAD: | |
1624 | cpufreq_remove_dev(sys_dev); | |
1625 | break; | |
1626 | } | |
1627 | } | |
1628 | return NOTIFY_OK; | |
1629 | } | |
1630 | ||
74b85f37 | 1631 | static struct notifier_block __cpuinitdata cpufreq_cpu_notifier = |
c32b6b8e AR |
1632 | { |
1633 | .notifier_call = cpufreq_cpu_callback, | |
1634 | }; | |
1da177e4 LT |
1635 | |
1636 | /********************************************************************* | |
1637 | * REGISTER / UNREGISTER CPUFREQ DRIVER * | |
1638 | *********************************************************************/ | |
1639 | ||
1640 | /** | |
1641 | * cpufreq_register_driver - register a CPU Frequency driver | |
1642 | * @driver_data: A struct cpufreq_driver containing the values# | |
1643 | * submitted by the CPU Frequency driver. | |
1644 | * | |
32ee8c3e | 1645 | * Registers a CPU Frequency driver to this core code. This code |
1da177e4 | 1646 | * returns zero on success, -EBUSY when another driver got here first |
32ee8c3e | 1647 | * (and isn't unregistered in the meantime). |
1da177e4 LT |
1648 | * |
1649 | */ | |
1650 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) | |
1651 | { | |
1652 | unsigned long flags; | |
1653 | int ret; | |
1654 | ||
1655 | if (!driver_data || !driver_data->verify || !driver_data->init || | |
1656 | ((!driver_data->setpolicy) && (!driver_data->target))) | |
1657 | return -EINVAL; | |
1658 | ||
1659 | dprintk("trying to register driver %s\n", driver_data->name); | |
1660 | ||
1661 | if (driver_data->setpolicy) | |
1662 | driver_data->flags |= CPUFREQ_CONST_LOOPS; | |
1663 | ||
1664 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
1665 | if (cpufreq_driver) { | |
1666 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1667 | return -EBUSY; | |
1668 | } | |
1669 | cpufreq_driver = driver_data; | |
1670 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1671 | ||
1672 | ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); | |
1673 | ||
1674 | if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { | |
1675 | int i; | |
1676 | ret = -ENODEV; | |
1677 | ||
1678 | /* check for at least one working CPU */ | |
1679 | for (i=0; i<NR_CPUS; i++) | |
1680 | if (cpufreq_cpu_data[i]) | |
1681 | ret = 0; | |
1682 | ||
1683 | /* if all ->init() calls failed, unregister */ | |
1684 | if (ret) { | |
e08f5f5b GS |
1685 | dprintk("no CPU initialized for driver %s\n", |
1686 | driver_data->name); | |
1687 | sysdev_driver_unregister(&cpu_sysdev_class, | |
1688 | &cpufreq_sysdev_driver); | |
1da177e4 LT |
1689 | |
1690 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
1691 | cpufreq_driver = NULL; | |
1692 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | if (!ret) { | |
65edc68c | 1697 | register_hotcpu_notifier(&cpufreq_cpu_notifier); |
1da177e4 LT |
1698 | dprintk("driver %s up and running\n", driver_data->name); |
1699 | cpufreq_debug_enable_ratelimit(); | |
1700 | } | |
1701 | ||
1702 | return (ret); | |
1703 | } | |
1704 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); | |
1705 | ||
1706 | ||
1707 | /** | |
1708 | * cpufreq_unregister_driver - unregister the current CPUFreq driver | |
1709 | * | |
32ee8c3e | 1710 | * Unregister the current CPUFreq driver. Only call this if you have |
1da177e4 LT |
1711 | * the right to do so, i.e. if you have succeeded in initialising before! |
1712 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is | |
1713 | * currently not initialised. | |
1714 | */ | |
1715 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) | |
1716 | { | |
1717 | unsigned long flags; | |
1718 | ||
1719 | cpufreq_debug_disable_ratelimit(); | |
1720 | ||
1721 | if (!cpufreq_driver || (driver != cpufreq_driver)) { | |
1722 | cpufreq_debug_enable_ratelimit(); | |
1723 | return -EINVAL; | |
1724 | } | |
1725 | ||
1726 | dprintk("unregistering driver %s\n", driver->name); | |
1727 | ||
1728 | sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); | |
65edc68c | 1729 | unregister_hotcpu_notifier(&cpufreq_cpu_notifier); |
1da177e4 LT |
1730 | |
1731 | spin_lock_irqsave(&cpufreq_driver_lock, flags); | |
1732 | cpufreq_driver = NULL; | |
1733 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1734 | ||
1735 | return 0; | |
1736 | } | |
1737 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); |