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