domain_governor.c 5.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * drivers/base/power/domain_governor.c - Governors for device PM domains.
 *
 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 *
 * This file is released under the GPLv2.
 */

#include <linux/kernel.h>
#include <linux/pm_domain.h>
#include <linux/pm_qos.h>
12
#include <linux/hrtimer.h>
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static int dev_update_qos_constraint(struct device *dev, void *data)
{
	s64 *constraint_ns_p = data;
	s32 constraint_ns = -1;

	if (dev->power.subsys_data && dev->power.subsys_data->domain_data)
		constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;

	if (constraint_ns < 0) {
		constraint_ns = dev_pm_qos_read_value(dev);
		constraint_ns *= NSEC_PER_USEC;
	}
	if (constraint_ns == 0)
		return 0;

	/*
	 * constraint_ns cannot be negative here, because the device has been
	 * suspended.
	 */
	if (constraint_ns < *constraint_ns_p || *constraint_ns_p == 0)
		*constraint_ns_p = constraint_ns;

	return 0;
}

39
40
41
42
/**
 * default_stop_ok - Default PM domain governor routine for stopping devices.
 * @dev: Device to check.
 */
43
static bool default_stop_ok(struct device *dev)
44
45
{
	struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
46
	unsigned long flags;
47
	s64 constraint_ns;
48
49
50

	dev_dbg(dev, "%s()\n", __func__);

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	spin_lock_irqsave(&dev->power.lock, flags);

	if (!td->constraint_changed) {
		bool ret = td->cached_stop_ok;

		spin_unlock_irqrestore(&dev->power.lock, flags);
		return ret;
	}
	td->constraint_changed = false;
	td->cached_stop_ok = false;
	td->effective_constraint_ns = -1;
	constraint_ns = __dev_pm_qos_read_value(dev);

	spin_unlock_irqrestore(&dev->power.lock, flags);

66
67
	if (constraint_ns < 0)
		return false;
68

69
70
71
	constraint_ns *= NSEC_PER_USEC;
	/*
	 * We can walk the children without any additional locking, because
72
73
	 * they all have been suspended at this point and their
	 * effective_constraint_ns fields won't be modified in parallel with us.
74
75
76
77
78
79
	 */
	if (!dev->power.ignore_children)
		device_for_each_child(dev, &constraint_ns,
				      dev_update_qos_constraint);

	if (constraint_ns > 0) {
80
81
		constraint_ns -= td->suspend_latency_ns +
				td->resume_latency_ns;
82
83
84
85
		if (constraint_ns == 0)
			return false;
	}
	td->effective_constraint_ns = constraint_ns;
86
87
	td->cached_stop_ok = constraint_ns >= 0;

88
89
90
91
	/*
	 * The children have been suspended already, so we don't need to take
	 * their stop latencies into account here.
	 */
92
	return td->cached_stop_ok;
93
94
}

95
96
97
98
99
100
101
102
103
104
105
/**
 * default_power_down_ok - Default generic PM domain power off governor routine.
 * @pd: PM domain to check.
 *
 * This routine must be executed under the PM domain's lock.
 */
static bool default_power_down_ok(struct dev_pm_domain *pd)
{
	struct generic_pm_domain *genpd = pd_to_genpd(pd);
	struct gpd_link *link;
	struct pm_domain_data *pdd;
106
	s64 min_off_time_ns;
107
108
	s64 off_on_time_ns;

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	if (genpd->max_off_time_changed) {
		struct gpd_link *link;

		/*
		 * We have to invalidate the cached results for the masters, so
		 * use the observation that default_power_down_ok() is not
		 * going to be called for any master until this instance
		 * returns.
		 */
		list_for_each_entry(link, &genpd->slave_links, slave_node)
			link->master->max_off_time_changed = true;

		genpd->max_off_time_changed = false;
		genpd->cached_power_down_ok = false;
		genpd->max_off_time_ns = -1;
	} else {
		return genpd->cached_power_down_ok;
	}

128
129
130
	off_on_time_ns = genpd->power_off_latency_ns +
				genpd->power_on_latency_ns;

131
	min_off_time_ns = -1;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	/*
	 * Check if subdomains can be off for enough time.
	 *
	 * All subdomains have been powered off already at this point.
	 */
	list_for_each_entry(link, &genpd->master_links, master_node) {
		struct generic_pm_domain *sd = link->slave;
		s64 sd_max_off_ns = sd->max_off_time_ns;

		if (sd_max_off_ns < 0)
			continue;

		/*
		 * Check if the subdomain is allowed to be off long enough for
		 * the current domain to turn off and on (that's how much time
		 * it will have to wait worst case).
		 */
		if (sd_max_off_ns <= off_on_time_ns)
			return false;
151
152
153

		if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
			min_off_time_ns = sd_max_off_ns;
154
155
156
157
158
159
160
	}

	/*
	 * Check if the devices in the domain can be off enough time.
	 */
	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
		struct gpd_timing_data *td;
161
		s64 constraint_ns;
162

163
164
165
166
167
		/*
		 * Check if the device is allowed to be off long enough for the
		 * domain to turn off and on (that's how much time it will
		 * have to wait worst case).
		 */
168
		td = &to_gpd_data(pdd)->td;
169
170
171
172
173
174
175
176
		constraint_ns = td->effective_constraint_ns;
		/* default_stop_ok() need not be called before us. */
		if (constraint_ns < 0) {
			constraint_ns = dev_pm_qos_read_value(pdd->dev);
			constraint_ns *= NSEC_PER_USEC;
		}
		if (constraint_ns == 0)
			continue;
177
178

		/*
179
180
		 * constraint_ns cannot be negative here, because the device has
		 * been suspended.
181
		 */
182
183
184
		if (constraint_ns <= off_on_time_ns)
			return false;

185
186
		if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
			min_off_time_ns = constraint_ns;
187
188
	}

189
190
	genpd->cached_power_down_ok = true;

191
	/*
192
193
194
	 * If the computed minimum device off time is negative, there are no
	 * latency constraints, so the domain can spend arbitrary time in the
	 * "off" state.
195
	 */
196
	if (min_off_time_ns < 0)
197
		return true;
198
199

	/*
200
201
202
	 * The difference between the computed minimum subdomain or device off
	 * time and the time needed to turn the domain on is the maximum
	 * theoretical time this domain can spend in the "off" state.
203
	 */
204
	genpd->max_off_time_ns = min_off_time_ns - genpd->power_on_latency_ns;
205
206
207
	return true;
}

208
209
210
211
212
static bool always_on_power_down_ok(struct dev_pm_domain *domain)
{
	return false;
}

213
214
215
216
217
struct dev_power_governor simple_qos_governor = {
	.stop_ok = default_stop_ok,
	.power_down_ok = default_power_down_ok,
};

218
219
220
221
222
223
224
/**
 * pm_genpd_gov_always_on - A governor implementing an always-on policy
 */
struct dev_power_governor pm_domain_always_on_gov = {
	.power_down_ok = always_on_power_down_ok,
	.stop_ok = default_stop_ok,
};