blob: 99896fbf18e439003095ccf18f2689ec6b7c1be3 [file] [log] [blame]
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001/*
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. Wysockib02c9992011-12-01 00:02:05 +01009#include <linux/kernel.h>
10#include <linux/pm_domain.h>
11#include <linux/pm_qos.h>
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010012#include <linux/hrtimer.h>
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010013
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020014static int dev_update_qos_constraint(struct device *dev, void *data)
15{
16 s64 *constraint_ns_p = data;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010017 s64 constraint_ns;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020018
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010019 if (dev->power.subsys_data && dev->power.subsys_data->domain_data) {
20 /*
21 * Only take suspend-time QoS constraints of devices into
22 * account, because constraints updated after the device has
23 * been suspended are not guaranteed to be taken into account
24 * anyway. In order for them to take effect, the device has to
25 * be resumed and suspended again.
26 */
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020027 constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010028 } else {
29 /*
30 * The child is not in a domain and there's no info on its
31 * suspend/resume latencies, so assume them to be negligible and
32 * take its current PM QoS constraint (that's the only thing
33 * known at this point anyway).
34 */
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020035 constraint_ns = dev_pm_qos_read_value(dev);
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010036 constraint_ns *= NSEC_PER_USEC;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020037 }
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010038
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010039 if (constraint_ns < *constraint_ns_p)
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020040 *constraint_ns_p = constraint_ns;
41
42 return 0;
43}
44
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010045/**
Ulf Hansson9df39212016-03-31 11:21:25 +020046 * default_suspend_ok - Default PM domain governor routine to suspend devices.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010047 * @dev: Device to check.
48 */
Ulf Hansson9df39212016-03-31 11:21:25 +020049static bool default_suspend_ok(struct device *dev)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010050{
51 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020052 unsigned long flags;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020053 s64 constraint_ns;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010054
55 dev_dbg(dev, "%s()\n", __func__);
56
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020057 spin_lock_irqsave(&dev->power.lock, flags);
58
59 if (!td->constraint_changed) {
Ulf Hansson9df39212016-03-31 11:21:25 +020060 bool ret = td->cached_suspend_ok;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020061
62 spin_unlock_irqrestore(&dev->power.lock, flags);
63 return ret;
64 }
65 td->constraint_changed = false;
Ulf Hansson9df39212016-03-31 11:21:25 +020066 td->cached_suspend_ok = false;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010067 td->effective_constraint_ns = 0;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020068 constraint_ns = __dev_pm_qos_read_value(dev);
69
70 spin_unlock_irqrestore(&dev->power.lock, flags);
71
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010072 if (constraint_ns == 0)
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020073 return false;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010074
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020075 constraint_ns *= NSEC_PER_USEC;
76 /*
77 * We can walk the children without any additional locking, because
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020078 * they all have been suspended at this point and their
79 * effective_constraint_ns fields won't be modified in parallel with us.
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020080 */
81 if (!dev->power.ignore_children)
82 device_for_each_child(dev, &constraint_ns,
83 dev_update_qos_constraint);
84
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010085 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010086 /* "No restriction", so the device is allowed to suspend. */
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010087 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010088 td->cached_suspend_ok = true;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010089 } else if (constraint_ns == 0) {
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010090 /*
91 * This triggers if one of the children that don't belong to a
Rafael J. Wysocki0759e802017-11-07 11:33:49 +010092 * domain has a zero PM QoS constraint and it's better not to
93 * suspend then. effective_constraint_ns is zero already and
94 * cached_suspend_ok is false, so bail out.
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +010095 */
96 return false;
97 } else {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020098 constraint_ns -= td->suspend_latency_ns +
99 td->resume_latency_ns;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +0100100 /*
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100101 * effective_constraint_ns is zero already and cached_suspend_ok
102 * is false, so if the computed value is not positive, return
103 * right away.
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +0100104 */
105 if (constraint_ns <= 0)
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200106 return false;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +0100107
108 td->effective_constraint_ns = constraint_ns;
109 td->cached_suspend_ok = true;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200110 }
Ulf Hanssona98f1b72015-10-13 16:10:28 +0200111
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200112 /*
113 * The children have been suspended already, so we don't need to take
Ulf Hansson9df39212016-03-31 11:21:25 +0200114 * their suspend latencies into account here.
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +0200115 */
Ulf Hansson9df39212016-03-31 11:21:25 +0200116 return td->cached_suspend_ok;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100117}
118
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100119static bool __default_power_down_ok(struct dev_pm_domain *pd,
120 unsigned int state)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100121{
122 struct generic_pm_domain *genpd = pd_to_genpd(pd);
123 struct gpd_link *link;
124 struct pm_domain_data *pdd;
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200125 s64 min_off_time_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100126 s64 off_on_time_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100127
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100128 off_on_time_ns = genpd->states[state].power_off_latency_ns +
129 genpd->states[state].power_on_latency_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200130
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100131
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200132 min_off_time_ns = -1;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100133 /*
134 * Check if subdomains can be off for enough time.
135 *
136 * All subdomains have been powered off already at this point.
137 */
138 list_for_each_entry(link, &genpd->master_links, master_node) {
139 struct generic_pm_domain *sd = link->slave;
140 s64 sd_max_off_ns = sd->max_off_time_ns;
141
142 if (sd_max_off_ns < 0)
143 continue;
144
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100145 /*
146 * Check if the subdomain is allowed to be off long enough for
147 * the current domain to turn off and on (that's how much time
148 * it will have to wait worst case).
149 */
150 if (sd_max_off_ns <= off_on_time_ns)
151 return false;
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200152
153 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
154 min_off_time_ns = sd_max_off_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100155 }
156
157 /*
158 * Check if the devices in the domain can be off enough time.
159 */
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100160 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
161 struct gpd_timing_data *td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200162 s64 constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100163
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200164 /*
165 * Check if the device is allowed to be off long enough for the
166 * domain to turn off and on (that's how much time it will
167 * have to wait worst case).
168 */
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100169 td = &to_gpd_data(pdd)->td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200170 constraint_ns = td->effective_constraint_ns;
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +0100171 /*
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100172 * Zero means "no suspend at all" and this runs only when all
173 * devices in the domain are suspended, so it must be positive.
Rafael J. Wysocki704d2ce2017-11-07 02:23:18 +0100174 */
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100175 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200176 continue;
177
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200178 if (constraint_ns <= off_on_time_ns)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100179 return false;
180
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200181 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
182 min_off_time_ns = constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100183 }
184
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200185 /*
186 * If the computed minimum device off time is negative, there are no
187 * latency constraints, so the domain can spend arbitrary time in the
188 * "off" state.
189 */
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200190 if (min_off_time_ns < 0)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100191 return true;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100192
193 /*
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200194 * The difference between the computed minimum subdomain or device off
195 * time and the time needed to turn the domain on is the maximum
196 * theoretical time this domain can spend in the "off" state.
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100197 */
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100198 genpd->max_off_time_ns = min_off_time_ns -
199 genpd->states[state].power_on_latency_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100200 return true;
201}
202
Krzysztof Kozlowski268cd2e2017-06-28 16:56:21 +0200203/**
204 * default_power_down_ok - Default generic PM domain power off governor routine.
205 * @pd: PM domain to check.
206 *
207 * This routine must be executed under the PM domain's lock.
208 */
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100209static bool default_power_down_ok(struct dev_pm_domain *pd)
210{
211 struct generic_pm_domain *genpd = pd_to_genpd(pd);
212 struct gpd_link *link;
213
214 if (!genpd->max_off_time_changed)
215 return genpd->cached_power_down_ok;
216
217 /*
218 * We have to invalidate the cached results for the masters, so
219 * use the observation that default_power_down_ok() is not
220 * going to be called for any master until this instance
221 * returns.
222 */
223 list_for_each_entry(link, &genpd->slave_links, slave_node)
224 link->master->max_off_time_changed = true;
225
226 genpd->max_off_time_ns = -1;
227 genpd->max_off_time_changed = false;
228 genpd->cached_power_down_ok = true;
229 genpd->state_idx = genpd->state_count - 1;
230
231 /* Find a state to power down to, starting from the deepest. */
232 while (!__default_power_down_ok(pd, genpd->state_idx)) {
233 if (genpd->state_idx == 0) {
234 genpd->cached_power_down_ok = false;
235 break;
236 }
237 genpd->state_idx--;
238 }
239
240 return genpd->cached_power_down_ok;
241}
242
Mark Brown925b44a2011-12-08 23:27:28 +0100243static bool always_on_power_down_ok(struct dev_pm_domain *domain)
244{
245 return false;
246}
247
Rafael J. Wysockie59a8db2012-01-14 00:39:36 +0100248struct dev_power_governor simple_qos_governor = {
Ulf Hansson9df39212016-03-31 11:21:25 +0200249 .suspend_ok = default_suspend_ok,
Rafael J. Wysockie59a8db2012-01-14 00:39:36 +0100250 .power_down_ok = default_power_down_ok,
251};
252
Mark Brown925b44a2011-12-08 23:27:28 +0100253/**
254 * pm_genpd_gov_always_on - A governor implementing an always-on policy
255 */
256struct dev_power_governor pm_domain_always_on_gov = {
257 .power_down_ok = always_on_power_down_ok,
Ulf Hansson9df39212016-03-31 11:21:25 +0200258 .suspend_ok = default_suspend_ok,
Mark Brown925b44a2011-12-08 23:27:28 +0100259};