Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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