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 | 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 | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 53 | constraint_ns = dev_pm_qos_read_value(dev); |
| 54 | if (constraint_ns < 0) |
| 55 | return false; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 56 | |
Rafael J. Wysocki | a5bef810 | 2012-04-29 22:54:17 +0200 | [diff] [blame] | 57 | constraint_ns *= NSEC_PER_USEC; |
| 58 | /* |
| 59 | * We can walk the children without any additional locking, because |
| 60 | * they all have been suspended at this point. |
| 61 | */ |
| 62 | if (!dev->power.ignore_children) |
| 63 | device_for_each_child(dev, &constraint_ns, |
| 64 | dev_update_qos_constraint); |
| 65 | |
| 66 | if (constraint_ns > 0) { |
| 67 | constraint_ns -= td->start_latency_ns; |
| 68 | if (constraint_ns == 0) |
| 69 | return false; |
| 70 | } |
| 71 | td->effective_constraint_ns = constraint_ns; |
| 72 | /* |
| 73 | * The children have been suspended already, so we don't need to take |
| 74 | * their stop latencies into account here. |
| 75 | */ |
| 76 | return constraint_ns > td->stop_latency_ns || constraint_ns == 0; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 79 | /** |
| 80 | * default_power_down_ok - Default generic PM domain power off governor routine. |
| 81 | * @pd: PM domain to check. |
| 82 | * |
| 83 | * This routine must be executed under the PM domain's lock. |
| 84 | */ |
| 85 | static bool default_power_down_ok(struct dev_pm_domain *pd) |
| 86 | { |
| 87 | struct generic_pm_domain *genpd = pd_to_genpd(pd); |
| 88 | struct gpd_link *link; |
| 89 | struct pm_domain_data *pdd; |
| 90 | s64 min_dev_off_time_ns; |
| 91 | s64 off_on_time_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 92 | |
| 93 | off_on_time_ns = genpd->power_off_latency_ns + |
| 94 | genpd->power_on_latency_ns; |
| 95 | /* |
| 96 | * It doesn't make sense to remove power from the domain if saving |
| 97 | * the state of all devices in it and the power off/power on operations |
| 98 | * take too much time. |
| 99 | * |
| 100 | * All devices in this domain have been stopped already at this point. |
| 101 | */ |
| 102 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 103 | if (pdd->dev->driver) |
| 104 | off_on_time_ns += |
| 105 | to_gpd_data(pdd)->td.save_state_latency_ns; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Check if subdomains can be off for enough time. |
| 110 | * |
| 111 | * All subdomains have been powered off already at this point. |
| 112 | */ |
| 113 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 114 | struct generic_pm_domain *sd = link->slave; |
| 115 | s64 sd_max_off_ns = sd->max_off_time_ns; |
| 116 | |
| 117 | if (sd_max_off_ns < 0) |
| 118 | continue; |
| 119 | |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 120 | /* |
| 121 | * Check if the subdomain is allowed to be off long enough for |
| 122 | * the current domain to turn off and on (that's how much time |
| 123 | * it will have to wait worst case). |
| 124 | */ |
| 125 | if (sd_max_off_ns <= off_on_time_ns) |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Check if the devices in the domain can be off enough time. |
| 131 | */ |
| 132 | min_dev_off_time_ns = -1; |
| 133 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 134 | struct gpd_timing_data *td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 135 | s64 constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 136 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 137 | if (!pdd->dev->driver) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 138 | continue; |
| 139 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Check if the device is allowed to be off long enough for the |
| 142 | * domain to turn off and on (that's how much time it will |
| 143 | * have to wait worst case). |
| 144 | */ |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 145 | td = &to_gpd_data(pdd)->td; |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 146 | constraint_ns = td->effective_constraint_ns; |
| 147 | /* default_stop_ok() need not be called before us. */ |
| 148 | if (constraint_ns < 0) { |
| 149 | constraint_ns = dev_pm_qos_read_value(pdd->dev); |
| 150 | constraint_ns *= NSEC_PER_USEC; |
| 151 | } |
| 152 | if (constraint_ns == 0) |
| 153 | continue; |
| 154 | |
| 155 | /* |
| 156 | * constraint_ns cannot be negative here, because the device has |
| 157 | * been suspended. |
| 158 | */ |
| 159 | constraint_ns -= td->restore_state_latency_ns; |
| 160 | if (constraint_ns <= off_on_time_ns) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 161 | return false; |
| 162 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 163 | if (min_dev_off_time_ns > constraint_ns |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 164 | || min_dev_off_time_ns < 0) |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 165 | min_dev_off_time_ns = constraint_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 168 | /* |
| 169 | * If the computed minimum device off time is negative, there are no |
| 170 | * latency constraints, so the domain can spend arbitrary time in the |
| 171 | * "off" state. |
| 172 | */ |
| 173 | if (min_dev_off_time_ns < 0) |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 174 | return true; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 175 | |
| 176 | /* |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 177 | * The difference between the computed minimum device off time and the |
| 178 | * time needed to turn the domain on is the maximum theoretical time |
| 179 | * this domain can spend in the "off" state. |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 180 | */ |
Rafael J. Wysocki | dd8683e | 2012-04-29 22:54:30 +0200 | [diff] [blame] | 181 | genpd->max_off_time_ns = min_dev_off_time_ns - |
| 182 | genpd->power_on_latency_ns; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 186 | static bool always_on_power_down_ok(struct dev_pm_domain *domain) |
| 187 | { |
| 188 | return false; |
| 189 | } |
| 190 | |
Rafael J. Wysocki | e59a8db | 2012-01-14 00:39:36 +0100 | [diff] [blame] | 191 | #else /* !CONFIG_PM_RUNTIME */ |
| 192 | |
| 193 | bool default_stop_ok(struct device *dev) |
| 194 | { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | #define default_power_down_ok NULL |
| 199 | #define always_on_power_down_ok NULL |
| 200 | |
| 201 | #endif /* !CONFIG_PM_RUNTIME */ |
| 202 | |
| 203 | struct dev_power_governor simple_qos_governor = { |
| 204 | .stop_ok = default_stop_ok, |
| 205 | .power_down_ok = default_power_down_ok, |
| 206 | }; |
| 207 | |
Mark Brown | 925b44a | 2011-12-08 23:27:28 +0100 | [diff] [blame] | 208 | /** |
| 209 | * pm_genpd_gov_always_on - A governor implementing an always-on policy |
| 210 | */ |
| 211 | struct dev_power_governor pm_domain_always_on_gov = { |
| 212 | .power_down_ok = always_on_power_down_ok, |
| 213 | .stop_ok = default_stop_ok, |
| 214 | }; |