blob: 2aae623fd84073b38bfb530c174b1b9d23122dd8 [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
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/pm_domain.h>
12#include <linux/pm_qos.h>
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010013#include <linux/hrtimer.h>
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010014
Rafael J. Wysockie59a8db2012-01-14 00:39:36 +010015#ifdef CONFIG_PM_RUNTIME
16
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020017static int dev_update_qos_constraint(struct device *dev, void *data)
18{
19 s64 *constraint_ns_p = data;
20 s32 constraint_ns = -1;
21
22 if (dev->power.subsys_data && dev->power.subsys_data->domain_data)
23 constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;
24
25 if (constraint_ns < 0) {
26 constraint_ns = dev_pm_qos_read_value(dev);
27 constraint_ns *= NSEC_PER_USEC;
28 }
29 if (constraint_ns == 0)
30 return 0;
31
32 /*
33 * constraint_ns cannot be negative here, because the device has been
34 * suspended.
35 */
36 if (constraint_ns < *constraint_ns_p || *constraint_ns_p == 0)
37 *constraint_ns_p = constraint_ns;
38
39 return 0;
40}
41
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010042/**
43 * default_stop_ok - Default PM domain governor routine for stopping devices.
44 * @dev: Device to check.
45 */
46bool default_stop_ok(struct device *dev)
47{
48 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020049 s64 constraint_ns;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010050
51 dev_dbg(dev, "%s()\n", __func__);
52
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020053 constraint_ns = dev_pm_qos_read_value(dev);
54 if (constraint_ns < 0)
55 return false;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010056
Rafael J. Wysockia5bef8102012-04-29 22:54:17 +020057 constraint_ns *= NSEC_PER_USEC;
58 /*
59 * We can walk the children without any additional locking, because
60 * they all have been suspended at this point.
61 */
62 if (!dev->power.ignore_children)
63 device_for_each_child(dev, &constraint_ns,
64 dev_update_qos_constraint);
65
66 if (constraint_ns > 0) {
67 constraint_ns -= td->start_latency_ns;
68 if (constraint_ns == 0)
69 return false;
70 }
71 td->effective_constraint_ns = constraint_ns;
72 /*
73 * The children have been suspended already, so we don't need to take
74 * their stop latencies into account here.
75 */
76 return constraint_ns > td->stop_latency_ns || constraint_ns == 0;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010077}
78
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010079/**
80 * default_power_down_ok - Default generic PM domain power off governor routine.
81 * @pd: PM domain to check.
82 *
83 * This routine must be executed under the PM domain's lock.
84 */
85static bool default_power_down_ok(struct dev_pm_domain *pd)
86{
87 struct generic_pm_domain *genpd = pd_to_genpd(pd);
88 struct gpd_link *link;
89 struct pm_domain_data *pdd;
90 s64 min_dev_off_time_ns;
91 s64 off_on_time_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +010092
93 off_on_time_ns = genpd->power_off_latency_ns +
94 genpd->power_on_latency_ns;
95 /*
96 * It doesn't make sense to remove power from the domain if saving
97 * the state of all devices in it and the power off/power on operations
98 * take too much time.
99 *
100 * All devices in this domain have been stopped already at this point.
101 */
102 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
103 if (pdd->dev->driver)
104 off_on_time_ns +=
105 to_gpd_data(pdd)->td.save_state_latency_ns;
106 }
107
108 /*
109 * Check if subdomains can be off for enough time.
110 *
111 * All subdomains have been powered off already at this point.
112 */
113 list_for_each_entry(link, &genpd->master_links, master_node) {
114 struct generic_pm_domain *sd = link->slave;
115 s64 sd_max_off_ns = sd->max_off_time_ns;
116
117 if (sd_max_off_ns < 0)
118 continue;
119
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100120 /*
121 * Check if the subdomain is allowed to be off long enough for
122 * the current domain to turn off and on (that's how much time
123 * it will have to wait worst case).
124 */
125 if (sd_max_off_ns <= off_on_time_ns)
126 return false;
127 }
128
129 /*
130 * Check if the devices in the domain can be off enough time.
131 */
132 min_dev_off_time_ns = -1;
133 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
134 struct gpd_timing_data *td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200135 s64 constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100136
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200137 if (!pdd->dev->driver)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100138 continue;
139
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200140 /*
141 * Check if the device is allowed to be off long enough for the
142 * domain to turn off and on (that's how much time it will
143 * have to wait worst case).
144 */
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100145 td = &to_gpd_data(pdd)->td;
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200146 constraint_ns = td->effective_constraint_ns;
147 /* default_stop_ok() need not be called before us. */
148 if (constraint_ns < 0) {
149 constraint_ns = dev_pm_qos_read_value(pdd->dev);
150 constraint_ns *= NSEC_PER_USEC;
151 }
152 if (constraint_ns == 0)
153 continue;
154
155 /*
156 * constraint_ns cannot be negative here, because the device has
157 * been suspended.
158 */
159 constraint_ns -= td->restore_state_latency_ns;
160 if (constraint_ns <= off_on_time_ns)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100161 return false;
162
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200163 if (min_dev_off_time_ns > constraint_ns
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100164 || min_dev_off_time_ns < 0)
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200165 min_dev_off_time_ns = constraint_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100166 }
167
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200168 /*
169 * If the computed minimum device off time is negative, there are no
170 * latency constraints, so the domain can spend arbitrary time in the
171 * "off" state.
172 */
173 if (min_dev_off_time_ns < 0)
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100174 return true;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100175
176 /*
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200177 * The difference between the computed minimum device off time and the
178 * time needed to turn the domain on is the maximum theoretical time
179 * this domain can spend in the "off" state.
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100180 */
Rafael J. Wysockidd8683e2012-04-29 22:54:30 +0200181 genpd->max_off_time_ns = min_dev_off_time_ns -
182 genpd->power_on_latency_ns;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100183 return true;
184}
185
Mark Brown925b44a2011-12-08 23:27:28 +0100186static bool always_on_power_down_ok(struct dev_pm_domain *domain)
187{
188 return false;
189}
190
Rafael J. Wysockie59a8db2012-01-14 00:39:36 +0100191#else /* !CONFIG_PM_RUNTIME */
192
193bool default_stop_ok(struct device *dev)
194{
195 return false;
196}
197
198#define default_power_down_ok NULL
199#define always_on_power_down_ok NULL
200
201#endif /* !CONFIG_PM_RUNTIME */
202
203struct dev_power_governor simple_qos_governor = {
204 .stop_ok = default_stop_ok,
205 .power_down_ok = default_power_down_ok,
206};
207
Mark Brown925b44a2011-12-08 23:27:28 +0100208/**
209 * pm_genpd_gov_always_on - A governor implementing an always-on policy
210 */
211struct dev_power_governor pm_domain_always_on_gov = {
212 .power_down_ok = always_on_power_down_ok,
213 .stop_ok = default_stop_ok,
214};