2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/kernel.h>
11 #include <linux/platform_device.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); \
33 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
35 ktime_t __start = ktime_get(); \
36 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
37 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
38 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
39 if (!__retval && __elapsed > __td->field) { \
40 __td->field = __elapsed; \
41 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
43 genpd->max_off_time_changed = true; \
44 __td->constraint_changed = true; \
49 static LIST_HEAD(gpd_list
);
50 static DEFINE_MUTEX(gpd_list_lock
);
52 static struct generic_pm_domain
*pm_genpd_lookup_name(const char *domain_name
)
54 struct generic_pm_domain
*genpd
= NULL
, *gpd
;
56 if (IS_ERR_OR_NULL(domain_name
))
59 mutex_lock(&gpd_list_lock
);
60 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
61 if (!strcmp(gpd
->name
, domain_name
)) {
66 mutex_unlock(&gpd_list_lock
);
70 struct generic_pm_domain
*dev_to_genpd(struct device
*dev
)
72 if (IS_ERR_OR_NULL(dev
->pm_domain
))
73 return ERR_PTR(-EINVAL
);
75 return pd_to_genpd(dev
->pm_domain
);
78 static int genpd_stop_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
80 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, stop
, dev
,
81 stop_latency_ns
, "stop");
84 static int genpd_start_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
86 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, start
, dev
,
87 start_latency_ns
, "start");
90 static bool genpd_sd_counter_dec(struct generic_pm_domain
*genpd
)
94 if (!WARN_ON(atomic_read(&genpd
->sd_count
) == 0))
95 ret
= !!atomic_dec_and_test(&genpd
->sd_count
);
100 static void genpd_sd_counter_inc(struct generic_pm_domain
*genpd
)
102 atomic_inc(&genpd
->sd_count
);
103 smp_mb__after_atomic();
106 static void genpd_acquire_lock(struct generic_pm_domain
*genpd
)
110 mutex_lock(&genpd
->lock
);
112 * Wait for the domain to transition into either the active,
113 * or the power off state.
116 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
117 TASK_UNINTERRUPTIBLE
);
118 if (genpd
->status
== GPD_STATE_ACTIVE
119 || genpd
->status
== GPD_STATE_POWER_OFF
)
121 mutex_unlock(&genpd
->lock
);
125 mutex_lock(&genpd
->lock
);
127 finish_wait(&genpd
->status_wait_queue
, &wait
);
130 static void genpd_release_lock(struct generic_pm_domain
*genpd
)
132 mutex_unlock(&genpd
->lock
);
135 static void genpd_set_active(struct generic_pm_domain
*genpd
)
137 if (genpd
->resume_count
== 0)
138 genpd
->status
= GPD_STATE_ACTIVE
;
141 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain
*genpd
)
145 if (!genpd
->cpuidle_data
)
148 usecs64
= genpd
->power_on_latency_ns
;
149 do_div(usecs64
, NSEC_PER_USEC
);
150 usecs64
+= genpd
->cpuidle_data
->saved_exit_latency
;
151 genpd
->cpuidle_data
->idle_state
->exit_latency
= usecs64
;
155 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
156 * @genpd: PM domain to power up.
158 * Restore power to @genpd and all of its masters so that it is possible to
159 * resume a device belonging to it.
161 static int __pm_genpd_poweron(struct generic_pm_domain
*genpd
)
162 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
164 struct gpd_link
*link
;
168 /* If the domain's master is being waited for, we have to wait too. */
170 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
171 TASK_UNINTERRUPTIBLE
);
172 if (genpd
->status
!= GPD_STATE_WAIT_MASTER
)
174 mutex_unlock(&genpd
->lock
);
178 mutex_lock(&genpd
->lock
);
180 finish_wait(&genpd
->status_wait_queue
, &wait
);
182 if (genpd
->status
== GPD_STATE_ACTIVE
183 || (genpd
->prepared_count
> 0 && genpd
->suspend_power_off
))
186 if (genpd
->status
!= GPD_STATE_POWER_OFF
) {
187 genpd_set_active(genpd
);
191 if (genpd
->cpuidle_data
) {
192 cpuidle_pause_and_lock();
193 genpd
->cpuidle_data
->idle_state
->disabled
= true;
194 cpuidle_resume_and_unlock();
199 * The list is guaranteed not to change while the loop below is being
200 * executed, unless one of the masters' .power_on() callbacks fiddles
203 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
204 genpd_sd_counter_inc(link
->master
);
205 genpd
->status
= GPD_STATE_WAIT_MASTER
;
207 mutex_unlock(&genpd
->lock
);
209 ret
= pm_genpd_poweron(link
->master
);
211 mutex_lock(&genpd
->lock
);
214 * The "wait for parent" status is guaranteed not to change
215 * while the master is powering on.
217 genpd
->status
= GPD_STATE_POWER_OFF
;
218 wake_up_all(&genpd
->status_wait_queue
);
220 genpd_sd_counter_dec(link
->master
);
225 if (genpd
->power_on
) {
226 ktime_t time_start
= ktime_get();
229 ret
= genpd
->power_on(genpd
);
233 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
234 if (elapsed_ns
> genpd
->power_on_latency_ns
) {
235 genpd
->power_on_latency_ns
= elapsed_ns
;
236 genpd
->max_off_time_changed
= true;
237 genpd_recalc_cpu_exit_latency(genpd
);
239 pr_warning("%s: Power-on latency exceeded, "
240 "new value %lld ns\n", genpd
->name
,
246 genpd_set_active(genpd
);
251 list_for_each_entry_continue_reverse(link
, &genpd
->slave_links
, slave_node
)
252 genpd_sd_counter_dec(link
->master
);
258 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
259 * @genpd: PM domain to power up.
261 int pm_genpd_poweron(struct generic_pm_domain
*genpd
)
265 mutex_lock(&genpd
->lock
);
266 ret
= __pm_genpd_poweron(genpd
);
267 mutex_unlock(&genpd
->lock
);
272 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
273 * @domain_name: Name of the PM domain to power up.
275 int pm_genpd_name_poweron(const char *domain_name
)
277 struct generic_pm_domain
*genpd
;
279 genpd
= pm_genpd_lookup_name(domain_name
);
280 return genpd
? pm_genpd_poweron(genpd
) : -EINVAL
;
283 #ifdef CONFIG_PM_RUNTIME
285 static int genpd_start_dev_no_timing(struct generic_pm_domain
*genpd
,
288 return GENPD_DEV_CALLBACK(genpd
, int, start
, dev
);
291 static int genpd_save_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
293 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, save_state
, dev
,
294 save_state_latency_ns
, "state save");
297 static int genpd_restore_dev(struct generic_pm_domain
*genpd
, struct device
*dev
)
299 return GENPD_DEV_TIMED_CALLBACK(genpd
, int, restore_state
, dev
,
300 restore_state_latency_ns
,
304 static int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
305 unsigned long val
, void *ptr
)
307 struct generic_pm_domain_data
*gpd_data
;
310 gpd_data
= container_of(nb
, struct generic_pm_domain_data
, nb
);
312 mutex_lock(&gpd_data
->lock
);
313 dev
= gpd_data
->base
.dev
;
315 mutex_unlock(&gpd_data
->lock
);
318 mutex_unlock(&gpd_data
->lock
);
321 struct generic_pm_domain
*genpd
;
322 struct pm_domain_data
*pdd
;
324 spin_lock_irq(&dev
->power
.lock
);
326 pdd
= dev
->power
.subsys_data
?
327 dev
->power
.subsys_data
->domain_data
: NULL
;
328 if (pdd
&& pdd
->dev
) {
329 to_gpd_data(pdd
)->td
.constraint_changed
= true;
330 genpd
= dev_to_genpd(dev
);
332 genpd
= ERR_PTR(-ENODATA
);
335 spin_unlock_irq(&dev
->power
.lock
);
337 if (!IS_ERR(genpd
)) {
338 mutex_lock(&genpd
->lock
);
339 genpd
->max_off_time_changed
= true;
340 mutex_unlock(&genpd
->lock
);
344 if (!dev
|| dev
->power
.ignore_children
)
352 * __pm_genpd_save_device - Save the pre-suspend state of a device.
353 * @pdd: Domain data of the device to save the state of.
354 * @genpd: PM domain the device belongs to.
356 static int __pm_genpd_save_device(struct pm_domain_data
*pdd
,
357 struct generic_pm_domain
*genpd
)
358 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
360 struct generic_pm_domain_data
*gpd_data
= to_gpd_data(pdd
);
361 struct device
*dev
= pdd
->dev
;
364 if (gpd_data
->need_restore
)
367 mutex_unlock(&genpd
->lock
);
369 genpd_start_dev(genpd
, dev
);
370 ret
= genpd_save_dev(genpd
, dev
);
371 genpd_stop_dev(genpd
, dev
);
373 mutex_lock(&genpd
->lock
);
376 gpd_data
->need_restore
= true;
382 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
383 * @pdd: Domain data of the device to restore the state of.
384 * @genpd: PM domain the device belongs to.
386 static void __pm_genpd_restore_device(struct pm_domain_data
*pdd
,
387 struct generic_pm_domain
*genpd
)
388 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
390 struct generic_pm_domain_data
*gpd_data
= to_gpd_data(pdd
);
391 struct device
*dev
= pdd
->dev
;
392 bool need_restore
= gpd_data
->need_restore
;
394 gpd_data
->need_restore
= false;
395 mutex_unlock(&genpd
->lock
);
397 genpd_start_dev(genpd
, dev
);
399 genpd_restore_dev(genpd
, dev
);
401 mutex_lock(&genpd
->lock
);
405 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
406 * @genpd: PM domain to check.
408 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
409 * a "power off" operation, which means that a "power on" has occured in the
410 * meantime, or if its resume_count field is different from zero, which means
411 * that one of its devices has been resumed in the meantime.
413 static bool genpd_abort_poweroff(struct generic_pm_domain
*genpd
)
415 return genpd
->status
== GPD_STATE_WAIT_MASTER
416 || genpd
->status
== GPD_STATE_ACTIVE
|| genpd
->resume_count
> 0;
420 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
421 * @genpd: PM domait to power off.
423 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
426 static void genpd_queue_power_off_work(struct generic_pm_domain
*genpd
)
428 queue_work(pm_wq
, &genpd
->power_off_work
);
432 * pm_genpd_poweroff - Remove power from a given PM domain.
433 * @genpd: PM domain to power down.
435 * If all of the @genpd's devices have been suspended and all of its subdomains
436 * have been powered down, run the runtime suspend callbacks provided by all of
437 * the @genpd's devices' drivers and remove power from @genpd.
439 static int pm_genpd_poweroff(struct generic_pm_domain
*genpd
)
440 __releases(&genpd
->lock
) __acquires(&genpd
->lock
)
442 struct pm_domain_data
*pdd
;
443 struct gpd_link
*link
;
444 unsigned int not_suspended
;
449 * Do not try to power off the domain in the following situations:
450 * (1) The domain is already in the "power off" state.
451 * (2) The domain is waiting for its master to power up.
452 * (3) One of the domain's devices is being resumed right now.
453 * (4) System suspend is in progress.
455 if (genpd
->status
== GPD_STATE_POWER_OFF
456 || genpd
->status
== GPD_STATE_WAIT_MASTER
457 || genpd
->resume_count
> 0 || genpd
->prepared_count
> 0)
460 if (atomic_read(&genpd
->sd_count
) > 0)
464 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
465 enum pm_qos_flags_status stat
;
467 stat
= dev_pm_qos_flags(pdd
->dev
,
468 PM_QOS_FLAG_NO_POWER_OFF
469 | PM_QOS_FLAG_REMOTE_WAKEUP
);
470 if (stat
> PM_QOS_FLAGS_NONE
)
473 if (pdd
->dev
->driver
&& (!pm_runtime_suspended(pdd
->dev
)
474 || 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
->cpuidle_data
) {
518 * If cpuidle_data is set, cpuidle should turn the domain off
519 * when the CPU in it is idle. In that case we don't decrement
520 * the subdomain counts of the master domains, so that power is
521 * not removed from the current domain prematurely as a result
522 * of cutting off the masters' power.
524 genpd
->status
= GPD_STATE_POWER_OFF
;
525 cpuidle_pause_and_lock();
526 genpd
->cpuidle_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 stop_ok
= genpd
->gov
? genpd
->gov
->stop_ok
: NULL
;
616 if (stop_ok
&& !stop_ok(dev
))
619 ret
= genpd_stop_dev(genpd
, dev
);
624 * If power.irq_safe is set, this routine will be run with interrupts
625 * off, so it can't use mutexes.
627 if (dev
->power
.irq_safe
)
630 mutex_lock(&genpd
->lock
);
631 genpd
->in_progress
++;
632 pm_genpd_poweroff(genpd
);
633 genpd
->in_progress
--;
634 mutex_unlock(&genpd
->lock
);
640 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
641 * @dev: Device to resume.
643 * Carry out a runtime resume of a device under the assumption that its
644 * pm_domain field points to the domain member of an object of type
645 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
647 static int pm_genpd_runtime_resume(struct device
*dev
)
649 struct generic_pm_domain
*genpd
;
653 dev_dbg(dev
, "%s()\n", __func__
);
655 genpd
= dev_to_genpd(dev
);
659 /* If power.irq_safe, the PM domain is never powered off. */
660 if (dev
->power
.irq_safe
)
661 return genpd_start_dev_no_timing(genpd
, dev
);
663 mutex_lock(&genpd
->lock
);
664 ret
= __pm_genpd_poweron(genpd
);
666 mutex_unlock(&genpd
->lock
);
669 genpd
->status
= GPD_STATE_BUSY
;
670 genpd
->resume_count
++;
672 prepare_to_wait(&genpd
->status_wait_queue
, &wait
,
673 TASK_UNINTERRUPTIBLE
);
675 * If current is the powering off task, we have been called
676 * reentrantly from one of the device callbacks, so we should
679 if (!genpd
->poweroff_task
|| genpd
->poweroff_task
== current
)
681 mutex_unlock(&genpd
->lock
);
685 mutex_lock(&genpd
->lock
);
687 finish_wait(&genpd
->status_wait_queue
, &wait
);
688 __pm_genpd_restore_device(dev
->power
.subsys_data
->domain_data
, genpd
);
689 genpd
->resume_count
--;
690 genpd_set_active(genpd
);
691 wake_up_all(&genpd
->status_wait_queue
);
692 mutex_unlock(&genpd
->lock
);
697 static bool pd_ignore_unused
;
698 static int __init
pd_ignore_unused_setup(char *__unused
)
700 pd_ignore_unused
= true;
703 __setup("pd_ignore_unused", pd_ignore_unused_setup
);
706 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
708 void pm_genpd_poweroff_unused(void)
710 struct generic_pm_domain
*genpd
;
712 if (pd_ignore_unused
) {
713 pr_warn("genpd: Not disabling unused power domains\n");
717 mutex_lock(&gpd_list_lock
);
719 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
)
720 genpd_queue_power_off_work(genpd
);
722 mutex_unlock(&gpd_list_lock
);
725 static int __init
genpd_poweroff_unused(void)
727 pm_genpd_poweroff_unused();
730 late_initcall(genpd_poweroff_unused
);
734 static inline int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
735 unsigned long val
, void *ptr
)
741 genpd_queue_power_off_work(struct generic_pm_domain
*genpd
) {}
743 static inline void genpd_power_off_work_fn(struct work_struct
*work
) {}
745 #define pm_genpd_runtime_suspend NULL
746 #define pm_genpd_runtime_resume NULL
748 #endif /* CONFIG_PM_RUNTIME */
750 #ifdef CONFIG_PM_SLEEP
753 * pm_genpd_present - Check if the given PM domain has been initialized.
754 * @genpd: PM domain to check.
756 static bool pm_genpd_present(struct generic_pm_domain
*genpd
)
758 struct generic_pm_domain
*gpd
;
760 if (IS_ERR_OR_NULL(genpd
))
763 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
770 static bool genpd_dev_active_wakeup(struct generic_pm_domain
*genpd
,
773 return GENPD_DEV_CALLBACK(genpd
, bool, active_wakeup
, dev
);
777 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
778 * @genpd: PM domain to power off, if possible.
780 * Check if the given PM domain can be powered off (during system suspend or
781 * hibernation) and do that if so. Also, in that case propagate to its masters.
783 * This function is only called in "noirq" and "syscore" stages of system power
784 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
785 * executed sequentially, so it is guaranteed that it will never run twice in
788 static void pm_genpd_sync_poweroff(struct generic_pm_domain
*genpd
)
790 struct gpd_link
*link
;
792 if (genpd
->status
== GPD_STATE_POWER_OFF
)
795 if (genpd
->suspended_count
!= genpd
->device_count
796 || atomic_read(&genpd
->sd_count
) > 0)
799 if (genpd
->power_off
)
800 genpd
->power_off(genpd
);
802 genpd
->status
= GPD_STATE_POWER_OFF
;
804 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
805 genpd_sd_counter_dec(link
->master
);
806 pm_genpd_sync_poweroff(link
->master
);
811 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
812 * @genpd: PM domain to power on.
814 * This function is only called in "noirq" and "syscore" stages of system power
815 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
816 * executed sequentially, so it is guaranteed that it will never run twice in
819 static void pm_genpd_sync_poweron(struct generic_pm_domain
*genpd
)
821 struct gpd_link
*link
;
823 if (genpd
->status
!= GPD_STATE_POWER_OFF
)
826 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
827 pm_genpd_sync_poweron(link
->master
);
828 genpd_sd_counter_inc(link
->master
);
832 genpd
->power_on(genpd
);
834 genpd
->status
= GPD_STATE_ACTIVE
;
838 * resume_needed - Check whether to resume a device before system suspend.
839 * @dev: Device to check.
840 * @genpd: PM domain the device belongs to.
842 * There are two cases in which a device that can wake up the system from sleep
843 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
844 * to wake up the system and it has to remain active for this purpose while the
845 * system is in the sleep state and (2) if the device is not enabled to wake up
846 * the system from sleep states and it generally doesn't generate wakeup signals
847 * by itself (those signals are generated on its behalf by other parts of the
848 * system). In the latter case it may be necessary to reconfigure the device's
849 * wakeup settings during system suspend, because it may have been set up to
850 * signal remote wakeup from the system's working state as needed by runtime PM.
851 * Return 'true' in either of the above cases.
853 static bool resume_needed(struct device
*dev
, struct generic_pm_domain
*genpd
)
857 if (!device_can_wakeup(dev
))
860 active_wakeup
= genpd_dev_active_wakeup(genpd
, dev
);
861 return device_may_wakeup(dev
) ? active_wakeup
: !active_wakeup
;
865 * pm_genpd_prepare - Start power transition of a device in a PM domain.
866 * @dev: Device to start the transition of.
868 * Start a power transition of a device (during a system-wide power transition)
869 * under the assumption that its pm_domain field points to the domain member of
870 * an object of type struct generic_pm_domain representing a PM domain
871 * consisting of I/O devices.
873 static int pm_genpd_prepare(struct device
*dev
)
875 struct generic_pm_domain
*genpd
;
878 dev_dbg(dev
, "%s()\n", __func__
);
880 genpd
= dev_to_genpd(dev
);
885 * If a wakeup request is pending for the device, it should be woken up
886 * at this point and a system wakeup event should be reported if it's
887 * set up to wake up the system from sleep states.
889 pm_runtime_get_noresume(dev
);
890 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
891 pm_wakeup_event(dev
, 0);
893 if (pm_wakeup_pending()) {
898 if (resume_needed(dev
, genpd
))
899 pm_runtime_resume(dev
);
901 genpd_acquire_lock(genpd
);
903 if (genpd
->prepared_count
++ == 0) {
904 genpd
->suspended_count
= 0;
905 genpd
->suspend_power_off
= genpd
->status
== GPD_STATE_POWER_OFF
;
908 genpd_release_lock(genpd
);
910 if (genpd
->suspend_power_off
) {
911 pm_runtime_put_noidle(dev
);
916 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
917 * so pm_genpd_poweron() will return immediately, but if the device
918 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
919 * to make it operational.
921 pm_runtime_resume(dev
);
922 __pm_runtime_disable(dev
, false);
924 ret
= pm_generic_prepare(dev
);
926 mutex_lock(&genpd
->lock
);
928 if (--genpd
->prepared_count
== 0)
929 genpd
->suspend_power_off
= false;
931 mutex_unlock(&genpd
->lock
);
932 pm_runtime_enable(dev
);
940 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
941 * @dev: Device to suspend.
943 * Suspend a device under the assumption that its pm_domain field points to the
944 * domain member of an object of type struct generic_pm_domain representing
945 * a PM domain consisting of I/O devices.
947 static int pm_genpd_suspend(struct device
*dev
)
949 struct generic_pm_domain
*genpd
;
951 dev_dbg(dev
, "%s()\n", __func__
);
953 genpd
= dev_to_genpd(dev
);
957 return genpd
->suspend_power_off
? 0 : pm_generic_suspend(dev
);
961 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
962 * @dev: Device to suspend.
964 * Carry out a late suspend of a device under the assumption that its
965 * pm_domain field points to the domain member of an object of type
966 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
968 static int pm_genpd_suspend_late(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 : pm_generic_suspend_late(dev
);
982 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
983 * @dev: Device to suspend.
985 * Stop the device and remove power from the domain if all devices in it have
988 static int pm_genpd_suspend_noirq(struct device
*dev
)
990 struct generic_pm_domain
*genpd
;
992 dev_dbg(dev
, "%s()\n", __func__
);
994 genpd
= dev_to_genpd(dev
);
998 if (genpd
->suspend_power_off
999 || (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
)))
1002 genpd_stop_dev(genpd
, dev
);
1005 * Since all of the "noirq" callbacks are executed sequentially, it is
1006 * guaranteed that this function will never run twice in parallel for
1007 * the same PM domain, so it is not necessary to use locking here.
1009 genpd
->suspended_count
++;
1010 pm_genpd_sync_poweroff(genpd
);
1016 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1017 * @dev: Device to resume.
1019 * Restore power to the device's PM domain, if necessary, and start the device.
1021 static int pm_genpd_resume_noirq(struct device
*dev
)
1023 struct generic_pm_domain
*genpd
;
1025 dev_dbg(dev
, "%s()\n", __func__
);
1027 genpd
= dev_to_genpd(dev
);
1031 if (genpd
->suspend_power_off
1032 || (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
)))
1036 * Since all of the "noirq" callbacks are executed sequentially, it is
1037 * guaranteed that this function will never run twice in parallel for
1038 * the same PM domain, so it is not necessary to use locking here.
1040 pm_genpd_sync_poweron(genpd
);
1041 genpd
->suspended_count
--;
1043 return genpd_start_dev(genpd
, dev
);
1047 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1048 * @dev: Device to resume.
1050 * Carry out an early resume of a device under the assumption that its
1051 * pm_domain field points to the domain member of an object of type
1052 * struct generic_pm_domain representing a power domain consisting of I/O
1055 static int pm_genpd_resume_early(struct device
*dev
)
1057 struct generic_pm_domain
*genpd
;
1059 dev_dbg(dev
, "%s()\n", __func__
);
1061 genpd
= dev_to_genpd(dev
);
1065 return genpd
->suspend_power_off
? 0 : pm_generic_resume_early(dev
);
1069 * pm_genpd_resume - Resume of device in an I/O PM domain.
1070 * @dev: Device to resume.
1072 * Resume a device under the assumption that its pm_domain field points to the
1073 * domain member of an object of type struct generic_pm_domain representing
1074 * a power domain consisting of I/O devices.
1076 static int pm_genpd_resume(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 : pm_generic_resume(dev
);
1090 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1091 * @dev: Device to freeze.
1093 * Freeze 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_freeze(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 : pm_generic_freeze(dev
);
1111 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1112 * @dev: Device to freeze.
1114 * Carry out a late freeze of a device under the assumption that its
1115 * pm_domain field points to the domain member of an object of type
1116 * struct generic_pm_domain representing a power domain consisting of I/O
1119 static int pm_genpd_freeze_late(struct device
*dev
)
1121 struct generic_pm_domain
*genpd
;
1123 dev_dbg(dev
, "%s()\n", __func__
);
1125 genpd
= dev_to_genpd(dev
);
1129 return genpd
->suspend_power_off
? 0 : pm_generic_freeze_late(dev
);
1133 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1134 * @dev: Device to freeze.
1136 * Carry out a late freeze of a device under the assumption that its
1137 * pm_domain field points to the domain member of an object of type
1138 * struct generic_pm_domain representing a power domain consisting of I/O
1141 static int pm_genpd_freeze_noirq(struct device
*dev
)
1143 struct generic_pm_domain
*genpd
;
1145 dev_dbg(dev
, "%s()\n", __func__
);
1147 genpd
= dev_to_genpd(dev
);
1151 return genpd
->suspend_power_off
? 0 : genpd_stop_dev(genpd
, dev
);
1155 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1156 * @dev: Device to thaw.
1158 * Start the device, unless power has been removed from the domain already
1159 * before the system transition.
1161 static int pm_genpd_thaw_noirq(struct device
*dev
)
1163 struct generic_pm_domain
*genpd
;
1165 dev_dbg(dev
, "%s()\n", __func__
);
1167 genpd
= dev_to_genpd(dev
);
1171 return genpd
->suspend_power_off
? 0 : genpd_start_dev(genpd
, dev
);
1175 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1176 * @dev: Device to thaw.
1178 * Carry out an early thaw of a device under the assumption that its
1179 * pm_domain field points to the domain member of an object of type
1180 * struct generic_pm_domain representing a power domain consisting of I/O
1183 static int pm_genpd_thaw_early(struct device
*dev
)
1185 struct generic_pm_domain
*genpd
;
1187 dev_dbg(dev
, "%s()\n", __func__
);
1189 genpd
= dev_to_genpd(dev
);
1193 return genpd
->suspend_power_off
? 0 : pm_generic_thaw_early(dev
);
1197 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1198 * @dev: Device to thaw.
1200 * Thaw a device under the assumption that its pm_domain field points to the
1201 * domain member of an object of type struct generic_pm_domain representing
1202 * a power domain consisting of I/O devices.
1204 static int pm_genpd_thaw(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 : pm_generic_thaw(dev
);
1218 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1219 * @dev: Device to resume.
1221 * Make sure the domain will be in the same power state as before the
1222 * hibernation the system is resuming from and start the device if necessary.
1224 static int pm_genpd_restore_noirq(struct device
*dev
)
1226 struct generic_pm_domain
*genpd
;
1228 dev_dbg(dev
, "%s()\n", __func__
);
1230 genpd
= dev_to_genpd(dev
);
1235 * Since all of the "noirq" callbacks are executed sequentially, it is
1236 * guaranteed that this function will never run twice in parallel for
1237 * the same PM domain, so it is not necessary to use locking here.
1239 * At this point suspended_count == 0 means we are being run for the
1240 * first time for the given domain in the present cycle.
1242 if (genpd
->suspended_count
++ == 0) {
1244 * The boot kernel might put the domain into arbitrary state,
1245 * so make it appear as powered off to pm_genpd_sync_poweron(),
1246 * so that it tries to power it on in case it was really off.
1248 genpd
->status
= GPD_STATE_POWER_OFF
;
1249 if (genpd
->suspend_power_off
) {
1251 * If the domain was off before the hibernation, make
1252 * sure it will be off going forward.
1254 if (genpd
->power_off
)
1255 genpd
->power_off(genpd
);
1261 if (genpd
->suspend_power_off
)
1264 pm_genpd_sync_poweron(genpd
);
1266 return genpd_start_dev(genpd
, dev
);
1270 * pm_genpd_complete - Complete power transition of a device in a power domain.
1271 * @dev: Device to complete the transition of.
1273 * Complete a power transition of a device (during a system-wide power
1274 * transition) under the assumption that its pm_domain field points to the
1275 * domain member of an object of type struct generic_pm_domain representing
1276 * a power domain consisting of I/O devices.
1278 static void pm_genpd_complete(struct device
*dev
)
1280 struct generic_pm_domain
*genpd
;
1283 dev_dbg(dev
, "%s()\n", __func__
);
1285 genpd
= dev_to_genpd(dev
);
1289 mutex_lock(&genpd
->lock
);
1291 run_complete
= !genpd
->suspend_power_off
;
1292 if (--genpd
->prepared_count
== 0)
1293 genpd
->suspend_power_off
= false;
1295 mutex_unlock(&genpd
->lock
);
1298 pm_generic_complete(dev
);
1299 pm_runtime_set_active(dev
);
1300 pm_runtime_enable(dev
);
1301 pm_request_idle(dev
);
1306 * genpd_syscore_switch - Switch power during system core suspend or resume.
1307 * @dev: Device that normally is marked as "always on" to switch power for.
1309 * This routine may only be called during the system core (syscore) suspend or
1310 * resume phase for devices whose "always on" flags are set.
1312 static void genpd_syscore_switch(struct device
*dev
, bool suspend
)
1314 struct generic_pm_domain
*genpd
;
1316 genpd
= dev_to_genpd(dev
);
1317 if (!pm_genpd_present(genpd
))
1321 genpd
->suspended_count
++;
1322 pm_genpd_sync_poweroff(genpd
);
1324 pm_genpd_sync_poweron(genpd
);
1325 genpd
->suspended_count
--;
1329 void pm_genpd_syscore_poweroff(struct device
*dev
)
1331 genpd_syscore_switch(dev
, true);
1333 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff
);
1335 void pm_genpd_syscore_poweron(struct device
*dev
)
1337 genpd_syscore_switch(dev
, false);
1339 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron
);
1343 #define pm_genpd_prepare NULL
1344 #define pm_genpd_suspend NULL
1345 #define pm_genpd_suspend_late NULL
1346 #define pm_genpd_suspend_noirq NULL
1347 #define pm_genpd_resume_early NULL
1348 #define pm_genpd_resume_noirq NULL
1349 #define pm_genpd_resume NULL
1350 #define pm_genpd_freeze NULL
1351 #define pm_genpd_freeze_late NULL
1352 #define pm_genpd_freeze_noirq NULL
1353 #define pm_genpd_thaw_early NULL
1354 #define pm_genpd_thaw_noirq NULL
1355 #define pm_genpd_thaw NULL
1356 #define pm_genpd_restore_noirq NULL
1357 #define pm_genpd_complete NULL
1359 #endif /* CONFIG_PM_SLEEP */
1361 static struct generic_pm_domain_data
*__pm_genpd_alloc_dev_data(struct device
*dev
)
1363 struct generic_pm_domain_data
*gpd_data
;
1365 gpd_data
= kzalloc(sizeof(*gpd_data
), GFP_KERNEL
);
1369 mutex_init(&gpd_data
->lock
);
1370 gpd_data
->nb
.notifier_call
= genpd_dev_pm_qos_notifier
;
1371 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1375 static void __pm_genpd_free_dev_data(struct device
*dev
,
1376 struct generic_pm_domain_data
*gpd_data
)
1378 dev_pm_qos_remove_notifier(dev
, &gpd_data
->nb
);
1383 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1384 * @genpd: PM domain to add the device to.
1385 * @dev: Device to be added.
1386 * @td: Set of PM QoS timing parameters to attach to the device.
1388 int __pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1389 struct gpd_timing_data
*td
)
1391 struct generic_pm_domain_data
*gpd_data_new
, *gpd_data
= NULL
;
1392 struct pm_domain_data
*pdd
;
1395 dev_dbg(dev
, "%s()\n", __func__
);
1397 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
))
1400 gpd_data_new
= __pm_genpd_alloc_dev_data(dev
);
1404 genpd_acquire_lock(genpd
);
1406 if (genpd
->prepared_count
> 0) {
1411 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
)
1412 if (pdd
->dev
== dev
) {
1417 ret
= dev_pm_get_subsys_data(dev
);
1421 genpd
->device_count
++;
1422 genpd
->max_off_time_changed
= true;
1424 spin_lock_irq(&dev
->power
.lock
);
1426 dev
->pm_domain
= &genpd
->domain
;
1427 if (dev
->power
.subsys_data
->domain_data
) {
1428 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
1430 gpd_data
= gpd_data_new
;
1431 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1433 gpd_data
->refcount
++;
1437 spin_unlock_irq(&dev
->power
.lock
);
1439 if (genpd
->attach_dev
)
1440 genpd
->attach_dev(dev
);
1442 mutex_lock(&gpd_data
->lock
);
1443 gpd_data
->base
.dev
= dev
;
1444 list_add_tail(&gpd_data
->base
.list_node
, &genpd
->dev_list
);
1445 gpd_data
->need_restore
= genpd
->status
== GPD_STATE_POWER_OFF
;
1446 gpd_data
->td
.constraint_changed
= true;
1447 gpd_data
->td
.effective_constraint_ns
= -1;
1448 mutex_unlock(&gpd_data
->lock
);
1451 genpd_release_lock(genpd
);
1453 if (gpd_data
!= gpd_data_new
)
1454 __pm_genpd_free_dev_data(dev
, gpd_data_new
);
1460 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1461 * @domain_name: Name of the PM domain to add the device to.
1462 * @dev: Device to be added.
1463 * @td: Set of PM QoS timing parameters to attach to the device.
1465 int __pm_genpd_name_add_device(const char *domain_name
, struct device
*dev
,
1466 struct gpd_timing_data
*td
)
1468 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name
), dev
, td
);
1472 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1473 * @genpd: PM domain to remove the device from.
1474 * @dev: Device to be removed.
1476 int pm_genpd_remove_device(struct generic_pm_domain
*genpd
,
1479 struct generic_pm_domain_data
*gpd_data
;
1480 struct pm_domain_data
*pdd
;
1481 bool remove
= false;
1484 dev_dbg(dev
, "%s()\n", __func__
);
1486 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
)
1487 || IS_ERR_OR_NULL(dev
->pm_domain
)
1488 || pd_to_genpd(dev
->pm_domain
) != genpd
)
1491 genpd_acquire_lock(genpd
);
1493 if (genpd
->prepared_count
> 0) {
1498 genpd
->device_count
--;
1499 genpd
->max_off_time_changed
= true;
1501 if (genpd
->detach_dev
)
1502 genpd
->detach_dev(dev
);
1504 spin_lock_irq(&dev
->power
.lock
);
1506 dev
->pm_domain
= NULL
;
1507 pdd
= dev
->power
.subsys_data
->domain_data
;
1508 list_del_init(&pdd
->list_node
);
1509 gpd_data
= to_gpd_data(pdd
);
1510 if (--gpd_data
->refcount
== 0) {
1511 dev
->power
.subsys_data
->domain_data
= NULL
;
1515 spin_unlock_irq(&dev
->power
.lock
);
1517 mutex_lock(&gpd_data
->lock
);
1519 mutex_unlock(&gpd_data
->lock
);
1521 genpd_release_lock(genpd
);
1523 dev_pm_put_subsys_data(dev
);
1525 __pm_genpd_free_dev_data(dev
, gpd_data
);
1530 genpd_release_lock(genpd
);
1536 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1537 * @dev: Device to set/unset the flag for.
1538 * @val: The new value of the device's "need restore" flag.
1540 void pm_genpd_dev_need_restore(struct device
*dev
, bool val
)
1542 struct pm_subsys_data
*psd
;
1543 unsigned long flags
;
1545 spin_lock_irqsave(&dev
->power
.lock
, flags
);
1547 psd
= dev_to_psd(dev
);
1548 if (psd
&& psd
->domain_data
)
1549 to_gpd_data(psd
->domain_data
)->need_restore
= val
;
1551 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
1553 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore
);
1556 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1557 * @genpd: Master PM domain to add the subdomain to.
1558 * @subdomain: Subdomain to be added.
1560 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1561 struct generic_pm_domain
*subdomain
)
1563 struct gpd_link
*link
;
1566 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
)
1567 || genpd
== subdomain
)
1571 genpd_acquire_lock(genpd
);
1572 mutex_lock_nested(&subdomain
->lock
, SINGLE_DEPTH_NESTING
);
1574 if (subdomain
->status
!= GPD_STATE_POWER_OFF
1575 && subdomain
->status
!= GPD_STATE_ACTIVE
) {
1576 mutex_unlock(&subdomain
->lock
);
1577 genpd_release_lock(genpd
);
1581 if (genpd
->status
== GPD_STATE_POWER_OFF
1582 && subdomain
->status
!= GPD_STATE_POWER_OFF
) {
1587 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
1588 if (link
->slave
== subdomain
&& link
->master
== genpd
) {
1594 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1599 link
->master
= genpd
;
1600 list_add_tail(&link
->master_node
, &genpd
->master_links
);
1601 link
->slave
= subdomain
;
1602 list_add_tail(&link
->slave_node
, &subdomain
->slave_links
);
1603 if (subdomain
->status
!= GPD_STATE_POWER_OFF
)
1604 genpd_sd_counter_inc(genpd
);
1607 mutex_unlock(&subdomain
->lock
);
1608 genpd_release_lock(genpd
);
1614 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1615 * @master_name: Name of the master PM domain to add the subdomain to.
1616 * @subdomain_name: Name of the subdomain to be added.
1618 int pm_genpd_add_subdomain_names(const char *master_name
,
1619 const char *subdomain_name
)
1621 struct generic_pm_domain
*master
= NULL
, *subdomain
= NULL
, *gpd
;
1623 if (IS_ERR_OR_NULL(master_name
) || IS_ERR_OR_NULL(subdomain_name
))
1626 mutex_lock(&gpd_list_lock
);
1627 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
1628 if (!master
&& !strcmp(gpd
->name
, master_name
))
1631 if (!subdomain
&& !strcmp(gpd
->name
, subdomain_name
))
1634 if (master
&& subdomain
)
1637 mutex_unlock(&gpd_list_lock
);
1639 return pm_genpd_add_subdomain(master
, subdomain
);
1643 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1644 * @genpd: Master PM domain to remove the subdomain from.
1645 * @subdomain: Subdomain to be removed.
1647 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
1648 struct generic_pm_domain
*subdomain
)
1650 struct gpd_link
*link
;
1653 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
))
1657 genpd_acquire_lock(genpd
);
1659 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
1660 if (link
->slave
!= subdomain
)
1663 mutex_lock_nested(&subdomain
->lock
, SINGLE_DEPTH_NESTING
);
1665 if (subdomain
->status
!= GPD_STATE_POWER_OFF
1666 && subdomain
->status
!= GPD_STATE_ACTIVE
) {
1667 mutex_unlock(&subdomain
->lock
);
1668 genpd_release_lock(genpd
);
1672 list_del(&link
->master_node
);
1673 list_del(&link
->slave_node
);
1675 if (subdomain
->status
!= GPD_STATE_POWER_OFF
)
1676 genpd_sd_counter_dec(genpd
);
1678 mutex_unlock(&subdomain
->lock
);
1684 genpd_release_lock(genpd
);
1690 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1691 * @genpd: PM domain to be connected with cpuidle.
1692 * @state: cpuidle state this domain can disable/enable.
1694 * Make a PM domain behave as though it contained a CPU core, that is, instead
1695 * of calling its power down routine it will enable the given cpuidle state so
1696 * that the cpuidle subsystem can power it down (if possible and desirable).
1698 int pm_genpd_attach_cpuidle(struct generic_pm_domain
*genpd
, int state
)
1700 struct cpuidle_driver
*cpuidle_drv
;
1701 struct gpd_cpuidle_data
*cpuidle_data
;
1702 struct cpuidle_state
*idle_state
;
1705 if (IS_ERR_OR_NULL(genpd
) || state
< 0)
1708 genpd_acquire_lock(genpd
);
1710 if (genpd
->cpuidle_data
) {
1714 cpuidle_data
= kzalloc(sizeof(*cpuidle_data
), GFP_KERNEL
);
1715 if (!cpuidle_data
) {
1719 cpuidle_drv
= cpuidle_driver_ref();
1724 if (cpuidle_drv
->state_count
<= state
) {
1728 idle_state
= &cpuidle_drv
->states
[state
];
1729 if (!idle_state
->disabled
) {
1733 cpuidle_data
->idle_state
= idle_state
;
1734 cpuidle_data
->saved_exit_latency
= idle_state
->exit_latency
;
1735 genpd
->cpuidle_data
= cpuidle_data
;
1736 genpd_recalc_cpu_exit_latency(genpd
);
1739 genpd_release_lock(genpd
);
1743 cpuidle_driver_unref();
1746 kfree(cpuidle_data
);
1751 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1752 * @name: Name of the domain to connect to cpuidle.
1753 * @state: cpuidle state this domain can manipulate.
1755 int pm_genpd_name_attach_cpuidle(const char *name
, int state
)
1757 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name
), state
);
1761 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1762 * @genpd: PM domain to remove the cpuidle connection from.
1764 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1767 int pm_genpd_detach_cpuidle(struct generic_pm_domain
*genpd
)
1769 struct gpd_cpuidle_data
*cpuidle_data
;
1770 struct cpuidle_state
*idle_state
;
1773 if (IS_ERR_OR_NULL(genpd
))
1776 genpd_acquire_lock(genpd
);
1778 cpuidle_data
= genpd
->cpuidle_data
;
1779 if (!cpuidle_data
) {
1783 idle_state
= cpuidle_data
->idle_state
;
1784 if (!idle_state
->disabled
) {
1788 idle_state
->exit_latency
= cpuidle_data
->saved_exit_latency
;
1789 cpuidle_driver_unref();
1790 genpd
->cpuidle_data
= NULL
;
1791 kfree(cpuidle_data
);
1794 genpd_release_lock(genpd
);
1799 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1800 * @name: Name of the domain to disconnect cpuidle from.
1802 int pm_genpd_name_detach_cpuidle(const char *name
)
1804 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name
));
1807 /* Default device callbacks for generic PM domains. */
1810 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1811 * @dev: Device to handle.
1813 static int pm_genpd_default_save_state(struct device
*dev
)
1815 int (*cb
)(struct device
*__dev
);
1817 if (dev
->type
&& dev
->type
->pm
)
1818 cb
= dev
->type
->pm
->runtime_suspend
;
1819 else if (dev
->class && dev
->class->pm
)
1820 cb
= dev
->class->pm
->runtime_suspend
;
1821 else if (dev
->bus
&& dev
->bus
->pm
)
1822 cb
= dev
->bus
->pm
->runtime_suspend
;
1826 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
1827 cb
= dev
->driver
->pm
->runtime_suspend
;
1829 return cb
? cb(dev
) : 0;
1833 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1834 * @dev: Device to handle.
1836 static int pm_genpd_default_restore_state(struct device
*dev
)
1838 int (*cb
)(struct device
*__dev
);
1840 if (dev
->type
&& dev
->type
->pm
)
1841 cb
= dev
->type
->pm
->runtime_resume
;
1842 else if (dev
->class && dev
->class->pm
)
1843 cb
= dev
->class->pm
->runtime_resume
;
1844 else if (dev
->bus
&& dev
->bus
->pm
)
1845 cb
= dev
->bus
->pm
->runtime_resume
;
1849 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
1850 cb
= dev
->driver
->pm
->runtime_resume
;
1852 return cb
? cb(dev
) : 0;
1856 * pm_genpd_init - Initialize a generic I/O PM domain object.
1857 * @genpd: PM domain object to initialize.
1858 * @gov: PM domain governor to associate with the domain (may be NULL).
1859 * @is_off: Initial value of the domain's power_is_off field.
1861 void pm_genpd_init(struct generic_pm_domain
*genpd
,
1862 struct dev_power_governor
*gov
, bool is_off
)
1864 if (IS_ERR_OR_NULL(genpd
))
1867 INIT_LIST_HEAD(&genpd
->master_links
);
1868 INIT_LIST_HEAD(&genpd
->slave_links
);
1869 INIT_LIST_HEAD(&genpd
->dev_list
);
1870 mutex_init(&genpd
->lock
);
1872 INIT_WORK(&genpd
->power_off_work
, genpd_power_off_work_fn
);
1873 genpd
->in_progress
= 0;
1874 atomic_set(&genpd
->sd_count
, 0);
1875 genpd
->status
= is_off
? GPD_STATE_POWER_OFF
: GPD_STATE_ACTIVE
;
1876 init_waitqueue_head(&genpd
->status_wait_queue
);
1877 genpd
->poweroff_task
= NULL
;
1878 genpd
->resume_count
= 0;
1879 genpd
->device_count
= 0;
1880 genpd
->max_off_time_ns
= -1;
1881 genpd
->max_off_time_changed
= true;
1882 genpd
->domain
.ops
.runtime_suspend
= pm_genpd_runtime_suspend
;
1883 genpd
->domain
.ops
.runtime_resume
= pm_genpd_runtime_resume
;
1884 genpd
->domain
.ops
.prepare
= pm_genpd_prepare
;
1885 genpd
->domain
.ops
.suspend
= pm_genpd_suspend
;
1886 genpd
->domain
.ops
.suspend_late
= pm_genpd_suspend_late
;
1887 genpd
->domain
.ops
.suspend_noirq
= pm_genpd_suspend_noirq
;
1888 genpd
->domain
.ops
.resume_noirq
= pm_genpd_resume_noirq
;
1889 genpd
->domain
.ops
.resume_early
= pm_genpd_resume_early
;
1890 genpd
->domain
.ops
.resume
= pm_genpd_resume
;
1891 genpd
->domain
.ops
.freeze
= pm_genpd_freeze
;
1892 genpd
->domain
.ops
.freeze_late
= pm_genpd_freeze_late
;
1893 genpd
->domain
.ops
.freeze_noirq
= pm_genpd_freeze_noirq
;
1894 genpd
->domain
.ops
.thaw_noirq
= pm_genpd_thaw_noirq
;
1895 genpd
->domain
.ops
.thaw_early
= pm_genpd_thaw_early
;
1896 genpd
->domain
.ops
.thaw
= pm_genpd_thaw
;
1897 genpd
->domain
.ops
.poweroff
= pm_genpd_suspend
;
1898 genpd
->domain
.ops
.poweroff_late
= pm_genpd_suspend_late
;
1899 genpd
->domain
.ops
.poweroff_noirq
= pm_genpd_suspend_noirq
;
1900 genpd
->domain
.ops
.restore_noirq
= pm_genpd_restore_noirq
;
1901 genpd
->domain
.ops
.restore_early
= pm_genpd_resume_early
;
1902 genpd
->domain
.ops
.restore
= pm_genpd_resume
;
1903 genpd
->domain
.ops
.complete
= pm_genpd_complete
;
1904 genpd
->dev_ops
.save_state
= pm_genpd_default_save_state
;
1905 genpd
->dev_ops
.restore_state
= pm_genpd_default_restore_state
;
1906 mutex_lock(&gpd_list_lock
);
1907 list_add(&genpd
->gpd_list_node
, &gpd_list
);
1908 mutex_unlock(&gpd_list_lock
);
1911 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1913 * Device Tree based PM domain providers.
1915 * The code below implements generic device tree based PM domain providers that
1916 * bind device tree nodes with generic PM domains registered in the system.
1918 * Any driver that registers generic PM domains and needs to support binding of
1919 * devices to these domains is supposed to register a PM domain provider, which
1920 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1922 * Two simple mapping functions have been provided for convenience:
1923 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1924 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1929 * struct of_genpd_provider - PM domain provider registration structure
1930 * @link: Entry in global list of PM domain providers
1931 * @node: Pointer to device tree node of PM domain provider
1932 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1934 * @data: context pointer to be passed into @xlate callback
1936 struct of_genpd_provider
{
1937 struct list_head link
;
1938 struct device_node
*node
;
1939 genpd_xlate_t xlate
;
1943 /* List of registered PM domain providers. */
1944 static LIST_HEAD(of_genpd_providers
);
1945 /* Mutex to protect the list above. */
1946 static DEFINE_MUTEX(of_genpd_mutex
);
1949 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1950 * @genpdspec: OF phandle args to map into a PM domain
1951 * @data: xlate function private data - pointer to struct generic_pm_domain
1953 * This is a generic xlate function that can be used to model PM domains that
1954 * have their own device tree nodes. The private data of xlate function needs
1955 * to be a valid pointer to struct generic_pm_domain.
1957 struct generic_pm_domain
*__of_genpd_xlate_simple(
1958 struct of_phandle_args
*genpdspec
,
1961 if (genpdspec
->args_count
!= 0)
1962 return ERR_PTR(-EINVAL
);
1965 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple
);
1968 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1969 * @genpdspec: OF phandle args to map into a PM domain
1970 * @data: xlate function private data - pointer to struct genpd_onecell_data
1972 * This is a generic xlate function that can be used to model simple PM domain
1973 * controllers that have one device tree node and provide multiple PM domains.
1974 * A single cell is used as an index into an array of PM domains specified in
1975 * the genpd_onecell_data struct when registering the provider.
1977 struct generic_pm_domain
*__of_genpd_xlate_onecell(
1978 struct of_phandle_args
*genpdspec
,
1981 struct genpd_onecell_data
*genpd_data
= data
;
1982 unsigned int idx
= genpdspec
->args
[0];
1984 if (genpdspec
->args_count
!= 1)
1985 return ERR_PTR(-EINVAL
);
1987 if (idx
>= genpd_data
->num_domains
) {
1988 pr_err("%s: invalid domain index %u\n", __func__
, idx
);
1989 return ERR_PTR(-EINVAL
);
1992 if (!genpd_data
->domains
[idx
])
1993 return ERR_PTR(-ENOENT
);
1995 return genpd_data
->domains
[idx
];
1997 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell
);
2000 * __of_genpd_add_provider() - Register a PM domain provider for a node
2001 * @np: Device node pointer associated with the PM domain provider.
2002 * @xlate: Callback for decoding PM domain from phandle arguments.
2003 * @data: Context pointer for @xlate callback.
2005 int __of_genpd_add_provider(struct device_node
*np
, genpd_xlate_t xlate
,
2008 struct of_genpd_provider
*cp
;
2010 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
2014 cp
->node
= of_node_get(np
);
2018 mutex_lock(&of_genpd_mutex
);
2019 list_add(&cp
->link
, &of_genpd_providers
);
2020 mutex_unlock(&of_genpd_mutex
);
2021 pr_debug("Added domain provider from %s\n", np
->full_name
);
2025 EXPORT_SYMBOL_GPL(__of_genpd_add_provider
);
2028 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2029 * @np: Device node pointer associated with the PM domain provider
2031 void of_genpd_del_provider(struct device_node
*np
)
2033 struct of_genpd_provider
*cp
;
2035 mutex_lock(&of_genpd_mutex
);
2036 list_for_each_entry(cp
, &of_genpd_providers
, link
) {
2037 if (cp
->node
== np
) {
2038 list_del(&cp
->link
);
2039 of_node_put(cp
->node
);
2044 mutex_unlock(&of_genpd_mutex
);
2046 EXPORT_SYMBOL_GPL(of_genpd_del_provider
);
2049 * of_genpd_get_from_provider() - Look-up PM domain
2050 * @genpdspec: OF phandle args to use for look-up
2052 * Looks for a PM domain provider under the node specified by @genpdspec and if
2053 * found, uses xlate function of the provider to map phandle args to a PM
2056 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2059 static struct generic_pm_domain
*of_genpd_get_from_provider(
2060 struct of_phandle_args
*genpdspec
)
2062 struct generic_pm_domain
*genpd
= ERR_PTR(-ENOENT
);
2063 struct of_genpd_provider
*provider
;
2065 mutex_lock(&of_genpd_mutex
);
2067 /* Check if we have such a provider in our array */
2068 list_for_each_entry(provider
, &of_genpd_providers
, link
) {
2069 if (provider
->node
== genpdspec
->np
)
2070 genpd
= provider
->xlate(genpdspec
, provider
->data
);
2075 mutex_unlock(&of_genpd_mutex
);
2081 * genpd_dev_pm_detach - Detach a device from its PM domain.
2082 * @dev: Device to attach.
2083 * @power_off: Currently not used
2085 * Try to locate a corresponding generic PM domain, which the device was
2086 * attached to previously. If such is found, the device is detached from it.
2088 static void genpd_dev_pm_detach(struct device
*dev
, bool power_off
)
2090 struct generic_pm_domain
*pd
= NULL
, *gpd
;
2093 if (!dev
->pm_domain
)
2096 mutex_lock(&gpd_list_lock
);
2097 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
2098 if (&gpd
->domain
== dev
->pm_domain
) {
2103 mutex_unlock(&gpd_list_lock
);
2108 dev_dbg(dev
, "removing from PM domain %s\n", pd
->name
);
2111 ret
= pm_genpd_remove_device(pd
, dev
);
2118 dev_err(dev
, "failed to remove from PM domain %s: %d",
2123 /* Check if PM domain can be powered off after removing this device. */
2124 genpd_queue_power_off_work(pd
);
2128 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2129 * @dev: Device to attach.
2131 * Parse device's OF node to find a PM domain specifier. If such is found,
2132 * attaches the device to retrieved pm_domain ops.
2134 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2135 * backwards compatibility with existing DTBs.
2137 * Returns 0 on successfully attached PM domain or negative error code.
2139 int genpd_dev_pm_attach(struct device
*dev
)
2141 struct of_phandle_args pd_args
;
2142 struct generic_pm_domain
*pd
;
2151 ret
= of_parse_phandle_with_args(dev
->of_node
, "power-domains",
2152 "#power-domain-cells", 0, &pd_args
);
2158 * Try legacy Samsung-specific bindings
2159 * (for backwards compatibility of DT ABI)
2161 pd_args
.args_count
= 0;
2162 pd_args
.np
= of_parse_phandle(dev
->of_node
,
2163 "samsung,power-domain", 0);
2168 pd
= of_genpd_get_from_provider(&pd_args
);
2170 dev_dbg(dev
, "%s() failed to find PM domain: %ld\n",
2171 __func__
, PTR_ERR(pd
));
2172 of_node_put(dev
->of_node
);
2176 dev_dbg(dev
, "adding to PM domain %s\n", pd
->name
);
2179 ret
= pm_genpd_add_device(pd
, dev
);
2186 dev_err(dev
, "failed to add to PM domain %s: %d",
2188 of_node_put(dev
->of_node
);
2192 dev
->pm_domain
->detach
= genpd_dev_pm_detach
;
2196 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach
);
2200 /*** debugfs support ***/
2202 #ifdef CONFIG_PM_ADVANCED_DEBUG
2203 #include <linux/pm.h>
2204 #include <linux/device.h>
2205 #include <linux/debugfs.h>
2206 #include <linux/seq_file.h>
2207 #include <linux/init.h>
2208 #include <linux/kobject.h>
2209 static struct dentry
*pm_genpd_debugfs_dir
;
2212 * TODO: This function is a slightly modified version of rtpm_status_show
2213 * from sysfs.c, but dependencies between PM_GENERIC_DOMAINS and PM_RUNTIME
2214 * are too loose to generalize it.
2216 #ifdef CONFIG_PM_RUNTIME
2217 static void rtpm_status_str(struct seq_file
*s
, struct device
*dev
)
2219 static const char * const status_lookup
[] = {
2220 [RPM_ACTIVE
] = "active",
2221 [RPM_RESUMING
] = "resuming",
2222 [RPM_SUSPENDED
] = "suspended",
2223 [RPM_SUSPENDING
] = "suspending"
2227 if (dev
->power
.runtime_error
)
2229 else if (dev
->power
.disable_depth
)
2231 else if (dev
->power
.runtime_status
< ARRAY_SIZE(status_lookup
))
2232 p
= status_lookup
[dev
->power
.runtime_status
];
2239 static void rtpm_status_str(struct seq_file
*s
, struct device
*dev
)
2241 seq_puts(s
, "active");
2245 static int pm_genpd_summary_one(struct seq_file
*s
,
2246 struct generic_pm_domain
*gpd
)
2248 static const char * const status_lookup
[] = {
2249 [GPD_STATE_ACTIVE
] = "on",
2250 [GPD_STATE_WAIT_MASTER
] = "wait-master",
2251 [GPD_STATE_BUSY
] = "busy",
2252 [GPD_STATE_REPEAT
] = "off-in-progress",
2253 [GPD_STATE_POWER_OFF
] = "off"
2255 struct pm_domain_data
*pm_data
;
2256 const char *kobj_path
;
2257 struct gpd_link
*link
;
2260 ret
= mutex_lock_interruptible(&gpd
->lock
);
2262 return -ERESTARTSYS
;
2264 if (WARN_ON(gpd
->status
>= ARRAY_SIZE(status_lookup
)))
2266 seq_printf(s
, "%-30s %-15s ", gpd
->name
, status_lookup
[gpd
->status
]);
2269 * Modifications on the list require holding locks on both
2270 * master and slave, so we are safe.
2271 * Also gpd->name is immutable.
2273 list_for_each_entry(link
, &gpd
->master_links
, master_node
) {
2274 seq_printf(s
, "%s", link
->slave
->name
);
2275 if (!list_is_last(&link
->master_node
, &gpd
->master_links
))
2279 list_for_each_entry(pm_data
, &gpd
->dev_list
, list_node
) {
2280 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
, GFP_KERNEL
);
2281 if (kobj_path
== NULL
)
2284 seq_printf(s
, "\n %-50s ", kobj_path
);
2285 rtpm_status_str(s
, pm_data
->dev
);
2291 mutex_unlock(&gpd
->lock
);
2296 static int pm_genpd_summary_show(struct seq_file
*s
, void *data
)
2298 struct generic_pm_domain
*gpd
;
2301 seq_puts(s
, " domain status slaves\n");
2302 seq_puts(s
, " /device runtime status\n");
2303 seq_puts(s
, "----------------------------------------------------------------------\n");
2305 ret
= mutex_lock_interruptible(&gpd_list_lock
);
2307 return -ERESTARTSYS
;
2309 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
2310 ret
= pm_genpd_summary_one(s
, gpd
);
2314 mutex_unlock(&gpd_list_lock
);
2319 static int pm_genpd_summary_open(struct inode
*inode
, struct file
*file
)
2321 return single_open(file
, pm_genpd_summary_show
, NULL
);
2324 static const struct file_operations pm_genpd_summary_fops
= {
2325 .open
= pm_genpd_summary_open
,
2327 .llseek
= seq_lseek
,
2328 .release
= single_release
,
2331 static int __init
pm_genpd_debug_init(void)
2335 pm_genpd_debugfs_dir
= debugfs_create_dir("pm_genpd", NULL
);
2337 if (!pm_genpd_debugfs_dir
)
2340 d
= debugfs_create_file("pm_genpd_summary", S_IRUGO
,
2341 pm_genpd_debugfs_dir
, NULL
, &pm_genpd_summary_fops
);
2347 late_initcall(pm_genpd_debug_init
);
2349 static void __exit
pm_genpd_debug_exit(void)
2351 debugfs_remove_recursive(pm_genpd_debugfs_dir
);
2353 __exitcall(pm_genpd_debug_exit
);
2354 #endif /* CONFIG_PM_ADVANCED_DEBUG */