blob: 8c91badff00bd31e32473ed12d137da8f33eb33c [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>
Arjan van de Ven288f0232009-09-19 13:35:33 +020022#include <trace/events/power.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040023
24#include "cpuidle.h"
25
26DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Daniel Lezcano4c637b22013-04-23 08:54:33 +000027DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -040028
29DEFINE_MUTEX(cpuidle_lock);
30LIST_HEAD(cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040031
32static int enabled_devices;
Len Brown62027ae2011-04-01 18:13:10 -040033static int off __read_mostly;
Len Browna0bfa132011-04-01 19:34:59 -040034static int initialized __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040035
36int cpuidle_disabled(void)
37{
38 return off;
39}
Len Brownd91ee582011-04-01 18:28:35 -040040void disable_cpuidle(void)
41{
42 off = 1;
43}
Len Brown4f86d3a2007-10-03 18:58:00 -040044
45/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010046 * cpuidle_play_dead - cpu off-lining
47 *
Toshi Kaniee01e662012-03-31 21:37:02 -060048 * Returns in case of an error or no driver
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010049 */
50int cpuidle_play_dead(void)
51{
52 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +000053 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010054 int i;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010055
Toshi Kaniee01e662012-03-31 21:37:02 -060056 if (!drv)
57 return -ENODEV;
58
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010059 /* Find lowest-power state that supports long-term idle */
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010060 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 Ostrovsky1a022e32012-03-13 19:55:09 +010063
64 return -ENODEV;
65}
66
67/**
Colin Cross56cfbf72012-05-07 17:57:39 -070068 * 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 */
73int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
Daniel Lezcano554c06b2013-04-23 08:54:31 +000074 int index)
Colin Cross56cfbf72012-05-07 17:57:39 -070075{
76 int entered_state;
77
Daniel Lezcano554c06b2013-04-23 08:54:31 +000078 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 Cross56cfbf72012-05-07 17:57:39 -070095
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 Wernera474a512012-11-27 14:17:58 +0100101 dev->states_usage[entered_state].time += dev->last_residency;
Colin Cross56cfbf72012-05-07 17:57:39 -0700102 dev->states_usage[entered_state].usage++;
103 } else {
104 dev->last_residency = 0;
105 }
106
107 return entered_state;
108}
109
110/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400111 * cpuidle_idle_call - the main idle loop
112 *
113 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -0400114 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -0400115 */
Len Browna0bfa132011-04-01 19:34:59 -0400116int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -0400117{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600118 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000119 struct cpuidle_driver *drv;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530120 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -0400121
Len Browna0bfa132011-04-01 19:34:59 -0400122 if (off)
123 return -ENODEV;
124
125 if (!initialized)
126 return -ENODEV;
127
Len Brown4f86d3a2007-10-03 18:58:00 -0400128 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -0400129 if (!dev || !dev->enabled)
130 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -0400131
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000132 drv = cpuidle_get_cpu_driver(dev);
133
Len Brown4f86d3a2007-10-03 18:58:00 -0400134 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530135 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700136 if (need_resched()) {
Youquan Songd73d68d2012-10-26 12:26:59 +0200137 dev->last_residency = 0;
138 /* give the governor an opportunity to reflect on the outcome */
139 if (cpuidle_curr_governor->reflect)
140 cpuidle_curr_governor->reflect(dev, next_state);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700141 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400142 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700143 }
144
Steven Rostedt76027ea2012-02-07 09:46:01 -0500145 trace_cpu_idle_rcuidle(next_state, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100146
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000147 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
148 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
149 &dev->cpu);
150
Colin Cross4126c012012-05-07 17:57:41 -0700151 if (cpuidle_state_is_coupled(dev, drv, next_state))
152 entered_state = cpuidle_enter_state_coupled(dev, drv,
153 next_state);
154 else
155 entered_state = cpuidle_enter_state(dev, drv, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100156
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000157 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
158 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
159 &dev->cpu);
160
Steven Rostedt76027ea2012-02-07 09:46:01 -0500161 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100162
Len Brown4f86d3a2007-10-03 18:58:00 -0400163 /* give the governor an opportunity to reflect on the outcome */
164 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530165 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400166
167 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400168}
169
170/**
171 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
172 */
173void cpuidle_install_idle_handler(void)
174{
Len Browna0bfa132011-04-01 19:34:59 -0400175 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400176 /* Make sure all changes finished before we switch to new idle */
177 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400178 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400179 }
180}
181
182/**
183 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
184 */
185void cpuidle_uninstall_idle_handler(void)
186{
Len Browna0bfa132011-04-01 19:34:59 -0400187 if (enabled_devices) {
188 initialized = 0;
Thomas Gleixner4a162512012-05-07 17:59:48 +0000189 kick_all_cpus_sync();
Len Brown4f86d3a2007-10-03 18:58:00 -0400190 }
191}
192
193/**
194 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
195 */
196void cpuidle_pause_and_lock(void)
197{
198 mutex_lock(&cpuidle_lock);
199 cpuidle_uninstall_idle_handler();
200}
201
202EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
203
204/**
205 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
206 */
207void cpuidle_resume_and_unlock(void)
208{
209 cpuidle_install_idle_handler();
210 mutex_unlock(&cpuidle_lock);
211}
212
213EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
214
Preeti U Murthy8651f972012-07-09 10:12:56 +0200215/* Currently used in suspend/resume path to suspend cpuidle */
216void cpuidle_pause(void)
217{
218 mutex_lock(&cpuidle_lock);
219 cpuidle_uninstall_idle_handler();
220 mutex_unlock(&cpuidle_lock);
221}
222
223/* Currently used in suspend/resume path to resume cpuidle */
224void cpuidle_resume(void)
225{
226 mutex_lock(&cpuidle_lock);
227 cpuidle_install_idle_handler();
228 mutex_unlock(&cpuidle_lock);
229}
230
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100231#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530232static int poll_idle(struct cpuidle_device *dev,
233 struct cpuidle_driver *drv, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100234{
235 ktime_t t1, t2;
236 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100237
238 t1 = ktime_get();
239 local_irq_enable();
240 while (!need_resched())
241 cpu_relax();
242
243 t2 = ktime_get();
244 diff = ktime_to_us(ktime_sub(t2, t1));
245 if (diff > INT_MAX)
246 diff = INT_MAX;
247
Deepthi Dharware978aa72011-10-28 16:20:09 +0530248 dev->last_residency = (int) diff;
249
250 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100251}
252
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530253static void poll_idle_init(struct cpuidle_driver *drv)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100254{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530255 struct cpuidle_state *state = &drv->states[0];
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100256
Thomas Renninger720f1c32011-01-07 11:29:43 +0100257 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100258 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
259 state->exit_latency = 0;
260 state->target_residency = 0;
261 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500262 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100263 state->enter = poll_idle;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200264 state->disabled = false;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100265}
266#else
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530267static void poll_idle_init(struct cpuidle_driver *drv) {}
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100268#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
269
Len Brown4f86d3a2007-10-03 18:58:00 -0400270/**
271 * cpuidle_enable_device - enables idle PM for a CPU
272 * @dev: the CPU
273 *
274 * This function must be called between cpuidle_pause_and_lock and
275 * cpuidle_resume_and_unlock when used externally.
276 */
277int cpuidle_enable_device(struct cpuidle_device *dev)
278{
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200279 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000280 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400281
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700282 if (!dev)
283 return -EINVAL;
284
Len Brown4f86d3a2007-10-03 18:58:00 -0400285 if (dev->enabled)
286 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000287
288 drv = cpuidle_get_cpu_driver(dev);
289
Robert Leee1689792012-03-20 15:22:42 -0500290 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400291 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000292
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200293 if (!dev->registered)
294 return -EINVAL;
295
Len Brown4f86d3a2007-10-03 18:58:00 -0400296 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200297 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400298
Robert Leee1689792012-03-20 15:22:42 -0500299 poll_idle_init(drv);
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100300
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000301 ret = cpuidle_add_device_sysfs(dev);
302 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400303 return ret;
304
305 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500306 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400307 goto fail_sysfs;
308
Len Brown4f86d3a2007-10-03 18:58:00 -0400309 smp_wmb();
310
311 dev->enabled = 1;
312
313 enabled_devices++;
314 return 0;
315
316fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000317 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400318
319 return ret;
320}
321
322EXPORT_SYMBOL_GPL(cpuidle_enable_device);
323
324/**
325 * cpuidle_disable_device - disables idle PM for a CPU
326 * @dev: the CPU
327 *
328 * This function must be called between cpuidle_pause_and_lock and
329 * cpuidle_resume_and_unlock when used externally.
330 */
331void cpuidle_disable_device(struct cpuidle_device *dev)
332{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000333 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
334
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530335 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400336 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000337
338 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400339 return;
340
341 dev->enabled = 0;
342
343 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000344 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400345
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000346 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400347 enabled_devices--;
348}
349
350EXPORT_SYMBOL_GPL(cpuidle_disable_device);
351
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200352static void __cpuidle_unregister_device(struct cpuidle_device *dev)
353{
354 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
355
356 list_del(&dev->device_list);
357 per_cpu(cpuidle_devices, dev->cpu) = NULL;
358 module_put(drv->owner);
359}
360
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530361static void __cpuidle_device_init(struct cpuidle_device *dev)
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200362{
363 memset(dev->states_usage, 0, sizeof(dev->states_usage));
364 dev->last_residency = 0;
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200365}
366
Len Brown4f86d3a2007-10-03 18:58:00 -0400367/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400368 * __cpuidle_register_device - internal register function called before register
369 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400370 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400371 *
372 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400373 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400374static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400375{
376 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000377 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400378
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000379 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400380 return -EINVAL;
381
Len Brown4f86d3a2007-10-03 18:58:00 -0400382 per_cpu(cpuidle_devices, dev->cpu) = dev;
383 list_add(&dev->device_list, &cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400384
Colin Cross4126c012012-05-07 17:57:41 -0700385 ret = cpuidle_coupled_register_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530386 if (ret)
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200387 __cpuidle_unregister_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530388 else
389 dev->registered = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400390
Viresh Kumar47182662013-10-03 21:26:46 +0530391 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400392}
393
394/**
395 * cpuidle_register_device - registers a CPU's idle PM feature
396 * @dev: the cpu
397 */
398int cpuidle_register_device(struct cpuidle_device *dev)
399{
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200400 int ret = -EBUSY;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400401
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700402 if (!dev)
403 return -EINVAL;
404
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400405 mutex_lock(&cpuidle_lock);
406
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200407 if (dev->registered)
408 goto out_unlock;
409
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530410 __cpuidle_device_init(dev);
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200411
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200412 ret = __cpuidle_register_device(dev);
413 if (ret)
414 goto out_unlock;
415
416 ret = cpuidle_add_sysfs(dev);
417 if (ret)
418 goto out_unregister;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400419
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200420 ret = cpuidle_enable_device(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200421 if (ret)
422 goto out_sysfs;
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200423
Len Brown4f86d3a2007-10-03 18:58:00 -0400424 cpuidle_install_idle_handler();
425
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200426out_unlock:
Len Brown4f86d3a2007-10-03 18:58:00 -0400427 mutex_unlock(&cpuidle_lock);
428
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200429 return ret;
430
431out_sysfs:
432 cpuidle_remove_sysfs(dev);
433out_unregister:
434 __cpuidle_unregister_device(dev);
435 goto out_unlock;
Len Brown4f86d3a2007-10-03 18:58:00 -0400436}
437
438EXPORT_SYMBOL_GPL(cpuidle_register_device);
439
440/**
441 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
442 * @dev: the cpu
443 */
444void cpuidle_unregister_device(struct cpuidle_device *dev)
445{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400446 if (dev->registered == 0)
447 return;
448
Len Brown4f86d3a2007-10-03 18:58:00 -0400449 cpuidle_pause_and_lock();
450
451 cpuidle_disable_device(dev);
452
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200453 cpuidle_remove_sysfs(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200454
455 __cpuidle_unregister_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400456
Colin Cross4126c012012-05-07 17:57:41 -0700457 cpuidle_coupled_unregister_device(dev);
458
Len Brown4f86d3a2007-10-03 18:58:00 -0400459 cpuidle_resume_and_unlock();
Len Brown4f86d3a2007-10-03 18:58:00 -0400460}
461
462EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
463
Daniel Lezcano1c192d02013-04-23 15:28:44 +0000464/**
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000465 * cpuidle_unregister: unregister a driver and the devices. This function
466 * can be used only if the driver has been previously registered through
467 * the cpuidle_register function.
468 *
469 * @drv: a valid pointer to a struct cpuidle_driver
470 */
471void cpuidle_unregister(struct cpuidle_driver *drv)
472{
473 int cpu;
474 struct cpuidle_device *device;
475
Daniel Lezcano82467a52013-06-07 21:53:09 +0000476 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000477 device = &per_cpu(cpuidle_dev, cpu);
478 cpuidle_unregister_device(device);
479 }
480
481 cpuidle_unregister_driver(drv);
482}
483EXPORT_SYMBOL_GPL(cpuidle_unregister);
484
485/**
486 * cpuidle_register: registers the driver and the cpu devices with the
487 * coupled_cpus passed as parameter. This function is used for all common
488 * initialization pattern there are in the arch specific drivers. The
489 * devices is globally defined in this file.
490 *
491 * @drv : a valid pointer to a struct cpuidle_driver
492 * @coupled_cpus: a cpumask for the coupled states
493 *
494 * Returns 0 on success, < 0 otherwise
495 */
496int cpuidle_register(struct cpuidle_driver *drv,
497 const struct cpumask *const coupled_cpus)
498{
499 int ret, cpu;
500 struct cpuidle_device *device;
501
502 ret = cpuidle_register_driver(drv);
503 if (ret) {
504 pr_err("failed to register cpuidle driver\n");
505 return ret;
506 }
507
Daniel Lezcano82467a52013-06-07 21:53:09 +0000508 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000509 device = &per_cpu(cpuidle_dev, cpu);
510 device->cpu = cpu;
511
512#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
513 /*
Viresh Kumarcaf4a362013-10-03 21:26:41 +0530514 * On multiplatform for ARM, the coupled idle states could be
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000515 * enabled in the kernel even if the cpuidle driver does not
516 * use it. Note, coupled_cpus is a struct copy.
517 */
518 if (coupled_cpus)
519 device->coupled_cpus = *coupled_cpus;
520#endif
521 ret = cpuidle_register_device(device);
522 if (!ret)
523 continue;
524
525 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
526
527 cpuidle_unregister(drv);
528 break;
529 }
530
531 return ret;
532}
533EXPORT_SYMBOL_GPL(cpuidle_register);
534
Len Brown4f86d3a2007-10-03 18:58:00 -0400535#ifdef CONFIG_SMP
536
537static void smp_callback(void *v)
538{
539 /* we already woke the CPU up, nothing more to do */
540}
541
542/*
543 * This function gets called when a part of the kernel has a new latency
544 * requirement. This means we need to get all processors out of their C-state,
545 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
546 * wakes them all right up.
547 */
548static int cpuidle_latency_notify(struct notifier_block *b,
549 unsigned long l, void *v)
550{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200551 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400552 return NOTIFY_OK;
553}
554
555static struct notifier_block cpuidle_latency_notifier = {
556 .notifier_call = cpuidle_latency_notify,
557};
558
Mark Grossd82b3512008-02-04 22:30:08 -0800559static inline void latency_notifier_init(struct notifier_block *n)
560{
561 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
562}
Len Brown4f86d3a2007-10-03 18:58:00 -0400563
564#else /* CONFIG_SMP */
565
566#define latency_notifier_init(x) do { } while (0)
567
568#endif /* CONFIG_SMP */
569
570/**
571 * cpuidle_init - core initializer
572 */
573static int __init cpuidle_init(void)
574{
575 int ret;
576
Len Brown62027ae2011-04-01 18:13:10 -0400577 if (cpuidle_disabled())
578 return -ENODEV;
579
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800580 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400581 if (ret)
582 return ret;
583
584 latency_notifier_init(&cpuidle_latency_notifier);
585
586 return 0;
587}
588
Len Brown62027ae2011-04-01 18:13:10 -0400589module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400590core_initcall(cpuidle_init);