2ff818c2ae6bab2c18072a22a42984f76fa51ddc
[cascardo/linux.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 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1272
1273 /**
1274  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1275  * @genpd: PM domain to remove the device from.
1276  * @dev: Device to be removed.
1277  */
1278 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1279                            struct device *dev)
1280 {
1281         struct generic_pm_domain_data *gpd_data;
1282         struct pm_domain_data *pdd;
1283         int ret = 0;
1284
1285         dev_dbg(dev, "%s()\n", __func__);
1286
1287         if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1288                 return -EINVAL;
1289
1290         /* The above validation also means we have existing domain_data. */
1291         pdd = dev->power.subsys_data->domain_data;
1292         gpd_data = to_gpd_data(pdd);
1293         dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1294
1295         mutex_lock(&genpd->lock);
1296
1297         if (genpd->prepared_count > 0) {
1298                 ret = -EAGAIN;
1299                 goto out;
1300         }
1301
1302         genpd->device_count--;
1303         genpd->max_off_time_changed = true;
1304
1305         if (genpd->detach_dev)
1306                 genpd->detach_dev(genpd, dev);
1307
1308         list_del_init(&pdd->list_node);
1309
1310         mutex_unlock(&genpd->lock);
1311
1312         genpd_free_dev_data(dev, gpd_data);
1313
1314         return 0;
1315
1316  out:
1317         mutex_unlock(&genpd->lock);
1318         dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1319
1320         return ret;
1321 }
1322 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1323
1324 /**
1325  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1326  * @genpd: Master PM domain to add the subdomain to.
1327  * @subdomain: Subdomain to be added.
1328  */
1329 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1330                            struct generic_pm_domain *subdomain)
1331 {
1332         struct gpd_link *link, *itr;
1333         int ret = 0;
1334
1335         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1336             || genpd == subdomain)
1337                 return -EINVAL;
1338
1339         link = kzalloc(sizeof(*link), GFP_KERNEL);
1340         if (!link)
1341                 return -ENOMEM;
1342
1343         mutex_lock(&subdomain->lock);
1344         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1345
1346         if (genpd->status == GPD_STATE_POWER_OFF
1347             &&  subdomain->status != GPD_STATE_POWER_OFF) {
1348                 ret = -EINVAL;
1349                 goto out;
1350         }
1351
1352         list_for_each_entry(itr, &genpd->master_links, master_node) {
1353                 if (itr->slave == subdomain && itr->master == genpd) {
1354                         ret = -EINVAL;
1355                         goto out;
1356                 }
1357         }
1358
1359         link->master = genpd;
1360         list_add_tail(&link->master_node, &genpd->master_links);
1361         link->slave = subdomain;
1362         list_add_tail(&link->slave_node, &subdomain->slave_links);
1363         if (subdomain->status != GPD_STATE_POWER_OFF)
1364                 genpd_sd_counter_inc(genpd);
1365
1366  out:
1367         mutex_unlock(&genpd->lock);
1368         mutex_unlock(&subdomain->lock);
1369         if (ret)
1370                 kfree(link);
1371         return ret;
1372 }
1373 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1374
1375 /**
1376  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1377  * @genpd: Master PM domain to remove the subdomain from.
1378  * @subdomain: Subdomain to be removed.
1379  */
1380 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1381                               struct generic_pm_domain *subdomain)
1382 {
1383         struct gpd_link *link;
1384         int ret = -EINVAL;
1385
1386         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1387                 return -EINVAL;
1388
1389         mutex_lock(&subdomain->lock);
1390         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1391
1392         if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1393                 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1394                         subdomain->name);
1395                 ret = -EBUSY;
1396                 goto out;
1397         }
1398
1399         list_for_each_entry(link, &genpd->master_links, master_node) {
1400                 if (link->slave != subdomain)
1401                         continue;
1402
1403                 list_del(&link->master_node);
1404                 list_del(&link->slave_node);
1405                 kfree(link);
1406                 if (subdomain->status != GPD_STATE_POWER_OFF)
1407                         genpd_sd_counter_dec(genpd);
1408
1409                 ret = 0;
1410                 break;
1411         }
1412
1413 out:
1414         mutex_unlock(&genpd->lock);
1415         mutex_unlock(&subdomain->lock);
1416
1417         return ret;
1418 }
1419 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1420
1421 /* Default device callbacks for generic PM domains. */
1422
1423 /**
1424  * pm_genpd_default_save_state - Default "save device state" for PM domains.
1425  * @dev: Device to handle.
1426  */
1427 static int pm_genpd_default_save_state(struct device *dev)
1428 {
1429         int (*cb)(struct device *__dev);
1430
1431         if (dev->type && dev->type->pm)
1432                 cb = dev->type->pm->runtime_suspend;
1433         else if (dev->class && dev->class->pm)
1434                 cb = dev->class->pm->runtime_suspend;
1435         else if (dev->bus && dev->bus->pm)
1436                 cb = dev->bus->pm->runtime_suspend;
1437         else
1438                 cb = NULL;
1439
1440         if (!cb && dev->driver && dev->driver->pm)
1441                 cb = dev->driver->pm->runtime_suspend;
1442
1443         return cb ? cb(dev) : 0;
1444 }
1445
1446 /**
1447  * pm_genpd_default_restore_state - Default PM domains "restore device state".
1448  * @dev: Device to handle.
1449  */
1450 static int pm_genpd_default_restore_state(struct device *dev)
1451 {
1452         int (*cb)(struct device *__dev);
1453
1454         if (dev->type && dev->type->pm)
1455                 cb = dev->type->pm->runtime_resume;
1456         else if (dev->class && dev->class->pm)
1457                 cb = dev->class->pm->runtime_resume;
1458         else if (dev->bus && dev->bus->pm)
1459                 cb = dev->bus->pm->runtime_resume;
1460         else
1461                 cb = NULL;
1462
1463         if (!cb && dev->driver && dev->driver->pm)
1464                 cb = dev->driver->pm->runtime_resume;
1465
1466         return cb ? cb(dev) : 0;
1467 }
1468
1469 /**
1470  * pm_genpd_init - Initialize a generic I/O PM domain object.
1471  * @genpd: PM domain object to initialize.
1472  * @gov: PM domain governor to associate with the domain (may be NULL).
1473  * @is_off: Initial value of the domain's power_is_off field.
1474  */
1475 void pm_genpd_init(struct generic_pm_domain *genpd,
1476                    struct dev_power_governor *gov, bool is_off)
1477 {
1478         if (IS_ERR_OR_NULL(genpd))
1479                 return;
1480
1481         INIT_LIST_HEAD(&genpd->master_links);
1482         INIT_LIST_HEAD(&genpd->slave_links);
1483         INIT_LIST_HEAD(&genpd->dev_list);
1484         mutex_init(&genpd->lock);
1485         genpd->gov = gov;
1486         INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1487         atomic_set(&genpd->sd_count, 0);
1488         genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1489         genpd->device_count = 0;
1490         genpd->max_off_time_ns = -1;
1491         genpd->max_off_time_changed = true;
1492         genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1493         genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1494         genpd->domain.ops.prepare = pm_genpd_prepare;
1495         genpd->domain.ops.suspend = pm_genpd_suspend;
1496         genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1497         genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1498         genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1499         genpd->domain.ops.resume_early = pm_genpd_resume_early;
1500         genpd->domain.ops.resume = pm_genpd_resume;
1501         genpd->domain.ops.freeze = pm_genpd_freeze;
1502         genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1503         genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1504         genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1505         genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1506         genpd->domain.ops.thaw = pm_genpd_thaw;
1507         genpd->domain.ops.poweroff = pm_genpd_suspend;
1508         genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1509         genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1510         genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1511         genpd->domain.ops.restore_early = pm_genpd_resume_early;
1512         genpd->domain.ops.restore = pm_genpd_resume;
1513         genpd->domain.ops.complete = pm_genpd_complete;
1514         genpd->dev_ops.save_state = pm_genpd_default_save_state;
1515         genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1516
1517         if (genpd->flags & GENPD_FLAG_PM_CLK) {
1518                 genpd->dev_ops.stop = pm_clk_suspend;
1519                 genpd->dev_ops.start = pm_clk_resume;
1520         }
1521
1522         mutex_lock(&gpd_list_lock);
1523         list_add(&genpd->gpd_list_node, &gpd_list);
1524         mutex_unlock(&gpd_list_lock);
1525 }
1526 EXPORT_SYMBOL_GPL(pm_genpd_init);
1527
1528 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1529 /*
1530  * Device Tree based PM domain providers.
1531  *
1532  * The code below implements generic device tree based PM domain providers that
1533  * bind device tree nodes with generic PM domains registered in the system.
1534  *
1535  * Any driver that registers generic PM domains and needs to support binding of
1536  * devices to these domains is supposed to register a PM domain provider, which
1537  * maps a PM domain specifier retrieved from the device tree to a PM domain.
1538  *
1539  * Two simple mapping functions have been provided for convenience:
1540  *  - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1541  *  - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1542  *    index.
1543  */
1544
1545 /**
1546  * struct of_genpd_provider - PM domain provider registration structure
1547  * @link: Entry in global list of PM domain providers
1548  * @node: Pointer to device tree node of PM domain provider
1549  * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1550  *         into a PM domain.
1551  * @data: context pointer to be passed into @xlate callback
1552  */
1553 struct of_genpd_provider {
1554         struct list_head link;
1555         struct device_node *node;
1556         genpd_xlate_t xlate;
1557         void *data;
1558 };
1559
1560 /* List of registered PM domain providers. */
1561 static LIST_HEAD(of_genpd_providers);
1562 /* Mutex to protect the list above. */
1563 static DEFINE_MUTEX(of_genpd_mutex);
1564
1565 /**
1566  * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1567  * @genpdspec: OF phandle args to map into a PM domain
1568  * @data: xlate function private data - pointer to struct generic_pm_domain
1569  *
1570  * This is a generic xlate function that can be used to model PM domains that
1571  * have their own device tree nodes. The private data of xlate function needs
1572  * to be a valid pointer to struct generic_pm_domain.
1573  */
1574 struct generic_pm_domain *__of_genpd_xlate_simple(
1575                                         struct of_phandle_args *genpdspec,
1576                                         void *data)
1577 {
1578         if (genpdspec->args_count != 0)
1579                 return ERR_PTR(-EINVAL);
1580         return data;
1581 }
1582 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1583
1584 /**
1585  * __of_genpd_xlate_onecell() - Xlate function using a single index.
1586  * @genpdspec: OF phandle args to map into a PM domain
1587  * @data: xlate function private data - pointer to struct genpd_onecell_data
1588  *
1589  * This is a generic xlate function that can be used to model simple PM domain
1590  * controllers that have one device tree node and provide multiple PM domains.
1591  * A single cell is used as an index into an array of PM domains specified in
1592  * the genpd_onecell_data struct when registering the provider.
1593  */
1594 struct generic_pm_domain *__of_genpd_xlate_onecell(
1595                                         struct of_phandle_args *genpdspec,
1596                                         void *data)
1597 {
1598         struct genpd_onecell_data *genpd_data = data;
1599         unsigned int idx = genpdspec->args[0];
1600
1601         if (genpdspec->args_count != 1)
1602                 return ERR_PTR(-EINVAL);
1603
1604         if (idx >= genpd_data->num_domains) {
1605                 pr_err("%s: invalid domain index %u\n", __func__, idx);
1606                 return ERR_PTR(-EINVAL);
1607         }
1608
1609         if (!genpd_data->domains[idx])
1610                 return ERR_PTR(-ENOENT);
1611
1612         return genpd_data->domains[idx];
1613 }
1614 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1615
1616 /**
1617  * __of_genpd_add_provider() - Register a PM domain provider for a node
1618  * @np: Device node pointer associated with the PM domain provider.
1619  * @xlate: Callback for decoding PM domain from phandle arguments.
1620  * @data: Context pointer for @xlate callback.
1621  */
1622 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1623                         void *data)
1624 {
1625         struct of_genpd_provider *cp;
1626
1627         cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1628         if (!cp)
1629                 return -ENOMEM;
1630
1631         cp->node = of_node_get(np);
1632         cp->data = data;
1633         cp->xlate = xlate;
1634
1635         mutex_lock(&of_genpd_mutex);
1636         list_add(&cp->link, &of_genpd_providers);
1637         mutex_unlock(&of_genpd_mutex);
1638         pr_debug("Added domain provider from %s\n", np->full_name);
1639
1640         return 0;
1641 }
1642 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1643
1644 /**
1645  * of_genpd_del_provider() - Remove a previously registered PM domain provider
1646  * @np: Device node pointer associated with the PM domain provider
1647  */
1648 void of_genpd_del_provider(struct device_node *np)
1649 {
1650         struct of_genpd_provider *cp;
1651
1652         mutex_lock(&of_genpd_mutex);
1653         list_for_each_entry(cp, &of_genpd_providers, link) {
1654                 if (cp->node == np) {
1655                         list_del(&cp->link);
1656                         of_node_put(cp->node);
1657                         kfree(cp);
1658                         break;
1659                 }
1660         }
1661         mutex_unlock(&of_genpd_mutex);
1662 }
1663 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1664
1665 /**
1666  * of_genpd_get_from_provider() - Look-up PM domain
1667  * @genpdspec: OF phandle args to use for look-up
1668  *
1669  * Looks for a PM domain provider under the node specified by @genpdspec and if
1670  * found, uses xlate function of the provider to map phandle args to a PM
1671  * domain.
1672  *
1673  * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1674  * on failure.
1675  */
1676 struct generic_pm_domain *of_genpd_get_from_provider(
1677                                         struct of_phandle_args *genpdspec)
1678 {
1679         struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1680         struct of_genpd_provider *provider;
1681
1682         mutex_lock(&of_genpd_mutex);
1683
1684         /* Check if we have such a provider in our array */
1685         list_for_each_entry(provider, &of_genpd_providers, link) {
1686                 if (provider->node == genpdspec->np)
1687                         genpd = provider->xlate(genpdspec, provider->data);
1688                 if (!IS_ERR(genpd))
1689                         break;
1690         }
1691
1692         mutex_unlock(&of_genpd_mutex);
1693
1694         return genpd;
1695 }
1696 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1697
1698 /**
1699  * genpd_dev_pm_detach - Detach a device from its PM domain.
1700  * @dev: Device to detach.
1701  * @power_off: Currently not used
1702  *
1703  * Try to locate a corresponding generic PM domain, which the device was
1704  * attached to previously. If such is found, the device is detached from it.
1705  */
1706 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1707 {
1708         struct generic_pm_domain *pd;
1709         unsigned int i;
1710         int ret = 0;
1711
1712         pd = pm_genpd_lookup_dev(dev);
1713         if (!pd)
1714                 return;
1715
1716         dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1717
1718         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1719                 ret = pm_genpd_remove_device(pd, dev);
1720                 if (ret != -EAGAIN)
1721                         break;
1722
1723                 mdelay(i);
1724                 cond_resched();
1725         }
1726
1727         if (ret < 0) {
1728                 dev_err(dev, "failed to remove from PM domain %s: %d",
1729                         pd->name, ret);
1730                 return;
1731         }
1732
1733         /* Check if PM domain can be powered off after removing this device. */
1734         genpd_queue_power_off_work(pd);
1735 }
1736
1737 static void genpd_dev_pm_sync(struct device *dev)
1738 {
1739         struct generic_pm_domain *pd;
1740
1741         pd = dev_to_genpd(dev);
1742         if (IS_ERR(pd))
1743                 return;
1744
1745         genpd_queue_power_off_work(pd);
1746 }
1747
1748 /**
1749  * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1750  * @dev: Device to attach.
1751  *
1752  * Parse device's OF node to find a PM domain specifier. If such is found,
1753  * attaches the device to retrieved pm_domain ops.
1754  *
1755  * Both generic and legacy Samsung-specific DT bindings are supported to keep
1756  * backwards compatibility with existing DTBs.
1757  *
1758  * Returns 0 on successfully attached PM domain or negative error code. Note
1759  * that if a power-domain exists for the device, but it cannot be found or
1760  * turned on, then return -EPROBE_DEFER to ensure that the device is not
1761  * probed and to re-try again later.
1762  */
1763 int genpd_dev_pm_attach(struct device *dev)
1764 {
1765         struct of_phandle_args pd_args;
1766         struct generic_pm_domain *pd;
1767         unsigned int i;
1768         int ret;
1769
1770         if (!dev->of_node)
1771                 return -ENODEV;
1772
1773         if (dev->pm_domain)
1774                 return -EEXIST;
1775
1776         ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1777                                         "#power-domain-cells", 0, &pd_args);
1778         if (ret < 0) {
1779                 if (ret != -ENOENT)
1780                         return ret;
1781
1782                 /*
1783                  * Try legacy Samsung-specific bindings
1784                  * (for backwards compatibility of DT ABI)
1785                  */
1786                 pd_args.args_count = 0;
1787                 pd_args.np = of_parse_phandle(dev->of_node,
1788                                                 "samsung,power-domain", 0);
1789                 if (!pd_args.np)
1790                         return -ENOENT;
1791         }
1792
1793         pd = of_genpd_get_from_provider(&pd_args);
1794         of_node_put(pd_args.np);
1795         if (IS_ERR(pd)) {
1796                 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1797                         __func__, PTR_ERR(pd));
1798                 return -EPROBE_DEFER;
1799         }
1800
1801         dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1802
1803         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1804                 ret = pm_genpd_add_device(pd, dev);
1805                 if (ret != -EAGAIN)
1806                         break;
1807
1808                 mdelay(i);
1809                 cond_resched();
1810         }
1811
1812         if (ret < 0) {
1813                 dev_err(dev, "failed to add to PM domain %s: %d",
1814                         pd->name, ret);
1815                 goto out;
1816         }
1817
1818         dev->pm_domain->detach = genpd_dev_pm_detach;
1819         dev->pm_domain->sync = genpd_dev_pm_sync;
1820         ret = genpd_poweron(pd);
1821
1822 out:
1823         return ret ? -EPROBE_DEFER : 0;
1824 }
1825 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1826 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1827
1828
1829 /***        debugfs support        ***/
1830
1831 #ifdef CONFIG_PM_ADVANCED_DEBUG
1832 #include <linux/pm.h>
1833 #include <linux/device.h>
1834 #include <linux/debugfs.h>
1835 #include <linux/seq_file.h>
1836 #include <linux/init.h>
1837 #include <linux/kobject.h>
1838 static struct dentry *pm_genpd_debugfs_dir;
1839
1840 /*
1841  * TODO: This function is a slightly modified version of rtpm_status_show
1842  * from sysfs.c, so generalize it.
1843  */
1844 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1845 {
1846         static const char * const status_lookup[] = {
1847                 [RPM_ACTIVE] = "active",
1848                 [RPM_RESUMING] = "resuming",
1849                 [RPM_SUSPENDED] = "suspended",
1850                 [RPM_SUSPENDING] = "suspending"
1851         };
1852         const char *p = "";
1853
1854         if (dev->power.runtime_error)
1855                 p = "error";
1856         else if (dev->power.disable_depth)
1857                 p = "unsupported";
1858         else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1859                 p = status_lookup[dev->power.runtime_status];
1860         else
1861                 WARN_ON(1);
1862
1863         seq_puts(s, p);
1864 }
1865
1866 static int pm_genpd_summary_one(struct seq_file *s,
1867                                 struct generic_pm_domain *genpd)
1868 {
1869         static const char * const status_lookup[] = {
1870                 [GPD_STATE_ACTIVE] = "on",
1871                 [GPD_STATE_POWER_OFF] = "off"
1872         };
1873         struct pm_domain_data *pm_data;
1874         const char *kobj_path;
1875         struct gpd_link *link;
1876         int ret;
1877
1878         ret = mutex_lock_interruptible(&genpd->lock);
1879         if (ret)
1880                 return -ERESTARTSYS;
1881
1882         if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1883                 goto exit;
1884         seq_printf(s, "%-30s  %-15s ", genpd->name, status_lookup[genpd->status]);
1885
1886         /*
1887          * Modifications on the list require holding locks on both
1888          * master and slave, so we are safe.
1889          * Also genpd->name is immutable.
1890          */
1891         list_for_each_entry(link, &genpd->master_links, master_node) {
1892                 seq_printf(s, "%s", link->slave->name);
1893                 if (!list_is_last(&link->master_node, &genpd->master_links))
1894                         seq_puts(s, ", ");
1895         }
1896
1897         list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1898                 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1899                 if (kobj_path == NULL)
1900                         continue;
1901
1902                 seq_printf(s, "\n    %-50s  ", kobj_path);
1903                 rtpm_status_str(s, pm_data->dev);
1904                 kfree(kobj_path);
1905         }
1906
1907         seq_puts(s, "\n");
1908 exit:
1909         mutex_unlock(&genpd->lock);
1910
1911         return 0;
1912 }
1913
1914 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1915 {
1916         struct generic_pm_domain *genpd;
1917         int ret = 0;
1918
1919         seq_puts(s, "domain                          status          slaves\n");
1920         seq_puts(s, "    /device                                             runtime status\n");
1921         seq_puts(s, "----------------------------------------------------------------------\n");
1922
1923         ret = mutex_lock_interruptible(&gpd_list_lock);
1924         if (ret)
1925                 return -ERESTARTSYS;
1926
1927         list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1928                 ret = pm_genpd_summary_one(s, genpd);
1929                 if (ret)
1930                         break;
1931         }
1932         mutex_unlock(&gpd_list_lock);
1933
1934         return ret;
1935 }
1936
1937 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1938 {
1939         return single_open(file, pm_genpd_summary_show, NULL);
1940 }
1941
1942 static const struct file_operations pm_genpd_summary_fops = {
1943         .open = pm_genpd_summary_open,
1944         .read = seq_read,
1945         .llseek = seq_lseek,
1946         .release = single_release,
1947 };
1948
1949 static int __init pm_genpd_debug_init(void)
1950 {
1951         struct dentry *d;
1952
1953         pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1954
1955         if (!pm_genpd_debugfs_dir)
1956                 return -ENOMEM;
1957
1958         d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1959                         pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1960         if (!d)
1961                 return -ENOMEM;
1962
1963         return 0;
1964 }
1965 late_initcall(pm_genpd_debug_init);
1966
1967 static void __exit pm_genpd_debug_exit(void)
1968 {
1969         debugfs_remove_recursive(pm_genpd_debugfs_dir);
1970 }
1971 __exitcall(pm_genpd_debug_exit);
1972 #endif /* CONFIG_PM_ADVANCED_DEBUG */