cpufreq: Drop redundant check from cpufreq_update_current_freq()
[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/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/tick.h>
32 #include <trace/events/power.h>
33
34 static LIST_HEAD(cpufreq_policy_list);
35
36 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37 {
38 return cpumask_empty(policy->cpus);
39 }
40
41 /* Macros to iterate over CPU policies */
42 #define for_each_suitable_policy(__policy, __active) \
43 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
44 if ((__active) == !policy_is_inactive(__policy))
45
46 #define for_each_active_policy(__policy) \
47 for_each_suitable_policy(__policy, true)
48 #define for_each_inactive_policy(__policy) \
49 for_each_suitable_policy(__policy, false)
50
51 #define for_each_policy(__policy) \
52 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53
54 /* Iterate over governors */
55 static LIST_HEAD(cpufreq_governor_list);
56 #define for_each_governor(__governor) \
57 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58
59 /**
60 * The "cpufreq driver" - the arch- or hardware-dependent low
61 * level driver of CPUFreq support, and its spinlock. This lock
62 * also protects the cpufreq_cpu_data array.
63 */
64 static struct cpufreq_driver *cpufreq_driver;
65 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
66 static DEFINE_RWLOCK(cpufreq_driver_lock);
67
68 /* Flag to suspend/resume CPUFreq governors */
69 static bool cpufreq_suspended;
70
71 static inline bool has_target(void)
72 {
73 return cpufreq_driver->target_index || cpufreq_driver->target;
74 }
75
76 /* internal prototypes */
77 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
78 static int cpufreq_init_governor(struct cpufreq_policy *policy);
79 static void cpufreq_exit_governor(struct cpufreq_policy *policy);
80 static int cpufreq_start_governor(struct cpufreq_policy *policy);
81 static void cpufreq_stop_governor(struct cpufreq_policy *policy);
82 static void cpufreq_governor_limits(struct cpufreq_policy *policy);
83
84 /**
85 * Two notifier lists: the "policy" list is involved in the
86 * validation process for a new CPU frequency policy; the
87 * "transition" list for kernel code that needs to handle
88 * changes to devices when the CPU clock speed changes.
89 * The mutex locks both lists.
90 */
91 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
92 static struct srcu_notifier_head cpufreq_transition_notifier_list;
93
94 static bool init_cpufreq_transition_notifier_list_called;
95 static int __init init_cpufreq_transition_notifier_list(void)
96 {
97 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
98 init_cpufreq_transition_notifier_list_called = true;
99 return 0;
100 }
101 pure_initcall(init_cpufreq_transition_notifier_list);
102
103 static int off __read_mostly;
104 static int cpufreq_disabled(void)
105 {
106 return off;
107 }
108 void disable_cpufreq(void)
109 {
110 off = 1;
111 }
112 static DEFINE_MUTEX(cpufreq_governor_mutex);
113
114 bool have_governor_per_policy(void)
115 {
116 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
117 }
118 EXPORT_SYMBOL_GPL(have_governor_per_policy);
119
120 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
121 {
122 if (have_governor_per_policy())
123 return &policy->kobj;
124 else
125 return cpufreq_global_kobject;
126 }
127 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
128
129 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
130 {
131 u64 idle_time;
132 u64 cur_wall_time;
133 u64 busy_time;
134
135 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
136
137 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
138 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
139 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
140 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
141 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
142 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
143
144 idle_time = cur_wall_time - busy_time;
145 if (wall)
146 *wall = cputime_to_usecs(cur_wall_time);
147
148 return cputime_to_usecs(idle_time);
149 }
150
151 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
152 {
153 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
154
155 if (idle_time == -1ULL)
156 return get_cpu_idle_time_jiffy(cpu, wall);
157 else if (!io_busy)
158 idle_time += get_cpu_iowait_time_us(cpu, wall);
159
160 return idle_time;
161 }
162 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
163
164 /*
165 * This is a generic cpufreq init() routine which can be used by cpufreq
166 * drivers of SMP systems. It will do following:
167 * - validate & show freq table passed
168 * - set policies transition latency
169 * - policy->cpus with all possible CPUs
170 */
171 int cpufreq_generic_init(struct cpufreq_policy *policy,
172 struct cpufreq_frequency_table *table,
173 unsigned int transition_latency)
174 {
175 int ret;
176
177 ret = cpufreq_table_validate_and_show(policy, table);
178 if (ret) {
179 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
180 return ret;
181 }
182
183 policy->cpuinfo.transition_latency = transition_latency;
184
185 /*
186 * The driver only supports the SMP configuration where all processors
187 * share the clock and voltage and clock.
188 */
189 cpumask_setall(policy->cpus);
190
191 return 0;
192 }
193 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
194
195 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
196 {
197 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
198
199 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
200 }
201 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
202
203 unsigned int cpufreq_generic_get(unsigned int cpu)
204 {
205 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
206
207 if (!policy || IS_ERR(policy->clk)) {
208 pr_err("%s: No %s associated to cpu: %d\n",
209 __func__, policy ? "clk" : "policy", cpu);
210 return 0;
211 }
212
213 return clk_get_rate(policy->clk) / 1000;
214 }
215 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
216
217 /**
218 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
219 *
220 * @cpu: cpu to find policy for.
221 *
222 * This returns policy for 'cpu', returns NULL if it doesn't exist.
223 * It also increments the kobject reference count to mark it busy and so would
224 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
225 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
226 * freed as that depends on the kobj count.
227 *
228 * Return: A valid policy on success, otherwise NULL on failure.
229 */
230 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
231 {
232 struct cpufreq_policy *policy = NULL;
233 unsigned long flags;
234
235 if (WARN_ON(cpu >= nr_cpu_ids))
236 return NULL;
237
238 /* get the cpufreq driver */
239 read_lock_irqsave(&cpufreq_driver_lock, flags);
240
241 if (cpufreq_driver) {
242 /* get the CPU */
243 policy = cpufreq_cpu_get_raw(cpu);
244 if (policy)
245 kobject_get(&policy->kobj);
246 }
247
248 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
249
250 return policy;
251 }
252 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
253
254 /**
255 * cpufreq_cpu_put: Decrements the usage count of a policy
256 *
257 * @policy: policy earlier returned by cpufreq_cpu_get().
258 *
259 * This decrements the kobject reference count incremented earlier by calling
260 * cpufreq_cpu_get().
261 */
262 void cpufreq_cpu_put(struct cpufreq_policy *policy)
263 {
264 kobject_put(&policy->kobj);
265 }
266 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
267
268 /*********************************************************************
269 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
270 *********************************************************************/
271
272 /**
273 * adjust_jiffies - adjust the system "loops_per_jiffy"
274 *
275 * This function alters the system "loops_per_jiffy" for the clock
276 * speed change. Note that loops_per_jiffy cannot be updated on SMP
277 * systems as each CPU might be scaled differently. So, use the arch
278 * per-CPU loops_per_jiffy value wherever possible.
279 */
280 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
281 {
282 #ifndef CONFIG_SMP
283 static unsigned long l_p_j_ref;
284 static unsigned int l_p_j_ref_freq;
285
286 if (ci->flags & CPUFREQ_CONST_LOOPS)
287 return;
288
289 if (!l_p_j_ref_freq) {
290 l_p_j_ref = loops_per_jiffy;
291 l_p_j_ref_freq = ci->old;
292 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
293 l_p_j_ref, l_p_j_ref_freq);
294 }
295 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
296 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
297 ci->new);
298 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
299 loops_per_jiffy, ci->new);
300 }
301 #endif
302 }
303
304 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
305 struct cpufreq_freqs *freqs, unsigned int state)
306 {
307 BUG_ON(irqs_disabled());
308
309 if (cpufreq_disabled())
310 return;
311
312 freqs->flags = cpufreq_driver->flags;
313 pr_debug("notification %u of frequency transition to %u kHz\n",
314 state, freqs->new);
315
316 switch (state) {
317
318 case CPUFREQ_PRECHANGE:
319 /* detect if the driver reported a value as "old frequency"
320 * which is not equal to what the cpufreq core thinks is
321 * "old frequency".
322 */
323 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
324 if ((policy) && (policy->cpu == freqs->cpu) &&
325 (policy->cur) && (policy->cur != freqs->old)) {
326 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
327 freqs->old, policy->cur);
328 freqs->old = policy->cur;
329 }
330 }
331 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
332 CPUFREQ_PRECHANGE, freqs);
333 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
334 break;
335
336 case CPUFREQ_POSTCHANGE:
337 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
338 pr_debug("FREQ: %lu - CPU: %lu\n",
339 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
340 trace_cpu_frequency(freqs->new, freqs->cpu);
341 cpufreq_stats_record_transition(policy, freqs->new);
342 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
343 CPUFREQ_POSTCHANGE, freqs);
344 if (likely(policy) && likely(policy->cpu == freqs->cpu))
345 policy->cur = freqs->new;
346 break;
347 }
348 }
349
350 /**
351 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
352 * on frequency transition.
353 *
354 * This function calls the transition notifiers and the "adjust_jiffies"
355 * function. It is called twice on all CPU frequency changes that have
356 * external effects.
357 */
358 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
359 struct cpufreq_freqs *freqs, unsigned int state)
360 {
361 for_each_cpu(freqs->cpu, policy->cpus)
362 __cpufreq_notify_transition(policy, freqs, state);
363 }
364
365 /* Do post notifications when there are chances that transition has failed */
366 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
367 struct cpufreq_freqs *freqs, int transition_failed)
368 {
369 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
370 if (!transition_failed)
371 return;
372
373 swap(freqs->old, freqs->new);
374 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
375 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
376 }
377
378 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
379 struct cpufreq_freqs *freqs)
380 {
381
382 /*
383 * Catch double invocations of _begin() which lead to self-deadlock.
384 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
385 * doesn't invoke _begin() on their behalf, and hence the chances of
386 * double invocations are very low. Moreover, there are scenarios
387 * where these checks can emit false-positive warnings in these
388 * drivers; so we avoid that by skipping them altogether.
389 */
390 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
391 && current == policy->transition_task);
392
393 wait:
394 wait_event(policy->transition_wait, !policy->transition_ongoing);
395
396 spin_lock(&policy->transition_lock);
397
398 if (unlikely(policy->transition_ongoing)) {
399 spin_unlock(&policy->transition_lock);
400 goto wait;
401 }
402
403 policy->transition_ongoing = true;
404 policy->transition_task = current;
405
406 spin_unlock(&policy->transition_lock);
407
408 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
409 }
410 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
411
412 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
413 struct cpufreq_freqs *freqs, int transition_failed)
414 {
415 if (unlikely(WARN_ON(!policy->transition_ongoing)))
416 return;
417
418 cpufreq_notify_post_transition(policy, freqs, transition_failed);
419
420 policy->transition_ongoing = false;
421 policy->transition_task = NULL;
422
423 wake_up(&policy->transition_wait);
424 }
425 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
426
427 /*
428 * Fast frequency switching status count. Positive means "enabled", negative
429 * means "disabled" and 0 means "not decided yet".
430 */
431 static int cpufreq_fast_switch_count;
432 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
433
434 static void cpufreq_list_transition_notifiers(void)
435 {
436 struct notifier_block *nb;
437
438 pr_info("Registered transition notifiers:\n");
439
440 mutex_lock(&cpufreq_transition_notifier_list.mutex);
441
442 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
443 pr_info("%pF\n", nb->notifier_call);
444
445 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
446 }
447
448 /**
449 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
450 * @policy: cpufreq policy to enable fast frequency switching for.
451 *
452 * Try to enable fast frequency switching for @policy.
453 *
454 * The attempt will fail if there is at least one transition notifier registered
455 * at this point, as fast frequency switching is quite fundamentally at odds
456 * with transition notifiers. Thus if successful, it will make registration of
457 * transition notifiers fail going forward.
458 */
459 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
460 {
461 lockdep_assert_held(&policy->rwsem);
462
463 if (!policy->fast_switch_possible)
464 return;
465
466 mutex_lock(&cpufreq_fast_switch_lock);
467 if (cpufreq_fast_switch_count >= 0) {
468 cpufreq_fast_switch_count++;
469 policy->fast_switch_enabled = true;
470 } else {
471 pr_warn("CPU%u: Fast frequency switching not enabled\n",
472 policy->cpu);
473 cpufreq_list_transition_notifiers();
474 }
475 mutex_unlock(&cpufreq_fast_switch_lock);
476 }
477 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
478
479 /**
480 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
481 * @policy: cpufreq policy to disable fast frequency switching for.
482 */
483 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
484 {
485 mutex_lock(&cpufreq_fast_switch_lock);
486 if (policy->fast_switch_enabled) {
487 policy->fast_switch_enabled = false;
488 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
489 cpufreq_fast_switch_count--;
490 }
491 mutex_unlock(&cpufreq_fast_switch_lock);
492 }
493 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
494
495 /*********************************************************************
496 * SYSFS INTERFACE *
497 *********************************************************************/
498 static ssize_t show_boost(struct kobject *kobj,
499 struct attribute *attr, char *buf)
500 {
501 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
502 }
503
504 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
505 const char *buf, size_t count)
506 {
507 int ret, enable;
508
509 ret = sscanf(buf, "%d", &enable);
510 if (ret != 1 || enable < 0 || enable > 1)
511 return -EINVAL;
512
513 if (cpufreq_boost_trigger_state(enable)) {
514 pr_err("%s: Cannot %s BOOST!\n",
515 __func__, enable ? "enable" : "disable");
516 return -EINVAL;
517 }
518
519 pr_debug("%s: cpufreq BOOST %s\n",
520 __func__, enable ? "enabled" : "disabled");
521
522 return count;
523 }
524 define_one_global_rw(boost);
525
526 static struct cpufreq_governor *find_governor(const char *str_governor)
527 {
528 struct cpufreq_governor *t;
529
530 for_each_governor(t)
531 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
532 return t;
533
534 return NULL;
535 }
536
537 /**
538 * cpufreq_parse_governor - parse a governor string
539 */
540 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
541 struct cpufreq_governor **governor)
542 {
543 int err = -EINVAL;
544
545 if (cpufreq_driver->setpolicy) {
546 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
547 *policy = CPUFREQ_POLICY_PERFORMANCE;
548 err = 0;
549 } else if (!strncasecmp(str_governor, "powersave",
550 CPUFREQ_NAME_LEN)) {
551 *policy = CPUFREQ_POLICY_POWERSAVE;
552 err = 0;
553 }
554 } else {
555 struct cpufreq_governor *t;
556
557 mutex_lock(&cpufreq_governor_mutex);
558
559 t = find_governor(str_governor);
560
561 if (t == NULL) {
562 int ret;
563
564 mutex_unlock(&cpufreq_governor_mutex);
565 ret = request_module("cpufreq_%s", str_governor);
566 mutex_lock(&cpufreq_governor_mutex);
567
568 if (ret == 0)
569 t = find_governor(str_governor);
570 }
571
572 if (t != NULL) {
573 *governor = t;
574 err = 0;
575 }
576
577 mutex_unlock(&cpufreq_governor_mutex);
578 }
579 return err;
580 }
581
582 /**
583 * cpufreq_per_cpu_attr_read() / show_##file_name() -
584 * print out cpufreq information
585 *
586 * Write out information from cpufreq_driver->policy[cpu]; object must be
587 * "unsigned int".
588 */
589
590 #define show_one(file_name, object) \
591 static ssize_t show_##file_name \
592 (struct cpufreq_policy *policy, char *buf) \
593 { \
594 return sprintf(buf, "%u\n", policy->object); \
595 }
596
597 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
598 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
599 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
600 show_one(scaling_min_freq, min);
601 show_one(scaling_max_freq, max);
602
603 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
604 {
605 ssize_t ret;
606
607 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
608 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
609 else
610 ret = sprintf(buf, "%u\n", policy->cur);
611 return ret;
612 }
613
614 static int cpufreq_set_policy(struct cpufreq_policy *policy,
615 struct cpufreq_policy *new_policy);
616
617 /**
618 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
619 */
620 #define store_one(file_name, object) \
621 static ssize_t store_##file_name \
622 (struct cpufreq_policy *policy, const char *buf, size_t count) \
623 { \
624 int ret, temp; \
625 struct cpufreq_policy new_policy; \
626 \
627 memcpy(&new_policy, policy, sizeof(*policy)); \
628 \
629 ret = sscanf(buf, "%u", &new_policy.object); \
630 if (ret != 1) \
631 return -EINVAL; \
632 \
633 temp = new_policy.object; \
634 ret = cpufreq_set_policy(policy, &new_policy); \
635 if (!ret) \
636 policy->user_policy.object = temp; \
637 \
638 return ret ? ret : count; \
639 }
640
641 store_one(scaling_min_freq, min);
642 store_one(scaling_max_freq, max);
643
644 /**
645 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
646 */
647 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
648 char *buf)
649 {
650 unsigned int cur_freq = __cpufreq_get(policy);
651 if (!cur_freq)
652 return sprintf(buf, "<unknown>");
653 return sprintf(buf, "%u\n", cur_freq);
654 }
655
656 /**
657 * show_scaling_governor - show the current policy for the specified CPU
658 */
659 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
660 {
661 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
662 return sprintf(buf, "powersave\n");
663 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
664 return sprintf(buf, "performance\n");
665 else if (policy->governor)
666 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
667 policy->governor->name);
668 return -EINVAL;
669 }
670
671 /**
672 * store_scaling_governor - store policy for the specified CPU
673 */
674 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
675 const char *buf, size_t count)
676 {
677 int ret;
678 char str_governor[16];
679 struct cpufreq_policy new_policy;
680
681 memcpy(&new_policy, policy, sizeof(*policy));
682
683 ret = sscanf(buf, "%15s", str_governor);
684 if (ret != 1)
685 return -EINVAL;
686
687 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
688 &new_policy.governor))
689 return -EINVAL;
690
691 ret = cpufreq_set_policy(policy, &new_policy);
692 return ret ? ret : count;
693 }
694
695 /**
696 * show_scaling_driver - show the cpufreq driver currently loaded
697 */
698 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
699 {
700 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
701 }
702
703 /**
704 * show_scaling_available_governors - show the available CPUfreq governors
705 */
706 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
707 char *buf)
708 {
709 ssize_t i = 0;
710 struct cpufreq_governor *t;
711
712 if (!has_target()) {
713 i += sprintf(buf, "performance powersave");
714 goto out;
715 }
716
717 for_each_governor(t) {
718 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
719 - (CPUFREQ_NAME_LEN + 2)))
720 goto out;
721 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
722 }
723 out:
724 i += sprintf(&buf[i], "\n");
725 return i;
726 }
727
728 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
729 {
730 ssize_t i = 0;
731 unsigned int cpu;
732
733 for_each_cpu(cpu, mask) {
734 if (i)
735 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
736 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
737 if (i >= (PAGE_SIZE - 5))
738 break;
739 }
740 i += sprintf(&buf[i], "\n");
741 return i;
742 }
743 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
744
745 /**
746 * show_related_cpus - show the CPUs affected by each transition even if
747 * hw coordination is in use
748 */
749 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
750 {
751 return cpufreq_show_cpus(policy->related_cpus, buf);
752 }
753
754 /**
755 * show_affected_cpus - show the CPUs affected by each transition
756 */
757 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
758 {
759 return cpufreq_show_cpus(policy->cpus, buf);
760 }
761
762 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
763 const char *buf, size_t count)
764 {
765 unsigned int freq = 0;
766 unsigned int ret;
767
768 if (!policy->governor || !policy->governor->store_setspeed)
769 return -EINVAL;
770
771 ret = sscanf(buf, "%u", &freq);
772 if (ret != 1)
773 return -EINVAL;
774
775 policy->governor->store_setspeed(policy, freq);
776
777 return count;
778 }
779
780 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
781 {
782 if (!policy->governor || !policy->governor->show_setspeed)
783 return sprintf(buf, "<unsupported>\n");
784
785 return policy->governor->show_setspeed(policy, buf);
786 }
787
788 /**
789 * show_bios_limit - show the current cpufreq HW/BIOS limitation
790 */
791 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
792 {
793 unsigned int limit;
794 int ret;
795 if (cpufreq_driver->bios_limit) {
796 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
797 if (!ret)
798 return sprintf(buf, "%u\n", limit);
799 }
800 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
801 }
802
803 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
804 cpufreq_freq_attr_ro(cpuinfo_min_freq);
805 cpufreq_freq_attr_ro(cpuinfo_max_freq);
806 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
807 cpufreq_freq_attr_ro(scaling_available_governors);
808 cpufreq_freq_attr_ro(scaling_driver);
809 cpufreq_freq_attr_ro(scaling_cur_freq);
810 cpufreq_freq_attr_ro(bios_limit);
811 cpufreq_freq_attr_ro(related_cpus);
812 cpufreq_freq_attr_ro(affected_cpus);
813 cpufreq_freq_attr_rw(scaling_min_freq);
814 cpufreq_freq_attr_rw(scaling_max_freq);
815 cpufreq_freq_attr_rw(scaling_governor);
816 cpufreq_freq_attr_rw(scaling_setspeed);
817
818 static struct attribute *default_attrs[] = {
819 &cpuinfo_min_freq.attr,
820 &cpuinfo_max_freq.attr,
821 &cpuinfo_transition_latency.attr,
822 &scaling_min_freq.attr,
823 &scaling_max_freq.attr,
824 &affected_cpus.attr,
825 &related_cpus.attr,
826 &scaling_governor.attr,
827 &scaling_driver.attr,
828 &scaling_available_governors.attr,
829 &scaling_setspeed.attr,
830 NULL
831 };
832
833 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
834 #define to_attr(a) container_of(a, struct freq_attr, attr)
835
836 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
837 {
838 struct cpufreq_policy *policy = to_policy(kobj);
839 struct freq_attr *fattr = to_attr(attr);
840 ssize_t ret;
841
842 down_read(&policy->rwsem);
843 ret = fattr->show(policy, buf);
844 up_read(&policy->rwsem);
845
846 return ret;
847 }
848
849 static ssize_t store(struct kobject *kobj, struct attribute *attr,
850 const char *buf, size_t count)
851 {
852 struct cpufreq_policy *policy = to_policy(kobj);
853 struct freq_attr *fattr = to_attr(attr);
854 ssize_t ret = -EINVAL;
855
856 get_online_cpus();
857
858 if (cpu_online(policy->cpu)) {
859 down_write(&policy->rwsem);
860 ret = fattr->store(policy, buf, count);
861 up_write(&policy->rwsem);
862 }
863
864 put_online_cpus();
865
866 return ret;
867 }
868
869 static void cpufreq_sysfs_release(struct kobject *kobj)
870 {
871 struct cpufreq_policy *policy = to_policy(kobj);
872 pr_debug("last reference is dropped\n");
873 complete(&policy->kobj_unregister);
874 }
875
876 static const struct sysfs_ops sysfs_ops = {
877 .show = show,
878 .store = store,
879 };
880
881 static struct kobj_type ktype_cpufreq = {
882 .sysfs_ops = &sysfs_ops,
883 .default_attrs = default_attrs,
884 .release = cpufreq_sysfs_release,
885 };
886
887 static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
888 {
889 struct device *cpu_dev;
890
891 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
892
893 if (!policy)
894 return 0;
895
896 cpu_dev = get_cpu_device(cpu);
897 if (WARN_ON(!cpu_dev))
898 return 0;
899
900 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
901 }
902
903 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
904 {
905 struct device *cpu_dev;
906
907 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
908
909 cpu_dev = get_cpu_device(cpu);
910 if (WARN_ON(!cpu_dev))
911 return;
912
913 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
914 }
915
916 /* Add/remove symlinks for all related CPUs */
917 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
918 {
919 unsigned int j;
920 int ret = 0;
921
922 /* Some related CPUs might not be present (physically hotplugged) */
923 for_each_cpu(j, policy->real_cpus) {
924 ret = add_cpu_dev_symlink(policy, j);
925 if (ret)
926 break;
927 }
928
929 return ret;
930 }
931
932 static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
933 {
934 unsigned int j;
935
936 /* Some related CPUs might not be present (physically hotplugged) */
937 for_each_cpu(j, policy->real_cpus)
938 remove_cpu_dev_symlink(policy, j);
939 }
940
941 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
942 {
943 struct freq_attr **drv_attr;
944 int ret = 0;
945
946 /* set up files for this cpu device */
947 drv_attr = cpufreq_driver->attr;
948 while (drv_attr && *drv_attr) {
949 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
950 if (ret)
951 return ret;
952 drv_attr++;
953 }
954 if (cpufreq_driver->get) {
955 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
956 if (ret)
957 return ret;
958 }
959
960 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
961 if (ret)
962 return ret;
963
964 if (cpufreq_driver->bios_limit) {
965 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
966 if (ret)
967 return ret;
968 }
969
970 return cpufreq_add_dev_symlink(policy);
971 }
972
973 __weak struct cpufreq_governor *cpufreq_default_governor(void)
974 {
975 return NULL;
976 }
977
978 static int cpufreq_init_policy(struct cpufreq_policy *policy)
979 {
980 struct cpufreq_governor *gov = NULL;
981 struct cpufreq_policy new_policy;
982
983 memcpy(&new_policy, policy, sizeof(*policy));
984
985 /* Update governor of new_policy to the governor used before hotplug */
986 gov = find_governor(policy->last_governor);
987 if (gov) {
988 pr_debug("Restoring governor %s for cpu %d\n",
989 policy->governor->name, policy->cpu);
990 } else {
991 gov = cpufreq_default_governor();
992 if (!gov)
993 return -ENODATA;
994 }
995
996 new_policy.governor = gov;
997
998 /* Use the default policy if there is no last_policy. */
999 if (cpufreq_driver->setpolicy) {
1000 if (policy->last_policy)
1001 new_policy.policy = policy->last_policy;
1002 else
1003 cpufreq_parse_governor(gov->name, &new_policy.policy,
1004 NULL);
1005 }
1006 /* set default policy */
1007 return cpufreq_set_policy(policy, &new_policy);
1008 }
1009
1010 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1011 {
1012 int ret = 0;
1013
1014 /* Has this CPU been taken care of already? */
1015 if (cpumask_test_cpu(cpu, policy->cpus))
1016 return 0;
1017
1018 down_write(&policy->rwsem);
1019 if (has_target())
1020 cpufreq_stop_governor(policy);
1021
1022 cpumask_set_cpu(cpu, policy->cpus);
1023
1024 if (has_target()) {
1025 ret = cpufreq_start_governor(policy);
1026 if (ret)
1027 pr_err("%s: Failed to start governor\n", __func__);
1028 }
1029 up_write(&policy->rwsem);
1030 return ret;
1031 }
1032
1033 static void handle_update(struct work_struct *work)
1034 {
1035 struct cpufreq_policy *policy =
1036 container_of(work, struct cpufreq_policy, update);
1037 unsigned int cpu = policy->cpu;
1038 pr_debug("handle_update for cpu %u called\n", cpu);
1039 cpufreq_update_policy(cpu);
1040 }
1041
1042 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1043 {
1044 struct device *dev = get_cpu_device(cpu);
1045 struct cpufreq_policy *policy;
1046 int ret;
1047
1048 if (WARN_ON(!dev))
1049 return NULL;
1050
1051 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1052 if (!policy)
1053 return NULL;
1054
1055 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1056 goto err_free_policy;
1057
1058 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1059 goto err_free_cpumask;
1060
1061 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1062 goto err_free_rcpumask;
1063
1064 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1065 cpufreq_global_kobject, "policy%u", cpu);
1066 if (ret) {
1067 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1068 goto err_free_real_cpus;
1069 }
1070
1071 INIT_LIST_HEAD(&policy->policy_list);
1072 init_rwsem(&policy->rwsem);
1073 spin_lock_init(&policy->transition_lock);
1074 init_waitqueue_head(&policy->transition_wait);
1075 init_completion(&policy->kobj_unregister);
1076 INIT_WORK(&policy->update, handle_update);
1077
1078 policy->cpu = cpu;
1079 return policy;
1080
1081 err_free_real_cpus:
1082 free_cpumask_var(policy->real_cpus);
1083 err_free_rcpumask:
1084 free_cpumask_var(policy->related_cpus);
1085 err_free_cpumask:
1086 free_cpumask_var(policy->cpus);
1087 err_free_policy:
1088 kfree(policy);
1089
1090 return NULL;
1091 }
1092
1093 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
1094 {
1095 struct kobject *kobj;
1096 struct completion *cmp;
1097
1098 if (notify)
1099 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1100 CPUFREQ_REMOVE_POLICY, policy);
1101
1102 down_write(&policy->rwsem);
1103 cpufreq_stats_free_table(policy);
1104 cpufreq_remove_dev_symlink(policy);
1105 kobj = &policy->kobj;
1106 cmp = &policy->kobj_unregister;
1107 up_write(&policy->rwsem);
1108 kobject_put(kobj);
1109
1110 /*
1111 * We need to make sure that the underlying kobj is
1112 * actually not referenced anymore by anybody before we
1113 * proceed with unloading.
1114 */
1115 pr_debug("waiting for dropping of refcount\n");
1116 wait_for_completion(cmp);
1117 pr_debug("wait complete\n");
1118 }
1119
1120 static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
1121 {
1122 unsigned long flags;
1123 int cpu;
1124
1125 /* Remove policy from list */
1126 write_lock_irqsave(&cpufreq_driver_lock, flags);
1127 list_del(&policy->policy_list);
1128
1129 for_each_cpu(cpu, policy->related_cpus)
1130 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1131 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1132
1133 cpufreq_policy_put_kobj(policy, notify);
1134 free_cpumask_var(policy->real_cpus);
1135 free_cpumask_var(policy->related_cpus);
1136 free_cpumask_var(policy->cpus);
1137 kfree(policy);
1138 }
1139
1140 static int cpufreq_online(unsigned int cpu)
1141 {
1142 struct cpufreq_policy *policy;
1143 bool new_policy;
1144 unsigned long flags;
1145 unsigned int j;
1146 int ret;
1147
1148 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1149
1150 /* Check if this CPU already has a policy to manage it */
1151 policy = per_cpu(cpufreq_cpu_data, cpu);
1152 if (policy) {
1153 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1154 if (!policy_is_inactive(policy))
1155 return cpufreq_add_policy_cpu(policy, cpu);
1156
1157 /* This is the only online CPU for the policy. Start over. */
1158 new_policy = false;
1159 down_write(&policy->rwsem);
1160 policy->cpu = cpu;
1161 policy->governor = NULL;
1162 up_write(&policy->rwsem);
1163 } else {
1164 new_policy = true;
1165 policy = cpufreq_policy_alloc(cpu);
1166 if (!policy)
1167 return -ENOMEM;
1168 }
1169
1170 cpumask_copy(policy->cpus, cpumask_of(cpu));
1171
1172 /* call driver. From then on the cpufreq must be able
1173 * to accept all calls to ->verify and ->setpolicy for this CPU
1174 */
1175 ret = cpufreq_driver->init(policy);
1176 if (ret) {
1177 pr_debug("initialization failed\n");
1178 goto out_free_policy;
1179 }
1180
1181 down_write(&policy->rwsem);
1182
1183 if (new_policy) {
1184 /* related_cpus should at least include policy->cpus. */
1185 cpumask_copy(policy->related_cpus, policy->cpus);
1186 /* Remember CPUs present at the policy creation time. */
1187 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
1188 }
1189
1190 /*
1191 * affected cpus must always be the one, which are online. We aren't
1192 * managing offline cpus here.
1193 */
1194 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1195
1196 if (new_policy) {
1197 policy->user_policy.min = policy->min;
1198 policy->user_policy.max = policy->max;
1199
1200 write_lock_irqsave(&cpufreq_driver_lock, flags);
1201 for_each_cpu(j, policy->related_cpus)
1202 per_cpu(cpufreq_cpu_data, j) = policy;
1203 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1204 }
1205
1206 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1207 policy->cur = cpufreq_driver->get(policy->cpu);
1208 if (!policy->cur) {
1209 pr_err("%s: ->get() failed\n", __func__);
1210 goto out_exit_policy;
1211 }
1212 }
1213
1214 /*
1215 * Sometimes boot loaders set CPU frequency to a value outside of
1216 * frequency table present with cpufreq core. In such cases CPU might be
1217 * unstable if it has to run on that frequency for long duration of time
1218 * and so its better to set it to a frequency which is specified in
1219 * freq-table. This also makes cpufreq stats inconsistent as
1220 * cpufreq-stats would fail to register because current frequency of CPU
1221 * isn't found in freq-table.
1222 *
1223 * Because we don't want this change to effect boot process badly, we go
1224 * for the next freq which is >= policy->cur ('cur' must be set by now,
1225 * otherwise we will end up setting freq to lowest of the table as 'cur'
1226 * is initialized to zero).
1227 *
1228 * We are passing target-freq as "policy->cur - 1" otherwise
1229 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1230 * equal to target-freq.
1231 */
1232 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1233 && has_target()) {
1234 /* Are we running at unknown frequency ? */
1235 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1236 if (ret == -EINVAL) {
1237 /* Warn user and fix it */
1238 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1239 __func__, policy->cpu, policy->cur);
1240 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1241 CPUFREQ_RELATION_L);
1242
1243 /*
1244 * Reaching here after boot in a few seconds may not
1245 * mean that system will remain stable at "unknown"
1246 * frequency for longer duration. Hence, a BUG_ON().
1247 */
1248 BUG_ON(ret);
1249 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1250 __func__, policy->cpu, policy->cur);
1251 }
1252 }
1253
1254 if (new_policy) {
1255 ret = cpufreq_add_dev_interface(policy);
1256 if (ret)
1257 goto out_exit_policy;
1258
1259 cpufreq_stats_create_table(policy);
1260 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1261 CPUFREQ_CREATE_POLICY, policy);
1262
1263 write_lock_irqsave(&cpufreq_driver_lock, flags);
1264 list_add(&policy->policy_list, &cpufreq_policy_list);
1265 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1266 }
1267
1268 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1269 CPUFREQ_START, policy);
1270
1271 ret = cpufreq_init_policy(policy);
1272 if (ret) {
1273 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1274 __func__, cpu, ret);
1275 /* cpufreq_policy_free() will notify based on this */
1276 new_policy = false;
1277 goto out_exit_policy;
1278 }
1279
1280 up_write(&policy->rwsem);
1281
1282 kobject_uevent(&policy->kobj, KOBJ_ADD);
1283
1284 /* Callback for handling stuff after policy is ready */
1285 if (cpufreq_driver->ready)
1286 cpufreq_driver->ready(policy);
1287
1288 pr_debug("initialization complete\n");
1289
1290 return 0;
1291
1292 out_exit_policy:
1293 up_write(&policy->rwsem);
1294
1295 if (cpufreq_driver->exit)
1296 cpufreq_driver->exit(policy);
1297 out_free_policy:
1298 cpufreq_policy_free(policy, !new_policy);
1299 return ret;
1300 }
1301
1302 /**
1303 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1304 * @dev: CPU device.
1305 * @sif: Subsystem interface structure pointer (not used)
1306 */
1307 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1308 {
1309 struct cpufreq_policy *policy;
1310 unsigned cpu = dev->id;
1311
1312 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1313
1314 if (cpu_online(cpu))
1315 return cpufreq_online(cpu);
1316
1317 /*
1318 * A hotplug notifier will follow and we will handle it as CPU online
1319 * then. For now, just create the sysfs link, unless there is no policy
1320 * or the link is already present.
1321 */
1322 policy = per_cpu(cpufreq_cpu_data, cpu);
1323 if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1324 return 0;
1325
1326 return add_cpu_dev_symlink(policy, cpu);
1327 }
1328
1329 static void cpufreq_offline(unsigned int cpu)
1330 {
1331 struct cpufreq_policy *policy;
1332 int ret;
1333
1334 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1335
1336 policy = cpufreq_cpu_get_raw(cpu);
1337 if (!policy) {
1338 pr_debug("%s: No cpu_data found\n", __func__);
1339 return;
1340 }
1341
1342 down_write(&policy->rwsem);
1343 if (has_target())
1344 cpufreq_stop_governor(policy);
1345
1346 cpumask_clear_cpu(cpu, policy->cpus);
1347
1348 if (policy_is_inactive(policy)) {
1349 if (has_target())
1350 strncpy(policy->last_governor, policy->governor->name,
1351 CPUFREQ_NAME_LEN);
1352 else
1353 policy->last_policy = policy->policy;
1354 } else if (cpu == policy->cpu) {
1355 /* Nominate new CPU */
1356 policy->cpu = cpumask_any(policy->cpus);
1357 }
1358
1359 /* Start governor again for active policy */
1360 if (!policy_is_inactive(policy)) {
1361 if (has_target()) {
1362 ret = cpufreq_start_governor(policy);
1363 if (ret)
1364 pr_err("%s: Failed to start governor\n", __func__);
1365 }
1366
1367 goto unlock;
1368 }
1369
1370 if (cpufreq_driver->stop_cpu)
1371 cpufreq_driver->stop_cpu(policy);
1372
1373 if (has_target())
1374 cpufreq_exit_governor(policy);
1375
1376 /*
1377 * Perform the ->exit() even during light-weight tear-down,
1378 * since this is a core component, and is essential for the
1379 * subsequent light-weight ->init() to succeed.
1380 */
1381 if (cpufreq_driver->exit) {
1382 cpufreq_driver->exit(policy);
1383 policy->freq_table = NULL;
1384 }
1385
1386 unlock:
1387 up_write(&policy->rwsem);
1388 }
1389
1390 /**
1391 * cpufreq_remove_dev - remove a CPU device
1392 *
1393 * Removes the cpufreq interface for a CPU device.
1394 */
1395 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1396 {
1397 unsigned int cpu = dev->id;
1398 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1399
1400 if (!policy)
1401 return;
1402
1403 if (cpu_online(cpu))
1404 cpufreq_offline(cpu);
1405
1406 cpumask_clear_cpu(cpu, policy->real_cpus);
1407 remove_cpu_dev_symlink(policy, cpu);
1408
1409 if (cpumask_empty(policy->real_cpus))
1410 cpufreq_policy_free(policy, true);
1411 }
1412
1413 /**
1414 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1415 * in deep trouble.
1416 * @policy: policy managing CPUs
1417 * @new_freq: CPU frequency the CPU actually runs at
1418 *
1419 * We adjust to current frequency first, and need to clean up later.
1420 * So either call to cpufreq_update_policy() or schedule handle_update()).
1421 */
1422 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1423 unsigned int new_freq)
1424 {
1425 struct cpufreq_freqs freqs;
1426
1427 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1428 policy->cur, new_freq);
1429
1430 freqs.old = policy->cur;
1431 freqs.new = new_freq;
1432
1433 cpufreq_freq_transition_begin(policy, &freqs);
1434 cpufreq_freq_transition_end(policy, &freqs, 0);
1435 }
1436
1437 /**
1438 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1439 * @cpu: CPU number
1440 *
1441 * This is the last known freq, without actually getting it from the driver.
1442 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1443 */
1444 unsigned int cpufreq_quick_get(unsigned int cpu)
1445 {
1446 struct cpufreq_policy *policy;
1447 unsigned int ret_freq = 0;
1448 unsigned long flags;
1449
1450 read_lock_irqsave(&cpufreq_driver_lock, flags);
1451
1452 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1453 ret_freq = cpufreq_driver->get(cpu);
1454 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1455 return ret_freq;
1456 }
1457
1458 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1459
1460 policy = cpufreq_cpu_get(cpu);
1461 if (policy) {
1462 ret_freq = policy->cur;
1463 cpufreq_cpu_put(policy);
1464 }
1465
1466 return ret_freq;
1467 }
1468 EXPORT_SYMBOL(cpufreq_quick_get);
1469
1470 /**
1471 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1472 * @cpu: CPU number
1473 *
1474 * Just return the max possible frequency for a given CPU.
1475 */
1476 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1477 {
1478 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1479 unsigned int ret_freq = 0;
1480
1481 if (policy) {
1482 ret_freq = policy->max;
1483 cpufreq_cpu_put(policy);
1484 }
1485
1486 return ret_freq;
1487 }
1488 EXPORT_SYMBOL(cpufreq_quick_get_max);
1489
1490 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1491 {
1492 unsigned int ret_freq = 0;
1493
1494 if (!cpufreq_driver->get)
1495 return ret_freq;
1496
1497 ret_freq = cpufreq_driver->get(policy->cpu);
1498
1499 /*
1500 * Updating inactive policies is invalid, so avoid doing that. Also
1501 * if fast frequency switching is used with the given policy, the check
1502 * against policy->cur is pointless, so skip it in that case too.
1503 */
1504 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
1505 return ret_freq;
1506
1507 if (ret_freq && policy->cur &&
1508 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1509 /* verify no discrepancy between actual and
1510 saved value exists */
1511 if (unlikely(ret_freq != policy->cur)) {
1512 cpufreq_out_of_sync(policy, ret_freq);
1513 schedule_work(&policy->update);
1514 }
1515 }
1516
1517 return ret_freq;
1518 }
1519
1520 /**
1521 * cpufreq_get - get the current CPU frequency (in kHz)
1522 * @cpu: CPU number
1523 *
1524 * Get the CPU current (static) CPU frequency
1525 */
1526 unsigned int cpufreq_get(unsigned int cpu)
1527 {
1528 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1529 unsigned int ret_freq = 0;
1530
1531 if (policy) {
1532 down_read(&policy->rwsem);
1533 ret_freq = __cpufreq_get(policy);
1534 up_read(&policy->rwsem);
1535
1536 cpufreq_cpu_put(policy);
1537 }
1538
1539 return ret_freq;
1540 }
1541 EXPORT_SYMBOL(cpufreq_get);
1542
1543 static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1544 {
1545 unsigned int new_freq;
1546
1547 new_freq = cpufreq_driver->get(policy->cpu);
1548 if (!new_freq)
1549 return 0;
1550
1551 if (!policy->cur) {
1552 pr_debug("cpufreq: Driver did not initialize current freq\n");
1553 policy->cur = new_freq;
1554 } else if (policy->cur != new_freq && has_target()) {
1555 cpufreq_out_of_sync(policy, new_freq);
1556 }
1557
1558 return new_freq;
1559 }
1560
1561 static struct subsys_interface cpufreq_interface = {
1562 .name = "cpufreq",
1563 .subsys = &cpu_subsys,
1564 .add_dev = cpufreq_add_dev,
1565 .remove_dev = cpufreq_remove_dev,
1566 };
1567
1568 /*
1569 * In case platform wants some specific frequency to be configured
1570 * during suspend..
1571 */
1572 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1573 {
1574 int ret;
1575
1576 if (!policy->suspend_freq) {
1577 pr_debug("%s: suspend_freq not defined\n", __func__);
1578 return 0;
1579 }
1580
1581 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1582 policy->suspend_freq);
1583
1584 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1585 CPUFREQ_RELATION_H);
1586 if (ret)
1587 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1588 __func__, policy->suspend_freq, ret);
1589
1590 return ret;
1591 }
1592 EXPORT_SYMBOL(cpufreq_generic_suspend);
1593
1594 /**
1595 * cpufreq_suspend() - Suspend CPUFreq governors
1596 *
1597 * Called during system wide Suspend/Hibernate cycles for suspending governors
1598 * as some platforms can't change frequency after this point in suspend cycle.
1599 * Because some of the devices (like: i2c, regulators, etc) they use for
1600 * changing frequency are suspended quickly after this point.
1601 */
1602 void cpufreq_suspend(void)
1603 {
1604 struct cpufreq_policy *policy;
1605
1606 if (!cpufreq_driver)
1607 return;
1608
1609 if (!has_target() && !cpufreq_driver->suspend)
1610 goto suspend;
1611
1612 pr_debug("%s: Suspending Governors\n", __func__);
1613
1614 for_each_active_policy(policy) {
1615 if (has_target()) {
1616 down_write(&policy->rwsem);
1617 cpufreq_stop_governor(policy);
1618 up_write(&policy->rwsem);
1619 }
1620
1621 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1622 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1623 policy);
1624 }
1625
1626 suspend:
1627 cpufreq_suspended = true;
1628 }
1629
1630 /**
1631 * cpufreq_resume() - Resume CPUFreq governors
1632 *
1633 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1634 * are suspended with cpufreq_suspend().
1635 */
1636 void cpufreq_resume(void)
1637 {
1638 struct cpufreq_policy *policy;
1639 int ret;
1640
1641 if (!cpufreq_driver)
1642 return;
1643
1644 cpufreq_suspended = false;
1645
1646 if (!has_target() && !cpufreq_driver->resume)
1647 return;
1648
1649 pr_debug("%s: Resuming Governors\n", __func__);
1650
1651 for_each_active_policy(policy) {
1652 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1653 pr_err("%s: Failed to resume driver: %p\n", __func__,
1654 policy);
1655 } else if (has_target()) {
1656 down_write(&policy->rwsem);
1657 ret = cpufreq_start_governor(policy);
1658 up_write(&policy->rwsem);
1659
1660 if (ret)
1661 pr_err("%s: Failed to start governor for policy: %p\n",
1662 __func__, policy);
1663 }
1664 }
1665 }
1666
1667 /**
1668 * cpufreq_get_current_driver - return current driver's name
1669 *
1670 * Return the name string of the currently loaded cpufreq driver
1671 * or NULL, if none.
1672 */
1673 const char *cpufreq_get_current_driver(void)
1674 {
1675 if (cpufreq_driver)
1676 return cpufreq_driver->name;
1677
1678 return NULL;
1679 }
1680 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1681
1682 /**
1683 * cpufreq_get_driver_data - return current driver data
1684 *
1685 * Return the private data of the currently loaded cpufreq
1686 * driver, or NULL if no cpufreq driver is loaded.
1687 */
1688 void *cpufreq_get_driver_data(void)
1689 {
1690 if (cpufreq_driver)
1691 return cpufreq_driver->driver_data;
1692
1693 return NULL;
1694 }
1695 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1696
1697 /*********************************************************************
1698 * NOTIFIER LISTS INTERFACE *
1699 *********************************************************************/
1700
1701 /**
1702 * cpufreq_register_notifier - register a driver with cpufreq
1703 * @nb: notifier function to register
1704 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1705 *
1706 * Add a driver to one of two lists: either a list of drivers that
1707 * are notified about clock rate changes (once before and once after
1708 * the transition), or a list of drivers that are notified about
1709 * changes in cpufreq policy.
1710 *
1711 * This function may sleep, and has the same return conditions as
1712 * blocking_notifier_chain_register.
1713 */
1714 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1715 {
1716 int ret;
1717
1718 if (cpufreq_disabled())
1719 return -EINVAL;
1720
1721 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1722
1723 switch (list) {
1724 case CPUFREQ_TRANSITION_NOTIFIER:
1725 mutex_lock(&cpufreq_fast_switch_lock);
1726
1727 if (cpufreq_fast_switch_count > 0) {
1728 mutex_unlock(&cpufreq_fast_switch_lock);
1729 return -EBUSY;
1730 }
1731 ret = srcu_notifier_chain_register(
1732 &cpufreq_transition_notifier_list, nb);
1733 if (!ret)
1734 cpufreq_fast_switch_count--;
1735
1736 mutex_unlock(&cpufreq_fast_switch_lock);
1737 break;
1738 case CPUFREQ_POLICY_NOTIFIER:
1739 ret = blocking_notifier_chain_register(
1740 &cpufreq_policy_notifier_list, nb);
1741 break;
1742 default:
1743 ret = -EINVAL;
1744 }
1745
1746 return ret;
1747 }
1748 EXPORT_SYMBOL(cpufreq_register_notifier);
1749
1750 /**
1751 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1752 * @nb: notifier block to be unregistered
1753 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1754 *
1755 * Remove a driver from the CPU frequency notifier list.
1756 *
1757 * This function may sleep, and has the same return conditions as
1758 * blocking_notifier_chain_unregister.
1759 */
1760 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1761 {
1762 int ret;
1763
1764 if (cpufreq_disabled())
1765 return -EINVAL;
1766
1767 switch (list) {
1768 case CPUFREQ_TRANSITION_NOTIFIER:
1769 mutex_lock(&cpufreq_fast_switch_lock);
1770
1771 ret = srcu_notifier_chain_unregister(
1772 &cpufreq_transition_notifier_list, nb);
1773 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1774 cpufreq_fast_switch_count++;
1775
1776 mutex_unlock(&cpufreq_fast_switch_lock);
1777 break;
1778 case CPUFREQ_POLICY_NOTIFIER:
1779 ret = blocking_notifier_chain_unregister(
1780 &cpufreq_policy_notifier_list, nb);
1781 break;
1782 default:
1783 ret = -EINVAL;
1784 }
1785
1786 return ret;
1787 }
1788 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1789
1790
1791 /*********************************************************************
1792 * GOVERNORS *
1793 *********************************************************************/
1794
1795 /**
1796 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1797 * @policy: cpufreq policy to switch the frequency for.
1798 * @target_freq: New frequency to set (may be approximate).
1799 *
1800 * Carry out a fast frequency switch without sleeping.
1801 *
1802 * The driver's ->fast_switch() callback invoked by this function must be
1803 * suitable for being called from within RCU-sched read-side critical sections
1804 * and it is expected to select the minimum available frequency greater than or
1805 * equal to @target_freq (CPUFREQ_RELATION_L).
1806 *
1807 * This function must not be called if policy->fast_switch_enabled is unset.
1808 *
1809 * Governors calling this function must guarantee that it will never be invoked
1810 * twice in parallel for the same policy and that it will never be called in
1811 * parallel with either ->target() or ->target_index() for the same policy.
1812 *
1813 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1814 * callback to indicate an error condition, the hardware configuration must be
1815 * preserved.
1816 */
1817 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1818 unsigned int target_freq)
1819 {
1820 target_freq = clamp_val(target_freq, policy->min, policy->max);
1821
1822 return cpufreq_driver->fast_switch(policy, target_freq);
1823 }
1824 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1825
1826 /* Must set freqs->new to intermediate frequency */
1827 static int __target_intermediate(struct cpufreq_policy *policy,
1828 struct cpufreq_freqs *freqs, int index)
1829 {
1830 int ret;
1831
1832 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1833
1834 /* We don't need to switch to intermediate freq */
1835 if (!freqs->new)
1836 return 0;
1837
1838 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1839 __func__, policy->cpu, freqs->old, freqs->new);
1840
1841 cpufreq_freq_transition_begin(policy, freqs);
1842 ret = cpufreq_driver->target_intermediate(policy, index);
1843 cpufreq_freq_transition_end(policy, freqs, ret);
1844
1845 if (ret)
1846 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1847 __func__, ret);
1848
1849 return ret;
1850 }
1851
1852 static int __target_index(struct cpufreq_policy *policy, int index)
1853 {
1854 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1855 unsigned int intermediate_freq = 0;
1856 unsigned int newfreq = policy->freq_table[index].frequency;
1857 int retval = -EINVAL;
1858 bool notify;
1859
1860 if (newfreq == policy->cur)
1861 return 0;
1862
1863 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1864 if (notify) {
1865 /* Handle switching to intermediate frequency */
1866 if (cpufreq_driver->get_intermediate) {
1867 retval = __target_intermediate(policy, &freqs, index);
1868 if (retval)
1869 return retval;
1870
1871 intermediate_freq = freqs.new;
1872 /* Set old freq to intermediate */
1873 if (intermediate_freq)
1874 freqs.old = freqs.new;
1875 }
1876
1877 freqs.new = newfreq;
1878 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1879 __func__, policy->cpu, freqs.old, freqs.new);
1880
1881 cpufreq_freq_transition_begin(policy, &freqs);
1882 }
1883
1884 retval = cpufreq_driver->target_index(policy, index);
1885 if (retval)
1886 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1887 retval);
1888
1889 if (notify) {
1890 cpufreq_freq_transition_end(policy, &freqs, retval);
1891
1892 /*
1893 * Failed after setting to intermediate freq? Driver should have
1894 * reverted back to initial frequency and so should we. Check
1895 * here for intermediate_freq instead of get_intermediate, in
1896 * case we haven't switched to intermediate freq at all.
1897 */
1898 if (unlikely(retval && intermediate_freq)) {
1899 freqs.old = intermediate_freq;
1900 freqs.new = policy->restore_freq;
1901 cpufreq_freq_transition_begin(policy, &freqs);
1902 cpufreq_freq_transition_end(policy, &freqs, 0);
1903 }
1904 }
1905
1906 return retval;
1907 }
1908
1909 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1910 unsigned int target_freq,
1911 unsigned int relation)
1912 {
1913 unsigned int old_target_freq = target_freq;
1914 int index;
1915
1916 if (cpufreq_disabled())
1917 return -ENODEV;
1918
1919 /* Make sure that target_freq is within supported range */
1920 target_freq = clamp_val(target_freq, policy->min, policy->max);
1921
1922 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1923 policy->cpu, target_freq, relation, old_target_freq);
1924
1925 /*
1926 * This might look like a redundant call as we are checking it again
1927 * after finding index. But it is left intentionally for cases where
1928 * exactly same freq is called again and so we can save on few function
1929 * calls.
1930 */
1931 if (target_freq == policy->cur)
1932 return 0;
1933
1934 /* Save last value to restore later on errors */
1935 policy->restore_freq = policy->cur;
1936
1937 if (cpufreq_driver->target)
1938 return cpufreq_driver->target(policy, target_freq, relation);
1939
1940 if (!cpufreq_driver->target_index)
1941 return -EINVAL;
1942
1943 index = cpufreq_frequency_table_target(policy, target_freq, relation);
1944
1945 return __target_index(policy, index);
1946 }
1947 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1948
1949 int cpufreq_driver_target(struct cpufreq_policy *policy,
1950 unsigned int target_freq,
1951 unsigned int relation)
1952 {
1953 int ret = -EINVAL;
1954
1955 down_write(&policy->rwsem);
1956
1957 ret = __cpufreq_driver_target(policy, target_freq, relation);
1958
1959 up_write(&policy->rwsem);
1960
1961 return ret;
1962 }
1963 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1964
1965 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1966 {
1967 return NULL;
1968 }
1969
1970 static int cpufreq_init_governor(struct cpufreq_policy *policy)
1971 {
1972 int ret;
1973
1974 /* Don't start any governor operations if we are entering suspend */
1975 if (cpufreq_suspended)
1976 return 0;
1977 /*
1978 * Governor might not be initiated here if ACPI _PPC changed
1979 * notification happened, so check it.
1980 */
1981 if (!policy->governor)
1982 return -EINVAL;
1983
1984 if (policy->governor->max_transition_latency &&
1985 policy->cpuinfo.transition_latency >
1986 policy->governor->max_transition_latency) {
1987 struct cpufreq_governor *gov = cpufreq_fallback_governor();
1988
1989 if (gov) {
1990 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
1991 policy->governor->name, gov->name);
1992 policy->governor = gov;
1993 } else {
1994 return -EINVAL;
1995 }
1996 }
1997
1998 if (!try_module_get(policy->governor->owner))
1999 return -EINVAL;
2000
2001 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2002
2003 if (policy->governor->init) {
2004 ret = policy->governor->init(policy);
2005 if (ret) {
2006 module_put(policy->governor->owner);
2007 return ret;
2008 }
2009 }
2010
2011 return 0;
2012 }
2013
2014 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2015 {
2016 if (cpufreq_suspended || !policy->governor)
2017 return;
2018
2019 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2020
2021 if (policy->governor->exit)
2022 policy->governor->exit(policy);
2023
2024 module_put(policy->governor->owner);
2025 }
2026
2027 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2028 {
2029 int ret;
2030
2031 if (cpufreq_suspended)
2032 return 0;
2033
2034 if (!policy->governor)
2035 return -EINVAL;
2036
2037 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2038
2039 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2040 cpufreq_update_current_freq(policy);
2041
2042 if (policy->governor->start) {
2043 ret = policy->governor->start(policy);
2044 if (ret)
2045 return ret;
2046 }
2047
2048 if (policy->governor->limits)
2049 policy->governor->limits(policy);
2050
2051 return 0;
2052 }
2053
2054 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2055 {
2056 if (cpufreq_suspended || !policy->governor)
2057 return;
2058
2059 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2060
2061 if (policy->governor->stop)
2062 policy->governor->stop(policy);
2063 }
2064
2065 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2066 {
2067 if (cpufreq_suspended || !policy->governor)
2068 return;
2069
2070 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2071
2072 if (policy->governor->limits)
2073 policy->governor->limits(policy);
2074 }
2075
2076 int cpufreq_register_governor(struct cpufreq_governor *governor)
2077 {
2078 int err;
2079
2080 if (!governor)
2081 return -EINVAL;
2082
2083 if (cpufreq_disabled())
2084 return -ENODEV;
2085
2086 mutex_lock(&cpufreq_governor_mutex);
2087
2088 err = -EBUSY;
2089 if (!find_governor(governor->name)) {
2090 err = 0;
2091 list_add(&governor->governor_list, &cpufreq_governor_list);
2092 }
2093
2094 mutex_unlock(&cpufreq_governor_mutex);
2095 return err;
2096 }
2097 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2098
2099 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2100 {
2101 struct cpufreq_policy *policy;
2102 unsigned long flags;
2103
2104 if (!governor)
2105 return;
2106
2107 if (cpufreq_disabled())
2108 return;
2109
2110 /* clear last_governor for all inactive policies */
2111 read_lock_irqsave(&cpufreq_driver_lock, flags);
2112 for_each_inactive_policy(policy) {
2113 if (!strcmp(policy->last_governor, governor->name)) {
2114 policy->governor = NULL;
2115 strcpy(policy->last_governor, "\0");
2116 }
2117 }
2118 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2119
2120 mutex_lock(&cpufreq_governor_mutex);
2121 list_del(&governor->governor_list);
2122 mutex_unlock(&cpufreq_governor_mutex);
2123 return;
2124 }
2125 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2126
2127
2128 /*********************************************************************
2129 * POLICY INTERFACE *
2130 *********************************************************************/
2131
2132 /**
2133 * cpufreq_get_policy - get the current cpufreq_policy
2134 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2135 * is written
2136 *
2137 * Reads the current cpufreq policy.
2138 */
2139 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2140 {
2141 struct cpufreq_policy *cpu_policy;
2142 if (!policy)
2143 return -EINVAL;
2144
2145 cpu_policy = cpufreq_cpu_get(cpu);
2146 if (!cpu_policy)
2147 return -EINVAL;
2148
2149 memcpy(policy, cpu_policy, sizeof(*policy));
2150
2151 cpufreq_cpu_put(cpu_policy);
2152 return 0;
2153 }
2154 EXPORT_SYMBOL(cpufreq_get_policy);
2155
2156 /*
2157 * policy : current policy.
2158 * new_policy: policy to be set.
2159 */
2160 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2161 struct cpufreq_policy *new_policy)
2162 {
2163 struct cpufreq_governor *old_gov;
2164 int ret;
2165
2166 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2167 new_policy->cpu, new_policy->min, new_policy->max);
2168
2169 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2170
2171 /*
2172 * This check works well when we store new min/max freq attributes,
2173 * because new_policy is a copy of policy with one field updated.
2174 */
2175 if (new_policy->min > new_policy->max)
2176 return -EINVAL;
2177
2178 /* verify the cpu speed can be set within this limit */
2179 ret = cpufreq_driver->verify(new_policy);
2180 if (ret)
2181 return ret;
2182
2183 /* adjust if necessary - all reasons */
2184 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2185 CPUFREQ_ADJUST, new_policy);
2186
2187 /*
2188 * verify the cpu speed can be set within this limit, which might be
2189 * different to the first one
2190 */
2191 ret = cpufreq_driver->verify(new_policy);
2192 if (ret)
2193 return ret;
2194
2195 /* notification of the new policy */
2196 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2197 CPUFREQ_NOTIFY, new_policy);
2198
2199 policy->min = new_policy->min;
2200 policy->max = new_policy->max;
2201
2202 pr_debug("new min and max freqs are %u - %u kHz\n",
2203 policy->min, policy->max);
2204
2205 if (cpufreq_driver->setpolicy) {
2206 policy->policy = new_policy->policy;
2207 pr_debug("setting range\n");
2208 return cpufreq_driver->setpolicy(new_policy);
2209 }
2210
2211 if (new_policy->governor == policy->governor) {
2212 pr_debug("cpufreq: governor limits update\n");
2213 cpufreq_governor_limits(policy);
2214 return 0;
2215 }
2216
2217 pr_debug("governor switch\n");
2218
2219 /* save old, working values */
2220 old_gov = policy->governor;
2221 /* end old governor */
2222 if (old_gov) {
2223 cpufreq_stop_governor(policy);
2224 cpufreq_exit_governor(policy);
2225 }
2226
2227 /* start new governor */
2228 policy->governor = new_policy->governor;
2229 ret = cpufreq_init_governor(policy);
2230 if (!ret) {
2231 ret = cpufreq_start_governor(policy);
2232 if (!ret) {
2233 pr_debug("cpufreq: governor change\n");
2234 return 0;
2235 }
2236 cpufreq_exit_governor(policy);
2237 }
2238
2239 /* new governor failed, so re-start old one */
2240 pr_debug("starting governor %s failed\n", policy->governor->name);
2241 if (old_gov) {
2242 policy->governor = old_gov;
2243 if (cpufreq_init_governor(policy))
2244 policy->governor = NULL;
2245 else
2246 cpufreq_start_governor(policy);
2247 }
2248
2249 return ret;
2250 }
2251
2252 /**
2253 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2254 * @cpu: CPU which shall be re-evaluated
2255 *
2256 * Useful for policy notifiers which have different necessities
2257 * at different times.
2258 */
2259 int cpufreq_update_policy(unsigned int cpu)
2260 {
2261 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2262 struct cpufreq_policy new_policy;
2263 int ret;
2264
2265 if (!policy)
2266 return -ENODEV;
2267
2268 down_write(&policy->rwsem);
2269
2270 pr_debug("updating policy for CPU %u\n", cpu);
2271 memcpy(&new_policy, policy, sizeof(*policy));
2272 new_policy.min = policy->user_policy.min;
2273 new_policy.max = policy->user_policy.max;
2274
2275 /*
2276 * BIOS might change freq behind our back
2277 * -> ask driver for current freq and notify governors about a change
2278 */
2279 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2280 if (cpufreq_suspended) {
2281 ret = -EAGAIN;
2282 goto unlock;
2283 }
2284 new_policy.cur = cpufreq_update_current_freq(policy);
2285 if (WARN_ON(!new_policy.cur)) {
2286 ret = -EIO;
2287 goto unlock;
2288 }
2289 }
2290
2291 ret = cpufreq_set_policy(policy, &new_policy);
2292
2293 unlock:
2294 up_write(&policy->rwsem);
2295
2296 cpufreq_cpu_put(policy);
2297 return ret;
2298 }
2299 EXPORT_SYMBOL(cpufreq_update_policy);
2300
2301 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2302 unsigned long action, void *hcpu)
2303 {
2304 unsigned int cpu = (unsigned long)hcpu;
2305
2306 switch (action & ~CPU_TASKS_FROZEN) {
2307 case CPU_ONLINE:
2308 case CPU_DOWN_FAILED:
2309 cpufreq_online(cpu);
2310 break;
2311
2312 case CPU_DOWN_PREPARE:
2313 cpufreq_offline(cpu);
2314 break;
2315 }
2316 return NOTIFY_OK;
2317 }
2318
2319 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2320 .notifier_call = cpufreq_cpu_callback,
2321 };
2322
2323 /*********************************************************************
2324 * BOOST *
2325 *********************************************************************/
2326 static int cpufreq_boost_set_sw(int state)
2327 {
2328 struct cpufreq_policy *policy;
2329 int ret = -EINVAL;
2330
2331 for_each_active_policy(policy) {
2332 if (!policy->freq_table)
2333 continue;
2334
2335 ret = cpufreq_frequency_table_cpuinfo(policy,
2336 policy->freq_table);
2337 if (ret) {
2338 pr_err("%s: Policy frequency update failed\n",
2339 __func__);
2340 break;
2341 }
2342
2343 down_write(&policy->rwsem);
2344 policy->user_policy.max = policy->max;
2345 cpufreq_governor_limits(policy);
2346 up_write(&policy->rwsem);
2347 }
2348
2349 return ret;
2350 }
2351
2352 int cpufreq_boost_trigger_state(int state)
2353 {
2354 unsigned long flags;
2355 int ret = 0;
2356
2357 if (cpufreq_driver->boost_enabled == state)
2358 return 0;
2359
2360 write_lock_irqsave(&cpufreq_driver_lock, flags);
2361 cpufreq_driver->boost_enabled = state;
2362 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2363
2364 ret = cpufreq_driver->set_boost(state);
2365 if (ret) {
2366 write_lock_irqsave(&cpufreq_driver_lock, flags);
2367 cpufreq_driver->boost_enabled = !state;
2368 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2369
2370 pr_err("%s: Cannot %s BOOST\n",
2371 __func__, state ? "enable" : "disable");
2372 }
2373
2374 return ret;
2375 }
2376
2377 static bool cpufreq_boost_supported(void)
2378 {
2379 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
2380 }
2381
2382 static int create_boost_sysfs_file(void)
2383 {
2384 int ret;
2385
2386 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2387 if (ret)
2388 pr_err("%s: cannot register global BOOST sysfs file\n",
2389 __func__);
2390
2391 return ret;
2392 }
2393
2394 static void remove_boost_sysfs_file(void)
2395 {
2396 if (cpufreq_boost_supported())
2397 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2398 }
2399
2400 int cpufreq_enable_boost_support(void)
2401 {
2402 if (!cpufreq_driver)
2403 return -EINVAL;
2404
2405 if (cpufreq_boost_supported())
2406 return 0;
2407
2408 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2409
2410 /* This will get removed on driver unregister */
2411 return create_boost_sysfs_file();
2412 }
2413 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2414
2415 int cpufreq_boost_enabled(void)
2416 {
2417 return cpufreq_driver->boost_enabled;
2418 }
2419 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2420
2421 /*********************************************************************
2422 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2423 *********************************************************************/
2424
2425 /**
2426 * cpufreq_register_driver - register a CPU Frequency driver
2427 * @driver_data: A struct cpufreq_driver containing the values#
2428 * submitted by the CPU Frequency driver.
2429 *
2430 * Registers a CPU Frequency driver to this core code. This code
2431 * returns zero on success, -EEXIST when another driver got here first
2432 * (and isn't unregistered in the meantime).
2433 *
2434 */
2435 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2436 {
2437 unsigned long flags;
2438 int ret;
2439
2440 if (cpufreq_disabled())
2441 return -ENODEV;
2442
2443 if (!driver_data || !driver_data->verify || !driver_data->init ||
2444 !(driver_data->setpolicy || driver_data->target_index ||
2445 driver_data->target) ||
2446 (driver_data->setpolicy && (driver_data->target_index ||
2447 driver_data->target)) ||
2448 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
2449 return -EINVAL;
2450
2451 pr_debug("trying to register driver %s\n", driver_data->name);
2452
2453 /* Protect against concurrent CPU online/offline. */
2454 get_online_cpus();
2455
2456 write_lock_irqsave(&cpufreq_driver_lock, flags);
2457 if (cpufreq_driver) {
2458 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2459 ret = -EEXIST;
2460 goto out;
2461 }
2462 cpufreq_driver = driver_data;
2463 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2464
2465 if (driver_data->setpolicy)
2466 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2467
2468 if (cpufreq_boost_supported()) {
2469 ret = create_boost_sysfs_file();
2470 if (ret)
2471 goto err_null_driver;
2472 }
2473
2474 ret = subsys_interface_register(&cpufreq_interface);
2475 if (ret)
2476 goto err_boost_unreg;
2477
2478 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2479 list_empty(&cpufreq_policy_list)) {
2480 /* if all ->init() calls failed, unregister */
2481 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2482 driver_data->name);
2483 goto err_if_unreg;
2484 }
2485
2486 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2487 pr_debug("driver %s up and running\n", driver_data->name);
2488 goto out;
2489
2490 err_if_unreg:
2491 subsys_interface_unregister(&cpufreq_interface);
2492 err_boost_unreg:
2493 remove_boost_sysfs_file();
2494 err_null_driver:
2495 write_lock_irqsave(&cpufreq_driver_lock, flags);
2496 cpufreq_driver = NULL;
2497 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2498 out:
2499 put_online_cpus();
2500 return ret;
2501 }
2502 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2503
2504 /**
2505 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2506 *
2507 * Unregister the current CPUFreq driver. Only call this if you have
2508 * the right to do so, i.e. if you have succeeded in initialising before!
2509 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2510 * currently not initialised.
2511 */
2512 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2513 {
2514 unsigned long flags;
2515
2516 if (!cpufreq_driver || (driver != cpufreq_driver))
2517 return -EINVAL;
2518
2519 pr_debug("unregistering driver %s\n", driver->name);
2520
2521 /* Protect against concurrent cpu hotplug */
2522 get_online_cpus();
2523 subsys_interface_unregister(&cpufreq_interface);
2524 remove_boost_sysfs_file();
2525 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2526
2527 write_lock_irqsave(&cpufreq_driver_lock, flags);
2528
2529 cpufreq_driver = NULL;
2530
2531 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2532 put_online_cpus();
2533
2534 return 0;
2535 }
2536 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2537
2538 /*
2539 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2540 * or mutexes when secondary CPUs are halted.
2541 */
2542 static struct syscore_ops cpufreq_syscore_ops = {
2543 .shutdown = cpufreq_suspend,
2544 };
2545
2546 struct kobject *cpufreq_global_kobject;
2547 EXPORT_SYMBOL(cpufreq_global_kobject);
2548
2549 static int __init cpufreq_core_init(void)
2550 {
2551 if (cpufreq_disabled())
2552 return -ENODEV;
2553
2554 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2555 BUG_ON(!cpufreq_global_kobject);
2556
2557 register_syscore_ops(&cpufreq_syscore_ops);
2558
2559 return 0;
2560 }
2561 core_initcall(cpufreq_core_init);
This page took 0.105835 seconds and 5 git commands to generate.