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