PM / Domains: Add "wait for parent" status for generic PM domains
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / power / domain.c
CommitLineData
f721889f
RW
1/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h>
14#include <linux/slab.h>
15#include <linux/err.h>
17b75eca
RW
16#include <linux/sched.h>
17#include <linux/suspend.h>
f721889f 18
5125bbf3
RW
19static LIST_HEAD(gpd_list);
20static DEFINE_MUTEX(gpd_list_lock);
21
5248051b
RW
22#ifdef CONFIG_PM
23
24static struct generic_pm_domain *dev_to_genpd(struct device *dev)
25{
26 if (IS_ERR_OR_NULL(dev->pm_domain))
27 return ERR_PTR(-EINVAL);
28
596ba34b 29 return pd_to_genpd(dev->pm_domain);
5248051b 30}
f721889f 31
c4bb3160 32static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
f721889f 33{
c4bb3160
RW
34 bool ret = false;
35
36 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
37 ret = !!atomic_dec_and_test(&genpd->sd_count);
38
39 return ret;
40}
41
42static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
43{
44 atomic_inc(&genpd->sd_count);
45 smp_mb__after_atomic_inc();
f721889f
RW
46}
47
17b75eca
RW
48static void genpd_acquire_lock(struct generic_pm_domain *genpd)
49{
50 DEFINE_WAIT(wait);
51
52 mutex_lock(&genpd->lock);
53 /*
54 * Wait for the domain to transition into either the active,
55 * or the power off state.
56 */
57 for (;;) {
58 prepare_to_wait(&genpd->status_wait_queue, &wait,
59 TASK_UNINTERRUPTIBLE);
c6d22b37
RW
60 if (genpd->status == GPD_STATE_ACTIVE
61 || genpd->status == GPD_STATE_POWER_OFF)
17b75eca
RW
62 break;
63 mutex_unlock(&genpd->lock);
64
65 schedule();
66
67 mutex_lock(&genpd->lock);
68 }
69 finish_wait(&genpd->status_wait_queue, &wait);
70}
71
72static void genpd_release_lock(struct generic_pm_domain *genpd)
73{
74 mutex_unlock(&genpd->lock);
75}
76
c6d22b37
RW
77static void genpd_set_active(struct generic_pm_domain *genpd)
78{
79 if (genpd->resume_count == 0)
80 genpd->status = GPD_STATE_ACTIVE;
81}
82
5248051b 83/**
3f241775 84 * __pm_genpd_poweron - Restore power to a given PM domain and its parents.
5248051b
RW
85 * @genpd: PM domain to power up.
86 *
87 * Restore power to @genpd and all of its parents so that it is possible to
88 * resume a device belonging to it.
89 */
3f241775
RW
90int __pm_genpd_poweron(struct generic_pm_domain *genpd)
91 __releases(&genpd->lock) __acquires(&genpd->lock)
5248051b 92{
3f241775 93 DEFINE_WAIT(wait);
5248051b
RW
94 int ret = 0;
95
3f241775
RW
96 /* If the domain's parent is being waited for, we have to wait too. */
97 for (;;) {
98 prepare_to_wait(&genpd->status_wait_queue, &wait,
99 TASK_UNINTERRUPTIBLE);
100 if (genpd->status != GPD_STATE_WAIT_PARENT)
101 break;
102 mutex_unlock(&genpd->lock);
17b75eca 103
3f241775
RW
104 schedule();
105
106 mutex_lock(&genpd->lock);
107 }
108 finish_wait(&genpd->status_wait_queue, &wait);
9e08cf42 109
17b75eca 110 if (genpd->status == GPD_STATE_ACTIVE
596ba34b 111 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
3f241775 112 return 0;
5248051b 113
c6d22b37
RW
114 if (genpd->status != GPD_STATE_POWER_OFF) {
115 genpd_set_active(genpd);
3f241775 116 return 0;
c6d22b37
RW
117 }
118
3f241775
RW
119 if (genpd->parent) {
120 genpd_sd_counter_inc(genpd->parent);
121 genpd->status = GPD_STATE_WAIT_PARENT;
3c07cbc4 122
5248051b 123 mutex_unlock(&genpd->lock);
5248051b 124
3f241775 125 ret = pm_genpd_poweron(genpd->parent);
9e08cf42
RW
126
127 mutex_lock(&genpd->lock);
128
3f241775
RW
129 /*
130 * The "wait for parent" status is guaranteed not to change
131 * while the parent is powering on.
132 */
133 genpd->status = GPD_STATE_POWER_OFF;
134 wake_up_all(&genpd->status_wait_queue);
9e08cf42
RW
135 if (ret)
136 goto err;
5248051b
RW
137 }
138
9e08cf42 139 if (genpd->power_on) {
fe202fde 140 ret = genpd->power_on(genpd);
9e08cf42
RW
141 if (ret)
142 goto err;
3c07cbc4 143 }
5248051b 144
9e08cf42
RW
145 genpd_set_active(genpd);
146
3f241775 147 return 0;
9e08cf42
RW
148
149 err:
150 if (genpd->parent)
151 genpd_sd_counter_dec(genpd->parent);
152
3f241775
RW
153 return ret;
154}
155
156/**
157 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
158 * @genpd: PM domain to power up.
159 */
160int pm_genpd_poweron(struct generic_pm_domain *genpd)
161{
162 int ret;
163
164 mutex_lock(&genpd->lock);
165 ret = __pm_genpd_poweron(genpd);
166 mutex_unlock(&genpd->lock);
167 return ret;
5248051b
RW
168}
169
170#endif /* CONFIG_PM */
171
172#ifdef CONFIG_PM_RUNTIME
173
f721889f
RW
174/**
175 * __pm_genpd_save_device - Save the pre-suspend state of a device.
176 * @dle: Device list entry of the device to save the state of.
177 * @genpd: PM domain the device belongs to.
178 */
179static int __pm_genpd_save_device(struct dev_list_entry *dle,
180 struct generic_pm_domain *genpd)
17b75eca 181 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f
RW
182{
183 struct device *dev = dle->dev;
184 struct device_driver *drv = dev->driver;
185 int ret = 0;
186
187 if (dle->need_restore)
188 return 0;
189
17b75eca
RW
190 mutex_unlock(&genpd->lock);
191
f721889f
RW
192 if (drv && drv->pm && drv->pm->runtime_suspend) {
193 if (genpd->start_device)
194 genpd->start_device(dev);
195
196 ret = drv->pm->runtime_suspend(dev);
197
198 if (genpd->stop_device)
199 genpd->stop_device(dev);
200 }
201
17b75eca
RW
202 mutex_lock(&genpd->lock);
203
f721889f
RW
204 if (!ret)
205 dle->need_restore = true;
206
207 return ret;
208}
209
210/**
211 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
212 * @dle: Device list entry of the device to restore the state of.
213 * @genpd: PM domain the device belongs to.
214 */
215static void __pm_genpd_restore_device(struct dev_list_entry *dle,
216 struct generic_pm_domain *genpd)
17b75eca 217 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f
RW
218{
219 struct device *dev = dle->dev;
220 struct device_driver *drv = dev->driver;
221
222 if (!dle->need_restore)
223 return;
224
17b75eca
RW
225 mutex_unlock(&genpd->lock);
226
f721889f
RW
227 if (drv && drv->pm && drv->pm->runtime_resume) {
228 if (genpd->start_device)
229 genpd->start_device(dev);
230
231 drv->pm->runtime_resume(dev);
232
233 if (genpd->stop_device)
234 genpd->stop_device(dev);
235 }
236
17b75eca
RW
237 mutex_lock(&genpd->lock);
238
f721889f
RW
239 dle->need_restore = false;
240}
241
c6d22b37
RW
242/**
243 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
244 * @genpd: PM domain to check.
245 *
246 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
247 * a "power off" operation, which means that a "power on" has occured in the
248 * meantime, or if its resume_count field is different from zero, which means
249 * that one of its devices has been resumed in the meantime.
250 */
251static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
252{
3f241775
RW
253 return genpd->status == GPD_STATE_WAIT_PARENT
254 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
c6d22b37
RW
255}
256
56375fd4
RW
257/**
258 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
259 * @genpd: PM domait to power off.
260 *
261 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
262 * before.
263 */
0bc5b2de 264void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
56375fd4
RW
265{
266 if (!work_pending(&genpd->power_off_work))
267 queue_work(pm_wq, &genpd->power_off_work);
268}
269
f721889f
RW
270/**
271 * pm_genpd_poweroff - Remove power from a given PM domain.
272 * @genpd: PM domain to power down.
273 *
274 * If all of the @genpd's devices have been suspended and all of its subdomains
275 * have been powered down, run the runtime suspend callbacks provided by all of
276 * the @genpd's devices' drivers and remove power from @genpd.
277 */
278static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
17b75eca 279 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f
RW
280{
281 struct generic_pm_domain *parent;
282 struct dev_list_entry *dle;
283 unsigned int not_suspended;
c6d22b37 284 int ret = 0;
f721889f 285
c6d22b37
RW
286 start:
287 /*
288 * Do not try to power off the domain in the following situations:
289 * (1) The domain is already in the "power off" state.
3f241775 290 * (2) The domain is waiting for its parent to power up.
c6d22b37 291 * (3) One of the domain's devices is being resumed right now.
3f241775 292 * (4) System suspend is in progress.
c6d22b37 293 */
3f241775
RW
294 if (genpd->status == GPD_STATE_POWER_OFF
295 || genpd->status == GPD_STATE_WAIT_PARENT
296 || genpd->resume_count > 0 || genpd->prepared_count > 0)
f721889f
RW
297 return 0;
298
c4bb3160 299 if (atomic_read(&genpd->sd_count) > 0)
f721889f
RW
300 return -EBUSY;
301
302 not_suspended = 0;
303 list_for_each_entry(dle, &genpd->dev_list, node)
304 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
305 not_suspended++;
306
307 if (not_suspended > genpd->in_progress)
308 return -EBUSY;
309
c6d22b37
RW
310 if (genpd->poweroff_task) {
311 /*
312 * Another instance of pm_genpd_poweroff() is executing
313 * callbacks, so tell it to start over and return.
314 */
315 genpd->status = GPD_STATE_REPEAT;
316 return 0;
317 }
318
f721889f
RW
319 if (genpd->gov && genpd->gov->power_down_ok) {
320 if (!genpd->gov->power_down_ok(&genpd->domain))
321 return -EAGAIN;
322 }
323
17b75eca 324 genpd->status = GPD_STATE_BUSY;
c6d22b37 325 genpd->poweroff_task = current;
17b75eca 326
f721889f 327 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
3c07cbc4
RW
328 ret = atomic_read(&genpd->sd_count) == 0 ?
329 __pm_genpd_save_device(dle, genpd) : -EBUSY;
3f241775
RW
330
331 if (genpd_abort_poweroff(genpd))
332 goto out;
333
697a7f37
RW
334 if (ret) {
335 genpd_set_active(genpd);
336 goto out;
337 }
f721889f 338
c6d22b37
RW
339 if (genpd->status == GPD_STATE_REPEAT) {
340 genpd->poweroff_task = NULL;
341 goto start;
342 }
343 }
17b75eca 344
3c07cbc4
RW
345 if (genpd->power_off) {
346 if (atomic_read(&genpd->sd_count) > 0) {
347 ret = -EBUSY;
c6d22b37
RW
348 goto out;
349 }
17b75eca 350
3c07cbc4
RW
351 /*
352 * If sd_count > 0 at this point, one of the children hasn't
353 * managed to call pm_genpd_poweron() for the parent yet after
354 * incrementing it. In that case pm_genpd_poweron() will wait
355 * for us to drop the lock, so we can call .power_off() and let
356 * the pm_genpd_poweron() restore power for us (this shouldn't
357 * happen very often).
358 */
d2805402
RW
359 ret = genpd->power_off(genpd);
360 if (ret == -EBUSY) {
361 genpd_set_active(genpd);
d2805402
RW
362 goto out;
363 }
364 }
f721889f 365
17b75eca 366 genpd->status = GPD_STATE_POWER_OFF;
f721889f 367
3c07cbc4
RW
368 parent = genpd->parent;
369 if (parent && genpd_sd_counter_dec(parent))
370 genpd_queue_power_off_work(parent);
f721889f 371
c6d22b37
RW
372 out:
373 genpd->poweroff_task = NULL;
374 wake_up_all(&genpd->status_wait_queue);
375 return ret;
f721889f
RW
376}
377
378/**
379 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
380 * @work: Work structure used for scheduling the execution of this function.
381 */
382static void genpd_power_off_work_fn(struct work_struct *work)
383{
384 struct generic_pm_domain *genpd;
385
386 genpd = container_of(work, struct generic_pm_domain, power_off_work);
387
17b75eca 388 genpd_acquire_lock(genpd);
f721889f 389 pm_genpd_poweroff(genpd);
17b75eca 390 genpd_release_lock(genpd);
f721889f
RW
391}
392
393/**
394 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
395 * @dev: Device to suspend.
396 *
397 * Carry out a runtime suspend of a device under the assumption that its
398 * pm_domain field points to the domain member of an object of type
399 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
400 */
401static int pm_genpd_runtime_suspend(struct device *dev)
402{
403 struct generic_pm_domain *genpd;
404
405 dev_dbg(dev, "%s()\n", __func__);
406
5248051b
RW
407 genpd = dev_to_genpd(dev);
408 if (IS_ERR(genpd))
f721889f
RW
409 return -EINVAL;
410
f721889f
RW
411 if (genpd->stop_device) {
412 int ret = genpd->stop_device(dev);
413 if (ret)
17b75eca 414 return ret;
f721889f 415 }
17b75eca 416
c6d22b37 417 mutex_lock(&genpd->lock);
f721889f
RW
418 genpd->in_progress++;
419 pm_genpd_poweroff(genpd);
420 genpd->in_progress--;
c6d22b37 421 mutex_unlock(&genpd->lock);
f721889f
RW
422
423 return 0;
424}
425
596ba34b
RW
426/**
427 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
428 * @dev: Device to resume.
429 * @genpd: PM domain the device belongs to.
430 */
431static void __pm_genpd_runtime_resume(struct device *dev,
432 struct generic_pm_domain *genpd)
433{
434 struct dev_list_entry *dle;
435
436 list_for_each_entry(dle, &genpd->dev_list, node) {
437 if (dle->dev == dev) {
438 __pm_genpd_restore_device(dle, genpd);
439 break;
440 }
441 }
596ba34b
RW
442}
443
f721889f
RW
444/**
445 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
446 * @dev: Device to resume.
447 *
448 * Carry out a runtime resume of a device under the assumption that its
449 * pm_domain field points to the domain member of an object of type
450 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
451 */
452static int pm_genpd_runtime_resume(struct device *dev)
453{
454 struct generic_pm_domain *genpd;
c6d22b37 455 DEFINE_WAIT(wait);
f721889f
RW
456 int ret;
457
458 dev_dbg(dev, "%s()\n", __func__);
459
5248051b
RW
460 genpd = dev_to_genpd(dev);
461 if (IS_ERR(genpd))
f721889f
RW
462 return -EINVAL;
463
c6d22b37 464 mutex_lock(&genpd->lock);
3f241775
RW
465 ret = __pm_genpd_poweron(genpd);
466 if (ret) {
467 mutex_unlock(&genpd->lock);
468 return ret;
469 }
17b75eca 470 genpd->status = GPD_STATE_BUSY;
c6d22b37
RW
471 genpd->resume_count++;
472 for (;;) {
473 prepare_to_wait(&genpd->status_wait_queue, &wait,
474 TASK_UNINTERRUPTIBLE);
475 /*
476 * If current is the powering off task, we have been called
477 * reentrantly from one of the device callbacks, so we should
478 * not wait.
479 */
480 if (!genpd->poweroff_task || genpd->poweroff_task == current)
481 break;
482 mutex_unlock(&genpd->lock);
483
484 schedule();
485
486 mutex_lock(&genpd->lock);
487 }
488 finish_wait(&genpd->status_wait_queue, &wait);
596ba34b 489 __pm_genpd_runtime_resume(dev, genpd);
c6d22b37
RW
490 genpd->resume_count--;
491 genpd_set_active(genpd);
17b75eca 492 wake_up_all(&genpd->status_wait_queue);
c6d22b37 493 mutex_unlock(&genpd->lock);
17b75eca
RW
494
495 if (genpd->start_device)
496 genpd->start_device(dev);
f721889f
RW
497
498 return 0;
499}
500
17f2ae7f
RW
501/**
502 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
503 */
504void pm_genpd_poweroff_unused(void)
505{
506 struct generic_pm_domain *genpd;
507
508 mutex_lock(&gpd_list_lock);
509
510 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
511 genpd_queue_power_off_work(genpd);
512
513 mutex_unlock(&gpd_list_lock);
514}
515
f721889f
RW
516#else
517
518static inline void genpd_power_off_work_fn(struct work_struct *work) {}
596ba34b
RW
519static inline void __pm_genpd_runtime_resume(struct device *dev,
520 struct generic_pm_domain *genpd) {}
f721889f
RW
521
522#define pm_genpd_runtime_suspend NULL
523#define pm_genpd_runtime_resume NULL
524
525#endif /* CONFIG_PM_RUNTIME */
526
596ba34b
RW
527#ifdef CONFIG_PM_SLEEP
528
529/**
530 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
531 * @genpd: PM domain to power off, if possible.
532 *
533 * Check if the given PM domain can be powered off (during system suspend or
534 * hibernation) and do that if so. Also, in that case propagate to its parent.
535 *
536 * This function is only called in "noirq" stages of system power transitions,
537 * so it need not acquire locks (all of the "noirq" callbacks are executed
538 * sequentially, so it is guaranteed that it will never run twice in parallel).
539 */
540static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
541{
542 struct generic_pm_domain *parent = genpd->parent;
543
17b75eca 544 if (genpd->status == GPD_STATE_POWER_OFF)
596ba34b
RW
545 return;
546
c4bb3160
RW
547 if (genpd->suspended_count != genpd->device_count
548 || atomic_read(&genpd->sd_count) > 0)
596ba34b
RW
549 return;
550
551 if (genpd->power_off)
552 genpd->power_off(genpd);
553
17b75eca 554 genpd->status = GPD_STATE_POWER_OFF;
596ba34b
RW
555 if (parent) {
556 genpd_sd_counter_dec(parent);
557 pm_genpd_sync_poweroff(parent);
558 }
559}
560
4ecd6e65
RW
561/**
562 * resume_needed - Check whether to resume a device before system suspend.
563 * @dev: Device to check.
564 * @genpd: PM domain the device belongs to.
565 *
566 * There are two cases in which a device that can wake up the system from sleep
567 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
568 * to wake up the system and it has to remain active for this purpose while the
569 * system is in the sleep state and (2) if the device is not enabled to wake up
570 * the system from sleep states and it generally doesn't generate wakeup signals
571 * by itself (those signals are generated on its behalf by other parts of the
572 * system). In the latter case it may be necessary to reconfigure the device's
573 * wakeup settings during system suspend, because it may have been set up to
574 * signal remote wakeup from the system's working state as needed by runtime PM.
575 * Return 'true' in either of the above cases.
576 */
577static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
578{
579 bool active_wakeup;
580
581 if (!device_can_wakeup(dev))
582 return false;
583
584 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
585 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
586}
587
596ba34b
RW
588/**
589 * pm_genpd_prepare - Start power transition of a device in a PM domain.
590 * @dev: Device to start the transition of.
591 *
592 * Start a power transition of a device (during a system-wide power transition)
593 * under the assumption that its pm_domain field points to the domain member of
594 * an object of type struct generic_pm_domain representing a PM domain
595 * consisting of I/O devices.
596 */
597static int pm_genpd_prepare(struct device *dev)
598{
599 struct generic_pm_domain *genpd;
b6c10c84 600 int ret;
596ba34b
RW
601
602 dev_dbg(dev, "%s()\n", __func__);
603
604 genpd = dev_to_genpd(dev);
605 if (IS_ERR(genpd))
606 return -EINVAL;
607
17b75eca
RW
608 /*
609 * If a wakeup request is pending for the device, it should be woken up
610 * at this point and a system wakeup event should be reported if it's
611 * set up to wake up the system from sleep states.
612 */
613 pm_runtime_get_noresume(dev);
614 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
615 pm_wakeup_event(dev, 0);
616
617 if (pm_wakeup_pending()) {
618 pm_runtime_put_sync(dev);
619 return -EBUSY;
620 }
621
4ecd6e65
RW
622 if (resume_needed(dev, genpd))
623 pm_runtime_resume(dev);
624
17b75eca 625 genpd_acquire_lock(genpd);
596ba34b
RW
626
627 if (genpd->prepared_count++ == 0)
17b75eca
RW
628 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
629
630 genpd_release_lock(genpd);
596ba34b
RW
631
632 if (genpd->suspend_power_off) {
17b75eca 633 pm_runtime_put_noidle(dev);
596ba34b
RW
634 return 0;
635 }
636
637 /*
17b75eca
RW
638 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
639 * so pm_genpd_poweron() will return immediately, but if the device
640 * is suspended (e.g. it's been stopped by .stop_device()), we need
641 * to make it operational.
596ba34b 642 */
17b75eca 643 pm_runtime_resume(dev);
596ba34b
RW
644 __pm_runtime_disable(dev, false);
645
b6c10c84
RW
646 ret = pm_generic_prepare(dev);
647 if (ret) {
648 mutex_lock(&genpd->lock);
649
650 if (--genpd->prepared_count == 0)
651 genpd->suspend_power_off = false;
652
653 mutex_unlock(&genpd->lock);
17b75eca 654 pm_runtime_enable(dev);
b6c10c84 655 }
17b75eca
RW
656
657 pm_runtime_put_sync(dev);
b6c10c84 658 return ret;
596ba34b
RW
659}
660
661/**
662 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
663 * @dev: Device to suspend.
664 *
665 * Suspend a device under the assumption that its pm_domain field points to the
666 * domain member of an object of type struct generic_pm_domain representing
667 * a PM domain consisting of I/O devices.
668 */
669static int pm_genpd_suspend(struct device *dev)
670{
671 struct generic_pm_domain *genpd;
672
673 dev_dbg(dev, "%s()\n", __func__);
674
675 genpd = dev_to_genpd(dev);
676 if (IS_ERR(genpd))
677 return -EINVAL;
678
679 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
680}
681
682/**
683 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
684 * @dev: Device to suspend.
685 *
686 * Carry out a late suspend of a device under the assumption that its
687 * pm_domain field points to the domain member of an object of type
688 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
689 */
690static int pm_genpd_suspend_noirq(struct device *dev)
691{
692 struct generic_pm_domain *genpd;
693 int ret;
694
695 dev_dbg(dev, "%s()\n", __func__);
696
697 genpd = dev_to_genpd(dev);
698 if (IS_ERR(genpd))
699 return -EINVAL;
700
701 if (genpd->suspend_power_off)
702 return 0;
703
704 ret = pm_generic_suspend_noirq(dev);
705 if (ret)
706 return ret;
707
d4f2d87a
RW
708 if (device_may_wakeup(dev)
709 && genpd->active_wakeup && genpd->active_wakeup(dev))
710 return 0;
711
596ba34b
RW
712 if (genpd->stop_device)
713 genpd->stop_device(dev);
714
715 /*
716 * Since all of the "noirq" callbacks are executed sequentially, it is
717 * guaranteed that this function will never run twice in parallel for
718 * the same PM domain, so it is not necessary to use locking here.
719 */
720 genpd->suspended_count++;
721 pm_genpd_sync_poweroff(genpd);
722
723 return 0;
724}
725
726/**
727 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
728 * @dev: Device to resume.
729 *
730 * Carry out an early resume of a device under the assumption that its
731 * pm_domain field points to the domain member of an object of type
732 * struct generic_pm_domain representing a power domain consisting of I/O
733 * devices.
734 */
735static int pm_genpd_resume_noirq(struct device *dev)
736{
737 struct generic_pm_domain *genpd;
738
739 dev_dbg(dev, "%s()\n", __func__);
740
741 genpd = dev_to_genpd(dev);
742 if (IS_ERR(genpd))
743 return -EINVAL;
744
745 if (genpd->suspend_power_off)
746 return 0;
747
748 /*
749 * Since all of the "noirq" callbacks are executed sequentially, it is
750 * guaranteed that this function will never run twice in parallel for
751 * the same PM domain, so it is not necessary to use locking here.
752 */
753 pm_genpd_poweron(genpd);
754 genpd->suspended_count--;
755 if (genpd->start_device)
756 genpd->start_device(dev);
757
758 return pm_generic_resume_noirq(dev);
759}
760
761/**
762 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
763 * @dev: Device to resume.
764 *
765 * Resume a device under the assumption that its pm_domain field points to the
766 * domain member of an object of type struct generic_pm_domain representing
767 * a power domain consisting of I/O devices.
768 */
769static int pm_genpd_resume(struct device *dev)
770{
771 struct generic_pm_domain *genpd;
772
773 dev_dbg(dev, "%s()\n", __func__);
774
775 genpd = dev_to_genpd(dev);
776 if (IS_ERR(genpd))
777 return -EINVAL;
778
779 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
780}
781
782/**
783 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
784 * @dev: Device to freeze.
785 *
786 * Freeze a device under the assumption that its pm_domain field points to the
787 * domain member of an object of type struct generic_pm_domain representing
788 * a power domain consisting of I/O devices.
789 */
790static int pm_genpd_freeze(struct device *dev)
791{
792 struct generic_pm_domain *genpd;
793
794 dev_dbg(dev, "%s()\n", __func__);
795
796 genpd = dev_to_genpd(dev);
797 if (IS_ERR(genpd))
798 return -EINVAL;
799
800 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
801}
802
803/**
804 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
805 * @dev: Device to freeze.
806 *
807 * Carry out a late freeze of a device under the assumption that its
808 * pm_domain field points to the domain member of an object of type
809 * struct generic_pm_domain representing a power domain consisting of I/O
810 * devices.
811 */
812static int pm_genpd_freeze_noirq(struct device *dev)
813{
814 struct generic_pm_domain *genpd;
815 int ret;
816
817 dev_dbg(dev, "%s()\n", __func__);
818
819 genpd = dev_to_genpd(dev);
820 if (IS_ERR(genpd))
821 return -EINVAL;
822
823 if (genpd->suspend_power_off)
824 return 0;
825
826 ret = pm_generic_freeze_noirq(dev);
827 if (ret)
828 return ret;
829
830 if (genpd->stop_device)
831 genpd->stop_device(dev);
832
833 return 0;
834}
835
836/**
837 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
838 * @dev: Device to thaw.
839 *
840 * Carry out an early thaw of a device under the assumption that its
841 * pm_domain field points to the domain member of an object of type
842 * struct generic_pm_domain representing a power domain consisting of I/O
843 * devices.
844 */
845static int pm_genpd_thaw_noirq(struct device *dev)
846{
847 struct generic_pm_domain *genpd;
848
849 dev_dbg(dev, "%s()\n", __func__);
850
851 genpd = dev_to_genpd(dev);
852 if (IS_ERR(genpd))
853 return -EINVAL;
854
855 if (genpd->suspend_power_off)
856 return 0;
857
858 if (genpd->start_device)
859 genpd->start_device(dev);
860
861 return pm_generic_thaw_noirq(dev);
862}
863
864/**
865 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
866 * @dev: Device to thaw.
867 *
868 * Thaw a device under the assumption that its pm_domain field points to the
869 * domain member of an object of type struct generic_pm_domain representing
870 * a power domain consisting of I/O devices.
871 */
872static int pm_genpd_thaw(struct device *dev)
873{
874 struct generic_pm_domain *genpd;
875
876 dev_dbg(dev, "%s()\n", __func__);
877
878 genpd = dev_to_genpd(dev);
879 if (IS_ERR(genpd))
880 return -EINVAL;
881
882 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
883}
884
885/**
886 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
887 * @dev: Device to suspend.
888 *
889 * Power off a device under the assumption that its pm_domain field points to
890 * the domain member of an object of type struct generic_pm_domain representing
891 * a PM domain consisting of I/O devices.
892 */
893static int pm_genpd_dev_poweroff(struct device *dev)
894{
895 struct generic_pm_domain *genpd;
896
897 dev_dbg(dev, "%s()\n", __func__);
898
899 genpd = dev_to_genpd(dev);
900 if (IS_ERR(genpd))
901 return -EINVAL;
902
903 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
904}
905
906/**
907 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
908 * @dev: Device to suspend.
909 *
910 * Carry out a late powering off of a device under the assumption that its
911 * pm_domain field points to the domain member of an object of type
912 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
913 */
914static int pm_genpd_dev_poweroff_noirq(struct device *dev)
915{
916 struct generic_pm_domain *genpd;
917 int ret;
918
919 dev_dbg(dev, "%s()\n", __func__);
920
921 genpd = dev_to_genpd(dev);
922 if (IS_ERR(genpd))
923 return -EINVAL;
924
925 if (genpd->suspend_power_off)
926 return 0;
927
928 ret = pm_generic_poweroff_noirq(dev);
929 if (ret)
930 return ret;
931
d4f2d87a
RW
932 if (device_may_wakeup(dev)
933 && genpd->active_wakeup && genpd->active_wakeup(dev))
934 return 0;
935
596ba34b
RW
936 if (genpd->stop_device)
937 genpd->stop_device(dev);
938
939 /*
940 * Since all of the "noirq" callbacks are executed sequentially, it is
941 * guaranteed that this function will never run twice in parallel for
942 * the same PM domain, so it is not necessary to use locking here.
943 */
944 genpd->suspended_count++;
945 pm_genpd_sync_poweroff(genpd);
946
947 return 0;
948}
949
950/**
951 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
952 * @dev: Device to resume.
953 *
954 * Carry out an early restore of a device under the assumption that its
955 * pm_domain field points to the domain member of an object of type
956 * struct generic_pm_domain representing a power domain consisting of I/O
957 * devices.
958 */
959static int pm_genpd_restore_noirq(struct device *dev)
960{
961 struct generic_pm_domain *genpd;
962
963 dev_dbg(dev, "%s()\n", __func__);
964
965 genpd = dev_to_genpd(dev);
966 if (IS_ERR(genpd))
967 return -EINVAL;
968
969 /*
970 * Since all of the "noirq" callbacks are executed sequentially, it is
971 * guaranteed that this function will never run twice in parallel for
972 * the same PM domain, so it is not necessary to use locking here.
973 */
17b75eca 974 genpd->status = GPD_STATE_POWER_OFF;
596ba34b
RW
975 if (genpd->suspend_power_off) {
976 /*
977 * The boot kernel might put the domain into the power on state,
978 * so make sure it really is powered off.
979 */
980 if (genpd->power_off)
981 genpd->power_off(genpd);
982 return 0;
983 }
984
985 pm_genpd_poweron(genpd);
986 genpd->suspended_count--;
987 if (genpd->start_device)
988 genpd->start_device(dev);
989
990 return pm_generic_restore_noirq(dev);
991}
992
993/**
994 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
995 * @dev: Device to resume.
996 *
997 * Restore a device under the assumption that its pm_domain field points to the
998 * domain member of an object of type struct generic_pm_domain representing
999 * a power domain consisting of I/O devices.
1000 */
1001static int pm_genpd_restore(struct device *dev)
1002{
1003 struct generic_pm_domain *genpd;
1004
1005 dev_dbg(dev, "%s()\n", __func__);
1006
1007 genpd = dev_to_genpd(dev);
1008 if (IS_ERR(genpd))
1009 return -EINVAL;
1010
1011 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
1012}
1013
1014/**
1015 * pm_genpd_complete - Complete power transition of a device in a power domain.
1016 * @dev: Device to complete the transition of.
1017 *
1018 * Complete a power transition of a device (during a system-wide power
1019 * transition) under the assumption that its pm_domain field points to the
1020 * domain member of an object of type struct generic_pm_domain representing
1021 * a power domain consisting of I/O devices.
1022 */
1023static void pm_genpd_complete(struct device *dev)
1024{
1025 struct generic_pm_domain *genpd;
1026 bool run_complete;
1027
1028 dev_dbg(dev, "%s()\n", __func__);
1029
1030 genpd = dev_to_genpd(dev);
1031 if (IS_ERR(genpd))
1032 return;
1033
1034 mutex_lock(&genpd->lock);
1035
1036 run_complete = !genpd->suspend_power_off;
1037 if (--genpd->prepared_count == 0)
1038 genpd->suspend_power_off = false;
1039
1040 mutex_unlock(&genpd->lock);
1041
1042 if (run_complete) {
1043 pm_generic_complete(dev);
6f00ff78 1044 pm_runtime_set_active(dev);
596ba34b 1045 pm_runtime_enable(dev);
6f00ff78 1046 pm_runtime_idle(dev);
596ba34b
RW
1047 }
1048}
1049
1050#else
1051
1052#define pm_genpd_prepare NULL
1053#define pm_genpd_suspend NULL
1054#define pm_genpd_suspend_noirq NULL
1055#define pm_genpd_resume_noirq NULL
1056#define pm_genpd_resume NULL
1057#define pm_genpd_freeze NULL
1058#define pm_genpd_freeze_noirq NULL
1059#define pm_genpd_thaw_noirq NULL
1060#define pm_genpd_thaw NULL
1061#define pm_genpd_dev_poweroff_noirq NULL
1062#define pm_genpd_dev_poweroff NULL
1063#define pm_genpd_restore_noirq NULL
1064#define pm_genpd_restore NULL
1065#define pm_genpd_complete NULL
1066
1067#endif /* CONFIG_PM_SLEEP */
1068
f721889f
RW
1069/**
1070 * pm_genpd_add_device - Add a device to an I/O PM domain.
1071 * @genpd: PM domain to add the device to.
1072 * @dev: Device to be added.
1073 */
1074int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1075{
1076 struct dev_list_entry *dle;
1077 int ret = 0;
1078
1079 dev_dbg(dev, "%s()\n", __func__);
1080
1081 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1082 return -EINVAL;
1083
17b75eca 1084 genpd_acquire_lock(genpd);
f721889f 1085
17b75eca 1086 if (genpd->status == GPD_STATE_POWER_OFF) {
f721889f
RW
1087 ret = -EINVAL;
1088 goto out;
1089 }
1090
596ba34b
RW
1091 if (genpd->prepared_count > 0) {
1092 ret = -EAGAIN;
1093 goto out;
1094 }
1095
f721889f
RW
1096 list_for_each_entry(dle, &genpd->dev_list, node)
1097 if (dle->dev == dev) {
1098 ret = -EINVAL;
1099 goto out;
1100 }
1101
1102 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1103 if (!dle) {
1104 ret = -ENOMEM;
1105 goto out;
1106 }
1107
1108 dle->dev = dev;
1109 dle->need_restore = false;
1110 list_add_tail(&dle->node, &genpd->dev_list);
596ba34b 1111 genpd->device_count++;
f721889f
RW
1112
1113 spin_lock_irq(&dev->power.lock);
1114 dev->pm_domain = &genpd->domain;
1115 spin_unlock_irq(&dev->power.lock);
1116
1117 out:
17b75eca 1118 genpd_release_lock(genpd);
f721889f
RW
1119
1120 return ret;
1121}
1122
1123/**
1124 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1125 * @genpd: PM domain to remove the device from.
1126 * @dev: Device to be removed.
1127 */
1128int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1129 struct device *dev)
1130{
1131 struct dev_list_entry *dle;
1132 int ret = -EINVAL;
1133
1134 dev_dbg(dev, "%s()\n", __func__);
1135
1136 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1137 return -EINVAL;
1138
17b75eca 1139 genpd_acquire_lock(genpd);
f721889f 1140
596ba34b
RW
1141 if (genpd->prepared_count > 0) {
1142 ret = -EAGAIN;
1143 goto out;
1144 }
1145
f721889f
RW
1146 list_for_each_entry(dle, &genpd->dev_list, node) {
1147 if (dle->dev != dev)
1148 continue;
1149
1150 spin_lock_irq(&dev->power.lock);
1151 dev->pm_domain = NULL;
1152 spin_unlock_irq(&dev->power.lock);
1153
596ba34b 1154 genpd->device_count--;
f721889f
RW
1155 list_del(&dle->node);
1156 kfree(dle);
1157
1158 ret = 0;
1159 break;
1160 }
1161
596ba34b 1162 out:
17b75eca 1163 genpd_release_lock(genpd);
f721889f
RW
1164
1165 return ret;
1166}
1167
1168/**
1169 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1170 * @genpd: Master PM domain to add the subdomain to.
1171 * @new_subdomain: Subdomain to be added.
1172 */
1173int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1174 struct generic_pm_domain *new_subdomain)
1175{
1176 struct generic_pm_domain *subdomain;
1177 int ret = 0;
1178
1179 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1180 return -EINVAL;
1181
17b75eca
RW
1182 start:
1183 genpd_acquire_lock(genpd);
1184 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
f721889f 1185
17b75eca
RW
1186 if (new_subdomain->status != GPD_STATE_POWER_OFF
1187 && new_subdomain->status != GPD_STATE_ACTIVE) {
1188 mutex_unlock(&new_subdomain->lock);
1189 genpd_release_lock(genpd);
1190 goto start;
1191 }
1192
1193 if (genpd->status == GPD_STATE_POWER_OFF
1194 && new_subdomain->status != GPD_STATE_POWER_OFF) {
f721889f
RW
1195 ret = -EINVAL;
1196 goto out;
1197 }
1198
1199 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1200 if (subdomain == new_subdomain) {
1201 ret = -EINVAL;
1202 goto out;
1203 }
1204 }
1205
f721889f
RW
1206 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1207 new_subdomain->parent = genpd;
17b75eca 1208 if (subdomain->status != GPD_STATE_POWER_OFF)
c4bb3160 1209 genpd_sd_counter_inc(genpd);
f721889f 1210
f721889f 1211 out:
17b75eca
RW
1212 mutex_unlock(&new_subdomain->lock);
1213 genpd_release_lock(genpd);
f721889f
RW
1214
1215 return ret;
1216}
1217
1218/**
1219 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1220 * @genpd: Master PM domain to remove the subdomain from.
1221 * @target: Subdomain to be removed.
1222 */
1223int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1224 struct generic_pm_domain *target)
1225{
1226 struct generic_pm_domain *subdomain;
1227 int ret = -EINVAL;
1228
1229 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1230 return -EINVAL;
1231
17b75eca
RW
1232 start:
1233 genpd_acquire_lock(genpd);
f721889f
RW
1234
1235 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1236 if (subdomain != target)
1237 continue;
1238
1239 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1240
17b75eca
RW
1241 if (subdomain->status != GPD_STATE_POWER_OFF
1242 && subdomain->status != GPD_STATE_ACTIVE) {
1243 mutex_unlock(&subdomain->lock);
1244 genpd_release_lock(genpd);
1245 goto start;
1246 }
1247
f721889f
RW
1248 list_del(&subdomain->sd_node);
1249 subdomain->parent = NULL;
17b75eca 1250 if (subdomain->status != GPD_STATE_POWER_OFF)
f721889f
RW
1251 genpd_sd_counter_dec(genpd);
1252
1253 mutex_unlock(&subdomain->lock);
1254
1255 ret = 0;
1256 break;
1257 }
1258
17b75eca 1259 genpd_release_lock(genpd);
f721889f
RW
1260
1261 return ret;
1262}
1263
1264/**
1265 * pm_genpd_init - Initialize a generic I/O PM domain object.
1266 * @genpd: PM domain object to initialize.
1267 * @gov: PM domain governor to associate with the domain (may be NULL).
1268 * @is_off: Initial value of the domain's power_is_off field.
1269 */
1270void pm_genpd_init(struct generic_pm_domain *genpd,
1271 struct dev_power_governor *gov, bool is_off)
1272{
1273 if (IS_ERR_OR_NULL(genpd))
1274 return;
1275
1276 INIT_LIST_HEAD(&genpd->sd_node);
1277 genpd->parent = NULL;
1278 INIT_LIST_HEAD(&genpd->dev_list);
1279 INIT_LIST_HEAD(&genpd->sd_list);
1280 mutex_init(&genpd->lock);
1281 genpd->gov = gov;
1282 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1283 genpd->in_progress = 0;
c4bb3160 1284 atomic_set(&genpd->sd_count, 0);
17b75eca
RW
1285 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1286 init_waitqueue_head(&genpd->status_wait_queue);
c6d22b37
RW
1287 genpd->poweroff_task = NULL;
1288 genpd->resume_count = 0;
596ba34b
RW
1289 genpd->device_count = 0;
1290 genpd->suspended_count = 0;
f721889f
RW
1291 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1292 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1293 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
596ba34b
RW
1294 genpd->domain.ops.prepare = pm_genpd_prepare;
1295 genpd->domain.ops.suspend = pm_genpd_suspend;
1296 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1297 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1298 genpd->domain.ops.resume = pm_genpd_resume;
1299 genpd->domain.ops.freeze = pm_genpd_freeze;
1300 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1301 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1302 genpd->domain.ops.thaw = pm_genpd_thaw;
1303 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1304 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1305 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1306 genpd->domain.ops.restore = pm_genpd_restore;
1307 genpd->domain.ops.complete = pm_genpd_complete;
5125bbf3
RW
1308 mutex_lock(&gpd_list_lock);
1309 list_add(&genpd->gpd_list_node, &gpd_list);
1310 mutex_unlock(&gpd_list_lock);
1311}