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