PM / Domains: Try power off masters in error path of __pm_genpd_poweron()
[linux/fpc-iii.git] / drivers / base / power / domain.c
blob62f7572502355358f14a722a00f8d3932f3dcbc1
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/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_qos.h>
16 #include <linux/pm_clock.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/sched.h>
20 #include <linux/suspend.h>
21 #include <linux/export.h>
23 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
25 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
26 ({ \
27 type (*__routine)(struct device *__d); \
28 type __ret = (type)0; \
30 __routine = genpd->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_dbg(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;
75 * Get the generic PM domain for a particular struct device.
76 * This validates the struct device pointer, the PM domain pointer,
77 * and checks that the PM domain pointer is a real generic PM domain.
78 * Any failure results in NULL being returned.
80 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
82 struct generic_pm_domain *genpd = NULL, *gpd;
84 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
85 return NULL;
87 mutex_lock(&gpd_list_lock);
88 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
89 if (&gpd->domain == dev->pm_domain) {
90 genpd = gpd;
91 break;
94 mutex_unlock(&gpd_list_lock);
96 return genpd;
100 * This should only be used where we are certain that the pm_domain
101 * attached to the device is a genpd domain.
103 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
105 if (IS_ERR_OR_NULL(dev->pm_domain))
106 return ERR_PTR(-EINVAL);
108 return pd_to_genpd(dev->pm_domain);
111 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
113 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
114 stop_latency_ns, "stop");
117 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev,
118 bool timed)
120 if (!timed)
121 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
123 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
124 start_latency_ns, "start");
127 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
129 bool ret = false;
131 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
132 ret = !!atomic_dec_and_test(&genpd->sd_count);
134 return ret;
137 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
139 atomic_inc(&genpd->sd_count);
140 smp_mb__after_atomic();
143 static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
145 s64 usecs64;
147 if (!genpd->cpuidle_data)
148 return;
150 usecs64 = genpd->power_on_latency_ns;
151 do_div(usecs64, NSEC_PER_USEC);
152 usecs64 += genpd->cpuidle_data->saved_exit_latency;
153 genpd->cpuidle_data->idle_state->exit_latency = usecs64;
156 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
158 ktime_t time_start;
159 s64 elapsed_ns;
160 int ret;
162 if (!genpd->power_on)
163 return 0;
165 if (!timed)
166 return genpd->power_on(genpd);
168 time_start = ktime_get();
169 ret = genpd->power_on(genpd);
170 if (ret)
171 return ret;
173 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
174 if (elapsed_ns <= genpd->power_on_latency_ns)
175 return ret;
177 genpd->power_on_latency_ns = elapsed_ns;
178 genpd->max_off_time_changed = true;
179 genpd_recalc_cpu_exit_latency(genpd);
180 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
181 genpd->name, "on", elapsed_ns);
183 return ret;
186 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
188 ktime_t time_start;
189 s64 elapsed_ns;
190 int ret;
192 if (!genpd->power_off)
193 return 0;
195 if (!timed)
196 return genpd->power_off(genpd);
198 time_start = ktime_get();
199 ret = genpd->power_off(genpd);
200 if (ret == -EBUSY)
201 return ret;
203 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
204 if (elapsed_ns <= genpd->power_off_latency_ns)
205 return ret;
207 genpd->power_off_latency_ns = elapsed_ns;
208 genpd->max_off_time_changed = true;
209 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
210 genpd->name, "off", elapsed_ns);
212 return ret;
216 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
217 * @genpd: PM domait to power off.
219 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
220 * before.
222 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
224 queue_work(pm_wq, &genpd->power_off_work);
228 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
229 * @genpd: PM domain to power up.
231 * Restore power to @genpd and all of its masters so that it is possible to
232 * resume a device belonging to it.
234 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
236 struct gpd_link *link;
237 int ret = 0;
239 if (genpd->status == GPD_STATE_ACTIVE
240 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
241 return 0;
243 if (genpd->cpuidle_data) {
244 cpuidle_pause_and_lock();
245 genpd->cpuidle_data->idle_state->disabled = true;
246 cpuidle_resume_and_unlock();
247 goto out;
251 * The list is guaranteed not to change while the loop below is being
252 * executed, unless one of the masters' .power_on() callbacks fiddles
253 * with it.
255 list_for_each_entry(link, &genpd->slave_links, slave_node) {
256 genpd_sd_counter_inc(link->master);
258 ret = pm_genpd_poweron(link->master);
259 if (ret) {
260 genpd_sd_counter_dec(link->master);
261 goto err;
265 ret = genpd_power_on(genpd, true);
266 if (ret)
267 goto err;
269 out:
270 genpd->status = GPD_STATE_ACTIVE;
271 return 0;
273 err:
274 list_for_each_entry_continue_reverse(link,
275 &genpd->slave_links,
276 slave_node) {
277 genpd_sd_counter_dec(link->master);
278 genpd_queue_power_off_work(link->master);
281 return ret;
285 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
286 * @genpd: PM domain to power up.
288 int pm_genpd_poweron(struct generic_pm_domain *genpd)
290 int ret;
292 mutex_lock(&genpd->lock);
293 ret = __pm_genpd_poweron(genpd);
294 mutex_unlock(&genpd->lock);
295 return ret;
299 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
300 * @domain_name: Name of the PM domain to power up.
302 int pm_genpd_name_poweron(const char *domain_name)
304 struct generic_pm_domain *genpd;
306 genpd = pm_genpd_lookup_name(domain_name);
307 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
310 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
312 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
313 save_state_latency_ns, "state save");
316 static int genpd_restore_dev(struct generic_pm_domain *genpd,
317 struct device *dev, bool timed)
319 if (!timed)
320 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
322 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
323 restore_state_latency_ns,
324 "state restore");
327 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
328 unsigned long val, void *ptr)
330 struct generic_pm_domain_data *gpd_data;
331 struct device *dev;
333 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
334 dev = gpd_data->base.dev;
336 for (;;) {
337 struct generic_pm_domain *genpd;
338 struct pm_domain_data *pdd;
340 spin_lock_irq(&dev->power.lock);
342 pdd = dev->power.subsys_data ?
343 dev->power.subsys_data->domain_data : NULL;
344 if (pdd && pdd->dev) {
345 to_gpd_data(pdd)->td.constraint_changed = true;
346 genpd = dev_to_genpd(dev);
347 } else {
348 genpd = ERR_PTR(-ENODATA);
351 spin_unlock_irq(&dev->power.lock);
353 if (!IS_ERR(genpd)) {
354 mutex_lock(&genpd->lock);
355 genpd->max_off_time_changed = true;
356 mutex_unlock(&genpd->lock);
359 dev = dev->parent;
360 if (!dev || dev->power.ignore_children)
361 break;
364 return NOTIFY_DONE;
368 * pm_genpd_poweroff - Remove power from a given PM domain.
369 * @genpd: PM domain to power down.
371 * If all of the @genpd's devices have been suspended and all of its subdomains
372 * have been powered down, remove power from @genpd.
374 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
376 struct pm_domain_data *pdd;
377 struct gpd_link *link;
378 unsigned int not_suspended = 0;
381 * Do not try to power off the domain in the following situations:
382 * (1) The domain is already in the "power off" state.
383 * (2) System suspend is in progress.
385 if (genpd->status == GPD_STATE_POWER_OFF
386 || genpd->prepared_count > 0)
387 return 0;
389 if (atomic_read(&genpd->sd_count) > 0)
390 return -EBUSY;
392 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
393 enum pm_qos_flags_status stat;
395 stat = dev_pm_qos_flags(pdd->dev,
396 PM_QOS_FLAG_NO_POWER_OFF
397 | PM_QOS_FLAG_REMOTE_WAKEUP);
398 if (stat > PM_QOS_FLAGS_NONE)
399 return -EBUSY;
401 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
402 || pdd->dev->power.irq_safe))
403 not_suspended++;
406 if (not_suspended > genpd->in_progress)
407 return -EBUSY;
409 if (genpd->gov && genpd->gov->power_down_ok) {
410 if (!genpd->gov->power_down_ok(&genpd->domain))
411 return -EAGAIN;
414 if (genpd->cpuidle_data) {
416 * If cpuidle_data is set, cpuidle should turn the domain off
417 * when the CPU in it is idle. In that case we don't decrement
418 * the subdomain counts of the master domains, so that power is
419 * not removed from the current domain prematurely as a result
420 * of cutting off the masters' power.
422 genpd->status = GPD_STATE_POWER_OFF;
423 cpuidle_pause_and_lock();
424 genpd->cpuidle_data->idle_state->disabled = false;
425 cpuidle_resume_and_unlock();
426 return 0;
429 if (genpd->power_off) {
430 int ret;
432 if (atomic_read(&genpd->sd_count) > 0)
433 return -EBUSY;
436 * If sd_count > 0 at this point, one of the subdomains hasn't
437 * managed to call pm_genpd_poweron() for the master yet after
438 * incrementing it. In that case pm_genpd_poweron() will wait
439 * for us to drop the lock, so we can call .power_off() and let
440 * the pm_genpd_poweron() restore power for us (this shouldn't
441 * happen very often).
443 ret = genpd_power_off(genpd, true);
444 if (ret)
445 return ret;
448 genpd->status = GPD_STATE_POWER_OFF;
450 list_for_each_entry(link, &genpd->slave_links, slave_node) {
451 genpd_sd_counter_dec(link->master);
452 genpd_queue_power_off_work(link->master);
455 return 0;
459 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
460 * @work: Work structure used for scheduling the execution of this function.
462 static void genpd_power_off_work_fn(struct work_struct *work)
464 struct generic_pm_domain *genpd;
466 genpd = container_of(work, struct generic_pm_domain, power_off_work);
468 mutex_lock(&genpd->lock);
469 pm_genpd_poweroff(genpd);
470 mutex_unlock(&genpd->lock);
474 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
475 * @dev: Device to suspend.
477 * Carry out a runtime suspend of a device under the assumption that its
478 * pm_domain field points to the domain member of an object of type
479 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
481 static int pm_genpd_runtime_suspend(struct device *dev)
483 struct generic_pm_domain *genpd;
484 bool (*stop_ok)(struct device *__dev);
485 int ret;
487 dev_dbg(dev, "%s()\n", __func__);
489 genpd = dev_to_genpd(dev);
490 if (IS_ERR(genpd))
491 return -EINVAL;
493 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
494 if (stop_ok && !stop_ok(dev))
495 return -EBUSY;
497 ret = genpd_save_dev(genpd, dev);
498 if (ret)
499 return ret;
501 ret = genpd_stop_dev(genpd, dev);
502 if (ret) {
503 genpd_restore_dev(genpd, dev, true);
504 return ret;
508 * If power.irq_safe is set, this routine will be run with interrupts
509 * off, so it can't use mutexes.
511 if (dev->power.irq_safe)
512 return 0;
514 mutex_lock(&genpd->lock);
515 genpd->in_progress++;
516 pm_genpd_poweroff(genpd);
517 genpd->in_progress--;
518 mutex_unlock(&genpd->lock);
520 return 0;
524 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
525 * @dev: Device to resume.
527 * Carry out a runtime resume of a device under the assumption that its
528 * pm_domain field points to the domain member of an object of type
529 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
531 static int pm_genpd_runtime_resume(struct device *dev)
533 struct generic_pm_domain *genpd;
534 int ret;
535 bool timed = true;
537 dev_dbg(dev, "%s()\n", __func__);
539 genpd = dev_to_genpd(dev);
540 if (IS_ERR(genpd))
541 return -EINVAL;
543 /* If power.irq_safe, the PM domain is never powered off. */
544 if (dev->power.irq_safe) {
545 timed = false;
546 goto out;
549 mutex_lock(&genpd->lock);
550 ret = __pm_genpd_poweron(genpd);
551 mutex_unlock(&genpd->lock);
553 if (ret)
554 return ret;
556 out:
557 genpd_start_dev(genpd, dev, timed);
558 genpd_restore_dev(genpd, dev, timed);
560 return 0;
563 static bool pd_ignore_unused;
564 static int __init pd_ignore_unused_setup(char *__unused)
566 pd_ignore_unused = true;
567 return 1;
569 __setup("pd_ignore_unused", pd_ignore_unused_setup);
572 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
574 void pm_genpd_poweroff_unused(void)
576 struct generic_pm_domain *genpd;
578 if (pd_ignore_unused) {
579 pr_warn("genpd: Not disabling unused power domains\n");
580 return;
583 mutex_lock(&gpd_list_lock);
585 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
586 genpd_queue_power_off_work(genpd);
588 mutex_unlock(&gpd_list_lock);
591 static int __init genpd_poweroff_unused(void)
593 pm_genpd_poweroff_unused();
594 return 0;
596 late_initcall(genpd_poweroff_unused);
598 #ifdef CONFIG_PM_SLEEP
601 * pm_genpd_present - Check if the given PM domain has been initialized.
602 * @genpd: PM domain to check.
604 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
606 const struct generic_pm_domain *gpd;
608 if (IS_ERR_OR_NULL(genpd))
609 return false;
611 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
612 if (gpd == genpd)
613 return true;
615 return false;
618 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
619 struct device *dev)
621 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
625 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
626 * @genpd: PM domain to power off, if possible.
627 * @timed: True if latency measurements are allowed.
629 * Check if the given PM domain can be powered off (during system suspend or
630 * hibernation) and do that if so. Also, in that case propagate to its masters.
632 * This function is only called in "noirq" and "syscore" stages of system power
633 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
634 * executed sequentially, so it is guaranteed that it will never run twice in
635 * parallel).
637 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
638 bool timed)
640 struct gpd_link *link;
642 if (genpd->status == GPD_STATE_POWER_OFF)
643 return;
645 if (genpd->suspended_count != genpd->device_count
646 || atomic_read(&genpd->sd_count) > 0)
647 return;
649 genpd_power_off(genpd, timed);
651 genpd->status = GPD_STATE_POWER_OFF;
653 list_for_each_entry(link, &genpd->slave_links, slave_node) {
654 genpd_sd_counter_dec(link->master);
655 pm_genpd_sync_poweroff(link->master, timed);
660 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
661 * @genpd: PM domain to power on.
662 * @timed: True if latency measurements are allowed.
664 * This function is only called in "noirq" and "syscore" stages of system power
665 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
666 * executed sequentially, so it is guaranteed that it will never run twice in
667 * parallel).
669 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
670 bool timed)
672 struct gpd_link *link;
674 if (genpd->status == GPD_STATE_ACTIVE)
675 return;
677 list_for_each_entry(link, &genpd->slave_links, slave_node) {
678 pm_genpd_sync_poweron(link->master, timed);
679 genpd_sd_counter_inc(link->master);
682 genpd_power_on(genpd, timed);
684 genpd->status = GPD_STATE_ACTIVE;
688 * resume_needed - Check whether to resume a device before system suspend.
689 * @dev: Device to check.
690 * @genpd: PM domain the device belongs to.
692 * There are two cases in which a device that can wake up the system from sleep
693 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
694 * to wake up the system and it has to remain active for this purpose while the
695 * system is in the sleep state and (2) if the device is not enabled to wake up
696 * the system from sleep states and it generally doesn't generate wakeup signals
697 * by itself (those signals are generated on its behalf by other parts of the
698 * system). In the latter case it may be necessary to reconfigure the device's
699 * wakeup settings during system suspend, because it may have been set up to
700 * signal remote wakeup from the system's working state as needed by runtime PM.
701 * Return 'true' in either of the above cases.
703 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
705 bool active_wakeup;
707 if (!device_can_wakeup(dev))
708 return false;
710 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
711 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
715 * pm_genpd_prepare - Start power transition of a device in a PM domain.
716 * @dev: Device to start the transition of.
718 * Start a power transition of a device (during a system-wide power transition)
719 * under the assumption that its pm_domain field points to the domain member of
720 * an object of type struct generic_pm_domain representing a PM domain
721 * consisting of I/O devices.
723 static int pm_genpd_prepare(struct device *dev)
725 struct generic_pm_domain *genpd;
726 int ret;
728 dev_dbg(dev, "%s()\n", __func__);
730 genpd = dev_to_genpd(dev);
731 if (IS_ERR(genpd))
732 return -EINVAL;
735 * If a wakeup request is pending for the device, it should be woken up
736 * at this point and a system wakeup event should be reported if it's
737 * set up to wake up the system from sleep states.
739 pm_runtime_get_noresume(dev);
740 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
741 pm_wakeup_event(dev, 0);
743 if (pm_wakeup_pending()) {
744 pm_runtime_put(dev);
745 return -EBUSY;
748 if (resume_needed(dev, genpd))
749 pm_runtime_resume(dev);
751 mutex_lock(&genpd->lock);
753 if (genpd->prepared_count++ == 0) {
754 genpd->suspended_count = 0;
755 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
758 mutex_unlock(&genpd->lock);
760 if (genpd->suspend_power_off) {
761 pm_runtime_put_noidle(dev);
762 return 0;
766 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
767 * so pm_genpd_poweron() will return immediately, but if the device
768 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
769 * to make it operational.
771 pm_runtime_resume(dev);
772 __pm_runtime_disable(dev, false);
774 ret = pm_generic_prepare(dev);
775 if (ret) {
776 mutex_lock(&genpd->lock);
778 if (--genpd->prepared_count == 0)
779 genpd->suspend_power_off = false;
781 mutex_unlock(&genpd->lock);
782 pm_runtime_enable(dev);
785 pm_runtime_put(dev);
786 return ret;
790 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
791 * @dev: Device to suspend.
793 * Suspend a device under the assumption that its pm_domain field points to the
794 * domain member of an object of type struct generic_pm_domain representing
795 * a PM domain consisting of I/O devices.
797 static int pm_genpd_suspend(struct device *dev)
799 struct generic_pm_domain *genpd;
801 dev_dbg(dev, "%s()\n", __func__);
803 genpd = dev_to_genpd(dev);
804 if (IS_ERR(genpd))
805 return -EINVAL;
807 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
811 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
812 * @dev: Device to suspend.
814 * Carry out a late suspend of a device under the assumption that its
815 * pm_domain field points to the domain member of an object of type
816 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
818 static int pm_genpd_suspend_late(struct device *dev)
820 struct generic_pm_domain *genpd;
822 dev_dbg(dev, "%s()\n", __func__);
824 genpd = dev_to_genpd(dev);
825 if (IS_ERR(genpd))
826 return -EINVAL;
828 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
832 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
833 * @dev: Device to suspend.
835 * Stop the device and remove power from the domain if all devices in it have
836 * been stopped.
838 static int pm_genpd_suspend_noirq(struct device *dev)
840 struct generic_pm_domain *genpd;
842 dev_dbg(dev, "%s()\n", __func__);
844 genpd = dev_to_genpd(dev);
845 if (IS_ERR(genpd))
846 return -EINVAL;
848 if (genpd->suspend_power_off
849 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
850 return 0;
852 genpd_stop_dev(genpd, dev);
855 * Since all of the "noirq" callbacks are executed sequentially, it is
856 * guaranteed that this function will never run twice in parallel for
857 * the same PM domain, so it is not necessary to use locking here.
859 genpd->suspended_count++;
860 pm_genpd_sync_poweroff(genpd, true);
862 return 0;
866 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
867 * @dev: Device to resume.
869 * Restore power to the device's PM domain, if necessary, and start the device.
871 static int pm_genpd_resume_noirq(struct device *dev)
873 struct generic_pm_domain *genpd;
875 dev_dbg(dev, "%s()\n", __func__);
877 genpd = dev_to_genpd(dev);
878 if (IS_ERR(genpd))
879 return -EINVAL;
881 if (genpd->suspend_power_off
882 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
883 return 0;
886 * Since all of the "noirq" callbacks are executed sequentially, it is
887 * guaranteed that this function will never run twice in parallel for
888 * the same PM domain, so it is not necessary to use locking here.
890 pm_genpd_sync_poweron(genpd, true);
891 genpd->suspended_count--;
893 return genpd_start_dev(genpd, dev, true);
897 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
898 * @dev: Device to resume.
900 * Carry out an early resume of a device under the assumption that its
901 * pm_domain field points to the domain member of an object of type
902 * struct generic_pm_domain representing a power domain consisting of I/O
903 * devices.
905 static int pm_genpd_resume_early(struct device *dev)
907 struct generic_pm_domain *genpd;
909 dev_dbg(dev, "%s()\n", __func__);
911 genpd = dev_to_genpd(dev);
912 if (IS_ERR(genpd))
913 return -EINVAL;
915 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
919 * pm_genpd_resume - Resume of device in an I/O PM domain.
920 * @dev: Device to resume.
922 * Resume a device under the assumption that its pm_domain field points to the
923 * domain member of an object of type struct generic_pm_domain representing
924 * a power domain consisting of I/O devices.
926 static int pm_genpd_resume(struct device *dev)
928 struct generic_pm_domain *genpd;
930 dev_dbg(dev, "%s()\n", __func__);
932 genpd = dev_to_genpd(dev);
933 if (IS_ERR(genpd))
934 return -EINVAL;
936 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
940 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
941 * @dev: Device to freeze.
943 * Freeze a device under the assumption that its pm_domain field points to the
944 * domain member of an object of type struct generic_pm_domain representing
945 * a power domain consisting of I/O devices.
947 static int pm_genpd_freeze(struct device *dev)
949 struct generic_pm_domain *genpd;
951 dev_dbg(dev, "%s()\n", __func__);
953 genpd = dev_to_genpd(dev);
954 if (IS_ERR(genpd))
955 return -EINVAL;
957 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
961 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
962 * @dev: Device to freeze.
964 * Carry out a late freeze of a device under the assumption that its
965 * pm_domain field points to the domain member of an object of type
966 * struct generic_pm_domain representing a power domain consisting of I/O
967 * devices.
969 static int pm_genpd_freeze_late(struct device *dev)
971 struct generic_pm_domain *genpd;
973 dev_dbg(dev, "%s()\n", __func__);
975 genpd = dev_to_genpd(dev);
976 if (IS_ERR(genpd))
977 return -EINVAL;
979 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
983 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
984 * @dev: Device to freeze.
986 * Carry out a late freeze of a device under the assumption that its
987 * pm_domain field points to the domain member of an object of type
988 * struct generic_pm_domain representing a power domain consisting of I/O
989 * devices.
991 static int pm_genpd_freeze_noirq(struct device *dev)
993 struct generic_pm_domain *genpd;
995 dev_dbg(dev, "%s()\n", __func__);
997 genpd = dev_to_genpd(dev);
998 if (IS_ERR(genpd))
999 return -EINVAL;
1001 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1005 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1006 * @dev: Device to thaw.
1008 * Start the device, unless power has been removed from the domain already
1009 * before the system transition.
1011 static int pm_genpd_thaw_noirq(struct device *dev)
1013 struct generic_pm_domain *genpd;
1015 dev_dbg(dev, "%s()\n", __func__);
1017 genpd = dev_to_genpd(dev);
1018 if (IS_ERR(genpd))
1019 return -EINVAL;
1021 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev, true);
1025 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1026 * @dev: Device to thaw.
1028 * Carry out an early thaw of a device under the assumption that its
1029 * pm_domain field points to the domain member of an object of type
1030 * struct generic_pm_domain representing a power domain consisting of I/O
1031 * devices.
1033 static int pm_genpd_thaw_early(struct device *dev)
1035 struct generic_pm_domain *genpd;
1037 dev_dbg(dev, "%s()\n", __func__);
1039 genpd = dev_to_genpd(dev);
1040 if (IS_ERR(genpd))
1041 return -EINVAL;
1043 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
1047 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1048 * @dev: Device to thaw.
1050 * Thaw a device under the assumption that its pm_domain field points to the
1051 * domain member of an object of type struct generic_pm_domain representing
1052 * a power domain consisting of I/O devices.
1054 static int pm_genpd_thaw(struct device *dev)
1056 struct generic_pm_domain *genpd;
1058 dev_dbg(dev, "%s()\n", __func__);
1060 genpd = dev_to_genpd(dev);
1061 if (IS_ERR(genpd))
1062 return -EINVAL;
1064 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1068 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1069 * @dev: Device to resume.
1071 * Make sure the domain will be in the same power state as before the
1072 * hibernation the system is resuming from and start the device if necessary.
1074 static int pm_genpd_restore_noirq(struct device *dev)
1076 struct generic_pm_domain *genpd;
1078 dev_dbg(dev, "%s()\n", __func__);
1080 genpd = dev_to_genpd(dev);
1081 if (IS_ERR(genpd))
1082 return -EINVAL;
1085 * Since all of the "noirq" callbacks are executed sequentially, it is
1086 * guaranteed that this function will never run twice in parallel for
1087 * the same PM domain, so it is not necessary to use locking here.
1089 * At this point suspended_count == 0 means we are being run for the
1090 * first time for the given domain in the present cycle.
1092 if (genpd->suspended_count++ == 0) {
1094 * The boot kernel might put the domain into arbitrary state,
1095 * so make it appear as powered off to pm_genpd_sync_poweron(),
1096 * so that it tries to power it on in case it was really off.
1098 genpd->status = GPD_STATE_POWER_OFF;
1099 if (genpd->suspend_power_off) {
1101 * If the domain was off before the hibernation, make
1102 * sure it will be off going forward.
1104 genpd_power_off(genpd, true);
1106 return 0;
1110 if (genpd->suspend_power_off)
1111 return 0;
1113 pm_genpd_sync_poweron(genpd, true);
1115 return genpd_start_dev(genpd, dev, true);
1119 * pm_genpd_complete - Complete power transition of a device in a power domain.
1120 * @dev: Device to complete the transition of.
1122 * Complete a power transition of a device (during a system-wide power
1123 * transition) under the assumption that its pm_domain field points to the
1124 * domain member of an object of type struct generic_pm_domain representing
1125 * a power domain consisting of I/O devices.
1127 static void pm_genpd_complete(struct device *dev)
1129 struct generic_pm_domain *genpd;
1130 bool run_complete;
1132 dev_dbg(dev, "%s()\n", __func__);
1134 genpd = dev_to_genpd(dev);
1135 if (IS_ERR(genpd))
1136 return;
1138 mutex_lock(&genpd->lock);
1140 run_complete = !genpd->suspend_power_off;
1141 if (--genpd->prepared_count == 0)
1142 genpd->suspend_power_off = false;
1144 mutex_unlock(&genpd->lock);
1146 if (run_complete) {
1147 pm_generic_complete(dev);
1148 pm_runtime_set_active(dev);
1149 pm_runtime_enable(dev);
1150 pm_request_idle(dev);
1155 * genpd_syscore_switch - Switch power during system core suspend or resume.
1156 * @dev: Device that normally is marked as "always on" to switch power for.
1158 * This routine may only be called during the system core (syscore) suspend or
1159 * resume phase for devices whose "always on" flags are set.
1161 static void genpd_syscore_switch(struct device *dev, bool suspend)
1163 struct generic_pm_domain *genpd;
1165 genpd = dev_to_genpd(dev);
1166 if (!pm_genpd_present(genpd))
1167 return;
1169 if (suspend) {
1170 genpd->suspended_count++;
1171 pm_genpd_sync_poweroff(genpd, false);
1172 } else {
1173 pm_genpd_sync_poweron(genpd, false);
1174 genpd->suspended_count--;
1178 void pm_genpd_syscore_poweroff(struct device *dev)
1180 genpd_syscore_switch(dev, true);
1182 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1184 void pm_genpd_syscore_poweron(struct device *dev)
1186 genpd_syscore_switch(dev, false);
1188 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1190 #else /* !CONFIG_PM_SLEEP */
1192 #define pm_genpd_prepare NULL
1193 #define pm_genpd_suspend NULL
1194 #define pm_genpd_suspend_late NULL
1195 #define pm_genpd_suspend_noirq NULL
1196 #define pm_genpd_resume_early NULL
1197 #define pm_genpd_resume_noirq NULL
1198 #define pm_genpd_resume NULL
1199 #define pm_genpd_freeze NULL
1200 #define pm_genpd_freeze_late NULL
1201 #define pm_genpd_freeze_noirq NULL
1202 #define pm_genpd_thaw_early NULL
1203 #define pm_genpd_thaw_noirq NULL
1204 #define pm_genpd_thaw NULL
1205 #define pm_genpd_restore_noirq NULL
1206 #define pm_genpd_complete NULL
1208 #endif /* CONFIG_PM_SLEEP */
1210 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1211 struct generic_pm_domain *genpd,
1212 struct gpd_timing_data *td)
1214 struct generic_pm_domain_data *gpd_data;
1215 int ret;
1217 ret = dev_pm_get_subsys_data(dev);
1218 if (ret)
1219 return ERR_PTR(ret);
1221 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1222 if (!gpd_data) {
1223 ret = -ENOMEM;
1224 goto err_put;
1227 if (td)
1228 gpd_data->td = *td;
1230 gpd_data->base.dev = dev;
1231 gpd_data->td.constraint_changed = true;
1232 gpd_data->td.effective_constraint_ns = -1;
1233 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1235 spin_lock_irq(&dev->power.lock);
1237 if (dev->power.subsys_data->domain_data) {
1238 ret = -EINVAL;
1239 goto err_free;
1242 dev->power.subsys_data->domain_data = &gpd_data->base;
1243 dev->pm_domain = &genpd->domain;
1245 spin_unlock_irq(&dev->power.lock);
1247 return gpd_data;
1249 err_free:
1250 spin_unlock_irq(&dev->power.lock);
1251 kfree(gpd_data);
1252 err_put:
1253 dev_pm_put_subsys_data(dev);
1254 return ERR_PTR(ret);
1257 static void genpd_free_dev_data(struct device *dev,
1258 struct generic_pm_domain_data *gpd_data)
1260 spin_lock_irq(&dev->power.lock);
1262 dev->pm_domain = NULL;
1263 dev->power.subsys_data->domain_data = NULL;
1265 spin_unlock_irq(&dev->power.lock);
1267 kfree(gpd_data);
1268 dev_pm_put_subsys_data(dev);
1272 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1273 * @genpd: PM domain to add the device to.
1274 * @dev: Device to be added.
1275 * @td: Set of PM QoS timing parameters to attach to the device.
1277 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1278 struct gpd_timing_data *td)
1280 struct generic_pm_domain_data *gpd_data;
1281 int ret = 0;
1283 dev_dbg(dev, "%s()\n", __func__);
1285 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1286 return -EINVAL;
1288 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1289 if (IS_ERR(gpd_data))
1290 return PTR_ERR(gpd_data);
1292 mutex_lock(&genpd->lock);
1294 if (genpd->prepared_count > 0) {
1295 ret = -EAGAIN;
1296 goto out;
1299 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1300 if (ret)
1301 goto out;
1303 genpd->device_count++;
1304 genpd->max_off_time_changed = true;
1306 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1308 out:
1309 mutex_unlock(&genpd->lock);
1311 if (ret)
1312 genpd_free_dev_data(dev, gpd_data);
1313 else
1314 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1316 return ret;
1320 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1321 * @domain_name: Name of the PM domain to add the device to.
1322 * @dev: Device to be added.
1323 * @td: Set of PM QoS timing parameters to attach to the device.
1325 int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1326 struct gpd_timing_data *td)
1328 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1332 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1333 * @genpd: PM domain to remove the device from.
1334 * @dev: Device to be removed.
1336 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1337 struct device *dev)
1339 struct generic_pm_domain_data *gpd_data;
1340 struct pm_domain_data *pdd;
1341 int ret = 0;
1343 dev_dbg(dev, "%s()\n", __func__);
1345 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1346 return -EINVAL;
1348 /* The above validation also means we have existing domain_data. */
1349 pdd = dev->power.subsys_data->domain_data;
1350 gpd_data = to_gpd_data(pdd);
1351 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1353 mutex_lock(&genpd->lock);
1355 if (genpd->prepared_count > 0) {
1356 ret = -EAGAIN;
1357 goto out;
1360 genpd->device_count--;
1361 genpd->max_off_time_changed = true;
1363 if (genpd->detach_dev)
1364 genpd->detach_dev(genpd, dev);
1366 list_del_init(&pdd->list_node);
1368 mutex_unlock(&genpd->lock);
1370 genpd_free_dev_data(dev, gpd_data);
1372 return 0;
1374 out:
1375 mutex_unlock(&genpd->lock);
1376 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1378 return ret;
1382 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1383 * @genpd: Master PM domain to add the subdomain to.
1384 * @subdomain: Subdomain to be added.
1386 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1387 struct generic_pm_domain *subdomain)
1389 struct gpd_link *link;
1390 int ret = 0;
1392 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1393 || genpd == subdomain)
1394 return -EINVAL;
1396 mutex_lock(&genpd->lock);
1397 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1399 if (genpd->status == GPD_STATE_POWER_OFF
1400 && subdomain->status != GPD_STATE_POWER_OFF) {
1401 ret = -EINVAL;
1402 goto out;
1405 list_for_each_entry(link, &genpd->master_links, master_node) {
1406 if (link->slave == subdomain && link->master == genpd) {
1407 ret = -EINVAL;
1408 goto out;
1412 link = kzalloc(sizeof(*link), GFP_KERNEL);
1413 if (!link) {
1414 ret = -ENOMEM;
1415 goto out;
1417 link->master = genpd;
1418 list_add_tail(&link->master_node, &genpd->master_links);
1419 link->slave = subdomain;
1420 list_add_tail(&link->slave_node, &subdomain->slave_links);
1421 if (subdomain->status != GPD_STATE_POWER_OFF)
1422 genpd_sd_counter_inc(genpd);
1424 out:
1425 mutex_unlock(&subdomain->lock);
1426 mutex_unlock(&genpd->lock);
1428 return ret;
1432 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1433 * @master_name: Name of the master PM domain to add the subdomain to.
1434 * @subdomain_name: Name of the subdomain to be added.
1436 int pm_genpd_add_subdomain_names(const char *master_name,
1437 const char *subdomain_name)
1439 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1441 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1442 return -EINVAL;
1444 mutex_lock(&gpd_list_lock);
1445 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1446 if (!master && !strcmp(gpd->name, master_name))
1447 master = gpd;
1449 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1450 subdomain = gpd;
1452 if (master && subdomain)
1453 break;
1455 mutex_unlock(&gpd_list_lock);
1457 return pm_genpd_add_subdomain(master, subdomain);
1461 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1462 * @genpd: Master PM domain to remove the subdomain from.
1463 * @subdomain: Subdomain to be removed.
1465 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1466 struct generic_pm_domain *subdomain)
1468 struct gpd_link *link;
1469 int ret = -EINVAL;
1471 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1472 return -EINVAL;
1474 mutex_lock(&genpd->lock);
1476 list_for_each_entry(link, &genpd->master_links, master_node) {
1477 if (link->slave != subdomain)
1478 continue;
1480 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1482 list_del(&link->master_node);
1483 list_del(&link->slave_node);
1484 kfree(link);
1485 if (subdomain->status != GPD_STATE_POWER_OFF)
1486 genpd_sd_counter_dec(genpd);
1488 mutex_unlock(&subdomain->lock);
1490 ret = 0;
1491 break;
1494 mutex_unlock(&genpd->lock);
1496 return ret;
1500 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1501 * @genpd: PM domain to be connected with cpuidle.
1502 * @state: cpuidle state this domain can disable/enable.
1504 * Make a PM domain behave as though it contained a CPU core, that is, instead
1505 * of calling its power down routine it will enable the given cpuidle state so
1506 * that the cpuidle subsystem can power it down (if possible and desirable).
1508 int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1510 struct cpuidle_driver *cpuidle_drv;
1511 struct gpd_cpuidle_data *cpuidle_data;
1512 struct cpuidle_state *idle_state;
1513 int ret = 0;
1515 if (IS_ERR_OR_NULL(genpd) || state < 0)
1516 return -EINVAL;
1518 mutex_lock(&genpd->lock);
1520 if (genpd->cpuidle_data) {
1521 ret = -EEXIST;
1522 goto out;
1524 cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
1525 if (!cpuidle_data) {
1526 ret = -ENOMEM;
1527 goto out;
1529 cpuidle_drv = cpuidle_driver_ref();
1530 if (!cpuidle_drv) {
1531 ret = -ENODEV;
1532 goto err_drv;
1534 if (cpuidle_drv->state_count <= state) {
1535 ret = -EINVAL;
1536 goto err;
1538 idle_state = &cpuidle_drv->states[state];
1539 if (!idle_state->disabled) {
1540 ret = -EAGAIN;
1541 goto err;
1543 cpuidle_data->idle_state = idle_state;
1544 cpuidle_data->saved_exit_latency = idle_state->exit_latency;
1545 genpd->cpuidle_data = cpuidle_data;
1546 genpd_recalc_cpu_exit_latency(genpd);
1548 out:
1549 mutex_unlock(&genpd->lock);
1550 return ret;
1552 err:
1553 cpuidle_driver_unref();
1555 err_drv:
1556 kfree(cpuidle_data);
1557 goto out;
1561 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1562 * @name: Name of the domain to connect to cpuidle.
1563 * @state: cpuidle state this domain can manipulate.
1565 int pm_genpd_name_attach_cpuidle(const char *name, int state)
1567 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1571 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1572 * @genpd: PM domain to remove the cpuidle connection from.
1574 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1575 * given PM domain.
1577 int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1579 struct gpd_cpuidle_data *cpuidle_data;
1580 struct cpuidle_state *idle_state;
1581 int ret = 0;
1583 if (IS_ERR_OR_NULL(genpd))
1584 return -EINVAL;
1586 mutex_lock(&genpd->lock);
1588 cpuidle_data = genpd->cpuidle_data;
1589 if (!cpuidle_data) {
1590 ret = -ENODEV;
1591 goto out;
1593 idle_state = cpuidle_data->idle_state;
1594 if (!idle_state->disabled) {
1595 ret = -EAGAIN;
1596 goto out;
1598 idle_state->exit_latency = cpuidle_data->saved_exit_latency;
1599 cpuidle_driver_unref();
1600 genpd->cpuidle_data = NULL;
1601 kfree(cpuidle_data);
1603 out:
1604 mutex_unlock(&genpd->lock);
1605 return ret;
1609 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1610 * @name: Name of the domain to disconnect cpuidle from.
1612 int pm_genpd_name_detach_cpuidle(const char *name)
1614 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1617 /* Default device callbacks for generic PM domains. */
1620 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1621 * @dev: Device to handle.
1623 static int pm_genpd_default_save_state(struct device *dev)
1625 int (*cb)(struct device *__dev);
1627 if (dev->type && dev->type->pm)
1628 cb = dev->type->pm->runtime_suspend;
1629 else if (dev->class && dev->class->pm)
1630 cb = dev->class->pm->runtime_suspend;
1631 else if (dev->bus && dev->bus->pm)
1632 cb = dev->bus->pm->runtime_suspend;
1633 else
1634 cb = NULL;
1636 if (!cb && dev->driver && dev->driver->pm)
1637 cb = dev->driver->pm->runtime_suspend;
1639 return cb ? cb(dev) : 0;
1643 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1644 * @dev: Device to handle.
1646 static int pm_genpd_default_restore_state(struct device *dev)
1648 int (*cb)(struct device *__dev);
1650 if (dev->type && dev->type->pm)
1651 cb = dev->type->pm->runtime_resume;
1652 else if (dev->class && dev->class->pm)
1653 cb = dev->class->pm->runtime_resume;
1654 else if (dev->bus && dev->bus->pm)
1655 cb = dev->bus->pm->runtime_resume;
1656 else
1657 cb = NULL;
1659 if (!cb && dev->driver && dev->driver->pm)
1660 cb = dev->driver->pm->runtime_resume;
1662 return cb ? cb(dev) : 0;
1666 * pm_genpd_init - Initialize a generic I/O PM domain object.
1667 * @genpd: PM domain object to initialize.
1668 * @gov: PM domain governor to associate with the domain (may be NULL).
1669 * @is_off: Initial value of the domain's power_is_off field.
1671 void pm_genpd_init(struct generic_pm_domain *genpd,
1672 struct dev_power_governor *gov, bool is_off)
1674 if (IS_ERR_OR_NULL(genpd))
1675 return;
1677 INIT_LIST_HEAD(&genpd->master_links);
1678 INIT_LIST_HEAD(&genpd->slave_links);
1679 INIT_LIST_HEAD(&genpd->dev_list);
1680 mutex_init(&genpd->lock);
1681 genpd->gov = gov;
1682 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1683 genpd->in_progress = 0;
1684 atomic_set(&genpd->sd_count, 0);
1685 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1686 genpd->device_count = 0;
1687 genpd->max_off_time_ns = -1;
1688 genpd->max_off_time_changed = true;
1689 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1690 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1691 genpd->domain.ops.prepare = pm_genpd_prepare;
1692 genpd->domain.ops.suspend = pm_genpd_suspend;
1693 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1694 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1695 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1696 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1697 genpd->domain.ops.resume = pm_genpd_resume;
1698 genpd->domain.ops.freeze = pm_genpd_freeze;
1699 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1700 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1701 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1702 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1703 genpd->domain.ops.thaw = pm_genpd_thaw;
1704 genpd->domain.ops.poweroff = pm_genpd_suspend;
1705 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1706 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1707 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1708 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1709 genpd->domain.ops.restore = pm_genpd_resume;
1710 genpd->domain.ops.complete = pm_genpd_complete;
1711 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1712 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1714 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1715 genpd->dev_ops.stop = pm_clk_suspend;
1716 genpd->dev_ops.start = pm_clk_resume;
1719 mutex_lock(&gpd_list_lock);
1720 list_add(&genpd->gpd_list_node, &gpd_list);
1721 mutex_unlock(&gpd_list_lock);
1723 EXPORT_SYMBOL_GPL(pm_genpd_init);
1725 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1727 * Device Tree based PM domain providers.
1729 * The code below implements generic device tree based PM domain providers that
1730 * bind device tree nodes with generic PM domains registered in the system.
1732 * Any driver that registers generic PM domains and needs to support binding of
1733 * devices to these domains is supposed to register a PM domain provider, which
1734 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1736 * Two simple mapping functions have been provided for convenience:
1737 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1738 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1739 * index.
1743 * struct of_genpd_provider - PM domain provider registration structure
1744 * @link: Entry in global list of PM domain providers
1745 * @node: Pointer to device tree node of PM domain provider
1746 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1747 * into a PM domain.
1748 * @data: context pointer to be passed into @xlate callback
1750 struct of_genpd_provider {
1751 struct list_head link;
1752 struct device_node *node;
1753 genpd_xlate_t xlate;
1754 void *data;
1757 /* List of registered PM domain providers. */
1758 static LIST_HEAD(of_genpd_providers);
1759 /* Mutex to protect the list above. */
1760 static DEFINE_MUTEX(of_genpd_mutex);
1763 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1764 * @genpdspec: OF phandle args to map into a PM domain
1765 * @data: xlate function private data - pointer to struct generic_pm_domain
1767 * This is a generic xlate function that can be used to model PM domains that
1768 * have their own device tree nodes. The private data of xlate function needs
1769 * to be a valid pointer to struct generic_pm_domain.
1771 struct generic_pm_domain *__of_genpd_xlate_simple(
1772 struct of_phandle_args *genpdspec,
1773 void *data)
1775 if (genpdspec->args_count != 0)
1776 return ERR_PTR(-EINVAL);
1777 return data;
1779 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1782 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1783 * @genpdspec: OF phandle args to map into a PM domain
1784 * @data: xlate function private data - pointer to struct genpd_onecell_data
1786 * This is a generic xlate function that can be used to model simple PM domain
1787 * controllers that have one device tree node and provide multiple PM domains.
1788 * A single cell is used as an index into an array of PM domains specified in
1789 * the genpd_onecell_data struct when registering the provider.
1791 struct generic_pm_domain *__of_genpd_xlate_onecell(
1792 struct of_phandle_args *genpdspec,
1793 void *data)
1795 struct genpd_onecell_data *genpd_data = data;
1796 unsigned int idx = genpdspec->args[0];
1798 if (genpdspec->args_count != 1)
1799 return ERR_PTR(-EINVAL);
1801 if (idx >= genpd_data->num_domains) {
1802 pr_err("%s: invalid domain index %u\n", __func__, idx);
1803 return ERR_PTR(-EINVAL);
1806 if (!genpd_data->domains[idx])
1807 return ERR_PTR(-ENOENT);
1809 return genpd_data->domains[idx];
1811 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1814 * __of_genpd_add_provider() - Register a PM domain provider for a node
1815 * @np: Device node pointer associated with the PM domain provider.
1816 * @xlate: Callback for decoding PM domain from phandle arguments.
1817 * @data: Context pointer for @xlate callback.
1819 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1820 void *data)
1822 struct of_genpd_provider *cp;
1824 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1825 if (!cp)
1826 return -ENOMEM;
1828 cp->node = of_node_get(np);
1829 cp->data = data;
1830 cp->xlate = xlate;
1832 mutex_lock(&of_genpd_mutex);
1833 list_add(&cp->link, &of_genpd_providers);
1834 mutex_unlock(&of_genpd_mutex);
1835 pr_debug("Added domain provider from %s\n", np->full_name);
1837 return 0;
1839 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1842 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1843 * @np: Device node pointer associated with the PM domain provider
1845 void of_genpd_del_provider(struct device_node *np)
1847 struct of_genpd_provider *cp;
1849 mutex_lock(&of_genpd_mutex);
1850 list_for_each_entry(cp, &of_genpd_providers, link) {
1851 if (cp->node == np) {
1852 list_del(&cp->link);
1853 of_node_put(cp->node);
1854 kfree(cp);
1855 break;
1858 mutex_unlock(&of_genpd_mutex);
1860 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1863 * of_genpd_get_from_provider() - Look-up PM domain
1864 * @genpdspec: OF phandle args to use for look-up
1866 * Looks for a PM domain provider under the node specified by @genpdspec and if
1867 * found, uses xlate function of the provider to map phandle args to a PM
1868 * domain.
1870 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1871 * on failure.
1873 struct generic_pm_domain *of_genpd_get_from_provider(
1874 struct of_phandle_args *genpdspec)
1876 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1877 struct of_genpd_provider *provider;
1879 mutex_lock(&of_genpd_mutex);
1881 /* Check if we have such a provider in our array */
1882 list_for_each_entry(provider, &of_genpd_providers, link) {
1883 if (provider->node == genpdspec->np)
1884 genpd = provider->xlate(genpdspec, provider->data);
1885 if (!IS_ERR(genpd))
1886 break;
1889 mutex_unlock(&of_genpd_mutex);
1891 return genpd;
1893 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1896 * genpd_dev_pm_detach - Detach a device from its PM domain.
1897 * @dev: Device to detach.
1898 * @power_off: Currently not used
1900 * Try to locate a corresponding generic PM domain, which the device was
1901 * attached to previously. If such is found, the device is detached from it.
1903 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1905 struct generic_pm_domain *pd;
1906 unsigned int i;
1907 int ret = 0;
1909 pd = pm_genpd_lookup_dev(dev);
1910 if (!pd)
1911 return;
1913 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1915 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1916 ret = pm_genpd_remove_device(pd, dev);
1917 if (ret != -EAGAIN)
1918 break;
1920 mdelay(i);
1921 cond_resched();
1924 if (ret < 0) {
1925 dev_err(dev, "failed to remove from PM domain %s: %d",
1926 pd->name, ret);
1927 return;
1930 /* Check if PM domain can be powered off after removing this device. */
1931 genpd_queue_power_off_work(pd);
1934 static void genpd_dev_pm_sync(struct device *dev)
1936 struct generic_pm_domain *pd;
1938 pd = dev_to_genpd(dev);
1939 if (IS_ERR(pd))
1940 return;
1942 genpd_queue_power_off_work(pd);
1946 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1947 * @dev: Device to attach.
1949 * Parse device's OF node to find a PM domain specifier. If such is found,
1950 * attaches the device to retrieved pm_domain ops.
1952 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1953 * backwards compatibility with existing DTBs.
1955 * Returns 0 on successfully attached PM domain or negative error code. Note
1956 * that if a power-domain exists for the device, but it cannot be found or
1957 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1958 * probed and to re-try again later.
1960 int genpd_dev_pm_attach(struct device *dev)
1962 struct of_phandle_args pd_args;
1963 struct generic_pm_domain *pd;
1964 unsigned int i;
1965 int ret;
1967 if (!dev->of_node)
1968 return -ENODEV;
1970 if (dev->pm_domain)
1971 return -EEXIST;
1973 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1974 "#power-domain-cells", 0, &pd_args);
1975 if (ret < 0) {
1976 if (ret != -ENOENT)
1977 return ret;
1980 * Try legacy Samsung-specific bindings
1981 * (for backwards compatibility of DT ABI)
1983 pd_args.args_count = 0;
1984 pd_args.np = of_parse_phandle(dev->of_node,
1985 "samsung,power-domain", 0);
1986 if (!pd_args.np)
1987 return -ENOENT;
1990 pd = of_genpd_get_from_provider(&pd_args);
1991 if (IS_ERR(pd)) {
1992 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1993 __func__, PTR_ERR(pd));
1994 of_node_put(dev->of_node);
1995 return -EPROBE_DEFER;
1998 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2000 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2001 ret = pm_genpd_add_device(pd, dev);
2002 if (ret != -EAGAIN)
2003 break;
2005 mdelay(i);
2006 cond_resched();
2009 if (ret < 0) {
2010 dev_err(dev, "failed to add to PM domain %s: %d",
2011 pd->name, ret);
2012 of_node_put(dev->of_node);
2013 goto out;
2016 dev->pm_domain->detach = genpd_dev_pm_detach;
2017 dev->pm_domain->sync = genpd_dev_pm_sync;
2018 ret = pm_genpd_poweron(pd);
2020 out:
2021 return ret ? -EPROBE_DEFER : 0;
2023 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2024 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2027 /*** debugfs support ***/
2029 #ifdef CONFIG_PM_ADVANCED_DEBUG
2030 #include <linux/pm.h>
2031 #include <linux/device.h>
2032 #include <linux/debugfs.h>
2033 #include <linux/seq_file.h>
2034 #include <linux/init.h>
2035 #include <linux/kobject.h>
2036 static struct dentry *pm_genpd_debugfs_dir;
2039 * TODO: This function is a slightly modified version of rtpm_status_show
2040 * from sysfs.c, so generalize it.
2042 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2044 static const char * const status_lookup[] = {
2045 [RPM_ACTIVE] = "active",
2046 [RPM_RESUMING] = "resuming",
2047 [RPM_SUSPENDED] = "suspended",
2048 [RPM_SUSPENDING] = "suspending"
2050 const char *p = "";
2052 if (dev->power.runtime_error)
2053 p = "error";
2054 else if (dev->power.disable_depth)
2055 p = "unsupported";
2056 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2057 p = status_lookup[dev->power.runtime_status];
2058 else
2059 WARN_ON(1);
2061 seq_puts(s, p);
2064 static int pm_genpd_summary_one(struct seq_file *s,
2065 struct generic_pm_domain *genpd)
2067 static const char * const status_lookup[] = {
2068 [GPD_STATE_ACTIVE] = "on",
2069 [GPD_STATE_POWER_OFF] = "off"
2071 struct pm_domain_data *pm_data;
2072 const char *kobj_path;
2073 struct gpd_link *link;
2074 int ret;
2076 ret = mutex_lock_interruptible(&genpd->lock);
2077 if (ret)
2078 return -ERESTARTSYS;
2080 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2081 goto exit;
2082 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
2085 * Modifications on the list require holding locks on both
2086 * master and slave, so we are safe.
2087 * Also genpd->name is immutable.
2089 list_for_each_entry(link, &genpd->master_links, master_node) {
2090 seq_printf(s, "%s", link->slave->name);
2091 if (!list_is_last(&link->master_node, &genpd->master_links))
2092 seq_puts(s, ", ");
2095 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2096 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
2097 if (kobj_path == NULL)
2098 continue;
2100 seq_printf(s, "\n %-50s ", kobj_path);
2101 rtpm_status_str(s, pm_data->dev);
2102 kfree(kobj_path);
2105 seq_puts(s, "\n");
2106 exit:
2107 mutex_unlock(&genpd->lock);
2109 return 0;
2112 static int pm_genpd_summary_show(struct seq_file *s, void *data)
2114 struct generic_pm_domain *genpd;
2115 int ret = 0;
2117 seq_puts(s, "domain status slaves\n");
2118 seq_puts(s, " /device runtime status\n");
2119 seq_puts(s, "----------------------------------------------------------------------\n");
2121 ret = mutex_lock_interruptible(&gpd_list_lock);
2122 if (ret)
2123 return -ERESTARTSYS;
2125 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2126 ret = pm_genpd_summary_one(s, genpd);
2127 if (ret)
2128 break;
2130 mutex_unlock(&gpd_list_lock);
2132 return ret;
2135 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2137 return single_open(file, pm_genpd_summary_show, NULL);
2140 static const struct file_operations pm_genpd_summary_fops = {
2141 .open = pm_genpd_summary_open,
2142 .read = seq_read,
2143 .llseek = seq_lseek,
2144 .release = single_release,
2147 static int __init pm_genpd_debug_init(void)
2149 struct dentry *d;
2151 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2153 if (!pm_genpd_debugfs_dir)
2154 return -ENOMEM;
2156 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2157 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2158 if (!d)
2159 return -ENOMEM;
2161 return 0;
2163 late_initcall(pm_genpd_debug_init);
2165 static void __exit pm_genpd_debug_exit(void)
2167 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2169 __exitcall(pm_genpd_debug_exit);
2170 #endif /* CONFIG_PM_ADVANCED_DEBUG */