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