Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Linux/drivers/pmdomain/   (Open Source Betriebssystem Version 6.17.9©)  Datei vom 24.10.2025 mit Größe 99 kB image not shown  

Quelle  core.c   Sprache: C

 
// SPDX-License-Identifier: GPL-2.0
/*
 * drivers/base/power/domain.c - Common code related to device power domains.
 *
 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 */

#define pr_fmt(fmt) "PM: " fmt

#include <linux/delay.h>
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/pm_opp.h>
#include <linux/pm_runtime.h>
#include <linux/pm_domain.h>
#include <linux/pm_qos.h>
#include <linux/pm_clock.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/suspend.h>
#include <linux/export.h>
#include <linux/cpu.h>
#include <linux/debugfs.h>

/* Provides a unique ID for each genpd device */
static DEFINE_IDA(genpd_ida);

/* The bus for genpd_providers. */
static const struct bus_type genpd_provider_bus_type = {
 .name  = "genpd_provider",
};

/* The parent for genpd_provider devices. */
static struct device genpd_provider_bus = {
 .init_name = "genpd_provider",
};

#define GENPD_RETRY_MAX_MS 250  /* Approximate */

#define GENPD_DEV_CALLBACK(genpd, type, callback, dev)  \
({        \
 type (*__routine)(struct device *__d);    \
 type __ret = (type)0;     \
        \
 __routine = genpd->dev_ops.callback;    \
 if (__routine) {     \
  __ret = __routine(dev);    \
 }       \
 __ret;       \
})

static LIST_HEAD(gpd_list);
static DEFINE_MUTEX(gpd_list_lock);

struct genpd_lock_ops {
 void (*lock)(struct generic_pm_domain *genpd);
 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
 int (*lock_interruptible)(struct generic_pm_domain *genpd);
 void (*unlock)(struct generic_pm_domain *genpd);
};

static void genpd_lock_mtx(struct generic_pm_domain *genpd)
{
 mutex_lock(&genpd->mlock);
}

static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
     int depth)
{
 mutex_lock_nested(&genpd->mlock, depth);
}

static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
{
 return mutex_lock_interruptible(&genpd->mlock);
}

static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
{
 return mutex_unlock(&genpd->mlock);
}

static const struct genpd_lock_ops genpd_mtx_ops = {
 .lock = genpd_lock_mtx,
 .lock_nested = genpd_lock_nested_mtx,
 .lock_interruptible = genpd_lock_interruptible_mtx,
 .unlock = genpd_unlock_mtx,
};

static void genpd_lock_spin(struct generic_pm_domain *genpd)
 __acquires(&genpd->slock)
{
 unsigned long flags;

 spin_lock_irqsave(&genpd->slock, flags);
 genpd->lock_flags = flags;
}

static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
     int depth)
 __acquires(&genpd->slock)
{
 unsigned long flags;

 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
 genpd->lock_flags = flags;
}

static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
 __acquires(&genpd->slock)
{
 unsigned long flags;

 spin_lock_irqsave(&genpd->slock, flags);
 genpd->lock_flags = flags;
 return 0;
}

static void genpd_unlock_spin(struct generic_pm_domain *genpd)
 __releases(&genpd->slock)
{
 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
}

static const struct genpd_lock_ops genpd_spin_ops = {
 .lock = genpd_lock_spin,
 .lock_nested = genpd_lock_nested_spin,
 .lock_interruptible = genpd_lock_interruptible_spin,
 .unlock = genpd_unlock_spin,
};

static void genpd_lock_raw_spin(struct generic_pm_domain *genpd)
 __acquires(&genpd->raw_slock)
{
 unsigned long flags;

 raw_spin_lock_irqsave(&genpd->raw_slock, flags);
 genpd->raw_lock_flags = flags;
}

static void genpd_lock_nested_raw_spin(struct generic_pm_domain *genpd,
     int depth)
 __acquires(&genpd->raw_slock)
{
 unsigned long flags;

 raw_spin_lock_irqsave_nested(&genpd->raw_slock, flags, depth);
 genpd->raw_lock_flags = flags;
}

static int genpd_lock_interruptible_raw_spin(struct generic_pm_domain *genpd)
 __acquires(&genpd->raw_slock)
{
 unsigned long flags;

 raw_spin_lock_irqsave(&genpd->raw_slock, flags);
 genpd->raw_lock_flags = flags;
 return 0;
}

static void genpd_unlock_raw_spin(struct generic_pm_domain *genpd)
 __releases(&genpd->raw_slock)
{
 raw_spin_unlock_irqrestore(&genpd->raw_slock, genpd->raw_lock_flags);
}

static const struct genpd_lock_ops genpd_raw_spin_ops = {
 .lock = genpd_lock_raw_spin,
 .lock_nested = genpd_lock_nested_raw_spin,
 .lock_interruptible = genpd_lock_interruptible_raw_spin,
 .unlock = genpd_unlock_raw_spin,
};

#define genpd_lock(p)   p->lock_ops->lock(p)
#define genpd_lock_nested(p, d)  p->lock_ops->lock_nested(p, d)
#define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
#define genpd_unlock(p)   p->lock_ops->unlock(p)

#define genpd_status_on(genpd)  (genpd->status == GENPD_STATE_ON)
#define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
#define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
#define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
#define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
#define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
#define genpd_is_opp_table_fw(genpd) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
#define genpd_is_dev_name_fw(genpd) (genpd->flags & GENPD_FLAG_DEV_NAME_FW)
#define genpd_is_no_sync_state(genpd) (genpd->flags & GENPD_FLAG_NO_SYNC_STATE)
#define genpd_is_no_stay_on(genpd) (genpd->flags & GENPD_FLAG_NO_STAY_ON)

static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
  const struct generic_pm_domain *genpd)
{
 bool ret;

 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);

 /*
 * Warn once if an IRQ safe device is attached to a domain, which
 * callbacks are allowed to sleep. This indicates a suboptimal
 * configuration for PM, but it doesn't matter for an always on domain.
 */

 if (genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd))
  return ret;

 if (ret)
  dev_warn_once(dev, "PM domain %s will not be powered off\n",
         dev_name(&genpd->dev));

 return ret;
}

static int genpd_runtime_suspend(struct device *dev);

/*
 * Get the generic PM domain for a particular struct device.
 * This validates the struct device pointer, the PM domain pointer,
 * and checks that the PM domain pointer is a real generic PM domain.
 * Any failure results in NULL being returned.
 */

static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
{
 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
  return NULL;

 /* A genpd's always have its ->runtime_suspend() callback assigned. */
 if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
  return pd_to_genpd(dev->pm_domain);

 return NULL;
}

/*
 * This should only be used where we are certain that the pm_domain
 * attached to the device is a genpd domain.
 */

static struct generic_pm_domain *dev_to_genpd(struct device *dev)
{
 if (IS_ERR_OR_NULL(dev->pm_domain))
  return ERR_PTR(-EINVAL);

 return pd_to_genpd(dev->pm_domain);
}

struct device *dev_to_genpd_dev(struct device *dev)
{
 struct generic_pm_domain *genpd = dev_to_genpd(dev);

 if (IS_ERR(genpd))
  return ERR_CAST(genpd);

 return &genpd->dev;
}

static int genpd_stop_dev(const struct generic_pm_domain *genpd,
     struct device *dev)
{
 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
}

static int genpd_start_dev(const struct generic_pm_domain *genpd,
      struct device *dev)
{
 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
}

static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
{
 bool ret = false;

 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  ret = !!atomic_dec_and_test(&genpd->sd_count);

 return ret;
}

static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
{
 atomic_inc(&genpd->sd_count);
 smp_mb__after_atomic();
}

#ifdef CONFIG_DEBUG_FS
static struct dentry *genpd_debugfs_dir;

static void genpd_debug_add(struct generic_pm_domain *genpd);

static void genpd_debug_remove(struct generic_pm_domain *genpd)
{
 if (!genpd_debugfs_dir)
  return;

 debugfs_lookup_and_remove(dev_name(&genpd->dev), genpd_debugfs_dir);
}

static void genpd_update_accounting(struct generic_pm_domain *genpd)
{
 u64 delta, now;

 now = ktime_get_mono_fast_ns();
 if (now <= genpd->accounting_time)
  return;

 delta = now - genpd->accounting_time;

 /*
 * If genpd->status is active, it means we are just
 * out of off and so update the idle time and vice
 * versa.
 */

 if (genpd->status == GENPD_STATE_ON)
  genpd->states[genpd->state_idx].idle_time += delta;
 else
  genpd->on_time += delta;

 genpd->accounting_time = now;
}

static void genpd_reflect_residency(struct generic_pm_domain *genpd)
{
 struct genpd_governor_data *gd = genpd->gd;
 struct genpd_power_state *state, *next_state;
 unsigned int state_idx;
 s64 sleep_ns, target_ns;

 if (!gd || !gd->reflect_residency)
  return;

 sleep_ns = ktime_to_ns(ktime_sub(ktime_get(), gd->last_enter));
 state_idx = genpd->state_idx;
 state = &genpd->states[state_idx];
 target_ns = state->power_off_latency_ns + state->residency_ns;

 if (sleep_ns < target_ns) {
  state->above++;
 } else if (state_idx < (genpd->state_count -1)) {
  next_state = &genpd->states[state_idx + 1];
  target_ns = next_state->power_off_latency_ns +
   next_state->residency_ns;

  if (sleep_ns >= target_ns)
   state->below++;
 }

 gd->reflect_residency = false;
}
#else
static inline void genpd_debug_add(struct generic_pm_domain *genpd) {}
static inline void genpd_debug_remove(struct generic_pm_domain *genpd) {}
static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
static inline void genpd_reflect_residency(struct generic_pm_domain *genpd) {}
#endif

static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
        unsigned int state)
{
 struct generic_pm_domain_data *pd_data;
 struct pm_domain_data *pdd;
 struct gpd_link *link;

 /* New requested state is same as Max requested state */
 if (state == genpd->performance_state)
  return state;

 /* New requested state is higher than Max requested state */
 if (state > genpd->performance_state)
  return state;

 /* Traverse all devices within the domain */
 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  pd_data = to_gpd_data(pdd);

  if (pd_data->performance_state > state)
   state = pd_data->performance_state;
 }

 /*
 * Traverse all sub-domains within the domain. This can be
 * done without any additional locking as the link->performance_state
 * field is protected by the parent genpd->lock, which is already taken.
 *
 * Also note that link->performance_state (subdomain's performance state
 * requirement to parent domain) is different from
 * link->child->performance_state (current performance state requirement
 * of the devices/sub-domains of the subdomain) and so can have a
 * different value.
 *
 * Note that we also take vote from powered-off sub-domains into account
 * as the same is done for devices right now.
 */

 list_for_each_entry(link, &genpd->parent_links, parent_node) {
  if (link->performance_state > state)
   state = link->performance_state;
 }

 return state;
}

static int genpd_xlate_performance_state(struct generic_pm_domain *genpd,
      struct generic_pm_domain *parent,
      unsigned int pstate)
{
 if (!parent->set_performance_state)
  return pstate;

 return dev_pm_opp_xlate_performance_state(genpd->opp_table,
        parent->opp_table,
        pstate);
}

static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
     unsigned int state, int depth);

static void _genpd_rollback_parent_state(struct gpd_link *link, int depth)
{
 struct generic_pm_domain *parent = link->parent;
 int parent_state;

 genpd_lock_nested(parent, depth + 1);

 parent_state = link->prev_performance_state;
 link->performance_state = parent_state;

 parent_state = _genpd_reeval_performance_state(parent, parent_state);
 if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
  pr_err("%s: Failed to roll back to %d performance state\n",
         parent->name, parent_state);
 }

 genpd_unlock(parent);
}

static int _genpd_set_parent_state(struct generic_pm_domain *genpd,
       struct gpd_link *link,
       unsigned int state, int depth)
{
 struct generic_pm_domain *parent = link->parent;
 int parent_state, ret;

 /* Find parent's performance state */
 ret = genpd_xlate_performance_state(genpd, parent, state);
 if (unlikely(ret < 0))
  return ret;

 parent_state = ret;

 genpd_lock_nested(parent, depth + 1);

 link->prev_performance_state = link->performance_state;
 link->performance_state = parent_state;

 parent_state = _genpd_reeval_performance_state(parent, parent_state);
 ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
 if (ret)
  link->performance_state = link->prev_performance_state;

 genpd_unlock(parent);

 return ret;
}

static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
     unsigned int state, int depth)
{
 struct gpd_link *link = NULL;
 int ret;

 if (state == genpd->performance_state)
  return 0;

 /* When scaling up, propagate to parents first in normal order */
 if (state > genpd->performance_state) {
  list_for_each_entry(link, &genpd->child_links, child_node) {
   ret = _genpd_set_parent_state(genpd, link, state, depth);
   if (ret)
    goto rollback_parents_up;
  }
 }

 if (genpd->set_performance_state) {
  ret = genpd->set_performance_state(genpd, state);
  if (ret) {
   if (link)
    goto rollback_parents_up;
   return ret;
  }
 }

 /* When scaling down, propagate to parents last in reverse order */
 if (state < genpd->performance_state) {
  list_for_each_entry_reverse(link, &genpd->child_links, child_node) {
   ret = _genpd_set_parent_state(genpd, link, state, depth);
   if (ret)
    goto rollback_parents_down;
  }
 }

 genpd->performance_state = state;
 return 0;

rollback_parents_up:
 list_for_each_entry_continue_reverse(link, &genpd->child_links, child_node)
  _genpd_rollback_parent_state(link, depth);
 return ret;
rollback_parents_down:
 list_for_each_entry_continue(link, &genpd->child_links, child_node)
  _genpd_rollback_parent_state(link, depth);
 return ret;
}

static int genpd_set_performance_state(struct device *dev, unsigned int state)
{
 struct generic_pm_domain *genpd = dev_to_genpd(dev);
 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
 unsigned int prev_state;
 int ret;

 prev_state = gpd_data->performance_state;
 if (prev_state == state)
  return 0;

 gpd_data->performance_state = state;
 state = _genpd_reeval_performance_state(genpd, state);

 ret = _genpd_set_performance_state(genpd, state, 0);
 if (ret)
  gpd_data->performance_state = prev_state;

 return ret;
}

static int genpd_drop_performance_state(struct device *dev)
{
 unsigned int prev_state = dev_gpd_data(dev)->performance_state;

 if (!genpd_set_performance_state(dev, 0))
  return prev_state;

 return 0;
}

static void genpd_restore_performance_state(struct device *dev,
         unsigned int state)
{
 if (state)
  genpd_set_performance_state(dev, state);
}

static int genpd_dev_pm_set_performance_state(struct device *dev,
           unsigned int state)
{
 struct generic_pm_domain *genpd = dev_to_genpd(dev);
 int ret = 0;

 genpd_lock(genpd);
 if (pm_runtime_suspended(dev)) {
  dev_gpd_data(dev)->rpm_pstate = state;
 } else {
  ret = genpd_set_performance_state(dev, state);
  if (!ret)
   dev_gpd_data(dev)->rpm_pstate = 0;
 }
 genpd_unlock(genpd);

 return ret;
}

/**
 * dev_pm_genpd_set_performance_state- Set performance state of device's power
 * domain.
 *
 * @dev: Device for which the performance-state needs to be set.
 * @state: Target performance state of the device. This can be set as 0 when the
 *    device doesn't have any performance state constraints left (And so
 *    the device wouldn't participate anymore to find the target
 *    performance state of the genpd).
 *
 * It is assumed that the users guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Returns 0 on success and negative error values on failures.
 */

int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
{
 struct generic_pm_domain *genpd;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return -ENODEV;

 if (WARN_ON(!dev->power.subsys_data ||
       !dev->power.subsys_data->domain_data))
  return -EINVAL;

 return genpd_dev_pm_set_performance_state(dev, state);
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);

/**
 * dev_pm_genpd_set_next_wakeup - Notify PM framework of an impending wakeup.
 *
 * @dev: Device to handle
 * @next: impending interrupt/wakeup for the device
 *
 *
 * Allow devices to inform of the next wakeup. It's assumed that the users
 * guarantee that the genpd wouldn't be detached while this routine is getting
 * called. Additionally, it's also assumed that @dev isn't runtime suspended
 * (RPM_SUSPENDED)."
 * Although devices are expected to update the next_wakeup after the end of
 * their usecase as well, it is possible the devices themselves may not know
 * about that, so stale @next will be ignored when powering off the domain.
 */

void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
{
 struct generic_pm_domain *genpd;
 struct gpd_timing_data *td;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return;

 td = to_gpd_data(dev->power.subsys_data->domain_data)->td;
 if (td)
  td->next_wakeup = next;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_set_next_wakeup);

/**
 * dev_pm_genpd_get_next_hrtimer - Return the next_hrtimer for the genpd
 * @dev: A device that is attached to the genpd.
 *
 * This routine should typically be called for a device, at the point of when a
 * GENPD_NOTIFY_PRE_OFF notification has been sent for it.
 *
 * Returns the aggregated value of the genpd's next hrtimer or KTIME_MAX if no
 * valid value have been set.
 */

ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
{
 struct generic_pm_domain *genpd;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return KTIME_MAX;

 if (genpd->gd)
  return genpd->gd->next_hrtimer;

 return KTIME_MAX;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_get_next_hrtimer);

/*
 * dev_pm_genpd_synced_poweroff - Next power off should be synchronous
 *
 * @dev: A device that is attached to the genpd.
 *
 * Allows a consumer of the genpd to notify the provider that the next power off
 * should be synchronous.
 *
 * It is assumed that the users guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 */

void dev_pm_genpd_synced_poweroff(struct device *dev)
{
 struct generic_pm_domain *genpd;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return;

 genpd_lock(genpd);
 genpd->synced_poweroff = true;
 genpd_unlock(genpd);
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_synced_poweroff);

/**
 * dev_pm_genpd_set_hwmode() - Set the HW mode for the device and its PM domain.
 *
 * @dev: Device for which the HW-mode should be changed.
 * @enable: Value to set or unset the HW-mode.
 *
 * Some PM domains can rely on HW signals to control the power for a device. To
 * allow a consumer driver to switch the behaviour for its device in runtime,
 * which may be beneficial from a latency or energy point of view, this function
 * may be called.
 *
 * It is assumed that the users guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Return: Returns 0 on success and negative error values on failures.
 */

int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
{
 struct generic_pm_domain *genpd;
 int ret = 0;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return -ENODEV;

 if (!genpd->set_hwmode_dev)
  return -EOPNOTSUPP;

 genpd_lock(genpd);

 if (dev_gpd_data(dev)->hw_mode == enable)
  goto out;

 ret = genpd->set_hwmode_dev(genpd, dev, enable);
 if (!ret)
  dev_gpd_data(dev)->hw_mode = enable;

out:
 genpd_unlock(genpd);
 return ret;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_set_hwmode);

/**
 * dev_pm_genpd_get_hwmode() - Get the HW mode setting for the device.
 *
 * @dev: Device for which the current HW-mode setting should be fetched.
 *
 * This helper function allows consumer drivers to fetch the current HW mode
 * setting of its the device.
 *
 * It is assumed that the users guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Return: Returns the HW mode setting of device from SW cached hw_mode.
 */

bool dev_pm_genpd_get_hwmode(struct device *dev)
{
 return dev_gpd_data(dev)->hw_mode;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_get_hwmode);

/**
 * dev_pm_genpd_rpm_always_on() - Control if the PM domain can be powered off.
 *
 * @dev: Device for which the PM domain may need to stay on for.
 * @on: Value to set or unset for the condition.
 *
 * For some usecases a consumer driver requires its device to remain power-on
 * from the PM domain perspective during runtime. This function allows the
 * behaviour to be dynamically controlled for a device attached to a genpd.
 *
 * It is assumed that the users guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Return: Returns 0 on success and negative error values on failures.
 */

int dev_pm_genpd_rpm_always_on(struct device *dev, bool on)
{
 struct generic_pm_domain *genpd;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return -ENODEV;

 genpd_lock(genpd);
 dev_gpd_data(dev)->rpm_always_on = on;
 genpd_unlock(genpd);

 return 0;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_rpm_always_on);

/**
 * dev_pm_genpd_is_on() - Get device's current power domain status
 *
 * @dev: Device to get the current power status
 *
 * This function checks whether the generic power domain associated with the
 * given device is on or not by verifying if genpd_status_on equals
 * GENPD_STATE_ON.
 *
 * Note: this function returns the power status of the genpd at the time of the
 * call. The power status may change after due to activity from other devices
 * sharing the same genpd. Therefore, this information should not be relied for
 * long-term decisions about the device power state.
 *
 * Return: 'true' if the device's power domain is on, 'false' otherwise.
 */

bool dev_pm_genpd_is_on(struct device *dev)
{
 struct generic_pm_domain *genpd;
 bool is_on;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return false;

 genpd_lock(genpd);
 is_on = genpd_status_on(genpd);
 genpd_unlock(genpd);

 return is_on;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_is_on);

/**
 * pm_genpd_inc_rejected() - Adjust the rejected/usage counts for an idle-state.
 *
 * @genpd: The PM domain the idle-state belongs to.
 * @state_idx: The index of the idle-state that failed.
 *
 * In some special cases the ->power_off() callback is asynchronously powering
 * off the PM domain, leading to that it may return zero to indicate success,
 * even though the actual power-off could fail. To account for this correctly in
 * the rejected/usage counts for the idle-state statistics, users can call this
 * function to adjust the values.
 *
 * It is assumed that the users guarantee that the genpd doesn't get removed
 * while this routine is getting called.
 */

void pm_genpd_inc_rejected(struct generic_pm_domain *genpd,
      unsigned int state_idx)
{
 genpd_lock(genpd);
 genpd->states[genpd->state_idx].rejected++;
 genpd->states[genpd->state_idx].usage--;
 genpd_unlock(genpd);
}
EXPORT_SYMBOL_GPL(pm_genpd_inc_rejected);

static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
{
 unsigned int state_idx = genpd->state_idx;
 ktime_t time_start;
 s64 elapsed_ns;
 int ret;

 /* Notify consumers that we are about to power on. */
 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
          GENPD_NOTIFY_PRE_ON,
          GENPD_NOTIFY_OFF, NULL);
 ret = notifier_to_errno(ret);
 if (ret)
  return ret;

 if (!genpd->power_on)
  goto out;

 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
 if (!timed) {
  ret = genpd->power_on(genpd);
  if (ret)
   goto err;

  goto out;
 }

 time_start = ktime_get();
 ret = genpd->power_on(genpd);
 if (ret)
  goto err;

 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
  goto out;

 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
 genpd->gd->max_off_time_changed = true;
 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
   dev_name(&genpd->dev), "on", elapsed_ns);

out:
 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
 genpd->synced_poweroff = false;
 return 0;
err:
 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
    NULL);
 return ret;
}

static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
{
 unsigned int state_idx = genpd->state_idx;
 ktime_t time_start;
 s64 elapsed_ns;
 int ret;

 /* Notify consumers that we are about to power off. */
 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
          GENPD_NOTIFY_PRE_OFF,
          GENPD_NOTIFY_ON, NULL);
 ret = notifier_to_errno(ret);
 if (ret)
  return ret;

 if (!genpd->power_off)
  goto out;

 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
 if (!timed) {
  ret = genpd->power_off(genpd);
  if (ret)
   goto busy;

  goto out;
 }

 time_start = ktime_get();
 ret = genpd->power_off(genpd);
 if (ret)
  goto busy;

 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
  goto out;

 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
 genpd->gd->max_off_time_changed = true;
 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
   dev_name(&genpd->dev), "off", elapsed_ns);

out:
 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
    NULL);
 return 0;
busy:
 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
 return ret;
}

/**
 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
 * @genpd: PM domain to power off.
 *
 * Queue up the execution of genpd_power_off() unless it's already been done
 * before.
 */

static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
{
 queue_work(pm_wq, &genpd->power_off_work);
}

/**
 * genpd_power_off - Remove power from a given PM domain.
 * @genpd: PM domain to power down.
 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
 * RPM status of the releated device is in an intermediate state, not yet turned
 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
 * be RPM_SUSPENDED, while it tries to power off the PM domain.
 * @depth: nesting count for lockdep.
 *
 * If all of the @genpd's devices have been suspended and all of its subdomains
 * have been powered down, remove power from @genpd.
 */

static void genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
       unsigned int depth)
{
 struct pm_domain_data *pdd;
 struct gpd_link *link;
 unsigned int not_suspended = 0;

 /*
 * Do not try to power off the domain in the following situations:
 * The domain is already in the "power off" state.
 * System suspend is in progress.
 * The domain is configured as always on.
 * The domain was on at boot and still need to stay on.
 * The domain has a subdomain being powered on.
 */

 if (!genpd_status_on(genpd) || genpd->prepared_count > 0 ||
     genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd) ||
     genpd->stay_on || atomic_read(&genpd->sd_count) > 0)
  return;

 /*
 * The children must be in their deepest (powered-off) states to allow
 * the parent to be powered off. Note that, there's no need for
 * additional locking, as powering on a child, requires the parent's
 * lock to be acquired first.
 */

 list_for_each_entry(link, &genpd->parent_links, parent_node) {
  struct generic_pm_domain *child = link->child;
  if (child->state_idx < child->state_count - 1)
   return;
 }

 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  /*
 * Do not allow PM domain to be powered off, when an IRQ safe
 * device is part of a non-IRQ safe domain.
 */

  if (!pm_runtime_suspended(pdd->dev) ||
   irq_safe_dev_in_sleep_domain(pdd->dev, genpd))
   not_suspended++;

  /* The device may need its PM domain to stay powered on. */
  if (to_gpd_data(pdd)->rpm_always_on)
   return;
 }

 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
  return;

 if (genpd->gov && genpd->gov->power_down_ok) {
  if (!genpd->gov->power_down_ok(&genpd->domain))
   return;
 }

 /* Default to shallowest state. */
 if (!genpd->gov)
  genpd->state_idx = 0;

 /* Don't power off, if a child domain is waiting to power on. */
 if (atomic_read(&genpd->sd_count) > 0)
  return;

 if (_genpd_power_off(genpd, true)) {
  genpd->states[genpd->state_idx].rejected++;
  return;
 }

 genpd->status = GENPD_STATE_OFF;
 genpd_update_accounting(genpd);
 genpd->states[genpd->state_idx].usage++;

 list_for_each_entry(link, &genpd->child_links, child_node) {
  genpd_sd_counter_dec(link->parent);
  genpd_lock_nested(link->parent, depth + 1);
  genpd_power_off(link->parent, false, depth + 1);
  genpd_unlock(link->parent);
 }
}

/**
 * genpd_power_on - Restore power to a given PM domain and its parents.
 * @genpd: PM domain to power up.
 * @depth: nesting count for lockdep.
 *
 * Restore power to @genpd and all of its parents so that it is possible to
 * resume a device belonging to it.
 */

static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
{
 struct gpd_link *link;
 int ret = 0;

 if (genpd_status_on(genpd))
  return 0;

 /* Reflect over the entered idle-states residency for debugfs. */
 genpd_reflect_residency(genpd);

 /*
 * The list is guaranteed not to change while the loop below is being
 * executed, unless one of the parents' .power_on() callbacks fiddles
 * with it.
 */

 list_for_each_entry(link, &genpd->child_links, child_node) {
  struct generic_pm_domain *parent = link->parent;

  genpd_sd_counter_inc(parent);

  genpd_lock_nested(parent, depth + 1);
  ret = genpd_power_on(parent, depth + 1);
  genpd_unlock(parent);

  if (ret) {
   genpd_sd_counter_dec(parent);
   goto err;
  }
 }

 ret = _genpd_power_on(genpd, true);
 if (ret)
  goto err;

 genpd->status = GENPD_STATE_ON;
 genpd_update_accounting(genpd);

 return 0;

 err:
 list_for_each_entry_continue_reverse(link,
     &genpd->child_links,
     child_node) {
  genpd_sd_counter_dec(link->parent);
  genpd_lock_nested(link->parent, depth + 1);
  genpd_power_off(link->parent, false, depth + 1);
  genpd_unlock(link->parent);
 }

 return ret;
}

static int genpd_dev_pm_start(struct device *dev)
{
 struct generic_pm_domain *genpd = dev_to_genpd(dev);

 return genpd_start_dev(genpd, dev);
}

static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
         unsigned long val, void *ptr)
{
 struct generic_pm_domain_data *gpd_data;
 struct device *dev;

 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
 dev = gpd_data->base.dev;

 for (;;) {
  struct generic_pm_domain *genpd = ERR_PTR(-ENODATA);
  struct pm_domain_data *pdd;
  struct gpd_timing_data *td;

  spin_lock_irq(&dev->power.lock);

  pdd = dev->power.subsys_data ?
    dev->power.subsys_data->domain_data : NULL;
  if (pdd) {
   td = to_gpd_data(pdd)->td;
   if (td) {
    td->constraint_changed = true;
    genpd = dev_to_genpd(dev);
   }
  }

  spin_unlock_irq(&dev->power.lock);

  if (!IS_ERR(genpd)) {
   genpd_lock(genpd);
   genpd->gd->max_off_time_changed = true;
   genpd_unlock(genpd);
  }

  dev = dev->parent;
  if (!dev || dev->power.ignore_children)
   break;
 }

 return NOTIFY_DONE;
}

/**
 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
 * @work: Work structure used for scheduling the execution of this function.
 */

static void genpd_power_off_work_fn(struct work_struct *work)
{
 struct generic_pm_domain *genpd;

 genpd = container_of(work, struct generic_pm_domain, power_off_work);

 genpd_lock(genpd);
 genpd_power_off(genpd, false, 0);
 genpd_unlock(genpd);
}

/**
 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
 * @dev: Device to handle.
 */

static int __genpd_runtime_suspend(struct device *dev)
{
 int (*cb)(struct device *__dev);

 if (dev->type && dev->type->pm)
  cb = dev->type->pm->runtime_suspend;
 else if (dev->class && dev->class->pm)
  cb = dev->class->pm->runtime_suspend;
 else if (dev->bus && dev->bus->pm)
  cb = dev->bus->pm->runtime_suspend;
 else
  cb = NULL;

 if (!cb && dev->driver && dev->driver->pm)
  cb = dev->driver->pm->runtime_suspend;

 return cb ? cb(dev) : 0;
}

/**
 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
 * @dev: Device to handle.
 */

static int __genpd_runtime_resume(struct device *dev)
{
 int (*cb)(struct device *__dev);

 if (dev->type && dev->type->pm)
  cb = dev->type->pm->runtime_resume;
 else if (dev->class && dev->class->pm)
  cb = dev->class->pm->runtime_resume;
 else if (dev->bus && dev->bus->pm)
  cb = dev->bus->pm->runtime_resume;
 else
  cb = NULL;

 if (!cb && dev->driver && dev->driver->pm)
  cb = dev->driver->pm->runtime_resume;

 return cb ? cb(dev) : 0;
}

/**
 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
 * @dev: Device to suspend.
 *
 * Carry out a runtime suspend of a device under the assumption that its
 * pm_domain field points to the domain member of an object of type
 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 */

static int genpd_runtime_suspend(struct device *dev)
{
 struct generic_pm_domain *genpd;
 bool (*suspend_ok)(struct device *__dev);
 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
 struct gpd_timing_data *td = gpd_data->td;
 bool runtime_pm = pm_runtime_enabled(dev);
 ktime_t time_start = 0;
 s64 elapsed_ns;
 int ret;

 dev_dbg(dev, "%s()\n", __func__);

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return -EINVAL;

 /*
 * A runtime PM centric subsystem/driver may re-use the runtime PM
 * callbacks for other purposes than runtime PM. In those scenarios
 * runtime PM is disabled. Under these circumstances, we shall skip
 * validating/measuring the PM QoS latency.
 */

 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
 if (runtime_pm && suspend_ok && !suspend_ok(dev))
  return -EBUSY;

 /* Measure suspend latency. */
 if (td && runtime_pm)
  time_start = ktime_get();

 ret = __genpd_runtime_suspend(dev);
 if (ret)
  return ret;

 ret = genpd_stop_dev(genpd, dev);
 if (ret) {
  __genpd_runtime_resume(dev);
  return ret;
 }

 /* Update suspend latency value if the measured time exceeds it. */
 if (td && runtime_pm) {
  elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  if (elapsed_ns > td->suspend_latency_ns) {
   td->suspend_latency_ns = elapsed_ns;
   dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
    elapsed_ns);
   genpd->gd->max_off_time_changed = true;
   td->constraint_changed = true;
  }
 }

 /*
 * If power.irq_safe is set, this routine may be run with
 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
 */

 if (irq_safe_dev_in_sleep_domain(dev, genpd))
  return 0;

 genpd_lock(genpd);
 genpd_power_off(genpd, true, 0);
 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
 genpd_unlock(genpd);

 return 0;
}

/**
 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
 * @dev: Device to resume.
 *
 * Carry out a runtime resume of a device under the assumption that its
 * pm_domain field points to the domain member of an object of type
 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 */

static int genpd_runtime_resume(struct device *dev)
{
 struct generic_pm_domain *genpd;
 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
 struct gpd_timing_data *td = gpd_data->td;
 bool timed = td && pm_runtime_enabled(dev);
 ktime_t time_start = 0;
 s64 elapsed_ns;
 int ret;

 dev_dbg(dev, "%s()\n", __func__);

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return -EINVAL;

 /*
 * As we don't power off a non IRQ safe domain, which holds
 * an IRQ safe device, we don't need to restore power to it.
 */

 if (irq_safe_dev_in_sleep_domain(dev, genpd))
  goto out;

 genpd_lock(genpd);
 genpd_restore_performance_state(dev, gpd_data->rpm_pstate);
 ret = genpd_power_on(genpd, 0);
 genpd_unlock(genpd);

 if (ret)
  return ret;

 out:
 /* Measure resume latency. */
 if (timed)
  time_start = ktime_get();

 ret = genpd_start_dev(genpd, dev);
 if (ret)
  goto err_poweroff;

 ret = __genpd_runtime_resume(dev);
 if (ret)
  goto err_stop;

 /* Update resume latency value if the measured time exceeds it. */
 if (timed) {
  elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  if (elapsed_ns > td->resume_latency_ns) {
   td->resume_latency_ns = elapsed_ns;
   dev_dbg(dev, "resume latency exceeded, %lld ns\n",
    elapsed_ns);
   genpd->gd->max_off_time_changed = true;
   td->constraint_changed = true;
  }
 }

 return 0;

err_stop:
 genpd_stop_dev(genpd, dev);
err_poweroff:
 if (!pm_runtime_is_irq_safe(dev) || genpd_is_irq_safe(genpd)) {
  genpd_lock(genpd);
  genpd_power_off(genpd, true, 0);
  gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
  genpd_unlock(genpd);
 }

 return ret;
}

static bool pd_ignore_unused;
static int __init pd_ignore_unused_setup(char *__unused)
{
 pd_ignore_unused = true;
 return 1;
}
__setup("pd_ignore_unused", pd_ignore_unused_setup);

/**
 * genpd_power_off_unused - Power off all PM domains with no devices in use.
 */

static int __init genpd_power_off_unused(void)
{
 struct generic_pm_domain *genpd;

 if (pd_ignore_unused) {
  pr_warn("genpd: Not disabling unused power domains\n");
  return 0;
 }

 pr_info("genpd: Disabling unused power domains\n");
 mutex_lock(&gpd_list_lock);

 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  genpd_queue_power_off_work(genpd);
 }

 mutex_unlock(&gpd_list_lock);

 return 0;
}
late_initcall_sync(genpd_power_off_unused);

#ifdef CONFIG_PM_SLEEP

/**
 * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
 * @genpd: PM domain to power off, if possible.
 * @use_lock: use the lock.
 * @depth: nesting count for lockdep.
 *
 * Check if the given PM domain can be powered off (during system suspend or
 * hibernation) and do that if so.  Also, in that case propagate to its parents.
 *
 * This function is only called in "noirq" and "syscore" stages of system power
 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
 * these cases the lock must be held.
 */

static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
     unsigned int depth)
{
 struct gpd_link *link;

 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
  return;

 if (genpd->suspended_count != genpd->device_count
     || atomic_read(&genpd->sd_count) > 0)
  return;

 /* Check that the children are in their deepest (powered-off) state. */
 list_for_each_entry(link, &genpd->parent_links, parent_node) {
  struct generic_pm_domain *child = link->child;
  if (child->state_idx < child->state_count - 1)
   return;
 }

 /* Choose the deepest state when suspending */
 genpd->state_idx = genpd->state_count - 1;
 if (_genpd_power_off(genpd, false)) {
  genpd->states[genpd->state_idx].rejected++;
  return;
 } else {
  genpd->states[genpd->state_idx].usage++;
 }

 genpd->status = GENPD_STATE_OFF;

 list_for_each_entry(link, &genpd->child_links, child_node) {
  genpd_sd_counter_dec(link->parent);

  if (use_lock)
   genpd_lock_nested(link->parent, depth + 1);

  genpd_sync_power_off(link->parent, use_lock, depth + 1);

  if (use_lock)
   genpd_unlock(link->parent);
 }
}

/**
 * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
 * @genpd: PM domain to power on.
 * @use_lock: use the lock.
 * @depth: nesting count for lockdep.
 *
 * This function is only called in "noirq" and "syscore" stages of system power
 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
 * these cases the lock must be held.
 */

static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
    unsigned int depth)
{
 struct gpd_link *link;

 if (genpd_status_on(genpd))
  return;

 list_for_each_entry(link, &genpd->child_links, child_node) {
  genpd_sd_counter_inc(link->parent);

  if (use_lock)
   genpd_lock_nested(link->parent, depth + 1);

  genpd_sync_power_on(link->parent, use_lock, depth + 1);

  if (use_lock)
   genpd_unlock(link->parent);
 }

 _genpd_power_on(genpd, false);
 genpd->status = GENPD_STATE_ON;
}

/**
 * genpd_prepare - Start power transition of a device in a PM domain.
 * @dev: Device to start the transition of.
 *
 * Start a power transition of a device (during a system-wide power transition)
 * under the assumption that its pm_domain field points to the domain member of
 * an object of type struct generic_pm_domain representing a PM domain
 * consisting of I/O devices.
 */

static int genpd_prepare(struct device *dev)
{
 struct generic_pm_domain *genpd;
 int ret;

 dev_dbg(dev, "%s()\n", __func__);

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return -EINVAL;

 genpd_lock(genpd);
 genpd->prepared_count++;
 genpd_unlock(genpd);

 ret = pm_generic_prepare(dev);
 if (ret < 0) {
  genpd_lock(genpd);

  genpd->prepared_count--;

  genpd_unlock(genpd);
 }

 /* Never return 1, as genpd don't cope with the direct_complete path. */
 return ret >= 0 ? 0 : ret;
}

/**
 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
 *   I/O pm domain.
 * @dev: Device to suspend.
 * @suspend_noirq: Generic suspend_noirq callback.
 * @resume_noirq: Generic resume_noirq callback.
 *
 * Stop the device and remove power from the domain if all devices in it have
 * been stopped.
 */

static int genpd_finish_suspend(struct device *dev,
    int (*suspend_noirq)(struct device *dev),
    int (*resume_noirq)(struct device *dev))
{
 struct generic_pm_domain *genpd;
 int ret = 0;

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return -EINVAL;

 ret = suspend_noirq(dev);
 if (ret)
  return ret;

 if (device_awake_path(dev) && genpd_is_active_wakeup(genpd))
  return 0;

 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
     !pm_runtime_status_suspended(dev)) {
  ret = genpd_stop_dev(genpd, dev);
  if (ret) {
   resume_noirq(dev);
   return ret;
  }
 }

 genpd_lock(genpd);
 genpd->suspended_count++;
 genpd_sync_power_off(genpd, true, 0);
 genpd_unlock(genpd);

 return 0;
}

/**
 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
 * @dev: Device to suspend.
 *
 * Stop the device and remove power from the domain if all devices in it have
 * been stopped.
 */

static int genpd_suspend_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_suspend(dev,
        pm_generic_suspend_noirq,
        pm_generic_resume_noirq);
}

/**
 * genpd_finish_resume - Completion of resume of device in an I/O PM domain.
 * @dev: Device to resume.
 * @resume_noirq: Generic resume_noirq callback.
 *
 * Restore power to the device's PM domain, if necessary, and start the device.
 */

static int genpd_finish_resume(struct device *dev,
          int (*resume_noirq)(struct device *dev))
{
 struct generic_pm_domain *genpd;
 int ret;

 dev_dbg(dev, "%s()\n", __func__);

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return -EINVAL;

 if (device_awake_path(dev) && genpd_is_active_wakeup(genpd))
  return resume_noirq(dev);

 genpd_lock(genpd);
 genpd_sync_power_on(genpd, true, 0);
 genpd->suspended_count--;
 genpd_unlock(genpd);

 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
     !pm_runtime_status_suspended(dev)) {
  ret = genpd_start_dev(genpd, dev);
  if (ret)
   return ret;
 }

 return pm_generic_resume_noirq(dev);
}

/**
 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
 * @dev: Device to resume.
 *
 * Restore power to the device's PM domain, if necessary, and start the device.
 */

static int genpd_resume_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_resume(dev, pm_generic_resume_noirq);
}

/**
 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
 * @dev: Device to freeze.
 *
 * Carry out a late freeze of a device under the assumption that its
 * pm_domain field points to the domain member of an object of type
 * struct generic_pm_domain representing a power domain consisting of I/O
 * devices.
 */

static int genpd_freeze_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_suspend(dev,
        pm_generic_freeze_noirq,
        pm_generic_thaw_noirq);
}

/**
 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
 * @dev: Device to thaw.
 *
 * Start the device, unless power has been removed from the domain already
 * before the system transition.
 */

static int genpd_thaw_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_resume(dev, pm_generic_thaw_noirq);
}

/**
 * genpd_poweroff_noirq - Completion of hibernation of device in an
 *   I/O PM domain.
 * @dev: Device to poweroff.
 *
 * Stop the device and remove power from the domain if all devices in it have
 * been stopped.
 */

static int genpd_poweroff_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_suspend(dev,
        pm_generic_poweroff_noirq,
        pm_generic_restore_noirq);
}

/**
 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
 * @dev: Device to resume.
 *
 * Make sure the domain will be in the same power state as before the
 * hibernation the system is resuming from and start the device if necessary.
 */

static int genpd_restore_noirq(struct device *dev)
{
 dev_dbg(dev, "%s()\n", __func__);

 return genpd_finish_resume(dev, pm_generic_restore_noirq);
}

/**
 * genpd_complete - Complete power transition of a device in a power domain.
 * @dev: Device to complete the transition of.
 *
 * Complete a power transition of a device (during a system-wide power
 * transition) under the assumption that its pm_domain field points to the
 * domain member of an object of type struct generic_pm_domain representing
 * a power domain consisting of I/O devices.
 */

static void genpd_complete(struct device *dev)
{
 struct generic_pm_domain *genpd;

 dev_dbg(dev, "%s()\n", __func__);

 genpd = dev_to_genpd(dev);
 if (IS_ERR(genpd))
  return;

 pm_generic_complete(dev);

 genpd_lock(genpd);

 genpd->prepared_count--;
 if (!genpd->prepared_count)
  genpd_queue_power_off_work(genpd);

 genpd_unlock(genpd);
}

static void genpd_switch_state(struct device *dev, bool suspend)
{
 struct generic_pm_domain *genpd;
 bool use_lock;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return;

 use_lock = genpd_is_irq_safe(genpd);

 if (use_lock)
  genpd_lock(genpd);

 if (suspend) {
  genpd->suspended_count++;
  genpd_sync_power_off(genpd, use_lock, 0);
 } else {
  genpd_sync_power_on(genpd, use_lock, 0);
  genpd->suspended_count--;
 }

 if (use_lock)
  genpd_unlock(genpd);
}

/**
 * dev_pm_genpd_suspend - Synchronously try to suspend the genpd for @dev
 * @dev: The device that is attached to the genpd, that can be suspended.
 *
 * This routine should typically be called for a device that needs to be
 * suspended during the syscore suspend phase. It may also be called during
 * suspend-to-idle to suspend a corresponding CPU device that is attached to a
 * genpd.
 */

void dev_pm_genpd_suspend(struct device *dev)
{
 genpd_switch_state(dev, true);
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_suspend);

/**
 * dev_pm_genpd_resume - Synchronously try to resume the genpd for @dev
 * @dev: The device that is attached to the genpd, which needs to be resumed.
 *
 * This routine should typically be called for a device that needs to be resumed
 * during the syscore resume phase. It may also be called during suspend-to-idle
 * to resume a corresponding CPU device that is attached to a genpd.
 */

void dev_pm_genpd_resume(struct device *dev)
{
 genpd_switch_state(dev, false);
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_resume);

#else /* !CONFIG_PM_SLEEP */

#define genpd_prepare  NULL
#define genpd_suspend_noirq NULL
#define genpd_resume_noirq NULL
#define genpd_freeze_noirq NULL
#define genpd_thaw_noirq NULL
#define genpd_poweroff_noirq NULL
#define genpd_restore_noirq NULL
#define genpd_complete  NULL

#endif /* CONFIG_PM_SLEEP */

static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
          bool has_governor)
{
 struct generic_pm_domain_data *gpd_data;
 struct gpd_timing_data *td;
 int ret;

 ret = dev_pm_get_subsys_data(dev);
 if (ret)
  return ERR_PTR(ret);

 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
 if (!gpd_data) {
  ret = -ENOMEM;
  goto err_put;
 }

 gpd_data->base.dev = dev;
 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;

 /* Allocate data used by a governor. */
 if (has_governor) {
  td = kzalloc(sizeof(*td), GFP_KERNEL);
  if (!td) {
   ret = -ENOMEM;
   goto err_free;
  }

  td->constraint_changed = true;
  td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
  td->next_wakeup = KTIME_MAX;
  gpd_data->td = td;
 }

 spin_lock_irq(&dev->power.lock);

 if (dev->power.subsys_data->domain_data)
  ret = -EINVAL;
 else
  dev->power.subsys_data->domain_data = &gpd_data->base;

 spin_unlock_irq(&dev->power.lock);

 if (ret)
  goto err_free;

 return gpd_data;

 err_free:
 kfree(gpd_data->td);
 kfree(gpd_data);
 err_put:
 dev_pm_put_subsys_data(dev);
 return ERR_PTR(ret);
}

static void genpd_free_dev_data(struct device *dev,
    struct generic_pm_domain_data *gpd_data)
{
 spin_lock_irq(&dev->power.lock);

 dev->power.subsys_data->domain_data = NULL;

 spin_unlock_irq(&dev->power.lock);

 dev_pm_opp_clear_config(gpd_data->opp_token);
 kfree(gpd_data->td);
 kfree(gpd_data);
 dev_pm_put_subsys_data(dev);
}

static void genpd_update_cpumask(struct generic_pm_domain *genpd,
     int cpu, bool set, unsigned int depth)
{
 struct gpd_link *link;

 if (!genpd_is_cpu_domain(genpd))
  return;

 list_for_each_entry(link, &genpd->child_links, child_node) {
  struct generic_pm_domain *parent = link->parent;

  genpd_lock_nested(parent, depth + 1);
  genpd_update_cpumask(parent, cpu, set, depth + 1);
  genpd_unlock(parent);
 }

 if (set)
  cpumask_set_cpu(cpu, genpd->cpus);
 else
  cpumask_clear_cpu(cpu, genpd->cpus);
}

static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
{
 if (cpu >= 0)
  genpd_update_cpumask(genpd, cpu, true, 0);
}

static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
{
 if (cpu >= 0)
  genpd_update_cpumask(genpd, cpu, false, 0);
}

static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
{
 int cpu;

 if (!genpd_is_cpu_domain(genpd))
  return -1;

 for_each_possible_cpu(cpu) {
  if (get_cpu_device(cpu) == dev)
   return cpu;
 }

 return -1;
}

static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
       struct device *base_dev)
{
 struct genpd_governor_data *gd = genpd->gd;
 struct generic_pm_domain_data *gpd_data;
 int ret;

 dev_dbg(dev, "%s()\n", __func__);

 gpd_data = genpd_alloc_dev_data(dev, gd);
 if (IS_ERR(gpd_data))
  return PTR_ERR(gpd_data);

 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);

 gpd_data->hw_mode = genpd->get_hwmode_dev ? genpd->get_hwmode_dev(genpd, dev) : false;

 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
 if (ret)
  goto out;

 genpd_lock(genpd);

 genpd_set_cpumask(genpd, gpd_data->cpu);

 genpd->device_count++;
 if (gd)
  gd->max_off_time_changed = true;

 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);

 genpd_unlock(genpd);
 dev_pm_domain_set(dev, &genpd->domain);
 out:
 if (ret)
  genpd_free_dev_data(dev, gpd_data);
 else
  dev_pm_qos_add_notifier(dev, &gpd_data->nb,
     DEV_PM_QOS_RESUME_LATENCY);

 return ret;
}

/**
 * pm_genpd_add_device - Add a device to an I/O PM domain.
 * @genpd: PM domain to add the device to.
 * @dev: Device to be added.
 */

int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
{
 int ret;

 if (!genpd || !dev)
  return -EINVAL;

 mutex_lock(&gpd_list_lock);
 ret = genpd_add_device(genpd, dev, dev);
 mutex_unlock(&gpd_list_lock);

 return ret;
}
EXPORT_SYMBOL_GPL(pm_genpd_add_device);

static int genpd_remove_device(struct generic_pm_domain *genpd,
          struct device *dev)
{
 struct generic_pm_domain_data *gpd_data;
 struct pm_domain_data *pdd;
 int ret = 0;

 dev_dbg(dev, "%s()\n", __func__);

 pdd = dev->power.subsys_data->domain_data;
 gpd_data = to_gpd_data(pdd);
 dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
       DEV_PM_QOS_RESUME_LATENCY);

 genpd_lock(genpd);

 if (genpd->prepared_count > 0) {
  ret = -EAGAIN;
  goto out;
 }

 genpd->device_count--;
 if (genpd->gd)
  genpd->gd->max_off_time_changed = true;

 genpd_clear_cpumask(genpd, gpd_data->cpu);

 list_del_init(&pdd->list_node);

 genpd_unlock(genpd);

 dev_pm_domain_set(dev, NULL);

 if (genpd->detach_dev)
  genpd->detach_dev(genpd, dev);

 genpd_free_dev_data(dev, gpd_data);

 return 0;

 out:
 genpd_unlock(genpd);
 dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);

 return ret;
}

/**
 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
 * @dev: Device to be removed.
 */

int pm_genpd_remove_device(struct device *dev)
{
 struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);

 if (!genpd)
  return -EINVAL;

 return genpd_remove_device(genpd, dev);
}
EXPORT_SYMBOL_GPL(pm_genpd_remove_device);

/**
 * dev_pm_genpd_add_notifier - Add a genpd power on/off notifier for @dev
 *
 * @dev: Device that should be associated with the notifier
 * @nb: The notifier block to register
 *
 * Users may call this function to add a genpd power on/off notifier for an
 * attached @dev. Only one notifier per device is allowed. The notifier is
 * sent when genpd is powering on/off the PM domain.
 *
 * It is assumed that the user guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Returns 0 on success and negative error values on failures.
 */

int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb)
{
 struct generic_pm_domain *genpd;
 struct generic_pm_domain_data *gpd_data;
 int ret;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return -ENODEV;

 if (WARN_ON(!dev->power.subsys_data ||
       !dev->power.subsys_data->domain_data))
  return -EINVAL;

 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
 if (gpd_data->power_nb)
  return -EEXIST;

 genpd_lock(genpd);
 ret = raw_notifier_chain_register(&genpd->power_notifiers, nb);
 genpd_unlock(genpd);

 if (ret) {
  dev_warn(dev, "failed to add notifier for PM domain %s\n",
    dev_name(&genpd->dev));
  return ret;
 }

 gpd_data->power_nb = nb;
 return 0;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_add_notifier);

/**
 * dev_pm_genpd_remove_notifier - Remove a genpd power on/off notifier for @dev
 *
 * @dev: Device that is associated with the notifier
 *
 * Users may call this function to remove a genpd power on/off notifier for an
 * attached @dev.
 *
 * It is assumed that the user guarantee that the genpd wouldn't be detached
 * while this routine is getting called.
 *
 * Returns 0 on success and negative error values on failures.
 */

int dev_pm_genpd_remove_notifier(struct device *dev)
{
 struct generic_pm_domain *genpd;
 struct generic_pm_domain_data *gpd_data;
 int ret;

 genpd = dev_to_genpd_safe(dev);
 if (!genpd)
  return -ENODEV;

 if (WARN_ON(!dev->power.subsys_data ||
       !dev->power.subsys_data->domain_data))
  return -EINVAL;

 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
 if (!gpd_data->power_nb)
  return -ENODEV;

 genpd_lock(genpd);
 ret = raw_notifier_chain_unregister(&genpd->power_notifiers,
         gpd_data->power_nb);
 genpd_unlock(genpd);

 if (ret) {
  dev_warn(dev, "failed to remove notifier for PM domain %s\n",
    dev_name(&genpd->dev));
  return ret;
 }

 gpd_data->power_nb = NULL;
 return 0;
}
EXPORT_SYMBOL_GPL(dev_pm_genpd_remove_notifier);

static int genpd_add_subdomain(struct generic_pm_domain *genpd,
          struct generic_pm_domain *subdomain)
{
 struct gpd_link *link, *itr;
 int ret = 0;

 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
     || genpd == subdomain)
  return -EINVAL;

 /*
 * If the domain can be powered on/off in an IRQ safe
 * context, ensure that the subdomain can also be
 * powered on/off in that context.
 */

 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
  WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
       dev_name(&genpd->dev), subdomain->name);
  return -EINVAL;
 }

 link = kzalloc(sizeof(*link), GFP_KERNEL);
 if (!link)
  return -ENOMEM;

 genpd_lock(subdomain);
 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);

 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
  ret = -EINVAL;
  goto out;
 }

 list_for_each_entry(itr, &genpd->parent_links, parent_node) {
  if (itr->child == subdomain && itr->parent == genpd) {
   ret = -EINVAL;
   goto out;
  }
 }

 link->parent = genpd;
 list_add_tail(&link->parent_node, &genpd->parent_links);
 link->child = subdomain;
 list_add_tail(&link->child_node, &subdomain->child_links);
 if (genpd_status_on(subdomain))
  genpd_sd_counter_inc(genpd);

 out:
 genpd_unlock(genpd);
 genpd_unlock(subdomain);
 if (ret)
  kfree(link);
 return ret;
}

/**
 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
 * @genpd: Leader PM domain to add the subdomain to.
 * @subdomain: Subdomain to be added.
 */

int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
      struct generic_pm_domain *subdomain)
{
 int ret;

 mutex_lock(&gpd_list_lock);
 ret = genpd_add_subdomain(genpd, subdomain);
 mutex_unlock(&gpd_list_lock);

 return ret;
}
EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);

/**
 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
 * @genpd: Leader PM domain to remove the subdomain from.
 * @subdomain: Subdomain to be removed.
 */

int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
         struct generic_pm_domain *subdomain)
{
 struct gpd_link *l, *link;
 int ret = -EINVAL;

 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  return -EINVAL;

 genpd_lock(subdomain);
 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);

 if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
  pr_warn("%s: unable to remove subdomain %s\n",
   dev_name(&genpd->dev), subdomain->name);
  ret = -EBUSY;
  goto out;
 }

 list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
  if (link->child != subdomain)
   continue;

  list_del(&link->parent_node);
  list_del(&link->child_node);
  kfree(link);
  if (genpd_status_on(subdomain))
   genpd_sd_counter_dec(genpd);

  ret = 0;
  break;
 }

out:
 genpd_unlock(genpd);
 genpd_unlock(subdomain);

 return ret;
}
EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);

static void genpd_free_default_power_state(struct genpd_power_state *states,
        unsigned int state_count)
{
 kfree(states);
}

static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
{
 struct genpd_power_state *state;

 state = kzalloc(sizeof(*state), GFP_KERNEL);
 if (!state)
  return -ENOMEM;

 genpd->states = state;
 genpd->state_count = 1;
 genpd->free_states = genpd_free_default_power_state;

 return 0;
}

static void genpd_provider_release(struct device *dev)
{
 /* nothing to be done here */
}

static int genpd_alloc_data(struct generic_pm_domain *genpd)
{
 struct genpd_governor_data *gd = NULL;
 int ret;

 if (genpd_is_cpu_domain(genpd) &&
     !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
  return -ENOMEM;

 if (genpd->gov) {
  gd = kzalloc(sizeof(*gd), GFP_KERNEL);
  if (!gd) {
   ret = -ENOMEM;
   goto free;
  }

  gd->max_off_time_ns = -1;
  gd->max_off_time_changed = true;
  gd->next_wakeup = KTIME_MAX;
  gd->next_hrtimer = KTIME_MAX;
 }

 /* Use only one "off" state if there were no states declared */
 if (genpd->state_count == 0) {
  ret = genpd_set_default_power_state(genpd);
  if (ret)
   goto free;
 }

 genpd->gd = gd;
 device_initialize(&genpd->dev);
 genpd->dev.release = genpd_provider_release;
 genpd->dev.bus = &genpd_provider_bus_type;
 genpd->dev.parent = &genpd_provider_bus;

 if (!genpd_is_dev_name_fw(genpd)) {
  dev_set_name(&genpd->dev, "%s", genpd->name);
 } else {
  ret = ida_alloc(&genpd_ida, GFP_KERNEL);
  if (ret < 0)
   goto put;

  genpd->device_id = ret;
  dev_set_name(&genpd->dev, "%s_%u", genpd->name, genpd->device_id);
 }

 return 0;
put:
 put_device(&genpd->dev);
 if (genpd->free_states == genpd_free_default_power_state) {
  kfree(genpd->states);
  genpd->states = NULL;
 }
free:
 if (genpd_is_cpu_domain(genpd))
  free_cpumask_var(genpd->cpus);
 kfree(gd);
 return ret;
}

static void genpd_free_data(struct generic_pm_domain *genpd)
{
 put_device(&genpd->dev);
 if (genpd->device_id != -ENXIO)
  ida_free(&genpd_ida, genpd->device_id);
 if (genpd_is_cpu_domain(genpd))
  free_cpumask_var(genpd->cpus);
 if (genpd->free_states)
  genpd->free_states(genpd->states, genpd->state_count);
 kfree(genpd->gd);
}

static void genpd_lock_init(struct generic_pm_domain *genpd)
{
 if (genpd_is_cpu_domain(genpd)) {
  raw_spin_lock_init(&genpd->raw_slock);
  genpd->lock_ops = &genpd_raw_spin_ops;
 } else if (genpd_is_irq_safe(genpd)) {
  spin_lock_init(&genpd->slock);
  genpd->lock_ops = &genpd_spin_ops;
 } else {
  mutex_init(&genpd->mlock);
  genpd->lock_ops = &genpd_mtx_ops;
 }
}

#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
static void genpd_set_stay_on(struct generic_pm_domain *genpd, bool is_off)
{
 genpd->stay_on = !genpd_is_no_stay_on(genpd) && !is_off;
}
#else
static void genpd_set_stay_on(struct generic_pm_domain *genpd, bool is_off)
{
 genpd->stay_on = false;
}
#endif

/**
 * pm_genpd_init - Initialize a generic I/O PM domain object.
 * @genpd: PM domain object to initialize.
 * @gov: PM domain governor to associate with the domain (may be NULL).
 * @is_off: Initial value of the domain's power_is_off field.
 *
 * Returns 0 on successful initialization, else a negative error code.
 */

int pm_genpd_init(struct generic_pm_domain *genpd,
    struct dev_power_governor *gov, bool is_off)
{
 int ret;

 if (IS_ERR_OR_NULL(genpd))
  return -EINVAL;

 INIT_LIST_HEAD(&genpd->parent_links);
 INIT_LIST_HEAD(&genpd->child_links);
 INIT_LIST_HEAD(&genpd->dev_list);
 RAW_INIT_NOTIFIER_HEAD(&genpd->power_notifiers);
 genpd_lock_init(genpd);
 genpd->gov = gov;
 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
 atomic_set(&genpd->sd_count, 0);
 genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
 genpd_set_stay_on(genpd, is_off);
 genpd->sync_state = GENPD_SYNC_STATE_OFF;
 genpd->device_count = 0;
 genpd->provider = NULL;
 genpd->device_id = -ENXIO;
 genpd->has_provider = false;
 genpd->opp_table = NULL;
 genpd->accounting_time = ktime_get_mono_fast_ns();
 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
 genpd->domain.ops.prepare = genpd_prepare;
 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
 genpd->domain.ops.complete = genpd_complete;
 genpd->domain.start = genpd_dev_pm_start;
 genpd->domain.set_performance_state = genpd_dev_pm_set_performance_state;

 if (genpd->flags & GENPD_FLAG_PM_CLK) {
  genpd->dev_ops.stop = pm_clk_suspend;
  genpd->dev_ops.start = pm_clk_resume;
 }

 /* The always-on governor works better with the corresponding flag. */
 if (gov == &pm_domain_always_on_gov)
  genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;

 /* Always-on domains must be powered on at initialization. */
 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
   !genpd_status_on(genpd)) {
  pr_err("always-on PM domain %s is not on\n", genpd->name);
  return -EINVAL;
 }

 /* Multiple states but no governor doesn't make sense. */
 if (!gov && genpd->state_count > 1)
  pr_warn("%s: no governor for states\n", genpd->name);

 ret = genpd_alloc_data(genpd);
 if (ret)
  return ret;

 mutex_lock(&gpd_list_lock);
 list_add(&genpd->gpd_list_node, &gpd_list);
 mutex_unlock(&gpd_list_lock);
 genpd_debug_add(genpd);

 return 0;
}
EXPORT_SYMBOL_GPL(pm_genpd_init);

static int genpd_remove(struct generic_pm_domain *genpd)
{
 struct gpd_link *l, *link;

 if (IS_ERR_OR_NULL(genpd))
  return -EINVAL;

 genpd_lock(genpd);

 if (genpd->has_provider) {
  genpd_unlock(genpd);
  pr_err("Provider present, unable to remove %s\n", dev_name(&genpd->dev));
  return -EBUSY;
 }

 if (!list_empty(&genpd->parent_links) || genpd->device_count) {
  genpd_unlock(genpd);
  pr_err("%s: unable to remove %s\n", __func__, dev_name(&genpd->dev));
  return -EBUSY;
 }

 list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
  list_del(&link->parent_node);
  list_del(&link->child_node);
  kfree(link);
 }

 list_del(&genpd->gpd_list_node);
 genpd_unlock(genpd);
 genpd_debug_remove(genpd);
 cancel_work_sync(&genpd->power_off_work);
 genpd_free_data(genpd);

 pr_debug("%s: removed %s\n", __func__, dev_name(&genpd->dev));

 return 0;
}

/**
 * pm_genpd_remove - Remove a generic I/O PM domain
 * @genpd: Pointer to PM domain that is to be removed.
 *
 * To remove the PM domain, this function:
 *  - Removes the PM domain as a subdomain to any parent domains,
 *    if it was added.
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=96 H=96 G=95

¤ Dauer der Verarbeitung: 0.22 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.