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/pm_qos.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/suspend.h>
19 #include <linux/export.h>
21 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
23 type (*__routine)(struct device *__d); \
24 type __ret = (type)0; \
26 __routine = genpd->dev_ops.callback; \
28 __ret = __routine(dev); \
30 __routine = dev_gpd_data(dev)->ops.callback; \
32 __ret = __routine(dev); \
37 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
39 ktime_t __start = ktime_get(); \
40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
42 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
43 if (!__retval && __elapsed > __td->field) { \
44 __td->field = __elapsed; \
45 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
47 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
53 static LIST_HEAD(gpd_list
);
54 static DEFINE_MUTEX(gpd_list_lock
);
56 static struct generic_pm_domain
*pm_genpd_lookup_name(const char *domain_name
)
58 struct generic_pm_domain
*genpd
= NULL
, *gpd
;
60 if (IS_ERR_OR_NULL(domain_name
))
63 mutex_lock(&gpd_list_lock
);
64 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
65 if (!strcmp(gpd
->name
, domain_name
)) {
70 mutex_unlock(&gpd_list_lock
);
76 struct generic_pm_domain
*dev_to_genpd(struct device
*dev
)
78 if (IS_ERR_OR_NULL(dev
->pm_domain
))
79 return ERR_PTR(-EINVAL
);
81 return pd_to_genpd(dev
->pm_domain
);
84 static int genpd_stop_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
86 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, stop
, dev
,
87 stop_latency_ns
, "stop");
90 static int genpd_start_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
92 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, start
, dev
,
93 start_latency_ns
, "start");
96 static bool genpd_sd_counter_dec(struct generic_pm_domain
*genpd
)
100 if (!WARN_ON(atomic_read(&genpd
->sd_count
) == 0))
101 ret
= !!atomic_dec_and_test(&genpd
->sd_count
);
106 static void genpd_sd_counter_inc(struct generic_pm_domain
*genpd
)
108 atomic_inc(&genpd
->sd_count
);
109 smp_mb__after_atomic_inc();
112 static void genpd_acquire_lock(struct generic_pm_domain
*genpd
)
116 mutex_lock(&genpd
->lock
);
118 * Wait for the domain to transition into either the active,
119 * or the power off state.
122 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
123 TASK_UNINTERRUPTIBLE
);
124 if (genpd
->status
== GPD_STATE_ACTIVE
125 || genpd
->status
== GPD_STATE_POWER_OFF
)
127 mutex_unlock(&genpd
->lock
);
131 mutex_lock(&genpd
->lock
);
133 finish_wait(&genpd
->status_wait_queue
, &wait
);
136 static void genpd_release_lock(struct generic_pm_domain
*genpd
)
138 mutex_unlock(&genpd
->lock
);
141 static void genpd_set_active(struct generic_pm_domain
*genpd
)
143 if (genpd
->resume_count
== 0)
144 genpd
->status
= GPD_STATE_ACTIVE
;
147 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain
*genpd
)
151 if (!genpd
->cpu_data
)
154 usecs64
= genpd
->power_on_latency_ns
;
155 do_div(usecs64
, NSEC_PER_USEC
);
156 usecs64
+= genpd
->cpu_data
->saved_exit_latency
;
157 genpd
->cpu_data
->idle_state
->exit_latency
= usecs64
;
161 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
162 * @genpd: PM domain to power up.
164 * Restore power to @genpd and all of its masters so that it is possible to
165 * resume a device belonging to it.
167 static int __pm_genpd_poweron(struct generic_pm_domain
*genpd
)
168 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
170 struct gpd_link
*link
;
174 /* If the domain's master is being waited for, we have to wait too. */
176 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
177 TASK_UNINTERRUPTIBLE
);
178 if (genpd
->status
!= GPD_STATE_WAIT_MASTER
)
180 mutex_unlock(&genpd
->lock
);
184 mutex_lock(&genpd
->lock
);
186 finish_wait(&genpd
->status_wait_queue
, &wait
);
188 if (genpd
->status
== GPD_STATE_ACTIVE
189 || (genpd
->prepared_count
> 0 && genpd
->suspend_power_off
))
192 if (genpd
->status
!= GPD_STATE_POWER_OFF
) {
193 genpd_set_active(genpd
);
197 if (genpd
->cpu_data
) {
198 cpuidle_pause_and_lock();
199 genpd
->cpu_data
->idle_state
->disabled
= true;
200 cpuidle_resume_and_unlock();
205 * The list is guaranteed not to change while the loop below is being
206 * executed, unless one of the masters' .power_on() callbacks fiddles
209 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
210 genpd_sd_counter_inc(link
->master
);
211 genpd
->status
= GPD_STATE_WAIT_MASTER
;
213 mutex_unlock(&genpd
->lock
);
215 ret
= pm_genpd_poweron(link
->master
);
217 mutex_lock(&genpd
->lock
);
220 * The "wait for parent" status is guaranteed not to change
221 * while the master is powering on.
223 genpd
->status
= GPD_STATE_POWER_OFF
;
224 wake_up_all(&genpd
->status_wait_queue
);
226 genpd_sd_counter_dec(link
->master
);
231 if (genpd
->power_on
) {
232 ktime_t time_start
= ktime_get();
235 ret
= genpd
->power_on(genpd
);
239 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
240 if (elapsed_ns
> genpd
->power_on_latency_ns
) {
241 genpd
->power_on_latency_ns
= elapsed_ns
;
242 genpd
->max_off_time_changed
= true;
243 genpd_recalc_cpu_exit_latency(genpd
);
245 pr_warning("%s: Power-on latency exceeded, "
246 "new value %lld ns\n", genpd
->name
,
252 genpd_set_active(genpd
);
257 list_for_each_entry_continue_reverse(link
, &genpd
->slave_links
, slave_node
)
258 genpd_sd_counter_dec(link
->master
);
264 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
265 * @genpd: PM domain to power up.
267 int pm_genpd_poweron(struct generic_pm_domain
*genpd
)
271 mutex_lock(&genpd
->lock
);
272 ret
= __pm_genpd_poweron(genpd
);
273 mutex_unlock(&genpd
->lock
);
278 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
279 * @domain_name: Name of the PM domain to power up.
281 int pm_genpd_name_poweron(const char *domain_name
)
283 struct generic_pm_domain
*genpd
;
285 genpd
= pm_genpd_lookup_name(domain_name
);
286 return genpd
? pm_genpd_poweron(genpd
) : -EINVAL
;
289 #endif /* CONFIG_PM */
291 #ifdef CONFIG_PM_RUNTIME
293 static int genpd_start_dev_no_timing(struct generic_pm_domain
*genpd
,
296 return GENPD_DEV_CALLBACK(genpd
, int, start
, dev
);
299 static int genpd_save_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
301 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, save_state
, dev
,
302 save_state_latency_ns
, "state save");
305 static int genpd_restore_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
307 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, restore_state
, dev
,
308 restore_state_latency_ns
,
312 static int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
313 unsigned long val
, void *ptr
)
315 struct generic_pm_domain_data
*gpd_data
;
318 gpd_data
= container_of(nb
, struct generic_pm_domain_data
, nb
);
320 mutex_lock(&gpd_data
->lock
);
321 dev
= gpd_data
->base
.dev
;
323 mutex_unlock(&gpd_data
->lock
);
326 mutex_unlock(&gpd_data
->lock
);
329 struct generic_pm_domain
*genpd
;
330 struct pm_domain_data
*pdd
;
332 spin_lock_irq(&dev
->power
.lock
);
334 pdd
= dev
->power
.subsys_data
?
335 dev
->power
.subsys_data
->domain_data
: NULL
;
336 if (pdd
&& pdd
->dev
) {
337 to_gpd_data(pdd
)->td
.constraint_changed
= true;
338 genpd
= dev_to_genpd(dev
);
340 genpd
= ERR_PTR(-ENODATA
);
343 spin_unlock_irq(&dev
->power
.lock
);
345 if (!IS_ERR(genpd
)) {
346 mutex_lock(&genpd
->lock
);
347 genpd
->max_off_time_changed
= true;
348 mutex_unlock(&genpd
->lock
);
352 if (!dev
|| dev
->power
.ignore_children
)
360 * __pm_genpd_save_device - Save the pre-suspend state of a device.
361 * @pdd: Domain data of the device to save the state of.
362 * @genpd: PM domain the device belongs to.
364 static int __pm_genpd_save_device(struct pm_domain_data
*pdd
,
365 struct generic_pm_domain
*genpd
)
366 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
368 struct generic_pm_domain_data
*gpd_data
= to_gpd_data(pdd
);
369 struct device
*dev
= pdd
->dev
;
372 if (gpd_data
->need_restore
)
375 mutex_unlock(&genpd
->lock
);
377 genpd_start_dev(genpd
, dev
);
378 ret
= genpd_save_dev(genpd
, dev
);
379 genpd_stop_dev(genpd
, dev
);
381 mutex_lock(&genpd
->lock
);
384 gpd_data
->need_restore
= true;
390 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
391 * @pdd: Domain data of the device to restore the state of.
392 * @genpd: PM domain the device belongs to.
394 static void __pm_genpd_restore_device(struct pm_domain_data
*pdd
,
395 struct generic_pm_domain
*genpd
)
396 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
398 struct generic_pm_domain_data
*gpd_data
= to_gpd_data(pdd
);
399 struct device
*dev
= pdd
->dev
;
400 bool need_restore
= gpd_data
->need_restore
;
402 gpd_data
->need_restore
= false;
403 mutex_unlock(&genpd
->lock
);
405 genpd_start_dev(genpd
, dev
);
407 genpd_restore_dev(genpd
, dev
);
409 mutex_lock(&genpd
->lock
);
413 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
414 * @genpd: PM domain to check.
416 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
417 * a "power off" operation, which means that a "power on" has occured in the
418 * meantime, or if its resume_count field is different from zero, which means
419 * that one of its devices has been resumed in the meantime.
421 static bool genpd_abort_poweroff(struct generic_pm_domain
*genpd
)
423 return genpd
->status
== GPD_STATE_WAIT_MASTER
424 || genpd
->status
== GPD_STATE_ACTIVE
|| genpd
->resume_count
> 0;
428 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
429 * @genpd: PM domait to power off.
431 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
434 void genpd_queue_power_off_work(struct generic_pm_domain
*genpd
)
436 if (!work_pending(&genpd
->power_off_work
))
437 queue_work(pm_wq
, &genpd
->power_off_work
);
441 * pm_genpd_poweroff - Remove power from a given PM domain.
442 * @genpd: PM domain to power down.
444 * If all of the @genpd's devices have been suspended and all of its subdomains
445 * have been powered down, run the runtime suspend callbacks provided by all of
446 * the @genpd's devices' drivers and remove power from @genpd.
448 static int pm_genpd_poweroff(struct generic_pm_domain
*genpd
)
449 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
451 struct pm_domain_data
*pdd
;
452 struct gpd_link
*link
;
453 unsigned int not_suspended
;
458 * Do not try to power off the domain in the following situations:
459 * (1) The domain is already in the "power off" state.
460 * (2) The domain is waiting for its master to power up.
461 * (3) One of the domain's devices is being resumed right now.
462 * (4) System suspend is in progress.
464 if (genpd
->status
== GPD_STATE_POWER_OFF
465 || genpd
->status
== GPD_STATE_WAIT_MASTER
466 || genpd
->resume_count
> 0 || genpd
->prepared_count
> 0)
469 if (atomic_read(&genpd
->sd_count
) > 0)
473 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
)
474 if (pdd
->dev
->driver
&& (!pm_runtime_suspended(pdd
->dev
)
475 || pdd
->dev
->power
.irq_safe
))
478 if (not_suspended
> genpd
->in_progress
)
481 if (genpd
->poweroff_task
) {
483 * Another instance of pm_genpd_poweroff() is executing
484 * callbacks, so tell it to start over and return.
486 genpd
->status
= GPD_STATE_REPEAT
;
490 if (genpd
->gov
&& genpd
->gov
->power_down_ok
) {
491 if (!genpd
->gov
->power_down_ok(&genpd
->domain
))
495 genpd
->status
= GPD_STATE_BUSY
;
496 genpd
->poweroff_task
= current
;
498 list_for_each_entry_reverse(pdd
, &genpd
->dev_list
, list_node
) {
499 ret
= atomic_read(&genpd
->sd_count
) == 0 ?
500 __pm_genpd_save_device(pdd
, genpd
) : -EBUSY
;
502 if (genpd_abort_poweroff(genpd
))
506 genpd_set_active(genpd
);
510 if (genpd
->status
== GPD_STATE_REPEAT
) {
511 genpd
->poweroff_task
= NULL
;
516 if (genpd
->cpu_data
) {
518 * If cpu_data is set, cpuidle should turn the domain off when
519 * the CPU in it is idle. In that case we don't decrement the
520 * subdomain counts of the master domains, so that power is not
521 * removed from the current domain prematurely as a result of
522 * cutting off the masters' power.
524 genpd
->status
= GPD_STATE_POWER_OFF
;
525 cpuidle_pause_and_lock();
526 genpd
->cpu_data
->idle_state
->disabled
= false;
527 cpuidle_resume_and_unlock();
531 if (genpd
->power_off
) {
535 if (atomic_read(&genpd
->sd_count
) > 0) {
540 time_start
= ktime_get();
543 * If sd_count > 0 at this point, one of the subdomains hasn't
544 * managed to call pm_genpd_poweron() for the master yet after
545 * incrementing it. In that case pm_genpd_poweron() will wait
546 * for us to drop the lock, so we can call .power_off() and let
547 * the pm_genpd_poweron() restore power for us (this shouldn't
548 * happen very often).
550 ret
= genpd
->power_off(genpd
);
552 genpd_set_active(genpd
);
556 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
557 if (elapsed_ns
> genpd
->power_off_latency_ns
) {
558 genpd
->power_off_latency_ns
= elapsed_ns
;
559 genpd
->max_off_time_changed
= true;
561 pr_warning("%s: Power-off latency exceeded, "
562 "new value %lld ns\n", genpd
->name
,
567 genpd
->status
= GPD_STATE_POWER_OFF
;
569 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
570 genpd_sd_counter_dec(link
->master
);
571 genpd_queue_power_off_work(link
->master
);
575 genpd
->poweroff_task
= NULL
;
576 wake_up_all(&genpd
->status_wait_queue
);
581 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
582 * @work: Work structure used for scheduling the execution of this function.
584 static void genpd_power_off_work_fn(struct work_struct
*work
)
586 struct generic_pm_domain
*genpd
;
588 genpd
= container_of(work
, struct generic_pm_domain
, power_off_work
);
590 genpd_acquire_lock(genpd
);
591 pm_genpd_poweroff(genpd
);
592 genpd_release_lock(genpd
);
596 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
597 * @dev: Device to suspend.
599 * Carry out a runtime suspend of a device under the assumption that its
600 * pm_domain field points to the domain member of an object of type
601 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
603 static int pm_genpd_runtime_suspend(struct device
*dev
)
605 struct generic_pm_domain
*genpd
;
606 bool (*stop_ok
)(struct device
*__dev
);
609 dev_dbg(dev
, "%s()\n", __func__
);
611 genpd
= dev_to_genpd(dev
);
615 might_sleep_if(!genpd
->dev_irq_safe
);
617 stop_ok
= genpd
->gov
? genpd
->gov
->stop_ok
: NULL
;
618 if (stop_ok
&& !stop_ok(dev
))
621 ret
= genpd_stop_dev(genpd
, dev
);
626 * If power.irq_safe is set, this routine will be run with interrupts
627 * off, so it can't use mutexes.
629 if (dev
->power
.irq_safe
)
632 mutex_lock(&genpd
->lock
);
633 genpd
->in_progress
++;
634 pm_genpd_poweroff(genpd
);
635 genpd
->in_progress
--;
636 mutex_unlock(&genpd
->lock
);
642 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
643 * @dev: Device to resume.
645 * Carry out a runtime resume of a device under the assumption that its
646 * pm_domain field points to the domain member of an object of type
647 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
649 static int pm_genpd_runtime_resume(struct device
*dev
)
651 struct generic_pm_domain
*genpd
;
655 dev_dbg(dev
, "%s()\n", __func__
);
657 genpd
= dev_to_genpd(dev
);
661 might_sleep_if(!genpd
->dev_irq_safe
);
663 /* If power.irq_safe, the PM domain is never powered off. */
664 if (dev
->power
.irq_safe
)
665 return genpd_start_dev_no_timing(genpd
, dev
);
667 mutex_lock(&genpd
->lock
);
668 ret
= __pm_genpd_poweron(genpd
);
670 mutex_unlock(&genpd
->lock
);
673 genpd
->status
= GPD_STATE_BUSY
;
674 genpd
->resume_count
++;
676 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
677 TASK_UNINTERRUPTIBLE
);
679 * If current is the powering off task, we have been called
680 * reentrantly from one of the device callbacks, so we should
683 if (!genpd
->poweroff_task
|| genpd
->poweroff_task
== current
)
685 mutex_unlock(&genpd
->lock
);
689 mutex_lock(&genpd
->lock
);
691 finish_wait(&genpd
->status_wait_queue
, &wait
);
692 __pm_genpd_restore_device(dev
->power
.subsys_data
->domain_data
, genpd
);
693 genpd
->resume_count
--;
694 genpd_set_active(genpd
);
695 wake_up_all(&genpd
->status_wait_queue
);
696 mutex_unlock(&genpd
->lock
);
702 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
704 void pm_genpd_poweroff_unused(void)
706 struct generic_pm_domain
*genpd
;
708 mutex_lock(&gpd_list_lock
);
710 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
)
711 genpd_queue_power_off_work(genpd
);
713 mutex_unlock(&gpd_list_lock
);
718 static inline int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
719 unsigned long val
, void *ptr
)
724 static inline void genpd_power_off_work_fn(struct work_struct
*work
) {}
726 #define pm_genpd_runtime_suspend NULL
727 #define pm_genpd_runtime_resume NULL
729 #endif /* CONFIG_PM_RUNTIME */
731 #ifdef CONFIG_PM_SLEEP
734 * pm_genpd_present - Check if the given PM domain has been initialized.
735 * @genpd: PM domain to check.
737 static bool pm_genpd_present(struct generic_pm_domain
*genpd
)
739 struct generic_pm_domain
*gpd
;
741 if (IS_ERR_OR_NULL(genpd
))
744 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
751 static bool genpd_dev_active_wakeup(struct generic_pm_domain
*genpd
,
754 return GENPD_DEV_CALLBACK(genpd
, bool, active_wakeup
, dev
);
757 static int genpd_suspend_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
759 return GENPD_DEV_CALLBACK(genpd
, int, suspend
, dev
);
762 static int genpd_suspend_late(struct generic_pm_domain
*genpd
, struct device
*dev
)
764 return GENPD_DEV_CALLBACK(genpd
, int, suspend_late
, dev
);
767 static int genpd_resume_early(struct generic_pm_domain
*genpd
, struct device
*dev
)
769 return GENPD_DEV_CALLBACK(genpd
, int, resume_early
, dev
);
772 static int genpd_resume_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
774 return GENPD_DEV_CALLBACK(genpd
, int, resume
, dev
);
777 static int genpd_freeze_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
779 return GENPD_DEV_CALLBACK(genpd
, int, freeze
, dev
);
782 static int genpd_freeze_late(struct generic_pm_domain
*genpd
, struct device
*dev
)
784 return GENPD_DEV_CALLBACK(genpd
, int, freeze_late
, dev
);
787 static int genpd_thaw_early(struct generic_pm_domain
*genpd
, struct device
*dev
)
789 return GENPD_DEV_CALLBACK(genpd
, int, thaw_early
, dev
);
792 static int genpd_thaw_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
794 return GENPD_DEV_CALLBACK(genpd
, int, thaw
, dev
);
798 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
799 * @genpd: PM domain to power off, if possible.
801 * Check if the given PM domain can be powered off (during system suspend or
802 * hibernation) and do that if so. Also, in that case propagate to its masters.
804 * This function is only called in "noirq" and "syscore" stages of system power
805 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
806 * executed sequentially, so it is guaranteed that it will never run twice in
809 static void pm_genpd_sync_poweroff(struct generic_pm_domain
*genpd
)
811 struct gpd_link
*link
;
813 if (genpd
->status
== GPD_STATE_POWER_OFF
)
816 if (genpd
->suspended_count
!= genpd
->device_count
817 || atomic_read(&genpd
->sd_count
) > 0)
820 if (genpd
->power_off
)
821 genpd
->power_off(genpd
);
823 genpd
->status
= GPD_STATE_POWER_OFF
;
825 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
826 genpd_sd_counter_dec(link
->master
);
827 pm_genpd_sync_poweroff(link
->master
);
832 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
833 * @genpd: PM domain to power on.
835 * This function is only called in "noirq" and "syscore" stages of system power
836 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
837 * executed sequentially, so it is guaranteed that it will never run twice in
840 static void pm_genpd_sync_poweron(struct generic_pm_domain
*genpd
)
842 struct gpd_link
*link
;
844 if (genpd
->status
!= GPD_STATE_POWER_OFF
)
847 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
848 pm_genpd_sync_poweron(link
->master
);
849 genpd_sd_counter_inc(link
->master
);
853 genpd
->power_on(genpd
);
855 genpd
->status
= GPD_STATE_ACTIVE
;
859 * resume_needed - Check whether to resume a device before system suspend.
860 * @dev: Device to check.
861 * @genpd: PM domain the device belongs to.
863 * There are two cases in which a device that can wake up the system from sleep
864 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
865 * to wake up the system and it has to remain active for this purpose while the
866 * system is in the sleep state and (2) if the device is not enabled to wake up
867 * the system from sleep states and it generally doesn't generate wakeup signals
868 * by itself (those signals are generated on its behalf by other parts of the
869 * system). In the latter case it may be necessary to reconfigure the device's
870 * wakeup settings during system suspend, because it may have been set up to
871 * signal remote wakeup from the system's working state as needed by runtime PM.
872 * Return 'true' in either of the above cases.
874 static bool resume_needed(struct device
*dev
, struct generic_pm_domain
*genpd
)
878 if (!device_can_wakeup(dev
))
881 active_wakeup
= genpd_dev_active_wakeup(genpd
, dev
);
882 return device_may_wakeup(dev
) ? active_wakeup
: !active_wakeup
;
886 * pm_genpd_prepare - Start power transition of a device in a PM domain.
887 * @dev: Device to start the transition of.
889 * Start a power transition of a device (during a system-wide power transition)
890 * under the assumption that its pm_domain field points to the domain member of
891 * an object of type struct generic_pm_domain representing a PM domain
892 * consisting of I/O devices.
894 static int pm_genpd_prepare(struct device
*dev
)
896 struct generic_pm_domain
*genpd
;
899 dev_dbg(dev
, "%s()\n", __func__
);
901 genpd
= dev_to_genpd(dev
);
906 * If a wakeup request is pending for the device, it should be woken up
907 * at this point and a system wakeup event should be reported if it's
908 * set up to wake up the system from sleep states.
910 pm_runtime_get_noresume(dev
);
911 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
912 pm_wakeup_event(dev
, 0);
914 if (pm_wakeup_pending()) {
915 pm_runtime_put_sync(dev
);
919 if (resume_needed(dev
, genpd
))
920 pm_runtime_resume(dev
);
922 genpd_acquire_lock(genpd
);
924 if (genpd
->prepared_count
++ == 0) {
925 genpd
->suspended_count
= 0;
926 genpd
->suspend_power_off
= genpd
->status
== GPD_STATE_POWER_OFF
;
929 genpd_release_lock(genpd
);
931 if (genpd
->suspend_power_off
) {
932 pm_runtime_put_noidle(dev
);
937 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
938 * so pm_genpd_poweron() will return immediately, but if the device
939 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
940 * to make it operational.
942 pm_runtime_resume(dev
);
943 __pm_runtime_disable(dev
, false);
945 ret
= pm_generic_prepare(dev
);
947 mutex_lock(&genpd
->lock
);
949 if (--genpd
->prepared_count
== 0)
950 genpd
->suspend_power_off
= false;
952 mutex_unlock(&genpd
->lock
);
953 pm_runtime_enable(dev
);
956 pm_runtime_put_sync(dev
);
961 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
962 * @dev: Device to suspend.
964 * Suspend a device under the assumption that its pm_domain field points to the
965 * domain member of an object of type struct generic_pm_domain representing
966 * a PM domain consisting of I/O devices.
968 static int pm_genpd_suspend(struct device
*dev
)
970 struct generic_pm_domain
*genpd
;
972 dev_dbg(dev
, "%s()\n", __func__
);
974 genpd
= dev_to_genpd(dev
);
978 return genpd
->suspend_power_off
? 0 : genpd_suspend_dev(genpd
, dev
);
982 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
983 * @dev: Device to suspend.
985 * Carry out a late suspend of a device under the assumption that its
986 * pm_domain field points to the domain member of an object of type
987 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
989 static int pm_genpd_suspend_late(struct device
*dev
)
991 struct generic_pm_domain
*genpd
;
993 dev_dbg(dev
, "%s()\n", __func__
);
995 genpd
= dev_to_genpd(dev
);
999 return genpd
->suspend_power_off
? 0 : genpd_suspend_late(genpd
, dev
);
1003 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1004 * @dev: Device to suspend.
1006 * Stop the device and remove power from the domain if all devices in it have
1009 static int pm_genpd_suspend_noirq(struct device
*dev
)
1011 struct generic_pm_domain
*genpd
;
1013 dev_dbg(dev
, "%s()\n", __func__
);
1015 genpd
= dev_to_genpd(dev
);
1019 if (genpd
->suspend_power_off
1020 || (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
)))
1023 genpd_stop_dev(genpd
, dev
);
1026 * Since all of the "noirq" callbacks are executed sequentially, it is
1027 * guaranteed that this function will never run twice in parallel for
1028 * the same PM domain, so it is not necessary to use locking here.
1030 genpd
->suspended_count
++;
1031 pm_genpd_sync_poweroff(genpd
);
1037 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1038 * @dev: Device to resume.
1040 * Restore power to the device's PM domain, if necessary, and start the device.
1042 static int pm_genpd_resume_noirq(struct device
*dev
)
1044 struct generic_pm_domain
*genpd
;
1046 dev_dbg(dev
, "%s()\n", __func__
);
1048 genpd
= dev_to_genpd(dev
);
1052 if (genpd
->suspend_power_off
1053 || (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
)))
1057 * Since all of the "noirq" callbacks are executed sequentially, it is
1058 * guaranteed that this function will never run twice in parallel for
1059 * the same PM domain, so it is not necessary to use locking here.
1061 pm_genpd_sync_poweron(genpd
);
1062 genpd
->suspended_count
--;
1064 return genpd_start_dev(genpd
, dev
);
1068 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1069 * @dev: Device to resume.
1071 * Carry out an early resume of a device under the assumption that its
1072 * pm_domain field points to the domain member of an object of type
1073 * struct generic_pm_domain representing a power domain consisting of I/O
1076 static int pm_genpd_resume_early(struct device
*dev
)
1078 struct generic_pm_domain
*genpd
;
1080 dev_dbg(dev
, "%s()\n", __func__
);
1082 genpd
= dev_to_genpd(dev
);
1086 return genpd
->suspend_power_off
? 0 : genpd_resume_early(genpd
, dev
);
1090 * pm_genpd_resume - Resume of device in an I/O PM domain.
1091 * @dev: Device to resume.
1093 * Resume a device under the assumption that its pm_domain field points to the
1094 * domain member of an object of type struct generic_pm_domain representing
1095 * a power domain consisting of I/O devices.
1097 static int pm_genpd_resume(struct device
*dev
)
1099 struct generic_pm_domain
*genpd
;
1101 dev_dbg(dev
, "%s()\n", __func__
);
1103 genpd
= dev_to_genpd(dev
);
1107 return genpd
->suspend_power_off
? 0 : genpd_resume_dev(genpd
, dev
);
1111 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1112 * @dev: Device to freeze.
1114 * Freeze a device under the assumption that its pm_domain field points to the
1115 * domain member of an object of type struct generic_pm_domain representing
1116 * a power domain consisting of I/O devices.
1118 static int pm_genpd_freeze(struct device
*dev
)
1120 struct generic_pm_domain
*genpd
;
1122 dev_dbg(dev
, "%s()\n", __func__
);
1124 genpd
= dev_to_genpd(dev
);
1128 return genpd
->suspend_power_off
? 0 : genpd_freeze_dev(genpd
, dev
);
1132 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1133 * @dev: Device to freeze.
1135 * Carry out a late freeze of a device under the assumption that its
1136 * pm_domain field points to the domain member of an object of type
1137 * struct generic_pm_domain representing a power domain consisting of I/O
1140 static int pm_genpd_freeze_late(struct device
*dev
)
1142 struct generic_pm_domain
*genpd
;
1144 dev_dbg(dev
, "%s()\n", __func__
);
1146 genpd
= dev_to_genpd(dev
);
1150 return genpd
->suspend_power_off
? 0 : genpd_freeze_late(genpd
, dev
);
1154 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1155 * @dev: Device to freeze.
1157 * Carry out a late freeze of a device under the assumption that its
1158 * pm_domain field points to the domain member of an object of type
1159 * struct generic_pm_domain representing a power domain consisting of I/O
1162 static int pm_genpd_freeze_noirq(struct device
*dev
)
1164 struct generic_pm_domain
*genpd
;
1166 dev_dbg(dev
, "%s()\n", __func__
);
1168 genpd
= dev_to_genpd(dev
);
1172 return genpd
->suspend_power_off
? 0 : genpd_stop_dev(genpd
, dev
);
1176 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1177 * @dev: Device to thaw.
1179 * Start the device, unless power has been removed from the domain already
1180 * before the system transition.
1182 static int pm_genpd_thaw_noirq(struct device
*dev
)
1184 struct generic_pm_domain
*genpd
;
1186 dev_dbg(dev
, "%s()\n", __func__
);
1188 genpd
= dev_to_genpd(dev
);
1192 return genpd
->suspend_power_off
? 0 : genpd_start_dev(genpd
, dev
);
1196 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1197 * @dev: Device to thaw.
1199 * Carry out an early thaw of a device under the assumption that its
1200 * pm_domain field points to the domain member of an object of type
1201 * struct generic_pm_domain representing a power domain consisting of I/O
1204 static int pm_genpd_thaw_early(struct device
*dev
)
1206 struct generic_pm_domain
*genpd
;
1208 dev_dbg(dev
, "%s()\n", __func__
);
1210 genpd
= dev_to_genpd(dev
);
1214 return genpd
->suspend_power_off
? 0 : genpd_thaw_early(genpd
, dev
);
1218 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1219 * @dev: Device to thaw.
1221 * Thaw a device under the assumption that its pm_domain field points to the
1222 * domain member of an object of type struct generic_pm_domain representing
1223 * a power domain consisting of I/O devices.
1225 static int pm_genpd_thaw(struct device
*dev
)
1227 struct generic_pm_domain
*genpd
;
1229 dev_dbg(dev
, "%s()\n", __func__
);
1231 genpd
= dev_to_genpd(dev
);
1235 return genpd
->suspend_power_off
? 0 : genpd_thaw_dev(genpd
, dev
);
1239 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1240 * @dev: Device to resume.
1242 * Make sure the domain will be in the same power state as before the
1243 * hibernation the system is resuming from and start the device if necessary.
1245 static int pm_genpd_restore_noirq(struct device
*dev
)
1247 struct generic_pm_domain
*genpd
;
1249 dev_dbg(dev
, "%s()\n", __func__
);
1251 genpd
= dev_to_genpd(dev
);
1256 * Since all of the "noirq" callbacks are executed sequentially, it is
1257 * guaranteed that this function will never run twice in parallel for
1258 * the same PM domain, so it is not necessary to use locking here.
1260 * At this point suspended_count == 0 means we are being run for the
1261 * first time for the given domain in the present cycle.
1263 if (genpd
->suspended_count
++ == 0) {
1265 * The boot kernel might put the domain into arbitrary state,
1266 * so make it appear as powered off to pm_genpd_sync_poweron(),
1267 * so that it tries to power it on in case it was really off.
1269 genpd
->status
= GPD_STATE_POWER_OFF
;
1270 if (genpd
->suspend_power_off
) {
1272 * If the domain was off before the hibernation, make
1273 * sure it will be off going forward.
1275 if (genpd
->power_off
)
1276 genpd
->power_off(genpd
);
1282 if (genpd
->suspend_power_off
)
1285 pm_genpd_sync_poweron(genpd
);
1287 return genpd_start_dev(genpd
, dev
);
1291 * pm_genpd_complete - Complete power transition of a device in a power domain.
1292 * @dev: Device to complete the transition of.
1294 * Complete a power transition of a device (during a system-wide power
1295 * transition) under the assumption that its pm_domain field points to the
1296 * domain member of an object of type struct generic_pm_domain representing
1297 * a power domain consisting of I/O devices.
1299 static void pm_genpd_complete(struct device
*dev
)
1301 struct generic_pm_domain
*genpd
;
1304 dev_dbg(dev
, "%s()\n", __func__
);
1306 genpd
= dev_to_genpd(dev
);
1310 mutex_lock(&genpd
->lock
);
1312 run_complete
= !genpd
->suspend_power_off
;
1313 if (--genpd
->prepared_count
== 0)
1314 genpd
->suspend_power_off
= false;
1316 mutex_unlock(&genpd
->lock
);
1319 pm_generic_complete(dev
);
1320 pm_runtime_set_active(dev
);
1321 pm_runtime_enable(dev
);
1322 pm_runtime_idle(dev
);
1327 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1328 * @dev: Device that normally is marked as "always on" to switch power for.
1330 * This routine may only be called during the system core (syscore) suspend or
1331 * resume phase for devices whose "always on" flags are set.
1333 void pm_genpd_syscore_switch(struct device
*dev
, bool suspend
)
1335 struct generic_pm_domain
*genpd
;
1337 genpd
= dev_to_genpd(dev
);
1338 if (!pm_genpd_present(genpd
))
1342 genpd
->suspended_count
++;
1343 pm_genpd_sync_poweroff(genpd
);
1345 pm_genpd_sync_poweron(genpd
);
1346 genpd
->suspended_count
--;
1349 EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch
);
1353 #define pm_genpd_prepare NULL
1354 #define pm_genpd_suspend NULL
1355 #define pm_genpd_suspend_late NULL
1356 #define pm_genpd_suspend_noirq NULL
1357 #define pm_genpd_resume_early NULL
1358 #define pm_genpd_resume_noirq NULL
1359 #define pm_genpd_resume NULL
1360 #define pm_genpd_freeze NULL
1361 #define pm_genpd_freeze_late NULL
1362 #define pm_genpd_freeze_noirq NULL
1363 #define pm_genpd_thaw_early NULL
1364 #define pm_genpd_thaw_noirq NULL
1365 #define pm_genpd_thaw NULL
1366 #define pm_genpd_restore_noirq NULL
1367 #define pm_genpd_complete NULL
1369 #endif /* CONFIG_PM_SLEEP */
1371 static struct generic_pm_domain_data
*__pm_genpd_alloc_dev_data(struct device
*dev
)
1373 struct generic_pm_domain_data
*gpd_data
;
1375 gpd_data
= kzalloc(sizeof(*gpd_data
), GFP_KERNEL
);
1379 mutex_init(&gpd_data
->lock
);
1380 gpd_data
->nb
.notifier_call
= genpd_dev_pm_qos_notifier
;
1381 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1385 static void __pm_genpd_free_dev_data(struct device
*dev
,
1386 struct generic_pm_domain_data
*gpd_data
)
1388 dev_pm_qos_remove_notifier(dev
, &gpd_data
->nb
);
1393 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1394 * @genpd: PM domain to add the device to.
1395 * @dev: Device to be added.
1396 * @td: Set of PM QoS timing parameters to attach to the device.
1398 int __pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1399 struct gpd_timing_data
*td
)
1401 struct generic_pm_domain_data
*gpd_data_new
, *gpd_data
= NULL
;
1402 struct pm_domain_data
*pdd
;
1405 dev_dbg(dev
, "%s()\n", __func__
);
1407 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
))
1410 gpd_data_new
= __pm_genpd_alloc_dev_data(dev
);
1414 genpd_acquire_lock(genpd
);
1416 if (genpd
->prepared_count
> 0) {
1421 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
)
1422 if (pdd
->dev
== dev
) {
1427 ret
= dev_pm_get_subsys_data(dev
);
1431 genpd
->device_count
++;
1432 genpd
->max_off_time_changed
= true;
1434 spin_lock_irq(&dev
->power
.lock
);
1436 dev
->pm_domain
= &genpd
->domain
;
1437 if (dev
->power
.subsys_data
->domain_data
) {
1438 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
1440 gpd_data
= gpd_data_new
;
1441 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1443 gpd_data
->refcount
++;
1447 spin_unlock_irq(&dev
->power
.lock
);
1449 mutex_lock(&gpd_data
->lock
);
1450 gpd_data
->base
.dev
= dev
;
1451 list_add_tail(&gpd_data
->base
.list_node
, &genpd
->dev_list
);
1452 gpd_data
->need_restore
= genpd
->status
== GPD_STATE_POWER_OFF
;
1453 gpd_data
->td
.constraint_changed
= true;
1454 gpd_data
->td
.effective_constraint_ns
= -1;
1455 mutex_unlock(&gpd_data
->lock
);
1458 genpd_release_lock(genpd
);
1460 if (gpd_data
!= gpd_data_new
)
1461 __pm_genpd_free_dev_data(dev
, gpd_data_new
);
1467 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1468 * @genpd_node: Device tree node pointer representing a PM domain to which the
1469 * the device is added to.
1470 * @dev: Device to be added.
1471 * @td: Set of PM QoS timing parameters to attach to the device.
1473 int __pm_genpd_of_add_device(struct device_node
*genpd_node
, struct device
*dev
,
1474 struct gpd_timing_data
*td
)
1476 struct generic_pm_domain
*genpd
= NULL
, *gpd
;
1478 dev_dbg(dev
, "%s()\n", __func__
);
1480 if (IS_ERR_OR_NULL(genpd_node
) || IS_ERR_OR_NULL(dev
))
1483 mutex_lock(&gpd_list_lock
);
1484 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
1485 if (gpd
->of_node
== genpd_node
) {
1490 mutex_unlock(&gpd_list_lock
);
1495 return __pm_genpd_add_device(genpd
, dev
, td
);
1500 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1501 * @domain_name: Name of the PM domain to add the device to.
1502 * @dev: Device to be added.
1503 * @td: Set of PM QoS timing parameters to attach to the device.
1505 int __pm_genpd_name_add_device(const char *domain_name
, struct device
*dev
,
1506 struct gpd_timing_data
*td
)
1508 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name
), dev
, td
);
1512 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1513 * @genpd: PM domain to remove the device from.
1514 * @dev: Device to be removed.
1516 int pm_genpd_remove_device(struct generic_pm_domain
*genpd
,
1519 struct generic_pm_domain_data
*gpd_data
;
1520 struct pm_domain_data
*pdd
;
1521 bool remove
= false;
1524 dev_dbg(dev
, "%s()\n", __func__
);
1526 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
)
1527 || IS_ERR_OR_NULL(dev
->pm_domain
)
1528 || pd_to_genpd(dev
->pm_domain
) != genpd
)
1531 genpd_acquire_lock(genpd
);
1533 if (genpd
->prepared_count
> 0) {
1538 genpd
->device_count
--;
1539 genpd
->max_off_time_changed
= true;
1541 spin_lock_irq(&dev
->power
.lock
);
1543 dev
->pm_domain
= NULL
;
1544 pdd
= dev
->power
.subsys_data
->domain_data
;
1545 list_del_init(&pdd
->list_node
);
1546 gpd_data
= to_gpd_data(pdd
);
1547 if (--gpd_data
->refcount
== 0) {
1548 dev
->power
.subsys_data
->domain_data
= NULL
;
1552 spin_unlock_irq(&dev
->power
.lock
);
1554 mutex_lock(&gpd_data
->lock
);
1556 mutex_unlock(&gpd_data
->lock
);
1558 genpd_release_lock(genpd
);
1560 dev_pm_put_subsys_data(dev
);
1562 __pm_genpd_free_dev_data(dev
, gpd_data
);
1567 genpd_release_lock(genpd
);
1573 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1574 * @dev: Device to set/unset the flag for.
1575 * @val: The new value of the device's "need restore" flag.
1577 void pm_genpd_dev_need_restore(struct device
*dev
, bool val
)
1579 struct pm_subsys_data
*psd
;
1580 unsigned long flags
;
1582 spin_lock_irqsave(&dev
->power
.lock
, flags
);
1584 psd
= dev_to_psd(dev
);
1585 if (psd
&& psd
->domain_data
)
1586 to_gpd_data(psd
->domain_data
)->need_restore
= val
;
1588 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
1590 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore
);
1593 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1594 * @genpd: Master PM domain to add the subdomain to.
1595 * @subdomain: Subdomain to be added.
1597 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1598 struct generic_pm_domain
*subdomain
)
1600 struct gpd_link
*link
;
1603 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
)
1604 || genpd
== subdomain
)
1608 genpd_acquire_lock(genpd
);
1609 mutex_lock_nested(&subdomain
->lock
, SINGLE_DEPTH_NESTING
);
1611 if (subdomain
->status
!= GPD_STATE_POWER_OFF
1612 && subdomain
->status
!= GPD_STATE_ACTIVE
) {
1613 mutex_unlock(&subdomain
->lock
);
1614 genpd_release_lock(genpd
);
1618 if (genpd
->status
== GPD_STATE_POWER_OFF
1619 && subdomain
->status
!= GPD_STATE_POWER_OFF
) {
1624 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
1625 if (link
->slave
== subdomain
&& link
->master
== genpd
) {
1631 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1636 link
->master
= genpd
;
1637 list_add_tail(&link
->master_node
, &genpd
->master_links
);
1638 link
->slave
= subdomain
;
1639 list_add_tail(&link
->slave_node
, &subdomain
->slave_links
);
1640 if (subdomain
->status
!= GPD_STATE_POWER_OFF
)
1641 genpd_sd_counter_inc(genpd
);
1644 mutex_unlock(&subdomain
->lock
);
1645 genpd_release_lock(genpd
);
1651 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1652 * @master_name: Name of the master PM domain to add the subdomain to.
1653 * @subdomain_name: Name of the subdomain to be added.
1655 int pm_genpd_add_subdomain_names(const char *master_name
,
1656 const char *subdomain_name
)
1658 struct generic_pm_domain
*master
= NULL
, *subdomain
= NULL
, *gpd
;
1660 if (IS_ERR_OR_NULL(master_name
) || IS_ERR_OR_NULL(subdomain_name
))
1663 mutex_lock(&gpd_list_lock
);
1664 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
1665 if (!master
&& !strcmp(gpd
->name
, master_name
))
1668 if (!subdomain
&& !strcmp(gpd
->name
, subdomain_name
))
1671 if (master
&& subdomain
)
1674 mutex_unlock(&gpd_list_lock
);
1676 return pm_genpd_add_subdomain(master
, subdomain
);
1680 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1681 * @genpd: Master PM domain to remove the subdomain from.
1682 * @subdomain: Subdomain to be removed.
1684 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
1685 struct generic_pm_domain
*subdomain
)
1687 struct gpd_link
*link
;
1690 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
))
1694 genpd_acquire_lock(genpd
);
1696 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
1697 if (link
->slave
!= subdomain
)
1700 mutex_lock_nested(&subdomain
->lock
, SINGLE_DEPTH_NESTING
);
1702 if (subdomain
->status
!= GPD_STATE_POWER_OFF
1703 && subdomain
->status
!= GPD_STATE_ACTIVE
) {
1704 mutex_unlock(&subdomain
->lock
);
1705 genpd_release_lock(genpd
);
1709 list_del(&link
->master_node
);
1710 list_del(&link
->slave_node
);
1712 if (subdomain
->status
!= GPD_STATE_POWER_OFF
)
1713 genpd_sd_counter_dec(genpd
);
1715 mutex_unlock(&subdomain
->lock
);
1721 genpd_release_lock(genpd
);
1727 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1728 * @dev: Device to add the callbacks to.
1729 * @ops: Set of callbacks to add.
1730 * @td: Timing data to add to the device along with the callbacks (optional).
1732 * Every call to this routine should be balanced with a call to
1733 * __pm_genpd_remove_callbacks() and they must not be nested.
1735 int pm_genpd_add_callbacks(struct device
*dev
, struct gpd_dev_ops
*ops
,
1736 struct gpd_timing_data
*td
)
1738 struct generic_pm_domain_data
*gpd_data_new
, *gpd_data
= NULL
;
1744 gpd_data_new
= __pm_genpd_alloc_dev_data(dev
);
1748 pm_runtime_disable(dev
);
1751 ret
= dev_pm_get_subsys_data(dev
);
1755 spin_lock_irq(&dev
->power
.lock
);
1757 if (dev
->power
.subsys_data
->domain_data
) {
1758 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
1760 gpd_data
= gpd_data_new
;
1761 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1763 gpd_data
->refcount
++;
1764 gpd_data
->ops
= *ops
;
1768 spin_unlock_irq(&dev
->power
.lock
);
1772 pm_runtime_enable(dev
);
1774 if (gpd_data
!= gpd_data_new
)
1775 __pm_genpd_free_dev_data(dev
, gpd_data_new
);
1779 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks
);
1782 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1783 * @dev: Device to remove the callbacks from.
1784 * @clear_td: If set, clear the device's timing data too.
1786 * This routine can only be called after pm_genpd_add_callbacks().
1788 int __pm_genpd_remove_callbacks(struct device
*dev
, bool clear_td
)
1790 struct generic_pm_domain_data
*gpd_data
= NULL
;
1791 bool remove
= false;
1794 if (!(dev
&& dev
->power
.subsys_data
))
1797 pm_runtime_disable(dev
);
1800 spin_lock_irq(&dev
->power
.lock
);
1802 if (dev
->power
.subsys_data
->domain_data
) {
1803 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
1804 gpd_data
->ops
= (struct gpd_dev_ops
){ NULL
};
1806 gpd_data
->td
= (struct gpd_timing_data
){ 0 };
1808 if (--gpd_data
->refcount
== 0) {
1809 dev
->power
.subsys_data
->domain_data
= NULL
;
1816 spin_unlock_irq(&dev
->power
.lock
);
1819 pm_runtime_enable(dev
);
1824 dev_pm_put_subsys_data(dev
);
1826 __pm_genpd_free_dev_data(dev
, gpd_data
);
1830 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks
);
1833 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1834 * @genpd: PM domain to be connected with cpuidle.
1835 * @state: cpuidle state this domain can disable/enable.
1837 * Make a PM domain behave as though it contained a CPU core, that is, instead
1838 * of calling its power down routine it will enable the given cpuidle state so
1839 * that the cpuidle subsystem can power it down (if possible and desirable).
1841 int pm_genpd_attach_cpuidle(struct generic_pm_domain
*genpd
, int state
)
1843 struct cpuidle_driver
*cpuidle_drv
;
1844 struct gpd_cpu_data
*cpu_data
;
1845 struct cpuidle_state
*idle_state
;
1848 if (IS_ERR_OR_NULL(genpd
) || state
< 0)
1851 genpd_acquire_lock(genpd
);
1853 if (genpd
->cpu_data
) {
1857 cpu_data
= kzalloc(sizeof(*cpu_data
), GFP_KERNEL
);
1862 cpuidle_drv
= cpuidle_driver_ref();
1867 if (cpuidle_drv
->state_count
<= state
) {
1871 idle_state
= &cpuidle_drv
->states
[state
];
1872 if (!idle_state
->disabled
) {
1876 cpu_data
->idle_state
= idle_state
;
1877 cpu_data
->saved_exit_latency
= idle_state
->exit_latency
;
1878 genpd
->cpu_data
= cpu_data
;
1879 genpd_recalc_cpu_exit_latency(genpd
);
1882 genpd_release_lock(genpd
);
1886 cpuidle_driver_unref();
1894 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1895 * @name: Name of the domain to connect to cpuidle.
1896 * @state: cpuidle state this domain can manipulate.
1898 int pm_genpd_name_attach_cpuidle(const char *name
, int state
)
1900 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name
), state
);
1904 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1905 * @genpd: PM domain to remove the cpuidle connection from.
1907 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1910 int pm_genpd_detach_cpuidle(struct generic_pm_domain
*genpd
)
1912 struct gpd_cpu_data
*cpu_data
;
1913 struct cpuidle_state
*idle_state
;
1916 if (IS_ERR_OR_NULL(genpd
))
1919 genpd_acquire_lock(genpd
);
1921 cpu_data
= genpd
->cpu_data
;
1926 idle_state
= cpu_data
->idle_state
;
1927 if (!idle_state
->disabled
) {
1931 idle_state
->exit_latency
= cpu_data
->saved_exit_latency
;
1932 cpuidle_driver_unref();
1933 genpd
->cpu_data
= NULL
;
1937 genpd_release_lock(genpd
);
1942 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1943 * @name: Name of the domain to disconnect cpuidle from.
1945 int pm_genpd_name_detach_cpuidle(const char *name
)
1947 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name
));
1950 /* Default device callbacks for generic PM domains. */
1953 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1954 * @dev: Device to handle.
1956 static int pm_genpd_default_save_state(struct device
*dev
)
1958 int (*cb
)(struct device
*__dev
);
1960 cb
= dev_gpd_data(dev
)->ops
.save_state
;
1964 if (dev
->type
&& dev
->type
->pm
)
1965 cb
= dev
->type
->pm
->runtime_suspend
;
1966 else if (dev
->class && dev
->class->pm
)
1967 cb
= dev
->class->pm
->runtime_suspend
;
1968 else if (dev
->bus
&& dev
->bus
->pm
)
1969 cb
= dev
->bus
->pm
->runtime_suspend
;
1973 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
1974 cb
= dev
->driver
->pm
->runtime_suspend
;
1976 return cb
? cb(dev
) : 0;
1980 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1981 * @dev: Device to handle.
1983 static int pm_genpd_default_restore_state(struct device
*dev
)
1985 int (*cb
)(struct device
*__dev
);
1987 cb
= dev_gpd_data(dev
)->ops
.restore_state
;
1991 if (dev
->type
&& dev
->type
->pm
)
1992 cb
= dev
->type
->pm
->runtime_resume
;
1993 else if (dev
->class && dev
->class->pm
)
1994 cb
= dev
->class->pm
->runtime_resume
;
1995 else if (dev
->bus
&& dev
->bus
->pm
)
1996 cb
= dev
->bus
->pm
->runtime_resume
;
2000 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
2001 cb
= dev
->driver
->pm
->runtime_resume
;
2003 return cb
? cb(dev
) : 0;
2006 #ifdef CONFIG_PM_SLEEP
2009 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2010 * @dev: Device to handle.
2012 static int pm_genpd_default_suspend(struct device
*dev
)
2014 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.suspend
;
2016 return cb
? cb(dev
) : pm_generic_suspend(dev
);
2020 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2021 * @dev: Device to handle.
2023 static int pm_genpd_default_suspend_late(struct device
*dev
)
2025 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.suspend_late
;
2027 return cb
? cb(dev
) : pm_generic_suspend_late(dev
);
2031 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2032 * @dev: Device to handle.
2034 static int pm_genpd_default_resume_early(struct device
*dev
)
2036 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.resume_early
;
2038 return cb
? cb(dev
) : pm_generic_resume_early(dev
);
2042 * pm_genpd_default_resume - Default "device resume" for PM domians.
2043 * @dev: Device to handle.
2045 static int pm_genpd_default_resume(struct device
*dev
)
2047 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.resume
;
2049 return cb
? cb(dev
) : pm_generic_resume(dev
);
2053 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2054 * @dev: Device to handle.
2056 static int pm_genpd_default_freeze(struct device
*dev
)
2058 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.freeze
;
2060 return cb
? cb(dev
) : pm_generic_freeze(dev
);
2064 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2065 * @dev: Device to handle.
2067 static int pm_genpd_default_freeze_late(struct device
*dev
)
2069 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.freeze_late
;
2071 return cb
? cb(dev
) : pm_generic_freeze_late(dev
);
2075 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2076 * @dev: Device to handle.
2078 static int pm_genpd_default_thaw_early(struct device
*dev
)
2080 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.thaw_early
;
2082 return cb
? cb(dev
) : pm_generic_thaw_early(dev
);
2086 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2087 * @dev: Device to handle.
2089 static int pm_genpd_default_thaw(struct device
*dev
)
2091 int (*cb
)(struct device
*__dev
) = dev_gpd_data(dev
)->ops
.thaw
;
2093 return cb
? cb(dev
) : pm_generic_thaw(dev
);
2096 #else /* !CONFIG_PM_SLEEP */
2098 #define pm_genpd_default_suspend NULL
2099 #define pm_genpd_default_suspend_late NULL
2100 #define pm_genpd_default_resume_early NULL
2101 #define pm_genpd_default_resume NULL
2102 #define pm_genpd_default_freeze NULL
2103 #define pm_genpd_default_freeze_late NULL
2104 #define pm_genpd_default_thaw_early NULL
2105 #define pm_genpd_default_thaw NULL
2107 #endif /* !CONFIG_PM_SLEEP */
2110 * pm_genpd_init - Initialize a generic I/O PM domain object.
2111 * @genpd: PM domain object to initialize.
2112 * @gov: PM domain governor to associate with the domain (may be NULL).
2113 * @is_off: Initial value of the domain's power_is_off field.
2115 void pm_genpd_init(struct generic_pm_domain
*genpd
,
2116 struct dev_power_governor
*gov
, bool is_off
)
2118 if (IS_ERR_OR_NULL(genpd
))
2121 INIT_LIST_HEAD(&genpd
->master_links
);
2122 INIT_LIST_HEAD(&genpd
->slave_links
);
2123 INIT_LIST_HEAD(&genpd
->dev_list
);
2124 mutex_init(&genpd
->lock
);
2126 INIT_WORK(&genpd
->power_off_work
, genpd_power_off_work_fn
);
2127 genpd
->in_progress
= 0;
2128 atomic_set(&genpd
->sd_count
, 0);
2129 genpd
->status
= is_off
? GPD_STATE_POWER_OFF
: GPD_STATE_ACTIVE
;
2130 init_waitqueue_head(&genpd
->status_wait_queue
);
2131 genpd
->poweroff_task
= NULL
;
2132 genpd
->resume_count
= 0;
2133 genpd
->device_count
= 0;
2134 genpd
->max_off_time_ns
= -1;
2135 genpd
->max_off_time_changed
= true;
2136 genpd
->domain
.ops
.runtime_suspend
= pm_genpd_runtime_suspend
;
2137 genpd
->domain
.ops
.runtime_resume
= pm_genpd_runtime_resume
;
2138 genpd
->domain
.ops
.runtime_idle
= pm_generic_runtime_idle
;
2139 genpd
->domain
.ops
.prepare
= pm_genpd_prepare
;
2140 genpd
->domain
.ops
.suspend
= pm_genpd_suspend
;
2141 genpd
->domain
.ops
.suspend_late
= pm_genpd_suspend_late
;
2142 genpd
->domain
.ops
.suspend_noirq
= pm_genpd_suspend_noirq
;
2143 genpd
->domain
.ops
.resume_noirq
= pm_genpd_resume_noirq
;
2144 genpd
->domain
.ops
.resume_early
= pm_genpd_resume_early
;
2145 genpd
->domain
.ops
.resume
= pm_genpd_resume
;
2146 genpd
->domain
.ops
.freeze
= pm_genpd_freeze
;
2147 genpd
->domain
.ops
.freeze_late
= pm_genpd_freeze_late
;
2148 genpd
->domain
.ops
.freeze_noirq
= pm_genpd_freeze_noirq
;
2149 genpd
->domain
.ops
.thaw_noirq
= pm_genpd_thaw_noirq
;
2150 genpd
->domain
.ops
.thaw_early
= pm_genpd_thaw_early
;
2151 genpd
->domain
.ops
.thaw
= pm_genpd_thaw
;
2152 genpd
->domain
.ops
.poweroff
= pm_genpd_suspend
;
2153 genpd
->domain
.ops
.poweroff_late
= pm_genpd_suspend_late
;
2154 genpd
->domain
.ops
.poweroff_noirq
= pm_genpd_suspend_noirq
;
2155 genpd
->domain
.ops
.restore_noirq
= pm_genpd_restore_noirq
;
2156 genpd
->domain
.ops
.restore_early
= pm_genpd_resume_early
;
2157 genpd
->domain
.ops
.restore
= pm_genpd_resume
;
2158 genpd
->domain
.ops
.complete
= pm_genpd_complete
;
2159 genpd
->dev_ops
.save_state
= pm_genpd_default_save_state
;
2160 genpd
->dev_ops
.restore_state
= pm_genpd_default_restore_state
;
2161 genpd
->dev_ops
.suspend
= pm_genpd_default_suspend
;
2162 genpd
->dev_ops
.suspend_late
= pm_genpd_default_suspend_late
;
2163 genpd
->dev_ops
.resume_early
= pm_genpd_default_resume_early
;
2164 genpd
->dev_ops
.resume
= pm_genpd_default_resume
;
2165 genpd
->dev_ops
.freeze
= pm_genpd_default_freeze
;
2166 genpd
->dev_ops
.freeze_late
= pm_genpd_default_freeze_late
;
2167 genpd
->dev_ops
.thaw_early
= pm_genpd_default_thaw_early
;
2168 genpd
->dev_ops
.thaw
= pm_genpd_default_thaw
;
2169 mutex_lock(&gpd_list_lock
);
2170 list_add(&genpd
->gpd_list_node
, &gpd_list
);
2171 mutex_unlock(&gpd_list_lock
);