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