main.c 27 KB

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