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