2 * drivers/base/power/domain_governor.c - Governors for device PM domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/kernel.h>
10 #include <linux/pm_domain.h>
11 #include <linux/pm_qos.h>
12 #include <linux/hrtimer.h>
14 static int dev_update_qos_constraint(struct device *dev, void *data)
16 s64 *constraint_ns_p = data;
17 s32 constraint_ns = -1;
19 if (dev->power.subsys_data && dev->power.subsys_data->domain_data)
20 constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;
22 if (constraint_ns < 0) {
23 constraint_ns = dev_pm_qos_read_value(dev);
24 constraint_ns *= NSEC_PER_USEC;
26 if (constraint_ns == 0)
30 * constraint_ns cannot be negative here, because the device has been
33 if (constraint_ns < *constraint_ns_p || *constraint_ns_p == 0)
34 *constraint_ns_p = constraint_ns;
40 * default_stop_ok - Default PM domain governor routine for stopping devices.
41 * @dev: Device to check.
43 static bool default_stop_ok(struct device *dev)
45 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
49 dev_dbg(dev, "%s()\n", __func__);
51 spin_lock_irqsave(&dev->power.lock, flags);
53 if (!td->constraint_changed) {
54 bool ret = td->cached_stop_ok;
56 spin_unlock_irqrestore(&dev->power.lock, flags);
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);
64 spin_unlock_irqrestore(&dev->power.lock, flags);
66 if (constraint_ns < 0)
69 constraint_ns *= NSEC_PER_USEC;
71 * We can walk the children without any additional locking, because
72 * they all have been suspended at this point and their
73 * effective_constraint_ns fields won't be modified in parallel with us.
75 if (!dev->power.ignore_children)
76 device_for_each_child(dev, &constraint_ns,
77 dev_update_qos_constraint);
79 if (constraint_ns > 0) {
80 constraint_ns -= td->save_state_latency_ns +
82 td->start_latency_ns +
83 td->restore_state_latency_ns;
84 if (constraint_ns == 0)
87 td->effective_constraint_ns = constraint_ns;
88 td->cached_stop_ok = constraint_ns >= 0;
91 * The children have been suspended already, so we don't need to take
92 * their stop latencies into account here.
94 return td->cached_stop_ok;
98 * default_power_down_ok - Default generic PM domain power off governor routine.
99 * @pd: PM domain to check.
101 * This routine must be executed under the PM domain's lock.
103 static bool default_power_down_ok(struct dev_pm_domain *pd)
105 struct generic_pm_domain *genpd = pd_to_genpd(pd);
106 struct gpd_link *link;
107 struct pm_domain_data *pdd;
111 if (genpd->max_off_time_changed) {
112 struct gpd_link *link;
115 * We have to invalidate the cached results for the masters, so
116 * use the observation that default_power_down_ok() is not
117 * going to be called for any master until this instance
120 list_for_each_entry(link, &genpd->slave_links, slave_node)
121 link->master->max_off_time_changed = true;
123 genpd->max_off_time_changed = false;
124 genpd->cached_power_down_ok = false;
125 genpd->max_off_time_ns = -1;
127 return genpd->cached_power_down_ok;
130 off_on_time_ns = genpd->power_off_latency_ns +
131 genpd->power_on_latency_ns;
133 min_off_time_ns = -1;
135 * Check if subdomains can be off for enough time.
137 * All subdomains have been powered off already at this point.
139 list_for_each_entry(link, &genpd->master_links, master_node) {
140 struct generic_pm_domain *sd = link->slave;
141 s64 sd_max_off_ns = sd->max_off_time_ns;
143 if (sd_max_off_ns < 0)
147 * Check if the subdomain is allowed to be off long enough for
148 * the current domain to turn off and on (that's how much time
149 * it will have to wait worst case).
151 if (sd_max_off_ns <= off_on_time_ns)
154 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
155 min_off_time_ns = sd_max_off_ns;
159 * Check if the devices in the domain can be off enough time.
161 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
162 struct gpd_timing_data *td;
165 if (!pdd->dev->driver)
169 * Check if the device is allowed to be off long enough for the
170 * domain to turn off and on (that's how much time it will
171 * have to wait worst case).
173 td = &to_gpd_data(pdd)->td;
174 constraint_ns = td->effective_constraint_ns;
175 /* default_stop_ok() need not be called before us. */
176 if (constraint_ns < 0) {
177 constraint_ns = dev_pm_qos_read_value(pdd->dev);
178 constraint_ns *= NSEC_PER_USEC;
180 if (constraint_ns == 0)
184 * constraint_ns cannot be negative here, because the device has
187 if (constraint_ns <= off_on_time_ns)
190 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
191 min_off_time_ns = constraint_ns;
194 genpd->cached_power_down_ok = true;
197 * If the computed minimum device off time is negative, there are no
198 * latency constraints, so the domain can spend arbitrary time in the
201 if (min_off_time_ns < 0)
205 * The difference between the computed minimum subdomain or device off
206 * time and the time needed to turn the domain on is the maximum
207 * theoretical time this domain can spend in the "off" state.
209 genpd->max_off_time_ns = min_off_time_ns - genpd->power_on_latency_ns;
213 static bool always_on_power_down_ok(struct dev_pm_domain *domain)
218 struct dev_power_governor simple_qos_governor = {
219 .stop_ok = default_stop_ok,
220 .power_down_ok = default_power_down_ok,
224 * pm_genpd_gov_always_on - A governor implementing an always-on policy
226 struct dev_power_governor pm_domain_always_on_gov = {
227 .power_down_ok = always_on_power_down_ok,
228 .stop_ok = default_stop_ok,