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