ARM: rockchip: rk3228: add grf definition
[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  * 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 <asm/cputime.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/threads.h>
18 #include <linux/kobject.h>
19 #include <linux/sysfs.h>
20 #include <linux/completion.h>
21 #include <linux/workqueue.h>
22 #include <linux/cpumask.h>
23 #include <asm/div64.h>
24 #include <asm/cputime.h>
25
26 #define CPUFREQ_NAME_LEN 16
27 /* Print length for names. Extra 1 space for accomodating '\n' in prints */
28 #define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1)
29
30
31 /*********************************************************************
32  *                     CPUFREQ NOTIFIER INTERFACE                    *
33  *********************************************************************/
34
35 #define CPUFREQ_TRANSITION_NOTIFIER     (0)
36 #define CPUFREQ_POLICY_NOTIFIER         (1)
37
38 #ifdef CONFIG_CPU_FREQ
39 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
40 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
41 extern void disable_cpufreq(void);
42 #else           /* CONFIG_CPU_FREQ */
43 static inline int cpufreq_register_notifier(struct notifier_block *nb,
44                                                 unsigned int list)
45 {
46         return 0;
47 }
48 static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
49                                                 unsigned int list)
50 {
51         return 0;
52 }
53 static inline void disable_cpufreq(void) { }
54 #endif          /* CONFIG_CPU_FREQ */
55
56 /* if (cpufreq_driver->target) exists, the ->governor decides what frequency
57  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
58  * two generic policies are available:
59  */
60
61 #define CPUFREQ_POLICY_POWERSAVE        (1)
62 #define CPUFREQ_POLICY_PERFORMANCE      (2)
63
64 /* Frequency values here are CPU kHz so that hardware which doesn't run
65  * with some frequencies can complain without having to guess what per
66  * cent / per mille means.
67  * Maximum transition latency is in nanoseconds - if it's unknown,
68  * CPUFREQ_ETERNAL shall be used.
69  */
70
71 struct cpufreq_governor;
72
73 /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
74 extern struct kobject *cpufreq_global_kobject;
75
76 #define CPUFREQ_ETERNAL                 (-1)
77 struct cpufreq_cpuinfo {
78         unsigned int            max_freq;
79         unsigned int            min_freq;
80
81         /* in 10^(-9) s = nanoseconds */
82         unsigned int            transition_latency;
83 };
84
85 struct cpufreq_real_policy {
86         unsigned int            min;    /* in kHz */
87         unsigned int            max;    /* in kHz */
88         unsigned int            policy; /* see above */
89         struct cpufreq_governor *governor; /* see below */
90 };
91
92 struct cpufreq_policy {
93         /* CPUs sharing clock, require sw coordination */
94         cpumask_var_t           cpus;   /* Online CPUs only */
95         cpumask_var_t           related_cpus; /* Online + Offline CPUs */
96
97         unsigned int            shared_type; /* ACPI: ANY or ALL affected CPUs
98                                                 should set cpufreq */
99         unsigned int            cpu;    /* cpu nr of CPU managing this policy */
100         unsigned int            last_cpu; /* cpu nr of previous CPU that managed
101                                            * this policy */
102         struct cpufreq_cpuinfo  cpuinfo;/* see above */
103
104         unsigned int            min;    /* in kHz */
105         unsigned int            max;    /* in kHz */
106         unsigned int            cur;    /* in kHz, only needed if cpufreq
107                                          * governors are used */
108         unsigned int            policy; /* see above */
109         struct cpufreq_governor *governor; /* see below */
110         void                    *governor_data;
111         bool                    governor_enabled; /* governor start/stop flag */
112
113         struct work_struct      update; /* if update_policy() needs to be
114                                          * called, but you're in IRQ context */
115
116         struct cpufreq_real_policy      user_policy;
117
118         struct kobject          kobj;
119         struct completion       kobj_unregister;
120 };
121
122 #define CPUFREQ_ADJUST                  (0)
123 #define CPUFREQ_INCOMPATIBLE            (1)
124 #define CPUFREQ_NOTIFY                  (2)
125 #define CPUFREQ_START                   (3)
126 #define CPUFREQ_UPDATE_POLICY_CPU       (4)
127
128 /* Only for ACPI */
129 #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
130 #define CPUFREQ_SHARED_TYPE_HW   (1) /* HW does needed coordination */
131 #define CPUFREQ_SHARED_TYPE_ALL  (2) /* All dependent CPUs should set freq */
132 #define CPUFREQ_SHARED_TYPE_ANY  (3) /* Freq can be set from any dependent CPU*/
133
134 static inline bool policy_is_shared(struct cpufreq_policy *policy)
135 {
136         return cpumask_weight(policy->cpus) > 1;
137 }
138
139 /******************** cpufreq transition notifiers *******************/
140
141 #define CPUFREQ_PRECHANGE       (0)
142 #define CPUFREQ_POSTCHANGE      (1)
143 #define CPUFREQ_RESUMECHANGE    (8)
144 #define CPUFREQ_SUSPENDCHANGE   (9)
145
146 struct cpufreq_freqs {
147         unsigned int cpu;       /* cpu nr */
148         unsigned int old;
149         unsigned int new;
150         u8 flags;               /* flags of cpufreq_driver, see below. */
151 };
152
153
154 /**
155  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
156  * @old:   old value
157  * @div:   divisor
158  * @mult:  multiplier
159  *
160  *
161  *    new = old * mult / div
162  */
163 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
164 {
165 #if BITS_PER_LONG == 32
166
167         u64 result = ((u64) old) * ((u64) mult);
168         do_div(result, div);
169         return (unsigned long) result;
170
171 #elif BITS_PER_LONG == 64
172
173         unsigned long result = old * ((u64) mult);
174         result /= div;
175         return result;
176
177 #endif
178 };
179
180 /*********************************************************************
181  *                          CPUFREQ GOVERNORS                        *
182  *********************************************************************/
183
184 #define CPUFREQ_GOV_START       1
185 #define CPUFREQ_GOV_STOP        2
186 #define CPUFREQ_GOV_LIMITS      3
187 #define CPUFREQ_GOV_POLICY_INIT 4
188 #define CPUFREQ_GOV_POLICY_EXIT 5
189
190 struct cpufreq_governor {
191         char    name[CPUFREQ_NAME_LEN];
192         int     initialized;
193         int     (*governor)     (struct cpufreq_policy *policy,
194                                  unsigned int event);
195         ssize_t (*show_setspeed)        (struct cpufreq_policy *policy,
196                                          char *buf);
197         int     (*store_setspeed)       (struct cpufreq_policy *policy,
198                                          unsigned int freq);
199         unsigned int max_transition_latency; /* HW must be able to switch to
200                         next freq faster than this value in nano secs or we
201                         will fallback to performance governor */
202         struct list_head        governor_list;
203         struct module           *owner;
204 };
205
206 /*
207  * Pass a target to the cpufreq driver.
208  */
209 extern int cpufreq_driver_target(struct cpufreq_policy *policy,
210                                  unsigned int target_freq,
211                                  unsigned int relation);
212 extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
213                                    unsigned int target_freq,
214                                    unsigned int relation);
215
216
217 extern int __cpufreq_driver_getavg(struct cpufreq_policy *policy,
218                                    unsigned int cpu);
219
220 int cpufreq_register_governor(struct cpufreq_governor *governor);
221 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
222
223
224 /*********************************************************************
225  *                      CPUFREQ DRIVER INTERFACE                     *
226  *********************************************************************/
227
228 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
229 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
230
231 struct freq_attr;
232
233 struct cpufreq_driver {
234         struct module           *owner;
235         char                    name[CPUFREQ_NAME_LEN];
236         u8                      flags;
237         /*
238          * This should be set by platforms having multiple clock-domains, i.e.
239          * supporting multiple policies. With this sysfs directories of governor
240          * would be created in cpu/cpu<num>/cpufreq/ directory and so they can
241          * use the same governor with different tunables for different clusters.
242          */
243         bool                    have_governor_per_policy;
244
245         /* needed by all drivers */
246         int     (*init)         (struct cpufreq_policy *policy);
247         int     (*verify)       (struct cpufreq_policy *policy);
248
249         /* define one out of two */
250         int     (*setpolicy)    (struct cpufreq_policy *policy);
251         int     (*target)       (struct cpufreq_policy *policy,
252                                  unsigned int target_freq,
253                                  unsigned int relation);
254
255         /* should be defined, if possible */
256         unsigned int    (*get)  (unsigned int cpu);
257
258         /* optional */
259         unsigned int (*getavg)  (struct cpufreq_policy *policy,
260                                  unsigned int cpu);
261         int     (*bios_limit)   (int cpu, unsigned int *limit);
262
263         int     (*exit)         (struct cpufreq_policy *policy);
264         int     (*suspend)      (struct cpufreq_policy *policy);
265         int     (*resume)       (struct cpufreq_policy *policy);
266         struct freq_attr        **attr;
267 };
268
269 /* flags */
270
271 #define CPUFREQ_STICKY          0x01    /* the driver isn't removed even if
272                                          * all ->init() calls failed */
273 #define CPUFREQ_CONST_LOOPS     0x02    /* loops_per_jiffy or other kernel
274                                          * "constants" aren't affected by
275                                          * frequency transitions */
276 #define CPUFREQ_PM_NO_WARN      0x04    /* don't warn on suspend/resume speed
277                                          * mismatches */
278
279 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
280 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
281
282
283 void cpufreq_notify_transition(struct cpufreq_policy *policy,
284                 struct cpufreq_freqs *freqs, unsigned int state);
285
286 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max)
287 {
288         if (policy->min < min)
289                 policy->min = min;
290         if (policy->max < min)
291                 policy->max = min;
292         if (policy->min > max)
293                 policy->min = max;
294         if (policy->max > max)
295                 policy->max = max;
296         if (policy->min > policy->max)
297                 policy->min = policy->max;
298         return;
299 }
300
301 struct freq_attr {
302         struct attribute attr;
303         ssize_t (*show)(struct cpufreq_policy *, char *);
304         ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
305 };
306
307 #define cpufreq_freq_attr_ro(_name)             \
308 static struct freq_attr _name =                 \
309 __ATTR(_name, 0444, show_##_name, NULL)
310
311 #define cpufreq_freq_attr_ro_perm(_name, _perm) \
312 static struct freq_attr _name =                 \
313 __ATTR(_name, _perm, show_##_name, NULL)
314
315 #define cpufreq_freq_attr_rw(_name)             \
316 static struct freq_attr _name =                 \
317 __ATTR(_name, 0644, show_##_name, store_##_name)
318
319 struct global_attr {
320         struct attribute attr;
321         ssize_t (*show)(struct kobject *kobj,
322                         struct attribute *attr, char *buf);
323         ssize_t (*store)(struct kobject *a, struct attribute *b,
324                          const char *c, size_t count);
325 };
326
327 #define define_one_global_ro(_name)             \
328 static struct global_attr _name =               \
329 __ATTR(_name, 0444, show_##_name, NULL)
330
331 #define define_one_global_rw(_name)             \
332 static struct global_attr _name =               \
333 __ATTR(_name, 0644, show_##_name, store_##_name)
334
335 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
336 void cpufreq_cpu_put(struct cpufreq_policy *data);
337 const char *cpufreq_get_current_driver(void);
338
339 /*********************************************************************
340  *                        CPUFREQ 2.6. INTERFACE                     *
341  *********************************************************************/
342 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
343 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
344 int cpufreq_update_policy(unsigned int cpu);
345 bool have_governor_per_policy(void);
346 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
347
348 #ifdef CONFIG_CPU_FREQ
349 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
350 unsigned int cpufreq_get(unsigned int cpu);
351 #else
352 static inline unsigned int cpufreq_get(unsigned int cpu)
353 {
354         return 0;
355 }
356 #endif
357
358 /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
359 #ifdef CONFIG_CPU_FREQ
360 unsigned int cpufreq_quick_get(unsigned int cpu);
361 unsigned int cpufreq_quick_get_max(unsigned int cpu);
362 #else
363 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
364 {
365         return 0;
366 }
367 static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
368 {
369         return 0;
370 }
371 #endif
372
373
374 /*********************************************************************
375  *                       CPUFREQ DEFAULT GOVERNOR                    *
376  *********************************************************************/
377
378
379 /*
380   Performance governor is fallback governor if any other gov failed to
381   auto load due latency restrictions
382 */
383 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
384 extern struct cpufreq_governor cpufreq_gov_performance;
385 #endif
386 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
387 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_performance)
388 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
389 extern struct cpufreq_governor cpufreq_gov_powersave;
390 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_powersave)
391 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
392 extern struct cpufreq_governor cpufreq_gov_userspace;
393 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_userspace)
394 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
395 extern struct cpufreq_governor cpufreq_gov_ondemand;
396 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_ondemand)
397 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
398 extern struct cpufreq_governor cpufreq_gov_conservative;
399 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_conservative)
400 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE)
401 extern struct cpufreq_governor cpufreq_gov_interactive;
402 #define CPUFREQ_DEFAULT_GOVERNOR        (&cpufreq_gov_interactive)
403 #endif
404
405
406 /*********************************************************************
407  *                     FREQUENCY TABLE HELPERS                       *
408  *********************************************************************/
409
410 #define CPUFREQ_ENTRY_INVALID ~0
411 #define CPUFREQ_TABLE_END     ~1
412
413 struct cpufreq_frequency_table {
414         unsigned int    index;     /* any */
415         unsigned int    frequency; /* kHz - doesn't need to be in ascending
416                                     * order */
417 };
418
419 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
420                                     struct cpufreq_frequency_table *table);
421
422 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
423                                    struct cpufreq_frequency_table *table);
424
425 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
426                                    struct cpufreq_frequency_table *table,
427                                    unsigned int target_freq,
428                                    unsigned int relation,
429                                    unsigned int *index);
430
431 /* the following 3 funtions are for cpufreq core use only */
432 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
433
434 /* the following are really really optional */
435 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
436
437 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
438                                       unsigned int cpu);
439 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
440
441 void cpufreq_frequency_table_put_attr(unsigned int cpu);
442
443 /*********************************************************************
444  *                         CPUFREQ STATS                             *
445  *********************************************************************/
446
447 void acct_update_power(struct task_struct *p, cputime_t cputime);
448
449 #endif /* _LINUX_CPUFREQ_H */