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