Linux 3.18.86
[linux/fpc-iii.git] / drivers / base / power / domain.c
blobad7e3fb0ea3e966c99480457dc7da99babd7d0c2
1 /*
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.
7 */
9 #include <linux/kernel.h>
10 #include <linux/io.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) \
22 ({ \
23 type (*__routine)(struct device *__d); \
24 type __ret = (type)0; \
26 __routine = genpd->dev_ops.callback; \
27 if (__routine) { \
28 __ret = __routine(dev); \
29 } \
30 __ret; \
33 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
34 ({ \
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", \
42 __elapsed); \
43 genpd->max_off_time_changed = true; \
44 __td->constraint_changed = true; \
45 } \
46 __retval; \
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))
57 return NULL;
59 mutex_lock(&gpd_list_lock);
60 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
61 if (!strcmp(gpd->name, domain_name)) {
62 genpd = gpd;
63 break;
66 mutex_unlock(&gpd_list_lock);
67 return genpd;
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)
92 bool ret = false;
94 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
95 ret = !!atomic_dec_and_test(&genpd->sd_count);
97 return ret;
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)
108 DEFINE_WAIT(wait);
110 mutex_lock(&genpd->lock);
112 * Wait for the domain to transition into either the active,
113 * or the power off state.
115 for (;;) {
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)
120 break;
121 mutex_unlock(&genpd->lock);
123 schedule();
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)
143 s64 usecs64;
145 if (!genpd->cpuidle_data)
146 return;
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;
165 DEFINE_WAIT(wait);
166 int ret = 0;
168 /* If the domain's master is being waited for, we have to wait too. */
169 for (;;) {
170 prepare_to_wait(&genpd->status_wait_queue, &wait,
171 TASK_UNINTERRUPTIBLE);
172 if (genpd->status != GPD_STATE_WAIT_MASTER)
173 break;
174 mutex_unlock(&genpd->lock);
176 schedule();
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))
184 return 0;
186 if (genpd->status != GPD_STATE_POWER_OFF) {
187 genpd_set_active(genpd);
188 return 0;
191 if (genpd->cpuidle_data) {
192 cpuidle_pause_and_lock();
193 genpd->cpuidle_data->idle_state->disabled = true;
194 cpuidle_resume_and_unlock();
195 goto out;
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
201 * with it.
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);
219 if (ret) {
220 genpd_sd_counter_dec(link->master);
221 goto err;
225 if (genpd->power_on) {
226 ktime_t time_start = ktime_get();
227 s64 elapsed_ns;
229 ret = genpd->power_on(genpd);
230 if (ret)
231 goto err;
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);
238 if (genpd->name)
239 pr_warning("%s: Power-on latency exceeded, "
240 "new value %lld ns\n", genpd->name,
241 elapsed_ns);
245 out:
246 genpd_set_active(genpd);
248 return 0;
250 err:
251 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
252 genpd_sd_counter_dec(link->master);
254 return ret;
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)
263 int ret;
265 mutex_lock(&genpd->lock);
266 ret = __pm_genpd_poweron(genpd);
267 mutex_unlock(&genpd->lock);
268 return ret;
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,
286 struct device *dev)
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,
301 "state restore");
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;
308 struct device *dev;
310 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
312 mutex_lock(&gpd_data->lock);
313 dev = gpd_data->base.dev;
314 if (!dev) {
315 mutex_unlock(&gpd_data->lock);
316 return NOTIFY_DONE;
318 mutex_unlock(&gpd_data->lock);
320 for (;;) {
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);
331 } else {
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);
343 dev = dev->parent;
344 if (!dev || dev->power.ignore_children)
345 break;
348 return NOTIFY_DONE;
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;
362 int ret = 0;
364 if (gpd_data->need_restore > 0)
365 return 0;
368 * If the value of the need_restore flag is still unknown at this point,
369 * we trust that pm_genpd_poweroff() has verified that the device is
370 * already runtime PM suspended.
372 if (gpd_data->need_restore < 0) {
373 gpd_data->need_restore = 1;
374 return 0;
377 mutex_unlock(&genpd->lock);
379 genpd_start_dev(genpd, dev);
380 ret = genpd_save_dev(genpd, dev);
381 genpd_stop_dev(genpd, dev);
383 mutex_lock(&genpd->lock);
385 if (!ret)
386 gpd_data->need_restore = 1;
388 return ret;
392 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
393 * @pdd: Domain data of the device to restore the state of.
394 * @genpd: PM domain the device belongs to.
396 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
397 struct generic_pm_domain *genpd)
398 __releases(&genpd->lock) __acquires(&genpd->lock)
400 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
401 struct device *dev = pdd->dev;
402 int need_restore = gpd_data->need_restore;
404 gpd_data->need_restore = 0;
405 mutex_unlock(&genpd->lock);
407 genpd_start_dev(genpd, dev);
410 * Call genpd_restore_dev() for recently added devices too (need_restore
411 * is negative then).
413 if (need_restore)
414 genpd_restore_dev(genpd, dev);
416 mutex_lock(&genpd->lock);
420 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
421 * @genpd: PM domain to check.
423 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
424 * a "power off" operation, which means that a "power on" has occured in the
425 * meantime, or if its resume_count field is different from zero, which means
426 * that one of its devices has been resumed in the meantime.
428 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
430 return genpd->status == GPD_STATE_WAIT_MASTER
431 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
435 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
436 * @genpd: PM domait to power off.
438 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
439 * before.
441 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
443 queue_work(pm_wq, &genpd->power_off_work);
447 * pm_genpd_poweroff - Remove power from a given PM domain.
448 * @genpd: PM domain to power down.
450 * If all of the @genpd's devices have been suspended and all of its subdomains
451 * have been powered down, run the runtime suspend callbacks provided by all of
452 * the @genpd's devices' drivers and remove power from @genpd.
454 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
455 __releases(&genpd->lock) __acquires(&genpd->lock)
457 struct pm_domain_data *pdd;
458 struct gpd_link *link;
459 unsigned int not_suspended;
460 int ret = 0;
462 start:
464 * Do not try to power off the domain in the following situations:
465 * (1) The domain is already in the "power off" state.
466 * (2) The domain is waiting for its master to power up.
467 * (3) One of the domain's devices is being resumed right now.
468 * (4) System suspend is in progress.
470 if (genpd->status == GPD_STATE_POWER_OFF
471 || genpd->status == GPD_STATE_WAIT_MASTER
472 || genpd->resume_count > 0 || genpd->prepared_count > 0)
473 return 0;
475 if (atomic_read(&genpd->sd_count) > 0)
476 return -EBUSY;
478 not_suspended = 0;
479 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
480 enum pm_qos_flags_status stat;
482 stat = dev_pm_qos_flags(pdd->dev,
483 PM_QOS_FLAG_NO_POWER_OFF
484 | PM_QOS_FLAG_REMOTE_WAKEUP);
485 if (stat > PM_QOS_FLAGS_NONE)
486 return -EBUSY;
488 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
489 || pdd->dev->power.irq_safe))
490 not_suspended++;
493 if (not_suspended > genpd->in_progress)
494 return -EBUSY;
496 if (genpd->poweroff_task) {
498 * Another instance of pm_genpd_poweroff() is executing
499 * callbacks, so tell it to start over and return.
501 genpd->status = GPD_STATE_REPEAT;
502 return 0;
505 if (genpd->gov && genpd->gov->power_down_ok) {
506 if (!genpd->gov->power_down_ok(&genpd->domain))
507 return -EAGAIN;
510 genpd->status = GPD_STATE_BUSY;
511 genpd->poweroff_task = current;
513 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
514 ret = atomic_read(&genpd->sd_count) == 0 ?
515 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
517 if (genpd_abort_poweroff(genpd))
518 goto out;
520 if (ret) {
521 genpd_set_active(genpd);
522 goto out;
525 if (genpd->status == GPD_STATE_REPEAT) {
526 genpd->poweroff_task = NULL;
527 goto start;
531 if (genpd->cpuidle_data) {
533 * If cpuidle_data is set, cpuidle should turn the domain off
534 * when the CPU in it is idle. In that case we don't decrement
535 * the subdomain counts of the master domains, so that power is
536 * not removed from the current domain prematurely as a result
537 * of cutting off the masters' power.
539 genpd->status = GPD_STATE_POWER_OFF;
540 cpuidle_pause_and_lock();
541 genpd->cpuidle_data->idle_state->disabled = false;
542 cpuidle_resume_and_unlock();
543 goto out;
546 if (genpd->power_off) {
547 ktime_t time_start;
548 s64 elapsed_ns;
550 if (atomic_read(&genpd->sd_count) > 0) {
551 ret = -EBUSY;
552 goto out;
555 time_start = ktime_get();
558 * If sd_count > 0 at this point, one of the subdomains hasn't
559 * managed to call pm_genpd_poweron() for the master yet after
560 * incrementing it. In that case pm_genpd_poweron() will wait
561 * for us to drop the lock, so we can call .power_off() and let
562 * the pm_genpd_poweron() restore power for us (this shouldn't
563 * happen very often).
565 ret = genpd->power_off(genpd);
566 if (ret == -EBUSY) {
567 genpd_set_active(genpd);
568 goto out;
571 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
572 if (elapsed_ns > genpd->power_off_latency_ns) {
573 genpd->power_off_latency_ns = elapsed_ns;
574 genpd->max_off_time_changed = true;
575 if (genpd->name)
576 pr_warning("%s: Power-off latency exceeded, "
577 "new value %lld ns\n", genpd->name,
578 elapsed_ns);
582 genpd->status = GPD_STATE_POWER_OFF;
584 list_for_each_entry(link, &genpd->slave_links, slave_node) {
585 genpd_sd_counter_dec(link->master);
586 genpd_queue_power_off_work(link->master);
589 out:
590 genpd->poweroff_task = NULL;
591 wake_up_all(&genpd->status_wait_queue);
592 return ret;
596 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
597 * @work: Work structure used for scheduling the execution of this function.
599 static void genpd_power_off_work_fn(struct work_struct *work)
601 struct generic_pm_domain *genpd;
603 genpd = container_of(work, struct generic_pm_domain, power_off_work);
605 genpd_acquire_lock(genpd);
606 pm_genpd_poweroff(genpd);
607 genpd_release_lock(genpd);
611 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
612 * @dev: Device to suspend.
614 * Carry out a runtime suspend of a device under the assumption that its
615 * pm_domain field points to the domain member of an object of type
616 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
618 static int pm_genpd_runtime_suspend(struct device *dev)
620 struct generic_pm_domain *genpd;
621 struct generic_pm_domain_data *gpd_data;
622 bool (*stop_ok)(struct device *__dev);
623 int ret;
625 dev_dbg(dev, "%s()\n", __func__);
627 genpd = dev_to_genpd(dev);
628 if (IS_ERR(genpd))
629 return -EINVAL;
631 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
632 if (stop_ok && !stop_ok(dev))
633 return -EBUSY;
635 ret = genpd_stop_dev(genpd, dev);
636 if (ret)
637 return ret;
640 * If power.irq_safe is set, this routine will be run with interrupts
641 * off, so it can't use mutexes.
643 if (dev->power.irq_safe)
644 return 0;
646 mutex_lock(&genpd->lock);
649 * If we have an unknown state of the need_restore flag, it means none
650 * of the runtime PM callbacks has been invoked yet. Let's update the
651 * flag to reflect that the current state is active.
653 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
654 if (gpd_data->need_restore < 0)
655 gpd_data->need_restore = 0;
657 genpd->in_progress++;
658 pm_genpd_poweroff(genpd);
659 genpd->in_progress--;
660 mutex_unlock(&genpd->lock);
662 return 0;
666 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
667 * @dev: Device to resume.
669 * Carry out a runtime resume of a device under the assumption that its
670 * pm_domain field points to the domain member of an object of type
671 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
673 static int pm_genpd_runtime_resume(struct device *dev)
675 struct generic_pm_domain *genpd;
676 DEFINE_WAIT(wait);
677 int ret;
679 dev_dbg(dev, "%s()\n", __func__);
681 genpd = dev_to_genpd(dev);
682 if (IS_ERR(genpd))
683 return -EINVAL;
685 /* If power.irq_safe, the PM domain is never powered off. */
686 if (dev->power.irq_safe)
687 return genpd_start_dev_no_timing(genpd, dev);
689 mutex_lock(&genpd->lock);
690 ret = __pm_genpd_poweron(genpd);
691 if (ret) {
692 mutex_unlock(&genpd->lock);
693 return ret;
695 genpd->status = GPD_STATE_BUSY;
696 genpd->resume_count++;
697 for (;;) {
698 prepare_to_wait(&genpd->status_wait_queue, &wait,
699 TASK_UNINTERRUPTIBLE);
701 * If current is the powering off task, we have been called
702 * reentrantly from one of the device callbacks, so we should
703 * not wait.
705 if (!genpd->poweroff_task || genpd->poweroff_task == current)
706 break;
707 mutex_unlock(&genpd->lock);
709 schedule();
711 mutex_lock(&genpd->lock);
713 finish_wait(&genpd->status_wait_queue, &wait);
714 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
715 genpd->resume_count--;
716 genpd_set_active(genpd);
717 wake_up_all(&genpd->status_wait_queue);
718 mutex_unlock(&genpd->lock);
720 return 0;
723 static bool pd_ignore_unused;
724 static int __init pd_ignore_unused_setup(char *__unused)
726 pd_ignore_unused = true;
727 return 1;
729 __setup("pd_ignore_unused", pd_ignore_unused_setup);
732 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
734 void pm_genpd_poweroff_unused(void)
736 struct generic_pm_domain *genpd;
738 if (pd_ignore_unused) {
739 pr_warn("genpd: Not disabling unused power domains\n");
740 return;
743 mutex_lock(&gpd_list_lock);
745 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
746 genpd_queue_power_off_work(genpd);
748 mutex_unlock(&gpd_list_lock);
751 static int __init genpd_poweroff_unused(void)
753 pm_genpd_poweroff_unused();
754 return 0;
756 late_initcall(genpd_poweroff_unused);
758 #else
760 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
761 unsigned long val, void *ptr)
763 return NOTIFY_DONE;
766 static inline void
767 genpd_queue_power_off_work(struct generic_pm_domain *genpd) {}
769 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
771 #define pm_genpd_runtime_suspend NULL
772 #define pm_genpd_runtime_resume NULL
774 #endif /* CONFIG_PM_RUNTIME */
776 #ifdef CONFIG_PM_SLEEP
779 * pm_genpd_present - Check if the given PM domain has been initialized.
780 * @genpd: PM domain to check.
782 static bool pm_genpd_present(struct generic_pm_domain *genpd)
784 struct generic_pm_domain *gpd;
786 if (IS_ERR_OR_NULL(genpd))
787 return false;
789 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
790 if (gpd == genpd)
791 return true;
793 return false;
796 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
797 struct device *dev)
799 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
803 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
804 * @genpd: PM domain to power off, if possible.
806 * Check if the given PM domain can be powered off (during system suspend or
807 * hibernation) and do that if so. Also, in that case propagate to its masters.
809 * This function is only called in "noirq" and "syscore" stages of system power
810 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
811 * executed sequentially, so it is guaranteed that it will never run twice in
812 * parallel).
814 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
816 struct gpd_link *link;
818 if (genpd->status == GPD_STATE_POWER_OFF)
819 return;
821 if (genpd->suspended_count != genpd->device_count
822 || atomic_read(&genpd->sd_count) > 0)
823 return;
825 if (genpd->power_off)
826 genpd->power_off(genpd);
828 genpd->status = GPD_STATE_POWER_OFF;
830 list_for_each_entry(link, &genpd->slave_links, slave_node) {
831 genpd_sd_counter_dec(link->master);
832 pm_genpd_sync_poweroff(link->master);
837 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
838 * @genpd: PM domain to power on.
840 * This function is only called in "noirq" and "syscore" stages of system power
841 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
842 * executed sequentially, so it is guaranteed that it will never run twice in
843 * parallel).
845 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
847 struct gpd_link *link;
849 if (genpd->status != GPD_STATE_POWER_OFF)
850 return;
852 list_for_each_entry(link, &genpd->slave_links, slave_node) {
853 pm_genpd_sync_poweron(link->master);
854 genpd_sd_counter_inc(link->master);
857 if (genpd->power_on)
858 genpd->power_on(genpd);
860 genpd->status = GPD_STATE_ACTIVE;
864 * resume_needed - Check whether to resume a device before system suspend.
865 * @dev: Device to check.
866 * @genpd: PM domain the device belongs to.
868 * There are two cases in which a device that can wake up the system from sleep
869 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
870 * to wake up the system and it has to remain active for this purpose while the
871 * system is in the sleep state and (2) if the device is not enabled to wake up
872 * the system from sleep states and it generally doesn't generate wakeup signals
873 * by itself (those signals are generated on its behalf by other parts of the
874 * system). In the latter case it may be necessary to reconfigure the device's
875 * wakeup settings during system suspend, because it may have been set up to
876 * signal remote wakeup from the system's working state as needed by runtime PM.
877 * Return 'true' in either of the above cases.
879 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
881 bool active_wakeup;
883 if (!device_can_wakeup(dev))
884 return false;
886 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
887 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
891 * pm_genpd_prepare - Start power transition of a device in a PM domain.
892 * @dev: Device to start the transition of.
894 * Start a power transition of a device (during a system-wide power transition)
895 * under the assumption that its pm_domain field points to the domain member of
896 * an object of type struct generic_pm_domain representing a PM domain
897 * consisting of I/O devices.
899 static int pm_genpd_prepare(struct device *dev)
901 struct generic_pm_domain *genpd;
902 int ret;
904 dev_dbg(dev, "%s()\n", __func__);
906 genpd = dev_to_genpd(dev);
907 if (IS_ERR(genpd))
908 return -EINVAL;
911 * If a wakeup request is pending for the device, it should be woken up
912 * at this point and a system wakeup event should be reported if it's
913 * set up to wake up the system from sleep states.
915 pm_runtime_get_noresume(dev);
916 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
917 pm_wakeup_event(dev, 0);
919 if (pm_wakeup_pending()) {
920 pm_runtime_put(dev);
921 return -EBUSY;
924 if (resume_needed(dev, genpd))
925 pm_runtime_resume(dev);
927 genpd_acquire_lock(genpd);
929 if (genpd->prepared_count++ == 0) {
930 genpd->suspended_count = 0;
931 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
934 genpd_release_lock(genpd);
936 if (genpd->suspend_power_off) {
937 pm_runtime_put_noidle(dev);
938 return 0;
942 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
943 * so pm_genpd_poweron() will return immediately, but if the device
944 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
945 * to make it operational.
947 pm_runtime_resume(dev);
948 __pm_runtime_disable(dev, false);
950 ret = pm_generic_prepare(dev);
951 if (ret) {
952 mutex_lock(&genpd->lock);
954 if (--genpd->prepared_count == 0)
955 genpd->suspend_power_off = false;
957 mutex_unlock(&genpd->lock);
958 pm_runtime_enable(dev);
961 pm_runtime_put(dev);
962 return ret;
966 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
967 * @dev: Device to suspend.
969 * Suspend a device under the assumption that its pm_domain field points to the
970 * domain member of an object of type struct generic_pm_domain representing
971 * a PM domain consisting of I/O devices.
973 static int pm_genpd_suspend(struct device *dev)
975 struct generic_pm_domain *genpd;
977 dev_dbg(dev, "%s()\n", __func__);
979 genpd = dev_to_genpd(dev);
980 if (IS_ERR(genpd))
981 return -EINVAL;
983 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
987 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
988 * @dev: Device to suspend.
990 * Carry out a late suspend of a device under the assumption that its
991 * pm_domain field points to the domain member of an object of type
992 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
994 static int pm_genpd_suspend_late(struct device *dev)
996 struct generic_pm_domain *genpd;
998 dev_dbg(dev, "%s()\n", __func__);
1000 genpd = dev_to_genpd(dev);
1001 if (IS_ERR(genpd))
1002 return -EINVAL;
1004 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
1008 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1009 * @dev: Device to suspend.
1011 * Stop the device and remove power from the domain if all devices in it have
1012 * been stopped.
1014 static int pm_genpd_suspend_noirq(struct device *dev)
1016 struct generic_pm_domain *genpd;
1018 dev_dbg(dev, "%s()\n", __func__);
1020 genpd = dev_to_genpd(dev);
1021 if (IS_ERR(genpd))
1022 return -EINVAL;
1024 if (genpd->suspend_power_off
1025 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1026 return 0;
1028 genpd_stop_dev(genpd, dev);
1031 * Since all of the "noirq" callbacks are executed sequentially, it is
1032 * guaranteed that this function will never run twice in parallel for
1033 * the same PM domain, so it is not necessary to use locking here.
1035 genpd->suspended_count++;
1036 pm_genpd_sync_poweroff(genpd);
1038 return 0;
1042 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1043 * @dev: Device to resume.
1045 * Restore power to the device's PM domain, if necessary, and start the device.
1047 static int pm_genpd_resume_noirq(struct device *dev)
1049 struct generic_pm_domain *genpd;
1051 dev_dbg(dev, "%s()\n", __func__);
1053 genpd = dev_to_genpd(dev);
1054 if (IS_ERR(genpd))
1055 return -EINVAL;
1057 if (genpd->suspend_power_off
1058 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1059 return 0;
1062 * Since all of the "noirq" callbacks are executed sequentially, it is
1063 * guaranteed that this function will never run twice in parallel for
1064 * the same PM domain, so it is not necessary to use locking here.
1066 pm_genpd_sync_poweron(genpd);
1067 genpd->suspended_count--;
1069 return genpd_start_dev(genpd, dev);
1073 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1074 * @dev: Device to resume.
1076 * Carry out an early resume of a device under the assumption that its
1077 * pm_domain field points to the domain member of an object of type
1078 * struct generic_pm_domain representing a power domain consisting of I/O
1079 * devices.
1081 static int pm_genpd_resume_early(struct device *dev)
1083 struct generic_pm_domain *genpd;
1085 dev_dbg(dev, "%s()\n", __func__);
1087 genpd = dev_to_genpd(dev);
1088 if (IS_ERR(genpd))
1089 return -EINVAL;
1091 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
1095 * pm_genpd_resume - Resume of device in an I/O PM domain.
1096 * @dev: Device to resume.
1098 * Resume a device under the assumption that its pm_domain field points to the
1099 * domain member of an object of type struct generic_pm_domain representing
1100 * a power domain consisting of I/O devices.
1102 static int pm_genpd_resume(struct device *dev)
1104 struct generic_pm_domain *genpd;
1106 dev_dbg(dev, "%s()\n", __func__);
1108 genpd = dev_to_genpd(dev);
1109 if (IS_ERR(genpd))
1110 return -EINVAL;
1112 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
1116 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1117 * @dev: Device to freeze.
1119 * Freeze a device under the assumption that its pm_domain field points to the
1120 * domain member of an object of type struct generic_pm_domain representing
1121 * a power domain consisting of I/O devices.
1123 static int pm_genpd_freeze(struct device *dev)
1125 struct generic_pm_domain *genpd;
1127 dev_dbg(dev, "%s()\n", __func__);
1129 genpd = dev_to_genpd(dev);
1130 if (IS_ERR(genpd))
1131 return -EINVAL;
1133 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
1137 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1138 * @dev: Device to freeze.
1140 * Carry out a late freeze of a device under the assumption that its
1141 * pm_domain field points to the domain member of an object of type
1142 * struct generic_pm_domain representing a power domain consisting of I/O
1143 * devices.
1145 static int pm_genpd_freeze_late(struct device *dev)
1147 struct generic_pm_domain *genpd;
1149 dev_dbg(dev, "%s()\n", __func__);
1151 genpd = dev_to_genpd(dev);
1152 if (IS_ERR(genpd))
1153 return -EINVAL;
1155 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
1159 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1160 * @dev: Device to freeze.
1162 * Carry out a late freeze of a device under the assumption that its
1163 * pm_domain field points to the domain member of an object of type
1164 * struct generic_pm_domain representing a power domain consisting of I/O
1165 * devices.
1167 static int pm_genpd_freeze_noirq(struct device *dev)
1169 struct generic_pm_domain *genpd;
1171 dev_dbg(dev, "%s()\n", __func__);
1173 genpd = dev_to_genpd(dev);
1174 if (IS_ERR(genpd))
1175 return -EINVAL;
1177 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1181 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1182 * @dev: Device to thaw.
1184 * Start the device, unless power has been removed from the domain already
1185 * before the system transition.
1187 static int pm_genpd_thaw_noirq(struct device *dev)
1189 struct generic_pm_domain *genpd;
1191 dev_dbg(dev, "%s()\n", __func__);
1193 genpd = dev_to_genpd(dev);
1194 if (IS_ERR(genpd))
1195 return -EINVAL;
1197 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1201 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1202 * @dev: Device to thaw.
1204 * Carry out an early thaw of a device under the assumption that its
1205 * pm_domain field points to the domain member of an object of type
1206 * struct generic_pm_domain representing a power domain consisting of I/O
1207 * devices.
1209 static int pm_genpd_thaw_early(struct device *dev)
1211 struct generic_pm_domain *genpd;
1213 dev_dbg(dev, "%s()\n", __func__);
1215 genpd = dev_to_genpd(dev);
1216 if (IS_ERR(genpd))
1217 return -EINVAL;
1219 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
1223 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1224 * @dev: Device to thaw.
1226 * Thaw a device under the assumption that its pm_domain field points to the
1227 * domain member of an object of type struct generic_pm_domain representing
1228 * a power domain consisting of I/O devices.
1230 static int pm_genpd_thaw(struct device *dev)
1232 struct generic_pm_domain *genpd;
1234 dev_dbg(dev, "%s()\n", __func__);
1236 genpd = dev_to_genpd(dev);
1237 if (IS_ERR(genpd))
1238 return -EINVAL;
1240 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1244 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1245 * @dev: Device to resume.
1247 * Make sure the domain will be in the same power state as before the
1248 * hibernation the system is resuming from and start the device if necessary.
1250 static int pm_genpd_restore_noirq(struct device *dev)
1252 struct generic_pm_domain *genpd;
1254 dev_dbg(dev, "%s()\n", __func__);
1256 genpd = dev_to_genpd(dev);
1257 if (IS_ERR(genpd))
1258 return -EINVAL;
1261 * Since all of the "noirq" callbacks are executed sequentially, it is
1262 * guaranteed that this function will never run twice in parallel for
1263 * the same PM domain, so it is not necessary to use locking here.
1265 * At this point suspended_count == 0 means we are being run for the
1266 * first time for the given domain in the present cycle.
1268 if (genpd->suspended_count++ == 0) {
1270 * The boot kernel might put the domain into arbitrary state,
1271 * so make it appear as powered off to pm_genpd_sync_poweron(),
1272 * so that it tries to power it on in case it was really off.
1274 genpd->status = GPD_STATE_POWER_OFF;
1275 if (genpd->suspend_power_off) {
1277 * If the domain was off before the hibernation, make
1278 * sure it will be off going forward.
1280 if (genpd->power_off)
1281 genpd->power_off(genpd);
1283 return 0;
1287 if (genpd->suspend_power_off)
1288 return 0;
1290 pm_genpd_sync_poweron(genpd);
1292 return genpd_start_dev(genpd, dev);
1296 * pm_genpd_complete - Complete power transition of a device in a power domain.
1297 * @dev: Device to complete the transition of.
1299 * Complete a power transition of a device (during a system-wide power
1300 * transition) under the assumption that its pm_domain field points to the
1301 * domain member of an object of type struct generic_pm_domain representing
1302 * a power domain consisting of I/O devices.
1304 static void pm_genpd_complete(struct device *dev)
1306 struct generic_pm_domain *genpd;
1307 bool run_complete;
1309 dev_dbg(dev, "%s()\n", __func__);
1311 genpd = dev_to_genpd(dev);
1312 if (IS_ERR(genpd))
1313 return;
1315 mutex_lock(&genpd->lock);
1317 run_complete = !genpd->suspend_power_off;
1318 if (--genpd->prepared_count == 0)
1319 genpd->suspend_power_off = false;
1321 mutex_unlock(&genpd->lock);
1323 if (run_complete) {
1324 pm_generic_complete(dev);
1325 pm_runtime_set_active(dev);
1326 pm_runtime_enable(dev);
1327 pm_request_idle(dev);
1332 * genpd_syscore_switch - Switch power during system core suspend or resume.
1333 * @dev: Device that normally is marked as "always on" to switch power for.
1335 * This routine may only be called during the system core (syscore) suspend or
1336 * resume phase for devices whose "always on" flags are set.
1338 static void genpd_syscore_switch(struct device *dev, bool suspend)
1340 struct generic_pm_domain *genpd;
1342 genpd = dev_to_genpd(dev);
1343 if (!pm_genpd_present(genpd))
1344 return;
1346 if (suspend) {
1347 genpd->suspended_count++;
1348 pm_genpd_sync_poweroff(genpd);
1349 } else {
1350 pm_genpd_sync_poweron(genpd);
1351 genpd->suspended_count--;
1355 void pm_genpd_syscore_poweroff(struct device *dev)
1357 genpd_syscore_switch(dev, true);
1359 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1361 void pm_genpd_syscore_poweron(struct device *dev)
1363 genpd_syscore_switch(dev, false);
1365 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1367 #else
1369 #define pm_genpd_prepare NULL
1370 #define pm_genpd_suspend NULL
1371 #define pm_genpd_suspend_late NULL
1372 #define pm_genpd_suspend_noirq NULL
1373 #define pm_genpd_resume_early NULL
1374 #define pm_genpd_resume_noirq NULL
1375 #define pm_genpd_resume NULL
1376 #define pm_genpd_freeze NULL
1377 #define pm_genpd_freeze_late NULL
1378 #define pm_genpd_freeze_noirq NULL
1379 #define pm_genpd_thaw_early NULL
1380 #define pm_genpd_thaw_noirq NULL
1381 #define pm_genpd_thaw NULL
1382 #define pm_genpd_restore_noirq NULL
1383 #define pm_genpd_complete NULL
1385 #endif /* CONFIG_PM_SLEEP */
1387 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1389 struct generic_pm_domain_data *gpd_data;
1391 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1392 if (!gpd_data)
1393 return NULL;
1395 mutex_init(&gpd_data->lock);
1396 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1397 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1398 return gpd_data;
1401 static void __pm_genpd_free_dev_data(struct device *dev,
1402 struct generic_pm_domain_data *gpd_data)
1404 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1405 kfree(gpd_data);
1409 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1410 * @genpd: PM domain to add the device to.
1411 * @dev: Device to be added.
1412 * @td: Set of PM QoS timing parameters to attach to the device.
1414 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1415 struct gpd_timing_data *td)
1417 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1418 struct pm_domain_data *pdd;
1419 int ret = 0;
1421 dev_dbg(dev, "%s()\n", __func__);
1423 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1424 return -EINVAL;
1426 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1427 if (!gpd_data_new)
1428 return -ENOMEM;
1430 genpd_acquire_lock(genpd);
1432 if (genpd->prepared_count > 0) {
1433 ret = -EAGAIN;
1434 goto out;
1437 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1438 if (pdd->dev == dev) {
1439 ret = -EINVAL;
1440 goto out;
1443 ret = dev_pm_get_subsys_data(dev);
1444 if (ret)
1445 goto out;
1447 genpd->device_count++;
1448 genpd->max_off_time_changed = true;
1450 spin_lock_irq(&dev->power.lock);
1452 dev->pm_domain = &genpd->domain;
1453 if (dev->power.subsys_data->domain_data) {
1454 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1455 } else {
1456 gpd_data = gpd_data_new;
1457 dev->power.subsys_data->domain_data = &gpd_data->base;
1459 gpd_data->refcount++;
1460 if (td)
1461 gpd_data->td = *td;
1463 spin_unlock_irq(&dev->power.lock);
1465 if (genpd->attach_dev)
1466 genpd->attach_dev(genpd, dev);
1468 mutex_lock(&gpd_data->lock);
1469 gpd_data->base.dev = dev;
1470 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1471 gpd_data->need_restore = -1;
1472 gpd_data->td.constraint_changed = true;
1473 gpd_data->td.effective_constraint_ns = -1;
1474 mutex_unlock(&gpd_data->lock);
1476 out:
1477 genpd_release_lock(genpd);
1479 if (gpd_data != gpd_data_new)
1480 __pm_genpd_free_dev_data(dev, gpd_data_new);
1482 return ret;
1486 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1487 * @domain_name: Name of the PM domain to add the device to.
1488 * @dev: Device to be added.
1489 * @td: Set of PM QoS timing parameters to attach to the device.
1491 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1492 struct gpd_timing_data *td)
1494 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1498 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1499 * @genpd: PM domain to remove the device from.
1500 * @dev: Device to be removed.
1502 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1503 struct device *dev)
1505 struct generic_pm_domain_data *gpd_data;
1506 struct pm_domain_data *pdd;
1507 bool remove = false;
1508 int ret = 0;
1510 dev_dbg(dev, "%s()\n", __func__);
1512 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1513 || IS_ERR_OR_NULL(dev->pm_domain)
1514 || pd_to_genpd(dev->pm_domain) != genpd)
1515 return -EINVAL;
1517 genpd_acquire_lock(genpd);
1519 if (genpd->prepared_count > 0) {
1520 ret = -EAGAIN;
1521 goto out;
1524 genpd->device_count--;
1525 genpd->max_off_time_changed = true;
1527 if (genpd->detach_dev)
1528 genpd->detach_dev(genpd, dev);
1530 spin_lock_irq(&dev->power.lock);
1532 dev->pm_domain = NULL;
1533 pdd = dev->power.subsys_data->domain_data;
1534 list_del_init(&pdd->list_node);
1535 gpd_data = to_gpd_data(pdd);
1536 if (--gpd_data->refcount == 0) {
1537 dev->power.subsys_data->domain_data = NULL;
1538 remove = true;
1541 spin_unlock_irq(&dev->power.lock);
1543 mutex_lock(&gpd_data->lock);
1544 pdd->dev = NULL;
1545 mutex_unlock(&gpd_data->lock);
1547 genpd_release_lock(genpd);
1549 dev_pm_put_subsys_data(dev);
1550 if (remove)
1551 __pm_genpd_free_dev_data(dev, gpd_data);
1553 return 0;
1555 out:
1556 genpd_release_lock(genpd);
1558 return ret;
1562 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1563 * @dev: Device to set/unset the flag for.
1564 * @val: The new value of the device's "need restore" flag.
1566 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1568 struct pm_subsys_data *psd;
1569 unsigned long flags;
1571 spin_lock_irqsave(&dev->power.lock, flags);
1573 psd = dev_to_psd(dev);
1574 if (psd && psd->domain_data)
1575 to_gpd_data(psd->domain_data)->need_restore = val ? 1 : 0;
1577 spin_unlock_irqrestore(&dev->power.lock, flags);
1579 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1582 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1583 * @genpd: Master PM domain to add the subdomain to.
1584 * @subdomain: Subdomain to be added.
1586 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1587 struct generic_pm_domain *subdomain)
1589 struct gpd_link *link;
1590 int ret = 0;
1592 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1593 || genpd == subdomain)
1594 return -EINVAL;
1596 start:
1597 genpd_acquire_lock(genpd);
1598 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1600 if (subdomain->status != GPD_STATE_POWER_OFF
1601 && subdomain->status != GPD_STATE_ACTIVE) {
1602 mutex_unlock(&subdomain->lock);
1603 genpd_release_lock(genpd);
1604 goto start;
1607 if (genpd->status == GPD_STATE_POWER_OFF
1608 && subdomain->status != GPD_STATE_POWER_OFF) {
1609 ret = -EINVAL;
1610 goto out;
1613 list_for_each_entry(link, &genpd->master_links, master_node) {
1614 if (link->slave == subdomain && link->master == genpd) {
1615 ret = -EINVAL;
1616 goto out;
1620 link = kzalloc(sizeof(*link), GFP_KERNEL);
1621 if (!link) {
1622 ret = -ENOMEM;
1623 goto out;
1625 link->master = genpd;
1626 list_add_tail(&link->master_node, &genpd->master_links);
1627 link->slave = subdomain;
1628 list_add_tail(&link->slave_node, &subdomain->slave_links);
1629 if (subdomain->status != GPD_STATE_POWER_OFF)
1630 genpd_sd_counter_inc(genpd);
1632 out:
1633 mutex_unlock(&subdomain->lock);
1634 genpd_release_lock(genpd);
1636 return ret;
1640 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1641 * @master_name: Name of the master PM domain to add the subdomain to.
1642 * @subdomain_name: Name of the subdomain to be added.
1644 int pm_genpd_add_subdomain_names(const char *master_name,
1645 const char *subdomain_name)
1647 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1649 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1650 return -EINVAL;
1652 mutex_lock(&gpd_list_lock);
1653 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1654 if (!master && !strcmp(gpd->name, master_name))
1655 master = gpd;
1657 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1658 subdomain = gpd;
1660 if (master && subdomain)
1661 break;
1663 mutex_unlock(&gpd_list_lock);
1665 return pm_genpd_add_subdomain(master, subdomain);
1669 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1670 * @genpd: Master PM domain to remove the subdomain from.
1671 * @subdomain: Subdomain to be removed.
1673 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1674 struct generic_pm_domain *subdomain)
1676 struct gpd_link *l, *link;
1677 int ret = -EINVAL;
1679 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1680 return -EINVAL;
1682 start:
1683 genpd_acquire_lock(genpd);
1685 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1686 if (link->slave != subdomain)
1687 continue;
1689 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1691 if (subdomain->status != GPD_STATE_POWER_OFF
1692 && subdomain->status != GPD_STATE_ACTIVE) {
1693 mutex_unlock(&subdomain->lock);
1694 genpd_release_lock(genpd);
1695 goto start;
1698 list_del(&link->master_node);
1699 list_del(&link->slave_node);
1700 kfree(link);
1701 if (subdomain->status != GPD_STATE_POWER_OFF)
1702 genpd_sd_counter_dec(genpd);
1704 mutex_unlock(&subdomain->lock);
1706 ret = 0;
1707 break;
1710 genpd_release_lock(genpd);
1712 return ret;
1716 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1717 * @genpd: PM domain to be connected with cpuidle.
1718 * @state: cpuidle state this domain can disable/enable.
1720 * Make a PM domain behave as though it contained a CPU core, that is, instead
1721 * of calling its power down routine it will enable the given cpuidle state so
1722 * that the cpuidle subsystem can power it down (if possible and desirable).
1724 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1726 struct cpuidle_driver *cpuidle_drv;
1727 struct gpd_cpuidle_data *cpuidle_data;
1728 struct cpuidle_state *idle_state;
1729 int ret = 0;
1731 if (IS_ERR_OR_NULL(genpd) || state < 0)
1732 return -EINVAL;
1734 genpd_acquire_lock(genpd);
1736 if (genpd->cpuidle_data) {
1737 ret = -EEXIST;
1738 goto out;
1740 cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
1741 if (!cpuidle_data) {
1742 ret = -ENOMEM;
1743 goto out;
1745 cpuidle_drv = cpuidle_driver_ref();
1746 if (!cpuidle_drv) {
1747 ret = -ENODEV;
1748 goto err_drv;
1750 if (cpuidle_drv->state_count <= state) {
1751 ret = -EINVAL;
1752 goto err;
1754 idle_state = &cpuidle_drv->states[state];
1755 if (!idle_state->disabled) {
1756 ret = -EAGAIN;
1757 goto err;
1759 cpuidle_data->idle_state = idle_state;
1760 cpuidle_data->saved_exit_latency = idle_state->exit_latency;
1761 genpd->cpuidle_data = cpuidle_data;
1762 genpd_recalc_cpu_exit_latency(genpd);
1764 out:
1765 genpd_release_lock(genpd);
1766 return ret;
1768 err:
1769 cpuidle_driver_unref();
1771 err_drv:
1772 kfree(cpuidle_data);
1773 goto out;
1777 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1778 * @name: Name of the domain to connect to cpuidle.
1779 * @state: cpuidle state this domain can manipulate.
1781 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1783 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1787 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1788 * @genpd: PM domain to remove the cpuidle connection from.
1790 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1791 * given PM domain.
1793 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1795 struct gpd_cpuidle_data *cpuidle_data;
1796 struct cpuidle_state *idle_state;
1797 int ret = 0;
1799 if (IS_ERR_OR_NULL(genpd))
1800 return -EINVAL;
1802 genpd_acquire_lock(genpd);
1804 cpuidle_data = genpd->cpuidle_data;
1805 if (!cpuidle_data) {
1806 ret = -ENODEV;
1807 goto out;
1809 idle_state = cpuidle_data->idle_state;
1810 if (!idle_state->disabled) {
1811 ret = -EAGAIN;
1812 goto out;
1814 idle_state->exit_latency = cpuidle_data->saved_exit_latency;
1815 cpuidle_driver_unref();
1816 genpd->cpuidle_data = NULL;
1817 kfree(cpuidle_data);
1819 out:
1820 genpd_release_lock(genpd);
1821 return ret;
1825 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1826 * @name: Name of the domain to disconnect cpuidle from.
1828 int pm_genpd_name_detach_cpuidle(const char *name)
1830 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1833 /* Default device callbacks for generic PM domains. */
1836 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1837 * @dev: Device to handle.
1839 static int pm_genpd_default_save_state(struct device *dev)
1841 int (*cb)(struct device *__dev);
1843 if (dev->type && dev->type->pm)
1844 cb = dev->type->pm->runtime_suspend;
1845 else if (dev->class && dev->class->pm)
1846 cb = dev->class->pm->runtime_suspend;
1847 else if (dev->bus && dev->bus->pm)
1848 cb = dev->bus->pm->runtime_suspend;
1849 else
1850 cb = NULL;
1852 if (!cb && dev->driver && dev->driver->pm)
1853 cb = dev->driver->pm->runtime_suspend;
1855 return cb ? cb(dev) : 0;
1859 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1860 * @dev: Device to handle.
1862 static int pm_genpd_default_restore_state(struct device *dev)
1864 int (*cb)(struct device *__dev);
1866 if (dev->type && dev->type->pm)
1867 cb = dev->type->pm->runtime_resume;
1868 else if (dev->class && dev->class->pm)
1869 cb = dev->class->pm->runtime_resume;
1870 else if (dev->bus && dev->bus->pm)
1871 cb = dev->bus->pm->runtime_resume;
1872 else
1873 cb = NULL;
1875 if (!cb && dev->driver && dev->driver->pm)
1876 cb = dev->driver->pm->runtime_resume;
1878 return cb ? cb(dev) : 0;
1882 * pm_genpd_init - Initialize a generic I/O PM domain object.
1883 * @genpd: PM domain object to initialize.
1884 * @gov: PM domain governor to associate with the domain (may be NULL).
1885 * @is_off: Initial value of the domain's power_is_off field.
1887 void pm_genpd_init(struct generic_pm_domain *genpd,
1888 struct dev_power_governor *gov, bool is_off)
1890 if (IS_ERR_OR_NULL(genpd))
1891 return;
1893 INIT_LIST_HEAD(&genpd->master_links);
1894 INIT_LIST_HEAD(&genpd->slave_links);
1895 INIT_LIST_HEAD(&genpd->dev_list);
1896 mutex_init(&genpd->lock);
1897 genpd->gov = gov;
1898 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1899 genpd->in_progress = 0;
1900 atomic_set(&genpd->sd_count, 0);
1901 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1902 init_waitqueue_head(&genpd->status_wait_queue);
1903 genpd->poweroff_task = NULL;
1904 genpd->resume_count = 0;
1905 genpd->device_count = 0;
1906 genpd->max_off_time_ns = -1;
1907 genpd->max_off_time_changed = true;
1908 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1909 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1910 genpd->domain.ops.prepare = pm_genpd_prepare;
1911 genpd->domain.ops.suspend = pm_genpd_suspend;
1912 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1913 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1914 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1915 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1916 genpd->domain.ops.resume = pm_genpd_resume;
1917 genpd->domain.ops.freeze = pm_genpd_freeze;
1918 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1919 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1920 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1921 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1922 genpd->domain.ops.thaw = pm_genpd_thaw;
1923 genpd->domain.ops.poweroff = pm_genpd_suspend;
1924 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1925 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1926 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1927 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1928 genpd->domain.ops.restore = pm_genpd_resume;
1929 genpd->domain.ops.complete = pm_genpd_complete;
1930 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1931 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1932 mutex_lock(&gpd_list_lock);
1933 list_add(&genpd->gpd_list_node, &gpd_list);
1934 mutex_unlock(&gpd_list_lock);
1937 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1939 * Device Tree based PM domain providers.
1941 * The code below implements generic device tree based PM domain providers that
1942 * bind device tree nodes with generic PM domains registered in the system.
1944 * Any driver that registers generic PM domains and needs to support binding of
1945 * devices to these domains is supposed to register a PM domain provider, which
1946 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1948 * Two simple mapping functions have been provided for convenience:
1949 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1950 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1951 * index.
1955 * struct of_genpd_provider - PM domain provider registration structure
1956 * @link: Entry in global list of PM domain providers
1957 * @node: Pointer to device tree node of PM domain provider
1958 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1959 * into a PM domain.
1960 * @data: context pointer to be passed into @xlate callback
1962 struct of_genpd_provider {
1963 struct list_head link;
1964 struct device_node *node;
1965 genpd_xlate_t xlate;
1966 void *data;
1969 /* List of registered PM domain providers. */
1970 static LIST_HEAD(of_genpd_providers);
1971 /* Mutex to protect the list above. */
1972 static DEFINE_MUTEX(of_genpd_mutex);
1975 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1976 * @genpdspec: OF phandle args to map into a PM domain
1977 * @data: xlate function private data - pointer to struct generic_pm_domain
1979 * This is a generic xlate function that can be used to model PM domains that
1980 * have their own device tree nodes. The private data of xlate function needs
1981 * to be a valid pointer to struct generic_pm_domain.
1983 struct generic_pm_domain *__of_genpd_xlate_simple(
1984 struct of_phandle_args *genpdspec,
1985 void *data)
1987 if (genpdspec->args_count != 0)
1988 return ERR_PTR(-EINVAL);
1989 return data;
1991 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1994 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1995 * @genpdspec: OF phandle args to map into a PM domain
1996 * @data: xlate function private data - pointer to struct genpd_onecell_data
1998 * This is a generic xlate function that can be used to model simple PM domain
1999 * controllers that have one device tree node and provide multiple PM domains.
2000 * A single cell is used as an index into an array of PM domains specified in
2001 * the genpd_onecell_data struct when registering the provider.
2003 struct generic_pm_domain *__of_genpd_xlate_onecell(
2004 struct of_phandle_args *genpdspec,
2005 void *data)
2007 struct genpd_onecell_data *genpd_data = data;
2008 unsigned int idx = genpdspec->args[0];
2010 if (genpdspec->args_count != 1)
2011 return ERR_PTR(-EINVAL);
2013 if (idx >= genpd_data->num_domains) {
2014 pr_err("%s: invalid domain index %u\n", __func__, idx);
2015 return ERR_PTR(-EINVAL);
2018 if (!genpd_data->domains[idx])
2019 return ERR_PTR(-ENOENT);
2021 return genpd_data->domains[idx];
2023 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
2026 * __of_genpd_add_provider() - Register a PM domain provider for a node
2027 * @np: Device node pointer associated with the PM domain provider.
2028 * @xlate: Callback for decoding PM domain from phandle arguments.
2029 * @data: Context pointer for @xlate callback.
2031 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2032 void *data)
2034 struct of_genpd_provider *cp;
2036 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2037 if (!cp)
2038 return -ENOMEM;
2040 cp->node = of_node_get(np);
2041 cp->data = data;
2042 cp->xlate = xlate;
2044 mutex_lock(&of_genpd_mutex);
2045 list_add(&cp->link, &of_genpd_providers);
2046 mutex_unlock(&of_genpd_mutex);
2047 pr_debug("Added domain provider from %s\n", np->full_name);
2049 return 0;
2051 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
2054 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2055 * @np: Device node pointer associated with the PM domain provider
2057 void of_genpd_del_provider(struct device_node *np)
2059 struct of_genpd_provider *cp, *tmp;
2061 mutex_lock(&of_genpd_mutex);
2062 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2063 if (cp->node == np) {
2064 list_del(&cp->link);
2065 of_node_put(cp->node);
2066 kfree(cp);
2067 break;
2070 mutex_unlock(&of_genpd_mutex);
2072 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2075 * of_genpd_get_from_provider() - Look-up PM domain
2076 * @genpdspec: OF phandle args to use for look-up
2078 * Looks for a PM domain provider under the node specified by @genpdspec and if
2079 * found, uses xlate function of the provider to map phandle args to a PM
2080 * domain.
2082 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2083 * on failure.
2085 static struct generic_pm_domain *of_genpd_get_from_provider(
2086 struct of_phandle_args *genpdspec)
2088 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2089 struct of_genpd_provider *provider;
2091 mutex_lock(&of_genpd_mutex);
2093 /* Check if we have such a provider in our array */
2094 list_for_each_entry(provider, &of_genpd_providers, link) {
2095 if (provider->node == genpdspec->np)
2096 genpd = provider->xlate(genpdspec, provider->data);
2097 if (!IS_ERR(genpd))
2098 break;
2101 mutex_unlock(&of_genpd_mutex);
2103 return genpd;
2107 * genpd_dev_pm_detach - Detach a device from its PM domain.
2108 * @dev: Device to attach.
2109 * @power_off: Currently not used
2111 * Try to locate a corresponding generic PM domain, which the device was
2112 * attached to previously. If such is found, the device is detached from it.
2114 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2116 struct generic_pm_domain *pd = NULL, *gpd;
2117 int ret = 0;
2119 if (!dev->pm_domain)
2120 return;
2122 mutex_lock(&gpd_list_lock);
2123 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2124 if (&gpd->domain == dev->pm_domain) {
2125 pd = gpd;
2126 break;
2129 mutex_unlock(&gpd_list_lock);
2131 if (!pd)
2132 return;
2134 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2136 while (1) {
2137 ret = pm_genpd_remove_device(pd, dev);
2138 if (ret != -EAGAIN)
2139 break;
2140 cond_resched();
2143 if (ret < 0) {
2144 dev_err(dev, "failed to remove from PM domain %s: %d",
2145 pd->name, ret);
2146 return;
2149 /* Check if PM domain can be powered off after removing this device. */
2150 genpd_queue_power_off_work(pd);
2154 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2155 * @dev: Device to attach.
2157 * Parse device's OF node to find a PM domain specifier. If such is found,
2158 * attaches the device to retrieved pm_domain ops.
2160 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2161 * backwards compatibility with existing DTBs.
2163 * Returns 0 on successfully attached PM domain or negative error code.
2165 int genpd_dev_pm_attach(struct device *dev)
2167 struct of_phandle_args pd_args;
2168 struct generic_pm_domain *pd;
2169 int ret;
2171 if (!dev->of_node)
2172 return -ENODEV;
2174 if (dev->pm_domain)
2175 return -EEXIST;
2177 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2178 "#power-domain-cells", 0, &pd_args);
2179 if (ret < 0) {
2180 if (ret != -ENOENT)
2181 return ret;
2184 * Try legacy Samsung-specific bindings
2185 * (for backwards compatibility of DT ABI)
2187 pd_args.args_count = 0;
2188 pd_args.np = of_parse_phandle(dev->of_node,
2189 "samsung,power-domain", 0);
2190 if (!pd_args.np)
2191 return -ENOENT;
2194 pd = of_genpd_get_from_provider(&pd_args);
2195 if (IS_ERR(pd)) {
2196 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2197 __func__, PTR_ERR(pd));
2198 of_node_put(dev->of_node);
2199 return PTR_ERR(pd);
2202 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2204 while (1) {
2205 ret = pm_genpd_add_device(pd, dev);
2206 if (ret != -EAGAIN)
2207 break;
2208 cond_resched();
2211 if (ret < 0) {
2212 dev_err(dev, "failed to add to PM domain %s: %d",
2213 pd->name, ret);
2214 of_node_put(dev->of_node);
2215 return ret;
2218 dev->pm_domain->detach = genpd_dev_pm_detach;
2220 return 0;
2222 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2223 #endif
2226 /*** debugfs support ***/
2228 #ifdef CONFIG_PM_ADVANCED_DEBUG
2229 #include <linux/pm.h>
2230 #include <linux/device.h>
2231 #include <linux/debugfs.h>
2232 #include <linux/seq_file.h>
2233 #include <linux/init.h>
2234 #include <linux/kobject.h>
2235 static struct dentry *pm_genpd_debugfs_dir;
2238 * TODO: This function is a slightly modified version of rtpm_status_show
2239 * from sysfs.c, but dependencies between PM_GENERIC_DOMAINS and PM_RUNTIME
2240 * are too loose to generalize it.
2242 #ifdef CONFIG_PM_RUNTIME
2243 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2245 static const char * const status_lookup[] = {
2246 [RPM_ACTIVE] = "active",
2247 [RPM_RESUMING] = "resuming",
2248 [RPM_SUSPENDED] = "suspended",
2249 [RPM_SUSPENDING] = "suspending"
2251 const char *p = "";
2253 if (dev->power.runtime_error)
2254 p = "error";
2255 else if (dev->power.disable_depth)
2256 p = "unsupported";
2257 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2258 p = status_lookup[dev->power.runtime_status];
2259 else
2260 WARN_ON(1);
2262 seq_puts(s, p);
2264 #else
2265 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2267 seq_puts(s, "active");
2269 #endif
2271 static int pm_genpd_summary_one(struct seq_file *s,
2272 struct generic_pm_domain *gpd)
2274 static const char * const status_lookup[] = {
2275 [GPD_STATE_ACTIVE] = "on",
2276 [GPD_STATE_WAIT_MASTER] = "wait-master",
2277 [GPD_STATE_BUSY] = "busy",
2278 [GPD_STATE_REPEAT] = "off-in-progress",
2279 [GPD_STATE_POWER_OFF] = "off"
2281 struct pm_domain_data *pm_data;
2282 const char *kobj_path;
2283 struct gpd_link *link;
2284 int ret;
2286 ret = mutex_lock_interruptible(&gpd->lock);
2287 if (ret)
2288 return -ERESTARTSYS;
2290 if (WARN_ON(gpd->status >= ARRAY_SIZE(status_lookup)))
2291 goto exit;
2292 seq_printf(s, "%-30s %-15s ", gpd->name, status_lookup[gpd->status]);
2295 * Modifications on the list require holding locks on both
2296 * master and slave, so we are safe.
2297 * Also gpd->name is immutable.
2299 list_for_each_entry(link, &gpd->master_links, master_node) {
2300 seq_printf(s, "%s", link->slave->name);
2301 if (!list_is_last(&link->master_node, &gpd->master_links))
2302 seq_puts(s, ", ");
2305 list_for_each_entry(pm_data, &gpd->dev_list, list_node) {
2306 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
2307 if (kobj_path == NULL)
2308 continue;
2310 seq_printf(s, "\n %-50s ", kobj_path);
2311 rtpm_status_str(s, pm_data->dev);
2312 kfree(kobj_path);
2315 seq_puts(s, "\n");
2316 exit:
2317 mutex_unlock(&gpd->lock);
2319 return 0;
2322 static int pm_genpd_summary_show(struct seq_file *s, void *data)
2324 struct generic_pm_domain *gpd;
2325 int ret = 0;
2327 seq_puts(s, " domain status slaves\n");
2328 seq_puts(s, " /device runtime status\n");
2329 seq_puts(s, "----------------------------------------------------------------------\n");
2331 ret = mutex_lock_interruptible(&gpd_list_lock);
2332 if (ret)
2333 return -ERESTARTSYS;
2335 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2336 ret = pm_genpd_summary_one(s, gpd);
2337 if (ret)
2338 break;
2340 mutex_unlock(&gpd_list_lock);
2342 return ret;
2345 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2347 return single_open(file, pm_genpd_summary_show, NULL);
2350 static const struct file_operations pm_genpd_summary_fops = {
2351 .open = pm_genpd_summary_open,
2352 .read = seq_read,
2353 .llseek = seq_lseek,
2354 .release = single_release,
2357 static int __init pm_genpd_debug_init(void)
2359 struct dentry *d;
2361 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2363 if (!pm_genpd_debugfs_dir)
2364 return -ENOMEM;
2366 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2367 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2368 if (!d)
2369 return -ENOMEM;
2371 return 0;
2373 late_initcall(pm_genpd_debug_init);
2375 static void __exit pm_genpd_debug_exit(void)
2377 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2379 __exitcall(pm_genpd_debug_exit);
2380 #endif /* CONFIG_PM_ADVANCED_DEBUG */