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