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