main.c 27 KB

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