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