Merge back earlier cpufreq material for v4.8.
[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 if (cpufreq_suspended)
1548 return 0;
1549
1550 new_freq = cpufreq_driver->get(policy->cpu);
1551 if (!new_freq)
1552 return 0;
1553
1554 if (!policy->cur) {
1555 pr_debug("cpufreq: Driver did not initialize current freq\n");
1556 policy->cur = new_freq;
1557 } else if (policy->cur != new_freq && has_target()) {
1558 cpufreq_out_of_sync(policy, new_freq);
1559 }
1560
1561 return new_freq;
1562 }
1563
1564 static struct subsys_interface cpufreq_interface = {
1565 .name = "cpufreq",
1566 .subsys = &cpu_subsys,
1567 .add_dev = cpufreq_add_dev,
1568 .remove_dev = cpufreq_remove_dev,
1569 };
1570
1571 /*
1572 * In case platform wants some specific frequency to be configured
1573 * during suspend..
1574 */
1575 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1576 {
1577 int ret;
1578
1579 if (!policy->suspend_freq) {
1580 pr_debug("%s: suspend_freq not defined\n", __func__);
1581 return 0;
1582 }
1583
1584 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1585 policy->suspend_freq);
1586
1587 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1588 CPUFREQ_RELATION_H);
1589 if (ret)
1590 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1591 __func__, policy->suspend_freq, ret);
1592
1593 return ret;
1594 }
1595 EXPORT_SYMBOL(cpufreq_generic_suspend);
1596
1597 /**
1598 * cpufreq_suspend() - Suspend CPUFreq governors
1599 *
1600 * Called during system wide Suspend/Hibernate cycles for suspending governors
1601 * as some platforms can't change frequency after this point in suspend cycle.
1602 * Because some of the devices (like: i2c, regulators, etc) they use for
1603 * changing frequency are suspended quickly after this point.
1604 */
1605 void cpufreq_suspend(void)
1606 {
1607 struct cpufreq_policy *policy;
1608
1609 if (!cpufreq_driver)
1610 return;
1611
1612 if (!has_target() && !cpufreq_driver->suspend)
1613 goto suspend;
1614
1615 pr_debug("%s: Suspending Governors\n", __func__);
1616
1617 for_each_active_policy(policy) {
1618 if (has_target()) {
1619 down_write(&policy->rwsem);
1620 cpufreq_stop_governor(policy);
1621 up_write(&policy->rwsem);
1622 }
1623
1624 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1625 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1626 policy);
1627 }
1628
1629 suspend:
1630 cpufreq_suspended = true;
1631 }
1632
1633 /**
1634 * cpufreq_resume() - Resume CPUFreq governors
1635 *
1636 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1637 * are suspended with cpufreq_suspend().
1638 */
1639 void cpufreq_resume(void)
1640 {
1641 struct cpufreq_policy *policy;
1642 int ret;
1643
1644 if (!cpufreq_driver)
1645 return;
1646
1647 cpufreq_suspended = false;
1648
1649 if (!has_target() && !cpufreq_driver->resume)
1650 return;
1651
1652 pr_debug("%s: Resuming Governors\n", __func__);
1653
1654 for_each_active_policy(policy) {
1655 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1656 pr_err("%s: Failed to resume driver: %p\n", __func__,
1657 policy);
1658 } else if (has_target()) {
1659 down_write(&policy->rwsem);
1660 ret = cpufreq_start_governor(policy);
1661 up_write(&policy->rwsem);
1662
1663 if (ret)
1664 pr_err("%s: Failed to start governor for policy: %p\n",
1665 __func__, policy);
1666 }
1667 }
1668 }
1669
1670 /**
1671 * cpufreq_get_current_driver - return current driver's name
1672 *
1673 * Return the name string of the currently loaded cpufreq driver
1674 * or NULL, if none.
1675 */
1676 const char *cpufreq_get_current_driver(void)
1677 {
1678 if (cpufreq_driver)
1679 return cpufreq_driver->name;
1680
1681 return NULL;
1682 }
1683 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1684
1685 /**
1686 * cpufreq_get_driver_data - return current driver data
1687 *
1688 * Return the private data of the currently loaded cpufreq
1689 * driver, or NULL if no cpufreq driver is loaded.
1690 */
1691 void *cpufreq_get_driver_data(void)
1692 {
1693 if (cpufreq_driver)
1694 return cpufreq_driver->driver_data;
1695
1696 return NULL;
1697 }
1698 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1699
1700 /*********************************************************************
1701 * NOTIFIER LISTS INTERFACE *
1702 *********************************************************************/
1703
1704 /**
1705 * cpufreq_register_notifier - register a driver with cpufreq
1706 * @nb: notifier function to register
1707 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1708 *
1709 * Add a driver to one of two lists: either a list of drivers that
1710 * are notified about clock rate changes (once before and once after
1711 * the transition), or a list of drivers that are notified about
1712 * changes in cpufreq policy.
1713 *
1714 * This function may sleep, and has the same return conditions as
1715 * blocking_notifier_chain_register.
1716 */
1717 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1718 {
1719 int ret;
1720
1721 if (cpufreq_disabled())
1722 return -EINVAL;
1723
1724 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1725
1726 switch (list) {
1727 case CPUFREQ_TRANSITION_NOTIFIER:
1728 mutex_lock(&cpufreq_fast_switch_lock);
1729
1730 if (cpufreq_fast_switch_count > 0) {
1731 mutex_unlock(&cpufreq_fast_switch_lock);
1732 return -EBUSY;
1733 }
1734 ret = srcu_notifier_chain_register(
1735 &cpufreq_transition_notifier_list, nb);
1736 if (!ret)
1737 cpufreq_fast_switch_count--;
1738
1739 mutex_unlock(&cpufreq_fast_switch_lock);
1740 break;
1741 case CPUFREQ_POLICY_NOTIFIER:
1742 ret = blocking_notifier_chain_register(
1743 &cpufreq_policy_notifier_list, nb);
1744 break;
1745 default:
1746 ret = -EINVAL;
1747 }
1748
1749 return ret;
1750 }
1751 EXPORT_SYMBOL(cpufreq_register_notifier);
1752
1753 /**
1754 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1755 * @nb: notifier block to be unregistered
1756 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1757 *
1758 * Remove a driver from the CPU frequency notifier list.
1759 *
1760 * This function may sleep, and has the same return conditions as
1761 * blocking_notifier_chain_unregister.
1762 */
1763 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1764 {
1765 int ret;
1766
1767 if (cpufreq_disabled())
1768 return -EINVAL;
1769
1770 switch (list) {
1771 case CPUFREQ_TRANSITION_NOTIFIER:
1772 mutex_lock(&cpufreq_fast_switch_lock);
1773
1774 ret = srcu_notifier_chain_unregister(
1775 &cpufreq_transition_notifier_list, nb);
1776 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1777 cpufreq_fast_switch_count++;
1778
1779 mutex_unlock(&cpufreq_fast_switch_lock);
1780 break;
1781 case CPUFREQ_POLICY_NOTIFIER:
1782 ret = blocking_notifier_chain_unregister(
1783 &cpufreq_policy_notifier_list, nb);
1784 break;
1785 default:
1786 ret = -EINVAL;
1787 }
1788
1789 return ret;
1790 }
1791 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1792
1793
1794 /*********************************************************************
1795 * GOVERNORS *
1796 *********************************************************************/
1797
1798 /**
1799 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1800 * @policy: cpufreq policy to switch the frequency for.
1801 * @target_freq: New frequency to set (may be approximate).
1802 *
1803 * Carry out a fast frequency switch without sleeping.
1804 *
1805 * The driver's ->fast_switch() callback invoked by this function must be
1806 * suitable for being called from within RCU-sched read-side critical sections
1807 * and it is expected to select the minimum available frequency greater than or
1808 * equal to @target_freq (CPUFREQ_RELATION_L).
1809 *
1810 * This function must not be called if policy->fast_switch_enabled is unset.
1811 *
1812 * Governors calling this function must guarantee that it will never be invoked
1813 * twice in parallel for the same policy and that it will never be called in
1814 * parallel with either ->target() or ->target_index() for the same policy.
1815 *
1816 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1817 * callback to indicate an error condition, the hardware configuration must be
1818 * preserved.
1819 */
1820 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1821 unsigned int target_freq)
1822 {
1823 target_freq = clamp_val(target_freq, policy->min, policy->max);
1824
1825 return cpufreq_driver->fast_switch(policy, target_freq);
1826 }
1827 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1828
1829 /* Must set freqs->new to intermediate frequency */
1830 static int __target_intermediate(struct cpufreq_policy *policy,
1831 struct cpufreq_freqs *freqs, int index)
1832 {
1833 int ret;
1834
1835 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1836
1837 /* We don't need to switch to intermediate freq */
1838 if (!freqs->new)
1839 return 0;
1840
1841 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1842 __func__, policy->cpu, freqs->old, freqs->new);
1843
1844 cpufreq_freq_transition_begin(policy, freqs);
1845 ret = cpufreq_driver->target_intermediate(policy, index);
1846 cpufreq_freq_transition_end(policy, freqs, ret);
1847
1848 if (ret)
1849 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1850 __func__, ret);
1851
1852 return ret;
1853 }
1854
1855 static int __target_index(struct cpufreq_policy *policy, int index)
1856 {
1857 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1858 unsigned int intermediate_freq = 0;
1859 unsigned int newfreq = policy->freq_table[index].frequency;
1860 int retval = -EINVAL;
1861 bool notify;
1862
1863 if (newfreq == policy->cur)
1864 return 0;
1865
1866 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1867 if (notify) {
1868 /* Handle switching to intermediate frequency */
1869 if (cpufreq_driver->get_intermediate) {
1870 retval = __target_intermediate(policy, &freqs, index);
1871 if (retval)
1872 return retval;
1873
1874 intermediate_freq = freqs.new;
1875 /* Set old freq to intermediate */
1876 if (intermediate_freq)
1877 freqs.old = freqs.new;
1878 }
1879
1880 freqs.new = newfreq;
1881 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1882 __func__, policy->cpu, freqs.old, freqs.new);
1883
1884 cpufreq_freq_transition_begin(policy, &freqs);
1885 }
1886
1887 retval = cpufreq_driver->target_index(policy, index);
1888 if (retval)
1889 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1890 retval);
1891
1892 if (notify) {
1893 cpufreq_freq_transition_end(policy, &freqs, retval);
1894
1895 /*
1896 * Failed after setting to intermediate freq? Driver should have
1897 * reverted back to initial frequency and so should we. Check
1898 * here for intermediate_freq instead of get_intermediate, in
1899 * case we haven't switched to intermediate freq at all.
1900 */
1901 if (unlikely(retval && intermediate_freq)) {
1902 freqs.old = intermediate_freq;
1903 freqs.new = policy->restore_freq;
1904 cpufreq_freq_transition_begin(policy, &freqs);
1905 cpufreq_freq_transition_end(policy, &freqs, 0);
1906 }
1907 }
1908
1909 return retval;
1910 }
1911
1912 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1913 unsigned int target_freq,
1914 unsigned int relation)
1915 {
1916 unsigned int old_target_freq = target_freq;
1917 int index;
1918
1919 if (cpufreq_disabled())
1920 return -ENODEV;
1921
1922 /* Make sure that target_freq is within supported range */
1923 target_freq = clamp_val(target_freq, policy->min, policy->max);
1924
1925 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1926 policy->cpu, target_freq, relation, old_target_freq);
1927
1928 /*
1929 * This might look like a redundant call as we are checking it again
1930 * after finding index. But it is left intentionally for cases where
1931 * exactly same freq is called again and so we can save on few function
1932 * calls.
1933 */
1934 if (target_freq == policy->cur)
1935 return 0;
1936
1937 /* Save last value to restore later on errors */
1938 policy->restore_freq = policy->cur;
1939
1940 if (cpufreq_driver->target)
1941 return cpufreq_driver->target(policy, target_freq, relation);
1942
1943 if (!cpufreq_driver->target_index)
1944 return -EINVAL;
1945
1946 index = cpufreq_frequency_table_target(policy, target_freq, relation);
1947
1948 return __target_index(policy, index);
1949 }
1950 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1951
1952 int cpufreq_driver_target(struct cpufreq_policy *policy,
1953 unsigned int target_freq,
1954 unsigned int relation)
1955 {
1956 int ret = -EINVAL;
1957
1958 down_write(&policy->rwsem);
1959
1960 ret = __cpufreq_driver_target(policy, target_freq, relation);
1961
1962 up_write(&policy->rwsem);
1963
1964 return ret;
1965 }
1966 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1967
1968 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1969 {
1970 return NULL;
1971 }
1972
1973 static int cpufreq_init_governor(struct cpufreq_policy *policy)
1974 {
1975 int ret;
1976
1977 /* Don't start any governor operations if we are entering suspend */
1978 if (cpufreq_suspended)
1979 return 0;
1980 /*
1981 * Governor might not be initiated here if ACPI _PPC changed
1982 * notification happened, so check it.
1983 */
1984 if (!policy->governor)
1985 return -EINVAL;
1986
1987 if (policy->governor->max_transition_latency &&
1988 policy->cpuinfo.transition_latency >
1989 policy->governor->max_transition_latency) {
1990 struct cpufreq_governor *gov = cpufreq_fallback_governor();
1991
1992 if (gov) {
1993 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
1994 policy->governor->name, gov->name);
1995 policy->governor = gov;
1996 } else {
1997 return -EINVAL;
1998 }
1999 }
2000
2001 if (!try_module_get(policy->governor->owner))
2002 return -EINVAL;
2003
2004 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2005
2006 if (policy->governor->init) {
2007 ret = policy->governor->init(policy);
2008 if (ret) {
2009 module_put(policy->governor->owner);
2010 return ret;
2011 }
2012 }
2013
2014 return 0;
2015 }
2016
2017 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2018 {
2019 if (cpufreq_suspended || !policy->governor)
2020 return;
2021
2022 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2023
2024 if (policy->governor->exit)
2025 policy->governor->exit(policy);
2026
2027 module_put(policy->governor->owner);
2028 }
2029
2030 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2031 {
2032 int ret;
2033
2034 if (cpufreq_suspended)
2035 return 0;
2036
2037 if (!policy->governor)
2038 return -EINVAL;
2039
2040 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2041
2042 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2043 cpufreq_update_current_freq(policy);
2044
2045 if (policy->governor->start) {
2046 ret = policy->governor->start(policy);
2047 if (ret)
2048 return ret;
2049 }
2050
2051 if (policy->governor->limits)
2052 policy->governor->limits(policy);
2053
2054 return 0;
2055 }
2056
2057 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2058 {
2059 if (cpufreq_suspended || !policy->governor)
2060 return;
2061
2062 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2063
2064 if (policy->governor->stop)
2065 policy->governor->stop(policy);
2066 }
2067
2068 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2069 {
2070 if (cpufreq_suspended || !policy->governor)
2071 return;
2072
2073 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2074
2075 if (policy->governor->limits)
2076 policy->governor->limits(policy);
2077 }
2078
2079 int cpufreq_register_governor(struct cpufreq_governor *governor)
2080 {
2081 int err;
2082
2083 if (!governor)
2084 return -EINVAL;
2085
2086 if (cpufreq_disabled())
2087 return -ENODEV;
2088
2089 mutex_lock(&cpufreq_governor_mutex);
2090
2091 err = -EBUSY;
2092 if (!find_governor(governor->name)) {
2093 err = 0;
2094 list_add(&governor->governor_list, &cpufreq_governor_list);
2095 }
2096
2097 mutex_unlock(&cpufreq_governor_mutex);
2098 return err;
2099 }
2100 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2101
2102 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2103 {
2104 struct cpufreq_policy *policy;
2105 unsigned long flags;
2106
2107 if (!governor)
2108 return;
2109
2110 if (cpufreq_disabled())
2111 return;
2112
2113 /* clear last_governor for all inactive policies */
2114 read_lock_irqsave(&cpufreq_driver_lock, flags);
2115 for_each_inactive_policy(policy) {
2116 if (!strcmp(policy->last_governor, governor->name)) {
2117 policy->governor = NULL;
2118 strcpy(policy->last_governor, "\0");
2119 }
2120 }
2121 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2122
2123 mutex_lock(&cpufreq_governor_mutex);
2124 list_del(&governor->governor_list);
2125 mutex_unlock(&cpufreq_governor_mutex);
2126 return;
2127 }
2128 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2129
2130
2131 /*********************************************************************
2132 * POLICY INTERFACE *
2133 *********************************************************************/
2134
2135 /**
2136 * cpufreq_get_policy - get the current cpufreq_policy
2137 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2138 * is written
2139 *
2140 * Reads the current cpufreq policy.
2141 */
2142 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2143 {
2144 struct cpufreq_policy *cpu_policy;
2145 if (!policy)
2146 return -EINVAL;
2147
2148 cpu_policy = cpufreq_cpu_get(cpu);
2149 if (!cpu_policy)
2150 return -EINVAL;
2151
2152 memcpy(policy, cpu_policy, sizeof(*policy));
2153
2154 cpufreq_cpu_put(cpu_policy);
2155 return 0;
2156 }
2157 EXPORT_SYMBOL(cpufreq_get_policy);
2158
2159 /*
2160 * policy : current policy.
2161 * new_policy: policy to be set.
2162 */
2163 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2164 struct cpufreq_policy *new_policy)
2165 {
2166 struct cpufreq_governor *old_gov;
2167 int ret;
2168
2169 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2170 new_policy->cpu, new_policy->min, new_policy->max);
2171
2172 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2173
2174 /*
2175 * This check works well when we store new min/max freq attributes,
2176 * because new_policy is a copy of policy with one field updated.
2177 */
2178 if (new_policy->min > new_policy->max)
2179 return -EINVAL;
2180
2181 /* verify the cpu speed can be set within this limit */
2182 ret = cpufreq_driver->verify(new_policy);
2183 if (ret)
2184 return ret;
2185
2186 /* adjust if necessary - all reasons */
2187 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2188 CPUFREQ_ADJUST, new_policy);
2189
2190 /*
2191 * verify the cpu speed can be set within this limit, which might be
2192 * different to the first one
2193 */
2194 ret = cpufreq_driver->verify(new_policy);
2195 if (ret)
2196 return ret;
2197
2198 /* notification of the new policy */
2199 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2200 CPUFREQ_NOTIFY, new_policy);
2201
2202 policy->min = new_policy->min;
2203 policy->max = new_policy->max;
2204
2205 pr_debug("new min and max freqs are %u - %u kHz\n",
2206 policy->min, policy->max);
2207
2208 if (cpufreq_driver->setpolicy) {
2209 policy->policy = new_policy->policy;
2210 pr_debug("setting range\n");
2211 return cpufreq_driver->setpolicy(new_policy);
2212 }
2213
2214 if (new_policy->governor == policy->governor) {
2215 pr_debug("cpufreq: governor limits update\n");
2216 cpufreq_governor_limits(policy);
2217 return 0;
2218 }
2219
2220 pr_debug("governor switch\n");
2221
2222 /* save old, working values */
2223 old_gov = policy->governor;
2224 /* end old governor */
2225 if (old_gov) {
2226 cpufreq_stop_governor(policy);
2227 cpufreq_exit_governor(policy);
2228 }
2229
2230 /* start new governor */
2231 policy->governor = new_policy->governor;
2232 ret = cpufreq_init_governor(policy);
2233 if (!ret) {
2234 ret = cpufreq_start_governor(policy);
2235 if (!ret) {
2236 pr_debug("cpufreq: governor change\n");
2237 return 0;
2238 }
2239 cpufreq_exit_governor(policy);
2240 }
2241
2242 /* new governor failed, so re-start old one */
2243 pr_debug("starting governor %s failed\n", policy->governor->name);
2244 if (old_gov) {
2245 policy->governor = old_gov;
2246 if (cpufreq_init_governor(policy))
2247 policy->governor = NULL;
2248 else
2249 cpufreq_start_governor(policy);
2250 }
2251
2252 return ret;
2253 }
2254
2255 /**
2256 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2257 * @cpu: CPU which shall be re-evaluated
2258 *
2259 * Useful for policy notifiers which have different necessities
2260 * at different times.
2261 */
2262 int cpufreq_update_policy(unsigned int cpu)
2263 {
2264 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2265 struct cpufreq_policy new_policy;
2266 int ret;
2267
2268 if (!policy)
2269 return -ENODEV;
2270
2271 down_write(&policy->rwsem);
2272
2273 pr_debug("updating policy for CPU %u\n", cpu);
2274 memcpy(&new_policy, policy, sizeof(*policy));
2275 new_policy.min = policy->user_policy.min;
2276 new_policy.max = policy->user_policy.max;
2277
2278 /*
2279 * BIOS might change freq behind our back
2280 * -> ask driver for current freq and notify governors about a change
2281 */
2282 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2283 if (cpufreq_suspended) {
2284 ret = -EAGAIN;
2285 goto unlock;
2286 }
2287 new_policy.cur = cpufreq_update_current_freq(policy);
2288 if (WARN_ON(!new_policy.cur)) {
2289 ret = -EIO;
2290 goto unlock;
2291 }
2292 }
2293
2294 ret = cpufreq_set_policy(policy, &new_policy);
2295
2296 unlock:
2297 up_write(&policy->rwsem);
2298
2299 cpufreq_cpu_put(policy);
2300 return ret;
2301 }
2302 EXPORT_SYMBOL(cpufreq_update_policy);
2303
2304 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2305 unsigned long action, void *hcpu)
2306 {
2307 unsigned int cpu = (unsigned long)hcpu;
2308
2309 switch (action & ~CPU_TASKS_FROZEN) {
2310 case CPU_ONLINE:
2311 case CPU_DOWN_FAILED:
2312 cpufreq_online(cpu);
2313 break;
2314
2315 case CPU_DOWN_PREPARE:
2316 cpufreq_offline(cpu);
2317 break;
2318 }
2319 return NOTIFY_OK;
2320 }
2321
2322 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2323 .notifier_call = cpufreq_cpu_callback,
2324 };
2325
2326 /*********************************************************************
2327 * BOOST *
2328 *********************************************************************/
2329 static int cpufreq_boost_set_sw(int state)
2330 {
2331 struct cpufreq_policy *policy;
2332 int ret = -EINVAL;
2333
2334 for_each_active_policy(policy) {
2335 if (!policy->freq_table)
2336 continue;
2337
2338 ret = cpufreq_frequency_table_cpuinfo(policy,
2339 policy->freq_table);
2340 if (ret) {
2341 pr_err("%s: Policy frequency update failed\n",
2342 __func__);
2343 break;
2344 }
2345
2346 down_write(&policy->rwsem);
2347 policy->user_policy.max = policy->max;
2348 cpufreq_governor_limits(policy);
2349 up_write(&policy->rwsem);
2350 }
2351
2352 return ret;
2353 }
2354
2355 int cpufreq_boost_trigger_state(int state)
2356 {
2357 unsigned long flags;
2358 int ret = 0;
2359
2360 if (cpufreq_driver->boost_enabled == state)
2361 return 0;
2362
2363 write_lock_irqsave(&cpufreq_driver_lock, flags);
2364 cpufreq_driver->boost_enabled = state;
2365 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2366
2367 ret = cpufreq_driver->set_boost(state);
2368 if (ret) {
2369 write_lock_irqsave(&cpufreq_driver_lock, flags);
2370 cpufreq_driver->boost_enabled = !state;
2371 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2372
2373 pr_err("%s: Cannot %s BOOST\n",
2374 __func__, state ? "enable" : "disable");
2375 }
2376
2377 return ret;
2378 }
2379
2380 static bool cpufreq_boost_supported(void)
2381 {
2382 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
2383 }
2384
2385 static int create_boost_sysfs_file(void)
2386 {
2387 int ret;
2388
2389 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2390 if (ret)
2391 pr_err("%s: cannot register global BOOST sysfs file\n",
2392 __func__);
2393
2394 return ret;
2395 }
2396
2397 static void remove_boost_sysfs_file(void)
2398 {
2399 if (cpufreq_boost_supported())
2400 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2401 }
2402
2403 int cpufreq_enable_boost_support(void)
2404 {
2405 if (!cpufreq_driver)
2406 return -EINVAL;
2407
2408 if (cpufreq_boost_supported())
2409 return 0;
2410
2411 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2412
2413 /* This will get removed on driver unregister */
2414 return create_boost_sysfs_file();
2415 }
2416 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2417
2418 int cpufreq_boost_enabled(void)
2419 {
2420 return cpufreq_driver->boost_enabled;
2421 }
2422 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2423
2424 /*********************************************************************
2425 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2426 *********************************************************************/
2427
2428 /**
2429 * cpufreq_register_driver - register a CPU Frequency driver
2430 * @driver_data: A struct cpufreq_driver containing the values#
2431 * submitted by the CPU Frequency driver.
2432 *
2433 * Registers a CPU Frequency driver to this core code. This code
2434 * returns zero on success, -EEXIST when another driver got here first
2435 * (and isn't unregistered in the meantime).
2436 *
2437 */
2438 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2439 {
2440 unsigned long flags;
2441 int ret;
2442
2443 if (cpufreq_disabled())
2444 return -ENODEV;
2445
2446 if (!driver_data || !driver_data->verify || !driver_data->init ||
2447 !(driver_data->setpolicy || driver_data->target_index ||
2448 driver_data->target) ||
2449 (driver_data->setpolicy && (driver_data->target_index ||
2450 driver_data->target)) ||
2451 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
2452 return -EINVAL;
2453
2454 pr_debug("trying to register driver %s\n", driver_data->name);
2455
2456 /* Protect against concurrent CPU online/offline. */
2457 get_online_cpus();
2458
2459 write_lock_irqsave(&cpufreq_driver_lock, flags);
2460 if (cpufreq_driver) {
2461 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2462 ret = -EEXIST;
2463 goto out;
2464 }
2465 cpufreq_driver = driver_data;
2466 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2467
2468 if (driver_data->setpolicy)
2469 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2470
2471 if (cpufreq_boost_supported()) {
2472 ret = create_boost_sysfs_file();
2473 if (ret)
2474 goto err_null_driver;
2475 }
2476
2477 ret = subsys_interface_register(&cpufreq_interface);
2478 if (ret)
2479 goto err_boost_unreg;
2480
2481 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2482 list_empty(&cpufreq_policy_list)) {
2483 /* if all ->init() calls failed, unregister */
2484 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2485 driver_data->name);
2486 goto err_if_unreg;
2487 }
2488
2489 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2490 pr_debug("driver %s up and running\n", driver_data->name);
2491 goto out;
2492
2493 err_if_unreg:
2494 subsys_interface_unregister(&cpufreq_interface);
2495 err_boost_unreg:
2496 remove_boost_sysfs_file();
2497 err_null_driver:
2498 write_lock_irqsave(&cpufreq_driver_lock, flags);
2499 cpufreq_driver = NULL;
2500 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2501 out:
2502 put_online_cpus();
2503 return ret;
2504 }
2505 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2506
2507 /**
2508 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2509 *
2510 * Unregister the current CPUFreq driver. Only call this if you have
2511 * the right to do so, i.e. if you have succeeded in initialising before!
2512 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2513 * currently not initialised.
2514 */
2515 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2516 {
2517 unsigned long flags;
2518
2519 if (!cpufreq_driver || (driver != cpufreq_driver))
2520 return -EINVAL;
2521
2522 pr_debug("unregistering driver %s\n", driver->name);
2523
2524 /* Protect against concurrent cpu hotplug */
2525 get_online_cpus();
2526 subsys_interface_unregister(&cpufreq_interface);
2527 remove_boost_sysfs_file();
2528 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2529
2530 write_lock_irqsave(&cpufreq_driver_lock, flags);
2531
2532 cpufreq_driver = NULL;
2533
2534 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2535 put_online_cpus();
2536
2537 return 0;
2538 }
2539 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2540
2541 /*
2542 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2543 * or mutexes when secondary CPUs are halted.
2544 */
2545 static struct syscore_ops cpufreq_syscore_ops = {
2546 .shutdown = cpufreq_suspend,
2547 };
2548
2549 struct kobject *cpufreq_global_kobject;
2550 EXPORT_SYMBOL(cpufreq_global_kobject);
2551
2552 static int __init cpufreq_core_init(void)
2553 {
2554 if (cpufreq_disabled())
2555 return -ENODEV;
2556
2557 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2558 BUG_ON(!cpufreq_global_kobject);
2559
2560 register_syscore_ops(&cpufreq_syscore_ops);
2561
2562 return 0;
2563 }
2564 core_initcall(cpufreq_core_init);
This page took 0.110292 seconds and 6 git commands to generate.