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