drm/amdgpu: cleanup instance limit on VCN4 v4
[linux-2.6-block.git] / drivers / cpuidle / cpuidle.c
CommitLineData
4f86d3a8
LB
1/*
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
6ab4b199 11#include "linux/percpu-defs.h"
b60e6a0e 12#include <linux/clockchips.h>
4f86d3a8
LB
13#include <linux/kernel.h>
14#include <linux/mutex.h>
15#include <linux/sched.h>
e6017571 16#include <linux/sched/clock.h>
4f86d3a8 17#include <linux/notifier.h>
e8db0be1 18#include <linux/pm_qos.h>
4f86d3a8
LB
19#include <linux/cpu.h>
20#include <linux/cpuidle.h>
9a0b8415 21#include <linux/ktime.h>
2e94d1f7 22#include <linux/hrtimer.h>
884b17e1 23#include <linux/module.h>
38106313 24#include <linux/suspend.h>
124cf911 25#include <linux/tick.h>
bf9282dc 26#include <linux/mmu_context.h>
e67198cc 27#include <linux/context_tracking.h>
288f023e 28#include <trace/events/power.h>
4f86d3a8
LB
29
30#include "cpuidle.h"
31
32DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
4c637b21 33DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
4f86d3a8
LB
34
35DEFINE_MUTEX(cpuidle_lock);
36LIST_HEAD(cpuidle_detected_devices);
4f86d3a8
LB
37
38static int enabled_devices;
62027aea 39static int off __read_mostly;
a0bfa137 40static int initialized __read_mostly;
62027aea
LB
41
42int cpuidle_disabled(void)
43{
44 return off;
45}
d91ee586
LB
46void disable_cpuidle(void)
47{
48 off = 1;
49}
4f86d3a8 50
ef2b22ac
RW
51bool cpuidle_not_available(struct cpuidle_driver *drv,
52 struct cpuidle_device *dev)
31a34090
RW
53{
54 return off || !initialized || !drv || !dev || !dev->enabled;
55}
56
1a022e3f
BO
57/**
58 * cpuidle_play_dead - cpu off-lining
59 *
ee01e663 60 * Returns in case of an error or no driver
1a022e3f
BO
61 */
62int cpuidle_play_dead(void)
63{
64 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 65 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
8aef33a7 66 int i;
1a022e3f 67
ee01e663
TK
68 if (!drv)
69 return -ENODEV;
70
1a022e3f 71 /* Find lowest-power state that supports long-term idle */
7d51d979 72 for (i = drv->state_count - 1; i >= 0; i--)
8aef33a7
DL
73 if (drv->states[i].enter_dead)
74 return drv->states[i].enter_dead(dev, i);
1a022e3f
BO
75
76 return -ENODEV;
77}
78
ef2b22ac 79static int find_deepest_state(struct cpuidle_driver *drv,
0d94039f 80 struct cpuidle_device *dev,
c1d51f68 81 u64 max_latency_ns,
0d94039f 82 unsigned int forbidden_flags,
28ba086e 83 bool s2idle)
a6220fc1 84{
c1d51f68 85 u64 latency_req = 0;
51164251 86 int i, ret = 0;
a6220fc1 87
51164251 88 for (i = 1; i < drv->state_count; i++) {
a6220fc1 89 struct cpuidle_state *s = &drv->states[i];
a6220fc1 90
99e98d3f 91 if (dev->states_usage[i].disable ||
c1d51f68
RW
92 s->exit_latency_ns <= latency_req ||
93 s->exit_latency_ns > max_latency_ns ||
99e98d3f
RW
94 (s->flags & forbidden_flags) ||
95 (s2idle && !s->enter_s2idle))
a6220fc1
RW
96 continue;
97
c1d51f68 98 latency_req = s->exit_latency_ns;
a6220fc1
RW
99 ret = i;
100 }
101 return ret;
102}
103
0e7414b7 104/**
c55b51a0
DL
105 * cpuidle_use_deepest_state - Set/unset governor override mode.
106 * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
0e7414b7 107 *
c55b51a0
DL
108 * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
109 * state with exit latency within @latency_limit_ns (override governors going
110 * forward), or do not override governors if it is zero.
0e7414b7 111 */
c55b51a0 112void cpuidle_use_deepest_state(u64 latency_limit_ns)
bb8313b6
JP
113{
114 struct cpuidle_device *dev;
115
116 preempt_disable();
117 dev = cpuidle_get_device();
41dc750e 118 if (dev)
c55b51a0 119 dev->forced_idle_latency_limit_ns = latency_limit_ns;
bb8313b6
JP
120 preempt_enable();
121}
122
ef2b22ac
RW
123/**
124 * cpuidle_find_deepest_state - Find the deepest available idle state.
125 * @drv: cpuidle driver for the given CPU.
126 * @dev: cpuidle device for the given CPU.
cefb9409
BG
127 * @latency_limit_ns: Idle state exit latency limit
128 *
129 * Return: the index of the deepest available idle state.
ef2b22ac
RW
130 */
131int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
5aa9ba63
DL
132 struct cpuidle_device *dev,
133 u64 latency_limit_ns)
ef2b22ac 134{
5aa9ba63 135 return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
ef2b22ac
RW
136}
137
bb8313b6 138#ifdef CONFIG_SUSPEND
28ba086e 139static void enter_s2idle_proper(struct cpuidle_driver *drv,
124cf911
RW
140 struct cpuidle_device *dev, int index)
141{
64bdff69 142 ktime_t time_start, time_end;
8747f202 143 struct cpuidle_state *target_state = &drv->states[index];
64bdff69
RW
144
145 time_start = ns_to_ktime(local_clock());
146
1098582a 147 tick_freeze();
124cf911
RW
148 /*
149 * The state used here cannot be a "coupled" one, because the "coupled"
150 * cpuidle mechanism enables interrupts and doing that with timekeeping
151 * suspended is generally unsafe.
152 */
63caae84 153 stop_critical_timings();
8747f202 154 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
e67198cc 155 ct_idle_enter();
8747f202 156 target_state->enter_s2idle(dev, drv, index);
49d9c593
PZ
157 if (WARN_ON_ONCE(!irqs_disabled()))
158 local_irq_disable();
8747f202 159 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
e67198cc 160 ct_idle_exit();
1098582a 161 tick_unfreeze();
63caae84 162 start_critical_timings();
64bdff69
RW
163
164 time_end = ns_to_ktime(local_clock());
165
166 dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
167 dev->states_usage[index].s2idle_usage++;
124cf911
RW
168}
169
38106313 170/**
28ba086e 171 * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
ef2b22ac
RW
172 * @drv: cpuidle driver for the given CPU.
173 * @dev: cpuidle device for the given CPU.
38106313 174 *
28ba086e 175 * If there are states with the ->enter_s2idle callback, find the deepest of
ef2b22ac 176 * them and enter it with frozen tick.
38106313 177 */
28ba086e 178int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
38106313 179{
38106313
RW
180 int index;
181
124cf911 182 /*
28ba086e 183 * Find the deepest state with ->enter_s2idle present, which guarantees
124cf911
RW
184 * that interrupts won't be enabled when it exits and allows the tick to
185 * be frozen safely.
186 */
c1d51f68 187 index = find_deepest_state(drv, dev, U64_MAX, 0, true);
10e8b11e 188 if (index > 0) {
28ba086e 189 enter_s2idle_proper(drv, dev, index);
10e8b11e
RW
190 local_irq_enable();
191 }
ef2b22ac 192 return index;
38106313 193}
87e9b9f1 194#endif /* CONFIG_SUSPEND */
38106313 195
56cfbf74
CC
196/**
197 * cpuidle_enter_state - enter the state and update stats
198 * @dev: cpuidle device for this cpu
199 * @drv: cpuidle driver for this cpu
7312280b 200 * @index: index into the states table in @drv of the state to enter
56cfbf74
CC
201 */
202int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
554c06ba 203 int index)
56cfbf74
CC
204{
205 int entered_state;
206
554c06ba 207 struct cpuidle_state *target_state = &drv->states[index];
df8d9eea 208 bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
dbd1b8ea 209 ktime_t time_start, time_end;
554c06ba 210
df8d9eea
RW
211 /*
212 * Tell the time framework to switch to a broadcast timer because our
213 * local timer will be shut down. If a local timer is used from another
214 * CPU as a broadcast timer, this call may fail if it is not available.
215 */
827a5aef 216 if (broadcast && tick_broadcast_enter()) {
c1d51f68 217 index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
0d94039f
RW
218 CPUIDLE_FLAG_TIMER_STOP, false);
219 if (index < 0) {
220 default_idle_call();
221 return -EBUSY;
222 }
223 target_state = &drv->states[index];
f187851b 224 broadcast = false;
827a5aef 225 }
df8d9eea 226
bf9282dc
PZ
227 if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
228 leave_mm(dev->cpu);
229
faad3849
RW
230 /* Take note of the planned idle state. */
231 sched_idle_set_state(target_state);
232
1098582a 233 trace_cpu_idle(index, dev->cpu);
dbd1b8ea 234 time_start = ns_to_ktime(local_clock());
554c06ba 235
63caae84 236 stop_critical_timings();
8747f202 237 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
e67198cc 238 ct_idle_enter();
554c06ba 239 entered_state = target_state->enter(dev, drv, index);
8747f202 240 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
e67198cc 241 ct_idle_exit();
63caae84 242 start_critical_timings();
554c06ba 243
f9fccdb9 244 sched_clock_idle_wakeup_event();
dbd1b8ea 245 time_end = ns_to_ktime(local_clock());
1098582a 246 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
554c06ba 247
faad3849
RW
248 /* The cpu is no longer idle or about to enter idle. */
249 sched_idle_set_state(NULL);
250
df8d9eea
RW
251 if (broadcast) {
252 if (WARN_ON_ONCE(!irqs_disabled()))
253 local_irq_disable();
254
255 tick_broadcast_exit();
256 }
257
e7387da5 258 if (!cpuidle_state_is_coupled(drv, index))
0b89e9aa 259 local_irq_enable();
554c06ba 260
56cfbf74 261 if (entered_state >= 0) {
c1d51f68 262 s64 diff, delay = drv->states[entered_state].exit_latency_ns;
04dab58a
RW
263 int i;
264
7037b43e
FL
265 /*
266 * Update cpuidle counters
267 * This can be moved to within driver enter routine,
56cfbf74
CC
268 * but that results in multiple copies of same code.
269 */
c1d51f68 270 diff = ktime_sub(time_end, time_start);
7037b43e 271
c1d51f68
RW
272 dev->last_residency_ns = diff;
273 dev->states_usage[entered_state].time_ns += diff;
56cfbf74 274 dev->states_usage[entered_state].usage++;
04dab58a 275
c1d51f68 276 if (diff < drv->states[entered_state].target_residency_ns) {
04dab58a 277 for (i = entered_state - 1; i >= 0; i--) {
99e98d3f 278 if (dev->states_usage[i].disable)
04dab58a
RW
279 continue;
280
281 /* Shallower states are enabled, so update. */
282 dev->states_usage[entered_state].above++;
6ab4b199 283 trace_cpu_idle_miss(dev->cpu, entered_state, false);
04dab58a
RW
284 break;
285 }
286 } else if (diff > delay) {
287 for (i = entered_state + 1; i < drv->state_count; i++) {
99e98d3f 288 if (dev->states_usage[i].disable)
04dab58a
RW
289 continue;
290
291 /*
292 * Update if a deeper state would have been a
293 * better match for the observed idle duration.
294 */
6ab4b199 295 if (diff - delay >= drv->states[i].target_residency_ns) {
04dab58a 296 dev->states_usage[entered_state].below++;
6ab4b199
KP
297 trace_cpu_idle_miss(dev->cpu, entered_state, true);
298 }
04dab58a
RW
299
300 break;
301 }
302 }
56cfbf74 303 } else {
c1d51f68 304 dev->last_residency_ns = 0;
f49735f4 305 dev->states_usage[index].rejected++;
56cfbf74
CC
306 }
307
308 return entered_state;
309}
310
4f86d3a8 311/**
907e30f1
DL
312 * cpuidle_select - ask the cpuidle framework to choose an idle state
313 *
314 * @drv: the cpuidle driver
315 * @dev: the cpuidle device
45f1ff59 316 * @stop_tick: indication on whether or not to stop the tick
4f86d3a8 317 *
51164251 318 * Returns the index of the idle state. The return value must not be negative.
45f1ff59
RW
319 *
320 * The memory location pointed to by @stop_tick is expected to be written the
321 * 'false' boolean value if the scheduler tick should not be stopped before
322 * entering the returned state.
4f86d3a8 323 */
45f1ff59
RW
324int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
325 bool *stop_tick)
4f86d3a8 326{
45f1ff59 327 return cpuidle_curr_governor->select(drv, dev, stop_tick);
907e30f1 328}
ba8f20c2 329
907e30f1
DL
330/**
331 * cpuidle_enter - enter into the specified idle state
332 *
333 * @drv: the cpuidle driver tied with the cpu
334 * @dev: the cpuidle device
335 * @index: the index in the idle state table
336 *
337 * Returns the index in the idle state, < 0 in case of error.
338 * The error code depends on the backend driver
339 */
340int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
341 int index)
342{
6f9b83ac
UH
343 int ret = 0;
344
345 /*
346 * Store the next hrtimer, which becomes either next tick or the next
347 * timer event, whatever expires first. Additionally, to make this data
348 * useful for consumers outside cpuidle, we rely on that the governor's
349 * ->select() callback have decided, whether to stop the tick or not.
350 */
351 WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
352
4c1ed5a6 353 if (cpuidle_state_is_coupled(drv, index))
6f9b83ac
UH
354 ret = cpuidle_enter_state_coupled(dev, drv, index);
355 else
356 ret = cpuidle_enter_state(dev, drv, index);
357
358 WRITE_ONCE(dev->next_hrtimer, 0);
359 return ret;
907e30f1 360}
b60e6a0e 361
907e30f1
DL
362/**
363 * cpuidle_reflect - tell the underlying governor what was the state
364 * we were in
365 *
366 * @dev : the cpuidle device
367 * @index: the index in the idle state table
368 *
369 */
370void cpuidle_reflect(struct cpuidle_device *dev, int index)
371{
a802ea96 372 if (cpuidle_curr_governor->reflect && index >= 0)
907e30f1 373 cpuidle_curr_governor->reflect(dev, index);
4f86d3a8
LB
374}
375
7a25759e
MG
376/*
377 * Min polling interval of 10usec is a guess. It is assuming that
378 * for most users, the time for a single ping-pong workload like
379 * perf bench pipe would generally complete within 10usec but
380 * this is hardware dependant. Actual time can be estimated with
381 *
382 * perf bench sched pipe -l 10000
383 *
384 * Run multiple times to avoid cpufreq effects.
385 */
386#define CPUIDLE_POLL_MIN 10000
387#define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
388
259231a0
MT
389/**
390 * cpuidle_poll_time - return amount of time to poll for,
391 * governors can override dev->poll_limit_ns if necessary
392 *
393 * @drv: the cpuidle driver tied with the cpu
394 * @dev: the cpuidle device
395 *
396 */
397u64 cpuidle_poll_time(struct cpuidle_driver *drv,
398 struct cpuidle_device *dev)
399{
400 int i;
401 u64 limit_ns;
402
7a25759e
MG
403 BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
404
259231a0
MT
405 if (dev->poll_limit_ns)
406 return dev->poll_limit_ns;
407
7a25759e 408 limit_ns = CPUIDLE_POLL_MAX;
259231a0 409 for (i = 1; i < drv->state_count; i++) {
7a25759e
MG
410 u64 state_limit;
411
99e98d3f 412 if (dev->states_usage[i].disable)
259231a0
MT
413 continue;
414
7a25759e
MG
415 state_limit = drv->states[i].target_residency_ns;
416 if (state_limit < CPUIDLE_POLL_MIN)
417 continue;
418
419 limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
36fcb429 420 break;
259231a0
MT
421 }
422
423 dev->poll_limit_ns = limit_ns;
424
425 return dev->poll_limit_ns;
426}
427
4f86d3a8
LB
428/**
429 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
430 */
431void cpuidle_install_idle_handler(void)
432{
a0bfa137 433 if (enabled_devices) {
4f86d3a8
LB
434 /* Make sure all changes finished before we switch to new idle */
435 smp_wmb();
a0bfa137 436 initialized = 1;
4f86d3a8
LB
437 }
438}
439
440/**
441 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
442 */
443void cpuidle_uninstall_idle_handler(void)
444{
a0bfa137
LB
445 if (enabled_devices) {
446 initialized = 0;
2ed903c5 447 wake_up_all_idle_cpus();
4f86d3a8 448 }
442bf3aa
DL
449
450 /*
451 * Make sure external observers (such as the scheduler)
452 * are done looking at pointed idle states.
453 */
454 synchronize_rcu();
4f86d3a8
LB
455}
456
457/**
458 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
459 */
460void cpuidle_pause_and_lock(void)
461{
462 mutex_lock(&cpuidle_lock);
463 cpuidle_uninstall_idle_handler();
464}
465
466EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
467
468/**
469 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
470 */
471void cpuidle_resume_and_unlock(void)
472{
473 cpuidle_install_idle_handler();
474 mutex_unlock(&cpuidle_lock);
475}
476
477EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
478
8651f97b
PM
479/* Currently used in suspend/resume path to suspend cpuidle */
480void cpuidle_pause(void)
481{
482 mutex_lock(&cpuidle_lock);
483 cpuidle_uninstall_idle_handler();
484 mutex_unlock(&cpuidle_lock);
485}
486
487/* Currently used in suspend/resume path to resume cpuidle */
488void cpuidle_resume(void)
489{
490 mutex_lock(&cpuidle_lock);
491 cpuidle_install_idle_handler();
492 mutex_unlock(&cpuidle_lock);
493}
494
4f86d3a8
LB
495/**
496 * cpuidle_enable_device - enables idle PM for a CPU
497 * @dev: the CPU
498 *
499 * This function must be called between cpuidle_pause_and_lock and
500 * cpuidle_resume_and_unlock when used externally.
501 */
502int cpuidle_enable_device(struct cpuidle_device *dev)
503{
5df0aa73 504 int ret;
bf4d1b5d 505 struct cpuidle_driver *drv;
4f86d3a8 506
1b0a0e9a
SB
507 if (!dev)
508 return -EINVAL;
509
4f86d3a8
LB
510 if (dev->enabled)
511 return 0;
bf4d1b5d 512
e7b06a09
GJ
513 if (!cpuidle_curr_governor)
514 return -EIO;
515
bf4d1b5d
DL
516 drv = cpuidle_get_cpu_driver(dev);
517
e7b06a09 518 if (!drv)
4f86d3a8 519 return -EIO;
bf4d1b5d 520
10b9d3f8
DL
521 if (!dev->registered)
522 return -EINVAL;
523
bf4d1b5d
DL
524 ret = cpuidle_add_device_sysfs(dev);
525 if (ret)
4f86d3a8
LB
526 return ret;
527
3fc74bd8
GJ
528 if (cpuidle_curr_governor->enable) {
529 ret = cpuidle_curr_governor->enable(drv, dev);
530 if (ret)
531 goto fail_sysfs;
532 }
4f86d3a8 533
4f86d3a8
LB
534 smp_wmb();
535
536 dev->enabled = 1;
537
538 enabled_devices++;
539 return 0;
540
541fail_sysfs:
bf4d1b5d 542 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
543
544 return ret;
545}
546
547EXPORT_SYMBOL_GPL(cpuidle_enable_device);
548
549/**
550 * cpuidle_disable_device - disables idle PM for a CPU
551 * @dev: the CPU
552 *
553 * This function must be called between cpuidle_pause_and_lock and
554 * cpuidle_resume_and_unlock when used externally.
555 */
556void cpuidle_disable_device(struct cpuidle_device *dev)
557{
bf4d1b5d
DL
558 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
559
cf31cd1a 560 if (!dev || !dev->enabled)
4f86d3a8 561 return;
bf4d1b5d
DL
562
563 if (!drv || !cpuidle_curr_governor)
4f86d3a8
LB
564 return;
565
566 dev->enabled = 0;
567
568 if (cpuidle_curr_governor->disable)
bf4d1b5d 569 cpuidle_curr_governor->disable(drv, dev);
4f86d3a8 570
bf4d1b5d 571 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
572 enabled_devices--;
573}
574
575EXPORT_SYMBOL_GPL(cpuidle_disable_device);
576
f6bb51a5
DL
577static void __cpuidle_unregister_device(struct cpuidle_device *dev)
578{
579 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
580
581 list_del(&dev->device_list);
582 per_cpu(cpuidle_devices, dev->cpu) = NULL;
583 module_put(drv->owner);
c998c078
DG
584
585 dev->registered = 0;
f6bb51a5
DL
586}
587
267d4bf8 588static void __cpuidle_device_init(struct cpuidle_device *dev)
5df0aa73
DL
589{
590 memset(dev->states_usage, 0, sizeof(dev->states_usage));
c1d51f68 591 dev->last_residency_ns = 0;
6f9b83ac 592 dev->next_hrtimer = 0;
5df0aa73
DL
593}
594
4f86d3a8 595/**
dcb84f33
VP
596 * __cpuidle_register_device - internal register function called before register
597 * and enable routines
4f86d3a8 598 * @dev: the cpu
dcb84f33
VP
599 *
600 * cpuidle_lock mutex must be held before this is called
4f86d3a8 601 */
dcb84f33 602static int __cpuidle_register_device(struct cpuidle_device *dev)
4f86d3a8 603{
bf4d1b5d 604 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
99e98d3f 605 int i, ret;
4f86d3a8 606
bf4d1b5d 607 if (!try_module_get(drv->owner))
4f86d3a8
LB
608 return -EINVAL;
609
75a80267 610 for (i = 0; i < drv->state_count; i++) {
ba1e78a1 611 if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
99e98d3f
RW
612 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
613
75a80267
RW
614 if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
615 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
616 }
617
4f86d3a8
LB
618 per_cpu(cpuidle_devices, dev->cpu) = dev;
619 list_add(&dev->device_list, &cpuidle_detected_devices);
4f86d3a8 620
4126c019 621 ret = cpuidle_coupled_register_device(dev);
47182668 622 if (ret)
f6bb51a5 623 __cpuidle_unregister_device(dev);
47182668
VK
624 else
625 dev->registered = 1;
4f86d3a8 626
47182668 627 return ret;
dcb84f33
VP
628}
629
630/**
631 * cpuidle_register_device - registers a CPU's idle PM feature
632 * @dev: the cpu
633 */
634int cpuidle_register_device(struct cpuidle_device *dev)
635{
c878a52d 636 int ret = -EBUSY;
dcb84f33 637
1b0a0e9a
SB
638 if (!dev)
639 return -EINVAL;
640
dcb84f33
VP
641 mutex_lock(&cpuidle_lock);
642
c878a52d
DL
643 if (dev->registered)
644 goto out_unlock;
645
267d4bf8 646 __cpuidle_device_init(dev);
5df0aa73 647
f6bb51a5
DL
648 ret = __cpuidle_register_device(dev);
649 if (ret)
650 goto out_unlock;
651
652 ret = cpuidle_add_sysfs(dev);
653 if (ret)
654 goto out_unregister;
dcb84f33 655
10b9d3f8 656 ret = cpuidle_enable_device(dev);
f6bb51a5
DL
657 if (ret)
658 goto out_sysfs;
10b9d3f8 659
4f86d3a8
LB
660 cpuidle_install_idle_handler();
661
f6bb51a5 662out_unlock:
4f86d3a8
LB
663 mutex_unlock(&cpuidle_lock);
664
f6bb51a5
DL
665 return ret;
666
667out_sysfs:
668 cpuidle_remove_sysfs(dev);
669out_unregister:
670 __cpuidle_unregister_device(dev);
671 goto out_unlock;
4f86d3a8
LB
672}
673
674EXPORT_SYMBOL_GPL(cpuidle_register_device);
675
676/**
677 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
678 * @dev: the cpu
679 */
680void cpuidle_unregister_device(struct cpuidle_device *dev)
681{
813e8e3d 682 if (!dev || dev->registered == 0)
dcb84f33
VP
683 return;
684
4f86d3a8
LB
685 cpuidle_pause_and_lock();
686
687 cpuidle_disable_device(dev);
688
1aef40e2 689 cpuidle_remove_sysfs(dev);
f6bb51a5
DL
690
691 __cpuidle_unregister_device(dev);
4f86d3a8 692
4126c019
CC
693 cpuidle_coupled_unregister_device(dev);
694
4f86d3a8 695 cpuidle_resume_and_unlock();
4f86d3a8
LB
696}
697
698EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
699
1c192d04 700/**
4c637b21
DL
701 * cpuidle_unregister: unregister a driver and the devices. This function
702 * can be used only if the driver has been previously registered through
703 * the cpuidle_register function.
704 *
705 * @drv: a valid pointer to a struct cpuidle_driver
706 */
707void cpuidle_unregister(struct cpuidle_driver *drv)
708{
709 int cpu;
710 struct cpuidle_device *device;
711
82467a5a 712 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
713 device = &per_cpu(cpuidle_dev, cpu);
714 cpuidle_unregister_device(device);
715 }
716
717 cpuidle_unregister_driver(drv);
718}
719EXPORT_SYMBOL_GPL(cpuidle_unregister);
720
721/**
722 * cpuidle_register: registers the driver and the cpu devices with the
723 * coupled_cpus passed as parameter. This function is used for all common
724 * initialization pattern there are in the arch specific drivers. The
725 * devices is globally defined in this file.
726 *
727 * @drv : a valid pointer to a struct cpuidle_driver
728 * @coupled_cpus: a cpumask for the coupled states
729 *
730 * Returns 0 on success, < 0 otherwise
731 */
732int cpuidle_register(struct cpuidle_driver *drv,
733 const struct cpumask *const coupled_cpus)
734{
735 int ret, cpu;
736 struct cpuidle_device *device;
737
738 ret = cpuidle_register_driver(drv);
739 if (ret) {
740 pr_err("failed to register cpuidle driver\n");
741 return ret;
742 }
743
82467a5a 744 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
745 device = &per_cpu(cpuidle_dev, cpu);
746 device->cpu = cpu;
747
748#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
749 /*
caf4a36e 750 * On multiplatform for ARM, the coupled idle states could be
4c637b21
DL
751 * enabled in the kernel even if the cpuidle driver does not
752 * use it. Note, coupled_cpus is a struct copy.
753 */
754 if (coupled_cpus)
755 device->coupled_cpus = *coupled_cpus;
756#endif
757 ret = cpuidle_register_device(device);
758 if (!ret)
759 continue;
760
761 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
762
763 cpuidle_unregister(drv);
764 break;
765 }
766
767 return ret;
768}
769EXPORT_SYMBOL_GPL(cpuidle_register);
770
4f86d3a8
LB
771/**
772 * cpuidle_init - core initializer
773 */
774static int __init cpuidle_init(void)
775{
62027aea
LB
776 if (cpuidle_disabled())
777 return -ENODEV;
778
3a4a0042 779 return cpuidle_add_interface(cpu_subsys.dev_root);
4f86d3a8
LB
780}
781
62027aea 782module_param(off, int, 0444);
61cb5758 783module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
4f86d3a8 784core_initcall(cpuidle_init);