]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/cpufreq.h
cpufreq: intel_pstate: Reuse refresh_frequency_limits()
[linux.git] / include / linux / cpufreq.h
1 /*
2  * linux/include/linux/cpufreq.h
3  *
4  * Copyright (C) 2001 Russell King
5  *           (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #ifndef _LINUX_CPUFREQ_H
12 #define _LINUX_CPUFREQ_H
13
14 #include <linux/clk.h>
15 #include <linux/cpumask.h>
16 #include <linux/completion.h>
17 #include <linux/kobject.h>
18 #include <linux/notifier.h>
19 #include <linux/spinlock.h>
20 #include <linux/sysfs.h>
21
22 /*********************************************************************
23  *                        CPUFREQ INTERFACE                          *
24  *********************************************************************/
25 /*
26  * Frequency values here are CPU kHz
27  *
28  * Maximum transition latency is in nanoseconds - if it's unknown,
29  * CPUFREQ_ETERNAL shall be used.
30  */
31
32 #define CPUFREQ_ETERNAL                 (-1)
33 #define CPUFREQ_NAME_LEN                16
34 /* Print length for names. Extra 1 space for accommodating '\n' in prints */
35 #define CPUFREQ_NAME_PLEN               (CPUFREQ_NAME_LEN + 1)
36
37 struct cpufreq_governor;
38
39 enum cpufreq_table_sorting {
40         CPUFREQ_TABLE_UNSORTED,
41         CPUFREQ_TABLE_SORTED_ASCENDING,
42         CPUFREQ_TABLE_SORTED_DESCENDING
43 };
44
45 struct cpufreq_cpuinfo {
46         unsigned int            max_freq;
47         unsigned int            min_freq;
48
49         /* in 10^(-9) s = nanoseconds */
50         unsigned int            transition_latency;
51 };
52
53 struct cpufreq_user_policy {
54         unsigned int            min;    /* in kHz */
55         unsigned int            max;    /* in kHz */
56 };
57
58 struct cpufreq_policy {
59         /* CPUs sharing clock, require sw coordination */
60         cpumask_var_t           cpus;   /* Online CPUs only */
61         cpumask_var_t           related_cpus; /* Online + Offline CPUs */
62         cpumask_var_t           real_cpus; /* Related and present */
63
64         unsigned int            shared_type; /* ACPI: ANY or ALL affected CPUs
65                                                 should set cpufreq */
66         unsigned int            cpu;    /* cpu managing this policy, must be online */
67
68         struct clk              *clk;
69         struct cpufreq_cpuinfo  cpuinfo;/* see above */
70
71         unsigned int            min;    /* in kHz */
72         unsigned int            max;    /* in kHz */
73         unsigned int            cur;    /* in kHz, only needed if cpufreq
74                                          * governors are used */
75         unsigned int            restore_freq; /* = policy->cur before transition */
76         unsigned int            suspend_freq; /* freq to set during suspend */
77
78         unsigned int            policy; /* see above */
79         unsigned int            last_policy; /* policy before unplug */
80         struct cpufreq_governor *governor; /* see below */
81         void                    *governor_data;
82         char                    last_governor[CPUFREQ_NAME_LEN]; /* last governor used */
83
84         struct work_struct      update; /* if update_policy() needs to be
85                                          * called, but you're in IRQ context */
86
87         struct cpufreq_user_policy user_policy;
88         struct cpufreq_frequency_table  *freq_table;
89         enum cpufreq_table_sorting freq_table_sorted;
90
91         struct list_head        policy_list;
92         struct kobject          kobj;
93         struct completion       kobj_unregister;
94
95         /*
96          * The rules for this semaphore:
97          * - Any routine that wants to read from the policy structure will
98          *   do a down_read on this semaphore.
99          * - Any routine that will write to the policy structure and/or may take away
100          *   the policy altogether (eg. CPU hotplug), will hold this lock in write
101          *   mode before doing so.
102          */
103         struct rw_semaphore     rwsem;
104
105         /*
106          * Fast switch flags:
107          * - fast_switch_possible should be set by the driver if it can
108          *   guarantee that frequency can be changed on any CPU sharing the
109          *   policy and that the change will affect all of the policy CPUs then.
110          * - fast_switch_enabled is to be set by governors that support fast
111          *   frequency switching with the help of cpufreq_enable_fast_switch().
112          */
113         bool                    fast_switch_possible;
114         bool                    fast_switch_enabled;
115
116         /*
117          * Preferred average time interval between consecutive invocations of
118          * the driver to set the frequency for this policy.  To be set by the
119          * scaling driver (0, which is the default, means no preference).
120          */
121         unsigned int            transition_delay_us;
122
123         /*
124          * Remote DVFS flag (Not added to the driver structure as we don't want
125          * to access another structure from scheduler hotpath).
126          *
127          * Should be set if CPUs can do DVFS on behalf of other CPUs from
128          * different cpufreq policies.
129          */
130         bool                    dvfs_possible_from_any_cpu;
131
132          /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
133         unsigned int cached_target_freq;
134         int cached_resolved_idx;
135
136         /* Synchronization for frequency transitions */
137         bool                    transition_ongoing; /* Tracks transition status */
138         spinlock_t              transition_lock;
139         wait_queue_head_t       transition_wait;
140         struct task_struct      *transition_task; /* Task which is doing the transition */
141
142         /* cpufreq-stats */
143         struct cpufreq_stats    *stats;
144
145         /* For cpufreq driver's internal use */
146         void                    *driver_data;
147
148         /* Pointer to the cooling device if used for thermal mitigation */
149         struct thermal_cooling_device *cdev;
150
151         struct notifier_block nb_min;
152         struct notifier_block nb_max;
153 };
154
155 struct cpufreq_freqs {
156         struct cpufreq_policy *policy;
157         unsigned int old;
158         unsigned int new;
159         u8 flags;               /* flags of cpufreq_driver, see below. */
160 };
161
162 /* Only for ACPI */
163 #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
164 #define CPUFREQ_SHARED_TYPE_HW   (1) /* HW does needed coordination */
165 #define CPUFREQ_SHARED_TYPE_ALL  (2) /* All dependent CPUs should set freq */
166 #define CPUFREQ_SHARED_TYPE_ANY  (3) /* Freq can be set from any dependent CPU*/
167
168 #ifdef CONFIG_CPU_FREQ
169 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
170 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
171 void cpufreq_cpu_put(struct cpufreq_policy *policy);
172 #else
173 static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
174 {
175         return NULL;
176 }
177 static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
178 {
179         return NULL;
180 }
181 static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
182 #endif
183
184 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
185 {
186         return cpumask_empty(policy->cpus);
187 }
188
189 static inline bool policy_is_shared(struct cpufreq_policy *policy)
190 {
191         return cpumask_weight(policy->cpus) > 1;
192 }
193
194 /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
195 extern struct kobject *cpufreq_global_kobject;
196
197 #ifdef CONFIG_CPU_FREQ
198 unsigned int cpufreq_get(unsigned int cpu);
199 unsigned int cpufreq_quick_get(unsigned int cpu);
200 unsigned int cpufreq_quick_get_max(unsigned int cpu);
201 void disable_cpufreq(void);
202
203 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
204
205 struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu);
206 void cpufreq_cpu_release(struct cpufreq_policy *policy);
207 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
208 int cpufreq_set_policy(struct cpufreq_policy *policy,
209                        struct cpufreq_policy *new_policy);
210 void refresh_frequency_limits(struct cpufreq_policy *policy);
211 void cpufreq_update_policy(unsigned int cpu);
212 void cpufreq_update_limits(unsigned int cpu);
213 bool have_governor_per_policy(void);
214 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
215 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy);
216 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy);
217 #else
218 static inline unsigned int cpufreq_get(unsigned int cpu)
219 {
220         return 0;
221 }
222 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
223 {
224         return 0;
225 }
226 static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
227 {
228         return 0;
229 }
230 static inline void disable_cpufreq(void) { }
231 #endif
232
233 #ifdef CONFIG_CPU_FREQ_STAT
234 void cpufreq_stats_create_table(struct cpufreq_policy *policy);
235 void cpufreq_stats_free_table(struct cpufreq_policy *policy);
236 void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
237                                      unsigned int new_freq);
238 #else
239 static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { }
240 static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { }
241 static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
242                                                    unsigned int new_freq) { }
243 #endif /* CONFIG_CPU_FREQ_STAT */
244
245 /*********************************************************************
246  *                      CPUFREQ DRIVER INTERFACE                     *
247  *********************************************************************/
248
249 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
250 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
251 #define CPUFREQ_RELATION_C 2  /* closest frequency to target */
252
253 struct freq_attr {
254         struct attribute attr;
255         ssize_t (*show)(struct cpufreq_policy *, char *);
256         ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
257 };
258
259 #define cpufreq_freq_attr_ro(_name)             \
260 static struct freq_attr _name =                 \
261 __ATTR(_name, 0444, show_##_name, NULL)
262
263 #define cpufreq_freq_attr_ro_perm(_name, _perm) \
264 static struct freq_attr _name =                 \
265 __ATTR(_name, _perm, show_##_name, NULL)
266
267 #define cpufreq_freq_attr_rw(_name)             \
268 static struct freq_attr _name =                 \
269 __ATTR(_name, 0644, show_##_name, store_##_name)
270
271 #define cpufreq_freq_attr_wo(_name)             \
272 static struct freq_attr _name =                 \
273 __ATTR(_name, 0200, NULL, store_##_name)
274
275 #define define_one_global_ro(_name)             \
276 static struct kobj_attribute _name =            \
277 __ATTR(_name, 0444, show_##_name, NULL)
278
279 #define define_one_global_rw(_name)             \
280 static struct kobj_attribute _name =            \
281 __ATTR(_name, 0644, show_##_name, store_##_name)
282
283
284 struct cpufreq_driver {
285         char            name[CPUFREQ_NAME_LEN];
286         u8              flags;
287         void            *driver_data;
288
289         /* needed by all drivers */
290         int             (*init)(struct cpufreq_policy *policy);
291         int             (*verify)(struct cpufreq_policy *policy);
292
293         /* define one out of two */
294         int             (*setpolicy)(struct cpufreq_policy *policy);
295
296         /*
297          * On failure, should always restore frequency to policy->restore_freq
298          * (i.e. old freq).
299          */
300         int             (*target)(struct cpufreq_policy *policy,
301                                   unsigned int target_freq,
302                                   unsigned int relation);       /* Deprecated */
303         int             (*target_index)(struct cpufreq_policy *policy,
304                                         unsigned int index);
305         unsigned int    (*fast_switch)(struct cpufreq_policy *policy,
306                                        unsigned int target_freq);
307
308         /*
309          * Caches and returns the lowest driver-supported frequency greater than
310          * or equal to the target frequency, subject to any driver limitations.
311          * Does not set the frequency. Only to be implemented for drivers with
312          * target().
313          */
314         unsigned int    (*resolve_freq)(struct cpufreq_policy *policy,
315                                         unsigned int target_freq);
316
317         /*
318          * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION
319          * unset.
320          *
321          * get_intermediate should return a stable intermediate frequency
322          * platform wants to switch to and target_intermediate() should set CPU
323          * to to that frequency, before jumping to the frequency corresponding
324          * to 'index'. Core will take care of sending notifications and driver
325          * doesn't have to handle them in target_intermediate() or
326          * target_index().
327          *
328          * Drivers can return '0' from get_intermediate() in case they don't
329          * wish to switch to intermediate frequency for some target frequency.
330          * In that case core will directly call ->target_index().
331          */
332         unsigned int    (*get_intermediate)(struct cpufreq_policy *policy,
333                                             unsigned int index);
334         int             (*target_intermediate)(struct cpufreq_policy *policy,
335                                                unsigned int index);
336
337         /* should be defined, if possible */
338         unsigned int    (*get)(unsigned int cpu);
339
340         /* Called to update policy limits on firmware notifications. */
341         void            (*update_limits)(unsigned int cpu);
342
343         /* optional */
344         int             (*bios_limit)(int cpu, unsigned int *limit);
345
346         int             (*online)(struct cpufreq_policy *policy);
347         int             (*offline)(struct cpufreq_policy *policy);
348         int             (*exit)(struct cpufreq_policy *policy);
349         void            (*stop_cpu)(struct cpufreq_policy *policy);
350         int             (*suspend)(struct cpufreq_policy *policy);
351         int             (*resume)(struct cpufreq_policy *policy);
352
353         /* Will be called after the driver is fully initialized */
354         void            (*ready)(struct cpufreq_policy *policy);
355
356         struct freq_attr **attr;
357
358         /* platform specific boost support code */
359         bool            boost_enabled;
360         int             (*set_boost)(int state);
361 };
362
363 /* flags */
364
365 /* driver isn't removed even if all ->init() calls failed */
366 #define CPUFREQ_STICKY                          BIT(0)
367
368 /* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */
369 #define CPUFREQ_CONST_LOOPS                     BIT(1)
370
371 /* don't warn on suspend/resume speed mismatches */
372 #define CPUFREQ_PM_NO_WARN                      BIT(2)
373
374 /*
375  * This should be set by platforms having multiple clock-domains, i.e.
376  * supporting multiple policies. With this sysfs directories of governor would
377  * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
378  * governor with different tunables for different clusters.
379  */
380 #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY        BIT(3)
381
382 /*
383  * Driver will do POSTCHANGE notifications from outside of their ->target()
384  * routine and so must set cpufreq_driver->flags with this flag, so that core
385  * can handle them specially.
386  */
387 #define CPUFREQ_ASYNC_NOTIFICATION              BIT(4)
388
389 /*
390  * Set by drivers which want cpufreq core to check if CPU is running at a
391  * frequency present in freq-table exposed by the driver. For these drivers if
392  * CPU is found running at an out of table freq, we will try to set it to a freq
393  * from the table. And if that fails, we will stop further boot process by
394  * issuing a BUG_ON().
395  */
396 #define CPUFREQ_NEED_INITIAL_FREQ_CHECK BIT(5)
397
398 /*
399  * Set by drivers to disallow use of governors with "dynamic_switching" flag
400  * set.
401  */
402 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING       BIT(6)
403
404 /*
405  * Set by drivers that want the core to automatically register the cpufreq
406  * driver as a thermal cooling device.
407  */
408 #define CPUFREQ_IS_COOLING_DEV                  BIT(7)
409
410 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
411 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
412
413 const char *cpufreq_get_current_driver(void);
414 void *cpufreq_get_driver_data(void);
415
416 static inline int cpufreq_thermal_control_enabled(struct cpufreq_driver *drv)
417 {
418         return IS_ENABLED(CONFIG_CPU_THERMAL) &&
419                 (drv->flags & CPUFREQ_IS_COOLING_DEV);
420 }
421
422 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy,
423                 unsigned int min, unsigned int max)
424 {
425         if (policy->min < min)
426                 policy->min = min;
427         if (policy->max < min)
428                 policy->max = min;
429         if (policy->min > max)
430                 policy->min = max;
431         if (policy->max > max)
432                 policy->max = max;
433         if (policy->min > policy->max)
434                 policy->min = policy->max;
435         return;
436 }
437
438 static inline void
439 cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
440 {
441         cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
442                         policy->cpuinfo.max_freq);
443 }
444
445 #ifdef CONFIG_CPU_FREQ
446 void cpufreq_suspend(void);
447 void cpufreq_resume(void);
448 int cpufreq_generic_suspend(struct cpufreq_policy *policy);
449 #else
450 static inline void cpufreq_suspend(void) {}
451 static inline void cpufreq_resume(void) {}
452 #endif
453
454 /*********************************************************************
455  *                     CPUFREQ NOTIFIER INTERFACE                    *
456  *********************************************************************/
457
458 #define CPUFREQ_TRANSITION_NOTIFIER     (0)
459 #define CPUFREQ_POLICY_NOTIFIER         (1)
460
461 /* Transition notifiers */
462 #define CPUFREQ_PRECHANGE               (0)
463 #define CPUFREQ_POSTCHANGE              (1)
464
465 /* Policy Notifiers  */
466 #define CPUFREQ_ADJUST                  (0)
467 #define CPUFREQ_NOTIFY                  (1)
468
469 #ifdef CONFIG_CPU_FREQ
470 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
471 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
472
473 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
474                 struct cpufreq_freqs *freqs);
475 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
476                 struct cpufreq_freqs *freqs, int transition_failed);
477
478 #else /* CONFIG_CPU_FREQ */
479 static inline int cpufreq_register_notifier(struct notifier_block *nb,
480                                                 unsigned int list)
481 {
482         return 0;
483 }
484 static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
485                                                 unsigned int list)
486 {
487         return 0;
488 }
489 #endif /* !CONFIG_CPU_FREQ */
490
491 /**
492  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
493  * safe)
494  * @old:   old value
495  * @div:   divisor
496  * @mult:  multiplier
497  *
498  *
499  * new = old * mult / div
500  */
501 static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
502                 u_int mult)
503 {
504 #if BITS_PER_LONG == 32
505         u64 result = ((u64) old) * ((u64) mult);
506         do_div(result, div);
507         return (unsigned long) result;
508
509 #elif BITS_PER_LONG == 64
510         unsigned long result = old * ((u64) mult);
511         result /= div;
512         return result;
513 #endif
514 }
515
516 /*********************************************************************
517  *                          CPUFREQ GOVERNORS                        *
518  *********************************************************************/
519
520 /*
521  * If (cpufreq_driver->target) exists, the ->governor decides what frequency
522  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
523  * two generic policies are available:
524  */
525 #define CPUFREQ_POLICY_POWERSAVE        (1)
526 #define CPUFREQ_POLICY_PERFORMANCE      (2)
527
528 /*
529  * The polling frequency depends on the capability of the processor. Default
530  * polling frequency is 1000 times the transition latency of the processor. The
531  * ondemand governor will work on any processor with transition latency <= 10ms,
532  * using appropriate sampling rate.
533  */
534 #define LATENCY_MULTIPLIER              (1000)
535
536 struct cpufreq_governor {
537         char    name[CPUFREQ_NAME_LEN];
538         int     (*init)(struct cpufreq_policy *policy);
539         void    (*exit)(struct cpufreq_policy *policy);
540         int     (*start)(struct cpufreq_policy *policy);
541         void    (*stop)(struct cpufreq_policy *policy);
542         void    (*limits)(struct cpufreq_policy *policy);
543         ssize_t (*show_setspeed)        (struct cpufreq_policy *policy,
544                                          char *buf);
545         int     (*store_setspeed)       (struct cpufreq_policy *policy,
546                                          unsigned int freq);
547         /* For governors which change frequency dynamically by themselves */
548         bool                    dynamic_switching;
549         struct list_head        governor_list;
550         struct module           *owner;
551 };
552
553 /* Pass a target to the cpufreq driver */
554 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
555                                         unsigned int target_freq);
556 int cpufreq_driver_target(struct cpufreq_policy *policy,
557                                  unsigned int target_freq,
558                                  unsigned int relation);
559 int __cpufreq_driver_target(struct cpufreq_policy *policy,
560                                    unsigned int target_freq,
561                                    unsigned int relation);
562 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
563                                          unsigned int target_freq);
564 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy);
565 int cpufreq_register_governor(struct cpufreq_governor *governor);
566 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
567
568 struct cpufreq_governor *cpufreq_default_governor(void);
569 struct cpufreq_governor *cpufreq_fallback_governor(void);
570
571 static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy)
572 {
573         if (policy->max < policy->cur)
574                 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
575         else if (policy->min > policy->cur)
576                 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
577 }
578
579 /* Governor attribute set */
580 struct gov_attr_set {
581         struct kobject kobj;
582         struct list_head policy_list;
583         struct mutex update_lock;
584         int usage_count;
585 };
586
587 /* sysfs ops for cpufreq governors */
588 extern const struct sysfs_ops governor_sysfs_ops;
589
590 void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node);
591 void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node);
592 unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node);
593
594 /* Governor sysfs attribute */
595 struct governor_attr {
596         struct attribute attr;
597         ssize_t (*show)(struct gov_attr_set *attr_set, char *buf);
598         ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf,
599                          size_t count);
600 };
601
602 static inline bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy)
603 {
604         /*
605          * Allow remote callbacks if:
606          * - dvfs_possible_from_any_cpu flag is set
607          * - the local and remote CPUs share cpufreq policy
608          */
609         return policy->dvfs_possible_from_any_cpu ||
610                 cpumask_test_cpu(smp_processor_id(), policy->cpus);
611 }
612
613 /*********************************************************************
614  *                     FREQUENCY TABLE HELPERS                       *
615  *********************************************************************/
616
617 /* Special Values of .frequency field */
618 #define CPUFREQ_ENTRY_INVALID   ~0u
619 #define CPUFREQ_TABLE_END       ~1u
620 /* Special Values of .flags field */
621 #define CPUFREQ_BOOST_FREQ      (1 << 0)
622
623 struct cpufreq_frequency_table {
624         unsigned int    flags;
625         unsigned int    driver_data; /* driver specific data, not used by core */
626         unsigned int    frequency; /* kHz - doesn't need to be in ascending
627                                     * order */
628 };
629
630 #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
631 int dev_pm_opp_init_cpufreq_table(struct device *dev,
632                                   struct cpufreq_frequency_table **table);
633 void dev_pm_opp_free_cpufreq_table(struct device *dev,
634                                    struct cpufreq_frequency_table **table);
635 #else
636 static inline int dev_pm_opp_init_cpufreq_table(struct device *dev,
637                                                 struct cpufreq_frequency_table
638                                                 **table)
639 {
640         return -EINVAL;
641 }
642
643 static inline void dev_pm_opp_free_cpufreq_table(struct device *dev,
644                                                  struct cpufreq_frequency_table
645                                                  **table)
646 {
647 }
648 #endif
649
650 /*
651  * cpufreq_for_each_entry -     iterate over a cpufreq_frequency_table
652  * @pos:        the cpufreq_frequency_table * to use as a loop cursor.
653  * @table:      the cpufreq_frequency_table * to iterate over.
654  */
655
656 #define cpufreq_for_each_entry(pos, table)      \
657         for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)
658
659 /*
660  * cpufreq_for_each_entry_idx - iterate over a cpufreq_frequency_table
661  *      with index
662  * @pos:        the cpufreq_frequency_table * to use as a loop cursor.
663  * @table:      the cpufreq_frequency_table * to iterate over.
664  * @idx:        the table entry currently being processed
665  */
666
667 #define cpufreq_for_each_entry_idx(pos, table, idx)     \
668         for (pos = table, idx = 0; pos->frequency != CPUFREQ_TABLE_END; \
669                 pos++, idx++)
670
671 /*
672  * cpufreq_for_each_valid_entry -     iterate over a cpufreq_frequency_table
673  *      excluding CPUFREQ_ENTRY_INVALID frequencies.
674  * @pos:        the cpufreq_frequency_table * to use as a loop cursor.
675  * @table:      the cpufreq_frequency_table * to iterate over.
676  */
677
678 #define cpufreq_for_each_valid_entry(pos, table)                        \
679         for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)   \
680                 if (pos->frequency == CPUFREQ_ENTRY_INVALID)            \
681                         continue;                                       \
682                 else
683
684 /*
685  * cpufreq_for_each_valid_entry_idx -     iterate with index over a cpufreq
686  *      frequency_table excluding CPUFREQ_ENTRY_INVALID frequencies.
687  * @pos:        the cpufreq_frequency_table * to use as a loop cursor.
688  * @table:      the cpufreq_frequency_table * to iterate over.
689  * @idx:        the table entry currently being processed
690  */
691
692 #define cpufreq_for_each_valid_entry_idx(pos, table, idx)               \
693         cpufreq_for_each_entry_idx(pos, table, idx)                     \
694                 if (pos->frequency == CPUFREQ_ENTRY_INVALID)            \
695                         continue;                                       \
696                 else
697
698
699 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
700                                     struct cpufreq_frequency_table *table);
701
702 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
703                                    struct cpufreq_frequency_table *table);
704 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
705
706 int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
707                                  unsigned int target_freq,
708                                  unsigned int relation);
709 int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
710                 unsigned int freq);
711
712 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
713
714 #ifdef CONFIG_CPU_FREQ
715 int cpufreq_boost_trigger_state(int state);
716 int cpufreq_boost_enabled(void);
717 int cpufreq_enable_boost_support(void);
718 bool policy_has_boost_freq(struct cpufreq_policy *policy);
719
720 /* Find lowest freq at or above target in a table in ascending order */
721 static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy,
722                                               unsigned int target_freq)
723 {
724         struct cpufreq_frequency_table *table = policy->freq_table;
725         struct cpufreq_frequency_table *pos;
726         unsigned int freq;
727         int idx, best = -1;
728
729         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
730                 freq = pos->frequency;
731
732                 if (freq >= target_freq)
733                         return idx;
734
735                 best = idx;
736         }
737
738         return best;
739 }
740
741 /* Find lowest freq at or above target in a table in descending order */
742 static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy,
743                                               unsigned int target_freq)
744 {
745         struct cpufreq_frequency_table *table = policy->freq_table;
746         struct cpufreq_frequency_table *pos;
747         unsigned int freq;
748         int idx, best = -1;
749
750         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
751                 freq = pos->frequency;
752
753                 if (freq == target_freq)
754                         return idx;
755
756                 if (freq > target_freq) {
757                         best = idx;
758                         continue;
759                 }
760
761                 /* No freq found above target_freq */
762                 if (best == -1)
763                         return idx;
764
765                 return best;
766         }
767
768         return best;
769 }
770
771 /* Works only on sorted freq-tables */
772 static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
773                                              unsigned int target_freq)
774 {
775         target_freq = clamp_val(target_freq, policy->min, policy->max);
776
777         if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
778                 return cpufreq_table_find_index_al(policy, target_freq);
779         else
780                 return cpufreq_table_find_index_dl(policy, target_freq);
781 }
782
783 /* Find highest freq at or below target in a table in ascending order */
784 static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy,
785                                               unsigned int target_freq)
786 {
787         struct cpufreq_frequency_table *table = policy->freq_table;
788         struct cpufreq_frequency_table *pos;
789         unsigned int freq;
790         int idx, best = -1;
791
792         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
793                 freq = pos->frequency;
794
795                 if (freq == target_freq)
796                         return idx;
797
798                 if (freq < target_freq) {
799                         best = idx;
800                         continue;
801                 }
802
803                 /* No freq found below target_freq */
804                 if (best == -1)
805                         return idx;
806
807                 return best;
808         }
809
810         return best;
811 }
812
813 /* Find highest freq at or below target in a table in descending order */
814 static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy,
815                                               unsigned int target_freq)
816 {
817         struct cpufreq_frequency_table *table = policy->freq_table;
818         struct cpufreq_frequency_table *pos;
819         unsigned int freq;
820         int idx, best = -1;
821
822         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
823                 freq = pos->frequency;
824
825                 if (freq <= target_freq)
826                         return idx;
827
828                 best = idx;
829         }
830
831         return best;
832 }
833
834 /* Works only on sorted freq-tables */
835 static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
836                                              unsigned int target_freq)
837 {
838         target_freq = clamp_val(target_freq, policy->min, policy->max);
839
840         if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
841                 return cpufreq_table_find_index_ah(policy, target_freq);
842         else
843                 return cpufreq_table_find_index_dh(policy, target_freq);
844 }
845
846 /* Find closest freq to target in a table in ascending order */
847 static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy,
848                                               unsigned int target_freq)
849 {
850         struct cpufreq_frequency_table *table = policy->freq_table;
851         struct cpufreq_frequency_table *pos;
852         unsigned int freq;
853         int idx, best = -1;
854
855         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
856                 freq = pos->frequency;
857
858                 if (freq == target_freq)
859                         return idx;
860
861                 if (freq < target_freq) {
862                         best = idx;
863                         continue;
864                 }
865
866                 /* No freq found below target_freq */
867                 if (best == -1)
868                         return idx;
869
870                 /* Choose the closest freq */
871                 if (target_freq - table[best].frequency > freq - target_freq)
872                         return idx;
873
874                 return best;
875         }
876
877         return best;
878 }
879
880 /* Find closest freq to target in a table in descending order */
881 static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy,
882                                               unsigned int target_freq)
883 {
884         struct cpufreq_frequency_table *table = policy->freq_table;
885         struct cpufreq_frequency_table *pos;
886         unsigned int freq;
887         int idx, best = -1;
888
889         cpufreq_for_each_valid_entry_idx(pos, table, idx) {
890                 freq = pos->frequency;
891
892                 if (freq == target_freq)
893                         return idx;
894
895                 if (freq > target_freq) {
896                         best = idx;
897                         continue;
898                 }
899
900                 /* No freq found above target_freq */
901                 if (best == -1)
902                         return idx;
903
904                 /* Choose the closest freq */
905                 if (table[best].frequency - target_freq > target_freq - freq)
906                         return idx;
907
908                 return best;
909         }
910
911         return best;
912 }
913
914 /* Works only on sorted freq-tables */
915 static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
916                                              unsigned int target_freq)
917 {
918         target_freq = clamp_val(target_freq, policy->min, policy->max);
919
920         if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
921                 return cpufreq_table_find_index_ac(policy, target_freq);
922         else
923                 return cpufreq_table_find_index_dc(policy, target_freq);
924 }
925
926 static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
927                                                  unsigned int target_freq,
928                                                  unsigned int relation)
929 {
930         if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED))
931                 return cpufreq_table_index_unsorted(policy, target_freq,
932                                                     relation);
933
934         switch (relation) {
935         case CPUFREQ_RELATION_L:
936                 return cpufreq_table_find_index_l(policy, target_freq);
937         case CPUFREQ_RELATION_H:
938                 return cpufreq_table_find_index_h(policy, target_freq);
939         case CPUFREQ_RELATION_C:
940                 return cpufreq_table_find_index_c(policy, target_freq);
941         default:
942                 pr_err("%s: Invalid relation: %d\n", __func__, relation);
943                 return -EINVAL;
944         }
945 }
946
947 static inline int cpufreq_table_count_valid_entries(const struct cpufreq_policy *policy)
948 {
949         struct cpufreq_frequency_table *pos;
950         int count = 0;
951
952         if (unlikely(!policy->freq_table))
953                 return 0;
954
955         cpufreq_for_each_valid_entry(pos, policy->freq_table)
956                 count++;
957
958         return count;
959 }
960 #else
961 static inline int cpufreq_boost_trigger_state(int state)
962 {
963         return 0;
964 }
965 static inline int cpufreq_boost_enabled(void)
966 {
967         return 0;
968 }
969
970 static inline int cpufreq_enable_boost_support(void)
971 {
972         return -EINVAL;
973 }
974
975 static inline bool policy_has_boost_freq(struct cpufreq_policy *policy)
976 {
977         return false;
978 }
979 #endif
980
981 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
982 void sched_cpufreq_governor_change(struct cpufreq_policy *policy,
983                         struct cpufreq_governor *old_gov);
984 #else
985 static inline void sched_cpufreq_governor_change(struct cpufreq_policy *policy,
986                         struct cpufreq_governor *old_gov) { }
987 #endif
988
989 extern void arch_freq_prepare_all(void);
990 extern unsigned int arch_freq_get_on_cpu(int cpu);
991
992 extern void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
993                                 unsigned long max_freq);
994
995 /* the following are really really optional */
996 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
997 extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs;
998 extern struct freq_attr *cpufreq_generic_attr[];
999 int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy);
1000
1001 unsigned int cpufreq_generic_get(unsigned int cpu);
1002 int cpufreq_generic_init(struct cpufreq_policy *policy,
1003                 struct cpufreq_frequency_table *table,
1004                 unsigned int transition_latency);
1005 #endif /* _LINUX_CPUFREQ_H */