Commit | Line | Data |
---|---|---|
b02c999a RW |
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 | ||
b02c999a RW |
9 | #include <linux/kernel.h> |
10 | #include <linux/pm_domain.h> | |
11 | #include <linux/pm_qos.h> | |
221e9b58 | 12 | #include <linux/hrtimer.h> |
b02c999a | 13 | |
a5bef810 RW |
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 | ||
b02c999a | 39 | /** |
9df3921e | 40 | * default_suspend_ok - Default PM domain governor routine to suspend devices. |
b02c999a RW |
41 | * @dev: Device to check. |
42 | */ | |
9df3921e | 43 | static bool default_suspend_ok(struct device *dev) |
b02c999a RW |
44 | { |
45 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; | |
6ff7bb0d | 46 | unsigned long flags; |
a5bef810 | 47 | s64 constraint_ns; |
b02c999a RW |
48 | |
49 | dev_dbg(dev, "%s()\n", __func__); | |
50 | ||
6ff7bb0d RW |
51 | spin_lock_irqsave(&dev->power.lock, flags); |
52 | ||
53 | if (!td->constraint_changed) { | |
9df3921e | 54 | bool ret = td->cached_suspend_ok; |
6ff7bb0d RW |
55 | |
56 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
57 | return ret; | |
58 | } | |
59 | td->constraint_changed = false; | |
9df3921e | 60 | td->cached_suspend_ok = false; |
6ff7bb0d RW |
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 | ||
a5bef810 RW |
66 | if (constraint_ns < 0) |
67 | return false; | |
b02c999a | 68 | |
a5bef810 RW |
69 | constraint_ns *= NSEC_PER_USEC; |
70 | /* | |
71 | * We can walk the children without any additional locking, because | |
6ff7bb0d RW |
72 | * they all have been suspended at this point and their |
73 | * effective_constraint_ns fields won't be modified in parallel with us. | |
a5bef810 RW |
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) { | |
2b1d88cd UH |
80 | constraint_ns -= td->suspend_latency_ns + |
81 | td->resume_latency_ns; | |
a5bef810 RW |
82 | if (constraint_ns == 0) |
83 | return false; | |
84 | } | |
85 | td->effective_constraint_ns = constraint_ns; | |
9df3921e | 86 | td->cached_suspend_ok = constraint_ns >= 0; |
a98f1b78 | 87 | |
a5bef810 RW |
88 | /* |
89 | * The children have been suspended already, so we don't need to take | |
9df3921e | 90 | * their suspend latencies into account here. |
a5bef810 | 91 | */ |
9df3921e | 92 | return td->cached_suspend_ok; |
b02c999a RW |
93 | } |
94 | ||
fc5cbf0c AH |
95 | static bool __default_power_down_ok(struct dev_pm_domain *pd, |
96 | unsigned int state) | |
221e9b58 RW |
97 | { |
98 | struct generic_pm_domain *genpd = pd_to_genpd(pd); | |
99 | struct gpd_link *link; | |
100 | struct pm_domain_data *pdd; | |
b723b0eb | 101 | s64 min_off_time_ns; |
221e9b58 | 102 | s64 off_on_time_ns; |
221e9b58 | 103 | |
fc5cbf0c AH |
104 | off_on_time_ns = genpd->states[state].power_off_latency_ns + |
105 | genpd->states[state].power_on_latency_ns; | |
6ff7bb0d | 106 | |
221e9b58 | 107 | |
b723b0eb | 108 | min_off_time_ns = -1; |
221e9b58 RW |
109 | /* |
110 | * Check if subdomains can be off for enough time. | |
111 | * | |
112 | * All subdomains have been powered off already at this point. | |
113 | */ | |
114 | list_for_each_entry(link, &genpd->master_links, master_node) { | |
115 | struct generic_pm_domain *sd = link->slave; | |
116 | s64 sd_max_off_ns = sd->max_off_time_ns; | |
117 | ||
118 | if (sd_max_off_ns < 0) | |
119 | continue; | |
120 | ||
221e9b58 RW |
121 | /* |
122 | * Check if the subdomain is allowed to be off long enough for | |
123 | * the current domain to turn off and on (that's how much time | |
124 | * it will have to wait worst case). | |
125 | */ | |
126 | if (sd_max_off_ns <= off_on_time_ns) | |
127 | return false; | |
b723b0eb RW |
128 | |
129 | if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0) | |
130 | min_off_time_ns = sd_max_off_ns; | |
221e9b58 RW |
131 | } |
132 | ||
133 | /* | |
134 | * Check if the devices in the domain can be off enough time. | |
135 | */ | |
221e9b58 RW |
136 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
137 | struct gpd_timing_data *td; | |
dd8683e9 | 138 | s64 constraint_ns; |
221e9b58 | 139 | |
dd8683e9 RW |
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 | */ | |
221e9b58 | 145 | td = &to_gpd_data(pdd)->td; |
dd8683e9 | 146 | constraint_ns = td->effective_constraint_ns; |
9df3921e | 147 | /* default_suspend_ok() need not be called before us. */ |
dd8683e9 RW |
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; | |
221e9b58 | 154 | |
221e9b58 | 155 | /* |
dd8683e9 RW |
156 | * constraint_ns cannot be negative here, because the device has |
157 | * been suspended. | |
221e9b58 | 158 | */ |
dd8683e9 RW |
159 | if (constraint_ns <= off_on_time_ns) |
160 | return false; | |
161 | ||
b723b0eb RW |
162 | if (min_off_time_ns > constraint_ns || min_off_time_ns < 0) |
163 | min_off_time_ns = constraint_ns; | |
221e9b58 RW |
164 | } |
165 | ||
166 | /* | |
dd8683e9 RW |
167 | * If the computed minimum device off time is negative, there are no |
168 | * latency constraints, so the domain can spend arbitrary time in the | |
169 | * "off" state. | |
221e9b58 | 170 | */ |
b723b0eb | 171 | if (min_off_time_ns < 0) |
dd8683e9 | 172 | return true; |
221e9b58 RW |
173 | |
174 | /* | |
b723b0eb RW |
175 | * The difference between the computed minimum subdomain or device off |
176 | * time and the time needed to turn the domain on is the maximum | |
177 | * theoretical time this domain can spend in the "off" state. | |
221e9b58 | 178 | */ |
fc5cbf0c AH |
179 | genpd->max_off_time_ns = min_off_time_ns - |
180 | genpd->states[state].power_on_latency_ns; | |
221e9b58 RW |
181 | return true; |
182 | } | |
183 | ||
268cd2ed KK |
184 | /** |
185 | * default_power_down_ok - Default generic PM domain power off governor routine. | |
186 | * @pd: PM domain to check. | |
187 | * | |
188 | * This routine must be executed under the PM domain's lock. | |
189 | */ | |
fc5cbf0c AH |
190 | static bool default_power_down_ok(struct dev_pm_domain *pd) |
191 | { | |
192 | struct generic_pm_domain *genpd = pd_to_genpd(pd); | |
193 | struct gpd_link *link; | |
194 | ||
195 | if (!genpd->max_off_time_changed) | |
196 | return genpd->cached_power_down_ok; | |
197 | ||
198 | /* | |
199 | * We have to invalidate the cached results for the masters, so | |
200 | * use the observation that default_power_down_ok() is not | |
201 | * going to be called for any master until this instance | |
202 | * returns. | |
203 | */ | |
204 | list_for_each_entry(link, &genpd->slave_links, slave_node) | |
205 | link->master->max_off_time_changed = true; | |
206 | ||
207 | genpd->max_off_time_ns = -1; | |
208 | genpd->max_off_time_changed = false; | |
209 | genpd->cached_power_down_ok = true; | |
210 | genpd->state_idx = genpd->state_count - 1; | |
211 | ||
212 | /* Find a state to power down to, starting from the deepest. */ | |
213 | while (!__default_power_down_ok(pd, genpd->state_idx)) { | |
214 | if (genpd->state_idx == 0) { | |
215 | genpd->cached_power_down_ok = false; | |
216 | break; | |
217 | } | |
218 | genpd->state_idx--; | |
219 | } | |
220 | ||
221 | return genpd->cached_power_down_ok; | |
222 | } | |
223 | ||
925b44a2 MB |
224 | static bool always_on_power_down_ok(struct dev_pm_domain *domain) |
225 | { | |
226 | return false; | |
227 | } | |
228 | ||
e59a8db8 | 229 | struct dev_power_governor simple_qos_governor = { |
9df3921e | 230 | .suspend_ok = default_suspend_ok, |
e59a8db8 RW |
231 | .power_down_ok = default_power_down_ok, |
232 | }; | |
233 | ||
925b44a2 MB |
234 | /** |
235 | * pm_genpd_gov_always_on - A governor implementing an always-on policy | |
236 | */ | |
237 | struct dev_power_governor pm_domain_always_on_gov = { | |
238 | .power_down_ok = always_on_power_down_ok, | |
9df3921e | 239 | .suspend_ok = default_suspend_ok, |
925b44a2 | 240 | }; |