blob: c90418822f4043b388b84c2b077b65b59a5fe2e1 [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>
16#include <linux/module.h>
17#include <linux/kobject.h>
18#include <linux/completion.h>
19
20#define CPUIDLE_STATE_MAX 8
21#define CPUIDLE_NAME_LEN 16
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080022#define CPUIDLE_DESC_LEN 32
Len Brown4f86d3a2007-10-03 18:58:00 -040023
24struct cpuidle_device;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053025struct cpuidle_driver;
Len Brown4f86d3a2007-10-03 18:58:00 -040026
27
28/****************************
29 * CPUIDLE DEVICE INTERFACE *
30 ****************************/
31
Deepthi Dharwar42027352011-10-28 16:20:33 +053032struct cpuidle_state_usage {
33 void *driver_data;
34
35 unsigned long long usage;
36 unsigned long long time; /* in US */
37};
38
Len Brown4f86d3a2007-10-03 18:58:00 -040039struct cpuidle_state {
40 char name[CPUIDLE_NAME_LEN];
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080041 char desc[CPUIDLE_DESC_LEN];
Len Brown4f86d3a2007-10-03 18:58:00 -040042
43 unsigned int flags;
44 unsigned int exit_latency; /* in US */
45 unsigned int power_usage; /* in mW */
46 unsigned int target_residency; /* in US */
47
Len Brown4f86d3a2007-10-03 18:58:00 -040048 int (*enter) (struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053049 struct cpuidle_driver *drv,
Deepthi Dharware978aa72011-10-28 16:20:09 +053050 int index);
Len Brown4f86d3a2007-10-03 18:58:00 -040051};
52
53/* Idle State Flags */
54#define CPUIDLE_FLAG_TIME_VALID (0x01) /* is residency time measurable? */
Len Brown4f86d3a2007-10-03 18:58:00 -040055
56#define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
57
58/**
59 * cpuidle_get_statedata - retrieves private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053060 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040061 */
Deepthi Dharwar42027352011-10-28 16:20:33 +053062static inline void *cpuidle_get_statedata(struct cpuidle_state_usage *st_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -040063{
Deepthi Dharwar42027352011-10-28 16:20:33 +053064 return st_usage->driver_data;
Len Brown4f86d3a2007-10-03 18:58:00 -040065}
66
67/**
68 * cpuidle_set_statedata - stores private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053069 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040070 * @data: the private data
71 */
72static inline void
Deepthi Dharwar42027352011-10-28 16:20:33 +053073cpuidle_set_statedata(struct cpuidle_state_usage *st_usage, void *data)
Len Brown4f86d3a2007-10-03 18:58:00 -040074{
Deepthi Dharwar42027352011-10-28 16:20:33 +053075 st_usage->driver_data = data;
Len Brown4f86d3a2007-10-03 18:58:00 -040076}
77
78struct cpuidle_state_kobj {
79 struct cpuidle_state *state;
Deepthi Dharwar42027352011-10-28 16:20:33 +053080 struct cpuidle_state_usage *state_usage;
Len Brown4f86d3a2007-10-03 18:58:00 -040081 struct completion kobj_unregister;
82 struct kobject kobj;
83};
84
85struct cpuidle_device {
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040086 unsigned int registered:1;
Harvey Harrisonb5556a62008-02-06 22:39:44 +010087 unsigned int enabled:1;
Len Brown4f86d3a2007-10-03 18:58:00 -040088 unsigned int cpu;
89
90 int last_residency;
91 int state_count;
Deepthi Dharwar42027352011-10-28 16:20:33 +053092 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040093 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040094
95 struct list_head device_list;
96 struct kobject kobj;
97 struct completion kobj_unregister;
98 void *governor_data;
Len Brown4f86d3a2007-10-03 18:58:00 -040099};
100
101DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
102
103/**
104 * cpuidle_get_last_residency - retrieves the last state's residency time
105 * @dev: the target CPU
106 *
107 * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
108 */
109static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
110{
111 return dev->last_residency;
112}
113
114
115/****************************
116 * CPUIDLE DRIVER INTERFACE *
117 ****************************/
118
119struct cpuidle_driver {
120 char name[CPUIDLE_NAME_LEN];
121 struct module *owner;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530122
123 unsigned int power_specified:1;
124 struct cpuidle_state states[CPUIDLE_STATE_MAX];
125 int state_count;
126 int safe_state_index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400127};
128
129#ifdef CONFIG_CPU_IDLE
Len Brownd91ee582011-04-01 18:28:35 -0400130extern void disable_cpuidle(void);
Len Browna0bfa132011-04-01 19:34:59 -0400131extern int cpuidle_idle_call(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400132
133extern int cpuidle_register_driver(struct cpuidle_driver *drv);
Len Brown752138d2010-05-22 16:57:26 -0400134struct cpuidle_driver *cpuidle_get_driver(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400135extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
136extern int cpuidle_register_device(struct cpuidle_device *dev);
137extern void cpuidle_unregister_device(struct cpuidle_device *dev);
138
139extern void cpuidle_pause_and_lock(void);
140extern void cpuidle_resume_and_unlock(void);
141extern int cpuidle_enable_device(struct cpuidle_device *dev);
142extern void cpuidle_disable_device(struct cpuidle_device *dev);
143
144#else
Len Brownd91ee582011-04-01 18:28:35 -0400145static inline void disable_cpuidle(void) { }
Len Browna0bfa132011-04-01 19:34:59 -0400146static inline int cpuidle_idle_call(void) { return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400147
148static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown6b2c6762010-05-11 16:50:52 -0400149{return -ENODEV; }
Len Brown752138d2010-05-22 16:57:26 -0400150static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400151static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
152static inline int cpuidle_register_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400153{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400154static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
155
156static inline void cpuidle_pause_and_lock(void) { }
157static inline void cpuidle_resume_and_unlock(void) { }
158static inline int cpuidle_enable_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400159{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400160static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
161
162#endif
163
164/******************************
165 * CPUIDLE GOVERNOR INTERFACE *
166 ******************************/
167
168struct cpuidle_governor {
169 char name[CPUIDLE_NAME_LEN];
170 struct list_head governor_list;
171 unsigned int rating;
172
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530173 int (*enable) (struct cpuidle_driver *drv,
174 struct cpuidle_device *dev);
175 void (*disable) (struct cpuidle_driver *drv,
176 struct cpuidle_device *dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400177
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530178 int (*select) (struct cpuidle_driver *drv,
179 struct cpuidle_device *dev);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530180 void (*reflect) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400181
182 struct module *owner;
183};
184
185#ifdef CONFIG_CPU_IDLE
186
187extern int cpuidle_register_governor(struct cpuidle_governor *gov);
188extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
189
190#else
191
192static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
193{return 0;}
194static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
195
196#endif
197
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800198#ifdef CONFIG_ARCH_HAS_CPU_RELAX
199#define CPUIDLE_DRIVER_STATE_START 1
200#else
201#define CPUIDLE_DRIVER_STATE_START 0
202#endif
203
Len Brown4f86d3a2007-10-03 18:58:00 -0400204#endif /* _LINUX_CPUIDLE_H */