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