PM / Domains: Add default power off governor function (v4)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / power / domain_governor.c
blobda78540e9b406bbb70d831f82a012c9c1b810538
1 /*
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.
7 */
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>
15 /**
16 * default_stop_ok - Default PM domain governor routine for stopping devices.
17 * @dev: Device to check.
19 bool default_stop_ok(struct device *dev)
21 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
23 dev_dbg(dev, "%s()\n", __func__);
25 if (dev->power.max_time_suspended_ns < 0 || td->break_even_ns == 0)
26 return true;
28 return td->stop_latency_ns + td->start_latency_ns < td->break_even_ns
29 && td->break_even_ns < dev->power.max_time_suspended_ns;
32 /**
33 * default_power_down_ok - Default generic PM domain power off governor routine.
34 * @pd: PM domain to check.
36 * This routine must be executed under the PM domain's lock.
38 static bool default_power_down_ok(struct dev_pm_domain *pd)
40 struct generic_pm_domain *genpd = pd_to_genpd(pd);
41 struct gpd_link *link;
42 struct pm_domain_data *pdd;
43 s64 min_dev_off_time_ns;
44 s64 off_on_time_ns;
45 ktime_t time_now = ktime_get();
47 off_on_time_ns = genpd->power_off_latency_ns +
48 genpd->power_on_latency_ns;
50 * It doesn't make sense to remove power from the domain if saving
51 * the state of all devices in it and the power off/power on operations
52 * take too much time.
54 * All devices in this domain have been stopped already at this point.
56 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
57 if (pdd->dev->driver)
58 off_on_time_ns +=
59 to_gpd_data(pdd)->td.save_state_latency_ns;
63 * Check if subdomains can be off for enough time.
65 * All subdomains have been powered off already at this point.
67 list_for_each_entry(link, &genpd->master_links, master_node) {
68 struct generic_pm_domain *sd = link->slave;
69 s64 sd_max_off_ns = sd->max_off_time_ns;
71 if (sd_max_off_ns < 0)
72 continue;
74 sd_max_off_ns -= ktime_to_ns(ktime_sub(time_now,
75 sd->power_off_time));
77 * Check if the subdomain is allowed to be off long enough for
78 * the current domain to turn off and on (that's how much time
79 * it will have to wait worst case).
81 if (sd_max_off_ns <= off_on_time_ns)
82 return false;
86 * Check if the devices in the domain can be off enough time.
88 min_dev_off_time_ns = -1;
89 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
90 struct gpd_timing_data *td;
91 struct device *dev = pdd->dev;
92 s64 dev_off_time_ns;
94 if (!dev->driver || dev->power.max_time_suspended_ns < 0)
95 continue;
97 td = &to_gpd_data(pdd)->td;
98 dev_off_time_ns = dev->power.max_time_suspended_ns -
99 (td->start_latency_ns + td->restore_state_latency_ns +
100 ktime_to_ns(ktime_sub(time_now,
101 dev->power.suspend_time)));
102 if (dev_off_time_ns <= off_on_time_ns)
103 return false;
105 if (min_dev_off_time_ns > dev_off_time_ns
106 || min_dev_off_time_ns < 0)
107 min_dev_off_time_ns = dev_off_time_ns;
110 if (min_dev_off_time_ns < 0) {
112 * There are no latency constraints, so the domain can spend
113 * arbitrary time in the "off" state.
115 genpd->max_off_time_ns = -1;
116 return true;
120 * The difference between the computed minimum delta and the time needed
121 * to turn the domain on is the maximum theoretical time this domain can
122 * spend in the "off" state.
124 min_dev_off_time_ns -= genpd->power_on_latency_ns;
127 * If the difference between the computed minimum delta and the time
128 * needed to turn the domain off and back on on is smaller than the
129 * domain's power break even time, removing power from the domain is not
130 * worth it.
132 if (genpd->break_even_ns >
133 min_dev_off_time_ns - genpd->power_off_latency_ns)
134 return false;
136 genpd->max_off_time_ns = min_dev_off_time_ns;
137 return true;
140 struct dev_power_governor simple_qos_governor = {
141 .stop_ok = default_stop_ok,
142 .power_down_ok = default_power_down_ok,