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