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