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