Greg Kroah-Hartman | 5de363b | 2019-04-02 15:32:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * drivers/base/power/domain_governor.c - Governors for device PM domains. |
| 4 | * |
| 5 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 6 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/pm_domain.h> |
| 9 | #include <linux/pm_qos.h> |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 10 | #include <linux/hrtimer.h> |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 11 | #include <linux/cpuidle.h> |
| 12 | #include <linux/cpumask.h> |
| 13 | #include <linux/ktime.h> |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 14 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 15 | static int dev_update_qos_constraint(struct device *dev, void *data) |
| 16 | { |
| 17 | s64 *constraint_ns_p = data; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 18 | s64 constraint_ns; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 19 | |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 20 | if (dev->power.subsys_data && dev->power.subsys_data->domain_data) { |
| 21 | /* |
| 22 | * Only take suspend-time QoS constraints of devices into |
| 23 | * account, because constraints updated after the device has |
| 24 | * been suspended are not guaranteed to be taken into account |
| 25 | * anyway. In order for them to take effect, the device has to |
| 26 | * be resumed and suspended again. |
| 27 | */ |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 28 | constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 29 | } else { |
| 30 | /* |
| 31 | * The child is not in a domain and there's no info on its |
| 32 | * suspend/resume latencies, so assume them to be negligible and |
| 33 | * take its current PM QoS constraint (that's the only thing |
| 34 | * known at this point anyway). |
| 35 | */ |
Viresh Kumar | 2a79ea5 | 2019-07-04 13:06:19 +0530 | [diff] [blame] | 36 | constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY); |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 37 | constraint_ns *= NSEC_PER_USEC; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 38 | } |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 39 | |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 40 | if (constraint_ns < *constraint_ns_p) |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 41 | *constraint_ns_p = constraint_ns; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 46 | /** |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 47 | * default_suspend_ok - Default PM domain governor routine to suspend devices. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 48 | * @dev: Device to check. |
| 49 | */ |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 50 | static bool default_suspend_ok(struct device *dev) |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 51 | { |
| 52 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 53 | unsigned long flags; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 54 | s64 constraint_ns; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 55 | |
| 56 | dev_dbg(dev, "%s()\n", __func__); |
| 57 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 58 | spin_lock_irqsave(&dev->power.lock, flags); |
| 59 | |
| 60 | if (!td->constraint_changed) { |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 61 | bool ret = td->cached_suspend_ok; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 62 | |
| 63 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 64 | return ret; |
| 65 | } |
| 66 | td->constraint_changed = false; |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 67 | td->cached_suspend_ok = false; |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 68 | td->effective_constraint_ns = 0; |
Viresh Kumar | 8262331 | 2019-07-04 13:06:18 +0530 | [diff] [blame] | 69 | constraint_ns = __dev_pm_qos_resume_latency(dev); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 70 | |
| 71 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 72 | |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 73 | if (constraint_ns == 0) |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 74 | return false; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 75 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 76 | constraint_ns *= NSEC_PER_USEC; |
| 77 | /* |
| 78 | * We can walk the children without any additional locking, because |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 79 | * they all have been suspended at this point and their |
| 80 | * effective_constraint_ns fields won't be modified in parallel with us. |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 81 | */ |
| 82 | if (!dev->power.ignore_children) |
| 83 | device_for_each_child(dev, &constraint_ns, |
| 84 | dev_update_qos_constraint); |
| 85 | |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 86 | if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) { |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 87 | /* "No restriction", so the device is allowed to suspend. */ |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 88 | td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 89 | td->cached_suspend_ok = true; |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 90 | } else if (constraint_ns == 0) { |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 91 | /* |
| 92 | * This triggers if one of the children that don't belong to a |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 93 | * domain has a zero PM QoS constraint and it's better not to |
| 94 | * suspend then. effective_constraint_ns is zero already and |
| 95 | * cached_suspend_ok is false, so bail out. |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 96 | */ |
| 97 | return false; |
| 98 | } else { |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 99 | constraint_ns -= td->suspend_latency_ns + |
| 100 | td->resume_latency_ns; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 101 | /* |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 102 | * effective_constraint_ns is zero already and cached_suspend_ok |
| 103 | * is false, so if the computed value is not positive, return |
| 104 | * right away. |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 105 | */ |
| 106 | if (constraint_ns <= 0) |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 107 | return false; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 108 | |
| 109 | td->effective_constraint_ns = constraint_ns; |
| 110 | td->cached_suspend_ok = true; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 111 | } |
Ulf Hansson | a98f1b7 | 2015-10-13 16:10:28 +0200 | [diff] [blame] | 112 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 113 | /* |
| 114 | * The children have been suspended already, so we don't need to take |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 115 | * their suspend latencies into account here. |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 116 | */ |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 117 | return td->cached_suspend_ok; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 120 | static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now) |
| 121 | { |
| 122 | ktime_t domain_wakeup = KTIME_MAX; |
| 123 | ktime_t next_wakeup; |
| 124 | struct pm_domain_data *pdd; |
| 125 | struct gpd_link *link; |
| 126 | |
| 127 | if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY)) |
| 128 | return; |
| 129 | |
| 130 | /* |
| 131 | * Devices that have a predictable wakeup pattern, may specify |
| 132 | * their next wakeup. Let's find the next wakeup from all the |
| 133 | * devices attached to this domain and from all the sub-domains. |
| 134 | * It is possible that component's a next wakeup may have become |
| 135 | * stale when we read that here. We will ignore to ensure the domain |
| 136 | * is able to enter its optimal idle state. |
| 137 | */ |
| 138 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 139 | next_wakeup = to_gpd_data(pdd)->next_wakeup; |
| 140 | if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now)) |
| 141 | if (ktime_before(next_wakeup, domain_wakeup)) |
| 142 | domain_wakeup = next_wakeup; |
| 143 | } |
| 144 | |
| 145 | list_for_each_entry(link, &genpd->parent_links, parent_node) { |
| 146 | next_wakeup = link->child->next_wakeup; |
| 147 | if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now)) |
| 148 | if (ktime_before(next_wakeup, domain_wakeup)) |
| 149 | domain_wakeup = next_wakeup; |
| 150 | } |
| 151 | |
| 152 | genpd->next_wakeup = domain_wakeup; |
| 153 | } |
| 154 | |
| 155 | static bool next_wakeup_allows_state(struct generic_pm_domain *genpd, |
| 156 | unsigned int state, ktime_t now) |
| 157 | { |
| 158 | ktime_t domain_wakeup = genpd->next_wakeup; |
| 159 | s64 idle_time_ns, min_sleep_ns; |
| 160 | |
| 161 | min_sleep_ns = genpd->states[state].power_off_latency_ns + |
| 162 | genpd->states[state].residency_ns; |
| 163 | |
| 164 | idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now)); |
| 165 | |
| 166 | return idle_time_ns >= min_sleep_ns; |
| 167 | } |
| 168 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 169 | static bool __default_power_down_ok(struct dev_pm_domain *pd, |
| 170 | unsigned int state) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 171 | { |
| 172 | struct generic_pm_domain *genpd = pd_to_genpd(pd); |
| 173 | struct gpd_link *link; |
| 174 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 175 | s64 min_off_time_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 176 | s64 off_on_time_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 177 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 178 | off_on_time_ns = genpd->states[state].power_off_latency_ns + |
| 179 | genpd->states[state].power_on_latency_ns; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 180 | |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 181 | min_off_time_ns = -1; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 182 | /* |
| 183 | * Check if subdomains can be off for enough time. |
| 184 | * |
| 185 | * All subdomains have been powered off already at this point. |
| 186 | */ |
Kees Cook | 8d87ae4 | 2020-07-08 16:32:13 -0700 | [diff] [blame] | 187 | list_for_each_entry(link, &genpd->parent_links, parent_node) { |
| 188 | struct generic_pm_domain *sd = link->child; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 189 | s64 sd_max_off_ns = sd->max_off_time_ns; |
| 190 | |
| 191 | if (sd_max_off_ns < 0) |
| 192 | continue; |
| 193 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Check if the subdomain is allowed to be off long enough for |
| 196 | * the current domain to turn off and on (that's how much time |
| 197 | * it will have to wait worst case). |
| 198 | */ |
| 199 | if (sd_max_off_ns <= off_on_time_ns) |
| 200 | return false; |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 201 | |
| 202 | if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0) |
| 203 | min_off_time_ns = sd_max_off_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Check if the devices in the domain can be off enough time. |
| 208 | */ |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 209 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 210 | struct gpd_timing_data *td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 211 | s64 constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 212 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 213 | /* |
| 214 | * Check if the device is allowed to be off long enough for the |
| 215 | * domain to turn off and on (that's how much time it will |
| 216 | * have to wait worst case). |
| 217 | */ |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 218 | td = &to_gpd_data(pdd)->td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 219 | constraint_ns = td->effective_constraint_ns; |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 220 | /* |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 221 | * Zero means "no suspend at all" and this runs only when all |
| 222 | * devices in the domain are suspended, so it must be positive. |
Rafael J. Wysocki | 704d2ce | 2017-11-07 02:23:18 +0100 | [diff] [blame] | 223 | */ |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 224 | if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 225 | continue; |
| 226 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 227 | if (constraint_ns <= off_on_time_ns) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 228 | return false; |
| 229 | |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 230 | if (min_off_time_ns > constraint_ns || min_off_time_ns < 0) |
| 231 | min_off_time_ns = constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 234 | /* |
| 235 | * If the computed minimum device off time is negative, there are no |
| 236 | * latency constraints, so the domain can spend arbitrary time in the |
| 237 | * "off" state. |
| 238 | */ |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 239 | if (min_off_time_ns < 0) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 240 | return true; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 241 | |
| 242 | /* |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 243 | * The difference between the computed minimum subdomain or device off |
| 244 | * time and the time needed to turn the domain on is the maximum |
| 245 | * theoretical time this domain can spend in the "off" state. |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 246 | */ |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 247 | genpd->max_off_time_ns = min_off_time_ns - |
| 248 | genpd->states[state].power_on_latency_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
Krzysztof Kozlowski | 268cd2e | 2017-06-28 16:56:21 +0200 | [diff] [blame] | 252 | /** |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 253 | * _default_power_down_ok - Default generic PM domain power off governor routine. |
Krzysztof Kozlowski | 268cd2e | 2017-06-28 16:56:21 +0200 | [diff] [blame] | 254 | * @pd: PM domain to check. |
Yang Yingliang | 763663c | 2021-05-12 15:25:15 +0800 | [diff] [blame] | 255 | * @now: current ktime. |
Krzysztof Kozlowski | 268cd2e | 2017-06-28 16:56:21 +0200 | [diff] [blame] | 256 | * |
| 257 | * This routine must be executed under the PM domain's lock. |
| 258 | */ |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 259 | static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now) |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 260 | { |
| 261 | struct generic_pm_domain *genpd = pd_to_genpd(pd); |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 262 | int state_idx = genpd->state_count - 1; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 263 | struct gpd_link *link; |
| 264 | |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Find the next wakeup from devices that can determine their own wakeup |
| 267 | * to find when the domain would wakeup and do it for every device down |
| 268 | * the hierarchy. It is not worth while to sleep if the state's residency |
| 269 | * cannot be met. |
| 270 | */ |
| 271 | update_domain_next_wakeup(genpd, now); |
| 272 | if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (genpd->next_wakeup != KTIME_MAX)) { |
| 273 | /* Let's find out the deepest domain idle state, the devices prefer */ |
| 274 | while (state_idx >= 0) { |
| 275 | if (next_wakeup_allows_state(genpd, state_idx, now)) { |
| 276 | genpd->max_off_time_changed = true; |
| 277 | break; |
| 278 | } |
| 279 | state_idx--; |
| 280 | } |
| 281 | |
| 282 | if (state_idx < 0) { |
| 283 | state_idx = 0; |
| 284 | genpd->cached_power_down_ok = false; |
| 285 | goto done; |
| 286 | } |
| 287 | } |
| 288 | |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 289 | if (!genpd->max_off_time_changed) { |
| 290 | genpd->state_idx = genpd->cached_power_down_state_idx; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 291 | return genpd->cached_power_down_ok; |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 292 | } |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 293 | |
| 294 | /* |
Kees Cook | 8d87ae4 | 2020-07-08 16:32:13 -0700 | [diff] [blame] | 295 | * We have to invalidate the cached results for the parents, so |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 296 | * use the observation that default_power_down_ok() is not |
Kees Cook | 8d87ae4 | 2020-07-08 16:32:13 -0700 | [diff] [blame] | 297 | * going to be called for any parent until this instance |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 298 | * returns. |
| 299 | */ |
Kees Cook | 8d87ae4 | 2020-07-08 16:32:13 -0700 | [diff] [blame] | 300 | list_for_each_entry(link, &genpd->child_links, child_node) |
| 301 | link->parent->max_off_time_changed = true; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 302 | |
| 303 | genpd->max_off_time_ns = -1; |
| 304 | genpd->max_off_time_changed = false; |
| 305 | genpd->cached_power_down_ok = true; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 306 | |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 307 | /* |
| 308 | * Find a state to power down to, starting from the state |
| 309 | * determined by the next wakeup. |
| 310 | */ |
| 311 | while (!__default_power_down_ok(pd, state_idx)) { |
| 312 | if (state_idx == 0) { |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 313 | genpd->cached_power_down_ok = false; |
| 314 | break; |
| 315 | } |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 316 | state_idx--; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 319 | done: |
| 320 | genpd->state_idx = state_idx; |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 321 | genpd->cached_power_down_state_idx = genpd->state_idx; |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 322 | return genpd->cached_power_down_ok; |
| 323 | } |
| 324 | |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 325 | static bool default_power_down_ok(struct dev_pm_domain *pd) |
| 326 | { |
| 327 | return _default_power_down_ok(pd, ktime_get()); |
| 328 | } |
| 329 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 330 | static bool always_on_power_down_ok(struct dev_pm_domain *domain) |
| 331 | { |
| 332 | return false; |
| 333 | } |
| 334 | |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 335 | #ifdef CONFIG_CPU_IDLE |
| 336 | static bool cpu_power_down_ok(struct dev_pm_domain *pd) |
| 337 | { |
| 338 | struct generic_pm_domain *genpd = pd_to_genpd(pd); |
| 339 | struct cpuidle_device *dev; |
| 340 | ktime_t domain_wakeup, next_hrtimer; |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 341 | ktime_t now = ktime_get(); |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 342 | s64 idle_duration_ns; |
| 343 | int cpu, i; |
| 344 | |
| 345 | /* Validate dev PM QoS constraints. */ |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 346 | if (!_default_power_down_ok(pd, now)) |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 347 | return false; |
| 348 | |
| 349 | if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN)) |
| 350 | return true; |
| 351 | |
| 352 | /* |
| 353 | * Find the next wakeup for any of the online CPUs within the PM domain |
| 354 | * and its subdomains. Note, we only need the genpd->cpus, as it already |
| 355 | * contains a mask of all CPUs from subdomains. |
| 356 | */ |
| 357 | domain_wakeup = ktime_set(KTIME_SEC_MAX, 0); |
| 358 | for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) { |
| 359 | dev = per_cpu(cpuidle_devices, cpu); |
| 360 | if (dev) { |
| 361 | next_hrtimer = READ_ONCE(dev->next_hrtimer); |
| 362 | if (ktime_before(next_hrtimer, domain_wakeup)) |
| 363 | domain_wakeup = next_hrtimer; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* The minimum idle duration is from now - until the next wakeup. */ |
Lina Iyer | c79aa08 | 2021-01-20 08:50:42 -0700 | [diff] [blame] | 368 | idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now)); |
Ulf Hansson | e949996 | 2019-04-11 20:17:33 +0200 | [diff] [blame] | 369 | if (idle_duration_ns <= 0) |
| 370 | return false; |
| 371 | |
| 372 | /* |
| 373 | * Find the deepest idle state that has its residency value satisfied |
| 374 | * and by also taking into account the power off latency for the state. |
| 375 | * Start at the state picked by the dev PM QoS constraint validation. |
| 376 | */ |
| 377 | i = genpd->state_idx; |
| 378 | do { |
| 379 | if (idle_duration_ns >= (genpd->states[i].residency_ns + |
| 380 | genpd->states[i].power_off_latency_ns)) { |
| 381 | genpd->state_idx = i; |
| 382 | return true; |
| 383 | } |
| 384 | } while (--i >= 0); |
| 385 | |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | struct dev_power_governor pm_domain_cpu_gov = { |
| 390 | .suspend_ok = default_suspend_ok, |
| 391 | .power_down_ok = cpu_power_down_ok, |
| 392 | }; |
| 393 | #endif |
| 394 | |
Rafael J. Wysocki | e59a8db | 2012-01-14 00:39:36 +0100 | [diff] [blame] | 395 | struct dev_power_governor simple_qos_governor = { |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 396 | .suspend_ok = default_suspend_ok, |
Rafael J. Wysocki | e59a8db | 2012-01-14 00:39:36 +0100 | [diff] [blame] | 397 | .power_down_ok = default_power_down_ok, |
| 398 | }; |
| 399 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 400 | /** |
| 401 | * pm_genpd_gov_always_on - A governor implementing an always-on policy |
| 402 | */ |
| 403 | struct dev_power_governor pm_domain_always_on_gov = { |
| 404 | .power_down_ok = always_on_power_down_ok, |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 405 | .suspend_ok = default_suspend_ok, |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 406 | }; |