main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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_locked);
  48. static LIST_HEAD(dpm_off);
  49. static LIST_HEAD(dpm_off_irq);
  50. static LIST_HEAD(dpm_destroy);
  51. static DEFINE_MUTEX(dpm_list_mtx);
  52. static DECLARE_RWSEM(pm_sleep_rwsem);
  53. int (*platform_enable_wakeup)(struct device *dev, int is_on);
  54. /**
  55. * device_pm_add - add a device to the list of active devices
  56. * @dev: Device to be added to the list
  57. */
  58. void device_pm_add(struct device *dev)
  59. {
  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. list_add_tail(&dev->power.entry, &dpm_active);
  65. mutex_unlock(&dpm_list_mtx);
  66. }
  67. /**
  68. * device_pm_remove - remove a device from the list of active devices
  69. * @dev: Device to be removed from the list
  70. *
  71. * This function also removes the device's PM-related sysfs attributes.
  72. */
  73. void device_pm_remove(struct device *dev)
  74. {
  75. /*
  76. * If this function is called during a suspend, it will be blocked,
  77. * because we're holding the device's semaphore at that time, which may
  78. * lead to a deadlock. In that case we want to print a warning.
  79. * However, it may also be called by unregister_dropped_devices() with
  80. * the device's semaphore released, in which case the warning should
  81. * not be printed.
  82. */
  83. if (down_trylock(&dev->sem)) {
  84. if (down_read_trylock(&pm_sleep_rwsem)) {
  85. /* No suspend in progress, wait on dev->sem */
  86. down(&dev->sem);
  87. up_read(&pm_sleep_rwsem);
  88. } else {
  89. /* Suspend in progress, we may deadlock */
  90. dev_warn(dev, "Suspicious %s during suspend\n",
  91. __FUNCTION__);
  92. dump_stack();
  93. /* The user has been warned ... */
  94. down(&dev->sem);
  95. }
  96. }
  97. pr_debug("PM: Removing info for %s:%s\n",
  98. dev->bus ? dev->bus->name : "No Bus",
  99. kobject_name(&dev->kobj));
  100. mutex_lock(&dpm_list_mtx);
  101. dpm_sysfs_remove(dev);
  102. list_del_init(&dev->power.entry);
  103. mutex_unlock(&dpm_list_mtx);
  104. up(&dev->sem);
  105. }
  106. /**
  107. * device_pm_schedule_removal - schedule the removal of a suspended device
  108. * @dev: Device to destroy
  109. *
  110. * Moves the device to the dpm_destroy list for further processing by
  111. * unregister_dropped_devices().
  112. */
  113. void device_pm_schedule_removal(struct device *dev)
  114. {
  115. pr_debug("PM: Preparing for removal: %s:%s\n",
  116. dev->bus ? dev->bus->name : "No Bus",
  117. kobject_name(&dev->kobj));
  118. mutex_lock(&dpm_list_mtx);
  119. list_move_tail(&dev->power.entry, &dpm_destroy);
  120. mutex_unlock(&dpm_list_mtx);
  121. }
  122. EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
  123. /**
  124. * pm_sleep_lock - mutual exclusion for registration and suspend
  125. *
  126. * Returns 0 if no suspend is underway and device registration
  127. * may proceed, otherwise -EBUSY.
  128. */
  129. int pm_sleep_lock(void)
  130. {
  131. if (down_read_trylock(&pm_sleep_rwsem))
  132. return 0;
  133. return -EBUSY;
  134. }
  135. /**
  136. * pm_sleep_unlock - mutual exclusion for registration and suspend
  137. *
  138. * This routine undoes the effect of device_pm_add_lock
  139. * when a device's registration is complete.
  140. */
  141. void pm_sleep_unlock(void)
  142. {
  143. up_read(&pm_sleep_rwsem);
  144. }
  145. /*------------------------- Resume routines -------------------------*/
  146. /**
  147. * resume_device_early - Power on one device (early resume).
  148. * @dev: Device.
  149. *
  150. * Must be called with interrupts disabled.
  151. */
  152. static int resume_device_early(struct device *dev)
  153. {
  154. int error = 0;
  155. TRACE_DEVICE(dev);
  156. TRACE_RESUME(0);
  157. if (dev->bus && dev->bus->resume_early) {
  158. dev_dbg(dev, "EARLY resume\n");
  159. error = dev->bus->resume_early(dev);
  160. }
  161. TRACE_RESUME(error);
  162. return error;
  163. }
  164. /**
  165. * dpm_power_up - Power on all regular (non-sysdev) devices.
  166. *
  167. * Walk the dpm_off_irq list and power each device up. This
  168. * is used for devices that required they be powered down with
  169. * interrupts disabled. As devices are powered on, they are moved
  170. * to the dpm_off list.
  171. *
  172. * Must be called with interrupts disabled and only one CPU running.
  173. */
  174. static void dpm_power_up(void)
  175. {
  176. while (!list_empty(&dpm_off_irq)) {
  177. struct list_head *entry = dpm_off_irq.next;
  178. struct device *dev = to_device(entry);
  179. list_move_tail(entry, &dpm_off);
  180. resume_device_early(dev);
  181. }
  182. }
  183. /**
  184. * device_power_up - Turn on all devices that need special attention.
  185. *
  186. * Power on system devices, then devices that required we shut them down
  187. * with interrupts disabled.
  188. *
  189. * Must be called with interrupts disabled.
  190. */
  191. void device_power_up(void)
  192. {
  193. sysdev_resume();
  194. dpm_power_up();
  195. }
  196. EXPORT_SYMBOL_GPL(device_power_up);
  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. TRACE_DEVICE(dev);
  206. TRACE_RESUME(0);
  207. if (dev->bus && dev->bus->resume) {
  208. dev_dbg(dev,"resuming\n");
  209. error = dev->bus->resume(dev);
  210. }
  211. if (!error && dev->type && dev->type->resume) {
  212. dev_dbg(dev,"resuming\n");
  213. error = dev->type->resume(dev);
  214. }
  215. if (!error && dev->class && dev->class->resume) {
  216. dev_dbg(dev,"class resume\n");
  217. error = dev->class->resume(dev);
  218. }
  219. TRACE_RESUME(error);
  220. return error;
  221. }
  222. /**
  223. * dpm_resume - Resume every device.
  224. *
  225. * Resume the devices that have either not gone through
  226. * the late suspend, or that did go through it but also
  227. * went through the early resume.
  228. *
  229. * Take devices from the dpm_off_list, resume them,
  230. * and put them on the dpm_locked list.
  231. */
  232. static void dpm_resume(void)
  233. {
  234. mutex_lock(&dpm_list_mtx);
  235. while(!list_empty(&dpm_off)) {
  236. struct list_head *entry = dpm_off.next;
  237. struct device *dev = to_device(entry);
  238. list_move_tail(entry, &dpm_locked);
  239. mutex_unlock(&dpm_list_mtx);
  240. resume_device(dev);
  241. mutex_lock(&dpm_list_mtx);
  242. }
  243. mutex_unlock(&dpm_list_mtx);
  244. }
  245. /**
  246. * unlock_all_devices - Release each device's semaphore
  247. *
  248. * Go through the dpm_off list. Put each device on the dpm_active
  249. * list and unlock it.
  250. */
  251. static void unlock_all_devices(void)
  252. {
  253. mutex_lock(&dpm_list_mtx);
  254. while (!list_empty(&dpm_locked)) {
  255. struct list_head *entry = dpm_locked.prev;
  256. struct device *dev = to_device(entry);
  257. list_move(entry, &dpm_active);
  258. up(&dev->sem);
  259. }
  260. mutex_unlock(&dpm_list_mtx);
  261. }
  262. /**
  263. * unregister_dropped_devices - Unregister devices scheduled for removal
  264. *
  265. * Unregister all devices on the dpm_destroy list.
  266. */
  267. static void unregister_dropped_devices(void)
  268. {
  269. mutex_lock(&dpm_list_mtx);
  270. while (!list_empty(&dpm_destroy)) {
  271. struct list_head *entry = dpm_destroy.next;
  272. struct device *dev = to_device(entry);
  273. up(&dev->sem);
  274. mutex_unlock(&dpm_list_mtx);
  275. /* This also removes the device from the list */
  276. device_unregister(dev);
  277. mutex_lock(&dpm_list_mtx);
  278. }
  279. mutex_unlock(&dpm_list_mtx);
  280. }
  281. /**
  282. * device_resume - Restore state of each device in system.
  283. *
  284. * Resume all the devices, unlock them all, and allow new
  285. * devices to be registered once again.
  286. */
  287. void device_resume(void)
  288. {
  289. might_sleep();
  290. dpm_resume();
  291. unlock_all_devices();
  292. unregister_dropped_devices();
  293. up_write(&pm_sleep_rwsem);
  294. }
  295. EXPORT_SYMBOL_GPL(device_resume);
  296. /*------------------------- Suspend routines -------------------------*/
  297. static inline char *suspend_verb(u32 event)
  298. {
  299. switch (event) {
  300. case PM_EVENT_SUSPEND: return "suspend";
  301. case PM_EVENT_FREEZE: return "freeze";
  302. case PM_EVENT_PRETHAW: return "prethaw";
  303. default: return "(unknown suspend event)";
  304. }
  305. }
  306. static void
  307. suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
  308. {
  309. dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
  310. ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
  311. ", may wakeup" : "");
  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. if (dev->bus && dev->bus->suspend_late) {
  324. suspend_device_dbg(dev, state, "LATE ");
  325. error = dev->bus->suspend_late(dev, state);
  326. suspend_report_result(dev->bus->suspend_late, error);
  327. }
  328. return error;
  329. }
  330. /**
  331. * device_power_down - Shut down special devices.
  332. * @state: Power state to enter.
  333. *
  334. * Power down devices that require interrupts to be disabled
  335. * and move them from the dpm_off list to the dpm_off_irq list.
  336. * Then power down system devices.
  337. *
  338. * Must be called with interrupts disabled and only one CPU running.
  339. */
  340. int device_power_down(pm_message_t state)
  341. {
  342. int error = 0;
  343. while (!list_empty(&dpm_off)) {
  344. struct list_head *entry = dpm_off.prev;
  345. struct device *dev = to_device(entry);
  346. list_del_init(&dev->power.entry);
  347. error = suspend_device_late(dev, state);
  348. if (error) {
  349. printk(KERN_ERR "Could not power down device %s: "
  350. "error %d\n",
  351. kobject_name(&dev->kobj), error);
  352. if (list_empty(&dev->power.entry))
  353. list_add(&dev->power.entry, &dpm_off);
  354. break;
  355. }
  356. if (list_empty(&dev->power.entry))
  357. list_add(&dev->power.entry, &dpm_off_irq);
  358. }
  359. if (!error)
  360. error = sysdev_suspend(state);
  361. if (error)
  362. dpm_power_up();
  363. return error;
  364. }
  365. EXPORT_SYMBOL_GPL(device_power_down);
  366. /**
  367. * suspend_device - Save state of one device.
  368. * @dev: Device.
  369. * @state: Power state device is entering.
  370. */
  371. static int suspend_device(struct device *dev, pm_message_t state)
  372. {
  373. int error = 0;
  374. if (dev->power.power_state.event) {
  375. dev_dbg(dev, "PM: suspend %d-->%d\n",
  376. dev->power.power_state.event, state.event);
  377. }
  378. if (dev->class && dev->class->suspend) {
  379. suspend_device_dbg(dev, state, "class ");
  380. error = dev->class->suspend(dev, state);
  381. suspend_report_result(dev->class->suspend, error);
  382. }
  383. if (!error && dev->type && dev->type->suspend) {
  384. suspend_device_dbg(dev, state, "type ");
  385. error = dev->type->suspend(dev, state);
  386. suspend_report_result(dev->type->suspend, error);
  387. }
  388. if (!error && dev->bus && dev->bus->suspend) {
  389. suspend_device_dbg(dev, state, "");
  390. error = dev->bus->suspend(dev, state);
  391. suspend_report_result(dev->bus->suspend, error);
  392. }
  393. return error;
  394. }
  395. /**
  396. * dpm_suspend - Suspend every device.
  397. * @state: Power state to put each device in.
  398. *
  399. * Walk the dpm_locked list. Suspend each device and move it
  400. * to the dpm_off list.
  401. *
  402. * (For historical reasons, if it returns -EAGAIN, that used to mean
  403. * that the device would be called again with interrupts disabled.
  404. * These days, we use the "suspend_late()" callback for that, so we
  405. * print a warning and consider it an error).
  406. */
  407. static int dpm_suspend(pm_message_t state)
  408. {
  409. int error = 0;
  410. mutex_lock(&dpm_list_mtx);
  411. while (!list_empty(&dpm_locked)) {
  412. struct list_head *entry = dpm_locked.prev;
  413. struct device *dev = to_device(entry);
  414. list_del_init(&dev->power.entry);
  415. mutex_unlock(&dpm_list_mtx);
  416. error = suspend_device(dev, state);
  417. if (error) {
  418. printk(KERN_ERR "Could not suspend device %s: "
  419. "error %d%s\n",
  420. kobject_name(&dev->kobj),
  421. error,
  422. (error == -EAGAIN ?
  423. " (please convert to suspend_late)" :
  424. ""));
  425. mutex_lock(&dpm_list_mtx);
  426. if (list_empty(&dev->power.entry))
  427. list_add(&dev->power.entry, &dpm_locked);
  428. mutex_unlock(&dpm_list_mtx);
  429. break;
  430. }
  431. mutex_lock(&dpm_list_mtx);
  432. if (list_empty(&dev->power.entry))
  433. list_add(&dev->power.entry, &dpm_off);
  434. }
  435. mutex_unlock(&dpm_list_mtx);
  436. return error;
  437. }
  438. /**
  439. * lock_all_devices - Acquire every device's semaphore
  440. *
  441. * Go through the dpm_active list. Carefully lock each device's
  442. * semaphore and put it in on the dpm_locked list.
  443. */
  444. static void lock_all_devices(void)
  445. {
  446. mutex_lock(&dpm_list_mtx);
  447. while (!list_empty(&dpm_active)) {
  448. struct list_head *entry = dpm_active.next;
  449. struct device *dev = to_device(entry);
  450. /* Required locking order is dev->sem first,
  451. * then dpm_list_mutex. Hence this awkward code.
  452. */
  453. get_device(dev);
  454. mutex_unlock(&dpm_list_mtx);
  455. down(&dev->sem);
  456. mutex_lock(&dpm_list_mtx);
  457. if (list_empty(entry))
  458. up(&dev->sem); /* Device was removed */
  459. else
  460. list_move_tail(entry, &dpm_locked);
  461. put_device(dev);
  462. }
  463. mutex_unlock(&dpm_list_mtx);
  464. }
  465. /**
  466. * device_suspend - Save state and stop all devices in system.
  467. *
  468. * Prevent new devices from being registered, then lock all devices
  469. * and suspend them.
  470. */
  471. int device_suspend(pm_message_t state)
  472. {
  473. int error;
  474. might_sleep();
  475. down_write(&pm_sleep_rwsem);
  476. lock_all_devices();
  477. error = dpm_suspend(state);
  478. if (error)
  479. device_resume();
  480. return error;
  481. }
  482. EXPORT_SYMBOL_GPL(device_suspend);
  483. void __suspend_report_result(const char *function, void *fn, int ret)
  484. {
  485. if (ret) {
  486. printk(KERN_ERR "%s(): ", function);
  487. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  488. printk("%d\n", ret);
  489. }
  490. }
  491. EXPORT_SYMBOL_GPL(__suspend_report_result);