5decc295def447b15bb7e8e5aa146d731dfbe9c0
[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         pm_runtime_get_noresume(dev);
692         if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
693                 pm_wakeup_event(dev, 0);
694
695         if (pm_wakeup_pending()) {
696                 pm_runtime_put(dev);
697                 return -EBUSY;
698         }
699
700         if (resume_needed(dev, genpd))
701                 pm_runtime_resume(dev);
702
703         mutex_lock(&genpd->lock);
704
705         if (genpd->prepared_count++ == 0) {
706                 genpd->suspended_count = 0;
707                 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
708         }
709
710         mutex_unlock(&genpd->lock);
711
712         if (genpd->suspend_power_off) {
713                 pm_runtime_put_noidle(dev);
714                 return 0;
715         }
716
717         /*
718          * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
719          * so genpd_poweron() will return immediately, but if the device
720          * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
721          * to make it operational.
722          */
723         pm_runtime_resume(dev);
724         __pm_runtime_disable(dev, false);
725
726         ret = pm_generic_prepare(dev);
727         if (ret) {
728                 mutex_lock(&genpd->lock);
729
730                 if (--genpd->prepared_count == 0)
731                         genpd->suspend_power_off = false;
732
733                 mutex_unlock(&genpd->lock);
734                 pm_runtime_enable(dev);
735         }
736
737         pm_runtime_put(dev);
738         return ret;
739 }
740
741 /**
742  * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
743  * @dev: Device to suspend.
744  *
745  * Suspend a device under the assumption that its pm_domain field points to the
746  * domain member of an object of type struct generic_pm_domain representing
747  * a PM domain consisting of I/O devices.
748  */
749 static int pm_genpd_suspend(struct device *dev)
750 {
751         struct generic_pm_domain *genpd;
752
753         dev_dbg(dev, "%s()\n", __func__);
754
755         genpd = dev_to_genpd(dev);
756         if (IS_ERR(genpd))
757                 return -EINVAL;
758
759         return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
760 }
761
762 /**
763  * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
764  * @dev: Device to suspend.
765  *
766  * Carry out a late suspend of a device under the assumption that its
767  * pm_domain field points to the domain member of an object of type
768  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
769  */
770 static int pm_genpd_suspend_late(struct device *dev)
771 {
772         struct generic_pm_domain *genpd;
773
774         dev_dbg(dev, "%s()\n", __func__);
775
776         genpd = dev_to_genpd(dev);
777         if (IS_ERR(genpd))
778                 return -EINVAL;
779
780         return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
781 }
782
783 /**
784  * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
785  * @dev: Device to suspend.
786  *
787  * Stop the device and remove power from the domain if all devices in it have
788  * been stopped.
789  */
790 static int pm_genpd_suspend_noirq(struct device *dev)
791 {
792         struct generic_pm_domain *genpd;
793
794         dev_dbg(dev, "%s()\n", __func__);
795
796         genpd = dev_to_genpd(dev);
797         if (IS_ERR(genpd))
798                 return -EINVAL;
799
800         if (genpd->suspend_power_off
801             || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
802                 return 0;
803
804         genpd_stop_dev(genpd, dev);
805
806         /*
807          * Since all of the "noirq" callbacks are executed sequentially, it is
808          * guaranteed that this function will never run twice in parallel for
809          * the same PM domain, so it is not necessary to use locking here.
810          */
811         genpd->suspended_count++;
812         pm_genpd_sync_poweroff(genpd, true);
813
814         return 0;
815 }
816
817 /**
818  * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
819  * @dev: Device to resume.
820  *
821  * Restore power to the device's PM domain, if necessary, and start the device.
822  */
823 static int pm_genpd_resume_noirq(struct device *dev)
824 {
825         struct generic_pm_domain *genpd;
826
827         dev_dbg(dev, "%s()\n", __func__);
828
829         genpd = dev_to_genpd(dev);
830         if (IS_ERR(genpd))
831                 return -EINVAL;
832
833         if (genpd->suspend_power_off
834             || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
835                 return 0;
836
837         /*
838          * Since all of the "noirq" callbacks are executed sequentially, it is
839          * guaranteed that this function will never run twice in parallel for
840          * the same PM domain, so it is not necessary to use locking here.
841          */
842         pm_genpd_sync_poweron(genpd, true);
843         genpd->suspended_count--;
844
845         return genpd_start_dev(genpd, dev);
846 }
847
848 /**
849  * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
850  * @dev: Device to resume.
851  *
852  * Carry out an early resume of a device under the assumption that its
853  * pm_domain field points to the domain member of an object of type
854  * struct generic_pm_domain representing a power domain consisting of I/O
855  * devices.
856  */
857 static int pm_genpd_resume_early(struct device *dev)
858 {
859         struct generic_pm_domain *genpd;
860
861         dev_dbg(dev, "%s()\n", __func__);
862
863         genpd = dev_to_genpd(dev);
864         if (IS_ERR(genpd))
865                 return -EINVAL;
866
867         return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
868 }
869
870 /**
871  * pm_genpd_resume - Resume of device in an I/O PM domain.
872  * @dev: Device to resume.
873  *
874  * Resume a device under the assumption that its pm_domain field points to the
875  * domain member of an object of type struct generic_pm_domain representing
876  * a power domain consisting of I/O devices.
877  */
878 static int pm_genpd_resume(struct device *dev)
879 {
880         struct generic_pm_domain *genpd;
881
882         dev_dbg(dev, "%s()\n", __func__);
883
884         genpd = dev_to_genpd(dev);
885         if (IS_ERR(genpd))
886                 return -EINVAL;
887
888         return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
889 }
890
891 /**
892  * pm_genpd_freeze - Freezing a device in an I/O PM domain.
893  * @dev: Device to freeze.
894  *
895  * Freeze a device under the assumption that its pm_domain field points to the
896  * domain member of an object of type struct generic_pm_domain representing
897  * a power domain consisting of I/O devices.
898  */
899 static int pm_genpd_freeze(struct device *dev)
900 {
901         struct generic_pm_domain *genpd;
902
903         dev_dbg(dev, "%s()\n", __func__);
904
905         genpd = dev_to_genpd(dev);
906         if (IS_ERR(genpd))
907                 return -EINVAL;
908
909         return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
910 }
911
912 /**
913  * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
914  * @dev: Device to freeze.
915  *
916  * Carry out a late freeze of a device under the assumption that its
917  * pm_domain field points to the domain member of an object of type
918  * struct generic_pm_domain representing a power domain consisting of I/O
919  * devices.
920  */
921 static int pm_genpd_freeze_late(struct device *dev)
922 {
923         struct generic_pm_domain *genpd;
924
925         dev_dbg(dev, "%s()\n", __func__);
926
927         genpd = dev_to_genpd(dev);
928         if (IS_ERR(genpd))
929                 return -EINVAL;
930
931         return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
932 }
933
934 /**
935  * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
936  * @dev: Device to freeze.
937  *
938  * Carry out a late freeze of a device under the assumption that its
939  * pm_domain field points to the domain member of an object of type
940  * struct generic_pm_domain representing a power domain consisting of I/O
941  * devices.
942  */
943 static int pm_genpd_freeze_noirq(struct device *dev)
944 {
945         struct generic_pm_domain *genpd;
946
947         dev_dbg(dev, "%s()\n", __func__);
948
949         genpd = dev_to_genpd(dev);
950         if (IS_ERR(genpd))
951                 return -EINVAL;
952
953         return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
954 }
955
956 /**
957  * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
958  * @dev: Device to thaw.
959  *
960  * Start the device, unless power has been removed from the domain already
961  * before the system transition.
962  */
963 static int pm_genpd_thaw_noirq(struct device *dev)
964 {
965         struct generic_pm_domain *genpd;
966
967         dev_dbg(dev, "%s()\n", __func__);
968
969         genpd = dev_to_genpd(dev);
970         if (IS_ERR(genpd))
971                 return -EINVAL;
972
973         return genpd->suspend_power_off ?
974                 0 : genpd_start_dev(genpd, dev);
975 }
976
977 /**
978  * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
979  * @dev: Device to thaw.
980  *
981  * Carry out an early thaw of a device under the assumption that its
982  * pm_domain field points to the domain member of an object of type
983  * struct generic_pm_domain representing a power domain consisting of I/O
984  * devices.
985  */
986 static int pm_genpd_thaw_early(struct device *dev)
987 {
988         struct generic_pm_domain *genpd;
989
990         dev_dbg(dev, "%s()\n", __func__);
991
992         genpd = dev_to_genpd(dev);
993         if (IS_ERR(genpd))
994                 return -EINVAL;
995
996         return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
997 }
998
999 /**
1000  * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1001  * @dev: Device to thaw.
1002  *
1003  * Thaw a device under the assumption that its pm_domain field points to the
1004  * domain member of an object of type struct generic_pm_domain representing
1005  * a power domain consisting of I/O devices.
1006  */
1007 static int pm_genpd_thaw(struct device *dev)
1008 {
1009         struct generic_pm_domain *genpd;
1010
1011         dev_dbg(dev, "%s()\n", __func__);
1012
1013         genpd = dev_to_genpd(dev);
1014         if (IS_ERR(genpd))
1015                 return -EINVAL;
1016
1017         return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1018 }
1019
1020 /**
1021  * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1022  * @dev: Device to resume.
1023  *
1024  * Make sure the domain will be in the same power state as before the
1025  * hibernation the system is resuming from and start the device if necessary.
1026  */
1027 static int pm_genpd_restore_noirq(struct device *dev)
1028 {
1029         struct generic_pm_domain *genpd;
1030
1031         dev_dbg(dev, "%s()\n", __func__);
1032
1033         genpd = dev_to_genpd(dev);
1034         if (IS_ERR(genpd))
1035                 return -EINVAL;
1036
1037         /*
1038          * Since all of the "noirq" callbacks are executed sequentially, it is
1039          * guaranteed that this function will never run twice in parallel for
1040          * the same PM domain, so it is not necessary to use locking here.
1041          *
1042          * At this point suspended_count == 0 means we are being run for the
1043          * first time for the given domain in the present cycle.
1044          */
1045         if (genpd->suspended_count++ == 0) {
1046                 /*
1047                  * The boot kernel might put the domain into arbitrary state,
1048                  * so make it appear as powered off to pm_genpd_sync_poweron(),
1049                  * so that it tries to power it on in case it was really off.
1050                  */
1051                 genpd->status = GPD_STATE_POWER_OFF;
1052                 if (genpd->suspend_power_off) {
1053                         /*
1054                          * If the domain was off before the hibernation, make
1055                          * sure it will be off going forward.
1056                          */
1057                         genpd_power_off(genpd, true);
1058
1059                         return 0;
1060                 }
1061         }
1062
1063         if (genpd->suspend_power_off)
1064                 return 0;
1065
1066         pm_genpd_sync_poweron(genpd, true);
1067
1068         return genpd_start_dev(genpd, dev);
1069 }
1070
1071 /**
1072  * pm_genpd_complete - Complete power transition of a device in a power domain.
1073  * @dev: Device to complete the transition of.
1074  *
1075  * Complete a power transition of a device (during a system-wide power
1076  * transition) under the assumption that its pm_domain field points to the
1077  * domain member of an object of type struct generic_pm_domain representing
1078  * a power domain consisting of I/O devices.
1079  */
1080 static void pm_genpd_complete(struct device *dev)
1081 {
1082         struct generic_pm_domain *genpd;
1083         bool run_complete;
1084
1085         dev_dbg(dev, "%s()\n", __func__);
1086
1087         genpd = dev_to_genpd(dev);
1088         if (IS_ERR(genpd))
1089                 return;
1090
1091         mutex_lock(&genpd->lock);
1092
1093         run_complete = !genpd->suspend_power_off;
1094         if (--genpd->prepared_count == 0)
1095                 genpd->suspend_power_off = false;
1096
1097         mutex_unlock(&genpd->lock);
1098
1099         if (run_complete) {
1100                 pm_generic_complete(dev);
1101                 pm_runtime_set_active(dev);
1102                 pm_runtime_enable(dev);
1103                 pm_request_idle(dev);
1104         }
1105 }
1106
1107 /**
1108  * genpd_syscore_switch - Switch power during system core suspend or resume.
1109  * @dev: Device that normally is marked as "always on" to switch power for.
1110  *
1111  * This routine may only be called during the system core (syscore) suspend or
1112  * resume phase for devices whose "always on" flags are set.
1113  */
1114 static void genpd_syscore_switch(struct device *dev, bool suspend)
1115 {
1116         struct generic_pm_domain *genpd;
1117
1118         genpd = dev_to_genpd(dev);
1119         if (!pm_genpd_present(genpd))
1120                 return;
1121
1122         if (suspend) {
1123                 genpd->suspended_count++;
1124                 pm_genpd_sync_poweroff(genpd, false);
1125         } else {
1126                 pm_genpd_sync_poweron(genpd, false);
1127                 genpd->suspended_count--;
1128         }
1129 }
1130
1131 void pm_genpd_syscore_poweroff(struct device *dev)
1132 {
1133         genpd_syscore_switch(dev, true);
1134 }
1135 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1136
1137 void pm_genpd_syscore_poweron(struct device *dev)
1138 {
1139         genpd_syscore_switch(dev, false);
1140 }
1141 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1142
1143 #else /* !CONFIG_PM_SLEEP */
1144
1145 #define pm_genpd_prepare                NULL
1146 #define pm_genpd_suspend                NULL
1147 #define pm_genpd_suspend_late           NULL
1148 #define pm_genpd_suspend_noirq          NULL
1149 #define pm_genpd_resume_early           NULL
1150 #define pm_genpd_resume_noirq           NULL
1151 #define pm_genpd_resume                 NULL
1152 #define pm_genpd_freeze                 NULL
1153 #define pm_genpd_freeze_late            NULL
1154 #define pm_genpd_freeze_noirq           NULL
1155 #define pm_genpd_thaw_early             NULL
1156 #define pm_genpd_thaw_noirq             NULL
1157 #define pm_genpd_thaw                   NULL
1158 #define pm_genpd_restore_noirq          NULL
1159 #define pm_genpd_complete               NULL
1160
1161 #endif /* CONFIG_PM_SLEEP */
1162
1163 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1164                                         struct generic_pm_domain *genpd,
1165                                         struct gpd_timing_data *td)
1166 {
1167         struct generic_pm_domain_data *gpd_data;
1168         int ret;
1169
1170         ret = dev_pm_get_subsys_data(dev);
1171         if (ret)
1172                 return ERR_PTR(ret);
1173
1174         gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1175         if (!gpd_data) {
1176                 ret = -ENOMEM;
1177                 goto err_put;
1178         }
1179
1180         if (td)
1181                 gpd_data->td = *td;
1182
1183         gpd_data->base.dev = dev;
1184         gpd_data->td.constraint_changed = true;
1185         gpd_data->td.effective_constraint_ns = -1;
1186         gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1187
1188         spin_lock_irq(&dev->power.lock);
1189
1190         if (dev->power.subsys_data->domain_data) {
1191                 ret = -EINVAL;
1192                 goto err_free;
1193         }
1194
1195         dev->power.subsys_data->domain_data = &gpd_data->base;
1196         dev->pm_domain = &genpd->domain;
1197
1198         spin_unlock_irq(&dev->power.lock);
1199
1200         return gpd_data;
1201
1202  err_free:
1203         spin_unlock_irq(&dev->power.lock);
1204         kfree(gpd_data);
1205  err_put:
1206         dev_pm_put_subsys_data(dev);
1207         return ERR_PTR(ret);
1208 }
1209
1210 static void genpd_free_dev_data(struct device *dev,
1211                                 struct generic_pm_domain_data *gpd_data)
1212 {
1213         spin_lock_irq(&dev->power.lock);
1214
1215         dev->pm_domain = NULL;
1216         dev->power.subsys_data->domain_data = NULL;
1217
1218         spin_unlock_irq(&dev->power.lock);
1219
1220         kfree(gpd_data);
1221         dev_pm_put_subsys_data(dev);
1222 }
1223
1224 /**
1225  * __pm_genpd_add_device - Add a device to an I/O PM domain.
1226  * @genpd: PM domain to add the device to.
1227  * @dev: Device to be added.
1228  * @td: Set of PM QoS timing parameters to attach to the device.
1229  */
1230 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1231                           struct gpd_timing_data *td)
1232 {
1233         struct generic_pm_domain_data *gpd_data;
1234         int ret = 0;
1235
1236         dev_dbg(dev, "%s()\n", __func__);
1237
1238         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1239                 return -EINVAL;
1240
1241         gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1242         if (IS_ERR(gpd_data))
1243                 return PTR_ERR(gpd_data);
1244
1245         mutex_lock(&genpd->lock);
1246
1247         if (genpd->prepared_count > 0) {
1248                 ret = -EAGAIN;
1249                 goto out;
1250         }
1251
1252         ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1253         if (ret)
1254                 goto out;
1255
1256         genpd->device_count++;
1257         genpd->max_off_time_changed = true;
1258
1259         list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1260
1261  out:
1262         mutex_unlock(&genpd->lock);
1263
1264         if (ret)
1265                 genpd_free_dev_data(dev, gpd_data);
1266         else
1267                 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1268
1269         return ret;
1270 }
1271
1272 /**
1273  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1274  * @genpd: PM domain to remove the device from.
1275  * @dev: Device to be removed.
1276  */
1277 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1278                            struct device *dev)
1279 {
1280         struct generic_pm_domain_data *gpd_data;
1281         struct pm_domain_data *pdd;
1282         int ret = 0;
1283
1284         dev_dbg(dev, "%s()\n", __func__);
1285
1286         if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1287                 return -EINVAL;
1288
1289         /* The above validation also means we have existing domain_data. */
1290         pdd = dev->power.subsys_data->domain_data;
1291         gpd_data = to_gpd_data(pdd);
1292         dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1293
1294         mutex_lock(&genpd->lock);
1295
1296         if (genpd->prepared_count > 0) {
1297                 ret = -EAGAIN;
1298                 goto out;
1299         }
1300
1301         genpd->device_count--;
1302         genpd->max_off_time_changed = true;
1303
1304         if (genpd->detach_dev)
1305                 genpd->detach_dev(genpd, dev);
1306
1307         list_del_init(&pdd->list_node);
1308
1309         mutex_unlock(&genpd->lock);
1310
1311         genpd_free_dev_data(dev, gpd_data);
1312
1313         return 0;
1314
1315  out:
1316         mutex_unlock(&genpd->lock);
1317         dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1318
1319         return ret;
1320 }
1321
1322 /**
1323  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1324  * @genpd: Master PM domain to add the subdomain to.
1325  * @subdomain: Subdomain to be added.
1326  */
1327 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1328                            struct generic_pm_domain *subdomain)
1329 {
1330         struct gpd_link *link, *itr;
1331         int ret = 0;
1332
1333         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1334             || genpd == subdomain)
1335                 return -EINVAL;
1336
1337         link = kzalloc(sizeof(*link), GFP_KERNEL);
1338         if (!link)
1339                 return -ENOMEM;
1340
1341         mutex_lock(&subdomain->lock);
1342         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1343
1344         if (genpd->status == GPD_STATE_POWER_OFF
1345             &&  subdomain->status != GPD_STATE_POWER_OFF) {
1346                 ret = -EINVAL;
1347                 goto out;
1348         }
1349
1350         list_for_each_entry(itr, &genpd->master_links, master_node) {
1351                 if (itr->slave == subdomain && itr->master == genpd) {
1352                         ret = -EINVAL;
1353                         goto out;
1354                 }
1355         }
1356
1357         link->master = genpd;
1358         list_add_tail(&link->master_node, &genpd->master_links);
1359         link->slave = subdomain;
1360         list_add_tail(&link->slave_node, &subdomain->slave_links);
1361         if (subdomain->status != GPD_STATE_POWER_OFF)
1362                 genpd_sd_counter_inc(genpd);
1363
1364  out:
1365         mutex_unlock(&genpd->lock);
1366         mutex_unlock(&subdomain->lock);
1367         if (ret)
1368                 kfree(link);
1369         return ret;
1370 }
1371 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1372
1373 /**
1374  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1375  * @genpd: Master PM domain to remove the subdomain from.
1376  * @subdomain: Subdomain to be removed.
1377  */
1378 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1379                               struct generic_pm_domain *subdomain)
1380 {
1381         struct gpd_link *link;
1382         int ret = -EINVAL;
1383
1384         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1385                 return -EINVAL;
1386
1387         mutex_lock(&subdomain->lock);
1388         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1389
1390         if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1391                 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1392                         subdomain->name);
1393                 ret = -EBUSY;
1394                 goto out;
1395         }
1396
1397         list_for_each_entry(link, &genpd->master_links, master_node) {
1398                 if (link->slave != subdomain)
1399                         continue;
1400
1401                 list_del(&link->master_node);
1402                 list_del(&link->slave_node);
1403                 kfree(link);
1404                 if (subdomain->status != GPD_STATE_POWER_OFF)
1405                         genpd_sd_counter_dec(genpd);
1406
1407                 ret = 0;
1408                 break;
1409         }
1410
1411 out:
1412         mutex_unlock(&genpd->lock);
1413         mutex_unlock(&subdomain->lock);
1414
1415         return ret;
1416 }
1417 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1418
1419 /* Default device callbacks for generic PM domains. */
1420
1421 /**
1422  * pm_genpd_default_save_state - Default "save device state" for PM domains.
1423  * @dev: Device to handle.
1424  */
1425 static int pm_genpd_default_save_state(struct device *dev)
1426 {
1427         int (*cb)(struct device *__dev);
1428
1429         if (dev->type && dev->type->pm)
1430                 cb = dev->type->pm->runtime_suspend;
1431         else if (dev->class && dev->class->pm)
1432                 cb = dev->class->pm->runtime_suspend;
1433         else if (dev->bus && dev->bus->pm)
1434                 cb = dev->bus->pm->runtime_suspend;
1435         else
1436                 cb = NULL;
1437
1438         if (!cb && dev->driver && dev->driver->pm)
1439                 cb = dev->driver->pm->runtime_suspend;
1440
1441         return cb ? cb(dev) : 0;
1442 }
1443
1444 /**
1445  * pm_genpd_default_restore_state - Default PM domains "restore device state".
1446  * @dev: Device to handle.
1447  */
1448 static int pm_genpd_default_restore_state(struct device *dev)
1449 {
1450         int (*cb)(struct device *__dev);
1451
1452         if (dev->type && dev->type->pm)
1453                 cb = dev->type->pm->runtime_resume;
1454         else if (dev->class && dev->class->pm)
1455                 cb = dev->class->pm->runtime_resume;
1456         else if (dev->bus && dev->bus->pm)
1457                 cb = dev->bus->pm->runtime_resume;
1458         else
1459                 cb = NULL;
1460
1461         if (!cb && dev->driver && dev->driver->pm)
1462                 cb = dev->driver->pm->runtime_resume;
1463
1464         return cb ? cb(dev) : 0;
1465 }
1466
1467 /**
1468  * pm_genpd_init - Initialize a generic I/O PM domain object.
1469  * @genpd: PM domain object to initialize.
1470  * @gov: PM domain governor to associate with the domain (may be NULL).
1471  * @is_off: Initial value of the domain's power_is_off field.
1472  */
1473 void pm_genpd_init(struct generic_pm_domain *genpd,
1474                    struct dev_power_governor *gov, bool is_off)
1475 {
1476         if (IS_ERR_OR_NULL(genpd))
1477                 return;
1478
1479         INIT_LIST_HEAD(&genpd->master_links);
1480         INIT_LIST_HEAD(&genpd->slave_links);
1481         INIT_LIST_HEAD(&genpd->dev_list);
1482         mutex_init(&genpd->lock);
1483         genpd->gov = gov;
1484         INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1485         atomic_set(&genpd->sd_count, 0);
1486         genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1487         genpd->device_count = 0;
1488         genpd->max_off_time_ns = -1;
1489         genpd->max_off_time_changed = true;
1490         genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1491         genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1492         genpd->domain.ops.prepare = pm_genpd_prepare;
1493         genpd->domain.ops.suspend = pm_genpd_suspend;
1494         genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1495         genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1496         genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1497         genpd->domain.ops.resume_early = pm_genpd_resume_early;
1498         genpd->domain.ops.resume = pm_genpd_resume;
1499         genpd->domain.ops.freeze = pm_genpd_freeze;
1500         genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1501         genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1502         genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1503         genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1504         genpd->domain.ops.thaw = pm_genpd_thaw;
1505         genpd->domain.ops.poweroff = pm_genpd_suspend;
1506         genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1507         genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1508         genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1509         genpd->domain.ops.restore_early = pm_genpd_resume_early;
1510         genpd->domain.ops.restore = pm_genpd_resume;
1511         genpd->domain.ops.complete = pm_genpd_complete;
1512         genpd->dev_ops.save_state = pm_genpd_default_save_state;
1513         genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1514
1515         if (genpd->flags & GENPD_FLAG_PM_CLK) {
1516                 genpd->dev_ops.stop = pm_clk_suspend;
1517                 genpd->dev_ops.start = pm_clk_resume;
1518         }
1519
1520         mutex_lock(&gpd_list_lock);
1521         list_add(&genpd->gpd_list_node, &gpd_list);
1522         mutex_unlock(&gpd_list_lock);
1523 }
1524 EXPORT_SYMBOL_GPL(pm_genpd_init);
1525
1526 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1527 /*
1528  * Device Tree based PM domain providers.
1529  *
1530  * The code below implements generic device tree based PM domain providers that
1531  * bind device tree nodes with generic PM domains registered in the system.
1532  *
1533  * Any driver that registers generic PM domains and needs to support binding of
1534  * devices to these domains is supposed to register a PM domain provider, which
1535  * maps a PM domain specifier retrieved from the device tree to a PM domain.
1536  *
1537  * Two simple mapping functions have been provided for convenience:
1538  *  - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1539  *  - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1540  *    index.
1541  */
1542
1543 /**
1544  * struct of_genpd_provider - PM domain provider registration structure
1545  * @link: Entry in global list of PM domain providers
1546  * @node: Pointer to device tree node of PM domain provider
1547  * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1548  *         into a PM domain.
1549  * @data: context pointer to be passed into @xlate callback
1550  */
1551 struct of_genpd_provider {
1552         struct list_head link;
1553         struct device_node *node;
1554         genpd_xlate_t xlate;
1555         void *data;
1556 };
1557
1558 /* List of registered PM domain providers. */
1559 static LIST_HEAD(of_genpd_providers);
1560 /* Mutex to protect the list above. */
1561 static DEFINE_MUTEX(of_genpd_mutex);
1562
1563 /**
1564  * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1565  * @genpdspec: OF phandle args to map into a PM domain
1566  * @data: xlate function private data - pointer to struct generic_pm_domain
1567  *
1568  * This is a generic xlate function that can be used to model PM domains that
1569  * have their own device tree nodes. The private data of xlate function needs
1570  * to be a valid pointer to struct generic_pm_domain.
1571  */
1572 struct generic_pm_domain *__of_genpd_xlate_simple(
1573                                         struct of_phandle_args *genpdspec,
1574                                         void *data)
1575 {
1576         if (genpdspec->args_count != 0)
1577                 return ERR_PTR(-EINVAL);
1578         return data;
1579 }
1580 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1581
1582 /**
1583  * __of_genpd_xlate_onecell() - Xlate function using a single index.
1584  * @genpdspec: OF phandle args to map into a PM domain
1585  * @data: xlate function private data - pointer to struct genpd_onecell_data
1586  *
1587  * This is a generic xlate function that can be used to model simple PM domain
1588  * controllers that have one device tree node and provide multiple PM domains.
1589  * A single cell is used as an index into an array of PM domains specified in
1590  * the genpd_onecell_data struct when registering the provider.
1591  */
1592 struct generic_pm_domain *__of_genpd_xlate_onecell(
1593                                         struct of_phandle_args *genpdspec,
1594                                         void *data)
1595 {
1596         struct genpd_onecell_data *genpd_data = data;
1597         unsigned int idx = genpdspec->args[0];
1598
1599         if (genpdspec->args_count != 1)
1600                 return ERR_PTR(-EINVAL);
1601
1602         if (idx >= genpd_data->num_domains) {
1603                 pr_err("%s: invalid domain index %u\n", __func__, idx);
1604                 return ERR_PTR(-EINVAL);
1605         }
1606
1607         if (!genpd_data->domains[idx])
1608                 return ERR_PTR(-ENOENT);
1609
1610         return genpd_data->domains[idx];
1611 }
1612 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1613
1614 /**
1615  * __of_genpd_add_provider() - Register a PM domain provider for a node
1616  * @np: Device node pointer associated with the PM domain provider.
1617  * @xlate: Callback for decoding PM domain from phandle arguments.
1618  * @data: Context pointer for @xlate callback.
1619  */
1620 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1621                         void *data)
1622 {
1623         struct of_genpd_provider *cp;
1624
1625         cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1626         if (!cp)
1627                 return -ENOMEM;
1628
1629         cp->node = of_node_get(np);
1630         cp->data = data;
1631         cp->xlate = xlate;
1632
1633         mutex_lock(&of_genpd_mutex);
1634         list_add(&cp->link, &of_genpd_providers);
1635         mutex_unlock(&of_genpd_mutex);
1636         pr_debug("Added domain provider from %s\n", np->full_name);
1637
1638         return 0;
1639 }
1640 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1641
1642 /**
1643  * of_genpd_del_provider() - Remove a previously registered PM domain provider
1644  * @np: Device node pointer associated with the PM domain provider
1645  */
1646 void of_genpd_del_provider(struct device_node *np)
1647 {
1648         struct of_genpd_provider *cp;
1649
1650         mutex_lock(&of_genpd_mutex);
1651         list_for_each_entry(cp, &of_genpd_providers, link) {
1652                 if (cp->node == np) {
1653                         list_del(&cp->link);
1654                         of_node_put(cp->node);
1655                         kfree(cp);
1656                         break;
1657                 }
1658         }
1659         mutex_unlock(&of_genpd_mutex);
1660 }
1661 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1662
1663 /**
1664  * of_genpd_get_from_provider() - Look-up PM domain
1665  * @genpdspec: OF phandle args to use for look-up
1666  *
1667  * Looks for a PM domain provider under the node specified by @genpdspec and if
1668  * found, uses xlate function of the provider to map phandle args to a PM
1669  * domain.
1670  *
1671  * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1672  * on failure.
1673  */
1674 struct generic_pm_domain *of_genpd_get_from_provider(
1675                                         struct of_phandle_args *genpdspec)
1676 {
1677         struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1678         struct of_genpd_provider *provider;
1679
1680         mutex_lock(&of_genpd_mutex);
1681
1682         /* Check if we have such a provider in our array */
1683         list_for_each_entry(provider, &of_genpd_providers, link) {
1684                 if (provider->node == genpdspec->np)
1685                         genpd = provider->xlate(genpdspec, provider->data);
1686                 if (!IS_ERR(genpd))
1687                         break;
1688         }
1689
1690         mutex_unlock(&of_genpd_mutex);
1691
1692         return genpd;
1693 }
1694 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1695
1696 /**
1697  * genpd_dev_pm_detach - Detach a device from its PM domain.
1698  * @dev: Device to detach.
1699  * @power_off: Currently not used
1700  *
1701  * Try to locate a corresponding generic PM domain, which the device was
1702  * attached to previously. If such is found, the device is detached from it.
1703  */
1704 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1705 {
1706         struct generic_pm_domain *pd;
1707         unsigned int i;
1708         int ret = 0;
1709
1710         pd = pm_genpd_lookup_dev(dev);
1711         if (!pd)
1712                 return;
1713
1714         dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1715
1716         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1717                 ret = pm_genpd_remove_device(pd, dev);
1718                 if (ret != -EAGAIN)
1719                         break;
1720
1721                 mdelay(i);
1722                 cond_resched();
1723         }
1724
1725         if (ret < 0) {
1726                 dev_err(dev, "failed to remove from PM domain %s: %d",
1727                         pd->name, ret);
1728                 return;
1729         }
1730
1731         /* Check if PM domain can be powered off after removing this device. */
1732         genpd_queue_power_off_work(pd);
1733 }
1734
1735 static void genpd_dev_pm_sync(struct device *dev)
1736 {
1737         struct generic_pm_domain *pd;
1738
1739         pd = dev_to_genpd(dev);
1740         if (IS_ERR(pd))
1741                 return;
1742
1743         genpd_queue_power_off_work(pd);
1744 }
1745
1746 /**
1747  * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1748  * @dev: Device to attach.
1749  *
1750  * Parse device's OF node to find a PM domain specifier. If such is found,
1751  * attaches the device to retrieved pm_domain ops.
1752  *
1753  * Both generic and legacy Samsung-specific DT bindings are supported to keep
1754  * backwards compatibility with existing DTBs.
1755  *
1756  * Returns 0 on successfully attached PM domain or negative error code. Note
1757  * that if a power-domain exists for the device, but it cannot be found or
1758  * turned on, then return -EPROBE_DEFER to ensure that the device is not
1759  * probed and to re-try again later.
1760  */
1761 int genpd_dev_pm_attach(struct device *dev)
1762 {
1763         struct of_phandle_args pd_args;
1764         struct generic_pm_domain *pd;
1765         unsigned int i;
1766         int ret;
1767
1768         if (!dev->of_node)
1769                 return -ENODEV;
1770
1771         if (dev->pm_domain)
1772                 return -EEXIST;
1773
1774         ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1775                                         "#power-domain-cells", 0, &pd_args);
1776         if (ret < 0) {
1777                 if (ret != -ENOENT)
1778                         return ret;
1779
1780                 /*
1781                  * Try legacy Samsung-specific bindings
1782                  * (for backwards compatibility of DT ABI)
1783                  */
1784                 pd_args.args_count = 0;
1785                 pd_args.np = of_parse_phandle(dev->of_node,
1786                                                 "samsung,power-domain", 0);
1787                 if (!pd_args.np)
1788                         return -ENOENT;
1789         }
1790
1791         pd = of_genpd_get_from_provider(&pd_args);
1792         of_node_put(pd_args.np);
1793         if (IS_ERR(pd)) {
1794                 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1795                         __func__, PTR_ERR(pd));
1796                 return -EPROBE_DEFER;
1797         }
1798
1799         dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1800
1801         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1802                 ret = pm_genpd_add_device(pd, dev);
1803                 if (ret != -EAGAIN)
1804                         break;
1805
1806                 mdelay(i);
1807                 cond_resched();
1808         }
1809
1810         if (ret < 0) {
1811                 dev_err(dev, "failed to add to PM domain %s: %d",
1812                         pd->name, ret);
1813                 goto out;
1814         }
1815
1816         dev->pm_domain->detach = genpd_dev_pm_detach;
1817         dev->pm_domain->sync = genpd_dev_pm_sync;
1818         ret = genpd_poweron(pd);
1819
1820 out:
1821         return ret ? -EPROBE_DEFER : 0;
1822 }
1823 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1824 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1825
1826
1827 /***        debugfs support        ***/
1828
1829 #ifdef CONFIG_PM_ADVANCED_DEBUG
1830 #include <linux/pm.h>
1831 #include <linux/device.h>
1832 #include <linux/debugfs.h>
1833 #include <linux/seq_file.h>
1834 #include <linux/init.h>
1835 #include <linux/kobject.h>
1836 static struct dentry *pm_genpd_debugfs_dir;
1837
1838 /*
1839  * TODO: This function is a slightly modified version of rtpm_status_show
1840  * from sysfs.c, so generalize it.
1841  */
1842 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1843 {
1844         static const char * const status_lookup[] = {
1845                 [RPM_ACTIVE] = "active",
1846                 [RPM_RESUMING] = "resuming",
1847                 [RPM_SUSPENDED] = "suspended",
1848                 [RPM_SUSPENDING] = "suspending"
1849         };
1850         const char *p = "";
1851
1852         if (dev->power.runtime_error)
1853                 p = "error";
1854         else if (dev->power.disable_depth)
1855                 p = "unsupported";
1856         else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1857                 p = status_lookup[dev->power.runtime_status];
1858         else
1859                 WARN_ON(1);
1860
1861         seq_puts(s, p);
1862 }
1863
1864 static int pm_genpd_summary_one(struct seq_file *s,
1865                                 struct generic_pm_domain *genpd)
1866 {
1867         static const char * const status_lookup[] = {
1868                 [GPD_STATE_ACTIVE] = "on",
1869                 [GPD_STATE_POWER_OFF] = "off"
1870         };
1871         struct pm_domain_data *pm_data;
1872         const char *kobj_path;
1873         struct gpd_link *link;
1874         int ret;
1875
1876         ret = mutex_lock_interruptible(&genpd->lock);
1877         if (ret)
1878                 return -ERESTARTSYS;
1879
1880         if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1881                 goto exit;
1882         seq_printf(s, "%-30s  %-15s ", genpd->name, status_lookup[genpd->status]);
1883
1884         /*
1885          * Modifications on the list require holding locks on both
1886          * master and slave, so we are safe.
1887          * Also genpd->name is immutable.
1888          */
1889         list_for_each_entry(link, &genpd->master_links, master_node) {
1890                 seq_printf(s, "%s", link->slave->name);
1891                 if (!list_is_last(&link->master_node, &genpd->master_links))
1892                         seq_puts(s, ", ");
1893         }
1894
1895         list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1896                 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1897                 if (kobj_path == NULL)
1898                         continue;
1899
1900                 seq_printf(s, "\n    %-50s  ", kobj_path);
1901                 rtpm_status_str(s, pm_data->dev);
1902                 kfree(kobj_path);
1903         }
1904
1905         seq_puts(s, "\n");
1906 exit:
1907         mutex_unlock(&genpd->lock);
1908
1909         return 0;
1910 }
1911
1912 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1913 {
1914         struct generic_pm_domain *genpd;
1915         int ret = 0;
1916
1917         seq_puts(s, "domain                          status          slaves\n");
1918         seq_puts(s, "    /device                                             runtime status\n");
1919         seq_puts(s, "----------------------------------------------------------------------\n");
1920
1921         ret = mutex_lock_interruptible(&gpd_list_lock);
1922         if (ret)
1923                 return -ERESTARTSYS;
1924
1925         list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1926                 ret = pm_genpd_summary_one(s, genpd);
1927                 if (ret)
1928                         break;
1929         }
1930         mutex_unlock(&gpd_list_lock);
1931
1932         return ret;
1933 }
1934
1935 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1936 {
1937         return single_open(file, pm_genpd_summary_show, NULL);
1938 }
1939
1940 static const struct file_operations pm_genpd_summary_fops = {
1941         .open = pm_genpd_summary_open,
1942         .read = seq_read,
1943         .llseek = seq_lseek,
1944         .release = single_release,
1945 };
1946
1947 static int __init pm_genpd_debug_init(void)
1948 {
1949         struct dentry *d;
1950
1951         pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1952
1953         if (!pm_genpd_debugfs_dir)
1954                 return -ENOMEM;
1955
1956         d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1957                         pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1958         if (!d)
1959                 return -ENOMEM;
1960
1961         return 0;
1962 }
1963 late_initcall(pm_genpd_debug_init);
1964
1965 static void __exit pm_genpd_debug_exit(void)
1966 {
1967         debugfs_remove_recursive(pm_genpd_debugfs_dir);
1968 }
1969 __exitcall(pm_genpd_debug_exit);
1970 #endif /* CONFIG_PM_ADVANCED_DEBUG */