Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[linux-2.6.git] / drivers / base / power / domain.c
blobbfb8955c406c5ef02d819d4d5fa02d709f0a6ec0
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/init.h>
10 #include <linux/kernel.h>
11 #include <linux/io.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 } else { \
30 __routine = dev_gpd_data(dev)->ops.callback; \
31 if (__routine) \
32 __ret = __routine(dev); \
33 } \
34 __ret; \
37 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
38 ({ \
39 ktime_t __start = ktime_get(); \
40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
42 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
43 if (!__retval && __elapsed > __td->field) { \
44 __td->field = __elapsed; \
45 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
46 __elapsed); \
47 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
49 } \
50 __retval; \
53 static LIST_HEAD(gpd_list);
54 static DEFINE_MUTEX(gpd_list_lock);
56 static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
58 struct generic_pm_domain *genpd = NULL, *gpd;
60 if (IS_ERR_OR_NULL(domain_name))
61 return NULL;
63 mutex_lock(&gpd_list_lock);
64 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
65 if (!strcmp(gpd->name, domain_name)) {
66 genpd = gpd;
67 break;
70 mutex_unlock(&gpd_list_lock);
71 return genpd;
74 #ifdef CONFIG_PM
76 struct generic_pm_domain *dev_to_genpd(struct device *dev)
78 if (IS_ERR_OR_NULL(dev->pm_domain))
79 return ERR_PTR(-EINVAL);
81 return pd_to_genpd(dev->pm_domain);
84 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
86 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
87 stop_latency_ns, "stop");
90 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
92 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
93 start_latency_ns, "start");
96 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
98 bool ret = false;
100 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
101 ret = !!atomic_dec_and_test(&genpd->sd_count);
103 return ret;
106 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
108 atomic_inc(&genpd->sd_count);
109 smp_mb__after_atomic_inc();
112 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
114 DEFINE_WAIT(wait);
116 mutex_lock(&genpd->lock);
118 * Wait for the domain to transition into either the active,
119 * or the power off state.
121 for (;;) {
122 prepare_to_wait(&genpd->status_wait_queue, &wait,
123 TASK_UNINTERRUPTIBLE);
124 if (genpd->status == GPD_STATE_ACTIVE
125 || genpd->status == GPD_STATE_POWER_OFF)
126 break;
127 mutex_unlock(&genpd->lock);
129 schedule();
131 mutex_lock(&genpd->lock);
133 finish_wait(&genpd->status_wait_queue, &wait);
136 static void genpd_release_lock(struct generic_pm_domain *genpd)
138 mutex_unlock(&genpd->lock);
141 static void genpd_set_active(struct generic_pm_domain *genpd)
143 if (genpd->resume_count == 0)
144 genpd->status = GPD_STATE_ACTIVE;
147 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
149 s64 usecs64;
151 if (!genpd->cpu_data)
152 return;
154 usecs64 = genpd->power_on_latency_ns;
155 do_div(usecs64, NSEC_PER_USEC);
156 usecs64 += genpd->cpu_data->saved_exit_latency;
157 genpd->cpu_data->idle_state->exit_latency = usecs64;
161 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
162 * @genpd: PM domain to power up.
164 * Restore power to @genpd and all of its masters so that it is possible to
165 * resume a device belonging to it.
167 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
168 __releases(&genpd->lock) __acquires(&genpd->lock)
170 struct gpd_link *link;
171 DEFINE_WAIT(wait);
172 int ret = 0;
174 /* If the domain's master is being waited for, we have to wait too. */
175 for (;;) {
176 prepare_to_wait(&genpd->status_wait_queue, &wait,
177 TASK_UNINTERRUPTIBLE);
178 if (genpd->status != GPD_STATE_WAIT_MASTER)
179 break;
180 mutex_unlock(&genpd->lock);
182 schedule();
184 mutex_lock(&genpd->lock);
186 finish_wait(&genpd->status_wait_queue, &wait);
188 if (genpd->status == GPD_STATE_ACTIVE
189 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
190 return 0;
192 if (genpd->status != GPD_STATE_POWER_OFF) {
193 genpd_set_active(genpd);
194 return 0;
197 if (genpd->cpu_data) {
198 cpuidle_pause_and_lock();
199 genpd->cpu_data->idle_state->disabled = true;
200 cpuidle_resume_and_unlock();
201 goto out;
205 * The list is guaranteed not to change while the loop below is being
206 * executed, unless one of the masters' .power_on() callbacks fiddles
207 * with it.
209 list_for_each_entry(link, &genpd->slave_links, slave_node) {
210 genpd_sd_counter_inc(link->master);
211 genpd->status = GPD_STATE_WAIT_MASTER;
213 mutex_unlock(&genpd->lock);
215 ret = pm_genpd_poweron(link->master);
217 mutex_lock(&genpd->lock);
220 * The "wait for parent" status is guaranteed not to change
221 * while the master is powering on.
223 genpd->status = GPD_STATE_POWER_OFF;
224 wake_up_all(&genpd->status_wait_queue);
225 if (ret) {
226 genpd_sd_counter_dec(link->master);
227 goto err;
231 if (genpd->power_on) {
232 ktime_t time_start = ktime_get();
233 s64 elapsed_ns;
235 ret = genpd->power_on(genpd);
236 if (ret)
237 goto err;
239 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
240 if (elapsed_ns > genpd->power_on_latency_ns) {
241 genpd->power_on_latency_ns = elapsed_ns;
242 genpd->max_off_time_changed = true;
243 genpd_recalc_cpu_exit_latency(genpd);
244 if (genpd->name)
245 pr_warning("%s: Power-on latency exceeded, "
246 "new value %lld ns\n", genpd->name,
247 elapsed_ns);
251 out:
252 genpd_set_active(genpd);
254 return 0;
256 err:
257 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
258 genpd_sd_counter_dec(link->master);
260 return ret;
264 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
265 * @genpd: PM domain to power up.
267 int pm_genpd_poweron(struct generic_pm_domain *genpd)
269 int ret;
271 mutex_lock(&genpd->lock);
272 ret = __pm_genpd_poweron(genpd);
273 mutex_unlock(&genpd->lock);
274 return ret;
278 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
279 * @domain_name: Name of the PM domain to power up.
281 int pm_genpd_name_poweron(const char *domain_name)
283 struct generic_pm_domain *genpd;
285 genpd = pm_genpd_lookup_name(domain_name);
286 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
289 #endif /* CONFIG_PM */
291 #ifdef CONFIG_PM_RUNTIME
293 static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
294 struct device *dev)
296 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
299 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
301 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
302 save_state_latency_ns, "state save");
305 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
307 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
308 restore_state_latency_ns,
309 "state restore");
312 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
313 unsigned long val, void *ptr)
315 struct generic_pm_domain_data *gpd_data;
316 struct device *dev;
318 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
320 mutex_lock(&gpd_data->lock);
321 dev = gpd_data->base.dev;
322 if (!dev) {
323 mutex_unlock(&gpd_data->lock);
324 return NOTIFY_DONE;
326 mutex_unlock(&gpd_data->lock);
328 for (;;) {
329 struct generic_pm_domain *genpd;
330 struct pm_domain_data *pdd;
332 spin_lock_irq(&dev->power.lock);
334 pdd = dev->power.subsys_data ?
335 dev->power.subsys_data->domain_data : NULL;
336 if (pdd && pdd->dev) {
337 to_gpd_data(pdd)->td.constraint_changed = true;
338 genpd = dev_to_genpd(dev);
339 } else {
340 genpd = ERR_PTR(-ENODATA);
343 spin_unlock_irq(&dev->power.lock);
345 if (!IS_ERR(genpd)) {
346 mutex_lock(&genpd->lock);
347 genpd->max_off_time_changed = true;
348 mutex_unlock(&genpd->lock);
351 dev = dev->parent;
352 if (!dev || dev->power.ignore_children)
353 break;
356 return NOTIFY_DONE;
360 * __pm_genpd_save_device - Save the pre-suspend state of a device.
361 * @pdd: Domain data of the device to save the state of.
362 * @genpd: PM domain the device belongs to.
364 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
365 struct generic_pm_domain *genpd)
366 __releases(&genpd->lock) __acquires(&genpd->lock)
368 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
369 struct device *dev = pdd->dev;
370 int ret = 0;
372 if (gpd_data->need_restore)
373 return 0;
375 mutex_unlock(&genpd->lock);
377 genpd_start_dev(genpd, dev);
378 ret = genpd_save_dev(genpd, dev);
379 genpd_stop_dev(genpd, dev);
381 mutex_lock(&genpd->lock);
383 if (!ret)
384 gpd_data->need_restore = true;
386 return ret;
390 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
391 * @pdd: Domain data of the device to restore the state of.
392 * @genpd: PM domain the device belongs to.
394 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
395 struct generic_pm_domain *genpd)
396 __releases(&genpd->lock) __acquires(&genpd->lock)
398 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
399 struct device *dev = pdd->dev;
400 bool need_restore = gpd_data->need_restore;
402 gpd_data->need_restore = false;
403 mutex_unlock(&genpd->lock);
405 genpd_start_dev(genpd, dev);
406 if (need_restore)
407 genpd_restore_dev(genpd, dev);
409 mutex_lock(&genpd->lock);
413 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
414 * @genpd: PM domain to check.
416 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
417 * a "power off" operation, which means that a "power on" has occured in the
418 * meantime, or if its resume_count field is different from zero, which means
419 * that one of its devices has been resumed in the meantime.
421 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
423 return genpd->status == GPD_STATE_WAIT_MASTER
424 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
428 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
429 * @genpd: PM domait to power off.
431 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
432 * before.
434 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
436 queue_work(pm_wq, &genpd->power_off_work);
440 * pm_genpd_poweroff - Remove power from a given PM domain.
441 * @genpd: PM domain to power down.
443 * If all of the @genpd's devices have been suspended and all of its subdomains
444 * have been powered down, run the runtime suspend callbacks provided by all of
445 * the @genpd's devices' drivers and remove power from @genpd.
447 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
448 __releases(&genpd->lock) __acquires(&genpd->lock)
450 struct pm_domain_data *pdd;
451 struct gpd_link *link;
452 unsigned int not_suspended;
453 int ret = 0;
455 start:
457 * Do not try to power off the domain in the following situations:
458 * (1) The domain is already in the "power off" state.
459 * (2) The domain is waiting for its master to power up.
460 * (3) One of the domain's devices is being resumed right now.
461 * (4) System suspend is in progress.
463 if (genpd->status == GPD_STATE_POWER_OFF
464 || genpd->status == GPD_STATE_WAIT_MASTER
465 || genpd->resume_count > 0 || genpd->prepared_count > 0)
466 return 0;
468 if (atomic_read(&genpd->sd_count) > 0)
469 return -EBUSY;
471 not_suspended = 0;
472 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
473 enum pm_qos_flags_status stat;
475 stat = dev_pm_qos_flags(pdd->dev,
476 PM_QOS_FLAG_NO_POWER_OFF
477 | PM_QOS_FLAG_REMOTE_WAKEUP);
478 if (stat > PM_QOS_FLAGS_NONE)
479 return -EBUSY;
481 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
482 || pdd->dev->power.irq_safe))
483 not_suspended++;
486 if (not_suspended > genpd->in_progress)
487 return -EBUSY;
489 if (genpd->poweroff_task) {
491 * Another instance of pm_genpd_poweroff() is executing
492 * callbacks, so tell it to start over and return.
494 genpd->status = GPD_STATE_REPEAT;
495 return 0;
498 if (genpd->gov && genpd->gov->power_down_ok) {
499 if (!genpd->gov->power_down_ok(&genpd->domain))
500 return -EAGAIN;
503 genpd->status = GPD_STATE_BUSY;
504 genpd->poweroff_task = current;
506 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
507 ret = atomic_read(&genpd->sd_count) == 0 ?
508 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
510 if (genpd_abort_poweroff(genpd))
511 goto out;
513 if (ret) {
514 genpd_set_active(genpd);
515 goto out;
518 if (genpd->status == GPD_STATE_REPEAT) {
519 genpd->poweroff_task = NULL;
520 goto start;
524 if (genpd->cpu_data) {
526 * If cpu_data is set, cpuidle should turn the domain off when
527 * the CPU in it is idle. In that case we don't decrement the
528 * subdomain counts of the master domains, so that power is not
529 * removed from the current domain prematurely as a result of
530 * cutting off the masters' power.
532 genpd->status = GPD_STATE_POWER_OFF;
533 cpuidle_pause_and_lock();
534 genpd->cpu_data->idle_state->disabled = false;
535 cpuidle_resume_and_unlock();
536 goto out;
539 if (genpd->power_off) {
540 ktime_t time_start;
541 s64 elapsed_ns;
543 if (atomic_read(&genpd->sd_count) > 0) {
544 ret = -EBUSY;
545 goto out;
548 time_start = ktime_get();
551 * If sd_count > 0 at this point, one of the subdomains hasn't
552 * managed to call pm_genpd_poweron() for the master yet after
553 * incrementing it. In that case pm_genpd_poweron() will wait
554 * for us to drop the lock, so we can call .power_off() and let
555 * the pm_genpd_poweron() restore power for us (this shouldn't
556 * happen very often).
558 ret = genpd->power_off(genpd);
559 if (ret == -EBUSY) {
560 genpd_set_active(genpd);
561 goto out;
564 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
565 if (elapsed_ns > genpd->power_off_latency_ns) {
566 genpd->power_off_latency_ns = elapsed_ns;
567 genpd->max_off_time_changed = true;
568 if (genpd->name)
569 pr_warning("%s: Power-off latency exceeded, "
570 "new value %lld ns\n", genpd->name,
571 elapsed_ns);
575 genpd->status = GPD_STATE_POWER_OFF;
577 list_for_each_entry(link, &genpd->slave_links, slave_node) {
578 genpd_sd_counter_dec(link->master);
579 genpd_queue_power_off_work(link->master);
582 out:
583 genpd->poweroff_task = NULL;
584 wake_up_all(&genpd->status_wait_queue);
585 return ret;
589 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
590 * @work: Work structure used for scheduling the execution of this function.
592 static void genpd_power_off_work_fn(struct work_struct *work)
594 struct generic_pm_domain *genpd;
596 genpd = container_of(work, struct generic_pm_domain, power_off_work);
598 genpd_acquire_lock(genpd);
599 pm_genpd_poweroff(genpd);
600 genpd_release_lock(genpd);
604 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
605 * @dev: Device to suspend.
607 * Carry out a runtime suspend of a device under the assumption that its
608 * pm_domain field points to the domain member of an object of type
609 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
611 static int pm_genpd_runtime_suspend(struct device *dev)
613 struct generic_pm_domain *genpd;
614 bool (*stop_ok)(struct device *__dev);
615 int ret;
617 dev_dbg(dev, "%s()\n", __func__);
619 genpd = dev_to_genpd(dev);
620 if (IS_ERR(genpd))
621 return -EINVAL;
623 might_sleep_if(!genpd->dev_irq_safe);
625 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
626 if (stop_ok && !stop_ok(dev))
627 return -EBUSY;
629 ret = genpd_stop_dev(genpd, dev);
630 if (ret)
631 return ret;
634 * If power.irq_safe is set, this routine will be run with interrupts
635 * off, so it can't use mutexes.
637 if (dev->power.irq_safe)
638 return 0;
640 mutex_lock(&genpd->lock);
641 genpd->in_progress++;
642 pm_genpd_poweroff(genpd);
643 genpd->in_progress--;
644 mutex_unlock(&genpd->lock);
646 return 0;
650 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
651 * @dev: Device to resume.
653 * Carry out a runtime resume of a device under the assumption that its
654 * pm_domain field points to the domain member of an object of type
655 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
657 static int pm_genpd_runtime_resume(struct device *dev)
659 struct generic_pm_domain *genpd;
660 DEFINE_WAIT(wait);
661 int ret;
663 dev_dbg(dev, "%s()\n", __func__);
665 genpd = dev_to_genpd(dev);
666 if (IS_ERR(genpd))
667 return -EINVAL;
669 might_sleep_if(!genpd->dev_irq_safe);
671 /* If power.irq_safe, the PM domain is never powered off. */
672 if (dev->power.irq_safe)
673 return genpd_start_dev_no_timing(genpd, dev);
675 mutex_lock(&genpd->lock);
676 ret = __pm_genpd_poweron(genpd);
677 if (ret) {
678 mutex_unlock(&genpd->lock);
679 return ret;
681 genpd->status = GPD_STATE_BUSY;
682 genpd->resume_count++;
683 for (;;) {
684 prepare_to_wait(&genpd->status_wait_queue, &wait,
685 TASK_UNINTERRUPTIBLE);
687 * If current is the powering off task, we have been called
688 * reentrantly from one of the device callbacks, so we should
689 * not wait.
691 if (!genpd->poweroff_task || genpd->poweroff_task == current)
692 break;
693 mutex_unlock(&genpd->lock);
695 schedule();
697 mutex_lock(&genpd->lock);
699 finish_wait(&genpd->status_wait_queue, &wait);
700 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
701 genpd->resume_count--;
702 genpd_set_active(genpd);
703 wake_up_all(&genpd->status_wait_queue);
704 mutex_unlock(&genpd->lock);
706 return 0;
710 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
712 void pm_genpd_poweroff_unused(void)
714 struct generic_pm_domain *genpd;
716 mutex_lock(&gpd_list_lock);
718 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
719 genpd_queue_power_off_work(genpd);
721 mutex_unlock(&gpd_list_lock);
724 #else
726 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
727 unsigned long val, void *ptr)
729 return NOTIFY_DONE;
732 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
734 #define pm_genpd_runtime_suspend NULL
735 #define pm_genpd_runtime_resume NULL
737 #endif /* CONFIG_PM_RUNTIME */
739 #ifdef CONFIG_PM_SLEEP
742 * pm_genpd_present - Check if the given PM domain has been initialized.
743 * @genpd: PM domain to check.
745 static bool pm_genpd_present(struct generic_pm_domain *genpd)
747 struct generic_pm_domain *gpd;
749 if (IS_ERR_OR_NULL(genpd))
750 return false;
752 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
753 if (gpd == genpd)
754 return true;
756 return false;
759 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
760 struct device *dev)
762 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
765 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
767 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
770 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
772 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
775 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
777 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
780 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
782 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
785 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
787 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
790 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
792 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
795 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
797 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
800 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
802 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
806 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
807 * @genpd: PM domain to power off, if possible.
809 * Check if the given PM domain can be powered off (during system suspend or
810 * hibernation) and do that if so. Also, in that case propagate to its masters.
812 * This function is only called in "noirq" and "syscore" stages of system power
813 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
814 * executed sequentially, so it is guaranteed that it will never run twice in
815 * parallel).
817 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
819 struct gpd_link *link;
821 if (genpd->status == GPD_STATE_POWER_OFF)
822 return;
824 if (genpd->suspended_count != genpd->device_count
825 || atomic_read(&genpd->sd_count) > 0)
826 return;
828 if (genpd->power_off)
829 genpd->power_off(genpd);
831 genpd->status = GPD_STATE_POWER_OFF;
833 list_for_each_entry(link, &genpd->slave_links, slave_node) {
834 genpd_sd_counter_dec(link->master);
835 pm_genpd_sync_poweroff(link->master);
840 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
841 * @genpd: PM domain to power on.
843 * This function is only called in "noirq" and "syscore" stages of system power
844 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
845 * executed sequentially, so it is guaranteed that it will never run twice in
846 * parallel).
848 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
850 struct gpd_link *link;
852 if (genpd->status != GPD_STATE_POWER_OFF)
853 return;
855 list_for_each_entry(link, &genpd->slave_links, slave_node) {
856 pm_genpd_sync_poweron(link->master);
857 genpd_sd_counter_inc(link->master);
860 if (genpd->power_on)
861 genpd->power_on(genpd);
863 genpd->status = GPD_STATE_ACTIVE;
867 * resume_needed - Check whether to resume a device before system suspend.
868 * @dev: Device to check.
869 * @genpd: PM domain the device belongs to.
871 * There are two cases in which a device that can wake up the system from sleep
872 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
873 * to wake up the system and it has to remain active for this purpose while the
874 * system is in the sleep state and (2) if the device is not enabled to wake up
875 * the system from sleep states and it generally doesn't generate wakeup signals
876 * by itself (those signals are generated on its behalf by other parts of the
877 * system). In the latter case it may be necessary to reconfigure the device's
878 * wakeup settings during system suspend, because it may have been set up to
879 * signal remote wakeup from the system's working state as needed by runtime PM.
880 * Return 'true' in either of the above cases.
882 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
884 bool active_wakeup;
886 if (!device_can_wakeup(dev))
887 return false;
889 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
890 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
894 * pm_genpd_prepare - Start power transition of a device in a PM domain.
895 * @dev: Device to start the transition of.
897 * Start a power transition of a device (during a system-wide power transition)
898 * under the assumption that its pm_domain field points to the domain member of
899 * an object of type struct generic_pm_domain representing a PM domain
900 * consisting of I/O devices.
902 static int pm_genpd_prepare(struct device *dev)
904 struct generic_pm_domain *genpd;
905 int ret;
907 dev_dbg(dev, "%s()\n", __func__);
909 genpd = dev_to_genpd(dev);
910 if (IS_ERR(genpd))
911 return -EINVAL;
914 * If a wakeup request is pending for the device, it should be woken up
915 * at this point and a system wakeup event should be reported if it's
916 * set up to wake up the system from sleep states.
918 pm_runtime_get_noresume(dev);
919 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
920 pm_wakeup_event(dev, 0);
922 if (pm_wakeup_pending()) {
923 pm_runtime_put(dev);
924 return -EBUSY;
927 if (resume_needed(dev, genpd))
928 pm_runtime_resume(dev);
930 genpd_acquire_lock(genpd);
932 if (genpd->prepared_count++ == 0) {
933 genpd->suspended_count = 0;
934 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
937 genpd_release_lock(genpd);
939 if (genpd->suspend_power_off) {
940 pm_runtime_put_noidle(dev);
941 return 0;
945 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
946 * so pm_genpd_poweron() will return immediately, but if the device
947 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
948 * to make it operational.
950 pm_runtime_resume(dev);
951 __pm_runtime_disable(dev, false);
953 ret = pm_generic_prepare(dev);
954 if (ret) {
955 mutex_lock(&genpd->lock);
957 if (--genpd->prepared_count == 0)
958 genpd->suspend_power_off = false;
960 mutex_unlock(&genpd->lock);
961 pm_runtime_enable(dev);
964 pm_runtime_put(dev);
965 return ret;
969 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
970 * @dev: Device to suspend.
972 * Suspend a device under the assumption that its pm_domain field points to the
973 * domain member of an object of type struct generic_pm_domain representing
974 * a PM domain consisting of I/O devices.
976 static int pm_genpd_suspend(struct device *dev)
978 struct generic_pm_domain *genpd;
980 dev_dbg(dev, "%s()\n", __func__);
982 genpd = dev_to_genpd(dev);
983 if (IS_ERR(genpd))
984 return -EINVAL;
986 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
990 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
991 * @dev: Device to suspend.
993 * Carry out a late suspend of a device under the assumption that its
994 * pm_domain field points to the domain member of an object of type
995 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
997 static int pm_genpd_suspend_late(struct device *dev)
999 struct generic_pm_domain *genpd;
1001 dev_dbg(dev, "%s()\n", __func__);
1003 genpd = dev_to_genpd(dev);
1004 if (IS_ERR(genpd))
1005 return -EINVAL;
1007 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1011 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1012 * @dev: Device to suspend.
1014 * Stop the device and remove power from the domain if all devices in it have
1015 * been stopped.
1017 static int pm_genpd_suspend_noirq(struct device *dev)
1019 struct generic_pm_domain *genpd;
1021 dev_dbg(dev, "%s()\n", __func__);
1023 genpd = dev_to_genpd(dev);
1024 if (IS_ERR(genpd))
1025 return -EINVAL;
1027 if (genpd->suspend_power_off
1028 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1029 return 0;
1031 genpd_stop_dev(genpd, dev);
1034 * Since all of the "noirq" callbacks are executed sequentially, it is
1035 * guaranteed that this function will never run twice in parallel for
1036 * the same PM domain, so it is not necessary to use locking here.
1038 genpd->suspended_count++;
1039 pm_genpd_sync_poweroff(genpd);
1041 return 0;
1045 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1046 * @dev: Device to resume.
1048 * Restore power to the device's PM domain, if necessary, and start the device.
1050 static int pm_genpd_resume_noirq(struct device *dev)
1052 struct generic_pm_domain *genpd;
1054 dev_dbg(dev, "%s()\n", __func__);
1056 genpd = dev_to_genpd(dev);
1057 if (IS_ERR(genpd))
1058 return -EINVAL;
1060 if (genpd->suspend_power_off
1061 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1062 return 0;
1065 * Since all of the "noirq" callbacks are executed sequentially, it is
1066 * guaranteed that this function will never run twice in parallel for
1067 * the same PM domain, so it is not necessary to use locking here.
1069 pm_genpd_sync_poweron(genpd);
1070 genpd->suspended_count--;
1072 return genpd_start_dev(genpd, dev);
1076 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1077 * @dev: Device to resume.
1079 * Carry out an early resume of a device under the assumption that its
1080 * pm_domain field points to the domain member of an object of type
1081 * struct generic_pm_domain representing a power domain consisting of I/O
1082 * devices.
1084 static int pm_genpd_resume_early(struct device *dev)
1086 struct generic_pm_domain *genpd;
1088 dev_dbg(dev, "%s()\n", __func__);
1090 genpd = dev_to_genpd(dev);
1091 if (IS_ERR(genpd))
1092 return -EINVAL;
1094 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1098 * pm_genpd_resume - Resume of device in an I/O PM domain.
1099 * @dev: Device to resume.
1101 * Resume a device under the assumption that its pm_domain field points to the
1102 * domain member of an object of type struct generic_pm_domain representing
1103 * a power domain consisting of I/O devices.
1105 static int pm_genpd_resume(struct device *dev)
1107 struct generic_pm_domain *genpd;
1109 dev_dbg(dev, "%s()\n", __func__);
1111 genpd = dev_to_genpd(dev);
1112 if (IS_ERR(genpd))
1113 return -EINVAL;
1115 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1119 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1120 * @dev: Device to freeze.
1122 * Freeze a device under the assumption that its pm_domain field points to the
1123 * domain member of an object of type struct generic_pm_domain representing
1124 * a power domain consisting of I/O devices.
1126 static int pm_genpd_freeze(struct device *dev)
1128 struct generic_pm_domain *genpd;
1130 dev_dbg(dev, "%s()\n", __func__);
1132 genpd = dev_to_genpd(dev);
1133 if (IS_ERR(genpd))
1134 return -EINVAL;
1136 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1140 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1141 * @dev: Device to freeze.
1143 * Carry out a late freeze of a device under the assumption that its
1144 * pm_domain field points to the domain member of an object of type
1145 * struct generic_pm_domain representing a power domain consisting of I/O
1146 * devices.
1148 static int pm_genpd_freeze_late(struct device *dev)
1150 struct generic_pm_domain *genpd;
1152 dev_dbg(dev, "%s()\n", __func__);
1154 genpd = dev_to_genpd(dev);
1155 if (IS_ERR(genpd))
1156 return -EINVAL;
1158 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1162 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1163 * @dev: Device to freeze.
1165 * Carry out a late freeze of a device under the assumption that its
1166 * pm_domain field points to the domain member of an object of type
1167 * struct generic_pm_domain representing a power domain consisting of I/O
1168 * devices.
1170 static int pm_genpd_freeze_noirq(struct device *dev)
1172 struct generic_pm_domain *genpd;
1174 dev_dbg(dev, "%s()\n", __func__);
1176 genpd = dev_to_genpd(dev);
1177 if (IS_ERR(genpd))
1178 return -EINVAL;
1180 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1184 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1185 * @dev: Device to thaw.
1187 * Start the device, unless power has been removed from the domain already
1188 * before the system transition.
1190 static int pm_genpd_thaw_noirq(struct device *dev)
1192 struct generic_pm_domain *genpd;
1194 dev_dbg(dev, "%s()\n", __func__);
1196 genpd = dev_to_genpd(dev);
1197 if (IS_ERR(genpd))
1198 return -EINVAL;
1200 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1204 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1205 * @dev: Device to thaw.
1207 * Carry out an early thaw of a device under the assumption that its
1208 * pm_domain field points to the domain member of an object of type
1209 * struct generic_pm_domain representing a power domain consisting of I/O
1210 * devices.
1212 static int pm_genpd_thaw_early(struct device *dev)
1214 struct generic_pm_domain *genpd;
1216 dev_dbg(dev, "%s()\n", __func__);
1218 genpd = dev_to_genpd(dev);
1219 if (IS_ERR(genpd))
1220 return -EINVAL;
1222 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1226 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1227 * @dev: Device to thaw.
1229 * Thaw a device under the assumption that its pm_domain field points to the
1230 * domain member of an object of type struct generic_pm_domain representing
1231 * a power domain consisting of I/O devices.
1233 static int pm_genpd_thaw(struct device *dev)
1235 struct generic_pm_domain *genpd;
1237 dev_dbg(dev, "%s()\n", __func__);
1239 genpd = dev_to_genpd(dev);
1240 if (IS_ERR(genpd))
1241 return -EINVAL;
1243 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1247 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1248 * @dev: Device to resume.
1250 * Make sure the domain will be in the same power state as before the
1251 * hibernation the system is resuming from and start the device if necessary.
1253 static int pm_genpd_restore_noirq(struct device *dev)
1255 struct generic_pm_domain *genpd;
1257 dev_dbg(dev, "%s()\n", __func__);
1259 genpd = dev_to_genpd(dev);
1260 if (IS_ERR(genpd))
1261 return -EINVAL;
1264 * Since all of the "noirq" callbacks are executed sequentially, it is
1265 * guaranteed that this function will never run twice in parallel for
1266 * the same PM domain, so it is not necessary to use locking here.
1268 * At this point suspended_count == 0 means we are being run for the
1269 * first time for the given domain in the present cycle.
1271 if (genpd->suspended_count++ == 0) {
1273 * The boot kernel might put the domain into arbitrary state,
1274 * so make it appear as powered off to pm_genpd_sync_poweron(),
1275 * so that it tries to power it on in case it was really off.
1277 genpd->status = GPD_STATE_POWER_OFF;
1278 if (genpd->suspend_power_off) {
1280 * If the domain was off before the hibernation, make
1281 * sure it will be off going forward.
1283 if (genpd->power_off)
1284 genpd->power_off(genpd);
1286 return 0;
1290 if (genpd->suspend_power_off)
1291 return 0;
1293 pm_genpd_sync_poweron(genpd);
1295 return genpd_start_dev(genpd, dev);
1299 * pm_genpd_complete - Complete power transition of a device in a power domain.
1300 * @dev: Device to complete the transition of.
1302 * Complete a power transition of a device (during a system-wide power
1303 * transition) under the assumption that its pm_domain field points to the
1304 * domain member of an object of type struct generic_pm_domain representing
1305 * a power domain consisting of I/O devices.
1307 static void pm_genpd_complete(struct device *dev)
1309 struct generic_pm_domain *genpd;
1310 bool run_complete;
1312 dev_dbg(dev, "%s()\n", __func__);
1314 genpd = dev_to_genpd(dev);
1315 if (IS_ERR(genpd))
1316 return;
1318 mutex_lock(&genpd->lock);
1320 run_complete = !genpd->suspend_power_off;
1321 if (--genpd->prepared_count == 0)
1322 genpd->suspend_power_off = false;
1324 mutex_unlock(&genpd->lock);
1326 if (run_complete) {
1327 pm_generic_complete(dev);
1328 pm_runtime_set_active(dev);
1329 pm_runtime_enable(dev);
1330 pm_request_idle(dev);
1335 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1336 * @dev: Device that normally is marked as "always on" to switch power for.
1338 * This routine may only be called during the system core (syscore) suspend or
1339 * resume phase for devices whose "always on" flags are set.
1341 void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1343 struct generic_pm_domain *genpd;
1345 genpd = dev_to_genpd(dev);
1346 if (!pm_genpd_present(genpd))
1347 return;
1349 if (suspend) {
1350 genpd->suspended_count++;
1351 pm_genpd_sync_poweroff(genpd);
1352 } else {
1353 pm_genpd_sync_poweron(genpd);
1354 genpd->suspended_count--;
1357 EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1359 #else
1361 #define pm_genpd_prepare NULL
1362 #define pm_genpd_suspend NULL
1363 #define pm_genpd_suspend_late NULL
1364 #define pm_genpd_suspend_noirq NULL
1365 #define pm_genpd_resume_early NULL
1366 #define pm_genpd_resume_noirq NULL
1367 #define pm_genpd_resume NULL
1368 #define pm_genpd_freeze NULL
1369 #define pm_genpd_freeze_late NULL
1370 #define pm_genpd_freeze_noirq NULL
1371 #define pm_genpd_thaw_early NULL
1372 #define pm_genpd_thaw_noirq NULL
1373 #define pm_genpd_thaw NULL
1374 #define pm_genpd_restore_noirq NULL
1375 #define pm_genpd_complete NULL
1377 #endif /* CONFIG_PM_SLEEP */
1379 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1381 struct generic_pm_domain_data *gpd_data;
1383 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1384 if (!gpd_data)
1385 return NULL;
1387 mutex_init(&gpd_data->lock);
1388 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1389 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1390 return gpd_data;
1393 static void __pm_genpd_free_dev_data(struct device *dev,
1394 struct generic_pm_domain_data *gpd_data)
1396 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1397 kfree(gpd_data);
1401 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1402 * @genpd: PM domain to add the device to.
1403 * @dev: Device to be added.
1404 * @td: Set of PM QoS timing parameters to attach to the device.
1406 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1407 struct gpd_timing_data *td)
1409 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1410 struct pm_domain_data *pdd;
1411 int ret = 0;
1413 dev_dbg(dev, "%s()\n", __func__);
1415 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1416 return -EINVAL;
1418 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1419 if (!gpd_data_new)
1420 return -ENOMEM;
1422 genpd_acquire_lock(genpd);
1424 if (genpd->prepared_count > 0) {
1425 ret = -EAGAIN;
1426 goto out;
1429 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1430 if (pdd->dev == dev) {
1431 ret = -EINVAL;
1432 goto out;
1435 ret = dev_pm_get_subsys_data(dev);
1436 if (ret)
1437 goto out;
1439 genpd->device_count++;
1440 genpd->max_off_time_changed = true;
1442 spin_lock_irq(&dev->power.lock);
1444 dev->pm_domain = &genpd->domain;
1445 if (dev->power.subsys_data->domain_data) {
1446 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1447 } else {
1448 gpd_data = gpd_data_new;
1449 dev->power.subsys_data->domain_data = &gpd_data->base;
1451 gpd_data->refcount++;
1452 if (td)
1453 gpd_data->td = *td;
1455 spin_unlock_irq(&dev->power.lock);
1457 mutex_lock(&gpd_data->lock);
1458 gpd_data->base.dev = dev;
1459 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1460 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1461 gpd_data->td.constraint_changed = true;
1462 gpd_data->td.effective_constraint_ns = -1;
1463 mutex_unlock(&gpd_data->lock);
1465 out:
1466 genpd_release_lock(genpd);
1468 if (gpd_data != gpd_data_new)
1469 __pm_genpd_free_dev_data(dev, gpd_data_new);
1471 return ret;
1475 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1476 * @genpd_node: Device tree node pointer representing a PM domain to which the
1477 * the device is added to.
1478 * @dev: Device to be added.
1479 * @td: Set of PM QoS timing parameters to attach to the device.
1481 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1482 struct gpd_timing_data *td)
1484 struct generic_pm_domain *genpd = NULL, *gpd;
1486 dev_dbg(dev, "%s()\n", __func__);
1488 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1489 return -EINVAL;
1491 mutex_lock(&gpd_list_lock);
1492 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1493 if (gpd->of_node == genpd_node) {
1494 genpd = gpd;
1495 break;
1498 mutex_unlock(&gpd_list_lock);
1500 if (!genpd)
1501 return -EINVAL;
1503 return __pm_genpd_add_device(genpd, dev, td);
1508 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1509 * @domain_name: Name of the PM domain to add the device to.
1510 * @dev: Device to be added.
1511 * @td: Set of PM QoS timing parameters to attach to the device.
1513 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1514 struct gpd_timing_data *td)
1516 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1520 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1521 * @genpd: PM domain to remove the device from.
1522 * @dev: Device to be removed.
1524 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1525 struct device *dev)
1527 struct generic_pm_domain_data *gpd_data;
1528 struct pm_domain_data *pdd;
1529 bool remove = false;
1530 int ret = 0;
1532 dev_dbg(dev, "%s()\n", __func__);
1534 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1535 || IS_ERR_OR_NULL(dev->pm_domain)
1536 || pd_to_genpd(dev->pm_domain) != genpd)
1537 return -EINVAL;
1539 genpd_acquire_lock(genpd);
1541 if (genpd->prepared_count > 0) {
1542 ret = -EAGAIN;
1543 goto out;
1546 genpd->device_count--;
1547 genpd->max_off_time_changed = true;
1549 spin_lock_irq(&dev->power.lock);
1551 dev->pm_domain = NULL;
1552 pdd = dev->power.subsys_data->domain_data;
1553 list_del_init(&pdd->list_node);
1554 gpd_data = to_gpd_data(pdd);
1555 if (--gpd_data->refcount == 0) {
1556 dev->power.subsys_data->domain_data = NULL;
1557 remove = true;
1560 spin_unlock_irq(&dev->power.lock);
1562 mutex_lock(&gpd_data->lock);
1563 pdd->dev = NULL;
1564 mutex_unlock(&gpd_data->lock);
1566 genpd_release_lock(genpd);
1568 dev_pm_put_subsys_data(dev);
1569 if (remove)
1570 __pm_genpd_free_dev_data(dev, gpd_data);
1572 return 0;
1574 out:
1575 genpd_release_lock(genpd);
1577 return ret;
1581 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1582 * @dev: Device to set/unset the flag for.
1583 * @val: The new value of the device's "need restore" flag.
1585 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1587 struct pm_subsys_data *psd;
1588 unsigned long flags;
1590 spin_lock_irqsave(&dev->power.lock, flags);
1592 psd = dev_to_psd(dev);
1593 if (psd && psd->domain_data)
1594 to_gpd_data(psd->domain_data)->need_restore = val;
1596 spin_unlock_irqrestore(&dev->power.lock, flags);
1598 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1601 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1602 * @genpd: Master PM domain to add the subdomain to.
1603 * @subdomain: Subdomain to be added.
1605 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1606 struct generic_pm_domain *subdomain)
1608 struct gpd_link *link;
1609 int ret = 0;
1611 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1612 || genpd == subdomain)
1613 return -EINVAL;
1615 start:
1616 genpd_acquire_lock(genpd);
1617 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1619 if (subdomain->status != GPD_STATE_POWER_OFF
1620 && subdomain->status != GPD_STATE_ACTIVE) {
1621 mutex_unlock(&subdomain->lock);
1622 genpd_release_lock(genpd);
1623 goto start;
1626 if (genpd->status == GPD_STATE_POWER_OFF
1627 && subdomain->status != GPD_STATE_POWER_OFF) {
1628 ret = -EINVAL;
1629 goto out;
1632 list_for_each_entry(link, &genpd->master_links, master_node) {
1633 if (link->slave == subdomain && link->master == genpd) {
1634 ret = -EINVAL;
1635 goto out;
1639 link = kzalloc(sizeof(*link), GFP_KERNEL);
1640 if (!link) {
1641 ret = -ENOMEM;
1642 goto out;
1644 link->master = genpd;
1645 list_add_tail(&link->master_node, &genpd->master_links);
1646 link->slave = subdomain;
1647 list_add_tail(&link->slave_node, &subdomain->slave_links);
1648 if (subdomain->status != GPD_STATE_POWER_OFF)
1649 genpd_sd_counter_inc(genpd);
1651 out:
1652 mutex_unlock(&subdomain->lock);
1653 genpd_release_lock(genpd);
1655 return ret;
1659 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1660 * @master_name: Name of the master PM domain to add the subdomain to.
1661 * @subdomain_name: Name of the subdomain to be added.
1663 int pm_genpd_add_subdomain_names(const char *master_name,
1664 const char *subdomain_name)
1666 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1668 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1669 return -EINVAL;
1671 mutex_lock(&gpd_list_lock);
1672 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1673 if (!master && !strcmp(gpd->name, master_name))
1674 master = gpd;
1676 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1677 subdomain = gpd;
1679 if (master && subdomain)
1680 break;
1682 mutex_unlock(&gpd_list_lock);
1684 return pm_genpd_add_subdomain(master, subdomain);
1688 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1689 * @genpd: Master PM domain to remove the subdomain from.
1690 * @subdomain: Subdomain to be removed.
1692 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1693 struct generic_pm_domain *subdomain)
1695 struct gpd_link *link;
1696 int ret = -EINVAL;
1698 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1699 return -EINVAL;
1701 start:
1702 genpd_acquire_lock(genpd);
1704 list_for_each_entry(link, &genpd->master_links, master_node) {
1705 if (link->slave != subdomain)
1706 continue;
1708 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1710 if (subdomain->status != GPD_STATE_POWER_OFF
1711 && subdomain->status != GPD_STATE_ACTIVE) {
1712 mutex_unlock(&subdomain->lock);
1713 genpd_release_lock(genpd);
1714 goto start;
1717 list_del(&link->master_node);
1718 list_del(&link->slave_node);
1719 kfree(link);
1720 if (subdomain->status != GPD_STATE_POWER_OFF)
1721 genpd_sd_counter_dec(genpd);
1723 mutex_unlock(&subdomain->lock);
1725 ret = 0;
1726 break;
1729 genpd_release_lock(genpd);
1731 return ret;
1735 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1736 * @dev: Device to add the callbacks to.
1737 * @ops: Set of callbacks to add.
1738 * @td: Timing data to add to the device along with the callbacks (optional).
1740 * Every call to this routine should be balanced with a call to
1741 * __pm_genpd_remove_callbacks() and they must not be nested.
1743 int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1744 struct gpd_timing_data *td)
1746 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1747 int ret = 0;
1749 if (!(dev && ops))
1750 return -EINVAL;
1752 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1753 if (!gpd_data_new)
1754 return -ENOMEM;
1756 pm_runtime_disable(dev);
1757 device_pm_lock();
1759 ret = dev_pm_get_subsys_data(dev);
1760 if (ret)
1761 goto out;
1763 spin_lock_irq(&dev->power.lock);
1765 if (dev->power.subsys_data->domain_data) {
1766 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1767 } else {
1768 gpd_data = gpd_data_new;
1769 dev->power.subsys_data->domain_data = &gpd_data->base;
1771 gpd_data->refcount++;
1772 gpd_data->ops = *ops;
1773 if (td)
1774 gpd_data->td = *td;
1776 spin_unlock_irq(&dev->power.lock);
1778 out:
1779 device_pm_unlock();
1780 pm_runtime_enable(dev);
1782 if (gpd_data != gpd_data_new)
1783 __pm_genpd_free_dev_data(dev, gpd_data_new);
1785 return ret;
1787 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1790 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1791 * @dev: Device to remove the callbacks from.
1792 * @clear_td: If set, clear the device's timing data too.
1794 * This routine can only be called after pm_genpd_add_callbacks().
1796 int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1798 struct generic_pm_domain_data *gpd_data = NULL;
1799 bool remove = false;
1800 int ret = 0;
1802 if (!(dev && dev->power.subsys_data))
1803 return -EINVAL;
1805 pm_runtime_disable(dev);
1806 device_pm_lock();
1808 spin_lock_irq(&dev->power.lock);
1810 if (dev->power.subsys_data->domain_data) {
1811 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1812 gpd_data->ops = (struct gpd_dev_ops){ NULL };
1813 if (clear_td)
1814 gpd_data->td = (struct gpd_timing_data){ 0 };
1816 if (--gpd_data->refcount == 0) {
1817 dev->power.subsys_data->domain_data = NULL;
1818 remove = true;
1820 } else {
1821 ret = -EINVAL;
1824 spin_unlock_irq(&dev->power.lock);
1826 device_pm_unlock();
1827 pm_runtime_enable(dev);
1829 if (ret)
1830 return ret;
1832 dev_pm_put_subsys_data(dev);
1833 if (remove)
1834 __pm_genpd_free_dev_data(dev, gpd_data);
1836 return 0;
1838 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1841 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1842 * @genpd: PM domain to be connected with cpuidle.
1843 * @state: cpuidle state this domain can disable/enable.
1845 * Make a PM domain behave as though it contained a CPU core, that is, instead
1846 * of calling its power down routine it will enable the given cpuidle state so
1847 * that the cpuidle subsystem can power it down (if possible and desirable).
1849 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1851 struct cpuidle_driver *cpuidle_drv;
1852 struct gpd_cpu_data *cpu_data;
1853 struct cpuidle_state *idle_state;
1854 int ret = 0;
1856 if (IS_ERR_OR_NULL(genpd) || state < 0)
1857 return -EINVAL;
1859 genpd_acquire_lock(genpd);
1861 if (genpd->cpu_data) {
1862 ret = -EEXIST;
1863 goto out;
1865 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1866 if (!cpu_data) {
1867 ret = -ENOMEM;
1868 goto out;
1870 cpuidle_drv = cpuidle_driver_ref();
1871 if (!cpuidle_drv) {
1872 ret = -ENODEV;
1873 goto err_drv;
1875 if (cpuidle_drv->state_count <= state) {
1876 ret = -EINVAL;
1877 goto err;
1879 idle_state = &cpuidle_drv->states[state];
1880 if (!idle_state->disabled) {
1881 ret = -EAGAIN;
1882 goto err;
1884 cpu_data->idle_state = idle_state;
1885 cpu_data->saved_exit_latency = idle_state->exit_latency;
1886 genpd->cpu_data = cpu_data;
1887 genpd_recalc_cpu_exit_latency(genpd);
1889 out:
1890 genpd_release_lock(genpd);
1891 return ret;
1893 err:
1894 cpuidle_driver_unref();
1896 err_drv:
1897 kfree(cpu_data);
1898 goto out;
1902 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1903 * @name: Name of the domain to connect to cpuidle.
1904 * @state: cpuidle state this domain can manipulate.
1906 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1908 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1912 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1913 * @genpd: PM domain to remove the cpuidle connection from.
1915 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1916 * given PM domain.
1918 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1920 struct gpd_cpu_data *cpu_data;
1921 struct cpuidle_state *idle_state;
1922 int ret = 0;
1924 if (IS_ERR_OR_NULL(genpd))
1925 return -EINVAL;
1927 genpd_acquire_lock(genpd);
1929 cpu_data = genpd->cpu_data;
1930 if (!cpu_data) {
1931 ret = -ENODEV;
1932 goto out;
1934 idle_state = cpu_data->idle_state;
1935 if (!idle_state->disabled) {
1936 ret = -EAGAIN;
1937 goto out;
1939 idle_state->exit_latency = cpu_data->saved_exit_latency;
1940 cpuidle_driver_unref();
1941 genpd->cpu_data = NULL;
1942 kfree(cpu_data);
1944 out:
1945 genpd_release_lock(genpd);
1946 return ret;
1950 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1951 * @name: Name of the domain to disconnect cpuidle from.
1953 int pm_genpd_name_detach_cpuidle(const char *name)
1955 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1958 /* Default device callbacks for generic PM domains. */
1961 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1962 * @dev: Device to handle.
1964 static int pm_genpd_default_save_state(struct device *dev)
1966 int (*cb)(struct device *__dev);
1968 cb = dev_gpd_data(dev)->ops.save_state;
1969 if (cb)
1970 return cb(dev);
1972 if (dev->type && dev->type->pm)
1973 cb = dev->type->pm->runtime_suspend;
1974 else if (dev->class && dev->class->pm)
1975 cb = dev->class->pm->runtime_suspend;
1976 else if (dev->bus && dev->bus->pm)
1977 cb = dev->bus->pm->runtime_suspend;
1978 else
1979 cb = NULL;
1981 if (!cb && dev->driver && dev->driver->pm)
1982 cb = dev->driver->pm->runtime_suspend;
1984 return cb ? cb(dev) : 0;
1988 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1989 * @dev: Device to handle.
1991 static int pm_genpd_default_restore_state(struct device *dev)
1993 int (*cb)(struct device *__dev);
1995 cb = dev_gpd_data(dev)->ops.restore_state;
1996 if (cb)
1997 return cb(dev);
1999 if (dev->type && dev->type->pm)
2000 cb = dev->type->pm->runtime_resume;
2001 else if (dev->class && dev->class->pm)
2002 cb = dev->class->pm->runtime_resume;
2003 else if (dev->bus && dev->bus->pm)
2004 cb = dev->bus->pm->runtime_resume;
2005 else
2006 cb = NULL;
2008 if (!cb && dev->driver && dev->driver->pm)
2009 cb = dev->driver->pm->runtime_resume;
2011 return cb ? cb(dev) : 0;
2014 #ifdef CONFIG_PM_SLEEP
2017 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2018 * @dev: Device to handle.
2020 static int pm_genpd_default_suspend(struct device *dev)
2022 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
2024 return cb ? cb(dev) : pm_generic_suspend(dev);
2028 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2029 * @dev: Device to handle.
2031 static int pm_genpd_default_suspend_late(struct device *dev)
2033 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
2035 return cb ? cb(dev) : pm_generic_suspend_late(dev);
2039 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2040 * @dev: Device to handle.
2042 static int pm_genpd_default_resume_early(struct device *dev)
2044 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
2046 return cb ? cb(dev) : pm_generic_resume_early(dev);
2050 * pm_genpd_default_resume - Default "device resume" for PM domians.
2051 * @dev: Device to handle.
2053 static int pm_genpd_default_resume(struct device *dev)
2055 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
2057 return cb ? cb(dev) : pm_generic_resume(dev);
2061 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2062 * @dev: Device to handle.
2064 static int pm_genpd_default_freeze(struct device *dev)
2066 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
2068 return cb ? cb(dev) : pm_generic_freeze(dev);
2072 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2073 * @dev: Device to handle.
2075 static int pm_genpd_default_freeze_late(struct device *dev)
2077 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
2079 return cb ? cb(dev) : pm_generic_freeze_late(dev);
2083 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2084 * @dev: Device to handle.
2086 static int pm_genpd_default_thaw_early(struct device *dev)
2088 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
2090 return cb ? cb(dev) : pm_generic_thaw_early(dev);
2094 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2095 * @dev: Device to handle.
2097 static int pm_genpd_default_thaw(struct device *dev)
2099 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
2101 return cb ? cb(dev) : pm_generic_thaw(dev);
2104 #else /* !CONFIG_PM_SLEEP */
2106 #define pm_genpd_default_suspend NULL
2107 #define pm_genpd_default_suspend_late NULL
2108 #define pm_genpd_default_resume_early NULL
2109 #define pm_genpd_default_resume NULL
2110 #define pm_genpd_default_freeze NULL
2111 #define pm_genpd_default_freeze_late NULL
2112 #define pm_genpd_default_thaw_early NULL
2113 #define pm_genpd_default_thaw NULL
2115 #endif /* !CONFIG_PM_SLEEP */
2118 * pm_genpd_init - Initialize a generic I/O PM domain object.
2119 * @genpd: PM domain object to initialize.
2120 * @gov: PM domain governor to associate with the domain (may be NULL).
2121 * @is_off: Initial value of the domain's power_is_off field.
2123 void pm_genpd_init(struct generic_pm_domain *genpd,
2124 struct dev_power_governor *gov, bool is_off)
2126 if (IS_ERR_OR_NULL(genpd))
2127 return;
2129 INIT_LIST_HEAD(&genpd->master_links);
2130 INIT_LIST_HEAD(&genpd->slave_links);
2131 INIT_LIST_HEAD(&genpd->dev_list);
2132 mutex_init(&genpd->lock);
2133 genpd->gov = gov;
2134 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2135 genpd->in_progress = 0;
2136 atomic_set(&genpd->sd_count, 0);
2137 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2138 init_waitqueue_head(&genpd->status_wait_queue);
2139 genpd->poweroff_task = NULL;
2140 genpd->resume_count = 0;
2141 genpd->device_count = 0;
2142 genpd->max_off_time_ns = -1;
2143 genpd->max_off_time_changed = true;
2144 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2145 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2146 genpd->domain.ops.prepare = pm_genpd_prepare;
2147 genpd->domain.ops.suspend = pm_genpd_suspend;
2148 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
2149 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2150 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
2151 genpd->domain.ops.resume_early = pm_genpd_resume_early;
2152 genpd->domain.ops.resume = pm_genpd_resume;
2153 genpd->domain.ops.freeze = pm_genpd_freeze;
2154 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
2155 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2156 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
2157 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
2158 genpd->domain.ops.thaw = pm_genpd_thaw;
2159 genpd->domain.ops.poweroff = pm_genpd_suspend;
2160 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
2161 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
2162 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
2163 genpd->domain.ops.restore_early = pm_genpd_resume_early;
2164 genpd->domain.ops.restore = pm_genpd_resume;
2165 genpd->domain.ops.complete = pm_genpd_complete;
2166 genpd->dev_ops.save_state = pm_genpd_default_save_state;
2167 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
2168 genpd->dev_ops.suspend = pm_genpd_default_suspend;
2169 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2170 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2171 genpd->dev_ops.resume = pm_genpd_default_resume;
2172 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2173 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2174 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2175 genpd->dev_ops.thaw = pm_genpd_default_thaw;
2176 mutex_lock(&gpd_list_lock);
2177 list_add(&genpd->gpd_list_node, &gpd_list);
2178 mutex_unlock(&gpd_list_lock);