at commit 8980bc57
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* |
| 3 | * linux/include/linux/cpufreq.h |
| 4 | * |
| 5 | * Copyright (C) 2001 Russell King |
| 6 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> |
| 7 | */ |
| 8 | #ifndef _LINUX_CPUFREQ_H |
| 9 | #define _LINUX_CPUFREQ_H |
| 10 | |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <linux/cpumask.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/kobject.h> |
| 16 | #include <linux/notifier.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/pm_opp.h> |
| 19 | #include <linux/pm_qos.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/sysfs.h> |
| 22 | #include <linux/minmax.h> |
| 23 | |
| 24 | /********************************************************************* |
| 25 | * CPUFREQ INTERFACE * |
| 26 | *********************************************************************/ |
| 27 | /* |
| 28 | * Frequency values here are CPU kHz |
| 29 | */ |
| 30 | |
| 31 | #define CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS NSEC_PER_MSEC |
| 32 | |
| 33 | #define CPUFREQ_NAME_LEN 16 |
| 34 | /* Print length for names. Extra 1 space for accommodating '\n' in prints */ |
| 35 | #define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1) |
| 36 | |
| 37 | struct cpufreq_governor; |
| 38 | |
| 39 | enum cpufreq_table_sorting { |
| 40 | CPUFREQ_TABLE_UNSORTED, |
| 41 | CPUFREQ_TABLE_SORTED_ASCENDING, |
| 42 | CPUFREQ_TABLE_SORTED_DESCENDING |
| 43 | }; |
| 44 | |
| 45 | struct cpufreq_cpuinfo { |
| 46 | unsigned int max_freq; |
| 47 | unsigned int min_freq; |
| 48 | |
| 49 | /* in 10^(-9) s = nanoseconds */ |
| 50 | unsigned int transition_latency; |
| 51 | }; |
| 52 | |
| 53 | struct cpufreq_policy { |
| 54 | /* CPUs sharing clock, require sw coordination */ |
| 55 | cpumask_var_t cpus; /* Online CPUs only */ |
| 56 | cpumask_var_t related_cpus; /* Online + Offline CPUs */ |
| 57 | cpumask_var_t real_cpus; /* Related and present */ |
| 58 | |
| 59 | unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs |
| 60 | should set cpufreq */ |
| 61 | unsigned int cpu; /* cpu managing this policy, must be online */ |
| 62 | |
| 63 | struct clk *clk; |
| 64 | struct cpufreq_cpuinfo cpuinfo;/* see above */ |
| 65 | |
| 66 | unsigned int min; /* in kHz */ |
| 67 | unsigned int max; /* in kHz */ |
| 68 | unsigned int cur; /* in kHz, only needed if cpufreq |
| 69 | * governors are used */ |
| 70 | unsigned int suspend_freq; /* freq to set during suspend */ |
| 71 | |
| 72 | unsigned int policy; /* see above */ |
| 73 | unsigned int last_policy; /* policy before unplug */ |
| 74 | struct cpufreq_governor *governor; /* see below */ |
| 75 | void *governor_data; |
| 76 | char last_governor[CPUFREQ_NAME_LEN]; /* last governor used */ |
| 77 | |
| 78 | struct work_struct update; /* if update_policy() needs to be |
| 79 | * called, but you're in IRQ context */ |
| 80 | |
| 81 | struct freq_constraints constraints; |
| 82 | struct freq_qos_request min_freq_req; |
| 83 | struct freq_qos_request max_freq_req; |
| 84 | struct freq_qos_request boost_freq_req; |
| 85 | |
| 86 | struct cpufreq_frequency_table *freq_table; |
| 87 | enum cpufreq_table_sorting freq_table_sorted; |
| 88 | |
| 89 | struct list_head policy_list; |
| 90 | struct kobject kobj; |
| 91 | struct completion kobj_unregister; |
| 92 | |
| 93 | /* |
| 94 | * The rules for this semaphore: |
| 95 | * - Any routine that wants to read from the policy structure will |
| 96 | * do a down_read on this semaphore. |
| 97 | * - Any routine that will write to the policy structure and/or may take away |
| 98 | * the policy altogether (eg. CPU hotplug), will hold this lock in write |
| 99 | * mode before doing so. |
| 100 | */ |
| 101 | struct rw_semaphore rwsem; |
| 102 | |
| 103 | /* |
| 104 | * Fast switch flags: |
| 105 | * - fast_switch_possible should be set by the driver if it can |
| 106 | * guarantee that frequency can be changed on any CPU sharing the |
| 107 | * policy and that the change will affect all of the policy CPUs then. |
| 108 | * - fast_switch_enabled is to be set by governors that support fast |
| 109 | * frequency switching with the help of cpufreq_enable_fast_switch(). |
| 110 | */ |
| 111 | bool fast_switch_possible; |
| 112 | bool fast_switch_enabled; |
| 113 | |
| 114 | /* |
| 115 | * Set if the CPUFREQ_GOV_STRICT_TARGET flag is set for the current |
| 116 | * governor. |
| 117 | */ |
| 118 | bool strict_target; |
| 119 | |
| 120 | /* |
| 121 | * Set if inefficient frequencies were found in the frequency table. |
| 122 | * This indicates if the relation flag CPUFREQ_RELATION_E can be |
| 123 | * honored. |
| 124 | */ |
| 125 | bool efficiencies_available; |
| 126 | |
| 127 | /* |
| 128 | * Preferred average time interval between consecutive invocations of |
| 129 | * the driver to set the frequency for this policy. To be set by the |
| 130 | * scaling driver (0, which is the default, means no preference). |
| 131 | */ |
| 132 | unsigned int transition_delay_us; |
| 133 | |
| 134 | /* |
| 135 | * Remote DVFS flag (Not added to the driver structure as we don't want |
| 136 | * to access another structure from scheduler hotpath). |
| 137 | * |
| 138 | * Should be set if CPUs can do DVFS on behalf of other CPUs from |
| 139 | * different cpufreq policies. |
| 140 | */ |
| 141 | bool dvfs_possible_from_any_cpu; |
| 142 | |
| 143 | /* Per policy boost enabled flag. */ |
| 144 | bool boost_enabled; |
| 145 | |
| 146 | /* Per policy boost supported flag. */ |
| 147 | bool boost_supported; |
| 148 | |
| 149 | /* Pending policy->min/max update for the driver */ |
| 150 | bool update_limits; |
| 151 | |
| 152 | /* Cached frequency lookup from cpufreq_driver_resolve_freq. */ |
| 153 | unsigned int cached_target_freq; |
| 154 | unsigned int cached_resolved_idx; |
| 155 | |
| 156 | /* Synchronization for frequency transitions */ |
| 157 | bool transition_ongoing; /* Tracks transition status */ |
| 158 | spinlock_t transition_lock; |
| 159 | wait_queue_head_t transition_wait; |
| 160 | struct task_struct *transition_task; /* Task which is doing the transition */ |
| 161 | |
| 162 | /* cpufreq-stats */ |
| 163 | struct cpufreq_stats *stats; |
| 164 | |
| 165 | /* For cpufreq driver's internal use */ |
| 166 | void *driver_data; |
| 167 | |
| 168 | /* Pointer to the cooling device if used for thermal mitigation */ |
| 169 | struct thermal_cooling_device *cdev; |
| 170 | |
| 171 | struct notifier_block nb_min; |
| 172 | struct notifier_block nb_max; |
| 173 | }; |
| 174 | |
| 175 | DEFINE_GUARD(cpufreq_policy_write, struct cpufreq_policy *, |
| 176 | down_write(&_T->rwsem), up_write(&_T->rwsem)) |
| 177 | |
| 178 | DEFINE_GUARD(cpufreq_policy_read, struct cpufreq_policy *, |
| 179 | down_read(&_T->rwsem), up_read(&_T->rwsem)) |
| 180 | |
| 181 | /* |
| 182 | * Used for passing new cpufreq policy data to the cpufreq driver's ->verify() |
| 183 | * callback for sanitization. That callback is only expected to modify the min |
| 184 | * and max values, if necessary, and specifically it must not update the |
| 185 | * frequency table. |
| 186 | */ |
| 187 | struct cpufreq_policy_data { |
| 188 | struct cpufreq_cpuinfo cpuinfo; |
| 189 | struct cpufreq_frequency_table *freq_table; |
| 190 | unsigned int cpu; |
| 191 | unsigned int min; /* in kHz */ |
| 192 | unsigned int max; /* in kHz */ |
| 193 | }; |
| 194 | |
| 195 | struct cpufreq_freqs { |
| 196 | struct cpufreq_policy *policy; |
| 197 | unsigned int old; |
| 198 | unsigned int new; |
| 199 | u8 flags; /* flags of cpufreq_driver, see below. */ |
| 200 | }; |
| 201 | |
| 202 | /* Only for ACPI */ |
| 203 | #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */ |
| 204 | #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */ |
| 205 | #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */ |
| 206 | #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/ |
| 207 | |
| 208 | #ifdef CONFIG_CPU_FREQ |
| 209 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu); |
| 210 | struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu); |
| 211 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu); |
| 212 | void cpufreq_cpu_put(struct cpufreq_policy *policy); |
| 213 | #else |
| 214 | static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
| 215 | { |
| 216 | return NULL; |
| 217 | } |
| 218 | static inline struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu) |
| 219 | { |
| 220 | return NULL; |
| 221 | } |
| 222 | static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
| 223 | { |
| 224 | return NULL; |
| 225 | } |
| 226 | static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { } |
| 227 | #endif |
| 228 | |
| 229 | /* Scope based cleanup macro for cpufreq_policy kobject reference counting */ |
| 230 | DEFINE_FREE(put_cpufreq_policy, struct cpufreq_policy *, if (_T) cpufreq_cpu_put(_T)) |
| 231 | |
| 232 | static inline bool policy_is_inactive(struct cpufreq_policy *policy) |
| 233 | { |
| 234 | return cpumask_empty(policy->cpus); |
| 235 | } |
| 236 | |
| 237 | static inline bool policy_is_shared(struct cpufreq_policy *policy) |
| 238 | { |
| 239 | return cpumask_nth(1, policy->cpus) < nr_cpumask_bits; |
| 240 | } |
| 241 | |
| 242 | #ifdef CONFIG_CPU_FREQ |
| 243 | unsigned int cpufreq_get(unsigned int cpu); |
| 244 | unsigned int cpufreq_quick_get(unsigned int cpu); |
| 245 | unsigned int cpufreq_quick_get_max(unsigned int cpu); |
| 246 | unsigned int cpufreq_get_hw_max_freq(unsigned int cpu); |
| 247 | void disable_cpufreq(void); |
| 248 | |
| 249 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy); |
| 250 | |
| 251 | void refresh_frequency_limits(struct cpufreq_policy *policy); |
| 252 | void cpufreq_update_policy(unsigned int cpu); |
| 253 | void cpufreq_update_limits(unsigned int cpu); |
| 254 | bool have_governor_per_policy(void); |
| 255 | bool cpufreq_supports_freq_invariance(void); |
| 256 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy); |
| 257 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy); |
| 258 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy); |
| 259 | bool has_target_index(void); |
| 260 | |
| 261 | DECLARE_PER_CPU(unsigned long, cpufreq_pressure); |
| 262 | static inline unsigned long cpufreq_get_pressure(int cpu) |
| 263 | { |
| 264 | return READ_ONCE(per_cpu(cpufreq_pressure, cpu)); |
| 265 | } |
| 266 | #else |
| 267 | static inline unsigned int cpufreq_get(unsigned int cpu) |
| 268 | { |
| 269 | return 0; |
| 270 | } |
| 271 | static inline unsigned int cpufreq_quick_get(unsigned int cpu) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
| 275 | static inline unsigned int cpufreq_quick_get_max(unsigned int cpu) |
| 276 | { |
| 277 | return 0; |
| 278 | } |
| 279 | static inline unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) |
| 280 | { |
| 281 | return 0; |
| 282 | } |
| 283 | static inline bool cpufreq_supports_freq_invariance(void) |
| 284 | { |
| 285 | return false; |
| 286 | } |
| 287 | static inline void disable_cpufreq(void) { } |
| 288 | static inline void cpufreq_update_limits(unsigned int cpu) { } |
| 289 | static inline unsigned long cpufreq_get_pressure(int cpu) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | #ifdef CONFIG_CPU_FREQ_STAT |
| 296 | void cpufreq_stats_create_table(struct cpufreq_policy *policy); |
| 297 | void cpufreq_stats_free_table(struct cpufreq_policy *policy); |
| 298 | void cpufreq_stats_record_transition(struct cpufreq_policy *policy, |
| 299 | unsigned int new_freq); |
| 300 | #else |
| 301 | static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { } |
| 302 | static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { } |
| 303 | static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy, |
| 304 | unsigned int new_freq) { } |
| 305 | #endif /* CONFIG_CPU_FREQ_STAT */ |
| 306 | |
| 307 | /********************************************************************* |
| 308 | * CPUFREQ DRIVER INTERFACE * |
| 309 | *********************************************************************/ |
| 310 | |
| 311 | #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */ |
| 312 | #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */ |
| 313 | #define CPUFREQ_RELATION_C 2 /* closest frequency to target */ |
| 314 | /* relation flags */ |
| 315 | #define CPUFREQ_RELATION_E BIT(2) /* Get if possible an efficient frequency */ |
| 316 | |
| 317 | #define CPUFREQ_RELATION_LE (CPUFREQ_RELATION_L | CPUFREQ_RELATION_E) |
| 318 | #define CPUFREQ_RELATION_HE (CPUFREQ_RELATION_H | CPUFREQ_RELATION_E) |
| 319 | #define CPUFREQ_RELATION_CE (CPUFREQ_RELATION_C | CPUFREQ_RELATION_E) |
| 320 | |
| 321 | struct freq_attr { |
| 322 | struct attribute attr; |
| 323 | ssize_t (*show)(struct cpufreq_policy *, char *); |
| 324 | ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count); |
| 325 | }; |
| 326 | |
| 327 | #define cpufreq_freq_attr_ro(_name) \ |
| 328 | static struct freq_attr _name = \ |
| 329 | __ATTR(_name, 0444, show_##_name, NULL) |
| 330 | |
| 331 | #define cpufreq_freq_attr_ro_perm(_name, _perm) \ |
| 332 | static struct freq_attr _name = \ |
| 333 | __ATTR(_name, _perm, show_##_name, NULL) |
| 334 | |
| 335 | #define cpufreq_freq_attr_rw(_name) \ |
| 336 | static struct freq_attr _name = \ |
| 337 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 338 | |
| 339 | #define cpufreq_freq_attr_wo(_name) \ |
| 340 | static struct freq_attr _name = \ |
| 341 | __ATTR(_name, 0200, NULL, store_##_name) |
| 342 | |
| 343 | #define define_one_global_ro(_name) \ |
| 344 | static struct kobj_attribute _name = \ |
| 345 | __ATTR(_name, 0444, show_##_name, NULL) |
| 346 | |
| 347 | #define define_one_global_rw(_name) \ |
| 348 | static struct kobj_attribute _name = \ |
| 349 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 350 | |
| 351 | |
| 352 | struct cpufreq_driver { |
| 353 | char name[CPUFREQ_NAME_LEN]; |
| 354 | u16 flags; |
| 355 | void *driver_data; |
| 356 | |
| 357 | /* needed by all drivers */ |
| 358 | int (*init)(struct cpufreq_policy *policy); |
| 359 | int (*verify)(struct cpufreq_policy_data *policy); |
| 360 | |
| 361 | /* define one out of two */ |
| 362 | int (*setpolicy)(struct cpufreq_policy *policy); |
| 363 | |
| 364 | int (*target)(struct cpufreq_policy *policy, |
| 365 | unsigned int target_freq, |
| 366 | unsigned int relation); /* Deprecated */ |
| 367 | int (*target_index)(struct cpufreq_policy *policy, |
| 368 | unsigned int index); |
| 369 | unsigned int (*fast_switch)(struct cpufreq_policy *policy, |
| 370 | unsigned int target_freq); |
| 371 | /* |
| 372 | * ->fast_switch() replacement for drivers that use an internal |
| 373 | * representation of performance levels and can pass hints other than |
| 374 | * the target performance level to the hardware. This can only be set |
| 375 | * if ->fast_switch is set too, because in those cases (under specific |
| 376 | * conditions) scale invariance can be disabled, which causes the |
| 377 | * schedutil governor to fall back to the latter. |
| 378 | */ |
| 379 | void (*adjust_perf)(struct cpufreq_policy *policy, |
| 380 | unsigned long min_perf, |
| 381 | unsigned long target_perf, |
| 382 | unsigned long max_perf, |
| 383 | unsigned long capacity); |
| 384 | |
| 385 | /* |
| 386 | * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION |
| 387 | * unset. |
| 388 | * |
| 389 | * get_intermediate should return a stable intermediate frequency |
| 390 | * platform wants to switch to and target_intermediate() should set CPU |
| 391 | * to that frequency, before jumping to the frequency corresponding |
| 392 | * to 'index'. Core will take care of sending notifications and driver |
| 393 | * doesn't have to handle them in target_intermediate() or |
| 394 | * target_index(). |
| 395 | * |
| 396 | * Drivers can return '0' from get_intermediate() in case they don't |
| 397 | * wish to switch to intermediate frequency for some target frequency. |
| 398 | * In that case core will directly call ->target_index(). |
| 399 | */ |
| 400 | unsigned int (*get_intermediate)(struct cpufreq_policy *policy, |
| 401 | unsigned int index); |
| 402 | int (*target_intermediate)(struct cpufreq_policy *policy, |
| 403 | unsigned int index); |
| 404 | |
| 405 | /* should be defined, if possible, return 0 on error */ |
| 406 | unsigned int (*get)(unsigned int cpu); |
| 407 | |
| 408 | /* Called to update policy limits on firmware notifications. */ |
| 409 | void (*update_limits)(struct cpufreq_policy *policy); |
| 410 | |
| 411 | /* optional */ |
| 412 | int (*bios_limit)(int cpu, unsigned int *limit); |
| 413 | |
| 414 | int (*online)(struct cpufreq_policy *policy); |
| 415 | int (*offline)(struct cpufreq_policy *policy); |
| 416 | void (*exit)(struct cpufreq_policy *policy); |
| 417 | int (*suspend)(struct cpufreq_policy *policy); |
| 418 | int (*resume)(struct cpufreq_policy *policy); |
| 419 | |
| 420 | /* Will be called after the driver is fully initialized */ |
| 421 | void (*ready)(struct cpufreq_policy *policy); |
| 422 | |
| 423 | /* Return the capacity reference frequency for policy. */ |
| 424 | unsigned int (*scale_freq_ref)(struct cpufreq_policy *policy); |
| 425 | |
| 426 | struct freq_attr **attr; |
| 427 | |
| 428 | /* platform specific boost support code */ |
| 429 | bool boost_enabled; |
| 430 | int (*set_boost)(struct cpufreq_policy *policy, int state); |
| 431 | |
| 432 | /* |
| 433 | * Set by drivers that want to register with the energy model after the |
| 434 | * policy is properly initialized, but before the governor is started. |
| 435 | */ |
| 436 | void (*register_em)(struct cpufreq_policy *policy); |
| 437 | }; |
| 438 | |
| 439 | /* flags */ |
| 440 | |
| 441 | /* |
| 442 | * Set by drivers that need to update internal upper and lower boundaries along |
| 443 | * with the target frequency and so the core and governors should also invoke |
| 444 | * the driver if the target frequency does not change, but the policy min or max |
| 445 | * may have changed. |
| 446 | */ |
| 447 | #define CPUFREQ_NEED_UPDATE_LIMITS BIT(0) |
| 448 | |
| 449 | /* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */ |
| 450 | #define CPUFREQ_CONST_LOOPS BIT(1) |
| 451 | |
| 452 | /* |
| 453 | * Set by drivers that want the core to automatically register the cpufreq |
| 454 | * driver as a thermal cooling device. |
| 455 | */ |
| 456 | #define CPUFREQ_IS_COOLING_DEV BIT(2) |
| 457 | |
| 458 | /* |
| 459 | * This should be set by platforms having multiple clock-domains, i.e. |
| 460 | * supporting multiple policies. With this sysfs directories of governor would |
| 461 | * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same |
| 462 | * governor with different tunables for different clusters. |
| 463 | */ |
| 464 | #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY BIT(3) |
| 465 | |
| 466 | /* |
| 467 | * Driver will do POSTCHANGE notifications from outside of their ->target() |
| 468 | * routine and so must set cpufreq_driver->flags with this flag, so that core |
| 469 | * can handle them specially. |
| 470 | */ |
| 471 | #define CPUFREQ_ASYNC_NOTIFICATION BIT(4) |
| 472 | |
| 473 | /* |
| 474 | * Set by drivers which want cpufreq core to check if CPU is running at a |
| 475 | * frequency present in freq-table exposed by the driver. For these drivers if |
| 476 | * CPU is found running at an out of table freq, we will try to set it to a freq |
| 477 | * from the table. And if that fails, we will stop further boot process by |
| 478 | * issuing a BUG_ON(). |
| 479 | */ |
| 480 | #define CPUFREQ_NEED_INITIAL_FREQ_CHECK BIT(5) |
| 481 | |
| 482 | /* |
| 483 | * Set by drivers to disallow use of governors with "dynamic_switching" flag |
| 484 | * set. |
| 485 | */ |
| 486 | #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING BIT(6) |
| 487 | |
| 488 | int cpufreq_register_driver(struct cpufreq_driver *driver_data); |
| 489 | void cpufreq_unregister_driver(struct cpufreq_driver *driver_data); |
| 490 | |
| 491 | bool cpufreq_driver_test_flags(u16 flags); |
| 492 | const char *cpufreq_get_current_driver(void); |
| 493 | void *cpufreq_get_driver_data(void); |
| 494 | |
| 495 | static inline int cpufreq_thermal_control_enabled(struct cpufreq_driver *drv) |
| 496 | { |
| 497 | return IS_ENABLED(CONFIG_CPU_THERMAL) && |
| 498 | (drv->flags & CPUFREQ_IS_COOLING_DEV); |
| 499 | } |
| 500 | |
| 501 | static inline void cpufreq_verify_within_limits(struct cpufreq_policy_data *policy, |
| 502 | unsigned int min, |
| 503 | unsigned int max) |
| 504 | { |
| 505 | policy->max = clamp(policy->max, min, max); |
| 506 | policy->min = clamp(policy->min, min, policy->max); |
| 507 | } |
| 508 | |
| 509 | static inline void |
| 510 | cpufreq_verify_within_cpu_limits(struct cpufreq_policy_data *policy) |
| 511 | { |
| 512 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 513 | policy->cpuinfo.max_freq); |
| 514 | } |
| 515 | |
| 516 | #ifdef CONFIG_CPU_FREQ |
| 517 | void cpufreq_suspend(void); |
| 518 | void cpufreq_resume(void); |
| 519 | int cpufreq_generic_suspend(struct cpufreq_policy *policy); |
| 520 | #else |
| 521 | static inline void cpufreq_suspend(void) {} |
| 522 | static inline void cpufreq_resume(void) {} |
| 523 | #endif |
| 524 | |
| 525 | /********************************************************************* |
| 526 | * CPUFREQ NOTIFIER INTERFACE * |
| 527 | *********************************************************************/ |
| 528 | |
| 529 | #define CPUFREQ_TRANSITION_NOTIFIER (0) |
| 530 | #define CPUFREQ_POLICY_NOTIFIER (1) |
| 531 | |
| 532 | /* Transition notifiers */ |
| 533 | #define CPUFREQ_PRECHANGE (0) |
| 534 | #define CPUFREQ_POSTCHANGE (1) |
| 535 | |
| 536 | /* Policy Notifiers */ |
| 537 | #define CPUFREQ_CREATE_POLICY (0) |
| 538 | #define CPUFREQ_REMOVE_POLICY (1) |
| 539 | |
| 540 | #ifdef CONFIG_CPU_FREQ |
| 541 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list); |
| 542 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list); |
| 543 | |
| 544 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
| 545 | struct cpufreq_freqs *freqs); |
| 546 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, |
| 547 | struct cpufreq_freqs *freqs, int transition_failed); |
| 548 | |
| 549 | #else /* CONFIG_CPU_FREQ */ |
| 550 | static inline int cpufreq_register_notifier(struct notifier_block *nb, |
| 551 | unsigned int list) |
| 552 | { |
| 553 | return 0; |
| 554 | } |
| 555 | static inline int cpufreq_unregister_notifier(struct notifier_block *nb, |
| 556 | unsigned int list) |
| 557 | { |
| 558 | return 0; |
| 559 | } |
| 560 | #endif /* !CONFIG_CPU_FREQ */ |
| 561 | |
| 562 | /** |
| 563 | * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch |
| 564 | * safe) |
| 565 | * @old: old value |
| 566 | * @div: divisor |
| 567 | * @mult: multiplier |
| 568 | * |
| 569 | * |
| 570 | * new = old * mult / div |
| 571 | */ |
| 572 | static inline unsigned long cpufreq_scale(unsigned long old, u_int div, |
| 573 | u_int mult) |
| 574 | { |
| 575 | #if BITS_PER_LONG == 32 |
| 576 | u64 result = ((u64) old) * ((u64) mult); |
| 577 | do_div(result, div); |
| 578 | return (unsigned long) result; |
| 579 | |
| 580 | #elif BITS_PER_LONG == 64 |
| 581 | unsigned long result = old * ((u64) mult); |
| 582 | result /= div; |
| 583 | return result; |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | /********************************************************************* |
| 588 | * CPUFREQ GOVERNORS * |
| 589 | *********************************************************************/ |
| 590 | |
| 591 | #define CPUFREQ_POLICY_UNKNOWN (0) |
| 592 | /* |
| 593 | * If (cpufreq_driver->target) exists, the ->governor decides what frequency |
| 594 | * within the limits is used. If (cpufreq_driver->setpolicy> exists, these |
| 595 | * two generic policies are available: |
| 596 | */ |
| 597 | #define CPUFREQ_POLICY_POWERSAVE (1) |
| 598 | #define CPUFREQ_POLICY_PERFORMANCE (2) |
| 599 | |
| 600 | struct cpufreq_governor { |
| 601 | char name[CPUFREQ_NAME_LEN]; |
| 602 | int (*init)(struct cpufreq_policy *policy); |
| 603 | void (*exit)(struct cpufreq_policy *policy); |
| 604 | int (*start)(struct cpufreq_policy *policy); |
| 605 | void (*stop)(struct cpufreq_policy *policy); |
| 606 | void (*limits)(struct cpufreq_policy *policy); |
| 607 | ssize_t (*show_setspeed) (struct cpufreq_policy *policy, |
| 608 | char *buf); |
| 609 | int (*store_setspeed) (struct cpufreq_policy *policy, |
| 610 | unsigned int freq); |
| 611 | struct list_head governor_list; |
| 612 | struct module *owner; |
| 613 | u8 flags; |
| 614 | }; |
| 615 | |
| 616 | /* Governor flags */ |
| 617 | |
| 618 | /* For governors which change frequency dynamically by themselves */ |
| 619 | #define CPUFREQ_GOV_DYNAMIC_SWITCHING BIT(0) |
| 620 | |
| 621 | /* For governors wanting the target frequency to be set exactly */ |
| 622 | #define CPUFREQ_GOV_STRICT_TARGET BIT(1) |
| 623 | |
| 624 | |
| 625 | /* Pass a target to the cpufreq driver */ |
| 626 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, |
| 627 | unsigned int target_freq); |
| 628 | void cpufreq_driver_adjust_perf(struct cpufreq_policy *policy, |
| 629 | unsigned long min_perf, |
| 630 | unsigned long target_perf, |
| 631 | unsigned long max_perf, |
| 632 | unsigned long capacity); |
| 633 | bool cpufreq_driver_has_adjust_perf(void); |
| 634 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
| 635 | unsigned int target_freq, |
| 636 | unsigned int relation); |
| 637 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
| 638 | unsigned int target_freq, |
| 639 | unsigned int relation); |
| 640 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, |
| 641 | unsigned int target_freq); |
| 642 | unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy); |
| 643 | int cpufreq_register_governor(struct cpufreq_governor *governor); |
| 644 | void cpufreq_unregister_governor(struct cpufreq_governor *governor); |
| 645 | int cpufreq_start_governor(struct cpufreq_policy *policy); |
| 646 | void cpufreq_stop_governor(struct cpufreq_policy *policy); |
| 647 | |
| 648 | #define cpufreq_governor_init(__governor) \ |
| 649 | static int __init __governor##_init(void) \ |
| 650 | { \ |
| 651 | return cpufreq_register_governor(&__governor); \ |
| 652 | } \ |
| 653 | core_initcall(__governor##_init) |
| 654 | |
| 655 | #define cpufreq_governor_exit(__governor) \ |
| 656 | static void __exit __governor##_exit(void) \ |
| 657 | { \ |
| 658 | return cpufreq_unregister_governor(&__governor); \ |
| 659 | } \ |
| 660 | module_exit(__governor##_exit) |
| 661 | |
| 662 | struct cpufreq_governor *cpufreq_default_governor(void); |
| 663 | struct cpufreq_governor *cpufreq_fallback_governor(void); |
| 664 | |
| 665 | #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL |
| 666 | bool sugov_is_governor(struct cpufreq_policy *policy); |
| 667 | #else |
| 668 | static inline bool sugov_is_governor(struct cpufreq_policy *policy) |
| 669 | { |
| 670 | return false; |
| 671 | } |
| 672 | #endif |
| 673 | |
| 674 | static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy) |
| 675 | { |
| 676 | if (policy->max < policy->cur) |
| 677 | __cpufreq_driver_target(policy, policy->max, |
| 678 | CPUFREQ_RELATION_HE); |
| 679 | else if (policy->min > policy->cur) |
| 680 | __cpufreq_driver_target(policy, policy->min, |
| 681 | CPUFREQ_RELATION_LE); |
| 682 | } |
| 683 | |
| 684 | /* Governor attribute set */ |
| 685 | struct gov_attr_set { |
| 686 | struct kobject kobj; |
| 687 | struct list_head policy_list; |
| 688 | struct mutex update_lock; |
| 689 | int usage_count; |
| 690 | }; |
| 691 | |
| 692 | /* sysfs ops for cpufreq governors */ |
| 693 | extern const struct sysfs_ops governor_sysfs_ops; |
| 694 | |
| 695 | static inline struct gov_attr_set *to_gov_attr_set(struct kobject *kobj) |
| 696 | { |
| 697 | return container_of(kobj, struct gov_attr_set, kobj); |
| 698 | } |
| 699 | |
| 700 | void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node); |
| 701 | void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node); |
| 702 | unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node); |
| 703 | |
| 704 | /* Governor sysfs attribute */ |
| 705 | struct governor_attr { |
| 706 | struct attribute attr; |
| 707 | ssize_t (*show)(struct gov_attr_set *attr_set, char *buf); |
| 708 | ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf, |
| 709 | size_t count); |
| 710 | }; |
| 711 | |
| 712 | /********************************************************************* |
| 713 | * FREQUENCY TABLE HELPERS * |
| 714 | *********************************************************************/ |
| 715 | |
| 716 | /* Special Values of .frequency field */ |
| 717 | #define CPUFREQ_ENTRY_INVALID ~0u |
| 718 | #define CPUFREQ_TABLE_END ~1u |
| 719 | /* Special Values of .flags field */ |
| 720 | #define CPUFREQ_BOOST_FREQ (1 << 0) |
| 721 | #define CPUFREQ_INEFFICIENT_FREQ (1 << 1) |
| 722 | |
| 723 | struct cpufreq_frequency_table { |
| 724 | unsigned int flags; |
| 725 | unsigned int driver_data; /* driver specific data, not used by core */ |
| 726 | unsigned int frequency; /* kHz - doesn't need to be in ascending |
| 727 | * order */ |
| 728 | }; |
| 729 | |
| 730 | /* |
| 731 | * cpufreq_for_each_entry - iterate over a cpufreq_frequency_table |
| 732 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. |
| 733 | * @table: the cpufreq_frequency_table * to iterate over. |
| 734 | */ |
| 735 | |
| 736 | #define cpufreq_for_each_entry(pos, table) \ |
| 737 | for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) |
| 738 | |
| 739 | /* |
| 740 | * cpufreq_for_each_entry_idx - iterate over a cpufreq_frequency_table |
| 741 | * with index |
| 742 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. |
| 743 | * @table: the cpufreq_frequency_table * to iterate over. |
| 744 | * @idx: the table entry currently being processed |
| 745 | */ |
| 746 | |
| 747 | #define cpufreq_for_each_entry_idx(pos, table, idx) \ |
| 748 | for (pos = table, idx = 0; pos->frequency != CPUFREQ_TABLE_END; \ |
| 749 | pos++, idx++) |
| 750 | |
| 751 | /* |
| 752 | * cpufreq_for_each_valid_entry - iterate over a cpufreq_frequency_table |
| 753 | * excluding CPUFREQ_ENTRY_INVALID frequencies. |
| 754 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. |
| 755 | * @table: the cpufreq_frequency_table * to iterate over. |
| 756 | */ |
| 757 | |
| 758 | #define cpufreq_for_each_valid_entry(pos, table) \ |
| 759 | for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) \ |
| 760 | if (pos->frequency == CPUFREQ_ENTRY_INVALID) \ |
| 761 | continue; \ |
| 762 | else |
| 763 | |
| 764 | /* |
| 765 | * cpufreq_for_each_valid_entry_idx - iterate with index over a cpufreq |
| 766 | * frequency_table excluding CPUFREQ_ENTRY_INVALID frequencies. |
| 767 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. |
| 768 | * @table: the cpufreq_frequency_table * to iterate over. |
| 769 | * @idx: the table entry currently being processed |
| 770 | */ |
| 771 | |
| 772 | #define cpufreq_for_each_valid_entry_idx(pos, table, idx) \ |
| 773 | cpufreq_for_each_entry_idx(pos, table, idx) \ |
| 774 | if (pos->frequency == CPUFREQ_ENTRY_INVALID) \ |
| 775 | continue; \ |
| 776 | else |
| 777 | |
| 778 | /** |
| 779 | * cpufreq_for_each_efficient_entry_idx - iterate with index over a cpufreq |
| 780 | * frequency_table excluding CPUFREQ_ENTRY_INVALID and |
| 781 | * CPUFREQ_INEFFICIENT_FREQ frequencies. |
| 782 | * @pos: the &struct cpufreq_frequency_table to use as a loop cursor. |
| 783 | * @table: the &struct cpufreq_frequency_table to iterate over. |
| 784 | * @idx: the table entry currently being processed. |
| 785 | * @efficiencies: set to true to only iterate over efficient frequencies. |
| 786 | */ |
| 787 | |
| 788 | #define cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) \ |
| 789 | cpufreq_for_each_valid_entry_idx(pos, table, idx) \ |
| 790 | if (efficiencies && (pos->flags & CPUFREQ_INEFFICIENT_FREQ)) \ |
| 791 | continue; \ |
| 792 | else |
| 793 | |
| 794 | |
| 795 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy); |
| 796 | |
| 797 | int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy); |
| 798 | |
| 799 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy); |
| 800 | |
| 801 | int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, |
| 802 | unsigned int target_freq, unsigned int min, |
| 803 | unsigned int max, unsigned int relation); |
| 804 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
| 805 | unsigned int freq); |
| 806 | |
| 807 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf); |
| 808 | |
| 809 | #ifdef CONFIG_CPU_FREQ |
| 810 | bool cpufreq_boost_enabled(void); |
| 811 | int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state); |
| 812 | |
| 813 | /* Find lowest freq at or above target in a table in ascending order */ |
| 814 | static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy, |
| 815 | unsigned int target_freq, |
| 816 | bool efficiencies) |
| 817 | { |
| 818 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 819 | struct cpufreq_frequency_table *pos; |
| 820 | unsigned int freq; |
| 821 | int idx, best = -1; |
| 822 | |
| 823 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 824 | freq = pos->frequency; |
| 825 | |
| 826 | if (freq >= target_freq) |
| 827 | return idx; |
| 828 | |
| 829 | best = idx; |
| 830 | } |
| 831 | |
| 832 | return best; |
| 833 | } |
| 834 | |
| 835 | /* Find lowest freq at or above target in a table in descending order */ |
| 836 | static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy, |
| 837 | unsigned int target_freq, |
| 838 | bool efficiencies) |
| 839 | { |
| 840 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 841 | struct cpufreq_frequency_table *pos; |
| 842 | unsigned int freq; |
| 843 | int idx, best = -1; |
| 844 | |
| 845 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 846 | freq = pos->frequency; |
| 847 | |
| 848 | if (freq == target_freq) |
| 849 | return idx; |
| 850 | |
| 851 | if (freq > target_freq) { |
| 852 | best = idx; |
| 853 | continue; |
| 854 | } |
| 855 | |
| 856 | /* No freq found above target_freq */ |
| 857 | if (best == -1) |
| 858 | return idx; |
| 859 | |
| 860 | return best; |
| 861 | } |
| 862 | |
| 863 | return best; |
| 864 | } |
| 865 | |
| 866 | static inline int find_index_l(struct cpufreq_policy *policy, |
| 867 | unsigned int target_freq, |
| 868 | unsigned int min, unsigned int max, |
| 869 | bool efficiencies) |
| 870 | { |
| 871 | target_freq = clamp_val(target_freq, min, max); |
| 872 | |
| 873 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 874 | return cpufreq_table_find_index_al(policy, target_freq, |
| 875 | efficiencies); |
| 876 | else |
| 877 | return cpufreq_table_find_index_dl(policy, target_freq, |
| 878 | efficiencies); |
| 879 | } |
| 880 | |
| 881 | /* Works only on sorted freq-tables */ |
| 882 | static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy, |
| 883 | unsigned int target_freq, |
| 884 | bool efficiencies) |
| 885 | { |
| 886 | return find_index_l(policy, target_freq, policy->min, policy->max, efficiencies); |
| 887 | } |
| 888 | |
| 889 | /* Find highest freq at or below target in a table in ascending order */ |
| 890 | static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy, |
| 891 | unsigned int target_freq, |
| 892 | bool efficiencies) |
| 893 | { |
| 894 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 895 | struct cpufreq_frequency_table *pos; |
| 896 | unsigned int freq; |
| 897 | int idx, best = -1; |
| 898 | |
| 899 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 900 | freq = pos->frequency; |
| 901 | |
| 902 | if (freq == target_freq) |
| 903 | return idx; |
| 904 | |
| 905 | if (freq < target_freq) { |
| 906 | best = idx; |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | /* No freq found below target_freq */ |
| 911 | if (best == -1) |
| 912 | return idx; |
| 913 | |
| 914 | return best; |
| 915 | } |
| 916 | |
| 917 | return best; |
| 918 | } |
| 919 | |
| 920 | /* Find highest freq at or below target in a table in descending order */ |
| 921 | static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy, |
| 922 | unsigned int target_freq, |
| 923 | bool efficiencies) |
| 924 | { |
| 925 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 926 | struct cpufreq_frequency_table *pos; |
| 927 | unsigned int freq; |
| 928 | int idx, best = -1; |
| 929 | |
| 930 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 931 | freq = pos->frequency; |
| 932 | |
| 933 | if (freq <= target_freq) |
| 934 | return idx; |
| 935 | |
| 936 | best = idx; |
| 937 | } |
| 938 | |
| 939 | return best; |
| 940 | } |
| 941 | |
| 942 | static inline int find_index_h(struct cpufreq_policy *policy, |
| 943 | unsigned int target_freq, |
| 944 | unsigned int min, unsigned int max, |
| 945 | bool efficiencies) |
| 946 | { |
| 947 | target_freq = clamp_val(target_freq, min, max); |
| 948 | |
| 949 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 950 | return cpufreq_table_find_index_ah(policy, target_freq, |
| 951 | efficiencies); |
| 952 | else |
| 953 | return cpufreq_table_find_index_dh(policy, target_freq, |
| 954 | efficiencies); |
| 955 | } |
| 956 | |
| 957 | /* Works only on sorted freq-tables */ |
| 958 | static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy, |
| 959 | unsigned int target_freq, |
| 960 | bool efficiencies) |
| 961 | { |
| 962 | return find_index_h(policy, target_freq, policy->min, policy->max, efficiencies); |
| 963 | } |
| 964 | |
| 965 | /* Find closest freq to target in a table in ascending order */ |
| 966 | static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy, |
| 967 | unsigned int target_freq, |
| 968 | bool efficiencies) |
| 969 | { |
| 970 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 971 | struct cpufreq_frequency_table *pos; |
| 972 | unsigned int freq; |
| 973 | int idx, best = -1; |
| 974 | |
| 975 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 976 | freq = pos->frequency; |
| 977 | |
| 978 | if (freq == target_freq) |
| 979 | return idx; |
| 980 | |
| 981 | if (freq < target_freq) { |
| 982 | best = idx; |
| 983 | continue; |
| 984 | } |
| 985 | |
| 986 | /* No freq found below target_freq */ |
| 987 | if (best == -1) |
| 988 | return idx; |
| 989 | |
| 990 | /* Choose the closest freq */ |
| 991 | if (target_freq - table[best].frequency > freq - target_freq) |
| 992 | return idx; |
| 993 | |
| 994 | return best; |
| 995 | } |
| 996 | |
| 997 | return best; |
| 998 | } |
| 999 | |
| 1000 | /* Find closest freq to target in a table in descending order */ |
| 1001 | static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy, |
| 1002 | unsigned int target_freq, |
| 1003 | bool efficiencies) |
| 1004 | { |
| 1005 | struct cpufreq_frequency_table *table = policy->freq_table; |
| 1006 | struct cpufreq_frequency_table *pos; |
| 1007 | unsigned int freq; |
| 1008 | int idx, best = -1; |
| 1009 | |
| 1010 | cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) { |
| 1011 | freq = pos->frequency; |
| 1012 | |
| 1013 | if (freq == target_freq) |
| 1014 | return idx; |
| 1015 | |
| 1016 | if (freq > target_freq) { |
| 1017 | best = idx; |
| 1018 | continue; |
| 1019 | } |
| 1020 | |
| 1021 | /* No freq found above target_freq */ |
| 1022 | if (best == -1) |
| 1023 | return idx; |
| 1024 | |
| 1025 | /* Choose the closest freq */ |
| 1026 | if (table[best].frequency - target_freq > target_freq - freq) |
| 1027 | return idx; |
| 1028 | |
| 1029 | return best; |
| 1030 | } |
| 1031 | |
| 1032 | return best; |
| 1033 | } |
| 1034 | |
| 1035 | static inline int find_index_c(struct cpufreq_policy *policy, |
| 1036 | unsigned int target_freq, |
| 1037 | unsigned int min, unsigned int max, |
| 1038 | bool efficiencies) |
| 1039 | { |
| 1040 | target_freq = clamp_val(target_freq, min, max); |
| 1041 | |
| 1042 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 1043 | return cpufreq_table_find_index_ac(policy, target_freq, |
| 1044 | efficiencies); |
| 1045 | else |
| 1046 | return cpufreq_table_find_index_dc(policy, target_freq, |
| 1047 | efficiencies); |
| 1048 | } |
| 1049 | |
| 1050 | /* Works only on sorted freq-tables */ |
| 1051 | static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy, |
| 1052 | unsigned int target_freq, |
| 1053 | bool efficiencies) |
| 1054 | { |
| 1055 | return find_index_c(policy, target_freq, policy->min, policy->max, efficiencies); |
| 1056 | } |
| 1057 | |
| 1058 | static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, |
| 1059 | unsigned int min, unsigned int max, |
| 1060 | int idx) |
| 1061 | { |
| 1062 | unsigned int freq; |
| 1063 | |
| 1064 | if (idx < 0) |
| 1065 | return false; |
| 1066 | |
| 1067 | freq = policy->freq_table[idx].frequency; |
| 1068 | |
| 1069 | return freq == clamp_val(freq, min, max); |
| 1070 | } |
| 1071 | |
| 1072 | static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy, |
| 1073 | unsigned int target_freq, |
| 1074 | unsigned int min, |
| 1075 | unsigned int max, |
| 1076 | unsigned int relation) |
| 1077 | { |
| 1078 | bool efficiencies = policy->efficiencies_available && |
| 1079 | (relation & CPUFREQ_RELATION_E); |
| 1080 | int idx; |
| 1081 | |
| 1082 | /* cpufreq_table_index_unsorted() has no use for this flag anyway */ |
| 1083 | relation &= ~CPUFREQ_RELATION_E; |
| 1084 | |
| 1085 | if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED)) |
| 1086 | return cpufreq_table_index_unsorted(policy, target_freq, min, |
| 1087 | max, relation); |
| 1088 | retry: |
| 1089 | switch (relation) { |
| 1090 | case CPUFREQ_RELATION_L: |
| 1091 | idx = find_index_l(policy, target_freq, min, max, efficiencies); |
| 1092 | break; |
| 1093 | case CPUFREQ_RELATION_H: |
| 1094 | idx = find_index_h(policy, target_freq, min, max, efficiencies); |
| 1095 | break; |
| 1096 | case CPUFREQ_RELATION_C: |
| 1097 | idx = find_index_c(policy, target_freq, min, max, efficiencies); |
| 1098 | break; |
| 1099 | default: |
| 1100 | WARN_ON_ONCE(1); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* Limit frequency index to honor min and max */ |
| 1105 | if (!cpufreq_is_in_limits(policy, min, max, idx) && efficiencies) { |
| 1106 | efficiencies = false; |
| 1107 | goto retry; |
| 1108 | } |
| 1109 | |
| 1110 | return idx; |
| 1111 | } |
| 1112 | |
| 1113 | static inline int cpufreq_table_count_valid_entries(const struct cpufreq_policy *policy) |
| 1114 | { |
| 1115 | struct cpufreq_frequency_table *pos; |
| 1116 | int count = 0; |
| 1117 | |
| 1118 | if (unlikely(!policy->freq_table)) |
| 1119 | return 0; |
| 1120 | |
| 1121 | cpufreq_for_each_valid_entry(pos, policy->freq_table) |
| 1122 | count++; |
| 1123 | |
| 1124 | return count; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * cpufreq_table_set_inefficient() - Mark a frequency as inefficient |
| 1129 | * @policy: the &struct cpufreq_policy containing the inefficient frequency |
| 1130 | * @frequency: the inefficient frequency |
| 1131 | * |
| 1132 | * The &struct cpufreq_policy must use a sorted frequency table |
| 1133 | * |
| 1134 | * Return: %0 on success or a negative errno code |
| 1135 | */ |
| 1136 | |
| 1137 | static inline int |
| 1138 | cpufreq_table_set_inefficient(struct cpufreq_policy *policy, |
| 1139 | unsigned int frequency) |
| 1140 | { |
| 1141 | struct cpufreq_frequency_table *pos; |
| 1142 | |
| 1143 | /* Not supported */ |
| 1144 | if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) |
| 1145 | return -EINVAL; |
| 1146 | |
| 1147 | cpufreq_for_each_valid_entry(pos, policy->freq_table) { |
| 1148 | if (pos->frequency == frequency) { |
| 1149 | pos->flags |= CPUFREQ_INEFFICIENT_FREQ; |
| 1150 | policy->efficiencies_available = true; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | |
| 1158 | static inline int parse_perf_domain(int cpu, const char *list_name, |
| 1159 | const char *cell_name, |
| 1160 | struct of_phandle_args *args) |
| 1161 | { |
| 1162 | int ret; |
| 1163 | |
| 1164 | struct device_node *cpu_np __free(device_node) = of_cpu_device_node_get(cpu); |
| 1165 | if (!cpu_np) |
| 1166 | return -ENODEV; |
| 1167 | |
| 1168 | ret = of_parse_phandle_with_args(cpu_np, list_name, cell_name, 0, |
| 1169 | args); |
| 1170 | if (ret < 0) |
| 1171 | return ret; |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_name, |
| 1176 | const char *cell_name, struct cpumask *cpumask, |
| 1177 | struct of_phandle_args *pargs) |
| 1178 | { |
| 1179 | int cpu, ret; |
| 1180 | struct of_phandle_args args; |
| 1181 | |
| 1182 | ret = parse_perf_domain(pcpu, list_name, cell_name, pargs); |
| 1183 | if (ret < 0) |
| 1184 | return ret; |
| 1185 | |
| 1186 | cpumask_set_cpu(pcpu, cpumask); |
| 1187 | |
| 1188 | for_each_possible_cpu(cpu) { |
| 1189 | if (cpu == pcpu) |
| 1190 | continue; |
| 1191 | |
| 1192 | ret = parse_perf_domain(cpu, list_name, cell_name, &args); |
| 1193 | if (ret < 0) |
| 1194 | continue; |
| 1195 | |
| 1196 | if (of_phandle_args_equal(pargs, &args)) |
| 1197 | cpumask_set_cpu(cpu, cpumask); |
| 1198 | |
| 1199 | of_node_put(args.np); |
| 1200 | } |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | #else |
| 1205 | static inline bool cpufreq_boost_enabled(void) |
| 1206 | { |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | static inline int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) |
| 1211 | { |
| 1212 | return -EOPNOTSUPP; |
| 1213 | } |
| 1214 | |
| 1215 | static inline int |
| 1216 | cpufreq_table_set_inefficient(struct cpufreq_policy *policy, |
| 1217 | unsigned int frequency) |
| 1218 | { |
| 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
| 1222 | static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_name, |
| 1223 | const char *cell_name, struct cpumask *cpumask, |
| 1224 | struct of_phandle_args *pargs) |
| 1225 | { |
| 1226 | return -EOPNOTSUPP; |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
| 1230 | extern int arch_freq_get_on_cpu(int cpu); |
| 1231 | |
| 1232 | #ifndef arch_set_freq_scale |
| 1233 | static __always_inline |
| 1234 | void arch_set_freq_scale(const struct cpumask *cpus, |
| 1235 | unsigned long cur_freq, |
| 1236 | unsigned long max_freq) |
| 1237 | { |
| 1238 | } |
| 1239 | #endif |
| 1240 | |
| 1241 | /* the following are really really optional */ |
| 1242 | extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs; |
| 1243 | extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs; |
| 1244 | int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy); |
| 1245 | |
| 1246 | unsigned int cpufreq_generic_get(unsigned int cpu); |
| 1247 | void cpufreq_generic_init(struct cpufreq_policy *policy, |
| 1248 | struct cpufreq_frequency_table *table, |
| 1249 | unsigned int transition_latency); |
| 1250 | |
| 1251 | bool cpufreq_ready_for_eas(const struct cpumask *cpu_mask); |
| 1252 | |
| 1253 | static inline void cpufreq_register_em_with_opp(struct cpufreq_policy *policy) |
| 1254 | { |
| 1255 | dev_pm_opp_of_register_em(get_cpu_device(policy->cpu), |
| 1256 | policy->related_cpus); |
| 1257 | } |
| 1258 | #endif /* _LINUX_CPUFREQ_H */ |