blob: 125150dc6e81edf5a120adffce852080eeccf76f [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;
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020035static bool use_deepest_state __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040036
37int cpuidle_disabled(void)
38{
39 return off;
40}
Len Brownd91ee582011-04-01 18:28:35 -040041void disable_cpuidle(void)
42{
43 off = 1;
44}
Len Brown4f86d3a2007-10-03 18:58:00 -040045
46/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010047 * cpuidle_play_dead - cpu off-lining
48 *
Toshi Kaniee01e662012-03-31 21:37:02 -060049 * Returns in case of an error or no driver
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010050 */
51int cpuidle_play_dead(void)
52{
53 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +000054 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010055 int i;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010056
Toshi Kaniee01e662012-03-31 21:37:02 -060057 if (!drv)
58 return -ENODEV;
59
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010060 /* Find lowest-power state that supports long-term idle */
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010061 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
62 if (drv->states[i].enter_dead)
63 return drv->states[i].enter_dead(dev, i);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010064
65 return -ENODEV;
66}
67
68/**
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020069 * cpuidle_use_deepest_state - Enable/disable the "deepest idle" mode.
70 * @enable: Whether enable or disable the feature.
71 *
72 * If the "deepest idle" mode is enabled, cpuidle will ignore the governor and
73 * always use the state with the greatest exit latency (out of the states that
74 * are not disabled).
75 *
76 * This function can only be called after cpuidle_pause() to avoid races.
77 */
78void cpuidle_use_deepest_state(bool enable)
79{
80 use_deepest_state = enable;
81}
82
83/**
84 * cpuidle_find_deepest_state - Find the state of the greatest exit latency.
85 * @drv: cpuidle driver for a given CPU.
86 * @dev: cpuidle device for a given CPU.
87 */
88static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
89 struct cpuidle_device *dev)
90{
91 unsigned int latency_req = 0;
92 int i, ret = CPUIDLE_DRIVER_STATE_START - 1;
93
94 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
95 struct cpuidle_state *s = &drv->states[i];
96 struct cpuidle_state_usage *su = &dev->states_usage[i];
97
98 if (s->disabled || su->disable || s->exit_latency <= latency_req)
99 continue;
100
101 latency_req = s->exit_latency;
102 ret = i;
103 }
104 return ret;
105}
106
107/**
Colin Cross56cfbf72012-05-07 17:57:39 -0700108 * cpuidle_enter_state - enter the state and update stats
109 * @dev: cpuidle device for this cpu
110 * @drv: cpuidle driver for this cpu
111 * @next_state: index into drv->states of the state to enter
112 */
113int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000114 int index)
Colin Cross56cfbf72012-05-07 17:57:39 -0700115{
116 int entered_state;
117
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000118 struct cpuidle_state *target_state = &drv->states[index];
119 ktime_t time_start, time_end;
120 s64 diff;
121
Sandeep Tripathy30fe6882014-07-02 15:00:58 +0530122 trace_cpu_idle_rcuidle(index, dev->cpu);
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000123 time_start = ktime_get();
124
125 entered_state = target_state->enter(dev, drv, index);
126
127 time_end = ktime_get();
Sandeep Tripathy30fe6882014-07-02 15:00:58 +0530128 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000129
Paul Burton0b89e9a2014-03-06 11:02:01 +0000130 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
131 local_irq_enable();
Daniel Lezcano554c06b2013-04-23 08:54:31 +0000132
133 diff = ktime_to_us(ktime_sub(time_end, time_start));
134 if (diff > INT_MAX)
135 diff = INT_MAX;
136
137 dev->last_residency = (int) diff;
Colin Cross56cfbf72012-05-07 17:57:39 -0700138
139 if (entered_state >= 0) {
140 /* Update cpuidle counters */
141 /* This can be moved to within driver enter routine
142 * but that results in multiple copies of same code.
143 */
Julius Wernera474a512012-11-27 14:17:58 +0100144 dev->states_usage[entered_state].time += dev->last_residency;
Colin Cross56cfbf72012-05-07 17:57:39 -0700145 dev->states_usage[entered_state].usage++;
146 } else {
147 dev->last_residency = 0;
148 }
149
150 return entered_state;
151}
152
153/**
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100154 * cpuidle_select - ask the cpuidle framework to choose an idle state
Len Brown4f86d3a2007-10-03 18:58:00 -0400155 *
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100156 * @drv: the cpuidle driver
157 * @dev: the cpuidle device
158 *
159 * Returns the index of the idle state.
Len Brown4f86d3a2007-10-03 18:58:00 -0400160 */
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100161int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400162{
Rafael J. Wysocki52c324f2014-05-01 00:13:47 +0200163 if (off || !initialized)
164 return -ENODEV;
165
166 if (!drv || !dev || !dev->enabled)
167 return -EBUSY;
168
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +0200169 if (unlikely(use_deepest_state))
170 return cpuidle_find_deepest_state(drv, dev);
171
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100172 return cpuidle_curr_governor->select(drv, dev);
173}
Len Brown4f86d3a2007-10-03 18:58:00 -0400174
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100175/**
176 * cpuidle_enter - enter into the specified idle state
177 *
178 * @drv: the cpuidle driver tied with the cpu
179 * @dev: the cpuidle device
180 * @index: the index in the idle state table
181 *
182 * Returns the index in the idle state, < 0 in case of error.
183 * The error code depends on the backend driver
184 */
185int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
186 int index)
187{
188 if (cpuidle_state_is_coupled(dev, drv, index))
189 return cpuidle_enter_state_coupled(dev, drv, index);
190 return cpuidle_enter_state(dev, drv, index);
191}
Len Browna0bfa132011-04-01 19:34:59 -0400192
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100193/**
194 * cpuidle_reflect - tell the underlying governor what was the state
195 * we were in
196 *
197 * @dev : the cpuidle device
198 * @index: the index in the idle state table
199 *
200 */
201void cpuidle_reflect(struct cpuidle_device *dev, int index)
202{
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +0200203 if (cpuidle_curr_governor->reflect && !unlikely(use_deepest_state))
Daniel Lezcano907e30f2014-03-03 08:48:50 +0100204 cpuidle_curr_governor->reflect(dev, index);
Len Brown4f86d3a2007-10-03 18:58:00 -0400205}
206
207/**
208 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
209 */
210void cpuidle_install_idle_handler(void)
211{
Len Browna0bfa132011-04-01 19:34:59 -0400212 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400213 /* Make sure all changes finished before we switch to new idle */
214 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400215 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400216 }
217}
218
219/**
220 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
221 */
222void cpuidle_uninstall_idle_handler(void)
223{
Len Browna0bfa132011-04-01 19:34:59 -0400224 if (enabled_devices) {
225 initialized = 0;
Chuansheng Liu2ed903c2014-09-04 15:17:55 +0800226 wake_up_all_idle_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400227 }
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400228
229 /*
230 * Make sure external observers (such as the scheduler)
231 * are done looking at pointed idle states.
232 */
233 synchronize_rcu();
Len Brown4f86d3a2007-10-03 18:58:00 -0400234}
235
236/**
237 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
238 */
239void cpuidle_pause_and_lock(void)
240{
241 mutex_lock(&cpuidle_lock);
242 cpuidle_uninstall_idle_handler();
243}
244
245EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
246
247/**
248 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
249 */
250void cpuidle_resume_and_unlock(void)
251{
252 cpuidle_install_idle_handler();
253 mutex_unlock(&cpuidle_lock);
254}
255
256EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
257
Preeti U Murthy8651f972012-07-09 10:12:56 +0200258/* Currently used in suspend/resume path to suspend cpuidle */
259void cpuidle_pause(void)
260{
261 mutex_lock(&cpuidle_lock);
262 cpuidle_uninstall_idle_handler();
263 mutex_unlock(&cpuidle_lock);
264}
265
266/* Currently used in suspend/resume path to resume cpuidle */
267void cpuidle_resume(void)
268{
269 mutex_lock(&cpuidle_lock);
270 cpuidle_install_idle_handler();
271 mutex_unlock(&cpuidle_lock);
272}
273
Len Brown4f86d3a2007-10-03 18:58:00 -0400274/**
275 * cpuidle_enable_device - enables idle PM for a CPU
276 * @dev: the CPU
277 *
278 * This function must be called between cpuidle_pause_and_lock and
279 * cpuidle_resume_and_unlock when used externally.
280 */
281int cpuidle_enable_device(struct cpuidle_device *dev)
282{
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200283 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000284 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400285
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700286 if (!dev)
287 return -EINVAL;
288
Len Brown4f86d3a2007-10-03 18:58:00 -0400289 if (dev->enabled)
290 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000291
292 drv = cpuidle_get_cpu_driver(dev);
293
Robert Leee1689792012-03-20 15:22:42 -0500294 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400295 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000296
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200297 if (!dev->registered)
298 return -EINVAL;
299
Len Brown4f86d3a2007-10-03 18:58:00 -0400300 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200301 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400302
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000303 ret = cpuidle_add_device_sysfs(dev);
304 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400305 return ret;
306
307 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500308 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400309 goto fail_sysfs;
310
Len Brown4f86d3a2007-10-03 18:58:00 -0400311 smp_wmb();
312
313 dev->enabled = 1;
314
315 enabled_devices++;
316 return 0;
317
318fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000319 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400320
321 return ret;
322}
323
324EXPORT_SYMBOL_GPL(cpuidle_enable_device);
325
326/**
327 * cpuidle_disable_device - disables idle PM for a CPU
328 * @dev: the CPU
329 *
330 * This function must be called between cpuidle_pause_and_lock and
331 * cpuidle_resume_and_unlock when used externally.
332 */
333void cpuidle_disable_device(struct cpuidle_device *dev)
334{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000335 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
336
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530337 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400338 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000339
340 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400341 return;
342
343 dev->enabled = 0;
344
345 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000346 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400347
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000348 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400349 enabled_devices--;
350}
351
352EXPORT_SYMBOL_GPL(cpuidle_disable_device);
353
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200354static void __cpuidle_unregister_device(struct cpuidle_device *dev)
355{
356 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
357
358 list_del(&dev->device_list);
359 per_cpu(cpuidle_devices, dev->cpu) = NULL;
360 module_put(drv->owner);
361}
362
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530363static void __cpuidle_device_init(struct cpuidle_device *dev)
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200364{
365 memset(dev->states_usage, 0, sizeof(dev->states_usage));
366 dev->last_residency = 0;
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200367}
368
Len Brown4f86d3a2007-10-03 18:58:00 -0400369/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400370 * __cpuidle_register_device - internal register function called before register
371 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400372 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400373 *
374 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400375 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400376static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400377{
378 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000379 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400380
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000381 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400382 return -EINVAL;
383
Len Brown4f86d3a2007-10-03 18:58:00 -0400384 per_cpu(cpuidle_devices, dev->cpu) = dev;
385 list_add(&dev->device_list, &cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400386
Colin Cross4126c012012-05-07 17:57:41 -0700387 ret = cpuidle_coupled_register_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530388 if (ret)
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200389 __cpuidle_unregister_device(dev);
Viresh Kumar47182662013-10-03 21:26:46 +0530390 else
391 dev->registered = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400392
Viresh Kumar47182662013-10-03 21:26:46 +0530393 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400394}
395
396/**
397 * cpuidle_register_device - registers a CPU's idle PM feature
398 * @dev: the cpu
399 */
400int cpuidle_register_device(struct cpuidle_device *dev)
401{
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200402 int ret = -EBUSY;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400403
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700404 if (!dev)
405 return -EINVAL;
406
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400407 mutex_lock(&cpuidle_lock);
408
Daniel Lezcanoc878a522013-06-12 15:08:55 +0200409 if (dev->registered)
410 goto out_unlock;
411
Viresh Kumar267d4bf2013-10-03 21:26:43 +0530412 __cpuidle_device_init(dev);
Daniel Lezcano5df0aa72013-06-12 15:08:54 +0200413
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200414 ret = __cpuidle_register_device(dev);
415 if (ret)
416 goto out_unlock;
417
418 ret = cpuidle_add_sysfs(dev);
419 if (ret)
420 goto out_unregister;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400421
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200422 ret = cpuidle_enable_device(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200423 if (ret)
424 goto out_sysfs;
Daniel Lezcano10b9d3f2013-06-12 15:08:49 +0200425
Len Brown4f86d3a2007-10-03 18:58:00 -0400426 cpuidle_install_idle_handler();
427
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200428out_unlock:
Len Brown4f86d3a2007-10-03 18:58:00 -0400429 mutex_unlock(&cpuidle_lock);
430
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200431 return ret;
432
433out_sysfs:
434 cpuidle_remove_sysfs(dev);
435out_unregister:
436 __cpuidle_unregister_device(dev);
437 goto out_unlock;
Len Brown4f86d3a2007-10-03 18:58:00 -0400438}
439
440EXPORT_SYMBOL_GPL(cpuidle_register_device);
441
442/**
443 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
444 * @dev: the cpu
445 */
446void cpuidle_unregister_device(struct cpuidle_device *dev)
447{
Konrad Rzeszutek Wilk813e8e32013-12-03 10:59:58 -0500448 if (!dev || dev->registered == 0)
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400449 return;
450
Len Brown4f86d3a2007-10-03 18:58:00 -0400451 cpuidle_pause_and_lock();
452
453 cpuidle_disable_device(dev);
454
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200455 cpuidle_remove_sysfs(dev);
Daniel Lezcanof6bb51a2013-06-12 15:08:53 +0200456
457 __cpuidle_unregister_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400458
Colin Cross4126c012012-05-07 17:57:41 -0700459 cpuidle_coupled_unregister_device(dev);
460
Len Brown4f86d3a2007-10-03 18:58:00 -0400461 cpuidle_resume_and_unlock();
Len Brown4f86d3a2007-10-03 18:58:00 -0400462}
463
464EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
465
Daniel Lezcano1c192d02013-04-23 15:28:44 +0000466/**
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000467 * cpuidle_unregister: unregister a driver and the devices. This function
468 * can be used only if the driver has been previously registered through
469 * the cpuidle_register function.
470 *
471 * @drv: a valid pointer to a struct cpuidle_driver
472 */
473void cpuidle_unregister(struct cpuidle_driver *drv)
474{
475 int cpu;
476 struct cpuidle_device *device;
477
Daniel Lezcano82467a52013-06-07 21:53:09 +0000478 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000479 device = &per_cpu(cpuidle_dev, cpu);
480 cpuidle_unregister_device(device);
481 }
482
483 cpuidle_unregister_driver(drv);
484}
485EXPORT_SYMBOL_GPL(cpuidle_unregister);
486
487/**
488 * cpuidle_register: registers the driver and the cpu devices with the
489 * coupled_cpus passed as parameter. This function is used for all common
490 * initialization pattern there are in the arch specific drivers. The
491 * devices is globally defined in this file.
492 *
493 * @drv : a valid pointer to a struct cpuidle_driver
494 * @coupled_cpus: a cpumask for the coupled states
495 *
496 * Returns 0 on success, < 0 otherwise
497 */
498int cpuidle_register(struct cpuidle_driver *drv,
499 const struct cpumask *const coupled_cpus)
500{
501 int ret, cpu;
502 struct cpuidle_device *device;
503
504 ret = cpuidle_register_driver(drv);
505 if (ret) {
506 pr_err("failed to register cpuidle driver\n");
507 return ret;
508 }
509
Daniel Lezcano82467a52013-06-07 21:53:09 +0000510 for_each_cpu(cpu, drv->cpumask) {
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000511 device = &per_cpu(cpuidle_dev, cpu);
512 device->cpu = cpu;
513
514#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
515 /*
Viresh Kumarcaf4a362013-10-03 21:26:41 +0530516 * On multiplatform for ARM, the coupled idle states could be
Daniel Lezcano4c637b22013-04-23 08:54:33 +0000517 * enabled in the kernel even if the cpuidle driver does not
518 * use it. Note, coupled_cpus is a struct copy.
519 */
520 if (coupled_cpus)
521 device->coupled_cpus = *coupled_cpus;
522#endif
523 ret = cpuidle_register_device(device);
524 if (!ret)
525 continue;
526
527 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
528
529 cpuidle_unregister(drv);
530 break;
531 }
532
533 return ret;
534}
535EXPORT_SYMBOL_GPL(cpuidle_register);
536
Len Brown4f86d3a2007-10-03 18:58:00 -0400537#ifdef CONFIG_SMP
538
Len Brown4f86d3a2007-10-03 18:58:00 -0400539/*
540 * This function gets called when a part of the kernel has a new latency
541 * requirement. This means we need to get all processors out of their C-state,
542 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
543 * wakes them all right up.
544 */
545static int cpuidle_latency_notify(struct notifier_block *b,
546 unsigned long l, void *v)
547{
Chuansheng Liu2ed903c2014-09-04 15:17:55 +0800548 wake_up_all_idle_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400549 return NOTIFY_OK;
550}
551
552static struct notifier_block cpuidle_latency_notifier = {
553 .notifier_call = cpuidle_latency_notify,
554};
555
Mark Grossd82b3512008-02-04 22:30:08 -0800556static inline void latency_notifier_init(struct notifier_block *n)
557{
558 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
559}
Len Brown4f86d3a2007-10-03 18:58:00 -0400560
561#else /* CONFIG_SMP */
562
563#define latency_notifier_init(x) do { } while (0)
564
565#endif /* CONFIG_SMP */
566
567/**
568 * cpuidle_init - core initializer
569 */
570static int __init cpuidle_init(void)
571{
572 int ret;
573
Len Brown62027ae2011-04-01 18:13:10 -0400574 if (cpuidle_disabled())
575 return -ENODEV;
576
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800577 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400578 if (ret)
579 return ret;
580
581 latency_notifier_init(&cpuidle_latency_notifier);
582
583 return 0;
584}
585
Len Brown62027ae2011-04-01 18:13:10 -0400586module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400587core_initcall(cpuidle_init);