main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 initialize 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 separate list is used for keeping track of power info, because the power
  16. * domain dependencies may differ from the ancestral dependencies that the
  17. * subsystem list maintains.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/resume-trace.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sched.h>
  27. #include <linux/async.h>
  28. #include <linux/suspend.h>
  29. #include "../base.h"
  30. #include "power.h"
  31. /*
  32. * The entries in the dpm_list list are in a depth first order, simply
  33. * because children are guaranteed to be discovered after parents, and
  34. * are inserted at the back of the list on discovery.
  35. *
  36. * Since device_pm_add() may be called with a device lock held,
  37. * we must never try to acquire a device lock while holding
  38. * dpm_list_mutex.
  39. */
  40. LIST_HEAD(dpm_list);
  41. LIST_HEAD(dpm_prepared_list);
  42. LIST_HEAD(dpm_suspended_list);
  43. LIST_HEAD(dpm_noirq_list);
  44. static DEFINE_MUTEX(dpm_list_mtx);
  45. static pm_message_t pm_transition;
  46. static int async_error;
  47. /**
  48. * device_pm_init - Initialize the PM-related part of a device object.
  49. * @dev: Device object being initialized.
  50. */
  51. void device_pm_init(struct device *dev)
  52. {
  53. dev->power.in_suspend = false;
  54. init_completion(&dev->power.completion);
  55. complete_all(&dev->power.completion);
  56. dev->power.wakeup = NULL;
  57. spin_lock_init(&dev->power.lock);
  58. pm_runtime_init(dev);
  59. }
  60. /**
  61. * device_pm_lock - Lock the list of active devices used by the PM core.
  62. */
  63. void device_pm_lock(void)
  64. {
  65. mutex_lock(&dpm_list_mtx);
  66. }
  67. /**
  68. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  69. */
  70. void device_pm_unlock(void)
  71. {
  72. mutex_unlock(&dpm_list_mtx);
  73. }
  74. /**
  75. * device_pm_add - Add a device to the PM core's list of active devices.
  76. * @dev: Device to add to the list.
  77. */
  78. void device_pm_add(struct device *dev)
  79. {
  80. pr_debug("PM: Adding info for %s:%s\n",
  81. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  82. mutex_lock(&dpm_list_mtx);
  83. if (dev->parent && dev->parent->power.in_suspend)
  84. dev_warn(dev, "parent %s should not be sleeping\n",
  85. dev_name(dev->parent));
  86. list_add_tail(&dev->power.entry, &dpm_list);
  87. mutex_unlock(&dpm_list_mtx);
  88. }
  89. /**
  90. * device_pm_remove - Remove a device from the PM core's list of active devices.
  91. * @dev: Device to be removed from the list.
  92. */
  93. void device_pm_remove(struct device *dev)
  94. {
  95. pr_debug("PM: Removing info for %s:%s\n",
  96. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  97. complete_all(&dev->power.completion);
  98. mutex_lock(&dpm_list_mtx);
  99. list_del_init(&dev->power.entry);
  100. mutex_unlock(&dpm_list_mtx);
  101. device_wakeup_disable(dev);
  102. pm_runtime_remove(dev);
  103. }
  104. /**
  105. * device_pm_move_before - Move device in the PM core's list of active devices.
  106. * @deva: Device to move in dpm_list.
  107. * @devb: Device @deva should come before.
  108. */
  109. void device_pm_move_before(struct device *deva, struct device *devb)
  110. {
  111. pr_debug("PM: Moving %s:%s before %s:%s\n",
  112. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  113. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  114. /* Delete deva from dpm_list and reinsert before devb. */
  115. list_move_tail(&deva->power.entry, &devb->power.entry);
  116. }
  117. /**
  118. * device_pm_move_after - Move device in the PM core's list of active devices.
  119. * @deva: Device to move in dpm_list.
  120. * @devb: Device @deva should come after.
  121. */
  122. void device_pm_move_after(struct device *deva, struct device *devb)
  123. {
  124. pr_debug("PM: Moving %s:%s after %s:%s\n",
  125. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  126. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  127. /* Delete deva from dpm_list and reinsert after devb. */
  128. list_move(&deva->power.entry, &devb->power.entry);
  129. }
  130. /**
  131. * device_pm_move_last - Move device to end of the PM core's list of devices.
  132. * @dev: Device to move in dpm_list.
  133. */
  134. void device_pm_move_last(struct device *dev)
  135. {
  136. pr_debug("PM: Moving %s:%s to end of list\n",
  137. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  138. list_move_tail(&dev->power.entry, &dpm_list);
  139. }
  140. static ktime_t initcall_debug_start(struct device *dev)
  141. {
  142. ktime_t calltime = ktime_set(0, 0);
  143. if (initcall_debug) {
  144. pr_info("calling %s+ @ %i\n",
  145. dev_name(dev), task_pid_nr(current));
  146. calltime = ktime_get();
  147. }
  148. return calltime;
  149. }
  150. static void initcall_debug_report(struct device *dev, ktime_t calltime,
  151. int error)
  152. {
  153. ktime_t delta, rettime;
  154. if (initcall_debug) {
  155. rettime = ktime_get();
  156. delta = ktime_sub(rettime, calltime);
  157. pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
  158. error, (unsigned long long)ktime_to_ns(delta) >> 10);
  159. }
  160. }
  161. /**
  162. * dpm_wait - Wait for a PM operation to complete.
  163. * @dev: Device to wait for.
  164. * @async: If unset, wait only if the device's power.async_suspend flag is set.
  165. */
  166. static void dpm_wait(struct device *dev, bool async)
  167. {
  168. if (!dev)
  169. return;
  170. if (async || (pm_async_enabled && dev->power.async_suspend))
  171. wait_for_completion(&dev->power.completion);
  172. }
  173. static int dpm_wait_fn(struct device *dev, void *async_ptr)
  174. {
  175. dpm_wait(dev, *((bool *)async_ptr));
  176. return 0;
  177. }
  178. static void dpm_wait_for_children(struct device *dev, bool async)
  179. {
  180. device_for_each_child(dev, &async, dpm_wait_fn);
  181. }
  182. /**
  183. * pm_op - Execute the PM operation appropriate for given PM event.
  184. * @dev: Device to handle.
  185. * @ops: PM operations to choose from.
  186. * @state: PM transition of the system being carried out.
  187. */
  188. static int pm_op(struct device *dev,
  189. const struct dev_pm_ops *ops,
  190. pm_message_t state)
  191. {
  192. int error = 0;
  193. ktime_t calltime;
  194. calltime = initcall_debug_start(dev);
  195. switch (state.event) {
  196. #ifdef CONFIG_SUSPEND
  197. case PM_EVENT_SUSPEND:
  198. if (ops->suspend) {
  199. error = ops->suspend(dev);
  200. suspend_report_result(ops->suspend, error);
  201. }
  202. break;
  203. case PM_EVENT_RESUME:
  204. if (ops->resume) {
  205. error = ops->resume(dev);
  206. suspend_report_result(ops->resume, error);
  207. }
  208. break;
  209. #endif /* CONFIG_SUSPEND */
  210. #ifdef CONFIG_HIBERNATION
  211. case PM_EVENT_FREEZE:
  212. case PM_EVENT_QUIESCE:
  213. if (ops->freeze) {
  214. error = ops->freeze(dev);
  215. suspend_report_result(ops->freeze, error);
  216. }
  217. break;
  218. case PM_EVENT_HIBERNATE:
  219. if (ops->poweroff) {
  220. error = ops->poweroff(dev);
  221. suspend_report_result(ops->poweroff, error);
  222. }
  223. break;
  224. case PM_EVENT_THAW:
  225. case PM_EVENT_RECOVER:
  226. if (ops->thaw) {
  227. error = ops->thaw(dev);
  228. suspend_report_result(ops->thaw, error);
  229. }
  230. break;
  231. case PM_EVENT_RESTORE:
  232. if (ops->restore) {
  233. error = ops->restore(dev);
  234. suspend_report_result(ops->restore, error);
  235. }
  236. break;
  237. #endif /* CONFIG_HIBERNATION */
  238. default:
  239. error = -EINVAL;
  240. }
  241. initcall_debug_report(dev, calltime, error);
  242. return error;
  243. }
  244. /**
  245. * pm_noirq_op - Execute the PM operation appropriate for given PM event.
  246. * @dev: Device to handle.
  247. * @ops: PM operations to choose from.
  248. * @state: PM transition of the system being carried out.
  249. *
  250. * The driver of @dev will not receive interrupts while this function is being
  251. * executed.
  252. */
  253. static int pm_noirq_op(struct device *dev,
  254. const struct dev_pm_ops *ops,
  255. pm_message_t state)
  256. {
  257. int error = 0;
  258. ktime_t calltime = ktime_set(0, 0), delta, rettime;
  259. if (initcall_debug) {
  260. pr_info("calling %s+ @ %i, parent: %s\n",
  261. dev_name(dev), task_pid_nr(current),
  262. dev->parent ? dev_name(dev->parent) : "none");
  263. calltime = ktime_get();
  264. }
  265. switch (state.event) {
  266. #ifdef CONFIG_SUSPEND
  267. case PM_EVENT_SUSPEND:
  268. if (ops->suspend_noirq) {
  269. error = ops->suspend_noirq(dev);
  270. suspend_report_result(ops->suspend_noirq, error);
  271. }
  272. break;
  273. case PM_EVENT_RESUME:
  274. if (ops->resume_noirq) {
  275. error = ops->resume_noirq(dev);
  276. suspend_report_result(ops->resume_noirq, error);
  277. }
  278. break;
  279. #endif /* CONFIG_SUSPEND */
  280. #ifdef CONFIG_HIBERNATION
  281. case PM_EVENT_FREEZE:
  282. case PM_EVENT_QUIESCE:
  283. if (ops->freeze_noirq) {
  284. error = ops->freeze_noirq(dev);
  285. suspend_report_result(ops->freeze_noirq, error);
  286. }
  287. break;
  288. case PM_EVENT_HIBERNATE:
  289. if (ops->poweroff_noirq) {
  290. error = ops->poweroff_noirq(dev);
  291. suspend_report_result(ops->poweroff_noirq, error);
  292. }
  293. break;
  294. case PM_EVENT_THAW:
  295. case PM_EVENT_RECOVER:
  296. if (ops->thaw_noirq) {
  297. error = ops->thaw_noirq(dev);
  298. suspend_report_result(ops->thaw_noirq, error);
  299. }
  300. break;
  301. case PM_EVENT_RESTORE:
  302. if (ops->restore_noirq) {
  303. error = ops->restore_noirq(dev);
  304. suspend_report_result(ops->restore_noirq, error);
  305. }
  306. break;
  307. #endif /* CONFIG_HIBERNATION */
  308. default:
  309. error = -EINVAL;
  310. }
  311. if (initcall_debug) {
  312. rettime = ktime_get();
  313. delta = ktime_sub(rettime, calltime);
  314. printk("initcall %s_i+ returned %d after %Ld usecs\n",
  315. dev_name(dev), error,
  316. (unsigned long long)ktime_to_ns(delta) >> 10);
  317. }
  318. return error;
  319. }
  320. static char *pm_verb(int event)
  321. {
  322. switch (event) {
  323. case PM_EVENT_SUSPEND:
  324. return "suspend";
  325. case PM_EVENT_RESUME:
  326. return "resume";
  327. case PM_EVENT_FREEZE:
  328. return "freeze";
  329. case PM_EVENT_QUIESCE:
  330. return "quiesce";
  331. case PM_EVENT_HIBERNATE:
  332. return "hibernate";
  333. case PM_EVENT_THAW:
  334. return "thaw";
  335. case PM_EVENT_RESTORE:
  336. return "restore";
  337. case PM_EVENT_RECOVER:
  338. return "recover";
  339. default:
  340. return "(unknown PM event)";
  341. }
  342. }
  343. static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
  344. {
  345. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  346. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  347. ", may wakeup" : "");
  348. }
  349. static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
  350. int error)
  351. {
  352. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  353. dev_name(dev), pm_verb(state.event), info, error);
  354. }
  355. static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
  356. {
  357. ktime_t calltime;
  358. u64 usecs64;
  359. int usecs;
  360. calltime = ktime_get();
  361. usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
  362. do_div(usecs64, NSEC_PER_USEC);
  363. usecs = usecs64;
  364. if (usecs == 0)
  365. usecs = 1;
  366. pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
  367. info ?: "", info ? " " : "", pm_verb(state.event),
  368. usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
  369. }
  370. /*------------------------- Resume routines -------------------------*/
  371. /**
  372. * device_resume_noirq - Execute an "early resume" callback for given device.
  373. * @dev: Device to handle.
  374. * @state: PM transition of the system being carried out.
  375. *
  376. * The driver of @dev will not receive interrupts while this function is being
  377. * executed.
  378. */
  379. static int device_resume_noirq(struct device *dev, pm_message_t state)
  380. {
  381. int error = 0;
  382. TRACE_DEVICE(dev);
  383. TRACE_RESUME(0);
  384. if (dev->bus && dev->bus->pm) {
  385. pm_dev_dbg(dev, state, "EARLY ");
  386. error = pm_noirq_op(dev, dev->bus->pm, state);
  387. if (error)
  388. goto End;
  389. }
  390. if (dev->type && dev->type->pm) {
  391. pm_dev_dbg(dev, state, "EARLY type ");
  392. error = pm_noirq_op(dev, dev->type->pm, state);
  393. if (error)
  394. goto End;
  395. }
  396. if (dev->class && dev->class->pm) {
  397. pm_dev_dbg(dev, state, "EARLY class ");
  398. error = pm_noirq_op(dev, dev->class->pm, state);
  399. }
  400. End:
  401. TRACE_RESUME(error);
  402. return error;
  403. }
  404. /**
  405. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  406. * @state: PM transition of the system being carried out.
  407. *
  408. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  409. * enable device drivers to receive interrupts.
  410. */
  411. void dpm_resume_noirq(pm_message_t state)
  412. {
  413. ktime_t starttime = ktime_get();
  414. mutex_lock(&dpm_list_mtx);
  415. while (!list_empty(&dpm_noirq_list)) {
  416. struct device *dev = to_device(dpm_noirq_list.next);
  417. int error;
  418. get_device(dev);
  419. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  420. mutex_unlock(&dpm_list_mtx);
  421. error = device_resume_noirq(dev, state);
  422. if (error)
  423. pm_dev_err(dev, state, " early", error);
  424. mutex_lock(&dpm_list_mtx);
  425. put_device(dev);
  426. }
  427. mutex_unlock(&dpm_list_mtx);
  428. dpm_show_time(starttime, state, "early");
  429. resume_device_irqs();
  430. }
  431. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  432. /**
  433. * legacy_resume - Execute a legacy (bus or class) resume callback for device.
  434. * @dev: Device to resume.
  435. * @cb: Resume callback to execute.
  436. */
  437. static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
  438. {
  439. int error;
  440. ktime_t calltime;
  441. calltime = initcall_debug_start(dev);
  442. error = cb(dev);
  443. suspend_report_result(cb, error);
  444. initcall_debug_report(dev, calltime, error);
  445. return error;
  446. }
  447. /**
  448. * device_resume - Execute "resume" callbacks for given device.
  449. * @dev: Device to handle.
  450. * @state: PM transition of the system being carried out.
  451. * @async: If true, the device is being resumed asynchronously.
  452. */
  453. static int device_resume(struct device *dev, pm_message_t state, bool async)
  454. {
  455. int error = 0;
  456. TRACE_DEVICE(dev);
  457. TRACE_RESUME(0);
  458. dpm_wait(dev->parent, async);
  459. device_lock(dev);
  460. dev->power.in_suspend = false;
  461. if (dev->bus) {
  462. if (dev->bus->pm) {
  463. pm_dev_dbg(dev, state, "");
  464. error = pm_op(dev, dev->bus->pm, state);
  465. } else if (dev->bus->resume) {
  466. pm_dev_dbg(dev, state, "legacy ");
  467. error = legacy_resume(dev, dev->bus->resume);
  468. }
  469. if (error)
  470. goto End;
  471. }
  472. if (dev->type) {
  473. if (dev->type->pm) {
  474. pm_dev_dbg(dev, state, "type ");
  475. error = pm_op(dev, dev->type->pm, state);
  476. }
  477. if (error)
  478. goto End;
  479. }
  480. if (dev->class) {
  481. if (dev->class->pm) {
  482. pm_dev_dbg(dev, state, "class ");
  483. error = pm_op(dev, dev->class->pm, state);
  484. } else if (dev->class->resume) {
  485. pm_dev_dbg(dev, state, "legacy class ");
  486. error = legacy_resume(dev, dev->class->resume);
  487. }
  488. }
  489. End:
  490. device_unlock(dev);
  491. complete_all(&dev->power.completion);
  492. TRACE_RESUME(error);
  493. return error;
  494. }
  495. static void async_resume(void *data, async_cookie_t cookie)
  496. {
  497. struct device *dev = (struct device *)data;
  498. int error;
  499. error = device_resume(dev, pm_transition, true);
  500. if (error)
  501. pm_dev_err(dev, pm_transition, " async", error);
  502. put_device(dev);
  503. }
  504. static bool is_async(struct device *dev)
  505. {
  506. return dev->power.async_suspend && pm_async_enabled
  507. && !pm_trace_is_enabled();
  508. }
  509. /**
  510. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  511. * @state: PM transition of the system being carried out.
  512. *
  513. * Execute the appropriate "resume" callback for all devices whose status
  514. * indicates that they are suspended.
  515. */
  516. static void dpm_resume(pm_message_t state)
  517. {
  518. struct device *dev;
  519. ktime_t starttime = ktime_get();
  520. mutex_lock(&dpm_list_mtx);
  521. pm_transition = state;
  522. async_error = 0;
  523. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  524. INIT_COMPLETION(dev->power.completion);
  525. if (is_async(dev)) {
  526. get_device(dev);
  527. async_schedule(async_resume, dev);
  528. }
  529. }
  530. while (!list_empty(&dpm_suspended_list)) {
  531. dev = to_device(dpm_suspended_list.next);
  532. get_device(dev);
  533. if (!is_async(dev)) {
  534. int error;
  535. mutex_unlock(&dpm_list_mtx);
  536. error = device_resume(dev, state, false);
  537. if (error)
  538. pm_dev_err(dev, state, "", error);
  539. mutex_lock(&dpm_list_mtx);
  540. }
  541. if (!list_empty(&dev->power.entry))
  542. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  543. put_device(dev);
  544. }
  545. mutex_unlock(&dpm_list_mtx);
  546. async_synchronize_full();
  547. dpm_show_time(starttime, state, NULL);
  548. }
  549. /**
  550. * device_complete - Complete a PM transition for given device.
  551. * @dev: Device to handle.
  552. * @state: PM transition of the system being carried out.
  553. */
  554. static void device_complete(struct device *dev, pm_message_t state)
  555. {
  556. device_lock(dev);
  557. if (dev->class && dev->class->pm && dev->class->pm->complete) {
  558. pm_dev_dbg(dev, state, "completing class ");
  559. dev->class->pm->complete(dev);
  560. }
  561. if (dev->type && dev->type->pm && dev->type->pm->complete) {
  562. pm_dev_dbg(dev, state, "completing type ");
  563. dev->type->pm->complete(dev);
  564. }
  565. if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
  566. pm_dev_dbg(dev, state, "completing ");
  567. dev->bus->pm->complete(dev);
  568. }
  569. device_unlock(dev);
  570. }
  571. /**
  572. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  573. * @state: PM transition of the system being carried out.
  574. *
  575. * Execute the ->complete() callbacks for all devices whose PM status is not
  576. * DPM_ON (this allows new devices to be registered).
  577. */
  578. static void dpm_complete(pm_message_t state)
  579. {
  580. struct list_head list;
  581. INIT_LIST_HEAD(&list);
  582. mutex_lock(&dpm_list_mtx);
  583. while (!list_empty(&dpm_prepared_list)) {
  584. struct device *dev = to_device(dpm_prepared_list.prev);
  585. get_device(dev);
  586. dev->power.in_suspend = false;
  587. list_move(&dev->power.entry, &list);
  588. mutex_unlock(&dpm_list_mtx);
  589. device_complete(dev, state);
  590. mutex_lock(&dpm_list_mtx);
  591. put_device(dev);
  592. }
  593. list_splice(&list, &dpm_list);
  594. mutex_unlock(&dpm_list_mtx);
  595. }
  596. /**
  597. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  598. * @state: PM transition of the system being carried out.
  599. *
  600. * Execute "resume" callbacks for all devices and complete the PM transition of
  601. * the system.
  602. */
  603. void dpm_resume_end(pm_message_t state)
  604. {
  605. might_sleep();
  606. dpm_resume(state);
  607. dpm_complete(state);
  608. }
  609. EXPORT_SYMBOL_GPL(dpm_resume_end);
  610. /*------------------------- Suspend routines -------------------------*/
  611. /**
  612. * resume_event - Return a "resume" message for given "suspend" sleep state.
  613. * @sleep_state: PM message representing a sleep state.
  614. *
  615. * Return a PM message representing the resume event corresponding to given
  616. * sleep state.
  617. */
  618. static pm_message_t resume_event(pm_message_t sleep_state)
  619. {
  620. switch (sleep_state.event) {
  621. case PM_EVENT_SUSPEND:
  622. return PMSG_RESUME;
  623. case PM_EVENT_FREEZE:
  624. case PM_EVENT_QUIESCE:
  625. return PMSG_RECOVER;
  626. case PM_EVENT_HIBERNATE:
  627. return PMSG_RESTORE;
  628. }
  629. return PMSG_ON;
  630. }
  631. /**
  632. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  633. * @dev: Device to handle.
  634. * @state: PM transition of the system being carried out.
  635. *
  636. * The driver of @dev will not receive interrupts while this function is being
  637. * executed.
  638. */
  639. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  640. {
  641. int error = 0;
  642. if (dev->class && dev->class->pm) {
  643. pm_dev_dbg(dev, state, "LATE class ");
  644. error = pm_noirq_op(dev, dev->class->pm, state);
  645. if (error)
  646. goto End;
  647. }
  648. if (dev->type && dev->type->pm) {
  649. pm_dev_dbg(dev, state, "LATE type ");
  650. error = pm_noirq_op(dev, dev->type->pm, state);
  651. if (error)
  652. goto End;
  653. }
  654. if (dev->bus && dev->bus->pm) {
  655. pm_dev_dbg(dev, state, "LATE ");
  656. error = pm_noirq_op(dev, dev->bus->pm, state);
  657. }
  658. End:
  659. return error;
  660. }
  661. /**
  662. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  663. * @state: PM transition of the system being carried out.
  664. *
  665. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  666. * handlers for all non-sysdev devices.
  667. */
  668. int dpm_suspend_noirq(pm_message_t state)
  669. {
  670. ktime_t starttime = ktime_get();
  671. int error = 0;
  672. suspend_device_irqs();
  673. mutex_lock(&dpm_list_mtx);
  674. while (!list_empty(&dpm_suspended_list)) {
  675. struct device *dev = to_device(dpm_suspended_list.prev);
  676. get_device(dev);
  677. mutex_unlock(&dpm_list_mtx);
  678. error = device_suspend_noirq(dev, state);
  679. mutex_lock(&dpm_list_mtx);
  680. if (error) {
  681. pm_dev_err(dev, state, " late", error);
  682. put_device(dev);
  683. break;
  684. }
  685. if (!list_empty(&dev->power.entry))
  686. list_move(&dev->power.entry, &dpm_noirq_list);
  687. put_device(dev);
  688. }
  689. mutex_unlock(&dpm_list_mtx);
  690. if (error)
  691. dpm_resume_noirq(resume_event(state));
  692. else
  693. dpm_show_time(starttime, state, "late");
  694. return error;
  695. }
  696. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  697. /**
  698. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  699. * @dev: Device to suspend.
  700. * @state: PM transition of the system being carried out.
  701. * @cb: Suspend callback to execute.
  702. */
  703. static int legacy_suspend(struct device *dev, pm_message_t state,
  704. int (*cb)(struct device *dev, pm_message_t state))
  705. {
  706. int error;
  707. ktime_t calltime;
  708. calltime = initcall_debug_start(dev);
  709. error = cb(dev, state);
  710. suspend_report_result(cb, error);
  711. initcall_debug_report(dev, calltime, error);
  712. return error;
  713. }
  714. /**
  715. * device_suspend - Execute "suspend" callbacks for given device.
  716. * @dev: Device to handle.
  717. * @state: PM transition of the system being carried out.
  718. * @async: If true, the device is being suspended asynchronously.
  719. */
  720. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  721. {
  722. int error = 0;
  723. dpm_wait_for_children(dev, async);
  724. device_lock(dev);
  725. if (async_error)
  726. goto End;
  727. if (pm_wakeup_pending()) {
  728. async_error = -EBUSY;
  729. goto End;
  730. }
  731. if (dev->class) {
  732. if (dev->class->pm) {
  733. pm_dev_dbg(dev, state, "class ");
  734. error = pm_op(dev, dev->class->pm, state);
  735. } else if (dev->class->suspend) {
  736. pm_dev_dbg(dev, state, "legacy class ");
  737. error = legacy_suspend(dev, state, dev->class->suspend);
  738. }
  739. if (error)
  740. goto End;
  741. }
  742. if (dev->type) {
  743. if (dev->type->pm) {
  744. pm_dev_dbg(dev, state, "type ");
  745. error = pm_op(dev, dev->type->pm, state);
  746. }
  747. if (error)
  748. goto End;
  749. }
  750. if (dev->bus) {
  751. if (dev->bus->pm) {
  752. pm_dev_dbg(dev, state, "");
  753. error = pm_op(dev, dev->bus->pm, state);
  754. } else if (dev->bus->suspend) {
  755. pm_dev_dbg(dev, state, "legacy ");
  756. error = legacy_suspend(dev, state, dev->bus->suspend);
  757. }
  758. }
  759. End:
  760. device_unlock(dev);
  761. complete_all(&dev->power.completion);
  762. if (error)
  763. async_error = error;
  764. return error;
  765. }
  766. static void async_suspend(void *data, async_cookie_t cookie)
  767. {
  768. struct device *dev = (struct device *)data;
  769. int error;
  770. error = __device_suspend(dev, pm_transition, true);
  771. if (error)
  772. pm_dev_err(dev, pm_transition, " async", error);
  773. put_device(dev);
  774. }
  775. static int device_suspend(struct device *dev)
  776. {
  777. INIT_COMPLETION(dev->power.completion);
  778. if (pm_async_enabled && dev->power.async_suspend) {
  779. get_device(dev);
  780. async_schedule(async_suspend, dev);
  781. return 0;
  782. }
  783. return __device_suspend(dev, pm_transition, false);
  784. }
  785. /**
  786. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  787. * @state: PM transition of the system being carried out.
  788. */
  789. static int dpm_suspend(pm_message_t state)
  790. {
  791. ktime_t starttime = ktime_get();
  792. int error = 0;
  793. mutex_lock(&dpm_list_mtx);
  794. pm_transition = state;
  795. async_error = 0;
  796. while (!list_empty(&dpm_prepared_list)) {
  797. struct device *dev = to_device(dpm_prepared_list.prev);
  798. get_device(dev);
  799. mutex_unlock(&dpm_list_mtx);
  800. error = device_suspend(dev);
  801. mutex_lock(&dpm_list_mtx);
  802. if (error) {
  803. pm_dev_err(dev, state, "", error);
  804. put_device(dev);
  805. break;
  806. }
  807. if (!list_empty(&dev->power.entry))
  808. list_move(&dev->power.entry, &dpm_suspended_list);
  809. put_device(dev);
  810. if (async_error)
  811. break;
  812. }
  813. mutex_unlock(&dpm_list_mtx);
  814. async_synchronize_full();
  815. if (!error)
  816. error = async_error;
  817. if (!error)
  818. dpm_show_time(starttime, state, NULL);
  819. return error;
  820. }
  821. /**
  822. * device_prepare - Prepare a device for system power transition.
  823. * @dev: Device to handle.
  824. * @state: PM transition of the system being carried out.
  825. *
  826. * Execute the ->prepare() callback(s) for given device. No new children of the
  827. * device may be registered after this function has returned.
  828. */
  829. static int device_prepare(struct device *dev, pm_message_t state)
  830. {
  831. int error = 0;
  832. device_lock(dev);
  833. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  834. pm_dev_dbg(dev, state, "preparing ");
  835. error = dev->bus->pm->prepare(dev);
  836. suspend_report_result(dev->bus->pm->prepare, error);
  837. if (error)
  838. goto End;
  839. }
  840. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  841. pm_dev_dbg(dev, state, "preparing type ");
  842. error = dev->type->pm->prepare(dev);
  843. suspend_report_result(dev->type->pm->prepare, error);
  844. if (error)
  845. goto End;
  846. }
  847. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  848. pm_dev_dbg(dev, state, "preparing class ");
  849. error = dev->class->pm->prepare(dev);
  850. suspend_report_result(dev->class->pm->prepare, error);
  851. }
  852. End:
  853. device_unlock(dev);
  854. return error;
  855. }
  856. /**
  857. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  858. * @state: PM transition of the system being carried out.
  859. *
  860. * Execute the ->prepare() callback(s) for all devices.
  861. */
  862. static int dpm_prepare(pm_message_t state)
  863. {
  864. int error = 0;
  865. mutex_lock(&dpm_list_mtx);
  866. while (!list_empty(&dpm_list)) {
  867. struct device *dev = to_device(dpm_list.next);
  868. get_device(dev);
  869. mutex_unlock(&dpm_list_mtx);
  870. pm_runtime_get_noresume(dev);
  871. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  872. pm_wakeup_event(dev, 0);
  873. pm_runtime_put_sync(dev);
  874. error = pm_wakeup_pending() ?
  875. -EBUSY : device_prepare(dev, state);
  876. mutex_lock(&dpm_list_mtx);
  877. if (error) {
  878. if (error == -EAGAIN) {
  879. put_device(dev);
  880. error = 0;
  881. continue;
  882. }
  883. printk(KERN_INFO "PM: Device %s not prepared "
  884. "for power transition: code %d\n",
  885. dev_name(dev), error);
  886. put_device(dev);
  887. break;
  888. }
  889. dev->power.in_suspend = true;
  890. if (!list_empty(&dev->power.entry))
  891. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  892. put_device(dev);
  893. }
  894. mutex_unlock(&dpm_list_mtx);
  895. return error;
  896. }
  897. /**
  898. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  899. * @state: PM transition of the system being carried out.
  900. *
  901. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  902. * callbacks for them.
  903. */
  904. int dpm_suspend_start(pm_message_t state)
  905. {
  906. int error;
  907. might_sleep();
  908. error = dpm_prepare(state);
  909. if (!error)
  910. error = dpm_suspend(state);
  911. return error;
  912. }
  913. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  914. void __suspend_report_result(const char *function, void *fn, int ret)
  915. {
  916. if (ret)
  917. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  918. }
  919. EXPORT_SYMBOL_GPL(__suspend_report_result);
  920. /**
  921. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  922. * @dev: Device to wait for.
  923. * @subordinate: Device that needs to wait for @dev.
  924. */
  925. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  926. {
  927. dpm_wait(dev, subordinate->power.async_suspend);
  928. return async_error;
  929. }
  930. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);