1 /* 2 * cpuidle.h - a generic framework for CPU idle power management 3 * 4 * (C) 2007 Venkatesh Pallipadi <[email protected]> 5 * Shaohua Li <[email protected]> 6 * Adam Belay <[email protected]> 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/hrtimer.h> 17 18 #define CPUIDLE_STATE_MAX 10 19 #define CPUIDLE_NAME_LEN 16 20 #define CPUIDLE_DESC_LEN 32 21 22 struct module; 23 24 struct cpuidle_device; 25 struct cpuidle_driver; 26 27 28 /**************************** 29 * CPUIDLE DEVICE INTERFACE * 30 ****************************/ 31 32 struct cpuidle_state_usage { 33 unsigned long long disable; 34 unsigned long long usage; 35 unsigned long long time; /* in US */ 36 unsigned long long above; /* Number of times it's been too deep */ 37 unsigned long long below; /* Number of times it's been too shallow */ 38 #ifdef CONFIG_SUSPEND 39 unsigned long long s2idle_usage; 40 unsigned long long s2idle_time; /* in US */ 41 #endif 42 }; 43 44 struct cpuidle_state { 45 char name[CPUIDLE_NAME_LEN]; 46 char desc[CPUIDLE_DESC_LEN]; 47 48 unsigned int flags; 49 unsigned int exit_latency; /* in US */ 50 int power_usage; /* in mW */ 51 unsigned int target_residency; /* in US */ 52 bool disabled; /* disabled on all CPUs */ 53 54 int (*enter) (struct cpuidle_device *dev, 55 struct cpuidle_driver *drv, 56 int index); 57 58 int (*enter_dead) (struct cpuidle_device *dev, int index); 59 60 /* 61 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping 62 * suspended, so it must not re-enable interrupts at any point (even 63 * temporarily) or attempt to change states of clock event devices. 64 */ 65 void (*enter_s2idle) (struct cpuidle_device *dev, 66 struct cpuidle_driver *drv, 67 int index); 68 }; 69 70 /* Idle State Flags */ 71 #define CPUIDLE_FLAG_NONE (0x00) 72 #define CPUIDLE_FLAG_POLLING BIT(0) /* polling state */ 73 #define CPUIDLE_FLAG_COUPLED BIT(1) /* state applies to multiple cpus */ 74 #define CPUIDLE_FLAG_TIMER_STOP BIT(2) /* timer is stopped on this state */ 75 76 struct cpuidle_device_kobj; 77 struct cpuidle_state_kobj; 78 struct cpuidle_driver_kobj; 79 80 struct cpuidle_device { 81 unsigned int registered:1; 82 unsigned int enabled:1; 83 unsigned int use_deepest_state:1; 84 unsigned int poll_time_limit:1; 85 unsigned int cpu; 86 ktime_t next_hrtimer; 87 88 int last_residency; 89 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX]; 90 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX]; 91 struct cpuidle_driver_kobj *kobj_driver; 92 struct cpuidle_device_kobj *kobj_dev; 93 struct list_head device_list; 94 95 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 96 cpumask_t coupled_cpus; 97 struct cpuidle_coupled *coupled; 98 #endif 99 }; 100 101 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 102 DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev); 103 104 /**************************** 105 * CPUIDLE DRIVER INTERFACE * 106 ****************************/ 107 108 struct cpuidle_driver { 109 const char *name; 110 struct module *owner; 111 int refcnt; 112 113 /* used by the cpuidle framework to setup the broadcast timer */ 114 unsigned int bctimer:1; 115 /* states array must be ordered in decreasing power consumption */ 116 struct cpuidle_state states[CPUIDLE_STATE_MAX]; 117 int state_count; 118 int safe_state_index; 119 120 /* the driver handles the cpus in cpumask */ 121 struct cpumask *cpumask; 122 }; 123 124 #ifdef CONFIG_CPU_IDLE 125 extern void disable_cpuidle(void); 126 extern bool cpuidle_not_available(struct cpuidle_driver *drv, 127 struct cpuidle_device *dev); 128 129 extern int cpuidle_select(struct cpuidle_driver *drv, 130 struct cpuidle_device *dev, 131 bool *stop_tick); 132 extern int cpuidle_enter(struct cpuidle_driver *drv, 133 struct cpuidle_device *dev, int index); 134 extern void cpuidle_reflect(struct cpuidle_device *dev, int index); 135 136 extern int cpuidle_register_driver(struct cpuidle_driver *drv); 137 extern struct cpuidle_driver *cpuidle_get_driver(void); 138 extern struct cpuidle_driver *cpuidle_driver_ref(void); 139 extern void cpuidle_driver_unref(void); 140 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv); 141 extern int cpuidle_register_device(struct cpuidle_device *dev); 142 extern void cpuidle_unregister_device(struct cpuidle_device *dev); 143 extern int cpuidle_register(struct cpuidle_driver *drv, 144 const struct cpumask *const coupled_cpus); 145 extern void cpuidle_unregister(struct cpuidle_driver *drv); 146 extern void cpuidle_pause_and_lock(void); 147 extern void cpuidle_resume_and_unlock(void); 148 extern void cpuidle_pause(void); 149 extern void cpuidle_resume(void); 150 extern int cpuidle_enable_device(struct cpuidle_device *dev); 151 extern void cpuidle_disable_device(struct cpuidle_device *dev); 152 extern int cpuidle_play_dead(void); 153 154 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev); 155 static inline struct cpuidle_device *cpuidle_get_device(void) 156 {return __this_cpu_read(cpuidle_devices); } 157 #else 158 static inline void disable_cpuidle(void) { } 159 static inline bool cpuidle_not_available(struct cpuidle_driver *drv, 160 struct cpuidle_device *dev) 161 {return true; } 162 static inline int cpuidle_select(struct cpuidle_driver *drv, 163 struct cpuidle_device *dev, bool *stop_tick) 164 {return -ENODEV; } 165 static inline int cpuidle_enter(struct cpuidle_driver *drv, 166 struct cpuidle_device *dev, int index) 167 {return -ENODEV; } 168 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { } 169 static inline int cpuidle_register_driver(struct cpuidle_driver *drv) 170 {return -ENODEV; } 171 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; } 172 static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; } 173 static inline void cpuidle_driver_unref(void) {} 174 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { } 175 static inline int cpuidle_register_device(struct cpuidle_device *dev) 176 {return -ENODEV; } 177 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { } 178 static inline int cpuidle_register(struct cpuidle_driver *drv, 179 const struct cpumask *const coupled_cpus) 180 {return -ENODEV; } 181 static inline void cpuidle_unregister(struct cpuidle_driver *drv) { } 182 static inline void cpuidle_pause_and_lock(void) { } 183 static inline void cpuidle_resume_and_unlock(void) { } 184 static inline void cpuidle_pause(void) { } 185 static inline void cpuidle_resume(void) { } 186 static inline int cpuidle_enable_device(struct cpuidle_device *dev) 187 {return -ENODEV; } 188 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { } 189 static inline int cpuidle_play_dead(void) {return -ENODEV; } 190 static inline struct cpuidle_driver *cpuidle_get_cpu_driver( 191 struct cpuidle_device *dev) {return NULL; } 192 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; } 193 #endif 194 195 #ifdef CONFIG_CPU_IDLE 196 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 197 struct cpuidle_device *dev); 198 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv, 199 struct cpuidle_device *dev); 200 extern void cpuidle_use_deepest_state(bool enable); 201 #else 202 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 203 struct cpuidle_device *dev) 204 {return -ENODEV; } 205 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv, 206 struct cpuidle_device *dev) 207 {return -ENODEV; } 208 static inline void cpuidle_use_deepest_state(bool enable) 209 { 210 } 211 #endif 212 213 /* kernel/sched/idle.c */ 214 extern void sched_idle_set_state(struct cpuidle_state *idle_state); 215 extern void default_idle_call(void); 216 217 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 218 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a); 219 #else 220 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a) 221 { 222 } 223 #endif 224 225 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX) 226 void cpuidle_poll_state_init(struct cpuidle_driver *drv); 227 #else 228 static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {} 229 #endif 230 231 /****************************** 232 * CPUIDLE GOVERNOR INTERFACE * 233 ******************************/ 234 235 struct cpuidle_governor { 236 char name[CPUIDLE_NAME_LEN]; 237 struct list_head governor_list; 238 unsigned int rating; 239 240 int (*enable) (struct cpuidle_driver *drv, 241 struct cpuidle_device *dev); 242 void (*disable) (struct cpuidle_driver *drv, 243 struct cpuidle_device *dev); 244 245 int (*select) (struct cpuidle_driver *drv, 246 struct cpuidle_device *dev, 247 bool *stop_tick); 248 void (*reflect) (struct cpuidle_device *dev, int index); 249 }; 250 251 #ifdef CONFIG_CPU_IDLE 252 extern int cpuidle_register_governor(struct cpuidle_governor *gov); 253 extern int cpuidle_governor_latency_req(unsigned int cpu); 254 #else 255 static inline int cpuidle_register_governor(struct cpuidle_governor *gov) 256 {return 0;} 257 #endif 258 259 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, is_retention) \ 260 ({ \ 261 int __ret = 0; \ 262 \ 263 if (!idx) { \ 264 cpu_do_idle(); \ 265 return idx; \ 266 } \ 267 \ 268 if (!is_retention) \ 269 __ret = cpu_pm_enter(); \ 270 if (!__ret) { \ 271 __ret = low_level_idle_enter(idx); \ 272 if (!is_retention) \ 273 cpu_pm_exit(); \ 274 } \ 275 \ 276 __ret ? -1 : idx; \ 277 }) 278 279 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \ 280 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 0) 281 282 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \ 283 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, 1) 284 285 #endif /* _LINUX_CPUIDLE_H */ 286