Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/domain_governor.c - Governors for device PM domains. |
| 3 | * |
| 4 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. |
| 5 | * |
| 6 | * This file is released under the GPLv2. |
| 7 | */ |
| 8 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/pm_domain.h> |
| 11 | #include <linux/pm_qos.h> |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 12 | #include <linux/hrtimer.h> |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 13 | |
Rafael J. Wysocki | e59a8db | 2012-01-14 00:39:36 +0100 | [diff] [blame] | 14 | #ifdef CONFIG_PM_RUNTIME |
| 15 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 16 | static int dev_update_qos_constraint(struct device *dev, void *data) |
| 17 | { |
| 18 | s64 *constraint_ns_p = data; |
| 19 | s32 constraint_ns = -1; |
| 20 | |
| 21 | if (dev->power.subsys_data && dev->power.subsys_data->domain_data) |
| 22 | constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns; |
| 23 | |
| 24 | if (constraint_ns < 0) { |
| 25 | constraint_ns = dev_pm_qos_read_value(dev); |
| 26 | constraint_ns *= NSEC_PER_USEC; |
| 27 | } |
| 28 | if (constraint_ns == 0) |
| 29 | return 0; |
| 30 | |
| 31 | /* |
| 32 | * constraint_ns cannot be negative here, because the device has been |
| 33 | * suspended. |
| 34 | */ |
| 35 | if (constraint_ns < *constraint_ns_p || *constraint_ns_p == 0) |
| 36 | *constraint_ns_p = constraint_ns; |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 41 | /** |
| 42 | * default_stop_ok - Default PM domain governor routine for stopping devices. |
| 43 | * @dev: Device to check. |
| 44 | */ |
| 45 | bool default_stop_ok(struct device *dev) |
| 46 | { |
| 47 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 48 | unsigned long flags; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 49 | s64 constraint_ns; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 50 | |
| 51 | dev_dbg(dev, "%s()\n", __func__); |
| 52 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 53 | spin_lock_irqsave(&dev->power.lock, flags); |
| 54 | |
| 55 | if (!td->constraint_changed) { |
| 56 | bool ret = td->cached_stop_ok; |
| 57 | |
| 58 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 59 | return ret; |
| 60 | } |
| 61 | td->constraint_changed = false; |
| 62 | td->cached_stop_ok = false; |
| 63 | td->effective_constraint_ns = -1; |
| 64 | constraint_ns = __dev_pm_qos_read_value(dev); |
| 65 | |
| 66 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 67 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 68 | if (constraint_ns < 0) |
| 69 | return false; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 70 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 71 | constraint_ns *= NSEC_PER_USEC; |
| 72 | /* |
| 73 | * We can walk the children without any additional locking, because |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 74 | * they all have been suspended at this point and their |
| 75 | * 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] | 76 | */ |
| 77 | if (!dev->power.ignore_children) |
| 78 | device_for_each_child(dev, &constraint_ns, |
| 79 | dev_update_qos_constraint); |
| 80 | |
| 81 | if (constraint_ns > 0) { |
| 82 | constraint_ns -= td->start_latency_ns; |
| 83 | if (constraint_ns == 0) |
| 84 | return false; |
| 85 | } |
| 86 | td->effective_constraint_ns = constraint_ns; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 87 | td->cached_stop_ok = constraint_ns > td->stop_latency_ns || |
| 88 | constraint_ns == 0; |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 89 | /* |
| 90 | * The children have been suspended already, so we don't need to take |
| 91 | * their stop latencies into account here. |
| 92 | */ |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 93 | return td->cached_stop_ok; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 96 | /** |
| 97 | * default_power_down_ok - Default generic PM domain power off governor routine. |
| 98 | * @pd: PM domain to check. |
| 99 | * |
| 100 | * This routine must be executed under the PM domain's lock. |
| 101 | */ |
| 102 | static bool default_power_down_ok(struct dev_pm_domain *pd) |
| 103 | { |
| 104 | struct generic_pm_domain *genpd = pd_to_genpd(pd); |
| 105 | struct gpd_link *link; |
| 106 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 107 | s64 min_off_time_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 108 | s64 off_on_time_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 109 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 110 | if (genpd->max_off_time_changed) { |
| 111 | struct gpd_link *link; |
| 112 | |
| 113 | /* |
| 114 | * We have to invalidate the cached results for the masters, so |
| 115 | * use the observation that default_power_down_ok() is not |
| 116 | * going to be called for any master until this instance |
| 117 | * returns. |
| 118 | */ |
| 119 | list_for_each_entry(link, &genpd->slave_links, slave_node) |
| 120 | link->master->max_off_time_changed = true; |
| 121 | |
| 122 | genpd->max_off_time_changed = false; |
| 123 | genpd->cached_power_down_ok = false; |
| 124 | genpd->max_off_time_ns = -1; |
| 125 | } else { |
| 126 | return genpd->cached_power_down_ok; |
| 127 | } |
| 128 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 129 | off_on_time_ns = genpd->power_off_latency_ns + |
| 130 | genpd->power_on_latency_ns; |
| 131 | /* |
| 132 | * It doesn't make sense to remove power from the domain if saving |
| 133 | * the state of all devices in it and the power off/power on operations |
| 134 | * take too much time. |
| 135 | * |
| 136 | * All devices in this domain have been stopped already at this point. |
| 137 | */ |
| 138 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 139 | if (pdd->dev->driver) |
| 140 | off_on_time_ns += |
| 141 | to_gpd_data(pdd)->td.save_state_latency_ns; |
| 142 | } |
| 143 | |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 144 | min_off_time_ns = -1; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 145 | /* |
| 146 | * Check if subdomains can be off for enough time. |
| 147 | * |
| 148 | * All subdomains have been powered off already at this point. |
| 149 | */ |
| 150 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 151 | struct generic_pm_domain *sd = link->slave; |
| 152 | s64 sd_max_off_ns = sd->max_off_time_ns; |
| 153 | |
| 154 | if (sd_max_off_ns < 0) |
| 155 | continue; |
| 156 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 157 | /* |
| 158 | * Check if the subdomain is allowed to be off long enough for |
| 159 | * the current domain to turn off and on (that's how much time |
| 160 | * it will have to wait worst case). |
| 161 | */ |
| 162 | if (sd_max_off_ns <= off_on_time_ns) |
| 163 | return false; |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 164 | |
| 165 | if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0) |
| 166 | min_off_time_ns = sd_max_off_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Check if the devices in the domain can be off enough time. |
| 171 | */ |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 172 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 173 | struct gpd_timing_data *td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 174 | s64 constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 175 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 176 | if (!pdd->dev->driver) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 177 | continue; |
| 178 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Check if the device is allowed to be off long enough for the |
| 181 | * domain to turn off and on (that's how much time it will |
| 182 | * have to wait worst case). |
| 183 | */ |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 184 | td = &to_gpd_data(pdd)->td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 185 | constraint_ns = td->effective_constraint_ns; |
| 186 | /* default_stop_ok() need not be called before us. */ |
| 187 | if (constraint_ns < 0) { |
| 188 | constraint_ns = dev_pm_qos_read_value(pdd->dev); |
| 189 | constraint_ns *= NSEC_PER_USEC; |
| 190 | } |
| 191 | if (constraint_ns == 0) |
| 192 | continue; |
| 193 | |
| 194 | /* |
| 195 | * constraint_ns cannot be negative here, because the device has |
| 196 | * been suspended. |
| 197 | */ |
| 198 | constraint_ns -= td->restore_state_latency_ns; |
| 199 | if (constraint_ns <= off_on_time_ns) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 200 | return false; |
| 201 | |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 202 | if (min_off_time_ns > constraint_ns || min_off_time_ns < 0) |
| 203 | min_off_time_ns = constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 206 | genpd->cached_power_down_ok = true; |
| 207 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 208 | /* |
| 209 | * If the computed minimum device off time is negative, there are no |
| 210 | * latency constraints, so the domain can spend arbitrary time in the |
| 211 | * "off" state. |
| 212 | */ |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 213 | if (min_off_time_ns < 0) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 214 | return true; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 215 | |
| 216 | /* |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 217 | * The difference between the computed minimum subdomain or device off |
| 218 | * time and the time needed to turn the domain on is the maximum |
| 219 | * theoretical time this domain can spend in the "off" state. |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 220 | */ |
Rafael J. Wysocki | b723b0e | 2012-05-07 22:00:59 +0200 | [diff] [blame] | 221 | genpd->max_off_time_ns = min_off_time_ns - genpd->power_on_latency_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 225 | static bool always_on_power_down_ok(struct dev_pm_domain *domain) |
| 226 | { |
| 227 | return false; |
| 228 | } |
| 229 | |
Rafael J. Wysocki | e59a8db | 2012-01-14 00:39:36 +0100 | [diff] [blame] | 230 | #else /* !CONFIG_PM_RUNTIME */ |
| 231 | |
| 232 | bool default_stop_ok(struct device *dev) |
| 233 | { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | #define default_power_down_ok NULL |
| 238 | #define always_on_power_down_ok NULL |
| 239 | |
| 240 | #endif /* !CONFIG_PM_RUNTIME */ |
| 241 | |
| 242 | struct dev_power_governor simple_qos_governor = { |
| 243 | .stop_ok = default_stop_ok, |
| 244 | .power_down_ok = default_power_down_ok, |
| 245 | }; |
| 246 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 247 | /** |
| 248 | * pm_genpd_gov_always_on - A governor implementing an always-on policy |
| 249 | */ |
| 250 | struct dev_power_governor pm_domain_always_on_gov = { |
| 251 | .power_down_ok = always_on_power_down_ok, |
| 252 | .stop_ok = default_stop_ok, |
| 253 | }; |