]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/base/power/domain.c
Merge branches 'pm-cpuidle', 'pm-cpufreq', 'pm-domains' and 'pm-sleep'
[mirror_ubuntu-bionic-kernel.git] / drivers / base / power / domain.c
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
39 static LIST_HEAD(gpd_list);
40 static 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 */
48 struct 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 */
71 static 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
79 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
80 {
81 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
82 }
83
84 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
85 {
86 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
87 }
88
89 static 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
99 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100 {
101 atomic_inc(&genpd->sd_count);
102 smp_mb__after_atomic();
103 }
104
105 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
106 {
107 ktime_t time_start;
108 s64 elapsed_ns;
109 int ret;
110
111 if (!genpd->power_on)
112 return 0;
113
114 if (!timed)
115 return genpd->power_on(genpd);
116
117 time_start = ktime_get();
118 ret = genpd->power_on(genpd);
119 if (ret)
120 return ret;
121
122 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
123 if (elapsed_ns <= genpd->power_on_latency_ns)
124 return ret;
125
126 genpd->power_on_latency_ns = elapsed_ns;
127 genpd->max_off_time_changed = true;
128 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
129 genpd->name, "on", elapsed_ns);
130
131 return ret;
132 }
133
134 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
135 {
136 ktime_t time_start;
137 s64 elapsed_ns;
138 int ret;
139
140 if (!genpd->power_off)
141 return 0;
142
143 if (!timed)
144 return genpd->power_off(genpd);
145
146 time_start = ktime_get();
147 ret = genpd->power_off(genpd);
148 if (ret == -EBUSY)
149 return ret;
150
151 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
152 if (elapsed_ns <= genpd->power_off_latency_ns)
153 return ret;
154
155 genpd->power_off_latency_ns = elapsed_ns;
156 genpd->max_off_time_changed = true;
157 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
158 genpd->name, "off", elapsed_ns);
159
160 return ret;
161 }
162
163 /**
164 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
165 * @genpd: PM domain to power off.
166 *
167 * Queue up the execution of genpd_poweroff() unless it's already been done
168 * before.
169 */
170 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
171 {
172 queue_work(pm_wq, &genpd->power_off_work);
173 }
174
175 /**
176 * __genpd_poweron - Restore power to a given PM domain and its masters.
177 * @genpd: PM domain to power up.
178 * @depth: nesting count for lockdep.
179 *
180 * Restore power to @genpd and all of its masters so that it is possible to
181 * resume a device belonging to it.
182 */
183 static int __genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
184 {
185 struct gpd_link *link;
186 int ret = 0;
187
188 if (genpd->status == GPD_STATE_ACTIVE
189 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
190 return 0;
191
192 /*
193 * The list is guaranteed not to change while the loop below is being
194 * executed, unless one of the masters' .power_on() callbacks fiddles
195 * with it.
196 */
197 list_for_each_entry(link, &genpd->slave_links, slave_node) {
198 struct generic_pm_domain *master = link->master;
199
200 genpd_sd_counter_inc(master);
201
202 mutex_lock_nested(&master->lock, depth + 1);
203 ret = __genpd_poweron(master, depth + 1);
204 mutex_unlock(&master->lock);
205
206 if (ret) {
207 genpd_sd_counter_dec(master);
208 goto err;
209 }
210 }
211
212 ret = genpd_power_on(genpd, true);
213 if (ret)
214 goto err;
215
216 genpd->status = GPD_STATE_ACTIVE;
217 return 0;
218
219 err:
220 list_for_each_entry_continue_reverse(link,
221 &genpd->slave_links,
222 slave_node) {
223 genpd_sd_counter_dec(link->master);
224 genpd_queue_power_off_work(link->master);
225 }
226
227 return ret;
228 }
229
230 /**
231 * genpd_poweron - Restore power to a given PM domain and its masters.
232 * @genpd: PM domain to power up.
233 */
234 static int genpd_poweron(struct generic_pm_domain *genpd)
235 {
236 int ret;
237
238 mutex_lock(&genpd->lock);
239 ret = __genpd_poweron(genpd, 0);
240 mutex_unlock(&genpd->lock);
241 return ret;
242 }
243
244
245 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
246 {
247 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
248 }
249
250 static int genpd_restore_dev(struct generic_pm_domain *genpd,
251 struct device *dev)
252 {
253 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
254 }
255
256 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
257 unsigned long val, void *ptr)
258 {
259 struct generic_pm_domain_data *gpd_data;
260 struct device *dev;
261
262 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
263 dev = gpd_data->base.dev;
264
265 for (;;) {
266 struct generic_pm_domain *genpd;
267 struct pm_domain_data *pdd;
268
269 spin_lock_irq(&dev->power.lock);
270
271 pdd = dev->power.subsys_data ?
272 dev->power.subsys_data->domain_data : NULL;
273 if (pdd && pdd->dev) {
274 to_gpd_data(pdd)->td.constraint_changed = true;
275 genpd = dev_to_genpd(dev);
276 } else {
277 genpd = ERR_PTR(-ENODATA);
278 }
279
280 spin_unlock_irq(&dev->power.lock);
281
282 if (!IS_ERR(genpd)) {
283 mutex_lock(&genpd->lock);
284 genpd->max_off_time_changed = true;
285 mutex_unlock(&genpd->lock);
286 }
287
288 dev = dev->parent;
289 if (!dev || dev->power.ignore_children)
290 break;
291 }
292
293 return NOTIFY_DONE;
294 }
295
296 /**
297 * genpd_poweroff - Remove power from a given PM domain.
298 * @genpd: PM domain to power down.
299 * @is_async: PM domain is powered down from a scheduled work
300 *
301 * If all of the @genpd's devices have been suspended and all of its subdomains
302 * have been powered down, remove power from @genpd.
303 */
304 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
305 {
306 struct pm_domain_data *pdd;
307 struct gpd_link *link;
308 unsigned int not_suspended = 0;
309
310 /*
311 * Do not try to power off the domain in the following situations:
312 * (1) The domain is already in the "power off" state.
313 * (2) System suspend is in progress.
314 */
315 if (genpd->status == GPD_STATE_POWER_OFF
316 || genpd->prepared_count > 0)
317 return 0;
318
319 if (atomic_read(&genpd->sd_count) > 0)
320 return -EBUSY;
321
322 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
323 enum pm_qos_flags_status stat;
324
325 stat = dev_pm_qos_flags(pdd->dev,
326 PM_QOS_FLAG_NO_POWER_OFF
327 | PM_QOS_FLAG_REMOTE_WAKEUP);
328 if (stat > PM_QOS_FLAGS_NONE)
329 return -EBUSY;
330
331 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
332 not_suspended++;
333 }
334
335 if (not_suspended > 1 || (not_suspended == 1 && is_async))
336 return -EBUSY;
337
338 if (genpd->gov && genpd->gov->power_down_ok) {
339 if (!genpd->gov->power_down_ok(&genpd->domain))
340 return -EAGAIN;
341 }
342
343 if (genpd->power_off) {
344 int ret;
345
346 if (atomic_read(&genpd->sd_count) > 0)
347 return -EBUSY;
348
349 /*
350 * If sd_count > 0 at this point, one of the subdomains hasn't
351 * managed to call genpd_poweron() for the master yet after
352 * incrementing it. In that case genpd_poweron() will wait
353 * for us to drop the lock, so we can call .power_off() and let
354 * the genpd_poweron() restore power for us (this shouldn't
355 * happen very often).
356 */
357 ret = genpd_power_off(genpd, true);
358 if (ret)
359 return ret;
360 }
361
362 genpd->status = GPD_STATE_POWER_OFF;
363
364 list_for_each_entry(link, &genpd->slave_links, slave_node) {
365 genpd_sd_counter_dec(link->master);
366 genpd_queue_power_off_work(link->master);
367 }
368
369 return 0;
370 }
371
372 /**
373 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
374 * @work: Work structure used for scheduling the execution of this function.
375 */
376 static void genpd_power_off_work_fn(struct work_struct *work)
377 {
378 struct generic_pm_domain *genpd;
379
380 genpd = container_of(work, struct generic_pm_domain, power_off_work);
381
382 mutex_lock(&genpd->lock);
383 genpd_poweroff(genpd, true);
384 mutex_unlock(&genpd->lock);
385 }
386
387 /**
388 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
389 * @dev: Device to suspend.
390 *
391 * Carry out a runtime suspend of a device under the assumption that its
392 * pm_domain field points to the domain member of an object of type
393 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
394 */
395 static int pm_genpd_runtime_suspend(struct device *dev)
396 {
397 struct generic_pm_domain *genpd;
398 bool (*stop_ok)(struct device *__dev);
399 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
400 bool runtime_pm = pm_runtime_enabled(dev);
401 ktime_t time_start;
402 s64 elapsed_ns;
403 int ret;
404
405 dev_dbg(dev, "%s()\n", __func__);
406
407 genpd = dev_to_genpd(dev);
408 if (IS_ERR(genpd))
409 return -EINVAL;
410
411 /*
412 * A runtime PM centric subsystem/driver may re-use the runtime PM
413 * callbacks for other purposes than runtime PM. In those scenarios
414 * runtime PM is disabled. Under these circumstances, we shall skip
415 * validating/measuring the PM QoS latency.
416 */
417 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
418 if (runtime_pm && stop_ok && !stop_ok(dev))
419 return -EBUSY;
420
421 /* Measure suspend latency. */
422 if (runtime_pm)
423 time_start = ktime_get();
424
425 ret = genpd_save_dev(genpd, dev);
426 if (ret)
427 return ret;
428
429 ret = genpd_stop_dev(genpd, dev);
430 if (ret) {
431 genpd_restore_dev(genpd, dev);
432 return ret;
433 }
434
435 /* Update suspend latency value if the measured time exceeds it. */
436 if (runtime_pm) {
437 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
438 if (elapsed_ns > td->suspend_latency_ns) {
439 td->suspend_latency_ns = elapsed_ns;
440 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
441 elapsed_ns);
442 genpd->max_off_time_changed = true;
443 td->constraint_changed = true;
444 }
445 }
446
447 /*
448 * If power.irq_safe is set, this routine will be run with interrupts
449 * off, so it can't use mutexes.
450 */
451 if (dev->power.irq_safe)
452 return 0;
453
454 mutex_lock(&genpd->lock);
455 genpd_poweroff(genpd, false);
456 mutex_unlock(&genpd->lock);
457
458 return 0;
459 }
460
461 /**
462 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
463 * @dev: Device to resume.
464 *
465 * Carry out a runtime resume of a device under the assumption that its
466 * pm_domain field points to the domain member of an object of type
467 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
468 */
469 static int pm_genpd_runtime_resume(struct device *dev)
470 {
471 struct generic_pm_domain *genpd;
472 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
473 bool runtime_pm = pm_runtime_enabled(dev);
474 ktime_t time_start;
475 s64 elapsed_ns;
476 int ret;
477 bool timed = true;
478
479 dev_dbg(dev, "%s()\n", __func__);
480
481 genpd = dev_to_genpd(dev);
482 if (IS_ERR(genpd))
483 return -EINVAL;
484
485 /* If power.irq_safe, the PM domain is never powered off. */
486 if (dev->power.irq_safe) {
487 timed = false;
488 goto out;
489 }
490
491 mutex_lock(&genpd->lock);
492 ret = __genpd_poweron(genpd, 0);
493 mutex_unlock(&genpd->lock);
494
495 if (ret)
496 return ret;
497
498 out:
499 /* Measure resume latency. */
500 if (timed && runtime_pm)
501 time_start = ktime_get();
502
503 genpd_start_dev(genpd, dev);
504 genpd_restore_dev(genpd, dev);
505
506 /* Update resume latency value if the measured time exceeds it. */
507 if (timed && runtime_pm) {
508 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
509 if (elapsed_ns > td->resume_latency_ns) {
510 td->resume_latency_ns = elapsed_ns;
511 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
512 elapsed_ns);
513 genpd->max_off_time_changed = true;
514 td->constraint_changed = true;
515 }
516 }
517
518 return 0;
519 }
520
521 static bool pd_ignore_unused;
522 static int __init pd_ignore_unused_setup(char *__unused)
523 {
524 pd_ignore_unused = true;
525 return 1;
526 }
527 __setup("pd_ignore_unused", pd_ignore_unused_setup);
528
529 /**
530 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
531 */
532 static int __init genpd_poweroff_unused(void)
533 {
534 struct generic_pm_domain *genpd;
535
536 if (pd_ignore_unused) {
537 pr_warn("genpd: Not disabling unused power domains\n");
538 return 0;
539 }
540
541 mutex_lock(&gpd_list_lock);
542
543 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
544 genpd_queue_power_off_work(genpd);
545
546 mutex_unlock(&gpd_list_lock);
547
548 return 0;
549 }
550 late_initcall(genpd_poweroff_unused);
551
552 #ifdef CONFIG_PM_SLEEP
553
554 /**
555 * pm_genpd_present - Check if the given PM domain has been initialized.
556 * @genpd: PM domain to check.
557 */
558 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
559 {
560 const struct generic_pm_domain *gpd;
561
562 if (IS_ERR_OR_NULL(genpd))
563 return false;
564
565 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
566 if (gpd == genpd)
567 return true;
568
569 return false;
570 }
571
572 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
573 struct device *dev)
574 {
575 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
576 }
577
578 /**
579 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
580 * @genpd: PM domain to power off, if possible.
581 * @timed: True if latency measurements are allowed.
582 *
583 * Check if the given PM domain can be powered off (during system suspend or
584 * hibernation) and do that if so. Also, in that case propagate to its masters.
585 *
586 * This function is only called in "noirq" and "syscore" stages of system power
587 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
588 * executed sequentially, so it is guaranteed that it will never run twice in
589 * parallel).
590 */
591 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
592 bool timed)
593 {
594 struct gpd_link *link;
595
596 if (genpd->status == GPD_STATE_POWER_OFF)
597 return;
598
599 if (genpd->suspended_count != genpd->device_count
600 || atomic_read(&genpd->sd_count) > 0)
601 return;
602
603 genpd_power_off(genpd, timed);
604
605 genpd->status = GPD_STATE_POWER_OFF;
606
607 list_for_each_entry(link, &genpd->slave_links, slave_node) {
608 genpd_sd_counter_dec(link->master);
609 pm_genpd_sync_poweroff(link->master, timed);
610 }
611 }
612
613 /**
614 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
615 * @genpd: PM domain to power on.
616 * @timed: True if latency measurements are allowed.
617 *
618 * This function is only called in "noirq" and "syscore" stages of system power
619 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
620 * executed sequentially, so it is guaranteed that it will never run twice in
621 * parallel).
622 */
623 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
624 bool timed)
625 {
626 struct gpd_link *link;
627
628 if (genpd->status == GPD_STATE_ACTIVE)
629 return;
630
631 list_for_each_entry(link, &genpd->slave_links, slave_node) {
632 pm_genpd_sync_poweron(link->master, timed);
633 genpd_sd_counter_inc(link->master);
634 }
635
636 genpd_power_on(genpd, timed);
637
638 genpd->status = GPD_STATE_ACTIVE;
639 }
640
641 /**
642 * resume_needed - Check whether to resume a device before system suspend.
643 * @dev: Device to check.
644 * @genpd: PM domain the device belongs to.
645 *
646 * There are two cases in which a device that can wake up the system from sleep
647 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
648 * to wake up the system and it has to remain active for this purpose while the
649 * system is in the sleep state and (2) if the device is not enabled to wake up
650 * the system from sleep states and it generally doesn't generate wakeup signals
651 * by itself (those signals are generated on its behalf by other parts of the
652 * system). In the latter case it may be necessary to reconfigure the device's
653 * wakeup settings during system suspend, because it may have been set up to
654 * signal remote wakeup from the system's working state as needed by runtime PM.
655 * Return 'true' in either of the above cases.
656 */
657 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
658 {
659 bool active_wakeup;
660
661 if (!device_can_wakeup(dev))
662 return false;
663
664 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
665 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
666 }
667
668 /**
669 * pm_genpd_prepare - Start power transition of a device in a PM domain.
670 * @dev: Device to start the transition of.
671 *
672 * Start a power transition of a device (during a system-wide power transition)
673 * under the assumption that its pm_domain field points to the domain member of
674 * an object of type struct generic_pm_domain representing a PM domain
675 * consisting of I/O devices.
676 */
677 static int pm_genpd_prepare(struct device *dev)
678 {
679 struct generic_pm_domain *genpd;
680 int ret;
681
682 dev_dbg(dev, "%s()\n", __func__);
683
684 genpd = dev_to_genpd(dev);
685 if (IS_ERR(genpd))
686 return -EINVAL;
687
688 /*
689 * If a wakeup request is pending for the device, it should be woken up
690 * at this point and a system wakeup event should be reported if it's
691 * set up to wake up the system from sleep states.
692 */
693 pm_runtime_get_noresume(dev);
694 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
695 pm_wakeup_event(dev, 0);
696
697 if (pm_wakeup_pending()) {
698 pm_runtime_put(dev);
699 return -EBUSY;
700 }
701
702 if (resume_needed(dev, genpd))
703 pm_runtime_resume(dev);
704
705 mutex_lock(&genpd->lock);
706
707 if (genpd->prepared_count++ == 0) {
708 genpd->suspended_count = 0;
709 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
710 }
711
712 mutex_unlock(&genpd->lock);
713
714 if (genpd->suspend_power_off) {
715 pm_runtime_put_noidle(dev);
716 return 0;
717 }
718
719 /*
720 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
721 * so genpd_poweron() will return immediately, but if the device
722 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
723 * to make it operational.
724 */
725 pm_runtime_resume(dev);
726 __pm_runtime_disable(dev, false);
727
728 ret = pm_generic_prepare(dev);
729 if (ret) {
730 mutex_lock(&genpd->lock);
731
732 if (--genpd->prepared_count == 0)
733 genpd->suspend_power_off = false;
734
735 mutex_unlock(&genpd->lock);
736 pm_runtime_enable(dev);
737 }
738
739 pm_runtime_put(dev);
740 return ret;
741 }
742
743 /**
744 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
745 * @dev: Device to suspend.
746 *
747 * Suspend a device under the assumption that its pm_domain field points to the
748 * domain member of an object of type struct generic_pm_domain representing
749 * a PM domain consisting of I/O devices.
750 */
751 static int pm_genpd_suspend(struct device *dev)
752 {
753 struct generic_pm_domain *genpd;
754
755 dev_dbg(dev, "%s()\n", __func__);
756
757 genpd = dev_to_genpd(dev);
758 if (IS_ERR(genpd))
759 return -EINVAL;
760
761 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
762 }
763
764 /**
765 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
766 * @dev: Device to suspend.
767 *
768 * Carry out a late suspend of a device under the assumption that its
769 * pm_domain field points to the domain member of an object of type
770 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
771 */
772 static int pm_genpd_suspend_late(struct device *dev)
773 {
774 struct generic_pm_domain *genpd;
775
776 dev_dbg(dev, "%s()\n", __func__);
777
778 genpd = dev_to_genpd(dev);
779 if (IS_ERR(genpd))
780 return -EINVAL;
781
782 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
783 }
784
785 /**
786 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
787 * @dev: Device to suspend.
788 *
789 * Stop the device and remove power from the domain if all devices in it have
790 * been stopped.
791 */
792 static int pm_genpd_suspend_noirq(struct device *dev)
793 {
794 struct generic_pm_domain *genpd;
795
796 dev_dbg(dev, "%s()\n", __func__);
797
798 genpd = dev_to_genpd(dev);
799 if (IS_ERR(genpd))
800 return -EINVAL;
801
802 if (genpd->suspend_power_off
803 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
804 return 0;
805
806 genpd_stop_dev(genpd, dev);
807
808 /*
809 * Since all of the "noirq" callbacks are executed sequentially, it is
810 * guaranteed that this function will never run twice in parallel for
811 * the same PM domain, so it is not necessary to use locking here.
812 */
813 genpd->suspended_count++;
814 pm_genpd_sync_poweroff(genpd, true);
815
816 return 0;
817 }
818
819 /**
820 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
821 * @dev: Device to resume.
822 *
823 * Restore power to the device's PM domain, if necessary, and start the device.
824 */
825 static int pm_genpd_resume_noirq(struct device *dev)
826 {
827 struct generic_pm_domain *genpd;
828
829 dev_dbg(dev, "%s()\n", __func__);
830
831 genpd = dev_to_genpd(dev);
832 if (IS_ERR(genpd))
833 return -EINVAL;
834
835 if (genpd->suspend_power_off
836 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
837 return 0;
838
839 /*
840 * Since all of the "noirq" callbacks are executed sequentially, it is
841 * guaranteed that this function will never run twice in parallel for
842 * the same PM domain, so it is not necessary to use locking here.
843 */
844 pm_genpd_sync_poweron(genpd, true);
845 genpd->suspended_count--;
846
847 return genpd_start_dev(genpd, dev);
848 }
849
850 /**
851 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
852 * @dev: Device to resume.
853 *
854 * Carry out an early resume of a device under the assumption that its
855 * pm_domain field points to the domain member of an object of type
856 * struct generic_pm_domain representing a power domain consisting of I/O
857 * devices.
858 */
859 static int pm_genpd_resume_early(struct device *dev)
860 {
861 struct generic_pm_domain *genpd;
862
863 dev_dbg(dev, "%s()\n", __func__);
864
865 genpd = dev_to_genpd(dev);
866 if (IS_ERR(genpd))
867 return -EINVAL;
868
869 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
870 }
871
872 /**
873 * pm_genpd_resume - Resume of device in an I/O PM domain.
874 * @dev: Device to resume.
875 *
876 * Resume a device under the assumption that its pm_domain field points to the
877 * domain member of an object of type struct generic_pm_domain representing
878 * a power domain consisting of I/O devices.
879 */
880 static int pm_genpd_resume(struct device *dev)
881 {
882 struct generic_pm_domain *genpd;
883
884 dev_dbg(dev, "%s()\n", __func__);
885
886 genpd = dev_to_genpd(dev);
887 if (IS_ERR(genpd))
888 return -EINVAL;
889
890 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
891 }
892
893 /**
894 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
895 * @dev: Device to freeze.
896 *
897 * Freeze a device under the assumption that its pm_domain field points to the
898 * domain member of an object of type struct generic_pm_domain representing
899 * a power domain consisting of I/O devices.
900 */
901 static int pm_genpd_freeze(struct device *dev)
902 {
903 struct generic_pm_domain *genpd;
904
905 dev_dbg(dev, "%s()\n", __func__);
906
907 genpd = dev_to_genpd(dev);
908 if (IS_ERR(genpd))
909 return -EINVAL;
910
911 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
912 }
913
914 /**
915 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
916 * @dev: Device to freeze.
917 *
918 * Carry out a late freeze of a device under the assumption that its
919 * pm_domain field points to the domain member of an object of type
920 * struct generic_pm_domain representing a power domain consisting of I/O
921 * devices.
922 */
923 static int pm_genpd_freeze_late(struct device *dev)
924 {
925 struct generic_pm_domain *genpd;
926
927 dev_dbg(dev, "%s()\n", __func__);
928
929 genpd = dev_to_genpd(dev);
930 if (IS_ERR(genpd))
931 return -EINVAL;
932
933 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
934 }
935
936 /**
937 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
938 * @dev: Device to freeze.
939 *
940 * Carry out a late freeze of a device under the assumption that its
941 * pm_domain field points to the domain member of an object of type
942 * struct generic_pm_domain representing a power domain consisting of I/O
943 * devices.
944 */
945 static int pm_genpd_freeze_noirq(struct device *dev)
946 {
947 struct generic_pm_domain *genpd;
948
949 dev_dbg(dev, "%s()\n", __func__);
950
951 genpd = dev_to_genpd(dev);
952 if (IS_ERR(genpd))
953 return -EINVAL;
954
955 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
956 }
957
958 /**
959 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
960 * @dev: Device to thaw.
961 *
962 * Start the device, unless power has been removed from the domain already
963 * before the system transition.
964 */
965 static int pm_genpd_thaw_noirq(struct device *dev)
966 {
967 struct generic_pm_domain *genpd;
968
969 dev_dbg(dev, "%s()\n", __func__);
970
971 genpd = dev_to_genpd(dev);
972 if (IS_ERR(genpd))
973 return -EINVAL;
974
975 return genpd->suspend_power_off ?
976 0 : genpd_start_dev(genpd, dev);
977 }
978
979 /**
980 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
981 * @dev: Device to thaw.
982 *
983 * Carry out an early thaw of a device under the assumption that its
984 * pm_domain field points to the domain member of an object of type
985 * struct generic_pm_domain representing a power domain consisting of I/O
986 * devices.
987 */
988 static int pm_genpd_thaw_early(struct device *dev)
989 {
990 struct generic_pm_domain *genpd;
991
992 dev_dbg(dev, "%s()\n", __func__);
993
994 genpd = dev_to_genpd(dev);
995 if (IS_ERR(genpd))
996 return -EINVAL;
997
998 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
999 }
1000
1001 /**
1002 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1003 * @dev: Device to thaw.
1004 *
1005 * Thaw a device under the assumption that its pm_domain field points to the
1006 * domain member of an object of type struct generic_pm_domain representing
1007 * a power domain consisting of I/O devices.
1008 */
1009 static int pm_genpd_thaw(struct device *dev)
1010 {
1011 struct generic_pm_domain *genpd;
1012
1013 dev_dbg(dev, "%s()\n", __func__);
1014
1015 genpd = dev_to_genpd(dev);
1016 if (IS_ERR(genpd))
1017 return -EINVAL;
1018
1019 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1020 }
1021
1022 /**
1023 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1024 * @dev: Device to resume.
1025 *
1026 * Make sure the domain will be in the same power state as before the
1027 * hibernation the system is resuming from and start the device if necessary.
1028 */
1029 static int pm_genpd_restore_noirq(struct device *dev)
1030 {
1031 struct generic_pm_domain *genpd;
1032
1033 dev_dbg(dev, "%s()\n", __func__);
1034
1035 genpd = dev_to_genpd(dev);
1036 if (IS_ERR(genpd))
1037 return -EINVAL;
1038
1039 /*
1040 * Since all of the "noirq" callbacks are executed sequentially, it is
1041 * guaranteed that this function will never run twice in parallel for
1042 * the same PM domain, so it is not necessary to use locking here.
1043 *
1044 * At this point suspended_count == 0 means we are being run for the
1045 * first time for the given domain in the present cycle.
1046 */
1047 if (genpd->suspended_count++ == 0) {
1048 /*
1049 * The boot kernel might put the domain into arbitrary state,
1050 * so make it appear as powered off to pm_genpd_sync_poweron(),
1051 * so that it tries to power it on in case it was really off.
1052 */
1053 genpd->status = GPD_STATE_POWER_OFF;
1054 if (genpd->suspend_power_off) {
1055 /*
1056 * If the domain was off before the hibernation, make
1057 * sure it will be off going forward.
1058 */
1059 genpd_power_off(genpd, true);
1060
1061 return 0;
1062 }
1063 }
1064
1065 if (genpd->suspend_power_off)
1066 return 0;
1067
1068 pm_genpd_sync_poweron(genpd, true);
1069
1070 return genpd_start_dev(genpd, dev);
1071 }
1072
1073 /**
1074 * pm_genpd_complete - Complete power transition of a device in a power domain.
1075 * @dev: Device to complete the transition of.
1076 *
1077 * Complete a power transition of a device (during a system-wide power
1078 * transition) under the assumption that its pm_domain field points to the
1079 * domain member of an object of type struct generic_pm_domain representing
1080 * a power domain consisting of I/O devices.
1081 */
1082 static void pm_genpd_complete(struct device *dev)
1083 {
1084 struct generic_pm_domain *genpd;
1085 bool run_complete;
1086
1087 dev_dbg(dev, "%s()\n", __func__);
1088
1089 genpd = dev_to_genpd(dev);
1090 if (IS_ERR(genpd))
1091 return;
1092
1093 mutex_lock(&genpd->lock);
1094
1095 run_complete = !genpd->suspend_power_off;
1096 if (--genpd->prepared_count == 0)
1097 genpd->suspend_power_off = false;
1098
1099 mutex_unlock(&genpd->lock);
1100
1101 if (run_complete) {
1102 pm_generic_complete(dev);
1103 pm_runtime_set_active(dev);
1104 pm_runtime_enable(dev);
1105 pm_request_idle(dev);
1106 }
1107 }
1108
1109 /**
1110 * genpd_syscore_switch - Switch power during system core suspend or resume.
1111 * @dev: Device that normally is marked as "always on" to switch power for.
1112 *
1113 * This routine may only be called during the system core (syscore) suspend or
1114 * resume phase for devices whose "always on" flags are set.
1115 */
1116 static void genpd_syscore_switch(struct device *dev, bool suspend)
1117 {
1118 struct generic_pm_domain *genpd;
1119
1120 genpd = dev_to_genpd(dev);
1121 if (!pm_genpd_present(genpd))
1122 return;
1123
1124 if (suspend) {
1125 genpd->suspended_count++;
1126 pm_genpd_sync_poweroff(genpd, false);
1127 } else {
1128 pm_genpd_sync_poweron(genpd, false);
1129 genpd->suspended_count--;
1130 }
1131 }
1132
1133 void pm_genpd_syscore_poweroff(struct device *dev)
1134 {
1135 genpd_syscore_switch(dev, true);
1136 }
1137 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1138
1139 void pm_genpd_syscore_poweron(struct device *dev)
1140 {
1141 genpd_syscore_switch(dev, false);
1142 }
1143 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1144
1145 #else /* !CONFIG_PM_SLEEP */
1146
1147 #define pm_genpd_prepare NULL
1148 #define pm_genpd_suspend NULL
1149 #define pm_genpd_suspend_late NULL
1150 #define pm_genpd_suspend_noirq NULL
1151 #define pm_genpd_resume_early NULL
1152 #define pm_genpd_resume_noirq NULL
1153 #define pm_genpd_resume NULL
1154 #define pm_genpd_freeze NULL
1155 #define pm_genpd_freeze_late NULL
1156 #define pm_genpd_freeze_noirq NULL
1157 #define pm_genpd_thaw_early NULL
1158 #define pm_genpd_thaw_noirq NULL
1159 #define pm_genpd_thaw NULL
1160 #define pm_genpd_restore_noirq NULL
1161 #define pm_genpd_complete NULL
1162
1163 #endif /* CONFIG_PM_SLEEP */
1164
1165 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1166 struct generic_pm_domain *genpd,
1167 struct gpd_timing_data *td)
1168 {
1169 struct generic_pm_domain_data *gpd_data;
1170 int ret;
1171
1172 ret = dev_pm_get_subsys_data(dev);
1173 if (ret)
1174 return ERR_PTR(ret);
1175
1176 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1177 if (!gpd_data) {
1178 ret = -ENOMEM;
1179 goto err_put;
1180 }
1181
1182 if (td)
1183 gpd_data->td = *td;
1184
1185 gpd_data->base.dev = dev;
1186 gpd_data->td.constraint_changed = true;
1187 gpd_data->td.effective_constraint_ns = -1;
1188 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1189
1190 spin_lock_irq(&dev->power.lock);
1191
1192 if (dev->power.subsys_data->domain_data) {
1193 ret = -EINVAL;
1194 goto err_free;
1195 }
1196
1197 dev->power.subsys_data->domain_data = &gpd_data->base;
1198
1199 spin_unlock_irq(&dev->power.lock);
1200
1201 dev_pm_domain_set(dev, &genpd->domain);
1202
1203 return gpd_data;
1204
1205 err_free:
1206 spin_unlock_irq(&dev->power.lock);
1207 kfree(gpd_data);
1208 err_put:
1209 dev_pm_put_subsys_data(dev);
1210 return ERR_PTR(ret);
1211 }
1212
1213 static void genpd_free_dev_data(struct device *dev,
1214 struct generic_pm_domain_data *gpd_data)
1215 {
1216 dev_pm_domain_set(dev, NULL);
1217
1218 spin_lock_irq(&dev->power.lock);
1219
1220 dev->power.subsys_data->domain_data = NULL;
1221
1222 spin_unlock_irq(&dev->power.lock);
1223
1224 kfree(gpd_data);
1225 dev_pm_put_subsys_data(dev);
1226 }
1227
1228 /**
1229 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1230 * @genpd: PM domain to add the device to.
1231 * @dev: Device to be added.
1232 * @td: Set of PM QoS timing parameters to attach to the device.
1233 */
1234 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1235 struct gpd_timing_data *td)
1236 {
1237 struct generic_pm_domain_data *gpd_data;
1238 int ret = 0;
1239
1240 dev_dbg(dev, "%s()\n", __func__);
1241
1242 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1243 return -EINVAL;
1244
1245 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1246 if (IS_ERR(gpd_data))
1247 return PTR_ERR(gpd_data);
1248
1249 mutex_lock(&genpd->lock);
1250
1251 if (genpd->prepared_count > 0) {
1252 ret = -EAGAIN;
1253 goto out;
1254 }
1255
1256 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1257 if (ret)
1258 goto out;
1259
1260 genpd->device_count++;
1261 genpd->max_off_time_changed = true;
1262
1263 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1264
1265 out:
1266 mutex_unlock(&genpd->lock);
1267
1268 if (ret)
1269 genpd_free_dev_data(dev, gpd_data);
1270 else
1271 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1272
1273 return ret;
1274 }
1275 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1276
1277 /**
1278 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1279 * @genpd: PM domain to remove the device from.
1280 * @dev: Device to be removed.
1281 */
1282 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1283 struct device *dev)
1284 {
1285 struct generic_pm_domain_data *gpd_data;
1286 struct pm_domain_data *pdd;
1287 int ret = 0;
1288
1289 dev_dbg(dev, "%s()\n", __func__);
1290
1291 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1292 return -EINVAL;
1293
1294 /* The above validation also means we have existing domain_data. */
1295 pdd = dev->power.subsys_data->domain_data;
1296 gpd_data = to_gpd_data(pdd);
1297 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1298
1299 mutex_lock(&genpd->lock);
1300
1301 if (genpd->prepared_count > 0) {
1302 ret = -EAGAIN;
1303 goto out;
1304 }
1305
1306 genpd->device_count--;
1307 genpd->max_off_time_changed = true;
1308
1309 if (genpd->detach_dev)
1310 genpd->detach_dev(genpd, dev);
1311
1312 list_del_init(&pdd->list_node);
1313
1314 mutex_unlock(&genpd->lock);
1315
1316 genpd_free_dev_data(dev, gpd_data);
1317
1318 return 0;
1319
1320 out:
1321 mutex_unlock(&genpd->lock);
1322 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1323
1324 return ret;
1325 }
1326 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1327
1328 /**
1329 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1330 * @genpd: Master PM domain to add the subdomain to.
1331 * @subdomain: Subdomain to be added.
1332 */
1333 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1334 struct generic_pm_domain *subdomain)
1335 {
1336 struct gpd_link *link, *itr;
1337 int ret = 0;
1338
1339 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1340 || genpd == subdomain)
1341 return -EINVAL;
1342
1343 link = kzalloc(sizeof(*link), GFP_KERNEL);
1344 if (!link)
1345 return -ENOMEM;
1346
1347 mutex_lock(&subdomain->lock);
1348 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1349
1350 if (genpd->status == GPD_STATE_POWER_OFF
1351 && subdomain->status != GPD_STATE_POWER_OFF) {
1352 ret = -EINVAL;
1353 goto out;
1354 }
1355
1356 list_for_each_entry(itr, &genpd->master_links, master_node) {
1357 if (itr->slave == subdomain && itr->master == genpd) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361 }
1362
1363 link->master = genpd;
1364 list_add_tail(&link->master_node, &genpd->master_links);
1365 link->slave = subdomain;
1366 list_add_tail(&link->slave_node, &subdomain->slave_links);
1367 if (subdomain->status != GPD_STATE_POWER_OFF)
1368 genpd_sd_counter_inc(genpd);
1369
1370 out:
1371 mutex_unlock(&genpd->lock);
1372 mutex_unlock(&subdomain->lock);
1373 if (ret)
1374 kfree(link);
1375 return ret;
1376 }
1377 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1378
1379 /**
1380 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1381 * @genpd: Master PM domain to remove the subdomain from.
1382 * @subdomain: Subdomain to be removed.
1383 */
1384 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1385 struct generic_pm_domain *subdomain)
1386 {
1387 struct gpd_link *link;
1388 int ret = -EINVAL;
1389
1390 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1391 return -EINVAL;
1392
1393 mutex_lock(&subdomain->lock);
1394 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1395
1396 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1397 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1398 subdomain->name);
1399 ret = -EBUSY;
1400 goto out;
1401 }
1402
1403 list_for_each_entry(link, &genpd->master_links, master_node) {
1404 if (link->slave != subdomain)
1405 continue;
1406
1407 list_del(&link->master_node);
1408 list_del(&link->slave_node);
1409 kfree(link);
1410 if (subdomain->status != GPD_STATE_POWER_OFF)
1411 genpd_sd_counter_dec(genpd);
1412
1413 ret = 0;
1414 break;
1415 }
1416
1417 out:
1418 mutex_unlock(&genpd->lock);
1419 mutex_unlock(&subdomain->lock);
1420
1421 return ret;
1422 }
1423 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1424
1425 /* Default device callbacks for generic PM domains. */
1426
1427 /**
1428 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1429 * @dev: Device to handle.
1430 */
1431 static int pm_genpd_default_save_state(struct device *dev)
1432 {
1433 int (*cb)(struct device *__dev);
1434
1435 if (dev->type && dev->type->pm)
1436 cb = dev->type->pm->runtime_suspend;
1437 else if (dev->class && dev->class->pm)
1438 cb = dev->class->pm->runtime_suspend;
1439 else if (dev->bus && dev->bus->pm)
1440 cb = dev->bus->pm->runtime_suspend;
1441 else
1442 cb = NULL;
1443
1444 if (!cb && dev->driver && dev->driver->pm)
1445 cb = dev->driver->pm->runtime_suspend;
1446
1447 return cb ? cb(dev) : 0;
1448 }
1449
1450 /**
1451 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1452 * @dev: Device to handle.
1453 */
1454 static int pm_genpd_default_restore_state(struct device *dev)
1455 {
1456 int (*cb)(struct device *__dev);
1457
1458 if (dev->type && dev->type->pm)
1459 cb = dev->type->pm->runtime_resume;
1460 else if (dev->class && dev->class->pm)
1461 cb = dev->class->pm->runtime_resume;
1462 else if (dev->bus && dev->bus->pm)
1463 cb = dev->bus->pm->runtime_resume;
1464 else
1465 cb = NULL;
1466
1467 if (!cb && dev->driver && dev->driver->pm)
1468 cb = dev->driver->pm->runtime_resume;
1469
1470 return cb ? cb(dev) : 0;
1471 }
1472
1473 /**
1474 * pm_genpd_init - Initialize a generic I/O PM domain object.
1475 * @genpd: PM domain object to initialize.
1476 * @gov: PM domain governor to associate with the domain (may be NULL).
1477 * @is_off: Initial value of the domain's power_is_off field.
1478 */
1479 void pm_genpd_init(struct generic_pm_domain *genpd,
1480 struct dev_power_governor *gov, bool is_off)
1481 {
1482 if (IS_ERR_OR_NULL(genpd))
1483 return;
1484
1485 INIT_LIST_HEAD(&genpd->master_links);
1486 INIT_LIST_HEAD(&genpd->slave_links);
1487 INIT_LIST_HEAD(&genpd->dev_list);
1488 mutex_init(&genpd->lock);
1489 genpd->gov = gov;
1490 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1491 atomic_set(&genpd->sd_count, 0);
1492 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1493 genpd->device_count = 0;
1494 genpd->max_off_time_ns = -1;
1495 genpd->max_off_time_changed = true;
1496 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1497 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1498 genpd->domain.ops.prepare = pm_genpd_prepare;
1499 genpd->domain.ops.suspend = pm_genpd_suspend;
1500 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1501 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1502 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1503 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1504 genpd->domain.ops.resume = pm_genpd_resume;
1505 genpd->domain.ops.freeze = pm_genpd_freeze;
1506 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1507 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1508 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1509 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1510 genpd->domain.ops.thaw = pm_genpd_thaw;
1511 genpd->domain.ops.poweroff = pm_genpd_suspend;
1512 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1513 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1514 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1515 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1516 genpd->domain.ops.restore = pm_genpd_resume;
1517 genpd->domain.ops.complete = pm_genpd_complete;
1518 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1519 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1520
1521 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1522 genpd->dev_ops.stop = pm_clk_suspend;
1523 genpd->dev_ops.start = pm_clk_resume;
1524 }
1525
1526 mutex_lock(&gpd_list_lock);
1527 list_add(&genpd->gpd_list_node, &gpd_list);
1528 mutex_unlock(&gpd_list_lock);
1529 }
1530 EXPORT_SYMBOL_GPL(pm_genpd_init);
1531
1532 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1533 /*
1534 * Device Tree based PM domain providers.
1535 *
1536 * The code below implements generic device tree based PM domain providers that
1537 * bind device tree nodes with generic PM domains registered in the system.
1538 *
1539 * Any driver that registers generic PM domains and needs to support binding of
1540 * devices to these domains is supposed to register a PM domain provider, which
1541 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1542 *
1543 * Two simple mapping functions have been provided for convenience:
1544 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1545 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1546 * index.
1547 */
1548
1549 /**
1550 * struct of_genpd_provider - PM domain provider registration structure
1551 * @link: Entry in global list of PM domain providers
1552 * @node: Pointer to device tree node of PM domain provider
1553 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1554 * into a PM domain.
1555 * @data: context pointer to be passed into @xlate callback
1556 */
1557 struct of_genpd_provider {
1558 struct list_head link;
1559 struct device_node *node;
1560 genpd_xlate_t xlate;
1561 void *data;
1562 };
1563
1564 /* List of registered PM domain providers. */
1565 static LIST_HEAD(of_genpd_providers);
1566 /* Mutex to protect the list above. */
1567 static DEFINE_MUTEX(of_genpd_mutex);
1568
1569 /**
1570 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1571 * @genpdspec: OF phandle args to map into a PM domain
1572 * @data: xlate function private data - pointer to struct generic_pm_domain
1573 *
1574 * This is a generic xlate function that can be used to model PM domains that
1575 * have their own device tree nodes. The private data of xlate function needs
1576 * to be a valid pointer to struct generic_pm_domain.
1577 */
1578 struct generic_pm_domain *__of_genpd_xlate_simple(
1579 struct of_phandle_args *genpdspec,
1580 void *data)
1581 {
1582 if (genpdspec->args_count != 0)
1583 return ERR_PTR(-EINVAL);
1584 return data;
1585 }
1586 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1587
1588 /**
1589 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1590 * @genpdspec: OF phandle args to map into a PM domain
1591 * @data: xlate function private data - pointer to struct genpd_onecell_data
1592 *
1593 * This is a generic xlate function that can be used to model simple PM domain
1594 * controllers that have one device tree node and provide multiple PM domains.
1595 * A single cell is used as an index into an array of PM domains specified in
1596 * the genpd_onecell_data struct when registering the provider.
1597 */
1598 struct generic_pm_domain *__of_genpd_xlate_onecell(
1599 struct of_phandle_args *genpdspec,
1600 void *data)
1601 {
1602 struct genpd_onecell_data *genpd_data = data;
1603 unsigned int idx = genpdspec->args[0];
1604
1605 if (genpdspec->args_count != 1)
1606 return ERR_PTR(-EINVAL);
1607
1608 if (idx >= genpd_data->num_domains) {
1609 pr_err("%s: invalid domain index %u\n", __func__, idx);
1610 return ERR_PTR(-EINVAL);
1611 }
1612
1613 if (!genpd_data->domains[idx])
1614 return ERR_PTR(-ENOENT);
1615
1616 return genpd_data->domains[idx];
1617 }
1618 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1619
1620 /**
1621 * __of_genpd_add_provider() - Register a PM domain provider for a node
1622 * @np: Device node pointer associated with the PM domain provider.
1623 * @xlate: Callback for decoding PM domain from phandle arguments.
1624 * @data: Context pointer for @xlate callback.
1625 */
1626 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1627 void *data)
1628 {
1629 struct of_genpd_provider *cp;
1630
1631 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1632 if (!cp)
1633 return -ENOMEM;
1634
1635 cp->node = of_node_get(np);
1636 cp->data = data;
1637 cp->xlate = xlate;
1638
1639 mutex_lock(&of_genpd_mutex);
1640 list_add(&cp->link, &of_genpd_providers);
1641 mutex_unlock(&of_genpd_mutex);
1642 pr_debug("Added domain provider from %s\n", np->full_name);
1643
1644 return 0;
1645 }
1646 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1647
1648 /**
1649 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1650 * @np: Device node pointer associated with the PM domain provider
1651 */
1652 void of_genpd_del_provider(struct device_node *np)
1653 {
1654 struct of_genpd_provider *cp;
1655
1656 mutex_lock(&of_genpd_mutex);
1657 list_for_each_entry(cp, &of_genpd_providers, link) {
1658 if (cp->node == np) {
1659 list_del(&cp->link);
1660 of_node_put(cp->node);
1661 kfree(cp);
1662 break;
1663 }
1664 }
1665 mutex_unlock(&of_genpd_mutex);
1666 }
1667 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1668
1669 /**
1670 * of_genpd_get_from_provider() - Look-up PM domain
1671 * @genpdspec: OF phandle args to use for look-up
1672 *
1673 * Looks for a PM domain provider under the node specified by @genpdspec and if
1674 * found, uses xlate function of the provider to map phandle args to a PM
1675 * domain.
1676 *
1677 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1678 * on failure.
1679 */
1680 struct generic_pm_domain *of_genpd_get_from_provider(
1681 struct of_phandle_args *genpdspec)
1682 {
1683 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1684 struct of_genpd_provider *provider;
1685
1686 mutex_lock(&of_genpd_mutex);
1687
1688 /* Check if we have such a provider in our array */
1689 list_for_each_entry(provider, &of_genpd_providers, link) {
1690 if (provider->node == genpdspec->np)
1691 genpd = provider->xlate(genpdspec, provider->data);
1692 if (!IS_ERR(genpd))
1693 break;
1694 }
1695
1696 mutex_unlock(&of_genpd_mutex);
1697
1698 return genpd;
1699 }
1700 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1701
1702 /**
1703 * genpd_dev_pm_detach - Detach a device from its PM domain.
1704 * @dev: Device to detach.
1705 * @power_off: Currently not used
1706 *
1707 * Try to locate a corresponding generic PM domain, which the device was
1708 * attached to previously. If such is found, the device is detached from it.
1709 */
1710 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1711 {
1712 struct generic_pm_domain *pd;
1713 unsigned int i;
1714 int ret = 0;
1715
1716 pd = pm_genpd_lookup_dev(dev);
1717 if (!pd)
1718 return;
1719
1720 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1721
1722 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1723 ret = pm_genpd_remove_device(pd, dev);
1724 if (ret != -EAGAIN)
1725 break;
1726
1727 mdelay(i);
1728 cond_resched();
1729 }
1730
1731 if (ret < 0) {
1732 dev_err(dev, "failed to remove from PM domain %s: %d",
1733 pd->name, ret);
1734 return;
1735 }
1736
1737 /* Check if PM domain can be powered off after removing this device. */
1738 genpd_queue_power_off_work(pd);
1739 }
1740
1741 static void genpd_dev_pm_sync(struct device *dev)
1742 {
1743 struct generic_pm_domain *pd;
1744
1745 pd = dev_to_genpd(dev);
1746 if (IS_ERR(pd))
1747 return;
1748
1749 genpd_queue_power_off_work(pd);
1750 }
1751
1752 /**
1753 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1754 * @dev: Device to attach.
1755 *
1756 * Parse device's OF node to find a PM domain specifier. If such is found,
1757 * attaches the device to retrieved pm_domain ops.
1758 *
1759 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1760 * backwards compatibility with existing DTBs.
1761 *
1762 * Returns 0 on successfully attached PM domain or negative error code. Note
1763 * that if a power-domain exists for the device, but it cannot be found or
1764 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1765 * probed and to re-try again later.
1766 */
1767 int genpd_dev_pm_attach(struct device *dev)
1768 {
1769 struct of_phandle_args pd_args;
1770 struct generic_pm_domain *pd;
1771 unsigned int i;
1772 int ret;
1773
1774 if (!dev->of_node)
1775 return -ENODEV;
1776
1777 if (dev->pm_domain)
1778 return -EEXIST;
1779
1780 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1781 "#power-domain-cells", 0, &pd_args);
1782 if (ret < 0) {
1783 if (ret != -ENOENT)
1784 return ret;
1785
1786 /*
1787 * Try legacy Samsung-specific bindings
1788 * (for backwards compatibility of DT ABI)
1789 */
1790 pd_args.args_count = 0;
1791 pd_args.np = of_parse_phandle(dev->of_node,
1792 "samsung,power-domain", 0);
1793 if (!pd_args.np)
1794 return -ENOENT;
1795 }
1796
1797 pd = of_genpd_get_from_provider(&pd_args);
1798 of_node_put(pd_args.np);
1799 if (IS_ERR(pd)) {
1800 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1801 __func__, PTR_ERR(pd));
1802 return -EPROBE_DEFER;
1803 }
1804
1805 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1806
1807 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1808 ret = pm_genpd_add_device(pd, dev);
1809 if (ret != -EAGAIN)
1810 break;
1811
1812 mdelay(i);
1813 cond_resched();
1814 }
1815
1816 if (ret < 0) {
1817 dev_err(dev, "failed to add to PM domain %s: %d",
1818 pd->name, ret);
1819 goto out;
1820 }
1821
1822 dev->pm_domain->detach = genpd_dev_pm_detach;
1823 dev->pm_domain->sync = genpd_dev_pm_sync;
1824 ret = genpd_poweron(pd);
1825
1826 out:
1827 return ret ? -EPROBE_DEFER : 0;
1828 }
1829 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1830 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1831
1832
1833 /*** debugfs support ***/
1834
1835 #ifdef CONFIG_PM_ADVANCED_DEBUG
1836 #include <linux/pm.h>
1837 #include <linux/device.h>
1838 #include <linux/debugfs.h>
1839 #include <linux/seq_file.h>
1840 #include <linux/init.h>
1841 #include <linux/kobject.h>
1842 static struct dentry *pm_genpd_debugfs_dir;
1843
1844 /*
1845 * TODO: This function is a slightly modified version of rtpm_status_show
1846 * from sysfs.c, so generalize it.
1847 */
1848 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1849 {
1850 static const char * const status_lookup[] = {
1851 [RPM_ACTIVE] = "active",
1852 [RPM_RESUMING] = "resuming",
1853 [RPM_SUSPENDED] = "suspended",
1854 [RPM_SUSPENDING] = "suspending"
1855 };
1856 const char *p = "";
1857
1858 if (dev->power.runtime_error)
1859 p = "error";
1860 else if (dev->power.disable_depth)
1861 p = "unsupported";
1862 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1863 p = status_lookup[dev->power.runtime_status];
1864 else
1865 WARN_ON(1);
1866
1867 seq_puts(s, p);
1868 }
1869
1870 static int pm_genpd_summary_one(struct seq_file *s,
1871 struct generic_pm_domain *genpd)
1872 {
1873 static const char * const status_lookup[] = {
1874 [GPD_STATE_ACTIVE] = "on",
1875 [GPD_STATE_POWER_OFF] = "off"
1876 };
1877 struct pm_domain_data *pm_data;
1878 const char *kobj_path;
1879 struct gpd_link *link;
1880 int ret;
1881
1882 ret = mutex_lock_interruptible(&genpd->lock);
1883 if (ret)
1884 return -ERESTARTSYS;
1885
1886 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1887 goto exit;
1888 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1889
1890 /*
1891 * Modifications on the list require holding locks on both
1892 * master and slave, so we are safe.
1893 * Also genpd->name is immutable.
1894 */
1895 list_for_each_entry(link, &genpd->master_links, master_node) {
1896 seq_printf(s, "%s", link->slave->name);
1897 if (!list_is_last(&link->master_node, &genpd->master_links))
1898 seq_puts(s, ", ");
1899 }
1900
1901 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1902 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1903 if (kobj_path == NULL)
1904 continue;
1905
1906 seq_printf(s, "\n %-50s ", kobj_path);
1907 rtpm_status_str(s, pm_data->dev);
1908 kfree(kobj_path);
1909 }
1910
1911 seq_puts(s, "\n");
1912 exit:
1913 mutex_unlock(&genpd->lock);
1914
1915 return 0;
1916 }
1917
1918 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1919 {
1920 struct generic_pm_domain *genpd;
1921 int ret = 0;
1922
1923 seq_puts(s, "domain status slaves\n");
1924 seq_puts(s, " /device runtime status\n");
1925 seq_puts(s, "----------------------------------------------------------------------\n");
1926
1927 ret = mutex_lock_interruptible(&gpd_list_lock);
1928 if (ret)
1929 return -ERESTARTSYS;
1930
1931 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1932 ret = pm_genpd_summary_one(s, genpd);
1933 if (ret)
1934 break;
1935 }
1936 mutex_unlock(&gpd_list_lock);
1937
1938 return ret;
1939 }
1940
1941 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1942 {
1943 return single_open(file, pm_genpd_summary_show, NULL);
1944 }
1945
1946 static const struct file_operations pm_genpd_summary_fops = {
1947 .open = pm_genpd_summary_open,
1948 .read = seq_read,
1949 .llseek = seq_lseek,
1950 .release = single_release,
1951 };
1952
1953 static int __init pm_genpd_debug_init(void)
1954 {
1955 struct dentry *d;
1956
1957 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1958
1959 if (!pm_genpd_debugfs_dir)
1960 return -ENOMEM;
1961
1962 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1963 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1964 if (!d)
1965 return -ENOMEM;
1966
1967 return 0;
1968 }
1969 late_initcall(pm_genpd_debug_init);
1970
1971 static void __exit pm_genpd_debug_exit(void)
1972 {
1973 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1974 }
1975 __exitcall(pm_genpd_debug_exit);
1976 #endif /* CONFIG_PM_ADVANCED_DEBUG */