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