2 * linux/include/linux/cpufreq.h
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
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.
11 #ifndef _LINUX_CPUFREQ_H
12 #define _LINUX_CPUFREQ_H
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>
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)
31 /*********************************************************************
32 * CPUFREQ NOTIFIER INTERFACE *
33 *********************************************************************/
35 #define CPUFREQ_TRANSITION_NOTIFIER (0)
36 #define CPUFREQ_POLICY_NOTIFIER (1)
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,
48 static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
53 static inline void disable_cpufreq(void) { }
54 #endif /* CONFIG_CPU_FREQ */
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:
61 #define CPUFREQ_POLICY_POWERSAVE (1)
62 #define CPUFREQ_POLICY_PERFORMANCE (2)
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.
71 struct cpufreq_governor;
73 /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
74 extern struct kobject *cpufreq_global_kobject;
76 #define CPUFREQ_ETERNAL (-1)
77 struct cpufreq_cpuinfo {
78 unsigned int max_freq;
79 unsigned int min_freq;
81 /* in 10^(-9) s = nanoseconds */
82 unsigned int transition_latency;
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 */
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 */
97 unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs
99 unsigned int cpu; /* cpu nr of CPU managing this policy */
100 unsigned int last_cpu; /* cpu nr of previous CPU that managed
102 struct cpufreq_cpuinfo cpuinfo;/* see above */
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 */
111 bool governor_enabled; /* governor start/stop flag */
113 struct work_struct update; /* if update_policy() needs to be
114 * called, but you're in IRQ context */
116 struct cpufreq_real_policy user_policy;
119 struct completion kobj_unregister;
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)
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*/
134 static inline bool policy_is_shared(struct cpufreq_policy *policy)
136 return cpumask_weight(policy->cpus) > 1;
139 /******************** cpufreq transition notifiers *******************/
141 #define CPUFREQ_PRECHANGE (0)
142 #define CPUFREQ_POSTCHANGE (1)
143 #define CPUFREQ_RESUMECHANGE (8)
144 #define CPUFREQ_SUSPENDCHANGE (9)
146 struct cpufreq_freqs {
147 unsigned int cpu; /* cpu nr */
150 u8 flags; /* flags of cpufreq_driver, see below. */
155 * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
161 * new = old * mult / div
163 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
165 #if BITS_PER_LONG == 32
167 u64 result = ((u64) old) * ((u64) mult);
169 return (unsigned long) result;
171 #elif BITS_PER_LONG == 64
173 unsigned long result = old * ((u64) mult);
180 /*********************************************************************
181 * CPUFREQ GOVERNORS *
182 *********************************************************************/
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
190 struct cpufreq_governor {
191 char name[CPUFREQ_NAME_LEN];
193 int (*governor) (struct cpufreq_policy *policy,
195 ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
197 int (*store_setspeed) (struct cpufreq_policy *policy,
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;
207 * Pass a target to the cpufreq driver.
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);
217 extern int __cpufreq_driver_getavg(struct cpufreq_policy *policy,
220 int cpufreq_register_governor(struct cpufreq_governor *governor);
221 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
224 /*********************************************************************
225 * CPUFREQ DRIVER INTERFACE *
226 *********************************************************************/
228 #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
229 #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
233 struct cpufreq_driver {
234 struct module *owner;
235 char name[CPUFREQ_NAME_LEN];
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.
243 bool have_governor_per_policy;
245 /* needed by all drivers */
246 int (*init) (struct cpufreq_policy *policy);
247 int (*verify) (struct cpufreq_policy *policy);
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);
255 /* should be defined, if possible */
256 unsigned int (*get) (unsigned int cpu);
259 unsigned int (*getavg) (struct cpufreq_policy *policy,
261 int (*bios_limit) (int cpu, unsigned int *limit);
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;
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
279 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
280 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
283 void cpufreq_notify_transition(struct cpufreq_policy *policy,
284 struct cpufreq_freqs *freqs, unsigned int state);
286 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max)
288 if (policy->min < min)
290 if (policy->max < min)
292 if (policy->min > max)
294 if (policy->max > max)
296 if (policy->min > policy->max)
297 policy->min = policy->max;
302 struct attribute attr;
303 ssize_t (*show)(struct cpufreq_policy *, char *);
304 ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
307 #define cpufreq_freq_attr_ro(_name) \
308 static struct freq_attr _name = \
309 __ATTR(_name, 0444, show_##_name, NULL)
311 #define cpufreq_freq_attr_ro_perm(_name, _perm) \
312 static struct freq_attr _name = \
313 __ATTR(_name, _perm, show_##_name, NULL)
315 #define cpufreq_freq_attr_rw(_name) \
316 static struct freq_attr _name = \
317 __ATTR(_name, 0644, show_##_name, store_##_name)
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);
327 #define define_one_global_ro(_name) \
328 static struct global_attr _name = \
329 __ATTR(_name, 0444, show_##_name, NULL)
331 #define define_one_global_rw(_name) \
332 static struct global_attr _name = \
333 __ATTR(_name, 0644, show_##_name, store_##_name)
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);
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);
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);
352 static inline unsigned int cpufreq_get(unsigned int cpu)
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);
363 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
367 static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
374 /*********************************************************************
375 * CPUFREQ DEFAULT GOVERNOR *
376 *********************************************************************/
380 Performance governor is fallback governor if any other gov failed to
381 auto load due latency restrictions
383 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
384 extern struct cpufreq_governor cpufreq_gov_performance;
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)
406 /*********************************************************************
407 * FREQUENCY TABLE HELPERS *
408 *********************************************************************/
410 #define CPUFREQ_ENTRY_INVALID ~0
411 #define CPUFREQ_TABLE_END ~1
413 struct cpufreq_frequency_table {
414 unsigned int index; /* any */
415 unsigned int frequency; /* kHz - doesn't need to be in ascending
419 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
420 struct cpufreq_frequency_table *table);
422 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
423 struct cpufreq_frequency_table *table);
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);
431 /* the following 3 funtions are for cpufreq core use only */
432 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
434 /* the following are really really optional */
435 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
437 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
439 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
441 void cpufreq_frequency_table_put_attr(unsigned int cpu);
443 /*********************************************************************
445 *********************************************************************/
447 void acct_update_power(struct task_struct *p, cputime_t cputime);
449 #endif /* _LINUX_CPUFREQ_H */