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