blob: 2a4154a09e4dca0dc9af3aa09f9a395a313f60e8 [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;
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
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010039/**
40 * default_stop_ok - Default PM domain governor routine for stopping devices.
41 * @dev: Device to check.
42 */
Ulf Hansson0f574d42014-09-03 12:52:30 +020043static bool default_stop_ok(struct device *dev)
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010044{
45 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020046 unsigned long flags;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020047 s64 constraint_ns;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010048
49 dev_dbg(dev, "%s()\n", __func__);
50
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020051 spin_lock_irqsave(&dev->power.lock, flags);
52
53 if (!td->constraint_changed) {
54 bool ret = td->cached_stop_ok;
55
56 spin_unlock_irqrestore(&dev->power.lock, flags);
57 return ret;
58 }
59 td->constraint_changed = false;
60 td->cached_stop_ok = false;
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
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020066 if (constraint_ns < 0)
67 return false;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010068
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020069 constraint_ns *= NSEC_PER_USEC;
70 /*
71 * We can walk the children without any additional locking, because
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020072 * they all have been suspended at this point and their
73 * effective_constraint_ns fields won't be modified in parallel with us.
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020074 */
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) {
80 constraint_ns -= td->start_latency_ns;
81 if (constraint_ns == 0)
82 return false;
83 }
84 td->effective_constraint_ns = constraint_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020085 td->cached_stop_ok = constraint_ns > td->stop_latency_ns ||
86 constraint_ns == 0;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020087 /*
88 * The children have been suspended already, so we don't need to take
89 * their stop latencies into account here.
90 */
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020091 return td->cached_stop_ok;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010092}
93
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010094/**
95 * default_power_down_ok - Default generic PM domain power off governor routine.
96 * @pd: PM domain to check.
97 *
98 * This routine must be executed under the PM domain's lock.
99 */
100static bool default_power_down_ok(struct dev_pm_domain *pd)
101{
102 struct generic_pm_domain *genpd = pd_to_genpd(pd);
103 struct gpd_link *link;
104 struct pm_domain_data *pdd;
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200105 s64 min_off_time_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100106 s64 off_on_time_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100107
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200108 if (genpd->max_off_time_changed) {
109 struct gpd_link *link;
110
111 /*
112 * We have to invalidate the cached results for the masters, so
113 * use the observation that default_power_down_ok() is not
114 * going to be called for any master until this instance
115 * returns.
116 */
117 list_for_each_entry(link, &genpd->slave_links, slave_node)
118 link->master->max_off_time_changed = true;
119
120 genpd->max_off_time_changed = false;
121 genpd->cached_power_down_ok = false;
122 genpd->max_off_time_ns = -1;
123 } else {
124 return genpd->cached_power_down_ok;
125 }
126
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100127 off_on_time_ns = genpd->power_off_latency_ns +
128 genpd->power_on_latency_ns;
129 /*
130 * It doesn't make sense to remove power from the domain if saving
131 * the state of all devices in it and the power off/power on operations
132 * take too much time.
133 *
134 * All devices in this domain have been stopped already at this point.
135 */
136 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
137 if (pdd->dev->driver)
138 off_on_time_ns +=
139 to_gpd_data(pdd)->td.save_state_latency_ns;
140 }
141
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200142 min_off_time_ns = -1;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100143 /*
144 * Check if subdomains can be off for enough time.
145 *
146 * All subdomains have been powered off already at this point.
147 */
148 list_for_each_entry(link, &genpd->master_links, master_node) {
149 struct generic_pm_domain *sd = link->slave;
150 s64 sd_max_off_ns = sd->max_off_time_ns;
151
152 if (sd_max_off_ns < 0)
153 continue;
154
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100155 /*
156 * Check if the subdomain is allowed to be off long enough for
157 * the current domain to turn off and on (that's how much time
158 * it will have to wait worst case).
159 */
160 if (sd_max_off_ns <= off_on_time_ns)
161 return false;
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200162
163 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
164 min_off_time_ns = sd_max_off_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100165 }
166
167 /*
168 * Check if the devices in the domain can be off enough time.
169 */
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100170 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
171 struct gpd_timing_data *td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200172 s64 constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100173
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200174 if (!pdd->dev->driver)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100175 continue;
176
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200177 /*
178 * Check if the device is allowed to be off long enough for the
179 * domain to turn off and on (that's how much time it will
180 * have to wait worst case).
181 */
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100182 td = &to_gpd_data(pdd)->td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200183 constraint_ns = td->effective_constraint_ns;
184 /* default_stop_ok() need not be called before us. */
185 if (constraint_ns < 0) {
186 constraint_ns = dev_pm_qos_read_value(pdd->dev);
187 constraint_ns *= NSEC_PER_USEC;
188 }
189 if (constraint_ns == 0)
190 continue;
191
192 /*
193 * constraint_ns cannot be negative here, because the device has
194 * been suspended.
195 */
196 constraint_ns -= td->restore_state_latency_ns;
197 if (constraint_ns <= off_on_time_ns)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100198 return false;
199
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200200 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
201 min_off_time_ns = constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100202 }
203
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200204 genpd->cached_power_down_ok = true;
205
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200206 /*
207 * If the computed minimum device off time is negative, there are no
208 * latency constraints, so the domain can spend arbitrary time in the
209 * "off" state.
210 */
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200211 if (min_off_time_ns < 0)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100212 return true;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100213
214 /*
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200215 * The difference between the computed minimum subdomain or device off
216 * time and the time needed to turn the domain on is the maximum
217 * theoretical time this domain can spend in the "off" state.
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100218 */
Rafael J. Wysockib723b0e2012-05-07 22:00:59 +0200219 genpd->max_off_time_ns = min_off_time_ns - genpd->power_on_latency_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100220 return true;
221}
222
Mark Brown925b44a2011-12-08 23:27:28 +0100223static bool always_on_power_down_ok(struct dev_pm_domain *domain)
224{
225 return false;
226}
227
Rafael J. Wysockie59a8db2012-01-14 00:39:36 +0100228struct dev_power_governor simple_qos_governor = {
229 .stop_ok = default_stop_ok,
230 .power_down_ok = default_power_down_ok,
231};
232
Mark Brown925b44a2011-12-08 23:27:28 +0100233/**
234 * pm_genpd_gov_always_on - A governor implementing an always-on policy
235 */
236struct dev_power_governor pm_domain_always_on_gov = {
237 .power_down_ok = always_on_power_down_ok,
238 .stop_ok = default_stop_ok,
239};