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