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);
71 struct generic_pm_domain *dev_to_genpd(struct device *dev)
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
76 return pd_to_genpd(dev->pm_domain);
79 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
81 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
82 stop_latency_ns, "stop");
85 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
87 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
88 start_latency_ns, "start");
91 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
95 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
96 ret = !!atomic_dec_and_test(&genpd->sd_count);
101 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
103 atomic_inc(&genpd->sd_count);
104 smp_mb__after_atomic_inc();
107 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
111 mutex_lock(&genpd->lock);
113 * Wait for the domain to transition into either the active,
114 * or the power off state.
117 prepare_to_wait(&genpd->status_wait_queue, &wait,
118 TASK_UNINTERRUPTIBLE);
119 if (genpd->status == GPD_STATE_ACTIVE
120 || genpd->status == GPD_STATE_POWER_OFF)
122 mutex_unlock(&genpd->lock);
126 mutex_lock(&genpd->lock);
128 finish_wait(&genpd->status_wait_queue, &wait);
131 static void genpd_release_lock(struct generic_pm_domain *genpd)
133 mutex_unlock(&genpd->lock);
136 static void genpd_set_active(struct generic_pm_domain *genpd)
138 if (genpd->resume_count == 0)
139 genpd->status = GPD_STATE_ACTIVE;
142 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
146 if (!genpd->cpu_data)
149 usecs64 = genpd->power_on_latency_ns;
150 do_div(usecs64, NSEC_PER_USEC);
151 usecs64 += genpd->cpu_data->saved_exit_latency;
152 genpd->cpu_data->idle_state->exit_latency = usecs64;
156 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
157 * @genpd: PM domain to power up.
159 * Restore power to @genpd and all of its masters so that it is possible to
160 * resume a device belonging to it.
162 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
163 __releases(&genpd->lock) __acquires(&genpd->lock)
165 struct gpd_link *link;
169 /* If the domain's master is being waited for, we have to wait too. */
171 prepare_to_wait(&genpd->status_wait_queue, &wait,
172 TASK_UNINTERRUPTIBLE);
173 if (genpd->status != GPD_STATE_WAIT_MASTER)
175 mutex_unlock(&genpd->lock);
179 mutex_lock(&genpd->lock);
181 finish_wait(&genpd->status_wait_queue, &wait);
183 if (genpd->status == GPD_STATE_ACTIVE
184 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
187 if (genpd->status != GPD_STATE_POWER_OFF) {
188 genpd_set_active(genpd);
192 if (genpd->cpu_data) {
193 cpuidle_pause_and_lock();
194 genpd->cpu_data->idle_state->disabled = true;
195 cpuidle_resume_and_unlock();
200 * The list is guaranteed not to change while the loop below is being
201 * executed, unless one of the masters' .power_on() callbacks fiddles
204 list_for_each_entry(link, &genpd->slave_links, slave_node) {
205 genpd_sd_counter_inc(link->master);
206 genpd->status = GPD_STATE_WAIT_MASTER;
208 mutex_unlock(&genpd->lock);
210 ret = pm_genpd_poweron(link->master);
212 mutex_lock(&genpd->lock);
215 * The "wait for parent" status is guaranteed not to change
216 * while the master is powering on.
218 genpd->status = GPD_STATE_POWER_OFF;
219 wake_up_all(&genpd->status_wait_queue);
221 genpd_sd_counter_dec(link->master);
226 if (genpd->power_on) {
227 ktime_t time_start = ktime_get();
230 ret = genpd->power_on(genpd);
234 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
235 if (elapsed_ns > genpd->power_on_latency_ns) {
236 genpd->power_on_latency_ns = elapsed_ns;
237 genpd->max_off_time_changed = true;
238 genpd_recalc_cpu_exit_latency(genpd);
240 pr_warning("%s: Power-on latency exceeded, "
241 "new value %lld ns\n", genpd->name,
247 genpd_set_active(genpd);
252 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
253 genpd_sd_counter_dec(link->master);
259 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
260 * @genpd: PM domain to power up.
262 int pm_genpd_poweron(struct generic_pm_domain *genpd)
266 mutex_lock(&genpd->lock);
267 ret = __pm_genpd_poweron(genpd);
268 mutex_unlock(&genpd->lock);
273 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
274 * @domain_name: Name of the PM domain to power up.
276 int pm_genpd_name_poweron(const char *domain_name)
278 struct generic_pm_domain *genpd;
280 genpd = pm_genpd_lookup_name(domain_name);
281 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
284 #endif /* CONFIG_PM */
286 #ifdef CONFIG_PM_RUNTIME
288 static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
291 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
294 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
296 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
297 save_state_latency_ns, "state save");
300 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
302 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
303 restore_state_latency_ns,
307 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
308 unsigned long val, void *ptr)
310 struct generic_pm_domain_data *gpd_data;
313 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
315 mutex_lock(&gpd_data->lock);
316 dev = gpd_data->base.dev;
318 mutex_unlock(&gpd_data->lock);
321 mutex_unlock(&gpd_data->lock);
324 struct generic_pm_domain *genpd;
325 struct pm_domain_data *pdd;
327 spin_lock_irq(&dev->power.lock);
329 pdd = dev->power.subsys_data ?
330 dev->power.subsys_data->domain_data : NULL;
331 if (pdd && pdd->dev) {
332 to_gpd_data(pdd)->td.constraint_changed = true;
333 genpd = dev_to_genpd(dev);
335 genpd = ERR_PTR(-ENODATA);
338 spin_unlock_irq(&dev->power.lock);
340 if (!IS_ERR(genpd)) {
341 mutex_lock(&genpd->lock);
342 genpd->max_off_time_changed = true;
343 mutex_unlock(&genpd->lock);
347 if (!dev || dev->power.ignore_children)
355 * __pm_genpd_save_device - Save the pre-suspend state of a device.
356 * @pdd: Domain data of the device to save the state of.
357 * @genpd: PM domain the device belongs to.
359 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
360 struct generic_pm_domain *genpd)
361 __releases(&genpd->lock) __acquires(&genpd->lock)
363 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
364 struct device *dev = pdd->dev;
367 if (gpd_data->need_restore)
370 mutex_unlock(&genpd->lock);
372 genpd_start_dev(genpd, dev);
373 ret = genpd_save_dev(genpd, dev);
374 genpd_stop_dev(genpd, dev);
376 mutex_lock(&genpd->lock);
379 gpd_data->need_restore = true;
385 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
386 * @pdd: Domain data of the device to restore the state of.
387 * @genpd: PM domain the device belongs to.
389 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
390 struct generic_pm_domain *genpd)
391 __releases(&genpd->lock) __acquires(&genpd->lock)
393 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
394 struct device *dev = pdd->dev;
395 bool need_restore = gpd_data->need_restore;
397 gpd_data->need_restore = false;
398 mutex_unlock(&genpd->lock);
400 genpd_start_dev(genpd, dev);
402 genpd_restore_dev(genpd, dev);
404 mutex_lock(&genpd->lock);
408 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
409 * @genpd: PM domain to check.
411 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
412 * a "power off" operation, which means that a "power on" has occured in the
413 * meantime, or if its resume_count field is different from zero, which means
414 * that one of its devices has been resumed in the meantime.
416 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
418 return genpd->status == GPD_STATE_WAIT_MASTER
419 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
423 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
424 * @genpd: PM domait to power off.
426 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
429 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
431 queue_work(pm_wq, &genpd->power_off_work);
435 * pm_genpd_poweroff - Remove power from a given PM domain.
436 * @genpd: PM domain to power down.
438 * If all of the @genpd's devices have been suspended and all of its subdomains
439 * have been powered down, run the runtime suspend callbacks provided by all of
440 * the @genpd's devices' drivers and remove power from @genpd.
442 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
443 __releases(&genpd->lock) __acquires(&genpd->lock)
445 struct pm_domain_data *pdd;
446 struct gpd_link *link;
447 unsigned int not_suspended;
452 * Do not try to power off the domain in the following situations:
453 * (1) The domain is already in the "power off" state.
454 * (2) The domain is waiting for its master to power up.
455 * (3) One of the domain's devices is being resumed right now.
456 * (4) System suspend is in progress.
458 if (genpd->status == GPD_STATE_POWER_OFF
459 || genpd->status == GPD_STATE_WAIT_MASTER
460 || genpd->resume_count > 0 || genpd->prepared_count > 0)
463 if (atomic_read(&genpd->sd_count) > 0)
467 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
468 enum pm_qos_flags_status stat;
470 stat = dev_pm_qos_flags(pdd->dev,
471 PM_QOS_FLAG_NO_POWER_OFF
472 | PM_QOS_FLAG_REMOTE_WAKEUP);
473 if (stat > PM_QOS_FLAGS_NONE)
476 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
477 || pdd->dev->power.irq_safe))
481 if (not_suspended > genpd->in_progress)
484 if (genpd->poweroff_task) {
486 * Another instance of pm_genpd_poweroff() is executing
487 * callbacks, so tell it to start over and return.
489 genpd->status = GPD_STATE_REPEAT;
493 if (genpd->gov && genpd->gov->power_down_ok) {
494 if (!genpd->gov->power_down_ok(&genpd->domain))
498 genpd->status = GPD_STATE_BUSY;
499 genpd->poweroff_task = current;
501 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
502 ret = atomic_read(&genpd->sd_count) == 0 ?
503 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
505 if (genpd_abort_poweroff(genpd))
509 genpd_set_active(genpd);
513 if (genpd->status == GPD_STATE_REPEAT) {
514 genpd->poweroff_task = NULL;
519 if (genpd->cpu_data) {
521 * If cpu_data is set, cpuidle should turn the domain off when
522 * the CPU in it is idle. In that case we don't decrement the
523 * subdomain counts of the master domains, so that power is not
524 * removed from the current domain prematurely as a result of
525 * cutting off the masters' power.
527 genpd->status = GPD_STATE_POWER_OFF;
528 cpuidle_pause_and_lock();
529 genpd->cpu_data->idle_state->disabled = false;
530 cpuidle_resume_and_unlock();
534 if (genpd->power_off) {
538 if (atomic_read(&genpd->sd_count) > 0) {
543 time_start = ktime_get();
546 * If sd_count > 0 at this point, one of the subdomains hasn't
547 * managed to call pm_genpd_poweron() for the master yet after
548 * incrementing it. In that case pm_genpd_poweron() will wait
549 * for us to drop the lock, so we can call .power_off() and let
550 * the pm_genpd_poweron() restore power for us (this shouldn't
551 * happen very often).
553 ret = genpd->power_off(genpd);
555 genpd_set_active(genpd);
559 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
560 if (elapsed_ns > genpd->power_off_latency_ns) {
561 genpd->power_off_latency_ns = elapsed_ns;
562 genpd->max_off_time_changed = true;
564 pr_warning("%s: Power-off latency exceeded, "
565 "new value %lld ns\n", genpd->name,
570 genpd->status = GPD_STATE_POWER_OFF;
572 list_for_each_entry(link, &genpd->slave_links, slave_node) {
573 genpd_sd_counter_dec(link->master);
574 genpd_queue_power_off_work(link->master);
578 genpd->poweroff_task = NULL;
579 wake_up_all(&genpd->status_wait_queue);
584 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
585 * @work: Work structure used for scheduling the execution of this function.
587 static void genpd_power_off_work_fn(struct work_struct *work)
589 struct generic_pm_domain *genpd;
591 genpd = container_of(work, struct generic_pm_domain, power_off_work);
593 genpd_acquire_lock(genpd);
594 pm_genpd_poweroff(genpd);
595 genpd_release_lock(genpd);
599 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
600 * @dev: Device to suspend.
602 * Carry out a runtime suspend of a device under the assumption that its
603 * pm_domain field points to the domain member of an object of type
604 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
606 static int pm_genpd_runtime_suspend(struct device *dev)
608 struct generic_pm_domain *genpd;
609 bool (*stop_ok)(struct device *__dev);
612 dev_dbg(dev, "%s()\n", __func__);
614 genpd = dev_to_genpd(dev);
618 might_sleep_if(!genpd->dev_irq_safe);
620 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
621 if (stop_ok && !stop_ok(dev))
624 ret = genpd_stop_dev(genpd, dev);
629 * If power.irq_safe is set, this routine will be run with interrupts
630 * off, so it can't use mutexes.
632 if (dev->power.irq_safe)
635 mutex_lock(&genpd->lock);
636 genpd->in_progress++;
637 pm_genpd_poweroff(genpd);
638 genpd->in_progress--;
639 mutex_unlock(&genpd->lock);
645 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
646 * @dev: Device to resume.
648 * Carry out a runtime resume of a device under the assumption that its
649 * pm_domain field points to the domain member of an object of type
650 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
652 static int pm_genpd_runtime_resume(struct device *dev)
654 struct generic_pm_domain *genpd;
658 dev_dbg(dev, "%s()\n", __func__);
660 genpd = dev_to_genpd(dev);
664 might_sleep_if(!genpd->dev_irq_safe);
666 /* If power.irq_safe, the PM domain is never powered off. */
667 if (dev->power.irq_safe)
668 return genpd_start_dev_no_timing(genpd, dev);
670 mutex_lock(&genpd->lock);
671 ret = __pm_genpd_poweron(genpd);
673 mutex_unlock(&genpd->lock);
676 genpd->status = GPD_STATE_BUSY;
677 genpd->resume_count++;
679 prepare_to_wait(&genpd->status_wait_queue, &wait,
680 TASK_UNINTERRUPTIBLE);
682 * If current is the powering off task, we have been called
683 * reentrantly from one of the device callbacks, so we should
686 if (!genpd->poweroff_task || genpd->poweroff_task == current)
688 mutex_unlock(&genpd->lock);
692 mutex_lock(&genpd->lock);
694 finish_wait(&genpd->status_wait_queue, &wait);
695 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
696 genpd->resume_count--;
697 genpd_set_active(genpd);
698 wake_up_all(&genpd->status_wait_queue);
699 mutex_unlock(&genpd->lock);
704 static bool pd_ignore_unused;
705 static int __init pd_ignore_unused_setup(char *__unused)
707 pd_ignore_unused = true;
710 __setup("pd_ignore_unused", pd_ignore_unused_setup);
713 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
715 void pm_genpd_poweroff_unused(void)
717 struct generic_pm_domain *genpd;
719 if (pd_ignore_unused) {
720 pr_warn("genpd: Not disabling unused power domains\n");
724 mutex_lock(&gpd_list_lock);
726 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
727 genpd_queue_power_off_work(genpd);
729 mutex_unlock(&gpd_list_lock);
734 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
735 unsigned long val, void *ptr)
740 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
742 #define pm_genpd_runtime_suspend NULL
743 #define pm_genpd_runtime_resume NULL
745 #endif /* CONFIG_PM_RUNTIME */
747 #ifdef CONFIG_PM_SLEEP
750 * pm_genpd_present - Check if the given PM domain has been initialized.
751 * @genpd: PM domain to check.
753 static bool pm_genpd_present(struct generic_pm_domain *genpd)
755 struct generic_pm_domain *gpd;
757 if (IS_ERR_OR_NULL(genpd))
760 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
767 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
770 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
773 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
775 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
778 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
780 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
783 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
785 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
788 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
790 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
793 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
795 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
798 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
800 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
803 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
805 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
808 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
810 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
814 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
815 * @genpd: PM domain to power off, if possible.
817 * Check if the given PM domain can be powered off (during system suspend or
818 * hibernation) and do that if so. Also, in that case propagate to its masters.
820 * This function is only called in "noirq" and "syscore" stages of system power
821 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
822 * executed sequentially, so it is guaranteed that it will never run twice in
825 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
827 struct gpd_link *link;
829 if (genpd->status == GPD_STATE_POWER_OFF)
832 if (genpd->suspended_count != genpd->device_count
833 || atomic_read(&genpd->sd_count) > 0)
836 if (genpd->power_off)
837 genpd->power_off(genpd);
839 genpd->status = GPD_STATE_POWER_OFF;
841 list_for_each_entry(link, &genpd->slave_links, slave_node) {
842 genpd_sd_counter_dec(link->master);
843 pm_genpd_sync_poweroff(link->master);
848 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
849 * @genpd: PM domain to power on.
851 * This function is only called in "noirq" and "syscore" stages of system power
852 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
853 * executed sequentially, so it is guaranteed that it will never run twice in
856 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
858 struct gpd_link *link;
860 if (genpd->status != GPD_STATE_POWER_OFF)
863 list_for_each_entry(link, &genpd->slave_links, slave_node) {
864 pm_genpd_sync_poweron(link->master);
865 genpd_sd_counter_inc(link->master);
869 genpd->power_on(genpd);
871 genpd->status = GPD_STATE_ACTIVE;
875 * resume_needed - Check whether to resume a device before system suspend.
876 * @dev: Device to check.
877 * @genpd: PM domain the device belongs to.
879 * There are two cases in which a device that can wake up the system from sleep
880 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
881 * to wake up the system and it has to remain active for this purpose while the
882 * system is in the sleep state and (2) if the device is not enabled to wake up
883 * the system from sleep states and it generally doesn't generate wakeup signals
884 * by itself (those signals are generated on its behalf by other parts of the
885 * system). In the latter case it may be necessary to reconfigure the device's
886 * wakeup settings during system suspend, because it may have been set up to
887 * signal remote wakeup from the system's working state as needed by runtime PM.
888 * Return 'true' in either of the above cases.
890 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
894 if (!device_can_wakeup(dev))
897 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
898 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
902 * pm_genpd_prepare - Start power transition of a device in a PM domain.
903 * @dev: Device to start the transition of.
905 * Start a power transition of a device (during a system-wide power transition)
906 * under the assumption that its pm_domain field points to the domain member of
907 * an object of type struct generic_pm_domain representing a PM domain
908 * consisting of I/O devices.
910 static int pm_genpd_prepare(struct device *dev)
912 struct generic_pm_domain *genpd;
915 dev_dbg(dev, "%s()\n", __func__);
917 genpd = dev_to_genpd(dev);
922 * If a wakeup request is pending for the device, it should be woken up
923 * at this point and a system wakeup event should be reported if it's
924 * set up to wake up the system from sleep states.
926 pm_runtime_get_noresume(dev);
927 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
928 pm_wakeup_event(dev, 0);
930 if (pm_wakeup_pending()) {
935 if (resume_needed(dev, genpd))
936 pm_runtime_resume(dev);
938 genpd_acquire_lock(genpd);
940 if (genpd->prepared_count++ == 0) {
941 genpd->suspended_count = 0;
942 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
945 genpd_release_lock(genpd);
947 if (genpd->suspend_power_off) {
948 pm_runtime_put_noidle(dev);
953 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
954 * so pm_genpd_poweron() will return immediately, but if the device
955 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
956 * to make it operational.
958 pm_runtime_resume(dev);
959 __pm_runtime_disable(dev, false);
961 ret = pm_generic_prepare(dev);
963 mutex_lock(&genpd->lock);
965 if (--genpd->prepared_count == 0)
966 genpd->suspend_power_off = false;
968 mutex_unlock(&genpd->lock);
969 pm_runtime_enable(dev);
977 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
978 * @dev: Device to suspend.
980 * Suspend a device under the assumption that its pm_domain field points to the
981 * domain member of an object of type struct generic_pm_domain representing
982 * a PM domain consisting of I/O devices.
984 static int pm_genpd_suspend(struct device *dev)
986 struct generic_pm_domain *genpd;
988 dev_dbg(dev, "%s()\n", __func__);
990 genpd = dev_to_genpd(dev);
994 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
998 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
999 * @dev: Device to suspend.
1001 * Carry out a late suspend of a device under the assumption that its
1002 * pm_domain field points to the domain member of an object of type
1003 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1005 static int pm_genpd_suspend_late(struct device *dev)
1007 struct generic_pm_domain *genpd;
1009 dev_dbg(dev, "%s()\n", __func__);
1011 genpd = dev_to_genpd(dev);
1015 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1019 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1020 * @dev: Device to suspend.
1022 * Stop the device and remove power from the domain if all devices in it have
1025 static int pm_genpd_suspend_noirq(struct device *dev)
1027 struct generic_pm_domain *genpd;
1029 dev_dbg(dev, "%s()\n", __func__);
1031 genpd = dev_to_genpd(dev);
1035 if (genpd->suspend_power_off
1036 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1039 genpd_stop_dev(genpd, dev);
1042 * Since all of the "noirq" callbacks are executed sequentially, it is
1043 * guaranteed that this function will never run twice in parallel for
1044 * the same PM domain, so it is not necessary to use locking here.
1046 genpd->suspended_count++;
1047 pm_genpd_sync_poweroff(genpd);
1053 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1054 * @dev: Device to resume.
1056 * Restore power to the device's PM domain, if necessary, and start the device.
1058 static int pm_genpd_resume_noirq(struct device *dev)
1060 struct generic_pm_domain *genpd;
1062 dev_dbg(dev, "%s()\n", __func__);
1064 genpd = dev_to_genpd(dev);
1068 if (genpd->suspend_power_off
1069 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1073 * Since all of the "noirq" callbacks are executed sequentially, it is
1074 * guaranteed that this function will never run twice in parallel for
1075 * the same PM domain, so it is not necessary to use locking here.
1077 pm_genpd_sync_poweron(genpd);
1078 genpd->suspended_count--;
1080 return genpd_start_dev(genpd, dev);
1084 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1085 * @dev: Device to resume.
1087 * Carry out an early resume of a device under the assumption that its
1088 * pm_domain field points to the domain member of an object of type
1089 * struct generic_pm_domain representing a power domain consisting of I/O
1092 static int pm_genpd_resume_early(struct device *dev)
1094 struct generic_pm_domain *genpd;
1096 dev_dbg(dev, "%s()\n", __func__);
1098 genpd = dev_to_genpd(dev);
1102 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1106 * pm_genpd_resume - Resume of device in an I/O PM domain.
1107 * @dev: Device to resume.
1109 * Resume a device under the assumption that its pm_domain field points to the
1110 * domain member of an object of type struct generic_pm_domain representing
1111 * a power domain consisting of I/O devices.
1113 static int pm_genpd_resume(struct device *dev)
1115 struct generic_pm_domain *genpd;
1117 dev_dbg(dev, "%s()\n", __func__);
1119 genpd = dev_to_genpd(dev);
1123 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1127 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1128 * @dev: Device to freeze.
1130 * Freeze a device under the assumption that its pm_domain field points to the
1131 * domain member of an object of type struct generic_pm_domain representing
1132 * a power domain consisting of I/O devices.
1134 static int pm_genpd_freeze(struct device *dev)
1136 struct generic_pm_domain *genpd;
1138 dev_dbg(dev, "%s()\n", __func__);
1140 genpd = dev_to_genpd(dev);
1144 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1148 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1149 * @dev: Device to freeze.
1151 * Carry out a late freeze of a device under the assumption that its
1152 * pm_domain field points to the domain member of an object of type
1153 * struct generic_pm_domain representing a power domain consisting of I/O
1156 static int pm_genpd_freeze_late(struct device *dev)
1158 struct generic_pm_domain *genpd;
1160 dev_dbg(dev, "%s()\n", __func__);
1162 genpd = dev_to_genpd(dev);
1166 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1170 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1171 * @dev: Device to freeze.
1173 * Carry out a late freeze of a device under the assumption that its
1174 * pm_domain field points to the domain member of an object of type
1175 * struct generic_pm_domain representing a power domain consisting of I/O
1178 static int pm_genpd_freeze_noirq(struct device *dev)
1180 struct generic_pm_domain *genpd;
1182 dev_dbg(dev, "%s()\n", __func__);
1184 genpd = dev_to_genpd(dev);
1188 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1192 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1193 * @dev: Device to thaw.
1195 * Start the device, unless power has been removed from the domain already
1196 * before the system transition.
1198 static int pm_genpd_thaw_noirq(struct device *dev)
1200 struct generic_pm_domain *genpd;
1202 dev_dbg(dev, "%s()\n", __func__);
1204 genpd = dev_to_genpd(dev);
1208 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1212 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1213 * @dev: Device to thaw.
1215 * Carry out an early thaw of a device under the assumption that its
1216 * pm_domain field points to the domain member of an object of type
1217 * struct generic_pm_domain representing a power domain consisting of I/O
1220 static int pm_genpd_thaw_early(struct device *dev)
1222 struct generic_pm_domain *genpd;
1224 dev_dbg(dev, "%s()\n", __func__);
1226 genpd = dev_to_genpd(dev);
1230 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1234 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1235 * @dev: Device to thaw.
1237 * Thaw a device under the assumption that its pm_domain field points to the
1238 * domain member of an object of type struct generic_pm_domain representing
1239 * a power domain consisting of I/O devices.
1241 static int pm_genpd_thaw(struct device *dev)
1243 struct generic_pm_domain *genpd;
1245 dev_dbg(dev, "%s()\n", __func__);
1247 genpd = dev_to_genpd(dev);
1251 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1255 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1256 * @dev: Device to resume.
1258 * Make sure the domain will be in the same power state as before the
1259 * hibernation the system is resuming from and start the device if necessary.
1261 static int pm_genpd_restore_noirq(struct device *dev)
1263 struct generic_pm_domain *genpd;
1265 dev_dbg(dev, "%s()\n", __func__);
1267 genpd = dev_to_genpd(dev);
1272 * Since all of the "noirq" callbacks are executed sequentially, it is
1273 * guaranteed that this function will never run twice in parallel for
1274 * the same PM domain, so it is not necessary to use locking here.
1276 * At this point suspended_count == 0 means we are being run for the
1277 * first time for the given domain in the present cycle.
1279 if (genpd->suspended_count++ == 0) {
1281 * The boot kernel might put the domain into arbitrary state,
1282 * so make it appear as powered off to pm_genpd_sync_poweron(),
1283 * so that it tries to power it on in case it was really off.
1285 genpd->status = GPD_STATE_POWER_OFF;
1286 if (genpd->suspend_power_off) {
1288 * If the domain was off before the hibernation, make
1289 * sure it will be off going forward.
1291 if (genpd->power_off)
1292 genpd->power_off(genpd);
1298 if (genpd->suspend_power_off)
1301 pm_genpd_sync_poweron(genpd);
1303 return genpd_start_dev(genpd, dev);
1307 * pm_genpd_complete - Complete power transition of a device in a power domain.
1308 * @dev: Device to complete the transition of.
1310 * Complete a power transition of a device (during a system-wide power
1311 * transition) under the assumption that its pm_domain field points to the
1312 * domain member of an object of type struct generic_pm_domain representing
1313 * a power domain consisting of I/O devices.
1315 static void pm_genpd_complete(struct device *dev)
1317 struct generic_pm_domain *genpd;
1320 dev_dbg(dev, "%s()\n", __func__);
1322 genpd = dev_to_genpd(dev);
1326 mutex_lock(&genpd->lock);
1328 run_complete = !genpd->suspend_power_off;
1329 if (--genpd->prepared_count == 0)
1330 genpd->suspend_power_off = false;
1332 mutex_unlock(&genpd->lock);
1335 pm_generic_complete(dev);
1336 pm_runtime_set_active(dev);
1337 pm_runtime_enable(dev);
1338 pm_request_idle(dev);
1343 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1344 * @dev: Device that normally is marked as "always on" to switch power for.
1346 * This routine may only be called during the system core (syscore) suspend or
1347 * resume phase for devices whose "always on" flags are set.
1349 void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1351 struct generic_pm_domain *genpd;
1353 genpd = dev_to_genpd(dev);
1354 if (!pm_genpd_present(genpd))
1358 genpd->suspended_count++;
1359 pm_genpd_sync_poweroff(genpd);
1361 pm_genpd_sync_poweron(genpd);
1362 genpd->suspended_count--;
1365 EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1369 #define pm_genpd_prepare NULL
1370 #define pm_genpd_suspend NULL
1371 #define pm_genpd_suspend_late NULL
1372 #define pm_genpd_suspend_noirq NULL
1373 #define pm_genpd_resume_early NULL
1374 #define pm_genpd_resume_noirq NULL
1375 #define pm_genpd_resume NULL
1376 #define pm_genpd_freeze NULL
1377 #define pm_genpd_freeze_late NULL
1378 #define pm_genpd_freeze_noirq NULL
1379 #define pm_genpd_thaw_early NULL
1380 #define pm_genpd_thaw_noirq NULL
1381 #define pm_genpd_thaw NULL
1382 #define pm_genpd_restore_noirq NULL
1383 #define pm_genpd_complete NULL
1385 #endif /* CONFIG_PM_SLEEP */
1387 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1389 struct generic_pm_domain_data *gpd_data;
1391 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1395 mutex_init(&gpd_data->lock);
1396 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1397 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1401 static void __pm_genpd_free_dev_data(struct device *dev,
1402 struct generic_pm_domain_data *gpd_data)
1404 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1409 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1410 * @genpd: PM domain to add the device to.
1411 * @dev: Device to be added.
1412 * @td: Set of PM QoS timing parameters to attach to the device.
1414 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1415 struct gpd_timing_data *td)
1417 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1418 struct pm_domain_data *pdd;
1421 dev_dbg(dev, "%s()\n", __func__);
1423 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1426 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1430 genpd_acquire_lock(genpd);
1432 if (genpd->prepared_count > 0) {
1437 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1438 if (pdd->dev == dev) {
1443 ret = dev_pm_get_subsys_data(dev);
1447 genpd->device_count++;
1448 genpd->max_off_time_changed = true;
1450 spin_lock_irq(&dev->power.lock);
1452 dev->pm_domain = &genpd->domain;
1453 if (dev->power.subsys_data->domain_data) {
1454 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1456 gpd_data = gpd_data_new;
1457 dev->power.subsys_data->domain_data = &gpd_data->base;
1459 gpd_data->refcount++;
1463 spin_unlock_irq(&dev->power.lock);
1465 mutex_lock(&gpd_data->lock);
1466 gpd_data->base.dev = dev;
1467 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1468 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1469 gpd_data->td.constraint_changed = true;
1470 gpd_data->td.effective_constraint_ns = -1;
1471 mutex_unlock(&gpd_data->lock);
1474 genpd_release_lock(genpd);
1476 if (gpd_data != gpd_data_new)
1477 __pm_genpd_free_dev_data(dev, gpd_data_new);
1483 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1484 * @genpd_node: Device tree node pointer representing a PM domain to which the
1485 * the device is added to.
1486 * @dev: Device to be added.
1487 * @td: Set of PM QoS timing parameters to attach to the device.
1489 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1490 struct gpd_timing_data *td)
1492 struct generic_pm_domain *genpd = NULL, *gpd;
1494 dev_dbg(dev, "%s()\n", __func__);
1496 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1499 mutex_lock(&gpd_list_lock);
1500 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1501 if (gpd->of_node == genpd_node) {
1506 mutex_unlock(&gpd_list_lock);
1511 return __pm_genpd_add_device(genpd, dev, td);
1516 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1517 * @domain_name: Name of the PM domain to add the device to.
1518 * @dev: Device to be added.
1519 * @td: Set of PM QoS timing parameters to attach to the device.
1521 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1522 struct gpd_timing_data *td)
1524 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1528 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1529 * @genpd: PM domain to remove the device from.
1530 * @dev: Device to be removed.
1532 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1535 struct generic_pm_domain_data *gpd_data;
1536 struct pm_domain_data *pdd;
1537 bool remove = false;
1540 dev_dbg(dev, "%s()\n", __func__);
1542 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1543 || IS_ERR_OR_NULL(dev->pm_domain)
1544 || pd_to_genpd(dev->pm_domain) != genpd)
1547 genpd_acquire_lock(genpd);
1549 if (genpd->prepared_count > 0) {
1554 genpd->device_count--;
1555 genpd->max_off_time_changed = true;
1557 spin_lock_irq(&dev->power.lock);
1559 dev->pm_domain = NULL;
1560 pdd = dev->power.subsys_data->domain_data;
1561 list_del_init(&pdd->list_node);
1562 gpd_data = to_gpd_data(pdd);
1563 if (--gpd_data->refcount == 0) {
1564 dev->power.subsys_data->domain_data = NULL;
1568 spin_unlock_irq(&dev->power.lock);
1570 mutex_lock(&gpd_data->lock);
1572 mutex_unlock(&gpd_data->lock);
1574 genpd_release_lock(genpd);
1576 dev_pm_put_subsys_data(dev);
1578 __pm_genpd_free_dev_data(dev, gpd_data);
1583 genpd_release_lock(genpd);
1589 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1590 * @dev: Device to set/unset the flag for.
1591 * @val: The new value of the device's "need restore" flag.
1593 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1595 struct pm_subsys_data *psd;
1596 unsigned long flags;
1598 spin_lock_irqsave(&dev->power.lock, flags);
1600 psd = dev_to_psd(dev);
1601 if (psd && psd->domain_data)
1602 to_gpd_data(psd->domain_data)->need_restore = val;
1604 spin_unlock_irqrestore(&dev->power.lock, flags);
1606 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1609 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1610 * @genpd: Master PM domain to add the subdomain to.
1611 * @subdomain: Subdomain to be added.
1613 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1614 struct generic_pm_domain *subdomain)
1616 struct gpd_link *link;
1619 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1620 || genpd == subdomain)
1624 genpd_acquire_lock(genpd);
1625 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1627 if (subdomain->status != GPD_STATE_POWER_OFF
1628 && subdomain->status != GPD_STATE_ACTIVE) {
1629 mutex_unlock(&subdomain->lock);
1630 genpd_release_lock(genpd);
1634 if (genpd->status == GPD_STATE_POWER_OFF
1635 && subdomain->status != GPD_STATE_POWER_OFF) {
1640 list_for_each_entry(link, &genpd->master_links, master_node) {
1641 if (link->slave == subdomain && link->master == genpd) {
1647 link = kzalloc(sizeof(*link), GFP_KERNEL);
1652 link->master = genpd;
1653 list_add_tail(&link->master_node, &genpd->master_links);
1654 link->slave = subdomain;
1655 list_add_tail(&link->slave_node, &subdomain->slave_links);
1656 if (subdomain->status != GPD_STATE_POWER_OFF)
1657 genpd_sd_counter_inc(genpd);
1660 mutex_unlock(&subdomain->lock);
1661 genpd_release_lock(genpd);
1667 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1668 * @master_name: Name of the master PM domain to add the subdomain to.
1669 * @subdomain_name: Name of the subdomain to be added.
1671 int pm_genpd_add_subdomain_names(const char *master_name,
1672 const char *subdomain_name)
1674 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1676 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1679 mutex_lock(&gpd_list_lock);
1680 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1681 if (!master && !strcmp(gpd->name, master_name))
1684 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1687 if (master && subdomain)
1690 mutex_unlock(&gpd_list_lock);
1692 return pm_genpd_add_subdomain(master, subdomain);
1696 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1697 * @genpd: Master PM domain to remove the subdomain from.
1698 * @subdomain: Subdomain to be removed.
1700 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1701 struct generic_pm_domain *subdomain)
1703 struct gpd_link *link;
1706 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1710 genpd_acquire_lock(genpd);
1712 list_for_each_entry(link, &genpd->master_links, master_node) {
1713 if (link->slave != subdomain)
1716 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1718 if (subdomain->status != GPD_STATE_POWER_OFF
1719 && subdomain->status != GPD_STATE_ACTIVE) {
1720 mutex_unlock(&subdomain->lock);
1721 genpd_release_lock(genpd);
1725 list_del(&link->master_node);
1726 list_del(&link->slave_node);
1728 if (subdomain->status != GPD_STATE_POWER_OFF)
1729 genpd_sd_counter_dec(genpd);
1731 mutex_unlock(&subdomain->lock);
1737 genpd_release_lock(genpd);
1743 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1744 * @genpd: PM domain to be connected with cpuidle.
1745 * @state: cpuidle state this domain can disable/enable.
1747 * Make a PM domain behave as though it contained a CPU core, that is, instead
1748 * of calling its power down routine it will enable the given cpuidle state so
1749 * that the cpuidle subsystem can power it down (if possible and desirable).
1751 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1753 struct cpuidle_driver *cpuidle_drv;
1754 struct gpd_cpu_data *cpu_data;
1755 struct cpuidle_state *idle_state;
1758 if (IS_ERR_OR_NULL(genpd) || state < 0)
1761 genpd_acquire_lock(genpd);
1763 if (genpd->cpu_data) {
1767 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1772 cpuidle_drv = cpuidle_driver_ref();
1777 if (cpuidle_drv->state_count <= state) {
1781 idle_state = &cpuidle_drv->states[state];
1782 if (!idle_state->disabled) {
1786 cpu_data->idle_state = idle_state;
1787 cpu_data->saved_exit_latency = idle_state->exit_latency;
1788 genpd->cpu_data = cpu_data;
1789 genpd_recalc_cpu_exit_latency(genpd);
1792 genpd_release_lock(genpd);
1796 cpuidle_driver_unref();
1804 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1805 * @name: Name of the domain to connect to cpuidle.
1806 * @state: cpuidle state this domain can manipulate.
1808 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1810 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1814 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1815 * @genpd: PM domain to remove the cpuidle connection from.
1817 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1820 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1822 struct gpd_cpu_data *cpu_data;
1823 struct cpuidle_state *idle_state;
1826 if (IS_ERR_OR_NULL(genpd))
1829 genpd_acquire_lock(genpd);
1831 cpu_data = genpd->cpu_data;
1836 idle_state = cpu_data->idle_state;
1837 if (!idle_state->disabled) {
1841 idle_state->exit_latency = cpu_data->saved_exit_latency;
1842 cpuidle_driver_unref();
1843 genpd->cpu_data = NULL;
1847 genpd_release_lock(genpd);
1852 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1853 * @name: Name of the domain to disconnect cpuidle from.
1855 int pm_genpd_name_detach_cpuidle(const char *name)
1857 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1860 /* Default device callbacks for generic PM domains. */
1863 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1864 * @dev: Device to handle.
1866 static int pm_genpd_default_save_state(struct device *dev)
1868 int (*cb)(struct device *__dev);
1870 if (dev->type && dev->type->pm)
1871 cb = dev->type->pm->runtime_suspend;
1872 else if (dev->class && dev->class->pm)
1873 cb = dev->class->pm->runtime_suspend;
1874 else if (dev->bus && dev->bus->pm)
1875 cb = dev->bus->pm->runtime_suspend;
1879 if (!cb && dev->driver && dev->driver->pm)
1880 cb = dev->driver->pm->runtime_suspend;
1882 return cb ? cb(dev) : 0;
1886 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1887 * @dev: Device to handle.
1889 static int pm_genpd_default_restore_state(struct device *dev)
1891 int (*cb)(struct device *__dev);
1893 if (dev->type && dev->type->pm)
1894 cb = dev->type->pm->runtime_resume;
1895 else if (dev->class && dev->class->pm)
1896 cb = dev->class->pm->runtime_resume;
1897 else if (dev->bus && dev->bus->pm)
1898 cb = dev->bus->pm->runtime_resume;
1902 if (!cb && dev->driver && dev->driver->pm)
1903 cb = dev->driver->pm->runtime_resume;
1905 return cb ? cb(dev) : 0;
1909 * pm_genpd_init - Initialize a generic I/O PM domain object.
1910 * @genpd: PM domain object to initialize.
1911 * @gov: PM domain governor to associate with the domain (may be NULL).
1912 * @is_off: Initial value of the domain's power_is_off field.
1914 void pm_genpd_init(struct generic_pm_domain *genpd,
1915 struct dev_power_governor *gov, bool is_off)
1917 if (IS_ERR_OR_NULL(genpd))
1920 INIT_LIST_HEAD(&genpd->master_links);
1921 INIT_LIST_HEAD(&genpd->slave_links);
1922 INIT_LIST_HEAD(&genpd->dev_list);
1923 mutex_init(&genpd->lock);
1925 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1926 genpd->in_progress = 0;
1927 atomic_set(&genpd->sd_count, 0);
1928 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1929 init_waitqueue_head(&genpd->status_wait_queue);
1930 genpd->poweroff_task = NULL;
1931 genpd->resume_count = 0;
1932 genpd->device_count = 0;
1933 genpd->max_off_time_ns = -1;
1934 genpd->max_off_time_changed = true;
1935 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1936 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1937 genpd->domain.ops.prepare = pm_genpd_prepare;
1938 genpd->domain.ops.suspend = pm_genpd_suspend;
1939 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1940 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1941 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1942 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1943 genpd->domain.ops.resume = pm_genpd_resume;
1944 genpd->domain.ops.freeze = pm_genpd_freeze;
1945 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1946 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1947 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1948 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1949 genpd->domain.ops.thaw = pm_genpd_thaw;
1950 genpd->domain.ops.poweroff = pm_genpd_suspend;
1951 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1952 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1953 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1954 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1955 genpd->domain.ops.restore = pm_genpd_resume;
1956 genpd->domain.ops.complete = pm_genpd_complete;
1957 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1958 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1959 genpd->dev_ops.suspend = pm_generic_suspend;
1960 genpd->dev_ops.suspend_late = pm_generic_suspend_late;
1961 genpd->dev_ops.resume_early = pm_generic_resume_early;
1962 genpd->dev_ops.resume = pm_generic_resume;
1963 genpd->dev_ops.freeze = pm_generic_freeze;
1964 genpd->dev_ops.freeze_late = pm_generic_freeze_late;
1965 genpd->dev_ops.thaw_early = pm_generic_thaw_early;
1966 genpd->dev_ops.thaw = pm_generic_thaw;
1967 mutex_lock(&gpd_list_lock);
1968 list_add(&genpd->gpd_list_node, &gpd_list);
1969 mutex_unlock(&gpd_list_lock);