blob: 7408af843b8ac891b1fd59557c5cb7faa117c0bd [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * cpuidle.h - a generic framework for CPU idle power management
3 *
4 * (C) 2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
7 *
8 * This code is licenced under the GPL.
9 */
10
11#ifndef _LINUX_CPUIDLE_H
12#define _LINUX_CPUIDLE_H
13
14#include <linux/percpu.h>
15#include <linux/list.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016#include <linux/kobject.h>
17#include <linux/completion.h>
18
19#define CPUIDLE_STATE_MAX 8
20#define CPUIDLE_NAME_LEN 16
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080021#define CPUIDLE_DESC_LEN 32
Len Brown4f86d3a2007-10-03 18:58:00 -040022
Paul Gortmakerde477252011-05-26 13:46:22 -040023struct module;
24
Len Brown4f86d3a2007-10-03 18:58:00 -040025struct cpuidle_device;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053026struct cpuidle_driver;
Len Brown4f86d3a2007-10-03 18:58:00 -040027
28
29/****************************
30 * CPUIDLE DEVICE INTERFACE *
31 ****************************/
32
Deepthi Dharwar42027352011-10-28 16:20:33 +053033struct cpuidle_state_usage {
34 void *driver_data;
35
36 unsigned long long usage;
37 unsigned long long time; /* in US */
38};
39
Len Brown4f86d3a2007-10-03 18:58:00 -040040struct cpuidle_state {
41 char name[CPUIDLE_NAME_LEN];
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080042 char desc[CPUIDLE_DESC_LEN];
Len Brown4f86d3a2007-10-03 18:58:00 -040043
44 unsigned int flags;
45 unsigned int exit_latency; /* in US */
46 unsigned int power_usage; /* in mW */
47 unsigned int target_residency; /* in US */
48
Len Brown4f86d3a2007-10-03 18:58:00 -040049 int (*enter) (struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053050 struct cpuidle_driver *drv,
Deepthi Dharware978aa72011-10-28 16:20:09 +053051 int index);
Len Brown4f86d3a2007-10-03 18:58:00 -040052};
53
54/* Idle State Flags */
55#define CPUIDLE_FLAG_TIME_VALID (0x01) /* is residency time measurable? */
Len Brown4f86d3a2007-10-03 18:58:00 -040056
57#define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
58
59/**
60 * cpuidle_get_statedata - retrieves private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053061 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040062 */
Deepthi Dharwar42027352011-10-28 16:20:33 +053063static inline void *cpuidle_get_statedata(struct cpuidle_state_usage *st_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -040064{
Deepthi Dharwar42027352011-10-28 16:20:33 +053065 return st_usage->driver_data;
Len Brown4f86d3a2007-10-03 18:58:00 -040066}
67
68/**
69 * cpuidle_set_statedata - stores private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053070 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040071 * @data: the private data
72 */
73static inline void
Deepthi Dharwar42027352011-10-28 16:20:33 +053074cpuidle_set_statedata(struct cpuidle_state_usage *st_usage, void *data)
Len Brown4f86d3a2007-10-03 18:58:00 -040075{
Deepthi Dharwar42027352011-10-28 16:20:33 +053076 st_usage->driver_data = data;
Len Brown4f86d3a2007-10-03 18:58:00 -040077}
78
79struct cpuidle_state_kobj {
80 struct cpuidle_state *state;
Deepthi Dharwar42027352011-10-28 16:20:33 +053081 struct cpuidle_state_usage *state_usage;
Len Brown4f86d3a2007-10-03 18:58:00 -040082 struct completion kobj_unregister;
83 struct kobject kobj;
84};
85
86struct cpuidle_device {
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040087 unsigned int registered:1;
Harvey Harrisonb5556a62008-02-06 22:39:44 +010088 unsigned int enabled:1;
Len Brown4f86d3a2007-10-03 18:58:00 -040089 unsigned int cpu;
90
91 int last_residency;
92 int state_count;
Deepthi Dharwar42027352011-10-28 16:20:33 +053093 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040094 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040095
96 struct list_head device_list;
97 struct kobject kobj;
98 struct completion kobj_unregister;
99 void *governor_data;
Len Brown4f86d3a2007-10-03 18:58:00 -0400100};
101
102DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
103
104/**
105 * cpuidle_get_last_residency - retrieves the last state's residency time
106 * @dev: the target CPU
107 *
108 * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
109 */
110static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
111{
112 return dev->last_residency;
113}
114
115
116/****************************
117 * CPUIDLE DRIVER INTERFACE *
118 ****************************/
119
120struct cpuidle_driver {
121 char name[CPUIDLE_NAME_LEN];
122 struct module *owner;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530123
124 unsigned int power_specified:1;
125 struct cpuidle_state states[CPUIDLE_STATE_MAX];
126 int state_count;
127 int safe_state_index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400128};
129
130#ifdef CONFIG_CPU_IDLE
Len Brownd91ee582011-04-01 18:28:35 -0400131extern void disable_cpuidle(void);
Len Browna0bfa132011-04-01 19:34:59 -0400132extern int cpuidle_idle_call(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400133
134extern int cpuidle_register_driver(struct cpuidle_driver *drv);
Len Brown752138d2010-05-22 16:57:26 -0400135struct cpuidle_driver *cpuidle_get_driver(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400136extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
137extern int cpuidle_register_device(struct cpuidle_device *dev);
138extern void cpuidle_unregister_device(struct cpuidle_device *dev);
139
140extern void cpuidle_pause_and_lock(void);
141extern void cpuidle_resume_and_unlock(void);
142extern int cpuidle_enable_device(struct cpuidle_device *dev);
143extern void cpuidle_disable_device(struct cpuidle_device *dev);
144
145#else
Len Brownd91ee582011-04-01 18:28:35 -0400146static inline void disable_cpuidle(void) { }
Len Browna0bfa132011-04-01 19:34:59 -0400147static inline int cpuidle_idle_call(void) { return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400148
149static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown6b2c6762010-05-11 16:50:52 -0400150{return -ENODEV; }
Len Brown752138d2010-05-22 16:57:26 -0400151static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400152static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
153static inline int cpuidle_register_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400154{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400155static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
156
157static inline void cpuidle_pause_and_lock(void) { }
158static inline void cpuidle_resume_and_unlock(void) { }
159static inline int cpuidle_enable_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400160{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400161static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
162
163#endif
164
165/******************************
166 * CPUIDLE GOVERNOR INTERFACE *
167 ******************************/
168
169struct cpuidle_governor {
170 char name[CPUIDLE_NAME_LEN];
171 struct list_head governor_list;
172 unsigned int rating;
173
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530174 int (*enable) (struct cpuidle_driver *drv,
175 struct cpuidle_device *dev);
176 void (*disable) (struct cpuidle_driver *drv,
177 struct cpuidle_device *dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400178
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530179 int (*select) (struct cpuidle_driver *drv,
180 struct cpuidle_device *dev);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530181 void (*reflect) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400182
183 struct module *owner;
184};
185
186#ifdef CONFIG_CPU_IDLE
187
188extern int cpuidle_register_governor(struct cpuidle_governor *gov);
189extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
190
191#else
192
193static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
194{return 0;}
195static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
196
197#endif
198
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800199#ifdef CONFIG_ARCH_HAS_CPU_RELAX
200#define CPUIDLE_DRIVER_STATE_START 1
201#else
202#define CPUIDLE_DRIVER_STATE_START 0
203#endif
204
Len Brown4f86d3a2007-10-03 18:58:00 -0400205#endif /* _LINUX_CPUIDLE_H */