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/kernel.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/pm_domain.h>
13 #include <linux/pm_qos.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
18 #include <linux/export.h>
20 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
22 type (*__routine)(struct device *__d); \
23 type __ret = (type)0; \
25 __routine = genpd->dev_ops.callback; \
27 __ret = __routine(dev); \
32 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
34 ktime_t __start = ktime_get(); \
35 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
36 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
37 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
38 if (!__retval && __elapsed > __td->field) { \
39 __td->field = __elapsed; \
40 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
42 genpd->max_off_time_changed = true; \
43 __td->constraint_changed = true; \
48 static LIST_HEAD(gpd_list);
49 static DEFINE_MUTEX(gpd_list_lock);
51 static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
53 struct generic_pm_domain *genpd = NULL, *gpd;
55 if (IS_ERR_OR_NULL(domain_name))
58 mutex_lock(&gpd_list_lock);
59 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
60 if (!strcmp(gpd->name, domain_name)) {
65 mutex_unlock(&gpd_list_lock);
69 struct generic_pm_domain *dev_to_genpd(struct device *dev)
71 if (IS_ERR_OR_NULL(dev->pm_domain))
72 return ERR_PTR(-EINVAL);
74 return pd_to_genpd(dev->pm_domain);
77 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
79 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
80 stop_latency_ns, "stop");
83 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
85 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
86 start_latency_ns, "start");
89 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
99 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
101 atomic_inc(&genpd->sd_count);
102 smp_mb__after_atomic();
105 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
109 mutex_lock(&genpd->lock);
111 * Wait for the domain to transition into either the active,
112 * or the power off state.
115 prepare_to_wait(&genpd->status_wait_queue, &wait,
116 TASK_UNINTERRUPTIBLE);
117 if (genpd->status == GPD_STATE_ACTIVE
118 || genpd->status == GPD_STATE_POWER_OFF)
120 mutex_unlock(&genpd->lock);
124 mutex_lock(&genpd->lock);
126 finish_wait(&genpd->status_wait_queue, &wait);
129 static void genpd_release_lock(struct generic_pm_domain *genpd)
131 mutex_unlock(&genpd->lock);
134 static void genpd_set_active(struct generic_pm_domain *genpd)
136 if (genpd->resume_count == 0)
137 genpd->status = GPD_STATE_ACTIVE;
140 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
144 if (!genpd->cpu_data)
147 usecs64 = genpd->power_on_latency_ns;
148 do_div(usecs64, NSEC_PER_USEC);
149 usecs64 += genpd->cpu_data->saved_exit_latency;
150 genpd->cpu_data->idle_state->exit_latency = usecs64;
154 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
155 * @genpd: PM domain to power up.
157 * Restore power to @genpd and all of its masters so that it is possible to
158 * resume a device belonging to it.
160 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
161 __releases(&genpd->lock) __acquires(&genpd->lock)
163 struct gpd_link *link;
167 /* If the domain's master is being waited for, we have to wait too. */
169 prepare_to_wait(&genpd->status_wait_queue, &wait,
170 TASK_UNINTERRUPTIBLE);
171 if (genpd->status != GPD_STATE_WAIT_MASTER)
173 mutex_unlock(&genpd->lock);
177 mutex_lock(&genpd->lock);
179 finish_wait(&genpd->status_wait_queue, &wait);
181 if (genpd->status == GPD_STATE_ACTIVE
182 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
185 if (genpd->status != GPD_STATE_POWER_OFF) {
186 genpd_set_active(genpd);
190 if (genpd->cpu_data) {
191 cpuidle_pause_and_lock();
192 genpd->cpu_data->idle_state->disabled = true;
193 cpuidle_resume_and_unlock();
198 * The list is guaranteed not to change while the loop below is being
199 * executed, unless one of the masters' .power_on() callbacks fiddles
202 list_for_each_entry(link, &genpd->slave_links, slave_node) {
203 genpd_sd_counter_inc(link->master);
204 genpd->status = GPD_STATE_WAIT_MASTER;
206 mutex_unlock(&genpd->lock);
208 ret = pm_genpd_poweron(link->master);
210 mutex_lock(&genpd->lock);
213 * The "wait for parent" status is guaranteed not to change
214 * while the master is powering on.
216 genpd->status = GPD_STATE_POWER_OFF;
217 wake_up_all(&genpd->status_wait_queue);
219 genpd_sd_counter_dec(link->master);
224 if (genpd->power_on) {
225 ktime_t time_start = ktime_get();
228 ret = genpd->power_on(genpd);
232 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
233 if (elapsed_ns > genpd->power_on_latency_ns) {
234 genpd->power_on_latency_ns = elapsed_ns;
235 genpd->max_off_time_changed = true;
236 genpd_recalc_cpu_exit_latency(genpd);
238 pr_warning("%s: Power-on latency exceeded, "
239 "new value %lld ns\n", genpd->name,
245 genpd_set_active(genpd);
250 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
251 genpd_sd_counter_dec(link->master);
257 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
258 * @genpd: PM domain to power up.
260 int pm_genpd_poweron(struct generic_pm_domain *genpd)
264 mutex_lock(&genpd->lock);
265 ret = __pm_genpd_poweron(genpd);
266 mutex_unlock(&genpd->lock);
271 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
272 * @domain_name: Name of the PM domain to power up.
274 int pm_genpd_name_poweron(const char *domain_name)
276 struct generic_pm_domain *genpd;
278 genpd = pm_genpd_lookup_name(domain_name);
279 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
282 #ifdef CONFIG_PM_RUNTIME
284 static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
287 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
290 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
292 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
293 save_state_latency_ns, "state save");
296 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
298 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
299 restore_state_latency_ns,
303 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
304 unsigned long val, void *ptr)
306 struct generic_pm_domain_data *gpd_data;
309 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
311 mutex_lock(&gpd_data->lock);
312 dev = gpd_data->base.dev;
314 mutex_unlock(&gpd_data->lock);
317 mutex_unlock(&gpd_data->lock);
320 struct generic_pm_domain *genpd;
321 struct pm_domain_data *pdd;
323 spin_lock_irq(&dev->power.lock);
325 pdd = dev->power.subsys_data ?
326 dev->power.subsys_data->domain_data : NULL;
327 if (pdd && pdd->dev) {
328 to_gpd_data(pdd)->td.constraint_changed = true;
329 genpd = dev_to_genpd(dev);
331 genpd = ERR_PTR(-ENODATA);
334 spin_unlock_irq(&dev->power.lock);
336 if (!IS_ERR(genpd)) {
337 mutex_lock(&genpd->lock);
338 genpd->max_off_time_changed = true;
339 mutex_unlock(&genpd->lock);
343 if (!dev || dev->power.ignore_children)
351 * __pm_genpd_save_device - Save the pre-suspend state of a device.
352 * @pdd: Domain data of the device to save the state of.
353 * @genpd: PM domain the device belongs to.
355 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
356 struct generic_pm_domain *genpd)
357 __releases(&genpd->lock) __acquires(&genpd->lock)
359 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
360 struct device *dev = pdd->dev;
363 if (gpd_data->need_restore)
366 mutex_unlock(&genpd->lock);
368 genpd_start_dev(genpd, dev);
369 ret = genpd_save_dev(genpd, dev);
370 genpd_stop_dev(genpd, dev);
372 mutex_lock(&genpd->lock);
375 gpd_data->need_restore = true;
381 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
382 * @pdd: Domain data of the device to restore the state of.
383 * @genpd: PM domain the device belongs to.
385 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
386 struct generic_pm_domain *genpd)
387 __releases(&genpd->lock) __acquires(&genpd->lock)
389 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
390 struct device *dev = pdd->dev;
391 bool need_restore = gpd_data->need_restore;
393 gpd_data->need_restore = false;
394 mutex_unlock(&genpd->lock);
396 genpd_start_dev(genpd, dev);
398 genpd_restore_dev(genpd, dev);
400 mutex_lock(&genpd->lock);
404 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
405 * @genpd: PM domain to check.
407 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
408 * a "power off" operation, which means that a "power on" has occured in the
409 * meantime, or if its resume_count field is different from zero, which means
410 * that one of its devices has been resumed in the meantime.
412 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
414 return genpd->status == GPD_STATE_WAIT_MASTER
415 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
419 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
420 * @genpd: PM domait to power off.
422 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
425 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
427 queue_work(pm_wq, &genpd->power_off_work);
431 * pm_genpd_poweroff - Remove power from a given PM domain.
432 * @genpd: PM domain to power down.
434 * If all of the @genpd's devices have been suspended and all of its subdomains
435 * have been powered down, run the runtime suspend callbacks provided by all of
436 * the @genpd's devices' drivers and remove power from @genpd.
438 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
439 __releases(&genpd->lock) __acquires(&genpd->lock)
441 struct pm_domain_data *pdd;
442 struct gpd_link *link;
443 unsigned int not_suspended;
448 * Do not try to power off the domain in the following situations:
449 * (1) The domain is already in the "power off" state.
450 * (2) The domain is waiting for its master to power up.
451 * (3) One of the domain's devices is being resumed right now.
452 * (4) System suspend is in progress.
454 if (genpd->status == GPD_STATE_POWER_OFF
455 || genpd->status == GPD_STATE_WAIT_MASTER
456 || genpd->resume_count > 0 || genpd->prepared_count > 0)
459 if (atomic_read(&genpd->sd_count) > 0)
463 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
464 enum pm_qos_flags_status stat;
466 stat = dev_pm_qos_flags(pdd->dev,
467 PM_QOS_FLAG_NO_POWER_OFF
468 | PM_QOS_FLAG_REMOTE_WAKEUP);
469 if (stat > PM_QOS_FLAGS_NONE)
472 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
473 || pdd->dev->power.irq_safe))
477 if (not_suspended > genpd->in_progress)
480 if (genpd->poweroff_task) {
482 * Another instance of pm_genpd_poweroff() is executing
483 * callbacks, so tell it to start over and return.
485 genpd->status = GPD_STATE_REPEAT;
489 if (genpd->gov && genpd->gov->power_down_ok) {
490 if (!genpd->gov->power_down_ok(&genpd->domain))
494 genpd->status = GPD_STATE_BUSY;
495 genpd->poweroff_task = current;
497 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
498 ret = atomic_read(&genpd->sd_count) == 0 ?
499 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
501 if (genpd_abort_poweroff(genpd))
505 genpd_set_active(genpd);
509 if (genpd->status == GPD_STATE_REPEAT) {
510 genpd->poweroff_task = NULL;
515 if (genpd->cpu_data) {
517 * If cpu_data is set, cpuidle should turn the domain off when
518 * the CPU in it is idle. In that case we don't decrement the
519 * subdomain counts of the master domains, so that power is not
520 * removed from the current domain prematurely as a result of
521 * cutting off the masters' power.
523 genpd->status = GPD_STATE_POWER_OFF;
524 cpuidle_pause_and_lock();
525 genpd->cpu_data->idle_state->disabled = false;
526 cpuidle_resume_and_unlock();
530 if (genpd->power_off) {
534 if (atomic_read(&genpd->sd_count) > 0) {
539 time_start = ktime_get();
542 * If sd_count > 0 at this point, one of the subdomains hasn't
543 * managed to call pm_genpd_poweron() for the master yet after
544 * incrementing it. In that case pm_genpd_poweron() will wait
545 * for us to drop the lock, so we can call .power_off() and let
546 * the pm_genpd_poweron() restore power for us (this shouldn't
547 * happen very often).
549 ret = genpd->power_off(genpd);
551 genpd_set_active(genpd);
555 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
556 if (elapsed_ns > genpd->power_off_latency_ns) {
557 genpd->power_off_latency_ns = elapsed_ns;
558 genpd->max_off_time_changed = true;
560 pr_warning("%s: Power-off latency exceeded, "
561 "new value %lld ns\n", genpd->name,
566 genpd->status = GPD_STATE_POWER_OFF;
568 list_for_each_entry(link, &genpd->slave_links, slave_node) {
569 genpd_sd_counter_dec(link->master);
570 genpd_queue_power_off_work(link->master);
574 genpd->poweroff_task = NULL;
575 wake_up_all(&genpd->status_wait_queue);
580 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
581 * @work: Work structure used for scheduling the execution of this function.
583 static void genpd_power_off_work_fn(struct work_struct *work)
585 struct generic_pm_domain *genpd;
587 genpd = container_of(work, struct generic_pm_domain, power_off_work);
589 genpd_acquire_lock(genpd);
590 pm_genpd_poweroff(genpd);
591 genpd_release_lock(genpd);
595 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
596 * @dev: Device to suspend.
598 * Carry out a runtime suspend of a device under the assumption that its
599 * pm_domain field points to the domain member of an object of type
600 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
602 static int pm_genpd_runtime_suspend(struct device *dev)
604 struct generic_pm_domain *genpd;
605 bool (*stop_ok)(struct device *__dev);
608 dev_dbg(dev, "%s()\n", __func__);
610 genpd = dev_to_genpd(dev);
614 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
615 if (stop_ok && !stop_ok(dev))
618 ret = genpd_stop_dev(genpd, dev);
623 * If power.irq_safe is set, this routine will be run with interrupts
624 * off, so it can't use mutexes.
626 if (dev->power.irq_safe)
629 mutex_lock(&genpd->lock);
630 genpd->in_progress++;
631 pm_genpd_poweroff(genpd);
632 genpd->in_progress--;
633 mutex_unlock(&genpd->lock);
639 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
640 * @dev: Device to resume.
642 * Carry out a runtime resume of a device under the assumption that its
643 * pm_domain field points to the domain member of an object of type
644 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
646 static int pm_genpd_runtime_resume(struct device *dev)
648 struct generic_pm_domain *genpd;
652 dev_dbg(dev, "%s()\n", __func__);
654 genpd = dev_to_genpd(dev);
658 /* If power.irq_safe, the PM domain is never powered off. */
659 if (dev->power.irq_safe)
660 return genpd_start_dev_no_timing(genpd, dev);
662 mutex_lock(&genpd->lock);
663 ret = __pm_genpd_poweron(genpd);
665 mutex_unlock(&genpd->lock);
668 genpd->status = GPD_STATE_BUSY;
669 genpd->resume_count++;
671 prepare_to_wait(&genpd->status_wait_queue, &wait,
672 TASK_UNINTERRUPTIBLE);
674 * If current is the powering off task, we have been called
675 * reentrantly from one of the device callbacks, so we should
678 if (!genpd->poweroff_task || genpd->poweroff_task == current)
680 mutex_unlock(&genpd->lock);
684 mutex_lock(&genpd->lock);
686 finish_wait(&genpd->status_wait_queue, &wait);
687 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
688 genpd->resume_count--;
689 genpd_set_active(genpd);
690 wake_up_all(&genpd->status_wait_queue);
691 mutex_unlock(&genpd->lock);
696 static bool pd_ignore_unused;
697 static int __init pd_ignore_unused_setup(char *__unused)
699 pd_ignore_unused = true;
702 __setup("pd_ignore_unused", pd_ignore_unused_setup);
705 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
707 void pm_genpd_poweroff_unused(void)
709 struct generic_pm_domain *genpd;
711 if (pd_ignore_unused) {
712 pr_warn("genpd: Not disabling unused power domains\n");
716 mutex_lock(&gpd_list_lock);
718 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
719 genpd_queue_power_off_work(genpd);
721 mutex_unlock(&gpd_list_lock);
724 static int __init genpd_poweroff_unused(void)
726 pm_genpd_poweroff_unused();
729 late_initcall(genpd_poweroff_unused);
733 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
734 unsigned long val, void *ptr)
740 genpd_queue_power_off_work(struct generic_pm_domain *genpd) {}
742 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
744 #define pm_genpd_runtime_suspend NULL
745 #define pm_genpd_runtime_resume NULL
747 #endif /* CONFIG_PM_RUNTIME */
749 #ifdef CONFIG_PM_SLEEP
752 * pm_genpd_present - Check if the given PM domain has been initialized.
753 * @genpd: PM domain to check.
755 static bool pm_genpd_present(struct generic_pm_domain *genpd)
757 struct generic_pm_domain *gpd;
759 if (IS_ERR_OR_NULL(genpd))
762 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
769 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
772 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
776 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
777 * @genpd: PM domain to power off, if possible.
779 * Check if the given PM domain can be powered off (during system suspend or
780 * hibernation) and do that if so. Also, in that case propagate to its masters.
782 * This function is only called in "noirq" and "syscore" stages of system power
783 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
784 * executed sequentially, so it is guaranteed that it will never run twice in
787 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
789 struct gpd_link *link;
791 if (genpd->status == GPD_STATE_POWER_OFF)
794 if (genpd->suspended_count != genpd->device_count
795 || atomic_read(&genpd->sd_count) > 0)
798 if (genpd->power_off)
799 genpd->power_off(genpd);
801 genpd->status = GPD_STATE_POWER_OFF;
803 list_for_each_entry(link, &genpd->slave_links, slave_node) {
804 genpd_sd_counter_dec(link->master);
805 pm_genpd_sync_poweroff(link->master);
810 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
811 * @genpd: PM domain to power on.
813 * This function is only called in "noirq" and "syscore" stages of system power
814 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
815 * executed sequentially, so it is guaranteed that it will never run twice in
818 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
820 struct gpd_link *link;
822 if (genpd->status != GPD_STATE_POWER_OFF)
825 list_for_each_entry(link, &genpd->slave_links, slave_node) {
826 pm_genpd_sync_poweron(link->master);
827 genpd_sd_counter_inc(link->master);
831 genpd->power_on(genpd);
833 genpd->status = GPD_STATE_ACTIVE;
837 * resume_needed - Check whether to resume a device before system suspend.
838 * @dev: Device to check.
839 * @genpd: PM domain the device belongs to.
841 * There are two cases in which a device that can wake up the system from sleep
842 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
843 * to wake up the system and it has to remain active for this purpose while the
844 * system is in the sleep state and (2) if the device is not enabled to wake up
845 * the system from sleep states and it generally doesn't generate wakeup signals
846 * by itself (those signals are generated on its behalf by other parts of the
847 * system). In the latter case it may be necessary to reconfigure the device's
848 * wakeup settings during system suspend, because it may have been set up to
849 * signal remote wakeup from the system's working state as needed by runtime PM.
850 * Return 'true' in either of the above cases.
852 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
856 if (!device_can_wakeup(dev))
859 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
860 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
864 * pm_genpd_prepare - Start power transition of a device in a PM domain.
865 * @dev: Device to start the transition of.
867 * Start a power transition of a device (during a system-wide power transition)
868 * under the assumption that its pm_domain field points to the domain member of
869 * an object of type struct generic_pm_domain representing a PM domain
870 * consisting of I/O devices.
872 static int pm_genpd_prepare(struct device *dev)
874 struct generic_pm_domain *genpd;
877 dev_dbg(dev, "%s()\n", __func__);
879 genpd = dev_to_genpd(dev);
884 * If a wakeup request is pending for the device, it should be woken up
885 * at this point and a system wakeup event should be reported if it's
886 * set up to wake up the system from sleep states.
888 pm_runtime_get_noresume(dev);
889 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
890 pm_wakeup_event(dev, 0);
892 if (pm_wakeup_pending()) {
897 if (resume_needed(dev, genpd))
898 pm_runtime_resume(dev);
900 genpd_acquire_lock(genpd);
902 if (genpd->prepared_count++ == 0) {
903 genpd->suspended_count = 0;
904 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
907 genpd_release_lock(genpd);
909 if (genpd->suspend_power_off) {
910 pm_runtime_put_noidle(dev);
915 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
916 * so pm_genpd_poweron() will return immediately, but if the device
917 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
918 * to make it operational.
920 pm_runtime_resume(dev);
921 __pm_runtime_disable(dev, false);
923 ret = pm_generic_prepare(dev);
925 mutex_lock(&genpd->lock);
927 if (--genpd->prepared_count == 0)
928 genpd->suspend_power_off = false;
930 mutex_unlock(&genpd->lock);
931 pm_runtime_enable(dev);
939 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
940 * @dev: Device to suspend.
942 * Suspend a device under the assumption that its pm_domain field points to the
943 * domain member of an object of type struct generic_pm_domain representing
944 * a PM domain consisting of I/O devices.
946 static int pm_genpd_suspend(struct device *dev)
948 struct generic_pm_domain *genpd;
950 dev_dbg(dev, "%s()\n", __func__);
952 genpd = dev_to_genpd(dev);
956 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
960 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
961 * @dev: Device to suspend.
963 * Carry out a late suspend of a device under the assumption that its
964 * pm_domain field points to the domain member of an object of type
965 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
967 static int pm_genpd_suspend_late(struct device *dev)
969 struct generic_pm_domain *genpd;
971 dev_dbg(dev, "%s()\n", __func__);
973 genpd = dev_to_genpd(dev);
977 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
981 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
982 * @dev: Device to suspend.
984 * Stop the device and remove power from the domain if all devices in it have
987 static int pm_genpd_suspend_noirq(struct device *dev)
989 struct generic_pm_domain *genpd;
991 dev_dbg(dev, "%s()\n", __func__);
993 genpd = dev_to_genpd(dev);
997 if (genpd->suspend_power_off
998 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1001 genpd_stop_dev(genpd, dev);
1004 * Since all of the "noirq" callbacks are executed sequentially, it is
1005 * guaranteed that this function will never run twice in parallel for
1006 * the same PM domain, so it is not necessary to use locking here.
1008 genpd->suspended_count++;
1009 pm_genpd_sync_poweroff(genpd);
1015 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1016 * @dev: Device to resume.
1018 * Restore power to the device's PM domain, if necessary, and start the device.
1020 static int pm_genpd_resume_noirq(struct device *dev)
1022 struct generic_pm_domain *genpd;
1024 dev_dbg(dev, "%s()\n", __func__);
1026 genpd = dev_to_genpd(dev);
1030 if (genpd->suspend_power_off
1031 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1035 * Since all of the "noirq" callbacks are executed sequentially, it is
1036 * guaranteed that this function will never run twice in parallel for
1037 * the same PM domain, so it is not necessary to use locking here.
1039 pm_genpd_sync_poweron(genpd);
1040 genpd->suspended_count--;
1042 return genpd_start_dev(genpd, dev);
1046 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1047 * @dev: Device to resume.
1049 * Carry out an early resume of a device under the assumption that its
1050 * pm_domain field points to the domain member of an object of type
1051 * struct generic_pm_domain representing a power domain consisting of I/O
1054 static int pm_genpd_resume_early(struct device *dev)
1056 struct generic_pm_domain *genpd;
1058 dev_dbg(dev, "%s()\n", __func__);
1060 genpd = dev_to_genpd(dev);
1064 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
1068 * pm_genpd_resume - Resume of device in an I/O PM domain.
1069 * @dev: Device to resume.
1071 * Resume a device under the assumption that its pm_domain field points to the
1072 * domain member of an object of type struct generic_pm_domain representing
1073 * a power domain consisting of I/O devices.
1075 static int pm_genpd_resume(struct device *dev)
1077 struct generic_pm_domain *genpd;
1079 dev_dbg(dev, "%s()\n", __func__);
1081 genpd = dev_to_genpd(dev);
1085 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
1089 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1090 * @dev: Device to freeze.
1092 * Freeze a device under the assumption that its pm_domain field points to the
1093 * domain member of an object of type struct generic_pm_domain representing
1094 * a power domain consisting of I/O devices.
1096 static int pm_genpd_freeze(struct device *dev)
1098 struct generic_pm_domain *genpd;
1100 dev_dbg(dev, "%s()\n", __func__);
1102 genpd = dev_to_genpd(dev);
1106 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
1110 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1111 * @dev: Device to freeze.
1113 * Carry out a late freeze of a device under the assumption that its
1114 * pm_domain field points to the domain member of an object of type
1115 * struct generic_pm_domain representing a power domain consisting of I/O
1118 static int pm_genpd_freeze_late(struct device *dev)
1120 struct generic_pm_domain *genpd;
1122 dev_dbg(dev, "%s()\n", __func__);
1124 genpd = dev_to_genpd(dev);
1128 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
1132 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1133 * @dev: Device to freeze.
1135 * Carry out a late freeze of a device under the assumption that its
1136 * pm_domain field points to the domain member of an object of type
1137 * struct generic_pm_domain representing a power domain consisting of I/O
1140 static int pm_genpd_freeze_noirq(struct device *dev)
1142 struct generic_pm_domain *genpd;
1144 dev_dbg(dev, "%s()\n", __func__);
1146 genpd = dev_to_genpd(dev);
1150 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1154 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1155 * @dev: Device to thaw.
1157 * Start the device, unless power has been removed from the domain already
1158 * before the system transition.
1160 static int pm_genpd_thaw_noirq(struct device *dev)
1162 struct generic_pm_domain *genpd;
1164 dev_dbg(dev, "%s()\n", __func__);
1166 genpd = dev_to_genpd(dev);
1170 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1174 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1175 * @dev: Device to thaw.
1177 * Carry out an early thaw of a device under the assumption that its
1178 * pm_domain field points to the domain member of an object of type
1179 * struct generic_pm_domain representing a power domain consisting of I/O
1182 static int pm_genpd_thaw_early(struct device *dev)
1184 struct generic_pm_domain *genpd;
1186 dev_dbg(dev, "%s()\n", __func__);
1188 genpd = dev_to_genpd(dev);
1192 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
1196 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1197 * @dev: Device to thaw.
1199 * Thaw a device under the assumption that its pm_domain field points to the
1200 * domain member of an object of type struct generic_pm_domain representing
1201 * a power domain consisting of I/O devices.
1203 static int pm_genpd_thaw(struct device *dev)
1205 struct generic_pm_domain *genpd;
1207 dev_dbg(dev, "%s()\n", __func__);
1209 genpd = dev_to_genpd(dev);
1213 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1217 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1218 * @dev: Device to resume.
1220 * Make sure the domain will be in the same power state as before the
1221 * hibernation the system is resuming from and start the device if necessary.
1223 static int pm_genpd_restore_noirq(struct device *dev)
1225 struct generic_pm_domain *genpd;
1227 dev_dbg(dev, "%s()\n", __func__);
1229 genpd = dev_to_genpd(dev);
1234 * Since all of the "noirq" callbacks are executed sequentially, it is
1235 * guaranteed that this function will never run twice in parallel for
1236 * the same PM domain, so it is not necessary to use locking here.
1238 * At this point suspended_count == 0 means we are being run for the
1239 * first time for the given domain in the present cycle.
1241 if (genpd->suspended_count++ == 0) {
1243 * The boot kernel might put the domain into arbitrary state,
1244 * so make it appear as powered off to pm_genpd_sync_poweron(),
1245 * so that it tries to power it on in case it was really off.
1247 genpd->status = GPD_STATE_POWER_OFF;
1248 if (genpd->suspend_power_off) {
1250 * If the domain was off before the hibernation, make
1251 * sure it will be off going forward.
1253 if (genpd->power_off)
1254 genpd->power_off(genpd);
1260 if (genpd->suspend_power_off)
1263 pm_genpd_sync_poweron(genpd);
1265 return genpd_start_dev(genpd, dev);
1269 * pm_genpd_complete - Complete power transition of a device in a power domain.
1270 * @dev: Device to complete the transition of.
1272 * Complete a power transition of a device (during a system-wide power
1273 * transition) under the assumption that its pm_domain field points to the
1274 * domain member of an object of type struct generic_pm_domain representing
1275 * a power domain consisting of I/O devices.
1277 static void pm_genpd_complete(struct device *dev)
1279 struct generic_pm_domain *genpd;
1282 dev_dbg(dev, "%s()\n", __func__);
1284 genpd = dev_to_genpd(dev);
1288 mutex_lock(&genpd->lock);
1290 run_complete = !genpd->suspend_power_off;
1291 if (--genpd->prepared_count == 0)
1292 genpd->suspend_power_off = false;
1294 mutex_unlock(&genpd->lock);
1297 pm_generic_complete(dev);
1298 pm_runtime_set_active(dev);
1299 pm_runtime_enable(dev);
1300 pm_request_idle(dev);
1305 * genpd_syscore_switch - Switch power during system core suspend or resume.
1306 * @dev: Device that normally is marked as "always on" to switch power for.
1308 * This routine may only be called during the system core (syscore) suspend or
1309 * resume phase for devices whose "always on" flags are set.
1311 static void genpd_syscore_switch(struct device *dev, bool suspend)
1313 struct generic_pm_domain *genpd;
1315 genpd = dev_to_genpd(dev);
1316 if (!pm_genpd_present(genpd))
1320 genpd->suspended_count++;
1321 pm_genpd_sync_poweroff(genpd);
1323 pm_genpd_sync_poweron(genpd);
1324 genpd->suspended_count--;
1328 void pm_genpd_syscore_poweroff(struct device *dev)
1330 genpd_syscore_switch(dev, true);
1332 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1334 void pm_genpd_syscore_poweron(struct device *dev)
1336 genpd_syscore_switch(dev, false);
1338 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1342 #define pm_genpd_prepare NULL
1343 #define pm_genpd_suspend NULL
1344 #define pm_genpd_suspend_late NULL
1345 #define pm_genpd_suspend_noirq NULL
1346 #define pm_genpd_resume_early NULL
1347 #define pm_genpd_resume_noirq NULL
1348 #define pm_genpd_resume NULL
1349 #define pm_genpd_freeze NULL
1350 #define pm_genpd_freeze_late NULL
1351 #define pm_genpd_freeze_noirq NULL
1352 #define pm_genpd_thaw_early NULL
1353 #define pm_genpd_thaw_noirq NULL
1354 #define pm_genpd_thaw NULL
1355 #define pm_genpd_restore_noirq NULL
1356 #define pm_genpd_complete NULL
1358 #endif /* CONFIG_PM_SLEEP */
1360 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1362 struct generic_pm_domain_data *gpd_data;
1364 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1368 mutex_init(&gpd_data->lock);
1369 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1370 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1374 static void __pm_genpd_free_dev_data(struct device *dev,
1375 struct generic_pm_domain_data *gpd_data)
1377 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1382 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1383 * @genpd: PM domain to add the device to.
1384 * @dev: Device to be added.
1385 * @td: Set of PM QoS timing parameters to attach to the device.
1387 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1388 struct gpd_timing_data *td)
1390 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1391 struct pm_domain_data *pdd;
1394 dev_dbg(dev, "%s()\n", __func__);
1396 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1399 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1403 genpd_acquire_lock(genpd);
1405 if (genpd->prepared_count > 0) {
1410 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1411 if (pdd->dev == dev) {
1416 ret = dev_pm_get_subsys_data(dev);
1420 genpd->device_count++;
1421 genpd->max_off_time_changed = true;
1423 spin_lock_irq(&dev->power.lock);
1425 dev->pm_domain = &genpd->domain;
1426 if (dev->power.subsys_data->domain_data) {
1427 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1429 gpd_data = gpd_data_new;
1430 dev->power.subsys_data->domain_data = &gpd_data->base;
1432 gpd_data->refcount++;
1436 spin_unlock_irq(&dev->power.lock);
1438 mutex_lock(&gpd_data->lock);
1439 gpd_data->base.dev = dev;
1440 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1441 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1442 gpd_data->td.constraint_changed = true;
1443 gpd_data->td.effective_constraint_ns = -1;
1444 mutex_unlock(&gpd_data->lock);
1447 genpd_release_lock(genpd);
1449 if (gpd_data != gpd_data_new)
1450 __pm_genpd_free_dev_data(dev, gpd_data_new);
1456 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1457 * @genpd_node: Device tree node pointer representing a PM domain to which the
1458 * the device is added to.
1459 * @dev: Device to be added.
1460 * @td: Set of PM QoS timing parameters to attach to the device.
1462 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1463 struct gpd_timing_data *td)
1465 struct generic_pm_domain *genpd = NULL, *gpd;
1467 dev_dbg(dev, "%s()\n", __func__);
1469 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1472 mutex_lock(&gpd_list_lock);
1473 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1474 if (gpd->of_node == genpd_node) {
1479 mutex_unlock(&gpd_list_lock);
1484 return __pm_genpd_add_device(genpd, dev, td);
1489 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1490 * @domain_name: Name of the PM domain to add the device to.
1491 * @dev: Device to be added.
1492 * @td: Set of PM QoS timing parameters to attach to the device.
1494 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1495 struct gpd_timing_data *td)
1497 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1501 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1502 * @genpd: PM domain to remove the device from.
1503 * @dev: Device to be removed.
1505 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1508 struct generic_pm_domain_data *gpd_data;
1509 struct pm_domain_data *pdd;
1510 bool remove = false;
1513 dev_dbg(dev, "%s()\n", __func__);
1515 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1516 || IS_ERR_OR_NULL(dev->pm_domain)
1517 || pd_to_genpd(dev->pm_domain) != genpd)
1520 genpd_acquire_lock(genpd);
1522 if (genpd->prepared_count > 0) {
1527 genpd->device_count--;
1528 genpd->max_off_time_changed = true;
1530 spin_lock_irq(&dev->power.lock);
1532 dev->pm_domain = NULL;
1533 pdd = dev->power.subsys_data->domain_data;
1534 list_del_init(&pdd->list_node);
1535 gpd_data = to_gpd_data(pdd);
1536 if (--gpd_data->refcount == 0) {
1537 dev->power.subsys_data->domain_data = NULL;
1541 spin_unlock_irq(&dev->power.lock);
1543 mutex_lock(&gpd_data->lock);
1545 mutex_unlock(&gpd_data->lock);
1547 genpd_release_lock(genpd);
1549 dev_pm_put_subsys_data(dev);
1551 __pm_genpd_free_dev_data(dev, gpd_data);
1556 genpd_release_lock(genpd);
1562 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1563 * @dev: Device to set/unset the flag for.
1564 * @val: The new value of the device's "need restore" flag.
1566 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1568 struct pm_subsys_data *psd;
1569 unsigned long flags;
1571 spin_lock_irqsave(&dev->power.lock, flags);
1573 psd = dev_to_psd(dev);
1574 if (psd && psd->domain_data)
1575 to_gpd_data(psd->domain_data)->need_restore = val;
1577 spin_unlock_irqrestore(&dev->power.lock, flags);
1579 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1582 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1583 * @genpd: Master PM domain to add the subdomain to.
1584 * @subdomain: Subdomain to be added.
1586 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1587 struct generic_pm_domain *subdomain)
1589 struct gpd_link *link;
1592 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1593 || genpd == subdomain)
1597 genpd_acquire_lock(genpd);
1598 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1600 if (subdomain->status != GPD_STATE_POWER_OFF
1601 && subdomain->status != GPD_STATE_ACTIVE) {
1602 mutex_unlock(&subdomain->lock);
1603 genpd_release_lock(genpd);
1607 if (genpd->status == GPD_STATE_POWER_OFF
1608 && subdomain->status != GPD_STATE_POWER_OFF) {
1613 list_for_each_entry(link, &genpd->master_links, master_node) {
1614 if (link->slave == subdomain && link->master == genpd) {
1620 link = kzalloc(sizeof(*link), GFP_KERNEL);
1625 link->master = genpd;
1626 list_add_tail(&link->master_node, &genpd->master_links);
1627 link->slave = subdomain;
1628 list_add_tail(&link->slave_node, &subdomain->slave_links);
1629 if (subdomain->status != GPD_STATE_POWER_OFF)
1630 genpd_sd_counter_inc(genpd);
1633 mutex_unlock(&subdomain->lock);
1634 genpd_release_lock(genpd);
1640 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1641 * @master_name: Name of the master PM domain to add the subdomain to.
1642 * @subdomain_name: Name of the subdomain to be added.
1644 int pm_genpd_add_subdomain_names(const char *master_name,
1645 const char *subdomain_name)
1647 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1649 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1652 mutex_lock(&gpd_list_lock);
1653 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1654 if (!master && !strcmp(gpd->name, master_name))
1657 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1660 if (master && subdomain)
1663 mutex_unlock(&gpd_list_lock);
1665 return pm_genpd_add_subdomain(master, subdomain);
1669 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1670 * @genpd: Master PM domain to remove the subdomain from.
1671 * @subdomain: Subdomain to be removed.
1673 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1674 struct generic_pm_domain *subdomain)
1676 struct gpd_link *link;
1679 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1683 genpd_acquire_lock(genpd);
1685 list_for_each_entry(link, &genpd->master_links, master_node) {
1686 if (link->slave != subdomain)
1689 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1691 if (subdomain->status != GPD_STATE_POWER_OFF
1692 && subdomain->status != GPD_STATE_ACTIVE) {
1693 mutex_unlock(&subdomain->lock);
1694 genpd_release_lock(genpd);
1698 list_del(&link->master_node);
1699 list_del(&link->slave_node);
1701 if (subdomain->status != GPD_STATE_POWER_OFF)
1702 genpd_sd_counter_dec(genpd);
1704 mutex_unlock(&subdomain->lock);
1710 genpd_release_lock(genpd);
1716 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1717 * @genpd: PM domain to be connected with cpuidle.
1718 * @state: cpuidle state this domain can disable/enable.
1720 * Make a PM domain behave as though it contained a CPU core, that is, instead
1721 * of calling its power down routine it will enable the given cpuidle state so
1722 * that the cpuidle subsystem can power it down (if possible and desirable).
1724 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1726 struct cpuidle_driver *cpuidle_drv;
1727 struct gpd_cpu_data *cpu_data;
1728 struct cpuidle_state *idle_state;
1731 if (IS_ERR_OR_NULL(genpd) || state < 0)
1734 genpd_acquire_lock(genpd);
1736 if (genpd->cpu_data) {
1740 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1745 cpuidle_drv = cpuidle_driver_ref();
1750 if (cpuidle_drv->state_count <= state) {
1754 idle_state = &cpuidle_drv->states[state];
1755 if (!idle_state->disabled) {
1759 cpu_data->idle_state = idle_state;
1760 cpu_data->saved_exit_latency = idle_state->exit_latency;
1761 genpd->cpu_data = cpu_data;
1762 genpd_recalc_cpu_exit_latency(genpd);
1765 genpd_release_lock(genpd);
1769 cpuidle_driver_unref();
1777 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1778 * @name: Name of the domain to connect to cpuidle.
1779 * @state: cpuidle state this domain can manipulate.
1781 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1783 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1787 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1788 * @genpd: PM domain to remove the cpuidle connection from.
1790 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1793 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1795 struct gpd_cpu_data *cpu_data;
1796 struct cpuidle_state *idle_state;
1799 if (IS_ERR_OR_NULL(genpd))
1802 genpd_acquire_lock(genpd);
1804 cpu_data = genpd->cpu_data;
1809 idle_state = cpu_data->idle_state;
1810 if (!idle_state->disabled) {
1814 idle_state->exit_latency = cpu_data->saved_exit_latency;
1815 cpuidle_driver_unref();
1816 genpd->cpu_data = NULL;
1820 genpd_release_lock(genpd);
1825 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1826 * @name: Name of the domain to disconnect cpuidle from.
1828 int pm_genpd_name_detach_cpuidle(const char *name)
1830 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1833 /* Default device callbacks for generic PM domains. */
1836 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1837 * @dev: Device to handle.
1839 static int pm_genpd_default_save_state(struct device *dev)
1841 int (*cb)(struct device *__dev);
1843 if (dev->type && dev->type->pm)
1844 cb = dev->type->pm->runtime_suspend;
1845 else if (dev->class && dev->class->pm)
1846 cb = dev->class->pm->runtime_suspend;
1847 else if (dev->bus && dev->bus->pm)
1848 cb = dev->bus->pm->runtime_suspend;
1852 if (!cb && dev->driver && dev->driver->pm)
1853 cb = dev->driver->pm->runtime_suspend;
1855 return cb ? cb(dev) : 0;
1859 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1860 * @dev: Device to handle.
1862 static int pm_genpd_default_restore_state(struct device *dev)
1864 int (*cb)(struct device *__dev);
1866 if (dev->type && dev->type->pm)
1867 cb = dev->type->pm->runtime_resume;
1868 else if (dev->class && dev->class->pm)
1869 cb = dev->class->pm->runtime_resume;
1870 else if (dev->bus && dev->bus->pm)
1871 cb = dev->bus->pm->runtime_resume;
1875 if (!cb && dev->driver && dev->driver->pm)
1876 cb = dev->driver->pm->runtime_resume;
1878 return cb ? cb(dev) : 0;
1882 * pm_genpd_init - Initialize a generic I/O PM domain object.
1883 * @genpd: PM domain object to initialize.
1884 * @gov: PM domain governor to associate with the domain (may be NULL).
1885 * @is_off: Initial value of the domain's power_is_off field.
1887 void pm_genpd_init(struct generic_pm_domain *genpd,
1888 struct dev_power_governor *gov, bool is_off)
1890 if (IS_ERR_OR_NULL(genpd))
1893 INIT_LIST_HEAD(&genpd->master_links);
1894 INIT_LIST_HEAD(&genpd->slave_links);
1895 INIT_LIST_HEAD(&genpd->dev_list);
1896 mutex_init(&genpd->lock);
1898 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1899 genpd->in_progress = 0;
1900 atomic_set(&genpd->sd_count, 0);
1901 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1902 init_waitqueue_head(&genpd->status_wait_queue);
1903 genpd->poweroff_task = NULL;
1904 genpd->resume_count = 0;
1905 genpd->device_count = 0;
1906 genpd->max_off_time_ns = -1;
1907 genpd->max_off_time_changed = true;
1908 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1909 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1910 genpd->domain.ops.prepare = pm_genpd_prepare;
1911 genpd->domain.ops.suspend = pm_genpd_suspend;
1912 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1913 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1914 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1915 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1916 genpd->domain.ops.resume = pm_genpd_resume;
1917 genpd->domain.ops.freeze = pm_genpd_freeze;
1918 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1919 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1920 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1921 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1922 genpd->domain.ops.thaw = pm_genpd_thaw;
1923 genpd->domain.ops.poweroff = pm_genpd_suspend;
1924 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1925 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1926 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1927 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1928 genpd->domain.ops.restore = pm_genpd_resume;
1929 genpd->domain.ops.complete = pm_genpd_complete;
1930 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1931 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1932 mutex_lock(&gpd_list_lock);
1933 list_add(&genpd->gpd_list_node, &gpd_list);
1934 mutex_unlock(&gpd_list_lock);