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