blob: 5ad9a4e2bc591361793521ef1bf573264ec973c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Viresh Kumarbb176f72013-06-19 14:19:33 +05302 * linux/include/linux/cpufreq.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05304 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Thiago Farina335dc332011-04-28 20:42:53 -03006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/cpumask.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/completion.h>
16#include <linux/kobject.h>
17#include <linux/notifier.h>
18#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*********************************************************************
Viresh Kumar74aca952013-08-06 22:53:04 +053021 * CPUFREQ INTERFACE *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *********************************************************************/
Viresh Kumar74aca952013-08-06 22:53:04 +053023/*
24 * Frequency values here are CPU kHz
25 *
Dave Jonesb53cc6e2005-05-31 19:03:47 -070026 * Maximum transition latency is in nanoseconds - if it's unknown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * CPUFREQ_ETERNAL shall be used.
28 */
29
Viresh Kumar74aca952013-08-06 22:53:04 +053030#define CPUFREQ_ETERNAL (-1)
31#define CPUFREQ_NAME_LEN 16
32/* Print length for names. Extra 1 space for accomodating '\n' in prints */
33#define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1)
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct cpufreq_governor;
36
Viresh Kumar74aca952013-08-06 22:53:04 +053037struct cpufreq_freqs {
38 unsigned int cpu; /* cpu nr */
39 unsigned int old;
40 unsigned int new;
41 u8 flags; /* flags of cpufreq_driver, see below. */
42};
Thomas Renninger8aa84ad2009-07-24 15:25:05 +020043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct cpufreq_cpuinfo {
45 unsigned int max_freq;
46 unsigned int min_freq;
Thiago Farina335dc332011-04-28 20:42:53 -030047
48 /* in 10^(-9) s = nanoseconds */
49 unsigned int transition_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52struct cpufreq_real_policy {
53 unsigned int min; /* in kHz */
54 unsigned int max; /* in kHz */
Thiago Farina335dc332011-04-28 20:42:53 -030055 unsigned int policy; /* see above */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct cpufreq_governor *governor; /* see below */
57};
58
59struct cpufreq_policy {
Viresh Kumar951fc5f2013-01-31 02:03:53 +000060 /* CPUs sharing clock, require sw coordination */
61 cpumask_var_t cpus; /* Online CPUs only */
62 cpumask_var_t related_cpus; /* Online + Offline CPUs */
63
Viresh Kumar62b36cc2013-02-01 06:40:02 +000064 unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -050065 should set cpufreq */
Viresh Kumarb8eed8a2013-01-14 13:23:03 +000066 unsigned int cpu; /* cpu nr of CPU managing this policy */
67 unsigned int last_cpu; /* cpu nr of previous CPU that managed
68 * this policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct cpufreq_cpuinfo cpuinfo;/* see above */
70
71 unsigned int min; /* in kHz */
72 unsigned int max; /* in kHz */
73 unsigned int cur; /* in kHz, only needed if cpufreq
74 * governors are used */
Thiago Farina335dc332011-04-28 20:42:53 -030075 unsigned int policy; /* see above */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct cpufreq_governor *governor; /* see below */
Viresh Kumar7bd353a2013-03-27 15:58:57 +000077 void *governor_data;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +080078 bool governor_enabled; /* governor start/stop flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct work_struct update; /* if update_policy() needs to be
81 * called, but you're in IRQ context */
82
83 struct cpufreq_real_policy user_policy;
84
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053085 struct list_head policy_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct kobject kobj;
87 struct completion kobj_unregister;
88};
89
Viresh Kumar62b36cc2013-02-01 06:40:02 +000090/* Only for ACPI */
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -040091#define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
92#define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
93#define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
94#define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Daniel Vetterc75b5052013-10-08 10:56:11 +020096#ifdef CONFIG_CPU_FREQ
Viresh Kumar74aca952013-08-06 22:53:04 +053097struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +053098void cpufreq_cpu_put(struct cpufreq_policy *policy);
Daniel Vetterc75b5052013-10-08 10:56:11 +020099#else
100static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
101{
102 return NULL;
103}
104static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
105#endif
Viresh Kumar74aca952013-08-06 22:53:04 +0530106
Fabio Baltieri2624f902013-01-31 09:44:40 +0000107static inline bool policy_is_shared(struct cpufreq_policy *policy)
108{
109 return cpumask_weight(policy->cpus) > 1;
110}
111
Viresh Kumar74aca952013-08-06 22:53:04 +0530112/* /sys/devices/system/cpu/cpufreq: entry point for global variables */
113extern struct kobject *cpufreq_global_kobject;
114int cpufreq_get_global_kobject(void);
115void cpufreq_put_global_kobject(void);
116int cpufreq_sysfs_create_file(const struct attribute *attr);
117void cpufreq_sysfs_remove_file(const struct attribute *attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Viresh Kumar74aca952013-08-06 22:53:04 +0530119#ifdef CONFIG_CPU_FREQ
120unsigned int cpufreq_get(unsigned int cpu);
121unsigned int cpufreq_quick_get(unsigned int cpu);
122unsigned int cpufreq_quick_get_max(unsigned int cpu);
123void disable_cpufreq(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Viresh Kumar74aca952013-08-06 22:53:04 +0530125u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
126int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
127int cpufreq_update_policy(unsigned int cpu);
128bool have_governor_per_policy(void);
129struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
130#else
131static inline unsigned int cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Viresh Kumar74aca952013-08-06 22:53:04 +0530133 return 0;
134}
135static inline unsigned int cpufreq_quick_get(unsigned int cpu)
136{
137 return 0;
138}
139static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
140{
141 return 0;
142}
143static inline void disable_cpufreq(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*********************************************************************
147 * CPUFREQ DRIVER INTERFACE *
148 *********************************************************************/
149
150#define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
151#define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
152
Viresh Kumar74aca952013-08-06 22:53:04 +0530153struct freq_attr {
154 struct attribute attr;
155 ssize_t (*show)(struct cpufreq_policy *, char *);
156 ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
157};
158
159#define cpufreq_freq_attr_ro(_name) \
160static struct freq_attr _name = \
161__ATTR(_name, 0444, show_##_name, NULL)
162
163#define cpufreq_freq_attr_ro_perm(_name, _perm) \
164static struct freq_attr _name = \
165__ATTR(_name, _perm, show_##_name, NULL)
166
167#define cpufreq_freq_attr_rw(_name) \
168static struct freq_attr _name = \
169__ATTR(_name, 0644, show_##_name, store_##_name)
170
171struct global_attr {
172 struct attribute attr;
173 ssize_t (*show)(struct kobject *kobj,
174 struct attribute *attr, char *buf);
175 ssize_t (*store)(struct kobject *a, struct attribute *b,
176 const char *c, size_t count);
177};
178
179#define define_one_global_ro(_name) \
180static struct global_attr _name = \
181__ATTR(_name, 0444, show_##_name, NULL)
182
183#define define_one_global_rw(_name) \
184static struct global_attr _name = \
185__ATTR(_name, 0644, show_##_name, store_##_name)
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188struct cpufreq_driver {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 char name[CPUFREQ_NAME_LEN];
190 u8 flags;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000191 /*
192 * This should be set by platforms having multiple clock-domains, i.e.
193 * supporting multiple policies. With this sysfs directories of governor
194 * would be created in cpu/cpu<num>/cpufreq/ directory and so they can
195 * use the same governor with different tunables for different clusters.
196 */
197 bool have_governor_per_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* needed by all drivers */
200 int (*init) (struct cpufreq_policy *policy);
201 int (*verify) (struct cpufreq_policy *policy);
202
203 /* define one out of two */
204 int (*setpolicy) (struct cpufreq_policy *policy);
205 int (*target) (struct cpufreq_policy *policy,
206 unsigned int target_freq,
207 unsigned int relation);
208
209 /* should be defined, if possible */
210 unsigned int (*get) (unsigned int cpu);
211
212 /* optional */
Thomas Renningere2f74f32009-11-19 12:31:01 +0100213 int (*bios_limit) (int cpu, unsigned int *limit);
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int (*exit) (struct cpufreq_policy *policy);
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +0100216 int (*suspend) (struct cpufreq_policy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int (*resume) (struct cpufreq_policy *policy);
218 struct freq_attr **attr;
219};
220
221/* flags */
Thiago Farina335dc332011-04-28 20:42:53 -0300222#define CPUFREQ_STICKY 0x01 /* the driver isn't removed even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * all ->init() calls failed */
Thiago Farina335dc332011-04-28 20:42:53 -0300224#define CPUFREQ_CONST_LOOPS 0x02 /* loops_per_jiffy or other kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * "constants" aren't affected by
226 * frequency transitions */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700227#define CPUFREQ_PM_NO_WARN 0x04 /* don't warn on suspend/resume speed
228 * mismatches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds221dee22007-02-26 14:55:48 -0800230int cpufreq_register_driver(struct cpufreq_driver *driver_data);
231int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Viresh Kumar74aca952013-08-06 22:53:04 +0530233const char *cpufreq_get_current_driver(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Viresh Kumarbb176f72013-06-19 14:19:33 +0530235static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy,
236 unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 if (policy->min < min)
239 policy->min = min;
240 if (policy->max < min)
241 policy->max = min;
242 if (policy->min > max)
243 policy->min = max;
244 if (policy->max > max)
245 policy->max = max;
246 if (policy->min > policy->max)
247 policy->min = policy->max;
248 return;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*********************************************************************
Viresh Kumar74aca952013-08-06 22:53:04 +0530252 * CPUFREQ NOTIFIER INTERFACE *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *********************************************************************/
254
Viresh Kumar74aca952013-08-06 22:53:04 +0530255#define CPUFREQ_TRANSITION_NOTIFIER (0)
256#define CPUFREQ_POLICY_NOTIFIER (1)
257
258/* Transition notifiers */
259#define CPUFREQ_PRECHANGE (0)
260#define CPUFREQ_POSTCHANGE (1)
261#define CPUFREQ_RESUMECHANGE (8)
262#define CPUFREQ_SUSPENDCHANGE (9)
263
264/* Policy Notifiers */
265#define CPUFREQ_ADJUST (0)
266#define CPUFREQ_INCOMPATIBLE (1)
267#define CPUFREQ_NOTIFY (2)
268#define CPUFREQ_START (3)
269#define CPUFREQ_UPDATE_POLICY_CPU (4)
270
271#ifdef CONFIG_CPU_FREQ
272int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
273int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
274
275void cpufreq_notify_transition(struct cpufreq_policy *policy,
276 struct cpufreq_freqs *freqs, unsigned int state);
277
278#else /* CONFIG_CPU_FREQ */
279static inline int cpufreq_register_notifier(struct notifier_block *nb,
280 unsigned int list)
281{
282 return 0;
283}
284static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
285 unsigned int list)
286{
287 return 0;
288}
289#endif /* !CONFIG_CPU_FREQ */
290
291/**
292 * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
293 * safe)
294 * @old: old value
295 * @div: divisor
296 * @mult: multiplier
297 *
298 *
299 * new = old * mult / div
300 */
301static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
302 u_int mult)
303{
304#if BITS_PER_LONG == 32
305 u64 result = ((u64) old) * ((u64) mult);
306 do_div(result, div);
307 return (unsigned long) result;
308
309#elif BITS_PER_LONG == 64
310 unsigned long result = old * ((u64) mult);
311 result /= div;
312 return result;
313#endif
314}
315
316/*********************************************************************
317 * CPUFREQ GOVERNORS *
318 *********************************************************************/
319
320/*
321 * If (cpufreq_driver->target) exists, the ->governor decides what frequency
322 * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
323 * two generic policies are available:
324 */
325#define CPUFREQ_POLICY_POWERSAVE (1)
326#define CPUFREQ_POLICY_PERFORMANCE (2)
327
328/* Governor Events */
329#define CPUFREQ_GOV_START 1
330#define CPUFREQ_GOV_STOP 2
331#define CPUFREQ_GOV_LIMITS 3
332#define CPUFREQ_GOV_POLICY_INIT 4
333#define CPUFREQ_GOV_POLICY_EXIT 5
334
335struct cpufreq_governor {
336 char name[CPUFREQ_NAME_LEN];
337 int initialized;
338 int (*governor) (struct cpufreq_policy *policy,
339 unsigned int event);
340 ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
341 char *buf);
342 int (*store_setspeed) (struct cpufreq_policy *policy,
343 unsigned int freq);
344 unsigned int max_transition_latency; /* HW must be able to switch to
345 next freq faster than this value in nano secs or we
346 will fallback to performance governor */
347 struct list_head governor_list;
348 struct module *owner;
349};
350
351/* Pass a target to the cpufreq driver */
352int cpufreq_driver_target(struct cpufreq_policy *policy,
353 unsigned int target_freq,
354 unsigned int relation);
355int __cpufreq_driver_target(struct cpufreq_policy *policy,
356 unsigned int target_freq,
357 unsigned int relation);
358int cpufreq_register_governor(struct cpufreq_governor *governor);
359void cpufreq_unregister_governor(struct cpufreq_governor *governor);
360
361/* CPUFREQ DEFAULT GOVERNOR */
Thomas Renninger1c256242007-10-02 13:28:12 -0700362/*
Viresh Kumarbb176f72013-06-19 14:19:33 +0530363 * Performance governor is fallback governor if any other gov failed to auto
364 * load due latency restrictions
365 */
Thomas Renninger6afde102007-10-02 13:28:13 -0700366#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367extern struct cpufreq_governor cpufreq_gov_performance;
Thomas Renninger6afde102007-10-02 13:28:13 -0700368#endif
Thomas Renninger1c256242007-10-02 13:28:12 -0700369#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
370#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_performance)
Alessandro Guido30d221d2008-04-18 13:31:13 -0700371#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
372extern struct cpufreq_governor cpufreq_gov_powersave;
373#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_powersave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
375extern struct cpufreq_governor cpufreq_gov_userspace;
Thomas Renninger1c256242007-10-02 13:28:12 -0700376#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_userspace)
377#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
378extern struct cpufreq_governor cpufreq_gov_ondemand;
379#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_ondemand)
380#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
381extern struct cpufreq_governor cpufreq_gov_conservative;
382#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_conservative)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*********************************************************************
386 * FREQUENCY TABLE HELPERS *
387 *********************************************************************/
388
389#define CPUFREQ_ENTRY_INVALID ~0
390#define CPUFREQ_TABLE_END ~1
391
392struct cpufreq_frequency_table {
Viresh Kumar50701582013-03-30 16:25:15 +0530393 unsigned int driver_data; /* driver specific data, not used by core */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned int frequency; /* kHz - doesn't need to be in ascending
395 * order */
396};
397
398int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
399 struct cpufreq_frequency_table *table);
400
401int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
402 struct cpufreq_frequency_table *table);
403
404int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
405 struct cpufreq_frequency_table *table,
406 unsigned int target_freq,
407 unsigned int relation,
408 unsigned int *index);
409
Viresh Kumar74aca952013-08-06 22:53:04 +0530410void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
411ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
412
413/* the following funtion is for cpufreq core use only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/* the following are really really optional */
417extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
Thiago Farina335dc332011-04-28 20:42:53 -0300418void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420void cpufreq_frequency_table_put_attr(unsigned int cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#endif /* _LINUX_CPUFREQ_H */