blob: 4b6b5bea8f793383d19eac64948097eed0426a9e [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>
Robert Leee1689792012-03-20 15:22:42 -050016#include <linux/hrtimer.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040017
Len Brown86239ce2013-02-27 13:18:50 -050018#define CPUIDLE_STATE_MAX 10
Len Brown4f86d3a2007-10-03 18:58:00 -040019#define CPUIDLE_NAME_LEN 16
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080020#define CPUIDLE_DESC_LEN 32
Len Brown4f86d3a2007-10-03 18:58:00 -040021
Paul Gortmakerde477252011-05-26 13:46:22 -040022struct module;
23
Len Brown4f86d3a2007-10-03 18:58:00 -040024struct cpuidle_device;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053025struct cpuidle_driver;
Len Brown4f86d3a2007-10-03 18:58:00 -040026
27
28/****************************
29 * CPUIDLE DEVICE INTERFACE *
30 ****************************/
31
Deepthi Dharwar42027352011-10-28 16:20:33 +053032struct cpuidle_state_usage {
ShuoX Liudc7fd272012-07-03 19:05:31 +020033 unsigned long long disable;
Deepthi Dharwar42027352011-10-28 16:20:33 +053034 unsigned long long usage;
35 unsigned long long time; /* in US */
Rafael J. Wysocki04dab582018-12-10 12:30:23 +010036 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 */
Rafael J. Wysocki64bdff62018-03-14 12:27:21 +010038#ifdef CONFIG_SUSPEND
39 unsigned long long s2idle_usage;
40 unsigned long long s2idle_time; /* in US */
41#endif
Deepthi Dharwar42027352011-10-28 16:20:33 +053042};
43
Len Brown4f86d3a2007-10-03 18:58:00 -040044struct cpuidle_state {
45 char name[CPUIDLE_NAME_LEN];
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -080046 char desc[CPUIDLE_DESC_LEN];
Len Brown4f86d3a2007-10-03 18:58:00 -040047
48 unsigned int flags;
49 unsigned int exit_latency; /* in US */
Boris Ostrovsky02401c02012-03-13 19:55:10 +010050 int power_usage; /* in mW */
Len Brown4f86d3a2007-10-03 18:58:00 -040051 unsigned int target_residency; /* in US */
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +020052 bool disabled; /* disabled on all CPUs */
Len Brown4f86d3a2007-10-03 18:58:00 -040053
Len Brown4f86d3a2007-10-03 18:58:00 -040054 int (*enter) (struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053055 struct cpuidle_driver *drv,
Deepthi Dharware978aa72011-10-28 16:20:09 +053056 int index);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010057
58 int (*enter_dead) (struct cpuidle_device *dev, int index);
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010059
60 /*
Rafael J. Wysocki28ba0862017-08-10 00:14:45 +020061 * CPUs execute ->enter_s2idle with the local tick or entire timekeeping
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010062 * suspended, so it must not re-enable interrupts at any point (even
63 * temporarily) or attempt to change states of clock event devices.
64 */
Rafael J. Wysocki28ba0862017-08-10 00:14:45 +020065 void (*enter_s2idle) (struct cpuidle_device *dev,
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010066 struct cpuidle_driver *drv,
67 int index);
Len Brown4f86d3a2007-10-03 18:58:00 -040068};
69
70/* Idle State Flags */
Gautham R. Shenoy9e9fc6f2017-01-25 14:06:27 +053071#define CPUIDLE_FLAG_NONE (0x00)
Yangtao Li44021602019-01-15 12:22:10 -050072#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 */
Len Brown4f86d3a2007-10-03 18:58:00 -040075
Daniel Lezcano728ce222013-06-12 15:08:51 +020076struct cpuidle_device_kobj;
Daniel Lezcano1a706432013-06-12 15:08:52 +020077struct cpuidle_state_kobj;
78struct cpuidle_driver_kobj;
Daniel Lezcano728ce222013-06-12 15:08:51 +020079
Len Brown4f86d3a2007-10-03 18:58:00 -040080struct cpuidle_device {
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040081 unsigned int registered:1;
Harvey Harrisonb5556a62008-02-06 22:39:44 +010082 unsigned int enabled:1;
Jacob Panbb8313b2016-11-28 23:03:04 -080083 unsigned int use_deepest_state:1;
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020084 unsigned int poll_time_limit:1;
Len Brown4f86d3a2007-10-03 18:58:00 -040085 unsigned int cpu;
Ulf Hansson6f9b83a2019-03-27 15:35:47 +010086 ktime_t next_hrtimer;
Len Brown4f86d3a2007-10-03 18:58:00 -040087
Marcelo Tosatti7d4daee2019-07-03 20:51:27 -030088 int last_state_idx;
Len Brown4f86d3a2007-10-03 18:58:00 -040089 int last_residency;
Marcelo Tosatti259231a2019-07-03 20:51:26 -030090 u64 poll_limit_ns;
Deepthi Dharwar42027352011-10-28 16:20:33 +053091 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
Len Brown4f86d3a2007-10-03 18:58:00 -040092 struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +000093 struct cpuidle_driver_kobj *kobj_driver;
Daniel Lezcano728ce222013-06-12 15:08:51 +020094 struct cpuidle_device_kobj *kobj_dev;
Len Brown4f86d3a2007-10-03 18:58:00 -040095 struct list_head device_list;
Colin Cross4126c012012-05-07 17:57:41 -070096
97#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
Colin Cross4126c012012-05-07 17:57:41 -070098 cpumask_t coupled_cpus;
99 struct cpuidle_coupled *coupled;
100#endif
Len Brown4f86d3a2007-10-03 18:58:00 -0400101};
102
103DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Paul Burtonf08dbf82014-01-08 11:23:35 +0000104DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400105
Len Brown4f86d3a2007-10-03 18:58:00 -0400106/****************************
107 * CPUIDLE DRIVER INTERFACE *
108 ****************************/
109
110struct cpuidle_driver {
Daniel Lezcanodb70b042012-03-26 14:51:27 +0200111 const char *name;
Len Brown4f86d3a2007-10-03 18:58:00 -0400112 struct module *owner;
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000113 int refcnt;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530114
Daniel Lezcanoa06df062013-03-27 10:22:10 +0000115 /* used by the cpuidle framework to setup the broadcast timer */
116 unsigned int bctimer:1;
Daniel Lezcano8aef33a2013-01-15 14:18:04 +0100117 /* states array must be ordered in decreasing power consumption */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530118 struct cpuidle_state states[CPUIDLE_STATE_MAX];
119 int state_count;
120 int safe_state_index;
Daniel Lezcano82467a52013-06-07 21:53:09 +0000121
122 /* the driver handles the cpus in cpumask */
Viresh Kumar6587fca2013-10-03 21:26:40 +0530123 struct cpumask *cpumask;
Joao Martinscb5d8c42019-09-08 00:45:21 +0100124
125 /* preferred governor to switch at register time */
126 const char *governor;
Len Brown4f86d3a2007-10-03 18:58:00 -0400127};
128
129#ifdef CONFIG_CPU_IDLE
Len Brownd91ee582011-04-01 18:28:35 -0400130extern void disable_cpuidle(void);
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100131extern bool cpuidle_not_available(struct cpuidle_driver *drv,
132 struct cpuidle_device *dev);
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100133
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100134extern int cpuidle_select(struct cpuidle_driver *drv,
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +0100135 struct cpuidle_device *dev,
136 bool *stop_tick);
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100137extern int cpuidle_enter(struct cpuidle_driver *drv,
138 struct cpuidle_device *dev, int index);
139extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
Marcelo Tosatti259231a2019-07-03 20:51:26 -0300140extern u64 cpuidle_poll_time(struct cpuidle_driver *drv,
141 struct cpuidle_device *dev);
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100142
Len Brown4f86d3a2007-10-03 18:58:00 -0400143extern int cpuidle_register_driver(struct cpuidle_driver *drv);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200144extern struct cpuidle_driver *cpuidle_get_driver(void);
145extern struct cpuidle_driver *cpuidle_driver_ref(void);
146extern void cpuidle_driver_unref(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400147extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
148extern int cpuidle_register_device(struct cpuidle_device *dev);
149extern void cpuidle_unregister_device(struct cpuidle_device *dev);
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000150extern int cpuidle_register(struct cpuidle_driver *drv,
151 const struct cpumask *const coupled_cpus);
152extern void cpuidle_unregister(struct cpuidle_driver *drv);
Len Brown4f86d3a2007-10-03 18:58:00 -0400153extern void cpuidle_pause_and_lock(void);
154extern void cpuidle_resume_and_unlock(void);
Preeti U Murthy8651f972012-07-09 10:12:56 +0200155extern void cpuidle_pause(void);
156extern void cpuidle_resume(void);
Len Brown4f86d3a2007-10-03 18:58:00 -0400157extern int cpuidle_enable_device(struct cpuidle_device *dev);
158extern void cpuidle_disable_device(struct cpuidle_device *dev);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100159extern int cpuidle_play_dead(void);
160
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000161extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
Catalin Marinas9bd616e2016-06-01 18:52:16 +0100162static inline struct cpuidle_device *cpuidle_get_device(void)
163{return __this_cpu_read(cpuidle_devices); }
Len Brown4f86d3a2007-10-03 18:58:00 -0400164#else
Len Brownd91ee582011-04-01 18:28:35 -0400165static inline void disable_cpuidle(void) { }
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100166static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
167 struct cpuidle_device *dev)
168{return true; }
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100169static inline int cpuidle_select(struct cpuidle_driver *drv,
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +0100170 struct cpuidle_device *dev, bool *stop_tick)
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100171{return -ENODEV; }
172static inline int cpuidle_enter(struct cpuidle_driver *drv,
173 struct cpuidle_device *dev, int index)
174{return -ENODEV; }
175static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
Stephen Rothwell7dcddef2019-07-31 15:29:52 +1000176static inline u64 cpuidle_poll_time(struct cpuidle_driver *drv,
Marcelo Tosatti259231a2019-07-03 20:51:26 -0300177 struct cpuidle_device *dev)
178{return 0; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400179static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown6b2c6762010-05-11 16:50:52 -0400180{return -ENODEV; }
Len Brown752138d2010-05-22 16:57:26 -0400181static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200182static inline struct cpuidle_driver *cpuidle_driver_ref(void) {return NULL; }
183static inline void cpuidle_driver_unref(void) {}
Len Brown4f86d3a2007-10-03 18:58:00 -0400184static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
185static inline int cpuidle_register_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400186{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400187static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000188static inline int cpuidle_register(struct cpuidle_driver *drv,
189 const struct cpumask *const coupled_cpus)
190{return -ENODEV; }
191static inline void cpuidle_unregister(struct cpuidle_driver *drv) { }
Len Brown4f86d3a2007-10-03 18:58:00 -0400192static inline void cpuidle_pause_and_lock(void) { }
193static inline void cpuidle_resume_and_unlock(void) { }
Preeti U Murthy8651f972012-07-09 10:12:56 +0200194static inline void cpuidle_pause(void) { }
195static inline void cpuidle_resume(void) { }
Len Brown4f86d3a2007-10-03 18:58:00 -0400196static inline int cpuidle_enable_device(struct cpuidle_device *dev)
Len Brown6b2c6762010-05-11 16:50:52 -0400197{return -ENODEV; }
Len Brown4f86d3a2007-10-03 18:58:00 -0400198static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100199static inline int cpuidle_play_dead(void) {return -ENODEV; }
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200200static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
201 struct cpuidle_device *dev) {return NULL; }
Catalin Marinas9bd616e2016-06-01 18:52:16 +0100202static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200203#endif
204
Jacob Panbb8313b2016-11-28 23:03:04 -0800205#ifdef CONFIG_CPU_IDLE
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200206extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
207 struct cpuidle_device *dev);
Rafael J. Wysocki28ba0862017-08-10 00:14:45 +0200208extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200209 struct cpuidle_device *dev);
Jacob Panbb8313b2016-11-28 23:03:04 -0800210extern void cpuidle_use_deepest_state(bool enable);
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200211#else
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100212static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
213 struct cpuidle_device *dev)
214{return -ENODEV; }
Rafael J. Wysocki28ba0862017-08-10 00:14:45 +0200215static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100216 struct cpuidle_device *dev)
217{return -ENODEV; }
Jacob Panbb8313b2016-11-28 23:03:04 -0800218static inline void cpuidle_use_deepest_state(bool enable)
219{
220}
Len Brown4f86d3a2007-10-03 18:58:00 -0400221#endif
222
Rafael J. Wysockifaad3842015-05-10 01:18:03 +0200223/* kernel/sched/idle.c */
224extern void sched_idle_set_state(struct cpuidle_state *idle_state);
Rafael J. Wysocki827a5ae2015-05-10 01:18:46 +0200225extern void default_idle_call(void);
Rafael J. Wysockifaad3842015-05-10 01:18:03 +0200226
Colin Cross20ff51a2012-05-07 17:57:42 -0700227#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
228void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a);
Arnd Bergmannc7a9b092012-08-15 20:51:54 +0000229#else
230static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
231{
232}
Colin Cross20ff51a2012-05-07 17:57:42 -0700233#endif
234
Rafael J. Wysockid7212cf2018-02-12 11:34:22 +0100235#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +0200236void cpuidle_poll_state_init(struct cpuidle_driver *drv);
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +0200237#else
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +0200238static inline void cpuidle_poll_state_init(struct cpuidle_driver *drv) {}
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +0200239#endif
240
Len Brown4f86d3a2007-10-03 18:58:00 -0400241/******************************
242 * CPUIDLE GOVERNOR INTERFACE *
243 ******************************/
244
245struct cpuidle_governor {
246 char name[CPUIDLE_NAME_LEN];
247 struct list_head governor_list;
248 unsigned int rating;
249
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530250 int (*enable) (struct cpuidle_driver *drv,
251 struct cpuidle_device *dev);
252 void (*disable) (struct cpuidle_driver *drv,
253 struct cpuidle_device *dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400254
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530255 int (*select) (struct cpuidle_driver *drv,
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +0100256 struct cpuidle_device *dev,
257 bool *stop_tick);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530258 void (*reflect) (struct cpuidle_device *dev, int index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400259};
260
261#ifdef CONFIG_CPU_IDLE
Len Brown4f86d3a2007-10-03 18:58:00 -0400262extern int cpuidle_register_governor(struct cpuidle_governor *gov);
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200263extern int cpuidle_governor_latency_req(unsigned int cpu);
Len Brown4f86d3a2007-10-03 18:58:00 -0400264#else
Len Brown4f86d3a2007-10-03 18:58:00 -0400265static inline int cpuidle_register_governor(struct cpuidle_governor *gov)
266{return 0;}
Len Brown4f86d3a2007-10-03 18:58:00 -0400267#endif
268
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100269#define __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, \
270 idx, \
271 state, \
272 is_retention) \
Prashanth Prakashdb50a742017-11-15 10:11:49 -0700273({ \
274 int __ret = 0; \
275 \
276 if (!idx) { \
277 cpu_do_idle(); \
278 return idx; \
279 } \
280 \
281 if (!is_retention) \
282 __ret = cpu_pm_enter(); \
283 if (!__ret) { \
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100284 __ret = low_level_idle_enter(state); \
Prashanth Prakashdb50a742017-11-15 10:11:49 -0700285 if (!is_retention) \
286 cpu_pm_exit(); \
287 } \
288 \
289 __ret ? -1 : idx; \
Sudeep Holla220276e2016-07-19 18:52:56 +0100290})
291
Prashanth Prakashdb50a742017-11-15 10:11:49 -0700292#define CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx) \
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100293 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 0)
Prashanth Prakashdb50a742017-11-15 10:11:49 -0700294
295#define CPU_PM_CPU_IDLE_ENTER_RETENTION(low_level_idle_enter, idx) \
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100296 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, idx, 1)
297
298#define CPU_PM_CPU_IDLE_ENTER_PARAM(low_level_idle_enter, idx, state) \
299 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 0)
300
301#define CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(low_level_idle_enter, idx, state) \
302 __CPU_PM_CPU_IDLE_ENTER(low_level_idle_enter, idx, state, 1)
Prashanth Prakashdb50a742017-11-15 10:11:49 -0700303
Len Brown4f86d3a2007-10-03 18:58:00 -0400304#endif /* _LINUX_CPUIDLE_H */