vserver 1.9.3
[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
213
214 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
215 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
216
217
218 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
219
220
221 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max) 
222 {
223         if (policy->min < min)
224                 policy->min = min;
225         if (policy->max < min)
226                 policy->max = min;
227         if (policy->min > max)
228                 policy->min = max;
229         if (policy->max > max)
230                 policy->max = max;
231         if (policy->min > policy->max)
232                 policy->min = policy->max;
233         return;
234 }
235
236 struct freq_attr {
237         struct attribute attr;
238         ssize_t (*show)(struct cpufreq_policy *, char *);
239         ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
240 };
241
242
243 /*********************************************************************
244  *                        CPUFREQ 2.6. INTERFACE                     *
245  *********************************************************************/
246 int cpufreq_set_policy(struct cpufreq_policy *policy);
247 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
248 int cpufreq_update_policy(unsigned int cpu);
249
250 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
251 unsigned int cpufreq_get(unsigned int cpu);
252
253 /* the proc_intf.c needs this */
254 int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpufreq_governor **governor);
255
256
257 /*********************************************************************
258  *                      CPUFREQ USERSPACE GOVERNOR                   *
259  *********************************************************************/
260 #ifdef CONFIG_CPU_FREQ_24_API
261
262 int cpufreq_setmax(unsigned int cpu);
263 int cpufreq_set(unsigned int kHz, unsigned int cpu);
264
265
266 /* /proc/sys/cpu */
267 enum {
268         CPU_NR   = 1,           /* compatibilty reasons */
269         CPU_NR_0 = 1,
270         CPU_NR_1 = 2,
271         CPU_NR_2 = 3,
272         CPU_NR_3 = 4,
273         CPU_NR_4 = 5,
274         CPU_NR_5 = 6,
275         CPU_NR_6 = 7,
276         CPU_NR_7 = 8,
277         CPU_NR_8 = 9,
278         CPU_NR_9 = 10,
279         CPU_NR_10 = 11,
280         CPU_NR_11 = 12,
281         CPU_NR_12 = 13,
282         CPU_NR_13 = 14,
283         CPU_NR_14 = 15,
284         CPU_NR_15 = 16,
285         CPU_NR_16 = 17,
286         CPU_NR_17 = 18,
287         CPU_NR_18 = 19,
288         CPU_NR_19 = 20,
289         CPU_NR_20 = 21,
290         CPU_NR_21 = 22,
291         CPU_NR_22 = 23,
292         CPU_NR_23 = 24,
293         CPU_NR_24 = 25,
294         CPU_NR_25 = 26,
295         CPU_NR_26 = 27,
296         CPU_NR_27 = 28,
297         CPU_NR_28 = 29,
298         CPU_NR_29 = 30,
299         CPU_NR_30 = 31,
300         CPU_NR_31 = 32,
301 };
302
303 /* /proc/sys/cpu/{0,1,...,(NR_CPUS-1)} */
304 enum {
305         CPU_NR_FREQ_MAX = 1,
306         CPU_NR_FREQ_MIN = 2,
307         CPU_NR_FREQ = 3,
308 };
309
310 #endif /* CONFIG_CPU_FREQ_24_API */
311
312
313 /*********************************************************************
314  *                       CPUFREQ DEFAULT GOVERNOR                    *
315  *********************************************************************/
316
317
318 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
319 extern struct cpufreq_governor cpufreq_gov_performance;
320 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_performance
321 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
322 extern struct cpufreq_governor cpufreq_gov_userspace;
323 #define CPUFREQ_DEFAULT_GOVERNOR        &cpufreq_gov_userspace
324 #endif
325
326
327 /*********************************************************************
328  *                     FREQUENCY TABLE HELPERS                       *
329  *********************************************************************/
330
331 #define CPUFREQ_ENTRY_INVALID ~0
332 #define CPUFREQ_TABLE_END     ~1
333
334 struct cpufreq_frequency_table {
335         unsigned int    index;     /* any */
336         unsigned int    frequency; /* kHz - doesn't need to be in ascending
337                                     * order */
338 };
339
340 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
341                                     struct cpufreq_frequency_table *table);
342
343 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
344                                    struct cpufreq_frequency_table *table);
345
346 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
347                                    struct cpufreq_frequency_table *table,
348                                    unsigned int target_freq,
349                                    unsigned int relation,
350                                    unsigned int *index);
351
352 /* the following are really really optional */
353 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
354
355 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table, 
356                                       unsigned int cpu);
357
358 void cpufreq_frequency_table_put_attr(unsigned int cpu);
359
360
361 #endif /* _LINUX_CPUFREQ_H */