Loading...
1/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
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>
22
23#include "power.h"
24
25#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
27#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
35 } \
36 __ret; \
37})
38
39static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
42/*
43 * Get the generic PM domain for a particular struct device.
44 * This validates the struct device pointer, the PM domain pointer,
45 * and checks that the PM domain pointer is a real generic PM domain.
46 * Any failure results in NULL being returned.
47 */
48struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
49{
50 struct generic_pm_domain *genpd = NULL, *gpd;
51
52 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 return NULL;
54
55 mutex_lock(&gpd_list_lock);
56 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
57 if (&gpd->domain == dev->pm_domain) {
58 genpd = gpd;
59 break;
60 }
61 }
62 mutex_unlock(&gpd_list_lock);
63
64 return genpd;
65}
66
67/*
68 * This should only be used where we are certain that the pm_domain
69 * attached to the device is a genpd domain.
70 */
71static struct generic_pm_domain *dev_to_genpd(struct device *dev)
72{
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
75
76 return pd_to_genpd(dev->pm_domain);
77}
78
79static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
80{
81 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
82}
83
84static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
85{
86 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
87}
88
89static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
90{
91 bool ret = false;
92
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
95
96 return ret;
97}
98
99static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100{
101 atomic_inc(&genpd->sd_count);
102 smp_mb__after_atomic();
103}
104
105static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
106{
107 unsigned int state_idx = genpd->state_idx;
108 ktime_t time_start;
109 s64 elapsed_ns;
110 int ret;
111
112 if (!genpd->power_on)
113 return 0;
114
115 if (!timed)
116 return genpd->power_on(genpd);
117
118 time_start = ktime_get();
119 ret = genpd->power_on(genpd);
120 if (ret)
121 return ret;
122
123 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
124 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
125 return ret;
126
127 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
128 genpd->max_off_time_changed = true;
129 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
130 genpd->name, "on", elapsed_ns);
131
132 return ret;
133}
134
135static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
136{
137 unsigned int state_idx = genpd->state_idx;
138 ktime_t time_start;
139 s64 elapsed_ns;
140 int ret;
141
142 if (!genpd->power_off)
143 return 0;
144
145 if (!timed)
146 return genpd->power_off(genpd);
147
148 time_start = ktime_get();
149 ret = genpd->power_off(genpd);
150 if (ret == -EBUSY)
151 return ret;
152
153 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
154 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
155 return ret;
156
157 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
158 genpd->max_off_time_changed = true;
159 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
160 genpd->name, "off", elapsed_ns);
161
162 return ret;
163}
164
165/**
166 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
167 * @genpd: PM domain to power off.
168 *
169 * Queue up the execution of genpd_poweroff() unless it's already been done
170 * before.
171 */
172static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
173{
174 queue_work(pm_wq, &genpd->power_off_work);
175}
176
177/**
178 * genpd_poweron - Restore power to a given PM domain and its masters.
179 * @genpd: PM domain to power up.
180 * @depth: nesting count for lockdep.
181 *
182 * Restore power to @genpd and all of its masters so that it is possible to
183 * resume a device belonging to it.
184 */
185static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
186{
187 struct gpd_link *link;
188 int ret = 0;
189
190 if (genpd->status == GPD_STATE_ACTIVE
191 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
192 return 0;
193
194 /*
195 * The list is guaranteed not to change while the loop below is being
196 * executed, unless one of the masters' .power_on() callbacks fiddles
197 * with it.
198 */
199 list_for_each_entry(link, &genpd->slave_links, slave_node) {
200 struct generic_pm_domain *master = link->master;
201
202 genpd_sd_counter_inc(master);
203
204 mutex_lock_nested(&master->lock, depth + 1);
205 ret = genpd_poweron(master, depth + 1);
206 mutex_unlock(&master->lock);
207
208 if (ret) {
209 genpd_sd_counter_dec(master);
210 goto err;
211 }
212 }
213
214 ret = genpd_power_on(genpd, true);
215 if (ret)
216 goto err;
217
218 genpd->status = GPD_STATE_ACTIVE;
219 return 0;
220
221 err:
222 list_for_each_entry_continue_reverse(link,
223 &genpd->slave_links,
224 slave_node) {
225 genpd_sd_counter_dec(link->master);
226 genpd_queue_power_off_work(link->master);
227 }
228
229 return ret;
230}
231
232static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
233{
234 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
235}
236
237static int genpd_restore_dev(struct generic_pm_domain *genpd,
238 struct device *dev)
239{
240 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
241}
242
243static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
244 unsigned long val, void *ptr)
245{
246 struct generic_pm_domain_data *gpd_data;
247 struct device *dev;
248
249 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
250 dev = gpd_data->base.dev;
251
252 for (;;) {
253 struct generic_pm_domain *genpd;
254 struct pm_domain_data *pdd;
255
256 spin_lock_irq(&dev->power.lock);
257
258 pdd = dev->power.subsys_data ?
259 dev->power.subsys_data->domain_data : NULL;
260 if (pdd && pdd->dev) {
261 to_gpd_data(pdd)->td.constraint_changed = true;
262 genpd = dev_to_genpd(dev);
263 } else {
264 genpd = ERR_PTR(-ENODATA);
265 }
266
267 spin_unlock_irq(&dev->power.lock);
268
269 if (!IS_ERR(genpd)) {
270 mutex_lock(&genpd->lock);
271 genpd->max_off_time_changed = true;
272 mutex_unlock(&genpd->lock);
273 }
274
275 dev = dev->parent;
276 if (!dev || dev->power.ignore_children)
277 break;
278 }
279
280 return NOTIFY_DONE;
281}
282
283/**
284 * genpd_poweroff - Remove power from a given PM domain.
285 * @genpd: PM domain to power down.
286 * @is_async: PM domain is powered down from a scheduled work
287 *
288 * If all of the @genpd's devices have been suspended and all of its subdomains
289 * have been powered down, remove power from @genpd.
290 */
291static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
292{
293 struct pm_domain_data *pdd;
294 struct gpd_link *link;
295 unsigned int not_suspended = 0;
296
297 /*
298 * Do not try to power off the domain in the following situations:
299 * (1) The domain is already in the "power off" state.
300 * (2) System suspend is in progress.
301 */
302 if (genpd->status == GPD_STATE_POWER_OFF
303 || genpd->prepared_count > 0)
304 return 0;
305
306 if (atomic_read(&genpd->sd_count) > 0)
307 return -EBUSY;
308
309 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
310 enum pm_qos_flags_status stat;
311
312 stat = dev_pm_qos_flags(pdd->dev,
313 PM_QOS_FLAG_NO_POWER_OFF
314 | PM_QOS_FLAG_REMOTE_WAKEUP);
315 if (stat > PM_QOS_FLAGS_NONE)
316 return -EBUSY;
317
318 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
319 not_suspended++;
320 }
321
322 if (not_suspended > 1 || (not_suspended == 1 && is_async))
323 return -EBUSY;
324
325 if (genpd->gov && genpd->gov->power_down_ok) {
326 if (!genpd->gov->power_down_ok(&genpd->domain))
327 return -EAGAIN;
328 }
329
330 if (genpd->power_off) {
331 int ret;
332
333 if (atomic_read(&genpd->sd_count) > 0)
334 return -EBUSY;
335
336 /*
337 * If sd_count > 0 at this point, one of the subdomains hasn't
338 * managed to call genpd_poweron() for the master yet after
339 * incrementing it. In that case genpd_poweron() will wait
340 * for us to drop the lock, so we can call .power_off() and let
341 * the genpd_poweron() restore power for us (this shouldn't
342 * happen very often).
343 */
344 ret = genpd_power_off(genpd, true);
345 if (ret)
346 return ret;
347 }
348
349 genpd->status = GPD_STATE_POWER_OFF;
350
351 list_for_each_entry(link, &genpd->slave_links, slave_node) {
352 genpd_sd_counter_dec(link->master);
353 genpd_queue_power_off_work(link->master);
354 }
355
356 return 0;
357}
358
359/**
360 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
361 * @work: Work structure used for scheduling the execution of this function.
362 */
363static void genpd_power_off_work_fn(struct work_struct *work)
364{
365 struct generic_pm_domain *genpd;
366
367 genpd = container_of(work, struct generic_pm_domain, power_off_work);
368
369 mutex_lock(&genpd->lock);
370 genpd_poweroff(genpd, true);
371 mutex_unlock(&genpd->lock);
372}
373
374/**
375 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
376 * @dev: Device to suspend.
377 *
378 * Carry out a runtime suspend of a device under the assumption that its
379 * pm_domain field points to the domain member of an object of type
380 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
381 */
382static int pm_genpd_runtime_suspend(struct device *dev)
383{
384 struct generic_pm_domain *genpd;
385 bool (*stop_ok)(struct device *__dev);
386 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
387 bool runtime_pm = pm_runtime_enabled(dev);
388 ktime_t time_start;
389 s64 elapsed_ns;
390 int ret;
391
392 dev_dbg(dev, "%s()\n", __func__);
393
394 genpd = dev_to_genpd(dev);
395 if (IS_ERR(genpd))
396 return -EINVAL;
397
398 /*
399 * A runtime PM centric subsystem/driver may re-use the runtime PM
400 * callbacks for other purposes than runtime PM. In those scenarios
401 * runtime PM is disabled. Under these circumstances, we shall skip
402 * validating/measuring the PM QoS latency.
403 */
404 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
405 if (runtime_pm && stop_ok && !stop_ok(dev))
406 return -EBUSY;
407
408 /* Measure suspend latency. */
409 if (runtime_pm)
410 time_start = ktime_get();
411
412 ret = genpd_save_dev(genpd, dev);
413 if (ret)
414 return ret;
415
416 ret = genpd_stop_dev(genpd, dev);
417 if (ret) {
418 genpd_restore_dev(genpd, dev);
419 return ret;
420 }
421
422 /* Update suspend latency value if the measured time exceeds it. */
423 if (runtime_pm) {
424 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
425 if (elapsed_ns > td->suspend_latency_ns) {
426 td->suspend_latency_ns = elapsed_ns;
427 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
428 elapsed_ns);
429 genpd->max_off_time_changed = true;
430 td->constraint_changed = true;
431 }
432 }
433
434 /*
435 * If power.irq_safe is set, this routine will be run with interrupts
436 * off, so it can't use mutexes.
437 */
438 if (dev->power.irq_safe)
439 return 0;
440
441 mutex_lock(&genpd->lock);
442 genpd_poweroff(genpd, false);
443 mutex_unlock(&genpd->lock);
444
445 return 0;
446}
447
448/**
449 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
450 * @dev: Device to resume.
451 *
452 * Carry out a runtime resume of a device under the assumption that its
453 * pm_domain field points to the domain member of an object of type
454 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
455 */
456static int pm_genpd_runtime_resume(struct device *dev)
457{
458 struct generic_pm_domain *genpd;
459 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
460 bool runtime_pm = pm_runtime_enabled(dev);
461 ktime_t time_start;
462 s64 elapsed_ns;
463 int ret;
464 bool timed = true;
465
466 dev_dbg(dev, "%s()\n", __func__);
467
468 genpd = dev_to_genpd(dev);
469 if (IS_ERR(genpd))
470 return -EINVAL;
471
472 /* If power.irq_safe, the PM domain is never powered off. */
473 if (dev->power.irq_safe) {
474 timed = false;
475 goto out;
476 }
477
478 mutex_lock(&genpd->lock);
479 ret = genpd_poweron(genpd, 0);
480 mutex_unlock(&genpd->lock);
481
482 if (ret)
483 return ret;
484
485 out:
486 /* Measure resume latency. */
487 if (timed && runtime_pm)
488 time_start = ktime_get();
489
490 ret = genpd_start_dev(genpd, dev);
491 if (ret)
492 goto err_poweroff;
493
494 ret = genpd_restore_dev(genpd, dev);
495 if (ret)
496 goto err_stop;
497
498 /* Update resume latency value if the measured time exceeds it. */
499 if (timed && runtime_pm) {
500 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
501 if (elapsed_ns > td->resume_latency_ns) {
502 td->resume_latency_ns = elapsed_ns;
503 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
504 elapsed_ns);
505 genpd->max_off_time_changed = true;
506 td->constraint_changed = true;
507 }
508 }
509
510 return 0;
511
512err_stop:
513 genpd_stop_dev(genpd, dev);
514err_poweroff:
515 if (!dev->power.irq_safe) {
516 mutex_lock(&genpd->lock);
517 genpd_poweroff(genpd, 0);
518 mutex_unlock(&genpd->lock);
519 }
520
521 return ret;
522}
523
524static bool pd_ignore_unused;
525static int __init pd_ignore_unused_setup(char *__unused)
526{
527 pd_ignore_unused = true;
528 return 1;
529}
530__setup("pd_ignore_unused", pd_ignore_unused_setup);
531
532/**
533 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
534 */
535static int __init genpd_poweroff_unused(void)
536{
537 struct generic_pm_domain *genpd;
538
539 if (pd_ignore_unused) {
540 pr_warn("genpd: Not disabling unused power domains\n");
541 return 0;
542 }
543
544 mutex_lock(&gpd_list_lock);
545
546 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
547 genpd_queue_power_off_work(genpd);
548
549 mutex_unlock(&gpd_list_lock);
550
551 return 0;
552}
553late_initcall(genpd_poweroff_unused);
554
555#ifdef CONFIG_PM_SLEEP
556
557/**
558 * pm_genpd_present - Check if the given PM domain has been initialized.
559 * @genpd: PM domain to check.
560 */
561static bool pm_genpd_present(const struct generic_pm_domain *genpd)
562{
563 const struct generic_pm_domain *gpd;
564
565 if (IS_ERR_OR_NULL(genpd))
566 return false;
567
568 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
569 if (gpd == genpd)
570 return true;
571
572 return false;
573}
574
575static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
576 struct device *dev)
577{
578 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
579}
580
581/**
582 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
583 * @genpd: PM domain to power off, if possible.
584 * @timed: True if latency measurements are allowed.
585 *
586 * Check if the given PM domain can be powered off (during system suspend or
587 * hibernation) and do that if so. Also, in that case propagate to its masters.
588 *
589 * This function is only called in "noirq" and "syscore" stages of system power
590 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
591 * executed sequentially, so it is guaranteed that it will never run twice in
592 * parallel).
593 */
594static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
595 bool timed)
596{
597 struct gpd_link *link;
598
599 if (genpd->status == GPD_STATE_POWER_OFF)
600 return;
601
602 if (genpd->suspended_count != genpd->device_count
603 || atomic_read(&genpd->sd_count) > 0)
604 return;
605
606 /* Choose the deepest state when suspending */
607 genpd->state_idx = genpd->state_count - 1;
608 genpd_power_off(genpd, timed);
609
610 genpd->status = GPD_STATE_POWER_OFF;
611
612 list_for_each_entry(link, &genpd->slave_links, slave_node) {
613 genpd_sd_counter_dec(link->master);
614 pm_genpd_sync_poweroff(link->master, timed);
615 }
616}
617
618/**
619 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
620 * @genpd: PM domain to power on.
621 * @timed: True if latency measurements are allowed.
622 *
623 * This function is only called in "noirq" and "syscore" stages of system power
624 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
625 * executed sequentially, so it is guaranteed that it will never run twice in
626 * parallel).
627 */
628static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
629 bool timed)
630{
631 struct gpd_link *link;
632
633 if (genpd->status == GPD_STATE_ACTIVE)
634 return;
635
636 list_for_each_entry(link, &genpd->slave_links, slave_node) {
637 pm_genpd_sync_poweron(link->master, timed);
638 genpd_sd_counter_inc(link->master);
639 }
640
641 genpd_power_on(genpd, timed);
642
643 genpd->status = GPD_STATE_ACTIVE;
644}
645
646/**
647 * resume_needed - Check whether to resume a device before system suspend.
648 * @dev: Device to check.
649 * @genpd: PM domain the device belongs to.
650 *
651 * There are two cases in which a device that can wake up the system from sleep
652 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
653 * to wake up the system and it has to remain active for this purpose while the
654 * system is in the sleep state and (2) if the device is not enabled to wake up
655 * the system from sleep states and it generally doesn't generate wakeup signals
656 * by itself (those signals are generated on its behalf by other parts of the
657 * system). In the latter case it may be necessary to reconfigure the device's
658 * wakeup settings during system suspend, because it may have been set up to
659 * signal remote wakeup from the system's working state as needed by runtime PM.
660 * Return 'true' in either of the above cases.
661 */
662static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
663{
664 bool active_wakeup;
665
666 if (!device_can_wakeup(dev))
667 return false;
668
669 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
670 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
671}
672
673/**
674 * pm_genpd_prepare - Start power transition of a device in a PM domain.
675 * @dev: Device to start the transition of.
676 *
677 * Start a power transition of a device (during a system-wide power transition)
678 * under the assumption that its pm_domain field points to the domain member of
679 * an object of type struct generic_pm_domain representing a PM domain
680 * consisting of I/O devices.
681 */
682static int pm_genpd_prepare(struct device *dev)
683{
684 struct generic_pm_domain *genpd;
685 int ret;
686
687 dev_dbg(dev, "%s()\n", __func__);
688
689 genpd = dev_to_genpd(dev);
690 if (IS_ERR(genpd))
691 return -EINVAL;
692
693 /*
694 * If a wakeup request is pending for the device, it should be woken up
695 * at this point and a system wakeup event should be reported if it's
696 * set up to wake up the system from sleep states.
697 */
698 pm_runtime_get_noresume(dev);
699 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
700 pm_wakeup_event(dev, 0);
701
702 if (pm_wakeup_pending()) {
703 pm_runtime_put(dev);
704 return -EBUSY;
705 }
706
707 if (resume_needed(dev, genpd))
708 pm_runtime_resume(dev);
709
710 mutex_lock(&genpd->lock);
711
712 if (genpd->prepared_count++ == 0) {
713 genpd->suspended_count = 0;
714 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
715 }
716
717 mutex_unlock(&genpd->lock);
718
719 if (genpd->suspend_power_off) {
720 pm_runtime_put_noidle(dev);
721 return 0;
722 }
723
724 /*
725 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
726 * so genpd_poweron() will return immediately, but if the device
727 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
728 * to make it operational.
729 */
730 pm_runtime_resume(dev);
731 __pm_runtime_disable(dev, false);
732
733 ret = pm_generic_prepare(dev);
734 if (ret) {
735 mutex_lock(&genpd->lock);
736
737 if (--genpd->prepared_count == 0)
738 genpd->suspend_power_off = false;
739
740 mutex_unlock(&genpd->lock);
741 pm_runtime_enable(dev);
742 }
743
744 pm_runtime_put(dev);
745 return ret;
746}
747
748/**
749 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
750 * @dev: Device to suspend.
751 *
752 * Suspend a device under the assumption that its pm_domain field points to the
753 * domain member of an object of type struct generic_pm_domain representing
754 * a PM domain consisting of I/O devices.
755 */
756static int pm_genpd_suspend(struct device *dev)
757{
758 struct generic_pm_domain *genpd;
759
760 dev_dbg(dev, "%s()\n", __func__);
761
762 genpd = dev_to_genpd(dev);
763 if (IS_ERR(genpd))
764 return -EINVAL;
765
766 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
767}
768
769/**
770 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
771 * @dev: Device to suspend.
772 *
773 * Carry out a late suspend of a device under the assumption that its
774 * pm_domain field points to the domain member of an object of type
775 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
776 */
777static int pm_genpd_suspend_late(struct device *dev)
778{
779 struct generic_pm_domain *genpd;
780
781 dev_dbg(dev, "%s()\n", __func__);
782
783 genpd = dev_to_genpd(dev);
784 if (IS_ERR(genpd))
785 return -EINVAL;
786
787 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
788}
789
790/**
791 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
792 * @dev: Device to suspend.
793 *
794 * Stop the device and remove power from the domain if all devices in it have
795 * been stopped.
796 */
797static int pm_genpd_suspend_noirq(struct device *dev)
798{
799 struct generic_pm_domain *genpd;
800
801 dev_dbg(dev, "%s()\n", __func__);
802
803 genpd = dev_to_genpd(dev);
804 if (IS_ERR(genpd))
805 return -EINVAL;
806
807 if (genpd->suspend_power_off
808 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
809 return 0;
810
811 genpd_stop_dev(genpd, dev);
812
813 /*
814 * Since all of the "noirq" callbacks are executed sequentially, it is
815 * guaranteed that this function will never run twice in parallel for
816 * the same PM domain, so it is not necessary to use locking here.
817 */
818 genpd->suspended_count++;
819 pm_genpd_sync_poweroff(genpd, true);
820
821 return 0;
822}
823
824/**
825 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
826 * @dev: Device to resume.
827 *
828 * Restore power to the device's PM domain, if necessary, and start the device.
829 */
830static int pm_genpd_resume_noirq(struct device *dev)
831{
832 struct generic_pm_domain *genpd;
833
834 dev_dbg(dev, "%s()\n", __func__);
835
836 genpd = dev_to_genpd(dev);
837 if (IS_ERR(genpd))
838 return -EINVAL;
839
840 if (genpd->suspend_power_off
841 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
842 return 0;
843
844 /*
845 * Since all of the "noirq" callbacks are executed sequentially, it is
846 * guaranteed that this function will never run twice in parallel for
847 * the same PM domain, so it is not necessary to use locking here.
848 */
849 pm_genpd_sync_poweron(genpd, true);
850 genpd->suspended_count--;
851
852 return genpd_start_dev(genpd, dev);
853}
854
855/**
856 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
857 * @dev: Device to resume.
858 *
859 * Carry out an early resume of a device under the assumption that its
860 * pm_domain field points to the domain member of an object of type
861 * struct generic_pm_domain representing a power domain consisting of I/O
862 * devices.
863 */
864static int pm_genpd_resume_early(struct device *dev)
865{
866 struct generic_pm_domain *genpd;
867
868 dev_dbg(dev, "%s()\n", __func__);
869
870 genpd = dev_to_genpd(dev);
871 if (IS_ERR(genpd))
872 return -EINVAL;
873
874 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
875}
876
877/**
878 * pm_genpd_resume - Resume of device in an I/O PM domain.
879 * @dev: Device to resume.
880 *
881 * Resume a device under the assumption that its pm_domain field points to the
882 * domain member of an object of type struct generic_pm_domain representing
883 * a power domain consisting of I/O devices.
884 */
885static int pm_genpd_resume(struct device *dev)
886{
887 struct generic_pm_domain *genpd;
888
889 dev_dbg(dev, "%s()\n", __func__);
890
891 genpd = dev_to_genpd(dev);
892 if (IS_ERR(genpd))
893 return -EINVAL;
894
895 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
896}
897
898/**
899 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
900 * @dev: Device to freeze.
901 *
902 * Freeze a device under the assumption that its pm_domain field points to the
903 * domain member of an object of type struct generic_pm_domain representing
904 * a power domain consisting of I/O devices.
905 */
906static int pm_genpd_freeze(struct device *dev)
907{
908 struct generic_pm_domain *genpd;
909
910 dev_dbg(dev, "%s()\n", __func__);
911
912 genpd = dev_to_genpd(dev);
913 if (IS_ERR(genpd))
914 return -EINVAL;
915
916 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
917}
918
919/**
920 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
921 * @dev: Device to freeze.
922 *
923 * Carry out a late freeze of a device under the assumption that its
924 * pm_domain field points to the domain member of an object of type
925 * struct generic_pm_domain representing a power domain consisting of I/O
926 * devices.
927 */
928static int pm_genpd_freeze_late(struct device *dev)
929{
930 struct generic_pm_domain *genpd;
931
932 dev_dbg(dev, "%s()\n", __func__);
933
934 genpd = dev_to_genpd(dev);
935 if (IS_ERR(genpd))
936 return -EINVAL;
937
938 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
939}
940
941/**
942 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
943 * @dev: Device to freeze.
944 *
945 * Carry out a late freeze of a device under the assumption that its
946 * pm_domain field points to the domain member of an object of type
947 * struct generic_pm_domain representing a power domain consisting of I/O
948 * devices.
949 */
950static int pm_genpd_freeze_noirq(struct device *dev)
951{
952 struct generic_pm_domain *genpd;
953
954 dev_dbg(dev, "%s()\n", __func__);
955
956 genpd = dev_to_genpd(dev);
957 if (IS_ERR(genpd))
958 return -EINVAL;
959
960 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
961}
962
963/**
964 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
965 * @dev: Device to thaw.
966 *
967 * Start the device, unless power has been removed from the domain already
968 * before the system transition.
969 */
970static int pm_genpd_thaw_noirq(struct device *dev)
971{
972 struct generic_pm_domain *genpd;
973
974 dev_dbg(dev, "%s()\n", __func__);
975
976 genpd = dev_to_genpd(dev);
977 if (IS_ERR(genpd))
978 return -EINVAL;
979
980 return genpd->suspend_power_off ?
981 0 : genpd_start_dev(genpd, dev);
982}
983
984/**
985 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
986 * @dev: Device to thaw.
987 *
988 * Carry out an early thaw of a device under the assumption that its
989 * pm_domain field points to the domain member of an object of type
990 * struct generic_pm_domain representing a power domain consisting of I/O
991 * devices.
992 */
993static int pm_genpd_thaw_early(struct device *dev)
994{
995 struct generic_pm_domain *genpd;
996
997 dev_dbg(dev, "%s()\n", __func__);
998
999 genpd = dev_to_genpd(dev);
1000 if (IS_ERR(genpd))
1001 return -EINVAL;
1002
1003 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
1004}
1005
1006/**
1007 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1008 * @dev: Device to thaw.
1009 *
1010 * Thaw a device under the assumption that its pm_domain field points to the
1011 * domain member of an object of type struct generic_pm_domain representing
1012 * a power domain consisting of I/O devices.
1013 */
1014static int pm_genpd_thaw(struct device *dev)
1015{
1016 struct generic_pm_domain *genpd;
1017
1018 dev_dbg(dev, "%s()\n", __func__);
1019
1020 genpd = dev_to_genpd(dev);
1021 if (IS_ERR(genpd))
1022 return -EINVAL;
1023
1024 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1025}
1026
1027/**
1028 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1029 * @dev: Device to resume.
1030 *
1031 * Make sure the domain will be in the same power state as before the
1032 * hibernation the system is resuming from and start the device if necessary.
1033 */
1034static int pm_genpd_restore_noirq(struct device *dev)
1035{
1036 struct generic_pm_domain *genpd;
1037
1038 dev_dbg(dev, "%s()\n", __func__);
1039
1040 genpd = dev_to_genpd(dev);
1041 if (IS_ERR(genpd))
1042 return -EINVAL;
1043
1044 /*
1045 * Since all of the "noirq" callbacks are executed sequentially, it is
1046 * guaranteed that this function will never run twice in parallel for
1047 * the same PM domain, so it is not necessary to use locking here.
1048 *
1049 * At this point suspended_count == 0 means we are being run for the
1050 * first time for the given domain in the present cycle.
1051 */
1052 if (genpd->suspended_count++ == 0) {
1053 /*
1054 * The boot kernel might put the domain into arbitrary state,
1055 * so make it appear as powered off to pm_genpd_sync_poweron(),
1056 * so that it tries to power it on in case it was really off.
1057 */
1058 genpd->status = GPD_STATE_POWER_OFF;
1059 if (genpd->suspend_power_off) {
1060 /*
1061 * If the domain was off before the hibernation, make
1062 * sure it will be off going forward.
1063 */
1064 genpd_power_off(genpd, true);
1065
1066 return 0;
1067 }
1068 }
1069
1070 if (genpd->suspend_power_off)
1071 return 0;
1072
1073 pm_genpd_sync_poweron(genpd, true);
1074
1075 return genpd_start_dev(genpd, dev);
1076}
1077
1078/**
1079 * pm_genpd_complete - Complete power transition of a device in a power domain.
1080 * @dev: Device to complete the transition of.
1081 *
1082 * Complete a power transition of a device (during a system-wide power
1083 * transition) under the assumption that its pm_domain field points to the
1084 * domain member of an object of type struct generic_pm_domain representing
1085 * a power domain consisting of I/O devices.
1086 */
1087static void pm_genpd_complete(struct device *dev)
1088{
1089 struct generic_pm_domain *genpd;
1090 bool run_complete;
1091
1092 dev_dbg(dev, "%s()\n", __func__);
1093
1094 genpd = dev_to_genpd(dev);
1095 if (IS_ERR(genpd))
1096 return;
1097
1098 mutex_lock(&genpd->lock);
1099
1100 run_complete = !genpd->suspend_power_off;
1101 if (--genpd->prepared_count == 0)
1102 genpd->suspend_power_off = false;
1103
1104 mutex_unlock(&genpd->lock);
1105
1106 if (run_complete) {
1107 pm_generic_complete(dev);
1108 pm_runtime_set_active(dev);
1109 pm_runtime_enable(dev);
1110 pm_request_idle(dev);
1111 }
1112}
1113
1114/**
1115 * genpd_syscore_switch - Switch power during system core suspend or resume.
1116 * @dev: Device that normally is marked as "always on" to switch power for.
1117 *
1118 * This routine may only be called during the system core (syscore) suspend or
1119 * resume phase for devices whose "always on" flags are set.
1120 */
1121static void genpd_syscore_switch(struct device *dev, bool suspend)
1122{
1123 struct generic_pm_domain *genpd;
1124
1125 genpd = dev_to_genpd(dev);
1126 if (!pm_genpd_present(genpd))
1127 return;
1128
1129 if (suspend) {
1130 genpd->suspended_count++;
1131 pm_genpd_sync_poweroff(genpd, false);
1132 } else {
1133 pm_genpd_sync_poweron(genpd, false);
1134 genpd->suspended_count--;
1135 }
1136}
1137
1138void pm_genpd_syscore_poweroff(struct device *dev)
1139{
1140 genpd_syscore_switch(dev, true);
1141}
1142EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1143
1144void pm_genpd_syscore_poweron(struct device *dev)
1145{
1146 genpd_syscore_switch(dev, false);
1147}
1148EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1149
1150#else /* !CONFIG_PM_SLEEP */
1151
1152#define pm_genpd_prepare NULL
1153#define pm_genpd_suspend NULL
1154#define pm_genpd_suspend_late NULL
1155#define pm_genpd_suspend_noirq NULL
1156#define pm_genpd_resume_early NULL
1157#define pm_genpd_resume_noirq NULL
1158#define pm_genpd_resume NULL
1159#define pm_genpd_freeze NULL
1160#define pm_genpd_freeze_late NULL
1161#define pm_genpd_freeze_noirq NULL
1162#define pm_genpd_thaw_early NULL
1163#define pm_genpd_thaw_noirq NULL
1164#define pm_genpd_thaw NULL
1165#define pm_genpd_restore_noirq NULL
1166#define pm_genpd_complete NULL
1167
1168#endif /* CONFIG_PM_SLEEP */
1169
1170static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1171 struct generic_pm_domain *genpd,
1172 struct gpd_timing_data *td)
1173{
1174 struct generic_pm_domain_data *gpd_data;
1175 int ret;
1176
1177 ret = dev_pm_get_subsys_data(dev);
1178 if (ret)
1179 return ERR_PTR(ret);
1180
1181 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1182 if (!gpd_data) {
1183 ret = -ENOMEM;
1184 goto err_put;
1185 }
1186
1187 if (td)
1188 gpd_data->td = *td;
1189
1190 gpd_data->base.dev = dev;
1191 gpd_data->td.constraint_changed = true;
1192 gpd_data->td.effective_constraint_ns = -1;
1193 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1194
1195 spin_lock_irq(&dev->power.lock);
1196
1197 if (dev->power.subsys_data->domain_data) {
1198 ret = -EINVAL;
1199 goto err_free;
1200 }
1201
1202 dev->power.subsys_data->domain_data = &gpd_data->base;
1203
1204 spin_unlock_irq(&dev->power.lock);
1205
1206 dev_pm_domain_set(dev, &genpd->domain);
1207
1208 return gpd_data;
1209
1210 err_free:
1211 spin_unlock_irq(&dev->power.lock);
1212 kfree(gpd_data);
1213 err_put:
1214 dev_pm_put_subsys_data(dev);
1215 return ERR_PTR(ret);
1216}
1217
1218static void genpd_free_dev_data(struct device *dev,
1219 struct generic_pm_domain_data *gpd_data)
1220{
1221 dev_pm_domain_set(dev, NULL);
1222
1223 spin_lock_irq(&dev->power.lock);
1224
1225 dev->power.subsys_data->domain_data = NULL;
1226
1227 spin_unlock_irq(&dev->power.lock);
1228
1229 kfree(gpd_data);
1230 dev_pm_put_subsys_data(dev);
1231}
1232
1233/**
1234 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1235 * @genpd: PM domain to add the device to.
1236 * @dev: Device to be added.
1237 * @td: Set of PM QoS timing parameters to attach to the device.
1238 */
1239int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1240 struct gpd_timing_data *td)
1241{
1242 struct generic_pm_domain_data *gpd_data;
1243 int ret = 0;
1244
1245 dev_dbg(dev, "%s()\n", __func__);
1246
1247 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1248 return -EINVAL;
1249
1250 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1251 if (IS_ERR(gpd_data))
1252 return PTR_ERR(gpd_data);
1253
1254 mutex_lock(&genpd->lock);
1255
1256 if (genpd->prepared_count > 0) {
1257 ret = -EAGAIN;
1258 goto out;
1259 }
1260
1261 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1262 if (ret)
1263 goto out;
1264
1265 genpd->device_count++;
1266 genpd->max_off_time_changed = true;
1267
1268 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1269
1270 out:
1271 mutex_unlock(&genpd->lock);
1272
1273 if (ret)
1274 genpd_free_dev_data(dev, gpd_data);
1275 else
1276 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1277
1278 return ret;
1279}
1280EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1281
1282/**
1283 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1284 * @genpd: PM domain to remove the device from.
1285 * @dev: Device to be removed.
1286 */
1287int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1288 struct device *dev)
1289{
1290 struct generic_pm_domain_data *gpd_data;
1291 struct pm_domain_data *pdd;
1292 int ret = 0;
1293
1294 dev_dbg(dev, "%s()\n", __func__);
1295
1296 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1297 return -EINVAL;
1298
1299 /* The above validation also means we have existing domain_data. */
1300 pdd = dev->power.subsys_data->domain_data;
1301 gpd_data = to_gpd_data(pdd);
1302 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1303
1304 mutex_lock(&genpd->lock);
1305
1306 if (genpd->prepared_count > 0) {
1307 ret = -EAGAIN;
1308 goto out;
1309 }
1310
1311 genpd->device_count--;
1312 genpd->max_off_time_changed = true;
1313
1314 if (genpd->detach_dev)
1315 genpd->detach_dev(genpd, dev);
1316
1317 list_del_init(&pdd->list_node);
1318
1319 mutex_unlock(&genpd->lock);
1320
1321 genpd_free_dev_data(dev, gpd_data);
1322
1323 return 0;
1324
1325 out:
1326 mutex_unlock(&genpd->lock);
1327 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1328
1329 return ret;
1330}
1331EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1332
1333/**
1334 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1335 * @genpd: Master PM domain to add the subdomain to.
1336 * @subdomain: Subdomain to be added.
1337 */
1338int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1339 struct generic_pm_domain *subdomain)
1340{
1341 struct gpd_link *link, *itr;
1342 int ret = 0;
1343
1344 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1345 || genpd == subdomain)
1346 return -EINVAL;
1347
1348 link = kzalloc(sizeof(*link), GFP_KERNEL);
1349 if (!link)
1350 return -ENOMEM;
1351
1352 mutex_lock(&subdomain->lock);
1353 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1354
1355 if (genpd->status == GPD_STATE_POWER_OFF
1356 && subdomain->status != GPD_STATE_POWER_OFF) {
1357 ret = -EINVAL;
1358 goto out;
1359 }
1360
1361 list_for_each_entry(itr, &genpd->master_links, master_node) {
1362 if (itr->slave == subdomain && itr->master == genpd) {
1363 ret = -EINVAL;
1364 goto out;
1365 }
1366 }
1367
1368 link->master = genpd;
1369 list_add_tail(&link->master_node, &genpd->master_links);
1370 link->slave = subdomain;
1371 list_add_tail(&link->slave_node, &subdomain->slave_links);
1372 if (subdomain->status != GPD_STATE_POWER_OFF)
1373 genpd_sd_counter_inc(genpd);
1374
1375 out:
1376 mutex_unlock(&genpd->lock);
1377 mutex_unlock(&subdomain->lock);
1378 if (ret)
1379 kfree(link);
1380 return ret;
1381}
1382EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1383
1384/**
1385 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1386 * @genpd: Master PM domain to remove the subdomain from.
1387 * @subdomain: Subdomain to be removed.
1388 */
1389int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1390 struct generic_pm_domain *subdomain)
1391{
1392 struct gpd_link *link;
1393 int ret = -EINVAL;
1394
1395 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1396 return -EINVAL;
1397
1398 mutex_lock(&subdomain->lock);
1399 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1400
1401 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1402 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1403 subdomain->name);
1404 ret = -EBUSY;
1405 goto out;
1406 }
1407
1408 list_for_each_entry(link, &genpd->master_links, master_node) {
1409 if (link->slave != subdomain)
1410 continue;
1411
1412 list_del(&link->master_node);
1413 list_del(&link->slave_node);
1414 kfree(link);
1415 if (subdomain->status != GPD_STATE_POWER_OFF)
1416 genpd_sd_counter_dec(genpd);
1417
1418 ret = 0;
1419 break;
1420 }
1421
1422out:
1423 mutex_unlock(&genpd->lock);
1424 mutex_unlock(&subdomain->lock);
1425
1426 return ret;
1427}
1428EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1429
1430/* Default device callbacks for generic PM domains. */
1431
1432/**
1433 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1434 * @dev: Device to handle.
1435 */
1436static int pm_genpd_default_save_state(struct device *dev)
1437{
1438 int (*cb)(struct device *__dev);
1439
1440 if (dev->type && dev->type->pm)
1441 cb = dev->type->pm->runtime_suspend;
1442 else if (dev->class && dev->class->pm)
1443 cb = dev->class->pm->runtime_suspend;
1444 else if (dev->bus && dev->bus->pm)
1445 cb = dev->bus->pm->runtime_suspend;
1446 else
1447 cb = NULL;
1448
1449 if (!cb && dev->driver && dev->driver->pm)
1450 cb = dev->driver->pm->runtime_suspend;
1451
1452 return cb ? cb(dev) : 0;
1453}
1454
1455/**
1456 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1457 * @dev: Device to handle.
1458 */
1459static int pm_genpd_default_restore_state(struct device *dev)
1460{
1461 int (*cb)(struct device *__dev);
1462
1463 if (dev->type && dev->type->pm)
1464 cb = dev->type->pm->runtime_resume;
1465 else if (dev->class && dev->class->pm)
1466 cb = dev->class->pm->runtime_resume;
1467 else if (dev->bus && dev->bus->pm)
1468 cb = dev->bus->pm->runtime_resume;
1469 else
1470 cb = NULL;
1471
1472 if (!cb && dev->driver && dev->driver->pm)
1473 cb = dev->driver->pm->runtime_resume;
1474
1475 return cb ? cb(dev) : 0;
1476}
1477
1478/**
1479 * pm_genpd_init - Initialize a generic I/O PM domain object.
1480 * @genpd: PM domain object to initialize.
1481 * @gov: PM domain governor to associate with the domain (may be NULL).
1482 * @is_off: Initial value of the domain's power_is_off field.
1483 */
1484void pm_genpd_init(struct generic_pm_domain *genpd,
1485 struct dev_power_governor *gov, bool is_off)
1486{
1487 if (IS_ERR_OR_NULL(genpd))
1488 return;
1489
1490 INIT_LIST_HEAD(&genpd->master_links);
1491 INIT_LIST_HEAD(&genpd->slave_links);
1492 INIT_LIST_HEAD(&genpd->dev_list);
1493 mutex_init(&genpd->lock);
1494 genpd->gov = gov;
1495 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1496 atomic_set(&genpd->sd_count, 0);
1497 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1498 genpd->device_count = 0;
1499 genpd->max_off_time_ns = -1;
1500 genpd->max_off_time_changed = true;
1501 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1502 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1503 genpd->domain.ops.prepare = pm_genpd_prepare;
1504 genpd->domain.ops.suspend = pm_genpd_suspend;
1505 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1506 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1507 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1508 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1509 genpd->domain.ops.resume = pm_genpd_resume;
1510 genpd->domain.ops.freeze = pm_genpd_freeze;
1511 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1512 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1513 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1514 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1515 genpd->domain.ops.thaw = pm_genpd_thaw;
1516 genpd->domain.ops.poweroff = pm_genpd_suspend;
1517 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1518 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1519 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1520 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1521 genpd->domain.ops.restore = pm_genpd_resume;
1522 genpd->domain.ops.complete = pm_genpd_complete;
1523 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1524 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1525
1526 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1527 genpd->dev_ops.stop = pm_clk_suspend;
1528 genpd->dev_ops.start = pm_clk_resume;
1529 }
1530
1531 if (genpd->state_idx >= GENPD_MAX_NUM_STATES) {
1532 pr_warn("Initial state index out of bounds.\n");
1533 genpd->state_idx = GENPD_MAX_NUM_STATES - 1;
1534 }
1535
1536 if (genpd->state_count > GENPD_MAX_NUM_STATES) {
1537 pr_warn("Limiting states to %d\n", GENPD_MAX_NUM_STATES);
1538 genpd->state_count = GENPD_MAX_NUM_STATES;
1539 }
1540
1541 /* Use only one "off" state if there were no states declared */
1542 if (genpd->state_count == 0)
1543 genpd->state_count = 1;
1544
1545 mutex_lock(&gpd_list_lock);
1546 list_add(&genpd->gpd_list_node, &gpd_list);
1547 mutex_unlock(&gpd_list_lock);
1548}
1549EXPORT_SYMBOL_GPL(pm_genpd_init);
1550
1551#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1552/*
1553 * Device Tree based PM domain providers.
1554 *
1555 * The code below implements generic device tree based PM domain providers that
1556 * bind device tree nodes with generic PM domains registered in the system.
1557 *
1558 * Any driver that registers generic PM domains and needs to support binding of
1559 * devices to these domains is supposed to register a PM domain provider, which
1560 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1561 *
1562 * Two simple mapping functions have been provided for convenience:
1563 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1564 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1565 * index.
1566 */
1567
1568/**
1569 * struct of_genpd_provider - PM domain provider registration structure
1570 * @link: Entry in global list of PM domain providers
1571 * @node: Pointer to device tree node of PM domain provider
1572 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1573 * into a PM domain.
1574 * @data: context pointer to be passed into @xlate callback
1575 */
1576struct of_genpd_provider {
1577 struct list_head link;
1578 struct device_node *node;
1579 genpd_xlate_t xlate;
1580 void *data;
1581};
1582
1583/* List of registered PM domain providers. */
1584static LIST_HEAD(of_genpd_providers);
1585/* Mutex to protect the list above. */
1586static DEFINE_MUTEX(of_genpd_mutex);
1587
1588/**
1589 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1590 * @genpdspec: OF phandle args to map into a PM domain
1591 * @data: xlate function private data - pointer to struct generic_pm_domain
1592 *
1593 * This is a generic xlate function that can be used to model PM domains that
1594 * have their own device tree nodes. The private data of xlate function needs
1595 * to be a valid pointer to struct generic_pm_domain.
1596 */
1597struct generic_pm_domain *__of_genpd_xlate_simple(
1598 struct of_phandle_args *genpdspec,
1599 void *data)
1600{
1601 if (genpdspec->args_count != 0)
1602 return ERR_PTR(-EINVAL);
1603 return data;
1604}
1605EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1606
1607/**
1608 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1609 * @genpdspec: OF phandle args to map into a PM domain
1610 * @data: xlate function private data - pointer to struct genpd_onecell_data
1611 *
1612 * This is a generic xlate function that can be used to model simple PM domain
1613 * controllers that have one device tree node and provide multiple PM domains.
1614 * A single cell is used as an index into an array of PM domains specified in
1615 * the genpd_onecell_data struct when registering the provider.
1616 */
1617struct generic_pm_domain *__of_genpd_xlate_onecell(
1618 struct of_phandle_args *genpdspec,
1619 void *data)
1620{
1621 struct genpd_onecell_data *genpd_data = data;
1622 unsigned int idx = genpdspec->args[0];
1623
1624 if (genpdspec->args_count != 1)
1625 return ERR_PTR(-EINVAL);
1626
1627 if (idx >= genpd_data->num_domains) {
1628 pr_err("%s: invalid domain index %u\n", __func__, idx);
1629 return ERR_PTR(-EINVAL);
1630 }
1631
1632 if (!genpd_data->domains[idx])
1633 return ERR_PTR(-ENOENT);
1634
1635 return genpd_data->domains[idx];
1636}
1637EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1638
1639/**
1640 * __of_genpd_add_provider() - Register a PM domain provider for a node
1641 * @np: Device node pointer associated with the PM domain provider.
1642 * @xlate: Callback for decoding PM domain from phandle arguments.
1643 * @data: Context pointer for @xlate callback.
1644 */
1645int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1646 void *data)
1647{
1648 struct of_genpd_provider *cp;
1649
1650 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1651 if (!cp)
1652 return -ENOMEM;
1653
1654 cp->node = of_node_get(np);
1655 cp->data = data;
1656 cp->xlate = xlate;
1657
1658 mutex_lock(&of_genpd_mutex);
1659 list_add(&cp->link, &of_genpd_providers);
1660 mutex_unlock(&of_genpd_mutex);
1661 pr_debug("Added domain provider from %s\n", np->full_name);
1662
1663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1666
1667/**
1668 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1669 * @np: Device node pointer associated with the PM domain provider
1670 */
1671void of_genpd_del_provider(struct device_node *np)
1672{
1673 struct of_genpd_provider *cp;
1674
1675 mutex_lock(&of_genpd_mutex);
1676 list_for_each_entry(cp, &of_genpd_providers, link) {
1677 if (cp->node == np) {
1678 list_del(&cp->link);
1679 of_node_put(cp->node);
1680 kfree(cp);
1681 break;
1682 }
1683 }
1684 mutex_unlock(&of_genpd_mutex);
1685}
1686EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1687
1688/**
1689 * of_genpd_get_from_provider() - Look-up PM domain
1690 * @genpdspec: OF phandle args to use for look-up
1691 *
1692 * Looks for a PM domain provider under the node specified by @genpdspec and if
1693 * found, uses xlate function of the provider to map phandle args to a PM
1694 * domain.
1695 *
1696 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1697 * on failure.
1698 */
1699struct generic_pm_domain *of_genpd_get_from_provider(
1700 struct of_phandle_args *genpdspec)
1701{
1702 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1703 struct of_genpd_provider *provider;
1704
1705 if (!genpdspec)
1706 return ERR_PTR(-EINVAL);
1707
1708 mutex_lock(&of_genpd_mutex);
1709
1710 /* Check if we have such a provider in our array */
1711 list_for_each_entry(provider, &of_genpd_providers, link) {
1712 if (provider->node == genpdspec->np)
1713 genpd = provider->xlate(genpdspec, provider->data);
1714 if (!IS_ERR(genpd))
1715 break;
1716 }
1717
1718 mutex_unlock(&of_genpd_mutex);
1719
1720 return genpd;
1721}
1722EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1723
1724/**
1725 * genpd_dev_pm_detach - Detach a device from its PM domain.
1726 * @dev: Device to detach.
1727 * @power_off: Currently not used
1728 *
1729 * Try to locate a corresponding generic PM domain, which the device was
1730 * attached to previously. If such is found, the device is detached from it.
1731 */
1732static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1733{
1734 struct generic_pm_domain *pd;
1735 unsigned int i;
1736 int ret = 0;
1737
1738 pd = pm_genpd_lookup_dev(dev);
1739 if (!pd)
1740 return;
1741
1742 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1743
1744 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1745 ret = pm_genpd_remove_device(pd, dev);
1746 if (ret != -EAGAIN)
1747 break;
1748
1749 mdelay(i);
1750 cond_resched();
1751 }
1752
1753 if (ret < 0) {
1754 dev_err(dev, "failed to remove from PM domain %s: %d",
1755 pd->name, ret);
1756 return;
1757 }
1758
1759 /* Check if PM domain can be powered off after removing this device. */
1760 genpd_queue_power_off_work(pd);
1761}
1762
1763static void genpd_dev_pm_sync(struct device *dev)
1764{
1765 struct generic_pm_domain *pd;
1766
1767 pd = dev_to_genpd(dev);
1768 if (IS_ERR(pd))
1769 return;
1770
1771 genpd_queue_power_off_work(pd);
1772}
1773
1774/**
1775 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1776 * @dev: Device to attach.
1777 *
1778 * Parse device's OF node to find a PM domain specifier. If such is found,
1779 * attaches the device to retrieved pm_domain ops.
1780 *
1781 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1782 * backwards compatibility with existing DTBs.
1783 *
1784 * Returns 0 on successfully attached PM domain or negative error code. Note
1785 * that if a power-domain exists for the device, but it cannot be found or
1786 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1787 * probed and to re-try again later.
1788 */
1789int genpd_dev_pm_attach(struct device *dev)
1790{
1791 struct of_phandle_args pd_args;
1792 struct generic_pm_domain *pd;
1793 unsigned int i;
1794 int ret;
1795
1796 if (!dev->of_node)
1797 return -ENODEV;
1798
1799 if (dev->pm_domain)
1800 return -EEXIST;
1801
1802 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1803 "#power-domain-cells", 0, &pd_args);
1804 if (ret < 0) {
1805 if (ret != -ENOENT)
1806 return ret;
1807
1808 /*
1809 * Try legacy Samsung-specific bindings
1810 * (for backwards compatibility of DT ABI)
1811 */
1812 pd_args.args_count = 0;
1813 pd_args.np = of_parse_phandle(dev->of_node,
1814 "samsung,power-domain", 0);
1815 if (!pd_args.np)
1816 return -ENOENT;
1817 }
1818
1819 pd = of_genpd_get_from_provider(&pd_args);
1820 of_node_put(pd_args.np);
1821 if (IS_ERR(pd)) {
1822 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1823 __func__, PTR_ERR(pd));
1824 return -EPROBE_DEFER;
1825 }
1826
1827 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1828
1829 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1830 ret = pm_genpd_add_device(pd, dev);
1831 if (ret != -EAGAIN)
1832 break;
1833
1834 mdelay(i);
1835 cond_resched();
1836 }
1837
1838 if (ret < 0) {
1839 dev_err(dev, "failed to add to PM domain %s: %d",
1840 pd->name, ret);
1841 goto out;
1842 }
1843
1844 dev->pm_domain->detach = genpd_dev_pm_detach;
1845 dev->pm_domain->sync = genpd_dev_pm_sync;
1846
1847 mutex_lock(&pd->lock);
1848 ret = genpd_poweron(pd, 0);
1849 mutex_unlock(&pd->lock);
1850out:
1851 return ret ? -EPROBE_DEFER : 0;
1852}
1853EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1854#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1855
1856
1857/*** debugfs support ***/
1858
1859#ifdef CONFIG_PM_ADVANCED_DEBUG
1860#include <linux/pm.h>
1861#include <linux/device.h>
1862#include <linux/debugfs.h>
1863#include <linux/seq_file.h>
1864#include <linux/init.h>
1865#include <linux/kobject.h>
1866static struct dentry *pm_genpd_debugfs_dir;
1867
1868/*
1869 * TODO: This function is a slightly modified version of rtpm_status_show
1870 * from sysfs.c, so generalize it.
1871 */
1872static void rtpm_status_str(struct seq_file *s, struct device *dev)
1873{
1874 static const char * const status_lookup[] = {
1875 [RPM_ACTIVE] = "active",
1876 [RPM_RESUMING] = "resuming",
1877 [RPM_SUSPENDED] = "suspended",
1878 [RPM_SUSPENDING] = "suspending"
1879 };
1880 const char *p = "";
1881
1882 if (dev->power.runtime_error)
1883 p = "error";
1884 else if (dev->power.disable_depth)
1885 p = "unsupported";
1886 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1887 p = status_lookup[dev->power.runtime_status];
1888 else
1889 WARN_ON(1);
1890
1891 seq_puts(s, p);
1892}
1893
1894static int pm_genpd_summary_one(struct seq_file *s,
1895 struct generic_pm_domain *genpd)
1896{
1897 static const char * const status_lookup[] = {
1898 [GPD_STATE_ACTIVE] = "on",
1899 [GPD_STATE_POWER_OFF] = "off"
1900 };
1901 struct pm_domain_data *pm_data;
1902 const char *kobj_path;
1903 struct gpd_link *link;
1904 char state[16];
1905 int ret;
1906
1907 ret = mutex_lock_interruptible(&genpd->lock);
1908 if (ret)
1909 return -ERESTARTSYS;
1910
1911 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1912 goto exit;
1913 if (genpd->status == GPD_STATE_POWER_OFF)
1914 snprintf(state, sizeof(state), "%s-%u",
1915 status_lookup[genpd->status], genpd->state_idx);
1916 else
1917 snprintf(state, sizeof(state), "%s",
1918 status_lookup[genpd->status]);
1919 seq_printf(s, "%-30s %-15s ", genpd->name, state);
1920
1921 /*
1922 * Modifications on the list require holding locks on both
1923 * master and slave, so we are safe.
1924 * Also genpd->name is immutable.
1925 */
1926 list_for_each_entry(link, &genpd->master_links, master_node) {
1927 seq_printf(s, "%s", link->slave->name);
1928 if (!list_is_last(&link->master_node, &genpd->master_links))
1929 seq_puts(s, ", ");
1930 }
1931
1932 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1933 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1934 if (kobj_path == NULL)
1935 continue;
1936
1937 seq_printf(s, "\n %-50s ", kobj_path);
1938 rtpm_status_str(s, pm_data->dev);
1939 kfree(kobj_path);
1940 }
1941
1942 seq_puts(s, "\n");
1943exit:
1944 mutex_unlock(&genpd->lock);
1945
1946 return 0;
1947}
1948
1949static int pm_genpd_summary_show(struct seq_file *s, void *data)
1950{
1951 struct generic_pm_domain *genpd;
1952 int ret = 0;
1953
1954 seq_puts(s, "domain status slaves\n");
1955 seq_puts(s, " /device runtime status\n");
1956 seq_puts(s, "----------------------------------------------------------------------\n");
1957
1958 ret = mutex_lock_interruptible(&gpd_list_lock);
1959 if (ret)
1960 return -ERESTARTSYS;
1961
1962 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1963 ret = pm_genpd_summary_one(s, genpd);
1964 if (ret)
1965 break;
1966 }
1967 mutex_unlock(&gpd_list_lock);
1968
1969 return ret;
1970}
1971
1972static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1973{
1974 return single_open(file, pm_genpd_summary_show, NULL);
1975}
1976
1977static const struct file_operations pm_genpd_summary_fops = {
1978 .open = pm_genpd_summary_open,
1979 .read = seq_read,
1980 .llseek = seq_lseek,
1981 .release = single_release,
1982};
1983
1984static int __init pm_genpd_debug_init(void)
1985{
1986 struct dentry *d;
1987
1988 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1989
1990 if (!pm_genpd_debugfs_dir)
1991 return -ENOMEM;
1992
1993 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1994 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1995 if (!d)
1996 return -ENOMEM;
1997
1998 return 0;
1999}
2000late_initcall(pm_genpd_debug_init);
2001
2002static void __exit pm_genpd_debug_exit(void)
2003{
2004 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2005}
2006__exitcall(pm_genpd_debug_exit);
2007#endif /* CONFIG_PM_ADVANCED_DEBUG */
1/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
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>
22
23#include "power.h"
24
25#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
27#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
35 } \
36 __ret; \
37})
38
39static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
42struct genpd_lock_ops {
43 void (*lock)(struct generic_pm_domain *genpd);
44 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
45 int (*lock_interruptible)(struct generic_pm_domain *genpd);
46 void (*unlock)(struct generic_pm_domain *genpd);
47};
48
49static void genpd_lock_mtx(struct generic_pm_domain *genpd)
50{
51 mutex_lock(&genpd->mlock);
52}
53
54static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
55 int depth)
56{
57 mutex_lock_nested(&genpd->mlock, depth);
58}
59
60static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
61{
62 return mutex_lock_interruptible(&genpd->mlock);
63}
64
65static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
66{
67 return mutex_unlock(&genpd->mlock);
68}
69
70static const struct genpd_lock_ops genpd_mtx_ops = {
71 .lock = genpd_lock_mtx,
72 .lock_nested = genpd_lock_nested_mtx,
73 .lock_interruptible = genpd_lock_interruptible_mtx,
74 .unlock = genpd_unlock_mtx,
75};
76
77static void genpd_lock_spin(struct generic_pm_domain *genpd)
78 __acquires(&genpd->slock)
79{
80 unsigned long flags;
81
82 spin_lock_irqsave(&genpd->slock, flags);
83 genpd->lock_flags = flags;
84}
85
86static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
87 int depth)
88 __acquires(&genpd->slock)
89{
90 unsigned long flags;
91
92 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
93 genpd->lock_flags = flags;
94}
95
96static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
97 __acquires(&genpd->slock)
98{
99 unsigned long flags;
100
101 spin_lock_irqsave(&genpd->slock, flags);
102 genpd->lock_flags = flags;
103 return 0;
104}
105
106static void genpd_unlock_spin(struct generic_pm_domain *genpd)
107 __releases(&genpd->slock)
108{
109 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
110}
111
112static const struct genpd_lock_ops genpd_spin_ops = {
113 .lock = genpd_lock_spin,
114 .lock_nested = genpd_lock_nested_spin,
115 .lock_interruptible = genpd_lock_interruptible_spin,
116 .unlock = genpd_unlock_spin,
117};
118
119#define genpd_lock(p) p->lock_ops->lock(p)
120#define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
121#define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
122#define genpd_unlock(p) p->lock_ops->unlock(p)
123
124#define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
125#define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
126#define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
127#define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
128
129static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
130 const struct generic_pm_domain *genpd)
131{
132 bool ret;
133
134 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
135
136 /*
137 * Warn once if an IRQ safe device is attached to a no sleep domain, as
138 * to indicate a suboptimal configuration for PM. For an always on
139 * domain this isn't case, thus don't warn.
140 */
141 if (ret && !genpd_is_always_on(genpd))
142 dev_warn_once(dev, "PM domain %s will not be powered off\n",
143 genpd->name);
144
145 return ret;
146}
147
148/*
149 * Get the generic PM domain for a particular struct device.
150 * This validates the struct device pointer, the PM domain pointer,
151 * and checks that the PM domain pointer is a real generic PM domain.
152 * Any failure results in NULL being returned.
153 */
154static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
155{
156 struct generic_pm_domain *genpd = NULL, *gpd;
157
158 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
159 return NULL;
160
161 mutex_lock(&gpd_list_lock);
162 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
163 if (&gpd->domain == dev->pm_domain) {
164 genpd = gpd;
165 break;
166 }
167 }
168 mutex_unlock(&gpd_list_lock);
169
170 return genpd;
171}
172
173/*
174 * This should only be used where we are certain that the pm_domain
175 * attached to the device is a genpd domain.
176 */
177static struct generic_pm_domain *dev_to_genpd(struct device *dev)
178{
179 if (IS_ERR_OR_NULL(dev->pm_domain))
180 return ERR_PTR(-EINVAL);
181
182 return pd_to_genpd(dev->pm_domain);
183}
184
185static int genpd_stop_dev(const struct generic_pm_domain *genpd,
186 struct device *dev)
187{
188 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
189}
190
191static int genpd_start_dev(const struct generic_pm_domain *genpd,
192 struct device *dev)
193{
194 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
195}
196
197static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
198{
199 bool ret = false;
200
201 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
202 ret = !!atomic_dec_and_test(&genpd->sd_count);
203
204 return ret;
205}
206
207static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
208{
209 atomic_inc(&genpd->sd_count);
210 smp_mb__after_atomic();
211}
212
213#ifdef CONFIG_DEBUG_FS
214static void genpd_update_accounting(struct generic_pm_domain *genpd)
215{
216 ktime_t delta, now;
217
218 now = ktime_get();
219 delta = ktime_sub(now, genpd->accounting_time);
220
221 /*
222 * If genpd->status is active, it means we are just
223 * out of off and so update the idle time and vice
224 * versa.
225 */
226 if (genpd->status == GPD_STATE_ACTIVE) {
227 int state_idx = genpd->state_idx;
228
229 genpd->states[state_idx].idle_time =
230 ktime_add(genpd->states[state_idx].idle_time, delta);
231 } else {
232 genpd->on_time = ktime_add(genpd->on_time, delta);
233 }
234
235 genpd->accounting_time = now;
236}
237#else
238static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
239#endif
240
241/**
242 * dev_pm_genpd_set_performance_state- Set performance state of device's power
243 * domain.
244 *
245 * @dev: Device for which the performance-state needs to be set.
246 * @state: Target performance state of the device. This can be set as 0 when the
247 * device doesn't have any performance state constraints left (And so
248 * the device wouldn't participate anymore to find the target
249 * performance state of the genpd).
250 *
251 * It is assumed that the users guarantee that the genpd wouldn't be detached
252 * while this routine is getting called.
253 *
254 * Returns 0 on success and negative error values on failures.
255 */
256int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
257{
258 struct generic_pm_domain *genpd;
259 struct generic_pm_domain_data *gpd_data, *pd_data;
260 struct pm_domain_data *pdd;
261 unsigned int prev;
262 int ret = 0;
263
264 genpd = dev_to_genpd(dev);
265 if (IS_ERR(genpd))
266 return -ENODEV;
267
268 if (unlikely(!genpd->set_performance_state))
269 return -EINVAL;
270
271 if (unlikely(!dev->power.subsys_data ||
272 !dev->power.subsys_data->domain_data)) {
273 WARN_ON(1);
274 return -EINVAL;
275 }
276
277 genpd_lock(genpd);
278
279 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
280 prev = gpd_data->performance_state;
281 gpd_data->performance_state = state;
282
283 /* New requested state is same as Max requested state */
284 if (state == genpd->performance_state)
285 goto unlock;
286
287 /* New requested state is higher than Max requested state */
288 if (state > genpd->performance_state)
289 goto update_state;
290
291 /* Traverse all devices within the domain */
292 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
293 pd_data = to_gpd_data(pdd);
294
295 if (pd_data->performance_state > state)
296 state = pd_data->performance_state;
297 }
298
299 if (state == genpd->performance_state)
300 goto unlock;
301
302 /*
303 * We aren't propagating performance state changes of a subdomain to its
304 * masters as we don't have hardware that needs it. Over that, the
305 * performance states of subdomain and its masters may not have
306 * one-to-one mapping and would require additional information. We can
307 * get back to this once we have hardware that needs it. For that
308 * reason, we don't have to consider performance state of the subdomains
309 * of genpd here.
310 */
311
312update_state:
313 if (genpd_status_on(genpd)) {
314 ret = genpd->set_performance_state(genpd, state);
315 if (ret) {
316 gpd_data->performance_state = prev;
317 goto unlock;
318 }
319 }
320
321 genpd->performance_state = state;
322
323unlock:
324 genpd_unlock(genpd);
325
326 return ret;
327}
328EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
329
330static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
331{
332 unsigned int state_idx = genpd->state_idx;
333 ktime_t time_start;
334 s64 elapsed_ns;
335 int ret;
336
337 if (!genpd->power_on)
338 return 0;
339
340 if (!timed)
341 return genpd->power_on(genpd);
342
343 time_start = ktime_get();
344 ret = genpd->power_on(genpd);
345 if (ret)
346 return ret;
347
348 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
349
350 if (unlikely(genpd->set_performance_state)) {
351 ret = genpd->set_performance_state(genpd, genpd->performance_state);
352 if (ret) {
353 pr_warn("%s: Failed to set performance state %d (%d)\n",
354 genpd->name, genpd->performance_state, ret);
355 }
356 }
357
358 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
359 return ret;
360
361 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
362 genpd->max_off_time_changed = true;
363 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
364 genpd->name, "on", elapsed_ns);
365
366 return ret;
367}
368
369static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
370{
371 unsigned int state_idx = genpd->state_idx;
372 ktime_t time_start;
373 s64 elapsed_ns;
374 int ret;
375
376 if (!genpd->power_off)
377 return 0;
378
379 if (!timed)
380 return genpd->power_off(genpd);
381
382 time_start = ktime_get();
383 ret = genpd->power_off(genpd);
384 if (ret == -EBUSY)
385 return ret;
386
387 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
388 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
389 return ret;
390
391 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
392 genpd->max_off_time_changed = true;
393 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
394 genpd->name, "off", elapsed_ns);
395
396 return ret;
397}
398
399/**
400 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
401 * @genpd: PM domain to power off.
402 *
403 * Queue up the execution of genpd_power_off() unless it's already been done
404 * before.
405 */
406static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
407{
408 queue_work(pm_wq, &genpd->power_off_work);
409}
410
411/**
412 * genpd_power_off - Remove power from a given PM domain.
413 * @genpd: PM domain to power down.
414 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
415 * RPM status of the releated device is in an intermediate state, not yet turned
416 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
417 * be RPM_SUSPENDED, while it tries to power off the PM domain.
418 *
419 * If all of the @genpd's devices have been suspended and all of its subdomains
420 * have been powered down, remove power from @genpd.
421 */
422static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
423 unsigned int depth)
424{
425 struct pm_domain_data *pdd;
426 struct gpd_link *link;
427 unsigned int not_suspended = 0;
428
429 /*
430 * Do not try to power off the domain in the following situations:
431 * (1) The domain is already in the "power off" state.
432 * (2) System suspend is in progress.
433 */
434 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
435 return 0;
436
437 /*
438 * Abort power off for the PM domain in the following situations:
439 * (1) The domain is configured as always on.
440 * (2) When the domain has a subdomain being powered on.
441 */
442 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
443 return -EBUSY;
444
445 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
446 enum pm_qos_flags_status stat;
447
448 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
449 if (stat > PM_QOS_FLAGS_NONE)
450 return -EBUSY;
451
452 /*
453 * Do not allow PM domain to be powered off, when an IRQ safe
454 * device is part of a non-IRQ safe domain.
455 */
456 if (!pm_runtime_suspended(pdd->dev) ||
457 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
458 not_suspended++;
459 }
460
461 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
462 return -EBUSY;
463
464 if (genpd->gov && genpd->gov->power_down_ok) {
465 if (!genpd->gov->power_down_ok(&genpd->domain))
466 return -EAGAIN;
467 }
468
469 if (genpd->power_off) {
470 int ret;
471
472 if (atomic_read(&genpd->sd_count) > 0)
473 return -EBUSY;
474
475 /*
476 * If sd_count > 0 at this point, one of the subdomains hasn't
477 * managed to call genpd_power_on() for the master yet after
478 * incrementing it. In that case genpd_power_on() will wait
479 * for us to drop the lock, so we can call .power_off() and let
480 * the genpd_power_on() restore power for us (this shouldn't
481 * happen very often).
482 */
483 ret = _genpd_power_off(genpd, true);
484 if (ret)
485 return ret;
486 }
487
488 genpd->status = GPD_STATE_POWER_OFF;
489 genpd_update_accounting(genpd);
490
491 list_for_each_entry(link, &genpd->slave_links, slave_node) {
492 genpd_sd_counter_dec(link->master);
493 genpd_lock_nested(link->master, depth + 1);
494 genpd_power_off(link->master, false, depth + 1);
495 genpd_unlock(link->master);
496 }
497
498 return 0;
499}
500
501/**
502 * genpd_power_on - Restore power to a given PM domain and its masters.
503 * @genpd: PM domain to power up.
504 * @depth: nesting count for lockdep.
505 *
506 * Restore power to @genpd and all of its masters so that it is possible to
507 * resume a device belonging to it.
508 */
509static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
510{
511 struct gpd_link *link;
512 int ret = 0;
513
514 if (genpd_status_on(genpd))
515 return 0;
516
517 /*
518 * The list is guaranteed not to change while the loop below is being
519 * executed, unless one of the masters' .power_on() callbacks fiddles
520 * with it.
521 */
522 list_for_each_entry(link, &genpd->slave_links, slave_node) {
523 struct generic_pm_domain *master = link->master;
524
525 genpd_sd_counter_inc(master);
526
527 genpd_lock_nested(master, depth + 1);
528 ret = genpd_power_on(master, depth + 1);
529 genpd_unlock(master);
530
531 if (ret) {
532 genpd_sd_counter_dec(master);
533 goto err;
534 }
535 }
536
537 ret = _genpd_power_on(genpd, true);
538 if (ret)
539 goto err;
540
541 genpd->status = GPD_STATE_ACTIVE;
542 genpd_update_accounting(genpd);
543
544 return 0;
545
546 err:
547 list_for_each_entry_continue_reverse(link,
548 &genpd->slave_links,
549 slave_node) {
550 genpd_sd_counter_dec(link->master);
551 genpd_lock_nested(link->master, depth + 1);
552 genpd_power_off(link->master, false, depth + 1);
553 genpd_unlock(link->master);
554 }
555
556 return ret;
557}
558
559static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
560 unsigned long val, void *ptr)
561{
562 struct generic_pm_domain_data *gpd_data;
563 struct device *dev;
564
565 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
566 dev = gpd_data->base.dev;
567
568 for (;;) {
569 struct generic_pm_domain *genpd;
570 struct pm_domain_data *pdd;
571
572 spin_lock_irq(&dev->power.lock);
573
574 pdd = dev->power.subsys_data ?
575 dev->power.subsys_data->domain_data : NULL;
576 if (pdd) {
577 to_gpd_data(pdd)->td.constraint_changed = true;
578 genpd = dev_to_genpd(dev);
579 } else {
580 genpd = ERR_PTR(-ENODATA);
581 }
582
583 spin_unlock_irq(&dev->power.lock);
584
585 if (!IS_ERR(genpd)) {
586 genpd_lock(genpd);
587 genpd->max_off_time_changed = true;
588 genpd_unlock(genpd);
589 }
590
591 dev = dev->parent;
592 if (!dev || dev->power.ignore_children)
593 break;
594 }
595
596 return NOTIFY_DONE;
597}
598
599/**
600 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
601 * @work: Work structure used for scheduling the execution of this function.
602 */
603static void genpd_power_off_work_fn(struct work_struct *work)
604{
605 struct generic_pm_domain *genpd;
606
607 genpd = container_of(work, struct generic_pm_domain, power_off_work);
608
609 genpd_lock(genpd);
610 genpd_power_off(genpd, false, 0);
611 genpd_unlock(genpd);
612}
613
614/**
615 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
616 * @dev: Device to handle.
617 */
618static int __genpd_runtime_suspend(struct device *dev)
619{
620 int (*cb)(struct device *__dev);
621
622 if (dev->type && dev->type->pm)
623 cb = dev->type->pm->runtime_suspend;
624 else if (dev->class && dev->class->pm)
625 cb = dev->class->pm->runtime_suspend;
626 else if (dev->bus && dev->bus->pm)
627 cb = dev->bus->pm->runtime_suspend;
628 else
629 cb = NULL;
630
631 if (!cb && dev->driver && dev->driver->pm)
632 cb = dev->driver->pm->runtime_suspend;
633
634 return cb ? cb(dev) : 0;
635}
636
637/**
638 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
639 * @dev: Device to handle.
640 */
641static int __genpd_runtime_resume(struct device *dev)
642{
643 int (*cb)(struct device *__dev);
644
645 if (dev->type && dev->type->pm)
646 cb = dev->type->pm->runtime_resume;
647 else if (dev->class && dev->class->pm)
648 cb = dev->class->pm->runtime_resume;
649 else if (dev->bus && dev->bus->pm)
650 cb = dev->bus->pm->runtime_resume;
651 else
652 cb = NULL;
653
654 if (!cb && dev->driver && dev->driver->pm)
655 cb = dev->driver->pm->runtime_resume;
656
657 return cb ? cb(dev) : 0;
658}
659
660/**
661 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
662 * @dev: Device to suspend.
663 *
664 * Carry out a runtime suspend of a device under the assumption that its
665 * pm_domain field points to the domain member of an object of type
666 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
667 */
668static int genpd_runtime_suspend(struct device *dev)
669{
670 struct generic_pm_domain *genpd;
671 bool (*suspend_ok)(struct device *__dev);
672 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
673 bool runtime_pm = pm_runtime_enabled(dev);
674 ktime_t time_start;
675 s64 elapsed_ns;
676 int ret;
677
678 dev_dbg(dev, "%s()\n", __func__);
679
680 genpd = dev_to_genpd(dev);
681 if (IS_ERR(genpd))
682 return -EINVAL;
683
684 /*
685 * A runtime PM centric subsystem/driver may re-use the runtime PM
686 * callbacks for other purposes than runtime PM. In those scenarios
687 * runtime PM is disabled. Under these circumstances, we shall skip
688 * validating/measuring the PM QoS latency.
689 */
690 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
691 if (runtime_pm && suspend_ok && !suspend_ok(dev))
692 return -EBUSY;
693
694 /* Measure suspend latency. */
695 time_start = 0;
696 if (runtime_pm)
697 time_start = ktime_get();
698
699 ret = __genpd_runtime_suspend(dev);
700 if (ret)
701 return ret;
702
703 ret = genpd_stop_dev(genpd, dev);
704 if (ret) {
705 __genpd_runtime_resume(dev);
706 return ret;
707 }
708
709 /* Update suspend latency value if the measured time exceeds it. */
710 if (runtime_pm) {
711 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
712 if (elapsed_ns > td->suspend_latency_ns) {
713 td->suspend_latency_ns = elapsed_ns;
714 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
715 elapsed_ns);
716 genpd->max_off_time_changed = true;
717 td->constraint_changed = true;
718 }
719 }
720
721 /*
722 * If power.irq_safe is set, this routine may be run with
723 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
724 */
725 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
726 return 0;
727
728 genpd_lock(genpd);
729 genpd_power_off(genpd, true, 0);
730 genpd_unlock(genpd);
731
732 return 0;
733}
734
735/**
736 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
737 * @dev: Device to resume.
738 *
739 * Carry out a runtime resume of a device under the assumption that its
740 * pm_domain field points to the domain member of an object of type
741 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
742 */
743static int genpd_runtime_resume(struct device *dev)
744{
745 struct generic_pm_domain *genpd;
746 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
747 bool runtime_pm = pm_runtime_enabled(dev);
748 ktime_t time_start;
749 s64 elapsed_ns;
750 int ret;
751 bool timed = true;
752
753 dev_dbg(dev, "%s()\n", __func__);
754
755 genpd = dev_to_genpd(dev);
756 if (IS_ERR(genpd))
757 return -EINVAL;
758
759 /*
760 * As we don't power off a non IRQ safe domain, which holds
761 * an IRQ safe device, we don't need to restore power to it.
762 */
763 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
764 timed = false;
765 goto out;
766 }
767
768 genpd_lock(genpd);
769 ret = genpd_power_on(genpd, 0);
770 genpd_unlock(genpd);
771
772 if (ret)
773 return ret;
774
775 out:
776 /* Measure resume latency. */
777 time_start = 0;
778 if (timed && runtime_pm)
779 time_start = ktime_get();
780
781 ret = genpd_start_dev(genpd, dev);
782 if (ret)
783 goto err_poweroff;
784
785 ret = __genpd_runtime_resume(dev);
786 if (ret)
787 goto err_stop;
788
789 /* Update resume latency value if the measured time exceeds it. */
790 if (timed && runtime_pm) {
791 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
792 if (elapsed_ns > td->resume_latency_ns) {
793 td->resume_latency_ns = elapsed_ns;
794 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
795 elapsed_ns);
796 genpd->max_off_time_changed = true;
797 td->constraint_changed = true;
798 }
799 }
800
801 return 0;
802
803err_stop:
804 genpd_stop_dev(genpd, dev);
805err_poweroff:
806 if (!pm_runtime_is_irq_safe(dev) ||
807 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
808 genpd_lock(genpd);
809 genpd_power_off(genpd, true, 0);
810 genpd_unlock(genpd);
811 }
812
813 return ret;
814}
815
816static bool pd_ignore_unused;
817static int __init pd_ignore_unused_setup(char *__unused)
818{
819 pd_ignore_unused = true;
820 return 1;
821}
822__setup("pd_ignore_unused", pd_ignore_unused_setup);
823
824/**
825 * genpd_power_off_unused - Power off all PM domains with no devices in use.
826 */
827static int __init genpd_power_off_unused(void)
828{
829 struct generic_pm_domain *genpd;
830
831 if (pd_ignore_unused) {
832 pr_warn("genpd: Not disabling unused power domains\n");
833 return 0;
834 }
835
836 mutex_lock(&gpd_list_lock);
837
838 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
839 genpd_queue_power_off_work(genpd);
840
841 mutex_unlock(&gpd_list_lock);
842
843 return 0;
844}
845late_initcall(genpd_power_off_unused);
846
847#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
848
849static bool genpd_present(const struct generic_pm_domain *genpd)
850{
851 const struct generic_pm_domain *gpd;
852
853 if (IS_ERR_OR_NULL(genpd))
854 return false;
855
856 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
857 if (gpd == genpd)
858 return true;
859
860 return false;
861}
862
863#endif
864
865#ifdef CONFIG_PM_SLEEP
866
867/**
868 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
869 * @genpd: PM domain to power off, if possible.
870 * @use_lock: use the lock.
871 * @depth: nesting count for lockdep.
872 *
873 * Check if the given PM domain can be powered off (during system suspend or
874 * hibernation) and do that if so. Also, in that case propagate to its masters.
875 *
876 * This function is only called in "noirq" and "syscore" stages of system power
877 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
878 * these cases the lock must be held.
879 */
880static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
881 unsigned int depth)
882{
883 struct gpd_link *link;
884
885 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
886 return;
887
888 if (genpd->suspended_count != genpd->device_count
889 || atomic_read(&genpd->sd_count) > 0)
890 return;
891
892 /* Choose the deepest state when suspending */
893 genpd->state_idx = genpd->state_count - 1;
894 if (_genpd_power_off(genpd, false))
895 return;
896
897 genpd->status = GPD_STATE_POWER_OFF;
898
899 list_for_each_entry(link, &genpd->slave_links, slave_node) {
900 genpd_sd_counter_dec(link->master);
901
902 if (use_lock)
903 genpd_lock_nested(link->master, depth + 1);
904
905 genpd_sync_power_off(link->master, use_lock, depth + 1);
906
907 if (use_lock)
908 genpd_unlock(link->master);
909 }
910}
911
912/**
913 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
914 * @genpd: PM domain to power on.
915 * @use_lock: use the lock.
916 * @depth: nesting count for lockdep.
917 *
918 * This function is only called in "noirq" and "syscore" stages of system power
919 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
920 * these cases the lock must be held.
921 */
922static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
923 unsigned int depth)
924{
925 struct gpd_link *link;
926
927 if (genpd_status_on(genpd))
928 return;
929
930 list_for_each_entry(link, &genpd->slave_links, slave_node) {
931 genpd_sd_counter_inc(link->master);
932
933 if (use_lock)
934 genpd_lock_nested(link->master, depth + 1);
935
936 genpd_sync_power_on(link->master, use_lock, depth + 1);
937
938 if (use_lock)
939 genpd_unlock(link->master);
940 }
941
942 _genpd_power_on(genpd, false);
943
944 genpd->status = GPD_STATE_ACTIVE;
945}
946
947/**
948 * resume_needed - Check whether to resume a device before system suspend.
949 * @dev: Device to check.
950 * @genpd: PM domain the device belongs to.
951 *
952 * There are two cases in which a device that can wake up the system from sleep
953 * states should be resumed by genpd_prepare(): (1) if the device is enabled
954 * to wake up the system and it has to remain active for this purpose while the
955 * system is in the sleep state and (2) if the device is not enabled to wake up
956 * the system from sleep states and it generally doesn't generate wakeup signals
957 * by itself (those signals are generated on its behalf by other parts of the
958 * system). In the latter case it may be necessary to reconfigure the device's
959 * wakeup settings during system suspend, because it may have been set up to
960 * signal remote wakeup from the system's working state as needed by runtime PM.
961 * Return 'true' in either of the above cases.
962 */
963static bool resume_needed(struct device *dev,
964 const struct generic_pm_domain *genpd)
965{
966 bool active_wakeup;
967
968 if (!device_can_wakeup(dev))
969 return false;
970
971 active_wakeup = genpd_is_active_wakeup(genpd);
972 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
973}
974
975/**
976 * genpd_prepare - Start power transition of a device in a PM domain.
977 * @dev: Device to start the transition of.
978 *
979 * Start a power transition of a device (during a system-wide power transition)
980 * under the assumption that its pm_domain field points to the domain member of
981 * an object of type struct generic_pm_domain representing a PM domain
982 * consisting of I/O devices.
983 */
984static int genpd_prepare(struct device *dev)
985{
986 struct generic_pm_domain *genpd;
987 int ret;
988
989 dev_dbg(dev, "%s()\n", __func__);
990
991 genpd = dev_to_genpd(dev);
992 if (IS_ERR(genpd))
993 return -EINVAL;
994
995 /*
996 * If a wakeup request is pending for the device, it should be woken up
997 * at this point and a system wakeup event should be reported if it's
998 * set up to wake up the system from sleep states.
999 */
1000 if (resume_needed(dev, genpd))
1001 pm_runtime_resume(dev);
1002
1003 genpd_lock(genpd);
1004
1005 if (genpd->prepared_count++ == 0)
1006 genpd->suspended_count = 0;
1007
1008 genpd_unlock(genpd);
1009
1010 ret = pm_generic_prepare(dev);
1011 if (ret < 0) {
1012 genpd_lock(genpd);
1013
1014 genpd->prepared_count--;
1015
1016 genpd_unlock(genpd);
1017 }
1018
1019 /* Never return 1, as genpd don't cope with the direct_complete path. */
1020 return ret >= 0 ? 0 : ret;
1021}
1022
1023/**
1024 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1025 * I/O pm domain.
1026 * @dev: Device to suspend.
1027 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1028 *
1029 * Stop the device and remove power from the domain if all devices in it have
1030 * been stopped.
1031 */
1032static int genpd_finish_suspend(struct device *dev, bool poweroff)
1033{
1034 struct generic_pm_domain *genpd;
1035 int ret = 0;
1036
1037 genpd = dev_to_genpd(dev);
1038 if (IS_ERR(genpd))
1039 return -EINVAL;
1040
1041 if (poweroff)
1042 ret = pm_generic_poweroff_noirq(dev);
1043 else
1044 ret = pm_generic_suspend_noirq(dev);
1045 if (ret)
1046 return ret;
1047
1048 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1049 return 0;
1050
1051 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1052 !pm_runtime_status_suspended(dev)) {
1053 ret = genpd_stop_dev(genpd, dev);
1054 if (ret) {
1055 if (poweroff)
1056 pm_generic_restore_noirq(dev);
1057 else
1058 pm_generic_resume_noirq(dev);
1059 return ret;
1060 }
1061 }
1062
1063 genpd_lock(genpd);
1064 genpd->suspended_count++;
1065 genpd_sync_power_off(genpd, true, 0);
1066 genpd_unlock(genpd);
1067
1068 return 0;
1069}
1070
1071/**
1072 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1073 * @dev: Device to suspend.
1074 *
1075 * Stop the device and remove power from the domain if all devices in it have
1076 * been stopped.
1077 */
1078static int genpd_suspend_noirq(struct device *dev)
1079{
1080 dev_dbg(dev, "%s()\n", __func__);
1081
1082 return genpd_finish_suspend(dev, false);
1083}
1084
1085/**
1086 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1087 * @dev: Device to resume.
1088 *
1089 * Restore power to the device's PM domain, if necessary, and start the device.
1090 */
1091static int genpd_resume_noirq(struct device *dev)
1092{
1093 struct generic_pm_domain *genpd;
1094 int ret;
1095
1096 dev_dbg(dev, "%s()\n", __func__);
1097
1098 genpd = dev_to_genpd(dev);
1099 if (IS_ERR(genpd))
1100 return -EINVAL;
1101
1102 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1103 return pm_generic_resume_noirq(dev);
1104
1105 genpd_lock(genpd);
1106 genpd_sync_power_on(genpd, true, 0);
1107 genpd->suspended_count--;
1108 genpd_unlock(genpd);
1109
1110 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1111 !pm_runtime_status_suspended(dev)) {
1112 ret = genpd_start_dev(genpd, dev);
1113 if (ret)
1114 return ret;
1115 }
1116
1117 return pm_generic_resume_noirq(dev);
1118}
1119
1120/**
1121 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1122 * @dev: Device to freeze.
1123 *
1124 * Carry out a late freeze of a device under the assumption that its
1125 * pm_domain field points to the domain member of an object of type
1126 * struct generic_pm_domain representing a power domain consisting of I/O
1127 * devices.
1128 */
1129static int genpd_freeze_noirq(struct device *dev)
1130{
1131 const struct generic_pm_domain *genpd;
1132 int ret = 0;
1133
1134 dev_dbg(dev, "%s()\n", __func__);
1135
1136 genpd = dev_to_genpd(dev);
1137 if (IS_ERR(genpd))
1138 return -EINVAL;
1139
1140 ret = pm_generic_freeze_noirq(dev);
1141 if (ret)
1142 return ret;
1143
1144 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1145 !pm_runtime_status_suspended(dev))
1146 ret = genpd_stop_dev(genpd, dev);
1147
1148 return ret;
1149}
1150
1151/**
1152 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1153 * @dev: Device to thaw.
1154 *
1155 * Start the device, unless power has been removed from the domain already
1156 * before the system transition.
1157 */
1158static int genpd_thaw_noirq(struct device *dev)
1159{
1160 const struct generic_pm_domain *genpd;
1161 int ret = 0;
1162
1163 dev_dbg(dev, "%s()\n", __func__);
1164
1165 genpd = dev_to_genpd(dev);
1166 if (IS_ERR(genpd))
1167 return -EINVAL;
1168
1169 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1170 !pm_runtime_status_suspended(dev)) {
1171 ret = genpd_start_dev(genpd, dev);
1172 if (ret)
1173 return ret;
1174 }
1175
1176 return pm_generic_thaw_noirq(dev);
1177}
1178
1179/**
1180 * genpd_poweroff_noirq - Completion of hibernation of device in an
1181 * I/O PM domain.
1182 * @dev: Device to poweroff.
1183 *
1184 * Stop the device and remove power from the domain if all devices in it have
1185 * been stopped.
1186 */
1187static int genpd_poweroff_noirq(struct device *dev)
1188{
1189 dev_dbg(dev, "%s()\n", __func__);
1190
1191 return genpd_finish_suspend(dev, true);
1192}
1193
1194/**
1195 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1196 * @dev: Device to resume.
1197 *
1198 * Make sure the domain will be in the same power state as before the
1199 * hibernation the system is resuming from and start the device if necessary.
1200 */
1201static int genpd_restore_noirq(struct device *dev)
1202{
1203 struct generic_pm_domain *genpd;
1204 int ret = 0;
1205
1206 dev_dbg(dev, "%s()\n", __func__);
1207
1208 genpd = dev_to_genpd(dev);
1209 if (IS_ERR(genpd))
1210 return -EINVAL;
1211
1212 /*
1213 * At this point suspended_count == 0 means we are being run for the
1214 * first time for the given domain in the present cycle.
1215 */
1216 genpd_lock(genpd);
1217 if (genpd->suspended_count++ == 0)
1218 /*
1219 * The boot kernel might put the domain into arbitrary state,
1220 * so make it appear as powered off to genpd_sync_power_on(),
1221 * so that it tries to power it on in case it was really off.
1222 */
1223 genpd->status = GPD_STATE_POWER_OFF;
1224
1225 genpd_sync_power_on(genpd, true, 0);
1226 genpd_unlock(genpd);
1227
1228 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1229 !pm_runtime_status_suspended(dev)) {
1230 ret = genpd_start_dev(genpd, dev);
1231 if (ret)
1232 return ret;
1233 }
1234
1235 return pm_generic_restore_noirq(dev);
1236}
1237
1238/**
1239 * genpd_complete - Complete power transition of a device in a power domain.
1240 * @dev: Device to complete the transition of.
1241 *
1242 * Complete a power transition of a device (during a system-wide power
1243 * transition) under the assumption that its pm_domain field points to the
1244 * domain member of an object of type struct generic_pm_domain representing
1245 * a power domain consisting of I/O devices.
1246 */
1247static void genpd_complete(struct device *dev)
1248{
1249 struct generic_pm_domain *genpd;
1250
1251 dev_dbg(dev, "%s()\n", __func__);
1252
1253 genpd = dev_to_genpd(dev);
1254 if (IS_ERR(genpd))
1255 return;
1256
1257 pm_generic_complete(dev);
1258
1259 genpd_lock(genpd);
1260
1261 genpd->prepared_count--;
1262 if (!genpd->prepared_count)
1263 genpd_queue_power_off_work(genpd);
1264
1265 genpd_unlock(genpd);
1266}
1267
1268/**
1269 * genpd_syscore_switch - Switch power during system core suspend or resume.
1270 * @dev: Device that normally is marked as "always on" to switch power for.
1271 *
1272 * This routine may only be called during the system core (syscore) suspend or
1273 * resume phase for devices whose "always on" flags are set.
1274 */
1275static void genpd_syscore_switch(struct device *dev, bool suspend)
1276{
1277 struct generic_pm_domain *genpd;
1278
1279 genpd = dev_to_genpd(dev);
1280 if (!genpd_present(genpd))
1281 return;
1282
1283 if (suspend) {
1284 genpd->suspended_count++;
1285 genpd_sync_power_off(genpd, false, 0);
1286 } else {
1287 genpd_sync_power_on(genpd, false, 0);
1288 genpd->suspended_count--;
1289 }
1290}
1291
1292void pm_genpd_syscore_poweroff(struct device *dev)
1293{
1294 genpd_syscore_switch(dev, true);
1295}
1296EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1297
1298void pm_genpd_syscore_poweron(struct device *dev)
1299{
1300 genpd_syscore_switch(dev, false);
1301}
1302EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1303
1304#else /* !CONFIG_PM_SLEEP */
1305
1306#define genpd_prepare NULL
1307#define genpd_suspend_noirq NULL
1308#define genpd_resume_noirq NULL
1309#define genpd_freeze_noirq NULL
1310#define genpd_thaw_noirq NULL
1311#define genpd_poweroff_noirq NULL
1312#define genpd_restore_noirq NULL
1313#define genpd_complete NULL
1314
1315#endif /* CONFIG_PM_SLEEP */
1316
1317static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1318 struct generic_pm_domain *genpd,
1319 struct gpd_timing_data *td)
1320{
1321 struct generic_pm_domain_data *gpd_data;
1322 int ret;
1323
1324 ret = dev_pm_get_subsys_data(dev);
1325 if (ret)
1326 return ERR_PTR(ret);
1327
1328 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1329 if (!gpd_data) {
1330 ret = -ENOMEM;
1331 goto err_put;
1332 }
1333
1334 if (td)
1335 gpd_data->td = *td;
1336
1337 gpd_data->base.dev = dev;
1338 gpd_data->td.constraint_changed = true;
1339 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1340 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1341
1342 spin_lock_irq(&dev->power.lock);
1343
1344 if (dev->power.subsys_data->domain_data) {
1345 ret = -EINVAL;
1346 goto err_free;
1347 }
1348
1349 dev->power.subsys_data->domain_data = &gpd_data->base;
1350
1351 spin_unlock_irq(&dev->power.lock);
1352
1353 return gpd_data;
1354
1355 err_free:
1356 spin_unlock_irq(&dev->power.lock);
1357 kfree(gpd_data);
1358 err_put:
1359 dev_pm_put_subsys_data(dev);
1360 return ERR_PTR(ret);
1361}
1362
1363static void genpd_free_dev_data(struct device *dev,
1364 struct generic_pm_domain_data *gpd_data)
1365{
1366 spin_lock_irq(&dev->power.lock);
1367
1368 dev->power.subsys_data->domain_data = NULL;
1369
1370 spin_unlock_irq(&dev->power.lock);
1371
1372 kfree(gpd_data);
1373 dev_pm_put_subsys_data(dev);
1374}
1375
1376static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1377 struct gpd_timing_data *td)
1378{
1379 struct generic_pm_domain_data *gpd_data;
1380 int ret = 0;
1381
1382 dev_dbg(dev, "%s()\n", __func__);
1383
1384 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1385 return -EINVAL;
1386
1387 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1388 if (IS_ERR(gpd_data))
1389 return PTR_ERR(gpd_data);
1390
1391 genpd_lock(genpd);
1392
1393 if (genpd->prepared_count > 0) {
1394 ret = -EAGAIN;
1395 goto out;
1396 }
1397
1398 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1399 if (ret)
1400 goto out;
1401
1402 dev_pm_domain_set(dev, &genpd->domain);
1403
1404 genpd->device_count++;
1405 genpd->max_off_time_changed = true;
1406
1407 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1408
1409 out:
1410 genpd_unlock(genpd);
1411
1412 if (ret)
1413 genpd_free_dev_data(dev, gpd_data);
1414 else
1415 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1416
1417 return ret;
1418}
1419
1420/**
1421 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1422 * @genpd: PM domain to add the device to.
1423 * @dev: Device to be added.
1424 * @td: Set of PM QoS timing parameters to attach to the device.
1425 */
1426int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1427 struct gpd_timing_data *td)
1428{
1429 int ret;
1430
1431 mutex_lock(&gpd_list_lock);
1432 ret = genpd_add_device(genpd, dev, td);
1433 mutex_unlock(&gpd_list_lock);
1434
1435 return ret;
1436}
1437EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1438
1439static int genpd_remove_device(struct generic_pm_domain *genpd,
1440 struct device *dev)
1441{
1442 struct generic_pm_domain_data *gpd_data;
1443 struct pm_domain_data *pdd;
1444 int ret = 0;
1445
1446 dev_dbg(dev, "%s()\n", __func__);
1447
1448 pdd = dev->power.subsys_data->domain_data;
1449 gpd_data = to_gpd_data(pdd);
1450 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1451
1452 genpd_lock(genpd);
1453
1454 if (genpd->prepared_count > 0) {
1455 ret = -EAGAIN;
1456 goto out;
1457 }
1458
1459 genpd->device_count--;
1460 genpd->max_off_time_changed = true;
1461
1462 if (genpd->detach_dev)
1463 genpd->detach_dev(genpd, dev);
1464
1465 dev_pm_domain_set(dev, NULL);
1466
1467 list_del_init(&pdd->list_node);
1468
1469 genpd_unlock(genpd);
1470
1471 genpd_free_dev_data(dev, gpd_data);
1472
1473 return 0;
1474
1475 out:
1476 genpd_unlock(genpd);
1477 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1478
1479 return ret;
1480}
1481
1482/**
1483 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1484 * @genpd: PM domain to remove the device from.
1485 * @dev: Device to be removed.
1486 */
1487int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1488 struct device *dev)
1489{
1490 if (!genpd || genpd != genpd_lookup_dev(dev))
1491 return -EINVAL;
1492
1493 return genpd_remove_device(genpd, dev);
1494}
1495EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1496
1497static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1498 struct generic_pm_domain *subdomain)
1499{
1500 struct gpd_link *link, *itr;
1501 int ret = 0;
1502
1503 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1504 || genpd == subdomain)
1505 return -EINVAL;
1506
1507 /*
1508 * If the domain can be powered on/off in an IRQ safe
1509 * context, ensure that the subdomain can also be
1510 * powered on/off in that context.
1511 */
1512 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1513 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1514 genpd->name, subdomain->name);
1515 return -EINVAL;
1516 }
1517
1518 link = kzalloc(sizeof(*link), GFP_KERNEL);
1519 if (!link)
1520 return -ENOMEM;
1521
1522 genpd_lock(subdomain);
1523 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1524
1525 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1526 ret = -EINVAL;
1527 goto out;
1528 }
1529
1530 list_for_each_entry(itr, &genpd->master_links, master_node) {
1531 if (itr->slave == subdomain && itr->master == genpd) {
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535 }
1536
1537 link->master = genpd;
1538 list_add_tail(&link->master_node, &genpd->master_links);
1539 link->slave = subdomain;
1540 list_add_tail(&link->slave_node, &subdomain->slave_links);
1541 if (genpd_status_on(subdomain))
1542 genpd_sd_counter_inc(genpd);
1543
1544 out:
1545 genpd_unlock(genpd);
1546 genpd_unlock(subdomain);
1547 if (ret)
1548 kfree(link);
1549 return ret;
1550}
1551
1552/**
1553 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1554 * @genpd: Master PM domain to add the subdomain to.
1555 * @subdomain: Subdomain to be added.
1556 */
1557int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1558 struct generic_pm_domain *subdomain)
1559{
1560 int ret;
1561
1562 mutex_lock(&gpd_list_lock);
1563 ret = genpd_add_subdomain(genpd, subdomain);
1564 mutex_unlock(&gpd_list_lock);
1565
1566 return ret;
1567}
1568EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1569
1570/**
1571 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1572 * @genpd: Master PM domain to remove the subdomain from.
1573 * @subdomain: Subdomain to be removed.
1574 */
1575int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1576 struct generic_pm_domain *subdomain)
1577{
1578 struct gpd_link *l, *link;
1579 int ret = -EINVAL;
1580
1581 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1582 return -EINVAL;
1583
1584 genpd_lock(subdomain);
1585 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1586
1587 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1588 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1589 subdomain->name);
1590 ret = -EBUSY;
1591 goto out;
1592 }
1593
1594 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1595 if (link->slave != subdomain)
1596 continue;
1597
1598 list_del(&link->master_node);
1599 list_del(&link->slave_node);
1600 kfree(link);
1601 if (genpd_status_on(subdomain))
1602 genpd_sd_counter_dec(genpd);
1603
1604 ret = 0;
1605 break;
1606 }
1607
1608out:
1609 genpd_unlock(genpd);
1610 genpd_unlock(subdomain);
1611
1612 return ret;
1613}
1614EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1615
1616static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1617{
1618 struct genpd_power_state *state;
1619
1620 state = kzalloc(sizeof(*state), GFP_KERNEL);
1621 if (!state)
1622 return -ENOMEM;
1623
1624 genpd->states = state;
1625 genpd->state_count = 1;
1626 genpd->free = state;
1627
1628 return 0;
1629}
1630
1631static void genpd_lock_init(struct generic_pm_domain *genpd)
1632{
1633 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1634 spin_lock_init(&genpd->slock);
1635 genpd->lock_ops = &genpd_spin_ops;
1636 } else {
1637 mutex_init(&genpd->mlock);
1638 genpd->lock_ops = &genpd_mtx_ops;
1639 }
1640}
1641
1642/**
1643 * pm_genpd_init - Initialize a generic I/O PM domain object.
1644 * @genpd: PM domain object to initialize.
1645 * @gov: PM domain governor to associate with the domain (may be NULL).
1646 * @is_off: Initial value of the domain's power_is_off field.
1647 *
1648 * Returns 0 on successful initialization, else a negative error code.
1649 */
1650int pm_genpd_init(struct generic_pm_domain *genpd,
1651 struct dev_power_governor *gov, bool is_off)
1652{
1653 int ret;
1654
1655 if (IS_ERR_OR_NULL(genpd))
1656 return -EINVAL;
1657
1658 INIT_LIST_HEAD(&genpd->master_links);
1659 INIT_LIST_HEAD(&genpd->slave_links);
1660 INIT_LIST_HEAD(&genpd->dev_list);
1661 genpd_lock_init(genpd);
1662 genpd->gov = gov;
1663 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1664 atomic_set(&genpd->sd_count, 0);
1665 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1666 genpd->device_count = 0;
1667 genpd->max_off_time_ns = -1;
1668 genpd->max_off_time_changed = true;
1669 genpd->provider = NULL;
1670 genpd->has_provider = false;
1671 genpd->accounting_time = ktime_get();
1672 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1673 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1674 genpd->domain.ops.prepare = genpd_prepare;
1675 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1676 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1677 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1678 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1679 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1680 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1681 genpd->domain.ops.complete = genpd_complete;
1682
1683 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1684 genpd->dev_ops.stop = pm_clk_suspend;
1685 genpd->dev_ops.start = pm_clk_resume;
1686 }
1687
1688 /* Always-on domains must be powered on at initialization. */
1689 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1690 return -EINVAL;
1691
1692 /* Use only one "off" state if there were no states declared */
1693 if (genpd->state_count == 0) {
1694 ret = genpd_set_default_power_state(genpd);
1695 if (ret)
1696 return ret;
1697 }
1698
1699 mutex_lock(&gpd_list_lock);
1700 list_add(&genpd->gpd_list_node, &gpd_list);
1701 mutex_unlock(&gpd_list_lock);
1702
1703 return 0;
1704}
1705EXPORT_SYMBOL_GPL(pm_genpd_init);
1706
1707static int genpd_remove(struct generic_pm_domain *genpd)
1708{
1709 struct gpd_link *l, *link;
1710
1711 if (IS_ERR_OR_NULL(genpd))
1712 return -EINVAL;
1713
1714 genpd_lock(genpd);
1715
1716 if (genpd->has_provider) {
1717 genpd_unlock(genpd);
1718 pr_err("Provider present, unable to remove %s\n", genpd->name);
1719 return -EBUSY;
1720 }
1721
1722 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1723 genpd_unlock(genpd);
1724 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1725 return -EBUSY;
1726 }
1727
1728 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1729 list_del(&link->master_node);
1730 list_del(&link->slave_node);
1731 kfree(link);
1732 }
1733
1734 list_del(&genpd->gpd_list_node);
1735 genpd_unlock(genpd);
1736 cancel_work_sync(&genpd->power_off_work);
1737 kfree(genpd->free);
1738 pr_debug("%s: removed %s\n", __func__, genpd->name);
1739
1740 return 0;
1741}
1742
1743/**
1744 * pm_genpd_remove - Remove a generic I/O PM domain
1745 * @genpd: Pointer to PM domain that is to be removed.
1746 *
1747 * To remove the PM domain, this function:
1748 * - Removes the PM domain as a subdomain to any parent domains,
1749 * if it was added.
1750 * - Removes the PM domain from the list of registered PM domains.
1751 *
1752 * The PM domain will only be removed, if the associated provider has
1753 * been removed, it is not a parent to any other PM domain and has no
1754 * devices associated with it.
1755 */
1756int pm_genpd_remove(struct generic_pm_domain *genpd)
1757{
1758 int ret;
1759
1760 mutex_lock(&gpd_list_lock);
1761 ret = genpd_remove(genpd);
1762 mutex_unlock(&gpd_list_lock);
1763
1764 return ret;
1765}
1766EXPORT_SYMBOL_GPL(pm_genpd_remove);
1767
1768#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1769
1770/*
1771 * Device Tree based PM domain providers.
1772 *
1773 * The code below implements generic device tree based PM domain providers that
1774 * bind device tree nodes with generic PM domains registered in the system.
1775 *
1776 * Any driver that registers generic PM domains and needs to support binding of
1777 * devices to these domains is supposed to register a PM domain provider, which
1778 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1779 *
1780 * Two simple mapping functions have been provided for convenience:
1781 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1782 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1783 * index.
1784 */
1785
1786/**
1787 * struct of_genpd_provider - PM domain provider registration structure
1788 * @link: Entry in global list of PM domain providers
1789 * @node: Pointer to device tree node of PM domain provider
1790 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1791 * into a PM domain.
1792 * @data: context pointer to be passed into @xlate callback
1793 */
1794struct of_genpd_provider {
1795 struct list_head link;
1796 struct device_node *node;
1797 genpd_xlate_t xlate;
1798 void *data;
1799};
1800
1801/* List of registered PM domain providers. */
1802static LIST_HEAD(of_genpd_providers);
1803/* Mutex to protect the list above. */
1804static DEFINE_MUTEX(of_genpd_mutex);
1805
1806/**
1807 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1808 * @genpdspec: OF phandle args to map into a PM domain
1809 * @data: xlate function private data - pointer to struct generic_pm_domain
1810 *
1811 * This is a generic xlate function that can be used to model PM domains that
1812 * have their own device tree nodes. The private data of xlate function needs
1813 * to be a valid pointer to struct generic_pm_domain.
1814 */
1815static struct generic_pm_domain *genpd_xlate_simple(
1816 struct of_phandle_args *genpdspec,
1817 void *data)
1818{
1819 return data;
1820}
1821
1822/**
1823 * genpd_xlate_onecell() - Xlate function using a single index.
1824 * @genpdspec: OF phandle args to map into a PM domain
1825 * @data: xlate function private data - pointer to struct genpd_onecell_data
1826 *
1827 * This is a generic xlate function that can be used to model simple PM domain
1828 * controllers that have one device tree node and provide multiple PM domains.
1829 * A single cell is used as an index into an array of PM domains specified in
1830 * the genpd_onecell_data struct when registering the provider.
1831 */
1832static struct generic_pm_domain *genpd_xlate_onecell(
1833 struct of_phandle_args *genpdspec,
1834 void *data)
1835{
1836 struct genpd_onecell_data *genpd_data = data;
1837 unsigned int idx = genpdspec->args[0];
1838
1839 if (genpdspec->args_count != 1)
1840 return ERR_PTR(-EINVAL);
1841
1842 if (idx >= genpd_data->num_domains) {
1843 pr_err("%s: invalid domain index %u\n", __func__, idx);
1844 return ERR_PTR(-EINVAL);
1845 }
1846
1847 if (!genpd_data->domains[idx])
1848 return ERR_PTR(-ENOENT);
1849
1850 return genpd_data->domains[idx];
1851}
1852
1853/**
1854 * genpd_add_provider() - Register a PM domain provider for a node
1855 * @np: Device node pointer associated with the PM domain provider.
1856 * @xlate: Callback for decoding PM domain from phandle arguments.
1857 * @data: Context pointer for @xlate callback.
1858 */
1859static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1860 void *data)
1861{
1862 struct of_genpd_provider *cp;
1863
1864 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1865 if (!cp)
1866 return -ENOMEM;
1867
1868 cp->node = of_node_get(np);
1869 cp->data = data;
1870 cp->xlate = xlate;
1871
1872 mutex_lock(&of_genpd_mutex);
1873 list_add(&cp->link, &of_genpd_providers);
1874 mutex_unlock(&of_genpd_mutex);
1875 pr_debug("Added domain provider from %pOF\n", np);
1876
1877 return 0;
1878}
1879
1880/**
1881 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1882 * @np: Device node pointer associated with the PM domain provider.
1883 * @genpd: Pointer to PM domain associated with the PM domain provider.
1884 */
1885int of_genpd_add_provider_simple(struct device_node *np,
1886 struct generic_pm_domain *genpd)
1887{
1888 int ret = -EINVAL;
1889
1890 if (!np || !genpd)
1891 return -EINVAL;
1892
1893 mutex_lock(&gpd_list_lock);
1894
1895 if (genpd_present(genpd)) {
1896 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1897 if (!ret) {
1898 genpd->provider = &np->fwnode;
1899 genpd->has_provider = true;
1900 }
1901 }
1902
1903 mutex_unlock(&gpd_list_lock);
1904
1905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1908
1909/**
1910 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1911 * @np: Device node pointer associated with the PM domain provider.
1912 * @data: Pointer to the data associated with the PM domain provider.
1913 */
1914int of_genpd_add_provider_onecell(struct device_node *np,
1915 struct genpd_onecell_data *data)
1916{
1917 unsigned int i;
1918 int ret = -EINVAL;
1919
1920 if (!np || !data)
1921 return -EINVAL;
1922
1923 mutex_lock(&gpd_list_lock);
1924
1925 if (!data->xlate)
1926 data->xlate = genpd_xlate_onecell;
1927
1928 for (i = 0; i < data->num_domains; i++) {
1929 if (!data->domains[i])
1930 continue;
1931 if (!genpd_present(data->domains[i]))
1932 goto error;
1933
1934 data->domains[i]->provider = &np->fwnode;
1935 data->domains[i]->has_provider = true;
1936 }
1937
1938 ret = genpd_add_provider(np, data->xlate, data);
1939 if (ret < 0)
1940 goto error;
1941
1942 mutex_unlock(&gpd_list_lock);
1943
1944 return 0;
1945
1946error:
1947 while (i--) {
1948 if (!data->domains[i])
1949 continue;
1950 data->domains[i]->provider = NULL;
1951 data->domains[i]->has_provider = false;
1952 }
1953
1954 mutex_unlock(&gpd_list_lock);
1955
1956 return ret;
1957}
1958EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1959
1960/**
1961 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1962 * @np: Device node pointer associated with the PM domain provider
1963 */
1964void of_genpd_del_provider(struct device_node *np)
1965{
1966 struct of_genpd_provider *cp, *tmp;
1967 struct generic_pm_domain *gpd;
1968
1969 mutex_lock(&gpd_list_lock);
1970 mutex_lock(&of_genpd_mutex);
1971 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
1972 if (cp->node == np) {
1973 /*
1974 * For each PM domain associated with the
1975 * provider, set the 'has_provider' to false
1976 * so that the PM domain can be safely removed.
1977 */
1978 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
1979 if (gpd->provider == &np->fwnode)
1980 gpd->has_provider = false;
1981
1982 list_del(&cp->link);
1983 of_node_put(cp->node);
1984 kfree(cp);
1985 break;
1986 }
1987 }
1988 mutex_unlock(&of_genpd_mutex);
1989 mutex_unlock(&gpd_list_lock);
1990}
1991EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1992
1993/**
1994 * genpd_get_from_provider() - Look-up PM domain
1995 * @genpdspec: OF phandle args to use for look-up
1996 *
1997 * Looks for a PM domain provider under the node specified by @genpdspec and if
1998 * found, uses xlate function of the provider to map phandle args to a PM
1999 * domain.
2000 *
2001 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2002 * on failure.
2003 */
2004static struct generic_pm_domain *genpd_get_from_provider(
2005 struct of_phandle_args *genpdspec)
2006{
2007 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2008 struct of_genpd_provider *provider;
2009
2010 if (!genpdspec)
2011 return ERR_PTR(-EINVAL);
2012
2013 mutex_lock(&of_genpd_mutex);
2014
2015 /* Check if we have such a provider in our array */
2016 list_for_each_entry(provider, &of_genpd_providers, link) {
2017 if (provider->node == genpdspec->np)
2018 genpd = provider->xlate(genpdspec, provider->data);
2019 if (!IS_ERR(genpd))
2020 break;
2021 }
2022
2023 mutex_unlock(&of_genpd_mutex);
2024
2025 return genpd;
2026}
2027
2028/**
2029 * of_genpd_add_device() - Add a device to an I/O PM domain
2030 * @genpdspec: OF phandle args to use for look-up PM domain
2031 * @dev: Device to be added.
2032 *
2033 * Looks-up an I/O PM domain based upon phandle args provided and adds
2034 * the device to the PM domain. Returns a negative error code on failure.
2035 */
2036int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2037{
2038 struct generic_pm_domain *genpd;
2039 int ret;
2040
2041 mutex_lock(&gpd_list_lock);
2042
2043 genpd = genpd_get_from_provider(genpdspec);
2044 if (IS_ERR(genpd)) {
2045 ret = PTR_ERR(genpd);
2046 goto out;
2047 }
2048
2049 ret = genpd_add_device(genpd, dev, NULL);
2050
2051out:
2052 mutex_unlock(&gpd_list_lock);
2053
2054 return ret;
2055}
2056EXPORT_SYMBOL_GPL(of_genpd_add_device);
2057
2058/**
2059 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2060 * @parent_spec: OF phandle args to use for parent PM domain look-up
2061 * @subdomain_spec: OF phandle args to use for subdomain look-up
2062 *
2063 * Looks-up a parent PM domain and subdomain based upon phandle args
2064 * provided and adds the subdomain to the parent PM domain. Returns a
2065 * negative error code on failure.
2066 */
2067int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2068 struct of_phandle_args *subdomain_spec)
2069{
2070 struct generic_pm_domain *parent, *subdomain;
2071 int ret;
2072
2073 mutex_lock(&gpd_list_lock);
2074
2075 parent = genpd_get_from_provider(parent_spec);
2076 if (IS_ERR(parent)) {
2077 ret = PTR_ERR(parent);
2078 goto out;
2079 }
2080
2081 subdomain = genpd_get_from_provider(subdomain_spec);
2082 if (IS_ERR(subdomain)) {
2083 ret = PTR_ERR(subdomain);
2084 goto out;
2085 }
2086
2087 ret = genpd_add_subdomain(parent, subdomain);
2088
2089out:
2090 mutex_unlock(&gpd_list_lock);
2091
2092 return ret;
2093}
2094EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2095
2096/**
2097 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2098 * @provider: Pointer to device structure associated with provider
2099 *
2100 * Find the last PM domain that was added by a particular provider and
2101 * remove this PM domain from the list of PM domains. The provider is
2102 * identified by the 'provider' device structure that is passed. The PM
2103 * domain will only be removed, if the provider associated with domain
2104 * has been removed.
2105 *
2106 * Returns a valid pointer to struct generic_pm_domain on success or
2107 * ERR_PTR() on failure.
2108 */
2109struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2110{
2111 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2112 int ret;
2113
2114 if (IS_ERR_OR_NULL(np))
2115 return ERR_PTR(-EINVAL);
2116
2117 mutex_lock(&gpd_list_lock);
2118 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2119 if (gpd->provider == &np->fwnode) {
2120 ret = genpd_remove(gpd);
2121 genpd = ret ? ERR_PTR(ret) : gpd;
2122 break;
2123 }
2124 }
2125 mutex_unlock(&gpd_list_lock);
2126
2127 return genpd;
2128}
2129EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2130
2131/**
2132 * genpd_dev_pm_detach - Detach a device from its PM domain.
2133 * @dev: Device to detach.
2134 * @power_off: Currently not used
2135 *
2136 * Try to locate a corresponding generic PM domain, which the device was
2137 * attached to previously. If such is found, the device is detached from it.
2138 */
2139static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2140{
2141 struct generic_pm_domain *pd;
2142 unsigned int i;
2143 int ret = 0;
2144
2145 pd = dev_to_genpd(dev);
2146 if (IS_ERR(pd))
2147 return;
2148
2149 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2150
2151 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2152 ret = genpd_remove_device(pd, dev);
2153 if (ret != -EAGAIN)
2154 break;
2155
2156 mdelay(i);
2157 cond_resched();
2158 }
2159
2160 if (ret < 0) {
2161 dev_err(dev, "failed to remove from PM domain %s: %d",
2162 pd->name, ret);
2163 return;
2164 }
2165
2166 /* Check if PM domain can be powered off after removing this device. */
2167 genpd_queue_power_off_work(pd);
2168}
2169
2170static void genpd_dev_pm_sync(struct device *dev)
2171{
2172 struct generic_pm_domain *pd;
2173
2174 pd = dev_to_genpd(dev);
2175 if (IS_ERR(pd))
2176 return;
2177
2178 genpd_queue_power_off_work(pd);
2179}
2180
2181/**
2182 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2183 * @dev: Device to attach.
2184 *
2185 * Parse device's OF node to find a PM domain specifier. If such is found,
2186 * attaches the device to retrieved pm_domain ops.
2187 *
2188 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2189 * backwards compatibility with existing DTBs.
2190 *
2191 * Returns 0 on successfully attached PM domain or negative error code. Note
2192 * that if a power-domain exists for the device, but it cannot be found or
2193 * turned on, then return -EPROBE_DEFER to ensure that the device is not
2194 * probed and to re-try again later.
2195 */
2196int genpd_dev_pm_attach(struct device *dev)
2197{
2198 struct of_phandle_args pd_args;
2199 struct generic_pm_domain *pd;
2200 unsigned int i;
2201 int ret;
2202
2203 if (!dev->of_node)
2204 return -ENODEV;
2205
2206 if (dev->pm_domain)
2207 return -EEXIST;
2208
2209 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2210 "#power-domain-cells", 0, &pd_args);
2211 if (ret < 0)
2212 return ret;
2213
2214 mutex_lock(&gpd_list_lock);
2215 pd = genpd_get_from_provider(&pd_args);
2216 of_node_put(pd_args.np);
2217 if (IS_ERR(pd)) {
2218 mutex_unlock(&gpd_list_lock);
2219 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2220 __func__, PTR_ERR(pd));
2221 return -EPROBE_DEFER;
2222 }
2223
2224 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2225
2226 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2227 ret = genpd_add_device(pd, dev, NULL);
2228 if (ret != -EAGAIN)
2229 break;
2230
2231 mdelay(i);
2232 cond_resched();
2233 }
2234 mutex_unlock(&gpd_list_lock);
2235
2236 if (ret < 0) {
2237 if (ret != -EPROBE_DEFER)
2238 dev_err(dev, "failed to add to PM domain %s: %d",
2239 pd->name, ret);
2240 goto out;
2241 }
2242
2243 dev->pm_domain->detach = genpd_dev_pm_detach;
2244 dev->pm_domain->sync = genpd_dev_pm_sync;
2245
2246 genpd_lock(pd);
2247 ret = genpd_power_on(pd, 0);
2248 genpd_unlock(pd);
2249out:
2250 return ret ? -EPROBE_DEFER : 0;
2251}
2252EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2253
2254static const struct of_device_id idle_state_match[] = {
2255 { .compatible = "domain-idle-state", },
2256 { }
2257};
2258
2259static int genpd_parse_state(struct genpd_power_state *genpd_state,
2260 struct device_node *state_node)
2261{
2262 int err;
2263 u32 residency;
2264 u32 entry_latency, exit_latency;
2265
2266 err = of_property_read_u32(state_node, "entry-latency-us",
2267 &entry_latency);
2268 if (err) {
2269 pr_debug(" * %pOF missing entry-latency-us property\n",
2270 state_node);
2271 return -EINVAL;
2272 }
2273
2274 err = of_property_read_u32(state_node, "exit-latency-us",
2275 &exit_latency);
2276 if (err) {
2277 pr_debug(" * %pOF missing exit-latency-us property\n",
2278 state_node);
2279 return -EINVAL;
2280 }
2281
2282 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2283 if (!err)
2284 genpd_state->residency_ns = 1000 * residency;
2285
2286 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2287 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2288 genpd_state->fwnode = &state_node->fwnode;
2289
2290 return 0;
2291}
2292
2293static int genpd_iterate_idle_states(struct device_node *dn,
2294 struct genpd_power_state *states)
2295{
2296 int ret;
2297 struct of_phandle_iterator it;
2298 struct device_node *np;
2299 int i = 0;
2300
2301 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2302 if (ret <= 0)
2303 return ret;
2304
2305 /* Loop over the phandles until all the requested entry is found */
2306 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2307 np = it.node;
2308 if (!of_match_node(idle_state_match, np))
2309 continue;
2310 if (states) {
2311 ret = genpd_parse_state(&states[i], np);
2312 if (ret) {
2313 pr_err("Parsing idle state node %pOF failed with err %d\n",
2314 np, ret);
2315 of_node_put(np);
2316 return ret;
2317 }
2318 }
2319 i++;
2320 }
2321
2322 return i;
2323}
2324
2325/**
2326 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2327 *
2328 * @dn: The genpd device node
2329 * @states: The pointer to which the state array will be saved.
2330 * @n: The count of elements in the array returned from this function.
2331 *
2332 * Returns the device states parsed from the OF node. The memory for the states
2333 * is allocated by this function and is the responsibility of the caller to
2334 * free the memory after use. If no domain idle states is found it returns
2335 * -EINVAL and in case of errors, a negative error code.
2336 */
2337int of_genpd_parse_idle_states(struct device_node *dn,
2338 struct genpd_power_state **states, int *n)
2339{
2340 struct genpd_power_state *st;
2341 int ret;
2342
2343 ret = genpd_iterate_idle_states(dn, NULL);
2344 if (ret <= 0)
2345 return ret < 0 ? ret : -EINVAL;
2346
2347 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2348 if (!st)
2349 return -ENOMEM;
2350
2351 ret = genpd_iterate_idle_states(dn, st);
2352 if (ret <= 0) {
2353 kfree(st);
2354 return ret < 0 ? ret : -EINVAL;
2355 }
2356
2357 *states = st;
2358 *n = ret;
2359
2360 return 0;
2361}
2362EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2363
2364#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2365
2366
2367/*** debugfs support ***/
2368
2369#ifdef CONFIG_DEBUG_FS
2370#include <linux/pm.h>
2371#include <linux/device.h>
2372#include <linux/debugfs.h>
2373#include <linux/seq_file.h>
2374#include <linux/init.h>
2375#include <linux/kobject.h>
2376static struct dentry *genpd_debugfs_dir;
2377
2378/*
2379 * TODO: This function is a slightly modified version of rtpm_status_show
2380 * from sysfs.c, so generalize it.
2381 */
2382static void rtpm_status_str(struct seq_file *s, struct device *dev)
2383{
2384 static const char * const status_lookup[] = {
2385 [RPM_ACTIVE] = "active",
2386 [RPM_RESUMING] = "resuming",
2387 [RPM_SUSPENDED] = "suspended",
2388 [RPM_SUSPENDING] = "suspending"
2389 };
2390 const char *p = "";
2391
2392 if (dev->power.runtime_error)
2393 p = "error";
2394 else if (dev->power.disable_depth)
2395 p = "unsupported";
2396 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2397 p = status_lookup[dev->power.runtime_status];
2398 else
2399 WARN_ON(1);
2400
2401 seq_puts(s, p);
2402}
2403
2404static int genpd_summary_one(struct seq_file *s,
2405 struct generic_pm_domain *genpd)
2406{
2407 static const char * const status_lookup[] = {
2408 [GPD_STATE_ACTIVE] = "on",
2409 [GPD_STATE_POWER_OFF] = "off"
2410 };
2411 struct pm_domain_data *pm_data;
2412 const char *kobj_path;
2413 struct gpd_link *link;
2414 char state[16];
2415 int ret;
2416
2417 ret = genpd_lock_interruptible(genpd);
2418 if (ret)
2419 return -ERESTARTSYS;
2420
2421 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2422 goto exit;
2423 if (!genpd_status_on(genpd))
2424 snprintf(state, sizeof(state), "%s-%u",
2425 status_lookup[genpd->status], genpd->state_idx);
2426 else
2427 snprintf(state, sizeof(state), "%s",
2428 status_lookup[genpd->status]);
2429 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2430
2431 /*
2432 * Modifications on the list require holding locks on both
2433 * master and slave, so we are safe.
2434 * Also genpd->name is immutable.
2435 */
2436 list_for_each_entry(link, &genpd->master_links, master_node) {
2437 seq_printf(s, "%s", link->slave->name);
2438 if (!list_is_last(&link->master_node, &genpd->master_links))
2439 seq_puts(s, ", ");
2440 }
2441
2442 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2443 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2444 genpd_is_irq_safe(genpd) ?
2445 GFP_ATOMIC : GFP_KERNEL);
2446 if (kobj_path == NULL)
2447 continue;
2448
2449 seq_printf(s, "\n %-50s ", kobj_path);
2450 rtpm_status_str(s, pm_data->dev);
2451 kfree(kobj_path);
2452 }
2453
2454 seq_puts(s, "\n");
2455exit:
2456 genpd_unlock(genpd);
2457
2458 return 0;
2459}
2460
2461static int genpd_summary_show(struct seq_file *s, void *data)
2462{
2463 struct generic_pm_domain *genpd;
2464 int ret = 0;
2465
2466 seq_puts(s, "domain status slaves\n");
2467 seq_puts(s, " /device runtime status\n");
2468 seq_puts(s, "----------------------------------------------------------------------\n");
2469
2470 ret = mutex_lock_interruptible(&gpd_list_lock);
2471 if (ret)
2472 return -ERESTARTSYS;
2473
2474 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2475 ret = genpd_summary_one(s, genpd);
2476 if (ret)
2477 break;
2478 }
2479 mutex_unlock(&gpd_list_lock);
2480
2481 return ret;
2482}
2483
2484static int genpd_status_show(struct seq_file *s, void *data)
2485{
2486 static const char * const status_lookup[] = {
2487 [GPD_STATE_ACTIVE] = "on",
2488 [GPD_STATE_POWER_OFF] = "off"
2489 };
2490
2491 struct generic_pm_domain *genpd = s->private;
2492 int ret = 0;
2493
2494 ret = genpd_lock_interruptible(genpd);
2495 if (ret)
2496 return -ERESTARTSYS;
2497
2498 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2499 goto exit;
2500
2501 if (genpd->status == GPD_STATE_POWER_OFF)
2502 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2503 genpd->state_idx);
2504 else
2505 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2506exit:
2507 genpd_unlock(genpd);
2508 return ret;
2509}
2510
2511static int genpd_sub_domains_show(struct seq_file *s, void *data)
2512{
2513 struct generic_pm_domain *genpd = s->private;
2514 struct gpd_link *link;
2515 int ret = 0;
2516
2517 ret = genpd_lock_interruptible(genpd);
2518 if (ret)
2519 return -ERESTARTSYS;
2520
2521 list_for_each_entry(link, &genpd->master_links, master_node)
2522 seq_printf(s, "%s\n", link->slave->name);
2523
2524 genpd_unlock(genpd);
2525 return ret;
2526}
2527
2528static int genpd_idle_states_show(struct seq_file *s, void *data)
2529{
2530 struct generic_pm_domain *genpd = s->private;
2531 unsigned int i;
2532 int ret = 0;
2533
2534 ret = genpd_lock_interruptible(genpd);
2535 if (ret)
2536 return -ERESTARTSYS;
2537
2538 seq_puts(s, "State Time Spent(ms)\n");
2539
2540 for (i = 0; i < genpd->state_count; i++) {
2541 ktime_t delta = 0;
2542 s64 msecs;
2543
2544 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2545 (genpd->state_idx == i))
2546 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2547
2548 msecs = ktime_to_ms(
2549 ktime_add(genpd->states[i].idle_time, delta));
2550 seq_printf(s, "S%-13i %lld\n", i, msecs);
2551 }
2552
2553 genpd_unlock(genpd);
2554 return ret;
2555}
2556
2557static int genpd_active_time_show(struct seq_file *s, void *data)
2558{
2559 struct generic_pm_domain *genpd = s->private;
2560 ktime_t delta = 0;
2561 int ret = 0;
2562
2563 ret = genpd_lock_interruptible(genpd);
2564 if (ret)
2565 return -ERESTARTSYS;
2566
2567 if (genpd->status == GPD_STATE_ACTIVE)
2568 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2569
2570 seq_printf(s, "%lld ms\n", ktime_to_ms(
2571 ktime_add(genpd->on_time, delta)));
2572
2573 genpd_unlock(genpd);
2574 return ret;
2575}
2576
2577static int genpd_total_idle_time_show(struct seq_file *s, void *data)
2578{
2579 struct generic_pm_domain *genpd = s->private;
2580 ktime_t delta = 0, total = 0;
2581 unsigned int i;
2582 int ret = 0;
2583
2584 ret = genpd_lock_interruptible(genpd);
2585 if (ret)
2586 return -ERESTARTSYS;
2587
2588 for (i = 0; i < genpd->state_count; i++) {
2589
2590 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2591 (genpd->state_idx == i))
2592 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2593
2594 total = ktime_add(total, genpd->states[i].idle_time);
2595 }
2596 total = ktime_add(total, delta);
2597
2598 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2599
2600 genpd_unlock(genpd);
2601 return ret;
2602}
2603
2604
2605static int genpd_devices_show(struct seq_file *s, void *data)
2606{
2607 struct generic_pm_domain *genpd = s->private;
2608 struct pm_domain_data *pm_data;
2609 const char *kobj_path;
2610 int ret = 0;
2611
2612 ret = genpd_lock_interruptible(genpd);
2613 if (ret)
2614 return -ERESTARTSYS;
2615
2616 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2617 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2618 genpd_is_irq_safe(genpd) ?
2619 GFP_ATOMIC : GFP_KERNEL);
2620 if (kobj_path == NULL)
2621 continue;
2622
2623 seq_printf(s, "%s\n", kobj_path);
2624 kfree(kobj_path);
2625 }
2626
2627 genpd_unlock(genpd);
2628 return ret;
2629}
2630
2631#define define_genpd_open_function(name) \
2632static int genpd_##name##_open(struct inode *inode, struct file *file) \
2633{ \
2634 return single_open(file, genpd_##name##_show, inode->i_private); \
2635}
2636
2637define_genpd_open_function(summary);
2638define_genpd_open_function(status);
2639define_genpd_open_function(sub_domains);
2640define_genpd_open_function(idle_states);
2641define_genpd_open_function(active_time);
2642define_genpd_open_function(total_idle_time);
2643define_genpd_open_function(devices);
2644
2645#define define_genpd_debugfs_fops(name) \
2646static const struct file_operations genpd_##name##_fops = { \
2647 .open = genpd_##name##_open, \
2648 .read = seq_read, \
2649 .llseek = seq_lseek, \
2650 .release = single_release, \
2651}
2652
2653define_genpd_debugfs_fops(summary);
2654define_genpd_debugfs_fops(status);
2655define_genpd_debugfs_fops(sub_domains);
2656define_genpd_debugfs_fops(idle_states);
2657define_genpd_debugfs_fops(active_time);
2658define_genpd_debugfs_fops(total_idle_time);
2659define_genpd_debugfs_fops(devices);
2660
2661static int __init genpd_debug_init(void)
2662{
2663 struct dentry *d;
2664 struct generic_pm_domain *genpd;
2665
2666 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2667
2668 if (!genpd_debugfs_dir)
2669 return -ENOMEM;
2670
2671 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2672 genpd_debugfs_dir, NULL, &genpd_summary_fops);
2673 if (!d)
2674 return -ENOMEM;
2675
2676 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2677 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
2678 if (!d)
2679 return -ENOMEM;
2680
2681 debugfs_create_file("current_state", 0444,
2682 d, genpd, &genpd_status_fops);
2683 debugfs_create_file("sub_domains", 0444,
2684 d, genpd, &genpd_sub_domains_fops);
2685 debugfs_create_file("idle_states", 0444,
2686 d, genpd, &genpd_idle_states_fops);
2687 debugfs_create_file("active_time", 0444,
2688 d, genpd, &genpd_active_time_fops);
2689 debugfs_create_file("total_idle_time", 0444,
2690 d, genpd, &genpd_total_idle_time_fops);
2691 debugfs_create_file("devices", 0444,
2692 d, genpd, &genpd_devices_fops);
2693 }
2694
2695 return 0;
2696}
2697late_initcall(genpd_debug_init);
2698
2699static void __exit genpd_debug_exit(void)
2700{
2701 debugfs_remove_recursive(genpd_debugfs_dir);
2702}
2703__exitcall(genpd_debug_exit);
2704#endif /* CONFIG_DEBUG_FS */