2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_domain.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
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 generic_pm_domain_data *__gpd_data = dev_gpd_data(dev); \
42 if (__elapsed > __gpd_data->td.field) { \
43 __gpd_data->td.field = __elapsed; \
44 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
50 static LIST_HEAD(gpd_list);
51 static DEFINE_MUTEX(gpd_list_lock);
55 struct generic_pm_domain *dev_to_genpd(struct device *dev)
57 if (IS_ERR_OR_NULL(dev->pm_domain))
58 return ERR_PTR(-EINVAL);
60 return pd_to_genpd(dev->pm_domain);
63 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
65 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
66 stop_latency_ns, "stop");
69 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
71 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
72 start_latency_ns, "start");
75 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
77 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
78 save_state_latency_ns, "state save");
81 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
83 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
84 restore_state_latency_ns,
88 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
92 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
93 ret = !!atomic_dec_and_test(&genpd->sd_count);
98 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100 atomic_inc(&genpd->sd_count);
101 smp_mb__after_atomic_inc();
104 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
108 mutex_lock(&genpd->lock);
110 * Wait for the domain to transition into either the active,
111 * or the power off state.
114 prepare_to_wait(&genpd->status_wait_queue, &wait,
115 TASK_UNINTERRUPTIBLE);
116 if (genpd->status == GPD_STATE_ACTIVE
117 || genpd->status == GPD_STATE_POWER_OFF)
119 mutex_unlock(&genpd->lock);
123 mutex_lock(&genpd->lock);
125 finish_wait(&genpd->status_wait_queue, &wait);
128 static void genpd_release_lock(struct generic_pm_domain *genpd)
130 mutex_unlock(&genpd->lock);
133 static void genpd_set_active(struct generic_pm_domain *genpd)
135 if (genpd->resume_count == 0)
136 genpd->status = GPD_STATE_ACTIVE;
140 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
141 * @genpd: PM domain to power up.
143 * Restore power to @genpd and all of its masters so that it is possible to
144 * resume a device belonging to it.
146 int __pm_genpd_poweron(struct generic_pm_domain *genpd)
147 __releases(&genpd->lock) __acquires(&genpd->lock)
149 struct gpd_link *link;
153 /* If the domain's master is being waited for, we have to wait too. */
155 prepare_to_wait(&genpd->status_wait_queue, &wait,
156 TASK_UNINTERRUPTIBLE);
157 if (genpd->status != GPD_STATE_WAIT_MASTER)
159 mutex_unlock(&genpd->lock);
163 mutex_lock(&genpd->lock);
165 finish_wait(&genpd->status_wait_queue, &wait);
167 if (genpd->status == GPD_STATE_ACTIVE
168 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
171 if (genpd->status != GPD_STATE_POWER_OFF) {
172 genpd_set_active(genpd);
177 * The list is guaranteed not to change while the loop below is being
178 * executed, unless one of the masters' .power_on() callbacks fiddles
181 list_for_each_entry(link, &genpd->slave_links, slave_node) {
182 genpd_sd_counter_inc(link->master);
183 genpd->status = GPD_STATE_WAIT_MASTER;
185 mutex_unlock(&genpd->lock);
187 ret = pm_genpd_poweron(link->master);
189 mutex_lock(&genpd->lock);
192 * The "wait for parent" status is guaranteed not to change
193 * while the master is powering on.
195 genpd->status = GPD_STATE_POWER_OFF;
196 wake_up_all(&genpd->status_wait_queue);
198 genpd_sd_counter_dec(link->master);
203 if (genpd->power_on) {
204 ktime_t time_start = ktime_get();
207 ret = genpd->power_on(genpd);
211 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
212 if (elapsed_ns > genpd->power_on_latency_ns) {
213 genpd->power_on_latency_ns = elapsed_ns;
215 pr_warning("%s: Power-on latency exceeded, "
216 "new value %lld ns\n", genpd->name,
221 genpd_set_active(genpd);
226 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
227 genpd_sd_counter_dec(link->master);
233 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
234 * @genpd: PM domain to power up.
236 int pm_genpd_poweron(struct generic_pm_domain *genpd)
240 mutex_lock(&genpd->lock);
241 ret = __pm_genpd_poweron(genpd);
242 mutex_unlock(&genpd->lock);
246 #endif /* CONFIG_PM */
248 #ifdef CONFIG_PM_RUNTIME
251 * __pm_genpd_save_device - Save the pre-suspend state of a device.
252 * @pdd: Domain data of the device to save the state of.
253 * @genpd: PM domain the device belongs to.
255 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
256 struct generic_pm_domain *genpd)
257 __releases(&genpd->lock) __acquires(&genpd->lock)
259 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
260 struct device *dev = pdd->dev;
263 if (gpd_data->need_restore)
266 mutex_unlock(&genpd->lock);
268 genpd_start_dev(genpd, dev);
269 ret = genpd_save_dev(genpd, dev);
270 genpd_stop_dev(genpd, dev);
272 mutex_lock(&genpd->lock);
275 gpd_data->need_restore = true;
281 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
282 * @pdd: Domain data of the device to restore the state of.
283 * @genpd: PM domain the device belongs to.
285 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
286 struct generic_pm_domain *genpd)
287 __releases(&genpd->lock) __acquires(&genpd->lock)
289 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
290 struct device *dev = pdd->dev;
292 if (!gpd_data->need_restore)
295 mutex_unlock(&genpd->lock);
297 genpd_start_dev(genpd, dev);
298 genpd_restore_dev(genpd, dev);
299 genpd_stop_dev(genpd, dev);
301 mutex_lock(&genpd->lock);
303 gpd_data->need_restore = false;
307 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
308 * @genpd: PM domain to check.
310 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
311 * a "power off" operation, which means that a "power on" has occured in the
312 * meantime, or if its resume_count field is different from zero, which means
313 * that one of its devices has been resumed in the meantime.
315 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
317 return genpd->status == GPD_STATE_WAIT_MASTER
318 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
322 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
323 * @genpd: PM domait to power off.
325 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
328 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
330 if (!work_pending(&genpd->power_off_work))
331 queue_work(pm_wq, &genpd->power_off_work);
335 * pm_genpd_poweroff - Remove power from a given PM domain.
336 * @genpd: PM domain to power down.
338 * If all of the @genpd's devices have been suspended and all of its subdomains
339 * have been powered down, run the runtime suspend callbacks provided by all of
340 * the @genpd's devices' drivers and remove power from @genpd.
342 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
343 __releases(&genpd->lock) __acquires(&genpd->lock)
345 struct pm_domain_data *pdd;
346 struct gpd_link *link;
347 unsigned int not_suspended;
352 * Do not try to power off the domain in the following situations:
353 * (1) The domain is already in the "power off" state.
354 * (2) The domain is waiting for its master to power up.
355 * (3) One of the domain's devices is being resumed right now.
356 * (4) System suspend is in progress.
358 if (genpd->status == GPD_STATE_POWER_OFF
359 || genpd->status == GPD_STATE_WAIT_MASTER
360 || genpd->resume_count > 0 || genpd->prepared_count > 0)
363 if (atomic_read(&genpd->sd_count) > 0)
367 list_for_each_entry(pdd, &genpd->dev_list, list_node)
368 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
369 || pdd->dev->power.irq_safe))
372 if (not_suspended > genpd->in_progress)
375 if (genpd->poweroff_task) {
377 * Another instance of pm_genpd_poweroff() is executing
378 * callbacks, so tell it to start over and return.
380 genpd->status = GPD_STATE_REPEAT;
384 if (genpd->gov && genpd->gov->power_down_ok) {
385 if (!genpd->gov->power_down_ok(&genpd->domain))
389 genpd->status = GPD_STATE_BUSY;
390 genpd->poweroff_task = current;
392 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
393 ret = atomic_read(&genpd->sd_count) == 0 ?
394 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
396 if (genpd_abort_poweroff(genpd))
400 genpd_set_active(genpd);
404 if (genpd->status == GPD_STATE_REPEAT) {
405 genpd->poweroff_task = NULL;
410 if (genpd->power_off) {
414 if (atomic_read(&genpd->sd_count) > 0) {
419 time_start = ktime_get();
422 * If sd_count > 0 at this point, one of the subdomains hasn't
423 * managed to call pm_genpd_poweron() for the master yet after
424 * incrementing it. In that case pm_genpd_poweron() will wait
425 * for us to drop the lock, so we can call .power_off() and let
426 * the pm_genpd_poweron() restore power for us (this shouldn't
427 * happen very often).
429 ret = genpd->power_off(genpd);
431 genpd_set_active(genpd);
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436 if (elapsed_ns > genpd->power_off_latency_ns) {
437 genpd->power_off_latency_ns = elapsed_ns;
439 pr_warning("%s: Power-off latency exceeded, "
440 "new value %lld ns\n", genpd->name,
445 genpd->status = GPD_STATE_POWER_OFF;
446 genpd->power_off_time = ktime_get();
448 /* Update PM QoS information for devices in the domain. */
449 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
450 struct gpd_timing_data *td = &to_gpd_data(pdd)->td;
452 pm_runtime_update_max_time_suspended(pdd->dev,
453 td->start_latency_ns +
454 td->restore_state_latency_ns +
455 genpd->power_on_latency_ns);
458 list_for_each_entry(link, &genpd->slave_links, slave_node) {
459 genpd_sd_counter_dec(link->master);
460 genpd_queue_power_off_work(link->master);
464 genpd->poweroff_task = NULL;
465 wake_up_all(&genpd->status_wait_queue);
470 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
471 * @work: Work structure used for scheduling the execution of this function.
473 static void genpd_power_off_work_fn(struct work_struct *work)
475 struct generic_pm_domain *genpd;
477 genpd = container_of(work, struct generic_pm_domain, power_off_work);
479 genpd_acquire_lock(genpd);
480 pm_genpd_poweroff(genpd);
481 genpd_release_lock(genpd);
485 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
486 * @dev: Device to suspend.
488 * Carry out a runtime suspend of a device under the assumption that its
489 * pm_domain field points to the domain member of an object of type
490 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
492 static int pm_genpd_runtime_suspend(struct device *dev)
494 struct generic_pm_domain *genpd;
495 bool (*stop_ok)(struct device *__dev);
498 dev_dbg(dev, "%s()\n", __func__);
500 genpd = dev_to_genpd(dev);
504 might_sleep_if(!genpd->dev_irq_safe);
506 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
507 if (stop_ok && !stop_ok(dev))
510 ret = genpd_stop_dev(genpd, dev);
514 pm_runtime_update_max_time_suspended(dev,
515 dev_gpd_data(dev)->td.start_latency_ns);
518 * If power.irq_safe is set, this routine will be run with interrupts
519 * off, so it can't use mutexes.
521 if (dev->power.irq_safe)
524 mutex_lock(&genpd->lock);
525 genpd->in_progress++;
526 pm_genpd_poweroff(genpd);
527 genpd->in_progress--;
528 mutex_unlock(&genpd->lock);
534 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
535 * @dev: Device to resume.
537 * Carry out a runtime resume of a device under the assumption that its
538 * pm_domain field points to the domain member of an object of type
539 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
541 static int pm_genpd_runtime_resume(struct device *dev)
543 struct generic_pm_domain *genpd;
547 dev_dbg(dev, "%s()\n", __func__);
549 genpd = dev_to_genpd(dev);
553 might_sleep_if(!genpd->dev_irq_safe);
555 /* If power.irq_safe, the PM domain is never powered off. */
556 if (dev->power.irq_safe)
559 mutex_lock(&genpd->lock);
560 ret = __pm_genpd_poweron(genpd);
562 mutex_unlock(&genpd->lock);
565 genpd->status = GPD_STATE_BUSY;
566 genpd->resume_count++;
568 prepare_to_wait(&genpd->status_wait_queue, &wait,
569 TASK_UNINTERRUPTIBLE);
571 * If current is the powering off task, we have been called
572 * reentrantly from one of the device callbacks, so we should
575 if (!genpd->poweroff_task || genpd->poweroff_task == current)
577 mutex_unlock(&genpd->lock);
581 mutex_lock(&genpd->lock);
583 finish_wait(&genpd->status_wait_queue, &wait);
584 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
585 genpd->resume_count--;
586 genpd_set_active(genpd);
587 wake_up_all(&genpd->status_wait_queue);
588 mutex_unlock(&genpd->lock);
591 genpd_start_dev(genpd, dev);
597 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
599 void pm_genpd_poweroff_unused(void)
601 struct generic_pm_domain *genpd;
603 mutex_lock(&gpd_list_lock);
605 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
606 genpd_queue_power_off_work(genpd);
608 mutex_unlock(&gpd_list_lock);
613 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
615 #define pm_genpd_runtime_suspend NULL
616 #define pm_genpd_runtime_resume NULL
618 #endif /* CONFIG_PM_RUNTIME */
620 #ifdef CONFIG_PM_SLEEP
622 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
625 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
628 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
630 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
633 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
635 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
638 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
640 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
643 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
645 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
648 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
650 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
653 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
655 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
658 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
660 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
663 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
665 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
669 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
670 * @genpd: PM domain to power off, if possible.
672 * Check if the given PM domain can be powered off (during system suspend or
673 * hibernation) and do that if so. Also, in that case propagate to its masters.
675 * This function is only called in "noirq" stages of system power transitions,
676 * so it need not acquire locks (all of the "noirq" callbacks are executed
677 * sequentially, so it is guaranteed that it will never run twice in parallel).
679 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
681 struct gpd_link *link;
683 if (genpd->status == GPD_STATE_POWER_OFF)
686 if (genpd->suspended_count != genpd->device_count
687 || atomic_read(&genpd->sd_count) > 0)
690 if (genpd->power_off)
691 genpd->power_off(genpd);
693 genpd->status = GPD_STATE_POWER_OFF;
695 list_for_each_entry(link, &genpd->slave_links, slave_node) {
696 genpd_sd_counter_dec(link->master);
697 pm_genpd_sync_poweroff(link->master);
702 * resume_needed - Check whether to resume a device before system suspend.
703 * @dev: Device to check.
704 * @genpd: PM domain the device belongs to.
706 * There are two cases in which a device that can wake up the system from sleep
707 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
708 * to wake up the system and it has to remain active for this purpose while the
709 * system is in the sleep state and (2) if the device is not enabled to wake up
710 * the system from sleep states and it generally doesn't generate wakeup signals
711 * by itself (those signals are generated on its behalf by other parts of the
712 * system). In the latter case it may be necessary to reconfigure the device's
713 * wakeup settings during system suspend, because it may have been set up to
714 * signal remote wakeup from the system's working state as needed by runtime PM.
715 * Return 'true' in either of the above cases.
717 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
721 if (!device_can_wakeup(dev))
724 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
725 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
729 * pm_genpd_prepare - Start power transition of a device in a PM domain.
730 * @dev: Device to start the transition of.
732 * Start a power transition of a device (during a system-wide power transition)
733 * under the assumption that its pm_domain field points to the domain member of
734 * an object of type struct generic_pm_domain representing a PM domain
735 * consisting of I/O devices.
737 static int pm_genpd_prepare(struct device *dev)
739 struct generic_pm_domain *genpd;
742 dev_dbg(dev, "%s()\n", __func__);
744 genpd = dev_to_genpd(dev);
749 * If a wakeup request is pending for the device, it should be woken up
750 * at this point and a system wakeup event should be reported if it's
751 * set up to wake up the system from sleep states.
753 pm_runtime_get_noresume(dev);
754 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
755 pm_wakeup_event(dev, 0);
757 if (pm_wakeup_pending()) {
758 pm_runtime_put_sync(dev);
762 if (resume_needed(dev, genpd))
763 pm_runtime_resume(dev);
765 genpd_acquire_lock(genpd);
767 if (genpd->prepared_count++ == 0)
768 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
770 genpd_release_lock(genpd);
772 if (genpd->suspend_power_off) {
773 pm_runtime_put_noidle(dev);
778 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
779 * so pm_genpd_poweron() will return immediately, but if the device
780 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
781 * to make it operational.
783 pm_runtime_resume(dev);
784 __pm_runtime_disable(dev, false);
786 ret = pm_generic_prepare(dev);
788 mutex_lock(&genpd->lock);
790 if (--genpd->prepared_count == 0)
791 genpd->suspend_power_off = false;
793 mutex_unlock(&genpd->lock);
794 pm_runtime_enable(dev);
797 pm_runtime_put_sync(dev);
802 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
803 * @dev: Device to suspend.
805 * Suspend a device under the assumption that its pm_domain field points to the
806 * domain member of an object of type struct generic_pm_domain representing
807 * a PM domain consisting of I/O devices.
809 static int pm_genpd_suspend(struct device *dev)
811 struct generic_pm_domain *genpd;
813 dev_dbg(dev, "%s()\n", __func__);
815 genpd = dev_to_genpd(dev);
819 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
823 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
824 * @dev: Device to suspend.
826 * Carry out a late suspend of a device under the assumption that its
827 * pm_domain field points to the domain member of an object of type
828 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
830 static int pm_genpd_suspend_noirq(struct device *dev)
832 struct generic_pm_domain *genpd;
835 dev_dbg(dev, "%s()\n", __func__);
837 genpd = dev_to_genpd(dev);
841 if (genpd->suspend_power_off)
844 ret = genpd_suspend_late(genpd, dev);
848 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
851 genpd_stop_dev(genpd, dev);
854 * Since all of the "noirq" callbacks are executed sequentially, it is
855 * guaranteed that this function will never run twice in parallel for
856 * the same PM domain, so it is not necessary to use locking here.
858 genpd->suspended_count++;
859 pm_genpd_sync_poweroff(genpd);
865 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
866 * @dev: Device to resume.
868 * Carry out an early resume of a device under the assumption that its
869 * pm_domain field points to the domain member of an object of type
870 * struct generic_pm_domain representing a power domain consisting of I/O
873 static int pm_genpd_resume_noirq(struct device *dev)
875 struct generic_pm_domain *genpd;
877 dev_dbg(dev, "%s()\n", __func__);
879 genpd = dev_to_genpd(dev);
883 if (genpd->suspend_power_off)
887 * Since all of the "noirq" callbacks are executed sequentially, it is
888 * guaranteed that this function will never run twice in parallel for
889 * the same PM domain, so it is not necessary to use locking here.
891 pm_genpd_poweron(genpd);
892 genpd->suspended_count--;
893 genpd_start_dev(genpd, dev);
895 return genpd_resume_early(genpd, dev);
899 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
900 * @dev: Device to resume.
902 * Resume a device under the assumption that its pm_domain field points to the
903 * domain member of an object of type struct generic_pm_domain representing
904 * a power domain consisting of I/O devices.
906 static int pm_genpd_resume(struct device *dev)
908 struct generic_pm_domain *genpd;
910 dev_dbg(dev, "%s()\n", __func__);
912 genpd = dev_to_genpd(dev);
916 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
920 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
921 * @dev: Device to freeze.
923 * Freeze a device under the assumption that its pm_domain field points to the
924 * domain member of an object of type struct generic_pm_domain representing
925 * a power domain consisting of I/O devices.
927 static int pm_genpd_freeze(struct device *dev)
929 struct generic_pm_domain *genpd;
931 dev_dbg(dev, "%s()\n", __func__);
933 genpd = dev_to_genpd(dev);
937 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
941 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
942 * @dev: Device to freeze.
944 * Carry out a late freeze of a device under the assumption that its
945 * pm_domain field points to the domain member of an object of type
946 * struct generic_pm_domain representing a power domain consisting of I/O
949 static int pm_genpd_freeze_noirq(struct device *dev)
951 struct generic_pm_domain *genpd;
954 dev_dbg(dev, "%s()\n", __func__);
956 genpd = dev_to_genpd(dev);
960 if (genpd->suspend_power_off)
963 ret = genpd_freeze_late(genpd, dev);
967 genpd_stop_dev(genpd, dev);
973 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
974 * @dev: Device to thaw.
976 * Carry out an early thaw of a device under the assumption that its
977 * pm_domain field points to the domain member of an object of type
978 * struct generic_pm_domain representing a power domain consisting of I/O
981 static int pm_genpd_thaw_noirq(struct device *dev)
983 struct generic_pm_domain *genpd;
985 dev_dbg(dev, "%s()\n", __func__);
987 genpd = dev_to_genpd(dev);
991 if (genpd->suspend_power_off)
994 genpd_start_dev(genpd, dev);
996 return genpd_thaw_early(genpd, dev);
1000 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1001 * @dev: Device to thaw.
1003 * Thaw a device under the assumption that its pm_domain field points to the
1004 * domain member of an object of type struct generic_pm_domain representing
1005 * a power domain consisting of I/O devices.
1007 static int pm_genpd_thaw(struct device *dev)
1009 struct generic_pm_domain *genpd;
1011 dev_dbg(dev, "%s()\n", __func__);
1013 genpd = dev_to_genpd(dev);
1017 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1021 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
1022 * @dev: Device to resume.
1024 * Carry out an early restore of a device under the assumption that its
1025 * pm_domain field points to the domain member of an object of type
1026 * struct generic_pm_domain representing a power domain consisting of I/O
1029 static int pm_genpd_restore_noirq(struct device *dev)
1031 struct generic_pm_domain *genpd;
1033 dev_dbg(dev, "%s()\n", __func__);
1035 genpd = dev_to_genpd(dev);
1040 * Since all of the "noirq" callbacks are executed sequentially, it is
1041 * guaranteed that this function will never run twice in parallel for
1042 * the same PM domain, so it is not necessary to use locking here.
1044 genpd->status = GPD_STATE_POWER_OFF;
1045 if (genpd->suspend_power_off) {
1047 * The boot kernel might put the domain into the power on state,
1048 * so make sure it really is powered off.
1050 if (genpd->power_off)
1051 genpd->power_off(genpd);
1055 pm_genpd_poweron(genpd);
1056 genpd->suspended_count--;
1057 genpd_start_dev(genpd, dev);
1059 return genpd_resume_early(genpd, dev);
1063 * pm_genpd_complete - Complete power transition of a device in a power domain.
1064 * @dev: Device to complete the transition of.
1066 * Complete a power transition of a device (during a system-wide power
1067 * transition) under the assumption that its pm_domain field points to the
1068 * domain member of an object of type struct generic_pm_domain representing
1069 * a power domain consisting of I/O devices.
1071 static void pm_genpd_complete(struct device *dev)
1073 struct generic_pm_domain *genpd;
1076 dev_dbg(dev, "%s()\n", __func__);
1078 genpd = dev_to_genpd(dev);
1082 mutex_lock(&genpd->lock);
1084 run_complete = !genpd->suspend_power_off;
1085 if (--genpd->prepared_count == 0)
1086 genpd->suspend_power_off = false;
1088 mutex_unlock(&genpd->lock);
1091 pm_generic_complete(dev);
1092 pm_runtime_set_active(dev);
1093 pm_runtime_enable(dev);
1094 pm_runtime_idle(dev);
1100 #define pm_genpd_prepare NULL
1101 #define pm_genpd_suspend NULL
1102 #define pm_genpd_suspend_noirq NULL
1103 #define pm_genpd_resume_noirq NULL
1104 #define pm_genpd_resume NULL
1105 #define pm_genpd_freeze NULL
1106 #define pm_genpd_freeze_noirq NULL
1107 #define pm_genpd_thaw_noirq NULL
1108 #define pm_genpd_thaw NULL
1109 #define pm_genpd_restore_noirq NULL
1110 #define pm_genpd_complete NULL
1112 #endif /* CONFIG_PM_SLEEP */
1115 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1116 * @genpd: PM domain to add the device to.
1117 * @dev: Device to be added.
1118 * @td: Set of PM QoS timing parameters to attach to the device.
1120 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1121 struct gpd_timing_data *td)
1123 struct generic_pm_domain_data *gpd_data;
1124 struct pm_domain_data *pdd;
1127 dev_dbg(dev, "%s()\n", __func__);
1129 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1132 genpd_acquire_lock(genpd);
1134 if (genpd->status == GPD_STATE_POWER_OFF) {
1139 if (genpd->prepared_count > 0) {
1144 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1145 if (pdd->dev == dev) {
1150 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1156 genpd->device_count++;
1158 dev->pm_domain = &genpd->domain;
1159 dev_pm_get_subsys_data(dev);
1160 dev->power.subsys_data->domain_data = &gpd_data->base;
1161 gpd_data->base.dev = dev;
1162 gpd_data->need_restore = false;
1163 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1168 genpd_release_lock(genpd);
1174 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1175 * @genpd: PM domain to remove the device from.
1176 * @dev: Device to be removed.
1178 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1181 struct pm_domain_data *pdd;
1184 dev_dbg(dev, "%s()\n", __func__);
1186 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1189 genpd_acquire_lock(genpd);
1191 if (genpd->prepared_count > 0) {
1196 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1197 if (pdd->dev != dev)
1200 list_del_init(&pdd->list_node);
1202 dev_pm_put_subsys_data(dev);
1203 dev->pm_domain = NULL;
1204 kfree(to_gpd_data(pdd));
1206 genpd->device_count--;
1213 genpd_release_lock(genpd);
1219 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1220 * @genpd: Master PM domain to add the subdomain to.
1221 * @subdomain: Subdomain to be added.
1223 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1224 struct generic_pm_domain *subdomain)
1226 struct gpd_link *link;
1229 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1233 genpd_acquire_lock(genpd);
1234 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1236 if (subdomain->status != GPD_STATE_POWER_OFF
1237 && subdomain->status != GPD_STATE_ACTIVE) {
1238 mutex_unlock(&subdomain->lock);
1239 genpd_release_lock(genpd);
1243 if (genpd->status == GPD_STATE_POWER_OFF
1244 && subdomain->status != GPD_STATE_POWER_OFF) {
1249 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1250 if (link->slave == subdomain && link->master == genpd) {
1256 link = kzalloc(sizeof(*link), GFP_KERNEL);
1261 link->master = genpd;
1262 list_add_tail(&link->master_node, &genpd->master_links);
1263 link->slave = subdomain;
1264 list_add_tail(&link->slave_node, &subdomain->slave_links);
1265 if (subdomain->status != GPD_STATE_POWER_OFF)
1266 genpd_sd_counter_inc(genpd);
1269 mutex_unlock(&subdomain->lock);
1270 genpd_release_lock(genpd);
1276 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1277 * @genpd: Master PM domain to remove the subdomain from.
1278 * @subdomain: Subdomain to be removed.
1280 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1281 struct generic_pm_domain *subdomain)
1283 struct gpd_link *link;
1286 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1290 genpd_acquire_lock(genpd);
1292 list_for_each_entry(link, &genpd->master_links, master_node) {
1293 if (link->slave != subdomain)
1296 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1298 if (subdomain->status != GPD_STATE_POWER_OFF
1299 && subdomain->status != GPD_STATE_ACTIVE) {
1300 mutex_unlock(&subdomain->lock);
1301 genpd_release_lock(genpd);
1305 list_del(&link->master_node);
1306 list_del(&link->slave_node);
1308 if (subdomain->status != GPD_STATE_POWER_OFF)
1309 genpd_sd_counter_dec(genpd);
1311 mutex_unlock(&subdomain->lock);
1317 genpd_release_lock(genpd);
1323 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1324 * @dev: Device to add the callbacks to.
1325 * @ops: Set of callbacks to add.
1326 * @td: Timing data to add to the device along with the callbacks (optional).
1328 int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1329 struct gpd_timing_data *td)
1331 struct pm_domain_data *pdd;
1334 if (!(dev && dev->power.subsys_data && ops))
1337 pm_runtime_disable(dev);
1340 pdd = dev->power.subsys_data->domain_data;
1342 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1344 gpd_data->ops = *ops;
1352 pm_runtime_enable(dev);
1356 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1359 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1360 * @dev: Device to remove the callbacks from.
1361 * @clear_td: If set, clear the device's timing data too.
1363 int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1365 struct pm_domain_data *pdd;
1368 if (!(dev && dev->power.subsys_data))
1371 pm_runtime_disable(dev);
1374 pdd = dev->power.subsys_data->domain_data;
1376 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1378 gpd_data->ops = (struct gpd_dev_ops){ 0 };
1380 gpd_data->td = (struct gpd_timing_data){ 0 };
1386 pm_runtime_enable(dev);
1390 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1392 /* Default device callbacks for generic PM domains. */
1395 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1396 * @dev: Device to handle.
1398 static int pm_genpd_default_save_state(struct device *dev)
1400 int (*cb)(struct device *__dev);
1401 struct device_driver *drv = dev->driver;
1403 cb = dev_gpd_data(dev)->ops.save_state;
1407 if (drv && drv->pm && drv->pm->runtime_suspend)
1408 return drv->pm->runtime_suspend(dev);
1414 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1415 * @dev: Device to handle.
1417 static int pm_genpd_default_restore_state(struct device *dev)
1419 int (*cb)(struct device *__dev);
1420 struct device_driver *drv = dev->driver;
1422 cb = dev_gpd_data(dev)->ops.restore_state;
1426 if (drv && drv->pm && drv->pm->runtime_resume)
1427 return drv->pm->runtime_resume(dev);
1432 #ifdef CONFIG_PM_SLEEP
1435 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1436 * @dev: Device to handle.
1438 static int pm_genpd_default_suspend(struct device *dev)
1440 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
1442 return cb ? cb(dev) : pm_generic_suspend(dev);
1446 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1447 * @dev: Device to handle.
1449 static int pm_genpd_default_suspend_late(struct device *dev)
1451 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
1453 return cb ? cb(dev) : pm_generic_suspend_noirq(dev);
1457 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1458 * @dev: Device to handle.
1460 static int pm_genpd_default_resume_early(struct device *dev)
1462 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
1464 return cb ? cb(dev) : pm_generic_resume_noirq(dev);
1468 * pm_genpd_default_resume - Default "device resume" for PM domians.
1469 * @dev: Device to handle.
1471 static int pm_genpd_default_resume(struct device *dev)
1473 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
1475 return cb ? cb(dev) : pm_generic_resume(dev);
1479 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1480 * @dev: Device to handle.
1482 static int pm_genpd_default_freeze(struct device *dev)
1484 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1486 return cb ? cb(dev) : pm_generic_freeze(dev);
1490 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1491 * @dev: Device to handle.
1493 static int pm_genpd_default_freeze_late(struct device *dev)
1495 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1497 return cb ? cb(dev) : pm_generic_freeze_noirq(dev);
1501 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1502 * @dev: Device to handle.
1504 static int pm_genpd_default_thaw_early(struct device *dev)
1506 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1508 return cb ? cb(dev) : pm_generic_thaw_noirq(dev);
1512 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1513 * @dev: Device to handle.
1515 static int pm_genpd_default_thaw(struct device *dev)
1517 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1519 return cb ? cb(dev) : pm_generic_thaw(dev);
1522 #else /* !CONFIG_PM_SLEEP */
1524 #define pm_genpd_default_suspend NULL
1525 #define pm_genpd_default_suspend_late NULL
1526 #define pm_genpd_default_resume_early NULL
1527 #define pm_genpd_default_resume NULL
1528 #define pm_genpd_default_freeze NULL
1529 #define pm_genpd_default_freeze_late NULL
1530 #define pm_genpd_default_thaw_early NULL
1531 #define pm_genpd_default_thaw NULL
1533 #endif /* !CONFIG_PM_SLEEP */
1536 * pm_genpd_init - Initialize a generic I/O PM domain object.
1537 * @genpd: PM domain object to initialize.
1538 * @gov: PM domain governor to associate with the domain (may be NULL).
1539 * @is_off: Initial value of the domain's power_is_off field.
1541 void pm_genpd_init(struct generic_pm_domain *genpd,
1542 struct dev_power_governor *gov, bool is_off)
1544 if (IS_ERR_OR_NULL(genpd))
1547 INIT_LIST_HEAD(&genpd->master_links);
1548 INIT_LIST_HEAD(&genpd->slave_links);
1549 INIT_LIST_HEAD(&genpd->dev_list);
1550 mutex_init(&genpd->lock);
1552 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1553 genpd->in_progress = 0;
1554 atomic_set(&genpd->sd_count, 0);
1555 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1556 init_waitqueue_head(&genpd->status_wait_queue);
1557 genpd->poweroff_task = NULL;
1558 genpd->resume_count = 0;
1559 genpd->device_count = 0;
1560 genpd->suspended_count = 0;
1561 genpd->max_off_time_ns = -1;
1562 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1563 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1564 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1565 genpd->domain.ops.prepare = pm_genpd_prepare;
1566 genpd->domain.ops.suspend = pm_genpd_suspend;
1567 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1568 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1569 genpd->domain.ops.resume = pm_genpd_resume;
1570 genpd->domain.ops.freeze = pm_genpd_freeze;
1571 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1572 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1573 genpd->domain.ops.thaw = pm_genpd_thaw;
1574 genpd->domain.ops.poweroff = pm_genpd_suspend;
1575 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1576 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1577 genpd->domain.ops.restore = pm_genpd_resume;
1578 genpd->domain.ops.complete = pm_genpd_complete;
1579 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1580 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1581 genpd->dev_ops.suspend = pm_genpd_default_suspend;
1582 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
1583 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
1584 genpd->dev_ops.resume = pm_genpd_default_resume;
1585 genpd->dev_ops.freeze = pm_genpd_default_freeze;
1586 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
1587 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
1588 genpd->dev_ops.thaw = pm_genpd_default_thaw;
1589 mutex_lock(&gpd_list_lock);
1590 list_add(&genpd->gpd_list_node, &gpd_list);
1591 mutex_unlock(&gpd_list_lock);