| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * linux/drivers/cpufreq/cpufreq.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 9 | 10 | * Added handling for CPU hotplug |
|---|
| 10 | 11 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
|---|
| 11 | 12 | * Fix handling for CPU hotplug -- affected CPUs |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 15 | | - * published by the Free Software Foundation. |
|---|
| 16 | 13 | */ |
|---|
| 17 | 14 | |
|---|
| 18 | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| .. | .. |
|---|
| 27 | 24 | #include <linux/kernel_stat.h> |
|---|
| 28 | 25 | #include <linux/module.h> |
|---|
| 29 | 26 | #include <linux/mutex.h> |
|---|
| 27 | +#include <linux/pm_qos.h> |
|---|
| 30 | 28 | #include <linux/slab.h> |
|---|
| 31 | 29 | #include <linux/suspend.h> |
|---|
| 32 | 30 | #include <linux/syscore_ops.h> |
|---|
| 33 | 31 | #include <linux/tick.h> |
|---|
| 34 | 32 | #include <trace/events/power.h> |
|---|
| 35 | | - |
|---|
| 36 | | -#if IS_ENABLED(CONFIG_ARM_ROCKCHIP_CPUFREQ) |
|---|
| 37 | | -#include "rockchip-cpufreq.h" |
|---|
| 38 | | -#endif |
|---|
| 33 | +#include <trace/hooks/cpufreq.h> |
|---|
| 39 | 34 | |
|---|
| 40 | 35 | static LIST_HEAD(cpufreq_policy_list); |
|---|
| 41 | | - |
|---|
| 42 | | -static inline bool policy_is_inactive(struct cpufreq_policy *policy) |
|---|
| 43 | | -{ |
|---|
| 44 | | - return cpumask_empty(policy->cpus); |
|---|
| 45 | | -} |
|---|
| 46 | 36 | |
|---|
| 47 | 37 | /* Macros to iterate over CPU policies */ |
|---|
| 48 | 38 | #define for_each_suitable_policy(__policy, __active) \ |
|---|
| .. | .. |
|---|
| 62 | 52 | #define for_each_governor(__governor) \ |
|---|
| 63 | 53 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) |
|---|
| 64 | 54 | |
|---|
| 65 | | -/** |
|---|
| 55 | +static char default_governor[CPUFREQ_NAME_LEN]; |
|---|
| 56 | + |
|---|
| 57 | +/* |
|---|
| 66 | 58 | * The "cpufreq driver" - the arch- or hardware-dependent low |
|---|
| 67 | 59 | * level driver of CPUFreq support, and its spinlock. This lock |
|---|
| 68 | 60 | * also protects the cpufreq_cpu_data array. |
|---|
| .. | .. |
|---|
| 70 | 62 | static struct cpufreq_driver *cpufreq_driver; |
|---|
| 71 | 63 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
|---|
| 72 | 64 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
|---|
| 65 | + |
|---|
| 66 | +static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance); |
|---|
| 67 | +bool cpufreq_supports_freq_invariance(void) |
|---|
| 68 | +{ |
|---|
| 69 | + return static_branch_likely(&cpufreq_freq_invariance); |
|---|
| 70 | +} |
|---|
| 73 | 71 | |
|---|
| 74 | 72 | /* Flag to suspend/resume CPUFreq governors */ |
|---|
| 75 | 73 | static bool cpufreq_suspended; |
|---|
| .. | .. |
|---|
| 83 | 81 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
|---|
| 84 | 82 | static int cpufreq_init_governor(struct cpufreq_policy *policy); |
|---|
| 85 | 83 | static void cpufreq_exit_governor(struct cpufreq_policy *policy); |
|---|
| 86 | | -static int cpufreq_start_governor(struct cpufreq_policy *policy); |
|---|
| 87 | | -static void cpufreq_stop_governor(struct cpufreq_policy *policy); |
|---|
| 88 | 84 | static void cpufreq_governor_limits(struct cpufreq_policy *policy); |
|---|
| 85 | +static int cpufreq_set_policy(struct cpufreq_policy *policy, |
|---|
| 86 | + struct cpufreq_governor *new_gov, |
|---|
| 87 | + unsigned int new_pol); |
|---|
| 89 | 88 | |
|---|
| 90 | | -/** |
|---|
| 89 | +/* |
|---|
| 91 | 90 | * Two notifier lists: the "policy" list is involved in the |
|---|
| 92 | 91 | * validation process for a new CPU frequency policy; the |
|---|
| 93 | 92 | * "transition" list for kernel code that needs to handle |
|---|
| .. | .. |
|---|
| 114 | 113 | } |
|---|
| 115 | 114 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
|---|
| 116 | 115 | |
|---|
| 116 | +static struct kobject *cpufreq_global_kobject; |
|---|
| 117 | + |
|---|
| 117 | 118 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
|---|
| 118 | 119 | { |
|---|
| 119 | 120 | if (have_governor_per_policy()) |
|---|
| .. | .. |
|---|
| 125 | 126 | |
|---|
| 126 | 127 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
|---|
| 127 | 128 | { |
|---|
| 128 | | - u64 idle_time; |
|---|
| 129 | + struct kernel_cpustat kcpustat; |
|---|
| 129 | 130 | u64 cur_wall_time; |
|---|
| 131 | + u64 idle_time; |
|---|
| 130 | 132 | u64 busy_time; |
|---|
| 131 | 133 | |
|---|
| 132 | 134 | cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); |
|---|
| 133 | 135 | |
|---|
| 134 | | - busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; |
|---|
| 135 | | - busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; |
|---|
| 136 | | - busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; |
|---|
| 137 | | - busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; |
|---|
| 138 | | - busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; |
|---|
| 139 | | - busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; |
|---|
| 136 | + kcpustat_cpu_fetch(&kcpustat, cpu); |
|---|
| 137 | + |
|---|
| 138 | + busy_time = kcpustat.cpustat[CPUTIME_USER]; |
|---|
| 139 | + busy_time += kcpustat.cpustat[CPUTIME_SYSTEM]; |
|---|
| 140 | + busy_time += kcpustat.cpustat[CPUTIME_IRQ]; |
|---|
| 141 | + busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ]; |
|---|
| 142 | + busy_time += kcpustat.cpustat[CPUTIME_STEAL]; |
|---|
| 143 | + busy_time += kcpustat.cpustat[CPUTIME_NICE]; |
|---|
| 140 | 144 | |
|---|
| 141 | 145 | idle_time = cur_wall_time - busy_time; |
|---|
| 142 | 146 | if (wall) |
|---|
| .. | .. |
|---|
| 158 | 162 | } |
|---|
| 159 | 163 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); |
|---|
| 160 | 164 | |
|---|
| 161 | | -__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
|---|
| 162 | | - unsigned long max_freq) |
|---|
| 163 | | -{ |
|---|
| 164 | | -} |
|---|
| 165 | | -EXPORT_SYMBOL_GPL(arch_set_freq_scale); |
|---|
| 166 | | - |
|---|
| 167 | | -__weak void arch_set_max_freq_scale(struct cpumask *cpus, |
|---|
| 168 | | - unsigned long policy_max_freq) |
|---|
| 169 | | -{ |
|---|
| 170 | | -} |
|---|
| 171 | | -EXPORT_SYMBOL_GPL(arch_set_max_freq_scale); |
|---|
| 172 | | - |
|---|
| 173 | 165 | /* |
|---|
| 174 | 166 | * This is a generic cpufreq init() routine which can be used by cpufreq |
|---|
| 175 | 167 | * drivers of SMP systems. It will do following: |
|---|
| .. | .. |
|---|
| 177 | 169 | * - set policies transition latency |
|---|
| 178 | 170 | * - policy->cpus with all possible CPUs |
|---|
| 179 | 171 | */ |
|---|
| 180 | | -int cpufreq_generic_init(struct cpufreq_policy *policy, |
|---|
| 172 | +void cpufreq_generic_init(struct cpufreq_policy *policy, |
|---|
| 181 | 173 | struct cpufreq_frequency_table *table, |
|---|
| 182 | 174 | unsigned int transition_latency) |
|---|
| 183 | 175 | { |
|---|
| .. | .. |
|---|
| 189 | 181 | * share the clock and voltage and clock. |
|---|
| 190 | 182 | */ |
|---|
| 191 | 183 | cpumask_setall(policy->cpus); |
|---|
| 192 | | - |
|---|
| 193 | | - return 0; |
|---|
| 194 | 184 | } |
|---|
| 195 | 185 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); |
|---|
| 196 | 186 | |
|---|
| .. | .. |
|---|
| 217 | 207 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); |
|---|
| 218 | 208 | |
|---|
| 219 | 209 | /** |
|---|
| 220 | | - * cpufreq_cpu_get: returns policy for a cpu and marks it busy. |
|---|
| 210 | + * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. |
|---|
| 211 | + * @cpu: CPU to find the policy for. |
|---|
| 221 | 212 | * |
|---|
| 222 | | - * @cpu: cpu to find policy for. |
|---|
| 213 | + * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment |
|---|
| 214 | + * the kobject reference counter of that policy. Return a valid policy on |
|---|
| 215 | + * success or NULL on failure. |
|---|
| 223 | 216 | * |
|---|
| 224 | | - * This returns policy for 'cpu', returns NULL if it doesn't exist. |
|---|
| 225 | | - * It also increments the kobject reference count to mark it busy and so would |
|---|
| 226 | | - * require a corresponding call to cpufreq_cpu_put() to decrement it back. |
|---|
| 227 | | - * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be |
|---|
| 228 | | - * freed as that depends on the kobj count. |
|---|
| 229 | | - * |
|---|
| 230 | | - * Return: A valid policy on success, otherwise NULL on failure. |
|---|
| 217 | + * The policy returned by this function has to be released with the help of |
|---|
| 218 | + * cpufreq_cpu_put() to balance its kobject reference counter properly. |
|---|
| 231 | 219 | */ |
|---|
| 232 | 220 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
|---|
| 233 | 221 | { |
|---|
| .. | .. |
|---|
| 254 | 242 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
|---|
| 255 | 243 | |
|---|
| 256 | 244 | /** |
|---|
| 257 | | - * cpufreq_cpu_put: Decrements the usage count of a policy |
|---|
| 258 | | - * |
|---|
| 259 | | - * @policy: policy earlier returned by cpufreq_cpu_get(). |
|---|
| 260 | | - * |
|---|
| 261 | | - * This decrements the kobject reference count incremented earlier by calling |
|---|
| 262 | | - * cpufreq_cpu_get(). |
|---|
| 245 | + * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. |
|---|
| 246 | + * @policy: cpufreq policy returned by cpufreq_cpu_get(). |
|---|
| 263 | 247 | */ |
|---|
| 264 | 248 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
|---|
| 265 | 249 | { |
|---|
| .. | .. |
|---|
| 267 | 251 | } |
|---|
| 268 | 252 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); |
|---|
| 269 | 253 | |
|---|
| 254 | +/** |
|---|
| 255 | + * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. |
|---|
| 256 | + * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). |
|---|
| 257 | + */ |
|---|
| 258 | +void cpufreq_cpu_release(struct cpufreq_policy *policy) |
|---|
| 259 | +{ |
|---|
| 260 | + if (WARN_ON(!policy)) |
|---|
| 261 | + return; |
|---|
| 262 | + |
|---|
| 263 | + lockdep_assert_held(&policy->rwsem); |
|---|
| 264 | + |
|---|
| 265 | + up_write(&policy->rwsem); |
|---|
| 266 | + |
|---|
| 267 | + cpufreq_cpu_put(policy); |
|---|
| 268 | +} |
|---|
| 269 | + |
|---|
| 270 | +/** |
|---|
| 271 | + * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. |
|---|
| 272 | + * @cpu: CPU to find the policy for. |
|---|
| 273 | + * |
|---|
| 274 | + * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and |
|---|
| 275 | + * if the policy returned by it is not NULL, acquire its rwsem for writing. |
|---|
| 276 | + * Return the policy if it is active or release it and return NULL otherwise. |
|---|
| 277 | + * |
|---|
| 278 | + * The policy returned by this function has to be released with the help of |
|---|
| 279 | + * cpufreq_cpu_release() in order to release its rwsem and balance its usage |
|---|
| 280 | + * counter properly. |
|---|
| 281 | + */ |
|---|
| 282 | +struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) |
|---|
| 283 | +{ |
|---|
| 284 | + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
|---|
| 285 | + |
|---|
| 286 | + if (!policy) |
|---|
| 287 | + return NULL; |
|---|
| 288 | + |
|---|
| 289 | + down_write(&policy->rwsem); |
|---|
| 290 | + |
|---|
| 291 | + if (policy_is_inactive(policy)) { |
|---|
| 292 | + cpufreq_cpu_release(policy); |
|---|
| 293 | + return NULL; |
|---|
| 294 | + } |
|---|
| 295 | + |
|---|
| 296 | + return policy; |
|---|
| 297 | +} |
|---|
| 298 | + |
|---|
| 270 | 299 | /********************************************************************* |
|---|
| 271 | 300 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * |
|---|
| 272 | 301 | *********************************************************************/ |
|---|
| 273 | 302 | |
|---|
| 274 | | -/** |
|---|
| 303 | +/* |
|---|
| 275 | 304 | * adjust_jiffies - adjust the system "loops_per_jiffy" |
|---|
| 276 | 305 | * |
|---|
| 277 | 306 | * This function alters the system "loops_per_jiffy" for the clock |
|---|
| .. | .. |
|---|
| 317 | 346 | struct cpufreq_freqs *freqs, |
|---|
| 318 | 347 | unsigned int state) |
|---|
| 319 | 348 | { |
|---|
| 349 | + int cpu; |
|---|
| 350 | + |
|---|
| 320 | 351 | BUG_ON(irqs_disabled()); |
|---|
| 321 | 352 | |
|---|
| 322 | 353 | if (cpufreq_disabled()) |
|---|
| 323 | 354 | return; |
|---|
| 324 | 355 | |
|---|
| 356 | + freqs->policy = policy; |
|---|
| 325 | 357 | freqs->flags = cpufreq_driver->flags; |
|---|
| 326 | 358 | pr_debug("notification %u of frequency transition to %u kHz\n", |
|---|
| 327 | 359 | state, freqs->new); |
|---|
| .. | .. |
|---|
| 333 | 365 | * which is not equal to what the cpufreq core thinks is |
|---|
| 334 | 366 | * "old frequency". |
|---|
| 335 | 367 | */ |
|---|
| 336 | | - if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
|---|
| 337 | | - if (policy->cur && (policy->cur != freqs->old)) { |
|---|
| 338 | | - pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", |
|---|
| 339 | | - freqs->old, policy->cur); |
|---|
| 340 | | - freqs->old = policy->cur; |
|---|
| 341 | | - } |
|---|
| 368 | + if (policy->cur && policy->cur != freqs->old) { |
|---|
| 369 | + pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", |
|---|
| 370 | + freqs->old, policy->cur); |
|---|
| 371 | + freqs->old = policy->cur; |
|---|
| 342 | 372 | } |
|---|
| 343 | 373 | |
|---|
| 344 | | - for_each_cpu(freqs->cpu, policy->cpus) { |
|---|
| 345 | | - srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
|---|
| 346 | | - CPUFREQ_PRECHANGE, freqs); |
|---|
| 347 | | - } |
|---|
| 374 | + srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
|---|
| 375 | + CPUFREQ_PRECHANGE, freqs); |
|---|
| 348 | 376 | |
|---|
| 349 | 377 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
|---|
| 350 | 378 | break; |
|---|
| .. | .. |
|---|
| 354 | 382 | pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, |
|---|
| 355 | 383 | cpumask_pr_args(policy->cpus)); |
|---|
| 356 | 384 | |
|---|
| 357 | | - for_each_cpu(freqs->cpu, policy->cpus) { |
|---|
| 358 | | - trace_cpu_frequency(freqs->new, freqs->cpu); |
|---|
| 359 | | - srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
|---|
| 360 | | - CPUFREQ_POSTCHANGE, freqs); |
|---|
| 361 | | - } |
|---|
| 385 | + for_each_cpu(cpu, policy->cpus) |
|---|
| 386 | + trace_cpu_frequency(freqs->new, cpu); |
|---|
| 387 | + |
|---|
| 388 | + srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
|---|
| 389 | + CPUFREQ_POSTCHANGE, freqs); |
|---|
| 362 | 390 | |
|---|
| 363 | 391 | cpufreq_stats_record_transition(policy, freqs->new); |
|---|
| 364 | 392 | cpufreq_times_record_transition(policy, freqs->new); |
|---|
| 365 | 393 | policy->cur = freqs->new; |
|---|
| 394 | + trace_android_rvh_cpufreq_transition(policy); |
|---|
| 366 | 395 | } |
|---|
| 367 | 396 | } |
|---|
| 368 | 397 | |
|---|
| .. | .. |
|---|
| 416 | 445 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, |
|---|
| 417 | 446 | struct cpufreq_freqs *freqs, int transition_failed) |
|---|
| 418 | 447 | { |
|---|
| 419 | | - if (unlikely(WARN_ON(!policy->transition_ongoing))) |
|---|
| 448 | + if (WARN_ON(!policy->transition_ongoing)) |
|---|
| 420 | 449 | return; |
|---|
| 421 | 450 | |
|---|
| 422 | 451 | cpufreq_notify_post_transition(policy, freqs, transition_failed); |
|---|
| 423 | 452 | |
|---|
| 453 | + arch_set_freq_scale(policy->related_cpus, |
|---|
| 454 | + policy->cur, |
|---|
| 455 | + policy->cpuinfo.max_freq); |
|---|
| 456 | + |
|---|
| 457 | + spin_lock(&policy->transition_lock); |
|---|
| 424 | 458 | policy->transition_ongoing = false; |
|---|
| 425 | 459 | policy->transition_task = NULL; |
|---|
| 460 | + spin_unlock(&policy->transition_lock); |
|---|
| 426 | 461 | |
|---|
| 427 | 462 | wake_up(&policy->transition_wait); |
|---|
| 428 | 463 | } |
|---|
| .. | .. |
|---|
| 444 | 479 | mutex_lock(&cpufreq_transition_notifier_list.mutex); |
|---|
| 445 | 480 | |
|---|
| 446 | 481 | for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) |
|---|
| 447 | | - pr_info("%pF\n", nb->notifier_call); |
|---|
| 482 | + pr_info("%pS\n", nb->notifier_call); |
|---|
| 448 | 483 | |
|---|
| 449 | 484 | mutex_unlock(&cpufreq_transition_notifier_list.mutex); |
|---|
| 450 | 485 | } |
|---|
| .. | .. |
|---|
| 499 | 534 | /** |
|---|
| 500 | 535 | * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported |
|---|
| 501 | 536 | * one. |
|---|
| 537 | + * @policy: associated policy to interrogate |
|---|
| 502 | 538 | * @target_freq: target frequency to resolve. |
|---|
| 503 | 539 | * |
|---|
| 504 | 540 | * The target to driver frequency mapping is cached in the policy. |
|---|
| .. | .. |
|---|
| 509 | 545 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, |
|---|
| 510 | 546 | unsigned int target_freq) |
|---|
| 511 | 547 | { |
|---|
| 548 | + unsigned int old_target_freq = target_freq; |
|---|
| 549 | + |
|---|
| 512 | 550 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
|---|
| 551 | + trace_android_vh_cpufreq_resolve_freq(policy, target_freq, old_target_freq); |
|---|
| 513 | 552 | policy->cached_target_freq = target_freq; |
|---|
| 514 | 553 | |
|---|
| 515 | 554 | if (cpufreq_driver->target_index) { |
|---|
| 516 | | - int idx; |
|---|
| 555 | + unsigned int idx; |
|---|
| 517 | 556 | |
|---|
| 518 | 557 | idx = cpufreq_frequency_table_target(policy, target_freq, |
|---|
| 519 | 558 | CPUFREQ_RELATION_L); |
|---|
| .. | .. |
|---|
| 596 | 635 | return NULL; |
|---|
| 597 | 636 | } |
|---|
| 598 | 637 | |
|---|
| 599 | | -/** |
|---|
| 600 | | - * cpufreq_parse_governor - parse a governor string |
|---|
| 601 | | - */ |
|---|
| 602 | | -static int cpufreq_parse_governor(char *str_governor, |
|---|
| 603 | | - struct cpufreq_policy *policy) |
|---|
| 638 | +static struct cpufreq_governor *get_governor(const char *str_governor) |
|---|
| 604 | 639 | { |
|---|
| 605 | | - if (cpufreq_driver->setpolicy) { |
|---|
| 606 | | - if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { |
|---|
| 607 | | - policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
|---|
| 608 | | - return 0; |
|---|
| 609 | | - } |
|---|
| 640 | + struct cpufreq_governor *t; |
|---|
| 610 | 641 | |
|---|
| 611 | | - if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { |
|---|
| 612 | | - policy->policy = CPUFREQ_POLICY_POWERSAVE; |
|---|
| 613 | | - return 0; |
|---|
| 614 | | - } |
|---|
| 615 | | - } else { |
|---|
| 616 | | - struct cpufreq_governor *t; |
|---|
| 642 | + mutex_lock(&cpufreq_governor_mutex); |
|---|
| 643 | + t = find_governor(str_governor); |
|---|
| 644 | + if (!t) |
|---|
| 645 | + goto unlock; |
|---|
| 617 | 646 | |
|---|
| 618 | | - mutex_lock(&cpufreq_governor_mutex); |
|---|
| 647 | + if (!try_module_get(t->owner)) |
|---|
| 648 | + t = NULL; |
|---|
| 619 | 649 | |
|---|
| 620 | | - t = find_governor(str_governor); |
|---|
| 621 | | - if (!t) { |
|---|
| 622 | | - int ret; |
|---|
| 650 | +unlock: |
|---|
| 651 | + mutex_unlock(&cpufreq_governor_mutex); |
|---|
| 623 | 652 | |
|---|
| 624 | | - mutex_unlock(&cpufreq_governor_mutex); |
|---|
| 653 | + return t; |
|---|
| 654 | +} |
|---|
| 625 | 655 | |
|---|
| 626 | | - ret = request_module("cpufreq_%s", str_governor); |
|---|
| 627 | | - if (ret) |
|---|
| 628 | | - return -EINVAL; |
|---|
| 656 | +static unsigned int cpufreq_parse_policy(char *str_governor) |
|---|
| 657 | +{ |
|---|
| 658 | + if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) |
|---|
| 659 | + return CPUFREQ_POLICY_PERFORMANCE; |
|---|
| 629 | 660 | |
|---|
| 630 | | - mutex_lock(&cpufreq_governor_mutex); |
|---|
| 661 | + if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) |
|---|
| 662 | + return CPUFREQ_POLICY_POWERSAVE; |
|---|
| 631 | 663 | |
|---|
| 632 | | - t = find_governor(str_governor); |
|---|
| 633 | | - } |
|---|
| 634 | | - if (t && !try_module_get(t->owner)) |
|---|
| 635 | | - t = NULL; |
|---|
| 636 | | - |
|---|
| 637 | | - mutex_unlock(&cpufreq_governor_mutex); |
|---|
| 638 | | - |
|---|
| 639 | | - if (t) { |
|---|
| 640 | | - policy->governor = t; |
|---|
| 641 | | - return 0; |
|---|
| 642 | | - } |
|---|
| 643 | | - } |
|---|
| 644 | | - |
|---|
| 645 | | - return -EINVAL; |
|---|
| 664 | + return CPUFREQ_POLICY_UNKNOWN; |
|---|
| 646 | 665 | } |
|---|
| 647 | 666 | |
|---|
| 648 | 667 | /** |
|---|
| 668 | + * cpufreq_parse_governor - parse a governor string only for has_target() |
|---|
| 669 | + * @str_governor: Governor name. |
|---|
| 670 | + */ |
|---|
| 671 | +static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor) |
|---|
| 672 | +{ |
|---|
| 673 | + struct cpufreq_governor *t; |
|---|
| 674 | + |
|---|
| 675 | + t = get_governor(str_governor); |
|---|
| 676 | + if (t) |
|---|
| 677 | + return t; |
|---|
| 678 | + |
|---|
| 679 | + if (request_module("cpufreq_%s", str_governor)) |
|---|
| 680 | + return NULL; |
|---|
| 681 | + |
|---|
| 682 | + return get_governor(str_governor); |
|---|
| 683 | +} |
|---|
| 684 | + |
|---|
| 685 | +/* |
|---|
| 649 | 686 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
|---|
| 650 | 687 | * print out cpufreq information |
|---|
| 651 | 688 | * |
|---|
| .. | .. |
|---|
| 660 | 697 | return sprintf(buf, "%u\n", policy->object); \ |
|---|
| 661 | 698 | } |
|---|
| 662 | 699 | |
|---|
| 700 | +static ssize_t show_cpuinfo_max_freq(struct cpufreq_policy *policy, char *buf) |
|---|
| 701 | +{ |
|---|
| 702 | + unsigned int max_freq = policy->cpuinfo.max_freq; |
|---|
| 703 | + |
|---|
| 704 | + trace_android_vh_show_max_freq(policy, &max_freq); |
|---|
| 705 | + trace_android_rvh_show_max_freq(policy, &max_freq); |
|---|
| 706 | + return sprintf(buf, "%u\n", max_freq); |
|---|
| 707 | +} |
|---|
| 708 | + |
|---|
| 663 | 709 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); |
|---|
| 664 | | -show_one(cpuinfo_max_freq, cpuinfo.max_freq); |
|---|
| 665 | 710 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
|---|
| 666 | 711 | show_one(scaling_min_freq, min); |
|---|
| 667 | 712 | show_one(scaling_max_freq, max); |
|---|
| .. | .. |
|---|
| 679 | 724 | freq = arch_freq_get_on_cpu(policy->cpu); |
|---|
| 680 | 725 | if (freq) |
|---|
| 681 | 726 | ret = sprintf(buf, "%u\n", freq); |
|---|
| 682 | | - else if (cpufreq_driver && cpufreq_driver->setpolicy && |
|---|
| 683 | | - cpufreq_driver->get) |
|---|
| 727 | + else if (cpufreq_driver->setpolicy && cpufreq_driver->get) |
|---|
| 684 | 728 | ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
|---|
| 685 | 729 | else |
|---|
| 686 | 730 | ret = sprintf(buf, "%u\n", policy->cur); |
|---|
| 687 | 731 | return ret; |
|---|
| 688 | 732 | } |
|---|
| 689 | 733 | |
|---|
| 690 | | -static int cpufreq_set_policy(struct cpufreq_policy *policy, |
|---|
| 691 | | - struct cpufreq_policy *new_policy); |
|---|
| 692 | | - |
|---|
| 693 | | -/** |
|---|
| 734 | +/* |
|---|
| 694 | 735 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access |
|---|
| 695 | 736 | */ |
|---|
| 696 | 737 | #define store_one(file_name, object) \ |
|---|
| 697 | 738 | static ssize_t store_##file_name \ |
|---|
| 698 | 739 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
|---|
| 699 | 740 | { \ |
|---|
| 700 | | - int ret, temp; \ |
|---|
| 701 | | - struct cpufreq_policy new_policy; \ |
|---|
| 741 | + unsigned long val; \ |
|---|
| 742 | + int ret; \ |
|---|
| 702 | 743 | \ |
|---|
| 703 | | - memcpy(&new_policy, policy, sizeof(*policy)); \ |
|---|
| 704 | | - new_policy.min = policy->user_policy.min; \ |
|---|
| 705 | | - new_policy.max = policy->user_policy.max; \ |
|---|
| 706 | | - \ |
|---|
| 707 | | - ret = sscanf(buf, "%u", &new_policy.object); \ |
|---|
| 744 | + ret = sscanf(buf, "%lu", &val); \ |
|---|
| 708 | 745 | if (ret != 1) \ |
|---|
| 709 | 746 | return -EINVAL; \ |
|---|
| 710 | 747 | \ |
|---|
| 711 | | - temp = new_policy.object; \ |
|---|
| 712 | | - ret = cpufreq_set_policy(policy, &new_policy); \ |
|---|
| 713 | | - if (!ret) \ |
|---|
| 714 | | - policy->user_policy.object = temp; \ |
|---|
| 715 | | - \ |
|---|
| 716 | | - return ret ? ret : count; \ |
|---|
| 748 | + ret = freq_qos_update_request(policy->object##_freq_req, val);\ |
|---|
| 749 | + return ret >= 0 ? count : ret; \ |
|---|
| 717 | 750 | } |
|---|
| 718 | 751 | |
|---|
| 719 | 752 | store_one(scaling_min_freq, min); |
|---|
| 720 | 753 | store_one(scaling_max_freq, max); |
|---|
| 721 | 754 | |
|---|
| 722 | | -/** |
|---|
| 755 | +/* |
|---|
| 723 | 756 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware |
|---|
| 724 | 757 | */ |
|---|
| 725 | 758 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
|---|
| .. | .. |
|---|
| 733 | 766 | return sprintf(buf, "<unknown>\n"); |
|---|
| 734 | 767 | } |
|---|
| 735 | 768 | |
|---|
| 736 | | -/** |
|---|
| 769 | +/* |
|---|
| 737 | 770 | * show_scaling_governor - show the current policy for the specified CPU |
|---|
| 738 | 771 | */ |
|---|
| 739 | 772 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
|---|
| .. | .. |
|---|
| 748 | 781 | return -EINVAL; |
|---|
| 749 | 782 | } |
|---|
| 750 | 783 | |
|---|
| 751 | | -/** |
|---|
| 784 | +/* |
|---|
| 752 | 785 | * store_scaling_governor - store policy for the specified CPU |
|---|
| 753 | 786 | */ |
|---|
| 754 | 787 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
|---|
| 755 | 788 | const char *buf, size_t count) |
|---|
| 756 | 789 | { |
|---|
| 790 | + char str_governor[16]; |
|---|
| 757 | 791 | int ret; |
|---|
| 758 | | - char str_governor[16]; |
|---|
| 759 | | - struct cpufreq_policy new_policy; |
|---|
| 760 | | - |
|---|
| 761 | | - memcpy(&new_policy, policy, sizeof(*policy)); |
|---|
| 762 | 792 | |
|---|
| 763 | 793 | ret = sscanf(buf, "%15s", str_governor); |
|---|
| 764 | 794 | if (ret != 1) |
|---|
| 765 | 795 | return -EINVAL; |
|---|
| 766 | 796 | |
|---|
| 767 | | - if (cpufreq_parse_governor(str_governor, &new_policy)) |
|---|
| 768 | | - return -EINVAL; |
|---|
| 797 | + if (cpufreq_driver->setpolicy) { |
|---|
| 798 | + unsigned int new_pol; |
|---|
| 769 | 799 | |
|---|
| 770 | | - ret = cpufreq_set_policy(policy, &new_policy); |
|---|
| 800 | + new_pol = cpufreq_parse_policy(str_governor); |
|---|
| 801 | + if (!new_pol) |
|---|
| 802 | + return -EINVAL; |
|---|
| 771 | 803 | |
|---|
| 772 | | - if (new_policy.governor) |
|---|
| 773 | | - module_put(new_policy.governor->owner); |
|---|
| 804 | + ret = cpufreq_set_policy(policy, NULL, new_pol); |
|---|
| 805 | + } else { |
|---|
| 806 | + struct cpufreq_governor *new_gov; |
|---|
| 807 | + |
|---|
| 808 | + new_gov = cpufreq_parse_governor(str_governor); |
|---|
| 809 | + if (!new_gov) |
|---|
| 810 | + return -EINVAL; |
|---|
| 811 | + |
|---|
| 812 | + ret = cpufreq_set_policy(policy, new_gov, |
|---|
| 813 | + CPUFREQ_POLICY_UNKNOWN); |
|---|
| 814 | + |
|---|
| 815 | + module_put(new_gov->owner); |
|---|
| 816 | + } |
|---|
| 774 | 817 | |
|---|
| 775 | 818 | return ret ? ret : count; |
|---|
| 776 | 819 | } |
|---|
| 777 | 820 | |
|---|
| 778 | | -/** |
|---|
| 821 | +/* |
|---|
| 779 | 822 | * show_scaling_driver - show the cpufreq driver currently loaded |
|---|
| 780 | 823 | */ |
|---|
| 781 | 824 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
|---|
| .. | .. |
|---|
| 783 | 826 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
|---|
| 784 | 827 | } |
|---|
| 785 | 828 | |
|---|
| 786 | | -/** |
|---|
| 829 | +/* |
|---|
| 787 | 830 | * show_scaling_available_governors - show the available CPUfreq governors |
|---|
| 788 | 831 | */ |
|---|
| 789 | 832 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
|---|
| .. | .. |
|---|
| 797 | 840 | goto out; |
|---|
| 798 | 841 | } |
|---|
| 799 | 842 | |
|---|
| 843 | + mutex_lock(&cpufreq_governor_mutex); |
|---|
| 800 | 844 | for_each_governor(t) { |
|---|
| 801 | 845 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
|---|
| 802 | 846 | - (CPUFREQ_NAME_LEN + 2))) |
|---|
| 803 | | - goto out; |
|---|
| 847 | + break; |
|---|
| 804 | 848 | i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); |
|---|
| 805 | 849 | } |
|---|
| 850 | + mutex_unlock(&cpufreq_governor_mutex); |
|---|
| 806 | 851 | out: |
|---|
| 807 | 852 | i += sprintf(&buf[i], "\n"); |
|---|
| 808 | 853 | return i; |
|---|
| .. | .. |
|---|
| 825 | 870 | } |
|---|
| 826 | 871 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
|---|
| 827 | 872 | |
|---|
| 828 | | -/** |
|---|
| 873 | +/* |
|---|
| 829 | 874 | * show_related_cpus - show the CPUs affected by each transition even if |
|---|
| 830 | 875 | * hw coordination is in use |
|---|
| 831 | 876 | */ |
|---|
| .. | .. |
|---|
| 834 | 879 | return cpufreq_show_cpus(policy->related_cpus, buf); |
|---|
| 835 | 880 | } |
|---|
| 836 | 881 | |
|---|
| 837 | | -/** |
|---|
| 882 | +/* |
|---|
| 838 | 883 | * show_affected_cpus - show the CPUs affected by each transition |
|---|
| 839 | 884 | */ |
|---|
| 840 | 885 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) |
|---|
| .. | .. |
|---|
| 868 | 913 | return policy->governor->show_setspeed(policy, buf); |
|---|
| 869 | 914 | } |
|---|
| 870 | 915 | |
|---|
| 871 | | -/** |
|---|
| 916 | +/* |
|---|
| 872 | 917 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
|---|
| 873 | 918 | */ |
|---|
| 874 | 919 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) |
|---|
| 875 | 920 | { |
|---|
| 876 | 921 | unsigned int limit; |
|---|
| 877 | 922 | int ret; |
|---|
| 878 | | - if (cpufreq_driver->bios_limit) { |
|---|
| 879 | | - ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
|---|
| 880 | | - if (!ret) |
|---|
| 881 | | - return sprintf(buf, "%u\n", limit); |
|---|
| 882 | | - } |
|---|
| 923 | + ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
|---|
| 924 | + if (!ret) |
|---|
| 925 | + return sprintf(buf, "%u\n", limit); |
|---|
| 883 | 926 | return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); |
|---|
| 884 | 927 | } |
|---|
| 885 | 928 | |
|---|
| .. | .. |
|---|
| 978 | 1021 | .release = cpufreq_sysfs_release, |
|---|
| 979 | 1022 | }; |
|---|
| 980 | 1023 | |
|---|
| 981 | | -static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) |
|---|
| 1024 | +static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu, |
|---|
| 1025 | + struct device *dev) |
|---|
| 982 | 1026 | { |
|---|
| 983 | | - struct device *dev = get_cpu_device(cpu); |
|---|
| 984 | | - |
|---|
| 985 | | - if (!dev) |
|---|
| 1027 | + if (unlikely(!dev)) |
|---|
| 986 | 1028 | return; |
|---|
| 987 | 1029 | |
|---|
| 988 | 1030 | if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) |
|---|
| .. | .. |
|---|
| 1032 | 1074 | return 0; |
|---|
| 1033 | 1075 | } |
|---|
| 1034 | 1076 | |
|---|
| 1035 | | -__weak struct cpufreq_governor *cpufreq_default_governor(void) |
|---|
| 1036 | | -{ |
|---|
| 1037 | | - return NULL; |
|---|
| 1038 | | -} |
|---|
| 1039 | | - |
|---|
| 1040 | 1077 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
|---|
| 1041 | 1078 | { |
|---|
| 1042 | 1079 | struct cpufreq_governor *gov = NULL; |
|---|
| 1043 | | - struct cpufreq_policy new_policy; |
|---|
| 1080 | + unsigned int pol = CPUFREQ_POLICY_UNKNOWN; |
|---|
| 1081 | + int ret; |
|---|
| 1044 | 1082 | |
|---|
| 1045 | | - memcpy(&new_policy, policy, sizeof(*policy)); |
|---|
| 1083 | + if (has_target()) { |
|---|
| 1084 | + /* Update policy governor to the one used before hotplug. */ |
|---|
| 1085 | + gov = get_governor(policy->last_governor); |
|---|
| 1086 | + if (gov) { |
|---|
| 1087 | + pr_debug("Restoring governor %s for cpu %d\n", |
|---|
| 1088 | + gov->name, policy->cpu); |
|---|
| 1089 | + } else { |
|---|
| 1090 | + gov = get_governor(default_governor); |
|---|
| 1091 | + } |
|---|
| 1046 | 1092 | |
|---|
| 1047 | | - /* Update governor of new_policy to the governor used before hotplug */ |
|---|
| 1048 | | - gov = find_governor(policy->last_governor); |
|---|
| 1049 | | - if (gov) { |
|---|
| 1050 | | - pr_debug("Restoring governor %s for cpu %d\n", |
|---|
| 1051 | | - policy->governor->name, policy->cpu); |
|---|
| 1093 | + if (!gov) { |
|---|
| 1094 | + gov = cpufreq_default_governor(); |
|---|
| 1095 | + __module_get(gov->owner); |
|---|
| 1096 | + } |
|---|
| 1097 | + |
|---|
| 1052 | 1098 | } else { |
|---|
| 1053 | | - gov = cpufreq_default_governor(); |
|---|
| 1054 | | - if (!gov) |
|---|
| 1099 | + |
|---|
| 1100 | + /* Use the default policy if there is no last_policy. */ |
|---|
| 1101 | + if (policy->last_policy) { |
|---|
| 1102 | + pol = policy->last_policy; |
|---|
| 1103 | + } else { |
|---|
| 1104 | + pol = cpufreq_parse_policy(default_governor); |
|---|
| 1105 | + /* |
|---|
| 1106 | + * In case the default governor is neither "performance" |
|---|
| 1107 | + * nor "powersave", fall back to the initial policy |
|---|
| 1108 | + * value set by the driver. |
|---|
| 1109 | + */ |
|---|
| 1110 | + if (pol == CPUFREQ_POLICY_UNKNOWN) |
|---|
| 1111 | + pol = policy->policy; |
|---|
| 1112 | + } |
|---|
| 1113 | + if (pol != CPUFREQ_POLICY_PERFORMANCE && |
|---|
| 1114 | + pol != CPUFREQ_POLICY_POWERSAVE) |
|---|
| 1055 | 1115 | return -ENODATA; |
|---|
| 1056 | 1116 | } |
|---|
| 1057 | 1117 | |
|---|
| 1058 | | - new_policy.governor = gov; |
|---|
| 1118 | + ret = cpufreq_set_policy(policy, gov, pol); |
|---|
| 1119 | + if (gov) |
|---|
| 1120 | + module_put(gov->owner); |
|---|
| 1059 | 1121 | |
|---|
| 1060 | | - /* Use the default policy if there is no last_policy. */ |
|---|
| 1061 | | - if (cpufreq_driver->setpolicy) { |
|---|
| 1062 | | - if (policy->last_policy) |
|---|
| 1063 | | - new_policy.policy = policy->last_policy; |
|---|
| 1064 | | - else |
|---|
| 1065 | | - cpufreq_parse_governor(gov->name, &new_policy); |
|---|
| 1066 | | - } |
|---|
| 1067 | | - /* set default policy */ |
|---|
| 1068 | | - return cpufreq_set_policy(policy, &new_policy); |
|---|
| 1122 | + return ret; |
|---|
| 1069 | 1123 | } |
|---|
| 1070 | 1124 | |
|---|
| 1071 | 1125 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
|---|
| .. | .. |
|---|
| 1091 | 1145 | return ret; |
|---|
| 1092 | 1146 | } |
|---|
| 1093 | 1147 | |
|---|
| 1148 | +void refresh_frequency_limits(struct cpufreq_policy *policy) |
|---|
| 1149 | +{ |
|---|
| 1150 | + if (!policy_is_inactive(policy)) { |
|---|
| 1151 | + pr_debug("updating policy for CPU %u\n", policy->cpu); |
|---|
| 1152 | + |
|---|
| 1153 | + cpufreq_set_policy(policy, policy->governor, policy->policy); |
|---|
| 1154 | + } |
|---|
| 1155 | +} |
|---|
| 1156 | +EXPORT_SYMBOL(refresh_frequency_limits); |
|---|
| 1157 | + |
|---|
| 1094 | 1158 | static void handle_update(struct work_struct *work) |
|---|
| 1095 | 1159 | { |
|---|
| 1096 | 1160 | struct cpufreq_policy *policy = |
|---|
| 1097 | 1161 | container_of(work, struct cpufreq_policy, update); |
|---|
| 1098 | | - unsigned int cpu = policy->cpu; |
|---|
| 1099 | | - pr_debug("handle_update for cpu %u called\n", cpu); |
|---|
| 1100 | | - cpufreq_update_policy(cpu); |
|---|
| 1162 | + |
|---|
| 1163 | + pr_debug("handle_update for cpu %u called\n", policy->cpu); |
|---|
| 1164 | + down_write(&policy->rwsem); |
|---|
| 1165 | + refresh_frequency_limits(policy); |
|---|
| 1166 | + up_write(&policy->rwsem); |
|---|
| 1101 | 1167 | } |
|---|
| 1102 | 1168 | |
|---|
| 1103 | | -static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
|---|
| 1169 | +static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, |
|---|
| 1170 | + void *data) |
|---|
| 1104 | 1171 | { |
|---|
| 1105 | | - struct cpufreq_policy *policy; |
|---|
| 1106 | | - int ret; |
|---|
| 1172 | + struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); |
|---|
| 1107 | 1173 | |
|---|
| 1108 | | - policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
|---|
| 1109 | | - if (!policy) |
|---|
| 1110 | | - return NULL; |
|---|
| 1174 | + schedule_work(&policy->update); |
|---|
| 1175 | + return 0; |
|---|
| 1176 | +} |
|---|
| 1111 | 1177 | |
|---|
| 1112 | | - if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) |
|---|
| 1113 | | - goto err_free_policy; |
|---|
| 1178 | +static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, |
|---|
| 1179 | + void *data) |
|---|
| 1180 | +{ |
|---|
| 1181 | + struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); |
|---|
| 1114 | 1182 | |
|---|
| 1115 | | - if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) |
|---|
| 1116 | | - goto err_free_cpumask; |
|---|
| 1117 | | - |
|---|
| 1118 | | - if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
|---|
| 1119 | | - goto err_free_rcpumask; |
|---|
| 1120 | | - |
|---|
| 1121 | | - ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
|---|
| 1122 | | - cpufreq_global_kobject, "policy%u", cpu); |
|---|
| 1123 | | - if (ret) { |
|---|
| 1124 | | - pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); |
|---|
| 1125 | | - kobject_put(&policy->kobj); |
|---|
| 1126 | | - goto err_free_real_cpus; |
|---|
| 1127 | | - } |
|---|
| 1128 | | - |
|---|
| 1129 | | - INIT_LIST_HEAD(&policy->policy_list); |
|---|
| 1130 | | - init_rwsem(&policy->rwsem); |
|---|
| 1131 | | - spin_lock_init(&policy->transition_lock); |
|---|
| 1132 | | - init_waitqueue_head(&policy->transition_wait); |
|---|
| 1133 | | - init_completion(&policy->kobj_unregister); |
|---|
| 1134 | | - INIT_WORK(&policy->update, handle_update); |
|---|
| 1135 | | - |
|---|
| 1136 | | - policy->cpu = cpu; |
|---|
| 1137 | | - return policy; |
|---|
| 1138 | | - |
|---|
| 1139 | | -err_free_real_cpus: |
|---|
| 1140 | | - free_cpumask_var(policy->real_cpus); |
|---|
| 1141 | | -err_free_rcpumask: |
|---|
| 1142 | | - free_cpumask_var(policy->related_cpus); |
|---|
| 1143 | | -err_free_cpumask: |
|---|
| 1144 | | - free_cpumask_var(policy->cpus); |
|---|
| 1145 | | -err_free_policy: |
|---|
| 1146 | | - kfree(policy); |
|---|
| 1147 | | - |
|---|
| 1148 | | - return NULL; |
|---|
| 1183 | + schedule_work(&policy->update); |
|---|
| 1184 | + return 0; |
|---|
| 1149 | 1185 | } |
|---|
| 1150 | 1186 | |
|---|
| 1151 | 1187 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) |
|---|
| .. | .. |
|---|
| 1170 | 1206 | pr_debug("wait complete\n"); |
|---|
| 1171 | 1207 | } |
|---|
| 1172 | 1208 | |
|---|
| 1209 | +static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
|---|
| 1210 | +{ |
|---|
| 1211 | + struct cpufreq_policy *policy; |
|---|
| 1212 | + struct device *dev = get_cpu_device(cpu); |
|---|
| 1213 | + int ret; |
|---|
| 1214 | + |
|---|
| 1215 | + if (!dev) |
|---|
| 1216 | + return NULL; |
|---|
| 1217 | + |
|---|
| 1218 | + policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
|---|
| 1219 | + if (!policy) |
|---|
| 1220 | + return NULL; |
|---|
| 1221 | + |
|---|
| 1222 | + if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) |
|---|
| 1223 | + goto err_free_policy; |
|---|
| 1224 | + |
|---|
| 1225 | + if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) |
|---|
| 1226 | + goto err_free_cpumask; |
|---|
| 1227 | + |
|---|
| 1228 | + if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
|---|
| 1229 | + goto err_free_rcpumask; |
|---|
| 1230 | + |
|---|
| 1231 | + init_completion(&policy->kobj_unregister); |
|---|
| 1232 | + ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
|---|
| 1233 | + cpufreq_global_kobject, "policy%u", cpu); |
|---|
| 1234 | + if (ret) { |
|---|
| 1235 | + dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); |
|---|
| 1236 | + /* |
|---|
| 1237 | + * The entire policy object will be freed below, but the extra |
|---|
| 1238 | + * memory allocated for the kobject name needs to be freed by |
|---|
| 1239 | + * releasing the kobject. |
|---|
| 1240 | + */ |
|---|
| 1241 | + kobject_put(&policy->kobj); |
|---|
| 1242 | + goto err_free_real_cpus; |
|---|
| 1243 | + } |
|---|
| 1244 | + |
|---|
| 1245 | + freq_constraints_init(&policy->constraints); |
|---|
| 1246 | + |
|---|
| 1247 | + policy->nb_min.notifier_call = cpufreq_notifier_min; |
|---|
| 1248 | + policy->nb_max.notifier_call = cpufreq_notifier_max; |
|---|
| 1249 | + |
|---|
| 1250 | + ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, |
|---|
| 1251 | + &policy->nb_min); |
|---|
| 1252 | + if (ret) { |
|---|
| 1253 | + dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n", |
|---|
| 1254 | + ret, cpumask_pr_args(policy->cpus)); |
|---|
| 1255 | + goto err_kobj_remove; |
|---|
| 1256 | + } |
|---|
| 1257 | + |
|---|
| 1258 | + ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, |
|---|
| 1259 | + &policy->nb_max); |
|---|
| 1260 | + if (ret) { |
|---|
| 1261 | + dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n", |
|---|
| 1262 | + ret, cpumask_pr_args(policy->cpus)); |
|---|
| 1263 | + goto err_min_qos_notifier; |
|---|
| 1264 | + } |
|---|
| 1265 | + |
|---|
| 1266 | + INIT_LIST_HEAD(&policy->policy_list); |
|---|
| 1267 | + init_rwsem(&policy->rwsem); |
|---|
| 1268 | + spin_lock_init(&policy->transition_lock); |
|---|
| 1269 | + init_waitqueue_head(&policy->transition_wait); |
|---|
| 1270 | + INIT_WORK(&policy->update, handle_update); |
|---|
| 1271 | + |
|---|
| 1272 | + policy->cpu = cpu; |
|---|
| 1273 | + return policy; |
|---|
| 1274 | + |
|---|
| 1275 | +err_min_qos_notifier: |
|---|
| 1276 | + freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, |
|---|
| 1277 | + &policy->nb_min); |
|---|
| 1278 | +err_kobj_remove: |
|---|
| 1279 | + cpufreq_policy_put_kobj(policy); |
|---|
| 1280 | +err_free_real_cpus: |
|---|
| 1281 | + free_cpumask_var(policy->real_cpus); |
|---|
| 1282 | +err_free_rcpumask: |
|---|
| 1283 | + free_cpumask_var(policy->related_cpus); |
|---|
| 1284 | +err_free_cpumask: |
|---|
| 1285 | + free_cpumask_var(policy->cpus); |
|---|
| 1286 | +err_free_policy: |
|---|
| 1287 | + kfree(policy); |
|---|
| 1288 | + |
|---|
| 1289 | + return NULL; |
|---|
| 1290 | +} |
|---|
| 1291 | + |
|---|
| 1173 | 1292 | static void cpufreq_policy_free(struct cpufreq_policy *policy) |
|---|
| 1174 | 1293 | { |
|---|
| 1175 | 1294 | unsigned long flags; |
|---|
| .. | .. |
|---|
| 1182 | 1301 | for_each_cpu(cpu, policy->related_cpus) |
|---|
| 1183 | 1302 | per_cpu(cpufreq_cpu_data, cpu) = NULL; |
|---|
| 1184 | 1303 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
|---|
| 1304 | + |
|---|
| 1305 | + freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, |
|---|
| 1306 | + &policy->nb_max); |
|---|
| 1307 | + freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, |
|---|
| 1308 | + &policy->nb_min); |
|---|
| 1309 | + |
|---|
| 1310 | + /* Cancel any pending policy->update work before freeing the policy. */ |
|---|
| 1311 | + cancel_work_sync(&policy->update); |
|---|
| 1312 | + |
|---|
| 1313 | + if (policy->max_freq_req) { |
|---|
| 1314 | + /* |
|---|
| 1315 | + * CPUFREQ_CREATE_POLICY notification is sent only after |
|---|
| 1316 | + * successfully adding max_freq_req request. |
|---|
| 1317 | + */ |
|---|
| 1318 | + blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
|---|
| 1319 | + CPUFREQ_REMOVE_POLICY, policy); |
|---|
| 1320 | + freq_qos_remove_request(policy->max_freq_req); |
|---|
| 1321 | + } |
|---|
| 1322 | + |
|---|
| 1323 | + freq_qos_remove_request(policy->min_freq_req); |
|---|
| 1324 | + kfree(policy->min_freq_req); |
|---|
| 1185 | 1325 | |
|---|
| 1186 | 1326 | cpufreq_policy_put_kobj(policy); |
|---|
| 1187 | 1327 | free_cpumask_var(policy->real_cpus); |
|---|
| .. | .. |
|---|
| 1220 | 1360 | return -ENOMEM; |
|---|
| 1221 | 1361 | } |
|---|
| 1222 | 1362 | |
|---|
| 1223 | | - cpumask_copy(policy->cpus, cpumask_of(cpu)); |
|---|
| 1363 | + if (!new_policy && cpufreq_driver->online) { |
|---|
| 1364 | + ret = cpufreq_driver->online(policy); |
|---|
| 1365 | + if (ret) { |
|---|
| 1366 | + pr_debug("%s: %d: initialization failed\n", __func__, |
|---|
| 1367 | + __LINE__); |
|---|
| 1368 | + goto out_exit_policy; |
|---|
| 1369 | + } |
|---|
| 1224 | 1370 | |
|---|
| 1225 | | - /* call driver. From then on the cpufreq must be able |
|---|
| 1226 | | - * to accept all calls to ->verify and ->setpolicy for this CPU |
|---|
| 1227 | | - */ |
|---|
| 1228 | | - ret = cpufreq_driver->init(policy); |
|---|
| 1229 | | - if (ret) { |
|---|
| 1230 | | - pr_debug("initialization failed\n"); |
|---|
| 1231 | | - goto out_free_policy; |
|---|
| 1232 | | - } |
|---|
| 1371 | + /* Recover policy->cpus using related_cpus */ |
|---|
| 1372 | + cpumask_copy(policy->cpus, policy->related_cpus); |
|---|
| 1373 | + } else { |
|---|
| 1374 | + cpumask_copy(policy->cpus, cpumask_of(cpu)); |
|---|
| 1233 | 1375 | |
|---|
| 1234 | | - ret = cpufreq_table_validate_and_sort(policy); |
|---|
| 1235 | | - if (ret) |
|---|
| 1236 | | - goto out_exit_policy; |
|---|
| 1376 | + /* |
|---|
| 1377 | + * Call driver. From then on the cpufreq must be able |
|---|
| 1378 | + * to accept all calls to ->verify and ->setpolicy for this CPU. |
|---|
| 1379 | + */ |
|---|
| 1380 | + ret = cpufreq_driver->init(policy); |
|---|
| 1381 | + if (ret) { |
|---|
| 1382 | + pr_debug("%s: %d: initialization failed\n", __func__, |
|---|
| 1383 | + __LINE__); |
|---|
| 1384 | + goto out_free_policy; |
|---|
| 1385 | + } |
|---|
| 1237 | 1386 | |
|---|
| 1238 | | - down_write(&policy->rwsem); |
|---|
| 1387 | + /* |
|---|
| 1388 | + * The initialization has succeeded and the policy is online. |
|---|
| 1389 | + * If there is a problem with its frequency table, take it |
|---|
| 1390 | + * offline and drop it. |
|---|
| 1391 | + */ |
|---|
| 1392 | + ret = cpufreq_table_validate_and_sort(policy); |
|---|
| 1393 | + if (ret) |
|---|
| 1394 | + goto out_offline_policy; |
|---|
| 1239 | 1395 | |
|---|
| 1240 | | - if (new_policy) { |
|---|
| 1241 | 1396 | /* related_cpus should at least include policy->cpus. */ |
|---|
| 1242 | 1397 | cpumask_copy(policy->related_cpus, policy->cpus); |
|---|
| 1243 | 1398 | } |
|---|
| 1244 | 1399 | |
|---|
| 1400 | + down_write(&policy->rwsem); |
|---|
| 1245 | 1401 | /* |
|---|
| 1246 | 1402 | * affected cpus must always be the one, which are online. We aren't |
|---|
| 1247 | 1403 | * managing offline cpus here. |
|---|
| .. | .. |
|---|
| 1249 | 1405 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); |
|---|
| 1250 | 1406 | |
|---|
| 1251 | 1407 | if (new_policy) { |
|---|
| 1252 | | - policy->user_policy.min = policy->min; |
|---|
| 1253 | | - policy->user_policy.max = policy->max; |
|---|
| 1254 | | - |
|---|
| 1255 | 1408 | for_each_cpu(j, policy->related_cpus) { |
|---|
| 1256 | 1409 | per_cpu(cpufreq_cpu_data, j) = policy; |
|---|
| 1257 | | - add_cpu_dev_symlink(policy, j); |
|---|
| 1410 | + add_cpu_dev_symlink(policy, j, get_cpu_device(j)); |
|---|
| 1258 | 1411 | } |
|---|
| 1259 | | - } else { |
|---|
| 1260 | | - policy->min = policy->user_policy.min; |
|---|
| 1261 | | - policy->max = policy->user_policy.max; |
|---|
| 1412 | + |
|---|
| 1413 | + policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), |
|---|
| 1414 | + GFP_KERNEL); |
|---|
| 1415 | + if (!policy->min_freq_req) |
|---|
| 1416 | + goto out_destroy_policy; |
|---|
| 1417 | + |
|---|
| 1418 | + ret = freq_qos_add_request(&policy->constraints, |
|---|
| 1419 | + policy->min_freq_req, FREQ_QOS_MIN, |
|---|
| 1420 | + FREQ_QOS_MIN_DEFAULT_VALUE); |
|---|
| 1421 | + if (ret < 0) { |
|---|
| 1422 | + /* |
|---|
| 1423 | + * So we don't call freq_qos_remove_request() for an |
|---|
| 1424 | + * uninitialized request. |
|---|
| 1425 | + */ |
|---|
| 1426 | + kfree(policy->min_freq_req); |
|---|
| 1427 | + policy->min_freq_req = NULL; |
|---|
| 1428 | + goto out_destroy_policy; |
|---|
| 1429 | + } |
|---|
| 1430 | + |
|---|
| 1431 | + /* |
|---|
| 1432 | + * This must be initialized right here to avoid calling |
|---|
| 1433 | + * freq_qos_remove_request() on uninitialized request in case |
|---|
| 1434 | + * of errors. |
|---|
| 1435 | + */ |
|---|
| 1436 | + policy->max_freq_req = policy->min_freq_req + 1; |
|---|
| 1437 | + |
|---|
| 1438 | + ret = freq_qos_add_request(&policy->constraints, |
|---|
| 1439 | + policy->max_freq_req, FREQ_QOS_MAX, |
|---|
| 1440 | + FREQ_QOS_MAX_DEFAULT_VALUE); |
|---|
| 1441 | + if (ret < 0) { |
|---|
| 1442 | + policy->max_freq_req = NULL; |
|---|
| 1443 | + goto out_destroy_policy; |
|---|
| 1444 | + } |
|---|
| 1445 | + |
|---|
| 1446 | + blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
|---|
| 1447 | + CPUFREQ_CREATE_POLICY, policy); |
|---|
| 1262 | 1448 | } |
|---|
| 1263 | 1449 | |
|---|
| 1264 | | - if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
|---|
| 1450 | + if (cpufreq_driver->get && has_target()) { |
|---|
| 1265 | 1451 | policy->cur = cpufreq_driver->get(policy->cpu); |
|---|
| 1266 | 1452 | if (!policy->cur) { |
|---|
| 1267 | 1453 | pr_err("%s: ->get() failed\n", __func__); |
|---|
| .. | .. |
|---|
| 1289 | 1475 | */ |
|---|
| 1290 | 1476 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) |
|---|
| 1291 | 1477 | && has_target()) { |
|---|
| 1478 | + unsigned int old_freq = policy->cur; |
|---|
| 1479 | + |
|---|
| 1292 | 1480 | /* Are we running at unknown frequency ? */ |
|---|
| 1293 | | - ret = cpufreq_frequency_table_get_index(policy, policy->cur); |
|---|
| 1481 | + ret = cpufreq_frequency_table_get_index(policy, old_freq); |
|---|
| 1294 | 1482 | if (ret == -EINVAL) { |
|---|
| 1295 | | - /* Warn user and fix it */ |
|---|
| 1296 | | - pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", |
|---|
| 1297 | | - __func__, policy->cpu, policy->cur); |
|---|
| 1298 | | - ret = __cpufreq_driver_target(policy, policy->cur - 1, |
|---|
| 1299 | | - CPUFREQ_RELATION_L); |
|---|
| 1483 | + ret = __cpufreq_driver_target(policy, old_freq - 1, |
|---|
| 1484 | + CPUFREQ_RELATION_L); |
|---|
| 1300 | 1485 | |
|---|
| 1301 | 1486 | /* |
|---|
| 1302 | 1487 | * Reaching here after boot in a few seconds may not |
|---|
| .. | .. |
|---|
| 1304 | 1489 | * frequency for longer duration. Hence, a BUG_ON(). |
|---|
| 1305 | 1490 | */ |
|---|
| 1306 | 1491 | BUG_ON(ret); |
|---|
| 1307 | | - pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", |
|---|
| 1308 | | - __func__, policy->cpu, policy->cur); |
|---|
| 1492 | + pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n", |
|---|
| 1493 | + __func__, policy->cpu, old_freq, policy->cur); |
|---|
| 1309 | 1494 | } |
|---|
| 1310 | 1495 | } |
|---|
| 1311 | 1496 | |
|---|
| .. | .. |
|---|
| 1326 | 1511 | if (ret) { |
|---|
| 1327 | 1512 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", |
|---|
| 1328 | 1513 | __func__, cpu, ret); |
|---|
| 1329 | | - /* cpufreq_policy_free() will notify based on this */ |
|---|
| 1330 | | - new_policy = false; |
|---|
| 1331 | 1514 | goto out_destroy_policy; |
|---|
| 1332 | 1515 | } |
|---|
| 1333 | 1516 | |
|---|
| .. | .. |
|---|
| 1339 | 1522 | if (cpufreq_driver->ready) |
|---|
| 1340 | 1523 | cpufreq_driver->ready(policy); |
|---|
| 1341 | 1524 | |
|---|
| 1342 | | - if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
|---|
| 1343 | | - cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) |
|---|
| 1525 | + if (cpufreq_thermal_control_enabled(cpufreq_driver)) |
|---|
| 1344 | 1526 | policy->cdev = of_cpufreq_cooling_register(policy); |
|---|
| 1345 | 1527 | |
|---|
| 1346 | 1528 | pr_debug("initialization complete\n"); |
|---|
| .. | .. |
|---|
| 1352 | 1534 | remove_cpu_dev_symlink(policy, get_cpu_device(j)); |
|---|
| 1353 | 1535 | |
|---|
| 1354 | 1536 | up_write(&policy->rwsem); |
|---|
| 1537 | + |
|---|
| 1538 | +out_offline_policy: |
|---|
| 1539 | + if (cpufreq_driver->offline) |
|---|
| 1540 | + cpufreq_driver->offline(policy); |
|---|
| 1355 | 1541 | |
|---|
| 1356 | 1542 | out_exit_policy: |
|---|
| 1357 | 1543 | if (cpufreq_driver->exit) |
|---|
| .. | .. |
|---|
| 1384 | 1570 | /* Create sysfs link on CPU registration */ |
|---|
| 1385 | 1571 | policy = per_cpu(cpufreq_cpu_data, cpu); |
|---|
| 1386 | 1572 | if (policy) |
|---|
| 1387 | | - add_cpu_dev_symlink(policy, cpu); |
|---|
| 1573 | + add_cpu_dev_symlink(policy, cpu, dev); |
|---|
| 1388 | 1574 | |
|---|
| 1389 | 1575 | return 0; |
|---|
| 1390 | 1576 | } |
|---|
| .. | .. |
|---|
| 1430 | 1616 | goto unlock; |
|---|
| 1431 | 1617 | } |
|---|
| 1432 | 1618 | |
|---|
| 1433 | | - if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
|---|
| 1434 | | - cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) { |
|---|
| 1619 | + if (cpufreq_thermal_control_enabled(cpufreq_driver)) { |
|---|
| 1435 | 1620 | cpufreq_cooling_unregister(policy->cdev); |
|---|
| 1436 | 1621 | policy->cdev = NULL; |
|---|
| 1437 | 1622 | } |
|---|
| .. | .. |
|---|
| 1443 | 1628 | cpufreq_exit_governor(policy); |
|---|
| 1444 | 1629 | |
|---|
| 1445 | 1630 | /* |
|---|
| 1446 | | - * Perform the ->exit() even during light-weight tear-down, |
|---|
| 1447 | | - * since this is a core component, and is essential for the |
|---|
| 1448 | | - * subsequent light-weight ->init() to succeed. |
|---|
| 1631 | + * Perform the ->offline() during light-weight tear-down, as |
|---|
| 1632 | + * that allows fast recovery when the CPU comes back. |
|---|
| 1449 | 1633 | */ |
|---|
| 1450 | | - if (cpufreq_driver->exit) { |
|---|
| 1634 | + if (cpufreq_driver->offline) { |
|---|
| 1635 | + cpufreq_driver->offline(policy); |
|---|
| 1636 | + } else if (cpufreq_driver->exit) { |
|---|
| 1451 | 1637 | cpufreq_driver->exit(policy); |
|---|
| 1452 | 1638 | policy->freq_table = NULL; |
|---|
| 1453 | 1639 | } |
|---|
| .. | .. |
|---|
| 1457 | 1643 | return 0; |
|---|
| 1458 | 1644 | } |
|---|
| 1459 | 1645 | |
|---|
| 1460 | | -/** |
|---|
| 1646 | +/* |
|---|
| 1461 | 1647 | * cpufreq_remove_dev - remove a CPU device |
|---|
| 1462 | 1648 | * |
|---|
| 1463 | 1649 | * Removes the cpufreq interface for a CPU device. |
|---|
| .. | .. |
|---|
| 1476 | 1662 | cpumask_clear_cpu(cpu, policy->real_cpus); |
|---|
| 1477 | 1663 | remove_cpu_dev_symlink(policy, dev); |
|---|
| 1478 | 1664 | |
|---|
| 1479 | | - if (cpumask_empty(policy->real_cpus)) |
|---|
| 1665 | + if (cpumask_empty(policy->real_cpus)) { |
|---|
| 1666 | + /* We did light-weight exit earlier, do full tear down now */ |
|---|
| 1667 | + if (cpufreq_driver->offline) |
|---|
| 1668 | + cpufreq_driver->exit(policy); |
|---|
| 1669 | + |
|---|
| 1480 | 1670 | cpufreq_policy_free(policy); |
|---|
| 1671 | + } |
|---|
| 1481 | 1672 | } |
|---|
| 1482 | 1673 | |
|---|
| 1483 | 1674 | /** |
|---|
| .. | .. |
|---|
| 1502 | 1693 | |
|---|
| 1503 | 1694 | cpufreq_freq_transition_begin(policy, &freqs); |
|---|
| 1504 | 1695 | cpufreq_freq_transition_end(policy, &freqs, 0); |
|---|
| 1696 | +} |
|---|
| 1697 | + |
|---|
| 1698 | +static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) |
|---|
| 1699 | +{ |
|---|
| 1700 | + unsigned int new_freq; |
|---|
| 1701 | + |
|---|
| 1702 | + new_freq = cpufreq_driver->get(policy->cpu); |
|---|
| 1703 | + if (!new_freq) |
|---|
| 1704 | + return 0; |
|---|
| 1705 | + |
|---|
| 1706 | + /* |
|---|
| 1707 | + * If fast frequency switching is used with the given policy, the check |
|---|
| 1708 | + * against policy->cur is pointless, so skip it in that case. |
|---|
| 1709 | + */ |
|---|
| 1710 | + if (policy->fast_switch_enabled || !has_target()) |
|---|
| 1711 | + return new_freq; |
|---|
| 1712 | + |
|---|
| 1713 | + if (policy->cur != new_freq) { |
|---|
| 1714 | + cpufreq_out_of_sync(policy, new_freq); |
|---|
| 1715 | + if (update) |
|---|
| 1716 | + schedule_work(&policy->update); |
|---|
| 1717 | + } |
|---|
| 1718 | + |
|---|
| 1719 | + return new_freq; |
|---|
| 1505 | 1720 | } |
|---|
| 1506 | 1721 | |
|---|
| 1507 | 1722 | /** |
|---|
| .. | .. |
|---|
| 1557 | 1772 | } |
|---|
| 1558 | 1773 | EXPORT_SYMBOL(cpufreq_quick_get_max); |
|---|
| 1559 | 1774 | |
|---|
| 1560 | | -static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
|---|
| 1775 | +/** |
|---|
| 1776 | + * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU |
|---|
| 1777 | + * @cpu: CPU number |
|---|
| 1778 | + * |
|---|
| 1779 | + * The default return value is the max_freq field of cpuinfo. |
|---|
| 1780 | + */ |
|---|
| 1781 | +__weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) |
|---|
| 1561 | 1782 | { |
|---|
| 1783 | + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
|---|
| 1562 | 1784 | unsigned int ret_freq = 0; |
|---|
| 1563 | 1785 | |
|---|
| 1564 | | - if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get) |
|---|
| 1565 | | - return ret_freq; |
|---|
| 1566 | | - |
|---|
| 1567 | | - ret_freq = cpufreq_driver->get(policy->cpu); |
|---|
| 1568 | | - |
|---|
| 1569 | | - /* |
|---|
| 1570 | | - * If fast frequency switching is used with the given policy, the check |
|---|
| 1571 | | - * against policy->cur is pointless, so skip it in that case too. |
|---|
| 1572 | | - */ |
|---|
| 1573 | | - if (policy->fast_switch_enabled) |
|---|
| 1574 | | - return ret_freq; |
|---|
| 1575 | | - |
|---|
| 1576 | | - if (ret_freq && policy->cur && |
|---|
| 1577 | | - !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
|---|
| 1578 | | - /* verify no discrepancy between actual and |
|---|
| 1579 | | - saved value exists */ |
|---|
| 1580 | | - if (unlikely(ret_freq != policy->cur)) { |
|---|
| 1581 | | - cpufreq_out_of_sync(policy, ret_freq); |
|---|
| 1582 | | - schedule_work(&policy->update); |
|---|
| 1583 | | - } |
|---|
| 1786 | + if (policy) { |
|---|
| 1787 | + ret_freq = policy->cpuinfo.max_freq; |
|---|
| 1788 | + cpufreq_cpu_put(policy); |
|---|
| 1584 | 1789 | } |
|---|
| 1585 | 1790 | |
|---|
| 1586 | 1791 | return ret_freq; |
|---|
| 1792 | +} |
|---|
| 1793 | +EXPORT_SYMBOL(cpufreq_get_hw_max_freq); |
|---|
| 1794 | + |
|---|
| 1795 | +static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
|---|
| 1796 | +{ |
|---|
| 1797 | + if (unlikely(policy_is_inactive(policy))) |
|---|
| 1798 | + return 0; |
|---|
| 1799 | + |
|---|
| 1800 | + return cpufreq_verify_current_freq(policy, true); |
|---|
| 1587 | 1801 | } |
|---|
| 1588 | 1802 | |
|---|
| 1589 | 1803 | /** |
|---|
| .. | .. |
|---|
| 1599 | 1813 | |
|---|
| 1600 | 1814 | if (policy) { |
|---|
| 1601 | 1815 | down_read(&policy->rwsem); |
|---|
| 1602 | | - ret_freq = __cpufreq_get(policy); |
|---|
| 1816 | + if (cpufreq_driver->get) |
|---|
| 1817 | + ret_freq = __cpufreq_get(policy); |
|---|
| 1603 | 1818 | up_read(&policy->rwsem); |
|---|
| 1604 | 1819 | |
|---|
| 1605 | 1820 | cpufreq_cpu_put(policy); |
|---|
| .. | .. |
|---|
| 1608 | 1823 | return ret_freq; |
|---|
| 1609 | 1824 | } |
|---|
| 1610 | 1825 | EXPORT_SYMBOL(cpufreq_get); |
|---|
| 1611 | | - |
|---|
| 1612 | | -static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy) |
|---|
| 1613 | | -{ |
|---|
| 1614 | | - unsigned int new_freq; |
|---|
| 1615 | | - |
|---|
| 1616 | | - new_freq = cpufreq_driver->get(policy->cpu); |
|---|
| 1617 | | - if (!new_freq) |
|---|
| 1618 | | - return 0; |
|---|
| 1619 | | - |
|---|
| 1620 | | - if (!policy->cur) { |
|---|
| 1621 | | - pr_debug("cpufreq: Driver did not initialize current freq\n"); |
|---|
| 1622 | | - policy->cur = new_freq; |
|---|
| 1623 | | - } else if (policy->cur != new_freq && has_target()) { |
|---|
| 1624 | | - cpufreq_out_of_sync(policy, new_freq); |
|---|
| 1625 | | - } |
|---|
| 1626 | | - |
|---|
| 1627 | | - return new_freq; |
|---|
| 1628 | | -} |
|---|
| 1629 | 1826 | |
|---|
| 1630 | 1827 | static struct subsys_interface cpufreq_interface = { |
|---|
| 1631 | 1828 | .name = "cpufreq", |
|---|
| .. | .. |
|---|
| 1688 | 1885 | } |
|---|
| 1689 | 1886 | |
|---|
| 1690 | 1887 | if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) |
|---|
| 1691 | | - pr_err("%s: Failed to suspend driver: %p\n", __func__, |
|---|
| 1692 | | - policy); |
|---|
| 1888 | + pr_err("%s: Failed to suspend driver: %s\n", __func__, |
|---|
| 1889 | + cpufreq_driver->name); |
|---|
| 1693 | 1890 | } |
|---|
| 1694 | 1891 | |
|---|
| 1695 | 1892 | suspend: |
|---|
| .. | .. |
|---|
| 1734 | 1931 | __func__, policy); |
|---|
| 1735 | 1932 | } |
|---|
| 1736 | 1933 | } |
|---|
| 1934 | +} |
|---|
| 1935 | + |
|---|
| 1936 | +/** |
|---|
| 1937 | + * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones. |
|---|
| 1938 | + * @flags: Flags to test against the current cpufreq driver's flags. |
|---|
| 1939 | + * |
|---|
| 1940 | + * Assumes that the driver is there, so callers must ensure that this is the |
|---|
| 1941 | + * case. |
|---|
| 1942 | + */ |
|---|
| 1943 | +bool cpufreq_driver_test_flags(u16 flags) |
|---|
| 1944 | +{ |
|---|
| 1945 | + return !!(cpufreq_driver->flags & flags); |
|---|
| 1737 | 1946 | } |
|---|
| 1738 | 1947 | |
|---|
| 1739 | 1948 | /** |
|---|
| .. | .. |
|---|
| 1888 | 2097 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, |
|---|
| 1889 | 2098 | unsigned int target_freq) |
|---|
| 1890 | 2099 | { |
|---|
| 1891 | | - int ret; |
|---|
| 2100 | + unsigned int freq; |
|---|
| 2101 | + unsigned int old_target_freq = target_freq; |
|---|
| 2102 | + int cpu; |
|---|
| 1892 | 2103 | |
|---|
| 1893 | 2104 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
|---|
| 2105 | + trace_android_vh_cpufreq_fast_switch(policy, target_freq, old_target_freq); |
|---|
| 2106 | + freq = cpufreq_driver->fast_switch(policy, target_freq); |
|---|
| 1894 | 2107 | |
|---|
| 1895 | | - ret = cpufreq_driver->fast_switch(policy, target_freq); |
|---|
| 1896 | | - if (ret) |
|---|
| 1897 | | - cpufreq_times_record_transition(policy, ret); |
|---|
| 2108 | + if (!freq) |
|---|
| 2109 | + return 0; |
|---|
| 1898 | 2110 | |
|---|
| 1899 | | - return ret; |
|---|
| 2111 | + policy->cur = freq; |
|---|
| 2112 | + arch_set_freq_scale(policy->related_cpus, freq, |
|---|
| 2113 | + policy->cpuinfo.max_freq); |
|---|
| 2114 | + cpufreq_stats_record_transition(policy, freq); |
|---|
| 2115 | + cpufreq_times_record_transition(policy, freq); |
|---|
| 2116 | + trace_android_rvh_cpufreq_transition(policy); |
|---|
| 2117 | + |
|---|
| 2118 | + if (trace_cpu_frequency_enabled()) { |
|---|
| 2119 | + for_each_cpu(cpu, policy->cpus) |
|---|
| 2120 | + trace_cpu_frequency(freq, cpu); |
|---|
| 2121 | + } |
|---|
| 2122 | + |
|---|
| 2123 | + return freq; |
|---|
| 1900 | 2124 | } |
|---|
| 1901 | 2125 | EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); |
|---|
| 1902 | 2126 | |
|---|
| .. | .. |
|---|
| 1993 | 2217 | if (cpufreq_disabled()) |
|---|
| 1994 | 2218 | return -ENODEV; |
|---|
| 1995 | 2219 | |
|---|
| 1996 | | -#ifdef CONFIG_ARM_ROCKCHIP_CPUFREQ |
|---|
| 1997 | | - target_freq = rockchip_cpufreq_adjust_target(policy->cpu, target_freq); |
|---|
| 1998 | | -#endif |
|---|
| 1999 | 2220 | /* Make sure that target_freq is within supported range */ |
|---|
| 2000 | 2221 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
|---|
| 2222 | + trace_android_vh_cpufreq_target(policy, target_freq, old_target_freq); |
|---|
| 2001 | 2223 | |
|---|
| 2002 | 2224 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", |
|---|
| 2003 | 2225 | policy->cpu, target_freq, relation, old_target_freq); |
|---|
| .. | .. |
|---|
| 2008 | 2230 | * exactly same freq is called again and so we can save on few function |
|---|
| 2009 | 2231 | * calls. |
|---|
| 2010 | 2232 | */ |
|---|
| 2011 | | - if (target_freq == policy->cur) |
|---|
| 2233 | + if (target_freq == policy->cur && |
|---|
| 2234 | + !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) |
|---|
| 2012 | 2235 | return 0; |
|---|
| 2013 | 2236 | |
|---|
| 2014 | 2237 | /* Save last value to restore later on errors */ |
|---|
| .. | .. |
|---|
| 2030 | 2253 | unsigned int target_freq, |
|---|
| 2031 | 2254 | unsigned int relation) |
|---|
| 2032 | 2255 | { |
|---|
| 2033 | | - int ret = -EINVAL; |
|---|
| 2256 | + int ret; |
|---|
| 2034 | 2257 | |
|---|
| 2035 | 2258 | down_write(&policy->rwsem); |
|---|
| 2036 | 2259 | |
|---|
| .. | .. |
|---|
| 2062 | 2285 | return -EINVAL; |
|---|
| 2063 | 2286 | |
|---|
| 2064 | 2287 | /* Platform doesn't want dynamic frequency switching ? */ |
|---|
| 2065 | | - if (policy->governor->dynamic_switching && |
|---|
| 2288 | + if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && |
|---|
| 2066 | 2289 | cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { |
|---|
| 2067 | 2290 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
|---|
| 2068 | 2291 | |
|---|
| .. | .. |
|---|
| 2088 | 2311 | } |
|---|
| 2089 | 2312 | } |
|---|
| 2090 | 2313 | |
|---|
| 2314 | + policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET); |
|---|
| 2315 | + |
|---|
| 2091 | 2316 | return 0; |
|---|
| 2092 | 2317 | } |
|---|
| 2093 | 2318 | |
|---|
| .. | .. |
|---|
| 2104 | 2329 | module_put(policy->governor->owner); |
|---|
| 2105 | 2330 | } |
|---|
| 2106 | 2331 | |
|---|
| 2107 | | -static int cpufreq_start_governor(struct cpufreq_policy *policy) |
|---|
| 2332 | +int cpufreq_start_governor(struct cpufreq_policy *policy) |
|---|
| 2108 | 2333 | { |
|---|
| 2109 | 2334 | int ret; |
|---|
| 2110 | 2335 | |
|---|
| .. | .. |
|---|
| 2116 | 2341 | |
|---|
| 2117 | 2342 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
|---|
| 2118 | 2343 | |
|---|
| 2119 | | - if (cpufreq_driver->get && !cpufreq_driver->setpolicy) |
|---|
| 2120 | | - cpufreq_update_current_freq(policy); |
|---|
| 2344 | + if (cpufreq_driver->get) |
|---|
| 2345 | + cpufreq_verify_current_freq(policy, false); |
|---|
| 2121 | 2346 | |
|---|
| 2122 | 2347 | if (policy->governor->start) { |
|---|
| 2123 | 2348 | ret = policy->governor->start(policy); |
|---|
| .. | .. |
|---|
| 2131 | 2356 | return 0; |
|---|
| 2132 | 2357 | } |
|---|
| 2133 | 2358 | |
|---|
| 2134 | | -static void cpufreq_stop_governor(struct cpufreq_policy *policy) |
|---|
| 2359 | +void cpufreq_stop_governor(struct cpufreq_policy *policy) |
|---|
| 2135 | 2360 | { |
|---|
| 2136 | 2361 | if (cpufreq_suspended || !policy->governor) |
|---|
| 2137 | 2362 | return; |
|---|
| .. | .. |
|---|
| 2212 | 2437 | * cpufreq_get_policy - get the current cpufreq_policy |
|---|
| 2213 | 2438 | * @policy: struct cpufreq_policy into which the current cpufreq_policy |
|---|
| 2214 | 2439 | * is written |
|---|
| 2440 | + * @cpu: CPU to find the policy for |
|---|
| 2215 | 2441 | * |
|---|
| 2216 | 2442 | * Reads the current cpufreq policy. |
|---|
| 2217 | 2443 | */ |
|---|
| .. | .. |
|---|
| 2232 | 2458 | } |
|---|
| 2233 | 2459 | EXPORT_SYMBOL(cpufreq_get_policy); |
|---|
| 2234 | 2460 | |
|---|
| 2235 | | -/* |
|---|
| 2236 | | - * policy : current policy. |
|---|
| 2237 | | - * new_policy: policy to be set. |
|---|
| 2461 | +/** |
|---|
| 2462 | + * cpufreq_set_policy - Modify cpufreq policy parameters. |
|---|
| 2463 | + * @policy: Policy object to modify. |
|---|
| 2464 | + * @new_gov: Policy governor pointer. |
|---|
| 2465 | + * @new_pol: Policy value (for drivers with built-in governors). |
|---|
| 2466 | + * |
|---|
| 2467 | + * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency |
|---|
| 2468 | + * limits to be set for the policy, update @policy with the verified limits |
|---|
| 2469 | + * values and either invoke the driver's ->setpolicy() callback (if present) or |
|---|
| 2470 | + * carry out a governor update for @policy. That is, run the current governor's |
|---|
| 2471 | + * ->limits() callback (if @new_gov points to the same object as the one in |
|---|
| 2472 | + * @policy) or replace the governor for @policy with @new_gov. |
|---|
| 2473 | + * |
|---|
| 2474 | + * The cpuinfo part of @policy is not updated by this function. |
|---|
| 2238 | 2475 | */ |
|---|
| 2239 | 2476 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
|---|
| 2240 | | - struct cpufreq_policy *new_policy) |
|---|
| 2477 | + struct cpufreq_governor *new_gov, |
|---|
| 2478 | + unsigned int new_pol) |
|---|
| 2241 | 2479 | { |
|---|
| 2480 | + struct cpufreq_policy_data new_data; |
|---|
| 2242 | 2481 | struct cpufreq_governor *old_gov; |
|---|
| 2243 | 2482 | int ret; |
|---|
| 2244 | 2483 | |
|---|
| 2245 | | - pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
|---|
| 2246 | | - new_policy->cpu, new_policy->min, new_policy->max); |
|---|
| 2247 | | - |
|---|
| 2248 | | - memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
|---|
| 2249 | | - |
|---|
| 2484 | + memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
|---|
| 2485 | + new_data.freq_table = policy->freq_table; |
|---|
| 2486 | + new_data.cpu = policy->cpu; |
|---|
| 2250 | 2487 | /* |
|---|
| 2251 | | - * This check works well when we store new min/max freq attributes, |
|---|
| 2252 | | - * because new_policy is a copy of policy with one field updated. |
|---|
| 2253 | | - */ |
|---|
| 2254 | | - if (new_policy->min > new_policy->max) |
|---|
| 2255 | | - return -EINVAL; |
|---|
| 2256 | | - |
|---|
| 2257 | | - /* verify the cpu speed can be set within this limit */ |
|---|
| 2258 | | - ret = cpufreq_driver->verify(new_policy); |
|---|
| 2259 | | - if (ret) |
|---|
| 2260 | | - return ret; |
|---|
| 2261 | | - |
|---|
| 2262 | | - /* adjust if necessary - all reasons */ |
|---|
| 2263 | | - blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
|---|
| 2264 | | - CPUFREQ_ADJUST, new_policy); |
|---|
| 2265 | | - |
|---|
| 2266 | | - /* |
|---|
| 2267 | | - * verify the cpu speed can be set within this limit, which might be |
|---|
| 2268 | | - * different to the first one |
|---|
| 2488 | + * PM QoS framework collects all the requests from users and provide us |
|---|
| 2489 | + * the final aggregated value here. |
|---|
| 2269 | 2490 | */ |
|---|
| 2270 | | - ret = cpufreq_driver->verify(new_policy); |
|---|
| 2491 | + new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); |
|---|
| 2492 | + new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); |
|---|
| 2493 | + |
|---|
| 2494 | + pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
|---|
| 2495 | + new_data.cpu, new_data.min, new_data.max); |
|---|
| 2496 | + |
|---|
| 2497 | + /* |
|---|
| 2498 | + * Verify that the CPU speed can be set within these limits and make sure |
|---|
| 2499 | + * that min <= max. |
|---|
| 2500 | + */ |
|---|
| 2501 | + ret = cpufreq_driver->verify(&new_data); |
|---|
| 2271 | 2502 | if (ret) |
|---|
| 2272 | 2503 | return ret; |
|---|
| 2273 | 2504 | |
|---|
| 2274 | | - /* notification of the new policy */ |
|---|
| 2275 | | - blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
|---|
| 2276 | | - CPUFREQ_NOTIFY, new_policy); |
|---|
| 2277 | | - |
|---|
| 2278 | | - policy->min = new_policy->min; |
|---|
| 2279 | | - policy->max = new_policy->max; |
|---|
| 2505 | + policy->min = new_data.min; |
|---|
| 2506 | + policy->max = new_data.max; |
|---|
| 2280 | 2507 | trace_cpu_frequency_limits(policy); |
|---|
| 2281 | | - |
|---|
| 2282 | | - arch_set_max_freq_scale(policy->cpus, policy->max); |
|---|
| 2283 | 2508 | |
|---|
| 2284 | 2509 | policy->cached_target_freq = UINT_MAX; |
|---|
| 2285 | 2510 | |
|---|
| .. | .. |
|---|
| 2287 | 2512 | policy->min, policy->max); |
|---|
| 2288 | 2513 | |
|---|
| 2289 | 2514 | if (cpufreq_driver->setpolicy) { |
|---|
| 2290 | | - policy->policy = new_policy->policy; |
|---|
| 2515 | + policy->policy = new_pol; |
|---|
| 2291 | 2516 | pr_debug("setting range\n"); |
|---|
| 2292 | | - return cpufreq_driver->setpolicy(new_policy); |
|---|
| 2517 | + return cpufreq_driver->setpolicy(policy); |
|---|
| 2293 | 2518 | } |
|---|
| 2294 | 2519 | |
|---|
| 2295 | | - if (new_policy->governor == policy->governor) { |
|---|
| 2296 | | - pr_debug("cpufreq: governor limits update\n"); |
|---|
| 2520 | + if (new_gov == policy->governor) { |
|---|
| 2521 | + pr_debug("governor limits update\n"); |
|---|
| 2297 | 2522 | cpufreq_governor_limits(policy); |
|---|
| 2298 | 2523 | return 0; |
|---|
| 2299 | 2524 | } |
|---|
| .. | .. |
|---|
| 2309 | 2534 | } |
|---|
| 2310 | 2535 | |
|---|
| 2311 | 2536 | /* start new governor */ |
|---|
| 2312 | | - policy->governor = new_policy->governor; |
|---|
| 2537 | + policy->governor = new_gov; |
|---|
| 2313 | 2538 | ret = cpufreq_init_governor(policy); |
|---|
| 2314 | 2539 | if (!ret) { |
|---|
| 2315 | 2540 | ret = cpufreq_start_governor(policy); |
|---|
| 2316 | 2541 | if (!ret) { |
|---|
| 2317 | | - pr_debug("cpufreq: governor change\n"); |
|---|
| 2542 | + pr_debug("governor change\n"); |
|---|
| 2318 | 2543 | return 0; |
|---|
| 2319 | 2544 | } |
|---|
| 2320 | 2545 | cpufreq_exit_governor(policy); |
|---|
| .. | .. |
|---|
| 2332 | 2557 | |
|---|
| 2333 | 2558 | return ret; |
|---|
| 2334 | 2559 | } |
|---|
| 2560 | +EXPORT_TRACEPOINT_SYMBOL_GPL(cpu_frequency_limits); |
|---|
| 2335 | 2561 | |
|---|
| 2336 | 2562 | /** |
|---|
| 2337 | | - * cpufreq_update_policy - re-evaluate an existing cpufreq policy |
|---|
| 2338 | | - * @cpu: CPU which shall be re-evaluated |
|---|
| 2563 | + * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. |
|---|
| 2564 | + * @cpu: CPU to re-evaluate the policy for. |
|---|
| 2339 | 2565 | * |
|---|
| 2340 | | - * Useful for policy notifiers which have different necessities |
|---|
| 2341 | | - * at different times. |
|---|
| 2566 | + * Update the current frequency for the cpufreq policy of @cpu and use |
|---|
| 2567 | + * cpufreq_set_policy() to re-apply the min and max limits, which triggers the |
|---|
| 2568 | + * evaluation of policy notifiers and the cpufreq driver's ->verify() callback |
|---|
| 2569 | + * for the policy in question, among other things. |
|---|
| 2342 | 2570 | */ |
|---|
| 2343 | 2571 | void cpufreq_update_policy(unsigned int cpu) |
|---|
| 2344 | 2572 | { |
|---|
| 2345 | | - struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
|---|
| 2346 | | - struct cpufreq_policy new_policy; |
|---|
| 2573 | + struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); |
|---|
| 2347 | 2574 | |
|---|
| 2348 | 2575 | if (!policy) |
|---|
| 2349 | 2576 | return; |
|---|
| 2350 | | - |
|---|
| 2351 | | - down_write(&policy->rwsem); |
|---|
| 2352 | | - |
|---|
| 2353 | | - if (policy_is_inactive(policy)) |
|---|
| 2354 | | - goto unlock; |
|---|
| 2355 | | - |
|---|
| 2356 | | - pr_debug("updating policy for CPU %u\n", cpu); |
|---|
| 2357 | | - memcpy(&new_policy, policy, sizeof(*policy)); |
|---|
| 2358 | | - new_policy.min = policy->user_policy.min; |
|---|
| 2359 | | - new_policy.max = policy->user_policy.max; |
|---|
| 2360 | 2577 | |
|---|
| 2361 | 2578 | /* |
|---|
| 2362 | 2579 | * BIOS might change freq behind our back |
|---|
| 2363 | 2580 | * -> ask driver for current freq and notify governors about a change |
|---|
| 2364 | 2581 | */ |
|---|
| 2365 | | - if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
|---|
| 2366 | | - if (cpufreq_suspended) |
|---|
| 2367 | | - goto unlock; |
|---|
| 2582 | + if (cpufreq_driver->get && has_target() && |
|---|
| 2583 | + (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) |
|---|
| 2584 | + goto unlock; |
|---|
| 2368 | 2585 | |
|---|
| 2369 | | - new_policy.cur = cpufreq_update_current_freq(policy); |
|---|
| 2370 | | - if (WARN_ON(!new_policy.cur)) |
|---|
| 2371 | | - goto unlock; |
|---|
| 2372 | | - } |
|---|
| 2373 | | - |
|---|
| 2374 | | - cpufreq_set_policy(policy, &new_policy); |
|---|
| 2586 | + refresh_frequency_limits(policy); |
|---|
| 2375 | 2587 | |
|---|
| 2376 | 2588 | unlock: |
|---|
| 2377 | | - up_write(&policy->rwsem); |
|---|
| 2378 | | - |
|---|
| 2379 | | - cpufreq_cpu_put(policy); |
|---|
| 2589 | + cpufreq_cpu_release(policy); |
|---|
| 2380 | 2590 | } |
|---|
| 2381 | 2591 | EXPORT_SYMBOL(cpufreq_update_policy); |
|---|
| 2592 | + |
|---|
| 2593 | +/** |
|---|
| 2594 | + * cpufreq_update_limits - Update policy limits for a given CPU. |
|---|
| 2595 | + * @cpu: CPU to update the policy limits for. |
|---|
| 2596 | + * |
|---|
| 2597 | + * Invoke the driver's ->update_limits callback if present or call |
|---|
| 2598 | + * cpufreq_update_policy() for @cpu. |
|---|
| 2599 | + */ |
|---|
| 2600 | +void cpufreq_update_limits(unsigned int cpu) |
|---|
| 2601 | +{ |
|---|
| 2602 | + if (cpufreq_driver->update_limits) |
|---|
| 2603 | + cpufreq_driver->update_limits(cpu); |
|---|
| 2604 | + else |
|---|
| 2605 | + cpufreq_update_policy(cpu); |
|---|
| 2606 | +} |
|---|
| 2607 | +EXPORT_SYMBOL_GPL(cpufreq_update_limits); |
|---|
| 2382 | 2608 | |
|---|
| 2383 | 2609 | /********************************************************************* |
|---|
| 2384 | 2610 | * BOOST * |
|---|
| 2385 | 2611 | *********************************************************************/ |
|---|
| 2386 | | -static int cpufreq_boost_set_sw(int state) |
|---|
| 2612 | +static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) |
|---|
| 2387 | 2613 | { |
|---|
| 2388 | | - struct cpufreq_policy *policy; |
|---|
| 2389 | | - int ret = -EINVAL; |
|---|
| 2614 | + int ret; |
|---|
| 2390 | 2615 | |
|---|
| 2391 | | - for_each_active_policy(policy) { |
|---|
| 2392 | | - if (!policy->freq_table) |
|---|
| 2393 | | - continue; |
|---|
| 2616 | + if (!policy->freq_table) |
|---|
| 2617 | + return -ENXIO; |
|---|
| 2394 | 2618 | |
|---|
| 2395 | | - ret = cpufreq_frequency_table_cpuinfo(policy, |
|---|
| 2396 | | - policy->freq_table); |
|---|
| 2397 | | - if (ret) { |
|---|
| 2398 | | - pr_err("%s: Policy frequency update failed\n", |
|---|
| 2399 | | - __func__); |
|---|
| 2400 | | - break; |
|---|
| 2401 | | - } |
|---|
| 2402 | | - |
|---|
| 2403 | | - down_write(&policy->rwsem); |
|---|
| 2404 | | - policy->user_policy.max = policy->max; |
|---|
| 2405 | | - cpufreq_governor_limits(policy); |
|---|
| 2406 | | - up_write(&policy->rwsem); |
|---|
| 2619 | + ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); |
|---|
| 2620 | + if (ret) { |
|---|
| 2621 | + pr_err("%s: Policy frequency update failed\n", __func__); |
|---|
| 2622 | + return ret; |
|---|
| 2407 | 2623 | } |
|---|
| 2408 | 2624 | |
|---|
| 2409 | | - return ret; |
|---|
| 2625 | + ret = freq_qos_update_request(policy->max_freq_req, policy->max); |
|---|
| 2626 | + if (ret < 0) |
|---|
| 2627 | + return ret; |
|---|
| 2628 | + |
|---|
| 2629 | + return 0; |
|---|
| 2410 | 2630 | } |
|---|
| 2411 | 2631 | |
|---|
| 2412 | 2632 | int cpufreq_boost_trigger_state(int state) |
|---|
| 2413 | 2633 | { |
|---|
| 2634 | + struct cpufreq_policy *policy; |
|---|
| 2414 | 2635 | unsigned long flags; |
|---|
| 2415 | 2636 | int ret = 0; |
|---|
| 2416 | 2637 | |
|---|
| .. | .. |
|---|
| 2421 | 2642 | cpufreq_driver->boost_enabled = state; |
|---|
| 2422 | 2643 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
|---|
| 2423 | 2644 | |
|---|
| 2424 | | - ret = cpufreq_driver->set_boost(state); |
|---|
| 2425 | | - if (ret) { |
|---|
| 2426 | | - write_lock_irqsave(&cpufreq_driver_lock, flags); |
|---|
| 2427 | | - cpufreq_driver->boost_enabled = !state; |
|---|
| 2428 | | - write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
|---|
| 2429 | | - |
|---|
| 2430 | | - pr_err("%s: Cannot %s BOOST\n", |
|---|
| 2431 | | - __func__, state ? "enable" : "disable"); |
|---|
| 2645 | + get_online_cpus(); |
|---|
| 2646 | + for_each_active_policy(policy) { |
|---|
| 2647 | + ret = cpufreq_driver->set_boost(policy, state); |
|---|
| 2648 | + if (ret) |
|---|
| 2649 | + goto err_reset_state; |
|---|
| 2432 | 2650 | } |
|---|
| 2651 | + put_online_cpus(); |
|---|
| 2652 | + |
|---|
| 2653 | + return 0; |
|---|
| 2654 | + |
|---|
| 2655 | +err_reset_state: |
|---|
| 2656 | + put_online_cpus(); |
|---|
| 2657 | + |
|---|
| 2658 | + write_lock_irqsave(&cpufreq_driver_lock, flags); |
|---|
| 2659 | + cpufreq_driver->boost_enabled = !state; |
|---|
| 2660 | + write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
|---|
| 2661 | + |
|---|
| 2662 | + pr_err("%s: Cannot %s BOOST\n", |
|---|
| 2663 | + __func__, state ? "enable" : "disable"); |
|---|
| 2433 | 2664 | |
|---|
| 2434 | 2665 | return ret; |
|---|
| 2435 | 2666 | } |
|---|
| 2436 | 2667 | |
|---|
| 2437 | 2668 | static bool cpufreq_boost_supported(void) |
|---|
| 2438 | 2669 | { |
|---|
| 2439 | | - return likely(cpufreq_driver) && cpufreq_driver->set_boost; |
|---|
| 2670 | + return cpufreq_driver->set_boost; |
|---|
| 2440 | 2671 | } |
|---|
| 2441 | 2672 | |
|---|
| 2442 | 2673 | static int create_boost_sysfs_file(void) |
|---|
| .. | .. |
|---|
| 2527 | 2758 | driver_data->target) || |
|---|
| 2528 | 2759 | (driver_data->setpolicy && (driver_data->target_index || |
|---|
| 2529 | 2760 | driver_data->target)) || |
|---|
| 2530 | | - (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) |
|---|
| 2761 | + (!driver_data->get_intermediate != !driver_data->target_intermediate) || |
|---|
| 2762 | + (!driver_data->online != !driver_data->offline)) |
|---|
| 2531 | 2763 | return -EINVAL; |
|---|
| 2532 | 2764 | |
|---|
| 2533 | 2765 | pr_debug("trying to register driver %s\n", driver_data->name); |
|---|
| .. | .. |
|---|
| 2543 | 2775 | } |
|---|
| 2544 | 2776 | cpufreq_driver = driver_data; |
|---|
| 2545 | 2777 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
|---|
| 2778 | + |
|---|
| 2779 | + /* |
|---|
| 2780 | + * Mark support for the scheduler's frequency invariance engine for |
|---|
| 2781 | + * drivers that implement target(), target_index() or fast_switch(). |
|---|
| 2782 | + */ |
|---|
| 2783 | + if (!cpufreq_driver->setpolicy) { |
|---|
| 2784 | + static_branch_enable_cpuslocked(&cpufreq_freq_invariance); |
|---|
| 2785 | + pr_debug("supports frequency invariance"); |
|---|
| 2786 | + } |
|---|
| 2546 | 2787 | |
|---|
| 2547 | 2788 | if (driver_data->setpolicy) |
|---|
| 2548 | 2789 | driver_data->flags |= CPUFREQ_CONST_LOOPS; |
|---|
| .. | .. |
|---|
| 2592 | 2833 | } |
|---|
| 2593 | 2834 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); |
|---|
| 2594 | 2835 | |
|---|
| 2595 | | -/** |
|---|
| 2836 | +/* |
|---|
| 2596 | 2837 | * cpufreq_unregister_driver - unregister the current CPUFreq driver |
|---|
| 2597 | 2838 | * |
|---|
| 2598 | 2839 | * Unregister the current CPUFreq driver. Only call this if you have |
|---|
| .. | .. |
|---|
| 2613 | 2854 | cpus_read_lock(); |
|---|
| 2614 | 2855 | subsys_interface_unregister(&cpufreq_interface); |
|---|
| 2615 | 2856 | remove_boost_sysfs_file(); |
|---|
| 2857 | + static_branch_disable_cpuslocked(&cpufreq_freq_invariance); |
|---|
| 2616 | 2858 | cpuhp_remove_state_nocalls_cpuslocked(hp_online); |
|---|
| 2617 | 2859 | |
|---|
| 2618 | 2860 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
|---|
| .. | .. |
|---|
| 2626 | 2868 | } |
|---|
| 2627 | 2869 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); |
|---|
| 2628 | 2870 | |
|---|
| 2629 | | -struct kobject *cpufreq_global_kobject; |
|---|
| 2630 | | -EXPORT_SYMBOL(cpufreq_global_kobject); |
|---|
| 2631 | | - |
|---|
| 2632 | 2871 | static int __init cpufreq_core_init(void) |
|---|
| 2633 | 2872 | { |
|---|
| 2873 | + struct cpufreq_governor *gov = cpufreq_default_governor(); |
|---|
| 2874 | + |
|---|
| 2634 | 2875 | if (cpufreq_disabled()) |
|---|
| 2635 | 2876 | return -ENODEV; |
|---|
| 2636 | 2877 | |
|---|
| 2637 | 2878 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); |
|---|
| 2638 | 2879 | BUG_ON(!cpufreq_global_kobject); |
|---|
| 2639 | 2880 | |
|---|
| 2881 | + if (!strlen(default_governor)) |
|---|
| 2882 | + strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); |
|---|
| 2883 | + |
|---|
| 2640 | 2884 | return 0; |
|---|
| 2641 | 2885 | } |
|---|
| 2642 | 2886 | module_param(off, int, 0444); |
|---|
| 2887 | +module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); |
|---|
| 2643 | 2888 | core_initcall(cpufreq_core_init); |
|---|