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