blob: ca4e4983773f8d28f42b076b3d18d0399a4c7427 [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>
Robert Leee1689792012-03-20 15:22:42 -050018#include <linux/hrtimer.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040019
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
Paul Gortmakerde477252011-05-26 13:46:22 -040024struct module;
25
Len Brown4f86d3a2007-10-03 18:58:00 -040026struct cpuidle_device;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053027struct cpuidle_driver;
Len Brown4f86d3a2007-10-03 18:58:00 -040028
29
30/****************************
31 * CPUIDLE DEVICE INTERFACE *
32 ****************************/
33
Deepthi Dharwar42027352011-10-28 16:20:33 +053034struct cpuidle_state_usage {
35 void *driver_data;
36
37 unsigned long long usage;
38 unsigned long long time; /* in US */
39};
40
Len Brown4f86d3a2007-10-03 18:58:00 -040041struct cpuidle_state {
42 char name[CPUIDLE_NAME_LEN];
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080043 char desc[CPUIDLE_DESC_LEN];
Len Brown4f86d3a2007-10-03 18:58:00 -040044
45 unsigned int flags;
46 unsigned int exit_latency; /* in US */
47 unsigned int power_usage; /* in mW */
48 unsigned int target_residency; /* in US */
ShuoX Liu3a533962012-03-28 15:19:11 -070049 unsigned int disable;
Len Brown4f86d3a2007-10-03 18:58:00 -040050
Len Brown4f86d3a2007-10-03 18:58:00 -040051 int (*enter) (struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053052 struct cpuidle_driver *drv,
Deepthi Dharware978aa72011-10-28 16:20:09 +053053 int index);
Len Brown4f86d3a2007-10-03 18:58:00 -040054};
55
56/* Idle State Flags */
57#define CPUIDLE_FLAG_TIME_VALID (0x01) /* is residency time measurable? */
Len Brown4f86d3a2007-10-03 18:58:00 -040058
59#define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
60
61/**
62 * cpuidle_get_statedata - retrieves private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053063 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040064 */
Deepthi Dharwar42027352011-10-28 16:20:33 +053065static inline void *cpuidle_get_statedata(struct cpuidle_state_usage *st_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -040066{
Deepthi Dharwar42027352011-10-28 16:20:33 +053067 return st_usage->driver_data;
Len Brown4f86d3a2007-10-03 18:58:00 -040068}
69
70/**
71 * cpuidle_set_statedata - stores private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053072 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040073 * @data: the private data
74 */
75static inline void
Deepthi Dharwar42027352011-10-28 16:20:33 +053076cpuidle_set_statedata(struct cpuidle_state_usage *st_usage, void *data)
Len Brown4f86d3a2007-10-03 18:58:00 -040077{
Deepthi Dharwar42027352011-10-28 16:20:33 +053078 st_usage->driver_data = data;
Len Brown4f86d3a2007-10-03 18:58:00 -040079}
80
81struct cpuidle_state_kobj {
82 struct cpuidle_state *state;
Deepthi Dharwar42027352011-10-28 16:20:33 +053083 struct cpuidle_state_usage *state_usage;
Len Brown4f86d3a2007-10-03 18:58:00 -040084 struct completion kobj_unregister;
85 struct kobject kobj;
86};
87
88struct cpuidle_device {
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040089 unsigned int registered:1;
Harvey Harrisonb5556a62008-02-06 22:39:44 +010090 unsigned int enabled:1;
Len Brown4f86d3a2007-10-03 18:58:00 -040091 unsigned int cpu;
92
93 int last_residency;
94 int state_count;
Deepthi Dharwar42027352011-10-28 16:20:33 +053095 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040096 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040097
98 struct list_head device_list;
99 struct kobject kobj;
100 struct completion kobj_unregister;
101 void *governor_data;
Len Brown4f86d3a2007-10-03 18:58:00 -0400102};
103
104DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
105
106/**
107 * cpuidle_get_last_residency - retrieves the last state's residency time
108 * @dev: the target CPU
109 *
110 * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
111 */
112static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
113{
114 return dev->last_residency;
115}
116
117
118/****************************
119 * CPUIDLE DRIVER INTERFACE *
120 ****************************/
121
122struct cpuidle_driver {
123 char name[CPUIDLE_NAME_LEN];
124 struct module *owner;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530125
126 unsigned int power_specified:1;
Robert Leee1689792012-03-20 15:22:42 -0500127 /* set to 1 to use the core cpuidle time keeping (for all states). */
128 unsigned int en_core_tk_irqen:1;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530129 struct cpuidle_state states[CPUIDLE_STATE_MAX];
130 int state_count;
131 int safe_state_index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400132};
133
134#ifdef CONFIG_CPU_IDLE
Len Brownd91ee582011-04-01 18:28:35 -0400135extern void disable_cpuidle(void);
Len Browna0bfa132011-04-01 19:34:59 -0400136extern int cpuidle_idle_call(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400137extern int cpuidle_register_driver(struct cpuidle_driver *drv);
Len Brown752138d2010-05-22 16:57:26 -0400138struct cpuidle_driver *cpuidle_get_driver(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400139extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
140extern int cpuidle_register_device(struct cpuidle_device *dev);
141extern void cpuidle_unregister_device(struct cpuidle_device *dev);
142
143extern void cpuidle_pause_and_lock(void);
144extern void cpuidle_resume_and_unlock(void);
145extern int cpuidle_enable_device(struct cpuidle_device *dev);
146extern void cpuidle_disable_device(struct cpuidle_device *dev);
Robert Leee1689792012-03-20 15:22:42 -0500147extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
148 struct cpuidle_driver *drv, int index,
149 int (*enter)(struct cpuidle_device *dev,
150 struct cpuidle_driver *drv, int index));
Len Brown4f86d3a2007-10-03 18:58:00 -0400151#else
Len Brownd91ee582011-04-01 18:28:35 -0400152static inline void disable_cpuidle(void) { }
Len Browna0bfa132011-04-01 19:34:59 -0400153static inline int cpuidle_idle_call(void) { return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400154static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown6b2c6762010-05-11 16:50:52 -0400155{return -ENODEV; }
Len Brown752138d2010-05-22 16:57:26 -0400156static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400157static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
158static inline int cpuidle_register_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_unregister_device(struct cpuidle_device *dev) { }
161
162static inline void cpuidle_pause_and_lock(void) { }
163static inline void cpuidle_resume_and_unlock(void) { }
164static inline int cpuidle_enable_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400165{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400166static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
Robert Leee1689792012-03-20 15:22:42 -0500167static inline int cpuidle_wrap_enter(struct cpuidle_device *dev,
168 struct cpuidle_driver *drv, int index,
169 int (*enter)(struct cpuidle_device *dev,
170 struct cpuidle_driver *drv, int index))
171{ return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400172
173#endif
174
175/******************************
176 * CPUIDLE GOVERNOR INTERFACE *
177 ******************************/
178
179struct cpuidle_governor {
180 char name[CPUIDLE_NAME_LEN];
181 struct list_head governor_list;
182 unsigned int rating;
183
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530184 int (*enable) (struct cpuidle_driver *drv,
185 struct cpuidle_device *dev);
186 void (*disable) (struct cpuidle_driver *drv,
187 struct cpuidle_device *dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400188
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530189 int (*select) (struct cpuidle_driver *drv,
190 struct cpuidle_device *dev);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530191 void (*reflect) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400192
193 struct module *owner;
194};
195
196#ifdef CONFIG_CPU_IDLE
197
198extern int cpuidle_register_governor(struct cpuidle_governor *gov);
199extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
200
Thomas Renninger65b7f832012-01-17 22:40:08 +0100201#ifdef CONFIG_INTEL_IDLE
202extern int intel_idle_cpu_init(int cpu);
Len Brown4f86d3a2007-10-03 18:58:00 -0400203#else
Thomas Renninger65b7f832012-01-17 22:40:08 +0100204static inline int intel_idle_cpu_init(int cpu) { return -1; }
205#endif
206
207#else
208static inline int intel_idle_cpu_init(int cpu) { return -1; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400209
210static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
211{return 0;}
212static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
213
214#endif
215
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800216#ifdef CONFIG_ARCH_HAS_CPU_RELAX
217#define CPUIDLE_DRIVER_STATE_START 1
218#else
219#define CPUIDLE_DRIVER_STATE_START 0
220#endif
221
Len Brown4f86d3a2007-10-03 18:58:00 -0400222#endif /* _LINUX_CPUIDLE_H */