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