linux/drivers/base/power/domain.c
<<
>>
Prefs
   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/init.h>
  10#include <linux/kernel.h>
  11#include <linux/io.h>
  12#include <linux/pm_runtime.h>
  13#include <linux/pm_domain.h>
  14#include <linux/pm_qos.h>
  15#include <linux/slab.h>
  16#include <linux/err.h>
  17#include <linux/sched.h>
  18#include <linux/suspend.h>
  19#include <linux/export.h>
  20
  21#define GENPD_DEV_CALLBACK(genpd, type, callback, dev)          \
  22({                                                              \
  23        type (*__routine)(struct device *__d);                  \
  24        type __ret = (type)0;                                   \
  25                                                                \
  26        __routine = genpd->dev_ops.callback;                    \
  27        if (__routine) {                                        \
  28                __ret = __routine(dev);                         \
  29        } else {                                                \
  30                __routine = dev_gpd_data(dev)->ops.callback;    \
  31                if (__routine)                                  \
  32                        __ret = __routine(dev);                 \
  33        }                                                       \
  34        __ret;                                                  \
  35})
  36
  37#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name)       \
  38({                                                                              \
  39        ktime_t __start = ktime_get();                                          \
  40        type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev);         \
  41        s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start));           \
  42        struct gpd_timing_data *__td = &dev_gpd_data(dev)->td;                  \
  43        if (!__retval && __elapsed > __td->field) {                             \
  44                __td->field = __elapsed;                                        \
  45                dev_warn(dev, name " latency exceeded, new value %lld ns\n",    \
  46                        __elapsed);                                             \
  47                genpd->max_off_time_changed = true;                             \
  48                __td->constraint_changed = true;                                \
  49        }                                                                       \
  50        __retval;                                                               \
  51})
  52
  53static LIST_HEAD(gpd_list);
  54static DEFINE_MUTEX(gpd_list_lock);
  55
  56static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
  57{
  58        struct generic_pm_domain *genpd = NULL, *gpd;
  59
  60        if (IS_ERR_OR_NULL(domain_name))
  61                return NULL;
  62
  63        mutex_lock(&gpd_list_lock);
  64        list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  65                if (!strcmp(gpd->name, domain_name)) {
  66                        genpd = gpd;
  67                        break;
  68                }
  69        }
  70        mutex_unlock(&gpd_list_lock);
  71        return genpd;
  72}
  73
  74#ifdef CONFIG_PM
  75
  76struct generic_pm_domain *dev_to_genpd(struct device *dev)
  77{
  78        if (IS_ERR_OR_NULL(dev->pm_domain))
  79                return ERR_PTR(-EINVAL);
  80
  81        return pd_to_genpd(dev->pm_domain);
  82}
  83
  84static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
  85{
  86        return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
  87                                        stop_latency_ns, "stop");
  88}
  89
  90static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
  91{
  92        return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
  93                                        start_latency_ns, "start");
  94}
  95
  96static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  97{
  98        bool ret = false;
  99
 100        if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
 101                ret = !!atomic_dec_and_test(&genpd->sd_count);
 102
 103        return ret;
 104}
 105
 106static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
 107{
 108        atomic_inc(&genpd->sd_count);
 109        smp_mb__after_atomic_inc();
 110}
 111
 112static void genpd_acquire_lock(struct generic_pm_domain *genpd)
 113{
 114        DEFINE_WAIT(wait);
 115
 116        mutex_lock(&genpd->lock);
 117        /*
 118         * Wait for the domain to transition into either the active,
 119         * or the power off state.
 120         */
 121        for (;;) {
 122                prepare_to_wait(&genpd->status_wait_queue, &wait,
 123                                TASK_UNINTERRUPTIBLE);
 124                if (genpd->status == GPD_STATE_ACTIVE
 125                    || genpd->status == GPD_STATE_POWER_OFF)
 126                        break;
 127                mutex_unlock(&genpd->lock);
 128
 129                schedule();
 130
 131                mutex_lock(&genpd->lock);
 132        }
 133        finish_wait(&genpd->status_wait_queue, &wait);
 134}
 135
 136static void genpd_release_lock(struct generic_pm_domain *genpd)
 137{
 138        mutex_unlock(&genpd->lock);
 139}
 140
 141static void genpd_set_active(struct generic_pm_domain *genpd)
 142{
 143        if (genpd->resume_count == 0)
 144                genpd->status = GPD_STATE_ACTIVE;
 145}
 146
 147static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
 148{
 149        s64 usecs64;
 150
 151        if (!genpd->cpu_data)
 152                return;
 153
 154        usecs64 = genpd->power_on_latency_ns;
 155        do_div(usecs64, NSEC_PER_USEC);
 156        usecs64 += genpd->cpu_data->saved_exit_latency;
 157        genpd->cpu_data->idle_state->exit_latency = usecs64;
 158}
 159
 160/**
 161 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
 162 * @genpd: PM domain to power up.
 163 *
 164 * Restore power to @genpd and all of its masters so that it is possible to
 165 * resume a device belonging to it.
 166 */
 167static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
 168        __releases(&genpd->lock) __acquires(&genpd->lock)
 169{
 170        struct gpd_link *link;
 171        DEFINE_WAIT(wait);
 172        int ret = 0;
 173
 174        /* If the domain's master is being waited for, we have to wait too. */
 175        for (;;) {
 176                prepare_to_wait(&genpd->status_wait_queue, &wait,
 177                                TASK_UNINTERRUPTIBLE);
 178                if (genpd->status != GPD_STATE_WAIT_MASTER)
 179                        break;
 180                mutex_unlock(&genpd->lock);
 181
 182                schedule();
 183
 184                mutex_lock(&genpd->lock);
 185        }
 186        finish_wait(&genpd->status_wait_queue, &wait);
 187
 188        if (genpd->status == GPD_STATE_ACTIVE
 189            || (genpd->prepared_count > 0 && genpd->suspend_power_off))
 190                return 0;
 191
 192        if (genpd->status != GPD_STATE_POWER_OFF) {
 193                genpd_set_active(genpd);
 194                return 0;
 195        }
 196
 197        if (genpd->cpu_data) {
 198                cpuidle_pause_and_lock();
 199                genpd->cpu_data->idle_state->disabled = true;
 200                cpuidle_resume_and_unlock();
 201                goto out;
 202        }
 203
 204        /*
 205         * The list is guaranteed not to change while the loop below is being
 206         * executed, unless one of the masters' .power_on() callbacks fiddles
 207         * with it.
 208         */
 209        list_for_each_entry(link, &genpd->slave_links, slave_node) {
 210                genpd_sd_counter_inc(link->master);
 211                genpd->status = GPD_STATE_WAIT_MASTER;
 212
 213                mutex_unlock(&genpd->lock);
 214
 215                ret = pm_genpd_poweron(link->master);
 216
 217                mutex_lock(&genpd->lock);
 218
 219                /*
 220                 * The "wait for parent" status is guaranteed not to change
 221                 * while the master is powering on.
 222                 */
 223                genpd->status = GPD_STATE_POWER_OFF;
 224                wake_up_all(&genpd->status_wait_queue);
 225                if (ret) {
 226                        genpd_sd_counter_dec(link->master);
 227                        goto err;
 228                }
 229        }
 230
 231        if (genpd->power_on) {
 232                ktime_t time_start = ktime_get();
 233                s64 elapsed_ns;
 234
 235                ret = genpd->power_on(genpd);
 236                if (ret)
 237                        goto err;
 238
 239                elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 240                if (elapsed_ns > genpd->power_on_latency_ns) {
 241                        genpd->power_on_latency_ns = elapsed_ns;
 242                        genpd->max_off_time_changed = true;
 243                        genpd_recalc_cpu_exit_latency(genpd);
 244                        if (genpd->name)
 245                                pr_warning("%s: Power-on latency exceeded, "
 246                                        "new value %lld ns\n", genpd->name,
 247                                        elapsed_ns);
 248                }
 249        }
 250
 251 out:
 252        genpd_set_active(genpd);
 253
 254        return 0;
 255
 256 err:
 257        list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
 258                genpd_sd_counter_dec(link->master);
 259
 260        return ret;
 261}
 262
 263/**
 264 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
 265 * @genpd: PM domain to power up.
 266 */
 267int pm_genpd_poweron(struct generic_pm_domain *genpd)
 268{
 269        int ret;
 270
 271        mutex_lock(&genpd->lock);
 272        ret = __pm_genpd_poweron(genpd);
 273        mutex_unlock(&genpd->lock);
 274        return ret;
 275}
 276
 277/**
 278 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
 279 * @domain_name: Name of the PM domain to power up.
 280 */
 281int pm_genpd_name_poweron(const char *domain_name)
 282{
 283        struct generic_pm_domain *genpd;
 284
 285        genpd = pm_genpd_lookup_name(domain_name);
 286        return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
 287}
 288
 289#endif /* CONFIG_PM */
 290
 291#ifdef CONFIG_PM_RUNTIME
 292
 293static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
 294                                     struct device *dev)
 295{
 296        return GENPD_DEV_CALLBACK(genpd, int, start, dev);
 297}
 298
 299static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
 300{
 301        return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
 302                                        save_state_latency_ns, "state save");
 303}
 304
 305static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
 306{
 307        return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
 308                                        restore_state_latency_ns,
 309                                        "state restore");
 310}
 311
 312static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
 313                                     unsigned long val, void *ptr)
 314{
 315        struct generic_pm_domain_data *gpd_data;
 316        struct device *dev;
 317
 318        gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
 319
 320        mutex_lock(&gpd_data->lock);
 321        dev = gpd_data->base.dev;
 322        if (!dev) {
 323                mutex_unlock(&gpd_data->lock);
 324                return NOTIFY_DONE;
 325        }
 326        mutex_unlock(&gpd_data->lock);
 327
 328        for (;;) {
 329                struct generic_pm_domain *genpd;
 330                struct pm_domain_data *pdd;
 331
 332                spin_lock_irq(&dev->power.lock);
 333
 334                pdd = dev->power.subsys_data ?
 335                                dev->power.subsys_data->domain_data : NULL;
 336                if (pdd && pdd->dev) {
 337                        to_gpd_data(pdd)->td.constraint_changed = true;
 338                        genpd = dev_to_genpd(dev);
 339                } else {
 340                        genpd = ERR_PTR(-ENODATA);
 341                }
 342
 343                spin_unlock_irq(&dev->power.lock);
 344
 345                if (!IS_ERR(genpd)) {
 346                        mutex_lock(&genpd->lock);
 347                        genpd->max_off_time_changed = true;
 348                        mutex_unlock(&genpd->lock);
 349                }
 350
 351                dev = dev->parent;
 352                if (!dev || dev->power.ignore_children)
 353                        break;
 354        }
 355
 356        return NOTIFY_DONE;
 357}
 358
 359/**
 360 * __pm_genpd_save_device - Save the pre-suspend state of a device.
 361 * @pdd: Domain data of the device to save the state of.
 362 * @genpd: PM domain the device belongs to.
 363 */
 364static int __pm_genpd_save_device(struct pm_domain_data *pdd,
 365                                  struct generic_pm_domain *genpd)
 366        __releases(&genpd->lock) __acquires(&genpd->lock)
 367{
 368        struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
 369        struct device *dev = pdd->dev;
 370        int ret = 0;
 371
 372        if (gpd_data->need_restore)
 373                return 0;
 374
 375        mutex_unlock(&genpd->lock);
 376
 377        genpd_start_dev(genpd, dev);
 378        ret = genpd_save_dev(genpd, dev);
 379        genpd_stop_dev(genpd, dev);
 380
 381        mutex_lock(&genpd->lock);
 382
 383        if (!ret)
 384                gpd_data->need_restore = true;
 385
 386        return ret;
 387}
 388
 389/**
 390 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
 391 * @pdd: Domain data of the device to restore the state of.
 392 * @genpd: PM domain the device belongs to.
 393 */
 394static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
 395                                      struct generic_pm_domain *genpd)
 396        __releases(&genpd->lock) __acquires(&genpd->lock)
 397{
 398        struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
 399        struct device *dev = pdd->dev;
 400        bool need_restore = gpd_data->need_restore;
 401
 402        gpd_data->need_restore = false;
 403        mutex_unlock(&genpd->lock);
 404
 405        genpd_start_dev(genpd, dev);
 406        if (need_restore)
 407                genpd_restore_dev(genpd, dev);
 408
 409        mutex_lock(&genpd->lock);
 410}
 411
 412/**
 413 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
 414 * @genpd: PM domain to check.
 415 *
 416 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
 417 * a "power off" operation, which means that a "power on" has occured in the
 418 * meantime, or if its resume_count field is different from zero, which means
 419 * that one of its devices has been resumed in the meantime.
 420 */
 421static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
 422{
 423        return genpd->status == GPD_STATE_WAIT_MASTER
 424                || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
 425}
 426
 427/**
 428 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
 429 * @genpd: PM domait to power off.
 430 *
 431 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
 432 * before.
 433 */
 434void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
 435{
 436        queue_work(pm_wq, &genpd->power_off_work);
 437}
 438
 439/**
 440 * pm_genpd_poweroff - Remove power from a given PM domain.
 441 * @genpd: PM domain to power down.
 442 *
 443 * If all of the @genpd's devices have been suspended and all of its subdomains
 444 * have been powered down, run the runtime suspend callbacks provided by all of
 445 * the @genpd's devices' drivers and remove power from @genpd.
 446 */
 447static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
 448        __releases(&genpd->lock) __acquires(&genpd->lock)
 449{
 450        struct pm_domain_data *pdd;
 451        struct gpd_link *link;
 452        unsigned int not_suspended;
 453        int ret = 0;
 454
 455 start:
 456        /*
 457         * Do not try to power off the domain in the following situations:
 458         * (1) The domain is already in the "power off" state.
 459         * (2) The domain is waiting for its master to power up.
 460         * (3) One of the domain's devices is being resumed right now.
 461         * (4) System suspend is in progress.
 462         */
 463        if (genpd->status == GPD_STATE_POWER_OFF
 464            || genpd->status == GPD_STATE_WAIT_MASTER
 465            || genpd->resume_count > 0 || genpd->prepared_count > 0)
 466                return 0;
 467
 468        if (atomic_read(&genpd->sd_count) > 0)
 469                return -EBUSY;
 470
 471        not_suspended = 0;
 472        list_for_each_entry(pdd, &genpd->dev_list, list_node) {
 473                enum pm_qos_flags_status stat;
 474
 475                stat = dev_pm_qos_flags(pdd->dev,
 476                                        PM_QOS_FLAG_NO_POWER_OFF
 477                                                | PM_QOS_FLAG_REMOTE_WAKEUP);
 478                if (stat > PM_QOS_FLAGS_NONE)
 479                        return -EBUSY;
 480
 481                if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
 482                    || pdd->dev->power.irq_safe))
 483                        not_suspended++;
 484        }
 485
 486        if (not_suspended > genpd->in_progress)
 487                return -EBUSY;
 488
 489        if (genpd->poweroff_task) {
 490                /*
 491                 * Another instance of pm_genpd_poweroff() is executing
 492                 * callbacks, so tell it to start over and return.
 493                 */
 494                genpd->status = GPD_STATE_REPEAT;
 495                return 0;
 496        }
 497
 498        if (genpd->gov && genpd->gov->power_down_ok) {
 499                if (!genpd->gov->power_down_ok(&genpd->domain))
 500                        return -EAGAIN;
 501        }
 502
 503        genpd->status = GPD_STATE_BUSY;
 504        genpd->poweroff_task = current;
 505
 506        list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
 507                ret = atomic_read(&genpd->sd_count) == 0 ?
 508                        __pm_genpd_save_device(pdd, genpd) : -EBUSY;
 509
 510                if (genpd_abort_poweroff(genpd))
 511                        goto out;
 512
 513                if (ret) {
 514                        genpd_set_active(genpd);
 515                        goto out;
 516                }
 517
 518                if (genpd->status == GPD_STATE_REPEAT) {
 519                        genpd->poweroff_task = NULL;
 520                        goto start;
 521                }
 522        }
 523
 524        if (genpd->cpu_data) {
 525                /*
 526                 * If cpu_data is set, cpuidle should turn the domain off when
 527                 * the CPU in it is idle.  In that case we don't decrement the
 528                 * subdomain counts of the master domains, so that power is not
 529                 * removed from the current domain prematurely as a result of
 530                 * cutting off the masters' power.
 531                 */
 532                genpd->status = GPD_STATE_POWER_OFF;
 533                cpuidle_pause_and_lock();
 534                genpd->cpu_data->idle_state->disabled = false;
 535                cpuidle_resume_and_unlock();
 536                goto out;
 537        }
 538
 539        if (genpd->power_off) {
 540                ktime_t time_start;
 541                s64 elapsed_ns;
 542
 543                if (atomic_read(&genpd->sd_count) > 0) {
 544                        ret = -EBUSY;
 545                        goto out;
 546                }
 547
 548                time_start = ktime_get();
 549
 550                /*
 551                 * If sd_count > 0 at this point, one of the subdomains hasn't
 552                 * managed to call pm_genpd_poweron() for the master yet after
 553                 * incrementing it.  In that case pm_genpd_poweron() will wait
 554                 * for us to drop the lock, so we can call .power_off() and let
 555                 * the pm_genpd_poweron() restore power for us (this shouldn't
 556                 * happen very often).
 557                 */
 558                ret = genpd->power_off(genpd);
 559                if (ret == -EBUSY) {
 560                        genpd_set_active(genpd);
 561                        goto out;
 562                }
 563
 564                elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 565                if (elapsed_ns > genpd->power_off_latency_ns) {
 566                        genpd->power_off_latency_ns = elapsed_ns;
 567                        genpd->max_off_time_changed = true;
 568                        if (genpd->name)
 569                                pr_warning("%s: Power-off latency exceeded, "
 570                                        "new value %lld ns\n", genpd->name,
 571                                        elapsed_ns);
 572                }
 573        }
 574
 575        genpd->status = GPD_STATE_POWER_OFF;
 576
 577        list_for_each_entry(link, &genpd->slave_links, slave_node) {
 578                genpd_sd_counter_dec(link->master);
 579                genpd_queue_power_off_work(link->master);
 580        }
 581
 582 out:
 583        genpd->poweroff_task = NULL;
 584        wake_up_all(&genpd->status_wait_queue);
 585        return ret;
 586}
 587
 588/**
 589 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
 590 * @work: Work structure used for scheduling the execution of this function.
 591 */
 592static void genpd_power_off_work_fn(struct work_struct *work)
 593{
 594        struct generic_pm_domain *genpd;
 595
 596        genpd = container_of(work, struct generic_pm_domain, power_off_work);
 597
 598        genpd_acquire_lock(genpd);
 599        pm_genpd_poweroff(genpd);
 600        genpd_release_lock(genpd);
 601}
 602
 603/**
 604 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
 605 * @dev: Device to suspend.
 606 *
 607 * Carry out a runtime suspend of a device under the assumption that its
 608 * pm_domain field points to the domain member of an object of type
 609 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 610 */
 611static int pm_genpd_runtime_suspend(struct device *dev)
 612{
 613        struct generic_pm_domain *genpd;
 614        bool (*stop_ok)(struct device *__dev);
 615        int ret;
 616
 617        dev_dbg(dev, "%s()\n", __func__);
 618
 619        genpd = dev_to_genpd(dev);
 620        if (IS_ERR(genpd))
 621                return -EINVAL;
 622
 623        might_sleep_if(!genpd->dev_irq_safe);
 624
 625        stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
 626        if (stop_ok && !stop_ok(dev))
 627                return -EBUSY;
 628
 629        ret = genpd_stop_dev(genpd, dev);
 630        if (ret)
 631                return ret;
 632
 633        /*
 634         * If power.irq_safe is set, this routine will be run with interrupts
 635         * off, so it can't use mutexes.
 636         */
 637        if (dev->power.irq_safe)
 638                return 0;
 639
 640        mutex_lock(&genpd->lock);
 641        genpd->in_progress++;
 642        pm_genpd_poweroff(genpd);
 643        genpd->in_progress--;
 644        mutex_unlock(&genpd->lock);
 645
 646        return 0;
 647}
 648
 649/**
 650 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
 651 * @dev: Device to resume.
 652 *
 653 * Carry out a runtime resume of a device under the assumption that its
 654 * pm_domain field points to the domain member of an object of type
 655 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 656 */
 657static int pm_genpd_runtime_resume(struct device *dev)
 658{
 659        struct generic_pm_domain *genpd;
 660        DEFINE_WAIT(wait);
 661        int ret;
 662
 663        dev_dbg(dev, "%s()\n", __func__);
 664
 665        genpd = dev_to_genpd(dev);
 666        if (IS_ERR(genpd))
 667                return -EINVAL;
 668
 669        might_sleep_if(!genpd->dev_irq_safe);
 670
 671        /* If power.irq_safe, the PM domain is never powered off. */
 672        if (dev->power.irq_safe)
 673                return genpd_start_dev_no_timing(genpd, dev);
 674
 675        mutex_lock(&genpd->lock);
 676        ret = __pm_genpd_poweron(genpd);
 677        if (ret) {
 678                mutex_unlock(&genpd->lock);
 679                return ret;
 680        }
 681        genpd->status = GPD_STATE_BUSY;
 682        genpd->resume_count++;
 683        for (;;) {
 684                prepare_to_wait(&genpd->status_wait_queue, &wait,
 685                                TASK_UNINTERRUPTIBLE);
 686                /*
 687                 * If current is the powering off task, we have been called
 688                 * reentrantly from one of the device callbacks, so we should
 689                 * not wait.
 690                 */
 691                if (!genpd->poweroff_task || genpd->poweroff_task == current)
 692                        break;
 693                mutex_unlock(&genpd->lock);
 694
 695                schedule();
 696
 697                mutex_lock(&genpd->lock);
 698        }
 699        finish_wait(&genpd->status_wait_queue, &wait);
 700        __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
 701        genpd->resume_count--;
 702        genpd_set_active(genpd);
 703        wake_up_all(&genpd->status_wait_queue);
 704        mutex_unlock(&genpd->lock);
 705
 706        return 0;
 707}
 708
 709/**
 710 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
 711 */
 712void pm_genpd_poweroff_unused(void)
 713{
 714        struct generic_pm_domain *genpd;
 715
 716        mutex_lock(&gpd_list_lock);
 717
 718        list_for_each_entry(genpd, &gpd_list, gpd_list_node)
 719                genpd_queue_power_off_work(genpd);
 720
 721        mutex_unlock(&gpd_list_lock);
 722}
 723
 724#else
 725
 726static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
 727                                            unsigned long val, void *ptr)
 728{
 729        return NOTIFY_DONE;
 730}
 731
 732static inline void genpd_power_off_work_fn(struct work_struct *work) {}
 733
 734#define pm_genpd_runtime_suspend        NULL
 735#define pm_genpd_runtime_resume         NULL
 736
 737#endif /* CONFIG_PM_RUNTIME */
 738
 739#ifdef CONFIG_PM_SLEEP
 740
 741/**
 742 * pm_genpd_present - Check if the given PM domain has been initialized.
 743 * @genpd: PM domain to check.
 744 */
 745static bool pm_genpd_present(struct generic_pm_domain *genpd)
 746{
 747        struct generic_pm_domain *gpd;
 748
 749        if (IS_ERR_OR_NULL(genpd))
 750                return false;
 751
 752        list_for_each_entry(gpd, &gpd_list, gpd_list_node)
 753                if (gpd == genpd)
 754                        return true;
 755
 756        return false;
 757}
 758
 759static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
 760                                    struct device *dev)
 761{
 762        return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
 763}
 764
 765static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
 766{
 767        return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
 768}
 769
 770static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
 771{
 772        return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
 773}
 774
 775static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
 776{
 777        return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
 778}
 779
 780static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
 781{
 782        return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
 783}
 784
 785static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
 786{
 787        return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
 788}
 789
 790static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
 791{
 792        return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
 793}
 794
 795static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
 796{
 797        return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
 798}
 799
 800static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
 801{
 802        return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
 803}
 804
 805/**
 806 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
 807 * @genpd: PM domain to power off, if possible.
 808 *
 809 * Check if the given PM domain can be powered off (during system suspend or
 810 * hibernation) and do that if so.  Also, in that case propagate to its masters.
 811 *
 812 * This function is only called in "noirq" and "syscore" stages of system power
 813 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
 814 * executed sequentially, so it is guaranteed that it will never run twice in
 815 * parallel).
 816 */
 817static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
 818{
 819        struct gpd_link *link;
 820
 821        if (genpd->status == GPD_STATE_POWER_OFF)
 822                return;
 823
 824        if (genpd->suspended_count != genpd->device_count
 825            || atomic_read(&genpd->sd_count) > 0)
 826                return;
 827
 828        if (genpd->power_off)
 829                genpd->power_off(genpd);
 830
 831        genpd->status = GPD_STATE_POWER_OFF;
 832
 833        list_for_each_entry(link, &genpd->slave_links, slave_node) {
 834                genpd_sd_counter_dec(link->master);
 835                pm_genpd_sync_poweroff(link->master);
 836        }
 837}
 838
 839/**
 840 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
 841 * @genpd: PM domain to power on.
 842 *
 843 * This function is only called in "noirq" and "syscore" stages of system power
 844 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
 845 * executed sequentially, so it is guaranteed that it will never run twice in
 846 * parallel).
 847 */
 848static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
 849{
 850        struct gpd_link *link;
 851
 852        if (genpd->status != GPD_STATE_POWER_OFF)
 853                return;
 854
 855        list_for_each_entry(link, &genpd->slave_links, slave_node) {
 856                pm_genpd_sync_poweron(link->master);
 857                genpd_sd_counter_inc(link->master);
 858        }
 859
 860        if (genpd->power_on)
 861                genpd->power_on(genpd);
 862
 863        genpd->status = GPD_STATE_ACTIVE;
 864}
 865
 866/**
 867 * resume_needed - Check whether to resume a device before system suspend.
 868 * @dev: Device to check.
 869 * @genpd: PM domain the device belongs to.
 870 *
 871 * There are two cases in which a device that can wake up the system from sleep
 872 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
 873 * to wake up the system and it has to remain active for this purpose while the
 874 * system is in the sleep state and (2) if the device is not enabled to wake up
 875 * the system from sleep states and it generally doesn't generate wakeup signals
 876 * by itself (those signals are generated on its behalf by other parts of the
 877 * system).  In the latter case it may be necessary to reconfigure the device's
 878 * wakeup settings during system suspend, because it may have been set up to
 879 * signal remote wakeup from the system's working state as needed by runtime PM.
 880 * Return 'true' in either of the above cases.
 881 */
 882static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
 883{
 884        bool active_wakeup;
 885
 886        if (!device_can_wakeup(dev))
 887                return false;
 888
 889        active_wakeup = genpd_dev_active_wakeup(genpd, dev);
 890        return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
 891}
 892
 893/**
 894 * pm_genpd_prepare - Start power transition of a device in a PM domain.
 895 * @dev: Device to start the transition of.
 896 *
 897 * Start a power transition of a device (during a system-wide power transition)
 898 * under the assumption that its pm_domain field points to the domain member of
 899 * an object of type struct generic_pm_domain representing a PM domain
 900 * consisting of I/O devices.
 901 */
 902static int pm_genpd_prepare(struct device *dev)
 903{
 904        struct generic_pm_domain *genpd;
 905        int ret;
 906
 907        dev_dbg(dev, "%s()\n", __func__);
 908
 909        genpd = dev_to_genpd(dev);
 910        if (IS_ERR(genpd))
 911                return -EINVAL;
 912
 913        /*
 914         * If a wakeup request is pending for the device, it should be woken up
 915         * at this point and a system wakeup event should be reported if it's
 916         * set up to wake up the system from sleep states.
 917         */
 918        pm_runtime_get_noresume(dev);
 919        if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
 920                pm_wakeup_event(dev, 0);
 921
 922        if (pm_wakeup_pending()) {
 923                pm_runtime_put(dev);
 924                return -EBUSY;
 925        }
 926
 927        if (resume_needed(dev, genpd))
 928                pm_runtime_resume(dev);
 929
 930        genpd_acquire_lock(genpd);
 931
 932        if (genpd->prepared_count++ == 0) {
 933                genpd->suspended_count = 0;
 934                genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
 935        }
 936
 937        genpd_release_lock(genpd);
 938
 939        if (genpd->suspend_power_off) {
 940                pm_runtime_put_noidle(dev);
 941                return 0;
 942        }
 943
 944        /*
 945         * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
 946         * so pm_genpd_poweron() will return immediately, but if the device
 947         * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
 948         * to make it operational.
 949         */
 950        pm_runtime_resume(dev);
 951        __pm_runtime_disable(dev, false);
 952
 953        ret = pm_generic_prepare(dev);
 954        if (ret) {
 955                mutex_lock(&genpd->lock);
 956
 957                if (--genpd->prepared_count == 0)
 958                        genpd->suspend_power_off = false;
 959
 960                mutex_unlock(&genpd->lock);
 961                pm_runtime_enable(dev);
 962        }
 963
 964        pm_runtime_put(dev);
 965        return ret;
 966}
 967
 968/**
 969 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
 970 * @dev: Device to suspend.
 971 *
 972 * Suspend a device under the assumption that its pm_domain field points to the
 973 * domain member of an object of type struct generic_pm_domain representing
 974 * a PM domain consisting of I/O devices.
 975 */
 976static int pm_genpd_suspend(struct device *dev)
 977{
 978        struct generic_pm_domain *genpd;
 979
 980        dev_dbg(dev, "%s()\n", __func__);
 981
 982        genpd = dev_to_genpd(dev);
 983        if (IS_ERR(genpd))
 984                return -EINVAL;
 985
 986        return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
 987}
 988
 989/**
 990 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
 991 * @dev: Device to suspend.
 992 *
 993 * Carry out a late suspend of a device under the assumption that its
 994 * pm_domain field points to the domain member of an object of type
 995 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 996 */
 997static int pm_genpd_suspend_late(struct device *dev)
 998{
 999        struct generic_pm_domain *genpd;
1000
1001        dev_dbg(dev, "%s()\n", __func__);
1002
1003        genpd = dev_to_genpd(dev);
1004        if (IS_ERR(genpd))
1005                return -EINVAL;
1006
1007        return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1008}
1009
1010/**
1011 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1012 * @dev: Device to suspend.
1013 *
1014 * Stop the device and remove power from the domain if all devices in it have
1015 * been stopped.
1016 */
1017static int pm_genpd_suspend_noirq(struct device *dev)
1018{
1019        struct generic_pm_domain *genpd;
1020
1021        dev_dbg(dev, "%s()\n", __func__);
1022
1023        genpd = dev_to_genpd(dev);
1024        if (IS_ERR(genpd))
1025                return -EINVAL;
1026
1027        if (genpd->suspend_power_off
1028            || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1029                return 0;
1030
1031        genpd_stop_dev(genpd, dev);
1032
1033        /*
1034         * Since all of the "noirq" callbacks are executed sequentially, it is
1035         * guaranteed that this function will never run twice in parallel for
1036         * the same PM domain, so it is not necessary to use locking here.
1037         */
1038        genpd->suspended_count++;
1039        pm_genpd_sync_poweroff(genpd);
1040
1041        return 0;
1042}
1043
1044/**
1045 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1046 * @dev: Device to resume.
1047 *
1048 * Restore power to the device's PM domain, if necessary, and start the device.
1049 */
1050static int pm_genpd_resume_noirq(struct device *dev)
1051{
1052        struct generic_pm_domain *genpd;
1053
1054        dev_dbg(dev, "%s()\n", __func__);
1055
1056        genpd = dev_to_genpd(dev);
1057        if (IS_ERR(genpd))
1058                return -EINVAL;
1059
1060        if (genpd->suspend_power_off
1061            || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1062                return 0;
1063
1064        /*
1065         * Since all of the "noirq" callbacks are executed sequentially, it is
1066         * guaranteed that this function will never run twice in parallel for
1067         * the same PM domain, so it is not necessary to use locking here.
1068         */
1069        pm_genpd_sync_poweron(genpd);
1070        genpd->suspended_count--;
1071
1072        return genpd_start_dev(genpd, dev);
1073}
1074
1075/**
1076 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1077 * @dev: Device to resume.
1078 *
1079 * Carry out an early resume of a device under the assumption that its
1080 * pm_domain field points to the domain member of an object of type
1081 * struct generic_pm_domain representing a power domain consisting of I/O
1082 * devices.
1083 */
1084static int pm_genpd_resume_early(struct device *dev)
1085{
1086        struct generic_pm_domain *genpd;
1087
1088        dev_dbg(dev, "%s()\n", __func__);
1089
1090        genpd = dev_to_genpd(dev);
1091        if (IS_ERR(genpd))
1092                return -EINVAL;
1093
1094        return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1095}
1096
1097/**
1098 * pm_genpd_resume - Resume of device in an I/O PM domain.
1099 * @dev: Device to resume.
1100 *
1101 * Resume a device under the assumption that its pm_domain field points to the
1102 * domain member of an object of type struct generic_pm_domain representing
1103 * a power domain consisting of I/O devices.
1104 */
1105static int pm_genpd_resume(struct device *dev)
1106{
1107        struct generic_pm_domain *genpd;
1108
1109        dev_dbg(dev, "%s()\n", __func__);
1110
1111        genpd = dev_to_genpd(dev);
1112        if (IS_ERR(genpd))
1113                return -EINVAL;
1114
1115        return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1116}
1117
1118/**
1119 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1120 * @dev: Device to freeze.
1121 *
1122 * Freeze a device under the assumption that its pm_domain field points to the
1123 * domain member of an object of type struct generic_pm_domain representing
1124 * a power domain consisting of I/O devices.
1125 */
1126static int pm_genpd_freeze(struct device *dev)
1127{
1128        struct generic_pm_domain *genpd;
1129
1130        dev_dbg(dev, "%s()\n", __func__);
1131
1132        genpd = dev_to_genpd(dev);
1133        if (IS_ERR(genpd))
1134                return -EINVAL;
1135
1136        return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1137}
1138
1139/**
1140 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1141 * @dev: Device to freeze.
1142 *
1143 * Carry out a late freeze of a device under the assumption that its
1144 * pm_domain field points to the domain member of an object of type
1145 * struct generic_pm_domain representing a power domain consisting of I/O
1146 * devices.
1147 */
1148static int pm_genpd_freeze_late(struct device *dev)
1149{
1150        struct generic_pm_domain *genpd;
1151
1152        dev_dbg(dev, "%s()\n", __func__);
1153
1154        genpd = dev_to_genpd(dev);
1155        if (IS_ERR(genpd))
1156                return -EINVAL;
1157
1158        return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1159}
1160
1161/**
1162 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1163 * @dev: Device to freeze.
1164 *
1165 * Carry out a late freeze of a device under the assumption that its
1166 * pm_domain field points to the domain member of an object of type
1167 * struct generic_pm_domain representing a power domain consisting of I/O
1168 * devices.
1169 */
1170static int pm_genpd_freeze_noirq(struct device *dev)
1171{
1172        struct generic_pm_domain *genpd;
1173
1174        dev_dbg(dev, "%s()\n", __func__);
1175
1176        genpd = dev_to_genpd(dev);
1177        if (IS_ERR(genpd))
1178                return -EINVAL;
1179
1180        return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1181}
1182
1183/**
1184 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1185 * @dev: Device to thaw.
1186 *
1187 * Start the device, unless power has been removed from the domain already
1188 * before the system transition.
1189 */
1190static int pm_genpd_thaw_noirq(struct device *dev)
1191{
1192        struct generic_pm_domain *genpd;
1193
1194        dev_dbg(dev, "%s()\n", __func__);
1195
1196        genpd = dev_to_genpd(dev);
1197        if (IS_ERR(genpd))
1198                return -EINVAL;
1199
1200        return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1201}
1202
1203/**
1204 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1205 * @dev: Device to thaw.
1206 *
1207 * Carry out an early thaw of a device under the assumption that its
1208 * pm_domain field points to the domain member of an object of type
1209 * struct generic_pm_domain representing a power domain consisting of I/O
1210 * devices.
1211 */
1212static int pm_genpd_thaw_early(struct device *dev)
1213{
1214        struct generic_pm_domain *genpd;
1215
1216        dev_dbg(dev, "%s()\n", __func__);
1217
1218        genpd = dev_to_genpd(dev);
1219        if (IS_ERR(genpd))
1220                return -EINVAL;
1221
1222        return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1223}
1224
1225/**
1226 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1227 * @dev: Device to thaw.
1228 *
1229 * Thaw a device under the assumption that its pm_domain field points to the
1230 * domain member of an object of type struct generic_pm_domain representing
1231 * a power domain consisting of I/O devices.
1232 */
1233static int pm_genpd_thaw(struct device *dev)
1234{
1235        struct generic_pm_domain *genpd;
1236
1237        dev_dbg(dev, "%s()\n", __func__);
1238
1239        genpd = dev_to_genpd(dev);
1240        if (IS_ERR(genpd))
1241                return -EINVAL;
1242
1243        return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1244}
1245
1246/**
1247 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1248 * @dev: Device to resume.
1249 *
1250 * Make sure the domain will be in the same power state as before the
1251 * hibernation the system is resuming from and start the device if necessary.
1252 */
1253static int pm_genpd_restore_noirq(struct device *dev)
1254{
1255        struct generic_pm_domain *genpd;
1256
1257        dev_dbg(dev, "%s()\n", __func__);
1258
1259        genpd = dev_to_genpd(dev);
1260        if (IS_ERR(genpd))
1261                return -EINVAL;
1262
1263        /*
1264         * Since all of the "noirq" callbacks are executed sequentially, it is
1265         * guaranteed that this function will never run twice in parallel for
1266         * the same PM domain, so it is not necessary to use locking here.
1267         *
1268         * At this point suspended_count == 0 means we are being run for the
1269         * first time for the given domain in the present cycle.
1270         */
1271        if (genpd->suspended_count++ == 0) {
1272                /*
1273                 * The boot kernel might put the domain into arbitrary state,
1274                 * so make it appear as powered off to pm_genpd_sync_poweron(),
1275                 * so that it tries to power it on in case it was really off.
1276                 */
1277                genpd->status = GPD_STATE_POWER_OFF;
1278                if (genpd->suspend_power_off) {
1279                        /*
1280                         * If the domain was off before the hibernation, make
1281                         * sure it will be off going forward.
1282                         */
1283                        if (genpd->power_off)
1284                                genpd->power_off(genpd);
1285
1286                        return 0;
1287                }
1288        }
1289
1290        if (genpd->suspend_power_off)
1291                return 0;
1292
1293        pm_genpd_sync_poweron(genpd);
1294
1295        return genpd_start_dev(genpd, dev);
1296}
1297
1298/**
1299 * pm_genpd_complete - Complete power transition of a device in a power domain.
1300 * @dev: Device to complete the transition of.
1301 *
1302 * Complete a power transition of a device (during a system-wide power
1303 * transition) under the assumption that its pm_domain field points to the
1304 * domain member of an object of type struct generic_pm_domain representing
1305 * a power domain consisting of I/O devices.
1306 */
1307static void pm_genpd_complete(struct device *dev)
1308{
1309        struct generic_pm_domain *genpd;
1310        bool run_complete;
1311
1312        dev_dbg(dev, "%s()\n", __func__);
1313
1314        genpd = dev_to_genpd(dev);
1315        if (IS_ERR(genpd))
1316                return;
1317
1318        mutex_lock(&genpd->lock);
1319
1320        run_complete = !genpd->suspend_power_off;
1321        if (--genpd->prepared_count == 0)
1322                genpd->suspend_power_off = false;
1323
1324        mutex_unlock(&genpd->lock);
1325
1326        if (run_complete) {
1327                pm_generic_complete(dev);
1328                pm_runtime_set_active(dev);
1329                pm_runtime_enable(dev);
1330                pm_request_idle(dev);
1331        }
1332}
1333
1334/**
1335 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1336 * @dev: Device that normally is marked as "always on" to switch power for.
1337 *
1338 * This routine may only be called during the system core (syscore) suspend or
1339 * resume phase for devices whose "always on" flags are set.
1340 */
1341void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1342{
1343        struct generic_pm_domain *genpd;
1344
1345        genpd = dev_to_genpd(dev);
1346        if (!pm_genpd_present(genpd))
1347                return;
1348
1349        if (suspend) {
1350                genpd->suspended_count++;
1351                pm_genpd_sync_poweroff(genpd);
1352        } else {
1353                pm_genpd_sync_poweron(genpd);
1354                genpd->suspended_count--;
1355        }
1356}
1357EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1358
1359#else
1360
1361#define pm_genpd_prepare                NULL
1362#define pm_genpd_suspend                NULL
1363#define pm_genpd_suspend_late           NULL
1364#define pm_genpd_suspend_noirq          NULL
1365#define pm_genpd_resume_early           NULL
1366#define pm_genpd_resume_noirq           NULL
1367#define pm_genpd_resume                 NULL
1368#define pm_genpd_freeze                 NULL
1369#define pm_genpd_freeze_late            NULL
1370#define pm_genpd_freeze_noirq           NULL
1371#define pm_genpd_thaw_early             NULL
1372#define pm_genpd_thaw_noirq             NULL
1373#define pm_genpd_thaw                   NULL
1374#define pm_genpd_restore_noirq          NULL
1375#define pm_genpd_complete               NULL
1376
1377#endif /* CONFIG_PM_SLEEP */
1378
1379static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1380{
1381        struct generic_pm_domain_data *gpd_data;
1382
1383        gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1384        if (!gpd_data)
1385                return NULL;
1386
1387        mutex_init(&gpd_data->lock);
1388        gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1389        dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1390        return gpd_data;
1391}
1392
1393static void __pm_genpd_free_dev_data(struct device *dev,
1394                                     struct generic_pm_domain_data *gpd_data)
1395{
1396        dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1397        kfree(gpd_data);
1398}
1399
1400/**
1401 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1402 * @genpd: PM domain to add the device to.
1403 * @dev: Device to be added.
1404 * @td: Set of PM QoS timing parameters to attach to the device.
1405 */
1406int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1407                          struct gpd_timing_data *td)
1408{
1409        struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1410        struct pm_domain_data *pdd;
1411        int ret = 0;
1412
1413        dev_dbg(dev, "%s()\n", __func__);
1414
1415        if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1416                return -EINVAL;
1417
1418        gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1419        if (!gpd_data_new)
1420                return -ENOMEM;
1421
1422        genpd_acquire_lock(genpd);
1423
1424        if (genpd->prepared_count > 0) {
1425                ret = -EAGAIN;
1426                goto out;
1427        }
1428
1429        list_for_each_entry(pdd, &genpd->dev_list, list_node)
1430                if (pdd->dev == dev) {
1431                        ret = -EINVAL;
1432                        goto out;
1433                }
1434
1435        ret = dev_pm_get_subsys_data(dev);
1436        if (ret)
1437                goto out;
1438
1439        genpd->device_count++;
1440        genpd->max_off_time_changed = true;
1441
1442        spin_lock_irq(&dev->power.lock);
1443
1444        dev->pm_domain = &genpd->domain;
1445        if (dev->power.subsys_data->domain_data) {
1446                gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1447        } else {
1448                gpd_data = gpd_data_new;
1449                dev->power.subsys_data->domain_data = &gpd_data->base;
1450        }
1451        gpd_data->refcount++;
1452        if (td)
1453                gpd_data->td = *td;
1454
1455        spin_unlock_irq(&dev->power.lock);
1456
1457        mutex_lock(&gpd_data->lock);
1458        gpd_data->base.dev = dev;
1459        list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1460        gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1461        gpd_data->td.constraint_changed = true;
1462        gpd_data->td.effective_constraint_ns = -1;
1463        mutex_unlock(&gpd_data->lock);
1464
1465 out:
1466        genpd_release_lock(genpd);
1467
1468        if (gpd_data != gpd_data_new)
1469                __pm_genpd_free_dev_data(dev, gpd_data_new);
1470
1471        return ret;
1472}
1473
1474/**
1475 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1476 * @genpd_node: Device tree node pointer representing a PM domain to which the
1477 *   the device is added to.
1478 * @dev: Device to be added.
1479 * @td: Set of PM QoS timing parameters to attach to the device.
1480 */
1481int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1482                             struct gpd_timing_data *td)
1483{
1484        struct generic_pm_domain *genpd = NULL, *gpd;
1485
1486        dev_dbg(dev, "%s()\n", __func__);
1487
1488        if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1489                return -EINVAL;
1490
1491        mutex_lock(&gpd_list_lock);
1492        list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1493                if (gpd->of_node == genpd_node) {
1494                        genpd = gpd;
1495                        break;
1496                }
1497        }
1498        mutex_unlock(&gpd_list_lock);
1499
1500        if (!genpd)
1501                return -EINVAL;
1502
1503        return __pm_genpd_add_device(genpd, dev, td);
1504}
1505
1506
1507/**
1508 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1509 * @domain_name: Name of the PM domain to add the device to.
1510 * @dev: Device to be added.
1511 * @td: Set of PM QoS timing parameters to attach to the device.
1512 */
1513int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1514                               struct gpd_timing_data *td)
1515{
1516        return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1517}
1518
1519/**
1520 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1521 * @genpd: PM domain to remove the device from.
1522 * @dev: Device to be removed.
1523 */
1524int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1525                           struct device *dev)
1526{
1527        struct generic_pm_domain_data *gpd_data;
1528        struct pm_domain_data *pdd;
1529        bool remove = false;
1530        int ret = 0;
1531
1532        dev_dbg(dev, "%s()\n", __func__);
1533
1534        if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1535            ||  IS_ERR_OR_NULL(dev->pm_domain)
1536            ||  pd_to_genpd(dev->pm_domain) != genpd)
1537                return -EINVAL;
1538
1539        genpd_acquire_lock(genpd);
1540
1541        if (genpd->prepared_count > 0) {
1542                ret = -EAGAIN;
1543                goto out;
1544        }
1545
1546        genpd->device_count--;
1547        genpd->max_off_time_changed = true;
1548
1549        spin_lock_irq(&dev->power.lock);
1550
1551        dev->pm_domain = NULL;
1552        pdd = dev->power.subsys_data->domain_data;
1553        list_del_init(&pdd->list_node);
1554        gpd_data = to_gpd_data(pdd);
1555        if (--gpd_data->refcount == 0) {
1556                dev->power.subsys_data->domain_data = NULL;
1557                remove = true;
1558        }
1559
1560        spin_unlock_irq(&dev->power.lock);
1561
1562        mutex_lock(&gpd_data->lock);
1563        pdd->dev = NULL;
1564        mutex_unlock(&gpd_data->lock);
1565
1566        genpd_release_lock(genpd);
1567
1568        dev_pm_put_subsys_data(dev);
1569        if (remove)
1570                __pm_genpd_free_dev_data(dev, gpd_data);
1571
1572        return 0;
1573
1574 out:
1575        genpd_release_lock(genpd);
1576
1577        return ret;
1578}
1579
1580/**
1581 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1582 * @dev: Device to set/unset the flag for.
1583 * @val: The new value of the device's "need restore" flag.
1584 */
1585void pm_genpd_dev_need_restore(struct device *dev, bool val)
1586{
1587        struct pm_subsys_data *psd;
1588        unsigned long flags;
1589
1590        spin_lock_irqsave(&dev->power.lock, flags);
1591
1592        psd = dev_to_psd(dev);
1593        if (psd && psd->domain_data)
1594                to_gpd_data(psd->domain_data)->need_restore = val;
1595
1596        spin_unlock_irqrestore(&dev->power.lock, flags);
1597}
1598EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1599
1600/**
1601 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1602 * @genpd: Master PM domain to add the subdomain to.
1603 * @subdomain: Subdomain to be added.
1604 */
1605int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1606                           struct generic_pm_domain *subdomain)
1607{
1608        struct gpd_link *link;
1609        int ret = 0;
1610
1611        if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1612            || genpd == subdomain)
1613                return -EINVAL;
1614
1615 start:
1616        genpd_acquire_lock(genpd);
1617        mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1618
1619        if (subdomain->status != GPD_STATE_POWER_OFF
1620            && subdomain->status != GPD_STATE_ACTIVE) {
1621                mutex_unlock(&subdomain->lock);
1622                genpd_release_lock(genpd);
1623                goto start;
1624        }
1625
1626        if (genpd->status == GPD_STATE_POWER_OFF
1627            &&  subdomain->status != GPD_STATE_POWER_OFF) {
1628                ret = -EINVAL;
1629                goto out;
1630        }
1631
1632        list_for_each_entry(link, &genpd->master_links, master_node) {
1633                if (link->slave == subdomain && link->master == genpd) {
1634                        ret = -EINVAL;
1635                        goto out;
1636                }
1637        }
1638
1639        link = kzalloc(sizeof(*link), GFP_KERNEL);
1640        if (!link) {
1641                ret = -ENOMEM;
1642                goto out;
1643        }
1644        link->master = genpd;
1645        list_add_tail(&link->master_node, &genpd->master_links);
1646        link->slave = subdomain;
1647        list_add_tail(&link->slave_node, &subdomain->slave_links);
1648        if (subdomain->status != GPD_STATE_POWER_OFF)
1649                genpd_sd_counter_inc(genpd);
1650
1651 out:
1652        mutex_unlock(&subdomain->lock);
1653        genpd_release_lock(genpd);
1654
1655        return ret;
1656}
1657
1658/**
1659 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1660 * @master_name: Name of the master PM domain to add the subdomain to.
1661 * @subdomain_name: Name of the subdomain to be added.
1662 */
1663int pm_genpd_add_subdomain_names(const char *master_name,
1664                                 const char *subdomain_name)
1665{
1666        struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1667
1668        if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1669                return -EINVAL;
1670
1671        mutex_lock(&gpd_list_lock);
1672        list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1673                if (!master && !strcmp(gpd->name, master_name))
1674                        master = gpd;
1675
1676                if (!subdomain && !strcmp(gpd->name, subdomain_name))
1677                        subdomain = gpd;
1678
1679                if (master && subdomain)
1680                        break;
1681        }
1682        mutex_unlock(&gpd_list_lock);
1683
1684        return pm_genpd_add_subdomain(master, subdomain);
1685}
1686
1687/**
1688 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1689 * @genpd: Master PM domain to remove the subdomain from.
1690 * @subdomain: Subdomain to be removed.
1691 */
1692int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1693                              struct generic_pm_domain *subdomain)
1694{
1695        struct gpd_link *link;
1696        int ret = -EINVAL;
1697
1698        if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1699                return -EINVAL;
1700
1701 start:
1702        genpd_acquire_lock(genpd);
1703
1704        list_for_each_entry(link, &genpd->master_links, master_node) {
1705                if (link->slave != subdomain)
1706                        continue;
1707
1708                mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1709
1710                if (subdomain->status != GPD_STATE_POWER_OFF
1711                    && subdomain->status != GPD_STATE_ACTIVE) {
1712                        mutex_unlock(&subdomain->lock);
1713                        genpd_release_lock(genpd);
1714                        goto start;
1715                }
1716
1717                list_del(&link->master_node);
1718                list_del(&link->slave_node);
1719                kfree(link);
1720                if (subdomain->status != GPD_STATE_POWER_OFF)
1721                        genpd_sd_counter_dec(genpd);
1722
1723                mutex_unlock(&subdomain->lock);
1724
1725                ret = 0;
1726                break;
1727        }
1728
1729        genpd_release_lock(genpd);
1730
1731        return ret;
1732}
1733
1734/**
1735 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1736 * @dev: Device to add the callbacks to.
1737 * @ops: Set of callbacks to add.
1738 * @td: Timing data to add to the device along with the callbacks (optional).
1739 *
1740 * Every call to this routine should be balanced with a call to
1741 * __pm_genpd_remove_callbacks() and they must not be nested.
1742 */
1743int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1744                           struct gpd_timing_data *td)
1745{
1746        struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1747        int ret = 0;
1748
1749        if (!(dev && ops))
1750                return -EINVAL;
1751
1752        gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1753        if (!gpd_data_new)
1754                return -ENOMEM;
1755
1756        pm_runtime_disable(dev);
1757        device_pm_lock();
1758
1759        ret = dev_pm_get_subsys_data(dev);
1760        if (ret)
1761                goto out;
1762
1763        spin_lock_irq(&dev->power.lock);
1764
1765        if (dev->power.subsys_data->domain_data) {
1766                gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1767        } else {
1768                gpd_data = gpd_data_new;
1769                dev->power.subsys_data->domain_data = &gpd_data->base;
1770        }
1771        gpd_data->refcount++;
1772        gpd_data->ops = *ops;
1773        if (td)
1774                gpd_data->td = *td;
1775
1776        spin_unlock_irq(&dev->power.lock);
1777
1778 out:
1779        device_pm_unlock();
1780        pm_runtime_enable(dev);
1781
1782        if (gpd_data != gpd_data_new)
1783                __pm_genpd_free_dev_data(dev, gpd_data_new);
1784
1785        return ret;
1786}
1787EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1788
1789/**
1790 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1791 * @dev: Device to remove the callbacks from.
1792 * @clear_td: If set, clear the device's timing data too.
1793 *
1794 * This routine can only be called after pm_genpd_add_callbacks().
1795 */
1796int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1797{
1798        struct generic_pm_domain_data *gpd_data = NULL;
1799        bool remove = false;
1800        int ret = 0;
1801
1802        if (!(dev && dev->power.subsys_data))
1803                return -EINVAL;
1804
1805        pm_runtime_disable(dev);
1806        device_pm_lock();
1807
1808        spin_lock_irq(&dev->power.lock);
1809
1810        if (dev->power.subsys_data->domain_data) {
1811                gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1812                gpd_data->ops = (struct gpd_dev_ops){ NULL };
1813                if (clear_td)
1814                        gpd_data->td = (struct gpd_timing_data){ 0 };
1815
1816                if (--gpd_data->refcount == 0) {
1817                        dev->power.subsys_data->domain_data = NULL;
1818                        remove = true;
1819                }
1820        } else {
1821                ret = -EINVAL;
1822        }
1823
1824        spin_unlock_irq(&dev->power.lock);
1825
1826        device_pm_unlock();
1827        pm_runtime_enable(dev);
1828
1829        if (ret)
1830                return ret;
1831
1832        dev_pm_put_subsys_data(dev);
1833        if (remove)
1834                __pm_genpd_free_dev_data(dev, gpd_data);
1835
1836        return 0;
1837}
1838EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1839
1840/**
1841 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1842 * @genpd: PM domain to be connected with cpuidle.
1843 * @state: cpuidle state this domain can disable/enable.
1844 *
1845 * Make a PM domain behave as though it contained a CPU core, that is, instead
1846 * of calling its power down routine it will enable the given cpuidle state so
1847 * that the cpuidle subsystem can power it down (if possible and desirable).
1848 */
1849int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1850{
1851        struct cpuidle_driver *cpuidle_drv;
1852        struct gpd_cpu_data *cpu_data;
1853        struct cpuidle_state *idle_state;
1854        int ret = 0;
1855
1856        if (IS_ERR_OR_NULL(genpd) || state < 0)
1857                return -EINVAL;
1858
1859        genpd_acquire_lock(genpd);
1860
1861        if (genpd->cpu_data) {
1862                ret = -EEXIST;
1863                goto out;
1864        }
1865        cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1866        if (!cpu_data) {
1867                ret = -ENOMEM;
1868                goto out;
1869        }
1870        cpuidle_drv = cpuidle_driver_ref();
1871        if (!cpuidle_drv) {
1872                ret = -ENODEV;
1873                goto err_drv;
1874        }
1875        if (cpuidle_drv->state_count <= state) {
1876                ret = -EINVAL;
1877                goto err;
1878        }
1879        idle_state = &cpuidle_drv->states[state];
1880        if (!idle_state->disabled) {
1881                ret = -EAGAIN;
1882                goto err;
1883        }
1884        cpu_data->idle_state = idle_state;
1885        cpu_data->saved_exit_latency = idle_state->exit_latency;
1886        genpd->cpu_data = cpu_data;
1887        genpd_recalc_cpu_exit_latency(genpd);
1888
1889 out:
1890        genpd_release_lock(genpd);
1891        return ret;
1892
1893 err:
1894        cpuidle_driver_unref();
1895
1896 err_drv:
1897        kfree(cpu_data);
1898        goto out;
1899}
1900
1901/**
1902 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1903 * @name: Name of the domain to connect to cpuidle.
1904 * @state: cpuidle state this domain can manipulate.
1905 */
1906int pm_genpd_name_attach_cpuidle(const char *name, int state)
1907{
1908        return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1909}
1910
1911/**
1912 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1913 * @genpd: PM domain to remove the cpuidle connection from.
1914 *
1915 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1916 * given PM domain.
1917 */
1918int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1919{
1920        struct gpd_cpu_data *cpu_data;
1921        struct cpuidle_state *idle_state;
1922        int ret = 0;
1923
1924        if (IS_ERR_OR_NULL(genpd))
1925                return -EINVAL;
1926
1927        genpd_acquire_lock(genpd);
1928
1929        cpu_data = genpd->cpu_data;
1930        if (!cpu_data) {
1931                ret = -ENODEV;
1932                goto out;
1933        }
1934        idle_state = cpu_data->idle_state;
1935        if (!idle_state->disabled) {
1936                ret = -EAGAIN;
1937                goto out;
1938        }
1939        idle_state->exit_latency = cpu_data->saved_exit_latency;
1940        cpuidle_driver_unref();
1941        genpd->cpu_data = NULL;
1942        kfree(cpu_data);
1943
1944 out:
1945        genpd_release_lock(genpd);
1946        return ret;
1947}
1948
1949/**
1950 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1951 * @name: Name of the domain to disconnect cpuidle from.
1952 */
1953int pm_genpd_name_detach_cpuidle(const char *name)
1954{
1955        return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1956}
1957
1958/* Default device callbacks for generic PM domains. */
1959
1960/**
1961 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1962 * @dev: Device to handle.
1963 */
1964static int pm_genpd_default_save_state(struct device *dev)
1965{
1966        int (*cb)(struct device *__dev);
1967
1968        cb = dev_gpd_data(dev)->ops.save_state;
1969        if (cb)
1970                return cb(dev);
1971
1972        if (dev->type && dev->type->pm)
1973                cb = dev->type->pm->runtime_suspend;
1974        else if (dev->class && dev->class->pm)
1975                cb = dev->class->pm->runtime_suspend;
1976        else if (dev->bus && dev->bus->pm)
1977                cb = dev->bus->pm->runtime_suspend;
1978        else
1979                cb = NULL;
1980
1981        if (!cb && dev->driver && dev->driver->pm)
1982                cb = dev->driver->pm->runtime_suspend;
1983
1984        return cb ? cb(dev) : 0;
1985}
1986
1987/**
1988 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1989 * @dev: Device to handle.
1990 */
1991static int pm_genpd_default_restore_state(struct device *dev)
1992{
1993        int (*cb)(struct device *__dev);
1994
1995        cb = dev_gpd_data(dev)->ops.restore_state;
1996        if (cb)
1997                return cb(dev);
1998
1999        if (dev->type && dev->type->pm)
2000                cb = dev->type->pm->runtime_resume;
2001        else if (dev->class && dev->class->pm)
2002                cb = dev->class->pm->runtime_resume;
2003        else if (dev->bus && dev->bus->pm)
2004                cb = dev->bus->pm->runtime_resume;
2005        else
2006                cb = NULL;
2007
2008        if (!cb && dev->driver && dev->driver->pm)
2009                cb = dev->driver->pm->runtime_resume;
2010
2011        return cb ? cb(dev) : 0;
2012}
2013
2014#ifdef CONFIG_PM_SLEEP
2015
2016/**
2017 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2018 * @dev: Device to handle.
2019 */
2020static int pm_genpd_default_suspend(struct device *dev)
2021{
2022        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
2023
2024        return cb ? cb(dev) : pm_generic_suspend(dev);
2025}
2026
2027/**
2028 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2029 * @dev: Device to handle.
2030 */
2031static int pm_genpd_default_suspend_late(struct device *dev)
2032{
2033        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
2034
2035        return cb ? cb(dev) : pm_generic_suspend_late(dev);
2036}
2037
2038/**
2039 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2040 * @dev: Device to handle.
2041 */
2042static int pm_genpd_default_resume_early(struct device *dev)
2043{
2044        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
2045
2046        return cb ? cb(dev) : pm_generic_resume_early(dev);
2047}
2048
2049/**
2050 * pm_genpd_default_resume - Default "device resume" for PM domians.
2051 * @dev: Device to handle.
2052 */
2053static int pm_genpd_default_resume(struct device *dev)
2054{
2055        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
2056
2057        return cb ? cb(dev) : pm_generic_resume(dev);
2058}
2059
2060/**
2061 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2062 * @dev: Device to handle.
2063 */
2064static int pm_genpd_default_freeze(struct device *dev)
2065{
2066        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
2067
2068        return cb ? cb(dev) : pm_generic_freeze(dev);
2069}
2070
2071/**
2072 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2073 * @dev: Device to handle.
2074 */
2075static int pm_genpd_default_freeze_late(struct device *dev)
2076{
2077        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
2078
2079        return cb ? cb(dev) : pm_generic_freeze_late(dev);
2080}
2081
2082/**
2083 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2084 * @dev: Device to handle.
2085 */
2086static int pm_genpd_default_thaw_early(struct device *dev)
2087{
2088        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
2089
2090        return cb ? cb(dev) : pm_generic_thaw_early(dev);
2091}
2092
2093/**
2094 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2095 * @dev: Device to handle.
2096 */
2097static int pm_genpd_default_thaw(struct device *dev)
2098{
2099        int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
2100
2101        return cb ? cb(dev) : pm_generic_thaw(dev);
2102}
2103
2104#else /* !CONFIG_PM_SLEEP */
2105
2106#define pm_genpd_default_suspend        NULL
2107#define pm_genpd_default_suspend_late   NULL
2108#define pm_genpd_default_resume_early   NULL
2109#define pm_genpd_default_resume         NULL
2110#define pm_genpd_default_freeze         NULL
2111#define pm_genpd_default_freeze_late    NULL
2112#define pm_genpd_default_thaw_early     NULL
2113#define pm_genpd_default_thaw           NULL
2114
2115#endif /* !CONFIG_PM_SLEEP */
2116
2117/**
2118 * pm_genpd_init - Initialize a generic I/O PM domain object.
2119 * @genpd: PM domain object to initialize.
2120 * @gov: PM domain governor to associate with the domain (may be NULL).
2121 * @is_off: Initial value of the domain's power_is_off field.
2122 */
2123void pm_genpd_init(struct generic_pm_domain *genpd,
2124                   struct dev_power_governor *gov, bool is_off)
2125{
2126        if (IS_ERR_OR_NULL(genpd))
2127                return;
2128
2129        INIT_LIST_HEAD(&genpd->master_links);
2130        INIT_LIST_HEAD(&genpd->slave_links);
2131        INIT_LIST_HEAD(&genpd->dev_list);
2132        mutex_init(&genpd->lock);
2133        genpd->gov = gov;
2134        INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2135        genpd->in_progress = 0;
2136        atomic_set(&genpd->sd_count, 0);
2137        genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2138        init_waitqueue_head(&genpd->status_wait_queue);
2139        genpd->poweroff_task = NULL;
2140        genpd->resume_count = 0;
2141        genpd->device_count = 0;
2142        genpd->max_off_time_ns = -1;
2143        genpd->max_off_time_changed = true;
2144        genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2145        genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2146        genpd->domain.ops.prepare = pm_genpd_prepare;
2147        genpd->domain.ops.suspend = pm_genpd_suspend;
2148        genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
2149        genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2150        genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
2151        genpd->domain.ops.resume_early = pm_genpd_resume_early;
2152        genpd->domain.ops.resume = pm_genpd_resume;
2153        genpd->domain.ops.freeze = pm_genpd_freeze;
2154        genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
2155        genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2156        genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
2157        genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
2158        genpd->domain.ops.thaw = pm_genpd_thaw;
2159        genpd->domain.ops.poweroff = pm_genpd_suspend;
2160        genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
2161        genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
2162        genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
2163        genpd->domain.ops.restore_early = pm_genpd_resume_early;
2164        genpd->domain.ops.restore = pm_genpd_resume;
2165        genpd->domain.ops.complete = pm_genpd_complete;
2166        genpd->dev_ops.save_state = pm_genpd_default_save_state;
2167        genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
2168        genpd->dev_ops.suspend = pm_genpd_default_suspend;
2169        genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2170        genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2171        genpd->dev_ops.resume = pm_genpd_default_resume;
2172        genpd->dev_ops.freeze = pm_genpd_default_freeze;
2173        genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2174        genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2175        genpd->dev_ops.thaw = pm_genpd_default_thaw;
2176        mutex_lock(&gpd_list_lock);
2177        list_add(&genpd->gpd_list_node, &gpd_list);
2178        mutex_unlock(&gpd_list_lock);
2179}
2180