Merge branch 'for-next-3.17' of git://git.samba.org/sfrench/cifs-2.6
[linux/fpc-iii.git] / drivers / base / power / domain.c
blobeee55c1e5fde49310779c9fc4e15aa8ce5415114
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/pm_runtime.h>
12 #include <linux/pm_domain.h>
13 #include <linux/pm_qos.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
18 #include <linux/export.h>
20 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
21 ({ \
22 type (*__routine)(struct device *__d); \
23 type __ret = (type)0; \
25 __routine = genpd->dev_ops.callback; \
26 if (__routine) { \
27 __ret = __routine(dev); \
28 } else { \
29 __routine = dev_gpd_data(dev)->ops.callback; \
30 if (__routine) \
31 __ret = __routine(dev); \
32 } \
33 __ret; \
36 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
37 ({ \
38 ktime_t __start = ktime_get(); \
39 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
40 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
41 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
42 if (!__retval && __elapsed > __td->field) { \
43 __td->field = __elapsed; \
44 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
45 __elapsed); \
46 genpd->max_off_time_changed = true; \
47 __td->constraint_changed = true; \
48 } \
49 __retval; \
52 static LIST_HEAD(gpd_list);
53 static DEFINE_MUTEX(gpd_list_lock);
55 static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
57 struct generic_pm_domain *genpd = NULL, *gpd;
59 if (IS_ERR_OR_NULL(domain_name))
60 return NULL;
62 mutex_lock(&gpd_list_lock);
63 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
64 if (!strcmp(gpd->name, domain_name)) {
65 genpd = gpd;
66 break;
69 mutex_unlock(&gpd_list_lock);
70 return genpd;
73 #ifdef CONFIG_PM
75 struct generic_pm_domain *dev_to_genpd(struct device *dev)
77 if (IS_ERR_OR_NULL(dev->pm_domain))
78 return ERR_PTR(-EINVAL);
80 return pd_to_genpd(dev->pm_domain);
83 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
85 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
86 stop_latency_ns, "stop");
89 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
91 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
92 start_latency_ns, "start");
95 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
97 bool ret = false;
99 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
100 ret = !!atomic_dec_and_test(&genpd->sd_count);
102 return ret;
105 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
107 atomic_inc(&genpd->sd_count);
108 smp_mb__after_atomic();
111 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
113 DEFINE_WAIT(wait);
115 mutex_lock(&genpd->lock);
117 * Wait for the domain to transition into either the active,
118 * or the power off state.
120 for (;;) {
121 prepare_to_wait(&genpd->status_wait_queue, &wait,
122 TASK_UNINTERRUPTIBLE);
123 if (genpd->status == GPD_STATE_ACTIVE
124 || genpd->status == GPD_STATE_POWER_OFF)
125 break;
126 mutex_unlock(&genpd->lock);
128 schedule();
130 mutex_lock(&genpd->lock);
132 finish_wait(&genpd->status_wait_queue, &wait);
135 static void genpd_release_lock(struct generic_pm_domain *genpd)
137 mutex_unlock(&genpd->lock);
140 static void genpd_set_active(struct generic_pm_domain *genpd)
142 if (genpd->resume_count == 0)
143 genpd->status = GPD_STATE_ACTIVE;
146 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
148 s64 usecs64;
150 if (!genpd->cpu_data)
151 return;
153 usecs64 = genpd->power_on_latency_ns;
154 do_div(usecs64, NSEC_PER_USEC);
155 usecs64 += genpd->cpu_data->saved_exit_latency;
156 genpd->cpu_data->idle_state->exit_latency = usecs64;
160 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
161 * @genpd: PM domain to power up.
163 * Restore power to @genpd and all of its masters so that it is possible to
164 * resume a device belonging to it.
166 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
167 __releases(&genpd->lock) __acquires(&genpd->lock)
169 struct gpd_link *link;
170 DEFINE_WAIT(wait);
171 int ret = 0;
173 /* If the domain's master is being waited for, we have to wait too. */
174 for (;;) {
175 prepare_to_wait(&genpd->status_wait_queue, &wait,
176 TASK_UNINTERRUPTIBLE);
177 if (genpd->status != GPD_STATE_WAIT_MASTER)
178 break;
179 mutex_unlock(&genpd->lock);
181 schedule();
183 mutex_lock(&genpd->lock);
185 finish_wait(&genpd->status_wait_queue, &wait);
187 if (genpd->status == GPD_STATE_ACTIVE
188 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
189 return 0;
191 if (genpd->status != GPD_STATE_POWER_OFF) {
192 genpd_set_active(genpd);
193 return 0;
196 if (genpd->cpu_data) {
197 cpuidle_pause_and_lock();
198 genpd->cpu_data->idle_state->disabled = true;
199 cpuidle_resume_and_unlock();
200 goto out;
204 * The list is guaranteed not to change while the loop below is being
205 * executed, unless one of the masters' .power_on() callbacks fiddles
206 * with it.
208 list_for_each_entry(link, &genpd->slave_links, slave_node) {
209 genpd_sd_counter_inc(link->master);
210 genpd->status = GPD_STATE_WAIT_MASTER;
212 mutex_unlock(&genpd->lock);
214 ret = pm_genpd_poweron(link->master);
216 mutex_lock(&genpd->lock);
219 * The "wait for parent" status is guaranteed not to change
220 * while the master is powering on.
222 genpd->status = GPD_STATE_POWER_OFF;
223 wake_up_all(&genpd->status_wait_queue);
224 if (ret) {
225 genpd_sd_counter_dec(link->master);
226 goto err;
230 if (genpd->power_on) {
231 ktime_t time_start = ktime_get();
232 s64 elapsed_ns;
234 ret = genpd->power_on(genpd);
235 if (ret)
236 goto err;
238 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
239 if (elapsed_ns > genpd->power_on_latency_ns) {
240 genpd->power_on_latency_ns = elapsed_ns;
241 genpd->max_off_time_changed = true;
242 genpd_recalc_cpu_exit_latency(genpd);
243 if (genpd->name)
244 pr_warning("%s: Power-on latency exceeded, "
245 "new value %lld ns\n", genpd->name,
246 elapsed_ns);
250 out:
251 genpd_set_active(genpd);
253 return 0;
255 err:
256 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
257 genpd_sd_counter_dec(link->master);
259 return ret;
263 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
264 * @genpd: PM domain to power up.
266 int pm_genpd_poweron(struct generic_pm_domain *genpd)
268 int ret;
270 mutex_lock(&genpd->lock);
271 ret = __pm_genpd_poweron(genpd);
272 mutex_unlock(&genpd->lock);
273 return ret;
277 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
278 * @domain_name: Name of the PM domain to power up.
280 int pm_genpd_name_poweron(const char *domain_name)
282 struct generic_pm_domain *genpd;
284 genpd = pm_genpd_lookup_name(domain_name);
285 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
288 #endif /* CONFIG_PM */
290 #ifdef CONFIG_PM_RUNTIME
292 static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
293 struct device *dev)
295 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
298 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
300 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
301 save_state_latency_ns, "state save");
304 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
306 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
307 restore_state_latency_ns,
308 "state restore");
311 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
312 unsigned long val, void *ptr)
314 struct generic_pm_domain_data *gpd_data;
315 struct device *dev;
317 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
319 mutex_lock(&gpd_data->lock);
320 dev = gpd_data->base.dev;
321 if (!dev) {
322 mutex_unlock(&gpd_data->lock);
323 return NOTIFY_DONE;
325 mutex_unlock(&gpd_data->lock);
327 for (;;) {
328 struct generic_pm_domain *genpd;
329 struct pm_domain_data *pdd;
331 spin_lock_irq(&dev->power.lock);
333 pdd = dev->power.subsys_data ?
334 dev->power.subsys_data->domain_data : NULL;
335 if (pdd && pdd->dev) {
336 to_gpd_data(pdd)->td.constraint_changed = true;
337 genpd = dev_to_genpd(dev);
338 } else {
339 genpd = ERR_PTR(-ENODATA);
342 spin_unlock_irq(&dev->power.lock);
344 if (!IS_ERR(genpd)) {
345 mutex_lock(&genpd->lock);
346 genpd->max_off_time_changed = true;
347 mutex_unlock(&genpd->lock);
350 dev = dev->parent;
351 if (!dev || dev->power.ignore_children)
352 break;
355 return NOTIFY_DONE;
359 * __pm_genpd_save_device - Save the pre-suspend state of a device.
360 * @pdd: Domain data of the device to save the state of.
361 * @genpd: PM domain the device belongs to.
363 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
364 struct generic_pm_domain *genpd)
365 __releases(&genpd->lock) __acquires(&genpd->lock)
367 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
368 struct device *dev = pdd->dev;
369 int ret = 0;
371 if (gpd_data->need_restore)
372 return 0;
374 mutex_unlock(&genpd->lock);
376 genpd_start_dev(genpd, dev);
377 ret = genpd_save_dev(genpd, dev);
378 genpd_stop_dev(genpd, dev);
380 mutex_lock(&genpd->lock);
382 if (!ret)
383 gpd_data->need_restore = true;
385 return ret;
389 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
390 * @pdd: Domain data of the device to restore the state of.
391 * @genpd: PM domain the device belongs to.
393 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
394 struct generic_pm_domain *genpd)
395 __releases(&genpd->lock) __acquires(&genpd->lock)
397 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
398 struct device *dev = pdd->dev;
399 bool need_restore = gpd_data->need_restore;
401 gpd_data->need_restore = false;
402 mutex_unlock(&genpd->lock);
404 genpd_start_dev(genpd, dev);
405 if (need_restore)
406 genpd_restore_dev(genpd, dev);
408 mutex_lock(&genpd->lock);
412 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
413 * @genpd: PM domain to check.
415 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
416 * a "power off" operation, which means that a "power on" has occured in the
417 * meantime, or if its resume_count field is different from zero, which means
418 * that one of its devices has been resumed in the meantime.
420 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
422 return genpd->status == GPD_STATE_WAIT_MASTER
423 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
427 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
428 * @genpd: PM domait to power off.
430 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
431 * before.
433 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
435 queue_work(pm_wq, &genpd->power_off_work);
439 * pm_genpd_poweroff - Remove power from a given PM domain.
440 * @genpd: PM domain to power down.
442 * If all of the @genpd's devices have been suspended and all of its subdomains
443 * have been powered down, run the runtime suspend callbacks provided by all of
444 * the @genpd's devices' drivers and remove power from @genpd.
446 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
447 __releases(&genpd->lock) __acquires(&genpd->lock)
449 struct pm_domain_data *pdd;
450 struct gpd_link *link;
451 unsigned int not_suspended;
452 int ret = 0;
454 start:
456 * Do not try to power off the domain in the following situations:
457 * (1) The domain is already in the "power off" state.
458 * (2) The domain is waiting for its master to power up.
459 * (3) One of the domain's devices is being resumed right now.
460 * (4) System suspend is in progress.
462 if (genpd->status == GPD_STATE_POWER_OFF
463 || genpd->status == GPD_STATE_WAIT_MASTER
464 || genpd->resume_count > 0 || genpd->prepared_count > 0)
465 return 0;
467 if (atomic_read(&genpd->sd_count) > 0)
468 return -EBUSY;
470 not_suspended = 0;
471 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
472 enum pm_qos_flags_status stat;
474 stat = dev_pm_qos_flags(pdd->dev,
475 PM_QOS_FLAG_NO_POWER_OFF
476 | PM_QOS_FLAG_REMOTE_WAKEUP);
477 if (stat > PM_QOS_FLAGS_NONE)
478 return -EBUSY;
480 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
481 || pdd->dev->power.irq_safe))
482 not_suspended++;
485 if (not_suspended > genpd->in_progress)
486 return -EBUSY;
488 if (genpd->poweroff_task) {
490 * Another instance of pm_genpd_poweroff() is executing
491 * callbacks, so tell it to start over and return.
493 genpd->status = GPD_STATE_REPEAT;
494 return 0;
497 if (genpd->gov && genpd->gov->power_down_ok) {
498 if (!genpd->gov->power_down_ok(&genpd->domain))
499 return -EAGAIN;
502 genpd->status = GPD_STATE_BUSY;
503 genpd->poweroff_task = current;
505 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
506 ret = atomic_read(&genpd->sd_count) == 0 ?
507 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
509 if (genpd_abort_poweroff(genpd))
510 goto out;
512 if (ret) {
513 genpd_set_active(genpd);
514 goto out;
517 if (genpd->status == GPD_STATE_REPEAT) {
518 genpd->poweroff_task = NULL;
519 goto start;
523 if (genpd->cpu_data) {
525 * If cpu_data is set, cpuidle should turn the domain off when
526 * the CPU in it is idle. In that case we don't decrement the
527 * subdomain counts of the master domains, so that power is not
528 * removed from the current domain prematurely as a result of
529 * cutting off the masters' power.
531 genpd->status = GPD_STATE_POWER_OFF;
532 cpuidle_pause_and_lock();
533 genpd->cpu_data->idle_state->disabled = false;
534 cpuidle_resume_and_unlock();
535 goto out;
538 if (genpd->power_off) {
539 ktime_t time_start;
540 s64 elapsed_ns;
542 if (atomic_read(&genpd->sd_count) > 0) {
543 ret = -EBUSY;
544 goto out;
547 time_start = ktime_get();
550 * If sd_count > 0 at this point, one of the subdomains hasn't
551 * managed to call pm_genpd_poweron() for the master yet after
552 * incrementing it. In that case pm_genpd_poweron() will wait
553 * for us to drop the lock, so we can call .power_off() and let
554 * the pm_genpd_poweron() restore power for us (this shouldn't
555 * happen very often).
557 ret = genpd->power_off(genpd);
558 if (ret == -EBUSY) {
559 genpd_set_active(genpd);
560 goto out;
563 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
564 if (elapsed_ns > genpd->power_off_latency_ns) {
565 genpd->power_off_latency_ns = elapsed_ns;
566 genpd->max_off_time_changed = true;
567 if (genpd->name)
568 pr_warning("%s: Power-off latency exceeded, "
569 "new value %lld ns\n", genpd->name,
570 elapsed_ns);
574 genpd->status = GPD_STATE_POWER_OFF;
576 list_for_each_entry(link, &genpd->slave_links, slave_node) {
577 genpd_sd_counter_dec(link->master);
578 genpd_queue_power_off_work(link->master);
581 out:
582 genpd->poweroff_task = NULL;
583 wake_up_all(&genpd->status_wait_queue);
584 return ret;
588 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
589 * @work: Work structure used for scheduling the execution of this function.
591 static void genpd_power_off_work_fn(struct work_struct *work)
593 struct generic_pm_domain *genpd;
595 genpd = container_of(work, struct generic_pm_domain, power_off_work);
597 genpd_acquire_lock(genpd);
598 pm_genpd_poweroff(genpd);
599 genpd_release_lock(genpd);
603 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
604 * @dev: Device to suspend.
606 * Carry out a runtime suspend of a device under the assumption that its
607 * pm_domain field points to the domain member of an object of type
608 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
610 static int pm_genpd_runtime_suspend(struct device *dev)
612 struct generic_pm_domain *genpd;
613 bool (*stop_ok)(struct device *__dev);
614 int ret;
616 dev_dbg(dev, "%s()\n", __func__);
618 genpd = dev_to_genpd(dev);
619 if (IS_ERR(genpd))
620 return -EINVAL;
622 might_sleep_if(!genpd->dev_irq_safe);
624 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
625 if (stop_ok && !stop_ok(dev))
626 return -EBUSY;
628 ret = genpd_stop_dev(genpd, dev);
629 if (ret)
630 return ret;
633 * If power.irq_safe is set, this routine will be run with interrupts
634 * off, so it can't use mutexes.
636 if (dev->power.irq_safe)
637 return 0;
639 mutex_lock(&genpd->lock);
640 genpd->in_progress++;
641 pm_genpd_poweroff(genpd);
642 genpd->in_progress--;
643 mutex_unlock(&genpd->lock);
645 return 0;
649 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
650 * @dev: Device to resume.
652 * Carry out a runtime resume of a device under the assumption that its
653 * pm_domain field points to the domain member of an object of type
654 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
656 static int pm_genpd_runtime_resume(struct device *dev)
658 struct generic_pm_domain *genpd;
659 DEFINE_WAIT(wait);
660 int ret;
662 dev_dbg(dev, "%s()\n", __func__);
664 genpd = dev_to_genpd(dev);
665 if (IS_ERR(genpd))
666 return -EINVAL;
668 might_sleep_if(!genpd->dev_irq_safe);
670 /* If power.irq_safe, the PM domain is never powered off. */
671 if (dev->power.irq_safe)
672 return genpd_start_dev_no_timing(genpd, dev);
674 mutex_lock(&genpd->lock);
675 ret = __pm_genpd_poweron(genpd);
676 if (ret) {
677 mutex_unlock(&genpd->lock);
678 return ret;
680 genpd->status = GPD_STATE_BUSY;
681 genpd->resume_count++;
682 for (;;) {
683 prepare_to_wait(&genpd->status_wait_queue, &wait,
684 TASK_UNINTERRUPTIBLE);
686 * If current is the powering off task, we have been called
687 * reentrantly from one of the device callbacks, so we should
688 * not wait.
690 if (!genpd->poweroff_task || genpd->poweroff_task == current)
691 break;
692 mutex_unlock(&genpd->lock);
694 schedule();
696 mutex_lock(&genpd->lock);
698 finish_wait(&genpd->status_wait_queue, &wait);
699 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
700 genpd->resume_count--;
701 genpd_set_active(genpd);
702 wake_up_all(&genpd->status_wait_queue);
703 mutex_unlock(&genpd->lock);
705 return 0;
708 static bool pd_ignore_unused;
709 static int __init pd_ignore_unused_setup(char *__unused)
711 pd_ignore_unused = true;
712 return 1;
714 __setup("pd_ignore_unused", pd_ignore_unused_setup);
717 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
719 void pm_genpd_poweroff_unused(void)
721 struct generic_pm_domain *genpd;
723 if (pd_ignore_unused) {
724 pr_warn("genpd: Not disabling unused power domains\n");
725 return;
728 mutex_lock(&gpd_list_lock);
730 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
731 genpd_queue_power_off_work(genpd);
733 mutex_unlock(&gpd_list_lock);
736 #else
738 static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
739 unsigned long val, void *ptr)
741 return NOTIFY_DONE;
744 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
746 #define pm_genpd_runtime_suspend NULL
747 #define pm_genpd_runtime_resume NULL
749 #endif /* CONFIG_PM_RUNTIME */
751 #ifdef CONFIG_PM_SLEEP
754 * pm_genpd_present - Check if the given PM domain has been initialized.
755 * @genpd: PM domain to check.
757 static bool pm_genpd_present(struct generic_pm_domain *genpd)
759 struct generic_pm_domain *gpd;
761 if (IS_ERR_OR_NULL(genpd))
762 return false;
764 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
765 if (gpd == genpd)
766 return true;
768 return false;
771 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
772 struct device *dev)
774 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
777 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
779 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
782 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
784 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
787 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
789 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
792 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
794 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
797 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
799 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
802 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
804 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
807 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
809 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
812 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
814 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
818 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
819 * @genpd: PM domain to power off, if possible.
821 * Check if the given PM domain can be powered off (during system suspend or
822 * hibernation) and do that if so. Also, in that case propagate to its masters.
824 * This function is only called in "noirq" and "syscore" stages of system power
825 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
826 * executed sequentially, so it is guaranteed that it will never run twice in
827 * parallel).
829 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
831 struct gpd_link *link;
833 if (genpd->status == GPD_STATE_POWER_OFF)
834 return;
836 if (genpd->suspended_count != genpd->device_count
837 || atomic_read(&genpd->sd_count) > 0)
838 return;
840 if (genpd->power_off)
841 genpd->power_off(genpd);
843 genpd->status = GPD_STATE_POWER_OFF;
845 list_for_each_entry(link, &genpd->slave_links, slave_node) {
846 genpd_sd_counter_dec(link->master);
847 pm_genpd_sync_poweroff(link->master);
852 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
853 * @genpd: PM domain to power on.
855 * This function is only called in "noirq" and "syscore" stages of system power
856 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
857 * executed sequentially, so it is guaranteed that it will never run twice in
858 * parallel).
860 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
862 struct gpd_link *link;
864 if (genpd->status != GPD_STATE_POWER_OFF)
865 return;
867 list_for_each_entry(link, &genpd->slave_links, slave_node) {
868 pm_genpd_sync_poweron(link->master);
869 genpd_sd_counter_inc(link->master);
872 if (genpd->power_on)
873 genpd->power_on(genpd);
875 genpd->status = GPD_STATE_ACTIVE;
879 * resume_needed - Check whether to resume a device before system suspend.
880 * @dev: Device to check.
881 * @genpd: PM domain the device belongs to.
883 * There are two cases in which a device that can wake up the system from sleep
884 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
885 * to wake up the system and it has to remain active for this purpose while the
886 * system is in the sleep state and (2) if the device is not enabled to wake up
887 * the system from sleep states and it generally doesn't generate wakeup signals
888 * by itself (those signals are generated on its behalf by other parts of the
889 * system). In the latter case it may be necessary to reconfigure the device's
890 * wakeup settings during system suspend, because it may have been set up to
891 * signal remote wakeup from the system's working state as needed by runtime PM.
892 * Return 'true' in either of the above cases.
894 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
896 bool active_wakeup;
898 if (!device_can_wakeup(dev))
899 return false;
901 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
902 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
906 * pm_genpd_prepare - Start power transition of a device in a PM domain.
907 * @dev: Device to start the transition of.
909 * Start a power transition of a device (during a system-wide power transition)
910 * under the assumption that its pm_domain field points to the domain member of
911 * an object of type struct generic_pm_domain representing a PM domain
912 * consisting of I/O devices.
914 static int pm_genpd_prepare(struct device *dev)
916 struct generic_pm_domain *genpd;
917 int ret;
919 dev_dbg(dev, "%s()\n", __func__);
921 genpd = dev_to_genpd(dev);
922 if (IS_ERR(genpd))
923 return -EINVAL;
926 * If a wakeup request is pending for the device, it should be woken up
927 * at this point and a system wakeup event should be reported if it's
928 * set up to wake up the system from sleep states.
930 pm_runtime_get_noresume(dev);
931 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
932 pm_wakeup_event(dev, 0);
934 if (pm_wakeup_pending()) {
935 pm_runtime_put(dev);
936 return -EBUSY;
939 if (resume_needed(dev, genpd))
940 pm_runtime_resume(dev);
942 genpd_acquire_lock(genpd);
944 if (genpd->prepared_count++ == 0) {
945 genpd->suspended_count = 0;
946 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
949 genpd_release_lock(genpd);
951 if (genpd->suspend_power_off) {
952 pm_runtime_put_noidle(dev);
953 return 0;
957 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
958 * so pm_genpd_poweron() will return immediately, but if the device
959 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
960 * to make it operational.
962 pm_runtime_resume(dev);
963 __pm_runtime_disable(dev, false);
965 ret = pm_generic_prepare(dev);
966 if (ret) {
967 mutex_lock(&genpd->lock);
969 if (--genpd->prepared_count == 0)
970 genpd->suspend_power_off = false;
972 mutex_unlock(&genpd->lock);
973 pm_runtime_enable(dev);
976 pm_runtime_put(dev);
977 return ret;
981 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
982 * @dev: Device to suspend.
984 * Suspend a device under the assumption that its pm_domain field points to the
985 * domain member of an object of type struct generic_pm_domain representing
986 * a PM domain consisting of I/O devices.
988 static int pm_genpd_suspend(struct device *dev)
990 struct generic_pm_domain *genpd;
992 dev_dbg(dev, "%s()\n", __func__);
994 genpd = dev_to_genpd(dev);
995 if (IS_ERR(genpd))
996 return -EINVAL;
998 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
1002 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
1003 * @dev: Device to suspend.
1005 * Carry out a late suspend of a device under the assumption that its
1006 * pm_domain field points to the domain member of an object of type
1007 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1009 static int pm_genpd_suspend_late(struct device *dev)
1011 struct generic_pm_domain *genpd;
1013 dev_dbg(dev, "%s()\n", __func__);
1015 genpd = dev_to_genpd(dev);
1016 if (IS_ERR(genpd))
1017 return -EINVAL;
1019 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1023 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1024 * @dev: Device to suspend.
1026 * Stop the device and remove power from the domain if all devices in it have
1027 * been stopped.
1029 static int pm_genpd_suspend_noirq(struct device *dev)
1031 struct generic_pm_domain *genpd;
1033 dev_dbg(dev, "%s()\n", __func__);
1035 genpd = dev_to_genpd(dev);
1036 if (IS_ERR(genpd))
1037 return -EINVAL;
1039 if (genpd->suspend_power_off
1040 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1041 return 0;
1043 genpd_stop_dev(genpd, dev);
1046 * Since all of the "noirq" callbacks are executed sequentially, it is
1047 * guaranteed that this function will never run twice in parallel for
1048 * the same PM domain, so it is not necessary to use locking here.
1050 genpd->suspended_count++;
1051 pm_genpd_sync_poweroff(genpd);
1053 return 0;
1057 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1058 * @dev: Device to resume.
1060 * Restore power to the device's PM domain, if necessary, and start the device.
1062 static int pm_genpd_resume_noirq(struct device *dev)
1064 struct generic_pm_domain *genpd;
1066 dev_dbg(dev, "%s()\n", __func__);
1068 genpd = dev_to_genpd(dev);
1069 if (IS_ERR(genpd))
1070 return -EINVAL;
1072 if (genpd->suspend_power_off
1073 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1074 return 0;
1077 * Since all of the "noirq" callbacks are executed sequentially, it is
1078 * guaranteed that this function will never run twice in parallel for
1079 * the same PM domain, so it is not necessary to use locking here.
1081 pm_genpd_sync_poweron(genpd);
1082 genpd->suspended_count--;
1084 return genpd_start_dev(genpd, dev);
1088 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1089 * @dev: Device to resume.
1091 * Carry out an early resume of a device under the assumption that its
1092 * pm_domain field points to the domain member of an object of type
1093 * struct generic_pm_domain representing a power domain consisting of I/O
1094 * devices.
1096 static int pm_genpd_resume_early(struct device *dev)
1098 struct generic_pm_domain *genpd;
1100 dev_dbg(dev, "%s()\n", __func__);
1102 genpd = dev_to_genpd(dev);
1103 if (IS_ERR(genpd))
1104 return -EINVAL;
1106 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1110 * pm_genpd_resume - Resume of device in an I/O PM domain.
1111 * @dev: Device to resume.
1113 * Resume a device under the assumption that its pm_domain field points to the
1114 * domain member of an object of type struct generic_pm_domain representing
1115 * a power domain consisting of I/O devices.
1117 static int pm_genpd_resume(struct device *dev)
1119 struct generic_pm_domain *genpd;
1121 dev_dbg(dev, "%s()\n", __func__);
1123 genpd = dev_to_genpd(dev);
1124 if (IS_ERR(genpd))
1125 return -EINVAL;
1127 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1131 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1132 * @dev: Device to freeze.
1134 * Freeze a device under the assumption that its pm_domain field points to the
1135 * domain member of an object of type struct generic_pm_domain representing
1136 * a power domain consisting of I/O devices.
1138 static int pm_genpd_freeze(struct device *dev)
1140 struct generic_pm_domain *genpd;
1142 dev_dbg(dev, "%s()\n", __func__);
1144 genpd = dev_to_genpd(dev);
1145 if (IS_ERR(genpd))
1146 return -EINVAL;
1148 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1152 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1153 * @dev: Device to freeze.
1155 * Carry out a late freeze of a device under the assumption that its
1156 * pm_domain field points to the domain member of an object of type
1157 * struct generic_pm_domain representing a power domain consisting of I/O
1158 * devices.
1160 static int pm_genpd_freeze_late(struct device *dev)
1162 struct generic_pm_domain *genpd;
1164 dev_dbg(dev, "%s()\n", __func__);
1166 genpd = dev_to_genpd(dev);
1167 if (IS_ERR(genpd))
1168 return -EINVAL;
1170 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1174 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1175 * @dev: Device to freeze.
1177 * Carry out a late freeze of a device under the assumption that its
1178 * pm_domain field points to the domain member of an object of type
1179 * struct generic_pm_domain representing a power domain consisting of I/O
1180 * devices.
1182 static int pm_genpd_freeze_noirq(struct device *dev)
1184 struct generic_pm_domain *genpd;
1186 dev_dbg(dev, "%s()\n", __func__);
1188 genpd = dev_to_genpd(dev);
1189 if (IS_ERR(genpd))
1190 return -EINVAL;
1192 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1196 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1197 * @dev: Device to thaw.
1199 * Start the device, unless power has been removed from the domain already
1200 * before the system transition.
1202 static int pm_genpd_thaw_noirq(struct device *dev)
1204 struct generic_pm_domain *genpd;
1206 dev_dbg(dev, "%s()\n", __func__);
1208 genpd = dev_to_genpd(dev);
1209 if (IS_ERR(genpd))
1210 return -EINVAL;
1212 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1216 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1217 * @dev: Device to thaw.
1219 * Carry out an early thaw of a device under the assumption that its
1220 * pm_domain field points to the domain member of an object of type
1221 * struct generic_pm_domain representing a power domain consisting of I/O
1222 * devices.
1224 static int pm_genpd_thaw_early(struct device *dev)
1226 struct generic_pm_domain *genpd;
1228 dev_dbg(dev, "%s()\n", __func__);
1230 genpd = dev_to_genpd(dev);
1231 if (IS_ERR(genpd))
1232 return -EINVAL;
1234 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1238 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1239 * @dev: Device to thaw.
1241 * Thaw a device under the assumption that its pm_domain field points to the
1242 * domain member of an object of type struct generic_pm_domain representing
1243 * a power domain consisting of I/O devices.
1245 static int pm_genpd_thaw(struct device *dev)
1247 struct generic_pm_domain *genpd;
1249 dev_dbg(dev, "%s()\n", __func__);
1251 genpd = dev_to_genpd(dev);
1252 if (IS_ERR(genpd))
1253 return -EINVAL;
1255 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1259 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1260 * @dev: Device to resume.
1262 * Make sure the domain will be in the same power state as before the
1263 * hibernation the system is resuming from and start the device if necessary.
1265 static int pm_genpd_restore_noirq(struct device *dev)
1267 struct generic_pm_domain *genpd;
1269 dev_dbg(dev, "%s()\n", __func__);
1271 genpd = dev_to_genpd(dev);
1272 if (IS_ERR(genpd))
1273 return -EINVAL;
1276 * Since all of the "noirq" callbacks are executed sequentially, it is
1277 * guaranteed that this function will never run twice in parallel for
1278 * the same PM domain, so it is not necessary to use locking here.
1280 * At this point suspended_count == 0 means we are being run for the
1281 * first time for the given domain in the present cycle.
1283 if (genpd->suspended_count++ == 0) {
1285 * The boot kernel might put the domain into arbitrary state,
1286 * so make it appear as powered off to pm_genpd_sync_poweron(),
1287 * so that it tries to power it on in case it was really off.
1289 genpd->status = GPD_STATE_POWER_OFF;
1290 if (genpd->suspend_power_off) {
1292 * If the domain was off before the hibernation, make
1293 * sure it will be off going forward.
1295 if (genpd->power_off)
1296 genpd->power_off(genpd);
1298 return 0;
1302 if (genpd->suspend_power_off)
1303 return 0;
1305 pm_genpd_sync_poweron(genpd);
1307 return genpd_start_dev(genpd, dev);
1311 * pm_genpd_complete - Complete power transition of a device in a power domain.
1312 * @dev: Device to complete the transition of.
1314 * Complete a power transition of a device (during a system-wide power
1315 * transition) under the assumption that its pm_domain field points to the
1316 * domain member of an object of type struct generic_pm_domain representing
1317 * a power domain consisting of I/O devices.
1319 static void pm_genpd_complete(struct device *dev)
1321 struct generic_pm_domain *genpd;
1322 bool run_complete;
1324 dev_dbg(dev, "%s()\n", __func__);
1326 genpd = dev_to_genpd(dev);
1327 if (IS_ERR(genpd))
1328 return;
1330 mutex_lock(&genpd->lock);
1332 run_complete = !genpd->suspend_power_off;
1333 if (--genpd->prepared_count == 0)
1334 genpd->suspend_power_off = false;
1336 mutex_unlock(&genpd->lock);
1338 if (run_complete) {
1339 pm_generic_complete(dev);
1340 pm_runtime_set_active(dev);
1341 pm_runtime_enable(dev);
1342 pm_request_idle(dev);
1347 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1348 * @dev: Device that normally is marked as "always on" to switch power for.
1350 * This routine may only be called during the system core (syscore) suspend or
1351 * resume phase for devices whose "always on" flags are set.
1353 void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1355 struct generic_pm_domain *genpd;
1357 genpd = dev_to_genpd(dev);
1358 if (!pm_genpd_present(genpd))
1359 return;
1361 if (suspend) {
1362 genpd->suspended_count++;
1363 pm_genpd_sync_poweroff(genpd);
1364 } else {
1365 pm_genpd_sync_poweron(genpd);
1366 genpd->suspended_count--;
1369 EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1371 #else
1373 #define pm_genpd_prepare NULL
1374 #define pm_genpd_suspend NULL
1375 #define pm_genpd_suspend_late NULL
1376 #define pm_genpd_suspend_noirq NULL
1377 #define pm_genpd_resume_early NULL
1378 #define pm_genpd_resume_noirq NULL
1379 #define pm_genpd_resume NULL
1380 #define pm_genpd_freeze NULL
1381 #define pm_genpd_freeze_late NULL
1382 #define pm_genpd_freeze_noirq NULL
1383 #define pm_genpd_thaw_early NULL
1384 #define pm_genpd_thaw_noirq NULL
1385 #define pm_genpd_thaw NULL
1386 #define pm_genpd_restore_noirq NULL
1387 #define pm_genpd_complete NULL
1389 #endif /* CONFIG_PM_SLEEP */
1391 static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1393 struct generic_pm_domain_data *gpd_data;
1395 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1396 if (!gpd_data)
1397 return NULL;
1399 mutex_init(&gpd_data->lock);
1400 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1401 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1402 return gpd_data;
1405 static void __pm_genpd_free_dev_data(struct device *dev,
1406 struct generic_pm_domain_data *gpd_data)
1408 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1409 kfree(gpd_data);
1413 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1414 * @genpd: PM domain to add the device to.
1415 * @dev: Device to be added.
1416 * @td: Set of PM QoS timing parameters to attach to the device.
1418 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1419 struct gpd_timing_data *td)
1421 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1422 struct pm_domain_data *pdd;
1423 int ret = 0;
1425 dev_dbg(dev, "%s()\n", __func__);
1427 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1428 return -EINVAL;
1430 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1431 if (!gpd_data_new)
1432 return -ENOMEM;
1434 genpd_acquire_lock(genpd);
1436 if (genpd->prepared_count > 0) {
1437 ret = -EAGAIN;
1438 goto out;
1441 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1442 if (pdd->dev == dev) {
1443 ret = -EINVAL;
1444 goto out;
1447 ret = dev_pm_get_subsys_data(dev);
1448 if (ret)
1449 goto out;
1451 genpd->device_count++;
1452 genpd->max_off_time_changed = true;
1454 spin_lock_irq(&dev->power.lock);
1456 dev->pm_domain = &genpd->domain;
1457 if (dev->power.subsys_data->domain_data) {
1458 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1459 } else {
1460 gpd_data = gpd_data_new;
1461 dev->power.subsys_data->domain_data = &gpd_data->base;
1463 gpd_data->refcount++;
1464 if (td)
1465 gpd_data->td = *td;
1467 spin_unlock_irq(&dev->power.lock);
1469 mutex_lock(&gpd_data->lock);
1470 gpd_data->base.dev = dev;
1471 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1472 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1473 gpd_data->td.constraint_changed = true;
1474 gpd_data->td.effective_constraint_ns = -1;
1475 mutex_unlock(&gpd_data->lock);
1477 out:
1478 genpd_release_lock(genpd);
1480 if (gpd_data != gpd_data_new)
1481 __pm_genpd_free_dev_data(dev, gpd_data_new);
1483 return ret;
1487 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1488 * @genpd_node: Device tree node pointer representing a PM domain to which the
1489 * the device is added to.
1490 * @dev: Device to be added.
1491 * @td: Set of PM QoS timing parameters to attach to the device.
1493 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1494 struct gpd_timing_data *td)
1496 struct generic_pm_domain *genpd = NULL, *gpd;
1498 dev_dbg(dev, "%s()\n", __func__);
1500 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1501 return -EINVAL;
1503 mutex_lock(&gpd_list_lock);
1504 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1505 if (gpd->of_node == genpd_node) {
1506 genpd = gpd;
1507 break;
1510 mutex_unlock(&gpd_list_lock);
1512 if (!genpd)
1513 return -EINVAL;
1515 return __pm_genpd_add_device(genpd, dev, td);
1520 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1521 * @domain_name: Name of the PM domain to add the device to.
1522 * @dev: Device to be added.
1523 * @td: Set of PM QoS timing parameters to attach to the device.
1525 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1526 struct gpd_timing_data *td)
1528 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1532 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1533 * @genpd: PM domain to remove the device from.
1534 * @dev: Device to be removed.
1536 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1537 struct device *dev)
1539 struct generic_pm_domain_data *gpd_data;
1540 struct pm_domain_data *pdd;
1541 bool remove = false;
1542 int ret = 0;
1544 dev_dbg(dev, "%s()\n", __func__);
1546 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1547 || IS_ERR_OR_NULL(dev->pm_domain)
1548 || pd_to_genpd(dev->pm_domain) != genpd)
1549 return -EINVAL;
1551 genpd_acquire_lock(genpd);
1553 if (genpd->prepared_count > 0) {
1554 ret = -EAGAIN;
1555 goto out;
1558 genpd->device_count--;
1559 genpd->max_off_time_changed = true;
1561 spin_lock_irq(&dev->power.lock);
1563 dev->pm_domain = NULL;
1564 pdd = dev->power.subsys_data->domain_data;
1565 list_del_init(&pdd->list_node);
1566 gpd_data = to_gpd_data(pdd);
1567 if (--gpd_data->refcount == 0) {
1568 dev->power.subsys_data->domain_data = NULL;
1569 remove = true;
1572 spin_unlock_irq(&dev->power.lock);
1574 mutex_lock(&gpd_data->lock);
1575 pdd->dev = NULL;
1576 mutex_unlock(&gpd_data->lock);
1578 genpd_release_lock(genpd);
1580 dev_pm_put_subsys_data(dev);
1581 if (remove)
1582 __pm_genpd_free_dev_data(dev, gpd_data);
1584 return 0;
1586 out:
1587 genpd_release_lock(genpd);
1589 return ret;
1593 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1594 * @dev: Device to set/unset the flag for.
1595 * @val: The new value of the device's "need restore" flag.
1597 void pm_genpd_dev_need_restore(struct device *dev, bool val)
1599 struct pm_subsys_data *psd;
1600 unsigned long flags;
1602 spin_lock_irqsave(&dev->power.lock, flags);
1604 psd = dev_to_psd(dev);
1605 if (psd && psd->domain_data)
1606 to_gpd_data(psd->domain_data)->need_restore = val;
1608 spin_unlock_irqrestore(&dev->power.lock, flags);
1610 EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1613 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1614 * @genpd: Master PM domain to add the subdomain to.
1615 * @subdomain: Subdomain to be added.
1617 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1618 struct generic_pm_domain *subdomain)
1620 struct gpd_link *link;
1621 int ret = 0;
1623 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1624 || genpd == subdomain)
1625 return -EINVAL;
1627 start:
1628 genpd_acquire_lock(genpd);
1629 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1631 if (subdomain->status != GPD_STATE_POWER_OFF
1632 && subdomain->status != GPD_STATE_ACTIVE) {
1633 mutex_unlock(&subdomain->lock);
1634 genpd_release_lock(genpd);
1635 goto start;
1638 if (genpd->status == GPD_STATE_POWER_OFF
1639 && subdomain->status != GPD_STATE_POWER_OFF) {
1640 ret = -EINVAL;
1641 goto out;
1644 list_for_each_entry(link, &genpd->master_links, master_node) {
1645 if (link->slave == subdomain && link->master == genpd) {
1646 ret = -EINVAL;
1647 goto out;
1651 link = kzalloc(sizeof(*link), GFP_KERNEL);
1652 if (!link) {
1653 ret = -ENOMEM;
1654 goto out;
1656 link->master = genpd;
1657 list_add_tail(&link->master_node, &genpd->master_links);
1658 link->slave = subdomain;
1659 list_add_tail(&link->slave_node, &subdomain->slave_links);
1660 if (subdomain->status != GPD_STATE_POWER_OFF)
1661 genpd_sd_counter_inc(genpd);
1663 out:
1664 mutex_unlock(&subdomain->lock);
1665 genpd_release_lock(genpd);
1667 return ret;
1671 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1672 * @master_name: Name of the master PM domain to add the subdomain to.
1673 * @subdomain_name: Name of the subdomain to be added.
1675 int pm_genpd_add_subdomain_names(const char *master_name,
1676 const char *subdomain_name)
1678 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1680 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1681 return -EINVAL;
1683 mutex_lock(&gpd_list_lock);
1684 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1685 if (!master && !strcmp(gpd->name, master_name))
1686 master = gpd;
1688 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1689 subdomain = gpd;
1691 if (master && subdomain)
1692 break;
1694 mutex_unlock(&gpd_list_lock);
1696 return pm_genpd_add_subdomain(master, subdomain);
1700 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1701 * @genpd: Master PM domain to remove the subdomain from.
1702 * @subdomain: Subdomain to be removed.
1704 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1705 struct generic_pm_domain *subdomain)
1707 struct gpd_link *link;
1708 int ret = -EINVAL;
1710 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1711 return -EINVAL;
1713 start:
1714 genpd_acquire_lock(genpd);
1716 list_for_each_entry(link, &genpd->master_links, master_node) {
1717 if (link->slave != subdomain)
1718 continue;
1720 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1722 if (subdomain->status != GPD_STATE_POWER_OFF
1723 && subdomain->status != GPD_STATE_ACTIVE) {
1724 mutex_unlock(&subdomain->lock);
1725 genpd_release_lock(genpd);
1726 goto start;
1729 list_del(&link->master_node);
1730 list_del(&link->slave_node);
1731 kfree(link);
1732 if (subdomain->status != GPD_STATE_POWER_OFF)
1733 genpd_sd_counter_dec(genpd);
1735 mutex_unlock(&subdomain->lock);
1737 ret = 0;
1738 break;
1741 genpd_release_lock(genpd);
1743 return ret;
1747 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1748 * @dev: Device to add the callbacks to.
1749 * @ops: Set of callbacks to add.
1750 * @td: Timing data to add to the device along with the callbacks (optional).
1752 * Every call to this routine should be balanced with a call to
1753 * __pm_genpd_remove_callbacks() and they must not be nested.
1755 int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1756 struct gpd_timing_data *td)
1758 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1759 int ret = 0;
1761 if (!(dev && ops))
1762 return -EINVAL;
1764 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1765 if (!gpd_data_new)
1766 return -ENOMEM;
1768 pm_runtime_disable(dev);
1769 device_pm_lock();
1771 ret = dev_pm_get_subsys_data(dev);
1772 if (ret)
1773 goto out;
1775 spin_lock_irq(&dev->power.lock);
1777 if (dev->power.subsys_data->domain_data) {
1778 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1779 } else {
1780 gpd_data = gpd_data_new;
1781 dev->power.subsys_data->domain_data = &gpd_data->base;
1783 gpd_data->refcount++;
1784 gpd_data->ops = *ops;
1785 if (td)
1786 gpd_data->td = *td;
1788 spin_unlock_irq(&dev->power.lock);
1790 out:
1791 device_pm_unlock();
1792 pm_runtime_enable(dev);
1794 if (gpd_data != gpd_data_new)
1795 __pm_genpd_free_dev_data(dev, gpd_data_new);
1797 return ret;
1799 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1802 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1803 * @dev: Device to remove the callbacks from.
1804 * @clear_td: If set, clear the device's timing data too.
1806 * This routine can only be called after pm_genpd_add_callbacks().
1808 int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1810 struct generic_pm_domain_data *gpd_data = NULL;
1811 bool remove = false;
1812 int ret = 0;
1814 if (!(dev && dev->power.subsys_data))
1815 return -EINVAL;
1817 pm_runtime_disable(dev);
1818 device_pm_lock();
1820 spin_lock_irq(&dev->power.lock);
1822 if (dev->power.subsys_data->domain_data) {
1823 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1824 gpd_data->ops = (struct gpd_dev_ops){ NULL };
1825 if (clear_td)
1826 gpd_data->td = (struct gpd_timing_data){ 0 };
1828 if (--gpd_data->refcount == 0) {
1829 dev->power.subsys_data->domain_data = NULL;
1830 remove = true;
1832 } else {
1833 ret = -EINVAL;
1836 spin_unlock_irq(&dev->power.lock);
1838 device_pm_unlock();
1839 pm_runtime_enable(dev);
1841 if (ret)
1842 return ret;
1844 dev_pm_put_subsys_data(dev);
1845 if (remove)
1846 __pm_genpd_free_dev_data(dev, gpd_data);
1848 return 0;
1850 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1853 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1854 * @genpd: PM domain to be connected with cpuidle.
1855 * @state: cpuidle state this domain can disable/enable.
1857 * Make a PM domain behave as though it contained a CPU core, that is, instead
1858 * of calling its power down routine it will enable the given cpuidle state so
1859 * that the cpuidle subsystem can power it down (if possible and desirable).
1861 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1863 struct cpuidle_driver *cpuidle_drv;
1864 struct gpd_cpu_data *cpu_data;
1865 struct cpuidle_state *idle_state;
1866 int ret = 0;
1868 if (IS_ERR_OR_NULL(genpd) || state < 0)
1869 return -EINVAL;
1871 genpd_acquire_lock(genpd);
1873 if (genpd->cpu_data) {
1874 ret = -EEXIST;
1875 goto out;
1877 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1878 if (!cpu_data) {
1879 ret = -ENOMEM;
1880 goto out;
1882 cpuidle_drv = cpuidle_driver_ref();
1883 if (!cpuidle_drv) {
1884 ret = -ENODEV;
1885 goto err_drv;
1887 if (cpuidle_drv->state_count <= state) {
1888 ret = -EINVAL;
1889 goto err;
1891 idle_state = &cpuidle_drv->states[state];
1892 if (!idle_state->disabled) {
1893 ret = -EAGAIN;
1894 goto err;
1896 cpu_data->idle_state = idle_state;
1897 cpu_data->saved_exit_latency = idle_state->exit_latency;
1898 genpd->cpu_data = cpu_data;
1899 genpd_recalc_cpu_exit_latency(genpd);
1901 out:
1902 genpd_release_lock(genpd);
1903 return ret;
1905 err:
1906 cpuidle_driver_unref();
1908 err_drv:
1909 kfree(cpu_data);
1910 goto out;
1914 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1915 * @name: Name of the domain to connect to cpuidle.
1916 * @state: cpuidle state this domain can manipulate.
1918 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1920 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1924 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1925 * @genpd: PM domain to remove the cpuidle connection from.
1927 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1928 * given PM domain.
1930 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1932 struct gpd_cpu_data *cpu_data;
1933 struct cpuidle_state *idle_state;
1934 int ret = 0;
1936 if (IS_ERR_OR_NULL(genpd))
1937 return -EINVAL;
1939 genpd_acquire_lock(genpd);
1941 cpu_data = genpd->cpu_data;
1942 if (!cpu_data) {
1943 ret = -ENODEV;
1944 goto out;
1946 idle_state = cpu_data->idle_state;
1947 if (!idle_state->disabled) {
1948 ret = -EAGAIN;
1949 goto out;
1951 idle_state->exit_latency = cpu_data->saved_exit_latency;
1952 cpuidle_driver_unref();
1953 genpd->cpu_data = NULL;
1954 kfree(cpu_data);
1956 out:
1957 genpd_release_lock(genpd);
1958 return ret;
1962 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1963 * @name: Name of the domain to disconnect cpuidle from.
1965 int pm_genpd_name_detach_cpuidle(const char *name)
1967 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1970 /* Default device callbacks for generic PM domains. */
1973 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1974 * @dev: Device to handle.
1976 static int pm_genpd_default_save_state(struct device *dev)
1978 int (*cb)(struct device *__dev);
1980 cb = dev_gpd_data(dev)->ops.save_state;
1981 if (cb)
1982 return cb(dev);
1984 if (dev->type && dev->type->pm)
1985 cb = dev->type->pm->runtime_suspend;
1986 else if (dev->class && dev->class->pm)
1987 cb = dev->class->pm->runtime_suspend;
1988 else if (dev->bus && dev->bus->pm)
1989 cb = dev->bus->pm->runtime_suspend;
1990 else
1991 cb = NULL;
1993 if (!cb && dev->driver && dev->driver->pm)
1994 cb = dev->driver->pm->runtime_suspend;
1996 return cb ? cb(dev) : 0;
2000 * pm_genpd_default_restore_state - Default PM domians "restore device state".
2001 * @dev: Device to handle.
2003 static int pm_genpd_default_restore_state(struct device *dev)
2005 int (*cb)(struct device *__dev);
2007 cb = dev_gpd_data(dev)->ops.restore_state;
2008 if (cb)
2009 return cb(dev);
2011 if (dev->type && dev->type->pm)
2012 cb = dev->type->pm->runtime_resume;
2013 else if (dev->class && dev->class->pm)
2014 cb = dev->class->pm->runtime_resume;
2015 else if (dev->bus && dev->bus->pm)
2016 cb = dev->bus->pm->runtime_resume;
2017 else
2018 cb = NULL;
2020 if (!cb && dev->driver && dev->driver->pm)
2021 cb = dev->driver->pm->runtime_resume;
2023 return cb ? cb(dev) : 0;
2026 #ifdef CONFIG_PM_SLEEP
2029 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2030 * @dev: Device to handle.
2032 static int pm_genpd_default_suspend(struct device *dev)
2034 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
2036 return cb ? cb(dev) : pm_generic_suspend(dev);
2040 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2041 * @dev: Device to handle.
2043 static int pm_genpd_default_suspend_late(struct device *dev)
2045 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
2047 return cb ? cb(dev) : pm_generic_suspend_late(dev);
2051 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2052 * @dev: Device to handle.
2054 static int pm_genpd_default_resume_early(struct device *dev)
2056 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
2058 return cb ? cb(dev) : pm_generic_resume_early(dev);
2062 * pm_genpd_default_resume - Default "device resume" for PM domians.
2063 * @dev: Device to handle.
2065 static int pm_genpd_default_resume(struct device *dev)
2067 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
2069 return cb ? cb(dev) : pm_generic_resume(dev);
2073 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2074 * @dev: Device to handle.
2076 static int pm_genpd_default_freeze(struct device *dev)
2078 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
2080 return cb ? cb(dev) : pm_generic_freeze(dev);
2084 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2085 * @dev: Device to handle.
2087 static int pm_genpd_default_freeze_late(struct device *dev)
2089 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
2091 return cb ? cb(dev) : pm_generic_freeze_late(dev);
2095 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2096 * @dev: Device to handle.
2098 static int pm_genpd_default_thaw_early(struct device *dev)
2100 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
2102 return cb ? cb(dev) : pm_generic_thaw_early(dev);
2106 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2107 * @dev: Device to handle.
2109 static int pm_genpd_default_thaw(struct device *dev)
2111 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
2113 return cb ? cb(dev) : pm_generic_thaw(dev);
2116 #else /* !CONFIG_PM_SLEEP */
2118 #define pm_genpd_default_suspend NULL
2119 #define pm_genpd_default_suspend_late NULL
2120 #define pm_genpd_default_resume_early NULL
2121 #define pm_genpd_default_resume NULL
2122 #define pm_genpd_default_freeze NULL
2123 #define pm_genpd_default_freeze_late NULL
2124 #define pm_genpd_default_thaw_early NULL
2125 #define pm_genpd_default_thaw NULL
2127 #endif /* !CONFIG_PM_SLEEP */
2130 * pm_genpd_init - Initialize a generic I/O PM domain object.
2131 * @genpd: PM domain object to initialize.
2132 * @gov: PM domain governor to associate with the domain (may be NULL).
2133 * @is_off: Initial value of the domain's power_is_off field.
2135 void pm_genpd_init(struct generic_pm_domain *genpd,
2136 struct dev_power_governor *gov, bool is_off)
2138 if (IS_ERR_OR_NULL(genpd))
2139 return;
2141 INIT_LIST_HEAD(&genpd->master_links);
2142 INIT_LIST_HEAD(&genpd->slave_links);
2143 INIT_LIST_HEAD(&genpd->dev_list);
2144 mutex_init(&genpd->lock);
2145 genpd->gov = gov;
2146 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2147 genpd->in_progress = 0;
2148 atomic_set(&genpd->sd_count, 0);
2149 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2150 init_waitqueue_head(&genpd->status_wait_queue);
2151 genpd->poweroff_task = NULL;
2152 genpd->resume_count = 0;
2153 genpd->device_count = 0;
2154 genpd->max_off_time_ns = -1;
2155 genpd->max_off_time_changed = true;
2156 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2157 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2158 genpd->domain.ops.prepare = pm_genpd_prepare;
2159 genpd->domain.ops.suspend = pm_genpd_suspend;
2160 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
2161 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2162 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
2163 genpd->domain.ops.resume_early = pm_genpd_resume_early;
2164 genpd->domain.ops.resume = pm_genpd_resume;
2165 genpd->domain.ops.freeze = pm_genpd_freeze;
2166 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
2167 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2168 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
2169 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
2170 genpd->domain.ops.thaw = pm_genpd_thaw;
2171 genpd->domain.ops.poweroff = pm_genpd_suspend;
2172 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
2173 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
2174 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
2175 genpd->domain.ops.restore_early = pm_genpd_resume_early;
2176 genpd->domain.ops.restore = pm_genpd_resume;
2177 genpd->domain.ops.complete = pm_genpd_complete;
2178 genpd->dev_ops.save_state = pm_genpd_default_save_state;
2179 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
2180 genpd->dev_ops.suspend = pm_genpd_default_suspend;
2181 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2182 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2183 genpd->dev_ops.resume = pm_genpd_default_resume;
2184 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2185 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2186 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2187 genpd->dev_ops.thaw = pm_genpd_default_thaw;
2188 mutex_lock(&gpd_list_lock);
2189 list_add(&genpd->gpd_list_node, &gpd_list);
2190 mutex_unlock(&gpd_list_lock);