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