blob: 1506d69b3f0f7dbd2b49147374d69cb62091ba55 [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/**
Daniel Lezcano907e30f2014-03-03 08:48:50 +010068 * cpuidle_enabled - check if the cpuidle framework is ready
69 * @dev: cpuidle device for this cpu
70 * @drv: cpuidle driver for this cpu
71 *
72 * Return 0 on success, otherwise:
73 * -NODEV : the cpuidle framework is not available
74 * -EBUSY : the cpuidle framework is not initialized
75 */
76int cpuidle_enabled(struct cpuidle_driver *drv, struct cpuidle_device *dev)
77{
78 if (off || !initialized)
79 return -ENODEV;
80
81 if (!drv || !dev || !dev->enabled)
82 return -EBUSY;
83
84 return 0;
85}
86
87/**
Colin Cross56cfbf72012-05-07 17:57:39 -070088 * cpuidle_enter_state - enter the state and update stats
89 * @dev: cpuidle device for this cpu
90 * @drv: cpuidle driver for this cpu
91 * @next_state: index into drv->states of the state to enter
92 */
93int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
Daniel Lezcano554c06b2013-04-23 08:54:31 +000094 int index)
Colin Cross56cfbf72012-05-07 17:57:39 -070095{
96 int entered_state;
97
Daniel Lezcano554c06b2013-04-23 08:54:31 +000098 struct cpuidle_state *target_state = &drv->states[index];
99 ktime_t time_start, time_end;
100 s64 diff;
101
102 time_start = ktime_get();
103
104 entered_state = target_state->enter(dev, drv, index);
105
106 time_end = ktime_get();
107
108 local_irq_enable();
109
110 diff = ktime_to_us(ktime_sub(time_end, time_start));
111 if (diff > INT_MAX)
112 diff = INT_MAX;
113
114 dev->last_residency = (int) diff;
Colin Cross56cfbf72012-05-07 17:57:39 -0700115
116 if (entered_state >= 0) {
117 /* Update cpuidle counters */
118 /* This can be moved to within driver enter routine
119 * but that results in multiple copies of same code.
120 */
Julius Wernera474a512012-11-27 14:17:58 +0100121 dev->states_usage[entered_state].time += dev->last_residency;
Colin Cross56cfbf72012-05-07 17:57:39 -0700122 dev->states_usage[entered_state].usage++;
123 } else {
124 dev->last_residency = 0;
125 }
126
127 return entered_state;
128}
129
130/**
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100131 * cpuidle_select - ask the cpuidle framework to choose an idle state
132 *
133 * @drv: the cpuidle driver
134 * @dev: the cpuidle device
135 *
136 * Returns the index of the idle state.
137 */
138int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
139{
140 return cpuidle_curr_governor->select(drv, dev);
141}
142
143/**
144 * cpuidle_enter - enter into the specified idle state
145 *
146 * @drv: the cpuidle driver tied with the cpu
147 * @dev: the cpuidle device
148 * @index: the index in the idle state table
149 *
150 * Returns the index in the idle state, < 0 in case of error.
151 * The error code depends on the backend driver
152 */
153int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
154 int index)
155{
156 if (cpuidle_state_is_coupled(dev, drv, index))
157 return cpuidle_enter_state_coupled(dev, drv, index);
158 return cpuidle_enter_state(dev, drv, index);
159}
160
161/**
162 * cpuidle_reflect - tell the underlying governor what was the state
163 * we were in
164 *
165 * @dev : the cpuidle device
166 * @index: the index in the idle state table
167 *
168 */
169void cpuidle_reflect(struct cpuidle_device *dev, int index)
170{
171 if (cpuidle_curr_governor->reflect)
172 cpuidle_curr_governor->reflect(dev, index);
173}
174
175/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400176 * cpuidle_idle_call - the main idle loop
177 *
178 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -0400179 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -0400180 */
Len Browna0bfa132011-04-01 19:34:59 -0400181int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -0400182{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600183 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100184 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
185 int next_state, entered_state, ret;
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530186 bool broadcast;
Len Brown4f86d3a2007-10-03 18:58:00 -0400187
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100188 ret = cpuidle_enabled(drv, dev);
189 if (ret < 0)
190 return ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000191
Len Brown4f86d3a2007-10-03 18:58:00 -0400192 /* ask the governor for the next state */
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100193 next_state = cpuidle_select(drv, dev);
194
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700195 if (need_resched()) {
Youquan Songd73d68d2012-10-26 12:26:59 +0200196 dev->last_residency = 0;
197 /* give the governor an opportunity to reflect on the outcome */
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100198 cpuidle_reflect(dev, next_state);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700199 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400200 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700201 }
202
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530203 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
204
Preeti U Murthyba8f20c2014-02-07 13:36:52 +0530205 if (broadcast &&
206 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
207 return -EBUSY;
208
Preeti U Murthyba8f20c2014-02-07 13:36:52 +0530209 trace_cpu_idle_rcuidle(next_state, dev->cpu);
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000210
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100211 entered_state = cpuidle_enter(drv, dev, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100212
Preeti U Murthyba8f20c2014-02-07 13:36:52 +0530213 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
214
Viresh Kumarfb11c9c2013-10-03 21:26:48 +0530215 if (broadcast)
Viresh Kumar6d281e92013-10-03 21:26:49 +0530216 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000217
Len Brown4f86d3a2007-10-03 18:58:00 -0400218 /* give the governor an opportunity to reflect on the outcome */
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100219 cpuidle_reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400220
221 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400222}
223
224/**
225 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
226 */
227void cpuidle_install_idle_handler(void)
228{
Len Browna0bfa132011-04-01 19:34:59 -0400229 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400230 /* Make sure all changes finished before we switch to new idle */
231 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400232 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400233 }
234}
235
236/**
237 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
238 */
239void cpuidle_uninstall_idle_handler(void)
240{
Len Browna0bfa132011-04-01 19:34:59 -0400241 if (enabled_devices) {
242 initialized = 0;
Thomas Gleixner4a162512012-05-07 17:59:48 +0000243 kick_all_cpus_sync();
Len Brown4f86d3a2007-10-03 18:58:00 -0400244 }
245}
246
247/**
248 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
249 */
250void cpuidle_pause_and_lock(void)
251{
252 mutex_lock(&cpuidle_lock);
253 cpuidle_uninstall_idle_handler();
254}
255
256EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
257
258/**
259 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
260 */
261void cpuidle_resume_and_unlock(void)
262{
263 cpuidle_install_idle_handler();
264 mutex_unlock(&cpuidle_lock);
265}
266
267EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
268
Preeti U Murthy8651f972012-07-09 10:12:56 +0200269/* Currently used in suspend/resume path to suspend cpuidle */
270void cpuidle_pause(void)
271{
272 mutex_lock(&cpuidle_lock);
273 cpuidle_uninstall_idle_handler();
274 mutex_unlock(&cpuidle_lock);
275}
276
277/* Currently used in suspend/resume path to resume cpuidle */
278void cpuidle_resume(void)
279{
280 mutex_lock(&cpuidle_lock);
281 cpuidle_install_idle_handler();
282 mutex_unlock(&cpuidle_lock);
283}
284
Len Brown4f86d3a2007-10-03 18:58:00 -0400285/**
286 * cpuidle_enable_device - enables idle PM for a CPU
287 * @dev: the CPU
288 *
289 * This function must be called between cpuidle_pause_and_lock and
290 * cpuidle_resume_and_unlock when used externally.
291 */
292int cpuidle_enable_device(struct cpuidle_device *dev)
293{
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200294 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000295 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400296
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700297 if (!dev)
298 return -EINVAL;
299
Len Brown4f86d3a2007-10-03 18:58:00 -0400300 if (dev->enabled)
301 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000302
303 drv = cpuidle_get_cpu_driver(dev);
304
Robert Leee1689792012-03-20 15:22:42 -0500305 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400306 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000307
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200308 if (!dev->registered)
309 return -EINVAL;
310
Len Brown4f86d3a2007-10-03 18:58:00 -0400311 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200312 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400313
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000314 ret = cpuidle_add_device_sysfs(dev);
315 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400316 return ret;
317
318 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500319 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400320 goto fail_sysfs;
321
Len Brown4f86d3a2007-10-03 18:58:00 -0400322 smp_wmb();
323
324 dev->enabled = 1;
325
326 enabled_devices++;
327 return 0;
328
329fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000330 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400331
332 return ret;
333}
334
335EXPORT_SYMBOL_GPL(cpuidle_enable_device);
336
337/**
338 * cpuidle_disable_device - disables idle PM for a CPU
339 * @dev: the CPU
340 *
341 * This function must be called between cpuidle_pause_and_lock and
342 * cpuidle_resume_and_unlock when used externally.
343 */
344void cpuidle_disable_device(struct cpuidle_device *dev)
345{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000346 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
347
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530348 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400349 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000350
351 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400352 return;
353
354 dev->enabled = 0;
355
356 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000357 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400358
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000359 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400360 enabled_devices--;
361}
362
363EXPORT_SYMBOL_GPL(cpuidle_disable_device);
364
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200365static void __cpuidle_unregister_device(struct cpuidle_device *dev)
366{
367 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
368
369 list_del(&dev->device_list);
370 per_cpu(cpuidle_devices, dev->cpu) = NULL;
371 module_put(drv->owner);
372}
373
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530374static void __cpuidle_device_init(struct cpuidle_device *dev)
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200375{
376 memset(dev->states_usage, 0, sizeof(dev->states_usage));
377 dev->last_residency = 0;
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200378}
379
Len Brown4f86d3a2007-10-03 18:58:00 -0400380/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400381 * __cpuidle_register_device - internal register function called before register
382 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400383 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400384 *
385 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400386 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400387static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400388{
389 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000390 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400391
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000392 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400393 return -EINVAL;
394
Len Brown4f86d3a2007-10-03 18:58:00 -0400395 per_cpu(cpuidle_devices, dev->cpu) = dev;
396 list_add(&dev->device_list, &cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400397
Colin Cross4126c012012-05-07 17:57:41 -0700398 ret = cpuidle_coupled_register_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530399 if (ret)
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200400 __cpuidle_unregister_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530401 else
402 dev->registered = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400403
Viresh Kumar47182662013-10-03 21:26:46 +0530404 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400405}
406
407/**
408 * cpuidle_register_device - registers a CPU's idle PM feature
409 * @dev: the cpu
410 */
411int cpuidle_register_device(struct cpuidle_device *dev)
412{
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200413 int ret = -EBUSY;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400414
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700415 if (!dev)
416 return -EINVAL;
417
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400418 mutex_lock(&cpuidle_lock);
419
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200420 if (dev->registered)
421 goto out_unlock;
422
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530423 __cpuidle_device_init(dev);
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200424
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200425 ret = __cpuidle_register_device(dev);
426 if (ret)
427 goto out_unlock;
428
429 ret = cpuidle_add_sysfs(dev);
430 if (ret)
431 goto out_unregister;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400432
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200433 ret = cpuidle_enable_device(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200434 if (ret)
435 goto out_sysfs;
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200436
Len Brown4f86d3a2007-10-03 18:58:00 -0400437 cpuidle_install_idle_handler();
438
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200439out_unlock:
Len Brown4f86d3a2007-10-03 18:58:00 -0400440 mutex_unlock(&cpuidle_lock);
441
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200442 return ret;
443
444out_sysfs:
445 cpuidle_remove_sysfs(dev);
446out_unregister:
447 __cpuidle_unregister_device(dev);
448 goto out_unlock;
Len Brown4f86d3a2007-10-03 18:58:00 -0400449}
450
451EXPORT_SYMBOL_GPL(cpuidle_register_device);
452
453/**
454 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
455 * @dev: the cpu
456 */
457void cpuidle_unregister_device(struct cpuidle_device *dev)
458{
Konrad Rzeszutek Wilk813e8e32013-12-03 10:59:58 -0500459 if (!dev || dev->registered == 0)
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400460 return;
461
Len Brown4f86d3a2007-10-03 18:58:00 -0400462 cpuidle_pause_and_lock();
463
464 cpuidle_disable_device(dev);
465
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200466 cpuidle_remove_sysfs(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200467
468 __cpuidle_unregister_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400469
Colin Cross4126c012012-05-07 17:57:41 -0700470 cpuidle_coupled_unregister_device(dev);
471
Len Brown4f86d3a2007-10-03 18:58:00 -0400472 cpuidle_resume_and_unlock();
Len Brown4f86d3a2007-10-03 18:58:00 -0400473}
474
475EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
476
Daniel Lezcano1c192d02013-04-23 15:28:44 +0000477/**
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000478 * cpuidle_unregister: unregister a driver and the devices. This function
479 * can be used only if the driver has been previously registered through
480 * the cpuidle_register function.
481 *
482 * @drv: a valid pointer to a struct cpuidle_driver
483 */
484void cpuidle_unregister(struct cpuidle_driver *drv)
485{
486 int cpu;
487 struct cpuidle_device *device;
488
Daniel Lezcano82467a52013-06-07 21:53:09 +0000489 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000490 device = &per_cpu(cpuidle_dev, cpu);
491 cpuidle_unregister_device(device);
492 }
493
494 cpuidle_unregister_driver(drv);
495}
496EXPORT_SYMBOL_GPL(cpuidle_unregister);
497
498/**
499 * cpuidle_register: registers the driver and the cpu devices with the
500 * coupled_cpus passed as parameter. This function is used for all common
501 * initialization pattern there are in the arch specific drivers. The
502 * devices is globally defined in this file.
503 *
504 * @drv : a valid pointer to a struct cpuidle_driver
505 * @coupled_cpus: a cpumask for the coupled states
506 *
507 * Returns 0 on success, < 0 otherwise
508 */
509int cpuidle_register(struct cpuidle_driver *drv,
510 const struct cpumask *const coupled_cpus)
511{
512 int ret, cpu;
513 struct cpuidle_device *device;
514
515 ret = cpuidle_register_driver(drv);
516 if (ret) {
517 pr_err("failed to register cpuidle driver\n");
518 return ret;
519 }
520
Daniel Lezcano82467a52013-06-07 21:53:09 +0000521 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000522 device = &per_cpu(cpuidle_dev, cpu);
523 device->cpu = cpu;
524
525#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
526 /*
Viresh Kumarcaf4a362013-10-03 21:26:41 +0530527 * On multiplatform for ARM, the coupled idle states could be
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000528 * enabled in the kernel even if the cpuidle driver does not
529 * use it. Note, coupled_cpus is a struct copy.
530 */
531 if (coupled_cpus)
532 device->coupled_cpus = *coupled_cpus;
533#endif
534 ret = cpuidle_register_device(device);
535 if (!ret)
536 continue;
537
538 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
539
540 cpuidle_unregister(drv);
541 break;
542 }
543
544 return ret;
545}
546EXPORT_SYMBOL_GPL(cpuidle_register);
547
Len Brown4f86d3a2007-10-03 18:58:00 -0400548#ifdef CONFIG_SMP
549
550static void smp_callback(void *v)
551{
552 /* we already woke the CPU up, nothing more to do */
553}
554
555/*
556 * This function gets called when a part of the kernel has a new latency
557 * requirement. This means we need to get all processors out of their C-state,
558 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
559 * wakes them all right up.
560 */
561static int cpuidle_latency_notify(struct notifier_block *b,
562 unsigned long l, void *v)
563{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200564 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400565 return NOTIFY_OK;
566}
567
568static struct notifier_block cpuidle_latency_notifier = {
569 .notifier_call = cpuidle_latency_notify,
570};
571
Mark Grossd82b3512008-02-04 22:30:08 -0800572static inline void latency_notifier_init(struct notifier_block *n)
573{
574 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
575}
Len Brown4f86d3a2007-10-03 18:58:00 -0400576
577#else /* CONFIG_SMP */
578
579#define latency_notifier_init(x) do { } while (0)
580
581#endif /* CONFIG_SMP */
582
583/**
584 * cpuidle_init - core initializer
585 */
586static int __init cpuidle_init(void)
587{
588 int ret;
589
Len Brown62027ae2011-04-01 18:13:10 -0400590 if (cpuidle_disabled())
591 return -ENODEV;
592
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800593 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400594 if (ret)
595 return ret;
596
597 latency_notifier_init(&cpuidle_latency_notifier);
598
599 return 0;
600}
601
Len Brown62027ae2011-04-01 18:13:10 -0400602module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400603core_initcall(cpuidle_init);