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));
/* * 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.
*/ staticstruct 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.
*/ staticstruct generic_pm_domain *dev_to_genpd(struct device *dev)
{ if (IS_ERR_OR_NULL(dev->pm_domain)) return ERR_PTR(-EINVAL);
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;
/* 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;
}
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);
}
staticint _genpd_set_parent_state(struct generic_pm_domain *genpd, struct gpd_link *link, unsignedint 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 = _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;
}
staticint _genpd_set_performance_state(struct generic_pm_domain *genpd, unsignedint 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;
}
}
staticint genpd_dev_pm_set_performance_state(struct device *dev, unsignedint 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, unsignedint 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;
/** * 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;
/* * 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;
/** * 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;
/** * 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;
/** * 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) returnfalse;
/** * 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, unsignedint 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);
/* 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);
/* 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);
/** * 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.
*/ staticvoid 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.
*/ staticvoid genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on, unsignedint depth)
{ struct pm_domain_data *pdd; struct gpd_link *link; unsignedint 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;
}
/** * 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.
*/ staticint genpd_power_on(struct generic_pm_domain *genpd, unsignedint 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;
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.
*/ staticvoid genpd_power_off_work_fn(struct work_struct *work)
{ struct generic_pm_domain *genpd;
/** * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks * @dev: Device to handle.
*/ staticint __genpd_runtime_suspend(struct device *dev)
{ int (*cb)(struct device *__dev);
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.
*/ staticint __genpd_runtime_resume(struct device *dev)
{ int (*cb)(struct device *__dev);
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.
*/ staticint 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;
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_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.
*/ staticint 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);
/** * genpd_power_off_unused - Power off all PM domains with no devices in use.
*/ staticint __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);
/** * 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.
*/ staticvoid genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock, unsignedint 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_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.
*/ staticvoid genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock, unsignedint depth)
{ struct gpd_link *link;
/** * 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.
*/ staticint 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;
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.
*/ staticint 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_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.
*/ staticint genpd_suspend_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticint 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);
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.
*/ staticint genpd_resume_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticint genpd_freeze_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticint genpd_thaw_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticint genpd_poweroff_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticint genpd_restore_noirq(struct device *dev)
{
dev_dbg(dev, "%s()\n", __func__);
/** * 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.
*/ staticvoid 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);
/** * 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);
/** * 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);
/** * 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);
/** * 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;
}
/** * 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;
}
staticint 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;
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);
/** * 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;
/* 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;
}
/** * 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;
/* 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;
/** * 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. * - Removes the PM domain from the list of registered PM domains. * * The PM domain will only be removed, if the associated provider has * been removed, it is not a parent to any other PM domain and has no * devices associated with it.
*/ int pm_genpd_remove(struct generic_pm_domain *genpd)
{ int ret;
mutex_lock(&gpd_list_lock);
ret = genpd_remove(genpd);
mutex_unlock(&gpd_list_lock);
return ret;
}
EXPORT_SYMBOL_GPL(pm_genpd_remove);
#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
/* * Device Tree based PM domain providers. * * The code below implements generic device tree based PM domain providers that * bind device tree nodes with generic PM domains registered in the system. * * Any driver that registers generic PM domains and needs to support binding of * devices to these domains is supposed to register a PM domain provider, which * maps a PM domain specifier retrieved from the device tree to a PM domain. * * Two simple mapping functions have been provided for convenience: * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping. * - genpd_xlate_onecell() for mapping of multiple PM domains per node by * index.
*/
/** * struct of_genpd_provider - PM domain provider registration structure * @link: Entry in global list of PM domain providers * @node: Pointer to device tree node of PM domain provider * @xlate: Provider-specific xlate callback mapping a set of specifier cells * into a PM domain. * @data: context pointer to be passed into @xlate callback
*/ struct of_genpd_provider { struct list_head link; struct device_node *node;
genpd_xlate_t xlate; void *data;
};
/* List of registered PM domain providers. */ static LIST_HEAD(of_genpd_providers); /* Mutex to protect the list above. */ static DEFINE_MUTEX(of_genpd_mutex); /* Used to prevent registering devices before the bus. */ staticbool genpd_bus_registered;
/** * genpd_xlate_simple() - Xlate function for direct node-domain mapping * @genpdspec: OF phandle args to map into a PM domain * @data: xlate function private data - pointer to struct generic_pm_domain * * This is a generic xlate function that can be used to model PM domains that * have their own device tree nodes. The private data of xlate function needs * to be a valid pointer to struct generic_pm_domain.
*/ staticstruct generic_pm_domain *genpd_xlate_simple( conststruct of_phandle_args *genpdspec, void *data)
{ return data;
}
/** * genpd_xlate_onecell() - Xlate function using a single index. * @genpdspec: OF phandle args to map into a PM domain * @data: xlate function private data - pointer to struct genpd_onecell_data * * This is a generic xlate function that can be used to model simple PM domain * controllers that have one device tree node and provide multiple PM domains. * A single cell is used as an index into an array of PM domains specified in * the genpd_onecell_data struct when registering the provider.
*/ staticstruct generic_pm_domain *genpd_xlate_onecell( conststruct of_phandle_args *genpdspec, void *data)
{ struct genpd_onecell_data *genpd_data = data; unsignedint idx = genpdspec->args[0];
if (genpdspec->args_count != 1) return ERR_PTR(-EINVAL);
if (idx >= genpd_data->num_domains) {
pr_err("%s: invalid domain index %u\n", __func__, idx); return ERR_PTR(-EINVAL);
}
if (!genpd_data->domains[idx]) return ERR_PTR(-ENOENT);
return genpd_data->domains[idx];
}
/** * genpd_add_provider() - Register a PM domain provider for a node * @np: Device node pointer associated with the PM domain provider. * @xlate: Callback for decoding PM domain from phandle arguments. * @data: Context pointer for @xlate callback.
*/ staticint genpd_add_provider(struct device_node *np, genpd_xlate_t xlate, void *data)
{ struct of_genpd_provider *cp;
cp = kzalloc(sizeof(*cp), GFP_KERNEL); if (!cp) return -ENOMEM;
ret = device_add(&genpd->dev); if (ret) return ret;
/* Parse genpd OPP table */ if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
ret = dev_pm_opp_of_add_table(&genpd->dev); if (ret) {
dev_err_probe(&genpd->dev, ret, "Failed to add OPP table\n"); goto err_del;
}
/* * Save table for faster processing while setting performance * state.
*/
genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
WARN_ON(IS_ERR(genpd->opp_table));
}
ret = genpd_add_provider(np, genpd_xlate_simple, genpd); if (ret) goto err_opp;
ret = device_add(&genpd->dev); if (ret) goto error;
/* Parse genpd OPP table */ if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i); if (ret) {
dev_err_probe(&genpd->dev, ret, "Failed to add OPP table for index %d\n", i);
device_del(&genpd->dev); goto error;
}
/* * Save table for faster processing while setting * performance state.
*/
genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
WARN_ON(IS_ERR(genpd->opp_table));
}
mutex_lock(&gpd_list_lock);
mutex_lock(&of_genpd_mutex);
list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) { if (cp->node == np) { /* * For each PM domain associated with the * provider, set the 'has_provider' to false * so that the PM domain can be safely removed.
*/
list_for_each_entry(gpd, &gpd_list, gpd_list_node) { if (gpd->provider == of_fwnode_handle(np)) {
gpd->has_provider = false;
if (gpd->opp_table) {
dev_pm_opp_put_opp_table(gpd->opp_table);
dev_pm_opp_of_remove_table(&gpd->dev);
}
/** * genpd_get_from_provider() - Look-up PM domain * @genpdspec: OF phandle args to use for look-up * * Looks for a PM domain provider under the node specified by @genpdspec and if * found, uses xlate function of the provider to map phandle args to a PM * domain. * * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR() * on failure.
*/ staticstruct generic_pm_domain *genpd_get_from_provider( conststruct of_phandle_args *genpdspec)
{ struct generic_pm_domain *genpd = ERR_PTR(-ENOENT); struct of_genpd_provider *provider;
if (!genpdspec) return ERR_PTR(-EINVAL);
mutex_lock(&of_genpd_mutex);
/* Check if we have such a provider in our array */
list_for_each_entry(provider, &of_genpd_providers, link) { if (provider->node == genpdspec->np)
genpd = provider->xlate(genpdspec, provider->data); if (!IS_ERR(genpd)) break;
}
mutex_unlock(&of_genpd_mutex);
return genpd;
}
/** * of_genpd_add_device() - Add a device to an I/O PM domain * @genpdspec: OF phandle args to use for look-up PM domain * @dev: Device to be added. * * Looks-up an I/O PM domain based upon phandle args provided and adds * the device to the PM domain. Returns a negative error code on failure.
*/ int of_genpd_add_device(conststruct of_phandle_args *genpdspec, struct device *dev)
{ struct generic_pm_domain *genpd; int ret;
if (!dev) return -EINVAL;
mutex_lock(&gpd_list_lock);
genpd = genpd_get_from_provider(genpdspec); if (IS_ERR(genpd)) {
ret = PTR_ERR(genpd); goto out;
}
/** * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain. * @parent_spec: OF phandle args to use for parent PM domain look-up * @subdomain_spec: OF phandle args to use for subdomain look-up * * Looks-up a parent PM domain and subdomain based upon phandle args * provided and adds the subdomain to the parent PM domain. Returns a * negative error code on failure.
*/ int of_genpd_add_subdomain(conststruct of_phandle_args *parent_spec, conststruct of_phandle_args *subdomain_spec)
{ struct generic_pm_domain *parent, *subdomain; int ret;
mutex_lock(&gpd_list_lock);
parent = genpd_get_from_provider(parent_spec); if (IS_ERR(parent)) {
ret = PTR_ERR(parent); goto out;
}
subdomain = genpd_get_from_provider(subdomain_spec); if (IS_ERR(subdomain)) {
ret = PTR_ERR(subdomain); goto out;
}
ret = genpd_add_subdomain(parent, subdomain);
out:
mutex_unlock(&gpd_list_lock);
return ret == -ENOENT ? -EPROBE_DEFER : ret;
}
EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
/** * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. * @parent_spec: OF phandle args to use for parent PM domain look-up * @subdomain_spec: OF phandle args to use for subdomain look-up * * Looks-up a parent PM domain and subdomain based upon phandle args * provided and removes the subdomain from the parent PM domain. Returns a * negative error code on failure.
*/ int of_genpd_remove_subdomain(conststruct of_phandle_args *parent_spec, conststruct of_phandle_args *subdomain_spec)
{ struct generic_pm_domain *parent, *subdomain; int ret;
mutex_lock(&gpd_list_lock);
parent = genpd_get_from_provider(parent_spec); if (IS_ERR(parent)) {
ret = PTR_ERR(parent); goto out;
}
subdomain = genpd_get_from_provider(subdomain_spec); if (IS_ERR(subdomain)) {
ret = PTR_ERR(subdomain); goto out;
}
ret = pm_genpd_remove_subdomain(parent, subdomain);
/** * of_genpd_remove_last - Remove the last PM domain registered for a provider * @np: Pointer to device node associated with provider * * Find the last PM domain that was added by a particular provider and * remove this PM domain from the list of PM domains. The provider is * identified by the 'provider' device structure that is passed. The PM * domain will only be removed, if the provider associated with domain * has been removed. * * Returns a valid pointer to struct generic_pm_domain on success or * ERR_PTR() on failure.
*/ struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
{ struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT); int ret;
if (IS_ERR_OR_NULL(np)) return ERR_PTR(-EINVAL);
mutex_lock(&gpd_list_lock);
list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) { if (gpd->provider == of_fwnode_handle(np)) {
ret = genpd_remove(gpd);
genpd = ret ? ERR_PTR(ret) : gpd; break;
}
}
mutex_unlock(&gpd_list_lock);
/** * genpd_dev_pm_detach - Detach a device from its PM domain. * @dev: Device to detach. * @power_off: Currently not used * * Try to locate a corresponding generic PM domain, which the device was * attached to previously. If such is found, the device is detached from it.
*/ staticvoid genpd_dev_pm_detach(struct device *dev, bool power_off)
{ struct generic_pm_domain *pd; unsignedint i; int ret = 0;
pd = dev_to_genpd(dev); if (IS_ERR(pd)) return;
dev_dbg(dev, "removing from PM domain %s\n", pd->name);
/* Drop the default performance state */ if (dev_gpd_data(dev)->default_pstate) {
dev_pm_genpd_set_performance_state(dev, 0);
dev_gpd_data(dev)->default_pstate = 0;
}
for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
ret = genpd_remove_device(pd, dev); if (ret != -EAGAIN) break;
mdelay(i);
cond_resched();
}
if (ret < 0) {
dev_err(dev, "failed to remove from PM domain %s: %d",
pd->name, ret); return;
}
/* Check if PM domain can be powered off after removing this device. */
genpd_queue_power_off_work(pd);
/* Unregister the device if it was created by genpd. */ if (dev->bus == &genpd_bus_type)
device_unregister(dev);
}
/* * For a single PM domain the index of the required OPP must be zero, so * let's try to assign a required dev in that case. In the multiple PM * domains case, we need platform code to specify the index.
*/ if (num_domains == 1) {
ret = genpd_set_required_opp_dev(dev, base_dev); if (ret) goto err;
}
ret = genpd_set_required_opp(dev, index); if (ret) goto err;
if (power_on) {
genpd_lock(pd);
ret = genpd_power_on(pd, 0);
genpd_unlock(pd);
}
if (ret) { /* Drop the default performance state */ if (dev_gpd_data(dev)->default_pstate) {
dev_pm_genpd_set_performance_state(dev, 0);
dev_gpd_data(dev)->default_pstate = 0;
}
/** * genpd_dev_pm_attach - Attach a device to its PM domain using DT. * @dev: Device to attach. * * Parse device's OF node to find a PM domain specifier. If such is found, * attaches the device to retrieved pm_domain ops. * * Returns 1 on successfully attached PM domain, 0 when the device don't need a * PM domain or when multiple power-domains exists for it, else a negative error * code. Note that if a power-domain exists for the device, but it cannot be * found or turned on, then return -EPROBE_DEFER to ensure that the device is * not probed and to re-try again later.
*/ int genpd_dev_pm_attach(struct device *dev)
{ if (!dev->of_node) return 0;
/* * Devices with multiple PM domains must be attached separately, as we * can only attach one PM domain per device.
*/ if (of_count_phandle_with_args(dev->of_node, "power-domains", "#power-domain-cells") != 1) return 0;
/** * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains. * @dev: The device used to lookup the PM domain. * @index: The index of the PM domain. * * Parse device's OF node to find a PM domain specifier at the provided @index. * If such is found, creates a virtual device and attaches it to the retrieved * pm_domain ops. To deal with detaching of the virtual device, the ->detach() * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach(). * * Returns the created virtual device if successfully attached PM domain, NULL * when the device don't need a PM domain, else an ERR_PTR() in case of * failures. If a power-domain exists for the device, but cannot be found or * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device * is not probed and to re-try again later.
*/ struct device *genpd_dev_pm_attach_by_id(struct device *dev, unsignedint index)
{ struct device *virt_dev; int num_domains; int ret;
if (!dev->of_node) return NULL;
/* Verify that the index is within a valid range. */
num_domains = of_count_phandle_with_args(dev->of_node, "power-domains", "#power-domain-cells"); if (num_domains < 0 || index >= num_domains) return NULL;
if (!genpd_bus_registered) return ERR_PTR(-ENODEV);
/* Allocate and register device on the genpd bus. */
virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL); if (!virt_dev) return ERR_PTR(-ENOMEM);
ret = device_register(virt_dev); if (ret) {
put_device(virt_dev); return ERR_PTR(ret);
}
/* Try to attach the device to the PM domain at the specified index. */
ret = __genpd_dev_pm_attach(virt_dev, dev, index, num_domains, false); if (ret < 1) {
device_unregister(virt_dev); return ret ? ERR_PTR(ret) : NULL;
}
/** * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains. * @dev: The device used to lookup the PM domain. * @name: The name of the PM domain. * * Parse device's OF node to find a PM domain specifier using the * power-domain-names DT property. For further description see * genpd_dev_pm_attach_by_id().
*/ struct device *genpd_dev_pm_attach_by_name(struct device *dev, constchar *name)
{ int index;
if (!dev->of_node) return NULL;
index = of_property_match_string(dev->of_node, "power-domain-names",
name); if (index < 0) return NULL;
staticint genpd_iterate_idle_states(struct device_node *dn, struct genpd_power_state *states)
{ int ret; struct of_phandle_iterator it; struct device_node *np; int i = 0;
ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL); if (ret <= 0) return ret == -ENOENT ? 0 : ret;
/* Loop over the phandles until all the requested entry is found */
of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
np = it.node; if (!of_match_node(idle_state_match, np)) continue;
if (!of_device_is_available(np)) continue;
if (states) {
ret = genpd_parse_state(&states[i], np); if (ret) {
pr_err("Parsing idle state node %pOF failed with err %d\n",
np, ret);
of_node_put(np); return ret;
}
}
i++;
}
return i;
}
/** * of_genpd_parse_idle_states: Return array of idle states for the genpd. * * @dn: The genpd device node * @states: The pointer to which the state array will be saved. * @n: The count of elements in the array returned from this function. * * Returns the device states parsed from the OF node. The memory for the states * is allocated by this function and is the responsibility of the caller to * free the memory after use. If any or zero compatible domain idle states is * found it returns 0 and in case of errors, a negative error code is returned.
*/ int of_genpd_parse_idle_states(struct device_node *dn, struct genpd_power_state **states, int *n)
{ struct genpd_power_state *st; int ret;
ret = genpd_iterate_idle_states(dn, NULL); if (ret < 0) return ret;
if (!ret) {
*states = NULL;
*n = 0; return 0;
}
st = kcalloc(ret, sizeof(*st), GFP_KERNEL); if (!st) return -ENOMEM;
ret = genpd_iterate_idle_states(dn, st); if (ret <= 0) {
kfree(st); return ret < 0 ? ret : -EINVAL;
}
/** * of_genpd_sync_state() - A common sync_state function for genpd providers * @np: The device node the genpd provider is associated with. * * The @np that corresponds to a genpd provider may provide one or multiple * genpds. This function makes use @np to find the genpds that belongs to the * provider. For each genpd we try a power-off.
*/ void of_genpd_sync_state(struct device_node *np)
{ struct generic_pm_domain *genpd;
#ifdef CONFIG_DEBUG_FS /* * TODO: This function is a slightly modified version of rtpm_status_show * from sysfs.c, so generalize it.
*/ staticvoid rtpm_status_str(struct seq_file *s, struct device *dev)
{ staticconstchar * const status_lookup[] = {
[RPM_ACTIVE] = "active",
[RPM_RESUMING] = "resuming",
[RPM_SUSPENDED] = "suspended",
[RPM_SUSPENDING] = "suspending"
}; constchar *p = "";
if (dev->power.runtime_error)
p = "error"; elseif (dev->power.disable_depth)
p = "unsupported"; elseif (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
p = status_lookup[dev->power.runtime_status]; else
WARN_ON(1);
/* * Modifications on the list require holding locks on both * parent and child, so we are safe. * Also the device name is immutable.
*/
list_for_each_entry(link, &genpd->parent_links, parent_node) { if (list_is_first(&link->parent_node, &genpd->parent_links))
seq_printf(s, "\n%48s", " ");
seq_printf(s, "%s", link->child->name); if (!list_is_last(&link->parent_node, &genpd->parent_links))
seq_puts(s, ", ");
}
staticint summary_show(struct seq_file *s, void *data)
{ struct generic_pm_domain *genpd; int ret = 0;
seq_puts(s, "domain status children performance\n");
seq_puts(s, " /device runtime status managed by\n");
seq_puts(s, "------------------------------------------------------------------------------\n");
ret = mutex_lock_interruptible(&gpd_list_lock); if (ret) return -ERESTARTSYS;
list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
ret = genpd_summary_one(s, genpd); if (ret) break;
}
mutex_unlock(&gpd_list_lock);
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.