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