xref: /openbmc/linux/drivers/base/power/main.c (revision f42b3800)
1 /*
2  * drivers/base/power/main.c - Where the driver meets power management.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  *
10  * The driver model core calls device_pm_add() when a device is registered.
11  * This will intialize the embedded device_pm_info object in the device
12  * and add it to the list of power-controlled devices. sysfs entries for
13  * controlling device power management will also be added.
14  *
15  * A different set of lists than the global subsystem list are used to
16  * keep track of power info because we use different lists to hold
17  * devices based on what stage of the power management process they
18  * are in. The power domain dependencies may also differ from the
19  * ancestral dependencies that the subsystem list maintains.
20  */
21 
22 #include <linux/device.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mutex.h>
25 #include <linux/pm.h>
26 #include <linux/resume-trace.h>
27 #include <linux/rwsem.h>
28 
29 #include "../base.h"
30 #include "power.h"
31 
32 /*
33  * The entries in the dpm_active list are in a depth first order, simply
34  * because children are guaranteed to be discovered after parents, and
35  * are inserted at the back of the list on discovery.
36  *
37  * All the other lists are kept in the same order, for consistency.
38  * However the lists aren't always traversed in the same order.
39  * Semaphores must be acquired from the top (i.e., front) down
40  * and released in the opposite order.  Devices must be suspended
41  * from the bottom (i.e., end) up and resumed in the opposite order.
42  * That way no parent will be suspended while it still has an active
43  * child.
44  *
45  * Since device_pm_add() may be called with a device semaphore held,
46  * we must never try to acquire a device semaphore while holding
47  * dpm_list_mutex.
48  */
49 
50 LIST_HEAD(dpm_active);
51 static LIST_HEAD(dpm_off);
52 static LIST_HEAD(dpm_off_irq);
53 static LIST_HEAD(dpm_destroy);
54 
55 static DEFINE_MUTEX(dpm_list_mtx);
56 
57 static DECLARE_RWSEM(pm_sleep_rwsem);
58 
59 int (*platform_enable_wakeup)(struct device *dev, int is_on);
60 
61 /**
62  *	device_pm_add - add a device to the list of active devices
63  *	@dev:	Device to be added to the list
64  */
65 void device_pm_add(struct device *dev)
66 {
67 	pr_debug("PM: Adding info for %s:%s\n",
68 		 dev->bus ? dev->bus->name : "No Bus",
69 		 kobject_name(&dev->kobj));
70 	mutex_lock(&dpm_list_mtx);
71 	list_add_tail(&dev->power.entry, &dpm_active);
72 	mutex_unlock(&dpm_list_mtx);
73 }
74 
75 /**
76  *	device_pm_remove - remove a device from the list of active devices
77  *	@dev:	Device to be removed from the list
78  *
79  *	This function also removes the device's PM-related sysfs attributes.
80  */
81 void device_pm_remove(struct device *dev)
82 {
83 	pr_debug("PM: Removing info for %s:%s\n",
84 		 dev->bus ? dev->bus->name : "No Bus",
85 		 kobject_name(&dev->kobj));
86 	mutex_lock(&dpm_list_mtx);
87 	dpm_sysfs_remove(dev);
88 	list_del_init(&dev->power.entry);
89 	mutex_unlock(&dpm_list_mtx);
90 }
91 
92 /**
93  *	device_pm_schedule_removal - schedule the removal of a suspended device
94  *	@dev:	Device to destroy
95  *
96  *	Moves the device to the dpm_destroy list for further processing by
97  *	unregister_dropped_devices().
98  */
99 void device_pm_schedule_removal(struct device *dev)
100 {
101 	pr_debug("PM: Preparing for removal: %s:%s\n",
102 		dev->bus ? dev->bus->name : "No Bus",
103 		kobject_name(&dev->kobj));
104 	mutex_lock(&dpm_list_mtx);
105 	list_move_tail(&dev->power.entry, &dpm_destroy);
106 	mutex_unlock(&dpm_list_mtx);
107 }
108 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
109 
110 /**
111  *	pm_sleep_lock - mutual exclusion for registration and suspend
112  *
113  *	Returns 0 if no suspend is underway and device registration
114  *	may proceed, otherwise -EBUSY.
115  */
116 int pm_sleep_lock(void)
117 {
118 	if (down_read_trylock(&pm_sleep_rwsem))
119 		return 0;
120 
121 	return -EBUSY;
122 }
123 
124 /**
125  *	pm_sleep_unlock - mutual exclusion for registration and suspend
126  *
127  *	This routine undoes the effect of device_pm_add_lock
128  *	when a device's registration is complete.
129  */
130 void pm_sleep_unlock(void)
131 {
132 	up_read(&pm_sleep_rwsem);
133 }
134 
135 
136 /*------------------------- Resume routines -------------------------*/
137 
138 /**
139  *	resume_device_early - Power on one device (early resume).
140  *	@dev:	Device.
141  *
142  *	Must be called with interrupts disabled.
143  */
144 static int resume_device_early(struct device *dev)
145 {
146 	int error = 0;
147 
148 	TRACE_DEVICE(dev);
149 	TRACE_RESUME(0);
150 
151 	if (dev->bus && dev->bus->resume_early) {
152 		dev_dbg(dev, "EARLY resume\n");
153 		error = dev->bus->resume_early(dev);
154 	}
155 
156 	TRACE_RESUME(error);
157 	return error;
158 }
159 
160 /**
161  *	dpm_power_up - Power on all regular (non-sysdev) devices.
162  *
163  *	Walk the dpm_off_irq list and power each device up. This
164  *	is used for devices that required they be powered down with
165  *	interrupts disabled. As devices are powered on, they are moved
166  *	to the dpm_off list.
167  *
168  *	Must be called with interrupts disabled and only one CPU running.
169  */
170 static void dpm_power_up(void)
171 {
172 
173 	while (!list_empty(&dpm_off_irq)) {
174 		struct list_head *entry = dpm_off_irq.next;
175 		struct device *dev = to_device(entry);
176 
177 		list_move_tail(entry, &dpm_off);
178 		resume_device_early(dev);
179 	}
180 }
181 
182 /**
183  *	device_power_up - Turn on all devices that need special attention.
184  *
185  *	Power on system devices, then devices that required we shut them down
186  *	with interrupts disabled.
187  *
188  *	Must be called with interrupts disabled.
189  */
190 void device_power_up(void)
191 {
192 	sysdev_resume();
193 	dpm_power_up();
194 }
195 EXPORT_SYMBOL_GPL(device_power_up);
196 
197 /**
198  *	resume_device - Restore state for one device.
199  *	@dev:	Device.
200  *
201  */
202 static int resume_device(struct device *dev)
203 {
204 	int error = 0;
205 
206 	TRACE_DEVICE(dev);
207 	TRACE_RESUME(0);
208 
209 	down(&dev->sem);
210 
211 	if (dev->bus && dev->bus->resume) {
212 		dev_dbg(dev,"resuming\n");
213 		error = dev->bus->resume(dev);
214 	}
215 
216 	if (!error && dev->type && dev->type->resume) {
217 		dev_dbg(dev,"resuming\n");
218 		error = dev->type->resume(dev);
219 	}
220 
221 	if (!error && dev->class && dev->class->resume) {
222 		dev_dbg(dev,"class resume\n");
223 		error = dev->class->resume(dev);
224 	}
225 
226 	up(&dev->sem);
227 
228 	TRACE_RESUME(error);
229 	return error;
230 }
231 
232 /**
233  *	dpm_resume - Resume every device.
234  *
235  *	Resume the devices that have either not gone through
236  *	the late suspend, or that did go through it but also
237  *	went through the early resume.
238  *
239  *	Take devices from the dpm_off_list, resume them,
240  *	and put them on the dpm_locked list.
241  */
242 static void dpm_resume(void)
243 {
244 	mutex_lock(&dpm_list_mtx);
245 	while(!list_empty(&dpm_off)) {
246 		struct list_head *entry = dpm_off.next;
247 		struct device *dev = to_device(entry);
248 
249 		list_move_tail(entry, &dpm_active);
250 		mutex_unlock(&dpm_list_mtx);
251 		resume_device(dev);
252 		mutex_lock(&dpm_list_mtx);
253 	}
254 	mutex_unlock(&dpm_list_mtx);
255 }
256 
257 /**
258  *	unregister_dropped_devices - Unregister devices scheduled for removal
259  *
260  *	Unregister all devices on the dpm_destroy list.
261  */
262 static void unregister_dropped_devices(void)
263 {
264 	mutex_lock(&dpm_list_mtx);
265 	while (!list_empty(&dpm_destroy)) {
266 		struct list_head *entry = dpm_destroy.next;
267 		struct device *dev = to_device(entry);
268 
269 		mutex_unlock(&dpm_list_mtx);
270 		/* This also removes the device from the list */
271 		device_unregister(dev);
272 		mutex_lock(&dpm_list_mtx);
273 	}
274 	mutex_unlock(&dpm_list_mtx);
275 }
276 
277 /**
278  *	device_resume - Restore state of each device in system.
279  *
280  *	Resume all the devices, unlock them all, and allow new
281  *	devices to be registered once again.
282  */
283 void device_resume(void)
284 {
285 	might_sleep();
286 	dpm_resume();
287 	unregister_dropped_devices();
288 	up_write(&pm_sleep_rwsem);
289 }
290 EXPORT_SYMBOL_GPL(device_resume);
291 
292 
293 /*------------------------- Suspend routines -------------------------*/
294 
295 static inline char *suspend_verb(u32 event)
296 {
297 	switch (event) {
298 	case PM_EVENT_SUSPEND:	return "suspend";
299 	case PM_EVENT_FREEZE:	return "freeze";
300 	case PM_EVENT_PRETHAW:	return "prethaw";
301 	default:		return "(unknown suspend event)";
302 	}
303 }
304 
305 static void
306 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
307 {
308 	dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
309 		((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
310 		", may wakeup" : "");
311 }
312 
313 /**
314  *	suspend_device_late - Shut down one device (late suspend).
315  *	@dev:	Device.
316  *	@state:	Power state device is entering.
317  *
318  *	This is called with interrupts off and only a single CPU running.
319  */
320 static int suspend_device_late(struct device *dev, pm_message_t state)
321 {
322 	int error = 0;
323 
324 	if (dev->bus && dev->bus->suspend_late) {
325 		suspend_device_dbg(dev, state, "LATE ");
326 		error = dev->bus->suspend_late(dev, state);
327 		suspend_report_result(dev->bus->suspend_late, error);
328 	}
329 	return error;
330 }
331 
332 /**
333  *	device_power_down - Shut down special devices.
334  *	@state:		Power state to enter.
335  *
336  *	Power down devices that require interrupts to be disabled
337  *	and move them from the dpm_off list to the dpm_off_irq list.
338  *	Then power down system devices.
339  *
340  *	Must be called with interrupts disabled and only one CPU running.
341  */
342 int device_power_down(pm_message_t state)
343 {
344 	int error = 0;
345 
346 	while (!list_empty(&dpm_off)) {
347 		struct list_head *entry = dpm_off.prev;
348 		struct device *dev = to_device(entry);
349 
350 		error = suspend_device_late(dev, state);
351 		if (error) {
352 			printk(KERN_ERR "Could not power down device %s: "
353 					"error %d\n",
354 					kobject_name(&dev->kobj), error);
355 			break;
356 		}
357 		if (!list_empty(&dev->power.entry))
358 			list_move(&dev->power.entry, &dpm_off_irq);
359 	}
360 
361 	if (!error)
362 		error = sysdev_suspend(state);
363 	if (error)
364 		dpm_power_up();
365 	return error;
366 }
367 EXPORT_SYMBOL_GPL(device_power_down);
368 
369 /**
370  *	suspend_device - Save state of one device.
371  *	@dev:	Device.
372  *	@state:	Power state device is entering.
373  */
374 static int suspend_device(struct device *dev, pm_message_t state)
375 {
376 	int error = 0;
377 
378 	down(&dev->sem);
379 
380 	if (dev->power.power_state.event) {
381 		dev_dbg(dev, "PM: suspend %d-->%d\n",
382 			dev->power.power_state.event, state.event);
383 	}
384 
385 	if (dev->class && dev->class->suspend) {
386 		suspend_device_dbg(dev, state, "class ");
387 		error = dev->class->suspend(dev, state);
388 		suspend_report_result(dev->class->suspend, error);
389 	}
390 
391 	if (!error && dev->type && dev->type->suspend) {
392 		suspend_device_dbg(dev, state, "type ");
393 		error = dev->type->suspend(dev, state);
394 		suspend_report_result(dev->type->suspend, error);
395 	}
396 
397 	if (!error && dev->bus && dev->bus->suspend) {
398 		suspend_device_dbg(dev, state, "");
399 		error = dev->bus->suspend(dev, state);
400 		suspend_report_result(dev->bus->suspend, error);
401 	}
402 
403 	up(&dev->sem);
404 
405 	return error;
406 }
407 
408 /**
409  *	dpm_suspend - Suspend every device.
410  *	@state:	Power state to put each device in.
411  *
412  *	Walk the dpm_locked list.  Suspend each device and move it
413  *	to the dpm_off list.
414  *
415  *	(For historical reasons, if it returns -EAGAIN, that used to mean
416  *	that the device would be called again with interrupts disabled.
417  *	These days, we use the "suspend_late()" callback for that, so we
418  *	print a warning and consider it an error).
419  */
420 static int dpm_suspend(pm_message_t state)
421 {
422 	int error = 0;
423 
424 	mutex_lock(&dpm_list_mtx);
425 	while (!list_empty(&dpm_active)) {
426 		struct list_head *entry = dpm_active.prev;
427 		struct device *dev = to_device(entry);
428 
429 		mutex_unlock(&dpm_list_mtx);
430 		error = suspend_device(dev, state);
431 		mutex_lock(&dpm_list_mtx);
432 		if (error) {
433 			printk(KERN_ERR "Could not suspend device %s: "
434 					"error %d%s\n",
435 					kobject_name(&dev->kobj),
436 					error,
437 					(error == -EAGAIN ?
438 					" (please convert to suspend_late)" :
439 					""));
440 			break;
441 		}
442 		if (!list_empty(&dev->power.entry))
443 			list_move(&dev->power.entry, &dpm_off);
444 	}
445 	mutex_unlock(&dpm_list_mtx);
446 
447 	return error;
448 }
449 
450 /**
451  *	device_suspend - Save state and stop all devices in system.
452  *	@state: new power management state
453  *
454  *	Prevent new devices from being registered, then lock all devices
455  *	and suspend them.
456  */
457 int device_suspend(pm_message_t state)
458 {
459 	int error;
460 
461 	might_sleep();
462 	down_write(&pm_sleep_rwsem);
463 	error = dpm_suspend(state);
464 	if (error)
465 		device_resume();
466 	return error;
467 }
468 EXPORT_SYMBOL_GPL(device_suspend);
469 
470 void __suspend_report_result(const char *function, void *fn, int ret)
471 {
472 	if (ret) {
473 		printk(KERN_ERR "%s(): ", function);
474 		print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
475 		printk("%d\n", ret);
476 	}
477 }
478 EXPORT_SYMBOL_GPL(__suspend_report_result);
479