]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
bb176f7d | 2 | * linux/include/linux/cpufreq.h |
1da177e4 | 3 | * |
bb176f7d VK |
4 | * Copyright (C) 2001 Russell King |
5 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> | |
335dc333 | 6 | * |
1da177e4 LT |
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. | |
10 | */ | |
11 | #ifndef _LINUX_CPUFREQ_H | |
12 | #define _LINUX_CPUFREQ_H | |
13 | ||
652ed95d | 14 | #include <linux/clk.h> |
5ff0a268 VK |
15 | #include <linux/cpumask.h> |
16 | #include <linux/completion.h> | |
1da177e4 | 17 | #include <linux/kobject.h> |
5ff0a268 | 18 | #include <linux/notifier.h> |
12478cf0 | 19 | #include <linux/spinlock.h> |
1da177e4 | 20 | #include <linux/sysfs.h> |
1da177e4 | 21 | |
1da177e4 | 22 | /********************************************************************* |
74aca95d | 23 | * CPUFREQ INTERFACE * |
1da177e4 | 24 | *********************************************************************/ |
74aca95d VK |
25 | /* |
26 | * Frequency values here are CPU kHz | |
27 | * | |
b53cc6ea | 28 | * Maximum transition latency is in nanoseconds - if it's unknown, |
1da177e4 LT |
29 | * CPUFREQ_ETERNAL shall be used. |
30 | */ | |
31 | ||
74aca95d VK |
32 | #define CPUFREQ_ETERNAL (-1) |
33 | #define CPUFREQ_NAME_LEN 16 | |
34 | /* Print length for names. Extra 1 space for accomodating '\n' in prints */ | |
35 | #define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1) | |
36 | ||
1da177e4 LT |
37 | struct cpufreq_governor; |
38 | ||
da0c6dc0 VK |
39 | enum cpufreq_table_sorting { |
40 | CPUFREQ_TABLE_UNSORTED, | |
41 | CPUFREQ_TABLE_SORTED_ASCENDING, | |
42 | CPUFREQ_TABLE_SORTED_DESCENDING | |
43 | }; | |
44 | ||
74aca95d VK |
45 | struct cpufreq_freqs { |
46 | unsigned int cpu; /* cpu nr */ | |
47 | unsigned int old; | |
48 | unsigned int new; | |
49 | u8 flags; /* flags of cpufreq_driver, see below. */ | |
50 | }; | |
8aa84ad8 | 51 | |
1da177e4 LT |
52 | struct cpufreq_cpuinfo { |
53 | unsigned int max_freq; | |
54 | unsigned int min_freq; | |
335dc333 TF |
55 | |
56 | /* in 10^(-9) s = nanoseconds */ | |
57 | unsigned int transition_latency; | |
1da177e4 LT |
58 | }; |
59 | ||
c7a7b418 | 60 | struct cpufreq_user_policy { |
1da177e4 LT |
61 | unsigned int min; /* in kHz */ |
62 | unsigned int max; /* in kHz */ | |
1da177e4 LT |
63 | }; |
64 | ||
65 | struct cpufreq_policy { | |
951fc5f4 VK |
66 | /* CPUs sharing clock, require sw coordination */ |
67 | cpumask_var_t cpus; /* Online CPUs only */ | |
68 | cpumask_var_t related_cpus; /* Online + Offline CPUs */ | |
559ed407 | 69 | cpumask_var_t real_cpus; /* Related and present */ |
951fc5f4 | 70 | |
62b36cc1 | 71 | unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs |
3b2d9942 | 72 | should set cpufreq */ |
9d16f207 | 73 | unsigned int cpu; /* cpu managing this policy, must be online */ |
9d16f207 | 74 | |
652ed95d | 75 | struct clk *clk; |
1da177e4 LT |
76 | struct cpufreq_cpuinfo cpuinfo;/* see above */ |
77 | ||
78 | unsigned int min; /* in kHz */ | |
79 | unsigned int max; /* in kHz */ | |
80 | unsigned int cur; /* in kHz, only needed if cpufreq | |
81 | * governors are used */ | |
1c03a2d0 | 82 | unsigned int restore_freq; /* = policy->cur before transition */ |
e28867ea VK |
83 | unsigned int suspend_freq; /* freq to set during suspend */ |
84 | ||
335dc333 | 85 | unsigned int policy; /* see above */ |
69030dd1 | 86 | unsigned int last_policy; /* policy before unplug */ |
1da177e4 | 87 | struct cpufreq_governor *governor; /* see below */ |
7bd353a9 | 88 | void *governor_data; |
4573237b | 89 | char last_governor[CPUFREQ_NAME_LEN]; /* last governor used */ |
1da177e4 | 90 | |
1da177e4 LT |
91 | struct work_struct update; /* if update_policy() needs to be |
92 | * called, but you're in IRQ context */ | |
93 | ||
c7a7b418 | 94 | struct cpufreq_user_policy user_policy; |
e0b3165b | 95 | struct cpufreq_frequency_table *freq_table; |
da0c6dc0 | 96 | enum cpufreq_table_sorting freq_table_sorted; |
1da177e4 | 97 | |
c88a1f8b | 98 | struct list_head policy_list; |
1da177e4 LT |
99 | struct kobject kobj; |
100 | struct completion kobj_unregister; | |
ad7722da | 101 | |
102 | /* | |
103 | * The rules for this semaphore: | |
104 | * - Any routine that wants to read from the policy structure will | |
105 | * do a down_read on this semaphore. | |
106 | * - Any routine that will write to the policy structure and/or may take away | |
107 | * the policy altogether (eg. CPU hotplug), will hold this lock in write | |
108 | * mode before doing so. | |
ad7722da | 109 | */ |
110 | struct rw_semaphore rwsem; | |
12478cf0 | 111 | |
b7898fda RW |
112 | /* |
113 | * Fast switch flags: | |
114 | * - fast_switch_possible should be set by the driver if it can | |
115 | * guarantee that frequency can be changed on any CPU sharing the | |
116 | * policy and that the change will affect all of the policy CPUs then. | |
117 | * - fast_switch_enabled is to be set by governors that support fast | |
118 | * freqnency switching with the help of cpufreq_enable_fast_switch(). | |
119 | */ | |
120 | bool fast_switch_possible; | |
121 | bool fast_switch_enabled; | |
122 | ||
12478cf0 SB |
123 | /* Synchronization for frequency transitions */ |
124 | bool transition_ongoing; /* Tracks transition status */ | |
125 | spinlock_t transition_lock; | |
126 | wait_queue_head_t transition_wait; | |
ca654dc3 | 127 | struct task_struct *transition_task; /* Task which is doing the transition */ |
413fffc3 | 128 | |
a9aaf291 VK |
129 | /* cpufreq-stats */ |
130 | struct cpufreq_stats *stats; | |
131 | ||
413fffc3 VK |
132 | /* For cpufreq driver's internal use */ |
133 | void *driver_data; | |
1da177e4 LT |
134 | }; |
135 | ||
62b36cc1 | 136 | /* Only for ACPI */ |
46f18e3a VP |
137 | #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */ |
138 | #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */ | |
139 | #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */ | |
140 | #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/ | |
1da177e4 | 141 | |
c75b505d | 142 | #ifdef CONFIG_CPU_FREQ |
1f0bd44e | 143 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu); |
74aca95d | 144 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu); |
3a3e9e06 | 145 | void cpufreq_cpu_put(struct cpufreq_policy *policy); |
c75b505d | 146 | #else |
1f0bd44e RW |
147 | static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
148 | { | |
149 | return NULL; | |
150 | } | |
c75b505d DV |
151 | static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
152 | { | |
153 | return NULL; | |
154 | } | |
155 | static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { } | |
156 | #endif | |
74aca95d | 157 | |
2624f90c FB |
158 | static inline bool policy_is_shared(struct cpufreq_policy *policy) |
159 | { | |
160 | return cpumask_weight(policy->cpus) > 1; | |
161 | } | |
162 | ||
74aca95d VK |
163 | /* /sys/devices/system/cpu/cpufreq: entry point for global variables */ |
164 | extern struct kobject *cpufreq_global_kobject; | |
1da177e4 | 165 | |
74aca95d VK |
166 | #ifdef CONFIG_CPU_FREQ |
167 | unsigned int cpufreq_get(unsigned int cpu); | |
168 | unsigned int cpufreq_quick_get(unsigned int cpu); | |
169 | unsigned int cpufreq_quick_get_max(unsigned int cpu); | |
170 | void disable_cpufreq(void); | |
1da177e4 | 171 | |
74aca95d VK |
172 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy); |
173 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu); | |
174 | int cpufreq_update_policy(unsigned int cpu); | |
175 | bool have_governor_per_policy(void); | |
176 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy); | |
b7898fda | 177 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy); |
6c9d9c81 | 178 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy); |
74aca95d VK |
179 | #else |
180 | static inline unsigned int cpufreq_get(unsigned int cpu) | |
1da177e4 | 181 | { |
74aca95d VK |
182 | return 0; |
183 | } | |
184 | static inline unsigned int cpufreq_quick_get(unsigned int cpu) | |
185 | { | |
186 | return 0; | |
187 | } | |
188 | static inline unsigned int cpufreq_quick_get_max(unsigned int cpu) | |
189 | { | |
190 | return 0; | |
191 | } | |
192 | static inline void disable_cpufreq(void) { } | |
1da177e4 | 193 | #endif |
1da177e4 | 194 | |
1aefc75b RW |
195 | #ifdef CONFIG_CPU_FREQ_STAT |
196 | void cpufreq_stats_create_table(struct cpufreq_policy *policy); | |
197 | void cpufreq_stats_free_table(struct cpufreq_policy *policy); | |
198 | void cpufreq_stats_record_transition(struct cpufreq_policy *policy, | |
199 | unsigned int new_freq); | |
200 | #else | |
201 | static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { } | |
202 | static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { } | |
203 | static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy, | |
204 | unsigned int new_freq) { } | |
205 | #endif /* CONFIG_CPU_FREQ_STAT */ | |
206 | ||
1da177e4 | 207 | /********************************************************************* |
74aca95d | 208 | * CPUFREQ DRIVER INTERFACE * |
1da177e4 LT |
209 | *********************************************************************/ |
210 | ||
74aca95d VK |
211 | #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */ |
212 | #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */ | |
5b0c0b16 | 213 | #define CPUFREQ_RELATION_C 2 /* closest frequency to target */ |
1da177e4 | 214 | |
74aca95d VK |
215 | struct freq_attr { |
216 | struct attribute attr; | |
217 | ssize_t (*show)(struct cpufreq_policy *, char *); | |
218 | ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count); | |
1da177e4 LT |
219 | }; |
220 | ||
74aca95d VK |
221 | #define cpufreq_freq_attr_ro(_name) \ |
222 | static struct freq_attr _name = \ | |
223 | __ATTR(_name, 0444, show_##_name, NULL) | |
1da177e4 | 224 | |
74aca95d VK |
225 | #define cpufreq_freq_attr_ro_perm(_name, _perm) \ |
226 | static struct freq_attr _name = \ | |
227 | __ATTR(_name, _perm, show_##_name, NULL) | |
1da177e4 | 228 | |
74aca95d VK |
229 | #define cpufreq_freq_attr_rw(_name) \ |
230 | static struct freq_attr _name = \ | |
231 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
232 | ||
233 | struct global_attr { | |
234 | struct attribute attr; | |
235 | ssize_t (*show)(struct kobject *kobj, | |
236 | struct attribute *attr, char *buf); | |
237 | ssize_t (*store)(struct kobject *a, struct attribute *b, | |
238 | const char *c, size_t count); | |
239 | }; | |
240 | ||
241 | #define define_one_global_ro(_name) \ | |
242 | static struct global_attr _name = \ | |
243 | __ATTR(_name, 0444, show_##_name, NULL) | |
244 | ||
245 | #define define_one_global_rw(_name) \ | |
246 | static struct global_attr _name = \ | |
247 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
1da177e4 | 248 | |
1da177e4 LT |
249 | |
250 | struct cpufreq_driver { | |
90452e61 VK |
251 | char name[CPUFREQ_NAME_LEN]; |
252 | u8 flags; | |
253 | void *driver_data; | |
1da177e4 LT |
254 | |
255 | /* needed by all drivers */ | |
90452e61 VK |
256 | int (*init)(struct cpufreq_policy *policy); |
257 | int (*verify)(struct cpufreq_policy *policy); | |
1da177e4 LT |
258 | |
259 | /* define one out of two */ | |
90452e61 | 260 | int (*setpolicy)(struct cpufreq_policy *policy); |
1c03a2d0 VK |
261 | |
262 | /* | |
263 | * On failure, should always restore frequency to policy->restore_freq | |
264 | * (i.e. old freq). | |
265 | */ | |
90452e61 VK |
266 | int (*target)(struct cpufreq_policy *policy, |
267 | unsigned int target_freq, | |
268 | unsigned int relation); /* Deprecated */ | |
269 | int (*target_index)(struct cpufreq_policy *policy, | |
270 | unsigned int index); | |
b7898fda RW |
271 | unsigned int (*fast_switch)(struct cpufreq_policy *policy, |
272 | unsigned int target_freq); | |
1c03a2d0 VK |
273 | /* |
274 | * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION | |
275 | * unset. | |
276 | * | |
277 | * get_intermediate should return a stable intermediate frequency | |
278 | * platform wants to switch to and target_intermediate() should set CPU | |
279 | * to to that frequency, before jumping to the frequency corresponding | |
280 | * to 'index'. Core will take care of sending notifications and driver | |
281 | * doesn't have to handle them in target_intermediate() or | |
282 | * target_index(). | |
283 | * | |
284 | * Drivers can return '0' from get_intermediate() in case they don't | |
285 | * wish to switch to intermediate frequency for some target frequency. | |
286 | * In that case core will directly call ->target_index(). | |
287 | */ | |
90452e61 VK |
288 | unsigned int (*get_intermediate)(struct cpufreq_policy *policy, |
289 | unsigned int index); | |
290 | int (*target_intermediate)(struct cpufreq_policy *policy, | |
291 | unsigned int index); | |
1da177e4 LT |
292 | |
293 | /* should be defined, if possible */ | |
90452e61 | 294 | unsigned int (*get)(unsigned int cpu); |
1da177e4 LT |
295 | |
296 | /* optional */ | |
90452e61 | 297 | int (*bios_limit)(int cpu, unsigned int *limit); |
bf0b90e3 | 298 | |
90452e61 VK |
299 | int (*exit)(struct cpufreq_policy *policy); |
300 | void (*stop_cpu)(struct cpufreq_policy *policy); | |
301 | int (*suspend)(struct cpufreq_policy *policy); | |
302 | int (*resume)(struct cpufreq_policy *policy); | |
7c45cf31 VK |
303 | |
304 | /* Will be called after the driver is fully initialized */ | |
305 | void (*ready)(struct cpufreq_policy *policy); | |
306 | ||
90452e61 | 307 | struct freq_attr **attr; |
6f19efc0 LM |
308 | |
309 | /* platform specific boost support code */ | |
90452e61 VK |
310 | bool boost_enabled; |
311 | int (*set_boost)(int state); | |
1da177e4 LT |
312 | }; |
313 | ||
314 | /* flags */ | |
6461f018 VK |
315 | #define CPUFREQ_STICKY (1 << 0) /* driver isn't removed even if |
316 | all ->init() calls failed */ | |
317 | #define CPUFREQ_CONST_LOOPS (1 << 1) /* loops_per_jiffy or other | |
318 | kernel "constants" aren't | |
319 | affected by frequency | |
320 | transitions */ | |
321 | #define CPUFREQ_PM_NO_WARN (1 << 2) /* don't warn on suspend/resume | |
322 | speed mismatches */ | |
1da177e4 | 323 | |
0b981e70 VK |
324 | /* |
325 | * This should be set by platforms having multiple clock-domains, i.e. | |
326 | * supporting multiple policies. With this sysfs directories of governor would | |
327 | * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same | |
328 | * governor with different tunables for different clusters. | |
329 | */ | |
330 | #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY (1 << 3) | |
331 | ||
7dbf694d VK |
332 | /* |
333 | * Driver will do POSTCHANGE notifications from outside of their ->target() | |
334 | * routine and so must set cpufreq_driver->flags with this flag, so that core | |
335 | * can handle them specially. | |
336 | */ | |
337 | #define CPUFREQ_ASYNC_NOTIFICATION (1 << 4) | |
338 | ||
ae6b4271 VK |
339 | /* |
340 | * Set by drivers which want cpufreq core to check if CPU is running at a | |
341 | * frequency present in freq-table exposed by the driver. For these drivers if | |
342 | * CPU is found running at an out of table freq, we will try to set it to a freq | |
343 | * from the table. And if that fails, we will stop further boot process by | |
344 | * issuing a BUG_ON(). | |
345 | */ | |
346 | #define CPUFREQ_NEED_INITIAL_FREQ_CHECK (1 << 5) | |
347 | ||
221dee28 LT |
348 | int cpufreq_register_driver(struct cpufreq_driver *driver_data); |
349 | int cpufreq_unregister_driver(struct cpufreq_driver *driver_data); | |
1da177e4 | 350 | |
74aca95d | 351 | const char *cpufreq_get_current_driver(void); |
51315cdf | 352 | void *cpufreq_get_driver_data(void); |
1da177e4 | 353 | |
bb176f7d VK |
354 | static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, |
355 | unsigned int min, unsigned int max) | |
1da177e4 LT |
356 | { |
357 | if (policy->min < min) | |
358 | policy->min = min; | |
359 | if (policy->max < min) | |
360 | policy->max = min; | |
361 | if (policy->min > max) | |
362 | policy->min = max; | |
363 | if (policy->max > max) | |
364 | policy->max = max; | |
365 | if (policy->min > policy->max) | |
366 | policy->min = policy->max; | |
367 | return; | |
368 | } | |
369 | ||
be49e346 VK |
370 | static inline void |
371 | cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) | |
372 | { | |
373 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, | |
374 | policy->cpuinfo.max_freq); | |
375 | } | |
376 | ||
2f0aea93 VK |
377 | #ifdef CONFIG_CPU_FREQ |
378 | void cpufreq_suspend(void); | |
379 | void cpufreq_resume(void); | |
e28867ea | 380 | int cpufreq_generic_suspend(struct cpufreq_policy *policy); |
2f0aea93 VK |
381 | #else |
382 | static inline void cpufreq_suspend(void) {} | |
383 | static inline void cpufreq_resume(void) {} | |
384 | #endif | |
385 | ||
74aca95d VK |
386 | /********************************************************************* |
387 | * CPUFREQ NOTIFIER INTERFACE * | |
388 | *********************************************************************/ | |
6dad2a29 | 389 | |
74aca95d VK |
390 | #define CPUFREQ_TRANSITION_NOTIFIER (0) |
391 | #define CPUFREQ_POLICY_NOTIFIER (1) | |
1da177e4 | 392 | |
74aca95d VK |
393 | /* Transition notifiers */ |
394 | #define CPUFREQ_PRECHANGE (0) | |
395 | #define CPUFREQ_POSTCHANGE (1) | |
6dad2a29 | 396 | |
74aca95d VK |
397 | /* Policy Notifiers */ |
398 | #define CPUFREQ_ADJUST (0) | |
6bfb7c74 VK |
399 | #define CPUFREQ_NOTIFY (1) |
400 | #define CPUFREQ_START (2) | |
401 | #define CPUFREQ_CREATE_POLICY (3) | |
402 | #define CPUFREQ_REMOVE_POLICY (4) | |
6dad2a29 | 403 | |
74aca95d VK |
404 | #ifdef CONFIG_CPU_FREQ |
405 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list); | |
406 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list); | |
6dad2a29 | 407 | |
12478cf0 SB |
408 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
409 | struct cpufreq_freqs *freqs); | |
410 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, | |
411 | struct cpufreq_freqs *freqs, int transition_failed); | |
1da177e4 | 412 | |
74aca95d VK |
413 | #else /* CONFIG_CPU_FREQ */ |
414 | static inline int cpufreq_register_notifier(struct notifier_block *nb, | |
415 | unsigned int list) | |
2eca40a8 RD |
416 | { |
417 | return 0; | |
418 | } | |
74aca95d VK |
419 | static inline int cpufreq_unregister_notifier(struct notifier_block *nb, |
420 | unsigned int list) | |
95235ca2 VP |
421 | { |
422 | return 0; | |
423 | } | |
74aca95d VK |
424 | #endif /* !CONFIG_CPU_FREQ */ |
425 | ||
426 | /** | |
427 | * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch | |
428 | * safe) | |
429 | * @old: old value | |
430 | * @div: divisor | |
431 | * @mult: multiplier | |
432 | * | |
433 | * | |
434 | * new = old * mult / div | |
435 | */ | |
436 | static inline unsigned long cpufreq_scale(unsigned long old, u_int div, | |
437 | u_int mult) | |
3d737108 | 438 | { |
74aca95d VK |
439 | #if BITS_PER_LONG == 32 |
440 | u64 result = ((u64) old) * ((u64) mult); | |
441 | do_div(result, div); | |
442 | return (unsigned long) result; | |
443 | ||
444 | #elif BITS_PER_LONG == 64 | |
445 | unsigned long result = old * ((u64) mult); | |
446 | result /= div; | |
447 | return result; | |
95235ca2 | 448 | #endif |
74aca95d | 449 | } |
95235ca2 | 450 | |
1da177e4 | 451 | /********************************************************************* |
74aca95d | 452 | * CPUFREQ GOVERNORS * |
1da177e4 LT |
453 | *********************************************************************/ |
454 | ||
74aca95d VK |
455 | /* |
456 | * If (cpufreq_driver->target) exists, the ->governor decides what frequency | |
457 | * within the limits is used. If (cpufreq_driver->setpolicy> exists, these | |
458 | * two generic policies are available: | |
459 | */ | |
460 | #define CPUFREQ_POLICY_POWERSAVE (1) | |
461 | #define CPUFREQ_POLICY_PERFORMANCE (2) | |
462 | ||
379480d8 RW |
463 | /* |
464 | * The polling frequency depends on the capability of the processor. Default | |
465 | * polling frequency is 1000 times the transition latency of the processor. The | |
466 | * ondemand governor will work on any processor with transition latency <= 10ms, | |
467 | * using appropriate sampling rate. | |
468 | * | |
469 | * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL) | |
470 | * the ondemand governor will not work. All times here are in us (microseconds). | |
471 | */ | |
472 | #define MIN_SAMPLING_RATE_RATIO (2) | |
473 | #define LATENCY_MULTIPLIER (1000) | |
474 | #define MIN_LATENCY_MULTIPLIER (20) | |
475 | #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) | |
476 | ||
74aca95d VK |
477 | struct cpufreq_governor { |
478 | char name[CPUFREQ_NAME_LEN]; | |
e788892b RW |
479 | int (*init)(struct cpufreq_policy *policy); |
480 | void (*exit)(struct cpufreq_policy *policy); | |
481 | int (*start)(struct cpufreq_policy *policy); | |
482 | void (*stop)(struct cpufreq_policy *policy); | |
483 | void (*limits)(struct cpufreq_policy *policy); | |
74aca95d VK |
484 | ssize_t (*show_setspeed) (struct cpufreq_policy *policy, |
485 | char *buf); | |
486 | int (*store_setspeed) (struct cpufreq_policy *policy, | |
487 | unsigned int freq); | |
488 | unsigned int max_transition_latency; /* HW must be able to switch to | |
489 | next freq faster than this value in nano secs or we | |
490 | will fallback to performance governor */ | |
491 | struct list_head governor_list; | |
492 | struct module *owner; | |
493 | }; | |
494 | ||
495 | /* Pass a target to the cpufreq driver */ | |
b7898fda RW |
496 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, |
497 | unsigned int target_freq); | |
74aca95d VK |
498 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
499 | unsigned int target_freq, | |
500 | unsigned int relation); | |
501 | int __cpufreq_driver_target(struct cpufreq_policy *policy, | |
502 | unsigned int target_freq, | |
503 | unsigned int relation); | |
504 | int cpufreq_register_governor(struct cpufreq_governor *governor); | |
505 | void cpufreq_unregister_governor(struct cpufreq_governor *governor); | |
506 | ||
de1df26b RW |
507 | struct cpufreq_governor *cpufreq_default_governor(void); |
508 | struct cpufreq_governor *cpufreq_fallback_governor(void); | |
1da177e4 | 509 | |
bf2be2de VK |
510 | static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy) |
511 | { | |
512 | if (policy->max < policy->cur) | |
513 | __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H); | |
514 | else if (policy->min > policy->cur) | |
515 | __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L); | |
516 | } | |
517 | ||
66893b6a RW |
518 | /* Governor attribute set */ |
519 | struct gov_attr_set { | |
520 | struct kobject kobj; | |
521 | struct list_head policy_list; | |
522 | struct mutex update_lock; | |
523 | int usage_count; | |
524 | }; | |
525 | ||
526 | /* sysfs ops for cpufreq governors */ | |
527 | extern const struct sysfs_ops governor_sysfs_ops; | |
528 | ||
529 | void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node); | |
530 | void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node); | |
531 | unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node); | |
532 | ||
533 | /* Governor sysfs attribute */ | |
534 | struct governor_attr { | |
535 | struct attribute attr; | |
536 | ssize_t (*show)(struct gov_attr_set *attr_set, char *buf); | |
537 | ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf, | |
538 | size_t count); | |
539 | }; | |
540 | ||
1da177e4 LT |
541 | /********************************************************************* |
542 | * FREQUENCY TABLE HELPERS * | |
543 | *********************************************************************/ | |
544 | ||
7f4b0461 | 545 | /* Special Values of .frequency field */ |
2b1987a9 BH |
546 | #define CPUFREQ_ENTRY_INVALID ~0u |
547 | #define CPUFREQ_TABLE_END ~1u | |
7f4b0461 VK |
548 | /* Special Values of .flags field */ |
549 | #define CPUFREQ_BOOST_FREQ (1 << 0) | |
1da177e4 LT |
550 | |
551 | struct cpufreq_frequency_table { | |
7f4b0461 | 552 | unsigned int flags; |
50701588 | 553 | unsigned int driver_data; /* driver specific data, not used by core */ |
1da177e4 LT |
554 | unsigned int frequency; /* kHz - doesn't need to be in ascending |
555 | * order */ | |
556 | }; | |
557 | ||
a0dd7b79 NM |
558 | #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP) |
559 | int dev_pm_opp_init_cpufreq_table(struct device *dev, | |
560 | struct cpufreq_frequency_table **table); | |
561 | void dev_pm_opp_free_cpufreq_table(struct device *dev, | |
562 | struct cpufreq_frequency_table **table); | |
563 | #else | |
564 | static inline int dev_pm_opp_init_cpufreq_table(struct device *dev, | |
565 | struct cpufreq_frequency_table | |
566 | **table) | |
567 | { | |
568 | return -EINVAL; | |
569 | } | |
570 | ||
571 | static inline void dev_pm_opp_free_cpufreq_table(struct device *dev, | |
572 | struct cpufreq_frequency_table | |
573 | **table) | |
574 | { | |
575 | } | |
576 | #endif | |
577 | ||
27e289dc SK |
578 | /* |
579 | * cpufreq_for_each_entry - iterate over a cpufreq_frequency_table | |
580 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. | |
581 | * @table: the cpufreq_frequency_table * to iterate over. | |
582 | */ | |
583 | ||
584 | #define cpufreq_for_each_entry(pos, table) \ | |
585 | for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) | |
586 | ||
587 | /* | |
588 | * cpufreq_for_each_valid_entry - iterate over a cpufreq_frequency_table | |
589 | * excluding CPUFREQ_ENTRY_INVALID frequencies. | |
590 | * @pos: the cpufreq_frequency_table * to use as a loop cursor. | |
591 | * @table: the cpufreq_frequency_table * to iterate over. | |
592 | */ | |
593 | ||
34b08705 RW |
594 | #define cpufreq_for_each_valid_entry(pos, table) \ |
595 | for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) \ | |
596 | if (pos->frequency == CPUFREQ_ENTRY_INVALID) \ | |
597 | continue; \ | |
598 | else | |
27e289dc | 599 | |
1da177e4 LT |
600 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
601 | struct cpufreq_frequency_table *table); | |
602 | ||
603 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, | |
604 | struct cpufreq_frequency_table *table); | |
18434512 | 605 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy); |
1da177e4 | 606 | |
da0c6dc0 VK |
607 | int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, |
608 | unsigned int target_freq, | |
609 | unsigned int relation); | |
d3916691 VK |
610 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
611 | unsigned int freq); | |
1da177e4 | 612 | |
74aca95d VK |
613 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf); |
614 | ||
6f19efc0 LM |
615 | #ifdef CONFIG_CPU_FREQ |
616 | int cpufreq_boost_trigger_state(int state); | |
6f19efc0 | 617 | int cpufreq_boost_enabled(void); |
44139ed4 VK |
618 | int cpufreq_enable_boost_support(void); |
619 | bool policy_has_boost_freq(struct cpufreq_policy *policy); | |
da0c6dc0 VK |
620 | |
621 | /* Find lowest freq at or above target in a table in ascending order */ | |
622 | static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy, | |
623 | unsigned int target_freq) | |
624 | { | |
625 | struct cpufreq_frequency_table *table = policy->freq_table; | |
626 | unsigned int freq; | |
627 | int i, best = -1; | |
628 | ||
629 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
630 | freq = table[i].frequency; | |
631 | ||
632 | if (freq >= target_freq) | |
633 | return i; | |
634 | ||
635 | best = i; | |
636 | } | |
637 | ||
638 | return best; | |
639 | } | |
640 | ||
641 | /* Find lowest freq at or above target in a table in descending order */ | |
642 | static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy, | |
643 | unsigned int target_freq) | |
644 | { | |
645 | struct cpufreq_frequency_table *table = policy->freq_table; | |
646 | unsigned int freq; | |
647 | int i, best = -1; | |
648 | ||
649 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
650 | freq = table[i].frequency; | |
651 | ||
652 | if (freq == target_freq) | |
653 | return i; | |
654 | ||
655 | if (freq > target_freq) { | |
656 | best = i; | |
657 | continue; | |
658 | } | |
659 | ||
660 | /* No freq found above target_freq */ | |
661 | if (best == -1) | |
662 | return i; | |
663 | ||
664 | return best; | |
665 | } | |
666 | ||
667 | return best; | |
668 | } | |
669 | ||
670 | /* Works only on sorted freq-tables */ | |
671 | static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy, | |
672 | unsigned int target_freq) | |
673 | { | |
674 | target_freq = clamp_val(target_freq, policy->min, policy->max); | |
675 | ||
676 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) | |
677 | return cpufreq_table_find_index_al(policy, target_freq); | |
678 | else | |
679 | return cpufreq_table_find_index_dl(policy, target_freq); | |
680 | } | |
681 | ||
682 | /* Find highest freq at or below target in a table in ascending order */ | |
683 | static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy, | |
684 | unsigned int target_freq) | |
685 | { | |
686 | struct cpufreq_frequency_table *table = policy->freq_table; | |
687 | unsigned int freq; | |
688 | int i, best = -1; | |
689 | ||
690 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
691 | freq = table[i].frequency; | |
692 | ||
693 | if (freq == target_freq) | |
694 | return i; | |
695 | ||
696 | if (freq < target_freq) { | |
697 | best = i; | |
698 | continue; | |
699 | } | |
700 | ||
701 | /* No freq found below target_freq */ | |
702 | if (best == -1) | |
703 | return i; | |
704 | ||
705 | return best; | |
706 | } | |
707 | ||
708 | return best; | |
709 | } | |
710 | ||
711 | /* Find highest freq at or below target in a table in descending order */ | |
712 | static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy, | |
713 | unsigned int target_freq) | |
714 | { | |
715 | struct cpufreq_frequency_table *table = policy->freq_table; | |
716 | unsigned int freq; | |
717 | int i, best = -1; | |
718 | ||
719 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
720 | freq = table[i].frequency; | |
721 | ||
722 | if (freq <= target_freq) | |
723 | return i; | |
724 | ||
725 | best = i; | |
726 | } | |
727 | ||
728 | return best; | |
729 | } | |
730 | ||
731 | /* Works only on sorted freq-tables */ | |
732 | static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy, | |
733 | unsigned int target_freq) | |
734 | { | |
735 | target_freq = clamp_val(target_freq, policy->min, policy->max); | |
736 | ||
737 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) | |
738 | return cpufreq_table_find_index_ah(policy, target_freq); | |
739 | else | |
740 | return cpufreq_table_find_index_dh(policy, target_freq); | |
741 | } | |
742 | ||
743 | /* Find closest freq to target in a table in ascending order */ | |
744 | static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy, | |
745 | unsigned int target_freq) | |
746 | { | |
747 | struct cpufreq_frequency_table *table = policy->freq_table; | |
748 | unsigned int freq; | |
749 | int i, best = -1; | |
750 | ||
751 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
752 | freq = table[i].frequency; | |
753 | ||
754 | if (freq == target_freq) | |
755 | return i; | |
756 | ||
757 | if (freq < target_freq) { | |
758 | best = i; | |
759 | continue; | |
760 | } | |
761 | ||
762 | /* No freq found below target_freq */ | |
763 | if (best == -1) | |
764 | return i; | |
765 | ||
766 | /* Choose the closest freq */ | |
767 | if (target_freq - table[best].frequency > freq - target_freq) | |
768 | return i; | |
769 | ||
770 | return best; | |
771 | } | |
772 | ||
773 | return best; | |
774 | } | |
775 | ||
776 | /* Find closest freq to target in a table in descending order */ | |
777 | static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy, | |
778 | unsigned int target_freq) | |
779 | { | |
780 | struct cpufreq_frequency_table *table = policy->freq_table; | |
781 | unsigned int freq; | |
782 | int i, best = -1; | |
783 | ||
784 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
785 | freq = table[i].frequency; | |
786 | ||
787 | if (freq == target_freq) | |
788 | return i; | |
789 | ||
790 | if (freq > target_freq) { | |
791 | best = i; | |
792 | continue; | |
793 | } | |
794 | ||
795 | /* No freq found above target_freq */ | |
796 | if (best == -1) | |
797 | return i; | |
798 | ||
799 | /* Choose the closest freq */ | |
800 | if (table[best].frequency - target_freq > target_freq - freq) | |
801 | return i; | |
802 | ||
803 | return best; | |
804 | } | |
805 | ||
806 | return best; | |
807 | } | |
808 | ||
809 | /* Works only on sorted freq-tables */ | |
810 | static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy, | |
811 | unsigned int target_freq) | |
812 | { | |
813 | target_freq = clamp_val(target_freq, policy->min, policy->max); | |
814 | ||
815 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) | |
816 | return cpufreq_table_find_index_ac(policy, target_freq); | |
817 | else | |
818 | return cpufreq_table_find_index_dc(policy, target_freq); | |
819 | } | |
820 | ||
821 | static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy, | |
822 | unsigned int target_freq, | |
823 | unsigned int relation) | |
824 | { | |
825 | if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED)) | |
826 | return cpufreq_table_index_unsorted(policy, target_freq, | |
827 | relation); | |
828 | ||
829 | switch (relation) { | |
830 | case CPUFREQ_RELATION_L: | |
831 | return cpufreq_table_find_index_l(policy, target_freq); | |
832 | case CPUFREQ_RELATION_H: | |
833 | return cpufreq_table_find_index_h(policy, target_freq); | |
834 | case CPUFREQ_RELATION_C: | |
835 | return cpufreq_table_find_index_c(policy, target_freq); | |
836 | default: | |
837 | pr_err("%s: Invalid relation: %d\n", __func__, relation); | |
838 | return -EINVAL; | |
839 | } | |
840 | } | |
6f19efc0 LM |
841 | #else |
842 | static inline int cpufreq_boost_trigger_state(int state) | |
843 | { | |
844 | return 0; | |
845 | } | |
6f19efc0 LM |
846 | static inline int cpufreq_boost_enabled(void) |
847 | { | |
848 | return 0; | |
849 | } | |
44139ed4 VK |
850 | |
851 | static inline int cpufreq_enable_boost_support(void) | |
852 | { | |
853 | return -EINVAL; | |
854 | } | |
855 | ||
856 | static inline bool policy_has_boost_freq(struct cpufreq_policy *policy) | |
857 | { | |
858 | return false; | |
859 | } | |
6f19efc0 | 860 | #endif |
1da177e4 LT |
861 | |
862 | /* the following are really really optional */ | |
863 | extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs; | |
21c36d35 | 864 | extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs; |
18434512 | 865 | extern struct freq_attr *cpufreq_generic_attr[]; |
27047a60 VK |
866 | int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, |
867 | struct cpufreq_frequency_table *table); | |
f4fd3797 | 868 | |
652ed95d | 869 | unsigned int cpufreq_generic_get(unsigned int cpu); |
70e9e778 VK |
870 | int cpufreq_generic_init(struct cpufreq_policy *policy, |
871 | struct cpufreq_frequency_table *table, | |
872 | unsigned int transition_latency); | |
1da177e4 | 873 | #endif /* _LINUX_CPUFREQ_H */ |