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>
13 #include <linux/hrtimer.h>
14 
15 #ifdef CONFIG_PM_RUNTIME
16 
17 /**
18  * default_stop_ok - Default PM domain governor routine for stopping devices.
19  * @dev: Device to check.
20  */
21 bool default_stop_ok(struct device *dev)
22 {
23 	struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
24 
25 	dev_dbg(dev, "%s()\n", __func__);
26 
27 	if (dev->power.max_time_suspended_ns < 0 || td->break_even_ns == 0)
28 		return true;
29 
30 	return td->stop_latency_ns + td->start_latency_ns < td->break_even_ns
31 		&& td->break_even_ns < dev->power.max_time_suspended_ns;
32 }
33 
34 /**
35  * default_power_down_ok - Default generic PM domain power off governor routine.
36  * @pd: PM domain to check.
37  *
38  * This routine must be executed under the PM domain's lock.
39  */
40 static bool default_power_down_ok(struct dev_pm_domain *pd)
41 {
42 	struct generic_pm_domain *genpd = pd_to_genpd(pd);
43 	struct gpd_link *link;
44 	struct pm_domain_data *pdd;
45 	s64 min_dev_off_time_ns;
46 	s64 off_on_time_ns;
47 	ktime_t time_now = ktime_get();
48 
49 	off_on_time_ns = genpd->power_off_latency_ns +
50 				genpd->power_on_latency_ns;
51 	/*
52 	 * It doesn't make sense to remove power from the domain if saving
53 	 * the state of all devices in it and the power off/power on operations
54 	 * take too much time.
55 	 *
56 	 * All devices in this domain have been stopped already at this point.
57 	 */
58 	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
59 		if (pdd->dev->driver)
60 			off_on_time_ns +=
61 				to_gpd_data(pdd)->td.save_state_latency_ns;
62 	}
63 
64 	/*
65 	 * Check if subdomains can be off for enough time.
66 	 *
67 	 * All subdomains have been powered off already at this point.
68 	 */
69 	list_for_each_entry(link, &genpd->master_links, master_node) {
70 		struct generic_pm_domain *sd = link->slave;
71 		s64 sd_max_off_ns = sd->max_off_time_ns;
72 
73 		if (sd_max_off_ns < 0)
74 			continue;
75 
76 		sd_max_off_ns -= ktime_to_ns(ktime_sub(time_now,
77 						       sd->power_off_time));
78 		/*
79 		 * Check if the subdomain is allowed to be off long enough for
80 		 * the current domain to turn off and on (that's how much time
81 		 * it will have to wait worst case).
82 		 */
83 		if (sd_max_off_ns <= off_on_time_ns)
84 			return false;
85 	}
86 
87 	/*
88 	 * Check if the devices in the domain can be off enough time.
89 	 */
90 	min_dev_off_time_ns = -1;
91 	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
92 		struct gpd_timing_data *td;
93 		struct device *dev = pdd->dev;
94 		s64 dev_off_time_ns;
95 
96 		if (!dev->driver || dev->power.max_time_suspended_ns < 0)
97 			continue;
98 
99 		td = &to_gpd_data(pdd)->td;
100 		dev_off_time_ns = dev->power.max_time_suspended_ns -
101 			(td->start_latency_ns + td->restore_state_latency_ns +
102 				ktime_to_ns(ktime_sub(time_now,
103 						dev->power.suspend_time)));
104 		if (dev_off_time_ns <= off_on_time_ns)
105 			return false;
106 
107 		if (min_dev_off_time_ns > dev_off_time_ns
108 		    || min_dev_off_time_ns < 0)
109 			min_dev_off_time_ns = dev_off_time_ns;
110 	}
111 
112 	if (min_dev_off_time_ns < 0) {
113 		/*
114 		 * There are no latency constraints, so the domain can spend
115 		 * arbitrary time in the "off" state.
116 		 */
117 		genpd->max_off_time_ns = -1;
118 		return true;
119 	}
120 
121 	/*
122 	 * The difference between the computed minimum delta and the time needed
123 	 * to turn the domain on is the maximum theoretical time this domain can
124 	 * spend in the "off" state.
125 	 */
126 	min_dev_off_time_ns -= genpd->power_on_latency_ns;
127 
128 	/*
129 	 * If the difference between the computed minimum delta and the time
130 	 * needed to turn the domain off and back on on is smaller than the
131 	 * domain's power break even time, removing power from the domain is not
132 	 * worth it.
133 	 */
134 	if (genpd->break_even_ns >
135 	    min_dev_off_time_ns - genpd->power_off_latency_ns)
136 		return false;
137 
138 	genpd->max_off_time_ns = min_dev_off_time_ns;
139 	return true;
140 }
141 
142 static bool always_on_power_down_ok(struct dev_pm_domain *domain)
143 {
144 	return false;
145 }
146 
147 #else /* !CONFIG_PM_RUNTIME */
148 
149 bool default_stop_ok(struct device *dev)
150 {
151 	return false;
152 }
153 
154 #define default_power_down_ok	NULL
155 #define always_on_power_down_ok	NULL
156 
157 #endif /* !CONFIG_PM_RUNTIME */
158 
159 struct dev_power_governor simple_qos_governor = {
160 	.stop_ok = default_stop_ok,
161 	.power_down_ok = default_power_down_ok,
162 };
163 
164 /**
165  * pm_genpd_gov_always_on - A governor implementing an always-on policy
166  */
167 struct dev_power_governor pm_domain_always_on_gov = {
168 	.power_down_ok = always_on_power_down_ok,
169 	.stop_ok = default_stop_ok,
170 };
171