blob: ca6cdf55eb18fdcefb947b5b69ed208e9b468a19 [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
ShuoX Liudc7fd272012-07-03 19:05:31 +020037 unsigned long long disable;
Deepthi Dharwar42027352011-10-28 16:20:33 +053038 unsigned long long usage;
39 unsigned long long time; /* in US */
40};
41
Len Brown4f86d3a2007-10-03 18:58:00 -040042struct cpuidle_state {
43 char name[CPUIDLE_NAME_LEN];
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080044 char desc[CPUIDLE_DESC_LEN];
Len Brown4f86d3a2007-10-03 18:58:00 -040045
46 unsigned int flags;
47 unsigned int exit_latency; /* in US */
Boris Ostrovsky02401c02012-03-13 19:55:10 +010048 int power_usage; /* in mW */
Len Brown4f86d3a2007-10-03 18:58:00 -040049 unsigned int target_residency; /* in US */
50
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);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010054
55 int (*enter_dead) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -040056};
57
58/* Idle State Flags */
59#define CPUIDLE_FLAG_TIME_VALID (0x01) /* is residency time measurable? */
Len Brown4f86d3a2007-10-03 18:58:00 -040060
61#define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000)
62
63/**
64 * cpuidle_get_statedata - retrieves private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053065 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040066 */
Deepthi Dharwar42027352011-10-28 16:20:33 +053067static inline void *cpuidle_get_statedata(struct cpuidle_state_usage *st_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -040068{
Deepthi Dharwar42027352011-10-28 16:20:33 +053069 return st_usage->driver_data;
Len Brown4f86d3a2007-10-03 18:58:00 -040070}
71
72/**
73 * cpuidle_set_statedata - stores private driver state data
Deepthi Dharwar42027352011-10-28 16:20:33 +053074 * @st_usage: the state usage statistics
Len Brown4f86d3a2007-10-03 18:58:00 -040075 * @data: the private data
76 */
77static inline void
Deepthi Dharwar42027352011-10-28 16:20:33 +053078cpuidle_set_statedata(struct cpuidle_state_usage *st_usage, void *data)
Len Brown4f86d3a2007-10-03 18:58:00 -040079{
Deepthi Dharwar42027352011-10-28 16:20:33 +053080 st_usage->driver_data = data;
Len Brown4f86d3a2007-10-03 18:58:00 -040081}
82
83struct cpuidle_state_kobj {
84 struct cpuidle_state *state;
Deepthi Dharwar42027352011-10-28 16:20:33 +053085 struct cpuidle_state_usage *state_usage;
Len Brown4f86d3a2007-10-03 18:58:00 -040086 struct completion kobj_unregister;
87 struct kobject kobj;
88};
89
90struct cpuidle_device {
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040091 unsigned int registered:1;
Harvey Harrisonb5556a62008-02-06 22:39:44 +010092 unsigned int enabled:1;
Len Brown4f86d3a2007-10-03 18:58:00 -040093 unsigned int cpu;
94
95 int last_residency;
96 int state_count;
Deepthi Dharwar42027352011-10-28 16:20:33 +053097 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040098 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040099
100 struct list_head device_list;
101 struct kobject kobj;
102 struct completion kobj_unregister;
Len Brown4f86d3a2007-10-03 18:58:00 -0400103};
104
105DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
106
107/**
108 * cpuidle_get_last_residency - retrieves the last state's residency time
109 * @dev: the target CPU
110 *
111 * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
112 */
113static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
114{
115 return dev->last_residency;
116}
117
118
119/****************************
120 * CPUIDLE DRIVER INTERFACE *
121 ****************************/
122
123struct cpuidle_driver {
Daniel Lezcanodb70b042012-03-26 14:51:27 +0200124 const char *name;
Len Brown4f86d3a2007-10-03 18:58:00 -0400125 struct module *owner;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530126
127 unsigned int power_specified:1;
Robert Leee1689792012-03-20 15:22:42 -0500128 /* set to 1 to use the core cpuidle time keeping (for all states). */
129 unsigned int en_core_tk_irqen:1;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530130 struct cpuidle_state states[CPUIDLE_STATE_MAX];
131 int state_count;
132 int safe_state_index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400133};
134
135#ifdef CONFIG_CPU_IDLE
Len Brownd91ee582011-04-01 18:28:35 -0400136extern void disable_cpuidle(void);
Len Browna0bfa132011-04-01 19:34:59 -0400137extern int cpuidle_idle_call(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400138extern int cpuidle_register_driver(struct cpuidle_driver *drv);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200139extern struct cpuidle_driver *cpuidle_get_driver(void);
140extern struct cpuidle_driver *cpuidle_driver_ref(void);
141extern void cpuidle_driver_unref(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400142extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
143extern int cpuidle_register_device(struct cpuidle_device *dev);
144extern void cpuidle_unregister_device(struct cpuidle_device *dev);
145
146extern void cpuidle_pause_and_lock(void);
147extern void cpuidle_resume_and_unlock(void);
Preeti U Murthy8651f972012-07-09 10:12:56 +0200148extern void cpuidle_pause(void);
149extern void cpuidle_resume(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400150extern int cpuidle_enable_device(struct cpuidle_device *dev);
151extern void cpuidle_disable_device(struct cpuidle_device *dev);
Robert Leee1689792012-03-20 15:22:42 -0500152extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
153 struct cpuidle_driver *drv, int index,
154 int (*enter)(struct cpuidle_device *dev,
155 struct cpuidle_driver *drv, int index));
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100156extern int cpuidle_play_dead(void);
157
Len Brown4f86d3a2007-10-03 18:58:00 -0400158#else
Len Brownd91ee582011-04-01 18:28:35 -0400159static inline void disable_cpuidle(void) { }
Len Browna0bfa132011-04-01 19:34:59 -0400160static inline int cpuidle_idle_call(void) { return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400161static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown6b2c6762010-05-11 16:50:52 -0400162{return -ENODEV; }
Len Brown752138d2010-05-22 16:57:26 -0400163static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200164static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
165static inline void cpuidle_driver_unref(void) {}
Len Brown4f86d3a2007-10-03 18:58:00 -0400166static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
167static inline int cpuidle_register_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400168{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400169static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
170
171static inline void cpuidle_pause_and_lock(void) { }
172static inline void cpuidle_resume_and_unlock(void) { }
Preeti U Murthy8651f972012-07-09 10:12:56 +0200173static inline void cpuidle_pause(void) { }
174static inline void cpuidle_resume(void) { }
Len Brown4f86d3a2007-10-03 18:58:00 -0400175static inline int cpuidle_enable_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400176{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400177static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
Robert Leee1689792012-03-20 15:22:42 -0500178static inline int cpuidle_wrap_enter(struct cpuidle_device *dev,
179 struct cpuidle_driver *drv, int index,
180 int (*enter)(struct cpuidle_device *dev,
181 struct cpuidle_driver *drv, int index))
182{ return -ENODEV; }
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100183static inline int cpuidle_play_dead(void) {return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400184
185#endif
186
187/******************************
188 * CPUIDLE GOVERNOR INTERFACE *
189 ******************************/
190
191struct cpuidle_governor {
192 char name[CPUIDLE_NAME_LEN];
193 struct list_head governor_list;
194 unsigned int rating;
195
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530196 int (*enable) (struct cpuidle_driver *drv,
197 struct cpuidle_device *dev);
198 void (*disable) (struct cpuidle_driver *drv,
199 struct cpuidle_device *dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400200
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530201 int (*select) (struct cpuidle_driver *drv,
202 struct cpuidle_device *dev);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530203 void (*reflect) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400204
205 struct module *owner;
206};
207
208#ifdef CONFIG_CPU_IDLE
209
210extern int cpuidle_register_governor(struct cpuidle_governor *gov);
211extern void cpuidle_unregister_governor(struct cpuidle_governor *gov);
212
213#else
214
215static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
216{return 0;}
217static inline void cpuidle_unregister_governor(struct cpuidle_governor *gov) { }
218
219#endif
220
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800221#ifdef CONFIG_ARCH_HAS_CPU_RELAX
222#define CPUIDLE_DRIVER_STATE_START 1
223#else
224#define CPUIDLE_DRIVER_STATE_START 0
225#endif
226
Len Brown4f86d3a2007-10-03 18:58:00 -0400227#endif /* _LINUX_CPUIDLE_H */