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