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 <[email protected]> 7 * (C) 2013 Viresh Kumar <[email protected]> 8 * 9 * Oct 2005 - Ashok Raj <[email protected]> 10 * Added handling for CPU hotplug 11 * Feb 2006 - Jacob Shin <[email protected]> 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 int arch_freq_get_on_cpu(int cpu) 733 { 734 return -EOPNOTSUPP; 735 } 736 737 static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *policy) 738 { 739 return arch_freq_get_on_cpu(policy->cpu) != -EOPNOTSUPP; 740 } 741 742 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 743 { 744 ssize_t ret; 745 int freq; 746 747 freq = IS_ENABLED(CONFIG_CPUFREQ_ARCH_CUR_FREQ) 748 ? arch_freq_get_on_cpu(policy->cpu) 749 : 0; 750 751 if (freq > 0) 752 ret = sysfs_emit(buf, "%u\n", freq); 753 else if (cpufreq_driver->setpolicy && cpufreq_driver->get) 754 ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 755 else 756 ret = sysfs_emit(buf, "%u\n", policy->cur); 757 return ret; 758 } 759 760 /* 761 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 762 */ 763 #define store_one(file_name, object) \ 764 static ssize_t store_##file_name \ 765 (struct cpufreq_policy *policy, const char *buf, size_t count) \ 766 { \ 767 unsigned long val; \ 768 int ret; \ 769 \ 770 ret = kstrtoul(buf, 0, &val); \ 771 if (ret) \ 772 return ret; \ 773 \ 774 ret = freq_qos_update_request(policy->object##_freq_req, val);\ 775 return ret >= 0 ? count : ret; \ 776 } 777 778 store_one(scaling_min_freq, min); 779 store_one(scaling_max_freq, max); 780 781 /* 782 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 783 */ 784 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 785 char *buf) 786 { 787 unsigned int cur_freq = __cpufreq_get(policy); 788 789 if (cur_freq) 790 return sysfs_emit(buf, "%u\n", cur_freq); 791 792 return sysfs_emit(buf, "<unknown>\n"); 793 } 794 795 /* 796 * show_cpuinfo_avg_freq - average CPU frequency as detected by hardware 797 */ 798 static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy, 799 char *buf) 800 { 801 int avg_freq = arch_freq_get_on_cpu(policy->cpu); 802 803 if (avg_freq > 0) 804 return sysfs_emit(buf, "%u\n", avg_freq); 805 return avg_freq != 0 ? avg_freq : -EINVAL; 806 } 807 808 /* 809 * show_scaling_governor - show the current policy for the specified CPU 810 */ 811 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 812 { 813 if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 814 return sysfs_emit(buf, "powersave\n"); 815 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 816 return sysfs_emit(buf, "performance\n"); 817 else if (policy->governor) 818 return sysfs_emit(buf, "%s\n", policy->governor->name); 819 return -EINVAL; 820 } 821 822 /* 823 * store_scaling_governor - store policy for the specified CPU 824 */ 825 static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 826 const char *buf, size_t count) 827 { 828 char str_governor[16]; 829 int ret; 830 831 ret = sscanf(buf, "%15s", str_governor); 832 if (ret != 1) 833 return -EINVAL; 834 835 if (cpufreq_driver->setpolicy) { 836 unsigned int new_pol; 837 838 new_pol = cpufreq_parse_policy(str_governor); 839 if (!new_pol) 840 return -EINVAL; 841 842 ret = cpufreq_set_policy(policy, NULL, new_pol); 843 } else { 844 struct cpufreq_governor *new_gov; 845 846 new_gov = cpufreq_parse_governor(str_governor); 847 if (!new_gov) 848 return -EINVAL; 849 850 ret = cpufreq_set_policy(policy, new_gov, 851 CPUFREQ_POLICY_UNKNOWN); 852 853 module_put(new_gov->owner); 854 } 855 856 return ret ? ret : count; 857 } 858 859 /* 860 * show_scaling_driver - show the cpufreq driver currently loaded 861 */ 862 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 863 { 864 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 865 } 866 867 /* 868 * show_scaling_available_governors - show the available CPUfreq governors 869 */ 870 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 871 char *buf) 872 { 873 ssize_t i = 0; 874 struct cpufreq_governor *t; 875 876 if (!has_target()) { 877 i += sysfs_emit(buf, "performance powersave"); 878 goto out; 879 } 880 881 mutex_lock(&cpufreq_governor_mutex); 882 for_each_governor(t) { 883 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 884 - (CPUFREQ_NAME_LEN + 2))) 885 break; 886 i += sysfs_emit_at(buf, i, "%s ", t->name); 887 } 888 mutex_unlock(&cpufreq_governor_mutex); 889 out: 890 i += sysfs_emit_at(buf, i, "\n"); 891 return i; 892 } 893 894 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 895 { 896 ssize_t i = 0; 897 unsigned int cpu; 898 899 for_each_cpu(cpu, mask) { 900 i += sysfs_emit_at(buf, i, "%u ", cpu); 901 if (i >= (PAGE_SIZE - 5)) 902 break; 903 } 904 905 /* Remove the extra space at the end */ 906 i--; 907 908 i += sysfs_emit_at(buf, i, "\n"); 909 return i; 910 } 911 EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 912 913 /* 914 * show_related_cpus - show the CPUs affected by each transition even if 915 * hw coordination is in use 916 */ 917 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 918 { 919 return cpufreq_show_cpus(policy->related_cpus, buf); 920 } 921 922 /* 923 * show_affected_cpus - show the CPUs affected by each transition 924 */ 925 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 926 { 927 return cpufreq_show_cpus(policy->cpus, buf); 928 } 929 930 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 931 const char *buf, size_t count) 932 { 933 unsigned int freq = 0; 934 unsigned int ret; 935 936 if (!policy->governor || !policy->governor->store_setspeed) 937 return -EINVAL; 938 939 ret = sscanf(buf, "%u", &freq); 940 if (ret != 1) 941 return -EINVAL; 942 943 policy->governor->store_setspeed(policy, freq); 944 945 return count; 946 } 947 948 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 949 { 950 if (!policy->governor || !policy->governor->show_setspeed) 951 return sysfs_emit(buf, "<unsupported>\n"); 952 953 return policy->governor->show_setspeed(policy, buf); 954 } 955 956 /* 957 * show_bios_limit - show the current cpufreq HW/BIOS limitation 958 */ 959 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 960 { 961 unsigned int limit; 962 int ret; 963 ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 964 if (!ret) 965 return sysfs_emit(buf, "%u\n", limit); 966 return sysfs_emit(buf, "%u\n", policy->cpuinfo.max_freq); 967 } 968 969 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 970 cpufreq_freq_attr_ro(cpuinfo_avg_freq); 971 cpufreq_freq_attr_ro(cpuinfo_min_freq); 972 cpufreq_freq_attr_ro(cpuinfo_max_freq); 973 cpufreq_freq_attr_ro(cpuinfo_transition_latency); 974 cpufreq_freq_attr_ro(scaling_available_governors); 975 cpufreq_freq_attr_ro(scaling_driver); 976 cpufreq_freq_attr_ro(scaling_cur_freq); 977 cpufreq_freq_attr_ro(bios_limit); 978 cpufreq_freq_attr_ro(related_cpus); 979 cpufreq_freq_attr_ro(affected_cpus); 980 cpufreq_freq_attr_rw(scaling_min_freq); 981 cpufreq_freq_attr_rw(scaling_max_freq); 982 cpufreq_freq_attr_rw(scaling_governor); 983 cpufreq_freq_attr_rw(scaling_setspeed); 984 985 static struct attribute *cpufreq_attrs[] = { 986 &cpuinfo_min_freq.attr, 987 &cpuinfo_max_freq.attr, 988 &cpuinfo_transition_latency.attr, 989 &scaling_min_freq.attr, 990 &scaling_max_freq.attr, 991 &affected_cpus.attr, 992 &related_cpus.attr, 993 &scaling_governor.attr, 994 &scaling_driver.attr, 995 &scaling_available_governors.attr, 996 &scaling_setspeed.attr, 997 NULL 998 }; 999 ATTRIBUTE_GROUPS(cpufreq); 1000 1001 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 1002 #define to_attr(a) container_of(a, struct freq_attr, attr) 1003 1004 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 1005 { 1006 struct cpufreq_policy *policy = to_policy(kobj); 1007 struct freq_attr *fattr = to_attr(attr); 1008 ssize_t ret = -EBUSY; 1009 1010 if (!fattr->show) 1011 return -EIO; 1012 1013 down_read(&policy->rwsem); 1014 if (likely(!policy_is_inactive(policy))) 1015 ret = fattr->show(policy, buf); 1016 up_read(&policy->rwsem); 1017 1018 return ret; 1019 } 1020 1021 static ssize_t store(struct kobject *kobj, struct attribute *attr, 1022 const char *buf, size_t count) 1023 { 1024 struct cpufreq_policy *policy = to_policy(kobj); 1025 struct freq_attr *fattr = to_attr(attr); 1026 ssize_t ret = -EBUSY; 1027 1028 if (!fattr->store) 1029 return -EIO; 1030 1031 down_write(&policy->rwsem); 1032 if (likely(!policy_is_inactive(policy))) 1033 ret = fattr->store(policy, buf, count); 1034 up_write(&policy->rwsem); 1035 1036 return ret; 1037 } 1038 1039 static void cpufreq_sysfs_release(struct kobject *kobj) 1040 { 1041 struct cpufreq_policy *policy = to_policy(kobj); 1042 pr_debug("last reference is dropped\n"); 1043 complete(&policy->kobj_unregister); 1044 } 1045 1046 static const struct sysfs_ops sysfs_ops = { 1047 .show = show, 1048 .store = store, 1049 }; 1050 1051 static const struct kobj_type ktype_cpufreq = { 1052 .sysfs_ops = &sysfs_ops, 1053 .default_groups = cpufreq_groups, 1054 .release = cpufreq_sysfs_release, 1055 }; 1056 1057 static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu, 1058 struct device *dev) 1059 { 1060 if (unlikely(!dev)) 1061 return; 1062 1063 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 1064 return; 1065 1066 dev_dbg(dev, "%s: Adding symlink\n", __func__); 1067 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 1068 dev_err(dev, "cpufreq symlink creation failed\n"); 1069 } 1070 1071 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu, 1072 struct device *dev) 1073 { 1074 dev_dbg(dev, "%s: Removing symlink\n", __func__); 1075 sysfs_remove_link(&dev->kobj, "cpufreq"); 1076 cpumask_clear_cpu(cpu, policy->real_cpus); 1077 } 1078 1079 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 1080 { 1081 struct freq_attr **drv_attr; 1082 int ret = 0; 1083 1084 /* set up files for this cpu device */ 1085 drv_attr = cpufreq_driver->attr; 1086 while (drv_attr && *drv_attr) { 1087 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 1088 if (ret) 1089 return ret; 1090 drv_attr++; 1091 } 1092 if (cpufreq_driver->get) { 1093 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 1094 if (ret) 1095 return ret; 1096 } 1097 1098 if (cpufreq_avg_freq_supported(policy)) { 1099 ret = sysfs_create_file(&policy->kobj, &cpuinfo_avg_freq.attr); 1100 if (ret) 1101 return ret; 1102 } 1103 1104 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 1105 if (ret) 1106 return ret; 1107 1108 if (cpufreq_driver->bios_limit) { 1109 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1110 if (ret) 1111 return ret; 1112 } 1113 1114 if (cpufreq_boost_supported()) { 1115 ret = sysfs_create_file(&policy->kobj, &local_boost.attr); 1116 if (ret) 1117 return ret; 1118 } 1119 1120 return 0; 1121 } 1122 1123 static int cpufreq_init_policy(struct cpufreq_policy *policy) 1124 { 1125 struct cpufreq_governor *gov = NULL; 1126 unsigned int pol = CPUFREQ_POLICY_UNKNOWN; 1127 int ret; 1128 1129 if (has_target()) { 1130 /* Update policy governor to the one used before hotplug. */ 1131 gov = get_governor(policy->last_governor); 1132 if (gov) { 1133 pr_debug("Restoring governor %s for cpu %d\n", 1134 gov->name, policy->cpu); 1135 } else { 1136 gov = get_governor(default_governor); 1137 } 1138 1139 if (!gov) { 1140 gov = cpufreq_default_governor(); 1141 __module_get(gov->owner); 1142 } 1143 1144 } else { 1145 1146 /* Use the default policy if there is no last_policy. */ 1147 if (policy->last_policy) { 1148 pol = policy->last_policy; 1149 } else { 1150 pol = cpufreq_parse_policy(default_governor); 1151 /* 1152 * In case the default governor is neither "performance" 1153 * nor "powersave", fall back to the initial policy 1154 * value set by the driver. 1155 */ 1156 if (pol == CPUFREQ_POLICY_UNKNOWN) 1157 pol = policy->policy; 1158 } 1159 if (pol != CPUFREQ_POLICY_PERFORMANCE && 1160 pol != CPUFREQ_POLICY_POWERSAVE) 1161 return -ENODATA; 1162 } 1163 1164 ret = cpufreq_set_policy(policy, gov, pol); 1165 if (gov) 1166 module_put(gov->owner); 1167 1168 return ret; 1169 } 1170 1171 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1172 { 1173 int ret = 0; 1174 1175 /* Has this CPU been taken care of already? */ 1176 if (cpumask_test_cpu(cpu, policy->cpus)) 1177 return 0; 1178 1179 down_write(&policy->rwsem); 1180 if (has_target()) 1181 cpufreq_stop_governor(policy); 1182 1183 cpumask_set_cpu(cpu, policy->cpus); 1184 1185 if (has_target()) { 1186 ret = cpufreq_start_governor(policy); 1187 if (ret) 1188 pr_err("%s: Failed to start governor\n", __func__); 1189 } 1190 up_write(&policy->rwsem); 1191 return ret; 1192 } 1193 1194 void refresh_frequency_limits(struct cpufreq_policy *policy) 1195 { 1196 if (!policy_is_inactive(policy)) { 1197 pr_debug("updating policy for CPU %u\n", policy->cpu); 1198 1199 cpufreq_set_policy(policy, policy->governor, policy->policy); 1200 } 1201 } 1202 EXPORT_SYMBOL(refresh_frequency_limits); 1203 1204 static void handle_update(struct work_struct *work) 1205 { 1206 struct cpufreq_policy *policy = 1207 container_of(work, struct cpufreq_policy, update); 1208 1209 pr_debug("handle_update for cpu %u called\n", policy->cpu); 1210 down_write(&policy->rwsem); 1211 refresh_frequency_limits(policy); 1212 up_write(&policy->rwsem); 1213 } 1214 1215 static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, 1216 void *data) 1217 { 1218 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); 1219 1220 schedule_work(&policy->update); 1221 return 0; 1222 } 1223 1224 static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, 1225 void *data) 1226 { 1227 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); 1228 1229 schedule_work(&policy->update); 1230 return 0; 1231 } 1232 1233 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 1234 { 1235 struct kobject *kobj; 1236 struct completion *cmp; 1237 1238 down_write(&policy->rwsem); 1239 cpufreq_stats_free_table(policy); 1240 kobj = &policy->kobj; 1241 cmp = &policy->kobj_unregister; 1242 up_write(&policy->rwsem); 1243 kobject_put(kobj); 1244 1245 /* 1246 * We need to make sure that the underlying kobj is 1247 * actually not referenced anymore by anybody before we 1248 * proceed with unloading. 1249 */ 1250 pr_debug("waiting for dropping of refcount\n"); 1251 wait_for_completion(cmp); 1252 pr_debug("wait complete\n"); 1253 } 1254 1255 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 1256 { 1257 struct cpufreq_policy *policy; 1258 struct device *dev = get_cpu_device(cpu); 1259 int ret; 1260 1261 if (!dev) 1262 return NULL; 1263 1264 policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1265 if (!policy) 1266 return NULL; 1267 1268 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1269 goto err_free_policy; 1270 1271 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1272 goto err_free_cpumask; 1273 1274 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1275 goto err_free_rcpumask; 1276 1277 init_completion(&policy->kobj_unregister); 1278 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 1279 cpufreq_global_kobject, "policy%u", cpu); 1280 if (ret) { 1281 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); 1282 /* 1283 * The entire policy object will be freed below, but the extra 1284 * memory allocated for the kobject name needs to be freed by 1285 * releasing the kobject. 1286 */ 1287 kobject_put(&policy->kobj); 1288 goto err_free_real_cpus; 1289 } 1290 1291 freq_constraints_init(&policy->constraints); 1292 1293 policy->nb_min.notifier_call = cpufreq_notifier_min; 1294 policy->nb_max.notifier_call = cpufreq_notifier_max; 1295 1296 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, 1297 &policy->nb_min); 1298 if (ret) { 1299 dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n", 1300 ret, cpu); 1301 goto err_kobj_remove; 1302 } 1303 1304 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, 1305 &policy->nb_max); 1306 if (ret) { 1307 dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n", 1308 ret, cpu); 1309 goto err_min_qos_notifier; 1310 } 1311 1312 INIT_LIST_HEAD(&policy->policy_list); 1313 init_rwsem(&policy->rwsem); 1314 spin_lock_init(&policy->transition_lock); 1315 init_waitqueue_head(&policy->transition_wait); 1316 INIT_WORK(&policy->update, handle_update); 1317 1318 policy->cpu = cpu; 1319 return policy; 1320 1321 err_min_qos_notifier: 1322 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 1323 &policy->nb_min); 1324 err_kobj_remove: 1325 cpufreq_policy_put_kobj(policy); 1326 err_free_real_cpus: 1327 free_cpumask_var(policy->real_cpus); 1328 err_free_rcpumask: 1329 free_cpumask_var(policy->related_cpus); 1330 err_free_cpumask: 1331 free_cpumask_var(policy->cpus); 1332 err_free_policy: 1333 kfree(policy); 1334 1335 return NULL; 1336 } 1337 1338 static void cpufreq_policy_free(struct cpufreq_policy *policy) 1339 { 1340 unsigned long flags; 1341 int cpu; 1342 1343 /* 1344 * The callers must ensure the policy is inactive by now, to avoid any 1345 * races with show()/store() callbacks. 1346 */ 1347 if (unlikely(!policy_is_inactive(policy))) 1348 pr_warn("%s: Freeing active policy\n", __func__); 1349 1350 /* Remove policy from list */ 1351 write_lock_irqsave(&cpufreq_driver_lock, flags); 1352 list_del(&policy->policy_list); 1353 1354 for_each_cpu(cpu, policy->related_cpus) 1355 per_cpu(cpufreq_cpu_data, cpu) = NULL; 1356 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1357 1358 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, 1359 &policy->nb_max); 1360 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 1361 &policy->nb_min); 1362 1363 /* Cancel any pending policy->update work before freeing the policy. */ 1364 cancel_work_sync(&policy->update); 1365 1366 if (policy->max_freq_req) { 1367 /* 1368 * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY 1369 * notification, since CPUFREQ_CREATE_POLICY notification was 1370 * sent after adding max_freq_req earlier. 1371 */ 1372 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1373 CPUFREQ_REMOVE_POLICY, policy); 1374 freq_qos_remove_request(policy->max_freq_req); 1375 } 1376 1377 freq_qos_remove_request(policy->min_freq_req); 1378 kfree(policy->min_freq_req); 1379 1380 cpufreq_policy_put_kobj(policy); 1381 free_cpumask_var(policy->real_cpus); 1382 free_cpumask_var(policy->related_cpus); 1383 free_cpumask_var(policy->cpus); 1384 kfree(policy); 1385 } 1386 1387 static int cpufreq_online(unsigned int cpu) 1388 { 1389 struct cpufreq_policy *policy; 1390 bool new_policy; 1391 unsigned long flags; 1392 unsigned int j; 1393 int ret; 1394 1395 pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 1396 1397 /* Check if this CPU already has a policy to manage it */ 1398 policy = per_cpu(cpufreq_cpu_data, cpu); 1399 if (policy) { 1400 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 1401 if (!policy_is_inactive(policy)) 1402 return cpufreq_add_policy_cpu(policy, cpu); 1403 1404 /* This is the only online CPU for the policy. Start over. */ 1405 new_policy = false; 1406 down_write(&policy->rwsem); 1407 policy->cpu = cpu; 1408 policy->governor = NULL; 1409 } else { 1410 new_policy = true; 1411 policy = cpufreq_policy_alloc(cpu); 1412 if (!policy) 1413 return -ENOMEM; 1414 down_write(&policy->rwsem); 1415 } 1416 1417 if (!new_policy && cpufreq_driver->online) { 1418 /* Recover policy->cpus using related_cpus */ 1419 cpumask_copy(policy->cpus, policy->related_cpus); 1420 1421 ret = cpufreq_driver->online(policy); 1422 if (ret) { 1423 pr_debug("%s: %d: initialization failed\n", __func__, 1424 __LINE__); 1425 goto out_exit_policy; 1426 } 1427 } else { 1428 cpumask_copy(policy->cpus, cpumask_of(cpu)); 1429 1430 /* 1431 * Call driver. From then on the cpufreq must be able 1432 * to accept all calls to ->verify and ->setpolicy for this CPU. 1433 */ 1434 ret = cpufreq_driver->init(policy); 1435 if (ret) { 1436 pr_debug("%s: %d: initialization failed\n", __func__, 1437 __LINE__); 1438 goto out_free_policy; 1439 } 1440 1441 /* 1442 * The initialization has succeeded and the policy is online. 1443 * If there is a problem with its frequency table, take it 1444 * offline and drop it. 1445 */ 1446 ret = cpufreq_table_validate_and_sort(policy); 1447 if (ret) 1448 goto out_offline_policy; 1449 1450 /* related_cpus should at least include policy->cpus. */ 1451 cpumask_copy(policy->related_cpus, policy->cpus); 1452 } 1453 1454 /* 1455 * affected cpus must always be the one, which are online. We aren't 1456 * managing offline cpus here. 1457 */ 1458 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 1459 1460 if (new_policy) { 1461 for_each_cpu(j, policy->related_cpus) { 1462 per_cpu(cpufreq_cpu_data, j) = policy; 1463 add_cpu_dev_symlink(policy, j, get_cpu_device(j)); 1464 } 1465 1466 policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), 1467 GFP_KERNEL); 1468 if (!policy->min_freq_req) { 1469 ret = -ENOMEM; 1470 goto out_destroy_policy; 1471 } 1472 1473 ret = freq_qos_add_request(&policy->constraints, 1474 policy->min_freq_req, FREQ_QOS_MIN, 1475 FREQ_QOS_MIN_DEFAULT_VALUE); 1476 if (ret < 0) { 1477 /* 1478 * So we don't call freq_qos_remove_request() for an 1479 * uninitialized request. 1480 */ 1481 kfree(policy->min_freq_req); 1482 policy->min_freq_req = NULL; 1483 goto out_destroy_policy; 1484 } 1485 1486 /* 1487 * This must be initialized right here to avoid calling 1488 * freq_qos_remove_request() on uninitialized request in case 1489 * of errors. 1490 */ 1491 policy->max_freq_req = policy->min_freq_req + 1; 1492 1493 ret = freq_qos_add_request(&policy->constraints, 1494 policy->max_freq_req, FREQ_QOS_MAX, 1495 FREQ_QOS_MAX_DEFAULT_VALUE); 1496 if (ret < 0) { 1497 policy->max_freq_req = NULL; 1498 goto out_destroy_policy; 1499 } 1500 1501 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1502 CPUFREQ_CREATE_POLICY, policy); 1503 } else { 1504 ret = freq_qos_update_request(policy->max_freq_req, policy->max); 1505 if (ret < 0) 1506 goto out_destroy_policy; 1507 } 1508 1509 if (cpufreq_driver->get && has_target()) { 1510 policy->cur = cpufreq_driver->get(policy->cpu); 1511 if (!policy->cur) { 1512 ret = -EIO; 1513 pr_err("%s: ->get() failed\n", __func__); 1514 goto out_destroy_policy; 1515 } 1516 } 1517 1518 /* 1519 * Sometimes boot loaders set CPU frequency to a value outside of 1520 * frequency table present with cpufreq core. In such cases CPU might be 1521 * unstable if it has to run on that frequency for long duration of time 1522 * and so its better to set it to a frequency which is specified in 1523 * freq-table. This also makes cpufreq stats inconsistent as 1524 * cpufreq-stats would fail to register because current frequency of CPU 1525 * isn't found in freq-table. 1526 * 1527 * Because we don't want this change to effect boot process badly, we go 1528 * for the next freq which is >= policy->cur ('cur' must be set by now, 1529 * otherwise we will end up setting freq to lowest of the table as 'cur' 1530 * is initialized to zero). 1531 * 1532 * We are passing target-freq as "policy->cur - 1" otherwise 1533 * __cpufreq_driver_target() would simply fail, as policy->cur will be 1534 * equal to target-freq. 1535 */ 1536 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1537 && has_target()) { 1538 unsigned int old_freq = policy->cur; 1539 1540 /* Are we running at unknown frequency ? */ 1541 ret = cpufreq_frequency_table_get_index(policy, old_freq); 1542 if (ret == -EINVAL) { 1543 ret = __cpufreq_driver_target(policy, old_freq - 1, 1544 CPUFREQ_RELATION_L); 1545 1546 /* 1547 * Reaching here after boot in a few seconds may not 1548 * mean that system will remain stable at "unknown" 1549 * frequency for longer duration. Hence, a BUG_ON(). 1550 */ 1551 BUG_ON(ret); 1552 pr_info("%s: CPU%d: Running at unlisted initial frequency: %u kHz, changing to: %u kHz\n", 1553 __func__, policy->cpu, old_freq, policy->cur); 1554 } 1555 } 1556 1557 if (new_policy) { 1558 ret = cpufreq_add_dev_interface(policy); 1559 if (ret) 1560 goto out_destroy_policy; 1561 1562 cpufreq_stats_create_table(policy); 1563 1564 write_lock_irqsave(&cpufreq_driver_lock, flags); 1565 list_add(&policy->policy_list, &cpufreq_policy_list); 1566 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1567 1568 /* 1569 * Register with the energy model before 1570 * em_rebuild_sched_domains() is called, which will result 1571 * in rebuilding of the sched domains, which should only be done 1572 * once the energy model is properly initialized for the policy 1573 * first. 1574 * 1575 * Also, this should be called before the policy is registered 1576 * with cooling framework. 1577 */ 1578 if (cpufreq_driver->register_em) 1579 cpufreq_driver->register_em(policy); 1580 } 1581 1582 ret = cpufreq_init_policy(policy); 1583 if (ret) { 1584 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 1585 __func__, cpu, ret); 1586 goto out_destroy_policy; 1587 } 1588 1589 up_write(&policy->rwsem); 1590 1591 kobject_uevent(&policy->kobj, KOBJ_ADD); 1592 1593 /* Callback for handling stuff after policy is ready */ 1594 if (cpufreq_driver->ready) 1595 cpufreq_driver->ready(policy); 1596 1597 /* Register cpufreq cooling only for a new policy */ 1598 if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver)) 1599 policy->cdev = of_cpufreq_cooling_register(policy); 1600 1601 /* Let the per-policy boost flag mirror the cpufreq_driver boost during init */ 1602 if (cpufreq_driver->set_boost && 1603 policy->boost_enabled != cpufreq_boost_enabled()) { 1604 policy->boost_enabled = cpufreq_boost_enabled(); 1605 ret = cpufreq_driver->set_boost(policy, policy->boost_enabled); 1606 if (ret) { 1607 /* If the set_boost fails, the online operation is not affected */ 1608 pr_info("%s: CPU%d: Cannot %s BOOST\n", __func__, policy->cpu, 1609 policy->boost_enabled ? "enable" : "disable"); 1610 policy->boost_enabled = !policy->boost_enabled; 1611 } 1612 } 1613 1614 pr_debug("initialization complete\n"); 1615 1616 return 0; 1617 1618 out_destroy_policy: 1619 for_each_cpu(j, policy->real_cpus) 1620 remove_cpu_dev_symlink(policy, j, get_cpu_device(j)); 1621 1622 out_offline_policy: 1623 if (cpufreq_driver->offline) 1624 cpufreq_driver->offline(policy); 1625 1626 out_exit_policy: 1627 if (cpufreq_driver->exit) 1628 cpufreq_driver->exit(policy); 1629 1630 out_free_policy: 1631 cpumask_clear(policy->cpus); 1632 up_write(&policy->rwsem); 1633 1634 cpufreq_policy_free(policy); 1635 return ret; 1636 } 1637 1638 /** 1639 * cpufreq_add_dev - the cpufreq interface for a CPU device. 1640 * @dev: CPU device. 1641 * @sif: Subsystem interface structure pointer (not used) 1642 */ 1643 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 1644 { 1645 struct cpufreq_policy *policy; 1646 unsigned cpu = dev->id; 1647 int ret; 1648 1649 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 1650 1651 if (cpu_online(cpu)) { 1652 ret = cpufreq_online(cpu); 1653 if (ret) 1654 return ret; 1655 } 1656 1657 /* Create sysfs link on CPU registration */ 1658 policy = per_cpu(cpufreq_cpu_data, cpu); 1659 if (policy) 1660 add_cpu_dev_symlink(policy, cpu, dev); 1661 1662 return 0; 1663 } 1664 1665 static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy) 1666 { 1667 int ret; 1668 1669 if (has_target()) 1670 cpufreq_stop_governor(policy); 1671 1672 cpumask_clear_cpu(cpu, policy->cpus); 1673 1674 if (!policy_is_inactive(policy)) { 1675 /* Nominate a new CPU if necessary. */ 1676 if (cpu == policy->cpu) 1677 policy->cpu = cpumask_any(policy->cpus); 1678 1679 /* Start the governor again for the active policy. */ 1680 if (has_target()) { 1681 ret = cpufreq_start_governor(policy); 1682 if (ret) 1683 pr_err("%s: Failed to start governor\n", __func__); 1684 } 1685 1686 return; 1687 } 1688 1689 if (has_target()) 1690 strscpy(policy->last_governor, policy->governor->name, 1691 CPUFREQ_NAME_LEN); 1692 else 1693 policy->last_policy = policy->policy; 1694 1695 if (has_target()) 1696 cpufreq_exit_governor(policy); 1697 1698 /* 1699 * Perform the ->offline() during light-weight tear-down, as 1700 * that allows fast recovery when the CPU comes back. 1701 */ 1702 if (cpufreq_driver->offline) { 1703 cpufreq_driver->offline(policy); 1704 return; 1705 } 1706 1707 if (cpufreq_driver->exit) 1708 cpufreq_driver->exit(policy); 1709 1710 policy->freq_table = NULL; 1711 } 1712 1713 static int cpufreq_offline(unsigned int cpu) 1714 { 1715 struct cpufreq_policy *policy; 1716 1717 pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 1718 1719 policy = cpufreq_cpu_get_raw(cpu); 1720 if (!policy) { 1721 pr_debug("%s: No cpu_data found\n", __func__); 1722 return 0; 1723 } 1724 1725 down_write(&policy->rwsem); 1726 1727 __cpufreq_offline(cpu, policy); 1728 1729 up_write(&policy->rwsem); 1730 return 0; 1731 } 1732 1733 /* 1734 * cpufreq_remove_dev - remove a CPU device 1735 * 1736 * Removes the cpufreq interface for a CPU device. 1737 */ 1738 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 1739 { 1740 unsigned int cpu = dev->id; 1741 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1742 1743 if (!policy) 1744 return; 1745 1746 down_write(&policy->rwsem); 1747 1748 if (cpu_online(cpu)) 1749 __cpufreq_offline(cpu, policy); 1750 1751 remove_cpu_dev_symlink(policy, cpu, dev); 1752 1753 if (!cpumask_empty(policy->real_cpus)) { 1754 up_write(&policy->rwsem); 1755 return; 1756 } 1757 1758 /* 1759 * Unregister cpufreq cooling once all the CPUs of the policy are 1760 * removed. 1761 */ 1762 if (cpufreq_thermal_control_enabled(cpufreq_driver)) { 1763 cpufreq_cooling_unregister(policy->cdev); 1764 policy->cdev = NULL; 1765 } 1766 1767 /* We did light-weight exit earlier, do full tear down now */ 1768 if (cpufreq_driver->offline && cpufreq_driver->exit) 1769 cpufreq_driver->exit(policy); 1770 1771 up_write(&policy->rwsem); 1772 1773 cpufreq_policy_free(policy); 1774 } 1775 1776 /** 1777 * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference. 1778 * @policy: Policy managing CPUs. 1779 * @new_freq: New CPU frequency. 1780 * 1781 * Adjust to the current frequency first and clean up later by either calling 1782 * cpufreq_update_policy(), or scheduling handle_update(). 1783 */ 1784 static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1785 unsigned int new_freq) 1786 { 1787 struct cpufreq_freqs freqs; 1788 1789 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1790 policy->cur, new_freq); 1791 1792 freqs.old = policy->cur; 1793 freqs.new = new_freq; 1794 1795 cpufreq_freq_transition_begin(policy, &freqs); 1796 cpufreq_freq_transition_end(policy, &freqs, 0); 1797 } 1798 1799 static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) 1800 { 1801 unsigned int new_freq; 1802 1803 new_freq = cpufreq_driver->get(policy->cpu); 1804 if (!new_freq) 1805 return 0; 1806 1807 /* 1808 * If fast frequency switching is used with the given policy, the check 1809 * against policy->cur is pointless, so skip it in that case. 1810 */ 1811 if (policy->fast_switch_enabled || !has_target()) 1812 return new_freq; 1813 1814 if (policy->cur != new_freq) { 1815 /* 1816 * For some platforms, the frequency returned by hardware may be 1817 * slightly different from what is provided in the frequency 1818 * table, for example hardware may return 499 MHz instead of 500 1819 * MHz. In such cases it is better to avoid getting into 1820 * unnecessary frequency updates. 1821 */ 1822 if (abs(policy->cur - new_freq) < KHZ_PER_MHZ) 1823 return policy->cur; 1824 1825 cpufreq_out_of_sync(policy, new_freq); 1826 if (update) 1827 schedule_work(&policy->update); 1828 } 1829 1830 return new_freq; 1831 } 1832 1833 /** 1834 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 1835 * @cpu: CPU number 1836 * 1837 * This is the last known freq, without actually getting it from the driver. 1838 * Return value will be same as what is shown in scaling_cur_freq in sysfs. 1839 */ 1840 unsigned int cpufreq_quick_get(unsigned int cpu) 1841 { 1842 struct cpufreq_policy *policy; 1843 unsigned int ret_freq = 0; 1844 unsigned long flags; 1845 1846 read_lock_irqsave(&cpufreq_driver_lock, flags); 1847 1848 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1849 ret_freq = cpufreq_driver->get(cpu); 1850 read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1851 return ret_freq; 1852 } 1853 1854 read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1855 1856 policy = cpufreq_cpu_get(cpu); 1857 if (policy) { 1858 ret_freq = policy->cur; 1859 cpufreq_cpu_put(policy); 1860 } 1861 1862 return ret_freq; 1863 } 1864 EXPORT_SYMBOL(cpufreq_quick_get); 1865 1866 /** 1867 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 1868 * @cpu: CPU number 1869 * 1870 * Just return the max possible frequency for a given CPU. 1871 */ 1872 unsigned int cpufreq_quick_get_max(unsigned int cpu) 1873 { 1874 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1875 unsigned int ret_freq = 0; 1876 1877 if (policy) { 1878 ret_freq = policy->max; 1879 cpufreq_cpu_put(policy); 1880 } 1881 1882 return ret_freq; 1883 } 1884 EXPORT_SYMBOL(cpufreq_quick_get_max); 1885 1886 /** 1887 * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU 1888 * @cpu: CPU number 1889 * 1890 * The default return value is the max_freq field of cpuinfo. 1891 */ 1892 __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) 1893 { 1894 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1895 unsigned int ret_freq = 0; 1896 1897 if (policy) { 1898 ret_freq = policy->cpuinfo.max_freq; 1899 cpufreq_cpu_put(policy); 1900 } 1901 1902 return ret_freq; 1903 } 1904 EXPORT_SYMBOL(cpufreq_get_hw_max_freq); 1905 1906 static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 1907 { 1908 if (unlikely(policy_is_inactive(policy))) 1909 return 0; 1910 1911 return cpufreq_verify_current_freq(policy, true); 1912 } 1913 1914 /** 1915 * cpufreq_get - get the current CPU frequency (in kHz) 1916 * @cpu: CPU number 1917 * 1918 * Get the CPU current (static) CPU frequency 1919 */ 1920 unsigned int cpufreq_get(unsigned int cpu) 1921 { 1922 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1923 unsigned int ret_freq = 0; 1924 1925 if (policy) { 1926 down_read(&policy->rwsem); 1927 if (cpufreq_driver->get) 1928 ret_freq = __cpufreq_get(policy); 1929 up_read(&policy->rwsem); 1930 1931 cpufreq_cpu_put(policy); 1932 } 1933 1934 return ret_freq; 1935 } 1936 EXPORT_SYMBOL(cpufreq_get); 1937 1938 static struct subsys_interface cpufreq_interface = { 1939 .name = "cpufreq", 1940 .subsys = &cpu_subsys, 1941 .add_dev = cpufreq_add_dev, 1942 .remove_dev = cpufreq_remove_dev, 1943 }; 1944 1945 /* 1946 * In case platform wants some specific frequency to be configured 1947 * during suspend.. 1948 */ 1949 int cpufreq_generic_suspend(struct cpufreq_policy *policy) 1950 { 1951 int ret; 1952 1953 if (!policy->suspend_freq) { 1954 pr_debug("%s: suspend_freq not defined\n", __func__); 1955 return 0; 1956 } 1957 1958 pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1959 policy->suspend_freq); 1960 1961 ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1962 CPUFREQ_RELATION_H); 1963 if (ret) 1964 pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1965 __func__, policy->suspend_freq, ret); 1966 1967 return ret; 1968 } 1969 EXPORT_SYMBOL(cpufreq_generic_suspend); 1970 1971 /** 1972 * cpufreq_suspend() - Suspend CPUFreq governors. 1973 * 1974 * Called during system wide Suspend/Hibernate cycles for suspending governors 1975 * as some platforms can't change frequency after this point in suspend cycle. 1976 * Because some of the devices (like: i2c, regulators, etc) they use for 1977 * changing frequency are suspended quickly after this point. 1978 */ 1979 void cpufreq_suspend(void) 1980 { 1981 struct cpufreq_policy *policy; 1982 1983 if (!cpufreq_driver) 1984 return; 1985 1986 if (!has_target() && !cpufreq_driver->suspend) 1987 goto suspend; 1988 1989 pr_debug("%s: Suspending Governors\n", __func__); 1990 1991 for_each_active_policy(policy) { 1992 if (has_target()) { 1993 down_write(&policy->rwsem); 1994 cpufreq_stop_governor(policy); 1995 up_write(&policy->rwsem); 1996 } 1997 1998 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) 1999 pr_err("%s: Failed to suspend driver: %s\n", __func__, 2000 cpufreq_driver->name); 2001 } 2002 2003 suspend: 2004 cpufreq_suspended = true; 2005 } 2006 2007 /** 2008 * cpufreq_resume() - Resume CPUFreq governors. 2009 * 2010 * Called during system wide Suspend/Hibernate cycle for resuming governors that 2011 * are suspended with cpufreq_suspend(). 2012 */ 2013 void cpufreq_resume(void) 2014 { 2015 struct cpufreq_policy *policy; 2016 int ret; 2017 2018 if (!cpufreq_driver) 2019 return; 2020 2021 if (unlikely(!cpufreq_suspended)) 2022 return; 2023 2024 cpufreq_suspended = false; 2025 2026 if (!has_target() && !cpufreq_driver->resume) 2027 return; 2028 2029 pr_debug("%s: Resuming Governors\n", __func__); 2030 2031 for_each_active_policy(policy) { 2032 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 2033 pr_err("%s: Failed to resume driver: %s\n", __func__, 2034 cpufreq_driver->name); 2035 } else if (has_target()) { 2036 down_write(&policy->rwsem); 2037 ret = cpufreq_start_governor(policy); 2038 up_write(&policy->rwsem); 2039 2040 if (ret) 2041 pr_err("%s: Failed to start governor for CPU%u's policy\n", 2042 __func__, policy->cpu); 2043 } 2044 } 2045 } 2046 2047 /** 2048 * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones. 2049 * @flags: Flags to test against the current cpufreq driver's flags. 2050 * 2051 * Assumes that the driver is there, so callers must ensure that this is the 2052 * case. 2053 */ 2054 bool cpufreq_driver_test_flags(u16 flags) 2055 { 2056 return !!(cpufreq_driver->flags & flags); 2057 } 2058 2059 /** 2060 * cpufreq_get_current_driver - Return the current driver's name. 2061 * 2062 * Return the name string of the currently registered cpufreq driver or NULL if 2063 * none. 2064 */ 2065 const char *cpufreq_get_current_driver(void) 2066 { 2067 if (cpufreq_driver) 2068 return cpufreq_driver->name; 2069 2070 return NULL; 2071 } 2072 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 2073 2074 /** 2075 * cpufreq_get_driver_data - Return current driver data. 2076 * 2077 * Return the private data of the currently registered cpufreq driver, or NULL 2078 * if no cpufreq driver has been registered. 2079 */ 2080 void *cpufreq_get_driver_data(void) 2081 { 2082 if (cpufreq_driver) 2083 return cpufreq_driver->driver_data; 2084 2085 return NULL; 2086 } 2087 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 2088 2089 /********************************************************************* 2090 * NOTIFIER LISTS INTERFACE * 2091 *********************************************************************/ 2092 2093 /** 2094 * cpufreq_register_notifier - Register a notifier with cpufreq. 2095 * @nb: notifier function to register. 2096 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. 2097 * 2098 * Add a notifier to one of two lists: either a list of notifiers that run on 2099 * clock rate changes (once before and once after every transition), or a list 2100 * of notifiers that ron on cpufreq policy changes. 2101 * 2102 * This function may sleep and it has the same return values as 2103 * blocking_notifier_chain_register(). 2104 */ 2105 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 2106 { 2107 int ret; 2108 2109 if (cpufreq_disabled()) 2110 return -EINVAL; 2111 2112 switch (list) { 2113 case CPUFREQ_TRANSITION_NOTIFIER: 2114 mutex_lock(&cpufreq_fast_switch_lock); 2115 2116 if (cpufreq_fast_switch_count > 0) { 2117 mutex_unlock(&cpufreq_fast_switch_lock); 2118 return -EBUSY; 2119 } 2120 ret = srcu_notifier_chain_register( 2121 &cpufreq_transition_notifier_list, nb); 2122 if (!ret) 2123 cpufreq_fast_switch_count--; 2124 2125 mutex_unlock(&cpufreq_fast_switch_lock); 2126 break; 2127 case CPUFREQ_POLICY_NOTIFIER: 2128 ret = blocking_notifier_chain_register( 2129 &cpufreq_policy_notifier_list, nb); 2130 break; 2131 default: 2132 ret = -EINVAL; 2133 } 2134 2135 return ret; 2136 } 2137 EXPORT_SYMBOL(cpufreq_register_notifier); 2138 2139 /** 2140 * cpufreq_unregister_notifier - Unregister a notifier from cpufreq. 2141 * @nb: notifier block to be unregistered. 2142 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. 2143 * 2144 * Remove a notifier from one of the cpufreq notifier lists. 2145 * 2146 * This function may sleep and it has the same return values as 2147 * blocking_notifier_chain_unregister(). 2148 */ 2149 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 2150 { 2151 int ret; 2152 2153 if (cpufreq_disabled()) 2154 return -EINVAL; 2155 2156 switch (list) { 2157 case CPUFREQ_TRANSITION_NOTIFIER: 2158 mutex_lock(&cpufreq_fast_switch_lock); 2159 2160 ret = srcu_notifier_chain_unregister( 2161 &cpufreq_transition_notifier_list, nb); 2162 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) 2163 cpufreq_fast_switch_count++; 2164 2165 mutex_unlock(&cpufreq_fast_switch_lock); 2166 break; 2167 case CPUFREQ_POLICY_NOTIFIER: 2168 ret = blocking_notifier_chain_unregister( 2169 &cpufreq_policy_notifier_list, nb); 2170 break; 2171 default: 2172 ret = -EINVAL; 2173 } 2174 2175 return ret; 2176 } 2177 EXPORT_SYMBOL(cpufreq_unregister_notifier); 2178 2179 2180 /********************************************************************* 2181 * GOVERNORS * 2182 *********************************************************************/ 2183 2184 /** 2185 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. 2186 * @policy: cpufreq policy to switch the frequency for. 2187 * @target_freq: New frequency to set (may be approximate). 2188 * 2189 * Carry out a fast frequency switch without sleeping. 2190 * 2191 * The driver's ->fast_switch() callback invoked by this function must be 2192 * suitable for being called from within RCU-sched read-side critical sections 2193 * and it is expected to select the minimum available frequency greater than or 2194 * equal to @target_freq (CPUFREQ_RELATION_L). 2195 * 2196 * This function must not be called if policy->fast_switch_enabled is unset. 2197 * 2198 * Governors calling this function must guarantee that it will never be invoked 2199 * twice in parallel for the same policy and that it will never be called in 2200 * parallel with either ->target() or ->target_index() for the same policy. 2201 * 2202 * Returns the actual frequency set for the CPU. 2203 * 2204 * If 0 is returned by the driver's ->fast_switch() callback to indicate an 2205 * error condition, the hardware configuration must be preserved. 2206 */ 2207 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, 2208 unsigned int target_freq) 2209 { 2210 unsigned int freq; 2211 int cpu; 2212 2213 target_freq = clamp_val(target_freq, policy->min, policy->max); 2214 freq = cpufreq_driver->fast_switch(policy, target_freq); 2215 2216 if (!freq) 2217 return 0; 2218 2219 policy->cur = freq; 2220 arch_set_freq_scale(policy->related_cpus, freq, 2221 arch_scale_freq_ref(policy->cpu)); 2222 cpufreq_stats_record_transition(policy, freq); 2223 2224 if (trace_cpu_frequency_enabled()) { 2225 for_each_cpu(cpu, policy->cpus) 2226 trace_cpu_frequency(freq, cpu); 2227 } 2228 2229 return freq; 2230 } 2231 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); 2232 2233 /** 2234 * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. 2235 * @cpu: Target CPU. 2236 * @min_perf: Minimum (required) performance level (units of @capacity). 2237 * @target_perf: Target (desired) performance level (units of @capacity). 2238 * @capacity: Capacity of the target CPU. 2239 * 2240 * Carry out a fast performance level switch of @cpu without sleeping. 2241 * 2242 * The driver's ->adjust_perf() callback invoked by this function must be 2243 * suitable for being called from within RCU-sched read-side critical sections 2244 * and it is expected to select a suitable performance level equal to or above 2245 * @min_perf and preferably equal to or below @target_perf. 2246 * 2247 * This function must not be called if policy->fast_switch_enabled is unset. 2248 * 2249 * Governors calling this function must guarantee that it will never be invoked 2250 * twice in parallel for the same CPU and that it will never be called in 2251 * parallel with either ->target() or ->target_index() or ->fast_switch() for 2252 * the same CPU. 2253 */ 2254 void cpufreq_driver_adjust_perf(unsigned int cpu, 2255 unsigned long min_perf, 2256 unsigned long target_perf, 2257 unsigned long capacity) 2258 { 2259 cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity); 2260 } 2261 2262 /** 2263 * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback. 2264 * 2265 * Return 'true' if the ->adjust_perf callback is present for the 2266 * current driver or 'false' otherwise. 2267 */ 2268 bool cpufreq_driver_has_adjust_perf(void) 2269 { 2270 return !!cpufreq_driver->adjust_perf; 2271 } 2272 2273 /* Must set freqs->new to intermediate frequency */ 2274 static int __target_intermediate(struct cpufreq_policy *policy, 2275 struct cpufreq_freqs *freqs, int index) 2276 { 2277 int ret; 2278 2279 freqs->new = cpufreq_driver->get_intermediate(policy, index); 2280 2281 /* We don't need to switch to intermediate freq */ 2282 if (!freqs->new) 2283 return 0; 2284 2285 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 2286 __func__, policy->cpu, freqs->old, freqs->new); 2287 2288 cpufreq_freq_transition_begin(policy, freqs); 2289 ret = cpufreq_driver->target_intermediate(policy, index); 2290 cpufreq_freq_transition_end(policy, freqs, ret); 2291 2292 if (ret) 2293 pr_err("%s: Failed to change to intermediate frequency: %d\n", 2294 __func__, ret); 2295 2296 return ret; 2297 } 2298 2299 static int __target_index(struct cpufreq_policy *policy, int index) 2300 { 2301 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 2302 unsigned int restore_freq, intermediate_freq = 0; 2303 unsigned int newfreq = policy->freq_table[index].frequency; 2304 int retval = -EINVAL; 2305 bool notify; 2306 2307 if (newfreq == policy->cur) 2308 return 0; 2309 2310 /* Save last value to restore later on errors */ 2311 restore_freq = policy->cur; 2312 2313 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 2314 if (notify) { 2315 /* Handle switching to intermediate frequency */ 2316 if (cpufreq_driver->get_intermediate) { 2317 retval = __target_intermediate(policy, &freqs, index); 2318 if (retval) 2319 return retval; 2320 2321 intermediate_freq = freqs.new; 2322 /* Set old freq to intermediate */ 2323 if (intermediate_freq) 2324 freqs.old = freqs.new; 2325 } 2326 2327 freqs.new = newfreq; 2328 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 2329 __func__, policy->cpu, freqs.old, freqs.new); 2330 2331 cpufreq_freq_transition_begin(policy, &freqs); 2332 } 2333 2334 retval = cpufreq_driver->target_index(policy, index); 2335 if (retval) 2336 pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 2337 retval); 2338 2339 if (notify) { 2340 cpufreq_freq_transition_end(policy, &freqs, retval); 2341 2342 /* 2343 * Failed after setting to intermediate freq? Driver should have 2344 * reverted back to initial frequency and so should we. Check 2345 * here for intermediate_freq instead of get_intermediate, in 2346 * case we haven't switched to intermediate freq at all. 2347 */ 2348 if (unlikely(retval && intermediate_freq)) { 2349 freqs.old = intermediate_freq; 2350 freqs.new = restore_freq; 2351 cpufreq_freq_transition_begin(policy, &freqs); 2352 cpufreq_freq_transition_end(policy, &freqs, 0); 2353 } 2354 } 2355 2356 return retval; 2357 } 2358 2359 int __cpufreq_driver_target(struct cpufreq_policy *policy, 2360 unsigned int target_freq, 2361 unsigned int relation) 2362 { 2363 unsigned int old_target_freq = target_freq; 2364 2365 if (cpufreq_disabled()) 2366 return -ENODEV; 2367 2368 target_freq = __resolve_freq(policy, target_freq, relation); 2369 2370 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 2371 policy->cpu, target_freq, relation, old_target_freq); 2372 2373 /* 2374 * This might look like a redundant call as we are checking it again 2375 * after finding index. But it is left intentionally for cases where 2376 * exactly same freq is called again and so we can save on few function 2377 * calls. 2378 */ 2379 if (target_freq == policy->cur && 2380 !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) 2381 return 0; 2382 2383 if (cpufreq_driver->target) { 2384 /* 2385 * If the driver hasn't setup a single inefficient frequency, 2386 * it's unlikely it knows how to decode CPUFREQ_RELATION_E. 2387 */ 2388 if (!policy->efficiencies_available) 2389 relation &= ~CPUFREQ_RELATION_E; 2390 2391 return cpufreq_driver->target(policy, target_freq, relation); 2392 } 2393 2394 if (!cpufreq_driver->target_index) 2395 return -EINVAL; 2396 2397 return __target_index(policy, policy->cached_resolved_idx); 2398 } 2399 EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 2400 2401 int cpufreq_driver_target(struct cpufreq_policy *policy, 2402 unsigned int target_freq, 2403 unsigned int relation) 2404 { 2405 int ret; 2406 2407 down_write(&policy->rwsem); 2408 2409 ret = __cpufreq_driver_target(policy, target_freq, relation); 2410 2411 up_write(&policy->rwsem); 2412 2413 return ret; 2414 } 2415 EXPORT_SYMBOL_GPL(cpufreq_driver_target); 2416 2417 __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 2418 { 2419 return NULL; 2420 } 2421 2422 static int cpufreq_init_governor(struct cpufreq_policy *policy) 2423 { 2424 int ret; 2425 2426 /* Don't start any governor operations if we are entering suspend */ 2427 if (cpufreq_suspended) 2428 return 0; 2429 /* 2430 * Governor might not be initiated here if ACPI _PPC changed 2431 * notification happened, so check it. 2432 */ 2433 if (!policy->governor) 2434 return -EINVAL; 2435 2436 /* Platform doesn't want dynamic frequency switching ? */ 2437 if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && 2438 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { 2439 struct cpufreq_governor *gov = cpufreq_fallback_governor(); 2440 2441 if (gov) { 2442 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", 2443 policy->governor->name, gov->name); 2444 policy->governor = gov; 2445 } else { 2446 return -EINVAL; 2447 } 2448 } 2449 2450 if (!try_module_get(policy->governor->owner)) 2451 return -EINVAL; 2452 2453 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2454 2455 if (policy->governor->init) { 2456 ret = policy->governor->init(policy); 2457 if (ret) { 2458 module_put(policy->governor->owner); 2459 return ret; 2460 } 2461 } 2462 2463 policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET); 2464 2465 return 0; 2466 } 2467 2468 static void cpufreq_exit_governor(struct cpufreq_policy *policy) 2469 { 2470 if (cpufreq_suspended || !policy->governor) 2471 return; 2472 2473 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2474 2475 if (policy->governor->exit) 2476 policy->governor->exit(policy); 2477 2478 module_put(policy->governor->owner); 2479 } 2480 2481 int cpufreq_start_governor(struct cpufreq_policy *policy) 2482 { 2483 int ret; 2484 2485 if (cpufreq_suspended) 2486 return 0; 2487 2488 if (!policy->governor) 2489 return -EINVAL; 2490 2491 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2492 2493 if (cpufreq_driver->get) 2494 cpufreq_verify_current_freq(policy, false); 2495 2496 if (policy->governor->start) { 2497 ret = policy->governor->start(policy); 2498 if (ret) 2499 return ret; 2500 } 2501 2502 if (policy->governor->limits) 2503 policy->governor->limits(policy); 2504 2505 return 0; 2506 } 2507 2508 void cpufreq_stop_governor(struct cpufreq_policy *policy) 2509 { 2510 if (cpufreq_suspended || !policy->governor) 2511 return; 2512 2513 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2514 2515 if (policy->governor->stop) 2516 policy->governor->stop(policy); 2517 } 2518 2519 static void cpufreq_governor_limits(struct cpufreq_policy *policy) 2520 { 2521 if (cpufreq_suspended || !policy->governor) 2522 return; 2523 2524 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2525 2526 if (policy->governor->limits) 2527 policy->governor->limits(policy); 2528 } 2529 2530 int cpufreq_register_governor(struct cpufreq_governor *governor) 2531 { 2532 int err; 2533 2534 if (!governor) 2535 return -EINVAL; 2536 2537 if (cpufreq_disabled()) 2538 return -ENODEV; 2539 2540 mutex_lock(&cpufreq_governor_mutex); 2541 2542 err = -EBUSY; 2543 if (!find_governor(governor->name)) { 2544 err = 0; 2545 list_add(&governor->governor_list, &cpufreq_governor_list); 2546 } 2547 2548 mutex_unlock(&cpufreq_governor_mutex); 2549 return err; 2550 } 2551 EXPORT_SYMBOL_GPL(cpufreq_register_governor); 2552 2553 void cpufreq_unregister_governor(struct cpufreq_governor *governor) 2554 { 2555 struct cpufreq_policy *policy; 2556 unsigned long flags; 2557 2558 if (!governor) 2559 return; 2560 2561 if (cpufreq_disabled()) 2562 return; 2563 2564 /* clear last_governor for all inactive policies */ 2565 read_lock_irqsave(&cpufreq_driver_lock, flags); 2566 for_each_inactive_policy(policy) { 2567 if (!strcmp(policy->last_governor, governor->name)) { 2568 policy->governor = NULL; 2569 strcpy(policy->last_governor, "\0"); 2570 } 2571 } 2572 read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2573 2574 mutex_lock(&cpufreq_governor_mutex); 2575 list_del(&governor->governor_list); 2576 mutex_unlock(&cpufreq_governor_mutex); 2577 } 2578 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 2579 2580 2581 /********************************************************************* 2582 * POLICY INTERFACE * 2583 *********************************************************************/ 2584 2585 /** 2586 * cpufreq_get_policy - get the current cpufreq_policy 2587 * @policy: struct cpufreq_policy into which the current cpufreq_policy 2588 * is written 2589 * @cpu: CPU to find the policy for 2590 * 2591 * Reads the current cpufreq policy. 2592 */ 2593 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 2594 { 2595 struct cpufreq_policy *cpu_policy; 2596 if (!policy) 2597 return -EINVAL; 2598 2599 cpu_policy = cpufreq_cpu_get(cpu); 2600 if (!cpu_policy) 2601 return -EINVAL; 2602 2603 memcpy(policy, cpu_policy, sizeof(*policy)); 2604 2605 cpufreq_cpu_put(cpu_policy); 2606 return 0; 2607 } 2608 EXPORT_SYMBOL(cpufreq_get_policy); 2609 2610 DEFINE_PER_CPU(unsigned long, cpufreq_pressure); 2611 2612 /** 2613 * cpufreq_update_pressure() - Update cpufreq pressure for CPUs 2614 * @policy: cpufreq policy of the CPUs. 2615 * 2616 * Update the value of cpufreq pressure for all @cpus in the policy. 2617 */ 2618 static void cpufreq_update_pressure(struct cpufreq_policy *policy) 2619 { 2620 unsigned long max_capacity, capped_freq, pressure; 2621 u32 max_freq; 2622 int cpu; 2623 2624 cpu = cpumask_first(policy->related_cpus); 2625 max_freq = arch_scale_freq_ref(cpu); 2626 capped_freq = policy->max; 2627 2628 /* 2629 * Handle properly the boost frequencies, which should simply clean 2630 * the cpufreq pressure value. 2631 */ 2632 if (max_freq <= capped_freq) { 2633 pressure = 0; 2634 } else { 2635 max_capacity = arch_scale_cpu_capacity(cpu); 2636 pressure = max_capacity - 2637 mult_frac(max_capacity, capped_freq, max_freq); 2638 } 2639 2640 for_each_cpu(cpu, policy->related_cpus) 2641 WRITE_ONCE(per_cpu(cpufreq_pressure, cpu), pressure); 2642 } 2643 2644 /** 2645 * cpufreq_set_policy - Modify cpufreq policy parameters. 2646 * @policy: Policy object to modify. 2647 * @new_gov: Policy governor pointer. 2648 * @new_pol: Policy value (for drivers with built-in governors). 2649 * 2650 * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency 2651 * limits to be set for the policy, update @policy with the verified limits 2652 * values and either invoke the driver's ->setpolicy() callback (if present) or 2653 * carry out a governor update for @policy. That is, run the current governor's 2654 * ->limits() callback (if @new_gov points to the same object as the one in 2655 * @policy) or replace the governor for @policy with @new_gov. 2656 * 2657 * The cpuinfo part of @policy is not updated by this function. 2658 */ 2659 static int cpufreq_set_policy(struct cpufreq_policy *policy, 2660 struct cpufreq_governor *new_gov, 2661 unsigned int new_pol) 2662 { 2663 struct cpufreq_policy_data new_data; 2664 struct cpufreq_governor *old_gov; 2665 int ret; 2666 2667 memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 2668 new_data.freq_table = policy->freq_table; 2669 new_data.cpu = policy->cpu; 2670 /* 2671 * PM QoS framework collects all the requests from users and provide us 2672 * the final aggregated value here. 2673 */ 2674 new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); 2675 new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); 2676 2677 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2678 new_data.cpu, new_data.min, new_data.max); 2679 2680 /* 2681 * Verify that the CPU speed can be set within these limits and make sure 2682 * that min <= max. 2683 */ 2684 ret = cpufreq_driver->verify(&new_data); 2685 if (ret) 2686 return ret; 2687 2688 /* 2689 * Resolve policy min/max to available frequencies. It ensures 2690 * no frequency resolution will neither overshoot the requested maximum 2691 * nor undershoot the requested minimum. 2692 */ 2693 policy->min = new_data.min; 2694 policy->max = new_data.max; 2695 policy->min = __resolve_freq(policy, policy->min, CPUFREQ_RELATION_L); 2696 policy->max = __resolve_freq(policy, policy->max, CPUFREQ_RELATION_H); 2697 trace_cpu_frequency_limits(policy); 2698 2699 cpufreq_update_pressure(policy); 2700 2701 policy->cached_target_freq = UINT_MAX; 2702 2703 pr_debug("new min and max freqs are %u - %u kHz\n", 2704 policy->min, policy->max); 2705 2706 if (cpufreq_driver->setpolicy) { 2707 policy->policy = new_pol; 2708 pr_debug("setting range\n"); 2709 return cpufreq_driver->setpolicy(policy); 2710 } 2711 2712 if (new_gov == policy->governor) { 2713 pr_debug("governor limits update\n"); 2714 cpufreq_governor_limits(policy); 2715 return 0; 2716 } 2717 2718 pr_debug("governor switch\n"); 2719 2720 /* save old, working values */ 2721 old_gov = policy->governor; 2722 /* end old governor */ 2723 if (old_gov) { 2724 cpufreq_stop_governor(policy); 2725 cpufreq_exit_governor(policy); 2726 } 2727 2728 /* start new governor */ 2729 policy->governor = new_gov; 2730 ret = cpufreq_init_governor(policy); 2731 if (!ret) { 2732 ret = cpufreq_start_governor(policy); 2733 if (!ret) { 2734 pr_debug("governor change\n"); 2735 return 0; 2736 } 2737 cpufreq_exit_governor(policy); 2738 } 2739 2740 /* new governor failed, so re-start old one */ 2741 pr_debug("starting governor %s failed\n", policy->governor->name); 2742 if (old_gov) { 2743 policy->governor = old_gov; 2744 if (cpufreq_init_governor(policy)) 2745 policy->governor = NULL; 2746 else 2747 cpufreq_start_governor(policy); 2748 } 2749 2750 return ret; 2751 } 2752 2753 /** 2754 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. 2755 * @cpu: CPU to re-evaluate the policy for. 2756 * 2757 * Update the current frequency for the cpufreq policy of @cpu and use 2758 * cpufreq_set_policy() to re-apply the min and max limits, which triggers the 2759 * evaluation of policy notifiers and the cpufreq driver's ->verify() callback 2760 * for the policy in question, among other things. 2761 */ 2762 void cpufreq_update_policy(unsigned int cpu) 2763 { 2764 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 2765 2766 if (!policy) 2767 return; 2768 2769 /* 2770 * BIOS might change freq behind our back 2771 * -> ask driver for current freq and notify governors about a change 2772 */ 2773 if (cpufreq_driver->get && has_target() && 2774 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) 2775 goto unlock; 2776 2777 refresh_frequency_limits(policy); 2778 2779 unlock: 2780 cpufreq_cpu_release(policy); 2781 } 2782 EXPORT_SYMBOL(cpufreq_update_policy); 2783 2784 /** 2785 * cpufreq_update_limits - Update policy limits for a given CPU. 2786 * @cpu: CPU to update the policy limits for. 2787 * 2788 * Invoke the driver's ->update_limits callback if present or call 2789 * cpufreq_update_policy() for @cpu. 2790 */ 2791 void cpufreq_update_limits(unsigned int cpu) 2792 { 2793 if (cpufreq_driver->update_limits) 2794 cpufreq_driver->update_limits(cpu); 2795 else 2796 cpufreq_update_policy(cpu); 2797 } 2798 EXPORT_SYMBOL_GPL(cpufreq_update_limits); 2799 2800 /********************************************************************* 2801 * BOOST * 2802 *********************************************************************/ 2803 static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) 2804 { 2805 int ret; 2806 2807 if (!policy->freq_table) 2808 return -ENXIO; 2809 2810 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); 2811 if (ret) { 2812 pr_err("%s: Policy frequency update failed\n", __func__); 2813 return ret; 2814 } 2815 2816 ret = freq_qos_update_request(policy->max_freq_req, policy->max); 2817 if (ret < 0) 2818 return ret; 2819 2820 return 0; 2821 } 2822 2823 int cpufreq_boost_trigger_state(int state) 2824 { 2825 struct cpufreq_policy *policy; 2826 unsigned long flags; 2827 int ret = 0; 2828 2829 if (cpufreq_driver->boost_enabled == state) 2830 return 0; 2831 2832 write_lock_irqsave(&cpufreq_driver_lock, flags); 2833 cpufreq_driver->boost_enabled = state; 2834 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2835 2836 cpus_read_lock(); 2837 for_each_active_policy(policy) { 2838 policy->boost_enabled = state; 2839 ret = cpufreq_driver->set_boost(policy, state); 2840 if (ret) { 2841 policy->boost_enabled = !policy->boost_enabled; 2842 goto err_reset_state; 2843 } 2844 } 2845 cpus_read_unlock(); 2846 2847 return 0; 2848 2849 err_reset_state: 2850 cpus_read_unlock(); 2851 2852 write_lock_irqsave(&cpufreq_driver_lock, flags); 2853 cpufreq_driver->boost_enabled = !state; 2854 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2855 2856 pr_err("%s: Cannot %s BOOST\n", 2857 __func__, str_enable_disable(state)); 2858 2859 return ret; 2860 } 2861 2862 static bool cpufreq_boost_supported(void) 2863 { 2864 return cpufreq_driver->set_boost; 2865 } 2866 2867 static int create_boost_sysfs_file(void) 2868 { 2869 int ret; 2870 2871 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 2872 if (ret) 2873 pr_err("%s: cannot register global BOOST sysfs file\n", 2874 __func__); 2875 2876 return ret; 2877 } 2878 2879 static void remove_boost_sysfs_file(void) 2880 { 2881 if (cpufreq_boost_supported()) 2882 sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 2883 } 2884 2885 int cpufreq_enable_boost_support(void) 2886 { 2887 if (!cpufreq_driver) 2888 return -EINVAL; 2889 2890 if (cpufreq_boost_supported()) 2891 return 0; 2892 2893 cpufreq_driver->set_boost = cpufreq_boost_set_sw; 2894 2895 /* This will get removed on driver unregister */ 2896 return create_boost_sysfs_file(); 2897 } 2898 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 2899 2900 bool cpufreq_boost_enabled(void) 2901 { 2902 return cpufreq_driver->boost_enabled; 2903 } 2904 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 2905 2906 /********************************************************************* 2907 * REGISTER / UNREGISTER CPUFREQ DRIVER * 2908 *********************************************************************/ 2909 static enum cpuhp_state hp_online; 2910 2911 static int cpuhp_cpufreq_online(unsigned int cpu) 2912 { 2913 cpufreq_online(cpu); 2914 2915 return 0; 2916 } 2917 2918 static int cpuhp_cpufreq_offline(unsigned int cpu) 2919 { 2920 cpufreq_offline(cpu); 2921 2922 return 0; 2923 } 2924 2925 /** 2926 * cpufreq_register_driver - register a CPU Frequency driver 2927 * @driver_data: A struct cpufreq_driver containing the values# 2928 * submitted by the CPU Frequency driver. 2929 * 2930 * Registers a CPU Frequency driver to this core code. This code 2931 * returns zero on success, -EEXIST when another driver got here first 2932 * (and isn't unregistered in the meantime). 2933 * 2934 */ 2935 int cpufreq_register_driver(struct cpufreq_driver *driver_data) 2936 { 2937 unsigned long flags; 2938 int ret; 2939 2940 if (cpufreq_disabled()) 2941 return -ENODEV; 2942 2943 /* 2944 * The cpufreq core depends heavily on the availability of device 2945 * structure, make sure they are available before proceeding further. 2946 */ 2947 if (!get_cpu_device(0)) 2948 return -EPROBE_DEFER; 2949 2950 if (!driver_data || !driver_data->verify || !driver_data->init || 2951 !(driver_data->setpolicy || driver_data->target_index || 2952 driver_data->target) || 2953 (driver_data->setpolicy && (driver_data->target_index || 2954 driver_data->target)) || 2955 (!driver_data->get_intermediate != !driver_data->target_intermediate) || 2956 (!driver_data->online != !driver_data->offline) || 2957 (driver_data->adjust_perf && !driver_data->fast_switch)) 2958 return -EINVAL; 2959 2960 pr_debug("trying to register driver %s\n", driver_data->name); 2961 2962 /* Protect against concurrent CPU online/offline. */ 2963 cpus_read_lock(); 2964 2965 write_lock_irqsave(&cpufreq_driver_lock, flags); 2966 if (cpufreq_driver) { 2967 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2968 ret = -EEXIST; 2969 goto out; 2970 } 2971 cpufreq_driver = driver_data; 2972 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2973 2974 /* 2975 * Mark support for the scheduler's frequency invariance engine for 2976 * drivers that implement target(), target_index() or fast_switch(). 2977 */ 2978 if (!cpufreq_driver->setpolicy) { 2979 static_branch_enable_cpuslocked(&cpufreq_freq_invariance); 2980 pr_debug("supports frequency invariance"); 2981 } 2982 2983 if (driver_data->setpolicy) 2984 driver_data->flags |= CPUFREQ_CONST_LOOPS; 2985 2986 if (cpufreq_boost_supported()) { 2987 ret = create_boost_sysfs_file(); 2988 if (ret) 2989 goto err_null_driver; 2990 } 2991 2992 ret = subsys_interface_register(&cpufreq_interface); 2993 if (ret) 2994 goto err_boost_unreg; 2995 2996 if (unlikely(list_empty(&cpufreq_policy_list))) { 2997 /* if all ->init() calls failed, unregister */ 2998 ret = -ENODEV; 2999 pr_debug("%s: No CPU initialized for driver %s\n", __func__, 3000 driver_data->name); 3001 goto err_if_unreg; 3002 } 3003 3004 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 3005 "cpufreq:online", 3006 cpuhp_cpufreq_online, 3007 cpuhp_cpufreq_offline); 3008 if (ret < 0) 3009 goto err_if_unreg; 3010 hp_online = ret; 3011 ret = 0; 3012 3013 pr_debug("driver %s up and running\n", driver_data->name); 3014 goto out; 3015 3016 err_if_unreg: 3017 subsys_interface_unregister(&cpufreq_interface); 3018 err_boost_unreg: 3019 remove_boost_sysfs_file(); 3020 err_null_driver: 3021 write_lock_irqsave(&cpufreq_driver_lock, flags); 3022 cpufreq_driver = NULL; 3023 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 3024 out: 3025 cpus_read_unlock(); 3026 return ret; 3027 } 3028 EXPORT_SYMBOL_GPL(cpufreq_register_driver); 3029 3030 /* 3031 * cpufreq_unregister_driver - unregister the current CPUFreq driver 3032 * 3033 * Unregister the current CPUFreq driver. Only call this if you have 3034 * the right to do so, i.e. if you have succeeded in initialising before! 3035 * Returns zero if successful, and -EINVAL if the cpufreq_driver is 3036 * currently not initialised. 3037 */ 3038 void cpufreq_unregister_driver(struct cpufreq_driver *driver) 3039 { 3040 unsigned long flags; 3041 3042 if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver))) 3043 return; 3044 3045 pr_debug("unregistering driver %s\n", driver->name); 3046 3047 /* Protect against concurrent cpu hotplug */ 3048 cpus_read_lock(); 3049 subsys_interface_unregister(&cpufreq_interface); 3050 remove_boost_sysfs_file(); 3051 static_branch_disable_cpuslocked(&cpufreq_freq_invariance); 3052 cpuhp_remove_state_nocalls_cpuslocked(hp_online); 3053 3054 write_lock_irqsave(&cpufreq_driver_lock, flags); 3055 3056 cpufreq_driver = NULL; 3057 3058 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 3059 cpus_read_unlock(); 3060 } 3061 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 3062 3063 static int __init cpufreq_core_init(void) 3064 { 3065 struct cpufreq_governor *gov = cpufreq_default_governor(); 3066 struct device *dev_root; 3067 3068 if (cpufreq_disabled()) 3069 return -ENODEV; 3070 3071 dev_root = bus_get_dev_root(&cpu_subsys); 3072 if (dev_root) { 3073 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj); 3074 put_device(dev_root); 3075 } 3076 BUG_ON(!cpufreq_global_kobject); 3077 3078 if (!strlen(default_governor)) 3079 strscpy(default_governor, gov->name, CPUFREQ_NAME_LEN); 3080 3081 return 0; 3082 } 3083 module_param(off, int, 0444); 3084 module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); 3085 core_initcall(cpufreq_core_init); 3086