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