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