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