Pull bugzilla-5737 into release branch
[firefly-linux-kernel-4.4.55.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  *
8  * $Id: cpufreq.h,v 1.36 2003/01/20 17:31:48 db Exp $
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 #ifndef _LINUX_CPUFREQ_H
15 #define _LINUX_CPUFREQ_H
16
17 #include <linux/mutex.h>
18 #include <linux/config.h>
19 #include <linux/notifier.h>
20 #include <linux/threads.h>
21 #include <linux/device.h>
22 #include <linux/kobject.h>
23 #include <linux/sysfs.h>
24 #include <linux/completion.h>
25 #include <linux/workqueue.h>
26 #include <linux/cpumask.h>
27 #include <asm/div64.h>
28
29 #define CPUFREQ_NAME_LEN 16
30
31
32 /*********************************************************************
33  *                     CPUFREQ NOTIFIER INTERFACE                    *
34  *********************************************************************/
35
36 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
37 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
38
39 #define CPUFREQ_TRANSITION_NOTIFIER     (0)
40 #define CPUFREQ_POLICY_NOTIFIER         (1)
41
42
43 /* if (cpufreq_driver->target) exists, the ->governor decides what frequency
44  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
45  * two generic policies are available:
46  */
47
48 #define CPUFREQ_POLICY_POWERSAVE        (1)
49 #define CPUFREQ_POLICY_PERFORMANCE      (2)
50
51 /* Frequency values here are CPU kHz so that hardware which doesn't run 
52  * with some frequencies can complain without having to guess what per 
53  * cent / per mille means. 
54  * Maximum transition latency is in nanoseconds - if it's unknown,
55  * CPUFREQ_ETERNAL shall be used.
56  */
57
58 struct cpufreq_governor;
59
60 #define CPUFREQ_ETERNAL                 (-1)
61 struct cpufreq_cpuinfo {
62         unsigned int            max_freq;
63         unsigned int            min_freq;
64         unsigned int            transition_latency; /* in 10^(-9) s = nanoseconds */
65 };
66
67 struct cpufreq_real_policy {
68         unsigned int            min;    /* in kHz */
69         unsigned int            max;    /* in kHz */
70         unsigned int            policy; /* see above */
71         struct cpufreq_governor *governor; /* see below */
72 };
73
74 struct cpufreq_policy {
75         cpumask_t               cpus;   /* affected CPUs */
76         unsigned int            shared_type; /* ANY or ALL affected CPUs
77                                                 should set cpufreq */
78         unsigned int            cpu;    /* cpu nr of registered CPU */
79         struct cpufreq_cpuinfo  cpuinfo;/* see above */
80
81         unsigned int            min;    /* in kHz */
82         unsigned int            max;    /* in kHz */
83         unsigned int            cur;    /* in kHz, only needed if cpufreq
84                                          * governors are used */
85         unsigned int            policy; /* see above */
86         struct cpufreq_governor *governor; /* see below */
87
88         struct mutex            lock;   /* CPU ->setpolicy or ->target may
89                                            only be called once a time */
90
91         struct work_struct      update; /* if update_policy() needs to be
92                                          * called, but you're in IRQ context */
93
94         struct cpufreq_real_policy      user_policy;
95
96         struct kobject          kobj;
97         struct completion       kobj_unregister;
98 };
99
100 #define CPUFREQ_ADJUST          (0)
101 #define CPUFREQ_INCOMPATIBLE    (1)
102 #define CPUFREQ_NOTIFY          (2)
103
104 #define CPUFREQ_SHARED_TYPE_ALL (0) /* All dependent CPUs should set freq */
105 #define CPUFREQ_SHARED_TYPE_ANY (1) /* Freq can be set from any dependent CPU */
106
107 /******************** cpufreq transition notifiers *******************/
108
109 #define CPUFREQ_PRECHANGE       (0)
110 #define CPUFREQ_POSTCHANGE      (1)
111 #define CPUFREQ_RESUMECHANGE    (8)
112 #define CPUFREQ_SUSPENDCHANGE   (9)
113
114 struct cpufreq_freqs {
115         unsigned int cpu;       /* cpu nr */
116         unsigned int old;
117         unsigned int new;
118         u8 flags;               /* flags of cpufreq_driver, see below. */
119 };
120
121
122 /**
123  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
124  * @old:   old value
125  * @div:   divisor
126  * @mult:  multiplier
127  *
128  *
129  *    new = old * mult / div
130  */
131 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
132 {
133 #if BITS_PER_LONG == 32
134
135         u64 result = ((u64) old) * ((u64) mult);
136         do_div(result, div);
137         return (unsigned long) result;
138
139 #elif BITS_PER_LONG == 64
140
141         unsigned long result = old * ((u64) mult);
142         result /= div;
143         return result;
144
145 #endif
146 };
147
148 /*********************************************************************
149  *                          CPUFREQ GOVERNORS                        *
150  *********************************************************************/
151
152 #define CPUFREQ_GOV_START  1
153 #define CPUFREQ_GOV_STOP   2
154 #define CPUFREQ_GOV_LIMITS 3
155
156 struct cpufreq_governor {
157         char    name[CPUFREQ_NAME_LEN];
158         int     (*governor)     (struct cpufreq_policy *policy,
159                                  unsigned int event);
160         struct list_head        governor_list;
161         struct module           *owner;
162 };
163
164 /* pass a target to the cpufreq driver 
165  */
166 extern int cpufreq_driver_target(struct cpufreq_policy *policy,
167                                  unsigned int target_freq,
168                                  unsigned int relation);
169 extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
170                                    unsigned int target_freq,
171                                    unsigned int relation);
172
173
174 /* pass an event to the cpufreq governor */
175 int cpufreq_governor(unsigned int cpu, unsigned int event);
176
177 int cpufreq_register_governor(struct cpufreq_governor *governor);
178 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
179
180
181 /*********************************************************************
182  *                      CPUFREQ DRIVER INTERFACE                     *
183  *********************************************************************/
184
185 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
186 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
187
188 struct freq_attr;
189
190 struct cpufreq_driver {
191         struct module           *owner;
192         char                    name[CPUFREQ_NAME_LEN];
193         u8                      flags;
194
195         /* needed by all drivers */
196         int     (*init)         (struct cpufreq_policy *policy);
197         int     (*verify)       (struct cpufreq_policy *policy);
198
199         /* define one out of two */
200         int     (*setpolicy)    (struct cpufreq_policy *policy);
201         int     (*target)       (struct cpufreq_policy *policy,
202                                  unsigned int target_freq,
203                                  unsigned int relation);
204
205         /* should be defined, if possible */
206         unsigned int    (*get)  (unsigned int cpu);
207
208         /* optional */
209         int     (*exit)         (struct cpufreq_policy *policy);
210         int     (*suspend)      (struct cpufreq_policy *policy, pm_message_t pmsg);
211         int     (*resume)       (struct cpufreq_policy *policy);
212         struct freq_attr        **attr;
213 };
214
215 /* flags */
216
217 #define CPUFREQ_STICKY          0x01    /* the driver isn't removed even if 
218                                          * all ->init() calls failed */
219 #define CPUFREQ_CONST_LOOPS     0x02    /* loops_per_jiffy or other kernel
220                                          * "constants" aren't affected by
221                                          * frequency transitions */
222 #define CPUFREQ_PM_NO_WARN      0x04    /* don't warn on suspend/resume speed
223                                          * mismatches */
224
225 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
226 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
227
228
229 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
230
231
232 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max) 
233 {
234         if (policy->min < min)
235                 policy->min = min;
236         if (policy->max < min)
237                 policy->max = min;
238         if (policy->min > max)
239                 policy->min = max;
240         if (policy->max > max)
241                 policy->max = max;
242         if (policy->min > policy->max)
243                 policy->min = policy->max;
244         return;
245 }
246
247 struct freq_attr {
248         struct attribute attr;
249         ssize_t (*show)(struct cpufreq_policy *, char *);
250         ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
251 };
252
253
254 /*********************************************************************
255  *                        CPUFREQ 2.6. INTERFACE                     *
256  *********************************************************************/
257 int cpufreq_set_policy(struct cpufreq_policy *policy);
258 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
259 int cpufreq_update_policy(unsigned int cpu);
260
261 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
262 unsigned int cpufreq_get(unsigned int cpu);
263
264 /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
265 #ifdef CONFIG_CPU_FREQ
266 unsigned int cpufreq_quick_get(unsigned int cpu);
267 #else
268 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
269 {
270         return 0;
271 }
272 #endif
273
274
275 /*********************************************************************
276  *                       CPUFREQ DEFAULT GOVERNOR                    *
277  *********************************************************************/
278
279
280 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
281 extern struct cpufreq_governor cpufreq_gov_performance;
282 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_performance
283 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
284 extern struct cpufreq_governor cpufreq_gov_userspace;
285 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_userspace
286 #endif
287
288
289 /*********************************************************************
290  *                     FREQUENCY TABLE HELPERS                       *
291  *********************************************************************/
292
293 #define CPUFREQ_ENTRY_INVALID ~0
294 #define CPUFREQ_TABLE_END     ~1
295
296 struct cpufreq_frequency_table {
297         unsigned int    index;     /* any */
298         unsigned int    frequency; /* kHz - doesn't need to be in ascending
299                                     * order */
300 };
301
302 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
303                                     struct cpufreq_frequency_table *table);
304
305 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
306                                    struct cpufreq_frequency_table *table);
307
308 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
309                                    struct cpufreq_frequency_table *table,
310                                    unsigned int target_freq,
311                                    unsigned int relation,
312                                    unsigned int *index);
313
314 /* the following 3 funtions are for cpufreq core use only */
315 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
316 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
317 void   cpufreq_cpu_put (struct cpufreq_policy *data);
318
319 /* the following are really really optional */
320 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
321
322 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table, 
323                                       unsigned int cpu);
324
325 void cpufreq_frequency_table_put_attr(unsigned int cpu);
326
327
328 /*********************************************************************
329  *                     UNIFIED DEBUG HELPERS                         *
330  *********************************************************************/
331
332 #define CPUFREQ_DEBUG_CORE      1
333 #define CPUFREQ_DEBUG_DRIVER    2
334 #define CPUFREQ_DEBUG_GOVERNOR  4
335
336 #ifdef CONFIG_CPU_FREQ_DEBUG
337
338 extern void cpufreq_debug_printk(unsigned int type, const char *prefix, 
339                                  const char *fmt, ...);
340
341 #else
342
343 #define cpufreq_debug_printk(msg...) do { } while(0)
344
345 #endif /* CONFIG_CPU_FREQ_DEBUG */
346
347 #endif /* _LINUX_CPUFREQ_H */