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