main.c 27 KB

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