Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/base/power/domain_governor.c - Governors for device PM domains.
0004  *
0005  * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/pm_domain.h>
0009 #include <linux/pm_qos.h>
0010 #include <linux/hrtimer.h>
0011 #include <linux/cpuidle.h>
0012 #include <linux/cpumask.h>
0013 #include <linux/ktime.h>
0014 
0015 static int dev_update_qos_constraint(struct device *dev, void *data)
0016 {
0017     s64 *constraint_ns_p = data;
0018     s64 constraint_ns;
0019 
0020     if (dev->power.subsys_data && dev->power.subsys_data->domain_data) {
0021         struct gpd_timing_data *td = dev_gpd_data(dev)->td;
0022 
0023         /*
0024          * Only take suspend-time QoS constraints of devices into
0025          * account, because constraints updated after the device has
0026          * been suspended are not guaranteed to be taken into account
0027          * anyway.  In order for them to take effect, the device has to
0028          * be resumed and suspended again.
0029          */
0030         constraint_ns = td ? td->effective_constraint_ns :
0031                 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
0032     } else {
0033         /*
0034          * The child is not in a domain and there's no info on its
0035          * suspend/resume latencies, so assume them to be negligible and
0036          * take its current PM QoS constraint (that's the only thing
0037          * known at this point anyway).
0038          */
0039         constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY);
0040         constraint_ns *= NSEC_PER_USEC;
0041     }
0042 
0043     if (constraint_ns < *constraint_ns_p)
0044         *constraint_ns_p = constraint_ns;
0045 
0046     return 0;
0047 }
0048 
0049 /**
0050  * default_suspend_ok - Default PM domain governor routine to suspend devices.
0051  * @dev: Device to check.
0052  */
0053 static bool default_suspend_ok(struct device *dev)
0054 {
0055     struct gpd_timing_data *td = dev_gpd_data(dev)->td;
0056     unsigned long flags;
0057     s64 constraint_ns;
0058 
0059     dev_dbg(dev, "%s()\n", __func__);
0060 
0061     spin_lock_irqsave(&dev->power.lock, flags);
0062 
0063     if (!td->constraint_changed) {
0064         bool ret = td->cached_suspend_ok;
0065 
0066         spin_unlock_irqrestore(&dev->power.lock, flags);
0067         return ret;
0068     }
0069     td->constraint_changed = false;
0070     td->cached_suspend_ok = false;
0071     td->effective_constraint_ns = 0;
0072     constraint_ns = __dev_pm_qos_resume_latency(dev);
0073 
0074     spin_unlock_irqrestore(&dev->power.lock, flags);
0075 
0076     if (constraint_ns == 0)
0077         return false;
0078 
0079     constraint_ns *= NSEC_PER_USEC;
0080     /*
0081      * We can walk the children without any additional locking, because
0082      * they all have been suspended at this point and their
0083      * effective_constraint_ns fields won't be modified in parallel with us.
0084      */
0085     if (!dev->power.ignore_children)
0086         device_for_each_child(dev, &constraint_ns,
0087                       dev_update_qos_constraint);
0088 
0089     if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
0090         /* "No restriction", so the device is allowed to suspend. */
0091         td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
0092         td->cached_suspend_ok = true;
0093     } else if (constraint_ns == 0) {
0094         /*
0095          * This triggers if one of the children that don't belong to a
0096          * domain has a zero PM QoS constraint and it's better not to
0097          * suspend then.  effective_constraint_ns is zero already and
0098          * cached_suspend_ok is false, so bail out.
0099          */
0100         return false;
0101     } else {
0102         constraint_ns -= td->suspend_latency_ns +
0103                 td->resume_latency_ns;
0104         /*
0105          * effective_constraint_ns is zero already and cached_suspend_ok
0106          * is false, so if the computed value is not positive, return
0107          * right away.
0108          */
0109         if (constraint_ns <= 0)
0110             return false;
0111 
0112         td->effective_constraint_ns = constraint_ns;
0113         td->cached_suspend_ok = true;
0114     }
0115 
0116     /*
0117      * The children have been suspended already, so we don't need to take
0118      * their suspend latencies into account here.
0119      */
0120     return td->cached_suspend_ok;
0121 }
0122 
0123 static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now)
0124 {
0125     ktime_t domain_wakeup = KTIME_MAX;
0126     ktime_t next_wakeup;
0127     struct pm_domain_data *pdd;
0128     struct gpd_link *link;
0129 
0130     if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY))
0131         return;
0132 
0133     /*
0134      * Devices that have a predictable wakeup pattern, may specify
0135      * their next wakeup. Let's find the next wakeup from all the
0136      * devices attached to this domain and from all the sub-domains.
0137      * It is possible that component's a next wakeup may have become
0138      * stale when we read that here. We will ignore to ensure the domain
0139      * is able to enter its optimal idle state.
0140      */
0141     list_for_each_entry(pdd, &genpd->dev_list, list_node) {
0142         next_wakeup = to_gpd_data(pdd)->td->next_wakeup;
0143         if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
0144             if (ktime_before(next_wakeup, domain_wakeup))
0145                 domain_wakeup = next_wakeup;
0146     }
0147 
0148     list_for_each_entry(link, &genpd->parent_links, parent_node) {
0149         struct genpd_governor_data *cgd = link->child->gd;
0150 
0151         next_wakeup = cgd ? cgd->next_wakeup : KTIME_MAX;
0152         if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
0153             if (ktime_before(next_wakeup, domain_wakeup))
0154                 domain_wakeup = next_wakeup;
0155     }
0156 
0157     genpd->gd->next_wakeup = domain_wakeup;
0158 }
0159 
0160 static bool next_wakeup_allows_state(struct generic_pm_domain *genpd,
0161                      unsigned int state, ktime_t now)
0162 {
0163     ktime_t domain_wakeup = genpd->gd->next_wakeup;
0164     s64 idle_time_ns, min_sleep_ns;
0165 
0166     min_sleep_ns = genpd->states[state].power_off_latency_ns +
0167                genpd->states[state].residency_ns;
0168 
0169     idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
0170 
0171     return idle_time_ns >= min_sleep_ns;
0172 }
0173 
0174 static bool __default_power_down_ok(struct dev_pm_domain *pd,
0175                      unsigned int state)
0176 {
0177     struct generic_pm_domain *genpd = pd_to_genpd(pd);
0178     struct gpd_link *link;
0179     struct pm_domain_data *pdd;
0180     s64 min_off_time_ns;
0181     s64 off_on_time_ns;
0182 
0183     off_on_time_ns = genpd->states[state].power_off_latency_ns +
0184         genpd->states[state].power_on_latency_ns;
0185 
0186     min_off_time_ns = -1;
0187     /*
0188      * Check if subdomains can be off for enough time.
0189      *
0190      * All subdomains have been powered off already at this point.
0191      */
0192     list_for_each_entry(link, &genpd->parent_links, parent_node) {
0193         struct genpd_governor_data *cgd = link->child->gd;
0194 
0195         s64 sd_max_off_ns = cgd ? cgd->max_off_time_ns : -1;
0196 
0197         if (sd_max_off_ns < 0)
0198             continue;
0199 
0200         /*
0201          * Check if the subdomain is allowed to be off long enough for
0202          * the current domain to turn off and on (that's how much time
0203          * it will have to wait worst case).
0204          */
0205         if (sd_max_off_ns <= off_on_time_ns)
0206             return false;
0207 
0208         if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
0209             min_off_time_ns = sd_max_off_ns;
0210     }
0211 
0212     /*
0213      * Check if the devices in the domain can be off enough time.
0214      */
0215     list_for_each_entry(pdd, &genpd->dev_list, list_node) {
0216         struct gpd_timing_data *td;
0217         s64 constraint_ns;
0218 
0219         /*
0220          * Check if the device is allowed to be off long enough for the
0221          * domain to turn off and on (that's how much time it will
0222          * have to wait worst case).
0223          */
0224         td = to_gpd_data(pdd)->td;
0225         constraint_ns = td->effective_constraint_ns;
0226         /*
0227          * Zero means "no suspend at all" and this runs only when all
0228          * devices in the domain are suspended, so it must be positive.
0229          */
0230         if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
0231             continue;
0232 
0233         if (constraint_ns <= off_on_time_ns)
0234             return false;
0235 
0236         if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
0237             min_off_time_ns = constraint_ns;
0238     }
0239 
0240     /*
0241      * If the computed minimum device off time is negative, there are no
0242      * latency constraints, so the domain can spend arbitrary time in the
0243      * "off" state.
0244      */
0245     if (min_off_time_ns < 0)
0246         return true;
0247 
0248     /*
0249      * The difference between the computed minimum subdomain or device off
0250      * time and the time needed to turn the domain on is the maximum
0251      * theoretical time this domain can spend in the "off" state.
0252      */
0253     genpd->gd->max_off_time_ns = min_off_time_ns -
0254         genpd->states[state].power_on_latency_ns;
0255     return true;
0256 }
0257 
0258 /**
0259  * _default_power_down_ok - Default generic PM domain power off governor routine.
0260  * @pd: PM domain to check.
0261  * @now: current ktime.
0262  *
0263  * This routine must be executed under the PM domain's lock.
0264  */
0265 static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now)
0266 {
0267     struct generic_pm_domain *genpd = pd_to_genpd(pd);
0268     struct genpd_governor_data *gd = genpd->gd;
0269     int state_idx = genpd->state_count - 1;
0270     struct gpd_link *link;
0271 
0272     /*
0273      * Find the next wakeup from devices that can determine their own wakeup
0274      * to find when the domain would wakeup and do it for every device down
0275      * the hierarchy. It is not worth while to sleep if the state's residency
0276      * cannot be met.
0277      */
0278     update_domain_next_wakeup(genpd, now);
0279     if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (gd->next_wakeup != KTIME_MAX)) {
0280         /* Let's find out the deepest domain idle state, the devices prefer */
0281         while (state_idx >= 0) {
0282             if (next_wakeup_allows_state(genpd, state_idx, now)) {
0283                 gd->max_off_time_changed = true;
0284                 break;
0285             }
0286             state_idx--;
0287         }
0288 
0289         if (state_idx < 0) {
0290             state_idx = 0;
0291             gd->cached_power_down_ok = false;
0292             goto done;
0293         }
0294     }
0295 
0296     if (!gd->max_off_time_changed) {
0297         genpd->state_idx = gd->cached_power_down_state_idx;
0298         return gd->cached_power_down_ok;
0299     }
0300 
0301     /*
0302      * We have to invalidate the cached results for the parents, so
0303      * use the observation that default_power_down_ok() is not
0304      * going to be called for any parent until this instance
0305      * returns.
0306      */
0307     list_for_each_entry(link, &genpd->child_links, child_node) {
0308         struct genpd_governor_data *pgd = link->parent->gd;
0309 
0310         if (pgd)
0311             pgd->max_off_time_changed = true;
0312     }
0313 
0314     gd->max_off_time_ns = -1;
0315     gd->max_off_time_changed = false;
0316     gd->cached_power_down_ok = true;
0317 
0318     /*
0319      * Find a state to power down to, starting from the state
0320      * determined by the next wakeup.
0321      */
0322     while (!__default_power_down_ok(pd, state_idx)) {
0323         if (state_idx == 0) {
0324             gd->cached_power_down_ok = false;
0325             break;
0326         }
0327         state_idx--;
0328     }
0329 
0330 done:
0331     genpd->state_idx = state_idx;
0332     gd->cached_power_down_state_idx = genpd->state_idx;
0333     return gd->cached_power_down_ok;
0334 }
0335 
0336 static bool default_power_down_ok(struct dev_pm_domain *pd)
0337 {
0338     return _default_power_down_ok(pd, ktime_get());
0339 }
0340 
0341 #ifdef CONFIG_CPU_IDLE
0342 static bool cpu_power_down_ok(struct dev_pm_domain *pd)
0343 {
0344     struct generic_pm_domain *genpd = pd_to_genpd(pd);
0345     struct cpuidle_device *dev;
0346     ktime_t domain_wakeup, next_hrtimer;
0347     ktime_t now = ktime_get();
0348     s64 idle_duration_ns;
0349     int cpu, i;
0350 
0351     /* Validate dev PM QoS constraints. */
0352     if (!_default_power_down_ok(pd, now))
0353         return false;
0354 
0355     if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN))
0356         return true;
0357 
0358     /*
0359      * Find the next wakeup for any of the online CPUs within the PM domain
0360      * and its subdomains. Note, we only need the genpd->cpus, as it already
0361      * contains a mask of all CPUs from subdomains.
0362      */
0363     domain_wakeup = ktime_set(KTIME_SEC_MAX, 0);
0364     for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) {
0365         dev = per_cpu(cpuidle_devices, cpu);
0366         if (dev) {
0367             next_hrtimer = READ_ONCE(dev->next_hrtimer);
0368             if (ktime_before(next_hrtimer, domain_wakeup))
0369                 domain_wakeup = next_hrtimer;
0370         }
0371     }
0372 
0373     /* The minimum idle duration is from now - until the next wakeup. */
0374     idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
0375     if (idle_duration_ns <= 0)
0376         return false;
0377 
0378     /*
0379      * Find the deepest idle state that has its residency value satisfied
0380      * and by also taking into account the power off latency for the state.
0381      * Start at the state picked by the dev PM QoS constraint validation.
0382      */
0383     i = genpd->state_idx;
0384     do {
0385         if (idle_duration_ns >= (genpd->states[i].residency_ns +
0386             genpd->states[i].power_off_latency_ns)) {
0387             genpd->state_idx = i;
0388             return true;
0389         }
0390     } while (--i >= 0);
0391 
0392     return false;
0393 }
0394 
0395 struct dev_power_governor pm_domain_cpu_gov = {
0396     .suspend_ok = default_suspend_ok,
0397     .power_down_ok = cpu_power_down_ok,
0398 };
0399 #endif
0400 
0401 struct dev_power_governor simple_qos_governor = {
0402     .suspend_ok = default_suspend_ok,
0403     .power_down_ok = default_power_down_ok,
0404 };
0405 
0406 /**
0407  * pm_genpd_gov_always_on - A governor implementing an always-on policy
0408  */
0409 struct dev_power_governor pm_domain_always_on_gov = {
0410     .suspend_ok = default_suspend_ok,
0411 };