Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * cpuidle.c - core cpuidle infrastructure |
| 3 | * |
| 4 | * (C) 2006-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 | |
Daniel Lezcano | b60e6a0 | 2013-03-21 12:21:31 +0000 | [diff] [blame] | 11 | #include <linux/clockchips.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/notifier.h> |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 16 | #include <linux/pm_qos.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 17 | #include <linux/cpu.h> |
| 18 | #include <linux/cpuidle.h> |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 19 | #include <linux/ktime.h> |
Arjan van de Ven | 2e94d1f | 2008-09-10 16:06:00 -0700 | [diff] [blame] | 20 | #include <linux/hrtimer.h> |
Paul Gortmaker | 884b17e | 2011-08-29 17:52:39 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Arjan van de Ven | 288f023 | 2009-09-19 13:35:33 +0200 | [diff] [blame] | 22 | #include <trace/events/power.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 23 | |
| 24 | #include "cpuidle.h" |
| 25 | |
| 26 | DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 27 | DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 28 | |
| 29 | DEFINE_MUTEX(cpuidle_lock); |
| 30 | LIST_HEAD(cpuidle_detected_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 31 | |
| 32 | static int enabled_devices; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 33 | static int off __read_mostly; |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 34 | static int initialized __read_mostly; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 35 | |
| 36 | int cpuidle_disabled(void) |
| 37 | { |
| 38 | return off; |
| 39 | } |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 40 | void disable_cpuidle(void) |
| 41 | { |
| 42 | off = 1; |
| 43 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 44 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 45 | static int __cpuidle_register_device(struct cpuidle_device *dev); |
| 46 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 47 | /** |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 48 | * cpuidle_play_dead - cpu off-lining |
| 49 | * |
Toshi Kani | ee01e66 | 2012-03-31 21:37:02 -0600 | [diff] [blame] | 50 | * Returns in case of an error or no driver |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 51 | */ |
| 52 | int cpuidle_play_dead(void) |
| 53 | { |
| 54 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 55 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
Daniel Lezcano | 8aef33a | 2013-01-15 14:18:04 +0100 | [diff] [blame] | 56 | int i; |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 57 | |
Toshi Kani | ee01e66 | 2012-03-31 21:37:02 -0600 | [diff] [blame] | 58 | if (!drv) |
| 59 | return -ENODEV; |
| 60 | |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 61 | /* Find lowest-power state that supports long-term idle */ |
Daniel Lezcano | 8aef33a | 2013-01-15 14:18:04 +0100 | [diff] [blame] | 62 | for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) |
| 63 | if (drv->states[i].enter_dead) |
| 64 | return drv->states[i].enter_dead(dev, i); |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 65 | |
| 66 | return -ENODEV; |
| 67 | } |
| 68 | |
| 69 | /** |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 70 | * cpuidle_enter_state - enter the state and update stats |
| 71 | * @dev: cpuidle device for this cpu |
| 72 | * @drv: cpuidle driver for this cpu |
| 73 | * @next_state: index into drv->states of the state to enter |
| 74 | */ |
| 75 | int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 76 | int index) |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 77 | { |
| 78 | int entered_state; |
| 79 | |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 80 | struct cpuidle_state *target_state = &drv->states[index]; |
| 81 | ktime_t time_start, time_end; |
| 82 | s64 diff; |
| 83 | |
| 84 | time_start = ktime_get(); |
| 85 | |
| 86 | entered_state = target_state->enter(dev, drv, index); |
| 87 | |
| 88 | time_end = ktime_get(); |
| 89 | |
| 90 | local_irq_enable(); |
| 91 | |
| 92 | diff = ktime_to_us(ktime_sub(time_end, time_start)); |
| 93 | if (diff > INT_MAX) |
| 94 | diff = INT_MAX; |
| 95 | |
| 96 | dev->last_residency = (int) diff; |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 97 | |
| 98 | if (entered_state >= 0) { |
| 99 | /* Update cpuidle counters */ |
| 100 | /* This can be moved to within driver enter routine |
| 101 | * but that results in multiple copies of same code. |
| 102 | */ |
Julius Werner | a474a51 | 2012-11-27 14:17:58 +0100 | [diff] [blame] | 103 | dev->states_usage[entered_state].time += dev->last_residency; |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 104 | dev->states_usage[entered_state].usage++; |
| 105 | } else { |
| 106 | dev->last_residency = 0; |
| 107 | } |
| 108 | |
| 109 | return entered_state; |
| 110 | } |
| 111 | |
| 112 | /** |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 113 | * cpuidle_idle_call - the main idle loop |
| 114 | * |
| 115 | * NOTE: no locks or semaphores should be used here |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 116 | * return non-zero on failure |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 117 | */ |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 118 | int cpuidle_idle_call(void) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 119 | { |
Christoph Lameter | 4a6f4fe | 2010-12-06 11:16:24 -0600 | [diff] [blame] | 120 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 121 | struct cpuidle_driver *drv; |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 122 | int next_state, entered_state; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 123 | |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 124 | if (off) |
| 125 | return -ENODEV; |
| 126 | |
| 127 | if (!initialized) |
| 128 | return -ENODEV; |
| 129 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 130 | /* check if the device is ready */ |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 131 | if (!dev || !dev->enabled) |
| 132 | return -EBUSY; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 133 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 134 | drv = cpuidle_get_cpu_driver(dev); |
| 135 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 136 | /* ask the governor for the next state */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 137 | next_state = cpuidle_curr_governor->select(drv, dev); |
Kevin Hilman | 246eb7f | 2009-10-26 16:50:18 -0700 | [diff] [blame] | 138 | if (need_resched()) { |
Youquan Song | d73d68d | 2012-10-26 12:26:59 +0200 | [diff] [blame] | 139 | dev->last_residency = 0; |
| 140 | /* give the governor an opportunity to reflect on the outcome */ |
| 141 | if (cpuidle_curr_governor->reflect) |
| 142 | cpuidle_curr_governor->reflect(dev, next_state); |
Kevin Hilman | 246eb7f | 2009-10-26 16:50:18 -0700 | [diff] [blame] | 143 | local_irq_enable(); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 144 | return 0; |
Kevin Hilman | 246eb7f | 2009-10-26 16:50:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Steven Rostedt | 76027ea | 2012-02-07 09:46:01 -0500 | [diff] [blame] | 147 | trace_cpu_idle_rcuidle(next_state, dev->cpu); |
Thomas Renninger | f77cfe4 | 2011-01-07 11:29:44 +0100 | [diff] [blame] | 148 | |
Daniel Lezcano | b60e6a0 | 2013-03-21 12:21:31 +0000 | [diff] [blame] | 149 | if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) |
| 150 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, |
| 151 | &dev->cpu); |
| 152 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 153 | if (cpuidle_state_is_coupled(dev, drv, next_state)) |
| 154 | entered_state = cpuidle_enter_state_coupled(dev, drv, |
| 155 | next_state); |
| 156 | else |
| 157 | entered_state = cpuidle_enter_state(dev, drv, next_state); |
Thomas Renninger | f77cfe4 | 2011-01-07 11:29:44 +0100 | [diff] [blame] | 158 | |
Daniel Lezcano | b60e6a0 | 2013-03-21 12:21:31 +0000 | [diff] [blame] | 159 | if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) |
| 160 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, |
| 161 | &dev->cpu); |
| 162 | |
Steven Rostedt | 76027ea | 2012-02-07 09:46:01 -0500 | [diff] [blame] | 163 | trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); |
Thomas Renninger | f77cfe4 | 2011-01-07 11:29:44 +0100 | [diff] [blame] | 164 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 165 | /* give the governor an opportunity to reflect on the outcome */ |
| 166 | if (cpuidle_curr_governor->reflect) |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 167 | cpuidle_curr_governor->reflect(dev, entered_state); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 168 | |
| 169 | return 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler |
| 174 | */ |
| 175 | void cpuidle_install_idle_handler(void) |
| 176 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 177 | if (enabled_devices) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 178 | /* Make sure all changes finished before we switch to new idle */ |
| 179 | smp_wmb(); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 180 | initialized = 1; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler |
| 186 | */ |
| 187 | void cpuidle_uninstall_idle_handler(void) |
| 188 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 189 | if (enabled_devices) { |
| 190 | initialized = 0; |
Thomas Gleixner | 4a16251 | 2012-05-07 17:59:48 +0000 | [diff] [blame] | 191 | kick_all_cpus_sync(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE |
| 197 | */ |
| 198 | void cpuidle_pause_and_lock(void) |
| 199 | { |
| 200 | mutex_lock(&cpuidle_lock); |
| 201 | cpuidle_uninstall_idle_handler(); |
| 202 | } |
| 203 | |
| 204 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); |
| 205 | |
| 206 | /** |
| 207 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation |
| 208 | */ |
| 209 | void cpuidle_resume_and_unlock(void) |
| 210 | { |
| 211 | cpuidle_install_idle_handler(); |
| 212 | mutex_unlock(&cpuidle_lock); |
| 213 | } |
| 214 | |
| 215 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); |
| 216 | |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 217 | /* Currently used in suspend/resume path to suspend cpuidle */ |
| 218 | void cpuidle_pause(void) |
| 219 | { |
| 220 | mutex_lock(&cpuidle_lock); |
| 221 | cpuidle_uninstall_idle_handler(); |
| 222 | mutex_unlock(&cpuidle_lock); |
| 223 | } |
| 224 | |
| 225 | /* Currently used in suspend/resume path to resume cpuidle */ |
| 226 | void cpuidle_resume(void) |
| 227 | { |
| 228 | mutex_lock(&cpuidle_lock); |
| 229 | cpuidle_install_idle_handler(); |
| 230 | mutex_unlock(&cpuidle_lock); |
| 231 | } |
| 232 | |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 233 | #ifdef CONFIG_ARCH_HAS_CPU_RELAX |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 234 | static int poll_idle(struct cpuidle_device *dev, |
| 235 | struct cpuidle_driver *drv, int index) |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 236 | { |
| 237 | ktime_t t1, t2; |
| 238 | s64 diff; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 239 | |
| 240 | t1 = ktime_get(); |
| 241 | local_irq_enable(); |
| 242 | while (!need_resched()) |
| 243 | cpu_relax(); |
| 244 | |
| 245 | t2 = ktime_get(); |
| 246 | diff = ktime_to_us(ktime_sub(t2, t1)); |
| 247 | if (diff > INT_MAX) |
| 248 | diff = INT_MAX; |
| 249 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 250 | dev->last_residency = (int) diff; |
| 251 | |
| 252 | return index; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 255 | static void poll_idle_init(struct cpuidle_driver *drv) |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 256 | { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 257 | struct cpuidle_state *state = &drv->states[0]; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 258 | |
Thomas Renninger | 720f1c3 | 2011-01-07 11:29:43 +0100 | [diff] [blame] | 259 | snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 260 | snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); |
| 261 | state->exit_latency = 0; |
| 262 | state->target_residency = 0; |
| 263 | state->power_usage = -1; |
Len Brown | d247632 | 2011-01-12 02:34:59 -0500 | [diff] [blame] | 264 | state->flags = 0; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 265 | state->enter = poll_idle; |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 266 | state->disabled = false; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 267 | } |
| 268 | #else |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 269 | static void poll_idle_init(struct cpuidle_driver *drv) {} |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 270 | #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ |
| 271 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 272 | /** |
| 273 | * cpuidle_enable_device - enables idle PM for a CPU |
| 274 | * @dev: the CPU |
| 275 | * |
| 276 | * This function must be called between cpuidle_pause_and_lock and |
| 277 | * cpuidle_resume_and_unlock when used externally. |
| 278 | */ |
| 279 | int cpuidle_enable_device(struct cpuidle_device *dev) |
| 280 | { |
| 281 | int ret, i; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 282 | struct cpuidle_driver *drv; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 283 | |
Srivatsa S. Bhat | 1b0a0e9 | 2012-05-04 14:06:02 -0700 | [diff] [blame] | 284 | if (!dev) |
| 285 | return -EINVAL; |
| 286 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 287 | if (dev->enabled) |
| 288 | return 0; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 289 | |
| 290 | drv = cpuidle_get_cpu_driver(dev); |
| 291 | |
Robert Lee | e168979 | 2012-03-20 15:22:42 -0500 | [diff] [blame] | 292 | if (!drv || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 293 | return -EIO; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 294 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 295 | if (!dev->state_count) |
Daniel Lezcano | fc850f3 | 2012-03-26 14:51:26 +0200 | [diff] [blame] | 296 | dev->state_count = drv->state_count; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 297 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 298 | if (dev->registered == 0) { |
| 299 | ret = __cpuidle_register_device(dev); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | } |
| 303 | |
Robert Lee | e168979 | 2012-03-20 15:22:42 -0500 | [diff] [blame] | 304 | poll_idle_init(drv); |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 305 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 306 | ret = cpuidle_add_device_sysfs(dev); |
| 307 | if (ret) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 308 | return ret; |
| 309 | |
| 310 | if (cpuidle_curr_governor->enable && |
Robert Lee | e168979 | 2012-03-20 15:22:42 -0500 | [diff] [blame] | 311 | (ret = cpuidle_curr_governor->enable(drv, dev))) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 312 | goto fail_sysfs; |
| 313 | |
| 314 | for (i = 0; i < dev->state_count; i++) { |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 315 | dev->states_usage[i].usage = 0; |
| 316 | dev->states_usage[i].time = 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 317 | } |
| 318 | dev->last_residency = 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 319 | |
| 320 | smp_wmb(); |
| 321 | |
| 322 | dev->enabled = 1; |
| 323 | |
| 324 | enabled_devices++; |
| 325 | return 0; |
| 326 | |
| 327 | fail_sysfs: |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 328 | cpuidle_remove_device_sysfs(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | EXPORT_SYMBOL_GPL(cpuidle_enable_device); |
| 334 | |
| 335 | /** |
| 336 | * cpuidle_disable_device - disables idle PM for a CPU |
| 337 | * @dev: the CPU |
| 338 | * |
| 339 | * This function must be called between cpuidle_pause_and_lock and |
| 340 | * cpuidle_resume_and_unlock when used externally. |
| 341 | */ |
| 342 | void cpuidle_disable_device(struct cpuidle_device *dev) |
| 343 | { |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 344 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
| 345 | |
Srivatsa S. Bhat | cf31cd1 | 2012-10-08 13:43:08 +0530 | [diff] [blame] | 346 | if (!dev || !dev->enabled) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 347 | return; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 348 | |
| 349 | if (!drv || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 350 | return; |
| 351 | |
| 352 | dev->enabled = 0; |
| 353 | |
| 354 | if (cpuidle_curr_governor->disable) |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 355 | cpuidle_curr_governor->disable(drv, dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 356 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 357 | cpuidle_remove_device_sysfs(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 358 | enabled_devices--; |
| 359 | } |
| 360 | |
| 361 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); |
| 362 | |
| 363 | /** |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 364 | * __cpuidle_register_device - internal register function called before register |
| 365 | * and enable routines |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 366 | * @dev: the cpu |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 367 | * |
| 368 | * cpuidle_lock mutex must be held before this is called |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 369 | */ |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 370 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 371 | { |
| 372 | int ret; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 373 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 374 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 375 | if (!try_module_get(drv->owner)) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 378 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
| 379 | list_add(&dev->device_list, &cpuidle_detected_devices); |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 380 | ret = cpuidle_add_sysfs(dev); |
Colin Cross | 3af272a | 2012-05-07 17:57:40 -0700 | [diff] [blame] | 381 | if (ret) |
| 382 | goto err_sysfs; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 383 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 384 | ret = cpuidle_coupled_register_device(dev); |
| 385 | if (ret) |
| 386 | goto err_coupled; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 387 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 388 | dev->registered = 1; |
| 389 | return 0; |
Colin Cross | 3af272a | 2012-05-07 17:57:40 -0700 | [diff] [blame] | 390 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 391 | err_coupled: |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 392 | cpuidle_remove_sysfs(dev); |
Colin Cross | 3af272a | 2012-05-07 17:57:40 -0700 | [diff] [blame] | 393 | err_sysfs: |
| 394 | list_del(&dev->device_list); |
| 395 | per_cpu(cpuidle_devices, dev->cpu) = NULL; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 396 | module_put(drv->owner); |
Colin Cross | 3af272a | 2012-05-07 17:57:40 -0700 | [diff] [blame] | 397 | return ret; |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * cpuidle_register_device - registers a CPU's idle PM feature |
| 402 | * @dev: the cpu |
| 403 | */ |
| 404 | int cpuidle_register_device(struct cpuidle_device *dev) |
| 405 | { |
| 406 | int ret; |
| 407 | |
Srivatsa S. Bhat | 1b0a0e9 | 2012-05-04 14:06:02 -0700 | [diff] [blame] | 408 | if (!dev) |
| 409 | return -EINVAL; |
| 410 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 411 | mutex_lock(&cpuidle_lock); |
| 412 | |
| 413 | if ((ret = __cpuidle_register_device(dev))) { |
| 414 | mutex_unlock(&cpuidle_lock); |
| 415 | return ret; |
| 416 | } |
| 417 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 418 | cpuidle_enable_device(dev); |
| 419 | cpuidle_install_idle_handler(); |
| 420 | |
| 421 | mutex_unlock(&cpuidle_lock); |
| 422 | |
| 423 | return 0; |
| 424 | |
| 425 | } |
| 426 | |
| 427 | EXPORT_SYMBOL_GPL(cpuidle_register_device); |
| 428 | |
| 429 | /** |
| 430 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature |
| 431 | * @dev: the cpu |
| 432 | */ |
| 433 | void cpuidle_unregister_device(struct cpuidle_device *dev) |
| 434 | { |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 435 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 436 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 437 | if (dev->registered == 0) |
| 438 | return; |
| 439 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 440 | cpuidle_pause_and_lock(); |
| 441 | |
| 442 | cpuidle_disable_device(dev); |
| 443 | |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 444 | cpuidle_remove_sysfs(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 445 | list_del(&dev->device_list); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 446 | per_cpu(cpuidle_devices, dev->cpu) = NULL; |
| 447 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 448 | cpuidle_coupled_unregister_device(dev); |
| 449 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 450 | cpuidle_resume_and_unlock(); |
| 451 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 452 | module_put(drv->owner); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); |
| 456 | |
Daniel Lezcano | 1c192d0 | 2013-04-23 15:28:44 +0000 | [diff] [blame] | 457 | /** |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 458 | * cpuidle_unregister: unregister a driver and the devices. This function |
| 459 | * can be used only if the driver has been previously registered through |
| 460 | * the cpuidle_register function. |
| 461 | * |
| 462 | * @drv: a valid pointer to a struct cpuidle_driver |
| 463 | */ |
| 464 | void cpuidle_unregister(struct cpuidle_driver *drv) |
| 465 | { |
| 466 | int cpu; |
| 467 | struct cpuidle_device *device; |
| 468 | |
| 469 | for_each_possible_cpu(cpu) { |
| 470 | device = &per_cpu(cpuidle_dev, cpu); |
| 471 | cpuidle_unregister_device(device); |
| 472 | } |
| 473 | |
| 474 | cpuidle_unregister_driver(drv); |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(cpuidle_unregister); |
| 477 | |
| 478 | /** |
| 479 | * cpuidle_register: registers the driver and the cpu devices with the |
| 480 | * coupled_cpus passed as parameter. This function is used for all common |
| 481 | * initialization pattern there are in the arch specific drivers. The |
| 482 | * devices is globally defined in this file. |
| 483 | * |
| 484 | * @drv : a valid pointer to a struct cpuidle_driver |
| 485 | * @coupled_cpus: a cpumask for the coupled states |
| 486 | * |
| 487 | * Returns 0 on success, < 0 otherwise |
| 488 | */ |
| 489 | int cpuidle_register(struct cpuidle_driver *drv, |
| 490 | const struct cpumask *const coupled_cpus) |
| 491 | { |
| 492 | int ret, cpu; |
| 493 | struct cpuidle_device *device; |
| 494 | |
| 495 | ret = cpuidle_register_driver(drv); |
| 496 | if (ret) { |
| 497 | pr_err("failed to register cpuidle driver\n"); |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | for_each_possible_cpu(cpu) { |
| 502 | device = &per_cpu(cpuidle_dev, cpu); |
| 503 | device->cpu = cpu; |
| 504 | |
| 505 | #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED |
| 506 | /* |
| 507 | * On multiplatform for ARM, the coupled idle states could |
| 508 | * enabled in the kernel even if the cpuidle driver does not |
| 509 | * use it. Note, coupled_cpus is a struct copy. |
| 510 | */ |
| 511 | if (coupled_cpus) |
| 512 | device->coupled_cpus = *coupled_cpus; |
| 513 | #endif |
| 514 | ret = cpuidle_register_device(device); |
| 515 | if (!ret) |
| 516 | continue; |
| 517 | |
| 518 | pr_err("Failed to register cpuidle device for cpu%d\n", cpu); |
| 519 | |
| 520 | cpuidle_unregister(drv); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(cpuidle_register); |
| 527 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 528 | #ifdef CONFIG_SMP |
| 529 | |
| 530 | static void smp_callback(void *v) |
| 531 | { |
| 532 | /* we already woke the CPU up, nothing more to do */ |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * This function gets called when a part of the kernel has a new latency |
| 537 | * requirement. This means we need to get all processors out of their C-state, |
| 538 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that |
| 539 | * wakes them all right up. |
| 540 | */ |
| 541 | static int cpuidle_latency_notify(struct notifier_block *b, |
| 542 | unsigned long l, void *v) |
| 543 | { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 544 | smp_call_function(smp_callback, NULL, 1); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 545 | return NOTIFY_OK; |
| 546 | } |
| 547 | |
| 548 | static struct notifier_block cpuidle_latency_notifier = { |
| 549 | .notifier_call = cpuidle_latency_notify, |
| 550 | }; |
| 551 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 552 | static inline void latency_notifier_init(struct notifier_block *n) |
| 553 | { |
| 554 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); |
| 555 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 556 | |
| 557 | #else /* CONFIG_SMP */ |
| 558 | |
| 559 | #define latency_notifier_init(x) do { } while (0) |
| 560 | |
| 561 | #endif /* CONFIG_SMP */ |
| 562 | |
| 563 | /** |
| 564 | * cpuidle_init - core initializer |
| 565 | */ |
| 566 | static int __init cpuidle_init(void) |
| 567 | { |
| 568 | int ret; |
| 569 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 570 | if (cpuidle_disabled()) |
| 571 | return -ENODEV; |
| 572 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 573 | ret = cpuidle_add_interface(cpu_subsys.dev_root); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 574 | if (ret) |
| 575 | return ret; |
| 576 | |
| 577 | latency_notifier_init(&cpuidle_latency_notifier); |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 582 | module_param(off, int, 0444); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 583 | core_initcall(cpuidle_init); |