Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/cpumask.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 26 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #define CPUFREQ_NAME_LEN 16 |
| 29 | |
| 30 | |
| 31 | /********************************************************************* |
| 32 | * CPUFREQ NOTIFIER INTERFACE * |
| 33 | *********************************************************************/ |
| 34 | |
| 35 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list); |
| 36 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list); |
| 37 | |
| 38 | #define CPUFREQ_TRANSITION_NOTIFIER (0) |
| 39 | #define CPUFREQ_POLICY_NOTIFIER (1) |
| 40 | |
| 41 | |
| 42 | /* if (cpufreq_driver->target) exists, the ->governor decides what frequency |
| 43 | * within the limits is used. If (cpufreq_driver->setpolicy> exists, these |
| 44 | * two generic policies are available: |
| 45 | */ |
| 46 | |
| 47 | #define CPUFREQ_POLICY_POWERSAVE (1) |
| 48 | #define CPUFREQ_POLICY_PERFORMANCE (2) |
| 49 | |
| 50 | /* Frequency values here are CPU kHz so that hardware which doesn't run |
| 51 | * with some frequencies can complain without having to guess what per |
| 52 | * cent / per mille means. |
Dave Jones | b53cc6e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 53 | * Maximum transition latency is in nanoseconds - if it's unknown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * CPUFREQ_ETERNAL shall be used. |
| 55 | */ |
| 56 | |
| 57 | struct cpufreq_governor; |
| 58 | |
| 59 | #define CPUFREQ_ETERNAL (-1) |
| 60 | struct cpufreq_cpuinfo { |
| 61 | unsigned int max_freq; |
| 62 | unsigned int min_freq; |
| 63 | unsigned int transition_latency; /* in 10^(-9) s = nanoseconds */ |
| 64 | }; |
| 65 | |
| 66 | struct cpufreq_real_policy { |
| 67 | unsigned int min; /* in kHz */ |
| 68 | unsigned int max; /* in kHz */ |
| 69 | unsigned int policy; /* see above */ |
| 70 | struct cpufreq_governor *governor; /* see below */ |
| 71 | }; |
| 72 | |
| 73 | struct cpufreq_policy { |
| 74 | cpumask_t cpus; /* affected CPUs */ |
Venkatesh Pallipadi | 3b2d994 | 2005-12-14 15:05:00 -0500 | [diff] [blame] | 75 | unsigned int shared_type; /* ANY or ALL affected CPUs |
| 76 | should set cpufreq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned int cpu; /* cpu nr of registered CPU */ |
| 78 | struct cpufreq_cpuinfo cpuinfo;/* see above */ |
| 79 | |
| 80 | unsigned int min; /* in kHz */ |
| 81 | unsigned int max; /* in kHz */ |
| 82 | unsigned int cur; /* in kHz, only needed if cpufreq |
| 83 | * governors are used */ |
| 84 | unsigned int policy; /* see above */ |
| 85 | struct cpufreq_governor *governor; /* see below */ |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct work_struct update; /* if update_policy() needs to be |
| 88 | * called, but you're in IRQ context */ |
| 89 | |
| 90 | struct cpufreq_real_policy user_policy; |
| 91 | |
| 92 | struct kobject kobj; |
| 93 | struct completion kobj_unregister; |
| 94 | }; |
| 95 | |
| 96 | #define CPUFREQ_ADJUST (0) |
| 97 | #define CPUFREQ_INCOMPATIBLE (1) |
| 98 | #define CPUFREQ_NOTIFY (2) |
| 99 | |
Venkatesh Pallipadi | 46f18e3 | 2006-06-26 00:34:43 -0400 | [diff] [blame] | 100 | #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */ |
| 101 | #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */ |
| 102 | #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */ |
| 103 | #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /******************** cpufreq transition notifiers *******************/ |
| 106 | |
| 107 | #define CPUFREQ_PRECHANGE (0) |
| 108 | #define CPUFREQ_POSTCHANGE (1) |
| 109 | #define CPUFREQ_RESUMECHANGE (8) |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 110 | #define CPUFREQ_SUSPENDCHANGE (9) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | struct cpufreq_freqs { |
| 113 | unsigned int cpu; /* cpu nr */ |
| 114 | unsigned int old; |
| 115 | unsigned int new; |
| 116 | u8 flags; /* flags of cpufreq_driver, see below. */ |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe) |
| 122 | * @old: old value |
| 123 | * @div: divisor |
| 124 | * @mult: multiplier |
| 125 | * |
| 126 | * |
| 127 | * new = old * mult / div |
| 128 | */ |
| 129 | static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult) |
| 130 | { |
| 131 | #if BITS_PER_LONG == 32 |
| 132 | |
| 133 | u64 result = ((u64) old) * ((u64) mult); |
| 134 | do_div(result, div); |
| 135 | return (unsigned long) result; |
| 136 | |
| 137 | #elif BITS_PER_LONG == 64 |
| 138 | |
| 139 | unsigned long result = old * ((u64) mult); |
| 140 | result /= div; |
| 141 | return result; |
| 142 | |
| 143 | #endif |
| 144 | }; |
| 145 | |
| 146 | /********************************************************************* |
| 147 | * CPUFREQ GOVERNORS * |
| 148 | *********************************************************************/ |
| 149 | |
| 150 | #define CPUFREQ_GOV_START 1 |
| 151 | #define CPUFREQ_GOV_STOP 2 |
| 152 | #define CPUFREQ_GOV_LIMITS 3 |
| 153 | |
| 154 | struct cpufreq_governor { |
| 155 | char name[CPUFREQ_NAME_LEN]; |
| 156 | int (*governor) (struct cpufreq_policy *policy, |
| 157 | unsigned int event); |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame^] | 158 | unsigned int max_transition_latency; /* HW must be able to switch to |
| 159 | next freq faster than this value in nano secs or we |
| 160 | will fallback to performance governor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | struct list_head governor_list; |
| 162 | struct module *owner; |
| 163 | }; |
| 164 | |
| 165 | /* pass a target to the cpufreq driver |
| 166 | */ |
| 167 | extern int cpufreq_driver_target(struct cpufreq_policy *policy, |
| 168 | unsigned int target_freq, |
| 169 | unsigned int relation); |
| 170 | extern int __cpufreq_driver_target(struct cpufreq_policy *policy, |
| 171 | unsigned int target_freq, |
| 172 | unsigned int relation); |
| 173 | |
| 174 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 175 | extern int __cpufreq_driver_getavg(struct cpufreq_policy *policy); |
Venkatesh Pallipadi | dfde5d6 | 2006-10-03 12:38:45 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | int cpufreq_register_governor(struct cpufreq_governor *governor); |
| 178 | void cpufreq_unregister_governor(struct cpufreq_governor *governor); |
| 179 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 180 | int lock_policy_rwsem_read(int cpu); |
| 181 | int lock_policy_rwsem_write(int cpu); |
| 182 | void unlock_policy_rwsem_read(int cpu); |
| 183 | void unlock_policy_rwsem_write(int cpu); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /********************************************************************* |
| 187 | * CPUFREQ DRIVER INTERFACE * |
| 188 | *********************************************************************/ |
| 189 | |
| 190 | #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */ |
| 191 | #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */ |
| 192 | |
| 193 | struct freq_attr; |
| 194 | |
| 195 | struct cpufreq_driver { |
| 196 | struct module *owner; |
| 197 | char name[CPUFREQ_NAME_LEN]; |
| 198 | u8 flags; |
| 199 | |
| 200 | /* needed by all drivers */ |
| 201 | int (*init) (struct cpufreq_policy *policy); |
| 202 | int (*verify) (struct cpufreq_policy *policy); |
| 203 | |
| 204 | /* define one out of two */ |
| 205 | int (*setpolicy) (struct cpufreq_policy *policy); |
| 206 | int (*target) (struct cpufreq_policy *policy, |
| 207 | unsigned int target_freq, |
| 208 | unsigned int relation); |
| 209 | |
| 210 | /* should be defined, if possible */ |
| 211 | unsigned int (*get) (unsigned int cpu); |
| 212 | |
| 213 | /* optional */ |
Venkatesh Pallipadi | dfde5d6 | 2006-10-03 12:38:45 -0700 | [diff] [blame] | 214 | unsigned int (*getavg) (unsigned int cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int (*exit) (struct cpufreq_policy *policy); |
Bernard Blackham | e00d996 | 2005-07-07 17:56:42 -0700 | [diff] [blame] | 216 | int (*suspend) (struct cpufreq_policy *policy, pm_message_t pmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int (*resume) (struct cpufreq_policy *policy); |
| 218 | struct freq_attr **attr; |
| 219 | }; |
| 220 | |
| 221 | /* flags */ |
| 222 | |
| 223 | #define CPUFREQ_STICKY 0x01 /* the driver isn't removed even if |
| 224 | * all ->init() calls failed */ |
| 225 | #define CPUFREQ_CONST_LOOPS 0x02 /* loops_per_jiffy or other kernel |
| 226 | * "constants" aren't affected by |
| 227 | * frequency transitions */ |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 228 | #define CPUFREQ_PM_NO_WARN 0x04 /* don't warn on suspend/resume speed |
| 229 | * mismatches */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 231 | int cpufreq_register_driver(struct cpufreq_driver *driver_data); |
| 232 | int cpufreq_unregister_driver(struct cpufreq_driver *driver_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | |
| 235 | void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state); |
| 236 | |
| 237 | |
| 238 | static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max) |
| 239 | { |
| 240 | if (policy->min < min) |
| 241 | policy->min = min; |
| 242 | if (policy->max < min) |
| 243 | policy->max = min; |
| 244 | if (policy->min > max) |
| 245 | policy->min = max; |
| 246 | if (policy->max > max) |
| 247 | policy->max = max; |
| 248 | if (policy->min > policy->max) |
| 249 | policy->min = policy->max; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | struct freq_attr { |
| 254 | struct attribute attr; |
| 255 | ssize_t (*show)(struct cpufreq_policy *, char *); |
| 256 | ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count); |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | /********************************************************************* |
| 261 | * CPUFREQ 2.6. INTERFACE * |
| 262 | *********************************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu); |
| 264 | int cpufreq_update_policy(unsigned int cpu); |
| 265 | |
Linus Torvalds | ff0ce68 | 2007-09-26 15:52:17 -0700 | [diff] [blame] | 266 | /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */ |
| 267 | unsigned int cpufreq_get(unsigned int cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Linus Torvalds | ff0ce68 | 2007-09-26 15:52:17 -0700 | [diff] [blame] | 269 | /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */ |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 270 | #ifdef CONFIG_CPU_FREQ |
| 271 | unsigned int cpufreq_quick_get(unsigned int cpu); |
| 272 | #else |
| 273 | static inline unsigned int cpufreq_quick_get(unsigned int cpu) |
| 274 | { |
| 275 | return 0; |
| 276 | } |
| 277 | #endif |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /********************************************************************* |
| 281 | * CPUFREQ DEFAULT GOVERNOR * |
| 282 | *********************************************************************/ |
| 283 | |
| 284 | |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame^] | 285 | /* |
| 286 | Performance governor is fallback governor if any other gov failed to |
| 287 | auto load due latency restrictions |
| 288 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | extern struct cpufreq_governor cpufreq_gov_performance; |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame^] | 290 | #define CPUFREQ_PERFORMANCE_GOVERNOR (&cpufreq_gov_performance) |
| 291 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE |
| 292 | #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_performance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE) |
| 294 | extern struct cpufreq_governor cpufreq_gov_userspace; |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame^] | 295 | #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_userspace) |
| 296 | #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND) |
| 297 | extern struct cpufreq_governor cpufreq_gov_ondemand; |
| 298 | #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_ondemand) |
| 299 | #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE) |
| 300 | extern struct cpufreq_governor cpufreq_gov_conservative; |
| 301 | #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_conservative) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | |
| 305 | /********************************************************************* |
| 306 | * FREQUENCY TABLE HELPERS * |
| 307 | *********************************************************************/ |
| 308 | |
| 309 | #define CPUFREQ_ENTRY_INVALID ~0 |
| 310 | #define CPUFREQ_TABLE_END ~1 |
| 311 | |
| 312 | struct cpufreq_frequency_table { |
| 313 | unsigned int index; /* any */ |
| 314 | unsigned int frequency; /* kHz - doesn't need to be in ascending |
| 315 | * order */ |
| 316 | }; |
| 317 | |
| 318 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
| 319 | struct cpufreq_frequency_table *table); |
| 320 | |
| 321 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, |
| 322 | struct cpufreq_frequency_table *table); |
| 323 | |
| 324 | int cpufreq_frequency_table_target(struct cpufreq_policy *policy, |
| 325 | struct cpufreq_frequency_table *table, |
| 326 | unsigned int target_freq, |
| 327 | unsigned int relation, |
| 328 | unsigned int *index); |
| 329 | |
| 330 | /* the following 3 funtions are for cpufreq core use only */ |
| 331 | struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu); |
| 332 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu); |
| 333 | void cpufreq_cpu_put (struct cpufreq_policy *data); |
| 334 | |
| 335 | /* the following are really really optional */ |
| 336 | extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs; |
| 337 | |
| 338 | void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table, |
| 339 | unsigned int cpu); |
| 340 | |
| 341 | void cpufreq_frequency_table_put_attr(unsigned int cpu); |
| 342 | |
| 343 | |
| 344 | /********************************************************************* |
| 345 | * UNIFIED DEBUG HELPERS * |
| 346 | *********************************************************************/ |
| 347 | |
| 348 | #define CPUFREQ_DEBUG_CORE 1 |
| 349 | #define CPUFREQ_DEBUG_DRIVER 2 |
| 350 | #define CPUFREQ_DEBUG_GOVERNOR 4 |
| 351 | |
| 352 | #ifdef CONFIG_CPU_FREQ_DEBUG |
| 353 | |
| 354 | extern void cpufreq_debug_printk(unsigned int type, const char *prefix, |
| 355 | const char *fmt, ...); |
| 356 | |
| 357 | #else |
| 358 | |
| 359 | #define cpufreq_debug_printk(msg...) do { } while(0) |
| 360 | |
| 361 | #endif /* CONFIG_CPU_FREQ_DEBUG */ |
| 362 | |
| 363 | #endif /* _LINUX_CPUFREQ_H */ |