1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/base/power/domain_governor.c - Governors for device PM domains.
4 *
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 */
7 #include <linux/kernel.h>
8 #include <linux/pm_domain.h>
9 #include <linux/pm_qos.h>
10 #include <linux/hrtimer.h>
11 #include <linux/cpu.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpumask.h>
14 #include <linux/ktime.h>
15
dev_update_qos_constraint(struct device * dev,void * data)16 static int dev_update_qos_constraint(struct device *dev, void *data)
17 {
18 s64 *constraint_ns_p = data;
19 s64 constraint_ns;
20
21 if (dev->power.subsys_data && dev->power.subsys_data->domain_data) {
22 struct gpd_timing_data *td = dev_gpd_data(dev)->td;
23
24 /*
25 * Only take suspend-time QoS constraints of devices into
26 * account, because constraints updated after the device has
27 * been suspended are not guaranteed to be taken into account
28 * anyway. In order for them to take effect, the device has to
29 * be resumed and suspended again.
30 */
31 constraint_ns = td ? td->effective_constraint_ns :
32 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
33 } else {
34 /*
35 * The child is not in a domain and there's no info on its
36 * suspend/resume latencies, so assume them to be negligible and
37 * take its current PM QoS constraint (that's the only thing
38 * known at this point anyway).
39 */
40 constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY);
41 constraint_ns *= NSEC_PER_USEC;
42 }
43
44 if (constraint_ns < *constraint_ns_p)
45 *constraint_ns_p = constraint_ns;
46
47 return 0;
48 }
49
50 /**
51 * default_suspend_ok - Default PM domain governor routine to suspend devices.
52 * @dev: Device to check.
53 */
default_suspend_ok(struct device * dev)54 static bool default_suspend_ok(struct device *dev)
55 {
56 struct gpd_timing_data *td = dev_gpd_data(dev)->td;
57 unsigned long flags;
58 s64 constraint_ns;
59
60 dev_dbg(dev, "%s()\n", __func__);
61
62 spin_lock_irqsave(&dev->power.lock, flags);
63
64 if (!td->constraint_changed) {
65 bool ret = td->cached_suspend_ok;
66
67 spin_unlock_irqrestore(&dev->power.lock, flags);
68 return ret;
69 }
70 td->constraint_changed = false;
71 td->cached_suspend_ok = false;
72 td->effective_constraint_ns = 0;
73 constraint_ns = __dev_pm_qos_resume_latency(dev);
74
75 spin_unlock_irqrestore(&dev->power.lock, flags);
76
77 if (constraint_ns == 0)
78 return false;
79
80 constraint_ns *= NSEC_PER_USEC;
81 /*
82 * We can walk the children without any additional locking, because
83 * they all have been suspended at this point and their
84 * effective_constraint_ns fields won't be modified in parallel with us.
85 */
86 if (!dev->power.ignore_children)
87 device_for_each_child(dev, &constraint_ns,
88 dev_update_qos_constraint);
89
90 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
91 /* "No restriction", so the device is allowed to suspend. */
92 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
93 td->cached_suspend_ok = true;
94 } else if (constraint_ns == 0) {
95 /*
96 * This triggers if one of the children that don't belong to a
97 * domain has a zero PM QoS constraint and it's better not to
98 * suspend then. effective_constraint_ns is zero already and
99 * cached_suspend_ok is false, so bail out.
100 */
101 return false;
102 } else {
103 constraint_ns -= td->suspend_latency_ns +
104 td->resume_latency_ns;
105 /*
106 * effective_constraint_ns is zero already and cached_suspend_ok
107 * is false, so if the computed value is not positive, return
108 * right away.
109 */
110 if (constraint_ns <= 0)
111 return false;
112
113 td->effective_constraint_ns = constraint_ns;
114 td->cached_suspend_ok = true;
115 }
116
117 /*
118 * The children have been suspended already, so we don't need to take
119 * their suspend latencies into account here.
120 */
121 return td->cached_suspend_ok;
122 }
123
update_domain_next_wakeup(struct generic_pm_domain * genpd,ktime_t now)124 static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now)
125 {
126 ktime_t domain_wakeup = KTIME_MAX;
127 ktime_t next_wakeup;
128 struct pm_domain_data *pdd;
129 struct gpd_link *link;
130
131 if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY))
132 return;
133
134 /*
135 * Devices that have a predictable wakeup pattern, may specify
136 * their next wakeup. Let's find the next wakeup from all the
137 * devices attached to this domain and from all the sub-domains.
138 * It is possible that component's a next wakeup may have become
139 * stale when we read that here. We will ignore to ensure the domain
140 * is able to enter its optimal idle state.
141 */
142 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
143 next_wakeup = to_gpd_data(pdd)->td->next_wakeup;
144 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
145 if (ktime_before(next_wakeup, domain_wakeup))
146 domain_wakeup = next_wakeup;
147 }
148
149 list_for_each_entry(link, &genpd->parent_links, parent_node) {
150 struct genpd_governor_data *cgd = link->child->gd;
151
152 next_wakeup = cgd ? cgd->next_wakeup : KTIME_MAX;
153 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
154 if (ktime_before(next_wakeup, domain_wakeup))
155 domain_wakeup = next_wakeup;
156 }
157
158 genpd->gd->next_wakeup = domain_wakeup;
159 }
160
next_wakeup_allows_state(struct generic_pm_domain * genpd,unsigned int state,ktime_t now)161 static bool next_wakeup_allows_state(struct generic_pm_domain *genpd,
162 unsigned int state, ktime_t now)
163 {
164 ktime_t domain_wakeup = genpd->gd->next_wakeup;
165 s64 idle_time_ns, min_sleep_ns;
166
167 min_sleep_ns = genpd->states[state].power_off_latency_ns +
168 genpd->states[state].residency_ns;
169
170 idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
171
172 return idle_time_ns >= min_sleep_ns;
173 }
174
__default_power_down_ok(struct dev_pm_domain * pd,unsigned int state)175 static bool __default_power_down_ok(struct dev_pm_domain *pd,
176 unsigned int state)
177 {
178 struct generic_pm_domain *genpd = pd_to_genpd(pd);
179 struct gpd_link *link;
180 struct pm_domain_data *pdd;
181 s64 min_off_time_ns;
182 s64 off_on_time_ns;
183
184 off_on_time_ns = genpd->states[state].power_off_latency_ns +
185 genpd->states[state].power_on_latency_ns;
186
187 min_off_time_ns = -1;
188 /*
189 * Check if subdomains can be off for enough time.
190 *
191 * All subdomains have been powered off already at this point.
192 */
193 list_for_each_entry(link, &genpd->parent_links, parent_node) {
194 struct genpd_governor_data *cgd = link->child->gd;
195
196 s64 sd_max_off_ns = cgd ? cgd->max_off_time_ns : -1;
197
198 if (sd_max_off_ns < 0)
199 continue;
200
201 /*
202 * Check if the subdomain is allowed to be off long enough for
203 * the current domain to turn off and on (that's how much time
204 * it will have to wait worst case).
205 */
206 if (sd_max_off_ns <= off_on_time_ns)
207 return false;
208
209 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
210 min_off_time_ns = sd_max_off_ns;
211 }
212
213 /*
214 * Check if the devices in the domain can be off enough time.
215 */
216 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
217 struct gpd_timing_data *td;
218 s64 constraint_ns;
219
220 /*
221 * Check if the device is allowed to be off long enough for the
222 * domain to turn off and on (that's how much time it will
223 * have to wait worst case).
224 */
225 td = to_gpd_data(pdd)->td;
226 constraint_ns = td->effective_constraint_ns;
227 /*
228 * Zero means "no suspend at all" and this runs only when all
229 * devices in the domain are suspended, so it must be positive.
230 */
231 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
232 continue;
233
234 if (constraint_ns <= off_on_time_ns)
235 return false;
236
237 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
238 min_off_time_ns = constraint_ns;
239 }
240
241 /*
242 * If the computed minimum device off time is negative, there are no
243 * latency constraints, so the domain can spend arbitrary time in the
244 * "off" state.
245 */
246 if (min_off_time_ns < 0)
247 return true;
248
249 /*
250 * The difference between the computed minimum subdomain or device off
251 * time and the time needed to turn the domain on is the maximum
252 * theoretical time this domain can spend in the "off" state.
253 */
254 genpd->gd->max_off_time_ns = min_off_time_ns -
255 genpd->states[state].power_on_latency_ns;
256 return true;
257 }
258
259 /**
260 * _default_power_down_ok - Default generic PM domain power off governor routine.
261 * @pd: PM domain to check.
262 * @now: current ktime.
263 *
264 * This routine must be executed under the PM domain's lock.
265 */
_default_power_down_ok(struct dev_pm_domain * pd,ktime_t now)266 static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now)
267 {
268 struct generic_pm_domain *genpd = pd_to_genpd(pd);
269 struct genpd_governor_data *gd = genpd->gd;
270 int state_idx = genpd->state_count - 1;
271 struct gpd_link *link;
272
273 /*
274 * Find the next wakeup from devices that can determine their own wakeup
275 * to find when the domain would wakeup and do it for every device down
276 * the hierarchy. It is not worth while to sleep if the state's residency
277 * cannot be met.
278 */
279 update_domain_next_wakeup(genpd, now);
280 if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (gd->next_wakeup != KTIME_MAX)) {
281 /* Let's find out the deepest domain idle state, the devices prefer */
282 while (state_idx >= 0) {
283 if (next_wakeup_allows_state(genpd, state_idx, now)) {
284 gd->max_off_time_changed = true;
285 break;
286 }
287 state_idx--;
288 }
289
290 if (state_idx < 0) {
291 state_idx = 0;
292 gd->cached_power_down_ok = false;
293 goto done;
294 }
295 }
296
297 if (!gd->max_off_time_changed) {
298 genpd->state_idx = gd->cached_power_down_state_idx;
299 return gd->cached_power_down_ok;
300 }
301
302 /*
303 * We have to invalidate the cached results for the parents, so
304 * use the observation that default_power_down_ok() is not
305 * going to be called for any parent until this instance
306 * returns.
307 */
308 list_for_each_entry(link, &genpd->child_links, child_node) {
309 struct genpd_governor_data *pgd = link->parent->gd;
310
311 if (pgd)
312 pgd->max_off_time_changed = true;
313 }
314
315 gd->max_off_time_ns = -1;
316 gd->max_off_time_changed = false;
317 gd->cached_power_down_ok = true;
318
319 /*
320 * Find a state to power down to, starting from the state
321 * determined by the next wakeup.
322 */
323 while (!__default_power_down_ok(pd, state_idx)) {
324 if (state_idx == 0) {
325 gd->cached_power_down_ok = false;
326 break;
327 }
328 state_idx--;
329 }
330
331 done:
332 genpd->state_idx = state_idx;
333 gd->cached_power_down_state_idx = genpd->state_idx;
334 return gd->cached_power_down_ok;
335 }
336
default_power_down_ok(struct dev_pm_domain * pd)337 static bool default_power_down_ok(struct dev_pm_domain *pd)
338 {
339 return _default_power_down_ok(pd, ktime_get());
340 }
341
342 #ifdef CONFIG_CPU_IDLE
cpu_power_down_ok(struct dev_pm_domain * pd)343 static bool cpu_power_down_ok(struct dev_pm_domain *pd)
344 {
345 struct generic_pm_domain *genpd = pd_to_genpd(pd);
346 struct cpuidle_device *dev;
347 ktime_t domain_wakeup, next_hrtimer;
348 ktime_t now = ktime_get();
349 struct device *cpu_dev;
350 s64 cpu_constraint, global_constraint;
351 s64 idle_duration_ns;
352 int cpu, i;
353
354 /* Validate dev PM QoS constraints. */
355 if (!_default_power_down_ok(pd, now))
356 return false;
357
358 if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN))
359 return true;
360
361 global_constraint = cpu_latency_qos_limit();
362 /*
363 * Find the next wakeup for any of the online CPUs within the PM domain
364 * and its subdomains. Note, we only need the genpd->cpus, as it already
365 * contains a mask of all CPUs from subdomains.
366 */
367 domain_wakeup = ktime_set(KTIME_SEC_MAX, 0);
368 for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) {
369 dev = per_cpu(cpuidle_devices, cpu);
370 if (dev) {
371 next_hrtimer = READ_ONCE(dev->next_hrtimer);
372 if (ktime_before(next_hrtimer, domain_wakeup))
373 domain_wakeup = next_hrtimer;
374 }
375
376 cpu_dev = get_cpu_device(cpu);
377 if (cpu_dev) {
378 cpu_constraint = dev_pm_qos_raw_resume_latency(cpu_dev);
379 if (cpu_constraint < global_constraint)
380 global_constraint = cpu_constraint;
381 }
382 }
383
384 global_constraint *= NSEC_PER_USEC;
385 /* The minimum idle duration is from now - until the next wakeup. */
386 idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
387 if (idle_duration_ns <= 0)
388 return false;
389
390 /* Store the next domain_wakeup to allow consumers to use it. */
391 genpd->gd->next_hrtimer = domain_wakeup;
392
393 /*
394 * Find the deepest idle state that has its residency value satisfied
395 * and by also taking into account the power off latency for the state.
396 * Start at the state picked by the dev PM QoS constraint validation.
397 */
398 i = genpd->state_idx;
399 do {
400 if ((idle_duration_ns >= (genpd->states[i].residency_ns +
401 genpd->states[i].power_off_latency_ns)) &&
402 (global_constraint >= (genpd->states[i].power_on_latency_ns +
403 genpd->states[i].power_off_latency_ns))) {
404 genpd->state_idx = i;
405 return true;
406 }
407 } while (--i >= 0);
408
409 return false;
410 }
411
412 struct dev_power_governor pm_domain_cpu_gov = {
413 .suspend_ok = default_suspend_ok,
414 .power_down_ok = cpu_power_down_ok,
415 };
416 #endif
417
418 struct dev_power_governor simple_qos_governor = {
419 .suspend_ok = default_suspend_ok,
420 .power_down_ok = default_power_down_ok,
421 };
422
423 /**
424 * pm_genpd_gov_always_on - A governor implementing an always-on policy
425 */
426 struct dev_power_governor pm_domain_always_on_gov = {
427 .suspend_ok = default_suspend_ok,
428 };
429