main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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->pwr_domain) {
  385. pm_dev_dbg(dev, state, "EARLY power domain ");
  386. pm_noirq_op(dev, &dev->pwr_domain->ops, state);
  387. }
  388. if (dev->type && dev->type->pm) {
  389. pm_dev_dbg(dev, state, "EARLY type ");
  390. error = pm_noirq_op(dev, dev->type->pm, state);
  391. } else if (dev->class && dev->class->pm) {
  392. pm_dev_dbg(dev, state, "EARLY class ");
  393. error = pm_noirq_op(dev, dev->class->pm, state);
  394. } else if (dev->bus && dev->bus->pm) {
  395. pm_dev_dbg(dev, state, "EARLY ");
  396. error = pm_noirq_op(dev, dev->bus->pm, state);
  397. }
  398. TRACE_RESUME(error);
  399. return error;
  400. }
  401. /**
  402. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  403. * @state: PM transition of the system being carried out.
  404. *
  405. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  406. * enable device drivers to receive interrupts.
  407. */
  408. void dpm_resume_noirq(pm_message_t state)
  409. {
  410. ktime_t starttime = ktime_get();
  411. mutex_lock(&dpm_list_mtx);
  412. while (!list_empty(&dpm_noirq_list)) {
  413. struct device *dev = to_device(dpm_noirq_list.next);
  414. int error;
  415. get_device(dev);
  416. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  417. mutex_unlock(&dpm_list_mtx);
  418. error = device_resume_noirq(dev, state);
  419. if (error)
  420. pm_dev_err(dev, state, " early", error);
  421. mutex_lock(&dpm_list_mtx);
  422. put_device(dev);
  423. }
  424. mutex_unlock(&dpm_list_mtx);
  425. dpm_show_time(starttime, state, "early");
  426. resume_device_irqs();
  427. }
  428. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  429. /**
  430. * legacy_resume - Execute a legacy (bus or class) resume callback for device.
  431. * @dev: Device to resume.
  432. * @cb: Resume callback to execute.
  433. */
  434. static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
  435. {
  436. int error;
  437. ktime_t calltime;
  438. calltime = initcall_debug_start(dev);
  439. error = cb(dev);
  440. suspend_report_result(cb, error);
  441. initcall_debug_report(dev, calltime, error);
  442. return error;
  443. }
  444. /**
  445. * device_resume - Execute "resume" callbacks for given device.
  446. * @dev: Device to handle.
  447. * @state: PM transition of the system being carried out.
  448. * @async: If true, the device is being resumed asynchronously.
  449. */
  450. static int device_resume(struct device *dev, pm_message_t state, bool async)
  451. {
  452. int error = 0;
  453. TRACE_DEVICE(dev);
  454. TRACE_RESUME(0);
  455. dpm_wait(dev->parent, async);
  456. device_lock(dev);
  457. dev->power.in_suspend = false;
  458. if (dev->pwr_domain) {
  459. pm_dev_dbg(dev, state, "power domain ");
  460. pm_op(dev, &dev->pwr_domain->ops, state);
  461. }
  462. if (dev->type && dev->type->pm) {
  463. pm_dev_dbg(dev, state, "type ");
  464. error = pm_op(dev, dev->type->pm, state);
  465. goto End;
  466. }
  467. if (dev->class) {
  468. if (dev->class->pm) {
  469. pm_dev_dbg(dev, state, "class ");
  470. error = pm_op(dev, dev->class->pm, state);
  471. goto End;
  472. } else if (dev->class->resume) {
  473. pm_dev_dbg(dev, state, "legacy class ");
  474. error = legacy_resume(dev, dev->class->resume);
  475. goto End;
  476. }
  477. }
  478. if (dev->bus) {
  479. if (dev->bus->pm) {
  480. pm_dev_dbg(dev, state, "");
  481. error = pm_op(dev, dev->bus->pm, state);
  482. } else if (dev->bus->resume) {
  483. pm_dev_dbg(dev, state, "legacy ");
  484. error = legacy_resume(dev, dev->bus->resume);
  485. }
  486. }
  487. End:
  488. device_unlock(dev);
  489. complete_all(&dev->power.completion);
  490. TRACE_RESUME(error);
  491. return error;
  492. }
  493. static void async_resume(void *data, async_cookie_t cookie)
  494. {
  495. struct device *dev = (struct device *)data;
  496. int error;
  497. error = device_resume(dev, pm_transition, true);
  498. if (error)
  499. pm_dev_err(dev, pm_transition, " async", error);
  500. put_device(dev);
  501. }
  502. static bool is_async(struct device *dev)
  503. {
  504. return dev->power.async_suspend && pm_async_enabled
  505. && !pm_trace_is_enabled();
  506. }
  507. /**
  508. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  509. * @state: PM transition of the system being carried out.
  510. *
  511. * Execute the appropriate "resume" callback for all devices whose status
  512. * indicates that they are suspended.
  513. */
  514. static void dpm_resume(pm_message_t state)
  515. {
  516. struct device *dev;
  517. ktime_t starttime = ktime_get();
  518. mutex_lock(&dpm_list_mtx);
  519. pm_transition = state;
  520. async_error = 0;
  521. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  522. INIT_COMPLETION(dev->power.completion);
  523. if (is_async(dev)) {
  524. get_device(dev);
  525. async_schedule(async_resume, dev);
  526. }
  527. }
  528. while (!list_empty(&dpm_suspended_list)) {
  529. dev = to_device(dpm_suspended_list.next);
  530. get_device(dev);
  531. if (!is_async(dev)) {
  532. int error;
  533. mutex_unlock(&dpm_list_mtx);
  534. error = device_resume(dev, state, false);
  535. if (error)
  536. pm_dev_err(dev, state, "", error);
  537. mutex_lock(&dpm_list_mtx);
  538. }
  539. if (!list_empty(&dev->power.entry))
  540. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  541. put_device(dev);
  542. }
  543. mutex_unlock(&dpm_list_mtx);
  544. async_synchronize_full();
  545. dpm_show_time(starttime, state, NULL);
  546. }
  547. /**
  548. * device_complete - Complete a PM transition for given device.
  549. * @dev: Device to handle.
  550. * @state: PM transition of the system being carried out.
  551. */
  552. static void device_complete(struct device *dev, pm_message_t state)
  553. {
  554. device_lock(dev);
  555. if (dev->pwr_domain && dev->pwr_domain->ops.complete) {
  556. pm_dev_dbg(dev, state, "completing power domain ");
  557. dev->pwr_domain->ops.complete(dev);
  558. }
  559. if (dev->type && dev->type->pm) {
  560. pm_dev_dbg(dev, state, "completing type ");
  561. if (dev->type->pm->complete)
  562. dev->type->pm->complete(dev);
  563. } else if (dev->class && dev->class->pm) {
  564. pm_dev_dbg(dev, state, "completing class ");
  565. if (dev->class->pm->complete)
  566. dev->class->pm->complete(dev);
  567. } else if (dev->bus && dev->bus->pm) {
  568. pm_dev_dbg(dev, state, "completing ");
  569. if (dev->bus->pm->complete)
  570. dev->bus->pm->complete(dev);
  571. }
  572. device_unlock(dev);
  573. }
  574. /**
  575. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  576. * @state: PM transition of the system being carried out.
  577. *
  578. * Execute the ->complete() callbacks for all devices whose PM status is not
  579. * DPM_ON (this allows new devices to be registered).
  580. */
  581. static void dpm_complete(pm_message_t state)
  582. {
  583. struct list_head list;
  584. INIT_LIST_HEAD(&list);
  585. mutex_lock(&dpm_list_mtx);
  586. while (!list_empty(&dpm_prepared_list)) {
  587. struct device *dev = to_device(dpm_prepared_list.prev);
  588. get_device(dev);
  589. dev->power.in_suspend = false;
  590. list_move(&dev->power.entry, &list);
  591. mutex_unlock(&dpm_list_mtx);
  592. device_complete(dev, state);
  593. mutex_lock(&dpm_list_mtx);
  594. put_device(dev);
  595. }
  596. list_splice(&list, &dpm_list);
  597. mutex_unlock(&dpm_list_mtx);
  598. }
  599. /**
  600. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  601. * @state: PM transition of the system being carried out.
  602. *
  603. * Execute "resume" callbacks for all devices and complete the PM transition of
  604. * the system.
  605. */
  606. void dpm_resume_end(pm_message_t state)
  607. {
  608. might_sleep();
  609. dpm_resume(state);
  610. dpm_complete(state);
  611. }
  612. EXPORT_SYMBOL_GPL(dpm_resume_end);
  613. /*------------------------- Suspend routines -------------------------*/
  614. /**
  615. * resume_event - Return a "resume" message for given "suspend" sleep state.
  616. * @sleep_state: PM message representing a sleep state.
  617. *
  618. * Return a PM message representing the resume event corresponding to given
  619. * sleep state.
  620. */
  621. static pm_message_t resume_event(pm_message_t sleep_state)
  622. {
  623. switch (sleep_state.event) {
  624. case PM_EVENT_SUSPEND:
  625. return PMSG_RESUME;
  626. case PM_EVENT_FREEZE:
  627. case PM_EVENT_QUIESCE:
  628. return PMSG_RECOVER;
  629. case PM_EVENT_HIBERNATE:
  630. return PMSG_RESTORE;
  631. }
  632. return PMSG_ON;
  633. }
  634. /**
  635. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  636. * @dev: Device to handle.
  637. * @state: PM transition of the system being carried out.
  638. *
  639. * The driver of @dev will not receive interrupts while this function is being
  640. * executed.
  641. */
  642. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  643. {
  644. int error;
  645. if (dev->type && dev->type->pm) {
  646. pm_dev_dbg(dev, state, "LATE type ");
  647. error = pm_noirq_op(dev, dev->type->pm, state);
  648. if (error)
  649. return error;
  650. } else if (dev->class && dev->class->pm) {
  651. pm_dev_dbg(dev, state, "LATE class ");
  652. error = pm_noirq_op(dev, dev->class->pm, state);
  653. if (error)
  654. return error;
  655. } else if (dev->bus && dev->bus->pm) {
  656. pm_dev_dbg(dev, state, "LATE ");
  657. error = pm_noirq_op(dev, dev->bus->pm, state);
  658. if (error)
  659. return error;
  660. }
  661. if (dev->pwr_domain) {
  662. pm_dev_dbg(dev, state, "LATE power domain ");
  663. pm_noirq_op(dev, &dev->pwr_domain->ops, state);
  664. }
  665. return 0;
  666. }
  667. /**
  668. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  669. * @state: PM transition of the system being carried out.
  670. *
  671. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  672. * handlers for all non-sysdev devices.
  673. */
  674. int dpm_suspend_noirq(pm_message_t state)
  675. {
  676. ktime_t starttime = ktime_get();
  677. int error = 0;
  678. suspend_device_irqs();
  679. mutex_lock(&dpm_list_mtx);
  680. while (!list_empty(&dpm_suspended_list)) {
  681. struct device *dev = to_device(dpm_suspended_list.prev);
  682. get_device(dev);
  683. mutex_unlock(&dpm_list_mtx);
  684. error = device_suspend_noirq(dev, state);
  685. mutex_lock(&dpm_list_mtx);
  686. if (error) {
  687. pm_dev_err(dev, state, " late", error);
  688. put_device(dev);
  689. break;
  690. }
  691. if (!list_empty(&dev->power.entry))
  692. list_move(&dev->power.entry, &dpm_noirq_list);
  693. put_device(dev);
  694. }
  695. mutex_unlock(&dpm_list_mtx);
  696. if (error)
  697. dpm_resume_noirq(resume_event(state));
  698. else
  699. dpm_show_time(starttime, state, "late");
  700. return error;
  701. }
  702. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  703. /**
  704. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  705. * @dev: Device to suspend.
  706. * @state: PM transition of the system being carried out.
  707. * @cb: Suspend callback to execute.
  708. */
  709. static int legacy_suspend(struct device *dev, pm_message_t state,
  710. int (*cb)(struct device *dev, pm_message_t state))
  711. {
  712. int error;
  713. ktime_t calltime;
  714. calltime = initcall_debug_start(dev);
  715. error = cb(dev, state);
  716. suspend_report_result(cb, error);
  717. initcall_debug_report(dev, calltime, error);
  718. return error;
  719. }
  720. /**
  721. * device_suspend - Execute "suspend" callbacks for given device.
  722. * @dev: Device to handle.
  723. * @state: PM transition of the system being carried out.
  724. * @async: If true, the device is being suspended asynchronously.
  725. */
  726. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  727. {
  728. int error = 0;
  729. dpm_wait_for_children(dev, async);
  730. device_lock(dev);
  731. if (async_error)
  732. goto End;
  733. if (pm_wakeup_pending()) {
  734. async_error = -EBUSY;
  735. goto End;
  736. }
  737. if (dev->type && dev->type->pm) {
  738. pm_dev_dbg(dev, state, "type ");
  739. error = pm_op(dev, dev->type->pm, state);
  740. goto Domain;
  741. }
  742. if (dev->class) {
  743. if (dev->class->pm) {
  744. pm_dev_dbg(dev, state, "class ");
  745. error = pm_op(dev, dev->class->pm, state);
  746. goto Domain;
  747. } else if (dev->class->suspend) {
  748. pm_dev_dbg(dev, state, "legacy class ");
  749. error = legacy_suspend(dev, state, dev->class->suspend);
  750. goto Domain;
  751. }
  752. }
  753. if (dev->bus) {
  754. if (dev->bus->pm) {
  755. pm_dev_dbg(dev, state, "");
  756. error = pm_op(dev, dev->bus->pm, state);
  757. } else if (dev->bus->suspend) {
  758. pm_dev_dbg(dev, state, "legacy ");
  759. error = legacy_suspend(dev, state, dev->bus->suspend);
  760. }
  761. }
  762. Domain:
  763. if (!error && dev->pwr_domain) {
  764. pm_dev_dbg(dev, state, "power domain ");
  765. pm_op(dev, &dev->pwr_domain->ops, state);
  766. }
  767. End:
  768. device_unlock(dev);
  769. complete_all(&dev->power.completion);
  770. if (error)
  771. async_error = error;
  772. return error;
  773. }
  774. static void async_suspend(void *data, async_cookie_t cookie)
  775. {
  776. struct device *dev = (struct device *)data;
  777. int error;
  778. error = __device_suspend(dev, pm_transition, true);
  779. if (error)
  780. pm_dev_err(dev, pm_transition, " async", error);
  781. put_device(dev);
  782. }
  783. static int device_suspend(struct device *dev)
  784. {
  785. INIT_COMPLETION(dev->power.completion);
  786. if (pm_async_enabled && dev->power.async_suspend) {
  787. get_device(dev);
  788. async_schedule(async_suspend, dev);
  789. return 0;
  790. }
  791. return __device_suspend(dev, pm_transition, false);
  792. }
  793. /**
  794. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  795. * @state: PM transition of the system being carried out.
  796. */
  797. static int dpm_suspend(pm_message_t state)
  798. {
  799. ktime_t starttime = ktime_get();
  800. int error = 0;
  801. mutex_lock(&dpm_list_mtx);
  802. pm_transition = state;
  803. async_error = 0;
  804. while (!list_empty(&dpm_prepared_list)) {
  805. struct device *dev = to_device(dpm_prepared_list.prev);
  806. get_device(dev);
  807. mutex_unlock(&dpm_list_mtx);
  808. error = device_suspend(dev);
  809. mutex_lock(&dpm_list_mtx);
  810. if (error) {
  811. pm_dev_err(dev, state, "", error);
  812. put_device(dev);
  813. break;
  814. }
  815. if (!list_empty(&dev->power.entry))
  816. list_move(&dev->power.entry, &dpm_suspended_list);
  817. put_device(dev);
  818. if (async_error)
  819. break;
  820. }
  821. mutex_unlock(&dpm_list_mtx);
  822. async_synchronize_full();
  823. if (!error)
  824. error = async_error;
  825. if (!error)
  826. dpm_show_time(starttime, state, NULL);
  827. return error;
  828. }
  829. /**
  830. * device_prepare - Prepare a device for system power transition.
  831. * @dev: Device to handle.
  832. * @state: PM transition of the system being carried out.
  833. *
  834. * Execute the ->prepare() callback(s) for given device. No new children of the
  835. * device may be registered after this function has returned.
  836. */
  837. static int device_prepare(struct device *dev, pm_message_t state)
  838. {
  839. int error = 0;
  840. device_lock(dev);
  841. if (dev->type && dev->type->pm) {
  842. pm_dev_dbg(dev, state, "preparing type ");
  843. if (dev->type->pm->prepare)
  844. error = dev->type->pm->prepare(dev);
  845. suspend_report_result(dev->type->pm->prepare, error);
  846. if (error)
  847. goto End;
  848. } else if (dev->class && dev->class->pm) {
  849. pm_dev_dbg(dev, state, "preparing class ");
  850. if (dev->class->pm->prepare)
  851. error = dev->class->pm->prepare(dev);
  852. suspend_report_result(dev->class->pm->prepare, error);
  853. if (error)
  854. goto End;
  855. } else if (dev->bus && dev->bus->pm) {
  856. pm_dev_dbg(dev, state, "preparing ");
  857. if (dev->bus->pm->prepare)
  858. error = dev->bus->pm->prepare(dev);
  859. suspend_report_result(dev->bus->pm->prepare, error);
  860. if (error)
  861. goto End;
  862. }
  863. if (dev->pwr_domain && dev->pwr_domain->ops.prepare) {
  864. pm_dev_dbg(dev, state, "preparing power domain ");
  865. dev->pwr_domain->ops.prepare(dev);
  866. }
  867. End:
  868. device_unlock(dev);
  869. return error;
  870. }
  871. /**
  872. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  873. * @state: PM transition of the system being carried out.
  874. *
  875. * Execute the ->prepare() callback(s) for all devices.
  876. */
  877. static int dpm_prepare(pm_message_t state)
  878. {
  879. int error = 0;
  880. mutex_lock(&dpm_list_mtx);
  881. while (!list_empty(&dpm_list)) {
  882. struct device *dev = to_device(dpm_list.next);
  883. get_device(dev);
  884. mutex_unlock(&dpm_list_mtx);
  885. pm_runtime_get_noresume(dev);
  886. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  887. pm_wakeup_event(dev, 0);
  888. pm_runtime_put_sync(dev);
  889. error = pm_wakeup_pending() ?
  890. -EBUSY : device_prepare(dev, state);
  891. mutex_lock(&dpm_list_mtx);
  892. if (error) {
  893. if (error == -EAGAIN) {
  894. put_device(dev);
  895. error = 0;
  896. continue;
  897. }
  898. printk(KERN_INFO "PM: Device %s not prepared "
  899. "for power transition: code %d\n",
  900. dev_name(dev), error);
  901. put_device(dev);
  902. break;
  903. }
  904. dev->power.in_suspend = true;
  905. if (!list_empty(&dev->power.entry))
  906. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  907. put_device(dev);
  908. }
  909. mutex_unlock(&dpm_list_mtx);
  910. return error;
  911. }
  912. /**
  913. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  914. * @state: PM transition of the system being carried out.
  915. *
  916. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  917. * callbacks for them.
  918. */
  919. int dpm_suspend_start(pm_message_t state)
  920. {
  921. int error;
  922. might_sleep();
  923. error = dpm_prepare(state);
  924. if (!error)
  925. error = dpm_suspend(state);
  926. return error;
  927. }
  928. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  929. void __suspend_report_result(const char *function, void *fn, int ret)
  930. {
  931. if (ret)
  932. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  933. }
  934. EXPORT_SYMBOL_GPL(__suspend_report_result);
  935. /**
  936. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  937. * @dev: Device to wait for.
  938. * @subordinate: Device that needs to wait for @dev.
  939. */
  940. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  941. {
  942. dpm_wait(dev, subordinate->power.async_suspend);
  943. return async_error;
  944. }
  945. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);