patch-2_6_7-vs1_9_1_12
[linux-2.6.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/config.h>
18 #include <linux/notifier.h>
19 #include <linux/threads.h>
20 #include <linux/device.h>
21 #include <linux/kobject.h>
22 #include <linux/sysfs.h>
23 #include <linux/completion.h>
24 #include <linux/workqueue.h>
25
26 #define CPUFREQ_NAME_LEN 16
27
28
29 /*********************************************************************
30  *                     CPUFREQ NOTIFIER INTERFACE                    *
31  *********************************************************************/
32
33 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
34 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
35
36 #define CPUFREQ_TRANSITION_NOTIFIER     (0)
37 #define CPUFREQ_POLICY_NOTIFIER         (1)
38
39
40 /* if (cpufreq_driver->target) exists, the ->governor decides what frequency
41  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
42  * two generic policies are available:
43  */
44
45 #define CPUFREQ_POLICY_POWERSAVE        (1)
46 #define CPUFREQ_POLICY_PERFORMANCE      (2)
47
48 /* Frequency values here are CPU kHz so that hardware which doesn't run 
49  * with some frequencies can complain without having to guess what per 
50  * cent / per mille means. 
51  * Maximum transition latency is in microseconds - if it's unknown,
52  * CPUFREQ_ETERNAL shall be used.
53  */
54
55 struct cpufreq_governor;
56
57 #define CPUFREQ_ETERNAL                 (-1)
58 struct cpufreq_cpuinfo {
59         unsigned int            max_freq;
60         unsigned int            min_freq;
61         unsigned int            transition_latency; /* in 10^(-9) s = nanoseconds */
62 };
63
64 struct cpufreq_real_policy {
65         unsigned int            min;    /* in kHz */
66         unsigned int            max;    /* in kHz */
67         unsigned int            policy; /* see above */
68         struct cpufreq_governor *governor; /* see below */
69 };
70
71 struct cpufreq_policy {
72         unsigned int            cpu;    /* cpu nr */
73         struct cpufreq_cpuinfo  cpuinfo;/* see above */
74
75         unsigned int            min;    /* in kHz */
76         unsigned int            max;    /* in kHz */
77         unsigned int            cur;    /* in kHz, only needed if cpufreq
78                                          * governors are used */
79         unsigned int            policy; /* see above */
80         struct cpufreq_governor *governor; /* see below */
81
82         struct semaphore        lock;   /* CPU ->setpolicy or ->target may
83                                            only be called once a time */
84
85         struct work_struct      update; /* if update_policy() needs to be
86                                          * called, but you're in IRQ context */
87
88         struct cpufreq_real_policy      user_policy;
89
90         struct kobject          kobj;
91         struct completion       kobj_unregister;
92 };
93
94 #define CPUFREQ_ADJUST          (0)
95 #define CPUFREQ_INCOMPATIBLE    (1)
96 #define CPUFREQ_NOTIFY          (2)
97
98
99 /******************** cpufreq transition notifiers *******************/
100
101 #define CPUFREQ_PRECHANGE       (0)
102 #define CPUFREQ_POSTCHANGE      (1)
103 #define CPUFREQ_RESUMECHANGE    (8)
104
105 struct cpufreq_freqs {
106         unsigned int cpu;       /* cpu nr */
107         unsigned int old;
108         unsigned int new;
109         u8 flags;               /* flags of cpufreq_driver, see below. */
110 };
111
112
113 /**
114  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
115  * @old:   old value
116  * @div:   divisor
117  * @mult:  multiplier
118  *
119  *
120  *    new = old * mult / div
121  */
122 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
123 {
124 #if BITS_PER_LONG == 32
125
126         u64 result = ((u64) old) * ((u64) mult);
127         do_div(result, div);
128         return (unsigned long) result;
129
130 #elif BITS_PER_LONG == 64
131
132         unsigned long result = old * ((u64) mult);
133         result /= div;
134         return result;
135
136 #endif
137 };
138
139 /*********************************************************************
140  *                          CPUFREQ GOVERNORS                        *
141  *********************************************************************/
142
143 #define CPUFREQ_GOV_START  1
144 #define CPUFREQ_GOV_STOP   2
145 #define CPUFREQ_GOV_LIMITS 3
146
147 struct cpufreq_governor {
148         char    name[CPUFREQ_NAME_LEN];
149         int     (*governor)     (struct cpufreq_policy *policy,
150                                  unsigned int event);
151         struct list_head        governor_list;
152         struct module           *owner;
153 };
154
155 /* pass a target to the cpufreq driver 
156  */
157 extern int cpufreq_driver_target(struct cpufreq_policy *policy,
158                                  unsigned int target_freq,
159                                  unsigned int relation);
160 extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
161                                    unsigned int target_freq,
162                                    unsigned int relation);
163
164
165 /* pass an event to the cpufreq governor */
166 int cpufreq_governor(unsigned int cpu, unsigned int event);
167
168 int cpufreq_register_governor(struct cpufreq_governor *governor);
169 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
170
171
172 /*********************************************************************
173  *                      CPUFREQ DRIVER INTERFACE                     *
174  *********************************************************************/
175
176 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
177 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
178
179 struct freq_attr;
180
181 struct cpufreq_driver {
182         struct module           *owner;
183         char                    name[CPUFREQ_NAME_LEN];
184         u8                      flags;
185
186         /* needed by all drivers */
187         int     (*init)         (struct cpufreq_policy *policy);
188         int     (*verify)       (struct cpufreq_policy *policy);
189
190         /* define one out of two */
191         int     (*setpolicy)    (struct cpufreq_policy *policy);
192         int     (*target)       (struct cpufreq_policy *policy,
193                                  unsigned int target_freq,
194                                  unsigned int relation);
195
196         /* should be defined, if possible */
197         unsigned int    (*get)  (unsigned int cpu);
198
199         /* optional */
200         int     (*exit)         (struct cpufreq_policy *policy);
201         int     (*resume)       (struct cpufreq_policy *policy);
202         struct freq_attr        **attr;
203 };
204
205 /* flags */
206
207 #define CPUFREQ_STICKY          0x01    /* the driver isn't removed even if 
208                                          * all ->init() calls failed */
209 #define CPUFREQ_CONST_LOOPS     0x02    /* loops_per_jiffy or other kernel
210                                          * "constants" aren't affected by
211                                          * frequency transitions */
212 #define CPUFREQ_PANIC_OUTOFSYNC 0x04    /* panic if cpufreq's opinion of
213                                          * current frequency differs from
214                                          * actual frequency */
215 #define CPUFREQ_PANIC_RESUME_OUTOFSYNC 0x08 /* panic if cpufreq's opinion of
216                                          * current frequency differs from
217                                          * actual frequency on resume
218                                          * from sleep. */
219
220
221 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
222 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
223
224
225 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
226
227
228 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max) 
229 {
230         if (policy->min < min)
231                 policy->min = min;
232         if (policy->max < min)
233                 policy->max = min;
234         if (policy->min > max)
235                 policy->min = max;
236         if (policy->max > max)
237                 policy->max = max;
238         if (policy->min > policy->max)
239                 policy->min = policy->max;
240         return;
241 }
242
243 struct freq_attr {
244         struct attribute attr;
245         ssize_t (*show)(struct cpufreq_policy *, char *);
246         ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
247 };
248
249
250 /*********************************************************************
251  *                        CPUFREQ 2.6. INTERFACE                     *
252  *********************************************************************/
253 int cpufreq_set_policy(struct cpufreq_policy *policy);
254 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
255 int cpufreq_update_policy(unsigned int cpu);
256
257 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
258 unsigned int cpufreq_get(unsigned int cpu);
259
260 /* the proc_intf.c needs this */
261 int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpufreq_governor **governor);
262
263
264 /*********************************************************************
265  *                      CPUFREQ USERSPACE GOVERNOR                   *
266  *********************************************************************/
267 #ifdef CONFIG_CPU_FREQ_24_API
268
269 int cpufreq_setmax(unsigned int cpu);
270 int cpufreq_set(unsigned int kHz, unsigned int cpu);
271
272
273 /* /proc/sys/cpu */
274 enum {
275         CPU_NR   = 1,           /* compatibilty reasons */
276         CPU_NR_0 = 1,
277         CPU_NR_1 = 2,
278         CPU_NR_2 = 3,
279         CPU_NR_3 = 4,
280         CPU_NR_4 = 5,
281         CPU_NR_5 = 6,
282         CPU_NR_6 = 7,
283         CPU_NR_7 = 8,
284         CPU_NR_8 = 9,
285         CPU_NR_9 = 10,
286         CPU_NR_10 = 11,
287         CPU_NR_11 = 12,
288         CPU_NR_12 = 13,
289         CPU_NR_13 = 14,
290         CPU_NR_14 = 15,
291         CPU_NR_15 = 16,
292         CPU_NR_16 = 17,
293         CPU_NR_17 = 18,
294         CPU_NR_18 = 19,
295         CPU_NR_19 = 20,
296         CPU_NR_20 = 21,
297         CPU_NR_21 = 22,
298         CPU_NR_22 = 23,
299         CPU_NR_23 = 24,
300         CPU_NR_24 = 25,
301         CPU_NR_25 = 26,
302         CPU_NR_26 = 27,
303         CPU_NR_27 = 28,
304         CPU_NR_28 = 29,
305         CPU_NR_29 = 30,
306         CPU_NR_30 = 31,
307         CPU_NR_31 = 32,
308 };
309
310 /* /proc/sys/cpu/{0,1,...,(NR_CPUS-1)} */
311 enum {
312         CPU_NR_FREQ_MAX = 1,
313         CPU_NR_FREQ_MIN = 2,
314         CPU_NR_FREQ = 3,
315 };
316
317 #endif /* CONFIG_CPU_FREQ_24_API */
318
319
320 /*********************************************************************
321  *                       CPUFREQ DEFAULT GOVERNOR                    *
322  *********************************************************************/
323
324
325 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
326 extern struct cpufreq_governor cpufreq_gov_performance;
327 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_performance
328 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
329 extern struct cpufreq_governor cpufreq_gov_userspace;
330 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_userspace
331 #endif
332
333
334 /*********************************************************************
335  *                     FREQUENCY TABLE HELPERS                       *
336  *********************************************************************/
337
338 #define CPUFREQ_ENTRY_INVALID ~0
339 #define CPUFREQ_TABLE_END     ~1
340
341 struct cpufreq_frequency_table {
342         unsigned int    index;     /* any */
343         unsigned int    frequency; /* kHz - doesn't need to be in ascending
344                                     * order */
345 };
346
347 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
348                                     struct cpufreq_frequency_table *table);
349
350 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
351                                    struct cpufreq_frequency_table *table);
352
353 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
354                                    struct cpufreq_frequency_table *table,
355                                    unsigned int target_freq,
356                                    unsigned int relation,
357                                    unsigned int *index);
358
359 /* the following are really really optional */
360 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
361
362 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table, 
363                                       unsigned int cpu);
364
365 void cpufreq_frequency_table_put_attr(unsigned int cpu);
366
367
368 #endif /* _LINUX_CPUFREQ_H */