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