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