main.c 26 KB

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