2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_domain.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
19 static LIST_HEAD(gpd_list);
20 static DEFINE_MUTEX(gpd_list_lock);
24 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
26 if (IS_ERR_OR_NULL(dev->pm_domain))
27 return ERR_PTR(-EINVAL);
29 return pd_to_genpd(dev->pm_domain);
32 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
36 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
37 ret = !!atomic_dec_and_test(&genpd->sd_count);
42 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
44 atomic_inc(&genpd->sd_count);
45 smp_mb__after_atomic_inc();
48 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
52 mutex_lock(&genpd->lock);
54 * Wait for the domain to transition into either the active,
55 * or the power off state.
58 prepare_to_wait(&genpd->status_wait_queue, &wait,
59 TASK_UNINTERRUPTIBLE);
60 if (genpd->status == GPD_STATE_ACTIVE
61 || genpd->status == GPD_STATE_POWER_OFF)
63 mutex_unlock(&genpd->lock);
67 mutex_lock(&genpd->lock);
69 finish_wait(&genpd->status_wait_queue, &wait);
72 static void genpd_release_lock(struct generic_pm_domain *genpd)
74 mutex_unlock(&genpd->lock);
77 static void genpd_set_active(struct generic_pm_domain *genpd)
79 if (genpd->resume_count == 0)
80 genpd->status = GPD_STATE_ACTIVE;
84 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
85 * @genpd: PM domain to power up.
87 * Restore power to @genpd and all of its masters so that it is possible to
88 * resume a device belonging to it.
90 int __pm_genpd_poweron(struct generic_pm_domain *genpd)
91 __releases(&genpd->lock) __acquires(&genpd->lock)
93 struct gpd_link *link;
97 /* If the domain's master is being waited for, we have to wait too. */
99 prepare_to_wait(&genpd->status_wait_queue, &wait,
100 TASK_UNINTERRUPTIBLE);
101 if (genpd->status != GPD_STATE_WAIT_MASTER)
103 mutex_unlock(&genpd->lock);
107 mutex_lock(&genpd->lock);
109 finish_wait(&genpd->status_wait_queue, &wait);
111 if (genpd->status == GPD_STATE_ACTIVE
112 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
115 if (genpd->status != GPD_STATE_POWER_OFF) {
116 genpd_set_active(genpd);
121 * The list is guaranteed not to change while the loop below is being
122 * executed, unless one of the masters' .power_on() callbacks fiddles
125 list_for_each_entry(link, &genpd->slave_links, slave_node) {
126 genpd_sd_counter_inc(link->master);
127 genpd->status = GPD_STATE_WAIT_MASTER;
129 mutex_unlock(&genpd->lock);
131 ret = pm_genpd_poweron(link->master);
133 mutex_lock(&genpd->lock);
136 * The "wait for parent" status is guaranteed not to change
137 * while the master is powering on.
139 genpd->status = GPD_STATE_POWER_OFF;
140 wake_up_all(&genpd->status_wait_queue);
142 genpd_sd_counter_dec(link->master);
147 if (genpd->power_on) {
148 ret = genpd->power_on(genpd);
153 genpd_set_active(genpd);
158 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
159 genpd_sd_counter_dec(link->master);
165 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
166 * @genpd: PM domain to power up.
168 int pm_genpd_poweron(struct generic_pm_domain *genpd)
172 mutex_lock(&genpd->lock);
173 ret = __pm_genpd_poweron(genpd);
174 mutex_unlock(&genpd->lock);
178 #endif /* CONFIG_PM */
180 #ifdef CONFIG_PM_RUNTIME
183 * __pm_genpd_save_device - Save the pre-suspend state of a device.
184 * @pdd: Domain data of the device to save the state of.
185 * @genpd: PM domain the device belongs to.
187 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
188 struct generic_pm_domain *genpd)
189 __releases(&genpd->lock) __acquires(&genpd->lock)
191 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
192 struct device *dev = pdd->dev;
193 struct device_driver *drv = dev->driver;
196 if (gpd_data->need_restore)
199 mutex_unlock(&genpd->lock);
201 if (drv && drv->pm && drv->pm->runtime_suspend) {
202 if (genpd->start_device)
203 genpd->start_device(dev);
205 ret = drv->pm->runtime_suspend(dev);
207 if (genpd->stop_device)
208 genpd->stop_device(dev);
211 mutex_lock(&genpd->lock);
214 gpd_data->need_restore = true;
220 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
221 * @pdd: Domain data of the device to restore the state of.
222 * @genpd: PM domain the device belongs to.
224 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
225 struct generic_pm_domain *genpd)
226 __releases(&genpd->lock) __acquires(&genpd->lock)
228 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
229 struct device *dev = pdd->dev;
230 struct device_driver *drv = dev->driver;
232 if (!gpd_data->need_restore)
235 mutex_unlock(&genpd->lock);
237 if (drv && drv->pm && drv->pm->runtime_resume) {
238 if (genpd->start_device)
239 genpd->start_device(dev);
241 drv->pm->runtime_resume(dev);
243 if (genpd->stop_device)
244 genpd->stop_device(dev);
247 mutex_lock(&genpd->lock);
249 gpd_data->need_restore = false;
253 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
254 * @genpd: PM domain to check.
256 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
257 * a "power off" operation, which means that a "power on" has occured in the
258 * meantime, or if its resume_count field is different from zero, which means
259 * that one of its devices has been resumed in the meantime.
261 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
263 return genpd->status == GPD_STATE_WAIT_MASTER
264 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
268 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
269 * @genpd: PM domait to power off.
271 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
274 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
276 if (!work_pending(&genpd->power_off_work))
277 queue_work(pm_wq, &genpd->power_off_work);
281 * pm_genpd_poweroff - Remove power from a given PM domain.
282 * @genpd: PM domain to power down.
284 * If all of the @genpd's devices have been suspended and all of its subdomains
285 * have been powered down, run the runtime suspend callbacks provided by all of
286 * the @genpd's devices' drivers and remove power from @genpd.
288 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
289 __releases(&genpd->lock) __acquires(&genpd->lock)
291 struct pm_domain_data *pdd;
292 struct gpd_link *link;
293 unsigned int not_suspended;
298 * Do not try to power off the domain in the following situations:
299 * (1) The domain is already in the "power off" state.
300 * (2) The domain is waiting for its master to power up.
301 * (3) One of the domain's devices is being resumed right now.
302 * (4) System suspend is in progress.
304 if (genpd->status == GPD_STATE_POWER_OFF
305 || genpd->status == GPD_STATE_WAIT_MASTER
306 || genpd->resume_count > 0 || genpd->prepared_count > 0)
309 if (atomic_read(&genpd->sd_count) > 0)
313 list_for_each_entry(pdd, &genpd->dev_list, list_node)
314 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
315 || pdd->dev->power.irq_safe))
318 if (not_suspended > genpd->in_progress)
321 if (genpd->poweroff_task) {
323 * Another instance of pm_genpd_poweroff() is executing
324 * callbacks, so tell it to start over and return.
326 genpd->status = GPD_STATE_REPEAT;
330 if (genpd->gov && genpd->gov->power_down_ok) {
331 if (!genpd->gov->power_down_ok(&genpd->domain))
335 genpd->status = GPD_STATE_BUSY;
336 genpd->poweroff_task = current;
338 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
339 ret = atomic_read(&genpd->sd_count) == 0 ?
340 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
342 if (genpd_abort_poweroff(genpd))
346 genpd_set_active(genpd);
350 if (genpd->status == GPD_STATE_REPEAT) {
351 genpd->poweroff_task = NULL;
356 if (genpd->power_off) {
357 if (atomic_read(&genpd->sd_count) > 0) {
363 * If sd_count > 0 at this point, one of the subdomains hasn't
364 * managed to call pm_genpd_poweron() for the master yet after
365 * incrementing it. In that case pm_genpd_poweron() will wait
366 * for us to drop the lock, so we can call .power_off() and let
367 * the pm_genpd_poweron() restore power for us (this shouldn't
368 * happen very often).
370 ret = genpd->power_off(genpd);
372 genpd_set_active(genpd);
377 genpd->status = GPD_STATE_POWER_OFF;
379 list_for_each_entry(link, &genpd->slave_links, slave_node) {
380 genpd_sd_counter_dec(link->master);
381 genpd_queue_power_off_work(link->master);
385 genpd->poweroff_task = NULL;
386 wake_up_all(&genpd->status_wait_queue);
391 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
392 * @work: Work structure used for scheduling the execution of this function.
394 static void genpd_power_off_work_fn(struct work_struct *work)
396 struct generic_pm_domain *genpd;
398 genpd = container_of(work, struct generic_pm_domain, power_off_work);
400 genpd_acquire_lock(genpd);
401 pm_genpd_poweroff(genpd);
402 genpd_release_lock(genpd);
406 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
407 * @dev: Device to suspend.
409 * Carry out a runtime suspend of a device under the assumption that its
410 * pm_domain field points to the domain member of an object of type
411 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
413 static int pm_genpd_runtime_suspend(struct device *dev)
415 struct generic_pm_domain *genpd;
417 dev_dbg(dev, "%s()\n", __func__);
419 genpd = dev_to_genpd(dev);
423 might_sleep_if(!genpd->dev_irq_safe);
425 if (genpd->stop_device) {
426 int ret = genpd->stop_device(dev);
432 * If power.irq_safe is set, this routine will be run with interrupts
433 * off, so it can't use mutexes.
435 if (dev->power.irq_safe)
438 mutex_lock(&genpd->lock);
439 genpd->in_progress++;
440 pm_genpd_poweroff(genpd);
441 genpd->in_progress--;
442 mutex_unlock(&genpd->lock);
448 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
449 * @dev: Device to resume.
451 * Carry out a runtime resume of a device under the assumption that its
452 * pm_domain field points to the domain member of an object of type
453 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
455 static int pm_genpd_runtime_resume(struct device *dev)
457 struct generic_pm_domain *genpd;
461 dev_dbg(dev, "%s()\n", __func__);
463 genpd = dev_to_genpd(dev);
467 might_sleep_if(!genpd->dev_irq_safe);
469 /* If power.irq_safe, the PM domain is never powered off. */
470 if (dev->power.irq_safe)
473 mutex_lock(&genpd->lock);
474 ret = __pm_genpd_poweron(genpd);
476 mutex_unlock(&genpd->lock);
479 genpd->status = GPD_STATE_BUSY;
480 genpd->resume_count++;
482 prepare_to_wait(&genpd->status_wait_queue, &wait,
483 TASK_UNINTERRUPTIBLE);
485 * If current is the powering off task, we have been called
486 * reentrantly from one of the device callbacks, so we should
489 if (!genpd->poweroff_task || genpd->poweroff_task == current)
491 mutex_unlock(&genpd->lock);
495 mutex_lock(&genpd->lock);
497 finish_wait(&genpd->status_wait_queue, &wait);
498 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
499 genpd->resume_count--;
500 genpd_set_active(genpd);
501 wake_up_all(&genpd->status_wait_queue);
502 mutex_unlock(&genpd->lock);
505 if (genpd->start_device)
506 genpd->start_device(dev);
512 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
514 void pm_genpd_poweroff_unused(void)
516 struct generic_pm_domain *genpd;
518 mutex_lock(&gpd_list_lock);
520 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
521 genpd_queue_power_off_work(genpd);
523 mutex_unlock(&gpd_list_lock);
528 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
530 #define pm_genpd_runtime_suspend NULL
531 #define pm_genpd_runtime_resume NULL
533 #endif /* CONFIG_PM_RUNTIME */
535 #ifdef CONFIG_PM_SLEEP
538 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
539 * @genpd: PM domain to power off, if possible.
541 * Check if the given PM domain can be powered off (during system suspend or
542 * hibernation) and do that if so. Also, in that case propagate to its masters.
544 * This function is only called in "noirq" stages of system power transitions,
545 * so it need not acquire locks (all of the "noirq" callbacks are executed
546 * sequentially, so it is guaranteed that it will never run twice in parallel).
548 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
550 struct gpd_link *link;
552 if (genpd->status == GPD_STATE_POWER_OFF)
555 if (genpd->suspended_count != genpd->device_count
556 || atomic_read(&genpd->sd_count) > 0)
559 if (genpd->power_off)
560 genpd->power_off(genpd);
562 genpd->status = GPD_STATE_POWER_OFF;
564 list_for_each_entry(link, &genpd->slave_links, slave_node) {
565 genpd_sd_counter_dec(link->master);
566 pm_genpd_sync_poweroff(link->master);
571 * resume_needed - Check whether to resume a device before system suspend.
572 * @dev: Device to check.
573 * @genpd: PM domain the device belongs to.
575 * There are two cases in which a device that can wake up the system from sleep
576 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
577 * to wake up the system and it has to remain active for this purpose while the
578 * system is in the sleep state and (2) if the device is not enabled to wake up
579 * the system from sleep states and it generally doesn't generate wakeup signals
580 * by itself (those signals are generated on its behalf by other parts of the
581 * system). In the latter case it may be necessary to reconfigure the device's
582 * wakeup settings during system suspend, because it may have been set up to
583 * signal remote wakeup from the system's working state as needed by runtime PM.
584 * Return 'true' in either of the above cases.
586 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
590 if (!device_can_wakeup(dev))
593 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
594 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
598 * pm_genpd_prepare - Start power transition of a device in a PM domain.
599 * @dev: Device to start the transition of.
601 * Start a power transition of a device (during a system-wide power transition)
602 * under the assumption that its pm_domain field points to the domain member of
603 * an object of type struct generic_pm_domain representing a PM domain
604 * consisting of I/O devices.
606 static int pm_genpd_prepare(struct device *dev)
608 struct generic_pm_domain *genpd;
611 dev_dbg(dev, "%s()\n", __func__);
613 genpd = dev_to_genpd(dev);
618 * If a wakeup request is pending for the device, it should be woken up
619 * at this point and a system wakeup event should be reported if it's
620 * set up to wake up the system from sleep states.
622 pm_runtime_get_noresume(dev);
623 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
624 pm_wakeup_event(dev, 0);
626 if (pm_wakeup_pending()) {
627 pm_runtime_put_sync(dev);
631 if (resume_needed(dev, genpd))
632 pm_runtime_resume(dev);
634 genpd_acquire_lock(genpd);
636 if (genpd->prepared_count++ == 0)
637 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
639 genpd_release_lock(genpd);
641 if (genpd->suspend_power_off) {
642 pm_runtime_put_noidle(dev);
647 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
648 * so pm_genpd_poweron() will return immediately, but if the device
649 * is suspended (e.g. it's been stopped by .stop_device()), we need
650 * to make it operational.
652 pm_runtime_resume(dev);
653 __pm_runtime_disable(dev, false);
655 ret = pm_generic_prepare(dev);
657 mutex_lock(&genpd->lock);
659 if (--genpd->prepared_count == 0)
660 genpd->suspend_power_off = false;
662 mutex_unlock(&genpd->lock);
663 pm_runtime_enable(dev);
666 pm_runtime_put_sync(dev);
671 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
672 * @dev: Device to suspend.
674 * Suspend a device under the assumption that its pm_domain field points to the
675 * domain member of an object of type struct generic_pm_domain representing
676 * a PM domain consisting of I/O devices.
678 static int pm_genpd_suspend(struct device *dev)
680 struct generic_pm_domain *genpd;
682 dev_dbg(dev, "%s()\n", __func__);
684 genpd = dev_to_genpd(dev);
688 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
692 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
693 * @dev: Device to suspend.
695 * Carry out a late suspend of a device under the assumption that its
696 * pm_domain field points to the domain member of an object of type
697 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
699 static int pm_genpd_suspend_noirq(struct device *dev)
701 struct generic_pm_domain *genpd;
704 dev_dbg(dev, "%s()\n", __func__);
706 genpd = dev_to_genpd(dev);
710 if (genpd->suspend_power_off)
713 ret = pm_generic_suspend_noirq(dev);
717 if (dev->power.wakeup_path
718 && genpd->active_wakeup && genpd->active_wakeup(dev))
721 if (genpd->stop_device)
722 genpd->stop_device(dev);
725 * Since all of the "noirq" callbacks are executed sequentially, it is
726 * guaranteed that this function will never run twice in parallel for
727 * the same PM domain, so it is not necessary to use locking here.
729 genpd->suspended_count++;
730 pm_genpd_sync_poweroff(genpd);
736 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
737 * @dev: Device to resume.
739 * Carry out an early resume of a device under the assumption that its
740 * pm_domain field points to the domain member of an object of type
741 * struct generic_pm_domain representing a power domain consisting of I/O
744 static int pm_genpd_resume_noirq(struct device *dev)
746 struct generic_pm_domain *genpd;
748 dev_dbg(dev, "%s()\n", __func__);
750 genpd = dev_to_genpd(dev);
754 if (genpd->suspend_power_off
755 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
759 * Since all of the "noirq" callbacks are executed sequentially, it is
760 * guaranteed that this function will never run twice in parallel for
761 * the same PM domain, so it is not necessary to use locking here.
763 pm_genpd_poweron(genpd);
764 genpd->suspended_count--;
765 if (genpd->start_device)
766 genpd->start_device(dev);
768 return pm_generic_resume_noirq(dev);
772 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
773 * @dev: Device to resume.
775 * Resume a device under the assumption that its pm_domain field points to the
776 * domain member of an object of type struct generic_pm_domain representing
777 * a power domain consisting of I/O devices.
779 static int pm_genpd_resume(struct device *dev)
781 struct generic_pm_domain *genpd;
783 dev_dbg(dev, "%s()\n", __func__);
785 genpd = dev_to_genpd(dev);
789 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
793 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
794 * @dev: Device to freeze.
796 * Freeze a device under the assumption that its pm_domain field points to the
797 * domain member of an object of type struct generic_pm_domain representing
798 * a power domain consisting of I/O devices.
800 static int pm_genpd_freeze(struct device *dev)
802 struct generic_pm_domain *genpd;
804 dev_dbg(dev, "%s()\n", __func__);
806 genpd = dev_to_genpd(dev);
810 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
814 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
815 * @dev: Device to freeze.
817 * Carry out a late freeze of a device under the assumption that its
818 * pm_domain field points to the domain member of an object of type
819 * struct generic_pm_domain representing a power domain consisting of I/O
822 static int pm_genpd_freeze_noirq(struct device *dev)
824 struct generic_pm_domain *genpd;
827 dev_dbg(dev, "%s()\n", __func__);
829 genpd = dev_to_genpd(dev);
833 if (genpd->suspend_power_off)
836 ret = pm_generic_freeze_noirq(dev);
840 if (genpd->stop_device)
841 genpd->stop_device(dev);
847 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
848 * @dev: Device to thaw.
850 * Carry out an early thaw of a device under the assumption that its
851 * pm_domain field points to the domain member of an object of type
852 * struct generic_pm_domain representing a power domain consisting of I/O
855 static int pm_genpd_thaw_noirq(struct device *dev)
857 struct generic_pm_domain *genpd;
859 dev_dbg(dev, "%s()\n", __func__);
861 genpd = dev_to_genpd(dev);
865 if (genpd->suspend_power_off)
868 if (genpd->start_device)
869 genpd->start_device(dev);
871 return pm_generic_thaw_noirq(dev);
875 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
876 * @dev: Device to thaw.
878 * Thaw a device under the assumption that its pm_domain field points to the
879 * domain member of an object of type struct generic_pm_domain representing
880 * a power domain consisting of I/O devices.
882 static int pm_genpd_thaw(struct device *dev)
884 struct generic_pm_domain *genpd;
886 dev_dbg(dev, "%s()\n", __func__);
888 genpd = dev_to_genpd(dev);
892 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
896 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
897 * @dev: Device to suspend.
899 * Power off a device under the assumption that its pm_domain field points to
900 * the domain member of an object of type struct generic_pm_domain representing
901 * a PM domain consisting of I/O devices.
903 static int pm_genpd_dev_poweroff(struct device *dev)
905 struct generic_pm_domain *genpd;
907 dev_dbg(dev, "%s()\n", __func__);
909 genpd = dev_to_genpd(dev);
913 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
917 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
918 * @dev: Device to suspend.
920 * Carry out a late powering off of a device under the assumption that its
921 * pm_domain field points to the domain member of an object of type
922 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
924 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
926 struct generic_pm_domain *genpd;
929 dev_dbg(dev, "%s()\n", __func__);
931 genpd = dev_to_genpd(dev);
935 if (genpd->suspend_power_off)
938 ret = pm_generic_poweroff_noirq(dev);
942 if (dev->power.wakeup_path
943 && genpd->active_wakeup && genpd->active_wakeup(dev))
946 if (genpd->stop_device)
947 genpd->stop_device(dev);
950 * Since all of the "noirq" callbacks are executed sequentially, it is
951 * guaranteed that this function will never run twice in parallel for
952 * the same PM domain, so it is not necessary to use locking here.
954 genpd->suspended_count++;
955 pm_genpd_sync_poweroff(genpd);
961 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
962 * @dev: Device to resume.
964 * Carry out an early restore of a device under the assumption that its
965 * pm_domain field points to the domain member of an object of type
966 * struct generic_pm_domain representing a power domain consisting of I/O
969 static int pm_genpd_restore_noirq(struct device *dev)
971 struct generic_pm_domain *genpd;
973 dev_dbg(dev, "%s()\n", __func__);
975 genpd = dev_to_genpd(dev);
980 * Since all of the "noirq" callbacks are executed sequentially, it is
981 * guaranteed that this function will never run twice in parallel for
982 * the same PM domain, so it is not necessary to use locking here.
984 genpd->status = GPD_STATE_POWER_OFF;
985 if (genpd->suspend_power_off) {
987 * The boot kernel might put the domain into the power on state,
988 * so make sure it really is powered off.
990 if (genpd->power_off)
991 genpd->power_off(genpd);
995 pm_genpd_poweron(genpd);
996 genpd->suspended_count--;
997 if (genpd->start_device)
998 genpd->start_device(dev);
1000 return pm_generic_restore_noirq(dev);
1004 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
1005 * @dev: Device to resume.
1007 * Restore a device under the assumption that its pm_domain field points to the
1008 * domain member of an object of type struct generic_pm_domain representing
1009 * a power domain consisting of I/O devices.
1011 static int pm_genpd_restore(struct device *dev)
1013 struct generic_pm_domain *genpd;
1015 dev_dbg(dev, "%s()\n", __func__);
1017 genpd = dev_to_genpd(dev);
1021 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
1025 * pm_genpd_complete - Complete power transition of a device in a power domain.
1026 * @dev: Device to complete the transition of.
1028 * Complete a power transition of a device (during a system-wide power
1029 * transition) under the assumption that its pm_domain field points to the
1030 * domain member of an object of type struct generic_pm_domain representing
1031 * a power domain consisting of I/O devices.
1033 static void pm_genpd_complete(struct device *dev)
1035 struct generic_pm_domain *genpd;
1038 dev_dbg(dev, "%s()\n", __func__);
1040 genpd = dev_to_genpd(dev);
1044 mutex_lock(&genpd->lock);
1046 run_complete = !genpd->suspend_power_off;
1047 if (--genpd->prepared_count == 0)
1048 genpd->suspend_power_off = false;
1050 mutex_unlock(&genpd->lock);
1053 pm_generic_complete(dev);
1054 pm_runtime_set_active(dev);
1055 pm_runtime_enable(dev);
1056 pm_runtime_idle(dev);
1062 #define pm_genpd_prepare NULL
1063 #define pm_genpd_suspend NULL
1064 #define pm_genpd_suspend_noirq NULL
1065 #define pm_genpd_resume_noirq NULL
1066 #define pm_genpd_resume NULL
1067 #define pm_genpd_freeze NULL
1068 #define pm_genpd_freeze_noirq NULL
1069 #define pm_genpd_thaw_noirq NULL
1070 #define pm_genpd_thaw NULL
1071 #define pm_genpd_dev_poweroff_noirq NULL
1072 #define pm_genpd_dev_poweroff NULL
1073 #define pm_genpd_restore_noirq NULL
1074 #define pm_genpd_restore NULL
1075 #define pm_genpd_complete NULL
1077 #endif /* CONFIG_PM_SLEEP */
1080 * pm_genpd_add_device - Add a device to an I/O PM domain.
1081 * @genpd: PM domain to add the device to.
1082 * @dev: Device to be added.
1084 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1086 struct generic_pm_domain_data *gpd_data;
1087 struct pm_domain_data *pdd;
1090 dev_dbg(dev, "%s()\n", __func__);
1092 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1095 genpd_acquire_lock(genpd);
1097 if (genpd->status == GPD_STATE_POWER_OFF) {
1102 if (genpd->prepared_count > 0) {
1107 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1108 if (pdd->dev == dev) {
1113 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1119 genpd->device_count++;
1121 dev->pm_domain = &genpd->domain;
1122 dev_pm_get_subsys_data(dev);
1123 dev->power.subsys_data->domain_data = &gpd_data->base;
1124 gpd_data->base.dev = dev;
1125 gpd_data->need_restore = false;
1126 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1129 genpd_release_lock(genpd);
1135 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1136 * @genpd: PM domain to remove the device from.
1137 * @dev: Device to be removed.
1139 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1142 struct pm_domain_data *pdd;
1145 dev_dbg(dev, "%s()\n", __func__);
1147 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1150 genpd_acquire_lock(genpd);
1152 if (genpd->prepared_count > 0) {
1157 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1158 if (pdd->dev != dev)
1161 list_del_init(&pdd->list_node);
1163 dev_pm_put_subsys_data(dev);
1164 dev->pm_domain = NULL;
1165 kfree(to_gpd_data(pdd));
1167 genpd->device_count--;
1174 genpd_release_lock(genpd);
1180 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1181 * @genpd: Master PM domain to add the subdomain to.
1182 * @subdomain: Subdomain to be added.
1184 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1185 struct generic_pm_domain *subdomain)
1187 struct gpd_link *link;
1190 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1194 genpd_acquire_lock(genpd);
1195 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1197 if (subdomain->status != GPD_STATE_POWER_OFF
1198 && subdomain->status != GPD_STATE_ACTIVE) {
1199 mutex_unlock(&subdomain->lock);
1200 genpd_release_lock(genpd);
1204 if (genpd->status == GPD_STATE_POWER_OFF
1205 && subdomain->status != GPD_STATE_POWER_OFF) {
1210 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1211 if (link->slave == subdomain && link->master == genpd) {
1217 link = kzalloc(sizeof(*link), GFP_KERNEL);
1222 link->master = genpd;
1223 list_add_tail(&link->master_node, &genpd->master_links);
1224 link->slave = subdomain;
1225 list_add_tail(&link->slave_node, &subdomain->slave_links);
1226 if (subdomain->status != GPD_STATE_POWER_OFF)
1227 genpd_sd_counter_inc(genpd);
1230 mutex_unlock(&subdomain->lock);
1231 genpd_release_lock(genpd);
1237 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1238 * @genpd: Master PM domain to remove the subdomain from.
1239 * @subdomain: Subdomain to be removed.
1241 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1242 struct generic_pm_domain *subdomain)
1244 struct gpd_link *link;
1247 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1251 genpd_acquire_lock(genpd);
1253 list_for_each_entry(link, &genpd->master_links, master_node) {
1254 if (link->slave != subdomain)
1257 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1259 if (subdomain->status != GPD_STATE_POWER_OFF
1260 && subdomain->status != GPD_STATE_ACTIVE) {
1261 mutex_unlock(&subdomain->lock);
1262 genpd_release_lock(genpd);
1266 list_del(&link->master_node);
1267 list_del(&link->slave_node);
1269 if (subdomain->status != GPD_STATE_POWER_OFF)
1270 genpd_sd_counter_dec(genpd);
1272 mutex_unlock(&subdomain->lock);
1278 genpd_release_lock(genpd);
1284 * pm_genpd_init - Initialize a generic I/O PM domain object.
1285 * @genpd: PM domain object to initialize.
1286 * @gov: PM domain governor to associate with the domain (may be NULL).
1287 * @is_off: Initial value of the domain's power_is_off field.
1289 void pm_genpd_init(struct generic_pm_domain *genpd,
1290 struct dev_power_governor *gov, bool is_off)
1292 if (IS_ERR_OR_NULL(genpd))
1295 INIT_LIST_HEAD(&genpd->master_links);
1296 INIT_LIST_HEAD(&genpd->slave_links);
1297 INIT_LIST_HEAD(&genpd->dev_list);
1298 mutex_init(&genpd->lock);
1300 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1301 genpd->in_progress = 0;
1302 atomic_set(&genpd->sd_count, 0);
1303 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1304 init_waitqueue_head(&genpd->status_wait_queue);
1305 genpd->poweroff_task = NULL;
1306 genpd->resume_count = 0;
1307 genpd->device_count = 0;
1308 genpd->suspended_count = 0;
1309 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1310 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1311 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1312 genpd->domain.ops.prepare = pm_genpd_prepare;
1313 genpd->domain.ops.suspend = pm_genpd_suspend;
1314 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1315 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1316 genpd->domain.ops.resume = pm_genpd_resume;
1317 genpd->domain.ops.freeze = pm_genpd_freeze;
1318 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1319 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1320 genpd->domain.ops.thaw = pm_genpd_thaw;
1321 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1322 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1323 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1324 genpd->domain.ops.restore = pm_genpd_restore;
1325 genpd->domain.ops.complete = pm_genpd_complete;
1326 mutex_lock(&gpd_list_lock);
1327 list_add(&genpd->gpd_list_node, &gpd_list);
1328 mutex_unlock(&gpd_list_lock);