config: tegra3: enable /dev mount with ACL
[linux-2.6.git] / drivers / cpufreq / cpufreq.c
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/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/notifier.h>
22 #include <linux/cpufreq.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
31 #include <linux/syscore_ops.h>
32 #include <linux/pm_qos_params.h>
33
34 #include <trace/events/power.h>
35
36 /**
37  * The "cpufreq driver" - the arch- or hardware-dependent low
38  * level driver of CPUFreq support, and its spinlock. This lock
39  * also protects the cpufreq_cpu_data array.
40  */
41 static struct cpufreq_driver *cpufreq_driver;
42 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
43 #ifdef CONFIG_HOTPLUG_CPU
44 /* This one keeps track of the previously set governor of a removed CPU */
45 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
46 #endif
47 static DEFINE_SPINLOCK(cpufreq_driver_lock);
48
49 /*
50  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
51  * all cpufreq/hotplug/workqueue/etc related lock issues.
52  *
53  * The rules for this semaphore:
54  * - Any routine that wants to read from the policy structure will
55  *   do a down_read on this semaphore.
56  * - Any routine that will write to the policy structure and/or may take away
57  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
58  *   mode before doing so.
59  *
60  * Additional rules:
61  * - All holders of the lock should check to make sure that the CPU they
62  *   are concerned with are online after they get the lock.
63  * - Governor routines that can be called in cpufreq hotplug path should not
64  *   take this sem as top level hotplug notifier handler takes this.
65  * - Lock should not be held across
66  *     __cpufreq_governor(data, CPUFREQ_GOV_STOP);
67  */
68 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
69 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
70
71 #define lock_policy_rwsem(mode, cpu)                                    \
72 static int lock_policy_rwsem_##mode                                     \
73 (int cpu)                                                               \
74 {                                                                       \
75         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);              \
76         BUG_ON(policy_cpu == -1);                                       \
77         down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));            \
78         if (unlikely(!cpu_online(cpu))) {                               \
79                 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));      \
80                 return -1;                                              \
81         }                                                               \
82                                                                         \
83         return 0;                                                       \
84 }
85
86 lock_policy_rwsem(read, cpu);
87
88 lock_policy_rwsem(write, cpu);
89
90 static void unlock_policy_rwsem_read(int cpu)
91 {
92         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
93         BUG_ON(policy_cpu == -1);
94         up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
95 }
96
97 static void unlock_policy_rwsem_write(int cpu)
98 {
99         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
100         BUG_ON(policy_cpu == -1);
101         up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
102 }
103
104
105 /* internal prototypes */
106 static int __cpufreq_governor(struct cpufreq_policy *policy,
107                 unsigned int event);
108 static unsigned int __cpufreq_get(unsigned int cpu);
109 static void handle_update(struct work_struct *work);
110
111 /**
112  * Two notifier lists: the "policy" list is involved in the
113  * validation process for a new CPU frequency policy; the
114  * "transition" list for kernel code that needs to handle
115  * changes to devices when the CPU clock speed changes.
116  * The mutex locks both lists.
117  */
118 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
119 static struct srcu_notifier_head cpufreq_transition_notifier_list;
120
121 static bool init_cpufreq_transition_notifier_list_called;
122 static int __init init_cpufreq_transition_notifier_list(void)
123 {
124         srcu_init_notifier_head(&cpufreq_transition_notifier_list);
125         init_cpufreq_transition_notifier_list_called = true;
126         return 0;
127 }
128 pure_initcall(init_cpufreq_transition_notifier_list);
129
130 static LIST_HEAD(cpufreq_governor_list);
131 static DEFINE_MUTEX(cpufreq_governor_mutex);
132
133 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
134 {
135         struct cpufreq_policy *data;
136         unsigned long flags;
137
138         if (cpu >= nr_cpu_ids)
139                 goto err_out;
140
141         /* get the cpufreq driver */
142         spin_lock_irqsave(&cpufreq_driver_lock, flags);
143
144         if (!cpufreq_driver)
145                 goto err_out_unlock;
146
147         if (!try_module_get(cpufreq_driver->owner))
148                 goto err_out_unlock;
149
150
151         /* get the CPU */
152         data = per_cpu(cpufreq_cpu_data, cpu);
153
154         if (!data)
155                 goto err_out_put_module;
156
157         if (!kobject_get(&data->kobj))
158                 goto err_out_put_module;
159
160         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
161         return data;
162
163 err_out_put_module:
164         module_put(cpufreq_driver->owner);
165 err_out_unlock:
166         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
167 err_out:
168         return NULL;
169 }
170 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
171
172
173 void cpufreq_cpu_put(struct cpufreq_policy *data)
174 {
175         kobject_put(&data->kobj);
176         module_put(cpufreq_driver->owner);
177 }
178 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
179
180
181 /*********************************************************************
182  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
183  *********************************************************************/
184
185 /**
186  * adjust_jiffies - adjust the system "loops_per_jiffy"
187  *
188  * This function alters the system "loops_per_jiffy" for the clock
189  * speed change. Note that loops_per_jiffy cannot be updated on SMP
190  * systems as each CPU might be scaled differently. So, use the arch
191  * per-CPU loops_per_jiffy value wherever possible.
192  */
193 #ifndef CONFIG_SMP
194 static unsigned long l_p_j_ref;
195 static unsigned int  l_p_j_ref_freq;
196
197 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
198 {
199         if (ci->flags & CPUFREQ_CONST_LOOPS)
200                 return;
201
202         if (!l_p_j_ref_freq) {
203                 l_p_j_ref = loops_per_jiffy;
204                 l_p_j_ref_freq = ci->old;
205                 pr_debug("saving %lu as reference value for loops_per_jiffy; "
206                         "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
207         }
208         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
209             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
210             (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
211                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
212                                                                 ci->new);
213                 pr_debug("scaling loops_per_jiffy to %lu "
214                         "for frequency %u kHz\n", loops_per_jiffy, ci->new);
215         }
216 }
217 #else
218 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
219 {
220         return;
221 }
222 #endif
223
224
225 /**
226  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
227  * on frequency transition.
228  *
229  * This function calls the transition notifiers and the "adjust_jiffies"
230  * function. It is called twice on all CPU frequency changes that have
231  * external effects.
232  */
233 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
234 {
235         struct cpufreq_policy *policy;
236
237         BUG_ON(irqs_disabled());
238
239         freqs->flags = cpufreq_driver->flags;
240         pr_debug("notification %u of frequency transition to %u kHz\n",
241                 state, freqs->new);
242
243         policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
244         switch (state) {
245
246         case CPUFREQ_PRECHANGE:
247                 /* detect if the driver reported a value as "old frequency"
248                  * which is not equal to what the cpufreq core thinks is
249                  * "old frequency".
250                  */
251                 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
252                         if ((policy) && (policy->cpu == freqs->cpu) &&
253                             (policy->cur) && (policy->cur != freqs->old)) {
254                                 pr_debug("Warning: CPU frequency is"
255                                         " %u, cpufreq assumed %u kHz.\n",
256                                         freqs->old, policy->cur);
257                                 freqs->old = policy->cur;
258                         }
259                 }
260                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
261                                 CPUFREQ_PRECHANGE, freqs);
262                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
263                 break;
264
265         case CPUFREQ_POSTCHANGE:
266                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
267                 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
268                         (unsigned long)freqs->cpu);
269                 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
270                 trace_cpu_frequency(freqs->new, freqs->cpu);
271                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
272                                 CPUFREQ_POSTCHANGE, freqs);
273                 if (likely(policy) && likely(policy->cpu == freqs->cpu))
274                         policy->cur = freqs->new;
275                 break;
276         }
277 }
278 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
279
280
281
282 /*********************************************************************
283  *                          SYSFS INTERFACE                          *
284  *********************************************************************/
285
286 static struct cpufreq_governor *__find_governor(const char *str_governor)
287 {
288         struct cpufreq_governor *t;
289
290         list_for_each_entry(t, &cpufreq_governor_list, governor_list)
291                 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
292                         return t;
293
294         return NULL;
295 }
296
297 /**
298  * cpufreq_parse_governor - parse a governor string
299  */
300 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
301                                 struct cpufreq_governor **governor)
302 {
303         int err = -EINVAL;
304
305         if (!cpufreq_driver)
306                 goto out;
307
308         if (cpufreq_driver->setpolicy) {
309                 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
310                         *policy = CPUFREQ_POLICY_PERFORMANCE;
311                         err = 0;
312                 } else if (!strnicmp(str_governor, "powersave",
313                                                 CPUFREQ_NAME_LEN)) {
314                         *policy = CPUFREQ_POLICY_POWERSAVE;
315                         err = 0;
316                 }
317         } else if (cpufreq_driver->target) {
318                 struct cpufreq_governor *t;
319
320                 mutex_lock(&cpufreq_governor_mutex);
321
322                 t = __find_governor(str_governor);
323
324                 if (t == NULL) {
325                         int ret;
326
327                         mutex_unlock(&cpufreq_governor_mutex);
328                         ret = request_module("cpufreq_%s", str_governor);
329                         mutex_lock(&cpufreq_governor_mutex);
330
331                         if (ret == 0)
332                                 t = __find_governor(str_governor);
333                 }
334
335                 if (t != NULL) {
336                         *governor = t;
337                         err = 0;
338                 }
339
340                 mutex_unlock(&cpufreq_governor_mutex);
341         }
342 out:
343         return err;
344 }
345
346
347 /**
348  * cpufreq_per_cpu_attr_read() / show_##file_name() -
349  * print out cpufreq information
350  *
351  * Write out information from cpufreq_driver->policy[cpu]; object must be
352  * "unsigned int".
353  */
354
355 #define show_one(file_name, object)                     \
356 static ssize_t show_##file_name                         \
357 (struct cpufreq_policy *policy, char *buf)              \
358 {                                                       \
359         return sprintf(buf, "%u\n", policy->object);    \
360 }
361
362 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
363 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
364 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
365 show_one(scaling_min_freq, min);
366 show_one(scaling_max_freq, max);
367 show_one(scaling_cur_freq, cur);
368 show_one(policy_min_freq, user_policy.min);
369 show_one(policy_max_freq, user_policy.max);
370
371 static int __cpufreq_set_policy(struct cpufreq_policy *data,
372                                 struct cpufreq_policy *policy);
373
374 /**
375  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
376  */
377 #define store_one(file_name, object)                    \
378 static ssize_t store_##file_name                                        \
379 (struct cpufreq_policy *policy, const char *buf, size_t count)          \
380 {                                                                       \
381         unsigned int ret = -EINVAL;                                     \
382         struct cpufreq_policy new_policy;                               \
383                                                                         \
384         ret = cpufreq_get_policy(&new_policy, policy->cpu);             \
385         if (ret)                                                        \
386                 return -EINVAL;                                         \
387                                                                         \
388         ret = sscanf(buf, "%u", &new_policy.object);                    \
389         if (ret != 1)                                                   \
390                 return -EINVAL;                                         \
391                                                                         \
392         ret = __cpufreq_set_policy(policy, &new_policy);                \
393         policy->user_policy.object = new_policy.object;                 \
394                                                                         \
395         return ret ? ret : count;                                       \
396 }
397
398 store_one(scaling_min_freq, min);
399 store_one(scaling_max_freq, max);
400
401 /**
402  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
403  */
404 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
405                                         char *buf)
406 {
407         unsigned int cur_freq = __cpufreq_get(policy->cpu);
408         if (!cur_freq)
409                 return sprintf(buf, "<unknown>");
410         return sprintf(buf, "%u\n", cur_freq);
411 }
412
413
414 /**
415  * show_scaling_governor - show the current policy for the specified CPU
416  */
417 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
418 {
419         if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
420                 return sprintf(buf, "powersave\n");
421         else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
422                 return sprintf(buf, "performance\n");
423         else if (policy->governor)
424                 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
425                                 policy->governor->name);
426         return -EINVAL;
427 }
428
429
430 /**
431  * store_scaling_governor - store policy for the specified CPU
432  */
433 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
434                                         const char *buf, size_t count)
435 {
436         unsigned int ret = -EINVAL;
437         char    str_governor[16];
438         struct cpufreq_policy new_policy;
439
440         ret = cpufreq_get_policy(&new_policy, policy->cpu);
441         if (ret)
442                 return ret;
443
444         ret = sscanf(buf, "%15s", str_governor);
445         if (ret != 1)
446                 return -EINVAL;
447
448         if (cpufreq_parse_governor(str_governor, &new_policy.policy,
449                                                 &new_policy.governor))
450                 return -EINVAL;
451
452         /* Do not use cpufreq_set_policy here or the user_policy.max
453            will be wrongly overridden */
454         ret = __cpufreq_set_policy(policy, &new_policy);
455
456         policy->user_policy.policy = policy->policy;
457         policy->user_policy.governor = policy->governor;
458
459         if (ret)
460                 return ret;
461         else
462                 return count;
463 }
464
465 /**
466  * show_scaling_driver - show the cpufreq driver currently loaded
467  */
468 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
469 {
470         return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
471 }
472
473 /**
474  * show_scaling_available_governors - show the available CPUfreq governors
475  */
476 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
477                                                 char *buf)
478 {
479         ssize_t i = 0;
480         struct cpufreq_governor *t;
481
482         if (!cpufreq_driver->target) {
483                 i += sprintf(buf, "performance powersave");
484                 goto out;
485         }
486
487         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
488                 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
489                     - (CPUFREQ_NAME_LEN + 2)))
490                         goto out;
491                 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
492         }
493 out:
494         i += sprintf(&buf[i], "\n");
495         return i;
496 }
497
498 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
499 {
500         ssize_t i = 0;
501         unsigned int cpu;
502
503         for_each_cpu(cpu, mask) {
504                 if (i)
505                         i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
506                 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
507                 if (i >= (PAGE_SIZE - 5))
508                         break;
509         }
510         i += sprintf(&buf[i], "\n");
511         return i;
512 }
513
514 /**
515  * show_related_cpus - show the CPUs affected by each transition even if
516  * hw coordination is in use
517  */
518 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
519 {
520         if (cpumask_empty(policy->related_cpus))
521                 return show_cpus(policy->cpus, buf);
522         return show_cpus(policy->related_cpus, buf);
523 }
524
525 /**
526  * show_affected_cpus - show the CPUs affected by each transition
527  */
528 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
529 {
530         return show_cpus(policy->cpus, buf);
531 }
532
533 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
534                                         const char *buf, size_t count)
535 {
536         unsigned int freq = 0;
537         unsigned int ret;
538
539         if (!policy->governor || !policy->governor->store_setspeed)
540                 return -EINVAL;
541
542         ret = sscanf(buf, "%u", &freq);
543         if (ret != 1)
544                 return -EINVAL;
545
546         policy->governor->store_setspeed(policy, freq);
547
548         return count;
549 }
550
551 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
552 {
553         if (!policy->governor || !policy->governor->show_setspeed)
554                 return sprintf(buf, "<unsupported>\n");
555
556         return policy->governor->show_setspeed(policy, buf);
557 }
558
559 /**
560  * show_scaling_driver - show the current cpufreq HW/BIOS limitation
561  */
562 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
563 {
564         unsigned int limit;
565         int ret;
566         if (cpufreq_driver->bios_limit) {
567                 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
568                 if (!ret)
569                         return sprintf(buf, "%u\n", limit);
570         }
571         return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
572 }
573
574 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
575 cpufreq_freq_attr_ro(cpuinfo_min_freq);
576 cpufreq_freq_attr_ro(cpuinfo_max_freq);
577 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
578 cpufreq_freq_attr_ro(scaling_available_governors);
579 cpufreq_freq_attr_ro(scaling_driver);
580 cpufreq_freq_attr_ro(scaling_cur_freq);
581 cpufreq_freq_attr_ro(bios_limit);
582 cpufreq_freq_attr_ro(related_cpus);
583 cpufreq_freq_attr_ro(affected_cpus);
584 cpufreq_freq_attr_rw(scaling_min_freq);
585 cpufreq_freq_attr_rw(scaling_max_freq);
586 cpufreq_freq_attr_rw(scaling_governor);
587 cpufreq_freq_attr_rw(scaling_setspeed);
588 cpufreq_freq_attr_ro(policy_min_freq);
589 cpufreq_freq_attr_ro(policy_max_freq);
590
591 static struct attribute *default_attrs[] = {
592         &cpuinfo_min_freq.attr,
593         &cpuinfo_max_freq.attr,
594         &cpuinfo_transition_latency.attr,
595         &scaling_min_freq.attr,
596         &scaling_max_freq.attr,
597         &affected_cpus.attr,
598         &related_cpus.attr,
599         &scaling_governor.attr,
600         &scaling_driver.attr,
601         &scaling_available_governors.attr,
602         &scaling_setspeed.attr,
603         &policy_min_freq.attr,
604         &policy_max_freq.attr,
605         NULL
606 };
607
608 struct kobject *cpufreq_global_kobject;
609 EXPORT_SYMBOL(cpufreq_global_kobject);
610
611 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
612 #define to_attr(a) container_of(a, struct freq_attr, attr)
613
614 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
615 {
616         struct cpufreq_policy *policy = to_policy(kobj);
617         struct freq_attr *fattr = to_attr(attr);
618         ssize_t ret = -EINVAL;
619         policy = cpufreq_cpu_get(policy->cpu);
620         if (!policy)
621                 goto no_policy;
622
623         if (lock_policy_rwsem_read(policy->cpu) < 0)
624                 goto fail;
625
626         if (fattr->show)
627                 ret = fattr->show(policy, buf);
628         else
629                 ret = -EIO;
630
631         unlock_policy_rwsem_read(policy->cpu);
632 fail:
633         cpufreq_cpu_put(policy);
634 no_policy:
635         return ret;
636 }
637
638 static ssize_t store(struct kobject *kobj, struct attribute *attr,
639                      const char *buf, size_t count)
640 {
641         struct cpufreq_policy *policy = to_policy(kobj);
642         struct freq_attr *fattr = to_attr(attr);
643         ssize_t ret = -EINVAL;
644         policy = cpufreq_cpu_get(policy->cpu);
645         if (!policy)
646                 goto no_policy;
647
648         if (lock_policy_rwsem_write(policy->cpu) < 0)
649                 goto fail;
650
651         if (fattr->store)
652                 ret = fattr->store(policy, buf, count);
653         else
654                 ret = -EIO;
655
656         unlock_policy_rwsem_write(policy->cpu);
657 fail:
658         cpufreq_cpu_put(policy);
659 no_policy:
660         return ret;
661 }
662
663 static void cpufreq_sysfs_release(struct kobject *kobj)
664 {
665         struct cpufreq_policy *policy = to_policy(kobj);
666         pr_debug("last reference is dropped\n");
667         complete(&policy->kobj_unregister);
668 }
669
670 static const struct sysfs_ops sysfs_ops = {
671         .show   = show,
672         .store  = store,
673 };
674
675 static struct kobj_type ktype_cpufreq = {
676         .sysfs_ops      = &sysfs_ops,
677         .default_attrs  = default_attrs,
678         .release        = cpufreq_sysfs_release,
679 };
680
681 /*
682  * Returns:
683  *   Negative: Failure
684  *   0:        Success
685  *   Positive: When we have a managed CPU and the sysfs got symlinked
686  */
687 static int cpufreq_add_dev_policy(unsigned int cpu,
688                                   struct cpufreq_policy *policy,
689                                   struct sys_device *sys_dev)
690 {
691         int ret = 0;
692 #ifdef CONFIG_SMP
693         unsigned long flags;
694         unsigned int j;
695 #ifdef CONFIG_HOTPLUG_CPU
696         struct cpufreq_governor *gov;
697
698         gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
699         if (gov) {
700                 policy->governor = gov;
701                 pr_debug("Restoring governor %s for cpu %d\n",
702                        policy->governor->name, cpu);
703         }
704 #endif
705
706         for_each_cpu(j, policy->cpus) {
707                 struct cpufreq_policy *managed_policy;
708
709                 if (cpu == j)
710                         continue;
711
712                 /* Check for existing affected CPUs.
713                  * They may not be aware of it due to CPU Hotplug.
714                  * cpufreq_cpu_put is called when the device is removed
715                  * in __cpufreq_remove_dev()
716                  */
717                 managed_policy = cpufreq_cpu_get(j);
718                 if (unlikely(managed_policy)) {
719
720                         /* Set proper policy_cpu */
721                         unlock_policy_rwsem_write(cpu);
722                         per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
723
724                         if (lock_policy_rwsem_write(cpu) < 0) {
725                                 /* Should not go through policy unlock path */
726                                 if (cpufreq_driver->exit)
727                                         cpufreq_driver->exit(policy);
728                                 cpufreq_cpu_put(managed_policy);
729                                 return -EBUSY;
730                         }
731
732                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
733                         cpumask_copy(managed_policy->cpus, policy->cpus);
734                         per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
735                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
736
737                         pr_debug("CPU already managed, adding link\n");
738                         ret = sysfs_create_link(&sys_dev->kobj,
739                                                 &managed_policy->kobj,
740                                                 "cpufreq");
741                         if (ret)
742                                 cpufreq_cpu_put(managed_policy);
743                         /*
744                          * Success. We only needed to be added to the mask.
745                          * Call driver->exit() because only the cpu parent of
746                          * the kobj needed to call init().
747                          */
748                         if (cpufreq_driver->exit)
749                                 cpufreq_driver->exit(policy);
750
751                         if (!ret)
752                                 return 1;
753                         else
754                                 return ret;
755                 }
756         }
757 #endif
758         return ret;
759 }
760
761
762 /* symlink affected CPUs */
763 static int cpufreq_add_dev_symlink(unsigned int cpu,
764                                    struct cpufreq_policy *policy)
765 {
766         unsigned int j;
767         int ret = 0;
768
769         for_each_cpu(j, policy->cpus) {
770                 struct cpufreq_policy *managed_policy;
771                 struct sys_device *cpu_sys_dev;
772
773                 if (j == cpu)
774                         continue;
775                 if (!cpu_online(j))
776                         continue;
777
778                 pr_debug("CPU %u already managed, adding link\n", j);
779                 managed_policy = cpufreq_cpu_get(cpu);
780                 cpu_sys_dev = get_cpu_sysdev(j);
781                 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
782                                         "cpufreq");
783                 if (ret) {
784                         cpufreq_cpu_put(managed_policy);
785                         return ret;
786                 }
787         }
788         return ret;
789 }
790
791 static int cpufreq_add_dev_interface(unsigned int cpu,
792                                      struct cpufreq_policy *policy,
793                                      struct sys_device *sys_dev)
794 {
795         struct cpufreq_policy new_policy;
796         struct freq_attr **drv_attr;
797         unsigned long flags;
798         int ret = 0;
799         unsigned int j;
800
801         /* prepare interface data */
802         ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
803                                    &sys_dev->kobj, "cpufreq");
804         if (ret)
805                 return ret;
806
807         /* set up files for this cpu device */
808         drv_attr = cpufreq_driver->attr;
809         while ((drv_attr) && (*drv_attr)) {
810                 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
811                 if (ret)
812                         goto err_out_kobj_put;
813                 drv_attr++;
814         }
815         if (cpufreq_driver->get) {
816                 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
817                 if (ret)
818                         goto err_out_kobj_put;
819         }
820         if (cpufreq_driver->target) {
821                 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
822                 if (ret)
823                         goto err_out_kobj_put;
824         }
825         if (cpufreq_driver->bios_limit) {
826                 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
827                 if (ret)
828                         goto err_out_kobj_put;
829         }
830
831         spin_lock_irqsave(&cpufreq_driver_lock, flags);
832         for_each_cpu(j, policy->cpus) {
833                 if (!cpu_online(j))
834                         continue;
835                 per_cpu(cpufreq_cpu_data, j) = policy;
836                 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
837         }
838         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
839
840         ret = cpufreq_add_dev_symlink(cpu, policy);
841         if (ret)
842                 goto err_out_kobj_put;
843
844         memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
845         /* assure that the starting sequence is run in __cpufreq_set_policy */
846         policy->governor = NULL;
847
848         /* set default policy */
849         ret = __cpufreq_set_policy(policy, &new_policy);
850         policy->user_policy.policy = policy->policy;
851         policy->user_policy.governor = policy->governor;
852
853         if (ret) {
854                 pr_debug("setting policy failed\n");
855                 if (cpufreq_driver->exit)
856                         cpufreq_driver->exit(policy);
857         }
858         return ret;
859
860 err_out_kobj_put:
861         kobject_put(&policy->kobj);
862         wait_for_completion(&policy->kobj_unregister);
863         return ret;
864 }
865
866
867 /**
868  * cpufreq_add_dev - add a CPU device
869  *
870  * Adds the cpufreq interface for a CPU device.
871  *
872  * The Oracle says: try running cpufreq registration/unregistration concurrently
873  * with with cpu hotplugging and all hell will break loose. Tried to clean this
874  * mess up, but more thorough testing is needed. - Mathieu
875  */
876 static int cpufreq_add_dev(struct sys_device *sys_dev)
877 {
878         unsigned int cpu = sys_dev->id;
879         int ret = 0, found = 0;
880         struct cpufreq_policy *policy;
881         unsigned long flags;
882         unsigned int j;
883 #ifdef CONFIG_HOTPLUG_CPU
884         int sibling;
885 #endif
886
887         if (cpu_is_offline(cpu))
888                 return 0;
889
890         pr_debug("adding CPU %u\n", cpu);
891
892 #ifdef CONFIG_SMP
893         /* check whether a different CPU already registered this
894          * CPU because it is in the same boat. */
895         policy = cpufreq_cpu_get(cpu);
896         if (unlikely(policy)) {
897                 cpufreq_cpu_put(policy);
898                 return 0;
899         }
900 #endif
901
902         if (!try_module_get(cpufreq_driver->owner)) {
903                 ret = -EINVAL;
904                 goto module_out;
905         }
906
907         ret = -ENOMEM;
908         policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
909         if (!policy)
910                 goto nomem_out;
911
912         if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
913                 goto err_free_policy;
914
915         if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
916                 goto err_free_cpumask;
917
918         policy->cpu = cpu;
919         cpumask_copy(policy->cpus, cpumask_of(cpu));
920
921         /* Initially set CPU itself as the policy_cpu */
922         per_cpu(cpufreq_policy_cpu, cpu) = cpu;
923         ret = (lock_policy_rwsem_write(cpu) < 0);
924         WARN_ON(ret);
925
926         init_completion(&policy->kobj_unregister);
927         INIT_WORK(&policy->update, handle_update);
928
929         /* Set governor before ->init, so that driver could check it */
930 #ifdef CONFIG_HOTPLUG_CPU
931         for_each_online_cpu(sibling) {
932                 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
933                 if (cp && cp->governor &&
934                     (cpumask_test_cpu(cpu, cp->related_cpus))) {
935                         policy->governor = cp->governor;
936                         found = 1;
937                         break;
938                 }
939         }
940 #endif
941         if (!found)
942                 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
943         /* call driver. From then on the cpufreq must be able
944          * to accept all calls to ->verify and ->setpolicy for this CPU
945          */
946         ret = cpufreq_driver->init(policy);
947         if (ret) {
948                 pr_debug("initialization failed\n");
949                 goto err_unlock_policy;
950         }
951         policy->user_policy.min = policy->min;
952         policy->user_policy.max = policy->max;
953
954         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
955                                      CPUFREQ_START, policy);
956
957         ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
958         if (ret) {
959                 if (ret > 0)
960                         /* This is a managed cpu, symlink created,
961                            exit with 0 */
962                         ret = 0;
963                 goto err_unlock_policy;
964         }
965
966         ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
967         if (ret)
968                 goto err_out_unregister;
969
970         unlock_policy_rwsem_write(cpu);
971
972         kobject_uevent(&policy->kobj, KOBJ_ADD);
973         module_put(cpufreq_driver->owner);
974         pr_debug("initialization complete\n");
975
976         return 0;
977
978
979 err_out_unregister:
980         spin_lock_irqsave(&cpufreq_driver_lock, flags);
981         for_each_cpu(j, policy->cpus)
982                 per_cpu(cpufreq_cpu_data, j) = NULL;
983         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
984
985         kobject_put(&policy->kobj);
986         wait_for_completion(&policy->kobj_unregister);
987
988 err_unlock_policy:
989         unlock_policy_rwsem_write(cpu);
990         free_cpumask_var(policy->related_cpus);
991 err_free_cpumask:
992         free_cpumask_var(policy->cpus);
993 err_free_policy:
994         kfree(policy);
995 nomem_out:
996         module_put(cpufreq_driver->owner);
997 module_out:
998         return ret;
999 }
1000
1001
1002 /**
1003  * __cpufreq_remove_dev - remove a CPU device
1004  *
1005  * Removes the cpufreq interface for a CPU device.
1006  * Caller should already have policy_rwsem in write mode for this CPU.
1007  * This routine frees the rwsem before returning.
1008  */
1009 static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1010 {
1011         unsigned int cpu = sys_dev->id;
1012         unsigned long flags;
1013         struct cpufreq_policy *data;
1014         struct kobject *kobj;
1015         struct completion *cmp;
1016 #ifdef CONFIG_SMP
1017         struct sys_device *cpu_sys_dev;
1018         unsigned int j;
1019 #endif
1020
1021         pr_debug("unregistering CPU %u\n", cpu);
1022
1023         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1024         data = per_cpu(cpufreq_cpu_data, cpu);
1025
1026         if (!data) {
1027                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1028                 unlock_policy_rwsem_write(cpu);
1029                 return -EINVAL;
1030         }
1031         per_cpu(cpufreq_cpu_data, cpu) = NULL;
1032
1033
1034 #ifdef CONFIG_SMP
1035         /* if this isn't the CPU which is the parent of the kobj, we
1036          * only need to unlink, put and exit
1037          */
1038         if (unlikely(cpu != data->cpu)) {
1039                 pr_debug("removing link\n");
1040                 cpumask_clear_cpu(cpu, data->cpus);
1041                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1042                 kobj = &sys_dev->kobj;
1043                 cpufreq_cpu_put(data);
1044                 unlock_policy_rwsem_write(cpu);
1045                 sysfs_remove_link(kobj, "cpufreq");
1046                 return 0;
1047         }
1048 #endif
1049
1050 #ifdef CONFIG_SMP
1051
1052 #ifdef CONFIG_HOTPLUG_CPU
1053         strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1054                         CPUFREQ_NAME_LEN);
1055 #endif
1056
1057         /* if we have other CPUs still registered, we need to unlink them,
1058          * or else wait_for_completion below will lock up. Clean the
1059          * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1060          * the sysfs links afterwards.
1061          */
1062         if (unlikely(cpumask_weight(data->cpus) > 1)) {
1063                 for_each_cpu(j, data->cpus) {
1064                         if (j == cpu)
1065                                 continue;
1066                         per_cpu(cpufreq_cpu_data, j) = NULL;
1067                 }
1068         }
1069
1070         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1071
1072         if (unlikely(cpumask_weight(data->cpus) > 1)) {
1073                 for_each_cpu(j, data->cpus) {
1074                         if (j == cpu)
1075                                 continue;
1076                         pr_debug("removing link for cpu %u\n", j);
1077 #ifdef CONFIG_HOTPLUG_CPU
1078                         strncpy(per_cpu(cpufreq_cpu_governor, j),
1079                                 data->governor->name, CPUFREQ_NAME_LEN);
1080 #endif
1081                         cpu_sys_dev = get_cpu_sysdev(j);
1082                         kobj = &cpu_sys_dev->kobj;
1083                         unlock_policy_rwsem_write(cpu);
1084                         sysfs_remove_link(kobj, "cpufreq");
1085                         lock_policy_rwsem_write(cpu);
1086                         cpufreq_cpu_put(data);
1087                 }
1088         }
1089 #else
1090         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1091 #endif
1092
1093         if (cpufreq_driver->target)
1094                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1095
1096         kobj = &data->kobj;
1097         cmp = &data->kobj_unregister;
1098         unlock_policy_rwsem_write(cpu);
1099         kobject_put(kobj);
1100
1101         /* we need to make sure that the underlying kobj is actually
1102          * not referenced anymore by anybody before we proceed with
1103          * unloading.
1104          */
1105         pr_debug("waiting for dropping of refcount\n");
1106         wait_for_completion(cmp);
1107         pr_debug("wait complete\n");
1108
1109         lock_policy_rwsem_write(cpu);
1110         if (cpufreq_driver->exit)
1111                 cpufreq_driver->exit(data);
1112         unlock_policy_rwsem_write(cpu);
1113
1114 #ifdef CONFIG_HOTPLUG_CPU
1115         /* when the CPU which is the parent of the kobj is hotplugged
1116          * offline, check for siblings, and create cpufreq sysfs interface
1117          * and symlinks
1118          */
1119         if (unlikely(cpumask_weight(data->cpus) > 1)) {
1120                 /* first sibling now owns the new sysfs dir */
1121                 cpumask_clear_cpu(cpu, data->cpus);
1122                 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1123
1124                 /* finally remove our own symlink */
1125                 lock_policy_rwsem_write(cpu);
1126                 __cpufreq_remove_dev(sys_dev);
1127         }
1128 #endif
1129
1130         free_cpumask_var(data->related_cpus);
1131         free_cpumask_var(data->cpus);
1132         kfree(data);
1133
1134         return 0;
1135 }
1136
1137
1138 static int cpufreq_remove_dev(struct sys_device *sys_dev)
1139 {
1140         unsigned int cpu = sys_dev->id;
1141         int retval;
1142
1143         if (cpu_is_offline(cpu))
1144                 return 0;
1145
1146         if (unlikely(lock_policy_rwsem_write(cpu)))
1147                 BUG();
1148
1149         retval = __cpufreq_remove_dev(sys_dev);
1150         return retval;
1151 }
1152
1153
1154 static void handle_update(struct work_struct *work)
1155 {
1156         struct cpufreq_policy *policy =
1157                 container_of(work, struct cpufreq_policy, update);
1158         unsigned int cpu = policy->cpu;
1159         pr_debug("handle_update for cpu %u called\n", cpu);
1160         cpufreq_update_policy(cpu);
1161 }
1162
1163 /**
1164  *      cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1165  *      @cpu: cpu number
1166  *      @old_freq: CPU frequency the kernel thinks the CPU runs at
1167  *      @new_freq: CPU frequency the CPU actually runs at
1168  *
1169  *      We adjust to current frequency first, and need to clean up later.
1170  *      So either call to cpufreq_update_policy() or schedule handle_update()).
1171  */
1172 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1173                                 unsigned int new_freq)
1174 {
1175         struct cpufreq_freqs freqs;
1176
1177         pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1178                "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1179
1180         freqs.cpu = cpu;
1181         freqs.old = old_freq;
1182         freqs.new = new_freq;
1183         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1184         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1185 }
1186
1187
1188 /**
1189  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1190  * @cpu: CPU number
1191  *
1192  * This is the last known freq, without actually getting it from the driver.
1193  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1194  */
1195 unsigned int cpufreq_quick_get(unsigned int cpu)
1196 {
1197         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1198         unsigned int ret_freq = 0;
1199
1200         if (policy) {
1201                 ret_freq = policy->cur;
1202                 cpufreq_cpu_put(policy);
1203         }
1204
1205         return ret_freq;
1206 }
1207 EXPORT_SYMBOL(cpufreq_quick_get);
1208
1209 /**
1210  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1211  * @cpu: CPU number
1212  *
1213  * Just return the max possible frequency for a given CPU.
1214  */
1215 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1216 {
1217         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1218         unsigned int ret_freq = 0;
1219
1220         if (policy) {
1221                 ret_freq = policy->max;
1222                 cpufreq_cpu_put(policy);
1223         }
1224
1225         return ret_freq;
1226 }
1227 EXPORT_SYMBOL(cpufreq_quick_get_max);
1228
1229
1230 static unsigned int __cpufreq_get(unsigned int cpu)
1231 {
1232         struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1233         unsigned int ret_freq = 0;
1234
1235         if (!cpufreq_driver->get)
1236                 return ret_freq;
1237
1238         ret_freq = cpufreq_driver->get(cpu);
1239
1240         if (ret_freq && policy->cur &&
1241                 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1242                 /* verify no discrepancy between actual and
1243                                         saved value exists */
1244                 if (unlikely(ret_freq != policy->cur)) {
1245                         cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1246                         schedule_work(&policy->update);
1247                 }
1248         }
1249
1250         return ret_freq;
1251 }
1252
1253 /**
1254  * cpufreq_get - get the current CPU frequency (in kHz)
1255  * @cpu: CPU number
1256  *
1257  * Get the CPU current (static) CPU frequency
1258  */
1259 unsigned int cpufreq_get(unsigned int cpu)
1260 {
1261         unsigned int ret_freq = 0;
1262         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1263
1264         if (!policy)
1265                 goto out;
1266
1267         if (unlikely(lock_policy_rwsem_read(cpu)))
1268                 goto out_policy;
1269
1270         ret_freq = __cpufreq_get(cpu);
1271
1272         unlock_policy_rwsem_read(cpu);
1273
1274 out_policy:
1275         cpufreq_cpu_put(policy);
1276 out:
1277         return ret_freq;
1278 }
1279 EXPORT_SYMBOL(cpufreq_get);
1280
1281 static struct sysdev_driver cpufreq_sysdev_driver = {
1282         .add            = cpufreq_add_dev,
1283         .remove         = cpufreq_remove_dev,
1284 };
1285
1286
1287 /**
1288  * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1289  *
1290  * This function is only executed for the boot processor.  The other CPUs
1291  * have been put offline by means of CPU hotplug.
1292  */
1293 static int cpufreq_bp_suspend(void)
1294 {
1295         int ret = 0;
1296
1297         int cpu = smp_processor_id();
1298         struct cpufreq_policy *cpu_policy;
1299
1300         pr_debug("suspending cpu %u\n", cpu);
1301
1302         /* If there's no policy for the boot CPU, we have nothing to do. */
1303         cpu_policy = cpufreq_cpu_get(cpu);
1304         if (!cpu_policy)
1305                 return 0;
1306
1307         if (cpufreq_driver->suspend) {
1308                 ret = cpufreq_driver->suspend(cpu_policy);
1309                 if (ret)
1310                         printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1311                                         "step on CPU %u\n", cpu_policy->cpu);
1312         }
1313
1314         cpufreq_cpu_put(cpu_policy);
1315         return ret;
1316 }
1317
1318 /**
1319  * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1320  *
1321  *      1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1322  *      2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1323  *          restored. It will verify that the current freq is in sync with
1324  *          what we believe it to be. This is a bit later than when it
1325  *          should be, but nonethteless it's better than calling
1326  *          cpufreq_driver->get() here which might re-enable interrupts...
1327  *
1328  * This function is only executed for the boot CPU.  The other CPUs have not
1329  * been turned on yet.
1330  */
1331 static void cpufreq_bp_resume(void)
1332 {
1333         int ret = 0;
1334
1335         int cpu = smp_processor_id();
1336         struct cpufreq_policy *cpu_policy;
1337
1338         pr_debug("resuming cpu %u\n", cpu);
1339
1340         /* If there's no policy for the boot CPU, we have nothing to do. */
1341         cpu_policy = cpufreq_cpu_get(cpu);
1342         if (!cpu_policy)
1343                 return;
1344
1345         if (cpufreq_driver->resume) {
1346                 ret = cpufreq_driver->resume(cpu_policy);
1347                 if (ret) {
1348                         printk(KERN_ERR "cpufreq: resume failed in ->resume "
1349                                         "step on CPU %u\n", cpu_policy->cpu);
1350                         goto fail;
1351                 }
1352         }
1353
1354         schedule_work(&cpu_policy->update);
1355
1356 fail:
1357         cpufreq_cpu_put(cpu_policy);
1358 }
1359
1360 static struct syscore_ops cpufreq_syscore_ops = {
1361         .suspend        = cpufreq_bp_suspend,
1362         .resume         = cpufreq_bp_resume,
1363 };
1364
1365
1366 /*********************************************************************
1367  *                     NOTIFIER LISTS INTERFACE                      *
1368  *********************************************************************/
1369
1370 /**
1371  *      cpufreq_register_notifier - register a driver with cpufreq
1372  *      @nb: notifier function to register
1373  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1374  *
1375  *      Add a driver to one of two lists: either a list of drivers that
1376  *      are notified about clock rate changes (once before and once after
1377  *      the transition), or a list of drivers that are notified about
1378  *      changes in cpufreq policy.
1379  *
1380  *      This function may sleep, and has the same return conditions as
1381  *      blocking_notifier_chain_register.
1382  */
1383 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1384 {
1385         int ret;
1386
1387         WARN_ON(!init_cpufreq_transition_notifier_list_called);
1388
1389         switch (list) {
1390         case CPUFREQ_TRANSITION_NOTIFIER:
1391                 ret = srcu_notifier_chain_register(
1392                                 &cpufreq_transition_notifier_list, nb);
1393                 break;
1394         case CPUFREQ_POLICY_NOTIFIER:
1395                 ret = blocking_notifier_chain_register(
1396                                 &cpufreq_policy_notifier_list, nb);
1397                 break;
1398         default:
1399                 ret = -EINVAL;
1400         }
1401
1402         return ret;
1403 }
1404 EXPORT_SYMBOL(cpufreq_register_notifier);
1405
1406
1407 /**
1408  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
1409  *      @nb: notifier block to be unregistered
1410  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1411  *
1412  *      Remove a driver from the CPU frequency notifier list.
1413  *
1414  *      This function may sleep, and has the same return conditions as
1415  *      blocking_notifier_chain_unregister.
1416  */
1417 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1418 {
1419         int ret;
1420
1421         switch (list) {
1422         case CPUFREQ_TRANSITION_NOTIFIER:
1423                 ret = srcu_notifier_chain_unregister(
1424                                 &cpufreq_transition_notifier_list, nb);
1425                 break;
1426         case CPUFREQ_POLICY_NOTIFIER:
1427                 ret = blocking_notifier_chain_unregister(
1428                                 &cpufreq_policy_notifier_list, nb);
1429                 break;
1430         default:
1431                 ret = -EINVAL;
1432         }
1433
1434         return ret;
1435 }
1436 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1437
1438
1439 /*********************************************************************
1440  *                              GOVERNORS                            *
1441  *********************************************************************/
1442
1443
1444 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1445                             unsigned int target_freq,
1446                             unsigned int relation)
1447 {
1448         int retval = -EINVAL;
1449
1450         pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1451                 target_freq, relation);
1452         trace_cpu_scale(policy->cpu, policy->cur, POWER_CPU_SCALE_START);
1453         if (cpu_online(policy->cpu) && cpufreq_driver->target)
1454                 retval = cpufreq_driver->target(policy, target_freq, relation);
1455         trace_cpu_scale(policy->cpu, target_freq, POWER_CPU_SCALE_DONE);
1456
1457         return retval;
1458 }
1459 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1460
1461 int cpufreq_driver_target(struct cpufreq_policy *policy,
1462                           unsigned int target_freq,
1463                           unsigned int relation)
1464 {
1465         int ret = -EINVAL;
1466
1467         policy = cpufreq_cpu_get(policy->cpu);
1468         if (!policy)
1469                 goto no_policy;
1470
1471         if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1472                 goto fail;
1473
1474         ret = __cpufreq_driver_target(policy, target_freq, relation);
1475
1476         unlock_policy_rwsem_write(policy->cpu);
1477
1478 fail:
1479         cpufreq_cpu_put(policy);
1480 no_policy:
1481         return ret;
1482 }
1483 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1484
1485 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1486 {
1487         int ret = 0;
1488
1489         policy = cpufreq_cpu_get(policy->cpu);
1490         if (!policy)
1491                 return -EINVAL;
1492
1493         if (cpu_online(cpu) && cpufreq_driver->getavg)
1494                 ret = cpufreq_driver->getavg(policy, cpu);
1495
1496         cpufreq_cpu_put(policy);
1497         return ret;
1498 }
1499 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1500
1501 /*
1502  * when "event" is CPUFREQ_GOV_LIMITS
1503  */
1504
1505 static int __cpufreq_governor(struct cpufreq_policy *policy,
1506                                         unsigned int event)
1507 {
1508         int ret;
1509
1510         /* Only must be defined when default governor is known to have latency
1511            restrictions, like e.g. conservative or ondemand.
1512            That this is the case is already ensured in Kconfig
1513         */
1514 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1515         struct cpufreq_governor *gov = &cpufreq_gov_performance;
1516 #else
1517         struct cpufreq_governor *gov = NULL;
1518 #endif
1519
1520         if (policy->governor->max_transition_latency &&
1521             policy->cpuinfo.transition_latency >
1522             policy->governor->max_transition_latency) {
1523                 if (!gov)
1524                         return -EINVAL;
1525                 else {
1526                         printk(KERN_WARNING "%s governor failed, too long"
1527                                " transition latency of HW, fallback"
1528                                " to %s governor\n",
1529                                policy->governor->name,
1530                                gov->name);
1531                         policy->governor = gov;
1532                 }
1533         }
1534
1535         if (!try_module_get(policy->governor->owner))
1536                 return -EINVAL;
1537
1538         pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1539                                                 policy->cpu, event);
1540         ret = policy->governor->governor(policy, event);
1541
1542         /* we keep one module reference alive for
1543                         each CPU governed by this CPU */
1544         if ((event != CPUFREQ_GOV_START) || ret)
1545                 module_put(policy->governor->owner);
1546         if ((event == CPUFREQ_GOV_STOP) && !ret)
1547                 module_put(policy->governor->owner);
1548
1549         return ret;
1550 }
1551
1552
1553 int cpufreq_register_governor(struct cpufreq_governor *governor)
1554 {
1555         int err;
1556
1557         if (!governor)
1558                 return -EINVAL;
1559
1560         mutex_lock(&cpufreq_governor_mutex);
1561
1562         err = -EBUSY;
1563         if (__find_governor(governor->name) == NULL) {
1564                 err = 0;
1565                 list_add(&governor->governor_list, &cpufreq_governor_list);
1566         }
1567
1568         mutex_unlock(&cpufreq_governor_mutex);
1569         return err;
1570 }
1571 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1572
1573
1574 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1575 {
1576 #ifdef CONFIG_HOTPLUG_CPU
1577         int cpu;
1578 #endif
1579
1580         if (!governor)
1581                 return;
1582
1583 #ifdef CONFIG_HOTPLUG_CPU
1584         for_each_present_cpu(cpu) {
1585                 if (cpu_online(cpu))
1586                         continue;
1587                 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1588                         strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1589         }
1590 #endif
1591
1592         mutex_lock(&cpufreq_governor_mutex);
1593         list_del(&governor->governor_list);
1594         mutex_unlock(&cpufreq_governor_mutex);
1595         return;
1596 }
1597 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1598
1599
1600
1601 /*********************************************************************
1602  *                          POLICY INTERFACE                         *
1603  *********************************************************************/
1604
1605 /**
1606  * cpufreq_get_policy - get the current cpufreq_policy
1607  * @policy: struct cpufreq_policy into which the current cpufreq_policy
1608  *      is written
1609  *
1610  * Reads the current cpufreq policy.
1611  */
1612 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1613 {
1614         struct cpufreq_policy *cpu_policy;
1615         if (!policy)
1616                 return -EINVAL;
1617
1618         cpu_policy = cpufreq_cpu_get(cpu);
1619         if (!cpu_policy)
1620                 return -EINVAL;
1621
1622         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1623
1624         cpufreq_cpu_put(cpu_policy);
1625         return 0;
1626 }
1627 EXPORT_SYMBOL(cpufreq_get_policy);
1628
1629
1630 /*
1631  * data   : current policy.
1632  * policy : policy to be set.
1633  */
1634 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1635                                 struct cpufreq_policy *policy)
1636 {
1637         int ret = 0;
1638         unsigned int qmin, qmax;
1639         unsigned int pmin = policy->min;
1640         unsigned int pmax = policy->max;
1641
1642         qmin = min((unsigned int)pm_qos_request(PM_QOS_CPU_FREQ_MIN),
1643                    data->user_policy.max);
1644         qmax = max((unsigned int)pm_qos_request(PM_QOS_CPU_FREQ_MAX),
1645                    data->user_policy.min);
1646
1647         pr_debug("setting new policy for CPU %u: %u - %u (%u - %u) kHz\n",
1648                 policy->cpu, pmin, pmax, qmin, qmax);
1649
1650         /* clamp the new policy to PM QoS limits */
1651         policy->min = max(pmin, qmin);
1652         policy->max = min(pmax, qmax);
1653
1654         memcpy(&policy->cpuinfo, &data->cpuinfo,
1655                                 sizeof(struct cpufreq_cpuinfo));
1656
1657         if (policy->min > data->user_policy.max ||
1658             policy->max < data->user_policy.min) {
1659                 ret = -EINVAL;
1660                 goto error_out;
1661         }
1662
1663         /* verify the cpu speed can be set within this limit */
1664         ret = cpufreq_driver->verify(policy);
1665         if (ret)
1666                 goto error_out;
1667
1668         /* adjust if necessary - all reasons */
1669         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1670                         CPUFREQ_ADJUST, policy);
1671
1672         /* adjust if necessary - hardware incompatibility*/
1673         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1674                         CPUFREQ_INCOMPATIBLE, policy);
1675
1676         /* verify the cpu speed can be set within this limit,
1677            which might be different to the first one */
1678         ret = cpufreq_driver->verify(policy);
1679         if (ret)
1680                 goto error_out;
1681
1682         /* notification of the new policy */
1683         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1684                         CPUFREQ_NOTIFY, policy);
1685
1686         data->min = policy->min;
1687         data->max = policy->max;
1688
1689         pr_debug("new min and max freqs are %u - %u kHz\n",
1690                                         data->min, data->max);
1691
1692         if (cpufreq_driver->setpolicy) {
1693                 data->policy = policy->policy;
1694                 pr_debug("setting range\n");
1695                 ret = cpufreq_driver->setpolicy(policy);
1696         } else {
1697                 if (policy->governor != data->governor) {
1698                         /* save old, working values */
1699                         struct cpufreq_governor *old_gov = data->governor;
1700
1701                         pr_debug("governor switch\n");
1702
1703                         /* end old governor */
1704                         if (data->governor)
1705                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1706
1707                         /* start new governor */
1708                         data->governor = policy->governor;
1709                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1710                                 /* new governor failed, so re-start old one */
1711                                 pr_debug("starting governor %s failed\n",
1712                                                         data->governor->name);
1713                                 if (old_gov) {
1714                                         data->governor = old_gov;
1715                                         __cpufreq_governor(data,
1716                                                            CPUFREQ_GOV_START);
1717                                 }
1718                                 ret = -EINVAL;
1719                                 goto error_out;
1720                         }
1721                         /* might be a policy change, too, so fall through */
1722                 }
1723                 pr_debug("governor: change or update limits\n");
1724                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1725         }
1726
1727 error_out:
1728         /* restore the limits that the policy requested */
1729         policy->min = pmin;
1730         policy->max = pmax;
1731         return ret;
1732 }
1733
1734 /**
1735  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
1736  *      @cpu: CPU which shall be re-evaluated
1737  *
1738  *      Useful for policy notifiers which have different necessities
1739  *      at different times.
1740  */
1741 int cpufreq_update_policy(unsigned int cpu)
1742 {
1743         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1744         struct cpufreq_policy policy;
1745         int ret;
1746
1747         if (!data) {
1748                 ret = -ENODEV;
1749                 goto no_policy;
1750         }
1751
1752         if (unlikely(lock_policy_rwsem_write(cpu))) {
1753                 ret = -EINVAL;
1754                 goto fail;
1755         }
1756
1757         pr_debug("updating policy for CPU %u\n", cpu);
1758         memcpy(&policy, data, sizeof(struct cpufreq_policy));
1759         policy.min = data->user_policy.min;
1760         policy.max = data->user_policy.max;
1761         policy.policy = data->user_policy.policy;
1762         policy.governor = data->user_policy.governor;
1763
1764         /* BIOS might change freq behind our back
1765           -> ask driver for current freq and notify governors about a change */
1766         if (cpufreq_driver->get) {
1767                 policy.cur = cpufreq_driver->get(cpu);
1768                 if (!data->cur) {
1769                         pr_debug("Driver did not initialize current freq");
1770                         data->cur = policy.cur;
1771                 } else {
1772                         if (data->cur != policy.cur)
1773                                 cpufreq_out_of_sync(cpu, data->cur,
1774                                                                 policy.cur);
1775                 }
1776         }
1777
1778         ret = __cpufreq_set_policy(data, &policy);
1779
1780         unlock_policy_rwsem_write(cpu);
1781
1782 fail:
1783         cpufreq_cpu_put(data);
1784 no_policy:
1785         return ret;
1786 }
1787 EXPORT_SYMBOL(cpufreq_update_policy);
1788
1789 /*
1790  *      cpufreq_set_gov - set governor for a cpu
1791  *      @cpu: CPU whose governor needs to be changed
1792  *      @target_gov: new governor to be set
1793  */
1794 int cpufreq_set_gov(char *target_gov, unsigned int cpu)
1795 {
1796         int ret = 0;
1797         struct cpufreq_policy new_policy;
1798         struct cpufreq_policy *cur_policy;
1799
1800         if (target_gov == NULL)
1801                 return -EINVAL;
1802
1803         /* Get current governer */
1804         cur_policy = cpufreq_cpu_get(cpu);
1805         if (!cur_policy)
1806                 return -EINVAL;
1807
1808         if (lock_policy_rwsem_read(cur_policy->cpu) < 0) {
1809                 ret = -EINVAL;
1810                 goto err_out;
1811         }
1812
1813         if (cur_policy->governor)
1814                 ret = strncmp(cur_policy->governor->name, target_gov,
1815                                         strlen(target_gov));
1816         else {
1817                 unlock_policy_rwsem_read(cur_policy->cpu);
1818                 ret = -EINVAL;
1819                 goto err_out;
1820         }
1821         unlock_policy_rwsem_read(cur_policy->cpu);
1822
1823         if (!ret) {
1824                 pr_debug(" Target governer & current governer is same\n");
1825                 ret = -EINVAL;
1826                 goto err_out;
1827         } else {
1828                 new_policy = *cur_policy;
1829                 if (cpufreq_parse_governor(target_gov, &new_policy.policy,
1830                                 &new_policy.governor)) {
1831                         ret = -EINVAL;
1832                         goto err_out;
1833                 }
1834
1835                 if (lock_policy_rwsem_write(cur_policy->cpu) < 0) {
1836                         ret = -EINVAL;
1837                         goto err_out;
1838                 }
1839
1840                 ret = __cpufreq_set_policy(cur_policy, &new_policy);
1841
1842                 cur_policy->user_policy.policy = cur_policy->policy;
1843                 cur_policy->user_policy.governor = cur_policy->governor;
1844
1845                 unlock_policy_rwsem_write(cur_policy->cpu);
1846         }
1847 err_out:
1848         cpufreq_cpu_put(cur_policy);
1849         return ret;
1850 }
1851 EXPORT_SYMBOL(cpufreq_set_gov);
1852
1853 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1854                                         unsigned long action, void *hcpu)
1855 {
1856         unsigned int cpu = (unsigned long)hcpu;
1857         struct sys_device *sys_dev;
1858
1859         sys_dev = get_cpu_sysdev(cpu);
1860         if (sys_dev) {
1861                 switch (action) {
1862                 case CPU_ONLINE:
1863                 case CPU_ONLINE_FROZEN:
1864                         cpufreq_add_dev(sys_dev);
1865                         break;
1866                 case CPU_DOWN_PREPARE:
1867                 case CPU_DOWN_PREPARE_FROZEN:
1868                         if (unlikely(lock_policy_rwsem_write(cpu)))
1869                                 BUG();
1870
1871                         __cpufreq_remove_dev(sys_dev);
1872                         break;
1873                 case CPU_DOWN_FAILED:
1874                 case CPU_DOWN_FAILED_FROZEN:
1875                         cpufreq_add_dev(sys_dev);
1876                         break;
1877                 }
1878         }
1879         return NOTIFY_OK;
1880 }
1881
1882 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1883     .notifier_call = cpufreq_cpu_callback,
1884 };
1885
1886 /*********************************************************************
1887  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
1888  *********************************************************************/
1889
1890 /**
1891  * cpufreq_register_driver - register a CPU Frequency driver
1892  * @driver_data: A struct cpufreq_driver containing the values#
1893  * submitted by the CPU Frequency driver.
1894  *
1895  *   Registers a CPU Frequency driver to this core code. This code
1896  * returns zero on success, -EBUSY when another driver got here first
1897  * (and isn't unregistered in the meantime).
1898  *
1899  */
1900 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1901 {
1902         unsigned long flags;
1903         int ret;
1904
1905         if (!driver_data || !driver_data->verify || !driver_data->init ||
1906             ((!driver_data->setpolicy) && (!driver_data->target)))
1907                 return -EINVAL;
1908
1909         pr_debug("trying to register driver %s\n", driver_data->name);
1910
1911         if (driver_data->setpolicy)
1912                 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1913
1914         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1915         if (cpufreq_driver) {
1916                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1917                 return -EBUSY;
1918         }
1919         cpufreq_driver = driver_data;
1920         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1921
1922         ret = sysdev_driver_register(&cpu_sysdev_class,
1923                                         &cpufreq_sysdev_driver);
1924         if (ret)
1925                 goto err_null_driver;
1926
1927         if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1928                 int i;
1929                 ret = -ENODEV;
1930
1931                 /* check for at least one working CPU */
1932                 for (i = 0; i < nr_cpu_ids; i++)
1933                         if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1934                                 ret = 0;
1935                                 break;
1936                         }
1937
1938                 /* if all ->init() calls failed, unregister */
1939                 if (ret) {
1940                         pr_debug("no CPU initialized for driver %s\n",
1941                                                         driver_data->name);
1942                         goto err_sysdev_unreg;
1943                 }
1944         }
1945
1946         register_hotcpu_notifier(&cpufreq_cpu_notifier);
1947         pr_debug("driver %s up and running\n", driver_data->name);
1948
1949         return 0;
1950 err_sysdev_unreg:
1951         sysdev_driver_unregister(&cpu_sysdev_class,
1952                         &cpufreq_sysdev_driver);
1953 err_null_driver:
1954         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1955         cpufreq_driver = NULL;
1956         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1957         return ret;
1958 }
1959 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1960
1961
1962 /**
1963  * cpufreq_unregister_driver - unregister the current CPUFreq driver
1964  *
1965  *    Unregister the current CPUFreq driver. Only call this if you have
1966  * the right to do so, i.e. if you have succeeded in initialising before!
1967  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1968  * currently not initialised.
1969  */
1970 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1971 {
1972         unsigned long flags;
1973
1974         if (!cpufreq_driver || (driver != cpufreq_driver))
1975                 return -EINVAL;
1976
1977         pr_debug("unregistering driver %s\n", driver->name);
1978
1979         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1980         unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1981
1982         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1983         cpufreq_driver = NULL;
1984         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1985
1986         return 0;
1987 }
1988 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1989
1990 static int cpu_freq_notify(struct notifier_block *b,
1991                            unsigned long l, void *v);
1992
1993 static struct notifier_block min_freq_notifier = {
1994         .notifier_call = cpu_freq_notify,
1995 };
1996 static struct notifier_block max_freq_notifier = {
1997         .notifier_call = cpu_freq_notify,
1998 };
1999
2000 static int cpu_freq_notify(struct notifier_block *b,
2001                            unsigned long l, void *v)
2002 {
2003         int cpu;
2004         pr_debug("PM QoS %s %lu\n",
2005                 b == &min_freq_notifier ? "min" : "max", l);
2006         for_each_online_cpu(cpu) {
2007                 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2008                 if (policy) {
2009                         cpufreq_update_policy(policy->cpu);
2010                         cpufreq_cpu_put(policy);
2011                 }
2012         }
2013         return NOTIFY_OK;
2014 }
2015
2016 static int __init cpufreq_core_init(void)
2017 {
2018         int cpu;
2019         int rc;
2020
2021         for_each_possible_cpu(cpu) {
2022                 per_cpu(cpufreq_policy_cpu, cpu) = -1;
2023                 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2024         }
2025
2026         cpufreq_global_kobject = kobject_create_and_add("cpufreq",
2027                                                 &cpu_sysdev_class.kset.kobj);
2028         BUG_ON(!cpufreq_global_kobject);
2029         register_syscore_ops(&cpufreq_syscore_ops);
2030         rc = pm_qos_add_notifier(PM_QOS_CPU_FREQ_MIN,
2031                                  &min_freq_notifier);
2032         BUG_ON(rc);
2033         rc = pm_qos_add_notifier(PM_QOS_CPU_FREQ_MAX,
2034                                  &max_freq_notifier);
2035         BUG_ON(rc);
2036
2037         return 0;
2038 }
2039 core_initcall(cpufreq_core_init);