main.c 12 KB

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