diff options
| author | Ulf Hansson <ulf.hansson@linaro.org> | 2023-12-13 12:33:05 +0100 |
|---|---|---|
| committer | Ulf Hansson <ulf.hansson@linaro.org> | 2023-12-18 12:49:48 +0100 |
| commit | d6948c13b663a284574cb9e502dd663e70d910e8 (patch) | |
| tree | 2569bab3c919278842ab3b3da640a333791e74c5 /drivers/base/power | |
| parent | f871e716058988ee1f97263f3247a2cf998f9579 (diff) | |
| download | linux-d6948c13b663a284574cb9e502dd663e70d910e8.tar.gz linux-d6948c13b663a284574cb9e502dd663e70d910e8.tar.bz2 linux-d6948c13b663a284574cb9e502dd663e70d910e8.zip | |
PM: domains: Move genpd and its governor to the pmdomain subsystem
It seems reasonable to collect the core parts for the generic PM domain,
along with its corresponding provider drivers. Therefore let's move the
files from drivers/base/power/ to drivers/pmdomain/ and while at it, let's
also rename the files accordingly.
Moreover, let's also update MAINTAINERS to reflect the update.
Cc: Kevin Hilman <khilman@kernel.org>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Link: https://lore.kernel.org/r/20231213113305.29098-1-ulf.hansson@linaro.org
Diffstat (limited to 'drivers/base/power')
| -rw-r--r-- | drivers/base/power/Makefile | 1 | ||||
| -rw-r--r-- | drivers/base/power/domain.c | 3399 | ||||
| -rw-r--r-- | drivers/base/power/domain_governor.c | 418 |
3 files changed, 0 insertions, 3818 deletions
diff --git a/drivers/base/power/Makefile b/drivers/base/power/Makefile index 8fdd0073eeeb..01f11629d241 100644 --- a/drivers/base/power/Makefile +++ b/drivers/base/power/Makefile @@ -2,7 +2,6 @@ obj-$(CONFIG_PM) += sysfs.o generic_ops.o common.o qos.o runtime.o wakeirq.o obj-$(CONFIG_PM_SLEEP) += main.o wakeup.o wakeup_stats.o obj-$(CONFIG_PM_TRACE_RTC) += trace.o -obj-$(CONFIG_PM_GENERIC_DOMAINS) += domain.o domain_governor.o obj-$(CONFIG_HAVE_CLK) += clock_ops.o obj-$(CONFIG_PM_QOS_KUNIT_TEST) += qos-test.o diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c deleted file mode 100644 index 9ee1bf2ece10..000000000000 --- a/drivers/base/power/domain.c +++ /dev/null @@ -1,3399 +0,0 @@ -// 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/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> - -#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, -}; - -#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) - -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", - genpd->name); - - 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); -} - -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(genpd->name, 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; -} -#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) {} -#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) -{ - struct generic_pm_domain *parent; - struct gpd_link *link; - int parent_state, ret; - - if (state == genpd->performance_state) - return 0; - - /* Propagate to parents of genpd */ - list_for_each_entry(link, &genpd->child_links, child_node) { - parent = link->parent; - - /* Find parent's performance state */ - ret = genpd_xlate_performance_state(genpd, parent, state); - if (unlikely(ret < 0)) - goto err; - - 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); - - if (ret) - goto err; - } - - if (genpd->set_performance_state) { - ret = genpd->set_performance_state(genpd, state); - if (ret) - goto err; - } - - genpd->performance_state = state; - return 0; - -err: - /* Encountered an error, lets rollback */ - list_for_each_entry_continue_reverse(link, &genpd->child_links, - child_node) { - parent = link->parent; - - 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); - } - - 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); - -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", - genpd->name, "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", - genpd->name, "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 int 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; - int ret; - - /* - * Do not try to power off the domain in the following situations: - * (1) The domain is already in the "power off" state. - * (2) System suspend is in progress. - */ - if (!genpd_status_on(genpd) || genpd->prepared_count > 0) - return 0; - - /* - * Abort power off for the PM domain in the following situations: - * (1) The domain is configured as always on. - * (2) When the domain has a subdomain being powered on. - */ - if (genpd_is_always_on(genpd) || - genpd_is_rpm_always_on(genpd) || - atomic_read(&genpd->sd_count) > 0) - return -EBUSY; - - /* - * 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 -EBUSY; - } - - 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++; - } - - if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on)) - return -EBUSY; - - if (genpd->gov && genpd->gov->power_down_ok) { - if (!genpd->gov->power_down_ok(&genpd->domain)) - return -EAGAIN; - } - - /* 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 -EBUSY; - - ret = _genpd_power_off(genpd, true); - if (ret) { - genpd->states[genpd->state_idx].rejected++; - return ret; - } - - 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); - } - - return 0; -} - -/** - * 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; - - /* - * 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); - gp |
