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