main.c 27 KB

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