blob: a55e68f2cfc8bfad02a85957c9d9ddc55823c6fe [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;
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530121 bool broadcast;
Len Brown4f86d3a2007-10-03 18:58:00 -0400122
Viresh Kumar9b29a862013-10-03 21:26:47 +0530123 if (off || !initialized)
Len Browna0bfa132011-04-01 19:34:59 -0400124 return -ENODEV;
125
Len Brown4f86d3a2007-10-03 18:58:00 -0400126 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -0400127 if (!dev || !dev->enabled)
128 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -0400129
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000130 drv = cpuidle_get_cpu_driver(dev);
131
Len Brown4f86d3a2007-10-03 18:58:00 -0400132 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530133 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700134 if (need_resched()) {
Youquan Songd73d68d2012-10-26 12:26:59 +0200135 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 Hilman246eb7f2009-10-26 16:50:18 -0700139 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400140 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700141 }
142
Steven Rostedt76027ea2012-02-07 09:46:01 -0500143 trace_cpu_idle_rcuidle(next_state, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100144
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530145 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
146
147 if (broadcast)
Viresh Kumar6d281e92013-10-03 21:26:49 +0530148 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000149
Colin Cross4126c012012-05-07 17:57:41 -0700150 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 Renningerf77cfe42011-01-07 11:29:44 +0100155
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530156 if (broadcast)
Viresh Kumar6d281e92013-10-03 21:26:49 +0530157 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000158
Steven Rostedt76027ea2012-02-07 09:46:01 -0500159 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100160
Len Brown4f86d3a2007-10-03 18:58:00 -0400161 /* give the governor an opportunity to reflect on the outcome */
162 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530163 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400164
165 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400166}
167
168/**
169 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
170 */
171void cpuidle_install_idle_handler(void)
172{
Len Browna0bfa132011-04-01 19:34:59 -0400173 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400174 /* Make sure all changes finished before we switch to new idle */
175 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400176 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400177 }
178}
179
180/**
181 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
182 */
183void cpuidle_uninstall_idle_handler(void)
184{
Len Browna0bfa132011-04-01 19:34:59 -0400185 if (enabled_devices) {
186 initialized = 0;
Thomas Gleixner4a162512012-05-07 17:59:48 +0000187 kick_all_cpus_sync();
Len Brown4f86d3a2007-10-03 18:58:00 -0400188 }
189}
190
191/**
192 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
193 */
194void cpuidle_pause_and_lock(void)
195{
196 mutex_lock(&cpuidle_lock);
197 cpuidle_uninstall_idle_handler();
198}
199
200EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
201
202/**
203 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
204 */
205void cpuidle_resume_and_unlock(void)
206{
207 cpuidle_install_idle_handler();
208 mutex_unlock(&cpuidle_lock);
209}
210
211EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
212
Preeti U Murthy8651f972012-07-09 10:12:56 +0200213/* Currently used in suspend/resume path to suspend cpuidle */
214void 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 */
222void cpuidle_resume(void)
223{
224 mutex_lock(&cpuidle_lock);
225 cpuidle_install_idle_handler();
226 mutex_unlock(&cpuidle_lock);
227}
228
Len Brown4f86d3a2007-10-03 18:58:00 -0400229/**
230 * cpuidle_enable_device - enables idle PM for a CPU
231 * @dev: the CPU
232 *
233 * This function must be called between cpuidle_pause_and_lock and
234 * cpuidle_resume_and_unlock when used externally.
235 */
236int cpuidle_enable_device(struct cpuidle_device *dev)
237{
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200238 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000239 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400240
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700241 if (!dev)
242 return -EINVAL;
243
Len Brown4f86d3a2007-10-03 18:58:00 -0400244 if (dev->enabled)
245 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000246
247 drv = cpuidle_get_cpu_driver(dev);
248
Robert Leee1689792012-03-20 15:22:42 -0500249 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400250 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000251
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200252 if (!dev->registered)
253 return -EINVAL;
254
Len Brown4f86d3a2007-10-03 18:58:00 -0400255 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200256 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400257
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000258 ret = cpuidle_add_device_sysfs(dev);
259 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400260 return ret;
261
262 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500263 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400264 goto fail_sysfs;
265
Len Brown4f86d3a2007-10-03 18:58:00 -0400266 smp_wmb();
267
268 dev->enabled = 1;
269
270 enabled_devices++;
271 return 0;
272
273fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000274 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400275
276 return ret;
277}
278
279EXPORT_SYMBOL_GPL(cpuidle_enable_device);
280
281/**
282 * cpuidle_disable_device - disables idle PM for a CPU
283 * @dev: the CPU
284 *
285 * This function must be called between cpuidle_pause_and_lock and
286 * cpuidle_resume_and_unlock when used externally.
287 */
288void cpuidle_disable_device(struct cpuidle_device *dev)
289{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000290 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
291
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530292 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400293 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000294
295 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400296 return;
297
298 dev->enabled = 0;
299
300 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000301 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400302
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000303 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400304 enabled_devices--;
305}
306
307EXPORT_SYMBOL_GPL(cpuidle_disable_device);
308
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200309static void __cpuidle_unregister_device(struct cpuidle_device *dev)
310{
311 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
312
313 list_del(&dev->device_list);
314 per_cpu(cpuidle_devices, dev->cpu) = NULL;
315 module_put(drv->owner);
316}
317
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530318static void __cpuidle_device_init(struct cpuidle_device *dev)
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200319{
320 memset(dev->states_usage, 0, sizeof(dev->states_usage));
321 dev->last_residency = 0;
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200322}
323
Len Brown4f86d3a2007-10-03 18:58:00 -0400324/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400325 * __cpuidle_register_device - internal register function called before register
326 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400327 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400328 *
329 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400330 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400331static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400332{
333 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000334 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400335
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000336 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400337 return -EINVAL;
338
Len Brown4f86d3a2007-10-03 18:58:00 -0400339 per_cpu(cpuidle_devices, dev->cpu) = dev;
340 list_add(&dev->device_list, &cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400341
Colin Cross4126c012012-05-07 17:57:41 -0700342 ret = cpuidle_coupled_register_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530343 if (ret)
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200344 __cpuidle_unregister_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530345 else
346 dev->registered = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400347
Viresh Kumar47182662013-10-03 21:26:46 +0530348 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400349}
350
351/**
352 * cpuidle_register_device - registers a CPU's idle PM feature
353 * @dev: the cpu
354 */
355int cpuidle_register_device(struct cpuidle_device *dev)
356{
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200357 int ret = -EBUSY;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400358
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700359 if (!dev)
360 return -EINVAL;
361
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400362 mutex_lock(&cpuidle_lock);
363
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200364 if (dev->registered)
365 goto out_unlock;
366
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530367 __cpuidle_device_init(dev);
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200368
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200369 ret = __cpuidle_register_device(dev);
370 if (ret)
371 goto out_unlock;
372
373 ret = cpuidle_add_sysfs(dev);
374 if (ret)
375 goto out_unregister;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400376
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200377 ret = cpuidle_enable_device(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200378 if (ret)
379 goto out_sysfs;
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200380
Len Brown4f86d3a2007-10-03 18:58:00 -0400381 cpuidle_install_idle_handler();
382
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200383out_unlock:
Len Brown4f86d3a2007-10-03 18:58:00 -0400384 mutex_unlock(&cpuidle_lock);
385
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200386 return ret;
387
388out_sysfs:
389 cpuidle_remove_sysfs(dev);
390out_unregister:
391 __cpuidle_unregister_device(dev);
392 goto out_unlock;
Len Brown4f86d3a2007-10-03 18:58:00 -0400393}
394
395EXPORT_SYMBOL_GPL(cpuidle_register_device);
396
397/**
398 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
399 * @dev: the cpu
400 */
401void cpuidle_unregister_device(struct cpuidle_device *dev)
402{
Konrad Rzeszutek Wilk813e8e32013-12-03 10:59:58 -0500403 if (!dev || dev->registered == 0)
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400404 return;
405
Len Brown4f86d3a2007-10-03 18:58:00 -0400406 cpuidle_pause_and_lock();
407
408 cpuidle_disable_device(dev);
409
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200410 cpuidle_remove_sysfs(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200411
412 __cpuidle_unregister_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400413
Colin Cross4126c012012-05-07 17:57:41 -0700414 cpuidle_coupled_unregister_device(dev);
415
Len Brown4f86d3a2007-10-03 18:58:00 -0400416 cpuidle_resume_and_unlock();
Len Brown4f86d3a2007-10-03 18:58:00 -0400417}
418
419EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
420
Daniel Lezcano1c192d02013-04-23 15:28:44 +0000421/**
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000422 * cpuidle_unregister: unregister a driver and the devices. This function
423 * can be used only if the driver has been previously registered through
424 * the cpuidle_register function.
425 *
426 * @drv: a valid pointer to a struct cpuidle_driver
427 */
428void cpuidle_unregister(struct cpuidle_driver *drv)
429{
430 int cpu;
431 struct cpuidle_device *device;
432
Daniel Lezcano82467a52013-06-07 21:53:09 +0000433 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000434 device = &per_cpu(cpuidle_dev, cpu);
435 cpuidle_unregister_device(device);
436 }
437
438 cpuidle_unregister_driver(drv);
439}
440EXPORT_SYMBOL_GPL(cpuidle_unregister);
441
442/**
443 * cpuidle_register: registers the driver and the cpu devices with the
444 * coupled_cpus passed as parameter. This function is used for all common
445 * initialization pattern there are in the arch specific drivers. The
446 * devices is globally defined in this file.
447 *
448 * @drv : a valid pointer to a struct cpuidle_driver
449 * @coupled_cpus: a cpumask for the coupled states
450 *
451 * Returns 0 on success, < 0 otherwise
452 */
453int cpuidle_register(struct cpuidle_driver *drv,
454 const struct cpumask *const coupled_cpus)
455{
456 int ret, cpu;
457 struct cpuidle_device *device;
458
459 ret = cpuidle_register_driver(drv);
460 if (ret) {
461 pr_err("failed to register cpuidle driver\n");
462 return ret;
463 }
464
Daniel Lezcano82467a52013-06-07 21:53:09 +0000465 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000466 device = &per_cpu(cpuidle_dev, cpu);
467 device->cpu = cpu;
468
469#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
470 /*
Viresh Kumarcaf4a362013-10-03 21:26:41 +0530471 * On multiplatform for ARM, the coupled idle states could be
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000472 * enabled in the kernel even if the cpuidle driver does not
473 * use it. Note, coupled_cpus is a struct copy.
474 */
475 if (coupled_cpus)
476 device->coupled_cpus = *coupled_cpus;
477#endif
478 ret = cpuidle_register_device(device);
479 if (!ret)
480 continue;
481
482 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
483
484 cpuidle_unregister(drv);
485 break;
486 }
487
488 return ret;
489}
490EXPORT_SYMBOL_GPL(cpuidle_register);
491
Len Brown4f86d3a2007-10-03 18:58:00 -0400492#ifdef CONFIG_SMP
493
494static void smp_callback(void *v)
495{
496 /* we already woke the CPU up, nothing more to do */
497}
498
499/*
500 * This function gets called when a part of the kernel has a new latency
501 * requirement. This means we need to get all processors out of their C-state,
502 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
503 * wakes them all right up.
504 */
505static int cpuidle_latency_notify(struct notifier_block *b,
506 unsigned long l, void *v)
507{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200508 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400509 return NOTIFY_OK;
510}
511
512static struct notifier_block cpuidle_latency_notifier = {
513 .notifier_call = cpuidle_latency_notify,
514};
515
Mark Grossd82b3512008-02-04 22:30:08 -0800516static inline void latency_notifier_init(struct notifier_block *n)
517{
518 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
519}
Len Brown4f86d3a2007-10-03 18:58:00 -0400520
521#else /* CONFIG_SMP */
522
523#define latency_notifier_init(x) do { } while (0)
524
525#endif /* CONFIG_SMP */
526
527/**
528 * cpuidle_init - core initializer
529 */
530static int __init cpuidle_init(void)
531{
532 int ret;
533
Len Brown62027ae2011-04-01 18:13:10 -0400534 if (cpuidle_disabled())
535 return -ENODEV;
536
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800537 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400538 if (ret)
539 return ret;
540
541 latency_notifier_init(&cpuidle_latency_notifier);
542
543 return 0;
544}
545
Len Brown62027ae2011-04-01 18:13:10 -0400546module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400547core_initcall(cpuidle_init);