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