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