main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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 (pm_wakeup_pending()) {
  771. async_error = -EBUSY;
  772. goto End;
  773. }
  774. if (dev->class) {
  775. if (dev->class->pm) {
  776. pm_dev_dbg(dev, state, "class ");
  777. error = pm_op(dev, dev->class->pm, state);
  778. } else if (dev->class->suspend) {
  779. pm_dev_dbg(dev, state, "legacy class ");
  780. error = legacy_suspend(dev, state, dev->class->suspend);
  781. }
  782. if (error)
  783. goto End;
  784. }
  785. if (dev->type) {
  786. if (dev->type->pm) {
  787. pm_dev_dbg(dev, state, "type ");
  788. error = pm_op(dev, dev->type->pm, state);
  789. }
  790. if (error)
  791. goto End;
  792. }
  793. if (dev->bus) {
  794. if (dev->bus->pm) {
  795. pm_dev_dbg(dev, state, "");
  796. error = pm_op(dev, dev->bus->pm, state);
  797. } else if (dev->bus->suspend) {
  798. pm_dev_dbg(dev, state, "legacy ");
  799. error = legacy_suspend(dev, state, dev->bus->suspend);
  800. }
  801. }
  802. if (!error)
  803. dev->power.status = DPM_OFF;
  804. End:
  805. device_unlock(dev);
  806. complete_all(&dev->power.completion);
  807. if (error)
  808. async_error = error;
  809. return error;
  810. }
  811. static void async_suspend(void *data, async_cookie_t cookie)
  812. {
  813. struct device *dev = (struct device *)data;
  814. int error;
  815. error = __device_suspend(dev, pm_transition, true);
  816. if (error)
  817. pm_dev_err(dev, pm_transition, " async", error);
  818. put_device(dev);
  819. }
  820. static int device_suspend(struct device *dev)
  821. {
  822. INIT_COMPLETION(dev->power.completion);
  823. if (pm_async_enabled && dev->power.async_suspend) {
  824. get_device(dev);
  825. async_schedule(async_suspend, dev);
  826. return 0;
  827. }
  828. return __device_suspend(dev, pm_transition, false);
  829. }
  830. /**
  831. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  832. * @state: PM transition of the system being carried out.
  833. */
  834. static int dpm_suspend(pm_message_t state)
  835. {
  836. struct list_head list;
  837. ktime_t starttime = ktime_get();
  838. int error = 0;
  839. INIT_LIST_HEAD(&list);
  840. mutex_lock(&dpm_list_mtx);
  841. pm_transition = state;
  842. async_error = 0;
  843. while (!list_empty(&dpm_list)) {
  844. struct device *dev = to_device(dpm_list.prev);
  845. get_device(dev);
  846. mutex_unlock(&dpm_list_mtx);
  847. error = device_suspend(dev);
  848. mutex_lock(&dpm_list_mtx);
  849. if (error) {
  850. pm_dev_err(dev, state, "", error);
  851. put_device(dev);
  852. break;
  853. }
  854. if (!list_empty(&dev->power.entry))
  855. list_move(&dev->power.entry, &list);
  856. put_device(dev);
  857. if (async_error)
  858. break;
  859. }
  860. list_splice(&list, dpm_list.prev);
  861. mutex_unlock(&dpm_list_mtx);
  862. async_synchronize_full();
  863. if (!error)
  864. error = async_error;
  865. if (!error)
  866. dpm_show_time(starttime, state, NULL);
  867. return error;
  868. }
  869. /**
  870. * device_prepare - Prepare a device for system power transition.
  871. * @dev: Device to handle.
  872. * @state: PM transition of the system being carried out.
  873. *
  874. * Execute the ->prepare() callback(s) for given device. No new children of the
  875. * device may be registered after this function has returned.
  876. */
  877. static int device_prepare(struct device *dev, pm_message_t state)
  878. {
  879. int error = 0;
  880. device_lock(dev);
  881. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  882. pm_dev_dbg(dev, state, "preparing ");
  883. error = dev->bus->pm->prepare(dev);
  884. suspend_report_result(dev->bus->pm->prepare, error);
  885. if (error)
  886. goto End;
  887. }
  888. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  889. pm_dev_dbg(dev, state, "preparing type ");
  890. error = dev->type->pm->prepare(dev);
  891. suspend_report_result(dev->type->pm->prepare, error);
  892. if (error)
  893. goto End;
  894. }
  895. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  896. pm_dev_dbg(dev, state, "preparing class ");
  897. error = dev->class->pm->prepare(dev);
  898. suspend_report_result(dev->class->pm->prepare, error);
  899. }
  900. End:
  901. device_unlock(dev);
  902. return error;
  903. }
  904. /**
  905. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  906. * @state: PM transition of the system being carried out.
  907. *
  908. * Execute the ->prepare() callback(s) for all devices.
  909. */
  910. static int dpm_prepare(pm_message_t state)
  911. {
  912. struct list_head list;
  913. int error = 0;
  914. INIT_LIST_HEAD(&list);
  915. mutex_lock(&dpm_list_mtx);
  916. transition_started = true;
  917. while (!list_empty(&dpm_list)) {
  918. struct device *dev = to_device(dpm_list.next);
  919. get_device(dev);
  920. dev->power.status = DPM_PREPARING;
  921. mutex_unlock(&dpm_list_mtx);
  922. pm_runtime_get_noresume(dev);
  923. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  924. pm_wakeup_event(dev, 0);
  925. if (pm_wakeup_pending()) {
  926. pm_runtime_put_sync(dev);
  927. error = -EBUSY;
  928. } else {
  929. error = device_prepare(dev, state);
  930. }
  931. mutex_lock(&dpm_list_mtx);
  932. if (error) {
  933. dev->power.status = DPM_ON;
  934. if (error == -EAGAIN) {
  935. put_device(dev);
  936. error = 0;
  937. continue;
  938. }
  939. printk(KERN_INFO "PM: Device %s not prepared "
  940. "for power transition: code %d\n",
  941. kobject_name(&dev->kobj), error);
  942. put_device(dev);
  943. break;
  944. }
  945. dev->power.status = DPM_SUSPENDING;
  946. if (!list_empty(&dev->power.entry))
  947. list_move_tail(&dev->power.entry, &list);
  948. put_device(dev);
  949. }
  950. list_splice(&list, &dpm_list);
  951. mutex_unlock(&dpm_list_mtx);
  952. return error;
  953. }
  954. /**
  955. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  956. * @state: PM transition of the system being carried out.
  957. *
  958. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  959. * callbacks for them.
  960. */
  961. int dpm_suspend_start(pm_message_t state)
  962. {
  963. int error;
  964. might_sleep();
  965. error = dpm_prepare(state);
  966. if (!error)
  967. error = dpm_suspend(state);
  968. return error;
  969. }
  970. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  971. void __suspend_report_result(const char *function, void *fn, int ret)
  972. {
  973. if (ret)
  974. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  975. }
  976. EXPORT_SYMBOL_GPL(__suspend_report_result);
  977. /**
  978. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  979. * @dev: Device to wait for.
  980. * @subordinate: Device that needs to wait for @dev.
  981. */
  982. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  983. {
  984. dpm_wait(dev, subordinate->power.async_suspend);
  985. return async_error;
  986. }
  987. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);