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