Merge tag 'pm-5.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[muen/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 #define pr_fmt(fmt) "PM: " fmt
10
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_opp.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/pm_domain.h>
18 #include <linux/pm_qos.h>
19 #include <linux/pm_clock.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/sched.h>
23 #include <linux/suspend.h>
24 #include <linux/export.h>
25
26 #include "power.h"
27
28 #define GENPD_RETRY_MAX_MS      250             /* Approximate */
29
30 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev)          \
31 ({                                                              \
32         type (*__routine)(struct device *__d);                  \
33         type __ret = (type)0;                                   \
34                                                                 \
35         __routine = genpd->dev_ops.callback;                    \
36         if (__routine) {                                        \
37                 __ret = __routine(dev);                         \
38         }                                                       \
39         __ret;                                                  \
40 })
41
42 static LIST_HEAD(gpd_list);
43 static DEFINE_MUTEX(gpd_list_lock);
44
45 struct genpd_lock_ops {
46         void (*lock)(struct generic_pm_domain *genpd);
47         void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
48         int (*lock_interruptible)(struct generic_pm_domain *genpd);
49         void (*unlock)(struct generic_pm_domain *genpd);
50 };
51
52 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
53 {
54         mutex_lock(&genpd->mlock);
55 }
56
57 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
58                                         int depth)
59 {
60         mutex_lock_nested(&genpd->mlock, depth);
61 }
62
63 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
64 {
65         return mutex_lock_interruptible(&genpd->mlock);
66 }
67
68 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
69 {
70         return mutex_unlock(&genpd->mlock);
71 }
72
73 static const struct genpd_lock_ops genpd_mtx_ops = {
74         .lock = genpd_lock_mtx,
75         .lock_nested = genpd_lock_nested_mtx,
76         .lock_interruptible = genpd_lock_interruptible_mtx,
77         .unlock = genpd_unlock_mtx,
78 };
79
80 static void genpd_lock_spin(struct generic_pm_domain *genpd)
81         __acquires(&genpd->slock)
82 {
83         unsigned long flags;
84
85         spin_lock_irqsave(&genpd->slock, flags);
86         genpd->lock_flags = flags;
87 }
88
89 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
90                                         int depth)
91         __acquires(&genpd->slock)
92 {
93         unsigned long flags;
94
95         spin_lock_irqsave_nested(&genpd->slock, flags, depth);
96         genpd->lock_flags = flags;
97 }
98
99 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
100         __acquires(&genpd->slock)
101 {
102         unsigned long flags;
103
104         spin_lock_irqsave(&genpd->slock, flags);
105         genpd->lock_flags = flags;
106         return 0;
107 }
108
109 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
110         __releases(&genpd->slock)
111 {
112         spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
113 }
114
115 static const struct genpd_lock_ops genpd_spin_ops = {
116         .lock = genpd_lock_spin,
117         .lock_nested = genpd_lock_nested_spin,
118         .lock_interruptible = genpd_lock_interruptible_spin,
119         .unlock = genpd_unlock_spin,
120 };
121
122 #define genpd_lock(p)                   p->lock_ops->lock(p)
123 #define genpd_lock_nested(p, d)         p->lock_ops->lock_nested(p, d)
124 #define genpd_lock_interruptible(p)     p->lock_ops->lock_interruptible(p)
125 #define genpd_unlock(p)                 p->lock_ops->unlock(p)
126
127 #define genpd_status_on(genpd)          (genpd->status == GPD_STATE_ACTIVE)
128 #define genpd_is_irq_safe(genpd)        (genpd->flags & GENPD_FLAG_IRQ_SAFE)
129 #define genpd_is_always_on(genpd)       (genpd->flags & GENPD_FLAG_ALWAYS_ON)
130 #define genpd_is_active_wakeup(genpd)   (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
131
132 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
133                 const struct generic_pm_domain *genpd)
134 {
135         bool ret;
136
137         ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
138
139         /*
140          * Warn once if an IRQ safe device is attached to a no sleep domain, as
141          * to indicate a suboptimal configuration for PM. For an always on
142          * domain this isn't case, thus don't warn.
143          */
144         if (ret && !genpd_is_always_on(genpd))
145                 dev_warn_once(dev, "PM domain %s will not be powered off\n",
146                                 genpd->name);
147
148         return ret;
149 }
150
151 /*
152  * Get the generic PM domain for a particular struct device.
153  * This validates the struct device pointer, the PM domain pointer,
154  * and checks that the PM domain pointer is a real generic PM domain.
155  * Any failure results in NULL being returned.
156  */
157 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
158 {
159         struct generic_pm_domain *genpd = NULL, *gpd;
160
161         if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
162                 return NULL;
163
164         mutex_lock(&gpd_list_lock);
165         list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
166                 if (&gpd->domain == dev->pm_domain) {
167                         genpd = gpd;
168                         break;
169                 }
170         }
171         mutex_unlock(&gpd_list_lock);
172
173         return genpd;
174 }
175
176 /*
177  * This should only be used where we are certain that the pm_domain
178  * attached to the device is a genpd domain.
179  */
180 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
181 {
182         if (IS_ERR_OR_NULL(dev->pm_domain))
183                 return ERR_PTR(-EINVAL);
184
185         return pd_to_genpd(dev->pm_domain);
186 }
187
188 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
189                           struct device *dev)
190 {
191         return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
192 }
193
194 static int genpd_start_dev(const struct generic_pm_domain *genpd,
195                            struct device *dev)
196 {
197         return GENPD_DEV_CALLBACK(genpd, int, start, dev);
198 }
199
200 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
201 {
202         bool ret = false;
203
204         if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
205                 ret = !!atomic_dec_and_test(&genpd->sd_count);
206
207         return ret;
208 }
209
210 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
211 {
212         atomic_inc(&genpd->sd_count);
213         smp_mb__after_atomic();
214 }
215
216 #ifdef CONFIG_DEBUG_FS
217 static void genpd_update_accounting(struct generic_pm_domain *genpd)
218 {
219         ktime_t delta, now;
220
221         now = ktime_get();
222         delta = ktime_sub(now, genpd->accounting_time);
223
224         /*
225          * If genpd->status is active, it means we are just
226          * out of off and so update the idle time and vice
227          * versa.
228          */
229         if (genpd->status == GPD_STATE_ACTIVE) {
230                 int state_idx = genpd->state_idx;
231
232                 genpd->states[state_idx].idle_time =
233                         ktime_add(genpd->states[state_idx].idle_time, delta);
234         } else {
235                 genpd->on_time = ktime_add(genpd->on_time, delta);
236         }
237
238         genpd->accounting_time = now;
239 }
240 #else
241 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
242 #endif
243
244 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
245                                            unsigned int state)
246 {
247         struct generic_pm_domain_data *pd_data;
248         struct pm_domain_data *pdd;
249         struct gpd_link *link;
250
251         /* New requested state is same as Max requested state */
252         if (state == genpd->performance_state)
253                 return state;
254
255         /* New requested state is higher than Max requested state */
256         if (state > genpd->performance_state)
257                 return state;
258
259         /* Traverse all devices within the domain */
260         list_for_each_entry(pdd, &genpd->dev_list, list_node) {
261                 pd_data = to_gpd_data(pdd);
262
263                 if (pd_data->performance_state > state)
264                         state = pd_data->performance_state;
265         }
266
267         /*
268          * Traverse all sub-domains within the domain. This can be
269          * done without any additional locking as the link->performance_state
270          * field is protected by the master genpd->lock, which is already taken.
271          *
272          * Also note that link->performance_state (subdomain's performance state
273          * requirement to master domain) is different from
274          * link->slave->performance_state (current performance state requirement
275          * of the devices/sub-domains of the subdomain) and so can have a
276          * different value.
277          *
278          * Note that we also take vote from powered-off sub-domains into account
279          * as the same is done for devices right now.
280          */
281         list_for_each_entry(link, &genpd->master_links, master_node) {
282                 if (link->performance_state > state)
283                         state = link->performance_state;
284         }
285
286         return state;
287 }
288
289 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
290                                         unsigned int state, int depth)
291 {
292         struct generic_pm_domain *master;
293         struct gpd_link *link;
294         int master_state, ret;
295
296         if (state == genpd->performance_state)
297                 return 0;
298
299         /* Propagate to masters of genpd */
300         list_for_each_entry(link, &genpd->slave_links, slave_node) {
301                 master = link->master;
302
303                 if (!master->set_performance_state)
304                         continue;
305
306                 /* Find master's performance state */
307                 ret = dev_pm_opp_xlate_performance_state(genpd->opp_table,
308                                                          master->opp_table,
309                                                          state);
310                 if (unlikely(ret < 0))
311                         goto err;
312
313                 master_state = ret;
314
315                 genpd_lock_nested(master, depth + 1);
316
317                 link->prev_performance_state = link->performance_state;
318                 link->performance_state = master_state;
319                 master_state = _genpd_reeval_performance_state(master,
320                                                 master_state);
321                 ret = _genpd_set_performance_state(master, master_state, depth + 1);
322                 if (ret)
323                         link->performance_state = link->prev_performance_state;
324
325                 genpd_unlock(master);
326
327                 if (ret)
328                         goto err;
329         }
330
331         ret = genpd->set_performance_state(genpd, state);
332         if (ret)
333                 goto err;
334
335         genpd->performance_state = state;
336         return 0;
337
338 err:
339         /* Encountered an error, lets rollback */
340         list_for_each_entry_continue_reverse(link, &genpd->slave_links,
341                                              slave_node) {
342                 master = link->master;
343
344                 if (!master->set_performance_state)
345                         continue;
346
347                 genpd_lock_nested(master, depth + 1);
348
349                 master_state = link->prev_performance_state;
350                 link->performance_state = master_state;
351
352                 master_state = _genpd_reeval_performance_state(master,
353                                                 master_state);
354                 if (_genpd_set_performance_state(master, master_state, depth + 1)) {
355                         pr_err("%s: Failed to roll back to %d performance state\n",
356                                master->name, master_state);
357                 }
358
359                 genpd_unlock(master);
360         }
361
362         return ret;
363 }
364
365 /**
366  * dev_pm_genpd_set_performance_state- Set performance state of device's power
367  * domain.
368  *
369  * @dev: Device for which the performance-state needs to be set.
370  * @state: Target performance state of the device. This can be set as 0 when the
371  *         device doesn't have any performance state constraints left (And so
372  *         the device wouldn't participate anymore to find the target
373  *         performance state of the genpd).
374  *
375  * It is assumed that the users guarantee that the genpd wouldn't be detached
376  * while this routine is getting called.
377  *
378  * Returns 0 on success and negative error values on failures.
379  */
380 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
381 {
382         struct generic_pm_domain *genpd;
383         struct generic_pm_domain_data *gpd_data;
384         unsigned int prev;
385         int ret;
386
387         genpd = dev_to_genpd(dev);
388         if (IS_ERR(genpd))
389                 return -ENODEV;
390
391         if (unlikely(!genpd->set_performance_state))
392                 return -EINVAL;
393
394         if (unlikely(!dev->power.subsys_data ||
395                      !dev->power.subsys_data->domain_data)) {
396                 WARN_ON(1);
397                 return -EINVAL;
398         }
399
400         genpd_lock(genpd);
401
402         gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
403         prev = gpd_data->performance_state;
404         gpd_data->performance_state = state;
405
406         state = _genpd_reeval_performance_state(genpd, state);
407         ret = _genpd_set_performance_state(genpd, state, 0);
408         if (ret)
409                 gpd_data->performance_state = prev;
410
411         genpd_unlock(genpd);
412
413         return ret;
414 }
415 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
416
417 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
418 {
419         unsigned int state_idx = genpd->state_idx;
420         ktime_t time_start;
421         s64 elapsed_ns;
422         int ret;
423
424         if (!genpd->power_on)
425                 return 0;
426
427         if (!timed)
428                 return genpd->power_on(genpd);
429
430         time_start = ktime_get();
431         ret = genpd->power_on(genpd);
432         if (ret)
433                 return ret;
434
435         elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436         if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
437                 return ret;
438
439         genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
440         genpd->max_off_time_changed = true;
441         pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
442                  genpd->name, "on", elapsed_ns);
443
444         return ret;
445 }
446
447 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
448 {
449         unsigned int state_idx = genpd->state_idx;
450         ktime_t time_start;
451         s64 elapsed_ns;
452         int ret;
453
454         if (!genpd->power_off)
455                 return 0;
456
457         if (!timed)
458                 return genpd->power_off(genpd);
459
460         time_start = ktime_get();
461         ret = genpd->power_off(genpd);
462         if (ret)
463                 return ret;
464
465         elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
466         if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
467                 return 0;
468
469         genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
470         genpd->max_off_time_changed = true;
471         pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
472                  genpd->name, "off", elapsed_ns);
473
474         return 0;
475 }
476
477 /**
478  * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
479  * @genpd: PM domain to power off.
480  *
481  * Queue up the execution of genpd_power_off() unless it's already been done
482  * before.
483  */
484 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
485 {
486         queue_work(pm_wq, &genpd->power_off_work);
487 }
488
489 /**
490  * genpd_power_off - Remove power from a given PM domain.
491  * @genpd: PM domain to power down.
492  * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
493  * RPM status of the releated device is in an intermediate state, not yet turned
494  * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
495  * be RPM_SUSPENDED, while it tries to power off the PM domain.
496  *
497  * If all of the @genpd's devices have been suspended and all of its subdomains
498  * have been powered down, remove power from @genpd.
499  */
500 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
501                            unsigned int depth)
502 {
503         struct pm_domain_data *pdd;
504         struct gpd_link *link;
505         unsigned int not_suspended = 0;
506
507         /*
508          * Do not try to power off the domain in the following situations:
509          * (1) The domain is already in the "power off" state.
510          * (2) System suspend is in progress.
511          */
512         if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
513                 return 0;
514
515         /*
516          * Abort power off for the PM domain in the following situations:
517          * (1) The domain is configured as always on.
518          * (2) When the domain has a subdomain being powered on.
519          */
520         if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
521                 return -EBUSY;
522
523         list_for_each_entry(pdd, &genpd->dev_list, list_node) {
524                 enum pm_qos_flags_status stat;
525
526                 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
527                 if (stat > PM_QOS_FLAGS_NONE)
528                         return -EBUSY;
529
530                 /*
531                  * Do not allow PM domain to be powered off, when an IRQ safe
532                  * device is part of a non-IRQ safe domain.
533                  */
534                 if (!pm_runtime_suspended(pdd->dev) ||
535                         irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
536                         not_suspended++;
537         }
538
539         if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
540                 return -EBUSY;
541
542         if (genpd->gov && genpd->gov->power_down_ok) {
543                 if (!genpd->gov->power_down_ok(&genpd->domain))
544                         return -EAGAIN;
545         }
546
547         /* Default to shallowest state. */
548         if (!genpd->gov)
549                 genpd->state_idx = 0;
550
551         if (genpd->power_off) {
552                 int ret;
553
554                 if (atomic_read(&genpd->sd_count) > 0)
555                         return -EBUSY;
556
557                 /*
558                  * If sd_count > 0 at this point, one of the subdomains hasn't
559                  * managed to call genpd_power_on() for the master yet after
560                  * incrementing it.  In that case genpd_power_on() will wait
561                  * for us to drop the lock, so we can call .power_off() and let
562                  * the genpd_power_on() restore power for us (this shouldn't
563                  * happen very often).
564                  */
565                 ret = _genpd_power_off(genpd, true);
566                 if (ret)
567                         return ret;
568         }
569
570         genpd->status = GPD_STATE_POWER_OFF;
571         genpd_update_accounting(genpd);
572
573         list_for_each_entry(link, &genpd->slave_links, slave_node) {
574                 genpd_sd_counter_dec(link->master);
575                 genpd_lock_nested(link->master, depth + 1);
576                 genpd_power_off(link->master, false, depth + 1);
577                 genpd_unlock(link->master);
578         }
579
580         return 0;
581 }
582
583 /**
584  * genpd_power_on - Restore power to a given PM domain and its masters.
585  * @genpd: PM domain to power up.
586  * @depth: nesting count for lockdep.
587  *
588  * Restore power to @genpd and all of its masters so that it is possible to
589  * resume a device belonging to it.
590  */
591 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
592 {
593         struct gpd_link *link;
594         int ret = 0;
595
596         if (genpd_status_on(genpd))
597                 return 0;
598
599         /*
600          * The list is guaranteed not to change while the loop below is being
601          * executed, unless one of the masters' .power_on() callbacks fiddles
602          * with it.
603          */
604         list_for_each_entry(link, &genpd->slave_links, slave_node) {
605                 struct generic_pm_domain *master = link->master;
606
607                 genpd_sd_counter_inc(master);
608
609                 genpd_lock_nested(master, depth + 1);
610                 ret = genpd_power_on(master, depth + 1);
611                 genpd_unlock(master);
612
613                 if (ret) {
614                         genpd_sd_counter_dec(master);
615                         goto err;
616                 }
617         }
618
619         ret = _genpd_power_on(genpd, true);
620         if (ret)
621                 goto err;
622
623         genpd->status = GPD_STATE_ACTIVE;
624         genpd_update_accounting(genpd);
625
626         return 0;
627
628  err:
629         list_for_each_entry_continue_reverse(link,
630                                         &genpd->slave_links,
631                                         slave_node) {
632                 genpd_sd_counter_dec(link->master);
633                 genpd_lock_nested(link->master, depth + 1);
634                 genpd_power_off(link->master, false, depth + 1);
635                 genpd_unlock(link->master);
636         }
637
638         return ret;
639 }
640
641 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
642                                      unsigned long val, void *ptr)
643 {
644         struct generic_pm_domain_data *gpd_data;
645         struct device *dev;
646
647         gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
648         dev = gpd_data->base.dev;
649
650         for (;;) {
651                 struct generic_pm_domain *genpd;
652                 struct pm_domain_data *pdd;
653
654                 spin_lock_irq(&dev->power.lock);
655
656                 pdd = dev->power.subsys_data ?
657                                 dev->power.subsys_data->domain_data : NULL;
658                 if (pdd) {
659                         to_gpd_data(pdd)->td.constraint_changed = true;
660                         genpd = dev_to_genpd(dev);
661                 } else {
662                         genpd = ERR_PTR(-ENODATA);
663                 }
664
665                 spin_unlock_irq(&dev->power.lock);
666
667                 if (!IS_ERR(genpd)) {
668                         genpd_lock(genpd);
669                         genpd->max_off_time_changed = true;
670                         genpd_unlock(genpd);
671                 }
672
673                 dev = dev->parent;
674                 if (!dev || dev->power.ignore_children)
675                         break;
676         }
677
678         return NOTIFY_DONE;
679 }
680
681 /**
682  * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
683  * @work: Work structure used for scheduling the execution of this function.
684  */
685 static void genpd_power_off_work_fn(struct work_struct *work)
686 {
687         struct generic_pm_domain *genpd;
688
689         genpd = container_of(work, struct generic_pm_domain, power_off_work);
690
691         genpd_lock(genpd);
692         genpd_power_off(genpd, false, 0);
693         genpd_unlock(genpd);
694 }
695
696 /**
697  * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
698  * @dev: Device to handle.
699  */
700 static int __genpd_runtime_suspend(struct device *dev)
701 {
702         int (*cb)(struct device *__dev);
703
704         if (dev->type && dev->type->pm)
705                 cb = dev->type->pm->runtime_suspend;
706         else if (dev->class && dev->class->pm)
707                 cb = dev->class->pm->runtime_suspend;
708         else if (dev->bus && dev->bus->pm)
709                 cb = dev->bus->pm->runtime_suspend;
710         else
711                 cb = NULL;
712
713         if (!cb && dev->driver && dev->driver->pm)
714                 cb = dev->driver->pm->runtime_suspend;
715
716         return cb ? cb(dev) : 0;
717 }
718
719 /**
720  * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
721  * @dev: Device to handle.
722  */
723 static int __genpd_runtime_resume(struct device *dev)
724 {
725         int (*cb)(struct device *__dev);
726
727         if (dev->type && dev->type->pm)
728                 cb = dev->type->pm->runtime_resume;
729         else if (dev->class && dev->class->pm)
730                 cb = dev->class->pm->runtime_resume;
731         else if (dev->bus && dev->bus->pm)
732                 cb = dev->bus->pm->runtime_resume;
733         else
734                 cb = NULL;
735
736         if (!cb && dev->driver && dev->driver->pm)
737                 cb = dev->driver->pm->runtime_resume;
738
739         return cb ? cb(dev) : 0;
740 }
741
742 /**
743  * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
744  * @dev: Device to suspend.
745  *
746  * Carry out a runtime suspend of a device under the assumption that its
747  * pm_domain field points to the domain member of an object of type
748  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
749  */
750 static int genpd_runtime_suspend(struct device *dev)
751 {
752         struct generic_pm_domain *genpd;
753         bool (*suspend_ok)(struct device *__dev);
754         struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
755         bool runtime_pm = pm_runtime_enabled(dev);
756         ktime_t time_start;
757         s64 elapsed_ns;
758         int ret;
759
760         dev_dbg(dev, "%s()\n", __func__);
761
762         genpd = dev_to_genpd(dev);
763         if (IS_ERR(genpd))
764                 return -EINVAL;
765
766         /*
767          * A runtime PM centric subsystem/driver may re-use the runtime PM
768          * callbacks for other purposes than runtime PM. In those scenarios
769          * runtime PM is disabled. Under these circumstances, we shall skip
770          * validating/measuring the PM QoS latency.
771          */
772         suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
773         if (runtime_pm && suspend_ok && !suspend_ok(dev))
774                 return -EBUSY;
775
776         /* Measure suspend latency. */
777         time_start = 0;
778         if (runtime_pm)
779                 time_start = ktime_get();
780
781         ret = __genpd_runtime_suspend(dev);
782         if (ret)
783                 return ret;
784
785         ret = genpd_stop_dev(genpd, dev);
786         if (ret) {
787                 __genpd_runtime_resume(dev);
788                 return ret;
789         }
790
791         /* Update suspend latency value if the measured time exceeds it. */
792         if (runtime_pm) {
793                 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
794                 if (elapsed_ns > td->suspend_latency_ns) {
795                         td->suspend_latency_ns = elapsed_ns;
796                         dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
797                                 elapsed_ns);
798                         genpd->max_off_time_changed = true;
799                         td->constraint_changed = true;
800                 }
801         }
802
803         /*
804          * If power.irq_safe is set, this routine may be run with
805          * IRQs disabled, so suspend only if the PM domain also is irq_safe.
806          */
807         if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
808                 return 0;
809
810         genpd_lock(genpd);
811         genpd_power_off(genpd, true, 0);
812         genpd_unlock(genpd);
813
814         return 0;
815 }
816
817 /**
818  * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
819  * @dev: Device to resume.
820  *
821  * Carry out a runtime resume of a device under the assumption that its
822  * pm_domain field points to the domain member of an object of type
823  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
824  */
825 static int genpd_runtime_resume(struct device *dev)
826 {
827         struct generic_pm_domain *genpd;
828         struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
829         bool runtime_pm = pm_runtime_enabled(dev);
830         ktime_t time_start;
831         s64 elapsed_ns;
832         int ret;
833         bool timed = true;
834
835         dev_dbg(dev, "%s()\n", __func__);
836
837         genpd = dev_to_genpd(dev);
838         if (IS_ERR(genpd))
839                 return -EINVAL;
840
841         /*
842          * As we don't power off a non IRQ safe domain, which holds
843          * an IRQ safe device, we don't need to restore power to it.
844          */
845         if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
846                 timed = false;
847                 goto out;
848         }
849
850         genpd_lock(genpd);
851         ret = genpd_power_on(genpd, 0);
852         genpd_unlock(genpd);
853
854         if (ret)
855                 return ret;
856
857  out:
858         /* Measure resume latency. */
859         time_start = 0;
860         if (timed && runtime_pm)
861                 time_start = ktime_get();
862
863         ret = genpd_start_dev(genpd, dev);
864         if (ret)
865                 goto err_poweroff;
866
867         ret = __genpd_runtime_resume(dev);
868         if (ret)
869                 goto err_stop;
870
871         /* Update resume latency value if the measured time exceeds it. */
872         if (timed && runtime_pm) {
873                 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
874                 if (elapsed_ns > td->resume_latency_ns) {
875                         td->resume_latency_ns = elapsed_ns;
876                         dev_dbg(dev, "resume latency exceeded, %lld ns\n",
877                                 elapsed_ns);
878                         genpd->max_off_time_changed = true;
879                         td->constraint_changed = true;
880                 }
881         }
882
883         return 0;
884
885 err_stop:
886         genpd_stop_dev(genpd, dev);
887 err_poweroff:
888         if (!pm_runtime_is_irq_safe(dev) ||
889                 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
890                 genpd_lock(genpd);
891                 genpd_power_off(genpd, true, 0);
892                 genpd_unlock(genpd);
893         }
894
895         return ret;
896 }
897
898 static bool pd_ignore_unused;
899 static int __init pd_ignore_unused_setup(char *__unused)
900 {
901         pd_ignore_unused = true;
902         return 1;
903 }
904 __setup("pd_ignore_unused", pd_ignore_unused_setup);
905
906 /**
907  * genpd_power_off_unused - Power off all PM domains with no devices in use.
908  */
909 static int __init genpd_power_off_unused(void)
910 {
911         struct generic_pm_domain *genpd;
912
913         if (pd_ignore_unused) {
914                 pr_warn("genpd: Not disabling unused power domains\n");
915                 return 0;
916         }
917
918         mutex_lock(&gpd_list_lock);
919
920         list_for_each_entry(genpd, &gpd_list, gpd_list_node)
921                 genpd_queue_power_off_work(genpd);
922
923         mutex_unlock(&gpd_list_lock);
924
925         return 0;
926 }
927 late_initcall(genpd_power_off_unused);
928
929 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
930
931 static bool genpd_present(const struct generic_pm_domain *genpd)
932 {
933         const struct generic_pm_domain *gpd;
934
935         if (IS_ERR_OR_NULL(genpd))
936                 return false;
937
938         list_for_each_entry(gpd, &gpd_list, gpd_list_node)
939                 if (gpd == genpd)
940                         return true;
941
942         return false;
943 }
944
945 #endif
946
947 #ifdef CONFIG_PM_SLEEP
948
949 /**
950  * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
951  * @genpd: PM domain to power off, if possible.
952  * @use_lock: use the lock.
953  * @depth: nesting count for lockdep.
954  *
955  * Check if the given PM domain can be powered off (during system suspend or
956  * hibernation) and do that if so.  Also, in that case propagate to its masters.
957  *
958  * This function is only called in "noirq" and "syscore" stages of system power
959  * transitions. The "noirq" callbacks may be executed asynchronously, thus in
960  * these cases the lock must be held.
961  */
962 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
963                                  unsigned int depth)
964 {
965         struct gpd_link *link;
966
967         if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
968                 return;
969
970         if (genpd->suspended_count != genpd->device_count
971             || atomic_read(&genpd->sd_count) > 0)
972                 return;
973
974         /* Choose the deepest state when suspending */
975         genpd->state_idx = genpd->state_count - 1;
976         if (_genpd_power_off(genpd, false))
977                 return;
978
979         genpd->status = GPD_STATE_POWER_OFF;
980
981         list_for_each_entry(link, &genpd->slave_links, slave_node) {
982                 genpd_sd_counter_dec(link->master);
983
984                 if (use_lock)
985                         genpd_lock_nested(link->master, depth + 1);
986
987                 genpd_sync_power_off(link->master, use_lock, depth + 1);
988
989                 if (use_lock)
990                         genpd_unlock(link->master);
991         }
992 }
993
994 /**
995  * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
996  * @genpd: PM domain to power on.
997  * @use_lock: use the lock.
998  * @depth: nesting count for lockdep.
999  *
1000  * This function is only called in "noirq" and "syscore" stages of system power
1001  * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1002  * these cases the lock must be held.
1003  */
1004 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1005                                 unsigned int depth)
1006 {
1007         struct gpd_link *link;
1008
1009         if (genpd_status_on(genpd))
1010                 return;
1011
1012         list_for_each_entry(link, &genpd->slave_links, slave_node) {
1013                 genpd_sd_counter_inc(link->master);
1014
1015                 if (use_lock)
1016                         genpd_lock_nested(link->master, depth + 1);
1017
1018                 genpd_sync_power_on(link->master, use_lock, depth + 1);
1019
1020                 if (use_lock)
1021                         genpd_unlock(link->master);
1022         }
1023
1024         _genpd_power_on(genpd, false);
1025
1026         genpd->status = GPD_STATE_ACTIVE;
1027 }
1028
1029 /**
1030  * resume_needed - Check whether to resume a device before system suspend.
1031  * @dev: Device to check.
1032  * @genpd: PM domain the device belongs to.
1033  *
1034  * There are two cases in which a device that can wake up the system from sleep
1035  * states should be resumed by genpd_prepare(): (1) if the device is enabled
1036  * to wake up the system and it has to remain active for this purpose while the
1037  * system is in the sleep state and (2) if the device is not enabled to wake up
1038  * the system from sleep states and it generally doesn't generate wakeup signals
1039  * by itself (those signals are generated on its behalf by other parts of the
1040  * system).  In the latter case it may be necessary to reconfigure the device's
1041  * wakeup settings during system suspend, because it may have been set up to
1042  * signal remote wakeup from the system's working state as needed by runtime PM.
1043  * Return 'true' in either of the above cases.
1044  */
1045 static bool resume_needed(struct device *dev,
1046                           const struct generic_pm_domain *genpd)
1047 {
1048         bool active_wakeup;
1049
1050         if (!device_can_wakeup(dev))
1051                 return false;
1052
1053         active_wakeup = genpd_is_active_wakeup(genpd);
1054         return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
1055 }
1056
1057 /**
1058  * genpd_prepare - Start power transition of a device in a PM domain.
1059  * @dev: Device to start the transition of.
1060  *
1061  * Start a power transition of a device (during a system-wide power transition)
1062  * under the assumption that its pm_domain field points to the domain member of
1063  * an object of type struct generic_pm_domain representing a PM domain
1064  * consisting of I/O devices.
1065  */
1066 static int genpd_prepare(struct device *dev)
1067 {
1068         struct generic_pm_domain *genpd;
1069         int ret;
1070
1071         dev_dbg(dev, "%s()\n", __func__);
1072
1073         genpd = dev_to_genpd(dev);
1074         if (IS_ERR(genpd))
1075                 return -EINVAL;
1076
1077         /*
1078          * If a wakeup request is pending for the device, it should be woken up
1079          * at this point and a system wakeup event should be reported if it's
1080          * set up to wake up the system from sleep states.
1081          */
1082         if (resume_needed(dev, genpd))
1083                 pm_runtime_resume(dev);
1084
1085         genpd_lock(genpd);
1086
1087         if (genpd->prepared_count++ == 0)
1088                 genpd->suspended_count = 0;
1089
1090         genpd_unlock(genpd);
1091
1092         ret = pm_generic_prepare(dev);
1093         if (ret < 0) {
1094                 genpd_lock(genpd);
1095
1096                 genpd->prepared_count--;
1097
1098                 genpd_unlock(genpd);
1099         }
1100
1101         /* Never return 1, as genpd don't cope with the direct_complete path. */
1102         return ret >= 0 ? 0 : ret;
1103 }
1104
1105 /**
1106  * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1107  *   I/O pm domain.
1108  * @dev: Device to suspend.
1109  * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1110  *
1111  * Stop the device and remove power from the domain if all devices in it have
1112  * been stopped.
1113  */
1114 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1115 {
1116         struct generic_pm_domain *genpd;
1117         int ret = 0;
1118
1119         genpd = dev_to_genpd(dev);
1120         if (IS_ERR(genpd))
1121                 return -EINVAL;
1122
1123         if (poweroff)
1124                 ret = pm_generic_poweroff_noirq(dev);
1125         else
1126                 ret = pm_generic_suspend_noirq(dev);
1127         if (ret)
1128                 return ret;
1129
1130         if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1131                 return 0;
1132
1133         if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1134             !pm_runtime_status_suspended(dev)) {
1135                 ret = genpd_stop_dev(genpd, dev);
1136                 if (ret) {
1137                         if (poweroff)
1138                                 pm_generic_restore_noirq(dev);
1139                         else
1140                                 pm_generic_resume_noirq(dev);
1141                         return ret;
1142                 }
1143         }
1144
1145         genpd_lock(genpd);
1146         genpd->suspended_count++;
1147         genpd_sync_power_off(genpd, true, 0);
1148         genpd_unlock(genpd);
1149
1150         return 0;
1151 }
1152
1153 /**
1154  * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1155  * @dev: Device to suspend.
1156  *
1157  * Stop the device and remove power from the domain if all devices in it have
1158  * been stopped.
1159  */
1160 static int genpd_suspend_noirq(struct device *dev)
1161 {
1162         dev_dbg(dev, "%s()\n", __func__);
1163
1164         return genpd_finish_suspend(dev, false);
1165 }
1166
1167 /**
1168  * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1169  * @dev: Device to resume.
1170  *
1171  * Restore power to the device's PM domain, if necessary, and start the device.
1172  */
1173 static int genpd_resume_noirq(struct device *dev)
1174 {
1175         struct generic_pm_domain *genpd;
1176         int ret;
1177
1178         dev_dbg(dev, "%s()\n", __func__);
1179
1180         genpd = dev_to_genpd(dev);
1181         if (IS_ERR(genpd))
1182                 return -EINVAL;
1183
1184         if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1185                 return pm_generic_resume_noirq(dev);
1186
1187         genpd_lock(genpd);
1188         genpd_sync_power_on(genpd, true, 0);
1189         genpd->suspended_count--;
1190         genpd_unlock(genpd);
1191
1192         if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1193             !pm_runtime_status_suspended(dev)) {
1194                 ret = genpd_start_dev(genpd, dev);
1195                 if (ret)
1196                         return ret;
1197         }
1198
1199         return pm_generic_resume_noirq(dev);
1200 }
1201
1202 /**
1203  * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1204  * @dev: Device to freeze.
1205  *
1206  * Carry out a late freeze of a device under the assumption that its
1207  * pm_domain field points to the domain member of an object of type
1208  * struct generic_pm_domain representing a power domain consisting of I/O
1209  * devices.
1210  */
1211 static int genpd_freeze_noirq(struct device *dev)
1212 {
1213         const struct generic_pm_domain *genpd;
1214         int ret = 0;
1215
1216         dev_dbg(dev, "%s()\n", __func__);
1217
1218         genpd = dev_to_genpd(dev);
1219         if (IS_ERR(genpd))
1220                 return -EINVAL;
1221
1222         ret = pm_generic_freeze_noirq(dev);
1223         if (ret)
1224                 return ret;
1225
1226         if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1227             !pm_runtime_status_suspended(dev))
1228                 ret = genpd_stop_dev(genpd, dev);
1229
1230         return ret;
1231 }
1232
1233 /**
1234  * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1235  * @dev: Device to thaw.
1236  *
1237  * Start the device, unless power has been removed from the domain already
1238  * before the system transition.
1239  */
1240 static int genpd_thaw_noirq(struct device *dev)
1241 {
1242         const struct generic_pm_domain *genpd;
1243         int ret = 0;
1244
1245         dev_dbg(dev, "%s()\n", __func__);
1246
1247         genpd = dev_to_genpd(dev);
1248         if (IS_ERR(genpd))
1249                 return -EINVAL;
1250
1251         if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1252             !pm_runtime_status_suspended(dev)) {
1253                 ret = genpd_start_dev(genpd, dev);
1254                 if (ret)
1255                         return ret;
1256         }
1257
1258         return pm_generic_thaw_noirq(dev);
1259 }
1260
1261 /**
1262  * genpd_poweroff_noirq - Completion of hibernation of device in an
1263  *   I/O PM domain.
1264  * @dev: Device to poweroff.
1265  *
1266  * Stop the device and remove power from the domain if all devices in it have
1267  * been stopped.
1268  */
1269 static int genpd_poweroff_noirq(struct device *dev)
1270 {
1271         dev_dbg(dev, "%s()\n", __func__);
1272
1273         return genpd_finish_suspend(dev, true);
1274 }
1275
1276 /**
1277  * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1278  * @dev: Device to resume.
1279  *
1280  * Make sure the domain will be in the same power state as before the
1281  * hibernation the system is resuming from and start the device if necessary.
1282  */
1283 static int genpd_restore_noirq(struct device *dev)
1284 {
1285         struct generic_pm_domain *genpd;
1286         int ret = 0;
1287
1288         dev_dbg(dev, "%s()\n", __func__);
1289
1290         genpd = dev_to_genpd(dev);
1291         if (IS_ERR(genpd))
1292                 return -EINVAL;
1293
1294         /*
1295          * At this point suspended_count == 0 means we are being run for the
1296          * first time for the given domain in the present cycle.
1297          */
1298         genpd_lock(genpd);
1299         if (genpd->suspended_count++ == 0)
1300                 /*
1301                  * The boot kernel might put the domain into arbitrary state,
1302                  * so make it appear as powered off to genpd_sync_power_on(),
1303                  * so that it tries to power it on in case it was really off.
1304                  */
1305                 genpd->status = GPD_STATE_POWER_OFF;
1306
1307         genpd_sync_power_on(genpd, true, 0);
1308         genpd_unlock(genpd);
1309
1310         if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1311             !pm_runtime_status_suspended(dev)) {
1312                 ret = genpd_start_dev(genpd, dev);
1313                 if (ret)
1314                         return ret;
1315         }
1316
1317         return pm_generic_restore_noirq(dev);
1318 }
1319
1320 /**
1321  * genpd_complete - Complete power transition of a device in a power domain.
1322  * @dev: Device to complete the transition of.
1323  *
1324  * Complete a power transition of a device (during a system-wide power
1325  * transition) under the assumption that its pm_domain field points to the
1326  * domain member of an object of type struct generic_pm_domain representing
1327  * a power domain consisting of I/O devices.
1328  */
1329 static void genpd_complete(struct device *dev)
1330 {
1331         struct generic_pm_domain *genpd;
1332
1333         dev_dbg(dev, "%s()\n", __func__);
1334
1335         genpd = dev_to_genpd(dev);
1336         if (IS_ERR(genpd))
1337                 return;
1338
1339         pm_generic_complete(dev);
1340
1341         genpd_lock(genpd);
1342
1343         genpd->prepared_count--;
1344         if (!genpd->prepared_count)
1345                 genpd_queue_power_off_work(genpd);
1346
1347         genpd_unlock(genpd);
1348 }
1349
1350 /**
1351  * genpd_syscore_switch - Switch power during system core suspend or resume.
1352  * @dev: Device that normally is marked as "always on" to switch power for.
1353  *
1354  * This routine may only be called during the system core (syscore) suspend or
1355  * resume phase for devices whose "always on" flags are set.
1356  */
1357 static void genpd_syscore_switch(struct device *dev, bool suspend)
1358 {
1359         struct generic_pm_domain *genpd;
1360
1361         genpd = dev_to_genpd(dev);
1362         if (!genpd_present(genpd))
1363                 return;
1364
1365         if (suspend) {
1366                 genpd->suspended_count++;
1367                 genpd_sync_power_off(genpd, false, 0);
1368         } else {
1369                 genpd_sync_power_on(genpd, false, 0);
1370                 genpd->suspended_count--;
1371         }
1372 }
1373
1374 void pm_genpd_syscore_poweroff(struct device *dev)
1375 {
1376         genpd_syscore_switch(dev, true);
1377 }
1378 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1379
1380 void pm_genpd_syscore_poweron(struct device *dev)
1381 {
1382         genpd_syscore_switch(dev, false);
1383 }
1384 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1385
1386 #else /* !CONFIG_PM_SLEEP */
1387
1388 #define genpd_prepare           NULL
1389 #define genpd_suspend_noirq     NULL
1390 #define genpd_resume_noirq      NULL
1391 #define genpd_freeze_noirq      NULL
1392 #define genpd_thaw_noirq        NULL
1393 #define genpd_poweroff_noirq    NULL
1394 #define genpd_restore_noirq     NULL
1395 #define genpd_complete          NULL
1396
1397 #endif /* CONFIG_PM_SLEEP */
1398
1399 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1400                                         struct gpd_timing_data *td)
1401 {
1402         struct generic_pm_domain_data *gpd_data;
1403         int ret;
1404
1405         ret = dev_pm_get_subsys_data(dev);
1406         if (ret)
1407                 return ERR_PTR(ret);
1408
1409         gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1410         if (!gpd_data) {
1411                 ret = -ENOMEM;
1412                 goto err_put;
1413         }
1414
1415         if (td)
1416                 gpd_data->td = *td;
1417
1418         gpd_data->base.dev = dev;
1419         gpd_data->td.constraint_changed = true;
1420         gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1421         gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1422
1423         spin_lock_irq(&dev->power.lock);
1424
1425         if (dev->power.subsys_data->domain_data) {
1426                 ret = -EINVAL;
1427                 goto err_free;
1428         }
1429
1430         dev->power.subsys_data->domain_data = &gpd_data->base;
1431
1432         spin_unlock_irq(&dev->power.lock);
1433
1434         return gpd_data;
1435
1436  err_free:
1437         spin_unlock_irq(&dev->power.lock);
1438         kfree(gpd_data);
1439  err_put:
1440         dev_pm_put_subsys_data(dev);
1441         return ERR_PTR(ret);
1442 }
1443
1444 static void genpd_free_dev_data(struct device *dev,
1445                                 struct generic_pm_domain_data *gpd_data)
1446 {
1447         spin_lock_irq(&dev->power.lock);
1448
1449         dev->power.subsys_data->domain_data = NULL;
1450
1451         spin_unlock_irq(&dev->power.lock);
1452
1453         kfree(gpd_data);
1454         dev_pm_put_subsys_data(dev);
1455 }
1456
1457 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1458                             struct gpd_timing_data *td)
1459 {
1460         struct generic_pm_domain_data *gpd_data;
1461         int ret;
1462
1463         dev_dbg(dev, "%s()\n", __func__);
1464
1465         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1466                 return -EINVAL;
1467
1468         gpd_data = genpd_alloc_dev_data(dev, td);
1469         if (IS_ERR(gpd_data))
1470                 return PTR_ERR(gpd_data);
1471
1472         genpd_lock(genpd);
1473
1474         ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1475         if (ret)
1476                 goto out;
1477
1478         dev_pm_domain_set(dev, &genpd->domain);
1479
1480         genpd->device_count++;
1481         genpd->max_off_time_changed = true;
1482
1483         list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1484
1485  out:
1486         genpd_unlock(genpd);
1487
1488         if (ret)
1489                 genpd_free_dev_data(dev, gpd_data);
1490         else
1491                 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1492
1493         return ret;
1494 }
1495
1496 /**
1497  * pm_genpd_add_device - Add a device to an I/O PM domain.
1498  * @genpd: PM domain to add the device to.
1499  * @dev: Device to be added.
1500  */
1501 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1502 {
1503         int ret;
1504
1505         mutex_lock(&gpd_list_lock);
1506         ret = genpd_add_device(genpd, dev, NULL);
1507         mutex_unlock(&gpd_list_lock);
1508
1509         return ret;
1510 }
1511 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1512
1513 static int genpd_remove_device(struct generic_pm_domain *genpd,
1514                                struct device *dev)
1515 {
1516         struct generic_pm_domain_data *gpd_data;
1517         struct pm_domain_data *pdd;
1518         int ret = 0;
1519
1520         dev_dbg(dev, "%s()\n", __func__);
1521
1522         pdd = dev->power.subsys_data->domain_data;
1523         gpd_data = to_gpd_data(pdd);
1524         dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1525
1526         genpd_lock(genpd);
1527
1528         if (genpd->prepared_count > 0) {
1529                 ret = -EAGAIN;
1530                 goto out;
1531         }
1532
1533         genpd->device_count--;
1534         genpd->max_off_time_changed = true;
1535
1536         if (genpd->detach_dev)
1537                 genpd->detach_dev(genpd, dev);
1538
1539         dev_pm_domain_set(dev, NULL);
1540
1541         list_del_init(&pdd->list_node);
1542
1543         genpd_unlock(genpd);
1544
1545         genpd_free_dev_data(dev, gpd_data);
1546
1547         return 0;
1548
1549  out:
1550         genpd_unlock(genpd);
1551         dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1552
1553         return ret;
1554 }
1555
1556 /**
1557  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1558  * @dev: Device to be removed.
1559  */
1560 int pm_genpd_remove_device(struct device *dev)
1561 {
1562         struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1563
1564         if (!genpd)
1565                 return -EINVAL;
1566
1567         return genpd_remove_device(genpd, dev);
1568 }
1569 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1570
1571 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1572                                struct generic_pm_domain *subdomain)
1573 {
1574         struct gpd_link *link, *itr;
1575         int ret = 0;
1576
1577         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1578             || genpd == subdomain)
1579                 return -EINVAL;
1580
1581         /*
1582          * If the domain can be powered on/off in an IRQ safe
1583          * context, ensure that the subdomain can also be
1584          * powered on/off in that context.
1585          */
1586         if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1587                 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1588                                 genpd->name, subdomain->name);
1589                 return -EINVAL;
1590         }
1591
1592         link = kzalloc(sizeof(*link), GFP_KERNEL);
1593         if (!link)
1594                 return -ENOMEM;
1595
1596         genpd_lock(subdomain);
1597         genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1598
1599         if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1600                 ret = -EINVAL;
1601                 goto out;
1602         }
1603
1604         list_for_each_entry(itr, &genpd->master_links, master_node) {
1605                 if (itr->slave == subdomain && itr->master == genpd) {
1606                         ret = -EINVAL;
1607                         goto out;
1608                 }
1609         }
1610
1611         link->master = genpd;
1612         list_add_tail(&link->master_node, &genpd->master_links);
1613         link->slave = subdomain;
1614         list_add_tail(&link->slave_node, &subdomain->slave_links);
1615         if (genpd_status_on(subdomain))
1616                 genpd_sd_counter_inc(genpd);
1617
1618  out:
1619         genpd_unlock(genpd);
1620         genpd_unlock(subdomain);
1621         if (ret)
1622                 kfree(link);
1623         return ret;
1624 }
1625
1626 /**
1627  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1628  * @genpd: Master PM domain to add the subdomain to.
1629  * @subdomain: Subdomain to be added.
1630  */
1631 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1632                            struct generic_pm_domain *subdomain)
1633 {
1634         int ret;
1635
1636         mutex_lock(&gpd_list_lock);
1637         ret = genpd_add_subdomain(genpd, subdomain);
1638         mutex_unlock(&gpd_list_lock);
1639
1640         return ret;
1641 }
1642 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1643
1644 /**
1645  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1646  * @genpd: Master PM domain to remove the subdomain from.
1647  * @subdomain: Subdomain to be removed.
1648  */
1649 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1650                               struct generic_pm_domain *subdomain)
1651 {
1652         struct gpd_link *l, *link;
1653         int ret = -EINVAL;
1654
1655         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1656                 return -EINVAL;
1657
1658         genpd_lock(subdomain);
1659         genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1660
1661         if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1662                 pr_warn("%s: unable to remove subdomain %s\n",
1663                         genpd->name, subdomain->name);
1664                 ret = -EBUSY;
1665                 goto out;
1666         }
1667
1668         list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1669                 if (link->slave != subdomain)
1670                         continue;
1671
1672                 list_del(&link->master_node);
1673                 list_del(&link->slave_node);
1674                 kfree(link);
1675                 if (genpd_status_on(subdomain))
1676                         genpd_sd_counter_dec(genpd);
1677
1678                 ret = 0;
1679                 break;
1680         }
1681
1682 out:
1683         genpd_unlock(genpd);
1684         genpd_unlock(subdomain);
1685
1686         return ret;
1687 }
1688 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1689
1690 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1691 {
1692         struct genpd_power_state *state;
1693
1694         state = kzalloc(sizeof(*state), GFP_KERNEL);
1695         if (!state)
1696                 return -ENOMEM;
1697
1698         genpd->states = state;
1699         genpd->state_count = 1;
1700         genpd->free = state;
1701
1702         return 0;
1703 }
1704
1705 static void genpd_lock_init(struct generic_pm_domain *genpd)
1706 {
1707         if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1708                 spin_lock_init(&genpd->slock);
1709                 genpd->lock_ops = &genpd_spin_ops;
1710         } else {
1711                 mutex_init(&genpd->mlock);
1712                 genpd->lock_ops = &genpd_mtx_ops;
1713         }
1714 }
1715
1716 /**
1717  * pm_genpd_init - Initialize a generic I/O PM domain object.
1718  * @genpd: PM domain object to initialize.
1719  * @gov: PM domain governor to associate with the domain (may be NULL).
1720  * @is_off: Initial value of the domain's power_is_off field.
1721  *
1722  * Returns 0 on successful initialization, else a negative error code.
1723  */
1724 int pm_genpd_init(struct generic_pm_domain *genpd,
1725                   struct dev_power_governor *gov, bool is_off)
1726 {
1727         int ret;
1728
1729         if (IS_ERR_OR_NULL(genpd))
1730                 return -EINVAL;
1731
1732         INIT_LIST_HEAD(&genpd->master_links);
1733         INIT_LIST_HEAD(&genpd->slave_links);
1734         INIT_LIST_HEAD(&genpd->dev_list);
1735         genpd_lock_init(genpd);
1736         genpd->gov = gov;
1737         INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1738         atomic_set(&genpd->sd_count, 0);
1739         genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1740         genpd->device_count = 0;
1741         genpd->max_off_time_ns = -1;
1742         genpd->max_off_time_changed = true;
1743         genpd->provider = NULL;
1744         genpd->has_provider = false;
1745         genpd->accounting_time = ktime_get();
1746         genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1747         genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1748         genpd->domain.ops.prepare = genpd_prepare;
1749         genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1750         genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1751         genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1752         genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1753         genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1754         genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1755         genpd->domain.ops.complete = genpd_complete;
1756
1757         if (genpd->flags & GENPD_FLAG_PM_CLK) {
1758                 genpd->dev_ops.stop = pm_clk_suspend;
1759                 genpd->dev_ops.start = pm_clk_resume;
1760         }
1761
1762         /* Always-on domains must be powered on at initialization. */
1763         if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1764                 return -EINVAL;
1765
1766         /* Use only one "off" state if there were no states declared */
1767         if (genpd->state_count == 0) {
1768                 ret = genpd_set_default_power_state(genpd);
1769                 if (ret)
1770                         return ret;
1771         } else if (!gov && genpd->state_count > 1) {
1772                 pr_warn("%s: no governor for states\n", genpd->name);
1773         }
1774
1775         device_initialize(&genpd->dev);
1776         dev_set_name(&genpd->dev, "%s", genpd->name);
1777
1778         mutex_lock(&gpd_list_lock);
1779         list_add(&genpd->gpd_list_node, &gpd_list);
1780         mutex_unlock(&gpd_list_lock);
1781
1782         return 0;
1783 }
1784 EXPORT_SYMBOL_GPL(pm_genpd_init);
1785
1786 static int genpd_remove(struct generic_pm_domain *genpd)
1787 {
1788         struct gpd_link *l, *link;
1789
1790         if (IS_ERR_OR_NULL(genpd))
1791                 return -EINVAL;
1792
1793         genpd_lock(genpd);
1794
1795         if (genpd->has_provider) {
1796                 genpd_unlock(genpd);
1797                 pr_err("Provider present, unable to remove %s\n", genpd->name);
1798                 return -EBUSY;
1799         }
1800
1801         if (!list_empty(&genpd->master_links) || genpd->device_count) {
1802                 genpd_unlock(genpd);
1803                 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1804                 return -EBUSY;
1805         }
1806
1807         list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1808                 list_del(&link->master_node);
1809                 list_del(&link->slave_node);
1810                 kfree(link);
1811         }
1812
1813         list_del(&genpd->gpd_list_node);
1814         genpd_unlock(genpd);
1815         cancel_work_sync(&genpd->power_off_work);
1816         kfree(genpd->free);
1817         pr_debug("%s: removed %s\n", __func__, genpd->name);
1818
1819         return 0;
1820 }
1821
1822 /**
1823  * pm_genpd_remove - Remove a generic I/O PM domain
1824  * @genpd: Pointer to PM domain that is to be removed.
1825  *
1826  * To remove the PM domain, this function:
1827  *  - Removes the PM domain as a subdomain to any parent domains,
1828  *    if it was added.
1829  *  - Removes the PM domain from the list of registered PM domains.
1830  *
1831  * The PM domain will only be removed, if the associated provider has
1832  * been removed, it is not a parent to any other PM domain and has no
1833  * devices associated with it.
1834  */
1835 int pm_genpd_remove(struct generic_pm_domain *genpd)
1836 {
1837         int ret;
1838
1839         mutex_lock(&gpd_list_lock);
1840         ret = genpd_remove(genpd);
1841         mutex_unlock(&gpd_list_lock);
1842
1843         return ret;
1844 }
1845 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1846
1847 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1848
1849 /*
1850  * Device Tree based PM domain providers.
1851  *
1852  * The code below implements generic device tree based PM domain providers that
1853  * bind device tree nodes with generic PM domains registered in the system.
1854  *
1855  * Any driver that registers generic PM domains and needs to support binding of
1856  * devices to these domains is supposed to register a PM domain provider, which
1857  * maps a PM domain specifier retrieved from the device tree to a PM domain.
1858  *
1859  * Two simple mapping functions have been provided for convenience:
1860  *  - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1861  *  - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1862  *    index.
1863  */
1864
1865 /**
1866  * struct of_genpd_provider - PM domain provider registration structure
1867  * @link: Entry in global list of PM domain providers
1868  * @node: Pointer to device tree node of PM domain provider
1869  * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1870  *         into a PM domain.
1871  * @data: context pointer to be passed into @xlate callback
1872  */
1873 struct of_genpd_provider {
1874         struct list_head link;
1875         struct device_node *node;
1876         genpd_xlate_t xlate;
1877         void *data;
1878 };
1879
1880 /* List of registered PM domain providers. */
1881 static LIST_HEAD(of_genpd_providers);
1882 /* Mutex to protect the list above. */
1883 static DEFINE_MUTEX(of_genpd_mutex);
1884
1885 /**
1886  * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1887  * @genpdspec: OF phandle args to map into a PM domain
1888  * @data: xlate function private data - pointer to struct generic_pm_domain
1889  *
1890  * This is a generic xlate function that can be used to model PM domains that
1891  * have their own device tree nodes. The private data of xlate function needs
1892  * to be a valid pointer to struct generic_pm_domain.
1893  */
1894 static struct generic_pm_domain *genpd_xlate_simple(
1895                                         struct of_phandle_args *genpdspec,
1896                                         void *data)
1897 {
1898         return data;
1899 }
1900
1901 /**
1902  * genpd_xlate_onecell() - Xlate function using a single index.
1903  * @genpdspec: OF phandle args to map into a PM domain
1904  * @data: xlate function private data - pointer to struct genpd_onecell_data
1905  *
1906  * This is a generic xlate function that can be used to model simple PM domain
1907  * controllers that have one device tree node and provide multiple PM domains.
1908  * A single cell is used as an index into an array of PM domains specified in
1909  * the genpd_onecell_data struct when registering the provider.
1910  */
1911 static struct generic_pm_domain *genpd_xlate_onecell(
1912                                         struct of_phandle_args *genpdspec,
1913                                         void *data)
1914 {
1915         struct genpd_onecell_data *genpd_data = data;
1916         unsigned int idx = genpdspec->args[0];
1917
1918         if (genpdspec->args_count != 1)
1919                 return ERR_PTR(-EINVAL);
1920
1921         if (idx >= genpd_data->num_domains) {
1922                 pr_err("%s: invalid domain index %u\n", __func__, idx);
1923                 return ERR_PTR(-EINVAL);
1924         }
1925
1926         if (!genpd_data->domains[idx])
1927                 return ERR_PTR(-ENOENT);
1928
1929         return genpd_data->domains[idx];
1930 }
1931
1932 /**
1933  * genpd_add_provider() - Register a PM domain provider for a node
1934  * @np: Device node pointer associated with the PM domain provider.
1935  * @xlate: Callback for decoding PM domain from phandle arguments.
1936  * @data: Context pointer for @xlate callback.
1937  */
1938 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1939                               void *data)
1940 {
1941         struct of_genpd_provider *cp;
1942
1943         cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1944         if (!cp)
1945                 return -ENOMEM;
1946
1947         cp->node = of_node_get(np);
1948         cp->data = data;
1949         cp->xlate = xlate;
1950
1951         mutex_lock(&of_genpd_mutex);
1952         list_add(&cp->link, &of_genpd_providers);
1953         mutex_unlock(&of_genpd_mutex);
1954         pr_debug("Added domain provider from %pOF\n", np);
1955
1956         return 0;
1957 }
1958
1959 /**
1960  * of_genpd_add_provider_simple() - Register a simple PM domain provider
1961  * @np: Device node pointer associated with the PM domain provider.
1962  * @genpd: Pointer to PM domain associated with the PM domain provider.
1963  */
1964 int of_genpd_add_provider_simple(struct device_node *np,
1965                                  struct generic_pm_domain *genpd)
1966 {
1967         int ret = -EINVAL;
1968
1969         if (!np || !genpd)
1970                 return -EINVAL;
1971
1972         mutex_lock(&gpd_list_lock);
1973
1974         if (!genpd_present(genpd))
1975                 goto unlock;
1976
1977         genpd->dev.of_node = np;
1978
1979         /* Parse genpd OPP table */
1980         if (genpd->set_performance_state) {
1981                 ret = dev_pm_opp_of_add_table(&genpd->dev);
1982                 if (ret) {
1983                         dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
1984                                 ret);
1985                         goto unlock;
1986                 }
1987
1988                 /*
1989                  * Save table for faster processing while setting performance
1990                  * state.
1991                  */
1992                 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
1993                 WARN_ON(!genpd->opp_table);
1994         }
1995
1996         ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1997         if (ret) {
1998                 if (genpd->set_performance_state) {
1999                         dev_pm_opp_put_opp_table(genpd->opp_table);
2000                         dev_pm_opp_of_remove_table(&genpd->dev);
2001                 }
2002
2003                 goto unlock;
2004         }
2005
2006         genpd->provider = &np->fwnode;
2007         genpd->has_provider = true;
2008
2009 unlock:
2010         mutex_unlock(&gpd_list_lock);
2011
2012         return ret;
2013 }
2014 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2015
2016 /**
2017  * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2018  * @np: Device node pointer associated with the PM domain provider.
2019  * @data: Pointer to the data associated with the PM domain provider.
2020  */
2021 int of_genpd_add_provider_onecell(struct device_node *np,
2022                                   struct genpd_onecell_data *data)
2023 {
2024         struct generic_pm_domain *genpd;
2025         unsigned int i;
2026         int ret = -EINVAL;
2027
2028         if (!np || !data)
2029                 return -EINVAL;
2030
2031         mutex_lock(&gpd_list_lock);
2032
2033         if (!data->xlate)
2034                 data->xlate = genpd_xlate_onecell;
2035
2036         for (i = 0; i < data->num_domains; i++) {
2037                 genpd = data->domains[i];
2038
2039                 if (!genpd)
2040                         continue;
2041                 if (!genpd_present(genpd))
2042                         goto error;
2043
2044                 genpd->dev.of_node = np;
2045
2046                 /* Parse genpd OPP table */
2047                 if (genpd->set_performance_state) {
2048                         ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2049                         if (ret) {
2050                                 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
2051                                         i, ret);
2052                                 goto error;
2053                         }
2054
2055                         /*
2056                          * Save table for faster processing while setting
2057                          * performance state.
2058                          */
2059                         genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
2060                         WARN_ON(!genpd->opp_table);
2061                 }
2062
2063                 genpd->provider = &np->fwnode;
2064                 genpd->has_provider = true;
2065         }
2066
2067         ret = genpd_add_provider(np, data->xlate, data);
2068         if (ret < 0)
2069                 goto error;
2070
2071         mutex_unlock(&gpd_list_lock);
2072
2073         return 0;
2074
2075 error:
2076         while (i--) {
2077                 genpd = data->domains[i];
2078
2079                 if (!genpd)
2080                         continue;
2081
2082                 genpd->provider = NULL;
2083                 genpd->has_provider = false;
2084
2085                 if (genpd->set_performance_state) {
2086                         dev_pm_opp_put_opp_table(genpd->opp_table);
2087                         dev_pm_opp_of_remove_table(&genpd->dev);
2088                 }
2089         }
2090
2091         mutex_unlock(&gpd_list_lock);
2092
2093         return ret;
2094 }
2095 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2096
2097 /**
2098  * of_genpd_del_provider() - Remove a previously registered PM domain provider
2099  * @np: Device node pointer associated with the PM domain provider
2100  */
2101 void of_genpd_del_provider(struct device_node *np)
2102 {
2103         struct of_genpd_provider *cp, *tmp;
2104         struct generic_pm_domain *gpd;
2105
2106         mutex_lock(&gpd_list_lock);
2107         mutex_lock(&of_genpd_mutex);
2108         list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2109                 if (cp->node == np) {
2110                         /*
2111                          * For each PM domain associated with the
2112                          * provider, set the 'has_provider' to false
2113                          * so that the PM domain can be safely removed.
2114                          */
2115                         list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2116                                 if (gpd->provider == &np->fwnode) {
2117                                         gpd->has_provider = false;
2118
2119                                         if (!gpd->set_performance_state)
2120                                                 continue;
2121
2122                                         dev_pm_opp_put_opp_table(gpd->opp_table);
2123                                         dev_pm_opp_of_remove_table(&gpd->dev);
2124                                 }
2125                         }
2126
2127                         list_del(&cp->link);
2128                         of_node_put(cp->node);
2129                         kfree(cp);
2130                         break;
2131                 }
2132         }
2133         mutex_unlock(&of_genpd_mutex);
2134         mutex_unlock(&gpd_list_lock);
2135 }
2136 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2137
2138 /**
2139  * genpd_get_from_provider() - Look-up PM domain
2140  * @genpdspec: OF phandle args to use for look-up
2141  *
2142  * Looks for a PM domain provider under the node specified by @genpdspec and if
2143  * found, uses xlate function of the provider to map phandle args to a PM
2144  * domain.
2145  *
2146  * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2147  * on failure.
2148  */
2149 static struct generic_pm_domain *genpd_get_from_provider(
2150                                         struct of_phandle_args *genpdspec)
2151 {
2152         struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2153         struct of_genpd_provider *provider;
2154
2155         if (!genpdspec)
2156                 return ERR_PTR(-EINVAL);
2157
2158         mutex_lock(&of_genpd_mutex);
2159
2160         /* Check if we have such a provider in our array */
2161         list_for_each_entry(provider, &of_genpd_providers, link) {
2162                 if (provider->node == genpdspec->np)
2163                         genpd = provider->xlate(genpdspec, provider->data);
2164                 if (!IS_ERR(genpd))
2165                         break;
2166         }
2167
2168         mutex_unlock(&of_genpd_mutex);
2169
2170         return genpd;
2171 }
2172
2173 /**
2174  * of_genpd_add_device() - Add a device to an I/O PM domain
2175  * @genpdspec: OF phandle args to use for look-up PM domain
2176  * @dev: Device to be added.
2177  *
2178  * Looks-up an I/O PM domain based upon phandle args provided and adds
2179  * the device to the PM domain. Returns a negative error code on failure.
2180  */
2181 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2182 {
2183         struct generic_pm_domain *genpd;
2184         int ret;
2185
2186         mutex_lock(&gpd_list_lock);
2187
2188         genpd = genpd_get_from_provider(genpdspec);
2189         if (IS_ERR(genpd)) {
2190                 ret = PTR_ERR(genpd);
2191                 goto out;
2192         }
2193
2194         ret = genpd_add_device(genpd, dev, NULL);
2195
2196 out:
2197         mutex_unlock(&gpd_list_lock);
2198
2199         return ret;
2200 }
2201 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2202
2203 /**
2204  * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2205  * @parent_spec: OF phandle args to use for parent PM domain look-up
2206  * @subdomain_spec: OF phandle args to use for subdomain look-up
2207  *
2208  * Looks-up a parent PM domain and subdomain based upon phandle args
2209  * provided and adds the subdomain to the parent PM domain. Returns a
2210  * negative error code on failure.
2211  */
2212 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2213                            struct of_phandle_args *subdomain_spec)
2214 {
2215         struct generic_pm_domain *parent, *subdomain;
2216         int ret;
2217
2218         mutex_lock(&gpd_list_lock);
2219
2220         parent = genpd_get_from_provider(parent_spec);
2221         if (IS_ERR(parent)) {
2222                 ret = PTR_ERR(parent);
2223                 goto out;
2224         }
2225
2226         subdomain = genpd_get_from_provider(subdomain_spec);
2227         if (IS_ERR(subdomain)) {
2228                 ret = PTR_ERR(subdomain);
2229                 goto out;
2230         }
2231
2232         ret = genpd_add_subdomain(parent, subdomain);
2233
2234 out:
2235         mutex_unlock(&gpd_list_lock);
2236
2237         return ret;
2238 }
2239 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2240
2241 /**
2242  * of_genpd_remove_last - Remove the last PM domain registered for a provider
2243  * @provider: Pointer to device structure associated with provider
2244  *
2245  * Find the last PM domain that was added by a particular provider and
2246  * remove this PM domain from the list of PM domains. The provider is
2247  * identified by the 'provider' device structure that is passed. The PM
2248  * domain will only be removed, if the provider associated with domain
2249  * has been removed.
2250  *
2251  * Returns a valid pointer to struct generic_pm_domain on success or
2252  * ERR_PTR() on failure.
2253  */
2254 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2255 {
2256         struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2257         int ret;
2258
2259         if (IS_ERR_OR_NULL(np))
2260                 return ERR_PTR(-EINVAL);
2261
2262         mutex_lock(&gpd_list_lock);
2263         list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2264                 if (gpd->provider == &np->fwnode) {
2265                         ret = genpd_remove(gpd);
2266                         genpd = ret ? ERR_PTR(ret) : gpd;
2267                         break;
2268                 }
2269         }
2270         mutex_unlock(&gpd_list_lock);
2271
2272         return genpd;
2273 }
2274 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2275
2276 static void genpd_release_dev(struct device *dev)
2277 {
2278         kfree(dev);
2279 }
2280
2281 static struct bus_type genpd_bus_type = {
2282         .name           = "genpd",
2283 };
2284
2285 /**
2286  * genpd_dev_pm_detach - Detach a device from its PM domain.
2287  * @dev: Device to detach.
2288  * @power_off: Currently not used
2289  *
2290  * Try to locate a corresponding generic PM domain, which the device was
2291  * attached to previously. If such is found, the device is detached from it.
2292  */
2293 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2294 {
2295         struct generic_pm_domain *pd;
2296         unsigned int i;
2297         int ret = 0;
2298
2299         pd = dev_to_genpd(dev);
2300         if (IS_ERR(pd))
2301                 return;
2302
2303         dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2304
2305         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2306                 ret = genpd_remove_device(pd, dev);
2307                 if (ret != -EAGAIN)
2308                         break;
2309
2310                 mdelay(i);
2311                 cond_resched();
2312         }
2313
2314         if (ret < 0) {
2315                 dev_err(dev, "failed to remove from PM domain %s: %d",
2316                         pd->name, ret);
2317                 return;
2318         }
2319
2320         /* Check if PM domain can be powered off after removing this device. */
2321         genpd_queue_power_off_work(pd);
2322
2323         /* Unregister the device if it was created by genpd. */
2324         if (dev->bus == &genpd_bus_type)
2325                 device_unregister(dev);
2326 }
2327
2328 static void genpd_dev_pm_sync(struct device *dev)
2329 {
2330         struct generic_pm_domain *pd;
2331
2332         pd = dev_to_genpd(dev);
2333         if (IS_ERR(pd))
2334                 return;
2335
2336         genpd_queue_power_off_work(pd);
2337 }
2338
2339 static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
2340                                  unsigned int index, bool power_on)
2341 {
2342         struct of_phandle_args pd_args;
2343         struct generic_pm_domain *pd;
2344         int ret;
2345
2346         ret = of_parse_phandle_with_args(np, "power-domains",
2347                                 "#power-domain-cells", index, &pd_args);
2348         if (ret < 0)
2349                 return ret;
2350
2351         mutex_lock(&gpd_list_lock);
2352         pd = genpd_get_from_provider(&pd_args);
2353         of_node_put(pd_args.np);
2354         if (IS_ERR(pd)) {
2355                 mutex_unlock(&gpd_list_lock);
2356                 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2357                         __func__, PTR_ERR(pd));
2358                 return driver_deferred_probe_check_state(dev);
2359         }
2360
2361         dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2362
2363         ret = genpd_add_device(pd, dev, NULL);
2364         mutex_unlock(&gpd_list_lock);
2365
2366         if (ret < 0) {
2367                 if (ret != -EPROBE_DEFER)
2368                         dev_err(dev, "failed to add to PM domain %s: %d",
2369                                 pd->name, ret);
2370                 return ret;
2371         }
2372
2373         dev->pm_domain->detach = genpd_dev_pm_detach;
2374         dev->pm_domain->sync = genpd_dev_pm_sync;
2375
2376         if (power_on) {
2377                 genpd_lock(pd);
2378                 ret = genpd_power_on(pd, 0);
2379                 genpd_unlock(pd);
2380         }
2381
2382         if (ret)
2383                 genpd_remove_device(pd, dev);
2384
2385         return ret ? -EPROBE_DEFER : 1;
2386 }
2387
2388 /**
2389  * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2390  * @dev: Device to attach.
2391  *
2392  * Parse device's OF node to find a PM domain specifier. If such is found,
2393  * attaches the device to retrieved pm_domain ops.
2394  *
2395  * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2396  * PM domain or when multiple power-domains exists for it, else a negative error
2397  * code. Note that if a power-domain exists for the device, but it cannot be
2398  * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2399  * not probed and to re-try again later.
2400  */
2401 int genpd_dev_pm_attach(struct device *dev)
2402 {
2403         if (!dev->of_node)
2404                 return 0;
2405
2406         /*
2407          * Devices with multiple PM domains must be attached separately, as we
2408          * can only attach one PM domain per device.
2409          */
2410         if (of_count_phandle_with_args(dev->of_node, "power-domains",
2411                                        "#power-domain-cells") != 1)
2412                 return 0;
2413
2414         return __genpd_dev_pm_attach(dev, dev->of_node, 0, true);
2415 }
2416 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2417
2418 /**
2419  * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2420  * @dev: The device used to lookup the PM domain.
2421  * @index: The index of the PM domain.
2422  *
2423  * Parse device's OF node to find a PM domain specifier at the provided @index.
2424  * If such is found, creates a virtual device and attaches it to the retrieved
2425  * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2426  * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2427  *
2428  * Returns the created virtual device if successfully attached PM domain, NULL
2429  * when the device don't need a PM domain, else an ERR_PTR() in case of
2430  * failures. If a power-domain exists for the device, but cannot be found or
2431  * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2432  * is not probed and to re-try again later.
2433  */
2434 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2435                                          unsigned int index)
2436 {
2437         struct device *virt_dev;
2438         int num_domains;
2439         int ret;
2440
2441         if (!dev->of_node)
2442                 return NULL;
2443
2444         /* Deal only with devices using multiple PM domains. */
2445         num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2446                                                  "#power-domain-cells");
2447         if (num_domains < 2 || index >= num_domains)
2448                 return NULL;
2449
2450         /* Allocate and register device on the genpd bus. */
2451         virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2452         if (!virt_dev)
2453                 return ERR_PTR(-ENOMEM);
2454
2455         dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2456         virt_dev->bus = &genpd_bus_type;
2457         virt_dev->release = genpd_release_dev;
2458
2459         ret = device_register(virt_dev);
2460         if (ret) {
2461                 kfree(virt_dev);
2462                 return ERR_PTR(ret);
2463         }
2464
2465         /* Try to attach the device to the PM domain at the specified index. */
2466         ret = __genpd_dev_pm_attach(virt_dev, dev->of_node, index, false);
2467         if (ret < 1) {
2468                 device_unregister(virt_dev);
2469                 return ret ? ERR_PTR(ret) : NULL;
2470         }
2471
2472         pm_runtime_enable(virt_dev);
2473         genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2474
2475         return virt_dev;
2476 }
2477 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2478
2479 /**
2480  * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2481  * @dev: The device used to lookup the PM domain.
2482  * @name: The name of the PM domain.
2483  *
2484  * Parse device's OF node to find a PM domain specifier using the
2485  * power-domain-names DT property. For further description see
2486  * genpd_dev_pm_attach_by_id().
2487  */
2488 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2489 {
2490         int index;
2491
2492         if (!dev->of_node)
2493                 return NULL;
2494
2495         index = of_property_match_string(dev->of_node, "power-domain-names",
2496                                          name);
2497         if (index < 0)
2498                 return NULL;
2499
2500         return genpd_dev_pm_attach_by_id(dev, index);
2501 }
2502
2503 static const struct of_device_id idle_state_match[] = {
2504         { .compatible = "domain-idle-state", },
2505         { }
2506 };
2507
2508 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2509                                     struct device_node *state_node)
2510 {
2511         int err;
2512         u32 residency;
2513         u32 entry_latency, exit_latency;
2514
2515         err = of_property_read_u32(state_node, "entry-latency-us",
2516                                                 &entry_latency);
2517         if (err) {
2518                 pr_debug(" * %pOF missing entry-latency-us property\n",
2519                          state_node);
2520                 return -EINVAL;
2521         }
2522
2523         err = of_property_read_u32(state_node, "exit-latency-us",
2524                                                 &exit_latency);
2525         if (err) {
2526                 pr_debug(" * %pOF missing exit-latency-us property\n",
2527                          state_node);
2528                 return -EINVAL;
2529         }
2530
2531         err = of_property_read_u32(state_node, "min-residency-us", &residency);
2532         if (!err)
2533                 genpd_state->residency_ns = 1000 * residency;
2534
2535         genpd_state->power_on_latency_ns = 1000 * exit_latency;
2536         genpd_state->power_off_latency_ns = 1000 * entry_latency;
2537         genpd_state->fwnode = &state_node->fwnode;
2538
2539         return 0;
2540 }
2541
2542 static int genpd_iterate_idle_states(struct device_node *dn,
2543                                      struct genpd_power_state *states)
2544 {
2545         int ret;
2546         struct of_phandle_iterator it;
2547         struct device_node *np;
2548         int i = 0;
2549
2550         ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2551         if (ret <= 0)
2552                 return ret;
2553
2554         /* Loop over the phandles until all the requested entry is found */
2555         of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2556                 np = it.node;
2557                 if (!of_match_node(idle_state_match, np))
2558                         continue;
2559                 if (states) {
2560                         ret = genpd_parse_state(&states[i], np);
2561                         if (ret) {
2562                                 pr_err("Parsing idle state node %pOF failed with err %d\n",
2563                                        np, ret);
2564                                 of_node_put(np);
2565                                 return ret;
2566                         }
2567                 }
2568                 i++;
2569         }
2570
2571         return i;
2572 }
2573
2574 /**
2575  * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2576  *
2577  * @dn: The genpd device node
2578  * @states: The pointer to which the state array will be saved.
2579  * @n: The count of elements in the array returned from this function.
2580  *
2581  * Returns the device states parsed from the OF node. The memory for the states
2582  * is allocated by this function and is the responsibility of the caller to
2583  * free the memory after use. If any or zero compatible domain idle states is
2584  * found it returns 0 and in case of errors, a negative error code is returned.
2585  */
2586 int of_genpd_parse_idle_states(struct device_node *dn,
2587                         struct genpd_power_state **states, int *n)
2588 {
2589         struct genpd_power_state *st;
2590         int ret;
2591
2592         ret = genpd_iterate_idle_states(dn, NULL);
2593         if (ret < 0)
2594                 return ret;
2595
2596         if (!ret) {
2597                 *states = NULL;
2598                 *n = 0;
2599                 return 0;
2600         }
2601
2602         st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2603         if (!st)
2604                 return -ENOMEM;
2605
2606         ret = genpd_iterate_idle_states(dn, st);
2607         if (ret <= 0) {
2608                 kfree(st);
2609                 return ret < 0 ? ret : -EINVAL;
2610         }
2611
2612         *states = st;
2613         *n = ret;
2614
2615         return 0;
2616 }
2617 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2618
2619 /**
2620  * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2621  *
2622  * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2623  * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2624  *      state.
2625  *
2626  * Returns performance state encoded in the OPP of the genpd. This calls
2627  * platform specific genpd->opp_to_performance_state() callback to translate
2628  * power domain OPP to performance state.
2629  *
2630  * Returns performance state on success and 0 on failure.
2631  */
2632 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2633                                                struct dev_pm_opp *opp)
2634 {
2635         struct generic_pm_domain *genpd = NULL;
2636         int state;
2637
2638         genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2639
2640         if (unlikely(!genpd->opp_to_performance_state))
2641                 return 0;
2642
2643         genpd_lock(genpd);
2644         state = genpd->opp_to_performance_state(genpd, opp);
2645         genpd_unlock(genpd);
2646
2647         return state;
2648 }
2649 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2650
2651 static int __init genpd_bus_init(void)
2652 {
2653         return bus_register(&genpd_bus_type);
2654 }
2655 core_initcall(genpd_bus_init);
2656
2657 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2658
2659
2660 /***        debugfs support        ***/
2661
2662 #ifdef CONFIG_DEBUG_FS
2663 #include <linux/pm.h>
2664 #include <linux/device.h>
2665 #include <linux/debugfs.h>
2666 #include <linux/seq_file.h>
2667 #include <linux/init.h>
2668 #include <linux/kobject.h>
2669 static struct dentry *genpd_debugfs_dir;
2670
2671 /*
2672  * TODO: This function is a slightly modified version of rtpm_status_show
2673  * from sysfs.c, so generalize it.
2674  */
2675 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2676 {
2677         static const char * const status_lookup[] = {
2678                 [RPM_ACTIVE] = "active",
2679                 [RPM_RESUMING] = "resuming",
2680                 [RPM_SUSPENDED] = "suspended",
2681                 [RPM_SUSPENDING] = "suspending"
2682         };
2683         const char *p = "";
2684
2685         if (dev->power.runtime_error)
2686                 p = "error";
2687         else if (dev->power.disable_depth)
2688                 p = "unsupported";
2689         else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2690                 p = status_lookup[dev->power.runtime_status];
2691         else
2692                 WARN_ON(1);
2693
2694         seq_puts(s, p);
2695 }
2696
2697 static int genpd_summary_one(struct seq_file *s,
2698                         struct generic_pm_domain *genpd)
2699 {
2700         static const char * const status_lookup[] = {
2701                 [GPD_STATE_ACTIVE] = "on",
2702                 [GPD_STATE_POWER_OFF] = "off"
2703         };
2704         struct pm_domain_data *pm_data;
2705         const char *kobj_path;
2706         struct gpd_link *link;
2707         char state[16];
2708         int ret;
2709
2710         ret = genpd_lock_interruptible(genpd);
2711         if (ret)
2712                 return -ERESTARTSYS;
2713
2714         if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2715                 goto exit;
2716         if (!genpd_status_on(genpd))
2717                 snprintf(state, sizeof(state), "%s-%u",
2718                          status_lookup[genpd->status], genpd->state_idx);
2719         else
2720                 snprintf(state, sizeof(state), "%s",
2721                          status_lookup[genpd->status]);
2722         seq_printf(s, "%-30s  %-15s ", genpd->name, state);
2723
2724         /*
2725          * Modifications on the list require holding locks on both
2726          * master and slave, so we are safe.
2727          * Also genpd->name is immutable.
2728          */
2729         list_for_each_entry(link, &genpd->master_links, master_node) {
2730                 seq_printf(s, "%s", link->slave->name);
2731                 if (!list_is_last(&link->master_node, &genpd->master_links))
2732                         seq_puts(s, ", ");
2733         }
2734
2735         list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2736                 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2737                                 genpd_is_irq_safe(genpd) ?
2738                                 GFP_ATOMIC : GFP_KERNEL);
2739                 if (kobj_path == NULL)
2740                         continue;
2741
2742                 seq_printf(s, "\n    %-50s  ", kobj_path);
2743                 rtpm_status_str(s, pm_data->dev);
2744                 kfree(kobj_path);
2745         }
2746
2747         seq_puts(s, "\n");
2748 exit:
2749         genpd_unlock(genpd);
2750
2751         return 0;
2752 }
2753
2754 static int summary_show(struct seq_file *s, void *data)
2755 {
2756         struct generic_pm_domain *genpd;
2757         int ret = 0;
2758
2759         seq_puts(s, "domain                          status          slaves\n");
2760         seq_puts(s, "    /device                                             runtime status\n");
2761         seq_puts(s, "----------------------------------------------------------------------\n");
2762
2763         ret = mutex_lock_interruptible(&gpd_list_lock);
2764         if (ret)
2765                 return -ERESTARTSYS;
2766
2767         list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2768                 ret = genpd_summary_one(s, genpd);
2769                 if (ret)
2770                         break;
2771         }
2772         mutex_unlock(&gpd_list_lock);
2773
2774         return ret;
2775 }
2776
2777 static int status_show(struct seq_file *s, void *data)
2778 {
2779         static const char * const status_lookup[] = {
2780                 [GPD_STATE_ACTIVE] = "on",
2781                 [GPD_STATE_POWER_OFF] = "off"
2782         };
2783
2784         struct generic_pm_domain *genpd = s->private;
2785         int ret = 0;
2786
2787         ret = genpd_lock_interruptible(genpd);
2788         if (ret)
2789                 return -ERESTARTSYS;
2790
2791         if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2792                 goto exit;
2793
2794         if (genpd->status == GPD_STATE_POWER_OFF)
2795                 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2796                         genpd->state_idx);
2797         else
2798                 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2799 exit:
2800         genpd_unlock(genpd);
2801         return ret;
2802 }
2803
2804 static int sub_domains_show(struct seq_file *s, void *data)
2805 {
2806         struct generic_pm_domain *genpd = s->private;
2807         struct gpd_link *link;
2808         int ret = 0;
2809
2810         ret = genpd_lock_interruptible(genpd);
2811         if (ret)
2812                 return -ERESTARTSYS;
2813
2814         list_for_each_entry(link, &genpd->master_links, master_node)
2815                 seq_printf(s, "%s\n", link->slave->name);
2816
2817         genpd_unlock(genpd);
2818         return ret;
2819 }
2820
2821 static int idle_states_show(struct seq_file *s, void *data)
2822 {
2823         struct generic_pm_domain *genpd = s->private;
2824         unsigned int i;
2825         int ret = 0;
2826
2827         ret = genpd_lock_interruptible(genpd);
2828         if (ret)
2829                 return -ERESTARTSYS;
2830
2831         seq_puts(s, "State          Time Spent(ms)\n");
2832
2833         for (i = 0; i < genpd->state_count; i++) {
2834                 ktime_t delta = 0;
2835                 s64 msecs;
2836
2837                 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2838                                 (genpd->state_idx == i))
2839                         delta = ktime_sub(ktime_get(), genpd->accounting_time);
2840
2841                 msecs = ktime_to_ms(
2842                         ktime_add(genpd->states[i].idle_time, delta));
2843                 seq_printf(s, "S%-13i %lld\n", i, msecs);
2844         }
2845
2846         genpd_unlock(genpd);
2847         return ret;
2848 }
2849
2850 static int active_time_show(struct seq_file *s, void *data)
2851 {
2852         struct generic_pm_domain *genpd = s->private;
2853         ktime_t delta = 0;
2854         int ret = 0;
2855
2856         ret = genpd_lock_interruptible(genpd);
2857         if (ret)
2858                 return -ERESTARTSYS;
2859
2860         if (genpd->status == GPD_STATE_ACTIVE)
2861                 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2862
2863         seq_printf(s, "%lld ms\n", ktime_to_ms(
2864                                 ktime_add(genpd->on_time, delta)));
2865
2866         genpd_unlock(genpd);
2867         return ret;
2868 }
2869
2870 static int total_idle_time_show(struct seq_file *s, void *data)
2871 {
2872         struct generic_pm_domain *genpd = s->private;
2873         ktime_t delta = 0, total = 0;
2874         unsigned int i;
2875         int ret = 0;
2876
2877         ret = genpd_lock_interruptible(genpd);
2878         if (ret)
2879                 return -ERESTARTSYS;
2880
2881         for (i = 0; i < genpd->state_count; i++) {
2882
2883                 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2884                                 (genpd->state_idx == i))
2885                         delta = ktime_sub(ktime_get(), genpd->accounting_time);
2886
2887                 total = ktime_add(total, genpd->states[i].idle_time);
2888         }
2889         total = ktime_add(total, delta);
2890
2891         seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2892
2893         genpd_unlock(genpd);
2894         return ret;
2895 }
2896
2897
2898 static int devices_show(struct seq_file *s, void *data)
2899 {
2900         struct generic_pm_domain *genpd = s->private;
2901         struct pm_domain_data *pm_data;
2902         const char *kobj_path;
2903         int ret = 0;
2904
2905         ret = genpd_lock_interruptible(genpd);
2906         if (ret)
2907                 return -ERESTARTSYS;
2908
2909         list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2910                 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2911                                 genpd_is_irq_safe(genpd) ?
2912                                 GFP_ATOMIC : GFP_KERNEL);
2913                 if (kobj_path == NULL)
2914                         continue;
2915
2916                 seq_printf(s, "%s\n", kobj_path);
2917                 kfree(kobj_path);
2918         }
2919
2920         genpd_unlock(genpd);
2921         return ret;
2922 }
2923
2924 static int perf_state_show(struct seq_file *s, void *data)
2925 {
2926         struct generic_pm_domain *genpd = s->private;
2927
2928         if (genpd_lock_interruptible(genpd))
2929                 return -ERESTARTSYS;
2930
2931         seq_printf(s, "%u\n", genpd->performance_state);
2932
2933         genpd_unlock(genpd);
2934         return 0;
2935 }
2936
2937 DEFINE_SHOW_ATTRIBUTE(summary);
2938 DEFINE_SHOW_ATTRIBUTE(status);
2939 DEFINE_SHOW_ATTRIBUTE(sub_domains);
2940 DEFINE_SHOW_ATTRIBUTE(idle_states);
2941 DEFINE_SHOW_ATTRIBUTE(active_time);
2942 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
2943 DEFINE_SHOW_ATTRIBUTE(devices);
2944 DEFINE_SHOW_ATTRIBUTE(perf_state);
2945
2946 static int __init genpd_debug_init(void)
2947 {
2948         struct dentry *d;
2949         struct generic_pm_domain *genpd;
2950
2951         genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2952
2953         debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
2954                             NULL, &summary_fops);
2955
2956         list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2957                 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
2958
2959                 debugfs_create_file("current_state", 0444,
2960                                 d, genpd, &status_fops);
2961                 debugfs_create_file("sub_domains", 0444,
2962                                 d, genpd, &sub_domains_fops);
2963                 debugfs_create_file("idle_states", 0444,
2964                                 d, genpd, &idle_states_fops);
2965                 debugfs_create_file("active_time", 0444,
2966                                 d, genpd, &active_time_fops);
2967                 debugfs_create_file("total_idle_time", 0444,
2968                                 d, genpd, &total_idle_time_fops);
2969                 debugfs_create_file("devices", 0444,
2970                                 d, genpd, &devices_fops);
2971                 if (genpd->set_performance_state)
2972                         debugfs_create_file("perf_state", 0444,
2973                                             d, genpd, &perf_state_fops);
2974         }
2975
2976         return 0;
2977 }
2978 late_initcall(genpd_debug_init);
2979
2980 static void __exit genpd_debug_exit(void)
2981 {
2982         debugfs_remove_recursive(genpd_debugfs_dir);
2983 }
2984 __exitcall(genpd_debug_exit);
2985 #endif /* CONFIG_DEBUG_FS */