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