aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
authorRafael J. Wysocki <rjw@sisk.pl>2012-05-01 15:34:07 -0400
committerRafael J. Wysocki <rjw@sisk.pl>2012-05-05 15:51:58 -0400
commit6ff7bb0d02f82968be13937c03e93b6c090229df (patch)
treeac2605f2f2b1602fddb88c4ba33c647608b594e6 /drivers/base
parentefa6902501ffc87d69bfb10b8a09b7d6ee222d77 (diff)
PM / Domains: Cache device stop and domain power off governor results, v3
The results of the default device stop and domain power off governor functions for generic PM domains, default_stop_ok() and default_power_down_ok(), depend only on the timing data of devices, which are static, and on their PM QoS constraints. Thus, in theory, these functions only need to carry out their computations, which may be time consuming in general, when it is known that the PM QoS constraint of at least one of the devices in question has changed. Use the PM QoS notifiers of devices to implement that. First, introduce new fields, constraint_changed and max_off_time_changed, into struct gpd_timing_data and struct generic_pm_domain, respectively, and register a PM QoS notifier function when adding a device into a domain that will set those fields to 'true' whenever the device's PM QoS constraint is modified. Second, make default_stop_ok() and default_power_down_ok() use those fields to decide whether or not to carry out their computations from scratch. The device and PM domain hierarchies are taken into account in that and the expense is that the changes of PM QoS constraints of suspended devices will not be taken into account immediately, which isn't guaranteed anyway in general. Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/power/domain.c120
-rw-r--r--drivers/base/power/domain_governor.c45
2 files changed, 146 insertions, 19 deletions
diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 6ae5672c35ab..cde5983de6c2 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -11,6 +11,7 @@
11#include <linux/io.h> 11#include <linux/io.h>
12#include <linux/pm_runtime.h> 12#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h> 13#include <linux/pm_domain.h>
14#include <linux/pm_qos.h>
14#include <linux/slab.h> 15#include <linux/slab.h>
15#include <linux/err.h> 16#include <linux/err.h>
16#include <linux/sched.h> 17#include <linux/sched.h>
@@ -38,11 +39,13 @@
38 ktime_t __start = ktime_get(); \ 39 ktime_t __start = ktime_get(); \
39 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \ 40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
40 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \ 41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
41 struct generic_pm_domain_data *__gpd_data = dev_gpd_data(dev); \ 42 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
42 if (__elapsed > __gpd_data->td.field) { \ 43 if (!__retval && __elapsed > __td->field) { \
43 __gpd_data->td.field = __elapsed; \ 44 __td->field = __elapsed; \
44 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \ 45 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
45 __elapsed); \ 46 __elapsed); \
47 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
46 } \ 49 } \
47 __retval; \ 50 __retval; \
48}) 51})
@@ -211,6 +214,7 @@ int __pm_genpd_poweron(struct generic_pm_domain *genpd)
211 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); 214 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
212 if (elapsed_ns > genpd->power_on_latency_ns) { 215 if (elapsed_ns > genpd->power_on_latency_ns) {
213 genpd->power_on_latency_ns = elapsed_ns; 216 genpd->power_on_latency_ns = elapsed_ns;
217 genpd->max_off_time_changed = true;
214 if (genpd->name) 218 if (genpd->name)
215 pr_warning("%s: Power-on latency exceeded, " 219 pr_warning("%s: Power-on latency exceeded, "
216 "new value %lld ns\n", genpd->name, 220 "new value %lld ns\n", genpd->name,
@@ -247,6 +251,53 @@ int pm_genpd_poweron(struct generic_pm_domain *genpd)
247 251
248#ifdef CONFIG_PM_RUNTIME 252#ifdef CONFIG_PM_RUNTIME
249 253
254static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
255 unsigned long val, void *ptr)
256{
257 struct generic_pm_domain_data *gpd_data;
258 struct device *dev;
259
260 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
261
262 mutex_lock(&gpd_data->lock);
263 dev = gpd_data->base.dev;
264 if (!dev) {
265 mutex_unlock(&gpd_data->lock);
266 return NOTIFY_DONE;
267 }
268 mutex_unlock(&gpd_data->lock);
269
270 for (;;) {
271 struct generic_pm_domain *genpd;
272 struct pm_domain_data *pdd;
273
274 spin_lock_irq(&dev->power.lock);
275
276 pdd = dev->power.subsys_data ?
277 dev->power.subsys_data->domain_data : NULL;
278 if (pdd) {
279 to_gpd_data(pdd)->td.constraint_changed = true;
280 genpd = dev_to_genpd(dev);
281 } else {
282 genpd = ERR_PTR(-ENODATA);
283 }
284
285 spin_unlock_irq(&dev->power.lock);
286
287 if (!IS_ERR(genpd)) {
288 mutex_lock(&genpd->lock);
289 genpd->max_off_time_changed = true;
290 mutex_unlock(&genpd->lock);
291 }
292
293 dev = dev->parent;
294 if (!dev || dev->power.ignore_children)
295 break;
296 }
297
298 return NOTIFY_DONE;
299}
300
250/** 301/**
251 * __pm_genpd_save_device - Save the pre-suspend state of a device. 302 * __pm_genpd_save_device - Save the pre-suspend state of a device.
252 * @pdd: Domain data of the device to save the state of. 303 * @pdd: Domain data of the device to save the state of.
@@ -381,7 +432,6 @@ static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
381 return 0; 432 return 0;
382 } 433 }
383 434
384 genpd->max_off_time_ns = -1;
385 if (genpd->gov && genpd->gov->power_down_ok) { 435 if (genpd->gov && genpd->gov->power_down_ok) {
386 if (!genpd->gov->power_down_ok(&genpd->domain)) 436 if (!genpd->gov->power_down_ok(&genpd->domain))
387 return -EAGAIN; 437 return -EAGAIN;
@@ -436,6 +486,7 @@ static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
436 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); 486 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
437 if (elapsed_ns > genpd->power_off_latency_ns) { 487 if (elapsed_ns > genpd->power_off_latency_ns) {
438 genpd->power_off_latency_ns = elapsed_ns; 488 genpd->power_off_latency_ns = elapsed_ns;
489 genpd->max_off_time_changed = true;
439 if (genpd->name) 490 if (genpd->name)
440 pr_warning("%s: Power-off latency exceeded, " 491 pr_warning("%s: Power-off latency exceeded, "
441 "new value %lld ns\n", genpd->name, 492 "new value %lld ns\n", genpd->name,
@@ -496,7 +547,6 @@ static int pm_genpd_runtime_suspend(struct device *dev)
496 if (dev_gpd_data(dev)->always_on) 547 if (dev_gpd_data(dev)->always_on)
497 return -EBUSY; 548 return -EBUSY;
498 549
499 dev_gpd_data(dev)->td.effective_constraint_ns = -1;
500 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL; 550 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
501 if (stop_ok && !stop_ok(dev)) 551 if (stop_ok && !stop_ok(dev))
502 return -EBUSY; 552 return -EBUSY;
@@ -601,6 +651,12 @@ void pm_genpd_poweroff_unused(void)
601 651
602#else 652#else
603 653
654static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
655 unsigned long val, void *ptr)
656{
657 return NOTIFY_DONE;
658}
659
604static inline void genpd_power_off_work_fn(struct work_struct *work) {} 660static inline void genpd_power_off_work_fn(struct work_struct *work) {}
605 661
606#define pm_genpd_runtime_suspend NULL 662#define pm_genpd_runtime_suspend NULL
@@ -1197,6 +1253,14 @@ int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1197 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) 1253 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1198 return -EINVAL; 1254 return -EINVAL;
1199 1255
1256 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1257 if (!gpd_data)
1258 return -ENOMEM;
1259
1260 mutex_init(&gpd_data->lock);
1261 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1262 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1263
1200 genpd_acquire_lock(genpd); 1264 genpd_acquire_lock(genpd);
1201 1265
1202 if (genpd->status == GPD_STATE_POWER_OFF) { 1266 if (genpd->status == GPD_STATE_POWER_OFF) {
@@ -1215,26 +1279,35 @@ int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1215 goto out; 1279 goto out;
1216 } 1280 }
1217 1281
1218 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1219 if (!gpd_data) {
1220 ret = -ENOMEM;
1221 goto out;
1222 }
1223
1224 genpd->device_count++; 1282 genpd->device_count++;
1283 genpd->max_off_time_changed = true;
1225 1284
1226 dev->pm_domain = &genpd->domain;
1227 dev_pm_get_subsys_data(dev); 1285 dev_pm_get_subsys_data(dev);
1286
1287 mutex_lock(&gpd_data->lock);
1288 spin_lock_irq(&dev->power.lock);
1289 dev->pm_domain = &genpd->domain;
1228 dev->power.subsys_data->domain_data = &gpd_data->base; 1290 dev->power.subsys_data->domain_data = &gpd_data->base;
1229 gpd_data->base.dev = dev; 1291 gpd_data->base.dev = dev;
1230 gpd_data->need_restore = false;
1231 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); 1292 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1293 gpd_data->need_restore = false;
1232 if (td) 1294 if (td)
1233 gpd_data->td = *td; 1295 gpd_data->td = *td;
1234 1296
1297 gpd_data->td.constraint_changed = true;
1298 gpd_data->td.effective_constraint_ns = -1;
1299 spin_unlock_irq(&dev->power.lock);
1300 mutex_unlock(&gpd_data->lock);
1301
1302 genpd_release_lock(genpd);
1303
1304 return 0;
1305
1235 out: 1306 out:
1236 genpd_release_lock(genpd); 1307 genpd_release_lock(genpd);
1237 1308
1309 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1310 kfree(gpd_data);
1238 return ret; 1311 return ret;
1239} 1312}
1240 1313
@@ -1278,6 +1351,7 @@ int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1278int pm_genpd_remove_device(struct generic_pm_domain *genpd, 1351int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1279 struct device *dev) 1352 struct device *dev)
1280{ 1353{
1354 struct generic_pm_domain_data *gpd_data;
1281 struct pm_domain_data *pdd; 1355 struct pm_domain_data *pdd;
1282 int ret = 0; 1356 int ret = 0;
1283 1357
@@ -1295,14 +1369,27 @@ int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1295 goto out; 1369 goto out;
1296 } 1370 }
1297 1371
1372 genpd->device_count--;
1373 genpd->max_off_time_changed = true;
1374
1375 spin_lock_irq(&dev->power.lock);
1298 dev->pm_domain = NULL; 1376 dev->pm_domain = NULL;
1299 pdd = dev->power.subsys_data->domain_data; 1377 pdd = dev->power.subsys_data->domain_data;
1300 list_del_init(&pdd->list_node); 1378 list_del_init(&pdd->list_node);
1301 dev->power.subsys_data->domain_data = NULL; 1379 dev->power.subsys_data->domain_data = NULL;
1302 dev_pm_put_subsys_data(dev); 1380 spin_unlock_irq(&dev->power.lock);
1303 kfree(to_gpd_data(pdd));
1304 1381
1305 genpd->device_count--; 1382 gpd_data = to_gpd_data(pdd);
1383 mutex_lock(&gpd_data->lock);
1384 pdd->dev = NULL;
1385 mutex_unlock(&gpd_data->lock);
1386
1387 genpd_release_lock(genpd);
1388
1389 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1390 kfree(gpd_data);
1391 dev_pm_put_subsys_data(dev);
1392 return 0;
1306 1393
1307 out: 1394 out:
1308 genpd_release_lock(genpd); 1395 genpd_release_lock(genpd);
@@ -1673,6 +1760,7 @@ void pm_genpd_init(struct generic_pm_domain *genpd,
1673 genpd->resume_count = 0; 1760 genpd->resume_count = 0;
1674 genpd->device_count = 0; 1761 genpd->device_count = 0;
1675 genpd->max_off_time_ns = -1; 1762 genpd->max_off_time_ns = -1;
1763 genpd->max_off_time_changed = true;
1676 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; 1764 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1677 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; 1765 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1678 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; 1766 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
diff --git a/drivers/base/power/domain_governor.c b/drivers/base/power/domain_governor.c
index 2aae623fd840..3a5c5346bc47 100644
--- a/drivers/base/power/domain_governor.c
+++ b/drivers/base/power/domain_governor.c
@@ -46,18 +46,34 @@ static int dev_update_qos_constraint(struct device *dev, void *data)
46bool default_stop_ok(struct device *dev) 46bool default_stop_ok(struct device *dev)
47{ 47{
48 struct gpd_timing_data *td = &dev_gpd_data(dev)->td; 48 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
49 unsigned long flags;
49 s64 constraint_ns; 50 s64 constraint_ns;
50 51
51 dev_dbg(dev, "%s()\n", __func__); 52 dev_dbg(dev, "%s()\n", __func__);
52 53
53 constraint_ns = dev_pm_qos_read_value(dev); 54 spin_lock_irqsave(&dev->power.lock, flags);
55
56 if (!td->constraint_changed) {
57 bool ret = td->cached_stop_ok;
58
59 spin_unlock_irqrestore(&dev->power.lock, flags);
60 return ret;
61 }
62 td->constraint_changed = false;
63 td->cached_stop_ok = false;
64 td->effective_constraint_ns = -1;
65 constraint_ns = __dev_pm_qos_read_value(dev);
66
67 spin_unlock_irqrestore(&dev->power.lock, flags);
68
54 if (constraint_ns < 0) 69 if (constraint_ns < 0)
55 return false; 70 return false;
56 71
57 constraint_ns *= NSEC_PER_USEC; 72 constraint_ns *= NSEC_PER_USEC;
58 /* 73 /*
59 * We can walk the children without any additional locking, because 74 * We can walk the children without any additional locking, because
60 * they all have been suspended at this point. 75 * they all have been suspended at this point and their
76 * effective_constraint_ns fields won't be modified in parallel with us.
61 */ 77 */
62 if (!dev->power.ignore_children) 78 if (!dev->power.ignore_children)
63 device_for_each_child(dev, &constraint_ns, 79 device_for_each_child(dev, &constraint_ns,
@@ -69,11 +85,13 @@ bool default_stop_ok(struct device *dev)
69 return false; 85 return false;
70 } 86 }
71 td->effective_constraint_ns = constraint_ns; 87 td->effective_constraint_ns = constraint_ns;
88 td->cached_stop_ok = constraint_ns > td->stop_latency_ns ||
89 constraint_ns == 0;
72 /* 90 /*
73 * The children have been suspended already, so we don't need to take 91 * The children have been suspended already, so we don't need to take
74 * their stop latencies into account here. 92 * their stop latencies into account here.
75 */ 93 */
76 return constraint_ns > td->stop_latency_ns || constraint_ns == 0; 94 return td->cached_stop_ok;
77} 95}
78 96
79/** 97/**
@@ -90,6 +108,25 @@ static bool default_power_down_ok(struct dev_pm_domain *pd)
90 s64 min_dev_off_time_ns; 108 s64 min_dev_off_time_ns;
91 s64 off_on_time_ns; 109 s64 off_on_time_ns;
92 110
111 if (genpd->max_off_time_changed) {
112 struct gpd_link *link;
113
114 /*
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
118 * returns.
119 */
120 list_for_each_entry(link, &genpd->slave_links, slave_node)
121 link->master->max_off_time_changed = true;
122
123 genpd->max_off_time_changed = false;
124 genpd->cached_power_down_ok = false;
125 genpd->max_off_time_ns = -1;
126 } else {
127 return genpd->cached_power_down_ok;
128 }
129
93 off_on_time_ns = genpd->power_off_latency_ns + 130 off_on_time_ns = genpd->power_off_latency_ns +
94 genpd->power_on_latency_ns; 131 genpd->power_on_latency_ns;
95 /* 132 /*
@@ -165,6 +202,8 @@ static bool default_power_down_ok(struct dev_pm_domain *pd)
165 min_dev_off_time_ns = constraint_ns; 202 min_dev_off_time_ns = constraint_ns;
166 } 203 }
167 204
205 genpd->cached_power_down_ok = true;
206
168 /* 207 /*
169 * If the computed minimum device off time is negative, there are no 208 * If the computed minimum device off time is negative, there are no
170 * latency constraints, so the domain can spend arbitrary time in the 209 * latency constraints, so the domain can spend arbitrary time in the