]>
Commit | Line | Data |
---|---|---|
f721889f RW |
1 | /* |
2 | * drivers/base/power/domain.c - Common code related to device power domains. | |
3 | * | |
4 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. | |
5 | * | |
6 | * This file is released under the GPLv2. | |
7 | */ | |
8 | ||
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> |
f721889f RW |
13 | #include <linux/pm_runtime.h> |
14 | #include <linux/pm_domain.h> | |
6ff7bb0d | 15 | #include <linux/pm_qos.h> |
c11f6f5b | 16 | #include <linux/pm_clock.h> |
f721889f RW |
17 | #include <linux/slab.h> |
18 | #include <linux/err.h> | |
17b75eca RW |
19 | #include <linux/sched.h> |
20 | #include <linux/suspend.h> | |
d5e4cbfe RW |
21 | #include <linux/export.h> |
22 | ||
aa8e54b5 TV |
23 | #include "power.h" |
24 | ||
93af5e93 GU |
25 | #define GENPD_RETRY_MAX_MS 250 /* Approximate */ |
26 | ||
d5e4cbfe RW |
27 | #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \ |
28 | ({ \ | |
29 | type (*__routine)(struct device *__d); \ | |
30 | type __ret = (type)0; \ | |
31 | \ | |
32 | __routine = genpd->dev_ops.callback; \ | |
33 | if (__routine) { \ | |
34 | __ret = __routine(dev); \ | |
d5e4cbfe RW |
35 | } \ |
36 | __ret; \ | |
37 | }) | |
f721889f | 38 | |
5125bbf3 RW |
39 | static LIST_HEAD(gpd_list); |
40 | static DEFINE_MUTEX(gpd_list_lock); | |
41 | ||
35241d12 LI |
42 | struct genpd_lock_ops { |
43 | void (*lock)(struct generic_pm_domain *genpd); | |
44 | void (*lock_nested)(struct generic_pm_domain *genpd, int depth); | |
45 | int (*lock_interruptible)(struct generic_pm_domain *genpd); | |
46 | void (*unlock)(struct generic_pm_domain *genpd); | |
47 | }; | |
48 | ||
49 | static void genpd_lock_mtx(struct generic_pm_domain *genpd) | |
50 | { | |
51 | mutex_lock(&genpd->mlock); | |
52 | } | |
53 | ||
54 | static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd, | |
55 | int depth) | |
56 | { | |
57 | mutex_lock_nested(&genpd->mlock, depth); | |
58 | } | |
59 | ||
60 | static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd) | |
61 | { | |
62 | return mutex_lock_interruptible(&genpd->mlock); | |
63 | } | |
64 | ||
65 | static void genpd_unlock_mtx(struct generic_pm_domain *genpd) | |
66 | { | |
67 | return mutex_unlock(&genpd->mlock); | |
68 | } | |
69 | ||
70 | static const struct genpd_lock_ops genpd_mtx_ops = { | |
71 | .lock = genpd_lock_mtx, | |
72 | .lock_nested = genpd_lock_nested_mtx, | |
73 | .lock_interruptible = genpd_lock_interruptible_mtx, | |
74 | .unlock = genpd_unlock_mtx, | |
75 | }; | |
76 | ||
d716f479 LI |
77 | static void genpd_lock_spin(struct generic_pm_domain *genpd) |
78 | __acquires(&genpd->slock) | |
79 | { | |
80 | unsigned long flags; | |
81 | ||
82 | spin_lock_irqsave(&genpd->slock, flags); | |
83 | genpd->lock_flags = flags; | |
84 | } | |
85 | ||
86 | static void genpd_lock_nested_spin(struct generic_pm_domain *genpd, | |
87 | int depth) | |
88 | __acquires(&genpd->slock) | |
89 | { | |
90 | unsigned long flags; | |
91 | ||
92 | spin_lock_irqsave_nested(&genpd->slock, flags, depth); | |
93 | genpd->lock_flags = flags; | |
94 | } | |
95 | ||
96 | static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd) | |
97 | __acquires(&genpd->slock) | |
98 | { | |
99 | unsigned long flags; | |
100 | ||
101 | spin_lock_irqsave(&genpd->slock, flags); | |
102 | genpd->lock_flags = flags; | |
103 | return 0; | |
104 | } | |
105 | ||
106 | static void genpd_unlock_spin(struct generic_pm_domain *genpd) | |
107 | __releases(&genpd->slock) | |
108 | { | |
109 | spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags); | |
110 | } | |
111 | ||
112 | static const struct genpd_lock_ops genpd_spin_ops = { | |
113 | .lock = genpd_lock_spin, | |
114 | .lock_nested = genpd_lock_nested_spin, | |
115 | .lock_interruptible = genpd_lock_interruptible_spin, | |
116 | .unlock = genpd_unlock_spin, | |
117 | }; | |
118 | ||
35241d12 LI |
119 | #define genpd_lock(p) p->lock_ops->lock(p) |
120 | #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d) | |
121 | #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p) | |
122 | #define genpd_unlock(p) p->lock_ops->unlock(p) | |
123 | ||
41e2c8e0 | 124 | #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE) |
d716f479 | 125 | #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE) |
ffaa42e8 | 126 | #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON) |
d716f479 LI |
127 | |
128 | static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev, | |
d8600c8b | 129 | const struct generic_pm_domain *genpd) |
d716f479 LI |
130 | { |
131 | bool ret; | |
132 | ||
133 | ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd); | |
134 | ||
075c37d5 UH |
135 | /* |
136 | * Warn once if an IRQ safe device is attached to a no sleep domain, as | |
137 | * to indicate a suboptimal configuration for PM. For an always on | |
138 | * domain this isn't case, thus don't warn. | |
139 | */ | |
140 | if (ret && !genpd_is_always_on(genpd)) | |
d716f479 LI |
141 | dev_warn_once(dev, "PM domain %s will not be powered off\n", |
142 | genpd->name); | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
446d999c RK |
147 | /* |
148 | * Get the generic PM domain for a particular struct device. | |
149 | * This validates the struct device pointer, the PM domain pointer, | |
150 | * and checks that the PM domain pointer is a real generic PM domain. | |
151 | * Any failure results in NULL being returned. | |
152 | */ | |
f58d4e5a | 153 | static struct generic_pm_domain *genpd_lookup_dev(struct device *dev) |
446d999c RK |
154 | { |
155 | struct generic_pm_domain *genpd = NULL, *gpd; | |
156 | ||
157 | if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain)) | |
158 | return NULL; | |
159 | ||
160 | mutex_lock(&gpd_list_lock); | |
161 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) { | |
162 | if (&gpd->domain == dev->pm_domain) { | |
163 | genpd = gpd; | |
164 | break; | |
165 | } | |
166 | } | |
167 | mutex_unlock(&gpd_list_lock); | |
168 | ||
169 | return genpd; | |
170 | } | |
171 | ||
172 | /* | |
173 | * This should only be used where we are certain that the pm_domain | |
174 | * attached to the device is a genpd domain. | |
175 | */ | |
176 | static struct generic_pm_domain *dev_to_genpd(struct device *dev) | |
5248051b RW |
177 | { |
178 | if (IS_ERR_OR_NULL(dev->pm_domain)) | |
179 | return ERR_PTR(-EINVAL); | |
180 | ||
596ba34b | 181 | return pd_to_genpd(dev->pm_domain); |
5248051b | 182 | } |
f721889f | 183 | |
d8600c8b KK |
184 | static int genpd_stop_dev(const struct generic_pm_domain *genpd, |
185 | struct device *dev) | |
d5e4cbfe | 186 | { |
2b1d88cd | 187 | return GENPD_DEV_CALLBACK(genpd, int, stop, dev); |
d5e4cbfe RW |
188 | } |
189 | ||
d8600c8b KK |
190 | static int genpd_start_dev(const struct generic_pm_domain *genpd, |
191 | struct device *dev) | |
d5e4cbfe | 192 | { |
2b1d88cd | 193 | return GENPD_DEV_CALLBACK(genpd, int, start, dev); |
d5e4cbfe RW |
194 | } |
195 | ||
c4bb3160 | 196 | static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd) |
f721889f | 197 | { |
c4bb3160 RW |
198 | bool ret = false; |
199 | ||
200 | if (!WARN_ON(atomic_read(&genpd->sd_count) == 0)) | |
201 | ret = !!atomic_dec_and_test(&genpd->sd_count); | |
202 | ||
203 | return ret; | |
204 | } | |
205 | ||
206 | static void genpd_sd_counter_inc(struct generic_pm_domain *genpd) | |
207 | { | |
208 | atomic_inc(&genpd->sd_count); | |
4e857c58 | 209 | smp_mb__after_atomic(); |
f721889f RW |
210 | } |
211 | ||
86e12eac | 212 | static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed) |
c8f0ea45 | 213 | { |
fc5cbf0c | 214 | unsigned int state_idx = genpd->state_idx; |
c8f0ea45 GU |
215 | ktime_t time_start; |
216 | s64 elapsed_ns; | |
217 | int ret; | |
218 | ||
219 | if (!genpd->power_on) | |
220 | return 0; | |
221 | ||
a4630c61 GU |
222 | if (!timed) |
223 | return genpd->power_on(genpd); | |
224 | ||
c8f0ea45 GU |
225 | time_start = ktime_get(); |
226 | ret = genpd->power_on(genpd); | |
227 | if (ret) | |
228 | return ret; | |
229 | ||
230 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); | |
fc5cbf0c | 231 | if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns) |
c8f0ea45 GU |
232 | return ret; |
233 | ||
fc5cbf0c | 234 | genpd->states[state_idx].power_on_latency_ns = elapsed_ns; |
c8f0ea45 | 235 | genpd->max_off_time_changed = true; |
6d7d5c32 RK |
236 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n", |
237 | genpd->name, "on", elapsed_ns); | |
c8f0ea45 GU |
238 | |
239 | return ret; | |
240 | } | |
241 | ||
86e12eac | 242 | static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed) |
c8f0ea45 | 243 | { |
fc5cbf0c | 244 | unsigned int state_idx = genpd->state_idx; |
c8f0ea45 GU |
245 | ktime_t time_start; |
246 | s64 elapsed_ns; | |
247 | int ret; | |
248 | ||
249 | if (!genpd->power_off) | |
250 | return 0; | |
251 | ||
a4630c61 GU |
252 | if (!timed) |
253 | return genpd->power_off(genpd); | |
254 | ||
c8f0ea45 GU |
255 | time_start = ktime_get(); |
256 | ret = genpd->power_off(genpd); | |
257 | if (ret == -EBUSY) | |
258 | return ret; | |
259 | ||
260 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); | |
fc5cbf0c | 261 | if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns) |
c8f0ea45 GU |
262 | return ret; |
263 | ||
fc5cbf0c | 264 | genpd->states[state_idx].power_off_latency_ns = elapsed_ns; |
c8f0ea45 | 265 | genpd->max_off_time_changed = true; |
6d7d5c32 RK |
266 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n", |
267 | genpd->name, "off", elapsed_ns); | |
c8f0ea45 GU |
268 | |
269 | return ret; | |
270 | } | |
271 | ||
29e47e21 | 272 | /** |
86e12eac | 273 | * genpd_queue_power_off_work - Queue up the execution of genpd_power_off(). |
a3d09c73 | 274 | * @genpd: PM domain to power off. |
29e47e21 | 275 | * |
86e12eac | 276 | * Queue up the execution of genpd_power_off() unless it's already been done |
29e47e21 UH |
277 | * before. |
278 | */ | |
279 | static void genpd_queue_power_off_work(struct generic_pm_domain *genpd) | |
280 | { | |
281 | queue_work(pm_wq, &genpd->power_off_work); | |
282 | } | |
283 | ||
1f8728b7 UH |
284 | /** |
285 | * genpd_power_off - Remove power from a given PM domain. | |
286 | * @genpd: PM domain to power down. | |
3c64649d UH |
287 | * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the |
288 | * RPM status of the releated device is in an intermediate state, not yet turned | |
289 | * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not | |
290 | * be RPM_SUSPENDED, while it tries to power off the PM domain. | |
1f8728b7 UH |
291 | * |
292 | * If all of the @genpd's devices have been suspended and all of its subdomains | |
293 | * have been powered down, remove power from @genpd. | |
294 | */ | |
2da83545 UH |
295 | static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on, |
296 | unsigned int depth) | |
1f8728b7 UH |
297 | { |
298 | struct pm_domain_data *pdd; | |
299 | struct gpd_link *link; | |
300 | unsigned int not_suspended = 0; | |
301 | ||
302 | /* | |
303 | * Do not try to power off the domain in the following situations: | |
304 | * (1) The domain is already in the "power off" state. | |
305 | * (2) System suspend is in progress. | |
306 | */ | |
41e2c8e0 | 307 | if (!genpd_status_on(genpd) || genpd->prepared_count > 0) |
1f8728b7 UH |
308 | return 0; |
309 | ||
ffaa42e8 UH |
310 | /* |
311 | * Abort power off for the PM domain in the following situations: | |
312 | * (1) The domain is configured as always on. | |
313 | * (2) When the domain has a subdomain being powered on. | |
314 | */ | |
315 | if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0) | |
1f8728b7 UH |
316 | return -EBUSY; |
317 | ||
318 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { | |
319 | enum pm_qos_flags_status stat; | |
320 | ||
321 | stat = dev_pm_qos_flags(pdd->dev, | |
322 | PM_QOS_FLAG_NO_POWER_OFF | |
323 | | PM_QOS_FLAG_REMOTE_WAKEUP); | |
324 | if (stat > PM_QOS_FLAGS_NONE) | |
325 | return -EBUSY; | |
326 | ||
327 | /* | |
328 | * Do not allow PM domain to be powered off, when an IRQ safe | |
329 | * device is part of a non-IRQ safe domain. | |
330 | */ | |
331 | if (!pm_runtime_suspended(pdd->dev) || | |
332 | irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd)) | |
333 | not_suspended++; | |
334 | } | |
335 | ||
3c64649d | 336 | if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on)) |
1f8728b7 UH |
337 | return -EBUSY; |
338 | ||
339 | if (genpd->gov && genpd->gov->power_down_ok) { | |
340 | if (!genpd->gov->power_down_ok(&genpd->domain)) | |
341 | return -EAGAIN; | |
342 | } | |
343 | ||
344 | if (genpd->power_off) { | |
345 | int ret; | |
346 | ||
347 | if (atomic_read(&genpd->sd_count) > 0) | |
348 | return -EBUSY; | |
349 | ||
350 | /* | |
351 | * If sd_count > 0 at this point, one of the subdomains hasn't | |
352 | * managed to call genpd_power_on() for the master yet after | |
353 | * incrementing it. In that case genpd_power_on() will wait | |
354 | * for us to drop the lock, so we can call .power_off() and let | |
355 | * the genpd_power_on() restore power for us (this shouldn't | |
356 | * happen very often). | |
357 | */ | |
358 | ret = _genpd_power_off(genpd, true); | |
359 | if (ret) | |
360 | return ret; | |
361 | } | |
362 | ||
363 | genpd->status = GPD_STATE_POWER_OFF; | |
364 | ||
365 | list_for_each_entry(link, &genpd->slave_links, slave_node) { | |
366 | genpd_sd_counter_dec(link->master); | |
2da83545 UH |
367 | genpd_lock_nested(link->master, depth + 1); |
368 | genpd_power_off(link->master, false, depth + 1); | |
369 | genpd_unlock(link->master); | |
1f8728b7 UH |
370 | } |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
5248051b | 375 | /** |
86e12eac | 376 | * genpd_power_on - Restore power to a given PM domain and its masters. |
5248051b | 377 | * @genpd: PM domain to power up. |
0106ef51 | 378 | * @depth: nesting count for lockdep. |
5248051b | 379 | * |
5063ce15 | 380 | * Restore power to @genpd and all of its masters so that it is possible to |
5248051b RW |
381 | * resume a device belonging to it. |
382 | */ | |
86e12eac | 383 | static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth) |
5248051b | 384 | { |
5063ce15 | 385 | struct gpd_link *link; |
5248051b RW |
386 | int ret = 0; |
387 | ||
41e2c8e0 | 388 | if (genpd_status_on(genpd)) |
3f241775 | 389 | return 0; |
5248051b | 390 | |
5063ce15 RW |
391 | /* |
392 | * The list is guaranteed not to change while the loop below is being | |
393 | * executed, unless one of the masters' .power_on() callbacks fiddles | |
394 | * with it. | |
395 | */ | |
396 | list_for_each_entry(link, &genpd->slave_links, slave_node) { | |
0106ef51 MS |
397 | struct generic_pm_domain *master = link->master; |
398 | ||
399 | genpd_sd_counter_inc(master); | |
400 | ||
35241d12 | 401 | genpd_lock_nested(master, depth + 1); |
86e12eac | 402 | ret = genpd_power_on(master, depth + 1); |
35241d12 | 403 | genpd_unlock(master); |
5248051b | 404 | |
5063ce15 | 405 | if (ret) { |
0106ef51 | 406 | genpd_sd_counter_dec(master); |
9e08cf42 | 407 | goto err; |
5063ce15 | 408 | } |
5248051b RW |
409 | } |
410 | ||
86e12eac | 411 | ret = _genpd_power_on(genpd, true); |
c8f0ea45 GU |
412 | if (ret) |
413 | goto err; | |
5248051b | 414 | |
ba2bbfbf | 415 | genpd->status = GPD_STATE_ACTIVE; |
3f241775 | 416 | return 0; |
9e08cf42 RW |
417 | |
418 | err: | |
29e47e21 UH |
419 | list_for_each_entry_continue_reverse(link, |
420 | &genpd->slave_links, | |
421 | slave_node) { | |
5063ce15 | 422 | genpd_sd_counter_dec(link->master); |
2da83545 UH |
423 | genpd_lock_nested(link->master, depth + 1); |
424 | genpd_power_off(link->master, false, depth + 1); | |
425 | genpd_unlock(link->master); | |
29e47e21 | 426 | } |
9e08cf42 | 427 | |
3f241775 RW |
428 | return ret; |
429 | } | |
430 | ||
6ff7bb0d RW |
431 | static int genpd_dev_pm_qos_notifier(struct notifier_block *nb, |
432 | unsigned long val, void *ptr) | |
433 | { | |
434 | struct generic_pm_domain_data *gpd_data; | |
435 | struct device *dev; | |
436 | ||
437 | gpd_data = container_of(nb, struct generic_pm_domain_data, nb); | |
6ff7bb0d | 438 | dev = gpd_data->base.dev; |
6ff7bb0d RW |
439 | |
440 | for (;;) { | |
441 | struct generic_pm_domain *genpd; | |
442 | struct pm_domain_data *pdd; | |
443 | ||
444 | spin_lock_irq(&dev->power.lock); | |
445 | ||
446 | pdd = dev->power.subsys_data ? | |
447 | dev->power.subsys_data->domain_data : NULL; | |
b4883ca4 | 448 | if (pdd) { |
6ff7bb0d RW |
449 | to_gpd_data(pdd)->td.constraint_changed = true; |
450 | genpd = dev_to_genpd(dev); | |
451 | } else { | |
452 | genpd = ERR_PTR(-ENODATA); | |
453 | } | |
454 | ||
455 | spin_unlock_irq(&dev->power.lock); | |
456 | ||
457 | if (!IS_ERR(genpd)) { | |
35241d12 | 458 | genpd_lock(genpd); |
6ff7bb0d | 459 | genpd->max_off_time_changed = true; |
35241d12 | 460 | genpd_unlock(genpd); |
6ff7bb0d RW |
461 | } |
462 | ||
463 | dev = dev->parent; | |
464 | if (!dev || dev->power.ignore_children) | |
465 | break; | |
466 | } | |
467 | ||
468 | return NOTIFY_DONE; | |
469 | } | |
470 | ||
f721889f RW |
471 | /** |
472 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0. | |
473 | * @work: Work structure used for scheduling the execution of this function. | |
474 | */ | |
475 | static void genpd_power_off_work_fn(struct work_struct *work) | |
476 | { | |
477 | struct generic_pm_domain *genpd; | |
478 | ||
479 | genpd = container_of(work, struct generic_pm_domain, power_off_work); | |
480 | ||
35241d12 | 481 | genpd_lock(genpd); |
2da83545 | 482 | genpd_power_off(genpd, false, 0); |
35241d12 | 483 | genpd_unlock(genpd); |
f721889f RW |
484 | } |
485 | ||
54eeddbf UH |
486 | /** |
487 | * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks | |
488 | * @dev: Device to handle. | |
489 | */ | |
490 | static int __genpd_runtime_suspend(struct device *dev) | |
491 | { | |
492 | int (*cb)(struct device *__dev); | |
493 | ||
494 | if (dev->type && dev->type->pm) | |
495 | cb = dev->type->pm->runtime_suspend; | |
496 | else if (dev->class && dev->class->pm) | |
497 | cb = dev->class->pm->runtime_suspend; | |
498 | else if (dev->bus && dev->bus->pm) | |
499 | cb = dev->bus->pm->runtime_suspend; | |
500 | else | |
501 | cb = NULL; | |
502 | ||
503 | if (!cb && dev->driver && dev->driver->pm) | |
504 | cb = dev->driver->pm->runtime_suspend; | |
505 | ||
506 | return cb ? cb(dev) : 0; | |
507 | } | |
508 | ||
509 | /** | |
510 | * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks | |
511 | * @dev: Device to handle. | |
512 | */ | |
513 | static int __genpd_runtime_resume(struct device *dev) | |
514 | { | |
515 | int (*cb)(struct device *__dev); | |
516 | ||
517 | if (dev->type && dev->type->pm) | |
518 | cb = dev->type->pm->runtime_resume; | |
519 | else if (dev->class && dev->class->pm) | |
520 | cb = dev->class->pm->runtime_resume; | |
521 | else if (dev->bus && dev->bus->pm) | |
522 | cb = dev->bus->pm->runtime_resume; | |
523 | else | |
524 | cb = NULL; | |
525 | ||
526 | if (!cb && dev->driver && dev->driver->pm) | |
527 | cb = dev->driver->pm->runtime_resume; | |
528 | ||
529 | return cb ? cb(dev) : 0; | |
530 | } | |
531 | ||
f721889f | 532 | /** |
795bd2e7 | 533 | * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain. |
f721889f RW |
534 | * @dev: Device to suspend. |
535 | * | |
536 | * Carry out a runtime suspend of a device under the assumption that its | |
537 | * pm_domain field points to the domain member of an object of type | |
538 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. | |
539 | */ | |
795bd2e7 | 540 | static int genpd_runtime_suspend(struct device *dev) |
f721889f RW |
541 | { |
542 | struct generic_pm_domain *genpd; | |
9df3921e | 543 | bool (*suspend_ok)(struct device *__dev); |
2b1d88cd | 544 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
ffe12855 | 545 | bool runtime_pm = pm_runtime_enabled(dev); |
2b1d88cd UH |
546 | ktime_t time_start; |
547 | s64 elapsed_ns; | |
d5e4cbfe | 548 | int ret; |
f721889f RW |
549 | |
550 | dev_dbg(dev, "%s()\n", __func__); | |
551 | ||
5248051b RW |
552 | genpd = dev_to_genpd(dev); |
553 | if (IS_ERR(genpd)) | |
f721889f RW |
554 | return -EINVAL; |
555 | ||
ffe12855 UH |
556 | /* |
557 | * A runtime PM centric subsystem/driver may re-use the runtime PM | |
558 | * callbacks for other purposes than runtime PM. In those scenarios | |
559 | * runtime PM is disabled. Under these circumstances, we shall skip | |
560 | * validating/measuring the PM QoS latency. | |
561 | */ | |
9df3921e UH |
562 | suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL; |
563 | if (runtime_pm && suspend_ok && !suspend_ok(dev)) | |
b02c999a RW |
564 | return -EBUSY; |
565 | ||
2b1d88cd | 566 | /* Measure suspend latency. */ |
d33d5a6c | 567 | time_start = 0; |
ffe12855 UH |
568 | if (runtime_pm) |
569 | time_start = ktime_get(); | |
2b1d88cd | 570 | |
54eeddbf | 571 | ret = __genpd_runtime_suspend(dev); |
d5e4cbfe RW |
572 | if (ret) |
573 | return ret; | |
17b75eca | 574 | |
2b1d88cd | 575 | ret = genpd_stop_dev(genpd, dev); |
ba2bbfbf | 576 | if (ret) { |
54eeddbf | 577 | __genpd_runtime_resume(dev); |
ba2bbfbf UH |
578 | return ret; |
579 | } | |
580 | ||
2b1d88cd | 581 | /* Update suspend latency value if the measured time exceeds it. */ |
ffe12855 UH |
582 | if (runtime_pm) { |
583 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); | |
584 | if (elapsed_ns > td->suspend_latency_ns) { | |
585 | td->suspend_latency_ns = elapsed_ns; | |
586 | dev_dbg(dev, "suspend latency exceeded, %lld ns\n", | |
587 | elapsed_ns); | |
588 | genpd->max_off_time_changed = true; | |
589 | td->constraint_changed = true; | |
590 | } | |
2b1d88cd UH |
591 | } |
592 | ||
0aa2a221 | 593 | /* |
d716f479 LI |
594 | * If power.irq_safe is set, this routine may be run with |
595 | * IRQs disabled, so suspend only if the PM domain also is irq_safe. | |
0aa2a221 | 596 | */ |
d716f479 | 597 | if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) |
0aa2a221 RW |
598 | return 0; |
599 | ||
35241d12 | 600 | genpd_lock(genpd); |
2da83545 | 601 | genpd_power_off(genpd, true, 0); |
35241d12 | 602 | genpd_unlock(genpd); |
f721889f RW |
603 | |
604 | return 0; | |
605 | } | |
606 | ||
f721889f | 607 | /** |
795bd2e7 | 608 | * genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
f721889f RW |
609 | * @dev: Device to resume. |
610 | * | |
611 | * Carry out a runtime resume of a device under the assumption that its | |
612 | * pm_domain field points to the domain member of an object of type | |
613 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. | |
614 | */ | |
795bd2e7 | 615 | static int genpd_runtime_resume(struct device *dev) |
f721889f RW |
616 | { |
617 | struct generic_pm_domain *genpd; | |
2b1d88cd | 618 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
ffe12855 | 619 | bool runtime_pm = pm_runtime_enabled(dev); |
2b1d88cd UH |
620 | ktime_t time_start; |
621 | s64 elapsed_ns; | |
f721889f | 622 | int ret; |
ba2bbfbf | 623 | bool timed = true; |
f721889f RW |
624 | |
625 | dev_dbg(dev, "%s()\n", __func__); | |
626 | ||
5248051b RW |
627 | genpd = dev_to_genpd(dev); |
628 | if (IS_ERR(genpd)) | |
f721889f RW |
629 | return -EINVAL; |
630 | ||
d716f479 LI |
631 | /* |
632 | * As we don't power off a non IRQ safe domain, which holds | |
633 | * an IRQ safe device, we don't need to restore power to it. | |
634 | */ | |
635 | if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) { | |
ba2bbfbf UH |
636 | timed = false; |
637 | goto out; | |
638 | } | |
0aa2a221 | 639 | |
35241d12 | 640 | genpd_lock(genpd); |
86e12eac | 641 | ret = genpd_power_on(genpd, 0); |
35241d12 | 642 | genpd_unlock(genpd); |
c6d22b37 | 643 | |
ba2bbfbf UH |
644 | if (ret) |
645 | return ret; | |
c6d22b37 | 646 | |
ba2bbfbf | 647 | out: |
2b1d88cd | 648 | /* Measure resume latency. */ |
ab51e6ba | 649 | time_start = 0; |
ffe12855 | 650 | if (timed && runtime_pm) |
2b1d88cd UH |
651 | time_start = ktime_get(); |
652 | ||
076395ca LP |
653 | ret = genpd_start_dev(genpd, dev); |
654 | if (ret) | |
655 | goto err_poweroff; | |
656 | ||
54eeddbf | 657 | ret = __genpd_runtime_resume(dev); |
076395ca LP |
658 | if (ret) |
659 | goto err_stop; | |
2b1d88cd UH |
660 | |
661 | /* Update resume latency value if the measured time exceeds it. */ | |
ffe12855 | 662 | if (timed && runtime_pm) { |
2b1d88cd UH |
663 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
664 | if (elapsed_ns > td->resume_latency_ns) { | |
665 | td->resume_latency_ns = elapsed_ns; | |
666 | dev_dbg(dev, "resume latency exceeded, %lld ns\n", | |
667 | elapsed_ns); | |
668 | genpd->max_off_time_changed = true; | |
669 | td->constraint_changed = true; | |
670 | } | |
671 | } | |
17b75eca | 672 | |
f721889f | 673 | return 0; |
076395ca LP |
674 | |
675 | err_stop: | |
676 | genpd_stop_dev(genpd, dev); | |
677 | err_poweroff: | |
d716f479 LI |
678 | if (!pm_runtime_is_irq_safe(dev) || |
679 | (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) { | |
35241d12 | 680 | genpd_lock(genpd); |
2da83545 | 681 | genpd_power_off(genpd, true, 0); |
35241d12 | 682 | genpd_unlock(genpd); |
076395ca LP |
683 | } |
684 | ||
685 | return ret; | |
f721889f RW |
686 | } |
687 | ||
39ac5ba5 TB |
688 | static bool pd_ignore_unused; |
689 | static int __init pd_ignore_unused_setup(char *__unused) | |
690 | { | |
691 | pd_ignore_unused = true; | |
692 | return 1; | |
693 | } | |
694 | __setup("pd_ignore_unused", pd_ignore_unused_setup); | |
695 | ||
17f2ae7f | 696 | /** |
86e12eac | 697 | * genpd_power_off_unused - Power off all PM domains with no devices in use. |
17f2ae7f | 698 | */ |
86e12eac | 699 | static int __init genpd_power_off_unused(void) |
17f2ae7f RW |
700 | { |
701 | struct generic_pm_domain *genpd; | |
702 | ||
39ac5ba5 TB |
703 | if (pd_ignore_unused) { |
704 | pr_warn("genpd: Not disabling unused power domains\n"); | |
bb4b72fc | 705 | return 0; |
39ac5ba5 TB |
706 | } |
707 | ||
17f2ae7f RW |
708 | mutex_lock(&gpd_list_lock); |
709 | ||
710 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) | |
711 | genpd_queue_power_off_work(genpd); | |
712 | ||
713 | mutex_unlock(&gpd_list_lock); | |
17f2ae7f | 714 | |
2fe71dcd UH |
715 | return 0; |
716 | } | |
86e12eac | 717 | late_initcall(genpd_power_off_unused); |
2fe71dcd | 718 | |
0159ec67 | 719 | #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF) |
596ba34b | 720 | |
77f827de RW |
721 | /** |
722 | * pm_genpd_present - Check if the given PM domain has been initialized. | |
723 | * @genpd: PM domain to check. | |
724 | */ | |
895b31f3 | 725 | static bool pm_genpd_present(const struct generic_pm_domain *genpd) |
77f827de | 726 | { |
895b31f3 | 727 | const struct generic_pm_domain *gpd; |
77f827de RW |
728 | |
729 | if (IS_ERR_OR_NULL(genpd)) | |
730 | return false; | |
731 | ||
732 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) | |
733 | if (gpd == genpd) | |
734 | return true; | |
735 | ||
736 | return false; | |
737 | } | |
738 | ||
0159ec67 JH |
739 | #endif |
740 | ||
741 | #ifdef CONFIG_PM_SLEEP | |
742 | ||
d8600c8b | 743 | static bool genpd_dev_active_wakeup(const struct generic_pm_domain *genpd, |
d5e4cbfe RW |
744 | struct device *dev) |
745 | { | |
746 | return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev); | |
747 | } | |
748 | ||
596ba34b | 749 | /** |
86e12eac | 750 | * genpd_sync_power_off - Synchronously power off a PM domain and its masters. |
596ba34b | 751 | * @genpd: PM domain to power off, if possible. |
0883ac03 UH |
752 | * @use_lock: use the lock. |
753 | * @depth: nesting count for lockdep. | |
596ba34b RW |
754 | * |
755 | * Check if the given PM domain can be powered off (during system suspend or | |
5063ce15 | 756 | * hibernation) and do that if so. Also, in that case propagate to its masters. |
596ba34b | 757 | * |
77f827de | 758 | * This function is only called in "noirq" and "syscore" stages of system power |
0883ac03 UH |
759 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in |
760 | * these cases the lock must be held. | |
596ba34b | 761 | */ |
0883ac03 UH |
762 | static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock, |
763 | unsigned int depth) | |
596ba34b | 764 | { |
5063ce15 | 765 | struct gpd_link *link; |
596ba34b | 766 | |
ffaa42e8 | 767 | if (!genpd_status_on(genpd) || genpd_is_always_on(genpd)) |
596ba34b RW |
768 | return; |
769 | ||
c4bb3160 RW |
770 | if (genpd->suspended_count != genpd->device_count |
771 | || atomic_read(&genpd->sd_count) > 0) | |
596ba34b RW |
772 | return; |
773 | ||
fc5cbf0c AH |
774 | /* Choose the deepest state when suspending */ |
775 | genpd->state_idx = genpd->state_count - 1; | |
1c14967c UH |
776 | if (_genpd_power_off(genpd, false)) |
777 | return; | |
596ba34b | 778 | |
17b75eca | 779 | genpd->status = GPD_STATE_POWER_OFF; |
5063ce15 RW |
780 | |
781 | list_for_each_entry(link, &genpd->slave_links, slave_node) { | |
782 | genpd_sd_counter_dec(link->master); | |
0883ac03 UH |
783 | |
784 | if (use_lock) | |
785 | genpd_lock_nested(link->master, depth + 1); | |
786 | ||
787 | genpd_sync_power_off(link->master, use_lock, depth + 1); | |
788 | ||
789 | if (use_lock) | |
790 | genpd_unlock(link->master); | |
596ba34b RW |
791 | } |
792 | } | |
793 | ||
802d8b49 | 794 | /** |
86e12eac | 795 | * genpd_sync_power_on - Synchronously power on a PM domain and its masters. |
802d8b49 | 796 | * @genpd: PM domain to power on. |
0883ac03 UH |
797 | * @use_lock: use the lock. |
798 | * @depth: nesting count for lockdep. | |
802d8b49 | 799 | * |
77f827de | 800 | * This function is only called in "noirq" and "syscore" stages of system power |
0883ac03 UH |
801 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in |
802 | * these cases the lock must be held. | |
802d8b49 | 803 | */ |
0883ac03 UH |
804 | static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock, |
805 | unsigned int depth) | |
802d8b49 RW |
806 | { |
807 | struct gpd_link *link; | |
808 | ||
41e2c8e0 | 809 | if (genpd_status_on(genpd)) |
802d8b49 RW |
810 | return; |
811 | ||
812 | list_for_each_entry(link, &genpd->slave_links, slave_node) { | |
802d8b49 | 813 | genpd_sd_counter_inc(link->master); |
0883ac03 UH |
814 | |
815 | if (use_lock) | |
816 | genpd_lock_nested(link->master, depth + 1); | |
817 | ||
818 | genpd_sync_power_on(link->master, use_lock, depth + 1); | |
819 | ||
820 | if (use_lock) | |
821 | genpd_unlock(link->master); | |
802d8b49 RW |
822 | } |
823 | ||
86e12eac | 824 | _genpd_power_on(genpd, false); |
802d8b49 RW |
825 | |
826 | genpd->status = GPD_STATE_ACTIVE; | |
827 | } | |
828 | ||
4ecd6e65 RW |
829 | /** |
830 | * resume_needed - Check whether to resume a device before system suspend. | |
831 | * @dev: Device to check. | |
832 | * @genpd: PM domain the device belongs to. | |
833 | * | |
834 | * There are two cases in which a device that can wake up the system from sleep | |
835 | * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled | |
836 | * to wake up the system and it has to remain active for this purpose while the | |
837 | * system is in the sleep state and (2) if the device is not enabled to wake up | |
838 | * the system from sleep states and it generally doesn't generate wakeup signals | |
839 | * by itself (those signals are generated on its behalf by other parts of the | |
840 | * system). In the latter case it may be necessary to reconfigure the device's | |
841 | * wakeup settings during system suspend, because it may have been set up to | |
842 | * signal remote wakeup from the system's working state as needed by runtime PM. | |
843 | * Return 'true' in either of the above cases. | |
844 | */ | |
d8600c8b KK |
845 | static bool resume_needed(struct device *dev, |
846 | const struct generic_pm_domain *genpd) | |
4ecd6e65 RW |
847 | { |
848 | bool active_wakeup; | |
849 | ||
850 | if (!device_can_wakeup(dev)) | |
851 | return false; | |
852 | ||
d5e4cbfe | 853 | active_wakeup = genpd_dev_active_wakeup(genpd, dev); |
4ecd6e65 RW |
854 | return device_may_wakeup(dev) ? active_wakeup : !active_wakeup; |
855 | } | |
856 | ||
596ba34b RW |
857 | /** |
858 | * pm_genpd_prepare - Start power transition of a device in a PM domain. | |
859 | * @dev: Device to start the transition of. | |
860 | * | |
861 | * Start a power transition of a device (during a system-wide power transition) | |
862 | * under the assumption that its pm_domain field points to the domain member of | |
863 | * an object of type struct generic_pm_domain representing a PM domain | |
864 | * consisting of I/O devices. | |
865 | */ | |
866 | static int pm_genpd_prepare(struct device *dev) | |
867 | { | |
868 | struct generic_pm_domain *genpd; | |
b6c10c84 | 869 | int ret; |
596ba34b RW |
870 | |
871 | dev_dbg(dev, "%s()\n", __func__); | |
872 | ||
873 | genpd = dev_to_genpd(dev); | |
874 | if (IS_ERR(genpd)) | |
875 | return -EINVAL; | |
876 | ||
17b75eca RW |
877 | /* |
878 | * If a wakeup request is pending for the device, it should be woken up | |
879 | * at this point and a system wakeup event should be reported if it's | |
880 | * set up to wake up the system from sleep states. | |
881 | */ | |
4ecd6e65 RW |
882 | if (resume_needed(dev, genpd)) |
883 | pm_runtime_resume(dev); | |
884 | ||
35241d12 | 885 | genpd_lock(genpd); |
596ba34b | 886 | |
39dd0f23 | 887 | if (genpd->prepared_count++ == 0) |
65533bbf | 888 | genpd->suspended_count = 0; |
17b75eca | 889 | |
35241d12 | 890 | genpd_unlock(genpd); |
596ba34b | 891 | |
b6c10c84 RW |
892 | ret = pm_generic_prepare(dev); |
893 | if (ret) { | |
35241d12 | 894 | genpd_lock(genpd); |
b6c10c84 | 895 | |
39dd0f23 | 896 | genpd->prepared_count--; |
b6c10c84 | 897 | |
35241d12 | 898 | genpd_unlock(genpd); |
b6c10c84 | 899 | } |
17b75eca | 900 | |
b6c10c84 | 901 | return ret; |
596ba34b RW |
902 | } |
903 | ||
0496c8ae | 904 | /** |
10da6542 MP |
905 | * genpd_finish_suspend - Completion of suspend or hibernation of device in an |
906 | * I/O pm domain. | |
0496c8ae | 907 | * @dev: Device to suspend. |
10da6542 | 908 | * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback. |
0496c8ae RW |
909 | * |
910 | * Stop the device and remove power from the domain if all devices in it have | |
911 | * been stopped. | |
912 | */ | |
10da6542 | 913 | static int genpd_finish_suspend(struct device *dev, bool poweroff) |
0496c8ae RW |
914 | { |
915 | struct generic_pm_domain *genpd; | |
122a2237 | 916 | int ret; |
0496c8ae | 917 | |
0496c8ae RW |
918 | genpd = dev_to_genpd(dev); |
919 | if (IS_ERR(genpd)) | |
920 | return -EINVAL; | |
596ba34b | 921 | |
39dd0f23 | 922 | if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)) |
d4f2d87a RW |
923 | return 0; |
924 | ||
10da6542 MP |
925 | if (poweroff) |
926 | ret = pm_generic_poweroff_noirq(dev); | |
927 | else | |
928 | ret = pm_generic_suspend_noirq(dev); | |
929 | if (ret) | |
930 | return ret; | |
931 | ||
122a2237 UH |
932 | if (genpd->dev_ops.stop && genpd->dev_ops.start) { |
933 | ret = pm_runtime_force_suspend(dev); | |
934 | if (ret) | |
935 | return ret; | |
936 | } | |
937 | ||
0883ac03 | 938 | genpd_lock(genpd); |
596ba34b | 939 | genpd->suspended_count++; |
0883ac03 UH |
940 | genpd_sync_power_off(genpd, true, 0); |
941 | genpd_unlock(genpd); | |
596ba34b RW |
942 | |
943 | return 0; | |
944 | } | |
945 | ||
10da6542 MP |
946 | /** |
947 | * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain. | |
948 | * @dev: Device to suspend. | |
949 | * | |
950 | * Stop the device and remove power from the domain if all devices in it have | |
951 | * been stopped. | |
952 | */ | |
953 | static int pm_genpd_suspend_noirq(struct device *dev) | |
954 | { | |
955 | dev_dbg(dev, "%s()\n", __func__); | |
956 | ||
957 | return genpd_finish_suspend(dev, false); | |
958 | } | |
959 | ||
596ba34b | 960 | /** |
0496c8ae | 961 | * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain. |
596ba34b RW |
962 | * @dev: Device to resume. |
963 | * | |
0496c8ae | 964 | * Restore power to the device's PM domain, if necessary, and start the device. |
596ba34b RW |
965 | */ |
966 | static int pm_genpd_resume_noirq(struct device *dev) | |
967 | { | |
968 | struct generic_pm_domain *genpd; | |
122a2237 | 969 | int ret = 0; |
596ba34b RW |
970 | |
971 | dev_dbg(dev, "%s()\n", __func__); | |
972 | ||
973 | genpd = dev_to_genpd(dev); | |
974 | if (IS_ERR(genpd)) | |
975 | return -EINVAL; | |
976 | ||
39dd0f23 | 977 | if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)) |
596ba34b RW |
978 | return 0; |
979 | ||
0883ac03 UH |
980 | genpd_lock(genpd); |
981 | genpd_sync_power_on(genpd, true, 0); | |
596ba34b | 982 | genpd->suspended_count--; |
0883ac03 | 983 | genpd_unlock(genpd); |
596ba34b | 984 | |
122a2237 UH |
985 | if (genpd->dev_ops.stop && genpd->dev_ops.start) |
986 | ret = pm_runtime_force_resume(dev); | |
987 | ||
10da6542 MP |
988 | ret = pm_generic_resume_noirq(dev); |
989 | if (ret) | |
990 | return ret; | |
991 | ||
122a2237 | 992 | return ret; |
596ba34b RW |
993 | } |
994 | ||
0496c8ae RW |
995 | /** |
996 | * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain. | |
596ba34b RW |
997 | * @dev: Device to freeze. |
998 | * | |
999 | * Carry out a late freeze of a device under the assumption that its | |
1000 | * pm_domain field points to the domain member of an object of type | |
1001 | * struct generic_pm_domain representing a power domain consisting of I/O | |
1002 | * devices. | |
1003 | */ | |
1004 | static int pm_genpd_freeze_noirq(struct device *dev) | |
1005 | { | |
d8600c8b | 1006 | const struct generic_pm_domain *genpd; |
122a2237 | 1007 | int ret = 0; |
596ba34b RW |
1008 | |
1009 | dev_dbg(dev, "%s()\n", __func__); | |
1010 | ||
1011 | genpd = dev_to_genpd(dev); | |
1012 | if (IS_ERR(genpd)) | |
1013 | return -EINVAL; | |
1014 | ||
10da6542 MP |
1015 | ret = pm_generic_freeze_noirq(dev); |
1016 | if (ret) | |
1017 | return ret; | |
1018 | ||
122a2237 UH |
1019 | if (genpd->dev_ops.stop && genpd->dev_ops.start) |
1020 | ret = pm_runtime_force_suspend(dev); | |
1021 | ||
1022 | return ret; | |
0496c8ae | 1023 | } |
596ba34b | 1024 | |
0496c8ae RW |
1025 | /** |
1026 | * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain. | |
1027 | * @dev: Device to thaw. | |
1028 | * | |
1029 | * Start the device, unless power has been removed from the domain already | |
1030 | * before the system transition. | |
1031 | */ | |
1032 | static int pm_genpd_thaw_noirq(struct device *dev) | |
1033 | { | |
d8600c8b | 1034 | const struct generic_pm_domain *genpd; |
122a2237 | 1035 | int ret = 0; |
596ba34b | 1036 | |
0496c8ae | 1037 | dev_dbg(dev, "%s()\n", __func__); |
596ba34b | 1038 | |
0496c8ae RW |
1039 | genpd = dev_to_genpd(dev); |
1040 | if (IS_ERR(genpd)) | |
1041 | return -EINVAL; | |
1042 | ||
10da6542 | 1043 | if (genpd->dev_ops.stop && genpd->dev_ops.start) { |
122a2237 | 1044 | ret = pm_runtime_force_resume(dev); |
10da6542 MP |
1045 | if (ret) |
1046 | return ret; | |
1047 | } | |
122a2237 | 1048 | |
10da6542 MP |
1049 | return pm_generic_thaw_noirq(dev); |
1050 | } | |
1051 | ||
1052 | /** | |
1053 | * pm_genpd_poweroff_noirq - Completion of hibernation of device in an | |
1054 | * I/O PM domain. | |
1055 | * @dev: Device to poweroff. | |
1056 | * | |
1057 | * Stop the device and remove power from the domain if all devices in it have | |
1058 | * been stopped. | |
1059 | */ | |
1060 | static int pm_genpd_poweroff_noirq(struct device *dev) | |
1061 | { | |
1062 | dev_dbg(dev, "%s()\n", __func__); | |
1063 | ||
1064 | return genpd_finish_suspend(dev, true); | |
596ba34b RW |
1065 | } |
1066 | ||
596ba34b | 1067 | /** |
0496c8ae | 1068 | * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain. |
596ba34b RW |
1069 | * @dev: Device to resume. |
1070 | * | |
0496c8ae RW |
1071 | * Make sure the domain will be in the same power state as before the |
1072 | * hibernation the system is resuming from and start the device if necessary. | |
596ba34b RW |
1073 | */ |
1074 | static int pm_genpd_restore_noirq(struct device *dev) | |
1075 | { | |
1076 | struct generic_pm_domain *genpd; | |
122a2237 | 1077 | int ret = 0; |
596ba34b RW |
1078 | |
1079 | dev_dbg(dev, "%s()\n", __func__); | |
1080 | ||
1081 | genpd = dev_to_genpd(dev); | |
1082 | if (IS_ERR(genpd)) | |
1083 | return -EINVAL; | |
1084 | ||
1085 | /* | |
65533bbf RW |
1086 | * At this point suspended_count == 0 means we are being run for the |
1087 | * first time for the given domain in the present cycle. | |
596ba34b | 1088 | */ |
0883ac03 | 1089 | genpd_lock(genpd); |
39dd0f23 | 1090 | if (genpd->suspended_count++ == 0) |
596ba34b | 1091 | /* |
65533bbf | 1092 | * The boot kernel might put the domain into arbitrary state, |
86e12eac | 1093 | * so make it appear as powered off to genpd_sync_power_on(), |
802d8b49 | 1094 | * so that it tries to power it on in case it was really off. |
596ba34b | 1095 | */ |
65533bbf | 1096 | genpd->status = GPD_STATE_POWER_OFF; |
18dd2ece | 1097 | |
0883ac03 UH |
1098 | genpd_sync_power_on(genpd, true, 0); |
1099 | genpd_unlock(genpd); | |
596ba34b | 1100 | |
10da6542 | 1101 | if (genpd->dev_ops.stop && genpd->dev_ops.start) { |
122a2237 | 1102 | ret = pm_runtime_force_resume(dev); |
10da6542 MP |
1103 | if (ret) |
1104 | return ret; | |
1105 | } | |
122a2237 | 1106 | |
10da6542 | 1107 | return pm_generic_restore_noirq(dev); |
596ba34b RW |
1108 | } |
1109 | ||
1110 | /** | |
1111 | * pm_genpd_complete - Complete power transition of a device in a power domain. | |
1112 | * @dev: Device to complete the transition of. | |
1113 | * | |
1114 | * Complete a power transition of a device (during a system-wide power | |
1115 | * transition) under the assumption that its pm_domain field points to the | |
1116 | * domain member of an object of type struct generic_pm_domain representing | |
1117 | * a power domain consisting of I/O devices. | |
1118 | */ | |
1119 | static void pm_genpd_complete(struct device *dev) | |
1120 | { | |
1121 | struct generic_pm_domain *genpd; | |
596ba34b RW |
1122 | |
1123 | dev_dbg(dev, "%s()\n", __func__); | |
1124 | ||
1125 | genpd = dev_to_genpd(dev); | |
1126 | if (IS_ERR(genpd)) | |
1127 | return; | |
1128 | ||
4d23a5e8 UH |
1129 | pm_generic_complete(dev); |
1130 | ||
35241d12 | 1131 | genpd_lock(genpd); |
596ba34b | 1132 | |
39dd0f23 | 1133 | genpd->prepared_count--; |
4d23a5e8 UH |
1134 | if (!genpd->prepared_count) |
1135 | genpd_queue_power_off_work(genpd); | |
596ba34b | 1136 | |
35241d12 | 1137 | genpd_unlock(genpd); |
596ba34b RW |
1138 | } |
1139 | ||
77f827de | 1140 | /** |
d47e6464 | 1141 | * genpd_syscore_switch - Switch power during system core suspend or resume. |
77f827de RW |
1142 | * @dev: Device that normally is marked as "always on" to switch power for. |
1143 | * | |
1144 | * This routine may only be called during the system core (syscore) suspend or | |
1145 | * resume phase for devices whose "always on" flags are set. | |
1146 | */ | |
d47e6464 | 1147 | static void genpd_syscore_switch(struct device *dev, bool suspend) |
77f827de RW |
1148 | { |
1149 | struct generic_pm_domain *genpd; | |
1150 | ||
8b55e55e KK |
1151 | genpd = genpd_lookup_dev(dev); |
1152 | if (!genpd) | |
77f827de RW |
1153 | return; |
1154 | ||
1155 | if (suspend) { | |
1156 | genpd->suspended_count++; | |
0883ac03 | 1157 | genpd_sync_power_off(genpd, false, 0); |
77f827de | 1158 | } else { |
0883ac03 | 1159 | genpd_sync_power_on(genpd, false, 0); |
77f827de RW |
1160 | genpd->suspended_count--; |
1161 | } | |
1162 | } | |
d47e6464 UH |
1163 | |
1164 | void pm_genpd_syscore_poweroff(struct device *dev) | |
1165 | { | |
1166 | genpd_syscore_switch(dev, true); | |
1167 | } | |
1168 | EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff); | |
1169 | ||
1170 | void pm_genpd_syscore_poweron(struct device *dev) | |
1171 | { | |
1172 | genpd_syscore_switch(dev, false); | |
1173 | } | |
1174 | EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron); | |
77f827de | 1175 | |
d30d819d | 1176 | #else /* !CONFIG_PM_SLEEP */ |
596ba34b RW |
1177 | |
1178 | #define pm_genpd_prepare NULL | |
596ba34b RW |
1179 | #define pm_genpd_suspend_noirq NULL |
1180 | #define pm_genpd_resume_noirq NULL | |
596ba34b RW |
1181 | #define pm_genpd_freeze_noirq NULL |
1182 | #define pm_genpd_thaw_noirq NULL | |
596ba34b | 1183 | #define pm_genpd_restore_noirq NULL |
596ba34b RW |
1184 | #define pm_genpd_complete NULL |
1185 | ||
1186 | #endif /* CONFIG_PM_SLEEP */ | |
1187 | ||
f104e1e5 UH |
1188 | static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev, |
1189 | struct generic_pm_domain *genpd, | |
1190 | struct gpd_timing_data *td) | |
1d5fcfec RW |
1191 | { |
1192 | struct generic_pm_domain_data *gpd_data; | |
3e235685 UH |
1193 | int ret; |
1194 | ||
1195 | ret = dev_pm_get_subsys_data(dev); | |
1196 | if (ret) | |
1197 | return ERR_PTR(ret); | |
1d5fcfec RW |
1198 | |
1199 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); | |
3e235685 UH |
1200 | if (!gpd_data) { |
1201 | ret = -ENOMEM; | |
1202 | goto err_put; | |
1203 | } | |
1d5fcfec | 1204 | |
f104e1e5 UH |
1205 | if (td) |
1206 | gpd_data->td = *td; | |
1207 | ||
1208 | gpd_data->base.dev = dev; | |
f104e1e5 UH |
1209 | gpd_data->td.constraint_changed = true; |
1210 | gpd_data->td.effective_constraint_ns = -1; | |
1211 | gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier; | |
1212 | ||
1213 | spin_lock_irq(&dev->power.lock); | |
1214 | ||
1215 | if (dev->power.subsys_data->domain_data) { | |
1216 | ret = -EINVAL; | |
1217 | goto err_free; | |
1218 | } | |
1219 | ||
1220 | dev->power.subsys_data->domain_data = &gpd_data->base; | |
f104e1e5 UH |
1221 | |
1222 | spin_unlock_irq(&dev->power.lock); | |
1223 | ||
989561de TV |
1224 | dev_pm_domain_set(dev, &genpd->domain); |
1225 | ||
1d5fcfec | 1226 | return gpd_data; |
3e235685 | 1227 | |
f104e1e5 UH |
1228 | err_free: |
1229 | spin_unlock_irq(&dev->power.lock); | |
1230 | kfree(gpd_data); | |
3e235685 UH |
1231 | err_put: |
1232 | dev_pm_put_subsys_data(dev); | |
1233 | return ERR_PTR(ret); | |
1d5fcfec RW |
1234 | } |
1235 | ||
49d400c7 UH |
1236 | static void genpd_free_dev_data(struct device *dev, |
1237 | struct generic_pm_domain_data *gpd_data) | |
1d5fcfec | 1238 | { |
989561de TV |
1239 | dev_pm_domain_set(dev, NULL); |
1240 | ||
f104e1e5 UH |
1241 | spin_lock_irq(&dev->power.lock); |
1242 | ||
f104e1e5 UH |
1243 | dev->power.subsys_data->domain_data = NULL; |
1244 | ||
1245 | spin_unlock_irq(&dev->power.lock); | |
1246 | ||
1d5fcfec | 1247 | kfree(gpd_data); |
3e235685 | 1248 | dev_pm_put_subsys_data(dev); |
1d5fcfec RW |
1249 | } |
1250 | ||
19efa5ff JH |
1251 | static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, |
1252 | struct gpd_timing_data *td) | |
f721889f | 1253 | { |
c0356db7 | 1254 | struct generic_pm_domain_data *gpd_data; |
f721889f RW |
1255 | int ret = 0; |
1256 | ||
1257 | dev_dbg(dev, "%s()\n", __func__); | |
1258 | ||
1259 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) | |
1260 | return -EINVAL; | |
1261 | ||
f104e1e5 | 1262 | gpd_data = genpd_alloc_dev_data(dev, genpd, td); |
3e235685 UH |
1263 | if (IS_ERR(gpd_data)) |
1264 | return PTR_ERR(gpd_data); | |
6ff7bb0d | 1265 | |
35241d12 | 1266 | genpd_lock(genpd); |
f721889f | 1267 | |
596ba34b RW |
1268 | if (genpd->prepared_count > 0) { |
1269 | ret = -EAGAIN; | |
1270 | goto out; | |
1271 | } | |
1272 | ||
b472c2fa UH |
1273 | ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0; |
1274 | if (ret) | |
1275 | goto out; | |
d79b6fe1 | 1276 | |
14b53064 UH |
1277 | genpd->device_count++; |
1278 | genpd->max_off_time_changed = true; | |
1279 | ||
1d5fcfec | 1280 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
6ff7bb0d | 1281 | |
f721889f | 1282 | out: |
35241d12 | 1283 | genpd_unlock(genpd); |
f721889f | 1284 | |
c0356db7 UH |
1285 | if (ret) |
1286 | genpd_free_dev_data(dev, gpd_data); | |
1287 | else | |
1288 | dev_pm_qos_add_notifier(dev, &gpd_data->nb); | |
1d5fcfec | 1289 | |
f721889f RW |
1290 | return ret; |
1291 | } | |
19efa5ff JH |
1292 | |
1293 | /** | |
1294 | * __pm_genpd_add_device - Add a device to an I/O PM domain. | |
1295 | * @genpd: PM domain to add the device to. | |
1296 | * @dev: Device to be added. | |
1297 | * @td: Set of PM QoS timing parameters to attach to the device. | |
1298 | */ | |
1299 | int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, | |
1300 | struct gpd_timing_data *td) | |
1301 | { | |
1302 | int ret; | |
1303 | ||
1304 | mutex_lock(&gpd_list_lock); | |
1305 | ret = genpd_add_device(genpd, dev, td); | |
1306 | mutex_unlock(&gpd_list_lock); | |
1307 | ||
1308 | return ret; | |
1309 | } | |
24c96dc7 | 1310 | EXPORT_SYMBOL_GPL(__pm_genpd_add_device); |
f721889f | 1311 | |
85168d56 UH |
1312 | static int genpd_remove_device(struct generic_pm_domain *genpd, |
1313 | struct device *dev) | |
f721889f | 1314 | { |
6ff7bb0d | 1315 | struct generic_pm_domain_data *gpd_data; |
4605ab65 | 1316 | struct pm_domain_data *pdd; |
efa69025 | 1317 | int ret = 0; |
f721889f RW |
1318 | |
1319 | dev_dbg(dev, "%s()\n", __func__); | |
1320 | ||
c0356db7 UH |
1321 | pdd = dev->power.subsys_data->domain_data; |
1322 | gpd_data = to_gpd_data(pdd); | |
1323 | dev_pm_qos_remove_notifier(dev, &gpd_data->nb); | |
1324 | ||
35241d12 | 1325 | genpd_lock(genpd); |
f721889f | 1326 | |
596ba34b RW |
1327 | if (genpd->prepared_count > 0) { |
1328 | ret = -EAGAIN; | |
1329 | goto out; | |
1330 | } | |
1331 | ||
6ff7bb0d RW |
1332 | genpd->device_count--; |
1333 | genpd->max_off_time_changed = true; | |
1334 | ||
d79b6fe1 | 1335 | if (genpd->detach_dev) |
c16561e8 | 1336 | genpd->detach_dev(genpd, dev); |
d79b6fe1 | 1337 | |
efa69025 | 1338 | list_del_init(&pdd->list_node); |
6ff7bb0d | 1339 | |
35241d12 | 1340 | genpd_unlock(genpd); |
6ff7bb0d | 1341 | |
c1dbe2fb | 1342 | genpd_free_dev_data(dev, gpd_data); |
1d5fcfec | 1343 | |
6ff7bb0d | 1344 | return 0; |
f721889f | 1345 | |
596ba34b | 1346 | out: |
35241d12 | 1347 | genpd_unlock(genpd); |
c0356db7 | 1348 | dev_pm_qos_add_notifier(dev, &gpd_data->nb); |
f721889f RW |
1349 | |
1350 | return ret; | |
1351 | } | |
85168d56 UH |
1352 | |
1353 | /** | |
1354 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. | |
1355 | * @genpd: PM domain to remove the device from. | |
1356 | * @dev: Device to be removed. | |
1357 | */ | |
1358 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, | |
1359 | struct device *dev) | |
1360 | { | |
1361 | if (!genpd || genpd != genpd_lookup_dev(dev)) | |
1362 | return -EINVAL; | |
1363 | ||
1364 | return genpd_remove_device(genpd, dev); | |
1365 | } | |
24c96dc7 | 1366 | EXPORT_SYMBOL_GPL(pm_genpd_remove_device); |
f721889f | 1367 | |
19efa5ff JH |
1368 | static int genpd_add_subdomain(struct generic_pm_domain *genpd, |
1369 | struct generic_pm_domain *subdomain) | |
f721889f | 1370 | { |
2547923d | 1371 | struct gpd_link *link, *itr; |
f721889f RW |
1372 | int ret = 0; |
1373 | ||
fb7268be RW |
1374 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain) |
1375 | || genpd == subdomain) | |
f721889f RW |
1376 | return -EINVAL; |
1377 | ||
d716f479 LI |
1378 | /* |
1379 | * If the domain can be powered on/off in an IRQ safe | |
1380 | * context, ensure that the subdomain can also be | |
1381 | * powered on/off in that context. | |
1382 | */ | |
1383 | if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) { | |
44cae7d5 | 1384 | WARN(1, "Parent %s of subdomain %s must be IRQ safe\n", |
d716f479 LI |
1385 | genpd->name, subdomain->name); |
1386 | return -EINVAL; | |
1387 | } | |
1388 | ||
2547923d LI |
1389 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
1390 | if (!link) | |
1391 | return -ENOMEM; | |
1392 | ||
35241d12 LI |
1393 | genpd_lock(subdomain); |
1394 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING); | |
f721889f | 1395 | |
41e2c8e0 | 1396 | if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) { |
f721889f RW |
1397 | ret = -EINVAL; |
1398 | goto out; | |
1399 | } | |
1400 | ||
2547923d LI |
1401 | list_for_each_entry(itr, &genpd->master_links, master_node) { |
1402 | if (itr->slave == subdomain && itr->master == genpd) { | |
f721889f RW |
1403 | ret = -EINVAL; |
1404 | goto out; | |
1405 | } | |
1406 | } | |
1407 | ||
5063ce15 RW |
1408 | link->master = genpd; |
1409 | list_add_tail(&link->master_node, &genpd->master_links); | |
bc0403ff RW |
1410 | link->slave = subdomain; |
1411 | list_add_tail(&link->slave_node, &subdomain->slave_links); | |
41e2c8e0 | 1412 | if (genpd_status_on(subdomain)) |
c4bb3160 | 1413 | genpd_sd_counter_inc(genpd); |
f721889f | 1414 | |
f721889f | 1415 | out: |
35241d12 LI |
1416 | genpd_unlock(genpd); |
1417 | genpd_unlock(subdomain); | |
2547923d LI |
1418 | if (ret) |
1419 | kfree(link); | |
f721889f RW |
1420 | return ret; |
1421 | } | |
19efa5ff JH |
1422 | |
1423 | /** | |
1424 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. | |
1425 | * @genpd: Master PM domain to add the subdomain to. | |
1426 | * @subdomain: Subdomain to be added. | |
1427 | */ | |
1428 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, | |
1429 | struct generic_pm_domain *subdomain) | |
1430 | { | |
1431 | int ret; | |
1432 | ||
1433 | mutex_lock(&gpd_list_lock); | |
1434 | ret = genpd_add_subdomain(genpd, subdomain); | |
1435 | mutex_unlock(&gpd_list_lock); | |
1436 | ||
1437 | return ret; | |
1438 | } | |
d60ee966 | 1439 | EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain); |
f721889f RW |
1440 | |
1441 | /** | |
1442 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. | |
1443 | * @genpd: Master PM domain to remove the subdomain from. | |
5063ce15 | 1444 | * @subdomain: Subdomain to be removed. |
f721889f RW |
1445 | */ |
1446 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, | |
5063ce15 | 1447 | struct generic_pm_domain *subdomain) |
f721889f | 1448 | { |
c6e83cac | 1449 | struct gpd_link *l, *link; |
f721889f RW |
1450 | int ret = -EINVAL; |
1451 | ||
5063ce15 | 1452 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
f721889f RW |
1453 | return -EINVAL; |
1454 | ||
35241d12 LI |
1455 | genpd_lock(subdomain); |
1456 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING); | |
f721889f | 1457 | |
beda5fc1 | 1458 | if (!list_empty(&subdomain->master_links) || subdomain->device_count) { |
30e7a65b JH |
1459 | pr_warn("%s: unable to remove subdomain %s\n", genpd->name, |
1460 | subdomain->name); | |
1461 | ret = -EBUSY; | |
1462 | goto out; | |
1463 | } | |
1464 | ||
c6e83cac | 1465 | list_for_each_entry_safe(link, l, &genpd->master_links, master_node) { |
5063ce15 | 1466 | if (link->slave != subdomain) |
f721889f RW |
1467 | continue; |
1468 | ||
5063ce15 RW |
1469 | list_del(&link->master_node); |
1470 | list_del(&link->slave_node); | |
1471 | kfree(link); | |
41e2c8e0 | 1472 | if (genpd_status_on(subdomain)) |
f721889f RW |
1473 | genpd_sd_counter_dec(genpd); |
1474 | ||
f721889f RW |
1475 | ret = 0; |
1476 | break; | |
1477 | } | |
1478 | ||
30e7a65b | 1479 | out: |
35241d12 LI |
1480 | genpd_unlock(genpd); |
1481 | genpd_unlock(subdomain); | |
f721889f RW |
1482 | |
1483 | return ret; | |
1484 | } | |
d60ee966 | 1485 | EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain); |
f721889f | 1486 | |
59d65b73 LI |
1487 | static int genpd_set_default_power_state(struct generic_pm_domain *genpd) |
1488 | { | |
1489 | struct genpd_power_state *state; | |
1490 | ||
1491 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
1492 | if (!state) | |
1493 | return -ENOMEM; | |
1494 | ||
1495 | genpd->states = state; | |
1496 | genpd->state_count = 1; | |
1497 | genpd->free = state; | |
1498 | ||
1499 | return 0; | |
1500 | } | |
1501 | ||
d716f479 LI |
1502 | static void genpd_lock_init(struct generic_pm_domain *genpd) |
1503 | { | |
1504 | if (genpd->flags & GENPD_FLAG_IRQ_SAFE) { | |
1505 | spin_lock_init(&genpd->slock); | |
1506 | genpd->lock_ops = &genpd_spin_ops; | |
1507 | } else { | |
1508 | mutex_init(&genpd->mlock); | |
1509 | genpd->lock_ops = &genpd_mtx_ops; | |
1510 | } | |
1511 | } | |
1512 | ||
f721889f RW |
1513 | /** |
1514 | * pm_genpd_init - Initialize a generic I/O PM domain object. | |
1515 | * @genpd: PM domain object to initialize. | |
1516 | * @gov: PM domain governor to associate with the domain (may be NULL). | |
1517 | * @is_off: Initial value of the domain's power_is_off field. | |
7eb231c3 UH |
1518 | * |
1519 | * Returns 0 on successful initialization, else a negative error code. | |
f721889f | 1520 | */ |
7eb231c3 UH |
1521 | int pm_genpd_init(struct generic_pm_domain *genpd, |
1522 | struct dev_power_governor *gov, bool is_off) | |
f721889f | 1523 | { |
59d65b73 LI |
1524 | int ret; |
1525 | ||
f721889f | 1526 | if (IS_ERR_OR_NULL(genpd)) |
7eb231c3 | 1527 | return -EINVAL; |
f721889f | 1528 | |
5063ce15 RW |
1529 | INIT_LIST_HEAD(&genpd->master_links); |
1530 | INIT_LIST_HEAD(&genpd->slave_links); | |
f721889f | 1531 | INIT_LIST_HEAD(&genpd->dev_list); |
d716f479 | 1532 | genpd_lock_init(genpd); |
f721889f RW |
1533 | genpd->gov = gov; |
1534 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); | |
c4bb3160 | 1535 | atomic_set(&genpd->sd_count, 0); |
17b75eca | 1536 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
596ba34b | 1537 | genpd->device_count = 0; |
221e9b58 | 1538 | genpd->max_off_time_ns = -1; |
6ff7bb0d | 1539 | genpd->max_off_time_changed = true; |
de0aa06d JH |
1540 | genpd->provider = NULL; |
1541 | genpd->has_provider = false; | |
795bd2e7 UH |
1542 | genpd->domain.ops.runtime_suspend = genpd_runtime_suspend; |
1543 | genpd->domain.ops.runtime_resume = genpd_runtime_resume; | |
596ba34b | 1544 | genpd->domain.ops.prepare = pm_genpd_prepare; |
596ba34b RW |
1545 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
1546 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; | |
596ba34b RW |
1547 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
1548 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; | |
10da6542 | 1549 | genpd->domain.ops.poweroff_noirq = pm_genpd_poweroff_noirq; |
596ba34b | 1550 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
596ba34b | 1551 | genpd->domain.ops.complete = pm_genpd_complete; |
c11f6f5b UH |
1552 | |
1553 | if (genpd->flags & GENPD_FLAG_PM_CLK) { | |
1554 | genpd->dev_ops.stop = pm_clk_suspend; | |
1555 | genpd->dev_ops.start = pm_clk_resume; | |
1556 | } | |
1557 | ||
ffaa42e8 UH |
1558 | /* Always-on domains must be powered on at initialization. */ |
1559 | if (genpd_is_always_on(genpd) && !genpd_status_on(genpd)) | |
1560 | return -EINVAL; | |
1561 | ||
fc5cbf0c | 1562 | /* Use only one "off" state if there were no states declared */ |
59d65b73 LI |
1563 | if (genpd->state_count == 0) { |
1564 | ret = genpd_set_default_power_state(genpd); | |
1565 | if (ret) | |
1566 | return ret; | |
1567 | } | |
fc5cbf0c | 1568 | |
5125bbf3 RW |
1569 | mutex_lock(&gpd_list_lock); |
1570 | list_add(&genpd->gpd_list_node, &gpd_list); | |
1571 | mutex_unlock(&gpd_list_lock); | |
7eb231c3 UH |
1572 | |
1573 | return 0; | |
5125bbf3 | 1574 | } |
be5ed55d | 1575 | EXPORT_SYMBOL_GPL(pm_genpd_init); |
aa42240a | 1576 | |
3fe57710 JH |
1577 | static int genpd_remove(struct generic_pm_domain *genpd) |
1578 | { | |
1579 | struct gpd_link *l, *link; | |
1580 | ||
1581 | if (IS_ERR_OR_NULL(genpd)) | |
1582 | return -EINVAL; | |
1583 | ||
35241d12 | 1584 | genpd_lock(genpd); |
3fe57710 JH |
1585 | |
1586 | if (genpd->has_provider) { | |
35241d12 | 1587 | genpd_unlock(genpd); |
3fe57710 JH |
1588 | pr_err("Provider present, unable to remove %s\n", genpd->name); |
1589 | return -EBUSY; | |
1590 | } | |
1591 | ||
1592 | if (!list_empty(&genpd->master_links) || genpd->device_count) { | |
35241d12 | 1593 | genpd_unlock(genpd); |
3fe57710 JH |
1594 | pr_err("%s: unable to remove %s\n", __func__, genpd->name); |
1595 | return -EBUSY; | |
1596 | } | |
1597 | ||
1598 | list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) { | |
1599 | list_del(&link->master_node); | |
1600 | list_del(&link->slave_node); | |
1601 | kfree(link); | |
1602 | } | |
1603 | ||
1604 | list_del(&genpd->gpd_list_node); | |
35241d12 | 1605 | genpd_unlock(genpd); |
3fe57710 | 1606 | cancel_work_sync(&genpd->power_off_work); |
59d65b73 | 1607 | kfree(genpd->free); |
3fe57710 JH |
1608 | pr_debug("%s: removed %s\n", __func__, genpd->name); |
1609 | ||
1610 | return 0; | |
1611 | } | |
1612 | ||
1613 | /** | |
1614 | * pm_genpd_remove - Remove a generic I/O PM domain | |
1615 | * @genpd: Pointer to PM domain that is to be removed. | |
1616 | * | |
1617 | * To remove the PM domain, this function: | |
1618 | * - Removes the PM domain as a subdomain to any parent domains, | |
1619 | * if it was added. | |
1620 | * - Removes the PM domain from the list of registered PM domains. | |
1621 | * | |
1622 | * The PM domain will only be removed, if the associated provider has | |
1623 | * been removed, it is not a parent to any other PM domain and has no | |
1624 | * devices associated with it. | |
1625 | */ | |
1626 | int pm_genpd_remove(struct generic_pm_domain *genpd) | |
1627 | { | |
1628 | int ret; | |
1629 | ||
1630 | mutex_lock(&gpd_list_lock); | |
1631 | ret = genpd_remove(genpd); | |
1632 | mutex_unlock(&gpd_list_lock); | |
1633 | ||
1634 | return ret; | |
1635 | } | |
1636 | EXPORT_SYMBOL_GPL(pm_genpd_remove); | |
1637 | ||
aa42240a | 1638 | #ifdef CONFIG_PM_GENERIC_DOMAINS_OF |
892ebdcc JH |
1639 | |
1640 | typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args, | |
1641 | void *data); | |
1642 | ||
aa42240a TF |
1643 | /* |
1644 | * Device Tree based PM domain providers. | |
1645 | * | |
1646 | * The code below implements generic device tree based PM domain providers that | |
1647 | * bind device tree nodes with generic PM domains registered in the system. | |
1648 | * | |
1649 | * Any driver that registers generic PM domains and needs to support binding of | |
1650 | * devices to these domains is supposed to register a PM domain provider, which | |
1651 | * maps a PM domain specifier retrieved from the device tree to a PM domain. | |
1652 | * | |
1653 | * Two simple mapping functions have been provided for convenience: | |
892ebdcc JH |
1654 | * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping. |
1655 | * - genpd_xlate_onecell() for mapping of multiple PM domains per node by | |
aa42240a TF |
1656 | * index. |
1657 | */ | |
1658 | ||
1659 | /** | |
1660 | * struct of_genpd_provider - PM domain provider registration structure | |
1661 | * @link: Entry in global list of PM domain providers | |
1662 | * @node: Pointer to device tree node of PM domain provider | |
1663 | * @xlate: Provider-specific xlate callback mapping a set of specifier cells | |
1664 | * into a PM domain. | |
1665 | * @data: context pointer to be passed into @xlate callback | |
1666 | */ | |
1667 | struct of_genpd_provider { | |
1668 | struct list_head link; | |
1669 | struct device_node *node; | |
1670 | genpd_xlate_t xlate; | |
1671 | void *data; | |
1672 | }; | |
1673 | ||
1674 | /* List of registered PM domain providers. */ | |
1675 | static LIST_HEAD(of_genpd_providers); | |
1676 | /* Mutex to protect the list above. */ | |
1677 | static DEFINE_MUTEX(of_genpd_mutex); | |
1678 | ||
1679 | /** | |
892ebdcc | 1680 | * genpd_xlate_simple() - Xlate function for direct node-domain mapping |
aa42240a TF |
1681 | * @genpdspec: OF phandle args to map into a PM domain |
1682 | * @data: xlate function private data - pointer to struct generic_pm_domain | |
1683 | * | |
1684 | * This is a generic xlate function that can be used to model PM domains that | |
1685 | * have their own device tree nodes. The private data of xlate function needs | |
1686 | * to be a valid pointer to struct generic_pm_domain. | |
1687 | */ | |
892ebdcc | 1688 | static struct generic_pm_domain *genpd_xlate_simple( |
aa42240a TF |
1689 | struct of_phandle_args *genpdspec, |
1690 | void *data) | |
1691 | { | |
1692 | if (genpdspec->args_count != 0) | |
1693 | return ERR_PTR(-EINVAL); | |
1694 | return data; | |
1695 | } | |
aa42240a TF |
1696 | |
1697 | /** | |
892ebdcc | 1698 | * genpd_xlate_onecell() - Xlate function using a single index. |
aa42240a TF |
1699 | * @genpdspec: OF phandle args to map into a PM domain |
1700 | * @data: xlate function private data - pointer to struct genpd_onecell_data | |
1701 | * | |
1702 | * This is a generic xlate function that can be used to model simple PM domain | |
1703 | * controllers that have one device tree node and provide multiple PM domains. | |
1704 | * A single cell is used as an index into an array of PM domains specified in | |
1705 | * the genpd_onecell_data struct when registering the provider. | |
1706 | */ | |
892ebdcc | 1707 | static struct generic_pm_domain *genpd_xlate_onecell( |
aa42240a TF |
1708 | struct of_phandle_args *genpdspec, |
1709 | void *data) | |
1710 | { | |
1711 | struct genpd_onecell_data *genpd_data = data; | |
1712 | unsigned int idx = genpdspec->args[0]; | |
1713 | ||
1714 | if (genpdspec->args_count != 1) | |
1715 | return ERR_PTR(-EINVAL); | |
1716 | ||
1717 | if (idx >= genpd_data->num_domains) { | |
1718 | pr_err("%s: invalid domain index %u\n", __func__, idx); | |
1719 | return ERR_PTR(-EINVAL); | |
1720 | } | |
1721 | ||
1722 | if (!genpd_data->domains[idx]) | |
1723 | return ERR_PTR(-ENOENT); | |
1724 | ||
1725 | return genpd_data->domains[idx]; | |
1726 | } | |
aa42240a TF |
1727 | |
1728 | /** | |
892ebdcc | 1729 | * genpd_add_provider() - Register a PM domain provider for a node |
aa42240a TF |
1730 | * @np: Device node pointer associated with the PM domain provider. |
1731 | * @xlate: Callback for decoding PM domain from phandle arguments. | |
1732 | * @data: Context pointer for @xlate callback. | |
1733 | */ | |
892ebdcc JH |
1734 | static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate, |
1735 | void *data) | |
aa42240a TF |
1736 | { |
1737 | struct of_genpd_provider *cp; | |
1738 | ||
1739 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); | |
1740 | if (!cp) | |
1741 | return -ENOMEM; | |
1742 | ||
1743 | cp->node = of_node_get(np); | |
1744 | cp->data = data; | |
1745 | cp->xlate = xlate; | |
1746 | ||
1747 | mutex_lock(&of_genpd_mutex); | |
1748 | list_add(&cp->link, &of_genpd_providers); | |
1749 | mutex_unlock(&of_genpd_mutex); | |
1750 | pr_debug("Added domain provider from %s\n", np->full_name); | |
1751 | ||
1752 | return 0; | |
1753 | } | |
892ebdcc JH |
1754 | |
1755 | /** | |
1756 | * of_genpd_add_provider_simple() - Register a simple PM domain provider | |
1757 | * @np: Device node pointer associated with the PM domain provider. | |
1758 | * @genpd: Pointer to PM domain associated with the PM domain provider. | |
1759 | */ | |
1760 | int of_genpd_add_provider_simple(struct device_node *np, | |
1761 | struct generic_pm_domain *genpd) | |
1762 | { | |
0159ec67 JH |
1763 | int ret = -EINVAL; |
1764 | ||
1765 | if (!np || !genpd) | |
1766 | return -EINVAL; | |
1767 | ||
1768 | mutex_lock(&gpd_list_lock); | |
1769 | ||
8ce95844 | 1770 | if (pm_genpd_present(genpd)) { |
0159ec67 | 1771 | ret = genpd_add_provider(np, genpd_xlate_simple, genpd); |
8ce95844 VK |
1772 | if (!ret) { |
1773 | genpd->provider = &np->fwnode; | |
1774 | genpd->has_provider = true; | |
1775 | } | |
de0aa06d JH |
1776 | } |
1777 | ||
0159ec67 JH |
1778 | mutex_unlock(&gpd_list_lock); |
1779 | ||
1780 | return ret; | |
892ebdcc JH |
1781 | } |
1782 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple); | |
1783 | ||
1784 | /** | |
1785 | * of_genpd_add_provider_onecell() - Register a onecell PM domain provider | |
1786 | * @np: Device node pointer associated with the PM domain provider. | |
1787 | * @data: Pointer to the data associated with the PM domain provider. | |
1788 | */ | |
1789 | int of_genpd_add_provider_onecell(struct device_node *np, | |
1790 | struct genpd_onecell_data *data) | |
1791 | { | |
0159ec67 | 1792 | unsigned int i; |
de0aa06d | 1793 | int ret = -EINVAL; |
0159ec67 JH |
1794 | |
1795 | if (!np || !data) | |
1796 | return -EINVAL; | |
1797 | ||
1798 | mutex_lock(&gpd_list_lock); | |
1799 | ||
1800 | for (i = 0; i < data->num_domains; i++) { | |
609bed67 TV |
1801 | if (!data->domains[i]) |
1802 | continue; | |
de0aa06d JH |
1803 | if (!pm_genpd_present(data->domains[i])) |
1804 | goto error; | |
1805 | ||
1806 | data->domains[i]->provider = &np->fwnode; | |
1807 | data->domains[i]->has_provider = true; | |
0159ec67 JH |
1808 | } |
1809 | ||
1810 | ret = genpd_add_provider(np, genpd_xlate_onecell, data); | |
de0aa06d JH |
1811 | if (ret < 0) |
1812 | goto error; | |
1813 | ||
1814 | mutex_unlock(&gpd_list_lock); | |
1815 | ||
1816 | return 0; | |
1817 | ||
1818 | error: | |
1819 | while (i--) { | |
609bed67 TV |
1820 | if (!data->domains[i]) |
1821 | continue; | |
de0aa06d JH |
1822 | data->domains[i]->provider = NULL; |
1823 | data->domains[i]->has_provider = false; | |
1824 | } | |
0159ec67 JH |
1825 | |
1826 | mutex_unlock(&gpd_list_lock); | |
1827 | ||
1828 | return ret; | |
892ebdcc JH |
1829 | } |
1830 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell); | |
aa42240a TF |
1831 | |
1832 | /** | |
1833 | * of_genpd_del_provider() - Remove a previously registered PM domain provider | |
1834 | * @np: Device node pointer associated with the PM domain provider | |
1835 | */ | |
1836 | void of_genpd_del_provider(struct device_node *np) | |
1837 | { | |
b556b15d | 1838 | struct of_genpd_provider *cp, *tmp; |
de0aa06d | 1839 | struct generic_pm_domain *gpd; |
aa42240a | 1840 | |
de0aa06d | 1841 | mutex_lock(&gpd_list_lock); |
aa42240a | 1842 | mutex_lock(&of_genpd_mutex); |
b556b15d | 1843 | list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) { |
aa42240a | 1844 | if (cp->node == np) { |
de0aa06d JH |
1845 | /* |
1846 | * For each PM domain associated with the | |
1847 | * provider, set the 'has_provider' to false | |
1848 | * so that the PM domain can be safely removed. | |
1849 | */ | |
1850 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) | |
1851 | if (gpd->provider == &np->fwnode) | |
1852 | gpd->has_provider = false; | |
1853 | ||
aa42240a TF |
1854 | list_del(&cp->link); |
1855 | of_node_put(cp->node); | |
1856 | kfree(cp); | |
1857 | break; | |
1858 | } | |
1859 | } | |
1860 | mutex_unlock(&of_genpd_mutex); | |
de0aa06d | 1861 | mutex_unlock(&gpd_list_lock); |
aa42240a TF |
1862 | } |
1863 | EXPORT_SYMBOL_GPL(of_genpd_del_provider); | |
1864 | ||
1865 | /** | |
f58d4e5a | 1866 | * genpd_get_from_provider() - Look-up PM domain |
aa42240a TF |
1867 | * @genpdspec: OF phandle args to use for look-up |
1868 | * | |
1869 | * Looks for a PM domain provider under the node specified by @genpdspec and if | |
1870 | * found, uses xlate function of the provider to map phandle args to a PM | |
1871 | * domain. | |
1872 | * | |
1873 | * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR() | |
1874 | * on failure. | |
1875 | */ | |
f58d4e5a | 1876 | static struct generic_pm_domain *genpd_get_from_provider( |
aa42240a TF |
1877 | struct of_phandle_args *genpdspec) |
1878 | { | |
1879 | struct generic_pm_domain *genpd = ERR_PTR(-ENOENT); | |
1880 | struct of_genpd_provider *provider; | |
1881 | ||
41795a8a JH |
1882 | if (!genpdspec) |
1883 | return ERR_PTR(-EINVAL); | |
1884 | ||
aa42240a TF |
1885 | mutex_lock(&of_genpd_mutex); |
1886 | ||
1887 | /* Check if we have such a provider in our array */ | |
1888 | list_for_each_entry(provider, &of_genpd_providers, link) { | |
1889 | if (provider->node == genpdspec->np) | |
1890 | genpd = provider->xlate(genpdspec, provider->data); | |
1891 | if (!IS_ERR(genpd)) | |
1892 | break; | |
1893 | } | |
1894 | ||
1895 | mutex_unlock(&of_genpd_mutex); | |
1896 | ||
1897 | return genpd; | |
1898 | } | |
1899 | ||
ec69572b JH |
1900 | /** |
1901 | * of_genpd_add_device() - Add a device to an I/O PM domain | |
1902 | * @genpdspec: OF phandle args to use for look-up PM domain | |
1903 | * @dev: Device to be added. | |
1904 | * | |
1905 | * Looks-up an I/O PM domain based upon phandle args provided and adds | |
1906 | * the device to the PM domain. Returns a negative error code on failure. | |
1907 | */ | |
1908 | int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev) | |
1909 | { | |
1910 | struct generic_pm_domain *genpd; | |
19efa5ff JH |
1911 | int ret; |
1912 | ||
1913 | mutex_lock(&gpd_list_lock); | |
ec69572b | 1914 | |
f58d4e5a | 1915 | genpd = genpd_get_from_provider(genpdspec); |
19efa5ff JH |
1916 | if (IS_ERR(genpd)) { |
1917 | ret = PTR_ERR(genpd); | |
1918 | goto out; | |
1919 | } | |
1920 | ||
1921 | ret = genpd_add_device(genpd, dev, NULL); | |
ec69572b | 1922 | |
19efa5ff JH |
1923 | out: |
1924 | mutex_unlock(&gpd_list_lock); | |
1925 | ||
1926 | return ret; | |
ec69572b JH |
1927 | } |
1928 | EXPORT_SYMBOL_GPL(of_genpd_add_device); | |
1929 | ||
1930 | /** | |
1931 | * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain. | |
1932 | * @parent_spec: OF phandle args to use for parent PM domain look-up | |
1933 | * @subdomain_spec: OF phandle args to use for subdomain look-up | |
1934 | * | |
1935 | * Looks-up a parent PM domain and subdomain based upon phandle args | |
1936 | * provided and adds the subdomain to the parent PM domain. Returns a | |
1937 | * negative error code on failure. | |
1938 | */ | |
1939 | int of_genpd_add_subdomain(struct of_phandle_args *parent_spec, | |
1940 | struct of_phandle_args *subdomain_spec) | |
1941 | { | |
1942 | struct generic_pm_domain *parent, *subdomain; | |
19efa5ff JH |
1943 | int ret; |
1944 | ||
1945 | mutex_lock(&gpd_list_lock); | |
ec69572b | 1946 | |
f58d4e5a | 1947 | parent = genpd_get_from_provider(parent_spec); |
19efa5ff JH |
1948 | if (IS_ERR(parent)) { |
1949 | ret = PTR_ERR(parent); | |
1950 | goto out; | |
1951 | } | |
ec69572b | 1952 | |
f58d4e5a | 1953 | subdomain = genpd_get_from_provider(subdomain_spec); |
19efa5ff JH |
1954 | if (IS_ERR(subdomain)) { |
1955 | ret = PTR_ERR(subdomain); | |
1956 | goto out; | |
1957 | } | |
1958 | ||
1959 | ret = genpd_add_subdomain(parent, subdomain); | |
ec69572b | 1960 | |
19efa5ff JH |
1961 | out: |
1962 | mutex_unlock(&gpd_list_lock); | |
1963 | ||
1964 | return ret; | |
ec69572b JH |
1965 | } |
1966 | EXPORT_SYMBOL_GPL(of_genpd_add_subdomain); | |
1967 | ||
17926551 JH |
1968 | /** |
1969 | * of_genpd_remove_last - Remove the last PM domain registered for a provider | |
1970 | * @provider: Pointer to device structure associated with provider | |
1971 | * | |
1972 | * Find the last PM domain that was added by a particular provider and | |
1973 | * remove this PM domain from the list of PM domains. The provider is | |
1974 | * identified by the 'provider' device structure that is passed. The PM | |
1975 | * domain will only be removed, if the provider associated with domain | |
1976 | * has been removed. | |
1977 | * | |
1978 | * Returns a valid pointer to struct generic_pm_domain on success or | |
1979 | * ERR_PTR() on failure. | |
1980 | */ | |
1981 | struct generic_pm_domain *of_genpd_remove_last(struct device_node *np) | |
1982 | { | |
a7e2d1bc | 1983 | struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT); |
17926551 JH |
1984 | int ret; |
1985 | ||
1986 | if (IS_ERR_OR_NULL(np)) | |
1987 | return ERR_PTR(-EINVAL); | |
1988 | ||
1989 | mutex_lock(&gpd_list_lock); | |
a7e2d1bc | 1990 | list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) { |
17926551 JH |
1991 | if (gpd->provider == &np->fwnode) { |
1992 | ret = genpd_remove(gpd); | |
1993 | genpd = ret ? ERR_PTR(ret) : gpd; | |
1994 | break; | |
1995 | } | |
1996 | } | |
1997 | mutex_unlock(&gpd_list_lock); | |
1998 | ||
1999 | return genpd; | |
2000 | } | |
2001 | EXPORT_SYMBOL_GPL(of_genpd_remove_last); | |
2002 | ||
aa42240a TF |
2003 | /** |
2004 | * genpd_dev_pm_detach - Detach a device from its PM domain. | |
8bb6944e | 2005 | * @dev: Device to detach. |
aa42240a TF |
2006 | * @power_off: Currently not used |
2007 | * | |
2008 | * Try to locate a corresponding generic PM domain, which the device was | |
2009 | * attached to previously. If such is found, the device is detached from it. | |
2010 | */ | |
2011 | static void genpd_dev_pm_detach(struct device *dev, bool power_off) | |
2012 | { | |
446d999c | 2013 | struct generic_pm_domain *pd; |
93af5e93 | 2014 | unsigned int i; |
aa42240a TF |
2015 | int ret = 0; |
2016 | ||
85168d56 UH |
2017 | pd = dev_to_genpd(dev); |
2018 | if (IS_ERR(pd)) | |
aa42240a TF |
2019 | return; |
2020 | ||
2021 | dev_dbg(dev, "removing from PM domain %s\n", pd->name); | |
2022 | ||
93af5e93 | 2023 | for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) { |
85168d56 | 2024 | ret = genpd_remove_device(pd, dev); |
aa42240a TF |
2025 | if (ret != -EAGAIN) |
2026 | break; | |
93af5e93 GU |
2027 | |
2028 | mdelay(i); | |
aa42240a TF |
2029 | cond_resched(); |
2030 | } | |
2031 | ||
2032 | if (ret < 0) { | |
2033 | dev_err(dev, "failed to remove from PM domain %s: %d", | |
2034 | pd->name, ret); | |
2035 | return; | |
2036 | } | |
2037 | ||
2038 | /* Check if PM domain can be powered off after removing this device. */ | |
2039 | genpd_queue_power_off_work(pd); | |
2040 | } | |
2041 | ||
632f7ce3 RK |
2042 | static void genpd_dev_pm_sync(struct device *dev) |
2043 | { | |
2044 | struct generic_pm_domain *pd; | |
2045 | ||
2046 | pd = dev_to_genpd(dev); | |
2047 | if (IS_ERR(pd)) | |
2048 | return; | |
2049 | ||
2050 | genpd_queue_power_off_work(pd); | |
2051 | } | |
2052 | ||
aa42240a TF |
2053 | /** |
2054 | * genpd_dev_pm_attach - Attach a device to its PM domain using DT. | |
2055 | * @dev: Device to attach. | |
2056 | * | |
2057 | * Parse device's OF node to find a PM domain specifier. If such is found, | |
2058 | * attaches the device to retrieved pm_domain ops. | |
2059 | * | |
2060 | * Both generic and legacy Samsung-specific DT bindings are supported to keep | |
2061 | * backwards compatibility with existing DTBs. | |
2062 | * | |
311fa6ad JH |
2063 | * Returns 0 on successfully attached PM domain or negative error code. Note |
2064 | * that if a power-domain exists for the device, but it cannot be found or | |
2065 | * turned on, then return -EPROBE_DEFER to ensure that the device is not | |
2066 | * probed and to re-try again later. | |
aa42240a TF |
2067 | */ |
2068 | int genpd_dev_pm_attach(struct device *dev) | |
2069 | { | |
2070 | struct of_phandle_args pd_args; | |
2071 | struct generic_pm_domain *pd; | |
93af5e93 | 2072 | unsigned int i; |
aa42240a TF |
2073 | int ret; |
2074 | ||
2075 | if (!dev->of_node) | |
2076 | return -ENODEV; | |
2077 | ||
2078 | if (dev->pm_domain) | |
2079 | return -EEXIST; | |
2080 | ||
2081 | ret = of_parse_phandle_with_args(dev->of_node, "power-domains", | |
2082 | "#power-domain-cells", 0, &pd_args); | |
2083 | if (ret < 0) { | |
2084 | if (ret != -ENOENT) | |
2085 | return ret; | |
2086 | ||
2087 | /* | |
2088 | * Try legacy Samsung-specific bindings | |
2089 | * (for backwards compatibility of DT ABI) | |
2090 | */ | |
2091 | pd_args.args_count = 0; | |
2092 | pd_args.np = of_parse_phandle(dev->of_node, | |
2093 | "samsung,power-domain", 0); | |
2094 | if (!pd_args.np) | |
2095 | return -ENOENT; | |
2096 | } | |
2097 | ||
19efa5ff | 2098 | mutex_lock(&gpd_list_lock); |
f58d4e5a | 2099 | pd = genpd_get_from_provider(&pd_args); |
265e2cf6 | 2100 | of_node_put(pd_args.np); |
aa42240a | 2101 | if (IS_ERR(pd)) { |
19efa5ff | 2102 | mutex_unlock(&gpd_list_lock); |
aa42240a TF |
2103 | dev_dbg(dev, "%s() failed to find PM domain: %ld\n", |
2104 | __func__, PTR_ERR(pd)); | |
311fa6ad | 2105 | return -EPROBE_DEFER; |
aa42240a TF |
2106 | } |
2107 | ||
2108 | dev_dbg(dev, "adding to PM domain %s\n", pd->name); | |
2109 | ||
93af5e93 | 2110 | for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) { |
19efa5ff | 2111 | ret = genpd_add_device(pd, dev, NULL); |
aa42240a TF |
2112 | if (ret != -EAGAIN) |
2113 | break; | |
93af5e93 GU |
2114 | |
2115 | mdelay(i); | |
aa42240a TF |
2116 | cond_resched(); |
2117 | } | |
19efa5ff | 2118 | mutex_unlock(&gpd_list_lock); |
aa42240a TF |
2119 | |
2120 | if (ret < 0) { | |
34994692 GU |
2121 | if (ret != -EPROBE_DEFER) |
2122 | dev_err(dev, "failed to add to PM domain %s: %d", | |
2123 | pd->name, ret); | |
311fa6ad | 2124 | goto out; |
aa42240a TF |
2125 | } |
2126 | ||
2127 | dev->pm_domain->detach = genpd_dev_pm_detach; | |
632f7ce3 | 2128 | dev->pm_domain->sync = genpd_dev_pm_sync; |
aa42240a | 2129 | |
35241d12 | 2130 | genpd_lock(pd); |
86e12eac | 2131 | ret = genpd_power_on(pd, 0); |
35241d12 | 2132 | genpd_unlock(pd); |
311fa6ad JH |
2133 | out: |
2134 | return ret ? -EPROBE_DEFER : 0; | |
aa42240a TF |
2135 | } |
2136 | EXPORT_SYMBOL_GPL(genpd_dev_pm_attach); | |
30f60428 LI |
2137 | |
2138 | static const struct of_device_id idle_state_match[] = { | |
598da548 | 2139 | { .compatible = "domain-idle-state", }, |
30f60428 LI |
2140 | { } |
2141 | }; | |
2142 | ||
2143 | static int genpd_parse_state(struct genpd_power_state *genpd_state, | |
2144 | struct device_node *state_node) | |
2145 | { | |
2146 | int err; | |
2147 | u32 residency; | |
2148 | u32 entry_latency, exit_latency; | |
30f60428 LI |
2149 | |
2150 | err = of_property_read_u32(state_node, "entry-latency-us", | |
2151 | &entry_latency); | |
2152 | if (err) { | |
2153 | pr_debug(" * %s missing entry-latency-us property\n", | |
2154 | state_node->full_name); | |
2155 | return -EINVAL; | |
2156 | } | |
2157 | ||
2158 | err = of_property_read_u32(state_node, "exit-latency-us", | |
2159 | &exit_latency); | |
2160 | if (err) { | |
2161 | pr_debug(" * %s missing exit-latency-us property\n", | |
2162 | state_node->full_name); | |
2163 | return -EINVAL; | |
2164 | } | |
2165 | ||
2166 | err = of_property_read_u32(state_node, "min-residency-us", &residency); | |
2167 | if (!err) | |
2168 | genpd_state->residency_ns = 1000 * residency; | |
2169 | ||
2170 | genpd_state->power_on_latency_ns = 1000 * exit_latency; | |
2171 | genpd_state->power_off_latency_ns = 1000 * entry_latency; | |
0c9b694a | 2172 | genpd_state->fwnode = &state_node->fwnode; |
30f60428 LI |
2173 | |
2174 | return 0; | |
2175 | } | |
2176 | ||
2177 | /** | |
2178 | * of_genpd_parse_idle_states: Return array of idle states for the genpd. | |
2179 | * | |
2180 | * @dn: The genpd device node | |
2181 | * @states: The pointer to which the state array will be saved. | |
2182 | * @n: The count of elements in the array returned from this function. | |
2183 | * | |
2184 | * Returns the device states parsed from the OF node. The memory for the states | |
2185 | * is allocated by this function and is the responsibility of the caller to | |
2186 | * free the memory after use. | |
2187 | */ | |
2188 | int of_genpd_parse_idle_states(struct device_node *dn, | |
2189 | struct genpd_power_state **states, int *n) | |
2190 | { | |
2191 | struct genpd_power_state *st; | |
2192 | struct device_node *np; | |
2193 | int i = 0; | |
2194 | int err, ret; | |
2195 | int count; | |
2196 | struct of_phandle_iterator it; | |
b539cc82 | 2197 | const struct of_device_id *match_id; |
30f60428 LI |
2198 | |
2199 | count = of_count_phandle_with_args(dn, "domain-idle-states", NULL); | |
a1fee00d | 2200 | if (count <= 0) |
30f60428 LI |
2201 | return -EINVAL; |
2202 | ||
2203 | st = kcalloc(count, sizeof(*st), GFP_KERNEL); | |
2204 | if (!st) | |
2205 | return -ENOMEM; | |
2206 | ||
2207 | /* Loop over the phandles until all the requested entry is found */ | |
2208 | of_for_each_phandle(&it, err, dn, "domain-idle-states", NULL, 0) { | |
2209 | np = it.node; | |
b539cc82 LI |
2210 | match_id = of_match_node(idle_state_match, np); |
2211 | if (!match_id) | |
2212 | continue; | |
30f60428 LI |
2213 | ret = genpd_parse_state(&st[i++], np); |
2214 | if (ret) { | |
2215 | pr_err | |
2216 | ("Parsing idle state node %s failed with err %d\n", | |
2217 | np->full_name, ret); | |
2218 | of_node_put(np); | |
2219 | kfree(st); | |
2220 | return ret; | |
2221 | } | |
2222 | } | |
2223 | ||
b539cc82 LI |
2224 | *n = i; |
2225 | if (!i) | |
2226 | kfree(st); | |
2227 | else | |
2228 | *states = st; | |
30f60428 LI |
2229 | |
2230 | return 0; | |
2231 | } | |
2232 | EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states); | |
2233 | ||
d30d819d | 2234 | #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */ |
2bd5306a MM |
2235 | |
2236 | ||
2237 | /*** debugfs support ***/ | |
2238 | ||
8b0510b5 | 2239 | #ifdef CONFIG_DEBUG_FS |
2bd5306a MM |
2240 | #include <linux/pm.h> |
2241 | #include <linux/device.h> | |
2242 | #include <linux/debugfs.h> | |
2243 | #include <linux/seq_file.h> | |
2244 | #include <linux/init.h> | |
2245 | #include <linux/kobject.h> | |
2246 | static struct dentry *pm_genpd_debugfs_dir; | |
2247 | ||
2248 | /* | |
2249 | * TODO: This function is a slightly modified version of rtpm_status_show | |
d30d819d | 2250 | * from sysfs.c, so generalize it. |
2bd5306a | 2251 | */ |
2bd5306a MM |
2252 | static void rtpm_status_str(struct seq_file *s, struct device *dev) |
2253 | { | |
2254 | static const char * const status_lookup[] = { | |
2255 | [RPM_ACTIVE] = "active", | |
2256 | [RPM_RESUMING] = "resuming", | |
2257 | [RPM_SUSPENDED] = "suspended", | |
2258 | [RPM_SUSPENDING] = "suspending" | |
2259 | }; | |
2260 | const char *p = ""; | |
2261 | ||
2262 | if (dev->power.runtime_error) | |
2263 | p = "error"; | |
2264 | else if (dev->power.disable_depth) | |
2265 | p = "unsupported"; | |
2266 | else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup)) | |
2267 | p = status_lookup[dev->power.runtime_status]; | |
2268 | else | |
2269 | WARN_ON(1); | |
2270 | ||
2271 | seq_puts(s, p); | |
2272 | } | |
2bd5306a MM |
2273 | |
2274 | static int pm_genpd_summary_one(struct seq_file *s, | |
66a5ca4b | 2275 | struct generic_pm_domain *genpd) |
2bd5306a MM |
2276 | { |
2277 | static const char * const status_lookup[] = { | |
2278 | [GPD_STATE_ACTIVE] = "on", | |
2bd5306a MM |
2279 | [GPD_STATE_POWER_OFF] = "off" |
2280 | }; | |
2281 | struct pm_domain_data *pm_data; | |
2282 | const char *kobj_path; | |
2283 | struct gpd_link *link; | |
6954d432 | 2284 | char state[16]; |
2bd5306a MM |
2285 | int ret; |
2286 | ||
35241d12 | 2287 | ret = genpd_lock_interruptible(genpd); |
2bd5306a MM |
2288 | if (ret) |
2289 | return -ERESTARTSYS; | |
2290 | ||
66a5ca4b | 2291 | if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup))) |
2bd5306a | 2292 | goto exit; |
41e2c8e0 | 2293 | if (!genpd_status_on(genpd)) |
0ba554e4 | 2294 | snprintf(state, sizeof(state), "%s-%u", |
6954d432 | 2295 | status_lookup[genpd->status], genpd->state_idx); |
fc5cbf0c | 2296 | else |
6954d432 GU |
2297 | snprintf(state, sizeof(state), "%s", |
2298 | status_lookup[genpd->status]); | |
2299 | seq_printf(s, "%-30s %-15s ", genpd->name, state); | |
2bd5306a MM |
2300 | |
2301 | /* | |
2302 | * Modifications on the list require holding locks on both | |
2303 | * master and slave, so we are safe. | |
66a5ca4b | 2304 | * Also genpd->name is immutable. |
2bd5306a | 2305 | */ |
66a5ca4b | 2306 | list_for_each_entry(link, &genpd->master_links, master_node) { |
2bd5306a | 2307 | seq_printf(s, "%s", link->slave->name); |
66a5ca4b | 2308 | if (!list_is_last(&link->master_node, &genpd->master_links)) |
2bd5306a MM |
2309 | seq_puts(s, ", "); |
2310 | } | |
2311 | ||
66a5ca4b | 2312 | list_for_each_entry(pm_data, &genpd->dev_list, list_node) { |
d716f479 LI |
2313 | kobj_path = kobject_get_path(&pm_data->dev->kobj, |
2314 | genpd_is_irq_safe(genpd) ? | |
2315 | GFP_ATOMIC : GFP_KERNEL); | |
2bd5306a MM |
2316 | if (kobj_path == NULL) |
2317 | continue; | |
2318 | ||
2319 | seq_printf(s, "\n %-50s ", kobj_path); | |
2320 | rtpm_status_str(s, pm_data->dev); | |
2321 | kfree(kobj_path); | |
2322 | } | |
2323 | ||
2324 | seq_puts(s, "\n"); | |
2325 | exit: | |
35241d12 | 2326 | genpd_unlock(genpd); |
2bd5306a MM |
2327 | |
2328 | return 0; | |
2329 | } | |
2330 | ||
2331 | static int pm_genpd_summary_show(struct seq_file *s, void *data) | |
2332 | { | |
66a5ca4b | 2333 | struct generic_pm_domain *genpd; |
2bd5306a MM |
2334 | int ret = 0; |
2335 | ||
15dec67a GU |
2336 | seq_puts(s, "domain status slaves\n"); |
2337 | seq_puts(s, " /device runtime status\n"); | |
2bd5306a MM |
2338 | seq_puts(s, "----------------------------------------------------------------------\n"); |
2339 | ||
2340 | ret = mutex_lock_interruptible(&gpd_list_lock); | |
2341 | if (ret) | |
2342 | return -ERESTARTSYS; | |
2343 | ||
66a5ca4b KH |
2344 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) { |
2345 | ret = pm_genpd_summary_one(s, genpd); | |
2bd5306a MM |
2346 | if (ret) |
2347 | break; | |
2348 | } | |
2349 | mutex_unlock(&gpd_list_lock); | |
2350 | ||
2351 | return ret; | |
2352 | } | |
2353 | ||
2354 | static int pm_genpd_summary_open(struct inode *inode, struct file *file) | |
2355 | { | |
2356 | return single_open(file, pm_genpd_summary_show, NULL); | |
2357 | } | |
2358 | ||
2359 | static const struct file_operations pm_genpd_summary_fops = { | |
2360 | .open = pm_genpd_summary_open, | |
2361 | .read = seq_read, | |
2362 | .llseek = seq_lseek, | |
2363 | .release = single_release, | |
2364 | }; | |
2365 | ||
2366 | static int __init pm_genpd_debug_init(void) | |
2367 | { | |
2368 | struct dentry *d; | |
2369 | ||
2370 | pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL); | |
2371 | ||
2372 | if (!pm_genpd_debugfs_dir) | |
2373 | return -ENOMEM; | |
2374 | ||
2375 | d = debugfs_create_file("pm_genpd_summary", S_IRUGO, | |
2376 | pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops); | |
2377 | if (!d) | |
2378 | return -ENOMEM; | |
2379 | ||
2380 | return 0; | |
2381 | } | |
2382 | late_initcall(pm_genpd_debug_init); | |
2383 | ||
2384 | static void __exit pm_genpd_debug_exit(void) | |
2385 | { | |
2386 | debugfs_remove_recursive(pm_genpd_debugfs_dir); | |
2387 | } | |
2388 | __exitcall(pm_genpd_debug_exit); | |
8b0510b5 | 2389 | #endif /* CONFIG_DEBUG_FS */ |