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