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