]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/base/power/domain_governor.c
51527ee92d101018680cd5b633265105a2952df1
[~shefty/rdma-dev.git] / drivers / base / power / domain_governor.c
1 /*
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>
13 #include <linux/hrtimer.h>
14
15 /**
16  * default_stop_ok - Default PM domain governor routine for stopping devices.
17  * @dev: Device to check.
18  */
19 bool default_stop_ok(struct device *dev)
20 {
21         struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
22
23         dev_dbg(dev, "%s()\n", __func__);
24
25         if (dev->power.max_time_suspended_ns < 0 || td->break_even_ns == 0)
26                 return true;
27
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;
30 }
31
32 /**
33  * default_power_down_ok - Default generic PM domain power off governor routine.
34  * @pd: PM domain to check.
35  *
36  * This routine must be executed under the PM domain's lock.
37  */
38 static bool default_power_down_ok(struct dev_pm_domain *pd)
39 {
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();
46
47         off_on_time_ns = genpd->power_off_latency_ns +
48                                 genpd->power_on_latency_ns;
49         /*
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.
53          *
54          * All devices in this domain have been stopped already at this point.
55          */
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;
60         }
61
62         /*
63          * Check if subdomains can be off for enough time.
64          *
65          * All subdomains have been powered off already at this point.
66          */
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;
70
71                 if (sd_max_off_ns < 0)
72                         continue;
73
74                 sd_max_off_ns -= ktime_to_ns(ktime_sub(time_now,
75                                                        sd->power_off_time));
76                 /*
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).
80                  */
81                 if (sd_max_off_ns <= off_on_time_ns)
82                         return false;
83         }
84
85         /*
86          * Check if the devices in the domain can be off enough time.
87          */
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;
93
94                 if (!dev->driver || dev->power.max_time_suspended_ns < 0)
95                         continue;
96
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;
104
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;
108         }
109
110         if (min_dev_off_time_ns < 0) {
111                 /*
112                  * There are no latency constraints, so the domain can spend
113                  * arbitrary time in the "off" state.
114                  */
115                 genpd->max_off_time_ns = -1;
116                 return true;
117         }
118
119         /*
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.
123          */
124         min_dev_off_time_ns -= genpd->power_on_latency_ns;
125
126         /*
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.
131          */
132         if (genpd->break_even_ns >
133             min_dev_off_time_ns - genpd->power_off_latency_ns)
134                 return false;
135
136         genpd->max_off_time_ns = min_dev_off_time_ns;
137         return true;
138 }
139
140 struct dev_power_governor simple_qos_governor = {
141         .stop_ok = default_stop_ok,
142         .power_down_ok = default_power_down_ok,
143 };
144
145 static bool always_on_power_down_ok(struct dev_pm_domain *domain)
146 {
147         return false;
148 }
149
150 /**
151  * pm_genpd_gov_always_on - A governor implementing an always-on policy
152  */
153 struct dev_power_governor pm_domain_always_on_gov = {
154         .power_down_ok = always_on_power_down_ok,
155         .stop_ok = default_stop_ok,
156 };