blob: 4d534582514e014b5fdb3fc5e0b9db7e52c7b306 [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
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 Lezcanob60e6a02013-03-21 12:21:31 +000011#include <linux/clockchips.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040012#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020016#include <linux/pm_qos.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040017#include <linux/cpu.h>
18#include <linux/cpuidle.h>
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -080019#include <linux/ktime.h>
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070020#include <linux/hrtimer.h>
Paul Gortmaker884b17e2011-08-29 17:52:39 -040021#include <linux/module.h>
Rafael J. Wysocki38106312015-02-12 23:33:15 +010022#include <linux/suspend.h>
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010023#include <linux/tick.h>
Arjan van de Ven288f0232009-09-19 13:35:33 +020024#include <trace/events/power.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040025
26#include "cpuidle.h"
27
28DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Daniel Lezcano4c637b22013-04-23 08:54:33 +000029DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -040030
31DEFINE_MUTEX(cpuidle_lock);
32LIST_HEAD(cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040033
34static int enabled_devices;
Len Brown62027ae2011-04-01 18:13:10 -040035static int off __read_mostly;
Len Browna0bfa132011-04-01 19:34:59 -040036static int initialized __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040037
38int cpuidle_disabled(void)
39{
40 return off;
41}
Len Brownd91ee582011-04-01 18:28:35 -040042void disable_cpuidle(void)
43{
44 off = 1;
45}
Len Brown4f86d3a2007-10-03 18:58:00 -040046
47/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010048 * cpuidle_play_dead - cpu off-lining
49 *
Toshi Kaniee01e662012-03-31 21:37:02 -060050 * Returns in case of an error or no driver
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010051 */
52int cpuidle_play_dead(void)
53{
54 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +000055 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010056 int i;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010057
Toshi Kaniee01e662012-03-31 21:37:02 -060058 if (!drv)
59 return -ENODEV;
60
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010061 /* Find lowest-power state that supports long-term idle */
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010062 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 Ostrovsky1a022e32012-03-13 19:55:09 +010065
66 return -ENODEV;
67}
68
69/**
Rafael J. Wysocki38106312015-02-12 23:33:15 +010070 * cpuidle_find_deepest_state - Find deepest state meeting specific conditions.
71 * @drv: cpuidle driver for the given CPU.
72 * @dev: cpuidle device for the given CPU.
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010073 * @freeze: Whether or not the state should be suitable for suspend-to-idle.
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020074 */
75static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010076 struct cpuidle_device *dev, bool freeze)
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020077{
78 unsigned int latency_req = 0;
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010079 int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020080
81 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
82 struct cpuidle_state *s = &drv->states[i];
83 struct cpuidle_state_usage *su = &dev->states_usage[i];
84
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010085 if (s->disabled || su->disable || s->exit_latency <= latency_req
86 || (freeze && !s->enter_freeze))
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020087 continue;
88
89 latency_req = s->exit_latency;
90 ret = i;
91 }
92 return ret;
93}
94
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +010095static void enter_freeze_proper(struct cpuidle_driver *drv,
96 struct cpuidle_device *dev, int index)
97{
98 tick_freeze();
99 /*
100 * The state used here cannot be a "coupled" one, because the "coupled"
101 * cpuidle mechanism enables interrupts and doing that with timekeeping
102 * suspended is generally unsafe.
103 */
104 drv->states[index].enter_freeze(dev, drv, index);
105 WARN_ON(!irqs_disabled());
106 /*
107 * timekeeping_resume() that will be called by tick_unfreeze() for the
108 * last CPU executing it calls functions containing RCU read-side
109 * critical sections, so tell RCU about that.
110 */
111 RCU_NONIDLE(tick_unfreeze());
112}
113
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +0200114/**
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100115 * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
116 *
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100117 * If there are states with the ->enter_freeze callback, find the deepest of
118 * them and enter it with frozen tick. Otherwise, find the deepest state
119 * available and enter it normally.
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100120 */
121void cpuidle_enter_freeze(void)
122{
123 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
124 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
125 int index;
126
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100127 /*
128 * Find the deepest state with ->enter_freeze present, which guarantees
129 * that interrupts won't be enabled when it exits and allows the tick to
130 * be frozen safely.
131 */
132 index = cpuidle_find_deepest_state(drv, dev, true);
133 if (index >= 0) {
134 enter_freeze_proper(drv, dev, index);
135 return;
136 }
137
138 /*
139 * It is not safe to freeze the tick, find the deepest state available
140 * at all and try to enter it normally.
141 */
142 index = cpuidle_find_deepest_state(drv, dev, false);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100143 if (index >= 0)
144 cpuidle_enter(drv, dev, index);
145 else
146 arch_cpu_idle();
147
148 /* Interrupts are enabled again here. */
149 local_irq_disable();
150}
151
152/**
Colin Cross56cfbf72012-05-07 17:57:39 -0700153 * cpuidle_enter_state - enter the state and update stats
154 * @dev: cpuidle device for this cpu
155 * @drv: cpuidle driver for this cpu
156 * @next_state: index into drv->states of the state to enter
157 */
158int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000159 int index)
Colin Cross56cfbf72012-05-07 17:57:39 -0700160{
161 int entered_state;
162
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000163 struct cpuidle_state *target_state = &drv->states[index];
164 ktime_t time_start, time_end;
165 s64 diff;
166
Sandeep Tripathy30fe6882014-07-02 15:00:58 +0530167 trace_cpu_idle_rcuidle(index, dev->cpu);
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000168 time_start = ktime_get();
169
170 entered_state = target_state->enter(dev, drv, index);
171
172 time_end = ktime_get();
Sandeep Tripathy30fe6882014-07-02 15:00:58 +0530173 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000174
Paul Burton0b89e9a2014-03-06 11:02:01 +0000175 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
176 local_irq_enable();
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000177
178 diff = ktime_to_us(ktime_sub(time_end, time_start));
179 if (diff > INT_MAX)
180 diff = INT_MAX;
181
182 dev->last_residency = (int) diff;
Colin Cross56cfbf72012-05-07 17:57:39 -0700183
184 if (entered_state >= 0) {
185 /* Update cpuidle counters */
186 /* This can be moved to within driver enter routine
187 * but that results in multiple copies of same code.
188 */
Julius Wernera474a512012-11-27 14:17:58 +0100189 dev->states_usage[entered_state].time += dev->last_residency;
Colin Cross56cfbf72012-05-07 17:57:39 -0700190 dev->states_usage[entered_state].usage++;
191 } else {
192 dev->last_residency = 0;
193 }
194
195 return entered_state;
196}
197
198/**
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100199 * cpuidle_select - ask the cpuidle framework to choose an idle state
Len Brown4f86d3a2007-10-03 18:58:00 -0400200 *
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100201 * @drv: the cpuidle driver
202 * @dev: the cpuidle device
203 *
204 * Returns the index of the idle state.
Len Brown4f86d3a2007-10-03 18:58:00 -0400205 */
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100206int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400207{
Rafael J. Wysocki52c324f2014-05-01 00:13:47 +0200208 if (off || !initialized)
209 return -ENODEV;
210
211 if (!drv || !dev || !dev->enabled)
212 return -EBUSY;
213
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100214 return cpuidle_curr_governor->select(drv, dev);
215}
Len Brown4f86d3a2007-10-03 18:58:00 -0400216
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100217/**
218 * cpuidle_enter - enter into the specified idle state
219 *
220 * @drv: the cpuidle driver tied with the cpu
221 * @dev: the cpuidle device
222 * @index: the index in the idle state table
223 *
224 * Returns the index in the idle state, < 0 in case of error.
225 * The error code depends on the backend driver
226 */
227int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
228 int index)
229{
230 if (cpuidle_state_is_coupled(dev, drv, index))
231 return cpuidle_enter_state_coupled(dev, drv, index);
232 return cpuidle_enter_state(dev, drv, index);
233}
Len Browna0bfa132011-04-01 19:34:59 -0400234
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100235/**
236 * cpuidle_reflect - tell the underlying governor what was the state
237 * we were in
238 *
239 * @dev : the cpuidle device
240 * @index: the index in the idle state table
241 *
242 */
243void cpuidle_reflect(struct cpuidle_device *dev, int index)
244{
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100245 if (cpuidle_curr_governor->reflect)
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100246 cpuidle_curr_governor->reflect(dev, index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400247}
248
249/**
250 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
251 */
252void cpuidle_install_idle_handler(void)
253{
Len Browna0bfa132011-04-01 19:34:59 -0400254 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400255 /* Make sure all changes finished before we switch to new idle */
256 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400257 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400258 }
259}
260
261/**
262 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
263 */
264void cpuidle_uninstall_idle_handler(void)
265{
Len Browna0bfa132011-04-01 19:34:59 -0400266 if (enabled_devices) {
267 initialized = 0;
Chuansheng Liu2ed903c2014-09-04 15:17:55 +0800268 wake_up_all_idle_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400269 }
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400270
271 /*
272 * Make sure external observers (such as the scheduler)
273 * are done looking at pointed idle states.
274 */
275 synchronize_rcu();
Len Brown4f86d3a2007-10-03 18:58:00 -0400276}
277
278/**
279 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
280 */
281void cpuidle_pause_and_lock(void)
282{
283 mutex_lock(&cpuidle_lock);
284 cpuidle_uninstall_idle_handler();
285}
286
287EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
288
289/**
290 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
291 */
292void cpuidle_resume_and_unlock(void)
293{
294 cpuidle_install_idle_handler();
295 mutex_unlock(&cpuidle_lock);
296}
297
298EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
299
Preeti U Murthy8651f972012-07-09 10:12:56 +0200300/* Currently used in suspend/resume path to suspend cpuidle */
301void cpuidle_pause(void)
302{
303 mutex_lock(&cpuidle_lock);
304 cpuidle_uninstall_idle_handler();
305 mutex_unlock(&cpuidle_lock);
306}
307
308/* Currently used in suspend/resume path to resume cpuidle */
309void cpuidle_resume(void)
310{
311 mutex_lock(&cpuidle_lock);
312 cpuidle_install_idle_handler();
313 mutex_unlock(&cpuidle_lock);
314}
315
Len Brown4f86d3a2007-10-03 18:58:00 -0400316/**
317 * cpuidle_enable_device - enables idle PM for a CPU
318 * @dev: the CPU
319 *
320 * This function must be called between cpuidle_pause_and_lock and
321 * cpuidle_resume_and_unlock when used externally.
322 */
323int cpuidle_enable_device(struct cpuidle_device *dev)
324{
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200325 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000326 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400327
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700328 if (!dev)
329 return -EINVAL;
330
Len Brown4f86d3a2007-10-03 18:58:00 -0400331 if (dev->enabled)
332 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000333
334 drv = cpuidle_get_cpu_driver(dev);
335
Robert Leee1689792012-03-20 15:22:42 -0500336 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400337 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000338
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200339 if (!dev->registered)
340 return -EINVAL;
341
Len Brown4f86d3a2007-10-03 18:58:00 -0400342 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200343 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400344
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000345 ret = cpuidle_add_device_sysfs(dev);
346 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400347 return ret;
348
349 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500350 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400351 goto fail_sysfs;
352
Len Brown4f86d3a2007-10-03 18:58:00 -0400353 smp_wmb();
354
355 dev->enabled = 1;
356
357 enabled_devices++;
358 return 0;
359
360fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000361 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400362
363 return ret;
364}
365
366EXPORT_SYMBOL_GPL(cpuidle_enable_device);
367
368/**
369 * cpuidle_disable_device - disables idle PM for a CPU
370 * @dev: the CPU
371 *
372 * This function must be called between cpuidle_pause_and_lock and
373 * cpuidle_resume_and_unlock when used externally.
374 */
375void cpuidle_disable_device(struct cpuidle_device *dev)
376{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000377 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
378
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530379 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400380 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000381
382 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400383 return;
384
385 dev->enabled = 0;
386
387 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000388 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400389
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000390 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400391 enabled_devices--;
392}
393
394EXPORT_SYMBOL_GPL(cpuidle_disable_device);
395
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200396static void __cpuidle_unregister_device(struct cpuidle_device *dev)
397{
398 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
399
400 list_del(&dev->device_list);
401 per_cpu(cpuidle_devices, dev->cpu) = NULL;
402 module_put(drv->owner);
403}
404
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530405static void __cpuidle_device_init(struct cpuidle_device *dev)
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200406{
407 memset(dev->states_usage, 0, sizeof(dev->states_usage));
408 dev->last_residency = 0;
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200409}
410
Len Brown4f86d3a2007-10-03 18:58:00 -0400411/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400412 * __cpuidle_register_device - internal register function called before register
413 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400414 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400415 *
416 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400417 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400418static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400419{
420 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000421 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400422
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000423 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400424 return -EINVAL;
425
Len Brown4f86d3a2007-10-03 18:58:00 -0400426 per_cpu(cpuidle_devices, dev->cpu) = dev;
427 list_add(&dev->device_list, &cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400428
Colin Cross4126c012012-05-07 17:57:41 -0700429 ret = cpuidle_coupled_register_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530430 if (ret)
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200431 __cpuidle_unregister_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530432 else
433 dev->registered = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400434
Viresh Kumar47182662013-10-03 21:26:46 +0530435 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400436}
437
438/**
439 * cpuidle_register_device - registers a CPU's idle PM feature
440 * @dev: the cpu
441 */
442int cpuidle_register_device(struct cpuidle_device *dev)
443{
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200444 int ret = -EBUSY;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400445
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700446 if (!dev)
447 return -EINVAL;
448
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400449 mutex_lock(&cpuidle_lock);
450
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200451 if (dev->registered)
452 goto out_unlock;
453
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530454 __cpuidle_device_init(dev);
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200455
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200456 ret = __cpuidle_register_device(dev);
457 if (ret)
458 goto out_unlock;
459
460 ret = cpuidle_add_sysfs(dev);
461 if (ret)
462 goto out_unregister;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400463
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200464 ret = cpuidle_enable_device(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200465 if (ret)
466 goto out_sysfs;
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200467
Len Brown4f86d3a2007-10-03 18:58:00 -0400468 cpuidle_install_idle_handler();
469
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200470out_unlock:
Len Brown4f86d3a2007-10-03 18:58:00 -0400471 mutex_unlock(&cpuidle_lock);
472
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200473 return ret;
474
475out_sysfs:
476 cpuidle_remove_sysfs(dev);
477out_unregister:
478 __cpuidle_unregister_device(dev);
479 goto out_unlock;
Len Brown4f86d3a2007-10-03 18:58:00 -0400480}
481
482EXPORT_SYMBOL_GPL(cpuidle_register_device);
483
484/**
485 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
486 * @dev: the cpu
487 */
488void cpuidle_unregister_device(struct cpuidle_device *dev)
489{
Konrad Rzeszutek Wilk813e8e32013-12-03 10:59:58 -0500490 if (!dev || dev->registered == 0)
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400491 return;
492
Len Brown4f86d3a2007-10-03 18:58:00 -0400493 cpuidle_pause_and_lock();
494
495 cpuidle_disable_device(dev);
496
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200497 cpuidle_remove_sysfs(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200498
499 __cpuidle_unregister_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400500
Colin Cross4126c012012-05-07 17:57:41 -0700501 cpuidle_coupled_unregister_device(dev);
502
Len Brown4f86d3a2007-10-03 18:58:00 -0400503 cpuidle_resume_and_unlock();
Len Brown4f86d3a2007-10-03 18:58:00 -0400504}
505
506EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
507
Daniel Lezcano1c192d02013-04-23 15:28:44 +0000508/**
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000509 * cpuidle_unregister: unregister a driver and the devices. This function
510 * can be used only if the driver has been previously registered through
511 * the cpuidle_register function.
512 *
513 * @drv: a valid pointer to a struct cpuidle_driver
514 */
515void cpuidle_unregister(struct cpuidle_driver *drv)
516{
517 int cpu;
518 struct cpuidle_device *device;
519
Daniel Lezcano82467a52013-06-07 21:53:09 +0000520 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000521 device = &per_cpu(cpuidle_dev, cpu);
522 cpuidle_unregister_device(device);
523 }
524
525 cpuidle_unregister_driver(drv);
526}
527EXPORT_SYMBOL_GPL(cpuidle_unregister);
528
529/**
530 * cpuidle_register: registers the driver and the cpu devices with the
531 * coupled_cpus passed as parameter. This function is used for all common
532 * initialization pattern there are in the arch specific drivers. The
533 * devices is globally defined in this file.
534 *
535 * @drv : a valid pointer to a struct cpuidle_driver
536 * @coupled_cpus: a cpumask for the coupled states
537 *
538 * Returns 0 on success, < 0 otherwise
539 */
540int cpuidle_register(struct cpuidle_driver *drv,
541 const struct cpumask *const coupled_cpus)
542{
543 int ret, cpu;
544 struct cpuidle_device *device;
545
546 ret = cpuidle_register_driver(drv);
547 if (ret) {
548 pr_err("failed to register cpuidle driver\n");
549 return ret;
550 }
551
Daniel Lezcano82467a52013-06-07 21:53:09 +0000552 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000553 device = &per_cpu(cpuidle_dev, cpu);
554 device->cpu = cpu;
555
556#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
557 /*
Viresh Kumarcaf4a362013-10-03 21:26:41 +0530558 * On multiplatform for ARM, the coupled idle states could be
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000559 * enabled in the kernel even if the cpuidle driver does not
560 * use it. Note, coupled_cpus is a struct copy.
561 */
562 if (coupled_cpus)
563 device->coupled_cpus = *coupled_cpus;
564#endif
565 ret = cpuidle_register_device(device);
566 if (!ret)
567 continue;
568
569 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
570
571 cpuidle_unregister(drv);
572 break;
573 }
574
575 return ret;
576}
577EXPORT_SYMBOL_GPL(cpuidle_register);
578
Len Brown4f86d3a2007-10-03 18:58:00 -0400579#ifdef CONFIG_SMP
580
Len Brown4f86d3a2007-10-03 18:58:00 -0400581/*
582 * This function gets called when a part of the kernel has a new latency
583 * requirement. This means we need to get all processors out of their C-state,
584 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
585 * wakes them all right up.
586 */
587static int cpuidle_latency_notify(struct notifier_block *b,
588 unsigned long l, void *v)
589{
Chuansheng Liu2ed903c2014-09-04 15:17:55 +0800590 wake_up_all_idle_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400591 return NOTIFY_OK;
592}
593
594static struct notifier_block cpuidle_latency_notifier = {
595 .notifier_call = cpuidle_latency_notify,
596};
597
Mark Grossd82b3512008-02-04 22:30:08 -0800598static inline void latency_notifier_init(struct notifier_block *n)
599{
600 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
601}
Len Brown4f86d3a2007-10-03 18:58:00 -0400602
603#else /* CONFIG_SMP */
604
605#define latency_notifier_init(x) do { } while (0)
606
607#endif /* CONFIG_SMP */
608
609/**
610 * cpuidle_init - core initializer
611 */
612static int __init cpuidle_init(void)
613{
614 int ret;
615
Len Brown62027ae2011-04-01 18:13:10 -0400616 if (cpuidle_disabled())
617 return -ENODEV;
618
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800619 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400620 if (ret)
621 return ret;
622
623 latency_notifier_init(&cpuidle_latency_notifier);
624
625 return 0;
626}
627
Len Brown62027ae2011-04-01 18:13:10 -0400628module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400629core_initcall(cpuidle_init);