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 #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 #define CPUIDLE_FLAG_RCU_IDLE BIT(6) /* idle-state takes care of RCU */ 86 87 struct cpuidle_device_kobj; 88 struct cpuidle_state_kobj; 89 struct cpuidle_driver_kobj; 90 91 struct cpuidle_device { 92 unsigned int registered:1; 93 unsigned int enabled:1; 94 unsigned int poll_time_limit:1; 95 unsigned int cpu; 96 ktime_t next_hrtimer; 97 98 int last_state_idx; 99 u64 last_residency_ns; 100 u64 poll_limit_ns; 101 u64 forced_idle_latency_limit_ns; 102 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX]; 103 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX]; 104 struct cpuidle_driver_kobj *kobj_driver; 105 struct cpuidle_device_kobj *kobj_dev; 106 struct list_head device_list; 107 108 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 109 cpumask_t coupled_cpus; 110 struct cpuidle_coupled *coupled; 111 #endif 112 }; 113 114 DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 115 DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev); 116 117 /**************************** 118 * CPUIDLE DRIVER INTERFACE * 119 ****************************/ 120 121 struct cpuidle_driver { 122 const char *name; 123 struct module *owner; 124 125 /* used by the cpuidle framework to setup the broadcast timer */ 126 unsigned int bctimer:1; 127 /* states array must be ordered in decreasing power consumption */ 128 struct cpuidle_state states[CPUIDLE_STATE_MAX]; 129 int state_count; 130 int safe_state_index; 131 132 /* the driver handles the cpus in cpumask */ 133 struct cpumask *cpumask; 134 135 /* preferred governor to switch at register time */ 136 const char *governor; 137 }; 138 139 #ifdef CONFIG_CPU_IDLE 140 extern void disable_cpuidle(void); 141 extern bool cpuidle_not_available(struct cpuidle_driver *drv, 142 struct cpuidle_device *dev); 143 144 extern int cpuidle_select(struct cpuidle_driver *drv, 145 struct cpuidle_device *dev, 146 bool *stop_tick); 147 extern int cpuidle_enter(struct cpuidle_driver *drv, 148 struct cpuidle_device *dev, int index); 149 extern void cpuidle_reflect(struct cpuidle_device *dev, int index); 150 extern u64 cpuidle_poll_time(struct cpuidle_driver *drv, 151 struct cpuidle_device *dev); 152 153 extern int cpuidle_register_driver(struct cpuidle_driver *drv); 154 extern struct cpuidle_driver *cpuidle_get_driver(void); 155 extern void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx, 156 bool disable); 157 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv); 158 extern int cpuidle_register_device(struct cpuidle_device *dev); 159 extern void cpuidle_unregister_device(struct cpuidle_device *dev); 160 extern int cpuidle_register(struct cpuidle_driver *drv, 161 const struct cpumask *const coupled_cpus); 162 extern void cpuidle_unregister(struct cpuidle_driver *drv); 163 extern void cpuidle_pause_and_lock(void); 164 extern void cpuidle_resume_and_unlock(void); 165 extern void cpuidle_pause(void); 166 extern void cpuidle_resume(void); 167 extern int cpuidle_enable_device(struct cpuidle_device *dev); 168 extern void cpuidle_disable_device(struct cpuidle_device *dev); 169 extern int cpuidle_play_dead(void); 170 171 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev); 172 static inline struct cpuidle_device *cpuidle_get_device(void) 173 {return __this_cpu_read(cpuidle_devices); } 174 #else 175 static inline void disable_cpuidle(void) { } 176 static inline bool cpuidle_not_available(struct cpuidle_driver *drv, 177 struct cpuidle_device *dev) 178 {return true; } 179 static inline int cpuidle_select(struct cpuidle_driver *drv, 180 struct cpuidle_device *dev, bool *stop_tick) 181 {return -ENODEV; } 182 static inline int cpuidle_enter(struct cpuidle_driver *drv, 183 struct cpuidle_device *dev, int index) 184 {return -ENODEV; } 185 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { } 186 static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv, 187 struct cpuidle_device *dev) 188 {return 0; } 189 static inline int cpuidle_register_driver(struct cpuidle_driver *drv) 190 {return -ENODEV; } 191 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; } 192 static inline void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, 193 int idx, bool disable) { } 194 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { } 195 static inline int cpuidle_register_device(struct cpuidle_device *dev) 196 {return -ENODEV; } 197 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { } 198 static inline int cpuidle_register(struct cpuidle_driver *drv, 199 const struct cpumask *const coupled_cpus) 200 {return -ENODEV; } 201 static inline void cpuidle_unregister(struct cpuidle_driver *drv) { } 202 static inline void cpuidle_pause_and_lock(void) { } 203 static inline void cpuidle_resume_and_unlock(void) { } 204 static inline void cpuidle_pause(void) { } 205 static inline void cpuidle_resume(void) { } 206 static inline int cpuidle_enable_device(struct cpuidle_device *dev) 207 {return -ENODEV; } 208 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { } 209 static inline int cpuidle_play_dead(void) {return -ENODEV; } 210 static inline struct cpuidle_driver *cpuidle_get_cpu_driver( 211 struct cpuidle_device *dev) {return NULL; } 212 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; } 213 #endif 214 215 #ifdef CONFIG_CPU_IDLE 216 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 217 struct cpuidle_device *dev, 218 u64 latency_limit_ns); 219 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv, 220 struct cpuidle_device *dev); 221 extern void cpuidle_use_deepest_state(u64 latency_limit_ns); 222 #else 223 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 224 struct cpuidle_device *dev, 225 u64 latency_limit_ns) 226 {return -ENODEV; } 227 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv, 228 struct cpuidle_device *dev) 229 {return -ENODEV; } 230 static inline void cpuidle_use_deepest_state(u64 latency_limit_ns) 231 { 232 } 233 #endif 234 235 /* kernel/sched/idle.c */ 236 extern void sched_idle_set_state(struct cpuidle_state *idle_state); 237 extern void default_idle_call(void); 238 239 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 240 void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a); 241 #else 242 static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a) 243 { 244 } 245 #endif 246 247 #if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX) 248 void cpuidle_poll_state_init(struct cpuidle_driver *drv); 249 #else 250 static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {} 251 #endif 252 253 /****************************** 254 * CPUIDLE GOVERNOR INTERFACE * 255 ******************************/ 256 257 struct cpuidle_governor { 258 char name[CPUIDLE_NAME_LEN]; 259 struct list_head governor_list; 260 unsigned int rating; 261 262 int (*enable) (struct cpuidle_driver *drv, 263 struct cpuidle_device *dev); 264 void (*disable) (struct cpuidle_driver *drv, 265 struct cpuidle_device *dev); 266 267 int (*select) (struct cpuidle_driver *drv, 268 struct cpuidle_device *dev, 269 bool *stop_tick); 270 void (*reflect) (struct cpuidle_device *dev, int index); 271 }; 272 273 #ifdef CONFIG_CPU_IDLE 274 extern int cpuidle_register_governor(struct cpuidle_governor *gov); 275 extern s64 cpuidle_governor_latency_req(unsigned int cpu); 276 #else 277 static inline int cpuidle_register_governor(struct cpuidle_governor *gov) 278 {return 0;} 279 #endif 280 281 #define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, \ 282 idx, \ 283 state, \ 284 is_retention) \ 285 ({ \ 286 int __ret = 0; \ 287 \ 288 if (!idx) { \ 289 cpu_do_idle(); \ 290 return idx; \ 291 } \ 292 \ 293 if (!is_retention) \ 294 __ret = cpu_pm_enter(); \ 295 if (!__ret) { \ 296 __ret = low_level_idle_enter(state); \ 297 if (!is_retention) \ 298 cpu_pm_exit(); \ 299 } \ 300 \ 301 __ret ? -1 : idx; \ 302 }) 303 304 #define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \ 305 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0) 306 307 #define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \ 308 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 1) 309 310 #define CPU_PM_CPU_IDLE_ENTER_PARAM(low_level_idle_enter, idx, state) \ 311 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 0) 312 313 #define CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(low_level_idle_enter, idx, state) \ 314 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 1) 315 316 #endif /* _LINUX_CPUIDLE_H */ 317