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); \
29 __routine = dev_gpd_data(dev)->ops.callback; \
31 __ret = __routine(dev); \
36 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
38 ktime_t __start = ktime_get(); \
39 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
40 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
41 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
42 if (!__retval && __elapsed > __td->field) { \
43 __td->field = __elapsed; \
44 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
46 genpd->max_off_time_changed = true; \
47 __td->constraint_changed = true; \
52 static LIST_HEAD(gpd_list);
53 static DEFINE_MUTEX(gpd_list_lock);
55 static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
57 struct generic_pm_domain *genpd = NULL, *gpd;
59 if (IS_ERR_OR_NULL(domain_name))
62 mutex_lock(&gpd_list_lock);
63 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
64 if (!strcmp(gpd->name, domain_name)) {
69 mutex_unlock(&gpd_list_lock);
75 struct generic_pm_domain *dev_to_genpd(struct device *dev)
77 if (IS_ERR_OR_NULL(dev->pm_domain))
78 return ERR_PTR(-EINVAL);
80 return pd_to_genpd(dev->pm_domain);
83 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
85 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
86 stop_latency_ns, "stop");
89 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
91 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
92 start_latency_ns, "start");
95 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
99 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
100 ret = !!atomic_dec_and_test(&genpd->sd_count);
105 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
107 atomic_inc(&genpd->sd_count);
108 smp_mb__after_atomic_inc();
111 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
115 mutex_lock(&genpd->lock);
117 * Wait for the domain to transition into either the active,
118 * or the power off state.
121 prepare_to_wait(&genpd->status_wait_queue, &wait,
122 TASK_UNINTERRUPTIBLE);
123 if (genpd->status == GPD_STATE_ACTIVE
124 || genpd->status == GPD_STATE_POWER_OFF)
126 mutex_unlock(&genpd->lock);
130 mutex_lock(&genpd->lock);
132 finish_wait(&genpd->status_wait_queue, &wait);
135 static void genpd_release_lock(struct generic_pm_domain *genpd)
137 mutex_unlock(&genpd->lock);
140 static void genpd_set_active(struct generic_pm_domain *genpd)
142 if (genpd->resume_count == 0)
143 genpd->status = GPD_STATE_ACTIVE;
146 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
150 if (!genpd->cpu_data)
153 usecs64 = genpd->power_on_latency_ns;
154 do_div(usecs64, NSEC_PER_USEC);
155 usecs64 += genpd->cpu_data->saved_exit_latency;
156 genpd->cpu_data->idle_state->exit_latency = usecs64;
160 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
161 * @genpd: PM domain to power up.
163 * Restore power to @genpd and all of its masters so that it is possible to
164 * resume a device belonging to it.
166 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
167 __releases(&genpd->lock) __acquires(&genpd->lock)
169 struct gpd_link *link;
173 /* If the domain's master is being waited for, we have to wait too. */
175 prepare_to_wait(&genpd->status_wait_queue, &wait,
176 TASK_UNINTERRUPTIBLE);
177 if (genpd->status != GPD_STATE_WAIT_MASTER)
179 mutex_unlock(&genpd->lock);
183 mutex_lock(&genpd->lock);
185 finish_wait(&genpd->status_wait_queue, &wait);
187 if (genpd->status == GPD_STATE_ACTIVE
188 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
191 if (genpd->status != GPD_STATE_POWER_OFF) {
192 genpd_set_active(genpd);
196 if (genpd->cpu_data) {
197 cpuidle_pause_and_lock();
198 genpd->cpu_data->idle_state->disabled = true;
199 cpuidle_resume_and_unlock();
204 * The list is guaranteed not to change while the loop below is being
205 * executed, unless one of the masters' .power_on() callbacks fiddles
208 list_for_each_entry(link, &genpd->slave_links, slave_node) {
209 genpd_sd_counter_inc(link->master);
210 genpd->status = GPD_STATE_WAIT_MASTER;
212 mutex_unlock(&genpd->lock);
214 ret = pm_genpd_poweron(link->master);
216 mutex_lock(&genpd->lock);
219 * The "wait for parent" status is guaranteed not to change
220 * while the master is powering on.
222 genpd->status = GPD_STATE_POWER_OFF;
223 wake_up_all(&genpd->status_wait_queue);
225 genpd_sd_counter_dec(link->master);
230 if (genpd->power_on) {
231 ktime_t time_start = ktime_get();
234 ret = genpd->power_on(genpd);
238 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
239 if (elapsed_ns > genpd->power_on_latency_ns) {
240 genpd->power_on_latency_ns = elapsed_ns;
241 genpd->max_off_time_changed = true;
242 genpd_recalc_cpu_exit_latency(genpd);
244 pr_warning("%s: Power-on latency exceeded, "
245 "new value %lld ns\n", genpd->name,
251 genpd_set_active(genpd);
256 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
257 genpd_sd_counter_dec(link->master);
263 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
264 * @genpd: PM domain to power up.
266 int pm_genpd_poweron(struct generic_pm_domain *genpd)
270 mutex_lock(&genpd->lock);
271 ret = __pm_genpd_poweron(genpd);
272 mutex_unlock(&genpd->lock);
277 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
278 * @domain_name: Name of the PM domain to power up.
280 int pm_genpd_name_poweron(const char *domain_name)
282 struct generic_pm_domain *genpd;
284 genpd = pm_genpd_lookup_name(domain_name);
285 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
288 #endif /* CONFIG_PM */
290 #ifdef CONFIG_PM_RUNTIME
292 static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
295 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
298 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
300 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
301 save_state_latency_ns, "state save");
304 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
306 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
307 restore_state_latency_ns,
311 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
312 unsigned long val, void *ptr)
314 struct generic_pm_domain_data *gpd_data;
317 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
319 mutex_lock(&gpd_data->lock);
320 dev = gpd_data->base.dev;
322 mutex_unlock(&gpd_data->lock);
325 mutex_unlock(&gpd_data->lock);
328 struct generic_pm_domain *genpd;
329 struct pm_domain_data *pdd;
331 spin_lock_irq(&dev->power.lock);
333 pdd = dev->power.subsys_data ?
334 dev->power.subsys_data->domain_data : NULL;
335 if (pdd && pdd->dev) {
336 to_gpd_data(pdd)->td.constraint_changed = true;
337 genpd = dev_to_genpd(dev);
339 genpd = ERR_PTR(-ENODATA);
342 spin_unlock_irq(&dev->power.lock);
344 if (!IS_ERR(genpd)) {
345 mutex_lock(&genpd->lock);
346 genpd->max_off_time_changed = true;
347 mutex_unlock(&genpd->lock);
351 if (!dev || dev->power.ignore_children)
359 * __pm_genpd_save_device - Save the pre-suspend state of a device.
360 * @pdd: Domain data of the device to save the state of.
361 * @genpd: PM domain the device belongs to.
363 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
364 struct generic_pm_domain *genpd)
365 __releases(&genpd->lock) __acquires(&genpd->lock)
367 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
368 struct device *dev = pdd->dev;
371 if (gpd_data->need_restore)
374 mutex_unlock(&genpd->lock);
376 genpd_start_dev(genpd, dev);
377 ret = genpd_save_dev(genpd, dev);
378 genpd_stop_dev(genpd, dev);
380 mutex_lock(&genpd->lock);
383 gpd_data->need_restore = true;
389 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
390 * @pdd: Domain data of the device to restore the state of.
391 * @genpd: PM domain the device belongs to.
393 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
394 struct generic_pm_domain *genpd)
395 __releases(&genpd->lock) __acquires(&genpd->lock)
397 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
398 struct device *dev = pdd->dev;
399 bool need_restore = gpd_data->need_restore;
401 gpd_data->need_restore = false;
402 mutex_unlock(&genpd->lock);
404 genpd_start_dev(genpd, dev);
406 genpd_restore_dev(genpd, dev);
408 mutex_lock(&genpd->lock);
412 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
413 * @genpd: PM domain to check.
415 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
416 * a "power off" operation, which means that a "power on" has occured in the
417 * meantime, or if its resume_count field is different from zero, which means
418 * that one of its devices has been resumed in the meantime.
420 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
422 return genpd->status == GPD_STATE_WAIT_MASTER
423 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
427 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
428 * @genpd: PM domait to power off.
430 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
433 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
435 queue_work(pm_wq, &genpd->power_off_work);
439 * pm_genpd_poweroff - Remove power from a given PM domain.
440 * @genpd: PM domain to power down.
442 * If all of the @genpd's devices have been suspended and all of its subdomains
443 * have been powered down, run the runtime suspend callbacks provided by all of
444 * the @genpd's devices' drivers and remove power from @genpd.
446 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
447 __releases(&genpd->lock) __acquires(&genpd->lock)
449 struct pm_domain_data *pdd;
450 struct gpd_link *link;
451 unsigned int not_suspended;
456 * Do not try to power off the domain in the following situations:
457 * (1) The domain is already in the "power off" state.
458 * (2) The domain is waiting for its master to power up.
459 * (3) One of the domain's devices is being resumed right now.
460 * (4) System suspend is in progress.
462 if (genpd->status == GPD_STATE_POWER_OFF
463 || genpd->status == GPD_STATE_WAIT_MASTER
464 || genpd->resume_count > 0 || genpd->prepared_count > 0)
467 if (atomic_read(&genpd->sd_count) > 0)
471 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
472 enum pm_qos_flags_status stat;
474 stat = dev_pm_qos_flags(pdd->dev,
475 PM_QOS_FLAG_NO_POWER_OFF
476 | PM_QOS_FLAG_REMOTE_WAKEUP);
477 if (stat > PM_QOS_FLAGS_NONE)
480 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
481 || pdd->dev->power.irq_safe))
485 if (not_suspended > genpd->in_progress)
488 if (genpd->poweroff_task) {
490 * Another instance of pm_genpd_poweroff() is executing
491 * callbacks, so tell it to start over and return.
493 genpd->status = GPD_STATE_REPEAT;
497 if (genpd->gov && genpd->gov->power_down_ok) {
498 if (!genpd->gov->power_down_ok(&genpd->domain))
502 genpd->status = GPD_STATE_BUSY;
503 genpd->poweroff_task = current;
505 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
506 ret = atomic_read(&genpd->sd_count) == 0 ?
507 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
509 if (genpd_abort_poweroff(genpd))
513 genpd_set_active(genpd);
517 if (genpd->status == GPD_STATE_REPEAT) {
518 genpd->poweroff_task = NULL;
523 if (genpd->cpu_data) {
525 * If cpu_data is set, cpuidle should turn the domain off when
526 * the CPU in it is idle. In that case we don't decrement the
527 * subdomain counts of the master domains, so that power is not
528 * removed from the current domain prematurely as a result of
529 * cutting off the masters' power.
531 genpd->status = GPD_STATE_POWER_OFF;
532 cpuidle_pause_and_lock();
533 genpd->cpu_data->idle_state->disabled = false;
534 cpuidle_resume_and_unlock();
538 if (genpd->power_off) {
542 if (atomic_read(&genpd->sd_count) > 0) {
547 time_start = ktime_get();
550 * If sd_count > 0 at this point, one of the subdomains hasn't
551 * managed to call pm_genpd_poweron() for the master yet after
552 * incrementing it. In that case pm_genpd_poweron() will wait
553 * for us to drop the lock, so we can call .power_off() and let
554 * the pm_genpd_poweron() restore power for us (this shouldn't
555 * happen very often).
557 ret = genpd->power_off(genpd);
559 genpd_set_active(genpd);
563 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
564 if (elapsed_ns > genpd->power_off_latency_ns) {
565 genpd->power_off_latency_ns = elapsed_ns;
566 genpd->max_off_time_changed = true;
568 pr_warning("%s: Power-off latency exceeded, "
569 "new value %lld ns\n", genpd->name,
574 genpd->status = GPD_STATE_POWER_OFF;
576 list_for_each_entry(link, &genpd->slave_links, slave_node) {
577 genpd_sd_counter_dec(link->master);
578 genpd_queue_power_off_work(link->master);
582 genpd->poweroff_task = NULL;
583 wake_up_all(&genpd->status_wait_queue);
588 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
589 * @work: Work structure used for scheduling the execution of this function.
591 static void genpd_power_off_work_fn(struct work_struct *work)
593 struct generic_pm_domain *genpd;
595 genpd = container_of(work, struct generic_pm_domain, power_off_work);
597 genpd_acquire_lock(genpd);
598 pm_genpd_poweroff(genpd);
599 genpd_release_lock(genpd);
603 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
604 * @dev: Device to suspend.
606 * Carry out a runtime suspend of a device under the assumption that its
607 * pm_domain field points to the domain member of an object of type
608 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
610 static int pm_genpd_runtime_suspend(struct device *dev)
612 struct generic_pm_domain *genpd;
613 bool (*stop_ok)(struct device *__dev);
616 dev_dbg(dev, "%s()\n", __func__);
618 genpd = dev_to_genpd(dev);
622 might_sleep_if(!genpd->dev_irq_safe);
624 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
625 if (stop_ok && !stop_ok(dev))
628 ret = genpd_stop_dev(genpd, dev);
633 * If power.irq_safe is set, this routine will be run with interrupts
634 * off, so it can't use mutexes.
636 if (dev->power.irq_safe)
639 mutex_lock(&genpd->lock);
640 genpd->in_progress++;
641 pm_genpd_poweroff(genpd);
642 genpd->in_progress--;
643 mutex_unlock(&genpd->lock);
649 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
650 * @dev: Device to resume.
652 * Carry out a runtime resume of a device under the assumption that its
653 * pm_domain field points to the domain member of an object of type
654 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
656 static int pm_genpd_runtime_resume(struct device *dev)
658 struct generic_pm_domain *genpd;
662 dev_dbg(dev, "%s()\n", __func__);
664 genpd = dev_to_genpd(dev);
668 might_sleep_if(!genpd->dev_irq_safe);
670 /* If power.irq_safe, the PM domain is never powered off. */
671 if (dev->power.irq_safe)
672 return genpd_start_dev_no_timing(genpd, dev);
674 mutex_lock(&genpd->lock);
675 ret = __pm_genpd_poweron(genpd);
677 mutex_unlock(&genpd->lock);
680 genpd->status = GPD_STATE_BUSY;
681 genpd->resume_count++;
683 prepare_to_wait(&genpd->status_wait_queue, &wait,
684 TASK_UNINTERRUPTIBLE);
686 * If current is the powering off task, we have been called
687 * reentrantly from one of the device callbacks, so we should
690 if (!genpd->poweroff_task || genpd->poweroff_task == current)
692 mutex_unlock(&genpd->lock);
696 mutex_lock(&genpd->lock);
698 finish_wait(&genpd->status_wait_queue, &wait);
699 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
700 genpd->resume_count--;
701 genpd_set_active(genpd);
702 wake_up_all(&genpd->status_wait_queue);
703 mutex_unlock(&genpd->lock);
709 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
711 void pm_genpd_poweroff_unused(void)
713 struct generic_pm_domain *genpd;
715 mutex_lock(&gpd_list_lock);
717 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
718 genpd_queue_power_off_work(genpd);
720 mutex_unlock(&gpd_list_lock);
725 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
726 unsigned long val, void *ptr)
731 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
733 #define pm_genpd_runtime_suspend NULL
734 #define pm_genpd_runtime_resume NULL
736 #endif /* CONFIG_PM_RUNTIME */
738 #ifdef CONFIG_PM_SLEEP
741 * pm_genpd_present - Check if the given PM domain has been initialized.
742 * @genpd: PM domain to check.
744 static bool pm_genpd_present(struct generic_pm_domain *genpd)
746 struct generic_pm_domain *gpd;
748 if (IS_ERR_OR_NULL(genpd))
751 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
758 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
761 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
764 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
766 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
769 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
771 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
774 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
776 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
779 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
781 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
784 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
786 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
789 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
791 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
794 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
796 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
799 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
801 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
805 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
806 * @genpd: PM domain to power off, if possible.
808 * Check if the given PM domain can be powered off (during system suspend or
809 * hibernation) and do that if so. Also, in that case propagate to its masters.
811 * This function is only called in "noirq" and "syscore" stages of system power
812 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
813 * executed sequentially, so it is guaranteed that it will never run twice in
816 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
818 struct gpd_link *link;
820 if (genpd->status == GPD_STATE_POWER_OFF)
823 if (genpd->suspended_count != genpd->device_count
824 || atomic_read(&genpd->sd_count) > 0)
827 if (genpd->power_off)
828 genpd->power_off(genpd);
830 genpd->status = GPD_STATE_POWER_OFF;
832 list_for_each_entry(link, &genpd->slave_links, slave_node) {
833 genpd_sd_counter_dec(link->master);
834 pm_genpd_sync_poweroff(link->master);
839 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
840 * @genpd: PM domain to power on.
842 * This function is only called in "noirq" and "syscore" stages of system power
843 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
844 * executed sequentially, so it is guaranteed that it will never run twice in
847 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
849 struct gpd_link *link;
851 if (genpd->status != GPD_STATE_POWER_OFF)
854 list_for_each_entry(link, &genpd->slave_links, slave_node) {
855 pm_genpd_sync_poweron(link->master);
856 genpd_sd_counter_inc(link->master);
860 genpd->power_on(genpd);
862 genpd->status = GPD_STATE_ACTIVE;
866 * resume_needed - Check whether to resume a device before system suspend.
867 * @dev: Device to check.
868 * @genpd: PM domain the device belongs to.
870 * There are two cases in which a device that can wake up the system from sleep
871 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
872 * to wake up the system and it has to remain active for this purpose while the
873 * system is in the sleep state and (2) if the device is not enabled to wake up
874 * the system from sleep states and it generally doesn't generate wakeup signals
875 * by itself (those signals are generated on its behalf by other parts of the
876 * system). In the latter case it may be necessary to reconfigure the device's
877 * wakeup settings during system suspend, because it may have been set up to
878 * signal remote wakeup from the system's working state as needed by runtime PM.
879 * Return 'true' in either of the above cases.
881 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
885 if (!device_can_wakeup(dev))
888 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
889 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
893 * pm_genpd_prepare - Start power transition of a device in a PM domain.
894 * @dev: Device to start the transition of.
896 * Start a power transition of a device (during a system-wide power transition)
897 * under the assumption that its pm_domain field points to the domain member of
898 * an object of type struct generic_pm_domain representing a PM domain
899 * consisting of I/O devices.
901 static int pm_genpd_prepare(struct device *dev)
903 struct generic_pm_domain *genpd;
906 dev_dbg(dev, "%s()\n", __func__);
908 genpd = dev_to_genpd(dev);
913 * If a wakeup request is pending for the device, it should be woken up
914 * at this point and a system wakeup event should be reported if it's
915 * set up to wake up the system from sleep states.
917 pm_runtime_get_noresume(dev);
918 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
919 pm_wakeup_event(dev, 0);
921 if (pm_wakeup_pending()) {
926 if (resume_needed(dev, genpd))
927 pm_runtime_resume(dev);
929 genpd_acquire_lock(genpd);
931 if (genpd->prepared_count++ == 0) {
932 genpd->suspended_count = 0;
933 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
936 genpd_release_lock(genpd);
938 if (genpd->suspend_power_off) {
939 pm_runtime_put_noidle(dev);
944 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
945 * so pm_genpd_poweron() will return immediately, but if the device
946 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
947 * to make it operational.
949 pm_runtime_resume(dev);
950 __pm_runtime_disable(dev, false);
952 ret = pm_generic_prepare(dev);
954 mutex_lock(&genpd->lock);
956 if (--genpd->prepared_count == 0)
957 genpd->suspend_power_off = false;
959 mutex_unlock(&genpd->lock);
960 pm_runtime_enable(dev);
968 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
969 * @dev: Device to suspend.
971 * Suspend a device under the assumption that its pm_domain field points to the
972 * domain member of an object of type struct generic_pm_domain representing
973 * a PM domain consisting of I/O devices.
975 static int pm_genpd_suspend(struct device *dev)
977 struct generic_pm_domain *genpd;
979 dev_dbg(dev, "%s()\n", __func__);
981 genpd = dev_to_genpd(dev);
985 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
989 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
990 * @dev: Device to suspend.
992 * Carry out a late suspend of a device under the assumption that its
993 * pm_domain field points to the domain member of an object of type
994 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
996 static int pm_genpd_suspend_late(struct device *dev)
998 struct generic_pm_domain *genpd;
1000 dev_dbg(dev, "%s()\n", __func__);
1002 genpd = dev_to_genpd(dev);
1006 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1010 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1011 * @dev: Device to suspend.
1013 * Stop the device and remove power from the domain if all devices in it have
1016 static int pm_genpd_suspend_noirq(struct device *dev)
1018 struct generic_pm_domain *genpd;
1020 dev_dbg(dev, "%s()\n", __func__);
1022 genpd = dev_to_genpd(dev);
1026 if (genpd->suspend_power_off
1027 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1030 genpd_stop_dev(genpd, dev);
1033 * Since all of the "noirq" callbacks are executed sequentially, it is
1034 * guaranteed that this function will never run twice in parallel for
1035 * the same PM domain, so it is not necessary to use locking here.
1037 genpd->suspended_count++;
1038 pm_genpd_sync_poweroff(genpd);
1044 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1045 * @dev: Device to resume.
1047 * Restore power to the device's PM domain, if necessary, and start the device.
1049 static int pm_genpd_resume_noirq(struct device *dev)
1051 struct generic_pm_domain *genpd;
1053 dev_dbg(dev, "%s()\n", __func__);
1055 genpd = dev_to_genpd(dev);
1059 if (genpd->suspend_power_off
1060 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1064 * Since all of the "noirq" callbacks are executed sequentially, it is
1065 * guaranteed that this function will never run twice in parallel for
1066 * the same PM domain, so it is not necessary to use locking here.
1068 pm_genpd_sync_poweron(genpd);
1069 genpd->suspended_count--;
1071 return genpd_start_dev(genpd, dev);
1075 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1076 * @dev: Device to resume.
1078 * Carry out an early resume of a device under the assumption that its
1079 * pm_domain field points to the domain member of an object of type
1080 * struct generic_pm_domain representing a power domain consisting of I/O
1083 static int pm_genpd_resume_early(struct device *dev)
1085 struct generic_pm_domain *genpd;
1087 dev_dbg(dev, "%s()\n", __func__);
1089 genpd = dev_to_genpd(dev);
1093 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1097 * pm_genpd_resume - Resume of device in an I/O PM domain.
1098 * @dev: Device to resume.
1100 * Resume a device under the assumption that its pm_domain field points to the
1101 * domain member of an object of type struct generic_pm_domain representing
1102 * a power domain consisting of I/O devices.
1104 static int pm_genpd_resume(struct device *dev)
1106 struct generic_pm_domain *genpd;
1108 dev_dbg(dev, "%s()\n", __func__);
1110 genpd = dev_to_genpd(dev);
1114 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1118 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1119 * @dev: Device to freeze.
1121 * Freeze a device under the assumption that its pm_domain field points to the
1122 * domain member of an object of type struct generic_pm_domain representing
1123 * a power domain consisting of I/O devices.
1125 static int pm_genpd_freeze(struct device *dev)
1127 struct generic_pm_domain *genpd;
1129 dev_dbg(dev, "%s()\n", __func__);
1131 genpd = dev_to_genpd(dev);
1135 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1139 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1140 * @dev: Device to freeze.
1142 * Carry out a late freeze of a device under the assumption that its
1143 * pm_domain field points to the domain member of an object of type
1144 * struct generic_pm_domain representing a power domain consisting of I/O
1147 static int pm_genpd_freeze_late(struct device *dev)
1149 struct generic_pm_domain *genpd;
1151 dev_dbg(dev, "%s()\n", __func__);
1153 genpd = dev_to_genpd(dev);
1157 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1161 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1162 * @dev: Device to freeze.
1164 * Carry out a late freeze of a device under the assumption that its
1165 * pm_domain field points to the domain member of an object of type
1166 * struct generic_pm_domain representing a power domain consisting of I/O
1169 static int pm_genpd_freeze_noirq(struct device *dev)
1171 struct generic_pm_domain *genpd;
1173 dev_dbg(dev, "%s()\n", __func__);
1175 genpd = dev_to_genpd(dev);
1179 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1183 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1184 * @dev: Device to thaw.
1186 * Start the device, unless power has been removed from the domain already
1187 * before the system transition.
1189 static int pm_genpd_thaw_noirq(struct device *dev)
1191 struct generic_pm_domain *genpd;
1193 dev_dbg(dev, "%s()\n", __func__);
1195 genpd = dev_to_genpd(dev);
1199 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1203 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1204 * @dev: Device to thaw.
1206 * Carry out an early thaw of a device under the assumption that its
1207 * pm_domain field points to the domain member of an object of type
1208 * struct generic_pm_domain representing a power domain consisting of I/O
1211 static int pm_genpd_thaw_early(struct device *dev)
1213 struct generic_pm_domain *genpd;
1215 dev_dbg(dev, "%s()\n", __func__);
1217 genpd = dev_to_genpd(dev);
1221 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1225 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1226 * @dev: Device to thaw.
1228 * Thaw a device under the assumption that its pm_domain field points to the
1229 * domain member of an object of type struct generic_pm_domain representing
1230 * a power domain consisting of I/O devices.
1232 static int pm_genpd_thaw(struct device *dev)
1234 struct generic_pm_domain *genpd;
1236 dev_dbg(dev, "%s()\n", __func__);
1238 genpd = dev_to_genpd(dev);
1242 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1246 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1247 * @dev: Device to resume.
1249 * Make sure the domain will be in the same power state as before the
1250 * hibernation the system is resuming from and start the device if necessary.
1252 static int pm_genpd_restore_noirq(struct device *dev)
1254 struct generic_pm_domain *genpd;
1256 dev_dbg(dev, "%s()\n", __func__);
1258 genpd = dev_to_genpd(dev);
1263 * Since all of the "noirq" callbacks are executed sequentially, it is
1264 * guaranteed that this function will never run twice in parallel for
1265 * the same PM domain, so it is not necessary to use locking here.
1267 * At this point suspended_count == 0 means we are being run for the
1268 * first time for the given domain in the present cycle.
1270 if (genpd->suspended_count++ == 0) {
1272 * The boot kernel might put the domain into arbitrary state,
1273 * so make it appear as powered off to pm_genpd_sync_poweron(),
1274 * so that it tries to power it on in case it was really off.
1276 genpd->status = GPD_STATE_POWER_OFF;
1277 if (genpd->suspend_power_off) {
1279 * If the domain was off before the hibernation, make
1280 * sure it will be off going forward.
1282 if (genpd->power_off)
1283 genpd->power_off(genpd);
1289 if (genpd->suspend_power_off)
1292 pm_genpd_sync_poweron(genpd);
1294 return genpd_start_dev(genpd, dev);
1298 * pm_genpd_complete - Complete power transition of a device in a power domain.
1299 * @dev: Device to complete the transition of.
1301 * Complete a power transition of a device (during a system-wide power
1302 * transition) under the assumption that its pm_domain field points to the
1303 * domain member of an object of type struct generic_pm_domain representing
1304 * a power domain consisting of I/O devices.
1306 static void pm_genpd_complete(struct device *dev)
1308 struct generic_pm_domain *genpd;
1311 dev_dbg(dev, "%s()\n", __func__);
1313 genpd = dev_to_genpd(dev);
1317 mutex_lock(&genpd->lock);
1319 run_complete = !genpd->suspend_power_off;
1320 if (--genpd->prepared_count == 0)
1321 genpd->suspend_power_off = false;
1323 mutex_unlock(&genpd->lock);
1326 pm_generic_complete(dev);
1327 pm_runtime_set_active(dev);
1328 pm_runtime_enable(dev);
1329 pm_request_idle(dev);
1334 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1335 * @dev: Device that normally is marked as "always on" to switch power for.
1337 * This routine may only be called during the system core (syscore) suspend or
1338 * resume phase for devices whose "always on" flags are set.
1340 void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1342 struct generic_pm_domain *genpd;
1344 genpd = dev_to_genpd(dev);
1345 if (!pm_genpd_present(genpd))
1349 genpd->suspended_count++;
1350 pm_genpd_sync_poweroff(genpd);
1352 pm_genpd_sync_poweron(genpd);
1353 genpd->suspended_count--;
1356 EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1360 #define pm_genpd_prepare NULL
1361 #define pm_genpd_suspend NULL
1362 #define pm_genpd_suspend_late NULL
1363 #define pm_genpd_suspend_noirq NULL
1364 #define pm_genpd_resume_early NULL
1365 #define pm_genpd_resume_noirq NULL
1366 #define pm_genpd_resume NULL
1367 #define pm_genpd_freeze NULL
1368 #define pm_genpd_freeze_late NULL
1369 #define pm_genpd_freeze_noirq NULL
1370 #define pm_genpd_thaw_early NULL
1371 #define pm_genpd_thaw_noirq NULL
1372 #define pm_genpd_thaw NULL
1373 #define pm_genpd_restore_noirq NULL
1374 #define pm_genpd_complete NULL
1376 #endif /* CONFIG_PM_SLEEP */
1378 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1380 struct generic_pm_domain_data *gpd_data;
1382 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1386 mutex_init(&gpd_data->lock);
1387 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1388 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1392 static void __pm_genpd_free_dev_data(struct device *dev,
1393 struct generic_pm_domain_data *gpd_data)
1395 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1400 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1401 * @genpd: PM domain to add the device to.
1402 * @dev: Device to be added.
1403 * @td: Set of PM QoS timing parameters to attach to the device.
1405 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1406 struct gpd_timing_data *td)
1408 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1409 struct pm_domain_data *pdd;
1412 dev_dbg(dev, "%s()\n", __func__);
1414 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1417 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1421 genpd_acquire_lock(genpd);
1423 if (genpd->prepared_count > 0) {
1428 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1429 if (pdd->dev == dev) {
1434 ret = dev_pm_get_subsys_data(dev);
1438 genpd->device_count++;
1439 genpd->max_off_time_changed = true;
1441 spin_lock_irq(&dev->power.lock);
1443 dev->pm_domain = &genpd->domain;
1444 if (dev->power.subsys_data->domain_data) {
1445 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1447 gpd_data = gpd_data_new;
1448 dev->power.subsys_data->domain_data = &gpd_data->base;
1450 gpd_data->refcount++;
1454 spin_unlock_irq(&dev->power.lock);
1456 mutex_lock(&gpd_data->lock);
1457 gpd_data->base.dev = dev;
1458 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1459 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1460 gpd_data->td.constraint_changed = true;
1461 gpd_data->td.effective_constraint_ns = -1;
1462 mutex_unlock(&gpd_data->lock);
1465 genpd_release_lock(genpd);
1467 if (gpd_data != gpd_data_new)
1468 __pm_genpd_free_dev_data(dev, gpd_data_new);
1474 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1475 * @genpd_node: Device tree node pointer representing a PM domain to which the
1476 * the device is added to.
1477 * @dev: Device to be added.
1478 * @td: Set of PM QoS timing parameters to attach to the device.
1480 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1481 struct gpd_timing_data *td)
1483 struct generic_pm_domain *genpd = NULL, *gpd;
1485 dev_dbg(dev, "%s()\n", __func__);
1487 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1490 mutex_lock(&gpd_list_lock);
1491 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1492 if (gpd->of_node == genpd_node) {
1497 mutex_unlock(&gpd_list_lock);
1502 return __pm_genpd_add_device(genpd, dev, td);
1507 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1508 * @domain_name: Name of the PM domain to add the device to.
1509 * @dev: Device to be added.
1510 * @td: Set of PM QoS timing parameters to attach to the device.
1512 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1513 struct gpd_timing_data *td)
1515 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1519 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1520 * @genpd: PM domain to remove the device from.
1521 * @dev: Device to be removed.
1523 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1526 struct generic_pm_domain_data *gpd_data;
1527 struct pm_domain_data *pdd;
1528 bool remove = false;
1531 dev_dbg(dev, "%s()\n", __func__);
1533 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1534 || IS_ERR_OR_NULL(dev->pm_domain)
1535 || pd_to_genpd(dev->pm_domain) != genpd)
1538 genpd_acquire_lock(genpd);
1540 if (genpd->prepared_count > 0) {
1545 genpd->device_count--;
1546 genpd->max_off_time_changed = true;
1548 spin_lock_irq(&dev->power.lock);
1550 dev->pm_domain = NULL;
1551 pdd = dev->power.subsys_data->domain_data;
1552 list_del_init(&pdd->list_node);
1553 gpd_data = to_gpd_data(pdd);
1554 if (--gpd_data->refcount == 0) {
1555 dev->power.subsys_data->domain_data = NULL;
1559 spin_unlock_irq(&dev->power.lock);
1561 mutex_lock(&gpd_data->lock);
1563 mutex_unlock(&gpd_data->lock);
1565 genpd_release_lock(genpd);
1567 dev_pm_put_subsys_data(dev);
1569 __pm_genpd_free_dev_data(dev, gpd_data);
1574 genpd_release_lock(genpd);
1580 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1581 * @dev: Device to set/unset the flag for.
1582 * @val: The new value of the device's "need restore" flag.
1584 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1586 struct pm_subsys_data *psd;
1587 unsigned long flags;
1589 spin_lock_irqsave(&dev->power.lock, flags);
1591 psd = dev_to_psd(dev);
1592 if (psd && psd->domain_data)
1593 to_gpd_data(psd->domain_data)->need_restore = val;
1595 spin_unlock_irqrestore(&dev->power.lock, flags);
1597 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1600 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1601 * @genpd: Master PM domain to add the subdomain to.
1602 * @subdomain: Subdomain to be added.
1604 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1605 struct generic_pm_domain *subdomain)
1607 struct gpd_link *link;
1610 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1611 || genpd == subdomain)
1615 genpd_acquire_lock(genpd);
1616 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1618 if (subdomain->status != GPD_STATE_POWER_OFF
1619 && subdomain->status != GPD_STATE_ACTIVE) {
1620 mutex_unlock(&subdomain->lock);
1621 genpd_release_lock(genpd);
1625 if (genpd->status == GPD_STATE_POWER_OFF
1626 && subdomain->status != GPD_STATE_POWER_OFF) {
1631 list_for_each_entry(link, &genpd->master_links, master_node) {
1632 if (link->slave == subdomain && link->master == genpd) {
1638 link = kzalloc(sizeof(*link), GFP_KERNEL);
1643 link->master = genpd;
1644 list_add_tail(&link->master_node, &genpd->master_links);
1645 link->slave = subdomain;
1646 list_add_tail(&link->slave_node, &subdomain->slave_links);
1647 if (subdomain->status != GPD_STATE_POWER_OFF)
1648 genpd_sd_counter_inc(genpd);
1651 mutex_unlock(&subdomain->lock);
1652 genpd_release_lock(genpd);
1658 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1659 * @master_name: Name of the master PM domain to add the subdomain to.
1660 * @subdomain_name: Name of the subdomain to be added.
1662 int pm_genpd_add_subdomain_names(const char *master_name,
1663 const char *subdomain_name)
1665 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1667 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1670 mutex_lock(&gpd_list_lock);
1671 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1672 if (!master && !strcmp(gpd->name, master_name))
1675 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1678 if (master && subdomain)
1681 mutex_unlock(&gpd_list_lock);
1683 return pm_genpd_add_subdomain(master, subdomain);
1687 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1688 * @genpd: Master PM domain to remove the subdomain from.
1689 * @subdomain: Subdomain to be removed.
1691 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1692 struct generic_pm_domain *subdomain)
1694 struct gpd_link *link;
1697 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1701 genpd_acquire_lock(genpd);
1703 list_for_each_entry(link, &genpd->master_links, master_node) {
1704 if (link->slave != subdomain)
1707 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1709 if (subdomain->status != GPD_STATE_POWER_OFF
1710 && subdomain->status != GPD_STATE_ACTIVE) {
1711 mutex_unlock(&subdomain->lock);
1712 genpd_release_lock(genpd);
1716 list_del(&link->master_node);
1717 list_del(&link->slave_node);
1719 if (subdomain->status != GPD_STATE_POWER_OFF)
1720 genpd_sd_counter_dec(genpd);
1722 mutex_unlock(&subdomain->lock);
1728 genpd_release_lock(genpd);
1734 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1735 * @dev: Device to add the callbacks to.
1736 * @ops: Set of callbacks to add.
1737 * @td: Timing data to add to the device along with the callbacks (optional).
1739 * Every call to this routine should be balanced with a call to
1740 * __pm_genpd_remove_callbacks() and they must not be nested.
1742 int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1743 struct gpd_timing_data *td)
1745 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1751 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1755 pm_runtime_disable(dev);
1758 ret = dev_pm_get_subsys_data(dev);
1762 spin_lock_irq(&dev->power.lock);
1764 if (dev->power.subsys_data->domain_data) {
1765 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1767 gpd_data = gpd_data_new;
1768 dev->power.subsys_data->domain_data = &gpd_data->base;
1770 gpd_data->refcount++;
1771 gpd_data->ops = *ops;
1775 spin_unlock_irq(&dev->power.lock);
1779 pm_runtime_enable(dev);
1781 if (gpd_data != gpd_data_new)
1782 __pm_genpd_free_dev_data(dev, gpd_data_new);
1786 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1789 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1790 * @dev: Device to remove the callbacks from.
1791 * @clear_td: If set, clear the device's timing data too.
1793 * This routine can only be called after pm_genpd_add_callbacks().
1795 int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1797 struct generic_pm_domain_data *gpd_data = NULL;
1798 bool remove = false;
1801 if (!(dev && dev->power.subsys_data))
1804 pm_runtime_disable(dev);
1807 spin_lock_irq(&dev->power.lock);
1809 if (dev->power.subsys_data->domain_data) {
1810 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1811 gpd_data->ops = (struct gpd_dev_ops){ NULL };
1813 gpd_data->td = (struct gpd_timing_data){ 0 };
1815 if (--gpd_data->refcount == 0) {
1816 dev->power.subsys_data->domain_data = NULL;
1823 spin_unlock_irq(&dev->power.lock);
1826 pm_runtime_enable(dev);
1831 dev_pm_put_subsys_data(dev);
1833 __pm_genpd_free_dev_data(dev, gpd_data);
1837 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1840 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1841 * @genpd: PM domain to be connected with cpuidle.
1842 * @state: cpuidle state this domain can disable/enable.
1844 * Make a PM domain behave as though it contained a CPU core, that is, instead
1845 * of calling its power down routine it will enable the given cpuidle state so
1846 * that the cpuidle subsystem can power it down (if possible and desirable).
1848 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1850 struct cpuidle_driver *cpuidle_drv;
1851 struct gpd_cpu_data *cpu_data;
1852 struct cpuidle_state *idle_state;
1855 if (IS_ERR_OR_NULL(genpd) || state < 0)
1858 genpd_acquire_lock(genpd);
1860 if (genpd->cpu_data) {
1864 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1869 cpuidle_drv = cpuidle_driver_ref();
1874 if (cpuidle_drv->state_count <= state) {
1878 idle_state = &cpuidle_drv->states[state];
1879 if (!idle_state->disabled) {
1883 cpu_data->idle_state = idle_state;
1884 cpu_data->saved_exit_latency = idle_state->exit_latency;
1885 genpd->cpu_data = cpu_data;
1886 genpd_recalc_cpu_exit_latency(genpd);
1889 genpd_release_lock(genpd);
1893 cpuidle_driver_unref();
1901 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1902 * @name: Name of the domain to connect to cpuidle.
1903 * @state: cpuidle state this domain can manipulate.
1905 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1907 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1911 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1912 * @genpd: PM domain to remove the cpuidle connection from.
1914 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1917 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1919 struct gpd_cpu_data *cpu_data;
1920 struct cpuidle_state *idle_state;
1923 if (IS_ERR_OR_NULL(genpd))
1926 genpd_acquire_lock(genpd);
1928 cpu_data = genpd->cpu_data;
1933 idle_state = cpu_data->idle_state;
1934 if (!idle_state->disabled) {
1938 idle_state->exit_latency = cpu_data->saved_exit_latency;
1939 cpuidle_driver_unref();
1940 genpd->cpu_data = NULL;
1944 genpd_release_lock(genpd);
1949 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1950 * @name: Name of the domain to disconnect cpuidle from.
1952 int pm_genpd_name_detach_cpuidle(const char *name)
1954 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1957 /* Default device callbacks for generic PM domains. */
1960 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1961 * @dev: Device to handle.
1963 static int pm_genpd_default_save_state(struct device *dev)
1965 int (*cb)(struct device *__dev);
1967 cb = dev_gpd_data(dev)->ops.save_state;
1971 if (dev->type && dev->type->pm)
1972 cb = dev->type->pm->runtime_suspend;
1973 else if (dev->class && dev->class->pm)
1974 cb = dev->class->pm->runtime_suspend;
1975 else if (dev->bus && dev->bus->pm)
1976 cb = dev->bus->pm->runtime_suspend;
1980 if (!cb && dev->driver && dev->driver->pm)
1981 cb = dev->driver->pm->runtime_suspend;
1983 return cb ? cb(dev) : 0;
1987 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1988 * @dev: Device to handle.
1990 static int pm_genpd_default_restore_state(struct device *dev)
1992 int (*cb)(struct device *__dev);
1994 cb = dev_gpd_data(dev)->ops.restore_state;
1998 if (dev->type && dev->type->pm)
1999 cb = dev->type->pm->runtime_resume;
2000 else if (dev->class && dev->class->pm)
2001 cb = dev->class->pm->runtime_resume;
2002 else if (dev->bus && dev->bus->pm)
2003 cb = dev->bus->pm->runtime_resume;
2007 if (!cb && dev->driver && dev->driver->pm)
2008 cb = dev->driver->pm->runtime_resume;
2010 return cb ? cb(dev) : 0;
2013 #ifdef CONFIG_PM_SLEEP
2016 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2017 * @dev: Device to handle.
2019 static int pm_genpd_default_suspend(struct device *dev)
2021 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
2023 return cb ? cb(dev) : pm_generic_suspend(dev);
2027 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2028 * @dev: Device to handle.
2030 static int pm_genpd_default_suspend_late(struct device *dev)
2032 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
2034 return cb ? cb(dev) : pm_generic_suspend_late(dev);
2038 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2039 * @dev: Device to handle.
2041 static int pm_genpd_default_resume_early(struct device *dev)
2043 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
2045 return cb ? cb(dev) : pm_generic_resume_early(dev);
2049 * pm_genpd_default_resume - Default "device resume" for PM domians.
2050 * @dev: Device to handle.
2052 static int pm_genpd_default_resume(struct device *dev)
2054 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
2056 return cb ? cb(dev) : pm_generic_resume(dev);
2060 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2061 * @dev: Device to handle.
2063 static int pm_genpd_default_freeze(struct device *dev)
2065 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
2067 return cb ? cb(dev) : pm_generic_freeze(dev);
2071 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2072 * @dev: Device to handle.
2074 static int pm_genpd_default_freeze_late(struct device *dev)
2076 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
2078 return cb ? cb(dev) : pm_generic_freeze_late(dev);
2082 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2083 * @dev: Device to handle.
2085 static int pm_genpd_default_thaw_early(struct device *dev)
2087 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
2089 return cb ? cb(dev) : pm_generic_thaw_early(dev);
2093 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2094 * @dev: Device to handle.
2096 static int pm_genpd_default_thaw(struct device *dev)
2098 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
2100 return cb ? cb(dev) : pm_generic_thaw(dev);
2103 #else /* !CONFIG_PM_SLEEP */
2105 #define pm_genpd_default_suspend NULL
2106 #define pm_genpd_default_suspend_late NULL
2107 #define pm_genpd_default_resume_early NULL
2108 #define pm_genpd_default_resume NULL
2109 #define pm_genpd_default_freeze NULL
2110 #define pm_genpd_default_freeze_late NULL
2111 #define pm_genpd_default_thaw_early NULL
2112 #define pm_genpd_default_thaw NULL
2114 #endif /* !CONFIG_PM_SLEEP */
2117 * pm_genpd_init - Initialize a generic I/O PM domain object.
2118 * @genpd: PM domain object to initialize.
2119 * @gov: PM domain governor to associate with the domain (may be NULL).
2120 * @is_off: Initial value of the domain's power_is_off field.
2122 void pm_genpd_init(struct generic_pm_domain *genpd,
2123 struct dev_power_governor *gov, bool is_off)
2125 if (IS_ERR_OR_NULL(genpd))
2128 INIT_LIST_HEAD(&genpd->master_links);
2129 INIT_LIST_HEAD(&genpd->slave_links);
2130 INIT_LIST_HEAD(&genpd->dev_list);
2131 mutex_init(&genpd->lock);
2133 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2134 genpd->in_progress = 0;
2135 atomic_set(&genpd->sd_count, 0);
2136 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2137 init_waitqueue_head(&genpd->status_wait_queue);
2138 genpd->poweroff_task = NULL;
2139 genpd->resume_count = 0;
2140 genpd->device_count = 0;
2141 genpd->max_off_time_ns = -1;
2142 genpd->max_off_time_changed = true;
2143 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2144 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2145 genpd->domain.ops.prepare = pm_genpd_prepare;
2146 genpd->domain.ops.suspend = pm_genpd_suspend;
2147 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
2148 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2149 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
2150 genpd->domain.ops.resume_early = pm_genpd_resume_early;
2151 genpd->domain.ops.resume = pm_genpd_resume;
2152 genpd->domain.ops.freeze = pm_genpd_freeze;
2153 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
2154 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2155 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
2156 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
2157 genpd->domain.ops.thaw = pm_genpd_thaw;
2158 genpd->domain.ops.poweroff = pm_genpd_suspend;
2159 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
2160 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
2161 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
2162 genpd->domain.ops.restore_early = pm_genpd_resume_early;
2163 genpd->domain.ops.restore = pm_genpd_resume;
2164 genpd->domain.ops.complete = pm_genpd_complete;
2165 genpd->dev_ops.save_state = pm_genpd_default_save_state;
2166 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
2167 genpd->dev_ops.suspend = pm_genpd_default_suspend;
2168 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2169 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2170 genpd->dev_ops.resume = pm_genpd_default_resume;
2171 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2172 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2173 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2174 genpd->dev_ops.thaw = pm_genpd_default_thaw;
2175 mutex_lock(&gpd_list_lock);
2176 list_add(&genpd->gpd_list_node, &gpd_list);
2177 mutex_unlock(&gpd_list_lock);