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