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