main.c 27 KB

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