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