main.c 12 KB

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