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