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