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