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