main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. }
  400. TRACE_RESUME(error);
  401. return error;
  402. }
  403. /**
  404. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  405. * @state: PM transition of the system being carried out.
  406. *
  407. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  408. * enable device drivers to receive interrupts.
  409. */
  410. void dpm_resume_noirq(pm_message_t state)
  411. {
  412. struct device *dev;
  413. ktime_t starttime = ktime_get();
  414. mutex_lock(&dpm_list_mtx);
  415. transition_started = false;
  416. list_for_each_entry(dev, &dpm_list, power.entry)
  417. if (dev->power.status > DPM_OFF) {
  418. int error;
  419. dev->power.status = DPM_OFF;
  420. error = device_resume_noirq(dev, state);
  421. if (error)
  422. pm_dev_err(dev, state, " early", error);
  423. }
  424. mutex_unlock(&dpm_list_mtx);
  425. dpm_show_time(starttime, state, "early");
  426. resume_device_irqs();
  427. }
  428. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  429. /**
  430. * legacy_resume - Execute a legacy (bus or class) resume callback for device.
  431. * @dev: Device to resume.
  432. * @cb: Resume callback to execute.
  433. */
  434. static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
  435. {
  436. int error;
  437. ktime_t calltime;
  438. calltime = initcall_debug_start(dev);
  439. error = cb(dev);
  440. suspend_report_result(cb, error);
  441. initcall_debug_report(dev, calltime, error);
  442. return error;
  443. }
  444. /**
  445. * device_resume - Execute "resume" callbacks for given device.
  446. * @dev: Device to handle.
  447. * @state: PM transition of the system being carried out.
  448. * @async: If true, the device is being resumed asynchronously.
  449. */
  450. static int device_resume(struct device *dev, pm_message_t state, bool async)
  451. {
  452. int error = 0;
  453. TRACE_DEVICE(dev);
  454. TRACE_RESUME(0);
  455. dpm_wait(dev->parent, async);
  456. device_lock(dev);
  457. dev->power.status = DPM_RESUMING;
  458. if (dev->bus) {
  459. if (dev->bus->pm) {
  460. pm_dev_dbg(dev, state, "");
  461. error = pm_op(dev, dev->bus->pm, state);
  462. } else if (dev->bus->resume) {
  463. pm_dev_dbg(dev, state, "legacy ");
  464. error = legacy_resume(dev, dev->bus->resume);
  465. }
  466. if (error)
  467. goto End;
  468. }
  469. if (dev->type) {
  470. if (dev->type->pm) {
  471. pm_dev_dbg(dev, state, "type ");
  472. error = pm_op(dev, dev->type->pm, state);
  473. }
  474. if (error)
  475. goto End;
  476. }
  477. if (dev->class) {
  478. if (dev->class->pm) {
  479. pm_dev_dbg(dev, state, "class ");
  480. error = pm_op(dev, dev->class->pm, state);
  481. } else if (dev->class->resume) {
  482. pm_dev_dbg(dev, state, "legacy class ");
  483. error = legacy_resume(dev, dev->class->resume);
  484. }
  485. }
  486. End:
  487. device_unlock(dev);
  488. complete_all(&dev->power.completion);
  489. TRACE_RESUME(error);
  490. return error;
  491. }
  492. static void async_resume(void *data, async_cookie_t cookie)
  493. {
  494. struct device *dev = (struct device *)data;
  495. int error;
  496. error = device_resume(dev, pm_transition, true);
  497. if (error)
  498. pm_dev_err(dev, pm_transition, " async", error);
  499. put_device(dev);
  500. }
  501. static bool is_async(struct device *dev)
  502. {
  503. return dev->power.async_suspend && pm_async_enabled
  504. && !pm_trace_is_enabled();
  505. }
  506. /**
  507. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  508. * @state: PM transition of the system being carried out.
  509. *
  510. * Execute the appropriate "resume" callback for all devices whose status
  511. * indicates that they are suspended.
  512. */
  513. static void dpm_resume(pm_message_t state)
  514. {
  515. struct list_head list;
  516. struct device *dev;
  517. ktime_t starttime = ktime_get();
  518. INIT_LIST_HEAD(&list);
  519. mutex_lock(&dpm_list_mtx);
  520. pm_transition = state;
  521. list_for_each_entry(dev, &dpm_list, power.entry) {
  522. if (dev->power.status < DPM_OFF)
  523. continue;
  524. INIT_COMPLETION(dev->power.completion);
  525. if (is_async(dev)) {
  526. get_device(dev);
  527. async_schedule(async_resume, dev);
  528. }
  529. }
  530. while (!list_empty(&dpm_list)) {
  531. dev = to_device(dpm_list.next);
  532. get_device(dev);
  533. if (dev->power.status >= DPM_OFF && !is_async(dev)) {
  534. int error;
  535. mutex_unlock(&dpm_list_mtx);
  536. error = device_resume(dev, state, false);
  537. mutex_lock(&dpm_list_mtx);
  538. if (error)
  539. pm_dev_err(dev, state, "", error);
  540. } else if (dev->power.status == DPM_SUSPENDING) {
  541. /* Allow new children of the device to be registered */
  542. dev->power.status = DPM_RESUMING;
  543. }
  544. if (!list_empty(&dev->power.entry))
  545. list_move_tail(&dev->power.entry, &list);
  546. put_device(dev);
  547. }
  548. list_splice(&list, &dpm_list);
  549. mutex_unlock(&dpm_list_mtx);
  550. async_synchronize_full();
  551. dpm_show_time(starttime, state, NULL);
  552. }
  553. /**
  554. * device_complete - Complete a PM transition for given device.
  555. * @dev: Device to handle.
  556. * @state: PM transition of the system being carried out.
  557. */
  558. static void device_complete(struct device *dev, pm_message_t state)
  559. {
  560. device_lock(dev);
  561. if (dev->class && dev->class->pm && dev->class->pm->complete) {
  562. pm_dev_dbg(dev, state, "completing class ");
  563. dev->class->pm->complete(dev);
  564. }
  565. if (dev->type && dev->type->pm && dev->type->pm->complete) {
  566. pm_dev_dbg(dev, state, "completing type ");
  567. dev->type->pm->complete(dev);
  568. }
  569. if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
  570. pm_dev_dbg(dev, state, "completing ");
  571. dev->bus->pm->complete(dev);
  572. }
  573. device_unlock(dev);
  574. }
  575. /**
  576. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  577. * @state: PM transition of the system being carried out.
  578. *
  579. * Execute the ->complete() callbacks for all devices whose PM status is not
  580. * DPM_ON (this allows new devices to be registered).
  581. */
  582. static void dpm_complete(pm_message_t state)
  583. {
  584. struct list_head list;
  585. INIT_LIST_HEAD(&list);
  586. mutex_lock(&dpm_list_mtx);
  587. transition_started = false;
  588. while (!list_empty(&dpm_list)) {
  589. struct device *dev = to_device(dpm_list.prev);
  590. get_device(dev);
  591. if (dev->power.status > DPM_ON) {
  592. dev->power.status = DPM_ON;
  593. mutex_unlock(&dpm_list_mtx);
  594. device_complete(dev, state);
  595. pm_runtime_put_sync(dev);
  596. mutex_lock(&dpm_list_mtx);
  597. }
  598. if (!list_empty(&dev->power.entry))
  599. list_move(&dev->power.entry, &list);
  600. put_device(dev);
  601. }
  602. list_splice(&list, &dpm_list);
  603. mutex_unlock(&dpm_list_mtx);
  604. }
  605. /**
  606. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  607. * @state: PM transition of the system being carried out.
  608. *
  609. * Execute "resume" callbacks for all devices and complete the PM transition of
  610. * the system.
  611. */
  612. void dpm_resume_end(pm_message_t state)
  613. {
  614. might_sleep();
  615. dpm_resume(state);
  616. dpm_complete(state);
  617. }
  618. EXPORT_SYMBOL_GPL(dpm_resume_end);
  619. /*------------------------- Suspend routines -------------------------*/
  620. /**
  621. * resume_event - Return a "resume" message for given "suspend" sleep state.
  622. * @sleep_state: PM message representing a sleep state.
  623. *
  624. * Return a PM message representing the resume event corresponding to given
  625. * sleep state.
  626. */
  627. static pm_message_t resume_event(pm_message_t sleep_state)
  628. {
  629. switch (sleep_state.event) {
  630. case PM_EVENT_SUSPEND:
  631. return PMSG_RESUME;
  632. case PM_EVENT_FREEZE:
  633. case PM_EVENT_QUIESCE:
  634. return PMSG_RECOVER;
  635. case PM_EVENT_HIBERNATE:
  636. return PMSG_RESTORE;
  637. }
  638. return PMSG_ON;
  639. }
  640. /**
  641. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  642. * @dev: Device to handle.
  643. * @state: PM transition of the system being carried out.
  644. *
  645. * The driver of @dev will not receive interrupts while this function is being
  646. * executed.
  647. */
  648. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  649. {
  650. int error = 0;
  651. if (dev->bus && dev->bus->pm) {
  652. pm_dev_dbg(dev, state, "LATE ");
  653. error = pm_noirq_op(dev, dev->bus->pm, state);
  654. }
  655. return error;
  656. }
  657. /**
  658. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  659. * @state: PM transition of the system being carried out.
  660. *
  661. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  662. * handlers for all non-sysdev devices.
  663. */
  664. int dpm_suspend_noirq(pm_message_t state)
  665. {
  666. struct device *dev;
  667. ktime_t starttime = ktime_get();
  668. int error = 0;
  669. suspend_device_irqs();
  670. mutex_lock(&dpm_list_mtx);
  671. list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
  672. error = device_suspend_noirq(dev, state);
  673. if (error) {
  674. pm_dev_err(dev, state, " late", error);
  675. break;
  676. }
  677. dev->power.status = DPM_OFF_IRQ;
  678. }
  679. mutex_unlock(&dpm_list_mtx);
  680. if (error)
  681. dpm_resume_noirq(resume_event(state));
  682. else
  683. dpm_show_time(starttime, state, "late");
  684. return error;
  685. }
  686. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  687. /**
  688. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  689. * @dev: Device to suspend.
  690. * @state: PM transition of the system being carried out.
  691. * @cb: Suspend callback to execute.
  692. */
  693. static int legacy_suspend(struct device *dev, pm_message_t state,
  694. int (*cb)(struct device *dev, pm_message_t state))
  695. {
  696. int error;
  697. ktime_t calltime;
  698. calltime = initcall_debug_start(dev);
  699. error = cb(dev, state);
  700. suspend_report_result(cb, error);
  701. initcall_debug_report(dev, calltime, error);
  702. return error;
  703. }
  704. static int async_error;
  705. /**
  706. * device_suspend - Execute "suspend" callbacks for given device.
  707. * @dev: Device to handle.
  708. * @state: PM transition of the system being carried out.
  709. * @async: If true, the device is being suspended asynchronously.
  710. */
  711. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  712. {
  713. int error = 0;
  714. dpm_wait_for_children(dev, async);
  715. device_lock(dev);
  716. if (async_error)
  717. goto End;
  718. if (dev->class) {
  719. if (dev->class->pm) {
  720. pm_dev_dbg(dev, state, "class ");
  721. error = pm_op(dev, dev->class->pm, state);
  722. } else if (dev->class->suspend) {
  723. pm_dev_dbg(dev, state, "legacy class ");
  724. error = legacy_suspend(dev, state, dev->class->suspend);
  725. }
  726. if (error)
  727. goto End;
  728. }
  729. if (dev->type) {
  730. if (dev->type->pm) {
  731. pm_dev_dbg(dev, state, "type ");
  732. error = pm_op(dev, dev->type->pm, state);
  733. }
  734. if (error)
  735. goto End;
  736. }
  737. if (dev->bus) {
  738. if (dev->bus->pm) {
  739. pm_dev_dbg(dev, state, "");
  740. error = pm_op(dev, dev->bus->pm, state);
  741. } else if (dev->bus->suspend) {
  742. pm_dev_dbg(dev, state, "legacy ");
  743. error = legacy_suspend(dev, state, dev->bus->suspend);
  744. }
  745. }
  746. if (!error)
  747. dev->power.status = DPM_OFF;
  748. End:
  749. device_unlock(dev);
  750. complete_all(&dev->power.completion);
  751. return error;
  752. }
  753. static void async_suspend(void *data, async_cookie_t cookie)
  754. {
  755. struct device *dev = (struct device *)data;
  756. int error;
  757. error = __device_suspend(dev, pm_transition, true);
  758. if (error) {
  759. pm_dev_err(dev, pm_transition, " async", error);
  760. async_error = error;
  761. }
  762. put_device(dev);
  763. }
  764. static int device_suspend(struct device *dev)
  765. {
  766. INIT_COMPLETION(dev->power.completion);
  767. if (pm_async_enabled && dev->power.async_suspend) {
  768. get_device(dev);
  769. async_schedule(async_suspend, dev);
  770. return 0;
  771. }
  772. return __device_suspend(dev, pm_transition, false);
  773. }
  774. /**
  775. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  776. * @state: PM transition of the system being carried out.
  777. */
  778. static int dpm_suspend(pm_message_t state)
  779. {
  780. struct list_head list;
  781. ktime_t starttime = ktime_get();
  782. int error = 0;
  783. INIT_LIST_HEAD(&list);
  784. mutex_lock(&dpm_list_mtx);
  785. pm_transition = state;
  786. async_error = 0;
  787. while (!list_empty(&dpm_list)) {
  788. struct device *dev = to_device(dpm_list.prev);
  789. get_device(dev);
  790. mutex_unlock(&dpm_list_mtx);
  791. error = device_suspend(dev);
  792. mutex_lock(&dpm_list_mtx);
  793. if (error) {
  794. pm_dev_err(dev, state, "", error);
  795. put_device(dev);
  796. break;
  797. }
  798. if (!list_empty(&dev->power.entry))
  799. list_move(&dev->power.entry, &list);
  800. put_device(dev);
  801. if (async_error)
  802. break;
  803. }
  804. list_splice(&list, dpm_list.prev);
  805. mutex_unlock(&dpm_list_mtx);
  806. async_synchronize_full();
  807. if (!error)
  808. error = async_error;
  809. if (!error)
  810. dpm_show_time(starttime, state, NULL);
  811. return error;
  812. }
  813. /**
  814. * device_prepare - Prepare a device for system power transition.
  815. * @dev: Device to handle.
  816. * @state: PM transition of the system being carried out.
  817. *
  818. * Execute the ->prepare() callback(s) for given device. No new children of the
  819. * device may be registered after this function has returned.
  820. */
  821. static int device_prepare(struct device *dev, pm_message_t state)
  822. {
  823. int error = 0;
  824. device_lock(dev);
  825. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  826. pm_dev_dbg(dev, state, "preparing ");
  827. error = dev->bus->pm->prepare(dev);
  828. suspend_report_result(dev->bus->pm->prepare, error);
  829. if (error)
  830. goto End;
  831. }
  832. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  833. pm_dev_dbg(dev, state, "preparing type ");
  834. error = dev->type->pm->prepare(dev);
  835. suspend_report_result(dev->type->pm->prepare, error);
  836. if (error)
  837. goto End;
  838. }
  839. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  840. pm_dev_dbg(dev, state, "preparing class ");
  841. error = dev->class->pm->prepare(dev);
  842. suspend_report_result(dev->class->pm->prepare, error);
  843. }
  844. End:
  845. device_unlock(dev);
  846. return error;
  847. }
  848. /**
  849. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  850. * @state: PM transition of the system being carried out.
  851. *
  852. * Execute the ->prepare() callback(s) for all devices.
  853. */
  854. static int dpm_prepare(pm_message_t state)
  855. {
  856. struct list_head list;
  857. int error = 0;
  858. INIT_LIST_HEAD(&list);
  859. mutex_lock(&dpm_list_mtx);
  860. transition_started = true;
  861. while (!list_empty(&dpm_list)) {
  862. struct device *dev = to_device(dpm_list.next);
  863. get_device(dev);
  864. dev->power.status = DPM_PREPARING;
  865. mutex_unlock(&dpm_list_mtx);
  866. pm_runtime_get_noresume(dev);
  867. if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
  868. /* Wake-up requested during system sleep transition. */
  869. pm_runtime_put_sync(dev);
  870. error = -EBUSY;
  871. } else {
  872. error = device_prepare(dev, state);
  873. }
  874. mutex_lock(&dpm_list_mtx);
  875. if (error) {
  876. dev->power.status = DPM_ON;
  877. if (error == -EAGAIN) {
  878. put_device(dev);
  879. error = 0;
  880. continue;
  881. }
  882. printk(KERN_ERR "PM: Failed to prepare device %s "
  883. "for power transition: error %d\n",
  884. kobject_name(&dev->kobj), error);
  885. put_device(dev);
  886. break;
  887. }
  888. dev->power.status = DPM_SUSPENDING;
  889. if (!list_empty(&dev->power.entry))
  890. list_move_tail(&dev->power.entry, &list);
  891. put_device(dev);
  892. }
  893. list_splice(&list, &dpm_list);
  894. mutex_unlock(&dpm_list_mtx);
  895. return error;
  896. }
  897. /**
  898. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  899. * @state: PM transition of the system being carried out.
  900. *
  901. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  902. * callbacks for them.
  903. */
  904. int dpm_suspend_start(pm_message_t state)
  905. {
  906. int error;
  907. might_sleep();
  908. error = dpm_prepare(state);
  909. if (!error)
  910. error = dpm_suspend(state);
  911. return error;
  912. }
  913. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  914. void __suspend_report_result(const char *function, void *fn, int ret)
  915. {
  916. if (ret)
  917. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  918. }
  919. EXPORT_SYMBOL_GPL(__suspend_report_result);
  920. /**
  921. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  922. * @dev: Device to wait for.
  923. * @subordinate: Device that needs to wait for @dev.
  924. */
  925. void device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  926. {
  927. dpm_wait(dev, subordinate->power.async_suspend);
  928. }
  929. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);