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